Skip to contentSkip to conversation

cgroup: New misc cgroup controller

RFC Superseded Vipin Sharma 3 participants 22 messages Mar 2, 2021 – Mar 16, 2021

Subsystems (5)
Documentation x86 KVM KVM x86 +1 more subsystems

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover cgroup: New misc cgroup controller

Hello

This patch series is creating a new misc cgroup controller for limiting and tracking of resources which are not abstract like other cgroup controllers.

This controller was initially proposed as encryption_id but after the feedbacks, it is now changed to misc cgroup.
https://lore.kernel.org/lkml/[email protected]/

Changes in RFC v2:
1. Documentation fixes.
2. Added kernel log messages.
3. Changed charge API to treat misc_cg as input parameter.
4. Added helper APIs to get and release references on the cgroup.

[1] https://lore.kernel.org/lkml/[email protected]
Vipin Sharma (2):
cgroup: sev: Add misc cgroup controller
cgroup: sev: Miscellaneous cgroup documentation.

 Documentation/admin-guide/cgroup-v1/index.rst |   1 +
 Documentation/admin-guide/cgroup-v1/misc.rst  |   4 +
 Documentation/admin-guide/cgroup-v2.rst       |  69 ++-
 arch/x86/kvm/svm/sev.c                        |  65 ++-
 arch/x86/kvm/svm/svm.h                        |   1 +
 include/linux/cgroup_subsys.h                 |   4 +
 include/linux/misc_cgroup.h                   | 122 +++++
 init/Kconfig                                  |  14 +
 kernel/cgroup/Makefile                        |   1 +
 kernel/cgroup/misc.c                          | 423 ++++++++++++++++++
 10 files changed, 692 insertions(+), 12 deletions(-)
 create mode 100644 Documentation/admin-guide/cgroup-v1/misc.rst
 create mode 100644 include/linux/misc_cgroup.h
 create mode 100644 kernel/cgroup/misc.c
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#1 lore raw

RFC v2 1/2 cgroup: sev: Add misc cgroup controller

The Miscellaneous cgroup provides the resource limiting and tracking mechanism for the scalar resources which cannot be abstracted like the other cgroup resources. Controller is enabled by the CONFIG_CGROUP_MISC config option.

The first two resources added to the miscellaneous controller are Secure Encrypted Virtualization (SEV) ASIDs and SEV - Encrypted State (SEV-ES) ASIDs. These limited ASIDs are used for encrypting virtual machines memory on the AMD platform

Miscellaneous controller provides 3 interface files:

misc.capacity
A read-only flat-keyed file shown only in the root cgroup. It shows miscellaneous scalar resources available on the platform along with their quantities::

	$ cat misc.capacity
	sev 50
	sev_es 10

misc.current
A read-only flat-keyed file shown in the non-root cgroups. It shows the current usage of the resources in the cgroup and its children::

	$ cat misc.current
	sev 3
	sev_es 0

misc.max
A read-write flat-keyed file shown in the non root cgroups. Allowed maximum usage of the resources in the cgroup and its children.::

	$ cat misc.max
	sev max
	sev_es 4

Limit can be set by::

	# echo sev 1 > misc.max

Limit can be set to max by::

	# echo sev max > misc.max

Limits can be set more than the capacity value in the misc.capacity file.

---
 arch/x86/kvm/svm/sev.c        |  65 +++++-
 arch/x86/kvm/svm/svm.h        |   1 +
 include/linux/cgroup_subsys.h |   4 +
 include/linux/misc_cgroup.h   | 122 ++++++++++
 init/Kconfig                  |  14 ++
 kernel/cgroup/Makefile        |   1 +
 kernel/cgroup/misc.c          | 423 ++++++++++++++++++++++++++++++++++
 7 files changed, 620 insertions(+), 10 deletions(-)
 create mode 100644 include/linux/misc_cgroup.h
 create mode 100644 kernel/cgroup/misc.c
arch/x86/kvm/svm/sev.c +55 -10
index 48017fef1cd9..dd05a1522862 100644
@@ -14,6 +14,7 @@
 #include <linux/psp-sev.h>
 #include <linux/pagemap.h>
 #include <linux/swap.h>
+#include <linux/misc_cgroup.h>
 #include <linux/processor.h>
 #include <linux/trace_events.h>
 #include <asm/fpu/internal.h>
@@ -27,6 +28,21 @@
 
 #define __ex(x) __kvm_handle_fault_on_reboot(x)
 
