Skip to contentSkip to conversation

drm/udl: fix a NULL pointer reference in udl_gem_free_object().

PATCH Applied Haixia Shi 3 participants 9 messages Aug 30, 2016 – Sep 6, 2016

Subsystems (3)
lore
Share & tools
lore

Haixia Shi

author
#0 lore raw

PATCH 1/2 drm/udl: fix a NULL pointer reference in udl_gem_free_object().

Previously this function had a NULL pointer check for gem->map_list.map, but that line was refactored after commit 0de23977cfeb5b357ec884ba15417ae118ff9e9bb ("drm/gem: convert to new unified vma manager").

After the refactor it is still necessasry to check that the vma manager is not NULL because udl_gem_free_object() may come in after the vma manager is destroyed.

---
 drivers/gpu/drm/udl/udl_gem.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/udl/udl_gem.c +2 -1
index 818e707..72e1bd4 100644
@@ -204,7 +204,8 @@ void udl_gem_free_object(struct drm_gem_object *gem_obj)
 	if (obj->pages)
 		udl_gem_put_pages(obj);
 
-	drm_gem_free_mmap_offset(gem_obj);
+	if (gem_obj->dev->vma_offset_manager)
+		drm_gem_free_mmap_offset(gem_obj);
 }
 
 /* the dumb interface doesn't work with the GEM straight MMAP
Signature · 6 lines
2.8.0.rc3.226.g39d4020

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

PATCH 2/2 drm/udl: implement usb_driver suspend/resume.

The usb_driver suspend and resume function pointers must be populated to prevent forced unbinding of USB interface driver. See usb/core/driver.c:
unbind_no_pm_drivers_interfaces().

Restore mode and damage the entire frame buffer upon resume.

TEST=suspend and resume with the same UDL device connected TEST=suspend with UDL, unplug UDL and resume
TEST=suspend with UDL, unplug and connect another UDL device then resume

---
 drivers/gpu/drm/udl/udl_drv.c     | 17 +++++++++++++++++
 drivers/gpu/drm/udl/udl_drv.h     |  2 ++
 drivers/gpu/drm/udl/udl_modeset.c | 13 +++++++++++++
 3 files changed, 32 insertions(+)
drivers/gpu/drm/udl/udl_drv.c +17 -0
index 17d34e0..e5dc73b 100644
@@ -16,6 +16,21 @@ static int udl_driver_set_busid(struct drm_device *d, struct drm_master *m)
 	return 0;
 }
 
+static int udl_usb_suspend(struct usb_interface *interface,
+			   pm_message_t message)
+{
+	return 0;
+}
+
+static int udl_usb_resume(struct usb_interface *interface)
+{
+	struct drm_device *dev = usb_get_intfdata(interface);
+
+	udl_modeset_restore(dev);
+	return 0;
+}
+
+
 static const struct vm_operations_struct udl_gem_vm_ops = {
 	.fault = udl_gem_fault,
 	.open = drm_gem_vm_open,
@@ -122,6 +137,8 @@ static struct usb_driver udl_driver = {
 	.name = "udl",
 	.probe = udl_usb_probe,
 	.disconnect = udl_usb_disconnect,
+	.suspend = udl_usb_suspend,
+	.resume = udl_usb_resume,
 	.id_table = id_table,
 };
 module_usb_driver(udl_driver);
drivers/gpu/drm/udl/udl_drv.h +2 -0
index 0b03d34..f338a57 100644
@@ -52,6 +52,7 @@ struct udl_device {
 	struct device *dev;
 	struct drm_device *ddev;
 	struct usb_device *udev;
+	struct drm_crtc *crtc;
 
 	int sku_pixel_limit;
 
@@ -87,6 +88,7 @@ struct udl_framebuffer {
 
 /* modeset */
 int udl_modeset_init(struct drm_device *dev);
+void udl_modeset_restore(struct drm_device *dev);
 void udl_modeset_cleanup(struct drm_device *dev);
 int udl_connector_init(struct drm_device *dev, struct drm_encoder *encoder);
 
