Skip to contentSkip to conversation

drm/fb-helper: unexpoert drm_fb_helper_generic_probe

PATCH In review Daniel Vetter 2 participants 5 messages Nov 12, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 drm/fb-helper: unexpoert drm_fb_helper_generic_probe

Not sure we don't yet have this as a patch somewhere ...

Motivation is that the automatic lifetime management of the generic fbdev code is quite tricky, and it'll get even more tricky. Allowing drivers to just use the fb_probe looks like a recipe for disaster.

Cc Gerd Hoffmann <[email protected]>
Cc Noralf Trønnes <[email protected]>
Cc Thomas Zimmermann <[email protected]>
---
 drivers/gpu/drm/drm_fb_helper.c | 14 +++-----------
 include/drm/drm_fb_helper.h     |  9 ---------
 2 files changed, 3 insertions(+), 20 deletions(-)
drivers/gpu/drm/drm_fb_helper.c +3 -11
index 1038a2f0639e..0ec98e046b59 100644
@@ -2035,21 +2035,14 @@ static struct fb_deferred_io drm_fbdev_defio = {
 	.deferred_io	= drm_fb_helper_deferred_io,
 };
 
-/**
- * drm_fb_helper_generic_probe - Generic fbdev emulation probe helper
- * @fb_helper: fbdev helper structure
- * @sizes: describes fbdev size and scanout surface size
- *
+/*
  * This function uses the client API to create a framebuffer backed by a dumb buffer.
  *
  * The _sys_ versions are used for &fb_ops.fb_read, fb_write, fb_fillrect,
  * fb_copyarea, fb_imageblit.
- *
- * Returns:
- * Zero on success or negative error code on failure.
  */
-int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper,
-				struct drm_fb_helper_surface_size *sizes)
+static int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper,
+				       struct drm_fb_helper_surface_size *sizes)
 {
 	struct drm_client_dev *client = &fb_helper->client;
 	struct drm_client_buffer *buffer;
@@ -2121,7 +2114,6 @@ int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper,
 
 	return 0;
 }
