xref: /linux/kernel/bpf/task_iter.c (revision a4ff64edf9edc8f05e2183610dc8306d3279c6ac)
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/bpf_mem_alloc.h>
11 #include <linux/btf_ids.h>
12 #include <linux/mm_types.h>
13 #include "mmap_unlock_work.h"
14 
15 static const char * const iter_task_type_names[] = {
16 	"ALL",
17 	"TID",
18 	"PID",
19 };
20 
21 struct bpf_iter_seq_task_common {
22 	struct pid_namespace *ns;
23 	enum bpf_iter_task_type	type;
24 	u32 pid;
25 	u32 pid_visiting;
26 };
27 
28 struct bpf_iter_seq_task_info {
29 	/* The first field must be struct bpf_iter_seq_task_common.
30 	 * this is assumed by {init, fini}_seq_pidns() callback functions.
31 	 */
32 	struct bpf_iter_seq_task_common common;
33 	u32 tid;
34 };
35 
36 static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_common *common,
37 						   u32 *tid,
38 						   bool skip_if_dup_files)
39 {
40 	struct task_struct *task;
41 	struct pid *pid;
42 	u32 next_tid;
43 
44 	if (!*tid) {
45 		/* The first time, the iterator calls this function. */
46 		pid = find_pid_ns(common->pid, common->ns);
47 		task = get_pid_task(pid, PIDTYPE_TGID);
48 		if (!task)
49 			return NULL;
50 
51 		*tid = common->pid;
52 		common->pid_visiting = common->pid;
53 
54 		return task;
55 	}
56 
57 	/* If the control returns to user space and comes back to the
58 	 * kernel again, *tid and common->pid_visiting should be the
59 	 * same for task_seq_start() to pick up the correct task.
60 	 */
61 	if (*tid == common->pid_visiting) {
62 		pid = find_pid_ns(common->pid_visiting, common->ns);
63 		task = get_pid_task(pid, PIDTYPE_PID);
64 
65 		return task;
66 	}
67 
68 	task = find_task_by_pid_ns(common->pid_visiting, common->ns);
69 	if (!task)
70 		return NULL;
71 
72 retry:
73 	task = next_thread(task);
74 
75 	next_tid = __task_pid_nr_ns(task, PIDTYPE_PID, common->ns);
76 	if (!next_tid || next_tid == common->pid) {
77 		/* Run out of tasks of a process.  The tasks of a
78 		 * thread_group are linked as circular linked list.
79 		 */
80 		return NULL;
81 	}
82 
83 	if (skip_if_dup_files && task->files == task->group_leader->files)
84 		goto retry;
85 
86 	*tid = common->pid_visiting = next_tid;
87 	get_task_struct(task);
88 	return task;
89 }
90 
91 static struct task_struct *task_seq_get_next(struct bpf_iter_seq_task_common *common,
92 					     u32 *tid,
93 					     bool skip_if_dup_files)
94 {
95 	struct task_struct *task = NULL;
96 	struct pid *pid;
97 
98 	if (common->type == BPF_TASK_ITER_TID) {
99 		if (*tid && *tid != common->pid)
100 			return NULL;
101 		rcu_read_lock();
102 		pid = find_pid_ns(common->pid, common->ns);
103 		if (pid) {
104 			task = get_pid_task(pid, PIDTYPE_TGID);
105 			*tid = common->pid;
106 		}
107 		rcu_read_unlock();
108 
109 		return task;
110 	}
111 
112 	if (common->type == BPF_TASK_ITER_TGID) {
113 		rcu_read_lock();
114 		task = task_group_seq_get_next(common, tid, skip_if_dup_files);
115 		rcu_read_unlock();
116 
117 		return task;
118 	}
119 
120 	rcu_read_lock();
121 retry:
122 	pid = find_ge_pid(*tid, common->ns);
123 	if (pid) {
124 		*tid = pid_nr_ns(pid, common->ns);
125 		task = get_pid_task(pid, PIDTYPE_PID);
126 		if (!task) {
127 			++*tid;
128 			goto retry;
129 		} else if (skip_if_dup_files && !thread_group_leader(task) &&
130 			   task->files == task->group_leader->files) {
131 			put_task_struct(task);
132 			task = NULL;
133 			++*tid;
134 			goto retry;
135 		}
136 	}
137 	rcu_read_unlock();
138 
139 	return task;
140 }
141 
142 static void *task_seq_start(struct seq_file *seq, loff_t *pos)
143 {
144 	struct bpf_iter_seq_task_info *info = seq->private;
145 	struct task_struct *task;
146 
147 	task = task_seq_get_next(&info->common, &info->tid, false);
148 	if (!task)
149 		return NULL;
150 
151 	if (*pos == 0)
152 		++*pos;
153 	return task;
154 }
155 
156 static void *task_seq_next(struct seq_file *seq, void *v, loff_t *pos)
157 {
158 	struct bpf_iter_seq_task_info *info = seq->private;
159 	struct task_struct *task;
160 
161 	++*pos;
162 	++info->tid;
163 	put_task_struct((struct task_struct *)v);
164 	task = task_seq_get_next(&info->common, &info->tid, false);
165 	if (!task)
166 		return NULL;
167 
168 	return task;
169 }
170 
171 struct bpf_iter__task {
172 	__bpf_md_ptr(struct bpf_iter_meta *, meta);
173 	__bpf_md_ptr(struct task_struct *, task);
174 };
175 
176 DEFINE_BPF_ITER_FUNC(task, struct bpf_iter_meta *meta, struct task_struct *task)
177 
178 static int __task_seq_show(struct seq_file *seq, struct task_struct *task,
179 			   bool in_stop)
180 {
181 	struct bpf_iter_meta meta;
182 	struct bpf_iter__task ctx;
183 	struct bpf_prog *prog;
184 
185 	meta.seq = seq;
186 	prog = bpf_iter_get_info(&meta, in_stop);
187 	if (!prog)
188 		return 0;
189 
190 	ctx.meta = &meta;
191 	ctx.task = task;
192 	return bpf_iter_run_prog(prog, &ctx);
193 }
194 
195 static int task_seq_show(struct seq_file *seq, void *v)
196 {
197 	return __task_seq_show(seq, v, false);
198 }
199 
200 static void task_seq_stop(struct seq_file *seq, void *v)
201 {
202 	if (!v)
203 		(void)__task_seq_show(seq, v, true);
204 	else
205 		put_task_struct((struct task_struct *)v);
206 }
207 
208 static int bpf_iter_attach_task(struct bpf_prog *prog,
209 				union bpf_iter_link_info *linfo,
210 				struct bpf_iter_aux_info *aux)
211 {
212 	unsigned int flags;
213 	struct pid *pid;
214 	pid_t tgid;
215 
216 	if ((!!linfo->task.tid + !!linfo->task.pid + !!linfo->task.pid_fd) > 1)
217 		return -EINVAL;
218 
219 	aux->task.type = BPF_TASK_ITER_ALL;
220 	if (linfo->task.tid != 0) {
221 		aux->task.type = BPF_TASK_ITER_TID;
222 		aux->task.pid = linfo->task.tid;
223 	}
224 	if (linfo->task.pid != 0) {
225 		aux->task.type = BPF_TASK_ITER_TGID;
226 		aux->task.pid = linfo->task.pid;
227 	}
228 	if (linfo->task.pid_fd != 0) {
229 		aux->task.type = BPF_TASK_ITER_TGID;
230 
231 		pid = pidfd_get_pid(linfo->task.pid_fd, &flags);
232 		if (IS_ERR(pid))
233 			return PTR_ERR(pid);
234 
235 		tgid = pid_nr_ns(pid, task_active_pid_ns(current));
236 		aux->task.pid = tgid;
237 		put_pid(pid);
238 	}
239 
240 	return 0;
241 }
242 
243 static const struct seq_operations task_seq_ops = {
244 	.start	= task_seq_start,
245 	.next	= task_seq_next,
246 	.stop	= task_seq_stop,
247 	.show	= task_seq_show,
248 };
249 
250 struct bpf_iter_seq_task_file_info {
251 	/* The first field must be struct bpf_iter_seq_task_common.
252 	 * this is assumed by {init, fini}_seq_pidns() callback functions.
253 	 */
254 	struct bpf_iter_seq_task_common common;
255 	struct task_struct *task;
256 	u32 tid;
257 	u32 fd;
258 };
259 
260 static struct file *
261 task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info)
262 {
263 	u32 saved_tid = info->tid;
264 	struct task_struct *curr_task;
265 	unsigned int curr_fd = info->fd;
266 
267 	/* If this function returns a non-NULL file object,
268 	 * it held a reference to the task/file.
269 	 * Otherwise, it does not hold any reference.
270 	 */
271 again:
272 	if (info->task) {
273 		curr_task = info->task;
274 		curr_fd = info->fd;
275 	} else {
276 		curr_task = task_seq_get_next(&info->common, &info->tid, true);
277                 if (!curr_task) {
278                         info->task = NULL;
279                         return NULL;
280                 }
281 
282 		/* set info->task */
283 		info->task = curr_task;
284 		if (saved_tid == info->tid)
285 			curr_fd = info->fd;
286 		else
287 			curr_fd = 0;
288 	}
289 
290 	rcu_read_lock();
291 	for (;; curr_fd++) {
292 		struct file *f;
293 		f = task_lookup_next_fdget_rcu(curr_task, &curr_fd);
294 		if (!f)
295 			break;
296 
297 		/* set info->fd */
298 		info->fd = curr_fd;
299 		rcu_read_unlock();
300 		return f;
301 	}
302 
303 	/* the current task is done, go to the next task */
304 	rcu_read_unlock();
305 	put_task_struct(curr_task);
306 
307 	if (info->common.type == BPF_TASK_ITER_TID) {
308 		info->task = NULL;
309 		return NULL;
310 	}
311 
312 	info->task = NULL;
313 	info->fd = 0;
314 	saved_tid = ++(info->tid);
315 	goto again;
316 }
317 
318 static void *task_file_seq_start(struct seq_file *seq, loff_t *pos)
319 {
320 	struct bpf_iter_seq_task_file_info *info = seq->private;
321 	struct file *file;
322 
323 	info->task = NULL;
324 	file = task_file_seq_get_next(info);
325 	if (file && *pos == 0)
326 		++*pos;
327 
328 	return file;
329 }
330 
331 static void *task_file_seq_next(struct seq_file *seq, void *v, loff_t *pos)
332 {
333 	struct bpf_iter_seq_task_file_info *info = seq->private;
334 
335 	++*pos;
336 	++info->fd;
337 	fput((struct file *)v);
338 	return task_file_seq_get_next(info);
339 }
340 
341 struct bpf_iter__task_file {
342 	__bpf_md_ptr(struct bpf_iter_meta *, meta);
343 	__bpf_md_ptr(struct task_struct *, task);
344 	u32 fd __aligned(8);
345 	__bpf_md_ptr(struct file *, file);
346 };
347 
348 DEFINE_BPF_ITER_FUNC(task_file, struct bpf_iter_meta *meta,
349 		     struct task_struct *task, u32 fd,
350 		     struct file *file)
351 
352 static int __task_file_seq_show(struct seq_file *seq, struct file *file,
353 				bool in_stop)
354 {
355 	struct bpf_iter_seq_task_file_info *info = seq->private;
356 	struct bpf_iter__task_file ctx;
357 	struct bpf_iter_meta meta;
358 	struct bpf_prog *prog;
359 
360 	meta.seq = seq;
361 	prog = bpf_iter_get_info(&meta, in_stop);
362 	if (!prog)
363 		return 0;
364 
365 	ctx.meta = &meta;
366 	ctx.task = info->task;
367 	ctx.fd = info->fd;
368 	ctx.file = file;
369 	return bpf_iter_run_prog(prog, &ctx);
370 }
371 
372 static int task_file_seq_show(struct seq_file *seq, void *v)
373 {
374 	return __task_file_seq_show(seq, v, false);
375 }
376 
377 static void task_file_seq_stop(struct seq_file *seq, void *v)
378 {
379 	struct bpf_iter_seq_task_file_info *info = seq->private;
380 
381 	if (!v) {
382 		(void)__task_file_seq_show(seq, v, true);
383 	} else {
384 		fput((struct file *)v);
385 		put_task_struct(info->task);
386 		info->task = NULL;
387 	}
388 }
389 
390 static int init_seq_pidns(void *priv_data, struct bpf_iter_aux_info *aux)
391 {
392 	struct bpf_iter_seq_task_common *common = priv_data;
393 
394 	common->ns = get_pid_ns(task_active_pid_ns(current));
395 	common->type = aux->task.type;
396 	common->pid = aux->task.pid;
397 
398 	return 0;
399 }
400 
401 static void fini_seq_pidns(void *priv_data)
402 {
403 	struct bpf_iter_seq_task_common *common = priv_data;
404 
405 	put_pid_ns(common->ns);
406 }
407 
408 static const struct seq_operations task_file_seq_ops = {
409 	.start	= task_file_seq_start,
410 	.next	= task_file_seq_next,
411 	.stop	= task_file_seq_stop,
412 	.show	= task_file_seq_show,
413 };
414 
415 struct bpf_iter_seq_task_vma_info {
416 	/* The first field must be struct bpf_iter_seq_task_common.
417 	 * this is assumed by {init, fini}_seq_pidns() callback functions.
418 	 */
419 	struct bpf_iter_seq_task_common common;
420 	struct task_struct *task;
421 	struct mm_struct *mm;
422 	struct vm_area_struct *vma;
423 	u32 tid;
424 	unsigned long prev_vm_start;
425 	unsigned long prev_vm_end;
426 };
427 
428 enum bpf_task_vma_iter_find_op {
429 	task_vma_iter_first_vma,   /* use find_vma() with addr 0 */
430 	task_vma_iter_next_vma,    /* use vma_next() with curr_vma */
431 	task_vma_iter_find_vma,    /* use find_vma() to find next vma */
432 };
433 
434 static struct vm_area_struct *
435 task_vma_seq_get_next(struct bpf_iter_seq_task_vma_info *info)
436 {
437 	enum bpf_task_vma_iter_find_op op;
438 	struct vm_area_struct *curr_vma;
439 	struct task_struct *curr_task;
440 	struct mm_struct *curr_mm;
441 	u32 saved_tid = info->tid;
442 
443 	/* If this function returns a non-NULL vma, it holds a reference to
444 	 * the task_struct, holds a refcount on mm->mm_users, and holds
445 	 * read lock on vma->mm->mmap_lock.
446 	 * If this function returns NULL, it does not hold any reference or
447 	 * lock.
448 	 */
449 	if (info->task) {
450 		curr_task = info->task;
451 		curr_vma = info->vma;
452 		curr_mm = info->mm;
453 		/* In case of lock contention, drop mmap_lock to unblock
454 		 * the writer.
455 		 *
456 		 * After relock, call find(mm, prev_vm_end - 1) to find
457 		 * new vma to process.
458 		 *
459 		 *   +------+------+-----------+
460 		 *   | VMA1 | VMA2 | VMA3      |
461 		 *   +------+------+-----------+
462 		 *   |      |      |           |
463 		 *  4k     8k     16k         400k
464 		 *
465 		 * For example, curr_vma == VMA2. Before unlock, we set
466 		 *
467 		 *    prev_vm_start = 8k
468 		 *    prev_vm_end   = 16k
469 		 *
470 		 * There are a few cases:
471 		 *
472 		 * 1) VMA2 is freed, but VMA3 exists.
473 		 *
474 		 *    find_vma() will return VMA3, just process VMA3.
475 		 *
476 		 * 2) VMA2 still exists.
477 		 *
478 		 *    find_vma() will return VMA2, process VMA2->next.
479 		 *
480 		 * 3) no more vma in this mm.
481 		 *
482 		 *    Process the next task.
483 		 *
484 		 * 4) find_vma() returns a different vma, VMA2'.
485 		 *
486 		 *    4.1) If VMA2 covers same range as VMA2', skip VMA2',
487 		 *         because we already covered the range;
488 		 *    4.2) VMA2 and VMA2' covers different ranges, process
489 		 *         VMA2'.
490 		 */
491 		if (mmap_lock_is_contended(curr_mm)) {
492 			info->prev_vm_start = curr_vma->vm_start;
493 			info->prev_vm_end = curr_vma->vm_end;
494 			op = task_vma_iter_find_vma;
495 			mmap_read_unlock(curr_mm);
496 			if (mmap_read_lock_killable(curr_mm)) {
497 				mmput(curr_mm);
498 				goto finish;
499 			}
500 		} else {
501 			op = task_vma_iter_next_vma;
502 		}
503 	} else {
504 again:
505 		curr_task = task_seq_get_next(&info->common, &info->tid, true);
506 		if (!curr_task) {
507 			info->tid++;
508 			goto finish;
509 		}
510 
511 		if (saved_tid != info->tid) {
512 			/* new task, process the first vma */
513 			op = task_vma_iter_first_vma;
514 		} else {
515 			/* Found the same tid, which means the user space
516 			 * finished data in previous buffer and read more.
517 			 * We dropped mmap_lock before returning to user
518 			 * space, so it is necessary to use find_vma() to
519 			 * find the next vma to process.
520 			 */
521 			op = task_vma_iter_find_vma;
522 		}
523 
524 		curr_mm = get_task_mm(curr_task);
525 		if (!curr_mm)
526 			goto next_task;
527 
528 		if (mmap_read_lock_killable(curr_mm)) {
529 			mmput(curr_mm);
530 			goto finish;
531 		}
532 	}
533 
534 	switch (op) {
535 	case task_vma_iter_first_vma:
536 		curr_vma = find_vma(curr_mm, 0);
537 		break;
538 	case task_vma_iter_next_vma:
539 		curr_vma = find_vma(curr_mm, curr_vma->vm_end);
540 		break;
541 	case task_vma_iter_find_vma:
542 		/* We dropped mmap_lock so it is necessary to use find_vma
543 		 * to find the next vma. This is similar to the  mechanism
544 		 * in show_smaps_rollup().
545 		 */
546 		curr_vma = find_vma(curr_mm, info->prev_vm_end - 1);
547 		/* case 1) and 4.2) above just use curr_vma */
548 
549 		/* check for case 2) or case 4.1) above */
550 		if (curr_vma &&
551 		    curr_vma->vm_start == info->prev_vm_start &&
552 		    curr_vma->vm_end == info->prev_vm_end)
553 			curr_vma = find_vma(curr_mm, curr_vma->vm_end);
554 		break;
555 	}
556 	if (!curr_vma) {
557 		/* case 3) above, or case 2) 4.1) with vma->next == NULL */
558 		mmap_read_unlock(curr_mm);
559 		mmput(curr_mm);
560 		goto next_task;
561 	}
562 	info->task = curr_task;
563 	info->vma = curr_vma;
564 	info->mm = curr_mm;
565 	return curr_vma;
566 
567 next_task:
568 	if (info->common.type == BPF_TASK_ITER_TID)
569 		goto finish;
570 
571 	put_task_struct(curr_task);
572 	info->task = NULL;
573 	info->mm = NULL;
574 	info->tid++;
575 	goto again;
576 
577 finish:
578 	if (curr_task)
579 		put_task_struct(curr_task);
580 	info->task = NULL;
581 	info->vma = NULL;
582 	info->mm = NULL;
583 	return NULL;
584 }
585 
586 static void *task_vma_seq_start(struct seq_file *seq, loff_t *pos)
587 {
588 	struct bpf_iter_seq_task_vma_info *info = seq->private;
589 	struct vm_area_struct *vma;
590 
591 	vma = task_vma_seq_get_next(info);
592 	if (vma && *pos == 0)
593 		++*pos;
594 
595 	return vma;
596 }
597 
598 static void *task_vma_seq_next(struct seq_file *seq, void *v, loff_t *pos)
599 {
600 	struct bpf_iter_seq_task_vma_info *info = seq->private;
601 
602 	++*pos;
603 	return task_vma_seq_get_next(info);
604 }
605 
606 struct bpf_iter__task_vma {
607 	__bpf_md_ptr(struct bpf_iter_meta *, meta);
608 	__bpf_md_ptr(struct task_struct *, task);
609 	__bpf_md_ptr(struct vm_area_struct *, vma);
610 };
611 
612 DEFINE_BPF_ITER_FUNC(task_vma, struct bpf_iter_meta *meta,
613 		     struct task_struct *task, struct vm_area_struct *vma)
614 
615 static int __task_vma_seq_show(struct seq_file *seq, bool in_stop)
616 {
617 	struct bpf_iter_seq_task_vma_info *info = seq->private;
618 	struct bpf_iter__task_vma ctx;
619 	struct bpf_iter_meta meta;
620 	struct bpf_prog *prog;
621 
622 	meta.seq = seq;
623 	prog = bpf_iter_get_info(&meta, in_stop);
624 	if (!prog)
625 		return 0;
626 
627 	ctx.meta = &meta;
628 	ctx.task = info->task;
629 	ctx.vma = info->vma;
630 	return bpf_iter_run_prog(prog, &ctx);
631 }
632 
633 static int task_vma_seq_show(struct seq_file *seq, void *v)
634 {
635 	return __task_vma_seq_show(seq, false);
636 }
637 
638 static void task_vma_seq_stop(struct seq_file *seq, void *v)
639 {
640 	struct bpf_iter_seq_task_vma_info *info = seq->private;
641 
642 	if (!v) {
643 		(void)__task_vma_seq_show(seq, true);
644 	} else {
645 		/* info->vma has not been seen by the BPF program. If the
646 		 * user space reads more, task_vma_seq_get_next should
647 		 * return this vma again. Set prev_vm_start to ~0UL,
648 		 * so that we don't skip the vma returned by the next
649 		 * find_vma() (case task_vma_iter_find_vma in
650 		 * task_vma_seq_get_next()).
651 		 */
652 		info->prev_vm_start = ~0UL;
653 		info->prev_vm_end = info->vma->vm_end;
654 		mmap_read_unlock(info->mm);
655 		mmput(info->mm);
656 		info->mm = NULL;
657 		put_task_struct(info->task);
658 		info->task = NULL;
659 	}
660 }
661 
662 static const struct seq_operations task_vma_seq_ops = {
663 	.start	= task_vma_seq_start,
664 	.next	= task_vma_seq_next,
665 	.stop	= task_vma_seq_stop,
666 	.show	= task_vma_seq_show,
667 };
668 
669 static const struct bpf_iter_seq_info task_seq_info = {
670 	.seq_ops		= &task_seq_ops,
671 	.init_seq_private	= init_seq_pidns,
672 	.fini_seq_private	= fini_seq_pidns,
673 	.seq_priv_size		= sizeof(struct bpf_iter_seq_task_info),
674 };
675 
676 static int bpf_iter_fill_link_info(const struct bpf_iter_aux_info *aux, struct bpf_link_info *info)
677 {
678 	switch (aux->task.type) {
679 	case BPF_TASK_ITER_TID:
680 		info->iter.task.tid = aux->task.pid;
681 		break;
682 	case BPF_TASK_ITER_TGID:
683 		info->iter.task.pid = aux->task.pid;
684 		break;
685 	default:
686 		break;
687 	}
688 	return 0;
689 }
690 
691 static void bpf_iter_task_show_fdinfo(const struct bpf_iter_aux_info *aux, struct seq_file *seq)
692 {
693 	seq_printf(seq, "task_type:\t%s\n", iter_task_type_names[aux->task.type]);
694 	if (aux->task.type == BPF_TASK_ITER_TID)
695 		seq_printf(seq, "tid:\t%u\n", aux->task.pid);
696 	else if (aux->task.type == BPF_TASK_ITER_TGID)
697 		seq_printf(seq, "pid:\t%u\n", aux->task.pid);
698 }
699 
700 static struct bpf_iter_reg task_reg_info = {
701 	.target			= "task",
702 	.attach_target		= bpf_iter_attach_task,
703 	.feature		= BPF_ITER_RESCHED,
704 	.ctx_arg_info_size	= 1,
705 	.ctx_arg_info		= {
706 		{ offsetof(struct bpf_iter__task, task),
707 		  PTR_TO_BTF_ID_OR_NULL | PTR_TRUSTED },
708 	},
709 	.seq_info		= &task_seq_info,
710 	.fill_link_info		= bpf_iter_fill_link_info,
711 	.show_fdinfo		= bpf_iter_task_show_fdinfo,
712 };
713 
714 static const struct bpf_iter_seq_info task_file_seq_info = {
715 	.seq_ops		= &task_file_seq_ops,
716 	.init_seq_private	= init_seq_pidns,
717 	.fini_seq_private	= fini_seq_pidns,
718 	.seq_priv_size		= sizeof(struct bpf_iter_seq_task_file_info),
719 };
720 
721 static struct bpf_iter_reg task_file_reg_info = {
722 	.target			= "task_file",
723 	.attach_target		= bpf_iter_attach_task,
724 	.feature		= BPF_ITER_RESCHED,
725 	.ctx_arg_info_size	= 2,
726 	.ctx_arg_info		= {
727 		{ offsetof(struct bpf_iter__task_file, task),
728 		  PTR_TO_BTF_ID_OR_NULL },
729 		{ offsetof(struct bpf_iter__task_file, file),
730 		  PTR_TO_BTF_ID_OR_NULL },
731 	},
732 	.seq_info		= &task_file_seq_info,
733 	.fill_link_info		= bpf_iter_fill_link_info,
734 	.show_fdinfo		= bpf_iter_task_show_fdinfo,
735 };
736 
737 static const struct bpf_iter_seq_info task_vma_seq_info = {
738 	.seq_ops		= &task_vma_seq_ops,
739 	.init_seq_private	= init_seq_pidns,
740 	.fini_seq_private	= fini_seq_pidns,
741 	.seq_priv_size		= sizeof(struct bpf_iter_seq_task_vma_info),
742 };
743 
744 static struct bpf_iter_reg task_vma_reg_info = {
745 	.target			= "task_vma",
746 	.attach_target		= bpf_iter_attach_task,
747 	.feature		= BPF_ITER_RESCHED,
748 	.ctx_arg_info_size	= 2,
749 	.ctx_arg_info		= {
750 		{ offsetof(struct bpf_iter__task_vma, task),
751 		  PTR_TO_BTF_ID_OR_NULL },
752 		{ offsetof(struct bpf_iter__task_vma, vma),
753 		  PTR_TO_BTF_ID_OR_NULL },
754 	},
755 	.seq_info		= &task_vma_seq_info,
756 	.fill_link_info		= bpf_iter_fill_link_info,
757 	.show_fdinfo		= bpf_iter_task_show_fdinfo,
758 };
759 
760 BPF_CALL_5(bpf_find_vma, struct task_struct *, task, u64, start,
761 	   bpf_callback_t, callback_fn, void *, callback_ctx, u64, flags)
762 {
763 	struct mmap_unlock_irq_work *work = NULL;
764 	struct vm_area_struct *vma;
765 	bool irq_work_busy = false;
766 	struct mm_struct *mm;
767 	int ret = -ENOENT;
768 
769 	if (flags)
770 		return -EINVAL;
771 
772 	if (!task)
773 		return -ENOENT;
774 
775 	mm = task->mm;
776 	if (!mm)
777 		return -ENOENT;
778 
779 	irq_work_busy = bpf_mmap_unlock_get_irq_work(&work);
780 
781 	if (irq_work_busy || !mmap_read_trylock(mm))
782 		return -EBUSY;
783 
784 	vma = find_vma(mm, start);
785 
786 	if (vma && vma->vm_start <= start && vma->vm_end > start) {
787 		callback_fn((u64)(long)task, (u64)(long)vma,
788 			    (u64)(long)callback_ctx, 0, 0);
789 		ret = 0;
790 	}
791 	bpf_mmap_unlock_mm(work, mm);
792 	return ret;
793 }
794 
795 const struct bpf_func_proto bpf_find_vma_proto = {
796 	.func		= bpf_find_vma,
797 	.ret_type	= RET_INTEGER,
798 	.arg1_type	= ARG_PTR_TO_BTF_ID,
799 	.arg1_btf_id	= &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
800 	.arg2_type	= ARG_ANYTHING,
801 	.arg3_type	= ARG_PTR_TO_FUNC,
802 	.arg4_type	= ARG_PTR_TO_STACK_OR_NULL,
803 	.arg5_type	= ARG_ANYTHING,
804 };
805 
806 struct bpf_iter_task_vma_kern_data {
807 	struct task_struct *task;
808 	struct mm_struct *mm;
809 	struct mmap_unlock_irq_work *work;
810 	struct vma_iterator vmi;
811 };
812 
813 struct bpf_iter_task_vma {
814 	/* opaque iterator state; having __u64 here allows to preserve correct
815 	 * alignment requirements in vmlinux.h, generated from BTF
816 	 */
817 	__u64 __opaque[1];
818 } __attribute__((aligned(8)));
819 
820 /* Non-opaque version of bpf_iter_task_vma */
821 struct bpf_iter_task_vma_kern {
822 	struct bpf_iter_task_vma_kern_data *data;
823 } __attribute__((aligned(8)));
824 
825 __bpf_kfunc_start_defs();
826 
827 __bpf_kfunc int bpf_iter_task_vma_new(struct bpf_iter_task_vma *it,
828 				      struct task_struct *task, u64 addr)
829 {
830 	struct bpf_iter_task_vma_kern *kit = (void *)it;
831 	bool irq_work_busy = false;
832 	int err;
833 
834 	BUILD_BUG_ON(sizeof(struct bpf_iter_task_vma_kern) != sizeof(struct bpf_iter_task_vma));
835 	BUILD_BUG_ON(__alignof__(struct bpf_iter_task_vma_kern) != __alignof__(struct bpf_iter_task_vma));
836 
837 	/* is_iter_reg_valid_uninit guarantees that kit hasn't been initialized
838 	 * before, so non-NULL kit->data doesn't point to previously
839 	 * bpf_mem_alloc'd bpf_iter_task_vma_kern_data
840 	 */
841 	kit->data = bpf_mem_alloc(&bpf_global_ma, sizeof(struct bpf_iter_task_vma_kern_data));
842 	if (!kit->data)
843 		return -ENOMEM;
844 
845 	kit->data->task = get_task_struct(task);
846 	kit->data->mm = task->mm;
847 	if (!kit->data->mm) {
848 		err = -ENOENT;
849 		goto err_cleanup_iter;
850 	}
851 
852 	/* kit->data->work == NULL is valid after bpf_mmap_unlock_get_irq_work */
853 	irq_work_busy = bpf_mmap_unlock_get_irq_work(&kit->data->work);
854 	if (irq_work_busy || !mmap_read_trylock(kit->data->mm)) {
855 		err = -EBUSY;
856 		goto err_cleanup_iter;
857 	}
858 
859 	vma_iter_init(&kit->data->vmi, kit->data->mm, addr);
860 	return 0;
861 
862 err_cleanup_iter:
863 	if (kit->data->task)
864 		put_task_struct(kit->data->task);
865 	bpf_mem_free(&bpf_global_ma, kit->data);
866 	/* NULL kit->data signals failed bpf_iter_task_vma initialization */
867 	kit->data = NULL;
868 	return err;
869 }
870 
871 __bpf_kfunc struct vm_area_struct *bpf_iter_task_vma_next(struct bpf_iter_task_vma *it)
872 {
873 	struct bpf_iter_task_vma_kern *kit = (void *)it;
874 
875 	if (!kit->data) /* bpf_iter_task_vma_new failed */
876 		return NULL;
877 	return vma_next(&kit->data->vmi);
878 }
879 
880 __bpf_kfunc void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it)
881 {
882 	struct bpf_iter_task_vma_kern *kit = (void *)it;
883 
884 	if (kit->data) {
885 		bpf_mmap_unlock_mm(kit->data->work, kit->data->mm);
886 		put_task_struct(kit->data->task);
887 		bpf_mem_free(&bpf_global_ma, kit->data);
888 	}
889 }
890 
891 __bpf_kfunc_end_defs();
892 
893 #ifdef CONFIG_CGROUPS
894 
895 struct bpf_iter_css_task {
896 	__u64 __opaque[1];
897 } __attribute__((aligned(8)));
898 
899 struct bpf_iter_css_task_kern {
900 	struct css_task_iter *css_it;
901 } __attribute__((aligned(8)));
902 
903 __bpf_kfunc_start_defs();
904 
905 __bpf_kfunc int bpf_iter_css_task_new(struct bpf_iter_css_task *it,
906 		struct cgroup_subsys_state *css, unsigned int flags)
907 {
908 	struct bpf_iter_css_task_kern *kit = (void *)it;
909 
910 	BUILD_BUG_ON(sizeof(struct bpf_iter_css_task_kern) != sizeof(struct bpf_iter_css_task));
911 	BUILD_BUG_ON(__alignof__(struct bpf_iter_css_task_kern) !=
912 					__alignof__(struct bpf_iter_css_task));
913 	kit->css_it = NULL;
914 	switch (flags) {
915 	case CSS_TASK_ITER_PROCS | CSS_TASK_ITER_THREADED:
916 	case CSS_TASK_ITER_PROCS:
917 	case 0:
918 		break;
919 	default:
920 		return -EINVAL;
921 	}
922 
923 	kit->css_it = bpf_mem_alloc(&bpf_global_ma, sizeof(struct css_task_iter));
924 	if (!kit->css_it)
925 		return -ENOMEM;
926 	css_task_iter_start(css, flags, kit->css_it);
927 	return 0;
928 }
929 
930 __bpf_kfunc struct task_struct *bpf_iter_css_task_next(struct bpf_iter_css_task *it)
931 {
932 	struct bpf_iter_css_task_kern *kit = (void *)it;
933 
934 	if (!kit->css_it)
935 		return NULL;
936 	return css_task_iter_next(kit->css_it);
937 }
938 
939 __bpf_kfunc void bpf_iter_css_task_destroy(struct bpf_iter_css_task *it)
940 {
941 	struct bpf_iter_css_task_kern *kit = (void *)it;
942 
943 	if (!kit->css_it)
944 		return;
945 	css_task_iter_end(kit->css_it);
946 	bpf_mem_free(&bpf_global_ma, kit->css_it);
947 }
948 
949 __bpf_kfunc_end_defs();
950 
951 #endif /* CONFIG_CGROUPS */
952 
953 struct bpf_iter_task {
954 	__u64 __opaque[3];
955 } __attribute__((aligned(8)));
956 
957 struct bpf_iter_task_kern {
958 	struct task_struct *task;
959 	struct task_struct *pos;
960 	unsigned int flags;
961 } __attribute__((aligned(8)));
962 
963 enum {
964 	/* all process in the system */
965 	BPF_TASK_ITER_ALL_PROCS,
966 	/* all threads in the system */
967 	BPF_TASK_ITER_ALL_THREADS,
968 	/* all threads of a specific process */
969 	BPF_TASK_ITER_PROC_THREADS
970 };
971 
972 __bpf_kfunc_start_defs();
973 
974 __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it,
975 		struct task_struct *task__nullable, unsigned int flags)
976 {
977 	struct bpf_iter_task_kern *kit = (void *)it;
978 
979 	BUILD_BUG_ON(sizeof(struct bpf_iter_task_kern) > sizeof(struct bpf_iter_task));
980 	BUILD_BUG_ON(__alignof__(struct bpf_iter_task_kern) !=
981 					__alignof__(struct bpf_iter_task));
982 
983 	kit->task = kit->pos = NULL;
984 	switch (flags) {
985 	case BPF_TASK_ITER_ALL_THREADS:
986 	case BPF_TASK_ITER_ALL_PROCS:
987 		break;
988 	case BPF_TASK_ITER_PROC_THREADS:
989 		if (!task__nullable)
990 			return -EINVAL;
991 		break;
992 	default:
993 		return -EINVAL;
994 	}
995 
996 	if (flags == BPF_TASK_ITER_PROC_THREADS)
997 		kit->task = task__nullable;
998 	else
999 		kit->task = &init_task;
1000 	kit->pos = kit->task;
1001 	kit->flags = flags;
1002 	return 0;
1003 }
1004 
1005 __bpf_kfunc struct task_struct *bpf_iter_task_next(struct bpf_iter_task *it)
1006 {
1007 	struct bpf_iter_task_kern *kit = (void *)it;
1008 	struct task_struct *pos;
1009 	unsigned int flags;
1010 
1011 	flags = kit->flags;
1012 	pos = kit->pos;
1013 
1014 	if (!pos)
1015 		return pos;
1016 
1017 	if (flags == BPF_TASK_ITER_ALL_PROCS)
1018 		goto get_next_task;
1019 
1020 	kit->pos = next_thread(kit->pos);
1021 	if (kit->pos == kit->task) {
1022 		if (flags == BPF_TASK_ITER_PROC_THREADS) {
1023 			kit->pos = NULL;
1024 			return pos;
1025 		}
1026 	} else
1027 		return pos;
1028 
1029 get_next_task:
1030 	kit->pos = next_task(kit->pos);
1031 	kit->task = kit->pos;
1032 	if (kit->pos == &init_task)
1033 		kit->pos = NULL;
1034 
1035 	return pos;
1036 }
1037 
1038 __bpf_kfunc void bpf_iter_task_destroy(struct bpf_iter_task *it)
1039 {
1040 }
1041 
1042 __bpf_kfunc_end_defs();
1043 
1044 DEFINE_PER_CPU(struct mmap_unlock_irq_work, mmap_unlock_work);
1045 
1046 static void do_mmap_read_unlock(struct irq_work *entry)
1047 {
1048 	struct mmap_unlock_irq_work *work;
1049 
1050 	if (WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_RT)))
1051 		return;
1052 
1053 	work = container_of(entry, struct mmap_unlock_irq_work, irq_work);
1054 	mmap_read_unlock_non_owner(work->mm);
1055 }
1056 
1057 static int __init task_iter_init(void)
1058 {
1059 	struct mmap_unlock_irq_work *work;
1060 	int ret, cpu;
1061 
1062 	for_each_possible_cpu(cpu) {
1063 		work = per_cpu_ptr(&mmap_unlock_work, cpu);
1064 		init_irq_work(&work->irq_work, do_mmap_read_unlock);
1065 	}
1066 
1067 	task_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
1068 	ret = bpf_iter_reg_target(&task_reg_info);
1069 	if (ret)
1070 		return ret;
1071 
1072 	task_file_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
1073 	task_file_reg_info.ctx_arg_info[1].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_FILE];
1074 	ret =  bpf_iter_reg_target(&task_file_reg_info);
1075 	if (ret)
1076 		return ret;
1077 
1078 	task_vma_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
1079 	task_vma_reg_info.ctx_arg_info[1].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_VMA];
1080 	return bpf_iter_reg_target(&task_vma_reg_info);
1081 }
1082 late_initcall(task_iter_init);
1083