From patchwork Fri Dec 6 14:19:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 13897272 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C4D52E77173 for ; Fri, 6 Dec 2024 14:20:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3AFAF6B0286; Fri, 6 Dec 2024 09:20:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 35FB96B0287; Fri, 6 Dec 2024 09:20:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1B3906B0288; Fri, 6 Dec 2024 09:20:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id F020F6B0286 for ; Fri, 6 Dec 2024 09:20:04 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A27581A1A03 for ; Fri, 6 Dec 2024 14:20:04 +0000 (UTC) X-FDA: 82864742772.03.74E9DE2 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2047.outbound.protection.outlook.com [40.107.244.47]) by imf04.hostedemail.com (Postfix) with ESMTP id 960D64001D for ; Fri, 6 Dec 2024 14:19:43 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=t13ZB2n6; spf=pass (imf04.hostedemail.com: domain of ziy@nvidia.com designates 40.107.244.47 as permitted sender) smtp.mailfrom=ziy@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733494783; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=oH/1z1D1la75uGl+GAjbHsZC5L78NRLB7tYMdFUSxKA=; b=L6tB9n/xYRFreZgmmf5othoPuvcFJJhGC1sCfuolN5ql4rWnOEIqOKiMO6PD9vSnyaJozo 1Jpb4KH9CPTNGm8YOs7lZ+6JBUuMZ3GaKTqUWSv6TkQNiYbIt3jZpdRa5eLlkNuRMwPM+u 2XfSmosA3+qPqaQegjtgvBepL1BlaGs= ARC-Authentication-Results: i=2; imf04.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=t13ZB2n6; spf=pass (imf04.hostedemail.com: domain of ziy@nvidia.com designates 40.107.244.47 as permitted sender) smtp.mailfrom=ziy@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1733494783; a=rsa-sha256; cv=pass; b=tYF5usB4WtkhdNwDONYjYEdlD75uzg9hp+8NuM/jUVf3TuAeC0I1Z+V8RCrtROgw9RanUG AD2NNamp2JevjeTachovINRrgZCrRsdtyX9VT18BCl7iHxURmrLdYBBTs/zyz8/m6G2bdK JrhKC+7hpzUJtGl13koZY6Csc41WqVQ= ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=inztBfE66z5ffmSA4m3eFSP9XNbhBOzvU2Emb0wY05tjTv01zfL567M//ImwrGCu8xOEfXg7OBkTSXueQfbTOT97IPmuEvR3DJMs1KOe3jYRlJ2ns0731kCleJXvMmyUjWBpN+VIVFUK/jC1tb5xBBh6uV3aZDBvPborY4clJkoX/GmgfYmdzTQkQy2jj7zhCUvi7y36eVujbNqNxBJLD9fnBYVUgAdVkSbbhiwBrgO2/lrdAXLaHpuTOu1Dp/v+Rolhgd5H99OpXv5xaQ0X+iRMTe9GGfwiWVnS3qDFXzS7+w5SIqCsSR5sY2n1f7iJLk87p5Vx8YxvCHDEMVo+nQ== 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=oH/1z1D1la75uGl+GAjbHsZC5L78NRLB7tYMdFUSxKA=; b=W6Hzyw5B5SMoWLfBA8ooVi/WTKJT0B2XivizcqKlS/Sefzjt7w6pZKFcZghzlevrhSDS7O48y0HLDN5gFCJ1K027r5Ax5zdYqn8kRyM+xx+U3demGaZlQ7/7T5R55G8VQf1EBKuBXQcE8HdVWuVxUzEqnf7dG1VJOzqrdkkQVcWCbgXcb+JuvMFl8MXBLcFn+TTUmzXWg2EkJA/E38j2OhjOUsStkbVRGLQxCr29caf9dZo2xsiJLf1h9PqFC2vZP5pGFScj0rsbyofLHJ7YRkAQRh7eKGQA+ILSeyR1NhYjr7RuQLnGghipbqFYTv2v2wRaZM6vGTi2nJl0FVoFyA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=oH/1z1D1la75uGl+GAjbHsZC5L78NRLB7tYMdFUSxKA=; b=t13ZB2n6QetsoCMBcTgBlaL2pF4pLHMt/Gzeggut/OGaHz7ijkMlRt7e6Bx5cepGkoKeXACc6YB9bLq70aaZ09XrfcavKpguL7YudVUmnvY62QuG+BUsyT2ppuS8M8i+Gv7foKvwuNBEGY8UBK3gjlxozvjJnfqkeEoXVolDnXVkpJ2uCuptq0f+9k3HEpLWH6TZBoMCRDGBPWOv5c2kgNTbCmiNeRSZ17bPR8B7q0sWI53Ix/iCXS7r/V4hCe4n1nL+0Axz0dji49PXEeNxbAjTGJf87w9FatIpjuFQwy2xWOs3j3vbuxY4qnsLiP74ZtaKWDDRR/go/ejoERGTEg== Received: from DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) by DS7PR12MB6312.namprd12.prod.outlook.com (2603:10b6:8:93::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8230.11; Fri, 6 Dec 2024 14:19:54 +0000 Received: from DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a]) by DS7PR12MB9473.namprd12.prod.outlook.com ([fe80::5189:ecec:d84a:133a%3]) with mapi id 15.20.8230.010; Fri, 6 Dec 2024 14:19:54 +0000 From: Zi Yan To: linux-mm@kvack.org, Andrew Morton , Geert Uytterhoeven Cc: Vlastimil Babka , David Hildenbrand , "Matthew Wilcox (Oracle)" , Miaohe Lin , Kefeng Wang , John Hubbard , "Huang, Ying" , Ryan Roberts , Alexander Potapenko , Kees Cook , linux-kernel@vger.kernel.org, Zi Yan , Geert Uytterhoeven Subject: [PATCH] mm: use clear_user_(high)page() for arch with special user folio handling Date: Fri, 6 Dec 2024 09:19:38 -0500 Message-ID: <20241206141938.2750393-1-ziy@nvidia.com> X-Mailer: git-send-email 2.45.2 X-ClientProxiedBy: MN2PR16CA0051.namprd16.prod.outlook.com (2603:10b6:208:234::20) To DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS7PR12MB9473:EE_|DS7PR12MB6312:EE_ X-MS-Office365-Filtering-Correlation-Id: fffdb609-1176-4c33-f70e-08dd16010e03 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|376014|366016|1800799024|10070799003; X-Microsoft-Antispam-Message-Info: FigxMTUc7R5W0HGY1m+xGzjSRThIRmDmub0SJca6woG8i/0IYSvjZpV5qsAX6CmUfxZT7z/YoMom+XJi73/FvKY1t/vz+IwUzi6qVEquJwTRCC0t51Hq44Wuxu9Hj7q+Kv55yp7IppYxRLrU2m/h89nUGgwe+7X7GjtA4WbAO5H6n3TLW9fzioYr8H7OWbUsi8O6Z+l5IQqQHIbqKIYvLVnqgXI+ipKaF4V7XU8abFJabCjGnDbQf5ASbHpB8tkaFJ0yPAYIdtL2HygBycePIbUz2W2ZDYSlJgsxdMhyM8hnXzpaVwHzOGuC8OhlSudtDUyvz57JqI7DEpa8eE3Sy9Nt5RjobPP90LhKb23fx7xwHBNbFxXy7eGNxMe1UHQyovOEjECzF3ifSHsQ0HRr3pQYspK/0Gd4BI5IiJNfH+muJmfkQAQtYJv/0Fud4XWTqTVh2DggiYmDcZkAotvbraQ2KtX1nA00s717gJ+dSV/qZlZ0V7noOAWwf2Egzo8HoAGuYDLbAiX7BZQ6eMHNwEJns/FMWUwY6YcaRNj704eTmNhYQxoHAnxsiWy4a7t8lfl/lx3QSrwMtmgQZeK5D9l7rtljNgmIP8O1v73Bk3rAdmPTqCKzabuBTXDMNY70Q+hbAXOm2oPN9dOiEykfRqNLfl3sXLp5BMT4SLW1N4NQOxOr+eP/76+uorfM7PVrZfAXf6aU6r7Lynkac/wq/pXq9NFqDzHIJ6SOjbSTO9RujiD45erz8PJp2KX6UB2hrUU55SVKIRfEujO65lI0hDeF6iiw6w6BsZW8vkQARUCKSucdmCVV753QFku9/k8TKQPFInqAzuQUZYMjd25LLfg6Fj1YpLETjXDm1joYln05VPF44H0E3FSgmiBVrohM0fqhJdUrBoqlrD7uK4qCGPHnA0qMmM9ijyrxphCJOgIDOSGQh0e30KW3cUyk1KdVcU7WSk7WaVZkgTROyiZ2nMWomQwaVhhNz7kNq2cwJURuRB6fi1aNmWWc8nmkQPnXAkbryImjUJe4A4i2o76VfkIZXTF5fRbtlSg3ewTAGcygUFDYKOKxC5QdwkE5gN8KDVQpEWXVezRLPZLuUL+5K5fmvxrKTQHo/xLhocsmxbmWcYZBfXpAqjib2MDgGy0od0YOUsXk2wd50p/GaI9/LflO/cLyyFBuoPGDu8dqa9eT/gGM/k0Dz/oKmyf9/Gn6JKHG3rFGBPZ/gJp6XnIaHidesRAjkVeXdAnWRLoZ8h8dttb917ut8O58mA2ZY4baQaeRMKOh3tQG99+sLOLfuIjdQWPByHF3PWpRQ8TOh82XacgFQbMV1EKyMVD5XGeWhECLhE8Pzn021sKzCnsCE67iJ6wAdES080KJ3zm2XIw= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS7PR12MB9473.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(7416014)(376014)(366016)(1800799024)(10070799003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: aAgoiQN8kg7r4mn84jYKblgSEeU8hXR70nj+xFLDJ91OTITLdGZDihC25/i939JD+u0SaZKCNjXJ0ImO4kt984hi8rFSBGfJRbOHL4+tMSDPSSACP0JblhMyArJmMmCxqnyNCjGfcc7Qhk/3P3GdJ/LRw8ChlQbBDM+mf4iwENs5Tzxr+WpaP4JulpK2rckybhjdqYH225bcJH6GKPL9Uekx/tz/8bnlqRhWXoQPl8Y0Bp6U8BHaeOj6HJzZP0NVZ6hs2w6WjyFVGij0BMQLqNcCQWL7khXXA4o10bkOgR9R9typKeCkeuTuyMRVd0H6IptK8U1EC0OctxWyhj0pKKar+nBIMYgFJW3/HcklQm4rbaHo/BvkGgiz5PsTFbp5BvOn1iNH1B7qNUcNyybwX9BTj3dXGYxmpLOo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wZhtD4Y7LT9gldXvRohyDOtUsZ7+VUX2gMuz9i2oTsf8CYDPk2155TeHAyITdd50Yf8iiBqvVaTW0Urh4ZaR1Zfu2D0kz8M/1sgFQ7x4DOuFG3B2ks+xW4E1Bc21R3DBHuZz6n2z6AdQO5eWdxeenJHVc1aKmZ6qk1pH1iYa X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: fffdb609-1176-4c33-f70e-08dd16010e03 X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Dec 2024 14:19:54.3773 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: ES0W/0Au4pZlILiirpIlCWyGGhAAuOgjaSAANAYkbIWYVgu2erGlhGgjnNE+UY0n X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6312 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 960D64001D X-Rspam-User: X-Stat-Signature: 6q5r4c3bftukk3crsywdgbbwmiqeu9ch X-HE-Tag: 1733494783-335076 X-HE-Meta: U2FsdGVkX1+gA2pBoEdC6IqkKkmQ69Hkwrv01mi3VwX5I54QI8CfZOKINPEM8/s8VlmusJW16wYN7CyQEWxbQv7dnd+yE1f0C/LhzJlps+vePqoZIwV9zzVgbA/73CRnKq6fIXq2BTSgsaypav0WAI9C4BEoEtlBODBU6XTU+7ViHJS+6hGwGkz1eJvrS3e6lCdBCaH8xkYQvJl79/T+ZlZLzy7yCsmPCI6SpNW1Q3URNTEAABkzzaiSzG/bke/7GSd3E5MyK197ggmFieJQZZ+tAg2MJ+mR7ERis7UDpT/CwnKVAbC7QHtzHrF3+fxmcX3+4DvkBa/FehMij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hgC/xXJuE9KrGZUpmWvk44dsa/EZyIpHq3KNRNn8m17aZ/JeM4dJ3K2qNmu48VMPXFaxtx9NE2f8N1rRSWTvEgHlvR0EsLkcNtgw= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: For architectures setting ARCH_HAS_CPU_CACHE_ALIASING, which requires flushing cache and arc, which sets folio->flags after clearing a user folio, __GFP_ZERO, which uses only clear_page(), is not enough to zero user folios and clear_user_(high)page() must be used. Otherwise, user data will be corrupted. Fix it by always clearing user folios with clear_user_(high)page() for CONFIG_ARCH_HAS_CPU_CACHE_ALIASING || CONFIG_ARC. Rename alloc_zeroed() to alloc_need_zeroing() and invert the logic to clarify its intend. Fixes: 5708d96da20b ("mm: avoid zeroing user movable page twice with init_on_alloc=1") Reported-by: Geert Uytterhoeven Closes: https://lore.kernel.org/linux-mm/CAMuHMdV1hRp_NtR5YnJo=HsfgKQeH91J537Gh4gKk3PFZhSkbA@mail.gmail.com/ Tested-by: Geert Uytterhoeven Signed-off-by: Zi Yan --- include/linux/highmem.h | 8 +++++++- include/linux/mm.h | 17 +++++++++++++++++ mm/huge_memory.c | 9 +++++---- mm/internal.h | 6 ------ mm/memory.c | 10 +++++----- 5 files changed, 34 insertions(+), 16 deletions(-) diff --git a/include/linux/highmem.h b/include/linux/highmem.h index 6e452bd8e7e3..d9beb8371daa 100644 --- a/include/linux/highmem.h +++ b/include/linux/highmem.h @@ -224,7 +224,13 @@ static inline struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma, unsigned long vaddr) { - return vma_alloc_folio(GFP_HIGHUSER_MOVABLE | __GFP_ZERO, 0, vma, vaddr); + struct folio *folio; + + folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, vaddr); + if (folio && alloc_need_zeroing()) + clear_user_highpage(&folio->page, vaddr); + + return folio; } #endif diff --git a/include/linux/mm.h b/include/linux/mm.h index c39c4945946c..6ac0308c4380 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4175,6 +4175,23 @@ static inline int do_mseal(unsigned long start, size_t len_in, unsigned long fla } #endif +/* + * alloc_need_zeroing checks if a user folio from page allocator needs to be + * zeroed or not. + */ +static inline bool alloc_need_zeroing(void) +{ + /* + * for user folios, arch with cache aliasing requires cache flush and + * arc sets folio->flags, so always return false to make caller use + * clear_user_page()/clear_user_highpage() + */ + return (IS_ENABLED(CONFIG_ARCH_HAS_CPU_CACHE_ALIASING) || + IS_ENABLED(CONFIG_ARC)) || + !static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, + &init_on_alloc); +} + int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status); int arch_set_shadow_stack_status(struct task_struct *t, unsigned long status); int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status); diff --git a/mm/huge_memory.c b/mm/huge_memory.c index ee335d96fc39..107130a5413a 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1176,11 +1176,12 @@ static struct folio *vma_alloc_anon_folio_pmd(struct vm_area_struct *vma, folio_throttle_swaprate(folio, gfp); /* - * When a folio is not zeroed during allocation (__GFP_ZERO not used), - * folio_zero_user() is used to make sure that the page corresponding - * to the faulting address will be hot in the cache after zeroing. + * When a folio is not zeroed during allocation (__GFP_ZERO not used) + * or user folios require special handling, folio_zero_user() is used to + * make sure that the page corresponding to the faulting address will be + * hot in the cache after zeroing. */ - if (!alloc_zeroed()) + if (alloc_need_zeroing()) folio_zero_user(folio, addr); /* * The memory barrier inside __folio_mark_uptodate makes sure that diff --git a/mm/internal.h b/mm/internal.h index cb8d8e8e3ffa..3bd08bafad04 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1285,12 +1285,6 @@ void touch_pud(struct vm_area_struct *vma, unsigned long addr, void touch_pmd(struct vm_area_struct *vma, unsigned long addr, pmd_t *pmd, bool write); -static inline bool alloc_zeroed(void) -{ - return static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, - &init_on_alloc); -} - /* * Parses a string with mem suffixes into its order. Useful to parse kernel * parameters. diff --git a/mm/memory.c b/mm/memory.c index 75c2dfd04f72..cf1611791856 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4733,12 +4733,12 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf) folio_throttle_swaprate(folio, gfp); /* * When a folio is not zeroed during allocation - * (__GFP_ZERO not used), folio_zero_user() is used - * to make sure that the page corresponding to the - * faulting address will be hot in the cache after - * zeroing. + * (__GFP_ZERO not used) or user folios require special + * handling, folio_zero_user() is used to make sure + * that the page corresponding to the faulting address + * will be hot in the cache after zeroing. */ - if (!alloc_zeroed()) + if (alloc_need_zeroing()) folio_zero_user(folio, vmf->address); return folio; }