mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Breno Leitao <leitao@debian.org>
Cc: Andrii Nakryiko <andrii@kernel.org>,
	linux-trace-kernel@vger.kernel.org,  peterz@infradead.org,
	oleg@redhat.com, rostedt@goodmis.org,  mhiramat@kernel.org,
	mingo@kernel.org, bpf@vger.kernel.org,
	 linux-kernel@vger.kernel.org, jolsa@kernel.org,
	paulmck@kernel.org
Subject: Re: [PATCH v3 tip/perf/core 2/2] uprobes: SRCU-protect uretprobe lifetime (with timeout)
Date: Tue, 25 Feb 2025 14:10:59 -0800	[thread overview]
Message-ID: <CAEf4Bzae9ngTyKjt1O2PO6Udk7cC5M9VGLrvjCyTv2fbRRFy_Q@mail.gmail.com> (raw)
In-Reply-To: <20250225-transparent-bronze-cobra-bafff4@leitao>

On Tue, Feb 25, 2025 at 3:46 AM Breno Leitao <leitao@debian.org> wrote:
>
> Hello Andrii,
>
> On Mon, Feb 24, 2025 at 02:23:51PM -0800, Andrii Nakryiko wrote:
> > On Mon, Feb 24, 2025 at 4:23 AM Breno Leitao <leitao@debian.org> wrote:
> > >
> > > Hello Andrii,
> > >
> > > On Wed, Oct 23, 2024 at 09:41:59PM -0700, Andrii Nakryiko wrote:
> > > >
> > > > +static struct uprobe* hprobe_expire(struct hprobe *hprobe, bool get)
> > > > +{
> > > > +     enum hprobe_state hstate;
> > > > +
> > > > +     /*
> > > > +      * return_instance's hprobe is protected by RCU.
> > > > +      * Underlying uprobe is itself protected from reuse by SRCU.
> > > > +      */
> > > > +     lockdep_assert(rcu_read_lock_held() && srcu_read_lock_held(&uretprobes_srcu));
> > >
> > > I am hitting this warning in d082ecbc71e9e ("Linux 6.14-rc4") on
> > > aarch64. I suppose this might happen on x86 as well, but I haven't
> > > tested.
> > >
> > >         WARNING: CPU: 28 PID: 158906 at kernel/events/uprobes.c:768 hprobe_expire (kernel/events/uprobes.c:825)
> > >
> > >         Call trace:
> > >         hprobe_expire (kernel/events/uprobes.c:825) (P)
> > >         uprobe_copy_process (kernel/events/uprobes.c:691 kernel/events/uprobes.c:2103 kernel/events/uprobes.c:2142)
> > >         copy_process (kernel/fork.c:2636)
> > >         kernel_clone (kernel/fork.c:2815)
> > >         __arm64_sys_clone (kernel/fork.c:? kernel/fork.c:2926 kernel/fork.c:2926)
> > >         invoke_syscall (arch/arm64/kernel/syscall.c:35 arch/arm64/kernel/syscall.c:49)
> > >         do_el0_svc (arch/arm64/kernel/syscall.c:139 arch/arm64/kernel/syscall.c:151)
> > >         el0_svc (arch/arm64/kernel/entry-common.c:165 arch/arm64/kernel/entry-common.c:178 arch/arm64/kernel/entry-common.c:745)
> > >         el0t_64_sync_handler (arch/arm64/kernel/entry-common.c:797)
> > >         el0t_64_sync (arch/arm64/kernel/entry.S:600)
> > >
> > > I broke down that warning, and the problem is on related to
> > > rcu_read_lock_held(), since RCU read lock does not seem to be held in
> > > this path.
> > >
> > > Reading this code, RCU read lock seems to protect old hprobe, which
> > > doesn't seem so.
> > >
> > > I am wondering if we need to protect it properly, something as:
> > >
> > >         @@ -2089,7 +2092,9 @@ static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask)
> > >                                 return -ENOMEM;
> > >
> > >                         /* if uprobe is non-NULL, we'll have an extra refcount for uprobe */
> > >         +               rcu_read_lock();
> > >                         uprobe = hprobe_expire(&o->hprobe, true);
> > >         +               rcu_write_lock();
> > >
> >
> > I think this is not good enough. rcu_read_lock/unlock should be around
> > the entire for loop, because, technically, that return_instance can be
> > freed before we even get to hprobe_expire.
>
> re you suggesting that we should use an RCU read lock to protect the
> "traversal" of return_instances? In other words, is it currently being
> traversed unsafely, given that return_instance can be freed at any time?

Yes, that's what I was suggesting initially.

But reading through all this again and paging in all the context I had
when I was implementing this, I think this
lockdep_assert(rcu_read_lock_held()) is a false positive.

For timer thread, yes, we need to keep rcu_read_lock(), because timer
thread doesn't own return_instance, so it has to guarantee that memory
won't go away.

But dup_utask() is called for current thread's return_instances, so
they can't go anywhere and RCU read lock is not necessary here.

So I'm going to send a patch removing part of this lockdep_assert()
and will expand the comment with the actual lifetime rules.

>
> > So, just like we have guard(srcu)(&uretprobes_srcu); we should have
> > guard(rcu)();
> >
> > Except, there is that kmemdup() hidden inside dup_return_instance(),
> > so we can't really do that.
>
> Right. kmemdup() is using GFP_KERNEL, which might sleep, so, it cannot
> be called using rcu read lock.
>
> Thanks
> --breno

      parent reply	other threads:[~2025-02-25 22:11 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-24  4:41 [PATCH v3 tip/perf/core 0/2] SRCU-protected uretprobes hot path Andrii Nakryiko
2024-10-24  4:41 ` [PATCH v3 tip/perf/core 1/2] uprobes: allow put_uprobe() from non-sleepable softirq context Andrii Nakryiko
2024-10-31 10:08   ` [tip: perf/core] " tip-bot2 for Andrii Nakryiko
2024-10-24  4:41 ` [PATCH v3 tip/perf/core 2/2] uprobes: SRCU-protect uretprobe lifetime (with timeout) Andrii Nakryiko
2024-10-31 10:08   ` [tip: perf/core] " tip-bot2 for Andrii Nakryiko
2025-02-24 12:22   ` [PATCH v3 tip/perf/core 2/2] " Breno Leitao
2025-02-24 22:23     ` Andrii Nakryiko
2025-02-25 11:46       ` Breno Leitao
2025-02-25 15:13         ` Paul E. McKenney
2025-02-25 22:10         ` Andrii Nakryiko [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAEf4Bzae9ngTyKjt1O2PO6Udk7cC5M9VGLrvjCyTv2fbRRFy_Q@mail.gmail.com \
    --to=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=jolsa@kernel.org \
    --cc=leitao@debian.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mingo@kernel.org \
    --cc=oleg@redhat.com \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®