From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A98BCC433F5 for ; Tue, 9 Nov 2021 14:58:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 930B760F6E for ; Tue, 9 Nov 2021 14:58:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234869AbhKIPBN (ORCPT ); Tue, 9 Nov 2021 10:01:13 -0500 Received: from mail.kernel.org ([198.145.29.99]:60986 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234250AbhKIPA4 (ORCPT ); Tue, 9 Nov 2021 10:00:56 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 5561F61211; Tue, 9 Nov 2021 14:58:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1636469890; bh=+9UT4JjEfvmDWmb+FT+mdfKzE0MepG7Qp1SoI0567aU=; h=In-Reply-To:References:Date:From:To:Cc:Subject:From; b=tES8zGCsmzJ78bp2AWpu3CbxhGHSYr+TrJEUdu/i/tstneuZIWFWNEHn0zduEzbSr cSj2Xc5xlNuC9prdBemJI63dbh+TdKLce8FGfubFxkx3cTR5yLDu5iQjTHYurbqZ7r Nnvr3gT5Wonwi8vC8gjoeI13e67rydusl0dKmTZvKbM8wtiMcwLXPUDHNwL3Baugfk AZExFuFAppcrPuRVSb8D9e6f4S7p5XnQXeZUQFQjiB/5at3+2iRyePPL1ktO8TQCmO bKs0St3v3cDm3lQYkcIRIwGvA2GyLM8Ftiji1+gXIkHGXvMIptKMW3Fb/vKxBz66x4 VSpSRBvQuNGCA== Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailauth.nyi.internal (Postfix) with ESMTP id 3D00D27C0054; Tue, 9 Nov 2021 09:58:08 -0500 (EST) Received: from imap48 ([10.202.2.98]) by compute4.internal (MEProxy); Tue, 09 Nov 2021 09:58:08 -0500 X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvuddrudeggdeilecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefofgggkfgjfhffhffvufgtsehttdertderredtnecuhfhrohhmpedftehnugih ucfnuhhtohhmihhrshhkihdfuceolhhuthhosehkvghrnhgvlhdrohhrgheqnecuggftrf grthhtvghrnheptdfhheettddvtedvtedugfeuuefhtddugedvleevleefvdetleffgfef vdekgeefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh eprghnugihodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdduudeiudekheei fedvqddvieefudeiiedtkedqlhhuthhopeepkhgvrhhnvghlrdhorhhgsehlihhnuhigrd hluhhtohdruhhs X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id C523321E0429; Tue, 9 Nov 2021 09:58:07 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.5.0-alpha0-1371-g2296cc3491-fm-20211109.003-g2296cc34 Mime-Version: 1.0 Message-Id: <649f4de7-3c91-4974-9af7-d981a2bf6224@www.fastmail.com> In-Reply-To: References: <85925a39-37c3-a79a-a084-51f2f291ca9c@intel.com> <472b8dbf-2c55-98c9-39ad-2db32a649a20@intel.com> Date: Tue, 09 Nov 2021 06:57:47 -0800 From: "Andy Lutomirski" To: "Brian Geffon" , "Dave Hansen" Cc: "Thomas Gleixner" , "Guenter Roeck" , "Borislav Petkov" , stable@vger.kernel.org, "the arch/x86 maintainers" , "Linux Kernel Mailing List" Subject: Re: XSAVE / RDPKRU on Intel 11th Gen Core CPUs Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Nov 9, 2021, at 5:43 AM, Brian Geffon wrote: > Hi Dave, > > On Tue, Nov 9, 2021 at 1:49 AM Dave Hansen wrote: >> Well, gosh, it's making it back to the software init value. If you do: >> >> echo 0x15555554 > /sys/kernel/debug/x86/init_pkru >> >> do you end up with 0x15555554 as the value? > > What's interesting is that writing to init_pkru fails with -EINVAL for me, > and I've traced it down to get_xsave_addr() returning NULL on the following > check: > > /* > * This assumes the last 'xsave*' instruction to > * have requested that 'xfeature_nr' be saved. > * If it did not, we might be seeing and old value > * of the field in the buffer. > * > * This can happen because the last 'xsave' did not > * request that this feature be saved (unlikely) > * or because the "init optimization" caused it > * to not be saved. > */ > if (!(xsave->header.xfeatures & BIT_ULL(xfeature_nr))) > return NULL; Here's an excerpt from an old email that I, perhaps unwisely, sent to Dave but not to a public list: static inline void write_pkru(u32 pkru) { struct pkru_state *pk; if (!boot_cpu_has(X86_FEATURE_OSPKE)) return; pk = get_xsave_addr(¤t->thread.fpu.state.xsave, XFEATURE_PKRU); /* * The PKRU value in xstate needs to be in sync with the value that is * written to the CPU. The FPU restore on return to userland would * otherwise load the previous value again. */ fpregs_lock(); if (pk) pk->pkru = pkru; ^^^ else we just write to the PKRU register but leave XINUSE[PKRU] clear on return to usermode? That seems... unwise. __write_pkru(pkru); fpregs_unlock(); } I bet you're hitting exactly this bug. The fix ended up being a whole series of patches, but the gist of it is that the write_pkru() slow path needs to set the xfeature bit in the xsave buffer and then do the write. It should be possible to make a little patch to do just this in a couple lines of code.