Skip to content

Commit 4ac4546

Browse files
davemarchevskyanakryiko
authored andcommitted
bpf: Introduce task_vma open-coded iterator kfuncs
This patch adds kfuncs bpf_iter_task_vma_{new,next,destroy} which allow creation and manipulation of struct bpf_iter_task_vma in open-coded iterator style. BPF programs can use these kfuncs directly or through bpf_for_each macro for natural-looking iteration of all task vmas. The implementation borrows heavily from bpf_find_vma helper's locking - differing only in that it holds the mmap_read lock for all iterations while the helper only executes its provided callback on a maximum of 1 vma. Aside from locking, struct vma_iterator and vma_next do all the heavy lifting. A pointer to an inner data struct, struct bpf_iter_task_vma_data, is the only field in struct bpf_iter_task_vma. This is because the inner data struct contains a struct vma_iterator (not ptr), whose size is likely to change under us. If bpf_iter_task_vma_kern contained vma_iterator directly such a change would require change in opaque bpf_iter_task_vma struct's size. So better to allocate vma_iterator using BPF allocator, and since that alloc must already succeed, might as well allocate all iter fields, thereby freezing struct bpf_iter_task_vma size. Signed-off-by: Dave Marchevsky <davemarchevsky@fb.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20231013204426.1074286-4-davemarchevsky@fb.com
1 parent 45b3894 commit 4ac4546

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

kernel/bpf/helpers.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2552,6 +2552,9 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
25522552
BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
25532553
BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
25542554
BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
2555+
BTF_ID_FLAGS(func, bpf_iter_task_vma_new, KF_ITER_NEW | KF_RCU)
2556+
BTF_ID_FLAGS(func, bpf_iter_task_vma_next, KF_ITER_NEXT | KF_RET_NULL)
2557+
BTF_ID_FLAGS(func, bpf_iter_task_vma_destroy, KF_ITER_DESTROY)
25552558
BTF_ID_FLAGS(func, bpf_dynptr_adjust)
25562559
BTF_ID_FLAGS(func, bpf_dynptr_is_null)
25572560
BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly)

kernel/bpf/task_iter.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include <linux/fs.h>
88
#include <linux/fdtable.h>
99
#include <linux/filter.h>
10+
#include <linux/bpf_mem_alloc.h>
1011
#include <linux/btf_ids.h>
12+
#include <linux/mm_types.h>
1113
#include "mmap_unlock_work.h"
1214

1315
static const char * const iter_task_type_names[] = {
@@ -803,6 +805,95 @@ const struct bpf_func_proto bpf_find_vma_proto = {
803805
.arg5_type = ARG_ANYTHING,
804806
};
805807

808+
struct bpf_iter_task_vma_kern_data {
809+
struct task_struct *task;
810+
struct mm_struct *mm;
811+
struct mmap_unlock_irq_work *work;
812+
struct vma_iterator vmi;
813+
};
814+
815+
struct bpf_iter_task_vma {
816+
/* opaque iterator state; having __u64 here allows to preserve correct
817+
* alignment requirements in vmlinux.h, generated from BTF
818+
*/
819+
__u64 __opaque[1];
820+
} __attribute__((aligned(8)));
821+
822+
/* Non-opaque version of bpf_iter_task_vma */
823+
struct bpf_iter_task_vma_kern {
824+
struct bpf_iter_task_vma_kern_data *data;
825+
} __attribute__((aligned(8)));
826+
827+
__diag_push();
828+
__diag_ignore_all("-Wmissing-prototypes",
829+
"Global functions as their definitions will be in vmlinux BTF");
830+
831+
__bpf_kfunc int bpf_iter_task_vma_new(struct bpf_iter_task_vma *it,
832+
struct task_struct *task, u64 addr)
833+
{
834+
struct bpf_iter_task_vma_kern *kit = (void *)it;
835+
bool irq_work_busy = false;
836+
int err;
837+
838+
BUILD_BUG_ON(sizeof(struct bpf_iter_task_vma_kern) != sizeof(struct bpf_iter_task_vma));
839+
BUILD_BUG_ON(__alignof__(struct bpf_iter_task_vma_kern) != __alignof__(struct bpf_iter_task_vma));
840+
841+
/* is_iter_reg_valid_uninit guarantees that kit hasn't been initialized
842+
* before, so non-NULL kit->data doesn't point to previously
843+
* bpf_mem_alloc'd bpf_iter_task_vma_kern_data
844+
*/
845+
kit->data = bpf_mem_alloc(&bpf_global_ma, sizeof(struct bpf_iter_task_vma_kern_data));
846+
if (!kit->data)
847+
return -ENOMEM;
848+
849+
kit->data->task = get_task_struct(task);
850+
kit->data->mm = task->mm;
851+
if (!kit->data->mm) {
852+
err = -ENOENT;
853+
goto err_cleanup_iter;
854+
}
855+
856+
/* kit->data->work == NULL is valid after bpf_mmap_unlock_get_irq_work */
857+
irq_work_busy = bpf_mmap_unlock_get_irq_work(&kit->data->work);
858+
if (irq_work_busy || !mmap_read_trylock(kit->data->mm)) {
859+
err = -EBUSY;
860+
goto err_cleanup_iter;
861+
}
862+
863+
vma_iter_init(&kit->data->vmi, kit->data->mm, addr);
864+
return 0;
865+
866+
err_cleanup_iter:
867+
if (kit->data->task)
868+
put_task_struct(kit->data->task);
869+
bpf_mem_free(&bpf_global_ma, kit->data);
870+
/* NULL kit->data signals failed bpf_iter_task_vma initialization */
871+
kit->data = NULL;
872+
return err;
873+
}
874+
875+
__bpf_kfunc struct vm_area_struct *bpf_iter_task_vma_next(struct bpf_iter_task_vma *it)
876+
{
877+
struct bpf_iter_task_vma_kern *kit = (void *)it;
878+
879+
if (!kit->data) /* bpf_iter_task_vma_new failed */
880+
return NULL;
881+
return vma_next(&kit->data->vmi);
882+
}
883+
884+
__bpf_kfunc void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it)
885+
{
886+
struct bpf_iter_task_vma_kern *kit = (void *)it;
887+
888+
if (kit->data) {
889+
bpf_mmap_unlock_mm(kit->data->work, kit->data->mm);
890+
put_task_struct(kit->data->task);
891+
bpf_mem_free(&bpf_global_ma, kit->data);
892+
}
893+
}
894+
895+
__diag_pop();
896+
806897
DEFINE_PER_CPU(struct mmap_unlock_irq_work, mmap_unlock_work);
807898

808899
static void do_mmap_read_unlock(struct irq_work *entry)

0 commit comments

Comments
 (0)