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);
75 next_tid = __task_pid_nr_ns(task, PIDTYPE_PID, common->ns);
76 if (!next_tid || next_tid == common->pid) {
77 /* Run out of tasks of a process. The tasks of a
78 * thread_group are linked as circular linked list.
83 if (skip_if_dup_files && task->files == task->group_leader->files)
86 *tid = common->pid_visiting = next_tid;
87 get_task_struct(task);
91 static struct task_struct *task_seq_get_next(struct bpf_iter_seq_task_common *common,
93 bool skip_if_dup_files)
95 struct task_struct *task = NULL;
98 if (common->type == BPF_TASK_ITER_TID) {
99 if (*tid && *tid != common->pid)
102 pid = find_pid_ns(common->pid, common->ns);
104 task = get_pid_task(pid, PIDTYPE_TGID);
112 if (common->type == BPF_TASK_ITER_TGID) {
114 task = task_group_seq_get_next(common, tid, skip_if_dup_files);
122 pid = find_ge_pid(*tid, common->ns);
124 *tid = pid_nr_ns(pid, common->ns);
125 task = get_pid_task(pid, PIDTYPE_PID);
129 } else if (skip_if_dup_files && !thread_group_leader(task) &&
130 task->files == task->group_leader->files) {
131 put_task_struct(task);
142 static void *task_seq_start(struct seq_file *seq, loff_t *pos)
144 struct bpf_iter_seq_task_info *info = seq->private;
145 struct task_struct *task;
147 task = task_seq_get_next(&info->common, &info->tid, false);
156 static void *task_seq_next(struct seq_file *seq, void *v, loff_t *pos)
158 struct bpf_iter_seq_task_info *info = seq->private;
159 struct task_struct *task;
163 put_task_struct((struct task_struct *)v);
164 task = task_seq_get_next(&info->common, &info->tid, false);
171 struct bpf_iter__task {
172 __bpf_md_ptr(struct bpf_iter_meta *, meta);
173 __bpf_md_ptr(struct task_struct *, task);
176 DEFINE_BPF_ITER_FUNC(task, struct bpf_iter_meta *meta, struct task_struct *task)
178 static int __task_seq_show(struct seq_file *seq, struct task_struct *task,
181 struct bpf_iter_meta meta;
182 struct bpf_iter__task ctx;
183 struct bpf_prog *prog;
186 prog = bpf_iter_get_info(&meta, in_stop);
192 return bpf_iter_run_prog(prog, &ctx);
195 static int task_seq_show(struct seq_file *seq, void *v)
197 return __task_seq_show(seq, v, false);
200 static void task_seq_stop(struct seq_file *seq, void *v)
203 (void)__task_seq_show(seq, v, true);
205 put_task_struct((struct task_struct *)v);
208 static int bpf_iter_attach_task(struct bpf_prog *prog,
209 union bpf_iter_link_info *linfo,
210 struct bpf_iter_aux_info *aux)
216 if ((!!linfo->task.tid + !!linfo->task.pid + !!linfo->task.pid_fd) > 1)
219 aux->task.type = BPF_TASK_ITER_ALL;
220 if (linfo->task.tid != 0) {
221 aux->task.type = BPF_TASK_ITER_TID;
222 aux->task.pid = linfo->task.tid;
224 if (linfo->task.pid != 0) {
225 aux->task.type = BPF_TASK_ITER_TGID;
226 aux->task.pid = linfo->task.pid;
228 if (linfo->task.pid_fd != 0) {
229 aux->task.type = BPF_TASK_ITER_TGID;
231 pid = pidfd_get_pid(linfo->task.pid_fd, &flags);
235 tgid = pid_nr_ns(pid, task_active_pid_ns(current));
236 aux->task.pid = tgid;
243 static const struct seq_operations task_seq_ops = {
244 .start = task_seq_start,
245 .next = task_seq_next,
246 .stop = task_seq_stop,
247 .show = task_seq_show,
250 struct bpf_iter_seq_task_file_info {
251 /* The first field must be struct bpf_iter_seq_task_common.
252 * this is assumed by {init, fini}_seq_pidns() callback functions.
254 struct bpf_iter_seq_task_common common;
255 struct task_struct *task;
261 task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info)
263 u32 saved_tid = info->tid;
264 struct task_struct *curr_task;
265 unsigned int curr_fd = info->fd;
267 /* If this function returns a non-NULL file object,
268 * it held a reference to the task/file.
269 * Otherwise, it does not hold any reference.
273 curr_task = info->task;
276 curr_task = task_seq_get_next(&info->common, &info->tid, true);
283 info->task = curr_task;
284 if (saved_tid == info->tid)
293 f = task_lookup_next_fdget_rcu(curr_task, &curr_fd);
303 /* the current task is done, go to the next task */
305 put_task_struct(curr_task);
307 if (info->common.type == BPF_TASK_ITER_TID) {
314 saved_tid = ++(info->tid);
318 static void *task_file_seq_start(struct seq_file *seq, loff_t *pos)
320 struct bpf_iter_seq_task_file_info *info = seq->private;
324 file = task_file_seq_get_next(info);
325 if (file && *pos == 0)
331 static void *task_file_seq_next(struct seq_file *seq, void *v, loff_t *pos)
333 struct bpf_iter_seq_task_file_info *info = seq->private;
337 fput((struct file *)v);
338 return task_file_seq_get_next(info);
341 struct bpf_iter__task_file {
342 __bpf_md_ptr(struct bpf_iter_meta *, meta);
343 __bpf_md_ptr(struct task_struct *, task);
345 __bpf_md_ptr(struct file *, file);
348 DEFINE_BPF_ITER_FUNC(task_file, struct bpf_iter_meta *meta,
349 struct task_struct *task, u32 fd,
352 static int __task_file_seq_show(struct seq_file *seq, struct file *file,
355 struct bpf_iter_seq_task_file_info *info = seq->private;
356 struct bpf_iter__task_file ctx;
357 struct bpf_iter_meta meta;
358 struct bpf_prog *prog;
361 prog = bpf_iter_get_info(&meta, in_stop);
366 ctx.task = info->task;
369 return bpf_iter_run_prog(prog, &ctx);
372 static int task_file_seq_show(struct seq_file *seq, void *v)
374 return __task_file_seq_show(seq, v, false);
377 static void task_file_seq_stop(struct seq_file *seq, void *v)
379 struct bpf_iter_seq_task_file_info *info = seq->private;
382 (void)__task_file_seq_show(seq, v, true);
384 fput((struct file *)v);
385 put_task_struct(info->task);
390 static int init_seq_pidns(void *priv_data, struct bpf_iter_aux_info *aux)
392 struct bpf_iter_seq_task_common *common = priv_data;
394 common->ns = get_pid_ns(task_active_pid_ns(current));
395 common->type = aux->task.type;
396 common->pid = aux->task.pid;
401 static void fini_seq_pidns(void *priv_data)
403 struct bpf_iter_seq_task_common *common = priv_data;
405 put_pid_ns(common->ns);
408 static const struct seq_operations task_file_seq_ops = {
409 .start = task_file_seq_start,
410 .next = task_file_seq_next,
411 .stop = task_file_seq_stop,
412 .show = task_file_seq_show,
415 struct bpf_iter_seq_task_vma_info {
416 /* The first field must be struct bpf_iter_seq_task_common.
417 * this is assumed by {init, fini}_seq_pidns() callback functions.
419 struct bpf_iter_seq_task_common common;
420 struct task_struct *task;
421 struct mm_struct *mm;
422 struct vm_area_struct *vma;
424 unsigned long prev_vm_start;
425 unsigned long prev_vm_end;
428 enum bpf_task_vma_iter_find_op {
429 task_vma_iter_first_vma, /* use find_vma() with addr 0 */
430 task_vma_iter_next_vma, /* use vma_next() with curr_vma */
431 task_vma_iter_find_vma, /* use find_vma() to find next vma */
434 static struct vm_area_struct *
435 task_vma_seq_get_next(struct bpf_iter_seq_task_vma_info *info)
437 enum bpf_task_vma_iter_find_op op;
438 struct vm_area_struct *curr_vma;
439 struct task_struct *curr_task;
440 struct mm_struct *curr_mm;
441 u32 saved_tid = info->tid;
443 /* If this function returns a non-NULL vma, it holds a reference to
444 * the task_struct, holds a refcount on mm->mm_users, and holds
445 * read lock on vma->mm->mmap_lock.
446 * If this function returns NULL, it does not hold any reference or
450 curr_task = info->task;
451 curr_vma = info->vma;
453 /* In case of lock contention, drop mmap_lock to unblock
456 * After relock, call find(mm, prev_vm_end - 1) to find
457 * new vma to process.
459 * +------+------+-----------+
460 * | VMA1 | VMA2 | VMA3 |
461 * +------+------+-----------+
465 * For example, curr_vma == VMA2. Before unlock, we set
470 * There are a few cases:
472 * 1) VMA2 is freed, but VMA3 exists.
474 * find_vma() will return VMA3, just process VMA3.
476 * 2) VMA2 still exists.
478 * find_vma() will return VMA2, process VMA2->next.
480 * 3) no more vma in this mm.
482 * Process the next task.
484 * 4) find_vma() returns a different vma, VMA2'.
486 * 4.1) If VMA2 covers same range as VMA2', skip VMA2',
487 * because we already covered the range;
488 * 4.2) VMA2 and VMA2' covers different ranges, process
491 if (mmap_lock_is_contended(curr_mm)) {
492 info->prev_vm_start = curr_vma->vm_start;
493 info->prev_vm_end = curr_vma->vm_end;
494 op = task_vma_iter_find_vma;
495 mmap_read_unlock(curr_mm);
496 if (mmap_read_lock_killable(curr_mm)) {
501 op = task_vma_iter_next_vma;
505 curr_task = task_seq_get_next(&info->common, &info->tid, true);
511 if (saved_tid != info->tid) {
512 /* new task, process the first vma */
513 op = task_vma_iter_first_vma;
515 /* Found the same tid, which means the user space
516 * finished data in previous buffer and read more.
517 * We dropped mmap_lock before returning to user
518 * space, so it is necessary to use find_vma() to
519 * find the next vma to process.
521 op = task_vma_iter_find_vma;
524 curr_mm = get_task_mm(curr_task);
528 if (mmap_read_lock_killable(curr_mm)) {
535 case task_vma_iter_first_vma:
536 curr_vma = find_vma(curr_mm, 0);
538 case task_vma_iter_next_vma:
539 curr_vma = find_vma(curr_mm, curr_vma->vm_end);
541 case task_vma_iter_find_vma:
542 /* We dropped mmap_lock so it is necessary to use find_vma
543 * to find the next vma. This is similar to the mechanism
544 * in show_smaps_rollup().
546 curr_vma = find_vma(curr_mm, info->prev_vm_end - 1);
547 /* case 1) and 4.2) above just use curr_vma */
549 /* check for case 2) or case 4.1) above */
551 curr_vma->vm_start == info->prev_vm_start &&
552 curr_vma->vm_end == info->prev_vm_end)
553 curr_vma = find_vma(curr_mm, curr_vma->vm_end);
557 /* case 3) above, or case 2) 4.1) with vma->next == NULL */
558 mmap_read_unlock(curr_mm);
562 info->task = curr_task;
563 info->vma = curr_vma;
568 if (info->common.type == BPF_TASK_ITER_TID)
571 put_task_struct(curr_task);
579 put_task_struct(curr_task);
586 static void *task_vma_seq_start(struct seq_file *seq, loff_t *pos)
588 struct bpf_iter_seq_task_vma_info *info = seq->private;
589 struct vm_area_struct *vma;
591 vma = task_vma_seq_get_next(info);
592 if (vma && *pos == 0)
598 static void *task_vma_seq_next(struct seq_file *seq, void *v, loff_t *pos)
600 struct bpf_iter_seq_task_vma_info *info = seq->private;
603 return task_vma_seq_get_next(info);
606 struct bpf_iter__task_vma {
607 __bpf_md_ptr(struct bpf_iter_meta *, meta);
608 __bpf_md_ptr(struct task_struct *, task);
609 __bpf_md_ptr(struct vm_area_struct *, vma);
612 DEFINE_BPF_ITER_FUNC(task_vma, struct bpf_iter_meta *meta,
613 struct task_struct *task, struct vm_area_struct *vma)
615 static int __task_vma_seq_show(struct seq_file *seq, bool in_stop)
617 struct bpf_iter_seq_task_vma_info *info = seq->private;
618 struct bpf_iter__task_vma ctx;
619 struct bpf_iter_meta meta;
620 struct bpf_prog *prog;
623 prog = bpf_iter_get_info(&meta, in_stop);
628 ctx.task = info->task;
630 return bpf_iter_run_prog(prog, &ctx);
633 static int task_vma_seq_show(struct seq_file *seq, void *v)
635 return __task_vma_seq_show(seq, false);
638 static void task_vma_seq_stop(struct seq_file *seq, void *v)
640 struct bpf_iter_seq_task_vma_info *info = seq->private;
643 (void)__task_vma_seq_show(seq, true);
645 /* info->vma has not been seen by the BPF program. If the
646 * user space reads more, task_vma_seq_get_next should
647 * return this vma again. Set prev_vm_start to ~0UL,
648 * so that we don't skip the vma returned by the next
649 * find_vma() (case task_vma_iter_find_vma in
650 * task_vma_seq_get_next()).
652 info->prev_vm_start = ~0UL;
653 info->prev_vm_end = info->vma->vm_end;
654 mmap_read_unlock(info->mm);
657 put_task_struct(info->task);
662 static const struct seq_operations task_vma_seq_ops = {
663 .start = task_vma_seq_start,
664 .next = task_vma_seq_next,
665 .stop = task_vma_seq_stop,
666 .show = task_vma_seq_show,
669 static const struct bpf_iter_seq_info task_seq_info = {
670 .seq_ops = &task_seq_ops,
671 .init_seq_private = init_seq_pidns,
672 .fini_seq_private = fini_seq_pidns,
673 .seq_priv_size = sizeof(struct bpf_iter_seq_task_info),
676 static int bpf_iter_fill_link_info(const struct bpf_iter_aux_info *aux, struct bpf_link_info *info)
678 switch (aux->task.type) {
679 case BPF_TASK_ITER_TID:
680 info->iter.task.tid = aux->task.pid;
682 case BPF_TASK_ITER_TGID:
683 info->iter.task.pid = aux->task.pid;
691 static void bpf_iter_task_show_fdinfo(const struct bpf_iter_aux_info *aux, struct seq_file *seq)
693 seq_printf(seq, "task_type:\t%s\n", iter_task_type_names[aux->task.type]);
694 if (aux->task.type == BPF_TASK_ITER_TID)
695 seq_printf(seq, "tid:\t%u\n", aux->task.pid);
696 else if (aux->task.type == BPF_TASK_ITER_TGID)
697 seq_printf(seq, "pid:\t%u\n", aux->task.pid);
700 static struct bpf_iter_reg task_reg_info = {
702 .attach_target = bpf_iter_attach_task,
703 .feature = BPF_ITER_RESCHED,
704 .ctx_arg_info_size = 1,
706 { offsetof(struct bpf_iter__task, task),
707 PTR_TO_BTF_ID_OR_NULL },
709 .seq_info = &task_seq_info,
710 .fill_link_info = bpf_iter_fill_link_info,
711 .show_fdinfo = bpf_iter_task_show_fdinfo,
714 static const struct bpf_iter_seq_info task_file_seq_info = {
715 .seq_ops = &task_file_seq_ops,
716 .init_seq_private = init_seq_pidns,
717 .fini_seq_private = fini_seq_pidns,
718 .seq_priv_size = sizeof(struct bpf_iter_seq_task_file_info),
721 static struct bpf_iter_reg task_file_reg_info = {
722 .target = "task_file",
723 .attach_target = bpf_iter_attach_task,
724 .feature = BPF_ITER_RESCHED,
725 .ctx_arg_info_size = 2,
727 { offsetof(struct bpf_iter__task_file, task),
728 PTR_TO_BTF_ID_OR_NULL },
729 { offsetof(struct bpf_iter__task_file, file),
730 PTR_TO_BTF_ID_OR_NULL },
732 .seq_info = &task_file_seq_info,
733 .fill_link_info = bpf_iter_fill_link_info,
734 .show_fdinfo = bpf_iter_task_show_fdinfo,
737 static const struct bpf_iter_seq_info task_vma_seq_info = {
738 .seq_ops = &task_vma_seq_ops,
739 .init_seq_private = init_seq_pidns,
740 .fini_seq_private = fini_seq_pidns,
741 .seq_priv_size = sizeof(struct bpf_iter_seq_task_vma_info),
744 static struct bpf_iter_reg task_vma_reg_info = {
745 .target = "task_vma",
746 .attach_target = bpf_iter_attach_task,
747 .feature = BPF_ITER_RESCHED,
748 .ctx_arg_info_size = 2,
750 { offsetof(struct bpf_iter__task_vma, task),
751 PTR_TO_BTF_ID_OR_NULL },
752 { offsetof(struct bpf_iter__task_vma, vma),
753 PTR_TO_BTF_ID_OR_NULL },
755 .seq_info = &task_vma_seq_info,
756 .fill_link_info = bpf_iter_fill_link_info,
757 .show_fdinfo = bpf_iter_task_show_fdinfo,
760 BPF_CALL_5(bpf_find_vma, struct task_struct *, task, u64, start,
761 bpf_callback_t, callback_fn, void *, callback_ctx, u64, flags)
763 struct mmap_unlock_irq_work *work = NULL;
764 struct vm_area_struct *vma;
765 bool irq_work_busy = false;
766 struct mm_struct *mm;
779 irq_work_busy = bpf_mmap_unlock_get_irq_work(&work);
781 if (irq_work_busy || !mmap_read_trylock(mm))
784 vma = find_vma(mm, start);
786 if (vma && vma->vm_start <= start && vma->vm_end > start) {
787 callback_fn((u64)(long)task, (u64)(long)vma,
788 (u64)(long)callback_ctx, 0, 0);
791 bpf_mmap_unlock_mm(work, mm);
795 const struct bpf_func_proto bpf_find_vma_proto = {
796 .func = bpf_find_vma,
797 .ret_type = RET_INTEGER,
798 .arg1_type = ARG_PTR_TO_BTF_ID,
799 .arg1_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
800 .arg2_type = ARG_ANYTHING,
801 .arg3_type = ARG_PTR_TO_FUNC,
802 .arg4_type = ARG_PTR_TO_STACK_OR_NULL,
803 .arg5_type = ARG_ANYTHING,
806 struct bpf_iter_task_vma_kern_data {
807 struct task_struct *task;
808 struct mm_struct *mm;
809 struct mmap_unlock_irq_work *work;
810 struct vma_iterator vmi;
813 struct bpf_iter_task_vma {
814 /* opaque iterator state; having __u64 here allows to preserve correct
815 * alignment requirements in vmlinux.h, generated from BTF
818 } __attribute__((aligned(8)));
820 /* Non-opaque version of bpf_iter_task_vma */
821 struct bpf_iter_task_vma_kern {
822 struct bpf_iter_task_vma_kern_data *data;
823 } __attribute__((aligned(8)));
826 __diag_ignore_all("-Wmissing-prototypes",
827 "Global functions as their definitions will be in vmlinux BTF");
829 __bpf_kfunc int bpf_iter_task_vma_new(struct bpf_iter_task_vma *it,
830 struct task_struct *task, u64 addr)
832 struct bpf_iter_task_vma_kern *kit = (void *)it;
833 bool irq_work_busy = false;
836 BUILD_BUG_ON(sizeof(struct bpf_iter_task_vma_kern) != sizeof(struct bpf_iter_task_vma));
837 BUILD_BUG_ON(__alignof__(struct bpf_iter_task_vma_kern) != __alignof__(struct bpf_iter_task_vma));
839 /* is_iter_reg_valid_uninit guarantees that kit hasn't been initialized
840 * before, so non-NULL kit->data doesn't point to previously
841 * bpf_mem_alloc'd bpf_iter_task_vma_kern_data
843 kit->data = bpf_mem_alloc(&bpf_global_ma, sizeof(struct bpf_iter_task_vma_kern_data));
847 kit->data->task = get_task_struct(task);
848 kit->data->mm = task->mm;
849 if (!kit->data->mm) {
851 goto err_cleanup_iter;
854 /* kit->data->work == NULL is valid after bpf_mmap_unlock_get_irq_work */
855 irq_work_busy = bpf_mmap_unlock_get_irq_work(&kit->data->work);
856 if (irq_work_busy || !mmap_read_trylock(kit->data->mm)) {
858 goto err_cleanup_iter;
861 vma_iter_init(&kit->data->vmi, kit->data->mm, addr);
866 put_task_struct(kit->data->task);
867 bpf_mem_free(&bpf_global_ma, kit->data);
868 /* NULL kit->data signals failed bpf_iter_task_vma initialization */
873 __bpf_kfunc struct vm_area_struct *bpf_iter_task_vma_next(struct bpf_iter_task_vma *it)
875 struct bpf_iter_task_vma_kern *kit = (void *)it;
877 if (!kit->data) /* bpf_iter_task_vma_new failed */
879 return vma_next(&kit->data->vmi);
882 __bpf_kfunc void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it)
884 struct bpf_iter_task_vma_kern *kit = (void *)it;
887 bpf_mmap_unlock_mm(kit->data->work, kit->data->mm);
888 put_task_struct(kit->data->task);
889 bpf_mem_free(&bpf_global_ma, kit->data);
895 struct bpf_iter_css_task {
897 } __attribute__((aligned(8)));
899 struct bpf_iter_css_task_kern {
900 struct css_task_iter *css_it;
901 } __attribute__((aligned(8)));
904 __diag_ignore_all("-Wmissing-prototypes",
905 "Global functions as their definitions will be in vmlinux BTF");
907 __bpf_kfunc int bpf_iter_css_task_new(struct bpf_iter_css_task *it,
908 struct cgroup_subsys_state *css, unsigned int flags)
910 struct bpf_iter_css_task_kern *kit = (void *)it;
912 BUILD_BUG_ON(sizeof(struct bpf_iter_css_task_kern) != sizeof(struct bpf_iter_css_task));
913 BUILD_BUG_ON(__alignof__(struct bpf_iter_css_task_kern) !=
914 __alignof__(struct bpf_iter_css_task));
917 case CSS_TASK_ITER_PROCS | CSS_TASK_ITER_THREADED:
918 case CSS_TASK_ITER_PROCS:
925 kit->css_it = bpf_mem_alloc(&bpf_global_ma, sizeof(struct css_task_iter));
928 css_task_iter_start(css, flags, kit->css_it);
932 __bpf_kfunc struct task_struct *bpf_iter_css_task_next(struct bpf_iter_css_task *it)
934 struct bpf_iter_css_task_kern *kit = (void *)it;
938 return css_task_iter_next(kit->css_it);
941 __bpf_kfunc void bpf_iter_css_task_destroy(struct bpf_iter_css_task *it)
943 struct bpf_iter_css_task_kern *kit = (void *)it;
947 css_task_iter_end(kit->css_it);
948 bpf_mem_free(&bpf_global_ma, kit->css_it);
953 struct bpf_iter_task {
955 } __attribute__((aligned(8)));
957 struct bpf_iter_task_kern {
958 struct task_struct *task;
959 struct task_struct *pos;
961 } __attribute__((aligned(8)));
964 /* all process in the system */
965 BPF_TASK_ITER_ALL_PROCS,
966 /* all threads in the system */
967 BPF_TASK_ITER_ALL_THREADS,
968 /* all threads of a specific process */
969 BPF_TASK_ITER_PROC_THREADS
973 __diag_ignore_all("-Wmissing-prototypes",
974 "Global functions as their definitions will be in vmlinux BTF");
976 __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it,
977 struct task_struct *task__nullable, unsigned int flags)
979 struct bpf_iter_task_kern *kit = (void *)it;
981 BUILD_BUG_ON(sizeof(struct bpf_iter_task_kern) > sizeof(struct bpf_iter_task));
982 BUILD_BUG_ON(__alignof__(struct bpf_iter_task_kern) !=
983 __alignof__(struct bpf_iter_task));
985 kit->task = kit->pos = NULL;
987 case BPF_TASK_ITER_ALL_THREADS:
988 case BPF_TASK_ITER_ALL_PROCS:
990 case BPF_TASK_ITER_PROC_THREADS:
998 if (flags == BPF_TASK_ITER_PROC_THREADS)
999 kit->task = task__nullable;
1001 kit->task = &init_task;
1002 kit->pos = kit->task;
1007 __bpf_kfunc struct task_struct *bpf_iter_task_next(struct bpf_iter_task *it)
1009 struct bpf_iter_task_kern *kit = (void *)it;
1010 struct task_struct *pos;
1019 if (flags == BPF_TASK_ITER_ALL_PROCS)
1022 kit->pos = next_thread(kit->pos);
1023 if (kit->pos == kit->task) {
1024 if (flags == BPF_TASK_ITER_PROC_THREADS) {
1032 kit->pos = next_task(kit->pos);
1033 kit->task = kit->pos;
1034 if (kit->pos == &init_task)
1040 __bpf_kfunc void bpf_iter_task_destroy(struct bpf_iter_task *it)
1046 DEFINE_PER_CPU(struct mmap_unlock_irq_work, mmap_unlock_work);
1048 static void do_mmap_read_unlock(struct irq_work *entry)
1050 struct mmap_unlock_irq_work *work;
1052 if (WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_RT)))
1055 work = container_of(entry, struct mmap_unlock_irq_work, irq_work);
1056 mmap_read_unlock_non_owner(work->mm);
1059 static int __init task_iter_init(void)
1061 struct mmap_unlock_irq_work *work;
1064 for_each_possible_cpu(cpu) {
1065 work = per_cpu_ptr(&mmap_unlock_work, cpu);
1066 init_irq_work(&work->irq_work, do_mmap_read_unlock);
1069 task_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
1070 ret = bpf_iter_reg_target(&task_reg_info);
1074 task_file_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
1075 task_file_reg_info.ctx_arg_info[1].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_FILE];
1076 ret = bpf_iter_reg_target(&task_file_reg_info);
1080 task_vma_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
1081 task_vma_reg_info.ctx_arg_info[1].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_VMA];
1082 return bpf_iter_reg_target(&task_vma_reg_info);
1084 late_initcall(task_iter_init);