Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3617354rdh; Mon, 27 Nov 2023 21:16:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IGYVOfZ7TtK5ZIgDKE1YsIN+B7shwi1vM55sMfj108+7eTAK2H3WRCsfJd6X+abA9PMM14m X-Received: by 2002:a17:903:22d0:b0:1cf:d06e:d730 with SMTP id y16-20020a17090322d000b001cfd06ed730mr6192239plg.1.1701148560681; Mon, 27 Nov 2023 21:16:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701148560; cv=none; d=google.com; s=arc-20160816; b=b2iCbXI1YpM9qg/tHy1tyk7VrdDUAg5c0Dgv0BEy24+v0Y7ToL1sAgQDfxRBYyvnA0 UvIkAhiAM9gBQ869/BZ3jgw7pdhs3g8ztIXzcr9AEtigDBh2u2L/H0BbQrD5pu34UY7/ +OXZpbVS+imzKuD0aUtr20pkHMcYGAxWEq7FmTHP4rxtPnuFpV9PQa3zBDAZcJVfP9zf OGTcgH4ryXGOEXzEW+Mc0qmT3VfaFq4Rg1pckudN09N3SzVlMXx0ewiV6o69CR8JtDpV RpfCTzCg7OSnij4nmZXLM9JbfJB5lOoNmooJAlJoXFsumkYP2Ky5ju3lSaGCxcQoBrKY jy7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=Ik56pMa+cc5sXNslBPLdn9s0SXOVMLoLqeBzMAkMVP4=; fh=I8v00HP8v/7XmvbJFVoSvRsEW7njtxJk0KlswSm8TEQ=; b=D0qmCj9GuSqNf0eCcqvfq0Umcg7Ar1Fr2b8K66ATAUdWeQddnkpfglLaXkXdI4ibCE qS6gpMWnj2QjM6NtP3HZtt2sZWWG+fMpct8m5EfTN6NjqwyoYQt5CEeS6ms7bdat8mmq wybFPHpgA2G10zEpGAaYhDQbr49R6m3zF1Jr7rKXqvbGYdypl1Qp/zzWSV99PkcJBSHI FdhQVfODmd3kOrklpH+vkUsgpArX2b4on7ElAlzPR7SfbHgV4bpB2UeJKhdQRDRteOyN Q510vcFP3KYWDw64tvcRhUv0AJW+4jj6wi6vFAvanOGrAB5OvOwtqh+emKV0KHzWCNAF K52g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=RQE1ZP95; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id z12-20020a170902cccc00b001c74d1da69csi12292012ple.362.2023.11.27.21.16.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 21:16:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=RQE1ZP95; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id 6909080A9937; Mon, 27 Nov 2023 21:15:59 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232891AbjK1FPq (ORCPT + 99 others); Tue, 28 Nov 2023 00:15:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234613AbjK1FPn (ORCPT ); Tue, 28 Nov 2023 00:15:43 -0500 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 91C631B6; Mon, 27 Nov 2023 21:15:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1701148549; x=1732684549; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=Q/YBiVMqybr/xjOXUiI/x11zIDZy2ZFZBISWrweQNL8=; b=RQE1ZP95VC91NmdA1kLtOk1QIDjsurMeTLHCNYaghojosDpftCsGHY1n sFoMVx0H+QOXuUmYy0EK/ebMvc2iso2vIzh7n/m1h0nfnQmp2rKHm7t4A 03UYo9feX29QaYRWh7prAgLK2kUtQ1R3X7Ix1vN1sQMO6wJl7FYgv0nY1 fT3bjJZdVHOXaoiePnDaj72Iz+p3/D9YlY+7ydN+KhjzE8PCeqk3HrRp+ 3voIExFbWaL3lSD7R3MZZaeaY5k7X4SGLejYZ6N4K191V3Da/WeBLn4bw hgJQTEHwKft1mV+6Pn+blpMrBUvES+yqr/kCnfE+yZ3dV5a+CeNFoEBIr g==; X-IronPort-AV: E=McAfee;i="6600,9927,10907"; a="396755069" X-IronPort-AV: E=Sophos;i="6.04,233,1695711600"; d="scan'208";a="396755069" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Nov 2023 21:15:49 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10907"; a="761835305" X-IronPort-AV: E=Sophos;i="6.04,233,1695711600"; d="scan'208";a="761835305" Received: from yilunxu-optiplex-7050.sh.intel.com (HELO localhost) ([10.239.159.165]) by orsmga007.jf.intel.com with ESMTP; 27 Nov 2023 21:15:47 -0800 Date: Tue, 28 Nov 2023 13:13:46 +0800 From: Xu Yilun To: Sean Christopherson Cc: Maxim Levitsky , Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 6/9] KVM: x86: Update guest cpu_caps at runtime for dynamic CPUID-based features Message-ID: References: <20231110235528.1561679-1-seanjc@google.com> <20231110235528.1561679-7-seanjc@google.com> <4484647425e2dbf92c76a173b7b14e346f60362d.camel@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 27 Nov 2023 21:15:59 -0800 (PST) On Mon, Nov 27, 2023 at 04:43:45PM -0800, Sean Christopherson wrote: > On Fri, Nov 24, 2023, Xu Yilun wrote: > > On Sun, Nov 19, 2023 at 07:35:30PM +0200, Maxim Levitsky wrote: > > > On Fri, 2023-11-10 at 15:55 -0800, Sean Christopherson wrote: > > > > static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *entries, > > > > int nent) > > > > { > > > > struct kvm_cpuid_entry2 *best; > > > > + struct kvm_vcpu *caps = vcpu; > > > > + > > > > + /* > > > > + * Don't update vCPU capabilities if KVM is updating CPUID entries that > > > > + * are coming in from userspace! > > > > + */ > > > > + if (entries != vcpu->arch.cpuid_entries) > > > > + caps = NULL; > > > > > > I think that this should be decided by the caller. Just a boolean will suffice. > > I strongly disagree. The _only_ time the caps should be updated is if > entries == vcpu->arch.cpuid_entries, and if entries == cpuid_entires than the caps > should _always_ be updated. > > > kvm_set_cpuid() calls this function only to validate/adjust the temporary > > "entries" variable. While kvm_update_cpuid_runtime() calls it to do system > > level changes. > > > > So I kind of agree to make the caller fully awared, how about adding a > > newly named wrapper for kvm_set_cpuid(), like: > > > > > > static void kvm_adjust_cpuid_entry(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *entries, > > int nent) > > > > { > > WARN_ON(entries == vcpu->arch.cpuid_entries); > > __kvm_update_cpuid_runtime(vcpu, entries, nent); > > But taking it a step further, we end up with > > WARN_ON_ONCE(update_caps != (entries == vcpu->arch.cpuid_entries)); > > which is silly since any bugs that would result in the WARN firing can be avoided > by doing: > > update_caps = entries == vcpu->arch.cpuid_entries; > > which eventually distils down to the code I posted. OK, I agree with you. My initial idea is to make developers easier to recognize what is happening by name, without looking into __kvm_update_cpuid_runtime(). But it seems causing other subtle confuse and wastes cycles. Maybe the comment is already good enough for developers. Thanks, Yilun