dmaengine: change dmaengine_get_unmap_data() first arg to dma_chan
Subsystems (5)
PATCH v4 1/2 dmaengine: change dmaengine_get_unmap_data() first arg to dma_chan
From: Frank Li <[email protected]>
Replace the struct device *dev argument with struct dma_chan *chan so callers do not need to reach into the dma_device internals to obtain the device pointer. The implementation now derives the device via dmaengine_get_dma_device(chan), which correctly handles the per-channel IOMMU mapping case.
Prepare to make dma_device private and make dmaengine API consistent.
---
V4: none, just respin to miss patch2
V3 split from origianl thread
https://lore.kernel.org/dmaengine/[email protected]/
V2 change collect Dave Jiang ack tags
v1 version at
https://lore.kernel.org/imx/[email protected]/T/#t
--- crypto/async_tx/async_memcpy.c | 4 ++-- crypto/async_tx/async_pq.c | 8 ++++---- crypto/async_tx/async_raid6_recov.c | 8 ++++---- crypto/async_tx/async_xor.c | 8 ++++---- drivers/dma/dmaengine.c | 4 ++-- drivers/dma/dmatest.c | 2 +- drivers/dma/mv_xor.c | 4 ++-- drivers/ntb/ntb_transport.c | 4 ++-- drivers/ntb/test/ntb_perf.c | 2 +- include/linux/dmaengine.h | 4 ++-- 10 files changed, 24 insertions(+), 24 deletions(-)
crypto/async_tx/async_memcpy.c +2 -2
@@ -39,8 +39,8 @@ async_memcpy(struct page *dest, struct page *src, unsigned int dest_offset,struct dma_async_tx_descriptor *tx =NULL ;struct dmaengine_unmap_data *unmap =NULL ; -if (device) - unmap =dmaengine_get_unmap_data (device->dev,2 ,GFP_NOWAIT ); +if (chan) + unmap =dmaengine_get_unmap_data (chan,2 ,GFP_NOWAIT );if (unmap &&is_dma_copy_aligned (device, src_offset, dest_offset, len)) {unsigned long dma_prep_flags =0 ;
crypto/async_tx/async_pq.c +4 -4
@@ -187,8 +187,8 @@ async_gen_syndrome(struct page **blocks, unsigned int *offsets, int disks,BUG_ON (disks >MAX_DISKS || !(P (blocks, disks) ||Q (blocks, disks))); -if (device) - unmap =dmaengine_get_unmap_data (device->dev, disks,GFP_NOWAIT ); +if (chan) + unmap =dmaengine_get_unmap_data (chan, disks,GFP_NOWAIT );/* XORing P/Q is only implemented in software */ if (unmap && !(submit->flags & ASYNC_TX_PQ_XOR_DST) &&
@@ -309,8 +309,8 @@ async_syndrome_val(struct page **blocks, unsigned int *offsets, int disks,BUG_ON (disks <4 || disks >MAX_DISKS ); -if (device) - unmap =dmaengine_get_unmap_data (device->dev, disks,GFP_NOWAIT ); +if (chan) + unmap =dmaengine_get_unmap_data (chan, disks,GFP_NOWAIT );if (unmap && disks <=dma_maxpq (device,0 ) &&is_dma_pq_aligned_offs (device, offsets, disks, len)) {
crypto/async_tx/async_raid6_recov.c +4 -4
@@ -28,8 +28,8 @@ async_sum_product(struct page *dest, unsigned int d_off,u8 ax, bx;u8 *a, *b, *c; -if (dma) - unmap =dmaengine_get_unmap_data (dma->dev,3 ,GFP_NOWAIT ); +if (chan) + unmap =dmaengine_get_unmap_data (chan,3 ,GFP_NOWAIT );if (unmap) {struct device *dev = dma->dev;
@@ -96,8 +96,8 @@ async_mult(struct page *dest, unsigned int d_off, struct page *src,const u8 *qmul;/* Q multiplier table */ u8 *d, *s; -if (dma) - unmap =dmaengine_get_unmap_data (dma->dev,3 ,GFP_NOWAIT ); +if (chan) + unmap =dmaengine_get_unmap_data (chan,3 ,GFP_NOWAIT );if (unmap) {dma_addr_t dma_dest[2 ];
crypto/async_tx/async_xor.c +4 -4
@@ -178,8 +178,8 @@ async_xor_offs(struct page *dest, unsigned int offset,BUG_ON (src_cnt <=1 ); -if (device) - unmap =dmaengine_get_unmap_data (device->dev, src_cnt+1 ,GFP_NOWAIT ); +if (chan) + unmap =dmaengine_get_unmap_data (chan, src_cnt +1 ,GFP_NOWAIT );if (unmap &&dma_xor_aligned_offsets (device, offset, src_offs, src_cnt, len)) {
@@ -310,8 +310,8 @@ async_xor_val_offs(struct page *dest, unsigned int offset,BUG_ON (src_cnt <=1 ); -if (device) - unmap =dmaengine_get_unmap_data (device->dev, src_cnt,GFP_NOWAIT ); +if (chan) + unmap =dmaengine_get_unmap_data (chan, src_cnt,GFP_NOWAIT );if (unmap && src_cnt <= device->max_xor &&dma_xor_aligned_offsets (device, offset, src_offs, src_cnt, len)) {
drivers/dma/dmaengine.c +2 -2
@@ -1477,7 +1477,7 @@ static int __init dmaengine_init_unmap_pool(void) }struct dmaengine_unmap_data * -dmaengine_get_unmap_data (struct device *dev,int nr,gfp_t flags) +dmaengine_get_unmap_data (struct dma_chan *chan,int nr,gfp_t flags) {struct dmaengine_unmap_data *unmap;
@@ -1487,7 +1487,7 @@ dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags)memset (unmap,0 ,sizeof (*unmap));kref_init (&unmap->kref); - unmap->dev = dev; + unmap->dev =dmaengine_get_dma_device (chan); unmap->map_cnt = nr;return unmap;
drivers/dma/dmatest.c +1 -1
@@ -748,7 +748,7 @@ static int dmatest_func(void *data) filltime =ktime_add (filltime, diff); } - um =dmaengine_get_unmap_data (dma_dev, src->cnt + dst->cnt, + um =dmaengine_get_unmap_data (chan, src->cnt + dst->cnt,GFP_KERNEL );if (!um) { failed_tests++;
drivers/dma/mv_xor.c +2 -2
@@ -794,7 +794,7 @@ static int mv_chan_memcpy_self_test(struct mv_xor_chan *mv_chan) goto out; } - unmap =dmaengine_get_unmap_data (dma_chan->device->dev,2 ,GFP_KERNEL ); + unmap =dmaengine_get_unmap_data (dma_chan,2 ,GFP_KERNEL );if (!unmap) { err = -ENOMEM ; goto free_resources;
@@ -927,7 +927,7 @@ mv_chan_xor_self_test(struct mv_xor_chan *mv_chan) goto out; } - unmap =dmaengine_get_unmap_data (dma_chan->device->dev, src_count +1 , + unmap =dmaengine_get_unmap_data (dma_chan, src_count +1 ,GFP_KERNEL );if (!unmap) { err = -ENOMEM ;
drivers/ntb/ntb_transport.c +2 -2
@@ -1532,7 +1532,7 @@ static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset)if (!is_dma_copy_aligned (device, pay_off, buff_off, len)) goto err; - unmap =dmaengine_get_unmap_data (device->dev,2 ,GFP_NOWAIT ); + unmap =dmaengine_get_unmap_data (chan,2 ,GFP_NOWAIT );if (!unmap) goto err;
@@ -1872,7 +1872,7 @@ static int ntb_async_tx_submit(struct ntb_transport_qp *qp,if (!is_dma_copy_aligned (device, buff_off, dest_off, len)) goto err; - unmap =dmaengine_get_unmap_data (device->dev,1 ,GFP_NOWAIT ); + unmap =dmaengine_get_unmap_data (chan,1 ,GFP_NOWAIT );if (!unmap) goto err;
drivers/ntb/test/ntb_perf.c +1 -1
@@ -809,7 +809,7 @@ static int perf_copy_chunk(struct perf_thread *pthr, dst_vaddr = dst; dst_dma_addr = peer->dma_dst_addr + (dst_vaddr - vbase); - unmap =dmaengine_get_unmap_data (dma_dev,1 ,GFP_NOWAIT ); + unmap =dmaengine_get_unmap_data (pthr->dma_chan,1 ,GFP_NOWAIT );if (!unmap)return -ENOMEM ;
include/linux/dmaengine.h +2 -2
@@ -676,7 +676,7 @@ static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx, }struct dmaengine_unmap_data * -dmaengine_get_unmap_data (struct device *dev,int nr,gfp_t flags); +dmaengine_get_unmap_data (struct dma_chan *chan,int nr,gfp_t flags);void dmaengine_unmap_put (struct dmaengine_unmap_data *unmap);#else static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx,
@@ -684,7 +684,7 @@ static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx, { }static inline struct dmaengine_unmap_data * -dmaengine_get_unmap_data (struct device *dev,int nr,gfp_t flags) +dmaengine_get_unmap_data (struct dma_chan *chan,int nr,gfp_t flags) {return NULL ; }
Signature · 1 line
2.43.0
PATCH v4 2/2 dmaengine: replace dma_maxqp() with dmaengine_maxpq() taking struct dma_chan *
From: Frank Li <[email protected]>
Replace dma_maxpq() with dmaengine_maxpq(chan, flags) as a channel-based API, consistent with the convention of other dmaengine_*() helpers that take struct dma_chan * rather than struct dma_device *.
---
change in v4: none, just respin because missed patch 2 Change in v3
split from original thread
https://lore.kernel.org/dmaengine/[email protected]/
--- crypto/async_tx/async_pq.c | 8 ++++---- drivers/dma/dmatest.c | 2 +- include/linux/dmaengine.h | 6 ++++-- 3 files changed, 9 insertions(+), 7 deletions(-)
crypto/async_tx/async_pq.c +4 -4
@@ -51,7 +51,7 @@ do_async_gen_syndrome(struct dma_chan *chan,while (src_cnt >0 ) { submit->flags = flags_orig; - pq_src_cnt =min (src_cnt,dma_maxpq (dma, dma_flags)); + pq_src_cnt =min (src_cnt,dmaengine_maxpq (chan, dma_flags));/* if we are submitting additional pqs, leave the chain open, * clear the callback parameters, and leave the destination * buffers mapped
@@ -192,8 +192,8 @@ async_gen_syndrome(struct page **blocks, unsigned int *offsets, int disks,/* XORing P/Q is only implemented in software */ if (unmap && !(submit->flags &ASYNC_TX_PQ_XOR_DST ) && - (src_cnt <=dma_maxpq (device,0 ) || -dma_maxpq (device,DMA_PREP_CONTINUE ) >0 ) && + (src_cnt <=dmaengine_maxpq (chan,0 ) || +dmaengine_maxpq (chan,DMA_PREP_CONTINUE ) >0 ) &&is_dma_pq_aligned_offs (device, offsets, disks, len)) {struct dma_async_tx_descriptor *tx;enum dma_ctrl_flags dma_flags =0 ;
@@ -312,7 +312,7 @@ async_syndrome_val(struct page **blocks, unsigned int *offsets, int disks,if (chan) unmap =dmaengine_get_unmap_data (chan, disks,GFP_NOWAIT ); -if (unmap && disks <=dma_maxpq (device,0 ) && +if (unmap && disks <=dmaengine_maxpq (chan,0 ) &&is_dma_pq_aligned_offs (device, offsets, disks, len)) {struct device *dev = device->dev;dma_addr_t pq[2 ];
drivers/dma/dmatest.c +1 -1
@@ -636,7 +636,7 @@ static int dmatest_func(void *data) params->alignment; }else if (thread->type ==DMA_PQ ) {/* force odd to ensure dst = src */ - src->cnt =min_odd (params->pq_sources |1 ,dma_maxpq (dev,0 )); + src->cnt =min_odd (params->pq_sources |1 ,dmaengine_maxpq (chan,0 )); dst->cnt =2 ; align = params->alignment <0 ? dev->pq_align : params->alignment;
include/linux/dmaengine.h +4 -2
@@ -1720,7 +1720,7 @@ static inline unsigned short dma_dev_to_maxpq(struct dma_device *dma)return dma->max_pq & ~DMA_HAS_PQ_CONTINUE ; } -/* dma_maxpq - reduce maxpq in the face of continued operations +/* dmaengine_maxpq - reduce maxpq in the face of continued operations * @dma - dma device with PQ capability * @flags - to check if DMA_PREP_CONTINUE and DMA_PREP_PQ_DISABLE_P are set *
@@ -1733,8 +1733,10 @@ static inline unsigned short dma_dev_to_maxpq(struct dma_device *dma)* In the case where P is disabled we only need 1 extra source: * 1/ {01} * Q : use Q to continue Q' calculation */ -static inline int dma_maxpq (struct dma_device *dma,enum dma_ctrl_flags flags) +static inline int dmaengine_maxpq (struct dma_chan *chan,enum dma_ctrl_flags flags) { +struct dma_device *dma = chan->device; +if (dma_dev_has_pq_continue (dma) || !dmaf_continue (flags))return dma_dev_to_maxpq (dma);if (dmaf_p_disabled_continue (flags))
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v4 2/2] dmaengine: replace dma_maxqp() with dmaengine_maxpq() taking struct dma_chan *
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] dmaengine: stale documentation and comments after dma_maxpq rename --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] dmaengine: stale documentation and comments after dma_maxpq rename --
commit d1c8559bd7d041826990cfe2359c6880e20a68d4
Author: Frank Li <[email protected]>
dmaengine: replace dma_maxqp() with dmaengine_maxpq() taking struct dma_chan *
This commit renames dma_maxpq() to dmaengine_maxpq() and updates it to take a struct dma_chan pointer instead of a struct dma_device pointer. It also updates the callers in the async_tx and dmatest subsystems to use the new API convention.
Quoted 4 lines
include/linux/dmaengine.h +0
-0
[ ... ]
include/linux/dmaengine.h static inline unsigned short dma_dev_to_maxpq(struct dma_device *dma) · line 1447
Quoted 7 lines
include/linux/dmaengine.h +1
-1@@ -1443,7 +1443,7 @@ static inline unsigned short dma_dev_to_maxpq(struct dma_device *dma)return dma->max_pq & ~DMA_HAS_PQ_CONTINUE ; } -/* dma_maxpq - reduce maxpq in the face of continued operations +/* dmaengine_maxpq - reduce maxpq in the face of continued operations * @dma - dma device with PQ capability
[Severity: Low]
Should this kernel-doc comment be updated to document the new @chan parameter instead of the removed @dma parameter?
Additionally, it looks like there is a stale reference to the old function name in drivers/dma/ioat/prep.c that says "see the comment for dma_maxpq in include/linux/dmaengine.h". Does that file need to be updated in this patch to reflect the rename?
Quoted 9 lines
include/linux/dmaengine.h +1
-1* @flags - to check if DMA_PREP_CONTINUE and DMA_PREP_PQ_DISABLE_P are set * @@ -1456,8 +1456,10 @@ static inline unsigned short dma_dev_to_maxpq(struct dma_device *dma)* In the case where P is disabled we only need 1 extra source: * 1/ {01} * Q : use Q to continue Q' calculation */ -static inline int dma_maxpq (struct dma_device *dma,enum dma_ctrl_flags flags) +static inline int dmaengine_maxpq (struct dma_chan *chan,enum dma_ctrl_flags flags) {
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] dmaengine: Mismatched DMA device used for mapping and unmapping - [High] dmaengine: NULL pointer…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] dmaengine: Mismatched DMA device used for mapping and unmapping
- [High] dmaengine: NULL pointer dereference in dmaengine_get_dma_device() during mv_xor probe --
commit 811fe2fcd19af9f3eb46610a12e2d1dc77b628e0
Author: Frank Li <[email protected]>
dmaengine: change dmaengine_get_unmap_data() first arg to dma_chan
This commit modifies dmaengine_get_unmap_data() to accept a dma_chan instead of a device pointer, automatically resolving the correct DMA device for the channel. It updates various callers in the crypto, NTB, and DMA test subsystems to match this new signature.
crypto/async_tx/async_memcpy.c line 43
Quoted 11 lines
crypto/async_tx/async_memcpy.c +2
-2@@ -39,8 +39,8 @@struct dma_async_tx_descriptor *tx =NULL ;struct dmaengine_unmap_data *unmap =NULL ; -if (device) - unmap =dmaengine_get_unmap_data (device->dev,2 ,GFP_NOWAIT ); +if (chan) + unmap =dmaengine_get_unmap_data (chan,2 ,GFP_NOWAIT );
[Severity: High]
Will this create a DMA API mapping mismatch for systems with per-channel IOMMUs?
The updated dmaengine_get_unmap_data() now sets unmap->dev using dmaengine_get_dma_device(chan). If the channel supports a per-channel IOMMU, this correctly resolves to the channel's specific device.
However, the memory mapping calls later in async_memcpy() still use the parent device:
unmap->addr[0] = dma_map_page(device->dev, src, src_offset, len,
DMA_TO_DEVICE);
When the operation finishes, dmaengine_unmap() will attempt to unmap the memory from unmap->dev (the child IOMMU device), while it was mapped to device->dev (the parent device). This asymmetry can cause stranded IOMMU mappings and invalid unmap page faults.
A similar mismatch appears in drivers/ntb/ntb_transport.c in ntb_async_rx_submit(), which maps using device->dev instead of resolving through the channel.
[ ... ]
drivers/dma/mv_xor.c line 792
Quoted 9 lines
drivers/dma/mv_xor.c +1
-1@@ -789,7 +789,7 @@ goto out; } - unmap =dmaengine_get_unmap_data (dma_chan->device->dev,2 ,GFP_KERNEL ); + unmap =dmaengine_get_unmap_data (dma_chan,2 ,GFP_KERNEL );
[Severity: High]
Does this introduce a NULL pointer dereference during device probe?
In the mv_xor driver, mv_chan_memcpy_self_test() is called during driver initialization via mv_xor_channel_add(). This happens before the channel is fully registered with the DMA core.
At this point, the channel's sysfs device pointer chan->dev is not yet allocated. When this patch changes the call to pass dma_chan instead of dma_chan->device->dev, the implementation of dmaengine_get_unmap_data() will call dmaengine_get_dma_device(chan).
Looking at include/linux/dmaengine.h:
static inline struct device *dmaengine_get_dma_device(struct dma_chan *chan)
{
if (chan->dev->chan_dma_dev)
return &chan->dev->device;
return chan->device->dev;
}This unconditionally dereferences chan->dev, which is NULL during the self-test, preventing the mv_xor driver from loading.
Quoted 2 lines
drivers/dma/mv_xor.c +0
-0if (!unmap) { err = -ENOMEM ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1