From patchwork Fri Dec 6 17:42:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 13897538 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 22C2FE77173 for ; Fri, 6 Dec 2024 17:42:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A1C4B6B02B6; Fri, 6 Dec 2024 12:42:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9CB616B02B7; Fri, 6 Dec 2024 12:42:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 844996B02B8; Fri, 6 Dec 2024 12:42:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 66F986B02B6 for ; Fri, 6 Dec 2024 12:42:56 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 08A73141550 for ; Fri, 6 Dec 2024 17:42:56 +0000 (UTC) X-FDA: 82865254164.21.ED38A85 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2048.outbound.protection.outlook.com [40.107.102.48]) by imf26.hostedemail.com (Postfix) with ESMTP id 4202B14000F for ; Fri, 6 Dec 2024 17:42:40 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=NDvMjLKW; spf=pass (imf26.hostedemail.com: domain of ziy@nvidia.com designates 40.107.102.48 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=1733506957; a=rsa-sha256; cv=pass; b=Z/OG/E8TkcJa+QP0GjFZM610eOnGvAa2M8VgWE/DttksaKwy/f0sSFuBNMVNg+iNo4K9bm EYqAHQK0lZjxwJzPeQeYBFgHD1boP0ytNj0EVpjuIThXuxveelGrn6pBXsCbgNltRDHuFs Wg5DVyZsB+309MzhLyRsl8rTZjJeBJg= ARC-Authentication-Results: i=2; imf26.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=NDvMjLKW; spf=pass (imf26.hostedemail.com: domain of ziy@nvidia.com designates 40.107.102.48 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=1733506957; 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=A0UP29TnbMqQy7tNeeZzVu2uyr3inJUGKk0FzecqmBQ=; b=FKcPgy5Wn3//ky5xzZF92p48DJn/46WU5TWl6B2AV9xUseKdi/9ygG5oGBWAopSexBCIS0 +cM8Ok8ab2TcpND4ADfbwGxPe7C/aLUOIheOmJJlu2m/qyvC8C9YAoyagOpRB4bJJf8OfS ajFcEr8xXhW7J3ih11j2+ZyHa9D4OMs= ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=hlyfb/xJZHdHhmUN8boAUFFdCLl/gZmBxelSmRynm5kKbrqwG1vPVBIexETCMSjAipM0KIJ4+ny4mFBQMgHFRSvuWTvw5gvW9seKk+IXgCuLhX5AV4oUULG+ed/Eq90gguFZ9UamYOsyIKlca1IezJrxQ31FITtyK1iL79dLKdYiuIQJt/SRuyxPM9DHRqoqANeqMl+I6Idk9q0dLbERbBb0zMpbLuJw3+FBdYtAKp+Ug84I9uM11tw7ultcFi4+54ICyxjid9qVxDjeX9QKiHPKylXEuVcH6A00EFCzXbtQ9y2pqB0mVqCCBia6DzPLJNTT5oi+Eqb54S3sYSGZSA== 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=A0UP29TnbMqQy7tNeeZzVu2uyr3inJUGKk0FzecqmBQ=; b=EQsTeHT2VrJesO4kZQMaJnb6FFIGVvjfbF415WfA10fbn9tSpdW3k7l5PeI3RRhPQxkw/mzdlog0OUcSPMTa8/jIYg8R+thXdo+sPige5s4u3IVg3Q2fRq8xuLCu0MiTEOb70CVunlzS1Y+oLYi8PY14PI5UfOYeEy+gpyzVQw65Lj9gG0NIIyqnKdRTIlUsfbp3pQqTfjjuOXG4DcTyyR7D1b1UcA91mBhv9QOuM9H+bJEShhj/iY4Uz+t5vcwC3qgCVXmqAKAXvyup47mWF0ITBp942vJSGlOvmvJ7rrxl6ZLlB4QNL+pllWqy5QhsnK8+JL5xN+NU61osN92dbw== 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=A0UP29TnbMqQy7tNeeZzVu2uyr3inJUGKk0FzecqmBQ=; b=NDvMjLKWpxIHn/ZmK7GHdzPHXmbsXYrc5ecHqtqHaoMyCNnkibbMLkFE6nzyQjSUvn+frKzRlSizXkC1E+F9lK6ZnQh7wa5hw2/OX2INwBqLEb2UAI5x2uJKrvzht6cHtlwdQKQYk1eoJ8As5HPyIODLw4Vv5xD2daLk/8oLCxfpqwblaE4oghtFM4VCfDx1ZftqbHcufft3JzLXh6o77o5S6vbLRNTxMnHzFcmFx9LhwRNhor/dpW/wpXu3cJmaAKoRQLmlPH4nW1ESC5u7p4HzrS1TQrL65w/HPXtWY44oTrymBI61sfqdimk9K1NNvQ9l1XfXOgGJmoL8rF5U0w== Received: from DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) by MW6PR12MB7069.namprd12.prod.outlook.com (2603:10b6:303:238::11) 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 17:42:49 +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 17:42:49 +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 , Vineet Gupta , Mathieu Desnoyers , linux-kernel@vger.kernel.org, Zi Yan , Geert Uytterhoeven Subject: [PATCH v2] mm: use clear_user_(high)page() for arch with special user folio handling Date: Fri, 6 Dec 2024 12:42:46 -0500 Message-ID: <20241206174246.2799715-1-ziy@nvidia.com> X-Mailer: git-send-email 2.45.2 X-ClientProxiedBy: BL1PR13CA0188.namprd13.prod.outlook.com (2603:10b6:208:2be::13) To DS7PR12MB9473.namprd12.prod.outlook.com (2603:10b6:8:252::5) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS7PR12MB9473:EE_|MW6PR12MB7069:EE_ X-MS-Office365-Filtering-Correlation-Id: f13e8c92-b75c-4bdd-bd29-08dd161d66e8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|7416014|376014; X-Microsoft-Antispam-Message-Info: GmmyV2g9pMdkd6xQbR3bqxapEBacXRfTQf5Xv1dpsYmtQzNjztujaxPf9rH1k5ALzSKh2BIQGa1rCrIxoTRwUuvKm0xgure8xzsP3+LdgT3YAxiEllFz1uEpftx107uZh8Xks2h1jje78s2jtzys2wUBFoxqjLZ1q0trr4bWj6C1KWUmdiKxsI9FtA7If5bD9620Xp5Td7mgYUmyI/7LWRO56dsVCRC9blatheV9vcmqHjMWnxJ5/e4xnpg1nOfKj0Ujgc1QrVVjInwFQhBp9AJ1UbOiIdTXgjB50QxqtElMfvcziszsa4SaMRGrSltWYTyLTMFfjz1nB2l3W15o8SEoCdFBAyC28q0wFjXMWyIlIKq1j2/VfcZEy2H5VRJ96nPrTwK3pDEltvvYnsCZ5/EpNCv0iiVRhCtbx2DL0hTh7SBg4JZvI36bWpzKINZAKQrY6QWatS1oUAV83cuq27Rsk+mTZcP+TACI1yBi4sOR3rUUs67xyfQJvzvtq8maCNTWlnu3tjAWXXOeYBS6H6FBWon1lMAuvUojqDus8sg5IbbnFTXnA6CLQrLvWilAoIVy2QUvKYTDUD6wtk9/JQ/8j96J5xWEZm3ppHFfzAh59lJnYgFbIb3MYr19QKISNXOAV0cSPLNSpirqAxOvdglN3h+kkd2ZEWYCIYvFJMdww+ewGvpW+SOrVMevmrciPtk3XmrMJQ23bwFCxl+4uF0AIUiWFVIIz17Qo6bYqMe6vJaE9XTswYoOu/vxUzwa9Ymuj5NM8q+wo5wA2URxThJvD0NxmxcE3xjS9OoEbuVrwG1pdZcbo8J5+1nQ6SFY3wh/L7e2py4nDV44e1P4gNyOQrOR5fdWVSTCld3Xjt6KikoZP6qMlZdfTWH0/XZPeA0KOCaFZ3XSozI8CfL/Fy9RR1oThzH6s9h5/t7xo+7bKyKm5E8HKmJnlC/BZ8SXybK2Gj15P0nF6ZbAmmJi8ILOaTCfliIi0jwXkEtUxTuR9Hq3pi0Fz2khUpLfv6K8y4kJq4uJX5OxqMFz4KD7mY3Yze3bBvFHxkJ12jAxwFcMhMSjFaITc7WYOreFlPnTqve/jNHtE/2twMxLK2kZofI4jfcQxFlLUz45nKTcKTR421otZYGG4SDbIdqG/z/8JKmKgJ8GPQQxqQzl4N/7Czf88BNrKjJBP25cKzeYj5f740YXjdGo+uO3yNPBtqSExSDjVwlf9jwhtcKItZt83Gd0WacRKBcf1lMgU6fr/xMIoZWIpGUsNj5S+3ilq2KkhC8dBbhltG0/GvW4WnHef/SBxESJOz0aIwI1ftUXtIFRkjonUNJnSAQ7KYaXR07sEtXjqrhUD9fd29rhnhoU6NM77Zgyn3yM3TXbFUnceQ0= 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)(1800799024)(366016)(7416014)(376014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: JIRb59N9e5S8tq2MXRMFRUnEEQdHTpzsXhcLv9N1G54ex1dUht7mQhzMBhAwZPbt0opHTwSHrzhczFwJhSc4fHdnwWeCcI7q7HyeJk9GZtAvBAKcTA/Td067tUY6ycrixmvfL+Rw8Syg3qTWdG5hKdfi2wcHCWzYPLQYW7Y2V3N/o92xqMjxlhv9WVr965XZM+P9PW5Tya/9aL0ZnZFlgymqVnZy9Rxz1Si7Ld0DssubBWaYrlyu1EvPlnBiOlFBdSABmRXKy1NqLKiuCViDaaWwbh1y1yxapfqgkfpqZG4l3p7vuRaJURG2rJEZAC/y+oebb2tWe53yIZot1q0xBd32libX8arhDMyrIP6O26PRApjDPpkTs1C8p/TNlpHqs84Gue6EvoJg6KDD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OPBVhNMkRsqhRYI4CJiCjsP5aXQxbEJFxraHz6XMNwTwupwo9/C7Sv7ZkJDzbn3tz/R3O07By/p2tSlhUaoBotPkRo4J5Oh5efSmcR3Ws8mrTId0fyCGLNtN X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: f13e8c92-b75c-4bdd-bd29-08dd161d66e8 X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB9473.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Dec 2024 17:42:49.4131 (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: e7REVsa9/1wA8xXN7i3Kp6+8cKx/kEkxTaiMR4ITr7o03cm30963kT3y3XDQ+1ki X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW6PR12MB7069 X-Rspamd-Queue-Id: 4202B14000F X-Stat-Signature: b1bes8jpijarcw1q6djx9b7hnjhh4c4o X-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1733506960-967361 X-HE-Meta: U2FsdGVkX19Wm4S7AXfnghPzMtnUmTA7xocrTEKb7aKl3FjS+rfwibUo/GVlDWw9ximgf7jA17D3WzZWCn9iuYM/wVBcl7tAo2JvFNS+/0n9zXpty3gnA9GJBh2ESW2VkbCEfs7om+gl5fR9p0xGmFMFlaZ1CKhvBSkp46fkeq5kn4xbNvOu3rPk1eJusuTVDdDg363w3nap5NE6O8bC7CZmYwOXq7qWrBGKICP0M2WGOKQ1Mr+ZMiJAK+surrKQb4hbSmWIiAwRe3Ax2LaTrTn3Tp3LoiubM03cvPjrEnyB9Y99wyA3oQMcqBSWS3cvGDB4bZ40tn94bREdXcuJlE+7WagUKEvrSQwliBbAQa+rJkSs9Eg6HUh4SUfF3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GULju2OC3XE9XnBlUws8KwS2fDTD35yML9V8LJOJOZyoAHWU= 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 cpu_dcache_is_aliasing() to true, which require flushing cache, and arc, which changes folio->flags after clearing a user folio, __GFP_ZERO using 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() when cpu_dcache_is_aliasing() is true or architecture is 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..ca8df5871213 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -31,6 +31,7 @@ #include #include #include +#include struct mempolicy; struct anon_vma; @@ -4175,6 +4176,22 @@ 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 changes folio->flags, so always return false to make caller use + * clear_user_page()/clear_user_highpage() + */ + return (cpu_dcache_is_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; }