mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Daniel Byrne <daniel1.byrne@intel.com>
To: Shivank Garg <shivankg@amd.com>,
	Ravi Jonnalagadda <ravis.opensrc@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	David Hildenbrand <david@kernel.org>, Zi Yan <ziy@nvidia.com>,
	Matthew Brost <matthew.brost@intel.com>,
	Joshua Hahn <joshua.hahnjy@gmail.com>,
	Rakie Kim <rakie.kim@sk.com>, Byungchul Park <byungchul@sk.com>,
	Gregory Price <gourry@gourry.net>,
	Ying Huang <ying.huang@linux.alibaba.com>,
	Alistair Popple <apopple@nvidia.com>,
	Lorenzo Stoakes <ljs@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	Vlastimil Babka <vbabka@kernel.org>,
	Mike Rapoport <rppt@kernel.org>,
	Suren Baghdasaryan <surenb@google.com>,
	Michal Hocko <mhocko@suse.com>,
	Karim Manaouil <kmanaouil.dev@gmail.com>,
	Frank van der Linden <fvdl@google.com>,
	Teja Vojjala <tejavojjala@google.com>,
	Pravin Tamkhane <pravintamkhane@google.com>,
	Kinsey Ho <kinseyho@google.com>, Wei Xu <weixugc@google.com>,
	Matthew Wilcox <willy@infradead.org>,
	Davidlohr Bueso <dave@stgolabs.net>,
	Vinod Koul <vkoul@kernel.org>, Bharata B Rao <bharata@amd.com>,
	SeongJae Park <sj@kernel.org>,
	David Rientjes <rientjes@google.com>,
	Xuezheng Chu <xuezhengchu@huawei.com>,
	Yiannis Nikolakopoulos <yiannis@zptcorp.com>,
	Dave Hansen <dave.hansen@intel.com>,
	Johannes Weiner <hannes@cmpxchg.org>,
	John Hubbard <jhubbard@nvidia.com>, Peter Xu <peterx@redhat.com>,
	Rik van Riel <riel@surriel.com>,
	Shakeel Butt <shakeel.butt@linux.dev>, Tejun Heo <tj@kernel.org>,
	Fan Ni <nifan.cxl@gmail.com>, Jonathan Cameron <jic23@kernel.org>,
	"Aneesh Kumar K.V" <aneesh.kumar@kernel.org>,
	Nathan Lynch <nathan.lynch@amd.com>, Frank Li <Frank.li@nxp.com>,
	Dan Williams <djbw@kernel.org>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	Mike Day <michael.day@amd.com>,
	dmaengine@vger.kernel.org,
	Vinicius Costa Gomes <vinicius.gomes@intel.com>,
	Frank Li <Frank.Li@kernel.org>, Dave Jiang <dave.jiang@intel.com>,
	Daniel Byrne <daniel1.byrne@intel.com>
Subject: [RFC PATCH 05/10] mm/migrate: give hugetlb its own bit for the content-copied marker
Date: Tue,  1 Dec 2026 06:12:54 -0800	[thread overview]
Message-ID: <20261201141259.2124188-6-daniel1.byrne@intel.com> (raw)
In-Reply-To: <20261201141259.2124188-1-daniel1.byrne@intel.com>

FOLIO_CONTENT_COPIED is BIT(2) of dst->migrate_info, which aliases
folio->private. On a hugetlb folio that word holds hugetlb's own flags,
and bit 2 is HPG_temporary. A surplus hugetlb destination has it set, so
__migrate_folio() reads the destination as already copied, skips the
copy and clears the flag. The data of the source folio is lost.

Give the marker a bit of its own on a hugetlb folio. HPG_CONTENT_COPIED
is the top bit of the flag word, which hugetlb does not use, and a
BUILD_BUG_ON keeps it above the flags hugetlb defines. It is set and
cleared with the atomic bit operations that hugetlb uses for its flags.

Set and consume the marker through folio_set_migrate_copied() and
folio_test_clear_migrate_copied(), which pick the encoding by the type
of the folio, and use them in the batch path and in dcbm.
migrate_huge_page_move_mapping() consumes the marker as well. Nothing
marks a hugetlb destination yet, so this only keeps a batched hugetlb
copy from needing another change there.

This is a fix to "mm/migrate: skip data copy for already-copied folios"
and is meant to be folded into it.

Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
---
A fix to patch 1, with patches 2 and 4 converted to the helper. For
folding into v7. On patches 1 to 4 alone, 1 GiB of anonymous hugetlb
pages moved to surplus destinations with move_pages() comes back with
every word wrong. The cover letter has the run.

 drivers/migrate_offload/dcbm/dcbm.c |  6 ++--
 include/linux/migrate.h             | 56 +++++++++++++++++++++++++++++
 mm/migrate.c                        | 17 +++++----
 3 files changed, 67 insertions(+), 12 deletions(-)

