* [PATCH v2] perf/x86/amd/uncore: Avoid a false positive warning about snprintf truncation in amd_uncore_umc_ctx_init
@ 2024-11-05 8:52 Jean Delvare
2024-11-05 9:34 ` Sandipan Das
2024-11-11 10:58 ` [tip: perf/core] " tip-bot2 for Jean Delvare
0 siblings, 2 replies; 3+ messages in thread
From: Jean Delvare @ 2024-11-05 8:52 UTC (permalink / raw)
To: LKML; +Cc: Sandipan Das, Peter Zijlstra
Fix the following warning:
CC [M] arch/x86/events/amd/uncore.o
arch/x86/events/amd/uncore.c: In function ‘amd_uncore_umc_ctx_init’:
arch/x86/events/amd/uncore.c:951:52: warning: ‘%d’ directive output may be truncated writing between 1 and 10 bytes into a region of size 8 [-Wformat-truncation=]
snprintf(pmu->name, sizeof(pmu->name), "amd_umc_%d", index);
^~
arch/x86/events/amd/uncore.c:951:43: note: directive argument in the range [0, 2147483647]
snprintf(pmu->name, sizeof(pmu->name), "amd_umc_%d", index);
^~~~~~~~~~~~
arch/x86/events/amd/uncore.c:951:4: note: ‘snprintf’ output between 10 and 19 bytes into a destination of size 16
snprintf(pmu->name, sizeof(pmu->name), "amd_umc_%d", index);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
As far as I can see, there can't be more than UNCORE_GROUP_MAX (256)
groups and each group can't have more than 255 PMU, so the number
printed by this %d can't exceed 65279, that's only 5 digits and would
fit into the buffer. So it's a false positive warning. But we can
make the compiler happy by declaring index as a 16-bit number.
Signed-off-by: Jean Delvare <jdelvare@suse.de>
---
Changes in v2:
* Use the proper printf conversion specifier, to be on the safe side.
An alternative fix would be to extend UNCORE_NAME_LEN to 20, the
downside being an increased memory consumption. Depends whether we
expect UNCORE_GROUP_MAX to ever be increased, or groups to ever
support more than 255 PMU.
arch/x86/events/amd/uncore.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
--- linux-6.12-rc6.orig/arch/x86/events/amd/uncore.c
+++ linux-6.12-rc6/arch/x86/events/amd/uncore.c
@@ -916,7 +916,8 @@ int amd_uncore_umc_ctx_init(struct amd_u
u8 group_num_pmcs[UNCORE_GROUP_MAX] = { 0 };
union amd_uncore_info info;
struct amd_uncore_pmu *pmu;
- int index = 0, gid, i;
+ int gid, i;
+ u16 index = 0;
if (pmu_version < 2)
return 0;
@@ -948,7 +949,7 @@ int amd_uncore_umc_ctx_init(struct amd_u
for_each_set_bit(gid, gmask, UNCORE_GROUP_MAX) {
for (i = 0; i < group_num_pmus[gid]; i++) {
pmu = &uncore->pmus[index];
- snprintf(pmu->name, sizeof(pmu->name), "amd_umc_%d", index);
+ snprintf(pmu->name, sizeof(pmu->name), "amd_umc_%hu", index);
pmu->num_counters = group_num_pmcs[gid] / group_num_pmus[gid];
pmu->msr_base = MSR_F19H_UMC_PERF_CTL + i * pmu->num_counters * 2;
pmu->rdpmc_base = -1;
--
Jean Delvare
SUSE L3 Support
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] perf/x86/amd/uncore: Avoid a false positive warning about snprintf truncation in amd_uncore_umc_ctx_init
2024-11-05 8:52 [PATCH v2] perf/x86/amd/uncore: Avoid a false positive warning about snprintf truncation in amd_uncore_umc_ctx_init Jean Delvare
@ 2024-11-05 9:34 ` Sandipan Das
2024-11-11 10:58 ` [tip: perf/core] " tip-bot2 for Jean Delvare
1 sibling, 0 replies; 3+ messages in thread
From: Sandipan Das @ 2024-11-05 9:34 UTC (permalink / raw)
To: Jean Delvare; +Cc: Peter Zijlstra, LKML
On 11/5/2024 2:22 PM, Jean Delvare wrote:
> Fix the following warning:
> CC [M] arch/x86/events/amd/uncore.o
> arch/x86/events/amd/uncore.c: In function ‘amd_uncore_umc_ctx_init’:
> arch/x86/events/amd/uncore.c:951:52: warning: ‘%d’ directive output may be truncated writing between 1 and 10 bytes into a region of size 8 [-Wformat-truncation=]
> snprintf(pmu->name, sizeof(pmu->name), "amd_umc_%d", index);
> ^~
> arch/x86/events/amd/uncore.c:951:43: note: directive argument in the range [0, 2147483647]
> snprintf(pmu->name, sizeof(pmu->name), "amd_umc_%d", index);
> ^~~~~~~~~~~~
> arch/x86/events/amd/uncore.c:951:4: note: ‘snprintf’ output between 10 and 19 bytes into a destination of size 16
> snprintf(pmu->name, sizeof(pmu->name), "amd_umc_%d", index);
> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> As far as I can see, there can't be more than UNCORE_GROUP_MAX (256)
> groups and each group can't have more than 255 PMU, so the number
> printed by this %d can't exceed 65279, that's only 5 digits and would
> fit into the buffer. So it's a false positive warning. But we can
> make the compiler happy by declaring index as a 16-bit number.
>
> Signed-off-by: Jean Delvare <jdelvare@suse.de>
> ---
> Changes in v2:
> * Use the proper printf conversion specifier, to be on the safe side.
>
> An alternative fix would be to extend UNCORE_NAME_LEN to 20, the
> downside being an increased memory consumption. Depends whether we
> expect UNCORE_GROUP_MAX to ever be increased, or groups to ever
> support more than 255 PMU.
>
Its very unlikely for UNCORE_GROUP_MAX to change and the current
solution looks good to me.
Reviewed-by: Sandipan Das <sandipan.das@amd.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
* [tip: perf/core] perf/x86/amd/uncore: Avoid a false positive warning about snprintf truncation in amd_uncore_umc_ctx_init
2024-11-05 8:52 [PATCH v2] perf/x86/amd/uncore: Avoid a false positive warning about snprintf truncation in amd_uncore_umc_ctx_init Jean Delvare
2024-11-05 9:34 ` Sandipan Das
@ 2024-11-11 10:58 ` tip-bot2 for Jean Delvare
1 sibling, 0 replies; 3+ messages in thread
From: tip-bot2 for Jean Delvare @ 2024-11-11 10:58 UTC (permalink / raw)
To: linux-tip-commits
Cc: Jean Delvare, Peter Zijlstra (Intel), Sandipan Das, x86,
linux-kernel
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 2e71e8bc6f02275a67bcb882779ab6d21abc68c4
Gitweb: https://git.kernel.org/tip/2e71e8bc6f02275a67bcb882779ab6d21abc68c4
Author: Jean Delvare <jdelvare@suse.de>
AuthorDate: Tue, 05 Nov 2024 09:52:53 +01:00
Committer: Peter Zijlstra <peterz@infradead.org>
CommitterDate: Mon, 11 Nov 2024 11:49:47 +01:00
perf/x86/amd/uncore: Avoid a false positive warning about snprintf truncation in amd_uncore_umc_ctx_init
Fix the following warning:
CC [M] arch/x86/events/amd/uncore.o
arch/x86/events/amd/uncore.c: In function ‘amd_uncore_umc_ctx_init’:
arch/x86/events/amd/uncore.c:951:52: warning: ‘%d’ directive output may be truncated writing between 1 and 10 bytes into a region of size 8 [-Wformat-truncation=]
snprintf(pmu->name, sizeof(pmu->name), "amd_umc_%d", index);
^~
arch/x86/events/amd/uncore.c:951:43: note: directive argument in the range [0, 2147483647]
snprintf(pmu->name, sizeof(pmu->name), "amd_umc_%d", index);
^~~~~~~~~~~~
arch/x86/events/amd/uncore.c:951:4: note: ‘snprintf’ output between 10 and 19 bytes into a destination of size 16
snprintf(pmu->name, sizeof(pmu->name), "amd_umc_%d", index);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
As far as I can see, there can't be more than UNCORE_GROUP_MAX (256)
groups and each group can't have more than 255 PMU, so the number
printed by this %d can't exceed 65279, that's only 5 digits and would
fit into the buffer. So it's a false positive warning. But we can
make the compiler happy by declaring index as a 16-bit number.
Signed-off-by: Jean Delvare <jdelvare@suse.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Sandipan Das <sandipan.das@amd.com>
Link: https://lore.kernel.org/r/20241105095253.18f34b4d@endymion.delvare
---
arch/x86/events/amd/uncore.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/arch/x86/events/amd/uncore.c b/arch/x86/events/amd/uncore.c
index 0bfde2e..49c26ce 100644
--- a/arch/x86/events/amd/uncore.c
+++ b/arch/x86/events/amd/uncore.c
@@ -916,7 +916,8 @@ int amd_uncore_umc_ctx_init(struct amd_uncore *uncore, unsigned int cpu)
u8 group_num_pmcs[UNCORE_GROUP_MAX] = { 0 };
union amd_uncore_info info;
struct amd_uncore_pmu *pmu;
- int index = 0, gid, i;
+ int gid, i;
+ u16 index = 0;
if (pmu_version < 2)
return 0;
@@ -948,7 +949,7 @@ int amd_uncore_umc_ctx_init(struct amd_uncore *uncore, unsigned int cpu)
for_each_set_bit(gid, gmask, UNCORE_GROUP_MAX) {
for (i = 0; i < group_num_pmus[gid]; i++) {
pmu = &uncore->pmus[index];
- snprintf(pmu->name, sizeof(pmu->name), "amd_umc_%d", index);
+ snprintf(pmu->name, sizeof(pmu->name), "amd_umc_%hu", index);
pmu->num_counters = group_num_pmcs[gid] / group_num_pmus[gid];
pmu->msr_base = MSR_F19H_UMC_PERF_CTL + i * pmu->num_counters * 2;
pmu->rdpmc_base = -1;
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-11-11 10:58 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-05 8:52 [PATCH v2] perf/x86/amd/uncore: Avoid a false positive warning about snprintf truncation in amd_uncore_umc_ctx_init Jean Delvare
2024-11-05 9:34 ` Sandipan Das
2024-11-11 10:58 ` [tip: perf/core] " tip-bot2 for Jean Delvare
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox