Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp122565rdf; Mon, 20 Nov 2023 19:10:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IHJzcaAlyIhfAHiGt7+Rj4ojDbiO8ziXSI03sUrxif7n+ElN/ubioXazEJNvBbQFkDQTxV4 X-Received: by 2002:a05:6a00:10d1:b0:6c6:9c3b:f0ed with SMTP id d17-20020a056a0010d100b006c69c3bf0edmr7873016pfu.7.1700536241620; Mon, 20 Nov 2023 19:10:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700536241; cv=none; d=google.com; s=arc-20160816; b=MlGhKRCXxT87+iZlm1apg8lSDHg9G+4Wuz9d2Qitb5ysguyl5+h95OL2sK8649faCi F6juA5ntG5y+S41lE7QMqkczI2oZVX08H7vkfWw0Qq2EbmsMXTd7IE76jcyDV1TDz4hy KWji83+72AQzbW3E62cbPyY+7nUwXtht1KOLQ2N2GzT/gy5fr1BZ6y628TAHp6L0QGbz Ql3GHKZOvroYsH0a5YvJbAcqbuxu6Ewc8TUqLjUTVEObvebhsjAoCESfRvvQh5e31cZO WLFlotlW8pRdQSs1qJTMEkjQp433Y1vqK7ttPHYZy1kv8cwQ3hbXl+0imkrhEjSzqW3S thRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=aE8hz6b0iKhrY1yTls4I+5cAfg7hEvMnIsV3UapUdtw=; fh=huYDiR9T6gVoYiKW//hiEasp2nl8TrUxUcnx5nDSc48=; b=WJnr99e2Gyq3puCq+sT/dt62eKmsKtsaA2REBUdwz3xlr1j/hO2cTW/ceS8g63CciO OC0QEifaeXR62vyWLKWrvHRZpykssjbe++D5Tw5WXmNabemdNfkmMDAvbDtP0xg8TLsM Yt46V5hNKPwa1JSiLN9AdSaLetRR7AwaCzV94jtdQU4wU4LfpdHw71NMKFXo//a3fUDN EhS9ZfPLgYme5QaQbUb73dior2tLqi1OF900/+9nyaYkcg8bVBWMAbPwZAvCoAndn4It +fNJzgTlnTzIdBw4NFjvfyqhEEBAI10RBbApBaJe+SuktxoKS/YR5c3vKcl/ry9lfxff FCOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fFPRvxZl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id f28-20020aa7969c000000b006cb88d3fab6si4044956pfk.69.2023.11.20.19.10.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Nov 2023 19:10:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fFPRvxZl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 44690803FC3B; Mon, 20 Nov 2023 19:10:39 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232876AbjKUDKb (ORCPT + 99 others); Mon, 20 Nov 2023 22:10:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229618AbjKUDKa (ORCPT ); Mon, 20 Nov 2023 22:10:30 -0500 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E63AFBC; Mon, 20 Nov 2023 19:10:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700536226; x=1732072226; h=date:from:to:cc:subject:message-id:references: mime-version:content-transfer-encoding:in-reply-to; bh=+P9oonEPLwebJ2u+rmYI9E5ttIAiBdzIFEWcnlkvP0U=; b=fFPRvxZloSQxNv/nw9SBQ5VfPH/+sETd9Jm9lRvYXIljuXFBH4ZA3P6G UDwHJbj6KmsKXhEprnOJ3Dy/xSU/GcBLKlYPHP0xQFtiASK3SLrhBzQ4O Oewta5Bgl3xXAvHO4Bf1WfSO/7R+nW/5IOA+HyPzxtvXaQ4skrm+8HoM1 EYXZbp/16H8JvDxdCvbsyVni8hegNrtvrtRH07Rb3RWTt33DbAbBkSxCn JPNelM3dkPGJpGnMDEez6SO7GdrlnjdhH8sY0kmUWHu5yHezupb1cLY+i ffxcCsZng7G4/4wEcYRIFEl8nsrBZh0yVA1WKb/lFOFOru1/NLLAL5+lx Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10900"; a="390609584" X-IronPort-AV: E=Sophos;i="6.04,215,1695711600"; d="scan'208";a="390609584" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Nov 2023 19:10:26 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,215,1695711600"; d="scan'208";a="7921020" Received: from yy-desk-7060.sh.intel.com (HELO localhost) ([10.239.159.76]) by fmviesa002.fm.intel.com with ESMTP; 20 Nov 2023 19:10:24 -0800 Date: Tue, 21 Nov 2023 11:10:24 +0800 From: Yuan Yao To: "Yang, Weijiang" Cc: Sean Christopherson , Paolo Bonzini , "kvm@vger.kernel.org" , "linux-kernel@vger.kernel.org" , Maxim Levitsky Subject: Re: [PATCH 3/9] KVM: x86: Initialize guest cpu_caps based on guest CPUID Message-ID: <20231121031024.gxghavohrkgh7psd@yy-desk-7060> References: <20231110235528.1561679-1-seanjc@google.com> <20231110235528.1561679-4-seanjc@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: NeoMutt/20171215 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,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 agentk.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 (agentk.vger.email [0.0.0.0]); Mon, 20 Nov 2023 19:10:39 -0800 (PST) On Fri, Nov 17, 2023 at 04:33:27PM +0800, Yang, Weijiang wrote: > On 11/17/2023 6:29 AM, Sean Christopherson wrote: > > On Thu, Nov 16, 2023, Weijiang Yang wrote: > > > On 11/11/2023 7:55 AM, Sean Christopherson wrote: > > > > > > [...] > > > > > > > -static __always_inline void guest_cpu_cap_check_and_set(struct kvm_vcpu *vcpu, > > > > - unsigned int x86_feature) > > > > +static __always_inline void guest_cpu_cap_clear(struct kvm_vcpu *vcpu, > > > > + unsigned int x86_feature) > > > > { > > > > - if (kvm_cpu_cap_has(x86_feature) && guest_cpuid_has(vcpu, x86_feature)) > > > > + unsigned int x86_leaf = __feature_leaf(x86_feature); > > > > + > > > > + reverse_cpuid_check(x86_leaf); > > > > + vcpu->arch.cpu_caps[x86_leaf] &= ~__feature_bit(x86_feature); > > > > +} > > > > + > > > > +static __always_inline void guest_cpu_cap_change(struct kvm_vcpu *vcpu, > > > > + unsigned int x86_feature, > > > > + bool guest_has_cap) > > > > +{ > > > > + if (guest_has_cap) > > > > guest_cpu_cap_set(vcpu, x86_feature); > > > > + else > > > > + guest_cpu_cap_clear(vcpu, x86_feature); > > > > +} > > > I don't see any necessity to add 3 functions, i.e., guest_cpu_cap_{set, clear, change}, for > > I want to have equivalents to the cpuid_entry_*() APIs so that we don't end up > > with two different sets of names. And the clear() API already has a second user. > > > > > guest_cpu_cap update. IMHO one function is enough, e.g,: > > Hrm, I open coded the OR/AND logic in cpuid_entry_change() to try to force CMOV > > instead of Jcc. That honestly seems like a pointless optimization. I would > > rather use the helpers, which is less code. > > > > > static __always_inline void guest_cpu_cap_update(struct kvm_vcpu *vcpu, > > >                                                  unsigned int x86_feature, > > >                                                  bool guest_has_cap) > > > { > > >         unsigned int x86_leaf = __feature_leaf(x86_feature); > > > > > > reverse_cpuid_check(x86_leaf); > > >         if (guest_has_cap) > > >                 vcpu->arch.cpu_caps[x86_leaf] |= __feature_bit(x86_feature); > > > else > > >                 vcpu->arch.cpu_caps[x86_leaf] &= ~__feature_bit(x86_feature); > > > } > > > > > > > + > > > > +static __always_inline void guest_cpu_cap_restrict(struct kvm_vcpu *vcpu, > > > > + unsigned int x86_feature) > > > > +{ > > > > + if (!kvm_cpu_cap_has(x86_feature)) > > > > + guest_cpu_cap_clear(vcpu, x86_feature); > > > > } > > > _restrict is not clear to me for what the function actually does -- it > > > conditionally clears guest cap depending on KVM support of the feature. > > > > > > How about renaming it to guest_cpu_cap_sync()? > > "sync" isn't correct because it's not synchronizing with KVM's capabilitiy, e.g. > > the guest capability will remaing unset if the guest CPUID bit is clear but the > > KVM capability is available. > > > > How about constrain()? > I don't know, just feel we already have guest_cpu_cap_{set, clear, change}, here the name cannot exactly match the behavior of the function, maybe guest_cpu_cap_filter()? But just ignore the nit, up to you to decide the name :-) How about guest_cpu_cap_kvm_restrict or guest_cpu_cap_kvm_constrain ? > >