From patchwork Thu Jan 19 21:22:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13108786 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 15118C46467 for ; Thu, 19 Jan 2023 21:23:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CCBFC6B0081; Thu, 19 Jan 2023 16:23:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C08D56B0088; Thu, 19 Jan 2023 16:23:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8F7E66B0089; Thu, 19 Jan 2023 16:23:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 76D276B0081 for ; Thu, 19 Jan 2023 16:23:41 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 4F31F40679 for ; Thu, 19 Jan 2023 21:23:41 +0000 (UTC) X-FDA: 80372825442.28.0A89FC4 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf30.hostedemail.com (Postfix) with ESMTP id 756AD80011 for ; Thu, 19 Jan 2023 21:23:39 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PuSAsgH0; spf=pass (imf30.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674163419; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references:dkim-signature; bh=BVe42IZ3bUVWxOXb5SeOdBeUgISVBwg/EiJcXbj5PIQ=; b=ol17rK7oEdBDs9Y0J+b6rholmKRdWOyCvI4DwV+Z03Mg9YE+8+ej/oVJfDntcj7Y3zhCan 7sumQBBmHV5ZD378DsugRwn26i7Oh/piGDnXPlPpuaX+mL39R+BMm/f7aF4FwWHjWerGGs Auq1FKT3KxsSpk8+6rKCH7cwmN9ZNzQ= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PuSAsgH0; spf=pass (imf30.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674163419; a=rsa-sha256; cv=none; b=YAWzdVRAWF2dxb7cc8U2YMZLqf8pLKebMlyIqnoDHy9rgry7gxfuBllZL/LkC3tanzpJax KFsoG/C1LyRlUJNnm7dhZxwEcH9cmRu/VwsrkTF0bTtpArhZf8JzGg/yky4Rh/Y+JLCu/5 UhqXbIrBy2Y1Yk45VsQN78+1pR7ks0U= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674163419; x=1705699419; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=3Wysy2UxXgn7XrxNNcTO6OHZIBa9HpH74QVE6RG86iA=; b=PuSAsgH0VqC1CMnyfFWiC4MIRN2J2yRDl/F1KMsx1zgh08mXUFiYbisQ qqdxPDl6EuQJiVs98L1yGG0qgviB2AeGNl1GDK3YOzWjK1Tqio8rFY5G1 lXeuaTcPoNK+dKrHxfXznOOObYZvIN9AiQrKNOQrZ8VUnuZ09sv9m6dd1 gQ+UIILUOq5K4sj47wWZT9rr6DgQFV4kx7pVgFPjKpM12txE9AdRL0Ocn wq9MaWCr49KbvsEaqaZgFv4XNmhTLxcwbNUHRHamSwm2Vevbon0tMalPs 4OByCwrbm2RBXOcipBMDbqkDx18Mxu/eGtNFbRlDJbjE9MVPrB/1Akb0E g==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="323119350" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="323119350" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jan 2023 13:23:37 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="989139021" X-IronPort-AV: E=Sophos;i="5.97,230,1669104000"; d="scan'208";a="989139021" Received: from hossain3-mobl.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.252.128.187]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jan 2023 13:23:36 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu , Christoph Hellwig Subject: [PATCH v5 08/39] x86/mm: Remove _PAGE_DIRTY from kernel RO pages Date: Thu, 19 Jan 2023 13:22:46 -0800 Message-Id: <20230119212317.8324-9-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230119212317.8324-1-rick.p.edgecombe@intel.com> References: <20230119212317.8324-1-rick.p.edgecombe@intel.com> X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 5ue4jidotwikwugabeiboz9ou5bi7mes X-Rspamd-Queue-Id: 756AD80011 X-HE-Tag: 1674163419-451789 X-HE-Meta: U2FsdGVkX1+7E1nPy1CTZUkpv6FBEUszBI5W2TrZrPPsnUYhlxX2rEjjv3My7wOw6ZDljmuVyl6cCEAW0MhH+SHtP+Nd5xmKcaJByH3B88DTt2v5xzBRnJ/z/NdAotg//5+JI6eqP/9L52mibdmyTvVLZcjGXuR0kw4/gpEoO4nx2qqeQTNKtp3rKYsBy7cJ4y7Q8FBMxXNp36HGojdt8zDTyKV7C/Y/WLz2xeebDj1BkewKEloSL13C0CMif2LxsvCKtV86pzjpCtaKDzdOOAql4oDv7THnOn+Hoc9fKuvkUaF9c36Hta9PRWUPKi52HlZkah+MIgaVYe8vajatDz+pTbUfqdOMvlsipEJXUZJ0QR6Fxdc6MA74EIGwg512dFExaQMStuhvKerLeourk7llfD2SSjy0gmZ4Ugg86SJWp1spS3ebz6J7M6lUpJPdb33ldcrsRamrkVhIFhumP/D6jgUFrWDb+n4zKtn+RZcWJrGWmDSdWR75qdLuGTr8/Ir3aAznN+abF0h2967nxQNhAjEFgZIWPJnjdtAhyiGNd2rNTthudSVjXAiJ1DvDcNUKJzR7DKCrPem4zqrJT2tPgsR0FItjJpEkNWZBKTp/Oi0p0BV00VEd0UsossGBPfe9GbIiYTUhX1WuNLSXnc04uckQE47tlkae4tKr3SCrqrVx9VhtAndjBy/epZyMwLy8k8hu4VAOkBWbgGBDqoeuhIpd5NFYdNyA3PrLtTT8/SycmsIPpxEcX4/iXm8eOQ374NjAEGIq/0ZwnXo/JBAbiwbWVqpWm5R/EXxnPpM10nt+xNFMlLb2KC4M1d2PaSTnkGdDnmuKnW2rUkhEaMBDiv8mZL2+Ac+9kpQk4NCC2frAWyrhY8XSa3n6MZwOUHyDND8aV1q8UQOMXGoI5ElV1V6OEbCoHw4HKodirIrSTHkJLnBtwg6y9TQeZJSNyKiRQoo0ViJpoN30WdJ /Ej//8+T ar6MjIvm2ExbTB9qJ8QxN+gAZg7uWELKXUbGmvqurw8CmpKP+wGUwj1C9PSS7OjNUKjh0BYBwKOKS6JtnnCvUWDg98A2c8Kcq+iJFYdQT8dVRHxdlkH3PlAqPwaCj8CMavBAN6+QVWFt3Nu8NbLzr6xYi5+No9oOGpjQcxIm4akvOCFzuzz3oReOCydYs1CvX/lmKzaUo4m/X34vL0H/EcqS78PNlx2ugVUwtmCM43CGtMq34sMowKo98mH7hOkk3ZhOH/d37UBoR3yUDkPSTdHrbDeFhKltQSJFCD9oa0f8T7IOqJ+haTun1zPiJJ+eRuSVwYe+FTXnQPiANtl8G1Qhrxala1Ps+lzVfeaaSA3sm0UVdDWdKOLEwkg5j2gEk47mhstQkB87Qnd5Nhf1HHvCRRIOTCV5tZ5L8vJKBIYmlpmOrbkPE9T9lBHx3bKfWzOmOTETWiwlU7VmcO69HAV8oBAOBVbteXOc9LwiTUFbhsCbIN9blteu9S5bT/XsdkhHnNGTs5zGY7cP7BsksScLyv0LB8Wl/M6qpjaIL29pvy5NATmFHDCADXQ== 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: From: Yu-cheng Yu New processors that support Shadow Stack regard Write=0,Dirty=1 PTEs as shadow stack pages. In normal cases, it can be helpful to create Write=1 PTEs as also Dirty=1 if HW dirty tracking is not needed, because if the Dirty bit is not already set the CPU has to set Dirty=1 when the memory gets written to. This creates additional work for the CPU. So traditional wisdom was to simply set the Dirty bit whenever you didn't care about it. However, it was never really very helpful for read-only kernel memory. When CR4.CET=1 and IA32_S_CET.SH_STK_EN=1, some instructions can write to such supervisor memory. The kernel does not set IA32_S_CET.SH_STK_EN, so avoiding kernel Write=0,Dirty=1 memory is not strictly needed for any functional reason. But having Write=0,Dirty=1 kernel memory doesn't have any functional benefit either, so to reduce ambiguity between shadow stack and regular Write=0 pages, remove Dirty=1 from any kernel Write=0 PTEs. Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Yu-cheng Yu Co-developed-by: Rick Edgecombe Signed-off-by: Rick Edgecombe Cc: "H. Peter Anvin" Cc: Kees Cook Cc: Thomas Gleixner Cc: Dave Hansen Cc: Christoph Hellwig Cc: Andy Lutomirski Cc: Ingo Molnar Cc: Borislav Petkov Cc: Peter Zijlstra Reviewed-by: Kees Cook --- v5: - Spelling and grammar in commit log (Boris) v3: - Update commit log (Andrew Cooper, Peterz) v2: - Normalize PTE bit descriptions between patches arch/x86/include/asm/pgtable_types.h | 6 +++--- arch/x86/mm/pat/set_memory.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/pgtable_types.h b/arch/x86/include/asm/pgtable_types.h index 447d4bee25c4..0646ad00178b 100644 --- a/arch/x86/include/asm/pgtable_types.h +++ b/arch/x86/include/asm/pgtable_types.h @@ -192,10 +192,10 @@ enum page_cache_mode { #define _KERNPG_TABLE (__PP|__RW| 0|___A| 0|___D| 0| 0| _ENC) #define _PAGE_TABLE_NOENC (__PP|__RW|_USR|___A| 0|___D| 0| 0) #define _PAGE_TABLE (__PP|__RW|_USR|___A| 0|___D| 0| 0| _ENC) -#define __PAGE_KERNEL_RO (__PP| 0| 0|___A|__NX|___D| 0|___G) -#define __PAGE_KERNEL_ROX (__PP| 0| 0|___A| 0|___D| 0|___G) +#define __PAGE_KERNEL_RO (__PP| 0| 0|___A|__NX| 0| 0|___G) +#define __PAGE_KERNEL_ROX (__PP| 0| 0|___A| 0| 0| 0|___G) #define __PAGE_KERNEL_NOCACHE (__PP|__RW| 0|___A|__NX|___D| 0|___G| __NC) -#define __PAGE_KERNEL_VVAR (__PP| 0|_USR|___A|__NX|___D| 0|___G) +#define __PAGE_KERNEL_VVAR (__PP| 0|_USR|___A|__NX| 0| 0|___G) #define __PAGE_KERNEL_LARGE (__PP|__RW| 0|___A|__NX|___D|_PSE|___G) #define __PAGE_KERNEL_LARGE_EXEC (__PP|__RW| 0|___A| 0|___D|_PSE|___G) #define __PAGE_KERNEL_WP (__PP|__RW| 0|___A|__NX|___D| 0|___G| __WP) diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index 356758b7d4b4..d41706ad29db 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -2073,7 +2073,7 @@ int set_memory_nx(unsigned long addr, int numpages) int set_memory_ro(unsigned long addr, int numpages) { - return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW), 0); + return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_RW | _PAGE_DIRTY), 0); } int set_memory_rox(unsigned long addr, int numpages)