Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1518088rwb; Thu, 10 Nov 2022 18:03:07 -0800 (PST) X-Google-Smtp-Source: AA0mqf67veAbRXp+2wROMHWQKleVqii/OBzo8/3rK+3al9PQqSgFW7CBzd/UstJZpPKRWfGQT8Xk X-Received: by 2002:a17:906:dffc:b0:79e:8360:8c3d with SMTP id lc28-20020a170906dffc00b0079e83608c3dmr290601ejc.146.1668132186784; Thu, 10 Nov 2022 18:03:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668132186; cv=none; d=google.com; s=arc-20160816; b=bEeH2VPxivowv838rUWfjkIsSOXZ7OI/sK1sHshlljxaBvkST+7OIJxbNCq9yRL+3G /wmPDIMthFik3TBbNukqQIpEghTFk4HNNFtEuc0ViWQOAqPPPQvdckSlogyxkRQbSdrD 60PYJT09ChGNxrjR93icUGt7xKztTW1lM8A+GOojlVOAY2TRDKlcxlbtY+OIwD5JOIgI XaoJK9nGZDdWb2y98G3dl1F8035ho6+hobOV/GIZ/Pa80KopfKGm2dL/R/LRStY+iLJN yqliDgyE/8KG6KcZvbWv73dnO7lf9Rt+WIPSbBPUyrOUK/wYqSrMA2ZpFdN7gk4N6QEX f7AQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=si9TrIRojMfRpExN1xhgZYhE+gWj7ZYAim+0hsJei5Q=; b=m2NSrp4fiO3nRlmV72mj2K3qtwPiBp0F0Vm+W9uZm////V3QmKr7n7D9+PJmz4PkjF JJv7CLx+F9zWCoOPCQXGvziUZ9DYGVcS75jGGmCC66qA5KxzcVaEjOUHExruKUo1kSIH fkqKAT7CflVjEgmnECckRBsZDmc7uITmZPabnXj228/XrRQZq3ZIs8Yc6TYAU9czdJVg cWincZ2Fx5JI4WERci3mVWiPdulU3ii3X2BEBBaC4GHpYxx2Pc3QtLPsxDDH+f0P3gEU pX5Z7NJ3BRYuDeXJ2UyMjz+LusJliJ35Hfg85ZjW7noMtyoE4a56nW7VTmHN+ZtiAROJ L/lA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=kdfmv7s9; 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=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nd32-20020a17090762a000b007877b1c7f27si790276ejc.829.2022.11.10.18.02.41; Thu, 10 Nov 2022 18:03:06 -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=@intel.com header.s=Intel header.b=kdfmv7s9; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231831AbiKKBiC (ORCPT + 92 others); Thu, 10 Nov 2022 20:38:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229757AbiKKBiB (ORCPT ); Thu, 10 Nov 2022 20:38:01 -0500 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 548EA51C24; Thu, 10 Nov 2022 17:38:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1668130680; x=1699666680; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=hOsb2zd+9aGqPYDtkVhR22vi8tnc3Ya1+UHhC6DCC0w=; b=kdfmv7s9VKFKJ2x0rx3OmExpzET2z/CNlX7l4SsqOwmxImjHyK0bLw++ Uh1F85ByCvcFof17P4z5KDeB/3e9uaErIFNI2eGk/2LlAQSnKKjhxHQRA w3TzL3X+uUoqs11Ztk1O7L84AdVyDvdbs1/TRCPRHWyXo7gVXgfmWPTkQ 1SJVoUCudZtTgnq2mxCTry+gHxfc5MdY0i/TVcjJYQSF/cKdn2+ovnn4N HgnqQxtzuSF7cp9QguFBR5rWQ7g/XZ4zlGbxJYQ9v8jImzNnP05t43BRB 2drJqzJbJEh9ufF8EypSi4/qam2zIitCZ4eqJwFyO3cUEaggiMlemY3eJ Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10527"; a="311499143" X-IronPort-AV: E=Sophos;i="5.96,155,1665471600"; d="scan'208";a="311499143" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2022 17:38:00 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10527"; a="706374427" X-IronPort-AV: E=Sophos;i="5.96,155,1665471600"; d="scan'208";a="706374427" Received: from csalvo-mobl1.amr.corp.intel.com (HELO [10.212.217.97]) ([10.212.217.97]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2022 17:37:58 -0800 Message-ID: Date: Thu, 10 Nov 2022 17:37:58 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 Subject: Re: [RESEND PATCH v6 1/2] x86/fpu: Allow PKRU to be (once again) written by ptrace. Content-Language: en-US To: Kyle Huey Cc: Linus Torvalds , Dave Hansen , Thomas Gleixner , Borislav Petkov , Ingo Molnar , x86@kernel.org, "H. Peter Anvin" , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra , Sean Christopherson , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Robert O'Callahan , David Manouchehri , Borislav Petkov , stable@vger.kernel.org References: <20221107063807.81774-1-khuey@kylehuey.com> <20221107063807.81774-2-khuey@kylehuey.com> <64e62ab9-71f6-6d90-24de-402921c244e7@intel.com> From: Dave Hansen In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_NONE 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 11/10/22 16:03, Kyle Huey wrote: > On Tue, Nov 8, 2022 at 10:23 AM Dave Hansen wrote: ... >> At a high level, this patch does a *LOT*. Generally, it's nice when >> bugfixes can be encapsulted in one patch, but I think there's too much >> going on here for one patch. > > Ok. How about I break the first part into two pieces, one that changes the > signatures of copy_uabi_from_kernel_to_xstate() and > copy_sigframe_from_user_to_xstate(), and one that moves the relevant > KVM code from fpu_copy_uabi_to_guest_fpstate() to copy_uabi_to_xstate() > and deals with the edge case behavior of the mask? Sounds like a good start. My gut says there's another patch or two that could be broken out, but that sounds like a reasonable next step. >>> diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c >>> index 3b28c5b25e12..c273669e8a00 100644 >>> --- a/arch/x86/kernel/fpu/core.c >>> +++ b/arch/x86/kernel/fpu/core.c >>> @@ -391,8 +391,6 @@ int fpu_copy_uabi_to_guest_fpstate(struct fpu_guest *gfpu, const void *buf, >>> { >>> struct fpstate *kstate = gfpu->fpstate; >>> const union fpregs_state *ustate = buf; >>> - struct pkru_state *xpkru; >>> - int ret; >>> >>> if (!cpu_feature_enabled(X86_FEATURE_XSAVE)) { >>> if (ustate->xsave.header.xfeatures & ~XFEATURE_MASK_FPSSE) >>> @@ -406,16 +404,16 @@ int fpu_copy_uabi_to_guest_fpstate(struct fpu_guest *gfpu, const void *buf, >>> if (ustate->xsave.header.xfeatures & ~xcr0) >>> return -EINVAL; >>> >>> - ret = copy_uabi_from_kernel_to_xstate(kstate, ustate); >>> - if (ret) >>> - return ret; >>> + /* >>> + * Nullify @vpkru to preserve its current value if PKRU's bit isn't set >>> + * in the header. KVM's odd ABI is to leave PKRU untouched in this >>> + * case (all other components are eventually re-initialized). >>> + * (Not clear that this is actually necessary for compat). >>> + */ >>> + if (!(ustate->xsave.header.xfeatures & XFEATURE_MASK_PKRU)) >>> + vpkru = NULL; >> >> I'm not a big fan of hunks that are part of bugfixes where it is not >> clear that the hunk is necessary. > > This is necessary to avoid changing KVM's behavior at the same time > that we change > ptrace, since KVM doesn't want the same behavior as ptrace. Your "This is necessary" doesn't really match with "Not clear that this is actually necessary" from the comment, right? Rather than claim whether it is necessary or not, maybe just say why it's there: it's there to preserve wonky KVM behavior. BTW, I'd love to know if KVM *REALLY* depends on this. It'd be nice to kill if not. >> Would something like this be more clear? >> >> if (hdr.xfeatures & XFEATURE_MASK_PKRU) { >> struct pkru_state *xpkru; >> >> xpkru = __raw_xsave_addr(xsave, XFEATURE_PKRU); >> *pkru = xpkru->pkru; >> } else { >> /* >> * KVM may pass a NULL 'pkru' to indicate >> * that it does not need PKRU updated. >> */ >> if (pkru) >> *pkru = 0; >> } > > Yeah, Sean Christopherson suggested this (with the else and if > collapsed into a single level) when I submitted this previously. I generally agree with Sean, but he's also been guilty of an atrocity or two over the years. :) While I generally like low levels of indentation I also think my version is much more clear in this case.