Message ID | 20230410141820.57328-1-itazur@amazon.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1931617vqo; Mon, 10 Apr 2023 07:25:04 -0700 (PDT) X-Google-Smtp-Source: AKy350ZLdFZ/QO8ZRhcQ9Ul2JQu49Uep37uUrQQqaCZix788K6JfZtnQ/Rlpy8dxipSIxhcgIA75 X-Received: by 2002:a17:906:1ccc:b0:94a:74b8:7a79 with SMTP id i12-20020a1709061ccc00b0094a74b87a79mr4028275ejh.59.1681136703790; Mon, 10 Apr 2023 07:25:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681136703; cv=none; d=google.com; s=arc-20160816; b=teTdNRT+K+CZgJWTJKsmyTyNzA1fua/CjeMtUeVmAaJWj7iCXocafZnJI+N4y9hu2V iJkEDEoLiOBbuGS3TnhNHycRBe/M12W3cLMHWgb66mBqk/FDdy2hYZOPfarqHqsPJa73 n/r/NL2xH6J8EXtU+ZKyEuH7jf+WlBEfnWE8a6ttBzLY3umD6uwGouaS2SxxaXCP6HJa fNtNusGc63uUDNHDOLGjmQM+wrcIeGN7oY5SKrinq1p5UxeVTxbdKXU3+Xok5/U8uZLR sKRxGT8Bvz8YJR2uoh4qf1LhZk/2JB/TCObWO7TaRwfonmvjOJo+a7LSEVX/l3ZTg8vs ULJg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=i+S/sDMXP9J94sRN9gjxGcSuFRzKplRBtZn53LQ1X8o=; b=kTI2D+LG+FxyXf2G02lC29WWSLybIVNnHmu7+VVz21iUlF5JRxGfwsHdXTVTPIDQVM U4ablPhZZvyuhojGbhBlPPw9qrhIdKrfpPSuZfu+bmB5hq+GmHhMPUl21VE1N/UFvwD9 2FY5jbq3Y0dYP2vs+SmAaB01nWscOfEM06LZF8rXpBszqlt2QQ2fiVH3cjjjhppbH7il +omNfLzrhwRk8H3OL5aZ3EFiLZTl6AzxXSfLMtyRvuEh2d8AmJM9ZeCzo3fw29zteHPf VTReRo4mVf6J8U/KgZ7qRcjKu61HoEsgTUOH7znKEWlnwGErVQC9Kwui63QtHVqgH9eX p3vQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=L5q0K+Ym; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dt12-20020a170907728c00b0094af9b72966si1113093ejc.1050.2023.04.10.07.24.39; Mon, 10 Apr 2023 07:25:03 -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=@amazon.com header.s=amazon201209 header.b=L5q0K+Ym; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229707AbjDJOSr (ORCPT <rfc822;yuanzuo1009@gmail.com> + 99 others); Mon, 10 Apr 2023 10:18:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229603AbjDJOSp (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 10 Apr 2023 10:18:45 -0400 Received: from smtp-fw-2101.amazon.com (smtp-fw-2101.amazon.com [72.21.196.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 753E5213C; Mon, 10 Apr 2023 07:18:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1681136324; x=1712672324; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=i+S/sDMXP9J94sRN9gjxGcSuFRzKplRBtZn53LQ1X8o=; b=L5q0K+YmTkCVAcIVOtlFdmrdi6Gf+cgVragl0cP7eIihGjuVuf2mvaYp Tv87h0EUPUlFZC+bFa+3T5kImYNcmUFbryLpux6M3929j28spHe7u1TIb lBXFW/A/tffo7gnRJ0wEe1jFkfR+PKpIVZ5n3iOCtcuncdtJNMGrghAO6 Q=; X-IronPort-AV: E=Sophos;i="5.98,333,1673913600"; d="scan'208";a="312348347" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-pdx-2a-m6i4x-d47337e0.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-2101.iad2.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Apr 2023 14:18:41 +0000 Received: from EX19MTAUWB002.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan3.pdx.amazon.com [10.236.137.198]) by email-inbound-relay-pdx-2a-m6i4x-d47337e0.us-west-2.amazon.com (Postfix) with ESMTPS id 0EA6060A8C; Mon, 10 Apr 2023 14:18:39 +0000 (UTC) Received: from EX19D002ANA003.ant.amazon.com (10.37.240.141) by EX19MTAUWB002.ant.amazon.com (10.250.64.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Mon, 10 Apr 2023 14:18:39 +0000 Received: from b0f1d8753182.ant.amazon.com.com (10.106.82.21) by EX19D002ANA003.ant.amazon.com (10.37.240.141) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Mon, 10 Apr 2023 14:18:35 +0000 From: Takahiro Itazuri <itazur@amazon.com> To: <kvm@vger.kernel.org> CC: Sean Christopherson <seanjc@google.com>, Paolo Bonzini <pbonzini@redhat.com>, <linux-kernel@vger.kernel.org>, Takahiro Itazuri <zulinx86@gmail.com>, Takahiro Itazuri <itazur@amazon.com> Subject: [PATCH] kvm: x86: Update KVM_GET_CPUID2 to return valid entry count Date: Mon, 10 Apr 2023 15:18:20 +0100 Message-ID: <20230410141820.57328-1-itazur@amazon.com> X-Mailer: git-send-email 2.38.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.106.82.21] X-ClientProxiedBy: EX19D041UWA002.ant.amazon.com (10.13.139.121) To EX19D002ANA003.ant.amazon.com (10.37.240.141) X-Spam-Status: No, score=-2.1 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, T_SPF_PERMERROR 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762799600140329790?= X-GMAIL-MSGID: =?utf-8?q?1762799600140329790?= |
Series |
kvm: x86: Update KVM_GET_CPUID2 to return valid entry count
|
|
Commit Message
Takahiro Itazuri
April 10, 2023, 2:18 p.m. UTC
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
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.
Signed-off-by: Takahiro Itazuri <itazur@amazon.com>
---
arch/x86/kvm/cpuid.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
Comments
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 <itazur@amazon.com> > --- > 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 = <errno> 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;
Date: Mon, 10 Apr 2023 08:47:05 -0700 From: Sean Christopherson <seanjc@google.com> > Capitalize KVM please, i.e. "KVM: x86:". Will fix. Thanks! > I think we should break from the (IMO) somewhat funky KVM ioctl() pattern of > > r = <errno> > 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; Looks better to me! Will fix this too!. Best regards, Takahiro Itazuri
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; }