From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796134762; b=mxsnm6ME6jE3TpLqTxyNUxNZle7SC50VCkoyrQmlhpB2XWp3/DSAnQbizH5rAEp0MMts vxqyvCxDBl4YNWk38oyTKpOd74i5VGoCckldiqg/kBl5RKOwyWFwguMZxixEFfNoNJ9Yb o6aguk4uNkNIH0wTNS173Am/v7qXQ8Lj7x21hQTlcYiC6m/w3b+7StE+GZ00ZqBuyLu0h lnclxUhxPQwM3YUo8kOOy3ysjdKtVOaYBNGF3qM6cNjRHKapMnKtYOBswXUD6MA1l1iCT it0MtB2DnngdYDHowrbMv+yhdmXlkIGA2EuS7xdnyM1frPf8EiTXFmChWk0AR6AABxQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796134762; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=fXtEA4BMwBlw+2kKzhGHehDnjw/qBTQgVF31PPY2/98=; b=fL7kVjRqZO5sQQUMrWrLh5AghZGkmDAkfqKObF/LqCUXGXPEYObsCZ2jGOIQajVnmPZZ NLIRU7hcJqjHjNlEN4szjBdoOAtinkgoUMhnOTmwb8uvbvt4TaBnl7gw82E0qxJd/LKJ4 mZhcCmqIv60Zl3bgdZanUdPAlNlqWIqnCO/hyG9NhKnVAQI3kIPVU5fYb+0Q2U5qbXVnO DDxCNjQNWs8ydwO8QGp3h1iyRB93RUAeEwbqw1/LIeyD6dzb2Bm4REwhub+qKeJX8cjcx UDPbl++qjJ29mfcsne4tc/GQRdTaw9lJ/cYzo5SVaQF48EyzRnw2RuNMsJh3inl6h7g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=TL+IoSMW; arc=none smtp.remote-ip=198.175.65.18 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 18430606A333; Tue, 1 Dec 2026 14:19:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 18430606A333 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=TL+IoSMW DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796134763; x=1827670763; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fXtEA4BMwBlw+2kKzhGHehDnjw/qBTQgVF31PPY2/98=; b=TL+IoSMWLrk1rF7nSYTYMqH8CaF32twfHcRkXCI1Bc3BZr6MH9YhIRiU etzLZAYGMrHCnbz93Oi706wwg/Y8r+FeBHcNL+AzQ6oOufRaFJabNjTfk 1rcnJ7m63BJzp8Cub8WBMTIPS57Tbpoupu9tEcD6FGJNMU0RU/WFBiFKX Cai5NC49Hj1uehj8P/cA7MaKxmI81Q8CxPcxy+CNfKQaTqzhqJJ5IEDCm x1U9rOljp0WrfJu4c7wB0ZMRcBqVZfnv3dxhwo8/zK4Ksrck9ngQt9s0V 70Z2xLOxUR74OqqHtr8hyHD3rbVSKdEAP7LLmA6aW2QMGCcMnttZyh2rU A==; X-CSE-ConnectionGUID: 22E0Ame8QZCyMv4o69OWtg== X-CSE-MsgGUID: OfVM+7O1TfOv9fgb5R1vCw== X-IronPort-AV: E=McAfee;i="6800,10657,11983"; a="5019228" X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="5019228" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa110.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 06:19:19 -0800 X-CSE-ConnectionGUID: JN8zvpzrRNuSYi+yzep7yw== X-CSE-MsgGUID: 0dhZqRF7RbinkiTO7+Lm8A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="13380704" Received: from jf5300-15a101203tc.jf.intel.com (HELO meta-crater2c.jf.intel.com) ([10.54.113.49]) by fmviesa005.fm.intel.com with ESMTP; 01 Dec 2026 06:19:17 -0800 From: Daniel Byrne 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@kvack.org, linux-kernel@vger.kernel.org, Mike Day , dmaengine@vger.kernel.org, Vinicius Costa Gomes , Frank Li , Dave Jiang , Daniel Byrne Subject: [RFC PATCH 03/10] mm/migrate: add copy offload registration infrastructure Date: Tue, 1 Dec 2026 06:12:52 -0800 Message-ID: <20261201141259.2124188-4-daniel1.byrne@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261201141259.2124188-1-daniel1.byrne@intel.com> References: <20261201141259.2124188-1-daniel1.byrne@intel.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Shivank Garg 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 Signed-off-by: Mike Day Signed-off-by: Shivank Garg [daniel: rebased to v7.4-rc2] Signed-off-by: Daniel Byrne --- 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 +#include +#include + +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 #include #include +#include #include @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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