+#ifndef CONFIG_KVM_AMD_SEV
+/*
+ * When this config is not defined, SEV feature is not supported and APIs in
+ * this file are not used but this file still gets compiled into the KVM AMD
+ * module.
+ *
+ * We will not have MISC_CG_RES_SEV and MISC_CG_RES_SEV_ES entries in the enum
+ * misc_res_type {} defined in linux/misc_cgroup.h.
+ *
+ * Below macros allow compilation to succeed.
+ */
+#define MISC_CG_RES_SEV MISC_CG_RES_TYPES
+#define MISC_CG_RES_SEV_ES MISC_CG_RES_TYPES
+#endif
+
 static u8 sev_enc_bit;
 static int sev_flush_asids(void);
 static DECLARE_RWSEM(sev_deactivate_lock);
@@ -88,8 +104,17 @@ static bool __sev_recycle_asids(int min_asid, int max_asid)
 
 static int sev_asid_new(struct kvm_sev_info *sev)
 {
-	int pos, min_asid, max_asid;
+	int pos, min_asid, max_asid, ret;
 	bool retry = true;
+	enum misc_res_type type;
+
+	type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
+	sev->misc_cg = get_current_misc_cg();
+	ret = misc_cg_try_charge(type, sev->misc_cg, 1);
+	if (ret) {
+		put_misc_cg(sev->misc_cg);
+		return ret;
+	}
 
 	mutex_lock(&sev_bitmap_lock);
 
@@ -107,7 +132,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 +141,10 @@ static int sev_asid_new(struct kvm_sev_info *sev)
 	mutex_unlock(&sev_bitmap_lock);
 
 	return pos + 1;
+e_uncharge:
+	misc_cg_uncharge(type, sev->misc_cg, 1);
+	put_misc_cg(sev->misc_cg);
+	return ret;
 }
 
 static int sev_get_asid(struct kvm *kvm)
@@ -124,14 +154,15 @@ 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_sev_info *sev)
 {
 	struct svm_cpu_data *sd;
 	int cpu, pos;
+	enum misc_res_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 +171,10 @@ static void sev_asid_free(int asid)
 	}
 
 	mutex_unlock(&sev_bitmap_lock);
+
+	type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
+	misc_cg_uncharge(type, sev->misc_cg, 1);
+	put_misc_cg(sev->misc_cg);
 }
 
 static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
@@ -187,19 +222,19 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
 	asid = sev_asid_new(sev);
 	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(sev);
 	return ret;
 }
 
@@ -1243,12 +1278,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(sev);
 }
 
 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;
 
@@ -1280,7 +1315,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 (misc_cg_set_capacity(MISC_CG_RES_SEV, sev_asid_count))
+		goto out;
+
+	pr_info("SEV supported: %u ASIDs\n", sev_asid_count);
 	sev_supported = true;
 
 	/* SEV-ES support requested? */
