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 4826A1E0E14 for ; Thu, 30 Jul 2026 04:04:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=198.175.65.10 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785384277; cv=fail; b=PSwF+BzKsVscrGAzXVgCUUmJb8iE6+g30NA0wDmiIXixVHucIqj87NS5+LSIn4rfcda7pOYFmIkvMT0sjzd72tfmN71TvTa8Yvxg5ziJCW+gsvdG7DBQ/eS5lbInvgVZudeMmlGzRfLHGNRe+CMqcmNO94eDGKb8RdTGaMFFFco= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785384277; c=relaxed/simple; bh=3IPcurqQZ46ZtNNs2p1uuhLkL3TOCRKQVtBQzQNd8Qc=; h=Message-ID:Date:Subject:To:CC:References:From:In-Reply-To: Content-Type:MIME-Version; b=FmuQRc+e7MvJJ8KALISjnnJYeL/9uyBImjNoixFCC1tg504q2/E0VdnfYlkRyF6gC7NcYFYKtvCtZ5EJrwixXXFwHqpN20R1QpupZIet+ILydhRHKMuqsTi58HuZ5TTXB7TVOoe3RXk/Tzr9vczpMeDGC96suIYCVjHFx7vBwFA= ARC-Authentication-Results:i=2; 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=fKe16cKg; arc=fail 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="fKe16cKg" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1785384276; x=1816920276; h=message-id:date:subject:to:cc:references:from: in-reply-to:content-transfer-encoding:mime-version; bh=3IPcurqQZ46ZtNNs2p1uuhLkL3TOCRKQVtBQzQNd8Qc=; b=fKe16cKg8Pr9gHmnvXoO+MTdmVVyV7FZ1GfG0vrkumj2O1T1V2H6VLx7 wj5fsQfVLHMPyD5zeXwsG+XPx6lwK5z3F2leA9NhA6/+f7BLxX7cHgyQg uW6ePsCr/6iUnJvth3DE1kqtz1jf9bZ3bBJW9klvy8GZK5Vxa+u/6GbdA sO3CZ0sdEQPhpZi5f2E05/QVbNz1FD6GpmhRaDHXTXUzX6doCCtWfWUfN NjMpUNMbnGt1IyvKA0zbqeF356iWifNFso67eQA+7KQX39J6SUc7lsQmx V9Eurlbx8s2XcfiR7G04JVCejlTAC9lK4zggU1WpmY6ia80sr4AzUss3B A==; X-CSE-ConnectionGUID: up5qZJc2TvqZc1DJqrBloA== X-CSE-MsgGUID: iKv1MuTbSUu/viZz9KGfNQ== X-IronPort-AV: E=McAfee;i="6800,10657,11859"; a="103402426" X-IronPort-AV: E=Sophos;i="6.25,193,1779174000"; d="scan'208";a="103402426" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Jul 2026 21:04:35 -0700 X-CSE-ConnectionGUID: zK6dN3OmTKis03OQ93Bjwg== X-CSE-MsgGUID: Mi93aPpcQ6aD/mVQiQ/Hhg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.25,193,1779174000"; d="scan'208";a="264003108" Received: from fmsmsx902.amr.corp.intel.com ([10.18.126.91]) by orviesa004.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Jul 2026 21:04:34 -0700 Received: from FMSMSX902.amr.corp.intel.com (10.18.126.91) by fmsmsx902.amr.corp.intel.com (10.18.126.91) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Wed, 29 Jul 2026 21:04:33 -0700 Received: from fmsedg901.ED.cps.intel.com (10.1.192.143) by FMSMSX902.amr.corp.intel.com (10.18.126.91) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45 via Frontend Transport; Wed, 29 Jul 2026 21:04:33 -0700 Received: from SA9PR02CU001.outbound.protection.outlook.com (40.93.196.26) by edgegateway.intel.com (192.55.55.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.45; Wed, 29 Jul 2026 21:04:30 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=UJS8ZRxXLGuDbvekUe0PbtDdNFosGXqmc4S69NpONqytMcV/nYrFeQ9eeUQo98bPM40jgJ2fX+9u+jvVJTsNuX7pDhu2IYNzTQANsELk3j1PuKB+EwfKYPfzNjLlEYGNGBRyT1Ya8UaSX36JukHX0S8PDHCMVmV303fkZTjVjWBlFltjxqpYGhkPNjpBBvKAu9tyjOzPzr09bGy9XKltZ7/lJjZxg+1m8E9i5s1aNNQZvJz98FsCR/tQ6svhYr7ZJwGuKYqGKFcmUfGqs8tbs4lsOOpgBebWX/LYtK+HCkTV3OGm+QQ6/ov9w/lWIuHqg+B7lpknMg0Ib0L/HtkrpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ImqZWhrD270TThTyIcP3SENcCyIIdEGVZYkLcd6LBDg=; b=ykxTd7V7QVAKXU+l36bQWq7RxxTHaCiyyimR3+NH4ctJVRQFgQnsru9EL/DQpPkX85VuszUDgs2Eljpz5rNZWeydwdkAMn3/7cGr8aID9n1F8fKO4zY5Q7hS7KgvvJE6vD7qgYjUKaWEqQaLxZCpyt/vV5PdmcA6n3WZb90m746RWEXRIZRMSvF7vvg/R9OCvzugYK97G0M+3hu7bVhP9lem3jqt7+17mYnxXJGsdDXQHjUWzTuhYevwjK+8PC3L6Zw+RKAlYYHzvcSSo6xeY2wliykKJJ+n0W0SnFuDzyXpjsUqBdm0quYkUnWQap4+K4H9l8pPzmR2jE3so9U1LQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; Received: from BN0PR11MB5709.namprd11.prod.outlook.com (2603:10b6:408:148::6) by BL1PR11MB6052.namprd11.prod.outlook.com (2603:10b6:208:394::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.270.13; Thu, 30 Jul 2026 04:04:28 +0000 Received: from BN0PR11MB5709.namprd11.prod.outlook.com ([fe80::ad31:3f30:20b8:26c]) by BN0PR11MB5709.namprd11.prod.outlook.com ([fe80::ad31:3f30:20b8:26c%4]) with mapi id 15.21.0270.012; Thu, 30 Jul 2026 04:04:28 +0000 Message-ID: Date: Thu, 30 Jul 2026 09:34:18 +0530 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4] mm/migrate_device: Clear stale mapping after freeing swapcache To: "David Hildenbrand (Arm)" , , CC: , , , , , , , , , References: <20260728062832.1107127-1-arvind.yadav@intel.com> <8d50f94b-6d95-4967-aa1f-2c14333ffd85@kernel.org> Content-Language: en-US From: "Yadav, Arvind" In-Reply-To: <8d50f94b-6d95-4967-aa1f-2c14333ffd85@kernel.org> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-ClientProxiedBy: MA0PR01CA0072.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a01:ad::19) To BN0PR11MB5709.namprd11.prod.outlook.com (2603:10b6:408:148::6) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN0PR11MB5709:EE_|BL1PR11MB6052:EE_ X-MS-Office365-Filtering-Correlation-Id: 4e7ddab2-d1e5-410e-a480-08deedefa641 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|23010399003|376014|7416014|5023799004|11063799006|56012099006|10067099003|4143699003|6133799003|22082099003|18002099003; X-Microsoft-Antispam-Message-Info: lk6PXy6e1HjCRbbObSJcWx19OrBYR49l8bm1Ms1C1YMFShcOfccBxRQw74qcX5F1NU7k1xcIx9I+pUSL4gYoU0eTNX57UazrmU79LscNQjBM9hw/gTUUDznsxbU/DPg15r62raaYtd1SfFJdWJ62zUf9e6sgSNNmuJ2eNYZnBInxTvP0bVi7Gg7khFPZvY0dm2YibxVRxIqd3dGNnftS6Jg22rdcvYrNGfBPzDbEUqeKKfaEwmV/+XRz06K0yE0H/69Pr8nJGbki3OfDY7j0iRo+CQoD3bJHPZXFGPOyRk4v20VpcfUs5qv5xMRBHcmCI0I+sjXgJDjED94MPN5Tpxc8Gav7fmLLSnebc3DspLbiCKYBvbNpmcAFA16tNu3yP3HXhWr+JyTsa0NVtIQ8PKJDipM1b/Ej5DBwFsA8vBWjTwQVrxrz2k2JFVOIxh7PtzzKaT+rYmIZWXm+d0Xb9MhgWO2XSTEoVVQEWWso6euGC8xez1pFNUHi8Nu+5rj7ZiEv/9sBR0I08bRQNehWfXsm9lZFdrY+XnkdMoWrQ/h9AnGHOxM5ni84dGRUJBH26+EWh8ctUNHoxgU8lfF4n5nAewdu98Eipbb1tLUHN0JgbfupL7488Ai5KcHG9icK+5J2OuZT4DParDUhn7xAsr3H5pk6h+r1K4jaV3TXDGA= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BN0PR11MB5709.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(23010399003)(376014)(7416014)(5023799004)(11063799006)(56012099006)(10067099003)(4143699003)(6133799003)(22082099003)(18002099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?d2hweHRibDBvRDQyL3J3enZucU9sVDBpTWcyR29oaFhrS0NEN2g3b2NJSGRy?= =?utf-8?B?TG0zS1hKOVN5VDFoODh4T2MvYStYSmFqU1cxM2xiVnNWMjk1bFZvU09wd0tC?= =?utf-8?B?OC9KRUJ5NE1yamo4TW5NVUFDK1dmblhDYWVaQ0t0MVIyS3A5M1A5L2hnOGZY?= =?utf-8?B?elpvTDdmVDJvQ1lXNHp6TGhtUnZlNFNYQmlGT002OExIMGhHNzZoY3lyVlh2?= =?utf-8?B?RWY2UWRwZERNdC9zUEhLQWJMWmJQVlVudDMxZTRPMUxRL0hCa2NKSlpkSmpV?= =?utf-8?B?Mnd3em5KNE9BWXhENDVtNnVPM09qb21vUDRXcEtoR0hxR2dqOXhlbVhPZWFp?= =?utf-8?B?cHZ0MytteEgyUXNPd3pHZ0hBU3BqanFJRGJWdUtESmI3NXRhZlEzMVFwSi9i?= =?utf-8?B?L3VzSWNXdXFXTHhWdXFHbC9QbVgvUVB0clBoSUNLWXU2d09jdEV4b0M2U2N0?= =?utf-8?B?VGZ1WUNLVDR6OEExY21OU0dvcDdETVZJbUwxZ1V6S2FxWm9kbi95Y0pQU1o0?= =?utf-8?B?dTNNUXVEaWE1dnNLVzhPczdhbF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utingPolicyChecked: BqYG8ss2KRJ+M82whKR33/9DTXW4EklOPn2gbHiW72CzkTVlKPVUemfpKt/6yHHUTIzAkLZj6kxvgjvEnPpf8tL2OMgHETAhWwaWK+UXxjHTm7b6CSyCpYP4/UgYGNUWXbSeGsdxg49/NyYXXedqD3XQE1WQDXjREXdPQlzL2Kx3vjh1rvE/hPFZbtK8sdYOGUMzUbJ8nyMhuF+wuIbzXfHMDens9xc3ZpcPEOk24udEKjsw3jmFDM21dCPAZE5ftoJ2QWAU1gLI1XGpYiiGuXXdSIyHFOMk8H3poElg46jyrLZovfi2Ggb9NzMSnhIzF9c4mlFu8d+FxcDg3Rrrrw== X-MS-Exchange-CrossTenant-Network-Message-Id: 4e7ddab2-d1e5-410e-a480-08deedefa641 X-MS-Exchange-CrossTenant-AuthSource: BN0PR11MB5709.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Jul 2026 04:04:28.1488 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: MLx5fyqMBolRO1qA54GOw5KkBLRumgxVKuIUf1H4keYiRBaiIQnbisqjxJJkkYyK3qZh8MkbWZRhBu6/WVPRqw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR11MB6052 X-OriginatorOrg: intel.com On 28-07-2026 18:20, David Hildenbrand (Arm) wrote: > On 7/28/26 08:28, Arvind Yadav wrote: >> __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 *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; > This function is surely a beauty. (had to rephrase that sentence 3 times ;) ) > > migrate_vma_split_unmapped_folio() modifies the src_pfns() entries on success. > (and somehow assumes that it's always a THP, what? After a MIGRATE_PFN_COMPOUND > value is set? What? Why the "nr = 1 << folio_order(folio);" in the caller). > > If we ended up modifying the current entry in such a way, shouldn't we just have > retry: label and restart at the very top of the function, where we just > naturally re-read the entry/page/folio and do the right thing? Agreed. Since migrate_vma_split_unmapped_folio() rewrites src_pfns[], I will retry the current entry so all state is re-read. I will keep nr set to the original folio size for the split-failure path, where goto next must skip the complete folio range. > >> } 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 *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; > While this looks good, I do wonder why do we have to supply the mapping here at all? > > Is there a path where we call folio_migrate_mapping() and the old folio (folio) > does no longer have the right mapping attached? > > It would be a lot less error prone if the function would just obtain the mapping > from the old folio. Agreed. I will make folio_migrate_mapping() obtain the mapping from the source folio and update its in-tree callers. Since this changes an exported helper, I will send it as a separate patch in v5. Please correct me if I misunderstood your suggestion. Thanks, Arvind >