xref: /linux-6.15/include/linux/misc_cgroup.h (revision a72232ea)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Miscellaneous cgroup controller.
4  *
5  * Copyright 2020 Google LLC
6  * Author: Vipin Sharma <[email protected]>
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 	MISC_CG_RES_TYPES
16 };
17 
18 struct misc_cg;
19 
20 #ifdef CONFIG_CGROUP_MISC
21 
22 #include <linux/cgroup.h>
23 
24 /**
25  * struct misc_res: Per cgroup per misc type resource
26  * @max: Maximum limit on the resource.
27  * @usage: Current usage of the resource.
28  * @failed: True if charged failed for the resource in a cgroup.
29  */
30 struct misc_res {
31 	unsigned long max;
32 	atomic_long_t usage;
33 	bool failed;
34 };
35 
36 /**
37  * struct misc_cg - Miscellaneous controller's cgroup structure.
38  * @css: cgroup subsys state object.
39  * @res: Array of misc resources usage in the cgroup.
40  */
41 struct misc_cg {
42 	struct cgroup_subsys_state css;
43 	struct misc_res res[MISC_CG_RES_TYPES];
44 };
45 
46 unsigned long misc_cg_res_total_usage(enum misc_res_type type);
47 int misc_cg_set_capacity(enum misc_res_type type, unsigned long capacity);
48 int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg,
49 		       unsigned long amount);
50 void misc_cg_uncharge(enum misc_res_type type, struct misc_cg *cg,
51 		      unsigned long amount);
52 
53 /**
54  * css_misc() - Get misc cgroup from the css.
55  * @css: cgroup subsys state object.
56  *
57  * Context: Any context.
58  * Return:
59  * * %NULL - If @css is null.
60  * * struct misc_cg* - misc cgroup pointer of the passed css.
61  */
62 static inline struct misc_cg *css_misc(struct cgroup_subsys_state *css)
63 {
64 	return css ? container_of(css, struct misc_cg, css) : NULL;
65 }
66 
67 /*
68  * get_current_misc_cg() - Find and get the misc cgroup of the current task.
69  *
70  * Returned cgroup has its ref count increased by 1. Caller must call
71  * put_misc_cg() to return the reference.
72  *
73  * Return: Misc cgroup to which the current task belongs to.
74  */
75 static inline struct misc_cg *get_current_misc_cg(void)
76 {
77 	return css_misc(task_get_css(current, misc_cgrp_id));
78 }
79 
80 /*
81  * put_misc_cg() - Put the misc cgroup and reduce its ref count.
82  * @cg - cgroup to put.
83  */
84 static inline void put_misc_cg(struct misc_cg *cg)
85 {
86 	if (cg)
87 		css_put(&cg->css);
88 }
89 
90 #else /* !CONFIG_CGROUP_MISC */
91 
92 unsigned long misc_cg_res_total_usage(enum misc_res_type type)
93 {
94 	return 0;
95 }
96 
97 static inline int misc_cg_set_capacity(enum misc_res_type type,
98 				       unsigned long capacity)
99 {
100 	return 0;
101 }
102 
103 static inline int misc_cg_try_charge(enum misc_res_type type,
104 				     struct misc_cg *cg,
105 				     unsigned long amount)
106 {
107 	return 0;
108 }
109 
110 static inline void misc_cg_uncharge(enum misc_res_type type,
111 				    struct misc_cg *cg,
112 				    unsigned long amount)
113 {
114 }
115 
116 static inline struct misc_cg *get_current_misc_cg(void)
117 {
118 	return NULL;
119 }
120 
121 static inline void put_misc_cg(struct misc_cg *cg)
122 {
123 }
124 
125 #endif /* CONFIG_CGROUP_MISC */
126 #endif /* _MISC_CGROUP_H_ */
127