Skip to content

Panic: division by zero in physicalCores() on Intel VMs with uneven core count #161

@skartikey

Description

@skartikey

Environment: Proxmox VM with 5 cores allocated (uneven number) from an 8-core Xeon host.

The Problem: In virtualized environments, especially with uneven core allocation, the CPUID instruction can return inconsistent or zero values that don't match real hardware behavior.

panic: runtime error: integer divide by zero

goroutine 1 [running]:
github.com/klauspost/cpuid/v2.physicalCores()
        github.com/klauspost/cpuid/v2@v2.2.10/cpuid.go:881 +0xef
github.com/klauspost/cpuid/v2.addInfo(0x11247c40, 0x42?)
        github.com/klauspost/cpuid/v2@v2.2.10/detect_x86.go:33 +0x1e5
github.com/klauspost/cpuid/v2.Detect()
        github.com/klauspost/cpuid/v2@v2.2.10/cpuid.go:376 +0x85
github.com/klauspost/cpuid/v2.init.0()
        github.com/klauspost/cpuid/v2@v2.2.10/cpuid.go:355 +0x9f

What's happening:

  1. Proxmox/KVM is virtualizing the CPU topology
  2. With 5 cores allocated (unusual number), the hypervisor's CPUID emulation returns problematic values
  3. Either threadsPerCore() returns 0, or logicalCores() returns 0
  4. physicalCores() does division without checking for zero

The specific bug location:

Looking at the code in physicalCores():

func physicalCores() int {
    v, _ := vendorID()
    switch v {
    case Intel:
        return logicalCores() / threadsPerCore()  // ← PANIC HERE
    case AMD, Hygon:
        lc := logicalCores()
        tpc := threadsPerCore()
        if lc > 0 && tpc > 0 {  // ← AMD has protection
            return lc / tpc
        }
        // ... fallback logic
    }
    return 0
}

Intel path has NO zero-check, while AMD does!

Related issues

resolves #16981

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions