From nobody Fri Aug 7 09:39:19 2026 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 9F5173B8407 for ; Tue, 28 Jul 2026 06:28:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785220128; cv=none; b=nDF1eeJhK9OmOTTqC2otR6c25q/uw9+LF3EqmVYvboXapcw0LsIy+OOQaU4fQNX36iOO9oOKtffpBtN0NVDlbYG9LX66rExYiK+z9djHKeH5ci1mNucjq/G5IRQnUk5ZZA7YrbeB2jxN5FWHEfPIYofLZrcRpm/HlbfZgT2ORsk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785220128; c=relaxed/simple; bh=kKygvtH/OzsvbEzhb1WD9p+0+rChW7yxWOwVTVF50wU=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=t6BI1F7hmzR0nv0kOqxzPp0+mVv2RFRNu/2/kXe6wTPMUfBCdE5ASwCDjfexvVpV2iKdlnCbqfrGy+ISr0LDf/dBbOFrw1Pz1wY1drS5Lv60jDwyU/GraOOFxVQXuL2Fqvkglkhslg3VW5zrz3oxgiFTZt+aonrcZyIIn75pUgw= 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=golX7a7n; arc=none smtp.client-ip=192.198.163.11 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="golX7a7n" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1785220127; x=1816756127; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=kKygvtH/OzsvbEzhb1WD9p+0+rChW7yxWOwVTVF50wU=; b=golX7a7nvJ2hb+KKJoESg8N48fmBbWb7zfuPcd7HtsP6MYoFukJju8/0 XdBlEM0ok/8zpdnwto9vegsxgwg4NBtrA2amoiDMcIjEKatLlAtllCe9b 0gYLsQdOjyW2k1xsQBpFID9DaLsduxLrv5oL6d2mRLKMV/nSynfyTeq/M YYXEvAuqBSFRdO6DDPKlAFu+73ox7VbGVeAkRb1vLWhY8YocJCTkGdits y4n7JsTihGADxOTGPIaEsW+ix1kw7ZAyvIH+uvJO97VMdSovVQ31tzfcV hwapseFDctE9rbaHuteO4mVtKMFbTS3SFYkfQ4o4brb7jqbyyhy2SuOuo A==; X-CSE-ConnectionGUID: c9vvyRl+SpSQ9BqnMefgkA== X-CSE-MsgGUID: twtgK4eDR5qjsEr+4HobzA== X-IronPort-AV: E=McAfee;i="6800,10657,11858"; a="96383809" X-IronPort-AV: E=Sophos;i="6.25,189,1779174000"; d="scan'208";a="96383809" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jul 2026 23:28:46 -0700 X-CSE-ConnectionGUID: 8rkuG3GNR1OX+PPXZvewPA== X-CSE-MsgGUID: T+f44TYfSoeVUAE6EBzY1Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,189,1779174000"; d="scan'208";a="289575953" Received: from varungup-desk.iind.intel.com ([10.190.238.71]) by orviesa002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jul 2026 23:28:42 -0700 From: Arvind Yadav To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: akpm@linux-foundation.org, david@kernel.org, matthew.brost@intel.com, joshua.hahnjy@gmail.com, ziy@nvidia.com, rakie.kim@sk.com, byungchul@sk.com, gourry@gourry.net, ying.huang@linux.alibaba.com, apopple@nvidia.com, balbirs@nvidia.com Subject: [PATCH v4] mm/migrate_device: Clear stale mapping after freeing swapcache Date: Tue, 28 Jul 2026 11:58:32 +0530 Message-ID: <20260728062832.1107127-1-arvind.yadav@intel.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" __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 554754eb26ff..0e6f00190fd8 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -1182,6 +1182,13 @@ static void __migrate_device_pages(unsigned long *sr= c_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 =3D 1; } else if ((src_pfns[i] & MIGRATE_PFN_MIGRATE) && (dst_pfns[i] & MIGRATE_PFN_COMPOUND) && !(src_pfns[i] & MIGRATE_PFN_COMPOUND)) { @@ -1221,6 +1228,12 @@ static void __migrate_device_pages(unsigned long *sr= c_pfns, folio =3D page_folio(migrate_pfn_to_page(src_pfns[i+j])); newfolio =3D page_folio(migrate_pfn_to_page(dst_pfns[i+j])); =20 + /* + * folio_free_swap() removed the folio from the swap + * cache. Refresh the saved mapping before migration. + */ + mapping =3D folio_mapping(folio); + r =3D folio_migrate_mapping(mapping, newfolio, folio, extra_cnt); if (r) src_pfns[i+j] &=3D ~MIGRATE_PFN_MIGRATE; --=20 2.43.0