Wire up the task_vma iterator to use the KF_ACQUIRE/KF_RELEASE and KF_FORBID_SLEEP infrastructure from the previous commits. Annotate bpf_iter_task_vma_next with KF_ACQUIRE | KF_FORBID_SLEEP so each returned VMA pointer becomes a tracked reference that prevents sleeping. Add bpf_iter_task_vma_release(it__iter) with KF_RELEASE that releases the mmap read lock, drops the acquired reference, and re-enables sleeping. The next call to _next will re-acquire the lock via mmap_read_trylock(). This enables a split iteration pattern: bpf_for_each(task_vma, vma, task, 0) { u64 start = vma->vm_start; /* vma access allowed, sleeping forbidden */ bpf_iter_task_vma_release(&___it); /* vma access forbidden, sleeping allowed */ bpf_copy_from_user(&buf, sizeof(buf), (void *)start); } Signed-off-by: Puranjay Mohan --- kernel/bpf/helpers.c | 3 ++- kernel/bpf/task_iter.c | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 7ac32798eb04..b3921c22810e 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -4605,8 +4605,9 @@ BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW) BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY) BTF_ID_FLAGS(func, bpf_iter_task_vma_new, KF_ITER_NEW | KF_RCU) -BTF_ID_FLAGS(func, bpf_iter_task_vma_next, KF_ITER_NEXT | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_iter_task_vma_next, KF_ITER_NEXT | KF_RET_NULL | KF_ACQUIRE | KF_FORBID_SLEEP) BTF_ID_FLAGS(func, bpf_iter_task_vma_destroy, KF_ITER_DESTROY) +BTF_ID_FLAGS(func, bpf_iter_task_vma_release, KF_RELEASE) #ifdef CONFIG_CGROUPS BTF_ID_FLAGS(func, bpf_iter_css_task_new, KF_ITER_NEW) BTF_ID_FLAGS(func, bpf_iter_css_task_next, KF_ITER_NEXT | KF_RET_NULL) diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c index a85115c191e4..55b264d634db 100644 --- a/kernel/bpf/task_iter.c +++ b/kernel/bpf/task_iter.c @@ -879,6 +879,16 @@ __bpf_kfunc struct vm_area_struct *bpf_iter_task_vma_next(struct bpf_iter_task_v return vma; } +__bpf_kfunc void bpf_iter_task_vma_release(struct bpf_iter_task_vma *it__iter) +{ + struct bpf_iter_task_vma_kern *kit = (void *)it__iter; + + if (kit->data && kit->data->locked) { + bpf_mmap_unlock_mm(kit->data->work, kit->data->mm); + kit->data->locked = false; + } +} + __bpf_kfunc void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it) { struct bpf_iter_task_vma_kern *kit = (void *)it; -- 2.47.3