1eaaacd23SYonghong Song // SPDX-License-Identifier: GPL-2.0-only
2eaaacd23SYonghong Song /* Copyright (c) 2020 Facebook */
3eaaacd23SYonghong Song
4eaaacd23SYonghong Song #include <linux/init.h>
5eaaacd23SYonghong Song #include <linux/namei.h>
6eaaacd23SYonghong Song #include <linux/pid_namespace.h>
7eaaacd23SYonghong Song #include <linux/fs.h>
8eaaacd23SYonghong Song #include <linux/filter.h>
9eaaacd23SYonghong Song #include <linux/bpf_mem_alloc.h>
104ac45468SDave Marchevsky #include <linux/btf_ids.h>
11951cf368SYonghong Song #include <linux/mm_types.h>
124ac45468SDave Marchevsky #include "mmap_unlock_work.h"
137c7e3d31SSong Liu
14eaaacd23SYonghong Song static const char * const iter_task_type_names[] = {
152c4fe44fSKui-Feng Lee "ALL",
162c4fe44fSKui-Feng Lee "TID",
172c4fe44fSKui-Feng Lee "PID",
182c4fe44fSKui-Feng Lee };
192c4fe44fSKui-Feng Lee
202c4fe44fSKui-Feng Lee struct bpf_iter_seq_task_common {
21eaaacd23SYonghong Song struct pid_namespace *ns;
22eaaacd23SYonghong Song enum bpf_iter_task_type type;
23f0d74c4dSKui-Feng Lee u32 pid;
24f0d74c4dSKui-Feng Lee u32 pid_visiting;
25f0d74c4dSKui-Feng Lee };
26eaaacd23SYonghong Song
27eaaacd23SYonghong Song struct bpf_iter_seq_task_info {
28eaaacd23SYonghong Song /* The first field must be struct bpf_iter_seq_task_common.
29eaaacd23SYonghong Song * this is assumed by {init, fini}_seq_pidns() callback functions.
30eaaacd23SYonghong Song */
31eaaacd23SYonghong Song struct bpf_iter_seq_task_common common;
32eaaacd23SYonghong Song u32 tid;
33eaaacd23SYonghong Song };
34eaaacd23SYonghong Song
task_group_seq_get_next(struct bpf_iter_seq_task_common * common,u32 * tid,bool skip_if_dup_files)35eaaacd23SYonghong Song static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_common *common,
36f0d74c4dSKui-Feng Lee u32 *tid,
37f0d74c4dSKui-Feng Lee bool skip_if_dup_files)
38f0d74c4dSKui-Feng Lee {
39f0d74c4dSKui-Feng Lee struct task_struct *task;
400ee9808bSOleg Nesterov struct pid *pid;
41f0d74c4dSKui-Feng Lee u32 next_tid;
42780aa8dfSOleg Nesterov
43f0d74c4dSKui-Feng Lee if (!*tid) {
44f0d74c4dSKui-Feng Lee /* The first time, the iterator calls this function. */
45f0d74c4dSKui-Feng Lee pid = find_pid_ns(common->pid, common->ns);
46f0d74c4dSKui-Feng Lee task = get_pid_task(pid, PIDTYPE_TGID);
47f0d74c4dSKui-Feng Lee if (!task)
48f0d74c4dSKui-Feng Lee return NULL;
49f0d74c4dSKui-Feng Lee
50f0d74c4dSKui-Feng Lee *tid = common->pid;
51f0d74c4dSKui-Feng Lee common->pid_visiting = common->pid;
52f0d74c4dSKui-Feng Lee
53f0d74c4dSKui-Feng Lee return task;
54f0d74c4dSKui-Feng Lee }
55f0d74c4dSKui-Feng Lee
56f0d74c4dSKui-Feng Lee /* If the control returns to user space and comes back to the
57f0d74c4dSKui-Feng Lee * kernel again, *tid and common->pid_visiting should be the
58f0d74c4dSKui-Feng Lee * same for task_seq_start() to pick up the correct task.
59f0d74c4dSKui-Feng Lee */
60f0d74c4dSKui-Feng Lee if (*tid == common->pid_visiting) {
61f0d74c4dSKui-Feng Lee pid = find_pid_ns(common->pid_visiting, common->ns);
62f0d74c4dSKui-Feng Lee task = get_pid_task(pid, PIDTYPE_PID);
63f0d74c4dSKui-Feng Lee
64f0d74c4dSKui-Feng Lee return task;
65f0d74c4dSKui-Feng Lee }
66f0d74c4dSKui-Feng Lee
67f0d74c4dSKui-Feng Lee task = find_task_by_pid_ns(common->pid_visiting, common->ns);
6849819213SOleg Nesterov if (!task)
69f0d74c4dSKui-Feng Lee return NULL;
70f0d74c4dSKui-Feng Lee
71f0d74c4dSKui-Feng Lee retry:
72f0d74c4dSKui-Feng Lee task = __next_thread(task);
732d161805SOleg Nesterov if (!task)
742d161805SOleg Nesterov return NULL;
752d161805SOleg Nesterov
76f0d74c4dSKui-Feng Lee next_tid = __task_pid_nr_ns(task, PIDTYPE_PID, common->ns);
77780aa8dfSOleg Nesterov if (!next_tid)
782d161805SOleg Nesterov goto retry;
792d161805SOleg Nesterov
80f0d74c4dSKui-Feng Lee if (skip_if_dup_files && task->files == task->group_leader->files)
810ee9808bSOleg Nesterov goto retry;
82f0d74c4dSKui-Feng Lee
83f0d74c4dSKui-Feng Lee *tid = common->pid_visiting = next_tid;
84780aa8dfSOleg Nesterov get_task_struct(task);
850ee9808bSOleg Nesterov return task;
860ee9808bSOleg Nesterov }
87f0d74c4dSKui-Feng Lee
task_seq_get_next(struct bpf_iter_seq_task_common * common,u32 * tid,bool skip_if_dup_files)88f0d74c4dSKui-Feng Lee static struct task_struct *task_seq_get_next(struct bpf_iter_seq_task_common *common,
89f0d74c4dSKui-Feng Lee u32 *tid,
90203d7b05SYonghong Song bool skip_if_dup_files)
91203d7b05SYonghong Song {
92eaaacd23SYonghong Song struct task_struct *task = NULL;
93eaaacd23SYonghong Song struct pid *pid;
94eaaacd23SYonghong Song
95eaaacd23SYonghong Song if (common->type == BPF_TASK_ITER_TID) {
96f0d74c4dSKui-Feng Lee if (*tid && *tid != common->pid)
97f0d74c4dSKui-Feng Lee return NULL;
98f0d74c4dSKui-Feng Lee rcu_read_lock();
99f0d74c4dSKui-Feng Lee pid = find_pid_ns(common->pid, common->ns);
100f0d74c4dSKui-Feng Lee if (pid) {
101f0d74c4dSKui-Feng Lee task = get_pid_task(pid, PIDTYPE_PID);
102*9495a5b7SJordan Rome *tid = common->pid;
103f0d74c4dSKui-Feng Lee }
104f0d74c4dSKui-Feng Lee rcu_read_unlock();
105f0d74c4dSKui-Feng Lee
106f0d74c4dSKui-Feng Lee return task;
107f0d74c4dSKui-Feng Lee }
108f0d74c4dSKui-Feng Lee
109f0d74c4dSKui-Feng Lee if (common->type == BPF_TASK_ITER_TGID) {
110f0d74c4dSKui-Feng Lee rcu_read_lock();
111f0d74c4dSKui-Feng Lee task = task_group_seq_get_next(common, tid, skip_if_dup_files);
112f0d74c4dSKui-Feng Lee rcu_read_unlock();
113f0d74c4dSKui-Feng Lee
114f0d74c4dSKui-Feng Lee return task;
115f0d74c4dSKui-Feng Lee }
116f0d74c4dSKui-Feng Lee
117f0d74c4dSKui-Feng Lee rcu_read_lock();
118eaaacd23SYonghong Song retry:
119c70f34a8SAndrii Nakryiko pid = find_ge_pid(*tid, common->ns);
120f0d74c4dSKui-Feng Lee if (pid) {
121c70f34a8SAndrii Nakryiko *tid = pid_nr_ns(pid, common->ns);
122f0d74c4dSKui-Feng Lee task = get_pid_task(pid, PIDTYPE_PID);
123eaaacd23SYonghong Song if (!task) {
124c70f34a8SAndrii Nakryiko ++*tid;
125c70f34a8SAndrii Nakryiko goto retry;
126c70f34a8SAndrii Nakryiko } else if (skip_if_dup_files && !thread_group_leader(task) &&
127a61daaf3SJonathan Lemon task->files == task->group_leader->files) {
128203d7b05SYonghong Song put_task_struct(task);
129203d7b05SYonghong Song task = NULL;
130203d7b05SYonghong Song ++*tid;
131203d7b05SYonghong Song goto retry;
132203d7b05SYonghong Song }
133c70f34a8SAndrii Nakryiko }
134c70f34a8SAndrii Nakryiko rcu_read_unlock();
135eaaacd23SYonghong Song
136eaaacd23SYonghong Song return task;
137eaaacd23SYonghong Song }
138eaaacd23SYonghong Song
task_seq_start(struct seq_file * seq,loff_t * pos)139eaaacd23SYonghong Song static void *task_seq_start(struct seq_file *seq, loff_t *pos)
140eaaacd23SYonghong Song {
141eaaacd23SYonghong Song struct bpf_iter_seq_task_info *info = seq->private;
142eaaacd23SYonghong Song struct task_struct *task;
143eaaacd23SYonghong Song
144eaaacd23SYonghong Song task = task_seq_get_next(&info->common, &info->tid, false);
145f0d74c4dSKui-Feng Lee if (!task)
146eaaacd23SYonghong Song return NULL;
147eaaacd23SYonghong Song
148eaaacd23SYonghong Song if (*pos == 0)
1493f9969f2SYonghong Song ++*pos;
150eaaacd23SYonghong Song return task;
151eaaacd23SYonghong Song }
152eaaacd23SYonghong Song
task_seq_next(struct seq_file * seq,void * v,loff_t * pos)153eaaacd23SYonghong Song static void *task_seq_next(struct seq_file *seq, void *v, loff_t *pos)
154eaaacd23SYonghong Song {
155eaaacd23SYonghong Song struct bpf_iter_seq_task_info *info = seq->private;
156eaaacd23SYonghong Song struct task_struct *task;
157eaaacd23SYonghong Song
158eaaacd23SYonghong Song ++*pos;
159eaaacd23SYonghong Song ++info->tid;
160eaaacd23SYonghong Song put_task_struct((struct task_struct *)v);
161eaaacd23SYonghong Song task = task_seq_get_next(&info->common, &info->tid, false);
162f0d74c4dSKui-Feng Lee if (!task)
163eaaacd23SYonghong Song return NULL;
164eaaacd23SYonghong Song
165eaaacd23SYonghong Song return task;
166eaaacd23SYonghong Song }
167eaaacd23SYonghong Song
168eaaacd23SYonghong Song struct bpf_iter__task {
169eaaacd23SYonghong Song __bpf_md_ptr(struct bpf_iter_meta *, meta);
170eaaacd23SYonghong Song __bpf_md_ptr(struct task_struct *, task);
171eaaacd23SYonghong Song };
172eaaacd23SYonghong Song
DEFINE_BPF_ITER_FUNC(task,struct bpf_iter_meta * meta,struct task_struct * task)173eaaacd23SYonghong Song DEFINE_BPF_ITER_FUNC(task, struct bpf_iter_meta *meta, struct task_struct *task)
174eaaacd23SYonghong Song
175eaaacd23SYonghong Song static int __task_seq_show(struct seq_file *seq, struct task_struct *task,
176eaaacd23SYonghong Song bool in_stop)
177eaaacd23SYonghong Song {
178eaaacd23SYonghong Song struct bpf_iter_meta meta;
179eaaacd23SYonghong Song struct bpf_iter__task ctx;
180eaaacd23SYonghong Song struct bpf_prog *prog;
181eaaacd23SYonghong Song
182eaaacd23SYonghong Song meta.seq = seq;
183eaaacd23SYonghong Song prog = bpf_iter_get_info(&meta, in_stop);
184eaaacd23SYonghong Song if (!prog)
185eaaacd23SYonghong Song return 0;
186eaaacd23SYonghong Song
187eaaacd23SYonghong Song ctx.meta = &meta;
188eaaacd23SYonghong Song ctx.task = task;
189eaaacd23SYonghong Song return bpf_iter_run_prog(prog, &ctx);
190eaaacd23SYonghong Song }
191eaaacd23SYonghong Song
task_seq_show(struct seq_file * seq,void * v)192eaaacd23SYonghong Song static int task_seq_show(struct seq_file *seq, void *v)
193eaaacd23SYonghong Song {
194eaaacd23SYonghong Song return __task_seq_show(seq, v, false);
195eaaacd23SYonghong Song }
196eaaacd23SYonghong Song
task_seq_stop(struct seq_file * seq,void * v)197eaaacd23SYonghong Song static void task_seq_stop(struct seq_file *seq, void *v)
198eaaacd23SYonghong Song {
199eaaacd23SYonghong Song if (!v)
200eaaacd23SYonghong Song (void)__task_seq_show(seq, v, true);
201eaaacd23SYonghong Song else
202eaaacd23SYonghong Song put_task_struct((struct task_struct *)v);
203eaaacd23SYonghong Song }
204eaaacd23SYonghong Song
bpf_iter_attach_task(struct bpf_prog * prog,union bpf_iter_link_info * linfo,struct bpf_iter_aux_info * aux)205eaaacd23SYonghong Song static int bpf_iter_attach_task(struct bpf_prog *prog,
206f0d74c4dSKui-Feng Lee union bpf_iter_link_info *linfo,
207f0d74c4dSKui-Feng Lee struct bpf_iter_aux_info *aux)
208f0d74c4dSKui-Feng Lee {
209f0d74c4dSKui-Feng Lee unsigned int flags;
210f0d74c4dSKui-Feng Lee struct pid *pid;
211f0d74c4dSKui-Feng Lee pid_t tgid;
212f0d74c4dSKui-Feng Lee
213f0d74c4dSKui-Feng Lee if ((!!linfo->task.tid + !!linfo->task.pid + !!linfo->task.pid_fd) > 1)
214f0d74c4dSKui-Feng Lee return -EINVAL;
215f0d74c4dSKui-Feng Lee
216f0d74c4dSKui-Feng Lee aux->task.type = BPF_TASK_ITER_ALL;
217f0d74c4dSKui-Feng Lee if (linfo->task.tid != 0) {
218f0d74c4dSKui-Feng Lee aux->task.type = BPF_TASK_ITER_TID;
219f0d74c4dSKui-Feng Lee aux->task.pid = linfo->task.tid;
220f0d74c4dSKui-Feng Lee }
221f0d74c4dSKui-Feng Lee if (linfo->task.pid != 0) {
222f0d74c4dSKui-Feng Lee aux->task.type = BPF_TASK_ITER_TGID;
223f0d74c4dSKui-Feng Lee aux->task.pid = linfo->task.pid;
224f0d74c4dSKui-Feng Lee }
225f0d74c4dSKui-Feng Lee if (linfo->task.pid_fd != 0) {
226f0d74c4dSKui-Feng Lee aux->task.type = BPF_TASK_ITER_TGID;
227f0d74c4dSKui-Feng Lee
228f0d74c4dSKui-Feng Lee pid = pidfd_get_pid(linfo->task.pid_fd, &flags);
229f0d74c4dSKui-Feng Lee if (IS_ERR(pid))
230f0d74c4dSKui-Feng Lee return PTR_ERR(pid);
231f0d74c4dSKui-Feng Lee
232f0d74c4dSKui-Feng Lee tgid = pid_nr_ns(pid, task_active_pid_ns(current));
233f0d74c4dSKui-Feng Lee aux->task.pid = tgid;
234f0d74c4dSKui-Feng Lee put_pid(pid);
235f0d74c4dSKui-Feng Lee }
236f0d74c4dSKui-Feng Lee
237f0d74c4dSKui-Feng Lee return 0;
238f0d74c4dSKui-Feng Lee }
239f0d74c4dSKui-Feng Lee
240f0d74c4dSKui-Feng Lee static const struct seq_operations task_seq_ops = {
241eaaacd23SYonghong Song .start = task_seq_start,
242eaaacd23SYonghong Song .next = task_seq_next,
243eaaacd23SYonghong Song .stop = task_seq_stop,
244eaaacd23SYonghong Song .show = task_seq_show,
245eaaacd23SYonghong Song };
246eaaacd23SYonghong Song
247eaaacd23SYonghong Song struct bpf_iter_seq_task_file_info {
248eaaacd23SYonghong Song /* The first field must be struct bpf_iter_seq_task_common.
249eaaacd23SYonghong Song * this is assumed by {init, fini}_seq_pidns() callback functions.
250eaaacd23SYonghong Song */
251eaaacd23SYonghong Song struct bpf_iter_seq_task_common common;
252eaaacd23SYonghong Song struct task_struct *task;
253eaaacd23SYonghong Song u32 tid;
254eaaacd23SYonghong Song u32 fd;
255eaaacd23SYonghong Song };
256eaaacd23SYonghong Song
257eaaacd23SYonghong Song static struct file *
task_file_seq_get_next(struct bpf_iter_seq_task_file_info * info)258eaaacd23SYonghong Song task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info)
25991b2db27SSong Liu {
260eaaacd23SYonghong Song u32 saved_tid = info->tid;
261f0d74c4dSKui-Feng Lee struct task_struct *curr_task;
262eaaacd23SYonghong Song unsigned int curr_fd = info->fd;
26366ed5944SEric W. Biederman struct file *f;
264bc239eb2SDan Carpenter
265eaaacd23SYonghong Song /* If this function returns a non-NULL file object,
266eaaacd23SYonghong Song * it held a reference to the task/file.
26766ed5944SEric W. Biederman * Otherwise, it does not hold any reference.
268eaaacd23SYonghong Song */
269eaaacd23SYonghong Song again:
270eaaacd23SYonghong Song if (info->task) {
27191b2db27SSong Liu curr_task = info->task;
27291b2db27SSong Liu curr_fd = info->fd;
273eaaacd23SYonghong Song } else {
274eaaacd23SYonghong Song curr_task = task_seq_get_next(&info->common, &info->tid, true);
275f0d74c4dSKui-Feng Lee if (!curr_task) {
27691b2db27SSong Liu info->task = NULL;
27791b2db27SSong Liu return NULL;
278eaaacd23SYonghong Song }
27991b2db27SSong Liu
280eaaacd23SYonghong Song /* set info->task */
281f0d74c4dSKui-Feng Lee info->task = curr_task;
28204901aabSYonghong Song if (saved_tid == info->tid)
283f0d74c4dSKui-Feng Lee curr_fd = info->fd;
284eaaacd23SYonghong Song else
285f0d74c4dSKui-Feng Lee curr_fd = 0;
286eaaacd23SYonghong Song }
287eaaacd23SYonghong Song
288eaaacd23SYonghong Song f = fget_task_next(curr_task, &curr_fd);
289eaaacd23SYonghong Song if (f) {
2900ede61d8SChristian Brauner /* set info->fd */
291bc239eb2SDan Carpenter info->fd = curr_fd;
292eaaacd23SYonghong Song return f;
293eaaacd23SYonghong Song }
294eaaacd23SYonghong Song
295eaaacd23SYonghong Song /* the current task is done, go to the next task */
296eaaacd23SYonghong Song put_task_struct(curr_task);
297eaaacd23SYonghong Song
298eaaacd23SYonghong Song if (info->common.type == BPF_TASK_ITER_TID) {
299eaaacd23SYonghong Song info->task = NULL;
300eaaacd23SYonghong Song return NULL;
301f0d74c4dSKui-Feng Lee }
302f0d74c4dSKui-Feng Lee
303f0d74c4dSKui-Feng Lee info->task = NULL;
304f0d74c4dSKui-Feng Lee info->fd = 0;
305f0d74c4dSKui-Feng Lee saved_tid = ++(info->tid);
306f0d74c4dSKui-Feng Lee goto again;
30791b2db27SSong Liu }
308eaaacd23SYonghong Song
task_file_seq_start(struct seq_file * seq,loff_t * pos)309f0d74c4dSKui-Feng Lee static void *task_file_seq_start(struct seq_file *seq, loff_t *pos)
310eaaacd23SYonghong Song {
311eaaacd23SYonghong Song struct bpf_iter_seq_task_file_info *info = seq->private;
312eaaacd23SYonghong Song struct file *file;
313eaaacd23SYonghong Song
314eaaacd23SYonghong Song info->task = NULL;
315eaaacd23SYonghong Song file = task_file_seq_get_next(info);
316eaaacd23SYonghong Song if (file && *pos == 0)
317eaaacd23SYonghong Song ++*pos;
318eaaacd23SYonghong Song
31991b2db27SSong Liu return file;
32091b2db27SSong Liu }
321eaaacd23SYonghong Song
task_file_seq_next(struct seq_file * seq,void * v,loff_t * pos)322eaaacd23SYonghong Song static void *task_file_seq_next(struct seq_file *seq, void *v, loff_t *pos)
323eaaacd23SYonghong Song {
324eaaacd23SYonghong Song struct bpf_iter_seq_task_file_info *info = seq->private;
325eaaacd23SYonghong Song
326eaaacd23SYonghong Song ++*pos;
327eaaacd23SYonghong Song ++info->fd;
328eaaacd23SYonghong Song fput((struct file *)v);
329eaaacd23SYonghong Song return task_file_seq_get_next(info);
330eaaacd23SYonghong Song }
331eaaacd23SYonghong Song
332eaaacd23SYonghong Song struct bpf_iter__task_file {
33391b2db27SSong Liu __bpf_md_ptr(struct bpf_iter_meta *, meta);
334eaaacd23SYonghong Song __bpf_md_ptr(struct task_struct *, task);
335eaaacd23SYonghong Song u32 fd __aligned(8);
336eaaacd23SYonghong Song __bpf_md_ptr(struct file *, file);
337eaaacd23SYonghong Song };
338eaaacd23SYonghong Song
DEFINE_BPF_ITER_FUNC(task_file,struct bpf_iter_meta * meta,struct task_struct * task,u32 fd,struct file * file)339eaaacd23SYonghong Song DEFINE_BPF_ITER_FUNC(task_file, struct bpf_iter_meta *meta,
340eaaacd23SYonghong Song struct task_struct *task, u32 fd,
341eaaacd23SYonghong Song struct file *file)
342eaaacd23SYonghong Song
343eaaacd23SYonghong Song static int __task_file_seq_show(struct seq_file *seq, struct file *file,
344eaaacd23SYonghong Song bool in_stop)
345eaaacd23SYonghong Song {
346eaaacd23SYonghong Song struct bpf_iter_seq_task_file_info *info = seq->private;
347eaaacd23SYonghong Song struct bpf_iter__task_file ctx;
348eaaacd23SYonghong Song struct bpf_iter_meta meta;
349eaaacd23SYonghong Song struct bpf_prog *prog;
350eaaacd23SYonghong Song
351eaaacd23SYonghong Song meta.seq = seq;
352eaaacd23SYonghong Song prog = bpf_iter_get_info(&meta, in_stop);
353eaaacd23SYonghong Song if (!prog)
354eaaacd23SYonghong Song return 0;
355eaaacd23SYonghong Song
356eaaacd23SYonghong Song ctx.meta = &meta;
357eaaacd23SYonghong Song ctx.task = info->task;
358eaaacd23SYonghong Song ctx.fd = info->fd;
359eaaacd23SYonghong Song ctx.file = file;
360eaaacd23SYonghong Song return bpf_iter_run_prog(prog, &ctx);
361eaaacd23SYonghong Song }
362eaaacd23SYonghong Song
task_file_seq_show(struct seq_file * seq,void * v)363eaaacd23SYonghong Song static int task_file_seq_show(struct seq_file *seq, void *v)
364eaaacd23SYonghong Song {
365eaaacd23SYonghong Song return __task_file_seq_show(seq, v, false);
366eaaacd23SYonghong Song }
367eaaacd23SYonghong Song
task_file_seq_stop(struct seq_file * seq,void * v)368eaaacd23SYonghong Song static void task_file_seq_stop(struct seq_file *seq, void *v)
369eaaacd23SYonghong Song {
370eaaacd23SYonghong Song struct bpf_iter_seq_task_file_info *info = seq->private;
371eaaacd23SYonghong Song
372eaaacd23SYonghong Song if (!v) {
373eaaacd23SYonghong Song (void)__task_file_seq_show(seq, v, true);
374eaaacd23SYonghong Song } else {
375eaaacd23SYonghong Song fput((struct file *)v);
376eaaacd23SYonghong Song put_task_struct(info->task);
377eaaacd23SYonghong Song info->task = NULL;
378eaaacd23SYonghong Song }
379eaaacd23SYonghong Song }
380eaaacd23SYonghong Song
init_seq_pidns(void * priv_data,struct bpf_iter_aux_info * aux)381eaaacd23SYonghong Song static int init_seq_pidns(void *priv_data, struct bpf_iter_aux_info *aux)
382eaaacd23SYonghong Song {
383eaaacd23SYonghong Song struct bpf_iter_seq_task_common *common = priv_data;
384eaaacd23SYonghong Song
385f9c79272SYonghong Song common->ns = get_pid_ns(task_active_pid_ns(current));
386eaaacd23SYonghong Song common->type = aux->task.type;
387eaaacd23SYonghong Song common->pid = aux->task.pid;
388eaaacd23SYonghong Song
389eaaacd23SYonghong Song return 0;
390f0d74c4dSKui-Feng Lee }
391f0d74c4dSKui-Feng Lee
fini_seq_pidns(void * priv_data)392f0d74c4dSKui-Feng Lee static void fini_seq_pidns(void *priv_data)
393eaaacd23SYonghong Song {
394eaaacd23SYonghong Song struct bpf_iter_seq_task_common *common = priv_data;
395eaaacd23SYonghong Song
396eaaacd23SYonghong Song put_pid_ns(common->ns);
397eaaacd23SYonghong Song }
398eaaacd23SYonghong Song
399eaaacd23SYonghong Song static const struct seq_operations task_file_seq_ops = {
400eaaacd23SYonghong Song .start = task_file_seq_start,
401eaaacd23SYonghong Song .next = task_file_seq_next,
402eaaacd23SYonghong Song .stop = task_file_seq_stop,
403eaaacd23SYonghong Song .show = task_file_seq_show,
404eaaacd23SYonghong Song };
405eaaacd23SYonghong Song
406eaaacd23SYonghong Song struct bpf_iter_seq_task_vma_info {
407eaaacd23SYonghong Song /* The first field must be struct bpf_iter_seq_task_common.
408eaaacd23SYonghong Song * this is assumed by {init, fini}_seq_pidns() callback functions.
409eaaacd23SYonghong Song */
4103a7b35b8SSong Liu struct bpf_iter_seq_task_common common;
4113a7b35b8SSong Liu struct task_struct *task;
4123a7b35b8SSong Liu struct mm_struct *mm;
4133a7b35b8SSong Liu struct vm_area_struct *vma;
4143a7b35b8SSong Liu u32 tid;
4153a7b35b8SSong Liu unsigned long prev_vm_start;
4167ff94f27SKui-Feng Lee unsigned long prev_vm_end;
4173a7b35b8SSong Liu };
4183a7b35b8SSong Liu
4193a7b35b8SSong Liu enum bpf_task_vma_iter_find_op {
4203a7b35b8SSong Liu task_vma_iter_first_vma, /* use find_vma() with addr 0 */
4213a7b35b8SSong Liu task_vma_iter_next_vma, /* use vma_next() with curr_vma */
4223a7b35b8SSong Liu task_vma_iter_find_vma, /* use find_vma() to find next vma */
4233a7b35b8SSong Liu };
424becc8cdbSLiam R. Howlett
425becc8cdbSLiam R. Howlett static struct vm_area_struct *
task_vma_seq_get_next(struct bpf_iter_seq_task_vma_info * info)4263a7b35b8SSong Liu task_vma_seq_get_next(struct bpf_iter_seq_task_vma_info *info)
4273a7b35b8SSong Liu {
4283a7b35b8SSong Liu enum bpf_task_vma_iter_find_op op;
4293a7b35b8SSong Liu struct vm_area_struct *curr_vma;
4303a7b35b8SSong Liu struct task_struct *curr_task;
4313a7b35b8SSong Liu struct mm_struct *curr_mm;
4323a7b35b8SSong Liu u32 saved_tid = info->tid;
4333a7b35b8SSong Liu
4343a7b35b8SSong Liu /* If this function returns a non-NULL vma, it holds a reference to
4357ff94f27SKui-Feng Lee * the task_struct, holds a refcount on mm->mm_users, and holds
436f0d74c4dSKui-Feng Lee * read lock on vma->mm->mmap_lock.
4373a7b35b8SSong Liu * If this function returns NULL, it does not hold any reference or
4383a7b35b8SSong Liu * lock.
4397ff94f27SKui-Feng Lee */
4407ff94f27SKui-Feng Lee if (info->task) {
4413a7b35b8SSong Liu curr_task = info->task;
4423a7b35b8SSong Liu curr_vma = info->vma;
4433a7b35b8SSong Liu curr_mm = info->mm;
4443a7b35b8SSong Liu /* In case of lock contention, drop mmap_lock to unblock
4453a7b35b8SSong Liu * the writer.
4463a7b35b8SSong Liu *
4477ff94f27SKui-Feng Lee * After relock, call find(mm, prev_vm_end - 1) to find
4483a7b35b8SSong Liu * new vma to process.
4493a7b35b8SSong Liu *
4503a7b35b8SSong Liu * +------+------+-----------+
4513a7b35b8SSong Liu * | VMA1 | VMA2 | VMA3 |
4523a7b35b8SSong Liu * +------+------+-----------+
4533a7b35b8SSong Liu * | | | |
4543a7b35b8SSong Liu * 4k 8k 16k 400k
4553a7b35b8SSong Liu *
4563a7b35b8SSong Liu * For example, curr_vma == VMA2. Before unlock, we set
4573a7b35b8SSong Liu *
4583a7b35b8SSong Liu * prev_vm_start = 8k
4593a7b35b8SSong Liu * prev_vm_end = 16k
4603a7b35b8SSong Liu *
4613a7b35b8SSong Liu * There are a few cases:
4623a7b35b8SSong Liu *
4633a7b35b8SSong Liu * 1) VMA2 is freed, but VMA3 exists.
4643a7b35b8SSong Liu *
4653a7b35b8SSong Liu * find_vma() will return VMA3, just process VMA3.
4663a7b35b8SSong Liu *
4673a7b35b8SSong Liu * 2) VMA2 still exists.
4683a7b35b8SSong Liu *
4693a7b35b8SSong Liu * find_vma() will return VMA2, process VMA2->next.
4703a7b35b8SSong Liu *
4713a7b35b8SSong Liu * 3) no more vma in this mm.
4723a7b35b8SSong Liu *
4733a7b35b8SSong Liu * Process the next task.
4743a7b35b8SSong Liu *
4753a7b35b8SSong Liu * 4) find_vma() returns a different vma, VMA2'.
4763a7b35b8SSong Liu *
4773a7b35b8SSong Liu * 4.1) If VMA2 covers same range as VMA2', skip VMA2',
4783a7b35b8SSong Liu * because we already covered the range;
4793a7b35b8SSong Liu * 4.2) VMA2 and VMA2' covers different ranges, process
4803a7b35b8SSong Liu * VMA2'.
4813a7b35b8SSong Liu */
4823a7b35b8SSong Liu if (mmap_lock_is_contended(curr_mm)) {
4833a7b35b8SSong Liu info->prev_vm_start = curr_vma->vm_start;
4843a7b35b8SSong Liu info->prev_vm_end = curr_vma->vm_end;
4853a7b35b8SSong Liu op = task_vma_iter_find_vma;
4867ff94f27SKui-Feng Lee mmap_read_unlock(curr_mm);
4873a7b35b8SSong Liu if (mmap_read_lock_killable(curr_mm)) {
4883a7b35b8SSong Liu mmput(curr_mm);
4893a7b35b8SSong Liu goto finish;
4907ff94f27SKui-Feng Lee }
4917ff94f27SKui-Feng Lee } else {
4927ff94f27SKui-Feng Lee op = task_vma_iter_next_vma;
4933a7b35b8SSong Liu }
4947ff94f27SKui-Feng Lee } else {
4953a7b35b8SSong Liu again:
4963a7b35b8SSong Liu curr_task = task_seq_get_next(&info->common, &info->tid, true);
4973a7b35b8SSong Liu if (!curr_task) {
4983a7b35b8SSong Liu info->tid++;
4993a7b35b8SSong Liu goto finish;
500f0d74c4dSKui-Feng Lee }
5013a7b35b8SSong Liu
502f0d74c4dSKui-Feng Lee if (saved_tid != info->tid) {
5033a7b35b8SSong Liu /* new task, process the first vma */
5043a7b35b8SSong Liu op = task_vma_iter_first_vma;
5053a7b35b8SSong Liu } else {
506f0d74c4dSKui-Feng Lee /* Found the same tid, which means the user space
5073a7b35b8SSong Liu * finished data in previous buffer and read more.
5083a7b35b8SSong Liu * We dropped mmap_lock before returning to user
5093a7b35b8SSong Liu * space, so it is necessary to use find_vma() to
5103a7b35b8SSong Liu * find the next vma to process.
5113a7b35b8SSong Liu */
5123a7b35b8SSong Liu op = task_vma_iter_find_vma;
5133a7b35b8SSong Liu }
5143a7b35b8SSong Liu
5153a7b35b8SSong Liu curr_mm = get_task_mm(curr_task);
5163a7b35b8SSong Liu if (!curr_mm)
5173a7b35b8SSong Liu goto next_task;
5183a7b35b8SSong Liu
5197ff94f27SKui-Feng Lee if (mmap_read_lock_killable(curr_mm)) {
5207ff94f27SKui-Feng Lee mmput(curr_mm);
5213a7b35b8SSong Liu goto finish;
5223a7b35b8SSong Liu }
5237ff94f27SKui-Feng Lee }
5247ff94f27SKui-Feng Lee
5253a7b35b8SSong Liu switch (op) {
5263a7b35b8SSong Liu case task_vma_iter_first_vma:
5277ff94f27SKui-Feng Lee curr_vma = find_vma(curr_mm, 0);
5283a7b35b8SSong Liu break;
5293a7b35b8SSong Liu case task_vma_iter_next_vma:
5303a7b35b8SSong Liu curr_vma = find_vma(curr_mm, curr_vma->vm_end);
5317ff94f27SKui-Feng Lee break;
5323a7b35b8SSong Liu case task_vma_iter_find_vma:
5333a7b35b8SSong Liu /* We dropped mmap_lock so it is necessary to use find_vma
5347ff94f27SKui-Feng Lee * to find the next vma. This is similar to the mechanism
5353a7b35b8SSong Liu * in show_smaps_rollup().
5363a7b35b8SSong Liu */
5373a7b35b8SSong Liu curr_vma = find_vma(curr_mm, info->prev_vm_end - 1);
5383a7b35b8SSong Liu /* case 1) and 4.2) above just use curr_vma */
5393a7b35b8SSong Liu
5403a7b35b8SSong Liu /* check for case 2) or case 4.1) above */
5417ff94f27SKui-Feng Lee if (curr_vma &&
5423a7b35b8SSong Liu curr_vma->vm_start == info->prev_vm_start &&
5433a7b35b8SSong Liu curr_vma->vm_end == info->prev_vm_end)
5443a7b35b8SSong Liu curr_vma = find_vma(curr_mm, curr_vma->vm_end);
5453a7b35b8SSong Liu break;
5463a7b35b8SSong Liu }
5473a7b35b8SSong Liu if (!curr_vma) {
5487ff94f27SKui-Feng Lee /* case 3) above, or case 2) 4.1) with vma->next == NULL */
5493a7b35b8SSong Liu mmap_read_unlock(curr_mm);
5503a7b35b8SSong Liu mmput(curr_mm);
5513a7b35b8SSong Liu goto next_task;
5523a7b35b8SSong Liu }
5537ff94f27SKui-Feng Lee info->task = curr_task;
5547ff94f27SKui-Feng Lee info->vma = curr_vma;
5553a7b35b8SSong Liu info->mm = curr_mm;
5563a7b35b8SSong Liu return curr_vma;
5573a7b35b8SSong Liu
5583a7b35b8SSong Liu next_task:
5597ff94f27SKui-Feng Lee if (info->common.type == BPF_TASK_ITER_TID)
5603a7b35b8SSong Liu goto finish;
5613a7b35b8SSong Liu
5623a7b35b8SSong Liu put_task_struct(curr_task);
563f0d74c4dSKui-Feng Lee info->task = NULL;
564f0d74c4dSKui-Feng Lee info->mm = NULL;
565f0d74c4dSKui-Feng Lee info->tid++;
5663a7b35b8SSong Liu goto again;
5673a7b35b8SSong Liu
5687ff94f27SKui-Feng Lee finish:
569f0d74c4dSKui-Feng Lee if (curr_task)
5703a7b35b8SSong Liu put_task_struct(curr_task);
5713a7b35b8SSong Liu info->task = NULL;
5723a7b35b8SSong Liu info->vma = NULL;
5733a7b35b8SSong Liu info->mm = NULL;
5743a7b35b8SSong Liu return NULL;
5753a7b35b8SSong Liu }
5763a7b35b8SSong Liu
task_vma_seq_start(struct seq_file * seq,loff_t * pos)5777ff94f27SKui-Feng Lee static void *task_vma_seq_start(struct seq_file *seq, loff_t *pos)
5783a7b35b8SSong Liu {
5793a7b35b8SSong Liu struct bpf_iter_seq_task_vma_info *info = seq->private;
5803a7b35b8SSong Liu struct vm_area_struct *vma;
5813a7b35b8SSong Liu
5823a7b35b8SSong Liu vma = task_vma_seq_get_next(info);
5833a7b35b8SSong Liu if (vma && *pos == 0)
5843a7b35b8SSong Liu ++*pos;
5853a7b35b8SSong Liu
5863a7b35b8SSong Liu return vma;
5873a7b35b8SSong Liu }
5883a7b35b8SSong Liu
task_vma_seq_next(struct seq_file * seq,void * v,loff_t * pos)5893a7b35b8SSong Liu static void *task_vma_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5903a7b35b8SSong Liu {
5913a7b35b8SSong Liu struct bpf_iter_seq_task_vma_info *info = seq->private;
5923a7b35b8SSong Liu
5933a7b35b8SSong Liu ++*pos;
5943a7b35b8SSong Liu return task_vma_seq_get_next(info);
5953a7b35b8SSong Liu }
5963a7b35b8SSong Liu
5973a7b35b8SSong Liu struct bpf_iter__task_vma {
5983a7b35b8SSong Liu __bpf_md_ptr(struct bpf_iter_meta *, meta);
5993a7b35b8SSong Liu __bpf_md_ptr(struct task_struct *, task);
6003a7b35b8SSong Liu __bpf_md_ptr(struct vm_area_struct *, vma);
6013a7b35b8SSong Liu };
6023a7b35b8SSong Liu
DEFINE_BPF_ITER_FUNC(task_vma,struct bpf_iter_meta * meta,struct task_struct * task,struct vm_area_struct * vma)6033a7b35b8SSong Liu DEFINE_BPF_ITER_FUNC(task_vma, struct bpf_iter_meta *meta,
6043a7b35b8SSong Liu struct task_struct *task, struct vm_area_struct *vma)
6053a7b35b8SSong Liu
6063a7b35b8SSong Liu static int __task_vma_seq_show(struct seq_file *seq, bool in_stop)
6073a7b35b8SSong Liu {
6083a7b35b8SSong Liu struct bpf_iter_seq_task_vma_info *info = seq->private;
6093a7b35b8SSong Liu struct bpf_iter__task_vma ctx;
6103a7b35b8SSong Liu struct bpf_iter_meta meta;
6113a7b35b8SSong Liu struct bpf_prog *prog;
6123a7b35b8SSong Liu
6133a7b35b8SSong Liu meta.seq = seq;
6143a7b35b8SSong Liu prog = bpf_iter_get_info(&meta, in_stop);
6153a7b35b8SSong Liu if (!prog)
6163a7b35b8SSong Liu return 0;
6173a7b35b8SSong Liu
6183a7b35b8SSong Liu ctx.meta = &meta;
6193a7b35b8SSong Liu ctx.task = info->task;
6203a7b35b8SSong Liu ctx.vma = info->vma;
6213a7b35b8SSong Liu return bpf_iter_run_prog(prog, &ctx);
6223a7b35b8SSong Liu }
6233a7b35b8SSong Liu
task_vma_seq_show(struct seq_file * seq,void * v)6243a7b35b8SSong Liu static int task_vma_seq_show(struct seq_file *seq, void *v)
6253a7b35b8SSong Liu {
6263a7b35b8SSong Liu return __task_vma_seq_show(seq, false);
6273a7b35b8SSong Liu }
6283a7b35b8SSong Liu
task_vma_seq_stop(struct seq_file * seq,void * v)6293a7b35b8SSong Liu static void task_vma_seq_stop(struct seq_file *seq, void *v)
6303a7b35b8SSong Liu {
6313a7b35b8SSong Liu struct bpf_iter_seq_task_vma_info *info = seq->private;
6323a7b35b8SSong Liu
6333a7b35b8SSong Liu if (!v) {
6343a7b35b8SSong Liu (void)__task_vma_seq_show(seq, true);
6353a7b35b8SSong Liu } else {
6363a7b35b8SSong Liu /* info->vma has not been seen by the BPF program. If the
6373a7b35b8SSong Liu * user space reads more, task_vma_seq_get_next should
6383a7b35b8SSong Liu * return this vma again. Set prev_vm_start to ~0UL,
6393a7b35b8SSong Liu * so that we don't skip the vma returned by the next
6403a7b35b8SSong Liu * find_vma() (case task_vma_iter_find_vma in
6413a7b35b8SSong Liu * task_vma_seq_get_next()).
6423a7b35b8SSong Liu */
6433a7b35b8SSong Liu info->prev_vm_start = ~0UL;
6443a7b35b8SSong Liu info->prev_vm_end = info->vma->vm_end;
6453a7b35b8SSong Liu mmap_read_unlock(info->mm);
6463a7b35b8SSong Liu mmput(info->mm);
6473a7b35b8SSong Liu info->mm = NULL;
6483a7b35b8SSong Liu put_task_struct(info->task);
6497ff94f27SKui-Feng Lee info->task = NULL;
6507ff94f27SKui-Feng Lee }
6517ff94f27SKui-Feng Lee }
6523a7b35b8SSong Liu
6533a7b35b8SSong Liu static const struct seq_operations task_vma_seq_ops = {
6543a7b35b8SSong Liu .start = task_vma_seq_start,
6553a7b35b8SSong Liu .next = task_vma_seq_next,
6563a7b35b8SSong Liu .stop = task_vma_seq_stop,
6573a7b35b8SSong Liu .show = task_vma_seq_show,
6583a7b35b8SSong Liu };
6593a7b35b8SSong Liu
6603a7b35b8SSong Liu static const struct bpf_iter_seq_info task_seq_info = {
6613a7b35b8SSong Liu .seq_ops = &task_seq_ops,
6623a7b35b8SSong Liu .init_seq_private = init_seq_pidns,
6633a7b35b8SSong Liu .fini_seq_private = fini_seq_pidns,
66414fc6bd6SYonghong Song .seq_priv_size = sizeof(struct bpf_iter_seq_task_info),
665eaaacd23SYonghong Song };
666eaaacd23SYonghong Song
bpf_iter_fill_link_info(const struct bpf_iter_aux_info * aux,struct bpf_link_info * info)667eaaacd23SYonghong Song static int bpf_iter_fill_link_info(const struct bpf_iter_aux_info *aux, struct bpf_link_info *info)
668eaaacd23SYonghong Song {
66914fc6bd6SYonghong Song switch (aux->task.type) {
67014fc6bd6SYonghong Song case BPF_TASK_ITER_TID:
67121fb6f2aSKui-Feng Lee info->iter.task.tid = aux->task.pid;
67221fb6f2aSKui-Feng Lee break;
67321fb6f2aSKui-Feng Lee case BPF_TASK_ITER_TGID:
67421fb6f2aSKui-Feng Lee info->iter.task.pid = aux->task.pid;
67521fb6f2aSKui-Feng Lee break;
67621fb6f2aSKui-Feng Lee default:
67721fb6f2aSKui-Feng Lee break;
67821fb6f2aSKui-Feng Lee }
67921fb6f2aSKui-Feng Lee return 0;
68021fb6f2aSKui-Feng Lee }
68121fb6f2aSKui-Feng Lee
bpf_iter_task_show_fdinfo(const struct bpf_iter_aux_info * aux,struct seq_file * seq)68221fb6f2aSKui-Feng Lee static void bpf_iter_task_show_fdinfo(const struct bpf_iter_aux_info *aux, struct seq_file *seq)
68321fb6f2aSKui-Feng Lee {
68421fb6f2aSKui-Feng Lee seq_printf(seq, "task_type:\t%s\n", iter_task_type_names[aux->task.type]);
68521fb6f2aSKui-Feng Lee if (aux->task.type == BPF_TASK_ITER_TID)
6862c4fe44fSKui-Feng Lee seq_printf(seq, "tid:\t%u\n", aux->task.pid);
6872c4fe44fSKui-Feng Lee else if (aux->task.type == BPF_TASK_ITER_TGID)
6882c4fe44fSKui-Feng Lee seq_printf(seq, "pid:\t%u\n", aux->task.pid);
6892c4fe44fSKui-Feng Lee }
6902c4fe44fSKui-Feng Lee
6912c4fe44fSKui-Feng Lee static struct bpf_iter_reg task_reg_info = {
6922c4fe44fSKui-Feng Lee .target = "task",
6932c4fe44fSKui-Feng Lee .attach_target = bpf_iter_attach_task,
6942c4fe44fSKui-Feng Lee .feature = BPF_ITER_RESCHED,
69514fc6bd6SYonghong Song .ctx_arg_info_size = 1,
69614fc6bd6SYonghong Song .ctx_arg_info = {
697f0d74c4dSKui-Feng Lee { offsetof(struct bpf_iter__task, task),
698cf83b2d2SYonghong Song PTR_TO_BTF_ID_OR_NULL | PTR_TRUSTED },
6993c32cc1bSYonghong Song },
7003c32cc1bSYonghong Song .seq_info = &task_seq_info,
7013c32cc1bSYonghong Song .fill_link_info = bpf_iter_fill_link_info,
7020de4f50dSChuyi Zhou .show_fdinfo = bpf_iter_task_show_fdinfo,
7033c32cc1bSYonghong Song };
70414fc6bd6SYonghong Song
70521fb6f2aSKui-Feng Lee static const struct bpf_iter_seq_info task_file_seq_info = {
7062c4fe44fSKui-Feng Lee .seq_ops = &task_file_seq_ops,
707eaaacd23SYonghong Song .init_seq_private = init_seq_pidns,
70815172a46SYonghong Song .fini_seq_private = fini_seq_pidns,
70914fc6bd6SYonghong Song .seq_priv_size = sizeof(struct bpf_iter_seq_task_file_info),
71015172a46SYonghong Song };
71115172a46SYonghong Song
71215172a46SYonghong Song static struct bpf_iter_reg task_file_reg_info = {
71315172a46SYonghong Song .target = "task_file",
71414fc6bd6SYonghong Song .attach_target = bpf_iter_attach_task,
71514fc6bd6SYonghong Song .feature = BPF_ITER_RESCHED,
71614fc6bd6SYonghong Song .ctx_arg_info_size = 2,
71714fc6bd6SYonghong Song .ctx_arg_info = {
718f0d74c4dSKui-Feng Lee { offsetof(struct bpf_iter__task_file, task),
719cf83b2d2SYonghong Song PTR_TO_BTF_ID_OR_NULL },
7203c32cc1bSYonghong Song { offsetof(struct bpf_iter__task_file, file),
7213c32cc1bSYonghong Song PTR_TO_BTF_ID_OR_NULL },
7223c32cc1bSYonghong Song },
7233c32cc1bSYonghong Song .seq_info = &task_file_seq_info,
7243c32cc1bSYonghong Song .fill_link_info = bpf_iter_fill_link_info,
7253c32cc1bSYonghong Song .show_fdinfo = bpf_iter_task_show_fdinfo,
7263c32cc1bSYonghong Song };
72714fc6bd6SYonghong Song
72821fb6f2aSKui-Feng Lee static const struct bpf_iter_seq_info task_vma_seq_info = {
7292c4fe44fSKui-Feng Lee .seq_ops = &task_vma_seq_ops,
73015172a46SYonghong Song .init_seq_private = init_seq_pidns,
73115172a46SYonghong Song .fini_seq_private = fini_seq_pidns,
7323a7b35b8SSong Liu .seq_priv_size = sizeof(struct bpf_iter_seq_task_vma_info),
7333a7b35b8SSong Liu };
7343a7b35b8SSong Liu
7353a7b35b8SSong Liu static struct bpf_iter_reg task_vma_reg_info = {
7363a7b35b8SSong Liu .target = "task_vma",
7373a7b35b8SSong Liu .attach_target = bpf_iter_attach_task,
7383a7b35b8SSong Liu .feature = BPF_ITER_RESCHED,
7393a7b35b8SSong Liu .ctx_arg_info_size = 2,
7403a7b35b8SSong Liu .ctx_arg_info = {
741f0d74c4dSKui-Feng Lee { offsetof(struct bpf_iter__task_vma, task),
7423a7b35b8SSong Liu PTR_TO_BTF_ID_OR_NULL },
7433a7b35b8SSong Liu { offsetof(struct bpf_iter__task_vma, vma),
7443a7b35b8SSong Liu PTR_TO_BTF_ID_OR_NULL },
7453a7b35b8SSong Liu },
7463a7b35b8SSong Liu .seq_info = &task_vma_seq_info,
7473a7b35b8SSong Liu .fill_link_info = bpf_iter_fill_link_info,
7483a7b35b8SSong Liu .show_fdinfo = bpf_iter_task_show_fdinfo,
7493a7b35b8SSong Liu };
7503a7b35b8SSong Liu
BPF_CALL_5(bpf_find_vma,struct task_struct *,task,u64,start,bpf_callback_t,callback_fn,void *,callback_ctx,u64,flags)75121fb6f2aSKui-Feng Lee BPF_CALL_5(bpf_find_vma, struct task_struct *, task, u64, start,
7522c4fe44fSKui-Feng Lee bpf_callback_t, callback_fn, void *, callback_ctx, u64, flags)
7533a7b35b8SSong Liu {
7543a7b35b8SSong Liu struct mmap_unlock_irq_work *work = NULL;
7557c7e3d31SSong Liu struct vm_area_struct *vma;
7567c7e3d31SSong Liu bool irq_work_busy = false;
7577c7e3d31SSong Liu struct mm_struct *mm;
7587c7e3d31SSong Liu int ret = -ENOENT;
7597c7e3d31SSong Liu
7607c7e3d31SSong Liu if (flags)
7617c7e3d31SSong Liu return -EINVAL;
7627c7e3d31SSong Liu
7637c7e3d31SSong Liu if (!task)
7647c7e3d31SSong Liu return -ENOENT;
7657c7e3d31SSong Liu
7667c7e3d31SSong Liu mm = task->mm;
7677c7e3d31SSong Liu if (!mm)
7687c7e3d31SSong Liu return -ENOENT;
7697c7e3d31SSong Liu
7707c7e3d31SSong Liu irq_work_busy = bpf_mmap_unlock_get_irq_work(&work);
7717c7e3d31SSong Liu
7727c7e3d31SSong Liu if (irq_work_busy || !mmap_read_trylock(mm))
7737c7e3d31SSong Liu return -EBUSY;
7747c7e3d31SSong Liu
7757c7e3d31SSong Liu vma = find_vma(mm, start);
7767c7e3d31SSong Liu
7777c7e3d31SSong Liu if (vma && vma->vm_start <= start && vma->vm_end > start) {
7787c7e3d31SSong Liu callback_fn((u64)(long)task, (u64)(long)vma,
7797c7e3d31SSong Liu (u64)(long)callback_ctx, 0, 0);
7807c7e3d31SSong Liu ret = 0;
7817c7e3d31SSong Liu }
7827c7e3d31SSong Liu bpf_mmap_unlock_mm(work, mm);
7837c7e3d31SSong Liu return ret;
7847c7e3d31SSong Liu }
7857c7e3d31SSong Liu
7867c7e3d31SSong Liu const struct bpf_func_proto bpf_find_vma_proto = {
7877c7e3d31SSong Liu .func = bpf_find_vma,
7887c7e3d31SSong Liu .ret_type = RET_INTEGER,
7897c7e3d31SSong Liu .arg1_type = ARG_PTR_TO_BTF_ID,
7907c7e3d31SSong Liu .arg1_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
7917c7e3d31SSong Liu .arg2_type = ARG_ANYTHING,
7927c7e3d31SSong Liu .arg3_type = ARG_PTR_TO_FUNC,
7937c7e3d31SSong Liu .arg4_type = ARG_PTR_TO_STACK_OR_NULL,
794d19ddb47SSong Liu .arg5_type = ARG_ANYTHING,
7957c7e3d31SSong Liu };
7967c7e3d31SSong Liu
7977c7e3d31SSong Liu struct bpf_iter_task_vma_kern_data {
7987c7e3d31SSong Liu struct task_struct *task;
7997c7e3d31SSong Liu struct mm_struct *mm;
8007c7e3d31SSong Liu struct mmap_unlock_irq_work *work;
8014ac45468SDave Marchevsky struct vma_iterator vmi;
8024ac45468SDave Marchevsky };
8034ac45468SDave Marchevsky
8044ac45468SDave Marchevsky struct bpf_iter_task_vma {
8054ac45468SDave Marchevsky /* opaque iterator state; having __u64 here allows to preserve correct
8064ac45468SDave Marchevsky * alignment requirements in vmlinux.h, generated from BTF
8074ac45468SDave Marchevsky */
8084ac45468SDave Marchevsky __u64 __opaque[1];
8094ac45468SDave Marchevsky } __attribute__((aligned(8)));
8104ac45468SDave Marchevsky
8114ac45468SDave Marchevsky /* Non-opaque version of bpf_iter_task_vma */
8124ac45468SDave Marchevsky struct bpf_iter_task_vma_kern {
8134ac45468SDave Marchevsky struct bpf_iter_task_vma_kern_data *data;
8144ac45468SDave Marchevsky } __attribute__((aligned(8)));
8154ac45468SDave Marchevsky
8164ac45468SDave Marchevsky __bpf_kfunc_start_defs();
8174ac45468SDave Marchevsky
bpf_iter_task_vma_new(struct bpf_iter_task_vma * it,struct task_struct * task,u64 addr)8184ac45468SDave Marchevsky __bpf_kfunc int bpf_iter_task_vma_new(struct bpf_iter_task_vma *it,
8194ac45468SDave Marchevsky struct task_struct *task, u64 addr)
820391145baSDave Marchevsky {
8214ac45468SDave Marchevsky struct bpf_iter_task_vma_kern *kit = (void *)it;
8224ac45468SDave Marchevsky bool irq_work_busy = false;
8234ac45468SDave Marchevsky int err;
8244ac45468SDave Marchevsky
8254ac45468SDave Marchevsky BUILD_BUG_ON(sizeof(struct bpf_iter_task_vma_kern) != sizeof(struct bpf_iter_task_vma));
8264ac45468SDave Marchevsky BUILD_BUG_ON(__alignof__(struct bpf_iter_task_vma_kern) != __alignof__(struct bpf_iter_task_vma));
8274ac45468SDave Marchevsky
8284ac45468SDave Marchevsky /* is_iter_reg_valid_uninit guarantees that kit hasn't been initialized
8294ac45468SDave Marchevsky * before, so non-NULL kit->data doesn't point to previously
8304ac45468SDave Marchevsky * bpf_mem_alloc'd bpf_iter_task_vma_kern_data
8314ac45468SDave Marchevsky */
8324ac45468SDave Marchevsky kit->data = bpf_mem_alloc(&bpf_global_ma, sizeof(struct bpf_iter_task_vma_kern_data));
8334ac45468SDave Marchevsky if (!kit->data)
8344ac45468SDave Marchevsky return -ENOMEM;
8354ac45468SDave Marchevsky
8364ac45468SDave Marchevsky kit->data->task = get_task_struct(task);
8374ac45468SDave Marchevsky kit->data->mm = task->mm;
8384ac45468SDave Marchevsky if (!kit->data->mm) {
8394ac45468SDave Marchevsky err = -ENOENT;
8404ac45468SDave Marchevsky goto err_cleanup_iter;
8414ac45468SDave Marchevsky }
8424ac45468SDave Marchevsky
8434ac45468SDave Marchevsky /* kit->data->work == NULL is valid after bpf_mmap_unlock_get_irq_work */
8444ac45468SDave Marchevsky irq_work_busy = bpf_mmap_unlock_get_irq_work(&kit->data->work);
8454ac45468SDave Marchevsky if (irq_work_busy || !mmap_read_trylock(kit->data->mm)) {
8464ac45468SDave Marchevsky err = -EBUSY;
8474ac45468SDave Marchevsky goto err_cleanup_iter;
8484ac45468SDave Marchevsky }
8494ac45468SDave Marchevsky
8504ac45468SDave Marchevsky vma_iter_init(&kit->data->vmi, kit->data->mm, addr);
8514ac45468SDave Marchevsky return 0;
8524ac45468SDave Marchevsky
8534ac45468SDave Marchevsky err_cleanup_iter:
8544ac45468SDave Marchevsky if (kit->data->task)
8554ac45468SDave Marchevsky put_task_struct(kit->data->task);
8564ac45468SDave Marchevsky bpf_mem_free(&bpf_global_ma, kit->data);
8574ac45468SDave Marchevsky /* NULL kit->data signals failed bpf_iter_task_vma initialization */
8584ac45468SDave Marchevsky kit->data = NULL;
8594ac45468SDave Marchevsky return err;
8604ac45468SDave Marchevsky }
8614ac45468SDave Marchevsky
bpf_iter_task_vma_next(struct bpf_iter_task_vma * it)8624ac45468SDave Marchevsky __bpf_kfunc struct vm_area_struct *bpf_iter_task_vma_next(struct bpf_iter_task_vma *it)
8634ac45468SDave Marchevsky {
8644ac45468SDave Marchevsky struct bpf_iter_task_vma_kern *kit = (void *)it;
8654ac45468SDave Marchevsky
8664ac45468SDave Marchevsky if (!kit->data) /* bpf_iter_task_vma_new failed */
8674ac45468SDave Marchevsky return NULL;
8684ac45468SDave Marchevsky return vma_next(&kit->data->vmi);
8694ac45468SDave Marchevsky }
8704ac45468SDave Marchevsky
bpf_iter_task_vma_destroy(struct bpf_iter_task_vma * it)8714ac45468SDave Marchevsky __bpf_kfunc void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it)
8724ac45468SDave Marchevsky {
8734ac45468SDave Marchevsky struct bpf_iter_task_vma_kern *kit = (void *)it;
8744ac45468SDave Marchevsky
8754ac45468SDave Marchevsky if (kit->data) {
8764ac45468SDave Marchevsky bpf_mmap_unlock_mm(kit->data->work, kit->data->mm);
8774ac45468SDave Marchevsky put_task_struct(kit->data->task);
8784ac45468SDave Marchevsky bpf_mem_free(&bpf_global_ma, kit->data);
8794ac45468SDave Marchevsky }
8804ac45468SDave Marchevsky }
8814ac45468SDave Marchevsky
8824ac45468SDave Marchevsky __bpf_kfunc_end_defs();
8834ac45468SDave Marchevsky
8844ac45468SDave Marchevsky #ifdef CONFIG_CGROUPS
8854ac45468SDave Marchevsky
886391145baSDave Marchevsky struct bpf_iter_css_task {
8874ac45468SDave Marchevsky __u64 __opaque[1];
88805670f81SMatthieu Baerts } __attribute__((aligned(8)));
88905670f81SMatthieu Baerts
8909c66dc94SChuyi Zhou struct bpf_iter_css_task_kern {
8919c66dc94SChuyi Zhou struct css_task_iter *css_it;
8929c66dc94SChuyi Zhou } __attribute__((aligned(8)));
8939c66dc94SChuyi Zhou
8949c66dc94SChuyi Zhou __bpf_kfunc_start_defs();
8959c66dc94SChuyi Zhou
bpf_iter_css_task_new(struct bpf_iter_css_task * it,struct cgroup_subsys_state * css,unsigned int flags)8969c66dc94SChuyi Zhou __bpf_kfunc int bpf_iter_css_task_new(struct bpf_iter_css_task *it,
8979c66dc94SChuyi Zhou struct cgroup_subsys_state *css, unsigned int flags)
898391145baSDave Marchevsky {
8999c66dc94SChuyi Zhou struct bpf_iter_css_task_kern *kit = (void *)it;
9009c66dc94SChuyi Zhou
9019c66dc94SChuyi Zhou BUILD_BUG_ON(sizeof(struct bpf_iter_css_task_kern) != sizeof(struct bpf_iter_css_task));
9029c66dc94SChuyi Zhou BUILD_BUG_ON(__alignof__(struct bpf_iter_css_task_kern) !=
9039c66dc94SChuyi Zhou __alignof__(struct bpf_iter_css_task));
9049c66dc94SChuyi Zhou kit->css_it = NULL;
9059c66dc94SChuyi Zhou switch (flags) {
9069c66dc94SChuyi Zhou case CSS_TASK_ITER_PROCS | CSS_TASK_ITER_THREADED:
9079c66dc94SChuyi Zhou case CSS_TASK_ITER_PROCS:
9089c66dc94SChuyi Zhou case 0:
9099c66dc94SChuyi Zhou break;
9109c66dc94SChuyi Zhou default:
9119c66dc94SChuyi Zhou return -EINVAL;
9129c66dc94SChuyi Zhou }
9139c66dc94SChuyi Zhou
9149c66dc94SChuyi Zhou kit->css_it = bpf_mem_alloc(&bpf_global_ma, sizeof(struct css_task_iter));
9159c66dc94SChuyi Zhou if (!kit->css_it)
9169c66dc94SChuyi Zhou return -ENOMEM;
9179c66dc94SChuyi Zhou css_task_iter_start(css, flags, kit->css_it);
9189c66dc94SChuyi Zhou return 0;
9199c66dc94SChuyi Zhou }
9209c66dc94SChuyi Zhou
bpf_iter_css_task_next(struct bpf_iter_css_task * it)9219c66dc94SChuyi Zhou __bpf_kfunc struct task_struct *bpf_iter_css_task_next(struct bpf_iter_css_task *it)
9229c66dc94SChuyi Zhou {
9239c66dc94SChuyi Zhou struct bpf_iter_css_task_kern *kit = (void *)it;
9249c66dc94SChuyi Zhou
9259c66dc94SChuyi Zhou if (!kit->css_it)
9269c66dc94SChuyi Zhou return NULL;
9279c66dc94SChuyi Zhou return css_task_iter_next(kit->css_it);
9289c66dc94SChuyi Zhou }
9299c66dc94SChuyi Zhou
bpf_iter_css_task_destroy(struct bpf_iter_css_task * it)9309c66dc94SChuyi Zhou __bpf_kfunc void bpf_iter_css_task_destroy(struct bpf_iter_css_task *it)
9319c66dc94SChuyi Zhou {
9329c66dc94SChuyi Zhou struct bpf_iter_css_task_kern *kit = (void *)it;
9339c66dc94SChuyi Zhou
9349c66dc94SChuyi Zhou if (!kit->css_it)
9359c66dc94SChuyi Zhou return;
9369c66dc94SChuyi Zhou css_task_iter_end(kit->css_it);
9379c66dc94SChuyi Zhou bpf_mem_free(&bpf_global_ma, kit->css_it);
9389c66dc94SChuyi Zhou }
9399c66dc94SChuyi Zhou
9409c66dc94SChuyi Zhou __bpf_kfunc_end_defs();
9419c66dc94SChuyi Zhou
9429c66dc94SChuyi Zhou #endif /* CONFIG_CGROUPS */
9439c66dc94SChuyi Zhou
944391145baSDave Marchevsky struct bpf_iter_task {
9459c66dc94SChuyi Zhou __u64 __opaque[3];
94605670f81SMatthieu Baerts } __attribute__((aligned(8)));
94705670f81SMatthieu Baerts
948c68a78ffSChuyi Zhou struct bpf_iter_task_kern {
949c68a78ffSChuyi Zhou struct task_struct *task;
950c68a78ffSChuyi Zhou struct task_struct *pos;
951c68a78ffSChuyi Zhou unsigned int flags;
952c68a78ffSChuyi Zhou } __attribute__((aligned(8)));
953c68a78ffSChuyi Zhou
954c68a78ffSChuyi Zhou enum {
955c68a78ffSChuyi Zhou /* all process in the system */
956c68a78ffSChuyi Zhou BPF_TASK_ITER_ALL_PROCS,
957c68a78ffSChuyi Zhou /* all threads in the system */
958c68a78ffSChuyi Zhou BPF_TASK_ITER_ALL_THREADS,
959c68a78ffSChuyi Zhou /* all threads of a specific process */
960c68a78ffSChuyi Zhou BPF_TASK_ITER_PROC_THREADS
961c68a78ffSChuyi Zhou };
962c68a78ffSChuyi Zhou
963c68a78ffSChuyi Zhou __bpf_kfunc_start_defs();
964c68a78ffSChuyi Zhou
bpf_iter_task_new(struct bpf_iter_task * it,struct task_struct * task__nullable,unsigned int flags)965c68a78ffSChuyi Zhou __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it,
966c68a78ffSChuyi Zhou struct task_struct *task__nullable, unsigned int flags)
967391145baSDave Marchevsky {
968c68a78ffSChuyi Zhou struct bpf_iter_task_kern *kit = (void *)it;
969c68a78ffSChuyi Zhou
970cb3ecf79SChuyi Zhou BUILD_BUG_ON(sizeof(struct bpf_iter_task_kern) > sizeof(struct bpf_iter_task));
971c68a78ffSChuyi Zhou BUILD_BUG_ON(__alignof__(struct bpf_iter_task_kern) !=
972c68a78ffSChuyi Zhou __alignof__(struct bpf_iter_task));
973c68a78ffSChuyi Zhou
974c68a78ffSChuyi Zhou kit->pos = NULL;
975c68a78ffSChuyi Zhou
976c68a78ffSChuyi Zhou switch (flags) {
977c68a78ffSChuyi Zhou case BPF_TASK_ITER_ALL_THREADS:
9785f2ae606SYafang Shao case BPF_TASK_ITER_ALL_PROCS:
9795f2ae606SYafang Shao break;
980c68a78ffSChuyi Zhou case BPF_TASK_ITER_PROC_THREADS:
981c68a78ffSChuyi Zhou if (!task__nullable)
982c68a78ffSChuyi Zhou return -EINVAL;
983cb3ecf79SChuyi Zhou break;
984c68a78ffSChuyi Zhou default:
985cb3ecf79SChuyi Zhou return -EINVAL;
986cb3ecf79SChuyi Zhou }
987c68a78ffSChuyi Zhou
988c68a78ffSChuyi Zhou if (flags == BPF_TASK_ITER_PROC_THREADS)
989c68a78ffSChuyi Zhou kit->task = task__nullable;
990c68a78ffSChuyi Zhou else
991c68a78ffSChuyi Zhou kit->task = &init_task;
992c68a78ffSChuyi Zhou kit->pos = kit->task;
993cb3ecf79SChuyi Zhou kit->flags = flags;
994c68a78ffSChuyi Zhou return 0;
995c68a78ffSChuyi Zhou }
996c68a78ffSChuyi Zhou
bpf_iter_task_next(struct bpf_iter_task * it)997c68a78ffSChuyi Zhou __bpf_kfunc struct task_struct *bpf_iter_task_next(struct bpf_iter_task *it)
998c68a78ffSChuyi Zhou {
999c68a78ffSChuyi Zhou struct bpf_iter_task_kern *kit = (void *)it;
1000c68a78ffSChuyi Zhou struct task_struct *pos;
1001c68a78ffSChuyi Zhou unsigned int flags;
1002c68a78ffSChuyi Zhou
1003c68a78ffSChuyi Zhou flags = kit->flags;
1004c68a78ffSChuyi Zhou pos = kit->pos;
1005c68a78ffSChuyi Zhou
1006c68a78ffSChuyi Zhou if (!pos)
1007c68a78ffSChuyi Zhou return pos;
1008c68a78ffSChuyi Zhou
1009c68a78ffSChuyi Zhou if (flags == BPF_TASK_ITER_ALL_PROCS)
1010c68a78ffSChuyi Zhou goto get_next_task;
1011c68a78ffSChuyi Zhou
1012c68a78ffSChuyi Zhou kit->pos = __next_thread(kit->pos);
1013c68a78ffSChuyi Zhou if (kit->pos || flags == BPF_TASK_ITER_PROC_THREADS)
1014c68a78ffSChuyi Zhou return pos;
1015c68a78ffSChuyi Zhou
10165a34f9daSOleg Nesterov get_next_task:
1017ac8148d9SOleg Nesterov kit->task = next_task(kit->task);
1018c68a78ffSChuyi Zhou if (kit->task == &init_task)
1019c68a78ffSChuyi Zhou kit->pos = NULL;
1020c68a78ffSChuyi Zhou else
1021ac8148d9SOleg Nesterov kit->pos = kit->task;
1022ac8148d9SOleg Nesterov
1023c68a78ffSChuyi Zhou return pos;
1024ac8148d9SOleg Nesterov }
1025ac8148d9SOleg Nesterov
bpf_iter_task_destroy(struct bpf_iter_task * it)1026c68a78ffSChuyi Zhou __bpf_kfunc void bpf_iter_task_destroy(struct bpf_iter_task *it)
1027c68a78ffSChuyi Zhou {
1028c68a78ffSChuyi Zhou }
1029c68a78ffSChuyi Zhou
1030c68a78ffSChuyi Zhou __bpf_kfunc_end_defs();
1031c68a78ffSChuyi Zhou
1032c68a78ffSChuyi Zhou DEFINE_PER_CPU(struct mmap_unlock_irq_work, mmap_unlock_work);
1033c68a78ffSChuyi Zhou
do_mmap_read_unlock(struct irq_work * entry)1034391145baSDave Marchevsky static void do_mmap_read_unlock(struct irq_work *entry)
1035c68a78ffSChuyi Zhou {
10367c7e3d31SSong Liu struct mmap_unlock_irq_work *work;
10377c7e3d31SSong Liu
10387c7e3d31SSong Liu if (WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_RT)))
10397c7e3d31SSong Liu return;
10407c7e3d31SSong Liu
10417c7e3d31SSong Liu work = container_of(entry, struct mmap_unlock_irq_work, irq_work);
10427c7e3d31SSong Liu mmap_read_unlock_non_owner(work->mm);
10437c7e3d31SSong Liu }
10447c7e3d31SSong Liu
task_iter_init(void)10457c7e3d31SSong Liu static int __init task_iter_init(void)
10467c7e3d31SSong Liu {
10477c7e3d31SSong Liu struct mmap_unlock_irq_work *work;
10487c7e3d31SSong Liu int ret, cpu;
104915172a46SYonghong Song
105015172a46SYonghong Song for_each_possible_cpu(cpu) {
10517c7e3d31SSong Liu work = per_cpu_ptr(&mmap_unlock_work, cpu);
10527c7e3d31SSong Liu init_irq_work(&work->irq_work, do_mmap_read_unlock);
10537c7e3d31SSong Liu }
10547c7e3d31SSong Liu
10557c7e3d31SSong Liu task_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
10567c7e3d31SSong Liu ret = bpf_iter_reg_target(&task_reg_info);
10577c7e3d31SSong Liu if (ret)
1058eaaacd23SYonghong Song return ret;
1059d19ddb47SSong Liu
1060eaaacd23SYonghong Song task_file_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
1061eaaacd23SYonghong Song task_file_reg_info.ctx_arg_info[1].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_FILE];
1062eaaacd23SYonghong Song ret = bpf_iter_reg_target(&task_file_reg_info);
1063eaaacd23SYonghong Song if (ret)
1064d19ddb47SSong Liu return ret;
1065d19ddb47SSong Liu
10663a7b35b8SSong Liu task_vma_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
10673a7b35b8SSong Liu task_vma_reg_info.ctx_arg_info[1].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_VMA];
10683a7b35b8SSong Liu return bpf_iter_reg_target(&task_vma_reg_info);
10693a7b35b8SSong Liu }
1070d19ddb47SSong Liu late_initcall(task_iter_init);
1071d19ddb47SSong Liu