From patchwork Mon Oct 7 18:23:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 13825099 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 6D26FCFB450 for ; Mon, 7 Oct 2024 18:23:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE7016B0085; Mon, 7 Oct 2024 14:23:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D96A06B0088; Mon, 7 Oct 2024 14:23:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C10096B0089; Mon, 7 Oct 2024 14:23:30 -0400 (EDT) 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 9FCC76B0085 for ; Mon, 7 Oct 2024 14:23:30 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 46C02AC7FF for ; Mon, 7 Oct 2024 18:23:30 +0000 (UTC) X-FDA: 82647628980.16.7CF822C Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2053.outbound.protection.outlook.com [40.107.237.53]) by imf13.hostedemail.com (Postfix) with ESMTP id 7A59320016 for ; Mon, 7 Oct 2024 18:23:27 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=PfrOBNpQ; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf13.hostedemail.com: domain of ziy@nvidia.com designates 40.107.237.53 as permitted sender) smtp.mailfrom=ziy@nvidia.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1728325365; a=rsa-sha256; cv=pass; b=pj35ZD9Rp/1B21Kq929I3WyZQqHRVniQWE4nNmogzPS5u/geaFaoMjMmOeI9xqRflEQHyD vXxOtZLC99SCrwWG75FGKxM/bfrIpbLOGHGd+e2EX6Qs3ETtqZL/GRbcrexM70RxeUHCKY mwuHeylM6bCXrtYsAckAyBwOfmQiJ0c= ARC-Authentication-Results: i=2; imf13.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=PfrOBNpQ; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf13.hostedemail.com: domain of ziy@nvidia.com designates 40.107.237.53 as permitted sender) smtp.mailfrom=ziy@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=1728325365; 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=/y+kO1QZm3er56og+QcjjxCV8PiYzkNX1GkmotgyUcg=; b=IAW8lZDEsEMw91HqUaMXleE6uOZCBHKowzrN5/fY2SOafLoLL98MQA/TKSQnjZn6pJZNmf dclChchdsliQadRNWRF1q516+w1vAWXolc1q5wX0cAKQQP6GPuIMm2EV/3CXz2JJa2lB9h NvB3g8sIeCSaYCOm052/J/p+bLFyuMY= ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=P004mnEjH+9cxAPQGy442KKEYjHYFGZh3QNOEUsadzmgaIPZTcnSnnTDTjG2h7Hj+kHuKa+j6yfMoN4CDmEYtdxK0hHb9S+bE6CHXjikFkvV+hFr4iO9umhu9Km+qIx2KUbOD7QNSC4Ss8sQjglJARa1/Jvi8eXZbJZ7P2cf2/pibna9NpcjbhOuJ8p2xAK6YaJhrs5zzAo7ePMAyILEDjxGJCNNwIS3yWTMo2j9tKORg9z3E5TPvpSAVuEsEFZ5WQB1/GfTyWaD+u2gleKX7P90orVjimPxGp1oK5xbdSHvXMG1TKCVhNW/q2MNzQ+B47MPt+9wVY/kfYguQ3bl9w== 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=/y+kO1QZm3er56og+QcjjxCV8PiYzkNX1GkmotgyUcg=; b=o83PmJetBvcgBiQQwd6JhIB6fOIEtGPSdsuHANntWBLszgGtjc01Dm4hFW8O3T1JHiwqjPLLIECHri4d43fhwR8gl/Bt3v2T4eWvrRE9NQ2UDRufKa0KYx4KLYU01mEs4PHbJpciawqVU+Q/88DqHDcHs0b/t1PwCrwr123WUImqjmr/l8aFUtnpyaZv69ulsxRBvgdxWnsQ3VrgpR3eIlvEnHQzgVV/HfoclJ3m0X4TNSzHZ0q1mGOdO4l79rew8sTu6uzC1vAnZk3umrqGjJ6vdELyvEQQ3oAyBZ8SMr7aF4wjMVCFvid7RVq4OHPNgps7wMaoKz+C/VgscB+n/Q== 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=/y+kO1QZm3er56og+QcjjxCV8PiYzkNX1GkmotgyUcg=; b=PfrOBNpQeMJmxPs9ukLsy81hA/DeWZfGiyYcca99YvJcUkT+0uQs1z7Hiz713KjBLycfoi59gc8XAEFZKLJDMvbV97y+1/LgpmavNydmYMecddZTxNhh3WSPyjavX8stCOlKt+Izn1/afMH10w5ICRQcNMl37F1qJotgZnyB1Itpz22dppm3sxJk+4olbeIQpa2YsNyKbaZMwbpeJpKX6iumclmdhvdXnPS+IWtCw8BGAv2965TUZmIC3AZmQP+jBC1LQECaTZ9MiAcG5yeKa51LRijKfNZp7uzh7SvLnHnPpcPVeVwQDu6OSpBFK1NYVlLy60+RtMlF8Psm1ly1eA== Received: from DS7PR12MB8324.namprd12.prod.outlook.com (2603:10b6:8:ec::9) by IA1PR12MB6257.namprd12.prod.outlook.com (2603:10b6:208:3e7::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8026.23; Mon, 7 Oct 2024 18:23:22 +0000 Received: from DS7PR12MB8324.namprd12.prod.outlook.com ([fe80::c45e:51b0:e1ec:1a32]) by DS7PR12MB8324.namprd12.prod.outlook.com ([fe80::c45e:51b0:e1ec:1a32%4]) with mapi id 15.20.8026.020; Mon, 7 Oct 2024 18:23:22 +0000 From: Zi Yan To: linux-mm@kvack.org, Alexander Potapenko , Kees Cook Cc: David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Miaohe Lin , Kefeng Wang , John Hubbard , "Huang, Ying" , linux-kernel@vger.kernel.org, Zi Yan Subject: [RFC PATCH] mm: avoid clearing user movable page twice with init_on_alloc=1 Date: Mon, 7 Oct 2024 14:23:15 -0400 Message-ID: <20241007182315.401167-1-ziy@nvidia.com> X-Mailer: git-send-email 2.45.2 X-ClientProxiedBy: BLAPR05CA0034.namprd05.prod.outlook.com (2603:10b6:208:335::15) To DS7PR12MB8324.namprd12.prod.outlook.com (2603:10b6:8:ec::9) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS7PR12MB8324:EE_|IA1PR12MB6257:EE_ X-MS-Office365-Filtering-Correlation-Id: 8975a458-8b35-44a3-6fc5-08dce6fd2019 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: ljWL2+Q88RHy09kStUzT44bavVb/WMNDHGczQjSd28YZNTbF9XjFeWg2U+iIEibJImPF6BLbjHn0A2PDUNgme/yAYzLn3ojYQV2LgPaC7LthyKwEMQkdS/Wv2CwYZ3KYWYjU+/24ThE4N93yhvW9+EBrrTz3HqKip8DELqpcvVbG/Hoo8EMf4uRqYmyrSU+Kez6qUAZwvVL13hF40iw42/whHgjMgsWZzkES24XhZf7g6WSCY8m5d/TwrbygegbuPZu4IxDS2HSlEYCOG/oIt/zhg3Up+dQw7k7Z1AKta+mQ/m/19G6pNP7Y7k5fqYn45TP21QFXuK/pl437uhVtPqwN4gLk6vKh4eka+EVLIc02EN8nip40KuPPgtABtZqteNqNqq6brvBCtiDihunMoDBcs++zxX/2VeQua+XI+h4GhPcD1P3U/FRXF6nP+vNRmBAOGWZ3y4xf5hGhV7g1eV/qffdZjZJr9NHm/ZIqneOvTVznb9X3h09uYcYWpA7kqZlksCGPezeK24SKqBcS7KCvWM2XLbd+7JeLX5M4gY+/PrCXGXEwAFYsoCc1CLI1Y4WxB83tpjk11/czVeJ58tB4D7nFdljCqFdz4MIFC07mp8DJmOlUHwvZigghSHZflW/etvIL4mhN4+trOW3y3u4YIyaX2RgIfi+Bbsi3gWyFLUHTApKwu09o+FZzcC+mZjwiO5G6QU1a2795OfbjRoleMSp2UVFlYsj2Ohq8T0hxmvBpVD+1jIqBKAW08ubuos5rn2itjZYNDvOAv1RRwWyO6BEJR0oXebo9IfsLTM+nJiMjNTQBKcr00QwIrZg162BAbJer/fcEOEW0eCkhqcXGvkMuaqoemz77glqjyweskcMUUt0vSKLr/mjPAlrNrYLaZyW0bOrwMFQ7jX2h5G83BJdhopLvNrbSYDF8WqjOTt3AVGxPIMfKHDKV83OMAVQj2liA82T+bwl1Frb0vKYk1w9/POWOwtSk9P3Hz79i+76YT1go209eFBBuBNdP02zsehZ3M1p+Ivljd4qSapQEbcqyAsSos8mMRGCsZHFu0DAI0HvXrgH+5IJeLJji2SazS5etP5ypEf1F0qcufQuoKTWE4YhNNEEzp/p+CxYCuQMyA4ohQTkaQjqak72lmUhK1tdKdwF0iFdFBd09yvKa8xPd+V3gEkbFyNx2Bul9VbLDapeq9MdmisCnmvXHlqa5AUBnU3aA7zoQEC3//bTOaAqGubntINLZpSnTwwMm4750tUNe6FifOMC79WexeqaM+UnHxL3vR68w0zK6iw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS7PR12MB8324.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: G/69p5ZOYzD25u8pIhA3PRIGJ1yR93m1snk8cOuCAPyh0XSqlE/obwFRXP4tHhS1MKeLAUXIbGTM0iJPFyVCeGAhjTUMx6h3pfs5/Mub471nHAAObY3FS/LbLT2ADEbAoJmSjMRohk/MGs8l9SOx5RR+fuXYjMzPTjroAqW0Y0B7HeBqFoGe35SF2eCzXz6lunZrpVBpedMQ9tmIG6cad7CuqOe2BLdkfnIq5REhLGzD70SdwntTK1Yzg8rEa1pR8ijjbNeJa0cdGr81ePnUANdyntPqcrkAhdWAPBsdOfIAyECy/DrOpy100kQ97oOhF26Tr17J7EMQ8rI2D+Hye/zKij91cIr/kUuqtlNRYaM5+Psr3z5GGpFBWyvSYUCCX0FX9+V4fghZ5BIVbUzfqUWBia8x5JnHGKJGhFh6F7/btSj98SsD5/D6liFycLyAbJAT5tyy2FUHMNRimTgCDqupGRVscysmN1TuwI6LuTVgs9U9v0CgdmmXxKOE4VDMN7MITBmFWBO7p8XQ6thwCANcJfpMe1EgW97fwE3nEHWHhc4AzhWtSirEnqNgPzHh16/434a1lTKA0Lc6p0N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AuthSource: DS7PR12MB8324.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Oct 2024 18:23:22.0984 (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: kezHw6G8GADw78BQbAAuj7qGSf+AZacdAqCvVfDzCYsGki2Rcv0GYpFc1gZM0byN X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6257 X-Rspam-User: X-Rspamd-Queue-Id: 7A59320016 X-Rspamd-Server: rspam01 X-Stat-Signature: q3q1jfz965eygakz6y1tje9mr4q614d8 X-HE-Tag: 1728325407-7182 X-HE-Meta: U2FsdGVkX19mQYN2vHTl6H2BZx8YcbQDqDffa7okqjM3y+VkkPH0RW0d2zqu7xoNOGYU/sff3zVjidldO9mKZg4m3eXKi+s2gm3pMmR78g+ONXsVUnN/8YpB3bJRy3GyAA+cJ6iOoSlEQBs+4Sxe86lk6Ql/TA72FJjGYBONtPqRYajsbGfWw8Efv+1SXvl+XY/0MoQ+usVI7gxDrnb3++GsF4EkKQZ3Yl8BOTIPaHmE1+dbPvRtBUSFFWYXxrUVRtKkSdU7qo+y+K/vGUdyA57oZepUP+rGe9m+/nwzvF/T7xsmfEFC5F/6hatus90HiS52TCLr/z+kkzO40X6HTb+R/xbfTPdMnqVUWh9Bmyt3oPfuy9BLuYzN6pp5z/cpAS0ftkhAG5dspOSzfK9VjwqPPr11zLzU72KQDxKJRbegkpYRSV6KooNwMQC5DPlXBHxN7QCtOGNpaQh8wqUVmVF0TZf6qkj1TI/RNLWC25NwyGA5aC7DFfNzEmlsLJJQ/h/keQCbMgXNckbmnupK4o02+7RhQmqo8Fc+x1CgOrVynDH2vKKw9UhN9QD4uKwQlrI2yDhKTgu5xEkiEZ+fJvHXEFgf8k70EAAuM9DBXW2vDFgixKOC2cerZ9zT2Ny6cBMMNm2/8z63GGrWUZ6EEOB8FGzfZT6wO0MgaebZcrwbKkV8mqEVWiCnilOU3O9zx/ov/lbnYcUC/VS1fjjyWBqb8HSiM5lKn5XjZCV9+UIZS7yK5crw2HZ/7S9MjG28HTN2Tazbh2wwSujPYupgc6xrclBR11WaCSTNziDsx9U82v58Th2AdD1Tx2pAMuKBFmzXz3Tz/2C11Km0zpG9VFkkfTpFNPil24WQ0lNz93o12DRxCSOEe5JMJNkk9z9qbKA2Jr6u0iuCnz89DuVcPXypNO66lLnbTYgZYmIri9xUhc4TBK2HpBYRU1tQP1MyiYXxOAseJFKOdQJhFrj YG7x6k2C 2LlkX3Dbqze5vCbMXxsrO8Czo1mZIA12hhGzJuvZxXYCvPE8NiEwdIypY/+rL2kutI6lvRLpB4oyHVx2bo/kj83GeAR2psq0LoaC/P9bm1yaeV+JdiFkyFUBP4Gzz2lTEMtqmLhrYWi0ZB7uh8mdo2C7q2KosH2cw38jdYOnQ8jcGvFq3H3M3a8rq84ojJDze/+yqGqdKO22My2nGWecc32FOCciQbdW9nP6oBAx193wxjBhh+NNXTjgUNYXzAJfjo07UdDCOD5NwdwygGJo0NSKEELhD3xUlsvX+zJ64NG7JqWSdWwEAs26orqlabHAMVauAUm0OqHo9GDh204ti/SaMKA== 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: Commit 6471384af2a6 ("mm: security: introduce init_on_alloc=1 and init_on_free=1 boot options") forces allocated page to be cleared in post_alloc_hook() when init_on_alloc=1. For non PMD folios, if arch does not define vma_alloc_zeroed_movable_folio(), the default implementation again clears the page return from the buddy allocator. So the page is cleared twice. Fix it by passing __GFP_ZERO instead to avoid double page clearing. At the moment, s390,arm64,x86,alpha,m68k are not impacted since they define their own vma_alloc_zeroed_movable_folio(). For PMD folios, folio_zero_user() is called to clear the folio again. Fix it by calling folio_zero_user() only if init_on_alloc is set. All arch are impacted. Signed-off-by: Zi Yan --- include/linux/highmem.h | 14 ++------------ mm/huge_memory.c | 4 +++- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/include/linux/highmem.h b/include/linux/highmem.h index 930a591b9b61..4b15224842e1 100644 --- a/include/linux/highmem.h +++ b/include/linux/highmem.h @@ -220,18 +220,8 @@ static inline void clear_user_highpage(struct page *page, unsigned long vaddr) * Return: A folio containing one allocated and zeroed page or NULL if * we are out of memory. */ -static inline -struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma, - unsigned long vaddr) -{ - struct folio *folio; - - folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, vaddr, false); - if (folio) - clear_user_highpage(&folio->page, vaddr); - - return folio; -} +#define vma_alloc_zeroed_movable_folio(vma, vaddr) \ + vma_alloc_folio(GFP_HIGHUSER_MOVABLE | __GFP_ZERO, 0, vma, vaddr, false) #endif static inline void clear_highpage(struct page *page) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index a7b05f4c2a5e..ff746151896f 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1177,7 +1177,9 @@ static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf, goto release; } - folio_zero_user(folio, vmf->address); + if (!static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, + &init_on_alloc)) + folio_zero_user(folio, vmf->address); /* * The memory barrier inside __folio_mark_uptodate makes sure that * folio_zero_user writes become visible before the set_pmd_at()