Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp4294078rwl; Mon, 10 Apr 2023 08:55:24 -0700 (PDT) X-Google-Smtp-Source: AKy350ZSV7oL/NTqe3Lc1yroieckU0grpBdUM7W3BbGexK7gp8d2VFcuME2Cevw/xXVxAIK9AZIe X-Received: by 2002:a05:6402:8c5:b0:504:b64d:759c with SMTP id d5-20020a05640208c500b00504b64d759cmr899832edz.35.1681142124411; Mon, 10 Apr 2023 08:55:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681142124; cv=none; d=google.com; s=arc-20160816; b=KmNy5wU8jq5kUHnIFjiQGsGzi0jshWaSxxCTnWE1AmG5V5FFLzCFTP/bcLjDL58GRo DIf22D1Gzja14N25R6kSLN5HNPCLJLZlrmWoUKBEFd71dKbpUsH54X6WmaRhhwmIuply ++CvWFzwTjobqRSzQf0wRhmiEvRuU6QeMR1Suh9KHnxKC8hNLYeGKkxKGxAHC+wX0c4x P776fAWBscXKia/GBjfVTt0xIseJyN7jKydzxhcr1UFRp5qf44R9dkjnEr+vdy4x6HFi DvMiHO/UBlKI9BtB5DnsgDJRYTVCapLmLS9Lf7ubJ2M7F/lxcZADWmCN6IHZ7jRCQ4ca 1ySg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=ZSgxZZBUIkwdGxxaEIDig44+mGz6dsvZJjxlvbsSdwM=; b=MKVAuqXaJouzTnGQWKoduNw0BKL6jiIeyNIK2h05TnTCbdtGSdF2dJjXH10gLl+Eff 7Q8q1HfXZktpjKyQzUqoe5NpM8mayF+qdOqUK4JT0cOtknabBjBuZZHdZLHmp8taooOf EWsrGq7cY6JiCoFy/UCeg5kuHqULoi5Wyj3tT09uDgXtaKAWI5aOgOQ5kJwuJXlbZAPU s03jY8TwhHCebFP9x4RxZ9ZO18beEfUpwEoev51SoDjxvk9rPyA0PW77bzJ49gyiAPHC GZE4S3X2RncRiu07TgZX8cvqiXodop3xYZgZ98Q7RmboBfGzeBtO4Kn5vOvEJyDRN7+o 9gvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=tUYMZF+k; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c2-20020aa7d602000000b005049c5452c1si3293228edr.579.2023.04.10.08.54.59; Mon, 10 Apr 2023 08:55:24 -0700 (PDT) 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=@google.com header.s=20210112 header.b=tUYMZF+k; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230060AbjDJPrK (ORCPT + 99 others); Mon, 10 Apr 2023 11:47:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57488 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229711AbjDJPrJ (ORCPT ); Mon, 10 Apr 2023 11:47:09 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90514E41 for ; Mon, 10 Apr 2023 08:47:07 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id x3-20020a170902ec8300b001a1a5f6f272so3590505plg.1 for ; Mon, 10 Apr 2023 08:47:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1681141627; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ZSgxZZBUIkwdGxxaEIDig44+mGz6dsvZJjxlvbsSdwM=; b=tUYMZF+kNfp7kRJwg/VaD+6vM8QC+7Yj1S/ewwY3KEBfFn8LAGYfZ2KAfp4acU3o6j 6VjoObnDJC2jH1fdr+NZb0XE7SZswgclbj/Aid4sT8fk1Cqgl+qy3FBPVpXpkZyQbBye vH6XTJt6gnpdTSGyyzqcQsxUw6dfCd7cwwxCpD+7ocO8/S9dC390yr3cSa4EmNiuzQj4 3CT/qa5Okvgt129LDLNfNfF4uee02qtQsLjurnWDsZ9Qez27SPABdONZ4/lP6lGa13qu PYGWdKQfWJoosYRejbbmaOuSgjn5smIOWk9otgpOIOZeLUWTOq0gPza9UjJKENIOBIPF QovA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681141627; h=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=ZSgxZZBUIkwdGxxaEIDig44+mGz6dsvZJjxlvbsSdwM=; b=wn0v3slAwSSDhpw1PicNPeGUIOgyIoOP+gFwnMrzd2hMlcY3VmJdS92KSwKL8muneo zfZoXV5rS6ntBGbu4KZOdUUFlN3Kzx/V65b/khHpEMaphG3ZTlm0tvjI5rgu07HGg+7w Jh5ETKNeGVpOHgRWO9FoEPSnAtJumLoI0meH1uzE7zviSlSpZx/Qc4v4ZhG7BG/wERyF A0/VPFnejdkYNDXuXtCdcfKccUwZnKXlC/E73fPWtS9qAgv0rhZw4Ig39YKsRTxJQR8E 1IFdmlxJfb6mxiQ+1mRMSYRRRP/7jGo3aXy93FrUnjPA7xtN4ZxEncq9bx63fcALP2d+ IpwQ== X-Gm-Message-State: AAQBX9fWiNylXcNSDqqO/1ISwZAFzgBGEfhdtx6NyFq9ZUfMOSMUKMzi pc3ROZkQaqYSaOxpcbnsBHR9OXr3k5g= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a65:4c88:0:b0:513:92e7:4555 with SMTP id m8-20020a654c88000000b0051392e74555mr5661893pgt.4.1681141626989; Mon, 10 Apr 2023 08:47:06 -0700 (PDT) Date: Mon, 10 Apr 2023 08:47:05 -0700 In-Reply-To: <20230410141820.57328-1-itazur@amazon.com> Mime-Version: 1.0 References: <20230410141820.57328-1-itazur@amazon.com> Message-ID: Subject: Re: [PATCH] kvm: x86: Update KVM_GET_CPUID2 to return valid entry count From: Sean Christopherson To: Takahiro Itazuri Cc: kvm@vger.kernel.org, Paolo Bonzini , linux-kernel@vger.kernel.org, Takahiro Itazuri Content-Type: text/plain; charset="us-ascii" X-Spam-Status: No, score=-7.7 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,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 lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Capitalize KVM please, i.e. "KVM: x86:". On Mon, Apr 10, 2023, Takahiro Itazuri wrote: > Modify the KVM_GET_CPUID2 API to return the number of valid entries in > nent field of kvm_cpuid2, even when the API is successful. > > Previously, the KVM_GET_CPUID2 API only updated the nent field when an Heh, I am so used to KVM_SET_CPUID2 being a source of bugs that it took me at least three read throughs before I caught that this is fixing the GET side. > error was returned. If the API was called with an entry count larger > than necessary (e.g., KVM_MAX_CPUID_ENTRIES), it would succeed, but the > nent field would continue to show a value larger than the actual number > of entries filled by the KVM_GET_CPUID2 API. With this change, users can > rely on the updated nent field and there is no need to traverse > unnecessary entries and check whether an entry is valid or not. While I completely agree that the not updating "nent" on success is asinine, I am mildly concerned about this being a breaking ABI change, e.g. if a VMM has "nent" marked as a consant/immutable value. I suspect it's ok because AFAICT, pretty much nothing outside of selftests actually uses KVM_GET_CPUID2. But at the very least, I'll push this out until 6.5 so that it can soak in linux-next for a long time. Paolo, any thoughts/objections? > Signed-off-by: Takahiro Itazuri > --- > arch/x86/kvm/cpuid.c | 8 +++++--- > 1 file changed, 5 insertions(+), 3 deletions(-) > > diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c > index 599aebec2d52..31838dfddda6 100644 > --- a/arch/x86/kvm/cpuid.c > +++ b/arch/x86/kvm/cpuid.c > @@ -523,10 +523,13 @@ int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, > struct kvm_cpuid2 *cpuid, > struct kvm_cpuid_entry2 __user *entries) > { > - int r; > + int nent, r; > + > + nent = cpuid->nent; > + cpuid->nent = vcpu->arch.cpuid_nent; > > r = -E2BIG; > - if (cpuid->nent < vcpu->arch.cpuid_nent) > + if (nent < vcpu->arch.cpuid_nent) > goto out; > r = -EFAULT; > if (copy_to_user(entries, vcpu->arch.cpuid_entries, > @@ -535,7 +538,6 @@ int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, > return 0; > > out: > - cpuid->nent = vcpu->arch.cpuid_nent; > return r; I think we should break from the (IMO) somewhat funky KVM ioctl() pattern of r = if (try something and it fails) goto out; and instead set "r" in the error paths. That avoids the need for a scratch "nent", and IMO makes this much more straightforward. int r = 0; if (cpuid->nent < vcpu->arch.cpuid_nent) r = -E2BIG; else if (copy_to_user(entries, vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2))) r = -EFAULT; /* * Update "nent" even on failure, e.g. so that userspace can fix an * -E2BIG issue by allocating a larger array. */ cpuid->nent = vcpu->arch.cpuid_nent; return r;