From: Louis Chauvet <louis.chauvet@bootlin.com>
To: "Haneen Mohammed" <hamohammed.sa@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Melissa Wen" <melissa.srw@gmail.com>,
"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
"Maxime Ripard" <mripard@kernel.org>,
"Thomas Zimmermann" <tzimmermann@suse.de>,
"David Airlie" <airlied@gmail.com>,
jose.exposito89@gmail.com, "Jonathan Corbet" <corbet@lwn.net>,
"Oarora Etimis" <oaroraetimis@gmail.com>,
"Harry Wentland" <harry.wentland@amd.com>,
"Lucas De Marchi" <demarchi@kernel.org>,
"Ville Syrjälä" <ville.syrjala@linux.intel.com>,
"Randy Dunlap" <rdunlap@infradead.org>,
"Nick Desaulniers" <ndesaulniers@google.com>,
"Louis Chauvet" <louis.chauvet@bootlin.com>,
"Shuah Khan" <skhan@linuxfoundation.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Nathan Chancellor" <nathan@kernel.org>,
"Nick Desaulniers" <nick.desaulniers+lkml@gmail.com>,
"Bill Wendling" <morbo@google.com>,
"Justin Stitt" <justinstitt@google.com>
Cc: Luca Ceresoli <luca.ceresoli@bootlin.com>,
Kory Maincent <kory.maincent@bootlin.com>,
sebastian.wick@redhat.com, victoria@system76.com,
thomas.petazzoni@bootlin.com, dri-devel@lists.freedesktop.org,
linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org,
llvm@lists.linux.dev
Subject: [PATCH v7 11/43] drm/vkms: Reference-count vkms_config and vkms_config_connector
Date: Thu, 26 Nov 2026 12:28:54 +0100 [thread overview]
Message-ID: <20261126-vkms-all-config-v7-11-21194cda2468@bootlin.com> (raw)
In-Reply-To: <20261126-vkms-all-config-v7-0-21194cda2468@bootlin.com>
The configuration objects are referenced from places with independent
lifetimes: connectors keep a pointer to their vkms_config_connector, the
DRM device outlives the configfs view, and userspace can delete a
configfs directory while the device is still running. Freeing a
configuration when its configfs representation disappears can therefore
lead to use-after-free.
Add explicit get/put references to vkms_config and vkms_config_connector:
a configuration destroyed while still referenced is detached and kept
alive until its last reference is dropped.
The connectors list owns the initial reference of each connector (taken by
kref_init() in vkms_config_create_connector()). A connector does not take
a reference on its configuration at creation time: the configuration
already owns the connector through config->connectors, so the opposite
reference would create a cycle that plain krefs cannot reclaim. Instead,
vkms_config_destroy_connector() pins the configuration when the connector
is detached, so a connector which outlives its configuration keeps it
alive until its own last put.
Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
---
drivers/gpu/drm/vkms/vkms_config.c | 74 ++++++++++++++++++++++++++++++++++----
drivers/gpu/drm/vkms/vkms_config.h | 48 +++++++++++++++++++++++++
drivers/gpu/drm/vkms/vkms_drv.c | 6 +++-
3 files changed, 121 insertions(+), 7 deletions(-)
diff --git a/drivers/gpu/drm/vkms/vkms_config.c b/drivers/gpu/drm/vkms/vkms_config.c
index 6699ddae7126..8cd4fe6551fb 100644
--- a/drivers/gpu/drm/vkms/vkms_config.c
+++ b/drivers/gpu/drm/vkms/vkms_config.c
@@ -9,6 +9,8 @@
#include "vkms_config.h"
+static void vkms_config_connector_release(struct kref *ref);
+
struct vkms_config *vkms_config_create(const char *dev_name)
{
struct vkms_config *config;
@@ -27,6 +29,8 @@ struct vkms_config *vkms_config_create(const char *dev_name)
INIT_LIST_HEAD(&config->crtcs);
INIT_LIST_HEAD(&config->encoders);
INIT_LIST_HEAD(&config->connectors);
+ /* refcnt starts at 1: the caller owns this initial reference */
+ kref_init(&config->refcnt);
mutex_init(&config->lock);
return config;
@@ -112,8 +116,9 @@ struct vkms_config *vkms_config_default_create(bool enable_cursor,
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_default_create);
-void vkms_config_destroy(struct vkms_config *config)
+static void vkms_config_release(struct kref *ref)
{
+ struct vkms_config *config = container_of(ref, struct vkms_config, refcnt);
struct vkms_config_plane *plane_cfg, *plane_tmp;
struct vkms_config_crtc *crtc_cfg, *crtc_tmp;
struct vkms_config_encoder *encoder_cfg, *encoder_tmp;
@@ -128,12 +133,31 @@ void vkms_config_destroy(struct vkms_config *config)
list_for_each_entry_safe(encoder_cfg, encoder_tmp, &config->encoders, link)
vkms_config_destroy_encoder(config, encoder_cfg);
- list_for_each_entry_safe(connector_cfg, connector_tmp, &config->connectors, link)
- vkms_config_destroy_connector(connector_cfg);
+ list_for_each_entry_safe(connector_cfg, connector_tmp, &config->connectors, link) {
+ /* The config is dying: drop the list's reference on each connector */
+ vkms_config_connector_put(connector_cfg);
+ }
mutex_destroy(&config->lock);
kfree_const(config->dev_name);
- kfree(config);
+ kfree_rcu(config, rcu);
+}
+
+void vkms_config_get(struct vkms_config *config)
+{
+ kref_get(&config->refcnt);
+}
+EXPORT_SYMBOL_IF_KUNIT(vkms_config_get);
+
+void vkms_config_put(struct vkms_config *config)
+{
+ kref_put(&config->refcnt, vkms_config_release);
+}
+EXPORT_SYMBOL_IF_KUNIT(vkms_config_put);
+
+void vkms_config_destroy(struct vkms_config *config)
+{
+ vkms_config_put(config);
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_destroy);
@@ -611,6 +635,8 @@ struct vkms_config_connector *vkms_config_create_connector(struct vkms_config *c
connector_cfg->config = config;
connector_cfg->status = connector_status_connected;
xa_init_flags(&connector_cfg->possible_encoders, XA_FLAGS_ALLOC);
+ /* refcnt starts at 1: this reference is owned by config->connectors */
+ kref_init(&connector_cfg->refcnt);
list_add_tail_rcu(&connector_cfg->link, &config->connectors);
@@ -618,12 +644,48 @@ struct vkms_config_connector *vkms_config_create_connector(struct vkms_config *c
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_create_connector);
-void vkms_config_destroy_connector(struct vkms_config_connector *connector_cfg)
+static void vkms_config_connector_release(struct kref *ref)
{
+ struct vkms_config_connector *connector_cfg =
+ container_of(ref, struct vkms_config_connector, refcnt);
xa_destroy(&connector_cfg->possible_encoders);
- list_del_rcu(&connector_cfg->link);
+ if (connector_cfg->unlinked) {
+ /* Detached by vkms_config_destroy_connector(): drop its config pin */
+ vkms_config_put(connector_cfg->config);
+ } else {
+ /* The config is dying and is draining us: just unlink */
+ list_del_init(&connector_cfg->link);
+ }
kfree_rcu(connector_cfg, rcu);
}
+
+struct vkms_config_connector *vkms_config_connector_get(struct vkms_config_connector *connector_cfg)
+{
+ kref_get(&connector_cfg->refcnt);
+ return connector_cfg;
+}
+EXPORT_SYMBOL_IF_KUNIT(vkms_config_connector_get);
+
+void vkms_config_connector_put(struct vkms_config_connector *connector_cfg)
+{
+ kref_put(&connector_cfg->refcnt, vkms_config_connector_release);
+}
+EXPORT_SYMBOL_IF_KUNIT(vkms_config_connector_put);
+
+void vkms_config_destroy_connector(struct vkms_config_connector *connector_cfg)
+{
+ /*
+ * Pin the config, then unlink and drop the list's reference. If this
+ * was the last connector reference the release callback runs right
+ * here, so it must not walk config->connectors (already unlinked)
+ * nor free the config (pinned). list_del_rcu() because a concurrent
+ * dump may still be walking this connector.
+ */
+ vkms_config_get(connector_cfg->config);
+ list_del_rcu(&connector_cfg->link);
+ connector_cfg->unlinked = true;
+ vkms_config_connector_put(connector_cfg);
+}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_destroy_connector);
int __must_check vkms_config_connector_attach_encoder(struct vkms_config_connector *connector_cfg,
diff --git a/drivers/gpu/drm/vkms/vkms_config.h b/drivers/gpu/drm/vkms/vkms_config.h
index 125273f67e7d..95e5c3694742 100644
--- a/drivers/gpu/drm/vkms/vkms_config.h
+++ b/drivers/gpu/drm/vkms/vkms_config.h
@@ -3,9 +3,14 @@
#ifndef _VKMS_CONFIG_H_
#define _VKMS_CONFIG_H_
+#include <linux/cleanup.h>
+#include <linux/kref.h>
#include <linux/list.h>
#include <linux/rculist.h>
#include <linux/mutex.h>
+#include <linux/rcupdate.h>
+#include <linux/string.h>
+#include <linux/list.h>
#include <linux/types.h>
#include <linux/xarray.h>
@@ -24,6 +29,8 @@
* @lock: Protects the whole configuration (lists and item fields) against
* concurrent access from the configfs, debugfs and DRM paths
* @dev: Used to store the current VKMS device. Only set when the device is instantiated.
+ * @refcnt: Reference counter, starts at 1 (owned by the creator)
+ * @rcu: RCU head for deferred freeing of this object
*/
struct vkms_config {
const char *dev_name;
@@ -33,6 +40,8 @@ struct vkms_config {
struct list_head connectors;
struct mutex lock;
struct vkms_device *dev;
+ struct kref refcnt;
+ struct rcu_head rcu;
};
/**
@@ -116,10 +125,14 @@ struct vkms_config_encoder {
* @config: The vkms_config this connector belongs to
* @status: Status (connected, disconnected...) of the connector
* @possible_encoders: Array of encoders that can be used with this connector
+ * @unlinked: Set when the connector was unlinked from config->connectors
+ * by vkms_config_destroy_connector()
* @connector: Internal usage. This pointer should never be considered as valid.
* It can be used to store a temporary reference to a VKMS connector
* during device creation. This pointer is not managed by the
* configuration and must be managed by other means.
+ * @refcnt: Reference counter, starts at 1 (owned by config->connectors). The
+ * object is freed only after an RCU grace period
*/
struct vkms_config_connector {
struct list_head link;
@@ -128,9 +141,12 @@ struct vkms_config_connector {
enum drm_connector_status status;
struct xarray possible_encoders;
+ bool unlinked;
/* Internal usage */
struct vkms_connector *connector;
+
+ struct kref refcnt;
};
/**
@@ -228,6 +244,23 @@ struct vkms_config *vkms_config_default_create(bool enable_cursor,
bool enable_overlay,
bool enable_plane_pipeline);
+/**
+ * vkms_config_get() - Acquire a reference to a VKMS configuration
+ * @config: vkms_config to acquire a reference to
+ */
+void vkms_config_get(struct vkms_config *config);
+
+/**
+ * vkms_config_put() - Release a reference to a VKMS configuration
+ * @config: vkms_config to release the reference from
+ *
+ * When the last reference is released, the configuration and all its
+ * sub-components are freed.
+ */
+void vkms_config_put(struct vkms_config *config);
+
+DEFINE_FREE(vkms_config_put, struct vkms_config *, if (_T) vkms_config_put(_T));
+
/**
* vkms_config_destroy() - Free a VKMS configuration
* @config: vkms_config to free
@@ -463,6 +496,21 @@ void vkms_config_encoder_detach_crtc(struct vkms_config_encoder *encoder_cfg,
*/
struct vkms_config_connector *vkms_config_create_connector(struct vkms_config *config);
+/**
+ * vkms_config_connector_get() - Acquire a reference to a connector configuration
+ * @connector_cfg: Connector configuration to acquire a reference to
+ */
+struct vkms_config_connector *
+vkms_config_connector_get(struct vkms_config_connector *connector_cfg);
+
+/**
+ * vkms_config_connector_put() - Release a reference to a connector configuration
+ * @connector_cfg: Connector configuration to release the reference from
+ *
+ * When the last reference is released, the connector configuration is freed.
+ */
+void vkms_config_connector_put(struct vkms_config_connector *connector_cfg);
+
/**
* vkms_config_destroy_connector() - Remove and free a connector configuration
* @connector_cfg: Connector configuration to destroy
diff --git a/drivers/gpu/drm/vkms/vkms_drv.c b/drivers/gpu/drm/vkms/vkms_drv.c
index 4cbb1173fcc6..24572bc779b4 100644
--- a/drivers/gpu/drm/vkms/vkms_drv.c
+++ b/drivers/gpu/drm/vkms/vkms_drv.c
@@ -163,6 +163,10 @@ int vkms_create(struct vkms_config *config)
struct faux_device *fdev;
struct vkms_device *vkms_device;
const char *dev_name;
+ /* The device keeps the config alive for its whole lifetime */
+ struct vkms_config *cfg __free(vkms_config_put) = config;
+
+ vkms_config_get(cfg);
dev_name = vkms_config_get_device_name(config);
fdev = faux_device_create(dev_name, NULL, NULL);
@@ -276,7 +280,7 @@ static void __exit vkms_exit(void)
return;
vkms_destroy(default_config);
- vkms_config_destroy(default_config);
+ vkms_config_put(default_config);
}
module_init(vkms_init);
--
2.56.0
next prev parent reply other threads:[~2026-11-26 11:30 UTC|newest]
Thread overview: 48+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 11:28 [PATCH v7 00/43] VKMS: Introduce multiple configFS attributes Louis Chauvet
2026-11-26 11:28 ` [PATCH v7 01/43] drm/vkms: Lock the config dump against concurrent configfs changes Louis Chauvet
2026-11-26 11:28 ` [PATCH v7 02/43] drm/drm_mode_config: Add helper to get plane type name Louis Chauvet
2026-11-26 11:28 ` [PATCH v7 03/43] drm/blend: Get a rotation name from its bitfield Louis Chauvet
2026-11-26 11:28 ` [PATCH v7 04/43] drm/tests: Add test cases for DRM rotation name Louis Chauvet
2026-11-26 11:28 ` [PATCH v7 05/43] drm/drm_color_mgmt: Expose drm_get_color_encoding_name Louis Chauvet
2026-11-26 11:28 ` [PATCH v7 06/43] drm/drm_color_mgmt: Expose drm_get_color_range_name Louis Chauvet
2026-11-26 11:28 ` [PATCH v7 07/43] drm/connector: Export drm_get_colorspace_name Louis Chauvet
2026-11-26 11:28 ` [PATCH v7 08/43] drm/drm_atomic_state_helper: Properly load default value for rotation Louis Chauvet
2026-11-26 11:28 ` [PATCH v7 09/43] Documentation: ABI: vkms: Add current VKMS ABI documentation Louis Chauvet
2026-11-26 11:28 ` [PATCH v7 10/43] drm/vkms: Reset config->dev when the device creation fails Louis Chauvet
2026-11-27 7:21 ` Oarora Etimis
2026-11-26 11:28 ` Louis Chauvet [this message]
2026-11-26 11:28 ` [PATCH v7 12/43] drm/vkms: Add KUnit test for device lifecycle Louis Chauvet
2026-11-26 11:28 ` [PATCH v7 13/43] drm/vkms: Add error handling in plane config creation Louis Chauvet
2026-11-26 11:28 ` [PATCH v7 14/43] drm/vkms: Simplify plane_release code Louis Chauvet
2026-11-26 11:28 ` [PATCH v7 15/43] drm/vkms: Explicitly display plane type Louis Chauvet
2026-11-26 11:28 ` [PATCH v7 16/43] drm/vkms: Use enabled/disabled instead of 1/0 for debug Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 17/43] drm/vkms: Introduce config for plane name Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 18/43] drm/vkms: Use plane folder name as " Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 19/43] drm/vkms: Introduce config for plane rotation Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 20/43] drm/vkms: Introduce configfs " Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 21/43] drm/vkms: Introduce config for plane color encoding Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 22/43] drm/vkms: Introduce configfs " Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 23/43] drm/vkms: Introduce config for plane color range Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 24/43] drm/vkms: Introduce configfs " Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 25/43] drm/vkms: Introduce config for plane format Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 26/43] drm/vkms: Introduce configfs " Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 27/43] drm/vkms: Properly render plane using their zpos Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 28/43] drm/vkms: Introduce config for plane zpos property Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 29/43] drm/vkms: Introduce configfs " Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 30/43] drm/vkms: Introduce back pointer to connector Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 31/43] drm/vkms: Introduce connector type Louis Chauvet
2026-11-27 7:15 ` Oarora Etimis
2026-11-26 11:29 ` [PATCH v7 32/43] drm/vkms: Introduce configfs for " Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 33/43] drm/vkms: Rename vkms_connector_init to vkms_connector_init_static Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 34/43] drm/vkms: Introduce config for connector supported colorspace Louis Chauvet
2026-11-26 16:44 ` kernel test robot
2026-11-26 11:29 ` [PATCH v7 35/43] drm/vkms: Introduce configfs " Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 36/43] drm/vkms: Introduce config for connector EDID Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 37/43] drm/vkms: Introduce configfs " Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 38/43] drm/vkms: Store the enabled/disabled status for connector Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 39/43] drm/connector: Add a way to detach encoders Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 40/43] drm/vkms: Allow to hot-add connectors Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 41/43] drm/vkms: Introduce configfs for dynamic connector creation Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 42/43] drm/vkms: Add connector parent configuration in vkms_config Louis Chauvet
2026-11-26 11:29 ` [PATCH v7 43/43] drm/vkms: Add ConfigFS interface for connector parent and port_id Louis Chauvet
2026-11-26 19:49 ` kernel test robot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261126-vkms-all-config-v7-11-21194cda2468@bootlin.com \
--to=louis.chauvet@bootlin.com \
--cc=airlied@gmail.com \
--cc=corbet@lwn.net \
--cc=demarchi@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=hamohammed.sa@gmail.com \
--cc=harry.wentland@amd.com \
--cc=jose.exposito89@gmail.com \
--cc=justinstitt@google.com \
--cc=kory.maincent@bootlin.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=luca.ceresoli@bootlin.com \
--cc=maarten.lankhorst@linux.intel.com \
--cc=melissa.srw@gmail.com \
--cc=morbo@google.com \
--cc=mripard@kernel.org \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=nick.desaulniers+lkml@gmail.com \
--cc=oaroraetimis@gmail.com \
--cc=ojeda@kernel.org \
--cc=rdunlap@infradead.org \
--cc=sebastian.wick@redhat.com \
--cc=simona@ffwll.ch \
--cc=skhan@linuxfoundation.org \
--cc=thomas.petazzoni@bootlin.com \
--cc=tzimmermann@suse.de \
--cc=victoria@system76.com \
--cc=ville.syrjala@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®