[PATCH v5] media: pci: intel: ivsc: improve device reference counting in mei_ace driver

Ma Ke posted 1 patch 3 days ago
drivers/media/pci/intel/ivsc/mei_ace.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH v5] media: pci: intel: ivsc: improve device reference counting in mei_ace driver
Posted by Ma Ke 3 days ago
The device reference counting in mei_ace_setup_dev_link() was
incomplete, as the reference acquired by device_find_child_by_name()
was not released immediately on the success path. Add put_device() to
properly balance the reference count. Additionally, the redundant
put_device() in mei_ace_remove() is removed.

Found by code review.

Signed-off-by: Ma Ke <make24@iscas.ac.cn>
---
Changes in v5:
- jumped to err instead of err_put to avoid calling put_device() again in err_put as reviewer's instructions;
Changes in v4:
- updated the subject as suggestions;
Changes in v3:
- deleted the tag of Fixes and CC, and moved put_device() to immediately after device_link_add() as suggestions;
Changes in v2:
- modified the put_device() operations and the patch title as suggestions.
---
 drivers/media/pci/intel/ivsc/mei_ace.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/media/pci/intel/ivsc/mei_ace.c b/drivers/media/pci/intel/ivsc/mei_ace.c
index 98310b8511b1..b306a320b70f 100644
--- a/drivers/media/pci/intel/ivsc/mei_ace.c
+++ b/drivers/media/pci/intel/ivsc/mei_ace.c
@@ -414,10 +414,11 @@ static int mei_ace_setup_dev_link(struct mei_ace *ace)
 	/* setup link between mei_ace and mei_csi */
 	ace->csi_link = device_link_add(csi_dev, dev, DL_FLAG_PM_RUNTIME |
 					DL_FLAG_RPM_ACTIVE | DL_FLAG_STATELESS);
+	put_device(csi_dev);
 	if (!ace->csi_link) {
 		ret = -EINVAL;
 		dev_err(dev, "failed to link to %s\n", dev_name(csi_dev));
-		goto err_put;
+		goto err;
 	}
 
 	ace->csi_dev = csi_dev;
@@ -522,7 +523,6 @@ static void mei_ace_remove(struct mei_cl_device *cldev)
 	cancel_work_sync(&ace->work);
 
 	device_link_del(ace->csi_link);
-	put_device(ace->csi_dev);
 
 	pm_runtime_disable(&cldev->dev);
 	pm_runtime_set_suspended(&cldev->dev);
-- 
2.17.1
Re: [PATCH v5] media: pci: intel: ivsc: improve device reference counting in mei_ace driver
Posted by Sakari Ailus 14 hours ago
Hi Ma,

Thanks for the update.

On Mon, Sep 29, 2025 at 09:30:07AM +0800, Ma Ke wrote:
> The device reference counting in mei_ace_setup_dev_link() was
> incomplete, as the reference acquired by device_find_child_by_name()
> was not released immediately on the success path. Add put_device() to
> properly balance the reference count. Additionally, the redundant
> put_device() in mei_ace_remove() is removed.
> 
> Found by code review.

The patch seems fine but the commit message is a bit more dramatic than
what reality justifies. I'll use this:

Put the reference to csi_dev acquired during driver probe in
mei_ace_setup_dev_link() inside the same function, instead of during driver
unbind in mei_ace_remove(). This can be done as device_link_add() already
takes a reference to csi_dev.

> 
> Signed-off-by: Ma Ke <make24@iscas.ac.cn>
> ---
> Changes in v5:
> - jumped to err instead of err_put to avoid calling put_device() again in err_put as reviewer's instructions;
> Changes in v4:
> - updated the subject as suggestions;
> Changes in v3:
> - deleted the tag of Fixes and CC, and moved put_device() to immediately after device_link_add() as suggestions;
> Changes in v2:
> - modified the put_device() operations and the patch title as suggestions.
> ---
>  drivers/media/pci/intel/ivsc/mei_ace.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/media/pci/intel/ivsc/mei_ace.c b/drivers/media/pci/intel/ivsc/mei_ace.c
> index 98310b8511b1..b306a320b70f 100644
> --- a/drivers/media/pci/intel/ivsc/mei_ace.c
> +++ b/drivers/media/pci/intel/ivsc/mei_ace.c
> @@ -414,10 +414,11 @@ static int mei_ace_setup_dev_link(struct mei_ace *ace)
>  	/* setup link between mei_ace and mei_csi */
>  	ace->csi_link = device_link_add(csi_dev, dev, DL_FLAG_PM_RUNTIME |
>  					DL_FLAG_RPM_ACTIVE | DL_FLAG_STATELESS);
> +	put_device(csi_dev);
>  	if (!ace->csi_link) {
>  		ret = -EINVAL;
>  		dev_err(dev, "failed to link to %s\n", dev_name(csi_dev));
> -		goto err_put;
> +		goto err;
>  	}
>  
>  	ace->csi_dev = csi_dev;
> @@ -522,7 +523,6 @@ static void mei_ace_remove(struct mei_cl_device *cldev)
>  	cancel_work_sync(&ace->work);
>  
>  	device_link_del(ace->csi_link);
> -	put_device(ace->csi_dev);
>  
>  	pm_runtime_disable(&cldev->dev);
>  	pm_runtime_set_suspended(&cldev->dev);

-- 
Regards,

Sakari Ailus
Re: [PATCH v5] media: pci: intel: ivsc: improve device reference counting in mei_ace driver
Posted by Markus Elfring 2 days, 17 hours ago
…
> properly balance the reference count. Additionally, the redundant

                                                          misplaced?


> put_device() in mei_ace_remove() is removed.

               call?


See also:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.17-rc7#n94


Under which circumstances would you become interested to apply an attribute
like “__free(put_device)”?
https://elixir.bootlin.com/linux/v6.17-rc7/source/include/linux/device.h#L1180

Regards,
Markus