Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp586289pxj; Fri, 28 May 2021 10:30:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyfnp3kHAS/lHgdMVeecrmrFG9AEVserPc0qYDcCwOhTpXRfb4Fme3HXeKc8AcZKiJg8DQ8 X-Received: by 2002:a02:a492:: with SMTP id d18mr9651058jam.28.1622223040219; Fri, 28 May 2021 10:30:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622223040; cv=none; d=google.com; s=arc-20160816; b=Dr4qdxatEjxJnTErYhg2kEupC2hqW0Xz7BoSv/F3cKgLoNZmfjHr9A2rIAY5LZwkZ1 YVcaIbccT1E3Fugw45RgopPqwOh4VBPqEfzh0F/l766LrJ/XS9w4fHcnLWLdmmnuEfOQ LkRpnrUTX1MrxmVNXqaL0m5Zw37tWmsJPntcOKvWHY4Hde+4Xgbm8+BlIAdx1FOo+z4P 4M1fVjIGWYuerpW3YQtiokFKNnJuua8A8SfNUoT2Hfw2Jn62IP/fRjz5G4tT7eTCWQye nL4T7L50B3MuCAwd/jfIsojJTzDbgyTE62d0VOCS4Epzi7GanIZX7fiGwRAyOpxE1FIV EBrA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:dkim-signature:dkim-signature:from; bh=wfCUOtFr0WygTFDkR5FKbL+FKBHLtouLyX2xmDMXokY=; b=SdY131bE41UQvtE0Fzh1ovnTWneb3smYz+mqiiYl7JRZdO8hPtMrrmlJ59uLFs8Cex tCbnN5g6esNJ5KyU2CdHchs/Kuqi/CwprD8tzC31oo/vccNP661dp8qTSSZDRN3+1d9B U2wosXnF+DBP6wWCERFHYirOiw63XWUEZle4FOHpgU3MKxNMsXbFOq+5fpq2tHxPYLY7 Hz9R499rr1pXFEPVWbd7btHjTtQ5pC9uTCjH9XxSDNswFBzV0boVixrRZDMWVaZ2g/tu 3lLEv7DxEavWQlxGxz2aYygeW17r6u4c08hT4iXJFkr9Qacs6K1fdBgGQUnYeFZLh1Th cHxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="XJHUYo/x"; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r84si6686269jac.11.2021.05.28.10.30.25; Fri, 28 May 2021 10:30:40 -0700 (PDT) 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=@linutronix.de header.s=2020 header.b="XJHUYo/x"; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235266AbhE1PeG (ORCPT + 99 others); Fri, 28 May 2021 11:34:06 -0400 Received: from Galois.linutronix.de ([193.142.43.55]:41870 "EHLO galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233270AbhE1PeE (ORCPT ); Fri, 28 May 2021 11:34:04 -0400 From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1622215949; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=wfCUOtFr0WygTFDkR5FKbL+FKBHLtouLyX2xmDMXokY=; b=XJHUYo/xVErYy2KSD2fMOaqRQNHK7WQFOUa/rY4p3/O3gHnW3FdxNMVZK5ldM978gRhWvw x/a9THMYnlNxTubGss82N/FovVPP5pi8qDEd/1LXAlhm4xkWrhuWALF7hBqvxIuv1cGigI FbnX2RsDcGVY4Tws6A0zX4XvFzkfyE6AiyeJOBVzE8oekXJQn5qg7miIHtro/y5GSqSM9V zRXIGiR3wgJHEI+rnIOo0xiHQFz44YHTaZ/HaZtKMzz0z85h+CdownR59LPzGxCCWx9TBH 9O9GlFDk+k0s439trD4K9ZE7POEOk1VlxnSAq68XA1la3LXdYagwH+tBtlH3AA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1622215949; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=wfCUOtFr0WygTFDkR5FKbL+FKBHLtouLyX2xmDMXokY=; b=Ytn9OwDnX8/ud2+p2S0N5XmEahRWOh7e9ogMSavA2u0i7uq2gVDRiN60Yzkif3BO8hhUDS YN7shPb+VW1CP4Bg== To: Dave Hansen , linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Dave Hansen , mingo@redhat.com, bp@alien8.de, x86@kernel.org, luto@kernel.org, shuah@kernel.org, babu.moger@amd.com, dave.kleikamp@oracle.com, linuxram@us.ibm.com, bauerman@linux.ibm.com Subject: Re: [PATCH 0/5] x86/pkeys: PKRU manipulation bug fixes and cleanups In-Reply-To: <20210527235109.B2A9F45F@viggo.jf.intel.com> References: <20210527235109.B2A9F45F@viggo.jf.intel.com> Date: Fri, 28 May 2021 17:32:28 +0200 Message-ID: <87eedq7u2b.ffs@nanos.tec.linutronix.de> MIME-Version: 1.0 Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 27 2021 at 16:51, Dave Hansen wrote: > Andy Lutomirski recently noted a probable bug in write_pkru(), but > it was unclear if it was user-visible. A recent bug report in > related code[1] forced me to take a look. > > Basically, manipulation of XSAVE state is too unstructured. > get_xsave_addr() gives callers the impression they can read and > write XSAVE state when there are a lot of pitfalls, like updates > to xstate.features bits. > > As a result, more than one call site screws up the modification > of PKRU in the XSAVE buffer. This series fixes that problem up > and also hopefully carves out a less error-prone path that can > be reused for other XSAVE features. > > This series: > * Moves the PKRU manipulation to a more appropriate location, > away from the page table code > * Wraps get_xsave_addr() with more structured, less error-prone > interfaces. > * Conditionally hides a pkey debugfs file, eliminating the need > for new runtime checks to work with the new interface. > * Add a selftest to make it more likely to catch bugs like this > in the future. This improved selftest catches this issue on > Intel CPUs. Without the improvement, it only triggers on AMD. I think all of this is fundamentaly wrong. Contrary to FPU state, PKRU has to be updated at context switch time. There is absolutely no point in having PKRU XSAVES managed. It's broken in several ways. Anything which clears and loads the FPU will load the wrong PKRU value. Go figure... So the right thing is to disable PKRU in XCR0 and on sched out simply do task->thread.pkru = read_pkru(); and on sched in write_pkru(task->thread.pkru); Simple, trivial and not going to be wreckaged by anything which fiddles with xstates. We all know by now that xstates is a trainwreck and not having stuff like that in there is making the fixes I'm doing way simpler. CET will have a similar issue, but we'll discuss that once we have the existing horrors sorted. Thanks, tglx