Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 26 Mar 2026 19:03:36 +0000
From:      Alexander Ziaee <ziaee@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Cc:        Matt Delco <delco@google.com>
Subject:   git: c505fc146884 - main - x86: Guard clock frequency against a divide by 0
Message-ID:  <69c58308.195e1.1d1363de@gitrepo.freebsd.org>

index | next in thread | raw e-mail

The branch main has been updated by ziaee:

URL: https://cgit.FreeBSD.org/src/commit/?id=c505fc1468849150f48484b225b6476d8316de57

commit c505fc1468849150f48484b225b6476d8316de57
Author:     Matt Delco <delco@google.com>
AuthorDate: 2026-03-26 17:22:54 +0000
Commit:     Alexander Ziaee <ziaee@FreeBSD.org>
CommitDate: 2026-03-26 19:02:06 +0000

    x86: Guard clock frequency against a divide by 0
    
    We may be running in a Virtual Machine which may not fully support
    hardware performance counters. If the MPERF counter somehow ends up
    at zero, return an error and fail gracefully instead of panicking.
    This patch is part of Google Cloud Engine (GCE) C4-LSSD turnup.
    
    Sponsored by:           Google
    Tested by:              NetApp (previous)
    PR:                     292808
    MFC after:              3 days
    Co-authored-by:         Aymeric Wibo <obiwac@google.com>
    Co-authored-by:         Jim Mattson <jmattson@google.com>
    Suggested by:           jrtc27 (split out this part)
    Reviewed by:            imp, obiwac, olce
    Differential Revision:  https://reviews.freebsd.org/D56056
---
 sys/x86/x86/cpu_machdep.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/sys/x86/x86/cpu_machdep.c b/sys/x86/x86/cpu_machdep.c
index 5483fbd6dd4e..775fee97d3cd 100644
--- a/sys/x86/x86/cpu_machdep.c
+++ b/sys/x86/x86/cpu_machdep.c
@@ -425,6 +425,7 @@ cpu_est_clockrate(int cpu_id, uint64_t *rate)
 	uint64_t tsc1, tsc2;
 	uint64_t acnt, mcnt, perf;
 	register_t reg;
+	int error = 0;
 
 	if (pcpu_find(cpu_id) == NULL || rate == NULL)
 		return (EINVAL);
@@ -460,6 +461,11 @@ cpu_est_clockrate(int cpu_id, uint64_t *rate)
 		acnt = rdmsr(MSR_APERF);
 		tsc2 = rdtsc();
 		intr_restore(reg);
+		if (mcnt == 0) {
+			tsc_perf_stat = 0;
+			error = EOPNOTSUPP;
+			goto err;
+		}
 		perf = 1000 * acnt / mcnt;
 		*rate = (tsc2 - tsc1) * perf;
 	} else {
@@ -470,6 +476,7 @@ cpu_est_clockrate(int cpu_id, uint64_t *rate)
 		*rate = (tsc2 - tsc1) * 1000;
 	}
 
+err:
 #ifdef SMP
 	if (smp_cpus > 1) {
 		thread_lock(curthread);
@@ -478,7 +485,7 @@ cpu_est_clockrate(int cpu_id, uint64_t *rate)
 	}
 #endif
 
-	return (0);
+	return (error);
 }
 
 /*


home | help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?69c58308.195e1.1d1363de>