Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1051802rwd; Thu, 18 May 2023 07:22:18 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6GjYGXqZ1ERgle09Q80OtrRtqH2SYplIv6VK/YUrD5fkfIAVJlrAAyX8DTDCtxTl0xsYDK X-Received: by 2002:a17:903:41cf:b0:1a9:498a:1da2 with SMTP id u15-20020a17090341cf00b001a9498a1da2mr3029275ple.56.1684419737723; Thu, 18 May 2023 07:22:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684419737; cv=none; d=google.com; s=arc-20160816; b=nj6Xdy1qHzHdKao76R/zpnOcRH+FjWbxIRwCrlDnLURYQpDHxaInR1jA26goSXWyCO /YKgcJzBbtlTny7hy9gfk/05d7s8D0Ui6ulzo8Iwny8WQ4MlM4qyZpWApt5VdNhAC3lw gwi3/6tv3iHLVaHMo+01Pkw/Ztz2SjT9iB2CahaPlEL/tQvplD3qhjGoAaCeDu0g7ynO yP0ZpFZiCFoWlX5b6/uTa2PQlmkXSqoP81F30VqFgclVr5ixTFylKzJW86AyGFWby5ZM Qz1ETa4+goZZNPBk6bqetKbU9G6xwqWZvn2QndY8RR27PMvyZKOACHEAgOmhOeXTGpzd /EXw== 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=/EG3kAoZFFoNmCZxjC6esPJxCN2aXyXfUCBCGN6t9H4=; b=vM6ZFIuSiDh2lXGExOjAnW1nR1/UPrR6AjsReWohVPq6n+TNyoBR6TaPwJIPrQ6vma aou25Fowx/snddMD1jfNCcNNB5DAtCN4NoGLusqGy+UnfgjSggHfpdX+Ex1pmP/RODW/ D2kzRlCsdTwXuFvUdA09KKbe6de2H+BjYAs/D1B5favVmjA7VxwmzhmBxnJge26K9n91 pj1YbhSYCiihTWq2BlU0EXeROQDffcbrrxJo5oi4IAZjS71WBXhRcrqvFU9cptIweAJJ pxuU4XATtsaicAzGJzqwBAcJ18EEpYmov94XnRxHsaJuSVZmmhcIAgxLBfP+pNsrYUC/ IL5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=qBiCGTID; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b8-20020a170903228800b001ab21df5958si1391106plh.515.2023.05.18.07.22.02; Thu, 18 May 2023 07:22:17 -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=qBiCGTID; 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 S230247AbjEROOq (ORCPT + 99 others); Thu, 18 May 2023 10:14:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229884AbjEROOo (ORCPT ); Thu, 18 May 2023 10:14:44 -0400 Received: from smtp-fw-80006.amazon.com (smtp-fw-80006.amazon.com [99.78.197.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3D14DC; Thu, 18 May 2023 07:14:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1684419284; x=1715955284; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=/EG3kAoZFFoNmCZxjC6esPJxCN2aXyXfUCBCGN6t9H4=; b=qBiCGTIDiOH5mAN/A1EqCjfnfQD5UEeF0WyP4RV2uHoX7qGpZGzf4duW 0yMhRbzAAmSmp0GRH0HovwkRb4cGIvtAWSK1ecn4nUm3nRjEGrVqc1lmx Iw66bOVbWhiyFSGmzdzwIYgfu4WonYLO+qTOg2SJoGC/eOad/DGddivU2 s=; X-IronPort-AV: E=Sophos;i="5.99,285,1677542400"; d="scan'208";a="214654107" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-iad-1d-m6i4x-b404fda3.us-east-1.amazon.com) ([10.25.36.210]) by smtp-border-fw-80006.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 May 2023 14:14:41 +0000 Received: from EX19MTAUWB002.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan3.iad.amazon.com [10.40.163.38]) by email-inbound-relay-iad-1d-m6i4x-b404fda3.us-east-1.amazon.com (Postfix) with ESMTPS id 1BC99822CA; Thu, 18 May 2023 14:14: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.26; Thu, 18 May 2023 14:14:38 +0000 Received: from b0f1d8753182.ant.amazon.com (10.106.82.23) by EX19D002ANA003.ant.amazon.com (10.37.240.141) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1118.26; Thu, 18 May 2023 14:14:35 +0000 From: Takahiro Itazuri To: CC: Sean Christopherson , Paolo Bonzini , , Takahiro Itazuri , Takahiro Itazuri Subject: [RESEND PATCH v2] KVM: x86: Update KVM_GET_CPUID2 to return valid entry count Date: Thu, 18 May 2023 15:14:20 +0100 Message-ID: <20230518141420.37404-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.23] X-ClientProxiedBy: EX19D035UWB003.ant.amazon.com (10.13.138.85) To EX19D002ANA003.ant.amazon.com (10.37.240.141) X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, T_SPF_PERMERROR 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 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. Signed-off-by: Takahiro Itazuri --- 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