Skip to contentSkip to conversation

blk-mq-dma: always initialize dma state

PATCH Superseded Keith Busch 4 participants 5 messages Dec 10, 2025 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 blk-mq-dma: always initialize dma state

From: Keith Busch <[email protected]>

Ensure the dma state is initialized when we're not using the contiguous iova, otherwise the caller may be using a stale state from a previous request that could use the coalesed iova allocation.

Fixes 2f6b2565d43cdb5 ("block: accumulate memory segment gaps per bio")
---
 block/blk-mq-dma.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
block/blk-mq-dma.c +3 -2
index e9108ccaf4b06..4ca768e0cc7eb 100644
@@ -196,8 +196,9 @@ static bool blk_dma_map_iter_start(struct request *req, struct device *dma_dev,
 		return false;
 	}
 
-	if (blk_can_dma_map_iova(req, dma_dev) &&
-	    dma_iova_try_alloc(dma_dev, state, vec.paddr, total_len))
+	if (!blk_can_dma_map_iova(req, dma_dev))
+		memset(state, 0, sizeof(*state));
+	else if (dma_iova_try_alloc(dma_dev, state, vec.paddr, total_len))
 		return blk_rq_dma_map_iova(req, dma_dev, state, iter, &vec);
 	return blk_dma_map_direct(req, dma_dev, iter, &vec);
 }
Signature · 1 line
2.47.3
#1 lore raw

block/blk-mq-dma.c static bool blk_dma_map_iter_start(struct request *req, struct device *dma_dev, · line 202

Quoted 6 lines · Keith Busch
block/blk-mq-dma.c +3 -2
-	if (blk_can_dma_map_iova(req, dma_dev) &&
-	    dma_iova_try_alloc(dma_dev, state, vec.paddr, total_len))
+	if (!blk_can_dma_map_iova(req, dma_dev))
+		memset(state, 0, sizeof(*state));
+	else if (dma_iova_try_alloc(dma_dev, state, vec.paddr, total_len))
 		return blk_rq_dma_map_iova(req, dma_dev, state, iter, &vec);

What about just doing the memset unconditionally? It's just two 64-bit fields so no real overhead, and it gives us a clean slate that avoid introducing other bugs later on.

#2 lore raw
Quoted 11 lines · Christoph Hellwig

block/blk-mq-dma.c static bool blk_dma_map_iter_start(struct request *req, struct device *dma_dev, · line 202

Quoted 6 lines · Keith Busch
block/blk-mq-dma.c +3 -2
-	if (blk_can_dma_map_iova(req, dma_dev) &&
-	    dma_iova_try_alloc(dma_dev, state, vec.paddr, total_len))
+	if (!blk_can_dma_map_iova(req, dma_dev))
+		memset(state, 0, sizeof(*state));
+	else if (dma_iova_try_alloc(dma_dev, state, vec.paddr, total_len))
 		return blk_rq_dma_map_iova(req, dma_dev, state, iter, &vec);

What about just doing the memset unconditionally? It's just two 64-bit fields so no real overhead, and it gives us a clean slate that avoid introducing other bugs later on.

I didn't do that is because dma_iova_try_alloc() also does that, so it'd be two repeated and unnecessary memset's on the same address. That feels undesirable no matter how small the struct is. I could remove the memset in dma_iova_try_alloc instead and make the caller initialize it. There are only two existing users of the API, so not a big deal to change it.

#3 lore raw
Quoted 13 lines · Keith Busch
Quoted 11 lines · Christoph Hellwig

block/blk-mq-dma.c static bool blk_dma_map_iter_start(struct request *req, struct device *dma_dev, · line 202

Quoted 6 lines · Keith Busch
block/blk-mq-dma.c +3 -2
-	if (blk_can_dma_map_iova(req, dma_dev) &&
-	    dma_iova_try_alloc(dma_dev, state, vec.paddr, total_len))
+	if (!blk_can_dma_map_iova(req, dma_dev))
+		memset(state, 0, sizeof(*state));
+	else if (dma_iova_try_alloc(dma_dev, state, vec.paddr, total_len))
 		return blk_rq_dma_map_iova(req, dma_dev, state, iter, &vec);

What about just doing the memset unconditionally? It's just two 64-bit fields so no real overhead, and it gives us a clean slate that avoid introducing other bugs later on.

Quoted 5 lines

I didn't do that is because dma_iova_try_alloc() also does that, so it'd be two repeated and unnecessary memset's on the same address. That feels undesirable no matter how small the struct is. I could remove the memset in dma_iova_try_alloc instead and make the caller initialize it. There are only two existing users of the API, so not a big deal to change it.

If we move it out we'll need a separate dma_iova_init helper or similar (which I think Leon had in earlier version before I optimized it away). Personally I'd prefer to just zero twice, as it's really cheap.

Tested-by on v1 1/1
#4 lore raw
In reply to Keith Busch
Quoted 20 lines · Keith Busch

From: Keith Busch <[email protected]>

Ensure the dma state is initialized when we're not using the contiguous iova, otherwise the caller may be using a stale state from a previous request that could use the coalesed iova allocation.

Fixes 2f6b2565d43cdb5 ("block: accumulate memory segment gaps per bio")

---
block/blk-mq-dma.c | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)
block/blk-mq-dma.c +0 -0
index e9108ccaf4b06..4ca768e0cc7eb 100644
@@ -196,8 +196,9 @@ static bool blk_dma_map_iter_start(struct request *req, struct device *dma_dev,
 		return false;
 	}

block/blk-mq-dma.c static bool blk_dma_map_iter_start(struct request *req, struct device *dma_dev,

Quoted 10 lines
block/blk-mq-dma.c +3 -3
 
-	if (blk_can_dma_map_iova(req, dma_dev) &&
-	    dma_iova_try_alloc(dma_dev, state, vec.paddr, total_len))
+	if (!blk_can_dma_map_iova(req, dma_dev))
+		memset(state, 0, sizeof(*state));
+	else if (dma_iova_try_alloc(dma_dev, state, vec.paddr, total_len))
 		return blk_rq_dma_map_iova(req, dma_dev, state, iter, &vec);
 	return blk_dma_map_direct(req, dma_dev, iter, &vec);
 }
--

Yes, that did the trick!
Tested on top of:
cc25df3e2e22 Merge tag 'for-6.19/block-20251201' of git://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux
and on top the latest upstream:
0048fbb4011e Merge tag 'locking-futex-2025-12-10' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Thanks,
Sebastian