From patchwork Sat Feb 18 21:14:30 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: 59022 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp556288wrn; Sat, 18 Feb 2023 13:23:46 -0800 (PST) X-Google-Smtp-Source: AK7set9E4yfLqL0e1wwPH+aSho2/jCSYGPhxMNFHodxR5LZzqZPBl3AB3y4baqNK6yGhysowzMhX X-Received: by 2002:a05:6a20:116:b0:bf:5915:37b1 with SMTP id 22-20020a056a20011600b000bf591537b1mr4341396pzr.49.1676755425784; Sat, 18 Feb 2023 13:23:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676755425; cv=none; d=google.com; s=arc-20160816; b=ztXWkivKIlz/rG7/YACovS4OKzVQLA0x4+qLFN7GF/AjqhKxnvLblJj7DWXkF2qtmt h8+CFc4iS1slSge2u8MXaWb/i/7jwQY4PeCbCwf88zC99I2wneeurk7WBnCAGj+Bbspa dl1dKXveJVCzopmadC9eVfHN0aWfC+4prPQgWy1jVwkvozP7xXVtn6iPrnPWfxxzY2Hm ICR0wRC2Brz/Lyv8i5XDPbWk4fV8JgQURegQeBCrnMTTIHnzsMUQ4hIXf3908gL8Ek9D DzHQo/BxuBR9BH9b/gsaYEt/u43nScUDx8N0dQmmNhiCB/zTrwg/94MOmxFT4fXofixA XLgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=JBsixjohsb7aDhNhJq/TrQJH3+e+COprL58KvKEzftQ=; b=YHFtDP0BIhQaSbF4tq0lwJxAYGAuZs9AsMART67d4n13JHX2uFdad3/oRlRcOdKirT iJ6RlNu2f/HSJm9r802dlToUkEm4GcxCB6IQ+8ldyZlO0OFyFrSTeGuLDDORqrv0DrpW Wb798Qd/zQIa+uAEAJ+f/1PXXAhEGF0MIE8yaa23hN44aetyXqiQpENuP3RzVJgokrBr jc4CRXCiiPSKruEj2RUdg1GXy76ZS2ZINkwoBrrOP8aATciMFdBHKunoDSayksUKiyvB q0BkgOfdefZnqCjYpoXn9MwiXjN/UqfnpUXcuM+YwLD5pR+DWFKRgeMV4cXBl1U19cRk xnGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=nKtWAGDV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a185-20020a6390c2000000b0050239e95d34si2887814pge.260.2023.02.18.13.23.32; Sat, 18 Feb 2023 13:23:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=nKtWAGDV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230364AbjBRVXH (ORCPT + 99 others); Sat, 18 Feb 2023 16:23:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230288AbjBRVWh (ORCPT ); Sat, 18 Feb 2023 16:22:37 -0500 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E9B71B301; Sat, 18 Feb 2023 13:19:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676755162; x=1708291162; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=YxwDoMp5u+7K/cKvKjbEHedNxVeyXtrjkBbg4BbPtvQ=; b=nKtWAGDV7XSGGwqyOcAfeKZN/XPbRGCZ6jZyC2je0dAbwWvpfXXwtkan pDFFhA1VtO+nDTbdsL7bQVt6bgi+vEg/up4NVZmRbQK4MSW4VbG/5oW79 VkxKmFR5pHG6OFd5d9mc7FOD9OEXYibgLSI98U0K69JY0AtvEnVCID8lt auycTJs1Y9nhuliF4+ERWIByWGadbUEs1wl/Cf41mCJSUH+JGfvLohfXB aJ/T3qIn8/DDgmQ9fjU3/FWL/+Q4vn78DbD4CDEJi8OpdlVVlc8MYsknG 6cmFg6caLjT55aDL/5RLSKqFdPAcqShzPwj1E7ymaKkVh5x4wMuf3mYiE Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427915" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427915" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:28 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241755" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241755" Received: from adityava-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.80.223]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2023 13:16:27 -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 Subject: [PATCH v6 38/41] x86/fpu: Add helper for initing features Date: Sat, 18 Feb 2023 13:14:30 -0800 Message-Id: <20230218211433.26859-39-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230218211433.26859-1-rick.p.edgecombe@intel.com> References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758205497130628162?= X-GMAIL-MSGID: =?utf-8?q?1758205497130628162?= If an xfeature is saved in a buffer, the xfeature's bit will be set in xsave->header.xfeatures. The CPU may opt to not save the xfeature if it is in it's init state. In this case the xfeature buffer address cannot be retrieved with get_xsave_addr(). Future patches will need to handle the case of writing to an xfeature that may not be saved. So provide helpers to init an xfeature in an xsave buffer. This could of course be done directly by reaching into the xsave buffer, however this would not be robust against future changes to optimize the xsave buffer by compacting it. In that case the xsave buffer would need to be re-arranged as well. So the logic properly belongs encapsulated in a helper where the logic can be unified. Tested-by: Pengfei Xu Tested-by: John Allen Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook --- v2: - New patch --- arch/x86/kernel/fpu/xstate.c | 58 +++++++++++++++++++++++++++++------- arch/x86/kernel/fpu/xstate.h | 6 ++++ 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index 13a80521dd51..3ff80be0a441 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -934,6 +934,24 @@ static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr) return (void *)xsave + xfeature_get_offset(xcomp_bv, xfeature_nr); } +static int xsave_buffer_access_checks(int xfeature_nr) +{ + /* + * Do we even *have* xsave state? + */ + if (!boot_cpu_has(X86_FEATURE_XSAVE)) + return 1; + + /* + * We should not ever be requesting features that we + * have not enabled. + */ + if (WARN_ON_ONCE(!xfeature_enabled(xfeature_nr))) + return 1; + + return 0; +} + /* * Given the xsave area and a state inside, this function returns the * address of the state. @@ -954,17 +972,7 @@ static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr) */ void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr) { - /* - * Do we even *have* xsave state? - */ - if (!boot_cpu_has(X86_FEATURE_XSAVE)) - return NULL; - - /* - * We should not ever be requesting features that we - * have not enabled. - */ - if (WARN_ON_ONCE(!xfeature_enabled(xfeature_nr))) + if (xsave_buffer_access_checks(xfeature_nr)) return NULL; /* @@ -984,6 +992,34 @@ void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr) return __raw_xsave_addr(xsave, xfeature_nr); } +/* + * Given the xsave area and a state inside, this function + * initializes an xfeature in the buffer. + * + * get_xsave_addr() will return NULL if the feature bit is + * not present in the header. This function will make it so + * the xfeature buffer address is ready to be retrieved by + * get_xsave_addr(). + * + * Inputs: + * xstate: the thread's storage area for all FPU data + * xfeature_nr: state which is defined in xsave.h (e.g. XFEATURE_FP, + * XFEATURE_SSE, etc...) + * Output: + * 1 if the feature cannot be inited, 0 on success + */ +int init_xfeature(struct xregs_state *xsave, int xfeature_nr) +{ + if (xsave_buffer_access_checks(xfeature_nr)) + return 1; + + /* + * Mark the feature inited. + */ + xsave->header.xfeatures |= BIT_ULL(xfeature_nr); + return 0; +} + #ifdef CONFIG_ARCH_HAS_PKEYS /* diff --git a/arch/x86/kernel/fpu/xstate.h b/arch/x86/kernel/fpu/xstate.h index a4ecb04d8d64..dc06f63063ee 100644 --- a/arch/x86/kernel/fpu/xstate.h +++ b/arch/x86/kernel/fpu/xstate.h @@ -54,6 +54,12 @@ extern void fpu__init_cpu_xstate(void); extern void fpu__init_system_xstate(unsigned int legacy_size); extern void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr); +extern int init_xfeature(struct xregs_state *xsave, int xfeature_nr); + +static inline int xfeature_saved(struct xregs_state *xsave, int xfeature_nr) +{ + return xsave->header.xfeatures & BIT_ULL(xfeature_nr); +} static inline u64 xfeatures_mask_supervisor(void) {