Skip to contentSkip to conversation

drm: udl: Destroy framebuffer only if it was initialized

PATCH Superseded Emil Lundmark 4 participants 8 messages Apr 20, 2018 – Sep 10, 2018

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

PATCH 1/1 drm: udl: Destroy framebuffer only if it was initialized

This fixes a NULL pointer dereference that can happen if the UDL driver is unloaded before the framebuffer is initialized. This can happen e.g. if the USB device is unplugged right after it was plugged in.

---
 drivers/gpu/drm/udl/udl_fb.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/udl/udl_fb.c +5 -3
index 2ebdc6d5a76e..5754e37f741b 100644
@@ -426,9 +426,11 @@ static void udl_fbdev_destroy(struct drm_device *dev,
 {
 	drm_fb_helper_unregister_fbi(&ufbdev->helper);
 	drm_fb_helper_fini(&ufbdev->helper);
-	drm_framebuffer_unregister_private(&ufbdev->ufb.base);
-	drm_framebuffer_cleanup(&ufbdev->ufb.base);
-	drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
+	if (ufbdev->ufb.obj) {
+		drm_framebuffer_unregister_private(&ufbdev->ufb.base);
+		drm_framebuffer_cleanup(&ufbdev->ufb.base);
+		drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
+	}
 }
 
 int udl_fbdev_init(struct drm_device *dev)
Signature · 1 line
2.17.0.484.g0c8726318c-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Emil Lundmark

This fixes a NULL pointer dereference that can happen if the UDL driver is unloaded before the framebuffer is initialized. This can happen e.g. if the USB device is unplugged right after it was plugged in.

JFYI, in future, if someone makes a suggestion on how to fix a bug, it's good practice to add a Suggested-by tag to give credit.

Quoted 27 lines
---
 drivers/gpu/drm/udl/udl_fb.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/udl/udl_fb.c +5 -3
index 2ebdc6d5a76e..5754e37f741b 100644
@@ -426,9 +426,11 @@ static void udl_fbdev_destroy(struct drm_device *dev,
 {
 	drm_fb_helper_unregister_fbi(&ufbdev->helper);
 	drm_fb_helper_fini(&ufbdev->helper);
-	drm_framebuffer_unregister_private(&ufbdev->ufb.base);
-	drm_framebuffer_cleanup(&ufbdev->ufb.base);
-	drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
+	if (ufbdev->ufb.obj) {
+		drm_framebuffer_unregister_private(&ufbdev->ufb.base);
+		drm_framebuffer_cleanup(&ufbdev->ufb.base);
+		drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
+	}
 }
 
 int udl_fbdev_init(struct drm_device *dev)
Signature · 1 line
2.17.0.484.g0c8726318c-goog
Signature · 1 line
Sean Paul, Software Engineer, Google / Chromium OS
#2 lore raw
Quoted 11 lines · Sean Paul
Quoted 5 lines · Emil Lundmark

This fixes a NULL pointer dereference that can happen if the UDL driver is unloaded before the framebuffer is initialized. This can happen e.g. if the USB device is unplugged right after it was plugged in.

JFYI, in future, if someone makes a suggestion on how to fix a bug, it's good practice to add a Suggested-by tag to give credit.

I think a bit more detail in the commit message on why this is even possible would be good. I think it can only happen when you only plug in the udl, without anything connected.

In that case fbdev setup will be delayed until something shows up (so we don't pin a too small fb for it, a feature requested by soc people). This can easily be tested:
First:
- plug in udl device
- wait a minute or so (to make it clear it's not a race)
- unplug

And then:
- plug in an udl device, but with something connected.
- unplug right away.

I expect that in the first case you'll always blow up, but in the 2nd one you'll never blow up (no matter how fast you unplug).

Cheers, Daniel

Quoted 35 lines
Quoted 28 lines
---
 drivers/gpu/drm/udl/udl_fb.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/udl/udl_fb.c +5 -3
index 2ebdc6d5a76e..5754e37f741b 100644
@@ -426,9 +426,11 @@ static void udl_fbdev_destroy(struct drm_device *dev,
 {
 	drm_fb_helper_unregister_fbi(&ufbdev->helper);
 	drm_fb_helper_fini(&ufbdev->helper);
-	drm_framebuffer_unregister_private(&ufbdev->ufb.base);
-	drm_framebuffer_cleanup(&ufbdev->ufb.base);
-	drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
+	if (ufbdev->ufb.obj) {
+		drm_framebuffer_unregister_private(&ufbdev->ufb.base);
+		drm_framebuffer_cleanup(&ufbdev->ufb.base);
+		drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
+	}
 }
 
 int udl_fbdev_init(struct drm_device *dev)
Signature · 1 line
2.17.0.484.g0c8726318c-goog
Signature · 5 lines
Sean Paul, Software Engineer, Google / Chromium OS
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#3 lore raw
Quoted 8 lines · Daniel Vetter
Quoted 7 lines · Sean Paul
Quoted 5 lines · Emil Lundmark

This fixes a NULL pointer dereference that can happen if the UDL driver is unloaded before the framebuffer is initialized. This can happen e.g. if the USB device is unplugged right after it was plugged in.

Quoted 8 lines
Quoted 4 lines

JFYI, in future, if someone makes a suggestion on how to fix a bug, it's good practice to add a Suggested-by tag to give credit.

I think a bit more detail in the commit message on why this is even possible would be good. I think it can only happen when you only plug in the udl, without anything connected.

Hehe, I was just reviewing this code internally, so I can answer that one :)

It happens when fbdev is disabled (which is the case for Chrome OS). Even though intialization of the fbdev part is optional (it's done in udlfb_create which is the callback for fb_probe()), the teardown isn't optional (udl_driver_unload -> udl_fbdev_cleanup -> udl_fbdev_destroy).

Note that udl_fbdev_cleanup *tries* to be conditional (you can see it does if (!udl->fbdev)) but that doesn't work, because udl->fbdev is always set during udl_fbdev_init.

Stéphane

Quoted 56 lines

In that case fbdev setup will be delayed until something shows up (so we don't pin a too small fb for it, a feature requested by soc people). This can easily be tested:
First:
- plug in udl device
- wait a minute or so (to make it clear it's not a race)
- unplug

And then:
- plug in an udl device, but with something connected.
- unplug right away.

I expect that in the first case you'll always blow up, but in the 2nd one you'll never blow up (no matter how fast you unplug).

Cheers, Daniel

Quoted 24 lines
Quoted 24 lines
---
 drivers/gpu/drm/udl/udl_fb.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/udl/udl_fb.c +5 -3
index 2ebdc6d5a76e..5754e37f741b 100644
@@ -426,9 +426,11 @@ static void udl_fbdev_destroy(struct drm_device *dev,
 {
    drm_fb_helper_unregister_fbi(&ufbdev->helper);
    drm_fb_helper_fini(&ufbdev->helper);
-   drm_framebuffer_unregister_private(&ufbdev->ufb.base);
-   drm_framebuffer_cleanup(&ufbdev->ufb.base);
-   drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
+   if (ufbdev->ufb.obj) {
+           drm_framebuffer_unregister_private(&ufbdev->ufb.base);
+           drm_framebuffer_cleanup(&ufbdev->ufb.base);
+           drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
+   }
 }
 
Quoted 11 lines
Quoted 4 lines
drivers/gpu/drm/udl/udl_fb.c +0 -1
 int udl_fbdev_init(struct drm_device *dev)
--

2.17.0.484.g0c8726318c-goog

--
Sean Paul, Software Engineer, Google / Chromium OS
_______________________________________________
dri-devel mailing list [email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Quoted 8 lines

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

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

#4 lore raw
Quoted 26 lines · Stéphane Marchesin
Quoted 8 lines · Daniel Vetter
Quoted 7 lines · Sean Paul
Quoted 5 lines · Emil Lundmark

This fixes a NULL pointer dereference that can happen if the UDL driver is unloaded before the framebuffer is initialized. This can happen e.g. if the USB device is unplugged right after it was plugged in.

Quoted 8 lines
Quoted 4 lines

JFYI, in future, if someone makes a suggestion on how to fix a bug, it's good practice to add a Suggested-by tag to give credit.

I think a bit more detail in the commit message on why this is even possible would be good. I think it can only happen when you only plug in the udl, without anything connected.

Hehe, I was just reviewing this code internally, so I can answer that one :)

It happens when fbdev is disabled (which is the case for Chrome OS). Even though intialization of the fbdev part is optional (it's done in udlfb_create which is the callback for fb_probe()), the teardown isn't optional (udl_driver_unload -> udl_fbdev_cleanup -> udl_fbdev_destroy).

Quoted 3 lines

Note that udl_fbdev_cleanup *tries* to be conditional (you can see it does if (!udl->fbdev)) but that doesn't work, because udl->fbdev is always set during udl_fbdev_init.

Ah right, that's another scenario where fbdev never gets fully initialized, totally forgot about it :-)

It's a bit unfortunately that we don't have a simple fix for this, i915 has a bunch of messy such checks too. Hopefully Noralf's work to make fbdev emulation look more like a normal kms client (which just happens to run within the kernel) will help here in the long run. -Daniel

Quoted 72 lines

Stéphane

Quoted 56 lines

In that case fbdev setup will be delayed until something shows up (so we don't pin a too small fb for it, a feature requested by soc people). This can easily be tested:
First:
- plug in udl device
- wait a minute or so (to make it clear it's not a race)
- unplug

And then:
- plug in an udl device, but with something connected.
- unplug right away.

I expect that in the first case you'll always blow up, but in the 2nd one you'll never blow up (no matter how fast you unplug).

Cheers, Daniel

Quoted 24 lines
Quoted 24 lines
---
 drivers/gpu/drm/udl/udl_fb.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/udl/udl_fb.c +5 -3
index 2ebdc6d5a76e..5754e37f741b 100644
@@ -426,9 +426,11 @@ static void udl_fbdev_destroy(struct drm_device *dev,
 {
    drm_fb_helper_unregister_fbi(&ufbdev->helper);
    drm_fb_helper_fini(&ufbdev->helper);
-   drm_framebuffer_unregister_private(&ufbdev->ufb.base);
-   drm_framebuffer_cleanup(&ufbdev->ufb.base);
-   drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
+   if (ufbdev->ufb.obj) {
+           drm_framebuffer_unregister_private(&ufbdev->ufb.base);
+           drm_framebuffer_cleanup(&ufbdev->ufb.base);
+           drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
+   }
 }
 
Quoted 11 lines
Quoted 4 lines
drivers/gpu/drm/udl/udl_fb.c +0 -1
 int udl_fbdev_init(struct drm_device *dev)
--

2.17.0.484.g0c8726318c-goog

--
Sean Paul, Software Engineer, Google / Chromium OS
_______________________________________________
dri-devel mailing list [email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Quoted 8 lines

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

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

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
#5 lore raw

PATCH v2 1/1 drm: udl: Destroy framebuffer only if it was initialized

In reply to Emil Lundmark

This fixes a NULL pointer dereference that can happen if the UDL driver is unloaded before the framebuffer is initialized. This can happen e.g. if the USB device is unplugged right after it was plugged in.

As explained by Stéphane Marchesin:

It happens when fbdev is disabled (which is the case for Chrome OS). Even though intialization of the fbdev part is optional (it's done in udlfb_create which is the callback for fb_probe()), the teardown isn't optional (udl_driver_unload -> udl_fbdev_cleanup -> udl_fbdev_destroy).

Note that udl_fbdev_cleanup *tries* to be conditional (you can see it does if (!udl->fbdev)) but that doesn't work, because udl->fbdev is always set during udl_fbdev_init.

---
Changes in v2:
- Updated commit message with explanation from Stéphane Marchesin

 drivers/gpu/drm/udl/udl_fb.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/udl/udl_fb.c +5 -3
index 2ebdc6d5a76e..5754e37f741b 100644
@@ -426,9 +426,11 @@ static void udl_fbdev_destroy(struct drm_device *dev,
 {
 	drm_fb_helper_unregister_fbi(&ufbdev->helper);
 	drm_fb_helper_fini(&ufbdev->helper);
-	drm_framebuffer_unregister_private(&ufbdev->ufb.base);
-	drm_framebuffer_cleanup(&ufbdev->ufb.base);
-	drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
+	if (ufbdev->ufb.obj) {
+		drm_framebuffer_unregister_private(&ufbdev->ufb.base);
+		drm_framebuffer_cleanup(&ufbdev->ufb.base);
+		drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
+	}
 }
 
 int udl_fbdev_init(struct drm_device *dev)
Signature · 1 line
2.17.0.921.gf22659ad46-goog
Acked-by on v2 1/1
#6 lore raw
Quoted 13 lines · Emil Lundmark

This fixes a NULL pointer dereference that can happen if the UDL driver is unloaded before the framebuffer is initialized. This can happen e.g. if the USB device is unplugged right after it was plugged in.

As explained by Stéphane Marchesin:

It happens when fbdev is disabled (which is the case for Chrome OS). Even though intialization of the fbdev part is optional (it's done in udlfb_create which is the callback for fb_probe()), the teardown isn't optional (udl_driver_unload -> udl_fbdev_cleanup -> udl_fbdev_destroy).

Quoted 6 lines

Note that udl_fbdev_cleanup *tries* to be conditional (you can see it does if (!udl->fbdev)) but that doesn't work, because udl->fbdev is always set during udl_fbdev_init.

You lost the r-b from Sean when resending, I'll leave that to Sean to readd when he merges. Anyway, lgtm now with the more detailed explanation.

Quoted 29 lines

---
Changes in v2:
- Updated commit message with explanation from Stéphane Marchesin

 drivers/gpu/drm/udl/udl_fb.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/udl/udl_fb.c +5 -3
index 2ebdc6d5a76e..5754e37f741b 100644
@@ -426,9 +426,11 @@ static void udl_fbdev_destroy(struct drm_device *dev,
 {
 	drm_fb_helper_unregister_fbi(&ufbdev->helper);
 	drm_fb_helper_fini(&ufbdev->helper);
-	drm_framebuffer_unregister_private(&ufbdev->ufb.base);
-	drm_framebuffer_cleanup(&ufbdev->ufb.base);
-	drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
+	if (ufbdev->ufb.obj) {
+		drm_framebuffer_unregister_private(&ufbdev->ufb.base);
+		drm_framebuffer_cleanup(&ufbdev->ufb.base);
+		drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
+	}
 }
 
 int udl_fbdev_init(struct drm_device *dev)
Signature · 1 line
2.17.0.921.gf22659ad46-goog
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#7 lore raw
In reply to Emil Lundmark
Quoted 14 lines · Emil Lundmark

This fixes a NULL pointer dereference that can happen if the UDL driver is unloaded before the framebuffer is initialized. This can happen e.g. if the USB device is unplugged right after it was plugged in.

As explained by Stéphane Marchesin:

It happens when fbdev is disabled (which is the case for Chrome OS). Even though intialization of the fbdev part is optional (it's done in udlfb_create which is the callback for fb_probe()), the teardown isn't optional (udl_driver_unload -> udl_fbdev_cleanup -> udl_fbdev_destroy).

Quoted 6 lines

Note that udl_fbdev_cleanup *tries* to be conditional (you can see it does if (!udl->fbdev)) but that doesn't work, because udl->fbdev is always set during udl_fbdev_init.

Many apologies, Emil, I completely dropped the ball on this one. I've just applied it to -misc-fixes.

Sean

Quoted 27 lines

---
Changes in v2:
- Updated commit message with explanation from Stéphane Marchesin

 drivers/gpu/drm/udl/udl_fb.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/udl/udl_fb.c +5 -4
index 2ebdc6d5a76e..5754e37f741b 100644
@@ -426,9 +426,11 @@ static void udl_fbdev_destroy(struct drm_device *dev,
 {
        drm_fb_helper_unregister_fbi(&ufbdev->helper);
        drm_fb_helper_fini(&ufbdev->helper);
-       drm_framebuffer_unregister_private(&ufbdev->ufb.base);
-       drm_framebuffer_cleanup(&ufbdev->ufb.base);
-       drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
+       if (ufbdev->ufb.obj) {
+               drm_framebuffer_unregister_private(&ufbdev->ufb.base);
+               drm_framebuffer_cleanup(&ufbdev->ufb.base);
+               drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
+       }
 }
 
 int udl_fbdev_init(struct drm_device *dev)
--
Quoted 2 lines

2.17.0.921.gf22659ad46-goog

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