From: Puranjay Mohan <puranjay@kernel.org>
To: bpf@vger.kernel.org
Cc: Puranjay Mohan <puranjay@kernel.org>,
Puranjay Mohan <puranjay12@gmail.com>,
Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Martin KaFai Lau <martin.lau@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>,
kernel-team@meta.com
Subject: [PATCH bpf-next 0/5] Introduce KF_FORBID_SLEEP modifier for acquire/release kfuncs
Date: Wed, 18 Feb 2026 10:25:45 -0800 [thread overview]
Message-ID: <20260218182555.1501495-1-puranjay@kernel.org> (raw)
Some BPF kfuncs acquire resources that prevent sleeping - a lock, a
reference to an object under a lock, a preempt-disable section. Today there
is no way for the verifier to know that holding a particular acquired
reference means sleeping is forbidden. Programs either run entirely in
sleepable or non-sleepable context, with no way to express "sleeping is
forbidden right now, but will be allowed once I release this reference."
This series adds KF_FORBID_SLEEP, a new kfunc flag that can be combined
with KF_ACQUIRE. When a kfunc annotated with KF_ACQUIRE | KF_FORBID_SLEEP
is called, the verifier tags the acquired reference with forbid_sleep and
increments a per-state forbid_sleep_count counter. When the reference is
released (through corresponding KF_RELEASE kfunc), the counter is
decremented. The verifier checks this counter everywhere it decides
whether sleeping is allowed — sleepable helpers, sleepable kfuncs, global
function calls, and resource leak checks.
This is fully generic. Any pair of KF_ACQUIRE/KF_RELEASE kfuncs can opt
into sleep prohibition by adding KF_FORBID_SLEEP to the acquire side.
To make this useful for iterators specifically, the series first extends
the verifier's iterator support to allow KF_ACQUIRE on _next and KF_RELEASE
on a separate kfunc taking an __iter argument. The verifier tracks the
acquired reference on the iterator's stack slot (st->id) and auto-releases
it on the next _next call and on the DRAINED (NULL) path, so the
acquire/release is transparent to programs that don't need mid-loop
release.
Iterator KF_ACQUIRE support is not useful on its own right now, but it
becomes the foundation for KF_FORBID_SLEEP: an iterator whose _next is
annotated with KF_ACQUIRE | KF_FORBID_SLEEP can now express "holding this
pointer forbids sleeping; calling _release invalidates the pointer and
re-enables sleeping."
The task_vma iterator is the first user. It holds mmap_lock during
iteration, preventing sleepable helpers like bpf_copy_from_user().
Currently, this can lead to a deadlock as the fault path also takes
mmap_lock.
With this series, a BPF program can release the lock mid-iteration:
bpf_for_each(task_vma, vma, task, 0) {
u64 start = vma->vm_start;
/* sleeping forbidden, but vma pointer access allowed */
bpf_iter_task_vma_release(&___it);
/* mmap_lock released, vma pointer invalidated */
/* sleeping is fine here */
bpf_copy_from_user(&buf, sizeof(buf), (void *)start);
}
The series is organized as:
Patch 1: KF_ACQUIRE/KF_RELEASE plumbing for iterators in the
verifier. Pure infrastructure, no behavioral change to
existing iterators.
Patch 2: KF_FORBID_SLEEP flag and forbid_sleep_count machinery.
Generic, works for any KF_ACQUIRE kfunc - iterator or not.
Patch 3: Move mmap_lock acquisition from _new to _next in the
task_vma iterator, preparing for re-acquisition after
release.
Patch 4: Wire up task_vma as the first user: annotate _next with
KF_ACQUIRE | KF_FORBID_SLEEP, add bpf_iter_task_vma_release().
Patch 5: Selftests covering the runtime path (release + copy_from_user)
and verifier rejection of invalid patterns (sleeping without
release, VMA access after release, double release, release
without acquire, nested iterator interaction).
Puranjay Mohan (5):
bpf: Add KF_ACQUIRE and KF_RELEASE support for iterators
bpf: Add KF_FORBID_SLEEP modifier for KF_ACQUIRE kfuncs
bpf: Move locking to bpf_iter_task_vma_next()
bpf: Add split iteration support to task_vma iterator
selftests/bpf: Add tests for split task_vma iterator
include/linux/bpf_verifier.h | 2 +
include/linux/btf.h | 1 +
kernel/bpf/helpers.c | 3 +-
kernel/bpf/task_iter.c | 44 ++++--
kernel/bpf/verifier.c | 121 +++++++++++++++--
.../testing/selftests/bpf/bpf_experimental.h | 1 +
.../testing/selftests/bpf/prog_tests/iters.c | 13 ++
.../selftests/bpf/progs/iters_task_vma.c | 39 ++++++
.../bpf/progs/iters_task_vma_nosleep.c | 125 ++++++++++++++++++
9 files changed, 325 insertions(+), 24 deletions(-)
create mode 100644 tools/testing/selftests/bpf/progs/iters_task_vma_nosleep.c
base-commit: 4c51f90d45dca71e7974ed5a7c40b9c04a6c6762
--
2.47.3
next reply other threads:[~2026-02-18 18:26 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-18 18:25 Puranjay Mohan [this message]
2026-02-18 18:25 ` [PATCH bpf-next 1/5] bpf: Add KF_ACQUIRE and KF_RELEASE support for iterators Puranjay Mohan
2026-02-19 17:45 ` Mykyta Yatsenko
2026-02-19 20:37 ` Eduard Zingerman
2026-02-20 1:05 ` Puranjay Mohan
2026-02-20 1:09 ` Eduard Zingerman
2026-02-18 18:25 ` [PATCH bpf-next 2/5] bpf: Add KF_FORBID_SLEEP modifier for KF_ACQUIRE kfuncs Puranjay Mohan
2026-02-19 3:24 ` Alexei Starovoitov
2026-02-20 0:14 ` Puranjay Mohan
2026-02-19 22:19 ` Eduard Zingerman
2026-02-20 0:51 ` Puranjay Mohan
2026-02-18 18:25 ` [PATCH bpf-next 3/5] bpf: Move locking to bpf_iter_task_vma_next() Puranjay Mohan
2026-02-18 18:25 ` [PATCH bpf-next 4/5] bpf: Add split iteration support to task_vma iterator Puranjay Mohan
2026-02-18 18:25 ` [PATCH bpf-next 5/5] selftests/bpf: Add tests for split " Puranjay Mohan
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=20260218182555.1501495-1-puranjay@kernel.org \
--to=puranjay@kernel.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=kernel-team@meta.com \
--cc=martin.lau@kernel.org \
--cc=memxor@gmail.com \
--cc=mykyta.yatsenko5@gmail.com \
--cc=puranjay12@gmail.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