From patchwork Sat Feb 18 21:13:58 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: 13145645 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 6012CC678D5 for ; Sat, 18 Feb 2023 21:16:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B135A280002; Sat, 18 Feb 2023 16:16:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A9D0C280004; Sat, 18 Feb 2023 16:16:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8EE8A280002; Sat, 18 Feb 2023 16:16:07 -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 6EE71280004 for ; Sat, 18 Feb 2023 16:16:07 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 4A131AAD71 for ; Sat, 18 Feb 2023 21:16:07 +0000 (UTC) X-FDA: 80481670374.11.8DD960D Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf12.hostedemail.com (Postfix) with ESMTP id 6155E4000A for ; Sat, 18 Feb 2023 21:16:05 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=XE1WC2ce; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676754965; 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=1qF79w3tBeVewLR/G4DWxS5EV45VnTvhj/Fuk26jDzE=; b=AH7z8Pqp8qCUHiO901g2sZzAePaAdirMJk9/vzcyC4y2Sq7Tkd2CBdfXRRIJyRmiptRvbq D1wYTkOLScvirPZ5iRlVuB2E5aM4HMzvGEDa8Q5LqpSs7vOT5urxi5PuVyOaFGbMz4e01X mcsLLXcnzrGsQImxaPWkHmJaFn7rcj0= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=XE1WC2ce; spf=pass (imf12.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676754965; a=rsa-sha256; cv=none; b=Qi0BLIJBBBj/w0sRLm7qgRw87HAEz6m69SfuxMKHJO0ce6OuYtpAQU7+FeCGbOp3Pc+8aX ziZuiUyhrEETc08Y2pXLMAZ5jJMo00XRjfXM7e34KsWtF53Nvf39p4CF9e3Jq5YR/E14PF +Y7Eba00ntZ95XORFf6tfMbZaRkI0Yw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676754965; x=1708290965; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=7jVlFLlhK4ICVGezzTiai5C8Zlo8EPl2MrkiyOokM44=; b=XE1WC2ce4e9PML4/uY9JnyglLzzzUJDXV+ZHO5EgA+IP6/1yt3cs+fDG B/qquk8R49JSWFfiqd4nPBS2yrWayG/AtcsgDnXRb87gbcQ3N2Mgvyhv0 elFZ2ddYxbPMnat+EjukYxnxpiq4bz7PTb/YJghmPj5LuRazFPaGcODCW 7qOIdtMoKFq/H1w1DsDeddcCd7qcr8q5xbUNJ63BkniEk6ucfv9xJJ4Kh xLets9rirtyS3zlrNpl0+OZmiSJCp4d+tTsjgdX8UtfSgGC8MJ0atixBJ 56AA+SR8BX/XpXlvJLcGEeGanO5cEKK/DtKH8ZNbEoBGCuR8boYm1bm8Z w==; X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="418427211" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="418427211" 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:01 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10625"; a="664241594" X-IronPort-AV: E=Sophos;i="5.97,309,1669104000"; d="scan'208";a="664241594" 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:00 -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 06/41] x86/fpu: Add helper for modifying xstate Date: Sat, 18 Feb 2023 13:13:58 -0800 Message-Id: <20230218211433.26859-7-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-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: c4poe1dkmt6heo84umh4twtyan4m9kho X-Rspamd-Queue-Id: 6155E4000A X-HE-Tag: 1676754965-577915 X-HE-Meta: U2FsdGVkX18KDSrD2TM/IVCwpS2XgqwCt3o0o9lJU2CQ8k3pW+trVgFZSs+hQi/5ViC26XylCxmpqx2A5a/rybfyCWCaFN9yGFzOT4auEeo2bReBF+ndNlYFMsueH5jUYvh6wOCcCPgydtqnLp9JJTp3LsD/qesdUUjFHk3nxkq7Y1YNE01hMMYNkfO/kUQDTqNpH1j+yrTUkGuU0HjSFStWgiN1BIf3Eiy1NxJuxsEHYPpRYKQ8PGvDMscwdQpi2uO5C0yEHqHxw9+K8/A5kB+9uxkkhH2MUn9JRRrxOIxDXJCoSb+9hY1TFogvgAHPc7H1seA1OIhVGqOtKd21+StgIVpHlnHVsW9kgzuZjEqMqYqEr/Yt2P9/uC7eRtDapGGQSfnoqLtt0eHzC30M0+ZXCPhXp0Y+KDdwh3nxySd8qEivnk8KfysWcGWNghn2DV4Mk43KxiFUbm+Eb4I36BV07z2qjbHiRhTaqeg6aFkCfrjafjhcPCm9v5zrp0A+VTqdF6jj0vmMri3z99OxDidsIuxmLZdnZ8GGFlkxaoRGowNZ9uO0xVDZRlf8ErT/luNr75XOdGGxBkx9IFP6v56r2dBlVC0oJ2vSdWcJdBdlEdD1N6uMqzmrRlRT8oAkVU8pvof7mZVzU9TPDYMcQhwLYX4I2ZZQ02SgfjxQ6naZYjTSY+C70JdF00SoxGht+dQsxc/qaXiNFVHlxkt8sCJi0qx/iFx25qgaARixY2lugf8Av2QDTBstj/0lAm4jXRWei6AjwBgtizfhfHff4Eh2eqiHl+PgUIMBNOC8pBH1NOFsExLPoWrcNdlxkg/89d2BSjZwDnlcc/fsnU7XjHKdP0dZZdzXhYhMlZpDKQJCwcpi/KUKpNbR0Wq5jVzu5fMQA7Af3YQCq1yC24+Fl5hdAi0CvpvvUq8sgF1rsDWc+r04Vi7yrWZY91g1V3QvVKJ0aitWSAHUCPs+RXs J/5Gv3yt fVfZgWONkvBrSfDZdCNpzxwkV5UHPC0NtY1/mRNOo9F7g2/uCSYUTI52xNrp5ECLx40iCaPJDtClMtN1TR/Z32mAjGR5yOpgR1c7wFtMhgwM5PTn8w1rijzcHdJI4Xl+N6YmFyuez9ID478/6V/4O45ATubIfxkk1/08W975Vzehww87/DXLc36SUvCFZDrXWaqvILz3q4YgTmjrTYaNK3vzUaRPJsbQci7TDw6EXevZqI7VpjTi3LBISxb5AdcZrL2K+9EsCjEnmMb6mCPiz6JdKglWJN4ivF3WY 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: Just like user xfeatures, supervisor xfeatures can be active in the registers or present in the task FPU buffer. If the registers are active, the registers can be modified directly. If the registers are not active, the modification must be performed on the task FPU buffer. When the state is not active, the kernel could perform modifications directly to the buffer. But in order for it to do that, it needs to know where in the buffer the specific state it wants to modify is located. Doing this is not robust against optimizations that compact the FPU buffer, as each access would require computing where in the buffer it is. The easiest way to modify supervisor xfeature data is to force restore the registers and write directly to the MSRs. Often times this is just fine anyway as the registers need to be restored before returning to userspace. Do this for now, leaving buffer writing optimizations for the future. Add a new function fpregs_lock_and_load() that can simultaneously call fpregs_lock() and do this restore. Also perform some extra sanity checks in this function since this will be used in non-fpu focused code. Tested-by: Pengfei Xu Tested-by: John Allen Reviewed-by: Kees Cook Suggested-by: Thomas Gleixner Signed-off-by: Rick Edgecombe --- v6: - Drop "but appear to work" (Boris) v5: - Fix spelling error (Boris) - Don't export fpregs_lock_and_load() (Boris) v3: - Rename to fpregs_lock_and_load() to match the unlocking fpregs_unlock(). (Kees) - Elaborate in comment about helper. (Dave) v2: - Drop optimization of writing directly the buffer, and change API accordingly. - fpregs_lock_and_load() suggested by tglx - Some commit log verbiage from dhansen --- arch/x86/include/asm/fpu/api.h | 9 +++++++++ arch/x86/kernel/fpu/core.c | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/arch/x86/include/asm/fpu/api.h b/arch/x86/include/asm/fpu/api.h index 503a577814b2..aadc6893dcaa 100644 --- a/arch/x86/include/asm/fpu/api.h +++ b/arch/x86/include/asm/fpu/api.h @@ -82,6 +82,15 @@ static inline void fpregs_unlock(void) preempt_enable(); } +/* + * FPU state gets lazily restored before returning to userspace. So when in the + * kernel, the valid FPU state may be kept in the buffer. This function will force + * restore all the fpu state to the registers early if needed, and lock them from + * being automatically saved/restored. Then FPU state can be modified safely in the + * registers, before unlocking with fpregs_unlock(). + */ +void fpregs_lock_and_load(void); + #ifdef CONFIG_X86_DEBUG_FPU extern void fpregs_assert_state_consistent(void); #else diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index caf33486dc5e..f851558b673f 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -753,6 +753,24 @@ void switch_fpu_return(void) } EXPORT_SYMBOL_GPL(switch_fpu_return); +void fpregs_lock_and_load(void) +{ + /* + * fpregs_lock() only disables preemption (mostly). So modifying state + * in an interrupt could screw up some in progress fpregs operation. + * Warn about it. + */ + WARN_ON_ONCE(!irq_fpu_usable()); + WARN_ON_ONCE(current->flags & PF_KTHREAD); + + fpregs_lock(); + + fpregs_assert_state_consistent(); + + if (test_thread_flag(TIF_NEED_FPU_LOAD)) + fpregs_restore_userregs(); +} + #ifdef CONFIG_X86_DEBUG_FPU /* * If current FPU state according to its tracking (loaded FPU context on this