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=1796134760; b=JtsPaW9Jm6PrPwoTBNm37O8SNrt/tth95/FaAvw/asG4subnZWwUN/rTIjVswtS2mKXp WullM9mrKWQKIpENZwYr4UTvUvcY62lYRp2gbeLhEJqMXyfoImWhXB7JrUymI+uPCJnK+ Lm3O1TMKcqirUoZPar9Jx13tS/4WwVQFUYHuCuWZJxZLizlzOyqeWyqJM+iRWqv2moHU9 jAiEN1kaZQCO1U+cBTAIpU4kA/3mLpnHAHLdafrFLVTb0pd1SYDDZB7D2DploOBDsrAau 6BXuoWuDbw6dHc//ItvyUEl+5Zdx/PLhz7xHAus6jjRBUxVkJrtzfrjYmiKojnLGtag== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796134760; 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=yI8GueJ15vv7PEtLIOWQZQpN6BWv/sVYHTG9A06DAiM=; b=yaH6ROPKFG2HDHpZzl/hKmJO6i0R4SpbXeX/EcicS3igtMYKDSFGJyOOFfqFGIDdc9vf qyzB2eVr3OBavRDGGTxveaSjElTEU9adzegByuMes8tWm9WOhnCSxY0Y2UazvL/FypltV /FyI+JSYc3ErGl5S9r1dJFt1s/WLJiF9uJLSQ4MdJbf4wB0oxbGk5kX6wuq9p4puKKdot s0RzQiqNtkUcxUpfn+9095xYbAICZSFFf5XHChN4cDlXN4R+agHO/CxOCKm6lKM8ZUIU4 chG4CFC2NNmqUlD/NGBKOXJtQ64gxtoMhNUgFHnslLy5qhVakSz2/4tCC3MRhDCaUgQ== 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=Eko8tp27; 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 C568A606A680; Tue, 1 Dec 2026 14:19:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C568A606A680 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=Eko8tp27 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796134761; x=1827670761; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yI8GueJ15vv7PEtLIOWQZQpN6BWv/sVYHTG9A06DAiM=; b=Eko8tp27lxG8nXrk3GhvCu0udzbW2XGiq0G//mkvxGF+bGlpfcR5x/vR woyAMUEzZ1FsixU8m1fSo1i9lzYDjNM4Z8nYZAmZk0WSLtmRGA42NPu+g GPoRIWwPqY4TrrXRMaOJgBT54tLkS+3ZGnRvCzk7rixDuUnkJ2mXLmCHa 0xxQgRUlpDWvG7Pc2IvV7CVErBDxoe9PW9DK9VbAt/WlEAvl8EGxy0F57 QuPH4u8U/LPea/NnQoWlrkzFCurgBHFOxvIAHrPVt75uJUgetCdIWXE76 9Lpp4+eim0Jcj42XxSeLgQPt1a1G0Ej0wcnkNOW+7HC8OT/c8ou9jES38 g==; X-CSE-ConnectionGUID: 6lPVOs6WTqetsX6u+EFuTQ== X-CSE-MsgGUID: yHd+FSMcT/iNbJ4D09RpgQ== X-IronPort-AV: E=McAfee;i="6800,10657,11983"; a="5019211" X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="5019211" 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:18 -0800 X-CSE-ConnectionGUID: u7hSjVMPQQGzXuPZbCzlmg== X-CSE-MsgGUID: k7eQK+AdR1i02MbH1UeXAg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="13380696" 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:15 -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 02/10] mm/migrate: add batch-copy path in migrate_pages_batch Date: Tue, 1 Dec 2026 06:12:51 -0800 Message-ID: <20261201141259.2124188-3-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 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 [daniel: rebased to v7.4-rc2] Signed-off-by: Daniel Byrne --- 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