iov_iter: Allow DMA-compatible device pages in bvec extraction
Subsystems (7)
PATCH v2 1/1 iov_iter: Allow DMA-compatible device pages in bvec extraction
Commit 38446014648c ("block: don't merge different kinds of P2P transfers in a single bio") made bio_add_page() reject pages from different dev_pagemaps. This was needed for PCI P2PDMA, but also rejected pages with compatible DMA mapping requirements.
Commit 41c665aae2b5 ("block: relax pgmap check in bio_add_page for compatible zone device pages") allows these pages in separate bvecs. Commit 13920e4b7b784 ("block: add pgmap check to biovec_phys_mergeable") prevents physically contiguous bvecs from different pgmaps from being merged into one segment.
But iov_iter_extract_bvecs() still rejects pages from different pgmaps without checking their DMA compatibility. Direct I/O uses this helper, so a non-atomic request submitted as a single bio transfers only the data before the pgmap boundary and returns fewer bytes than requested.
Fix this by moving zone_device_pages_compatible() from blk.h to memremap.h and using it in iov_iter_extract_bvecs(). Rename the helper to zone_device_pages_dma_compatible() to make its purpose clear.
---
Changes since v1:
https://lore.kernel.org/all/[email protected]/
Address David and Christoph's comments:
- Rename the helper to zone_device_pages_dma_compatible() and update all callers.
- Convert the comment to kernel-doc and explain DMA mapping compatibility.
- Reword the subject and commit message around DMA compatibility, with direct I/O as an example of the failure.
Other changes:
- Correct the kernel-doc: COHERENT pages can appear in bios and use the normal DMA mapping path.
block/bio-integrity.c | 2 +- block/bio.c | 2 +- block/blk.h | 19 ------------------- include/linux/memremap.h | 28 ++++++++++++++++++++++++++++ lib/iov_iter.c | 3 ++- 5 files changed, 32 insertions(+), 22 deletions(-)
block/bio-integrity.c +1 -1
@@ -230,7 +230,7 @@ int bio_integrity_add_page(struct bio *bio, struct page *page,if (bip->bip_vcnt >0 ) {struct bio_vec *bv = &bip->bip_vec[bip->bip_vcnt -1 ]; -if (!zone_device_pages_compatible (bv->bv_page, page)) +if (!zone_device_pages_dma_compatible (bv->bv_page, page))return 0 ;if (zone_device_pages_have_same_pgmap (bv->bv_page, page) &&bvec_try_merge_hw_page (q, bv, page, len, offset)) {
block/bio.c +1 -1
@@ -1057,7 +1057,7 @@ int bio_add_page(struct bio *bio, struct page *page,if (bio->bi_vcnt >0 ) {struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt -1 ]; -if (!zone_device_pages_compatible (bv->bv_page, page)) +if (!zone_device_pages_dma_compatible (bv->bv_page, page))return 0 ;if (zone_device_pages_have_same_pgmap (bv->bv_page, page) &&bvec_try_merge_page (bv, page, len, offset)) {
block/blk.h +0 -19
@@ -142,25 +142,6 @@ static inline bool biovec_phys_mergeable(struct request_queue *q,return true; } -/* -* Check if two pages from potentially different zone device pgmaps can -* coexist as separate bvec entries in the same bio. -* -* The block DMA iterator (blk_dma_map_iter_start) caches the P2PDMA mapping -* state from the first segment and applies it to all subsequent segments, so -* P2PDMA pages from different pgmaps must not be mixed in the same bio. -* -* Other zone device types (FS_DAX, GENERIC) use the same dma_map_phys() path -* as normal RAM. PRIVATE and COHERENT pages never appear in bios. -*/ -static inline bool zone_device_pages_compatible (const struct page *a, -const struct page *b) -{ -if (is_pci_p2pdma_page (a) ||is_pci_p2pdma_page (b)) -return zone_device_pages_have_same_pgmap (a, b); -return true; -} -static inline bool __bvec_gap_to_prev (const struct queue_limits *lim,struct bio_vec *bprv,unsigned int offset) {
include/linux/memremap.h +28 -0
@@ -202,6 +202,34 @@ static inline bool is_pci_p2pdma_page(const struct page *page)folio_is_pci_p2pdma (page_folio (page)); } +/** +* zone_device_pages_dma_compatible() - Check DMA mapping compatibility +* @a: First page +* @b: Second page +* +* Check whether two pages have compatible DMA mapping requirements. +* The block DMA iterator determines the P2PDMA mapping state from the +* first segment and reuses it for subsequent segments. If either page +* is P2PDMA, both must belong to the same pgmap. +* +* FS_DAX, GENERIC and COHERENT pages use the same dma_map_phys() path +* as normal RAM and may belong to different pgmaps. DEVICE_PRIVATE +* pages are not CPU-addressable and are not valid inputs to this helper. +* +* Pages from different pgmaps must still remain in separate physical +* segments. +* +* Return: %true if the pages have compatible DMA mapping requirements, +* otherwise %false. +*/ +static inline bool zone_device_pages_dma_compatible (const struct page *a, +const struct page *b) +{ +if (is_pci_p2pdma_page (a) ||is_pci_p2pdma_page (b)) +return zone_device_pages_have_same_pgmap (a, b); +return true; +} +static inline bool folio_is_device_coherent (const struct folio *folio) { return folio_is_zone_device(folio) &&
lib/iov_iter.c +2 -1
@@ -4,6 +4,7 @@#include <linux/fault-inject-usercopy.h> #include <linux/uio.h> #include <linux/pagemap.h> +#include <linux/memremap.h> #include <linux/highmem.h> #include <linux/slab.h> #include <linux/vmalloc.h>
@@ -1968,7 +1969,7 @@ ssize_t iov_iter_extract_bvecs(struct iov_iter *iter, struct bio_vec *bv,unsigned int nr_to_add;if (*nr_vecs >0 && - !zone_device_pages_have_same_pgmap (bv[*nr_vecs -1 ].bv_page, + !zone_device_pages_dma_compatible (bv[*nr_vecs -1 ].bv_page, pages[i]))break ;
Signature · 1 line
2.43.0
Looks good:
Quoted 24 lines · Naman Jain
Commit
38446014648c("block: don't merge different kinds of P2P transfers in a single bio") madebio_add_page()reject pages from differentdev_pagemaps. This was needed for PCI P2PDMA, but also rejected pages with compatible DMA mapping requirements.Commit
41c665aae2b5("block: relax pgmap check inbio_add_pagefor compatible zone device pages") allows these pages in separate bvecs. Commit13920e4b7b784("block: add pgmap check tobiovec_phys_mergeable") prevents physically contiguous bvecs from different pgmaps from being merged into one segment.But
iov_iter_extract_bvecs()still rejects pages from different pgmaps without checking their DMA compatibility. Direct I/O uses this helper, so a non-atomic request submitted as a single bio transfers only the data before the pgmap boundary and returns fewer bytes than requested.Fix this by moving
zone_device_pages_compatible()fromblk.htomemremap.hand using it iniov_iter_extract_bvecs(). Rename the helper tozone_device_pages_dma_compatible()to make its purpose clear.Fixes 38446014648c ("block: don't merge different kinds of P2P transfers in a single bio")Signed-off-by Naman Jain <[email protected]>---
[...]
Quoted 16 lines
static inline bool
folio_is_device_coherent(const struct folio *folio){include/linux/memremap.h +0
-0return folio_is_zone_device(folio) &&lib/iov_iter.c +1
-0@@ -4,6 +4,7 @@#include <linux/fault-inject-usercopy.h> #include <linux/uio.h> #include <linux/pagemap.h> +#include <linux/memremap.h> #include <linux/highmem.h> #include <linux/slab.h> #include <linux/vmalloc.h>
lib/iov_iter.c ssize_t iov_iter_extract_bvecs(struct iov_iter *iter, struct bio_vec *bv, · line 1973
Quoted 6 lines
lib/iov_iter.c +1
-1@@ -1968,7 +1969,7 @@ ssize_t iov_iter_extract_bvecs(struct iov_iter *iter, struct bio_vec *bv,unsigned int nr_to_add;if (*nr_vecs >0 && - !zone_device_pages_have_same_pgmap (bv[*nr_vecs -1 ].bv_page, + !zone_device_pages_dma_compatible (bv[*nr_vecs -1 ].bv_page, pages[i]))
Nit: ^ I'd just fit that into a single line to de-uglify.
Signature · 3 lines
Cheers, David
Quoted 46 lines · David Hildenbrand (Arm)
Quoted 24 lines · Naman Jain
Commit
38446014648c("block: don't merge different kinds of P2P transfers in a single bio") madebio_add_page()reject pages from differentdev_pagemaps. This was needed for PCI P2PDMA, but also rejected pages with compatible DMA mapping requirements.Commit
41c665aae2b5("block: relax pgmap check inbio_add_pagefor compatible zone device pages") allows these pages in separate bvecs. Commit13920e4b7b784("block: add pgmap check tobiovec_phys_mergeable") prevents physically contiguous bvecs from different pgmaps from being merged into one segment.But
iov_iter_extract_bvecs()still rejects pages from different pgmaps without checking their DMA compatibility. Direct I/O uses this helper, so a non-atomic request submitted as a single bio transfers only the data before the pgmap boundary and returns fewer bytes than requested.Fix this by moving
zone_device_pages_compatible()fromblk.htomemremap.hand using it iniov_iter_extract_bvecs(). Rename the helper tozone_device_pages_dma_compatible()to make its purpose clear.Fixes 38446014648c ("block: don't merge different kinds of P2P transfers in a single bio")Signed-off-by Naman Jain <[email protected]>---
[...]
Quoted 18 lines
static inline bool
folio_is_device_coherent(const struct folio *folio){include/linux/memremap.h +0
-0return folio_is_zone_device(folio) &&lib/iov_iter.c +1
-0@@ -4,6 +4,7 @@#include <linux/fault-inject-usercopy.h> #include <linux/uio.h> #include <linux/pagemap.h> +#include <linux/memremap.h> #include <linux/highmem.h> #include <linux/slab.h> #include <linux/vmalloc.h> @@ -1968,7 +1969,7 @@ ssize_t iov_iter_extract_bvecs(struct iov_iter *iter, struct bio_vec *bv,unsigned int nr_to_add;
Quoted 6 lines
lib/iov_iter.c ssize_t iov_iter_extract_bvecs(struct iov_iter *iter, struct bio_vec *bv, · line 1973
Quoted 4 lines
lib/iov_iter.c +1
-1if (*nr_vecs >0 && - !zone_device_pages_have_same_pgmap (bv[*nr_vecs -1 ].bv_page, + !zone_device_pages_dma_compatible (bv[*nr_vecs -1 ].bv_page, pages[i]))Nit: ^ I'd just fit that into a single line to de-uglify.
No, avoid ureadably long lines. This is excactly as it should be.
David Hildenbrand (Arm)
maintainerQuoted 47 lines · Christoph Hellwig
Quoted 46 lines · David Hildenbrand (Arm)
Quoted 24 lines · Naman Jain
Commit
38446014648c("block: don't merge different kinds of P2P transfers in a single bio") madebio_add_page()reject pages from differentdev_pagemaps. This was needed for PCI P2PDMA, but also rejected pages with compatible DMA mapping requirements.Commit
41c665aae2b5("block: relax pgmap check inbio_add_pagefor compatible zone device pages") allows these pages in separate bvecs. Commit13920e4b7b784("block: add pgmap check tobiovec_phys_mergeable") prevents physically contiguous bvecs from different pgmaps from being merged into one segment.But
iov_iter_extract_bvecs()still rejects pages from different pgmaps without checking their DMA compatibility. Direct I/O uses this helper, so a non-atomic request submitted as a single bio transfers only the data before the pgmap boundary and returns fewer bytes than requested.Fix this by moving
zone_device_pages_compatible()fromblk.htomemremap.hand using it iniov_iter_extract_bvecs(). Rename the helper tozone_device_pages_dma_compatible()to make its purpose clear.Fixes 38446014648c ("block: don't merge different kinds of P2P transfers in a single bio")Signed-off-by Naman Jain <[email protected]>---
[...]
Quoted 18 lines
static inline bool
folio_is_device_coherent(const struct folio *folio){include/linux/memremap.h +0
-0return folio_is_zone_device(folio) &&lib/iov_iter.c +1
-0@@ -4,6 +4,7 @@#include <linux/fault-inject-usercopy.h> #include <linux/uio.h> #include <linux/pagemap.h> +#include <linux/memremap.h> #include <linux/highmem.h> #include <linux/slab.h> #include <linux/vmalloc.h> @@ -1968,7 +1969,7 @@ ssize_t iov_iter_extract_bvecs(struct iov_iter *iter, struct bio_vec *bv,unsigned int nr_to_add;
Quoted 8 lines
Quoted 6 lines
lib/iov_iter.c ssize_t iov_iter_extract_bvecs(struct iov_iter *iter, struct bio_vec *bv, · line 1973
Quoted 4 lines
lib/iov_iter.c +1
-1if (*nr_vecs >0 && - !zone_device_pages_have_same_pgmap (bv[*nr_vecs -1 ].bv_page, + !zone_device_pages_dma_compatible (bv[*nr_vecs -1 ].bv_page, pages[i]))Nit: ^ I'd just fit that into a single line to de-uglify.
No, avoid ureadably long lines. This is excactly as it should be.
lib/iov_iter.c, your rules ;)
Signature · 3 lines
Cheers, David
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] iov_iter: inaccurate Fixes trailer cites 38446014648c instead of 12da89e8844a --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] iov_iter: inaccurate Fixes trailer cites 38446014648c instead of 12da89e8844a
--
commit 41e8c0f2dd22c3f923973ea161eb22f302ba5525
Author: Naman Jain <[email protected]>
Quoted 18 lines
iov_iter: Allow DMA-compatible device pages in bvec extractionCommit
38446014648c("block: don't merge different kinds of P2P transfers in a single bio") madebio_add_page()reject pages from differentdev_pagemaps. This was needed for PCI P2PDMA, but also rejected pages with compatible DMA mapping requirements.Commit
41c665aae2b5("block: relax pgmap check inbio_add_pagefor compatible zone device pages") allows these pages in separate bvecs. Commit13920e4b7b784("block: add pgmap check tobiovec_phys_mergeable") prevents physically contiguous bvecs from different pgmaps from being merged into one segment.But
iov_iter_extract_bvecs()still rejects pages from different pgmaps without checking their DMA compatibility. Direct I/O uses this helper, so a non-atomic request submitted as a single bio transfers only the data before the pgmap boundary and returns fewer bytes than requested.
Quoted 7 lines
Fix this by moving
zone_device_pages_compatible()fromblk.htomemremap.hand using it iniov_iter_extract_bvecs(). Rename the helper tozone_device_pages_dma_compatible()to make its purpose clear.Fixes 38446014648c ("block: don't merge different kinds of P2P transfers in a single bio")Signed-off-by Naman Jain <[email protected]>
[Severity: Low]
Should the Fixes tag cite 12da89e8844a ("block: open code bio_add_page and fix handling of mismatching P2P ranges") instead?
Commit 38446014648c ("block: don't merge different kinds of P2P transfers in a single bio") only touched bio_add_page() and bio_integrity_add_page(), which 41c665aae2b5 ("block: relax pgmap check in bio_add_page for compatible zone device pages") already fixed.
The separate check in the page-extraction loop came from 12da89e8844a ("block: open code bio_add_page and fix handling of mismatching P2P ranges") and was moved to iov_iter_extract_bvecs() by 91b73c458182 ("iov_iter: extract a iov_iter_extract_bvecs helper from bio code").
With Cc: [email protected], the current Fixes tag targets older stable trees that do not have iov_iter_extract_bvecs() and already carry the bio_add_page() fix.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 44 lines · On 12/4/2026 2:13 PM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low]iov_iter: inaccurate Fixes trailer cites38446014648cinstead of12da89e8844a
--commit
41e8c0f2dd22c3f923973ea161eb22f302ba5525
Author: Naman Jain <[email protected]>Quoted 18 lines
iov_iter: Allow DMA-compatible device pages in bvec extractionCommit
38446014648c("block: don't merge different kinds of P2P transfers in a single bio") madebio_add_page()reject pages from differentdev_pagemaps. This was needed for PCI P2PDMA, but also rejected pages with compatible DMA mapping requirements.Commit
41c665aae2b5("block: relax pgmap check inbio_add_pagefor compatible zone device pages") allows these pages in separate bvecs. Commit13920e4b7b784("block: add pgmap check tobiovec_phys_mergeable") prevents physically contiguous bvecs from different pgmaps from being merged into one segment.But
iov_iter_extract_bvecs()still rejects pages from different pgmaps without checking their DMA compatibility. Direct I/O uses this helper, so a non-atomic request submitted as a single bio transfers only the data before the pgmap boundary and returns fewer bytes than requested.Quoted 7 lines
Fix this by moving
zone_device_pages_compatible()fromblk.htomemremap.hand using it iniov_iter_extract_bvecs(). Rename the helper tozone_device_pages_dma_compatible()to make its purpose clear.Fixes 38446014648c ("block: don't merge different kinds of P2P transfers in a single bio")Signed-off-by Naman Jain <[email protected]>[Severity: Low]
Should the Fixes tag cite12da89e8844a("block: open codebio_add_pageand fix handling of mismatching P2P ranges") instead?Commit
38446014648c("block: don't merge different kinds of P2P transfers in a single bio") only touchedbio_add_page()andbio_integrity_add_page(), which41c665aae2b5("block: relax pgmap check inbio_add_pagefor compatible zone device pages") already fixed.
Quoted 9 lines
The separate check in the page-extraction loop came from
12da89e8844a("block: open codebio_add_pageand fix handling of mismatching P2P ranges") and was moved toiov_iter_extract_bvecs()by91b73c458182("iov_iter: extract aiov_iter_extract_bvecshelper from bio code").With Cc: [email protected], the current Fixes tag targets older stable trees that do not have
iov_iter_extract_bvecs()and already carry thebio_add_page()fix.
I think this is correct, we should use the Fix tag suggested here:
Please correct this if possible, while picking the patch, or let me know if I should spin v3 for this change.
Regards,
Naman