From: Eduard Zingerman <eddyz87@gmail.com>
To: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>,
bpf@vger.kernel.org, ast@kernel.org, andrii@kernel.org,
daniel@iogearbox.net, kafai@meta.com, kernel-team@meta.com,
memxor@gmail.com
Cc: Mykyta Yatsenko <yatsenko@meta.com>
Subject: Re: [PATCH RFC v1 5/5] bpf: remove lock from bpf_async_cb
Date: Wed, 05 Nov 2025 16:08:40 -0800 [thread overview]
Message-ID: <405f8893435d47badc66920b9e8e22b40be469a1.camel@gmail.com> (raw)
In-Reply-To: <0ee6e906-6bba-4145-8e06-f1c47ab19af3@gmail.com>
On Wed, 2025-11-05 at 23:39 +0000, Mykyta Yatsenko wrote:
> On 11/5/25 22:44, Eduard Zingerman wrote:
> > On Wed, 2025-11-05 at 15:30 +0000, Mykyta Yatsenko wrote:
> >
> > [...]
> >
> > > > > @@ -1472,12 +1489,19 @@ BPF_CALL_3(bpf_timer_start, struct bpf_async_kern *, timer, u64, nsecs, u64, fla
> > > > > return -EOPNOTSUPP;
> > > > > if (flags & ~(BPF_F_TIMER_ABS | BPF_F_TIMER_CPU_PIN))
> > > > > return -EINVAL;
> > > > > - __bpf_spin_lock_irqsave(&timer->lock);
> > > > > - t = timer->timer;
> > > > > - if (!t || !t->cb.prog) {
> > > > > - ret = -EINVAL;
> > > > > - goto out;
> > > > > - }
> > > > > +
> > > > > + guard(rcu)();
> > > > > +
> > > > > + t = READ_ONCE(async->timer);
> > > > > + if (!t)
> > > > > + return -EINVAL;
> > > > > +
> > > > > + /*
> > > > > + * Hold ref while scheduling timer, to make sure, we only cancel and free after
> > > > > + * hrtimer_start().
> > > > > + */
> > > > > + if (!bpf_async_tryget(&t->cb))
> > > > > + return -EINVAL;
> > > > Could you please explain in a bit more detail why tryget/put pair is
> > > > needed here?
> > > Yeah, we need to hold the reference to make sure even if cancel_and_free()
> > > go through, the underlying timer struct is not detached/freed, so we won't
> > > get into the situation when we first free, then schedule, with refcnt hold,
> > > we always first schedule and then free, this allows for cancellation run
> > > when
> > > the last ref is put.
> >
> > Sorry, I still don't get it.
> > In bpf_timer_start() you added `guard(rcu)()`.
> > In bpf_timer_cancel_and_free():
> >
> > - bpf_timer_cancel_and_free
> > - bpf_async_put(cb: &t->cb, type: BPF_ASYNC_TYPE_TIMER)
> > - bpf_timer_delete(t: (struct bpf_hrtimer *)cb);
> > - bpf_timer_delete_work(work: &t->cb.delete_work);
> > - call_rcu(head: &t->cb.rcu, func: bpf_async_cb_rcu_free)
> >
> > So, it looks like `t->cb` is protected by RCU and can't go away
> > between `guard(rcu)()` and bpf_timer_start() exit.
> > What will go wrong if tryget is removed?
>
> bpf_timer_delete() also calls hrtimer_cancel(). If bpf_timer_start()
> does not hold refcnt, we may run into the situation when hrtimer_cancel()
> runs before hrtimer_start(). The timer is going to be deleted after the
> grace period but it is not cancelled, and the timer callback may read
> after free.
> Holding refcnt makes sure hrtimer_cancel() will be called after
> hrtimer_start()
> (or way before it, and we error out).
Ok, so the following path is possible:
- bpf_timer_cancel_and_free
- bpf_async_put
- bpf_timer_delete (if refcount_dec_and_test(r: &cb->refcnt) returns true)
- queue_work
- bpf_timer_delete_work
- hrtimer_cancel
- call_rcu(&t->cb.rcu, bpf_async_cb_rcu_free)
And thus, the following sequence of events would be possible w/o the
tryget:
Thread A Thread B
-------------------------- ------------------------------
enter bpf_timer_start
enter RCU protected region
bpf_timer_cancel_and_free call
hrtimer_cancel()
call_rcu(&t->cb.rcu, bpf_async_cb_rcu_free)
hrtimer_start()
exit RCU protected region
... some thread ...
bpf_async_cb_rcu_free()
timer is popped from the queue, use after free
Makes sense, thank you for explaining.
[...]
prev parent reply other threads:[~2025-11-06 0:08 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-31 21:58 [PATCH RFC 0/5] bpf: avoid locks in bpf_timer and bpf_wq Mykyta Yatsenko
2025-10-31 21:58 ` [PATCH RFC v1 1/5] bpf: refactor bpf_async_cb callback update Mykyta Yatsenko
2025-11-04 1:58 ` Eduard Zingerman
2025-10-31 21:58 ` [PATCH RFC v1 2/5] bpf: refactor bpf_async_cb prog swap Mykyta Yatsenko
2025-11-04 18:42 ` Eduard Zingerman
2025-10-31 21:58 ` [PATCH RFC v1 3/5] bpf: factor out timer deletion helper Mykyta Yatsenko
2025-11-04 18:45 ` Eduard Zingerman
2025-10-31 21:58 ` [PATCH RFC v1 4/5] bpf: add refcnt into struct bpf_async_cb Mykyta Yatsenko
2025-10-31 22:35 ` bot+bpf-ci
2025-11-03 18:14 ` Alexei Starovoitov
2025-10-31 21:58 ` [PATCH RFC v1 5/5] bpf: remove lock from bpf_async_cb Mykyta Yatsenko
2025-11-04 22:01 ` Eduard Zingerman
2025-11-05 15:30 ` Mykyta Yatsenko
2025-11-05 22:44 ` Eduard Zingerman
2025-11-05 23:39 ` Mykyta Yatsenko
2025-11-06 0:08 ` Eduard Zingerman [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=405f8893435d47badc66920b9e8e22b40be469a1.camel@gmail.com \
--to=eddyz87@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kafai@meta.com \
--cc=kernel-team@meta.com \
--cc=memxor@gmail.com \
--cc=mykyta.yatsenko5@gmail.com \
--cc=yatsenko@meta.com \
/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;
as well as URLs for NNTP newsgroup(s).