Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Muchun Song <muchun.song@linux.dev>
To: James Houghton <jthoughton@google.com>
Cc: Muchun Song <muchun.song@linux.dev>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>, Oscar Salvador <osalvador@suse.de>,
	Andrew Morton <akpm@linux-foundation.org>,
	Kefeng Wang <wangkefeng.wang@huawei.com>,
	Nikos Nikoleris <nikos.nikoleris@arm.com>,
	Linu Cherian <linu.cherian@arm.com>,
	Mark Rutland <mark.rutland@arm.com>,
	David Hildenbrand <david@kernel.org>,
	Ryan Roberts <ryan.roberts@arm.com>,
	Nanyong Sun <sunnanyong@huawei.com>, Yu Zhao <yuzhao@google.com>,
	Frank van der Linden <fvdl@google.com>,
	David Rientjes <rientjes@google.com>,
	linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org
Subject: Re: [PATCH v3 04/19] hugetlb_vmemmap: Use try_update_vmemmap_pte to update in-use PTEs
Date: Tue, 24 Nov 2026 10:29:22 +0800	[thread overview]
Message-ID: <4A195604-B83B-4608-B32E-5F6CC85AE96F@linux.dev> (raw)
In-Reply-To: <CADrL8HUstWR2pajfYYzRqL7HziVfzZAXJas6MqJkK6pVxSzXEg@mail.gmail.com>



> On Nov 24, 2026, at 04:48, James Houghton <jthoughton@google.com> wrote:
> 
> On Sun, Nov 22, 2026 at 9:47 PM Muchun Song <muchun.song@linux.dev> wrote:
>> 
>> 
>> 
>>> On Nov 23, 2026, at 02:44, James Houghton <jthoughton@google.com> 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 <jthoughton@google.com>
>>> ---
>>> 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?

Yes, exactly.

> 
> 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.

We already have an opt-in config: ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP.
It already forces new architectures to pay attention to it.

Thanks,
Muchun

> 
> I'll switch it to opt-out in v4 unless you or someone else tells me otherwise.
> 
>> 
>>> +/*
>>> + * 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/
> 
>> 
>> Thanks,
>> Muchun
>> 
>>> +{
>>> +     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




  reply	other threads:[~2026-11-24  2:29 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22 18:44 [PATCH v3 00/19] Another attempt at HVO support on arm64 James Houghton
2026-11-22 18:44 ` [PATCH v3 01/19] arm64/pgtable: Clear AF with LDCLR on supported systems James Houghton
2026-11-22 18:44 ` [PATCH v3 02/19] hugetlb_vmemmap: Always flush TLB if needed upon PTE remapping James Houghton
2026-11-23  4:08   ` sashiko-bot
2026-11-22 18:44 ` [PATCH v3 03/19] hugetlb_vmemmap: Leave pages partially HVOed upon restore failure James Houghton
2026-11-22 18:44 ` [PATCH v3 04/19] hugetlb_vmemmap: Use try_update_vmemmap_pte to update in-use PTEs James Houghton
2026-11-23  2:47   ` Muchun Song
2026-11-23 20:48     ` James Houghton
2026-11-24  2:29       ` Muchun Song [this message]
2026-11-23  4:08   ` sashiko-bot
2026-11-22 18:44 ` [PATCH v3 05/19] hugetlb_vmemmap: Allow architectures to dynamically disallow HVO James Houghton
2026-11-23  3:01   ` Muchun Song
2026-11-23 20:31     ` James Houghton
2026-11-24  2:31       ` Muchun Song
2026-11-22 18:44 ` [PATCH v3 06/19] hugetlb: Optimize bootmem folios that were not pre-HVOed James Houghton
2026-11-23  3:10   ` Muchun Song
2026-11-23  4:08   ` sashiko-bot
2026-11-22 18:44 ` [PATCH v3 07/19] hugetlb_vmemmap: Allow architectures to make HVO enablement boot-time only James Houghton
2026-11-23  3:13   ` Muchun Song
2026-11-22 18:44 ` [PATCH v3 08/19] hugetlb_vmemmap: Expose whether HVO is enabled to architecture code James Houghton
2026-11-23  8:50   ` Muchun Song
2026-11-22 18:44 ` [PATCH v3 09/19] arm64: Add bbm_through_af capability James Houghton
2026-11-23  4:08   ` sashiko-bot
2026-11-23 19:22     ` James Houghton
2026-11-22 18:44 ` [PATCH v3 10/19] arm64: Implement try_update_vmemmap_pte using the AF trick James Houghton
2026-11-22 18:44 ` [PATCH v3 11/19] arm64/mm: Align vmemmap to maximal folio size James Houghton
2026-11-23  3:22   ` Muchun Song
2026-11-22 18:44 ` [PATCH v3 12/19] arm64: Support hugetlb vmemmap optimization James Houghton
2026-11-23  3:26   ` Muchun Song
2026-11-23  4:08   ` sashiko-bot
2026-11-22 18:44 ` [PATCH v3 13/19] hugetlb_vmemmap: Add fault injection for in-place vmemmap PTE updates James Houghton
2026-11-22 18:44 ` [PATCH v3 14/19] selftests/mm: Add HugeTLB vmemmap optimization stress test James Houghton
2026-11-23  4:08   ` sashiko-bot
2026-11-22 18:44 ` [PATCH v3 15/19 DO-NOT-MERGE] hugetlb_vmemmap: Use try_populate_vmemmap_pmd for replacing in-use PMDs James Houghton
2026-11-22 18:44 ` [PATCH v3 16/19 DO-NOT-MERGE] arm64: Implement try_populate_vmemmap_pmd using AF trick James Houghton
2026-11-23  4:08   ` sashiko-bot
2026-11-22 18:44 ` [PATCH v3 17/19 DO-NOT-MERGE] arm64: Drop BBML3 requirement for HVO James Houghton
2026-11-22 18:44 ` [PATCH v3 18/19 DO-NOT-MERGE] hugetlb_vmemmap: Add fault injection for in-place vmemmap PMD splits James Houghton
2026-11-22 18:44 ` [PATCH v3 19/19 DO-NOT-MERGE] selftests/mm: Add HVO pmd-split fault injection tests James Houghton

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=4A195604-B83B-4608-B32E-5F6CC85AE96F@linux.dev \
    --to=muchun.song@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=catalin.marinas@arm.com \
    --cc=david@kernel.org \
    --cc=fvdl@google.com \
    --cc=jthoughton@google.com \
    --cc=linu.cherian@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mark.rutland@arm.com \
    --cc=nikos.nikoleris@arm.com \
    --cc=osalvador@suse.de \
    --cc=rientjes@google.com \
    --cc=ryan.roberts@arm.com \
    --cc=sunnanyong@huawei.com \
    --cc=wangkefeng.wang@huawei.com \
    --cc=will@kernel.org \
    --cc=yuzhao@google.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox