[PATCH v2] drivers: hv: vmbus: Add missing check for dma_set_mask in vmbus_device_register()

Haoxiang Li posted 1 patch 1 year, 5 months ago
drivers/hv/vmbus_drv.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
[PATCH v2] drivers: hv: vmbus: Add missing check for dma_set_mask in vmbus_device_register()
Posted by Haoxiang Li 1 year, 5 months ago
child_device_obj->device cannot perform DMA properly if dma_set_mask()
returns non-zero. Add check for dma_set_mask() and return the error if it
fails. To do the right cleanup, call dma_set_mask() after device_register()
so that we can use existent error path of vmbus_device_register().

Fixes: 3a5469582c24 ("Drivers: hv: vmbus: Fix initialization of device object in vmbus_device_register()")
Signed-off-by: Haoxiang Li <make24@iscas.ac.cn>
---
Changes in v2:
Thanks for your comments. They help a lot. It is not sufficient to do the
cleanup just with kfree(). The memory allocated by dev_set_name()
should also be freed, which is done by put_device() (finally in
kobject_cleanup() [1]). I think performing a complete cleanup within
vmbus_device_register() would be verbose and detrimental to code
maintenance. I suggest calling dma_set_mask() after device_register(),
so that proper error handling can be achieved using the existent error path
of vmbus_device_register(), i.e., err_dev_unregister [2]. Moreover,
I found a similar usage in dmapool_checks() [3], which calls
dma_set_mask_and_coherent() after device_register(). I believe the
modification is workable.

Reference link:
[1]https://github.com/torvalds/linux/blob/master/lib/kobject.c#L695
[2]https://github.com/torvalds/linux/blob/master/drivers/hv/vmbus_drv.c#L1933
[3]https://github.com/torvalds/linux/blob/master/mm/dmapool_test.c#L105
---
 drivers/hv/vmbus_drv.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c
index 12a707ab73f8..f3999d8afd77 100644
--- a/drivers/hv/vmbus_drv.c
+++ b/drivers/hv/vmbus_drv.c
@@ -1897,7 +1897,6 @@ int vmbus_device_register(struct hv_device *child_device_obj)
 
 	child_device_obj->device.dma_parms = &child_device_obj->dma_parms;
 	child_device_obj->device.dma_mask = &child_device_obj->dma_mask;
-	dma_set_mask(&child_device_obj->device, DMA_BIT_MASK(64));
 
 	/*
 	 * Register with the LDM. This will kick off the driver/device
@@ -1910,6 +1909,12 @@ int vmbus_device_register(struct hv_device *child_device_obj)
 		return ret;
 	}
 
+	ret = dma_set_mask(&child_device_obj->device, DMA_BIT_MASK(64));
+	if (ret) {
+		pr_err("64-bit DMA enable failed!\n");
+		goto err_dev_unregister;
+	}
+
 	child_device_obj->channels_kset = kset_create_and_add("channels",
 							      NULL, kobj);
 	if (!child_device_obj->channels_kset) {
-- 
2.25.1
Re: [PATCH v2] drivers: hv: vmbus: Add missing check for dma_set_mask in vmbus_device_register()
Posted by Wei Liu 1 year, 5 months ago
On Wed, Jul 03, 2024 at 04:42:21PM +0800, Haoxiang Li wrote:
> child_device_obj->device cannot perform DMA properly if dma_set_mask()
> returns non-zero. Add check for dma_set_mask() and return the error if it
> fails. To do the right cleanup, call dma_set_mask() after device_register()
> so that we can use existent error path of vmbus_device_register().
> 
> Fixes: 3a5469582c24 ("Drivers: hv: vmbus: Fix initialization of device object in vmbus_device_register()")
> Signed-off-by: Haoxiang Li <make24@iscas.ac.cn>

The email address doesn't seem to match your name.  Why?

You should use your own email address to sign off the patches you write.

Thanks,
Wei.

> ---
> Changes in v2:
> Thanks for your comments. They help a lot. It is not sufficient to do the
> cleanup just with kfree(). The memory allocated by dev_set_name()
> should also be freed, which is done by put_device() (finally in
> kobject_cleanup() [1]). I think performing a complete cleanup within
> vmbus_device_register() would be verbose and detrimental to code
> maintenance. I suggest calling dma_set_mask() after device_register(),
> so that proper error handling can be achieved using the existent error path
> of vmbus_device_register(), i.e., err_dev_unregister [2]. Moreover,
> I found a similar usage in dmapool_checks() [3], which calls
> dma_set_mask_and_coherent() after device_register(). I believe the
> modification is workable.
> 
> Reference link:
> [1]https://github.com/torvalds/linux/blob/master/lib/kobject.c#L695
> [2]https://github.com/torvalds/linux/blob/master/drivers/hv/vmbus_drv.c#L1933
> [3]https://github.com/torvalds/linux/blob/master/mm/dmapool_test.c#L105
> ---
>  drivers/hv/vmbus_drv.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c
> index 12a707ab73f8..f3999d8afd77 100644
> --- a/drivers/hv/vmbus_drv.c
> +++ b/drivers/hv/vmbus_drv.c
> @@ -1897,7 +1897,6 @@ int vmbus_device_register(struct hv_device *child_device_obj)
>  
>  	child_device_obj->device.dma_parms = &child_device_obj->dma_parms;
>  	child_device_obj->device.dma_mask = &child_device_obj->dma_mask;
> -	dma_set_mask(&child_device_obj->device, DMA_BIT_MASK(64));
>  
>  	/*
>  	 * Register with the LDM. This will kick off the driver/device
> @@ -1910,6 +1909,12 @@ int vmbus_device_register(struct hv_device *child_device_obj)
>  		return ret;
>  	}
>  
> +	ret = dma_set_mask(&child_device_obj->device, DMA_BIT_MASK(64));
> +	if (ret) {
> +		pr_err("64-bit DMA enable failed!\n");
> +		goto err_dev_unregister;
> +	}
> +
>  	child_device_obj->channels_kset = kset_create_and_add("channels",
>  							      NULL, kobj);
>  	if (!child_device_obj->channels_kset) {
> -- 
> 2.25.1
> 
>
Re: [PATCH v2] drivers: hv: vmbus: Add missing check for dma_set_mask in vmbus_device_register()
Posted by Markus Elfring 1 year, 5 months ago
> child_device_obj->device cannot perform DMA properly if dma_set_mask()
> returns non-zero. …

Can the repetition of another wording suggestion influence the software evolution?
  Direct memory access can not be properly performed any more
  after a dma_set_mask() call failed.


See also:
https://elixir.bootlin.com/linux/v6.10-rc6/source/kernel/dma/mapping.c#L804


…
> Signed-off-by: Haoxiang Li <make24@iscas.ac.cn>

Under which circumstances will applications of the Developer's Certificate of Origin
be reconsidered any more (after three different names were presented so far)?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.10-rc6#n398


Would you like to append parentheses to another function name in the summary phrase?

Regards,
Markus