From patchwork Sun Mar 19 00:15:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13180134 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 D6DA3C76196 for ; Sun, 19 Mar 2023 00:16:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 46A46280004; Sat, 18 Mar 2023 20:16:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3F33F280001; Sat, 18 Mar 2023 20:16:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A96D280004; Sat, 18 Mar 2023 20:16:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id F1793280001 for ; Sat, 18 Mar 2023 20:16:07 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C3375160C80 for ; Sun, 19 Mar 2023 00:16:07 +0000 (UTC) X-FDA: 80583730374.23.E3C9C68 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by imf16.hostedemail.com (Postfix) with ESMTP id C1053180006 for ; Sun, 19 Mar 2023 00:16:05 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QPuaQR5V; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.115 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=1679184966; a=rsa-sha256; cv=none; b=VmkHpSuYjTQ3uHhg9bIG3C6Qklq72+RTWv+GuIL77pm6DXxbWUNVnRGKtyHjIzb13az6o/ zfrIsvFUqyNhwRXZFkQ7n1w1NtFbiwCay/cTCGb0fbj36NbyDrshKStIUHWnUzQiYOQMjh U2AYD9qwXf4DESqiySHhblGY22YPc2w= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QPuaQR5V; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.115 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=1679184966; 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=+zckbUWRWZvfPROGysXYXzeczc+/ZvxVdCvyMsXY99o=; b=bdVqBnf+RofA6H9lHVvJvmVg/578kLvHg3WZMGMCFWnGQhSqbYEeNg1jeuFU+bqAjZw8nn feWvM/SkSHkNaMDe7AyH3YhW0f53SRNlFLLF61mNV/MSPt9+/IVOmXrvY7ui7lVNwiQRav WpF4V/vGC2DDcH3CZxMkbCZDfXJQ82A= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1679184965; x=1710720965; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=JJ3ES6pKzLeV3jf7I3FnSvx/TVTiB5ybDZUP+4xGp5E=; b=QPuaQR5Vi4d3Z/x3CyUxK59vRUKuToq9j9Djl3/AZNL5VFrnUhj3+wUS V+e8hk9uYSdvuoi7snnZcabq/H97SMjgC/94Oiyhn4OBrSpsOjSsWFMKD 0sx6AGCXVvti9Z3q2ksWZj5JnmwWuILMgcnTDdsGO6Y/xCr4bHZxJazG9 Si0JexYLGLmk68X+e2DuZPa7ZDHZe7y1nuq4P4aAdoZjLBy1S+uSgzatH cy0tvjF7m8G+QX3T6zROWHoVh+MfCc4WON3GU8QSrgZULpwTQRKx3AAo1 BFJUin+YZv+Z8HBnlSKGexLK6XJkxCKnAtpOWNI39avew12wcnf7Z4WZs A==; X-IronPort-AV: E=McAfee;i="6600,9927,10653"; a="338490860" X-IronPort-AV: E=Sophos;i="5.98,272,1673942400"; d="scan'208";a="338490860" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2023 17:16:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10653"; a="749672788" X-IronPort-AV: E=Sophos;i="5.98,272,1673942400"; d="scan'208";a="749672788" Received: from bmahatwo-mobl1.gar.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.135.34.5]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2023 17:16:02 -0700 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, szabolcs.nagy@arm.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v8 06/40] x86/fpu: Add helper for modifying xstate Date: Sat, 18 Mar 2023 17:15:01 -0700 Message-Id: <20230319001535.23210-7-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230319001535.23210-1-rick.p.edgecombe@intel.com> References: <20230319001535.23210-1-rick.p.edgecombe@intel.com> X-Rspam-User: X-Rspamd-Queue-Id: C1053180006 X-Rspamd-Server: rspam01 X-Stat-Signature: 1rg8duppp8cqacpnqhd8x79kcgsboar7 X-HE-Tag: 1679184965-826996 X-HE-Meta: U2FsdGVkX196o1YugrQdV8G50lSj9vew4gateym0I7/x6rWz6tP16coVt0+uW2SnLyfV43oLc9eS8IQUrrCDL6bw0TUc1LU/mJ8ntgUXbvAbQB5airPgvdm468yZquS6UoAeGxwU3fUgGS1cvFKCYHS12/izUpC4Ovt9PGNiP5FxnteA7ubJ5Vlmtz0LPnAWvMAjXJ5Lxf1HR0OrFtD+UF2WP7bf+AKFdbfg8kVxAXcXymKGp+FFT0FruCqFZIDNEdZ1JwG8xVPAYoHx6vWEwreAowvW4mJJQModcAsEYflhpLn+W88g/BMwkMlsAFDj8gjl+ln83lJU8u1GsSio0ncJExM2ckbLKdlLe2wjRxCOs9vG+TkPt3gjQnYp3JHjt5m/tU6Tf9TcnEAxCS8EgTGkGUHxTCPP/O3E4ayJlIdi/l6yU24bD1dOrWa8LfxoFvQt/GrZzEHT7bqhuXa9X+MkN9TYgFRSUriDv27B1I3PLIAGSy+5YU9NcSz9jmP0RVyPsBj1/T1i4r4RVvvr22nlciRS06nbPy3HC374wYiNMebOORBvudhN1gfgyKuMhtRg/dRqbIju/hjdHEwkAiO6grJQ2erfOv87WJVZBEgp5kakk9jxRQCRMvoGqy41Uu7VigT8C46UJIdl/DJU5AcI82s2nKPVnKux/lZTyx7/uiqWTroHRWE+TuQ0xTh1nm3LmEo8aX8sbyrrwniBkY8sazy/oZtJvAKNeLkojMEy4tQR8cJL9lCKmR+HY/HOHWCf0ShUrmdBCaHIzoUlugdVzwlVg9F1s8XTRliqTOFI+Dr8eJwUkL7TJYQE7Y4LUmmXOd11F3L/VZOsGEUsK6+ybLUjSQgh4ixAASPHxshT/pF6jwwU5+eCSdMuI+2yFElIO3VjW/mukLNYCaHH2XLB937UB780+sse44MPqJxiReYIIgKn8AxuijKmULgXLaGCtcXeSc+5H0Dvacb CEmR7Cao 1yhZy5WU15mfNACcO2GwnEexg9gh4o9psN9UexUoSDjT3Mz6zhaeW8NZShLE4NSf+WVpO9QPTxHzRODL9un3ncdiT9SlY06WEYh2A4lx62KFnziDWKURpen1vPOdlWv/ZVzqeUJAWuV50F7/6AAEsLu6ydMhY+/+Vi/Lwz5URCg2njTPLLr4cEbudJF9fQdc9RfVpQBqLM7tniLNGy9icS+v6iTp7I7lA2FwNx5l+PPc2r2agXKBmDgajhAgFW8X+3MqqXu7Cf4r1MRFRJjB5/wX8HxdFe4+v9LpFji7bE1wZKEN1Tn/4Z5jIdpqPwCgNQIvNSVJqfbHb7n42/rim98Zz2vCC5yMRKsRq 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. Suggested-by: Thomas Gleixner Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook Acked-by: Mike Rapoport (IBM) Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook --- 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