Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp1069594imm; Fri, 12 Oct 2018 11:12:05 -0700 (PDT) X-Google-Smtp-Source: ACcGV605ETzu4EXwRAtiel8mPIZ5dPaXjcwxKR/frP9yNciIngBOG1Ec+CHfvu20R1TcEGZVSgaA X-Received: by 2002:a63:d14a:: with SMTP id c10-v6mr6515705pgj.384.1539367924980; Fri, 12 Oct 2018 11:12:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539367924; cv=none; d=google.com; s=arc-20160816; b=t+BO3U9yKEAUv7aN3Mu+aLWcsckCdYvBi/MOAV4xWqjxDTmSVn+mOZ0xqp51Z/M4my gs2R0Uu4M/xCN43qPOV8VFP+O8Pifd5uCheev2zaJTDnZa+irpDY3CUeiDQI4vn9BOsU VGsoeJrBMbvFvwgt59pTvB1FqR1KjWdxxmTMXPwEpwOcO4lfkGcmS8d4z+dZG8kqZpqS IaIrlQC5YDuEdNr1SXf51Lz2D/8mA/mJ/gc77+eu4qCGwZ5lgFp+Jo3q7oNu6KNwZumv EKOc1Wdz+YOMYKUbOldkyJthtOHCZCPKKXNzQuVNlPa9kuOX9rlgwcaxbnhumgAjbjrp aiGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=FGC6cqqhHDzwDVWKyBkbuITIB6j0TfXToDRgcCzuLiw=; b=Wyw3XZij6u72OtPXcuce1hFvUIL+Zz/ToJsbXDpmCT0ZHD/KWwyfvc3LNEVD3WtJt2 zS5ZM9rxFdhCcve/n03UL1M0m48ZxWIfz3U0wKwnRGzYWQga2y9gCjH4vRrRYKZopXC9 os7+xhXIdF0QSmxCqnyxdH5QEs0qGDEBS4Ib+/5kzgVLPBnEV0JovEq1VGXDUD2wFxqD cbp0eb8RsxOEKTppW33eLZajam4uJOtXhmZlE157tql5WTloANiX45BYPxuX7M9cxDYx KwvHwtEVkRfY0Kvwt/WdXSZ1P+raGAmsQqNmZwnladUMcAkyv7b2kl+gr5yfj9UFDqkK dk9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Y1fRmNY3; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h8-v6si1778580pls.94.2018.10.12.11.11.49; Fri, 12 Oct 2018 11:12:04 -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; dkim=pass header.i=@kernel.org header.s=default header.b=Y1fRmNY3; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726715AbeJMBni (ORCPT + 99 others); Fri, 12 Oct 2018 21:43:38 -0400 Received: from mail.kernel.org ([198.145.29.99]:46630 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725929AbeJMBni (ORCPT ); Fri, 12 Oct 2018 21:43:38 -0400 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5840621524 for ; Fri, 12 Oct 2018 18:09:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1539367794; bh=bqY6yoggV2wlerq78M5PLQiDOBJeTtjzULeSXpd5ABU=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=Y1fRmNY3b6BeIQdDriLMx5nBtA4RKUERQQqEVwavaY4WTqKLoe3Ra8smCbyC8YIwZ BrG9F8r2BFJ4jsEwfEA6MTzwWTP/RfIaiFo3Tu8UhcrcHo/+DLMWwM3mY/yU6+BGIe Okk7b5Je51YN+lCVO3F11XFFMXbIDE6DPHpCsUCU= Received: by mail-wm1-f46.google.com with SMTP id 185-v6so13809185wmt.2 for ; Fri, 12 Oct 2018 11:09:54 -0700 (PDT) X-Gm-Message-State: ABuFfoi8ogC76NZ+6DRUlE3fmYTQy95ZIEjtPbQ4Kbw15SOfd9baTAel QNlqKOPGUrkkM23AOwROkh4S7xlWCxBu4nLs8PLdpQ== X-Received: by 2002:a1c:d4b:: with SMTP id 72-v6mr6439698wmn.102.1539367792688; Fri, 12 Oct 2018 11:09:52 -0700 (PDT) MIME-Version: 1.0 References: <20181004140547.13014-1-bigeasy@linutronix.de> <20181004140547.13014-5-bigeasy@linutronix.de> <76caafd5-c85d-61bb-62ec-8056cd6d95ac@linux.intel.com> In-Reply-To: <76caafd5-c85d-61bb-62ec-8056cd6d95ac@linux.intel.com> From: Andy Lutomirski Date: Fri, 12 Oct 2018 11:09:41 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 04/11] x86/fpu: eager switch PKRU state To: Dave Hansen Cc: Sebastian Andrzej Siewior , LKML , X86 ML , Andrew Lutomirski , Paolo Bonzini , Radim Krcmar , kvm list , "Jason A. Donenfeld" , Rik van Riel Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Oct 12, 2018 at 10:51 AM Dave Hansen wrote: > > On 10/04/2018 07:05 AM, Sebastian Andrzej Siewior wrote: > > 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 "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. > > > > 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 | 20 +++++++++++++++---- > > arch/x86/include/asm/fpu/xstate.h | 2 ++ > > arch/x86/include/asm/pgtable.h | 6 +----- > > arch/x86/include/asm/pkeys.h | 2 +- > > arch/x86/kernel/fpu/core.c | 2 +- > > arch/x86/mm/pkeys.c | 31 ++++++++++++++++++++++------- > > include/linux/pkeys.h | 2 +- > > 7 files changed, 46 insertions(+), 19 deletions(-) > > > > diff --git a/arch/x86/include/asm/fpu/internal.h b/arch/x86/include/asm/fpu/internal.h > > index 16c4077ffc945..956d967ca824a 100644 > > --- a/arch/x86/include/asm/fpu/internal.h > > +++ b/arch/x86/include/asm/fpu/internal.h > > @@ -570,11 +570,23 @@ switch_fpu_prepare(struct fpu *old_fpu, int cpu) > > */ > > static inline void switch_fpu_finish(struct fpu *new_fpu, int cpu) > > { > > - bool preload = static_cpu_has(X86_FEATURE_FPU) && > > - new_fpu->initialized; > > + bool load_fpu; > > > > - if (preload) > > - __fpregs_load_activate(new_fpu, cpu); > > + load_fpu = static_cpu_has(X86_FEATURE_FPU) && new_fpu->initialized; > > + if (!load_fpu) > > + return; > > Needs comments, please. Especially around what an uninitialized new_fpu > means. See my other comment about getting rid of ->initialized *first*. > > > + __fpregs_load_activate(new_fpu, cpu); > > + > > +#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS > > + if (static_cpu_has(X86_FEATURE_OSPKE)) { > > FWIW, you should be able to use cpu_feature_enabled() instead of an > explicit #ifdef here. > > > + struct pkru_state *pk; > > + > > + pk = __raw_xsave_addr(&new_fpu->state.xsave, XFEATURE_PKRU); > > + if (pk->pkru != __read_pkru()) > > + __write_pkru(pk->pkru); > > + } > > +#endif > > } > > Comments here as well, please. > > I think the goal is to keep the PKRU state in the 'init state' when > possible and also to save the cost of WRPKRU. But, it would be really > nice to be explicit. I suspect that this makes basically no difference. PKRU is almost never in the init state on Linux. Also, it's a single word -- I doubt that the init state optimization is worth much. But maybe WRPKRU is more expensive than RDPKRU and a branch?