Skip to contentSkip to conversation

drm/client: fix circular reference counting issue

PATCH In review Christian König 3 participants 7 messages Jan 26, 2023 – Feb 17, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 drm/client: fix circular reference counting issue

We reference dump buffers both by their handle as well as their object. The problem is now that when anybody iterates over the DRM framebuffers and exports the underlying GEM objects through DMA-buf we run into a circular reference count situation.

The result is that the fbdev handling holds the GEM handle preventing the DMA-buf in the GEM object to be released. This DMA-buf in turn holds a reference to the driver module which on unload would release the fbdev.

Break that loop by releasing the handle as soon as the DRM framebuffer object is created. The DRM framebuffer and the DRM client buffer structure still hold a reference to the underlying GEM object preventing its destruction.

Fixes c76f0f7cb546 ("drm: Begin an API for in-kernel clients")
---
 drivers/gpu/drm/drm_client.c | 33 ++++++++++++++++++++-------------
 include/drm/drm_client.h     |  5 -----
 2 files changed, 20 insertions(+), 18 deletions(-)
drivers/gpu/drm/drm_client.c +20 -13
index 009e7b10455c..f6292ba0e6fc 100644
@@ -243,21 +243,17 @@ void drm_client_dev_restore(struct drm_device *dev)
 
 static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
 {
-	struct drm_device *dev = buffer->client->dev;
-
 	if (buffer->gem) {
 		drm_gem_vunmap_unlocked(buffer->gem, &buffer->map);
 		drm_gem_object_put(buffer->gem);
 	}
 
-	if (buffer->handle)
-		drm_mode_destroy_dumb(dev, buffer->handle, buffer->client->file);
-
 	kfree(buffer);
 }
 
 static struct drm_client_buffer *
-drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
+drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height,
+			 u32 format, u32 *handle)
 {
 	const struct drm_format_info *info = drm_format_info(format);
 	struct drm_mode_create_dumb dumb_args = { };
@@ -279,16 +275,15 @@ drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u
 	if (ret)
 		goto err_delete;
 
-	buffer->handle = dumb_args.handle;
-	buffer->pitch = dumb_args.pitch;
-
 	obj = drm_gem_object_lookup(client->file, dumb_args.handle);
 	if (!obj)  {
 		ret = -ENOENT;
 		goto err_delete;
 	}
 
+	buffer->pitch = dumb_args.pitch;
 	buffer->gem = obj;
+	*handle = dumb_args.handle;
 
 	return buffer;
 
@@ -375,7 +370,8 @@ static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
 }
 
 static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
