Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH 00/10] mm/migrate: dcbm for Intel DSA, on top of the batch copy offload RFC v6
@ 2026-12-01 14:12 Daniel Byrne
  2026-12-01 14:12 ` [RFC PATCH 01/10] mm/migrate: skip data copy for already-copied folios Daniel Byrne
                   ` (10 more replies)
  0 siblings, 11 replies; 14+ messages in thread
From: Daniel Byrne @ 2026-12-01 14:12 UTC (permalink / raw)
  To: Shivank Garg, Ravi Jonnalagadda
  Cc: Andrew Morton, David Hildenbrand, Zi Yan, Matthew Brost,
	Joshua Hahn, Rakie Kim, Byungchul Park, Gregory Price, Ying Huang,
	Alistair Popple, Lorenzo Stoakes, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Karim Manaouil, Frank van der Linden, Teja Vojjala,
	Pravin Tamkhane, Kinsey Ho, Wei Xu, Matthew Wilcox,
	Davidlohr Bueso, Vinod Koul, Bharata B Rao, SeongJae Park,
	David Rientjes, Xuezheng Chu, Yiannis Nikolakopoulos, Dave Hansen,
	Johannes Weiner, John Hubbard, Peter Xu, Rik van Riel,
	Shakeel Butt, Tejun Heo, Fan Ni, Jonathan Cameron,
	Aneesh Kumar K.V, Nathan Lynch, Frank Li, Dan Williams, linux-mm,
	linux-kernel, Mike Day, dmaengine, Vinicius Costa Gomes, Frank Li,
	Dave Jiang, Daniel Byrne

This is my implementation of dcbm for Intel DSA, on top of RFC v6 of
"Accelerate page migration with batch copying and hardware offload" [1].
It is for reference and for testing next to Ravi's report on v6 [2]. It
is not meant to be merged as it is. The first four patches are
Shivank's, and the rest is meant to be taken into v7 where it can fit.

What this RFC contains:
 - Patches 1 to 4 are patches 1 to 4 of RFC v6, rebased to v7.4-rc2
   with no other change, so that the series applies. Patch 1 applies as
   posted. Patches 2 to 4 are rebased, and every line they change is as
   in the posting.
 - Patches 5 to 7 are fixes to those four, meant to be folded in:
    5. FOLIO_CONTENT_COPIED is bit 2 of the word in which hugetlb keeps
       HPG_temporary, so a surplus hugetlb destination reads as already
       copied and its copy is skipped. This loses the data, with or
       without an offload driver, see "Testing". The marker gets a bit
       of its own on hugetlb, behind two helpers;
    6. a folio with private data, or with a migrate_folio() callback of
       its own, stays off the batch copy, since a copy made ahead of the
       move phase bypasses what those rely on;
    7. the provider is called with the folios of the batch locked, so it
       runs in a NOFS scope.
 - Patches 8 and 9 add to dmaengine what patch 10 uses: DMA_MEMCPY_SG
   again, and a prep flag for the provider's cache-control hint.
 - Patch 10 makes dcbm work on a provider that is shared and completes
   out of order, as DSA does:
    - every descriptor has its own completion, counted down on a
      pending counter, and an error result sends the folio to the CPU
      copy;
    - the channels are requested once, when offloading is enabled, and
      held. Concurrent batches share them through per-channel trylocks,
      and a batch that finds every channel busy is copied on the CPU;
    - source and destination are mapped per folio and paired by folio,
      without scatterlists that have to merge alike on both sides;
    - a batch keeps a bounded number of descriptors in flight on a
      channel, since a work queue has a fixed pool of them;
    - no timeout and no terminate. terminate_all acts on the whole
      channel and would end the other batches on it;
    - on a provider with DMA_MEMCPY_SG a slice goes out as
      scatter-gather transactions, and the copies ask for the
      cache-control hint unless a module parameter turns it off.

Against the three topics of Ravi's report [2]:

1. Channel lifetime. Patch 10 holds the channels. A provider that goes
   away under them is not handled in this series. That needs the
   dmaengine side first. I have a series for it that is not posted yet:
   the core marks a channel that its provider removes under clients as
   detached, the channel takes no new work, and the client releases it
   when it sees dma_chan_detached(). It builds on the two helpers of
   [3]. The dcbm side that releases such a channel follows with it.

2. Completion order. Patch 10 counts every descriptor on every
   provider and does not test DMA_COMPLETION_NO_ORDER. That capability
   goes away in my polled-status series [4], whose v2 is in
   preparation. With it a client can also wait for a descriptor by its
   cookie, and a descriptor without DMA_PREP_INTERRUPT no longer stays
   allocated on DSA.

3. Timeouts and failed channels. Patch 10 only waits. What I am working
   on beyond it is not in this series: a device whose completions are
   overdue takes no new work until everything out on it has landed, and
   the CPU copies meanwhile. NUMA hinting faults stay out of the offload
   by default there.

Also not in this RFC - the idxd side of patches 8 and 9, which turns a
scatter-gather transaction into one DSA batch descriptor and supports the
cache-control flag. It needs my idxd fixes, of which the third part is
not posted yet. Without it patch 10 uses one descriptor per folio on
DSA.

Testing:

The series ran on an Intel CPU with DSA and two NUMA nodes, on v7.4-rc2
with the idxd driver as it is there. Four DSA devices of one node had
four shared work queues each, which gives 16 dmaengine channels. The
load was move_pages() between the two nodes, and every run compared the
data with its pattern afterwards. The comparisons below are between
the rates of the move_pages() calls:
 - 2 MiB folios, one thread: dcbm is 5-6 times as fast as the CPU
   copy, on 1, 4 and 16 channels alike
 - 4 KiB folios, one thread: dcbm is about 1/3 slower than the CPU
   copy. Small folios are better left to the CPU, as in Ravi's report
 - batches of 2 MiB and 4 KiB folios together, on 4 channels and on 1
 - 2 MiB folios with 8 and 32 threads on 16 channels: about 4 and 5
   times the rate of one thread. With 32 threads about a fifth of the
   folios found every channel busy and were copied on the CPU. With 8
   threads on one channel about half of them were
 - offloading switched off and on ten times under four threads of load,
   with another channel count each time, and the module unloaded and
   loaded again
 - anonymous and shared hugetlb folios, to surplus destinations and to
   pool pages (patch 5)
 - the page cache of an ext4 file, clean and dirtied through the
   mapping. None of its folios went through dcbm (patch 6)

No page was left behind, no data differed, and the kernel log stayed
clean. The same patches were tested on a kernel with KASAN, lockdep,
DMA-API debugging and DEBUG_ATOMIC_SLEEP, with the same result and no
report.

For patch 5 a kernel with patches 1 to 4 only ran the hugetlb part, with
the dcbm module not loaded. It moved 1 GiB of anonymous hugetlb pages to
surplus destinations about 40 times faster than a move that copies them,
and every word of it differed afterwards. With patch 5 the data remains
the same.

I did not run a provider that goes away under the held channels, and a
channel that fails or times out. The series handles neither.

[1] https://lore.kernel.org/all/20260630-shivank-batch-migrate-offload-v6-0-da95d7e8b8a2@amd.com/
[2] https://lore.kernel.org/all/20261128224616.66122-1-ravis.opensrc@gmail.com/
[3] https://lore.kernel.org/r/20261130195203.1901143-1-daniel1.byrne@intel.com
[4] https://lore.kernel.org/dmaengine/20261019150632.4026424-1-daniel1.byrne@intel.com/

Tools:

I used Claude Opus/Fable model to review (sashiko and local reviews)
the code, suggest fixes, and edit the changelogs. I also used it to
run the builds and the test rounds described above. I reviewed the
results and fixes it suggested and I am responsible for them.

Daniel Byrne (6):
  mm/migrate: give hugetlb its own bit for the content-copied marker
  mm/migrate: keep folios with private data off the batch copy
  mm/migrate: no filesystem reclaim under the batch's folio locks
  dmaengine: reintroduce DMA_MEMCPY_SG
  dmaengine: add DMA_PREP_CACHE_CONTROL
  drivers/migrate_offload: make dcbm correct on shared, out-of-order
    engines

Shivank Garg (4):
  mm/migrate: skip data copy for already-copied folios
  mm/migrate: add batch-copy path in migrate_pages_batch
  mm/migrate: add copy offload registration infrastructure
  drivers/migrate_offload: add DMA batch copy driver (dcbm)

 .../driver-api/dmaengine/provider.rst         |  10 +
 MAINTAINERS                                   |   2 +
 drivers/Kconfig                               |   2 +
 drivers/Makefile                              |   2 +
 drivers/dma/dmaengine.c                       |   1 +
 drivers/migrate_offload/Kconfig               |   9 +
 drivers/migrate_offload/Makefile              |   1 +
 drivers/migrate_offload/dcbm/Makefile         |   1 +
 drivers/migrate_offload/dcbm/dcbm.c           | 785 ++++++++++++++++++
 include/linux/dmaengine.h                     |  24 +
 include/linux/migrate.h                       |  84 ++
 include/linux/migrate_copy_offload.h          |  69 ++
 mm/Kconfig                                    |   6 +
 mm/Makefile                                   |   1 +
 mm/migrate.c                                  | 160 +++-
 mm/migrate_copy_offload.c                     | 258 ++++++
 16 files changed, 1383 insertions(+), 32 deletions(-)
 create mode 100644 drivers/migrate_offload/Kconfig
 create mode 100644 drivers/migrate_offload/Makefile
 create mode 100644 drivers/migrate_offload/dcbm/Makefile
 create mode 100644 drivers/migrate_offload/dcbm/dcbm.c
 create mode 100644 include/linux/migrate_copy_offload.h
 create mode 100644 mm/migrate_copy_offload.c


base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
-- 
2.43.0



^ permalink raw reply	[flat|nested] 14+ messages in thread

* [RFC PATCH 01/10] mm/migrate: skip data copy for already-copied folios
  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 ` Daniel Byrne
  2026-12-01 14:12 ` [RFC PATCH 02/10] mm/migrate: add batch-copy path in migrate_pages_batch Daniel Byrne
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Daniel Byrne @ 2026-12-01 14:12 UTC (permalink / raw)
  To: Shivank Garg, Ravi Jonnalagadda
  Cc: Andrew Morton, David Hildenbrand, Zi Yan, Matthew Brost,
	Joshua Hahn, Rakie Kim, Byungchul Park, Gregory Price, Ying Huang,
	Alistair Popple, Lorenzo Stoakes, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Karim Manaouil, Frank van der Linden, Teja Vojjala,
	Pravin Tamkhane, Kinsey Ho, Wei Xu, Matthew Wilcox,
	Davidlohr Bueso, Vinod Koul, Bharata B Rao, SeongJae Park,
	David Rientjes, Xuezheng Chu, Yiannis Nikolakopoulos, Dave Hansen,
	Johannes Weiner, John Hubbard, Peter Xu, Rik van Riel,
	Shakeel Butt, Tejun Heo, Fan Ni, Jonathan Cameron,
	Aneesh Kumar K.V, Nathan Lynch, Frank Li, Dan Williams, linux-mm,
	linux-kernel, Mike Day, dmaengine, Vinicius Costa Gomes, Frank Li,
	Dave Jiang, Daniel Byrne

From: Shivank Garg <shivankg@amd.com>

Add FOLIO_CONTENT_COPIED marker to the dst->migrate_info migration
state. When set, it tells __migrate_folio() to skip folio_mc_copy()
and perform metadata-only migration. This bit is not set yet, the
batch-copy path enables it later in a subsequent patch.

Update __migrate_folio_extract() to preserve FOLIO_CONTENT_COPIED
while it consumes old states of src folio.

Move the dst->migrate_info state enum to migrate.h header file so
offload drivers can see FOLIO_CONTENT_COPIED.

On 32Bit, we do not have spare bit to store FOLIO_CONTENT_COPIED
info, migration copy offload is disabled for them.

Signed-off-by: Shivank Garg <shivankg@amd.com>
[daniel: rebased to v7.4-rc2]
Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
---
Patch 1/5 of RFC v6, rebased to v7.4-rc2. No other change. It is here so
that the series applies.

 include/linux/migrate.h | 25 +++++++++++++++++++++++++
 mm/migrate.c            | 29 +++++++++++++----------------
 2 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/include/linux/migrate.h b/include/linux/migrate.h
index 78424b3824c2..28b471dfae23 100644
--- a/include/linux/migrate.h
+++ b/include/linux/migrate.h
@@ -73,6 +73,31 @@ int folio_migrate_mapping(struct address_space *mapping,
 		struct folio *newfolio, struct folio *folio, int extra_count);
 int set_movable_ops(const struct movable_operations *ops, enum pagetype type);
 
+/*
+ * To record some information during migration, we use the migrate_info
+ * field of struct folio of the newly allocated destination folio,
+ * together with the anon_vma pointer. The state is encoded in the
+ * unused low bits of the pointer.
+ * This is safe because nobody is using it except us.
+ *
+ * FOLIO_WAS_MAPPED and FOLIO_WAS_MLOCKED record the src folios's state
+ * at unmap time and are consumed during the move/undo phase.
+ * 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.
+ */
+enum {
+	FOLIO_WAS_MAPPED	= BIT(0),
+	FOLIO_WAS_MLOCKED	= BIT(1),
+	FOLIO_OLD_STATES	= FOLIO_WAS_MAPPED | FOLIO_WAS_MLOCKED,
+#ifdef CONFIG_64BIT
+	FOLIO_CONTENT_COPIED	= BIT(2),
+#else
+	/* On 32bit we do not have a spare bit, migration-copy offload is disabled. */
+	FOLIO_CONTENT_COPIED	= 0,
+#endif
+};
+
 #else
 
 static inline void putback_movable_pages(struct list_head *l) {}
diff --git a/mm/migrate.c b/mm/migrate.c
index 875079464930..ca52cd32e25e 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -871,14 +871,21 @@ 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;
 
 	/* Check whether src does not have extra refs before we do more work */
 	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;
+	}
 
 	rc = __folio_migrate_mapping(mapping, dst, src, expected_count);
 	if (rc)
@@ -1141,17 +1148,6 @@ static int move_to_new_folio(struct folio *dst, struct folio *src,
 	return rc;
 }
 
