Skip to content

Commit 55e6be6

Browse files
committed
Merge branch 'for-5.13' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup
Pull cgroup changes from Tejun Heo: "The only notable change is Vipin's new misc cgroup controller. This implements generic support for resources which can be controlled by simply counting and limiting the number of resource instances - ie there's X number of these on the system and this cgroup subtree can have upto Y of those. The first user is the address space IDs used for virtual machine memory encryption and expected future usages are similar - niche hardware features with concrete resource limits and simple usage models" * 'for-5.13' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup: cgroup: use tsk->in_iowait instead of delayacct_is_task_waiting_on_io() cgroup/cpuset: fix typos in comments cgroup: misc: mark dummy misc_cg_res_total_usage() static inline svm/sev: Register SEV and SEV-ES ASIDs to the misc controller cgroup: Miscellaneous cgroup documentation. cgroup: Add misc cgroup controller
2 parents eb6bbac + ffeee41 commit 55e6be6

File tree

12 files changed

+699
-16
lines changed

12 files changed

+699
-16
lines changed

Documentation/admin-guide/cgroup-v1/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Control Groups version 1
1717
hugetlb
1818
memcg_test
1919
memory
20+
misc
2021
net_cls
2122
net_prio
2223
pids
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
===============
2+
Misc controller
3+
===============
4+
Please refer "Misc" documentation in Documentation/admin-guide/cgroup-v2.rst

