public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: EwanHai <ewandevelop@gmail.com>
To: "Zhao Liu" <zhao1.liu@intel.com>,
	"Daniel P . Berrangé" <berrange@redhat.com>,
	"Igor Mammedov" <imammedo@redhat.com>,
	"Eduardo Habkost" <eduardo@habkost.net>,
	"Marcel Apfelbaum" <marcel.apfelbaum@gmail.com>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Yanan Wang" <wangyanan55@huawei.com>,
	"Michael S . Tsirkin" <mst@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Richard Henderson" <richard.henderson@linaro.org>,
	"Eric Blake" <eblake@redhat.com>,
	"Markus Armbruster" <armbru@redhat.com>,
	"Marcelo Tosatti" <mtosatti@redhat.com>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	"Jonathan Cameron" <Jonathan.Cameron@huawei.com>,
	"Sia Jee Heng" <jeeheng.sia@starfivetech.com>,
	"Alireza Sanaee" <alireza.sanaee@huawei.com>,
	qemu-devel@nongnu.org, kvm@vger.kernel.org,
	qemu-riscv@nongnu.org, qemu-arm@nongnu.org,
	"Zhenyu Wang" <zhenyu.z.wang@intel.com>,
	"Dapeng Mi" <dapeng1.mi@linux.intel.com>
Cc: Yongwei Ma <yongwei.ma@intel.com>
Subject: Re: [PATCH v5 2/9] hw/core: Make CPU topology enumeration arch-agnostic
Date: Tue, 28 Apr 2026 17:43:25 +0800	[thread overview]
Message-ID: <4b66c381-09c5-496e-b95e-100d1ba20dc5@gmail.com> (raw)
In-Reply-To: <20241101083331.340178-3-zhao1.liu@intel.com>

Apologies for the noise, please ignore my previous reply, the corporate mailer
collapsed all the newlines. Resending in plain text below.

Zhao Liu wrote on 11/1/24 4:33 PM:
> Cache topology needs to be defined based on CPU topology levels. Thus,
> define CPU topology enumeration in qapi/machine.json to make it generic
> for all architectures.
> 
> To match the general topology naming style, rename CPU_TOPO_LEVEL_* to
> CPU_TOPOLOGY_LEVEL_*, and rename SMT and package levels to thread and
> socket.
> 
> Also, enumerate additional topology levels for non-i386 arches, and add
> a CPU_TOPOLOGY_LEVEL_DEFAULT to help future smp-cache object to work
> with compatibility requirement of arch-specific cache topology models.
> 
> Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
> Tested-by: Yongwei Ma <yongwei.ma@intel.com>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Acked-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---

...


> @@ -373,17 +373,17 @@ static void encode_topo_cpuid1f(CPUX86State *env, uint32_t count,
>      unsigned long level, base_level, next_level;
>      uint32_t num_threads_next_level, offset_next_level;
>  
> -    assert(count <= CPU_TOPO_LEVEL_PACKAGE);
> +    assert(count <= CPU_TOPOLOGY_LEVEL_SOCKET);
>  

Hi Zhao,

While booting Linux under TCG QEMU (-kernel + rootfs) with a CPU model that
enables cpuid_0x1f (e.g. Shijidadao-v2), I hit this assert: when the guest
queries CPUID.1F with a topology level greater than CPU_TOPOLOGY_LEVEL_SOCKET,
QEMU aborts. Under KVM the path isn't reached (CPUID is served from
KVM_SET_CPUID2), so this is TCG-only, which is probably why it hadn't been
caught earlier.

I'm not sure the assert is necessary: the code that follows already handles
the "input level out of range" case correctly. find_next_bit() returns SOCKET
once the bitmap is exhausted, the loop breaks with level = INVALID, and the
encoding path below produces the SDM-defined invalid-domain response (EAX=0,
EBX=0, ECX = count | INVALID_type, EDX = apic_id).

Per Intel SDM, CPUID Leaf 1FH does not constrain the input ECX value, so guest
software is free to query any sub-leaf index. Did the assert intentionally
protect an invariant I'm missing, or is it a leftover defensive check?

For reference, the KVM kernel side (arch/x86/kvm/cpuid.c kvm_cpuid()) has
explicitly handled out-of-range 0x1F subleaves this way since 2019:
    } else {
        *eax = *ebx = *ecx = *edx = 0;
        /*
         * When leaf 0BH or 1FH is defined, CL is pass-through
         * and EDX is always the x2APIC ID, even for undefined
         * subleaves. ...
         */
        if (function == 0xb || function == 0x1f) {
            entry = kvm_find_cpuid_entry_index(vcpu, function, 1);
            if (entry) {
                *ecx = index & 0xff;
                *edx = entry->edx;
            }
        }
    }
