Media device lifetime management
Subsystems (9)
Sakari Ailus
maintainerv2 cover Media device lifetime management
Hi folks,
This is a refresh of my 2016 RFC patchset to start addressing object lifetime issues in Media controller. It further allows continuing work to address lifetime management of media entities.
The underlying problem is described in detail in v4 of the previous RFC:
<URL:https://lore.kernel.org/linux-media/[email protected]/>. In brief, there is currently no connection between releasing media device (and related) memory and IOCTL calls, meaning that there is a time window during which released kernel memory can be accessed, and that access can be triggered from the user space. The only reason why this is not a grave security issue is that it is not triggerable by the user alone but requires unbinding a device. That is still not an excuse for not fixing it.
This set differs from the earlier RFC to address the issue in the following respects:
- Make changes for ipu3-cio2 driver, too.
- Continue to provide best effort attempt to keep the window between device removal and user space being able to access released memory as small as possible. This means the problem won't become worse for drivers for which Media device lifetime management has not been implemented.
The latter is achieved by adding a new object, Media devnode compat reference, which is allocated, refcounted and eventually released by the Media controller framework itself, and where the information on registration and open filehandles is maintained. This is only done if the driver does not manage the lifetime of the media device itself, i.e. its release operation is NULL.
Due to this, Media device file handles will also be introduced by this patchset. I thought the first user of this would be Media device events but it seems we already need them here.
Both ipu3-cio2 and omap3isp drivers are relieved of devm_request_irq() use, as device_release() releases the resources before calling the driver's remove function. While further work will be required also on these drivers to safely stop he hardware at unbind time, I don't see a reason not to merge these patches now.
Some patches are temporarily reverted in order to make reworks easier, then applied later on.
I've tested this on ipu3-cio2 with and without the refcounting patch (media:
ipu3-cio2: Release the cio2 device context by media device callback), including failures in a few parts of the driver initialisation process in the MC framework.
Questions and comments are welcome.
since v1:
- Align subject prefixes with current media tree practices.
- Make release changes to the vimc driver (last patch of the set). This was actually easy as vimc already centralised resource release to struct v4l2_device, so it was just moved to the media device.
- Move cdev field to struct media_devnode_compat_ref and add dev field to the struct, these are needed during device release. This now includes also the character device which is accessed by __fput(). I've now tested ipu3-cio2 and vimc with KASAN. As a by-product the kref in struct media_devnode_compat_ref becomes redundant and is removed. Both devices are registered in case of best effort memory safety support and used for refcounting.
- Drop omap3isp driver patch moving away from devm_request_irq().
- Add a patch to warn of drivers not releasing media device safely (i.e. relying on the best effort memory safety mechanism without refcounting).
- Add a patch to document how the best effort memory release safety helper works.
- Add a note on releasing driver's context with the media device, not the V4L2 device, in MC documentation.
- Check media device is registered before accessing its fops in media_read(), media_write(), media_ioctl and media_compat_ioctl().
- Document best effort media device lifetime management (new patch).
- Use media_devnode_free_minor() in unallocating device node minor number in media_devnode_register().
- Continue to rely on devm_register_irq() in ipu3-cio2 driver but register the IRQ later on (compared to v1).
- Drop the patch to move away from devm_request_irq() in omap3isp.
- Fix putting references to media device and V4L2 device in v4l2_device_release().
- Add missing media_device_get() (in v1) for M2M devices in video_register_media_controller().
- Unconditionally set the media devnode release function in media_device_init(). There's no harm doing so and the caller of media_device_init() may set the ops after calling the function.
Daniel Axtens (1):
media: uvcvideo: Refactor teardown of uvc on USB disconnect
Laurent Pinchart (1):
media: mc: Add per-file-handle data support
Logan Gunthorpe (1):
media: mc: utilize new cdev_device_add helper function
Sakari Ailus (26):
Revert "[media] media: fix media devnode ioctl/syscall and unregister
race"
Revert "media: utilize new cdev_device_add helper function" Revert "[media] media: fix use-after-free in cdev_put() when app exits
after driver unbind"
Revert "media: uvcvideo: Refactor teardown of uvc on USB disconnect" Revert "[media] media-device: dynamically allocate struct
media_devnode"
media: mc: Drop nop release callback
media: mc: Do not call cdev_device_del() if cdev_device_add() fails
media: mc: Delete character device early
media: mc: Split initialising and adding media devnode
media: mc: Shuffle functions around
media: mc: Initialise media devnode in media_device_init()
media: mc: Refactor media devnode minor clearing
media: mc: Unassign minor only if it has been assigned
media: mc: Refcount the media device
media: v4l: Acquire a reference to the media device for every video
device
media: mc: Postpone graph object removal until free
media: omap3isp: Release the isp device struct by media device
callback
media: ipu3-cio2: Call v4l2_device_unregister() earlier
media: ipu3-cio2: Request IRQ earlier
media: ipu3-cio2: Release the cio2 device context by media device
callback
media: vimc: Release resources on media device release
media: Documentation: Document how Media device resources are released
media: mc: Maintain a list of open file handles in a media device
media: mc: Implement best effort media device removal safety sans
refcount
media: mc: Warn about drivers not releasing media device safely
media: Documentation: Document media device memory safety helper
Documentation/driver-api/media/mc-core.rst | 18 +- drivers/media/cec/core/cec-core.c | 2 +- drivers/media/mc/mc-device.c | 260 ++++++++++++-------- drivers/media/mc/mc-devnode.c | 230 +++++++++++------ drivers/media/pci/intel/ipu3/ipu3-cio2.c | 70 ++++-- drivers/media/platform/ti/omap3isp/isp.c | 24 +- drivers/media/test-drivers/vimc/vimc-core.c | 15 +- drivers/media/usb/au0828/au0828-core.c | 4 +- drivers/media/usb/uvc/uvc_driver.c | 2 +- drivers/media/v4l2-core/v4l2-dev.c | 65 +++-- drivers/staging/media/sunxi/cedrus/cedrus.c | 2 +- include/media/media-device.h | 46 +++- include/media/media-devnode.h | 136 +++++++--- include/media/media-fh.h | 32 +++ 14 files changed, 632 insertions(+), 274 deletions(-) create mode 100644 include/media/media-fh.h
Signature · 1 line
2.39.2
Sakari Ailus
maintainerPATCH v2 1/29 Revert "[media] media: fix media devnode ioctl/syscall and unregister race"
This reverts commit 6f0dd24a084a ("[media] media: fix media devnode ioctl/syscall and unregister race"). The commit was part of an original patchset to avoid crashes when an unregistering device is in use.
--- drivers/media/mc/mc-device.c | 15 +++++++-------- drivers/media/mc/mc-devnode.c | 8 +------- include/media/media-devnode.h | 16 ++-------------- 3 files changed, 10 insertions(+), 29 deletions(-)
drivers/media/mc/mc-device.c +7 -8
@@ -741,7 +741,6 @@ int __must_check __media_device_register(struct media_device *mdev,if (ret <0 ) {/* devnode free is handled in media_devnode_*() */ mdev->devnode =NULL ; -media_devnode_unregister_prepare (devnode);media_devnode_unregister (devnode);return ret; }
@@ -797,9 +796,6 @@ void media_device_unregister(struct media_device *mdev)return ; } -/* Clear the devnode register bit to avoid races with media dev open */ -media_devnode_unregister_prepare (mdev->devnode); -/* Remove all entities from the media device */ list_for_each_entry_safe (entity, next, &mdev->entities, graph_obj.list)__media_device_unregister_entity (entity);
@@ -824,10 +820,13 @@ void media_device_unregister(struct media_device *mdev)dev_dbg (mdev->dev,"Media device unregistered\n" ); -device_remove_file (&mdev->devnode->dev, &dev_attr_model); -media_devnode_unregister (mdev->devnode); -/* devnode free is handled in media_devnode_*() */ - mdev->devnode =NULL ; +/* Check if mdev devnode was registered */ +if (media_devnode_is_registered (mdev->devnode)) { +device_remove_file (&mdev->devnode->dev, &dev_attr_model); +media_devnode_unregister (mdev->devnode); +/* devnode free is handled in media_devnode_*() */ + mdev->devnode =NULL ; + } }EXPORT_SYMBOL_GPL (media_device_unregister);
drivers/media/mc/mc-devnode.c +1 -7
@@ -267,7 +267,7 @@ int __must_check media_devnode_register(struct media_device *mdev,return ret; } -void media_devnode_unregister_prepare (struct media_devnode *devnode) +void media_devnode_unregister (struct media_devnode *devnode) {/* Check if devnode was ever registered at all */ if (!media_devnode_is_registered (devnode))
@@ -275,12 +275,6 @@ void media_devnode_unregister_prepare(struct media_devnode *devnode)mutex_lock (&media_devnode_lock);clear_bit (MEDIA_FLAG_REGISTERED , &devnode->flags); -mutex_unlock (&media_devnode_lock); -} - -void media_devnode_unregister (struct media_devnode *devnode) -{ -mutex_lock (&media_devnode_lock);/* Delete the cdev on this minor as well */ cdev_device_del (&devnode->cdev, &devnode->dev); devnode->media_dev =NULL ;
include/media/media-devnode.h +2 -14
@@ -115,19 +115,6 @@ int __must_check media_devnode_register(struct media_device *mdev,struct media_devnode *devnode, struct module *owner); -/** -* media_devnode_unregister_prepare - clear the media device node register bit -* @devnode: the device node to prepare for unregister -* -* This clears the passed device register bit. Future open calls will be met -* with errors. Should be called before media_devnode_unregister() to avoid -* races with unregister and device file open calls. -* -* This function can safely be called if the device node has never been -* registered or has already been unregistered. -*/ -void media_devnode_unregister_prepare (struct media_devnode *devnode); -/** * media_devnode_unregister - unregister a media device node * @devnode: the device node to unregister
@@ -135,7 +122,8 @@ void media_devnode_unregister_prepare(struct media_devnode *devnode);* This unregisters the passed device. Future open calls will be met with * errors. * -* Should be called after media_devnode_unregister_prepare() +* This function can safely be called if the device node has never been +* registered or has already been unregistered. */ void media_devnode_unregister (struct media_devnode *devnode);
Signature · 1 line
2.39.2
Sakari Ailus
maintainerPATCH v2 2/29 Revert "media: utilize new cdev_device_add helper function"
In reply to Sakari AilusThis reverts commit 857313e51006ff51524579bcd8808b70f9a80812. This patch is temporarily reverted for internal rework.
--- drivers/media/cec/core/cec-core.c | 16 ++++++++++++---- drivers/media/mc/mc-devnode.c | 21 ++++++++++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-)
drivers/media/cec/core/cec-core.c +12 -4
@@ -137,19 +137,26 @@ static int __must_check cec_devnode_register(struct cec_devnode *devnode,/* Part 2: Initialize and register the character device */ cdev_init (&devnode->cdev, &cec_devnode_fops); + devnode->cdev.kobj.parent = &devnode->dev.kobj; devnode->cdev.owner = owner;kobject_set_name (&devnode->cdev.kobj,"cec%d" , devnode->minor); devnode->registered = true; - ret =cdev_device_add (&devnode->cdev, &devnode->dev); -if (ret) { + ret =cdev_add (&devnode->cdev, devnode->dev.devt,1 ); +if (ret <0 ) { +pr_err ("%s: cdev_add failed\n" , __func__); devnode->registered = false; -pr_err ("%s: cdev_device_add failed\n" , __func__); goto clr_bit; } + ret =device_add (&devnode->dev); +if (ret) + goto cdev_del; +return 0 ; +cdev_del: +cdev_del (&devnode->cdev); clr_bit:mutex_lock (&cec_devnode_lock);clear_bit (devnode->minor, cec_devnode_nums);
@@ -195,7 +202,8 @@ static void cec_devnode_unregister(struct cec_adapter *adap)cec_adap_enable (adap);mutex_unlock (&adap->lock); -cdev_device_del (&devnode->cdev, &devnode->dev); +device_del (&devnode->dev); +cdev_del (&devnode->cdev);put_device (&devnode->dev); }
drivers/media/mc/mc-devnode.c +16 -5
@@ -240,23 +240,33 @@ int __must_check media_devnode_register(struct media_device *mdev,dev_set_name (&devnode->dev,"media%d" , devnode->minor);device_initialize (&devnode->dev); -/* Part 2: Initialize the character device */ +/* Part 2: Initialize and register the character device */ cdev_init (&devnode->cdev, &media_devnode_fops); devnode->cdev.owner = owner; + devnode->cdev.kobj.parent = &devnode->dev.kobj;kobject_set_name (&devnode->cdev.kobj,"media%d" , devnode->minor); -/* Part 3: Add the media and char device */ - ret =cdev_device_add (&devnode->cdev, &devnode->dev); + ret =cdev_add (&devnode->cdev,MKDEV (MAJOR (media_dev_t), + devnode->minor),1 );if (ret <0 ) { -pr_err ("%s: cdev_device_add failed\n" , __func__); +pr_err ("%s: cdev_add failed\n" , __func__); goto cdev_add_error; } +/* Part 3: Add the media device */ + ret =device_add (&devnode->dev); +if (ret <0 ) { +pr_err ("%s: device_add failed\n" , __func__); + goto device_add_error; + } +/* Part 4: Activate this minor. The char device can now be used. */ set_bit (MEDIA_FLAG_REGISTERED , &devnode->flags);return 0 ; +device_add_error: +cdev_del (&devnode->cdev); cdev_add_error:mutex_lock (&media_devnode_lock);clear_bit (devnode->minor, media_devnode_nums);
@@ -276,9 +286,10 @@ void media_devnode_unregister(struct media_devnode *devnode)mutex_lock (&media_devnode_lock);clear_bit (MEDIA_FLAG_REGISTERED , &devnode->flags);/* Delete the cdev on this minor as well */ -cdev_device_del (&devnode->cdev, &devnode->dev); +cdev_del (&devnode->cdev); devnode->media_dev =NULL ;mutex_unlock (&media_devnode_lock); +device_del (&devnode->dev);put_device (&devnode->dev); }
Signature · 1 line
2.39.2
Sakari Ailus
maintainerPATCH v2 3/29 Revert "[media] media: fix use-after-free in cdev_put() when app exits after driver unbind"
In reply to Sakari AilusThis reverts commit 5b28dde51d0c ("[media] media: fix use-after-free in cdev_put() when app exits after driver unbind"). The commit was part of an original patchset to avoid crashes when an unregistering device is in use.
--- drivers/media/mc/mc-device.c | 6 ++--- drivers/media/mc/mc-devnode.c | 48 ++++++++++++++--------------------- 2 files changed, 21 insertions(+), 33 deletions(-)
drivers/media/mc/mc-device.c +2 -4
@@ -732,16 +732,16 @@ int __must_check __media_device_register(struct media_device *mdev, ret =media_devnode_register (mdev, devnode, owner);if (ret <0 ) { -/* devnode free is handled in media_devnode_*() */ mdev->devnode =NULL ; +kfree (devnode);return ret; } ret =device_create_file (&devnode->dev, &dev_attr_model);if (ret <0 ) { -/* devnode free is handled in media_devnode_*() */ mdev->devnode =NULL ;media_devnode_unregister (devnode); +kfree (devnode);return ret; }
@@ -824,8 +824,6 @@ void media_device_unregister(struct media_device *mdev)if (media_devnode_is_registered (mdev->devnode)) {device_remove_file (&mdev->devnode->dev, &dev_attr_model);media_devnode_unregister (mdev->devnode); -/* devnode free is handled in media_devnode_*() */ - mdev->devnode =NULL ; } }EXPORT_SYMBOL_GPL (media_device_unregister);
drivers/media/mc/mc-devnode.c +19 -29
110 lines not on this page · raw message
Signature · 1 line
2.39.2
Sakari Ailus
maintainerPATCH v2 4/29 media: mc: utilize new cdev_device_add helper function
In reply to Sakari AilusFrom: Logan Gunthorpe <[email protected]>
Replace the open coded registration of the cdev and dev with the new device_add_cdev() helper. The helper replaces a common pattern by taking the proper reference against the parent device and adding both the cdev and the device.
--- drivers/media/cec/core/cec-core.c | 16 ++++------------ drivers/media/mc/mc-devnode.c | 23 +++++++++-------------- 2 files changed, 13 insertions(+), 26 deletions(-)
drivers/media/cec/core/cec-core.c +4 -12
@@ -137,26 +137,19 @@ static int __must_check cec_devnode_register(struct cec_devnode *devnode,/* Part 2: Initialize and register the character device */ cdev_init (&devnode->cdev, &cec_devnode_fops); - devnode->cdev.kobj.parent = &devnode->dev.kobj; devnode->cdev.owner = owner;kobject_set_name (&devnode->cdev.kobj,"cec%d" , devnode->minor); devnode->registered = true; - ret =cdev_add (&devnode->cdev, devnode->dev.devt,1 ); -if (ret <0 ) { -pr_err ("%s: cdev_add failed\n" , __func__); + ret =cdev_device_add (&devnode->cdev, &devnode->dev); +if (ret) { +pr_err ("%s: cdev_device_add failed\n" , __func__); devnode->registered = false; goto clr_bit; } - ret =device_add (&devnode->dev); -if (ret) - goto cdev_del; -return 0 ; -cdev_del: -cdev_del (&devnode->cdev); clr_bit:mutex_lock (&cec_devnode_lock);clear_bit (devnode->minor, cec_devnode_nums);
@@ -202,8 +195,7 @@ static void cec_devnode_unregister(struct cec_adapter *adap)cec_adap_enable (adap);mutex_unlock (&adap->lock); -device_del (&devnode->dev); -cdev_del (&devnode->cdev); +cdev_device_del (&devnode->cdev, &devnode->dev);put_device (&devnode->dev); }
drivers/media/mc/mc-devnode.c +9 -14
@@ -232,29 +232,24 @@ int __must_check media_devnode_register(struct media_device *mdev, devnode->minor = minor; devnode->media_dev = mdev; -/* Part 2: Initialize and register the character device */ +/* Part 2: Initialize the media and character devices */ cdev_init (&devnode->cdev, &media_devnode_fops); devnode->cdev.owner = owner;kobject_set_name (&devnode->cdev.kobj,"media%d" , devnode->minor); - ret =cdev_add (&devnode->cdev,MKDEV (MAJOR (media_dev_t), - devnode->minor),1 ); -if (ret <0 ) { -pr_err ("%s: cdev_add failed\n" , __func__); - goto error; - } - -/* Part 3: Register the media device */ devnode->dev.bus = &media_bus_type; devnode->dev.devt =MKDEV (MAJOR (media_dev_t), devnode->minor); devnode->dev.release = media_devnode_release;if (devnode->parent) devnode->dev.parent = devnode->parent;dev_set_name (&devnode->dev,"media%d" , devnode->minor); - ret =device_register (&devnode->dev); +device_initialize (&devnode->dev); + +/* Part 3: Add the media and character devices */ + ret =cdev_device_add (&devnode->cdev, &devnode->dev);if (ret <0 ) { -pr_err ("%s: device_register failed\n" , __func__); - goto error; +pr_err ("%s: cdev_device_add failed\n" , __func__); + goto cdev_add_error; }/* Part 4: Activate this minor. The char device can now be used. */
@@ -262,9 +257,9 @@ int __must_check media_devnode_register(struct media_device *mdev,return 0 ; -error: +cdev_add_error:mutex_lock (&media_devnode_lock); -cdev_del (&devnode->cdev); +cdev_device_del (&devnode->cdev, &devnode->dev);clear_bit (devnode->minor, media_devnode_nums);mutex_unlock (&media_devnode_lock);
Signature · 1 line
2.39.2
Laurent Pinchart
maintainerRe: [PATCH v2 04/29] media: mc: utilize new cdev_device_add helper function
Hi Sakari,
Thank you for the patch.
Quoted 10 lines · Sakari Ailus
From: Logan Gunthorpe <[email protected]>
Replace the open coded registration of the cdev and dev with the new
device_add_cdev()helper. The helper replaces a common pattern by taking the proper reference against the parent device and adding both the cdev and the device.Signed-off-by Logan Gunthorpe <[email protected]>Acked-by Hans Verkuil <[email protected]>Signed-off-by Greg Kroah-Hartman <[email protected]>
This reapplies a commit you've reverted in 02/29 in this series. I understand this is done to be able to apply the revert in 03/29 cleanly. Given that those three patches are consecutive, wouldn't it be better to squash 02/29, 03/29 and 04/29, with the commit message of 03/29 ? Otherwise, I would at least drop the Acked-by and Reviewed-by tags in the patches you reapply, as they've been reviewed in a different context.
The same applies to patches 05/29, 06/29 and 07/29.
Quoted 102 lines
--- drivers/media/cec/core/cec-core.c | 16 ++++------------ drivers/media/mc/mc-devnode.c | 23 +++++++++-------------- 2 files changed, 13 insertions(+), 26 deletions(-)drivers/media/cec/core/cec-core.c +4
-12@@ -137,26 +137,19 @@ static int __must_check cec_devnode_register(struct cec_devnode *devnode,/* Part 2: Initialize and register the character device */ cdev_init (&devnode->cdev, &cec_devnode_fops); - devnode->cdev.kobj.parent = &devnode->dev.kobj; devnode->cdev.owner = owner;kobject_set_name (&devnode->cdev.kobj,"cec%d" , devnode->minor); devnode->registered = true; - ret =cdev_add (&devnode->cdev, devnode->dev.devt,1 ); -if (ret <0 ) { -pr_err ("%s: cdev_add failed\n" , __func__); + ret =cdev_device_add (&devnode->cdev, &devnode->dev); +if (ret) { +pr_err ("%s: cdev_device_add failed\n" , __func__); devnode->registered = false; goto clr_bit; } - ret =device_add (&devnode->dev); -if (ret) - goto cdev_del; -return 0 ; -cdev_del: -cdev_del (&devnode->cdev); clr_bit:mutex_lock (&cec_devnode_lock);clear_bit (devnode->minor, cec_devnode_nums);@@ -202,8 +195,7 @@ static void cec_devnode_unregister(struct cec_adapter *adap)cec_adap_enable (adap);mutex_unlock (&adap->lock); -device_del (&devnode->dev); -cdev_del (&devnode->cdev); +cdev_device_del (&devnode->cdev, &devnode->dev);put_device (&devnode->dev); }drivers/media/mc/mc-devnode.c +9
-14@@ -232,29 +232,24 @@ int __must_check media_devnode_register(struct media_device *mdev, devnode->minor = minor; devnode->media_dev = mdev; -/* Part 2: Initialize and register the character device */ +/* Part 2: Initialize the media and character devices */ cdev_init (&devnode->cdev, &media_devnode_fops); devnode->cdev.owner = owner;kobject_set_name (&devnode->cdev.kobj,"media%d" , devnode->minor); - ret =cdev_add (&devnode->cdev,MKDEV (MAJOR (media_dev_t), - devnode->minor),1 ); -if (ret <0 ) { -pr_err ("%s: cdev_add failed\n" , __func__); - goto error; - } - -/* Part 3: Register the media device */ devnode->dev.bus = &media_bus_type; devnode->dev.devt =MKDEV (MAJOR (media_dev_t), devnode->minor); devnode->dev.release = media_devnode_release;if (devnode->parent) devnode->dev.parent = devnode->parent;dev_set_name (&devnode->dev,"media%d" , devnode->minor); - ret =device_register (&devnode->dev); +device_initialize (&devnode->dev); + +/* Part 3: Add the media and character devices */ + ret =cdev_device_add (&devnode->cdev, &devnode->dev);if (ret <0 ) { -pr_err ("%s: device_register failed\n" , __func__); - goto error; +pr_err ("%s: cdev_device_add failed\n" , __func__); + goto cdev_add_error; }/* Part 4: Activate this minor. The char device can now be used. */ @@ -262,9 +257,9 @@ int __must_check media_devnode_register(struct media_device *mdev,return 0 ; -error: +cdev_add_error:mutex_lock (&media_devnode_lock); -cdev_del (&devnode->cdev); +cdev_device_del (&devnode->cdev, &devnode->dev);clear_bit (devnode->minor, media_devnode_nums);mutex_unlock (&media_devnode_lock);
Signature · 3 lines
Regards, Laurent Pinchart
Laurent Pinchart
maintainerRe: [PATCH v2 04/29] media: mc: utilize new cdev_device_add helper function
Quoted 24 lines · Laurent Pinchart
Hi Sakari,
Thank you for the patch.
Quoted 10 lines · Sakari Ailus
From: Logan Gunthorpe <[email protected]>
Replace the open coded registration of the cdev and dev with the new
device_add_cdev()helper. The helper replaces a common pattern by taking the proper reference against the parent device and adding both the cdev and the device.Signed-off-by Logan Gunthorpe <[email protected]>Acked-by Hans Verkuil <[email protected]>Signed-off-by Greg Kroah-Hartman <[email protected]>This reapplies a commit you've reverted in 02/29 in this series. I understand this is done to be able to apply the revert in 03/29 cleanly. Given that those three patches are consecutive, wouldn't it be better to squash 02/29, 03/29 and 04/29, with the commit message of 03/29 ? Otherwise, I would at least drop the Acked-by and Reviewed-by tags in the patches you reapply, as they've been reviewed in a different context.
Quoted 1 line
The same applies to patches 05/29, 06/29 and 07/29.
And especially to those patches actually. 06/29 has a single line change for the uvcvideo driver, the revert in 05/29 and re-revert in 07/29 seem overkill.
It would also be nice to expand the commit messages of 03/29 and 06/29 to explain why the revert are needed.
Quoted 102 lines
Quoted 102 lines
--- drivers/media/cec/core/cec-core.c | 16 ++++------------ drivers/media/mc/mc-devnode.c | 23 +++++++++-------------- 2 files changed, 13 insertions(+), 26 deletions(-)drivers/media/cec/core/cec-core.c +4
-12@@ -137,26 +137,19 @@ static int __must_check cec_devnode_register(struct cec_devnode *devnode,/* Part 2: Initialize and register the character device */ cdev_init (&devnode->cdev, &cec_devnode_fops); - devnode->cdev.kobj.parent = &devnode->dev.kobj; devnode->cdev.owner = owner;kobject_set_name (&devnode->cdev.kobj,"cec%d" , devnode->minor); devnode->registered = true; - ret =cdev_add (&devnode->cdev, devnode->dev.devt,1 ); -if (ret <0 ) { -pr_err ("%s: cdev_add failed\n" , __func__); + ret =cdev_device_add (&devnode->cdev, &devnode->dev); +if (ret) { +pr_err ("%s: cdev_device_add failed\n" , __func__); devnode->registered = false; goto clr_bit; } - ret =device_add (&devnode->dev); -if (ret) - goto cdev_del; -return 0 ; -cdev_del: -cdev_del (&devnode->cdev); clr_bit:mutex_lock (&cec_devnode_lock);clear_bit (devnode->minor, cec_devnode_nums);@@ -202,8 +195,7 @@ static void cec_devnode_unregister(struct cec_adapter *adap)cec_adap_enable (adap);mutex_unlock (&adap->lock); -device_del (&devnode->dev); -cdev_del (&devnode->cdev); +cdev_device_del (&devnode->cdev, &devnode->dev);put_device (&devnode->dev); }drivers/media/mc/mc-devnode.c +9
-14@@ -232,29 +232,24 @@ int __must_check media_devnode_register(struct media_device *mdev, devnode->minor = minor; devnode->media_dev = mdev; -/* Part 2: Initialize and register the character device */ +/* Part 2: Initialize the media and character devices */ cdev_init (&devnode->cdev, &media_devnode_fops); devnode->cdev.owner = owner;kobject_set_name (&devnode->cdev.kobj,"media%d" , devnode->minor); - ret =cdev_add (&devnode->cdev,MKDEV (MAJOR (media_dev_t), - devnode->minor),1 ); -if (ret <0 ) { -pr_err ("%s: cdev_add failed\n" , __func__); - goto error; - } - -/* Part 3: Register the media device */ devnode->dev.bus = &media_bus_type; devnode->dev.devt =MKDEV (MAJOR (media_dev_t), devnode->minor); devnode->dev.release = media_devnode_release;if (devnode->parent) devnode->dev.parent = devnode->parent;dev_set_name (&devnode->dev,"media%d" , devnode->minor); - ret =device_register (&devnode->dev); +device_initialize (&devnode->dev); + +/* Part 3: Add the media and character devices */ + ret =cdev_device_add (&devnode->cdev, &devnode->dev);if (ret <0 ) { -pr_err ("%s: device_register failed\n" , __func__); - goto error; +pr_err ("%s: cdev_device_add failed\n" , __func__); + goto cdev_add_error; }/* Part 4: Activate this minor. The char device can now be used. */ @@ -262,9 +257,9 @@ int __must_check media_devnode_register(struct media_device *mdev,return 0 ; -error: +cdev_add_error:mutex_lock (&media_devnode_lock); -cdev_del (&devnode->cdev); +cdev_device_del (&devnode->cdev, &devnode->dev);clear_bit (devnode->minor, media_devnode_nums);mutex_unlock (&media_devnode_lock);
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerRe: [PATCH v2 04/29] media: mc: utilize new cdev_device_add helper function
Hi Laurent,
Quoted 25 lines · Laurent Pinchart
Quoted 24 lines · Laurent Pinchart
Hi Sakari,
Thank you for the patch.
Quoted 10 lines · Sakari Ailus
From: Logan Gunthorpe <[email protected]>
Replace the open coded registration of the cdev and dev with the new
device_add_cdev()helper. The helper replaces a common pattern by taking the proper reference against the parent device and adding both the cdev and the device.Signed-off-by Logan Gunthorpe <[email protected]>Acked-by Hans Verkuil <[email protected]>Signed-off-by Greg Kroah-Hartman <[email protected]>This reapplies a commit you've reverted in 02/29 in this series. I understand this is done to be able to apply the revert in 03/29 cleanly. Given that those three patches are consecutive, wouldn't it be better to squash 02/29, 03/29 and 04/29, with the commit message of 03/29 ? Otherwise, I would at least drop the Acked-by and Reviewed-by tags in the patches you reapply, as they've been reviewed in a different context.
Quoted 5 lines
Quoted 1 line
The same applies to patches 05/29, 06/29 and 07/29.
And especially to those patches actually. 06/29 has a single line change for the uvcvideo driver, the revert in 05/29 and re-revert in 07/29 seem overkill.
The revert can't be applied as-is otherwise but I'm fine merging them.
Quoted 3 lines
It would also be nice to expand the commit messages of 03/29 and 06/29 to explain why the revert are needed.
I can add that. These are basically improvements in the code but depend on commit a087ce704b80. Conceptually, it'd get quite difficult as what's really needed here is to get back to an earlier state, this is not development over the said commit.
I'll drop the acks.
Signature · 3 lines
Regards, Sakari Ailus
Sakari Ailus
maintainerPATCH v2 5/29 Revert "media: uvcvideo: Refactor teardown of uvc on USB disconnect"
In reply to Sakari AilusThis reverts commit 10e1fdb95809ed21406f53b5b4f064673a1b9ceb.
Temporarily revert this patch to revert a dependent patch. The patch is re-applied later, rebased on the revert.
--- drivers/media/usb/uvc/uvc_driver.c | 13 ++++--------- drivers/media/usb/uvc/uvc_status.c | 12 ++++-------- drivers/media/usb/uvc/uvcvideo.h | 1 - 3 files changed, 8 insertions(+), 18 deletions(-)
drivers/media/usb/uvc/uvc_driver.c +4 -9
@@ -1859,7 +1859,11 @@ static void uvc_delete(struct kref *kref)usb_put_intf (dev->intf);usb_put_dev (dev->udev); +if (dev->vdev.dev) +v4l2_device_unregister (&dev->vdev);#ifdef CONFIG_MEDIA_CONTROLLER +if (media_devnode_is_registered (dev->mdev.devnode)) +media_device_unregister (&dev->mdev);media_device_cleanup (&dev->mdev);#endif
@@ -1916,15 +1920,6 @@ static void uvc_unregister_video(struct uvc_device *dev)uvc_debugfs_cleanup_stream (stream); } - -uvc_status_unregister (dev); - -if (dev->vdev.dev) -v4l2_device_unregister (&dev->vdev); -#ifdef CONFIG_MEDIA_CONTROLLER -if (media_devnode_is_registered (dev->mdev.devnode)) -media_device_unregister (&dev->mdev); -#endif } int uvc_register_video_device(struct uvc_device *dev,
drivers/media/usb/uvc/uvc_status.c +4 -8
@@ -73,7 +73,7 @@ static int uvc_input_init(struct uvc_device *dev)return ret; } -static void uvc_input_unregister (struct uvc_device *dev) +static void uvc_input_cleanup (struct uvc_device *dev) {if (dev->input)input_unregister_device (dev->input);
@@ -90,7 +90,7 @@ static void uvc_input_report_key(struct uvc_device *dev, unsigned int code,#else #define uvc_input_init (dev) -#define uvc_input_unregister (dev) +#define uvc_input_cleanup (dev)#define uvc_input_report_key (dev, code, value)#endif /* CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV */
@@ -290,16 +290,12 @@ int uvc_status_init(struct uvc_device *dev)return 0 ; } -void uvc_status_unregister (struct uvc_device *dev) -{ -usb_kill_urb (dev->int_urb); -uvc_input_unregister (dev); -} -void uvc_status_cleanup (struct uvc_device *dev) { +usb_kill_urb (dev->int_urb);usb_free_urb (dev->int_urb);kfree (dev->status); +uvc_input_cleanup (dev); }int uvc_status_start (struct uvc_device *dev,gfp_t flags)
drivers/media/usb/uvc/uvcvideo.h +0 -1
@@ -742,7 +742,6 @@ int uvc_register_video_device(struct uvc_device *dev,/* Status */ int uvc_status_init (struct uvc_device *dev); -void uvc_status_unregister (struct uvc_device *dev);void uvc_status_cleanup (struct uvc_device *dev);int uvc_status_start (struct uvc_device *dev,gfp_t flags);void uvc_status_stop (struct uvc_device *dev);
Signature · 1 line
2.39.2
Sakari Ailus
maintainerPATCH v2 6/29 Revert "[media] media-device: dynamically allocate struct media_devnode"
In reply to Sakari AilusThis reverts commit a087ce704b80 ("[media] media-device: dynamically allocate struct media_devnode"). The commit was part of an original patchset to avoid crashes when an unregistering device is in use.
--- drivers/media/mc/mc-device.c | 44 +++++++-------------- drivers/media/mc/mc-devnode.c | 7 +--- drivers/media/usb/au0828/au0828-core.c | 4 +- drivers/media/usb/uvc/uvc_driver.c | 2 +- drivers/staging/media/sunxi/cedrus/cedrus.c | 2 +- include/media/media-device.h | 5 ++- include/media/media-devnode.h | 15 +------ 7 files changed, 25 insertions(+), 54 deletions(-)
drivers/media/mc/mc-device.c +14 -30
@@ -435,7 +435,7 @@ static long media_device_ioctl(struct file *filp, unsigned int cmd, unsigned long __arg) {struct media_devnode *devnode =media_devnode_data (filp); -struct media_device *dev = devnode->media_dev; +struct media_device *dev =to_media_device (devnode);const struct media_ioctl_info *info;void __user *arg = (void __user *)__arg;char __karg[256 ], *karg = __karg;
@@ -519,7 +519,7 @@ static long media_device_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) {struct media_devnode *devnode =media_devnode_data (filp); -struct media_device *dev = devnode->media_dev; +struct media_device *dev =to_media_device (devnode);long ret;switch (cmd) {
@@ -555,8 +555,7 @@ static const struct media_file_operations media_device_fops = {static ssize_t model_show (struct device *cd,struct device_attribute *attr,char *buf) { -struct media_devnode *devnode =to_media_devnode (cd); -struct media_device *mdev = devnode->media_dev; +struct media_device *mdev =to_media_device (to_media_devnode (cd));return sprintf (buf,"%.*s\n" , (int )sizeof (mdev->model), mdev->model); }
@@ -714,34 +713,23 @@ EXPORT_SYMBOL_GPL(media_device_cleanup);int __must_check __media_device_register (struct media_device *mdev,struct module *owner) { -struct media_devnode *devnode;int ret; - devnode =kzalloc (sizeof (*devnode),GFP_KERNEL ); -if (!devnode) -return -ENOMEM ; -/* Register the device node. */ - mdev->devnode = devnode; - devnode->fops = &media_device_fops; - devnode->parent = mdev->dev; - devnode->release = media_device_release; + mdev->devnode.fops = &media_device_fops; + mdev->devnode.parent = mdev->dev; + mdev->devnode.release = media_device_release;/* Set version 0 to indicate user-space that the graph is static */ mdev->topology_version =0 ; - ret =media_devnode_register (mdev, devnode, owner); -if (ret <0 ) { - mdev->devnode =NULL ; -kfree (devnode); + ret =media_devnode_register (&mdev->devnode, owner); +if (ret <0 )return ret; - } - ret =device_create_file (&devnode->dev, &dev_attr_model); + ret =device_create_file (&mdev->devnode.dev, &dev_attr_model);if (ret <0 ) { - mdev->devnode =NULL ; -media_devnode_unregister (devnode); -kfree (devnode); +media_devnode_unregister (&mdev->devnode);return ret; }
@@ -791,7 +779,7 @@ void media_device_unregister(struct media_device *mdev)mutex_lock (&mdev->graph_mutex);/* Check if mdev was ever registered at all */ -if (!media_devnode_is_registered (mdev->devnode)) { +if (!media_devnode_is_registered (&mdev->devnode)) {mutex_unlock (&mdev->graph_mutex);return ; }
@@ -818,13 +806,9 @@ void media_device_unregister(struct media_device *mdev)mutex_unlock (&mdev->graph_mutex); -dev_dbg (mdev->dev,"Media device unregistered\n" ); - -/* Check if mdev devnode was registered */ -if (media_devnode_is_registered (mdev->devnode)) { -device_remove_file (&mdev->devnode->dev, &dev_attr_model); -media_devnode_unregister (mdev->devnode); - } +device_remove_file (&mdev->devnode.dev, &dev_attr_model); +dev_dbg (mdev->dev,"Media device unregistering\n" ); +media_devnode_unregister (&mdev->devnode); }EXPORT_SYMBOL_GPL (media_device_unregister);
drivers/media/mc/mc-devnode.c +1 -6
@@ -32,7 +32,6 @@#include <linux/uaccess.h> #include <media/media-devnode.h> -#include <media/media-device.h> #define MEDIA_NUM_DEVICES 256#define MEDIA_NAME "media"
@@ -63,8 +62,6 @@ static void media_devnode_release(struct device *cd)/* Release media_devnode and perform other cleanups as needed. */ if (devnode->release) devnode->release (devnode); - -kfree (devnode); }static struct bus_type media_bus_type = {
@@ -210,8 +207,7 @@ static const struct file_operations media_devnode_fops = { .llseek = no_llseek, }; -int __must_check media_devnode_register (struct media_device *mdev, -struct media_devnode *devnode, +int __must_check media_devnode_register (struct media_devnode *devnode,struct module *owner) {int minor;
@@ -230,7 +226,6 @@ int __must_check media_devnode_register(struct media_device *mdev,mutex_unlock (&media_devnode_lock); devnode->minor = minor; - devnode->media_dev = mdev;/* Part 2: Initialize the media and character devices */ cdev_init (&devnode->cdev, &media_devnode_fops);
drivers/media/usb/au0828/au0828-core.c +2 -2
@@ -128,7 +128,7 @@ static void au0828_unregister_media_device(struct au0828_dev *dev)struct media_device *mdev = dev->media_dev;struct media_entity_notify *notify, *nextp; -if (!mdev || !media_devnode_is_registered (mdev->devnode)) +if (!mdev || !media_devnode_is_registered (&mdev->devnode))return ;/* Remove au0828 entity_notify callbacks */
@@ -566,7 +566,7 @@ static int au0828_media_device_register(struct au0828_dev *dev,if (!dev->media_dev)return 0 ; -if (!media_devnode_is_registered (dev->media_dev->devnode)) { +if (!media_devnode_is_registered (&dev->media_dev->devnode)) {/* register media device */ ret =media_device_register (dev->media_dev);
drivers/media/usb/uvc/uvc_driver.c +1 -1
@@ -1862,7 +1862,7 @@ static void uvc_delete(struct kref *kref)if (dev->vdev.dev)v4l2_device_unregister (&dev->vdev);#ifdef CONFIG_MEDIA_CONTROLLER -if (media_devnode_is_registered (dev->mdev.devnode)) +if (media_devnode_is_registered (&dev->mdev.devnode))media_device_unregister (&dev->mdev);media_device_cleanup (&dev->mdev);#endif
drivers/staging/media/sunxi/cedrus/cedrus.c +1 -1
@@ -548,7 +548,7 @@ static void cedrus_remove(struct platform_device *pdev)struct cedrus_dev *dev =platform_get_drvdata (pdev);cancel_delayed_work_sync (&dev->watchdog_work); -if (media_devnode_is_registered (dev->mdev.devnode)) { +if (media_devnode_is_registered (&dev->mdev.devnode)) {media_device_unregister (&dev->mdev);v4l2_m2m_unregister_media_controller (dev->m2m_dev);media_device_cleanup (&dev->mdev);
include/media/media-device.h +4 -1
@@ -145,7 +145,7 @@ struct media_device_ops {struct media_device {/* dev->driver_data points to this struct. */ struct device *dev; -struct media_devnode *devnode; +struct media_devnode devnode;char model[32 ];char driver_name[32 ];
@@ -191,6 +191,9 @@ struct usb_device;#define MEDIA_DEV_NOTIFY_PRE_LINK_CH 0#define MEDIA_DEV_NOTIFY_POST_LINK_CH 1 +/* media_devnode to media_device */ +#define to_media_device (node) container_of(node, struct media_device, devnode) +/** * media_device_init() - Initializes a media device element *
include/media/media-devnode.h +2 -13
@@ -21,8 +21,6 @@#include <linux/device.h> #include <linux/cdev.h> -struct media_device ; -/* * Flag to mark the media_devnode struct as registered. Drivers must not touch * this flag directly, it will be set and cleared by media_devnode_register and
@@ -73,8 +71,6 @@ struct media_file_operations {* before registering the node. */ struct media_devnode { -struct media_device *media_dev; -/* device ops */ const struct media_file_operations *fops;
@@ -97,8 +93,7 @@ struct media_devnode {/** * media_devnode_register - register a media device node * -* @mdev: struct media_device we want to register a device node -* @devnode: media device node structure we want to register +* @devnode: struct media_devnode we want to register a device node * @owner: should be filled with %THIS_MODULE * * The registration code assigns minor numbers and registers the new device node
@@ -111,8 +106,7 @@ struct media_devnode {* the media_devnode structure is *not* called, so the caller is responsible for * freeing any data. */ -int __must_check media_devnode_register (struct media_device *mdev, -struct media_devnode *devnode, +int __must_check media_devnode_register (struct media_devnode *devnode,struct module *owner);/**
@@ -142,14 +136,9 @@ static inline struct media_devnode *media_devnode_data(struct file *filp)* false otherwise. * * @devnode: pointer to struct &media_devnode. -* -* Note: If mdev is NULL, it also returns false. */ static inline int media_devnode_is_registered (struct media_devnode *devnode) { -if (!devnode) -return false; -return test_bit (MEDIA_FLAG_REGISTERED , &devnode->flags); }
Signature · 1 line
2.39.2
Sakari Ailus
maintainerPATCH v2 7/29 media: uvcvideo: Refactor teardown of uvc on USB disconnect
In reply to Sakari AilusFrom: Daniel Axtens <[email protected]>
Currently, disconnecting a USB webcam while it is in use prints out a number of warnings, such as:
WARNING: CPU: 2 PID: 3118 at /build/linux-ezBi1T/linux-4.8.0/fs/sysfs/group.c:237 sysfs_remove_group+0x8b/0x90
sysfs group ffffffffa7cd0780 not found for kobject 'event13'
This has been noticed before. [0]
This is because of the order in which things are torn down.
If there are no streams active during a USB disconnect:
- uvc_disconnect() is invoked via device_del() through the bus notifier mechanism.
- this calls uvc_unregister_video().
- uvc_unregister_video() unregisters the video device for each stream,
- because there are no streams open, it calls uvc_delete()
- uvc_delete() calls uvc_status_cleanup(), which cleans up the status input device.
- uvc_delete() calls media_device_unregister(), which cleans up the media device
- uvc_delete(), uvc_unregister_video() and uvc_disconnect() all return, and we end up back in device_del().
- device_del() then cleans up the sysfs folder for the camera with dpm_sysfs_remove(). Because uvc_status_cleanup() and media_device_unregister() have already been called, this all works nicely.
If, on the other hand, there *are* streams active during a USB disconnect:
- uvc_disconnect() is invoked
- this calls uvc_unregister_video()
- uvc_unregister_video() unregisters the video device for each stream,
- uvc_unregister_video() and uvc_disconnect() return, and we end up back in device_del().
- device_del() then cleans up the sysfs folder for the camera with dpm_sysfs_remove(). Because the status input device and the media device are children of the USB device, this also deletes their sysfs folders.
- Sometime later, the final stream is closed, invoking uvc_release().
- uvc_release() calls uvc_delete()
- uvc_delete() calls uvc_status_cleanup(), which cleans up the status input device. Because the sysfs directory has already been removed, this causes a WARNing.
- uvc_delete() calls media_device_unregister(), which cleans up the media device. Because the sysfs directory has already been removed, this causes another WARNing.
To fix this, we need to make sure the devices are always unregistered before the end of uvc_disconnect(). To this, move the unregistration into the disconnect path:
- split uvc_status_cleanup() into two parts, one on disconnect that unregisters and one on delete that frees.
- move v4l2_device_unregister() and media_device_unregister() into the disconnect path.
[0]: https://lkml.org/lkml/2016/12/8/657
[Renamed uvc_input_cleanup() to uvc_input_unregister()]
--- drivers/media/usb/uvc/uvc_driver.c | 13 +++++++++---- drivers/media/usb/uvc/uvc_status.c | 12 ++++++++---- drivers/media/usb/uvc/uvcvideo.h | 1 + 3 files changed, 18 insertions(+), 8 deletions(-)
drivers/media/usb/uvc/uvc_driver.c +9 -4
@@ -1859,11 +1859,7 @@ static void uvc_delete(struct kref *kref)usb_put_intf (dev->intf);usb_put_dev (dev->udev); -if (dev->vdev.dev) -v4l2_device_unregister (&dev->vdev);#ifdef CONFIG_MEDIA_CONTROLLER -if (media_devnode_is_registered (&dev->mdev.devnode)) -media_device_unregister (&dev->mdev);media_device_cleanup (&dev->mdev);#endif
@@ -1920,6 +1916,15 @@ static void uvc_unregister_video(struct uvc_device *dev)uvc_debugfs_cleanup_stream (stream); } + +uvc_status_unregister (dev); + +if (dev->vdev.dev) +v4l2_device_unregister (&dev->vdev); +#ifdef CONFIG_MEDIA_CONTROLLER +if (media_devnode_is_registered (&dev->mdev.devnode)) +media_device_unregister (&dev->mdev); +#endif } int uvc_register_video_device(struct uvc_device *dev,
drivers/media/usb/uvc/uvc_status.c +8 -4
@@ -73,7 +73,7 @@ static int uvc_input_init(struct uvc_device *dev)return ret; } -static void uvc_input_cleanup (struct uvc_device *dev) +static void uvc_input_unregister (struct uvc_device *dev) {if (dev->input)input_unregister_device (dev->input);
@@ -90,7 +90,7 @@ static void uvc_input_report_key(struct uvc_device *dev, unsigned int code,#else #define uvc_input_init (dev) -#define uvc_input_cleanup (dev) +#define uvc_input_unregister (dev)#define uvc_input_report_key (dev, code, value)#endif /* CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV */
@@ -290,12 +290,16 @@ int uvc_status_init(struct uvc_device *dev)return 0 ; } -void uvc_status_cleanup (struct uvc_device *dev) +void uvc_status_unregister (struct uvc_device *dev) {usb_kill_urb (dev->int_urb); +uvc_input_unregister (dev); +} + +void uvc_status_cleanup (struct uvc_device *dev) +{usb_free_urb (dev->int_urb);kfree (dev->status); -uvc_input_cleanup (dev); }int uvc_status_start (struct uvc_device *dev,gfp_t flags)
drivers/media/usb/uvc/uvcvideo.h +1 -0
@@ -742,6 +742,7 @@ int uvc_register_video_device(struct uvc_device *dev,/* Status */ int uvc_status_init (struct uvc_device *dev); +void uvc_status_unregister (struct uvc_device *dev);void uvc_status_cleanup (struct uvc_device *dev);int uvc_status_start (struct uvc_device *dev,gfp_t flags);void uvc_status_stop (struct uvc_device *dev);
Signature · 1 line
2.39.2
Sakari Ailus
maintainerPATCH v2 8/29 media: mc: Drop nop release callback
In reply to Sakari AilusThe release callback is only used to print a debug message. Drop it. (It will be re-introduced later in a different form.)
--- drivers/media/mc/mc-device.c | 6 ------ 1 file changed, 6 deletions(-)
drivers/media/mc/mc-device.c +0 -6
@@ -566,11 +566,6 @@ static DEVICE_ATTR_RO(model);* Registration/unregistration */ -static void media_device_release (struct media_devnode *devnode) -{ -dev_dbg (devnode->parent,"Media device released\n" ); -} -static void __media_device_unregister_entity (struct media_entity *entity) {struct media_device *mdev = entity->graph_obj.mdev;
@@ -718,7 +713,6 @@ int __must_check __media_device_register(struct media_device *mdev,/* Register the device node. */ mdev->devnode.fops = &media_device_fops; mdev->devnode.parent = mdev->dev; - mdev->devnode.release = media_device_release;/* Set version 0 to indicate user-space that the graph is static */ mdev->topology_version =0 ;
Signature · 1 line
2.39.2
Re: [PATCH v2 08/29] media: mc: Drop nop release callback
Hi Sakari,
Thank you for the patch.
Quoted 5 lines · Sakari Ailus
The release callback is only used to print a debug message. Drop it. (It will be re-introduced later in a different form.)
Signed-off-by Sakari Ailus <[email protected]>Acked-by Hans Verkuil <[email protected]>
Quoted 28 lines
--- drivers/media/mc/mc-device.c | 6 ------ 1 file changed, 6 deletions(-)drivers/media/mc/mc-device.c +0
-6@@ -566,11 +566,6 @@ static DEVICE_ATTR_RO(model);* Registration/unregistration */ -static void media_device_release (struct media_devnode *devnode) -{ -dev_dbg (devnode->parent,"Media device released\n" ); -} -static void __media_device_unregister_entity (struct media_entity *entity) {struct media_device *mdev = entity->graph_obj.mdev;@@ -718,7 +713,6 @@ int __must_check __media_device_register(struct media_device *mdev,/* Register the device node. */ mdev->devnode.fops = &media_device_fops; mdev->devnode.parent = mdev->dev; - mdev->devnode.release = media_device_release;/* Set version 0 to indicate user-space that the graph is static */ mdev->topology_version =0 ;
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerPATCH v2 9/29 media: mc: Do not call cdev_device_del() if cdev_device_add() fails
In reply to Sakari Ailuscdev_device_del() is the right function to remove a device when cdev_device_add() succeeds. If it does not, however, put_device() needs to be used instead. Fix this.
--- drivers/media/mc/mc-devnode.c | 1 - 1 file changed, 1 deletion(-)
drivers/media/mc/mc-devnode.c +0 -1
@@ -254,7 +254,6 @@ int __must_check media_devnode_register(struct media_devnode *devnode, cdev_add_error:mutex_lock (&media_devnode_lock); -cdev_device_del (&devnode->cdev, &devnode->dev);clear_bit (devnode->minor, media_devnode_nums);mutex_unlock (&media_devnode_lock);
Signature · 1 line
2.39.2
Laurent Pinchart
maintainerRe: [PATCH v2 09/29] media: mc: Do not call cdev_device_del() if cdev_device_add() fails
Hi Sakari,
Thank you for the patch.
Quoted 3 lines · Sakari Ailus
cdev_device_del()is the right function to remove a device whencdev_device_add()succeeds. If it does not, however,put_device()needs to be used instead. Fix this.
Where's the put_device() call ?
Quoted 18 lines
Signed-off-by Sakari Ailus <[email protected]>Acked-by Hans Verkuil <[email protected]>--- drivers/media/mc/mc-devnode.c | 1 - 1 file changed, 1 deletion(-)drivers/media/mc/mc-devnode.c +0
-1@@ -254,7 +254,6 @@ int __must_check media_devnode_register(struct media_devnode *devnode, cdev_add_error:mutex_lock (&media_devnode_lock); -cdev_device_del (&devnode->cdev, &devnode->dev);clear_bit (devnode->minor, media_devnode_nums);mutex_unlock (&media_devnode_lock);
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerRe: [PATCH v2 09/29] media: mc: Do not call cdev_device_del() if cdev_device_add() fails
Hi Laurent,
Quoted 10 lines · Laurent Pinchart
Hi Sakari,
Thank you for the patch.
Quoted 3 lines · Sakari Ailus
cdev_device_del()is the right function to remove a device whencdev_device_add()succeeds. If it does not, however,put_device()needs to be used instead. Fix this.Where's the
put_device()call ?
Seems to have gone missing at some point.
It was added though by "media: mc: Split initialising and adding media devnode" a little later as this got moved to mc-device.c, so fixing this here makes difference until that patch.
I'll address this for v3.
Quoted 20 lines
Quoted 19 lines
Signed-off-by Sakari Ailus <[email protected]>Acked-by Hans Verkuil <[email protected]>--- drivers/media/mc/mc-devnode.c | 1 - 1 file changed, 1 deletion(-)drivers/media/mc/mc-devnode.c +0
-1@@ -254,7 +254,6 @@ int __must_check media_devnode_register(struct media_devnode *devnode, cdev_add_error:mutex_lock (&media_devnode_lock); -cdev_device_del (&devnode->cdev, &devnode->dev);clear_bit (devnode->minor, media_devnode_nums);mutex_unlock (&media_devnode_lock);
Signature · 3 lines
Regards, Sakari Ailus
Sakari Ailus
maintainerPATCH v2 10/29 media: mc: Delete character device early
In reply to Sakari AilusThe parent of the character device related to the media devnode is the media devnode. Thus the character device needs to be released before the media devnode's release function. Move it to unregistering of the media devnode, which mirrors adding the character device in conjunction with registering the media devnode.
--- drivers/media/mc/mc-devnode.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/media/mc/mc-devnode.c +1 -3
@@ -51,9 +51,6 @@ static void media_devnode_release(struct device *cd)mutex_lock (&media_devnode_lock); -/* Delete the cdev on this minor as well */ -cdev_del (&devnode->cdev); -/* Mark device node number as free */ clear_bit (devnode->minor, media_devnode_nums);
@@ -270,6 +267,7 @@ void media_devnode_unregister(struct media_devnode *devnode)clear_bit (MEDIA_FLAG_REGISTERED , &devnode->flags);mutex_unlock (&media_devnode_lock); +cdev_del (&devnode->cdev);device_unregister (&devnode->dev); }
Signature · 1 line
2.39.2
Re: [PATCH v2 10/29] media: mc: Delete character device early
Hi Sakari,
Thank you for the patch.
Quoted 17 lines · Sakari Ailus
The parent of the character device related to the media devnode is the media devnode. Thus the character device needs to be released before the media devnode's release function. Move it to unregistering of the media devnode, which mirrors adding the character device in conjunction with registering the media devnode.
Signed-off-by Sakari Ailus <[email protected]>Acked-by Hans Verkuil <[email protected]>--- drivers/media/mc/mc-devnode.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)drivers/media/mc/mc-devnode.c +0
-0
drivers/media/mc/mc-devnode.c static void media_devnode_release(struct device *cd) · line 55
Quoted 8 lines
drivers/media/mc/mc-devnode.c +0
-3@@ -51,9 +51,6 @@ static void media_devnode_release(struct device *cd)mutex_lock (&media_devnode_lock); -/* Delete the cdev on this minor as well */ -cdev_del (&devnode->cdev); -/* Mark device node number as free */ clear_bit (devnode->minor, media_devnode_nums);
Should this be moved to media_devnode_unregister() too ? It can be done in a separate patch.
drivers/media/mc/mc-devnode.c void media_devnode_unregister(struct media_devnode *devnode) · line 270
Quoted 6 lines
drivers/media/mc/mc-devnode.c +1
-0@@ -270,6 +267,7 @@ void media_devnode_unregister(struct media_devnode *devnode)clear_bit (MEDIA_FLAG_REGISTERED , &devnode->flags);mutex_unlock (&media_devnode_lock); +cdev_del (&devnode->cdev);
I initially wondered if this could raise with the cdev access in media_open(), as the media_devnode_lock is released just before calling cdev_dev(), but my understanding is that the dev/open race is properly handled in the cdev layer.
I wonder if a similar change in v4l2-dev.c would be beneficial.
Quoted 3 lines
drivers/media/mc/mc-devnode.c +0
-0device_unregister (&devnode->dev); }
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerRe: [PATCH v2 10/29] media: mc: Delete character device early
Hi Laurent,
Quoted 25 lines · Laurent Pinchart
Hi Sakari,
Thank you for the patch.
Quoted 20 lines · Sakari Ailus
The parent of the character device related to the media devnode is the media devnode. Thus the character device needs to be released before the media devnode's release function. Move it to unregistering of the media devnode, which mirrors adding the character device in conjunction with registering the media devnode.
Signed-off-by Sakari Ailus <[email protected]>Acked-by Hans Verkuil <[email protected]>--- drivers/media/mc/mc-devnode.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)drivers/media/mc/mc-devnode.c +0
-0@@ -51,9 +51,6 @@ static void media_devnode_release(struct device *cd)mutex_lock (&media_devnode_lock);
Quoted 8 lines
drivers/media/mc/mc-devnode.c static void media_devnode_release(struct device *cd) · line 55
Quoted 5 lines
drivers/media/mc/mc-devnode.c +0
-3-/* Delete the cdev on this minor as well */ -cdev_del (&devnode->cdev); -/* Mark device node number as free */ clear_bit (devnode->minor, media_devnode_nums);Should this be moved to
media_devnode_unregister()too ? It can be done in a separate patch.
Good question. Yes, I think that seems reasonable. The minor isn't needed after unregistering the device.
Quoted 6 lines
Quoted 6 lines
drivers/media/mc/mc-devnode.c +0
-0@@ -270,6 +267,7 @@ void media_devnode_unregister(struct media_devnode *devnode)clear_bit (MEDIA_FLAG_REGISTERED , &devnode->flags);mutex_unlock (&media_devnode_lock);
Quoted 8 lines
drivers/media/mc/mc-devnode.c void media_devnode_unregister(struct media_devnode *devnode) · line 270
Quoted 1 line
drivers/media/mc/mc-devnode.c +1
-0+cdev_del (&devnode->cdev);I initially wondered if this could raise with the cdev access in
media_open(), as themedia_devnode_lockis released just before callingcdev_dev(), but my understanding is that the dev/open race is properly handled in the cdev layer.Reviewed-by Laurent Pinchart <[email protected]>
Thanks!
Quoted 2 lines
I wonder if a similar change in
v4l2-dev.cwould be beneficial.
Quite possibly. Let's see...
Signature · 3 lines
Kind regards, Sakari Ailus
Sakari Ailus
maintainerPATCH v2 11/29 media: mc: Split initialising and adding media devnode
In reply to Sakari AilusAs registering a device node of an entity belonging to a media device will require a reference to the struct device. Taking that reference is only possible once the device has been initialised, which took place only when it was registered. Split this in two, and initialise the device when the media device is allocated.
Don't distribute the effects of these changes yet. Add media_device_get() and media_device_put() first.
--- drivers/media/mc/mc-device.c | 18 +++++++++++++----- drivers/media/mc/mc-devnode.c | 17 ++++++++++------- include/media/media-devnode.h | 19 ++++++++++++++----- 3 files changed, 37 insertions(+), 17 deletions(-)
drivers/media/mc/mc-device.c +13 -5
@@ -717,19 +717,26 @@ int __must_check __media_device_register(struct media_device *mdev,/* Set version 0 to indicate user-space that the graph is static */ mdev->topology_version =0 ; +media_devnode_init (&mdev->devnode); + ret =media_devnode_register (&mdev->devnode, owner);if (ret <0 ) -return ret; + goto out_put; ret =device_create_file (&mdev->devnode.dev, &dev_attr_model); -if (ret <0 ) { -media_devnode_unregister (&mdev->devnode); -return ret; - } +if (ret <0 ) + goto out_unregister;dev_dbg (mdev->dev,"Media device registered\n" );return 0 ; + +out_unregister: +media_devnode_unregister (&mdev->devnode); +out_put: +put_device (&mdev->devnode.dev); + +return ret; }EXPORT_SYMBOL_GPL (__media_device_register);
@@ -803,6 +810,7 @@ void media_device_unregister(struct media_device *mdev)device_remove_file (&mdev->devnode.dev, &dev_attr_model);dev_dbg (mdev->dev,"Media device unregistering\n" );media_devnode_unregister (&mdev->devnode); +put_device (&mdev->devnode.dev); }EXPORT_SYMBOL_GPL (media_device_unregister);
drivers/media/mc/mc-devnode.c +10 -7
@@ -204,6 +204,11 @@ static const struct file_operations media_devnode_fops = { .llseek = no_llseek, }; +void media_devnode_init (struct media_devnode *devnode) +{ +device_initialize (&devnode->dev); +} +int __must_check media_devnode_register (struct media_devnode *devnode,struct module *owner) {
@@ -235,7 +240,6 @@ int __must_check media_devnode_register(struct media_devnode *devnode,if (devnode->parent) devnode->dev.parent = devnode->parent;dev_set_name (&devnode->dev,"media%d" , devnode->minor); -device_initialize (&devnode->dev);/* Part 3: Add the media and character devices */ ret =cdev_device_add (&devnode->cdev, &devnode->dev);
@@ -267,14 +271,13 @@ void media_devnode_unregister(struct media_devnode *devnode)clear_bit (MEDIA_FLAG_REGISTERED , &devnode->flags);mutex_unlock (&media_devnode_lock); -cdev_del (&devnode->cdev); -device_unregister (&devnode->dev); +cdev_device_del (&devnode->cdev, &devnode->dev); }/* * Initialise media for linux */ -static int __init media_devnode_init (void ) +static int __init media_devnode_module_init (void ) {int ret;
@@ -296,14 +299,14 @@ static int __init media_devnode_init(void)return 0 ; } -static void __exit media_devnode_exit(void) +static void __exit media_devnode_module_exit(void) {bus_unregister (&media_bus_type);unregister_chrdev_region (media_dev_t,MEDIA_NUM_DEVICES ); } -subsys_initcall (media_devnode_init); -module_exit(media_devnode_exit) +subsys_initcall (media_devnode_module_init); +module_exit(media_devnode_module_exit) MODULE_AUTHOR("Laurent Pinchart <[email protected]>");MODULE_DESCRIPTION ("Device node registration for media drivers" );
include/media/media-devnode.h +14 -5
@@ -90,6 +90,17 @@ struct media_devnode {/* dev to media_devnode */ #define to_media_devnode (cd) container_of(cd, struct media_devnode, dev) +/** +* media_devnode_init - initialise a media devnode +* +* @devnode: struct media_devnode we want to initialise +* +* Initialise a media devnode. Note that after initialising the media +* devnode is refcounted. Releasing references to it may be done using +* put_device(). +*/ +void media_devnode_init (struct media_devnode *devnode); +/** * media_devnode_register - register a media device node *
@@ -100,11 +111,9 @@ struct media_devnode {* with the kernel. An error is returned if no free minor number can be found, * or if the registration of the device node fails. * -* Zero is returned on success. -* -* Note that if the media_devnode_register call fails, the release() callback of -* the media_devnode structure is *not* called, so the caller is responsible for -* freeing any data. +* Zero is returned on success. Note that in case +* media_devnode_register() fails, the caller is responsible for +* releasing the reference to the device using put_device(). */ int __must_check media_devnode_register (struct media_devnode *devnode,struct module *owner);
Signature · 1 line
2.39.2
Re: [PATCH v2 11/29] media: mc: Split initialising and adding media devnode
Hi Sakari,
Thank you for the patch.
Quoted 1 line · Sakari Ailus
As registering a device node of an entity belonging to a media device
Did you mean s/As registering/Registering/ ?
Quoted 2 lines
will require a reference to the struct device. Taking that reference is only possible once the device has been initialised, which took place only
s/took/takes/
Quoted 1 line
when it was registered. Split this in two, and initialise the device when
s/was/is/
Quoted 4 lines
the media device is allocated.
Don't distribute the effects of these changes yet. Add
media_device_get()andmedia_device_put()first.
Don't propagate the effects of these changes to drivers yet, we want to expose media_device refcounting with media_device_get() and media_device_put() functions first.
I'm not sure that's exactly what you meant though.
Quoted 2 lines
Signed-off-by Sakari Ailus <[email protected]>Acked-by Hans Verkuil <[email protected]>
Quoted 24 lines
--- drivers/media/mc/mc-device.c | 18 +++++++++++++----- drivers/media/mc/mc-devnode.c | 17 ++++++++++------- include/media/media-devnode.h | 19 ++++++++++++++----- 3 files changed, 37 insertions(+), 17 deletions(-)drivers/media/mc/mc-device.c +3
-3@@ -717,19 +717,26 @@ int __must_check __media_device_register(struct media_device *mdev,/* Set version 0 to indicate user-space that the graph is static */ mdev->topology_version =0 ; +media_devnode_init (&mdev->devnode); + ret =media_devnode_register (&mdev->devnode, owner);if (ret <0 ) -return ret; + goto out_put; ret =device_create_file (&mdev->devnode.dev, &dev_attr_model); -if (ret <0 ) { -media_devnode_unregister (&mdev->devnode);
drivers/media/mc/mc-device.c int __must_check __media_device_register(struct media_device *mdev, · line 734
Quoted 10 lines
drivers/media/mc/mc-device.c +4
-2-return ret; - } +if (ret <0 ) + goto out_unregister;dev_dbg (mdev->dev,"Media device registered\n" );return 0 ; + +out_unregister:
I would name the labels err_unregister and err_put.
Quoted 112 lines
drivers/media/mc/mc-device.c +6
-0+media_devnode_unregister (&mdev->devnode); +out_put: +put_device (&mdev->devnode.dev); + +return ret; }EXPORT_SYMBOL_GPL (__media_device_register);@@ -803,6 +810,7 @@ void media_device_unregister(struct media_device *mdev)device_remove_file (&mdev->devnode.dev, &dev_attr_model);dev_dbg (mdev->dev,"Media device unregistering\n" );media_devnode_unregister (&mdev->devnode); +put_device (&mdev->devnode.dev); }EXPORT_SYMBOL_GPL (media_device_unregister);drivers/media/mc/mc-devnode.c +10
-7@@ -204,6 +204,11 @@ static const struct file_operations media_devnode_fops = { .llseek = no_llseek, }; +void media_devnode_init (struct media_devnode *devnode) +{ +device_initialize (&devnode->dev); +} +int __must_check media_devnode_register (struct media_devnode *devnode,struct module *owner) {@@ -235,7 +240,6 @@ int __must_check media_devnode_register(struct media_devnode *devnode,if (devnode->parent) devnode->dev.parent = devnode->parent;dev_set_name (&devnode->dev,"media%d" , devnode->minor); -device_initialize (&devnode->dev);/* Part 3: Add the media and character devices */ ret =cdev_device_add (&devnode->cdev, &devnode->dev);@@ -267,14 +271,13 @@ void media_devnode_unregister(struct media_devnode *devnode)clear_bit (MEDIA_FLAG_REGISTERED , &devnode->flags);mutex_unlock (&media_devnode_lock); -cdev_del (&devnode->cdev); -device_unregister (&devnode->dev); +cdev_device_del (&devnode->cdev, &devnode->dev); }/* * Initialise media for linux */ -static int __init media_devnode_init (void ) +static int __init media_devnode_module_init (void ) {int ret;@@ -296,14 +299,14 @@ static int __init media_devnode_init(void)return 0 ; } -static void __exit media_devnode_exit(void) +static void __exit media_devnode_module_exit(void) {bus_unregister (&media_bus_type);unregister_chrdev_region (media_dev_t,MEDIA_NUM_DEVICES ); } -subsys_initcall (media_devnode_init); -module_exit(media_devnode_exit) +subsys_initcall (media_devnode_module_init); +module_exit(media_devnode_module_exit) MODULE_AUTHOR("Laurent Pinchart <[email protected]>");MODULE_DESCRIPTION ("Device node registration for media drivers" );include/media/media-devnode.h +14
-5@@ -90,6 +90,17 @@ struct media_devnode {/* dev to media_devnode */ #define to_media_devnode (cd) container_of(cd, struct media_devnode, dev) +/** +* media_devnode_init - initialise a media devnode +* +* @devnode: struct media_devnode we want to initialise +* +* Initialise a media devnode. Note that after initialising the media +* devnode is refcounted. Releasing references to it may be done using +* put_device(). +*/ +void media_devnode_init (struct media_devnode *devnode); +/** * media_devnode_register - register a media device node * @@ -100,11 +111,9 @@ struct media_devnode {* with the kernel. An error is returned if no free minor number can be found, * or if the registration of the device node fails. * -* Zero is returned on success. -* -* Note that if the media_devnode_register call fails, the release() callback of -* the media_devnode structure is *not* called, so the caller is responsible for -* freeing any data. +* Zero is returned on success. Note that in case +* media_devnode_register() fails, the caller is responsible for +* releasing the reference to the device using put_device(). */ int __must_check media_devnode_register (struct media_devnode *devnode,struct module *owner);
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerRe: [PATCH v2 11/29] media: mc: Split initialising and adding media devnode
Hi Laurent,
Thanks for the review.
Quoted 8 lines · Laurent Pinchart
Hi Sakari,
Thank you for the patch.
Quoted 1 line · Sakari Ailus
As registering a device node of an entity belonging to a media device
Did you mean s/As registering/Registering/ ?
That works better in the current context. I'll use that.
Quoted 9 lines
Quoted 3 lines
will require a reference to the struct device. Taking that reference is only possible once the device has been initialised, which took place only
s/took/takes/
Quoted 1 line
when it was registered. Split this in two, and initialise the device when
s/was/is/
This describes the behaviour before the patch. I think it makes sense to use imperfect here.
Quoted 6 lines
Quoted 5 lines
the media device is allocated.
Don't distribute the effects of these changes yet. Add
media_device_get()andmedia_device_put()first.
Quoted 6 lines
Don't propagate the effects of these changes to drivers yet, we want to expose
media_devicerefcounting withmedia_device_get()andmedia_device_put()functions first.I'm not sure that's exactly what you meant though.
Yes, that's roughly what I meant, I can use the above text.
Quoted 5 lines
Quoted 3 lines
Signed-off-by Sakari Ailus <[email protected]>Acked-by Hans Verkuil <[email protected]>Reviewed-by Laurent Pinchart <[email protected]>
Thanks!
Quoted 30 lines
Quoted 30 lines
--- drivers/media/mc/mc-device.c | 18 +++++++++++++----- drivers/media/mc/mc-devnode.c | 17 ++++++++++------- include/media/media-devnode.h | 19 ++++++++++++++----- 3 files changed, 37 insertions(+), 17 deletions(-)drivers/media/mc/mc-device.c +5
-5@@ -717,19 +717,26 @@ int __must_check __media_device_register(struct media_device *mdev,/* Set version 0 to indicate user-space that the graph is static */ mdev->topology_version =0 ; +media_devnode_init (&mdev->devnode); + ret =media_devnode_register (&mdev->devnode, owner);if (ret <0 ) -return ret; + goto out_put; ret =device_create_file (&mdev->devnode.dev, &dev_attr_model); -if (ret <0 ) { -media_devnode_unregister (&mdev->devnode); -return ret; - } +if (ret <0 ) + goto out_unregister;
Quoted 7 lines
drivers/media/mc/mc-device.c int __must_check __media_device_register(struct media_device *mdev, · line 734
Quoted 5 lines
drivers/media/mc/mc-device.c +2
-0dev_dbg (mdev->dev,"Media device registered\n" );return 0 ; + +out_unregister:I would name the labels
err_unregisteranderr_put.
Sounds good.
Quoted 114 lines
Quoted 113 lines
drivers/media/mc/mc-device.c +6
-0+media_devnode_unregister (&mdev->devnode); +out_put: +put_device (&mdev->devnode.dev); + +return ret; }EXPORT_SYMBOL_GPL (__media_device_register);@@ -803,6 +810,7 @@ void media_device_unregister(struct media_device *mdev)device_remove_file (&mdev->devnode.dev, &dev_attr_model);dev_dbg (mdev->dev,"Media device unregistering\n" );media_devnode_unregister (&mdev->devnode); +put_device (&mdev->devnode.dev); }EXPORT_SYMBOL_GPL (media_device_unregister);drivers/media/mc/mc-devnode.c +10
-7@@ -204,6 +204,11 @@ static const struct file_operations media_devnode_fops = { .llseek = no_llseek, }; +void media_devnode_init (struct media_devnode *devnode) +{ +device_initialize (&devnode->dev); +} +int __must_check media_devnode_register (struct media_devnode *devnode,struct module *owner) {@@ -235,7 +240,6 @@ int __must_check media_devnode_register(struct media_devnode *devnode,if (devnode->parent) devnode->dev.parent = devnode->parent;dev_set_name (&devnode->dev,"media%d" , devnode->minor); -device_initialize (&devnode->dev);/* Part 3: Add the media and character devices */ ret =cdev_device_add (&devnode->cdev, &devnode->dev);@@ -267,14 +271,13 @@ void media_devnode_unregister(struct media_devnode *devnode)clear_bit (MEDIA_FLAG_REGISTERED , &devnode->flags);mutex_unlock (&media_devnode_lock); -cdev_del (&devnode->cdev); -device_unregister (&devnode->dev); +cdev_device_del (&devnode->cdev, &devnode->dev); }/* * Initialise media for linux */ -static int __init media_devnode_init (void ) +static int __init media_devnode_module_init (void ) {int ret;@@ -296,14 +299,14 @@ static int __init media_devnode_init(void)return 0 ; } -static void __exit media_devnode_exit(void) +static void __exit media_devnode_module_exit(void) {bus_unregister (&media_bus_type);unregister_chrdev_region (media_dev_t,MEDIA_NUM_DEVICES ); } -subsys_initcall (media_devnode_init); -module_exit(media_devnode_exit) +subsys_initcall (media_devnode_module_init); +module_exit(media_devnode_module_exit) MODULE_AUTHOR("Laurent Pinchart <[email protected]>");MODULE_DESCRIPTION ("Device node registration for media drivers" );include/media/media-devnode.h +14
-5@@ -90,6 +90,17 @@ struct media_devnode {/* dev to media_devnode */ #define to_media_devnode (cd) container_of(cd, struct media_devnode, dev) +/** +* media_devnode_init - initialise a media devnode +* +* @devnode: struct media_devnode we want to initialise +* +* Initialise a media devnode. Note that after initialising the media +* devnode is refcounted. Releasing references to it may be done using +* put_device(). +*/ +void media_devnode_init (struct media_devnode *devnode); +/** * media_devnode_register - register a media device node * @@ -100,11 +111,9 @@ struct media_devnode {* with the kernel. An error is returned if no free minor number can be found, * or if the registration of the device node fails. * -* Zero is returned on success. -* -* Note that if the media_devnode_register call fails, the release() callback of -* the media_devnode structure is *not* called, so the caller is responsible for -* freeing any data. +* Zero is returned on success. Note that in case +* media_devnode_register() fails, the caller is responsible for +* releasing the reference to the device using put_device(). */ int __must_check media_devnode_register (struct media_devnode *devnode,struct module *owner);
Signature · 3 lines
Regards, Sakari Ailus
Sakari Ailus
maintainerPATCH v2 12/29 media: mc: Shuffle functions around
In reply to Sakari AilusAs the call paths of the functions in question will change, move them around in anticipation of that. No other changes.
--- drivers/media/mc/mc-device.c | 54 ++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 27 deletions(-)
drivers/media/mc/mc-device.c +27 -27
@@ -673,6 +673,33 @@ void media_device_unregister_entity(struct media_entity *entity) }EXPORT_SYMBOL_GPL (media_device_unregister_entity); +void media_device_register_entity_notify (struct media_device *mdev, +struct media_entity_notify *nptr) +{ +mutex_lock (&mdev->graph_mutex); +list_add_tail (&nptr->list, &mdev->entity_notify); +mutex_unlock (&mdev->graph_mutex); +} +EXPORT_SYMBOL_GPL (media_device_register_entity_notify); + +/* +* Note: Should be called with mdev->lock held. +*/ +static void __media_device_unregister_entity_notify (struct media_device *mdev, +struct media_entity_notify *nptr) +{ +list_del (&nptr->list); +} + +void media_device_unregister_entity_notify (struct media_device *mdev, +struct media_entity_notify *nptr) +{ +mutex_lock (&mdev->graph_mutex); +__media_device_unregister_entity_notify (mdev, nptr); +mutex_unlock (&mdev->graph_mutex); +} +EXPORT_SYMBOL_GPL (media_device_unregister_entity_notify); +void media_device_init (struct media_device *mdev) {INIT_LIST_HEAD (&mdev->entities);
@@ -740,33 +767,6 @@ int __must_check __media_device_register(struct media_device *mdev, }EXPORT_SYMBOL_GPL (__media_device_register); -void media_device_register_entity_notify (struct media_device *mdev, -struct media_entity_notify *nptr) -{ -mutex_lock (&mdev->graph_mutex); -list_add_tail (&nptr->list, &mdev->entity_notify); -mutex_unlock (&mdev->graph_mutex); -} -EXPORT_SYMBOL_GPL (media_device_register_entity_notify); - -/* -* Note: Should be called with mdev->lock held. -*/ -static void __media_device_unregister_entity_notify (struct media_device *mdev, -struct media_entity_notify *nptr) -{ -list_del (&nptr->list); -} - -void media_device_unregister_entity_notify (struct media_device *mdev, -struct media_entity_notify *nptr) -{ -mutex_lock (&mdev->graph_mutex); -__media_device_unregister_entity_notify (mdev, nptr); -mutex_unlock (&mdev->graph_mutex); -} -EXPORT_SYMBOL_GPL (media_device_unregister_entity_notify); -void media_device_unregister (struct media_device *mdev) {struct media_entity *entity;
Signature · 1 line
2.39.2
Re: [PATCH v2 12/29] media: mc: Shuffle functions around
Hi Sakari,
Thank you for the patch.
Quoted 5 lines · Sakari Ailus
As the call paths of the functions in question will change, move them around in anticipation of that. No other changes.
Signed-off-by Sakari Ailus <[email protected]>Acked-by Hans Verkuil <[email protected]>
Quoted 76 lines
--- drivers/media/mc/mc-device.c | 54 ++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 27 deletions(-)drivers/media/mc/mc-device.c +27
-27@@ -673,6 +673,33 @@ void media_device_unregister_entity(struct media_entity *entity) }EXPORT_SYMBOL_GPL (media_device_unregister_entity); +void media_device_register_entity_notify (struct media_device *mdev, +struct media_entity_notify *nptr) +{ +mutex_lock (&mdev->graph_mutex); +list_add_tail (&nptr->list, &mdev->entity_notify); +mutex_unlock (&mdev->graph_mutex); +} +EXPORT_SYMBOL_GPL (media_device_register_entity_notify); + +/* +* Note: Should be called with mdev->lock held. +*/ +static void __media_device_unregister_entity_notify (struct media_device *mdev, +struct media_entity_notify *nptr) +{ +list_del (&nptr->list); +} + +void media_device_unregister_entity_notify (struct media_device *mdev, +struct media_entity_notify *nptr) +{ +mutex_lock (&mdev->graph_mutex); +__media_device_unregister_entity_notify (mdev, nptr); +mutex_unlock (&mdev->graph_mutex); +} +EXPORT_SYMBOL_GPL (media_device_unregister_entity_notify); +void media_device_init (struct media_device *mdev) {INIT_LIST_HEAD (&mdev->entities);@@ -740,33 +767,6 @@ int __must_check __media_device_register(struct media_device *mdev, }EXPORT_SYMBOL_GPL (__media_device_register); -void media_device_register_entity_notify (struct media_device *mdev, -struct media_entity_notify *nptr) -{ -mutex_lock (&mdev->graph_mutex); -list_add_tail (&nptr->list, &mdev->entity_notify); -mutex_unlock (&mdev->graph_mutex); -} -EXPORT_SYMBOL_GPL (media_device_register_entity_notify); - -/* -* Note: Should be called with mdev->lock held. -*/ -static void __media_device_unregister_entity_notify (struct media_device *mdev, -struct media_entity_notify *nptr) -{ -list_del (&nptr->list); -} - -void media_device_unregister_entity_notify (struct media_device *mdev, -struct media_entity_notify *nptr) -{ -mutex_lock (&mdev->graph_mutex); -__media_device_unregister_entity_notify (mdev, nptr); -mutex_unlock (&mdev->graph_mutex); -} -EXPORT_SYMBOL_GPL (media_device_unregister_entity_notify); -void media_device_unregister (struct media_device *mdev) {struct media_entity *entity;
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerPATCH v2 13/29 media: mc: Initialise media devnode in media_device_init()
In reply to Sakari AilusCall media_devnode_init() from media_device_init(). This has the effect of creating a struct device for the media_devnode before it is registered, making it possible to obtain a reference to it for e.g. video devices.
--- drivers/media/mc/mc-device.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-)
drivers/media/mc/mc-device.c +7 -14
@@ -711,8 +711,8 @@ void media_device_init(struct media_device *mdev)mutex_init (&mdev->req_queue_mutex);mutex_init (&mdev->graph_mutex);ida_init (&mdev->entity_internal_idx); -atomic_set (&mdev->request_id,0 ); +media_devnode_init (&mdev->devnode);if (!*mdev->bus_info) media_set_bus_info(mdev->bus_info, sizeof(mdev->bus_info),
@@ -729,6 +729,7 @@ void media_device_cleanup(struct media_device *mdev)media_graph_walk_cleanup (&mdev->pm_count_walk);mutex_destroy (&mdev->graph_mutex);mutex_destroy (&mdev->req_queue_mutex); +put_device (&mdev->devnode.dev); }EXPORT_SYMBOL_GPL (media_device_cleanup);
@@ -744,26 +745,19 @@ int __must_check __media_device_register(struct media_device *mdev,/* Set version 0 to indicate user-space that the graph is static */ mdev->topology_version =0 ; -media_devnode_init (&mdev->devnode); - ret =media_devnode_register (&mdev->devnode, owner);if (ret <0 ) - goto out_put; +return ret; ret =device_create_file (&mdev->devnode.dev, &dev_attr_model); -if (ret <0 ) - goto out_unregister; +if (ret <0 ) { +media_devnode_unregister (&mdev->devnode); +return ret; + }dev_dbg (mdev->dev,"Media device registered\n" );return 0 ; - -out_unregister: -media_devnode_unregister (&mdev->devnode); -out_put: -put_device (&mdev->devnode.dev); - -return ret; }EXPORT_SYMBOL_GPL (__media_device_register);
@@ -810,7 +804,6 @@ void media_device_unregister(struct media_device *mdev)device_remove_file (&mdev->devnode.dev, &dev_attr_model);dev_dbg (mdev->dev,"Media device unregistering\n" );media_devnode_unregister (&mdev->devnode); -put_device (&mdev->devnode.dev); }EXPORT_SYMBOL_GPL (media_device_unregister);
Signature · 1 line
2.39.2
Re: [PATCH v2 13/29] media: mc: Initialise media devnode in media_device_init()
Hi Sakari,
Thank you for the patch.
Quoted 15 lines · Sakari Ailus
Call
media_devnode_init()frommedia_device_init(). This has the effect of creating a struct device for themedia_devnodebefore it is registered, making it possible to obtain a reference to it for e.g. video devices.Signed-off-by Sakari Ailus <[email protected]>Acked-by Hans Verkuil <[email protected]>--- drivers/media/mc/mc-device.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-)drivers/media/mc/mc-device.c +0
-0
drivers/media/mc/mc-device.c void media_device_init(struct media_device *mdev) · line 714
Quoted 5 lines
drivers/media/mc/mc-device.c +0
-1@@ -711,8 +711,8 @@ void media_device_init(struct media_device *mdev)mutex_init (&mdev->req_queue_mutex);mutex_init (&mdev->graph_mutex);ida_init (&mdev->entity_internal_idx); -atomic_set (&mdev->request_id,0 );
I would add a blank line here.
Quoted 52 lines
drivers/media/mc/mc-device.c +7
-13+media_devnode_init (&mdev->devnode);if (!*mdev->bus_info) media_set_bus_info(mdev->bus_info, sizeof(mdev->bus_info),@@ -729,6 +729,7 @@ void media_device_cleanup(struct media_device *mdev)media_graph_walk_cleanup (&mdev->pm_count_walk);mutex_destroy (&mdev->graph_mutex);mutex_destroy (&mdev->req_queue_mutex); +put_device (&mdev->devnode.dev); }EXPORT_SYMBOL_GPL (media_device_cleanup);@@ -744,26 +745,19 @@ int __must_check __media_device_register(struct media_device *mdev,/* Set version 0 to indicate user-space that the graph is static */ mdev->topology_version =0 ; -media_devnode_init (&mdev->devnode); - ret =media_devnode_register (&mdev->devnode, owner);if (ret <0 ) - goto out_put; +return ret; ret =device_create_file (&mdev->devnode.dev, &dev_attr_model); -if (ret <0 ) - goto out_unregister; +if (ret <0 ) { +media_devnode_unregister (&mdev->devnode); +return ret; + }dev_dbg (mdev->dev,"Media device registered\n" );return 0 ; - -out_unregister: -media_devnode_unregister (&mdev->devnode); -out_put: -put_device (&mdev->devnode.dev); - -return ret; }EXPORT_SYMBOL_GPL (__media_device_register);@@ -810,7 +804,6 @@ void media_device_unregister(struct media_device *mdev)device_remove_file (&mdev->devnode.dev, &dev_attr_model);dev_dbg (mdev->dev,"Media device unregistering\n" );media_devnode_unregister (&mdev->devnode); -put_device (&mdev->devnode.dev); }EXPORT_SYMBOL_GPL (media_device_unregister);
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerPATCH v2 14/29 media: mc: Refactor media devnode minor clearing
In reply to Sakari AilusRefactor clearing media devnode minor bit in media devnode bitmap. Note that number is used instead of struct media_devnode as argument since the minor number will also be stored in a different structure soon.
--- drivers/media/mc/mc-devnode.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)
drivers/media/mc/mc-devnode.c +11 -8
@@ -44,17 +44,22 @@ static dev_t media_dev_t;static DEFINE_MUTEX (media_devnode_lock); static DECLARE_BITMAP(media_devnode_nums, MEDIA_NUM_DEVICES); -/* Called when the last user of the media device exits. */ -static void media_devnode_release (struct device *cd) +static void media_devnode_free_minor (unsigned int minor) { -struct media_devnode *devnode =to_media_devnode (cd); -mutex_lock (&media_devnode_lock);/* Mark device node number as free */ -clear_bit (devnode->minor, media_devnode_nums); +clear_bit (minor, media_devnode_nums);mutex_unlock (&media_devnode_lock); +} + +/* Called when the last user of the media device exits. */ +static void media_devnode_release (struct device *cd) +{ +struct media_devnode *devnode =to_media_devnode (cd); + +media_devnode_free_minor (devnode->minor);/* Release media_devnode and perform other cleanups as needed. */ if (devnode->release)
@@ -254,9 +259,7 @@ int __must_check media_devnode_register(struct media_devnode *devnode,return 0 ; cdev_add_error: -mutex_lock (&media_devnode_lock); -clear_bit (devnode->minor, media_devnode_nums); -mutex_unlock (&media_devnode_lock); +media_devnode_free_minor (devnode->minor);return ret; }
Signature · 1 line
2.39.2
Re: [PATCH v2 14/29] media: mc: Refactor media devnode minor clearing
Quoted 5 lines · Sakari Ailus
Refactor clearing media devnode minor bit in media devnode bitmap. Note that number is used instead of struct
media_devnodeas argument since the minor number will also be stored in a different structure soon.Signed-off-by Sakari Ailus <[email protected]>
Quoted 47 lines
--- drivers/media/mc/mc-devnode.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)drivers/media/mc/mc-devnode.c +11
-8@@ -44,17 +44,22 @@ static dev_t media_dev_t;static DEFINE_MUTEX (media_devnode_lock); static DECLARE_BITMAP(media_devnode_nums, MEDIA_NUM_DEVICES); -/* Called when the last user of the media device exits. */ -static void media_devnode_release (struct device *cd) +static void media_devnode_free_minor (unsigned int minor) { -struct media_devnode *devnode =to_media_devnode (cd); -mutex_lock (&media_devnode_lock);/* Mark device node number as free */ -clear_bit (devnode->minor, media_devnode_nums); +clear_bit (minor, media_devnode_nums);mutex_unlock (&media_devnode_lock); +} + +/* Called when the last user of the media device exits. */ +static void media_devnode_release (struct device *cd) +{ +struct media_devnode *devnode =to_media_devnode (cd); + +media_devnode_free_minor (devnode->minor);/* Release media_devnode and perform other cleanups as needed. */ if (devnode->release)@@ -254,9 +259,7 @@ int __must_check media_devnode_register(struct media_devnode *devnode,return 0 ; cdev_add_error: -mutex_lock (&media_devnode_lock); -clear_bit (devnode->minor, media_devnode_nums); -mutex_unlock (&media_devnode_lock); +media_devnode_free_minor (devnode->minor);return ret; }
Re: [PATCH v2 14/29] media: mc: Refactor media devnode minor clearing
In reply to Sakari AilusHi Sakari,
Thank you for the patch.
Quoted 5 lines · Sakari Ailus
Refactor clearing media devnode minor bit in media devnode bitmap. Note that number is used instead of struct
media_devnodeas argument since the minor number will also be stored in a different structure soon.Signed-off-by Sakari Ailus <[email protected]>
Quoted 47 lines
--- drivers/media/mc/mc-devnode.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)drivers/media/mc/mc-devnode.c +11
-8@@ -44,17 +44,22 @@ static dev_t media_dev_t;static DEFINE_MUTEX (media_devnode_lock); static DECLARE_BITMAP(media_devnode_nums, MEDIA_NUM_DEVICES); -/* Called when the last user of the media device exits. */ -static void media_devnode_release (struct device *cd) +static void media_devnode_free_minor (unsigned int minor) { -struct media_devnode *devnode =to_media_devnode (cd); -mutex_lock (&media_devnode_lock);/* Mark device node number as free */ -clear_bit (devnode->minor, media_devnode_nums); +clear_bit (minor, media_devnode_nums);mutex_unlock (&media_devnode_lock); +} + +/* Called when the last user of the media device exits. */ +static void media_devnode_release (struct device *cd) +{ +struct media_devnode *devnode =to_media_devnode (cd); + +media_devnode_free_minor (devnode->minor);/* Release media_devnode and perform other cleanups as needed. */ if (devnode->release)@@ -254,9 +259,7 @@ int __must_check media_devnode_register(struct media_devnode *devnode,return 0 ; cdev_add_error: -mutex_lock (&media_devnode_lock); -clear_bit (devnode->minor, media_devnode_nums); -mutex_unlock (&media_devnode_lock); +media_devnode_free_minor (devnode->minor);return ret; }
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerPATCH v2 15/29 media: mc: Unassign minor only if it has been assigned
In reply to Sakari AilusAssign the media device minor to -1 if it has not been previously assigned. There's no dependence to this yes but it will be required by patch "media: mc: Implement best effort media device removal safety sans refcount" soon.
--- drivers/media/mc/mc-devnode.c | 9 ++++++++- include/media/media-devnode.h | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-)
drivers/media/mc/mc-devnode.c +8 -1
@@ -59,7 +59,8 @@ static void media_devnode_release(struct device *cd) {struct media_devnode *devnode =to_media_devnode (cd); -media_devnode_free_minor (devnode->minor); +if (devnode->minor !=-1 ) +media_devnode_free_minor (devnode->minor);/* Release media_devnode and perform other cleanups as needed. */ if (devnode->release)
@@ -212,6 +213,7 @@ static const struct file_operations media_devnode_fops = {void media_devnode_init (struct media_devnode *devnode) {device_initialize (&devnode->dev); + devnode->minor =-1 ; } int __must_check media_devnode_register(struct media_devnode *devnode,
@@ -220,6 +222,9 @@ int __must_check media_devnode_register(struct media_devnode *devnode,int minor;int ret; +if (devnode->minor !=-1 ) +return -EINVAL ; +/* Part 1: Find a free minor number */ mutex_lock (&media_devnode_lock); minor =find_first_zero_bit (media_devnode_nums,MEDIA_NUM_DEVICES );
@@ -261,6 +266,8 @@ int __must_check media_devnode_register(struct media_devnode *devnode, cdev_add_error:media_devnode_free_minor (devnode->minor); + devnode->minor =-1 ; +return ret; }
include/media/media-devnode.h +1 -1
@@ -60,7 +60,7 @@ struct media_file_operations {* @dev: pointer to struct &device containing the media controller device * @cdev: struct cdev pointer character device * @parent: parent device -* @minor: device node minor number +* @minor: device node minor number, -1 if unassigned * @flags: flags, combination of the ``MEDIA_FLAG_*`` constants * @release: release callback called at the end of ``media_devnode_release()`` * routine at media-device.c.
Signature · 1 line
2.39.2
Re: [PATCH v2 15/29] media: mc: Unassign minor only if it has been assigned
Quoted 2 lines · Sakari Ailus
Assign the media device minor to -1 if it has not been previously assigned. There's no dependence to this yes but it will be required by
yes -> yet
Quoted 4 lines
patch "media: mc: Implement best effort media device removal safety sans refcount" soon.
Signed-off-by Sakari Ailus <[email protected]>
After fixing that somewhat confusing typo above:
Quoted 59 lines
--- drivers/media/mc/mc-devnode.c | 9 ++++++++- include/media/media-devnode.h | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-)drivers/media/mc/mc-devnode.c +8
-1@@ -59,7 +59,8 @@ static void media_devnode_release(struct device *cd) {struct media_devnode *devnode =to_media_devnode (cd); -media_devnode_free_minor (devnode->minor); +if (devnode->minor !=-1 ) +media_devnode_free_minor (devnode->minor);/* Release media_devnode and perform other cleanups as needed. */ if (devnode->release)@@ -212,6 +213,7 @@ static const struct file_operations media_devnode_fops = {void media_devnode_init (struct media_devnode *devnode) {device_initialize (&devnode->dev); + devnode->minor =-1 ; } int __must_check media_devnode_register(struct media_devnode *devnode,@@ -220,6 +222,9 @@ int __must_check media_devnode_register(struct media_devnode *devnode,int minor;int ret; +if (devnode->minor !=-1 ) +return -EINVAL ; +/* Part 1: Find a free minor number */ mutex_lock (&media_devnode_lock); minor =find_first_zero_bit (media_devnode_nums,MEDIA_NUM_DEVICES );@@ -261,6 +266,8 @@ int __must_check media_devnode_register(struct media_devnode *devnode, cdev_add_error:media_devnode_free_minor (devnode->minor); + devnode->minor =-1 ; +return ret; }include/media/media-devnode.h +1
-1@@ -60,7 +60,7 @@ struct media_file_operations {* @dev: pointer to struct &device containing the media controller device * @cdev: struct cdev pointer character device * @parent: parent device -* @minor: device node minor number +* @minor: device node minor number, -1 if unassigned * @flags: flags, combination of the ``MEDIA_FLAG_*`` constants * @release: release callback called at the end of ``media_devnode_release()`` * routine at media-device.c.
Laurent Pinchart
maintainerRe: [PATCH v2 15/29] media: mc: Unassign minor only if it has been assigned
In reply to Sakari AilusHi Sakari,
Thank you for the patch.
Quoted 4 lines · Sakari Ailus
Assign the media device minor to -1 if it has not been previously assigned. There's no dependence to this yes but it will be required by patch "media: mc: Implement best effort media device removal safety sans refcount" soon.
After a quick look at that patch, I don't see the dependency I'm afraid. Could you please explain it better ?
Quoted 11 lines
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/mc/mc-devnode.c | 9 ++++++++- include/media/media-devnode.h | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-)drivers/media/mc/mc-devnode.c +0
-0
drivers/media/mc/mc-devnode.c static void media_devnode_release(struct device *cd) · line 63
Quoted 6 lines
drivers/media/mc/mc-devnode.c +2
-1@@ -59,7 +59,8 @@ static void media_devnode_release(struct device *cd) {struct media_devnode *devnode =to_media_devnode (cd); -media_devnode_free_minor (devnode->minor); +if (devnode->minor !=-1 ) +media_devnode_free_minor (devnode->minor);
Should the test be moved to media_devnode_free_minor() ?
Quoted 43 lines
drivers/media/mc/mc-devnode.c +6
-0/* Release media_devnode and perform other cleanups as needed. */ if (devnode->release)@@ -212,6 +213,7 @@ static const struct file_operations media_devnode_fops = {void media_devnode_init (struct media_devnode *devnode) {device_initialize (&devnode->dev); + devnode->minor =-1 ; } int __must_check media_devnode_register(struct media_devnode *devnode,@@ -220,6 +222,9 @@ int __must_check media_devnode_register(struct media_devnode *devnode,int minor;int ret; +if (devnode->minor !=-1 ) +return -EINVAL ; +/* Part 1: Find a free minor number */ mutex_lock (&media_devnode_lock); minor =find_first_zero_bit (media_devnode_nums,MEDIA_NUM_DEVICES );@@ -261,6 +266,8 @@ int __must_check media_devnode_register(struct media_devnode *devnode, cdev_add_error:media_devnode_free_minor (devnode->minor); + devnode->minor =-1 ; +return ret; }include/media/media-devnode.h +1
-1@@ -60,7 +60,7 @@ struct media_file_operations {* @dev: pointer to struct &device containing the media controller device * @cdev: struct cdev pointer character device * @parent: parent device -* @minor: device node minor number +* @minor: device node minor number, -1 if unassigned * @flags: flags, combination of the ``MEDIA_FLAG_*`` constants * @release: release callback called at the end of ``media_devnode_release()`` * routine at media-device.c.
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerRe: [PATCH v2 15/29] media: mc: Unassign minor only if it has been assigned
Hi Laurent,
Quoted 27 lines · Laurent Pinchart
Hi Sakari,
Thank you for the patch.
Quoted 4 lines · Sakari Ailus
Assign the media device minor to -1 if it has not been previously assigned. There's no dependence to this yes but it will be required by patch "media: mc: Implement best effort media device removal safety sans refcount" soon.
After a quick look at that patch, I don't see the dependency I'm afraid. Could you please explain it better ?
Quoted 14 lines
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/mc/mc-devnode.c | 9 ++++++++- include/media/media-devnode.h | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-)drivers/media/mc/mc-devnode.c +0
-0@@ -59,7 +59,8 @@ static void media_devnode_release(struct device *cd) {struct media_devnode *devnode =to_media_devnode (cd);
Quoted 5 lines
drivers/media/mc/mc-devnode.c static void media_devnode_release(struct device *cd) · line 63
Quoted 3 lines
drivers/media/mc/mc-devnode.c +2
-1-media_devnode_free_minor (devnode->minor); +if (devnode->minor !=-1 ) +media_devnode_free_minor (devnode->minor);Should the test be moved to
media_devnode_free_minor()?
The intention here is to free the minor number once and only once.
Moving the test to media_devnode_free_minor() is not an option as the devnode may be released already by that time. See the patch the commit message refers to.
Quoted 45 lines
Quoted 44 lines
drivers/media/mc/mc-devnode.c +6
-0/* Release media_devnode and perform other cleanups as needed. */ if (devnode->release)@@ -212,6 +213,7 @@ static const struct file_operations media_devnode_fops = {void media_devnode_init (struct media_devnode *devnode) {device_initialize (&devnode->dev); + devnode->minor =-1 ; } int __must_check media_devnode_register(struct media_devnode *devnode,@@ -220,6 +222,9 @@ int __must_check media_devnode_register(struct media_devnode *devnode,int minor;int ret; +if (devnode->minor !=-1 ) +return -EINVAL ; +/* Part 1: Find a free minor number */ mutex_lock (&media_devnode_lock); minor =find_first_zero_bit (media_devnode_nums,MEDIA_NUM_DEVICES );@@ -261,6 +266,8 @@ int __must_check media_devnode_register(struct media_devnode *devnode, cdev_add_error:media_devnode_free_minor (devnode->minor); + devnode->minor =-1 ; +return ret; }include/media/media-devnode.h +1
-1@@ -60,7 +60,7 @@ struct media_file_operations {* @dev: pointer to struct &device containing the media controller device * @cdev: struct cdev pointer character device * @parent: parent device -* @minor: device node minor number +* @minor: device node minor number, -1 if unassigned * @flags: flags, combination of the ``MEDIA_FLAG_*`` constants * @release: release callback called at the end of ``media_devnode_release()`` * routine at media-device.c.
Signature · 3 lines
Regards, Sakari Ailus
Sakari Ailus
maintainerPATCH v2 16/29 media: mc: Refcount the media device
In reply to Sakari AilusAs the struct media_device embeds struct media_devnode, the lifetime of that object must be that same than that of the media_device.
References are obtained by media_device_get() and released by media_device_put(). In order to use refcounting, the driver must set the release callback before calling media_device_init() on the media device.
--- drivers/media/mc/mc-device.c | 36 +++++++++++++++++++++++++++++------ drivers/media/mc/mc-devnode.c | 6 +++++- include/media/media-device.h | 28 +++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 7 deletions(-)
drivers/media/mc/mc-device.c +30 -6
@@ -700,6 +700,31 @@ void media_device_unregister_entity_notify(struct media_device *mdev, }EXPORT_SYMBOL_GPL (media_device_unregister_entity_notify); +static void __media_device_release (struct media_device *mdev) +{ +dev_dbg (mdev->dev,"Media device released\n" ); + +ida_destroy (&mdev->entity_internal_idx); + mdev->entity_internal_idx_max =0 ; +media_graph_walk_cleanup (&mdev->pm_count_walk); +mutex_destroy (&mdev->graph_mutex); +mutex_destroy (&mdev->req_queue_mutex); +} + +static void media_device_release (struct media_devnode *devnode) +{ +struct media_device *mdev =to_media_device (devnode); + +if (mdev->ops && mdev->ops->release) { +/* +* If release op isn't set, __media_device_release() is called +* via media_device_cleanup(). +*/ +__media_device_release (mdev); + mdev->ops->release (mdev); + } +} +void media_device_init (struct media_device *mdev) {INIT_LIST_HEAD (&mdev->entities);
@@ -712,6 +737,8 @@ void media_device_init(struct media_device *mdev)mutex_init (&mdev->graph_mutex);ida_init (&mdev->entity_internal_idx);atomic_set (&mdev->request_id,0 ); + + mdev->devnode.release = media_device_release;media_devnode_init (&mdev->devnode);if (!*mdev->bus_info)
@@ -724,12 +751,9 @@ EXPORT_SYMBOL_GPL(media_device_init);void media_device_cleanup (struct media_device *mdev) { -ida_destroy (&mdev->entity_internal_idx); - mdev->entity_internal_idx_max =0 ; -media_graph_walk_cleanup (&mdev->pm_count_walk); -mutex_destroy (&mdev->graph_mutex); -mutex_destroy (&mdev->req_queue_mutex); -put_device (&mdev->devnode.dev); +WARN_ON (mdev->ops && mdev->ops->release); +__media_device_release (mdev); +media_device_put (mdev); }EXPORT_SYMBOL_GPL (media_device_cleanup);
drivers/media/mc/mc-devnode.c +5 -1
@@ -59,6 +59,10 @@ static void media_devnode_release(struct device *cd) {struct media_devnode *devnode =to_media_devnode (cd); +/* If the devnode has a ref, it is simply released by the user. */ +if (devnode->ref) +return ; +if (devnode->minor !=-1 )media_devnode_free_minor (devnode->minor);
@@ -213,6 +217,7 @@ static const struct file_operations media_devnode_fops = {void media_devnode_init (struct media_devnode *devnode) {device_initialize (&devnode->dev); + devnode->dev.release = media_devnode_release; devnode->minor =-1 ; }
@@ -246,7 +251,6 @@ int __must_check media_devnode_register(struct media_devnode *devnode, devnode->dev.bus = &media_bus_type; devnode->dev.devt =MKDEV (MAJOR (media_dev_t), devnode->minor); - devnode->dev.release = media_devnode_release;if (devnode->parent) devnode->dev.parent = devnode->parent;dev_set_name (&devnode->dev,"media%d" , devnode->minor);
include/media/media-device.h +28 -0
@@ -62,6 +62,7 @@ struct media_entity_notify {* request (and thus the buffer) must be available to the driver. * And once a buffer is queued, then the driver can complete * or delete objects from the request before req_queue exits. +* @release: Release the resources of the media device. */ struct media_device_ops { int (*link_notify)(struct media_link *link, u32 flags,
@@ -70,6 +71,7 @@ struct media_device_ops {void (*req_free)(struct media_request *req);int (*req_validate)(struct media_request *req);void (*req_queue)(struct media_request *req); +void (*release)(struct media_device *mdev); };/**
@@ -219,6 +221,30 @@ struct usb_device;*/ void media_device_init (struct media_device *mdev); +/** +* media_device_get() - Get a reference to a media device +* +* @mdev: media device +*/ +#define media_device_get (mdev) \ + do { \ + dev_dbg((mdev)->dev, "%s: get media device %s\n", \ + __func__, (mdev)->bus_info); \ + get_device(&(mdev)->devnode.dev); \ + } while (0) + +/** +* media_device_put() - Put a reference to a media device +* +* @mdev: media device +*/ +#define media_device_put (mdev) \ + do { \ + dev_dbg((mdev)->dev, "%s: put media device %s\n", \ + __func__, (mdev)->bus_info); \ + put_device(&(mdev)->devnode.dev); \ + } while (0) +/** * media_device_cleanup() - Cleanups a media device element *
@@ -432,6 +458,8 @@ void __media_device_usb_init(struct media_device *mdev, const char *driver_name);#else +#define media_device_get (mdev) do { } while (0) +#define media_device_put (mdev) do { } while (0)static inline int media_device_register (struct media_device *mdev) {return 0 ;
Signature · 1 line
2.39.2
Laurent Pinchart
maintainerRe: [PATCH v2 16/29] media: mc: Refcount the media device
Hi Sakari,
Thank you for the patch.
Quoted 81 lines · Sakari Ailus
As the struct
media_deviceembeds structmedia_devnode, the lifetime of that object must be that same than that of themedia_device.References are obtained by
media_device_get()and released bymedia_device_put(). In order to use refcounting, the driver must set the release callback before callingmedia_device_init()on the media device.Signed-off-by Sakari Ailus <[email protected]>Acked-by Hans Verkuil <[email protected]>--- drivers/media/mc/mc-device.c | 36 +++++++++++++++++++++++++++++------ drivers/media/mc/mc-devnode.c | 6 +++++- include/media/media-device.h | 28 +++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 7 deletions(-)drivers/media/mc/mc-device.c +30
-6@@ -700,6 +700,31 @@ void media_device_unregister_entity_notify(struct media_device *mdev, }EXPORT_SYMBOL_GPL (media_device_unregister_entity_notify); +static void __media_device_release (struct media_device *mdev) +{ +dev_dbg (mdev->dev,"Media device released\n" ); + +ida_destroy (&mdev->entity_internal_idx); + mdev->entity_internal_idx_max =0 ; +media_graph_walk_cleanup (&mdev->pm_count_walk); +mutex_destroy (&mdev->graph_mutex); +mutex_destroy (&mdev->req_queue_mutex); +} + +static void media_device_release (struct media_devnode *devnode) +{ +struct media_device *mdev =to_media_device (devnode); + +if (mdev->ops && mdev->ops->release) { +/* +* If release op isn't set, __media_device_release() is called +* via media_device_cleanup(). +*/ +__media_device_release (mdev); + mdev->ops->release (mdev); + } +} +void media_device_init (struct media_device *mdev) {INIT_LIST_HEAD (&mdev->entities);@@ -712,6 +737,8 @@ void media_device_init(struct media_device *mdev)mutex_init (&mdev->graph_mutex);ida_init (&mdev->entity_internal_idx);atomic_set (&mdev->request_id,0 ); + + mdev->devnode.release = media_device_release;media_devnode_init (&mdev->devnode);if (!*mdev->bus_info)@@ -724,12 +751,9 @@ EXPORT_SYMBOL_GPL(media_device_init);void media_device_cleanup (struct media_device *mdev) { -ida_destroy (&mdev->entity_internal_idx); - mdev->entity_internal_idx_max =0 ; -media_graph_walk_cleanup (&mdev->pm_count_walk); -mutex_destroy (&mdev->graph_mutex); -mutex_destroy (&mdev->req_queue_mutex); -put_device (&mdev->devnode.dev); +WARN_ON (mdev->ops && mdev->ops->release); +__media_device_release (mdev); +media_device_put (mdev); }EXPORT_SYMBOL_GPL (media_device_cleanup);drivers/media/mc/mc-devnode.c +0
-0
drivers/media/mc/mc-devnode.c static void media_devnode_release(struct device *cd) · line 63
Quoted 5 lines
drivers/media/mc/mc-devnode.c +2
-0@@ -59,6 +59,10 @@ static void media_devnode_release(struct device *cd) {struct media_devnode *devnode =to_media_devnode (cd); +/* If the devnode has a ref, it is simply released by the user. */ +if (devnode->ref)
The structure has no ref member.
Quoted 42 lines
drivers/media/mc/mc-devnode.c +3
-1+return ; +if (devnode->minor !=-1 )media_devnode_free_minor (devnode->minor);@@ -213,6 +217,7 @@ static const struct file_operations media_devnode_fops = {void media_devnode_init (struct media_devnode *devnode) {device_initialize (&devnode->dev); + devnode->dev.release = media_devnode_release; devnode->minor =-1 ; }@@ -246,7 +251,6 @@ int __must_check media_devnode_register(struct media_devnode *devnode, devnode->dev.bus = &media_bus_type; devnode->dev.devt =MKDEV (MAJOR (media_dev_t), devnode->minor); - devnode->dev.release = media_devnode_release;if (devnode->parent) devnode->dev.parent = devnode->parent;dev_set_name (&devnode->dev,"media%d" , devnode->minor);include/media/media-device.h +2
-0@@ -62,6 +62,7 @@ struct media_entity_notify {* request (and thus the buffer) must be available to the driver. * And once a buffer is queued, then the driver can complete * or delete objects from the request before req_queue exits. +* @release: Release the resources of the media device. */ struct media_device_ops { int (*link_notify)(struct media_link *link, u32 flags,@@ -70,6 +71,7 @@ struct media_device_ops {void (*req_free)(struct media_request *req);int (*req_validate)(struct media_request *req);void (*req_queue)(struct media_request *req); +void (*release)(struct media_device *mdev); };/**
include/media/media-device.h struct usb_device; · line 225
Quoted 5 lines
include/media/media-device.h +2
-0@@ -219,6 +221,30 @@ struct usb_device;*/ void media_device_init (struct media_device *mdev); +/** +* media_device_get() - Get a reference to a media device
Maybe mimick the get_device() wording and state "atomically increment the reference count for the media device" ? Same for put.
include/media/media-device.h struct usb_device; · line 227
Quoted 2 lines
include/media/media-device.h +2
-0+* +* @mdev: media device
This should return a pointer to the media_device, as other get functions do.
include/media/media-device.h struct usb_device; · line 232
Quoted 5 lines
include/media/media-device.h +5
-0+*/ +#define media_device_get (mdev) \ + do { \ + dev_dbg((mdev)->dev, "%s: get media device %s\n", \ + __func__, (mdev)->bus_info); \
Do we really need this ? I'd prefer inline functions to ensure type safety. If we need to track the get/put callers, I think using ftrace would be a better option.
Quoted 27 lines
include/media/media-device.h +17
-0+get_device (&(mdev)->devnode.dev); \ + }while (0 ) + +/** +* media_device_put() - Put a reference to a media device +* +* @mdev: media device + */ +#define media_device_put (mdev) \ + do { \ + dev_dbg((mdev)->dev, "%s: put media device %s\n", \ + __func__, (mdev)->bus_info); \ + put_device(&(mdev)->devnode.dev); \ + } while (0) +/** * media_device_cleanup() - Cleanups a media device element * @@ -432,6 +458,8 @@ void __media_device_usb_init(struct media_device *mdev, const char *driver_name);#else +#define media_device_get (mdev) do { } while (0) +#define media_device_put (mdev) do { } while (0)static inline int media_device_register (struct media_device *mdev) {return 0 ;
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerRe: [PATCH v2 16/29] media: mc: Refcount the media device
Hi Laurent,
Quoted 89 lines · Laurent Pinchart
Hi Sakari,
Thank you for the patch.
Quoted 84 lines · Sakari Ailus
As the struct
media_deviceembeds structmedia_devnode, the lifetime of that object must be that same than that of themedia_device.References are obtained by
media_device_get()and released bymedia_device_put(). In order to use refcounting, the driver must set the release callback before callingmedia_device_init()on the media device.Signed-off-by Sakari Ailus <[email protected]>Acked-by Hans Verkuil <[email protected]>--- drivers/media/mc/mc-device.c | 36 +++++++++++++++++++++++++++++------ drivers/media/mc/mc-devnode.c | 6 +++++- include/media/media-device.h | 28 +++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 7 deletions(-)drivers/media/mc/mc-device.c +30
-6@@ -700,6 +700,31 @@ void media_device_unregister_entity_notify(struct media_device *mdev, }EXPORT_SYMBOL_GPL (media_device_unregister_entity_notify); +static void __media_device_release (struct media_device *mdev) +{ +dev_dbg (mdev->dev,"Media device released\n" ); + +ida_destroy (&mdev->entity_internal_idx); + mdev->entity_internal_idx_max =0 ; +media_graph_walk_cleanup (&mdev->pm_count_walk); +mutex_destroy (&mdev->graph_mutex); +mutex_destroy (&mdev->req_queue_mutex); +} + +static void media_device_release (struct media_devnode *devnode) +{ +struct media_device *mdev =to_media_device (devnode); + +if (mdev->ops && mdev->ops->release) { +/* +* If release op isn't set, __media_device_release() is called +* via media_device_cleanup(). +*/ +__media_device_release (mdev); + mdev->ops->release (mdev); + } +} +void media_device_init (struct media_device *mdev) {INIT_LIST_HEAD (&mdev->entities);@@ -712,6 +737,8 @@ void media_device_init(struct media_device *mdev)mutex_init (&mdev->graph_mutex);ida_init (&mdev->entity_internal_idx);atomic_set (&mdev->request_id,0 ); + + mdev->devnode.release = media_device_release;media_devnode_init (&mdev->devnode);if (!*mdev->bus_info)@@ -724,12 +751,9 @@ EXPORT_SYMBOL_GPL(media_device_init);void media_device_cleanup (struct media_device *mdev) { -ida_destroy (&mdev->entity_internal_idx); - mdev->entity_internal_idx_max =0 ; -media_graph_walk_cleanup (&mdev->pm_count_walk); -mutex_destroy (&mdev->graph_mutex); -mutex_destroy (&mdev->req_queue_mutex); -put_device (&mdev->devnode.dev); +WARN_ON (mdev->ops && mdev->ops->release); +__media_device_release (mdev); +media_device_put (mdev); }EXPORT_SYMBOL_GPL (media_device_cleanup);drivers/media/mc/mc-devnode.c +0
-0@@ -59,6 +59,10 @@ static void media_devnode_release(struct device *cd) {struct media_devnode *devnode =to_media_devnode (cd);
Quoted 4 lines
drivers/media/mc/mc-devnode.c static void media_devnode_release(struct device *cd) · line 63
Quoted 2 lines
drivers/media/mc/mc-devnode.c +2
-0+/* If the devnode has a ref, it is simply released by the user. */ +if (devnode->ref)The structure has no ref member.
This could explain some of the problems GCC has had compiling this patch. ;-)
This belongs to patch "media: mc: Implement best effort media device removal safety sans refcount".
Quoted 46 lines
Quoted 46 lines
drivers/media/mc/mc-devnode.c +3
-1+return ; +if (devnode->minor !=-1 )media_devnode_free_minor (devnode->minor);@@ -213,6 +217,7 @@ static const struct file_operations media_devnode_fops = {void media_devnode_init (struct media_devnode *devnode) {device_initialize (&devnode->dev); + devnode->dev.release = media_devnode_release; devnode->minor =-1 ; }@@ -246,7 +251,6 @@ int __must_check media_devnode_register(struct media_devnode *devnode, devnode->dev.bus = &media_bus_type; devnode->dev.devt =MKDEV (MAJOR (media_dev_t), devnode->minor); - devnode->dev.release = media_devnode_release;if (devnode->parent) devnode->dev.parent = devnode->parent;dev_set_name (&devnode->dev,"media%d" , devnode->minor);include/media/media-device.h +2
-0@@ -62,6 +62,7 @@ struct media_entity_notify {* request (and thus the buffer) must be available to the driver. * And once a buffer is queued, then the driver can complete * or delete objects from the request before req_queue exits. +* @release: Release the resources of the media device. */ struct media_device_ops { int (*link_notify)(struct media_link *link, u32 flags,@@ -70,6 +71,7 @@ struct media_device_ops {void (*req_free)(struct media_request *req);int (*req_validate)(struct media_request *req);void (*req_queue)(struct media_request *req); +void (*release)(struct media_device *mdev); };/** @@ -219,6 +221,30 @@ struct usb_device;*/ void media_device_init (struct media_device *mdev);
Quoted 5 lines
include/media/media-device.h struct usb_device; · line 225
Quoted 2 lines
include/media/media-device.h +2
-0+/** +* media_device_get() - Get a reference to a media device Maybe mimick the
get_device()wording and state "atomically increment the reference count for the media device" ? Same for put.
Sounds good.
Quoted 6 lines
include/media/media-device.h struct usb_device; · line 227
Quoted 3 lines
include/media/media-device.h +2
-0+* +* @mdev: media device This should return a pointer to the
media_device, as other get functions do.
Yes, I'll do that for v3.
Quoted 10 lines
include/media/media-device.h struct usb_device; · line 232
Quoted 6 lines
include/media/media-device.h +5
-0+*/ +#define media_device_get (mdev) \ + do { \ + dev_dbg((mdev)->dev, "%s: get media device %s\n", \ + __func__, (mdev)->bus_info); \Do we really need this ? I'd prefer inline functions to ensure type safety. If we need to track the get/put callers, I think using ftrace would be a better option.
I think we can drop this. It was useful for development though.
Quoted 29 lines
Quoted 28 lines
include/media/media-device.h +17
-0+get_device (&(mdev)->devnode.dev); \ + }while (0 ) + +/** +* media_device_put() - Put a reference to a media device +* +* @mdev: media device + */ +#define media_device_put (mdev) \ + do { \ + dev_dbg((mdev)->dev, "%s: put media device %s\n", \ + __func__, (mdev)->bus_info); \ + put_device(&(mdev)->devnode.dev); \ + } while (0) +/** * media_device_cleanup() - Cleanups a media device element * @@ -432,6 +458,8 @@ void __media_device_usb_init(struct media_device *mdev, const char *driver_name);#else +#define media_device_get (mdev) do { } while (0) +#define media_device_put (mdev) do { } while (0)static inline int media_device_register (struct media_device *mdev) {return 0 ;
Signature · 3 lines
Regards, Sakari Ailus
Sakari Ailus
maintainerPATCH v2 17/29 media: v4l: Acquire a reference to the media device for every video device
In reply to Sakari AilusThe video device depends on the existence of its media device --- if there is one. Acquire a reference to it.
Note that when the media device release callback is used, then the V4L2 device release callback is ignored and a warning is issued if both are set.
--- drivers/media/v4l2-core/v4l2-dev.c | 51 ++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 17 deletions(-)
drivers/media/v4l2-core/v4l2-dev.c +34 -17
@@ -176,6 +176,11 @@ static void v4l2_device_release(struct device *cd) {struct video_device *vdev =to_video_device (cd);struct v4l2_device *v4l2_dev = vdev->v4l2_dev; +bool v4l2_dev_has_release = v4l2_dev->release; +#ifdef CONFIG_MEDIA_CONTROLLER +struct media_device *mdev = v4l2_dev->mdev; +bool mdev_has_release = mdev && mdev->ops && mdev->ops->release; +#endif mutex_lock (&videodev_lock);if (WARN_ON (video_devices[vdev->minor] != vdev)) {
@@ -198,8 +203,8 @@ static void v4l2_device_release(struct device *cd)mutex_unlock (&videodev_lock); -#if defined(CONFIG_MEDIA_CONTROLLER ) -if (v4l2_dev->mdev && vdev->vfl_dir !=VFL_DIR_M2M ) { +#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev && vdev->vfl_dir !=VFL_DIR_M2M ) {/* Remove interfaces and interface links */ media_devnode_remove (vdev->intf_devnode);if (vdev->entity.function !=MEDIA_ENT_F_UNKNOWN )
@@ -207,23 +212,31 @@ static void v4l2_device_release(struct device *cd) }#endif -/* Do not call v4l2_device_put if there is no release callback set. -* Drivers that have no v4l2_device release callback might free the -* v4l2_dev instance in the video_device release callback below, so we -* must perform this check here. -* -* TODO: In the long run all drivers that use v4l2_device should use the -* v4l2_device release callback. This check will then be unnecessary. -*/ -if (v4l2_dev->release ==NULL ) - v4l2_dev =NULL ; -/* Release video_device and perform other cleanups as needed. */ vdev->release (vdev); -/* Decrease v4l2_device refcount */ -if (v4l2_dev) +#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev) +media_device_put (mdev); + +/* +* Generally both struct media_device and struct v4l2_device are +* embedded in the same driver's context struct so having a release +* callback in both is a bug. +*/ +WARN_ON (v4l2_dev_has_release && mdev_has_release); +#endif + +/* +* Decrease v4l2_device refcount, but only if the media device doesn't +* have a release callback. +*/ +if (v4l2_dev_has_release +#ifdef CONFIG_MEDIA_CONTROLLER + && !mdev_has_release +#endif + )v4l2_device_put (v4l2_dev); }
@@ -792,11 +805,14 @@ static int video_register_media_controller(struct video_device *vdev)u32 intf_type;int ret; -/* Memory-to-memory devices are more complex and use +/* +* Memory-to-memory devices are more complex and use * their own function to register its mc entities. */ -if (!vdev->v4l2_dev->mdev || vdev->vfl_dir ==VFL_DIR_M2M ) +if (!vdev->v4l2_dev->mdev || vdev->vfl_dir ==VFL_DIR_M2M ) { +media_device_get (vdev->v4l2_dev->mdev);return 0 ; + } vdev->entity.obj_type =MEDIA_ENTITY_TYPE_VIDEO_DEVICE ; vdev->entity.function =MEDIA_ENT_F_UNKNOWN ;
@@ -875,6 +891,7 @@ static int video_register_media_controller(struct video_device *vdev)/* FIXME: how to create the other interface links? */ +media_device_get (vdev->v4l2_dev->mdev);#endif return 0 ; }
Signature · 1 line
2.39.2
Re: [PATCH v2 17/29] media: v4l: Acquire a reference to the media device for every video device
Quoted 60 lines · Sakari Ailus
The video device depends on the existence of its media device --- if there is one. Acquire a reference to it.
Note that when the media device release callback is used, then the V4L2 device release callback is ignored and a warning is issued if both are set.
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/v4l2-core/v4l2-dev.c | 51 ++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 17 deletions(-)drivers/media/v4l2-core/v4l2-dev.c +7
-15@@ -176,6 +176,11 @@ static void v4l2_device_release(struct device *cd) {struct video_device *vdev =to_video_device (cd);struct v4l2_device *v4l2_dev = vdev->v4l2_dev; +bool v4l2_dev_has_release = v4l2_dev->release; +#ifdef CONFIG_MEDIA_CONTROLLER +struct media_device *mdev = v4l2_dev->mdev; +bool mdev_has_release = mdev && mdev->ops && mdev->ops->release; +#endif mutex_lock (&videodev_lock);if (WARN_ON (video_devices[vdev->minor] != vdev)) {@@ -198,8 +203,8 @@ static void v4l2_device_release(struct device *cd)mutex_unlock (&videodev_lock); -#if defined(CONFIG_MEDIA_CONTROLLER ) -if (v4l2_dev->mdev && vdev->vfl_dir !=VFL_DIR_M2M ) { +#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev && vdev->vfl_dir !=VFL_DIR_M2M ) {/* Remove interfaces and interface links */ media_devnode_remove (vdev->intf_devnode);if (vdev->entity.function !=MEDIA_ENT_F_UNKNOWN )@@ -207,23 +212,31 @@ static void v4l2_device_release(struct device *cd) }#endif -/* Do not call v4l2_device_put if there is no release callback set. -* Drivers that have no v4l2_device release callback might free the -* v4l2_dev instance in the video_device release callback below, so we -* must perform this check here. -* -* TODO: In the long run all drivers that use v4l2_device should use the -* v4l2_device release callback. This check will then be unnecessary. -*/ -if (v4l2_dev->release ==NULL ) - v4l2_dev =NULL ; -/* Release video_device and perform other cleanups as needed. */ vdev->release (vdev); -/* Decrease v4l2_device refcount */ -if (v4l2_dev)
drivers/media/v4l2-core/v4l2-dev.c static void v4l2_device_release(struct device *cd) · line 228
Quoted 10 lines
drivers/media/v4l2-core/v4l2-dev.c +10
-0+#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev) +media_device_put (mdev); + +/* +* Generally both struct media_device and struct v4l2_device are +* embedded in the same driver's context struct so having a release +* callback in both is a bug. +*/ +WARN_ON (v4l2_dev_has_release && mdev_has_release);
How about:
if (WARN_ON(v4l2_dev_has_release && mdev_has_release)) v4l2_dev_has_release = false;
drivers/media/v4l2-core/v4l2-dev.c static void v4l2_device_release(struct device *cd) · line 239
Quoted 11 lines
drivers/media/v4l2-core/v4l2-dev.c +11
-0+#endif + +/* +* Decrease v4l2_device refcount, but only if the media device doesn't +* have a release callback. +*/ +if (v4l2_dev_has_release +#ifdef CONFIG_MEDIA_CONTROLLER + && !mdev_has_release +#endif + )
Then this change is no longer needed.
General question: do we have drivers today that set both release functions? Because that would now cause a WARN in the kernel log with this patch.
Quoted 21 lines
drivers/media/v4l2-core/v4l2-dev.c +5
-2v4l2_device_put (v4l2_dev); }@@ -792,11 +805,14 @@ static int video_register_media_controller(struct video_device *vdev)u32 intf_type;int ret; -/* Memory-to-memory devices are more complex and use +/* +* Memory-to-memory devices are more complex and use * their own function to register its mc entities. */ -if (!vdev->v4l2_dev->mdev || vdev->vfl_dir ==VFL_DIR_M2M ) +if (!vdev->v4l2_dev->mdev || vdev->vfl_dir ==VFL_DIR_M2M ) { +media_device_get (vdev->v4l2_dev->mdev);return 0 ; + } vdev->entity.obj_type =MEDIA_ENTITY_TYPE_VIDEO_DEVICE ; vdev->entity.function =MEDIA_ENT_F_UNKNOWN ;
drivers/media/v4l2-core/v4l2-dev.c static int video_register_media_controller(struct video_device *vdev) · line 897
Quoted 7 lines
drivers/media/v4l2-core/v4l2-dev.c +1
-0@@ -875,6 +891,7 @@ static int video_register_media_controller(struct video_device *vdev)/* FIXME: how to create the other interface links? */ +media_device_get (vdev->v4l2_dev->mdev);#endif return 0 ; }
Regards,
Hans
Laurent Pinchart
maintainerRe: [PATCH v2 17/29] media: v4l: Acquire a reference to the media device for every video device
Quoted 7 lines · Hans Verkuil
Quoted 6 lines · Sakari Ailus
The video device depends on the existence of its media device --- if there is one. Acquire a reference to it.
Note that when the media device release callback is used, then the V4L2 device release callback is ignored and a warning is issued if both are set.
Why is that ? The two are distinct objects, why can't they both have a release function ?
Quoted 90 lines
Quoted 54 lines
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/v4l2-core/v4l2-dev.c | 51 ++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 17 deletions(-)drivers/media/v4l2-core/v4l2-dev.c +7
-15@@ -176,6 +176,11 @@ static void v4l2_device_release(struct device *cd) {struct video_device *vdev =to_video_device (cd);struct v4l2_device *v4l2_dev = vdev->v4l2_dev; +bool v4l2_dev_has_release = v4l2_dev->release; +#ifdef CONFIG_MEDIA_CONTROLLER +struct media_device *mdev = v4l2_dev->mdev; +bool mdev_has_release = mdev && mdev->ops && mdev->ops->release; +#endif mutex_lock (&videodev_lock);if (WARN_ON (video_devices[vdev->minor] != vdev)) {@@ -198,8 +203,8 @@ static void v4l2_device_release(struct device *cd)mutex_unlock (&videodev_lock); -#if defined(CONFIG_MEDIA_CONTROLLER ) -if (v4l2_dev->mdev && vdev->vfl_dir !=VFL_DIR_M2M ) { +#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev && vdev->vfl_dir !=VFL_DIR_M2M ) {/* Remove interfaces and interface links */ media_devnode_remove (vdev->intf_devnode);if (vdev->entity.function !=MEDIA_ENT_F_UNKNOWN )@@ -207,23 +212,31 @@ static void v4l2_device_release(struct device *cd) }#endif -/* Do not call v4l2_device_put if there is no release callback set. -* Drivers that have no v4l2_device release callback might free the -* v4l2_dev instance in the video_device release callback below, so we -* must perform this check here. -* -* TODO: In the long run all drivers that use v4l2_device should use the -* v4l2_device release callback. This check will then be unnecessary. -*/ -if (v4l2_dev->release ==NULL ) - v4l2_dev =NULL ; -/* Release video_device and perform other cleanups as needed. */ vdev->release (vdev); -/* Decrease v4l2_device refcount */ -if (v4l2_dev)drivers/media/v4l2-core/v4l2-dev.c static void v4l2_device_release(struct device *cd) · line 228
Quoted 10 lines
drivers/media/v4l2-core/v4l2-dev.c +10
-0+#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev) +media_device_put (mdev); + +/* +* Generally both struct media_device and struct v4l2_device are +* embedded in the same driver's context struct so having a release +* callback in both is a bug. +*/ +WARN_ON (v4l2_dev_has_release && mdev_has_release);How about:
if (WARN_ON(v4l2_dev_has_release && mdev_has_release)) v4l2_dev_has_release = false;drivers/media/v4l2-core/v4l2-dev.c static void v4l2_device_release(struct device *cd) · line 239
Quoted 11 lines
drivers/media/v4l2-core/v4l2-dev.c +11
-0+#endif + +/* +* Decrease v4l2_device refcount, but only if the media device doesn't +* have a release callback. +*/ +if (v4l2_dev_has_release +#ifdef CONFIG_MEDIA_CONTROLLER + && !mdev_has_release +#endif + )Then this change is no longer needed.
General question: do we have drivers today that set both release functions? Because that would now cause a WARN in the kernel log with this patch.
Quoted 3 lines
drivers/media/v4l2-core/v4l2-dev.c +0
-0v4l2_device_put (v4l2_dev); }
Quoted 8 lines
drivers/media/v4l2-core/v4l2-dev.c static int video_register_media_controller(struct video_device *vdev) · line 810
Quoted 8 lines
drivers/media/v4l2-core/v4l2-dev.c +2
-1@@ -792,11 +805,14 @@ static int video_register_media_controller(struct video_device *vdev)u32 intf_type;int ret; -/* Memory-to-memory devices are more complex and use +/* +* Memory-to-memory devices are more complex and use * their own function to register its mc entities.
If you fix the comment style as a drive-by change, you could as well reflow it to 80 columns.
Quoted 17 lines
Quoted 17 lines
drivers/media/v4l2-core/v4l2-dev.c +4
-1*/ -if (!vdev->v4l2_dev->mdev || vdev->vfl_dir ==VFL_DIR_M2M ) +if (!vdev->v4l2_dev->mdev || vdev->vfl_dir ==VFL_DIR_M2M ) { +media_device_get (vdev->v4l2_dev->mdev);return 0 ; + } vdev->entity.obj_type =MEDIA_ENTITY_TYPE_VIDEO_DEVICE ; vdev->entity.function =MEDIA_ENT_F_UNKNOWN ;@@ -875,6 +891,7 @@ static int video_register_media_controller(struct video_device *vdev)/* FIXME: how to create the other interface links? */ +media_device_get (vdev->v4l2_dev->mdev);#endif return 0 ; }
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerRe: [PATCH v2 17/29] media: v4l: Acquire a reference to the media device for every video device
Hi Laurent,
Thank you for reviewing the set!
Quoted 11 lines · Laurent Pinchart
Quoted 7 lines · Hans Verkuil
Quoted 6 lines · Sakari Ailus
The video device depends on the existence of its media device --- if there is one. Acquire a reference to it.
Note that when the media device release callback is used, then the V4L2 device release callback is ignored and a warning is issued if both are set.
Why is that ? The two are distinct objects, why can't they both have a release function ?
You could, in principle, but in practice both of the structs are part of the same driver's device context struct which is a single allocation. You can only have a single release callback for it.
Quoted 95 lines
Quoted 95 lines
Quoted 55 lines
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/v4l2-core/v4l2-dev.c | 51 ++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 17 deletions(-)drivers/media/v4l2-core/v4l2-dev.c +7
-15@@ -176,6 +176,11 @@ static void v4l2_device_release(struct device *cd) {struct video_device *vdev =to_video_device (cd);struct v4l2_device *v4l2_dev = vdev->v4l2_dev; +bool v4l2_dev_has_release = v4l2_dev->release; +#ifdef CONFIG_MEDIA_CONTROLLER +struct media_device *mdev = v4l2_dev->mdev; +bool mdev_has_release = mdev && mdev->ops && mdev->ops->release; +#endif mutex_lock (&videodev_lock);if (WARN_ON (video_devices[vdev->minor] != vdev)) {@@ -198,8 +203,8 @@ static void v4l2_device_release(struct device *cd)mutex_unlock (&videodev_lock); -#if defined(CONFIG_MEDIA_CONTROLLER ) -if (v4l2_dev->mdev && vdev->vfl_dir !=VFL_DIR_M2M ) { +#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev && vdev->vfl_dir !=VFL_DIR_M2M ) {/* Remove interfaces and interface links */ media_devnode_remove (vdev->intf_devnode);if (vdev->entity.function !=MEDIA_ENT_F_UNKNOWN )@@ -207,23 +212,31 @@ static void v4l2_device_release(struct device *cd) }#endif -/* Do not call v4l2_device_put if there is no release callback set. -* Drivers that have no v4l2_device release callback might free the -* v4l2_dev instance in the video_device release callback below, so we -* must perform this check here. -* -* TODO: In the long run all drivers that use v4l2_device should use the -* v4l2_device release callback. This check will then be unnecessary. -*/ -if (v4l2_dev->release ==NULL ) - v4l2_dev =NULL ; -/* Release video_device and perform other cleanups as needed. */ vdev->release (vdev); -/* Decrease v4l2_device refcount */ -if (v4l2_dev)drivers/media/v4l2-core/v4l2-dev.c static void v4l2_device_release(struct device *cd) · line 228
Quoted 10 lines
drivers/media/v4l2-core/v4l2-dev.c +10
-0+#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev) +media_device_put (mdev); + +/* +* Generally both struct media_device and struct v4l2_device are +* embedded in the same driver's context struct so having a release +* callback in both is a bug. +*/ +WARN_ON (v4l2_dev_has_release && mdev_has_release);How about:
if (WARN_ON(v4l2_dev_has_release && mdev_has_release)) v4l2_dev_has_release = false;drivers/media/v4l2-core/v4l2-dev.c static void v4l2_device_release(struct device *cd) · line 239
Quoted 11 lines
drivers/media/v4l2-core/v4l2-dev.c +11
-0+#endif + +/* +* Decrease v4l2_device refcount, but only if the media device doesn't +* have a release callback. +*/ +if (v4l2_dev_has_release +#ifdef CONFIG_MEDIA_CONTROLLER + && !mdev_has_release +#endif + )Then this change is no longer needed.
General question: do we have drivers today that set both release functions? Because that would now cause a WARN in the kernel log with this patch.
Quoted 7 lines
drivers/media/v4l2-core/v4l2-dev.c +0
-0v4l2_device_put (v4l2_dev); }@@ -792,11 +805,14 @@ static int video_register_media_controller(struct video_device *vdev)u32 intf_type;int ret;
Quoted 7 lines
Quoted 4 lines
drivers/media/v4l2-core/v4l2-dev.c static int video_register_media_controller(struct video_device *vdev) · line 810
Quoted 4 lines
drivers/media/v4l2-core/v4l2-dev.c +2
-1-/* Memory-to-memory devices are more complex and use +/* +* Memory-to-memory devices are more complex and use * their own function to register its mc entities. If you fix the comment style as a drive-by change, you could as well reflow it to 80 columns.
I'll update this for the next version.
Quoted 19 lines
Quoted 18 lines
Quoted 18 lines
drivers/media/v4l2-core/v4l2-dev.c +4
-1*/ -if (!vdev->v4l2_dev->mdev || vdev->vfl_dir ==VFL_DIR_M2M ) +if (!vdev->v4l2_dev->mdev || vdev->vfl_dir ==VFL_DIR_M2M ) { +media_device_get (vdev->v4l2_dev->mdev);return 0 ; + } vdev->entity.obj_type =MEDIA_ENTITY_TYPE_VIDEO_DEVICE ; vdev->entity.function =MEDIA_ENT_F_UNKNOWN ;@@ -875,6 +891,7 @@ static int video_register_media_controller(struct video_device *vdev)/* FIXME: how to create the other interface links? */ +media_device_get (vdev->v4l2_dev->mdev);#endif return 0 ; }
Signature · 3 lines
Regards, Sakari Ailus
Laurent Pinchart
maintainerRe: [PATCH v2 17/29] media: v4l: Acquire a reference to the media device for every video device
Quoted 14 lines · Sakari Ailus
Hi Laurent,
Thank you for reviewing the set!
Quoted 9 lines · Laurent Pinchart
Quoted 7 lines · Hans Verkuil
Quoted 6 lines · Sakari Ailus
The video device depends on the existence of its media device --- if there is one. Acquire a reference to it.
Note that when the media device release callback is used, then the V4L2 device release callback is ignored and a warning is issued if both are set.
Quoted 6 lines
Quoted 2 lines
Why is that ? The two are distinct objects, why can't they both have a release function ?
You could, in principle, but in practice both of the structs are part of the same driver's device context struct which is a single allocation. You can only have a single release callback for it.
If both release callbacks freed the same data structure, that would indeed be a problem. There could be other use cases though. For instance, in the uvcvideo driver, the top-level structure is reference-counted, and the release callbacks of the video devices decrement that reference count. I don't expect drivers to do something similar with media_device and v4l2_device, but I'm not sure if we should forbid it completely. If we do, I would then rather deprecate the release callback of v4l2_device completely.
Quoted 121 lines
Quoted 94 lines
Quoted 94 lines
Quoted 54 lines
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/v4l2-core/v4l2-dev.c | 51 ++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 17 deletions(-)drivers/media/v4l2-core/v4l2-dev.c +7
-15@@ -176,6 +176,11 @@ static void v4l2_device_release(struct device *cd) {struct video_device *vdev =to_video_device (cd);struct v4l2_device *v4l2_dev = vdev->v4l2_dev; +bool v4l2_dev_has_release = v4l2_dev->release; +#ifdef CONFIG_MEDIA_CONTROLLER +struct media_device *mdev = v4l2_dev->mdev; +bool mdev_has_release = mdev && mdev->ops && mdev->ops->release; +#endif mutex_lock (&videodev_lock);if (WARN_ON (video_devices[vdev->minor] != vdev)) {@@ -198,8 +203,8 @@ static void v4l2_device_release(struct device *cd)mutex_unlock (&videodev_lock); -#if defined(CONFIG_MEDIA_CONTROLLER ) -if (v4l2_dev->mdev && vdev->vfl_dir !=VFL_DIR_M2M ) { +#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev && vdev->vfl_dir !=VFL_DIR_M2M ) {/* Remove interfaces and interface links */ media_devnode_remove (vdev->intf_devnode);if (vdev->entity.function !=MEDIA_ENT_F_UNKNOWN )@@ -207,23 +212,31 @@ static void v4l2_device_release(struct device *cd) }#endif -/* Do not call v4l2_device_put if there is no release callback set. -* Drivers that have no v4l2_device release callback might free the -* v4l2_dev instance in the video_device release callback below, so we -* must perform this check here. -* -* TODO: In the long run all drivers that use v4l2_device should use the -* v4l2_device release callback. This check will then be unnecessary. -*/ -if (v4l2_dev->release ==NULL ) - v4l2_dev =NULL ; -/* Release video_device and perform other cleanups as needed. */ vdev->release (vdev); -/* Decrease v4l2_device refcount */ -if (v4l2_dev)drivers/media/v4l2-core/v4l2-dev.c static void v4l2_device_release(struct device *cd) · line 228
Quoted 10 lines
drivers/media/v4l2-core/v4l2-dev.c +10
-0+#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev) +media_device_put (mdev); + +/* +* Generally both struct media_device and struct v4l2_device are +* embedded in the same driver's context struct so having a release +* callback in both is a bug. +*/ +WARN_ON (v4l2_dev_has_release && mdev_has_release);How about:
if (WARN_ON(v4l2_dev_has_release && mdev_has_release)) v4l2_dev_has_release = false;drivers/media/v4l2-core/v4l2-dev.c static void v4l2_device_release(struct device *cd) · line 239
Quoted 11 lines
drivers/media/v4l2-core/v4l2-dev.c +11
-0+#endif + +/* +* Decrease v4l2_device refcount, but only if the media device doesn't +* have a release callback. +*/ +if (v4l2_dev_has_release +#ifdef CONFIG_MEDIA_CONTROLLER + && !mdev_has_release +#endif + )Then this change is no longer needed.
General question: do we have drivers today that set both release functions? Because that would now cause a WARN in the kernel log with this patch.
Quoted 7 lines
drivers/media/v4l2-core/v4l2-dev.c +0
-0v4l2_device_put (v4l2_dev); }@@ -792,11 +805,14 @@ static int video_register_media_controller(struct video_device *vdev)u32 intf_type;int ret;Quoted 7 lines
Quoted 4 lines
drivers/media/v4l2-core/v4l2-dev.c static int video_register_media_controller(struct video_device *vdev) · line 810
Quoted 4 lines
drivers/media/v4l2-core/v4l2-dev.c +2
-1-/* Memory-to-memory devices are more complex and use +/* +* Memory-to-memory devices are more complex and use * their own function to register its mc entities. If you fix the comment style as a drive-by change, you could as well reflow it to 80 columns.
I'll update this for the next version.
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
drivers/media/v4l2-core/v4l2-dev.c +4
-1*/ -if (!vdev->v4l2_dev->mdev || vdev->vfl_dir ==VFL_DIR_M2M ) +if (!vdev->v4l2_dev->mdev || vdev->vfl_dir ==VFL_DIR_M2M ) { +media_device_get (vdev->v4l2_dev->mdev);return 0 ; + } vdev->entity.obj_type =MEDIA_ENTITY_TYPE_VIDEO_DEVICE ; vdev->entity.function =MEDIA_ENT_F_UNKNOWN ;@@ -875,6 +891,7 @@ static int video_register_media_controller(struct video_device *vdev)/* FIXME: how to create the other interface links? */ +media_device_get (vdev->v4l2_dev->mdev);#endif return 0 ; }
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerRe: [PATCH v2 17/29] media: v4l: Acquire a reference to the media device for every video device
Hi Laurent,
Quoted 22 lines · Laurent Pinchart
Quoted 14 lines · Sakari Ailus
Hi Laurent,
Thank you for reviewing the set!
Quoted 9 lines · Laurent Pinchart
Quoted 7 lines · Hans Verkuil
Quoted 6 lines · Sakari Ailus
The video device depends on the existence of its media device --- if there is one. Acquire a reference to it.
Note that when the media device release callback is used, then the V4L2 device release callback is ignored and a warning is issued if both are set.
Quoted 6 lines
Quoted 2 lines
Why is that ? The two are distinct objects, why can't they both have a release function ?
You could, in principle, but in practice both of the structs are part of the same driver's device context struct which is a single allocation. You can only have a single release callback for it.
Quoted 8 lines
If both release callbacks freed the same data structure, that would indeed be a problem. There could be other use cases though. For instance, in the uvcvideo driver, the top-level structure is reference-counted, and the release callbacks of the video devices decrement that reference count. I don't expect drivers to do something similar with
media_deviceandv4l2_device, but I'm not sure if we should forbid it completely. If we do, I would then rather deprecate the release callback ofv4l2_devicecompletely.
There are quite a few drivers using the struct v4l2_device's release callback, it seems. We very probably can't get rid of it except possibly in the long term.
The documentation already takes this into account, see patch "media:
Documentation: Document how Media device resources are released".
Signature · 3 lines
Regards, Sakari Ailus
Sakari Ailus
maintainerRe: [PATCH v2 17/29] media: v4l: Acquire a reference to the media device for every video device
In reply to Hans VerkuilHi Hans,
Many thanks for reviewing these.
Quoted 77 lines · Hans Verkuil
Quoted 60 lines · Sakari Ailus
The video device depends on the existence of its media device --- if there is one. Acquire a reference to it.
Note that when the media device release callback is used, then the V4L2 device release callback is ignored and a warning is issued if both are set.
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/v4l2-core/v4l2-dev.c | 51 ++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 17 deletions(-)drivers/media/v4l2-core/v4l2-dev.c +7
-15@@ -176,6 +176,11 @@ static void v4l2_device_release(struct device *cd) {struct video_device *vdev =to_video_device (cd);struct v4l2_device *v4l2_dev = vdev->v4l2_dev; +bool v4l2_dev_has_release = v4l2_dev->release; +#ifdef CONFIG_MEDIA_CONTROLLER +struct media_device *mdev = v4l2_dev->mdev; +bool mdev_has_release = mdev && mdev->ops && mdev->ops->release; +#endif mutex_lock (&videodev_lock);if (WARN_ON (video_devices[vdev->minor] != vdev)) {@@ -198,8 +203,8 @@ static void v4l2_device_release(struct device *cd)mutex_unlock (&videodev_lock); -#if defined(CONFIG_MEDIA_CONTROLLER ) -if (v4l2_dev->mdev && vdev->vfl_dir !=VFL_DIR_M2M ) { +#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev && vdev->vfl_dir !=VFL_DIR_M2M ) {/* Remove interfaces and interface links */ media_devnode_remove (vdev->intf_devnode);if (vdev->entity.function !=MEDIA_ENT_F_UNKNOWN )@@ -207,23 +212,31 @@ static void v4l2_device_release(struct device *cd) }#endif -/* Do not call v4l2_device_put if there is no release callback set. -* Drivers that have no v4l2_device release callback might free the -* v4l2_dev instance in the video_device release callback below, so we -* must perform this check here. -* -* TODO: In the long run all drivers that use v4l2_device should use the -* v4l2_device release callback. This check will then be unnecessary. -*/ -if (v4l2_dev->release ==NULL ) - v4l2_dev =NULL ; -/* Release video_device and perform other cleanups as needed. */ vdev->release (vdev); -/* Decrease v4l2_device refcount */ -if (v4l2_dev)drivers/media/v4l2-core/v4l2-dev.c static void v4l2_device_release(struct device *cd) · line 228
Quoted 10 lines
drivers/media/v4l2-core/v4l2-dev.c +10
-0+#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev) +media_device_put (mdev); + +/* +* Generally both struct media_device and struct v4l2_device are +* embedded in the same driver's context struct so having a release +* callback in both is a bug. +*/ +WARN_ON (v4l2_dev_has_release && mdev_has_release);How about:
if (WARN_ON(v4l2_dev_has_release && mdev_has_release)) v4l2_dev_has_release = false;
Quoted 13 lines
drivers/media/v4l2-core/v4l2-dev.c static void v4l2_device_release(struct device *cd) · line 239
Quoted 11 lines
drivers/media/v4l2-core/v4l2-dev.c +11
-0+#endif + +/* +* Decrease v4l2_device refcount, but only if the media device doesn't +* have a release callback. +*/ +if (v4l2_dev_has_release +#ifdef CONFIG_MEDIA_CONTROLLER + && !mdev_has_release +#endif + )Then this change is no longer needed.
Good idea.
I'll also rename v4l2_dev_has_release as v4l2_dev_call_release.
Quoted 3 lines
General question: do we have drivers today that set both release functions? Because that would now cause a WARN in the kernel log with this patch.
Indeed, the intention is to be vocal about it.
The only user of the v4l2_device release function I could find is drivers/media/radio/dsbr100.c . I may have missed some but it certainly isn't commonly used. Maybe we could try to drop refcounting from v4l2_device later on?
Quoted 29 lines
Quoted 29 lines
drivers/media/v4l2-core/v4l2-dev.c +6
-2v4l2_device_put (v4l2_dev); }@@ -792,11 +805,14 @@ static int video_register_media_controller(struct video_device *vdev)u32 intf_type;int ret; -/* Memory-to-memory devices are more complex and use +/* +* Memory-to-memory devices are more complex and use * their own function to register its mc entities. */ -if (!vdev->v4l2_dev->mdev || vdev->vfl_dir ==VFL_DIR_M2M ) +if (!vdev->v4l2_dev->mdev || vdev->vfl_dir ==VFL_DIR_M2M ) { +media_device_get (vdev->v4l2_dev->mdev);return 0 ; + } vdev->entity.obj_type =MEDIA_ENTITY_TYPE_VIDEO_DEVICE ; vdev->entity.function =MEDIA_ENT_F_UNKNOWN ;@@ -875,6 +891,7 @@ static int video_register_media_controller(struct video_device *vdev)/* FIXME: how to create the other interface links? */ +media_device_get (vdev->v4l2_dev->mdev);#endif return 0 ; }
Signature · 3 lines
Kind regards, Sakari Ailus
Re: [PATCH v2 17/29] media: v4l: Acquire a reference to the media device for every video device
Quoted 104 lines · Sakari Ailus
Hi Hans,
Many thanks for reviewing these.
Quoted 77 lines · Hans Verkuil
Quoted 60 lines · Sakari Ailus
The video device depends on the existence of its media device --- if there is one. Acquire a reference to it.
Note that when the media device release callback is used, then the V4L2 device release callback is ignored and a warning is issued if both are set.
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/v4l2-core/v4l2-dev.c | 51 ++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 17 deletions(-)drivers/media/v4l2-core/v4l2-dev.c +7
-15@@ -176,6 +176,11 @@ static void v4l2_device_release(struct device *cd) {struct video_device *vdev =to_video_device (cd);struct v4l2_device *v4l2_dev = vdev->v4l2_dev; +bool v4l2_dev_has_release = v4l2_dev->release; +#ifdef CONFIG_MEDIA_CONTROLLER +struct media_device *mdev = v4l2_dev->mdev; +bool mdev_has_release = mdev && mdev->ops && mdev->ops->release; +#endif mutex_lock (&videodev_lock);if (WARN_ON (video_devices[vdev->minor] != vdev)) {@@ -198,8 +203,8 @@ static void v4l2_device_release(struct device *cd)mutex_unlock (&videodev_lock); -#if defined(CONFIG_MEDIA_CONTROLLER ) -if (v4l2_dev->mdev && vdev->vfl_dir !=VFL_DIR_M2M ) { +#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev && vdev->vfl_dir !=VFL_DIR_M2M ) {/* Remove interfaces and interface links */ media_devnode_remove (vdev->intf_devnode);if (vdev->entity.function !=MEDIA_ENT_F_UNKNOWN )@@ -207,23 +212,31 @@ static void v4l2_device_release(struct device *cd) }#endif -/* Do not call v4l2_device_put if there is no release callback set. -* Drivers that have no v4l2_device release callback might free the -* v4l2_dev instance in the video_device release callback below, so we -* must perform this check here. -* -* TODO: In the long run all drivers that use v4l2_device should use the -* v4l2_device release callback. This check will then be unnecessary. -*/ -if (v4l2_dev->release ==NULL ) - v4l2_dev =NULL ; -/* Release video_device and perform other cleanups as needed. */ vdev->release (vdev); -/* Decrease v4l2_device refcount */ -if (v4l2_dev)drivers/media/v4l2-core/v4l2-dev.c static void v4l2_device_release(struct device *cd) · line 228
Quoted 10 lines
drivers/media/v4l2-core/v4l2-dev.c +10
-0+#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev) +media_device_put (mdev); + +/* +* Generally both struct media_device and struct v4l2_device are +* embedded in the same driver's context struct so having a release +* callback in both is a bug. +*/ +WARN_ON (v4l2_dev_has_release && mdev_has_release);How about:
if (WARN_ON(v4l2_dev_has_release && mdev_has_release)) v4l2_dev_has_release = false;Quoted 13 lines
drivers/media/v4l2-core/v4l2-dev.c static void v4l2_device_release(struct device *cd) · line 239
Quoted 11 lines
drivers/media/v4l2-core/v4l2-dev.c +11
-0+#endif + +/* +* Decrease v4l2_device refcount, but only if the media device doesn't +* have a release callback. +*/ +if (v4l2_dev_has_release +#ifdef CONFIG_MEDIA_CONTROLLER + && !mdev_has_release +#endif + )Then this change is no longer needed.
Good idea.
I'll also rename
v4l2_dev_has_releaseasv4l2_dev_call_release.Quoted 3 lines
General question: do we have drivers today that set both release functions? Because that would now cause a WARN in the kernel log with this patch.
Quoted 6 lines
Indeed, the intention is to be vocal about it.
The only user of the
v4l2_devicerelease function I could find isdrivers/media/radio/dsbr100.c. I may have missed some but it certainly isn't commonly used. Maybe we could try to drop refcounting fromv4l2_devicelater on?
There are a lot more drivers that use this. A quick grep shows gspca, hackrf, usbtv, pwc, au0828 and more.
git grep v4l2_dev.*release.*= drivers/media/
Currently it is the only way to properly release drivers that create multiple video (or other) devices.
Regards,
Hans
Quoted 31 lines
Quoted 30 lines
Quoted 30 lines
drivers/media/v4l2-core/v4l2-dev.c +6
-2v4l2_device_put (v4l2_dev); }@@ -792,11 +805,14 @@ static int video_register_media_controller(struct video_device *vdev)u32 intf_type;int ret; -/* Memory-to-memory devices are more complex and use +/* +* Memory-to-memory devices are more complex and use * their own function to register its mc entities. */ -if (!vdev->v4l2_dev->mdev || vdev->vfl_dir ==VFL_DIR_M2M ) +if (!vdev->v4l2_dev->mdev || vdev->vfl_dir ==VFL_DIR_M2M ) { +media_device_get (vdev->v4l2_dev->mdev);return 0 ; + } vdev->entity.obj_type =MEDIA_ENTITY_TYPE_VIDEO_DEVICE ; vdev->entity.function =MEDIA_ENT_F_UNKNOWN ;@@ -875,6 +891,7 @@ static int video_register_media_controller(struct video_device *vdev)/* FIXME: how to create the other interface links? */ +media_device_get (vdev->v4l2_dev->mdev);#endif return 0 ; }
Sakari Ailus
maintainerRe: [PATCH v2 17/29] media: v4l: Acquire a reference to the media device for every video device
Hi Hans,
Quoted 112 lines · Hans Verkuil
Quoted 104 lines · Sakari Ailus
Hi Hans,
Many thanks for reviewing these.
Quoted 77 lines · Hans Verkuil
Quoted 60 lines · Sakari Ailus
The video device depends on the existence of its media device --- if there is one. Acquire a reference to it.
Note that when the media device release callback is used, then the V4L2 device release callback is ignored and a warning is issued if both are set.
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/v4l2-core/v4l2-dev.c | 51 ++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 17 deletions(-)drivers/media/v4l2-core/v4l2-dev.c +7
-15@@ -176,6 +176,11 @@ static void v4l2_device_release(struct device *cd) {struct video_device *vdev =to_video_device (cd);struct v4l2_device *v4l2_dev = vdev->v4l2_dev; +bool v4l2_dev_has_release = v4l2_dev->release; +#ifdef CONFIG_MEDIA_CONTROLLER +struct media_device *mdev = v4l2_dev->mdev; +bool mdev_has_release = mdev && mdev->ops && mdev->ops->release; +#endif mutex_lock (&videodev_lock);if (WARN_ON (video_devices[vdev->minor] != vdev)) {@@ -198,8 +203,8 @@ static void v4l2_device_release(struct device *cd)mutex_unlock (&videodev_lock); -#if defined(CONFIG_MEDIA_CONTROLLER ) -if (v4l2_dev->mdev && vdev->vfl_dir !=VFL_DIR_M2M ) { +#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev && vdev->vfl_dir !=VFL_DIR_M2M ) {/* Remove interfaces and interface links */ media_devnode_remove (vdev->intf_devnode);if (vdev->entity.function !=MEDIA_ENT_F_UNKNOWN )@@ -207,23 +212,31 @@ static void v4l2_device_release(struct device *cd) }#endif -/* Do not call v4l2_device_put if there is no release callback set. -* Drivers that have no v4l2_device release callback might free the -* v4l2_dev instance in the video_device release callback below, so we -* must perform this check here. -* -* TODO: In the long run all drivers that use v4l2_device should use the -* v4l2_device release callback. This check will then be unnecessary. -*/ -if (v4l2_dev->release ==NULL ) - v4l2_dev =NULL ; -/* Release video_device and perform other cleanups as needed. */ vdev->release (vdev); -/* Decrease v4l2_device refcount */ -if (v4l2_dev)drivers/media/v4l2-core/v4l2-dev.c static void v4l2_device_release(struct device *cd) · line 228
Quoted 10 lines
drivers/media/v4l2-core/v4l2-dev.c +10
-0+#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev) +media_device_put (mdev); + +/* +* Generally both struct media_device and struct v4l2_device are +* embedded in the same driver's context struct so having a release +* callback in both is a bug. +*/ +WARN_ON (v4l2_dev_has_release && mdev_has_release);How about:
if (WARN_ON(v4l2_dev_has_release && mdev_has_release)) v4l2_dev_has_release = false;Quoted 13 lines
drivers/media/v4l2-core/v4l2-dev.c static void v4l2_device_release(struct device *cd) · line 239
Quoted 11 lines
drivers/media/v4l2-core/v4l2-dev.c +11
-0+#endif + +/* +* Decrease v4l2_device refcount, but only if the media device doesn't +* have a release callback. +*/ +if (v4l2_dev_has_release +#ifdef CONFIG_MEDIA_CONTROLLER + && !mdev_has_release +#endif + )Then this change is no longer needed.
Good idea.
I'll also rename
v4l2_dev_has_releaseasv4l2_dev_call_release.Quoted 3 lines
General question: do we have drivers today that set both release functions? Because that would now cause a WARN in the kernel log with this patch.
Quoted 6 lines
Indeed, the intention is to be vocal about it.
The only user of the
v4l2_devicerelease function I could find isdrivers/media/radio/dsbr100.c. I may have missed some but it certainly isn't commonly used. Maybe we could try to drop refcounting fromv4l2_devicelater on?
Quoted 7 lines
There are a lot more drivers that use this. A quick grep shows gspca, hackrf, usbtv, pwc, au0828 and more.
git grep v4l2_dev.*release.*=
drivers/media/Currently it is the only way to properly release drivers that create multiple video (or other) devices.
I mistakenly grepped for ->release, .release is actually more common. I'll check how this is currently being used.
Signature · 3 lines
Regards, Sakari Ailus
Sakari Ailus
maintainerRe: [PATCH v2 17/29] media: v4l: Acquire a reference to the media device for every video device
Hi Hans,
Quoted 118 lines · Sakari Ailus
Hi Hans,
Quoted 115 lines · Hans Verkuil
Quoted 104 lines · Sakari Ailus
Hi Hans,
Many thanks for reviewing these.
Quoted 77 lines · Hans Verkuil
Quoted 60 lines · Sakari Ailus
The video device depends on the existence of its media device --- if there is one. Acquire a reference to it.
Note that when the media device release callback is used, then the V4L2 device release callback is ignored and a warning is issued if both are set.
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/v4l2-core/v4l2-dev.c | 51 ++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 17 deletions(-)drivers/media/v4l2-core/v4l2-dev.c +7
-15@@ -176,6 +176,11 @@ static void v4l2_device_release(struct device *cd) {struct video_device *vdev =to_video_device (cd);struct v4l2_device *v4l2_dev = vdev->v4l2_dev; +bool v4l2_dev_has_release = v4l2_dev->release; +#ifdef CONFIG_MEDIA_CONTROLLER +struct media_device *mdev = v4l2_dev->mdev; +bool mdev_has_release = mdev && mdev->ops && mdev->ops->release; +#endif mutex_lock (&videodev_lock);if (WARN_ON (video_devices[vdev->minor] != vdev)) {@@ -198,8 +203,8 @@ static void v4l2_device_release(struct device *cd)mutex_unlock (&videodev_lock); -#if defined(CONFIG_MEDIA_CONTROLLER ) -if (v4l2_dev->mdev && vdev->vfl_dir !=VFL_DIR_M2M ) { +#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev && vdev->vfl_dir !=VFL_DIR_M2M ) {/* Remove interfaces and interface links */ media_devnode_remove (vdev->intf_devnode);if (vdev->entity.function !=MEDIA_ENT_F_UNKNOWN )@@ -207,23 +212,31 @@ static void v4l2_device_release(struct device *cd) }#endif -/* Do not call v4l2_device_put if there is no release callback set. -* Drivers that have no v4l2_device release callback might free the -* v4l2_dev instance in the video_device release callback below, so we -* must perform this check here. -* -* TODO: In the long run all drivers that use v4l2_device should use the -* v4l2_device release callback. This check will then be unnecessary. -*/ -if (v4l2_dev->release ==NULL ) - v4l2_dev =NULL ; -/* Release video_device and perform other cleanups as needed. */ vdev->release (vdev); -/* Decrease v4l2_device refcount */ -if (v4l2_dev)drivers/media/v4l2-core/v4l2-dev.c static void v4l2_device_release(struct device *cd) · line 228
Quoted 10 lines
drivers/media/v4l2-core/v4l2-dev.c +10
-0+#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev) +media_device_put (mdev); + +/* +* Generally both struct media_device and struct v4l2_device are +* embedded in the same driver's context struct so having a release +* callback in both is a bug. +*/ +WARN_ON (v4l2_dev_has_release && mdev_has_release);How about:
if (WARN_ON(v4l2_dev_has_release && mdev_has_release)) v4l2_dev_has_release = false;Quoted 13 lines
drivers/media/v4l2-core/v4l2-dev.c static void v4l2_device_release(struct device *cd) · line 239
Quoted 11 lines
drivers/media/v4l2-core/v4l2-dev.c +11
-0+#endif + +/* +* Decrease v4l2_device refcount, but only if the media device doesn't +* have a release callback. +*/ +if (v4l2_dev_has_release +#ifdef CONFIG_MEDIA_CONTROLLER + && !mdev_has_release +#endif + )Then this change is no longer needed.
Good idea.
I'll also rename
v4l2_dev_has_releaseasv4l2_dev_call_release.Quoted 3 lines
General question: do we have drivers today that set both release functions? Because that would now cause a WARN in the kernel log with this patch.
Quoted 6 lines
Indeed, the intention is to be vocal about it.
The only user of the
v4l2_devicerelease function I could find isdrivers/media/radio/dsbr100.c. I may have missed some but it certainly isn't commonly used. Maybe we could try to drop refcounting fromv4l2_devicelater on?There are a lot more drivers that use this. A quick grep shows gspca, hackrf, usbtv, pwc, au0828 and more.
Quoted 7 lines
Quoted 4 lines
git grep v4l2_dev.*release.*=
drivers/media/Currently it is the only way to properly release drivers that create multiple video (or other) devices.
I mistakenly grepped for ->release, .release is actually more common. I'll check how this is currently being used.
Getting back to the topic---indeed the V4L2 device release function is used by a number of drivers today. Moving to the Media device release function is no small task: I checked some drivers and while releasing the resources is centralised in this case, unregistering the interfaces and releasing actual resources may be intertwined so that fixing this requires reworking much of the driver code. It's better to leave this for driver authors or at least someone who has the hardware.
Signature · 1 line
Sakari Ailus
Re: [PATCH v2 17/29] media: v4l: Acquire a reference to the media device for every video device
Quoted 129 lines · Sakari Ailus
Hi Hans,
Quoted 118 lines · Sakari Ailus
Hi Hans,
Quoted 115 lines · Hans Verkuil
Quoted 104 lines · Sakari Ailus
Hi Hans,
Many thanks for reviewing these.
Quoted 77 lines · Hans Verkuil
Quoted 60 lines · Sakari Ailus
The video device depends on the existence of its media device --- if there is one. Acquire a reference to it.
Note that when the media device release callback is used, then the V4L2 device release callback is ignored and a warning is issued if both are set.
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/v4l2-core/v4l2-dev.c | 51 ++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 17 deletions(-)drivers/media/v4l2-core/v4l2-dev.c +7
-15@@ -176,6 +176,11 @@ static void v4l2_device_release(struct device *cd) {struct video_device *vdev =to_video_device (cd);struct v4l2_device *v4l2_dev = vdev->v4l2_dev; +bool v4l2_dev_has_release = v4l2_dev->release; +#ifdef CONFIG_MEDIA_CONTROLLER +struct media_device *mdev = v4l2_dev->mdev; +bool mdev_has_release = mdev && mdev->ops && mdev->ops->release; +#endif mutex_lock (&videodev_lock);if (WARN_ON (video_devices[vdev->minor] != vdev)) {@@ -198,8 +203,8 @@ static void v4l2_device_release(struct device *cd)mutex_unlock (&videodev_lock); -#if defined(CONFIG_MEDIA_CONTROLLER ) -if (v4l2_dev->mdev && vdev->vfl_dir !=VFL_DIR_M2M ) { +#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev && vdev->vfl_dir !=VFL_DIR_M2M ) {/* Remove interfaces and interface links */ media_devnode_remove (vdev->intf_devnode);if (vdev->entity.function !=MEDIA_ENT_F_UNKNOWN )@@ -207,23 +212,31 @@ static void v4l2_device_release(struct device *cd) }#endif -/* Do not call v4l2_device_put if there is no release callback set. -* Drivers that have no v4l2_device release callback might free the -* v4l2_dev instance in the video_device release callback below, so we -* must perform this check here. -* -* TODO: In the long run all drivers that use v4l2_device should use the -* v4l2_device release callback. This check will then be unnecessary. -*/ -if (v4l2_dev->release ==NULL ) - v4l2_dev =NULL ; -/* Release video_device and perform other cleanups as needed. */ vdev->release (vdev); -/* Decrease v4l2_device refcount */ -if (v4l2_dev)drivers/media/v4l2-core/v4l2-dev.c static void v4l2_device_release(struct device *cd) · line 228
Quoted 10 lines
drivers/media/v4l2-core/v4l2-dev.c +10
-0+#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev) +media_device_put (mdev); + +/* +* Generally both struct media_device and struct v4l2_device are +* embedded in the same driver's context struct so having a release +* callback in both is a bug. +*/ +WARN_ON (v4l2_dev_has_release && mdev_has_release);How about:
if (WARN_ON(v4l2_dev_has_release && mdev_has_release)) v4l2_dev_has_release = false;Quoted 13 lines
drivers/media/v4l2-core/v4l2-dev.c static void v4l2_device_release(struct device *cd) · line 239
Quoted 11 lines
drivers/media/v4l2-core/v4l2-dev.c +11
-0+#endif + +/* +* Decrease v4l2_device refcount, but only if the media device doesn't +* have a release callback. +*/ +if (v4l2_dev_has_release +#ifdef CONFIG_MEDIA_CONTROLLER + && !mdev_has_release +#endif + )Then this change is no longer needed.
Good idea.
I'll also rename
v4l2_dev_has_releaseasv4l2_dev_call_release.Quoted 3 lines
General question: do we have drivers today that set both release functions? Because that would now cause a WARN in the kernel log with this patch.
Quoted 6 lines
Indeed, the intention is to be vocal about it.
The only user of the
v4l2_devicerelease function I could find isdrivers/media/radio/dsbr100.c. I may have missed some but it certainly isn't commonly used. Maybe we could try to drop refcounting fromv4l2_devicelater on?There are a lot more drivers that use this. A quick grep shows gspca, hackrf, usbtv, pwc, au0828 and more.
Quoted 7 lines
Quoted 4 lines
git grep v4l2_dev.*release.*=
drivers/media/Currently it is the only way to properly release drivers that create multiple video (or other) devices.
I mistakenly grepped for ->release, .release is actually more common. I'll check how this is currently being used.
Quoted 8 lines
Getting back to the topic---indeed the V4L2 device release function is used by a number of drivers today. Moving to the Media device release function is no small task: I checked some drivers and while releasing the resources is centralised in this case, unregistering the interfaces and releasing actual resources may be intertwined so that fixing this requires reworking much of the driver code. It's better to leave this for driver authors or at least someone who has the hardware.
Indeed. Most if not all of these drivers do not create a media device anyway. Now, if you see drivers that have a media device AND use the v4l2_device release callback, then let me know. If I have the hardware, then I can try and switch it to using the media device callback since that would make more sense.
Regards,
Hans
Sakari Ailus
maintainerPATCH v2 18/29 media: mc: Postpone graph object removal until free
In reply to Sakari AilusThe media device itself will be unregistered based on it being unbound and driver's remove callback being called. The graph objects themselves may still be in use; rely on the media device release callback to release them.
--- drivers/media/mc/mc-device.c | 53 +++++++++++++++++------------------- 1 file changed, 25 insertions(+), 28 deletions(-)
drivers/media/mc/mc-device.c +25 -28
@@ -702,8 +702,33 @@ EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify);static void __media_device_release (struct media_device *mdev) { +struct media_entity *entity; +struct media_entity *next; +struct media_interface *intf, *tmp_intf; +struct media_entity_notify *notify, *nextp; +dev_dbg (mdev->dev,"Media device released\n" ); +/* Remove all entities from the media device */ +list_for_each_entry_safe (entity, next, &mdev->entities, graph_obj.list) +__media_device_unregister_entity (entity); + +/* Remove all entity_notify callbacks from the media device */ +list_for_each_entry_safe (notify, nextp, &mdev->entity_notify, list) +__media_device_unregister_entity_notify (mdev, notify); + +/* Remove all interfaces from the media device */ +list_for_each_entry_safe (intf, tmp_intf, &mdev->interfaces, + graph_obj.list) { +/* +* Unlink the interface, but don't free it here; the +* module which created it is responsible for freeing +* it +*/ +__media_remove_intf_links (intf); +media_gobj_destroy (&intf->graph_obj); + } +ida_destroy (&mdev->entity_internal_idx); mdev->entity_internal_idx_max =0 ;media_graph_walk_cleanup (&mdev->pm_count_walk);
@@ -787,42 +812,14 @@ EXPORT_SYMBOL_GPL(__media_device_register);void media_device_unregister (struct media_device *mdev) { -struct media_entity *entity; -struct media_entity *next; -struct media_interface *intf, *tmp_intf; -struct media_entity_notify *notify, *nextp; -if (mdev ==NULL )return ;mutex_lock (&mdev->graph_mutex); - -/* Check if mdev was ever registered at all */ if (!media_devnode_is_registered (&mdev->devnode)) {mutex_unlock (&mdev->graph_mutex);return ; } - -/* Remove all entities from the media device */ -list_for_each_entry_safe (entity, next, &mdev->entities, graph_obj.list) -__media_device_unregister_entity (entity); - -/* Remove all entity_notify callbacks from the media device */ -list_for_each_entry_safe (notify, nextp, &mdev->entity_notify, list) -__media_device_unregister_entity_notify (mdev, notify); - -/* Remove all interfaces from the media device */ -list_for_each_entry_safe (intf, tmp_intf, &mdev->interfaces, - graph_obj.list) { -/* -* Unlink the interface, but don't free it here; the -* module which created it is responsible for freeing -* it -*/ -__media_remove_intf_links (intf); -media_gobj_destroy (&intf->graph_obj); - } -mutex_unlock (&mdev->graph_mutex);device_remove_file (&mdev->devnode.dev, &dev_attr_model);
Signature · 1 line
2.39.2
Laurent Pinchart
maintainerRe: [PATCH v2 18/29] media: mc: Postpone graph object removal until free
Hi Sakari,
Thank you for the patch.
Quoted 16 lines · Sakari Ailus
The media device itself will be unregistered based on it being unbound and driver's remove callback being called. The graph objects themselves may still be in use; rely on the media device release callback to release them.
Signed-off-by Sakari Ailus <[email protected]>Acked-by Hans Verkuil <[email protected]>--- drivers/media/mc/mc-device.c | 53 +++++++++++++++++------------------- 1 file changed, 25 insertions(+), 28 deletions(-)drivers/media/mc/mc-device.c +0
-0
drivers/media/mc/mc-device.c EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify); · line 710
Quoted 9 lines
drivers/media/mc/mc-device.c +5
-0@@ -702,8 +702,33 @@ EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify);static void __media_device_release (struct media_device *mdev) { +struct media_entity *entity; +struct media_entity *next; +struct media_interface *intf, *tmp_intf; +struct media_entity_notify *notify, *nextp; +dev_dbg (mdev->dev,"Media device released\n" );
No need for locking ? I suppose we can't reach this point if someone else has a reference to the media device. A comment to mention it would be nice.
drivers/media/mc/mc-device.c EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify); · line 714
Quoted 4 lines
drivers/media/mc/mc-device.c +3
-0+/* Remove all entities from the media device */ +list_for_each_entry_safe (entity, next, &mdev->entities, graph_obj.list) +__media_device_unregister_entity (entity);
Should the __media_device_unregister_entity() function be renamed to __media_device_remove_entity() (in a separate patch) ? Same for __media_device_unregister_entity_notify().
Quoted 27 lines
drivers/media/mc/mc-device.c +17
-3+ +/* Remove all entity_notify callbacks from the media device */ +list_for_each_entry_safe (notify, nextp, &mdev->entity_notify, list) +__media_device_unregister_entity_notify (mdev, notify); + +/* Remove all interfaces from the media device */ +list_for_each_entry_safe (intf, tmp_intf, &mdev->interfaces, + graph_obj.list) { +/* +* Unlink the interface, but don't free it here; the +* module which created it is responsible for freeing +* it +*/ +__media_remove_intf_links (intf); +media_gobj_destroy (&intf->graph_obj); + } +ida_destroy (&mdev->entity_internal_idx); mdev->entity_internal_idx_max =0 ;media_graph_walk_cleanup (&mdev->pm_count_walk);@@ -787,42 +812,14 @@ EXPORT_SYMBOL_GPL(__media_device_register);void media_device_unregister (struct media_device *mdev) { -struct media_entity *entity; -struct media_entity *next; -struct media_interface *intf, *tmp_intf;
drivers/media/mc/mc-device.c EXPORT_SYMBOL_GPL(__media_device_register); · line 820
Quoted 10 lines
drivers/media/mc/mc-device.c +0
-4-struct media_entity_notify *notify, *nextp; -if (mdev ==NULL )return ;mutex_lock (&mdev->graph_mutex); - -/* Check if mdev was ever registered at all */ if (!media_devnode_is_registered (&mdev->devnode)) {mutex_unlock (&mdev->graph_mutex);
Unless I'm mistaken we don't need to lock the graph mutext to test this, so I think you can drop locking completely here.
Quoted 26 lines
drivers/media/mc/mc-device.c +0
-21return ; } - -/* Remove all entities from the media device */ -list_for_each_entry_safe (entity, next, &mdev->entities, graph_obj.list) -__media_device_unregister_entity (entity); - -/* Remove all entity_notify callbacks from the media device */ -list_for_each_entry_safe (notify, nextp, &mdev->entity_notify, list) -__media_device_unregister_entity_notify (mdev, notify); - -/* Remove all interfaces from the media device */ -list_for_each_entry_safe (intf, tmp_intf, &mdev->interfaces, - graph_obj.list) { -/* -* Unlink the interface, but don't free it here; the -* module which created it is responsible for freeing -* it -*/ -__media_remove_intf_links (intf); -media_gobj_destroy (&intf->graph_obj); - } -mutex_unlock (&mdev->graph_mutex);device_remove_file (&mdev->devnode.dev, &dev_attr_model);
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerRe: [PATCH v2 18/29] media: mc: Postpone graph object removal until free
Hi Laurent,
Thanks for the comments. Apologies for missing this earlier.
Quoted 22 lines · Laurent Pinchart
Hi Sakari,
Thank you for the patch.
Quoted 17 lines · Sakari Ailus
The media device itself will be unregistered based on it being unbound and driver's remove callback being called. The graph objects themselves may still be in use; rely on the media device release callback to release them.
Signed-off-by Sakari Ailus <[email protected]>Acked-by Hans Verkuil <[email protected]>--- drivers/media/mc/mc-device.c | 53 +++++++++++++++++------------------- 1 file changed, 25 insertions(+), 28 deletions(-)drivers/media/mc/mc-device.c +0
-0@@ -702,8 +702,33 @@ EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify);
Quoted 12 lines
drivers/media/mc/mc-device.c EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify); · line 710
Quoted 8 lines
drivers/media/mc/mc-device.c +5
-0static void __media_device_release (struct media_device *mdev) { +struct media_entity *entity; +struct media_entity *next; +struct media_interface *intf, *tmp_intf; +struct media_entity_notify *notify, *nextp; +dev_dbg (mdev->dev,"Media device released\n" );No need for locking ? I suppose we can't reach this point if someone else has a reference to the media device. A comment to mention it would be nice.
There's indeed no point in locking a mutex in memory that's about to get released. In fact, the mutex is about to get destroyed first. Other release callback don't have comments, although the purpose of __media_device_release wasn't that of an ordinary release callback. Still, bygones are bygones.
Quoted 9 lines
drivers/media/mc/mc-device.c EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify); · line 714
Quoted 5 lines
drivers/media/mc/mc-device.c +3
-0+/* Remove all entities from the media device */ +list_for_each_entry_safe (entity, next, &mdev->entities, graph_obj.list) +__media_device_unregister_entity (entity);Should the
__media_device_unregister_entity()function be renamed to__media_device_remove_entity()(in a separate patch) ? Same for__media_device_unregister_entity_notify().
"unregister" pairs with "register" so at least for now I'd prefer to keep the naming as-is. I'm fine discussing the naming after this set though.
Quoted 33 lines
Quoted 33 lines
drivers/media/mc/mc-device.c +17
-5+ +/* Remove all entity_notify callbacks from the media device */ +list_for_each_entry_safe (notify, nextp, &mdev->entity_notify, list) +__media_device_unregister_entity_notify (mdev, notify); + +/* Remove all interfaces from the media device */ +list_for_each_entry_safe (intf, tmp_intf, &mdev->interfaces, + graph_obj.list) { +/* +* Unlink the interface, but don't free it here; the +* module which created it is responsible for freeing +* it +*/ +__media_remove_intf_links (intf); +media_gobj_destroy (&intf->graph_obj); + } +ida_destroy (&mdev->entity_internal_idx); mdev->entity_internal_idx_max =0 ;media_graph_walk_cleanup (&mdev->pm_count_walk);@@ -787,42 +812,14 @@ EXPORT_SYMBOL_GPL(__media_device_register);void media_device_unregister (struct media_device *mdev) { -struct media_entity *entity; -struct media_entity *next; -struct media_interface *intf, *tmp_intf; -struct media_entity_notify *notify, *nextp; -if (mdev ==NULL )return ;
Quoted 8 lines
drivers/media/mc/mc-device.c EXPORT_SYMBOL_GPL(__media_device_register); · line 820
Quoted 5 lines
drivers/media/mc/mc-device.c +0
-2mutex_lock (&mdev->graph_mutex); - -/* Check if mdev was ever registered at all */ if (!media_devnode_is_registered (&mdev->devnode)) {mutex_unlock (&mdev->graph_mutex);Unless I'm mistaken we don't need to lock the graph mutext to test this, so I think you can drop locking completely here.
There may be IOCTL calls in progress while unregister takes place. The test seems to be fine outside the lock but the section below still needs the lock.
I'll change this for v4.
Quoted 28 lines
Quoted 27 lines
drivers/media/mc/mc-device.c +0
-21return ; } - -/* Remove all entities from the media device */ -list_for_each_entry_safe (entity, next, &mdev->entities, graph_obj.list) -__media_device_unregister_entity (entity); - -/* Remove all entity_notify callbacks from the media device */ -list_for_each_entry_safe (notify, nextp, &mdev->entity_notify, list) -__media_device_unregister_entity_notify (mdev, notify); - -/* Remove all interfaces from the media device */ -list_for_each_entry_safe (intf, tmp_intf, &mdev->interfaces, - graph_obj.list) { -/* -* Unlink the interface, but don't free it here; the -* module which created it is responsible for freeing -* it -*/ -__media_remove_intf_links (intf); -media_gobj_destroy (&intf->graph_obj); - } -mutex_unlock (&mdev->graph_mutex);device_remove_file (&mdev->devnode.dev, &dev_attr_model);
Signature · 3 lines
Kind regards, Sakari Ailus
Sakari Ailus
maintainerRe: [PATCH v2 18/29] media: mc: Postpone graph object removal until free
Quoted 21 lines · Sakari Ailus
Quoted 12 lines
Quoted 12 lines
drivers/media/mc/mc-device.c +0
-5@@ -787,42 +812,14 @@ EXPORT_SYMBOL_GPL(__media_device_register);void media_device_unregister (struct media_device *mdev) { -struct media_entity *entity; -struct media_entity *next; -struct media_interface *intf, *tmp_intf; -struct media_entity_notify *notify, *nextp; -if (mdev ==NULL )return ;Quoted 8 lines
drivers/media/mc/mc-device.c EXPORT_SYMBOL_GPL(__media_device_register); · line 820
Quoted 5 lines
drivers/media/mc/mc-device.c +0
-2mutex_lock (&mdev->graph_mutex); - -/* Check if mdev was ever registered at all */ if (!media_devnode_is_registered (&mdev->devnode)) {mutex_unlock (&mdev->graph_mutex);Unless I'm mistaken we don't need to lock the graph mutext to test this, so I think you can drop locking completely here.
Quoted 5 lines
There may be IOCTL calls in progress while unregister takes place. The test seems to be fine outside the lock but the section below still needs the lock.
I'll change this for v4.
Indeed the mutex can go, all of the work with the lists was removed by this patch.
Signature · 1 line
Sakari Ailus
Sakari Ailus
maintainerPATCH v2 19/29 media: omap3isp: Release the isp device struct by media device callback
In reply to Sakari AilusUse the media device release callback to release the isp device's data structure. This approach has the benefit of not releasing memory which may still be accessed through open file handles whilst the isp driver is being unbound.
--- drivers/media/platform/ti/omap3isp/isp.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-)
drivers/media/platform/ti/omap3isp/isp.c +19 -5
@@ -649,8 +649,11 @@ static irqreturn_t isp_isr(int irq, void *_isp)return IRQ_HANDLED ; } +static void isp_release (struct media_device *mdev); +static const struct media_device_ops isp_media_ops = { .link_notify = v4l2_pipeline_link_notify, + .release = isp_release, };/* -----------------------------------------------------------------------------
@@ -1607,7 +1610,6 @@ static void isp_unregister_entities(struct isp_device *isp)omap3isp_stat_unregister_entities (&isp->isp_hist);v4l2_device_unregister (&isp->v4l2_dev); -media_device_cleanup (&isp->media_dev); } static int isp_link_entity(
@@ -1955,6 +1957,19 @@ static void isp_detach_iommu(struct isp_device *isp)#endif } +static void isp_release (struct media_device *mdev) +{ +struct isp_device *isp = + container_of(mdev, struct isp_device, media_dev); + +isp_cleanup_modules (isp); + +media_entity_enum_cleanup (&isp->crashed); +v4l2_async_nf_cleanup (&isp->notifier); + +kfree (isp); +} +static int isp_attach_iommu (struct isp_device *isp) {#ifdef CONFIG_ARM_DMA_USE_IOMMU
@@ -2004,16 +2019,15 @@ static void isp_remove(struct platform_device *pdev)v4l2_async_nf_unregister (&isp->notifier);v4l2_async_nf_cleanup (&isp->notifier);isp_unregister_entities (isp); -isp_cleanup_modules (isp); +isp_xclk_cleanup (isp);__omap3isp_get (isp, false);isp_detach_iommu (isp);__omap3isp_put (isp, false); -media_entity_enum_cleanup (&isp->crashed); - -kfree (isp); +/* May release isp immediately */ +media_device_put (&isp->media_dev); }enum isp_of_phy {
Signature · 1 line
2.39.2
Laurent Pinchart
maintainerRe: [PATCH v2 19/29] media: omap3isp: Release the isp device struct by media device callback
Hi Sakari,
Thank you for the patch.
Quoted 16 lines · Sakari Ailus
Use the media device release callback to release the isp device's data structure. This approach has the benefit of not releasing memory which may still be accessed through open file handles whilst the isp driver is being unbound.
Signed-off-by Sakari Ailus <[email protected]>Acked-by Hans Verkuil <[email protected]>--- drivers/media/platform/ti/omap3isp/isp.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-)drivers/media/platform/ti/omap3isp/isp.c +0
-0
drivers/media/platform/ti/omap3isp/isp.c static irqreturn_t isp_isr(int irq, void *_isp) · line 653
Quoted 5 lines
drivers/media/platform/ti/omap3isp/isp.c +2
-0@@ -649,8 +649,11 @@ static irqreturn_t isp_isr(int irq, void *_isp)return IRQ_HANDLED ; } +static void isp_release (struct media_device *mdev); +
Forward declarations are not nice :-( Any hope to reorder functions to fix this ?
Quoted 17 lines
drivers/media/platform/ti/omap3isp/isp.c +1
-1static const struct media_device_ops isp_media_ops = { .link_notify = v4l2_pipeline_link_notify, + .release = isp_release, };/* ----------------------------------------------------------------------------- @@ -1607,7 +1610,6 @@ static void isp_unregister_entities(struct isp_device *isp)omap3isp_stat_unregister_entities (&isp->isp_hist);v4l2_device_unregister (&isp->v4l2_dev); -media_device_cleanup (&isp->media_dev); } static int isp_link_entity(@@ -1955,6 +1957,19 @@ static void isp_detach_iommu(struct isp_device *isp)#endif }
drivers/media/platform/ti/omap3isp/isp.c static void isp_detach_iommu(struct isp_device *isp) · line 1968
Quoted 10 lines
drivers/media/platform/ti/omap3isp/isp.c +9
-0+static void isp_release (struct media_device *mdev) +{ +struct isp_device *isp = + container_of(mdev, struct isp_device, media_dev); + +isp_cleanup_modules (isp); + +media_entity_enum_cleanup (&isp->crashed); +v4l2_async_nf_cleanup (&isp->notifier);
This duplicates the call in isp_remove().
Quoted 27 lines
drivers/media/platform/ti/omap3isp/isp.c +7
-4+ +kfree (isp); +} +static int isp_attach_iommu (struct isp_device *isp) {#ifdef CONFIG_ARM_DMA_USE_IOMMU @@ -2004,16 +2019,15 @@ static void isp_remove(struct platform_device *pdev)v4l2_async_nf_unregister (&isp->notifier);v4l2_async_nf_cleanup (&isp->notifier);isp_unregister_entities (isp); -isp_cleanup_modules (isp); +isp_xclk_cleanup (isp);__omap3isp_get (isp, false);isp_detach_iommu (isp);__omap3isp_put (isp, false); -media_entity_enum_cleanup (&isp->crashed); - -kfree (isp); +/* May release isp immediately */ +media_device_put (&isp->media_dev); }enum isp_of_phy {
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerRe: [PATCH v2 19/29] media: omap3isp: Release the isp device struct by media device callback
Hi Laurent,
Thanks for the review.
Quoted 24 lines · Laurent Pinchart
Hi Sakari,
Thank you for the patch.
Quoted 19 lines · Sakari Ailus
Use the media device release callback to release the isp device's data structure. This approach has the benefit of not releasing memory which may still be accessed through open file handles whilst the isp driver is being unbound.
Signed-off-by Sakari Ailus <[email protected]>Acked-by Hans Verkuil <[email protected]>--- drivers/media/platform/ti/omap3isp/isp.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-)drivers/media/platform/ti/omap3isp/isp.c +0
-0@@ -649,8 +649,11 @@ static irqreturn_t isp_isr(int irq, void *_isp)return IRQ_HANDLED ; }
Quoted 5 lines
drivers/media/platform/ti/omap3isp/isp.c static irqreturn_t isp_isr(int irq, void *_isp) · line 653
Quoted 2 lines
drivers/media/platform/ti/omap3isp/isp.c +2
-0+static void isp_release (struct media_device *mdev); +Forward declarations are not nice :-( Any hope to reorder functions to fix this ?
This depends on moving isp_cleanup_modules up, too. If you think it's the lesser evil I'm fine with that.
Quoted 19 lines
Quoted 19 lines
drivers/media/platform/ti/omap3isp/isp.c +1
-1static const struct media_device_ops isp_media_ops = { .link_notify = v4l2_pipeline_link_notify, + .release = isp_release, };/* ----------------------------------------------------------------------------- @@ -1607,7 +1610,6 @@ static void isp_unregister_entities(struct isp_device *isp)omap3isp_stat_unregister_entities (&isp->isp_hist);v4l2_device_unregister (&isp->v4l2_dev); -media_device_cleanup (&isp->media_dev); } static int isp_link_entity(@@ -1955,6 +1957,19 @@ static void isp_detach_iommu(struct isp_device *isp)#endif }
Quoted 11 lines
drivers/media/platform/ti/omap3isp/isp.c static void isp_detach_iommu(struct isp_device *isp) · line 1968
Quoted 9 lines
drivers/media/platform/ti/omap3isp/isp.c +9
-0+static void isp_release (struct media_device *mdev) +{ +struct isp_device *isp = + container_of(mdev, struct isp_device, media_dev); + +isp_cleanup_modules (isp); + +media_entity_enum_cleanup (&isp->crashed); +v4l2_async_nf_cleanup (&isp->notifier);This duplicates the call in
isp_remove().
I'll drop the one in isp_remove().
Quoted 29 lines
Quoted 28 lines
drivers/media/platform/ti/omap3isp/isp.c +7
-4+ +kfree (isp); +} +static int isp_attach_iommu (struct isp_device *isp) {#ifdef CONFIG_ARM_DMA_USE_IOMMU @@ -2004,16 +2019,15 @@ static void isp_remove(struct platform_device *pdev)v4l2_async_nf_unregister (&isp->notifier);v4l2_async_nf_cleanup (&isp->notifier);isp_unregister_entities (isp); -isp_cleanup_modules (isp); +isp_xclk_cleanup (isp);__omap3isp_get (isp, false);isp_detach_iommu (isp);__omap3isp_put (isp, false); -media_entity_enum_cleanup (&isp->crashed); - -kfree (isp); +/* May release isp immediately */ +media_device_put (&isp->media_dev); }enum isp_of_phy {
Signature · 3 lines
Kind regards, Sakari Ailus
Sakari Ailus
maintainerPATCH v2 20/29 media: ipu3-cio2: Call v4l2_device_unregister() earlier
In reply to Sakari Ailusv4l2_device_unregister() unregisters V4L2 sub-device nodes among other things. Call it before releasing memory and other resources.
--- drivers/media/pci/intel/ipu3/ipu3-cio2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/pci/intel/ipu3/ipu3-cio2.c +1 -1
@@ -1827,11 +1827,11 @@ static void cio2_pci_remove(struct pci_dev *pci_dev)struct cio2_device *cio2 =pci_get_drvdata (pci_dev);media_device_unregister (&cio2->media_dev); +v4l2_device_unregister (&cio2->v4l2_dev);v4l2_async_nf_unregister (&cio2->notifier);v4l2_async_nf_cleanup (&cio2->notifier);cio2_queues_exit (cio2);cio2_fbpt_exit_dummy (cio2); -v4l2_device_unregister (&cio2->v4l2_dev);media_device_cleanup (&cio2->media_dev);mutex_destroy (&cio2->lock);
Signature · 1 line
2.39.2
Laurent Pinchart
maintainerRe: [PATCH v2 20/29] media: ipu3-cio2: Call v4l2_device_unregister() earlier
Hi Sakari,
Thank you for the patch.
Quoted 2 lines · Sakari Ailus
v4l2_device_unregister()unregisters V4L2 sub-device nodes among other things. Call it before releasing memory and other resources.
Please expand the commit message, it's not immediately clear why this is needed and what the consequences are.
Quoted 23 lines
Signed-off-by Sakari Ailus <[email protected]>Acked-by Hans Verkuil <[email protected]>--- drivers/media/pci/intel/ipu3/ipu3-cio2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/media/pci/intel/ipu3/ipu3-cio2.c +1
-1@@ -1827,11 +1827,11 @@ static void cio2_pci_remove(struct pci_dev *pci_dev)struct cio2_device *cio2 =pci_get_drvdata (pci_dev);media_device_unregister (&cio2->media_dev); +v4l2_device_unregister (&cio2->v4l2_dev);v4l2_async_nf_unregister (&cio2->notifier);v4l2_async_nf_cleanup (&cio2->notifier);cio2_queues_exit (cio2);cio2_fbpt_exit_dummy (cio2); -v4l2_device_unregister (&cio2->v4l2_dev);media_device_cleanup (&cio2->media_dev);mutex_destroy (&cio2->lock);
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerRe: [PATCH v2 20/29] media: ipu3-cio2: Call v4l2_device_unregister() earlier
Hi Laurent,
Quoted 10 lines · Laurent Pinchart
Hi Sakari,
Thank you for the patch.
Quoted 2 lines · Sakari Ailus
v4l2_device_unregister()unregisters V4L2 sub-device nodes among other things. Call it before releasing memory and other resources.Please expand the commit message, it's not immediately clear why this is needed and what the consequences are.
Thanks for the review.
There's actually a change in APIs here as the async notifier expects the V4L2 device to remain in place so the notifier can't be cleaned up before unregistering the V4L2 device. Further patches in the set move cleaning up the notifier to the release callback but I think it can well be done here, too. So I think this patch can be dropped.
Signature · 3 lines
Regards, Sakari Ailus
Sakari Ailus
maintainerRe: [PATCH v2 20/29] media: ipu3-cio2: Call v4l2_device_unregister() earlier
Quoted 14 lines · Sakari Ailus
Hi Laurent,
Quoted 10 lines · Laurent Pinchart
Hi Sakari,
Thank you for the patch.
Quoted 2 lines · Sakari Ailus
v4l2_device_unregister()unregisters V4L2 sub-device nodes among other things. Call it before releasing memory and other resources.Please expand the commit message, it's not immediately clear why this is needed and what the consequences are.
Quoted 7 lines
Thanks for the review.
There's actually a change in APIs here as the async notifier expects the V4L2 device to remain in place so the notifier can't be cleaned up before unregistering the V4L2 device. Further patches in the set move cleaning up the notifier to the release callback but I think it can well be done here, too. So I think this patch can be dropped.
That went a bit too fast. The call should be moved after the notifier cleanup.
Signature · 1 line
Sakari Ailus
Sakari Ailus
maintainerPATCH v2 21/29 media: ipu3-cio2: Request IRQ earlier
In reply to Sakari AilusCall devm_request_irq() before registering the async notifier, as otherwise it would be possible to use the device before the interrupts could be deliveted to the driver.
--- drivers/media/pci/intel/ipu3/ipu3-cio2.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/media/pci/intel/ipu3/ipu3-cio2.c +5 -5
@@ -1789,11 +1789,6 @@ static int cio2_pci_probe(struct pci_dev *pci_dev,v4l2_async_nf_init (&cio2->notifier, &cio2->v4l2_dev); -/* Register notifier for subdevices we care */ - r =cio2_parse_firmware (cio2); -if (r) - goto fail_clean_notifier; - r =devm_request_irq (dev, pci_dev->irq, cio2_irq,IRQF_SHARED ,CIO2_NAME , cio2);if (r) {
@@ -1801,6 +1796,11 @@ static int cio2_pci_probe(struct pci_dev *pci_dev, goto fail_clean_notifier; } +/* Register notifier for subdevices we care */ + r =cio2_parse_firmware (cio2); +if (r) + goto fail_clean_notifier; +pm_runtime_put_noidle (dev);pm_runtime_allow (dev);
Signature · 1 line
2.39.2
Re: [PATCH v2 21/29] media: ipu3-cio2: Request IRQ earlier
Quoted 3 lines · Sakari Ailus
Call
devm_request_irq()before registering the async notifier, as otherwise it would be possible to use the device before the interrupts could be deliveted to the driver.
deliveted -> delivered
Isn't this a regular fix? Ditto for the previous patch (20/29).
I'd just queue this up in the next PR.
Regards,
Hans
Quoted 34 lines
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/pci/intel/ipu3/ipu3-cio2.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)drivers/media/pci/intel/ipu3/ipu3-cio2.c +5
-5@@ -1789,11 +1789,6 @@ static int cio2_pci_probe(struct pci_dev *pci_dev,v4l2_async_nf_init (&cio2->notifier, &cio2->v4l2_dev); -/* Register notifier for subdevices we care */ - r =cio2_parse_firmware (cio2); -if (r) - goto fail_clean_notifier; - r =devm_request_irq (dev, pci_dev->irq, cio2_irq,IRQF_SHARED ,CIO2_NAME , cio2);if (r) {@@ -1801,6 +1796,11 @@ static int cio2_pci_probe(struct pci_dev *pci_dev, goto fail_clean_notifier; } +/* Register notifier for subdevices we care */ + r =cio2_parse_firmware (cio2); +if (r) + goto fail_clean_notifier; +pm_runtime_put_noidle (dev);pm_runtime_allow (dev);
Laurent Pinchart
maintainerRe: [PATCH v2 21/29] media: ipu3-cio2: Request IRQ earlier
Quoted 10 lines · Hans Verkuil
Quoted 3 lines · Sakari Ailus
Call
devm_request_irq()before registering the async notifier, as otherwise it would be possible to use the device before the interrupts could be deliveted to the driver.deliveted -> delivered
Isn't this a regular fix? Ditto for the previous patch (20/29).
I'd just queue this up in the next PR.
Quoted 33 lines
Quoted 33 lines
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/pci/intel/ipu3/ipu3-cio2.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)drivers/media/pci/intel/ipu3/ipu3-cio2.c +5
-5@@ -1789,11 +1789,6 @@ static int cio2_pci_probe(struct pci_dev *pci_dev,v4l2_async_nf_init (&cio2->notifier, &cio2->v4l2_dev); -/* Register notifier for subdevices we care */ - r =cio2_parse_firmware (cio2); -if (r) - goto fail_clean_notifier; - r =devm_request_irq (dev, pci_dev->irq, cio2_irq,IRQF_SHARED ,CIO2_NAME , cio2);if (r) {@@ -1801,6 +1796,11 @@ static int cio2_pci_probe(struct pci_dev *pci_dev, goto fail_clean_notifier; } +/* Register notifier for subdevices we care */ + r =cio2_parse_firmware (cio2); +if (r) + goto fail_clean_notifier; +pm_runtime_put_noidle (dev);pm_runtime_allow (dev);
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerRe: [PATCH v2 21/29] media: ipu3-cio2: Request IRQ earlier
Hi Laurent, Hans,
Quoted 11 lines · Laurent Pinchart
Quoted 10 lines · Hans Verkuil
Quoted 3 lines · Sakari Ailus
Call
devm_request_irq()before registering the async notifier, as otherwise it would be possible to use the device before the interrupts could be deliveted to the driver.deliveted -> delivered
Isn't this a regular fix? Ditto for the previous patch (20/29).
I'd just queue this up in the next PR.
Yeah, I wrote it as part of the set but missed there are no further dependencies. I'll post a new version of this separately.
Quoted 2 lines
Fixes tags would then be nice.
It was in the patch adding the driver. I'll add Fixes: line.
Signature · 3 lines
Regards, Sakari Ailus
Sakari Ailus
maintainerPATCH v2 22/29 media: ipu3-cio2: Release the cio2 device context by media device callback
In reply to Sakari AilusUse the media device release callback to release the cio2 device's data structure. This approach has the benefit of not releasing memory which may still be accessed through open file handles whilst the ipu3-cio2 driver is being unbound.
--- drivers/media/pci/intel/ipu3/ipu3-cio2.c | 58 ++++++++++++++++-------- 1 file changed, 40 insertions(+), 18 deletions(-)
drivers/media/pci/intel/ipu3/ipu3-cio2.c +40 -18
@@ -238,9 +238,15 @@ static int cio2_fbpt_init(struct cio2_device *cio2, struct cio2_queue *q)return 0 ; } -static void cio2_fbpt_exit (struct cio2_queue *q,struct device *dev) +static int cio2_fbpt_exit (struct cio2_queue *q,struct device *dev) { +if (!q->fbpt) +return -ENOENT ; +dma_free_coherent (dev,CIO2_FBPT_SIZE , q->fbpt, q->fbpt_bus_addr); + q->fbpt =NULL ; + +return 0 ; }/**************** CSI2 hardware setup ****************/
@@ -1643,13 +1649,13 @@ static int cio2_queue_init(struct cio2_device *cio2, struct cio2_queue *q)static void cio2_queue_exit (struct cio2_device *cio2,struct cio2_queue *q) { -vb2_video_unregister_device (&q->vdev);media_entity_cleanup (&q->vdev.entity);v4l2_device_unregister_subdev (&q->subdev);media_entity_cleanup (&q->subdev.entity); -cio2_fbpt_exit (q, &cio2->pci_dev->dev); -mutex_destroy (&q->subdev_lock); -mutex_destroy (&q->lock); +if (!cio2_fbpt_exit (q, &cio2->pci_dev->dev)) { +mutex_destroy (&q->subdev_lock); +mutex_destroy (&q->lock); + } }static int cio2_queues_init (struct cio2_device *cio2)
@@ -1695,6 +1701,23 @@ static int cio2_check_fwnode_graph(struct fwnode_handle *fwnode)return cio2_check_fwnode_graph (fwnode->secondary); } +static void cio2_media_release (struct media_device *mdev) +{ +struct cio2_device *cio2 = + container_of(mdev, struct cio2_device, media_dev); + +v4l2_async_nf_cleanup (&cio2->notifier); +cio2_queues_exit (cio2); +cio2_fbpt_exit_dummy (cio2); +mutex_destroy (&cio2->lock); + +kfree (cio2); +} + +static const struct media_device_ops cio2_mdev_ops = { + .release = cio2_media_release, +}; +/**************** PCI interface ****************/ static int cio2_pci_probe(struct pci_dev *pci_dev,
@@ -1722,7 +1745,7 @@ static int cio2_pci_probe(struct pci_dev *pci_dev,return r; } - cio2 =devm_kzalloc (dev,sizeof (*cio2),GFP_KERNEL ); + cio2 =kzalloc (sizeof (*cio2),GFP_KERNEL );if (!cio2)return -ENOMEM ; cio2->pci_dev = pci_dev;
@@ -1767,6 +1790,7 @@ static int cio2_pci_probe(struct pci_dev *pci_dev,mutex_init (&cio2->lock); cio2->media_dev.dev = dev; + cio2->media_dev.ops = &cio2_mdev_ops;strscpy (cio2->media_dev.model,CIO2_DEVICE_NAME ,sizeof (cio2->media_dev.model)); cio2->media_dev.hw_revision =0 ;
@@ -1774,7 +1798,7 @@ static int cio2_pci_probe(struct pci_dev *pci_dev,media_device_init (&cio2->media_dev); r =media_device_register (&cio2->media_dev);if (r <0 ) - goto fail_mutex_destroy; + goto fail_media_device_put; cio2->v4l2_dev.mdev = &cio2->media_dev; r =v4l2_device_register (dev, &cio2->v4l2_dev);
@@ -1808,35 +1832,33 @@ static int cio2_pci_probe(struct pci_dev *pci_dev, fail_clean_notifier:v4l2_async_nf_unregister (&cio2->notifier); -v4l2_async_nf_cleanup (&cio2->notifier); -cio2_queues_exit (cio2); + fail_v4l2_device_unregister:v4l2_device_unregister (&cio2->v4l2_dev); + fail_media_device_unregister:media_device_unregister (&cio2->media_dev); -media_device_cleanup (&cio2->media_dev); -fail_mutex_destroy: -mutex_destroy (&cio2->lock); -cio2_fbpt_exit_dummy (cio2); +fail_media_device_put: +media_device_put (&cio2->media_dev);return r; }static void cio2_pci_remove (struct pci_dev *pci_dev) {struct cio2_device *cio2 =pci_get_drvdata (pci_dev); +unsigned int i;media_device_unregister (&cio2->media_dev); +for (i =0 ; i <CIO2_QUEUES ; i++) +vb2_video_unregister_device (&cio2->queue[i].vdev);v4l2_device_unregister (&cio2->v4l2_dev);v4l2_async_nf_unregister (&cio2->notifier); -v4l2_async_nf_cleanup (&cio2->notifier); -cio2_queues_exit (cio2); -cio2_fbpt_exit_dummy (cio2); -media_device_cleanup (&cio2->media_dev); -mutex_destroy (&cio2->lock);pm_runtime_forbid (&pci_dev->dev);pm_runtime_get_noresume (&pci_dev->dev); + +media_device_put (&cio2->media_dev); }static int __maybe_unused cio2_runtime_suspend (struct device *dev)
Signature · 1 line
2.39.2
Laurent Pinchart
maintainerRe: [PATCH v2 22/29] media: ipu3-cio2: Release the cio2 device context by media device callback
Hi Sakari,
Thank you for the patch.
Quoted 33 lines · Sakari Ailus
Use the media device release callback to release the cio2 device's data structure. This approach has the benefit of not releasing memory which may still be accessed through open file handles whilst the ipu3-cio2 driver is being unbound.
Signed-off-by Sakari Ailus <[email protected]>Acked-by Hans Verkuil <[email protected]>--- drivers/media/pci/intel/ipu3/ipu3-cio2.c | 58 ++++++++++++++++-------- 1 file changed, 40 insertions(+), 18 deletions(-)drivers/media/pci/intel/ipu3/ipu3-cio2.c +7
-1@@ -238,9 +238,15 @@ static int cio2_fbpt_init(struct cio2_device *cio2, struct cio2_queue *q)return 0 ; } -static void cio2_fbpt_exit (struct cio2_queue *q,struct device *dev) +static int cio2_fbpt_exit (struct cio2_queue *q,struct device *dev) { +if (!q->fbpt) +return -ENOENT ; +dma_free_coherent (dev,CIO2_FBPT_SIZE , q->fbpt, q->fbpt_bus_addr); + q->fbpt =NULL ; + +return 0 ; }/**************** CSI2 hardware setup ****************/
drivers/media/pci/intel/ipu3/ipu3-cio2.c static int cio2_queue_init(struct cio2_device *cio2, struct cio2_queue *q) · line 1653
Quoted 6 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c +0
-1@@ -1643,13 +1649,13 @@ static int cio2_queue_init(struct cio2_device *cio2, struct cio2_queue *q)static void cio2_queue_exit (struct cio2_device *cio2,struct cio2_queue *q) { -vb2_video_unregister_device (&q->vdev);media_entity_cleanup (&q->vdev.entity);v4l2_device_unregister_subdev (&q->subdev);
Is the release callback the right time for this ?
drivers/media/pci/intel/ipu3/ipu3-cio2.c static int cio2_queue_init(struct cio2_device *cio2, struct cio2_queue *q) · line 1655
Quoted 5 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c +1
-3media_entity_cleanup (&q->subdev.entity); -cio2_fbpt_exit (q, &cio2->pci_dev->dev); -mutex_destroy (&q->subdev_lock); -mutex_destroy (&q->lock); +if (!cio2_fbpt_exit (q, &cio2->pci_dev->dev)) {
This doesn't look very nice, but I suppose there are many other things to clean up in this driver, so I'll close my eyes.
Quoted 101 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c +32
-13+mutex_destroy (&q->subdev_lock); +mutex_destroy (&q->lock); + } }static int cio2_queues_init (struct cio2_device *cio2)@@ -1695,6 +1701,23 @@ static int cio2_check_fwnode_graph(struct fwnode_handle *fwnode)return cio2_check_fwnode_graph (fwnode->secondary); } +static void cio2_media_release (struct media_device *mdev) +{ +struct cio2_device *cio2 = + container_of(mdev, struct cio2_device, media_dev); + +v4l2_async_nf_cleanup (&cio2->notifier); +cio2_queues_exit (cio2); +cio2_fbpt_exit_dummy (cio2); +mutex_destroy (&cio2->lock); + +kfree (cio2); +} + +static const struct media_device_ops cio2_mdev_ops = { + .release = cio2_media_release, +}; +/**************** PCI interface ****************/ static int cio2_pci_probe(struct pci_dev *pci_dev,@@ -1722,7 +1745,7 @@ static int cio2_pci_probe(struct pci_dev *pci_dev,return r; } - cio2 =devm_kzalloc (dev,sizeof (*cio2),GFP_KERNEL ); + cio2 =kzalloc (sizeof (*cio2),GFP_KERNEL );if (!cio2)return -ENOMEM ; cio2->pci_dev = pci_dev;@@ -1767,6 +1790,7 @@ static int cio2_pci_probe(struct pci_dev *pci_dev,mutex_init (&cio2->lock); cio2->media_dev.dev = dev; + cio2->media_dev.ops = &cio2_mdev_ops;strscpy (cio2->media_dev.model,CIO2_DEVICE_NAME ,sizeof (cio2->media_dev.model)); cio2->media_dev.hw_revision =0 ;@@ -1774,7 +1798,7 @@ static int cio2_pci_probe(struct pci_dev *pci_dev,media_device_init (&cio2->media_dev); r =media_device_register (&cio2->media_dev);if (r <0 ) - goto fail_mutex_destroy; + goto fail_media_device_put; cio2->v4l2_dev.mdev = &cio2->media_dev; r =v4l2_device_register (dev, &cio2->v4l2_dev);@@ -1808,35 +1832,33 @@ static int cio2_pci_probe(struct pci_dev *pci_dev, fail_clean_notifier:v4l2_async_nf_unregister (&cio2->notifier); -v4l2_async_nf_cleanup (&cio2->notifier); -cio2_queues_exit (cio2); + fail_v4l2_device_unregister:v4l2_device_unregister (&cio2->v4l2_dev); + fail_media_device_unregister:media_device_unregister (&cio2->media_dev); -media_device_cleanup (&cio2->media_dev); -fail_mutex_destroy: -mutex_destroy (&cio2->lock); -cio2_fbpt_exit_dummy (cio2); +fail_media_device_put: +media_device_put (&cio2->media_dev);return r; }static void cio2_pci_remove (struct pci_dev *pci_dev) {struct cio2_device *cio2 =pci_get_drvdata (pci_dev); +unsigned int i;media_device_unregister (&cio2->media_dev); +for (i =0 ; i <CIO2_QUEUES ; i++) +vb2_video_unregister_device (&cio2->queue[i].vdev);v4l2_device_unregister (&cio2->v4l2_dev);v4l2_async_nf_unregister (&cio2->notifier); -v4l2_async_nf_cleanup (&cio2->notifier); -cio2_queues_exit (cio2); -cio2_fbpt_exit_dummy (cio2); -media_device_cleanup (&cio2->media_dev); -mutex_destroy (&cio2->lock);pm_runtime_forbid (&pci_dev->dev);pm_runtime_get_noresume (&pci_dev->dev); + +media_device_put (&cio2->media_dev); }static int __maybe_unused cio2_runtime_suspend (struct device *dev)
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerRe: [PATCH v2 22/29] media: ipu3-cio2: Release the cio2 device context by media device callback
Hi Laurent,
Quoted 39 lines · Laurent Pinchart
Hi Sakari,
Thank you for the patch.
Quoted 34 lines · Sakari Ailus
Use the media device release callback to release the cio2 device's data structure. This approach has the benefit of not releasing memory which may still be accessed through open file handles whilst the ipu3-cio2 driver is being unbound.
Signed-off-by Sakari Ailus <[email protected]>Acked-by Hans Verkuil <[email protected]>--- drivers/media/pci/intel/ipu3/ipu3-cio2.c | 58 ++++++++++++++++-------- 1 file changed, 40 insertions(+), 18 deletions(-)drivers/media/pci/intel/ipu3/ipu3-cio2.c +7
-1@@ -238,9 +238,15 @@ static int cio2_fbpt_init(struct cio2_device *cio2, struct cio2_queue *q)return 0 ; } -static void cio2_fbpt_exit (struct cio2_queue *q,struct device *dev) +static int cio2_fbpt_exit (struct cio2_queue *q,struct device *dev) { +if (!q->fbpt) +return -ENOENT ; +dma_free_coherent (dev,CIO2_FBPT_SIZE , q->fbpt, q->fbpt_bus_addr); + q->fbpt =NULL ; + +return 0 ; }/**************** CSI2 hardware setup ****************/ @@ -1643,13 +1649,13 @@ static int cio2_queue_init(struct cio2_device *cio2, struct cio2_queue *q)
Quoted 7 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c static int cio2_queue_init(struct cio2_device *cio2, struct cio2_queue *q) · line 1653
Quoted 5 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c +0
-1static void cio2_queue_exit (struct cio2_device *cio2,struct cio2_queue *q) { -vb2_video_unregister_device (&q->vdev);media_entity_cleanup (&q->vdev.entity);v4l2_device_unregister_subdev (&q->subdev);Is the release callback the right time for this ?
Neither driver remove or media device release is entirely correct as we don't have refcounting for these yet. A better place would still be in the remove callback. I'll move it there.
Quoted 9 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c static int cio2_queue_init(struct cio2_device *cio2, struct cio2_queue *q) · line 1655
Quoted 6 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c +1
-3media_entity_cleanup (&q->subdev.entity); -cio2_fbpt_exit (q, &cio2->pci_dev->dev); -mutex_destroy (&q->subdev_lock); -mutex_destroy (&q->lock); +if (!cio2_fbpt_exit (q, &cio2->pci_dev->dev)) {This doesn't look very nice, but I suppose there are many other things to clean up in this driver, so I'll close my eyes.
I'd say ipu3-cio2 is one of the better CSI-2 receiver drivers.
This is related to error handing: you can't call mutex_destroy() on a mutex that's been already destroyed. cio2_queue_exit() is called when the media device is released but we don't know here whether mutexes have been initialised yet. I guess that's something that could be changed but it would create more lines of code elsewhere.
Quoted 103 lines
Quoted 102 lines
drivers/media/pci/intel/ipu3/ipu3-cio2.c +32
-13+mutex_destroy (&q->subdev_lock); +mutex_destroy (&q->lock); + } }static int cio2_queues_init (struct cio2_device *cio2)@@ -1695,6 +1701,23 @@ static int cio2_check_fwnode_graph(struct fwnode_handle *fwnode)return cio2_check_fwnode_graph (fwnode->secondary); } +static void cio2_media_release (struct media_device *mdev) +{ +struct cio2_device *cio2 = + container_of(mdev, struct cio2_device, media_dev); + +v4l2_async_nf_cleanup (&cio2->notifier); +cio2_queues_exit (cio2); +cio2_fbpt_exit_dummy (cio2); +mutex_destroy (&cio2->lock); + +kfree (cio2); +} + +static const struct media_device_ops cio2_mdev_ops = { + .release = cio2_media_release, +}; +/**************** PCI interface ****************/ static int cio2_pci_probe(struct pci_dev *pci_dev,@@ -1722,7 +1745,7 @@ static int cio2_pci_probe(struct pci_dev *pci_dev,return r; } - cio2 =devm_kzalloc (dev,sizeof (*cio2),GFP_KERNEL ); + cio2 =kzalloc (sizeof (*cio2),GFP_KERNEL );if (!cio2)return -ENOMEM ; cio2->pci_dev = pci_dev;@@ -1767,6 +1790,7 @@ static int cio2_pci_probe(struct pci_dev *pci_dev,mutex_init (&cio2->lock); cio2->media_dev.dev = dev; + cio2->media_dev.ops = &cio2_mdev_ops;strscpy (cio2->media_dev.model,CIO2_DEVICE_NAME ,sizeof (cio2->media_dev.model)); cio2->media_dev.hw_revision =0 ;@@ -1774,7 +1798,7 @@ static int cio2_pci_probe(struct pci_dev *pci_dev,media_device_init (&cio2->media_dev); r =media_device_register (&cio2->media_dev);if (r <0 ) - goto fail_mutex_destroy; + goto fail_media_device_put; cio2->v4l2_dev.mdev = &cio2->media_dev; r =v4l2_device_register (dev, &cio2->v4l2_dev);@@ -1808,35 +1832,33 @@ static int cio2_pci_probe(struct pci_dev *pci_dev, fail_clean_notifier:v4l2_async_nf_unregister (&cio2->notifier); -v4l2_async_nf_cleanup (&cio2->notifier); -cio2_queues_exit (cio2); + fail_v4l2_device_unregister:v4l2_device_unregister (&cio2->v4l2_dev); + fail_media_device_unregister:media_device_unregister (&cio2->media_dev); -media_device_cleanup (&cio2->media_dev); -fail_mutex_destroy: -mutex_destroy (&cio2->lock); -cio2_fbpt_exit_dummy (cio2); +fail_media_device_put: +media_device_put (&cio2->media_dev);return r; }static void cio2_pci_remove (struct pci_dev *pci_dev) {struct cio2_device *cio2 =pci_get_drvdata (pci_dev); +unsigned int i;media_device_unregister (&cio2->media_dev); +for (i =0 ; i <CIO2_QUEUES ; i++) +vb2_video_unregister_device (&cio2->queue[i].vdev);v4l2_device_unregister (&cio2->v4l2_dev);v4l2_async_nf_unregister (&cio2->notifier); -v4l2_async_nf_cleanup (&cio2->notifier); -cio2_queues_exit (cio2); -cio2_fbpt_exit_dummy (cio2); -media_device_cleanup (&cio2->media_dev); -mutex_destroy (&cio2->lock);pm_runtime_forbid (&pci_dev->dev);pm_runtime_get_noresume (&pci_dev->dev); + +media_device_put (&cio2->media_dev); }static int __maybe_unused cio2_runtime_suspend (struct device *dev)
Signature · 3 lines
Regards, Sakari Ailus
Sakari Ailus
maintainerPATCH v2 23/29 media: vimc: Release resources on media device release
In reply to Sakari AilusRelease all the resources when the media device is related, moving away form the struct v4l2_device used for that purpose.
--- drivers/media/test-drivers/vimc/vimc-core.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/media/test-drivers/vimc/vimc-core.c +9 -6
@@ -264,13 +264,12 @@ static int vimc_add_subdevs(struct vimc_device *vimc)return 0 ; } -static void vimc_v4l2_dev_release (struct v4l2_device *v4l2_dev) +static void vimc_mdev_release (struct media_device *mdev) {struct vimc_device *vimc = - container_of(v4l2_dev, struct vimc_device, v4l2_dev); + container_of_const(mdev, struct vimc_device, mdev);vimc_release_subdevs (vimc); -media_device_cleanup (&vimc->mdev);kfree (vimc->ent_devs);kfree (vimc); }
@@ -336,6 +335,10 @@ static int vimc_register_devices(struct vimc_device *vimc)return ret; } +static const struct media_device_ops vimc_mdev_ops = { + .release = vimc_mdev_release, +}; +static int vimc_probe (struct platform_device *pdev) {const struct font_desc *font =find_font ("VGA8x16" );
@@ -369,12 +372,12 @@ static int vimc_probe(struct platform_device *pdev)snprintf (vimc->mdev.bus_info,sizeof (vimc->mdev.bus_info),"platform:%s" ,VIMC_PDEV_NAME ); vimc->mdev.dev = &pdev->dev; + vimc->mdev.ops = &vimc_mdev_ops;media_device_init (&vimc->mdev); ret =vimc_register_devices (vimc);if (ret) { -media_device_cleanup (&vimc->mdev); -kfree (vimc); +media_device_put (&vimc->mdev);return ret; }/*
@@ -382,7 +385,6 @@ static int vimc_probe(struct platform_device *pdev)* if the registration fails, we release directly from probe */ - vimc->v4l2_dev.release = vimc_v4l2_dev_release;platform_set_drvdata (pdev, vimc);return 0 ; }
@@ -397,6 +399,7 @@ static void vimc_remove(struct platform_device *pdev)media_device_unregister (&vimc->mdev);v4l2_device_unregister (&vimc->v4l2_dev);v4l2_device_put (&vimc->v4l2_dev); +media_device_put (&vimc->mdev); }static void vimc_dev_release (struct device *dev)
Signature · 1 line
2.39.2
Re: [PATCH v2 23/29] media: vimc: Release resources on media device release
Quoted 2 lines · Sakari Ailus
Release all the resources when the media device is related, moving away form the struct
v4l2_deviceused for that purpose.
form -> from
Quoted 11 lines
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/test-drivers/vimc/vimc-core.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)drivers/media/test-drivers/vimc/vimc-core.c +0
-0
drivers/media/test-drivers/vimc/vimc-core.c static int vimc_add_subdevs(struct vimc_device *vimc) · line 270
Quoted 9 lines
drivers/media/test-drivers/vimc/vimc-core.c +2
-2@@ -264,13 +264,12 @@ static int vimc_add_subdevs(struct vimc_device *vimc)return 0 ; } -static void vimc_v4l2_dev_release (struct v4l2_device *v4l2_dev) +static void vimc_mdev_release (struct media_device *mdev) {struct vimc_device *vimc = - container_of(v4l2_dev, struct vimc_device, v4l2_dev); + container_of_const(mdev, struct vimc_device, mdev);
Why this change?
Quoted 41 lines
drivers/media/test-drivers/vimc/vimc-core.c +6
-4vimc_release_subdevs (vimc); -media_device_cleanup (&vimc->mdev);kfree (vimc->ent_devs);kfree (vimc); }@@ -336,6 +335,10 @@ static int vimc_register_devices(struct vimc_device *vimc)return ret; } +static const struct media_device_ops vimc_mdev_ops = { + .release = vimc_mdev_release, +}; +static int vimc_probe (struct platform_device *pdev) {const struct font_desc *font =find_font ("VGA8x16" );@@ -369,12 +372,12 @@ static int vimc_probe(struct platform_device *pdev)snprintf (vimc->mdev.bus_info,sizeof (vimc->mdev.bus_info),"platform:%s" ,VIMC_PDEV_NAME ); vimc->mdev.dev = &pdev->dev; + vimc->mdev.ops = &vimc_mdev_ops;media_device_init (&vimc->mdev); ret =vimc_register_devices (vimc);if (ret) { -media_device_cleanup (&vimc->mdev); -kfree (vimc); +media_device_put (&vimc->mdev);return ret; }/* @@ -382,7 +385,6 @@ static int vimc_probe(struct platform_device *pdev)* if the registration fails, we release directly from probe */ - vimc->v4l2_dev.release = vimc_v4l2_dev_release;platform_set_drvdata (pdev, vimc);return 0 ; }
drivers/media/test-drivers/vimc/vimc-core.c static void vimc_remove(struct platform_device *pdev) · line 405
Quoted 7 lines
drivers/media/test-drivers/vimc/vimc-core.c +1
-0@@ -397,6 +399,7 @@ static void vimc_remove(struct platform_device *pdev)media_device_unregister (&vimc->mdev);v4l2_device_unregister (&vimc->v4l2_dev);v4l2_device_put (&vimc->v4l2_dev); +media_device_put (&vimc->mdev); }static void vimc_dev_release (struct device *dev)
Regards,
Hans
Laurent Pinchart
maintainerRe: [PATCH v2 23/29] media: vimc: Release resources on media device release
Quoted 2 lines · Hans Verkuil
Quoted 1 line · Sakari Ailus
Release all the resources when the media device is related, moving away
s/related/released/
Quoted 3 lines
Quoted 1 line
form the struct
v4l2_deviceused for that purpose.form -> from
Please explain *why* in the commit message.
Quoted 70 lines
Quoted 10 lines
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/test-drivers/vimc/vimc-core.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)drivers/media/test-drivers/vimc/vimc-core.c +0
-0drivers/media/test-drivers/vimc/vimc-core.c static int vimc_add_subdevs(struct vimc_device *vimc) · line 270
Quoted 9 lines
drivers/media/test-drivers/vimc/vimc-core.c +2
-2@@ -264,13 +264,12 @@ static int vimc_add_subdevs(struct vimc_device *vimc)return 0 ; } -static void vimc_v4l2_dev_release (struct v4l2_device *v4l2_dev) +static void vimc_mdev_release (struct media_device *mdev) {struct vimc_device *vimc = - container_of(v4l2_dev, struct vimc_device, v4l2_dev); + container_of_const(mdev, struct vimc_device, mdev);Why this change?
Quoted 48 lines
drivers/media/test-drivers/vimc/vimc-core.c +7
-4vimc_release_subdevs (vimc); -media_device_cleanup (&vimc->mdev);kfree (vimc->ent_devs);kfree (vimc); }@@ -336,6 +335,10 @@ static int vimc_register_devices(struct vimc_device *vimc)return ret; } +static const struct media_device_ops vimc_mdev_ops = { + .release = vimc_mdev_release, +}; +static int vimc_probe (struct platform_device *pdev) {const struct font_desc *font =find_font ("VGA8x16" );@@ -369,12 +372,12 @@ static int vimc_probe(struct platform_device *pdev)snprintf (vimc->mdev.bus_info,sizeof (vimc->mdev.bus_info),"platform:%s" ,VIMC_PDEV_NAME ); vimc->mdev.dev = &pdev->dev; + vimc->mdev.ops = &vimc_mdev_ops;media_device_init (&vimc->mdev); ret =vimc_register_devices (vimc);if (ret) { -media_device_cleanup (&vimc->mdev); -kfree (vimc); +media_device_put (&vimc->mdev);return ret; }/* @@ -382,7 +385,6 @@ static int vimc_probe(struct platform_device *pdev)* if the registration fails, we release directly from probe */ - vimc->v4l2_dev.release = vimc_v4l2_dev_release;platform_set_drvdata (pdev, vimc);return 0 ; }@@ -397,6 +399,7 @@ static void vimc_remove(struct platform_device *pdev)media_device_unregister (&vimc->mdev);v4l2_device_unregister (&vimc->v4l2_dev);v4l2_device_put (&vimc->v4l2_dev); +media_device_put (&vimc->mdev); }static void vimc_dev_release (struct device *dev)
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerRe: [PATCH v2 23/29] media: vimc: Release resources on media device release
Hi Laurent,
Quoted 11 lines · Laurent Pinchart
Quoted 2 lines · Hans Verkuil
Quoted 1 line · Sakari Ailus
Release all the resources when the media device is related, moving away
s/related/released/
Quoted 3 lines
Quoted 1 line
form the struct
v4l2_deviceused for that purpose.form -> from
Please explain *why* in the commit message.
Just to show how the media device release callback is used. I'll add that to the commit message.
Signature · 3 lines
Regards, Sakari Ailus
Sakari Ailus
maintainerRe: [PATCH v2 23/29] media: vimc: Release resources on media device release
In reply to Hans VerkuilHi Hans,
Quoted 2 lines · Hans Verkuil
Quoted 1 line · Sakari Ailus
Release all the resources when the media device is related, moving away
s/related/released/
Quoted 3 lines
Quoted 1 line
form the struct
v4l2_deviceused for that purpose.form -> from
Yes.
Quoted 15 lines
Quoted 15 lines
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/test-drivers/vimc/vimc-core.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)drivers/media/test-drivers/vimc/vimc-core.c +0
-0@@ -264,13 +264,12 @@ static int vimc_add_subdevs(struct vimc_device *vimc)return 0 ; }
Quoted 8 lines
drivers/media/test-drivers/vimc/vimc-core.c static int vimc_add_subdevs(struct vimc_device *vimc) · line 270
Quoted 6 lines
drivers/media/test-drivers/vimc/vimc-core.c +2
-2-static void vimc_v4l2_dev_release (struct v4l2_device *v4l2_dev) +static void vimc_mdev_release (struct media_device *mdev) {struct vimc_device *vimc = - container_of(v4l2_dev, struct vimc_device, v4l2_dev); + container_of_const(mdev, struct vimc_device, mdev);Why this change?
I changed the line already. There's no reason to continue using container_of() instead of container_of_const() that takes const-ness into account, too.
Quoted 49 lines
Quoted 49 lines
drivers/media/test-drivers/vimc/vimc-core.c +7
-4vimc_release_subdevs (vimc); -media_device_cleanup (&vimc->mdev);kfree (vimc->ent_devs);kfree (vimc); }@@ -336,6 +335,10 @@ static int vimc_register_devices(struct vimc_device *vimc)return ret; } +static const struct media_device_ops vimc_mdev_ops = { + .release = vimc_mdev_release, +}; +static int vimc_probe (struct platform_device *pdev) {const struct font_desc *font =find_font ("VGA8x16" );@@ -369,12 +372,12 @@ static int vimc_probe(struct platform_device *pdev)snprintf (vimc->mdev.bus_info,sizeof (vimc->mdev.bus_info),"platform:%s" ,VIMC_PDEV_NAME ); vimc->mdev.dev = &pdev->dev; + vimc->mdev.ops = &vimc_mdev_ops;media_device_init (&vimc->mdev); ret =vimc_register_devices (vimc);if (ret) { -media_device_cleanup (&vimc->mdev); -kfree (vimc); +media_device_put (&vimc->mdev);return ret; }/* @@ -382,7 +385,6 @@ static int vimc_probe(struct platform_device *pdev)* if the registration fails, we release directly from probe */ - vimc->v4l2_dev.release = vimc_v4l2_dev_release;platform_set_drvdata (pdev, vimc);return 0 ; }@@ -397,6 +399,7 @@ static void vimc_remove(struct platform_device *pdev)media_device_unregister (&vimc->mdev);v4l2_device_unregister (&vimc->v4l2_dev);v4l2_device_put (&vimc->v4l2_dev); +media_device_put (&vimc->mdev); }static void vimc_dev_release (struct device *dev)
Signature · 3 lines
Regards, Sakari Ailus
Laurent Pinchart
maintainerRe: [PATCH v2 23/29] media: vimc: Release resources on media device release
Quoted 38 lines · Sakari Ailus
Hi Hans,
Quoted 2 lines · Hans Verkuil
Quoted 1 line · Sakari Ailus
Release all the resources when the media device is related, moving away
s/related/released/
Quoted 3 lines
Quoted 1 line
form the struct
v4l2_deviceused for that purpose.form -> from
Yes.
Quoted 15 lines
Quoted 15 lines
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/test-drivers/vimc/vimc-core.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)drivers/media/test-drivers/vimc/vimc-core.c +0
-0@@ -264,13 +264,12 @@ static int vimc_add_subdevs(struct vimc_device *vimc)return 0 ; }Quoted 8 lines
drivers/media/test-drivers/vimc/vimc-core.c static int vimc_add_subdevs(struct vimc_device *vimc) · line 270
Quoted 6 lines
drivers/media/test-drivers/vimc/vimc-core.c +2
-2-static void vimc_v4l2_dev_release (struct v4l2_device *v4l2_dev) +static void vimc_mdev_release (struct media_device *mdev) {struct vimc_device *vimc = - container_of(v4l2_dev, struct vimc_device, v4l2_dev); + container_of_const(mdev, struct vimc_device, mdev);Why this change?
Quoted 3 lines
I changed the line already. There's no reason to continue using
container_of()instead ofcontainer_of_const()that takes const-ness into account, too.
It should then be at least mentioned in the commit message.
Any plan to switch to container_of_const() globally in the subsystem ?
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
drivers/media/test-drivers/vimc/vimc-core.c +7
-4vimc_release_subdevs (vimc); -media_device_cleanup (&vimc->mdev);kfree (vimc->ent_devs);kfree (vimc); }@@ -336,6 +335,10 @@ static int vimc_register_devices(struct vimc_device *vimc)return ret; } +static const struct media_device_ops vimc_mdev_ops = { + .release = vimc_mdev_release, +}; +static int vimc_probe (struct platform_device *pdev) {const struct font_desc *font =find_font ("VGA8x16" );@@ -369,12 +372,12 @@ static int vimc_probe(struct platform_device *pdev)snprintf (vimc->mdev.bus_info,sizeof (vimc->mdev.bus_info),"platform:%s" ,VIMC_PDEV_NAME ); vimc->mdev.dev = &pdev->dev; + vimc->mdev.ops = &vimc_mdev_ops;media_device_init (&vimc->mdev); ret =vimc_register_devices (vimc);if (ret) { -media_device_cleanup (&vimc->mdev); -kfree (vimc); +media_device_put (&vimc->mdev);return ret; }/* @@ -382,7 +385,6 @@ static int vimc_probe(struct platform_device *pdev)* if the registration fails, we release directly from probe */ - vimc->v4l2_dev.release = vimc_v4l2_dev_release;platform_set_drvdata (pdev, vimc);return 0 ; }@@ -397,6 +399,7 @@ static void vimc_remove(struct platform_device *pdev)media_device_unregister (&vimc->mdev);v4l2_device_unregister (&vimc->v4l2_dev);v4l2_device_put (&vimc->v4l2_dev); +media_device_put (&vimc->mdev); }static void vimc_dev_release (struct device *dev)
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerRe: [PATCH v2 23/29] media: vimc: Release resources on media device release
Hi Laurent,
Quoted 39 lines · Laurent Pinchart
Quoted 38 lines · Sakari Ailus
Hi Hans,
Quoted 2 lines · Hans Verkuil
Quoted 1 line · Sakari Ailus
Release all the resources when the media device is related, moving away
s/related/released/
Quoted 3 lines
Quoted 1 line
form the struct
v4l2_deviceused for that purpose.form -> from
Yes.
Quoted 23 lines
Quoted 12 lines
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/test-drivers/vimc/vimc-core.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)drivers/media/test-drivers/vimc/vimc-core.c +0
-0drivers/media/test-drivers/vimc/vimc-core.c static int vimc_add_subdevs(struct vimc_device *vimc) · line 270
Quoted 9 lines
drivers/media/test-drivers/vimc/vimc-core.c +2
-2@@ -264,13 +264,12 @@ static int vimc_add_subdevs(struct vimc_device *vimc)return 0 ; } -static void vimc_v4l2_dev_release (struct v4l2_device *v4l2_dev) +static void vimc_mdev_release (struct media_device *mdev) {struct vimc_device *vimc = - container_of(v4l2_dev, struct vimc_device, v4l2_dev); + container_of_const(mdev, struct vimc_device, mdev);Why this change?
Quoted 5 lines
Quoted 3 lines
I changed the line already. There's no reason to continue using
container_of()instead ofcontainer_of_const()that takes const-ness into account, too.It should then be at least mentioned in the commit message.
I can add that.
Quoted 2 lines
Any plan to switch to
container_of_const()globally in the subsystem ?
This should of course be done.
I can post patches at some point unless someone gets there first. I can't promise a quick schedule though.
Signature · 3 lines
Regards, Sakari Ailus
Re: [PATCH v2 23/29] media: vimc: Release resources on media device release
In reply to Sakari AilusQuoted 38 lines · Sakari Ailus
Hi Hans,
Quoted 2 lines · Hans Verkuil
Quoted 1 line · Sakari Ailus
Release all the resources when the media device is related, moving away
s/related/released/
Quoted 3 lines
Quoted 1 line
form the struct
v4l2_deviceused for that purpose.form -> from
Yes.
Quoted 15 lines
Quoted 15 lines
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/test-drivers/vimc/vimc-core.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)drivers/media/test-drivers/vimc/vimc-core.c +0
-0@@ -264,13 +264,12 @@ static int vimc_add_subdevs(struct vimc_device *vimc)return 0 ; }Quoted 8 lines
drivers/media/test-drivers/vimc/vimc-core.c static int vimc_add_subdevs(struct vimc_device *vimc) · line 270
Quoted 6 lines
drivers/media/test-drivers/vimc/vimc-core.c +2
-2-static void vimc_v4l2_dev_release (struct v4l2_device *v4l2_dev) +static void vimc_mdev_release (struct media_device *mdev) {struct vimc_device *vimc = - container_of(v4l2_dev, struct vimc_device, v4l2_dev); + container_of_const(mdev, struct vimc_device, mdev);Why this change?
Quoted 3 lines
I changed the line already. There's no reason to continue using
container_of()instead ofcontainer_of_const()that takes const-ness into account, too.
But neither vimc nor mdev can be const anyway, that would make no sense here.
Regards,
Hans
Quoted 51 lines
Quoted 50 lines
Quoted 50 lines
drivers/media/test-drivers/vimc/vimc-core.c +7
-4vimc_release_subdevs (vimc); -media_device_cleanup (&vimc->mdev);kfree (vimc->ent_devs);kfree (vimc); }@@ -336,6 +335,10 @@ static int vimc_register_devices(struct vimc_device *vimc)return ret; } +static const struct media_device_ops vimc_mdev_ops = { + .release = vimc_mdev_release, +}; +static int vimc_probe (struct platform_device *pdev) {const struct font_desc *font =find_font ("VGA8x16" );@@ -369,12 +372,12 @@ static int vimc_probe(struct platform_device *pdev)snprintf (vimc->mdev.bus_info,sizeof (vimc->mdev.bus_info),"platform:%s" ,VIMC_PDEV_NAME ); vimc->mdev.dev = &pdev->dev; + vimc->mdev.ops = &vimc_mdev_ops;media_device_init (&vimc->mdev); ret =vimc_register_devices (vimc);if (ret) { -media_device_cleanup (&vimc->mdev); -kfree (vimc); +media_device_put (&vimc->mdev);return ret; }/* @@ -382,7 +385,6 @@ static int vimc_probe(struct platform_device *pdev)* if the registration fails, we release directly from probe */ - vimc->v4l2_dev.release = vimc_v4l2_dev_release;platform_set_drvdata (pdev, vimc);return 0 ; }@@ -397,6 +399,7 @@ static void vimc_remove(struct platform_device *pdev)media_device_unregister (&vimc->mdev);v4l2_device_unregister (&vimc->v4l2_dev);v4l2_device_put (&vimc->v4l2_dev); +media_device_put (&vimc->mdev); }static void vimc_dev_release (struct device *dev)
Sakari Ailus
maintainerRe: [PATCH v2 23/29] media: vimc: Release resources on media device release
Hi Hans,
Quoted 39 lines · Hans Verkuil
Quoted 38 lines · Sakari Ailus
Hi Hans,
Quoted 2 lines · Hans Verkuil
Quoted 1 line · Sakari Ailus
Release all the resources when the media device is related, moving away
s/related/released/
Quoted 3 lines
Quoted 1 line
form the struct
v4l2_deviceused for that purpose.form -> from
Yes.
Quoted 23 lines
Quoted 12 lines
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/test-drivers/vimc/vimc-core.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)drivers/media/test-drivers/vimc/vimc-core.c +0
-0drivers/media/test-drivers/vimc/vimc-core.c static int vimc_add_subdevs(struct vimc_device *vimc) · line 270
Quoted 9 lines
drivers/media/test-drivers/vimc/vimc-core.c +2
-2@@ -264,13 +264,12 @@ static int vimc_add_subdevs(struct vimc_device *vimc)return 0 ; } -static void vimc_v4l2_dev_release (struct v4l2_device *v4l2_dev) +static void vimc_mdev_release (struct media_device *mdev) {struct vimc_device *vimc = - container_of(v4l2_dev, struct vimc_device, v4l2_dev); + container_of_const(mdev, struct vimc_device, mdev);Why this change?
Quoted 6 lines
Quoted 3 lines
I changed the line already. There's no reason to continue using
container_of()instead ofcontainer_of_const()that takes const-ness into account, too.But neither vimc nor mdev can be const anyway, that would make no sense here.
Neither is const, true. Yet container_of_const() is preferred over container_of(), due to the fact that it does take const-ness into account. container_of() should really be avoided.
I'll add this to the commit message as Laurent suggested.
Signature · 1 line
Sakari Ailus
Re: [PATCH v2 23/29] media: vimc: Release resources on media device release
Quoted 42 lines · Sakari Ailus
Hi Hans,
Quoted 39 lines · Hans Verkuil
Quoted 38 lines · Sakari Ailus
Hi Hans,
Quoted 2 lines · Hans Verkuil
Quoted 1 line · Sakari Ailus
Release all the resources when the media device is related, moving away
s/related/released/
Quoted 3 lines
Quoted 1 line
form the struct
v4l2_deviceused for that purpose.form -> from
Yes.
Quoted 23 lines
Quoted 12 lines
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/test-drivers/vimc/vimc-core.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)drivers/media/test-drivers/vimc/vimc-core.c +0
-0drivers/media/test-drivers/vimc/vimc-core.c static int vimc_add_subdevs(struct vimc_device *vimc) · line 270
Quoted 9 lines
drivers/media/test-drivers/vimc/vimc-core.c +2
-2@@ -264,13 +264,12 @@ static int vimc_add_subdevs(struct vimc_device *vimc)return 0 ; } -static void vimc_v4l2_dev_release (struct v4l2_device *v4l2_dev) +static void vimc_mdev_release (struct media_device *mdev) {struct vimc_device *vimc = - container_of(v4l2_dev, struct vimc_device, v4l2_dev); + container_of_const(mdev, struct vimc_device, mdev);Why this change?
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
I changed the line already. There's no reason to continue using
container_of()instead ofcontainer_of_const()that takes const-ness into account, too.But neither vimc nor mdev can be const anyway, that would make no sense here.
Neither is const, true. Yet
container_of_const()is preferred over
Says who?
It makes sense in generic defines that use it, e.g.:
drivers/base/firmware_loader/sysfs.h:#define to_fw_sysfs(__dev) container_of_const(__dev, struct fw_sysfs, dev)
That way it can handle both const and non-const __dev pointers.
In cases where this doesn't come into play I think there is no need to make code changes. Perhaps when writing new code it might make sense to use it, but changing it in existing code, esp. as part of a patch that deals with something else entirely, seems just unnecessary churn.
I won't block this, but I recommend just dropping this change in this patch.
Regards,
Hans
Quoted 5 lines
container_of(), due to the fact that it does take const-ness into account.container_of()should really be avoided.I'll add this to the commit message as Laurent suggested.
Sakari Ailus
maintainerRe: [PATCH v2 23/29] media: vimc: Release resources on media device release
Hi Hans,
Quoted 47 lines · Hans Verkuil
Quoted 46 lines · Sakari Ailus
Hi Hans,
Quoted 43 lines · Hans Verkuil
Quoted 41 lines · Sakari Ailus
Hi Hans,
Quoted 2 lines · Hans Verkuil
Quoted 1 line · Sakari Ailus
Release all the resources when the media device is related, moving away
s/related/released/
Quoted 3 lines
Quoted 1 line
form the struct
v4l2_deviceused for that purpose.form -> from
Yes.
Quoted 15 lines
Quoted 15 lines
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/test-drivers/vimc/vimc-core.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)drivers/media/test-drivers/vimc/vimc-core.c +0
-0@@ -264,13 +264,12 @@ static int vimc_add_subdevs(struct vimc_device *vimc)return 0 ; }Quoted 8 lines
drivers/media/test-drivers/vimc/vimc-core.c static int vimc_add_subdevs(struct vimc_device *vimc) · line 270
Quoted 6 lines
drivers/media/test-drivers/vimc/vimc-core.c +2
-2-static void vimc_v4l2_dev_release (struct v4l2_device *v4l2_dev) +static void vimc_mdev_release (struct media_device *mdev) {struct vimc_device *vimc = - container_of(v4l2_dev, struct vimc_device, v4l2_dev); + container_of_const(mdev, struct vimc_device, mdev);Why this change?
I changed the line already. There's no reason to continue using
container_of()instead ofcontainer_of_const()that takes const-ness into account, too.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
But neither vimc nor mdev can be const anyway, that would make no sense here.
Neither is const, true. Yet
container_of_const()is preferred overSays who?
container_of() documentation comes with a big, fat warning on this issue.
I can post a patch to add an explicit recommentation, too.
Quoted 13 lines
It makes sense in generic defines that use it, e.g.:
drivers/base/firmware_loader/sysfs.h:#define
to_fw_sysfs(__dev)container_of_const(__dev, struct fw_sysfs, dev)That way it can handle both const and non-const
__devpointers.In cases where this doesn't come into play I think there is no need to make code changes. Perhaps when writing new code it might make sense to use it, but changing it in existing code, esp. as part of a patch that deals with something else entirely, seems just unnecessary churn.
I won't block this, but I recommend just dropping this change in this patch.
Signature · 3 lines
Regards, Sakari Ailus
Sakari Ailus
maintainerPATCH v2 24/29 media: Documentation: Document how Media device resources are released
In reply to Sakari AilusDocument that after unregistering, Media device memory resources are released by the release() callback rather than by calling media_device_cleanup().
Also add that driver memory resources should be bound to the Media device, not V4L2 device.
--- Documentation/driver-api/media/mc-core.rst | 18 ++++++++++++++++-- include/media/media-device.h | 6 ++++-- 2 files changed, 20 insertions(+), 4 deletions(-)
Documentation/driver-api/media/mc-core.rst +16 -2
@@ -46,13 +46,27 @@ Drivers initialise media device instances by calling :c:func:`media_device_init()`. After initialising a media device instance, it is registered by calling :c:func:`__media_device_register()` via the macro ``media_device_register()`` and unregistered by calling -:c:func:`media_device_unregister()`. An initialised media device must be -eventually cleaned up by calling :c:func:`media_device_cleanup()`. +:c:func:`media_device_unregister()`. The resources of an unregistered media +device will be released by the ``release()`` callback of :c:type:`media_device` +ops, which will be called when the last user of the media device has released it +calling :c:func:`media_device_put()`. + +The ``release()`` callback is the way all the resources of the media device are +released once :c:func:`media_device_init()` has been called. This is also +relevant during device driver's probe function as the ``release()`` callback +will also have to be able to safely release the resources related to a partially +initialised media device. Note that it is not allowed to unregister a media device instance that was not previously registered, or clean up a media device instance that was not previously initialised. +Media device and driver's per-device context +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Drivers should use the struct media_device_ops ``release()`` callback to release +their own resources and not e.g. that of the struct v4l2_device. + Entities ^^^^^^^^
include/media/media-device.h +4 -2
@@ -250,8 +250,10 @@ void media_device_init(struct media_device *mdev);* * @mdev: pointer to struct &media_device * -* This function that will destroy the graph_mutex that is -* initialized in media_device_init(). +* This function that will destroy the graph_mutex that is initialized in +* media_device_init(). Note that *only* drivers that do not manage releasing +* the memory of th media device itself call this function. This function is +* thus effectively DEPRECATED. */ void media_device_cleanup (struct media_device *mdev);
Signature · 1 line
2.39.2
Re: [PATCH v2 24/29] media: Documentation: Document how Media device resources are released
Quoted 8 lines · Sakari Ailus
Document that after unregistering, Media device memory resources are released by the
release()callback rather than by callingmedia_device_cleanup().Also add that driver memory resources should be bound to the Media device, not V4L2 device.
Signed-off-by Sakari Ailus <[email protected]>
Quoted 56 lines
--- Documentation/driver-api/media/mc-core.rst | 18 ++++++++++++++++-- include/media/media-device.h | 6 ++++-- 2 files changed, 20 insertions(+), 4 deletions(-)Documentation/driver-api/media/mc-core.rst +16
-2@@ -46,13 +46,27 @@ Drivers initialise media device instances by calling :c:func:`media_device_init()`. After initialising a media device instance, it is registered by calling :c:func:`__media_device_register()` via the macro ``media_device_register()`` and unregistered by calling -:c:func:`media_device_unregister()`. An initialised media device must be -eventually cleaned up by calling :c:func:`media_device_cleanup()`. +:c:func:`media_device_unregister()`. The resources of an unregistered media +device will be released by the ``release()`` callback of :c:type:`media_device` +ops, which will be called when the last user of the media device has released it +calling :c:func:`media_device_put()`. + +The ``release()`` callback is the way all the resources of the media device are +released once :c:func:`media_device_init()` has been called. This is also +relevant during device driver's probe function as the ``release()`` callback +will also have to be able to safely release the resources related to a partially +initialised media device. Note that it is not allowed to unregister a media device instance that was not previously registered, or clean up a media device instance that was not previously initialised. +Media device and driver's per-device context +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Drivers should use the struct media_device_ops ``release()`` callback to release +their own resources and not e.g. that of the struct v4l2_device. + Entities ^^^^^^^^include/media/media-device.h +4
-2@@ -250,8 +250,10 @@ void media_device_init(struct media_device *mdev);* * @mdev: pointer to struct &media_device * -* This function that will destroy the graph_mutex that is -* initialized in media_device_init(). +* This function that will destroy the graph_mutex that is initialized in +* media_device_init(). Note that *only* drivers that do not manage releasing +* the memory of th media device itself call this function. This function is +* thus effectively DEPRECATED. */ void media_device_cleanup (struct media_device *mdev);
Laurent Pinchart
maintainerRe: [PATCH v2 24/29] media: Documentation: Document how Media device resources are released
In reply to Sakari AilusHi Sakari,
Thank you for the patch.
Quoted 18 lines · Sakari Ailus
Document that after unregistering, Media device memory resources are released by the
release()callback rather than by callingmedia_device_cleanup().Also add that driver memory resources should be bound to the Media device, not V4L2 device.
Signed-off-by Sakari Ailus <[email protected]>--- Documentation/driver-api/media/mc-core.rst | 18 ++++++++++++++++-- include/media/media-device.h | 6 ++++-- 2 files changed, 20 insertions(+), 4 deletions(-)Documentation/driver-api/media/mc-core.rst +0
-0
Documentation/driver-api/media/mc-core.rst Drivers initialise media device instances by calling · line 49
Quoted 6 lines
Documentation/driver-api/media/mc-core.rst +1
-2@@ -46,13 +46,27 @@ Drivers initialise media device instances by calling :c:func:`media_device_init()`. After initialising a media device instance, it is registered by calling :c:func:`__media_device_register()` via the macro ``media_device_register()`` and unregistered by calling -:c:func:`media_device_unregister()`. An initialised media device must be -eventually cleaned up by calling :c:func:`media_device_cleanup()`. +:c:func:`media_device_unregister()`. The resources of an unregistered media
"of an unregistered media device" sounds weird here, I interpret it as applying only to media devices that have never been registered.
Documentation/driver-api/media/mc-core.rst Drivers initialise media device instances by calling · line 62
Quoted 13 lines
Documentation/driver-api/media/mc-core.rst +9
-0+device will be released by the ``release()`` callback of :c:type:`media_device` +ops, which will be called when the last user of the media device has released it +calling :c:func:`media_device_put()`. + +The ``release()`` callback is the way all the resources of the media device are +released once :c:func:`media_device_init()` has been called. This is also +relevant during device driver's probe function as the ``release()`` callback +will also have to be able to safely release the resources related to a partially +initialised media device. Note that it is not allowed to unregister a media device instance that was not previously registered, or clean up a media device instance that was not previously initialised.
Does this need an update, as we don't cleanup explicitly instead ?
Quoted 15 lines
Documentation/driver-api/media/mc-core.rst +6
-0+Media device and driver's per-device context +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Drivers should use the struct media_device_ops ``release()`` callback to release +their own resources and not e.g. that of the struct v4l2_device. + Entities ^^^^^^^^include/media/media-device.h +0
-0
include/media/media-device.h void media_device_init(struct media_device *mdev); · line 253
Quoted 6 lines
include/media/media-device.h +1
-2@@ -250,8 +250,10 @@ void media_device_init(struct media_device *mdev);* * @mdev: pointer to struct &media_device * -* This function that will destroy the graph_mutex that is -* initialized in media_device_init(). +* This function that will destroy the graph_mutex that is initialized in
While at it, s/that will/will/
include/media/media-device.h void media_device_init(struct media_device *mdev); · line 255
Quoted 2 lines
include/media/media-device.h +2
-0+* media_device_init(). Note that *only* drivers that do not manage releasing +* the memory of th media device itself call this function. This function is
s/of th/of the/
"that do not manage releasing the memory of the media device itself" is hard to understand for someone who hasn't paid close attention to the development of this series. This text needs improvements.
Quoted 4 lines
include/media/media-device.h +1
-0+* thus effectively DEPRECATED. */ void media_device_cleanup (struct media_device *mdev);
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerRe: [PATCH v2 24/29] media: Documentation: Document how Media device resources are released
Hi Laurent,
Quoted 18 lines · Laurent Pinchart
Hi Sakari,
Thank you for the patch.
Quoted 13 lines · Sakari Ailus
Document that after unregistering, Media device memory resources are released by the
release()callback rather than by callingmedia_device_cleanup().Also add that driver memory resources should be bound to the Media device, not V4L2 device.
Signed-off-by Sakari Ailus <[email protected]>--- Documentation/driver-api/media/mc-core.rst | 18 ++++++++++++++++-- include/media/media-device.h | 6 ++++-- 2 files changed, 20 insertions(+), 4 deletions(-)
Quoted 14 lines
Documentation/driver-api/media/mc-core.rst Drivers initialise media device instances by calling · line 49
Quoted 11 lines
Documentation/driver-api/media/mc-core.rst +1
-2@@ -46,13 +46,27 @@ Drivers initialise media device instances by calling :c:func:`media_device_init()`. After initialising a media device instance, it is registered by calling :c:func:`__media_device_register()` via the macro ``media_device_register()`` and unregistered by calling -:c:func:`media_device_unregister()`. An initialised media device must be -eventually cleaned up by calling :c:func:`media_device_cleanup()`. +:c:func:`media_device_unregister()`. The resources of an unregistered media"of an unregistered media device" sounds weird here, I interpret it as applying only to media devices that have never been registered.
How about "newly unregistered"?
Quoted 11 lines
Quoted 11 lines
Documentation/driver-api/media/mc-core.rst +9
-0+device will be released by the ``release()`` callback of :c:type:`media_device` +ops, which will be called when the last user of the media device has released it +calling :c:func:`media_device_put()`. + +The ``release()`` callback is the way all the resources of the media device are +released once :c:func:`media_device_init()` has been called. This is also +relevant during device driver's probe function as the ``release()`` callback +will also have to be able to safely release the resources related to a partially +initialised media device.
Quoted 5 lines
Documentation/driver-api/media/mc-core.rst Drivers initialise media device instances by calling · line 62
Quoted 3 lines
Documentation/driver-api/media/mc-core.rst +0
-0Note that it is not allowed to unregister a media device instance that was not previously registered, or clean up a media device instance that was not previously initialised.Does this need an update, as we don't cleanup explicitly instead ?
I think this should remain unchanged, at least as long as there are drivers using the old API.
Quoted 11 lines
Quoted 11 lines
Documentation/driver-api/media/mc-core.rst +6
-0+Media device and driver's per-device context +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Drivers should use the struct media_device_ops ``release()`` callback to release +their own resources and not e.g. that of the struct v4l2_device. + Entities ^^^^^^^^
Quoted 13 lines
include/media/media-device.h void media_device_init(struct media_device *mdev); · line 253
Quoted 11 lines
include/media/media-device.h +1
-2@@ -250,8 +250,10 @@ void media_device_init(struct media_device *mdev);* * @mdev: pointer to struct &media_device * -* This function that will destroy the graph_mutex that is -* initialized in media_device_init(). +* This function that will destroy the graph_mutex that is initialized in While at it, s/that will/will/
Yes.
Quoted 9 lines
include/media/media-device.h void media_device_init(struct media_device *mdev); · line 255
Quoted 3 lines
include/media/media-device.h +2
-0+* media_device_init(). Note that *only* drivers that do not manage releasing +* the memory of th media device itself call this function. This function is s/of th/of the/
"that do not manage releasing the memory of the media device itself" is hard to understand for someone who hasn't paid close attention to the development of this series. This text needs improvements.
Hmm. How about:
Note that only drivers that do not have a proper release callback of the struct media_device call this function.
Quoted 6 lines
Quoted 5 lines
include/media/media-device.h +1
-0+* thus effectively DEPRECATED. */ void media_device_cleanup (struct media_device *mdev);
Signature · 3 lines
Regards, Sakari Ailus
Sakari Ailus
maintainerPATCH v2 25/29 media: mc: Add per-file-handle data support
In reply to Sakari AilusFrom: Laurent Pinchart <[email protected]>
The media devnode core associates devnodes with files by storing the devnode pointer in the file structure private_data field. In order to allow tracking of per-file-handle data introduce a new media devnode file handle structure that stores the devnode pointer, and store a pointer to that structure in the file private_data field.
Users of the media devnode code (the only existing user being media_device) are responsible for managing their own subclass of the media_devnode_fh structure.
Prepare struct media_device_fh to be used for maintaining file handle list to avoid shuffling things here and there right after.
--- drivers/media/mc/mc-device.c | 14 +++++++++++++- drivers/media/mc/mc-devnode.c | 20 +++++++++----------- include/media/media-device.h | 7 +++++++ include/media/media-devnode.h | 18 +++++++++++++++++- include/media/media-fh.h | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 include/media/media-fh.h
drivers/media/mc/mc-device.c +13 -1
@@ -22,6 +22,7 @@#include <media/media-device.h> #include <media/media-devnode.h> #include <media/media-entity.h> +#include <media/media-fh.h> #include <media/media-request.h> #ifdef CONFIG_MEDIA_CONTROLLER
@@ -35,7 +36,6 @@#define MEDIA_ENT_SUBTYPE_MASK 0x0000ffff#define MEDIA_ENT_T_DEVNODE_UNKNOWN (MEDIA_ENT_F_OLD_BASE | \ MEDIA_ENT_SUBTYPE_MASK) -/* ----------------------------------------------------------------------------- * Userspace API */
@@ -47,11 +47,23 @@ static inline void __user *media_get_uptr(__u64 arg)static int media_device_open (struct file *filp) { +struct media_device_fh *fh; + + fh =kzalloc (sizeof (*fh),GFP_KERNEL ); +if (!fh) +return -ENOMEM ; + + filp->private_data = &fh->fh; +return 0 ; }static int media_device_close (struct file *filp) { +struct media_device_fh *fh =media_device_fh (filp); + +kfree (fh); +return 0 ; }
drivers/media/mc/mc-devnode.c +9 -11
@@ -150,6 +150,7 @@ static long media_compat_ioctl(struct file *filp, unsigned int cmd,static int media_open (struct inode *inode,struct file *filp) {struct media_devnode *devnode; +struct media_devnode_fh *fh;int ret;/* Check if the media device is available. This needs to be done with
@@ -170,17 +171,15 @@ static int media_open(struct inode *inode, struct file *filp)get_device (&devnode->dev);mutex_unlock (&media_devnode_lock); - filp->private_data = devnode; - -if (devnode->fops->open) { - ret = devnode->fops->open (filp); -if (ret) { -put_device (&devnode->dev); - filp->private_data =NULL ; -return ret; - } + ret = devnode->fops->open (filp); +if (ret) { +put_device (&devnode->dev); +return ret; } + fh = filp->private_data; + fh->devnode = devnode; +return 0 ; }
@@ -189,8 +188,7 @@ static int media_release(struct inode *inode, struct file *filp) {struct media_devnode *devnode =media_devnode_data (filp); -if (devnode->fops->release) - devnode->fops->release (filp); + devnode->fops->release (filp); filp->private_data =NULL ;
include/media/media-device.h +7 -0
@@ -110,6 +110,10 @@ struct media_device_ops {* other operations that stop or start streaming. * @request_id: Used to generate unique request IDs * +* @fh_list: List of file handles in the media device +* (struct media_device_fh.mdev_list). +* @fh_list_lock: Serialise access to fh_list list. +* * This structure represents an abstract high-level media device. It allows easy * access to entities and provides basic media device-level support. The * structure can be allocated directly or embedded in a larger structure.
@@ -182,6 +186,9 @@ struct media_device {struct mutex req_queue_mutex;atomic_t request_id; + +struct list_head fh_list; +spinlock_t fh_list_lock; };/* We don't need to include usb.h here */
include/media/media-devnode.h +17 -1
@@ -53,6 +53,20 @@ struct media_file_operations {int (*release) (struct file *); }; +/** +* struct media_devnode_fh - Media device node file handle +* @devnode: pointer to the media device node +* +* This structure serves as a base for per-file-handle data storage. Media +* device node users embed media_devnode_fh in their custom file handle data +* structures and store the media_devnode_fh in the file private_data in order +* to let the media device node core locate the media_devnode corresponding to a +* file handle. +*/ +struct media_devnode_fh { +struct media_devnode *devnode; +}; +/** * struct media_devnode - Media device node * @media_dev: pointer to struct &media_device
@@ -137,7 +151,9 @@ void media_devnode_unregister(struct media_devnode *devnode);*/ static inline struct media_devnode *media_devnode_data (struct file *filp) { -return filp->private_data; +struct media_devnode_fh *fh = filp->private_data; + +return fh->devnode; }/**
include/media/media-fh.h new file +32 -0
@@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Media device file handle +* +* Copyright (C) 2019--2023 Intel Corporation +*/ + +#ifndef MEDIA_FH_H +#define MEDIA_FH_H + +#include <linux/list.h> +#include <linux/file.h> + +#include <media/media-devnode.h> + +/** +* struct media_device_fh - File handle specific information on MC +* +* @fh: The media device file handle +* @mdev_list: This file handle in media device's list of file handles +*/ +struct media_device_fh { +struct media_devnode_fh fh; +struct list_head mdev_list; +}; + +static inline struct media_device_fh *media_device_fh (struct file *filp) +{ + return container_of(filp->private_data, struct media_device_fh, fh); +} + +#endif /* MEDIA_FH_H */
Signature · 1 line
2.39.2
Re: [PATCH v2 25/29] media: mc: Add per-file-handle data support
Quoted 12 lines · Sakari Ailus
From: Laurent Pinchart <[email protected]>
The media devnode core associates devnodes with files by storing the devnode pointer in the file structure
private_datafield. In order to allow tracking of per-file-handle data introduce a new media devnode file handle structure that stores the devnode pointer, and store a pointer to that structure in the fileprivate_datafield.Users of the media devnode code (the only existing user being
media_device) are responsible for managing their own subclass of themedia_devnode_fhstructure.
Quoted 6 lines
Signed-off-by Laurent Pinchart <[email protected]>Prepare struct
media_device_fhto be used for maintaining file handle list to avoid shuffling things here and there right after.Signed-off-by Sakari Ailus <[email protected]>
Quoted 199 lines
--- drivers/media/mc/mc-device.c | 14 +++++++++++++- drivers/media/mc/mc-devnode.c | 20 +++++++++----------- include/media/media-device.h | 7 +++++++ include/media/media-devnode.h | 18 +++++++++++++++++- include/media/media-fh.h | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 include/media/media-fh.hdrivers/media/mc/mc-device.c +13
-1@@ -22,6 +22,7 @@#include <media/media-device.h> #include <media/media-devnode.h> #include <media/media-entity.h> +#include <media/media-fh.h> #include <media/media-request.h> #ifdef CONFIG_MEDIA_CONTROLLER @@ -35,7 +36,6 @@#define MEDIA_ENT_SUBTYPE_MASK 0x0000ffff#define MEDIA_ENT_T_DEVNODE_UNKNOWN (MEDIA_ENT_F_OLD_BASE | \ MEDIA_ENT_SUBTYPE_MASK) -/* ----------------------------------------------------------------------------- * Userspace API */ @@ -47,11 +47,23 @@ static inline void __user *media_get_uptr(__u64 arg)static int media_device_open (struct file *filp) { +struct media_device_fh *fh; + + fh =kzalloc (sizeof (*fh),GFP_KERNEL ); +if (!fh) +return -ENOMEM ; + + filp->private_data = &fh->fh; +return 0 ; }static int media_device_close (struct file *filp) { +struct media_device_fh *fh =media_device_fh (filp); + +kfree (fh); +return 0 ; }drivers/media/mc/mc-devnode.c +9
-11@@ -150,6 +150,7 @@ static long media_compat_ioctl(struct file *filp, unsigned int cmd,static int media_open (struct inode *inode,struct file *filp) {struct media_devnode *devnode; +struct media_devnode_fh *fh;int ret;/* Check if the media device is available. This needs to be done with @@ -170,17 +171,15 @@ static int media_open(struct inode *inode, struct file *filp)get_device (&devnode->dev);mutex_unlock (&media_devnode_lock); - filp->private_data = devnode; - -if (devnode->fops->open) { - ret = devnode->fops->open (filp); -if (ret) { -put_device (&devnode->dev); - filp->private_data =NULL ; -return ret; - } + ret = devnode->fops->open (filp); +if (ret) { +put_device (&devnode->dev); +return ret; } + fh = filp->private_data; + fh->devnode = devnode; +return 0 ; }@@ -189,8 +188,7 @@ static int media_release(struct inode *inode, struct file *filp) {struct media_devnode *devnode =media_devnode_data (filp); -if (devnode->fops->release) - devnode->fops->release (filp); + devnode->fops->release (filp); filp->private_data =NULL ;include/media/media-device.h +7
-0@@ -110,6 +110,10 @@ struct media_device_ops {* other operations that stop or start streaming. * @request_id: Used to generate unique request IDs * +* @fh_list: List of file handles in the media device +* (struct media_device_fh.mdev_list). +* @fh_list_lock: Serialise access to fh_list list. +* * This structure represents an abstract high-level media device. It allows easy * access to entities and provides basic media device-level support. The * structure can be allocated directly or embedded in a larger structure. @@ -182,6 +186,9 @@ struct media_device {struct mutex req_queue_mutex;atomic_t request_id; + +struct list_head fh_list; +spinlock_t fh_list_lock; };/* We don't need to include usb.h here */ include/media/media-devnode.h +17
-1@@ -53,6 +53,20 @@ struct media_file_operations {int (*release) (struct file *); }; +/** +* struct media_devnode_fh - Media device node file handle +* @devnode: pointer to the media device node +* +* This structure serves as a base for per-file-handle data storage. Media +* device node users embed media_devnode_fh in their custom file handle data +* structures and store the media_devnode_fh in the file private_data in order +* to let the media device node core locate the media_devnode corresponding to a +* file handle. +*/ +struct media_devnode_fh { +struct media_devnode *devnode; +}; +/** * struct media_devnode - Media device node * @media_dev: pointer to struct &media_device @@ -137,7 +151,9 @@ void media_devnode_unregister(struct media_devnode *devnode);*/ static inline struct media_devnode *media_devnode_data (struct file *filp) { -return filp->private_data; +struct media_devnode_fh *fh = filp->private_data; + +return fh->devnode; }/** include/media/media-fh.h new file +32
-0@@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Media device file handle +* +* Copyright (C) 2019--2023 Intel Corporation +*/ + +#ifndef MEDIA_FH_H +#define MEDIA_FH_H + +#include <linux/list.h> +#include <linux/file.h> + +#include <media/media-devnode.h> + +/** +* struct media_device_fh - File handle specific information on MC +* +* @fh: The media device file handle +* @mdev_list: This file handle in media device's list of file handles +*/ +struct media_device_fh { +struct media_devnode_fh fh; +struct list_head mdev_list; +}; + +static inline struct media_device_fh *media_device_fh (struct file *filp) +{ + return container_of(filp->private_data, struct media_device_fh, fh); +} + +#endif /* MEDIA_FH_H */
Sakari Ailus
maintainerPATCH v2 26/29 media: mc: Maintain a list of open file handles in a media device
In reply to Sakari AilusThe list of file handles is needed to deliver media events as well as for other purposes in the future.
--- drivers/media/mc/mc-device.c | 23 ++++++++++++++++++++++- drivers/media/mc/mc-devnode.c | 2 +- include/media/media-devnode.h | 4 +++- 3 files changed, 26 insertions(+), 3 deletions(-)
drivers/media/mc/mc-device.c +22 -1
@@ -45,9 +45,11 @@ static inline void __user *media_get_uptr(__u64 arg)return (void __user *)(uintptr_t )arg; } -static int media_device_open (struct file *filp) +static int media_device_open (struct media_devnode *devnode,struct file *filp) { +struct media_device *mdev =to_media_device (devnode);struct media_device_fh *fh; +unsigned long flags; fh =kzalloc (sizeof (*fh),GFP_KERNEL );if (!fh)
@@ -55,12 +57,23 @@ static int media_device_open(struct file *filp) filp->private_data = &fh->fh; +spin_lock_irqsave (&mdev->fh_list_lock, flags); +list_add (&fh->mdev_list, &mdev->fh_list); +spin_unlock_irqrestore (&mdev->fh_list_lock, flags); +return 0 ; }static int media_device_close (struct file *filp) { +struct media_devnode *devnode =media_devnode_data (filp); +struct media_device *mdev =to_media_device (devnode);struct media_device_fh *fh =media_device_fh (filp); +unsigned long flags; + +spin_lock_irqsave (&mdev->fh_list_lock, flags); +list_del (&fh->mdev_list); +spin_unlock_irqrestore (&mdev->fh_list_lock, flags);kfree (fh);
@@ -769,11 +782,13 @@ void media_device_init(struct media_device *mdev)INIT_LIST_HEAD (&mdev->pads);INIT_LIST_HEAD (&mdev->links);INIT_LIST_HEAD (&mdev->entity_notify); +INIT_LIST_HEAD (&mdev->fh_list);mutex_init (&mdev->req_queue_mutex);mutex_init (&mdev->graph_mutex);ida_init (&mdev->entity_internal_idx);atomic_set (&mdev->request_id,0 ); +spin_lock_init (&mdev->fh_list_lock); mdev->devnode.release = media_device_release;media_devnode_init (&mdev->devnode);
@@ -824,6 +839,8 @@ EXPORT_SYMBOL_GPL(__media_device_register);void media_device_unregister (struct media_device *mdev) { +unsigned long flags; +if (mdev ==NULL )return ;
@@ -834,6 +851,10 @@ void media_device_unregister(struct media_device *mdev) }mutex_unlock (&mdev->graph_mutex); +spin_lock_irqsave (&mdev->fh_list_lock, flags); +list_del_init (&mdev->fh_list); +spin_unlock_irqrestore (&mdev->fh_list_lock, flags); +device_remove_file (&mdev->devnode.dev, &dev_attr_model);dev_dbg (mdev->dev,"Media device unregistering\n" );media_devnode_unregister (&mdev->devnode);
drivers/media/mc/mc-devnode.c +1 -1
@@ -171,7 +171,7 @@ static int media_open(struct inode *inode, struct file *filp)get_device (&devnode->dev);mutex_unlock (&media_devnode_lock); - ret = devnode->fops->open (filp); + ret = devnode->fops->open (devnode, filp);if (ret) {put_device (&devnode->dev);return ret;
include/media/media-devnode.h +3 -1
@@ -21,6 +21,8 @@#include <linux/device.h> #include <linux/cdev.h> +struct media_devnode ; +/* * Flag to mark the media_devnode struct as registered. Drivers must not touch * this flag directly, it will be set and cleared by media_devnode_register and
@@ -49,7 +51,7 @@ struct media_file_operations {__poll_t (*poll) (struct file *,struct poll_table_struct *);long (*ioctl) (struct file *,unsigned int ,unsigned long );long (*compat_ioctl) (struct file *,unsigned int ,unsigned long ); -int (*open) (struct file *); +int (*open) (struct media_devnode *,struct file *);int (*release) (struct file *); };
Signature · 1 line
2.39.2
Re: [PATCH v2 26/29] media: mc: Maintain a list of open file handles in a media device
Quoted 28 lines · Sakari Ailus
The list of file handles is needed to deliver media events as well as for other purposes in the future.
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/mc/mc-device.c | 23 ++++++++++++++++++++++- drivers/media/mc/mc-devnode.c | 2 +- include/media/media-devnode.h | 4 +++- 3 files changed, 26 insertions(+), 3 deletions(-)drivers/media/mc/mc-device.c +3
-1@@ -45,9 +45,11 @@ static inline void __user *media_get_uptr(__u64 arg)return (void __user *)(uintptr_t )arg; } -static int media_device_open (struct file *filp) +static int media_device_open (struct media_devnode *devnode,struct file *filp) { +struct media_device *mdev =to_media_device (devnode);struct media_device_fh *fh; +unsigned long flags; fh =kzalloc (sizeof (*fh),GFP_KERNEL );if (!fh)
drivers/media/mc/mc-device.c static int media_device_open(struct file *filp) · line 60
Quoted 4 lines
drivers/media/mc/mc-device.c +1
-0@@ -55,12 +57,23 @@ static int media_device_open(struct file *filp) filp->private_data = &fh->fh; +spin_lock_irqsave (&mdev->fh_list_lock, flags);
The only reason for using the irqsave variant is because we want to support events in the future, and those can be sent in irq context.
But it is confusing to see this being used here when it is not yet needed.
At minimum this should be explained in the commit log.
Regards,
Hans
Quoted 88 lines
drivers/media/mc/mc-device.c +18
-0+list_add (&fh->mdev_list, &mdev->fh_list); +spin_unlock_irqrestore (&mdev->fh_list_lock, flags); +return 0 ; }static int media_device_close (struct file *filp) { +struct media_devnode *devnode =media_devnode_data (filp); +struct media_device *mdev =to_media_device (devnode);struct media_device_fh *fh =media_device_fh (filp); +unsigned long flags; + +spin_lock_irqsave (&mdev->fh_list_lock, flags); +list_del (&fh->mdev_list); +spin_unlock_irqrestore (&mdev->fh_list_lock, flags);kfree (fh);@@ -769,11 +782,13 @@ void media_device_init(struct media_device *mdev)INIT_LIST_HEAD (&mdev->pads);INIT_LIST_HEAD (&mdev->links);INIT_LIST_HEAD (&mdev->entity_notify); +INIT_LIST_HEAD (&mdev->fh_list);mutex_init (&mdev->req_queue_mutex);mutex_init (&mdev->graph_mutex);ida_init (&mdev->entity_internal_idx);atomic_set (&mdev->request_id,0 ); +spin_lock_init (&mdev->fh_list_lock); mdev->devnode.release = media_device_release;media_devnode_init (&mdev->devnode);@@ -824,6 +839,8 @@ EXPORT_SYMBOL_GPL(__media_device_register);void media_device_unregister (struct media_device *mdev) { +unsigned long flags; +if (mdev ==NULL )return ;@@ -834,6 +851,10 @@ void media_device_unregister(struct media_device *mdev) }mutex_unlock (&mdev->graph_mutex); +spin_lock_irqsave (&mdev->fh_list_lock, flags); +list_del_init (&mdev->fh_list); +spin_unlock_irqrestore (&mdev->fh_list_lock, flags); +device_remove_file (&mdev->devnode.dev, &dev_attr_model);dev_dbg (mdev->dev,"Media device unregistering\n" );media_devnode_unregister (&mdev->devnode);drivers/media/mc/mc-devnode.c +1
-1@@ -171,7 +171,7 @@ static int media_open(struct inode *inode, struct file *filp)get_device (&devnode->dev);mutex_unlock (&media_devnode_lock); - ret = devnode->fops->open (filp); + ret = devnode->fops->open (devnode, filp);if (ret) {put_device (&devnode->dev);return ret;include/media/media-devnode.h +3
-1@@ -21,6 +21,8 @@#include <linux/device.h> #include <linux/cdev.h> +struct media_devnode ; +/* * Flag to mark the media_devnode struct as registered. Drivers must not touch * this flag directly, it will be set and cleared by media_devnode_register and @@ -49,7 +51,7 @@ struct media_file_operations {__poll_t (*poll) (struct file *,struct poll_table_struct *);long (*ioctl) (struct file *,unsigned int ,unsigned long );long (*compat_ioctl) (struct file *,unsigned int ,unsigned long ); -int (*open) (struct file *); +int (*open) (struct media_devnode *,struct file *);int (*release) (struct file *); };
Laurent Pinchart
maintainerRe: [PATCH v2 26/29] media: mc: Maintain a list of open file handles in a media device
Quoted 30 lines · Hans Verkuil
Quoted 29 lines · Sakari Ailus
The list of file handles is needed to deliver media events as well as for other purposes in the future.
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/mc/mc-device.c | 23 ++++++++++++++++++++++- drivers/media/mc/mc-devnode.c | 2 +- include/media/media-devnode.h | 4 +++- 3 files changed, 26 insertions(+), 3 deletions(-)drivers/media/mc/mc-device.c +3
-1@@ -45,9 +45,11 @@ static inline void __user *media_get_uptr(__u64 arg)return (void __user *)(uintptr_t )arg; } -static int media_device_open (struct file *filp) +static int media_device_open (struct media_devnode *devnode,struct file *filp) { +struct media_device *mdev =to_media_device (devnode);struct media_device_fh *fh; +unsigned long flags; fh =kzalloc (sizeof (*fh),GFP_KERNEL );if (!fh)@@ -55,12 +57,23 @@ static int media_device_open(struct file *filp)
Quoted 6 lines
drivers/media/mc/mc-device.c static int media_device_open(struct file *filp) · line 60
Quoted 3 lines
drivers/media/mc/mc-device.c +1
-0filp->private_data = &fh->fh; +spin_lock_irqsave (&mdev->fh_list_lock, flags);The only reason for using the irqsave variant is because we want to support events in the future, and those can be sent in irq context.
Even in that case, would media_device_open() ever be called from interrupt context ? spin_lock_irqsave() is only needed if you don't know which context the function can be called from. If we know we'll be called from interruptible context only, you can use spin_lock_irq() instead.
Quoted 93 lines
But it is confusing to see this being used here when it is not yet needed.
At minimum this should be explained in the commit log.
Quoted 88 lines
drivers/media/mc/mc-device.c +18
-0+list_add (&fh->mdev_list, &mdev->fh_list); +spin_unlock_irqrestore (&mdev->fh_list_lock, flags); +return 0 ; }static int media_device_close (struct file *filp) { +struct media_devnode *devnode =media_devnode_data (filp); +struct media_device *mdev =to_media_device (devnode);struct media_device_fh *fh =media_device_fh (filp); +unsigned long flags; + +spin_lock_irqsave (&mdev->fh_list_lock, flags); +list_del (&fh->mdev_list); +spin_unlock_irqrestore (&mdev->fh_list_lock, flags);kfree (fh);@@ -769,11 +782,13 @@ void media_device_init(struct media_device *mdev)INIT_LIST_HEAD (&mdev->pads);INIT_LIST_HEAD (&mdev->links);INIT_LIST_HEAD (&mdev->entity_notify); +INIT_LIST_HEAD (&mdev->fh_list);mutex_init (&mdev->req_queue_mutex);mutex_init (&mdev->graph_mutex);ida_init (&mdev->entity_internal_idx);atomic_set (&mdev->request_id,0 ); +spin_lock_init (&mdev->fh_list_lock); mdev->devnode.release = media_device_release;media_devnode_init (&mdev->devnode);@@ -824,6 +839,8 @@ EXPORT_SYMBOL_GPL(__media_device_register);void media_device_unregister (struct media_device *mdev) { +unsigned long flags; +if (mdev ==NULL )return ;@@ -834,6 +851,10 @@ void media_device_unregister(struct media_device *mdev) }mutex_unlock (&mdev->graph_mutex); +spin_lock_irqsave (&mdev->fh_list_lock, flags); +list_del_init (&mdev->fh_list); +spin_unlock_irqrestore (&mdev->fh_list_lock, flags); +device_remove_file (&mdev->devnode.dev, &dev_attr_model);dev_dbg (mdev->dev,"Media device unregistering\n" );media_devnode_unregister (&mdev->devnode);drivers/media/mc/mc-devnode.c +1
-1@@ -171,7 +171,7 @@ static int media_open(struct inode *inode, struct file *filp)get_device (&devnode->dev);mutex_unlock (&media_devnode_lock); - ret = devnode->fops->open (filp); + ret = devnode->fops->open (devnode, filp);if (ret) {put_device (&devnode->dev);return ret;include/media/media-devnode.h +3
-1@@ -21,6 +21,8 @@#include <linux/device.h> #include <linux/cdev.h> +struct media_devnode ; +/* * Flag to mark the media_devnode struct as registered. Drivers must not touch * this flag directly, it will be set and cleared by media_devnode_register and @@ -49,7 +51,7 @@ struct media_file_operations {__poll_t (*poll) (struct file *,struct poll_table_struct *);long (*ioctl) (struct file *,unsigned int ,unsigned long );long (*compat_ioctl) (struct file *,unsigned int ,unsigned long ); -int (*open) (struct file *); +int (*open) (struct media_devnode *,struct file *);int (*release) (struct file *); };
Signature · 3 lines
Regards, Laurent Pinchart
Re: [PATCH v2 26/29] media: mc: Maintain a list of open file handles in a media device
Quoted 38 lines · Laurent Pinchart
Quoted 30 lines · Hans Verkuil
Quoted 29 lines · Sakari Ailus
The list of file handles is needed to deliver media events as well as for other purposes in the future.
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/mc/mc-device.c | 23 ++++++++++++++++++++++- drivers/media/mc/mc-devnode.c | 2 +- include/media/media-devnode.h | 4 +++- 3 files changed, 26 insertions(+), 3 deletions(-)drivers/media/mc/mc-device.c +3
-1@@ -45,9 +45,11 @@ static inline void __user *media_get_uptr(__u64 arg)return (void __user *)(uintptr_t )arg; } -static int media_device_open (struct file *filp) +static int media_device_open (struct media_devnode *devnode,struct file *filp) { +struct media_device *mdev =to_media_device (devnode);struct media_device_fh *fh; +unsigned long flags; fh =kzalloc (sizeof (*fh),GFP_KERNEL );if (!fh)@@ -55,12 +57,23 @@ static int media_device_open(struct file *filp)Quoted 6 lines
drivers/media/mc/mc-device.c static int media_device_open(struct file *filp) · line 60
Quoted 3 lines
drivers/media/mc/mc-device.c +1
-0filp->private_data = &fh->fh; +spin_lock_irqsave (&mdev->fh_list_lock, flags);The only reason for using the irqsave variant is because we want to support events in the future, and those can be sent in irq context.
Quoted 5 lines
Even in that case, would
media_device_open()ever be called from interrupt context ?spin_lock_irqsave()is only needed if you don't know which context the function can be called from. If we know we'll be called from interruptible context only, you can usespin_lock_irq()instead.
Someone can call open() while at the same time the kernel sends a media event from interrupt context. Such an event function will walk over the fh_list. The irqsave here is meant to ensure that no event interrupt can run while we add our fh to the fh list.
But without interrupts that access fh_list the irqsave variant is confusing.
Regards,
Hans
Quoted 95 lines
Quoted 94 lines
But it is confusing to see this being used here when it is not yet needed.
At minimum this should be explained in the commit log.
Quoted 88 lines
drivers/media/mc/mc-device.c +18
-0+list_add (&fh->mdev_list, &mdev->fh_list); +spin_unlock_irqrestore (&mdev->fh_list_lock, flags); +return 0 ; }static int media_device_close (struct file *filp) { +struct media_devnode *devnode =media_devnode_data (filp); +struct media_device *mdev =to_media_device (devnode);struct media_device_fh *fh =media_device_fh (filp); +unsigned long flags; + +spin_lock_irqsave (&mdev->fh_list_lock, flags); +list_del (&fh->mdev_list); +spin_unlock_irqrestore (&mdev->fh_list_lock, flags);kfree (fh);@@ -769,11 +782,13 @@ void media_device_init(struct media_device *mdev)INIT_LIST_HEAD (&mdev->pads);INIT_LIST_HEAD (&mdev->links);INIT_LIST_HEAD (&mdev->entity_notify); +INIT_LIST_HEAD (&mdev->fh_list);mutex_init (&mdev->req_queue_mutex);mutex_init (&mdev->graph_mutex);ida_init (&mdev->entity_internal_idx);atomic_set (&mdev->request_id,0 ); +spin_lock_init (&mdev->fh_list_lock); mdev->devnode.release = media_device_release;media_devnode_init (&mdev->devnode);@@ -824,6 +839,8 @@ EXPORT_SYMBOL_GPL(__media_device_register);void media_device_unregister (struct media_device *mdev) { +unsigned long flags; +if (mdev ==NULL )return ;@@ -834,6 +851,10 @@ void media_device_unregister(struct media_device *mdev) }mutex_unlock (&mdev->graph_mutex); +spin_lock_irqsave (&mdev->fh_list_lock, flags); +list_del_init (&mdev->fh_list); +spin_unlock_irqrestore (&mdev->fh_list_lock, flags); +device_remove_file (&mdev->devnode.dev, &dev_attr_model);dev_dbg (mdev->dev,"Media device unregistering\n" );media_devnode_unregister (&mdev->devnode);drivers/media/mc/mc-devnode.c +1
-1@@ -171,7 +171,7 @@ static int media_open(struct inode *inode, struct file *filp)get_device (&devnode->dev);mutex_unlock (&media_devnode_lock); - ret = devnode->fops->open (filp); + ret = devnode->fops->open (devnode, filp);if (ret) {put_device (&devnode->dev);return ret;include/media/media-devnode.h +3
-1@@ -21,6 +21,8 @@#include <linux/device.h> #include <linux/cdev.h> +struct media_devnode ; +/* * Flag to mark the media_devnode struct as registered. Drivers must not touch * this flag directly, it will be set and cleared by media_devnode_register and @@ -49,7 +51,7 @@ struct media_file_operations {__poll_t (*poll) (struct file *,struct poll_table_struct *);long (*ioctl) (struct file *,unsigned int ,unsigned long );long (*compat_ioctl) (struct file *,unsigned int ,unsigned long ); -int (*open) (struct file *); +int (*open) (struct media_devnode *,struct file *);int (*release) (struct file *); };
Laurent Pinchart
maintainerRe: [PATCH v2 26/29] media: mc: Maintain a list of open file handles in a media device
Quoted 45 lines · Hans Verkuil
Quoted 38 lines · Laurent Pinchart
Quoted 30 lines · Hans Verkuil
Quoted 29 lines · Sakari Ailus
The list of file handles is needed to deliver media events as well as for other purposes in the future.
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/mc/mc-device.c | 23 ++++++++++++++++++++++- drivers/media/mc/mc-devnode.c | 2 +- include/media/media-devnode.h | 4 +++- 3 files changed, 26 insertions(+), 3 deletions(-)drivers/media/mc/mc-device.c +3
-1@@ -45,9 +45,11 @@ static inline void __user *media_get_uptr(__u64 arg)return (void __user *)(uintptr_t )arg; } -static int media_device_open (struct file *filp) +static int media_device_open (struct media_devnode *devnode,struct file *filp) { +struct media_device *mdev =to_media_device (devnode);struct media_device_fh *fh; +unsigned long flags; fh =kzalloc (sizeof (*fh),GFP_KERNEL );if (!fh)@@ -55,12 +57,23 @@ static int media_device_open(struct file *filp)Quoted 6 lines
drivers/media/mc/mc-device.c static int media_device_open(struct file *filp) · line 60
Quoted 3 lines
drivers/media/mc/mc-device.c +1
-0filp->private_data = &fh->fh; +spin_lock_irqsave (&mdev->fh_list_lock, flags);The only reason for using the irqsave variant is because we want to support events in the future, and those can be sent in irq context.
Quoted 5 lines
Even in that case, would
media_device_open()ever be called from interrupt context ?spin_lock_irqsave()is only needed if you don't know which context the function can be called from. If we know we'll be called from interruptible context only, you can usespin_lock_irq()instead.
Quoted 4 lines
Someone can call
open()while at the same time the kernel sends a media event from interrupt context. Such an event function will walk over thefh_list. The irqsave here is meant to ensure that no event interrupt can run while we add our fh to the fh list.
You don't need spin_lock_irqsave() for that, spin_lock_irq() is enough. In your interrupt handler, you need spin_lock() only. spin_lock_irqsave() is for places that can be called both from interruptible and non-interruptible contexts.
Quoted 96 lines
But without interrupts that access
fh_listthe irqsave variant is confusing.Quoted 93 lines
Quoted 93 lines
But it is confusing to see this being used here when it is not yet needed.
At minimum this should be explained in the commit log.
Quoted 88 lines
drivers/media/mc/mc-device.c +18
-0+list_add (&fh->mdev_list, &mdev->fh_list); +spin_unlock_irqrestore (&mdev->fh_list_lock, flags); +return 0 ; }static int media_device_close (struct file *filp) { +struct media_devnode *devnode =media_devnode_data (filp); +struct media_device *mdev =to_media_device (devnode);struct media_device_fh *fh =media_device_fh (filp); +unsigned long flags; + +spin_lock_irqsave (&mdev->fh_list_lock, flags); +list_del (&fh->mdev_list); +spin_unlock_irqrestore (&mdev->fh_list_lock, flags);kfree (fh);@@ -769,11 +782,13 @@ void media_device_init(struct media_device *mdev)INIT_LIST_HEAD (&mdev->pads);INIT_LIST_HEAD (&mdev->links);INIT_LIST_HEAD (&mdev->entity_notify); +INIT_LIST_HEAD (&mdev->fh_list);mutex_init (&mdev->req_queue_mutex);mutex_init (&mdev->graph_mutex);ida_init (&mdev->entity_internal_idx);atomic_set (&mdev->request_id,0 ); +spin_lock_init (&mdev->fh_list_lock); mdev->devnode.release = media_device_release;media_devnode_init (&mdev->devnode);@@ -824,6 +839,8 @@ EXPORT_SYMBOL_GPL(__media_device_register);void media_device_unregister (struct media_device *mdev) { +unsigned long flags; +if (mdev ==NULL )return ;@@ -834,6 +851,10 @@ void media_device_unregister(struct media_device *mdev) }mutex_unlock (&mdev->graph_mutex); +spin_lock_irqsave (&mdev->fh_list_lock, flags); +list_del_init (&mdev->fh_list); +spin_unlock_irqrestore (&mdev->fh_list_lock, flags); +device_remove_file (&mdev->devnode.dev, &dev_attr_model);dev_dbg (mdev->dev,"Media device unregistering\n" );media_devnode_unregister (&mdev->devnode);drivers/media/mc/mc-devnode.c +1
-1@@ -171,7 +171,7 @@ static int media_open(struct inode *inode, struct file *filp)get_device (&devnode->dev);mutex_unlock (&media_devnode_lock); - ret = devnode->fops->open (filp); + ret = devnode->fops->open (devnode, filp);if (ret) {put_device (&devnode->dev);return ret;include/media/media-devnode.h +3
-1@@ -21,6 +21,8 @@#include <linux/device.h> #include <linux/cdev.h> +struct media_devnode ; +/* * Flag to mark the media_devnode struct as registered. Drivers must not touch * this flag directly, it will be set and cleared by media_devnode_register and @@ -49,7 +51,7 @@ struct media_file_operations {__poll_t (*poll) (struct file *,struct poll_table_struct *);long (*ioctl) (struct file *,unsigned int ,unsigned long );long (*compat_ioctl) (struct file *,unsigned int ,unsigned long ); -int (*open) (struct file *); +int (*open) (struct media_devnode *,struct file *);int (*release) (struct file *); };
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerRe: [PATCH v2 26/29] media: mc: Maintain a list of open file handles in a media device
Hi Laurent, Hans,
Quoted 51 lines · Laurent Pinchart
Quoted 45 lines · Hans Verkuil
Quoted 38 lines · Laurent Pinchart
Quoted 30 lines · Hans Verkuil
Quoted 29 lines · Sakari Ailus
The list of file handles is needed to deliver media events as well as for other purposes in the future.
Signed-off-by Sakari Ailus <[email protected]>--- drivers/media/mc/mc-device.c | 23 ++++++++++++++++++++++- drivers/media/mc/mc-devnode.c | 2 +- include/media/media-devnode.h | 4 +++- 3 files changed, 26 insertions(+), 3 deletions(-)drivers/media/mc/mc-device.c +3
-1@@ -45,9 +45,11 @@ static inline void __user *media_get_uptr(__u64 arg)return (void __user *)(uintptr_t )arg; } -static int media_device_open (struct file *filp) +static int media_device_open (struct media_devnode *devnode,struct file *filp) { +struct media_device *mdev =to_media_device (devnode);struct media_device_fh *fh; +unsigned long flags; fh =kzalloc (sizeof (*fh),GFP_KERNEL );if (!fh)@@ -55,12 +57,23 @@ static int media_device_open(struct file *filp)Quoted 6 lines
drivers/media/mc/mc-device.c static int media_device_open(struct file *filp) · line 60
Quoted 3 lines
drivers/media/mc/mc-device.c +1
-0filp->private_data = &fh->fh; +spin_lock_irqsave (&mdev->fh_list_lock, flags);The only reason for using the irqsave variant is because we want to support events in the future, and those can be sent in irq context.
Quoted 5 lines
Even in that case, would
media_device_open()ever be called from interrupt context ?spin_lock_irqsave()is only needed if you don't know which context the function can be called from. If we know we'll be called from interruptible context only, you can usespin_lock_irq()instead.Quoted 4 lines
Someone can call
open()while at the same time the kernel sends a media event from interrupt context. Such an event function will walk over thefh_list. The irqsave here is meant to ensure that no event interrupt can run while we add our fh to the fh list.
Quoted 4 lines
You don't need
spin_lock_irqsave()for that,spin_lock_irq()is enough. In your interrupt handler, you needspin_lock()only.spin_lock_irqsave()is for places that can be called both from interruptible and non-interruptible contexts.
I'll address this in v3.
Signature · 1 line
Sakari Ailus
Sakari Ailus
maintainerPATCH v2 27/29 media: mc: Implement best effort media device removal safety sans refcount
In reply to Sakari AilusAdd a new helper data structures media_devnode_compat_ref and media_devnode_cdev. The latter is used to prevent user space from calling IOCTLs or other system calls to the media device that has been already unregistered and the former to help with obtaining the container struct (either media_devnode_compat_ref or media_devnode) in media_open().
The media device's memory may of course still be released during the call but there is only so much that can be done to this without the driver managing the lifetime of the resources it needs somehow.
This patch should be reverted once all drivers have been converted to manage their resources' lifetime.
--- drivers/media/mc/mc-device.c | 55 ++++++++++---- drivers/media/mc/mc-devnode.c | 117 ++++++++++++++++++++++------- drivers/media/v4l2-core/v4l2-dev.c | 28 +++++-- include/media/media-devnode.h | 64 +++++++++++++++- 4 files changed, 210 insertions(+), 54 deletions(-)
drivers/media/mc/mc-device.c +41 -14
@@ -55,6 +55,8 @@ static int media_device_open(struct media_devnode *devnode, struct file *filp)if (!fh)return -ENOMEM ; + fh->fh.ref = devnode->ref; + filp->private_data = &fh->fh;spin_lock_irqsave (&mdev->fh_list_lock, flags);
@@ -66,14 +68,19 @@ static int media_device_open(struct media_devnode *devnode, struct file *filp)static int media_device_close (struct file *filp) { -struct media_devnode *devnode =media_devnode_data (filp); -struct media_device *mdev =to_media_device (devnode); -struct media_device_fh *fh =media_device_fh (filp); -unsigned long flags; +struct media_device_fh *fh; -spin_lock_irqsave (&mdev->fh_list_lock, flags); -list_del (&fh->mdev_list); -spin_unlock_irqrestore (&mdev->fh_list_lock, flags); + fh =media_device_fh (filp); + +if (!fh->fh.ref ||atomic_read (&fh->fh.ref->registered)) { +struct media_devnode *devnode =media_devnode_data (filp); +struct media_device *mdev =to_media_device (devnode); +unsigned long flags; + +spin_lock_irqsave (&mdev->fh_list_lock, flags); +list_del (&fh->mdev_list); +spin_unlock_irqrestore (&mdev->fh_list_lock, flags); + }kfree (fh);
@@ -812,28 +819,45 @@ EXPORT_SYMBOL_GPL(media_device_cleanup);int __must_check __media_device_register (struct media_device *mdev,struct module *owner) { +struct media_devnode_compat_ref *ref =NULL ;int ret; +if (!mdev->ops || !mdev->ops->release) { + ref =kzalloc (sizeof (*mdev->devnode.ref),GFP_KERNEL ); +if (!ref) +return -ENOMEM ; + } +/* Register the device node. */ mdev->devnode.fops = &media_device_fops; mdev->devnode.parent = mdev->dev; + mdev->devnode.ref = ref;/* Set version 0 to indicate user-space that the graph is static */ mdev->topology_version =0 ; ret =media_devnode_register (&mdev->devnode, owner);if (ret <0 ) -return ret; + goto out_put_ref; + + ret =device_create_file (media_devnode_dev (&mdev->devnode), + &dev_attr_model); +if (ret <0 ) + goto out_devnode_unregister; - ret =device_create_file (&mdev->devnode.dev, &dev_attr_model); -if (ret <0 ) { -media_devnode_unregister (&mdev->devnode); -return ret; - }dev_dbg (mdev->dev,"Media device registered\n" );return 0 ; + +out_devnode_unregister: +media_devnode_unregister (&mdev->devnode); + +out_put_ref: +if (ref) +put_device (&ref->dev); + +return ret; }EXPORT_SYMBOL_GPL (__media_device_register);
@@ -855,9 +879,12 @@ void media_device_unregister(struct media_device *mdev)list_del_init (&mdev->fh_list);spin_unlock_irqrestore (&mdev->fh_list_lock, flags); -device_remove_file (&mdev->devnode.dev, &dev_attr_model); +device_remove_file (media_devnode_dev (&mdev->devnode), &dev_attr_model);dev_dbg (mdev->dev,"Media device unregistering\n" );media_devnode_unregister (&mdev->devnode); + +if (mdev->devnode.ref) +put_device (&mdev->devnode.ref->dev); }EXPORT_SYMBOL_GPL (media_device_unregister);
drivers/media/mc/mc-devnode.c +88 -29
225 lines not on this page · raw message
drivers/media/v4l2-core/v4l2-dev.c +21 -7
@@ -179,7 +179,7 @@ static void v4l2_device_release(struct device *cd)bool v4l2_dev_has_release = v4l2_dev->release;#ifdef CONFIG_MEDIA_CONTROLLER struct media_device *mdev = v4l2_dev->mdev; -bool mdev_has_release = mdev && mdev->ops && mdev->ops->release; +bool mdev_has_ref = mdev && mdev->devnode.ref;#endif mutex_lock (&videodev_lock);
@@ -212,12 +212,24 @@ static void v4l2_device_release(struct device *cd) }#endif -/* Release video_device and perform other -cleanups as needed. */ +/* +* Put struct media_devnode_compat_ref here as indicated by +* mdev_has_ref. mdev may be released by vdev->release() below. +*/ +#ifdef CONFIG_MEDIA_CONTROLLER +if (mdev && mdev_has_ref) +media_device_put (mdev); +#endif + +/* Release video_device and perform other cleanups as needed. */ vdev->release (vdev);#ifdef CONFIG_MEDIA_CONTROLLER -if (mdev) +/* +* Put a reference to struct media_device acquired in +* video_register_media_controller(). +*/ +if (mdev && !mdev_has_ref)media_device_put (mdev);/*
@@ -225,16 +237,18 @@ static void v4l2_device_release(struct device *cd)* embedded in the same driver's context struct so having a release * callback in both is a bug. */ -WARN_ON (v4l2_dev_has_release && mdev_has_release); +WARN_ON (v4l2_dev_has_release && !mdev_has_ref);#endif /* * Decrease v4l2_device refcount, but only if the media device doesn't -* have a release callback. +* have a release callback. Otherwise one could expect accessing +* released memory --- driver's context struct refcounted already via +* struct media_device. */ if (v4l2_dev_has_release #ifdef CONFIG_MEDIA_CONTROLLER - && !mdev_has_release + && mdev_has_ref #endif )v4l2_device_put (v4l2_dev);
include/media/media-devnode.h +60 -4
106 lines not on this page · raw message
Signature · 1 line
2.39.2
Sakari Ailus
maintainerPATCH v2 28/29 media: mc: Warn about drivers not releasing media device safely
In reply to Sakari AilusThe media device and associated resources may be released only when its memory is no longer used. Warn about drivers not doing this, but instead releasing the resources at driver unbind time.
--- drivers/media/mc/mc-device.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/media/mc/mc-device.c +3 -0
@@ -826,6 +826,9 @@ int __must_check __media_device_register(struct media_device *mdev, ref =kzalloc (sizeof (*mdev->devnode.ref),GFP_KERNEL );if (!ref)return -ENOMEM ; + +dev_warn (mdev->dev, +"Set mdev release op to safely release resources!\n" ); }/* Register the device node. */
Signature · 1 line
2.39.2
Sakari Ailus
maintainerPATCH v2 29/29 media: Documentation: Document media device memory safety helper
In reply to Sakari AilusDocument how the best effort memory safety helper for accessing media device works, and that drivers should be converted to refcount the media device.
--- drivers/media/mc/mc-devnode.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
drivers/media/mc/mc-devnode.c +26 -0
@@ -258,6 +258,32 @@ void media_devnode_init(struct media_devnode *devnode) devnode->minor =-1 ; } +/* +* Best effort media device lifetime management for old drivers +* +* Drivers that do not manage the lifetime of the media device are provided with +* a best effort lifetime management support. This means that as the driver does +* not release the media device once all users are gone but when the device is +* unbound, there are bound to be (brief) moments when released memory may get +* accessed. All drivers should be converted to release their memory at a safe +* time, i.e. provide a release callback in struct media_file_operations to do +* so. This is especially important for drivers for devices that are +* unpluggable, e.g. USB devices. +* +* A second struct device is used to manage the lifetime of a helper object, +* struct media_devnode_compat_ref. For a media device, one is initialised in +* media_devnode_register and put in media_devnode_unregister. This object is +* also used as the device of the media character device so file handles to the +* media device have a reference to this object. When the media device is +* released, any file handle retains a reference to this helper that also +* contains the media device's registration status. If a media device is +* released and a user space process attempts to access the file handle, an +* error is returned. +* +* The struct device in struct media_devnode is put at media_device_cleanup and +* uses an empty release callback, reflecting the expectation the driver will +* release the memory of the media device at unbind time. +*/ int __must_check media_devnode_register (struct media_devnode *devnode,struct module *owner) {
Signature · 1 line
2.39.2
Laurent Pinchart
maintainerQuoted 5 lines · Sakari Ailus
Hi folks,
This is a refresh of my 2016 RFC patchset to start addressing object lifetime issues in Media controller. It further allows continuing work to address lifetime management of media entities.
I think you win the prize of the refresh for the oldest patch series. Thanks for not dropping the ball. One day we'll make the media subsystem healthy :-)
Quoted 19 lines
The underlying problem is described in detail in v4 of the previous RFC:
<URL:https://lore.kernel.org/linux-media/[email protected]/>. In brief, there is currently no connection between releasing media device (and related) memory and IOCTL calls, meaning that there is a time window during which released kernel memory can be accessed, and that access can be triggered from the user space. The only reason why this is not a grave security issue is that it is not triggerable by the user alone but requires unbinding a device. That is still not an excuse for not fixing it.This set differs from the earlier RFC to address the issue in the following respects:
- Make changes for ipu3-cio2 driver, too.
- Continue to provide best effort attempt to keep the window between device removal and user space being able to access released memory as small as possible. This means the problem won't become worse for drivers for which Media device lifetime management has not been implemented.
Quoted 6 lines
The latter is achieved by adding a new object, Media devnode compat reference, which is allocated, refcounted and eventually released by the Media controller framework itself, and where the information on registration and open filehandles is maintained. This is only done if the driver does not manage the lifetime of the media device itself, i.e. its release operation is NULL.
Interesting. I'll check that when reviewing the patches.
Quoted 3 lines
Due to this, Media device file handles will also be introduced by this patchset. I thought the first user of this would be Media device events but it seems we already need them here.
Nice, it will become a useful feature.
Quoted 3 lines
Both ipu3-cio2 and omap3isp drivers are relieved of
devm_request_irq()use, asdevice_release()releases the resources before calling the driver's remove function.
Are you sure about that ? device_release() is the .release() function for device_ktype, which means it's called when the last reference to a struct device disappears. That should be way after .remove().
Quoted 124 lines
While further work will be required also on these drivers
to safely stop he hardware at unbind time, I don't see a reason not to merge these patches now.Some patches are temporarily reverted in order to make reworks easier, then applied later on.
I've tested this on ipu3-cio2 with and without the refcounting patch (media:
ipu3-cio2: Release the cio2 device context by media device callback), including failures in a few parts of the driver initialisation process in the MC framework.Questions and comments are welcome.
since v1:
- Align subject prefixes with current media tree practices.
- Make release changes to the vimc driver (last patch of the set). This was actually easy as vimc already centralised resource release to struct
v4l2_device, so it was just moved to the media device.- Move cdev field to struct
media_devnode_compat_refand add dev field to the struct, these are needed during device release. This now includes also the character device which is accessed by__fput(). I've now tested ipu3-cio2 and vimc with KASAN. As a by-product the kref in structmedia_devnode_compat_refbecomes redundant and is removed. Both devices are registered in case of best effort memory safety support and used for refcounting.- Drop omap3isp driver patch moving away from
devm_request_irq().- Add a patch to warn of drivers not releasing media device safely (i.e. relying on the best effort memory safety mechanism without refcounting).
- Add a patch to document how the best effort memory release safety helper works.
- Add a note on releasing driver's context with the media device, not the V4L2 device, in MC documentation.
- Check media device is registered before accessing its fops in
media_read(),media_write(),media_ioctlandmedia_compat_ioctl().- Document best effort media device lifetime management (new patch).
- Use
media_devnode_free_minor()in unallocating device node minor number inmedia_devnode_register().- Continue to rely on
devm_register_irq()in ipu3-cio2 driver but register the IRQ later on (compared to v1).- Drop the patch to move away from
devm_request_irq()in omap3isp.- Fix putting references to media device and V4L2 device in
v4l2_device_release().- Add missing
media_device_get()(in v1) for M2M devices invideo_register_media_controller().- Unconditionally set the media devnode release function in
media_device_init(). There's no harm doing so and the caller ofmedia_device_init()may set the ops after calling the function.Daniel Axtens (1):
media: uvcvideo: Refactor teardown of uvc on USB disconnectLaurent Pinchart (1):
media: mc: Add per-file-handle data supportLogan Gunthorpe (1):
media: mc: utilize newcdev_device_addhelper functionSakari Ailus (26):
Revert "[media] media: fix media devnode ioctl/syscall and unregister
race"
Revert "media: utilize newcdev_device_addhelper function" Revert "[media] media: fix use-after-free incdev_put()when app exits
after driver unbind"
Revert "media: uvcvideo: Refactor teardown of uvc on USB disconnect" Revert "[media] media-device: dynamically allocate struct
media_devnode"
media: mc: Drop nop release callback
media: mc: Do not callcdev_device_del()ifcdev_device_add()fails
media: mc: Delete character device early
media: mc: Split initialising and adding media devnode
media: mc: Shuffle functions around
media: mc: Initialise media devnode inmedia_device_init()
media: mc: Refactor media devnode minor clearing
media: mc: Unassign minor only if it has been assigned
media: mc: Refcount the media device
media: v4l: Acquire a reference to the media device for every video
device
media: mc: Postpone graph object removal until free
media: omap3isp: Release the isp device struct by media device
callback
media: ipu3-cio2: Callv4l2_device_unregister()earlier
media: ipu3-cio2: Request IRQ earlier
media: ipu3-cio2: Release the cio2 device context by media device
callback
media: vimc: Release resources on media device release
media: Documentation: Document how Media device resources are released
media: mc: Maintain a list of open file handles in a media device
media: mc: Implement best effort media device removal safety sans
refcount
media: mc: Warn about drivers not releasing media device safely
media: Documentation: Document media device memory safety helperDocumentation/driver-api/media/mc-core.rst | 18 +- drivers/media/cec/core/cec-core.c | 2 +- drivers/media/mc/mc-device.c | 260 ++++++++++++-------- drivers/media/mc/mc-devnode.c | 230 +++++++++++------ drivers/media/pci/intel/ipu3/ipu3-cio2.c | 70 ++++-- drivers/media/platform/ti/omap3isp/isp.c | 24 +- drivers/media/test-drivers/vimc/vimc-core.c | 15 +- drivers/media/usb/au0828/au0828-core.c | 4 +- drivers/media/usb/uvc/uvc_driver.c | 2 +- drivers/media/v4l2-core/v4l2-dev.c | 65 +++-- drivers/staging/media/sunxi/cedrus/cedrus.c | 2 +- include/media/media-device.h | 46 +++- include/media/media-devnode.h | 136 +++++++--- include/media/media-fh.h | 32 +++ 14 files changed, 632 insertions(+), 274 deletions(-) create mode 100644 include/media/media-fh.h
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerHi Laurent,
Quoted 10 lines · Laurent Pinchart
Quoted 5 lines · Sakari Ailus
Hi folks,
This is a refresh of my 2016 RFC patchset to start addressing object lifetime issues in Media controller. It further allows continuing work to address lifetime management of media entities.
I think you win the prize of the refresh for the oldest patch series. Thanks for not dropping the ball. One day we'll make the media subsystem healthy :-)
Maybe so, indeed. The problem has always been around and I think it needs to be addressed, although this only solves a part of it. It's been a low priority for me and figuring out how to prevent old drivers getting worse in this respect wasn't obvious at first, as you see from changes since v1.
Quoted 20 lines
Quoted 20 lines
The underlying problem is described in detail in v4 of the previous RFC:
<URL:https://lore.kernel.org/linux-media/[email protected]/>. In brief, there is currently no connection between releasing media device (and related) memory and IOCTL calls, meaning that there is a time window during which released kernel memory can be accessed, and that access can be triggered from the user space. The only reason why this is not a grave security issue is that it is not triggerable by the user alone but requires unbinding a device. That is still not an excuse for not fixing it.This set differs from the earlier RFC to address the issue in the following respects:
- Make changes for ipu3-cio2 driver, too.
- Continue to provide best effort attempt to keep the window between device removal and user space being able to access released memory as small as possible. This means the problem won't become worse for drivers for which Media device lifetime management has not been implemented.
Quoted 8 lines
Quoted 6 lines
The latter is achieved by adding a new object, Media devnode compat reference, which is allocated, refcounted and eventually released by the Media controller framework itself, and where the information on registration and open filehandles is maintained. This is only done if the driver does not manage the lifetime of the media device itself, i.e. its release operation is NULL.
Interesting. I'll check that when reviewing the patches.
Please. :-)
Quoted 7 lines
Quoted 4 lines
Due to this, Media device file handles will also be introduced by this patchset. I thought the first user of this would be Media device events but it seems we already need them here.
Nice, it will become a useful feature.
Quoted 7 lines
Quoted 3 lines
Both ipu3-cio2 and omap3isp drivers are relieved of
devm_request_irq()use, asdevice_release()releases the resources before calling the driver's remove function.Are you sure about that ?
device_release()is the .release() function fordevice_ktype, which means it's called when the last reference to a struct device disappears. That should be way after .remove().
It's a bit grey area. If an interrupt arrives from the device in the meantime, bad things will still happen. Of course it's not supposed to. Either way, I've dropped these changes as requested in review comments in v1 and as outlined below in changes since v1, but forgot to change this text (unchanged since v1).
Quoted 126 lines
Quoted 125 lines
While further work will be required also on these drivers
to safely stop he hardware at unbind time, I don't see a reason not to merge these patches now.Some patches are temporarily reverted in order to make reworks easier, then applied later on.
I've tested this on ipu3-cio2 with and without the refcounting patch (media:
ipu3-cio2: Release the cio2 device context by media device callback), including failures in a few parts of the driver initialisation process in the MC framework.Questions and comments are welcome.
since v1:
- Align subject prefixes with current media tree practices.
- Make release changes to the vimc driver (last patch of the set). This was actually easy as vimc already centralised resource release to struct
v4l2_device, so it was just moved to the media device.- Move cdev field to struct
media_devnode_compat_refand add dev field to the struct, these are needed during device release. This now includes also the character device which is accessed by__fput(). I've now tested ipu3-cio2 and vimc with KASAN. As a by-product the kref in structmedia_devnode_compat_refbecomes redundant and is removed. Both devices are registered in case of best effort memory safety support and used for refcounting.- Drop omap3isp driver patch moving away from
devm_request_irq().- Add a patch to warn of drivers not releasing media device safely (i.e. relying on the best effort memory safety mechanism without refcounting).
- Add a patch to document how the best effort memory release safety helper works.
- Add a note on releasing driver's context with the media device, not the V4L2 device, in MC documentation.
- Check media device is registered before accessing its fops in
media_read(),media_write(),media_ioctlandmedia_compat_ioctl().- Document best effort media device lifetime management (new patch).
- Use
media_devnode_free_minor()in unallocating device node minor number inmedia_devnode_register().- Continue to rely on
devm_register_irq()in ipu3-cio2 driver but register the IRQ later on (compared to v1).- Drop the patch to move away from
devm_request_irq()in omap3isp.- Fix putting references to media device and V4L2 device in
v4l2_device_release().- Add missing
media_device_get()(in v1) for M2M devices invideo_register_media_controller().- Unconditionally set the media devnode release function in
media_device_init(). There's no harm doing so and the caller ofmedia_device_init()may set the ops after calling the function.Daniel Axtens (1):
media: uvcvideo: Refactor teardown of uvc on USB disconnectLaurent Pinchart (1):
media: mc: Add per-file-handle data supportLogan Gunthorpe (1):
media: mc: utilize newcdev_device_addhelper functionSakari Ailus (26):
Revert "[media] media: fix media devnode ioctl/syscall and unregister
race"
Revert "media: utilize newcdev_device_addhelper function" Revert "[media] media: fix use-after-free incdev_put()when app exits
after driver unbind"
Revert "media: uvcvideo: Refactor teardown of uvc on USB disconnect" Revert "[media] media-device: dynamically allocate struct
media_devnode"
media: mc: Drop nop release callback
media: mc: Do not callcdev_device_del()ifcdev_device_add()fails
media: mc: Delete character device early
media: mc: Split initialising and adding media devnode
media: mc: Shuffle functions around
media: mc: Initialise media devnode inmedia_device_init()
media: mc: Refactor media devnode minor clearing
media: mc: Unassign minor only if it has been assigned
media: mc: Refcount the media device
media: v4l: Acquire a reference to the media device for every video
device
media: mc: Postpone graph object removal until free
media: omap3isp: Release the isp device struct by media device
callback
media: ipu3-cio2: Callv4l2_device_unregister()earlier
media: ipu3-cio2: Request IRQ earlier
media: ipu3-cio2: Release the cio2 device context by media device
callback
media: vimc: Release resources on media device release
media: Documentation: Document how Media device resources are released
media: mc: Maintain a list of open file handles in a media device
media: mc: Implement best effort media device removal safety sans
refcount
media: mc: Warn about drivers not releasing media device safely
media: Documentation: Document media device memory safety helperDocumentation/driver-api/media/mc-core.rst | 18 +- drivers/media/cec/core/cec-core.c | 2 +- drivers/media/mc/mc-device.c | 260 ++++++++++++-------- drivers/media/mc/mc-devnode.c | 230 +++++++++++------ drivers/media/pci/intel/ipu3/ipu3-cio2.c | 70 ++++-- drivers/media/platform/ti/omap3isp/isp.c | 24 +- drivers/media/test-drivers/vimc/vimc-core.c | 15 +- drivers/media/usb/au0828/au0828-core.c | 4 +- drivers/media/usb/uvc/uvc_driver.c | 2 +- drivers/media/v4l2-core/v4l2-dev.c | 65 +++-- drivers/staging/media/sunxi/cedrus/cedrus.c | 2 +- include/media/media-device.h | 46 +++- include/media/media-devnode.h | 136 +++++++--- include/media/media-fh.h | 32 +++ 14 files changed, 632 insertions(+), 274 deletions(-) create mode 100644 include/media/media-fh.h
Signature · 3 lines
Kind regards, Sakari Ailus
Laurent Pinchart
maintainerHi Sakari,
I've just noticed that I send multiple Reviewed-by tags for this series using Laurent Pinchart <[email protected]> when I meant Laurent Pinchart <[email protected]>. Could you replace the e-mail address when picking up the tags ?Sorry about the inconvenience.
Quoted 162 lines · Sakari Ailus
Hi folks,
This is a refresh of my 2016 RFC patchset to start addressing object lifetime issues in Media controller. It further allows continuing work to address lifetime management of media entities.
The underlying problem is described in detail in v4 of the previous RFC:
<URL:https://lore.kernel.org/linux-media/[email protected]/>. In brief, there is currently no connection between releasing media device (and related) memory and IOCTL calls, meaning that there is a time window during which released kernel memory can be accessed, and that access can be triggered from the user space. The only reason why this is not a grave security issue is that it is not triggerable by the user alone but requires unbinding a device. That is still not an excuse for not fixing it.This set differs from the earlier RFC to address the issue in the following respects:
- Make changes for ipu3-cio2 driver, too.
- Continue to provide best effort attempt to keep the window between device removal and user space being able to access released memory as small as possible. This means the problem won't become worse for drivers for which Media device lifetime management has not been implemented.
The latter is achieved by adding a new object, Media devnode compat reference, which is allocated, refcounted and eventually released by the Media controller framework itself, and where the information on registration and open filehandles is maintained. This is only done if the driver does not manage the lifetime of the media device itself, i.e. its release operation is NULL.
Due to this, Media device file handles will also be introduced by this patchset. I thought the first user of this would be Media device events but it seems we already need them here.
Both ipu3-cio2 and omap3isp drivers are relieved of
devm_request_irq()use, asdevice_release()releases the resources before calling the driver's remove function. While further work will be required also on these drivers to safely stop he hardware at unbind time, I don't see a reason not to merge these patches now.Some patches are temporarily reverted in order to make reworks easier, then applied later on.
I've tested this on ipu3-cio2 with and without the refcounting patch (media:
ipu3-cio2: Release the cio2 device context by media device callback), including failures in a few parts of the driver initialisation process in the MC framework.Questions and comments are welcome.
since v1:
- Align subject prefixes with current media tree practices.
- Make release changes to the vimc driver (last patch of the set). This was actually easy as vimc already centralised resource release to struct
v4l2_device, so it was just moved to the media device.- Move cdev field to struct
media_devnode_compat_refand add dev field to the struct, these are needed during device release. This now includes also the character device which is accessed by__fput(). I've now tested ipu3-cio2 and vimc with KASAN. As a by-product the kref in structmedia_devnode_compat_refbecomes redundant and is removed. Both devices are registered in case of best effort memory safety support and used for refcounting.- Drop omap3isp driver patch moving away from
devm_request_irq().- Add a patch to warn of drivers not releasing media device safely (i.e. relying on the best effort memory safety mechanism without refcounting).
- Add a patch to document how the best effort memory release safety helper works.
- Add a note on releasing driver's context with the media device, not the V4L2 device, in MC documentation.
- Check media device is registered before accessing its fops in
media_read(),media_write(),media_ioctlandmedia_compat_ioctl().- Document best effort media device lifetime management (new patch).
- Use
media_devnode_free_minor()in unallocating device node minor number inmedia_devnode_register().- Continue to rely on
devm_register_irq()in ipu3-cio2 driver but register the IRQ later on (compared to v1).- Drop the patch to move away from
devm_request_irq()in omap3isp.- Fix putting references to media device and V4L2 device in
v4l2_device_release().- Add missing
media_device_get()(in v1) for M2M devices invideo_register_media_controller().- Unconditionally set the media devnode release function in
media_device_init(). There's no harm doing so and the caller ofmedia_device_init()may set the ops after calling the function.Daniel Axtens (1):
media: uvcvideo: Refactor teardown of uvc on USB disconnectLaurent Pinchart (1):
media: mc: Add per-file-handle data supportLogan Gunthorpe (1):
media: mc: utilize newcdev_device_addhelper functionSakari Ailus (26):
Revert "[media] media: fix media devnode ioctl/syscall and unregister
race"
Revert "media: utilize newcdev_device_addhelper function" Revert "[media] media: fix use-after-free incdev_put()when app exits
after driver unbind"
Revert "media: uvcvideo: Refactor teardown of uvc on USB disconnect" Revert "[media] media-device: dynamically allocate struct
media_devnode"
media: mc: Drop nop release callback
media: mc: Do not callcdev_device_del()ifcdev_device_add()fails
media: mc: Delete character device early
media: mc: Split initialising and adding media devnode
media: mc: Shuffle functions around
media: mc: Initialise media devnode inmedia_device_init()
media: mc: Refactor media devnode minor clearing
media: mc: Unassign minor only if it has been assigned
media: mc: Refcount the media device
media: v4l: Acquire a reference to the media device for every video
device
media: mc: Postpone graph object removal until free
media: omap3isp: Release the isp device struct by media device
callback
media: ipu3-cio2: Callv4l2_device_unregister()earlier
media: ipu3-cio2: Request IRQ earlier
media: ipu3-cio2: Release the cio2 device context by media device
callback
media: vimc: Release resources on media device release
media: Documentation: Document how Media device resources are released
media: mc: Maintain a list of open file handles in a media device
media: mc: Implement best effort media device removal safety sans
refcount
media: mc: Warn about drivers not releasing media device safely
media: Documentation: Document media device memory safety helperDocumentation/driver-api/media/mc-core.rst | 18 +- drivers/media/cec/core/cec-core.c | 2 +- drivers/media/mc/mc-device.c | 260 ++++++++++++-------- drivers/media/mc/mc-devnode.c | 230 +++++++++++------ drivers/media/pci/intel/ipu3/ipu3-cio2.c | 70 ++++-- drivers/media/platform/ti/omap3isp/isp.c | 24 +- drivers/media/test-drivers/vimc/vimc-core.c | 15 +- drivers/media/usb/au0828/au0828-core.c | 4 +- drivers/media/usb/uvc/uvc_driver.c | 2 +- drivers/media/v4l2-core/v4l2-dev.c | 65 +++-- drivers/staging/media/sunxi/cedrus/cedrus.c | 2 +- include/media/media-device.h | 46 +++- include/media/media-devnode.h | 136 +++++++--- include/media/media-fh.h | 32 +++ 14 files changed, 632 insertions(+), 274 deletions(-) create mode 100644 include/media/media-fh.h
Signature · 3 lines
Regards, Laurent Pinchart
Sakari Ailus
maintainerQuoted 7 lines · Laurent Pinchart
Hi Sakari,
I've just noticed that I send multiple Reviewed-by tags for this series using Laurent Pinchart <[email protected]> when I meant Laurent Pinchart <[email protected]>. Could you replace the e-mail address when picking up the tags ?Sorry about the inconvenience.
No problem. I'll do it for v3.
Signature · 1 line
Sakari Ailus