drivers/gpu/drm/udl/udl_modeset.c +13 -0
index 7369512..69d6a4f 100644
@@ -309,6 +309,8 @@ static int udl_crtc_mode_set(struct drm_crtc *crtc,
 	char *wrptr;
 	int color_depth = 0;
 
+	udl->crtc = crtc;
+
 	buf = (char *)udl->mode_buf;
 
 	/* for now we just clip 24 -> 16 - if we fix that fix this */
@@ -450,6 +452,17 @@ int udl_modeset_init(struct drm_device *dev)
 	return 0;
 }
 
+void udl_modeset_restore(struct drm_device *dev)
+{
+	struct udl_device *udl = dev->dev_private;
+	struct udl_framebuffer *ufb;
+	if (!udl->crtc || !udl->crtc->primary->fb)
+		return;
+	udl_crtc_commit(udl->crtc);
+	ufb = to_udl_fb(udl->crtc->primary->fb);
+	udl_handle_damage(ufb, 0, 0, ufb->base.width, ufb->base.height);
+}
+
 void udl_modeset_cleanup(struct drm_device *dev)
 {
 	drm_mode_config_cleanup(dev);
Signature · 6 lines
2.8.0.rc3.226.g39d4020

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

Re: [PATCH 2/2] drm/udl: implement usb_driver suspend/resume.

Quoted 4 lines · Haixia Shi

The usb_driver suspend and resume function pointers must be populated to prevent forced unbinding of USB interface driver. See usb/core/driver.c:
unbind_no_pm_drivers_interfaces().

Quoted 8 lines

Restore mode and damage the entire frame buffer upon resume.

TEST=suspend and resume with the same UDL device connected TEST=suspend with UDL, unplug UDL and resume
TEST=suspend with UDL, unplug and connect another UDL device then resume

I think this patch stands well on its own, and given the work required for 1/2:

I took the liberty of fixing your checkpatch warnings and gave marcheu his é back.

Applied to drm-misc

Sean

Quoted 95 lines
---
 drivers/gpu/drm/udl/udl_drv.c     | 17 +++++++++++++++++
 drivers/gpu/drm/udl/udl_drv.h     |  2 ++
 drivers/gpu/drm/udl/udl_modeset.c | 13 +++++++++++++
 3 files changed, 32 insertions(+)
drivers/gpu/drm/udl/udl_drv.c +17 -0
index 17d34e0..e5dc73b 100644
@@ -16,6 +16,21 @@ static int udl_driver_set_busid(struct drm_device *d, struct drm_master *m)
        return 0;
 }
 