-EXPORT_SYMBOL(drm_fb_helper_generic_probe);
 
 static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs = {
 	.fb_probe = drm_fb_helper_generic_probe,
include/drm/drm_fb_helper.h +0 -9
index e3a75ff07390..dcffca73cd52 100644
@@ -272,8 +272,6 @@ int drm_fb_helper_debug_leave(struct fb_info *info);
 void drm_fb_helper_lastclose(struct drm_device *dev);
 void drm_fb_helper_output_poll_changed(struct drm_device *dev);
 
-int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper,
-				struct drm_fb_helper_surface_size *sizes);
 int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp);
 #else
 static inline void drm_fb_helper_prepare(struct drm_device *dev,
@@ -453,13 +451,6 @@ static inline void drm_fb_helper_output_poll_changed(struct drm_device *dev)
 {
 }
 
-static inline int
-drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper,
-			    struct drm_fb_helper_surface_size *sizes)
-{
-	return 0;
-}
-
 static inline int
 drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp)
 {
Signature · 6 lines
2.24.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

PATCH 2/2 drm/fb-helper: refcount drm_device for generic support

There's a pile of things protecting against racing hotunplugs:
- drm_dev_enter/exit against the underlying device disappearing
- drm_dev_get/put against drm_device disappearing
- we unregister everything in drm_dev_unregister to stop userspace from creating new open files to access the drm_device
- fbcon gets synchronously torn down (the console_lock is huge, so there's an upside to monolithic locking here).
- and for fbdev there's get_fb_info and put_fb_info

And I think we've almost got this right, except for a tiny thing: An fbdev ioctl might hold an elevate fb_info reference, which prevents us from doing the final cleanup in drm_fbdev_release. But it doesn't prevent the final cleanup of fbhelper->dev, which means even if the driver protects all hw access with drm_dev_enter/exit, we might oops way before that when trying to access freed memory.

Fix this by holding a drm_device reference for fbdev, which we drop from drm_fbdev_release.

This might lead to a reference loop, where the drm_device won't get cleaned up because of the fbdev reference, and fbdev isn't torn down yet because the drm_device cleanup code is written the wrong way round. But I think all drivers using the generic fbdev code get this right (because they use drm_client, so really can't get it wrong), hence this should be ok.

Also, more reasons to switch drivers over to generic fbdev support.

Since I had to re-review a pile of code also add a comment about why drm_fbdev_client_unregister can't race.

Cc Gerd Hoffmann <[email protected]>
Cc Noralf Trønnes <[email protected]>
Cc Thomas Zimmermann <[email protected]>

--
Entirely untested (aside from it compiles), but I think we need something like this to completely plug all fbdev related hotunplug issues. No need to roll out some fancy srcu scheme to fbdev, this seems to be solved with refcounting already.
-Daniel

---
 drivers/gpu/drm/drm_fb_helper.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/drm_fb_helper.c +4 -0
index 0ec98e046b59..62a1981d369e 100644
@@ -1992,6 +1992,7 @@ static void drm_fbdev_cleanup(struct drm_fb_helper *fb_helper)
 
 static void drm_fbdev_release(struct drm_fb_helper *fb_helper)
 {
+	drm_dev_put(fb_helper->dev);
 	drm_fbdev_cleanup(fb_helper);
 	drm_client_release(&fb_helper->client);
 	kfree(fb_helper);
@@ -2123,6 +2124,7 @@ static void drm_fbdev_client_unregister(struct drm_client_dev *client)
 {
 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
 
+	/* Protected against races by the clientlist_mutex. */
 	if (fb_helper->fbdev)
 		/* drm_fbdev_fb_destroy() takes care of cleanup */
 		drm_fb_helper_unregister_fbi(fb_helper);
@@ -2243,6 +2245,8 @@ int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp)
 		preferred_bpp = 32;
 	fb_helper->preferred_bpp = preferred_bpp;
 
+	drm_dev_get(dev);
+
 	ret = drm_fbdev_client_hotplug(&fb_helper->client);
 	if (ret)
 		DRM_DEV_DEBUG(dev->dev, "client hotplug ret=%d\n", ret);
Signature · 6 lines
2.24.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw

Re: [PATCH 2/2] drm/fb-helper: refcount drm_device for generic support

Den 12.11.2019 18.50, skrev Daniel Vetter:

Quoted 34 lines

There's a pile of things protecting against racing hotunplugs:
- drm_dev_enter/exit against the underlying device disappearing
- drm_dev_get/put against drm_device disappearing
- we unregister everything in drm_dev_unregister to stop userspace from creating new open files to access the drm_device
- fbcon gets synchronously torn down (the console_lock is huge, so there's an upside to monolithic locking here).
- and for fbdev there's get_fb_info and put_fb_info

And I think we've almost got this right, except for a tiny thing: An fbdev ioctl might hold an elevate fb_info reference, which prevents us from doing the final cleanup in drm_fbdev_release. But it doesn't prevent the final cleanup of fbhelper->dev, which means even if the driver protects all hw access with drm_dev_enter/exit, we might oops way before that when trying to access freed memory.

Fix this by holding a drm_device reference for fbdev, which we drop from drm_fbdev_release.

This might lead to a reference loop, where the drm_device won't get cleaned up because of the fbdev reference, and fbdev isn't torn down yet because the drm_device cleanup code is written the wrong way round. But I think all drivers using the generic fbdev code get this right (because they use drm_client, so really can't get it wrong), hence this should be ok.

Also, more reasons to switch drivers over to generic fbdev support.

Since I had to re-review a pile of code also add a comment about why drm_fbdev_client_unregister can't race.

Cc Gerd Hoffmann <[email protected]>
Cc Noralf Trønnes <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Quoted 5 lines

--
Entirely untested (aside from it compiles), but I think we need something like this to completely plug all fbdev related hotunplug issues. No need to roll out some fancy srcu scheme to fbdev, this seems to be solved with refcounting already.

We already hold a ref on drm_device taken in drm_client_init(). Hotunplug is working for generic fbdev, fbcon is automatically unbound and if userspace is open teardown is delayed until the fd is closed.

This comment here gives some hints:

/*
* fb_ops.fb_destroy is called by the last put_fb_info() call at the end of
* unregister_framebuffer() or fb_release().
*/
static void drm_fbdev_fb_destroy(struct fb_info *info)

{
	drm_fbdev_release(info->par);
}

Noralf.

Quoted 27 lines
(quoted diff) +0 -2
-Daniel
---
 drivers/gpu/drm/drm_fb_helper.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/drm_fb_helper.c +2 -0
index 0ec98e046b59..62a1981d369e 100644
@@ -1992,6 +1992,7 @@ static void drm_fbdev_cleanup(struct drm_fb_helper *fb_helper)
 
 static void drm_fbdev_release(struct drm_fb_helper *fb_helper)
 {
+	drm_dev_put(fb_helper->dev);
 	drm_fbdev_cleanup(fb_helper);
 	drm_client_release(&fb_helper->client);
 	kfree(fb_helper);
@@ -2123,6 +2124,7 @@ static void drm_fbdev_client_unregister(struct drm_client_dev *client)
 {
 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
 
+	/* Protected against races by the clientlist_mutex. */
 	if (fb_helper->fbdev)
 		/* drm_fbdev_fb_destroy() takes care of cleanup */
 		drm_fb_helper_unregister_fbi(fb_helper);

drivers/gpu/drm/drm_fb_helper.c int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp) · line 2252

Quoted 8 lines
drivers/gpu/drm/drm_fb_helper.c +2 -0
@@ -2243,6 +2245,8 @@ int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp)
 		preferred_bpp = 32;
 	fb_helper->preferred_bpp = preferred_bpp;
 
+	drm_dev_get(dev);
+
 	ret = drm_fbdev_client_hotplug(&fb_helper->client);
 	if (ret)
 		DRM_DEV_DEBUG(dev->dev, "client hotplug ret=%d\n", ret);

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw

Re: [PATCH 2/2] drm/fb-helper: refcount drm_device for generic support

Quoted 41 lines · Noralf Trønnes

Den 12.11.2019 18.50, skrev Daniel Vetter:

Quoted 34 lines

There's a pile of things protecting against racing hotunplugs:
- drm_dev_enter/exit against the underlying device disappearing
- drm_dev_get/put against drm_device disappearing
- we unregister everything in drm_dev_unregister to stop userspace from creating new open files to access the drm_device
- fbcon gets synchronously torn down (the console_lock is huge, so there's an upside to monolithic locking here).
- and for fbdev there's get_fb_info and put_fb_info

And I think we've almost got this right, except for a tiny thing: An fbdev ioctl might hold an elevate fb_info reference, which prevents us from doing the final cleanup in drm_fbdev_release. But it doesn't prevent the final cleanup of fbhelper->dev, which means even if the driver protects all hw access with drm_dev_enter/exit, we might oops way before that when trying to access freed memory.

Fix this by holding a drm_device reference for fbdev, which we drop from drm_fbdev_release.

This might lead to a reference loop, where the drm_device won't get cleaned up because of the fbdev reference, and fbdev isn't torn down yet because the drm_device cleanup code is written the wrong way round. But I think all drivers using the generic fbdev code get this right (because they use drm_client, so really can't get it wrong), hence this should be ok.

Also, more reasons to switch drivers over to generic fbdev support.

Since I had to re-review a pile of code also add a comment about why drm_fbdev_client_unregister can't race.

Cc Gerd Hoffmann <[email protected]>
Cc Noralf Trønnes <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Quoted 5 lines

--
Entirely untested (aside from it compiles), but I think we need something like this to completely plug all fbdev related hotunplug issues. No need to roll out some fancy srcu scheme to fbdev, this seems to be solved with refcounting already.

Quoted 3 lines

We already hold a ref on drm_device taken in drm_client_init(). Hotunplug is working for generic fbdev, fbcon is automatically unbound and if userspace is open teardown is delayed until the fd is closed.

Oh nice, I totally missed that.

Quoted 10 lines

This comment here gives some hints:

/*
* fb_ops.fb_destroy is called by the last put_fb_info() call at the end of
* unregister_framebuffer() or fb_release().
*/
static void drm_fbdev_fb_destroy(struct fb_info *info)

{
        drm_fbdev_release(info->par);
}

Yeah that part I got, but I missed that drm_client is holding a ref on the drm_device until drm_client_release time. I somehow thought we still had an issue there between fbdev references and drm_device teardown. But it's all fixed already.

I'll merge the first patch and drop this one, thanks for your review. -Daniel

Quoted 38 lines

Noralf.

Quoted 35 lines
(quoted diff) +0 -2
-Daniel
---
 drivers/gpu/drm/drm_fb_helper.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/drm_fb_helper.c +4 -0
index 0ec98e046b59..62a1981d369e 100644
@@ -1992,6 +1992,7 @@ static void drm_fbdev_cleanup(struct drm_fb_helper *fb_helper)
 
 static void drm_fbdev_release(struct drm_fb_helper *fb_helper)
 {
+     drm_dev_put(fb_helper->dev);
      drm_fbdev_cleanup(fb_helper);
      drm_client_release(&fb_helper->client);
      kfree(fb_helper);
@@ -2123,6 +2124,7 @@ static void drm_fbdev_client_unregister(struct drm_client_dev *client)
 {
      struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
 
+     /* Protected against races by the clientlist_mutex. */
      if (fb_helper->fbdev)
              /* drm_fbdev_fb_destroy() takes care of cleanup */
              drm_fb_helper_unregister_fbi(fb_helper);
@@ -2243,6 +2245,8 @@ int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp)
              preferred_bpp = 32;
      fb_helper->preferred_bpp = preferred_bpp;
 
+     drm_dev_get(dev);
+
      ret = drm_fbdev_client_hotplug(&fb_helper->client);
      if (ret)
              DRM_DEV_DEBUG(dev->dev, "client hotplug ret=%d\n", ret);
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v1 1/2
#4 lore raw
In reply to Daniel Vetter

Den 12.11.2019 18.50, skrev Daniel Vetter:

Quoted 11 lines

Not sure we don't yet have this as a patch somewhere ...

Motivation is that the automatic lifetime management of the generic fbdev code is quite tricky, and it'll get even more tricky. Allowing drivers to just use the fb_probe looks like a recipe for disaster.

Cc Gerd Hoffmann <[email protected]>
Cc Noralf Trønnes <[email protected]>
Cc Thomas Zimmermann <[email protected]>

---

This was needed during conversion of the cma-helper, and then I forgot...

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel