From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4A59D481A9A for ; Wed, 5 Aug 2026 23:10:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.12 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785971454; cv=none; b=Hbyju9x3YUEOn5jImLpxEv5Fi3rA2HFZrEuVfyZLBb0mpgi9PrNxxQcInG9wCzjYRrLD6EfNr/DDCWW//UAZGDY/PYxDItzTQ3cTWHryisaZ+qHyiz/MPiWHUTd7u+t35bQPLhnnKp5QGFYVYMbQait/6Y060+aX7LpDugCv9A8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785971454; c=relaxed/simple; bh=dNJ/oi0EebM2qITDTJ9UJ0BoOZBi/aCxva+AfF14at8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XWuj7ajBDx+pZSE+f4C0n382g1iVC62rCIpxljWSIepAn4Rpk6UrTg5SF4G7zPPPy8wf94zHL58k45ZLYJyyiAR0JgYxt09bNzW1Uj5G/ZkQOUkSbcSJhBOzKun/5KjVr0a20k31Ll0F8qwZ7mahleVID5jxC2lYklMkNhgZiy4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Mo0gf365; arc=none smtp.client-ip=192.198.163.12 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.b="Mo0gf365" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1785971451; x=1817507451; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=dNJ/oi0EebM2qITDTJ9UJ0BoOZBi/aCxva+AfF14at8=; b=Mo0gf365n/KhVFQsPA0udEeuRib0ao4X4g8V/J/0uVaIHjRpLr+jP1tJ mlNCqY9oBGv3m9ILkUIcbyoFA5qci1I2LrcMWzOtLs0jA1RdTskyijXVk I7UjVNxRdezAufqyZum6AQ91NkjvyX+cSeQzobGGo4GAGvg9cqc7ddiex zZhqD1jk7BXarm2QwlAnEc80jhiG6yvPlIxLgML1Xt1Kn30mM40C32JC5 6DSN5ygDfbBrqw/50ShtIL49qyuLRKfBDOKJyC2rgapNC1xJCvRitsCpv aotU6AUEG1zSeUh6oTLHMDu+LeVOyxzg4i3FpMY8s4qeM1mVAOvGYnbgt g==; X-CSE-ConnectionGUID: m5diZvaUQSCIoJyDZgHzUg== X-CSE-MsgGUID: +cq1h87NSISbn2ogTU4EdA== X-IronPort-AV: E=McAfee;i="6800,10657,11866"; a="90376832" X-IronPort-AV: E=Sophos;i="6.25,207,1779174000"; d="scan'208";a="90376832" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by fmvoesa106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Aug 2026 16:10:49 -0700 X-CSE-ConnectionGUID: gdzBu8tUSsWVVT+WYsw11w== X-CSE-MsgGUID: Douz6uonSw2oqZmSx0TiGg== X-ExtLoop1: 1 Received: from gsse-cloud1.jf.intel.com ([10.54.39.91]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Aug 2026 16:10:48 -0700 From: Matthew Brost To: intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Arvind Yadav , Andrew Morton , David Hildenbrand , Joshua Hahn , Rakie Kim , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Zi Yan , Balbir Singh Subject: [PATCH v3 1/6] mm/migrate_device: Clear stale mapping after freeing swapcache Date: Wed, 5 Aug 2026 16:10:36 -0700 Message-Id: <20260805231041.3791771-2-matthew.brost@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260805231041.3791771-1-matthew.brost@intel.com> References: <20260805231041.3791771-1-matthew.brost@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Arvind Yadav __migrate_device_pages() reads the folio mapping before calling folio_free_swap(). When folio_free_swap() succeeds, the folio is removed from the swap cache, but the saved mapping still points to swap_space. Passing the stale mapping to folio_migrate_mapping() makes it use the mapped-folio path for a folio that is no longer in swapcache. It can then operate on swap_space.i_pages with invalid reference accounting, eventually triggering a folio reference count BUG. After a successful split, nr still contains the number of pages in the original large folio, although each resulting page is now a separate order-0 folio. Reset nr to 1 so each split folio is processed separately, including its own swapcache removal and mapping lookup. Refresh the saved mapping after folio_free_swap() so the current folio state is used during migration. v2: - Refresh the mapping using folio_mapping(), as suggested by Zi Yan. v3: - Reset nr to 1 after a successful split so each resulting folio is processed independently, as suggested by Zi Yan. v4: - Re-read each source folio's mapping immediately before folio_migrate_mapping(), as suggested by Balbir Singh. - Add a warning to validate the post-split order-0 invariant, as suggested by Balbir Singh. Fixes: df263d9a7dff ("mm/migrate_device: try to handle swapcache pages") Cc: Andrew Morton Cc: David Hildenbrand Cc: Matthew Brost Cc: Joshua Hahn Cc: Rakie Kim Cc: Byungchul Park Cc: Gregory Price Cc: Ying Huang Cc: Alistair Popple Reviewed-by: Zi Yan Reviewed-by: Balbir Singh Signed-off-by: Arvind Yadav --- mm/migrate_device.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 908d2d4ec43a..162d29b2807a 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -1183,6 +1183,13 @@ static void __migrate_device_pages(unsigned long *src_pfns, MIGRATE_PFN_COMPOUND); goto next; } + + /* + * reset nr so that only first after-split folio + * is processed below + */ + VM_WARN_ON_ONCE(folio_test_large(folio)); + nr = 1; } else if ((src_pfns[i] & MIGRATE_PFN_MIGRATE) && (dst_pfns[i] & MIGRATE_PFN_COMPOUND) && !(src_pfns[i] & MIGRATE_PFN_COMPOUND)) { @@ -1222,6 +1229,12 @@ static void __migrate_device_pages(unsigned long *src_pfns, folio = page_folio(migrate_pfn_to_page(src_pfns[i+j])); newfolio = page_folio(migrate_pfn_to_page(dst_pfns[i+j])); + /* + * folio_free_swap() removed the folio from the swap + * cache. Refresh the saved mapping before migration. + */ + mapping = folio_mapping(folio); + r = folio_migrate_mapping(mapping, newfolio, folio, extra_cnt); if (r) src_pfns[i+j] &= ~MIGRATE_PFN_MIGRATE; -- 2.34.1