[PATCH 10/12] fs/resctrl: Extract the counter assignment mode switch

From: Jinjie Ruan

Date: Sun Nov 29 2026 - 22:25:34 EST


resctrl_mbm_assign_mode_write() switches the counter assignment mode
inline: the call to the architecture, and the follow-up work of
resetting the event configuration, the assignment on mkdir, and the
counter and RMID state, which differs between the modes. The MBA
software controller needs to switch the mode too, with the same
follow-up work.

Move the switching into a resctrl_mbm_assign_mode_set() helper, and
call it from the write path. The helper does nothing when the mode is
already the requested one. The visibility of the BMEC files is left to
the caller: the files only exist while resctrl is mounted, and the
mount switches the mode before the files are created.

No functional change.

Signed-off-by: Jinjie Ruan <ruanjinjie@xxxxxxxxxx>
---
fs/resctrl/internal.h | 2 ++
fs/resctrl/monitor.c | 62 ++++++++++++++++++++++++++-----------------
2 files changed, 40 insertions(+), 24 deletions(-)

diff --git a/fs/resctrl/internal.h b/fs/resctrl/internal.h
index e62a277dee85..79e2ad4e6ead 100644
--- a/fs/resctrl/internal.h
+++ b/fs/resctrl/internal.h
@@ -423,6 +423,8 @@ void *rdt_kn_parent_priv(struct kernfs_node *kn);

int resctrl_mbm_assign_mode_show(struct kernfs_open_file *of, struct seq_file *s, void *v);

+int resctrl_mbm_assign_mode_set(struct rdt_resource *r, bool enable);
+
ssize_t resctrl_mbm_assign_mode_write(struct kernfs_open_file *of, char *buf,
size_t nbytes, loff_t off);

diff --git a/fs/resctrl/monitor.c b/fs/resctrl/monitor.c
index b9896a0df35b..8ca9477ed825 100644
--- a/fs/resctrl/monitor.c
+++ b/fs/resctrl/monitor.c
@@ -1583,11 +1583,47 @@ int resctrl_mbm_assign_mode_show(struct kernfs_open_file *of,
return 0;
}

+int resctrl_mbm_assign_mode_set(struct rdt_resource *r, bool enable)
+{
+ struct rdt_l3_mon_domain *d;
+ int ret;
+
+ if (enable == resctrl_arch_mbm_cntr_assign_enabled(r))
+ return 0;
+
+ ret = resctrl_arch_mbm_cntr_assign_set(r, enable);
+ if (ret)
+ return ret;
+
+ /*
+ * Initialize the default memory transaction values for
+ * total and local events.
+ */
+ if (resctrl_is_mon_event_enabled(QOS_L3_MBM_TOTAL_EVENT_ID))
+ mon_event_all[QOS_L3_MBM_TOTAL_EVENT_ID].evt_cfg = r->mon.mbm_cfg_mask;
+ if (resctrl_is_mon_event_enabled(QOS_L3_MBM_LOCAL_EVENT_ID))
+ mon_event_all[QOS_L3_MBM_LOCAL_EVENT_ID].evt_cfg = r->mon.mbm_cfg_mask &
+ (READS_TO_LOCAL_MEM |
+ READS_TO_LOCAL_S_MEM |
+ NON_TEMP_WRITE_TO_LOCAL_MEM);
+ /* Enable auto assignment when switching to "mbm_event" mode */
+ if (enable)
+ r->mon.mbm_assign_on_mkdir = true;
+ /*
+ * Reset all the non-achitectural RMID state and assignable counters.
+ */
+ list_for_each_entry_rcu(d, &r->mon_domains, hdr.list, lockdep_is_cpus_held()) {
+ mbm_cntr_free_all(r, d);
+ resctrl_reset_rmid_all(r, d);
+ }
+
+ return 0;
+}
+
ssize_t resctrl_mbm_assign_mode_write(struct kernfs_open_file *of, char *buf,
size_t nbytes, loff_t off)
{
struct rdt_resource *r = rdt_kn_parent_priv(of->kn);
- struct rdt_l3_mon_domain *d;
int ret = 0;
bool enable;

@@ -1628,34 +1664,12 @@ ssize_t resctrl_mbm_assign_mode_write(struct kernfs_open_file *of, char *buf,
goto out_unlock;
}

- ret = resctrl_arch_mbm_cntr_assign_set(r, enable);
+ ret = resctrl_mbm_assign_mode_set(r, enable);
if (ret)
goto out_unlock;

/* Update the visibility of BMEC related files */
resctrl_bmec_files_show(r, NULL, !enable);
-
- /*
- * Initialize the default memory transaction values for
- * total and local events.
- */
- if (resctrl_is_mon_event_enabled(QOS_L3_MBM_TOTAL_EVENT_ID))
- mon_event_all[QOS_L3_MBM_TOTAL_EVENT_ID].evt_cfg = r->mon.mbm_cfg_mask;
- if (resctrl_is_mon_event_enabled(QOS_L3_MBM_LOCAL_EVENT_ID))
- mon_event_all[QOS_L3_MBM_LOCAL_EVENT_ID].evt_cfg = r->mon.mbm_cfg_mask &
- (READS_TO_LOCAL_MEM |
- READS_TO_LOCAL_S_MEM |
- NON_TEMP_WRITE_TO_LOCAL_MEM);
- /* Enable auto assignment when switching to "mbm_event" mode */
- if (enable)
- r->mon.mbm_assign_on_mkdir = true;
- /*
- * Reset all the non-achitectural RMID state and assignable counters.
- */
- list_for_each_entry_rcu(d, &r->mon_domains, hdr.list, lockdep_is_cpus_held()) {
- mbm_cntr_free_all(r, d);
- resctrl_reset_rmid_all(r, d);
- }
}

out_unlock:
--
2.34.1