+static int udl_usb_suspend(struct usb_interface *interface,
+                          pm_message_t message)
+{
+       return 0;
+}
+
+static int udl_usb_resume(struct usb_interface *interface)
+{
+       struct drm_device *dev = usb_get_intfdata(interface);
+
+       udl_modeset_restore(dev);
+       return 0;
+}
+
+
 static const struct vm_operations_struct udl_gem_vm_ops = {
        .fault = udl_gem_fault,
        .open = drm_gem_vm_open,
@@ -122,6 +137,8 @@ static struct usb_driver udl_driver = {
        .name = "udl",
        .probe = udl_usb_probe,
        .disconnect = udl_usb_disconnect,
+       .suspend = udl_usb_suspend,
+       .resume = udl_usb_resume,
        .id_table = id_table,
 };
 module_usb_driver(udl_driver);
drivers/gpu/drm/udl/udl_drv.h +2 -0
index 0b03d34..f338a57 100644
@@ -52,6 +52,7 @@ struct udl_device {
        struct device *dev;
        struct drm_device *ddev;
        struct usb_device *udev;
+       struct drm_crtc *crtc;
 
        int sku_pixel_limit;
 
@@ -87,6 +88,7 @@ struct udl_framebuffer {
 
 /* modeset */
 int udl_modeset_init(struct drm_device *dev);
+void udl_modeset_restore(struct drm_device *dev);
 void udl_modeset_cleanup(struct drm_device *dev);
 int udl_connector_init(struct drm_device *dev, struct drm_encoder *encoder);
 
drivers/gpu/drm/udl/udl_modeset.c +13 -1
index 7369512..69d6a4f 100644
@@ -309,6 +309,8 @@ static int udl_crtc_mode_set(struct drm_crtc *crtc,
        char *wrptr;
        int color_depth = 0;
 
+       udl->crtc = crtc;
+
        buf = (char *)udl->mode_buf;
 
        /* for now we just clip 24 -> 16 - if we fix that fix this */
@@ -450,6 +452,17 @@ int udl_modeset_init(struct drm_device *dev)
        return 0;
 }
 
+void udl_modeset_restore(struct drm_device *dev)
+{
+       struct udl_device *udl = dev->dev_private;
+       struct udl_framebuffer *ufb;
+       if (!udl->crtc || !udl->crtc->primary->fb)
+               return;
+       udl_crtc_commit(udl->crtc);
+       ufb = to_udl_fb(udl->crtc->primary->fb);
+       udl_handle_damage(ufb, 0, 0, ufb->base.width, ufb->base.height);
+}
+
 void udl_modeset_cleanup(struct drm_device *dev)
 {
        drm_mode_config_cleanup(dev);
--

2.8.0.rc3.226.g39d4020

Quoted 4 lines

_______________________________________________
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

#3 lore raw
In reply to Haixia Shi
Quoted 6 lines · Haixia Shi

Previously this function had a NULL pointer check for gem->map_list.map, but that line was refactored after commit 0de23977cfeb5b357ec884ba15417ae118ff9e9bb ("drm/gem: convert to new unified vma manager").

After the refactor it is still necessasry to check that the vma manager is not NULL because udl_gem_free_object() may come in after the vma manager is destroyed.

When/how does this happen? Backtrace? Destroying the vma manager before the objects are all gone sounds a bit fishy.
-Daniel

Quoted 28 lines
---
 drivers/gpu/drm/udl/udl_gem.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/udl/udl_gem.c +2 -1
index 818e707..72e1bd4 100644
@@ -204,7 +204,8 @@ void udl_gem_free_object(struct drm_gem_object *gem_obj)
 	if (obj->pages)
 		udl_gem_put_pages(obj);
 
-	drm_gem_free_mmap_offset(gem_obj);
+	if (gem_obj->dev->vma_offset_manager)
+		drm_gem_free_mmap_offset(gem_obj);
 }
 
 /* the dumb interface doesn't work with the GEM straight MMAP
Signature · 6 lines
2.8.0.rc3.226.g39d4020

_______________________________________________
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
author
#4 lore raw

For details see https://bugs.chromium.org/p/chromium/issues/detail?id=468050

So drm_mode_config_cleanup() is called from udl_driver_unload() in which we found there's still a framebuffer left, hence the WARN in drm_crtc.c:5495. This also forcefully releases all the buffers.

A bit later the actual drm_buf_release comes in which attempts to release the buffer again.

Quoted 37 lines · Daniel Vetter
Quoted 6 lines · Haixia Shi

Previously this function had a NULL pointer check for gem->map_list.map, but that line was refactored after commit 0de23977cfeb5b357ec884ba15417ae118ff9e9bb ("drm/gem: convert to new unified vma manager").

After the refactor it is still necessasry to check that the vma manager is not NULL because udl_gem_free_object() may come in after the vma manager is destroyed.

When/how does this happen? Backtrace? Destroying the vma manager before the objects are all gone sounds a bit fishy.
-Daniel

Quoted 24 lines
---
 drivers/gpu/drm/udl/udl_gem.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/udl/udl_gem.c +2 -2
index 818e707..72e1bd4 100644
@@ -204,7 +204,8 @@ void udl_gem_free_object(struct drm_gem_object *gem_obj)
      if (obj->pages)
              udl_gem_put_pages(obj);
 
-     drm_gem_free_mmap_offset(gem_obj);
+     if (gem_obj->dev->vma_offset_manager)
+             drm_gem_free_mmap_offset(gem_obj);
 }
 
 /* the dumb interface doesn't work with the GEM straight MMAP
--

2.8.0.rc3.226.g39d4020

Quoted 9 lines
Quoted 4 lines

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

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

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

#5 lore raw
Quoted 8 lines · Haixia Shi

For details see https://bugs.chromium.org/p/chromium/issues/detail?id=468050

So drm_mode_config_cleanup() is called from udl_driver_unload() in which we found there's still a framebuffer left, hence the WARN in drm_crtc.c:5495. This also forcefully releases all the buffers.

A bit later the actual drm_buf_release comes in which attempts to release the buffer again.

Leaving a drm_framebuffer behind on unload is definitely a bug, but not fixed with this patch here I think.

The dma-buf lifetime issue is far worse, since we simply don't handling those leaking past the lifetime of the exporting drm_device at all. The dma-buf has references to a lot more than just the vma manager. What we probably need is that every exported dma-buf holds a ref on the underlying drm_device, but that means untangling the refcounting&cleanup of that vs unplugging it.
-Daniel

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
#6 lore raw
Quoted 13 lines · Daniel Vetter
Quoted 8 lines · Haixia Shi

For details see https://bugs.chromium.org/p/chromium/issues/detail?id=468050

So drm_mode_config_cleanup() is called from udl_driver_unload() in which we found there's still a framebuffer left, hence the WARN in drm_crtc.c:5495. This also forcefully releases all the buffers.

A bit later the actual drm_buf_release comes in which attempts to release the buffer again.

Leaving a drm_framebuffer behind on unload is definitely a bug, but not fixed with this patch here I think.

Quoted 6 lines

The dma-buf lifetime issue is far worse, since we simply don't handling those leaking past the lifetime of the exporting drm_device at all. The dma-buf has references to a lot more than just the vma manager. What we probably need is that every exported dma-buf holds a ref on the underlying drm_device, but that means untangling the refcounting&cleanup of that vs unplugging it.

Just noticed that these problems started only when dma-buf export support was added (by you) to udl. That dma-buf support is a bit hack-ish (e.g. it leaks sg mappings since udl_unmap_dma_buf isn't implemented. Rough sketch of a fix:

- fix up udl_dmabuf.c. We could/should probably put most of these into the core as a set of helpers for drivers which use plain shmem gem objects.

- fix udl load/unload to no longer be midlayered, i.e. get rid of the ->load/unload hooks. There's tons of examples and drivers out there for templates.

- fix up the unplug hook to correctly unregister everything. With the fixed load/unload all the unplugged tracking is probably no longer neeeded. Also with this you can drop the drm_global_mutex dance from drm_unplug_dev. Also the sequence in drm_unplug_dev is wrong like the midlayered ->unload. First it should do all the unregistering, then release internal resources. Atm it's the other way round.

- make sure _all_ public objects (open files, counted by dev->open_count, dma-bufs, ...) hold a full reference onto the drm_device. For the dma-buf case this probably needs changes in drm_prime.c.

- Fix that little ordering issue with the leaking fb. It's probably not getting cleanup up as it should in udl_fbdev_unplug.

tada, bug fixed for real!

Cheers, Daniel

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

Daniel

Thanks! I agree the PATCH 1/2 needs some more work.

What do you think about the PATCH 2/2 (suspend/resume) -- would it make sense to review it as a single standalone patch?

Regards, Haixia

Quoted 49 lines · Daniel Vetter
Quoted 13 lines · Daniel Vetter
Quoted 8 lines · Haixia Shi

For details see https://bugs.chromium.org/p/chromium/issues/detail?id=468050

So drm_mode_config_cleanup() is called from udl_driver_unload() in which we found there's still a framebuffer left, hence the WARN in drm_crtc.c:5495. This also forcefully releases all the buffers.

A bit later the actual drm_buf_release comes in which attempts to release the buffer again.

Leaving a drm_framebuffer behind on unload is definitely a bug, but not fixed with this patch here I think.

Quoted 6 lines

The dma-buf lifetime issue is far worse, since we simply don't handling those leaking past the lifetime of the exporting drm_device at all. The dma-buf has references to a lot more than just the vma manager. What we probably need is that every exported dma-buf holds a ref on the underlying drm_device, but that means untangling the refcounting&cleanup of that vs unplugging it.

Just noticed that these problems started only when dma-buf export support was added (by you) to udl. That dma-buf support is a bit hack-ish (e.g. it leaks sg mappings since udl_unmap_dma_buf isn't implemented. Rough sketch of a fix:

- fix up udl_dmabuf.c. We could/should probably put most of these into the core as a set of helpers for drivers which use plain shmem gem objects.

- fix udl load/unload to no longer be midlayered, i.e. get rid of the ->load/unload hooks. There's tons of examples and drivers out there for templates.

- fix up the unplug hook to correctly unregister everything. With the fixed load/unload all the unplugged tracking is probably no longer neeeded. Also with this you can drop the drm_global_mutex dance from drm_unplug_dev. Also the sequence in drm_unplug_dev is wrong like the midlayered ->unload. First it should do all the unregistering, then release internal resources. Atm it's the other way round.

- make sure _all_ public objects (open files, counted by dev->open_count, dma-bufs, ...) hold a full reference onto the drm_device. For the dma-buf case this probably needs changes in drm_prime.c.

- Fix that little ordering issue with the leaking fb. It's probably not getting cleanup up as it should in udl_fbdev_unplug.

Quoted 7 lines

tada, bug fixed for real!

Cheers, Daniel
--
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

#8 lore raw
Quoted 6 lines · Haixia Shi

Daniel

Thanks! I agree the PATCH 1/2 needs some more work.

What do you think about the PATCH 2/2 (suspend/resume) -- would it make sense to review it as a single standalone patch?

Sure, but I have no clue about usb or udl-specific issues (I only analyzed the generic lifetime issues in patch 1/2), I think you need someone else for this. Sean Paul could probably help you out here. -Daniel

Quoted 60 lines

Regards, Haixia

Quoted 56 lines · Daniel Vetter
Quoted 13 lines · Daniel Vetter
Quoted 8 lines · Haixia Shi

For details see https://bugs.chromium.org/p/chromium/issues/detail?id=468050

So drm_mode_config_cleanup() is called from udl_driver_unload() in which we found there's still a framebuffer left, hence the WARN in drm_crtc.c:5495. This also forcefully releases all the buffers.

A bit later the actual drm_buf_release comes in which attempts to release the buffer again.

Leaving a drm_framebuffer behind on unload is definitely a bug, but not fixed with this patch here I think.

Quoted 6 lines

The dma-buf lifetime issue is far worse, since we simply don't handling those leaking past the lifetime of the exporting drm_device at all. The dma-buf has references to a lot more than just the vma manager. What we probably need is that every exported dma-buf holds a ref on the underlying drm_device, but that means untangling the refcounting&cleanup of that vs unplugging it.

Just noticed that these problems started only when dma-buf export support was added (by you) to udl. That dma-buf support is a bit hack-ish (e.g. it leaks sg mappings since udl_unmap_dma_buf isn't implemented. Rough sketch of a fix:

- fix up udl_dmabuf.c. We could/should probably put most of these into the core as a set of helpers for drivers which use plain shmem gem objects.

- fix udl load/unload to no longer be midlayered, i.e. get rid of the ->load/unload hooks. There's tons of examples and drivers out there for templates.

- fix up the unplug hook to correctly unregister everything. With the fixed load/unload all the unplugged tracking is probably no longer neeeded. Also with this you can drop the drm_global_mutex dance from drm_unplug_dev. Also the sequence in drm_unplug_dev is wrong like the midlayered ->unload. First it should do all the unregistering, then release internal resources. Atm it's the other way round.

- make sure _all_ public objects (open files, counted by dev->open_count, dma-bufs, ...) hold a full reference onto the drm_device. For the dma-buf case this probably needs changes in drm_prime.c.

- Fix that little ordering issue with the leaking fb. It's probably not getting cleanup up as it should in udl_fbdev_unplug.

tada, bug fixed for real!

Cheers, Daniel
--
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch

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