Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp2405126rdb; Fri, 8 Dec 2023 07:16:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IH+lv3tpYAP0eXq0eor+7nQ6PWoAdDceNhEO74Bbu7qV0a9YDTBezZj9sdyHBTqxDCgnEag X-Received: by 2002:a17:902:e749:b0:1d0:b033:4a98 with SMTP id p9-20020a170902e74900b001d0b0334a98mr187983plf.17.1702048566839; Fri, 08 Dec 2023 07:16:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702048566; cv=none; d=google.com; s=arc-20160816; b=kiFV1YMIXz/gFn1W5NHTd7iUC8rKgVCUqi1CAvrVVySgC+ymOyzjfPtwH+K1nf7dKv 9NbGm8fNdjKjUpWZbLFda4XqsiLfM3090MF2uN1Mk8hik7OsWD9J5oIhZF4ByFkH1wgY B265I3QtSycdW/z62YcrAx7M6Ht7FQc7ZLy8V03deIsYOwCrD1SobtLFtjIPhbWxil86 cswI4Jv4BaA3RgqyL1asZ6Yzxjs7F1wDEWfvluQXzC/4Hj7YZUq+BFxFFc5QildQB2Oq NOg3BM1eZjjWxWArpYXJSOPKfsmphY6SOOyWRMrKIJcfPMpu/EiWZ1ambGSt3rziCbfX v+wA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id:dkim-signature; bh=7g1DVTE/yUPzpMEJLLbgdrLjk1JCCnoCAxXauqWIKzI=; fh=Uybu0da0EIeQ5z+WCl6HYC4k4sHIVTmCUfaqg+mUfcc=; b=s90FhW2QrhK+sK/luUjzqmgjy75wEr6DPmUekEtTcbQd0q6tnauw8N23fdCaTNvZIz 4fK84x2hXQrV0naJxWVzAxbsfuMnM8+78m1NCQ6dH9Oachwgbw6Tn9JV020BzcGsr3RX 5kPPHhCINC39/tytlwlj3ySpUu5/aIrfzq9sR7+NStfuNKmyNkKZt3jpB0Gilpq06vat 21Qeh/qbIVcEOKkeORWLDN8fN74h06LCExurdkkE1G1pQ+UX4K9O2WV1C+5L2fGP0HDe IEli10vaXJYdWpZxaLOVeKS9a6JWxVfClUuq9kUMHcrAf4ucz8DhWr4bCEsPnvKu1Bkq 8dYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Rtxb8hXr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id z9-20020a170902834900b001cf68d533fasi1646581pln.633.2023.12.08.07.16.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 07:16:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Rtxb8hXr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 5736E80859A6; Fri, 8 Dec 2023 07:15:37 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1574187AbjLHPPW (ORCPT + 99 others); Fri, 8 Dec 2023 10:15:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1574175AbjLHPPV (ORCPT ); Fri, 8 Dec 2023 10:15:21 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5C0AF1 for ; Fri, 8 Dec 2023 07:15:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702048522; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7g1DVTE/yUPzpMEJLLbgdrLjk1JCCnoCAxXauqWIKzI=; b=Rtxb8hXrBRRKHy8dvODEeTJ5OBVP/I8thCmrPtdPDmn4VczIIjVH2CKuc9nLP5uOegGjuG rK4XLAGVapZaFAJjaSEwAKMf+B8Z7QvdghBhArO4NeWh774u1/SHSOLGRznZ7zJ9La6zHa T6cgeCebsr0hmOtFQudW8LpqyK+0HQA= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-618-E5NYtT-oOhqV7_q40-Vssw-1; Fri, 08 Dec 2023 10:15:21 -0500 X-MC-Unique: E5NYtT-oOhqV7_q40-Vssw-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-40c1e05ad32so15985615e9.0 for ; Fri, 08 Dec 2023 07:15:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702048520; x=1702653320; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=7g1DVTE/yUPzpMEJLLbgdrLjk1JCCnoCAxXauqWIKzI=; b=vyPyB283S5Z7ZaI0sESDJq1Z7fau0iHaSOf5qm2X2SsqxzIVhVPeUMcHgAkzhqTHLO +LrAHANBkeAft7FK3xQmQ09VxUhTjwNH77a5cJcf5zu2XA8960aNrpgwZOe9PLJTSgwO WHCOflwUOXbp5i/D6irDt+Pg7khYAyrF/EVsl1eAGk3t5ANsbJa3TeuPHtyE+vbxgxub 5UzvgoA1HP6/qNaF1+UsPTcf+2KXShIi754TuANLJ2YCcoWZ3hsVzXvAoWDoq9NuROCq hH9ppPglxTOdBEZgwP0TqunWHw2hyoxlqZT+tRcOrv8rQoqcqGEM5aJ0p9YyXp4hJh/n VrfA== X-Gm-Message-State: AOJu0Yz+j0X9nnzX8rM1Xvx0MrGXKc8qyZL1UfqUvMlKXubMi+aY5SCL xAcg7D8LwukWWSEVBmrzRdKThOmoglkJO1HRC2PXLjnLvCtNh+nxsONKGBKEHIVTUlDFeXu6SyX A2QF3CayIT2H3MZt1ThiLf1ZU X-Received: by 2002:a05:600c:228a:b0:40c:2205:e600 with SMTP id 10-20020a05600c228a00b0040c2205e600mr58208wmf.215.1702048519945; Fri, 08 Dec 2023 07:15:19 -0800 (PST) X-Received: by 2002:a05:600c:228a:b0:40c:2205:e600 with SMTP id 10-20020a05600c228a00b0040c2205e600mr58195wmf.215.1702048519507; Fri, 08 Dec 2023 07:15:19 -0800 (PST) Received: from starship ([89.237.98.20]) by smtp.gmail.com with ESMTPSA id l6-20020a5d4bc6000000b00333381c6e12sm2242436wrt.40.2023.12.08.07.15.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 07:15:19 -0800 (PST) Message-ID: Subject: Re: [PATCH v7 02/26] x86/fpu/xstate: Refine CET user xstate bit enabling From: Maxim Levitsky To: "Yang, Weijiang" Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, dave.hansen@intel.com, pbonzini@redhat.com, seanjc@google.com, peterz@infradead.org, chao.gao@intel.com, rick.p.edgecombe@intel.com, john.allen@amd.com Date: Fri, 08 Dec 2023 17:15:17 +0200 In-Reply-To: References: <20231124055330.138870-1-weijiang.yang@intel.com> <20231124055330.138870-3-weijiang.yang@intel.com> <20d45cb6adaa4a8203822535e069cdbbf3b8ba2d.camel@redhat.com> <039eaa7c35020774b74dc5e2d03bb0ecfa7c6d60.camel@redhat.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Fri, 08 Dec 2023 07:15:37 -0800 (PST) On Fri, 2023-12-08 at 22:57 +0800, Yang, Weijiang wrote: > On 12/6/2023 11:57 PM, Maxim Levitsky wrote: > > On Wed, 2023-12-06 at 09:03 +0800, Yang, Weijiang wrote: > > > On 12/5/2023 5:53 PM, Maxim Levitsky wrote: > > > > On Fri, 2023-12-01 at 14:51 +0800, Yang, Weijiang wrote: > > > > > On 12/1/2023 1:26 AM, Maxim Levitsky wrote: > > > > > > On Fri, 2023-11-24 at 00:53 -0500, Yang Weijiang wrote: > > > > > > > Remove XFEATURE_CET_USER entry from dependency array as the entry doesn't > > > > > > > reflect true dependency between CET features and the user xstate bit. > > > > > > > Enable the bit in fpu_kernel_cfg.max_features when either SHSTK or IBT is > > > > > > > available. > > > > > > > > > > > > > > Both user mode shadow stack and indirect branch tracking features depend > > > > > > > on XFEATURE_CET_USER bit in XSS to automatically save/restore user mode > > > > > > > xstate registers, i.e., IA32_U_CET and IA32_PL3_SSP whenever necessary. > > > > > > > > > > > > > > Note, the issue, i.e., CPUID only enumerates IBT but no SHSTK is resulted > > > > > > > from CET KVM series which synthesizes guest CPUIDs based on userspace > > > > > > > settings,in real world the case is rare. In other words, the exitings > > > > > > > dependency check is correct when only user mode SHSTK is available. > > > > > > > > > > > > > > Signed-off-by: Yang Weijiang > > > > > > > Reviewed-by: Rick Edgecombe > > > > > > > Tested-by: Rick Edgecombe > > > > > > > --- > > > > > > > arch/x86/kernel/fpu/xstate.c | 9 ++++++++- > > > > > > > 1 file changed, 8 insertions(+), 1 deletion(-) > > > > > > > > > > > > > > diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c > > > > > > > index 73f6bc00d178..6e50a4251e2b 100644 > > > > > > > --- a/arch/x86/kernel/fpu/xstate.c > > > > > > > +++ b/arch/x86/kernel/fpu/xstate.c > > > > > > > @@ -73,7 +73,6 @@ static unsigned short xsave_cpuid_features[] __initdata = { > > > > > > > [XFEATURE_PT_UNIMPLEMENTED_SO_FAR] = X86_FEATURE_INTEL_PT, > > > > > > > [XFEATURE_PKRU] = X86_FEATURE_OSPKE, > > > > > > > [XFEATURE_PASID] = X86_FEATURE_ENQCMD, > > > > > > > - [XFEATURE_CET_USER] = X86_FEATURE_SHSTK, > > > > > > > [XFEATURE_XTILE_CFG] = X86_FEATURE_AMX_TILE, > > > > > > > [XFEATURE_XTILE_DATA] = X86_FEATURE_AMX_TILE, > > > > > > > }; > > > > > > > @@ -798,6 +797,14 @@ void __init fpu__init_system_xstate(unsigned int legacy_size) > > > > > > > fpu_kernel_cfg.max_features &= ~BIT_ULL(i); > > > > > > > } > > > > > > > > > > > > > > + /* > > > > > > > + * CET user mode xstate bit has been cleared by above sanity check. > > > > > > > + * Now pick it up if either SHSTK or IBT is available. Either feature > > > > > > > + * depends on the xstate bit to save/restore user mode states. > > > > > > > + */ > > > > > > > + if (boot_cpu_has(X86_FEATURE_SHSTK) || boot_cpu_has(X86_FEATURE_IBT)) > > > > > > > + fpu_kernel_cfg.max_features |= BIT_ULL(XFEATURE_CET_USER); > > > > > > > + > > > > > > > if (!cpu_feature_enabled(X86_FEATURE_XFD)) > > > > > > > fpu_kernel_cfg.max_features &= ~XFEATURE_MASK_USER_DYNAMIC; > > > > > > > > > > > > > I am curious: > > > > > > > > > > > > Any reason why my review feedback was not applied even though you did agree > > > > > > that it is reasonable? > > > > > My apology! I changed the patch per you feedback but found XFEATURE_CET_USER didn't > > > > > work before sending out v7 version, after a close look at the existing code: > > > > > > > > > > for (i = 0; i < ARRAY_SIZE(xsave_cpuid_features); i++) { > > > > > unsigned short cid = xsave_cpuid_features[i]; > > > > > > > > > > /* Careful: X86_FEATURE_FPU is 0! */ > > > > > if ((i != XFEATURE_FP && !cid) || !boot_cpu_has(cid)) > > > > > fpu_kernel_cfg.max_features &= ~BIT_ULL(i); > > > > > } > > > > > > > > > > With removal of XFEATURE_CET_USER entry from xsave_cpuid_features, actually > > > > > above check will clear the bit from fpu_kernel_cfg.max_features. > > > > Are you sure about this? If we remove the XFEATURE_CET_USER from the xsave_cpuid_features, > > > > then the above loop will not touch it - it loops only over the items in the xsave_cpuid_features > > > > array. > > > No, the code is a bit tricky, the actual array size is XFEATURE_XTILE_DATA( ie, 18) + 1, those xfeature bits not listed in init code leave a blank entry with xsave_cpuid_features[i] == 0, so for the blank elements, the loop hits (i != XFEATURE_FP && !cid) then the relevant xfeature bit for i is cleared in fpu_kernel_cfg.max_features. I had the same illusion at first when I replied your comments in v6, and modified the code as you suggested but found the issue during tests. Please double check it. > > Oh I see now. IMHO the current code is broken, or at least it violates the > > 'Clear XSAVE features that are disabled in the normal CPUID' comment, because > > it also clears all xfeatures which have no CPUID bit in the table (except FPU, > > for which we have a workaround). > > > > > > How about we do this instead: > > > > for (i = 0; i < ARRAY_SIZE(xsave_cpuid_features); i++) { > > unsigned short cid = xsave_cpuid_features[i]; > > > > if (cid && !boot_cpu_has(cid)) > > fpu_kernel_cfg.max_features &= ~BIT_ULL(i); > > } > > I think existing code is more reasonable, the side-effect of current code, i.e., masking out > the unclaimed xfeature bits, sanitizes the bits in max_features at the first place, then following calculations derived from it become reasonable too. I strongly disagree with that. Kernel already removes all features bits which it knows nothing about. There is no need to also remove the xfeatures that it knows about but knows nothing about a CPUID bit. For such features the kernel needs either to accept it (like FPU) or remove the feature from set of supported features. > It also forces developers add explicit dependency claim between xfeature bit and its CPUID so that make it clear why one bit is needed in max_features. A static compile time assert for this is a good idea but not some undocumented cleaning of the bits that was not obvious to either you not me. Best regards, Maxim Levitsky > Regarding CET_USER bit handling, it could be a singular case, hope it can be tolerated :-) > > The only side effect is that this code will not clear features bits for which kernel knows > > no CPUID bit but kernel anyway clears _all_ features that it knows nothing about so there > > is no net change in behavior. > > > > (That is kernel only allows XFEATURE_MASK_USER_SUPPORTED | XFEATURE_MASK_SUPERVISOR_SUPPORTED) > > IMHO, the bits in the macros are just xfeature candidate bits, the relevant CPUID features could be disabled > for any reason, in this case, the bits should not be appear in fpu_kernel_cfg.max_features. >