-				   u32 width, u32 height, u32 format)
+				   u32 width, u32 height, u32 format,
+				   u32 handle)
 {
 	struct drm_client_dev *client = buffer->client;
 	struct drm_mode_fb_cmd fb_req = { };
@@ -387,7 +383,7 @@ static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
 	fb_req.depth = info->depth;
 	fb_req.width = width;
 	fb_req.height = height;
-	fb_req.handle = buffer->handle;
+	fb_req.handle = handle;
 	fb_req.pitch = buffer->pitch;
 
 	ret = drm_mode_addfb(client->dev, &fb_req, client->file);
@@ -424,13 +420,24 @@ struct drm_client_buffer *
 drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
 {
 	struct drm_client_buffer *buffer;
+	u32 handle;
 	int ret;
 
-	buffer = drm_client_buffer_create(client, width, height, format);
+	buffer = drm_client_buffer_create(client, width, height, format,
+					  &handle);
 	if (IS_ERR(buffer))
 		return buffer;
 
-	ret = drm_client_buffer_addfb(buffer, width, height, format);
+	ret = drm_client_buffer_addfb(buffer, width, height, format, handle);
+
+	/*
+	 * The handle is only needed for creating the framebuffer, destroy it
+	 * again to solve a circular dependency should anybody export the GEM
+	 * object as DMA-buf. The framebuffer and our buffer structure are still
+	 * holding references to the GEM object to prevent its destruction.
+	 */
+	drm_mode_destroy_dumb(client->dev, handle, client->file);
+
 	if (ret) {
 		drm_client_buffer_delete(buffer);
 		return ERR_PTR(ret);
include/drm/drm_client.h +0 -5
index 39482527a775..b5acdab73766 100644
@@ -134,11 +134,6 @@ struct drm_client_buffer {
 	 */
 	struct drm_client_dev *client;
 
-	/**
-	 * @handle: Buffer handle
-	 */
-	u32 handle;
-
 	/**
 	 * @pitch: Buffer pitch
 	 */
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 drm: remove dumb_destroy callback

Not used by any driver any more.

---
 drivers/gpu/drm/drm_dumb_buffers.c |  5 +----
 drivers/gpu/drm/drm_gem.c          |  7 -------
 drivers/gpu/drm/drm_internal.h     |  3 ---
 include/drm/drm_drv.h              | 19 -------------------
 4 files changed, 1 insertion(+), 33 deletions(-)
drivers/gpu/drm/drm_dumb_buffers.c +1 -4
index ad17fa21cebb..70032bba1c97 100644
@@ -139,10 +139,7 @@ int drm_mode_destroy_dumb(struct drm_device *dev, u32 handle,
 	if (!dev->driver->dumb_create)
 		return -ENOSYS;
 
-	if (dev->driver->dumb_destroy)
-		return dev->driver->dumb_destroy(file_priv, dev, handle);
-	else
-		return drm_gem_dumb_destroy(file_priv, dev, handle);
+	return drm_gem_handle_delete(file_priv, handle);
 }
 
 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
drivers/gpu/drm/drm_gem.c +0 -7
index 59a0bb5ebd85..aa15c52ae182 100644
@@ -336,13 +336,6 @@ int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
 }
 EXPORT_SYMBOL_GPL(drm_gem_dumb_map_offset);
 
-int drm_gem_dumb_destroy(struct drm_file *file,
-			 struct drm_device *dev,
-			 u32 handle)
-{
-	return drm_gem_handle_delete(file, handle);
-}
-
 /**
  * drm_gem_handle_create_tail - internal functions to create a handle
  * @file_priv: drm file-private structure to register the handle for
drivers/gpu/drm/drm_internal.h +0 -3
index ed2103ee272c..d7e023bbb0d5 100644
@@ -178,9 +178,6 @@ void drm_gem_unpin(struct drm_gem_object *obj);
 int drm_gem_vmap(struct drm_gem_object *obj, struct iosys_map *map);
 void drm_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map);
 
-int drm_gem_dumb_destroy(struct drm_file *file, struct drm_device *dev,
-			 u32 handle);
-
 /* drm_debugfs.c drm_debugfs_crc.c */
 #if defined(CONFIG_DEBUG_FS)
 int drm_debugfs_init(struct drm_minor *minor, int minor_id,
include/drm/drm_drv.h +0 -19
index 1d76d0686b03..5b86bb7603e7 100644
@@ -400,25 +400,6 @@ struct drm_driver {
 	int (*dumb_map_offset)(struct drm_file *file_priv,
 			       struct drm_device *dev, uint32_t handle,
 			       uint64_t *offset);
-	/**
-	 * @dumb_destroy:
-	 *
-	 * This destroys the userspace handle for the given dumb backing storage buffer.
-	 * Since buffer objects must be reference counted in the kernel a buffer object
-	 * won't be immediately freed if a framebuffer modeset object still uses it.
-	 *
-	 * Called by the user via ioctl.
-	 *
-	 * The default implementation is drm_gem_dumb_destroy(). GEM based drivers
-	 * must not overwrite this.
-	 *
-	 * Returns:
-	 *
-	 * Zero on success, negative errno on failure.
-	 */
-	int (*dumb_destroy)(struct drm_file *file_priv,
-			    struct drm_device *dev,
-			    uint32_t handle);
 
 	/** @major: driver major number */
 	int major;
Signature · 1 line
2.34.1
maintainer Reviewed-by Tested-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] drm: remove dumb_destroy callback

Am 26.01.23 um 11:28 schrieb Christian König:

Quoted 3 lines

Not used by any driver any more.

Same comments as for patch 1.

Quoted 85 lines
---
  drivers/gpu/drm/drm_dumb_buffers.c |  5 +----
  drivers/gpu/drm/drm_gem.c          |  7 -------
  drivers/gpu/drm/drm_internal.h     |  3 ---
  include/drm/drm_drv.h              | 19 -------------------
  4 files changed, 1 insertion(+), 33 deletions(-)
drivers/gpu/drm/drm_dumb_buffers.c +1 -4
index ad17fa21cebb..70032bba1c97 100644
@@ -139,10 +139,7 @@ int drm_mode_destroy_dumb(struct drm_device *dev, u32 handle,
  	if (!dev->driver->dumb_create)
  		return -ENOSYS;
 
-	if (dev->driver->dumb_destroy)
-		return dev->driver->dumb_destroy(file_priv, dev, handle);
-	else
-		return drm_gem_dumb_destroy(file_priv, dev, handle);
+	return drm_gem_handle_delete(file_priv, handle);
  }
 
  int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
drivers/gpu/drm/drm_gem.c +0 -7
index 59a0bb5ebd85..aa15c52ae182 100644
@@ -336,13 +336,6 @@ int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
  }
  EXPORT_SYMBOL_GPL(drm_gem_dumb_map_offset);
 
-int drm_gem_dumb_destroy(struct drm_file *file,
-			 struct drm_device *dev,
-			 u32 handle)
-{
-	return drm_gem_handle_delete(file, handle);
-}
-
  /**
   * drm_gem_handle_create_tail - internal functions to create a handle
   * @file_priv: drm file-private structure to register the handle for
drivers/gpu/drm/drm_internal.h +0 -3
index ed2103ee272c..d7e023bbb0d5 100644
@@ -178,9 +178,6 @@ void drm_gem_unpin(struct drm_gem_object *obj);
  int drm_gem_vmap(struct drm_gem_object *obj, struct iosys_map *map);
  void drm_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map);
 
-int drm_gem_dumb_destroy(struct drm_file *file, struct drm_device *dev,
-			 u32 handle);
-
  /* drm_debugfs.c drm_debugfs_crc.c */
  #if defined(CONFIG_DEBUG_FS)
  int drm_debugfs_init(struct drm_minor *minor, int minor_id,
include/drm/drm_drv.h +0 -19
index 1d76d0686b03..5b86bb7603e7 100644
@@ -400,25 +400,6 @@ struct drm_driver {
  	int (*dumb_map_offset)(struct drm_file *file_priv,
  			       struct drm_device *dev, uint32_t handle,
  			       uint64_t *offset);
-	/**
-	 * @dumb_destroy:
-	 *
-	 * This destroys the userspace handle for the given dumb backing storage buffer.
-	 * Since buffer objects must be reference counted in the kernel a buffer object
-	 * won't be immediately freed if a framebuffer modeset object still uses it.
-	 *
-	 * Called by the user via ioctl.
-	 *
-	 * The default implementation is drm_gem_dumb_destroy(). GEM based drivers
-	 * must not overwrite this.
-	 *
-	 * Returns:
-	 *
-	 * Zero on success, negative errno on failure.
-	 */
-	int (*dumb_destroy)(struct drm_file *file_priv,
-			    struct drm_device *dev,
-			    uint32_t handle);
 
  	/** @major: driver major number */
  	int major;
Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev
maintainer Reviewed-by Tested-by on v1 1/2
#3 lore raw
In reply to Christian König

Hi

Am 26.01.23 um 11:28 schrieb Christian König:

Quoted 10 lines

We reference dump buffers both by their handle as well as their object. The problem is now that when anybody iterates over the DRM framebuffers and exports the underlying GEM objects through DMA-buf we run into a circular reference count situation.

The result is that the fbdev handling holds the GEM handle preventing the DMA-buf in the GEM object to be released. This DMA-buf in turn holds a reference to the driver module which on unload would release the fbdev.

Quoted 8 lines

Break that loop by releasing the handle as soon as the DRM framebuffer object is created. The DRM framebuffer and the DRM client buffer structure still hold a reference to the underlying GEM object preventing its destruction.

Fixes c76f0f7cb546 ("drm: Begin an API for in-kernel clients")

I tested with Weston and Gnome in X11 and Wayland mode under simpledrm, which I started stopped from the console. No obvious problems.

I heard that sway/wlroots has issues with drivers that don't support dma-buf. Maybe(!) that could be affected by this patch.

Anyway, take my r-b, t-b tags.

Thank you for fixing this bug.

Best regards
Thomas

Quoted 114 lines
---
  drivers/gpu/drm/drm_client.c | 33 ++++++++++++++++++++-------------
  include/drm/drm_client.h     |  5 -----
  2 files changed, 20 insertions(+), 18 deletions(-)
drivers/gpu/drm/drm_client.c +20 -13
index 009e7b10455c..f6292ba0e6fc 100644
@@ -243,21 +243,17 @@ void drm_client_dev_restore(struct drm_device *dev)
 
  static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
  {
-	struct drm_device *dev = buffer->client->dev;
-
  	if (buffer->gem) {
  		drm_gem_vunmap_unlocked(buffer->gem, &buffer->map);
  		drm_gem_object_put(buffer->gem);
  	}
 
-	if (buffer->handle)
-		drm_mode_destroy_dumb(dev, buffer->handle, buffer->client->file);
-
  	kfree(buffer);
  }
 
  static struct drm_client_buffer *
-drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
+drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height,
+			 u32 format, u32 *handle)
  {
  	const struct drm_format_info *info = drm_format_info(format);
  	struct drm_mode_create_dumb dumb_args = { };
@@ -279,16 +275,15 @@ drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u
  	if (ret)
  		goto err_delete;
 
-	buffer->handle = dumb_args.handle;
-	buffer->pitch = dumb_args.pitch;
-
  	obj = drm_gem_object_lookup(client->file, dumb_args.handle);
  	if (!obj)  {
  		ret = -ENOENT;
  		goto err_delete;
  	}
 
+	buffer->pitch = dumb_args.pitch;
  	buffer->gem = obj;
+	*handle = dumb_args.handle;
 
  	return buffer;
 
@@ -375,7 +370,8 @@ static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
  }
 
  static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
-				   u32 width, u32 height, u32 format)
+				   u32 width, u32 height, u32 format,
+				   u32 handle)
  {
  	struct drm_client_dev *client = buffer->client;
  	struct drm_mode_fb_cmd fb_req = { };
@@ -387,7 +383,7 @@ static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
  	fb_req.depth = info->depth;
  	fb_req.width = width;
  	fb_req.height = height;
-	fb_req.handle = buffer->handle;
+	fb_req.handle = handle;
  	fb_req.pitch = buffer->pitch;
 
  	ret = drm_mode_addfb(client->dev, &fb_req, client->file);
@@ -424,13 +420,24 @@ struct drm_client_buffer *
  drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
  {
  	struct drm_client_buffer *buffer;
+	u32 handle;
  	int ret;
 
-	buffer = drm_client_buffer_create(client, width, height, format);
+	buffer = drm_client_buffer_create(client, width, height, format,
+					  &handle);
  	if (IS_ERR(buffer))
  		return buffer;
 
-	ret = drm_client_buffer_addfb(buffer, width, height, format);
+	ret = drm_client_buffer_addfb(buffer, width, height, format, handle);
+
+	/*
+	 * The handle is only needed for creating the framebuffer, destroy it
+	 * again to solve a circular dependency should anybody export the GEM
+	 * object as DMA-buf. The framebuffer and our buffer structure are still
+	 * holding references to the GEM object to prevent its destruction.
+	 */
+	drm_mode_destroy_dumb(client->dev, handle, client->file);
+
  	if (ret) {
  		drm_client_buffer_delete(buffer);
  		return ERR_PTR(ret);
include/drm/drm_client.h +0 -5
index 39482527a775..b5acdab73766 100644
@@ -134,11 +134,6 @@ struct drm_client_buffer {
  	 */
  	struct drm_client_dev *client;
 
-	/**
-	 * @handle: Buffer handle
-	 */
-	u32 handle;
-
  	/**
  	 * @pitch: Buffer pitch
  	 */
Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev
#4 lore raw
Quoted 22 lines · Thomas Zimmermann

Hi

Am 26.01.23 um 11:28 schrieb Christian König:

Quoted 10 lines

We reference dump buffers both by their handle as well as their object. The problem is now that when anybody iterates over the DRM framebuffers and exports the underlying GEM objects through DMA-buf we run into a circular reference count situation.

The result is that the fbdev handling holds the GEM handle preventing the DMA-buf in the GEM object to be released. This DMA-buf in turn holds a reference to the driver module which on unload would release the fbdev.

Quoted 8 lines

Break that loop by releasing the handle as soon as the DRM framebuffer object is created. The DRM framebuffer and the DRM client buffer structure still hold a reference to the underlying GEM object preventing its destruction.

Fixes c76f0f7cb546 ("drm: Begin an API for in-kernel clients")
Quoted 5 lines

I tested with Weston and Gnome in X11 and Wayland mode under simpledrm, which I started stopped from the console. No obvious problems.

I heard that sway/wlroots has issues with drivers that don't support dma-buf. Maybe(!) that could be affected by this patch.

dma-buf export should still work. Also the loop is imo a red herring, I think if you force unbind the driver then this should all get resolved automatically.

What is true is that once we start refcounting everything correctly then there will be elevated module refcounts, which means you cannot use module unloading to provoke a driver unbind, which would kick out all the leftover references. You instead need to manually unbind the driver first, which should drop all remaining references to zero (might need to kill also any userspace), and only then can you unload the driver.

But this confusion is extremely common, a lot of people think that just holding a module reference is enough, we really should also hold a drm_device reference for dma-buf ...
-Daniel

Quoted 121 lines

Anyway, take my r-b, t-b tags.

Thank you for fixing this bug.

Best regards
Thomas

Quoted 102 lines
---
  drivers/gpu/drm/drm_client.c | 33 ++++++++++++++++++++-------------
  include/drm/drm_client.h     |  5 -----
  2 files changed, 20 insertions(+), 18 deletions(-)
drivers/gpu/drm/drm_client.c +20 -13
index 009e7b10455c..f6292ba0e6fc 100644
@@ -243,21 +243,17 @@ void drm_client_dev_restore(struct drm_device *dev)
  static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
  {
-	struct drm_device *dev = buffer->client->dev;
-
  	if (buffer->gem) {
  		drm_gem_vunmap_unlocked(buffer->gem, &buffer->map);
  		drm_gem_object_put(buffer->gem);
  	}
-	if (buffer->handle)
-		drm_mode_destroy_dumb(dev, buffer->handle, buffer->client->file);
-
  	kfree(buffer);
  }
  static struct drm_client_buffer *
-drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
+drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height,
+			 u32 format, u32 *handle)
  {
  	const struct drm_format_info *info = drm_format_info(format);
  	struct drm_mode_create_dumb dumb_args = { };
@@ -279,16 +275,15 @@ drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u
  	if (ret)
  		goto err_delete;
-	buffer->handle = dumb_args.handle;
-	buffer->pitch = dumb_args.pitch;
-
  	obj = drm_gem_object_lookup(client->file, dumb_args.handle);
  	if (!obj)  {
  		ret = -ENOENT;
  		goto err_delete;
  	}
+	buffer->pitch = dumb_args.pitch;
  	buffer->gem = obj;
+	*handle = dumb_args.handle;
  	return buffer;
@@ -375,7 +370,8 @@ static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
  }
  static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
-				   u32 width, u32 height, u32 format)
+				   u32 width, u32 height, u32 format,
+				   u32 handle)
  {
  	struct drm_client_dev *client = buffer->client;
  	struct drm_mode_fb_cmd fb_req = { };
@@ -387,7 +383,7 @@ static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
  	fb_req.depth = info->depth;
  	fb_req.width = width;
  	fb_req.height = height;
-	fb_req.handle = buffer->handle;
+	fb_req.handle = handle;
  	fb_req.pitch = buffer->pitch;
  	ret = drm_mode_addfb(client->dev, &fb_req, client->file);
@@ -424,13 +420,24 @@ struct drm_client_buffer *
  drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
  {
  	struct drm_client_buffer *buffer;
+	u32 handle;
  	int ret;
-	buffer = drm_client_buffer_create(client, width, height, format);
+	buffer = drm_client_buffer_create(client, width, height, format,
+					  &handle);
  	if (IS_ERR(buffer))
  		return buffer;
-	ret = drm_client_buffer_addfb(buffer, width, height, format);
+	ret = drm_client_buffer_addfb(buffer, width, height, format, handle);
+
+	/*
+	 * The handle is only needed for creating the framebuffer, destroy it
+	 * again to solve a circular dependency should anybody export the GEM
+	 * object as DMA-buf. The framebuffer and our buffer structure are still
+	 * holding references to the GEM object to prevent its destruction.
+	 */
+	drm_mode_destroy_dumb(client->dev, handle, client->file);
+
  	if (ret) {
  		drm_client_buffer_delete(buffer);
  		return ERR_PTR(ret);
include/drm/drm_client.h +0 -5
index 39482527a775..b5acdab73766 100644
@@ -134,11 +134,6 @@ struct drm_client_buffer {
  	 */
  	struct drm_client_dev *client;
-	/**
-	 * @handle: Buffer handle
-	 */
-	u32 handle;
-
  	/**
  	 * @pitch: Buffer pitch
  	 */
Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#5 lore raw

Am 16.02.23 um 15:34 schrieb Daniel Vetter:

Quoted 38 lines · Thomas Zimmermann
Quoted 18 lines

Hi

Am 26.01.23 um 11:28 schrieb Christian König:

Quoted 15 lines

We reference dump buffers both by their handle as well as their object. The problem is now that when anybody iterates over the DRM framebuffers and exports the underlying GEM objects through DMA-buf we run into a circular reference count situation.

The result is that the fbdev handling holds the GEM handle preventing the DMA-buf in the GEM object to be released. This DMA-buf in turn holds a reference to the driver module which on unload would release the fbdev.

Break that loop by releasing the handle as soon as the DRM framebuffer object is created. The DRM framebuffer and the DRM client buffer structure still hold a reference to the underlying GEM object preventing its destruction.

Quoted 8 lines
Quoted 3 lines
Fixes c76f0f7cb546 ("drm: Begin an API for in-kernel clients")

I tested with Weston and Gnome in X11 and Wayland mode under simpledrm, which I started stopped from the console. No obvious problems.

I heard that sway/wlroots has issues with drivers that don't support dma-buf. Maybe(!) that could be affected by this patch.

dma-buf export should still work. Also the loop is imo a red herring, I think if you force unbind the driver then this should all get resolved automatically.

What is true is that once we start refcounting everything correctly then there will be elevated module refcounts, which means you cannot use module unloading to provoke a driver unbind, which would kick out all the leftover references. You instead need to manually unbind the driver first, which should drop all remaining references to zero (might need to kill also any userspace), and only then can you unload the driver.

Quoted 3 lines

But this confusion is extremely common, a lot of people think that just holding a module reference is enough, we really should also hold a drm_device reference for dma-buf ...

Yeah, hot plug removal of amdgpu revealed a couple of those as well.

Essentially what DMA-buf does with grabbing a module reference on the owner of a DMA-buf is a bad idea.

Instead we should reference the device or component which is exporting the buffer, but since we don't have a common structure here it's more work to generalize that approach.

Christian.

Quoted 124 lines

-Daniel

Quoted 119 lines

Anyway, take my r-b, t-b tags.

Thank you for fixing this bug.

Best regards
Thomas

Quoted 102 lines
---
   drivers/gpu/drm/drm_client.c | 33 ++++++++++++++++++++-------------
   include/drm/drm_client.h     |  5 -----
   2 files changed, 20 insertions(+), 18 deletions(-)
drivers/gpu/drm/drm_client.c +20 -13
index 009e7b10455c..f6292ba0e6fc 100644
@@ -243,21 +243,17 @@ void drm_client_dev_restore(struct drm_device *dev)
   static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
   {
-	struct drm_device *dev = buffer->client->dev;
-
   	if (buffer->gem) {
   		drm_gem_vunmap_unlocked(buffer->gem, &buffer->map);
   		drm_gem_object_put(buffer->gem);
   	}
-	if (buffer->handle)
-		drm_mode_destroy_dumb(dev, buffer->handle, buffer->client->file);
-
   	kfree(buffer);
   }
   static struct drm_client_buffer *
-drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
+drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height,
+			 u32 format, u32 *handle)
   {
   	const struct drm_format_info *info = drm_format_info(format);
   	struct drm_mode_create_dumb dumb_args = { };
@@ -279,16 +275,15 @@ drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u
   	if (ret)
   		goto err_delete;
-	buffer->handle = dumb_args.handle;
-	buffer->pitch = dumb_args.pitch;
-
   	obj = drm_gem_object_lookup(client->file, dumb_args.handle);
   	if (!obj)  {
   		ret = -ENOENT;
   		goto err_delete;
   	}
+	buffer->pitch = dumb_args.pitch;
   	buffer->gem = obj;
+	*handle = dumb_args.handle;
   	return buffer;
@@ -375,7 +370,8 @@ static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
   }
   static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
-				   u32 width, u32 height, u32 format)
+				   u32 width, u32 height, u32 format,
+				   u32 handle)
   {
   	struct drm_client_dev *client = buffer->client;
   	struct drm_mode_fb_cmd fb_req = { };
@@ -387,7 +383,7 @@ static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
   	fb_req.depth = info->depth;
   	fb_req.width = width;
   	fb_req.height = height;
-	fb_req.handle = buffer->handle;
+	fb_req.handle = handle;
   	fb_req.pitch = buffer->pitch;
   	ret = drm_mode_addfb(client->dev, &fb_req, client->file);
@@ -424,13 +420,24 @@ struct drm_client_buffer *
   drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
   {
   	struct drm_client_buffer *buffer;
+	u32 handle;
   	int ret;
-	buffer = drm_client_buffer_create(client, width, height, format);
+	buffer = drm_client_buffer_create(client, width, height, format,
+					  &handle);
   	if (IS_ERR(buffer))
   		return buffer;
-	ret = drm_client_buffer_addfb(buffer, width, height, format);
+	ret = drm_client_buffer_addfb(buffer, width, height, format, handle);
+
+	/*
+	 * The handle is only needed for creating the framebuffer, destroy it
+	 * again to solve a circular dependency should anybody export the GEM
+	 * object as DMA-buf. The framebuffer and our buffer structure are still
+	 * holding references to the GEM object to prevent its destruction.
+	 */
+	drm_mode_destroy_dumb(client->dev, handle, client->file);
+
   	if (ret) {
   		drm_client_buffer_delete(buffer);
   		return ERR_PTR(ret);
include/drm/drm_client.h +0 -5
index 39482527a775..b5acdab73766 100644
@@ -134,11 +134,6 @@ struct drm_client_buffer {
   	 */
   	struct drm_client_dev *client;
-	/**
-	 * @handle: Buffer handle
-	 */
-	u32 handle;
-
   	/**
   	 * @pitch: Buffer pitch
   	 */
Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev
#6 lore raw
Quoted 44 lines · Christian König

Am 16.02.23 um 15:34 schrieb Daniel Vetter:

Quoted 38 lines · Thomas Zimmermann
Quoted 18 lines

Hi

Am 26.01.23 um 11:28 schrieb Christian König:

Quoted 15 lines

We reference dump buffers both by their handle as well as their object. The problem is now that when anybody iterates over the DRM framebuffers and exports the underlying GEM objects through DMA-buf we run into a circular reference count situation.

The result is that the fbdev handling holds the GEM handle preventing the DMA-buf in the GEM object to be released. This DMA-buf in turn holds a reference to the driver module which on unload would release the fbdev.

Break that loop by releasing the handle as soon as the DRM framebuffer object is created. The DRM framebuffer and the DRM client buffer structure still hold a reference to the underlying GEM object preventing its destruction.

Quoted 8 lines
Quoted 3 lines
Fixes c76f0f7cb546 ("drm: Begin an API for in-kernel clients")

I tested with Weston and Gnome in X11 and Wayland mode under simpledrm, which I started stopped from the console. No obvious problems.

I heard that sway/wlroots has issues with drivers that don't support dma-buf. Maybe(!) that could be affected by this patch.

dma-buf export should still work. Also the loop is imo a red herring, I think if you force unbind the driver then this should all get resolved automatically.

What is true is that once we start refcounting everything correctly then there will be elevated module refcounts, which means you cannot use module unloading to provoke a driver unbind, which would kick out all the leftover references. You instead need to manually unbind the driver first, which should drop all remaining references to zero (might need to kill also any userspace), and only then can you unload the driver.

Quoted 3 lines

But this confusion is extremely common, a lot of people think that just holding a module reference is enough, we really should also hold a drm_device reference for dma-buf ...

Quoted 8 lines

Yeah, hot plug removal of amdgpu revealed a couple of those as well.

Essentially what DMA-buf does with grabbing a module reference on the owner of a DMA-buf is a bad idea.

Instead we should reference the device or component which is exporting the buffer, but since we don't have a common structure here it's more work to generalize that approach.

Well the device/component still needs to eventually hold a reference on the module, or bad things can happen. But yeah dma-buf also holding one but not a device/component reference is definitely bad. -Daniel

Quoted 128 lines

Christian.

Quoted 124 lines

-Daniel

Quoted 119 lines

Anyway, take my r-b, t-b tags.

Thank you for fixing this bug.

Best regards
Thomas

Quoted 92 lines
---
   drivers/gpu/drm/drm_client.c | 33 ++++++++++++++++++++-------------
   include/drm/drm_client.h     |  5 -----
   2 files changed, 20 insertions(+), 18 deletions(-)
drivers/gpu/drm/drm_client.c +20 -13
index 009e7b10455c..f6292ba0e6fc 100644
@@ -243,21 +243,17 @@ void drm_client_dev_restore(struct drm_device *dev)
   static void drm_client_buffer_delete(struct drm_client_buffer *buffer)
   {
-   struct drm_device *dev = buffer->client->dev;
-
    if (buffer->gem) {
            drm_gem_vunmap_unlocked(buffer->gem, &buffer->map);
            drm_gem_object_put(buffer->gem);
    }
-   if (buffer->handle)
-           drm_mode_destroy_dumb(dev, buffer->handle, buffer->client->file);
-
    kfree(buffer);
   }
   static struct drm_client_buffer *
-drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
+drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height,
+                    u32 format, u32 *handle)
   {
    const struct drm_format_info *info = drm_format_info(format);
    struct drm_mode_create_dumb dumb_args = { };
@@ -279,16 +275,15 @@ drm_client_buffer_create(struct drm_client_dev *client, u32 width, u32 height, u
    if (ret)
            goto err_delete;
-   buffer->handle = dumb_args.handle;
-   buffer->pitch = dumb_args.pitch;
-
    obj = drm_gem_object_lookup(client->file, dumb_args.handle);
    if (!obj)  {
            ret = -ENOENT;
            goto err_delete;
    }
+   buffer->pitch = dumb_args.pitch;
    buffer->gem = obj;
+   *handle = dumb_args.handle;
    return buffer;
@@ -375,7 +370,8 @@ static void drm_client_buffer_rmfb(struct drm_client_buffer *buffer)
   }
   static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
-                              u32 width, u32 height, u32 format)
+                              u32 width, u32 height, u32 format,
+                              u32 handle)
   {
    struct drm_client_dev *client = buffer->client;
    struct drm_mode_fb_cmd fb_req = { };
@@ -387,7 +383,7 @@ static int drm_client_buffer_addfb(struct drm_client_buffer *buffer,
    fb_req.depth = info->depth;
    fb_req.width = width;
    fb_req.height = height;
-   fb_req.handle = buffer->handle;
+   fb_req.handle = handle;
    fb_req.pitch = buffer->pitch;
    ret = drm_mode_addfb(client->dev, &fb_req, client->file);
@@ -424,13 +420,24 @@ struct drm_client_buffer *
   drm_client_framebuffer_create(struct drm_client_dev *client, u32 width, u32 height, u32 format)
   {
    struct drm_client_buffer *buffer;
+   u32 handle;
    int ret;
-   buffer = drm_client_buffer_create(client, width, height, format);
+   buffer = drm_client_buffer_create(client, width, height, format,
+                                     &handle);
    if (IS_ERR(buffer))
            return buffer;
-   ret = drm_client_buffer_addfb(buffer, width, height, format);
+   ret = drm_client_buffer_addfb(buffer, width, height, format, handle);
+
+   /*
+    * The handle is only needed for creating the framebuffer, destroy it
+    * again to solve a circular dependency should anybody export the GEM
+    * object as DMA-buf. The framebuffer and our buffer structure are still
+    * holding references to the GEM object to prevent its destruction.
+    */
+   drm_mode_destroy_dumb(client->dev, handle, client->file);
+
    if (ret) {
            drm_client_buffer_delete(buffer);
            return ERR_PTR(ret);
include/drm/drm_client.h +0 -0
index 39482527a775..b5acdab73766 100644

include/drm/drm_client.h struct drm_client_buffer { · line 138

Quoted 10 lines
include/drm/drm_client.h +0 -5
@@ -134,11 +134,6 @@ struct drm_client_buffer {
     */
    struct drm_client_dev *client;
-   /**
-    * @handle: Buffer handle
-    */
-   u32 handle;
-
    /**
     * @pitch: Buffer pitch
     */

--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch