xref: /openbmc/linux/include/linux/misc_cgroup.h (revision 32bf85c6)
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 #ifndef _MISC_CGROUP_H_
9 #define _MISC_CGROUP_H_
10 
11 /**
12  * Types of misc cgroup entries supported by the host.
13  */
14 enum misc_res_type {
15 #ifdef CONFIG_KVM_AMD_SEV
16 	/* AMD SEV ASIDs resource */
17 	MISC_CG_RES_SEV,
18 	/* AMD SEV-ES ASIDs resource */
19 	MISC_CG_RES_SEV_ES,
20 #endif
21 	MISC_CG_RES_TYPES
22 };
23 
24 struct misc_cg;
25 
26 #ifdef CONFIG_CGROUP_MISC
27 
28 #include <linux/cgroup.h>
29 
30 /**
31  * struct misc_res: Per cgroup per misc type resource
32  * @max: Maximum limit on the resource.
33  * @usage: Current usage of the resource.
34  * @events: Number of times, the resource limit exceeded.
35  */
36 struct misc_res {
37 	u64 max;
38 	atomic64_t usage;
39 	atomic64_t events;
40 };
41 
42 /**
43  * struct misc_cg - Miscellaneous controller's cgroup structure.
44  * @css: cgroup subsys state object.
45  * @events_file: Handle for the misc resources events file.
46  * @res: Array of misc resources usage in the cgroup.
47  */
48 struct misc_cg {
49 	struct cgroup_subsys_state css;
50 
51 	/* misc.events */
52 	struct cgroup_file events_file;
53 
54 	struct misc_res res[MISC_CG_RES_TYPES];
55 };
56 
57 u64 misc_cg_res_total_usage(enum misc_res_type type);
58 int misc_cg_set_capacity(enum misc_res_type type, u64 capacity);
59 int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg, u64 amount);
60 void misc_cg_uncharge(enum misc_res_type type, struct misc_cg *cg, u64 amount);
61 
62 /**
63  * css_misc() - Get misc cgroup from the css.
64  * @css: cgroup subsys state object.
65  *
66  * Context: Any context.
67  * Return:
68  * * %NULL - If @css is null.
69  * * struct misc_cg* - misc cgroup pointer of the passed css.
70  */
css_misc(struct cgroup_subsys_state * css)71 static inline struct misc_cg *css_misc(struct cgroup_subsys_state *css)
72 {
73 	return css ? container_of(css, struct misc_cg, css) : NULL;
74 }
75 
76 /*
77  * get_current_misc_cg() - Find and get the misc cgroup of the current task.
78  *
79  * Returned cgroup has its ref count increased by 1. Caller must call
80  * put_misc_cg() to return the reference.
81  *
82  * Return: Misc cgroup to which the current task belongs to.
83  */
get_current_misc_cg(void)84 static inline struct misc_cg *get_current_misc_cg(void)
85 {
86 	return css_misc(task_get_css(current, misc_cgrp_id));
87 }
88 
89 /*
90  * put_misc_cg() - Put the misc cgroup and reduce its ref count.
91  * @cg - cgroup to put.
92  */
put_misc_cg(struct misc_cg * cg)93 static inline void put_misc_cg(struct misc_cg *cg)
94 {
95 	if (cg)
96 		css_put(&cg->css);
97 }
98 
99 #else /* !CONFIG_CGROUP_MISC */
100 
misc_cg_res_total_usage(enum misc_res_type type)101 static inline u64 misc_cg_res_total_usage(enum misc_res_type type)
102 {
103 	return 0;
104 }
105 
misc_cg_set_capacity(enum misc_res_type type,u64 capacity)106 static inline int misc_cg_set_capacity(enum misc_res_type type, u64 capacity)
107 {
108 	return 0;
109 }
110 
misc_cg_try_charge(enum misc_res_type type,struct misc_cg * cg,u64 amount)111 static inline int misc_cg_try_charge(enum misc_res_type type,
112 				     struct misc_cg *cg,
113 				     u64 amount)
114 {
115 	return 0;
116 }
117 
misc_cg_uncharge(enum misc_res_type type,struct misc_cg * cg,u64 amount)118 static inline void misc_cg_uncharge(enum misc_res_type type,
119 				    struct misc_cg *cg,
120 				    u64 amount)
121 {
122 }
123 
get_current_misc_cg(void)124 static inline struct misc_cg *get_current_misc_cg(void)
125 {
126 	return NULL;
127 }
128 
put_misc_cg(struct misc_cg * cg)129 static inline void put_misc_cg(struct misc_cg *cg)
130 {
131 }
132 
133 #endif /* CONFIG_CGROUP_MISC */
134 #endif /* _MISC_CGROUP_H_ */
135