diff --git a/drivers/migrate_offload/dcbm/dcbm.c b/drivers/migrate_offload/dcbm/dcbm.c
index bacbcc689171..3df9232586ff 100644
--- a/drivers/migrate_offload/dcbm/dcbm.c
+++ b/drivers/migrate_offload/dcbm/dcbm.c
@@ -286,11 +286,11 @@ static int folios_copy_dma(struct list_head *dst_list,
 	}
 
 	/*
-	 * All folios copied; mark each dst with FOLIO_CONTENT_COPIED so
-	 * __migrate_folio() skips the per-folio copy in the move phase.
+	 * All folios copied; mark each destination so the move phase
+	 * skips the per-folio copy.
 	 */
 	list_for_each_entry(dst, dst_list, lru)
-		dst->migrate_info |= FOLIO_CONTENT_COPIED;
+		folio_set_migrate_copied(dst);
 
 	cleanup_dma_work(works, actual_channels);
 
diff --git a/include/linux/migrate.h b/include/linux/migrate.h
index 8e762f9bc16f..935304c89c8c 100644
--- a/include/linux/migrate.h
+++ b/include/linux/migrate.h
@@ -3,6 +3,7 @@
 #define _LINUX_MIGRATE_H
 
 #include <linux/mm.h>
+#include <linux/bitops.h>
 #include <linux/mempolicy.h>
 #include <linux/migrate_mode.h>
 #include <linux/hugetlb.h>
