cgroup: KVM: New Encryption IDs cgroup controller
Subsystems (5)
v5 cover cgroup: KVM: New Encryption IDs cgroup controller
Hello,
This patch adds a new cgroup controller, Encryption IDs, to track and limit the usage of encryption IDs on a host.
AMD provides Secure Encrypted Virtualization (SEV) and SEV with Encrypted State (SEV-ES) to encrypt the guest OS's memory using limited number of Address Space Identifiers (ASIDs).
This limited number of ASIDs creates issues like SEV ASID starvation and unoptimized scheduling in the cloud infrastucture.
In the RFC patch v1, I provided only SEV cgroup controller but based on the feedback and discussion it became clear that this cgroup controller can be extended to be used by Intel's Trusted Domain Extension (TDX) and s390's protected virtualization Secure Execution IDs (SEID)
This patch series provides a generic Encryption IDs controller with tracking support of the SEV and SEV-ES ASIDs.
Changes in v5:
- Changed controller filenames from encryption_ids.*.* to encids.*.*
- Documentation of cgroup v1 now points to cgroup v2.
Changes in v4:
- The max value can be set lower than the current.
- Added SEV-ES support.
Changes in v3:
- Fixes a build error when CONFIG_CGROUP is disabled.
Changes in v2:
- Changed cgroup name from sev to encryption_ids.
- Replaced SEV specific names in APIs and documentations with generic encryption IDs.
- Providing 3 cgroup files per encryption ID type. For example in SEV,
- encryption_ids.sev.stat (only in the root cgroup directory).
- encryption_ids.sev.max
- encryption_ids.sev.current
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/lkml/[email protected]/
[4] https://lore.kernel.org/lkml/[email protected]/
Vipin Sharma (2):
cgroup: svm: Add Encryption ID controller
cgroup: svm: Encryption IDs cgroup documentation.
.../admin-guide/cgroup-v1/encryption_ids.rst | 1 + Documentation/admin-guide/cgroup-v2.rst | 78 +++- arch/x86/kvm/svm/sev.c | 52 ++- include/linux/cgroup_subsys.h | 4 + include/linux/encryption_ids_cgroup.h | 72 +++ include/linux/kvm_host.h | 4 + init/Kconfig | 14 + kernel/cgroup/Makefile | 1 + kernel/cgroup/encryption_ids.c | 421 ++++++++++++++++++ 9 files changed, 633 insertions(+), 14 deletions(-) create mode 100644 Documentation/admin-guide/cgroup-v1/encryption_ids.rst create mode 100644 include/linux/encryption_ids_cgroup.h create mode 100644 kernel/cgroup/encryption_ids.c
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
PATCH v5 1/2 cgroup: svm: Add Encryption ID controller
Hardware memory encryption is available on multiple generic CPUs. For example AMD has Secure Encrypted Virtualization (SEV) and SEV - Encrypted State (SEV-ES).
These memory encryptions are useful in creating encrypted virtual machines (VMs) and user space programs.
There are limited number of encryption IDs that can be used simultaneously on a machine for encryption. This generates a need for the system admin to track, limit, allocate resources, and optimally schedule VMs and user workloads in the cloud infrastructure. Some malicious programs can exhaust all of these resources on a host causing starvation of other workloads.
Encryption ID controller allows control of these resources using Cgroups.
Controller is enabled by CGROUP_ENCRYPTION_IDS config option. Encryption controller provide 3 interface files for each encryption ID type. For example, in SEV:
1. encids.sev.max
Sets the maximum usage of SEV IDs in the cgroup.
2. encids.sev.current
Current usage of SEV IDs in the cgroup and its children.
3. encids.sev.stat
Shown only at the root cgroup. Displays total SEV IDs available on the platform and current usage count.
--- arch/x86/kvm/svm/sev.c | 52 +++- include/linux/cgroup_subsys.h | 4 + include/linux/encryption_ids_cgroup.h | 72 +++++ include/linux/kvm_host.h | 4 + init/Kconfig | 14 + kernel/cgroup/Makefile | 1 + kernel/cgroup/encryption_ids.c | 421 ++++++++++++++++++++++++++ 7 files changed, 556 insertions(+), 12 deletions(-) create mode 100644 include/linux/encryption_ids_cgroup.h create mode 100644 kernel/cgroup/encryption_ids.c
arch/x86/kvm/svm/sev.c +40 -12
@@ -14,6 +14,7 @@#include <linux/psp-sev.h> #include <linux/pagemap.h> #include <linux/swap.h> +#include <linux/encryption_ids_cgroup.h> #include <linux/processor.h> #include <linux/trace_events.h> #include <asm/fpu/internal.h>
@@ -86,10 +87,18 @@ static bool __sev_recycle_asids(int min_asid, int max_asid)return true; } -static int sev_asid_new (struct kvm_sev_info *sev) +static int sev_asid_new (struct kvm *kvm) { -int pos, min_asid, max_asid; +int pos, min_asid, max_asid, ret; +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;bool retry = true; +enum encryption_id_type type; + + type = sev->es_active ?ENCRYPTION_ID_SEV_ES :ENCRYPTION_ID_SEV ; + + ret =enc_id_cg_try_charge (kvm, type,1 ); +if (ret) +return ret;mutex_lock (&sev_bitmap_lock);
@@ -107,7 +116,8 @@ static int sev_asid_new(struct kvm_sev_info *sev) goto again; }mutex_unlock (&sev_bitmap_lock); -return -EBUSY ; + ret = -EBUSY ; + goto e_uncharge; }__set_bit (pos, sev_asid_bitmap);
@@ -115,6 +125,9 @@ static int sev_asid_new(struct kvm_sev_info *sev)mutex_unlock (&sev_bitmap_lock);return pos +1 ; +e_uncharge: +enc_id_cg_uncharge (kvm, type,1 ); +return ret; }static int sev_get_asid (struct kvm *kvm)
@@ -124,14 +137,16 @@ static int sev_get_asid(struct kvm *kvm)return sev->asid; } -static void sev_asid_free (int asid) +static void sev_asid_free (struct kvm *kvm) {struct svm_cpu_data *sd;int cpu, pos; +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +enum encryption_id_type type;mutex_lock (&sev_bitmap_lock); - pos = asid -1 ; + pos = sev->asid -1 ;__set_bit (pos, sev_reclaim_asid_bitmap);for_each_possible_cpu (cpu) {
@@ -140,6 +155,9 @@ static void sev_asid_free(int asid) }mutex_unlock (&sev_bitmap_lock); + + type = sev->es_active ?ENCRYPTION_ID_SEV_ES :ENCRYPTION_ID_SEV ; +enc_id_cg_uncharge (kvm, type,1 ); }static void sev_unbind_asid (struct kvm *kvm,unsigned int handle)
@@ -184,22 +202,22 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)if (unlikely (sev->active))return ret; - asid =sev_asid_new (sev); + asid =sev_asid_new (kvm);if (asid <0 )return ret; + sev->asid = asid; ret =sev_platform_init (&argp->error);if (ret) goto e_free; sev->active = true; - sev->asid = asid;INIT_LIST_HEAD (&sev->regions_list);return 0 ; e_free: -sev_asid_free (asid); +sev_asid_free (kvm);return ret; }
@@ -1240,12 +1258,12 @@ void sev_vm_destroy(struct kvm *kvm)mutex_unlock (&kvm->lock);sev_unbind_asid (kvm, sev->handle); -sev_asid_free (sev->asid); +sev_asid_free (kvm); }void __init sev_hardware_setup (void ) { -unsigned int eax, ebx, ecx, edx; +unsigned int eax, ebx, ecx, edx, sev_asid_count, sev_es_asid_count;bool sev_es_supported = false;bool sev_supported = false;
@@ -1277,7 +1295,11 @@ void __init sev_hardware_setup(void)if (!sev_reclaim_asid_bitmap) goto out; -pr_info ("SEV supported: %u ASIDs\n" , max_sev_asid - min_sev_asid +1 ); + sev_asid_count = max_sev_asid - min_sev_asid +1 ; +if (enc_id_cg_set_capacity (ENCRYPTION_ID_SEV , sev_asid_count)) + goto out; + +pr_info ("SEV supported: %u ASIDs\n" , sev_asid_count); sev_supported = true;/* SEV-ES support requested? */
@@ -1292,7 +1314,11 @@ void __init sev_hardware_setup(void)if (min_sev_asid ==1 ) goto out; -pr_info ("SEV-ES supported: %u ASIDs\n" , min_sev_asid -1 ); + sev_es_asid_count = min_sev_asid -1 ; +if (enc_id_cg_set_capacity (ENCRYPTION_ID_SEV_ES , sev_es_asid_count)) + goto out; + +pr_info ("SEV-ES supported: %u ASIDs\n" , sev_es_asid_count); sev_es_supported = true; out:
@@ -1307,6 +1333,8 @@ void sev_hardware_teardown(void)bitmap_free (sev_asid_bitmap);bitmap_free (sev_reclaim_asid_bitmap); +enc_id_cg_set_capacity (ENCRYPTION_ID_SEV ,0 ); +enc_id_cg_set_capacity (ENCRYPTION_ID_SEV_ES ,0 );sev_flush_asids (); }
include/linux/cgroup_subsys.h +4 -0
@@ -61,6 +61,10 @@ SUBSYS(pids) SUBSYS(rdma)#endif +#if IS_ENABLED (CONFIG_CGROUP_ENCRYPTION_IDS ) +SUBSYS(encids) +#endif +/* * The following subsystems are not supported on the default hierarchy. */
include/linux/encryption_ids_cgroup.h new file +72 -0
72 lines not on this page · raw message
include/linux/kvm_host.h +4 -0
@@ -27,6 +27,7 @@#include <linux/refcount.h> #include <linux/nospec.h> #include <asm/signal.h> +#include <linux/encryption_ids_cgroup.h> #include <linux/kvm.h> #include <linux/kvm_para.h>
@@ -513,6 +514,9 @@ struct kvm {pid_t userspace_pid;unsigned int max_halt_poll_ns;u32 dirty_ring_size; +#ifdef CONFIG_CGROUP_ENCRYPTION_IDS +struct encryption_id_cgroup *enc_id_cg; +#endif };#define kvm_err (fmt, ...) \
init/Kconfig +14 -0
@@ -1106,6 +1106,20 @@ config CGROUP_BPF BPF_CGROUP_INET_INGRESS will be executed on the ingress path of inet sockets. +config CGROUP_ENCRYPTION_IDS + bool "Encryption IDs controller" + depends on KVM_AMD_SEV + default n + help + Provides a controller for CPU encryption IDs on a host. + + Some platforms have limited number of encryption IDs which can be + used simultaneously, e.g., AMD's Secure Encrypted Virtualization + (SEV). This controller tracks and limits the total number of IDs used + by processes attached to a cgroup hierarchy. For more information, + please check Encryption IDs section in + /Documentation/admin-guide/cgroup-v2.rst. + config CGROUP_DEBUG bool "Debug controller" default n
kernel/cgroup/Makefile +1 -0
@@ -5,4 +5,5 @@ obj-$(CONFIG_CGROUP_FREEZER) += legacy_freezer.o obj-$(CONFIG_CGROUP_PIDS) += pids.o obj-$(CONFIG_CGROUP_RDMA) += rdma.o obj-$(CONFIG_CPUSETS) += cpuset.o +obj-$(CONFIG_CGROUP_ENCRYPTION_IDS) += encryption_ids.o obj-$(CONFIG_CGROUP_DEBUG) += debug.o
kernel/cgroup/encryption_ids.c new file +421 -0
421 lines not on this page · raw message
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
PATCH v5 2/2 cgroup: svm: Encryption IDs cgroup documentation.
In reply to Vipin SharmaDocumentation of Encryption IDs controller. This new controller is used to track and limit usage of hardware memory encryption capabilities on the CPUs.
--- .../admin-guide/cgroup-v1/encryption_ids.rst | 1 + Documentation/admin-guide/cgroup-v2.rst | 78 ++++++++++++++++++- 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 Documentation/admin-guide/cgroup-v1/encryption_ids.rst
Documentation/admin-guide/cgroup-v1/encryption_ids.rst new file +1 -0
@@ -0,0 +1 @@ +/Documentation/admin-guide/cgroup-v2.rst
Documentation/admin-guide/cgroup-v2.rst +76 -2
90 lines not on this page · raw message
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
Randy Dunlap
reviewerRe: [Patch v5 2/2] cgroup: svm: Encryption IDs cgroup documentation.
Quoted 44 lines · Vipin Sharma
Documentation of Encryption IDs controller. This new controller is used to track and limit usage of hardware memory encryption capabilities on the CPUs.
Signed-off-by Vipin Sharma <[email protected]>Reviewed-by David Rientjes <[email protected]>Reviewed-by Dionna Glaze <[email protected]>--- .../admin-guide/cgroup-v1/encryption_ids.rst | 1 + Documentation/admin-guide/cgroup-v2.rst | 78 ++++++++++++++++++- 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 Documentation/admin-guide/cgroup-v1/encryption_ids.rstDocumentation/admin-guide/cgroup-v1/encryption_ids.rst new file +1
-0@@ -0,0 +1 @@ +/Documentation/admin-guide/cgroup-v2.rstDocumentation/admin-guide/cgroup-v2.rst +7
-2@@ -63,8 +63,11 @@ v1 is available under :ref:`Documentation/admin-guide/cgroup-v1/index.rst <cgrou 5-7-1. RDMA Interface Files 5-8. HugeTLB 5.8-1. HugeTLB Interface Files - 5-8. Misc - 5-8-1. perf_event + 5-9. Encryption IDs + 5.9-1 Encryption IDs Interface Files + 5.9-2 Migration and Ownership + 5-10. Misc + 5-10-1. perf_event 5-N. Non-normative information 5-N-1. CPU controller root cgroup process behaviour 5-N-2. IO controller root cgroup process behaviour@@ -2160,6 +2163,77 @@ HugeTLB Interface Files are local to the cgroup i.e. not hierarchical. The file modified event generated on this file reflects only the local events. +Encryption IDs +--------------
Documentation/admin-guide/cgroup-v2.rst HugeTLB Interface Files · line 2177
Quoted 10 lines
Documentation/admin-guide/cgroup-v2.rst +10
-0+ +There are multiple hardware memory encryption capabilities provided by the +hardware vendors, like Secure Encrypted Virtualization (SEV) and SEV Encrypted +State (SEV-ES) from AMD. + +These features are being used in encrypting virtual machines (VMs) and user +space programs. However, only a small number of keys/IDs can be used +simultaneously. + +This limited availability of these IDs requires system admin to optimize
admins
Documentation/admin-guide/cgroup-v2.rst HugeTLB Interface Files · line 2182
Quoted 5 lines
Documentation/admin-guide/cgroup-v2.rst +5
-0+allocation, control, and track the usage of the resources in the cloud +infrastructure. This resource also needs to be protected from getting exhausted +by some malicious program and causing starvation for other programs. + +Encryption IDs controller provides capability to register the resource for
The Encryption IDs controller provides the capability to register the resource for
Documentation/admin-guide/cgroup-v2.rst HugeTLB Interface Files · line 2183
Quoted 1 line
Documentation/admin-guide/cgroup-v2.rst +1
-0+controlling and tracking through the cgroups.
through cgroups.
Documentation/admin-guide/cgroup-v2.rst HugeTLB Interface Files · line 2188
Quoted 5 lines
Documentation/admin-guide/cgroup-v2.rst +5
-0+ +Encryption IDs Interface Files +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Each encryption ID type have their own interface files,
has its own
Documentation/admin-guide/cgroup-v2.rst HugeTLB Interface Files · line 2189
Quoted 1 line
Documentation/admin-guide/cgroup-v2.rst +1
-0+encids.[ID TYPE].{max, current, stat}, where "ID TYPE" can be sev and
or
Documentation/admin-guide/cgroup-v2.rst HugeTLB Interface Files · line 2200
Quoted 11 lines
Documentation/admin-guide/cgroup-v2.rst +11
-0+sev-es. + + encids.[ID TYPE].stat + A read-only flat-keyed single value file. This file exists only in the + root cgroup. + + It shows the total number of encryption IDs available and currently in + use on the platform:: + # cat encids.sev.stat + total 509 + used 0
This is described above as a single-value file...
Is the max value a hardware limit or a software (flexible) limit?
Quoted 24 lines
Documentation/admin-guide/cgroup-v2.rst +24
-0+ + encids.[ID TYPE].max + A read-write file which exists on the non-root cgroups. File is used to + set maximum count of "[ID TYPE]" which can be used in the cgroup. + + Limit can be set to max by:: + # echo max > encids.sev.max + + Limit can be set by:: + # echo 100 > encids.sev.max + + This file shows the max limit of the encryption ID in the cgroup:: + # cat encids.sev.max + max + + OR:: + # cat encids.sev.max + 100 + + Limits can be set more than the "total" capacity value in the + encids.[ID TYPE].stat file, however, the controller ensures + that the usage never exceeds the "total" and the max limit. + + encids.[ID TYPE].current
Documentation/admin-guide/cgroup-v2.rst HugeTLB Interface Files · line 2234
Quoted 10 lines
Documentation/admin-guide/cgroup-v2.rst +10
-0+ A read-only single value file which exists on non-root cgroups. + + Shows the total number of encrypted IDs being used in the cgroup. + +Migration and Ownership +~~~~~~~~~~~~~~~~~~~~~~~ + +An encryption ID is charged to the cgroup in which it is used first, and +stays charged to that cgroup until that ID is freed. Migrating a process +to a different cgroup do not move the charge to the destination cgroup
does
Quoted 6 lines
Documentation/admin-guide/cgroup-v2.rst +2
-0+where the process has moved. + Misc ----
Signature · 3 lines
~Randy You can't do anything without having to do something else first. -- Belefant's Law
Re: [Patch v5 2/2] cgroup: svm: Encryption IDs cgroup documentation.
Quoted 137 lines · Randy Dunlap
Quoted 44 lines · Vipin Sharma
Documentation of Encryption IDs controller. This new controller is used to track and limit usage of hardware memory encryption capabilities on the CPUs.
Signed-off-by Vipin Sharma <[email protected]>Reviewed-by David Rientjes <[email protected]>Reviewed-by Dionna Glaze <[email protected]>--- .../admin-guide/cgroup-v1/encryption_ids.rst | 1 + Documentation/admin-guide/cgroup-v2.rst | 78 ++++++++++++++++++- 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 Documentation/admin-guide/cgroup-v1/encryption_ids.rstDocumentation/admin-guide/cgroup-v1/encryption_ids.rst new file +1
-0@@ -0,0 +1 @@ +/Documentation/admin-guide/cgroup-v2.rstDocumentation/admin-guide/cgroup-v2.rst +7
-2@@ -63,8 +63,11 @@ v1 is available under :ref:`Documentation/admin-guide/cgroup-v1/index.rst <cgrou 5-7-1. RDMA Interface Files 5-8. HugeTLB 5.8-1. HugeTLB Interface Files - 5-8. Misc - 5-8-1. perf_event + 5-9. Encryption IDs + 5.9-1 Encryption IDs Interface Files + 5.9-2 Migration and Ownership + 5-10. Misc + 5-10-1. perf_event 5-N. Non-normative information 5-N-1. CPU controller root cgroup process behaviour 5-N-2. IO controller root cgroup process behaviour@@ -2160,6 +2163,77 @@ HugeTLB Interface Files are local to the cgroup i.e. not hierarchical. The file modified event generated on this file reflects only the local events. +Encryption IDs +--------------Documentation/admin-guide/cgroup-v2.rst HugeTLB Interface Files · line 2177
Quoted 10 lines
Documentation/admin-guide/cgroup-v2.rst +10
-0+ +There are multiple hardware memory encryption capabilities provided by the +hardware vendors, like Secure Encrypted Virtualization (SEV) and SEV Encrypted +State (SEV-ES) from AMD. + +These features are being used in encrypting virtual machines (VMs) and user +space programs. However, only a small number of keys/IDs can be used +simultaneously. + +This limited availability of these IDs requires system admin to optimizeadmins
Documentation/admin-guide/cgroup-v2.rst HugeTLB Interface Files · line 2182
Quoted 5 lines
Documentation/admin-guide/cgroup-v2.rst +5
-0+allocation, control, and track the usage of the resources in the cloud +infrastructure. This resource also needs to be protected from getting exhausted +by some malicious program and causing starvation for other programs. + +Encryption IDs controller provides capability to register the resource forThe Encryption IDs controller provides the capability to register the resource for
Documentation/admin-guide/cgroup-v2.rst HugeTLB Interface Files · line 2183
Quoted 1 line
Documentation/admin-guide/cgroup-v2.rst +1
-0+controlling and tracking through the cgroups.through cgroups.
Documentation/admin-guide/cgroup-v2.rst HugeTLB Interface Files · line 2188
Quoted 5 lines
Documentation/admin-guide/cgroup-v2.rst +5
-0+ +Encryption IDs Interface Files +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Each encryption ID type have their own interface files,has its own
Documentation/admin-guide/cgroup-v2.rst HugeTLB Interface Files · line 2189
Quoted 1 line
Documentation/admin-guide/cgroup-v2.rst +1
-0+encids.[ID TYPE].{max, current, stat}, where "ID TYPE" can be sev andor
Documentation/admin-guide/cgroup-v2.rst HugeTLB Interface Files · line 2200
Quoted 11 lines
Documentation/admin-guide/cgroup-v2.rst +11
-0+sev-es. + + encids.[ID TYPE].stat + A read-only flat-keyed single value file. This file exists only in the + root cgroup. + + It shows the total number of encryption IDs available and currently in + use on the platform:: + # cat encids.sev.stat + total 509 + used 0This is described above as a single-value file...
Is the max value a hardware limit or a software (flexible) limit?
Quoted 24 lines
Documentation/admin-guide/cgroup-v2.rst +24
-0+ + encids.[ID TYPE].max + A read-write file which exists on the non-root cgroups. File is used to + set maximum count of "[ID TYPE]" which can be used in the cgroup. + + Limit can be set to max by:: + # echo max > encids.sev.max + + Limit can be set by:: + # echo 100 > encids.sev.max + + This file shows the max limit of the encryption ID in the cgroup:: + # cat encids.sev.max + max + + OR:: + # cat encids.sev.max + 100 + + Limits can be set more than the "total" capacity value in the + encids.[ID TYPE].stat file, however, the controller ensures + that the usage never exceeds the "total" and the max limit. + + encids.[ID TYPE].currentDocumentation/admin-guide/cgroup-v2.rst HugeTLB Interface Files · line 2234
Quoted 10 lines
Documentation/admin-guide/cgroup-v2.rst +10
-0+ A read-only single value file which exists on non-root cgroups. + + Shows the total number of encrypted IDs being used in the cgroup. + +Migration and Ownership +~~~~~~~~~~~~~~~~~~~~~~~ + +An encryption ID is charged to the cgroup in which it is used first, and +stays charged to that cgroup until that ID is freed. Migrating a process +to a different cgroup do not move the charge to the destination cgroupdoes
Quoted 12 lines
Documentation/admin-guide/cgroup-v2.rst HugeTLB Interface Files · line 2239
Quoted 6 lines
Documentation/admin-guide/cgroup-v2.rst +2
-0+where the process has moved. + Misc ------
~Randy
You can't do anything without having to do something else first. -- Belefant's Law
Thank you, I will fix them in the next patch.