Merge tag 'm68knommu-for-v5.11' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / kernel / bpf / task_iter.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2020 Facebook */
3
4 #include <linux/init.h>
5 #include <linux/namei.h>
6 #include <linux/pid_namespace.h>
7 #include <linux/fs.h>
8 #include <linux/fdtable.h>
9 #include <linux/filter.h>
10 #include <linux/btf_ids.h>
11
12 struct bpf_iter_seq_task_common {
13         struct pid_namespace *ns;
14 };
15
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.
19          */
20         struct bpf_iter_seq_task_common common;
21         u32 tid;
22 };
23
24 static struct task_struct *task_seq_get_next(struct pid_namespace *ns,
25                                              u32 *tid,
26                                              bool skip_if_dup_files)
27 {
28         struct task_struct *task = NULL;
29         struct pid *pid;
30
31         rcu_read_lock();
32 retry:
33         pid = find_ge_pid(*tid, ns);
34         if (pid) {
35                 *tid = pid_nr_ns(pid, ns);
36                 task = get_pid_task(pid, PIDTYPE_PID);
37                 if (!task) {
38                         ++*tid;
39                         goto retry;
40                 } else if (skip_if_dup_files && task->tgid != task->pid &&
41                            task->files == task->group_leader->files) {
42                         put_task_struct(task);
43                         task = NULL;
44                         ++*tid;
45                         goto retry;
46                 }
47         }
48         rcu_read_unlock();
49
50         return task;
51 }
52
53 static void *task_seq_start(struct seq_file *seq, loff_t *pos)
54 {
55         struct bpf_iter_seq_task_info *info = seq->private;
56         struct task_struct *task;
57
58         task = task_seq_get_next(info->common.ns, &info->tid, false);
59         if (!task)
60                 return NULL;
61
62         if (*pos == 0)
63                 ++*pos;
64         return task;
65 }
66
67 static void *task_seq_next(struct seq_file *seq, void *v, loff_t *pos)
68 {
69         struct bpf_iter_seq_task_info *info = seq->private;
70         struct task_struct *task;
71
72         ++*pos;
73         ++info->tid;
74         put_task_struct((struct task_struct *)v);
75         task = task_seq_get_next(info->common.ns, &info->tid, false);
76         if (!task)
77                 return NULL;
78
79         return task;
80 }
81
82 struct bpf_iter__task {
83         __bpf_md_ptr(struct bpf_iter_meta *, meta);
84         __bpf_md_ptr(struct task_struct *, task);
85 };
86
87 DEFINE_BPF_ITER_FUNC(task, struct bpf_iter_meta *meta, struct task_struct *task)
88
89 static int __task_seq_show(struct seq_file *seq, struct task_struct *task,
90                            bool in_stop)
91 {
92         struct bpf_iter_meta meta;
93         struct bpf_iter__task ctx;
94         struct bpf_prog *prog;
95
96         meta.seq = seq;
97         prog = bpf_iter_get_info(&meta, in_stop);
98         if (!prog)
99                 return 0;
100
101         meta.seq = seq;
102         ctx.meta = &meta;
103         ctx.task = task;
104         return bpf_iter_run_prog(prog, &ctx);
105 }
106
107 static int task_seq_show(struct seq_file *seq, void *v)
108 {
109         return __task_seq_show(seq, v, false);
110 }
111
112 static void task_seq_stop(struct seq_file *seq, void *v)
113 {
114         if (!v)
115                 (void)__task_seq_show(seq, v, true);
116         else
117                 put_task_struct((struct task_struct *)v);
118 }
119
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,
125 };
126
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.
130          */
131         struct bpf_iter_seq_task_common common;
132         struct task_struct *task;
133         u32 tid;
134         u32 fd;
135 };
136
137 static struct file *
138 task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info)
139 {
140         struct pid_namespace *ns = info->common.ns;
141         u32 curr_tid = info->tid;
142         struct task_struct *curr_task;
143         unsigned int curr_fd = info->fd;
144
145         /* If this function returns a non-NULL file object,
146          * it held a reference to the task/file.
147          * Otherwise, it does not hold any reference.
148          */
149 again:
150         if (info->task) {
151                 curr_task = info->task;
152                 curr_fd = info->fd;
153         } else {
154                 curr_task = task_seq_get_next(ns, &curr_tid, true);
155                 if (!curr_task) {
156                         info->task = NULL;
157                         return NULL;
158                 }
159
160                 /* set info->task and info->tid */
161                 info->task = curr_task;
162                 if (curr_tid == info->tid) {
163                         curr_fd = info->fd;
164                 } else {
165                         info->tid = curr_tid;
166                         curr_fd = 0;
167                 }
168         }
169
170         rcu_read_lock();
171         for (;; curr_fd++) {
172                 struct file *f;
173                 f = task_lookup_next_fd_rcu(curr_task, &curr_fd);
174                 if (!f)
175                         break;
176                 if (!get_file_rcu(f))
177                         continue;
178
179                 /* set info->fd */
180                 info->fd = curr_fd;
181                 rcu_read_unlock();
182                 return f;
183         }
184
185         /* the current task is done, go to the next task */
186         rcu_read_unlock();
187         put_task_struct(curr_task);
188         info->task = NULL;
189         info->fd = 0;
190         curr_tid = ++(info->tid);
191         goto again;
192 }
193
194 static void *task_file_seq_start(struct seq_file *seq, loff_t *pos)
195 {
196         struct bpf_iter_seq_task_file_info *info = seq->private;
197         struct file *file;
198
199         info->task = NULL;
200         file = task_file_seq_get_next(info);
201         if (file && *pos == 0)
202                 ++*pos;
203
204         return file;
205 }
206
207 static void *task_file_seq_next(struct seq_file *seq, void *v, loff_t *pos)
208 {
209         struct bpf_iter_seq_task_file_info *info = seq->private;
210
211         ++*pos;
212         ++info->fd;
213         fput((struct file *)v);
214         return task_file_seq_get_next(info);
215 }
216
217 struct bpf_iter__task_file {
218         __bpf_md_ptr(struct bpf_iter_meta *, meta);
219         __bpf_md_ptr(struct task_struct *, task);
220         u32 fd __aligned(8);
221         __bpf_md_ptr(struct file *, file);
222 };
223
224 DEFINE_BPF_ITER_FUNC(task_file, struct bpf_iter_meta *meta,
225                      struct task_struct *task, u32 fd,
226                      struct file *file)
227
228 static int __task_file_seq_show(struct seq_file *seq, struct file *file,
229                                 bool in_stop)
230 {
231         struct bpf_iter_seq_task_file_info *info = seq->private;
232         struct bpf_iter__task_file ctx;
233         struct bpf_iter_meta meta;
234         struct bpf_prog *prog;
235
236         meta.seq = seq;
237         prog = bpf_iter_get_info(&meta, in_stop);
238         if (!prog)
239                 return 0;
240
241         ctx.meta = &meta;
242         ctx.task = info->task;
243         ctx.fd = info->fd;
244         ctx.file = file;
245         return bpf_iter_run_prog(prog, &ctx);
246 }
247
248 static int task_file_seq_show(struct seq_file *seq, void *v)
249 {
250         return __task_file_seq_show(seq, v, false);
251 }
252
253 static void task_file_seq_stop(struct seq_file *seq, void *v)
254 {
255         struct bpf_iter_seq_task_file_info *info = seq->private;
256
257         if (!v) {
258                 (void)__task_file_seq_show(seq, v, true);
259         } else {
260                 fput((struct file *)v);
261                 put_task_struct(info->task);
262                 info->task = NULL;
263         }
264 }
265
266 static int init_seq_pidns(void *priv_data, struct bpf_iter_aux_info *aux)
267 {
268         struct bpf_iter_seq_task_common *common = priv_data;
269
270         common->ns = get_pid_ns(task_active_pid_ns(current));
271         return 0;
272 }
273
274 static void fini_seq_pidns(void *priv_data)
275 {
276         struct bpf_iter_seq_task_common *common = priv_data;
277
278         put_pid_ns(common->ns);
279 }
280
281 static const struct seq_operations task_file_seq_ops = {
282         .start  = task_file_seq_start,
283         .next   = task_file_seq_next,
284         .stop   = task_file_seq_stop,
285         .show   = task_file_seq_show,
286 };
287
288 BTF_ID_LIST(btf_task_file_ids)
289 BTF_ID(struct, task_struct)
290 BTF_ID(struct, file)
291
292 static const struct bpf_iter_seq_info task_seq_info = {
293         .seq_ops                = &task_seq_ops,
294         .init_seq_private       = init_seq_pidns,
295         .fini_seq_private       = fini_seq_pidns,
296         .seq_priv_size          = sizeof(struct bpf_iter_seq_task_info),
297 };
298
299 static struct bpf_iter_reg task_reg_info = {
300         .target                 = "task",
301         .feature                = BPF_ITER_RESCHED,
302         .ctx_arg_info_size      = 1,
303         .ctx_arg_info           = {
304                 { offsetof(struct bpf_iter__task, task),
305                   PTR_TO_BTF_ID_OR_NULL },
306         },
307         .seq_info               = &task_seq_info,
308 };
309
310 static const struct bpf_iter_seq_info task_file_seq_info = {
311         .seq_ops                = &task_file_seq_ops,
312         .init_seq_private       = init_seq_pidns,
313         .fini_seq_private       = fini_seq_pidns,
314         .seq_priv_size          = sizeof(struct bpf_iter_seq_task_file_info),
315 };
316
317 static struct bpf_iter_reg task_file_reg_info = {
318         .target                 = "task_file",
319         .feature                = BPF_ITER_RESCHED,
320         .ctx_arg_info_size      = 2,
321         .ctx_arg_info           = {
322                 { offsetof(struct bpf_iter__task_file, task),
323                   PTR_TO_BTF_ID_OR_NULL },
324                 { offsetof(struct bpf_iter__task_file, file),
325                   PTR_TO_BTF_ID_OR_NULL },
326         },
327         .seq_info               = &task_file_seq_info,
328 };
329
330 static int __init task_iter_init(void)
331 {
332         int ret;
333
334         task_reg_info.ctx_arg_info[0].btf_id = btf_task_file_ids[0];
335         ret = bpf_iter_reg_target(&task_reg_info);
336         if (ret)
337                 return ret;
338
339         task_file_reg_info.ctx_arg_info[0].btf_id = btf_task_file_ids[0];
340         task_file_reg_info.ctx_arg_info[1].btf_id = btf_task_file_ids[1];
341         return bpf_iter_reg_target(&task_file_reg_info);
342 }
343 late_initcall(task_iter_init);