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=1796134766; b=g980l1nSjlxyXRhhKMmDa+EljeQe9fmyXynWp0u11TLOMdijtDYgBwBh7ij9BSd60Wcu 0m1/2M8nyp/kwXy5K8Bw1s4J+2raRj2h+EmOmZpLABPfMAelc58abBL/aoHnSulCdtwYY CVlrkT+m/sgxdieWhf6UX3us0OkZBKj4gDr8V+ofRfcTgDwBwCprdoPkN46p6yV8IN4xU cNlH5XrhuGax1WR/N4nhLfVQjJpBHqbhYihn2aLEqtqBXNjTGNasNdN7Zzm6hJd7dsZuk /Ao2W3kjtv7ToPaXqbdRsfdwo95QCqXeY9skSGC5+6ZQVdDTTJ/26euDsJWzmvaFwfw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796134766; 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=atk0rSAkXq6Ib/w7OGFWIxNAByL1EAHtBo2qe0CK9rM=; b=Qc1bDAW32wikw8htGffDH3P5AsCWcGMZ1Sgo2E24hKicloVOTkEqBuMpluK3uwaQD8DM OdeDODtJJR9T+C1HFuBi2FyaJkaieoVK5Tp0TcfynxlNk9j/+hTkmqRI9gedtE+L39bXT D5rORzQXmz93/3hldaFj1PTt/TWKuMzq1vU9oOuqOtGHnUvDiw8sUXVdSlQAHJaNFJdla KvIhYrE1EUegjYz1Ow44GlqA5bSkvpNWixiSINdTiiqDNHaAaULOnSORMqPmJv+KsuMgz 6CtA0ZKiQF+aF0KdC8zqaZx+ivLn8U7vXa875sZ8gDxC1MGX5Z/W2qFYkYXVCMTu8CA== 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=ABRYYwTj; 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 8749D606A6A7; Tue, 1 Dec 2026 14:19:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8749D606A6A7 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=ABRYYwTj DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796134766; x=1827670766; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=atk0rSAkXq6Ib/w7OGFWIxNAByL1EAHtBo2qe0CK9rM=; b=ABRYYwTj95fw8VIxnZRq1oViy6xTrhKQYHP/rdjVx1n28/Vx6N+7ALzc UCY6tnJhMOe6yxkFkXqImxo65qAcF+/nZuaDUvg75YqmAHj+kFYf0cxl7 Lkn8kJt16XJHRwK85dqQKAMxOX7CKuidBDqGtX7YzxYq1mJ4az8pPY1BD H/jKz6MelDArbylKfKHONU+GqiBTPrn/siPNmpAmC7cXU9YxUUdTFRoWq gbiMdUK2KGa5v+9SgIinEFcPx9SnE9/4s3AmXXF1NbSrqvrNs9VUiBHKW uIgz2SYTQ8YU2zpZYBKbGYTwZne5x+YKAanJdE1/iy+vvlGeoq95YGQiz w==; X-CSE-ConnectionGUID: q59r502iSYSHaWraAXtrRw== X-CSE-MsgGUID: BXa83spmRPWIOdBudIUSyA== X-IronPort-AV: E=McAfee;i="6800,10657,11983"; a="5019263" X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="5019263" 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:21 -0800 X-CSE-ConnectionGUID: ZNfJaMtZQqW0Q0LSBVV+Kw== X-CSE-MsgGUID: quKovmQkS/CURa+/kbGFTw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="13380720" 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:19 -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 05/10] mm/migrate: give hugetlb its own bit for the content-copied marker Date: Tue, 1 Dec 2026 06:12:54 -0800 Message-ID: <20261201141259.2124188-6-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 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 --- 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 +#include #include #include #include @@ -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