xref: /openbmc/linux/kernel/bpf/task_iter.c (revision b92dd117)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2020 Facebook */
3 
4 #include <linux/init.h>
5 #include <linux/namei.h>
6 #include <linux/pid_namespace.h>
7 #include <linux/fs.h>
8 #include <linux/fdtable.h>
9 #include <linux/filter.h>
10 #include <linux/btf_ids.h>
11 #include "mmap_unlock_work.h"
12 
13 static const char * const iter_task_type_names[] = {
14 	"ALL",
15 	"TID",
16 	"PID",
17 };
18 
19 struct bpf_iter_seq_task_common {
20 	struct pid_namespace *ns;
21 	enum bpf_iter_task_type	type;
22 	u32 pid;
23 	u32 pid_visiting;
24 };
25 
26 struct bpf_iter_seq_task_info {
27 	/* The first field must be struct bpf_iter_seq_task_common.
28 	 * this is assumed by {init, fini}_seq_pidns() callback functions.
29 	 */
30 	struct bpf_iter_seq_task_common common;
31 	u32 tid;
32 };
33 
34 static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_common *common,
35 						   u32 *tid,
36 						   bool skip_if_dup_files)
37 {
38 	struct task_struct *task, *next_task;
39 	struct pid *pid;
40 	u32 saved_tid;
41 
42 	if (!*tid) {
43 		/* The first time, the iterator calls this function. */
44 		pid = find_pid_ns(common->pid, common->ns);
45 		if (!pid)
46 			return NULL;
47 
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 	pid = find_pid_ns(common->pid_visiting, common->ns);
70 	if (!pid)
71 		return NULL;
72 
73 	task = get_pid_task(pid, PIDTYPE_PID);
74 	if (!task)
75 		return NULL;
76 
77 retry:
78 	if (!pid_alive(task)) {
79 		put_task_struct(task);
80 		return NULL;
81 	}
82 
83 	next_task = next_thread(task);
84 	put_task_struct(task);
85 	if (!next_task)
86 		return NULL;
87 
88 	saved_tid = *tid;
89 	*tid = __task_pid_nr_ns(next_task, PIDTYPE_PID, common->ns);
90 	if (!*tid || *tid == common->pid) {
91 		/* Run out of tasks of a process.  The tasks of a
92 		 * thread_group are linked as circular linked list.
93 		 */
94 		*tid = saved_tid;
95 		return NULL;
96 	}
97 
98 	get_task_struct(next_task);
99 	common->pid_visiting = *tid;
100 
101 	if (skip_if_dup_files && task->files == task->group_leader->files) {
102 		task = next_task;
103 		goto retry;
104 	}
105 
106 	return next_task;
107 }
108 
109 static struct task_struct *task_seq_get_next(struct bpf_iter_seq_task_common *common,
110 					     u32 *tid,
111 					     bool skip_if_dup_files)
112 {
113 	struct task_struct *task = NULL;
114 	struct pid *pid;
115 
116 	if (common->type == BPF_TASK_ITER_TID) {
117 		if (*tid && *tid != common->pid)
118 			return NULL;
119 		rcu_read_lock();
120 		pid = find_pid_ns(common->pid, common->ns);
121 		if (pid) {
122 			task = get_pid_task(pid, PIDTYPE_TGID);
123 			*tid = common->pid;
124 		}
125 		rcu_read_unlock();
126 
127 		return task;
128 	}
129 
130 	if (common->type == BPF_TASK_ITER_TGID) {
131 		rcu_read_lock();
132 		task = task_group_seq_get_next(common, tid, skip_if_dup_files);
133 		rcu_read_unlock();
134 
135 		return task;
136 	}
137 
138 	rcu_read_lock();
139 retry:
140 	pid = find_ge_pid(*tid, common->ns);
141 	if (pid) {
142 		*tid = pid_nr_ns(pid, common->ns);
143 		task = get_pid_task(pid, PIDTYPE_PID);
144 		if (!task) {
145 			++*tid;
146 			goto retry;
147 		} else if (skip_if_dup_files && !thread_group_leader(task) &&
148 			   task->files == task->group_leader->files) {
149 			put_task_struct(task);
150 			task = NULL;
151 			++*tid;
152 			goto retry;
153 		}
154 	}
155 	rcu_read_unlock();
156 
157 	return task;
158 }
159 
160 static void *task_seq_start(struct seq_file *seq, loff_t *pos)
161 {
162 	struct bpf_iter_seq_task_info *info = seq->private;
163 	struct task_struct *task;
164 
165 	task = task_seq_get_next(&info->common, &info->tid, false);
166 	if (!task)
167 		return NULL;
168 
169 	if (*pos == 0)
170 		++*pos;
171 	return task;
172 }
173 
174 static void *task_seq_next(struct seq_file *seq, void *v, loff_t *pos)
175 {
176 	struct bpf_iter_seq_task_info *info = seq->private;
177 	struct task_struct *task;
178 
179 	++*pos;
180 	++info->tid;
181 	put_task_struct((struct task_struct *)v);
182 	task = task_seq_get_next(&info->common, &info->tid, false);
183 	if (!task)
184 		return NULL;
185 
186 	return task;
187 }
188 
189 struct bpf_iter__task {
190 	__bpf_md_ptr(struct bpf_iter_meta *, meta);
191 	__bpf_md_ptr(struct task_struct *, task);
192 };
193 
194 DEFINE_BPF_ITER_FUNC(task, struct bpf_iter_meta *meta, struct task_struct *task)
195 
196 static int __task_seq_show(struct seq_file *seq, struct task_struct *task,
197 			   bool in_stop)
198 {
199 	struct bpf_iter_meta meta;
200 	struct bpf_iter__task ctx;
201 	struct bpf_prog *prog;
202 
203 	meta.seq = seq;
204 	prog = bpf_iter_get_info(&meta, in_stop);
205 	if (!prog)
206 		return 0;
207 
208 	ctx.meta = &meta;
209 	ctx.task = task;
210 	return bpf_iter_run_prog(prog, &ctx);
211 }
212 
213 static int task_seq_show(struct seq_file *seq, void *v)
214 {
215 	return __task_seq_show(seq, v, false);
216 }
217 
218 static void task_seq_stop(struct seq_file *seq, void *v)
219 {
220 	if (!v)
221 		(void)__task_seq_show(seq, v, true);
222 	else
223 		put_task_struct((struct task_struct *)v);
224 }
225 
226 static int bpf_iter_attach_task(struct bpf_prog *prog,
227 				union bpf_iter_link_info *linfo,
228 				struct bpf_iter_aux_info *aux)
229 {
230 	unsigned int flags;
231 	struct pid *pid;
232 	pid_t tgid;
233 
234 	if ((!!linfo->task.tid + !!linfo->task.pid + !!linfo->task.pid_fd) > 1)
235 		return -EINVAL;
236 
237 	aux->task.type = BPF_TASK_ITER_ALL;
238 	if (linfo->task.tid != 0) {
239 		aux->task.type = BPF_TASK_ITER_TID;
240 		aux->task.pid = linfo->task.tid;
241 	}
242 	if (linfo->task.pid != 0) {
243 		aux->task.type = BPF_TASK_ITER_TGID;
244 		aux->task.pid = linfo->task.pid;
245 	}
246 	if (linfo->task.pid_fd != 0) {
247 		aux->task.type = BPF_TASK_ITER_TGID;
248 
249 		pid = pidfd_get_pid(linfo->task.pid_fd, &flags);
250 		if (IS_ERR(pid))
251 			return PTR_ERR(pid);
252 
253 		tgid = pid_nr_ns(pid, task_active_pid_ns(current));
254 		aux->task.pid = tgid;
255 		put_pid(pid);
256 	}
257 
258 	return 0;
259 }
260 
261 static const struct seq_operations task_seq_ops = {
262 	.start	= task_seq_start,
263 	.next	= task_seq_next,
264 	.stop	= task_seq_stop,
265 	.show	= task_seq_show,
266 };
267 
268 struct bpf_iter_seq_task_file_info {
269 	/* The first field must be struct bpf_iter_seq_task_common.
270 	 * this is assumed by {init, fini}_seq_pidns() callback functions.
271 	 */
272 	struct bpf_iter_seq_task_common common;
273 	struct task_struct *task;
274 	u32 tid;
275 	u32 fd;
276 };
277 
278 static struct file *
279 task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info)
280 {
281 	u32 saved_tid = info->tid;
282 	struct task_struct *curr_task;
283 	unsigned int curr_fd = info->fd;
284 
285 	/* If this function returns a non-NULL file object,
286 	 * it held a reference to the task/file.
287 	 * Otherwise, it does not hold any reference.
288 	 */
289 again:
290 	if (info->task) {
291 		curr_task = info->task;
292 		curr_fd = info->fd;
293 	} else {
294 		curr_task = task_seq_get_next(&info->common, &info->tid, true);
295                 if (!curr_task) {
296                         info->task = NULL;
297                         return NULL;
298                 }
299 
300 		/* set info->task */
301 		info->task = curr_task;
302 		if (saved_tid == info->tid)
303 			curr_fd = info->fd;
304 		else
305 			curr_fd = 0;
306 	}
307 
308 	rcu_read_lock();
309 	for (;; curr_fd++) {
310 		struct file *f;
311 		f = task_lookup_next_fd_rcu(curr_task, &curr_fd);
312 		if (!f)
313 			break;
314 		if (!get_file_rcu(f))
315 			continue;
316 
317 		/* set info->fd */
318 		info->fd = curr_fd;
319 		rcu_read_unlock();
320 		return f;
321 	}
322 
323 	/* the current task is done, go to the next task */
324 	rcu_read_unlock();
325 	put_task_struct(curr_task);
326 
327 	if (info->common.type == BPF_TASK_ITER_TID) {
328 		info->task = NULL;
329 		return NULL;
330 	}
331 
332 	info->task = NULL;
333 	info->fd = 0;
334 	saved_tid = ++(info->tid);
335 	goto again;
336 }
337 
338 static void *task_file_seq_start(struct seq_file *seq, loff_t *pos)
339 {
340 	struct bpf_iter_seq_task_file_info *info = seq->private;
341 	struct file *file;
342 
343 	info->task = NULL;
344 	file = task_file_seq_get_next(info);
345 	if (file && *pos == 0)
346 		++*pos;
347 
348 	return file;
349 }
350 
351 static void *task_file_seq_next(struct seq_file *seq, void *v, loff_t *pos)
352 {
353 	struct bpf_iter_seq_task_file_info *info = seq->private;
354 
355 	++*pos;
356 	++info->fd;
357 	fput((struct file *)v);
358 	return task_file_seq_get_next(info);
359 }
360 
361 struct bpf_iter__task_file {
362 	__bpf_md_ptr(struct bpf_iter_meta *, meta);
363 	__bpf_md_ptr(struct task_struct *, task);
364 	u32 fd __aligned(8);
365 	__bpf_md_ptr(struct file *, file);
366 };
367 
368 DEFINE_BPF_ITER_FUNC(task_file, struct bpf_iter_meta *meta,
369 		     struct task_struct *task, u32 fd,
370 		     struct file *file)
371 
372 static int __task_file_seq_show(struct seq_file *seq, struct file *file,
373 				bool in_stop)
374 {
375 	struct bpf_iter_seq_task_file_info *info = seq->private;
376 	struct bpf_iter__task_file ctx;
377 	struct bpf_iter_meta meta;
378 	struct bpf_prog *prog;
379 
380 	meta.seq = seq;
381 	prog = bpf_iter_get_info(&meta, in_stop);
382 	if (!prog)
383 		return 0;
384 
385 	ctx.meta = &meta;
386 	ctx.task = info->task;
387 	ctx.fd = info->fd;
388 	ctx.file = file;
389 	return bpf_iter_run_prog(prog, &ctx);
390 }
391 
392 static int task_file_seq_show(struct seq_file *seq, void *v)
393 {
394 	return __task_file_seq_show(seq, v, false);
395 }
396 
397 static void task_file_seq_stop(struct seq_file *seq, void *v)
398 {
399 	struct bpf_iter_seq_task_file_info *info = seq->private;
400 
401 	if (!v) {
402 		(void)__task_file_seq_show(seq, v, true);
403 	} else {
404 		fput((struct file *)v);
405 		put_task_struct(info->task);
406 		info->task = NULL;
407 	}
408 }
409 
410 static int init_seq_pidns(void *priv_data, struct bpf_iter_aux_info *aux)
411 {
412 	struct bpf_iter_seq_task_common *common = priv_data;
413 
414 	common->ns = get_pid_ns(task_active_pid_ns(current));
415 	common->type = aux->task.type;
416 	common->pid = aux->task.pid;
417 
418 	return 0;
419 }
420 
421 static void fini_seq_pidns(void *priv_data)
422 {
423 	struct bpf_iter_seq_task_common *common = priv_data;
424 
425 	put_pid_ns(common->ns);
426 }
427 
428 static const struct seq_operations task_file_seq_ops = {
429 	.start	= task_file_seq_start,
430 	.next	= task_file_seq_next,
431 	.stop	= task_file_seq_stop,
432 	.show	= task_file_seq_show,
433 };
434 
435 struct bpf_iter_seq_task_vma_info {
436 	/* The first field must be struct bpf_iter_seq_task_common.
437 	 * this is assumed by {init, fini}_seq_pidns() callback functions.
438 	 */
439 	struct bpf_iter_seq_task_common common;
440 	struct task_struct *task;
441 	struct vm_area_struct *vma;
442 	u32 tid;
443 	unsigned long prev_vm_start;
444 	unsigned long prev_vm_end;
445 };
446 
447 enum bpf_task_vma_iter_find_op {
448 	task_vma_iter_first_vma,   /* use find_vma() with addr 0 */
449 	task_vma_iter_next_vma,    /* use vma_next() with curr_vma */
450 	task_vma_iter_find_vma,    /* use find_vma() to find next vma */
451 };
452 
453 static struct vm_area_struct *
454 task_vma_seq_get_next(struct bpf_iter_seq_task_vma_info *info)
455 {
456 	enum bpf_task_vma_iter_find_op op;
457 	struct vm_area_struct *curr_vma;
458 	struct task_struct *curr_task;
459 	u32 saved_tid = info->tid;
460 
461 	/* If this function returns a non-NULL vma, it holds a reference to
462 	 * the task_struct, and holds read lock on vma->mm->mmap_lock.
463 	 * If this function returns NULL, it does not hold any reference or
464 	 * lock.
465 	 */
466 	if (info->task) {
467 		curr_task = info->task;
468 		curr_vma = info->vma;
469 		/* In case of lock contention, drop mmap_lock to unblock
470 		 * the writer.
471 		 *
472 		 * After relock, call find(mm, prev_vm_end - 1) to find
473 		 * new vma to process.
474 		 *
475 		 *   +------+------+-----------+
476 		 *   | VMA1 | VMA2 | VMA3      |
477 		 *   +------+------+-----------+
478 		 *   |      |      |           |
479 		 *  4k     8k     16k         400k
480 		 *
481 		 * For example, curr_vma == VMA2. Before unlock, we set
482 		 *
483 		 *    prev_vm_start = 8k
484 		 *    prev_vm_end   = 16k
485 		 *
486 		 * There are a few cases:
487 		 *
488 		 * 1) VMA2 is freed, but VMA3 exists.
489 		 *
490 		 *    find_vma() will return VMA3, just process VMA3.
491 		 *
492 		 * 2) VMA2 still exists.
493 		 *
494 		 *    find_vma() will return VMA2, process VMA2->next.
495 		 *
496 		 * 3) no more vma in this mm.
497 		 *
498 		 *    Process the next task.
499 		 *
500 		 * 4) find_vma() returns a different vma, VMA2'.
501 		 *
502 		 *    4.1) If VMA2 covers same range as VMA2', skip VMA2',
503 		 *         because we already covered the range;
504 		 *    4.2) VMA2 and VMA2' covers different ranges, process
505 		 *         VMA2'.
506 		 */
507 		if (mmap_lock_is_contended(curr_task->mm)) {
508 			info->prev_vm_start = curr_vma->vm_start;
509 			info->prev_vm_end = curr_vma->vm_end;
510 			op = task_vma_iter_find_vma;
511 			mmap_read_unlock(curr_task->mm);
512 			if (mmap_read_lock_killable(curr_task->mm))
513 				goto finish;
514 		} else {
515 			op = task_vma_iter_next_vma;
516 		}
517 	} else {
518 again:
519 		curr_task = task_seq_get_next(&info->common, &info->tid, true);
520 		if (!curr_task) {
521 			info->tid++;
522 			goto finish;
523 		}
524 
525 		if (saved_tid != info->tid) {
526 			/* new task, process the first vma */
527 			op = task_vma_iter_first_vma;
528 		} else {
529 			/* Found the same tid, which means the user space
530 			 * finished data in previous buffer and read more.
531 			 * We dropped mmap_lock before returning to user
532 			 * space, so it is necessary to use find_vma() to
533 			 * find the next vma to process.
534 			 */
535 			op = task_vma_iter_find_vma;
536 		}
537 
538 		if (!curr_task->mm)
539 			goto next_task;
540 
541 		if (mmap_read_lock_killable(curr_task->mm))
542 			goto finish;
543 	}
544 
545 	switch (op) {
546 	case task_vma_iter_first_vma:
547 		curr_vma = find_vma(curr_task->mm, 0);
548 		break;
549 	case task_vma_iter_next_vma:
550 		curr_vma = find_vma(curr_task->mm, curr_vma->vm_end);
551 		break;
552 	case task_vma_iter_find_vma:
553 		/* We dropped mmap_lock so it is necessary to use find_vma
554 		 * to find the next vma. This is similar to the  mechanism
555 		 * in show_smaps_rollup().
556 		 */
557 		curr_vma = find_vma(curr_task->mm, info->prev_vm_end - 1);
558 		/* case 1) and 4.2) above just use curr_vma */
559 
560 		/* check for case 2) or case 4.1) above */
561 		if (curr_vma &&
562 		    curr_vma->vm_start == info->prev_vm_start &&
563 		    curr_vma->vm_end == info->prev_vm_end)
564 			curr_vma = find_vma(curr_task->mm, curr_vma->vm_end);
565 		break;
566 	}
567 	if (!curr_vma) {
568 		/* case 3) above, or case 2) 4.1) with vma->next == NULL */
569 		mmap_read_unlock(curr_task->mm);
570 		goto next_task;
571 	}
572 	info->task = curr_task;
573 	info->vma = curr_vma;
574 	return curr_vma;
575 
576 next_task:
577 	if (info->common.type == BPF_TASK_ITER_TID)
578 		goto finish;
579 
580 	put_task_struct(curr_task);
581 	info->task = NULL;
582 	info->tid++;
583 	goto again;
584 
585 finish:
586 	if (curr_task)
587 		put_task_struct(curr_task);
588 	info->task = NULL;
589 	info->vma = NULL;
590 	return NULL;
591 }
592 
593 static void *task_vma_seq_start(struct seq_file *seq, loff_t *pos)
594 {
595 	struct bpf_iter_seq_task_vma_info *info = seq->private;
596 	struct vm_area_struct *vma;
597 
598 	vma = task_vma_seq_get_next(info);
599 	if (vma && *pos == 0)
600 		++*pos;
601 
602 	return vma;
603 }
604 
605 static void *task_vma_seq_next(struct seq_file *seq, void *v, loff_t *pos)
606 {
607 	struct bpf_iter_seq_task_vma_info *info = seq->private;
608 
609 	++*pos;
610 	return task_vma_seq_get_next(info);
611 }
612 
613 struct bpf_iter__task_vma {
614 	__bpf_md_ptr(struct bpf_iter_meta *, meta);
615 	__bpf_md_ptr(struct task_struct *, task);
616 	__bpf_md_ptr(struct vm_area_struct *, vma);
617 };
618 
619 DEFINE_BPF_ITER_FUNC(task_vma, struct bpf_iter_meta *meta,
620 		     struct task_struct *task, struct vm_area_struct *vma)
621 
622 static int __task_vma_seq_show(struct seq_file *seq, bool in_stop)
623 {
624 	struct bpf_iter_seq_task_vma_info *info = seq->private;
625 	struct bpf_iter__task_vma ctx;
626 	struct bpf_iter_meta meta;
627 	struct bpf_prog *prog;
628 
629 	meta.seq = seq;
630 	prog = bpf_iter_get_info(&meta, in_stop);
631 	if (!prog)
632 		return 0;
633 
634 	ctx.meta = &meta;
635 	ctx.task = info->task;
636 	ctx.vma = info->vma;
637 	return bpf_iter_run_prog(prog, &ctx);
638 }
639 
640 static int task_vma_seq_show(struct seq_file *seq, void *v)
641 {
642 	return __task_vma_seq_show(seq, false);
643 }
644 
645 static void task_vma_seq_stop(struct seq_file *seq, void *v)
646 {
647 	struct bpf_iter_seq_task_vma_info *info = seq->private;
648 
649 	if (!v) {
650 		(void)__task_vma_seq_show(seq, true);
651 	} else {
652 		/* info->vma has not been seen by the BPF program. If the
653 		 * user space reads more, task_vma_seq_get_next should
654 		 * return this vma again. Set prev_vm_start to ~0UL,
655 		 * so that we don't skip the vma returned by the next
656 		 * find_vma() (case task_vma_iter_find_vma in
657 		 * task_vma_seq_get_next()).
658 		 */
659 		info->prev_vm_start = ~0UL;
660 		info->prev_vm_end = info->vma->vm_end;
661 		mmap_read_unlock(info->task->mm);
662 		put_task_struct(info->task);
663 		info->task = NULL;
664 	}
665 }
666 
667 static const struct seq_operations task_vma_seq_ops = {
668 	.start	= task_vma_seq_start,
669 	.next	= task_vma_seq_next,
670 	.stop	= task_vma_seq_stop,
671 	.show	= task_vma_seq_show,
672 };
673 
674 static const struct bpf_iter_seq_info task_seq_info = {
675 	.seq_ops		= &task_seq_ops,
676 	.init_seq_private	= init_seq_pidns,
677 	.fini_seq_private	= fini_seq_pidns,
678 	.seq_priv_size		= sizeof(struct bpf_iter_seq_task_info),
679 };
680 
681 static int bpf_iter_fill_link_info(const struct bpf_iter_aux_info *aux, struct bpf_link_info *info)
682 {
683 	switch (aux->task.type) {
684 	case BPF_TASK_ITER_TID:
685 		info->iter.task.tid = aux->task.pid;
686 		break;
687 	case BPF_TASK_ITER_TGID:
688 		info->iter.task.pid = aux->task.pid;
689 		break;
690 	default:
691 		break;
692 	}
693 	return 0;
694 }
695 
696 static void bpf_iter_task_show_fdinfo(const struct bpf_iter_aux_info *aux, struct seq_file *seq)
697 {
698 	seq_printf(seq, "task_type:\t%s\n", iter_task_type_names[aux->task.type]);
699 	if (aux->task.type == BPF_TASK_ITER_TID)
700 		seq_printf(seq, "tid:\t%u\n", aux->task.pid);
701 	else if (aux->task.type == BPF_TASK_ITER_TGID)
702 		seq_printf(seq, "pid:\t%u\n", aux->task.pid);
703 }
704 
705 static struct bpf_iter_reg task_reg_info = {
706 	.target			= "task",
707 	.attach_target		= bpf_iter_attach_task,
708 	.feature		= BPF_ITER_RESCHED,
709 	.ctx_arg_info_size	= 1,
710 	.ctx_arg_info		= {
711 		{ offsetof(struct bpf_iter__task, task),
712 		  PTR_TO_BTF_ID_OR_NULL },
713 	},
714 	.seq_info		= &task_seq_info,
715 	.fill_link_info		= bpf_iter_fill_link_info,
716 	.show_fdinfo		= bpf_iter_task_show_fdinfo,
717 };
718 
719 static const struct bpf_iter_seq_info task_file_seq_info = {
720 	.seq_ops		= &task_file_seq_ops,
721 	.init_seq_private	= init_seq_pidns,
722 	.fini_seq_private	= fini_seq_pidns,
723 	.seq_priv_size		= sizeof(struct bpf_iter_seq_task_file_info),
724 };
725 
726 static struct bpf_iter_reg task_file_reg_info = {
727 	.target			= "task_file",
728 	.attach_target		= bpf_iter_attach_task,
729 	.feature		= BPF_ITER_RESCHED,
730 	.ctx_arg_info_size	= 2,
731 	.ctx_arg_info		= {
732 		{ offsetof(struct bpf_iter__task_file, task),
733 		  PTR_TO_BTF_ID_OR_NULL },
734 		{ offsetof(struct bpf_iter__task_file, file),
735 		  PTR_TO_BTF_ID_OR_NULL },
736 	},
737 	.seq_info		= &task_file_seq_info,
738 	.fill_link_info		= bpf_iter_fill_link_info,
739 	.show_fdinfo		= bpf_iter_task_show_fdinfo,
740 };
741 
742 static const struct bpf_iter_seq_info task_vma_seq_info = {
743 	.seq_ops		= &task_vma_seq_ops,
744 	.init_seq_private	= init_seq_pidns,
745 	.fini_seq_private	= fini_seq_pidns,
746 	.seq_priv_size		= sizeof(struct bpf_iter_seq_task_vma_info),
747 };
748 
749 static struct bpf_iter_reg task_vma_reg_info = {
750 	.target			= "task_vma",
751 	.attach_target		= bpf_iter_attach_task,
752 	.feature		= BPF_ITER_RESCHED,
753 	.ctx_arg_info_size	= 2,
754 	.ctx_arg_info		= {
755 		{ offsetof(struct bpf_iter__task_vma, task),
756 		  PTR_TO_BTF_ID_OR_NULL },
757 		{ offsetof(struct bpf_iter__task_vma, vma),
758 		  PTR_TO_BTF_ID_OR_NULL },
759 	},
760 	.seq_info		= &task_vma_seq_info,
761 	.fill_link_info		= bpf_iter_fill_link_info,
762 	.show_fdinfo		= bpf_iter_task_show_fdinfo,
763 };
764 
765 BPF_CALL_5(bpf_find_vma, struct task_struct *, task, u64, start,
766 	   bpf_callback_t, callback_fn, void *, callback_ctx, u64, flags)
767 {
768 	struct mmap_unlock_irq_work *work = NULL;
769 	struct vm_area_struct *vma;
770 	bool irq_work_busy = false;
771 	struct mm_struct *mm;
772 	int ret = -ENOENT;
773 
774 	if (flags)
775 		return -EINVAL;
776 
777 	if (!task)
778 		return -ENOENT;
779 
780 	mm = task->mm;
781 	if (!mm)
782 		return -ENOENT;
783 
784 	irq_work_busy = bpf_mmap_unlock_get_irq_work(&work);
785 
786 	if (irq_work_busy || !mmap_read_trylock(mm))
787 		return -EBUSY;
788 
789 	vma = find_vma(mm, start);
790 
791 	if (vma && vma->vm_start <= start && vma->vm_end > start) {
792 		callback_fn((u64)(long)task, (u64)(long)vma,
793 			    (u64)(long)callback_ctx, 0, 0);
794 		ret = 0;
795 	}
796 	bpf_mmap_unlock_mm(work, mm);
797 	return ret;
798 }
799 
800 const struct bpf_func_proto bpf_find_vma_proto = {
801 	.func		= bpf_find_vma,
802 	.ret_type	= RET_INTEGER,
803 	.arg1_type	= ARG_PTR_TO_BTF_ID,
804 	.arg1_btf_id	= &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
805 	.arg2_type	= ARG_ANYTHING,
806 	.arg3_type	= ARG_PTR_TO_FUNC,
807 	.arg4_type	= ARG_PTR_TO_STACK_OR_NULL,
808 	.arg5_type	= ARG_ANYTHING,
809 };
810 
811 DEFINE_PER_CPU(struct mmap_unlock_irq_work, mmap_unlock_work);
812 
813 static void do_mmap_read_unlock(struct irq_work *entry)
814 {
815 	struct mmap_unlock_irq_work *work;
816 
817 	if (WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_RT)))
818 		return;
819 
820 	work = container_of(entry, struct mmap_unlock_irq_work, irq_work);
821 	mmap_read_unlock_non_owner(work->mm);
822 }
823 
824 static int __init task_iter_init(void)
825 {
826 	struct mmap_unlock_irq_work *work;
827 	int ret, cpu;
828 
829 	for_each_possible_cpu(cpu) {
830 		work = per_cpu_ptr(&mmap_unlock_work, cpu);
831 		init_irq_work(&work->irq_work, do_mmap_read_unlock);
832 	}
833 
834 	task_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
835 	ret = bpf_iter_reg_target(&task_reg_info);
836 	if (ret)
837 		return ret;
838 
839 	task_file_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
840 	task_file_reg_info.ctx_arg_info[1].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_FILE];
841 	ret =  bpf_iter_reg_target(&task_file_reg_info);
842 	if (ret)
843 		return ret;
844 
845 	task_vma_reg_info.ctx_arg_info[0].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_TASK];
846 	task_vma_reg_info.ctx_arg_info[1].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_VMA];
847 	return bpf_iter_reg_target(&task_vma_reg_info);
848 }
849 late_initcall(task_iter_init);
850