Skip to contentSkip to conversation

dmaengine: Free channel and device IDs correctly

PATCH In review Changyul Lee 2 participants 2 messages Nov 6 – Nov 16 dmaengine

lore
Share & tools
lore
#0 lore raw

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.

Fixes 0821009445a8 ("dmaengine: fix channel index enumeration")

---

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
index 34be53487a21..6f9dcc735af5 100644
@@ -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
Reviewed-by on v1 1/1
#1 lore raw
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") 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.

Quoted 7 lines

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.

Fixes 0821009445a8 ("dmaengine: fix channel index enumeration")

---

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_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
index 34be53487a21..6f9dcc735af5 100644
@@ -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