From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) (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 668233C0626 for ; Mon, 27 Jul 2026 05:39:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.10 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785130778; cv=none; b=el2bVPR89/agsPnN0jwwtQLLygG+RLbIOpkpydHzeTr/5nkS693ITpY2ONhExeNbzuqMx5GqVay7vyXY8gq3jIRvxSRSBUyp37UY6p0JwQEyPKmygK1PrJ6676TfeR7emnJ8BOckIN+WbnwQUU70C74eHhQF6XLQRmX9AfhIg8w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785130778; c=relaxed/simple; bh=HDQMhBtH34OPq5lrFLacROh5yCkQVDcx1Q3l8xNuW9k=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=bGYcuJCTREg54oZL53Sy+UUK97BclCWz7w1WsAZSQ/0ETzIKTjjORz147SuyRZjD6o7cAGBvI5O2ZfnDEMu8lHwrXaNzZSmzjWz1zxzXUEzONaFI4/lmubHQwje61dHvyE88IxVAJYcsQYM/Ej5D1PcSNCs+SP/VpY295AIcpdo= 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=YVRd1ZGL; arc=none smtp.client-ip=198.175.65.10 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="YVRd1ZGL" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1785130775; x=1816666775; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=HDQMhBtH34OPq5lrFLacROh5yCkQVDcx1Q3l8xNuW9k=; b=YVRd1ZGLED19fR0+x1awJ6h72oEvnP1wAYHR1Uvw9Xq8E/EqEXOO128H +OqrZoq08XFo6Q2AgEZ/TjYUnb3wLnvvH6TjymjcFIQEnQNKh7ZKG+YH+ udeuQ5XnAqpJqgt6o6NyMd+UQTdiUoNqlxxdlUiAM9e/bp9dvNrFrX017 +P/8XxChLfuHB7G/GBzbIIeB7okd8H1sjYhOakcPyPMYQreEtp5holTmh 7P+Eie3CPspjWakgGWm7kZAx87Ke3El3n/B0aAVnRbkm95V3AnCTtvDbz YHnU9shZ3X1WWiE8ocjFBPCdee+9DU2nx9axwQvTqPQx+qGwdDDuK7mW3 A==; X-CSE-ConnectionGUID: CLS1AHlnQ8C8yn7IdxtAFA== X-CSE-MsgGUID: DUjqM4niRBCvzeaSfNljEA== X-IronPort-AV: E=McAfee;i="6800,10657,11857"; a="103103758" X-IronPort-AV: E=Sophos;i="6.25,187,1779174000"; d="scan'208";a="103103758" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jul 2026 22:39:35 -0700 X-CSE-ConnectionGUID: 5U4I0bmySoueg33hEVzjuA== X-CSE-MsgGUID: R/cRy4WSQ4WRwIr8Qa+36w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,187,1779174000"; d="scan'208";a="259946469" Received: from varungup-desk.iind.intel.com ([10.190.238.71]) by orviesa009-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jul 2026 22:39:30 -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 v3] mm/migrate_device: Clear stale mapping after freeing swapcache Date: Mon, 27 Jul 2026 11:09:20 +0530 Message-ID: <20260727053920.3494211-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: 8bit __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 take the mapped-folio path after the swapcache reference has been dropped. This can cause an invalid swap_space lock access followed by 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. 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 | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 554754eb26ff..74ac6651d4b3 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -1182,6 +1182,11 @@ 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 + */ + nr = 1; } else if ((src_pfns[i] & MIGRATE_PFN_MIGRATE) && (dst_pfns[i] & MIGRATE_PFN_COMPOUND) && !(src_pfns[i] & MIGRATE_PFN_COMPOUND)) { @@ -1204,6 +1209,12 @@ static void __migrate_device_pages(unsigned long *src_pfns, src_pfns[i] &= ~MIGRATE_PFN_MIGRATE; goto next; } + + /* + * folio_free_swap() removed the folio from the swap + * cache. Refresh the saved mapping before migration. + */ + mapping = folio_mapping(folio); } } else if (folio_is_zone_device(newfolio)) { /* -- 2.43.0