All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Sechang Lim" <rhkrqnwk98@gmail.com>, bpf@vger.kernel.org
Subject: Re: [PATCH bpf] bpf: Reject kernel threads in bpf_task_work_schedule()
Date: Thu, 26 Nov 2026 05:40:22 +0000	[thread overview]
Message-ID: <sashiko-outbox-234800@kernel.org> (raw)
In-Reply-To: <20261126053016.1145482-1-rhkrqnwk98@gmail.com>

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] bpf: irq_work data race in bpf_task_work_cancel_and_free()
  [1]
--

commit 582f8b25ac8ecb8f5cc73a481c3b1dcd182a967f
Author: Sechang Lim <rhkrqnwk98@gmail.com>

bpf: Reject kernel threads in bpf_task_work_schedule()

This commit rejects kernel threads (PF_KTHREAD) with -EPERM in
bpf_task_work_schedule() so that TIF_NOTIFY_SIGNAL is not left set on
threads that never return to user space.

> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index 1b95fe795b4d9..784674c5fddf9 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -4804,6 +4804,9 @@ static int bpf_task_work_schedule(struct task_struct *task, struct bpf_task_work
>  
>  	BTF_TYPE_EMIT(struct bpf_task_work);
>  
> +	if (task->flags & PF_KTHREAD)
> +		return -EPERM;
> +
>  	prog = bpf_prog_inc_not_zero(aux->prog);
>  	if (IS_ERR(prog))
>  		return -EBADF;

[Severity: Critical]
This problem wasn't introduced by this patch [1], but can
bpf_task_work_cancel_and_free() race with irq_work_single() and corrupt
ctx->irq_work?

When bpf_task_work_irq() finishes task_work_add(), it switches ctx->state
from BPF_TW_SCHEDULING to BPF_TW_SCHEDULED before returning to
irq_work_single().  If map deletion calls bpf_task_work_cancel_and_free()
right after that cmpxchg(), it sees BPF_TW_SCHEDULED and calls
init_irq_work() and irq_work_queue() on ctx->irq_work while
irq_work_single() is still clearing IRQ_WORK_BUSY in work->node.a_flags:

CPU0                              CPU1
irq_work_single()
  bpf_task_work_irq()
    cmpxchg(&ctx->state,
            BPF_TW_SCHEDULING,
            BPF_TW_SCHEDULED)
                                  bpf_task_work_cancel_and_free()
                                    state = xchg(&ctx->state, BPF_TW_FREED)
                                    if (state == BPF_TW_SCHEDULED)
                                      init_irq_work(&ctx->irq_work, ...)
                                      irq_work_queue(&ctx->irq_work)
  atomic_cmpxchg(&work->node.a_flags, ...)  <-- data race / flag corruption

kernel/bpf/helpers.c:bpf_task_work_cancel_and_free() {
	...
	state = xchg(&ctx->state, BPF_TW_FREED);
	if (state == BPF_TW_SCHEDULED) {
		/* run in irq_work to avoid locks in NMI */
		init_irq_work(&ctx->irq_work, bpf_task_work_cancel_scheduled);
		irq_work_queue(&ctx->irq_work);
		return;
	}
	...
}

Because init_irq_work() overwrites ctx->irq_work non-atomically and
bpf_task_work_cancel_and_free() can run in NMI context where
irq_work_sync() cannot be used, this corrupts the irq_work flags and can
cause lost cancellation or warnings and panics in the irq_work subsystem.

[1] https://sashiko.dev/#/bug/linux-a7b739e8-61a5-48f3-9ad1-75d3c31446fa

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126053016.1145482-1-rhkrqnwk98@gmail.com?part=1

  reply	other threads:[~2026-11-26  5:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  5:30 [PATCH bpf] bpf: Reject kernel threads in bpf_task_work_schedule() Sechang Lim
2026-11-26  5:40 ` sashiko-bot [this message]
2026-11-26 20:50 ` patchwork-bot+netdevbpf

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=sashiko-outbox-234800@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=rhkrqnwk98@gmail.com \
    --cc=sashiko-reviews@lists.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.