Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1738290imu; Wed, 28 Nov 2018 14:22:34 -0800 (PST) X-Google-Smtp-Source: AFSGD/V+LSydbxLCj25p0XkIKiy15I25cvvxCmv0w/XHJkhspydg79D4AR0CYPS1B3dMSz9jDCJt X-Received: by 2002:a17:902:4c08:: with SMTP id a8mr39236413ple.74.1543443754939; Wed, 28 Nov 2018 14:22:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543443754; cv=none; d=google.com; s=arc-20160816; b=H0DnYeV12UHWQLPPDBGgD7YMaWZt3+hdbTD2YKBn1Z2OleEFe7aiRxSDRYd3/YvpKl dffoaO2bvAEaDh1zArT0pEUBCQRBbDTyC/0XTmWYtd8asUarhrCrcPTxcMwgsdjIERN2 SSOeLJekERT4ZXX7rPW2/W/d0CTHNMYru7YDSyHju+hOa/ZXvhcFYi8DHtxmHOX6f+5v EInnvUuCXxM6LoUjBIPRPVYq5MUpohY5IVwOMKFbyM5Tl8hL9X/BnqBVjQ94BotvBpeT /S4rWgd4KuZ5dXWFjcXKCCaPDX5Avc+4jqHqGNwl7OZokdUWxJI42sohPhtgzHIqSyZP Zn5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=dn9A+rd0GukMUI9j3aydfmcsehuIwUVWxrTFe5tRNww=; b=NA/+fru6TWO2ziDhSHoLtKx62EOmva7U9vpyvW4YOBoyRExxqG/qCXYSMEC0TkJvFc RgfaiJM8euNv44CnXsAEIrFhQ5R2nWt5NbFMdWjleyM7sefRGpELmZh/INOHiKeKlgB9 I/ziNG/4zKVMC6ErM72Q1+KW/GzSRDLFWDqvAZhJd4Xw/T4r11aBDxdhDICmXEPo8+1Q 3WYz84grODQK05wZU5L7D25bF1aqnmxqUMuV7qYLjVLL7iNuPC9UFX5r0SCBwb3rukYM E6ng2Uqa16l5hKuzHg/tQ3CalspL7BRcKtXkw5E3qPrCm9eFfEPB3y+8VpNT0WQy9aeK taRQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i129si9182477pfb.32.2018.11.28.14.22.20; Wed, 28 Nov 2018 14:22:34 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727197AbeK2JYe (ORCPT + 99 others); Thu, 29 Nov 2018 04:24:34 -0500 Received: from Galois.linutronix.de ([146.0.238.70]:33193 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727137AbeK2JYd (ORCPT ); Thu, 29 Nov 2018 04:24:33 -0500 Received: from localhost ([127.0.0.1] helo=bazinga.breakpoint.cc) by Galois.linutronix.de with esmtp (Exim 4.80) (envelope-from ) id 1gS8Cs-0001GX-OQ; Wed, 28 Nov 2018 23:21:19 +0100 From: Sebastian Andrzej Siewior To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, Andy Lutomirski , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , kvm@vger.kernel.org, "Jason A. Donenfeld" , Rik van Riel , Dave Hansen , Sebastian Andrzej Siewior Subject: [PATCH 21/29] x86/fpu: Eager switch PKRU state Date: Wed, 28 Nov 2018 23:20:27 +0100 Message-Id: <20181128222035.2996-22-bigeasy@linutronix.de> X-Mailer: git-send-email 2.20.0.rc1 In-Reply-To: <20181128222035.2996-1-bigeasy@linutronix.de> References: <20181128222035.2996-1-bigeasy@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Rik van Riel While most of a task's FPU state is only needed in user space, the protection keys need to be in place immediately after a context switch. The reason is that any access to userspace memory while running in kernel mode also need to abide by the memory permissions specified in the protection keys. The "eager switch" is a preparation for loading the FPU state on return to userland. Instead of decoupling PKRU state from xstate I update PKRU within xstate on write operations by the kernel. The read/write_pkru() is moved to another header file so it can easily accessed from pgtable.h and fpu/internal.h. For user tasks we should always get the PKRU from the xsave area and it should not change anything because the PKRU value was loaded as part of FPU restore. For kernel kernel threads we now will have the default "allow everything" written. Before this commit the kernel thread would end up with a random value which it inherited from the previous user task. Signed-off-by: Rik van Riel [bigeasy: save pkru to xstate, no cache, don't use __raw_xsave_addr()] Signed-off-by: Sebastian Andrzej Siewior --- arch/x86/include/asm/fpu/internal.h | 20 ++++++++++++++++++-- arch/x86/include/asm/fpu/xstate.h | 1 + 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/fpu/internal.h b/arch/x86/include/asm/fpu/internal.h index 21a8b4f5a5efd..788b307e66233 100644 --- a/arch/x86/include/asm/fpu/internal.h +++ b/arch/x86/include/asm/fpu/internal.h @@ -557,8 +557,24 @@ switch_fpu_prepare(struct fpu *old_fpu, int cpu) */ static inline void switch_fpu_finish(struct fpu *new_fpu, int cpu) { - if (static_cpu_has(X86_FEATURE_FPU)) - __fpregs_load_activate(new_fpu, cpu); + struct pkru_state *pk; + u32 pkru_val = 0; + + if (!static_cpu_has(X86_FEATURE_FPU)) + return; + + __fpregs_load_activate(new_fpu, cpu); + + if (!cpu_feature_enabled(X86_FEATURE_OSPKE)) + return; + + if (current->mm) { + pk = get_xsave_addr(&new_fpu->state.xsave, XFEATURE_PKRU); + WARN_ON_ONCE(!pk); + if (pk) + pkru_val = pk->pkru; + } + __write_pkru(pkru_val); } /* diff --git a/arch/x86/include/asm/fpu/xstate.h b/arch/x86/include/asm/fpu/xstate.h index fbe41f808e5d8..4e18a837223ff 100644 --- a/arch/x86/include/asm/fpu/xstate.h +++ b/arch/x86/include/asm/fpu/xstate.h @@ -5,6 +5,7 @@ #include #include #include +#include /* Bit 63 of XCR0 is reserved for future expansion */ #define XFEATURE_MASK_EXTEND (~(XFEATURE_MASK_FPSSE | (1ULL << 63))) -- 2.20.0.rc1