Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp36928pxj; Thu, 27 May 2021 20:33:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJysEfSewZiIs5cf6IpgNh8Vv2WiBLG/tD69mBGXhmoSE9wYkxO3rLSWxd4fuRvKiegu03Ob X-Received: by 2002:a17:906:f8c8:: with SMTP id lh8mr6986346ejb.61.1622172788656; Thu, 27 May 2021 20:33:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622172788; cv=none; d=google.com; s=arc-20160816; b=GJgdzX7VtBGf1JGOzxcOK4YIofgZ5OJDvXHnMnXzu0eA27laHrA+mvaj+lkdgSr1/8 N9Bbd8boGflnmF+pvf520WkDggcFVMOmEjVMbhFg83R8XMHMwMnwlPErdt05c1HB56u5 Vrvz1vjIF3VZ+0SKi/iBS8oSV2GREImcYTK/VcgP80wJ4AoKynzEoWoqIBU5AkhKvM/N Mo05l1lRQoxQfioQKEu6mJeUN6fMuA8kribEhkazc6357PnM8mYwvS1UtBLiRPJsp6tc /a8rKvqj8EjTxiUO5tR6LZ9iNGhaJayWsraw+IAhSfvkYPCBnARFgPj+PUzbuECtWUzj hUPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:in-reply-to:references:date:from:cc :to:subject:ironport-sdr:ironport-sdr; bh=g2A8wfKfsMtWuF2uejFMTwpJCIgSq2DLx/MHwaoRCyo=; b=n4sx74kdPjbjxM1c2G4kQYi1rN1sKt8jLHF0C7jy3ug8OqnkdzVnUYCNlC5xH04WfJ BnZ89D72A/MCOuXPItWbYgmbokTERqyrHVrHwZ/pTxeP1BZUoHLjOl01ibYmDrLay0B9 mK4IQ9q5/4J7deRbHnr1bWvi0AEYVC5HGvzVNovfpDaneEmJrDAqhfz6RgLVvFyXDrhF aO6L8kijzfuFmwJOeJaXuve0RFpP3RMynktvAz5QABDfvDTId0a3Hu+t6O3XzgnXXQUq MBeJF46UVXrOpk5CAgAn9jt1zXrDknFfTf/UkkXmNsN0eT4cOpWfLuFLMMhfuVclxZkF Ro2g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id jg40si3761701ejc.465.2021.05.27.20.32.44; Thu, 27 May 2021 20:33:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236107AbhE0X7w (ORCPT + 99 others); Thu, 27 May 2021 19:59:52 -0400 Received: from mga04.intel.com ([192.55.52.120]:51095 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235437AbhE0X7l (ORCPT ); Thu, 27 May 2021 19:59:41 -0400 IronPort-SDR: EBxQa1/syJaKF41qD1IIL5kq/43cLMLgUQ1Movzo3Ucb4ihfiZ8xi3tKa1oBXdhdPs+mWumEyG dm1VnDILqAyQ== X-IronPort-AV: E=McAfee;i="6200,9189,9997"; a="200969205" X-IronPort-AV: E=Sophos;i="5.83,228,1616482800"; d="scan'208";a="200969205" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 May 2021 16:57:33 -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> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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) _