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