introduced in commit 43561123ab37 ("kvm: x86: Improve emulation of CPUID leaves
0BH and 1FH"). So KVM-accelerated guests already see the SDM-compliant response;
only TCG aborts.

Thanks,
Ewan


>      /*
>       * Find the No.(count + 1) topology level in avail_cpu_topo bitmap.
> -     * The search starts from bit 0 (CPU_TOPO_LEVEL_SMT).
> +     * The search starts from bit 0 (CPU_TOPOLOGY_LEVEL_THREAD).
>       */
> -    level = CPU_TOPO_LEVEL_SMT;
> +    level = CPU_TOPOLOGY_LEVEL_THREAD;
>      base_level = level;
>      for (int i = 0; i <= count; i++) {
>          level = find_next_bit(env->avail_cpu_topo,
> -                              CPU_TOPO_LEVEL_PACKAGE,
> +                              CPU_TOPOLOGY_LEVEL_SOCKET,
>                                base_level);
>  
>          /*
> @@ -391,20 +391,20 @@ static void encode_topo_cpuid1f(CPUX86State *env, uint32_t count,
>           * and it just encodes the invalid level (all fields are 0)
>           * into the last subleaf of 0x1f.
>           */
> -        if (level == CPU_TOPO_LEVEL_PACKAGE) {
> -            level = CPU_TOPO_LEVEL_INVALID;
> +        if (level == CPU_TOPOLOGY_LEVEL_SOCKET) {
> +            level = CPU_TOPOLOGY_LEVEL_INVALID;
>              break;
>          }
>          /* Search the next level. */
>          base_level = level + 1;
>      }
>  
> -    if (level == CPU_TOPO_LEVEL_INVALID) {
> +    if (level == CPU_TOPOLOGY_LEVEL_INVALID) {
>          num_threads_next_level = 0;
>          offset_next_level = 0;
>      } else {
>          next_level = find_next_bit(env->avail_cpu_topo,
> -                                   CPU_TOPO_LEVEL_PACKAGE,
> +                                   CPU_TOPOLOGY_LEVEL_SOCKET,
>                                     level + 1);
>          num_threads_next_level = num_threads_by_topo_level(topo_info,
>                                                             next_level);
...


  parent reply	other threads:[~2026-04-28  9:43 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-01  8:33 [PATCH v5 0/9] Introduce SMP Cache Topology Zhao Liu
2024-11-01  8:33 ` [PATCH v5 1/9] i386/cpu: Don't enumerate the "invalid" CPU topology level Zhao Liu
2024-11-01  8:33 ` [PATCH v5 2/9] hw/core: Make CPU topology enumeration arch-agnostic Zhao Liu
2026-04-28  8:42   ` Ewan Hai
2026-04-28  9:43   ` EwanHai [this message]
2024-11-01  8:33 ` [PATCH v5 3/9] qapi/qom: Define cache enumeration and properties for machine Zhao Liu
2024-11-01  8:33 ` [PATCH v5 4/9] hw/core: Check smp cache topology support " Zhao Liu
2024-11-01  8:33 ` [PATCH v5 5/9] hw/core: Add a helper to check the cache topology level Zhao Liu
2024-11-01  8:33 ` [PATCH v5 6/9] i386/cpu: Support thread and module level cache topology Zhao Liu
2024-11-01  8:33 ` [PATCH v5 7/9] i386/cpu: Update cache topology with machine's configuration Zhao Liu
2024-11-01  8:33 ` [PATCH v5 8/9] i386/pc: Support cache topology in -machine for PC machine Zhao Liu
2024-11-01  8:33 ` [PATCH v5 9/9] i386/cpu: add has_caches flag to check smp_cache configuration Zhao Liu
2024-11-05 22:54 ` [PATCH v5 0/9] Introduce SMP Cache Topology Philippe Mathieu-Daudé
2024-11-06  2:40   ` Zhao Liu

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=4b66c381-09c5-496e-b95e-100d1ba20dc5@gmail.com \
    --to=ewandevelop@gmail.com \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=alex.bennee@linaro.org \
    --cc=alireza.sanaee@huawei.com \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=eblake@redhat.com \
    --cc=eduardo@habkost.net \
    --cc=imammedo@redhat.com \
    --cc=jeeheng.sia@starfivetech.com \
    --cc=kvm@vger.kernel.org \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=mst@redhat.com \
    --cc=mtosatti@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@linaro.org \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-riscv@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=wangyanan55@huawei.com \
    --cc=yongwei.ma@intel.com \
    --cc=zhao1.liu@intel.com \
    --cc=zhenyu.z.wang@intel.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