Skip to contentSkip to conversation

of: dma_configure: Free old DMA map range

PATCH In review Mårten Lindahl 3 participants 3 messages Feb 4, 2022 – Feb 11, 2022 Device tree

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 of: dma_configure: Free old DMA map range

When unbinding/binding a driver with DMA mapped memory, the DMA map is not freed when the driver is reloaded. This leads to a memory leak when the DMA map is overwritten when reprobing the driver.

This can be reproduced with a platform driver having a dma-range:

dummy {
	...
	#address-cells = <0x2>;
	#size-cells = <0x2>;
	ranges;
	dma-ranges = <...>;
	...
};

and then unbinding/binding it:

~# echo soc:dummy >/sys/bus/platform/drivers/<driver>/unbind

DMA map object 0xffffff800b0ae540 still being held by &pdev->dev

~# echo soc:dummy >/sys/bus/platform/drivers/<driver>/bind ~# echo scan > /sys/kernel/debug/kmemleak
~# cat /sys/kernel/debug/kmemleak
unreferenced object 0xffffff800b0ae540 (size 64):
comm "sh", pid 833, jiffies 4295174550 (age 2535.352s) hex dump (first 32 bytes):

    00 00 00 80 00 00 00 00 00 00 00 00 00 00 00 00  ................
    00 00 00 80 00 00 00 00 00 00 00 80 00 00 00 00  ................
  backtrace:
    [<ffffffefd1694708>] create_object.isra.0+0x108/0x344
    [<ffffffefd1d1a850>] kmemleak_alloc+0x8c/0xd0
    [<ffffffefd167e2d0>] __kmalloc+0x440/0x6f0
    [<ffffffefd1a960a4>] of_dma_get_range+0x124/0x220
    [<ffffffefd1a8ce90>] of_dma_configure_id+0x40/0x2d0
    [<ffffffefd198b68c>] platform_dma_configure+0x5c/0xa4
    [<ffffffefd198846c>] really_probe+0x8c/0x514
    [<ffffffefd1988990>] __driver_probe_device+0x9c/0x19c
    [<ffffffefd1988cd8>] device_driver_attach+0x54/0xbc
    [<ffffffefd1986634>] bind_store+0xc4/0x120
    [<ffffffefd19856e0>] drv_attr_store+0x30/0x44
    [<ffffffefd173c9b0>] sysfs_kf_write+0x50/0x60
    [<ffffffefd173c1c4>] kernfs_fop_write_iter+0x124/0x1b4
    [<ffffffefd16a013c>] new_sync_write+0xdc/0x160
    [<ffffffefd16a256c>] vfs_write+0x23c/0x2a0
    [<ffffffefd16a2758>] ksys_write+0x64/0xec

Prevent overwriting the dma_range_map by freeing it before saving the new map.

---
 drivers/of/device.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/of/device.c +6 -0
index 874f031442dc..a67703d09bfd 100644
@@ -156,6 +156,12 @@ int of_dma_configure_id(struct device *dev, struct device_node *np,
 			kfree(map);
 			return -EINVAL;
 		}
+
+		/*
+		 * Since we are about to set a new range map we should make sure we
+		 * do not overwrite any existing one without having freed it first.
+		 */
+		kfree(dev->dma_range_map);
 	}
 
 	/*
Signature · 1 line
2.30.2
maintainer
#1 lore raw
Quoted 60 lines · Mårten Lindahl

When unbinding/binding a driver with DMA mapped memory, the DMA map is not freed when the driver is reloaded. This leads to a memory leak when the DMA map is overwritten when reprobing the driver.

This can be reproduced with a platform driver having a dma-range:

dummy {
	...
	#address-cells = <0x2>;
	#size-cells = <0x2>;
	ranges;
	dma-ranges = <...>;
	...
};

and then unbinding/binding it:

~# echo soc:dummy >/sys/bus/platform/drivers/<driver>/unbind

DMA map object 0xffffff800b0ae540 still being held by &pdev->dev

~# echo soc:dummy >/sys/bus/platform/drivers/<driver>/bind ~# echo scan > /sys/kernel/debug/kmemleak
~# cat /sys/kernel/debug/kmemleak
unreferenced object 0xffffff800b0ae540 (size 64):
comm "sh", pid 833, jiffies 4295174550 (age 2535.352s) hex dump (first 32 bytes):

    00 00 00 80 00 00 00 00 00 00 00 00 00 00 00 00  ................
    00 00 00 80 00 00 00 00 00 00 00 80 00 00 00 00  ................
  backtrace:
    [<ffffffefd1694708>] create_object.isra.0+0x108/0x344
    [<ffffffefd1d1a850>] kmemleak_alloc+0x8c/0xd0
    [<ffffffefd167e2d0>] __kmalloc+0x440/0x6f0
    [<ffffffefd1a960a4>] of_dma_get_range+0x124/0x220
    [<ffffffefd1a8ce90>] of_dma_configure_id+0x40/0x2d0
    [<ffffffefd198b68c>] platform_dma_configure+0x5c/0xa4
    [<ffffffefd198846c>] really_probe+0x8c/0x514
    [<ffffffefd1988990>] __driver_probe_device+0x9c/0x19c
    [<ffffffefd1988cd8>] device_driver_attach+0x54/0xbc
    [<ffffffefd1986634>] bind_store+0xc4/0x120
    [<ffffffefd19856e0>] drv_attr_store+0x30/0x44
    [<ffffffefd173c9b0>] sysfs_kf_write+0x50/0x60
    [<ffffffefd173c1c4>] kernfs_fop_write_iter+0x124/0x1b4
    [<ffffffefd16a013c>] new_sync_write+0xdc/0x160
    [<ffffffefd16a256c>] vfs_write+0x23c/0x2a0
    [<ffffffefd16a2758>] ksys_write+0x64/0xec

Prevent overwriting the dma_range_map by freeing it before saving the new map.

---
 drivers/of/device.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/of/device.c +0 -0
index 874f031442dc..a67703d09bfd 100644

drivers/of/device.c int of_dma_configure_id(struct device *dev, struct device_node *np, · line 164

Quoted 9 lines
drivers/of/device.c +6 -0
@@ -156,6 +156,12 @@ int of_dma_configure_id(struct device *dev, struct device_node *np,
 			kfree(map);
 			return -EINVAL;
 		}
+
+		/*
+		 * Since we are about to set a new range map we should make sure we
+		 * do not overwrite any existing one without having freed it first.
+		 */
+		kfree(dev->dma_range_map);

