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: 13145676 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 55929C636CC for ; Sat, 18 Feb 2023 21:17:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EBCEC280025; Sat, 18 Feb 2023 16:16:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DF9DE280024; Sat, 18 Feb 2023 16:16:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AEBB8280025; Sat, 18 Feb 2023 16:16:32 -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 9DEB0280019 for ; Sat, 18 Feb 2023 16:16:32 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 811FFA02DF for ; Sat, 18 Feb 2023 21:16:32 +0000 (UTC) X-FDA: 80481671424.23.0ADB730 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf25.hostedemail.com (Postfix) with ESMTP id 85B23A0006 for ; Sat, 18 Feb 2023 21:16:30 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Q+vf45Ln; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754990; 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=JBsixjohsb7aDhNhJq/TrQJH3+e+COprL58KvKEzftQ=; b=bIo6YlQlK9MukxAIH4JSS3u+06lK/2M9iiF9rw/QKqxZIis6rARRka8RcsDpjJdVc9EZY/ pFwrGbdYKIR/IWuISYzhSYT5ULmeuvTMbEYLsl7cy946CJY8902rCgaIuyl5fN2m9KuWzW 0geEs6E8GXaoBous7+JuCs8K3rgEF6U= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Q+vf45Ln; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf25.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754990; a=rsa-sha256; cv=none; b=CMqz6Quynqw0WV2kK7bXgd8uN2NuDy+FGCipniKFo+75o7z+5EQ2xyPU4p5//oVy9EzERl 86VBmJ9K8iKjE2dZHTavgfrK5OaWyMHf7lyyYVsJg4tA5laMuGuiFZSpFeFLu5t7Ba6IEu F2V1ZoQBTiZIkcHzFzu20RdDposwHB0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754990; x=1708290990; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=YxwDoMp5u+7K/cKvKjbEHedNxVeyXtrjkBbg4BbPtvQ=; b=Q+vf45LndY6+WFnKci4be8n28cUK9vpbcyBBhlJAeuTqvZzDF4zaeI9a KZM/wqf2I15vrqEWqEQIp5PrxNdkuAompr290npTiSfXyXZRLLLnyzr0P jhxfXEPV+reHNPv/P9SH318Tuuuc8nvHwjMY8tjNtQqHzcguZchRjeYYZ sOCB/i02M7UOrqd4sMJML7Mgr8CYZrf5kuxx/T1Gw+rOOqld4v5KzTQ0I dUvqmg+N1tp8RZ8vLkM/m7jRWE72BsRUwqv75+MiUEb5QJX5kGy3lCcao XSEnJ9+B90nmKB2reVNpYBCxgnW1s33sW+78Me5pusAs/UwOgIWIc5hmH g==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427916" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427916" 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-Rspamd-Queue-Id: 85B23A0006 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: nnqjsdh9ya8rdgxnspwbjfi6zig1onbb X-HE-Tag: 1676754990-383503 X-HE-Meta: U2FsdGVkX1+EpwDG1L7fUUzatQZfP/jxCMxH+B2fmwUeWn+LjE70YJEKxkwcpCw0pJjQYBIbQbizOG/y3rgTDQLXa3yH0ExCAfbD28KlLYGXi+VIjw4QBTPPl6YPl4nV+GKPjTyt+t2x7r+a8xNmpnky9LeeyNB+mBirsrpnqxqP14e81Mt7m91Zg41fjmCdE+BH2TcdU+nGz0QrqP1cay3bjCiIw6blmqijg+u2AZT/P0F6wymx6cRVEi+TyMkQLqKIcGEKaWFu4W5Ysl2QHG4hCW9niVmLWb/ByZnl18rXgjGOQm1LEa9TbTvzIpWnk60tV57ymWm72uWWeEhAPxAOgKVvZ1+ErpafpPtEGXs1O7EGwZRNQTueHaSs4MbBb559v29HdI88QxoG5hcqonDGYMJ/uarTZVwgwENLvHV/5mzmDKacjsGAN/lms94DWNMW18XcKzbItzznlGv28/1iTYrQKsuzylZwfkTE+Q0HOE8uhbeiFYefQ4YuexgDFO62XOeOcohacqTYkM60iC2z/PvXVegUf+zFF14h8XMKVN+JxfUZCWVW7T76hQQUEZXU1hvD2zDscgBCNpYK6pISPJpbXyudUqt37yrf0xOuQ2lcOdYm8QtcktHecr9kktpZpho3dbGfq1QA58LYzKrqtJYyGh4SKT95exm5WSguyH+c1pwdzlZUJq4+psGRpxqJWjLzUn9ox/TgwmdxvGLnAMZEZEruIGMofdPWjDN2YGTpJ0uhoGWy7mEk5e2k+/MrxIr//+LNXXEW+uiYfV3TGlvUV9lxhUuMl6FbgxMCuTeMAsEGgh32sELjqjqR8ZrZFCsQZiwYKSHquZn7vxF2myal+oZjS1pI11+M/HPKSNWA04UVkcZIFQd1AIL/VLEQplyIGpDYrvSRHOJWL/wF6qURjL49rm222l2Cj7Un1UyURI+4PO8Dw6GjApWMr5CzBESH9pJUZhmt0Xm NphN9eN/ zmkrx3by9sBqHOL/oT0c8fVO4RKZLDTChiLx1hLObr/lgKTlxbigZEmxWpv7CiAS35vj9ez3+sTqtor8gJar8nRZ8AKpLPNylGDwvyH4zPBqDYB3SlTyKj2oLBUUlo5cUKoP0yicmZWsBqnH86rxfZlwXIPhfB/RmIdVzVTmDzBOliTje4LUgi/awKNUK5ldaMWIH+P1ptToamLPdnTyo5+IV9nPRkX5y9/x9SbxsBhFyfv8= 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: 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) {