From patchwork Tue Jun 22 22:25:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Hansen X-Patchwork-Id: 12338691 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 7C7B7C2B9F4 for ; Tue, 22 Jun 2021 22:26:00 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 18E2460FF1 for ; Tue, 22 Jun 2021 22:26:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 18E2460FF1 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 948166B0075; Tue, 22 Jun 2021 18:25:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 91E776B0078; Tue, 22 Jun 2021 18:25:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 799A46B007B; Tue, 22 Jun 2021 18:25:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0130.hostedemail.com [216.40.44.130]) by kanga.kvack.org (Postfix) with ESMTP id 466736B0075 for ; Tue, 22 Jun 2021 18:25:55 -0400 (EDT) Received: from smtpin40.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 7A06319B13 for ; Tue, 22 Jun 2021 22:25:55 +0000 (UTC) X-FDA: 78282793470.40.A3A3BFB Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by imf18.hostedemail.com (Postfix) with ESMTP id BE43E20010B9 for ; Tue, 22 Jun 2021 22:25:54 +0000 (UTC) IronPort-SDR: R1UoInJC33cLM4QwBHChSEcjoT6QFz6C6Lw8gWQz9f7kNM9u1nWX/+Q6PfGy2F9TODhRfw/6ZQ eSo3sx7av+RQ== X-IronPort-AV: E=McAfee;i="6200,9189,10023"; a="228711304" X-IronPort-AV: E=Sophos;i="5.83,292,1616482800"; d="scan'208";a="228711304" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jun 2021 15:25:53 -0700 IronPort-SDR: vMSYxKo/Wdt2nBfKSQVOBDnCjXLTzGrSzIuC02M7uCibTY5/PqkB0hllQPbHOPcnGv7OEwnA0V WRrxhhUloKuw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.83,292,1616482800"; d="scan'208";a="444756949" Received: from viggo.jf.intel.com (HELO localhost.localdomain) ([10.54.77.144]) by orsmga007.jf.intel.com with ESMTP; 22 Jun 2021 15:25:53 -0700 Subject: [RFC][PATCH 7/8] x86/fpu: actually stop using XSAVE on 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 From: Dave Hansen Date: Tue, 22 Jun 2021 15:25:08 -0700 References: <20210622222455.E901B5AC@viggo.jf.intel.com> In-Reply-To: <20210622222455.E901B5AC@viggo.jf.intel.com> Message-Id: <20210622222508.2BB7D73C@viggo.jf.intel.com> X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: BE43E20010B9 X-Stat-Signature: 6uo1s3dmbg6bz9mr1poy3og4ny5t3mrk Authentication-Results: imf18.hostedemail.com; dkim=none; spf=none (imf18.hostedemail.com: domain of dave.hansen@linux.intel.com has no SPF policy when checking 192.55.52.88) smtp.mailfrom=dave.hansen@linux.intel.com; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none) X-HE-Tag: 1624400754-290841 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 Previously, 'xfeatures_mask_all' represented all of the features that might be found in a kernel XSAVE buffer. It also happened to be (XCR0|XSS): the set of XSAVE features enabled in the hardware. In other words, if an feature was XSAVE-enabled in the hardware, it was guaranteed to be found in the kernel XSAVE buffer. The goal of this series is to remove PKRU from the kernel XSAVE buffer, but to also leave it XSAVE-enabled in the hardware. This ensures that applications which read the hardware enabling status of PKRU directly with xgetbv(0) will not notice a thing. Locate all the places where 'xfeatures_mask_all' is used to represent the set of features in a kernel XSAVE buffer and replace it with xfeatures_mask_fpstate(). Update the fpstate access function (__raw_xsave_addr()) to WARN() if a caller attempts to access a non-present feature. Lastly, remove the get_xsaves_size() hack now that PKRU can be removed from the fpstate size calculations. The most visible effect if this an 8-byte drop in the "context size": Before: x86/fpu: Enabled xstate features 0x2ff, context size is 2568 bytes, using 'compacted' format. After: x86/fpu: XSAVE managing features 0xff, context size is 2560 bytes, using 'compacted' format. Signed-off-by: Dave Hansen Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: x86@kernel.org Cc: Andy Lutomirski --- b/arch/x86/include/asm/fpu/internal.h | 2 +- b/arch/x86/kernel/fpu/xstate.c | 29 +++++++++++++---------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff -puN arch/x86/include/asm/fpu/internal.h~warn-on-seeing-pkey-xfeature arch/x86/include/asm/fpu/internal.h --- a/arch/x86/include/asm/fpu/internal.h~warn-on-seeing-pkey-xfeature 2021-06-22 14:49:13.609051745 -0700 +++ b/arch/x86/include/asm/fpu/internal.h 2021-06-22 14:49:13.622051745 -0700 @@ -286,7 +286,7 @@ static inline void os_xrstor_booting(str */ static inline void os_xsave(struct xregs_state *xstate) { - u64 mask = xfeatures_mask_all; + u64 mask = xfeatures_mask_fpstate(); u32 lmask = mask; u32 hmask = mask >> 32; int err; diff -puN arch/x86/kernel/fpu/xstate.c~warn-on-seeing-pkey-xfeature arch/x86/kernel/fpu/xstate.c --- a/arch/x86/kernel/fpu/xstate.c~warn-on-seeing-pkey-xfeature 2021-06-22 14:49:13.613051745 -0700 +++ b/arch/x86/kernel/fpu/xstate.c 2021-06-22 14:49:13.628051745 -0700 @@ -168,9 +168,7 @@ static bool xfeature_enabled(enum xfeatu */ static bool xfeature_fpstate_enabled(enum xfeature xfeature) { - // For bisectability, mirror xfeature_enabled() for now. - //return xfeatures_mask_fpstate() & BIT_ULL(xfeature); - return xfeature_enabled(xfeature); + return xfeatures_mask_fpstate() & BIT_ULL(xfeature); } /* @@ -416,7 +414,7 @@ static void __init setup_init_fpu_buf(vo if (boot_cpu_has(X86_FEATURE_XSAVES)) init_fpstate.xsave.header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT | - xfeatures_mask_all; + xfeatures_mask_fpstate(); /* * Init all the features state with header.xfeatures being 0x0 @@ -644,7 +642,6 @@ static unsigned int __init get_xsaves_si static unsigned int __init get_xsaves_size_no_independent(void) { unsigned int size; - u64 xfeatures_in_xcr0; u64 old_xss; u64 old_xcr0; @@ -655,14 +652,8 @@ static unsigned int __init get_xsaves_si /* Disable independent features. */ wrmsrl(MSR_IA32_XSS, old_xss & ~xfeatures_mask_independent()); - /* - * *Temporarily* (to be removed in a later patch), ennsure there - * is still space for PKRU in the fpstate buffer even though it's - * essentially unused. - */ - xfeatures_in_xcr0 = xfeatures_mask_fpstate() | XFEATURE_MASK_PKRU; /* Disable user features which are not kept in the fpstate: */ - xsetbv(XCR_XFEATURE_ENABLED_MASK, old_xcr0 & xfeatures_in_xcr0); + xsetbv(XCR_XFEATURE_ENABLED_MASK, old_xcr0 & xfeatures_mask_fpstate()); /* * Ask the hardware what size is required of the buffer. @@ -843,8 +834,8 @@ void __init fpu__init_system_xstate(void } print_xstate_offset_size(); - pr_info("x86/fpu: Enabled xstate features 0x%llx, context size is %d bytes, using '%s' format.\n", - xfeatures_mask_all, + pr_info("x86/fpu: XSAVE managing features 0x%llx, context size is %d bytes, using '%s' format.\n", + xfeatures_mask_fpstate(), fpu_kernel_xstate_size, boot_cpu_has(X86_FEATURE_XSAVES) ? "compacted" : "standard"); return; @@ -879,9 +870,15 @@ void fpu__resume_cpu(void) * Given an xstate feature nr, calculate where in the xsave * buffer the state is. Callers should ensure that the buffer * is valid. + * + * This only works on kernel FPU buffers, like task->thread.fpu. */ static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr) { + if (!(xfeatures_mask_fpstate() & BIT_ULL(xfeature_nr))) { + WARN_ON_FPU(1); + return NULL; + } if (!xfeature_fpstate_enabled(xfeature_nr)) { WARN_ON_FPU(1); return NULL; @@ -1235,7 +1232,7 @@ void xsaves(struct xregs_state *xstate, if (mask & xfeatures_mask_independent()) xchk = ~xfeatures_mask_independent(); else - xchk = ~xfeatures_mask_all; + xchk = ~xfeatures_mask_fpstate(); if (WARN_ON_ONCE(!mask || mask & xchk)) return; @@ -1272,7 +1269,7 @@ void xrstors(struct xregs_state *xstate, if (mask & xfeatures_mask_independent()) xchk = ~xfeatures_mask_independent(); else - xchk = ~xfeatures_mask_all; + xchk = ~xfeatures_mask_fpstate(); if (WARN_ON_ONCE(!mask || mask & xchk)) return;