Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

arm64: mpam: Context switch the MPAM registers

MPAM allows traffic in the SoC to be labeled by the OS, these labels are
used to apply policy in caches and bandwidth regulators, and to monitor
traffic in the SoC. The label is made up of a PARTID and PMG value. The x86
equivalent calls these CLOSID and RMID, but they don't map precisely.

MPAM has two CPU system registers that is used to hold the PARTID and PMG
values that traffic generated at each exception level will use. These can
be set per-task by the resctrl file system. (resctrl is the defacto
interface for controlling this stuff).

Add a helper to switch this.

struct task_struct's separate CLOSID and RMID fields are insufficient to
implement resctrl using MPAM, as resctrl can change the PARTID (CLOSID) and
PMG (sort of like the RMID) separately. On x86, the rmid is an independent
number, so a race that writes a mismatched closid and rmid into hardware is
benign. On arm64, the pmg bits extend the partid.
(i.e. partid-5 has a pmg-0 that is not the same as partid-6's pmg-0). In
this case, mismatching the values will 'dirty' a pmg value that resctrl
believes is clean, and is not tracking with its 'limbo' code.

To avoid this, the partid and pmg are always read and written as a
pair. This requires a new u64 field. In struct task_struct there are two
u32, rmid and closid for the x86 case, but as we can't use them here do
something else. Add this new field, mpam_partid_pmg, to struct thread_info
to avoid adding more architecture specific code to struct task_struct.
Always use READ_ONCE()/WRITE_ONCE() when accessing this field.

Resctrl allows a per-cpu 'default' value to be set, this overrides the
values when scheduling a task in the default control-group, which has
PARTID 0. The way 'code data prioritisation' gets emulated means the
register value for the default group needs to be a variable.

The current system register value is kept in a per-cpu variable to avoid
writing to the system register if the value isn't going to change. Writes
to this register may reset the hardware state for regulating bandwidth.

Finally, there is no reason to context switch these registers unless there
is a driver changing the values in struct task_struct. Hide the whole thing
behind a static key. This also allows the driver to disable MPAM in
response to errors reported by hardware. Move the existing static key to
belong to the arch code, as in the future the MPAM driver may become a
loadable module.

All this should depend on whether there is an MPAM driver, hide it behind
CONFIG_ARM64_MPAM.

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: 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>
Reviewed-by: Catalin Marinas <catalin.marinas@arm.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>

+95 -4
+2
arch/arm64/Kconfig
··· 2039 2039 2040 2040 MPAM is exposed to user-space via the resctrl pseudo filesystem. 2041 2041 2042 + This option enables the extra context switch code. 2043 + 2042 2044 endmenu # "ARMv8.4 architectural features" 2043 2045 2044 2046 menu "ARMv8.5 architectural features"
+67
arch/arm64/include/asm/mpam.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + /* Copyright (C) 2025 Arm Ltd. */ 3 + 4 + #ifndef __ASM__MPAM_H 5 + #define __ASM__MPAM_H 6 + 7 + #include <linux/jump_label.h> 8 + #include <linux/percpu.h> 9 + #include <linux/sched.h> 10 + 11 + #include <asm/sysreg.h> 12 + 13 + DECLARE_STATIC_KEY_FALSE(mpam_enabled); 14 + DECLARE_PER_CPU(u64, arm64_mpam_default); 15 + DECLARE_PER_CPU(u64, arm64_mpam_current); 16 + 17 + /* 18 + * The value of the MPAM0_EL1 sysreg when a task is in resctrl's default group. 19 + * This is used by the context switch code to use the resctrl CPU property 20 + * instead. The value is modified when CDP is enabled/disabled by mounting 21 + * the resctrl filesystem. 22 + */ 23 + extern u64 arm64_mpam_global_default; 24 + 25 + /* 26 + * The resctrl filesystem writes to the partid/pmg values for threads and CPUs, 27 + * which may race with reads in mpam_thread_switch(). Ensure only one of the old 28 + * or new values are used. Particular care should be taken with the pmg field as 29 + * mpam_thread_switch() may read a partid and pmg that don't match, causing this 30 + * value to be stored with cache allocations, despite being considered 'free' by 31 + * resctrl. 32 + */ 33 + #ifdef CONFIG_ARM64_MPAM 34 + static inline u64 mpam_get_regval(struct task_struct *tsk) 35 + { 36 + return READ_ONCE(task_thread_info(tsk)->mpam_partid_pmg); 37 + } 38 + 39 + static inline void mpam_thread_switch(struct task_struct *tsk) 40 + { 41 + u64 oldregval; 42 + int cpu = smp_processor_id(); 43 + u64 regval = mpam_get_regval(tsk); 44 + 45 + if (!static_branch_likely(&mpam_enabled)) 46 + return; 47 + 48 + if (regval == READ_ONCE(arm64_mpam_global_default)) 49 + regval = READ_ONCE(per_cpu(arm64_mpam_default, cpu)); 50 + 51 + oldregval = READ_ONCE(per_cpu(arm64_mpam_current, cpu)); 52 + if (oldregval == regval) 53 + return; 54 + 55 + write_sysreg_s(regval | MPAM1_EL1_MPAMEN, SYS_MPAM1_EL1); 56 + isb(); 57 + 58 + /* Synchronising the EL0 write is left until the ERET to EL0 */ 59 + write_sysreg_s(regval, SYS_MPAM0_EL1); 60 + 61 + WRITE_ONCE(per_cpu(arm64_mpam_current, cpu), regval); 62 + } 63 + #else 64 + static inline void mpam_thread_switch(struct task_struct *tsk) {} 65 + #endif /* CONFIG_ARM64_MPAM */ 66 + 67 + #endif /* __ASM__MPAM_H */
+3
arch/arm64/include/asm/thread_info.h
··· 42 42 void *scs_base; 43 43 void *scs_sp; 44 44 #endif 45 + #ifdef CONFIG_ARM64_MPAM 46 + u64 mpam_partid_pmg; 47 + #endif 45 48 u32 cpu; 46 49 }; 47 50
+1
arch/arm64/kernel/Makefile
··· 67 67 obj-$(CONFIG_VMCORE_INFO) += vmcore_info.o 68 68 obj-$(CONFIG_ARM_SDE_INTERFACE) += sdei.o 69 69 obj-$(CONFIG_ARM64_PTR_AUTH) += pointer_auth.o 70 + obj-$(CONFIG_ARM64_MPAM) += mpam.o 70 71 obj-$(CONFIG_ARM64_MTE) += mte.o 71 72 obj-y += vdso-wrap.o 72 73 obj-$(CONFIG_COMPAT_VDSO) += vdso32-wrap.o
+13
arch/arm64/kernel/mpam.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Copyright (C) 2025 Arm Ltd. */ 3 + 4 + #include <asm/mpam.h> 5 + 6 + #include <linux/jump_label.h> 7 + #include <linux/percpu.h> 8 + 9 + DEFINE_STATIC_KEY_FALSE(mpam_enabled); 10 + DEFINE_PER_CPU(u64, arm64_mpam_default); 11 + DEFINE_PER_CPU(u64, arm64_mpam_current); 12 + 13 + u64 arm64_mpam_global_default;
+7
arch/arm64/kernel/process.c
··· 51 51 #include <asm/fpsimd.h> 52 52 #include <asm/gcs.h> 53 53 #include <asm/mmu_context.h> 54 + #include <asm/mpam.h> 54 55 #include <asm/mte.h> 55 56 #include <asm/processor.h> 56 57 #include <asm/pointer_auth.h> ··· 738 737 /* avoid expensive SCTLR_EL1 accesses if no change */ 739 738 if (prev->thread.sctlr_user != next->thread.sctlr_user) 740 739 update_sctlr_el1(next->thread.sctlr_user); 740 + 741 + /* 742 + * MPAM thread switch happens after the DSB to ensure prev's accesses 743 + * use prev's MPAM settings. 744 + */ 745 + mpam_thread_switch(next); 741 746 742 747 /* the actual thread switch */ 743 748 last = cpu_switch_to(prev, next);
-2
drivers/resctrl/mpam_devices.c
··· 29 29 30 30 #include "mpam_internal.h" 31 31 32 - DEFINE_STATIC_KEY_FALSE(mpam_enabled); /* This moves to arch code */ 33 - 34 32 /* 35 33 * mpam_list_lock protects the SRCU lists when writing. Once the 36 34 * mpam_enabled key is enabled these lists are read-only,
+2 -2
drivers/resctrl/mpam_internal.h
··· 16 16 #include <linux/srcu.h> 17 17 #include <linux/types.h> 18 18 19 + #include <asm/mpam.h> 20 + 19 21 #define MPAM_MSC_MAX_NUM_RIS 16 20 22 21 23 struct platform_device; 22 - 23 - DECLARE_STATIC_KEY_FALSE(mpam_enabled); 24 24 25 25 #ifdef CONFIG_MPAM_KUNIT_TEST 26 26 #define PACKED_FOR_KUNIT __packed