[PATCH RFC v2 2/2] cgroup/misc: Add support for Arm CCA MECIDs
From: Kohei Enju
Date: Wed Nov 11 2026 - 22:38:27 EST
Arm CCA MECIDs are limited platform resources. Add a misc cgroup
resource to allow the system administrator to account for and limit
their use.
The cca_mec resource represents the number of Realm VMs using the
private MEC policy. Each such Realm consumes one MECID. Charge the
resource when the Realm is created, and uncharge it when the Realm is
successfully destroyed.
Realms using the shared MEC policy don't consume this resource.
Signed-off-by: Kohei Enju <enju.kohei@xxxxxxxxxxx>
---
arch/arm64/include/asm/kvm_rmi.h | 2 ++
arch/arm64/kvm/rmi.c | 44 +++++++++++++++++++++++++++++++++++++++-
drivers/firmware/arm_rmm/rmi.c | 4 ++++
include/linux/misc_cgroup.h | 4 ++++
kernel/cgroup/misc.c | 4 ++++
5 files changed, 57 insertions(+), 1 deletion(-)
diff --git a/arch/arm64/include/asm/kvm_rmi.h b/arch/arm64/include/asm/kvm_rmi.h
index 08c1f6d0671d..658e65454282 100644
--- a/arch/arm64/include/asm/kvm_rmi.h
+++ b/arch/arm64/include/asm/kvm_rmi.h
@@ -60,6 +60,7 @@ enum realm_state {
* @ia_bits: Number of valid Input Address bits in the IPA
* @stage2_unmapped: The Realm stage-2 mappings have been removed
* @mec_policy: MEC policy for the Realm VM
+ * @misc_cg: Misc cgroup charged while the Realm owns a private MECID
*/
struct realm {
void *rd;
@@ -75,6 +76,7 @@ struct realm {
unsigned int ia_bits;
bool stage2_unmapped;
unsigned int mec_policy;
+ struct misc_cg *misc_cg;
};
/**
diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c
index 01ac8d4fec7c..d25d75744042 100644
--- a/arch/arm64/kvm/rmi.c
+++ b/arch/arm64/kvm/rmi.c
@@ -6,6 +6,7 @@
#include <uapi/linux/psci.h>
#include <linux/arm-rmi-cmds.h>
#include <linux/kvm_host.h>
+#include <linux/misc_cgroup.h>
#include <linux/overflow.h>
#include <linux/uaccess.h>
@@ -538,6 +539,36 @@ static int realm_init_sve_param(struct kvm *kvm, struct realm_params *params)
return 0;
}
+static int realm_mec_charge(struct realm *realm)
+{
+ struct misc_cg *misc_cg;
+ int ret;
+
+ if (realm->mec_policy != KVM_ARM_VM_RMI_MEC_POLICY_PRIVATE)
+ return 0;
+
+ misc_cg = get_current_misc_cg();
+
+ ret = misc_cg_try_charge(MISC_CG_RES_CCA_MEC, misc_cg, 1);
+ if (ret) {
+ put_misc_cg(misc_cg);
+ return ret;
+ }
+
+ realm->misc_cg = misc_cg;
+ return 0;
+}
+
+static void realm_mec_uncharge(struct realm *realm)
+{
+ if (!realm->misc_cg)
+ return;
+
+ misc_cg_uncharge(MISC_CG_RES_CCA_MEC, realm->misc_cg, 1);
+ put_misc_cg(realm->misc_cg);
+ realm->misc_cg = NULL;
+}
+
static int realm_create_rd(struct kvm *kvm)
{
struct realm *realm = &kvm->arch.realm;
@@ -594,12 +625,16 @@ static int realm_create_rd(struct kvm *kvm)
if (r)
goto out_undelegate_tables;
+ r = realm_mec_charge(realm);
+ if (r)
+ goto out_undelegate_tables;
+
params_phys = virt_to_phys(params);
rmi_ret = rmi_realm_create(rd_phys, params_phys, realm->sro);
if (rmi_ret) {
r = rmi_ret < 0 ? rmi_ret : -ENXIO;
- goto out_undelegate_tables;
+ goto out_uncharge_misc_cg;
}
realm->rd = rd;
@@ -607,6 +642,8 @@ static int realm_create_rd(struct kvm *kvm)
return 0;
+out_uncharge_misc_cg:
+ realm_mec_uncharge(realm);
out_undelegate_tables:
if (WARN_ON(rmi_undelegate_range(kvm->arch.mmu.pgd_phys,
top_delegated - kvm->arch.mmu.pgd_phys))) {
@@ -1727,10 +1764,15 @@ void kvm_destroy_realm(struct kvm *kvm)
if (WARN_ON(realm_destroy_rtts(kvm)))
return;
+ /*
+ * Keep the misc cgroup charge if RMI_REALM_DESTROY fails, as
+ * the corresponding MECID may still be allocated by the RMM.
+ */
if (WARN_ON(rmi_realm_destroy(rd_phys, realm->sro)))
return;
rmi_free_delegated_page(rd_phys);
realm->rd = NULL;
+ realm_mec_uncharge(realm);
}
if (WARN_ON(rmi_undelegate_range(kvm->arch.mmu.pgd_phys,
diff --git a/drivers/firmware/arm_rmm/rmi.c b/drivers/firmware/arm_rmm/rmi.c
index 27edf060d2d9..ba8a3e9a2603 100644
--- a/drivers/firmware/arm_rmm/rmi.c
+++ b/drivers/firmware/arm_rmm/rmi.c
@@ -7,6 +7,7 @@
#include <linux/cpufeature.h>
#include <linux/memblock.h>
#include <linux/memory.h>
+#include <linux/misc_cgroup.h>
#include <linux/mmzone.h>
#include <linux/slab.h>
@@ -1126,6 +1127,9 @@ static int __init arm64_init_rmi(void)
return ret;
}
+ WARN_ON_ONCE(misc_cg_set_capacity(MISC_CG_RES_CCA_MEC,
+ rmi_mec_count()));
+
/* Mark the RMM is usable */
WRITE_ONCE(arm64_rmi_is_available, true);
pr_info("RMI configured\n");
diff --git a/include/linux/misc_cgroup.h b/include/linux/misc_cgroup.h
index 0cb36a3ffc47..26131677170a 100644
--- a/include/linux/misc_cgroup.h
+++ b/include/linux/misc_cgroup.h
@@ -21,6 +21,10 @@ enum misc_res_type {
#ifdef CONFIG_INTEL_TDX_HOST
/** @MISC_CG_RES_TDX: Intel TDX HKIDs resource */
MISC_CG_RES_TDX,
+#endif
+#ifdef CONFIG_ARM_RMM_RMI
+ /** @MISC_CG_RES_CCA_MEC: ARM64 CCA MECIDs resource */
+ MISC_CG_RES_CCA_MEC,
#endif
/** @MISC_CG_RES_TYPES: count of enum misc_res_type constants */
MISC_CG_RES_TYPES
diff --git a/kernel/cgroup/misc.c b/kernel/cgroup/misc.c
index 4a9e2557141c..989316bcb5f0 100644
--- a/kernel/cgroup/misc.c
+++ b/kernel/cgroup/misc.c
@@ -28,6 +28,10 @@ static const char *const misc_res_name[] = {
/* Intel TDX HKIDs resource */
"tdx",
#endif
+#ifdef CONFIG_ARM_RMM_RMI
+ /* ARM64 CCA MECIDs resource */
+ "cca_mec",
+#endif
};
/* Root misc cgroup */
--
2.43.0