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/btf_ids.h>
12 struct bpf_iter_seq_task_common {
13 struct pid_namespace *ns;
16 struct bpf_iter_seq_task_info {
17 /* The first field must be struct bpf_iter_seq_task_common.
18 * this is assumed by {init, fini}_seq_pidns() callback functions.
20 struct bpf_iter_seq_task_common common;
24 static struct task_struct *task_seq_get_next(struct pid_namespace *ns,
26 bool skip_if_dup_files)
28 struct task_struct *task = NULL;
33 pid = find_ge_pid(*tid, ns);
35 *tid = pid_nr_ns(pid, ns);
36 task = get_pid_task(pid, PIDTYPE_PID);
40 } else if (skip_if_dup_files && task->tgid != task->pid &&
41 task->files == task->group_leader->files) {
42 put_task_struct(task);
53 static void *task_seq_start(struct seq_file *seq, loff_t *pos)
55 struct bpf_iter_seq_task_info *info = seq->private;
56 struct task_struct *task;
58 task = task_seq_get_next(info->common.ns, &info->tid, false);
67 static void *task_seq_next(struct seq_file *seq, void *v, loff_t *pos)
69 struct bpf_iter_seq_task_info *info = seq->private;
70 struct task_struct *task;
74 put_task_struct((struct task_struct *)v);
75 task = task_seq_get_next(info->common.ns, &info->tid, false);
82 struct bpf_iter__task {
83 __bpf_md_ptr(struct bpf_iter_meta *, meta);
84 __bpf_md_ptr(struct task_struct *, task);
87 DEFINE_BPF_ITER_FUNC(task, struct bpf_iter_meta *meta, struct task_struct *task)
89 static int __task_seq_show(struct seq_file *seq, struct task_struct *task,
92 struct bpf_iter_meta meta;
93 struct bpf_iter__task ctx;
94 struct bpf_prog *prog;
97 prog = bpf_iter_get_info(&meta, in_stop);
104 return bpf_iter_run_prog(prog, &ctx);
107 static int task_seq_show(struct seq_file *seq, void *v)
109 return __task_seq_show(seq, v, false);
112 static void task_seq_stop(struct seq_file *seq, void *v)
115 (void)__task_seq_show(seq, v, true);
117 put_task_struct((struct task_struct *)v);
120 static const struct seq_operations task_seq_ops = {
121 .start = task_seq_start,
122 .next = task_seq_next,
123 .stop = task_seq_stop,
124 .show = task_seq_show,
127 struct bpf_iter_seq_task_file_info {
128 /* The first field must be struct bpf_iter_seq_task_common.
129 * this is assumed by {init, fini}_seq_pidns() callback functions.
131 struct bpf_iter_seq_task_common common;
132 struct task_struct *task;
133 struct files_struct *files;
139 task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info,
140 struct task_struct **task, struct files_struct **fstruct)
142 struct pid_namespace *ns = info->common.ns;
143 u32 curr_tid = info->tid, max_fds;
144 struct files_struct *curr_files;
145 struct task_struct *curr_task;
146 int curr_fd = info->fd;
148 /* If this function returns a non-NULL file object,
149 * it held a reference to the task/files_struct/file.
150 * Otherwise, it does not hold any reference.
155 curr_files = *fstruct;
158 curr_task = task_seq_get_next(ns, &curr_tid, true);
162 curr_files = get_files_struct(curr_task);
164 put_task_struct(curr_task);
165 curr_tid = ++(info->tid);
170 /* set *fstruct, *task and info->tid */
171 *fstruct = curr_files;
173 if (curr_tid == info->tid) {
176 info->tid = curr_tid;
182 max_fds = files_fdtable(curr_files)->max_fds;
183 for (; curr_fd < max_fds; curr_fd++) {
186 f = fcheck_files(curr_files, curr_fd);
189 if (!get_file_rcu(f))
198 /* the current task is done, go to the next task */
200 put_files_struct(curr_files);
201 put_task_struct(curr_task);
205 curr_tid = ++(info->tid);
209 static void *task_file_seq_start(struct seq_file *seq, loff_t *pos)
211 struct bpf_iter_seq_task_file_info *info = seq->private;
212 struct files_struct *files = NULL;
213 struct task_struct *task = NULL;
216 file = task_file_seq_get_next(info, &task, &files);
231 static void *task_file_seq_next(struct seq_file *seq, void *v, loff_t *pos)
233 struct bpf_iter_seq_task_file_info *info = seq->private;
234 struct files_struct *files = info->files;
235 struct task_struct *task = info->task;
240 fput((struct file *)v);
241 file = task_file_seq_get_next(info, &task, &files);
254 struct bpf_iter__task_file {
255 __bpf_md_ptr(struct bpf_iter_meta *, meta);
256 __bpf_md_ptr(struct task_struct *, task);
258 __bpf_md_ptr(struct file *, file);
261 DEFINE_BPF_ITER_FUNC(task_file, struct bpf_iter_meta *meta,
262 struct task_struct *task, u32 fd,
265 static int __task_file_seq_show(struct seq_file *seq, struct file *file,
268 struct bpf_iter_seq_task_file_info *info = seq->private;
269 struct bpf_iter__task_file ctx;
270 struct bpf_iter_meta meta;
271 struct bpf_prog *prog;
274 prog = bpf_iter_get_info(&meta, in_stop);
279 ctx.task = info->task;
282 return bpf_iter_run_prog(prog, &ctx);
285 static int task_file_seq_show(struct seq_file *seq, void *v)
287 return __task_file_seq_show(seq, v, false);
290 static void task_file_seq_stop(struct seq_file *seq, void *v)
292 struct bpf_iter_seq_task_file_info *info = seq->private;
295 (void)__task_file_seq_show(seq, v, true);
297 fput((struct file *)v);
298 put_files_struct(info->files);
299 put_task_struct(info->task);
305 static int init_seq_pidns(void *priv_data, struct bpf_iter_aux_info *aux)
307 struct bpf_iter_seq_task_common *common = priv_data;
309 common->ns = get_pid_ns(task_active_pid_ns(current));
313 static void fini_seq_pidns(void *priv_data)
315 struct bpf_iter_seq_task_common *common = priv_data;
317 put_pid_ns(common->ns);
320 static const struct seq_operations task_file_seq_ops = {
321 .start = task_file_seq_start,
322 .next = task_file_seq_next,
323 .stop = task_file_seq_stop,
324 .show = task_file_seq_show,
327 BTF_ID_LIST(btf_task_file_ids)
328 BTF_ID(struct, task_struct)
331 static const struct bpf_iter_seq_info task_seq_info = {
332 .seq_ops = &task_seq_ops,
333 .init_seq_private = init_seq_pidns,
334 .fini_seq_private = fini_seq_pidns,
335 .seq_priv_size = sizeof(struct bpf_iter_seq_task_info),
338 static struct bpf_iter_reg task_reg_info = {
340 .ctx_arg_info_size = 1,
342 { offsetof(struct bpf_iter__task, task),
343 PTR_TO_BTF_ID_OR_NULL },
345 .seq_info = &task_seq_info,
348 static const struct bpf_iter_seq_info task_file_seq_info = {
349 .seq_ops = &task_file_seq_ops,
350 .init_seq_private = init_seq_pidns,
351 .fini_seq_private = fini_seq_pidns,
352 .seq_priv_size = sizeof(struct bpf_iter_seq_task_file_info),
355 static struct bpf_iter_reg task_file_reg_info = {
356 .target = "task_file",
357 .ctx_arg_info_size = 2,
359 { offsetof(struct bpf_iter__task_file, task),
360 PTR_TO_BTF_ID_OR_NULL },
361 { offsetof(struct bpf_iter__task_file, file),
362 PTR_TO_BTF_ID_OR_NULL },
364 .seq_info = &task_file_seq_info,
367 static int __init task_iter_init(void)
371 task_reg_info.ctx_arg_info[0].btf_id = btf_task_file_ids[0];
372 ret = bpf_iter_reg_target(&task_reg_info);
376 task_file_reg_info.ctx_arg_info[0].btf_id = btf_task_file_ids[0];
377 task_file_reg_info.ctx_arg_info[1].btf_id = btf_task_file_ids[1];
378 return bpf_iter_reg_target(&task_file_reg_info);
380 late_initcall(task_iter_init);