From: Eliav Farber <farbere@amazon.com>
To: Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Zi Yan <ziy@nvidia.com>
Cc: Lorenzo Stoakes <ljs@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
Vlastimil Babka <vbabka@kernel.org>,
Mike Rapoport <rppt@kernel.org>,
"Suren Baghdasaryan" <surenb@google.com>,
Michal Hocko <mhocko@suse.com>, Jann Horn <jannh@google.com>,
Pedro Falcato <pfalcato@suse.de>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
Nico Pache <nico.pache@linux.dev>,
"Ryan Roberts" <ryan.roberts@arm.com>,
Dev Jain <dev.jain@arm.com>, Barry Song <baohua@kernel.org>,
Lance Yang <lance.yang@linux.dev>,
Usama Arif <usama.arif@linux.dev>,
Kiryl Shutsemau <kas@kernel.org>, <linux-mm@kvack.org>,
<linux-kernel@vger.kernel.org>, Eliav Farber <farbere@amazon.com>
Subject: [PATCH v3 2/5] mm/huge_memory: check the window and the pfn alignment before inserting
Date: Tue, 24 Nov 2026 13:15:07 +0000 [thread overview]
Message-ID: <20261124131510.38745-3-farbere@amazon.com> (raw)
In-Reply-To: <20261124131510.38745-1-farbere@amazon.com>
insert_pmd() and insert_pud() check that addr is inside the VMA, but not
that the whole PMD or PUD is. A VMA ending part-way through one gets
mapped past its end. Neither checks the pfn, and pfn_pmd() on x86 rounds
an unaligned pfn down, mapping the wrong memory.
No caller can reach either case today; each checks the geometry first.
The next patch adds vmf_insert_pfn_order(), whose callers do not.
Check both ends of the window, and the pfn alignment in
vmf_insert_pfn_pmd() and vmf_insert_pfn_pud(). Fall back rather than
SIGBUS: a VMA that does not span a whole PMD is ordinary.
Several callers open-code the same test; vfio-pci and nvgrace-gpu share
one as is_aligned_for_order() in a vfio-private header. The callers'
checks stay: some do more than mm can, as device-dax turns a window
failure into SIGBUS and the fsdax check also gates its PMD-order mapping
entry.
Signed-off-by: Eliav Farber <farbere@amazon.com>
---
New in v3. In v2 these checks sat in vmf_insert_pfn_order(), where only
my own callers got them.
mm/huge_memory.c | 18 ++++++++++++++----
1 file changed, 14 insertions(+), 4 deletions(-)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 75e8641bf9ff..909db70aeba2 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1630,8 +1630,9 @@ static vm_fault_t insert_pmd(struct vm_area_struct *vma, unsigned long addr,
spinlock_t *ptl;
pmd_t entry;
- if (addr < vma->vm_start || addr >= vma->vm_end)
- return VM_FAULT_SIGBUS;
+ /* If the whole PMD doesn't fit in the VMA, fall back to 4KiB pages */
+ if (addr < vma->vm_start || addr + HPAGE_PMD_SIZE > vma->vm_end)
+ return VM_FAULT_FALLBACK;
if (arch_needs_pgtable_deposit()) {
pgtable = pte_alloc_one(vma->vm_mm);
@@ -1729,6 +1730,10 @@ vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, unsigned long pfn,
if (unlikely(is_huge_zero_pfn(pfn)))
return VM_FAULT_SIGBUS;
+ /* pfn_pmd() would round an unaligned pfn down and map the wrong memory */
+ if (!IS_ALIGNED(pfn, HPAGE_PMD_NR))
+ return VM_FAULT_FALLBACK;
+
pfnmap_setup_cachemode_pfn(pfn, &pgprot);
return insert_pmd(vma, addr, vmf->pmd, fop, pgprot, write);
@@ -1768,8 +1773,9 @@ static vm_fault_t insert_pud(struct vm_area_struct *vma, unsigned long addr,
spinlock_t *ptl;
pud_t entry;
- if (addr < vma->vm_start || addr >= vma->vm_end)
- return VM_FAULT_SIGBUS;
+ /* If the whole PUD doesn't fit in the VMA, fall back to smaller pages */
+ if (addr < vma->vm_start || addr + HPAGE_PUD_SIZE > vma->vm_end)
+ return VM_FAULT_FALLBACK;
ptl = pud_lock(mm, pud);
if (!pud_none(*pud)) {
@@ -1842,6 +1848,10 @@ vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, unsigned long pfn,
(VM_PFNMAP|VM_MIXEDMAP));
BUG_ON((vma->vm_flags & VM_PFNMAP) && vma_is_cow_mapping(vma));
+ /* pfn_pud() would round an unaligned pfn down and map the wrong memory */
+ if (!IS_ALIGNED(pfn, HPAGE_PUD_NR))
+ return VM_FAULT_FALLBACK;
+
pfnmap_setup_cachemode_pfn(pfn, &pgprot);
return insert_pud(vma, addr, vmf->pud, fop, pgprot, write);
--
2.47.3
next prev parent reply other threads:[~2026-11-24 13:15 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 13:15 [PATCH v3 0/5] mm, uio: huge PFN mappings for /dev/mem and UIO Eliav Farber
2026-11-24 13:15 ` [PATCH v3 1/5] mm: add MMAP_TRACK_PFN action and track_pfn_range() for lazy PFN maps Eliav Farber
2026-11-24 13:15 ` Eliav Farber [this message]
2026-11-24 13:15 ` [PATCH v3 3/5] mm: add vmf_insert_pfn_order() Eliav Farber
2026-11-24 13:15 ` [PATCH v3 4/5] mm/char-mem: implement huge_fault support for /dev/mem Eliav Farber
2026-11-25 13:16 ` sashiko-bot
2026-11-24 13:15 ` [PATCH v3 5/5] uio: implement huge_fault support for physical mappings Eliav Farber
2026-11-24 14:12 ` Greg Kroah-Hartman
2026-11-24 13:28 ` [PATCH v3 0/5] mm, uio: huge PFN mappings for /dev/mem and UIO Lorenzo Stoakes (ARM)
2026-11-24 18:15 ` Farber, Eliav
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=20261124131510.38745-3-farbere@amazon.com \
--to=farbere@amazon.com \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=gregkh@linuxfoundation.org \
--cc=jannh@google.com \
--cc=kas@kernel.org \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mhocko@suse.com \
--cc=nico.pache@linux.dev \
--cc=pfalcato@suse.de \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=surenb@google.com \
--cc=usama.arif@linux.dev \
--cc=vbabka@kernel.org \
--cc=ziy@nvidia.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.