mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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,
	Simona Vetter <simona.vetter@ffwll.ch>,
	 llvm@lists.linux.dev
Subject: [PATCH v6 12/43] drm/vkms: Reference-count vkms_config and vkms_config_connector
Date: Fri, 20 Nov 2026 18:58:54 +0100	[thread overview]
Message-ID: <20261120-vkms-all-config-v6-12-78ace90f4fc1@bootlin.com> (raw)
In-Reply-To: <20261120-vkms-all-config-v6-0-78ace90f4fc1@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.

Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
---
 drivers/gpu/drm/vkms/vkms_config.c | 88 +++++++++++++++++++++++++++++++++++---
 drivers/gpu/drm/vkms/vkms_config.h | 41 +++++++++++++++++-
 drivers/gpu/drm/vkms/vkms_drv.c    |  7 ++-
 3 files changed, 128 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/vkms/vkms_config.c b/drivers/gpu/drm/vkms/vkms_config.c
index 71877651cb00..50cde689f482 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,7 @@ 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);
+	kref_init(&config->refcnt);
 	mutex_init(&config->lock);
 
 	return config;
@@ -112,8 +115,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,13 +132,37 @@ 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 configuration is already being released and cannot be
+		 * pinned anymore: drop the initial reference directly. The
+		 * connector is still linked, so its release only unlinks it
+		 * without touching the configuration.
+		 */
+		vkms_config_connector_put(connector_cfg);
+	}
 
 	mutex_destroy(&config->lock);
 	kfree_const(config->dev_name);
 	kfree(config);
 }
+
+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);
 
 static bool valid_plane_number(const struct vkms_config *config)
@@ -610,6 +638,7 @@ 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);
+	kref_init(&connector_cfg->refcnt);
 
 	list_add_tail(&connector_cfg->link, &config->connectors);
 
@@ -617,12 +646,61 @@ 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(&connector_cfg->link);
+	if (list_empty(&connector_cfg->link)) {
+		/*
+		 * The connector was detached from the configuration by
+		 * vkms_config_destroy_connector() before its final put,
+		 * and that call pinned the configuration to keep it alive
+		 * while the connector still points to it; drop that
+		 * reference now that the last connector reference is gone.
+		 */
+		vkms_config_put(connector_cfg->config);
+	} else {
+		/*
+		 * The configuration is already being released and cannot be
+		 * pinned anymore: unlink the connector without touching the
+		 * configuration.
+		 */
+		list_del(&connector_cfg->link);
+	}
 	kfree(connector_cfg);
 }
+
+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 configuration and detach the connector before dropping
+	 * the initial reference: the release callback runs inside the put
+	 * when it is the last one, and would otherwise mutate the
+	 * configuration lists without holding config->lock, or free the
+	 * configuration while this call and its configfs caller still use
+	 * both. The pin is dropped again by the release when the connector
+	 * dies here; if it survives, it keeps the configuration alive until
+	 * the connector's final put.
+	 */
+	vkms_config_get(connector_cfg->config);
+	list_del_init(&connector_cfg->link);
+
+	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 10604f7f37e1..de6a89819daf 100644
--- a/drivers/gpu/drm/vkms/vkms_config.h
+++ b/drivers/gpu/drm/vkms/vkms_config.h
@@ -3,8 +3,12 @@
 #ifndef _VKMS_CONFIG_H_
 #define _VKMS_CONFIG_H_
 
-#include <linux/list.h>
+#include <linux/cleanup.h>
+#include <linux/kref.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>
 
@@ -32,6 +36,7 @@ struct vkms_config {
 	struct list_head connectors;
 	struct mutex lock;
 	struct vkms_device *dev;
+	struct kref refcnt;
 };
 
 /**
@@ -122,6 +127,8 @@ struct vkms_config_connector {
 
 	/* Internal usage */
 	struct vkms_connector *connector;
+
+	struct kref refcnt;
 };
 
 /**
@@ -211,6 +218,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
@@ -446,6 +470,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 5a640b531d88..64c75b038869 100644
--- a/drivers/gpu/drm/vkms/vkms_drv.c
+++ b/drivers/gpu/drm/vkms/vkms_drv.c
@@ -163,6 +163,9 @@ int vkms_create(struct vkms_config *config)
 	struct faux_device *fdev;
 	struct vkms_device *vkms_device;
 	const char *dev_name;
+	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);
@@ -210,11 +213,11 @@ int vkms_create(struct vkms_config *config)
 		goto out_devres;
 
 	drm_client_setup(&vkms_device->drm, NULL);
-
 	return 0;
 
 out_devres:
 	devres_release_group(&fdev->dev, NULL);
+	config->dev = NULL;
 out_unregister:
 	faux_device_destroy(fdev);
 	return ret;
@@ -275,7 +278,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


  parent reply	other threads:[~2026-11-20 18:03 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 17:58 [PATCH v6 00/43] VKMS: Introduce multiple configFS attributes Louis Chauvet
2026-11-20 17:58 ` [PATCH v6 01/43] drm/vkms: Explicitly display connector status Louis Chauvet
2026-11-20 17:58 ` [PATCH v6 02/43] drm/vkms: Lock the config dump against concurrent configfs changes Louis Chauvet
2026-11-20 17:58 ` [PATCH v6 03/43] drm/vkms: Add new lines for configfs attributes Louis Chauvet
2026-11-20 17:58 ` [PATCH v6 04/43] drm/drm_mode_config: Add helper to get plane type name Louis Chauvet
2026-11-20 17:58 ` [PATCH v6 05/43] drm/blend: Get a rotation name from its bitfield Louis Chauvet
2026-11-20 17:58 ` [PATCH v6 06/43] drm/tests: Add test cases for DRM rotation name Louis Chauvet
2026-11-20 17:58 ` [PATCH v6 07/43] drm/drm_color_mgmt: Expose drm_get_color_encoding_name Louis Chauvet
2026-11-20 17:58 ` [PATCH v6 08/43] drm/drm_color_mgmt: Expose drm_get_color_range_name Louis Chauvet
2026-11-20 17:58 ` [PATCH v6 09/43] drm/connector: Export drm_get_colorspace_name Louis Chauvet
2026-11-20 17:58 ` [PATCH v6 10/43] drm/drm_atomic_state_helper: Properly load default value for rotation Louis Chauvet
2026-11-20 17:58 ` [PATCH v6 11/43] Documentation: ABI: vkms: Add current VKMS ABI documentation Louis Chauvet
2026-11-20 17:58 ` Louis Chauvet [this message]
2026-11-20 17:58 ` [PATCH v6 13/43] drm/vkms: Add KUnit test for device lifecycle Louis Chauvet
2026-11-20 17:58 ` [PATCH v6 14/43] drm/vkms: Add error handling in plane config creation Louis Chauvet
2026-11-20 17:58 ` [PATCH v6 15/43] drm/vkms: Simplify plane_release code Louis Chauvet
2026-11-20 17:58 ` [PATCH v6 16/43] drm/vkms: Explicitly display plane type Louis Chauvet
2026-11-20 17:58 ` [PATCH v6 17/43] drm/vkms: Use enabled/disabled instead of 1/0 for debug Louis Chauvet
2026-11-20 17:59 ` [PATCH v6 18/43] drm/vkms: Introduce config for plane name Louis Chauvet
2026-11-20 17:59 ` [PATCH v6 19/43] drm/vkms: Use plane folder name as " Louis Chauvet
2026-11-20 17:59 ` [PATCH v6 20/43] drm/vkms: Introduce config for plane rotation Louis Chauvet
2026-11-20 17:59 ` [PATCH v6 21/43] drm/vkms: Introduce configfs " Louis Chauvet
2026-11-20 17:59 ` [PATCH v6 22/43] drm/vkms: Introduce config for plane color encoding Louis Chauvet
2026-11-20 17:59 ` [PATCH v6 23/43] drm/vkms: Introduce configfs " Louis Chauvet
2026-11-20 17:59 ` [PATCH v6 24/43] drm/vkms: Introduce config for plane color range Louis Chauvet
2026-11-20 17:59 ` [PATCH v6 25/43] drm/vkms: Introduce configfs " Louis Chauvet
2026-11-20 17:59 ` [PATCH v6 26/43] drm/vkms: Introduce config for plane format Louis Chauvet
2026-11-20 17:59 ` [PATCH v6 27/43] drm/vkms: Introduce configfs " Louis Chauvet
2026-11-20 17:59 ` [PATCH v6 28/43] drm/vkms: Properly render plane using their zpos Louis Chauvet
2026-11-20 17:59 ` [PATCH v6 29/43] drm/vkms: Introduce config for plane zpos property Louis Chauvet
2026-11-20 17:59 ` [PATCH v6 30/43] drm/vkms: Introduce configfs " Louis Chauvet
2026-11-20 17:59 ` [PATCH v6 31/43] drm/vkms: Introduce back pointer to connector Louis Chauvet
2026-11-20 17:59 ` [PATCH v6 32/43] drm/vkms: Introduce connector type Louis Chauvet
2026-11-20 17:59 ` [PATCH v6 33/43] drm/vkms: Introduce configfs for " Louis Chauvet
2026-11-20 17:59 ` [PATCH v6 34/43] drm/vkms: Rename vkms_connector_init to vkms_connector_init_static Louis Chauvet
2026-11-20 17:59 ` [PATCH v6 35/43] drm/vkms: Introduce config for connector supported colorspace Louis Chauvet

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=20261120-vkms-all-config-v6-12-78ace90f4fc1@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.vetter@ffwll.ch \
    --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®