kernel_optimize_test/drivers/base
Mårten Lindahl 8fe4da5524 driver core: Free DMA range map when device is released
commit d8f7a5484f2188e9af2d9e4e587587d724501b12 upstream.

When unbinding/binding a driver with DMA mapped memory, the DMA map is
not freed before 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

To prevent this we should free the dma_range_map when the device is
released.

Fixes: e0d072782c ("dma-mapping: introduce DMA range map, supplanting dma_pfn_offset")
Cc: stable <stable@vger.kernel.org>
Suggested-by: Rob Herring <robh@kernel.org>
Reviewed-by: Rob Herring <robh@kernel.org>
Signed-off-by: Mårten Lindahl <marten.lindahl@axis.com>
Link: https://lore.kernel.org/r/20220216094128.4025861-1-marten.lindahl@axis.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2022-03-02 11:42:56 +01:00
..
firmware_loader firmware_loader: fix pre-allocated buf built-in firmware use 2021-11-26 10:39:10 +01:00
power PM: s2idle: ACPI: Fix wakeup interrupts handling 2022-02-16 12:54:22 +01:00
regmap regmap-irq: Update interrupt clear register for proper reset 2022-03-02 11:42:52 +01:00
test drivers/base: build kunit tests without structleak plugin 2021-03-17 17:06:24 +01:00
arch_topology.c Driver Core patches for 5.10-rc1 2020-10-14 16:09:32 -07:00
attribute_container.c
base.h driver core: add deferring probe reason to devices_deferred property 2020-07-30 09:03:43 +02:00
bus.c drivers core: Miscellaneous changes for sysfs_emit 2020-10-02 13:12:07 +02:00
cacheinfo.c drivers core: Use sysfs_emit for shared_cpu_map_show and shared_cpu_list_show 2020-10-02 13:24:40 +02:00
class.c drivers core: Miscellaneous changes for sysfs_emit 2020-10-02 13:12:07 +02:00
component.c
container.c
core.c PM: runtime: Add safety net to supplier device release 2022-01-27 10:54:17 +01:00
cpu.c drivers core: Miscellaneous changes for sysfs_emit 2020-10-02 13:12:07 +02:00
dd.c driver core: Free DMA range map when device is released 2022-03-02 11:42:56 +01:00
devcoredump.c drivers core: Miscellaneous changes for sysfs_emit 2020-10-02 13:12:07 +02:00
devres.c devres: provide devm_krealloc() 2020-09-08 13:32:06 +02:00
devtmpfs.c devtmpfs regression fix: reconfigure on each mount 2022-01-20 09:17:49 +01:00
driver.c drivers: base: Convert to printk alias functions 2020-07-10 14:16:44 +02:00
firmware.c
hypervisor.c
init.c
isa.c
Kconfig
Makefile device property: Move fwnode_connection_find_match() under drivers/base/property.c 2020-09-08 13:32:06 +02:00
map.c
memory.c drivers/base/memory: don't store phys_device in memory blocks 2021-03-17 17:06:25 +01:00
module.c
node.c node: fix device cleanups in error handling code 2021-05-14 09:50:19 +02:00
pinctrl.c
platform-msi.c
platform.c drivers core: Miscellaneous changes for sysfs_emit 2020-10-02 13:12:07 +02:00
property.c device property: Fix fwnode_graph_devcon_match() fwnode leak 2022-01-27 10:54:25 +01:00
soc.c drivers core: Miscellaneous changes for sysfs_emit 2020-10-02 13:12:07 +02:00
swnode.c software node: fix wrong node passed to find nargs_prop 2022-01-27 10:53:59 +01:00
syscore.c syscore: Use pm_pr_dbg() for syscore_{suspend,resume}() 2020-09-08 13:32:06 +02:00
topology.c drivers core: Miscellaneous changes for sysfs_emit 2020-10-02 13:12:07 +02:00
transport_class.c