dmaengine: Free channel and device IDs correctly
PATCH 1/1 dmaengine: Free channel and device IDs correctly
Commit 0821009445a8 ("dmaengine: fix channel index enumeration") added device->chan_ida and moved freeing device->dev_id from chan_dev_release() to dma_async_device_unregister(). Since then, two paths handle these IDs incorrectly.
__dma_async_device_channel_unregister() frees chan->chan_id before device_unregister() removes the channel's sysfs entry. A channel registered on the same device in that window can get the same ID, and its device_register() fails:
sysfs: cannot create duplicate filename '/devices/platform/...
kobject: kobject_add_internal failed for dma0chan0 with -EEXIST, ...
idxd can hit this when one wq is unbound while another wq of the same device is bound, because it holds only the per-wq lock.
When dma_async_device_register() fails after registering at least one channel, its err_out path unregisters the channel devices but frees neither the channel IDs nor device->dev_id. The next DMA device then gets the next "dma<N>" number, skipping the leaked one.
In __dma_async_device_channel_unregister(), free chan_id after device_unregister(). In the err_out path, add the missing frees: each chan_id after device_unregister(), and dev_id after the loop.
---
Notes:
Found with a static analysis tool we are developing.
Tested in QEMU (x86_64) with two test modules:
- Unregister one channel and register another on the same dma_device from a second thread. A class_interface remove_dev() callback on the dma class starts the second thread while device_del() of the first channel is in progress, to make the window deterministic. Without this patch the new channel gets ID 0 and fails with -EEXIST. With it, the new channel gets ID 1 and is registered.
- Register a dma_device whose second channel fails to register (both channels request the same static channel ID). Without this patch chan_ida is not empty afterwards and the next dma_device gets dev_id
1. With it, chan_ida is empty and dev_id 0 is reused.
Not tested on idxd hardware.
drivers/dma/dmaengine.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/dma/dmaengine.c +3 -1
@@ -1171,8 +1171,8 @@ static void __dma_async_device_channel_unregister(struct dma_device *device, device->chancnt--; chan->chan_dev->chan =NULL ;mutex_unlock (&dma_list_mutex); -ida_free (&device->chan_ida, chan->chan_id);device_unregister (dmaengine_chan_dev (chan)); +ida_free (&device->chan_ida, chan->chan_id);free_percpu (chan->local); }
@@ -1307,8 +1307,10 @@ int dma_async_device_register(struct dma_device *device) chan->chan_dev->chan =NULL ;mutex_unlock (&dma_list_mutex);device_unregister (dmaengine_chan_dev (chan)); +ida_free (&device->chan_ida, chan->chan_id);free_percpu (chan->local); } +ida_free (&dma_ida, device->dev_id);return rc; }EXPORT_SYMBOL (dma_async_device_register);
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.43.0
Quoted 23 lines · Changyul Lee
[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
Commit
0821009445a8("dmaengine: fix channel index enumeration") addeddevice->chan_idaand moved freeingdevice->dev_idfromchan_dev_release()todma_async_device_unregister(). Since then, two paths handle these IDs incorrectly.
__dma_async_device_channel_unregister()freeschan->chan_idbeforedevice_unregister()removes the channel's sysfs entry. A channel registered on the same device in that window can get the same ID, and itsdevice_register()fails:sysfs: cannot create duplicate filename '/devices/platform/...
kobject:kobject_add_internalfailed for dma0chan0 with-EEXIST, ...idxd can hit this when one wq is unbound while another wq of the same device is bound, because it holds only the per-wq lock.
When
dma_async_device_register()fails after registering at least one channel, itserr_outpath unregisters the channel devices but frees neither the channel IDs nordevice->dev_id. The next DMA device then gets the next "dma<N>" number, skipping the leaked one.
Quoted 7 lines
In
__dma_async_device_channel_unregister(), freechan_idafterdevice_unregister(). In theerr_outpath, add the missing frees: eachchan_idafterdevice_unregister(), anddev_idafter the loop.Fixes 0821009445a8 ("dmaengine: fix channel index enumeration")Signed-off-by Changyul Lee <[email protected]>---
Quoted 53 lines
Notes:
Found with a static analysis tool we are developing.Tested in QEMU (
x86_64) with two test modules:- Unregister one channel and register another on the same
dma_devicefrom a second thread. Aclass_interfaceremove_dev()callback on the dma class starts the second thread whiledevice_del()of the first channel is in progress, to make the window deterministic. Without this patch the new channel gets ID 0 and fails with-EEXIST. With it, the new channel gets ID 1 and is registered.- Register a
dma_devicewhose second channel fails to register (both channels request the same static channel ID). Without this patchchan_idais not empty afterwards and the nextdma_devicegetsdev_id
1. With it,chan_idais empty anddev_id0 is reused.Not tested on idxd hardware.
drivers/dma/dmaengine.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/dma/dmaengine.c +3
-1@@ -1171,8 +1171,8 @@ static void __dma_async_device_channel_unregister(struct dma_device *device, device->chancnt--; chan->chan_dev->chan =NULL ;mutex_unlock (&dma_list_mutex); -ida_free (&device->chan_ida, chan->chan_id);device_unregister (dmaengine_chan_dev (chan)); +ida_free (&device->chan_ida, chan->chan_id);free_percpu (chan->local); }@@ -1307,8 +1307,10 @@ int dma_async_device_register(struct dma_device *device) chan->chan_dev->chan =NULL ;mutex_unlock (&dma_list_mutex);device_unregister (dmaengine_chan_dev (chan)); +ida_free (&device->chan_ida, chan->chan_id);free_percpu (chan->local); } +ida_free (&dma_ida, device->dev_id);return rc; }EXPORT_SYMBOL (dma_async_device_register);base-commit:
6628e7d4c6b25c8d18f1345e3a20d21829aae7e4--
2.43.0