Commit 2cbf7fc6 authored by Daniel Vetter's avatar Daniel Vetter

drm: Use drmm_ for drm_dev_init cleanup

Well for the simple stuff at least, vblank, gem and minor cleanup I
want to further split up as a demonstration.

v2: We need to clear drm_device->dev otherwise the debug drm printing
after our cleanup hook (e.g. in drm_manged_release) will chase
released memory and result in a use-after-free. Not really pretty, but
oh well.
Reviewed-by: default avatarSam Ravnborg <sam@ravnborg.org>
Signed-off-by: default avatarDaniel Vetter <daniel.vetter@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20200323144950.3018436-22-daniel.vetter@ffwll.ch
parent a5c71fdb
...@@ -580,6 +580,23 @@ static void drm_fs_inode_free(struct inode *inode) ...@@ -580,6 +580,23 @@ static void drm_fs_inode_free(struct inode *inode)
* used. * used.
*/ */
static void drm_dev_init_release(struct drm_device *dev, void *res)
{
drm_legacy_ctxbitmap_cleanup(dev);
drm_legacy_remove_map_hash(dev);
drm_fs_inode_free(dev->anon_inode);
put_device(dev->dev);
/* Prevent use-after-free in drm_managed_release when debugging is
* enabled. Slightly awkward, but can't really be helped. */
dev->dev = NULL;
mutex_destroy(&dev->master_mutex);
mutex_destroy(&dev->clientlist_mutex);
mutex_destroy(&dev->filelist_mutex);
mutex_destroy(&dev->struct_mutex);
drm_legacy_destroy_members(dev);
}
/** /**
* drm_dev_init - Initialise new DRM device * drm_dev_init - Initialise new DRM device
* @dev: DRM device * @dev: DRM device
...@@ -647,11 +664,15 @@ int drm_dev_init(struct drm_device *dev, ...@@ -647,11 +664,15 @@ int drm_dev_init(struct drm_device *dev,
mutex_init(&dev->clientlist_mutex); mutex_init(&dev->clientlist_mutex);
mutex_init(&dev->master_mutex); mutex_init(&dev->master_mutex);
ret = drmm_add_action(dev, drm_dev_init_release, NULL);
if (ret)
return ret;
dev->anon_inode = drm_fs_inode_new(); dev->anon_inode = drm_fs_inode_new();
if (IS_ERR(dev->anon_inode)) { if (IS_ERR(dev->anon_inode)) {
ret = PTR_ERR(dev->anon_inode); ret = PTR_ERR(dev->anon_inode);
DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret); DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
goto err_free; goto err;
} }
if (drm_core_check_feature(dev, DRIVER_RENDER)) { if (drm_core_check_feature(dev, DRIVER_RENDER)) {
...@@ -688,19 +709,12 @@ int drm_dev_init(struct drm_device *dev, ...@@ -688,19 +709,12 @@ int drm_dev_init(struct drm_device *dev,
if (drm_core_check_feature(dev, DRIVER_GEM)) if (drm_core_check_feature(dev, DRIVER_GEM))
drm_gem_destroy(dev); drm_gem_destroy(dev);
err_ctxbitmap: err_ctxbitmap:
drm_legacy_ctxbitmap_cleanup(dev);
drm_legacy_remove_map_hash(dev);
err_minors: err_minors:
drm_minor_free(dev, DRM_MINOR_PRIMARY); drm_minor_free(dev, DRM_MINOR_PRIMARY);
drm_minor_free(dev, DRM_MINOR_RENDER); drm_minor_free(dev, DRM_MINOR_RENDER);
drm_fs_inode_free(dev->anon_inode); err:
err_free: drm_managed_release(dev);
put_device(dev->dev);
mutex_destroy(&dev->master_mutex);
mutex_destroy(&dev->clientlist_mutex);
mutex_destroy(&dev->filelist_mutex);
mutex_destroy(&dev->struct_mutex);
drm_legacy_destroy_members(dev);
return ret; return ret;
} }
EXPORT_SYMBOL(drm_dev_init); EXPORT_SYMBOL(drm_dev_init);
...@@ -763,20 +777,8 @@ void drm_dev_fini(struct drm_device *dev) ...@@ -763,20 +777,8 @@ void drm_dev_fini(struct drm_device *dev)
if (drm_core_check_feature(dev, DRIVER_GEM)) if (drm_core_check_feature(dev, DRIVER_GEM))
drm_gem_destroy(dev); drm_gem_destroy(dev);
drm_legacy_ctxbitmap_cleanup(dev);
drm_legacy_remove_map_hash(dev);
drm_fs_inode_free(dev->anon_inode);
drm_minor_free(dev, DRM_MINOR_PRIMARY); drm_minor_free(dev, DRM_MINOR_PRIMARY);
drm_minor_free(dev, DRM_MINOR_RENDER); drm_minor_free(dev, DRM_MINOR_RENDER);
put_device(dev->dev);
mutex_destroy(&dev->master_mutex);
mutex_destroy(&dev->clientlist_mutex);
mutex_destroy(&dev->filelist_mutex);
mutex_destroy(&dev->struct_mutex);
drm_legacy_destroy_members(dev);
} }
EXPORT_SYMBOL(drm_dev_fini); EXPORT_SYMBOL(drm_dev_fini);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment