1 // SPDX-License-Identifier: GPL-2.0
3 * Miscellaneous cgroup controller
5 * Copyright 2020 Google LLC
6 * Author: Vipin Sharma <vipinsh@google.com>
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>
17 #define MAX_NUM ULONG_MAX
19 /* Miscellaneous res name, keep it in sync with enum misc_res_type */
20 static const char *const misc_res_name[] = {
21 #ifdef CONFIG_KVM_AMD_SEV
22 /* AMD SEV ASIDs resource */
24 /* AMD SEV-ES ASIDs resource */
29 /* Root misc cgroup */
30 static struct misc_cg root_cg;
33 * Miscellaneous resources capacity for the entire machine. 0 capacity means
34 * resource is not initialized or not present in the host.
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.
40 static unsigned long misc_res_capacity[MISC_CG_RES_TYPES];
43 * parent_misc() - Get the parent of the passed misc cgroup.
44 * @cgroup: cgroup whose parent needs to be fetched.
46 * Context: Any context.
48 * * struct misc_cg* - Parent of the @cgroup.
49 * * %NULL - If @cgroup is null or the passed cgroup does not have a parent.
51 static struct misc_cg *parent_misc(struct misc_cg *cgroup)
53 return cgroup ? css_misc(cgroup->css.parent) : NULL;
57 * valid_type() - Check if @type is valid or not.
58 * @type: misc res type.
60 * Context: Any context.
62 * * true - If valid type.
63 * * false - If not valid type.
65 static inline bool valid_type(enum misc_res_type type)
67 return type >= 0 && type < MISC_CG_RES_TYPES;
71 * misc_cg_res_total_usage() - Get the current total usage of the resource.
72 * @type: misc res type.
74 * Context: Any context.
75 * Return: Current total usage of the resource.
77 unsigned long misc_cg_res_total_usage(enum misc_res_type type)
80 return atomic_long_read(&root_cg.res[type].usage);
84 EXPORT_SYMBOL_GPL(misc_cg_res_total_usage);
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.
91 * If capacity is 0 then the charging a misc cgroup fails for that type.
93 * Context: Any context.
95 * * %0 - Successfully registered the capacity.
96 * * %-EINVAL - If @type is invalid.
98 int misc_cg_set_capacity(enum misc_res_type type, unsigned long capacity)
100 if (!valid_type(type))
103 WRITE_ONCE(misc_res_capacity[type], capacity);
106 EXPORT_SYMBOL_GPL(misc_cg_set_capacity);
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.
114 * Context: Any context.
116 static void misc_cg_cancel_charge(enum misc_res_type type, struct misc_cg *cg,
117 unsigned long amount)
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]);
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.
130 * Charge @amount to the misc cgroup. Caller must use the same cgroup during
133 * Context: Any context.
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
140 int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg,
141 unsigned long amount)
143 struct misc_cg *i, *j;
145 struct misc_res *res;
148 if (!(valid_type(type) && cg && READ_ONCE(misc_res_capacity[type])))
154 for (i = cg; i; i = parent_misc(i)) {
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])) {
167 for (j = i; j; j = parent_misc(j)) {
168 atomic_long_inc(&j->res[type].events);
169 cgroup_file_notify(&j->events_file);
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);
177 EXPORT_SYMBOL_GPL(misc_cg_try_charge);
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.
185 * Context: Any context.
187 void misc_cg_uncharge(enum misc_res_type type, struct misc_cg *cg,
188 unsigned long amount)
192 if (!(amount && valid_type(type) && cg))
195 for (i = cg; i; i = parent_misc(i))
196 misc_cg_cancel_charge(type, i, amount);
198 EXPORT_SYMBOL_GPL(misc_cg_uncharge);
201 * misc_cg_max_show() - Show the misc cgroup max limit.
202 * @sf: Interface file
203 * @v: Arguments passed
205 * Context: Any context.
206 * Return: 0 to denote successful print.
208 static int misc_cg_max_show(struct seq_file *sf, void *v)
211 struct misc_cg *cg = css_misc(seq_css(sf));
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);
218 seq_printf(sf, "%s max\n", misc_res_name[i]);
220 seq_printf(sf, "%s %lu\n", misc_res_name[i],
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
233 * @nbytes: Number of bytes of the data.
234 * @off: Offset in the file.
236 * User can pass data like:
237 * echo sev 23 > misc.max, OR
238 * echo sev max > misc.max
240 * Context: Any context.
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.
246 static ssize_t misc_cg_max_write(struct kernfs_open_file *of, char *buf,
247 size_t nbytes, loff_t off)
252 enum misc_res_type type = MISC_CG_RES_TYPES;
256 token = strsep(&buf, " ");
261 for (i = 0; i < MISC_CG_RES_TYPES; i++) {
262 if (!strcmp(misc_res_name[i], token)) {
268 if (type == MISC_CG_RES_TYPES)
271 if (!strcmp(MAX_STR, buf)) {
274 ret = kstrtoul(buf, 0, &max);
279 cg = css_misc(of_css(of));
281 if (READ_ONCE(misc_res_capacity[type]))
282 WRITE_ONCE(cg->res[type].max, max);
286 return ret ? ret : nbytes;
290 * misc_cg_current_show() - Show the current usage of the misc cgroup.
291 * @sf: Interface file
292 * @v: Arguments passed
294 * Context: Any context.
295 * Return: 0 to denote successful print.
297 static int misc_cg_current_show(struct seq_file *sf, void *v)
301 struct misc_cg *cg = css_misc(seq_css(sf));
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);
313 * misc_cg_capacity_show() - Show the total capacity of misc res on the host.
314 * @sf: Interface file
315 * @v: Arguments passed
317 * Only present in the root cgroup directory.
319 * Context: Any context.
320 * Return: 0 to denote successful print.
322 static int misc_cg_capacity_show(struct seq_file *sf, void *v)
327 for (i = 0; i < MISC_CG_RES_TYPES; i++) {
328 cap = READ_ONCE(misc_res_capacity[i]);
330 seq_printf(sf, "%s %lu\n", misc_res_name[i], cap);
336 static int misc_events_show(struct seq_file *sf, void *v)
338 struct misc_cg *cg = css_misc(seq_css(sf));
339 unsigned long events, i;
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);
349 /* Misc cgroup interface files */
350 static struct cftype misc_cg_files[] = {
353 .write = misc_cg_max_write,
354 .seq_show = misc_cg_max_show,
355 .flags = CFTYPE_NOT_ON_ROOT,
359 .seq_show = misc_cg_current_show,
360 .flags = CFTYPE_NOT_ON_ROOT,
364 .seq_show = misc_cg_capacity_show,
365 .flags = CFTYPE_ONLY_ON_ROOT,
369 .flags = CFTYPE_NOT_ON_ROOT,
370 .file_offset = offsetof(struct misc_cg, events_file),
371 .seq_show = misc_events_show,
377 * misc_cg_alloc() - Allocate misc cgroup.
378 * @parent_css: Parent cgroup.
380 * Context: Process context.
382 * * struct cgroup_subsys_state* - css of the allocated cgroup.
383 * * ERR_PTR(-ENOMEM) - No memory available to allocate.
385 static struct cgroup_subsys_state *
386 misc_cg_alloc(struct cgroup_subsys_state *parent_css)
388 enum misc_res_type i;
394 cg = kzalloc(sizeof(*cg), GFP_KERNEL);
396 return ERR_PTR(-ENOMEM);
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);
408 * misc_cg_free() - Free the misc cgroup.
409 * @css: cgroup subsys object.
411 * Context: Any context.
413 static void misc_cg_free(struct cgroup_subsys_state *css)
415 kfree(css_misc(css));
418 /* Cgroup controller callbacks */
419 struct 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,