Re: [PATCH v3 04/19] hugetlb_vmemmap: Use try_update_vmemmap_pte to update in-use PTEs

From
James Houghton <hidden>
Date
2026-11-23 20:49:38
Also in
linux-mm, lkml
Reply to
Muchun Song, 2026-11-23
On Sun, Nov 22, 2026 at 9:47 PM Muchun Song [off-list ref] wrote:

quoted
On Nov 23, 2026, at 02:44, James Houghton [off-list ref] wrote:

set_pte_at() cannot be used to replace in-use vmemmap PTEs on arm64, so
replace it with a more specific routine, try_update_vmemmap_pte().

try_update_vmemmap_pte() is used for modifying page table entries such
that there is a guarantee that no fault will be taken.

For the generic implementation, please note one difference: set_pte()
does not invoke page_table_check_ptes_set(), but set_pte_at(), the call
we are about to replace, does. However, this is a functional no-op
because page_table_check_ptes_set() does nothing for init_mm PTEs, which
vmemmap PTEs are.

Signed-off-by: James Houghton <redacted>
---
arch/loongarch/include/asm/pgtable.h |  2 ++
arch/riscv/include/asm/pgtable.h     |  2 ++
arch/x86/include/asm/pgtable.h       |  2 ++
include/linux/pgtable.h              | 22 +++++++++++++++++++
mm/hugetlb_vmemmap.c                 | 32 +++++++++++++++++++++++-----
5 files changed, 55 insertions(+), 5 deletions(-)
diff --git a/arch/loongarch/include/asm/pgtable.h b/arch/loongarch/include/asm/pgtable.h
index f87603135131..123eda3f6035 100644
--- a/arch/loongarch/include/asm/pgtable.h
+++ b/arch/loongarch/include/asm/pgtable.h
@@ -633,6 +633,8 @@ static inline long pmd_protnone(pmd_t pmd)
#define pmd_leaf(pmd)         ((pmd_val(pmd) & _PAGE_HUGE) != 0)
#define pud_leaf(pud)         ((pud_val(pud) & _PAGE_HUGE) != 0)

+#define ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PTE
+
/*
 * We provide our own get_unmapped area to cope with the virtual aliasing
 * constraints placed on us by the cache architecture.
diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h
index 2ebd5fa5adc5..c8d05bc12cc8 100644
--- a/arch/riscv/include/asm/pgtable.h
+++ b/arch/riscv/include/asm/pgtable.h
@@ -1172,6 +1172,8 @@ static inline pud_t pud_modify(pud_t pud, pgprot_t newprot)
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */

+#define ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PTE
+
/*
 * Encode/decode swap entries and swap PTEs. Swap PTEs are all PTEs that
 * are !pte_none() && !pte_present().
diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
index 000fc6c1ca37..271cdc61de3a 100644
--- a/arch/x86/include/asm/pgtable.h
+++ b/arch/x86/include/asm/pgtable.h
@@ -1372,6 +1372,8 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
}
#endif

+#define ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PTE
+
#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
static inline pud_t pudp_establish(struct vm_area_struct *vma,
              unsigned long address, pud_t *pudp, pud_t pud)
diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
index bc1353f03b08..ab33df268955 100644
--- a/include/linux/pgtable.h
+++ b/include/linux/pgtable.h
@@ -458,6 +458,28 @@ static inline void set_ptes(struct mm_struct *mm, unsigned long addr,
#endif
#define set_pte_at(mm, addr, ptep, pte) set_ptes(mm, addr, ptep, pte, 1)

+#ifdef ARCH_WANTS_GENERIC_POPULATE_VMEMMAP_PTE
Why not ARCH_WANTS_VMEMMAP_UPDATE_PTE? Then x86, loogarch and RISC-V do not need define
this macro in their ptable.h.
You're basically asking to make this opt-out instead of opt-in, right?

I like making the different architectures explicitly opt in to the
generic implementation; it forces any new architecture to decide if
the generic implementation is correct for them. But I don't feel
strongly.

I'll switch it to opt-out in v4 unless you or someone else tells me otherwise.
quoted
+/*
+ * try_update_vmemmap_pte - Remap PTEs used by the vmemmap.
+ * @addr: Base address of the remapped PTE.
+ * @ptep: Page table pointer to be overwritten.
+ * @pte: Page table entry to write.
+ *
+ * This function is only to be used to update PTEs that map the vmemmap. The
+ * only valid transitions supported by this function are: leaf-level
+ * (PAGE_SIZE), valid-to-valid. The pfn and prot bits may be changed.
+ *
+ * Implementations of this function must ensure that, while the update is taking
+ * place, CPUs will not fault on the remapped virtual address.
+ */
+static inline int try_update_vmemmap_pte(unsigned long addr, pte_t *ptep,
+                                      pte_t pte)
Please rename it to vmemmap_update_pte, all HVO-related helpers will be prefixed
with vmemmap.
Okay, will do.
Again, I still recommend having this function return void. That will make the
overall code simpler. If we actually run into related issues in the future, we
can add support for it then. I don't want to introduce unnecessary code
maintenance now.
Thanks. I will just point back to my reply from v2[1]. I hope Catalin
or Will have thoughts on this.

