Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: Handle show_fdinfo for the parameterized task BPF iterators

Show information of iterators in the respective files under
/proc/<pid>/fdinfo/.

For example, for a task file iterator with 1723 as the value of tid
parameter, its fdinfo would look like the following lines.

pos: 0
flags: 02000000
mnt_id: 14
ino: 38
link_type: iter
link_id: 51
prog_tag: a590ac96db22b825
prog_id: 299
target_name: task_file
task_type: TID
tid: 1723

This patch add the last three fields. task_type is the type of the
task parameter. TID means the iterator visit only the thread
specified by tid. The value of tid in the above example is 1723. For
the case of PID task_type, it means the iterator visits only threads
of a process and will show the pid value of the process instead of a
tid.

Signed-off-by: Kui-Feng Lee <kuifeng@fb.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Acked-by: Martin KaFai Lau <martin.lau@kernel.org>
Link: https://lore.kernel.org/bpf/20220926184957.208194-4-kuifeng@fb.com

authored by

Kui-Feng Lee and committed by
Andrii Nakryiko
2c4fe44f 21fb6f2a

+18
+18
kernel/bpf/task_iter.c
··· 10 10 #include <linux/btf_ids.h> 11 11 #include "mmap_unlock_work.h" 12 12 13 + static const char * const iter_task_type_names[] = { 14 + "ALL", 15 + "TID", 16 + "PID", 17 + }; 18 + 13 19 struct bpf_iter_seq_task_common { 14 20 struct pid_namespace *ns; 15 21 enum bpf_iter_task_type type; ··· 693 687 return 0; 694 688 } 695 689 690 + static void bpf_iter_task_show_fdinfo(const struct bpf_iter_aux_info *aux, struct seq_file *seq) 691 + { 692 + seq_printf(seq, "task_type:\t%s\n", iter_task_type_names[aux->task.type]); 693 + if (aux->task.type == BPF_TASK_ITER_TID) 694 + seq_printf(seq, "tid:\t%u\n", aux->task.pid); 695 + else if (aux->task.type == BPF_TASK_ITER_TGID) 696 + seq_printf(seq, "pid:\t%u\n", aux->task.pid); 697 + } 698 + 696 699 static struct bpf_iter_reg task_reg_info = { 697 700 .target = "task", 698 701 .attach_target = bpf_iter_attach_task, ··· 713 698 }, 714 699 .seq_info = &task_seq_info, 715 700 .fill_link_info = bpf_iter_fill_link_info, 701 + .show_fdinfo = bpf_iter_task_show_fdinfo, 716 702 }; 717 703 718 704 static const struct bpf_iter_seq_info task_file_seq_info = { ··· 736 720 }, 737 721 .seq_info = &task_file_seq_info, 738 722 .fill_link_info = bpf_iter_fill_link_info, 723 + .show_fdinfo = bpf_iter_task_show_fdinfo, 739 724 }; 740 725 741 726 static const struct bpf_iter_seq_info task_vma_seq_info = { ··· 759 742 }, 760 743 .seq_info = &task_vma_seq_info, 761 744 .fill_link_info = bpf_iter_fill_link_info, 745 + .show_fdinfo = bpf_iter_task_show_fdinfo, 762 746 }; 763 747 764 748 BPF_CALL_5(bpf_find_vma, struct task_struct *, task, u64, start,