Commit | Line | Data |
---|---|---|
a72232ea VS |
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 | /** | |
7a447968 | 12 | * enum misc_res_type - Types of misc cgroup entries supported by the host. |
a72232ea VS |
13 | */ |
14 | enum misc_res_type { | |
7aef27f0 | 15 | #ifdef CONFIG_KVM_AMD_SEV |
7a447968 | 16 | /** @MISC_CG_RES_SEV: AMD SEV ASIDs resource */ |
7aef27f0 | 17 | MISC_CG_RES_SEV, |
7a447968 | 18 | /** @MISC_CG_RES_SEV_ES: AMD SEV-ES ASIDs resource */ |
7aef27f0 | 19 | MISC_CG_RES_SEV_ES, |
7c035bea ZH |
20 | #endif |
21 | #ifdef CONFIG_INTEL_TDX_HOST | |
22 | /* Intel TDX HKIDs resource */ | |
23 | MISC_CG_RES_TDX, | |
7aef27f0 | 24 | #endif |
7a447968 | 25 | /** @MISC_CG_RES_TYPES: count of enum misc_res_type constants */ |
a72232ea VS |
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. | |
1028f391 | 38 | * @watermark: Historical maximum usage of the resource. |
a72232ea | 39 | * @usage: Current usage of the resource. |
62157e11 | 40 | * @events: Number of times, the resource limit exceeded. |
a72232ea VS |
41 | */ |
42 | struct misc_res { | |
32bf85c6 | 43 | u64 max; |
1028f391 | 44 | atomic64_t watermark; |
32bf85c6 HH |
45 | atomic64_t usage; |
46 | atomic64_t events; | |
6a26f9c6 | 47 | atomic64_t events_local; |
a72232ea VS |
48 | }; |
49 | ||
50 | /** | |
51 | * struct misc_cg - Miscellaneous controller's cgroup structure. | |
52 | * @css: cgroup subsys state object. | |
62157e11 | 53 | * @events_file: Handle for the misc resources events file. |
a72232ea VS |
54 | * @res: Array of misc resources usage in the cgroup. |
55 | */ | |
56 | struct misc_cg { | |
57 | struct cgroup_subsys_state css; | |
f279294b CX |
58 | |
59 | /* misc.events */ | |
60 | struct cgroup_file events_file; | |
6a26f9c6 XJ |
61 | /* misc.events.local */ |
62 | struct cgroup_file events_local_file; | |
f279294b | 63 | |
a72232ea VS |
64 | struct misc_res res[MISC_CG_RES_TYPES]; |
65 | }; | |
66 | ||
32bf85c6 HH |
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); | |
a72232ea VS |
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 | */ | |
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 | */ | |
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 | */ | |
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 | ||
32bf85c6 | 110 | static inline int misc_cg_set_capacity(enum misc_res_type type, u64 capacity) |
a72232ea VS |
111 | { |
112 | return 0; | |
113 | } | |
114 | ||
115 | static inline int misc_cg_try_charge(enum misc_res_type type, | |
116 | struct misc_cg *cg, | |
32bf85c6 | 117 | u64 amount) |
a72232ea VS |
118 | { |
119 | return 0; | |
120 | } | |
121 | ||
122 | static inline void misc_cg_uncharge(enum misc_res_type type, | |
123 | struct misc_cg *cg, | |
32bf85c6 | 124 | u64 amount) |
a72232ea VS |
125 | { |
126 | } | |
127 | ||
128 | static inline struct misc_cg *get_current_misc_cg(void) | |
129 | { | |
130 | return NULL; | |
131 | } | |
132 | ||
133 | static inline void put_misc_cg(struct misc_cg *cg) | |
134 | { | |
135 | } | |
136 | ||
137 | #endif /* CONFIG_CGROUP_MISC */ | |
138 | #endif /* _MISC_CGROUP_H_ */ |