Skip to contentSkip to conversation

x86/apic: fix integer overflow on 10 bit left shift of cpu_khz

PATCH In review Colin King 3 participants 3 messages Jun 19, 2019 – Jun 22, 2019

Subsystems (2)
lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH v2 1/1 x86/apic: fix integer overflow on 10 bit left shift of cpu_khz

From: Colin Ian King <[email protected]>

The left shift of unsigned int cpu_khz will overflow for large values of cpu_khz, so cast it to a long long before shifting it to avoid overvlow. For example, this can happen when cpu_khz is 4194305 (just less than 4.2 GHz). Also wrap line to avoid checkpatch wide line warning.

Addresses-Coverity: ("Unintentional integer overflow")

Fixes 8c3ba8d04924 ("x86, apic: ack all pending irqs when crashed/on kexec")
---
 arch/x86/kernel/apic/apic.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kernel/apic/apic.c +2 -1
index 8956072f677d..31426126e5e0 100644
@@ -1464,7 +1464,8 @@ static void apic_pending_intr_clear(void)
 		if (queued) {
 			if (boot_cpu_has(X86_FEATURE_TSC) && cpu_khz) {
 				ntsc = rdtsc();
-				max_loops = (cpu_khz << 10) - (ntsc - tsc);
+				max_loops = ((long long)cpu_khz << 10) -
+					    (ntsc - tsc);
 			} else {
 				max_loops--;
 			}
Signature · 4 lines
V2: replace right with left in commit subject and message. Doh.

--
2.20.1
#1 lore raw
Quoted 33 lines · Colin King

From: Colin Ian King <[email protected]>

The left shift of unsigned int cpu_khz will overflow for large values of cpu_khz, so cast it to a long long before shifting it to avoid overvlow. For example, this can happen when cpu_khz is 4194305 (just less than 4.2 GHz). Also wrap line to avoid checkpatch wide line warning.

Addresses-Coverity: ("Unintentional integer overflow")

Fixes 8c3ba8d04924 ("x86, apic: ack all pending irqs when crashed/on kexec")
---
 arch/x86/kernel/apic/apic.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kernel/apic/apic.c +2 -1
index 8956072f677d..31426126e5e0 100644
@@ -1464,7 +1464,8 @@ static void apic_pending_intr_clear(void)
 		if (queued) {
 			if (boot_cpu_has(X86_FEATURE_TSC) && cpu_khz) {
 				ntsc = rdtsc();
-				max_loops = (cpu_khz << 10) - (ntsc - tsc);
+				max_loops = ((long long)cpu_khz << 10) -
+					    (ntsc - tsc);
 			} else {
 				max_loops--;
 			}
Signature · 1 line
V2: replace right with left in commit subject and message. Doh.

Uh, why are you putting the v2 explanation here instead of between the --- cut off and the diffstat/

regards,
dan carpenter

#2 lore raw

[tip:x86/urgent] x86/apic: Fix integer overflow on 10 bit left shift of cpu_khz

In reply to Colin King
x86/apic: Fix integer overflow on 10 bit left shift of cpu_khz
Commit-ID:  ea136a112d89bade596314a1ae49f748902f4727
Gitweb:     https://git.kernel.org/tip/ea136a112d89bade596314a1ae49f748902f4727
Author:     Colin Ian King <[email protected]>
AuthorDate: Wed, 19 Jun 2019 19:14:46 +0100
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Sat, 22 Jun 2019 11:59:31 +0200

x86/apic: Fix integer overflow on 10 bit left shift of cpu_khz

The left shift of unsigned int cpu_khz will overflow for large values of cpu_khz, so cast it to a long long before shifting it to avoid overvlow. For example, this can happen when cpu_khz is 4194305, i.e. ~4.2 GHz.

Addresses-Coverity: ("Unintentional integer overflow")

Fixes 8c3ba8d04924 ("x86, apic: ack all pending irqs when crashed/on kexec")
Cc Borislav Petkov <[email protected]>
Cc "H . Peter Anvin" <[email protected]>
---
 arch/x86/kernel/apic/apic.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kernel/apic/apic.c +2 -1
index 177aa8ef2afa..85be316665b4 100644
@@ -1464,7 +1464,8 @@ static void apic_pending_intr_clear(void)
 		if (queued) {
 			if (boot_cpu_has(X86_FEATURE_TSC) && cpu_khz) {
 				ntsc = rdtsc();
-				max_loops = (cpu_khz << 10) - (ntsc - tsc);
+				max_loops = (long long)cpu_khz << 10;
+				max_loops -= ntsc - tsc;
 			} else {
 				max_loops--;
 			}