If struct fb_ops is defined in a library like cma, fb_open() takes a
ref on the library instead of the driver module. Use
fb_ops.fb_open/fb_release to ensure that the driver module is pinned.

Add drm_fb_helper_fb_open() and drm_fb_helper_fb_release() to
DRM_FB_HELPER_DEFAULT_OPS().

Signed-off-by: Noralf Trønnes <nor...@tronnes.org>
---
 drivers/gpu/drm/drm_fb_helper.c | 41 +++++++++++++++++++++++++++++++++++++++++
 include/drm/drm_fb_helper.h     | 14 ++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/drivers/gpu/drm/drm_fb_helper.c b/drivers/gpu/drm/drm_fb_helper.c
index 74c1053..b080004 100644
--- a/drivers/gpu/drm/drm_fb_helper.c
+++ b/drivers/gpu/drm/drm_fb_helper.c
@@ -1037,6 +1037,47 @@ void drm_fb_helper_fb_destroy(struct fb_info *info)
 EXPORT_SYMBOL(drm_fb_helper_fb_destroy);
 
 /**
+ * drm_fb_helper_fb_open - implementation for &fb_ops.fb_open
+ * @info: fbdev registered by the helper
+ * @user: 1=userspace, 0=fbcon
+ *
+ * If &fb_ops is wrapped in a library, pin the driver module.
+ */
+int drm_fb_helper_fb_open(struct fb_info *info, int user)
+{
+       struct drm_fb_helper *fb_helper = info->par;
+       struct drm_device *dev = fb_helper->dev;
+
+       /* Skip fbcon, it detaches itself in unregister_framebuffer() */
+       if (user && info->fbops->owner != dev->driver->fops->owner) {
+               if (!try_module_get(dev->driver->fops->owner))
+                       return -ENODEV;
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL(drm_fb_helper_fb_open);
+
+/**
+ * drm_fb_helper_fb_release - implementation for &fb_ops.fb_release
+ * @info: fbdev registered by the helper
+ * @user: 1=userspace, 0=fbcon
+ *
+ * If &fb_ops is wrapped in a library, unpin the driver module.
+ */
+int drm_fb_helper_fb_release(struct fb_info *info, int user)
+{
+       struct drm_fb_helper *fb_helper = info->par;
+       struct drm_device *dev = fb_helper->dev;
+
+       if (user && info->fbops->owner != dev->driver->fops->owner)
+               module_put(dev->driver->fops->owner);
+
+       return 0;
+}
+EXPORT_SYMBOL(drm_fb_helper_fb_release);
+
+/**
  * drm_fb_helper_sys_read - wrapper around fb_sys_read
  * @info: fb_info struct pointer
  * @buf: userspace buffer to read from framebuffer memory
diff --git a/include/drm/drm_fb_helper.h b/include/drm/drm_fb_helper.h
index dd78eb3..b44fc62 100644
--- a/include/drm/drm_fb_helper.h
+++ b/include/drm/drm_fb_helper.h
@@ -233,6 +233,8 @@ struct drm_fb_helper {
  */
 #define DRM_FB_HELPER_DEFAULT_OPS \
        .fb_destroy     = drm_fb_helper_fb_destroy, \
+       .fb_open        = drm_fb_helper_fb_open, \
+       .fb_release     = drm_fb_helper_fb_release, \
        .fb_check_var   = drm_fb_helper_check_var, \
        .fb_set_par     = drm_fb_helper_set_par, \
        .fb_setcmap     = drm_fb_helper_setcmap, \
@@ -270,6 +272,8 @@ void drm_fb_helper_deferred_io(struct fb_info *info,
                               struct list_head *pagelist);
 
 void drm_fb_helper_fb_destroy(struct fb_info *info);
+int drm_fb_helper_fb_open(struct fb_info *info, int user);
+int drm_fb_helper_fb_release(struct fb_info *info, int user);
 
 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
                               size_t count, loff_t *ppos);
@@ -405,6 +409,16 @@ static inline void drm_fb_helper_fb_destroy(struct fb_info 
*info)
 {
 }
 
+static inline int drm_fb_helper_fb_open(struct fb_info *info, int user)
+{
+       return -ENODEV;
+}
+
+static inline int drm_fb_helper_fb_release(struct fb_info *info, int user)
+{
+       return -ENODEV;
+}
+
 static inline ssize_t drm_fb_helper_sys_read(struct fb_info *info,
                                             char __user *buf, size_t count,
                                             loff_t *ppos)
-- 
2.7.4

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Reply via email to