linux: mod: Use u64 when normalizing capacity

For systems where CPU frequencies can exceed 4GHz(linux reports
frequency in khz, but muliplying it by SCHED_SCALE_CAPACITY or 1024 can
overflow u32). Use u64 to calculate normalized capacities instead.

BUG=b:348152361
TEST=tools/presubmit
Change-Id: Ica4ea2ea0dd15d2917d28581a929ead4a5681d2f
Signed-off-by: David Dai <davidai@google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5664534
Reviewed-by: Frederick Mayle <fmayle@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
David Dai 2024-06-27 12:18:34 -07:00 committed by crosvm LUCI
parent 6dd00ff093
commit 9d47f021d3

View file

@ -614,11 +614,14 @@ pub fn logical_core_capacity(cpu_id: usize) -> Result<u32> {
});
if let Ok(cpu_max_freqs) = cpu_max_freqs {
let largest_max_freq = cpu_max_freqs.iter().max().ok_or(Error::new(EINVAL))?;
let cpu_max_freq = cpu_max_freqs.get(cpu_id).ok_or(Error::new(EINVAL))?;
(cpu_capacity * largest_max_freq)
.checked_div(*cpu_max_freq)
.ok_or(Error::new(EINVAL))
let largest_max_freq = *cpu_max_freqs.iter().max().ok_or(Error::new(EINVAL))?;
let cpu_max_freq = *cpu_max_freqs.get(cpu_id).ok_or(Error::new(EINVAL))?;
let normalized_cpu_capacity = (u64::from(cpu_capacity) * u64::from(largest_max_freq))
.checked_div(u64::from(cpu_max_freq))
.ok_or(Error::new(EINVAL))?;
normalized_cpu_capacity
.try_into()
.map_err(|_| Error::new(EINVAL))
} else {
// cpu-freq is not enabled. Fall back to using the normalized capacity.
Ok(cpu_capacity)