Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Miscellaneous cgroup controller
4 *
5 * Copyright 2020 Google LLC
6 * Author: Vipin Sharma <vipinsh@google.com>
7 */
8
9#include <linux/limits.h>
10#include <linux/cgroup.h>
11#include <linux/errno.h>
12#include <linux/atomic.h>
13#include <linux/slab.h>
14#include <linux/misc_cgroup.h>
15
16#define MAX_STR "max"
17#define MAX_NUM ULONG_MAX
18
19/* Miscellaneous res name, keep it in sync with enum misc_res_type */
20static const char *const misc_res_name[] = {
21#ifdef CONFIG_KVM_AMD_SEV
22 /* AMD SEV ASIDs resource */
23 "sev",
24 /* AMD SEV-ES ASIDs resource */
25 "sev_es",
26#endif
27};
28
29/* Root misc cgroup */
30static struct misc_cg root_cg;
31
32/*
33 * Miscellaneous resources capacity for the entire machine. 0 capacity means
34 * resource is not initialized or not present in the host.
35 *
36 * root_cg.max and capacity are independent of each other. root_cg.max can be
37 * more than the actual capacity. We are using Limits resource distribution
38 * model of cgroup for miscellaneous controller.
39 */
40static unsigned long misc_res_capacity[MISC_CG_RES_TYPES];
41
42/**
43 * parent_misc() - Get the parent of the passed misc cgroup.
44 * @cgroup: cgroup whose parent needs to be fetched.
45 *
46 * Context: Any context.
47 * Return:
48 * * struct misc_cg* - Parent of the @cgroup.
49 * * %NULL - If @cgroup is null or the passed cgroup does not have a parent.
50 */
51static struct misc_cg *parent_misc(struct misc_cg *cgroup)
52{
53 return cgroup ? css_misc(cgroup->css.parent) : NULL;
54}
55
56/**
57 * valid_type() - Check if @type is valid or not.
58 * @type: misc res type.
59 *
60 * Context: Any context.
61 * Return:
62 * * true - If valid type.
63 * * false - If not valid type.
64 */
65static inline bool valid_type(enum misc_res_type type)
66{
67 return type >= 0 && type < MISC_CG_RES_TYPES;
68}
69
70/**
71 * misc_cg_res_total_usage() - Get the current total usage of the resource.
72 * @type: misc res type.
73 *
74 * Context: Any context.
75 * Return: Current total usage of the resource.
76 */
77unsigned long misc_cg_res_total_usage(enum misc_res_type type)
78{
79 if (valid_type(type))
80 return atomic_long_read(&root_cg.res[type].usage);
81
82 return 0;
83}
84EXPORT_SYMBOL_GPL(misc_cg_res_total_usage);
85
86/**
87 * misc_cg_set_capacity() - Set the capacity of the misc cgroup res.
88 * @type: Type of the misc res.
89 * @capacity: Supported capacity of the misc res on the host.
90 *
91 * If capacity is 0 then the charging a misc cgroup fails for that type.
92 *
93 * Context: Any context.
94 * Return:
95 * * %0 - Successfully registered the capacity.
96 * * %-EINVAL - If @type is invalid.
97 */
98int misc_cg_set_capacity(enum misc_res_type type, unsigned long capacity)
99{
100 if (!valid_type(type))
101 return -EINVAL;
102
103 WRITE_ONCE(misc_res_capacity[type], capacity);
104 return 0;
105}
106EXPORT_SYMBOL_GPL(misc_cg_set_capacity);
107
108/**
109 * misc_cg_cancel_charge() - Cancel the charge from the misc cgroup.
110 * @type: Misc res type in misc cg to cancel the charge from.
111 * @cg: Misc cgroup to cancel charge from.
112 * @amount: Amount to cancel.
113 *
114 * Context: Any context.
115 */
116static void misc_cg_cancel_charge(enum misc_res_type type, struct misc_cg *cg,
117 unsigned long amount)
118{
119 WARN_ONCE(atomic_long_add_negative(-amount, &cg->res[type].usage),
120 "misc cgroup resource %s became less than 0",
121 misc_res_name[type]);
122}
123
124/**
125 * misc_cg_try_charge() - Try charging the misc cgroup.
126 * @type: Misc res type to charge.
127 * @cg: Misc cgroup which will be charged.
128 * @amount: Amount to charge.
129 *
130 * Charge @amount to the misc cgroup. Caller must use the same cgroup during
131 * the uncharge call.
132 *
133 * Context: Any context.
134 * Return:
135 * * %0 - If successfully charged.
136 * * -EINVAL - If @type is invalid or misc res has 0 capacity.
137 * * -EBUSY - If max limit will be crossed or total usage will be more than the
138 * capacity.
139 */
140int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg,
141 unsigned long amount)
142{
143 struct misc_cg *i, *j;
144 int ret;
145 struct misc_res *res;
146 int new_usage;
147
148 if (!(valid_type(type) && cg && READ_ONCE(misc_res_capacity[type])))
149 return -EINVAL;
150
151 if (!amount)
152 return 0;
153
154 for (i = cg; i; i = parent_misc(i)) {
155 res = &i->res[type];
156
157 new_usage = atomic_long_add_return(amount, &res->usage);
158 if (new_usage > READ_ONCE(res->max) ||
159 new_usage > READ_ONCE(misc_res_capacity[type])) {
160 ret = -EBUSY;
161 goto err_charge;
162 }
163 }
164 return 0;
165
166err_charge:
167 for (j = i; j; j = parent_misc(j)) {
168 atomic_long_inc(&j->res[type].events);
169 cgroup_file_notify(&j->events_file);
170 }
171
172 for (j = cg; j != i; j = parent_misc(j))
173 misc_cg_cancel_charge(type, j, amount);
174 misc_cg_cancel_charge(type, i, amount);
175 return ret;
176}
177EXPORT_SYMBOL_GPL(misc_cg_try_charge);
178
179/**
180 * misc_cg_uncharge() - Uncharge the misc cgroup.
181 * @type: Misc res type which was charged.
182 * @cg: Misc cgroup which will be uncharged.
183 * @amount: Charged amount.
184 *
185 * Context: Any context.
186 */
187void misc_cg_uncharge(enum misc_res_type type, struct misc_cg *cg,
188 unsigned long amount)
189{
190 struct misc_cg *i;
191
192 if (!(amount && valid_type(type) && cg))
193 return;
194
195 for (i = cg; i; i = parent_misc(i))
196 misc_cg_cancel_charge(type, i, amount);
197}
198EXPORT_SYMBOL_GPL(misc_cg_uncharge);
199
200/**
201 * misc_cg_max_show() - Show the misc cgroup max limit.
202 * @sf: Interface file
203 * @v: Arguments passed
204 *
205 * Context: Any context.
206 * Return: 0 to denote successful print.
207 */
208static int misc_cg_max_show(struct seq_file *sf, void *v)
209{
210 int i;
211 struct misc_cg *cg = css_misc(seq_css(sf));
212 unsigned long max;
213
214 for (i = 0; i < MISC_CG_RES_TYPES; i++) {
215 if (READ_ONCE(misc_res_capacity[i])) {
216 max = READ_ONCE(cg->res[i].max);
217 if (max == MAX_NUM)
218 seq_printf(sf, "%s max\n", misc_res_name[i]);
219 else
220 seq_printf(sf, "%s %lu\n", misc_res_name[i],
221 max);
222 }
223 }
224
225 return 0;
226}
227
228/**
229 * misc_cg_max_write() - Update the maximum limit of the cgroup.
230 * @of: Handler for the file.
231 * @buf: Data from the user. It should be either "max", 0, or a positive
232 * integer.
233 * @nbytes: Number of bytes of the data.
234 * @off: Offset in the file.
235 *
236 * User can pass data like:
237 * echo sev 23 > misc.max, OR
238 * echo sev max > misc.max
239 *
240 * Context: Any context.
241 * Return:
242 * * >= 0 - Number of bytes processed in the input.
243 * * -EINVAL - If buf is not valid.
244 * * -ERANGE - If number is bigger than the unsigned long capacity.
245 */
246static ssize_t misc_cg_max_write(struct kernfs_open_file *of, char *buf,
247 size_t nbytes, loff_t off)
248{
249 struct misc_cg *cg;
250 unsigned long max;
251 int ret = 0, i;
252 enum misc_res_type type = MISC_CG_RES_TYPES;
253 char *token;
254
255 buf = strstrip(buf);
256 token = strsep(&buf, " ");
257
258 if (!token || !buf)
259 return -EINVAL;
260
261 for (i = 0; i < MISC_CG_RES_TYPES; i++) {
262 if (!strcmp(misc_res_name[i], token)) {
263 type = i;
264 break;
265 }
266 }
267
268 if (type == MISC_CG_RES_TYPES)
269 return -EINVAL;
270
271 if (!strcmp(MAX_STR, buf)) {
272 max = MAX_NUM;
273 } else {
274 ret = kstrtoul(buf, 0, &max);
275 if (ret)
276 return ret;
277 }
278
279 cg = css_misc(of_css(of));
280
281 if (READ_ONCE(misc_res_capacity[type]))
282 WRITE_ONCE(cg->res[type].max, max);
283 else
284 ret = -EINVAL;
285
286 return ret ? ret : nbytes;
287}
288
289/**
290 * misc_cg_current_show() - Show the current usage of the misc cgroup.
291 * @sf: Interface file
292 * @v: Arguments passed
293 *
294 * Context: Any context.
295 * Return: 0 to denote successful print.
296 */
297static int misc_cg_current_show(struct seq_file *sf, void *v)
298{
299 int i;
300 unsigned long usage;
301 struct misc_cg *cg = css_misc(seq_css(sf));
302
303 for (i = 0; i < MISC_CG_RES_TYPES; i++) {
304 usage = atomic_long_read(&cg->res[i].usage);
305 if (READ_ONCE(misc_res_capacity[i]) || usage)
306 seq_printf(sf, "%s %lu\n", misc_res_name[i], usage);
307 }
308
309 return 0;
310}
311
312/**
313 * misc_cg_capacity_show() - Show the total capacity of misc res on the host.
314 * @sf: Interface file
315 * @v: Arguments passed
316 *
317 * Only present in the root cgroup directory.
318 *
319 * Context: Any context.
320 * Return: 0 to denote successful print.
321 */
322static int misc_cg_capacity_show(struct seq_file *sf, void *v)
323{
324 int i;
325 unsigned long cap;
326
327 for (i = 0; i < MISC_CG_RES_TYPES; i++) {
328 cap = READ_ONCE(misc_res_capacity[i]);
329 if (cap)
330 seq_printf(sf, "%s %lu\n", misc_res_name[i], cap);
331 }
332
333 return 0;
334}
335
336static int misc_events_show(struct seq_file *sf, void *v)
337{
338 struct misc_cg *cg = css_misc(seq_css(sf));
339 unsigned long events, i;
340
341 for (i = 0; i < MISC_CG_RES_TYPES; i++) {
342 events = atomic_long_read(&cg->res[i].events);
343 if (READ_ONCE(misc_res_capacity[i]) || events)
344 seq_printf(sf, "%s.max %lu\n", misc_res_name[i], events);
345 }
346 return 0;
347}
348
349/* Misc cgroup interface files */
350static struct cftype misc_cg_files[] = {
351 {
352 .name = "max",
353 .write = misc_cg_max_write,
354 .seq_show = misc_cg_max_show,
355 .flags = CFTYPE_NOT_ON_ROOT,
356 },
357 {
358 .name = "current",
359 .seq_show = misc_cg_current_show,
360 .flags = CFTYPE_NOT_ON_ROOT,
361 },
362 {
363 .name = "capacity",
364 .seq_show = misc_cg_capacity_show,
365 .flags = CFTYPE_ONLY_ON_ROOT,
366 },
367 {
368 .name = "events",
369 .flags = CFTYPE_NOT_ON_ROOT,
370 .file_offset = offsetof(struct misc_cg, events_file),
371 .seq_show = misc_events_show,
372 },
373 {}
374};
375
376/**
377 * misc_cg_alloc() - Allocate misc cgroup.
378 * @parent_css: Parent cgroup.
379 *
380 * Context: Process context.
381 * Return:
382 * * struct cgroup_subsys_state* - css of the allocated cgroup.
383 * * ERR_PTR(-ENOMEM) - No memory available to allocate.
384 */
385static struct cgroup_subsys_state *
386misc_cg_alloc(struct cgroup_subsys_state *parent_css)
387{
388 enum misc_res_type i;
389 struct misc_cg *cg;
390
391 if (!parent_css) {
392 cg = &root_cg;
393 } else {
394 cg = kzalloc(sizeof(*cg), GFP_KERNEL);
395 if (!cg)
396 return ERR_PTR(-ENOMEM);
397 }
398
399 for (i = 0; i < MISC_CG_RES_TYPES; i++) {
400 WRITE_ONCE(cg->res[i].max, MAX_NUM);
401 atomic_long_set(&cg->res[i].usage, 0);
402 }
403
404 return &cg->css;
405}
406
407/**
408 * misc_cg_free() - Free the misc cgroup.
409 * @css: cgroup subsys object.
410 *
411 * Context: Any context.
412 */
413static void misc_cg_free(struct cgroup_subsys_state *css)
414{
415 kfree(css_misc(css));
416}
417
418/* Cgroup controller callbacks */
419struct cgroup_subsys misc_cgrp_subsys = {
420 .css_alloc = misc_cg_alloc,
421 .css_free = misc_cg_free,
422 .legacy_cftypes = misc_cg_files,
423 .dfl_cftypes = misc_cg_files,
424};
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Miscellaneous cgroup controller
4 *
5 * Copyright 2020 Google LLC
6 * Author: Vipin Sharma <vipinsh@google.com>
7 */
8
9#include <linux/limits.h>
10#include <linux/cgroup.h>
11#include <linux/errno.h>
12#include <linux/atomic.h>
13#include <linux/slab.h>
14#include <linux/misc_cgroup.h>
15
16#define MAX_STR "max"
17#define MAX_NUM U64_MAX
18
19/* Miscellaneous res name, keep it in sync with enum misc_res_type */
20static const char *const misc_res_name[] = {
21#ifdef CONFIG_KVM_AMD_SEV
22 /* AMD SEV ASIDs resource */
23 "sev",
24 /* AMD SEV-ES ASIDs resource */
25 "sev_es",
26#endif
27};
28
29/* Root misc cgroup */
30static struct misc_cg root_cg;
31
32/*
33 * Miscellaneous resources capacity for the entire machine. 0 capacity means
34 * resource is not initialized or not present in the host.
35 *
36 * root_cg.max and capacity are independent of each other. root_cg.max can be
37 * more than the actual capacity. We are using Limits resource distribution
38 * model of cgroup for miscellaneous controller.
39 */
40static u64 misc_res_capacity[MISC_CG_RES_TYPES];
41
42/**
43 * parent_misc() - Get the parent of the passed misc cgroup.
44 * @cgroup: cgroup whose parent needs to be fetched.
45 *
46 * Context: Any context.
47 * Return:
48 * * struct misc_cg* - Parent of the @cgroup.
49 * * %NULL - If @cgroup is null or the passed cgroup does not have a parent.
50 */
51static struct misc_cg *parent_misc(struct misc_cg *cgroup)
52{
53 return cgroup ? css_misc(cgroup->css.parent) : NULL;
54}
55
56/**
57 * valid_type() - Check if @type is valid or not.
58 * @type: misc res type.
59 *
60 * Context: Any context.
61 * Return:
62 * * true - If valid type.
63 * * false - If not valid type.
64 */
65static inline bool valid_type(enum misc_res_type type)
66{
67 return type >= 0 && type < MISC_CG_RES_TYPES;
68}
69
70/**
71 * misc_cg_res_total_usage() - Get the current total usage of the resource.
72 * @type: misc res type.
73 *
74 * Context: Any context.
75 * Return: Current total usage of the resource.
76 */
77u64 misc_cg_res_total_usage(enum misc_res_type type)
78{
79 if (valid_type(type))
80 return atomic64_read(&root_cg.res[type].usage);
81
82 return 0;
83}
84EXPORT_SYMBOL_GPL(misc_cg_res_total_usage);
85
86/**
87 * misc_cg_set_capacity() - Set the capacity of the misc cgroup res.
88 * @type: Type of the misc res.
89 * @capacity: Supported capacity of the misc res on the host.
90 *
91 * If capacity is 0 then the charging a misc cgroup fails for that type.
92 *
93 * Context: Any context.
94 * Return:
95 * * %0 - Successfully registered the capacity.
96 * * %-EINVAL - If @type is invalid.
97 */
98int misc_cg_set_capacity(enum misc_res_type type, u64 capacity)
99{
100 if (!valid_type(type))
101 return -EINVAL;
102
103 WRITE_ONCE(misc_res_capacity[type], capacity);
104 return 0;
105}
106EXPORT_SYMBOL_GPL(misc_cg_set_capacity);
107
108/**
109 * misc_cg_cancel_charge() - Cancel the charge from the misc cgroup.
110 * @type: Misc res type in misc cg to cancel the charge from.
111 * @cg: Misc cgroup to cancel charge from.
112 * @amount: Amount to cancel.
113 *
114 * Context: Any context.
115 */
116static void misc_cg_cancel_charge(enum misc_res_type type, struct misc_cg *cg,
117 u64 amount)
118{
119 WARN_ONCE(atomic64_add_negative(-amount, &cg->res[type].usage),
120 "misc cgroup resource %s became less than 0",
121 misc_res_name[type]);
122}
123
124static void misc_cg_update_watermark(struct misc_res *res, u64 new_usage)
125{
126 u64 old;
127
128 while (true) {
129 old = atomic64_read(&res->watermark);
130 if (new_usage <= old)
131 break;
132 if (atomic64_cmpxchg(&res->watermark, old, new_usage) == old)
133 break;
134 }
135}
136
137static void misc_cg_event(enum misc_res_type type, struct misc_cg *cg)
138{
139 atomic64_inc(&cg->res[type].events_local);
140 cgroup_file_notify(&cg->events_local_file);
141
142 for (; parent_misc(cg); cg = parent_misc(cg)) {
143 atomic64_inc(&cg->res[type].events);
144 cgroup_file_notify(&cg->events_file);
145 }
146}
147
148/**
149 * misc_cg_try_charge() - Try charging the misc cgroup.
150 * @type: Misc res type to charge.
151 * @cg: Misc cgroup which will be charged.
152 * @amount: Amount to charge.
153 *
154 * Charge @amount to the misc cgroup. Caller must use the same cgroup during
155 * the uncharge call.
156 *
157 * Context: Any context.
158 * Return:
159 * * %0 - If successfully charged.
160 * * -EINVAL - If @type is invalid or misc res has 0 capacity.
161 * * -EBUSY - If max limit will be crossed or total usage will be more than the
162 * capacity.
163 */
164int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg, u64 amount)
165{
166 struct misc_cg *i, *j;
167 int ret;
168 struct misc_res *res;
169 u64 new_usage;
170
171 if (!(valid_type(type) && cg && READ_ONCE(misc_res_capacity[type])))
172 return -EINVAL;
173
174 if (!amount)
175 return 0;
176
177 for (i = cg; i; i = parent_misc(i)) {
178 res = &i->res[type];
179
180 new_usage = atomic64_add_return(amount, &res->usage);
181 if (new_usage > READ_ONCE(res->max) ||
182 new_usage > READ_ONCE(misc_res_capacity[type])) {
183 ret = -EBUSY;
184 goto err_charge;
185 }
186 misc_cg_update_watermark(res, new_usage);
187 }
188 return 0;
189
190err_charge:
191 misc_cg_event(type, i);
192
193 for (j = cg; j != i; j = parent_misc(j))
194 misc_cg_cancel_charge(type, j, amount);
195 misc_cg_cancel_charge(type, i, amount);
196 return ret;
197}
198EXPORT_SYMBOL_GPL(misc_cg_try_charge);
199
200/**
201 * misc_cg_uncharge() - Uncharge the misc cgroup.
202 * @type: Misc res type which was charged.
203 * @cg: Misc cgroup which will be uncharged.
204 * @amount: Charged amount.
205 *
206 * Context: Any context.
207 */
208void misc_cg_uncharge(enum misc_res_type type, struct misc_cg *cg, u64 amount)
209{
210 struct misc_cg *i;
211
212 if (!(amount && valid_type(type) && cg))
213 return;
214
215 for (i = cg; i; i = parent_misc(i))
216 misc_cg_cancel_charge(type, i, amount);
217}
218EXPORT_SYMBOL_GPL(misc_cg_uncharge);
219
220/**
221 * misc_cg_max_show() - Show the misc cgroup max limit.
222 * @sf: Interface file
223 * @v: Arguments passed
224 *
225 * Context: Any context.
226 * Return: 0 to denote successful print.
227 */
228static int misc_cg_max_show(struct seq_file *sf, void *v)
229{
230 int i;
231 struct misc_cg *cg = css_misc(seq_css(sf));
232 u64 max;
233
234 for (i = 0; i < MISC_CG_RES_TYPES; i++) {
235 if (READ_ONCE(misc_res_capacity[i])) {
236 max = READ_ONCE(cg->res[i].max);
237 if (max == MAX_NUM)
238 seq_printf(sf, "%s max\n", misc_res_name[i]);
239 else
240 seq_printf(sf, "%s %llu\n", misc_res_name[i],
241 max);
242 }
243 }
244
245 return 0;
246}
247
248/**
249 * misc_cg_max_write() - Update the maximum limit of the cgroup.
250 * @of: Handler for the file.
251 * @buf: Data from the user. It should be either "max", 0, or a positive
252 * integer.
253 * @nbytes: Number of bytes of the data.
254 * @off: Offset in the file.
255 *
256 * User can pass data like:
257 * echo sev 23 > misc.max, OR
258 * echo sev max > misc.max
259 *
260 * Context: Any context.
261 * Return:
262 * * >= 0 - Number of bytes processed in the input.
263 * * -EINVAL - If buf is not valid.
264 * * -ERANGE - If number is bigger than the u64 capacity.
265 */
266static ssize_t misc_cg_max_write(struct kernfs_open_file *of, char *buf,
267 size_t nbytes, loff_t off)
268{
269 struct misc_cg *cg;
270 u64 max;
271 int ret = 0, i;
272 enum misc_res_type type = MISC_CG_RES_TYPES;
273 char *token;
274
275 buf = strstrip(buf);
276 token = strsep(&buf, " ");
277
278 if (!token || !buf)
279 return -EINVAL;
280
281 for (i = 0; i < MISC_CG_RES_TYPES; i++) {
282 if (!strcmp(misc_res_name[i], token)) {
283 type = i;
284 break;
285 }
286 }
287
288 if (type == MISC_CG_RES_TYPES)
289 return -EINVAL;
290
291 if (!strcmp(MAX_STR, buf)) {
292 max = MAX_NUM;
293 } else {
294 ret = kstrtou64(buf, 0, &max);
295 if (ret)
296 return ret;
297 }
298
299 cg = css_misc(of_css(of));
300
301 if (READ_ONCE(misc_res_capacity[type]))
302 WRITE_ONCE(cg->res[type].max, max);
303 else
304 ret = -EINVAL;
305
306 return ret ? ret : nbytes;
307}
308
309/**
310 * misc_cg_current_show() - Show the current usage of the misc cgroup.
311 * @sf: Interface file
312 * @v: Arguments passed
313 *
314 * Context: Any context.
315 * Return: 0 to denote successful print.
316 */
317static int misc_cg_current_show(struct seq_file *sf, void *v)
318{
319 int i;
320 u64 usage;
321 struct misc_cg *cg = css_misc(seq_css(sf));
322
323 for (i = 0; i < MISC_CG_RES_TYPES; i++) {
324 usage = atomic64_read(&cg->res[i].usage);
325 if (READ_ONCE(misc_res_capacity[i]) || usage)
326 seq_printf(sf, "%s %llu\n", misc_res_name[i], usage);
327 }
328
329 return 0;
330}
331
332/**
333 * misc_cg_peak_show() - Show the peak usage of the misc cgroup.
334 * @sf: Interface file
335 * @v: Arguments passed
336 *
337 * Context: Any context.
338 * Return: 0 to denote successful print.
339 */
340static int misc_cg_peak_show(struct seq_file *sf, void *v)
341{
342 int i;
343 u64 watermark;
344 struct misc_cg *cg = css_misc(seq_css(sf));
345
346 for (i = 0; i < MISC_CG_RES_TYPES; i++) {
347 watermark = atomic64_read(&cg->res[i].watermark);
348 if (READ_ONCE(misc_res_capacity[i]) || watermark)
349 seq_printf(sf, "%s %llu\n", misc_res_name[i], watermark);
350 }
351
352 return 0;
353}
354
355/**
356 * misc_cg_capacity_show() - Show the total capacity of misc res on the host.
357 * @sf: Interface file
358 * @v: Arguments passed
359 *
360 * Only present in the root cgroup directory.
361 *
362 * Context: Any context.
363 * Return: 0 to denote successful print.
364 */
365static int misc_cg_capacity_show(struct seq_file *sf, void *v)
366{
367 int i;
368 u64 cap;
369
370 for (i = 0; i < MISC_CG_RES_TYPES; i++) {
371 cap = READ_ONCE(misc_res_capacity[i]);
372 if (cap)
373 seq_printf(sf, "%s %llu\n", misc_res_name[i], cap);
374 }
375
376 return 0;
377}
378
379static int __misc_events_show(struct seq_file *sf, bool local)
380{
381 struct misc_cg *cg = css_misc(seq_css(sf));
382 u64 events;
383 int i;
384
385 for (i = 0; i < MISC_CG_RES_TYPES; i++) {
386 if (local)
387 events = atomic64_read(&cg->res[i].events_local);
388 else
389 events = atomic64_read(&cg->res[i].events);
390 if (READ_ONCE(misc_res_capacity[i]) || events)
391 seq_printf(sf, "%s.max %llu\n", misc_res_name[i], events);
392 }
393 return 0;
394}
395
396static int misc_events_show(struct seq_file *sf, void *v)
397{
398 return __misc_events_show(sf, false);
399}
400
401static int misc_events_local_show(struct seq_file *sf, void *v)
402{
403 return __misc_events_show(sf, true);
404}
405
406/* Misc cgroup interface files */
407static struct cftype misc_cg_files[] = {
408 {
409 .name = "max",
410 .write = misc_cg_max_write,
411 .seq_show = misc_cg_max_show,
412 .flags = CFTYPE_NOT_ON_ROOT,
413 },
414 {
415 .name = "current",
416 .seq_show = misc_cg_current_show,
417 },
418 {
419 .name = "peak",
420 .seq_show = misc_cg_peak_show,
421 },
422 {
423 .name = "capacity",
424 .seq_show = misc_cg_capacity_show,
425 .flags = CFTYPE_ONLY_ON_ROOT,
426 },
427 {
428 .name = "events",
429 .flags = CFTYPE_NOT_ON_ROOT,
430 .file_offset = offsetof(struct misc_cg, events_file),
431 .seq_show = misc_events_show,
432 },
433 {
434 .name = "events.local",
435 .flags = CFTYPE_NOT_ON_ROOT,
436 .file_offset = offsetof(struct misc_cg, events_local_file),
437 .seq_show = misc_events_local_show,
438 },
439 {}
440};
441
442/**
443 * misc_cg_alloc() - Allocate misc cgroup.
444 * @parent_css: Parent cgroup.
445 *
446 * Context: Process context.
447 * Return:
448 * * struct cgroup_subsys_state* - css of the allocated cgroup.
449 * * ERR_PTR(-ENOMEM) - No memory available to allocate.
450 */
451static struct cgroup_subsys_state *
452misc_cg_alloc(struct cgroup_subsys_state *parent_css)
453{
454 enum misc_res_type i;
455 struct misc_cg *cg;
456
457 if (!parent_css) {
458 cg = &root_cg;
459 } else {
460 cg = kzalloc(sizeof(*cg), GFP_KERNEL);
461 if (!cg)
462 return ERR_PTR(-ENOMEM);
463 }
464
465 for (i = 0; i < MISC_CG_RES_TYPES; i++) {
466 WRITE_ONCE(cg->res[i].max, MAX_NUM);
467 atomic64_set(&cg->res[i].usage, 0);
468 }
469
470 return &cg->css;
471}
472
473/**
474 * misc_cg_free() - Free the misc cgroup.
475 * @css: cgroup subsys object.
476 *
477 * Context: Any context.
478 */
479static void misc_cg_free(struct cgroup_subsys_state *css)
480{
481 kfree(css_misc(css));
482}
483
484/* Cgroup controller callbacks */
485struct cgroup_subsys misc_cgrp_subsys = {
486 .css_alloc = misc_cg_alloc,
487 .css_free = misc_cg_free,
488 .legacy_cftypes = misc_cg_files,
489 .dfl_cftypes = misc_cg_files,
490};