@@ -88,6 +89,18 @@ int migrate_folios_mc_copy(struct list_head *dst_list,
  * FOLIO_CONTENT_COPIED tells __migrate_folio() that the folio contents
  * have already been copied, so the per-folio copy can be skipped. A
  * driver must set this bit on each dst folio it copied.
+ *
+ * migrate_info aliases folio->private, which hugetlb uses as its own
+ * flag word, so FOLIO_WAS_* and the anon_vma pointer may never be
+ * stored on a hugetlb folio - it is migrated one folio at a time and
+ * never reaches the batched path that records them.
+ *
+ * The content-copied marker is needed on hugetlb too, so it lives in
+ * HPG_CONTENT_COPIED for those folios: a bit of the same word that
+ * hugetlb itself does not use, set and cleared with the atomic
+ * operations hugetlb uses for its flags. Use
+ * folio_set_migrate_copied() and folio_test_clear_migrate_copied()
+ * rather than touching either encoding directly.
  */
 enum {
 	FOLIO_WAS_MAPPED	= BIT(0),
@@ -101,6 +114,49 @@ enum {
 #endif
 };
 
+/*
+ * Where the marker lives on a hugetlb folio: the top bit of the flag
+ * word, far above the flags hugetlb defines (see the BUILD_BUG_ON in
+ * folio_set_migrate_copied()).
+ */
+#define HPG_CONTENT_COPIED	(BITS_PER_LONG - 1)
+
+/**
+ * folio_set_migrate_copied - mark a destination folio as already copied.
+ * @dst: destination folio, owned by the caller and not yet visible.
+ *
+ * Providers call this for every destination whose contents they copied,
+ * so that the move phase skips the per-folio copy.
+ */
+static inline void folio_set_migrate_copied(struct folio *dst)
+{
+	BUILD_BUG_ON(__NR_HPAGEFLAGS > HPG_CONTENT_COPIED);
+
+	if (folio_test_hugetlb(dst))
+		set_bit(HPG_CONTENT_COPIED, (unsigned long *)&dst->private);
+	else
+		dst->migrate_info |= FOLIO_CONTENT_COPIED;
+}
+
+/**
+ * folio_test_clear_migrate_copied - consume the already-copied marker.
+ * @dst: destination folio being moved.
+ *
+ * Return: true when the contents were copied before the move phase, in
+ * which case the marker is cleared.
+ */
+static inline bool folio_test_clear_migrate_copied(struct folio *dst)
+{
+	if (folio_test_hugetlb(dst))
+		return test_and_clear_bit(HPG_CONTENT_COPIED,
+					  (unsigned long *)&dst->private);
+
+	if (!(dst->migrate_info & FOLIO_CONTENT_COPIED))
+		return false;
+	dst->migrate_info &= ~FOLIO_CONTENT_COPIED;
+	return true;
+}
+
 #else
 
 static inline void putback_movable_pages(struct list_head *l) {}
diff --git a/mm/migrate.c b/mm/migrate.c
index 531d54b2185e..79970170a53a 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -742,13 +742,16 @@ int migrate_huge_page_move_mapping(struct address_space *mapping,
 {
 	XA_STATE(xas, &mapping->i_pages, src->index);
 	int rc, expected_count = folio_expected_ref_count(src) + 1;
+	const bool already_copied = folio_test_clear_migrate_copied(dst);
 
 	if (folio_ref_count(src) != expected_count)
 		return -EAGAIN;
 
-	rc = folio_mc_copy(dst, src);
-	if (unlikely(rc))
-		return rc;
+	if (!already_copied) {
+		rc = folio_mc_copy(dst, src);
+		if (unlikely(rc))
+			return rc;
+	}
 
 	xas_lock_irq(&xas);
 	if (!folio_ref_freeze(src, expected_count)) {
@@ -872,11 +875,7 @@ static int __migrate_folio(struct address_space *mapping, struct folio *dst,
 			   enum migrate_mode mode)
 {
 	int rc, expected_count = folio_expected_ref_count(src) + 1;
-	const bool already_copied = dst->migrate_info & FOLIO_CONTENT_COPIED;
-
-	/* Consume the content copied marker */
-	if (already_copied)
-		dst->migrate_info &= ~FOLIO_CONTENT_COPIED;
+	const bool already_copied = folio_test_clear_migrate_copied(dst);
 
 	/* Check whether src does not have extra refs before we do more work */
 	if (folio_ref_count(src) != expected_count)
@@ -1767,7 +1766,7 @@ int migrate_folios_mc_copy(struct list_head *dst_list,
 		ret = folio_mc_copy(dst, src);
 		if (ret)
 			return ret;
-		dst->migrate_info |= FOLIO_CONTENT_COPIED;
+		folio_set_migrate_copied(dst);
 		dst = list_next_entry(dst, lru);
 		cond_resched();
 	}
-- 
2.43.0


  parent reply	other threads:[~2026-12-01 14:19 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 14:12 [RFC PATCH 00/10] mm/migrate: dcbm for Intel DSA, on top of the batch copy offload RFC v6 Daniel Byrne
2026-12-01 14:12 ` [RFC PATCH 01/10] mm/migrate: skip data copy for already-copied folios Daniel Byrne
2026-12-01 14:12 ` [RFC PATCH 02/10] mm/migrate: add batch-copy path in migrate_pages_batch Daniel Byrne
2026-12-01 14:12 ` [RFC PATCH 03/10] mm/migrate: add copy offload registration infrastructure Daniel Byrne
2026-12-01 14:12 ` [RFC PATCH 04/10] drivers/migrate_offload: add DMA batch copy driver (dcbm) Daniel Byrne
2026-12-01 14:12 ` Daniel Byrne [this message]
2026-12-01 14:12 ` [RFC PATCH 06/10] mm/migrate: keep folios with private data off the batch copy Daniel Byrne
2026-12-01 14:12 ` [RFC PATCH 07/10] mm/migrate: no filesystem reclaim under the batch's folio locks Daniel Byrne
2026-12-01 14:12 ` [RFC PATCH 08/10] dmaengine: reintroduce DMA_MEMCPY_SG Daniel Byrne
2026-12-01 19:51   ` Frank Li
2026-12-01 14:12 ` [RFC PATCH 09/10] dmaengine: add DMA_PREP_CACHE_CONTROL Daniel Byrne
2026-12-01 14:12 ` [RFC PATCH 10/10] drivers/migrate_offload: make dcbm correct on shared, out-of-order engines Daniel Byrne
2026-12-01 15:24 ` [RFC PATCH 00/10] mm/migrate: dcbm for Intel DSA, on top of the batch copy offload RFC v6 Dave Hansen
2026-12-01 16:05   ` Garg, Shivank

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=20261201141259.2124188-6-daniel1.byrne@intel.com \
    --to=daniel1.byrne@intel.com \
    --cc=Frank.Li@kernel.org \
    --cc=Frank.li@nxp.com \
    --cc=akpm@linux-foundation.org \
    --cc=aneesh.kumar@kernel.org \
    --cc=apopple@nvidia.com \
    --cc=bharata@amd.com \
    --cc=byungchul@sk.com \
    --cc=dave.hansen@intel.com \
    --cc=dave.jiang@intel.com \
    --cc=dave@stgolabs.net \
    --cc=david@kernel.org \
    --cc=djbw@kernel.org \
    --cc=dmaengine@vger.kernel.org \
    --cc=fvdl@google.com \
    --cc=gourry@gourry.net \
    --cc=hannes@cmpxchg.org \
    --cc=jhubbard@nvidia.com \
    --cc=jic23@kernel.org \
    --cc=joshua.hahnjy@gmail.com \
    --cc=kinseyho@google.com \
    --cc=kmanaouil.dev@gmail.com \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=matthew.brost@intel.com \
    --cc=mhocko@suse.com \
    --cc=michael.day@amd.com \
    --cc=nathan.lynch@amd.com \
    --cc=nifan.cxl@gmail.com \
    --cc=peterx@redhat.com \
    --cc=pravintamkhane@google.com \
    --cc=rakie.kim@sk.com \
    --cc=ravis.opensrc@gmail.com \
    --cc=riel@surriel.com \
    --cc=rientjes@google.com \
    --cc=rppt@kernel.org \
    --cc=shakeel.butt@linux.dev \
    --cc=shivankg@amd.com \
    --cc=sj@kernel.org \
    --cc=surenb@google.com \
    --cc=tejavojjala@google.com \
    --cc=tj@kernel.org \
    --cc=vbabka@kernel.org \
    --cc=vinicius.gomes@intel.com \
    --cc=vkoul@kernel.org \
    --cc=weixugc@google.com \
    --cc=willy@infradead.org \
    --cc=xuezhengchu@huawei.com \
    --cc=yiannis@zptcorp.com \
    --cc=ying.huang@linux.alibaba.com \
    --cc=ziy@nvidia.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

all inboxes | Powered by JetHome®