Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
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 04/16] mm: userfaultfd: let UFFDIO_MOVE move memcomp entries
Date: Mon, 16 Nov 2026 19:01:10 +0800	[thread overview]
Message-ID: <20261116110122.309748-5-nakixii@gmail.com> (raw)
In-Reply-To: <20261116110122.309748-1-nakixii@gmail.com>

UFFDIO_MOVE rejects memcomp entries as unsupported non-swap entries.
Support moving them without requiring the memory-cgroup, shmem,
writeback or asynchronous-compression paths.

Move nonresident tokens like swap entries while keeping a lookup
reference across PTE revalidation. The reference prevents token reuse
from changing the backing whose resident state was checked. Return
-EAGAIN for a busy entry or a stale token rather than moving it without
a stable handle. Keep zero-token movement neutral to zero ownership.

A resident pending token still names a folio whose anonymous rmap points
to the source address. Restore that folio there, then retry through the
present-PTE move path instead of moving the resident token directly.
Drop both RCU PTE mappings before the token-lock lookup and preserve the
existing PTE and exclusivity checks.

Rollback of memcomp entries makes partially unmapped large folios more
common. The per-page exclusivity check also covers that state without
treating the first page's flag as the flag of every mapped subpage.

Signed-off-by: nakixii <nakixii@gmail.com>
---
 mm/userfaultfd.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)

diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index d8a699981d097ae393a981a33cede5f5ce6e2725..0ed91fdd2e80a89073ce2f165e0d557a55218e55 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -15,6 +15,7 @@
 #include <linux/mmu_notifier.h>
 #include <linux/hugetlb.h>
 #include <linux/shmem_fs.h>
+#include <linux/memcomp.h>
 #include <asm/tlbflush.h>
 #include <asm/tlb.h>
 #include "internal.h"
@@ -1150,6 +1151,9 @@ static int move_pages_pte(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd,
 	struct anon_vma *src_anon_vma = NULL;
 	struct mmu_notifier_range range;
 	int err = 0;
+	swp_entry_t memcomp_checked_entry = { };
+	bool memcomp_token_checked = false;
+	struct memcomp_ref memcomp_checked_ref = { };
 
 	flush_cache_range(src_vma, src_addr, src_addr + PAGE_SIZE);
 	mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm,
@@ -1338,6 +1342,77 @@ static int move_pages_pte(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd,
 		struct folio *folio = NULL;
 
 		entry = pte_to_swp_entry(orig_src_pte);
+		if (is_memcomp_entry(entry)) {
+			int resident;
+
+			if (!pte_swp_exclusive(orig_src_pte)) {
+				err = -EBUSY;
+				goto out;
+			}
+			/*
+			 * A resident token must be faulted back where its
+			 * folio's rmap expects it; the present-pte path then
+			 * moves the folio.  Any other token carries no
+			 * address and moves like a swap entry.
+			 *
+			 * pte_offset_map_nolock() leaves us in an RCU read-side
+			 * section.  Drop both mappings before querying the token:
+			 * memcomp_entry_resident_get() may take the token mutex when
+			 * it wins its trylock, and lockdep quite correctly rejects
+			 * even that conditional mutex acquisition in this context.
+			 * The PTEs are revalidated by retry before either move path.
+			 */
+			/* Zero is a pure value: MOVE keeps its logical owner count. */
+			if (!swp_offset(entry)) {
+				err = move_swap_pte(mm, dst_vma, dst_addr, src_addr,
+						    dst_pte, src_pte, orig_dst_pte,
+						    orig_src_pte, dst_ptl, src_ptl, NULL);
+				goto out;
+			}
+			if (!memcomp_token_checked ||
+			    memcomp_checked_entry.val != entry.val) {
+				if (memcomp_checked_ref.slot)
+					memcomp_entry_resident_put(&memcomp_checked_ref);
+				pte_unmap(src_pte);
+				pte_unmap(dst_pte);
+				dst_pte = NULL;
+				src_pte = NULL;
+				resident = memcomp_entry_resident_get(swp_offset(entry),
+								      &memcomp_checked_ref);
+				if (resident < 0) {
+					err = resident;
+					goto out;
+				}
+				if (resident) {
+					struct vm_fault vmf = {
+						.vma = src_vma,
+						.address = src_addr,
+						.pgoff = linear_page_index(src_vma, src_addr),
+						.pmd = src_pmd,
+						.orig_pte = orig_src_pte,
+					};
+
+					err = memcomp_restore_resident(&vmf,
+								       &memcomp_checked_ref);
+					memcomp_token_checked = false;
+					if (!err)
+						goto retry;
+					goto out;
+				}
+				if (!memcomp_checked_ref.slot) {
+					/* Do not move a stale token without a stable entry. */
+					err = -EAGAIN;
+					goto out;
+				}
+				memcomp_checked_entry = entry;
+				memcomp_token_checked = true;
+				goto retry;
+			}
+			err = move_swap_pte(mm, dst_vma, dst_addr, src_addr,
+					    dst_pte, src_pte, orig_dst_pte,
+					    orig_src_pte, dst_ptl, src_ptl, NULL);
+			goto out;
+		}
 		if (non_swap_entry(entry)) {
 			if (is_migration_entry(entry)) {
 				pte_unmap(src_pte);
@@ -1400,6 +1475,8 @@ static int move_pages_pte(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd,
 	}
 
 out:
+	if (memcomp_checked_ref.slot)
+		memcomp_entry_resident_put(&memcomp_checked_ref);
 	if (src_anon_vma) {
 		anon_vma_unlock_write(src_anon_vma);
 		put_anon_vma(src_anon_vma);


  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 ` nakixii [this message]
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 ` [RFC PATCH 09/16] mm: memcomp: batch unmap reserved large folios nakixii
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-5-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