From nobody Sat Apr 11 12:09:36 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 F2B9FC38A2D for ; Thu, 27 Oct 2022 09:21:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235356AbiJ0JVU (ORCPT ); Thu, 27 Oct 2022 05:21:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235293AbiJ0JU6 (ORCPT ); Thu, 27 Oct 2022 05:20:58 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50C004BA63 for ; Thu, 27 Oct 2022 02:20:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666862447; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=t13cvAMlG66kAZ5LFdmkhP3KTTs9ffalt27lXkT4M6k=; b=ZnKsJ+jwEumzN2PrjiV793IqcULQ8y6AU5ZzFWFTZ+fNaeCri3tQyK5NPxADBinHTyc9xx V6PJHZvPQqg5RtaNCcUApRCzRdlxgU3Cju/8CkJz2u+jl9YKwINqR0XExPVKbMnvaOftOz zyrkjIerEuBgKhwQFI8QFBRfky98b+0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-594-Gq6hNfj_NHSahHA2eE4mDw-1; Thu, 27 Oct 2022 05:20:41 -0400 X-MC-Unique: Gq6hNfj_NHSahHA2eE4mDw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A7A9881173C; Thu, 27 Oct 2022 09:20:40 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 83881422A9; Thu, 27 Oct 2022 09:20:38 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, stable@vger.kernel.org Subject: [PATCH v2] KVM: x86: Do not return host topology information from KVM_GET_SUPPORTED_CPUID Date: Thu, 27 Oct 2022 05:20:36 -0400 Message-Id: <20221027092036.2698180-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Passing the host topology to the guest is almost certainly wrong and will confuse the scheduler. In addition, several fields of these CPUID leaves vary on each processor; it is simply impossible to return the right values from KVM_GET_SUPPORTED_CPUID in such a way that they can be passed to KVM_SET_CPUID2. The values that will most likely prevent confusion are all zeroes. Userspace will have to override it anyway if it wishes to present a specific topology to the guest. Cc: stable@vger.kernel.org Signed-off-by: Paolo Bonzini --- Documentation/virt/kvm/api.rst | 14 ++++++++++++++ arch/x86/kvm/cpuid.c | 32 ++++++++++++++++---------------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index eee9f857a986..20f4f6b302ff 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -8249,6 +8249,20 @@ CPU[EAX=3D1]:ECX[24] (TSC_DEADLINE) is not reported = by ``KVM_GET_SUPPORTED_CPUID`` It can be enabled if ``KVM_CAP_TSC_DEADLINE_TIMER`` is present and the ker= nel has enabled in-kernel emulation of the local APIC. =20 +CPU topology +~~~~~~~~~~~~ + +Several CPUID values include topology information for the host CPU: +0x0b and 0x1f for Intel systems, 0x8000001e for AMD systems. Different +versions of KVM return different values for this information and userspace +should not rely on it. Currently they return all zeroes. + +If userspace wishes to set up a guest topology, it should be careful that +the values of these three leaves differ for each CPU. In particular, +the APIC ID is found in EDX for all subleaves of 0x0b and 0x1f, and in EAX +for 0x8000001e; the latter also encodes the core id and node id in bits +7:0 of EBX and ECX respectively. + Obsolete ioctls and capabilities ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ =20 diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 0810e93cbedc..164bfb7e7a16 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -759,16 +759,22 @@ struct kvm_cpuid_array { int nent; }; =20 +static struct kvm_cpuid_entry2 *get_next_cpuid(struct kvm_cpuid_array *arr= ay) +{ + if (array->nent >=3D array->maxnent) + return NULL; + + return &array->entries[array->nent++]; +} + static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *arra= y, u32 function, u32 index) { - struct kvm_cpuid_entry2 *entry; + struct kvm_cpuid_entry2 *entry =3D get_next_cpuid(array); =20 - if (array->nent >=3D array->maxnent) + if (!entry) return NULL; =20 - entry =3D &array->entries[array->nent++]; - memset(entry, 0, sizeof(*entry)); entry->function =3D function; entry->index =3D index; @@ -945,22 +951,13 @@ static inline int __do_cpuid_func(struct kvm_cpuid_ar= ray *array, u32 function) entry->edx =3D edx.full; break; } - /* - * Per Intel's SDM, the 0x1f is a superset of 0xb, - * thus they can be handled by common code. - */ case 0x1f: case 0xb: /* - * Populate entries until the level type (ECX[15:8]) of the - * previous entry is zero. Note, CPUID EAX.{0x1f,0xb}.0 is - * the starting entry, filled by the primary do_host_cpuid(). + * No topology; a valid topology is indicated by the presence + * of subleaf 1. */ - for (i =3D 1; entry->ecx & 0xff00; ++i) { - entry =3D do_host_cpuid(array, function, i); - if (!entry) - goto out; - } + entry->eax =3D entry->ebx =3D entry->ecx =3D 0; break; case 0xd: { u64 permitted_xcr0 =3D kvm_caps.supported_xcr0 & xstate_get_guest_group_= perm(); @@ -1193,6 +1190,9 @@ static inline int __do_cpuid_func(struct kvm_cpuid_ar= ray *array, u32 function) entry->ebx =3D entry->ecx =3D entry->edx =3D 0; break; case 0x8000001e: + /* Do not return host topology information. */ + entry->eax =3D entry->ebx =3D entry->ecx =3D 0; + entry->edx =3D 0; /* reserved */ break; case 0x8000001F: if (!kvm_cpu_cap_has(X86_FEATURE_SEV)) { --=20 2.31.1