From nobody Sun Feb 8 05:42:22 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CE62EC7EE2F for ; Fri, 26 May 2023 21:03:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229629AbjEZVDw (ORCPT ); Fri, 26 May 2023 17:03:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242813AbjEZVDq (ORCPT ); Fri, 26 May 2023 17:03:46 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BCDA2189 for ; Fri, 26 May 2023 14:03:45 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-2563aacedccso493025a91.1 for ; Fri, 26 May 2023 14:03:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685135025; x=1687727025; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=hTzx+6AXt6K1nszy+Fq4+7PqDznMb4tNoA3e1kmsjc4=; b=d6VBKb45i+nhnCfXA61AU9D4t4hC9NNWQ3oHjZy8kPJ8JbUc+p0PI3kEO/JJf4hxha CZp1j4ogdlVssTQXCqM6vFPLjoZaBDAvtmZL/IHXPlKjFVSUP/8calK2Rn3ZgQIPfLGr dvEYi267LxAayQftT9J4qsYNfZc9tmqbmFijCoJOuyYrUqn3d7R8364ynMB2lLDWl15A 3HApw/46yk2n7KEfdo4yjiPuQzTTRxfs6bQzMwDfcUcKD6MKtS7QqAFue/YfhAKtLa5W a2n4EbiHMOkUTMljD58mKPTGTbxVBlyZEIm0RXhO7Jh0s0wysOrHtj4/1nyMBWEvWxbu hXgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685135025; x=1687727025; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=hTzx+6AXt6K1nszy+Fq4+7PqDznMb4tNoA3e1kmsjc4=; b=DpHu8dhl40elbPq6Nk+pE6/5umb3/vHEhcYHUoLQaxzUjZKYy6R2dyR/4suxK2g03A n25ixwLU91y+bbyN+jFvI3jChSU7RH6XxdBqPzj7BYpaCREQgcOHnHXNNlzIFwpK1efl tx9YrPUcu51NcAiKHXazeeL/jeI4C1QJqQyia33e7padQnmn+7TbvGOwYA7cvZrH/KJ5 jk7nJhg4XJogo45xEsN0rv3Yf/zRonBCSScihq8e3cMHtX5ZnKie80V6jjPaXj5q65nC wYCdh0n3PbQG9M6aKZdW5UMMWHqRCzPcZoSxIr1WyC/5awXxND0b2xVVw4eHgXuG0Lds g3JQ== X-Gm-Message-State: AC+VfDyzX/3x/Z7+5jofQ8F3fLg90XG9g74T42UE9+DIIQPBhH4WjhTk V38lFnT1PAQqjVIWjg8cKK0GpaIUc8c= X-Google-Smtp-Source: ACHHUZ5lP+//MKLoyHxp7ZBBRTK8HPGitRtGZK1VQLXtpErXRJOzC4NPtxPD1FAXGvKn7FX7vuQdBUUv2CQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:8c83:b0:253:9751:325f with SMTP id b3-20020a17090a8c8300b002539751325fmr780071pjo.4.1685135025225; Fri, 26 May 2023 14:03:45 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 26 May 2023 14:03:39 -0700 In-Reply-To: <20230526210340.2799158-1-seanjc@google.com> Mime-Version: 1.0 References: <20230526210340.2799158-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog Message-ID: <20230526210340.2799158-2-seanjc@google.com> Subject: [PATCH v3 1/2] KVM: x86: Update number of entries for KVM_GET_CPUID2 on success, not failure From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Takahiro Itazuri Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Update cpuid->nent if and only if kvm_vcpu_ioctl_get_cpuid2() succeeds. The sole caller copies @cpuid to userspace only on success, i.e. the existing code effectively does nothing. Arguably, KVM should report the number of entries when returning -E2BIG so that userspace doesn't have to guess the size, but all other similar KVM ioctls() don't report the size either, i.e. userspace is conditioned to guess. Suggested-by: Takahiro Itazuri Link: https://lore.kernel.org/all/20230410141820.57328-1-itazur@amazon.com Signed-off-by: Sean Christopherson --- arch/x86/kvm/cpuid.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 0c9660a07b23..241f554f1764 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -501,20 +501,15 @@ int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid, struct kvm_cpuid_entry2 __user *entries) { - int r; - - r =3D -E2BIG; if (cpuid->nent < vcpu->arch.cpuid_nent) - goto out; - r =3D -EFAULT; + return -E2BIG; + if (copy_to_user(entries, vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2))) - goto out; - return 0; + return -EFAULT; =20 -out: cpuid->nent =3D vcpu->arch.cpuid_nent; - return r; + return 0; } =20 /* Mask kvm_cpu_caps for @leaf with the raw CPUID capabilities of this CPU= . */ --=20 2.41.0.rc0.172.g3f132b7071-goog From nobody Sun Feb 8 05:42:22 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F2F7C77B7A for ; Fri, 26 May 2023 21:03:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243931AbjEZVDy (ORCPT ); Fri, 26 May 2023 17:03:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243945AbjEZVDt (ORCPT ); Fri, 26 May 2023 17:03:49 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2CCFBB for ; Fri, 26 May 2023 14:03:47 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-ba8c9e9e164so2555941276.2 for ; Fri, 26 May 2023 14:03:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685135027; x=1687727027; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=94Gih39C/5Jv2sKi/kH1B+XJjmK37tFsbYqDzgh8LSo=; b=73RcvMGtNzkiHiAzrUk4D4BsUuUTIDzJR6V5U8XGMy27vYtPxtmlqH4IyiW0B8dF4O rA0ZaFT96DO5Xvb3oyGl8FUEw4jXnROB6ckPmAuDivzp5AUQk8VNSwa3ugxH4AGJQvMA xBP4sjdzixlOUqbAJE97AIk3m3mPDjp8DxuLalkyp4kNxG5QovsujN3ov45zLGAFnzA5 UeQu0iKn8bUYXNEdui1uwvMrpUsW5UBj4WxK7z07T8JbGH+MAWtkybrhXng4TQ+0SZl/ 5pLaVTGEBAHh3yjhomAGa6IIPlq1frwJZjRV/ITJGyTiEKVtGCvwVHvWbvMhT8fJgMXA N6aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685135027; x=1687727027; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=94Gih39C/5Jv2sKi/kH1B+XJjmK37tFsbYqDzgh8LSo=; b=XhPp0DoMh8lhOivOTkj5f9bpCTo39vGnlxYcLgTiUeXtD3Ri6ucAt80YdqWFTVlzPX YzDFwhkj5tF5j+Vh3CCly3sQI6O+R4SIRblSn0HuclO1hHZRxy0It+IYtOQ1mJuGOcOj 4dgedZ5sPDwp7KiqPD0pf2vy+MgPHNSjVvosiCmtv4i1XMZXJ1gBjYp96+3jC9j6zmQT eq5MSQzub49Y/C0zjTrM2pa2WBt94noW5lWZvBXos2e9/1OAZxRaaOqSIe8E6AI9SB76 j3c2j4S5tb/v0p1XVwD3s7TIF/ax/z9dLFV5F2GDF2ow7QBnD6ZBTS790xEqXBVS1a2H 8GSQ== X-Gm-Message-State: AC+VfDyHTTXuLMy4FEuarUUJ4RjaHxCTSZxys+dLVwdoqgM0VjStigDm Lm3DEFRwZDg6HBqnYig/oo5gIjnr9mU= X-Google-Smtp-Source: ACHHUZ7wz4/ZvBCdD3rlIOVUwAoTohMGbg3GIGXPcyRJsQsUEZ3BXzfeU21emCpgJ6/HAWSYg6v0LHO+KvU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:d648:0:b0:bab:9391:470d with SMTP id n69-20020a25d648000000b00bab9391470dmr1179468ybg.0.1685135027218; Fri, 26 May 2023 14:03:47 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 26 May 2023 14:03:40 -0700 In-Reply-To: <20230526210340.2799158-1-seanjc@google.com> Mime-Version: 1.0 References: <20230526210340.2799158-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog Message-ID: <20230526210340.2799158-3-seanjc@google.com> Subject: [PATCH v3 2/2] KVM: selftests: Extend cpuid_test to verify KVM_GET_CPUID2 "nent" updates From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Takahiro Itazuri Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Verify that KVM reports the actual number of CPUID entries on success, but doesn't touch the userspace struct on failure (which for better or worse, is KVM's ABI). Signed-off-by: Sean Christopherson --- .../testing/selftests/kvm/x86_64/cpuid_test.c | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tools/testing/selftests/kvm/x86_64/cpuid_test.c b/tools/testin= g/selftests/kvm/x86_64/cpuid_test.c index 2fc3ad9c887e..d3c3aa93f090 100644 --- a/tools/testing/selftests/kvm/x86_64/cpuid_test.c +++ b/tools/testing/selftests/kvm/x86_64/cpuid_test.c @@ -163,6 +163,25 @@ static void set_cpuid_after_run(struct kvm_vcpu *vcpu) ent->eax =3D eax; } =20 +static void test_get_cpuid2(struct kvm_vcpu *vcpu) +{ + struct kvm_cpuid2 *cpuid =3D allocate_kvm_cpuid2(vcpu->cpuid->nent + 1); + int i, r; + + vcpu_ioctl(vcpu, KVM_GET_CPUID2, cpuid); + TEST_ASSERT(cpuid->nent =3D=3D vcpu->cpuid->nent, + "KVM didn't update nent on success, wanted %u, got %u\n", + vcpu->cpuid->nent, cpuid->nent); + + for (i =3D 0; i < vcpu->cpuid->nent; i++) { + cpuid->nent =3D i; + r =3D __vcpu_ioctl(vcpu, KVM_GET_CPUID2, cpuid); + TEST_ASSERT(r && errno =3D=3D E2BIG, KVM_IOCTL_ERROR(KVM_GET_CPUID2, r)); + TEST_ASSERT(cpuid->nent =3D=3D i, "KVM modified nent on failure"); + } + free(cpuid); +} + int main(void) { struct kvm_vcpu *vcpu; @@ -183,5 +202,7 @@ int main(void) =20 set_cpuid_after_run(vcpu); =20 + test_get_cpuid2(vcpu); + kvm_vm_free(vm); } --=20 2.41.0.rc0.172.g3f132b7071-goog