1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * net/core/netclassid_cgroup.c Classid Cgroupfs Handling
4 *
5 * Authors: Thomas Graf <tgraf@suug.ch>
6 */
7
8 #include <linux/slab.h>
9 #include <linux/cgroup.h>
10 #include <linux/fdtable.h>
11 #include <linux/sched/task.h>
12
13 #include <net/cls_cgroup.h>
14 #include <net/sock.h>
15 #include <trace/hooks/net.h>
16
_trace_android_vh_task_get_classid(const struct sk_buff * skb,u32 * classid)17 void _trace_android_vh_task_get_classid(const struct sk_buff *skb, u32 *classid)
18 {
19 trace_android_vh_task_get_classid(skb, classid);
20 }
21 EXPORT_SYMBOL_GPL(_trace_android_vh_task_get_classid);
22
css_cls_state(struct cgroup_subsys_state * css)23 static inline struct cgroup_cls_state *css_cls_state(struct cgroup_subsys_state *css)
24 {
25 return css ? container_of(css, struct cgroup_cls_state, css) : NULL;
26 }
27
task_cls_state(struct task_struct * p)28 struct cgroup_cls_state *task_cls_state(struct task_struct *p)
29 {
30 return css_cls_state(task_css_check(p, net_cls_cgrp_id,
31 rcu_read_lock_bh_held()));
32 }
33 EXPORT_SYMBOL_GPL(task_cls_state);
34
35 static struct cgroup_subsys_state *
cgrp_css_alloc(struct cgroup_subsys_state * parent_css)36 cgrp_css_alloc(struct cgroup_subsys_state *parent_css)
37 {
38 struct cgroup_cls_state *cs;
39
40 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
41 if (!cs)
42 return ERR_PTR(-ENOMEM);
43
44 return &cs->css;
45 }
46
cgrp_css_online(struct cgroup_subsys_state * css)47 static int cgrp_css_online(struct cgroup_subsys_state *css)
48 {
49 struct cgroup_cls_state *cs = css_cls_state(css);
50 struct cgroup_cls_state *parent = css_cls_state(css->parent);
51
52 if (parent)
53 cs->classid = parent->classid;
54
55 return 0;
56 }
57
cgrp_css_free(struct cgroup_subsys_state * css)58 static void cgrp_css_free(struct cgroup_subsys_state *css)
59 {
60 kfree(css_cls_state(css));
61 }
62
63 /*
64 * To avoid freezing of sockets creation for tasks with big number of threads
65 * and opened sockets lets release file_lock every 1000 iterated descriptors.
66 * New sockets will already have been created with new classid.
67 */
68
69 struct update_classid_context {
70 u32 classid;
71 unsigned int batch;
72 };
73
74 #define UPDATE_CLASSID_BATCH 1000
75
update_classid_sock(const void * v,struct file * file,unsigned int n)76 static int update_classid_sock(const void *v, struct file *file, unsigned int n)
77 {
78 struct update_classid_context *ctx = (void *)v;
79 struct socket *sock = sock_from_file(file);
80
81 if (sock)
82 sock_cgroup_set_classid(&sock->sk->sk_cgrp_data, ctx->classid);
83 if (--ctx->batch == 0) {
84 ctx->batch = UPDATE_CLASSID_BATCH;
85 return n + 1;
86 }
87 return 0;
88 }
89
update_classid_task(struct task_struct * p,u32 classid)90 static void update_classid_task(struct task_struct *p, u32 classid)
91 {
92 struct update_classid_context ctx = {
93 .classid = classid,
94 .batch = UPDATE_CLASSID_BATCH
95 };
96 unsigned int fd = 0;
97
98 /* Only update the leader task, when many threads in this task,
99 * so it can avoid the useless traversal.
100 */
101 if (p != p->group_leader)
102 return;
103
104 do {
105 task_lock(p);
106 fd = iterate_fd(p->files, fd, update_classid_sock, &ctx);
107 task_unlock(p);
108 cond_resched();
109 } while (fd);
110 }
111
cgrp_attach(struct cgroup_taskset * tset)112 static void cgrp_attach(struct cgroup_taskset *tset)
113 {
114 struct cgroup_subsys_state *css;
115 struct task_struct *p;
116
117 cgroup_taskset_for_each(p, css, tset) {
118 update_classid_task(p, css_cls_state(css)->classid);
119 }
120 }
121
read_classid(struct cgroup_subsys_state * css,struct cftype * cft)122 static u64 read_classid(struct cgroup_subsys_state *css, struct cftype *cft)
123 {
124 return css_cls_state(css)->classid;
125 }
126
write_classid(struct cgroup_subsys_state * css,struct cftype * cft,u64 value)127 static int write_classid(struct cgroup_subsys_state *css, struct cftype *cft,
128 u64 value)
129 {
130 struct cgroup_cls_state *cs = css_cls_state(css);
131 struct css_task_iter it;
132 struct task_struct *p;
133
134 cs->classid = (u32)value;
135
136 css_task_iter_start(css, 0, &it);
137 while ((p = css_task_iter_next(&it)))
138 update_classid_task(p, cs->classid);
139 css_task_iter_end(&it);
140
141 return 0;
142 }
143
144 static struct cftype ss_files[] = {
145 {
146 .name = "classid",
147 .read_u64 = read_classid,
148 .write_u64 = write_classid,
149 },
150 { } /* terminate */
151 };
152
153 struct cgroup_subsys net_cls_cgrp_subsys = {
154 .css_alloc = cgrp_css_alloc,
155 .css_online = cgrp_css_online,
156 .css_free = cgrp_css_free,
157 .attach = cgrp_attach,
158 .legacy_cftypes = ss_files,
159 };
160