]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
arm_mpam: resctrl: Add CDP emulation
authorJames Morse <james.morse@arm.com>
Fri, 13 Mar 2026 14:45:57 +0000 (14:45 +0000)
committerJames Morse <james.morse@arm.com>
Fri, 27 Mar 2026 15:30:10 +0000 (15:30 +0000)
Intel RDT's CDP feature allows the cache to use a different control value
depending on whether the accesses was for instruction fetch or a data
access. MPAM's equivalent feature is the other way up: the CPU assigns a
different partid label to traffic depending on whether it was instruction
fetch or a data access, which causes the cache to use a different control
value based solely on the partid.

MPAM can emulate CDP, with the side effect that the alternative partid is
seen by all MSC, it can't be enabled per-MSC.

Add the resctrl hooks to turn this on or off. Add the helpers that match a
closid against a task, which need to be aware that the value written to
hardware is not the same as the one resctrl is using.

Update the 'arm64_mpam_global_default' variable the arch code uses during
context switch to know when the per-cpu value should be used instead. Also,
update these per-cpu values and sync the resulting mpam partid/pmg
configuration to hardware.

resctrl can enable CDP for L2 caches, L3 caches or both. When it is enabled
by one and not the other MPAM globally enabled CDP but hides the effect
on the other cache resource. This hiding is possible as CPOR is the only
supported cache control and that uses a resource bitmap; two partids with
the same bitmap act as one.

Awkwardly, the MB controls don't implement CDP and CDP can't be hidden as
the memory bandwidth control is a maximum per partid which can't be
modelled with more partids. If the total maximum is used for both the data
and instruction partids then then the maximum may be exceeded and if it is
split in two then the one using more bandwidth will hit a lower
limit. Hence, hide the MB controls completely if CDP is enabled for any
resource.

Tested-by: Gavin Shan <gshan@redhat.com>
Tested-by: Shaopeng Tan <tan.shaopeng@jp.fujitsu.com>
Tested-by: Peter Newman <peternewman@google.com>
Tested-by: Zeng Heng <zengheng4@huawei.com>
Tested-by: Punit Agrawal <punit.agrawal@oss.qualcomm.com>
Tested-by: Jesse Chick <jessechick@os.amperecomputing.com>
Cc: Dave Martin <Dave.Martin@arm.com>
Cc: Amit Singh Tomar <amitsinght@marvell.com>
Reviewed-by: Zeng Heng <zengheng4@huawei.com>
Reviewed-by: Shaopeng Tan <tan.shaopeng@jp.fujitsu.com>
Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com>
Reviewed-by: Gavin Shan <gshan@redhat.com>
Co-developed-by: Ben Horgan <ben.horgan@arm.com>
Signed-off-by: Ben Horgan <ben.horgan@arm.com>
Signed-off-by: James Morse <james.morse@arm.com>
arch/arm64/include/asm/mpam.h
drivers/resctrl/mpam_internal.h
drivers/resctrl/mpam_resctrl.c
include/linux/arm_mpam.h

index 05aa71200f61a6ad8ccd9470bf3012762f95c30d..70d396e7b6da857753e68851c196834890eb956d 100644 (file)
@@ -4,6 +4,7 @@
 #ifndef __ASM__MPAM_H
 #define __ASM__MPAM_H
 
+#include <linux/arm_mpam.h>
 #include <linux/bitfield.h>
 #include <linux/jump_label.h>
 #include <linux/percpu.h>
index f063a741aaba20001a9a909d87e85bd9e43e47da..2751eeaba302dbd9832bd7a384e468f28f265536 100644 (file)
@@ -342,6 +342,7 @@ struct mpam_resctrl_dom {
 struct mpam_resctrl_res {
        struct mpam_class       *class;
        struct rdt_resource     resctrl_res;
+       bool                    cdp_enabled;
 };
 
 static inline int mpam_alloc_csu_mon(struct mpam_class *class)
index 9cde5b7e644ccb5d1f8fd58e29c78d516f1bf921..2111542f485e17ff1a3398a4f81f472fb42b1e5d 100644 (file)
@@ -35,6 +35,10 @@ static struct mpam_resctrl_res mpam_resctrl_controls[RDT_NUM_RESOURCES];
 /* The lock for modifying resctrl's domain lists from cpuhp callbacks. */
 static DEFINE_MUTEX(domain_list_lock);
 
+/*
+ * MPAM emulates CDP by setting different PARTID in the I/D fields of MPAM0_EL1.
+ * This applies globally to all traffic the CPU generates.
+ */
 static bool cdp_enabled;
 
 bool resctrl_arch_alloc_capable(void)
@@ -50,6 +54,74 @@ bool resctrl_arch_alloc_capable(void)
        return false;
 }
 
