[PATCH v2 1/4] mm: update walk_to_pmd() to explicitly return an error
From: Lorenzo Stoakes (ARM)
Date: Tue Nov 17 2026 - 13:32:32 EST
Rather than callers inferring an error from a NULL pmd return, explicitly
return error codes.
This allows different error codes to be returned for different failure
conditions.
No functional change intended.
This is a prerequisite for the fix to insert_pfn() which follows, as that
needs to be able to tell a failure to allocate a page table apart from a
race with a huge fault, so mark it for stable alongside that fix.
Fixes: f9e54c3a2f5b ("vfio/pci: implement huge_fault support")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
---
mm/memory.c | 23 +++++++++++------------
1 file changed, 11 insertions(+), 12 deletions(-)
diff --git a/mm/memory.c b/mm/memory.c
index 3a20d64fd276..67ee25215ca5 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2366,7 +2366,7 @@ void zap_special_vma_range(struct vm_area_struct *vma, unsigned long address,
}
EXPORT_SYMBOL_GPL(zap_special_vma_range);
-static pmd_t *walk_to_pmd(struct mm_struct *mm, unsigned long addr)
+static int walk_to_pmd(struct mm_struct *mm, unsigned long addr, pmd_t **pmdp)
{
pgd_t *pgd;
p4d_t *p4d;
@@ -2376,24 +2376,25 @@ static pmd_t *walk_to_pmd(struct mm_struct *mm, unsigned long addr)
pgd = pgd_offset(mm, addr);
p4d = p4d_alloc(mm, pgd, addr);
if (!p4d)
- return NULL;
+ return -ENOMEM;
pud = pud_alloc(mm, p4d, addr);
if (!pud)
- return NULL;
+ return -ENOMEM;
pmd = pmd_alloc(mm, pud, addr);
if (!pmd)
- return NULL;
+ return -ENOMEM;
VM_BUG_ON(pmd_trans_huge(*pmd));
- return pmd;
+ *pmdp = pmd;
+ return 0;
}
pte_t *get_locked_pte(struct mm_struct *mm, unsigned long addr,
spinlock_t **ptl)
{
- pmd_t *pmd = walk_to_pmd(mm, addr);
+ pmd_t *pmd;
- if (!pmd)
+ if (walk_to_pmd(mm, addr, &pmd))
return NULL;
return pte_alloc_map_lock(mm, pmd, addr, ptl);
}
@@ -2532,13 +2533,11 @@ static int insert_pages(struct vm_area_struct *vma, unsigned long addr,
unsigned long curr_page_idx = 0;
unsigned long remaining_pages_total = *num;
unsigned long pages_to_write_in_pmd;
- int err = 0;
+ int err;
more:
- pmd = walk_to_pmd(mm, addr);
- if (!pmd) {
- err = -ENOMEM;
+ err = walk_to_pmd(mm, addr, &pmd);
+ if (err)
goto out;
- }
pages_to_write_in_pmd = min_t(unsigned long,
remaining_pages_total, PTRS_PER_PTE - pte_index(addr));
--
2.56.0