From: Niko Nikolov <nikolay.niko.nikolov@gmail.com>
To: ray.huang@amd.com, gautham.shenoy@amd.com, mario.limonciello@amd.com
Cc: perry.yuan@amd.com, linux-pm@vger.kernel.org,
linux-kernel@vger.kernel.org, shuah@kernel.org,
Niko Nikolov <nikolay.niko.nikolov@gmail.com>
Subject: [PATCH] x86/amd/power: Replace do_div() with div64_u64()
Date: Thu, 24 Jul 2025 14:21:05 -0700 [thread overview]
Message-ID: <20250724212221.9964-1-nikolay.niko.nikolov@gmail.com> (raw)
do_div() divides 64-by-32, and can risk truncation if divisor exceeds 32 bits.
Use div64_u64() for full 64/64-bit division as recommended, resolving static analysis warnings.
Signed-off-by: Niko Nikolov <nikolay.niko.nikolov@gmail.com>
---
arch/x86/events/amd/power.c | 49 ++++++++++++++++++-------------------
1 file changed, 24 insertions(+), 25 deletions(-)
diff --git a/arch/x86/events/amd/power.c b/arch/x86/events/amd/power.c
index dad42790cf7d..35eff3383660 100644
--- a/arch/x86/events/amd/power.c
+++ b/arch/x86/events/amd/power.c
@@ -15,12 +15,12 @@
#include "../perf_event.h"
/* Event code: LSB 8 bits, passed in attr->config any other bit is reserved. */
-#define AMD_POWER_EVENT_MASK 0xFFULL
+#define AMD_POWER_EVENT_MASK 0xFFULL
/*
* Accumulated power status counters.
*/
-#define AMD_POWER_EVENTSEL_PKG 1
+#define AMD_POWER_EVENTSEL_PKG 1
/*
* The ratio of compute unit power accumulator sample period to the
@@ -65,7 +65,7 @@ static void event_update(struct perf_event *event)
delta *= cpu_pwr_sample_ratio * 1000;
tdelta = new_ptsc - prev_ptsc;
- do_div(delta, tdelta);
+ div64_u64(delta, tdelta);
local64_add(delta, &event->count);
}
@@ -144,8 +144,8 @@ static void pmu_event_read(struct perf_event *event)
event_update(event);
}
-static ssize_t
-get_attr_cpumask(struct device *dev, struct device_attribute *attr, char *buf)
+static ssize_t get_attr_cpumask(struct device *dev,
+ struct device_attribute *attr, char *buf)
{
return cpumap_print_to_pagebuf(true, buf, &cpu_mask);
}
@@ -165,12 +165,12 @@ static struct attribute_group pmu_attr_group = {
* Currently it only supports to report the power of each
* processor/package.
*/
-EVENT_ATTR_STR(power-pkg, power_pkg, "event=0x01");
+EVENT_ATTR_STR(power - pkg, power_pkg, "event=0x01");
-EVENT_ATTR_STR(power-pkg.unit, power_pkg_unit, "mWatts");
+EVENT_ATTR_STR(power - pkg.unit, power_pkg_unit, "mWatts");
/* Convert the count from micro-Watts to milli-Watts. */
-EVENT_ATTR_STR(power-pkg.scale, power_pkg_scale, "1.000000e-3");
+EVENT_ATTR_STR(power - pkg.scale, power_pkg_scale, "1.000000e-3");
static struct attribute *events_attr[] = {
EVENT_PTR(power_pkg),
@@ -180,8 +180,8 @@ static struct attribute *events_attr[] = {
};
static struct attribute_group pmu_events_group = {
- .name = "events",
- .attrs = events_attr,
+ .name = "events",
+ .attrs = events_attr,
};
PMU_FORMAT_ATTR(event, "config:0-7");
@@ -192,8 +192,8 @@ static struct attribute *formats_attr[] = {
};
static struct attribute_group pmu_format_group = {
- .name = "format",
- .attrs = formats_attr,
+ .name = "format",
+ .attrs = formats_attr,
};
static const struct attribute_group *attr_groups[] = {
@@ -204,17 +204,17 @@ static const struct attribute_group *attr_groups[] = {
};
static struct pmu pmu_class = {
- .attr_groups = attr_groups,
+ .attr_groups = attr_groups,
/* system-wide only */
- .task_ctx_nr = perf_invalid_context,
- .event_init = pmu_event_init,
- .add = pmu_event_add,
- .del = pmu_event_del,
- .start = pmu_event_start,
- .stop = pmu_event_stop,
- .read = pmu_event_read,
- .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
- .module = THIS_MODULE,
+ .task_ctx_nr = perf_invalid_context,
+ .event_init = pmu_event_init,
+ .add = pmu_event_add,
+ .del = pmu_event_del,
+ .start = pmu_event_start,
+ .stop = pmu_event_stop,
+ .read = pmu_event_read,
+ .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
+ .module = THIS_MODULE,
};
static int power_cpu_exit(unsigned int cpu)
@@ -278,10 +278,9 @@ static int __init amd_power_pmu_init(void)
return -ENODEV;
}
-
cpuhp_setup_state(CPUHP_AP_PERF_X86_AMD_POWER_ONLINE,
- "perf/x86/amd/power:online",
- power_cpu_init, power_cpu_exit);
+ "perf/x86/amd/power:online", power_cpu_init,
+ power_cpu_exit);
ret = perf_pmu_register(&pmu_class, "power", -1);
if (WARN_ON(ret)) {
--
2.50.1
Hi,
This patch replaces do_div() with div64_u64() in arch/x86/events/amd/power.c
to avoid potential truncation/overflow, and to conform to kernel arithmetic
best practices.
It builds cleanly for me, but I do not have AMD hardware available to test
functionality at runtime, so additional testing on AMD systems would be
appreciated.
Thanks,
Niko Nikolov
next reply other threads:[~2025-07-24 21:22 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-24 21:21 Niko Nikolov [this message]
2025-07-28 19:05 ` [PATCH] x86/amd/power: Replace do_div() with div64_u64() David Laight
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250724212221.9964-1-nikolay.niko.nikolov@gmail.com \
--to=nikolay.niko.nikolov@gmail.com \
--cc=gautham.shenoy@amd.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=mario.limonciello@amd.com \
--cc=perry.yuan@amd.com \
--cc=ray.huang@amd.com \
--cc=shuah@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).