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 U64_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 u64 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 u64
misc_cg_res_total_usage(enum misc_res_type type
)
80 return atomic64_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
, u64 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
,
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
]);
124 static void misc_cg_update_watermark(struct misc_res
*res
, u64 new_usage
)
129 old
= atomic64_read(&res
->watermark
);
130 if (new_usage
<= old
)
132 if (atomic64_cmpxchg(&res
->watermark
, old
, new_usage
) == old
)
137 static void misc_cg_event(enum misc_res_type type
, struct misc_cg
*cg
)
139 atomic64_inc(&cg
->res
[type
].events_local
);
140 cgroup_file_notify(&cg
->events_local_file
);
142 for (; parent_misc(cg
); cg
= parent_misc(cg
)) {
143 atomic64_inc(&cg
->res
[type
].events
);
144 cgroup_file_notify(&cg
->events_file
);
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.
154 * Charge @amount to the misc cgroup. Caller must use the same cgroup during
157 * Context: Any context.
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
164 int misc_cg_try_charge(enum misc_res_type type
, struct misc_cg
*cg
, u64 amount
)
166 struct misc_cg
*i
, *j
;
168 struct misc_res
*res
;
171 if (!(valid_type(type
) && cg
&& READ_ONCE(misc_res_capacity
[type
])))
177 for (i
= cg
; i
; i
= parent_misc(i
)) {
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
])) {
186 misc_cg_update_watermark(res
, new_usage
);
191 misc_cg_event(type
, i
);
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
);
198 EXPORT_SYMBOL_GPL(misc_cg_try_charge
);
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.
206 * Context: Any context.
208 void misc_cg_uncharge(enum misc_res_type type
, struct misc_cg
*cg
, u64 amount
)
212 if (!(amount
&& valid_type(type
) && cg
))
215 for (i
= cg
; i
; i
= parent_misc(i
))
216 misc_cg_cancel_charge(type
, i
, amount
);
218 EXPORT_SYMBOL_GPL(misc_cg_uncharge
);
221 * misc_cg_max_show() - Show the misc cgroup max limit.
222 * @sf: Interface file
223 * @v: Arguments passed
225 * Context: Any context.
226 * Return: 0 to denote successful print.
228 static int misc_cg_max_show(struct seq_file
*sf
, void *v
)
231 struct misc_cg
*cg
= css_misc(seq_css(sf
));
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
);
238 seq_printf(sf
, "%s max\n", misc_res_name
[i
]);
240 seq_printf(sf
, "%s %llu\n", misc_res_name
[i
],
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
253 * @nbytes: Number of bytes of the data.
254 * @off: Offset in the file.
256 * User can pass data like:
257 * echo sev 23 > misc.max, OR
258 * echo sev max > misc.max
260 * Context: Any context.
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.
266 static ssize_t
misc_cg_max_write(struct kernfs_open_file
*of
, char *buf
,
267 size_t nbytes
, loff_t off
)
272 enum misc_res_type type
= MISC_CG_RES_TYPES
;
276 token
= strsep(&buf
, " ");
281 for (i
= 0; i
< MISC_CG_RES_TYPES
; i
++) {
282 if (!strcmp(misc_res_name
[i
], token
)) {
288 if (type
== MISC_CG_RES_TYPES
)
291 if (!strcmp(MAX_STR
, buf
)) {
294 ret
= kstrtou64(buf
, 0, &max
);
299 cg
= css_misc(of_css(of
));
301 if (READ_ONCE(misc_res_capacity
[type
]))
302 WRITE_ONCE(cg
->res
[type
].max
, max
);
306 return ret
? ret
: nbytes
;
310 * misc_cg_current_show() - Show the current usage of the misc cgroup.
311 * @sf: Interface file
312 * @v: Arguments passed
314 * Context: Any context.
315 * Return: 0 to denote successful print.
317 static int misc_cg_current_show(struct seq_file
*sf
, void *v
)
321 struct misc_cg
*cg
= css_misc(seq_css(sf
));
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
);
333 * misc_cg_peak_show() - Show the peak usage of the misc cgroup.
334 * @sf: Interface file
335 * @v: Arguments passed
337 * Context: Any context.
338 * Return: 0 to denote successful print.
340 static int misc_cg_peak_show(struct seq_file
*sf
, void *v
)
344 struct misc_cg
*cg
= css_misc(seq_css(sf
));
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
);
356 * misc_cg_capacity_show() - Show the total capacity of misc res on the host.
357 * @sf: Interface file
358 * @v: Arguments passed
360 * Only present in the root cgroup directory.
362 * Context: Any context.
363 * Return: 0 to denote successful print.
365 static int misc_cg_capacity_show(struct seq_file
*sf
, void *v
)
370 for (i
= 0; i
< MISC_CG_RES_TYPES
; i
++) {
371 cap
= READ_ONCE(misc_res_capacity
[i
]);
373 seq_printf(sf
, "%s %llu\n", misc_res_name
[i
], cap
);
379 static int __misc_events_show(struct seq_file
*sf
, bool local
)
381 struct misc_cg
*cg
= css_misc(seq_css(sf
));
385 for (i
= 0; i
< MISC_CG_RES_TYPES
; i
++) {
387 events
= atomic64_read(&cg
->res
[i
].events_local
);
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
);
396 static int misc_events_show(struct seq_file
*sf
, void *v
)
398 return __misc_events_show(sf
, false);
401 static int misc_events_local_show(struct seq_file
*sf
, void *v
)
403 return __misc_events_show(sf
, true);
406 /* Misc cgroup interface files */
407 static struct cftype misc_cg_files
[] = {
410 .write
= misc_cg_max_write
,
411 .seq_show
= misc_cg_max_show
,
412 .flags
= CFTYPE_NOT_ON_ROOT
,
416 .seq_show
= misc_cg_current_show
,
420 .seq_show
= misc_cg_peak_show
,
424 .seq_show
= misc_cg_capacity_show
,
425 .flags
= CFTYPE_ONLY_ON_ROOT
,
429 .flags
= CFTYPE_NOT_ON_ROOT
,
430 .file_offset
= offsetof(struct misc_cg
, events_file
),
431 .seq_show
= misc_events_show
,
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
,
443 * misc_cg_alloc() - Allocate misc cgroup.
444 * @parent_css: Parent cgroup.
446 * Context: Process context.
448 * * struct cgroup_subsys_state* - css of the allocated cgroup.
449 * * ERR_PTR(-ENOMEM) - No memory available to allocate.
451 static struct cgroup_subsys_state
*
452 misc_cg_alloc(struct cgroup_subsys_state
*parent_css
)
454 enum misc_res_type i
;
460 cg
= kzalloc(sizeof(*cg
), GFP_KERNEL
);
462 return ERR_PTR(-ENOMEM
);
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);
474 * misc_cg_free() - Free the misc cgroup.
475 * @css: cgroup subsys object.
477 * Context: Any context.
479 static void misc_cg_free(struct cgroup_subsys_state
*css
)
481 kfree(css_misc(css
));
484 /* Cgroup controller callbacks */
485 struct 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
,