From patchwork Mon Feb 27 22:29:25 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: 13154240 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 71AB1C7EE2E for ; Mon, 27 Feb 2023 22:31:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7E20B6B0081; Mon, 27 Feb 2023 17:31:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6E64C6B0085; Mon, 27 Feb 2023 17:31:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 474C26B0081; Mon, 27 Feb 2023 17:31:38 -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 0337F6B0081 for ; Mon, 27 Feb 2023 17:31:38 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id D407340A27 for ; Mon, 27 Feb 2023 22:31:37 +0000 (UTC) X-FDA: 80514519834.20.E44B7BB Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by imf28.hostedemail.com (Postfix) with ESMTP id EDA75C000E for ; Mon, 27 Feb 2023 22:31:35 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=XUlKpAPD; spf=pass (imf28.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.136 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=1677537096; 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=BsGO9+iSszn3nu39GKw6/CO3iDfwWfjJ8YMO59zeTxU=; b=PJ49Ix/LnsAgRdI8PWXpLzWXS3PXaSEnLEv1Hs3JKlIzMwi02S7OoK1e7E+Eu1Sl1RZj+i tGawtd/eZjBrh4cNtL2NuUPMyUTDB62sHwZjfC6LGq3iOVcr0eBYxhNErZ+iqvJTgxDwC0 4i6V5OW1cI5SCQOKuRBsfbL6zFB9/0Y= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=XUlKpAPD; spf=pass (imf28.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.136 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=1677537096; a=rsa-sha256; cv=none; b=6QNiS3wS6NIxaV9aWIv2DRqOCRY7/oWecOFVZGSnu/AumvHYz9K55QY8wcwNqr0SboKB0U lqtV/prZ4S0DhEYBKkwFrW0dDj5IjufSJR5YaTk3+qlbXzT/8km+QiWf3I/ZG5ekOaLu1c oAQBUcCx1UyBfQNHESQjW6NP0Kt4LgM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1677537096; x=1709073096; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=A4W74hb/mIAsetG4DQoyU98QDtxo6WTGpJJK3DMv6sU=; b=XUlKpAPDQSojiQPNyJIkqX8EbAm1FeGK5C+y/GGDh9HSCVcRRxCS350W rmNJN11SeSCoL0oPuTiucSyZd6rjz5quJMXZUJwjFVXfiHxJfjJdH7sti /Mn5KkSzF41EQNt3dFeQRoFpIwwMHrAXLKVJjiQSD4a8ZjzuQGh2azd9W 9sVkh0sghnfaB7+jLPLOEURg/FIqbEEXnFcp+OssGZ4Gi8QqbbKhBZdZV LF/Fa4mV/YazUALeDFNQ2NbquEjDq8dvdVQVa+XzDhZ6Tjsq/h18w256L j8pnvYEnvUobaJ/0cI9ksXz7PiOQVnmxKd8peRVhjXgPuKH0JXjVezuHf Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="313657206" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="313657206" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2023 14:31:14 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="848024424" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="848024424" Received: from leonqu-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.72.19]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2023 14:31:13 -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, david@redhat.com, debug@rivosinc.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu , Christoph Hellwig Subject: [PATCH v7 09/41] x86/mm: Remove _PAGE_DIRTY from kernel RO pages Date: Mon, 27 Feb 2023 14:29:25 -0800 Message-Id: <20230227222957.24501-10-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230227222957.24501-1-rick.p.edgecombe@intel.com> References: <20230227222957.24501-1-rick.p.edgecombe@intel.com> X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: EDA75C000E X-Stat-Signature: ojp8kyaamd3mkd148zdxxmqnsczstazj X-Rspam-User: X-HE-Tag: 1677537095-617122 X-HE-Meta: U2FsdGVkX19T6pM8a40c/Y+2sgyMYqLt+XzMh/iK5gKtMlO2RZtSS2TNpqEpPBWYOx55Jq04As/+Tly3MgdxvrjP8U2Z06aWfPLI36PWRtQSZ/6VO2+seBTKvYst4Px9y2ta17gvG9CB55BQBwR6uObYA4e4jtRRYJop4gkb/CgF6QZiBQKCRjV3LRpEqc0JnHVngdgyC3zqM9qNUI2WdoYWjhl7W/gbesVzmGtKo8++/ocVCyTjrJ/ugi9xr6PJhEJYLEnY/60Dov1S7i3qeeF2h4dKAW2S27up32aDyQUQuPTwPaHnHUPp6Sfia/sMkkJaEdjif/JzCU0k/QdvZD1wZD94CiIem2aWaAk1Qi0jRgar7caVn6m7rUc7yzOzaWfouH7J++5hB4BIDfDK7ODV6Bnkws7dXRRQuAlwhLqcPBdJHsvzayOBon/cn3P5ARycrMpGp/muV+WuVCCwEwZzcHtt1R5Gqca87w9kCZoCuA5Npt2tqVVDZAhOw9SODGfq1mBw4Z2Mv/B+/fdd5CAd1O15xPPj3YdT0NjpaN0yGhwXk0WIQ3W0/6qHEXLAm3p8zws0aW4sKHX0tbCqbtLJEDo6gO9Iv6LhQxltsvaQR5MPrkpFwrkwV5IDrOfiX1DUhrJbd20R4TCW8k+ctcyvOj8LSKOr5CYSGvOH+LG7iBQyZQ/0w4/LY3Vn1Gm/MS1az8xC2FSG891K/Z5yZIBZ84PFsGuHF+ICAIqekbEN/0tJCOWpwFcDLMbTOgGxdQrB+oBQp3CQjArMbe0IBzo+yeSasm3asNzngJ+JGi1Iu7IFcyes4a5r7siqXAQgXATK/k6HNA594LuJxMIEVWXBOtLXraJHin3q5f4icYjJL6ekOS1fmsZBPz2Yv9+rh7HPq4ieiJPpu9lXJ3kRyG3nQed/QdEpamOxQCweKOUyz7nt4t9QuxytmwPRbosxE6h0idE84a4nJfqrfDv HI8/GTyD tQAiXJ0iAZuGdWufNSeyYaBJhMU//FMghmjb/8IpQEjB1CM9HMlrXIC+w0385jMtQ44S9WCD7cDSbNzGTOQqP51q4jUbgJjADTGl/4JZDvfpcW1d2GqC0q99HOC9jgKDcwv6yJxUBO5o38EYpalXPH8AdP0oYhaPi2HCCaWyL4lTgAnsuQvpx88t9m7A2Xvq3wHUf5nMaWdKG8GA25DYSX07xKQW63lYvrx92eN93x90e8nvdgHQxzTopQaD5BeaZ01zAxoIs/QyrLOilV6C3hyCfqE/zAnOetMppm/t7PuNzOrboK4EY97CAH5PzjM2xeFh+I/WX6yEJZABmKrSLakgvswAPaQ/txutMH4ANikhr+h6XIUYxqW6eMLsv4h0ksJgL0Y7tLG6RtfnwUpDTBs5zmJuFtgH+RNCes+LswDYTnsTk9POuKmuZ2iHNCKrzkJZY43cAx1HlIVo/vKgXNRL/OACfWPPXCShPm7Z+zOCE+et/If6KKUzUpsOTr/xUitoh7wGf62lrmKXZjZetxVUNYyNtSdJpuAPUkeIUtK2NXdiyyc2KcGsvvw== 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 Tested-by: Kees Cook Acked-by: Mike Rapoport (IBM) Reviewed-by: Kees Cook 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 --- v6: - Also remove dirty from newly added set_memory_rox() 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 | 4 ++-- 2 files changed, 5 insertions(+), 5 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..1b5c0dc9f32b 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -2073,12 +2073,12 @@ 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) { - pgprot_t clr = __pgprot(_PAGE_RW); + pgprot_t clr = __pgprot(_PAGE_RW | _PAGE_DIRTY); if (__supported_pte_mask & _PAGE_NX) clr.pgprot |= _PAGE_NX;