xref: /linux/include/linux/misc_cgroup.h (revision 0be3ff0c)
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  * @failed: True if charged failed for the resource in a cgroup.
35  */
36 struct misc_res {
37 	unsigned long max;
38 	atomic_long_t usage;
39 	atomic_long_t events;
40 };
41 
42 /**
43  * struct misc_cg - Miscellaneous controller's cgroup structure.
44  * @css: cgroup subsys state object.
45  * @res: Array of misc resources usage in the cgroup.
46  */
47 struct misc_cg {
48 	struct cgroup_subsys_state css;
49 
50 	/* misc.events */
51 	struct cgroup_file events_file;
52 
53 	struct misc_res res[MISC_CG_RES_TYPES];
54 };
55 
56 unsigned long misc_cg_res_total_usage(enum misc_res_type type);
57 int misc_cg_set_capacity(enum misc_res_type type, unsigned long capacity);
58 int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg,
59 		       unsigned long amount);
60 void misc_cg_uncharge(enum misc_res_type type, struct misc_cg *cg,
61 		      unsigned long amount);
62 
63 /**
64  * css_misc() - Get misc cgroup from the css.
65  * @css: cgroup subsys state object.
66  *
67  * Context: Any context.
68  * Return:
69  * * %NULL - If @css is null.
70  * * struct misc_cg* - misc cgroup pointer of the passed css.
71  */
72 static inline struct misc_cg *css_misc(struct cgroup_subsys_state *css)
73 {
74 	return css ? container_of(css, struct misc_cg, css) : NULL;
75 }
76 
77 /*
78  * get_current_misc_cg() - Find and get the misc cgroup of the current task.
79  *
80  * Returned cgroup has its ref count increased by 1. Caller must call
81  * put_misc_cg() to return the reference.
82  *
83  * Return: Misc cgroup to which the current task belongs to.
84  */
85 static inline struct misc_cg *get_current_misc_cg(void)
86 {
87 	return css_misc(task_get_css(current, misc_cgrp_id));
88 }
89 
90 /*
91  * put_misc_cg() - Put the misc cgroup and reduce its ref count.
92  * @cg - cgroup to put.
93  */
94 static inline void put_misc_cg(struct misc_cg *cg)
95 {
96 	if (cg)
97 		css_put(&cg->css);
98 }
99 
100 #else /* !CONFIG_CGROUP_MISC */
101 
102 static inline unsigned long misc_cg_res_total_usage(enum misc_res_type type)
103 {
104 	return 0;
105 }
106 
107 static inline int misc_cg_set_capacity(enum misc_res_type type,
108 				       unsigned long capacity)
109 {
110 	return 0;
111 }
112 
113 static inline int misc_cg_try_charge(enum misc_res_type type,
114 				     struct misc_cg *cg,
115 				     unsigned long amount)
116 {
117 	return 0;
118 }
119 
120 static inline void misc_cg_uncharge(enum misc_res_type type,
121 				    struct misc_cg *cg,
122 				    unsigned long amount)
123 {
124 }
125 
126 static inline struct misc_cg *get_current_misc_cg(void)
127 {
128 	return NULL;
129 }
130 
131 static inline void put_misc_cg(struct misc_cg *cg)
132 {
133 }
134 
135 #endif /* CONFIG_CGROUP_MISC */
136 #endif /* _MISC_CGROUP_H_ */
137