From patchwork Thu May 27 23:51:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Hansen X-Patchwork-Id: 12285773 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EA23FC4708A for ; Thu, 27 May 2021 23:57:37 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9227261006 for ; Thu, 27 May 2021 23:57:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9227261006 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.intel.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 348956B006E; Thu, 27 May 2021 19:57:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 31F956B0070; Thu, 27 May 2021 19:57:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 197106B0071; Thu, 27 May 2021 19:57:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0073.hostedemail.com [216.40.44.73]) by kanga.kvack.org (Postfix) with ESMTP id D6B306B006E for ; Thu, 27 May 2021 19:57:36 -0400 (EDT) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 81D8E1812143D for ; Thu, 27 May 2021 23:57:36 +0000 (UTC) X-FDA: 78188675712.30.6DA86FD Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by imf01.hostedemail.com (Postfix) with ESMTP id 700F8503BD13 for ; Thu, 27 May 2021 23:57:27 +0000 (UTC) IronPort-SDR: 1wVit2CjKwqFkSe/pPnNqzhYhpfFmjcHR4NnQEqedoUQ5Khy6ZS5+bC3uwgwjYwbaRjHgvEegi jkkfW9GPYLSg== X-IronPort-AV: E=McAfee;i="6200,9189,9997"; a="190229886" X-IronPort-AV: E=Sophos;i="5.83,228,1616482800"; d="scan'208";a="190229886" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 May 2021 16:57:34 -0700 IronPort-SDR: yD/NbNm0YpzjCdn1XHFOqeXCAlkAigTM8S/w8sGOQLElDaLB+al68vr387z6prMfvaj6YU57xc eytDWg4WXIVA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.83,228,1616482800"; d="scan'208";a="397944347" Received: from viggo.jf.intel.com (HELO localhost.localdomain) ([10.54.77.144]) by orsmga003.jf.intel.com with ESMTP; 27 May 2021 16:57:33 -0700 Subject: [PATCH 1/5] x86/pkeys: move read_pkru() and write_pkru() To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org,Dave Hansen ,tglx@linutronix.de,mingo@redhat.com,bp@alien8.de,x86@kernel.org,luto@kernel.org,shuah@kernel.org,babu.moger@amd.com,dave.kleikamp@oracle.com,linuxram@us.ibm.com,bauerman@linux.ibm.com,bigeasy@linutronix.de From: Dave Hansen Date: Thu, 27 May 2021 16:51:11 -0700 References: <20210527235109.B2A9F45F@viggo.jf.intel.com> In-Reply-To: <20210527235109.B2A9F45F@viggo.jf.intel.com> Message-Id: <20210527235111.11857E30@viggo.jf.intel.com> Authentication-Results: imf01.hostedemail.com; dkim=none; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none); spf=none (imf01.hostedemail.com: domain of dave.hansen@linux.intel.com has no SPF policy when checking 134.134.136.126) smtp.mailfrom=dave.hansen@linux.intel.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 700F8503BD13 X-Stat-Signature: 97r96js933w3zyeteqb6m3f3sgrjxzd7 X-HE-Tag: 1622159847-635959 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: Dave Hansen write_pkru() was originally used just to write to the PKRU register. It was mercifully short and sweet and was not out of place in pgtable.h with some other pkey-related code. But, later work included a requirement to also modify the task XSAVE buffer when updating the register. This really is more related to the XSAVE architecture than to paging. Move the read/write_pkru() to asm/fpu/xstate.h. pgtable.h won't miss them. Signed-off-by: Dave Hansen Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: x86@kernel.org Cc: Andy Lutomirski Cc: Shuah Khan Cc: Babu Moger Cc: Dave Kleikamp Cc: Ram Pai Cc: Thiago Jung Bauermann Cc: Sebastian Andrzej Siewior --- b/arch/x86/include/asm/fpu/xstate.h | 29 +++++++++++++++++++++++++++++ b/arch/x86/include/asm/pgtable.h | 29 ----------------------------- b/arch/x86/kernel/process_64.c | 1 + b/arch/x86/kvm/svm/sev.c | 1 + b/arch/x86/kvm/x86.c | 1 + b/arch/x86/mm/pkeys.c | 1 + 6 files changed, 33 insertions(+), 29 deletions(-) diff -puN arch/x86/include/asm/fpu/xstate.h~move-write_pkru arch/x86/include/asm/fpu/xstate.h --- a/arch/x86/include/asm/fpu/xstate.h~move-write_pkru 2021-05-27 16:40:23.110705472 -0700 +++ b/arch/x86/include/asm/fpu/xstate.h 2021-05-27 16:40:23.132705472 -0700 @@ -6,6 +6,7 @@ #include #include +#include #include /* Bit 63 of XCR0 is reserved for future expansion */ @@ -116,4 +117,32 @@ void copy_kernel_to_dynamic_supervisor(s /* Validate an xstate header supplied by userspace (ptrace or sigreturn) */ int validate_user_xstate_header(const struct xstate_header *hdr); +static inline u32 read_pkru(void) +{ + if (boot_cpu_has(X86_FEATURE_OSPKE)) + return rdpkru(); + return 0; +} + +static inline void write_pkru(u32 pkru) +{ + struct pkru_state *pk; + + if (!boot_cpu_has(X86_FEATURE_OSPKE)) + return; + + pk = get_xsave_addr(¤t->thread.fpu.state.xsave, XFEATURE_PKRU); + + /* + * The PKRU value in xstate needs to be in sync with the value that is + * written to the CPU. The FPU restore on return to userland would + * otherwise load the previous value again. + */ + fpregs_lock(); + if (pk) + pk->pkru = pkru; + __write_pkru(pkru); + fpregs_unlock(); +} + #endif diff -puN arch/x86/include/asm/pgtable.h~move-write_pkru arch/x86/include/asm/pgtable.h --- a/arch/x86/include/asm/pgtable.h~move-write_pkru 2021-05-27 16:40:23.114705472 -0700 +++ b/arch/x86/include/asm/pgtable.h 2021-05-27 16:40:23.135705472 -0700 @@ -126,35 +126,6 @@ static inline int pte_dirty(pte_t pte) return pte_flags(pte) & _PAGE_DIRTY; } - -static inline u32 read_pkru(void) -{ - if (boot_cpu_has(X86_FEATURE_OSPKE)) - return rdpkru(); - return 0; -} - -static inline void write_pkru(u32 pkru) -{ - struct pkru_state *pk; - - if (!boot_cpu_has(X86_FEATURE_OSPKE)) - return; - - pk = get_xsave_addr(¤t->thread.fpu.state.xsave, XFEATURE_PKRU); - - /* - * The PKRU value in xstate needs to be in sync with the value that is - * written to the CPU. The FPU restore on return to userland would - * otherwise load the previous value again. - */ - fpregs_lock(); - if (pk) - pk->pkru = pkru; - __write_pkru(pkru); - fpregs_unlock(); -} - static inline int pte_young(pte_t pte) { return pte_flags(pte) & _PAGE_ACCESSED; diff -puN arch/x86/kernel/process_64.c~move-write_pkru arch/x86/kernel/process_64.c --- a/arch/x86/kernel/process_64.c~move-write_pkru 2021-05-27 16:40:23.117705472 -0700 +++ b/arch/x86/kernel/process_64.c 2021-05-27 16:40:23.138705472 -0700 @@ -41,6 +41,7 @@ #include #include +#include #include #include #include diff -puN arch/x86/kvm/svm/sev.c~move-write_pkru arch/x86/kvm/svm/sev.c --- a/arch/x86/kvm/svm/sev.c~move-write_pkru 2021-05-27 16:40:23.121705472 -0700 +++ b/arch/x86/kvm/svm/sev.c 2021-05-27 16:40:23.142705472 -0700 @@ -16,6 +16,7 @@ #include #include #include +#include #include #include diff -puN arch/x86/kvm/x86.c~move-write_pkru arch/x86/kvm/x86.c --- a/arch/x86/kvm/x86.c~move-write_pkru 2021-05-27 16:40:23.125705472 -0700 +++ b/arch/x86/kvm/x86.c 2021-05-27 16:40:23.150705472 -0700 @@ -66,6 +66,7 @@ #include #include #include +#include #include /* Ugh! */ #include #include diff -puN arch/x86/mm/pkeys.c~move-write_pkru arch/x86/mm/pkeys.c --- a/arch/x86/mm/pkeys.c~move-write_pkru 2021-05-27 16:40:23.128705472 -0700 +++ b/arch/x86/mm/pkeys.c 2021-05-27 16:40:23.151705472 -0700 @@ -10,6 +10,7 @@ #include /* boot_cpu_has, ... */ #include /* vma_pkey() */ +#include /* read/write_pkru() */ #include /* init_fpstate */ int __execute_only_pkey(struct mm_struct *mm)