Received: by 2002:a05:7412:b130:b0:e2:908c:2ebd with SMTP id az48csp142032rdb; Thu, 16 Nov 2023 14:29:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IEZ84iMRTSV0a1dRAL1xMSjRdtaofnZwlHxHs+n0K6s6u6+Zkc9fyilXL7rx9u+y6TT37Eh X-Received: by 2002:a17:90b:1b4a:b0:281:5550:ce3c with SMTP id nv10-20020a17090b1b4a00b002815550ce3cmr16148703pjb.38.1700173787138; Thu, 16 Nov 2023 14:29:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700173787; cv=none; d=google.com; s=arc-20160816; b=xJsSCqL6h4HYzLFN+VRmsabgHl9CDcAQIdM1ygnPkclP6vQQRhIsvDfn2Mke8aJV4y MSw1ZNtnTD2Gd+fZL5goG3ZLVi/lT3FJBgXP2tNERdL4/fuAZSieM87Y9O8JfLdKdUY2 C9ulvTh8xQ89aMnTPXuW+/9nzajteKoFoex7LbsjVQT6VU7d2Hq23QXYJxzMtyGbuRZT MkfXFPhdyj8eAtzqCiedrtTGkDomS90JYBoO0Evc6iAzSfo+WLr7Va+JvLdSvbm9geUX EClMV+KUa6gaLNiLKxubis3mzcnCbDNcwgxDJpAA/I4siTWNFodllbM14AhM3Hi+s/p9 uT5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date:dkim-signature; bh=EGIwhjjvgrhlrw+QQSLX3tkHhBU1bs3NPucIP+8y40A=; fh=TqYImRpunN8sqKD1wjpmxr7+p14fD0hbJJEdDRqqo9Q=; b=aPvJMT7T5hE3Bf2oxfNbGOMkXHoT5cOK5M1z9CTzZRkRUXO40TYnrcHa9MwOCFZHOe gVJrJIVyugef05FqHRqxY5HG02Ff0VEsJuFc26url6wYW5kyH2FD9GRBnlH+XFUB+r1F lT5VsgJvHagJd5kF3bP6WqPr/p068rLJLgjHn4T3lA6JTD7FxYys1JDxUYCmV7a6uBqt VJprokfWri/WZt8QBW0oDqO8XblJx2+Anv3TeChkvCzaz+eg4WziKg40KtVaqC51/IYq OLDHAEV/qGfO9IU8NNsarA9vVkdEi+Nd3Sxam6uDoizOAU4Dw+GU9H+pM+HL0RA3YgXt 6+qQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=WDBqCypK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id gd19-20020a17090b0fd300b0028031f2f450si459261pjb.22.2023.11.16.14.29.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Nov 2023 14:29:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=WDBqCypK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 94F2A825F17F; Thu, 16 Nov 2023 14:29:44 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234300AbjKPW3d (ORCPT + 99 others); Thu, 16 Nov 2023 17:29:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229576AbjKPW3a (ORCPT ); Thu, 16 Nov 2023 17:29:30 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 142F0D50 for ; Thu, 16 Nov 2023 14:29:05 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-da3b6438170so1758626276.1 for ; Thu, 16 Nov 2023 14:29:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700173744; x=1700778544; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=EGIwhjjvgrhlrw+QQSLX3tkHhBU1bs3NPucIP+8y40A=; b=WDBqCypKRfmdqyIZbx4YX1B8VtiZ6pA6Hww0p0FklcH6xr/6IvVVKfHibct+QfoTPo hWkOGx0zD3MMwvLDaivC+dNoBgaK266d8xWj2j9Ey9IE05bSUx+G6KOh0eqyZdlDB8hJ +h4yE7R+sJjvIYHBLq52s9oVLMWbEGNMOObLaC7fqNk8l9yAoTfZicJGf7q3+Bbj7v1d Q0X9BR7h3XkncZ9A3RQcLumiYYfl+ahBstWmeSpFjn0Pul7vtA0NNjNJUM+XkNJ2gGKX wol4BoRq8m4nESwhkbB0xlI+YAimQKd1/WNsWFeACQJpPBDGeDmEP4VEfHQbjzxNTYY1 ROcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700173744; x=1700778544; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=EGIwhjjvgrhlrw+QQSLX3tkHhBU1bs3NPucIP+8y40A=; b=cIoBuUzbx7ApW018MsqH0O2s5Sn+zVmN2P6ibY7XyzFISPzcdIAzJkED2Qttl5g/AL YTZLq6oyCR+Q8dzmQhE8tqqNJL5Cnxdk44gOmUDPGPxJwWp3uVHLbZpjVZvOEyDKHYIa VSEfHj58GaU4C0HQ4Kxsj0XmhJ/uFHCJUKTV08dopP2EJ/vvTFiFmk8JJvo6N+wIIb+Q 8A6c7jBw20XQWvEcQQ/eWQykafXC5EOFXVkd/bDsWCXYeL67vrdRM9I9B4Plr7rYUfjt GV6rjvNf69kPo3WZLKq9itT1EqMtQPD6azmI9YFhX4dUoa+3X7gLK1d+G4NjSX1Uvx// TYWQ== X-Gm-Message-State: AOJu0Yw0w8LLrAk4CfTRyy5SDM5dHee6WJ6xXJuME+h+nAiEj1xtTgxi 2dPWRKnGDEF76F0m1EbsWPnFoaQtuZE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:890a:0:b0:d9a:bce6:acf3 with SMTP id e10-20020a25890a000000b00d9abce6acf3mr520661ybl.0.1700173744305; Thu, 16 Nov 2023 14:29:04 -0800 (PST) Date: Thu, 16 Nov 2023 14:29:02 -0800 In-Reply-To: Mime-Version: 1.0 References: <20231110235528.1561679-1-seanjc@google.com> <20231110235528.1561679-4-seanjc@google.com> Message-ID: Subject: Re: [PATCH 3/9] KVM: x86: Initialize guest cpu_caps based on guest CPUID From: Sean Christopherson To: Weijiang Yang Cc: Paolo Bonzini , "kvm@vger.kernel.org" , "linux-kernel@vger.kernel.org" , Maxim Levitsky Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.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 (groat.vger.email [0.0.0.0]); Thu, 16 Nov 2023 14:29:44 -0800 (PST) On Thu, Nov 16, 2023, Weijiang Yang wrote: > On 11/11/2023 7:55 AM, Sean Christopherson wrote: >=20 > [...] >=20 > > -static __always_inline void guest_cpu_cap_check_and_set(struct kvm_vcp= u *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 =3D __feature_leaf(x86_feature); > > + > > + reverse_cpuid_check(x86_leaf); > > + vcpu->arch.cpu_caps[x86_leaf] &=3D ~__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); > > +} >=20 > I don't see any necessity to add 3 functions, i.e., guest_cpu_cap_{set, c= lear, 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 woul= d rather use the helpers, which is less code. > static __always_inline void guest_cpu_cap_update(struct kvm_vcpu *vcpu, > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 unsigned= int x86_feature, > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 bool gue= st_has_cap) > { > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 unsigned int x86_leaf =3D __fe= ature_leaf(x86_feature); >=20 > reverse_cpuid_check(x86_leaf); > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (guest_has_cap) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 vcpu->arch.cpu_caps[x86_leaf] |=3D __feature_bit(x86_fea= ture); > else > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 vcpu->arch.cpu_caps[x86_leaf] &=3D ~__feature_bit(x86_fe= ature); > } >=20 > > + > > +static __always_inline void guest_cpu_cap_restrict(struct kvm_vcpu *vc= pu, > > + unsigned int x86_feature) > > +{ > > + if (!kvm_cpu_cap_has(x86_feature)) > > + guest_cpu_cap_clear(vcpu, x86_feature); > > } >=20 > _restrict is not clear to me for what the function actually does -- it > conditionally clears guest cap depending on KVM support of the feature. >=20 > 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()?