From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Dev Jain <dev.jain@arm.com>,
ljs@kernel.org, akpm@linux-foundation.org, hughd@google.com,
chrisl@kernel.org, kasong@tencent.com
Cc: riel@surriel.com, liam@infradead.org, vbabka@kernel.org,
harry@kernel.org, jannh@google.com, lance.yang@linux.dev,
baolin.wang@linux.alibaba.com, shikemeng@huaweicloud.com,
nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org,
youngjun.park@lge.com, linux-mm@kvack.org, rppt@kernel.org,
surenb@google.com, mhocko@suse.com, pfalcato@suse.de,
ryan.roberts@arm.com, anshuman.khandual@arm.com
Subject: Re: [PATCH v5 8/8] mm/rmap: batch unmap anonymous swap-backed large folios
Date: Thu, 26 Nov 2026 13:20:50 +0100 [thread overview]
Message-ID: <e43ec92b-1759-494b-ae08-257e29d6999f@kernel.org> (raw)
In-Reply-To: <20261123053543.295467-9-dev.jain@arm.com>
On 11/23/26 06:35, Dev Jain wrote:
> Enable batch clearing of PTEs and batch swap setting of PTEs for anon
> swap-backed folio unmapping.
>
> Processing all PTEs of a large folio in one go helps us batch across
> atomics (add_mm_counter() etc), barriers in __folio_try_share_anon_rmap(),
> and repeated calls to page_vma_mapped_walk(). In general, batching helps
> execute similar code together, making the path more memory and CPU
> friendly.
>
> On arm64-contpte, batching also helps avoid redundant ptep_get() calls
> and TLB flushes while breaking the contpte mapping.
>
> The handling of anon-exclusivity is very similar to commit cac1db8c3aad
> ("mm: optimize mprotect() by PTE batching"). Since
> folio_unmap_pte_batch() does not look at the bits of the underlying page,
> process sub-batches of PTEs pointing to pages with the same exclusivity
> state, and batch set only those PTEs to swap PTEs in one go.
>
> Disable batching for sparc (because of arch_unmap_one), we will batch this
> later.
>
> Rmap accounting and reference accounting must happen when anon folio unmap
> succeeds. If a large folio is only partially batched or a later sub-batch
> fails, account only the pages that were actually unmapped.
>
> If the batch length is less than the number of pages in the folio, skip
> over this batch. page_vma_mapped_walk() handles this: check_pte() returns
> true only if any of [pvmw->pfn, pvmw->pfn + nr_pages) is mapped by the
> PTE. Swap PTEs have no underlying PFN, so check_pte() returns false until
> the walk reaches the next present PTE to unmap.
>
> Remove the finish_unmap label since no goto callers are left now.
>
> Signed-off-by: Dev Jain <dev.jain@arm.com>
> ---
> mm/rmap.c | 118 +++++++++++++++++++++++++++++++++++++++---------------
> 1 file changed, 86 insertions(+), 32 deletions(-)
>
> diff --git a/mm/rmap.c b/mm/rmap.c
> index 98690947ef19..818aecfa820b 100644
> --- a/mm/rmap.c
> +++ b/mm/rmap.c
> @@ -1982,12 +1982,13 @@ static inline unsigned int folio_unmap_pte_batch(struct folio *folio,
> end_addr = pte_table_clamped_end(addr, vma->vm_end);
> max_nr = (end_addr - addr) >> PAGE_SHIFT;
>
> - /* We only support lazyfree or file folios batching for now ... */
> - if (folio_test_anon(folio) && folio_test_swapbacked(folio))
> + if (pte_unused(pte))
> return 1;
>
> - if (pte_unused(pte))
> +#ifdef __HAVE_ARCH_UNMAP_ONE
> + if (folio_test_anon(folio) && folio_test_swapbacked(folio))
> return 1;
> +#endif
Oh no, what about performance on sparc64 ;)
>
> /*
> * If unmap fails, we need to restore the ptes. To avoid accidentally
> @@ -2158,16 +2159,25 @@ static pte_t swp_pte_prepare(swp_entry_t entry, pte_t old_pte,
> return swp_pte;
> }
>
> -static bool ttu_anon_swapbacked_folio(struct vm_area_struct *vma,
> +static void finish_folio_unmap_batch(struct vm_area_struct *vma,
> + struct folio *folio, struct page *page, unsigned long nr_pages)
> +{
> + folio_remove_rmap_ptes(folio, page, nr_pages, vma);
> + if (vma_test_any_mask(vma, VMA_LOCKED_MASK))
> + mlock_drain_local();
> + folio_put_refs(folio, nr_pages);
> +}
I think this cleanup can go into a separate cleanup patch to reduce the churn in
this patch.
> +
> +static bool __ttu_anon_swapbacked_folio(struct vm_area_struct *vma,
> struct folio *folio, struct page *page, unsigned long address,
> - pte_t *ptep, pte_t pteval)
> + pte_t *ptep, pte_t pteval, unsigned long nr_pages,
> + bool anon_exclusive)
> {
> - const bool anon_exclusive = folio_test_anon(folio) &&
> - PageAnonExclusive(page);
> swp_entry_t entry = folio_page_swap_entry(folio, page);
> struct mm_struct *mm = vma->vm_mm;
> + pte_t swp_pte;
>
> - if (folio_dup_swap_pages(folio, page, 1) < 0)
> + if (folio_dup_swap_pages(folio, page, nr_pages) < 0)
> return false;
>
> /*
> @@ -2176,25 +2186,61 @@ static bool ttu_anon_swapbacked_folio(struct vm_area_struct *vma,
> * so we'll not check/care.
> */
> if (arch_unmap_one(mm, vma, address, pteval) < 0) {
> - folio_put_swap_pages(folio, page, 1);
> + VM_WARN_ON(nr_pages != 1);
> + folio_put_swap_pages(folio, page, nr_pages);
> return false;
> }
>
> /* See folio_try_share_anon_rmap(): clear PTE first. */
> - if (anon_exclusive && folio_try_share_anon_rmap_pte(folio, page)) {
> - folio_put_swap_pages(folio, page, 1);
> + if (anon_exclusive &&
> + folio_try_share_anon_rmap_ptes(folio, page, nr_pages)) {
> + folio_put_swap_pages(folio, page, nr_pages);
> return false;
> }
>
> mm_prepare_for_swap_entries(mm);
> - dec_mm_counter(mm, MM_ANONPAGES);
> - inc_mm_counter(mm, MM_SWAPENTS);
> - set_pte_at(mm, address, ptep,
> - swp_pte_prepare(entry, pteval, anon_exclusive));
> + add_mm_counter(mm, MM_ANONPAGES, -nr_pages);
> + add_mm_counter(mm, MM_SWAPENTS, nr_pages);
> + swp_pte = swp_pte_prepare(entry, pteval, anon_exclusive);
> + set_softleaf_ptes(mm, address, ptep, swp_pte, nr_pages);
> return true;
> }
I think this change (make it consume nr_pages and anon_exclusive) can also go
into an earlier cleanup patch to make this patch easier to consume.
>
> -static bool ttu_anon_folio(struct vm_area_struct *vma, struct folio *folio,
> +static unsigned long ttu_anon_swapbacked_folio(struct vm_area_struct *vma,
> + struct folio *folio, struct page *first_page,
> + unsigned long address, pte_t *ptep, pte_t pteval,
> + unsigned long nr_pages)
> +{
> + unsigned long batch_idx = 0;
> + unsigned long nr_to_unmap = nr_pages;
> +
> + while (nr_pages) {
> + bool exclusive;
> + unsigned long len = page_anon_exclusive_batch(batch_idx,
> + nr_pages, first_page, &exclusive);
See below. This style of indenting creates an unreadable blob.
> +
> + if (!__ttu_anon_swapbacked_folio(vma, folio,
> + first_page + batch_idx, address, ptep, pteval,
> + len, exclusive)) {
Same here. Not pleasing to the eye.
> + /* Restore the remaining PTEs that were cleared. */
> + set_ptes(vma->vm_mm, address, ptep, pteval, nr_pages);
> + return nr_to_unmap - nr_pages;
> + }
> +
> + nr_pages -= len;
> + if (!nr_pages)
> + break;
> +
> + pteval = pte_advance_pfn(pteval, len);
> + address += len * PAGE_SIZE;
> + batch_idx += len;
> + ptep += len;
> + }
> +
> + return nr_to_unmap;
> +}
> +
> +static unsigned long ttu_anon_folio(struct vm_area_struct *vma, struct folio *folio,
> struct page *page, unsigned long address, pte_t *ptep,
> pte_t pteval, unsigned long nr_pages)
> {
> @@ -2203,15 +2249,21 @@ static bool ttu_anon_folio(struct vm_area_struct *vma, struct folio *folio,
> * See handle_pte_fault() ...
> */
> if (WARN_ON_ONCE(folio_test_swapbacked(folio) !=
> - folio_test_swapcache(folio)))
> - return false;
> + folio_test_swapcache(folio))) {
> + set_ptes(vma->vm_mm, address, ptep, pteval, nr_pages);
> + return 0;
> + }
>
> - if (!folio_test_swapbacked(folio))
> - return ttu_anon_lazyfree_folio(vma, folio, nr_pages);
> + if (!folio_test_swapbacked(folio)) {
> + if (!ttu_anon_lazyfree_folio(vma, folio, nr_pages)) {
> + set_ptes(vma->vm_mm, address, ptep, pteval, nr_pages);
> + return 0;
> + }
> + return nr_pages;
> + }
>
> - /* nr_pages > 1 not supported yet */
> return ttu_anon_swapbacked_folio(vma, folio, page, address, ptep,
> - pteval);
> + pteval, nr_pages);
> }
>
> /*
> @@ -2380,13 +2432,18 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
> */
> dec_mm_counter(mm, mm_counter(folio));
> } else if (folio_test_anon(folio)) {
> - if (!ttu_anon_folio(vma, folio, page, address,
> - pvmw.pte, pteval, nr_pages)) {
> - set_ptes(mm, address, pvmw.pte, pteval, nr_pages);
> + unsigned long nr_unmapped = ttu_anon_folio(vma, folio,
> + page, address, pvmw.pte,
> + pteval, nr_pages);
unreadable. Feel free to use > 80c and properly indent.
Maybe you can split this patch reasonable up further, there is jsut too much
happening at once.
--
Cheers,
David
next prev parent reply other threads:[~2026-11-26 12:21 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 5:35 [PATCH v5 0/8] Optimize anonymous swapbacked large folio unmapping Dev Jain
2026-11-23 5:35 ` [PATCH v5 1/8] mm/swapfile: add batched version of folio_dup_swap Dev Jain
2026-11-26 10:16 ` Garg, Shivank
2026-11-23 5:35 ` [PATCH v5 2/8] mm/swapfile: add batched version of folio_put_swap Dev Jain
2026-11-26 10:29 ` Garg, Shivank
2026-11-26 11:35 ` David Hildenbrand (Arm)
2026-11-23 5:35 ` [PATCH v5 3/8] mm: move anon-exclusive batch helper to rmap.h Dev Jain
2026-11-26 10:36 ` Garg, Shivank
2026-11-26 11:49 ` David Hildenbrand (Arm)
2026-11-23 5:35 ` [PATCH v5 4/8] mm/rmap: Add batched version of folio_try_share_anon_rmap_pte Dev Jain
2026-11-26 11:54 ` David Hildenbrand (Arm)
2026-11-26 11:59 ` Garg, Shivank
2026-11-23 5:35 ` [PATCH v5 5/8] mm/internal: rename swap offset helpers to softleaf offset Dev Jain
2026-11-26 12:09 ` David Hildenbrand (Arm)
2026-11-26 12:12 ` Garg, Shivank
2026-11-23 5:35 ` [PATCH v5 6/8] mm/internal: add set_softleaf_ptes Dev Jain
2026-11-26 12:11 ` David Hildenbrand (Arm)
2026-11-26 12:16 ` Garg, Shivank
2026-11-23 5:35 ` [PATCH v5 7/8] mm/memory: use set_softleaf_ptes for uffd-wp markers Dev Jain
2026-11-26 12:21 ` Garg, Shivank
2026-11-23 5:35 ` [PATCH v5 8/8] mm/rmap: batch unmap anonymous swap-backed large folios Dev Jain
2026-11-26 12:20 ` David Hildenbrand (Arm) [this message]
2026-11-23 6:46 ` [PATCH v5 0/8] Optimize anonymous swapbacked large folio unmapping Andrew Morton
2026-11-25 4:39 ` Dev Jain
2026-11-25 18:40 ` Garg, Shivank
2026-11-25 17:58 ` Garg, Shivank
2026-11-26 5:04 ` Dev Jain
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=e43ec92b-1759-494b-ae08-257e29d6999f@kernel.org \
--to=david@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=anshuman.khandual@arm.com \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=baoquan.he@linux.dev \
--cc=chrisl@kernel.org \
--cc=dev.jain@arm.com \
--cc=harry@kernel.org \
--cc=hughd@google.com \
--cc=jannh@google.com \
--cc=kasong@tencent.com \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mhocko@suse.com \
--cc=nphamcs@gmail.com \
--cc=pfalcato@suse.de \
--cc=riel@surriel.com \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=shikemeng@huaweicloud.com \
--cc=surenb@google.com \
--cc=vbabka@kernel.org \
--cc=youngjun.park@lge.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