From 9d47f021d32ffdff5caf984462d0e63dc62e6135 Mon Sep 17 00:00:00 2001 From: David Dai Date: Thu, 27 Jun 2024 12:18:34 -0700 Subject: [PATCH] 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 Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5664534 Reviewed-by: Frederick Mayle Reviewed-by: Daniel Verkamp --- base/src/sys/linux/mod.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/base/src/sys/linux/mod.rs b/base/src/sys/linux/mod.rs index f52398a33d..804d6e4265 100644 --- a/base/src/sys/linux/mod.rs +++ b/base/src/sys/linux/mod.rs @@ -614,11 +614,14 @@ pub fn logical_core_capacity(cpu_id: usize) -> Result { }); 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)