The map can't change, can't we just reuse the existing map. Or just bail early? I suppose the driver could have modified the dma masks. Or the IOMMU config could have changed.

Rob

#2 lore raw
Quoted 56 lines · Rob Herring
Quoted 55 lines · Mårten Lindahl

When unbinding/binding a driver with DMA mapped memory, the DMA map is not freed when the driver is reloaded. This leads to a memory leak when the DMA map is overwritten when reprobing the driver.

This can be reproduced with a platform driver having a dma-range:

dummy {
	...
	#address-cells = <0x2>;
	#size-cells = <0x2>;
	ranges;
	dma-ranges = <...>;
	...
};

and then unbinding/binding it:

~# echo soc:dummy >/sys/bus/platform/drivers/<driver>/unbind

DMA map object 0xffffff800b0ae540 still being held by &pdev->dev

~# echo soc:dummy >/sys/bus/platform/drivers/<driver>/bind ~# echo scan > /sys/kernel/debug/kmemleak
~# cat /sys/kernel/debug/kmemleak
unreferenced object 0xffffff800b0ae540 (size 64):
comm "sh", pid 833, jiffies 4295174550 (age 2535.352s) hex dump (first 32 bytes):

    00 00 00 80 00 00 00 00 00 00 00 00 00 00 00 00  ................
    00 00 00 80 00 00 00 00 00 00 00 80 00 00 00 00  ................
  backtrace:
    [<ffffffefd1694708>] create_object.isra.0+0x108/0x344
    [<ffffffefd1d1a850>] kmemleak_alloc+0x8c/0xd0
    [<ffffffefd167e2d0>] __kmalloc+0x440/0x6f0
    [<ffffffefd1a960a4>] of_dma_get_range+0x124/0x220
    [<ffffffefd1a8ce90>] of_dma_configure_id+0x40/0x2d0
    [<ffffffefd198b68c>] platform_dma_configure+0x5c/0xa4
    [<ffffffefd198846c>] really_probe+0x8c/0x514
    [<ffffffefd1988990>] __driver_probe_device+0x9c/0x19c
    [<ffffffefd1988cd8>] device_driver_attach+0x54/0xbc
    [<ffffffefd1986634>] bind_store+0xc4/0x120
    [<ffffffefd19856e0>] drv_attr_store+0x30/0x44
    [<ffffffefd173c9b0>] sysfs_kf_write+0x50/0x60
    [<ffffffefd173c1c4>] kernfs_fop_write_iter+0x124/0x1b4
    [<ffffffefd16a013c>] new_sync_write+0xdc/0x160
    [<ffffffefd16a256c>] vfs_write+0x23c/0x2a0
    [<ffffffefd16a2758>] ksys_write+0x64/0xec

Prevent overwriting the dma_range_map by freeing it before saving the new map.

---
 drivers/of/device.c | 6 ++++++
 1 file changed, 6 insertions(+)
Quoted 15 lines
Quoted 5 lines
drivers/of/device.c +0 -0
index 874f031442dc..a67703d09bfd 100644

drivers/of/device.c int of_dma_configure_id(struct device *dev, struct device_node *np, · line 164

Quoted 9 lines
drivers/of/device.c +6 -0
@@ -156,6 +156,12 @@ int of_dma_configure_id(struct device *dev, struct device_node *np,
 			kfree(map);
 			return -EINVAL;
 		}
+
+		/*
+		 * Since we are about to set a new range map we should make sure we
+		 * do not overwrite any existing one without having freed it first.
+		 */
+		kfree(dev->dma_range_map);

Hi Rob!
Thanks for looking at this.

Quoted 4 lines

The map can't change, can't we just reuse the existing map. Or just bail early? I suppose the driver could have modified the dma masks. Or the IOMMU config could have changed.

Yes, we can reuse the existing map. I will make a check for that instead.

Kind regards
Mårten

Quoted 2 lines

Rob