Skip to contentSkip to conversation

perf/x86: Fix integer overflow when left shifting an integer more than 32 bits

PATCH Applied Colin King 3 participants 5 messages Apr 20, 2021 – Apr 23, 2021

Subsystems (3)
lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 perf/x86: Fix integer overflow when left shifting an integer more than 32 bits

From: Colin Ian King <[email protected]>

The 64 bit value read from MSR_ARCH_PERFMON_FIXED_CTR_CTRL is being bit-wise masked with the value (0x03 << i*4). However, the shifted value is evaluated using 32 bit arithmetic, so will overflow when i > 8. Fix this by making 0x03 a ULL so that the shift is performed using 64 bit arithmetic.

Addresses-Coverity: ("Unintentional integer overflow")

Fixes a5ebe0ba3dff ("perf/x86: Check all MSRs before passing hw check")
---
 arch/x86/events/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/core.c +1 -1
index bafd93c54ffa..59c665c8c2e9 100644
@@ -261,7 +261,7 @@ static bool check_hw_exists(void)
 		for (i = 0; i < x86_pmu.num_counters_fixed; i++) {
 			if (fixed_counter_disabled(i))
 				continue;
-			if (val & (0x03 << i*4)) {
+			if (val & (0x03ULL << i*4)) {
 				bios_fail = 1;
 				val_fail = val;
 				reg_fail = reg;
Signature · 1 line
2.30.2
maintainer
#1 lore raw
Quoted 9 lines · Colin King

From: Colin Ian King <[email protected]>

The 64 bit value read from MSR_ARCH_PERFMON_FIXED_CTR_CTRL is being bit-wise masked with the value (0x03 << i*4). However, the shifted value is evaluated using 32 bit arithmetic, so will overflow when i > 8. Fix this by making 0x03 a ULL so that the shift is performed using 64 bit arithmetic.

Addresses-Coverity: ("Unintentional integer overflow")

Strange tag that, also inaccurate, wide shifts are UB and don't behave consistently.

As is, we've not had hardware with that many fixed counters, but yes, worth fixing I suppose.

Quoted 22 lines
Fixes a5ebe0ba3dff ("perf/x86: Check all MSRs before passing hw check")
---
 arch/x86/events/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/core.c +1 -1
index bafd93c54ffa..59c665c8c2e9 100644
@@ -261,7 +261,7 @@ static bool check_hw_exists(void)
 		for (i = 0; i < x86_pmu.num_counters_fixed; i++) {
 			if (fixed_counter_disabled(i))
 				continue;
-			if (val & (0x03 << i*4)) {
+			if (val & (0x03ULL << i*4)) {
 				bios_fail = 1;
 				val_fail = val;
 				reg_fail = reg;
Signature · 1 line
2.30.2
maintainer
#2 lore raw
Quoted 9 lines · Peter Zijlstra
Quoted 8 lines · Colin King

From: Colin Ian King <[email protected]>

The 64 bit value read from MSR_ARCH_PERFMON_FIXED_CTR_CTRL is being bit-wise masked with the value (0x03 << i*4). However, the shifted value is evaluated using 32 bit arithmetic, so will overflow when i > 8. Fix this by making 0x03 a ULL so that the shift is performed using 64 bit arithmetic.

Quoted 7 lines
Quoted 1 line

Addresses-Coverity: ("Unintentional integer overflow")

Strange tag that, also inaccurate, wide shifts are UB and don't behave consistently.

As is, we've not had hardware with that many fixed counters, but yes, worth fixing I suppose.

Patch now reads:

---
Subject: perf/x86: Allow for 8<num_fixed_counters<16
From: Colin Ian King <[email protected]>
Date: Tue, 20 Apr 2021 15:29:07 +0100

From: Colin Ian King <[email protected]>

The 64 bit value read from MSR_ARCH_PERFMON_FIXED_CTR_CTRL is being bit-wise masked with the value (0x03 << i*4). However, the shifted value is evaluated using 32 bit arithmetic, so will UB when i > 8. Fix this by making 0x03 a ULL so that the shift is performed using 64 bit arithmetic.

This makes the arithmetic internally consistent and preparers for the day when hardware provides 8<num_fixed_counters<16.

---
 arch/x86/events/core.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/core.c +1 -1
@@ -261,7 +261,7 @@ static bool check_hw_exists(void)
 		for (i = 0; i < x86_pmu.num_counters_fixed; i++) {
 			if (fixed_counter_disabled(i))
 				continue;
-			if (val & (0x03 << i*4)) {
+			if (val & (0x03ULL << i*4)) {
 				bios_fail = 1;
 				val_fail = val;
 				reg_fail = reg;
#3 lore raw
Quoted 27 lines · Peter Zijlstra
Quoted 9 lines · Peter Zijlstra
Quoted 8 lines · Colin King

From: Colin Ian King <[email protected]>

The 64 bit value read from MSR_ARCH_PERFMON_FIXED_CTR_CTRL is being bit-wise masked with the value (0x03 << i*4). However, the shifted value is evaluated using 32 bit arithmetic, so will overflow when i > 8. Fix this by making 0x03 a ULL so that the shift is performed using 64 bit arithmetic.

Quoted 7 lines
Quoted 1 line

Addresses-Coverity: ("Unintentional integer overflow")

Strange tag that, also inaccurate, wide shifts are UB and don't behave consistently.

As is, we've not had hardware with that many fixed counters, but yes, worth fixing I suppose.

Patch now reads:

---
Subject: perf/x86: Allow for 8<num_fixed_counters<16
From: Colin Ian King <[email protected]>
Date: Tue, 20 Apr 2021 15:29:07 +0100

From: Colin Ian King <[email protected]>

Quoted 8 lines

The 64 bit value read from MSR_ARCH_PERFMON_FIXED_CTR_CTRL is being bit-wise masked with the value (0x03 << i*4). However, the shifted value is evaluated using 32 bit arithmetic, so will UB when i > 8. Fix this by making 0x03 a ULL so that the shift is performed using 64 bit arithmetic.

This makes the arithmetic internally consistent and preparers for the day when hardware provides 8<num_fixed_counters<16.

Yep, that's good. Thanks.

Quoted 20 lines
---
 arch/x86/events/core.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/core.c +1 -1
@@ -261,7 +261,7 @@ static bool check_hw_exists(void)
 		for (i = 0; i < x86_pmu.num_counters_fixed; i++) {
 			if (fixed_counter_disabled(i))
 				continue;
-			if (val & (0x03 << i*4)) {
+			if (val & (0x03ULL << i*4)) {
 				bios_fail = 1;
 				val_fail = val;
 				reg_fail = reg;
merge bot Applied
#4 lore raw

[tip: perf/core] perf/x86: Allow for 8<num_fixed_counters<16

In reply to Colin King
The following commit has been merged into the perf/core branch of tip:

The following commit has been merged into the perf/core branch of tip:

Commit-ID:     32d35c4a96ec79446f0d7be308a6eb248b507a0b
Gitweb:        https://git.kernel.org/tip/32d35c4a96ec79446f0d7be308a6eb248b507a0b
Author:        Colin Ian King <[email protected]>
AuthorDate:    Tue, 20 Apr 2021 15:29:07 +01:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Fri, 23 Apr 2021 09:03:15 +02:00

perf/x86: Allow for 8<num_fixed_counters<16

The 64 bit value read from MSR_ARCH_PERFMON_FIXED_CTR_CTRL is being bit-wise masked with the value (0x03 << i*4). However, the shifted value is evaluated using 32 bit arithmetic, so will UB when i > 8. Fix this by making 0x03 a ULL so that the shift is performed using 64 bit arithmetic.

This makes the arithmetic internally consistent and preparers for the day when hardware provides 8<num_fixed_counters<16.

---
 arch/x86/events/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/core.c +1 -1
index 3fe66b7..c7fcc8d 100644
@@ -278,7 +278,7 @@ bool check_hw_exists(struct pmu *pmu, int num_counters, int num_counters_fixed)
 		for (i = 0; i < num_counters_fixed; i++) {
 			if (fixed_counter_disabled(i, pmu))
 				continue;
-			if (val & (0x03 << i*4)) {
+			if (val & (0x03ULL << i*4)) {
 				bios_fail = 1;
 				val_fail = val;
 				reg_fail = reg;