Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp171870pxb; Fri, 15 Oct 2021 03:14:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxYNndvYRUm/rLYLqUMFgwu1ryQKkpmQb1j24pb6YGnqONzz/Go9hI2pZ1IGh4G5nGdOhBd X-Received: by 2002:a05:6402:2789:: with SMTP id b9mr16288765ede.142.1634292840841; Fri, 15 Oct 2021 03:14:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634292840; cv=none; d=google.com; s=arc-20160816; b=xKewnTuNs0P06m79QEHwi0ejOgJiGI+5hGkJLuM6aeAK+TOK0Pa3mw9qz3wvw/jcRQ T4mN7Wf4N4+57c+19lc3Nx2m0jpL0cv1cb8v5nWP2Qfc8+TWL9GIzMPZr68XIxpCXII7 WMyEonwYH17cfF90Qv+T0NMhu+nkD/7zGfpAANYDZmXy9v0Dk54AOa/bTKkvkN6NtzBR rAg5G3J7U3zdF+nbPue/FH2w9Srn6vh9o7KSmmhenaemlJ7x7MY11oAGM1P31UmgYaoL 1ce1ZBj9Fqzy9Pleo1pkxZdbvq4f8H0sDuTjyXjM8wrCqOQ7AIxjrBLOTtSmm9HIz6D6 Y/7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:date:mime-version:references:subject:cc:to:from :dkim-signature:dkim-signature:message-id; bh=p/XFR0aVQMKKxVzOYz6MXsECkHIm28ch1vdabn5duRg=; b=CGe0SbtjDZ7YzIpxO07pJoC4ideUIPf30NDjehfur1/y2LPBpQRS8ZfGdB0IUHrT+T wBawFgW+KHK+Sw+AP4ehecmrJrndmOUSTm4NX2byOhKkUjvElc/FB7blolxTh347ZNsA ZDpaX4Mu8ND3XhCCYVMMaKezDw0XQHLxupIRV7oH/VC28R0QQyfLgpWkBpw79+/q7LnJ +90CJQ9mMVLcjX1tjYXqebEn8KusFEgTigDyfBEijYmHgAD5XcLPABK4k908y/kMq23L Ao2O+jp9Wxz2xbqF2qrAnUMP/FZY2hGT811az1t0hggbQEaMREUmn0nmYD2+mreRR5Ed tR+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=ikhPjIVe; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=y2iPbKYe; 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 q9si7291059ejb.449.2021.10.15.03.13.36; Fri, 15 Oct 2021 03:14:00 -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=ikhPjIVe; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=y2iPbKYe; 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 S234652AbhJOBSY (ORCPT + 99 others); Thu, 14 Oct 2021 21:18:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233454AbhJOBSN (ORCPT ); Thu, 14 Oct 2021 21:18:13 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5B6DC061753; Thu, 14 Oct 2021 18:16:07 -0700 (PDT) Message-ID: <20211015011538.839822981@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1634260566; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=p/XFR0aVQMKKxVzOYz6MXsECkHIm28ch1vdabn5duRg=; b=ikhPjIVeyC3IOe8gjkJYB46r2rQjjJiCfZdPDICGuMX5n7Z5IBAUvkDO82g/ZWV4oVSfB0 r2AwyyiwCflbr/JpV8KGpB+7Ih80Yxzf0UbUk3yftB38zcKlPVwxFoY329+xo5GPJicZH2 orJ5D5Uy9tHbvLesk3LhaLrRMc8c/2sUC8Q6lCfPHjYNDUdMKZYlrjs4AslcH6Y5Hpgkx/ FFdX1VdSQy09pD8O4vYLc0WnU29x5NnIx/y+Y/pjVXxhD4Wxmn+u9YV+sMsfNtYmtz+q7f rLnRJVC52i1RGzzha31CIISxyOIKdXJNfofWerH6fKU54XIBfUGj0xBLx8HLPA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1634260566; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=p/XFR0aVQMKKxVzOYz6MXsECkHIm28ch1vdabn5duRg=; b=y2iPbKYe3fOsGW0Vw6X8btssEiOozCvl7wx3IRjYAvUR7iiwFtTrXysaEi2ks6PxY/j8+e ilZ8DqpeRi8OdJCg== From: Thomas Gleixner To: LKML Cc: x86@kernel.org, "Chang S. Bae" , Dave Hansen , Arjan van de Ven , kvm@vger.kernel.org, Paolo Bonzini , "Liu, Jing2" , Sean Christopherson , Xiaoyao Li Subject: [patch V2 08/30] x86/fpu: Do not inherit FPU context for kernel and IO worker threads References: <20211015011411.304289784@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Date: Fri, 15 Oct 2021 03:16:06 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 minimaly required state for those tasks. Signed-off-by: Thomas Gleixner --- 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 191269edac97..9a6b195a8a00 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. */