Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=40.93.196.29 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796113890; b=Ys7scLImnskdzJ53N2jc06VE1YZQ6/phXzJIYKiHusr0N+lcB/A27X4H/VlF8l++JpM1 5NDFhqDDaM5qlKbP6pefpvAjQcIspb5DPhaSa5ms/B/zZwbvDEUlZyfvEMOfaIvDxxsfr wCQi2/0bsDxyvppXKvJP8OG5i7vHfmKJn/UZxOpKC4VfgZSxew74n1UTo163fE+53QwQH CAKoiNQViRcNqRxqkUCTOMvrUlx7l8LR5f1SxFk1KnZtuJ2+hxsr6+XexaQVXXVhSVDAD GLb9kX3Ea5ZpM9AL9ul+fejthaWm4wByMtczmzZeQF3VSvRNfQH0eBJRNi/sKQTWx4w== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796113890; h=DMARC-Filter:DKIM-Signature:Received:Received: X-MS-Exchange-Authentication-Results:Received-SPF:Received:Received: From:Date:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding: Message-ID:References:In-Reply-To:To:CC:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Originating-IP: X-ClientProxiedBy:X-EOPAttributedMessage:X-MS-PublicTrafficType: X-MS-TrafficTypeDiagnostic:X-MS-Office365-Filtering-Correlation-Id: X-MS-Exchange-SenderADCheck:X-MS-Exchange-AntiSpam-Relay: X-Microsoft-Antispam:X-Microsoft-Antispam-Message-Info: X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-OriginatorOrg: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-Id: X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=cXTTq7q4BaK5V5vnTxzr4X1SUK1vrJuCGyFODlE+5IU=; b=DeRe7DrktoXUpatAFGZlRdnTV70uBp3ox1GL3XWa6aBuJ4Z3OJM4IEZ9l12727TE4W0c jp9Shd0Uwc8cOTlAG6CRT9PezWt9mez47UL5F2CNSTYFFU3RhZVqUo73VeBm3vbojn2iP 5RqRtjRhyhFBKlA9EnYVmfBW9iX20mS15n6B/ARZysJSOqMkz66t0LelFcCh8igFBxnbm Yhp+zwRfGrCjYwjEroYAEjGPI0nxvpsCKaQxYiD5zz1U9MrDil4QghXbq3gurHkwFBa2f Ye719pjFnXOVzprR3pOf7b3+A0rlS11GSSReT7FvRosHQwsV9Q4b9TmD9aepFZBt64g== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=V2crasN8; arc=pass header.oldest-pass=0 smtp.remote-ip=40.93.196.29 Received: from SA9PR02CU001.outbound.protection.outlook.com (mail-southcentralusazon11013029.outbound.protection.outlook.com [40.93.196.29]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (secp384r1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A6A78606A31A for ; Tue, 1 Dec 2026 08:31:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A6A78606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=V2crasN8 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ZJ6f+Q3Au00w0iPYDl0C4ALYr8hVxnZVsY4h1YAqn81mbMXmxBgaGb93Nsp1Ou6ujdTViXXW419q/nkobbWlSZO3zd+VyHYPZz4OH+MF15ysgkHIX6fdxpjhtZU5I73cyy89hQrnPIsysnGorBNZChdbvSWiEGCEqepdFA7HfTyAxFXf7hTCtIOpRrHlNg1VF+oY7hGh97IjdfXmZXZMLdcW3cDDjJggiFWIueB5+iMWIlOrEtS4W+zRPeJezXTRn+CEUUtAENfuGHKA9cXJ0gHIrZLLZX8abTgnBzDcbIRbTCMtwdpNrasavwezfR++j28KdU216KR1P60o/6oAdA== 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=cXTTq7q4BaK5V5vnTxzr4X1SUK1vrJuCGyFODlE+5IU=; b=LL/qOy+u68VVpsqrHjpbO3QPyFIVLT8OkHX2PegtgnjPOG9iq1MZwhFGWSZsgj6OXOeqbpOBHmrJfGM2XDbNNgp7ZbnLUCEQx7ShXcvgowiFJ3mwNztXv+spaQjwAaHRxf8ZVXocMHV4Mct8z/n9U6c+0malVzuSotPTo3rX8CbP+0pyxXto3vVqI7j/o8frW6Xx1Vg/LTL2syA0OTg7Svn3rT+xB0MD8eyZ0JBpf96dWLJAfCvrT7pDn6EfbhsUgjmRCME7bDoYqvCe8KWXB+D7Tvh9Uwmtrc51VIErYEZmJFWz94re3DKkS7BzLHUO/4qb46UxVWIbAU7+LN73/A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=oracle.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=cXTTq7q4BaK5V5vnTxzr4X1SUK1vrJuCGyFODlE+5IU=; b=V2crasN8a+9gjG+eFc+Zafp6sGrf0kHZa0RnQuSVsDjMbCbd4FvuUMCijevanISqwrc9IP6fXQHYYqejX0G3xjoO8TGBujgh0OdrrpVDGg9ZaTVJzBz13gNOKGQX7GycHbaTB6BkFMw6ndsO0/iFQjkTnmFQvnplaE0vqron0bk= Received: from MN2PR04CA0025.namprd04.prod.outlook.com (2603:10b6:208:d4::21) by SJ5PPF0170DF9F2.namprd12.prod.outlook.com (2603:10b6:a0f:fc02::985) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.615.8; Tue, 1 Dec 2026 08:31:24 +0000 Received: from BL4PEPF000003F5.namprd04.prod.outlook.com (2603:10b6:208:d4:cafe::9b) by MN2PR04CA0025.outlook.office365.com (2603:10b6:208:d4::21) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.688.10 via Frontend Transport; Tue, 1 Dec 2026 08:31:23 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by BL4PEPF000003F5.mail.protection.outlook.com (10.167.249.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.5 via Frontend Transport; Tue, 1 Dec 2026 08:31:23 +0000 Received: from [127.0.1.1] (10.180.168.240) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Tue, 1 Dec 2026 02:31:14 -0600 From: Shivank Garg Date: Tue, 1 Dec 2026 08:29:51 +0000 Subject: [PATCH v4 5/6] mm: factor out the locality range computation from folio_zero_user() 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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-ID: <20261201-batpagecopy-v4-5-dd82a573e573@amd.com> References: <20261201-batpagecopy-v4-0-dd82a573e573@amd.com> In-Reply-To: <20261201-batpagecopy-v4-0-dd82a573e573@amd.com> To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Borislav Petkov , Ankur Arora , Dave Hansen , "Thomas Gleixner" , Bharata B Rao , "Hrushikesh Salunke" , Ingo Molnar , "H . Peter Anvin" , David Laight , David Rientjes , Linus Torvalds , "Teja Vojjala" , Huan Nguyen , "Pedro Falcato" , Peter Zijlstra , Zi Yan , Baolin Wang , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Kiryl Shutsemau , Ying Huang CC: , , , Shivank Garg X-Mailer: b4 0.17-dev X-Developer-Signature: v=1; a=ed25519-sha256; t=1796113822; l=3779; i=shivankg@amd.com; s=20260518; h=from:subject:message-id; bh=iufxTiIITPPlc8L/pQtIDytG8QGtq3f3DSqFS+yFFhM=; b=OvVbFIFi1bgbMYa6VLGfPooCz3Z9TEMVCJZE+ZSZaWr9TTKcWg3wTy84F5KGTEzuLj+TGIrL/ A23n+GfmD38BQll021BsWkoouzODLaLVHht6ZuLwd4gCXPmuI3v+Mim X-Developer-Key: i=shivankg@amd.com; a=ed25519; pk=2l2QGTeXuGkZTtfmx0nPQU8iFZfjYmX/ymMojitevx4= X-ClientProxiedBy: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL4PEPF000003F5:EE_|SJ5PPF0170DF9F2:EE_ X-MS-Office365-Filtering-Correlation-Id: 883f6a0a-cedd-472f-7783-08df4f857bef X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|23010399003|36860700016|376014|1800799024|82310400026|921020|261026121527099003|261102123227099003|261021121427099003|56012099006|11063799006|261021123027099003|261009223027099003|22082099003|18002099003|10067099003; X-Microsoft-Antispam-Message-Info: sU/YhlxmcxHB3rst4MrVESeOjZMgjvnXjBa6TF9i2GppxgK2dDGYnssMjnoJi2CfP6FlsFbox6gUU0LZu2ht9F4IuP7xgzrComefxLn/rRwltAVlgSO9BARlQYuPedMYmsZTYSrS9WyV9zrgqO15gw/SzslE1oDBOBxji51HVm+r1LkgViuhgJqDb+IBMd0zGsJ53MjgUuluWFMpSDJK/FraQzTUkihAVedOyAIa+nEUT2GsTRINtXzxFiq/JgaRo3KIaAFQJ7ZV86R0YWDAGpzgzCoAWRU4BL3gyyE8NUG37F5/fSuFYUH6eGXWCzZGvhx1xmbFJxhRvIUrOnFPaYDSWPYpjh4k+JF+NICNm/tAtEQqM1YXcPmZLgvicrxzNAlZ3Sil+oXy/I4FqPsjQWXoNccvsA2L4rpRBisxPy1RbNpd24IJlXHyOB7LNdZVaVna3gzasH/TV1lUeWmAIQdt9Xr5GVnUwFbc69lDoNyqnyZTfGj/Z6VN0+57jNtFWTFO9dB/pg74/BN6JZmX3W82JhR9muPl2zhsVtYISTl+prnS6mzbfYmqPY0Lz2D3V3zaVruQChecLCSxVHBNuIhC/JocgP6NeaV4Uz4frEvLv/0xdqNP3df0liWPUschbYR7shxseZg6h41diZeBLU5lI1rEtpJMiLZtsStsb8+jcg+Zyqr7iuE7vB/1sjpKi3zRwZE7lHICf7jpaO7b3PvEqnN1DXVGGcan8n2/Loz+Gy93eseSI1aV5gMKVqdB X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(7416014)(23010399003)(36860700016)(376014)(1800799024)(82310400026)(921020)(261026121527099003)(261102123227099003)(261021121427099003)(56012099006)(11063799006)(261021123027099003)(261009223027099003)(22082099003)(18002099003)(10067099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 6V5i0CQItshuYq3gT+XTCD0OYQrCt+0Yt9Cef029sNB3iwBjtdWsCxBlxmgr9GOxaR1pfX6d8uxvH7VUMlU/fBvyfgoOO1apkSsc2heNF1p+h7IrYFJJTIVUnxqxkTyBogkRW12MSJbWFB4bYKxz4+Cjxp3cLrrWILlVccp6v1FV6PdcYIUQhzkcWsplmgnwIHPxDlRwtwJ3nUCszBBeHP4MwxipAxicV4n7uqi+iMcsJH83eDwPZD+4rPcAaMLnL9MB3GliJNFAqIbAnAvlj79BOocTZIV7INaIUV9Tk51WgWAmZvQvljTBz38LLAdgXoi3TnR6EhtojLDp6BvOzlN7Xt0hsNYW5fYQY/IE+qGr4NpaIDUp2GLK4aHmJZSJA+PubmOThDiGfKmXcwVC0RgdRHgJNZ6782vu5QTptZen2WoaJaN7iq9em8Rzzuvt X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Dec 2026 08:31:23.7987 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 883f6a0a-cedd-472f-7783-08df4f857bef X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[satlexmb07.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BL4PEPF000003F5.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ5PPF0170DF9F2 folio_zero_user() splits a folio into three page ranges around the faulting page and clears them in order, so that the faulting page and its neighbours are cleared last and stay cache hot. Copying a folio to be mapped at a fault has the same locality requirement, and an upcoming patch converts copy_user_large_folio() to the same scheme. Move the range computation into folio_user_ranges() so that both can share it. Rename FOLIO_ZERO_LOCALITY_RADIUS to FOLIO_LOCALITY_RADIUS and generalise the comment above it, as it now describes both. No functional change. Signed-off-by: Shivank Garg --- mm/memory.c | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index dfa307a9228c..bd2c276e83aa 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -7767,31 +7767,35 @@ static void clear_contig_highpages(struct page *page, unsigned long addr, } /* - * When zeroing a folio, we want to differentiate between pages in the - * vicinity of the faulting address where we have spatial and temporal + * When zeroing or copying a folio, we want to differentiate between pages in + * the vicinity of the faulting address where we have spatial and temporal * locality, and those far away where we don't. * * Use a radius of 2 for determining the local neighbourhood. */ -#define FOLIO_ZERO_LOCALITY_RADIUS 2 +#define FOLIO_LOCALITY_RADIUS 2 -/** - * folio_zero_user - Zero a folio which will be mapped to userspace. - * @folio: The folio to zero. - * @addr_hint: The address accessed by the user or the base address. +/* + * Split a folio into the page index ranges to process, in order: + * + * r[0]: pages to the right of the faulting page's neighbourhood + * r[1]: pages to the left of the faulting page's neighbourhood + * r[2]: the faulting page and its immediate neighbourhood, to be processed + * last to keep its cachelines hot + * + * A range is empty if its length, range_len(), is 0. */ -void folio_zero_user(struct folio *folio, unsigned long addr_hint) +static void folio_user_ranges(struct folio *folio, unsigned long addr_hint, + struct range r[3]) { const unsigned long base_addr = ALIGN_DOWN(addr_hint, folio_size(folio)); const long fault_idx = (addr_hint - base_addr) / PAGE_SIZE; const struct range pg = DEFINE_RANGE(0, folio_nr_pages(folio) - 1); - const long radius = FOLIO_ZERO_LOCALITY_RADIUS; - struct range r[3]; - int i; + const long radius = FOLIO_LOCALITY_RADIUS; /* - * Faulting page and its immediate neighbourhood. Will be cleared at the - * end to keep its cachelines hot. + * Faulting page and its immediate neighbourhood. Will be processed at + * the end to keep its cachelines hot. */ r[2] = DEFINE_RANGE(fault_idx - radius < (long)pg.start ? pg.start : fault_idx - radius, fault_idx + radius > (long)pg.end ? pg.end : fault_idx + radius); @@ -7802,6 +7806,20 @@ void folio_zero_user(struct folio *folio, unsigned long addr_hint) /* Region to the right of the fault: always valid for the common fault_idx=0 case. */ r[0] = DEFINE_RANGE(r[2].end + 1, pg.end); +} + +/** + * folio_zero_user - Zero a folio which will be mapped to userspace. + * @folio: The folio to zero. + * @addr_hint: The address accessed by the user or the base address. + */ +void folio_zero_user(struct folio *folio, unsigned long addr_hint) +{ + const unsigned long base_addr = ALIGN_DOWN(addr_hint, folio_size(folio)); + struct range r[3]; + int i; + + folio_user_ranges(folio, addr_hint, r); for (i = 0; i < ARRAY_SIZE(r); i++) { const unsigned long addr = base_addr + r[i].start * PAGE_SIZE; -- 2.53.0