Re: [PATCH 6/8] perf/hw_breakpoint: Reduce contention with large number of tasks

From: Marco Elver
Date: Thu Jun 09 2022 - 09:29:42 EST


On Thu, Jun 09, 2022 at 03:03PM +0200, Dmitry Vyukov wrote:
[...]
> > -/* Serialize accesses to the above constraints */
> > -static DEFINE_MUTEX(nr_bp_mutex);
> > +/*
> > + * Synchronizes accesses to the per-CPU constraints; users of data in bp_cpuinfo
> > + * must acquire bp_cpuinfo_lock as writer to get a stable snapshot of all CPUs'
> > + * constraints. Modifications without use may only acquire bp_cpuinfo_lock as a
> > + * reader, but must otherwise ensure modifications are never lost.
> > + */
>
> I can't understand this comment.
> Modifications need to acquire in read mode, while only users must
> acquire in write mode. Shouldn't it be the other way around? What is
> "Modifications without use"?

Right, maybe this comment needs tweaking.

The main rules are -- the obvious ones:

- plain reads are ok with just a read-lock (target is task,
reading 'cpu_pinned');

- plain writes need a write-lock (target is CPU, writing
'cpu_pinned');

the not so obvious one:

- "modification without use" are the increment/decrement of
tsk_pinned done if the target is a task; in this case, we can
happily allow concurrent _atomic_ increments/decrements from
different tasks as long as there is no "use" i.e. read the
value and check it to make a decision if there is space or not
(this is only done by CPU targets).

So the main idea is that the rwlock when held as a reader permits these
"modifications without use" concurrently by task targets, but will block
a CPU target wishing to get a stable snapshot until that acquires the
rwlock as a writer.

The modifications done by task targets are done on atomic variables, so
we never loose any increments/decrements, but while these modifications
are going on, the global view of tsk_pinned may be inconsistent.
However, we know that once a CPU target acquires the rwlock as a writer,
there will be no more "readers" -- or rather any task targets that can
update tsk_pinned concurrently -- and therefore tsk_pinned must be
stable once we acquire the rwlock as a writer.

I'll have to think some more how to best update the comment...

> > +static DEFINE_RWLOCK(bp_cpuinfo_lock);
> > +
> > +/*
> > + * Synchronizes accesses to the per-task breakpoint list in task_bps_ht. Since
> > + * rhltable synchronizes concurrent insertions/deletions, independent tasks may
> > + * insert/delete concurrently; therefore, a mutex per task would be sufficient.
> > + *
> > + * To avoid bloating task_struct with infrequently used data, use a sharded
> > + * mutex that scales with number of CPUs.
> > + */
> > +static DEFINE_PER_CPU(struct mutex, task_sharded_mtx);
> > +
> > +static struct mutex *get_task_sharded_mtx(struct perf_event *bp)
> > +{
> > + int shard;
> > +
> > + if (!bp->hw.target)
> > + return NULL;
> > +
> > + /*
> > + * Compute a valid shard index into per-CPU data.
> > + */
> > + shard = task_pid_nr(bp->hw.target) % nr_cpu_ids;
> > + shard = cpumask_next(shard - 1, cpu_possible_mask);
> > + if (shard >= nr_cpu_ids)
> > + shard = cpumask_first(cpu_possible_mask);
> > +
> > + return per_cpu_ptr(&task_sharded_mtx, shard);
> > +}
> > +
> > +static struct mutex *bp_constraints_lock(struct perf_event *bp)
> > +{
> > + struct mutex *mtx = get_task_sharded_mtx(bp);
> > +
> > + if (mtx) {
> > + mutex_lock(mtx);
> > + read_lock(&bp_cpuinfo_lock);
>
> Is NR_CPUS == 1 case still important to optimize? I guess with small
> VMs it may be important again.
> If so, we could just write-lock bp_cpuinfo_lock always if NR_CPUS == 1.

Not sure, I guess it's easy to add the check for NR_CPUS==1.

[...]
> > @@ -397,12 +497,11 @@ static void __release_bp_slot(struct perf_event *bp, u64 bp_type)
> >
> > void release_bp_slot(struct perf_event *bp)
> > {
> > - mutex_lock(&nr_bp_mutex);
> > + struct mutex *mtx = bp_constraints_lock(bp);
> >
> > arch_unregister_hw_breakpoint(bp);
>
> If I understand this correctly, this can weaken protection for
> arch_unregister_hw_breakpoint() and __modify_bp_slot(). Previously
> they were globally serialized, but now several calls can run in
> parallel. Is it OK?

__modify_bp_slot() just calls __release_bp_slot() and
__reserve_bp_slot() which is related to constraints accounting, and is
all internal to hw_breakpoint.

Only ppc overrides some of the sea arch_ functions. In arch/powerpc:
arch_unregister_hw_breakpoint() looks like it only accesses
bp->ctx->task, so that looks ok; however, looks like
arch_release_bp_slot() might want its own lock because it mutates a
list, but that lock wants to be in powerpc code.