Documentation/admin-guide/cgroup-v2.rst

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@ v1 is available under :ref:`Documentation/admin-guide/cgroup-v1/index.rst <cgrou
6565
5-7-1. RDMA Interface Files
6666
5-8. HugeTLB
6767
5.8-1. HugeTLB Interface Files
68-
5-8. Misc
69-
5-8-1. perf_event
68+
5-9. Misc
69+
5.9-1 Miscellaneous cgroup Interface Files
70+
5.9-2 Migration and Ownership
71+
5-10. Others
72+
5-10-1. perf_event
7073
5-N. Non-normative information
7174
5-N-1. CPU controller root cgroup process behaviour
7275
5-N-2. IO controller root cgroup process behaviour
@@ -2171,6 +2174,72 @@ HugeTLB Interface Files
21712174
Misc
21722175
----
21732176

2177+
The Miscellaneous cgroup provides the resource limiting and tracking
2178+
mechanism for the scalar resources which cannot be abstracted like the other
2179+
cgroup resources. Controller is enabled by the CONFIG_CGROUP_MISC config
2180+
option.
2181+
2182+
A resource can be added to the controller via enum misc_res_type{} in the
2183+
include/linux/misc_cgroup.h file and the corresponding name via misc_res_name[]
2184+
in the kernel/cgroup/misc.c file. Provider of the resource must set its
2185+
capacity prior to using the resource by calling misc_cg_set_capacity().
2186+
2187+
Once a capacity is set then the resource usage can be updated using charge and
2188+
uncharge APIs. All of the APIs to interact with misc controller are in
2189+
include/linux/misc_cgroup.h.
2190+
2191+
Misc Interface Files
2192+
~~~~~~~~~~~~~~~~~~~~
2193+
2194+
Miscellaneous controller provides 3 interface files. If two misc resources (res_a and res_b) are registered then:
2195+
2196+
misc.capacity
2197+
A read-only flat-keyed file shown only in the root cgroup. It shows
2198+
miscellaneous scalar resources available on the platform along with
2199+
their quantities::
2200+
2201+
$ cat misc.capacity
2202+
res_a 50
2203+
res_b 10
2204+
2205+
misc.current
2206+
A read-only flat-keyed file shown in the non-root cgroups. It shows
2207+
the current usage of the resources in the cgroup and its children.::
2208+
2209+
$ cat misc.current
2210+
res_a 3
2211+
res_b 0
2212+
2213+
misc.max
2214+
A read-write flat-keyed file shown in the non root cgroups. Allowed
2215+
maximum usage of the resources in the cgroup and its children.::
2216+
2217+
$ cat misc.max
2218+
res_a max
2219+
res_b 4
2220+
2221+
Limit can be set by::
2222+
2223+
# echo res_a 1 > misc.max
2224+
2225+
Limit can be set to max by::
2226+
2227+
# echo res_a max > misc.max
2228+
2229+
Limits can be set higher than the capacity value in the misc.capacity
2230+
file.
2231+
2232+
Migration and Ownership
2233+
~~~~~~~~~~~~~~~~~~~~~~~
2234+
2235+
A miscellaneous scalar resource is charged to the cgroup in which it is used
2236+
first, and stays charged to that cgroup until that resource is freed. Migrating
2237+
a process to a different cgroup does not move the charge to the destination
2238+
cgroup where the process has moved.
2239+
2240+
Others
2241+
------
2242+
21742243
perf_event
21752244
~~~~~~~~~~
21762245

arch/x86/kvm/svm/sev.c

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/psp-sev.h>
1515
#include <linux/pagemap.h>
1616
#include <linux/swap.h>
17+
#include <linux/misc_cgroup.h>
1718
#include <linux/processor.h>
1819
#include <linux/trace_events.h>
1920
#include <asm/fpu/internal.h>
@@ -28,6 +29,21 @@
2829

2930
#define __ex(x) __kvm_handle_fault_on_reboot(x)
3031

32+
#ifndef CONFIG_KVM_AMD_SEV
33+
/*
34+
* When this config is not defined, SEV feature is not supported and APIs in
35+
* this file are not used but this file still gets compiled into the KVM AMD
36+
* module.
37+
*
38+
* We will not have MISC_CG_RES_SEV and MISC_CG_RES_SEV_ES entries in the enum
39+
* misc_res_type {} defined in linux/misc_cgroup.h.
40+
*
41+
* Below macros allow compilation to succeed.
42+
*/
43+
#define MISC_CG_RES_SEV MISC_CG_RES_TYPES
44+
#define MISC_CG_RES_SEV_ES MISC_CG_RES_TYPES
45+
#endif
46+
3147
static u8 sev_enc_bit;
3248
static int sev_flush_asids(void);
3349
static DECLARE_RWSEM(sev_deactivate_lock);
@@ -89,8 +105,19 @@ static bool __sev_recycle_asids(int min_asid, int max_asid)
89105

90106
static int sev_asid_new(struct kvm_sev_info *sev)
91107
{
92-
int pos, min_asid, max_asid;
108+
int pos, min_asid, max_asid, ret;
93109
bool retry = true;
110+
enum misc_res_type type;
111+
112+
type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
113+
WARN_ON(sev->misc_cg);
114+
sev->misc_cg = get_current_misc_cg();
115+
ret = misc_cg_try_charge(type, sev->misc_cg, 1);
116+
if (ret) {
117+
put_misc_cg(sev->misc_cg);
118+
sev->misc_cg = NULL;
119+
return ret;
120+
}
94121

95122
mutex_lock(&sev_bitmap_lock);
96123

@@ -108,14 +135,20 @@ static int sev_asid_new(struct kvm_sev_info *sev)
108135
goto again;
109136
}
110137
mutex_unlock(&sev_bitmap_lock);
111-
return -EBUSY;
138+
ret = -EBUSY;
139+
goto e_uncharge;
112140
}
113141

114142
__set_bit(pos, sev_asid_bitmap);
115143

116144
mutex_unlock(&sev_bitmap_lock);
117145

118146
return pos + 1;
147+
e_uncharge:
148+
misc_cg_uncharge(type, sev->misc_cg, 1);
149+
put_misc_cg(sev->misc_cg);
150+
sev->misc_cg = NULL;
151+
return ret;
119152
}
120153

121154
static int sev_get_asid(struct kvm *kvm)
@@ -125,14 +158,15 @@ static int sev_get_asid(struct kvm *kvm)
125158
return sev->asid;
126159
}
127160

128-
static void sev_asid_free(int asid)
161+
static void sev_asid_free(struct kvm_sev_info *sev)
129162
{
130163
struct svm_cpu_data *sd;
131164
int cpu, pos;
165+
enum misc_res_type type;
132166

133167
mutex_lock(&sev_bitmap_lock);
134168

135-
pos = asid - 1;
169+
pos = sev->asid - 1;
136170
__set_bit(pos, sev_reclaim_asid_bitmap);
137171

138172
for_each_possible_cpu(cpu) {
@@ -141,6 +175,11 @@ static void sev_asid_free(int asid)
141175
}
142176

143177
mutex_unlock(&sev_bitmap_lock);
178+
179+
type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
180+
misc_cg_uncharge(type, sev->misc_cg, 1);
181+
put_misc_cg(sev->misc_cg);
182+
sev->misc_cg = NULL;
144183
}
145184

146185
static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
@@ -188,19 +227,20 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
188227
asid = sev_asid_new(sev);
189228
if (asid < 0)
190229
return ret;
230+
sev->asid = asid;
191231

192232
ret = sev_platform_init(&argp->error);
193233
if (ret)
194234
goto e_free;
195235

196236
sev->active = true;
197-
sev->asid = asid;
198237
INIT_LIST_HEAD(&sev->regions_list);
199238

200239
return 0;
201240

202241
e_free:
203-
sev_asid_free(asid);
242+
sev_asid_free(sev);
243+
sev->asid = 0;
204244
return ret;
205245
}
206246

@@ -1315,12 +1355,12 @@ void sev_vm_destroy(struct kvm *kvm)
13151355
mutex_unlock(&kvm->lock);
13161356

13171357
sev_unbind_asid(kvm, sev->handle);
1318-
sev_asid_free(sev->asid);
1358+
sev_asid_free(sev);
13191359
}
13201360

13211361
void __init sev_hardware_setup(void)
13221362
{
1323-
unsigned int eax, ebx, ecx, edx;
1363+
unsigned int eax, ebx, ecx, edx, sev_asid_count, sev_es_asid_count;
13241364
bool sev_es_supported = false;
13251365
bool sev_supported = false;
13261366

@@ -1352,7 +1392,11 @@ void __init sev_hardware_setup(void)
13521392
if (!sev_reclaim_asid_bitmap)
13531393
goto out;
13541394

1355-
pr_info("SEV supported: %u ASIDs\n", max_sev_asid - min_sev_asid + 1);
1395+
sev_asid_count = max_sev_asid - min_sev_asid + 1;
1396+
if (misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count))
1397+
goto out;
1398+
1399+
pr_info("SEV supported: %u ASIDs\n", sev_asid_count);
13561400
sev_supported = true;
13571401

13581402
/* SEV-ES support requested? */
@@ -1367,7 +1411,11 @@ void __init sev_hardware_setup(void)
13671411
if (min_sev_asid == 1)
13681412
goto out;
13691413

1370-
pr_info("SEV-ES supported: %u ASIDs\n", min_sev_asid - 1);
1414+
sev_es_asid_count = min_sev_asid - 1;
1415+
if (misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count))
1416+
goto out;
1417+
1418+
pr_info("SEV-ES supported: %u ASIDs\n", sev_es_asid_count);
13711419
sev_es_supported = true;
13721420

13731421
out:
@@ -1382,6 +1430,8 @@ void sev_hardware_teardown(void)
13821430

13831431
bitmap_free(sev_asid_bitmap);
13841432
bitmap_free(sev_reclaim_asid_bitmap);
1433+
misc_cg_set_capacity(MISC_CG_RES_SEV, 0);
1434+
misc_cg_set_capacity(MISC_CG_RES_SEV_ES, 0);
13851435

13861436
sev_flush_asids();
13871437
}

arch/x86/kvm/svm/svm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ struct kvm_sev_info {
6565
unsigned long pages_locked; /* Number of pages locked */
6666
struct list_head regions_list; /* List of registered regions */
6767
u64 ap_jump_table; /* SEV-ES AP Jump Table address */
68+
struct misc_cg *misc_cg; /* For misc cgroup accounting */
6869
};
6970

7071
struct kvm_svm {

include/linux/cgroup_subsys.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ SUBSYS(pids)
6161
SUBSYS(rdma)
6262
#endif
6363

64+
#if IS_ENABLED(CONFIG_CGROUP_MISC)
65+
SUBSYS(misc)
66+
#endif
67+
6468
/*
6569
* The following subsystems are not supported on the default hierarchy.
6670
*/

0 commit comments

Comments
 (0)