Skip to contentSkip to conversation

perf/x86/amd: fix potential integer overflow on shift of a int

PATCH Applied Colin Ian King 4 participants 4 messages Dec 2, 2022 – Dec 27, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf/x86/amd: fix potential integer overflow on shift of a int

The left shift of int 32 bit integer constant 1 is evaluated using 32 bit arithmetic and then passed as a 64 bit function argument. In the case where i is 32 or more this can lead to an overflow. Avoid this by shifting using the BIT_ULL macro instead.

Fixes 471af006a747 ("perf/x86/amd: Constrain Large Increment per Cycle events")
---
 arch/x86/events/amd/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/amd/core.c +1 -1
index d6f3703e4119..4386b10682ce 100644
@@ -1387,7 +1387,7 @@ static int __init amd_core_pmu_init(void)
 		 * numbered counter following it.
 		 */
 		for (i = 0; i < x86_pmu.num_counters - 1; i += 2)
-			even_ctr_mask |= 1 << i;
+			even_ctr_mask |= BIT_ULL(i);
 
 		pair_constraint = (struct event_constraint)
 				    __EVENT_CONSTRAINT(0, even_ctr_mask, 0,
Signature · 1 line
2.38.1
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Colin Ian King

The left shift of int 32 bit integer constant 1 is evaluated using 32 bit arithmetic and then passed as a 64 bit function argument. In the case where i is 32 or more this can lead to an overflow. Avoid this by shifting using the BIT_ULL macro instead.

Fixes 471af006a747 ("perf/x86/amd: Constrain Large Increment per Cycle events")

Thanks,
Ian

Quoted 20 lines
---
 arch/x86/events/amd/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/amd/core.c +1 -2
index d6f3703e4119..4386b10682ce 100644
@@ -1387,7 +1387,7 @@ static int __init amd_core_pmu_init(void)
                 * numbered counter following it.
                 */
                for (i = 0; i < x86_pmu.num_counters - 1; i += 2)
-                       even_ctr_mask |= 1 << i;
+                       even_ctr_mask |= BIT_ULL(i);
 
                pair_constraint = (struct event_constraint)
                                    __EVENT_CONSTRAINT(0, even_ctr_mask, 0,
--

2.38.1

Acked-by
#2 lore raw
Quoted 11 lines · Ian Rogers
Quoted 8 lines · Colin Ian King

The left shift of int 32 bit integer constant 1 is evaluated using 32 bit arithmetic and then passed as a 64 bit function argument. In the case where i is 32 or more this can lead to an overflow. Avoid this by shifting using the BIT_ULL macro instead.

Fixes 471af006a747 ("perf/x86/amd: Constrain Large Increment per Cycle events")

Thanks,

Kim

merge bot Applied
#3 lore raw
In reply to Colin Ian King
The following commit has been merged into the perf/urgent branch of tip:

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

Commit-ID:     08245672cdc6505550d1a5020603b0a8d4a6dcc7
Gitweb:        https://git.kernel.org/tip/08245672cdc6505550d1a5020603b0a8d4a6dcc7
Author:        Colin Ian King <[email protected]>
AuthorDate:    Fri, 02 Dec 2022 13:51:49 
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Tue, 27 Dec 2022 12:44:00 +01:00

perf/x86/amd: fix potential integer overflow on shift of a int

The left shift of int 32 bit integer constant 1 is evaluated using 32 bit arithmetic and then passed as a 64 bit function argument. In the case where i is 32 or more this can lead to an overflow. Avoid this by shifting using the BIT_ULL macro instead.

Fixes 471af006a747 ("perf/x86/amd: Constrain Large Increment per Cycle events")
---
 arch/x86/events/amd/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/amd/core.c +1 -1
index d6f3703..4386b10 100644
@@ -1387,7 +1387,7 @@ static int __init amd_core_pmu_init(void)
 		 * numbered counter following it.
 		 */
 		for (i = 0; i < x86_pmu.num_counters - 1; i += 2)
-			even_ctr_mask |= 1 << i;
+			even_ctr_mask |= BIT_ULL(i);
 
 		pair_constraint = (struct event_constraint)
 				    __EVENT_CONSTRAINT(0, even_ctr_mask, 0,