Re: [PATCH v2 0/5] Introduce QPW for per-cpu operations (v2)

Frederic Weisbecker
2026-03-10T22:14:34Z

Le Tue, Mar 10, 2026 at 02:12:03PM -0300, Marcelo Tosatti a écrit :
> Hi Frederic,
>
> On Thu, Mar 05, 2026 at 05:55:12PM +0100, Frederic Weisbecker wrote:
> > Le Mon, Mar 02, 2026 at 12:49:45PM -0300, Marcelo Tosatti a écrit :
> > > The problem:
> > > Some places in the kernel implement a parallel programming strategy
> > > consisting on local_locks() for most of the work, and some rare remote
> > > operations are scheduled on target cpu. This keeps cache bouncing low since
> > > cacheline tends to be mostly local, and avoids the cost of locks in non-RT
> > > kernels, even though the very few remote operations will be expensive due
> > > to scheduling overhead.
> > >
> > > On the other hand, for RT workloads this can represent a problem: getting
> > > an important workload scheduled out to deal with remote requests is
> > > sure to introduce unexpected deadline misses.
> > >
> > > The idea:
> > > Currently with PREEMPT_RT=y, local_locks() become per-cpu spinlocks.
> > > In this case, instead of scheduling work on a remote cpu, it should
> > > be safe to grab that remote cpu's per-cpu spinlock and run the required
> > > work locally. That major cost, which is un/locking in every local function,
> > > already happens in PREEMPT_RT.
> > >
> > > Also, there is no need to worry about extra cache bouncing:
> > > The cacheline invalidation already happens due to schedule_work_on().
> > >
> > > This will avoid schedule_work_on(), and thus avoid scheduling-out an
> > > RT workload.
> > >
> > > Proposed solution:
> > > A new interface called Queue PerCPU Work (QPW), which should replace
> > > Work Queue in the above mentioned use case.
> > >
> > > If CONFIG_QPW=n this interfaces just wraps the current
> > > local_locks + WorkQueue behavior, so no expected change in runtime.
> > >
> > > If CONFIG_QPW=y, and qpw kernel boot option =1,
> > > queue_percpu_work_on(cpu,...) will lock that cpu's per-cpu structure
> > > and perform work on it locally. This is possible because on
> > > functions that can be used for performing remote work on remote
> > > per-cpu structures, the local_lock (which is already
> > > a this_cpu spinlock()), will be replaced by a qpw_spinlock(), which
> > > is able to get the per_cpu spinlock() for the cpu passed as parameter.
> >
> > So let me summarize what are the possible design solutions, on top of our discussions,
> > so we can compare:
> >
> > 1) Never queue remotely but always queue locally and execute on userspace
> > return via task work.
>
> How can you "queue locally" if the request is visible on a remote CPU?
>
> That is, the event which triggers the manipulation of data structures
> which need to be performed by the owner CPU (owner of the data
> structures) is triggered on a remote CPU.
>
> This is confusing.
>
> Can you also please give a practical example of such case ?

Right so in the case of LRU batching, it consists in always queue
locally as soon as there is something to do. Then no remote queueing
is necessary. Like here:

https://lwn.net/ml/all/20250703140717.25703-7-frederic@kernel.org/

>
> > Pros:
> > - Simple and easy to maintain.
> >
> > Cons:
> > - Need a case by case handling.
> >
> > - Might be suitable for full userspace applications but not for
> > some HPC usecases. In the best world MPI is fully implemented in
> > userspace but that doesn't appear to be the case.
> >
> > 2) Queue locally the workqueue right away
>
> Again, the event which triggers the manipulation of data structures
> by the owner CPU happens on a remote CPU.
> So how can you queue it locally ?

So that would be the same as above but instead of using task_work(), we
would force queue a workqueue locally. It's more agressive.

>
> > or do it remotely (if it's
> > really necessary) if the isolated CPU is in userspace, otherwise queue
> > it for execution on return to kernel. The work will be handled by preemption
> > to a worker or by a workqueue flush on return to userspace.
> >
> > Pros:
> > - The local queue handling is simple.
> >
> > Cons:
> > - The remote queue must synchronize with return to userspace and
> > eventually postpone to return to kernel if the target is in userspace.
> > Also it may need to differentiate IRQs and syscalls.
> >
> > - Therefore still involve some case by case handling eventually.
> >
> > - Flushing the global workqueues to avoid deadlocks is unadvised as shown
> > in the comment above flush_scheduled_work(). It even triggers a
> > warning. Significant efforts have been put to convert all the existing
> > users. It's not impossible to sell in our case because we shouldn't
> > hold a lock upon return to userspace. But that will restore a new
> > dangerous API.
> >
> > - Queueing the workqueue / flushing involves a context switch which
> > induce more noise (eg: tick restart)
> >
> > - As above, probably not suitable for HPC.
> >
> > 3) QPW: Handle the work remotely
> >
> > Pros:
> > - Works on all cases, without any surprise.
> >
> > Cons:
> > - Introduce new locking scheme to maintain and debug.
> >
> > - Needs case by case handling.
> >
> > Thoughts?
>
> Can you please be more verbose, mindful of lesser cognitive powers ? :-)

Arguably verbosity is not my most developed skill :o)

>
> Note: i also dislike the added layers (and multiple cases) QPW adds.
>
> But there is precedence with local locks...
>
> Code would be less complex in case spinlocks were added:
>
> 01b44456a7aa7c3b24fa9db7d1714b208b8ef3d8 mm/page_alloc: replace local_lock with normal spinlock
> 4b23a68f953628eb4e4b7fe1294ebf93d4b8ceee mm/page_alloc: protect PCP lists with a spinlock
>
> But people seem to reject that in the basis of performance
> degradation.

And that makes sense. Anyway, we have lockdep to help.

Thanks.

--
Frederic Weisbecker
SUSE Labs