Re: [PATCH v3 04/13] uprobes: travers uprobe's consumer list locklessly under SRCU protection

Andrii Nakryiko
2024-08-22T17:52:07Z

On Thu, Aug 22, 2024 at 10:35 AM Jiri Olsa <olsajiri@gmail.com> wrote:
>
> On Thu, Aug 22, 2024 at 09:59:29AM -0700, Andrii Nakryiko wrote:
> > On Thu, Aug 22, 2024 at 7:22 AM Jiri Olsa <olsajiri@gmail.com> wrote:
> > >
> > > On Mon, Aug 12, 2024 at 09:29:08PM -0700, Andrii Nakryiko wrote:
> > >
> > > SNIP
> > >
> > > > @@ -1125,18 +1103,31 @@ void uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc)
> > > > int err;
> > > >
> > > > down_write(&uprobe->register_rwsem);
> > > > - if (WARN_ON(!consumer_del(uprobe, uc))) {
> > > > - err = -ENOENT;
> > > > - } else {
> > > > - err = register_for_each_vma(uprobe, NULL);
> > > > - /* TODO : cant unregister? schedule a worker thread */
> > > > - if (unlikely(err))
> > > > - uprobe_warn(current, "unregister, leaking uprobe");
> > > > - }
> > > > +
> > > > + list_del_rcu(&uc->cons_node);
> > >
> > > hi,
> > > I'm using this patchset as base for my changes and stumbled on this today,
> > > I'm probably missing something, but should we keep the 'uprobe->consumer_rwsem'
> > > lock around the list_del_rcu?
> > >
> >
> > Note that original code also didn't take consumer_rwsem, but rather
> > kept register_rwsem (which we still use).
>
> humm, consumer_del took consumer_rwsem, right?
>

Ah, it was inside consume_del(), sorry, my bad. I can add nested
consumer_rwsem back, but what I mentioned earlier, regiser_rwsem is
sort of interchangeable and sufficient enough for working with
consumer list, it seems. There are a bunch of places where we iterated
this list without holding consumer_rwsem lock and that doesn't break
anything.

Also, consumer_add() and consumer_del() are always called with
register_rwsem, so that consumer_rwsem isn't necessary.

We also have prepare_uprobe() holding consumer_rwsem and there is a
comment about abuse of that rwsem and suggestion to move it to
registration, I never completely understood that. But prepare_uprobe()
doesn't seem to modify consumers list at all.

And the one remaining use of consumer_rwsem is filter_chain(), which
for handler_chain() will be also called under register_rwsem, if
purely lockless traversal is not enough.

There are two other calls to filter_chain() that are not protected by
register_rwsem, so just because of those two maybe we should keep
consumer_rwsem, but so far all the stress testing never caught any
problem.

> jirka
>
> >
> > There is a bit of mix of using register_rwsem and consumer_rwsem for
> > working with consumer list. Code hints at this as being undesirable
> > and "temporary", but you know, it's not broken :)
> >
> > Anyways, my point is that we didn't change the behavior, this should
> > be fine. That _rcu() in list_del_rcu() is not about lockless
> > modification of the list, but rather modification in such a way as to
> > keep lockless RCU-protected *readers* correct. It just does some more
> > memory barrier/release operations more carefully.
> >
> > > jirka
> > >
> > >
> > > > + err = register_for_each_vma(uprobe, NULL);
> > > > +
> > > > up_write(&uprobe->register_rwsem);
> > > >
> > > > - if (!err)
> > > > - put_uprobe(uprobe);
> > > > + /* TODO : cant unregister? schedule a worker thread */
> > > > + if (unlikely(err)) {
> > > > + uprobe_warn(current, "unregister, leaking uprobe");
> > > > + goto out_sync;
> > > > + }
> > > > +
> > > > + put_uprobe(uprobe);
> > > > +