Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1522100pxb; Wed, 20 Oct 2021 06:48:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzcH/l1OFtLzgh8ZfT7ijFDXupfaZI2zxpYpWfgY4mnoH1N1tuoh1IEalBccQwfFThTLa22 X-Received: by 2002:a63:6a05:: with SMTP id f5mr87333pgc.398.1634737715184; Wed, 20 Oct 2021 06:48:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634737715; cv=none; d=google.com; s=arc-20160816; b=NjvyfYlAvQVWDfAVIz2Pr3URGbLnoOiwApt0fJkt87rhezjXFyei7n5X7ug4KBaD2C yJD++DMsbad173GFTLIeCgpVxIefq6Li/OUDcWIfHxPOxcegpCMaK/m0uO2fBcozqfmn dbVIFRY+teSwuKTpr8YP8KddXuNPTF6KCYZ6XU4ytYHiBtnf3o4o/WKZ7R7I4B0zcE5i ea6cd54YcYQP0+ur1FNxeMXvJ64j1iyyT4ytjZVN1c4QXnIyAITa2u2Mo741rH/eXvj+ 7cJyGC546X0YTyZYnMkVILYUgcaUpnGYh2A+GySFkzpczAjUIyuc7lG9IdVO3kCSvZTR 27OA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=b+MJi8yWzhDmj5AxIYMmDl7AcBvxbFIfUkfoZozo79E=; b=ap/1ZZGvPVgBQEmz4WAPlbeA3yQj7DZdd4PffEKh78wnh/8lQ2HSA1A1YGEwA95IPw +f0wEmXuUdf4Ta7wRRSJtLby63UxWAc23BxCLAoJw/+jsMwy8k3jyUalKr5Jbxc+UbYs 4kDSu/J3aCrWSeb0pqmg/n0L8kjrIZcsBbRCXw/XU0OGLe+3DnWIL7sdE0zRjwTXVVTL X8lCjn8i60A5PQMQaJWEmBfKXvkYEHioVmKU64Av3rK7XAwP7p7BiTos4LvuX9x1M4dd zvg9pfhwCrqtvRzg0negx2DmlEoN7cCI5V0OwgS3MpblBKRkon0WvELfzPCbKR9lh3D5 qvpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=B0yqxWpE; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=7tBBu0uZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s15si4293383plg.442.2021.10.20.06.48.22; Wed, 20 Oct 2021 06:48:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=B0yqxWpE; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=7tBBu0uZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230484AbhJTNrs (ORCPT + 99 others); Wed, 20 Oct 2021 09:47:48 -0400 Received: from Galois.linutronix.de ([193.142.43.55]:53026 "EHLO galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230347AbhJTNq5 (ORCPT ); Wed, 20 Oct 2021 09:46:57 -0400 Date: Wed, 20 Oct 2021 13:44:41 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1634737482; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=b+MJi8yWzhDmj5AxIYMmDl7AcBvxbFIfUkfoZozo79E=; b=B0yqxWpENcGRFCshmQ8gG29IdL4PoI3w+qyZNMPdtRcw0yXvUWATPEgABtbDc9YDAZiHg2 S3V+jE+98g7dyRCeznjL7hFLqe0lJPpmm1bhoUPmnw1VmXlwpAXx6NIDbDNukfNCWFgDNb EUuCCzobNqLlCIejhKex3o/ung5bXyWI1Y+r0RlsiCRGkkOaaIyhN9PqirsjZnoa2ViLD2 0YaKWuE2ikDcvGvjHHnhO6NKQW9AEnw+f53fOjii1wfFPrxh018Plml5URtOaO3wGinfP4 L0ed+HThl6m3ik9CzPgPMKz+EG8y+Aze2rfEph36urlCKfJ682H1RSg/ODjBQA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1634737482; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=b+MJi8yWzhDmj5AxIYMmDl7AcBvxbFIfUkfoZozo79E=; b=7tBBu0uZugb35PwjydQHUvqlX8Ry7qWDu8b3VMQVpFB9HDeOab4IlHPZrfF1+NvRh7m0SM w9RnBqLNUlrxyYBA== From: "tip-bot2 for Thomas Gleixner" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/fpu] x86/fpu: Do not inherit FPU context for kernel and IO worker threads Cc: Thomas Gleixner , Borislav Petkov , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20211015011538.839822981@linutronix.de> References: <20211015011538.839822981@linutronix.de> MIME-Version: 1.0 Message-ID: <163473748163.25758.597185307000575063.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the x86/fpu branch of tip: Commit-ID: 509e7a30cd0a9f38abac4114832d9f69ff0d73b4 Gitweb: https://git.kernel.org/tip/509e7a30cd0a9f38abac4114832d9f69ff0d73b4 Author: Thomas Gleixner AuthorDate: Fri, 15 Oct 2021 03:16:06 +02:00 Committer: Borislav Petkov CommitterDate: Wed, 20 Oct 2021 15:27:26 +02:00 x86/fpu: Do not inherit FPU context for kernel and IO worker threads There is no reason why kernel and IO worker threads need a full clone of the parent's FPU state. Both are kernel threads which are not supposed to use FPU. So copying a large state or doing XSAVE() is pointless. Just clean out the minimally required state for those tasks. Signed-off-by: Thomas Gleixner Signed-off-by: Borislav Petkov Link: https://lkml.kernel.org/r/20211015011538.839822981@linutronix.de --- arch/x86/kernel/fpu/core.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index 191269e..9a6b195 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -212,6 +212,15 @@ static inline void fpstate_init_xstate(struct xregs_state *xsave) xsave->header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT | xfeatures_mask_all; } +static inline unsigned int init_fpstate_copy_size(void) +{ + if (!use_xsave()) + return fpu_kernel_xstate_size; + + /* XSAVE(S) just needs the legacy and the xstate header part */ + return sizeof(init_fpstate.xsave); +} + static inline void fpstate_init_fxstate(struct fxregs_state *fx) { fx->cwd = 0x37f; @@ -260,6 +269,23 @@ int fpu_clone(struct task_struct *dst) return 0; /* + * Enforce reload for user space tasks and prevent kernel threads + * from trying to save the FPU registers on context switch. + */ + set_tsk_thread_flag(dst, TIF_NEED_FPU_LOAD); + + /* + * No FPU state inheritance for kernel threads and IO + * worker threads. + */ + if (dst->flags & (PF_KTHREAD | PF_IO_WORKER)) { + /* Clear out the minimal state */ + memcpy(&dst_fpu->state, &init_fpstate, + init_fpstate_copy_size()); + return 0; + } + + /* * If the FPU registers are not owned by current just memcpy() the * state. Otherwise save the FPU registers directly into the * child's FPU context, without any memory-to-memory copying. @@ -272,8 +298,6 @@ int fpu_clone(struct task_struct *dst) save_fpregs_to_fpstate(dst_fpu); fpregs_unlock(); - set_tsk_thread_flag(dst, TIF_NEED_FPU_LOAD); - trace_x86_fpu_copy_src(src_fpu); trace_x86_fpu_copy_dst(dst_fpu); @@ -322,15 +346,6 @@ static inline void restore_fpregs_from_init_fpstate(u64 features_mask) pkru_write_default(); } -static inline unsigned int init_fpstate_copy_size(void) -{ - if (!use_xsave()) - return fpu_kernel_xstate_size; - - /* XSAVE(S) just needs the legacy and the xstate header part */ - return sizeof(init_fpstate.xsave); -} - /* * Reset current->fpu memory state to the init values. */