From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 753B4606A32C for ; Thu, 3 Dec 2026 08:43:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.a=rsa-sha256 header.s=default header.b=V/EVB6Qg Received: by kanga.kvack.org (Postfix) id 8FEA56B05B5; Thu, 3 Dec 2026 03:43:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8AFFE6B05B6; Thu, 3 Dec 2026 03:43:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C5236B05B7; Thu, 3 Dec 2026 03:43:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 49AA66B05B5 for ; Thu, 3 Dec 2026 03:43:27 -0500 (EST) Received: from smtpin09.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay05.hostedemail.com (Postfix) with ESMTP id DE03040589 for ; Thu, 3 Dec 2026 08:43:26 +0000 (UTC) X-FDA: 85502032812.09.F00B46D Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf25.hostedemail.com (Postfix) with ESMTP id 4AE1FA000A for ; Thu, 3 Dec 2026 08:43:25 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b="V/EVB6Qg"; dmarc=pass (policy=none) header.from=linux.microsoft.com; spf=pass (imf25.hostedemail.com: domain of namjain@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=namjain@linux.microsoft.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796287405; b=406+SQtmcs/pWtAGvzPj6yu7vYi1gq746DT5ZqTpv1UGvPxXkkelEOZhJwBf0DrrAJk0uo aOeZCzXfOGlQDw3gWtrzCE+rB3bUBkJKl801/DMkhuPhfTnqgII+KznntQLBGXZhp/UhhT +oJPTcutyZ+iJ0ZKSu7U8UyXNmRbeII= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796287405; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=uZeilcIOLboWzDwcV1z7wQ6qKwBy+oAnS1ah9YIRbQs=; b=kxo2rYinL+PemxSevLIS5ucT0MPZHEH5gS2vt8oAMtKkAYs7BsXkQzvquOzdPQdvN74UPG Gy3CZ96rJAex0aFbUNUUq6c5IjvI21mhGYAfLkbdcLOjJPSd90o7NFGjSl3aeaOWgWirPO +u9JHFdOt2ZpHMA2xMIuxVB/1k0Y2UI= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b="V/EVB6Qg"; dmarc=pass (policy=none) header.from=linux.microsoft.com; spf=pass (imf25.hostedemail.com: domain of namjain@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=namjain@linux.microsoft.com Received: from CPC-namja-026ON.localdomain (unknown [4.213.232.22]) by linux.microsoft.com (Postfix) with ESMTPSA id 6EB0120B7166; Thu, 3 Dec 2026 00:43:18 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 6EB0120B7166 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1796287402; bh=uZeilcIOLboWzDwcV1z7wQ6qKwBy+oAnS1ah9YIRbQs=; h=From:To:Cc:Subject:Date:From; b=V/EVB6QgMs3OmxurG9h2C1nKQVyavIQRarby3Yk12i44BeiFfpQZeRaDyDMCnVybM xz8sbiUWIUSBbXPbC9p1VCNYo2SzpR4O/90/2vCmnI8gsm1cRW7h3atxkX/0ax93zT 76i94zNe8hu3mCCUrT1lHlMpKmduKosH6Gg1tHKw= From: Naman Jain To: hch@lst.de, david@kernel.org, axboe@kernel.dk, akpm@linux-foundation.org, viro@zeniv.linux.org.uk Cc: osalvador@suse.de, logang@deltatee.com, leon@kernel.org, kbusch@kernel.org, ssengar@linux.microsoft.com, linux-block@vger.kernel.org, linux-mm@kvack.org, linux-cxl@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, stable@vger.kernel.org, namjain@linux.microsoft.com Subject: [PATCH v2] iov_iter: Allow DMA-compatible device pages in bvec extraction Date: Thu, 3 Dec 2026 08:43:15 +0000 Message-ID: <20261203084315.3776182-1-namjain@linux.microsoft.com> X-Mailer: git-send-email 2.43.0 X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Rspamd-Queue-Id: 4AE1FA000A X-Rspam-User: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Rspamd-Server: rspam03 X-Stat-Signature: jxwruqqa4od6ns8npuuow3gnpqw1tmpe X-HE-Tag: 1796287405-105302 X-HE-Meta: U2FsdGVkX1/MFOi7+iQYYiEo3eunmyUWxJGhySgMEZ2LiF028RSlfbMAvZo222i/e4acyEEEaLAYZ2IA0xPsfnefk+HfL7hnE1NNRwltoml8ucy3EcTjgTjn4dHEO4ndw6bh1iWcUnyRsO3ncKXnncmF/4Wz2hRuYdGDUUNW5kP8+PkWdp/S/Sn012BfAE4C4T5N/cUT5otQ1SMxza+SG5+t9YltTEWx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0U6ABlNe/4QqC1jyX9RZUeZomP2/JD10T6hE6qNR+qIBx1ae9sCEIIrWu0pY/rwjHvq6s8ymXt16E78fMmPVLQ7aYDGV8cIF/H25Sb8hWUGZcXnlNX1n0OTeilGq+ZtclFnqcFd0jcsEpyPzAfPom6sTnwDPd0nvRwod6WrapoTZbJTTltkIA7Yj8r3wa/dWjg Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: 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. Fixes: 38446014648c ("block: don't merge different kinds of P2P transfers in a single bio") Cc: stable@vger.kernel.org Signed-off-by: Naman Jain --- Changes since v1: https://lore.kernel.org/all/20261126053433.88531-1-namjain@linux.microsoft.com/ 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(-) diff --git a/block/bio-integrity.c b/block/bio-integrity.c index b23e2434d80c2..031967e623fc9 100644 --- a/block/bio-integrity.c +++ b/block/bio-integrity.c @@ -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)) { diff --git a/block/bio.c b/block/bio.c index f95b63c0604af..dcc83de3ab701 100644 --- a/block/bio.c +++ b/block/bio.c @@ -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)) { diff --git a/block/blk.h b/block/blk.h index 50abfd9328861..460c064f1d1b0 100644 --- a/block/blk.h +++ b/block/blk.h @@ -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) { diff --git a/include/linux/memremap.h b/include/linux/memremap.h index c8a4979d4c780..ff2f7868081ae 100644 --- a/include/linux/memremap.h +++ b/include/linux/memremap.h @@ -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) && diff --git a/lib/iov_iter.c b/lib/iov_iter.c index 56c59a2b589d3..32b57f5747782 100644 --- a/lib/iov_iter.c +++ b/lib/iov_iter.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -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; -- 2.43.0