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

drm: cleanup drm_core_{init,exit}()

Various cleanups to the DRM core initialization and exit handlers:

- Register chrdev last: Once register_chrdev() returns, open() will
succeed on the given chrdevs. This is usually not an issue, as no
chardevs are registered, yet. However, nodes can be created by
user-space via mknod(2), even though such major/minor combinations are
unknown to the kernel. Avoid calling into drm_stub_open() in those
cases.
Again, drm_stub_open() would just bail out as the inode is unknown,
but it's really non-obvious if you hack on drm_stub_open().

- Unify error-paths into just one label. All the error-path helpers can
be called even though the constructors were not called yet, or failed.
Hence, just call all cleanups unconditionally.

- Call into drm_global_release(). This is a no-op, but provides
debugging helpers in case there're GLOBALS left on module unload. This
function was unused until now.

- Use DRM_ERROR() instead of printk(), and also print the error-code on
failure (even if it is static!).

- Don't throw away error-codes of register_chrdev()!

- Don't hardcode -1 as errno. This is just plain wrong.

- Order exit-handlers in the exact reverse order of initialization
(except if the order actually matters for syncing-reasons, which is
not the case here, though).

v2:
- Call drm_core_exit() directly from the init-error-handler. Requires to
drop __exit annotation, though.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/20160901124837.680-7-dh.herrmann@gmail.com

authored by

David Herrmann and committed by
Daniel Vetter
2cc107dc 82d5e73f

+22 -26
+22 -26
drivers/gpu/drm/drm_drv.c
··· 820 820 .llseek = noop_llseek, 821 821 }; 822 822 823 + static void drm_core_exit(void) 824 + { 825 + unregister_chrdev(DRM_MAJOR, "drm"); 826 + debugfs_remove(drm_debugfs_root); 827 + drm_sysfs_destroy(); 828 + idr_destroy(&drm_minors_idr); 829 + drm_connector_ida_destroy(); 830 + drm_global_release(); 831 + } 832 + 823 833 static int __init drm_core_init(void) 824 834 { 825 - int ret = -ENOMEM; 835 + int ret; 826 836 827 837 drm_global_init(); 828 838 drm_connector_ida_init(); 829 839 idr_init(&drm_minors_idr); 830 840 831 - if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops)) 832 - goto err_p1; 833 - 834 841 ret = drm_sysfs_init(); 835 842 if (ret < 0) { 836 - printk(KERN_ERR "DRM: Error creating drm class.\n"); 837 - goto err_p2; 843 + DRM_ERROR("Cannot create DRM class: %d\n", ret); 844 + goto error; 838 845 } 839 846 840 847 drm_debugfs_root = debugfs_create_dir("dri", NULL); 841 848 if (!drm_debugfs_root) { 842 - DRM_ERROR("Cannot create /sys/kernel/debug/dri\n"); 843 - ret = -1; 844 - goto err_p3; 849 + ret = -ENOMEM; 850 + DRM_ERROR("Cannot create debugfs-root: %d\n", ret); 851 + goto error; 845 852 } 853 + 854 + ret = register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops); 855 + if (ret < 0) 856 + goto error; 846 857 847 858 DRM_INFO("Initialized\n"); 848 859 return 0; 849 - err_p3: 850 - drm_sysfs_destroy(); 851 - err_p2: 852 - unregister_chrdev(DRM_MAJOR, "drm"); 853 860 854 - idr_destroy(&drm_minors_idr); 855 - err_p1: 861 + error: 862 + drm_core_exit(); 856 863 return ret; 857 - } 858 - 859 - static void __exit drm_core_exit(void) 860 - { 861 - debugfs_remove(drm_debugfs_root); 862 - drm_sysfs_destroy(); 863 - 864 - unregister_chrdev(DRM_MAJOR, "drm"); 865 - 866 - drm_connector_ida_destroy(); 867 - idr_destroy(&drm_minors_idr); 868 864 } 869 865 870 866 module_init(drm_core_init);