Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp230198pxb; Tue, 15 Feb 2022 12:01:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJyEt9P+JyELYb0acQzmI9OsqOq3KMoyvi3e22ybV/HmZW0S3kV6pcAR8727vnt4JpAuVohE X-Received: by 2002:a62:e811:: with SMTP id c17mr361520pfi.56.1644955270725; Tue, 15 Feb 2022 12:01:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644955270; cv=none; d=google.com; s=arc-20160816; b=W5QLmNEIYX4dXvOExFYPUAluO2+xCiM4mqmvf84Ew3cG+y1gUxWhEYp9EHtvVZ80d0 NJd2+FSFHf0knvhZU0DT1E7x4YKW4O6SvyDYFXGtc1j0O8RiV+ywzcr4g77x/bjUCyA8 axrwvPE4NBm5GfrZ0UNjA7MLsjTmjYq38XwbaxUweALCYTxJ8igt+yOZMchvug6i6F99 p6DSLUz+UlU1R6Zc5XhKHf2sRDgWoA4XO3L8Wjh0QRUPN+cK8qOE4BtpObtYUzSVLaM4 yxK0iUmQZn70my5ySFd4gTYYaxci4DBZEMX1V+I5/bjefQidNgB+7vkUbhUoYnOyVifG x+6w== 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=rsKrphKAL3/1CC5z6jH9OwpoMXOTNr6E2gqOnqAmfTc=; b=K9UFVZDfUcvNO68t7ais0R8UQz/J9tomj7MbNdxrS5xEIWm4OKm8rmm5CBqS/RU8fx yFCwRycgLA90CWspCRGI+9sZkvUN9pacUUxegZoWLC3KWGqoSDyY+aIrGYWOC+Vk1Xh4 qTiNAMUeicl7pYZkQQpHJRxE4tO1lDdsljuS+p9JXaITUyunc3pgUW1/L9jksTOXAV74 52ZykxDmxAkWwHu+u8QvCfSnrWepTejZk5LfDVm6aDUgO5969nkVgYuEM3hG4fFO/Xr/ zf4gWPaUIcI6leuhN5J9psz7PP9/FxTRNzphLC1gtpe7x5EvJDQKqiH3e3DB2+czTFgB GS6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=di68p0dy; 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 21si3549943pgb.797.2022.02.15.12.00.51; Tue, 15 Feb 2022 12:01:10 -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=di68p0dy; 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 S232335AbiBORvk (ORCPT + 99 others); Tue, 15 Feb 2022 12:51:40 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:55234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229517AbiBORvg (ORCPT ); Tue, 15 Feb 2022 12:51:36 -0500 Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34E9EBF9 for ; Tue, 15 Feb 2022 09:51:25 -0800 (PST) Received: by mail-ed1-x533.google.com with SMTP id f17so753432edd.2 for ; Tue, 15 Feb 2022 09:51:25 -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=rsKrphKAL3/1CC5z6jH9OwpoMXOTNr6E2gqOnqAmfTc=; b=di68p0dyAx2HAO0bzn3kYS0zIYH26Jthhw3M6EEVlFzSZVY06lp2Cw+SdejpV20zJS PvnrJ2eL7y6A8bcN8WOsFYxgRrmy8oZfQUE/wYBuIbr3Z/HwOKA63bEvyPxBIcvTa4YP SkIO+0UzfHDguIb4nERzUc9E1FEv/9e0yhyKGdjqzTF7ohFgn98wGEvVT2Czjojm0WSg GdN+fGLLUUfIcs36YXBgSEylQATagUod4GTQFyQGWb747fzfcAOZkEghOgTgC1XZCRVY y0DQ1ua10TvjUvuHjghm/gurzjxiBnO0ewb/A+cV+buVK19BNy/xHCUr8lecHytOvclx vJiA== 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=rsKrphKAL3/1CC5z6jH9OwpoMXOTNr6E2gqOnqAmfTc=; b=o0c182zL1psSa0zWBvisOXkPcHJoUgf3FlWpDaj4QSQ1Qic9VVr2B1+8flPB8I+xXO nMXzjYQb5pqz8i/wRZ+oN5KyqWxUskf56oOyionrdxO25WFHRAq/yDKJMUQXv+Pjmzy4 GkAeyZlyLp6mGpxJlkwNI3GaVESYBN340VGXNmDXCXgDxn3or5cghxHB8Uowq2Vd5YPJ mp4rxhyzT+7YlhCv2cQfIBvEAfoQ8nGVFo7GQaVNwPFW5tced4QhyFyU0Un9fZDSRT7r FTbIDi1DgF0SLLDKy96a9RwV0x7lpP804+3Dl8KtxdhJWvBaX6OslQrVVU5XeAbbOi/1 htRQ== X-Gm-Message-State: AOAM533DEiUB1lt3VpR6PnciY8eRvyWRV6sGWamj59ej5r/b2aRreMm8 AQqMu6EthIHWgPYr8ctYPyQxY+0f9KZA8UfkC33jZyB2FA4= X-Received: by 2002:a05:6402:2993:: with SMTP id eq19mr37686edb.233.1644947483392; Tue, 15 Feb 2022 09:51:23 -0800 (PST) MIME-Version: 1.0 References: <20220215153644.3654582-1-bgeffon@google.com> <56fc0ced-d8d2-146f-6ca8-b95bd7e0b4f5@intel.com> In-Reply-To: <56fc0ced-d8d2-146f-6ca8-b95bd7e0b4f5@intel.com> From: Brian Geffon Date: Tue, 15 Feb 2022 12:50:47 -0500 Message-ID: Subject: Re: [PATCH] x86/fpu: Correct pkru/xstate inconsistency To: Dave Hansen Cc: 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 Hi Dave, Thank you for taking a look at this. On Tue, Feb 15, 2022 at 12:13 PM Dave Hansen wrote: > > On 2/15/22 07:36, Brian Geffon wrote: > > There are two issues with PKRU handling prior to 5.13. > > Are you sure both of these issues were introduced by 0cecca9d03c? I'm > surprised that the get_xsave_addr() issue is not older. > > Should this be two patches? You're right, the get_xsave_addr() issue is much older than the eager reloading of PKRU. I'll split this out into two patches. > > > The first is that when eagerly switching PKRU we check that current > > Don't forget to write in imperative mood. No "we's", please. > > https://www.kernel.org/doc/html/latest/process/maintainer-tip.html > > This goes for changelogs and comments too. This will be corrected in future patches. > > > 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. By forcing the read > > with this_cpu_read() the correct task is used. Without this it's > > possible when switching from a kernel thread to a userspace thread > > that we'll still observe the 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 we consider in that situation > > is the flags from some other non kernel task and the next fpu is > > passed in to switch_fpu_finish(). > > It makes *sense* that there would be a place in the context switch code > where 'current' is wonky, but I never realized this. This seems really > fragile, but *also* trivially detectable. > > Is the PKRU code really the only code to use 'current' in a buggy way > like this? Yes, because the remaining code in __switch_to() references the next task as next_p rather than current. Technically, it might be more correct to pass next_p to switch_fpu_finish(), what do you think? This may make sense since we're also passing the next fpu anyway. > > > The second issue is when using write_pkru() we only write to the > > xstate when the feature bit is set because get_xsave_addr() returns > > NULL when the feature bit is not set. This is problematic as the CPU > > is free to clear the feature bit when it observes the xstate in the > > init state, this behavior seems to be documented a few places throughout > > the kernel. If the bit was cleared then in write_pkru() we would happily > > write to PKRU without ever updating the xstate, and the FPU restore on > > return to userspace would load the old value agian. > > > ^ again > > It's probably worth noting that the AMD init tracker is a lot more > aggressive than Intel's. On Intel, I think XRSTOR is the only way to > get back to the init state. You're obviously hitting this on AMD. > > It's also *very* unlikely that PKRU gets back to a value of 0. I think > we added a selftest for this case in later kernels. > > That helps explain why this bug hung around for so long. > > > diff --git a/arch/x86/include/asm/fpu/internal.h b/arch/x86/include/asm/fpu/internal.h > > index 03b3de491b5e..540bda5bdd28 100644 > > --- a/arch/x86/include/asm/fpu/internal.h > > +++ b/arch/x86/include/asm/fpu/internal.h > > @@ -598,7 +598,7 @@ static inline void switch_fpu_finish(struct fpu *new_fpu) > > * PKRU state is switched eagerly because it needs to be valid before we > > * return to userland e.g. for a copy_to_user() operation. > > */ > > - if (!(current->flags & PF_KTHREAD)) { > > + if (!(this_cpu_read(current_task)->flags & PF_KTHREAD)) { > > This really deserves a specific comment. > > > /* > > * If the PKRU bit in xsave.header.xfeatures is not set, > > * then the PKRU component was in init state, which means > > diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h > > index 9e71bf86d8d0..aa381b530de0 100644 > > --- a/arch/x86/include/asm/pgtable.h > > +++ b/arch/x86/include/asm/pgtable.h > > @@ -140,16 +140,22 @@ static inline void write_pkru(u32 pkru) > > 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; > > + /* > > + * The CPU is free to clear the feature bit when the xstate is in the > > + * init state. For this reason, we need to make sure the feature bit is > > + * reset when we're explicitly writing to pkru. If we did not then we > > + * would write to pkru and it would not be saved on a context switch. > > + */ > > + current->thread.fpu.state.xsave.header.xfeatures |= XFEATURE_MASK_PKRU; > > I don't think we need to describe how the init optimization works again. > I'm also not sure it's worth mentioning context switches here. It's a > wider problem than that. Maybe: > > /* > * All fpregs will be XRSTOR'd from this buffer before returning > * to userspace. Ensure that XRSTOR does not init PKRU and that > * get_xsave_addr() will work. > */ > > > + pk = get_xsave_addr(¤t->thread.fpu.state.xsave, XFEATURE_PKRU); > > + BUG_ON(!pk); > > A BUG_ON() a line before a NULL pointer dereference doesn't tend to do > much good. > > > + pk->pkru = pkru; > > __write_pkru(pkru); > > fpregs_unlock(); > > } > Brian