1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include <linux/kernel.h>
35#include <linux/threads.h>
36#include <linux/atomic.h>
37#include <linux/cgroup.h>
38#include <linux/slab.h>
39
40#define PIDS_MAX (PID_MAX_LIMIT + 1ULL)
41#define PIDS_MAX_STR "max"
42
43struct pids_cgroup {
44 struct cgroup_subsys_state css;
45
46
47
48
49
50 atomic64_t counter;
51 int64_t limit;
52};
53
54static struct pids_cgroup *css_pids(struct cgroup_subsys_state *css)
55{
56 return container_of(css, struct pids_cgroup, css);
57}
58
59static struct pids_cgroup *parent_pids(struct pids_cgroup *pids)
60{
61 return css_pids(pids->css.parent);
62}
63
64static struct cgroup_subsys_state *
65pids_css_alloc(struct cgroup_subsys_state *parent)
66{
67 struct pids_cgroup *pids;
68
69 pids = kzalloc(sizeof(struct pids_cgroup), GFP_KERNEL);
70 if (!pids)
71 return ERR_PTR(-ENOMEM);
72
73 pids->limit = PIDS_MAX;
74 atomic64_set(&pids->counter, 0);
75 return &pids->css;
76}
77
78static void pids_css_free(struct cgroup_subsys_state *css)
79{
80 kfree(css_pids(css));
81}
82
83
84
85
86
87
88
89
90
91static void pids_cancel(struct pids_cgroup *pids, int num)
92{
93
94
95
96
97 WARN_ON_ONCE(atomic64_add_negative(-num, &pids->counter));
98}
99
100
101
102
103
104
105static void pids_uncharge(struct pids_cgroup *pids, int num)
106{
107 struct pids_cgroup *p;
108
109 for (p = pids; parent_pids(p); p = parent_pids(p))
110 pids_cancel(p, num);
111}
112
113
114
115
116
117
118
119
120
121
122static void pids_charge(struct pids_cgroup *pids, int num)
123{
124 struct pids_cgroup *p;
125
126 for (p = pids; parent_pids(p); p = parent_pids(p))
127 atomic64_add(num, &p->counter);
128}
129
130
131
132
133
134
135
136
137
138
139static int pids_try_charge(struct pids_cgroup *pids, int num)
140{
141 struct pids_cgroup *p, *q;
142
143 for (p = pids; parent_pids(p); p = parent_pids(p)) {
144 int64_t new = atomic64_add_return(num, &p->counter);
145
146
147
148
149
150
151 if (new > p->limit)
152 goto revert;
153 }
154
155 return 0;
156
157revert:
158 for (q = pids; q != p; q = parent_pids(q))
159 pids_cancel(q, num);
160 pids_cancel(p, num);
161
162 return -EAGAIN;
163}
164
165static int pids_can_attach(struct cgroup_taskset *tset)
166{
167 struct task_struct *task;
168 struct cgroup_subsys_state *dst_css;
169
170 cgroup_taskset_for_each(task, dst_css, tset) {
171 struct pids_cgroup *pids = css_pids(dst_css);
172 struct cgroup_subsys_state *old_css;
173 struct pids_cgroup *old_pids;
174
175
176
177
178
179
180 old_css = task_css(task, pids_cgrp_id);
181 old_pids = css_pids(old_css);
182
183 pids_charge(pids, 1);
184 pids_uncharge(old_pids, 1);
185 }
186
187 return 0;
188}
189
190static void pids_cancel_attach(struct cgroup_taskset *tset)
191{
192 struct task_struct *task;
193 struct cgroup_subsys_state *dst_css;
194
195 cgroup_taskset_for_each(task, dst_css, tset) {
196 struct pids_cgroup *pids = css_pids(dst_css);
197 struct cgroup_subsys_state *old_css;
198 struct pids_cgroup *old_pids;
199
200 old_css = task_css(task, pids_cgrp_id);
201 old_pids = css_pids(old_css);
202
203 pids_charge(old_pids, 1);
204 pids_uncharge(pids, 1);
205 }
206}
207
208
209
210
211
212static int pids_can_fork(struct task_struct *task)
213{
214 struct cgroup_subsys_state *css;
215 struct pids_cgroup *pids;
216
217 css = task_css_check(current, pids_cgrp_id, true);
218 pids = css_pids(css);
219 return pids_try_charge(pids, 1);
220}
221
222static void pids_cancel_fork(struct task_struct *task)
223{
224 struct cgroup_subsys_state *css;
225 struct pids_cgroup *pids;
226
227 css = task_css_check(current, pids_cgrp_id, true);
228 pids = css_pids(css);
229 pids_uncharge(pids, 1);
230}
231
232static void pids_free(struct task_struct *task)
233{
234 struct pids_cgroup *pids = css_pids(task_css(task, pids_cgrp_id));
235
236 pids_uncharge(pids, 1);
237}
238
239static ssize_t pids_max_write(struct kernfs_open_file *of, char *buf,
240 size_t nbytes, loff_t off)
241{
242 struct cgroup_subsys_state *css = of_css(of);
243 struct pids_cgroup *pids = css_pids(css);
244 int64_t limit;
245 int err;
246
247 buf = strstrip(buf);
248 if (!strcmp(buf, PIDS_MAX_STR)) {
249 limit = PIDS_MAX;
250 goto set_limit;
251 }
252
253 err = kstrtoll(buf, 0, &limit);
254 if (err)
255 return err;
256
257 if (limit < 0 || limit >= PIDS_MAX)
258 return -EINVAL;
259
260set_limit:
261
262
263
264
265 pids->limit = limit;
266 return nbytes;
267}
268
269static int pids_max_show(struct seq_file *sf, void *v)
270{
271 struct cgroup_subsys_state *css = seq_css(sf);
272 struct pids_cgroup *pids = css_pids(css);
273 int64_t limit = pids->limit;
274
275 if (limit >= PIDS_MAX)
276 seq_printf(sf, "%s\n", PIDS_MAX_STR);
277 else
278 seq_printf(sf, "%lld\n", limit);
279
280 return 0;
281}
282
283static s64 pids_current_read(struct cgroup_subsys_state *css,
284 struct cftype *cft)
285{
286 struct pids_cgroup *pids = css_pids(css);
287
288 return atomic64_read(&pids->counter);
289}
290
291static struct cftype pids_files[] = {
292 {
293 .name = "max",
294 .write = pids_max_write,
295 .seq_show = pids_max_show,
296 .flags = CFTYPE_NOT_ON_ROOT,
297 },
298 {
299 .name = "current",
300 .read_s64 = pids_current_read,
301 .flags = CFTYPE_NOT_ON_ROOT,
302 },
303 { }
304};
305
306struct cgroup_subsys pids_cgrp_subsys = {
307 .css_alloc = pids_css_alloc,
308 .css_free = pids_css_free,
309 .can_attach = pids_can_attach,
310 .cancel_attach = pids_cancel_attach,
311 .can_fork = pids_can_fork,
312 .cancel_fork = pids_cancel_fork,
313 .free = pids_free,
314 .legacy_cftypes = pids_files,
315 .dfl_cftypes = pids_files,
316};
317