[1] https://lore.kernel.org/linux-mm/CADrL8HWNaurYHGnpvC9qwr6bVU74YhhucCuhoVYcvqmSX+R6+Q@mail.gmail.com/ (local)
Thanks,
Muchun
quoted
+{
+     set_pte(ptep, pte);
+     return 0;
+}
+#endif
+
#ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
extern int ptep_set_access_flags(struct vm_area_struct *vma,
                               unsigned long address, pte_t *ptep,
diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
index 2e9cf8d39c89..b4d0311bcc4a 100644
--- a/mm/hugetlb_vmemmap.c
+++ b/mm/hugetlb_vmemmap.c
@@ -218,15 +218,19 @@ static int alloc_vmemmap_page_list(struct list_head *head,
      return -ENOMEM;
}

+/*
+ * Copy @src into the first page on @vmemmap_pages and return it. The page is
+ * left on the list; the caller must remove it only once it has successfully
+ * been installed in the vmemmap.
+ */
static struct page *vmemmap_dup_page_from_list(const struct page *src,
              struct list_head *vmemmap_pages)
{
      struct page *dst = list_first_entry(vmemmap_pages, struct page, lru);

-     list_del(&dst->lru);
      copy_page(page_address(dst), page_address(src));

-     /* Ensure updates to @dst page are visible before set_pte_at(). */
+     /* Ensure updates to @dst page are visible before try_update_vmemmap_pte(). */
      smp_wmb();

      return dst;
@@ -248,6 +252,8 @@ static int vmemmap_optimize_pte(pte_t *pte, unsigned long addr,
      const struct page *page = (const struct page *)addr;
      const struct folio *folio = page_folio(page);
      struct page *src = pte_page(ptep_get(pte)), *dst;
+     bool dup = false;
+     int ret;

      if (likely(vmemmap_optimizable_pfn(page_to_pfn(page), folio_order(folio)))) {
              dst = folio_vmemmap_shared_tail_page(folio);
@@ -255,16 +261,27 @@ static int vmemmap_optimize_pte(pte_t *pte, unsigned long addr,
                      return -ENOMEM;
      } else {
              dst = vmemmap_dup_page_from_list(src, walk->vmemmap_pages);
+             dup = true;
      }

-     list_add(&src->lru, walk->vmemmap_pages);
      /*
       * Map the shared tail page writable intentionally. A lockless get that
       * read a non-zero refcount before the remap can still do its cmpxchg
       * after it. The refcount is zero, so the cmpxchg fails and leaves the
       * page unchanged.
       */
-     set_pte_at(&init_mm, addr, pte, mk_pte(dst, PAGE_KERNEL));
+     ret = try_update_vmemmap_pte(addr, pte, mk_pte(dst, PAGE_KERNEL));
+     if (ret)
+             return ret;
+
+     /*
+      * We successfully overwrote the vmemmap PTE, so the page that was just
+      * unmapped can be freed, and if we mapped a duplicated page, remove it
+      * from the list so that it doesn't get freed later.
+      */
+     if (dup)
+             list_del(&dst->lru);
+     list_add(&src->lru, walk->vmemmap_pages);

      return 0;
}
@@ -275,6 +292,7 @@ static int vmemmap_restore_pte(pte_t *pte, unsigned long addr,
      const struct page *page = (const struct page *)addr;
      const struct folio *folio = page_folio(page);
      struct page *src = pte_page(ptep_get(pte)), *dst, *tail;
+     int ret;

      if (unlikely(!vmemmap_optimizable_pfn(page_to_pfn(page), folio_order(folio))))
              return 0;
@@ -291,7 +309,11 @@ static int vmemmap_restore_pte(pte_t *pte, unsigned long addr,
              return 0;

      dst = vmemmap_dup_page_from_list(src, walk->vmemmap_pages);
-     set_pte_at(&init_mm, addr, pte, mk_pte(dst, PAGE_KERNEL));
+     ret = try_update_vmemmap_pte(addr, pte, mk_pte(dst, PAGE_KERNEL));
+     if (ret)
+             return ret;
+
+     list_del(&dst->lru);

      return 0;
}
--
2.56.1.809.ga0b61035b5-goog
  
Thread (43 messages) 43 messages, 2 authors, last reply 4d ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help