1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2020 Facebook */
4 #include <linux/init.h>
5 #include <linux/namei.h>
6 #include <linux/pid_namespace.h>
8 #include <linux/fdtable.h>
9 #include <linux/filter.h>
10 #include <linux/bpf_mem_alloc.h>
11 #include <linux/btf_ids.h>
12 #include <linux/mm_types.h>
13 #include "mmap_unlock_work.h"
15 static const char * const iter_task_type_names[] = {
21 struct bpf_iter_seq_task_common {
22 struct pid_namespace *ns;
23 enum bpf_iter_task_type type;
28 struct bpf_iter_seq_task_info {
29 /* The first field must be struct bpf_iter_seq_task_common.
30 * this is assumed by {init, fini}_seq_pidns() callback functions.
32 struct bpf_iter_seq_task_common common;
36 static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_common *common,
38 bool skip_if_dup_files)
40 struct task_struct *task;
45 /* The first time, the iterator calls this function. */
46 pid = find_pid_ns(common->pid, common->ns);
47 task = get_pid_task(pid, PIDTYPE_TGID);
52 common->pid_visiting = common->pid;
57 /* If the control returns to user space and comes back to the
58 * kernel again, *tid and common->pid_visiting should be the
59 * same for task_seq_start() to pick up the correct task.
61 if (*tid == common->pid_visiting) {
62 pid = find_pid_ns(common->pid_visiting, common->ns);
63 task = get_pid_task(pid, PIDTYPE_PID);
68 task = find_task_by_pid_ns(common->pid_visiting, common->ns);
73 task = __next_thread(task);
77 next_tid = __task_pid_nr_ns(task, PIDTYPE_PID, common->ns);
81 if (skip_if_dup_files && task->files == task->group_leader->files)
84 *tid = common->pid_visiting = next_tid;
85 get_task_struct(task);
89 static struct task_struct *task_seq_get_next(struct bpf_iter_seq_task_common *common,
91 bool skip_if_dup_files)
93 struct task_struct *task = NULL;
96 if (common->type == BPF_TASK_ITER_TID) {
97 if (*tid && *tid != common->pid)
100 pid = find_pid_ns(common->pid, common->ns);
102 task = get_pid_task(pid, PIDTYPE_TGID);
110 if (common->type == BPF_TASK_ITER_TGID) {
112 task = task_group_seq_get_next(common, tid, skip_if_dup_files);
120 pid = find_ge_pid(*tid, common->ns);
122 *tid = pid_nr_ns(pid, common->ns);
123 task = get_pid_task(pid, PIDTYPE_PID);
127 } else if (skip_if_dup_files && !thread_group_leader(task) &&
128 task->files == task->group_leader->files) {
129 put_task_struct(task);
140 static void *task_seq_start(struct seq_file *seq, loff_t *pos)
142 struct bpf_iter_seq_task_info *info = seq->private;
143 struct task_struct *task;
145 task = task_seq_get_next(&info->common, &info->tid, false);
154 static void *task_seq_next(struct seq_file *seq, void *v, loff_t *pos)
156 struct bpf_iter_seq_task_info *info = seq->private;
157 struct task_struct *task;
161 put_task_struct((struct task_struct *)v);
162 task = task_seq_get_next(&info->common, &info->tid, false);
169 struct bpf_iter__task {
170 __bpf_md_ptr(struct bpf_iter_meta *, meta);
171 __bpf_md_ptr(struct task_struct *, task);
174 DEFINE_BPF_ITER_FUNC(task, struct bpf_iter_meta *meta, struct task_struct *task)
176 static int __task_seq_show(struct seq_file *seq, struct task_struct *task,
179 struct bpf_iter_meta meta;
180 struct bpf_iter__task ctx;
181 struct bpf_prog *prog;
184 prog = bpf_iter_get_info(&meta, in_stop);
190 return bpf_iter_run_prog(prog, &ctx);
193 static int task_seq_show(struct seq_file *seq, void *v)
195 return __task_seq_show(seq, v, false);
198 static void task_seq_stop(struct seq_file *seq, void *v)
201 (void)__task_seq_show(seq, v, true);
203 put_task_struct((struct task_struct *)v);
206 static int bpf_iter_attach_task(struct bpf_prog *prog,
207 union bpf_iter_link_info *linfo,
208 struct bpf_iter_aux_info *aux)
214 if ((!!linfo->task.tid + !!linfo->task.pid + !!linfo->task.pid_fd) > 1)
217 aux->task.type = BPF_TASK_ITER_ALL;
218 if (linfo->task.tid != 0) {
219 aux->task.type = BPF_TASK_ITER_TID;
220 aux->task.pid = linfo->task.tid;
222 if (linfo->task.pid != 0) {
223 aux->task.type = BPF_TASK_ITER_TGID;
224 aux->task.pid = linfo->task.pid;
226 if (linfo->task.pid_fd != 0) {
227 aux->task.type = BPF_TASK_ITER_TGID;
229 pid = pidfd_get_pid(linfo->task.pid_fd, &flags);
233 tgid = pid_nr_ns(pid, task_active_pid_ns(current));
234 aux->task.pid = tgid;
241 static const struct seq_operations task_seq_ops = {
242 .start = task_seq_start,
243 .next = task_seq_next,
244 .stop = task_seq_stop,
245 .show = task_seq_show,
248 struct bpf_iter_seq_task_file_info {
249 /* The first field must be struct bpf_iter_seq_task_common.
250 * this is assumed by {init, fini}_seq_pidns() callback functions.
252 struct bpf_iter_seq_task_common common;
253 struct task_struct *task;
259 task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info)
261 u32 saved_tid = info->tid;
262 struct task_struct *curr_task;
263 unsigned int curr_fd = info->fd;
265 /* If this function returns a non-NULL file object,
266 * it held a reference to the task/file.
267 * Otherwise, it does not hold any reference.
271 curr_task = info->task;
274 curr_task = task_seq_get_next(&info->common, &info->tid, true);
281 info->task = curr_task;
282 if (saved_tid == info->tid)
291 f = task_lookup_next_fdget_rcu(curr_task, &curr_fd);
301 /* the current task is done, go to the next task */
303 put_task_struct(curr_task);
305 if (info->common.type == BPF_TASK_ITER_TID) {
312 saved_tid = ++(info->tid);
316 static void *task_file_seq_start(struct seq_file *seq, loff_t *pos)
318 struct bpf_iter_seq_task_file_info *info = seq->private;
322 file = task_file_seq_get_next(info);
323 if (file && *pos == 0)
329 static void *task_file_seq_next(struct seq_file *seq, void *v, loff_t *pos)
331 struct bpf_iter_seq_task_file_info *info = seq->private;
335 fput((struct file *)v);
336 return task_file_seq_get_next(info);
339 struct bpf_iter__task_file {
340 __bpf_md_ptr(struct bpf_iter_meta *, meta);
341 __bpf_md_ptr(struct task_struct *, task);
343 __bpf_md_ptr(struct file *, file);
346 DEFINE_BPF_ITER_FUNC(task_file, struct bpf_iter_meta *meta,
347 struct task_struct *task, u32 fd,
350 static int __task_file_seq_show(struct seq_file *seq, struct file *file,
353 struct bpf_iter_seq_task_file_info *info = seq->private;
354 struct bpf_iter__task_file ctx;
355 struct bpf_iter_meta meta;
356 struct bpf_prog *prog;
359 prog = bpf_iter_get_info(&meta, in_stop);
364 ctx.task = info->task;
367 return bpf_iter_run_prog(prog, &ctx);
370 static int task_file_seq_show(struct seq_file *seq, void *v)
372 return __task_file_seq_show(seq, v, false);
375 static void task_file_seq_stop(struct seq_file *seq, void *v)
377 struct bpf_iter_seq_task_file_info *info = seq->private;
380 (void)__task_file_seq_show(seq, v, true);
382 fput((struct file *)v);
383 put_task_struct(info->task);
388 static int init_seq_pidns(void *priv_data, struct bpf_iter_aux_info *aux)
390 struct bpf_iter_seq_task_common *common = priv_data;
392 common->ns = get_pid_ns(task_active_pid_ns(current));
393 common->type = aux->task.type;
394 common->pid = aux->task.pid;
399 static void fini_seq_pidns(void *priv_data)
401 struct bpf_iter_seq_task_common *common = priv_data;
403 put_pid_ns(common->ns);
406 static const struct seq_operations task_file_seq_ops = {
407 .start = task_file_seq_start,
408 .next = task_file_seq_next,
409 .stop = task_file_seq_stop,
410 .show = task_file_seq_show,
413 struct bpf_iter_seq_task_vma_info {
414 /* The first field must be struct bpf_iter_seq_task_common.
415 * this is assumed by {init, fini}_seq_pidns() callback functions.
417 struct bpf_iter_seq_task_common common;
418 struct task_struct *task;
419 struct mm_struct *mm;
420 struct vm_area_struct *vma;
422 unsigned long prev_vm_start;
423 unsigned long prev_vm_end;
426 enum bpf_task_vma_iter_find_op {
427 task_vma_iter_first_vma, /* use find_vma() with addr 0 */
428 task_vma_iter_next_vma, /* use vma_next() with curr_vma */
429 task_vma_iter_find_vma, /* use find_vma() to find next vma */
432 static struct vm_area_struct *
433 task_vma_seq_get_next(struct bpf_iter_seq_task_vma_info *info)
435 enum bpf_task_vma_iter_find_op op;
436 struct vm_area_struct *curr_vma;
437 struct task_struct *curr_task;
438 struct mm_struct *curr_mm;
439 u32 saved_tid = info->tid;
441 /* If this function returns a non-NULL vma, it holds a reference to
442 * the task_struct, holds a refcount on mm->mm_users, and holds
443 * read lock on vma->mm->mmap_lock.
444 * If this function returns NULL, it does not hold any reference or
448 curr_task = info->task;
449 curr_vma = info->vma;
451 /* In case of lock contention, drop mmap_lock to unblock
454 * After relock, call find(mm, prev_vm_end - 1) to find
455 * new vma to process.
457 * +------+------+-----------+
458 * | VMA1 | VMA2 | VMA3 |
459 * +------+------+-----------+
463 * For example, curr_vma == VMA2. Before unlock, we set
468 * There are a few cases:
470 * 1) VMA2 is freed, but VMA3 exists.
472 * find_vma() will return VMA3, just process VMA3.
474 * 2) VMA2 still exists.
476 * find_vma() will return VMA2, process VMA2->next.
478 * 3) no more vma in this mm.
480 * Process the next task.
482 * 4) find_vma() returns a different vma, VMA2'.
484 * 4.1) If VMA2 covers same range as VMA2', skip VMA2',
485 * because we already covered the range;
486 * 4.2) VMA2 and VMA2' covers different ranges, process
489 if (mmap_lock_is_contended(curr_mm)) {
490 info->prev_vm_start = curr_vma->vm_start;
491 info->prev_vm_end = curr_vma->vm_end;
492 op = task_vma_iter_find_vma;
493 mmap_read_unlock(curr_mm);
494 if (mmap_read_lock_killable(curr_mm)) {
499 op = task_vma_iter_next_vma;
503 curr_task = task_seq_get_next(&info->common, &info->tid, true);
509 if (saved_tid != info->tid) {
510 /* new task, process the first vma */
511 op = task_vma_iter_first_vma;
513 /* Found the same tid, which means the user space
514 * finished data in previous buffer and read more.
515 * We dropped mmap_lock before returning to user
516 * space, so it is necessary to use find_vma() to
517 * find the next vma to process.
519 op = task_vma_iter_find_vma;
522 curr_mm = get_task_mm(curr_task);
526 if (mmap_read_lock_killable(curr_mm)) {
533 case task_vma_iter_first_vma:
534 curr_vma = find_vma(curr_mm, 0);
536 case task_vma_iter_next_vma:
537 curr_vma = find_vma(curr_mm, curr_vma->vm_end);
539 case task_vma_iter_find_vma:
540 /* We dropped mmap_lock so it is necessary to use find_vma
541 * to find the next vma. This is similar to the mechanism
542 * in show_smaps_rollup().
544 curr_vma = find_vma(curr_mm, info->prev_vm_end - 1);
545 /* case 1) and 4.2) above just use curr_vma */
547 /* check for case 2) or case 4.1) above */
549 curr_vma->vm_start == info->prev_vm_start &&
550 curr_vma->vm_end == info->prev_vm_end)
551 curr_vma = find_vma(curr_mm, curr_vma->vm_end);
555 /* case 3) above, or case 2) 4.1) with vma->next == NULL */
556 mmap_read_unlock(curr_mm);
560 info->task = curr_task;
561 info->vma = curr_vma;
566 if (info->common.type == BPF_TASK_ITER_TID)
569 put_task_struct(curr_task);
577 put_task_struct(curr_task);
584 static void *task_vma_seq_start(struct seq_file *seq, loff_t *pos)
586 struct bpf_iter_seq_task_vma_info *info = seq->private;
587 struct vm_area_struct *vma;
589 vma = task_vma_seq_get_next(info);
590 if (vma && *pos == 0)
596 static void *task_vma_seq_next(struct seq_file *seq, void *v, loff_t *pos)
598 struct bpf_iter_seq_task_vma_info *info = seq->private;
601 return task_vma_seq_get_next(info);
604 struct bpf_iter__task_vma {
605 __bpf_md_ptr(struct bpf_iter_meta *, meta);
606 __bpf_md_ptr(struct task_struct *, task);
607 __bpf_md_ptr(struct vm_area_struct *, vma);
610 DEFINE_BPF_ITER_FUNC(task_vma, struct bpf_iter_meta *meta,
611 struct task_struct *task, struct vm_area_struct *vma)
613 static int __task_vma_seq_show(struct seq_file *seq, bool in_stop)
615 struct bpf_iter_seq_task_vma_info *info = seq->private;
616 struct bpf_iter__task_vma ctx;
617 struct bpf_iter_meta meta;
618 struct bpf_prog *prog;
621 prog = bpf_iter_get_info(&meta, in_stop);
626 ctx.task = info->task;
628 return bpf_iter_run_prog(prog, &ctx);
631 static int task_vma_seq_show(struct seq_file *seq, void *v)
633 return __task_vma_seq_show(seq, false);
636 static void task_vma_seq_stop(struct seq_file *seq, void *v)
638 struct bpf_iter_seq_task_vma_info *info = seq->private;
641 (void)__task_vma_seq_show(seq, true);
643 /* info->vma has not been seen by the BPF program. If the
644 * user space reads more, task_vma_seq_get_next should
645 * return this vma again. Set prev_vm_start to ~0UL,
646 * so that we don't skip the vma returned by the next
647 * find_vma() (case task_vma_iter_find_vma in
648 * task_vma_seq_get_next()).
650 info->prev_vm_start = ~0UL;
651 info->prev_vm_end = info->vma->vm_end;
652 mmap_read_unlock(info->mm);
655 put_task_struct(info->task);
660 static const struct seq_operations task_vma_seq_ops = {
661 .start = task_vma_seq_start,
662 .next = task_vma_seq_next,
663 .stop = task_vma_seq_stop,
664 .show = task_vma_seq_show,
667 static const struct bpf_iter_seq_info task_seq_info = {
668 .seq_ops = &task_seq_ops,
669 .init_seq_private = init_seq_pidns,
670 .fini_seq_private = fini_seq_pidns,
671 .seq_priv_size = sizeof(struct bpf_iter_seq_task_info),
674 static int bpf_iter_fill_link_info(const struct bpf_iter_aux_info *aux, struct bpf_link_info *info)
676 switch (aux->task.type) {
677 case BPF_TASK_ITER_TID:
678 info->iter.task.tid = aux->task.pid;
680 case BPF_TASK_ITER_TGID:
681 info->iter.task.pid = aux->task.pid;
689 static void bpf_iter_task_show_fdinfo(const struct bpf_iter_aux_info *aux, struct seq_file *seq)
691 seq_printf(seq, "task_type:\t%s\n", iter_task_type_names[aux->task.type]);
692 if (aux->task.type == BPF_TASK_ITER_TID)
693 seq_printf(seq, "tid:\t%u\n", aux->task.pid);
694 else if (aux->task.type == BPF_TASK_ITER_TGID)
695 seq_printf(seq, "pid:\t%u\n", aux->task.pid);
698 static struct bpf_iter_reg task_reg_info = {
700 .attach_target = bpf_iter_attach_task,
701 .feature = BPF_ITER_RESCHED,
702 .ctx_arg_info_size = 1,
704 { offsetof(struct bpf_iter__task, task),
705 PTR_TO_BTF_ID_OR_NULL | PTR_TRUSTED },
707 .seq_info = &task_seq_info,
708 .fill_link_info = bpf_iter_fill_link_info,
709 .show_fdinfo = bpf_iter_task_show_fdinfo,
712 static const struct bpf_iter_seq_info task_file_seq_info = {
713 .seq_ops = &task_file_seq_ops,
714 .init_seq_private = init_seq_pidns,
715 .fini_seq_private = fini_seq_pidns,
716 .seq_priv_size = sizeof(struct bpf_iter_seq_task_file_info),
719 static struct bpf_iter_reg task_file_reg_info = {
720 .target = "task_file",
721 .attach_target = bpf_iter_attach_task,
722 .feature = BPF_ITER_RESCHED,
723 .ctx_arg_info_size = 2,
725 { offsetof(struct bpf_iter__task_file, task),
726 PTR_TO_BTF_ID_OR_NULL },
727 { offsetof(struct bpf_iter__task_file, file),
728 PTR_TO_BTF_ID_OR_NULL },
730 .seq_info = &task_file_seq_info,
731 .fill_link_info = bpf_iter_fill_link_info,
732 .show_fdinfo = bpf_iter_task_show_fdinfo,
735 static const struct bpf_iter_seq_info task_vma_seq_info = {
736 .seq_ops = &task_vma_seq_ops,
737 .init_seq_private = init_seq_pidns,
738 .fini_seq_private = fini_seq_pidns,
739 .seq_priv_size = sizeof(struct bpf_iter_seq_task_vma_info),
742 static struct bpf_iter_reg task_vma_reg_info = {
743 .target = "task_vma",
744 .attach_target = bpf_iter_attach_task,
745 .feature = BPF_ITER_RESCHED,
746 .ctx_arg_info_size = 2,
748 { offsetof(struct bpf_iter__task_vma, task),
749 PTR_TO_BTF_ID_OR_NULL },
750 { offsetof(struct bpf_iter__task_vma, vma),
751 PTR_TO_BTF_ID_OR_NULL },
753 .seq_info = &task_vma_seq_info,
754 .fill_link_info = bpf_iter_fill_link_info,
755 .show_fdinfo = bpf_iter_task_show_fdinfo,
758 BPF_CALL_5(bpf_find_vma, struct task_struct *, task, u64, start,
759 bpf_callback_t, callback_fn, void *, callback_ctx, u64, flags)
761 struct mmap_unlock_irq_work *work = NULL;
762 struct vm_area_struct *vma;
763 bool irq_work_busy = false;
764 struct mm_struct *mm;
777 irq_work_busy = bpf_mmap_unlock_get_irq_work(&work);
779 if (irq_work_busy || !mmap_read_trylock(mm))
782 vma = find_vma(mm, start);
784 if (vma && vma->vm_start <= start && vma->vm_end > start) {
785 callback_fn((u64)(long)task, (u64)(long)vma,
786 (u64)(long)callback_ctx, 0, 0);
789 bpf_mmap_unlock_mm(work, mm);
793 const struct bpf_func_proto bpf_find_vma_proto = {
794 .func = bpf_find_vma,
795 .ret_type = RET_INTEGER,
796 .arg1_type = ARG_PTR_TO_BTF_ID,
797 .arg1_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
798 .arg2_type = ARG_ANYTHING,
799 .arg3_type = ARG_PTR_TO_FUNC,
800 .arg4_type = ARG_PTR_TO_STACK_OR_NULL,
801 .arg5_type = ARG_ANYTHING,
804 struct bpf_iter_task_vma_kern_data {
805 struct task_struct *task;
806 struct mm_struct *mm;
807 struct mmap_unlock_irq_work *work;
808 struct vma_iterator vmi;
811 struct bpf_iter_task_vma {
812 /* opaque iterator state; having __u64 here allows to preserve correct
813 * alignment requirements in vmlinux.h, generated from BTF
816 } __attribute__((aligned(8)));
818 /* Non-opaque version of bpf_iter_task_vma */
819 struct bpf_iter_task_vma_kern {
820 struct bpf_iter_task_vma_kern_data *data;
821 } __attribute__((aligned(8)));
823 __bpf_kfunc_start_defs();
825 __bpf_kfunc int bpf_iter_task_vma_new(struct bpf_iter_task_vma *it,
826 struct task_struct *task, u64 addr)
828 struct bpf_iter_task_vma_kern *kit = (void *)it;
829 bool irq_work_busy = false;
832 BUILD_BUG_ON(sizeof(struct bpf_iter_task_vma_kern) != sizeof(struct bpf_iter_task_vma));
833 BUILD_BUG_ON(__alignof__(struct bpf_iter_task_vma_kern) != __alignof__(struct bpf_iter_task_vma));
835 /* is_iter_reg_valid_uninit guarantees that kit hasn't been initialized
836 * before, so non-NULL kit->data doesn't point to previously
837 * bpf_mem_alloc'd bpf_iter_task_vma_kern_data
839 kit->data = bpf_mem_alloc(&bpf_global_ma, sizeof(struct bpf_iter_task_vma_kern_data));
843 kit->data->task = get_task_struct(task);
844 kit->data->mm = task->mm;
845 if (!kit->data->mm) {
847 goto err_cleanup_iter;
850 /* kit->data->work == NULL is valid after bpf_mmap_unlock_get_irq_work */
851 irq_work_busy = bpf_mmap_unlock_get_irq_work(&kit->data->work);
852 if (irq_work_busy || !mmap_read_trylock(kit->data->mm)) {
854 goto err_cleanup_iter;
857 vma_iter_init(&kit->data->vmi, kit->data->mm, addr);
862 put_task_struct(kit->data->task);
863 bpf_mem_free(&bpf_global_ma, kit->data);
864 /* NULL kit->data signals failed bpf_iter_task_vma initialization */
869 __bpf_kfunc struct vm_area_struct *bpf_iter_task_vma_next(struct bpf_iter_task_vma *it)
871 struct bpf_iter_task_vma_kern *kit = (void *)it;
873 if (!kit->data) /* bpf_iter_task_vma_new failed */
875 return vma_next(&kit->data->vmi);
878 __bpf_kfunc void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it)
880 struct bpf_iter_task_vma_kern *kit = (void *)it;
883 bpf_mmap_unlock_mm(kit->data->work, kit->data->mm);
884 put_task_struct(kit->data->task);
885 bpf_mem_free(&bpf_global_ma, kit->data);
889 __bpf_kfunc_end_defs();
891 #ifdef CONFIG_CGROUPS
893 struct bpf_iter_css_task {
895 } __attribute__((aligned(8)));
897 struct bpf_iter_css_task_kern {
898 struct css_task_iter *css_it;
899 } __attribute__((aligned(8)));
901 __bpf_kfunc_start_defs();
903 __bpf_kfunc int bpf_iter_css_task_new(struct bpf_iter_css_task *it,
904 struct cgroup_subsys_state *css, unsigned int flags)
906 struct bpf_iter_css_task_kern *kit = (void *)it;
908 BUILD_BUG_ON(sizeof(struct bpf_iter_css_task_kern) != sizeof(struct bpf_iter_css_task));
909 BUILD_BUG_ON(__alignof__(struct bpf_iter_css_task_kern) !=
910 __alignof__(struct bpf_iter_css_task));
913 case CSS_TASK_ITER_PROCS | CSS_TASK_ITER_THREADED:
914 case CSS_TASK_ITER_PROCS:
921 kit->css_it = bpf_mem_alloc(&bpf_global_ma, sizeof(struct css_task_iter));
924 css_task_iter_start(css, flags, kit->css_it);
928 __bpf_kfunc struct task_struct *bpf_iter_css_task_next(struct bpf_iter_css_task *it)
930 struct bpf_iter_css_task_kern *kit = (void *)it;
934 return css_task_iter_next(kit->css_it);
937 __bpf_kfunc void bpf_iter_css_task_destroy(struct bpf_iter_css_task *it)
939 struct bpf_iter_css_task_kern *kit = (void *)it;
943 css_task_iter_end(kit->css_it);
944 bpf_mem_free(&bpf_global_ma, kit->css_it);
947 __bpf_kfunc_end_defs();
949 #endif /* CONFIG_CGROUPS */
951 struct bpf_iter_task {
953 } __attribute__((aligned(8)));
955 struct bpf_iter_task_kern {
956 struct task_struct *task;
957 struct task_struct *pos;
959 } __attribute__((aligned(8)));
962 /* all process in the system */
963 BPF_TASK_ITER_ALL_PROCS,
964 /* all threads in the system */
965 BPF_TASK_ITER_ALL_THREADS,
966 /* all threads of a specific process */
967 BPF_TASK_ITER_PROC_THREADS
970 __bpf_kfunc_start_defs();
972 __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it,
973 struct task_struct *task__nullable, unsigned int flags)
975 struct bpf_iter_task_kern *kit = (void *)it;
977 BUILD_BUG_ON(sizeof(struct bpf_iter_task_kern) > sizeof(struct bpf_iter_task));
978 BUILD_BUG_ON(__alignof__(struct bpf_iter_task_kern) !=
979 __alignof__(struct bpf_iter_task));
982 case BPF_TASK_ITER_ALL_THREADS:
983 case BPF_TASK_ITER_ALL_PROCS:
985 case BPF_TASK_ITER_PROC_THREADS:
993 if (flags == BPF_TASK_ITER_PROC_THREADS)
994 kit->task = task__nullable;
996 kit->task = &init_task;
997 kit->pos = kit->task;
1002 __bpf_kfunc struct task_struct *bpf_iter_task_next(struct bpf_iter_task *it)
1004 struct bpf_iter_task_kern *kit = (void *)it;
1005 struct task_struct *pos;
1014 if (flags == BPF_TASK_ITER_ALL_PROCS)
1017 kit->pos = __next_thread(kit->pos);
1018 if (kit->pos || flags == BPF_TASK_ITER_PROC_THREADS)
1022 kit->task = next_task(kit->task);
1023 if (kit->task == &init_task)
1026 kit->pos = kit->task;
1031 __bpf_kfunc void bpf_iter_task_destroy(struct bpf_iter_task *it)
1035 __bpf_kfunc_end_defs();
1037 DEFINE_PER_CPU(struct mmap_unlock_irq_work, mmap_unlock_work);
1039 static void do_mmap_read_unlock(struct irq_work *entry)
1041 struct mmap_unlock_irq_work *work;
1043 if (WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_RT)))
1046 work = container_of(entry, struct mmap_unlock_irq_work, irq_work);
1047 mmap_read_unlock_non_owner(work->mm);
1050 static int __init task_iter_init(void)
1052 struct mmap_unlock_irq_work *work;
1055 for_each_possible_cpu(cpu) {
1056 work = per_cpu_ptr(&mmap_unlock_work, cpu);
1057 init_irq_work(&work->irq_work, do_mmap_read_unlock);
1060 task_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
1061 ret = bpf_iter_reg_target(&task_reg_info);
1065 task_file_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
1066 task_file_reg_info.ctx_arg_info[1].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_FILE];
1067 ret = bpf_iter_reg_target(&task_file_reg_info);
1071 task_vma_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
1072 task_vma_reg_info.ctx_arg_info[1].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_VMA];
1073 return bpf_iter_reg_target(&task_vma_reg_info);
1075 late_initcall(task_iter_init);