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,
27 struct task_struct *task = NULL;
32 pid = find_ge_pid(*tid, ns);
34 *tid = pid_nr_ns(pid, ns);
35 task = get_pid_task(pid, PIDTYPE_PID);
46 static void *task_seq_start(struct seq_file *seq, loff_t *pos)
48 struct bpf_iter_seq_task_info *info = seq->private;
49 struct task_struct *task;
51 task = task_seq_get_next(info->common.ns, &info->tid);
60 static void *task_seq_next(struct seq_file *seq, void *v, loff_t *pos)
62 struct bpf_iter_seq_task_info *info = seq->private;
63 struct task_struct *task;
67 put_task_struct((struct task_struct *)v);
68 task = task_seq_get_next(info->common.ns, &info->tid);
75 struct bpf_iter__task {
76 __bpf_md_ptr(struct bpf_iter_meta *, meta);
77 __bpf_md_ptr(struct task_struct *, task);
80 DEFINE_BPF_ITER_FUNC(task, struct bpf_iter_meta *meta, struct task_struct *task)
82 static int __task_seq_show(struct seq_file *seq, struct task_struct *task,
85 struct bpf_iter_meta meta;
86 struct bpf_iter__task ctx;
87 struct bpf_prog *prog;
90 prog = bpf_iter_get_info(&meta, in_stop);
97 return bpf_iter_run_prog(prog, &ctx);
100 static int task_seq_show(struct seq_file *seq, void *v)
102 return __task_seq_show(seq, v, false);
105 static void task_seq_stop(struct seq_file *seq, void *v)
108 (void)__task_seq_show(seq, v, true);
110 put_task_struct((struct task_struct *)v);
113 static const struct seq_operations task_seq_ops = {
114 .start = task_seq_start,
115 .next = task_seq_next,
116 .stop = task_seq_stop,
117 .show = task_seq_show,
120 struct bpf_iter_seq_task_file_info {
121 /* The first field must be struct bpf_iter_seq_task_common.
122 * this is assumed by {init, fini}_seq_pidns() callback functions.
124 struct bpf_iter_seq_task_common common;
125 struct task_struct *task;
126 struct files_struct *files;
132 task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info,
133 struct task_struct **task, struct files_struct **fstruct)
135 struct pid_namespace *ns = info->common.ns;
136 u32 curr_tid = info->tid, max_fds;
137 struct files_struct *curr_files;
138 struct task_struct *curr_task;
139 int curr_fd = info->fd;
141 /* If this function returns a non-NULL file object,
142 * it held a reference to the task/files_struct/file.
143 * Otherwise, it does not hold any reference.
148 curr_files = *fstruct;
151 curr_task = task_seq_get_next(ns, &curr_tid);
155 curr_files = get_files_struct(curr_task);
157 put_task_struct(curr_task);
158 curr_tid = ++(info->tid);
163 /* set *fstruct, *task and info->tid */
164 *fstruct = curr_files;
166 if (curr_tid == info->tid) {
169 info->tid = curr_tid;
175 max_fds = files_fdtable(curr_files)->max_fds;
176 for (; curr_fd < max_fds; curr_fd++) {
179 f = fcheck_files(curr_files, curr_fd);
182 if (!get_file_rcu(f))
191 /* the current task is done, go to the next task */
193 put_files_struct(curr_files);
194 put_task_struct(curr_task);
198 curr_tid = ++(info->tid);
202 static void *task_file_seq_start(struct seq_file *seq, loff_t *pos)
204 struct bpf_iter_seq_task_file_info *info = seq->private;
205 struct files_struct *files = NULL;
206 struct task_struct *task = NULL;
209 file = task_file_seq_get_next(info, &task, &files);
224 static void *task_file_seq_next(struct seq_file *seq, void *v, loff_t *pos)
226 struct bpf_iter_seq_task_file_info *info = seq->private;
227 struct files_struct *files = info->files;
228 struct task_struct *task = info->task;
233 fput((struct file *)v);
234 file = task_file_seq_get_next(info, &task, &files);
247 struct bpf_iter__task_file {
248 __bpf_md_ptr(struct bpf_iter_meta *, meta);
249 __bpf_md_ptr(struct task_struct *, task);
251 __bpf_md_ptr(struct file *, file);
254 DEFINE_BPF_ITER_FUNC(task_file, struct bpf_iter_meta *meta,
255 struct task_struct *task, u32 fd,
258 static int __task_file_seq_show(struct seq_file *seq, struct file *file,
261 struct bpf_iter_seq_task_file_info *info = seq->private;
262 struct bpf_iter__task_file ctx;
263 struct bpf_iter_meta meta;
264 struct bpf_prog *prog;
267 prog = bpf_iter_get_info(&meta, in_stop);
272 ctx.task = info->task;
275 return bpf_iter_run_prog(prog, &ctx);
278 static int task_file_seq_show(struct seq_file *seq, void *v)
280 return __task_file_seq_show(seq, v, false);
283 static void task_file_seq_stop(struct seq_file *seq, void *v)
285 struct bpf_iter_seq_task_file_info *info = seq->private;
288 (void)__task_file_seq_show(seq, v, true);
290 fput((struct file *)v);
291 put_files_struct(info->files);
292 put_task_struct(info->task);
298 static int init_seq_pidns(void *priv_data, struct bpf_iter_aux_info *aux)
300 struct bpf_iter_seq_task_common *common = priv_data;
302 common->ns = get_pid_ns(task_active_pid_ns(current));
306 static void fini_seq_pidns(void *priv_data)
308 struct bpf_iter_seq_task_common *common = priv_data;
310 put_pid_ns(common->ns);
313 static const struct seq_operations task_file_seq_ops = {
314 .start = task_file_seq_start,
315 .next = task_file_seq_next,
316 .stop = task_file_seq_stop,
317 .show = task_file_seq_show,
320 BTF_ID_LIST(btf_task_file_ids)
321 BTF_ID(struct, task_struct)
324 static const struct bpf_iter_seq_info task_seq_info = {
325 .seq_ops = &task_seq_ops,
326 .init_seq_private = init_seq_pidns,
327 .fini_seq_private = fini_seq_pidns,
328 .seq_priv_size = sizeof(struct bpf_iter_seq_task_info),
331 static struct bpf_iter_reg task_reg_info = {
333 .ctx_arg_info_size = 1,
335 { offsetof(struct bpf_iter__task, task),
336 PTR_TO_BTF_ID_OR_NULL },
338 .seq_info = &task_seq_info,
341 static const struct bpf_iter_seq_info task_file_seq_info = {
342 .seq_ops = &task_file_seq_ops,
343 .init_seq_private = init_seq_pidns,
344 .fini_seq_private = fini_seq_pidns,
345 .seq_priv_size = sizeof(struct bpf_iter_seq_task_file_info),
348 static struct bpf_iter_reg task_file_reg_info = {
349 .target = "task_file",
350 .ctx_arg_info_size = 2,
352 { offsetof(struct bpf_iter__task_file, task),
353 PTR_TO_BTF_ID_OR_NULL },
354 { offsetof(struct bpf_iter__task_file, file),
355 PTR_TO_BTF_ID_OR_NULL },
357 .seq_info = &task_file_seq_info,
360 static int __init task_iter_init(void)
364 task_reg_info.ctx_arg_info[0].btf_id = btf_task_file_ids[0];
365 ret = bpf_iter_reg_target(&task_reg_info);
369 task_file_reg_info.ctx_arg_info[0].btf_id = btf_task_file_ids[0];
370 task_file_reg_info.ctx_arg_info[1].btf_id = btf_task_file_ids[1];
371 return bpf_iter_reg_target(&task_file_reg_info);
373 late_initcall(task_iter_init);