Skip to contentSkip to conversation

device core: fix dma_mask handling in platform_device_register_full

PATCH In review Christoph Hellwig 5 participants 8 messages Mar 11, 2020 Driver core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 device core: fix dma_mask handling in platform_device_register_full

Ever since the generic platform device code started allocating DMA masks itself the code to allocate and leak a private DMA mask in platform_device_register_full has been superflous. More so the fact that it unconditionally frees the DMA mask allocation in the failure path can lead to slab corruption if the function fails later on for a device where it didn't allocate the mask. Just remove the offending code.

Fixes cdfee5623290 ("driver core: initialize a default DMA mask for platform device")
---
 drivers/base/platform.c | 14 --------------
 1 file changed, 14 deletions(-)
drivers/base/platform.c +0 -14
index 7fa654f1288b..47d3e6187a1c 100644
@@ -662,19 +662,6 @@ struct platform_device *platform_device_register_full(
 	pdev->dev.of_node_reused = pdevinfo->of_node_reused;
 
 	if (pdevinfo->dma_mask) {
-		/*
-		 * This memory isn't freed when the device is put,
-		 * I don't have a nice idea for that though.  Conceptually
-		 * dma_mask in struct device should not be a pointer.
-		 * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
-		 */
-		pdev->dev.dma_mask =
-			kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
-		if (!pdev->dev.dma_mask)
-			goto err;
-
-		kmemleak_ignore(pdev->dev.dma_mask);
-
 		*pdev->dev.dma_mask = pdevinfo->dma_mask;
 		pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
 	}
@@ -700,7 +687,6 @@ struct platform_device *platform_device_register_full(
 	if (ret) {
 err:
 		ACPI_COMPANION_SET(&pdev->dev, NULL);
-		kfree(pdev->dev.dma_mask);
 		platform_device_put(pdev);
 		return ERR_PTR(ret);
 	}
Signature · 1 line
2.24.1
#1 lore raw
Quoted 40 lines · Christoph Hellwig

Ever since the generic platform device code started allocating DMA masks itself the code to allocate and leak a private DMA mask in platform_device_register_full has been superflous. More so the fact that it unconditionally frees the DMA mask allocation in the failure path can lead to slab corruption if the function fails later on for a device where it didn't allocate the mask. Just remove the offending code.

Fixes cdfee5623290 ("driver core: initialize a default DMA mask for platform device")
---
  drivers/base/platform.c | 14 --------------
  1 file changed, 14 deletions(-)
drivers/base/platform.c +0 -13
index 7fa654f1288b..47d3e6187a1c 100644
@@ -662,19 +662,6 @@ struct platform_device *platform_device_register_full(
  	pdev->dev.of_node_reused = pdevinfo->of_node_reused;
 
  	if (pdevinfo->dma_mask) {
-		/*
-		 * This memory isn't freed when the device is put,
-		 * I don't have a nice idea for that though.  Conceptually
-		 * dma_mask in struct device should not be a pointer.
-		 * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
-		 */
-		pdev->dev.dma_mask =
-			kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
-		if (!pdev->dev.dma_mask)
-			goto err;
-
-		kmemleak_ignore(pdev->dev.dma_mask);
-
  		*pdev->dev.dma_mask = pdevinfo->dma_mask;
  		pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
  	}

drivers/base/platform.c struct platform_device *platform_device_register_full( · line 692

Quoted 7 lines
drivers/base/platform.c +0 -1
@@ -700,7 +687,6 @@ struct platform_device *platform_device_register_full(
  	if (ret) {
  err:
  		ACPI_COMPANION_SET(&pdev->dev, NULL);
-		kfree(pdev->dev.dma_mask);
  		platform_device_put(pdev);
  		return ERR_PTR(ret);
  	}

I'd love to see this patch applied to the next round of stable kernels (at the very least 5.5.x) ASAP because I've been stuck with kernel 5.3 on Fedora for quite some time already.

Thank you!

maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Christoph Hellwig
Quoted 10 lines · Christoph Hellwig

Ever since the generic platform device code started allocating DMA masks itself the code to allocate and leak a private DMA mask in platform_device_register_full has been superflous. More so the fact that it unconditionally frees the DMA mask allocation in the failure path can lead to slab corruption if the function fails later on for a device where it didn't allocate the mask. Just remove the offending code.

Fixes cdfee5623290 ("driver core: initialize a default DMA mask for platform device")

No s-o-b from you? :(

I can take this, or Linus, you can take this now if you want to as well:

#3 lore raw
Quoted 8 lines · Greg KH
Quoted 7 lines · Christoph Hellwig

Ever since the generic platform device code started allocating DMA masks itself the code to allocate and leak a private DMA mask in platform_device_register_full has been superflous. More so the fact that it unconditionally frees the DMA mask allocation in the failure path can lead to slab corruption if the function fails later on for a device where it didn't allocate the mask. Just remove the offending code.

Quoted 7 lines
Quoted 3 lines
Fixes cdfee5623290 ("driver core: initialize a default DMA mask for platform device")

No s-o-b from you? :(

I can take this, or Linus, you can take this now if you want to as well:

Sorry, here it is:

maintainer
#4 lore raw
Quoted 13 lines · Christoph Hellwig
Quoted 12 lines · Greg KH
Quoted 10 lines · Christoph Hellwig

Ever since the generic platform device code started allocating DMA masks itself the code to allocate and leak a private DMA mask in platform_device_register_full has been superflous. More so the fact that it unconditionally frees the DMA mask allocation in the failure path can lead to slab corruption if the function fails later on for a device where it didn't allocate the mask. Just remove the offending code.

Fixes cdfee5623290 ("driver core: initialize a default DMA mask for platform device")
Quoted 7 lines
Quoted 3 lines

No s-o-b from you? :(

I can take this, or Linus, you can take this now if you want to as well:

Sorry, here it is:

Is this still needed with the patch that Linus just committed to his tree?

thanks,

greg k-h

#6 lore raw
In reply to Greg KH
Quoted 3 lines · Greg KH

Is this still needed with the patch that Linus just committed to his tree?

My patch is basically the same, just with the field renamed too, and not blindly just assigning to "*pdev->dev.dma_mask" (my variant does

        pdev->platform_dma_mask = pdevinfo->dma_mask;
        pdev->dev.dma_mask = &pdev->platform_dma_mask;

instead of that incomprehensible

        *pdev->dev.dma_mask = pdevinfo->dma_mask;

which depends on that dev.dma_mask pointer having been initialized in a random place earlier).

I had the cleanups (uncommited) in my tree, and just removed the kfree() as per Christoph.

Linus

Reviewed-by on v1 1/1
#7 lore raw
In reply to Christoph Hellwig
Quoted 6 lines · Christoph Hellwig

Ever since the generic platform device code started allocating DMA masks itself the code to allocate and leak a private DMA mask in platform_device_register_full has been superflous. More so the fact that it unconditionally frees the DMA mask allocation in the failure path can lead to slab corruption if the function fails later on for a device where it didn't allocate the mask. Just remove the offending code.

I'm sure I mentioned this in passing at the time, but only in the context of a cleanup; I never noticed it could be cause for an actual bug :)

Quoted 40 lines
Fixes cdfee5623290 ("driver core: initialize a default DMA mask for platform device")
---
  drivers/base/platform.c | 14 --------------
  1 file changed, 14 deletions(-)
drivers/base/platform.c +0 -14
index 7fa654f1288b..47d3e6187a1c 100644
@@ -662,19 +662,6 @@ struct platform_device *platform_device_register_full(
  	pdev->dev.of_node_reused = pdevinfo->of_node_reused;
 
  	if (pdevinfo->dma_mask) {
-		/*
-		 * This memory isn't freed when the device is put,
-		 * I don't have a nice idea for that though.  Conceptually
-		 * dma_mask in struct device should not be a pointer.
-		 * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
-		 */
-		pdev->dev.dma_mask =
-			kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
-		if (!pdev->dev.dma_mask)
-			goto err;
-
-		kmemleak_ignore(pdev->dev.dma_mask);
-
  		*pdev->dev.dma_mask = pdevinfo->dma_mask;
  		pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
  	}
@@ -700,7 +687,6 @@ struct platform_device *platform_device_register_full(
  	if (ret) {
  err:
  		ACPI_COMPANION_SET(&pdev->dev, NULL);
-		kfree(pdev->dev.dma_mask);
  		platform_device_put(pdev);
  		return ERR_PTR(ret);
  	}