Re: [PATCH v6 19/23] virt/steal_monitor: Provide default method to get num of CPUs for steal ratio
From: Yury Norov
Date: Mon Jul 06 2026 - 16:22:26 EST
On Wed, Jul 01, 2026 at 07:46:50PM +0530, Shrikanth Hegde wrote:
> This method informs the steal_monitor core, how many CPUs it needs to
> consider for steal ratio calculations.
> steal_ratio = (delta_steal * 100 * 100) / (delta_ns * number_of_cpus);
>
> Default method returns number of Active CPUs since it calculates steal
> time across active CPUs.
>
> Signed-off-by: Shrikanth Hegde <sshegde@xxxxxxxxxxxxx>
> ---
> v5->v6:
> - Add cpus_read_lock() for hotplug safety.
>
> drivers/virt/steal_monitor/defaults.c | 11 +++++++++++
> drivers/virt/steal_monitor/sm_core.h | 1 +
> 2 files changed, 12 insertions(+)
>
> diff --git a/drivers/virt/steal_monitor/defaults.c b/drivers/virt/steal_monitor/defaults.c
> index 4e2e5b233948..70dcfb1ce4cb 100644
> --- a/drivers/virt/steal_monitor/defaults.c
> +++ b/drivers/virt/steal_monitor/defaults.c
> @@ -27,6 +27,17 @@ u64 __weak get_system_steal_time(void)
> return total_steal;
> }
>
> +/*
> + * Return number of CPUs to consider for steal ratio calculation
> + *
> + * Default returns number of active CPUs.
> + */
> +unsigned int __weak get_num_cpus_steal_ratio(void)
> +{
> + guard(cpus_read_lock)();
> + return num_active_cpus();
> +}
How is this lock supposed to work? When you exit the scope, you can be
interrupted and your caller will get outdated information.
The other callers of num_active_cpus() don't bother taking the lock.
> +
> /*
> * Default implementation of decrementing the preferred CPUs based on steal
> * time. This is simple logic and decrease the preferred CPUs by 1 core.
> diff --git a/drivers/virt/steal_monitor/sm_core.h b/drivers/virt/steal_monitor/sm_core.h
> index 030f6236c38e..794d3be04248 100644
> --- a/drivers/virt/steal_monitor/sm_core.h
> +++ b/drivers/virt/steal_monitor/sm_core.h
> @@ -26,6 +26,7 @@ struct steal_monitor {
> extern struct steal_monitor sm_core_ctx;
>
> u64 get_system_steal_time(void);
> +unsigned int get_num_cpus_steal_ratio(void);
> void increase_preferred_cpus(struct steal_monitor *ctx);
> void decrease_preferred_cpus(struct steal_monitor *ctx);
>
> --
> 2.47.3