Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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