+bool resctrl_arch_get_cdp_enabled(enum resctrl_res_level rid)
+{
+       return mpam_resctrl_controls[rid].cdp_enabled;
+}
+
+/**
+ * resctrl_reset_task_closids() - Reset the PARTID/PMG values for all tasks.
+ *
+ * At boot, all existing tasks use partid zero for D and I.
+ * To enable/disable CDP emulation, all these tasks need relabelling.
+ */
+static void resctrl_reset_task_closids(void)
+{
+       struct task_struct *p, *t;
+
+       read_lock(&tasklist_lock);
+       for_each_process_thread(p, t) {
+               resctrl_arch_set_closid_rmid(t, RESCTRL_RESERVED_CLOSID,
+                                            RESCTRL_RESERVED_RMID);
+       }
+       read_unlock(&tasklist_lock);
+}
+
+int resctrl_arch_set_cdp_enabled(enum resctrl_res_level rid, bool enable)
+{
+       u32 partid_i = RESCTRL_RESERVED_CLOSID, partid_d = RESCTRL_RESERVED_CLOSID;
+       int cpu;
+
+       /*
+        * resctrl_arch_set_cdp_enabled() is only called with enable set to
+        * false on error and unmount.
+        */
+       cdp_enabled = enable;
+       mpam_resctrl_controls[rid].cdp_enabled = enable;
+
+       /* The mbw_max feature can't hide cdp as it's a per-partid maximum. */
+       if (cdp_enabled && !mpam_resctrl_controls[RDT_RESOURCE_MBA].cdp_enabled)
+               mpam_resctrl_controls[RDT_RESOURCE_MBA].resctrl_res.alloc_capable = false;
+
+       if (mpam_resctrl_controls[RDT_RESOURCE_MBA].cdp_enabled &&
+           mpam_resctrl_controls[RDT_RESOURCE_MBA].class)
+               mpam_resctrl_controls[RDT_RESOURCE_MBA].resctrl_res.alloc_capable = true;
+
+       if (enable) {
+               if (mpam_partid_max < 1)
+                       return -EINVAL;
+
+               partid_d = resctrl_get_config_index(RESCTRL_RESERVED_CLOSID, CDP_DATA);
+               partid_i = resctrl_get_config_index(RESCTRL_RESERVED_CLOSID, CDP_CODE);
+       }
+
+       mpam_set_task_partid_pmg(current, partid_d, partid_i, 0, 0);
+       WRITE_ONCE(arm64_mpam_global_default, mpam_get_regval(current));
+
+       resctrl_reset_task_closids();
+
+       for_each_possible_cpu(cpu)
+               mpam_set_cpu_defaults(cpu, partid_d, partid_i, 0, 0);
+       on_each_cpu(resctrl_arch_sync_cpu_closid_rmid, NULL, 1);
+
+       return 0;
+}
+
+static bool mpam_resctrl_hide_cdp(enum resctrl_res_level rid)
+{
+       return cdp_enabled && !resctrl_arch_get_cdp_enabled(rid);
+}
+
 /*
  * MSC may raise an error interrupt if it sees an out or range partid/pmg,
  * and go on to truncate the value. Regardless of what the hardware supports,
@@ -115,6 +187,30 @@ void resctrl_arch_set_closid_rmid(struct task_struct *tsk, u32 closid, u32 rmid)
        }
 }
 
+bool resctrl_arch_match_closid(struct task_struct *tsk, u32 closid)
+{
+       u64 regval = mpam_get_regval(tsk);
+       u32 tsk_closid = FIELD_GET(MPAM0_EL1_PARTID_D, regval);
+
+       if (cdp_enabled)
+               tsk_closid >>= 1;
+
+       return tsk_closid == closid;
+}
+
+/* The task's pmg is not unique, the partid must be considered too */
+bool resctrl_arch_match_rmid(struct task_struct *tsk, u32 closid, u32 rmid)
+{
+       u64 regval = mpam_get_regval(tsk);
+       u32 tsk_closid = FIELD_GET(MPAM0_EL1_PARTID_D, regval);
+       u32 tsk_rmid = FIELD_GET(MPAM0_EL1_PMG_D, regval);
+
+       if (cdp_enabled)
+               tsk_closid >>= 1;
+
+       return (tsk_closid == closid) && (tsk_rmid == rmid);
+}
+
 struct rdt_resource *resctrl_arch_get_resource(enum resctrl_res_level l)
 {
        if (l >= RDT_NUM_RESOURCES)
@@ -247,6 +343,14 @@ u32 resctrl_arch_get_config(struct rdt_resource *r, struct rdt_ctrl_domain *d,
        dom = container_of(d, struct mpam_resctrl_dom, resctrl_ctrl_dom);
        cprops = &res->class->props;
 
+       /*
+        * When CDP is enabled, but the resource doesn't support it,
+        * the control is cloned across both partids.
+        * Pick one at random to read:
+        */
+       if (mpam_resctrl_hide_cdp(r->rid))
+               type = CDP_DATA;
+
        partid = resctrl_get_config_index(closid, type);
        cfg = &dom->ctrl_comp->cfg[partid];
 
@@ -274,6 +378,7 @@ u32 resctrl_arch_get_config(struct rdt_resource *r, struct rdt_ctrl_domain *d,
 int resctrl_arch_update_one(struct rdt_resource *r, struct rdt_ctrl_domain *d,
                            u32 closid, enum resctrl_conf_type t, u32 cfg_val)
 {
+       int err;
        u32 partid;
        struct mpam_config cfg;
        struct mpam_props *cprops;
@@ -291,6 +396,9 @@ int resctrl_arch_update_one(struct rdt_resource *r, struct rdt_ctrl_domain *d,
        dom = container_of(d, struct mpam_resctrl_dom, resctrl_ctrl_dom);
        cprops = &res->class->props;
 
+       if (mpam_resctrl_hide_cdp(r->rid))
+               t = CDP_DATA;
+
        partid = resctrl_get_config_index(closid, t);
        if (!r->alloc_capable || partid >= resctrl_arch_get_num_closid(r)) {
                pr_debug("Not alloc capable or computed PARTID out of range\n");
@@ -313,6 +421,20 @@ int resctrl_arch_update_one(struct rdt_resource *r, struct rdt_ctrl_domain *d,
                return -EINVAL;
        }
 
+       /*
+        * When CDP is enabled, but the resource doesn't support it, we need to
+        * apply the same configuration to the other partid.
+        */
+       if (mpam_resctrl_hide_cdp(r->rid)) {
+               partid = resctrl_get_config_index(closid, CDP_CODE);
+               err = mpam_apply_config(dom->ctrl_comp, partid, &cfg);
+               if (err)
+                       return err;
+
+               partid = resctrl_get_config_index(closid, CDP_DATA);
+               return mpam_apply_config(dom->ctrl_comp, partid, &cfg);
+       }
+
        return mpam_apply_config(dom->ctrl_comp, partid, &cfg);
 }
 
index 5a78299ec464b9014f598c614963918d4368cdcc..d329b1dc148babd0278d4893d6fab3ab1f39f4a9 100644 (file)
@@ -56,6 +56,8 @@ void resctrl_arch_set_cpu_default_closid(int cpu, u32 closid);
 void resctrl_arch_set_closid_rmid(struct task_struct *tsk, u32 closid, u32 rmid);
 void resctrl_arch_set_cpu_default_closid_rmid(int cpu, u32 closid, u32 rmid);
 void resctrl_arch_sched_in(struct task_struct *tsk);
+bool resctrl_arch_match_closid(struct task_struct *tsk, u32 closid);
+bool resctrl_arch_match_rmid(struct task_struct *tsk, u32 closid, u32 rmid);
 
 /**
  * mpam_register_requestor() - Register a requestor with the MPAM driver