Skip to contentSkip to conversation

x86/apic: fix frequency in apic=verbose log output

PATCH Applied Julian Stecklina 2 participants 2 messages Oct 30, 2021 – Nov 7, 2025 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/apic: fix frequency in apic=verbose log output

From: Julian Stecklina <[email protected]>

When apic=verbose is specified, the LAPIC timer calibration prints its results to the console. While debugging virtualization code, we noticed that the CPU and bus frequencies are printed incorrectly.

Specifically, for a 1.7 GHz CPU with 1 GHz bus frequency and HZ=1000, the log includes a superfluous 0 after the period:

..... calibration result: 999978
..... CPU clock speed is 1696.0783 MHz.
..... host bus clock speed is 999.0978 MHz.

Looking at the code, this only worked as intended for HZ=100. After the fix, we now see the correct frequency in the log:

..... calibration result: 999828
..... CPU clock speed is 1696.507 MHz.
..... host bus clock speed is 999.828 MHz.

There is no functional change to the LAPIC calibration here, beyond the printf changes.

---
 arch/x86/kernel/apic/apic.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
arch/x86/kernel/apic/apic.c +8 -6
index b70344bf6600..925becaac12a 100644
@@ -198,6 +198,7 @@ static struct resource lapic_resource = {
 	.flags = IORESOURCE_MEM | IORESOURCE_BUSY,
 };
 
+/* Measured in ticks per HZ. */
 unsigned int lapic_timer_period = 0;
 
 static void apic_pm_activate(void);
@@ -835,6 +836,7 @@ static int __init calibrate_APIC_clock(void)
 	unsigned long jif_start;
 	unsigned long deltaj;
 	long delta, deltatsc;
+	long tsc_khz, bus_khz;
 	int pm_referenced = 0;
 
 	if (boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER))
@@ -937,16 +939,16 @@ static int __init calibrate_APIC_clock(void)
 		    lapic_timer_period);
 
 	if (boot_cpu_has(X86_FEATURE_TSC)) {
+		tsc_khz = (deltatsc * HZ) / (1000 * LAPIC_CAL_LOOPS);
 		apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
-			    "%ld.%04ld MHz.\n",
-			    (deltatsc / LAPIC_CAL_LOOPS) / (1000000 / HZ),
-			    (deltatsc / LAPIC_CAL_LOOPS) % (1000000 / HZ));
+			    "%ld.%03ld MHz.\n",
+			    tsc_khz / 1000, tsc_khz % 1000);
 	}
 
+	bus_khz = (long)lapic_timer_period * HZ / 1000;
 	apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
-		    "%u.%04u MHz.\n",
-		    lapic_timer_period / (1000000 / HZ),
-		    lapic_timer_period % (1000000 / HZ));
+		    "%ld.%03ld MHz.\n",
+		    bus_khz / 1000, bus_khz % 1000);
 
 	/*
 	 * Do a sanity check on the APIC calibration result
Signature · 1 line
2.31.1
#1 lore raw

[tip: x86/apic] x86/apic: Fix frequency in apic=verbose log output

The following commit has been merged into the x86/apic branch of tip:

The following commit has been merged into the x86/apic branch of tip:

Commit-ID:     ed4f9638d905a97cebd49ecea85cc9b706b73761
Gitweb:        https://git.kernel.org/tip/ed4f9638d905a97cebd49ecea85cc9b706b73761
Author:        Julian Stecklina <[email protected]>
AuthorDate:    Sat, 30 Oct 2021 16:21:48 +02:00
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Fri, 07 Nov 2025 17:48:14 +01:00

x86/apic: Fix frequency in apic=verbose log output

When apic=verbose is specified, the LAPIC timer calibration prints its results to the console. At least while debugging virtualization code, the CPU and bus frequencies are printed incorrectly.

Specifically, for a 1.7 GHz CPU with 1 GHz bus frequency and HZ=1000, the log includes a superfluous 0 after the period:

..... calibration result: 999978
..... CPU clock speed is 1696.0783 MHz.
..... host bus clock speed is 999.0978 MHz.

Looking at the code, this only worked as intended for HZ=100. After the fix, the correct frequency is printed:

..... calibration result: 999828
..... CPU clock speed is 1696.507 MHz.
..... host bus clock speed is 999.828 MHz.

There is no functional change to the LAPIC calibration here, beyond the printing format changes.

[ bp: - Massage commit message
- Figures it should apply this patch about ~4 years later
- Massage it into the current code ]

---
 arch/x86/kernel/apic/apic.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
arch/x86/kernel/apic/apic.c +9 -6
index 680d305..ca1c8b7 100644
@@ -173,6 +173,7 @@ static struct resource lapic_resource = {
 	.flags = IORESOURCE_MEM | IORESOURCE_BUSY,
 };
 
+/* Measured in ticks per HZ. */
 unsigned int lapic_timer_period = 0;
 
 static void apic_pm_activate(void);
@@ -792,6 +793,7 @@ static int __init calibrate_APIC_clock(void)
 {
 	struct clock_event_device *levt = this_cpu_ptr(&lapic_events);
 	u64 tsc_perj = 0, tsc_start = 0;
+	long delta_tsc_khz, bus_khz;
 	unsigned long jif_start;
 	unsigned long deltaj;
 	long delta, deltatsc;
@@ -894,14 +896,15 @@ static int __init calibrate_APIC_clock(void)
 	apic_pr_verbose("..... calibration result: %u\n", lapic_timer_period);
 
 	if (boot_cpu_has(X86_FEATURE_TSC)) {
-		apic_pr_verbose("..... CPU clock speed is %ld.%04ld MHz.\n",
-				(deltatsc / LAPIC_CAL_LOOPS) / (1000000 / HZ),
-				(deltatsc / LAPIC_CAL_LOOPS) % (1000000 / HZ));
+		delta_tsc_khz = (deltatsc * HZ) / (1000 * LAPIC_CAL_LOOPS);
+
+		apic_pr_verbose("..... CPU clock speed is %ld.%03ld MHz.\n",
+				delta_tsc_khz / 1000, delta_tsc_khz % 1000);
 	}
 
-	apic_pr_verbose("..... host bus clock speed is %u.%04u MHz.\n",
-			lapic_timer_period / (1000000 / HZ),
-			lapic_timer_period % (1000000 / HZ));
+	bus_khz = (long)lapic_timer_period * HZ / 1000;
+	apic_pr_verbose("..... host bus clock speed is %ld.%03ld MHz.\n",
+			bus_khz / 1000, bus_khz % 1000);
 
 	/*
 	 * Do a sanity check on the APIC calibration result