Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3536648imu; Wed, 7 Nov 2018 11:51:54 -0800 (PST) X-Google-Smtp-Source: AJdET5f7QwsegWs5pSWULP23sXpo/vuqbrmiYt6RY8flnO4lEB6mkS4axFUcH7/+OBJke4G6eS8t X-Received: by 2002:a17:902:3084:: with SMTP id v4-v6mr1561646plb.326.1541620314254; Wed, 07 Nov 2018 11:51:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541620314; cv=none; d=google.com; s=arc-20160816; b=cgPNvuJgieJHVDYkbS2ygc3WuZPuP5ID4RJbGd6lgcdmR/FT1rDgz0IzSgJR6//W+K lgjrv+1fv2eULL5phHc2TyMMXsD/3VfW0O9RM4no8d3+Rt8+Ju6/6DbZUIrzj5qxccdB o3hGmU5hB4EsQKSlai637JToUEjTeUoEMAIVAgbqIlr9JhXPcsUK5U085H/7CZ0nCxv/ 1Z8h0x2xI81uAH6E54HUW/wfsX/xnF+5vvSOOxkvsSD7PplMyWNc2R+YpYsPOnJVLVy3 +qZRJvyxN75yOGv+i07fHkMZeQvZx42dzV7GbJMK9nWq7TMH2gQL/ATF6QW5nzzPzNV5 pJSQ== 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=AZSdsGQ7D1EbRwDKEoVt3OzY0h1qV+3ea0GemG8oVfc=; b=pEaQ7AyAFvwldol9S9zacUPqurBh5NH6wQcq3a5WWu0KcJSQZlnCYSfBSoXMuNtWGi n8SXCLb33vCK7RFL4GKeVCsEvXp4YkEuMW/zaiUtyHstza0QU3fTqKNk2NZ/fl+THFT6 XZwAXYijjw7ohCx1Gh/xziNCBFVLYwGuLsO0UyIvjNxYwslMQTtPEkwKiU936dQTQ0cE gqs3nluqotEVjBFbgjCK33m+/L+r8RW9TkRwX0a/s83T9AflyTCu0ovpklQi/eyFRRCW mMnM9+zWPxmT4vJFVhKUZG7QyR+MDFuKpDYMEqniiZZkOq+yakfiei7HvcSOw0uJLA+3 rTCA== 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 s9si1451408pgv.397.2018.11.07.11.51.39; Wed, 07 Nov 2018 11:51:54 -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 S1727807AbeKHFVS (ORCPT + 99 others); Thu, 8 Nov 2018 00:21:18 -0500 Received: from Galois.linutronix.de ([146.0.238.70]:41948 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727579AbeKHFVS (ORCPT ); Thu, 8 Nov 2018 00:21:18 -0500 Received: from localhost ([127.0.0.1] helo=bazinga.breakpoint.cc) by Galois.linutronix.de with esmtp (Exim 4.80) (envelope-from ) id 1gKTpL-00070Q-QA; Wed, 07 Nov 2018 20:49:24 +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 17/23] x86/fpu: Eager switch PKRU state Date: Wed, 7 Nov 2018 20:48:52 +0100 Message-Id: <20181107194858.9380-18-bigeasy@linutronix.de> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181107194858.9380-1-bigeasy@linutronix.de> References: <20181107194858.9380-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. Signed-off-by: Rik van Riel [bigeasy: save pkru to xstate, no cache] Signed-off-by: Sebastian Andrzej Siewior --- arch/x86/include/asm/fpu/internal.h | 13 +++++++++++-- arch/x86/include/asm/fpu/xstate.h | 2 ++ arch/x86/kernel/fpu/xstate.c | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/fpu/internal.h b/arch/x86/include/asm/fpu/internal.h index 260cd4f4ba2bb..ed65e0642a1e1 100644 --- a/arch/x86/include/asm/fpu/internal.h +++ b/arch/x86/include/asm/fpu/internal.h @@ -561,8 +561,17 @@ 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); + if (!static_cpu_has(X86_FEATURE_FPU)) + return; + + __fpregs_load_activate(new_fpu, cpu); + + if (cpu_feature_enabled(X86_FEATURE_OSPKE)) { + struct pkru_state *pk; + + pk = __raw_xsave_addr(&new_fpu->state.xsave, XFEATURE_PKRU); + __write_pkru(pk->pkru); + } } /* diff --git a/arch/x86/include/asm/fpu/xstate.h b/arch/x86/include/asm/fpu/xstate.h index fbe41f808e5d8..dd138f5eb5c66 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))) @@ -47,6 +48,7 @@ extern void __init update_regset_xstate_info(unsigned int size, void fpu__xstate_clear_all_cpu_caps(void); void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr); +void *__raw_xsave_addr(struct xregs_state *xsave, int feature_nr); const void *get_xsave_field_ptr(int xfeature_nr); int using_compacted_format(void); int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int offset, unsigned int size); diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index 375226055a413..5b33985d9f475 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -811,7 +811,7 @@ void fpu__resume_cpu(void) * * Note: does not work for compacted buffers. */ -static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr) +void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr) { if (!xfeature_enabled(xfeature_nr)) { WARN_ON_FPU(1); -- 2.19.1