drm: Add the new api to install irq
Subsystems (2)
PATCH v2 1/1 drm: Add the new api to install irq
Add new api devm_drm_irq_install() to register interrupts, no need to call drm_irq_uninstall() when the drm module is removed.
v2:
fixed the wrong parameter.
--- drivers/gpu/drm/drm_drv.c | 23 +++++++++++++++++++++++ include/drm/drm_drv.h | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_drv.c +23 -0
@@ -39,6 +39,7 @@#include <drm/drm_color_mgmt.h> #include <drm/drm_drv.h> #include <drm/drm_file.h> +#include <drm/drm_irq.h> #include <drm/drm_managed.h> #include <drm/drm_mode_object.h> #include <drm/drm_print.h>
@@ -678,6 +679,28 @@ static int devm_drm_dev_init(struct device *parent,return ret; } +static void devm_drm_dev_irq_uninstall (void *data) +{ +drm_irq_uninstall (data); +} + +int devm_drm_irq_install (struct device *parent, +struct drm_device *dev,int irq) +{ +int ret; + + ret =drm_irq_install (dev, irq); +if (ret) +return ret; + + ret =devm_add_action (parent, devm_drm_dev_irq_uninstall, dev); +if (ret) +devm_drm_dev_irq_uninstall (dev); + +return ret; +} +EXPORT_SYMBOL (devm_drm_irq_install); +void *__devm_drm_dev_alloc (struct device *parent,struct drm_driver *driver,size_t size,size_t offset) {
include/drm/drm_drv.h +2 -1
@@ -513,7 +513,8 @@ struct drm_driver {void *__devm_drm_dev_alloc (struct device *parent,struct drm_driver *driver,size_t size,size_t offset); - +int devm_drm_irq_install (struct device *parent,struct drm_device *dev, +int irq);/** * devm_drm_dev_alloc - Resource managed allocation of a &drm_device instance * @parent: Parent device object
Signature · 1 line
2.7.4
Thomas Zimmermann
maintainerHi
Thanks, the code looks good already. There just are a few nits below.
Am 03.11.20 um 03:10 schrieb Tian Tao:
drivers/gpu/drm/drm_drv.c
Quoted 16 lines
Add new api
devm_drm_irq_install()to register interrupts, no need to calldrm_irq_uninstall()when the drm module is removed.v2:
fixed the wrong parameter.Signed-off-by Tian Tao <[email protected]>--- drivers/gpu/drm/drm_drv.c | 23 +++++++++++++++++++++++ include/drm/drm_drv.h | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_drv.c +0
-0
The implementation should rather go to drm_irq.c
include/drm/drm_drv.h
Quoted 41 lines
drivers/gpu/drm/drm_drv.c +23
-0@@ -39,6 +39,7 @@#include <drm/drm_color_mgmt.h> #include <drm/drm_drv.h> #include <drm/drm_file.h> +#include <drm/drm_irq.h> #include <drm/drm_managed.h> #include <drm/drm_mode_object.h> #include <drm/drm_print.h> @@ -678,6 +679,28 @@ static int devm_drm_dev_init(struct device *parent,return ret; } +static void devm_drm_dev_irq_uninstall (void *data) +{ +drm_irq_uninstall (data); +} + +int devm_drm_irq_install (struct device *parent, +struct drm_device *dev,int irq) +{ +int ret; + + ret =drm_irq_install (dev, irq); +if (ret) +return ret; + + ret =devm_add_action (parent, devm_drm_dev_irq_uninstall, dev); +if (ret) +devm_drm_dev_irq_uninstall (dev); + +return ret; +} +EXPORT_SYMBOL (devm_drm_irq_install); +void *__devm_drm_dev_alloc (struct device *parent,struct drm_driver *driver,size_t size,size_t offset) {include/drm/drm_drv.h +0
-0
And the declaration should go to drm_irq.h
We generally don't merge unused code, so you should convert at least one KMS driver, say hibmc, to use the new interface.
Best regards
Thomas
Quoted 11 lines
include/drm/drm_drv.h +2
-1@@ -513,7 +513,8 @@ struct drm_driver {void *__devm_drm_dev_alloc (struct device *parent,struct drm_driver *driver,size_t size,size_t offset); - +int devm_drm_irq_install (struct device *parent,struct drm_device *dev, +int irq);/** * devm_drm_dev_alloc - Resource managed allocation of a &drm_device instance * @parent: Parent device object
Signature · 6 lines
Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Maxfeldstr. 5, 90409 Nürnberg, Germany (HRB 36809, AG Nürnberg) Geschäftsführer: Felix Imendörffer
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 4 lines · Thomas Zimmermann
Hi
Thanks, the code looks good already. There just are a few nits below.
Thanks for the help with the review code.
Add the new api devm_drm_irq_install and himbc use the new interface as one patch or two?
Quoted 82 lines
Am 03.11.20 um 03:10 schrieb Tian Tao:
drivers/gpu/drm/drm_drv.c
Quoted 16 lines
Add new api
devm_drm_irq_install()to register interrupts, no need to calldrm_irq_uninstall()when the drm module is removed.v2:
fixed the wrong parameter.Signed-off-by Tian Tao <[email protected]>--- drivers/gpu/drm/drm_drv.c | 23 +++++++++++++++++++++++ include/drm/drm_drv.h | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_drv.c +0
-0The implementation should rather go to
drm_irq.cinclude/drm/drm_drv.h
Quoted 41 lines
drivers/gpu/drm/drm_drv.c +23
-0@@ -39,6 +39,7 @@#include <drm/drm_color_mgmt.h> #include <drm/drm_drv.h> #include <drm/drm_file.h> +#include <drm/drm_irq.h> #include <drm/drm_managed.h> #include <drm/drm_mode_object.h> #include <drm/drm_print.h> @@ -678,6 +679,28 @@ static int devm_drm_dev_init(struct device *parent,return ret; } +static void devm_drm_dev_irq_uninstall (void *data) +{ +drm_irq_uninstall (data); +} + +int devm_drm_irq_install (struct device *parent, +struct drm_device *dev,int irq) +{ +int ret; + + ret =drm_irq_install (dev, irq); +if (ret) +return ret; + + ret =devm_add_action (parent, devm_drm_dev_irq_uninstall, dev); +if (ret) +devm_drm_dev_irq_uninstall (dev); + +return ret; +} +EXPORT_SYMBOL (devm_drm_irq_install); +void *__devm_drm_dev_alloc (struct device *parent,struct drm_driver *driver,size_t size,size_t offset) {include/drm/drm_drv.h +0
-0And the declaration should go to
drm_irq.hWe generally don't merge unused code, so you should convert at least one KMS driver, say hibmc, to use the new interface.
Best regards
ThomasQuoted 11 lines
include/drm/drm_drv.h +2
-1@@ -513,7 +513,8 @@ struct drm_driver {void *__devm_drm_dev_alloc (struct device *parent,struct drm_driver *driver,size_t size,size_t offset); - +int devm_drm_irq_install (struct device *parent,struct drm_device *dev, +int irq);/** * devm_drm_dev_alloc - Resource managed allocation of a &drm_device instance * @parent: Parent device object
Thomas Zimmermann
maintainerHi
Am 03.11.20 um 09:57 schrieb tiantao (H):
Quoted 10 lines · Thomas Zimmermann
Quoted 4 lines
Hi
Thanks, the code looks good already. There just are a few nits below.
Thanks for the help with the review code.
Add the new apidevm_drm_irq_installand himbc use the new interface as one patch or two?
Better make two patches from it.
Best regards
Thomas
Quoted 85 lines
Quoted 84 lines
Am 03.11.20 um 03:10 schrieb Tian Tao:
drivers/gpu/drm/drm_drv.c
Quoted 16 lines
Add new api
devm_drm_irq_install()to register interrupts, no need to calldrm_irq_uninstall()when the drm module is removed.v2:
fixed the wrong parameter.Signed-off-by Tian Tao <[email protected]>---
drivers/gpu/drm/drm_drv.c| 23 +++++++++++++++++++++++ include/drm/drm_drv.h | 3 ++-2 files changed, 25 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_drv.c +0
-0The implementation should rather go to
drm_irq.cinclude/drm/drm_drv.h
Quoted 41 lines
drivers/gpu/drm/drm_drv.c +22
-0@@ -39,6 +39,7 @@ #include <drm/drm_color_mgmt.h> #include <drm/drm_drv.h> #include <drm/drm_file.h> +#include <drm/drm_irq.h> #include <drm/drm_managed.h> #include <drm/drm_mode_object.h> #include <drm/drm_print.h>@@ -678,6 +679,28 @@ static int devm_drm_dev_init(struct device *parent, return ret; } +static void devm_drm_dev_irq_uninstall(void *data) +{ + drm_irq_uninstall(data); +} + +int devm_drm_irq_install (struct device *parent, + struct drm_device *dev, int irq) +{ + int ret; + + ret = drm_irq_install(dev, irq); + if (ret) + return ret; + + ret = devm_add_action(parent, devm_drm_dev_irq_uninstall, dev); + if (ret) + devm_drm_dev_irq_uninstall(dev); + + return ret; +} +EXPORT_SYMBOL (devm_drm_irq_install); +void *__devm_drm_dev_alloc(struct device *parent, struct
drm_driver*driver,
size_tsize,size_toffset)
{include/drm/drm_drv.h +0
-0And the declaration should go to
drm_irq.hWe generally don't merge unused code, so you should convert at least one KMS driver, say hibmc, to use the new interface.
Best regards
ThomasQuoted 12 lines
include/drm/drm_drv.h +0
-0@@ -513,7 +513,8 @@ struct drm_driver {void *__devm_drm_dev_alloc(struct device *parent, struct
drm_driver*driver,
size_tsize,size_toffset);include/drm/drm_drv.h +2
-1- +int devm_drm_irq_install (struct device *parent,struct drm_device *dev, + int irq);/**
*devm_drm_dev_alloc- Resource managed allocation of a&drm_deviceinstance
* @parent: Parent device object
Signature · 6 lines
Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Maxfeldstr. 5, 90409 Nürnberg, Germany (HRB 36809, AG Nürnberg) Geschäftsführer: Felix Imendörffer
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Hi Tian.
Good to see more infrastructure support so one does not have to think about cleanup.
Quoted 25 lines · Tian Tao
Add new api
devm_drm_irq_install()to register interrupts, no need to calldrm_irq_uninstall()when the drm module is removed.v2:
fixed the wrong parameter.Signed-off-by Tian Tao <[email protected]>--- drivers/gpu/drm/drm_drv.c | 23 +++++++++++++++++++++++ include/drm/drm_drv.h | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_drv.c +1
-0@@ -39,6 +39,7 @@#include <drm/drm_color_mgmt.h> #include <drm/drm_drv.h> #include <drm/drm_file.h> +#include <drm/drm_irq.h> #include <drm/drm_managed.h> #include <drm/drm_mode_object.h> #include <drm/drm_print.h>
drivers/gpu/drm/drm_drv.c static int devm_drm_dev_init(struct device *parent, · line 682
Quoted 4 lines
drivers/gpu/drm/drm_drv.c +1
-0@@ -678,6 +679,28 @@ static int devm_drm_dev_init(struct device *parent,return ret; } +static void devm_drm_dev_irq_uninstall (void *data)
devm_drm_irq_uninstall??
It matches other names if you drop the _dev part.
drivers/gpu/drm/drm_drv.c static int devm_drm_dev_init(struct device *parent, · line 688
Quoted 6 lines
drivers/gpu/drm/drm_drv.c +6
-0+{ +drm_irq_uninstall (data); +} + +int devm_drm_irq_install (struct device *parent, +struct drm_device *dev,int irq)
As this is an exported function please add appropriate kernel-doc documentation. It should for example explain that there is no need to uninstall as this is done automagically.
Quoted 4 lines
drivers/gpu/drm/drm_drv.c +4
-0+{ +int ret; + + ret =drm_irq_install (dev, irq);
drivers/gpu/drm/drm_drv.c static int devm_drm_dev_init(struct device *parent, · line 702
Quoted 10 lines
drivers/gpu/drm/drm_drv.c +10
-0+if (ret) +return ret; + + ret =devm_add_action (parent, devm_drm_dev_irq_uninstall, dev); +if (ret) +devm_drm_dev_irq_uninstall (dev); + +return ret; +} +EXPORT_SYMBOL (devm_drm_irq_install);
The above are in addition to Thomas' feedback.
Sam
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 40 lines · Tian Tao
Add new api
devm_drm_irq_install()to register interrupts, no need to calldrm_irq_uninstall()when the drm module is removed.v2:
fixed the wrong parameter.Signed-off-by Tian Tao <[email protected]>--- drivers/gpu/drm/drm_drv.c | 23 +++++++++++++++++++++++ include/drm/drm_drv.h | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_drv.c +13
-0@@ -39,6 +39,7 @@#include <drm/drm_color_mgmt.h> #include <drm/drm_drv.h> #include <drm/drm_file.h> +#include <drm/drm_irq.h> #include <drm/drm_managed.h> #include <drm/drm_mode_object.h> #include <drm/drm_print.h> @@ -678,6 +679,28 @@ static int devm_drm_dev_init(struct device *parent,return ret; } +static void devm_drm_dev_irq_uninstall (void *data) +{ +drm_irq_uninstall (data); +} + +int devm_drm_irq_install (struct device *parent, +struct drm_device *dev,int irq) +{ +int ret; + + ret =drm_irq_install (dev, irq); +if (ret)
drivers/gpu/drm/drm_drv.c static int devm_drm_dev_init(struct device *parent, · line 703
Quoted 10 lines
drivers/gpu/drm/drm_drv.c +10
-0+return ret; + + ret =devm_add_action (parent, devm_drm_dev_irq_uninstall, dev); +if (ret) +devm_drm_dev_irq_uninstall (dev); + +return ret; +} +EXPORT_SYMBOL (devm_drm_irq_install); +
Shouldn't we tie the IRQ to the drm device (so with drmm_add_action) instead of tying it to the underlying device?
Maxime
Quoted 1 line
Thomas Zimmermann
maintainerHi
Am 03.11.20 um 10:52 schrieb Maxime Ripard:
Quoted 46 lines · Tian Tao
Quoted 45 lines
Add new api
devm_drm_irq_install()to register interrupts, no need to calldrm_irq_uninstall()when the drm module is removed.v2:
fixed the wrong parameter.Signed-off-by Tian Tao <[email protected]>--- drivers/gpu/drm/drm_drv.c | 23 +++++++++++++++++++++++ include/drm/drm_drv.h | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_drv.c +18
-0@@ -39,6 +39,7 @@#include <drm/drm_color_mgmt.h> #include <drm/drm_drv.h> #include <drm/drm_file.h> +#include <drm/drm_irq.h> #include <drm/drm_managed.h> #include <drm/drm_mode_object.h> #include <drm/drm_print.h> @@ -678,6 +679,28 @@ static int devm_drm_dev_init(struct device *parent,return ret; } +static void devm_drm_dev_irq_uninstall (void *data) +{ +drm_irq_uninstall (data); +} + +int devm_drm_irq_install (struct device *parent, +struct drm_device *dev,int irq) +{ +int ret; + + ret =drm_irq_install (dev, irq); +if (ret) +return ret; + + ret =devm_add_action (parent, devm_drm_dev_irq_uninstall, dev); +if (ret) +devm_drm_dev_irq_uninstall (dev);
Quoted 8 lines
drivers/gpu/drm/drm_drv.c static int devm_drm_dev_init(struct device *parent, · line 703
Quoted 5 lines
drivers/gpu/drm/drm_drv.c +5
-0+ +return ret; +} +EXPORT_SYMBOL (devm_drm_irq_install); +Shouldn't we tie the IRQ to the drm device (so with
drmm_add_action) instead of tying it to the underlying device?
If the HW device goes away, there won't be any more interrupts. So it's similar to devm_ functions for I/O memory. Why would you use the drmm_ interface?
Best regards
Thomas
Quoted 3 lines
Maxime
Quoted 1 line
Signature · 6 lines
Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Maxfeldstr. 5, 90409 Nürnberg, Germany (HRB 36809, AG Nürnberg) Geschäftsführer: Felix Imendörffer
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 58 lines · Thomas Zimmermann
Hi
Am 03.11.20 um 10:52 schrieb Maxime Ripard:
Quoted 46 lines · Tian Tao
Quoted 45 lines
Add new api
devm_drm_irq_install()to register interrupts, no need to calldrm_irq_uninstall()when the drm module is removed.v2:
fixed the wrong parameter.Signed-off-by Tian Tao <[email protected]>--- drivers/gpu/drm/drm_drv.c | 23 +++++++++++++++++++++++ include/drm/drm_drv.h | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_drv.c +18
-0@@ -39,6 +39,7 @@#include <drm/drm_color_mgmt.h> #include <drm/drm_drv.h> #include <drm/drm_file.h> +#include <drm/drm_irq.h> #include <drm/drm_managed.h> #include <drm/drm_mode_object.h> #include <drm/drm_print.h> @@ -678,6 +679,28 @@ static int devm_drm_dev_init(struct device *parent,return ret; } +static void devm_drm_dev_irq_uninstall (void *data) +{ +drm_irq_uninstall (data); +} + +int devm_drm_irq_install (struct device *parent, +struct drm_device *dev,int irq) +{ +int ret; + + ret =drm_irq_install (dev, irq); +if (ret) +return ret; + + ret =devm_add_action (parent, devm_drm_dev_irq_uninstall, dev); +if (ret) +devm_drm_dev_irq_uninstall (dev);Quoted 8 lines
drivers/gpu/drm/drm_drv.c static int devm_drm_dev_init(struct device *parent, · line 703
Quoted 5 lines
drivers/gpu/drm/drm_drv.c +5
-0+ +return ret; +} +EXPORT_SYMBOL (devm_drm_irq_install); +Shouldn't we tie the IRQ to the drm device (so with
drmm_add_action) instead of tying it to the underlying device?
Quoted 3 lines
If the HW device goes away, there won't be any more interrupts. So it's similar to
devm_functions for I/O memory. Why would you use thedrmm_interface?
drm_irq_install/uninstall do more that just calling request_irq and free_irq though, they will also run (among other things) the irq-related hooks in the drm driver (irq_preinstall, irq_postinstall irq_uninstall) and wake anything waiting for a vblank to occur, so we need the DRM device and driver to still be around when we run drm_irq_uninstall. That's why (I assume) you have to pass the drm_device as an argument and not simply the device.
This probably works in most case since you would allocate the drm_device with devm_drm_dev_alloc, and then run drm_irq_install, so in the undoing phase you would have first drm_irq_uninstall to run, and everything is fine.
However, if one doesn't use devm_drm_dev_alloc but would use devm_drm_irq_install, you would have first remove being called that would free the drm device, and then drm_irq_uninstall which will use a free'd pointer.
So yeah, even though the interrupt line itself is tied to the device, all the logic we have around the interrupt that is dealt with in drm_irq_install is really tied to the drm_device. And since we tie the life of drm_device to its underlying device already (either implicitly through devm_drm_dev_alloc, or explictly through manual allocation in probe and free in remove) we can't end up in a situation where the drm_device outlives its device.
Maxime
Quoted 63 lines · Maxime Ripard
Quoted 58 lines · Thomas Zimmermann
Hi
Am 03.11.20 um 10:52 schrieb Maxime Ripard:
Quoted 46 lines · Tian Tao
Quoted 45 lines
Add new api
devm_drm_irq_install()to register interrupts, no need to calldrm_irq_uninstall()when the drm module is removed.v2:
fixed the wrong parameter.Signed-off-by Tian Tao <[email protected]>--- drivers/gpu/drm/drm_drv.c | 23 +++++++++++++++++++++++ include/drm/drm_drv.h | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_drv.c +18
-0@@ -39,6 +39,7 @@#include <drm/drm_color_mgmt.h> #include <drm/drm_drv.h> #include <drm/drm_file.h> +#include <drm/drm_irq.h> #include <drm/drm_managed.h> #include <drm/drm_mode_object.h> #include <drm/drm_print.h> @@ -678,6 +679,28 @@ static int devm_drm_dev_init(struct device *parent,return ret; } +static void devm_drm_dev_irq_uninstall (void *data) +{ +drm_irq_uninstall (data); +} + +int devm_drm_irq_install (struct device *parent, +struct drm_device *dev,int irq) +{ +int ret; + + ret =drm_irq_install (dev, irq); +if (ret) +return ret; + + ret =devm_add_action (parent, devm_drm_dev_irq_uninstall, dev); +if (ret) +devm_drm_dev_irq_uninstall (dev);Quoted 8 lines
drivers/gpu/drm/drm_drv.c static int devm_drm_dev_init(struct device *parent, · line 703
Quoted 5 lines
drivers/gpu/drm/drm_drv.c +5
-0+ +return ret; +} +EXPORT_SYMBOL (devm_drm_irq_install); +Shouldn't we tie the IRQ to the drm device (so with
drmm_add_action) instead of tying it to the underlying device?Quoted 3 lines
If the HW device goes away, there won't be any more interrupts. So it's similar to
devm_functions for I/O memory. Why would you use thedrmm_interface?
Quoted 7 lines
drm_irq_install/uninstall do more that just calling
request_irqandfree_irqthough, they will also run (among other things) the irq-related hooks in the drm driver (irq_preinstall,irq_postinstallirq_uninstall) and wake anything waiting for a vblank to occur, so we need the DRM device and driver to still be around when we rundrm_irq_uninstall. That's why (I assume) you have to pass thedrm_deviceas an argument and not simply the device.
drm_device is guaranteed to outlive devm_, plus the hooks are meant to shut down hw. hw isn't around anymore when we do drmm_ cleanup, at least not in full generality.
Quoted 9 lines
This probably works in most case since you would allocate the
drm_devicewithdevm_drm_dev_alloc, and then rundrm_irq_install, so in the undoing phase you would have firstdrm_irq_uninstallto run, and everything is fine.However, if one doesn't use
devm_drm_dev_allocbut would usedevm_drm_irq_install, you would have first remove being called that would free the drm device, and thendrm_irq_uninstallwhich will use a free'd pointer.
Don't do that, it's broken :-)
Quoted 7 lines
So yeah, even though the interrupt line itself is tied to the device, all the logic we have around the interrupt that is dealt with in
drm_irq_installis really tied to thedrm_device. And since we tie the life ofdrm_deviceto its underlying device already (either implicitly throughdevm_drm_dev_alloc, or explictly through manual allocation in probe and free in remove) we can't end up in a situation where thedrm_deviceoutlives its device.
Most drivers get their drm_device lifetime completely wrong. That's why I typed drmm_ stuff. So this isn't a surprise at all, but it might motiveate a bunch more people to fix this up correctly.
Also, these drm_irq functions are 100% optional helpers (should maybe rename them to make this clearer) for modern drivers. They're only built in for DRIVER_LEGACY, because hysterical raisins.
-Daniel
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
Thomas Zimmermann
maintainerHi
Am 03.11.20 um 11:55 schrieb Daniel Vetter:
Quoted 81 lines · Maxime Ripard
Quoted 63 lines · Thomas Zimmermann
Quoted 58 lines
Hi
Am 03.11.20 um 10:52 schrieb Maxime Ripard:
Quoted 46 lines · Tian Tao
Quoted 45 lines
Add new api
devm_drm_irq_install()to register interrupts, no need to calldrm_irq_uninstall()when the drm module is removed.v2:
fixed the wrong parameter.Signed-off-by Tian Tao <[email protected]>--- drivers/gpu/drm/drm_drv.c | 23 +++++++++++++++++++++++ include/drm/drm_drv.h | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_drv.c +18
-0@@ -39,6 +39,7 @@#include <drm/drm_color_mgmt.h> #include <drm/drm_drv.h> #include <drm/drm_file.h> +#include <drm/drm_irq.h> #include <drm/drm_managed.h> #include <drm/drm_mode_object.h> #include <drm/drm_print.h> @@ -678,6 +679,28 @@ static int devm_drm_dev_init(struct device *parent,return ret; } +static void devm_drm_dev_irq_uninstall (void *data) +{ +drm_irq_uninstall (data); +} + +int devm_drm_irq_install (struct device *parent, +struct drm_device *dev,int irq) +{ +int ret; + + ret =drm_irq_install (dev, irq); +if (ret) +return ret; + + ret =devm_add_action (parent, devm_drm_dev_irq_uninstall, dev); +if (ret) +devm_drm_dev_irq_uninstall (dev);Quoted 8 lines
drivers/gpu/drm/drm_drv.c static int devm_drm_dev_init(struct device *parent, · line 703
Quoted 5 lines
drivers/gpu/drm/drm_drv.c +5
-0+ +return ret; +} +EXPORT_SYMBOL (devm_drm_irq_install); +Shouldn't we tie the IRQ to the drm device (so with
drmm_add_action) instead of tying it to the underlying device?Quoted 3 lines
If the HW device goes away, there won't be any more interrupts. So it's similar to
devm_functions for I/O memory. Why would you use thedrmm_interface?Quoted 7 lines
drm_irq_install/uninstall do more that just calling
request_irqandfree_irqthough, they will also run (among other things) the irq-related hooks in the drm driver (irq_preinstall,irq_postinstallirq_uninstall) and wake anything waiting for a vblank to occur, so we need the DRM device and driver to still be around when we rundrm_irq_uninstall. That's why (I assume) you have to pass thedrm_deviceas an argument and not simply the device.
drm_deviceis guaranteed to outlivedevm_, plus the hooks are meant to shut down hw. hw isn't around anymore when we dodrmm_cleanup, at least not in full generality.Quoted 5 lines
This probably works in most case since you would allocate the
drm_devicewithdevm_drm_dev_alloc, and then rundrm_irq_install, so in the undoing phase you would have firstdrm_irq_uninstallto run, and everything is fine.
Quoted 6 lines
Quoted 4 lines
However, if one doesn't use
devm_drm_dev_allocbut would usedevm_drm_irq_install, you would have first remove being called that would free the drm device, and thendrm_irq_uninstallwhich will use a free'd pointer.Don't do that, it's broken :-)
Umm, I just saw that hibmc doesn't use devm_drm_dev_alloc. So maybe we have to convert it first before using the managed irq function. OTOH converting it is a good idea in any case, so why not. :)
Best regards
Thomas
Quoted 18 lines
Quoted 8 lines
So yeah, even though the interrupt line itself is tied to the device, all the logic we have around the interrupt that is dealt with in
drm_irq_installis really tied to thedrm_device. And since we tie the life ofdrm_deviceto its underlying device already (either implicitly throughdevm_drm_dev_alloc, or explictly through manual allocation in probe and free in remove) we can't end up in a situation where thedrm_deviceoutlives its device.Most drivers get their
drm_devicelifetime completely wrong. That's why I typeddrmm_stuff. So this isn't a surprise at all, but it might motiveate a bunch more people to fix this up correctly.Also, these
drm_irqfunctions are 100% optional helpers (should maybe rename them to make this clearer) for modern drivers. They're only built in forDRIVER_LEGACY, because hysterical raisins.
-Daniel
Signature · 6 lines
Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Maxfeldstr. 5, 90409 Nürnberg, Germany (HRB 36809, AG Nürnberg) Geschäftsführer: Felix Imendörffer
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 89 lines · Thomas Zimmermann
Hi
Am 03.11.20 um 11:55 schrieb Daniel Vetter:
Quoted 86 lines · Maxime Ripard
Quoted 63 lines · Thomas Zimmermann
Quoted 58 lines
Hi
Am 03.11.20 um 10:52 schrieb Maxime Ripard:
Quoted 46 lines · Tian Tao
Quoted 45 lines
Add new api
devm_drm_irq_install()to register interrupts, no need to calldrm_irq_uninstall()when the drm module is removed.v2:
fixed the wrong parameter.Signed-off-by Tian Tao <[email protected]>--- drivers/gpu/drm/drm_drv.c | 23 +++++++++++++++++++++++ include/drm/drm_drv.h | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_drv.c +18
-0@@ -39,6 +39,7 @@#include <drm/drm_color_mgmt.h> #include <drm/drm_drv.h> #include <drm/drm_file.h> +#include <drm/drm_irq.h> #include <drm/drm_managed.h> #include <drm/drm_mode_object.h> #include <drm/drm_print.h> @@ -678,6 +679,28 @@ static int devm_drm_dev_init(struct device *parent,return ret; } +static void devm_drm_dev_irq_uninstall (void *data) +{ +drm_irq_uninstall (data); +} + +int devm_drm_irq_install (struct device *parent, +struct drm_device *dev,int irq) +{ +int ret; + + ret =drm_irq_install (dev, irq); +if (ret) +return ret; + + ret =devm_add_action (parent, devm_drm_dev_irq_uninstall, dev); +if (ret) +devm_drm_dev_irq_uninstall (dev);Quoted 8 lines
drivers/gpu/drm/drm_drv.c static int devm_drm_dev_init(struct device *parent, · line 703
Quoted 5 lines
drivers/gpu/drm/drm_drv.c +5
-0+ +return ret; +} +EXPORT_SYMBOL (devm_drm_irq_install); +Shouldn't we tie the IRQ to the drm device (so with
drmm_add_action) instead of tying it to the underlying device?Quoted 3 lines
If the HW device goes away, there won't be any more interrupts. So it's similar to
devm_functions for I/O memory. Why would you use thedrmm_interface?Quoted 7 lines
drm_irq_install/uninstall do more that just calling
request_irqandfree_irqthough, they will also run (among other things) the irq-related hooks in the drm driver (irq_preinstall,irq_postinstallirq_uninstall) and wake anything waiting for a vblank to occur, so we need the DRM device and driver to still be around when we rundrm_irq_uninstall. That's why (I assume) you have to pass thedrm_deviceas an argument and not simply the device.
drm_deviceis guaranteed to outlivedevm_, plus the hooks are meant to shut down hw. hw isn't around anymore when we dodrmm_cleanup, at least not in full generality.Quoted 9 lines
This probably works in most case since you would allocate the
drm_devicewithdevm_drm_dev_alloc, and then rundrm_irq_install, so in the undoing phase you would have firstdrm_irq_uninstallto run, and everything is fine.However, if one doesn't use
devm_drm_dev_allocbut would usedevm_drm_irq_install, you would have first remove being called that would free the drm device, and thendrm_irq_uninstallwhich will use a free'd pointer.
Quoted 5 lines
Quoted 1 line
Don't do that, it's broken :-)
Umm, I just saw that hibmc doesn't use
devm_drm_dev_alloc. So maybe we have to convert it first before using the managed irq function. OTOH converting it is a good idea in any case, so why not. :)
Yeah that doesn't work as-is.
I guess the second option would be if devm_drm_dev_irqinstall would take a drm_dev_get() reference of its own. Not sure that's a good idea, but it would make the thing a bit more flexible.
-Daniel
Quoted 40 lines
Best regards
ThomasQuoted 18 lines
Quoted 8 lines
So yeah, even though the interrupt line itself is tied to the device, all the logic we have around the interrupt that is dealt with in
drm_irq_installis really tied to thedrm_device. And since we tie the life ofdrm_deviceto its underlying device already (either implicitly throughdevm_drm_dev_alloc, or explictly through manual allocation in probe and free in remove) we can't end up in a situation where thedrm_deviceoutlives its device.Most drivers get their
drm_devicelifetime completely wrong. That's why I typeddrmm_stuff. So this isn't a surprise at all, but it might motiveate a bunch more people to fix this up correctly.Also, these
drm_irqfunctions are 100% optional helpers (should maybe rename them to make this clearer) for modern drivers. They're only built in forDRIVER_LEGACY, because hysterical raisins.
-DanielSignature · 16 lines
Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Maxfeldstr. 5, 90409 Nürnberg, Germany (HRB 36809, AG Nürnberg) Geschäftsführer: Felix Imendörffer _______________________________________________ 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
Hi!
Quoted 72 lines · Daniel Vetter
Quoted 63 lines · Maxime Ripard
Quoted 58 lines · Thomas Zimmermann
Hi
Am 03.11.20 um 10:52 schrieb Maxime Ripard:
Quoted 46 lines · Tian Tao
Quoted 45 lines
Add new api
devm_drm_irq_install()to register interrupts, no need to calldrm_irq_uninstall()when the drm module is removed.v2:
fixed the wrong parameter.Signed-off-by Tian Tao <[email protected]>--- drivers/gpu/drm/drm_drv.c | 23 +++++++++++++++++++++++ include/drm/drm_drv.h | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_drv.c +18
-0@@ -39,6 +39,7 @@#include <drm/drm_color_mgmt.h> #include <drm/drm_drv.h> #include <drm/drm_file.h> +#include <drm/drm_irq.h> #include <drm/drm_managed.h> #include <drm/drm_mode_object.h> #include <drm/drm_print.h> @@ -678,6 +679,28 @@ static int devm_drm_dev_init(struct device *parent,return ret; } +static void devm_drm_dev_irq_uninstall (void *data) +{ +drm_irq_uninstall (data); +} + +int devm_drm_irq_install (struct device *parent, +struct drm_device *dev,int irq) +{ +int ret; + + ret =drm_irq_install (dev, irq); +if (ret) +return ret; + + ret =devm_add_action (parent, devm_drm_dev_irq_uninstall, dev); +if (ret) +devm_drm_dev_irq_uninstall (dev);Quoted 8 lines
drivers/gpu/drm/drm_drv.c static int devm_drm_dev_init(struct device *parent, · line 703
Quoted 5 lines
drivers/gpu/drm/drm_drv.c +5
-0+ +return ret; +} +EXPORT_SYMBOL (devm_drm_irq_install); +Shouldn't we tie the IRQ to the drm device (so with
drmm_add_action) instead of tying it to the underlying device?Quoted 3 lines
If the HW device goes away, there won't be any more interrupts. So it's similar to
devm_functions for I/O memory. Why would you use thedrmm_interface?Quoted 7 lines
drm_irq_install/uninstall do more that just calling
request_irqandfree_irqthough, they will also run (among other things) the irq-related hooks in the drm driver (irq_preinstall,irq_postinstallirq_uninstall) and wake anything waiting for a vblank to occur, so we need the DRM device and driver to still be around when we rundrm_irq_uninstall. That's why (I assume) you have to pass thedrm_deviceas an argument and not simply the device.
Quoted 3 lines
drm_deviceis guaranteed to outlivedevm_, plus the hooks are meant to shut down hw. hw isn't around anymore when we dodrmm_cleanup, at least not in full generality.
drm_dev_put is either called through devm or in remove / unbind, and the drm_device takes a reference on its parent device, so how can the drm_device outlive its parent device?
Quoted 11 lines
Quoted 9 lines
This probably works in most case since you would allocate the
drm_devicewithdevm_drm_dev_alloc, and then rundrm_irq_install, so in the undoing phase you would have firstdrm_irq_uninstallto run, and everything is fine.However, if one doesn't use
devm_drm_dev_allocbut would usedevm_drm_irq_install, you would have first remove being called that would free the drm device, and thendrm_irq_uninstallwhich will use a free'd pointer.Don't do that, it's broken :-)
Well, yeah it's usually a pretty bad situation, but if we can fix it for free it doesn't hurt?
Maxime
Quoted 75 lines · Maxime Ripard
Hi!
Quoted 72 lines · Daniel Vetter
Quoted 70 lines · Maxime Ripard
Quoted 58 lines · Thomas Zimmermann
Hi
Am 03.11.20 um 10:52 schrieb Maxime Ripard:
Quoted 46 lines · Tian Tao
Quoted 45 lines
Add new api
devm_drm_irq_install()to register interrupts, no need to calldrm_irq_uninstall()when the drm module is removed.v2:
fixed the wrong parameter.Signed-off-by Tian Tao <[email protected]>--- drivers/gpu/drm/drm_drv.c | 23 +++++++++++++++++++++++ include/drm/drm_drv.h | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_drv.c +18
-0@@ -39,6 +39,7 @@#include <drm/drm_color_mgmt.h> #include <drm/drm_drv.h> #include <drm/drm_file.h> +#include <drm/drm_irq.h> #include <drm/drm_managed.h> #include <drm/drm_mode_object.h> #include <drm/drm_print.h> @@ -678,6 +679,28 @@ static int devm_drm_dev_init(struct device *parent,return ret; } +static void devm_drm_dev_irq_uninstall (void *data) +{ +drm_irq_uninstall (data); +} + +int devm_drm_irq_install (struct device *parent, +struct drm_device *dev,int irq) +{ +int ret; + + ret =drm_irq_install (dev, irq); +if (ret) +return ret; + + ret =devm_add_action (parent, devm_drm_dev_irq_uninstall, dev); +if (ret) +devm_drm_dev_irq_uninstall (dev);Quoted 8 lines
drivers/gpu/drm/drm_drv.c static int devm_drm_dev_init(struct device *parent, · line 703
Quoted 5 lines
drivers/gpu/drm/drm_drv.c +5
-0+ +return ret; +} +EXPORT_SYMBOL (devm_drm_irq_install); +Shouldn't we tie the IRQ to the drm device (so with
drmm_add_action) instead of tying it to the underlying device?Quoted 3 lines
If the HW device goes away, there won't be any more interrupts. So it's similar to
devm_functions for I/O memory. Why would you use thedrmm_interface?drm_irq_install/uninstall do more that just calling
request_irqandfree_irqthough, they will also run (among other things) the irq-related hooks in the drm driver (irq_preinstall,irq_postinstallirq_uninstall) and wake anything waiting for a vblank to occur, so we need the DRM device and driver to still be around when we rundrm_irq_uninstall. That's why (I assume) you have to pass thedrm_deviceas an argument and not simply the device.
Quoted 7 lines
Quoted 3 lines
drm_deviceis guaranteed to outlivedevm_, plus the hooks are meant to shut down hw. hw isn't around anymore when we dodrmm_cleanup, at least not in full generality.
drm_dev_putis either called through devm or in remove / unbind, and thedrm_devicetakes a reference on its parent device, so how can thedrm_deviceoutlive its parent device?
Oh there's more than just that going on. struct device has 2 lifetime things:
- devres resources: These are release on a) on driver unbind b) driver bind failure. Which means if you hotunplug, then devres is gone
- the kmalloced piece of memory containing the struct device, refcounted with kref. Totally independent.
So hw resources like irq should be managed with devres. Memory allocations (to prevent use-after-free) should be refcounted by a kref somewhere. In the case of struct device that's done by the driver core. In the case of struct drm_device and all the stuff hanging off that, it's done by drmm_ (ideally at least, since in practice all drivers except i915 get it wrong without drmm_).
Managing memory allocations with devres is almost always a bug.
So when you unbind/hotunplug a device, the following happens:
- driver unbind gets called and finishes
- devres cleans up hw resources
- as one of the last devres action the drm_dev_put gets called
- (if no userspace is around or anything else that holds a drm_device reference) drmm_ cleans up drm_device resources
- as the last cleanup drmm_ calls put_device()
- the actual struct device gets released
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
This probably works in most case since you would allocate the
drm_devicewithdevm_drm_dev_alloc, and then rundrm_irq_install, so in the undoing phase you would have firstdrm_irq_uninstallto run, and everything is fine.However, if one doesn't use
devm_drm_dev_allocbut would usedevm_drm_irq_install, you would have first remove being called that would free the drm device, and thendrm_irq_uninstallwhich will use a free'd pointer.
Quoted 4 lines
Quoted 1 line
Don't do that, it's broken :-)
Well, yeah it's usually a pretty bad situation, but if we can fix it for free it doesn't hurt?
See my comment somewhere, if the devm_drm_irq_install also holds a drm_dev_get reference, then no matter which wrong way you set stuff up, the right thing should happen.
-Daniel
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
Quoted 25 lines · Tian Tao
Add new api
devm_drm_irq_install()to register interrupts, no need to calldrm_irq_uninstall()when the drm module is removed.v2:
fixed the wrong parameter.Signed-off-by Tian Tao <[email protected]>--- drivers/gpu/drm/drm_drv.c | 23 +++++++++++++++++++++++ include/drm/drm_drv.h | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_drv.c +1
-0@@ -39,6 +39,7 @@#include <drm/drm_color_mgmt.h> #include <drm/drm_drv.h> #include <drm/drm_file.h> +#include <drm/drm_irq.h> #include <drm/drm_managed.h> #include <drm/drm_mode_object.h> #include <drm/drm_print.h>
drivers/gpu/drm/drm_drv.c static int devm_drm_dev_init(struct device *parent, · line 687
Quoted 9 lines
drivers/gpu/drm/drm_drv.c +6
-0@@ -678,6 +679,28 @@ static int devm_drm_dev_init(struct device *parent,return ret; } +static void devm_drm_dev_irq_uninstall (void *data) +{ +drm_irq_uninstall (data); +} + +int devm_drm_irq_install(struct device *parent,
parent argument should not be needed, we have drm_device->dev. If that doesn't work, then don't use the drm irq helpers, they're optional (and there's already devm versions of the core irq functions).
Just a detail aside from all the other things alreay mentioned. -Daniel
Quoted 36 lines
drivers/gpu/drm/drm_drv.c +16
-0+ struct drm_device *dev, int irq) +{ + int ret; + + ret =drm_irq_install (dev, irq); +if (ret) +return ret; + + ret =devm_add_action (parent, devm_drm_dev_irq_uninstall, dev); +if (ret) +devm_drm_dev_irq_uninstall (dev); + +return ret; +} +EXPORT_SYMBOL (devm_drm_irq_install); +void *__devm_drm_dev_alloc (struct device *parent,struct drm_driver *driver,size_t size,size_t offset) {include/drm/drm_drv.h +2
-1@@ -513,7 +513,8 @@ struct drm_driver {void *__devm_drm_dev_alloc (struct device *parent,struct drm_driver *driver,size_t size,size_t offset); - +int devm_drm_irq_install (struct device *parent,struct drm_device *dev, +int irq);/** * devm_drm_dev_alloc - Resource managed allocation of a &drm_device instance * @parent: Parent device object Signature · 1 line
2.7.4
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