From: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>
To: Yun Lu <luyun_611@163.com>,
ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
eddyz87@gmail.com, memxor@gmail.com, martin.lau@linux.dev,
song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org,
emil@etsalapatis.com, ihor.solodrai@linux.dev
Cc: yatsenko@meta.com, bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v4 2/3] bpf: Prevent task work context reuse while irq_work is busy
Date: Tue, 29 Sep 2026 13:02:27 +0100 [thread overview]
Message-ID: <434f2e94-190c-45cb-8d91-54310da039ea@gmail.com> (raw)
In-Reply-To: <20260929072800.113129-3-luyun_611@163.com>
On 9/29/26 8:27 AM, Yun Lu wrote:
> From: Yun Lu <luyun@kylinos.cn>
>
> A bpf_task_work context can return to STANDBY before its scheduling
> irq_work handler returns. Both callback completion and task_work_add()
> failure can publish STANDBY while the irq_work is still BUSY. A new
> round can then reinitialize and queue the same irq_work on another CPU
> while the old handler is still running.
>
> One concrete interleaving is:
>
> 1. CPU 0 successfully queues ctx->work with task_work_add() and pauses
> before its SCHEDULING-to-SCHEDULED cmpxchg.
>
> 2. The target task on CPU 1 completes the callback, resets ctx->task
> and publishes STANDBY.
>
> 3. CPU 2 starts a new round, changes STANDBY to PENDING and queues the
> same irq_work. The new handler changes PENDING to SCHEDULING, but
> task_work_add() returns -ESRCH because its target task has exited.
> The handler has not yet reset the context or rolled back the state.
>
> 4. CPU 0 resumes and changes the new round's SCHEDULING to SCHEDULED,
> even though no task work was queued for that round.
>
> 5. The new handler resets ctx->task, but its SCHEDULING-to-STANDBY
> cmpxchg fails. The context remains SCHEDULED with a NULL task, and
> subsequent attempts to acquire this context fail with -EBUSY.
>
> 6. Map-value deletion observes SCHEDULED and queues asynchronous
> cancellation, which passes the NULL ctx->task to task_work_cancel().
>
> Overlapping invocations also share one BUSY bit. An invocation can clear
> that bit while the other invocation is still running.
>
> Initialize the scheduling irq_work when the context is created and stop
> reinitializing it for each round. After claiming the context with the
> STANDBY-to-PENDING cmpxchg, check BUSY and reject reuse with -EBUSY if a
> previous invocation is still in progress. Checking after the cmpxchg
> orders the BUSY read after the STANDBY publication.
>
> No irq_work has been queued for the attempted round at this point, so
> the rejection can roll PENDING back to STANDBY. Use cmpxchg for that
> rollback to preserve FREED if a concurrent deletion has published it.
> Once BUSY clears, scheduling can be retried.
>
> Fixes: 38aa7003e369 ("bpf: task work scheduling kfuncs")
> Signed-off-by: Yun Lu <luyun@kylinos.cn>
> ---
Acked-by: Mykyta Yatsenko <yatsenko@meta.com>
> kernel/bpf/helpers.c | 14 +++++++++++++-
> 1 file changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index 028fc6796b59..350269b8646e 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -4553,6 +4553,7 @@ static struct bpf_task_work_ctx *bpf_task_work_fetch_ctx(struct bpf_task_work *t
> memset(ctx, 0, sizeof(*ctx));
> refcount_set(&ctx->refcnt, 1); /* map's own ref */
> ctx->state = BPF_TW_STANDBY;
> + init_irq_work(&ctx->irq_work, bpf_task_work_irq);
>
> old_ctx = cmpxchg(&twk->ctx, NULL, ctx);
> if (old_ctx) {
> @@ -4595,6 +4596,18 @@ static struct bpf_task_work_ctx *bpf_task_work_acquire_ctx(struct bpf_task_work
> bpf_task_work_ctx_put(ctx);
> return ERR_PTR(-EBUSY);
> }
> + /*
> + * STANDBY can be published before the previous scheduling irq_work
> + * returns. Claim PENDING before checking BUSY to order the check after
> + * that publication. No irq_work has been queued for this attempt yet,
> + * so reject reuse and roll back to STANDBY while BUSY is set. Preserve
> + * FREED if deletion wins the rollback race.
> + */
> + if (unlikely(irq_work_is_busy(&ctx->irq_work))) {
> + (void)cmpxchg(&ctx->state, BPF_TW_PENDING, BPF_TW_STANDBY);
> + bpf_task_work_ctx_put(ctx);
> + return ERR_PTR(-EBUSY);
> + }
>
> /*
> * If no process or bpffs is holding a reference to the map, no new callbacks should be
> @@ -4644,7 +4657,6 @@ static int bpf_task_work_schedule(struct task_struct *task, struct bpf_task_work
> ctx->map = map;
> ctx->map_val = (void *)tw - map->record->task_work_off;
> init_task_work(&ctx->work, bpf_task_work_callback);
> - init_irq_work(&ctx->irq_work, bpf_task_work_irq);
>
> irq_work_queue(&ctx->irq_work);
> return 0;
next prev parent reply other threads:[~2026-09-29 12:02 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-29 7:27 [PATCH bpf-next v4 0/3] bpf: Fix task work scheduling races Yun Lu
2026-09-29 7:27 ` [PATCH bpf-next v4 1/3] bpf: Fix NULL task dereference in task work cancellation Yun Lu
2026-09-29 7:27 ` [PATCH bpf-next v4 2/3] bpf: Prevent task work context reuse while irq_work is busy Yun Lu
2026-09-29 12:02 ` Mykyta Yatsenko [this message]
2026-09-29 7:28 ` [PATCH bpf-next v4 3/3] selftests/bpf: Add task work scheduling race test Yun Lu
2026-09-29 8:12 ` bot+bpf-ci
2026-09-29 12:00 ` Mykyta Yatsenko
2026-09-30 8:37 ` luyun
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=434f2e94-190c-45cb-8d91-54310da039ea@gmail.com \
--to=mykyta.yatsenko5@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=ihor.solodrai@linux.dev \
--cc=jolsa@kernel.org \
--cc=luyun_611@163.com \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=song@kernel.org \
--cc=yatsenko@meta.com \
--cc=yonghong.song@linux.dev \
/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