Re: [PATCH 4/7] virt: steal_governor: serialize sampling against CPU hotplug
From: Yury Norov
Date: Sun Nov 22 2026 - 11:21:38 EST
On Sun, Nov 22, 2026 at 09:24:09PM +0530, Shrikanth Hegde wrote:
> Hi Yury,
>
> On 11/20/26 9:05 PM, Yury Norov wrote:
> > On Fri, Nov 20, 2026 at 02:08:59PM +0530, Shrikanth Hegde wrote:
> > >
> > >
> > > On 11/19/26 10:20 PM, Yury Norov wrote:
> > > > On Thu, Nov 19, 2026 at 11:52:19AM +0530, Shrikanth Hegde wrote:
> > > > >
> > > > >
> > > > > On 11/14/26 12:12 AM, Yury Norov wrote:
> > > > > > Steal governor is not safe against a concurrent CPU hotplug operation
> > > > > > because it can change active and preferred cpumasks during the governor
> > > > > > sampling.
> > > > > >
> > > > > > To address the race, the governor would take the cpus_write_lock(). But
> > > > > > that may be impractical for the loadable module.
> > > > > >
> > > > > > Instead, implement the following locking policy:
> > > > > >
> > > > > > - if the code takes cpus_write_lock(), it's enough to write-access
> > > > > > the preferred cpumask;
> > > > > > - if the code takes cpus_read_lock(), it must also take
> > > > > > cpu_preferred_mutex in order to modify the preferred mask.
> > > > > >
> > > > > > The core scheduler and hotplug paths already modify the preferred cpumask
> > > > > > under the cpus_write_lock(), so no change in core logic is required.
> > > > > >
> > > > > > This provides a common update protocol for multiple policy drivers
> > > > > > without requiring writers to take the broader CPU hotplug write lock.
> > > > > >
> > > > >
> > > > > This is also necessary if there are multiple writers outside of core hotplug No?
> > > > > So should it go with the write interface series?
> > > >
> > > > The race is real, and it's already there, so this is the fix for an
> > > > existing functionality.
> > >
> > > At current, if there is hotplug, core takes cpus_write_lock to modify
> > > active/preferred which makes governor to wait.
> > >
> > > If the governor is modifying the preferred mask it is taking the read lock
> > > which makes the hotplug core to wait before modifying active/preferred masks.
> > >
> > > And there are no other infra modifying the preferred mask.
> > > Could you please explain where is race currently?
> >
> > Initially, both masks contain CPUs {0,1}. No cpus_read_lock() held.
> > Steal ratio is within thresholds, so neither update helper runs.
> >
> > Governor worker CPU hotplug
> > --------------- -----------
> > preferred_cpus_valid():
> > cpumask_empty(cpu_preferred_mask)
> > -> false
> >
> > cpumask_subset(cpu_preferred_mask,
> > cpu_active_mask):
> > load preferred word -> {0,1}
> >
> > cpus_write_lock()
> > sched_cpu_deactivate(1):
> > set_cpu_preferred(1, false)
> > /* preferred={0} */
> > set_cpu_active(1, false)
> > /* active={0} */
> > cpus_write_unlock()
> >
> > load active word -> {0}
> > /* {0,1} is not a subset of {0} */
> > return false
> >
> > /* Useless and misleading */
> > restore_preferred_to_active();
> >
> > /* Skips schedule_delayed_work(). */
> > return
>
> To address this, then we need only this right?
>
> diff --git a/drivers/virt/steal_governor.c b/drivers/virt/steal_governor.c
> index 81e5089ba824..6a49ca55fc57 100644
> --- a/drivers/virt/steal_governor.c
> +++ b/drivers/virt/steal_governor.c
> @@ -202,6 +202,7 @@ static void increase_preferred_cpus(void)
> static bool preferred_cpus_valid(void)
> {
> + guard(cpus_read_lock)();
> if (cpumask_empty(cpu_preferred_mask)) {
> pr_err("empty preferred mask. stopping\n");
> return false;
I just show you the race I've seen. The root problem is that the
governor loop is not protected itself. Even if you make each function
within the loop protected, it doesn't protect it as a whole.