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 v2] KVM: x86: Update KVM_GET_CPUID2 to return valid entry count
Date: Thu, 13 Apr 2023 18:58:44 +0100 [thread overview]
Message-ID: <20230413175844.21760-1-itazur@amazon.com> (raw)
Modify the KVM_GET_CPUID2 API to return the number of valid entries in
nent field of kvm_cpuid2 even on success.
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.
Suggested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Takahiro Itazuri <itazur@amazon.com>
---
Changes
v1 -> v2
* Capitalize "kvm" in the commit title.
* Remove a scratch "nent" variable.
* Link to v1: https://lore.kernel.org/all/20230410141820.57328-1-itazur@amazon.com/
arch/x86/kvm/cpuid.c | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index 599aebec2d52..20d28ebdc672 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -523,18 +523,18 @@ int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
struct kvm_cpuid2 *cpuid,
struct kvm_cpuid_entry2 __user *entries)
{
- int r;
+ int r = 0;
- r = -E2BIG;
if (cpuid->nent < vcpu->arch.cpuid_nent)
- goto out;
- r = -EFAULT;
- if (copy_to_user(entries, vcpu->arch.cpuid_entries,
+ r = -E2BIG;
+ else if (copy_to_user(entries, vcpu->arch.cpuid_entries,
vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
- goto out;
- return 0;
+ r = -EFAULT;
-out:
+ /*
+ * 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;
}
--
2.39.2
next reply other threads:[~2023-04-13 17:59 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-13 17:58 Takahiro Itazuri [this message]
2023-05-26 20:25 ` [PATCH v2] KVM: x86: Update KVM_GET_CPUID2 to return valid entry count Sean Christopherson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230413175844.21760-1-itazur@amazon.com \
--to=itazur@amazon.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=zulinx86@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox