Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2905128pxj; Mon, 14 Jun 2021 09:44:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJztwy6TAWtlR07CrVhcjUvsipAIR3122f7lyTFI6RqCQHIJiCdjeyEYSZsLR3jFfF6ytGvk X-Received: by 2002:a17:906:70d4:: with SMTP id g20mr15778882ejk.327.1623689087020; Mon, 14 Jun 2021 09:44:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623689087; cv=none; d=google.com; s=arc-20160816; b=flRcoad0xenf4mBUxs0b7T1kveizkwo6XGTbiUYBXJKEQ0tv+wPJnyDb25LpYkefe4 ooIxQI1IGa6WnItu4jUBARR138H+/42BVgv3r9yeRvEO14D8pDvof110HR8iG69+8Q1X MmcBvY/Ln65EjFZMgczLEllFmRGgDGlzNMM3eOp8+xpilK+G/QBbo9wsYp9NRI5jfG5L +SVO47I+LVm4ymSbD1J8L5PKKzBJ9VydibiCrSpVyBhNaNEWyIWmde/ObwSkqjTcotpw RiOHqaCrQ+8BHX28spVFTilckT4+43sS6XuFyN8X3AbmgoMbH0RpkDUX5aiPcAYslNho IuKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:subject:cc:to:from:date:dkim-signature:dkim-signature :message-id; bh=1A+rdrzCr24SbCzAOKAmEfMSvxh54YcsNwFR4gnZ6WY=; b=jkkTEpXx08pHd1VrOU0DUSnGpCUVjE2mvEo/3syrurwaxr4BHr4nO5LVJmgym34Iap 6iACX+QPd21VC+r+dsAbnxlJdHC/Jjtpth1O0vO8xwgOE8QzXIpAoYy2xU0wQoCuofua Cd/2KsI5i+wh50Nd+4Jeodxt55V1bSqM5qOAhVxKFmVM48zMD1VssP6WC+MTZdvK06fg mijse2CHzu85TV4o16U338VEOleqT/XMnV8zf+EOBvZ1J9NNzFZzmZNXK99aUpqlZQVm 6aKvXgSpfGvT0BoYtFe9+RhCb3cpA1aBpQ/X+YeMlC2tH5Gcylz3rewiI8PIiv6i9B9a WZLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=NjlZG9hz; dkim=neutral (no key) header.i=@linutronix.de; 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 r17si1186747edd.574.2021.06.14.09.44.24; Mon, 14 Jun 2021 09:44:47 -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=NjlZG9hz; dkim=neutral (no key) header.i=@linutronix.de; 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 S235358AbhFNQmd (ORCPT + 99 others); Mon, 14 Jun 2021 12:42:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235041AbhFNQkp (ORCPT ); Mon, 14 Jun 2021 12:40:45 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08643C0613A3 for ; Mon, 14 Jun 2021 09:38:42 -0700 (PDT) Message-Id: <20210614155358.277290739@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1623688720; 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: content-transfer-encoding:content-transfer-encoding: references:references; bh=1A+rdrzCr24SbCzAOKAmEfMSvxh54YcsNwFR4gnZ6WY=; b=NjlZG9hzPqVkJZl7/ymmdC6ggJbFc2pau0AhNQUi+jbYHT2IUyd5mXxQ5SIsLuL4kD6pt1 ttIBeiYP0yXX13JhGZAqY7AFTYjZrmm7YvU3fEPdQCjOmH/v41NCYAQ90CzLREIuJoB1xp nV+0UCpXDm+UOxQvroo3LoaeN6VLIaiT4/pRPTzm1kqS9inwx4PxpIDNyIP5RZO+TY8mMC Pzr6cA74tnBMDp+WFG/iCpTMaf9N8LsxZJjjFbJTvhsSOB7kAxoHduFYDlX4rUufKawURd Z5h/jmstqgbXM0Z4xakjvnFL8EJB5GCkEpTgj4VzVx1TsFqu5LNc3ETNLb9oYw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1623688720; 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: content-transfer-encoding:content-transfer-encoding: references:references; bh=1A+rdrzCr24SbCzAOKAmEfMSvxh54YcsNwFR4gnZ6WY=; b=2xRgB8unk4KzbbtepNwf3MBPFYGYylAIHT0PT0ZaeoJL2U4z0M3Qeefu+KsrhfoHIbyrjW zzDZcpOYHIkvSIAA== Date: Mon, 14 Jun 2021 17:44:53 +0200 From: Thomas Gleixner To: LKML Cc: Andy Lutomirski , Dave Hansen , Fenghua Yu , Tony Luck , Yu-cheng Yu , Sebastian Andrzej Siewior , Borislav Petkov , Peter Zijlstra , Kan Liang Subject: [patch V2 45/52] x86/fpu: Dont restore PKRU in fpregs_restore_userspace() References: <20210614154408.673478623@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-transfer-encoding: 8-bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org switch_to(), flush_thread() write the task's PKRU value eagerly so the PKRU value of current is always valid in the hardware. That means there is no point in restoring PKRU on exit to user or when reactivating the task's FPU registers in the signal frame setup path. This allows to remove all the xstate buffer updates with PKRU values once the PKRU state is stored in thread struct while a task is scheduled out. Signed-off-by: Thomas Gleixner --- arch/x86/include/asm/fpu/internal.h | 12 +++++++++++- arch/x86/include/asm/fpu/xstate.h | 19 +++++++++++++++++++ arch/x86/kernel/fpu/core.c | 2 +- 3 files changed, 31 insertions(+), 2 deletions(-) --- a/arch/x86/include/asm/fpu/internal.h +++ b/arch/x86/include/asm/fpu/internal.h @@ -455,7 +455,17 @@ static inline void fpregs_restore_userre return; if (!fpregs_state_valid(fpu, cpu)) { - restore_fpregs_from_fpstate(&fpu->state); + /* + * This restores _all_ xstate which has not been + * established yet. + * + * If PKRU is enabled, then the PKRU value is already + * correct because it was either set in switch_to() or in + * flush_thread(). So it is excluded because it might be + * not up to date in current->thread.fpu.xsave state. + */ + __restore_fpregs_from_fpstate(&fpu->state, + xfeatures_mask_restore_user()); fpregs_activate(fpu); fpu->last_cpu = cpu; } --- a/arch/x86/include/asm/fpu/xstate.h +++ b/arch/x86/include/asm/fpu/xstate.h @@ -35,6 +35,14 @@ XFEATURE_MASK_BNDREGS | \ XFEATURE_MASK_BNDCSR) +/* + * Features which are restored when returning to user space. + * PKRU is not restored on return to user space because PKRU + * is switched eagerly in switch_to() and flush_thread() + */ +#define XFEATURE_MASK_USER_RESTORE \ + (XFEATURE_MASK_USER_SUPPORTED & ~XFEATURE_MASK_PKRU) + /* All currently supported supervisor features */ #define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID) @@ -92,6 +100,17 @@ static inline u64 xfeatures_mask_uabi(vo return xfeatures_mask_all & XFEATURE_MASK_USER_SUPPORTED; } +/* + * The xfeatures which are restored by the kernel when returning to user + * mode. This is not necessarily the same as xfeatures_mask_uabi() as the + * kernel does not manage all XCR0 enabled features via xsave/xrstor as + * some of them have to be switched eagerly on context switch and exec(). + */ +static inline u64 xfeatures_mask_restore_user(void) +{ + return xfeatures_mask_all & XFEATURE_MASK_USER_RESTORE; +} + static inline u64 xfeatures_mask_independent(void) { if (!boot_cpu_has(X86_FEATURE_ARCH_LBR)) --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -470,7 +470,7 @@ void fpu__clear_user_states(struct fpu * } /* Reset user states in registers. */ - load_fpregs_from_init_fpstate(xfeatures_mask_uabi()); + load_fpregs_from_init_fpstate(xfeatures_mask_restore_user()); /* * Now all FPU registers have their desired values. Inform the FPU