Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm: Provide a driver hook for drm_dev_release()

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 responsibility 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
v4: Tidy language for kerneldoc
v5: Cross reference from drm_dev_init() to note that driver->release()
allows for arbitrary embedding.
v6: Refer to driver data rather than driver state, as state is now
becoming associated with the struct drm_atomic_state and friends.

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
[danvet: Use the proper reference for struct members, which is
&drm_driver.release.]
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/20170202093632.31017-1-chris@chris-wilson.co.uk

authored by

Chris Wilson and committed by
Daniel Vetter
f30c9257 8aaacbc0

+58 -20
+45 -20
drivers/gpu/drm/drm_drv.c
··· 465 465 * that do embed &struct drm_device it must be placed first in the overall 466 466 * structure, and the overall structure must be allocated using kmalloc(): The 467 467 * drm core's release function unconditionally calls kfree() on the @dev pointer 468 - * when the final reference is released. 468 + * when the final reference is released. To override this behaviour, and so 469 + * allow embedding of the drm_device inside the driver's device struct at an 470 + * arbitrary offset, you must supply a &drm_driver.release callback and control 471 + * the finalization explicitly. 469 472 * 470 473 * RETURNS: 471 474 * 0 on success, or error code on failure. ··· 556 553 EXPORT_SYMBOL(drm_dev_init); 557 554 558 555 /** 556 + * drm_dev_fini - Finalize a dead DRM device 557 + * @dev: DRM device 558 + * 559 + * Finalize a dead DRM device. This is the converse to drm_dev_init() and 560 + * frees up all data allocated by it. All driver private data should be 561 + * finalized first. Note that this function does not free the @dev, that is 562 + * left to the caller. 563 + * 564 + * The ref-count of @dev must be zero, and drm_dev_fini() should only be called 565 + * from a &drm_driver.release callback. 566 + */ 567 + void drm_dev_fini(struct drm_device *dev) 568 + { 569 + drm_vblank_cleanup(dev); 570 + 571 + if (drm_core_check_feature(dev, DRIVER_GEM)) 572 + drm_gem_destroy(dev); 573 + 574 + drm_legacy_ctxbitmap_cleanup(dev); 575 + drm_ht_remove(&dev->map_hash); 576 + drm_fs_inode_free(dev->anon_inode); 577 + 578 + drm_minor_free(dev, DRM_MINOR_PRIMARY); 579 + drm_minor_free(dev, DRM_MINOR_RENDER); 580 + drm_minor_free(dev, DRM_MINOR_CONTROL); 581 + 582 + mutex_destroy(&dev->master_mutex); 583 + mutex_destroy(&dev->ctxlist_mutex); 584 + mutex_destroy(&dev->filelist_mutex); 585 + mutex_destroy(&dev->struct_mutex); 586 + kfree(dev->unique); 587 + } 588 + EXPORT_SYMBOL(drm_dev_fini); 589 + 590 + /** 559 591 * drm_dev_alloc - Allocate new DRM device 560 592 * @driver: DRM driver to allocate device for 561 593 * @parent: Parent device object ··· 636 598 { 637 599 struct drm_device *dev = container_of(ref, struct drm_device, ref); 638 600 639 - drm_vblank_cleanup(dev); 640 - 641 - if (drm_core_check_feature(dev, DRIVER_GEM)) 642 - drm_gem_destroy(dev); 643 - 644 - drm_legacy_ctxbitmap_cleanup(dev); 645 - drm_ht_remove(&dev->map_hash); 646 - drm_fs_inode_free(dev->anon_inode); 647 - 648 - drm_minor_free(dev, DRM_MINOR_PRIMARY); 649 - drm_minor_free(dev, DRM_MINOR_RENDER); 650 - drm_minor_free(dev, DRM_MINOR_CONTROL); 651 - 652 - mutex_destroy(&dev->master_mutex); 653 - mutex_destroy(&dev->ctxlist_mutex); 654 - mutex_destroy(&dev->filelist_mutex); 655 - mutex_destroy(&dev->struct_mutex); 656 - kfree(dev->unique); 657 - kfree(dev); 601 + if (dev->driver->release) { 602 + dev->driver->release(dev); 603 + } else { 604 + drm_dev_fini(dev); 605 + kfree(dev); 606 + } 658 607 } 659 608 660 609 /**
+13
include/drm/drm_drv.h
··· 102 102 * 103 103 */ 104 104 void (*unload) (struct drm_device *); 105 + 106 + /** 107 + * @release: 108 + * 109 + * Optional callback for destroying device data after the final 110 + * reference is released, i.e. the device is being destroyed. Drivers 111 + * using this callback are responsible for calling drm_dev_fini() 112 + * to finalize the device and then freeing the struct themselves. 113 + */ 114 + void (*release) (struct drm_device *); 115 + 105 116 int (*set_busid)(struct drm_device *dev, struct drm_master *master); 106 117 107 118 /** ··· 448 437 int drm_dev_init(struct drm_device *dev, 449 438 struct drm_driver *driver, 450 439 struct device *parent); 440 + void drm_dev_fini(struct drm_device *dev); 441 + 451 442 struct drm_device *drm_dev_alloc(struct drm_driver *driver, 452 443 struct device *parent); 453 444 int drm_dev_register(struct drm_device *dev, unsigned long flags);