Skip to content

Commit a47e9cc

Browse files
Naman Jainkawasaki
authored andcommitted
block: relax pgmap check in bio_add_page for compatible zone device pages
bio_add_page() and bio_integrity_add_page() reject pages from different dev_pagemaps entirely, returning 0 even when those pages have compatible DMA mapping requirements. This forces callers to start a new bio when buffers span pgmap boundaries, even though the pages could safely coexist as separate bvec entries. This matters for guests where memory is registered through devm_memremap_pages() with MEMORY_DEVICE_GENERIC in multiple calls, creating separate dev_pagemaps for each chunk. When a direct I/O buffer spans two such chunks, bio_add_page() rejects the second page, forcing an unnecessary bio split or I/O failure. Introduce zone_device_pages_compatible() in blk.h to check whether two pages can coexist in the same bio as separate bvec entries. The block DMA iterator (blk_dma_map_iter_start) caches the P2PDMA mapping state from the first segment and applies it to all others, so P2PDMA pages from different pgmaps must not be mixed, and neither must P2PDMA and non-P2PDMA pages. All other combinations (MEMORY_DEVICE_GENERIC pages from different pgmaps, or MEMORY_DEVICE_GENERIC with normal RAM) use the same dma_map_phys path and are safe. Replace the blanket zone_device_pages_have_same_pgmap() rejection with zone_device_pages_compatible(), while keeping zone_device_pages_have_same_pgmap() as a merge guard. Pages from different pgmaps can be added as separate bvec entries but must not be coalesced into the same segment, as that would make it impossible to recover the correct pgmap via page_pgmap(). Fixes: 49580e6 ("block: add check when merging zone device pages") Cc: stable@vger.kernel.org Signed-off-by: Naman Jain <namjain@linux.microsoft.com>
1 parent 283f3cf commit a47e9cc

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

block/bio-integrity.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ int bio_integrity_add_page(struct bio *bio, struct page *page,
167167
if (bip->bip_vcnt > 0) {
168168
struct bio_vec *bv = &bip->bip_vec[bip->bip_vcnt - 1];
169169

170-
if (!zone_device_pages_have_same_pgmap(bv->bv_page, page))
170+
if (!zone_device_pages_compatible(bv->bv_page, page))
171171
return 0;
172-
173-
if (bvec_try_merge_hw_page(q, bv, page, len, offset)) {
172+
if (zone_device_pages_have_same_pgmap(bv->bv_page, page) &&
173+
bvec_try_merge_hw_page(q, bv, page, len, offset)) {
174174
bip->bip_iter.bi_size += len;
175175
return len;
176176
}

block/bio.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,10 +1070,10 @@ int bio_add_page(struct bio *bio, struct page *page,
10701070
if (bio->bi_vcnt > 0) {
10711071
struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1];
10721072

1073-
if (!zone_device_pages_have_same_pgmap(bv->bv_page, page))
1073+
if (!zone_device_pages_compatible(bv->bv_page, page))
10741074
return 0;
1075-
1076-
if (bvec_try_merge_page(bv, page, len, offset)) {
1075+
if (zone_device_pages_have_same_pgmap(bv->bv_page, page) &&
1076+
bvec_try_merge_page(bv, page, len, offset)) {
10771077
bio->bi_iter.bi_size += len;
10781078
return len;
10791079
}

block/blk.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,25 @@ static inline bool biovec_phys_mergeable(struct request_queue *q,
141141
return true;
142142
}
143143

144+
/*
145+
* Check if two pages from potentially different zone device pgmaps can
146+
* coexist as separate bvec entries in the same bio.
147+
*
148+
* The block DMA iterator (blk_dma_map_iter_start) caches the P2PDMA mapping
149+
* state from the first segment and applies it to all subsequent segments, so
150+
* P2PDMA pages from different pgmaps must not be mixed in the same bio.
151+
*
152+
* Other zone device types (FS_DAX, GENERIC) use the same dma_map_phys() path
153+
* as normal RAM. PRIVATE and COHERENT pages never appear in bios.
154+
*/
155+
static inline bool zone_device_pages_compatible(const struct page *a,
156+
const struct page *b)
157+
{
158+
if (is_pci_p2pdma_page(a) || is_pci_p2pdma_page(b))
159+
return zone_device_pages_have_same_pgmap(a, b);
160+
return true;
161+
}
162+
144163
static inline bool __bvec_gap_to_prev(const struct queue_limits *lim,
145164
struct bio_vec *bprv, unsigned int offset)
146165
{

0 commit comments

Comments
 (0)