-/*
- * To record some information during migration, we use the migrate_info
- * field of struct folio of the newly allocated destination folio.
- * This is safe because nobody is using it except us.
- */
-enum {
-	FOLIO_WAS_MAPPED = BIT(0),
-	FOLIO_WAS_MLOCKED = BIT(1),
-	FOLIO_OLD_STATES = FOLIO_WAS_MAPPED | FOLIO_WAS_MLOCKED,
-};
-
 static void __migrate_folio_record(struct folio *dst,
 		int old_folio_state, struct anon_vma *anon_vma)
 {
@@ -1163,9 +1159,10 @@ static void __migrate_folio_extract(struct folio *dst,
 {
 	unsigned long info = dst->migrate_info;
 
-	*anon_vmap = (struct anon_vma *)(info & ~FOLIO_OLD_STATES);
+	*anon_vmap = (struct anon_vma *)(info & ~(FOLIO_OLD_STATES |
+						  FOLIO_CONTENT_COPIED));
 	*old_folio_state = info & FOLIO_OLD_STATES;
-	dst->migrate_info = 0;
+	dst->migrate_info &= FOLIO_CONTENT_COPIED;
 }
 
 /* Restore the source folio to the original state upon failure */
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [RFC PATCH 02/10] mm/migrate: add batch-copy path in migrate_pages_batch
  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 ` Daniel Byrne
  2026-12-01 14:12 ` [RFC PATCH 03/10] mm/migrate: add copy offload registration infrastructure Daniel Byrne
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Daniel Byrne @ 2026-12-01 14:12 UTC (permalink / raw)
  To: Shivank Garg, Ravi Jonnalagadda
  Cc: Andrew Morton, David Hildenbrand, Zi Yan, Matthew Brost,
	Joshua Hahn, Rakie Kim, Byungchul Park, Gregory Price, Ying Huang,
	Alistair Popple, Lorenzo Stoakes, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Karim Manaouil, Frank van der Linden, Teja Vojjala,
	Pravin Tamkhane, Kinsey Ho, Wei Xu, Matthew Wilcox,
	Davidlohr Bueso, Vinod Koul, Bharata B Rao, SeongJae Park,
	David Rientjes, Xuezheng Chu, Yiannis Nikolakopoulos, Dave Hansen,
	Johannes Weiner, John Hubbard, Peter Xu, Rik van Riel,
	Shakeel Butt, Tejun Heo, Fan Ni, Jonathan Cameron,
	Aneesh Kumar K.V, Nathan Lynch, Frank Li, Dan Williams, linux-mm,
	linux-kernel, Mike Day, dmaengine, Vinicius Costa Gomes, Frank Li,
	Dave Jiang, Daniel Byrne

From: Shivank Garg <shivankg@amd.com>

Add migrate_folios_mc_copy() which walks src/dst folio lists in
lockstep, copies folio content via folio_mc_copy() and marks the dst
with FOLIO_CONTENT_COPIED so the move phase in __migrate_folio() can
skip the per-folio copy. The folios_cnt parameter is unused here,
present for the offload_copy callback signature compatibility used by
later patches in the series.

Split unmapped folios into batch-eligible (unmap_batch/dst_batch) and
standard (unmap_single/dst_single) lists. A folio is batch-eligible
when the migration reason is allowed by the driver, refcount equals
expected and it's not a movable_ops page.

After TLB flush, batch copy the eligible folios via
migrate_folios_mc_copy(), then run the move phase. The
FOLIO_CONTENT_COPIED marker is consumed on first read in the
__migrate_folio, so a -EAGAIN retry falls back to fresh per-folio copy.

Signed-off-by: Shivank Garg <shivankg@amd.com>
[daniel: rebased to v7.4-rc2]
Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
---
Patch 2/5 of RFC v6, rebased to v7.4-rc2. No other change. It is here so
that the series applies.

 include/linux/migrate.h |   3 ++
 mm/migrate.c            | 107 +++++++++++++++++++++++++++++++++++-----
 2 files changed, 97 insertions(+), 13 deletions(-)

diff --git a/include/linux/migrate.h b/include/linux/migrate.h
index 28b471dfae23..8e762f9bc16f 100644
--- a/include/linux/migrate.h
+++ b/include/linux/migrate.h
@@ -72,6 +72,9 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio);
 int folio_migrate_mapping(struct address_space *mapping,
 		struct folio *newfolio, struct folio *folio, int extra_count);
 int set_movable_ops(const struct movable_operations *ops, enum pagetype type);
+int migrate_folios_mc_copy(struct list_head *dst_list,
+		struct list_head *src_list,
+		unsigned int __always_unused folios_cnt);
 
 /*
  * To record some information during migration, we use the migrate_info
diff --git a/mm/migrate.c b/mm/migrate.c
index ca52cd32e25e..db87d9a09db5 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -52,6 +52,12 @@
 #include "page_alloc.h"
 #include "swap.h"
 
+/* For now, never offload. Wired up in later patch. */
+static bool migrate_should_offload(int reason)
+{
+	return false;
+}
+
 static const struct movable_operations *offline_movable_ops;
 static const struct movable_operations *zsmalloc_movable_ops;
 
@@ -1720,6 +1726,60 @@ static int migrate_hugetlbs(struct list_head *from, new_folio_t get_new_folio,
 	return nr_failed;
 }
 
+static bool folio_can_batch_copy(struct folio *folio)
+{
+	/* movable_ops pages have a separate migration path */
+	if (unlikely(page_has_movable_ops(&folio->page)))
+		return false;
+
+	/*
+	 * Only batch when refcount matches the expected refcount.
+	 * Otherwise a GUP holder could modify src between the
+	 * batch copy and the move-phase refcount check, leaving the
+	 * dst folio with a stale copy.
+	 */
+	return folio_ref_count(folio) == folio_expected_ref_count(folio) + 1;
+}
+
+/**
+ * migrate_folios_mc_copy - Copy the contents of a list of folios.
+ * @dst_list: destination folio list.
+ * @src_list: source folio list.
+ * @folios_cnt: unused here, present for callback signature compatibility.
+ *
+ * Walk src and dst folio lists in lockstep, copy each folio via
+ * folio_mc_copy(), and mark the dst with FOLIO_CONTENT_COPIED so the
+ * move phase in __migrate_folio() skips the per-folio copy. The caller
+ * must ensure both lists have the same number of entries.
+ *
+ * On error, dst folios already copied keep their FOLIO_CONTENT_COPIED
+ * marker, while the remaining folios fallback to per-folio CPU copy in
+ * the move phase.
+ *
+ * This function may sleep.
+ *
+ * Return: 0 on success, negative errno on failure.
+ */
+int migrate_folios_mc_copy(struct list_head *dst_list,
+		struct list_head *src_list,
+		unsigned int __always_unused folios_cnt)
+{
+	struct folio *src, *dst;
+	int ret;
+
+	dst = list_first_entry(dst_list, struct folio, lru);
+	list_for_each_entry(src, src_list, lru) {
+		ret = folio_mc_copy(dst, src);
+		if (ret)
+			return ret;
+		dst->migrate_info |= FOLIO_CONTENT_COPIED;
+		dst = list_next_entry(dst, lru);
+		cond_resched();
+	}
+
+	return 0;
+}
+
 static void migrate_folios_move(struct list_head *src_folios,
 		struct list_head *dst_folios,
 		free_folio_t put_new_folio, unsigned long private,
@@ -1819,9 +1879,13 @@ static int migrate_pages_batch(struct list_head *from,
 	bool is_large = false;
 	struct folio *folio, *folio2, *dst = NULL;
 	int rc, rc_saved = 0, nr_pages;
-	LIST_HEAD(unmap_folios);
-	LIST_HEAD(dst_folios);
+	unsigned int nr_batch = 0;
+	LIST_HEAD(unmap_batch);
+	LIST_HEAD(dst_batch);
+	LIST_HEAD(unmap_single);
+	LIST_HEAD(dst_single);
 	bool nosplit = (reason == MR_NUMA_MISPLACED);
+	bool offload = migrate_should_offload(reason);
 
 	VM_WARN_ON_ONCE(mode != MIGRATE_ASYNC &&
 			!list_empty(from) && !list_is_singular(from));
@@ -1915,8 +1979,8 @@ static int migrate_pages_batch(struct list_head *from,
 					private, folio, &dst, mode, ret_folios);
 			/*
 			 * The rules are:
-			 *	0: folio will be put on unmap_folios list,
-			 *	   dst folio put on dst_folios list
+			 *	0: folio put on unmap_batch or unmap_single,
+			 *	   dst folio put on dst_batch or dst_single
 			 *	-EAGAIN: stay on the from list
 			 *	-ENOMEM: stay on the from list
 			 *	Other errno: put on ret_folios list
@@ -1957,7 +2021,7 @@ static int migrate_pages_batch(struct list_head *from,
 				/* nr_failed isn't updated for not used */
 				stats->nr_thp_failed += thp_retry;
 				rc_saved = rc;
-				if (list_empty(&unmap_folios))
+				if (list_empty(&unmap_batch) && list_empty(&unmap_single))
 					goto out;
 				else
 					goto move;
@@ -1967,8 +2031,14 @@ static int migrate_pages_batch(struct list_head *from,
 				nr_retry_pages += nr_pages;
 				break;
 			case 0:
-				list_move_tail(&folio->lru, &unmap_folios);
-				list_add_tail(&dst->lru, &dst_folios);
+				if (offload && folio_can_batch_copy(folio)) {
+					list_move_tail(&folio->lru, &unmap_batch);
+					list_add_tail(&dst->lru, &dst_batch);
+					nr_batch++;
+				} else {
+					list_move_tail(&folio->lru, &unmap_single);
+					list_add_tail(&dst->lru, &dst_single);
+				}
 				break;
 			default:
 				/*
@@ -1991,17 +2061,26 @@ static int migrate_pages_batch(struct list_head *from,
 	/* Flush TLBs for all unmapped folios */
 	try_to_unmap_flush();
 
+	/* Batch-copy eligible folios before the move phase */
+	if (!list_empty(&unmap_batch))
+		migrate_folios_mc_copy(&dst_batch, &unmap_batch, nr_batch);
+
 	retry = 1;
 	for (pass = 0; pass < nr_pass && retry; pass++) {
 		retry = 0;
 		thp_retry = 0;
 		nr_retry_pages = 0;
 
-		/* Move the unmapped folios */
-		migrate_folios_move(&unmap_folios, &dst_folios,
-				put_new_folio, private, mode, reason,
-				ret_folios, stats, &retry, &thp_retry,
-				&nr_failed, &nr_retry_pages);
+		if (!list_empty(&unmap_batch))
+			migrate_folios_move(&unmap_batch, &dst_batch, put_new_folio,
+					private, mode, reason, ret_folios, stats,
+					&retry, &thp_retry, &nr_failed,
+					&nr_retry_pages);
+		if (!list_empty(&unmap_single))
+			migrate_folios_move(&unmap_single, &dst_single, put_new_folio,
+					private, mode, reason, ret_folios, stats,
+					&retry, &thp_retry, &nr_failed,
+					&nr_retry_pages);
 	}
 	nr_failed += retry;
 	stats->nr_thp_failed += thp_retry;
@@ -2010,7 +2089,9 @@ static int migrate_pages_batch(struct list_head *from,
 	rc = rc_saved ? : nr_failed;
 out:
 	/* Cleanup remaining folios */
-	migrate_folios_undo(&unmap_folios, &dst_folios,
+	migrate_folios_undo(&unmap_batch, &dst_batch,
+			put_new_folio, private, ret_folios);
+	migrate_folios_undo(&unmap_single, &dst_single,
 			put_new_folio, private, ret_folios);
 
 	return rc;
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [RFC PATCH 03/10] mm/migrate: add copy offload registration infrastructure
  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 ` Daniel Byrne
  2026-12-01 14:12 ` [RFC PATCH 04/10] drivers/migrate_offload: add DMA batch copy driver (dcbm) Daniel Byrne
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Daniel Byrne @ 2026-12-01 14:12 UTC (permalink / raw)
  To: Shivank Garg, Ravi Jonnalagadda
  Cc: Andrew Morton, David Hildenbrand, Zi Yan, Matthew Brost,
	Joshua Hahn, Rakie Kim, Byungchul Park, Gregory Price, Ying Huang,
	Alistair Popple, Lorenzo Stoakes, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Karim Manaouil, Frank van der Linden, Teja Vojjala,
	Pravin Tamkhane, Kinsey Ho, Wei Xu, Matthew Wilcox,
	Davidlohr Bueso, Vinod Koul, Bharata B Rao, SeongJae Park,
	David Rientjes, Xuezheng Chu, Yiannis Nikolakopoulos, Dave Hansen,
	Johannes Weiner, John Hubbard, Peter Xu, Rik van Riel,
	Shakeel Butt, Tejun Heo, Fan Ni, Jonathan Cameron,
	Aneesh Kumar K.V, Nathan Lynch, Frank Li, Dan Williams, linux-mm,
	linux-kernel, Mike Day, dmaengine, Vinicius Costa Gomes, Frank Li,
	Dave Jiang, Daniel Byrne

From: Shivank Garg <shivankg@amd.com>

Add a registration interface that lets a single offload provider
(DMA, multi-threaded CPU copy, etc) take over the batch folio copy
performed by migrate_pages_batch().

The provider fills in a struct migrator with an offload_copy()
callback and calls migrate_offload_register(), passing the set of
migration reasons it wants to handle. Registration updates the
migrate_offload_batch_copy_fn() static_call and enables the
migrate_offload_enabled static branch. migrate_offload_unregister()
reverts both.

The active_reason_mask is the single source of truth for which migration
reasons are eligible for batched copy and offload. This is checked with
the migrate_should_offload(). The active reason mask can be changed at
runtime via migrate_offload_set_reason_mask(). Also, add
migrate_offload_reason_mask_parse() and
migrate_offload_reason_mask_format() so a provider can expose its reason
mask using migrate_reason_names[] (e.g. "compaction,demotion", "all",
"none").

Only one migrator can be active at a time. A second registration
gets -EBUSY, and only the active migrator can unregister itself.
The static_call dispatch is protected by SRCU so that the
synchronize_srcu() in unregister waits for all in-flight copy before
the module reference is dropped.

Callbacks must set FOLIO_CONTENT_COPIED on each successfully copied
dst folio so __migrate_folio() skips the per-folio copy; folios
without the marker fall back to CPU copy in the move phase.

Co-developed-by: Mike Day <michael.day@amd.com>
Signed-off-by: Mike Day <michael.day@amd.com>
Signed-off-by: Shivank Garg <shivankg@amd.com>
[daniel: rebased to v7.4-rc2]
Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
---
Patch 3/5 of RFC v6, rebased to v7.4-rc2. No other change. It is here so
that the series applies.

 MAINTAINERS                          |   2 +
 include/linux/migrate_copy_offload.h |  69 ++++++++
 mm/Kconfig                           |   6 +
 mm/Makefile                          |   1 +
 mm/migrate.c                         |   9 +-
 mm/migrate_copy_offload.c            | 249 +++++++++++++++++++++++++++
 6 files changed, 329 insertions(+), 7 deletions(-)
 create mode 100644 include/linux/migrate_copy_offload.h
 create mode 100644 mm/migrate_copy_offload.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 222b7fc2c3aa..c340345ff124 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -17668,9 +17668,11 @@ L:	linux-mm@kvack.org
 S:	Maintained
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/mm/core.git for-next
 F:	include/linux/migrate.h
+F:	include/linux/migrate_copy_offload.h
 F:	include/linux/migrate_mode.h
 F:	include/trace/events/migrate.h
 F:	mm/migrate.c
+F:	mm/migrate_copy_offload.c
 F:	mm/migrate_device.c
 
 MEMORY MANAGEMENT - MISC
diff --git a/include/linux/migrate_copy_offload.h b/include/linux/migrate_copy_offload.h
new file mode 100644
index 000000000000..97941ebd4979
--- /dev/null
+++ b/include/linux/migrate_copy_offload.h
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_MIGRATE_COPY_OFFLOAD_H
+#define _LINUX_MIGRATE_COPY_OFFLOAD_H
+
+#include <linux/bits.h>
+#include <linux/errno.h>
+#include <linux/migrate_mode.h>
+
+struct list_head;
+struct module;
+
+#define MIGRATOR_NAME_LEN 32
+
+/*
+ * Reasons that may ever be offloaded. A driver's mask is clamped to this.
+ * Allow all reasons by default.
+ */
+#define MIGRATE_OFFLOAD_REASONS_ALLOWED		(BIT(MR_TYPES) - 1)
+
+/**
+ * struct migrator - batch-copy provider for page migration.
+ * @name: name of the provider.
+ * @offload_copy: copy @folio_cnt folios from @src_list to @dst_list.
+ *
+ *	The migrator may inspect @folio_cnt to decide whether the batch
+ *	is worth offloading, e.g. skip when the batch is too small to
+ *	amortize setup cost.
+ *	The callback must set FOLIO_CONTENT_COPIED in dst->migrate_info
+ *	for each successfully copied destination folio.
+ *	Folios without this marker are copied via per-folio CPU copy in
+ *	the move phase.
+ *
+ * @owner: module providing the migrator. NULL for built-in (=y) drivers.
+ */
+struct migrator {
+	char name[MIGRATOR_NAME_LEN];
+	int (*offload_copy)(struct list_head *dst_list, struct list_head *src_list,
+			unsigned int folio_cnt);
+	struct module *owner;
+};
+
+#ifdef CONFIG_MIGRATION_COPY_OFFLOAD
+int migrate_offload_register(const struct migrator *m, unsigned long reason_mask);
+int migrate_offload_unregister(const struct migrator *m);
+int migrate_offload_set_reason_mask(const struct migrator *m, unsigned long mask);
+int migrate_offload_reason_mask_parse(const char *buf, unsigned long *maskp);
+int migrate_offload_reason_mask_format(char *buf, unsigned long mask);
+bool migrate_should_offload(int reason);
+int migrate_offload_batch_copy(struct list_head *dst_batch,
+		struct list_head *src_batch, unsigned int nr_batch);
+#else
+static inline int migrate_offload_register(const struct migrator *m,
+		unsigned long reason_mask) { return -EOPNOTSUPP; }
+static inline int migrate_offload_unregister(const struct migrator *m) { return -EOPNOTSUPP; }
+static inline int migrate_offload_set_reason_mask(const struct migrator *m,
+		unsigned long mask) { return -EOPNOTSUPP; }
+static inline int migrate_offload_reason_mask_parse(const char *buf,
+		unsigned long *maskp) { return -EOPNOTSUPP; }
+static inline int migrate_offload_reason_mask_format(char *buf,
+		unsigned long mask) { return -EOPNOTSUPP; }
+static inline bool migrate_should_offload(int reason) { return false; }
+static inline int migrate_offload_batch_copy(struct list_head *dst_batch,
+		struct list_head *src_batch, unsigned int nr_batch)
+{
+	return -EOPNOTSUPP;
+}
+#endif
+
+#endif /* _LINUX_MIGRATE_COPY_OFFLOAD_H */
diff --git a/mm/Kconfig b/mm/Kconfig
index afff1b25b543..b991ddaef361 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -683,6 +683,12 @@ config MIGRATION
 config DEVICE_MIGRATION
 	def_bool MIGRATION && ZONE_DEVICE
 
+# Page-migration batch-copy offload infrastructure.
+# Selected by migrator drivers (e.g. CONFIG_DCBM_DMA).
+config MIGRATION_COPY_OFFLOAD
+	bool
+	depends on MIGRATION && 64BIT
+
 config ARCH_ENABLE_HUGEPAGE_MIGRATION
 	bool
 
diff --git a/mm/Makefile b/mm/Makefile
index 2a3ec53d62ee..e57dfc365638 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -97,6 +97,7 @@ obj-$(CONFIG_FAILSLAB) += failslab.o
 obj-$(CONFIG_FAIL_PAGE_ALLOC) += fail_page_alloc.o
 obj-$(CONFIG_MEMTEST)		+= memtest.o
 obj-$(CONFIG_MIGRATION) += migrate.o
+obj-$(CONFIG_MIGRATION_COPY_OFFLOAD) += migrate_copy_offload.o
 obj-$(CONFIG_NUMA) += memory-tiers.o
 obj-$(CONFIG_DEVICE_MIGRATION) += migrate_device.o
 obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += huge_memory.o khugepaged.o
diff --git a/mm/migrate.c b/mm/migrate.c
index db87d9a09db5..531d54b2185e 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -43,6 +43,7 @@
 #include <linux/sched/sysctl.h>
 #include <linux/memory-tiers.h>
 #include <linux/pagewalk.h>
+#include <linux/migrate_copy_offload.h>
 
 #include <asm/tlbflush.h>
 
@@ -52,12 +53,6 @@
 #include "page_alloc.h"
 #include "swap.h"
 
-/* For now, never offload. Wired up in later patch. */
-static bool migrate_should_offload(int reason)
-{
-	return false;
-}
-
 static const struct movable_operations *offline_movable_ops;
 static const struct movable_operations *zsmalloc_movable_ops;
 
@@ -2063,7 +2058,7 @@ static int migrate_pages_batch(struct list_head *from,
 
 	/* Batch-copy eligible folios before the move phase */
 	if (!list_empty(&unmap_batch))
-		migrate_folios_mc_copy(&dst_batch, &unmap_batch, nr_batch);
+		migrate_offload_batch_copy(&dst_batch, &unmap_batch, nr_batch);
 
 	retry = 1;
 	for (pass = 0; pass < nr_pass && retry; pass++) {
diff --git a/mm/migrate_copy_offload.c b/mm/migrate_copy_offload.c
new file mode 100644
index 000000000000..335db8e80556
--- /dev/null
+++ b/mm/migrate_copy_offload.c
@@ -0,0 +1,249 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bitops.h>
+#include <linux/jump_label.h>
+#include <linux/kstrtox.h>
+#include <linux/migrate.h>
+#include <linux/migrate_copy_offload.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/srcu.h>
+#include <linux/static_call.h>
+#include <linux/string.h>
+#include <linux/sysfs.h>
+
+DEFINE_STATIC_KEY_FALSE(migrate_offload_enabled);
+DEFINE_SRCU(migrate_offload_srcu);
+DEFINE_STATIC_CALL(migrate_offload_batch_copy_fn, migrate_folios_mc_copy);
+
+static DEFINE_MUTEX(migrator_mutex);
+static const struct migrator *active_migrator;
+
+/*
+ * The active migrator's reason mask. This is the single source of truth
+ * for which reasons are offloaded.
+ */
+static unsigned long active_reason_mask;
+
+bool migrate_should_offload(int reason)
+{
+	if (!static_branch_unlikely(&migrate_offload_enabled))
+		return false;
+
+	return READ_ONCE(active_reason_mask) & BIT(reason);
+}
+
+/**
+ * migrate_offload_reason_mask_parse - parse migration reason mask.
+ * @buf: input string, either a number (hex/decimal, e.g. "0x101") or a
+ *	comma-separated list of reason names (see migrate_reason_names[]),
+ *	e.g. "compaction,demotion". The tokens "all" and "none" select
+ *	every or no reason.
+ * @maskp: parsed mask, clamped to MIGRATE_OFFLOAD_REASONS_ALLOWED.
+ *
+ * Return: 0 on success, -EINVAL on an unknown name, -ENOMEM on OOM.
+ */
+int migrate_offload_reason_mask_parse(const char *buf, unsigned long *maskp)
+{
+	unsigned long mask;
+	char *copy, *p, *tok;
+	int ret = 0;
+
+	/* A plain number is treated as a raw mask. */
+	if (!kstrtoul(buf, 0, &mask))
+		goto done;
+
+	copy = kstrdup(buf, GFP_KERNEL);
+	if (!copy)
+		return -ENOMEM;
+	mask = 0;
+	p = strim(copy);
+
+	while ((tok = strsep(&p, ",")) != NULL) {
+		int i;
+
+		tok = strim(tok);
+		if (!*tok)
+			continue;
+		if (!strcmp(tok, "none")) {
+			mask = 0;
+			continue;
+		}
+		if (!strcmp(tok, "all")) {
+			mask = MIGRATE_OFFLOAD_REASONS_ALLOWED;
+			continue;
+		}
+		i = match_string(migrate_reason_names, MR_TYPES, tok);
+		if (i < 0) {
+			ret = -EINVAL;
+			break;
+		}
+		mask |= BIT(i);
+	}
+	kfree(copy);
+	if (ret)
+		return ret;
+done:
+	*maskp = mask & MIGRATE_OFFLOAD_REASONS_ALLOWED;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(migrate_offload_reason_mask_parse);
+
+/**
+ * migrate_offload_reason_mask_format - render a reason mask as names.
+ * @buf: output buffer from kernel_param_ops get.
+ * @mask: reason mask to render.
+ *
+ * Emits a comma-separated list of reason names, or "none".
+ *
+ * Return: number of bytes written to @buf.
+ */
+int migrate_offload_reason_mask_format(char *buf, unsigned long mask)
+{
+	int len = 0, i;
+
+	for_each_set_bit(i, &mask, MR_TYPES)
+		len += sysfs_emit_at(buf, len, "%s%s",
+				     len ? "," : "", migrate_reason_names[i]);
+	if (!len)
+		return sysfs_emit(buf, "none\n");
+
+	len += sysfs_emit_at(buf, len, "\n");
+	return len;
+}
+EXPORT_SYMBOL_GPL(migrate_offload_reason_mask_format);
+
+/*
+ * Hand the batch to the registered migrator. The migrator may decline
+ * (typically based on batch size), in which case the move phase falls
+ * back to per-folio CPU copy.
+ */
+int migrate_offload_batch_copy(struct list_head *dst_batch,
+		struct list_head *src_batch, unsigned int nr_batch)
+{
+	int idx, rc;
+
+	idx = srcu_read_lock(&migrate_offload_srcu);
+	rc = static_call(migrate_offload_batch_copy_fn)(dst_batch, src_batch, nr_batch);
+	srcu_read_unlock(&migrate_offload_srcu, idx);
+	return rc;
+}
+
+/**
+ * migrate_offload_set_reason_mask - update the active migrator's reason mask.
+ * @m: migrator (must be the currently active one).
+ * @mask: new reason mask.
+ *
+ * Return: 0 on success, -EINVAL if @m is NULL or not the active migrator.
+ */
+int migrate_offload_set_reason_mask(const struct migrator *m, unsigned long mask)
+{
+	if (!m)
+		return -EINVAL;
+
+	mask &= MIGRATE_OFFLOAD_REASONS_ALLOWED;
+
+	mutex_lock(&migrator_mutex);
+	if (active_migrator != m) {
+		mutex_unlock(&migrator_mutex);
+		return -EINVAL;
+	}
+	WRITE_ONCE(active_reason_mask, mask);
+	mutex_unlock(&migrator_mutex);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(migrate_offload_set_reason_mask);
+
+/**
+ * migrate_offload_register - register a batch-copy provider for page migration.
+ * @m: migrator to install.
+ * @reason_mask: initial set of BIT(MR_*) reasons to offload, can be changed
+ *	later via migrate_offload_set_reason_mask().
+ *
+ * Only one provider can be active at a time, returns -EBUSY if another migrator
+ * is already registered.
+ *
+ * Return: 0 on success, negative errno on failure.
+ */
+int migrate_offload_register(const struct migrator *m, unsigned long reason_mask)
+{
+	unsigned long mask;
+	int ret = 0;
+
+	if (!m || !m->offload_copy)
+		return -EINVAL;
+
+	mask = reason_mask & MIGRATE_OFFLOAD_REASONS_ALLOWED;
+
+	mutex_lock(&migrator_mutex);
+	if (active_migrator) {
+		ret = -EBUSY;
+		goto unlock;
+	}
+
+	/* @owner is NULL for built-in (=y) drivers; nothing to ref. */
+	if (m->owner && !try_module_get(m->owner)) {
+		ret = -ENODEV;
+		goto unlock;
+	}
+
+	WRITE_ONCE(active_reason_mask, mask);
+	static_call_update(migrate_offload_batch_copy_fn, m->offload_copy);
+	active_migrator = m;
+	static_branch_enable(&migrate_offload_enabled);
+
+unlock:
+	mutex_unlock(&migrator_mutex);
+
+	if (ret)
+		pr_err("migrate_offload: %s: failed to register (%d)\n", m->name, ret);
+	else
+		pr_info("migrate_offload: enabled by %s (reason_mask=0x%lx)\n",
+			m->name, mask);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(migrate_offload_register);
+
+/**
+ * migrate_offload_unregister - unregister the active batch-copy provider.
+ * @m: migrator to remove (must be the currently active one).
+ *
+ * Reverts static_call targets and waits for SRCU grace period so that
+ * no in-flight migration is still calling the driver functions before
+ * releasing the module.
+ *
+ * Return: 0 on success, negative errno on failure.
+ */
+int migrate_offload_unregister(const struct migrator *m)
+{
+	struct module *owner;
+
+	if (!m)
+		return -EINVAL;
+
+	mutex_lock(&migrator_mutex);
+	if (active_migrator != m) {
+		mutex_unlock(&migrator_mutex);
+		return -EINVAL;
+	}
+
+	/*
+	 * Disable the static branch first so new migrate_pages_batch() calls
+	 * cannot enter the batch path.
+	 */
+	static_branch_disable(&migrate_offload_enabled);
+	WRITE_ONCE(active_reason_mask, 0);
+	static_call_update(migrate_offload_batch_copy_fn, migrate_folios_mc_copy);
+	owner = active_migrator->owner;
+	active_migrator = NULL;
+	mutex_unlock(&migrator_mutex);
+
+	/* Wait for all in-flight callers to finish before module_put(). */
+	synchronize_srcu(&migrate_offload_srcu);
+	if (owner)
+		module_put(owner);
+
+	pr_info("migrate_offload: disabled by %s\n", m->name);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(migrate_offload_unregister);
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [RFC PATCH 04/10] drivers/migrate_offload: add DMA batch copy driver (dcbm)
  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
                   ` (2 preceding siblings ...)
  2026-12-01 14:12 ` [RFC PATCH 03/10] mm/migrate: add copy offload registration infrastructure Daniel Byrne
@ 2026-12-01 14:12 ` Daniel Byrne
  2026-12-01 14:12 ` [RFC PATCH 05/10] mm/migrate: give hugetlb its own bit for the content-copied marker Daniel Byrne
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Daniel Byrne @ 2026-12-01 14:12 UTC (permalink / raw)
  To: Shivank Garg, Ravi Jonnalagadda
  Cc: Andrew Morton, David Hildenbrand, Zi Yan, Matthew Brost,
	Joshua Hahn, Rakie Kim, Byungchul Park, Gregory Price, Ying Huang,
	Alistair Popple, Lorenzo Stoakes, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Karim Manaouil, Frank van der Linden, Teja Vojjala,
	Pravin Tamkhane, Kinsey Ho, Wei Xu, Matthew Wilcox,
	Davidlohr Bueso, Vinod Koul, Bharata B Rao, SeongJae Park,
	David Rientjes, Xuezheng Chu, Yiannis Nikolakopoulos, Dave Hansen,
	Johannes Weiner, John Hubbard, Peter Xu, Rik van Riel,
	Shakeel Butt, Tejun Heo, Fan Ni, Jonathan Cameron,
	Aneesh Kumar K.V, Nathan Lynch, Frank Li, Dan Williams, linux-mm,
	linux-kernel, Mike Day, dmaengine, Vinicius Costa Gomes, Frank Li,
	Dave Jiang, Daniel Byrne

From: Shivank Garg <shivankg@amd.com>

Add a simple DMAEngine-based migrator that plugs into the page
migration copy-offload infrastructure and batch-copies folios via
DMA memcpy channels. It is intended for testing the offload plumbing
and as a template for future migrators (SDXI, multi-threaded CPU copy,
etc.).

On success, the dst folios are marked with FOLIO_CONTENT_COPIED so
__migrate_folio skips the per-folio copy. On any DMA error,
FOLIO_CONTENT_COPIED market is not set and the migration falls back
to per-folio CPU copy.

dcbm registers with reason mask with reason mask
MIGRATE_OFFLOAD_REASONS_ALLOWED, this set can be narrowed at runtime
via the reason_mask parameter.

Runtime controls are under /sys/module/dcbm/parameters/:

  offloading      - enable/disable DMA offload (0/1)
  nr_dma_chan     - max DMA channels to use (1..N)
  reason_mask     - reasons to offload: names (e.g. "compaction,demotion"),
		    "all"/"none" or raw numerical mask.
  folios_migrated - folios DMA-copied (write to reset)
  folios_failures - fallback count (write to reset)

CONFIG_DCBM_DMA selects MIGRATION_COPY_OFFLOAD so enabling the
driver pulls in the infrastructure automatically.

Channel acquisition uses dma_request_chan_by_mask(DMA_MEMCPY), which
works for providers that set DMA_PRIVATE (e.g. AMD PTDMA). Generic
mem-to-mem engines that do not set DMA_PRIVATE (e.g. SDXI) should
acquire channels via dma_find_channel(DMA_MEMCPY) or the async_tx
APIs, which can be added in a follow-up.

[Correctness Note:
 Karim: Descriptor can complete out of order for some dmaengines.
 so descriptor chaining may yield incorrect results. For correctness
 we need to add callback to every descriptor
 https://lore.kernel.org/all/20260619160725.lfcxrbj5go67qy6u@wrangler
 Shivank: device_prep_dma_memcpy_sg() would solve this.
]

Reviewed-by: Karim Manaouil <kmanaouil.dev@gmail.com>
Signed-off-by: Shivank Garg <shivankg@amd.com>
[daniel: rebased to v7.4-rc2]
Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
---
Patch 4/5 of RFC v6, rebased to v7.4-rc2. No other change. It is here so
that the series applies.

 drivers/Kconfig                       |   2 +
 drivers/Makefile                      |   2 +
 drivers/migrate_offload/Kconfig       |   9 +
 drivers/migrate_offload/Makefile      |   1 +
 drivers/migrate_offload/dcbm/Makefile |   1 +
 drivers/migrate_offload/dcbm/dcbm.c   | 481 ++++++++++++++++++++++++++
 6 files changed, 496 insertions(+)
 create mode 100644 drivers/migrate_offload/Kconfig
 create mode 100644 drivers/migrate_offload/Makefile
 create mode 100644 drivers/migrate_offload/dcbm/Makefile
 create mode 100644 drivers/migrate_offload/dcbm/dcbm.c

diff --git a/drivers/Kconfig b/drivers/Kconfig
index f2bed2ddeb66..3e83a1475cbc 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -253,4 +253,6 @@ source "drivers/cdx/Kconfig"
 
 source "drivers/resctrl/Kconfig"
 
+source "drivers/migrate_offload/Kconfig"
+
 endmenu
diff --git a/drivers/Makefile b/drivers/Makefile
index 0841ea851847..88cb8e3e88df 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -42,6 +42,8 @@ obj-y				+= clk/
 # really early.
 obj-$(CONFIG_DMADEVICES)	+= dma/
 
+obj-$(CONFIG_MIGRATION_COPY_OFFLOAD)	+= migrate_offload/
+
 # SOC specific infrastructure drivers.
 obj-y				+= soc/
 obj-$(CONFIG_PM_GENERIC_DOMAINS)	+= pmdomain/
diff --git a/drivers/migrate_offload/Kconfig b/drivers/migrate_offload/Kconfig
new file mode 100644
index 000000000000..c9f2e21a95cf
--- /dev/null
+++ b/drivers/migrate_offload/Kconfig
@@ -0,0 +1,9 @@
+config DCBM_DMA
+	tristate "DMA Core Batch Migrator"
+	depends on MIGRATION && DMA_ENGINE && 64BIT
+	select MIGRATION_COPY_OFFLOAD
+	help
+	  DMA-based batch copy engine for page migration. Uses
+	  DMAEngine memcpy channels to offload folio data copies
+	  during migration. Primarily intended for testing the copy
+	  offload infrastructure.
diff --git a/drivers/migrate_offload/Makefile b/drivers/migrate_offload/Makefile
new file mode 100644
index 000000000000..9e16018beb15
--- /dev/null
+++ b/drivers/migrate_offload/Makefile
@@ -0,0 +1 @@
+obj-$(CONFIG_DCBM_DMA)		+= dcbm/
diff --git a/drivers/migrate_offload/dcbm/Makefile b/drivers/migrate_offload/dcbm/Makefile
new file mode 100644
index 000000000000..56ba47cce0f1
--- /dev/null
+++ b/drivers/migrate_offload/dcbm/Makefile
@@ -0,0 +1 @@
+obj-$(CONFIG_DCBM_DMA) += dcbm.o
diff --git a/drivers/migrate_offload/dcbm/dcbm.c b/drivers/migrate_offload/dcbm/dcbm.c
new file mode 100644
index 000000000000..bacbcc689171
--- /dev/null
+++ b/drivers/migrate_offload/dcbm/dcbm.c
@@ -0,0 +1,481 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * DMA Core Batch Migrator (DCBM)
+ *
+ * Uses DMAEngine memcpy channels to offload batch folio copies during
+ * page migration. Reference driver meant for testing the offload
+ * infrastructure.
+ *
+ * Copyright (C) 2024-26 Advanced Micro Devices, Inc.
+ */
+
+#include <linux/module.h>
+#include <linux/dma-mapping.h>
+#include <linux/dmaengine.h>
+#include <linux/migrate.h>
+#include <linux/migrate_copy_offload.h>
+
+#define MAX_DMA_CHANNELS	16
+
+static atomic_long_t folios_migrated;
+static atomic_long_t folios_failures;
+
+static bool offloading_enabled;
+static unsigned int nr_dma_channels = 1;
+static DEFINE_MUTEX(dcbm_mutex);
+
+struct dma_work {
+	struct dma_chan *chan;
+	struct completion done;
+	atomic_t pending;
+	struct sg_table *src_sgt;
+	struct sg_table *dst_sgt;
+	bool mapped;
+};
+
+static void dma_completion_callback(void *data)
+{
+	struct dma_work *work = data;
+
+	if (atomic_dec_and_test(&work->pending))
+		complete(&work->done);
+}
+
+static int setup_sg_tables(struct dma_work *work, struct list_head **src_pos,
+			   struct list_head **dst_pos, int nr)
+{
+	struct scatterlist *sg_src, *sg_dst;
+	struct device *dev;
+	int i, ret;
+
+	work->src_sgt = kmalloc_obj(*work->src_sgt, GFP_KERNEL);
+	if (!work->src_sgt)
+		return -ENOMEM;
+	work->dst_sgt = kmalloc_obj(*work->dst_sgt, GFP_KERNEL);
+	if (!work->dst_sgt) {
+		ret = -ENOMEM;
+		goto err_free_src;
+	}
+
+	ret = sg_alloc_table(work->src_sgt, nr, GFP_KERNEL);
+	if (ret)
+		goto err_free_dst;
+	ret = sg_alloc_table(work->dst_sgt, nr, GFP_KERNEL);
+	if (ret)
+		goto err_free_src_table;
+
+	sg_src = work->src_sgt->sgl;
+	sg_dst = work->dst_sgt->sgl;
+	for (i = 0; i < nr; i++) {
+		struct folio *src = list_entry(*src_pos, struct folio, lru);
+		struct folio *dst = list_entry(*dst_pos, struct folio, lru);
+
+		sg_set_folio(sg_src, src, folio_size(src), 0);
+		sg_set_folio(sg_dst, dst, folio_size(dst), 0);
+
+		*src_pos = (*src_pos)->next;
+		*dst_pos = (*dst_pos)->next;
+
+		if (i < nr - 1) {
+			sg_src = sg_next(sg_src);
+			sg_dst = sg_next(sg_dst);
+		}
+	}
+
+	dev = dmaengine_get_dma_device(work->chan);
+	if (!dev) {
+		ret = -ENODEV;
+		goto err_free_dst_table;
+	}
+	ret = dma_map_sgtable(dev, work->src_sgt, DMA_TO_DEVICE,
+			      DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_NO_KERNEL_MAPPING);
+	if (ret)
+		goto err_free_dst_table;
+	ret = dma_map_sgtable(dev, work->dst_sgt, DMA_FROM_DEVICE,
+			      DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_NO_KERNEL_MAPPING);
+	if (ret)
+		goto err_unmap_src;
+
+	/*
+	 * TODO: IOMMU may merge segments unevenly on the two sides, fall back
+	 * bail to CPU copy. In practice, I have not observed merging in tests.
+	 * Handling unequal nents is left for follow-up.
+	 */
+	if (work->src_sgt->nents != work->dst_sgt->nents) {
+		ret = -EINVAL;
+		goto err_unmap_dst;
+	}
+	work->mapped = true;
+	return 0;
+
+err_unmap_dst:
+	dma_unmap_sgtable(dev, work->dst_sgt, DMA_FROM_DEVICE,
+			  DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_NO_KERNEL_MAPPING);
+err_unmap_src:
+	dma_unmap_sgtable(dev, work->src_sgt, DMA_TO_DEVICE,
+			  DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_NO_KERNEL_MAPPING);
+err_free_dst_table:
+	sg_free_table(work->dst_sgt);
+err_free_src_table:
+	sg_free_table(work->src_sgt);
+err_free_dst:
+	kfree(work->dst_sgt);
+	work->dst_sgt = NULL;
+err_free_src:
+	kfree(work->src_sgt);
+	work->src_sgt = NULL;
+	return ret;
+}
+
+static void cleanup_dma_work(struct dma_work *works, int actual_channels)
+{
+	struct device *dev;
+	int i;
+
+	if (!works)
+		return;
+
+	for (i = 0; i < actual_channels; i++) {
+		if (!works[i].chan)
+			continue;
+
+		dev = dmaengine_get_dma_device(works[i].chan);
+
+		if (works[i].mapped)
+			dmaengine_terminate_sync(works[i].chan);
+
+		if (dev && works[i].mapped) {
+			if (works[i].src_sgt) {
+				dma_unmap_sgtable(dev, works[i].src_sgt,
+						  DMA_TO_DEVICE,
+						  DMA_ATTR_SKIP_CPU_SYNC |
+						  DMA_ATTR_NO_KERNEL_MAPPING);
+				sg_free_table(works[i].src_sgt);
+				kfree(works[i].src_sgt);
+			}
+			if (works[i].dst_sgt) {
+				dma_unmap_sgtable(dev, works[i].dst_sgt,
+						  DMA_FROM_DEVICE,
+						  DMA_ATTR_SKIP_CPU_SYNC |
+						  DMA_ATTR_NO_KERNEL_MAPPING);
+				sg_free_table(works[i].dst_sgt);
+				kfree(works[i].dst_sgt);
+			}
+		}
+		dma_release_channel(works[i].chan);
+	}
+	kfree(works);
+}
+
+static int submit_dma_transfers(struct dma_work *work)
+{
+	struct scatterlist *sg_src, *sg_dst;
+	struct dma_async_tx_descriptor *tx;
+	unsigned long flags = DMA_CTRL_ACK;
+	dma_cookie_t cookie;
+	int i;
+
+	atomic_set(&work->pending, 1);
+
+	sg_src = work->src_sgt->sgl;
+	sg_dst = work->dst_sgt->sgl;
+	for_each_sgtable_dma_sg(work->src_sgt, sg_src, i) {
+		if (i == work->src_sgt->nents - 1)
+			flags |= DMA_PREP_INTERRUPT;
+
+		tx = dmaengine_prep_dma_memcpy(work->chan,
+					       sg_dma_address(sg_dst),
+					       sg_dma_address(sg_src),
+					       sg_dma_len(sg_src), flags);
+		if (!tx) {
+			atomic_set(&work->pending, 0);
+			return -EIO;
+		}
+
+		if (i == work->src_sgt->nents - 1) {
+			tx->callback = dma_completion_callback;
+			tx->callback_param = work;
+		}
+
+		cookie = dmaengine_submit(tx);
+		if (dma_submit_error(cookie)) {
+			atomic_set(&work->pending, 0);
+			return -EIO;
+		}
+		sg_dst = sg_next(sg_dst);
+	}
+	return 0;
+}
+
+/**
+ * folios_copy_dma - copy a batch of folios via DMA memcpy
+ * @dst_list: destination folio list
+ * @src_list: source folio list
+ * @nr_folios: number of folios in each list
+ *
+ * Return: 0 on success, negative errno on failure.
+ */
+static int folios_copy_dma(struct list_head *dst_list,
+			   struct list_head *src_list, unsigned int nr_folios)
+{
+	struct folio *dst;
+	struct dma_work *works;
+	struct list_head *src_pos = src_list->next;
+	struct list_head *dst_pos = dst_list->next;
+	int i, folios_per_chan, ret;
+	dma_cap_mask_t mask;
+	int actual_channels = 0;
+	unsigned int max_channels;
+
+	max_channels = min3(READ_ONCE(nr_dma_channels), nr_folios,
+			    (unsigned int)MAX_DMA_CHANNELS);
+
+	works = kcalloc(max_channels, sizeof(*works), GFP_KERNEL);
+	if (!works)
+		return -ENOMEM;
+
+	dma_cap_zero(mask);
+	dma_cap_set(DMA_MEMCPY, mask);
+
+	for (i = 0; i < max_channels; i++) {
+		works[actual_channels].chan = dma_request_chan_by_mask(&mask);
+		if (IS_ERR(works[actual_channels].chan))
+			break;
+		init_completion(&works[actual_channels].done);
+		actual_channels++;
+	}
+
+	if (actual_channels == 0) {
+		kfree(works);
+		return -ENODEV;
+	}
+
+	for (i = 0; i < actual_channels; i++) {
+		folios_per_chan = nr_folios * (i + 1) / actual_channels -
+				(nr_folios * i) / actual_channels;
+		if (folios_per_chan == 0)
+			continue;
+
+		ret = setup_sg_tables(&works[i], &src_pos, &dst_pos,
+				      folios_per_chan);
+		if (ret)
+			goto err_cleanup;
+	}
+
+	for (i = 0; i < actual_channels; i++) {
+		if (!works[i].mapped)
+			continue;
+		ret = submit_dma_transfers(&works[i]);
+		if (ret)
+			goto err_cleanup;
+	}
+
+	for (i = 0; i < actual_channels; i++) {
+		if (atomic_read(&works[i].pending) > 0)
+			dma_async_issue_pending(works[i].chan);
+	}
+
+	for (i = 0; i < actual_channels; i++) {
+		if (atomic_read(&works[i].pending) == 0)
+			continue;
+		if (!wait_for_completion_timeout(&works[i].done,
+						 msecs_to_jiffies(10000))) {
+			ret = -ETIMEDOUT;
+			goto err_cleanup;
+		}
+	}
+
+	/*
+	 * All folios copied; mark each dst with FOLIO_CONTENT_COPIED so
+	 * __migrate_folio() skips the per-folio copy in the move phase.
+	 */
+	list_for_each_entry(dst, dst_list, lru)
+		dst->migrate_info |= FOLIO_CONTENT_COPIED;
+
+	cleanup_dma_work(works, actual_channels);
+
+	atomic_long_add(nr_folios, &folios_migrated);
+	return 0;
+
+err_cleanup:
+	pr_warn_ratelimited("dcbm: DMA copy failed (%d), falling back to CPU\n",
+			    ret);
+	cleanup_dma_work(works, actual_channels);
+
+	atomic_long_add(nr_folios, &folios_failures);
+	return ret;
+}
+
+static const struct migrator dma_migrator = {
+	.name = "DCBM",
+	.offload_copy = folios_copy_dma,
+	.owner = THIS_MODULE,
+};
+
+static unsigned long dcbm_reason_mask = MIGRATE_OFFLOAD_REASONS_ALLOWED;
+
+/* offloading: enable/disable DMA migration offload */
+static int offloading_param_set(const char *val, const struct kernel_param *kp)
+{
+	bool enable;
+	int ret;
+
+	ret = kstrtobool(val, &enable);
+	if (ret)
+		return ret;
+
+	mutex_lock(&dcbm_mutex);
+	if (enable == offloading_enabled) {
+		mutex_unlock(&dcbm_mutex);
+		return 0;
+	}
+	if (enable) {
+		ret = migrate_offload_register(&dma_migrator,
+					       READ_ONCE(dcbm_reason_mask));
+		if (ret) {
+			mutex_unlock(&dcbm_mutex);
+			return ret;
+		}
+		WRITE_ONCE(offloading_enabled, true);
+	} else {
+		migrate_offload_unregister(&dma_migrator);
+		WRITE_ONCE(offloading_enabled, false);
+	}
+	mutex_unlock(&dcbm_mutex);
+	return 0;
+}
+
+static int offloading_param_get(char *buffer, const struct kernel_param *kp)
+{
+	return sysfs_emit(buffer, "%d\n", READ_ONCE(offloading_enabled));
+}
+
+static const struct kernel_param_ops offloading_param_ops = {
+	.set = offloading_param_set,
+	.get = offloading_param_get,
+};
+module_param_cb(offloading, &offloading_param_ops, NULL, 0644);
+MODULE_PARM_DESC(offloading, "Enable DMA migration offload (0/1)");
+
+/* nr_dma_chan: max DMA channels to use per batch */
+static int nr_dma_chan_param_set(const char *val, const struct kernel_param *kp)
+{
+	unsigned int new_val;
+	int ret;
+
+	ret = kstrtouint(val, 0, &new_val);
+	if (ret)
+		return ret;
+	if (new_val < 1 || new_val > MAX_DMA_CHANNELS)
+		return -EINVAL;
+
+	mutex_lock(&dcbm_mutex);
+	WRITE_ONCE(nr_dma_channels, new_val);
+	mutex_unlock(&dcbm_mutex);
+	return 0;
+}
+
+static int nr_dma_chan_param_get(char *buffer, const struct kernel_param *kp)
+{
+	return sysfs_emit(buffer, "%u\n", READ_ONCE(nr_dma_channels));
+}
+
+static const struct kernel_param_ops nr_dma_chan_param_ops = {
+	.set = nr_dma_chan_param_set,
+	.get = nr_dma_chan_param_get,
+};
+module_param_cb(nr_dma_chan, &nr_dma_chan_param_ops, NULL, 0644);
+MODULE_PARM_DESC(nr_dma_chan, "Max DMA channels to use (1..16)");
+
+/* reason_mask: set of MR_* reasons this migrator handles */
+static int reason_mask_param_set(const char *val, const struct kernel_param *kp)
+{
+	unsigned long mask;
+	int ret;
+
+	ret = migrate_offload_reason_mask_parse(val, &mask);
+	if (ret)
+		return ret;
+
+	mutex_lock(&dcbm_mutex);
+	WRITE_ONCE(dcbm_reason_mask, mask);
+	if (offloading_enabled)
+		migrate_offload_set_reason_mask(&dma_migrator, mask);
+	mutex_unlock(&dcbm_mutex);
+	return 0;
+}
+
+static int reason_mask_param_get(char *buffer, const struct kernel_param *kp)
+{
+	return migrate_offload_reason_mask_format(buffer, READ_ONCE(dcbm_reason_mask));
+}
+
+static const struct kernel_param_ops reason_mask_param_ops = {
+	.set = reason_mask_param_set,
+	.get = reason_mask_param_get,
+};
+module_param_cb(reason_mask, &reason_mask_param_ops, NULL, 0644);
+MODULE_PARM_DESC(reason_mask,
+		 "Reasons to offload: comma-separated names (e.g. compaction,demotion), 'all', 'none', or a raw hex mask");
+
+/* folios_migrated / folios_failures: counters; any write resets to 0 */
+static int folios_migrated_param_set(const char *val, const struct kernel_param *kp)
+{
+	atomic_long_set(&folios_migrated, 0);
+	return 0;
+}
+
+static int folios_migrated_param_get(char *buffer, const struct kernel_param *kp)
+{
+	return sysfs_emit(buffer, "%ld\n", atomic_long_read(&folios_migrated));
+}
+
+static const struct kernel_param_ops folios_migrated_param_ops = {
+	.set = folios_migrated_param_set,
+	.get = folios_migrated_param_get,
+};
+module_param_cb(folios_migrated, &folios_migrated_param_ops, NULL, 0644);
+MODULE_PARM_DESC(folios_migrated, "Folios DMA-copied (write to reset)");
+
+static int folios_failures_param_set(const char *val, const struct kernel_param *kp)
+{
+	atomic_long_set(&folios_failures, 0);
+	return 0;
+}
+
+static int folios_failures_param_get(char *buffer, const struct kernel_param *kp)
+{
+	return sysfs_emit(buffer, "%ld\n", atomic_long_read(&folios_failures));
+}
+
+static const struct kernel_param_ops folios_failures_param_ops = {
+	.set = folios_failures_param_set,
+	.get = folios_failures_param_get,
+};
+module_param_cb(folios_failures, &folios_failures_param_ops, NULL, 0644);
+MODULE_PARM_DESC(folios_failures, "DMA-copy failure count (write to reset)");
+
+static int __init dcbm_init(void)
+{
+	pr_info("dcbm: DMA Core Batch Migrator initialized\n");
+	return 0;
+}
+
+static void __exit dcbm_exit(void)
+{
+	mutex_lock(&dcbm_mutex);
+	if (offloading_enabled) {
+		migrate_offload_unregister(&dma_migrator);
+		offloading_enabled = false;
+	}
+	mutex_unlock(&dcbm_mutex);
+
+	pr_info("dcbm: DMA Core Batch Migrator unloaded\n");
+}
+
+module_init(dcbm_init);
+module_exit(dcbm_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Shivank Garg");
+MODULE_DESCRIPTION("DMA Core Batch Migrator");
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [RFC PATCH 05/10] mm/migrate: give hugetlb its own bit for the content-copied marker
  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
                   ` (3 preceding siblings ...)
  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
  2026-12-01 14:12 ` [RFC PATCH 06/10] mm/migrate: keep folios with private data off the batch copy Daniel Byrne
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Daniel Byrne @ 2026-12-01 14:12 UTC (permalink / raw)
  To: Shivank Garg, Ravi Jonnalagadda
  Cc: Andrew Morton, David Hildenbrand, Zi Yan, Matthew Brost,
	Joshua Hahn, Rakie Kim, Byungchul Park, Gregory Price, Ying Huang,
	Alistair Popple, Lorenzo Stoakes, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Karim Manaouil, Frank van der Linden, Teja Vojjala,
	Pravin Tamkhane, Kinsey Ho, Wei Xu, Matthew Wilcox,
	Davidlohr Bueso, Vinod Koul, Bharata B Rao, SeongJae Park,
	David Rientjes, Xuezheng Chu, Yiannis Nikolakopoulos, Dave Hansen,
	Johannes Weiner, John Hubbard, Peter Xu, Rik van Riel,
	Shakeel Butt, Tejun Heo, Fan Ni, Jonathan Cameron,
	Aneesh Kumar K.V, Nathan Lynch, Frank Li, Dan Williams, linux-mm,
	linux-kernel, Mike Day, dmaengine, Vinicius Costa Gomes, Frank Li,
	Dave Jiang, Daniel Byrne

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



^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [RFC PATCH 06/10] mm/migrate: keep folios with private data off the batch copy
  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
                   ` (4 preceding siblings ...)
  2026-12-01 14:12 ` [RFC PATCH 05/10] mm/migrate: give hugetlb its own bit for the content-copied marker Daniel Byrne
@ 2026-12-01 14:12 ` Daniel Byrne
  2026-12-01 14:12 ` [RFC PATCH 07/10] mm/migrate: no filesystem reclaim under the batch's folio locks Daniel Byrne
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Daniel Byrne @ 2026-12-01 14:12 UTC (permalink / raw)
  To: Shivank Garg, Ravi Jonnalagadda
  Cc: Andrew Morton, David Hildenbrand, Zi Yan, Matthew Brost,
	Joshua Hahn, Rakie Kim, Byungchul Park, Gregory Price, Ying Huang,
	Alistair Popple, Lorenzo Stoakes, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Karim Manaouil, Frank van der Linden, Teja Vojjala,
	Pravin Tamkhane, Kinsey Ho, Wei Xu, Matthew Wilcox,
	Davidlohr Bueso, Vinod Koul, Bharata B Rao, SeongJae Park,
	David Rientjes, Xuezheng Chu, Yiannis Nikolakopoulos, Dave Hansen,
	Johannes Weiner, John Hubbard, Peter Xu, Rik van Riel,
	Shakeel Butt, Tejun Heo, Fan Ni, Jonathan Cameron,
	Aneesh Kumar K.V, Nathan Lynch, Frank Li, Dan Williams, linux-mm,
	linux-kernel, Mike Day, dmaengine, Vinicius Costa Gomes, Frank Li,
	Dave Jiang, Daniel Byrne

The batch path copies a folio after it is unmapped and before the move
phase. Buffer heads are written through without a folio reference, and
__buffer_migrate_folio() keeps such writers out only in the move phase,
after a batched copy has already run. The destination can then miss a
write. Other users of folio->private have migrate_folio() callbacks with
checks of their own, and a copy made ahead of them bypasses those too.

Refuse the batch copy for a folio that has private data attached, and
for one whose mapping migrates through a callback other than
migrate_folio(), filemap_migrate_folio() and the two buffer head ones.
Such a folio takes the per-folio copy in the move phase, as before the
batch path.

This is a fix to "mm/migrate: add batch-copy path in
migrate_pages_batch" and is meant to be folded into it.

Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
---
A fix to patch 2. For folding into v7.

 mm/migrate.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/mm/migrate.c b/mm/migrate.c
index 79970170a53a..e584c3776618 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -1722,10 +1722,34 @@ static int migrate_hugetlbs(struct list_head *from, new_folio_t get_new_folio,
 
 static bool folio_can_batch_copy(struct folio *folio)
 {
+	struct address_space *mapping;
+
 	/* movable_ops pages have a separate migration path */
 	if (unlikely(page_has_movable_ops(&folio->page)))
 		return false;
 
+	/*
+	 * Buffer heads are written through without a folio reference, and
+	 * __buffer_migrate_folio() keeps such writers out only in the move
+	 * phase, after a batched copy would already have run. Other users
+	 * of folio->private have migrate_folio() callbacks with checks of
+	 * their own that a copy made ahead of them would bypass as well.
+	 */
+	if (folio_has_attached_private(folio))
+		return false;
+
+	mapping = folio_mapping(folio);
+	if (mapping) {
+		const struct address_space_operations *a_ops = mapping->a_ops;
+
+		if (a_ops->migrate_folio &&
+		    a_ops->migrate_folio != migrate_folio &&
+		    a_ops->migrate_folio != filemap_migrate_folio &&
+		    a_ops->migrate_folio != buffer_migrate_folio &&
+		    a_ops->migrate_folio != buffer_migrate_folio_norefs)
+			return false;
+	}
+
 	/*
 	 * Only batch when refcount matches the expected refcount.
 	 * Otherwise a GUP holder could modify src between the
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [RFC PATCH 07/10] mm/migrate: no filesystem reclaim under the batch's folio locks
  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
                   ` (5 preceding siblings ...)
  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 ` Daniel Byrne
  2026-12-01 14:12 ` [RFC PATCH 08/10] dmaengine: reintroduce DMA_MEMCPY_SG Daniel Byrne
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Daniel Byrne @ 2026-12-01 14:12 UTC (permalink / raw)
  To: Shivank Garg, Ravi Jonnalagadda
  Cc: Andrew Morton, David Hildenbrand, Zi Yan, Matthew Brost,
	Joshua Hahn, Rakie Kim, Byungchul Park, Gregory Price, Ying Huang,
	Alistair Popple, Lorenzo Stoakes, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Karim Manaouil, Frank van der Linden, Teja Vojjala,
	Pravin Tamkhane, Kinsey Ho, Wei Xu, Matthew Wilcox,
	Davidlohr Bueso, Vinod Koul, Bharata B Rao, SeongJae Park,
	David Rientjes, Xuezheng Chu, Yiannis Nikolakopoulos, Dave Hansen,
	Johannes Weiner, John Hubbard, Peter Xu, Rik van Riel,
	Shakeel Butt, Tejun Heo, Fan Ni, Jonathan Cameron,
	Aneesh Kumar K.V, Nathan Lynch, Frank Li, Dan Williams, linux-mm,
	linux-kernel, Mike Day, dmaengine, Vinicius Costa Gomes, Frank Li,
	Dave Jiang, Daniel Byrne

migrate_offload_batch_copy() calls the provider with the folios of the
batch locked. A provider may allocate there. An allocation that enters
filesystem reclaim can prune an inode whose truncate then waits for one
of those folios, and the migration waits for the allocation.

Call the provider in a memalloc_nofs_save() scope.

This is a fix to "mm/migrate: add copy offload registration
infrastructure" and is meant to be folded into it.

Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
---
A fix to patch 3. For folding into v7.

 mm/migrate_copy_offload.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/mm/migrate_copy_offload.c b/mm/migrate_copy_offload.c
index 335db8e80556..3d0784a8f047 100644
--- a/mm/migrate_copy_offload.c
+++ b/mm/migrate_copy_offload.c
@@ -6,6 +6,7 @@
 #include <linux/migrate_copy_offload.h>
 #include <linux/mm.h>
 #include <linux/module.h>
+#include <linux/sched/mm.h>
 #include <linux/slab.h>
 #include <linux/srcu.h>
 #include <linux/static_call.h>
@@ -121,11 +122,19 @@ EXPORT_SYMBOL_GPL(migrate_offload_reason_mask_format);
 int migrate_offload_batch_copy(struct list_head *dst_batch,
 		struct list_head *src_batch, unsigned int nr_batch)
 {
+	unsigned int nofs;
 	int idx, rc;
 
+	/*
+	 * The batch's folios are locked. An allocation of the provider's
+	 * that reclaims could prune an inode whose truncate then waits for
+	 * one of them: no filesystem reclaim from here.
+	 */
+	nofs = memalloc_nofs_save();
 	idx = srcu_read_lock(&migrate_offload_srcu);
 	rc = static_call(migrate_offload_batch_copy_fn)(dst_batch, src_batch, nr_batch);
 	srcu_read_unlock(&migrate_offload_srcu, idx);
+	memalloc_nofs_restore(nofs);
 	return rc;
 }
 
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [RFC PATCH 08/10] dmaengine: reintroduce DMA_MEMCPY_SG
  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
                   ` (6 preceding siblings ...)
  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 ` 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
                   ` (2 subsequent siblings)
  10 siblings, 1 reply; 14+ messages in thread
From: Daniel Byrne @ 2026-12-01 14:12 UTC (permalink / raw)
  To: Shivank Garg, Ravi Jonnalagadda
  Cc: Andrew Morton, David Hildenbrand, Zi Yan, Matthew Brost,
	Joshua Hahn, Rakie Kim, Byungchul Park, Gregory Price, Ying Huang,
	Alistair Popple, Lorenzo Stoakes, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Karim Manaouil, Frank van der Linden, Teja Vojjala,
	Pravin Tamkhane, Kinsey Ho, Wei Xu, Matthew Wilcox,
	Davidlohr Bueso, Vinod Koul, Bharata B Rao, SeongJae Park,
	David Rientjes, Xuezheng Chu, Yiannis Nikolakopoulos, Dave Hansen,
	Johannes Weiner, John Hubbard, Peter Xu, Rik van Riel,
	Shakeel Butt, Tejun Heo, Fan Ni, Jonathan Cameron,
	Aneesh Kumar K.V, Nathan Lynch, Frank Li, Dan Williams, linux-mm,
	linux-kernel, Mike Day, dmaengine, Vinicius Costa Gomes, Frank Li,
	Dave Jiang, Daniel Byrne

Reintroduce the DMA_MEMCPY_SG transaction type and
device_prep_dma_memcpy_sg(), removed by commit 0cae04373b77 ("dmaengine:
remove DMA_MEMCPY_SG once again") when the last in-tree user went away.
The migration copy offload added later in this series is the new
user. It hands the provider a scatter/gather memcpy in one call so
that batch-capable engines such as DSA can turn the operation into a
single hardware batch descriptor. The transfer copies min(src, dst)
total bytes.

Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
---
The cover letter of RFC v6 lists this hook as a follow-up. This is the
dmaengine core part. No provider implements it in this series.

 .../driver-api/dmaengine/provider.rst         | 10 ++++++++++
 drivers/dma/dmaengine.c                       |  1 +
 include/linux/dmaengine.h                     | 20 +++++++++++++++++++
 3 files changed, 31 insertions(+)

diff --git a/Documentation/driver-api/dmaengine/provider.rst b/Documentation/driver-api/dmaengine/provider.rst
index be47852e5a0f..e844c7c0fce0 100644
--- a/Documentation/driver-api/dmaengine/provider.rst
+++ b/Documentation/driver-api/dmaengine/provider.rst
@@ -162,6 +162,16 @@ Currently, the types available are:
 
   - The device is able to do memory to memory copies
 
+- DMA_MEMCPY_SG
+
+  - The device supports memory to memory scatter-gather transfers.
+
+  - Even though a plain memcpy can look like a particular case of a
+    scatter-gather transfer, with a single chunk to copy, it's a distinct
+    transaction type in the mem2mem transfer case. This is because some very
+    simple devices might be able to do contiguous single-chunk memory copies,
+    but have no support for more complex SG transfers.
+
   - No matter what the overall size of the combined chunks for source and
     destination is, only as many bytes as the smallest of the two will be
     transmitted. That means the number and size of the scatter-gather buffers in
diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c
index 34be53487a21..bed2a80cb538 100644
--- a/drivers/dma/dmaengine.c
+++ b/drivers/dma/dmaengine.c
@@ -1219,6 +1219,7 @@ int dma_async_device_register(struct dma_device *device)
 }
 
 	CHECK_CAP(dma_memcpy,      DMA_MEMCPY);
+	CHECK_CAP(dma_memcpy_sg,   DMA_MEMCPY_SG);
 	CHECK_CAP(dma_xor,         DMA_XOR);
 	CHECK_CAP(dma_xor_val,     DMA_XOR_VAL);
 	CHECK_CAP(dma_pq,          DMA_PQ);
diff --git a/include/linux/dmaengine.h b/include/linux/dmaengine.h
index 805668fbe437..5df4bfc000a6 100644
--- a/include/linux/dmaengine.h
+++ b/include/linux/dmaengine.h
@@ -50,6 +50,7 @@ enum dma_status {
  */
 enum dma_transaction_type {
 	DMA_MEMCPY,
+	DMA_MEMCPY_SG,
 	DMA_XOR,
 	DMA_PQ,
 	DMA_XOR_VAL,
@@ -860,6 +861,7 @@ struct dma_filter {
  * @device_router_config: optional callback for DMA router configuration
  * @device_free_chan_resources: release DMA channel's resources
  * @device_prep_dma_memcpy: prepares a memcpy operation
+ * @device_prep_dma_memcpy_sg: prepares a memcpy_sg operation
  * @device_prep_dma_xor: prepares a xor operation
  * @device_prep_dma_xor_val: prepares a xor validation operation
  * @device_prep_dma_pq: prepares a pq operation
@@ -940,6 +942,11 @@ struct dma_device {
 	struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)(
 		struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
 		size_t len, unsigned long flags);
+	struct dma_async_tx_descriptor *(*device_prep_dma_memcpy_sg)(
+		struct dma_chan *chan,
+		struct scatterlist *dst_sg, unsigned int dst_nents,
+		struct scatterlist *src_sg, unsigned int src_nents,
+		unsigned long flags);
 	struct dma_async_tx_descriptor *(*device_prep_dma_xor)(
 		struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
 		unsigned int src_cnt, size_t len, unsigned long flags);
@@ -1246,6 +1253,19 @@ static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memcpy(
 						    len, flags);
 }
 
+static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memcpy_sg(
+		struct dma_chan *chan,
+		struct scatterlist *dst_sg, unsigned int dst_nents,
+		struct scatterlist *src_sg, unsigned int src_nents,
+		unsigned long flags)
+{
+	if (!chan || !chan->device || !chan->device->device_prep_dma_memcpy_sg)
+		return NULL;
+
+	return chan->device->device_prep_dma_memcpy_sg(chan, dst_sg, dst_nents,
+						       src_sg, src_nents, flags);
+}
+
 /**
  * dmaengine_get_max_xor - get the maximum number of XOR sources for a channel
  * @chan: DMA channel
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [RFC PATCH 09/10] dmaengine: add DMA_PREP_CACHE_CONTROL
  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
                   ` (7 preceding siblings ...)
  2026-12-01 14:12 ` [RFC PATCH 08/10] dmaengine: reintroduce DMA_MEMCPY_SG Daniel Byrne
@ 2026-12-01 14:12 ` 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
  10 siblings, 0 replies; 14+ messages in thread
From: Daniel Byrne @ 2026-12-01 14:12 UTC (permalink / raw)
  To: Shivank Garg, Ravi Jonnalagadda
  Cc: Andrew Morton, David Hildenbrand, Zi Yan, Matthew Brost,
	Joshua Hahn, Rakie Kim, Byungchul Park, Gregory Price, Ying Huang,
	Alistair Popple, Lorenzo Stoakes, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Karim Manaouil, Frank van der Linden, Teja Vojjala,
	Pravin Tamkhane, Kinsey Ho, Wei Xu, Matthew Wilcox,
	Davidlohr Bueso, Vinod Koul, Bharata B Rao, SeongJae Park,
	David Rientjes, Xuezheng Chu, Yiannis Nikolakopoulos, Dave Hansen,
	Johannes Weiner, John Hubbard, Peter Xu, Rik van Riel,
	Shakeel Butt, Tejun Heo, Fan Ni, Jonathan Cameron,
	Aneesh Kumar K.V, Nathan Lynch, Frank Li, Dan Williams, linux-mm,
	linux-kernel, Mike Day, dmaengine, Vinicius Costa Gomes, Frank Li,
	Dave Jiang, Daniel Byrne

Let clients request the provider's cache-control hint per descriptor.
On DSA, IDXD_OP_FLAG_CC steers the copied data toward the LLC on
write. This helps a consumer that reads the data immediately and
hurts one that does not. Since the right setting is workload
dependent, we expose it as a prep flag instead of hard-coding it in
the driver.

The flag is advisory, and no provider acts on it yet. The idxd side,
which maps it to IDXD_OP_FLAG_CC on MEMMOVE descriptors, needs idxd
changes that are not posted yet.

Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
---
No provider acts on the flag in this series.

 include/linux/dmaengine.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/include/linux/dmaengine.h b/include/linux/dmaengine.h
index 5df4bfc000a6..0b9235bd493a 100644
--- a/include/linux/dmaengine.h
+++ b/include/linux/dmaengine.h
@@ -201,6 +201,9 @@ struct dma_vec {
  *  transaction is marked with DMA_PREP_REPEAT will cause the new transaction
  *  to never be processed and stay in the issued queue forever. The flag is
  *  ignored if the previous transaction is not a repeated transaction.
+ * @DMA_PREP_CACHE_CONTROL: tell the driver to direct destination writes into
+ *  cache (cache-allocating) rather than bypassing it. The flag is advisory
+ *  and drivers that cannot honour it will ignore it.
  */
 enum dma_ctrl_flags {
 	DMA_PREP_INTERRUPT = (1 << 0),
@@ -213,6 +216,7 @@ enum dma_ctrl_flags {
 	DMA_PREP_CMD = (1 << 7),
 	DMA_PREP_REPEAT = (1 << 8),
 	DMA_PREP_LOAD_EOT = (1 << 9),
+	DMA_PREP_CACHE_CONTROL = (1 << 10),
 };
 
 /**
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [RFC PATCH 10/10] drivers/migrate_offload: make dcbm correct on shared, out-of-order engines
  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
                   ` (8 preceding siblings ...)
  2026-12-01 14:12 ` [RFC PATCH 09/10] dmaengine: add DMA_PREP_CACHE_CONTROL Daniel Byrne
@ 2026-12-01 14:12 ` 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
  10 siblings, 0 replies; 14+ messages in thread
From: Daniel Byrne @ 2026-12-01 14:12 UTC (permalink / raw)
  To: Shivank Garg, Ravi Jonnalagadda
  Cc: Andrew Morton, David Hildenbrand, Zi Yan, Matthew Brost,
	Joshua Hahn, Rakie Kim, Byungchul Park, Gregory Price, Ying Huang,
	Alistair Popple, Lorenzo Stoakes, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Karim Manaouil, Frank van der Linden, Teja Vojjala,
	Pravin Tamkhane, Kinsey Ho, Wei Xu, Matthew Wilcox,
	Davidlohr Bueso, Vinod Koul, Bharata B Rao, SeongJae Park,
	David Rientjes, Xuezheng Chu, Yiannis Nikolakopoulos, Dave Hansen,
	Johannes Weiner, John Hubbard, Peter Xu, Rik van Riel,
	Shakeel Butt, Tejun Heo, Fan Ni, Jonathan Cameron,
	Aneesh Kumar K.V, Nathan Lynch, Frank Li, Dan Williams, linux-mm,
	linux-kernel, Mike Day, dmaengine, Vinicius Costa Gomes, Frank Li,
	Dave Jiang, Daniel Byrne

dcbm set a callback on the last descriptor submitted to each channel,
requested its channels on every batch, mapped scatterlists whose
segments had to merge identically on both sides, timed out after ten
seconds and terminated the channel, and submitted more descriptors than
a work queue holds. Each of these is wrong on Intel DSA.

Completion. A work queue served by a group with several engines
completes descriptors out of order, so the last descriptor's callback
does not mean the slice has landed, and the batch could be marked
FOLIO_CONTENT_COPIED early. Give every descriptor its own completion,
counted down on a per-slice pending counter that starts with a
submission reference. Use the result-aware callback and fail the slice
on any error result, so a rejected or aborted descriptor sends its
folio to the CPU copy instead of being reported as copied.

Channels. Acquire the channels once when offloading is enabled, group
them by DMA device with the device's NUMA node, and try groups on the
destination node first: a cross-socket copy pays remote link bandwidth
on every written line. Concurrent batches share the pool through
per-channel trylocks; when every channel is busy the batch is refused
with -EBUSY (counted in batches_refused) and the move phase copies on
the CPU instead of queueing.

Mapping. A folio is physically contiguous and so one DMA segment. Map
source and destination of each copy with dma_map_page_attrs() and pair
them by folio, which removes the scatterlist allocations and the
IOMMU merge asymmetry that forced CPU fallbacks. On a provider that
advertises DMA_MEMCPY_SG, hand the slice over as scatter-gather
transactions of up to sg_elems (default 32) folios, which DSA executes
as one hardware batch descriptor; the scatterlists only carry the
per-folio DMA addresses.

Waiting. A timeout cannot be acted upon safely: the in-flight
descriptors reference the folios and the on-stack work, and dmaengine
has no per-descriptor abort. Wait for the completion; a stuck engine is
the device driver's to recover, and it completes the descriptors with
an error. Never terminate a channel: terminate_all is channel-wide and
kills other batches' descriptors.

Throttling. A work queue owns a fixed descriptor pool and the
non-blocking prep returns NULL when it is exhausted. Submit at most
max_inflight (default 32) descriptors per channel and wait for
completions before queueing more.

Request DMA_PREP_CACHE_CONTROL on the copies, so the first access after
remapping hits the LLC instead of DRAM; cache_ctrl turns it off.

Reported-by: Karim Manaouil <kmanaouil.dev@gmail.com>
Link: https://lore.kernel.org/linux-mm/20260619160725.lfcxrbj5go67qy6u@wrangler/
Suggested-by: Vinod Koul <vkoul@kernel.org>
Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
---
The idxd side of DMA_MEMCPY_SG is not in this series, so on DSA this
uses one descriptor per folio here.

 drivers/migrate_offload/dcbm/dcbm.c | 630 +++++++++++++++++++++-------
 1 file changed, 467 insertions(+), 163 deletions(-)

diff --git a/drivers/migrate_offload/dcbm/dcbm.c b/drivers/migrate_offload/dcbm/dcbm.c
index 3df9232586ff..03785a822161 100644
--- a/drivers/migrate_offload/dcbm/dcbm.c
+++ b/drivers/migrate_offload/dcbm/dcbm.c
@@ -9,127 +9,290 @@
  * Copyright (C) 2024-26 Advanced Micro Devices, Inc.
  */
 
+#include <linux/bitops.h>
 #include <linux/module.h>
 #include <linux/dma-mapping.h>
 #include <linux/dmaengine.h>
 #include <linux/migrate.h>
 #include <linux/migrate_copy_offload.h>
+#include <linux/mutex.h>
+#include <linux/scatterlist.h>
+#include <linux/slab.h>
+#include <linux/wait.h>
 
 #define MAX_DMA_CHANNELS	16
+/*
+ * Folios per scatter-gather transaction. A provider that supports
+ * DMA_MEMCPY_SG turns one transaction into one hardware batch, so
+ * this bounds the batch element count it must accept.
+ */
+#define DCBM_SG_ELEMS_DEFAULT	32
+/*
+ * Descriptors in flight per channel. A DSA work queue owns a fixed
+ * descriptor pool (its queue depth, 64 in a typical configuration)
+ * and prep returns NULL once it is exhausted, so a slice submits at
+ * most this many descriptors before waiting for completions.
+ */
+#define DCBM_MAX_INFLIGHT_DEFAULT	32
 
 static atomic_long_t folios_migrated;
 static atomic_long_t folios_failures;
+static atomic_long_t batches_refused;
 
 static bool offloading_enabled;
 static unsigned int nr_dma_channels = 1;
+static unsigned int sg_elems = DCBM_SG_ELEMS_DEFAULT;
+static unsigned int max_inflight = DCBM_MAX_INFLIGHT_DEFAULT;
+static bool cache_ctrl = true;
 static DEFINE_MUTEX(dcbm_mutex);
 
+/*
+ * Channels are acquired once when offloading is enabled and shared by
+ * concurrent batches through per-channel trylocks. When every channel
+ * is busy the batch is refused and the move phase copies on the CPU,
+ * instead of queueing behind other batches.
+ */
+static struct dcbm_chan {
+	struct dma_chan *chan;
+	struct mutex lock;
+} channels[MAX_DMA_CHANNELS];
+static unsigned int nr_channels;
+
+/*
+ * Channels grouped by DMA device. One batch maps its folios against a
+ * single device and uses only that group's channels. Concurrent
+ * batches rotate over the groups so every device contributes.
+ */
+static struct dcbm_group {
+	struct device *dev;
+	int node;
+	unsigned int first;
+	unsigned int nr;
+} groups[MAX_DMA_CHANNELS];
+static unsigned int nr_groups;
+static atomic_t group_cursor;
+
+struct dcbm_copy {
+	dma_addr_t src;
+	dma_addr_t dst;
+	size_t len;
+};
+
 struct dma_work {
 	struct dma_chan *chan;
-	struct completion done;
+	struct device *dev;
+	wait_queue_head_t waitq;
 	atomic_t pending;
-	struct sg_table *src_sgt;
-	struct sg_table *dst_sgt;
-	bool mapped;
+	atomic_t error;
+	struct dcbm_copy *copies;
+	unsigned int nr_copies;
+	bool submitted;
 };
 
-static void dma_completion_callback(void *data)
+/*
+ * Every descriptor carries its own completion. Engines such as Intel
+ * DSA complete the descriptors of one channel out of order when the
+ * work queue is served by several engines, so a callback on the last
+ * submitted descriptor does not mean the earlier ones have landed.
+ * The pending count starts at one for the submitter, so the work
+ * cannot complete before every descriptor has been submitted.
+ *
+ * A descriptor the hardware rejected or aborted completes with an
+ * error result; it must fail the batch, or a folio that was never
+ * written would be reported as copied.
+ */
+static void dma_completion_callback(void *data,
+				    const struct dmaengine_result *result)
 {
 	struct dma_work *work = data;
 
-	if (atomic_dec_and_test(&work->pending))
-		complete(&work->done);
+	if (!result || result->result != DMA_TRANS_NOERROR)
+		atomic_set(&work->error, -EIO);
+
+	atomic_dec(&work->pending);
+	wake_up(&work->waitq);
 }
 
-static int setup_sg_tables(struct dma_work *work, struct list_head **src_pos,
-			   struct list_head **dst_pos, int nr)
+static void dma_work_init(struct dma_work *work)
 {
-	struct scatterlist *sg_src, *sg_dst;
-	struct device *dev;
-	int i, ret;
+	init_waitqueue_head(&work->waitq);
+	/* Submission reference, dropped by dma_work_done_submitting(). */
+	atomic_set(&work->pending, 1);
+	atomic_set(&work->error, 0);
+}
 
-	work->src_sgt = kmalloc_obj(*work->src_sgt, GFP_KERNEL);
-	if (!work->src_sgt)
-		return -ENOMEM;
-	work->dst_sgt = kmalloc_obj(*work->dst_sgt, GFP_KERNEL);
-	if (!work->dst_sgt) {
-		ret = -ENOMEM;
-		goto err_free_src;
+static void dma_work_done_submitting(struct dma_work *work)
+{
+	atomic_dec(&work->pending);
+	wake_up(&work->waitq);
+}
+
+/*
+ * Throttle submission to max_inflight descriptors per channel, so a
+ * slice never outruns the channel's descriptor pool.
+ */
+static void dma_work_throttle(struct dma_work *work)
+{
+	unsigned int limit = READ_ONCE(max_inflight) + 1;
+
+	wait_event(work->waitq, atomic_read(&work->pending) < limit);
+}
+
+static unsigned long dcbm_claim_group(struct dcbm_group *grp,
+				      unsigned int want)
+{
+	unsigned long mask = 0;
+	unsigned int i, got = 0;
+
+	for (i = 0; i < grp->nr && got < want; i++) {
+		unsigned int idx = grp->first + i;
+
+		if (mutex_trylock(&channels[idx].lock)) {
+			mask |= BIT(idx);
+			got++;
+		}
 	}
+	return mask;
+}
 
-	ret = sg_alloc_table(work->src_sgt, nr, GFP_KERNEL);
-	if (ret)
-		goto err_free_dst;
-	ret = sg_alloc_table(work->dst_sgt, nr, GFP_KERNEL);
-	if (ret)
-		goto err_free_src_table;
+/*
+ * One selection pass: collect the groups whose node does (or does
+ * not) match @nid and try them starting from a rotating offset within
+ * that selection, so rotation is fair however the eligible groups are
+ * laid out in the global array.
+ */
+static unsigned long dcbm_claim_pass(int nid, bool match_node,
+				     unsigned int want,
+				     struct dcbm_group **grpp)
+{
+	unsigned int sel[MAX_DMA_CHANNELS];
+	unsigned int n = 0, g, i;
+
+	for (g = 0; g < nr_groups; g++)
+		if ((groups[g].node == nid) == match_node)
+			sel[n++] = g;
+	if (!n)
+		return 0;
+
+	g = (unsigned int)atomic_inc_return(&group_cursor) % n;
+	for (i = 0; i < n; i++) {
+		struct dcbm_group *grp = &groups[sel[(g + i) % n]];
+		unsigned long mask = dcbm_claim_group(grp, want);
+
+		if (mask) {
+			*grpp = grp;
+			return mask;
+		}
+	}
+	return 0;
+}
+
+/*
+ * Claim up to @want channels from one device group. Groups on the
+ * destination node are tried first: a cross-socket copy pays
+ * remote-link bandwidth on every written line plus a remote
+ * completion interrupt, so locality beats spreading. The first group
+ * with any free channel wins. If every channel of every group is busy
+ * the batch is refused.
+ */
+static unsigned long dcbm_claim_channels(unsigned int want, int nid,
+					 struct dcbm_group **grpp)
+{
+	unsigned long mask = dcbm_claim_pass(nid, true, want, grpp);
+
+	if (!mask)
+		mask = dcbm_claim_pass(nid, false, want, grpp);
+	return mask;
+}
+
+static void dcbm_release_channels(unsigned long mask)
+{
+	unsigned int i;
+
+	for_each_set_bit(i, &mask, nr_channels)
+		mutex_unlock(&channels[i].lock);
+}
+
+/*
+ * Map each folio of the slice on its own. A folio is physically
+ * contiguous, so it is one DMA segment, and the source and
+ * destination of a copy pair up by folio rather than by whatever
+ * segments an IOMMU would merge two scatterlists into.
+ */
+static int map_folios(struct dma_work *work, struct list_head **src_pos,
+		      struct list_head **dst_pos, unsigned int nr)
+{
+	struct device *dev = work->dev;
+	unsigned int i;
+
+	work->copies = kcalloc(nr, sizeof(*work->copies), GFP_KERNEL);
+	if (!work->copies)
+		return -ENOMEM;
 
-	sg_src = work->src_sgt->sgl;
-	sg_dst = work->dst_sgt->sgl;
 	for (i = 0; i < nr; i++) {
 		struct folio *src = list_entry(*src_pos, struct folio, lru);
 		struct folio *dst = list_entry(*dst_pos, struct folio, lru);
-
-		sg_set_folio(sg_src, src, folio_size(src), 0);
-		sg_set_folio(sg_dst, dst, folio_size(dst), 0);
+		struct dcbm_copy *copy = &work->copies[i];
+
+		copy->len = folio_size(src);
+		copy->src = dma_map_page_attrs(dev, folio_page(src, 0), 0,
+					       copy->len, DMA_TO_DEVICE, 0);
+		if (dma_mapping_error(dev, copy->src))
+			goto err;
+		copy->dst = dma_map_page_attrs(dev, folio_page(dst, 0), 0,
+					       copy->len, DMA_FROM_DEVICE, 0);
+		if (dma_mapping_error(dev, copy->dst)) {
+			dma_unmap_page_attrs(dev, copy->src, copy->len,
+					     DMA_TO_DEVICE, 0);
+			goto err;
+		}
+		work->nr_copies++;
 
 		*src_pos = (*src_pos)->next;
 		*dst_pos = (*dst_pos)->next;
-
-		if (i < nr - 1) {
-			sg_src = sg_next(sg_src);
-			sg_dst = sg_next(sg_dst);
-		}
 	}
+	return 0;
+err:
+	/* The mapped prefix is undone by cleanup_dma_work(). */
+	return -EIO;
+}
 
-	dev = dmaengine_get_dma_device(work->chan);
-	if (!dev) {
-		ret = -ENODEV;
-		goto err_free_dst_table;
-	}
-	ret = dma_map_sgtable(dev, work->src_sgt, DMA_TO_DEVICE,
-			      DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_NO_KERNEL_MAPPING);
-	if (ret)
-		goto err_free_dst_table;
-	ret = dma_map_sgtable(dev, work->dst_sgt, DMA_FROM_DEVICE,
-			      DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_NO_KERNEL_MAPPING);
-	if (ret)
-		goto err_unmap_src;
+static void unmap_folios(struct dma_work *work)
+{
+	unsigned int i;
 
-	/*
-	 * TODO: IOMMU may merge segments unevenly on the two sides, fall back
-	 * bail to CPU copy. In practice, I have not observed merging in tests.
-	 * Handling unequal nents is left for follow-up.
-	 */
-	if (work->src_sgt->nents != work->dst_sgt->nents) {
-		ret = -EINVAL;
-		goto err_unmap_dst;
+	for (i = 0; i < work->nr_copies; i++) {
+		struct dcbm_copy *copy = &work->copies[i];
+
+		dma_unmap_page_attrs(work->dev, copy->dst, copy->len,
+				     DMA_FROM_DEVICE, 0);
+		dma_unmap_page_attrs(work->dev, copy->src, copy->len,
+				     DMA_TO_DEVICE, 0);
 	}
-	work->mapped = true;
-	return 0;
+	work->nr_copies = 0;
+	kfree(work->copies);
+	work->copies = NULL;
+}
 
-err_unmap_dst:
-	dma_unmap_sgtable(dev, work->dst_sgt, DMA_FROM_DEVICE,
-			  DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_NO_KERNEL_MAPPING);
-err_unmap_src:
-	dma_unmap_sgtable(dev, work->src_sgt, DMA_TO_DEVICE,
-			  DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_NO_KERNEL_MAPPING);
-err_free_dst_table:
-	sg_free_table(work->dst_sgt);
-err_free_src_table:
-	sg_free_table(work->src_sgt);
-err_free_dst:
-	kfree(work->dst_sgt);
-	work->dst_sgt = NULL;
-err_free_src:
-	kfree(work->src_sgt);
-	work->src_sgt = NULL;
-	return ret;
+/*
+ * Wait for every descriptor of a slice that was handed to the engine.
+ * Nothing is ever terminated. The descriptors reference the folio
+ * mappings and the on-stack work until they complete, dmaengine has
+ * no per-descriptor abort, and a terminate ends every transaction that
+ * is out on the channel, whoever submitted it, and calls none of them
+ * back. Anything submitted runs to completion, and a failure only
+ * decides what the caller does afterwards.
+ */
+static int dma_work_wait(struct dma_work *work)
+{
+	if (!work->submitted)
+		return 0;
+	wait_event(work->waitq, !atomic_read(&work->pending));
+	return atomic_read(&work->error);
 }
 
 static void cleanup_dma_work(struct dma_work *works, int actual_channels)
 {
-	struct device *dev;
 	int i;
 
 	if (!works)
@@ -138,71 +301,121 @@ static void cleanup_dma_work(struct dma_work *works, int actual_channels)
 	for (i = 0; i < actual_channels; i++) {
 		if (!works[i].chan)
 			continue;
-
-		dev = dmaengine_get_dma_device(works[i].chan);
-
-		if (works[i].mapped)
-			dmaengine_terminate_sync(works[i].chan);
-
-		if (dev && works[i].mapped) {
-			if (works[i].src_sgt) {
-				dma_unmap_sgtable(dev, works[i].src_sgt,
-						  DMA_TO_DEVICE,
-						  DMA_ATTR_SKIP_CPU_SYNC |
-						  DMA_ATTR_NO_KERNEL_MAPPING);
-				sg_free_table(works[i].src_sgt);
-				kfree(works[i].src_sgt);
-			}
-			if (works[i].dst_sgt) {
-				dma_unmap_sgtable(dev, works[i].dst_sgt,
-						  DMA_FROM_DEVICE,
-						  DMA_ATTR_SKIP_CPU_SYNC |
-						  DMA_ATTR_NO_KERNEL_MAPPING);
-				sg_free_table(works[i].dst_sgt);
-				kfree(works[i].dst_sgt);
-			}
-		}
-		dma_release_channel(works[i].chan);
+		unmap_folios(&works[i]);
 	}
 	kfree(works);
 }
 
-static int submit_dma_transfers(struct dma_work *work)
+static int submit_one(struct dma_work *work, struct dma_async_tx_descriptor *tx)
 {
-	struct scatterlist *sg_src, *sg_dst;
-	struct dma_async_tx_descriptor *tx;
-	unsigned long flags = DMA_CTRL_ACK;
 	dma_cookie_t cookie;
-	int i;
 
-	atomic_set(&work->pending, 1);
+	tx->callback_result = dma_completion_callback;
+	tx->callback_param = work;
+	atomic_inc(&work->pending);
 
-	sg_src = work->src_sgt->sgl;
-	sg_dst = work->dst_sgt->sgl;
-	for_each_sgtable_dma_sg(work->src_sgt, sg_src, i) {
-		if (i == work->src_sgt->nents - 1)
-			flags |= DMA_PREP_INTERRUPT;
+	cookie = dmaengine_submit(tx);
+	if (dma_submit_error(cookie)) {
+		atomic_dec(&work->pending);
+		return -EIO;
+	}
+	/*
+	 * Start it now. The throttle sleeps for completions before the
+	 * next descriptor is prepared, and on a provider whose submit only
+	 * queues (a virt-dma driver such as ptdma) nothing completes until
+	 * issue_pending: deferring it to the end of the slice would wait
+	 * on descriptors that never ran. The descriptor's own channel,
+	 * because a work may spread its descriptors over several.
+	 */
+	dma_async_issue_pending(tx->chan);
+	return 0;
+}
 
-		tx = dmaengine_prep_dma_memcpy(work->chan,
-					       sg_dma_address(sg_dst),
-					       sg_dma_address(sg_src),
-					       sg_dma_len(sg_src), flags);
-		if (!tx) {
-			atomic_set(&work->pending, 0);
-			return -EIO;
+/*
+ * Hand the copies to the provider as scatter-gather transactions of
+ * up to sg_elems folios each. A batch-capable engine such as DSA
+ * executes one transaction as one hardware batch descriptor, so the
+ * submission cost is paid once per sg_elems folios instead of once
+ * per folio. The scatterlists carry the DMA addresses mapped by
+ * map_folios(); they are not mapped again.
+ */
+static int submit_sg_transfers(struct dma_work *work, unsigned long flags)
+{
+	struct scatterlist *src_sg, *dst_sg;
+	unsigned int elems = READ_ONCE(sg_elems);
+	unsigned int done = 0;
+	int ret = 0;
+
+	src_sg = kmalloc_array(2 * elems, sizeof(*src_sg), GFP_KERNEL);
+	if (!src_sg)
+		return -ENOMEM;
+	dst_sg = src_sg + elems;
+
+	while (done < work->nr_copies) {
+		unsigned int n = min(elems, work->nr_copies - done);
+		struct dma_async_tx_descriptor *tx;
+		unsigned int i;
+
+		sg_init_table(src_sg, n);
+		sg_init_table(dst_sg, n);
+		for (i = 0; i < n; i++) {
+			struct dcbm_copy *copy = &work->copies[done + i];
+
+			sg_dma_address(&src_sg[i]) = copy->src;
+			sg_dma_len(&src_sg[i]) = copy->len;
+			sg_dma_address(&dst_sg[i]) = copy->dst;
+			sg_dma_len(&dst_sg[i]) = copy->len;
 		}
 
-		if (i == work->src_sgt->nents - 1) {
-			tx->callback = dma_completion_callback;
-			tx->callback_param = work;
+		dma_work_throttle(work);
+		tx = dmaengine_prep_dma_memcpy_sg(work->chan, dst_sg, n,
+						  src_sg, n, flags);
+		if (!tx) {
+			ret = -EIO;
+			break;
 		}
+		ret = submit_one(work, tx);
+		if (ret)
+			break;
+		done += n;
+	}
+
+	kfree(src_sg);
+	return ret;
+}
 
-		cookie = dmaengine_submit(tx);
-		if (dma_submit_error(cookie)) {
-			atomic_set(&work->pending, 0);
+static int submit_dma_transfers(struct dma_work *work)
+{
+	struct dma_async_tx_descriptor *tx;
+	unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
+	unsigned int i;
+	int ret;
+
+	/*
+	 * Cache-allocating writes leave the copied data LLC-warm for the
+	 * first access after remapping, at the cost of cache footprint
+	 * for folios that are not touched soon.
+	 */
+	if (READ_ONCE(cache_ctrl))
+		flags |= DMA_PREP_CACHE_CONTROL;
+
+	dma_work_init(work);
+
+	if (dma_has_cap(DMA_MEMCPY_SG, work->chan->device->cap_mask))
+		return submit_sg_transfers(work, flags);
+
+	for (i = 0; i < work->nr_copies; i++) {
+		struct dcbm_copy *copy = &work->copies[i];
+
+		dma_work_throttle(work);
+		tx = dmaengine_prep_dma_memcpy(work->chan, copy->dst, copy->src,
+					       copy->len, flags);
+		if (!tx)
 			return -EIO;
-		}
-		sg_dst = sg_next(sg_dst);
+
+		ret = submit_one(work, tx);
+		if (ret)
+			return ret;
 	}
 	return 0;
 }
@@ -220,34 +433,35 @@ static int folios_copy_dma(struct list_head *dst_list,
 {
 	struct folio *dst;
 	struct dma_work *works;
+	struct dcbm_group *grp;
 	struct list_head *src_pos = src_list->next;
 	struct list_head *dst_pos = dst_list->next;
+	unsigned long chan_mask;
 	int i, folios_per_chan, ret;
-	dma_cap_mask_t mask;
 	int actual_channels = 0;
-	unsigned int max_channels;
+	unsigned int max_channels, idx;
 
 	max_channels = min3(READ_ONCE(nr_dma_channels), nr_folios,
 			    (unsigned int)MAX_DMA_CHANNELS);
 
-	works = kcalloc(max_channels, sizeof(*works), GFP_KERNEL);
-	if (!works)
-		return -ENOMEM;
-
-	dma_cap_zero(mask);
-	dma_cap_set(DMA_MEMCPY, mask);
+	/* Prefer the device closest to where the copies are written. */
+	dst = list_first_entry(dst_list, struct folio, lru);
+	chan_mask = dcbm_claim_channels(max_channels, folio_nid(dst), &grp);
+	if (!chan_mask) {
+		atomic_long_inc(&batches_refused);
+		return -EBUSY;
+	}
 
-	for (i = 0; i < max_channels; i++) {
-		works[actual_channels].chan = dma_request_chan_by_mask(&mask);
-		if (IS_ERR(works[actual_channels].chan))
-			break;
-		init_completion(&works[actual_channels].done);
-		actual_channels++;
+	works = kcalloc(hweight_long(chan_mask), sizeof(*works), GFP_KERNEL);
+	if (!works) {
+		dcbm_release_channels(chan_mask);
+		return -ENOMEM;
 	}
 
-	if (actual_channels == 0) {
-		kfree(works);
-		return -ENODEV;
+	for_each_set_bit(idx, &chan_mask, nr_channels) {
+		works[actual_channels].chan = channels[idx].chan;
+		works[actual_channels].dev = grp->dev;
+		actual_channels++;
 	}
 
 	for (i = 0; i < actual_channels; i++) {
@@ -256,34 +470,27 @@ static int folios_copy_dma(struct list_head *dst_list,
 		if (folios_per_chan == 0)
 			continue;
 
-		ret = setup_sg_tables(&works[i], &src_pos, &dst_pos,
-				      folios_per_chan);
+		ret = map_folios(&works[i], &src_pos, &dst_pos,
+				 folios_per_chan);
 		if (ret)
 			goto err_cleanup;
 	}
 
 	for (i = 0; i < actual_channels; i++) {
-		if (!works[i].mapped)
+		if (!works[i].copies)
 			continue;
 		ret = submit_dma_transfers(&works[i]);
+		dma_work_done_submitting(&works[i]);
+		works[i].submitted = true;
 		if (ret)
-			goto err_cleanup;
+			goto err_wait;
 	}
 
-	for (i = 0; i < actual_channels; i++) {
-		if (atomic_read(&works[i].pending) > 0)
-			dma_async_issue_pending(works[i].chan);
-	}
-
-	for (i = 0; i < actual_channels; i++) {
-		if (atomic_read(&works[i].pending) == 0)
-			continue;
-		if (!wait_for_completion_timeout(&works[i].done,
-						 msecs_to_jiffies(10000))) {
-			ret = -ETIMEDOUT;
-			goto err_cleanup;
-		}
-	}
+	ret = 0;
+	for (i = 0; i < actual_channels; i++)
+		ret |= dma_work_wait(&works[i]);
+	if (ret)
+		goto err_cleanup;
 
 	/*
 	 * All folios copied; mark each destination so the move phase
@@ -293,19 +500,76 @@ static int folios_copy_dma(struct list_head *dst_list,
 		folio_set_migrate_copied(dst);
 
 	cleanup_dma_work(works, actual_channels);
+	dcbm_release_channels(chan_mask);
 
 	atomic_long_add(nr_folios, &folios_migrated);
 	return 0;
 
+err_wait:
+	/* Whatever was queued before the failure runs out. */
+	for (i = 0; i < actual_channels; i++)
+		dma_work_wait(&works[i]);
 err_cleanup:
 	pr_warn_ratelimited("dcbm: DMA copy failed (%d), falling back to CPU\n",
 			    ret);
 	cleanup_dma_work(works, actual_channels);
+	dcbm_release_channels(chan_mask);
 
 	atomic_long_add(nr_folios, &folios_failures);
 	return ret;
 }
 
+static void dcbm_put_channels(void)
+{
+	while (nr_channels) {
+		nr_channels--;
+		dma_release_channel(channels[nr_channels].chan);
+		channels[nr_channels].chan = NULL;
+	}
+	nr_groups = 0;
+}
+
+static int dcbm_get_channels(void)
+{
+	dma_cap_mask_t mask;
+
+	dma_cap_zero(mask);
+	dma_cap_set(DMA_MEMCPY, mask);
+
+	while (nr_channels < nr_dma_channels) {
+		struct dma_chan *chan = dma_request_chan_by_mask(&mask);
+		struct device *dev;
+
+		if (IS_ERR(chan))
+			break;
+
+		/*
+		 * The descriptors of one batch may spread over several
+		 * channels but its folios are mapped only once, so a
+		 * batch must stay within one DMA device: group the
+		 * channels by device as they enumerate.
+		 */
+		dev = dmaengine_get_dma_device(chan);
+		if (!dev) {
+			dma_release_channel(chan);
+			break;
+		}
+		if (!nr_groups || groups[nr_groups - 1].dev != dev) {
+			groups[nr_groups].dev = dev;
+			groups[nr_groups].node = dev_to_node(dev);
+			groups[nr_groups].first = nr_channels;
+			groups[nr_groups].nr = 0;
+			nr_groups++;
+		}
+		groups[nr_groups - 1].nr++;
+
+		channels[nr_channels].chan = chan;
+		mutex_init(&channels[nr_channels].lock);
+		nr_channels++;
+	}
+	return nr_channels ? 0 : -ENODEV;
+}
+
 static const struct migrator dma_migrator = {
 	.name = "DCBM",
 	.offload_copy = folios_copy_dma,
@@ -330,15 +594,23 @@ static int offloading_param_set(const char *val, const struct kernel_param *kp)
 		return 0;
 	}
 	if (enable) {
+		ret = dcbm_get_channels();
+		if (ret) {
+			mutex_unlock(&dcbm_mutex);
+			return ret;
+		}
 		ret = migrate_offload_register(&dma_migrator,
 					       READ_ONCE(dcbm_reason_mask));
 		if (ret) {
+			dcbm_put_channels();
 			mutex_unlock(&dcbm_mutex);
 			return ret;
 		}
 		WRITE_ONCE(offloading_enabled, true);
 	} else {
 		migrate_offload_unregister(&dma_migrator);
+		/* No batch is in flight past unregister; channels are idle. */
+		dcbm_put_channels();
 		WRITE_ONCE(offloading_enabled, false);
 	}
 	mutex_unlock(&dcbm_mutex);
@@ -370,6 +642,10 @@ static int nr_dma_chan_param_set(const char *val, const struct kernel_param *kp)
 		return -EINVAL;
 
 	mutex_lock(&dcbm_mutex);
+	if (offloading_enabled) {
+		mutex_unlock(&dcbm_mutex);
+		return -EBUSY;
+	}
 	WRITE_ONCE(nr_dma_channels, new_val);
 	mutex_unlock(&dcbm_mutex);
 	return 0;
@@ -385,7 +661,7 @@ static const struct kernel_param_ops nr_dma_chan_param_ops = {
 	.get = nr_dma_chan_param_get,
 };
 module_param_cb(nr_dma_chan, &nr_dma_chan_param_ops, NULL, 0644);
-MODULE_PARM_DESC(nr_dma_chan, "Max DMA channels to use (1..16)");
+MODULE_PARM_DESC(nr_dma_chan, "DMA channels to acquire when enabling (1..16)");
 
 /* reason_mask: set of MR_* reasons this migrator handles */
 static int reason_mask_param_set(const char *val, const struct kernel_param *kp)
@@ -455,6 +731,33 @@ static const struct kernel_param_ops folios_failures_param_ops = {
 module_param_cb(folios_failures, &folios_failures_param_ops, NULL, 0644);
 MODULE_PARM_DESC(folios_failures, "DMA-copy failure count (write to reset)");
 
+static int batches_refused_param_set(const char *val, const struct kernel_param *kp)
+{
+	atomic_long_set(&batches_refused, 0);
+	return 0;
+}
+
+static int batches_refused_param_get(char *buffer, const struct kernel_param *kp)
+{
+	return sysfs_emit(buffer, "%ld\n", atomic_long_read(&batches_refused));
+}
+
+static const struct kernel_param_ops batches_refused_param_ops = {
+	.set = batches_refused_param_set,
+	.get = batches_refused_param_get,
+};
+module_param_cb(batches_refused, &batches_refused_param_ops, NULL, 0644);
+MODULE_PARM_DESC(batches_refused, "Batches refused because all channels were busy (write to reset)");
+
+module_param(cache_ctrl, bool, 0644);
+MODULE_PARM_DESC(cache_ctrl, "Request cache-allocating writes (DMA_PREP_CACHE_CONTROL)");
+
+module_param(max_inflight, uint, 0644);
+MODULE_PARM_DESC(max_inflight, "Descriptors in flight per channel before waiting for completions");
+
+module_param(sg_elems, uint, 0644);
+MODULE_PARM_DESC(sg_elems, "Folios per scatter-gather transaction on DMA_MEMCPY_SG providers");
+
 static int __init dcbm_init(void)
 {
 	pr_info("dcbm: DMA Core Batch Migrator initialized\n");
@@ -466,6 +769,7 @@ static void __exit dcbm_exit(void)
 	mutex_lock(&dcbm_mutex);
 	if (offloading_enabled) {
 		migrate_offload_unregister(&dma_migrator);
+		dcbm_put_channels();
 		offloading_enabled = false;
 	}
 	mutex_unlock(&dcbm_mutex);
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [RFC PATCH 00/10] mm/migrate: dcbm for Intel DSA, on top of the batch copy offload RFC v6
  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
                   ` (9 preceding siblings ...)
  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 ` Dave Hansen
  2026-12-01 16:05   ` Garg, Shivank
  10 siblings, 1 reply; 14+ messages in thread
From: Dave Hansen @ 2026-12-01 15:24 UTC (permalink / raw)
  To: Daniel Byrne, Shivank Garg, Ravi Jonnalagadda
  Cc: Andrew Morton, David Hildenbrand, Zi Yan, Matthew Brost,
	Joshua Hahn, Rakie Kim, Byungchul Park, Gregory Price, Ying Huang,
	Alistair Popple, Lorenzo Stoakes, Liam R. Howlett,
	Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Karim Manaouil, Frank van der Linden, Teja Vojjala,
	Pravin Tamkhane, Kinsey Ho, Wei Xu, Matthew Wilcox,
	Davidlohr Bueso, Vinod Koul, Bharata B Rao, SeongJae Park,
	David Rientjes, Xuezheng Chu, Yiannis Nikolakopoulos,
	Johannes Weiner, John Hubbard, Peter Xu, Rik van Riel,
	Shakeel Butt, Tejun Heo, Fan Ni, Jonathan Cameron,
	Aneesh Kumar K.V, Nathan Lynch, Frank Li, Dan Williams, linux-mm,
	linux-kernel, Mike Day, dmaengine, Vinicius Costa Gomes, Frank Li,
	Dave Jiang

On 12/1/26 06:12, Daniel Byrne wrote:
> This is my implementation of dcbm for Intel DSA

First of all, what's a dcbm? ;)

Looking at this, I'm having a hard time understanding what the goal of
this posting is. What should those of us on the large cc do with it?


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [RFC PATCH 00/10] mm/migrate: dcbm for Intel DSA, on top of the batch copy offload RFC v6
  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
  0 siblings, 0 replies; 14+ messages in thread
From: Garg, Shivank @ 2026-12-01 16:05 UTC (permalink / raw)
  To: dave.hansen@intel.com, ravis.opensrc@gmail.com,
	daniel1.byrne@intel.com
  Cc: xuezhengchu@huawei.com, ying.huang@linux.alibaba.com,
	sj@kernel.org, pravintamkhane@google.com, shakeel.butt@linux.dev,
	riel@surriel.com, joshua.hahnjy@gmail.com, david@kernel.org,
	mhocko@suse.com, aneesh.kumar@kernel.org, rientjes@google.com,
	hannes@cmpxchg.org, kinseyho@google.com, kmanaouil.dev@gmail.com,
	tj@kernel.org, weixugc@google.com, liam@infradead.org,
	linux-kernel@vger.kernel.org, gourry@gourry.net,
	vinicius.gomes@intel.com, Rao, Bharata Bhasker, byungchul@sk.com,
	djbw@kernel.org, dave.jiang@intel.com, rppt@kernel.org,
	willy@infradead.org, surenb@google.com, fvdl@google.com,
	matthew.brost@intel.com, Lynch, Nathan, jhubbard@nvidia.com,
	peterx@redhat.com, yiannis@zptcorp.com, michael.day@amd.com,
	rakie.kim@sk.com, Frank.Li@kernel.org, dave@stgolabs.net,
	apopple@nvidia.com, ziy@nvidia.com, vbabka@kernel.org,
	tejavojjala@google.com, ljs@kernel.org, Frank.li@nxp.com,
	linux-mm@kvack.org, akpm@linux-foundation.org, vkoul@kernel.org,
	nifan.cxl@gmail.com, jic23@kernel.org, dmaengine@vger.kernel.org

On Tue, 2026-12-01 at 07:24 -0800, Dave Hansen wrote:
> On 12/1/26 06:12, Daniel Byrne wrote:
> > This is my implementation of dcbm for Intel DSA
> 
> First of all, what's a dcbm? ;)

Hi Dave,

That name is mine. dcbm is the "DMA Core (or Copy) Batch Migrator", the
reference driver in my RFC "Accelerate page migration with batch copying
and hardware offload" [1].

[1]
https://lore.kernel.org/all/20260630-shivank-batch-migrate-offload-v6-0-da95d7e8b8a2@amd.com

> 
> Looking at this, I'm having a hard time understanding what the goal of
> this posting is. What should those of us on the large cc do with it?

The goal of the overall series is to make page migration faster and cheaper
on the CPU by offloading the folio copy to otherwise idle DMA engines,
through the dmaengine core-APIs. This matters for bulk migration such as
memory tiering (promotion and demotion between DRAM and CXL) and
compaction.

The Cc list is the one from my series, which is why it is so large.
It has grown over revisions. For v7, I'll trim it.

Thanks,
Shivank

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [RFC PATCH 08/10] dmaengine: reintroduce DMA_MEMCPY_SG
  2026-12-01 14:12 ` [RFC PATCH 08/10] dmaengine: reintroduce DMA_MEMCPY_SG Daniel Byrne
@ 2026-12-01 19:51   ` Frank Li
  0 siblings, 0 replies; 14+ messages in thread
From: Frank Li @ 2026-12-01 19:51 UTC (permalink / raw)
  To: Daniel Byrne
  Cc: Shivank Garg, Ravi Jonnalagadda, Andrew Morton, David Hildenbrand,
	Zi Yan, Matthew Brost, Joshua Hahn, Rakie Kim, Byungchul Park,
	Gregory Price, Ying Huang, Alistair Popple, Lorenzo Stoakes,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Karim Manaouil,
	Frank van der Linden, Teja Vojjala, Pravin Tamkhane, Kinsey Ho,
	Wei Xu, Matthew Wilcox, Davidlohr Bueso, Vinod Koul,
	Bharata B Rao, SeongJae Park, David Rientjes, Xuezheng Chu,
	Yiannis Nikolakopoulos, Dave Hansen, Johannes Weiner,
	John Hubbard, Peter Xu, Rik van Riel, Shakeel Butt, Tejun Heo,
	Fan Ni, Jonathan Cameron, Aneesh Kumar K.V, Nathan Lynch,
	Frank Li, Dan Williams, linux-mm, linux-kernel, Mike Day,
	dmaengine, Vinicius Costa Gomes, Frank Li, Dave Jiang

On Tue, Dec 01, 2026 at 06:12:57AM -0800, Daniel Byrne wrote:
> Reintroduce the DMA_MEMCPY_SG transaction type and
> device_prep_dma_memcpy_sg(), removed by commit 0cae04373b77 ("dmaengine:
> remove DMA_MEMCPY_SG once again") when the last in-tree user went away.
> The migration copy offload added later in this series is the new
> user. It hands the provider a scatter/gather memcpy in one call so
> that batch-capable engines such as DSA can turn the operation into a
> single hardware batch descriptor. The transfer copies min(src, dst)
> total bytes.
>
> Signed-off-by: Daniel Byrne <daniel1.byrne@intel.com>
> ---
> The cover letter of RFC v6 lists this hook as a follow-up. This is the
> dmaengine core part. No provider implements it in this series.
>
>  .../driver-api/dmaengine/provider.rst         | 10 ++++++++++
>  drivers/dma/dmaengine.c                       |  1 +
>  include/linux/dmaengine.h                     | 20 +++++++++++++++++++
>  3 files changed, 31 insertions(+)
>
> diff --git a/Documentation/driver-api/dmaengine/provider.rst b/Documentation/driver-api/dmaengine/provider.rst
> index be47852e5a0f..e844c7c0fce0 100644
> --- a/Documentation/driver-api/dmaengine/provider.rst
> +++ b/Documentation/driver-api/dmaengine/provider.rst
> @@ -162,6 +162,16 @@ Currently, the types available are:
>
>    - The device is able to do memory to memory copies
>
> +- DMA_MEMCPY_SG
> +
> +  - The device supports memory to memory scatter-gather transfers.
> +
> +  - Even though a plain memcpy can look like a particular case of a
> +    scatter-gather transfer, with a single chunk to copy, it's a distinct
> +    transaction type in the mem2mem transfer case. This is because some very
> +    simple devices might be able to do contiguous single-chunk memory copies,
> +    but have no support for more complex SG transfers.
> +
>    - No matter what the overall size of the combined chunks for source and
>      destination is, only as many bytes as the smallest of the two will be
>      transmitted. That means the number and size of the scatter-gather buffers in
> diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c
> index 34be53487a21..bed2a80cb538 100644
> --- a/drivers/dma/dmaengine.c
> +++ b/drivers/dma/dmaengine.c
> @@ -1219,6 +1219,7 @@ int dma_async_device_register(struct dma_device *device)
>  }
>
>  	CHECK_CAP(dma_memcpy,      DMA_MEMCPY);
> +	CHECK_CAP(dma_memcpy_sg,   DMA_MEMCPY_SG);
>  	CHECK_CAP(dma_xor,         DMA_XOR);
>  	CHECK_CAP(dma_xor_val,     DMA_XOR_VAL);
>  	CHECK_CAP(dma_pq,          DMA_PQ);
> diff --git a/include/linux/dmaengine.h b/include/linux/dmaengine.h
> index 805668fbe437..5df4bfc000a6 100644
> --- a/include/linux/dmaengine.h
> +++ b/include/linux/dmaengine.h
> @@ -50,6 +50,7 @@ enum dma_status {
>   */
>  enum dma_transaction_type {
>  	DMA_MEMCPY,
> +	DMA_MEMCPY_SG,
>  	DMA_XOR,
>  	DMA_PQ,
>  	DMA_XOR_VAL,
> @@ -860,6 +861,7 @@ struct dma_filter {
>   * @device_router_config: optional callback for DMA router configuration
>   * @device_free_chan_resources: release DMA channel's resources
>   * @device_prep_dma_memcpy: prepares a memcpy operation
> + * @device_prep_dma_memcpy_sg: prepares a memcpy_sg operation
>   * @device_prep_dma_xor: prepares a xor operation
>   * @device_prep_dma_xor_val: prepares a xor validation operation
>   * @device_prep_dma_pq: prepares a pq operation
> @@ -940,6 +942,11 @@ struct dma_device {
>  	struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)(
>  		struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
>  		size_t len, unsigned long flags);
> +	struct dma_async_tx_descriptor *(*device_prep_dma_memcpy_sg)(
> +		struct dma_chan *chan,
> +		struct scatterlist *dst_sg, unsigned int dst_nents,
> +		struct scatterlist *src_sg, unsigned int src_nents,
> +		unsigned long flags);
>  	struct dma_async_tx_descriptor *(*device_prep_dma_xor)(
>  		struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
>  		unsigned int src_cnt, size_t len, unsigned long flags);
> @@ -1246,6 +1253,19 @@ static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memcpy(
>  						    len, flags);
>  }
>
> +static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memcpy_sg(
> +		struct dma_chan *chan,
> +		struct scatterlist *dst_sg, unsigned int dst_nents,
> +		struct scatterlist *src_sg, unsigned int src_nents,
> +		unsigned long flags)
> +{
> +	if (!chan || !chan->device || !chan->device->device_prep_dma_memcpy_sg)
> +		return NULL;
> +
> +	return chan->device->device_prep_dma_memcpy_sg(chan, dst_sg, dst_nents,
> +						       src_sg, src_nents, flags);

The key is how resolve src_nents != dst_ndents case. and each sg is miss
matched, for example:

	0x04..0xfff		0x14..0x1fff
	0x1000..0x3fff		0x1000..0x1fff
				0x4000..0x4fff
				0x6000..0x5fff
	0x4000..0x5ffB		0x6000..0x6fEB

Frank

> +}
> +
>  /**
>   * dmaengine_get_max_xor - get the maximum number of XOR sources for a channel
>   * @chan: DMA channel
> --
> 2.43.0
>


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2026-12-01 19:51 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [RFC PATCH 05/10] mm/migrate: give hugetlb its own bit for the content-copied marker Daniel Byrne
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox