From: nakixii <nakixii@gmail.com>
To: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org,
Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@kernel.org>,
Johannes Weiner <hannes@cmpxchg.org>,
Chris Li <chrisl@kernel.org>, Kairui Song <kasong@tencent.com>,
Nhat Pham <nphamcs@gmail.com>, Yosry Ahmed <yosry@kernel.org>,
Minchan Kim <minchan@kernel.org>,
Sergey Senozhatsky <senozhatsky@chromium.org>,
Barry Song <baohua@kernel.org>, Hugh Dickins <hughd@google.com>,
Michal Hocko <mhocko@kernel.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Shakeel Butt <shakeel.butt@linux.dev>,
Mike Rapoport <rppt@kernel.org>, Peter Xu <peterx@redhat.com>,
Suren Baghdasaryan <surenb@google.com>
Subject: [RFC PATCH 09/16] mm: memcomp: batch unmap reserved large folios
Date: Mon, 16 Nov 2026 19:01:15 +0800 [thread overview]
Message-ID: <20261116110122.309748-10-nakixii@gmail.com> (raw)
In-Reply-To: <20261116110122.309748-1-nakixii@gmail.com>
Batch uniform PTE runs only for reserved memcomp folios. Retain the
single-PTE path for mixed protection, soft-dirty or exclusivity state.
Acquire all token owners before clearing mappings and unwind the whole
prefix when sharing or architecture checks fail.
Keep TLB ordering, notifier scope and the native rmap completion
context.
Do not import the general lazyfree batching policy.
Signed-off-by: nakixii <nakixii@gmail.com>
---
mm/memcomp_rmap.h | 139 ++++++++++++++++++++++++++++++++++++++++++++++
mm/rmap.c | 12 ++++
2 files changed, 151 insertions(+)
create mode 100644 mm/memcomp_rmap.h
diff --git a/mm/memcomp_rmap.h b/mm/memcomp_rmap.h
new file mode 100644
index 0000000000000000000000000000000000000000..52cdd471ad70fc27372fa819fc8996d57a3f418e
--- /dev/null
+++ b/mm/memcomp_rmap.h
@@ -0,0 +1,139 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _MM_MEMCOMP_RMAP_H
+#define _MM_MEMCOMP_RMAP_H
+
+#ifdef CONFIG_MEMCOMP
+/*
+ * Unlike the newer generic lazyfree batching path, this backport only batches
+ * a reserved memcomp span. Mixed protection or exclusive bits stay on the
+ * existing single-PTE path. The caller owns folio lock, PTL and notifier scope.
+ */
+static bool memcomp_unmap_exclusive_uniform(struct folio *folio)
+{
+ bool exclusive = PageAnonExclusive(folio_page(folio, 0));
+ unsigned int i;
+
+ for (i = 1; i < folio_nr_pages(folio); i++) {
+ if (PageAnonExclusive(folio_page(folio, i)) != exclusive)
+ return false;
+ }
+ return true;
+}
+
+static int memcomp_share_anon_ptes(struct folio *folio, struct page *page,
+ unsigned int nr)
+{
+ unsigned int i;
+
+ if (IS_ENABLED(CONFIG_HAVE_GUP_FAST))
+ /* Paired with the memory barrier in try_grab_folio(). */
+ smp_mb();
+ if (unlikely(folio_maybe_dma_pinned(folio)))
+ return -EBUSY;
+ for (i = 0; i < nr; i++)
+ ClearPageAnonExclusive(page + i);
+ if (IS_ENABLED(CONFIG_HAVE_GUP_FAST))
+ /* Paired with the smp_rmb() in gup_must_unshare(). */
+ smp_mb__after_atomic();
+ return 0;
+}
+
+/* 1: use the original actor; >1: complete batch; <0: restored, abort walk. */
+static int memcomp_unmap_ptes(struct page_vma_mapped_walk *pvmw,
+ struct folio *folio, struct vm_area_struct *vma,
+ enum ttu_flags flags)
+{
+ struct mm_struct *mm = vma->vm_mm;
+ unsigned long address = pvmw->address, end, token;
+ unsigned int i, nr, got = 0;
+ struct page *page;
+ swp_entry_t entry;
+ pte_t first, old, swap;
+ bool exclusive;
+
+ if (!folio_memcomp_reserved(folio) || !folio_test_large(folio) ||
+ !folio_test_anon(folio) || !folio_test_swapbacked(folio) ||
+ folio_test_hugetlb(folio) || (flags & TTU_HWPOISON) ||
+ !memcomp_unmap_exclusive_uniform(folio))
+ return 1;
+ first = ptep_get(pvmw->pte);
+ if (!pte_present(first) || pte_unused(first))
+ return 1;
+ page = pfn_to_page(pte_pfn(first));
+ nr = folio_nr_pages(folio) - folio_page_idx(folio, page);
+ end = min(pmd_addr_end(address, vma->vm_end), vma->vm_end);
+ nr = min_t(unsigned int, nr, (end - address) >> PAGE_SHIFT);
+ if (nr < 2)
+ return 1;
+ for (i = 1; i < nr; i++) {
+ if (!pte_same(pte_advance_pfn(first, i), ptep_get(pvmw->pte + i)))
+ break;
+ }
+ nr = i;
+ if (nr < 2)
+ return 1;
+ entry = page_swap_entry(page);
+ token = swp_offset(entry);
+ if (!is_memcomp_entry(entry) || !token || token > U32_MAX - (nr - 1))
+ return 1;
+ /* Each future PTE gets a real owner; failure unwinds the whole prefix. */
+ for (; got < nr; got++) {
+ if (!memcomp_entry_get(token + got))
+ goto refs_fail;
+ }
+ exclusive = PageAnonExclusive(page);
+ flush_cache_range(vma, address, address + nr * PAGE_SIZE);
+ old = get_and_clear_full_ptes(mm, address, pvmw->pte, nr, 0);
+ if (should_defer_flush(mm, flags)) {
+ for (i = 0; i < nr; i++)
+ set_tlb_ubc_flush_pending(mm, old, address + i * PAGE_SIZE);
+ } else {
+ flush_tlb_range(vma, address, address + nr * PAGE_SIZE);
+ }
+ if (pte_dirty(old))
+ folio_mark_dirty(folio);
+ update_hiwater_rss(mm);
+ for (i = 0; i < nr; i++) {
+ if (arch_unmap_one(mm, vma, address + i * PAGE_SIZE,
+ pte_advance_pfn(old, i)) < 0)
+ goto restore;
+ }
+ if (exclusive && memcomp_share_anon_ptes(folio, page, nr))
+ goto restore;
+ if (list_empty(&mm->mmlist)) {
+ spin_lock(&mmlist_lock);
+ if (list_empty(&mm->mmlist))
+ list_add(&mm->mmlist, &init_mm.mmlist);
+ spin_unlock(&mmlist_lock);
+ }
+ add_mm_counter(mm, MM_ANONPAGES, -(int)nr);
+ add_mm_counter(mm, MM_SWAPENTS, nr);
+ swap = swp_entry_to_pte(entry);
+ if (exclusive)
+ swap = pte_swp_mkexclusive(swap);
+ if (pte_soft_dirty(old))
+ swap = pte_swp_mksoft_dirty(swap);
+ if (pte_uffd_wp(old))
+ swap = pte_swp_mkuffd_wp(swap);
+ for (i = 0; i < nr; i++)
+ set_pte_at(mm, address + i * PAGE_SIZE, pvmw->pte + i,
+ pte_move_swp_offset(swap, i));
+ folio_remove_rmap_ptes(folio, page, nr, vma);
+ if (vma->vm_flags & VM_LOCKED)
+ mlock_drain_local();
+ folio_put_refs(folio, nr);
+ pvmw->pte += nr - 1;
+ pvmw->address += (nr - 1) * PAGE_SIZE;
+ return nr;
+restore:
+ /* Complete invalidation before set_ptes() may form a contpte range. */
+ flush_tlb_range(vma, address, address + nr * PAGE_SIZE);
+ /* Dirty/young aggregation is conservative; protection bits were uniform. */
+ set_ptes(mm, address, pvmw->pte, old, nr);
+refs_fail:
+ while (got)
+ memcomp_invalidate(token + --got);
+ return -EBUSY;
+}
+#endif /* CONFIG_MEMCOMP */
+#endif /* _MM_MEMCOMP_RMAP_H */
diff --git a/mm/rmap.c b/mm/rmap.c
index 41f1b9f645a261021698d7972649264de391601f..874cf92c1096267a5524b29ab55cc10a901cbcb1 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -1656,6 +1656,8 @@ void folio_remove_rmap_pmd(struct folio *folio, struct page *page,
#endif
}
+#include "memcomp_rmap.h"
+
/*
* @arg: enum ttu_flags will be passed to this argument
*/
@@ -1737,6 +1739,16 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
/* Unexpected PMD-mapped THP? */
VM_BUG_ON_FOLIO(!pvmw.pte, folio);
+#ifdef CONFIG_MEMCOMP
+ {
+ int nr = memcomp_unmap_ptes(&pvmw, folio, vma, flags);
+
+ if (nr < 0)
+ goto walk_abort;
+ if (nr > 1)
+ continue;
+ }
+#endif
pfn = pte_pfn(ptep_get(pvmw.pte));
subpage = folio_page(folio, pfn - folio_pfn(folio));
next prev parent reply other threads:[~2026-11-16 11:04 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 11:01 [RFC PATCH 00/16] mm: add in-memory compression of reclaimed pages nakixii
2026-11-16 11:01 ` [RFC PATCH 01/16] mm: userfaultfd: check the exclusivity of the page being moved nakixii
2026-11-16 11:01 ` [RFC PATCH 02/16] mm: add a swap entry type for in-memory compressed pages nakixii
2026-11-16 11:01 ` [RFC PATCH 03/16] mm: memcomp: compress reclaimed anonymous pages in memory nakixii
2026-11-16 11:01 ` [RFC PATCH 04/16] mm: userfaultfd: let UFFDIO_MOVE move memcomp entries nakixii
2026-11-16 11:01 ` [RFC PATCH 05/16] mm: memcomp: charge compressed pages to the source memcg nakixii
2026-11-16 11:01 ` [RFC PATCH 06/16] mm: memcomp: store shmem and tmpfs pages nakixii
2026-11-16 11:01 ` [RFC PATCH 07/16] mm: memcomp: write cold pages back to swap nakixii
2026-11-16 11:01 ` [RFC PATCH 08/16] mm: memcomp: compress foreground batches asynchronously nakixii
2026-11-16 11:01 ` nakixii [this message]
2026-11-16 11:01 ` [RFC PATCH 10/16] mm: memcomp: queue restored large folios for splitting nakixii
2026-11-16 11:01 ` [RFC PATCH 11/16] mm: memcomp: account user eviction and refault nakixii
2026-11-16 11:01 ` [RFC PATCH 12/16] mm: memcomp: report precise token residency nakixii
2026-11-16 11:01 ` [RFC PATCH 13/16] mm: memcomp: add KUnit tests nakixii
2026-11-16 11:01 ` [RFC PATCH 14/16] selftests/mm: add a memcomp pageout and refault test nakixii
2026-11-16 11:01 ` [RFC PATCH 15/16] mm: memcomp: add the design doc nakixii
2026-11-16 11:01 ` [RFC PATCH 16/16] mm: memcomp: add the admin guide nakixii
2026-11-16 11:14 ` [RFC PATCH 00/16] mm: add in-memory compression of reclaimed pages Mike Rapoport
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=20261116110122.309748-10-nakixii@gmail.com \
--to=nakixii@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=cgroups@vger.kernel.org \
--cc=chrisl@kernel.org \
--cc=david@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=hughd@google.com \
--cc=kasong@tencent.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=minchan@kernel.org \
--cc=nphamcs@gmail.com \
--cc=peterx@redhat.com \
--cc=roman.gushchin@linux.dev \
--cc=rppt@kernel.org \
--cc=senozhatsky@chromium.org \
--cc=shakeel.butt@linux.dev \
--cc=surenb@google.com \
--cc=yosry@kernel.org \
/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