@@ -1295,7 +1334,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 (misc_cg_set_capacity(MISC_CG_RES_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:
@@ -1310,6 +1353,8 @@ void sev_hardware_teardown(void)
 
 	bitmap_free(sev_asid_bitmap);
 	bitmap_free(sev_reclaim_asid_bitmap);
+	misc_cg_set_capacity(MISC_CG_RES_SEV, 0);
+	misc_cg_set_capacity(MISC_CG_RES_SEV_ES, 0);
 
 	sev_flush_asids();
 }
arch/x86/kvm/svm/svm.h +1 -0
index 6e7d070f8b86..8ed6ebf47885 100644
@@ -79,6 +79,7 @@ struct kvm_sev_info {
 	unsigned long pages_locked; /* Number of pages locked */
 	struct list_head regions_list;  /* List of registered regions */
 	u64 ap_jump_table;	/* SEV-ES AP Jump Table address */
+	struct misc_cg *misc_cg; /* For misc cgroup accounting */
 };
 
 struct kvm_svm {
include/linux/cgroup_subsys.h +4 -0
index acb77dcff3b4..445235487230 100644
@@ -61,6 +61,10 @@ SUBSYS(pids)
 SUBSYS(rdma)
 #endif
 
+#if IS_ENABLED(CONFIG_CGROUP_MISC)
+SUBSYS(misc)
+#endif
+
 /*
  * The following subsystems are not supported on the default hierarchy.
  */
include/linux/misc_cgroup.h new file +122 -0

122 lines not on this page · raw message

init/Kconfig +14 -0
index 29ad68325028..0b392135e555 100644
@@ -1105,6 +1105,20 @@ config CGROUP_BPF
 	  BPF_CGROUP_INET_INGRESS will be executed on the ingress path of
 	  inet sockets.
 
+config CGROUP_MISC
+	bool "Misc resource controller"
+	default n
+	help
+	  Provides a controller for miscellaneous resources on a host.
+
+	  Miscellaneous scalar resources are the resources on the host system
+	  which cannot be abstracted like the other cgroups. This controller
+	  tracks and limits the miscellaneous resources used by a process
+	  attached to a cgroup hierarchy.
+
+	  For more information, please check misc cgroup section in
+	  /Documentation/admin-guide/cgroup-v2.rst.
+
 config CGROUP_DEBUG
 	bool "Debug controller"
 	default n
kernel/cgroup/Makefile +1 -0
index 5d7a76bfbbb7..12f8457ad1f9 100644
@@ -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_MISC) += misc.o
 obj-$(CONFIG_CGROUP_DEBUG) += debug.o
kernel/cgroup/misc.c new file +423 -0

423 lines not on this page · raw message

Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
maintainer
#2 lore raw

Re: [RFC v2 1/2] cgroup: sev: Add misc cgroup controller

Hello,

include/linux/misc_cgroup.h line 36

Quoted 9 lines · Vipin Sharma
include/linux/misc_cgroup.h new file +9 -0
+/**
+ * struct misc_res: Per cgroup per misc type resource
+ * @max: Maximum count of the resource.
+ * @usage: Current usage of the resource.
+ */
+struct misc_res {
+	unsigned int max;
+	atomic_t usage;
+};

Can we do 64bits so that something which counts memory can use this too?

Quoted 8 lines

+/*
+ * Miscellaneous resources capacity for the entire machine. 0 capacity means
+ * resource is not initialized or not present in the host.
+ *
+ * root_cg.max and capacity are independent of each other. root_cg.max can be
+ * more than the actual capacity. We are using Limits resource distribution
+ * model of cgroup for miscellaneous controller. However, root_cg.current for a
+ * resource will never exceeds the resource capacity.

                                ^
                                typo
Quoted 13 lines
kernel/cgroup/misc.c new file +13 -0
+int misc_cg_set_capacity(enum misc_res_type type, unsigned int capacity)
+{
+	if (!valid_type(type))
+		return -EINVAL;
+
+	for (;;) {
+		int usage;
+		unsigned int old;
+
+		/*
+		 * Update the capacity while making sure that it's not below
+		 * the concurrently-changing usage value.
+		 *

kernel/cgroup/misc.c line 109

Quoted 10 lines
kernel/cgroup/misc.c new file +10 -0
+		 * The xchg implies two full memory barriers before and after,
+		 * so the read-swap-read is ordered and ensures coherency with
+		 * misc_cg_try_charge(): that function modifies the usage
+		 * before checking the capacity, so if it sees the old
+		 * capacity, we see the modified usage and retry.
+		 */
+		usage = atomic_read(&root_cg.res[type].usage);
+
+		if (usage > capacity)
+			return -EBUSY;

I'd rather go with allowing bringing down capacity below usage so that the users can set it to a lower value to drain existing usages while denying new ones. It's not like it's difficult to check the current total usage from the caller side, so I'm not sure it's very useful to shift the condition check here.

Quoted 3 lines
include/linux/misc_cgroup.h new file +3 -0
+int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg,
+		       unsigned int amount)
+{

...

Quoted 7 lines
kernel/cgroup/misc.c new file +7 -0
+	for (i = cg; i; i = parent_misc(i)) {
+		res = &i->res[type];
+
+		/*
+		 * The atomic_long_add_return() implies a full memory barrier
+		 * between incrementing the count and reading the capacity.
+		 * When racing with misc_cg_set_capacity(), we either see the

kernel/cgroup/misc.c line 185

Quoted 10 lines
kernel/cgroup/misc.c new file +10 -0
+		 * new capacity or the setter sees the counter has changed and
+		 * retries.
+		 */
+		new_usage = atomic_add_return(amount, &res->usage);
+		if (new_usage > res->max ||
+		    new_usage > misc_res_capacity[type]) {
+			pr_info("cgroup: charge rejected by misc controller for %s resource in ",
+				misc_res_name[type]);
+			pr_cont_cgroup_path(i->css.cgroup);
+			pr_cont("\n");

Should have commented on this in the priv thread but don't print something on every rejection. This often becomes a nuisance and can make an easy DoS vector at worst. If you wanna do it, print it once per cgroup or sth like that.

Otherwise, looks good to me.

Thanks.

Signature · 1 line
tejun
#3 lore raw

Re: [RFC v2 1/2] cgroup: sev: Add misc cgroup controller

Quoted 5 lines · Tejun Heo

Review

Quoted 2 lines
(quoted diff) +2 -0
+	atomic_t usage;
+};

Can we do 64bits so that something which counts memory can use this too?

Sure.

Quoted 10 lines

kernel/cgroup/misc.c line 109

Quoted 3 lines
kernel/cgroup/misc.c new file +3 -0
+
+		if (usage > capacity)
+			return -EBUSY;

I'd rather go with allowing bringing down capacity below usage so that the users can set it to a lower value to drain existing usages while denying new ones. It's not like it's difficult to check the current total usage from the caller side, so I'm not sure it's very useful to shift the condition check here.

Okay, I will change the code to set new capacity unconditionally.

Right now there is no API for the caller to know total usage, unless they keep their own tally, I was thinking it will be useful to add one more API

unsigned long misc_cg_res_total_usage(enum misc_res_type type)

It will return root_cg usage for "type" resource.
Will it be fine?

Quoted 9 lines

kernel/cgroup/misc.c line 185

Quoted 4 lines
kernel/cgroup/misc.c new file +4 -0
+			pr_info("cgroup: charge rejected by misc controller for %s resource in ",
+				misc_res_name[type]);
+			pr_cont_cgroup_path(i->css.cgroup);
+			pr_cont("\n");

Should have commented on this in the priv thread but don't print something on every rejection. This often becomes a nuisance and can make an easy DoS vector at worst. If you wanna do it, print it once per cgroup or sth like that.

I didn't think in that way. Thanks, I will print it once per cgroup.

Thanks
Vipin

maintainer
#4 lore raw

Re: [RFC v2 1/2] cgroup: sev: Add misc cgroup controller

Hello,

Quoted 7 lines · Vipin Sharma

Right now there is no API for the caller to know total usage, unless they keep their own tally, I was thinking it will be useful to add one more API

unsigned long misc_cg_res_total_usage(enum misc_res_type type)

It will return root_cg usage for "type" resource.
Will it be fine?

Yeah, that sounds fine.

Thanks.

Signature · 1 line
tejun
#5 lore raw

RFC v2 2/2 cgroup: sev: Miscellaneous cgroup documentation.

In reply to Vipin Sharma

Documentation of miscellaneous cgroup controller. This new controller is used to track and limit the usage of scalar resources.

---
 Documentation/admin-guide/cgroup-v1/index.rst |  1 +
 Documentation/admin-guide/cgroup-v1/misc.rst  |  4 ++
 Documentation/admin-guide/cgroup-v2.rst       | 69 ++++++++++++++++++-
 3 files changed, 72 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/admin-guide/cgroup-v1/misc.rst
Documentation/admin-guide/cgroup-v1/index.rst +1 -0
index 226f64473e8e..99fbc8a64ba9 100644
@@ -17,6 +17,7 @@ Control Groups version 1
     hugetlb
     memcg_test
     memory
+    misc
     net_cls
     net_prio
     pids
Documentation/admin-guide/cgroup-v1/misc.rst new file +4 -0
new file mode 100644
index 000000000000..661614c24df3
@@ -0,0 +1,4 @@
+===============
+Misc controller
+===============
+Please refer "Misc" documentation in Documentation/admin-guide/cgroup-v2.rst
Documentation/admin-guide/cgroup-v2.rst +67 -2

81 lines not on this page · raw message

Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#6 lore raw

Re: [RFC v2 2/2] cgroup: sev: Miscellaneous cgroup documentation.

Hi Vipin,

Quoted 8 lines · Vipin Sharma
Documentation/admin-guide/cgroup-v2.rst +4 -0
+Migration and Ownership
+~~~~~~~~~~~~~~~~~~~~~~~
+
+A miscellaneous scalar resource is charged to the cgroup in which it is

used +first, and stays charged to that cgroup until that resource is freed. Migrating +a process to a different cgroup does not move the charge to the destination +cgroup where the process has moved. +

I am trying to see if IOASIDs cgroup can also fit in this misc controller as yet another resource type.
https://lore.kernel.org/linux-iommu/20210303131726.7a8cb169@jacob-builder/T/#u
However, unlike sev IOASIDs need to be migrated if the process is moved to another cgroup. i.e. charge the destination and uncharge the source.

Do you think this behavior can be achieved by differentiating resource types? i.e. add attach callbacks for certain types. Having a single misc interface seems cleaner than creating another controller.

Thanks,

Jacob

#7 lore raw

Re: [RFC v2 2/2] cgroup: sev: Miscellaneous cgroup documentation.

Quoted 18 lines · Jacob Pan

Hi Vipin,

Quoted 8 lines · Vipin Sharma
Documentation/admin-guide/cgroup-v2.rst +4 -0
+Migration and Ownership
+~~~~~~~~~~~~~~~~~~~~~~~
+
+A miscellaneous scalar resource is charged to the cgroup in which it is

used +first, and stays charged to that cgroup until that resource is freed. Migrating +a process to a different cgroup does not move the charge to the destination +cgroup where the process has moved. +

I am trying to see if IOASIDs cgroup can also fit in this misc controller as yet another resource type.
https://lore.kernel.org/linux-iommu/20210303131726.7a8cb169@jacob-builder/T/#u
However, unlike sev IOASIDs need to be migrated if the process is moved to another cgroup. i.e. charge the destination and uncharge the source.

Quoted 3 lines

Do you think this behavior can be achieved by differentiating resource types? i.e. add attach callbacks for certain types. Having a single misc interface seems cleaner than creating another controller.

I think it makes sense to add support for migration for the resources which need it. Resources like SEV, SEV-ES will not participate in migration and won't stop can_attach() to succeed, other resources which need migration will allow or stop based on their limits and capacity in the destination.

maintainer
#8 lore raw

Re: [RFC v2 2/2] cgroup: sev: Miscellaneous cgroup documentation.

Hello,

Quoted 10 lines · Vipin Sharma
Quoted 9 lines

I am trying to see if IOASIDs cgroup can also fit in this misc controller as yet another resource type.
https://lore.kernel.org/linux-iommu/20210303131726.7a8cb169@jacob-builder/T/#u
However, unlike sev IOASIDs need to be migrated if the process is moved to another cgroup. i.e. charge the destination and uncharge the source.

Do you think this behavior can be achieved by differentiating resource types? i.e. add attach callbacks for certain types. Having a single misc interface seems cleaner than creating another controller.

Quoted 5 lines

I think it makes sense to add support for migration for the resources which need it. Resources like SEV, SEV-ES will not participate in migration and won't stop can_attach() to succeed, other resources which need migration will allow or stop based on their limits and capacity in the destination.

Please note that cgroup2 by and large don't really like or support charge migration or even migrations themselves. We tried that w/ memcg on cgroup1 and it turned out horrible. The expected usage model as decribed in the doc is using migration to seed a cgroup (or even better, use the new clone call to start in the target cgroup) and then stay there until exit. All existing controllers assume this usage model and I'm likely to nack deviation unless there are some super strong justifications.

Thanks.

Signature · 1 line
tejun
#9 lore raw

Re: [RFC v2 2/2] cgroup: sev: Miscellaneous cgroup documentation.

Hi Vipin & Tejun,

Sorry for the late reply, I sent from a different email address than I intended. Please see my comments inline.

Quoted 20 lines · Tejun Heo

Hello,

Quoted 16 lines · Vipin Sharma
Quoted 10 lines

I am trying to see if IOASIDs cgroup can also fit in this misc controller as yet another resource type.
https://lore.kernel.org/linux-iommu/20210303131726.7a8cb169@jacob-builder/T/#u
However, unlike sev IOASIDs need to be migrated if the process is moved to another cgroup. i.e. charge the destination and uncharge the source.

Do you think this behavior can be achieved by differentiating resource types? i.e. add attach callbacks for certain types. Having a single misc interface seems cleaner than creating another controller.

I think it makes sense to add support for migration for the resources which need it. Resources like SEV, SEV-ES will not participate in migration and won't stop can_attach() to succeed, other resources which need migration will allow or stop based on their limits and capacity in the destination.

Sounds good. Perhaps some capability/feature flags for each resource such that different behavior can be accommodated?
Could you please include me in your future posting? I will rebase on yours.

Quoted 8 lines

Please note that cgroup2 by and large don't really like or support charge migration or even migrations themselves. We tried that w/ memcg on cgroup1 and it turned out horrible. The expected usage model as decribed in the doc is using migration to seed a cgroup (or even better, use the new clone call to start in the target cgroup) and then stay there until exit. All existing controllers assume this usage model and I'm likely to nack deviation unless there are some super strong justifications.

Thank you so much for the pointers. Just to be clear, you meant
1. Use clone3 CLONE_INTO_CGROUP to put the child into a different cgroup.
2. Do not support migration of the parent (existing proc)

Thanks,

Jacob

#10 lore raw

Re: [RFC v2 2/2] cgroup: sev: Miscellaneous cgroup documentation.

Quoted 28 lines · Jacob Pan

Hi Vipin & Tejun,

Sorry for the late reply, I sent from a different email address than I intended. Please see my comments inline.

Quoted 20 lines · Tejun Heo

Hello,

Quoted 16 lines · Vipin Sharma
Quoted 10 lines

I am trying to see if IOASIDs cgroup can also fit in this misc controller as yet another resource type.
https://lore.kernel.org/linux-iommu/20210303131726.7a8cb169@jacob-builder/T/#u
However, unlike sev IOASIDs need to be migrated if the process is moved to another cgroup. i.e. charge the destination and uncharge the source.

Do you think this behavior can be achieved by differentiating resource types? i.e. add attach callbacks for certain types. Having a single misc interface seems cleaner than creating another controller.

I think it makes sense to add support for migration for the resources which need it. Resources like SEV, SEV-ES will not participate in migration and won't stop can_attach() to succeed, other resources which need migration will allow or stop based on their limits and capacity in the destination.

Quoted 3 lines

Sounds good. Perhaps some capability/feature flags for each resource such that different behavior can be accommodated?
Could you please include me in your future posting? I will rebase on yours.

Hi Jacob

Based on Tejun's response, I will not add charge migration support in misc controller.

I can definitly add you in my future posting, if you still wanna use it without charge migration support.

Thanks
Vipin

#11 lore raw

Re: [RFC v2 2/2] cgroup: sev: Miscellaneous cgroup documentation.

Hi Vipin,

Quoted 35 lines · Vipin Sharma
Quoted 29 lines · Jacob Pan

Hi Vipin & Tejun,

Sorry for the late reply, I sent from a different email address than I intended. Please see my comments inline.

Quoted 21 lines · Tejun Heo

Hello,

Quoted 17 lines · Vipin Sharma
Quoted 11 lines

I am trying to see if IOASIDs cgroup can also fit in this misc controller as yet another resource type.
https://lore.kernel.org/linux-iommu/20210303131726.7a8cb169@jacob-builder/T/#u
However, unlike sev IOASIDs need to be migrated if the process is moved to another cgroup. i.e. charge the destination and uncharge the source.

Do you think this behavior can be achieved by differentiating resource types? i.e. add attach callbacks for certain types. Having a single misc interface seems cleaner than creating another controller.

I think it makes sense to add support for migration for the resources which need it. Resources like SEV, SEV-ES will not participate in migration and won't stop can_attach() to succeed, other resources which need migration will allow or stop based on their limits and capacity in the destination.

Quoted 4 lines

Sounds good. Perhaps some capability/feature flags for each resource such that different behavior can be accommodated?
Could you please include me in your future posting? I will rebase on yours.

Quoted 5 lines

Hi Jacob

Based on Tejun's response, I will not add charge migration support in misc controller.

Sounds good. I need some confirmation on whether migration is a must have for VMs allocated IOASIDs.
Our primary goal is to limit the amount of IOASIDs that VMs can allocate. If a VM is migrated to a different cgroup, I think we need to charge/uncharge the destination/source cgroup in order enforce the limit. I am not an expert here, any feedback would be appreciated.

Quoted 3 lines

I can definitly add you in my future posting, if you still wanna use it without charge migration support.

Yes, please. I got your v3 already, so just future patches.

Quoted 2 lines

Thanks
Vipin

Thanks,

Jacob

maintainer
#12 lore raw

Re: [RFC v2 2/2] cgroup: sev: Miscellaneous cgroup documentation.

Quoted 4 lines · Jacob Pan

Our primary goal is to limit the amount of IOASIDs that VMs can allocate. If a VM is migrated to a different cgroup, I think we need to charge/uncharge the destination/source cgroup in order enforce the limit. I am not an expert here, any feedback would be appreciated.

That simply isn't a supported usage model. None of other resources will get tracked if you do that.

Thanks.

Signature · 1 line
tejun
#13 lore raw

Re: [RFC v2 2/2] cgroup: sev: Miscellaneous cgroup documentation.

Hi Tejun,

Quoted 8 lines · Tejun Heo
Quoted 4 lines · Jacob Pan

Our primary goal is to limit the amount of IOASIDs that VMs can allocate. If a VM is migrated to a different cgroup, I think we need to charge/uncharge the destination/source cgroup in order enforce the limit. I am not an expert here, any feedback would be appreciated.

That simply isn't a supported usage model. None of other resources will get tracked if you do that.

Isn't PIDs controller doing the charge/uncharge? I was under the impression that each resource can be independently charged/uncharged, why it affects other resources? Sorry for the basic question.

I also didn't quite get the limitation on cgroup v2 migration, this is much simpler than memcg. Could you give me some pointers?

BTW, since the IOASIDs are used to tag DMA and bound with guest process(mm) for shared virtual addressing. fork() cannot be supported, so I guess clone is not a solution here.

Thanks,

Jacob

maintainer
#14 lore raw

Re: [RFC v2 2/2] cgroup: sev: Miscellaneous cgroup documentation.

Hello,

Quoted 3 lines · Jacob Pan

Isn't PIDs controller doing the charge/uncharge? I was under the impression that each resource can be independently charged/uncharged, why it affects other resources? Sorry for the basic question.

Yeah, PID is an exception as we needed the initial migration to seed new cgroups and it gets really confusing with other ways to observe the processes - e.g. if you follow the original way of creating a cgroup, forking and then moving the seed process into the target cgroup, if we don't migrate the pid charge together, the numbers wouldn't agree and the seeder cgroup may end up running out of pids if there are any restrictions.

Quoted 2 lines

I also didn't quite get the limitation on cgroup v2 migration, this is much simpler than memcg. Could you give me some pointers?

Migration itself doesn't have restrictions but all resources are distributed on the same hierarchy, so the controllers are supposed to follow the same conventions that can be implemented by all controllers.

Quoted 3 lines

BTW, since the IOASIDs are used to tag DMA and bound with guest process(mm) for shared virtual addressing. fork() cannot be supported, so I guess clone is not a solution here.

Can you please elaborate what wouldn't work? The new spawning into a new cgroup w/ clone doesn't really change the usage model. It's just a neater way to seed a new cgroup. If you're saying that the overall usage model doesn't fit the needs of IOASIDs, it likely shouldn't be a cgroup controller.

Thanks.

Signature · 1 line
tejun
#15 lore raw

Re: [RFC v2 2/2] cgroup: sev: Miscellaneous cgroup documentation.

Hi Tejun,

Quoted 7 lines · Tejun Heo

Hello,

Quoted 3 lines · Jacob Pan

Isn't PIDs controller doing the charge/uncharge? I was under the impression that each resource can be independently charged/uncharged, why it affects other resources? Sorry for the basic question.

Quoted 8 lines

Yeah, PID is an exception as we needed the initial migration to seed new cgroups and it gets really confusing with other ways to observe the processes - e.g. if you follow the original way of creating a cgroup, forking and then moving the seed process into the target cgroup, if we don't migrate the pid charge together, the numbers wouldn't agree and the seeder cgroup may end up running out of pids if there are any restrictions.

Thanks for explaining. Unfortunately, it seems IOASIDs has a similar needs in terms of migrating the charge.

Quoted 7 lines
Quoted 2 lines

I also didn't quite get the limitation on cgroup v2 migration, this is much simpler than memcg. Could you give me some pointers?

Migration itself doesn't have restrictions but all resources are distributed on the same hierarchy, so the controllers are supposed to follow the same conventions that can be implemented by all controllers.

Got it, I guess that is the behavior required by the unified hierarchy. Cgroup v1 would be ok? But I am guessing we are not extending on v1?

Quoted 10 lines
Quoted 3 lines

BTW, since the IOASIDs are used to tag DMA and bound with guest process(mm) for shared virtual addressing. fork() cannot be supported, so I guess clone is not a solution here.

Can you please elaborate what wouldn't work? The new spawning into a new cgroup w/ clone doesn't really change the usage model. It's just a neater way to seed a new cgroup. If you're saying that the overall usage model doesn't fit the needs of IOASIDs, it likely shouldn't be a cgroup controller.

The IOASIDs are programmed into devices to generate DMA requests tagged with them. The IOMMU has a per device IOASID table with each entry has two pointers:
- the PGD of the guest process.
- the PGD of the host process

The result of this 2 stage/nested translation is that we can share virtual address (SVA) between guest process and DMA. The host process needs to allocate multiple IOASIDs since one IOASID is needed for each guest process who wants SVA.

The DMA binding among device-IOMMU-process is setup via a series of user APIs (e.g. via VFIO).

If a process calls fork(), the children does not inherit the IOASIDs and their bindings. Children who wish to use SVA has to call those APIs to establish the binding for themselves.

Therefore, if a host process allocates 10 IOASIDs then does a fork()/clone(), it cannot charge 10 IOASIDs in the new cgroup. i.e. the 10 IOASIDs stays with the process wherever it goes.

I feel this fit in the domain model, true?

Quoted 2 lines

Thanks.

Thanks,

Jacob

maintainer
#16 lore raw

Re: [RFC v2 2/2] cgroup: sev: Miscellaneous cgroup documentation.

Hello,

Quoted 6 lines · Jacob Pan
Quoted 4 lines

Migration itself doesn't have restrictions but all resources are distributed on the same hierarchy, so the controllers are supposed to follow the same conventions that can be implemented by all controllers.

Got it, I guess that is the behavior required by the unified hierarchy. Cgroup v1 would be ok? But I am guessing we are not extending on v1?

A new cgroup1 only controller is unlikely to be accpeted.

Quoted 18 lines

The IOASIDs are programmed into devices to generate DMA requests tagged with them. The IOMMU has a per device IOASID table with each entry has two pointers:
- the PGD of the guest process.
- the PGD of the host process

The result of this 2 stage/nested translation is that we can share virtual address (SVA) between guest process and DMA. The host process needs to allocate multiple IOASIDs since one IOASID is needed for each guest process who wants SVA.

The DMA binding among device-IOMMU-process is setup via a series of user APIs (e.g. via VFIO).

If a process calls fork(), the children does not inherit the IOASIDs and their bindings. Children who wish to use SVA has to call those APIs to establish the binding for themselves.

Quoted 5 lines

Therefore, if a host process allocates 10 IOASIDs then does a fork()/clone(), it cannot charge 10 IOASIDs in the new cgroup. i.e. the 10 IOASIDs stays with the process wherever it goes.

I feel this fit in the domain model, true?

I still don't get where migration is coming into the picture. Who's migrating where?

Thanks.

Signature · 1 line
tejun
#17 lore raw

Re: [RFC v2 2/2] cgroup: sev: Miscellaneous cgroup documentation.

Hi Tejun,

Quoted 30 lines · Tejun Heo

Hello,

Quoted 6 lines · Jacob Pan
Quoted 4 lines

Migration itself doesn't have restrictions but all resources are distributed on the same hierarchy, so the controllers are supposed to follow the same conventions that can be implemented by all controllers.

Got it, I guess that is the behavior required by the unified hierarchy. Cgroup v1 would be ok? But I am guessing we are not extending on v1?

A new cgroup1 only controller is unlikely to be accpeted.

Quoted 18 lines

The IOASIDs are programmed into devices to generate DMA requests tagged with them. The IOMMU has a per device IOASID table with each entry has two pointers:
- the PGD of the guest process.
- the PGD of the host process

The result of this 2 stage/nested translation is that we can share virtual address (SVA) between guest process and DMA. The host process needs to allocate multiple IOASIDs since one IOASID is needed for each guest process who wants SVA.

The DMA binding among device-IOMMU-process is setup via a series of user APIs (e.g. via VFIO).

If a process calls fork(), the children does not inherit the IOASIDs and their bindings. Children who wish to use SVA has to call those APIs to establish the binding for themselves.

Quoted 9 lines
Quoted 5 lines

Therefore, if a host process allocates 10 IOASIDs then does a fork()/clone(), it cannot charge 10 IOASIDs in the new cgroup. i.e. the 10 IOASIDs stays with the process wherever it goes.

I feel this fit in the domain model, true?

I still don't get where migration is coming into the picture. Who's migrating where?

Sorry, perhaps I can explain by an example.

There are two cgroups: cg_A and cg_B with limit set to 20 for both. Process1 is in cg_A. The initial state is:
cg_A/ioasid.current=0, cg_A/ioasid.max=20
cg_B/ioasid.current=0, cg_B/ioasid.max=20

Now, consider the following steps:

1. Process1 allocated 10 IOASIDs,
cg_A/ioasid.current=10,
cg_B/ioasid.current=0

2. then we want to move/migrate Process1 to cg_B. so we need uncharge 10 of cg_A, charge 10 of cg_B

3. After the migration, I expect
cg_A/ioasid.current=0,
cg_B/ioasid.current=10

We don't enforce the limit during this organizational change since we can't force free IOASIDs. But any new allocations will be subject to the limit set in ioasid.max.

Quoted 2 lines

Thanks.

Thanks,

Jacob

maintainer
#18 lore raw

Re: [RFC v2 2/2] cgroup: sev: Miscellaneous cgroup documentation.

Hello,

Quoted 2 lines · Jacob Pan

2. then we want to move/migrate Process1 to cg_B. so we need uncharge 10 of cg_A, charge 10 of cg_B

So, what I don't get is why this migration is necessary. This isn't supported as a usage pattern and no one, at least in terms of wide-spread usage, does this. Why is this a requirement for your use case?

Thanks.

Signature · 1 line
tejun
#19 lore raw

Re: [RFC v2 2/2] cgroup: sev: Miscellaneous cgroup documentation.

Hi Tejun,

Quoted 10 lines · Tejun Heo

Hello,

Quoted 2 lines · Jacob Pan

2. then we want to move/migrate Process1 to cg_B. so we need uncharge 10 of cg_A, charge 10 of cg_B

So, what I don't get is why this migration is necessary. This isn't supported as a usage pattern and no one, at least in terms of wide-spread usage, does this. Why is this a requirement for your use case?

I don't know if this is required. I thought utilities such as cgclassify need to be supported.
" cgclassify - move running task(s) to given cgroups "
If no such use case, I am fine with dropping the migration support. Just enforce limit on allocations.

Quoted 2 lines

Thanks.

Thanks,

Jacob

maintainer
#20 lore raw

Re: [RFC v2 2/2] cgroup: sev: Miscellaneous cgroup documentation.

Quoted 5 lines · Jacob Pan

I don't know if this is required. I thought utilities such as cgclassify need to be supported.
" cgclassify - move running task(s) to given cgroups "
If no such use case, I am fine with dropping the migration support. Just enforce limit on allocations.

Yeah, that's what all other controllers do. Please read the in-tree cgroup2 doc.

Thanks.

Signature · 1 line
tejun
#21 lore raw

Re: [RFC v2 2/2] cgroup: sev: Miscellaneous cgroup documentation.

Hi Tejun,

Quoted 10 lines · Tejun Heo
Quoted 5 lines · Jacob Pan

I don't know if this is required. I thought utilities such as cgclassify need to be supported.
" cgclassify - move running task(s) to given cgroups "
If no such use case, I am fine with dropping the migration support. Just enforce limit on allocations.

Yeah, that's what all other controllers do. Please read the in-tree cgroup2 doc.

Thanks for your patience and guidance, will try to merge with misc controller and go from there.

Thanks,

Jacob