From: Ashish Mhetre <amhetre@nvidia.com>
To: <robin.murphy@arm.com>, <joro@8bytes.org>, <will@kernel.org>,
<iommu@lists.linux.dev>
Cc: <linux-kernel@vger.kernel.org>, <linux-tegra@vger.kernel.org>,
"Ashish Mhetre" <amhetre@nvidia.com>
Subject: [PATCH RFC] iommu/dma: Validate page before accessing P2PDMA state
Date: Tue, 24 Feb 2026 10:42:57 +0000 [thread overview]
Message-ID: <20260224104257.1641429-1-amhetre@nvidia.com> (raw)
When mapping scatter-gather entries that reference reserved
memory regions without struct page backing (e.g., bootloader created
carveouts), is_pci_p2pdma_page() dereferences the page pointer
returned by sg_page() without first verifying its validity.
This causes a kernel paging fault when CONFIG_PCI_P2PDMA is enabled
and dma_map_sg_attrs() is called for memory regions that have no
associated struct page:
Unable to handle kernel paging request at virtual address fffffc007d100000
...
Call trace:
iommu_dma_map_sg+0x118/0x414
dma_map_sg_attrs+0x38/0x44
Fix this by adding a pfn_valid() check before calling
is_pci_p2pdma_page(). If the page frame number is invalid, skip the
P2PDMA check entirely as such memory cannot be P2PDMA memory anyway.
Signed-off-by: Ashish Mhetre <amhetre@nvidia.com>
---
drivers/iommu/dma-iommu.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c
index 5dac64be61bb..5f45f33b23c2 100644
--- a/drivers/iommu/dma-iommu.c
+++ b/drivers/iommu/dma-iommu.c
@@ -1423,6 +1423,9 @@ int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
size_t s_length = s->length;
size_t pad_len = (mask - iova_len + 1) & mask;
+ if (!pfn_valid(page_to_pfn(sg_page(s))))
+ goto post_pci_p2pdma;
+
switch (pci_p2pdma_state(&p2pdma_state, dev, sg_page(s))) {
case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE:
/*
@@ -1449,6 +1452,7 @@ int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
goto out_restore_sg;
}
+post_pci_p2pdma:
sg_dma_address(s) = s_iova_off;
sg_dma_len(s) = s_length;
s->offset -= s_iova_off;
--
2.25.1
next reply other threads:[~2026-02-24 10:43 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-24 10:42 Ashish Mhetre [this message]
2026-02-24 12:32 ` [PATCH RFC] iommu/dma: Validate page before accessing P2PDMA state Leon Romanovsky
2026-02-24 20:57 ` Pranjal Shrivastava
2026-02-25 4:49 ` Ashish Mhetre
2026-02-25 7:56 ` Leon Romanovsky
2026-02-25 20:11 ` Pranjal Shrivastava
2026-02-26 7:58 ` Leon Romanovsky
2026-02-27 5:46 ` Ashish Mhetre
2026-02-27 14:05 ` Robin Murphy
2026-02-27 14:08 ` Pranjal Shrivastava
2026-02-27 14:13 ` Jason Gunthorpe
2026-02-25 7:50 ` Leon Romanovsky
2026-02-25 20:15 ` Pranjal Shrivastava
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260224104257.1641429-1-amhetre@nvidia.com \
--to=amhetre@nvidia.com \
--cc=iommu@lists.linux.dev \
--cc=joro@8bytes.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-tegra@vger.kernel.org \
--cc=robin.murphy@arm.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox