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