Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp970065pxb; Wed, 16 Feb 2022 08:10:01 -0800 (PST) X-Google-Smtp-Source: ABdhPJzh7PNZCf/EVLAKDt5Da98V9DnumYZymw8oV64bavYCKd7fQ1m3GRe5yukqXdsLFbL4PqRo X-Received: by 2002:a05:6830:b96:b0:59a:9186:920b with SMTP id a22-20020a0568300b9600b0059a9186920bmr1055846otv.372.1645027801480; Wed, 16 Feb 2022 08:10:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645027801; cv=none; d=google.com; s=arc-20160816; b=VOX4bZm8Mz5K4sGoTcn2c8hxJEuCDLyr6rnKQNFzz3g/tmaD1hRPi0I/Sa0ZGBzpWt rXaVwCsRXiidIhXPRBsnkmgpCF4vopPo71pLp0uY1+eTwNApx9FNY+bx/hpu4vWTy+6b SVuyxwiELxCt4e7A77MwhEFJWtVhrVvlms4fLIfGIdVNGXp0JZPA33DI3NG/4dULz7tF PV4lKLdybw2hHbP0Ovc0diiqFwoJWQecMJV9jb2eYFsgz5m/omXpXVpT7uBzG2AYrt5w pfVfJFkAZuwmk6ootpSCIbeEZMBtm98J5LMDT2ZW2w3fXY1NZq62m1HXzvPiaZEEUuJI XvMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=N8yWtnRDmiBWo6FCFDEEJqCLqRrHkWjRAVi+s2lLvSo=; b=OYsLWRVqPXiJFvbOMZ9MMJhzvhtCkn75mbeHys+gB33JARCKmEUKyQDf8CNCs1pc4x 3d2q3ykxW8/TpcYh2kahw+bun2wGBFTmuXKABgwOuGg4/da6ro0Yvzz0/G0swjoN2GM6 bvXGHToKeKIoEle+SWpD/yoXmngI8KAz3erKCvrqH92a5gdWNPQoX+mtt45oLWkptg4a p3mQ2DqKQiqzlYOnZwN4ahtm73BC5rVmokb6Dm7wBkfSenMAE04ht/ckPMMCMkfTXDGw xxCmwTNGEQwm5uU2XkNMPZjfS1t7Ajo2yMMJodrWwB3tzIhleyi6yTVkPBNupf2e9XVU dLrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=f3BAJ3dm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s3si11431275oab.148.2022.02.16.08.09.44; Wed, 16 Feb 2022 08:10:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=f3BAJ3dm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235460AbiBPPPu (ORCPT + 99 others); Wed, 16 Feb 2022 10:15:50 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:39066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235515AbiBPPPs (ORCPT ); Wed, 16 Feb 2022 10:15:48 -0500 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E64C62A39E8 for ; Wed, 16 Feb 2022 07:15:35 -0800 (PST) Received: by mail-ej1-x62b.google.com with SMTP id p15so5308481ejc.7 for ; Wed, 16 Feb 2022 07:15:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=N8yWtnRDmiBWo6FCFDEEJqCLqRrHkWjRAVi+s2lLvSo=; b=f3BAJ3dmwr6zzbFGmJ1HHs6XNKfX2cnRHac2LOuMblS+kKXgin+3mpVmJPnogMaLRf 66LRYY89f9h/mtVellTbQy7ovV/L9QoA5GlYu2vTk25iirrtcx+xBih3GxkY7bqwPzhe LdY1iDVoEVR5IVbVqFUAH+oBTC/zcXdvuw+DPR7mMnbwezq1SEVrMtz4kr1s2c+4GlJe A0izkNiPQum40m5xPDgOQEQBQu3HvpenKgh3Z5XOqNhxjwOYDEP+j2eu0utMsqX0Ud+C 78ZW4jsF03+fDmeodT7K2ieDUaBhMKOeU6HAg2d7rz8WUdQOe5ZtGo9kyfF7qtfw/IhD zXVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=N8yWtnRDmiBWo6FCFDEEJqCLqRrHkWjRAVi+s2lLvSo=; b=L9/kaNmxxamBHZySO4AUlpbxLNMUZWzoA2GgYPwDvxag1HDu0DbvE4j6nt//OLsj6F hf6ugTGxEMZsSQF183g+jVQdY8+z0X4s7ce/5XuGVwo9tnrhV6QIqdcAo2vouoFAR/j2 +kOgBpvnkQmZEZgN007XmRMVZluxfbRLOssP414DEeaeZ16r3AVKTUkJH1BWko349Z+3 c0eC6rx9M2y8VyshRChUplLmmnM26x3BhP1S6vz+6S55PC2hT3E63MoCH0MeuXY74Bd5 BOes8tgLgEOjHFuqqLv9oKrpI2jedVJGK6ZsUbRaFjDyMd/3q8t7lBe+JcPOBe2rI367 No3A== X-Gm-Message-State: AOAM531wbrxNrTnElJGBMNF38TRo75cMs8a1zBlFrxV3boQwTbKsUtO7 j6Q2izsGQAvQEXB/Bpv1yRb22GU3P6lm3NGEkh85+Q== X-Received: by 2002:a17:907:72c3:b0:6ce:5256:1125 with SMTP id du3-20020a17090772c300b006ce52561125mr2616388ejc.697.1645024534258; Wed, 16 Feb 2022 07:15:34 -0800 (PST) MIME-Version: 1.0 References: <543efc25-9b99-53cd-e305-d8b4d917b64b@intel.com> <20220215192233.8717-1-bgeffon@google.com> In-Reply-To: From: Brian Geffon Date: Wed, 16 Feb 2022 10:14:58 -0500 Message-ID: Subject: Re: [PATCH stable 5.4,5.10] x86/fpu: Correct pkru/xstate inconsistency To: Greg KH Cc: Dave Hansen , Thomas Gleixner , Willis Kung , Guenter Roeck , Borislav Petkov , Andy Lutomirski , "# v4 . 10+" , "the arch/x86 maintainers" , LKML Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Feb 16, 2022 at 5:05 AM Greg KH wrote: > > On Tue, Feb 15, 2022 at 04:32:48PM -0500, Brian Geffon wrote: > > On Tue, Feb 15, 2022 at 2:45 PM Greg KH wrote: > > > > > > On Tue, Feb 15, 2022 at 11:22:33AM -0800, Brian Geffon wrote: > > > > When eagerly switching PKRU in switch_fpu_finish() it checks that > > > > current is not a kernel thread as kernel threads will never use PKRU. > > > > It's possible that this_cpu_read_stable() on current_task > > > > (ie. get_current()) is returning an old cached value. To resolve this > > > > reference next_p directly rather than relying on current. > > > > > > > > As written it's possible when switching from a kernel thread to a > > > > userspace thread to observe a cached PF_KTHREAD flag and never restore > > > > the PKRU. And as a result this issue only occurs when switching > > > > from a kernel thread to a userspace thread, switching from a non kernel > > > > thread works perfectly fine because all that is considered in that > > > > situation are the flags from some other non kernel task and the next fpu > > > > is passed in to switch_fpu_finish(). > > > > > > > > This behavior only exists between 5.2 and 5.13 when it was fixed by a > > > > rewrite decoupling PKRU from xstate, in: > > > > commit 954436989cc5 ("x86/fpu: Remove PKRU handling from switch_fpu_finish()") > > > > > > > > Unfortunately backporting the fix from 5.13 is probably not realistic as > > > > it's part of a 60+ patch series which rewrites most of the PKRU handling. > > > > > > > > Fixes: 0cecca9d03c9 ("x86/fpu: Eager switch PKRU state") > > > > Signed-off-by: Brian Geffon > > > > Signed-off-by: Willis Kung > > > > Tested-by: Willis Kung > > > > Cc: # v5.4.x > > > > Cc: # v5.10.x > > > > --- > > > > arch/x86/include/asm/fpu/internal.h | 13 ++++++++----- > > > > arch/x86/kernel/process_32.c | 6 ++---- > > > > arch/x86/kernel/process_64.c | 6 ++---- > > > > 3 files changed, 12 insertions(+), 13 deletions(-) > > > > > > So this is ONLY for 5.4.y and 5.10.y? I'm really really loath to take > > > non-upstream changes as 95% of the time (really) it goes wrong. > > > > That's correct, this bug was introduced in 5.2 and that code was > > completely refactored in 5.13 indirectly fixing it. > Hi Greg, > What series of commits contain that work? This is the series, https://lore.kernel.org/all/20210623120127.327154589@linutronix.de/, it does quite a bit of cleanup to correct the larger problem of having pkru merged into xstate. > And again, why not just take them? What is wrong with that if this is > such a big issue? Anything is possible I suppose but looking through the series it seems that it's not going to apply cleanly so we're going to end up with something that, if we made it work, would look very different and would touch a much larger cross section of code. If the concern here is risk of things going wrong, attempting to pull back or cherry-pick parts of this series seems riskier. However, if we don't attempt to pull back all those patches I will likely be proposing at least one more small patch for 5.4 and 5.10 to fix PKRU in these kernels because right now it's broken, particularly on AMD processors as Dave mentioned. > > > > How was this tested, and what do the maintainers of this subsystem > > > think? And will you be around to fix the bugs in this when they are > > > found? > > > > This has been trivial to reproduce, I've used a small repro which I've > > put here: https://gist.github.com/bgaff/9f8cbfc8dd22e60f9492e4f0aff8f04f > > , I also was able to reproduce this using the protection_keys self > > tests on a 11th Gen Core i5-1135G7. I'm happy to commit to addressing > > any bugs that may appear. I'll see what the maintainers say, but there > > is also a smaller fix that just involves using this_cpu_read() in > > switch_fpu_finish() for this specific issue, although that approach > > isn't as clean. > > Can you add the test to the in-kernel tests so that we make sure it is > fixed and never comes back? I'm already able to reproduce it with the kernel selftests. To be honest, I'm not sure why this hasn't been reported yet. I could be doing something horribly wrong. But it seems the likely reason is that my compiler is doing what it's allowed to do, which is optimize the load of current_task. current -> get_current() -> this_cpu_read_stable(current_task) is allowed to read a cached value. Perhaps gcc is just not taking advantage of that optimization, I'm not sure. But writing to current_task and then immediately reading it back via this_cpu_read_stable() can be problematic for this reason, and the disassembled code shows that this happening. Brian > > thanks, > > greg k-h