From patchwork Mon Feb 27 22:29:22 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: 13154237 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 E76EAC64ED6 for ; Mon, 27 Feb 2023 22:31:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1687D6B007E; Mon, 27 Feb 2023 17:31:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0FCBC6B0080; Mon, 27 Feb 2023 17:31:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E35F16B0081; Mon, 27 Feb 2023 17:31:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id D6A5A6B007E for ; Mon, 27 Feb 2023 17:31:35 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A6237160977 for ; Mon, 27 Feb 2023 22:31:35 +0000 (UTC) X-FDA: 80514519750.10.FEDF25F Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by imf28.hostedemail.com (Postfix) with ESMTP id C2B9CC000E for ; Mon, 27 Feb 2023 22:31:33 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=drISy8Ph; spf=pass (imf28.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.136 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=1677537094; 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=MvWt7ReyhF/wn7NboBw5a1sJLt/UxAkay4sKp6Ow5WM=; b=IOte67wezZdvn79i0jgALiJeepv/VCzXu4EBqfkddqdwiIAnoRLZ4YBgO2iTwRH56JCpQu LCgZAFlyt+Nk/bGDhkC4EVx13K/9zZHWlz6JGzAv9moCWYjlC2qoQT2LUoazFwsgydWbM1 myISWS9AB/Z6Dn5g4Fj7hk/nMI2GUzI= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=drISy8Ph; spf=pass (imf28.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.136 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=1677537094; a=rsa-sha256; cv=none; b=4NsoeMMqQ4htDet3T31rzsBVwxr7JrulBHPaBPZBchbxDBSNJBLrVhyRZbUKJ/dmRA7/Yt 5GDrr6o+JTONsF4SB2QKzaGPTeFcaEbSw5VCyHGAo5Tses3c+bcgX3fjQquUnsFwpd9Gaq anUAELLYy6UbbjIIplvkHvg209lBCt4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1677537093; x=1709073093; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=D7r3sPYzuOicvyDyAiCcjJlcjrFZlOzlMPKscQsbvdA=; b=drISy8Phx9ojyjpgcEFsd/xyYBD6/+Thpa3Wfy2w258MtzF0/ZPPyBQw 7j7oswAS1oVGSDzDs0Nv1p56+vSga1alNvgB37n7JYUKnm6/6rasgCuW+ Kylhf01j8LJkfjM70F7s+ZKdp1KxYVQT4xwu3UxROC3wNHffmlQGAgSlW c8An4A79SpmvfzEatSIAfIauZDAvsX41svFP+jK6Q+Q+sTxSA/42sKrey qmWStOkIcNtJRm6aXK12b//cV7oCNPoSO/308bA7DST5AlO9f1tgHtGQq GpcqmzyA2gA1yWdzMOCKeKL3Ko5f85SJg4TLA5/mEMSbDYMqO279e4136 g==; X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="313657134" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="313657134" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2023 14:31:12 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="848024393" X-IronPort-AV: E=Sophos;i="5.98,220,1673942400"; d="scan'208";a="848024393" Received: from leonqu-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.72.19]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Feb 2023 14:31:11 -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 v7 06/41] x86/fpu: Add helper for modifying xstate Date: Mon, 27 Feb 2023 14:29:22 -0800 Message-Id: <20230227222957.24501-7-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230227222957.24501-1-rick.p.edgecombe@intel.com> References: <20230227222957.24501-1-rick.p.edgecombe@intel.com> X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: C2B9CC000E X-Stat-Signature: 8wtuo3tdikjia3748i5w5garehty64ry X-Rspam-User: X-HE-Tag: 1677537093-810099 X-HE-Meta: U2FsdGVkX1+XOJYtrSIfDQf8mVX1FjhvydMkQC0RrrMSZExcyxWccO6FJYxep2fG8t6HYaaT7d+zzgei8OE5xzO32KkEsLjXnkGyg1no7vaX7d7zimb9Q4VJA7WfdSfG7F0NEyfvwnD27ap4HViSSA0w2bdOahNrRM7ZtCaqUj6hPK/TvO5RpSPkUynPfxPTo3lYscOZkL3Gs74Ww2Jg2ZEhQQXMz1WFPCOUDvC3+R1XwBkb6o38V4QLlDWZdtsUVS2RPuVwGUQjwL48SXSoxIJNuNkndtA3gQQtuMo5k4mxRA+wYp6H9IrDVhjcorBBvYj1V66oLfIkjtATvX5O6hqPqj8CyxMZNHrpuklsIMKegGyzREksjnvtcVNlXIkLBqPWBR2/WrbJv263zuTDpZfaHNBjc48XsGsSiLOUf9fVRFkx6doWkeRkDqaPw5O58w8TTvcFwlV+Wmozp+Qfsn4CdFYfv/6HqN1xL7WyEqILymQbVLvqMaAL01Gk+sbzH/GFxUhofeKb+q+8jA854hCUETYr3lo9fb2sPgOkrdiDljhvf9HFS4s9vFRXqy1yFZcM7sff3a4ljA9bOKsc3EB6JV7GbIwLZq5L3hGG9XfyxISCmZV7Bc1hmVtHcs/Yrh7DzHExjjirpgls3xiRzk4oQ6vDXUyct9T6LFVPM7s9IrISqILw05jukp9VhdN203nFRODwmWWrvA1QB+ysaUJO8xgSIVcr3ctJi8qS6sMvGG1Ui5flz3/NlSDmRu7ab1q28b2um2UhB2QBIp2AAzxTXZJk19ABvuNYv0IT6EK4nc2QbGCgBySc/4PBdEbD2wX5OrUIEAxV1oKoo/iBXZSNKyFvjzKXLaJo1kgRsnimTFZzz3DLKGGtt5m++C/rxrWyN95+qGmYPrIdBts7F4CFL+gERQ9ZJmdu8nJUzH3TAUsVzXdMhTp6SQdcntKLeogNyiwYboButfFlXde lwrBgyWA Eq8dbYktoovEOwIbYmiKp4qsDItbFSumkAiAe7IR4x9aSe8T4PG6h9WHICLEgS20hZnE1H/5TCR3RvLOhlqp9ymiYU/74WKHU63j/DaUIDy+iCNW5t+uXmfFMrFnOnPxpQVAfF4qqykzDVSz+y1XekSw9NY28PZBdL9bWDUCMol8x0nH3gnJWFtH43EVrjtP31+KKSmBHCEKQcAirBKfK6sGeFk6MxScc6vNHpClxd3iznSRv8Mb8dYe7b97tDevJ+dWDUBZuTS9IJGdb/URfgg1P0cTlyw91yWN3VPM2nzlILC+8+wR4f1Os34VTJD/tZeNSrHbtsvs4p2ITTyOXp9DBtTkzabRS2lDv 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 Tested-by: Kees Cook Acked-by: Mike Rapoport (IBM) 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