git: 7a068100b4b5 - stable/14 - x86: Guard clock frequency against a divide by 0
Alexander Ziaee <[email protected]>
| Newsgroups | gmane.os.freebsd.devel.cvs.src |
|---|---|
| Message-ID | <6a750b52.1f539.2da7e999__8623.27088122896$1786055665$gmane$org@gitrepo.freebsd.org> |
The branch stable/14 has been updated by ziaee: URL: https://cgit.FreeBSD.org/src/commit/?id=7a068100b4b57ef1db4cec7f3c35e5311617bb85 commit 7a068100b4b57ef1db4cec7f3c35e5311617bb85 Author: Matt Delco <[email protected]> AuthorDate: 2026-03-26 17:22:54 +0000 Commit: Alexander Ziaee <[email protected]> CommitDate: 2026-08-06 22:25:02 +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 <[email protected]> Co-authored-by: Jim Mattson <[email protected]> Suggested by: jrtc27 (split out this part) Reviewed by: imp, obiwac, olce Differential Revision: https://reviews.freebsd.org/D56056 (cherry picked from commit c505fc1468849150f48484b225b6476d8316de57) --- 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 9a188a922fad..dd19c2a46dbb 100644 --- a/sys/x86/x86/cpu_machdep.c +++ b/sys/x86/x86/cpu_machdep.c @@ -426,6 +426,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); @@ -461,6 +462,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 { @@ -471,6 +477,7 @@ cpu_est_clockrate(int cpu_id, uint64_t *rate) *rate = (tsc2 - tsc1) * 1000; } +err: #ifdef SMP if (smp_cpus > 1) { thread_lock(curthread); @@ -479,7 +486,7 @@ cpu_est_clockrate(int cpu_id, uint64_t *rate) } #endif - return (0); + return (error); } /*