Received: by 2002:a05:6a10:8395:0:0:0:0 with SMTP id n21csp599459pxh; Tue, 9 Nov 2021 15:58:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJzDg0+6KDw72kqVjIQ8GuMnEvpRuayIewuTc38EaQPC+gzDfNTznj67AOK5Or21PaTcXFGm X-Received: by 2002:a05:6e02:1a69:: with SMTP id w9mr8446308ilv.312.1636502315014; Tue, 09 Nov 2021 15:58:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1636502315; cv=none; d=google.com; s=arc-20160816; b=gsJYQ4HGrs7rVxq+HI/zo/d3mnYUQfTT/+a/EcGG4LimW+cN4jTO0E5CUqcKcJko0V BeQ4TjtJTGDnUlf2LuQysL5JweQm5n7o5vmr2k/dxAhR9WSKeENU0dkcJUlpVukm48DZ J2o/gtUUvF5zk7poMoRQtytmYgFHr3Ixf7eR5rzONcuVah98B+uYRrajD4iGnuNntObO QY69VuxeDcFECRyBCjOS0vEZogGSCt1dN/BSzQkKIOZqQyN2TmDLDFGJZUdw1sB1DXh8 umPdoxNhKaJ4/so5nYTIzcfFNRi2w5yrUqlN952wVaGqHzS4ViWci/zgkYtgUe9tXNmA rE8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:subject:cc:to:from:date:references:in-reply-to :message-id:mime-version:user-agent:dkim-signature; bh=oD3ikc7i6JMzU1W2pWhNZEH40HpLUKoGeRBpcc3g/EA=; b=qTldT8HZOtuo6GDCvZKnQRhJXs5f/8ibvuKoLHh4OBJhkgHC//5pays2pEqSTeI+rT uB0GEDf2VmO46ItTgmhoAvrPGqSKjYflvOg/A1rmlpXWzNxwJJLroAbgrnVuwHRJd0p9 61/HXF5wVb3wDpE211xUnoTlUN/h/E0jNJbte5nTges+sQB74Qn2k0bWfePTWg0yYva0 mJP/DTJOWJBElUL0QNd5bHP5oEQvzxISb1oaXGULaYqOhxjaO3+2dQt+ftHsLXvDjDRw oCtduaeOkLEUz0YahDEEZHyjM/BMKNMpas0FxW3MchqesO1fTCfqI3cNGlQVzqUzYsy0 3yQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=tES8zGCs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id j8si49021169ils.92.2021.11.09.15.58.22; Tue, 09 Nov 2021 15:58:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=tES8zGCs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S230181AbhKIPBJ (ORCPT + 97 others); Tue, 9 Nov 2021 10:01:09 -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.