diff mbox

[v3] drm: Provide a driver hook for drm_dev_release()

Message ID 20170121105825.27661-1-chris@chris-wilson.co.uk (mailing list archive)
State New, archived
Headers show

Commit Message

Chris Wilson Jan. 21, 2017, 10:58 a.m. UTC
Some state is coupled into the device lifetime outside of the
load/unload timeframe and requires teardown during final unreference
from drm_dev_release(). For example, dmabufs hold both a device and
module reference and may live longer than expected (i.e. the current
pattern of the driver tearing down its state and then releasing a
reference to the drm device) and yet touch driver private state when
destroyed.

v2: Export drm_dev_fini() and move the responsible for finalizing the
drm_device and freeing it to the release callback. (If no callback is
provided, the core will call drm_dev_fini() and kfree(dev) as before.)
v3: Remember to add drm_dev_fini() to drm_drv.h

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
---
 drivers/gpu/drm/drm_drv.c | 56 +++++++++++++++++++++++++++++++++--------------
 include/drm/drm_drv.h     | 11 ++++++++++
 2 files changed, 50 insertions(+), 17 deletions(-)

Comments

Noralf Trønnes Jan. 21, 2017, 11:02 a.m. UTC | #1
Den 21.01.2017 11:58, skrev Chris Wilson:
> Some state is coupled into the device lifetime outside of the
> load/unload timeframe and requires teardown during final unreference
> from drm_dev_release(). For example, dmabufs hold both a device and
> module reference and may live longer than expected (i.e. the current
> pattern of the driver tearing down its state and then releasing a
> reference to the drm device) and yet touch driver private state when
> destroyed.
>
> v2: Export drm_dev_fini() and move the responsible for finalizing the
> drm_device and freeing it to the release callback. (If no callback is
> provided, the core will call drm_dev_fini() and kfree(dev) as before.)
> v3: Remember to add drm_dev_fini() to drm_drv.h
>
> Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
> Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
> ---
>   drivers/gpu/drm/drm_drv.c | 56 +++++++++++++++++++++++++++++++++--------------
>   include/drm/drm_drv.h     | 11 ++++++++++
>   2 files changed, 50 insertions(+), 17 deletions(-)
>
> diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c
> index 1b11ab628da7..517718e4f6e4 100644
> --- a/drivers/gpu/drm/drm_drv.c
> +++ b/drivers/gpu/drm/drm_drv.c
> @@ -553,6 +553,39 @@ int drm_dev_init(struct drm_device *dev,
>   EXPORT_SYMBOL(drm_dev_init);
>   
>   /**
> + * drm_dev_fini - Finalize a dead DRM device
> + * @dev: DRM device
> + *
> + * Finalize a dead DRM device. This is the converse to drm_dev_init() and
> + * frees up all state allocated by it. All driver state should be finalized
> + * first. Note that this function does not free the @dev, that is left to the
> + * caller. drm_dev_fini() should only

Looks like the end of the sentence is missing.

Noralf.

> + *
> + * The ref-count of @dev must be zero, and drm_dev_fini() should only be called
> + * from a drm_driver->release() callback.
> + */
> +void drm_dev_fini(struct drm_device *dev)
> +{
> +	if (drm_core_check_feature(dev, DRIVER_GEM))
> +		drm_gem_destroy(dev);
> +
> +	drm_legacy_ctxbitmap_cleanup(dev);
> +	drm_ht_remove(&dev->map_hash);
> +	drm_fs_inode_free(dev->anon_inode);
> +
> +	drm_minor_free(dev, DRM_MINOR_PRIMARY);
> +	drm_minor_free(dev, DRM_MINOR_RENDER);
> +	drm_minor_free(dev, DRM_MINOR_CONTROL);
> +
> +	mutex_destroy(&dev->master_mutex);
> +	mutex_destroy(&dev->ctxlist_mutex);
> +	mutex_destroy(&dev->filelist_mutex);
> +	mutex_destroy(&dev->struct_mutex);
> +	kfree(dev->unique);
> +}
> +EXPORT_SYMBOL(drm_dev_fini);
> +
> +/**
>    * drm_dev_alloc - Allocate new DRM device
>    * @driver: DRM driver to allocate device for
>    * @parent: Parent device object
> @@ -598,23 +631,12 @@ static void drm_dev_release(struct kref *ref)
>   {
>   	struct drm_device *dev = container_of(ref, struct drm_device, ref);
>   
> -	if (drm_core_check_feature(dev, DRIVER_GEM))
> -		drm_gem_destroy(dev);
> -
> -	drm_legacy_ctxbitmap_cleanup(dev);
> -	drm_ht_remove(&dev->map_hash);
> -	drm_fs_inode_free(dev->anon_inode);
> -
> -	drm_minor_free(dev, DRM_MINOR_PRIMARY);
> -	drm_minor_free(dev, DRM_MINOR_RENDER);
> -	drm_minor_free(dev, DRM_MINOR_CONTROL);
> -
> -	mutex_destroy(&dev->master_mutex);
> -	mutex_destroy(&dev->ctxlist_mutex);
> -	mutex_destroy(&dev->filelist_mutex);
> -	mutex_destroy(&dev->struct_mutex);
> -	kfree(dev->unique);
> -	kfree(dev);
> +	if (dev->driver->release) {
> +		dev->driver->release(dev);
> +	} else {
> +		drm_dev_fini(dev);
> +		kfree(dev);
> +	}
>   }
>   
>   /**
> diff --git a/include/drm/drm_drv.h b/include/drm/drm_drv.h
> index 34ece393c639..bc5865ad0bbd 100644
> --- a/include/drm/drm_drv.h
> +++ b/include/drm/drm_drv.h
> @@ -103,6 +103,15 @@ struct drm_driver {
>   	 *
>   	 */
>   	void (*unload) (struct drm_device *);
> +
> +	/**
> +	 * @release:
> +	 *
> +	 * Optional callback for destroying device state after the final
> +	 * reference is released, i.e. the device is being destroyed.
> +	 */
> +	void (*release) (struct drm_device *);
> +
>   	int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
>   	int (*dma_quiescent) (struct drm_device *);
>   	int (*context_dtor) (struct drm_device *dev, int context);
> @@ -451,6 +460,8 @@ extern unsigned int drm_debug;
>   int drm_dev_init(struct drm_device *dev,
>   		 struct drm_driver *driver,
>   		 struct device *parent);
> +void drm_dev_fini(struct drm_device *dev);
> +
>   struct drm_device *drm_dev_alloc(struct drm_driver *driver,
>   				 struct device *parent);
>   int drm_dev_register(struct drm_device *dev, unsigned long flags);
Chris Wilson Jan. 21, 2017, 11:12 a.m. UTC | #2
On Sat, Jan 21, 2017 at 12:02:03PM +0100, Noralf Trønnes wrote:
> 
> Den 21.01.2017 11:58, skrev Chris Wilson:
> >Some state is coupled into the device lifetime outside of the
> >load/unload timeframe and requires teardown during final unreference
> >from drm_dev_release(). For example, dmabufs hold both a device and
> >module reference and may live longer than expected (i.e. the current
> >pattern of the driver tearing down its state and then releasing a
> >reference to the drm device) and yet touch driver private state when
> >destroyed.
> >
> >v2: Export drm_dev_fini() and move the responsible for finalizing the
> >drm_device and freeing it to the release callback. (If no callback is
> >provided, the core will call drm_dev_fini() and kfree(dev) as before.)
> >v3: Remember to add drm_dev_fini() to drm_drv.h
> >
> >Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
> >Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> >Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
> >---
> >  drivers/gpu/drm/drm_drv.c | 56 +++++++++++++++++++++++++++++++++--------------
> >  include/drm/drm_drv.h     | 11 ++++++++++
> >  2 files changed, 50 insertions(+), 17 deletions(-)
> >
> >diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c
> >index 1b11ab628da7..517718e4f6e4 100644
> >--- a/drivers/gpu/drm/drm_drv.c
> >+++ b/drivers/gpu/drm/drm_drv.c
> >@@ -553,6 +553,39 @@ int drm_dev_init(struct drm_device *dev,
> >  EXPORT_SYMBOL(drm_dev_init);
> >  /**
> >+ * drm_dev_fini - Finalize a dead DRM device
> >+ * @dev: DRM device
> >+ *
> >+ * Finalize a dead DRM device. This is the converse to drm_dev_init() and
> >+ * frees up all state allocated by it. All driver state should be finalized
> >+ * first. Note that this function does not free the @dev, that is left to the
> >+ * caller. drm_dev_fini() should only
> 
> Looks like the end of the sentence is missing.

Ah, started then moved it to the note about ref-count.
-Chris
Laurent Pinchart Jan. 21, 2017, 9:21 p.m. UTC | #3
Hi Chris,

Thank you for the patch.

On Saturday 21 Jan 2017 10:58:25 Chris Wilson wrote:
> Some state is coupled into the device lifetime outside of the
> load/unload timeframe and requires teardown during final unreference
> from drm_dev_release(). For example, dmabufs hold both a device and
> module reference and may live longer than expected (i.e. the current
> pattern of the driver tearing down its state and then releasing a
> reference to the drm device) and yet touch driver private state when
> destroyed.
> 
> v2: Export drm_dev_fini() and move the responsible for finalizing the
> drm_device and freeing it to the release callback. (If no callback is
> provided, the core will call drm_dev_fini() and kfree(dev) as before.)
> v3: Remember to add drm_dev_fini() to drm_drv.h

This takes my comments into account, thank you for that. Do you have a patch 
that shows usage of the release callback in a driver ?

> Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
> Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
> ---
>  drivers/gpu/drm/drm_drv.c | 56 ++++++++++++++++++++++++++++++--------------
>  include/drm/drm_drv.h     | 11 ++++++++++
>  2 files changed, 50 insertions(+), 17 deletions(-)
> 
> diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c
> index 1b11ab628da7..517718e4f6e4 100644
> --- a/drivers/gpu/drm/drm_drv.c
> +++ b/drivers/gpu/drm/drm_drv.c
> @@ -553,6 +553,39 @@ int drm_dev_init(struct drm_device *dev,
>  EXPORT_SYMBOL(drm_dev_init);
> 
>  /**
> + * drm_dev_fini - Finalize a dead DRM device
> + * @dev: DRM device
> + *
> + * Finalize a dead DRM device. This is the converse to drm_dev_init() and
> + * frees up all state allocated by it. All driver state should be finalized
> + * first. Note that this function does not free the @dev, that is left to
> the + * caller. drm_dev_fini() should only
> + *
> + * The ref-count of @dev must be zero, and drm_dev_fini() should only be
> called + * from a drm_driver->release() callback.
> + */
> +void drm_dev_fini(struct drm_device *dev)
> +{
> +	if (drm_core_check_feature(dev, DRIVER_GEM))
> +		drm_gem_destroy(dev);
> +
> +	drm_legacy_ctxbitmap_cleanup(dev);
> +	drm_ht_remove(&dev->map_hash);
> +	drm_fs_inode_free(dev->anon_inode);
> +
> +	drm_minor_free(dev, DRM_MINOR_PRIMARY);
> +	drm_minor_free(dev, DRM_MINOR_RENDER);
> +	drm_minor_free(dev, DRM_MINOR_CONTROL);
> +
> +	mutex_destroy(&dev->master_mutex);
> +	mutex_destroy(&dev->ctxlist_mutex);
> +	mutex_destroy(&dev->filelist_mutex);
> +	mutex_destroy(&dev->struct_mutex);
> +	kfree(dev->unique);
> +}
> +EXPORT_SYMBOL(drm_dev_fini);
> +
> +/**
>   * drm_dev_alloc - Allocate new DRM device
>   * @driver: DRM driver to allocate device for
>   * @parent: Parent device object
> @@ -598,23 +631,12 @@ static void drm_dev_release(struct kref *ref)
>  {
>  	struct drm_device *dev = container_of(ref, struct drm_device, ref);
> 
> -	if (drm_core_check_feature(dev, DRIVER_GEM))
> -		drm_gem_destroy(dev);
> -
> -	drm_legacy_ctxbitmap_cleanup(dev);
> -	drm_ht_remove(&dev->map_hash);
> -	drm_fs_inode_free(dev->anon_inode);
> -
> -	drm_minor_free(dev, DRM_MINOR_PRIMARY);
> -	drm_minor_free(dev, DRM_MINOR_RENDER);
> -	drm_minor_free(dev, DRM_MINOR_CONTROL);
> -
> -	mutex_destroy(&dev->master_mutex);
> -	mutex_destroy(&dev->ctxlist_mutex);
> -	mutex_destroy(&dev->filelist_mutex);
> -	mutex_destroy(&dev->struct_mutex);
> -	kfree(dev->unique);
> -	kfree(dev);
> +	if (dev->driver->release) {
> +		dev->driver->release(dev);
> +	} else {
> +		drm_dev_fini(dev);
> +		kfree(dev);
> +	}
>  }
> 
>  /**
> diff --git a/include/drm/drm_drv.h b/include/drm/drm_drv.h
> index 34ece393c639..bc5865ad0bbd 100644
> --- a/include/drm/drm_drv.h
> +++ b/include/drm/drm_drv.h
> @@ -103,6 +103,15 @@ struct drm_driver {
>  	 *
>  	 */
>  	void (*unload) (struct drm_device *);
> +
> +	/**
> +	 * @release:
> +	 *
> +	 * Optional callback for destroying device state after the final
> +	 * reference is released, i.e. the device is being destroyed.
> +	 */
> +	void (*release) (struct drm_device *);
> +
>  	int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file
> *file_priv); int (*dma_quiescent) (struct drm_device *);
>  	int (*context_dtor) (struct drm_device *dev, int context);
> @@ -451,6 +460,8 @@ extern unsigned int drm_debug;
>  int drm_dev_init(struct drm_device *dev,
>  		 struct drm_driver *driver,
>  		 struct device *parent);
> +void drm_dev_fini(struct drm_device *dev);
> +
>  struct drm_device *drm_dev_alloc(struct drm_driver *driver,
>  				 struct device *parent);
>  int drm_dev_register(struct drm_device *dev, unsigned long flags);
Chris Wilson Jan. 23, 2017, 9:45 a.m. UTC | #4
On Sat, Jan 21, 2017 at 11:21:28PM +0200, Laurent Pinchart wrote:
> Hi Chris,
> 
> Thank you for the patch.
> 
> On Saturday 21 Jan 2017 10:58:25 Chris Wilson wrote:
> > Some state is coupled into the device lifetime outside of the
> > load/unload timeframe and requires teardown during final unreference
> > from drm_dev_release(). For example, dmabufs hold both a device and
> > module reference and may live longer than expected (i.e. the current
> > pattern of the driver tearing down its state and then releasing a
> > reference to the drm device) and yet touch driver private state when
> > destroyed.
> > 
> > v2: Export drm_dev_fini() and move the responsible for finalizing the
> > drm_device and freeing it to the release callback. (If no callback is
> > provided, the core will call drm_dev_fini() and kfree(dev) as before.)
> > v3: Remember to add drm_dev_fini() to drm_drv.h
> 
> This takes my comments into account, thank you for that. Do you have a patch 
> that shows usage of the release callback in a driver ?

I haven't yet dared start to split i915 between pci-device teardown and
system teardown, but
https://cgit.freedesktop.org/~ickle/linux-2.6/commit/?h=prescheduler&id=00a6ab3689ad09a0f2ea1df8e4a03a38721d2328
shows a use of driver->release to avoid the memory corruption with
dmabuf versus the virtual device. vgem should also provide a useful
example.
-Chris
diff mbox

Patch

diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c
index 1b11ab628da7..517718e4f6e4 100644
--- a/drivers/gpu/drm/drm_drv.c
+++ b/drivers/gpu/drm/drm_drv.c
@@ -553,6 +553,39 @@  int drm_dev_init(struct drm_device *dev,
 EXPORT_SYMBOL(drm_dev_init);
 
 /**
+ * drm_dev_fini - Finalize a dead DRM device
+ * @dev: DRM device
+ *
+ * Finalize a dead DRM device. This is the converse to drm_dev_init() and
+ * frees up all state allocated by it. All driver state should be finalized
+ * first. Note that this function does not free the @dev, that is left to the
+ * caller. drm_dev_fini() should only
+ *
+ * The ref-count of @dev must be zero, and drm_dev_fini() should only be called
+ * from a drm_driver->release() callback.
+ */
+void drm_dev_fini(struct drm_device *dev)
+{
+	if (drm_core_check_feature(dev, DRIVER_GEM))
+		drm_gem_destroy(dev);
+
+	drm_legacy_ctxbitmap_cleanup(dev);
+	drm_ht_remove(&dev->map_hash);
+	drm_fs_inode_free(dev->anon_inode);
+
+	drm_minor_free(dev, DRM_MINOR_PRIMARY);
+	drm_minor_free(dev, DRM_MINOR_RENDER);
+	drm_minor_free(dev, DRM_MINOR_CONTROL);
+
+	mutex_destroy(&dev->master_mutex);
+	mutex_destroy(&dev->ctxlist_mutex);
+	mutex_destroy(&dev->filelist_mutex);
+	mutex_destroy(&dev->struct_mutex);
+	kfree(dev->unique);
+}
+EXPORT_SYMBOL(drm_dev_fini);
+
+/**
  * drm_dev_alloc - Allocate new DRM device
  * @driver: DRM driver to allocate device for
  * @parent: Parent device object
@@ -598,23 +631,12 @@  static void drm_dev_release(struct kref *ref)
 {
 	struct drm_device *dev = container_of(ref, struct drm_device, ref);
 
-	if (drm_core_check_feature(dev, DRIVER_GEM))
-		drm_gem_destroy(dev);
-
-	drm_legacy_ctxbitmap_cleanup(dev);
-	drm_ht_remove(&dev->map_hash);
-	drm_fs_inode_free(dev->anon_inode);
-
-	drm_minor_free(dev, DRM_MINOR_PRIMARY);
-	drm_minor_free(dev, DRM_MINOR_RENDER);
-	drm_minor_free(dev, DRM_MINOR_CONTROL);
-
-	mutex_destroy(&dev->master_mutex);
-	mutex_destroy(&dev->ctxlist_mutex);
-	mutex_destroy(&dev->filelist_mutex);
-	mutex_destroy(&dev->struct_mutex);
-	kfree(dev->unique);
-	kfree(dev);
+	if (dev->driver->release) {
+		dev->driver->release(dev);
+	} else {
+		drm_dev_fini(dev);
+		kfree(dev);
+	}
 }
 
 /**
diff --git a/include/drm/drm_drv.h b/include/drm/drm_drv.h
index 34ece393c639..bc5865ad0bbd 100644
--- a/include/drm/drm_drv.h
+++ b/include/drm/drm_drv.h
@@ -103,6 +103,15 @@  struct drm_driver {
 	 *
 	 */
 	void (*unload) (struct drm_device *);
+
+	/**
+	 * @release:
+	 *
+	 * Optional callback for destroying device state after the final
+	 * reference is released, i.e. the device is being destroyed.
+	 */
+	void (*release) (struct drm_device *);
+
 	int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
 	int (*dma_quiescent) (struct drm_device *);
 	int (*context_dtor) (struct drm_device *dev, int context);
@@ -451,6 +460,8 @@  extern unsigned int drm_debug;
 int drm_dev_init(struct drm_device *dev,
 		 struct drm_driver *driver,
 		 struct device *parent);
+void drm_dev_fini(struct drm_device *dev);
+
 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
 				 struct device *parent);
 int drm_dev_register(struct drm_device *dev, unsigned long flags);