Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp5479568imm; Wed, 12 Sep 2018 06:39:18 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZTwFu+m1VPIUs1JjLbvWTkrM9qVvzBOnKWDMOaCgFRB0eCAFttbHLnhijzu7u3vLQZDbYy X-Received: by 2002:a63:d518:: with SMTP id c24-v6mr2263805pgg.357.1536759557958; Wed, 12 Sep 2018 06:39:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536759557; cv=none; d=google.com; s=arc-20160816; b=Q0ZwTN7/4Moo2q4H2MMfP3If/pMOzKJlsaQrB37A9TZJplU16fiX9SbF/gZ7kFyfld aoGfKIuj00veidnUzsWFECwvbmaaDRgcA8gQiBPk2US4KAlps2wuusXJnp68sTARgA6k ty8CGEqle053vT9SLYGrSVr+g+p5uqjtf2gnZXI8opcyhihtFvN8rOJlOEIZPRElRXI3 Ezwt5+uVpC77b9h67r4DaYyBMBSaR8w65K+06RW+4PYp32onUegs4nm0jaY3Dbmwzlj4 55geiBCxOwhYWrjNOOYR0AWU8s6voToZI9nTLnWju7mj/YiYuX1xFZDv2BvtEUsqOg4e L75w== 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=NseQapFN+WZAizXGp+epnsMyL6UA7TmfcOc3T5U6pfQ=; b=bK7V8O110uKSj+tCVtbryi6I+yn1lxwRdhsKjAm5rfBQ/Bm4uml6a7Vna3hNOXI4KL dQYJ9i8wz3AHwUedGkh96se20T6O2RxStcSMpVDh7xYxNJzvTbZ52UBJtCBQT1n4t0kH t9szJ+ycMlqKQodZg9cg0wqNU6vwV2VUqeSkcU7UHoTsBAARYoRsmFgRo9WGJKBRTD0m mLto4D/bXywVaPzb2rHhQ9A56h4UFqalXKzpaAQWd2FbgW0lX4dQVyp0PHtC1EPf1hsd plce1LXzuXU2CP3MeWtFgUlD3mFn5fDIbxRonE+nM7BFtU+iAMm/HWq9LvjI4che4Scu OUmw== 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 b11-v6si1096461pgt.44.2018.09.12.06.38.58; Wed, 12 Sep 2018 06:39:17 -0700 (PDT) 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 S1728215AbeILSlY (ORCPT + 99 others); Wed, 12 Sep 2018 14:41:24 -0400 Received: from Galois.linutronix.de ([146.0.238.70]:43891 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726359AbeILSkc (ORCPT ); Wed, 12 Sep 2018 14:40:32 -0400 Received: from localhost ([127.0.0.1] helo=bazinga.breakpoint.cc) by Galois.linutronix.de with esmtp (Exim 4.80) (envelope-from ) id 1g05JC-0001Ap-R0; Wed, 12 Sep 2018 15:35:55 +0200 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 , Sebastian Andrzej Siewior Subject: [RFC PATCH 04/10] x86/fpu: eager switch PKRU state Date: Wed, 12 Sep 2018 15:33:47 +0200 Message-Id: <20180912133353.20595-5-bigeasy@linutronix.de> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20180912133353.20595-1-bigeasy@linutronix.de> References: <20180912133353.20595-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 accesses to userspace memory while running in kernel mode also need to abide by the memory permissions specified in the protection keys. The pkru info is put in its own cache line in the fpu struct because that cache line is accessed anyway at context switch time, and the location of the pkru info in the xsave buffer changes depending on what other FPU registers are in use if the CPU uses compressed xsave state (on by default). The initial state of pkru is zeroed out automatically by fpstate_init. Signed-off-by: Rik van Riel [bigeasy: load PKRU state only if we also load FPU content] Signed-off-by: Sebastian Andrzej Siewior --- arch/x86/include/asm/fpu/internal.h | 11 +++++++++-- arch/x86/include/asm/fpu/types.h | 10 ++++++++++ arch/x86/include/asm/pgtable.h | 6 +----- arch/x86/mm/pkeys.c | 14 ++++++++++++++ 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/fpu/internal.h b/arch/x86/include/asm/fpu/internal.h index 16c4077ffc945..57bd1576e033d 100644 --- a/arch/x86/include/asm/fpu/internal.h +++ b/arch/x86/include/asm/fpu/internal.h @@ -573,8 +573,15 @@ static inline void switch_fpu_finish(struct fpu *new_fpu, int cpu) bool preload = static_cpu_has(X86_FEATURE_FPU) && new_fpu->initialized; - if (preload) - __fpregs_load_activate(new_fpu, cpu); + if (!preload) + return; + + __fpregs_load_activate(new_fpu, cpu); + /* Protection keys need to be in place right at context switch time. */ + if (boot_cpu_has(X86_FEATURE_OSPKE)) { + if (new_fpu->pkru != __read_pkru()) + __write_pkru(new_fpu->pkru); + } } /* diff --git a/arch/x86/include/asm/fpu/types.h b/arch/x86/include/asm/fpu/types.h index 202c53918ecfa..6fa58d37938d2 100644 --- a/arch/x86/include/asm/fpu/types.h +++ b/arch/x86/include/asm/fpu/types.h @@ -293,6 +293,16 @@ struct fpu { */ unsigned int last_cpu; + /* + * Protection key bits. These also live inside fpu.state.xsave, + * but the location varies if the CPU uses the compressed format + * for XSAVE(OPT). + * + * The protection key needs to be switched out immediately at context + * switch time, so it is in place for things like copy_to_user. + */ + unsigned int pkru; + /* * @initialized: * diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 690c0307afed0..cc36f91011ad7 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -132,11 +132,7 @@ static inline u32 read_pkru(void) return 0; } -static inline void write_pkru(u32 pkru) -{ - if (boot_cpu_has(X86_FEATURE_OSPKE)) - __write_pkru(pkru); -} +extern void write_pkru(u32 pkru); static inline int pte_young(pte_t pte) { diff --git a/arch/x86/mm/pkeys.c b/arch/x86/mm/pkeys.c index 6e98e0a7c9231..c7a7b6bd64009 100644 --- a/arch/x86/mm/pkeys.c +++ b/arch/x86/mm/pkeys.c @@ -18,6 +18,20 @@ #include /* boot_cpu_has, ... */ #include /* vma_pkey() */ +#include + +void write_pkru(u32 pkru) +{ + if (!boot_cpu_has(X86_FEATURE_OSPKE)) + return; + + current->thread.fpu.pkru = pkru; + + __fpregs_changes_begin(); + __fpregs_load_activate(¤t->thread.fpu, smp_processor_id()); + __write_pkru(pkru); + __fpregs_changes_end(); +} int __execute_only_pkey(struct mm_struct *mm) { -- 2.19.0