x86,fs/resctrl: Fix long-standing issues
Subsystems (2)
Reinette Chatre
maintainerv4 cover x86,fs/resctrl: Fix long-standing issues
v3: https://lore.kernel.org/lkml/[email protected]/
v2: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
While reviewing the AET series [1] Sashiko reported a deadlock during mount, and a use-after-free when an L3 domain is removed during CPU offline. More issues were uncovered as fixes were developed and reviewed. While the goal is to fix all issues the races surrounding pseudo-locked regions are not yet solved and have been removed from this version of fixes.
Applies against tip/master to ensure it considers pending x86/cache changes.
Changes since V3:
- Drop majority of pseudo-locking fixes, only keep the double free/double list add fix.
- Add patch to help document safe RCU list traversal.
- See individual patches for detailed changes.
[1] https://sashiko.dev/#/patchset/20260429184858.36423-1-tony.luck%40intel.com
Reinette Chatre (7):
x86,fs/resctrl: Document safe RCU list traversal fs/resctrl: Fix deadlock on errors during mount fs/resctrl: Prevent use-after-free in rdtgroup_kn_put() fs/resctrl: Fix double-add of pseudo-locked region's RMID to free list fs/resctrl: Prevent deadlock and use-after-free in info file handlers x86/resctrl: Ensure domain fully initialized before placed on RCU list fs/resctrl: Fix UAF from worker threads when domains are removed
Tony Luck (3):
fs/resctrl: Move functions to avoid forward references in subsequent
fixes
fs/resctrl: Free mon_data structures on rdt_get_tree() failure fs/resctrl: Fix use-after-free during unmount
arch/x86/kernel/cpu/resctrl/core.c | 18 +- arch/x86/kernel/cpu/resctrl/ctrlmondata.c | 4 +- arch/x86/kernel/cpu/resctrl/intel_aet.c | 5 +- arch/x86/kernel/cpu/resctrl/monitor.c | 2 +- arch/x86/kernel/cpu/resctrl/rdtgroup.c | 4 +- fs/resctrl/ctrlmondata.c | 50 +- fs/resctrl/internal.h | 3 +- fs/resctrl/monitor.c | 123 ++-- fs/resctrl/pseudo_lock.c | 2 +- fs/resctrl/rdtgroup.c | 859 ++++++++++++++-------- 10 files changed, 663 insertions(+), 407 deletions(-)
Signature · 1 line
2.50.1
Reinette Chatre
maintainerPATCH v4 1/10 x86,fs/resctrl: Document safe RCU list traversal
rdt_resource::ctrl_domains and rdt_resource::mon_domains are RCU lists with entries added and removed by architecture from CPU hotplug callbacks that are run with cpus_write_lock() held. These lists can be traversed safely from resctrl fs by either holding cpus_read_lock() or relying on an RCU read-side critical section.
resctrl fs traversals of rdt_resource::ctrl_domains and rdt_resource::mon_domains are done using list_for_each_entry() with cpus_read_lock() held. Similarly, x86 architecture callbacks use list_for_each_entry() expecting that resctrl fs makes the call with cpus_read_lock() held. Inconsistently, a lockdep_assert_cpus_held() precedes the list_for_each_entry() call with varying distance to document this safe RCU list traversal.
In preparation for an upcoming traversal of rdt_resource::ctrl_domains that needs to be done from RCU read-side critical section there is a requirement for developers to always know exactly in which context the list is being traversed.
Replace the list_for_each_entry() traversals of RCU list with list_for_each_entry_rcu() to document that an RCU list is being traversed while making use of the built-in lockdep expression that additionally documents that it is cpus_read_lock() that enables the list to be traversed from non-RCU protection. Only revert to documenting the safety of traversal using a comment when lockdep does not have needed visibility in functions called via smp_call*().
The lockdep expression within list_for_each_entry_rcu() depends on RCU_EXPERT that is not set in a typical debug kernel so keep the existing lockdep_assert_cpus_held() that is active with CONFIG_LOCKDEP=y found in typical debug kernel.
---
Changes since v3:
- New patch.
--- arch/x86/kernel/cpu/resctrl/ctrlmondata.c | 4 ++-- arch/x86/kernel/cpu/resctrl/monitor.c | 2 +- arch/x86/kernel/cpu/resctrl/rdtgroup.c | 4 ++-- fs/resctrl/ctrlmondata.c | 12 +++++++----- fs/resctrl/monitor.c | 23 +++++++++++++--------- fs/resctrl/pseudo_lock.c | 2 +- fs/resctrl/rdtgroup.c | 24 +++++++++++------------ 7 files changed, 39 insertions(+), 32 deletions(-)
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +2 -2
@@ -53,7 +53,7 @@ int resctrl_arch_update_domains(struct rdt_resource *r, u32 closid)/* Walking r->domains, ensure it can't race with cpuhp */ lockdep_assert_cpus_held (); -list_for_each_entry (d, &r->ctrl_domains, hdr.list) { +list_for_each_entry_rcu (d, &r->ctrl_domains, hdr.list,lockdep_is_cpus_held ()) { hw_dom =resctrl_to_arch_ctrl_dom (d); msr_param.res =NULL ;for (t =0 ; t <CDP_NUM_TYPES ; t++) {
@@ -115,7 +115,7 @@ static void _resctrl_sdciae_enable(struct rdt_resource *r, bool enable)lockdep_assert_cpus_held ();/* Update MSR_IA32_L3_QOS_EXT_CFG MSR on all the CPUs in all domains */ -list_for_each_entry (d, &r->ctrl_domains, hdr.list) +list_for_each_entry_rcu (d, &r->ctrl_domains, hdr.list,lockdep_is_cpus_held ())on_each_cpu_mask (&d->hdr.cpu_mask, resctrl_sdciae_set_one_amd, &enable,1 ); }
arch/x86/kernel/cpu/resctrl/monitor.c +1 -1
@@ -500,7 +500,7 @@ static void _resctrl_abmc_enable(struct rdt_resource *r, bool enable)lockdep_assert_cpus_held (); -list_for_each_entry (d, &r->mon_domains, hdr.list) { +list_for_each_entry_rcu (d, &r->mon_domains, hdr.list,lockdep_is_cpus_held ()) {on_each_cpu_mask (&d->hdr.cpu_mask, resctrl_abmc_set_one_amd, &enable,1 );resctrl_arch_reset_rmid_all (r, d);
arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -2
@@ -151,7 +151,7 @@ static int set_cache_qos_cfg(int level, bool enable)return -ENOMEM ; r_l = &rdt_resources_all[level].r_resctrl; -list_for_each_entry (d, &r_l->ctrl_domains, hdr.list) { +list_for_each_entry_rcu (d, &r_l->ctrl_domains, hdr.list,lockdep_is_cpus_held ()) {if (r_l->cache.arch_has_per_cpu_cfg)/* Pick all the CPUs in the domain instance */ for_each_cpu (cpu, &d->hdr.cpu_mask)
@@ -249,7 +249,7 @@ void resctrl_arch_reset_all_ctrls(struct rdt_resource *r)* CBMs in all ctrl_domains to the maximum mask value. Pick one CPU * from each domain to update the MSRs below. */ -list_for_each_entry (d, &r->ctrl_domains, hdr.list) { +list_for_each_entry_rcu (d, &r->ctrl_domains, hdr.list,lockdep_is_cpus_held ()) { hw_dom =resctrl_to_arch_ctrl_dom (d);for (i =0 ; i < hw_res->num_closid; i++)
fs/resctrl/ctrlmondata.c +7 -5
@@ -261,7 +261,7 @@ static int parse_line(char *line, struct resctrl_schema *s,return -EINVAL ; } dom =strim (dom); -list_for_each_entry (d, &r->ctrl_domains, hdr.list) { +list_for_each_entry_rcu (d, &r->ctrl_domains, hdr.list,lockdep_is_cpus_held ()) {if (d->hdr.id == dom_id) { data.buf = dom; data.closid = rdtgrp->closid;
@@ -397,7 +397,7 @@ static void show_doms(struct seq_file *s, struct resctrl_schema *schema,if (resource_name)seq_printf (s,"%*s:" , max_name_width, resource_name); -list_for_each_entry (dom, &r->ctrl_domains, hdr.list) { +list_for_each_entry_rcu (dom, &r->ctrl_domains, hdr.list,lockdep_is_cpus_held ()) {if (sep)seq_puts (s,";" );
@@ -535,6 +535,8 @@ struct rdt_domain_hdr *resctrl_find_domain(struct list_head *h, int id,struct rdt_domain_hdr *d;struct list_head *l; +lockdep_assert_cpus_held (); +list_for_each (l, h) { d = list_entry(l, struct rdt_domain_hdr, list);/* When id is found, return its domain. */
@@ -717,7 +719,7 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)* struct mon_data. Search all domains in the resource for * one that matches this cache id. */ -list_for_each_entry (d, &r->mon_domains, hdr.list) { +list_for_each_entry_rcu (d, &r->mon_domains, hdr.list,lockdep_is_cpus_held ()) {if (d->ci_id == domid) { cpu =cpumask_any (&d->hdr.cpu_mask); ci =get_cpu_cacheinfo_level (cpu,RESCTRL_L3_CACHE );
@@ -817,7 +819,7 @@ static int resctrl_io_alloc_init_cbm(struct resctrl_schema *s, u32 closid)/* Keep CDP_CODE and CDP_DATA of io_alloc CLOSID's CBM in sync. */ if (resctrl_arch_get_cdp_enabled (r->rid)) { peer_type =resctrl_peer_type (s->conf_type); -list_for_each_entry (d, &s->res->ctrl_domains, hdr.list) +list_for_each_entry_rcu (d, &s->res->ctrl_domains, hdr.list,lockdep_is_cpus_held ())memcpy (&d->staged_config[peer_type], &d->staged_config[s->conf_type],sizeof (d->staged_config[0 ]));
@@ -980,7 +982,7 @@ static int resctrl_io_alloc_parse_line(char *line, struct rdt_resource *r, } dom =strim (dom); -list_for_each_entry (d, &r->ctrl_domains, hdr.list) { +list_for_each_entry_rcu (d, &r->ctrl_domains, hdr.list,lockdep_is_cpus_held ()) {if (update_all || d->hdr.id == dom_id) { data.buf = dom; data.mode =RDT_MODE_SHAREABLE ;
fs/resctrl/monitor.c +14 -9
@@ -304,7 +304,7 @@ static void add_rmid_to_limbo(struct rmid_entry *entry) idx =resctrl_arch_rmid_idx_encode (entry->closid, entry->rmid); entry->busy =0 ; -list_for_each_entry (d, &r->mon_domains, hdr.list) { +list_for_each_entry_rcu (d, &r->mon_domains, hdr.list,lockdep_is_cpus_held ()) {/* * For the first limbo RMID in the domain, * setup up the limbo worker.
@@ -502,6 +502,11 @@ static int __l3_mon_event_count_sum(struct rdtgroup *rdtgrp, struct rmid_read *r* all domains fail for any reason. */ ret = -EINVAL ; +/* +* RCU list being traversed with CPU hotplug lock held. lockdep +* unable to help prove this here since this work is scheduled via +* smp_call*(). Not called from MBM overflow handler. +*/ list_for_each_entry (d, &rr->r->mon_domains, hdr.list) {if (d->ci_id != rr->ci->id)continue ;
@@ -1226,7 +1231,7 @@ static int rdtgroup_assign_cntr_event(struct rdt_l3_mon_domain *d, struct rdtgroint ret =0 ;if (!d) { -list_for_each_entry (d, &r->mon_domains, hdr.list) { +list_for_each_entry_rcu (d, &r->mon_domains, hdr.list,lockdep_is_cpus_held ()) {int err; err =rdtgroup_alloc_assign_cntr (r, d, rdtgrp, mevt);
@@ -1298,7 +1303,7 @@ static void rdtgroup_unassign_cntr_event(struct rdt_l3_mon_domain *d, struct rdtstruct rdt_resource *r =resctrl_arch_get_resource (mevt->rid);if (!d) { -list_for_each_entry (d, &r->mon_domains, hdr.list) +list_for_each_entry_rcu (d, &r->mon_domains, hdr.list,lockdep_is_cpus_held ())rdtgroup_free_unassign_cntr (r, d, rdtgrp, mevt); }else {rdtgroup_free_unassign_cntr (r, d, rdtgrp, mevt);
@@ -1370,7 +1375,7 @@ static void rdtgroup_update_cntr_event(struct rdt_resource *r, struct rdtgroup *struct rdt_l3_mon_domain *d;int cntr_id; -list_for_each_entry (d, &r->mon_domains, hdr.list) { +list_for_each_entry_rcu (d, &r->mon_domains, hdr.list,lockdep_is_cpus_held ()) { cntr_id =mbm_cntr_get (r, d, rdtgrp, evtid);if (cntr_id >=0 ) rdtgroup_assign_cntr(r, d, evtid, rdtgrp->mon.rmid,
@@ -1540,7 +1545,7 @@ ssize_t resctrl_mbm_assign_mode_write(struct kernfs_open_file *of, char *buf,/* * Reset all the non-achitectural RMID state and assignable counters. */ -list_for_each_entry (d, &r->mon_domains, hdr.list) { +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); }
@@ -1563,7 +1568,7 @@ int resctrl_num_mbm_cntrs_show(struct kernfs_open_file *of,cpus_read_lock ();mutex_lock (&rdtgroup_mutex); -list_for_each_entry (dom, &r->mon_domains, hdr.list) { +list_for_each_entry_rcu (dom, &r->mon_domains, hdr.list,lockdep_is_cpus_held ()) {if (sep)seq_putc (s,';' );
@@ -1597,7 +1602,7 @@ int resctrl_available_mbm_cntrs_show(struct kernfs_open_file *of, goto out_unlock; } -list_for_each_entry (dom, &r->mon_domains, hdr.list) { +list_for_each_entry_rcu (dom, &r->mon_domains, hdr.list,lockdep_is_cpus_held ()) {if (sep)seq_putc (s,';' );
@@ -1647,7 +1652,7 @@ int mbm_L3_assignments_show(struct kernfs_open_file *of, struct seq_file *s, voi sep = false;seq_printf (s,"%s:" , mevt->name); -list_for_each_entry (d, &r->mon_domains, hdr.list) { +list_for_each_entry_rcu (d, &r->mon_domains, hdr.list,lockdep_is_cpus_held ()) {if (sep)seq_putc (s,';' );
@@ -1745,7 +1750,7 @@ static int resctrl_parse_mbm_assignment(struct rdt_resource *r, struct rdtgroup }/* Verify if the dom_id is valid */ -list_for_each_entry (d, &r->mon_domains, hdr.list) { +list_for_each_entry_rcu (d, &r->mon_domains, hdr.list,lockdep_is_cpus_held ()) {if (d->hdr.id == dom_id) { ret =rdtgroup_modify_assign_state (dom_str, d, rdtgrp, mevt);if (ret) {
fs/resctrl/pseudo_lock.c +1 -1
@@ -656,7 +656,7 @@ bool rdtgroup_pseudo_locked_in_hierarchy(struct rdt_ctrl_domain *d)* associated with them. */ for_each_alloc_capable_rdt_resource (r) { -list_for_each_entry (d_i, &r->ctrl_domains, hdr.list) { +list_for_each_entry_rcu (d_i, &r->ctrl_domains, hdr.list,lockdep_is_cpus_held ()) {if (d_i->plr)cpumask_or (cpu_with_psl, cpu_with_psl, &d_i->hdr.cpu_mask);
fs/resctrl/rdtgroup.c +12 -12
@@ -117,7 +117,7 @@ void rdt_staged_configs_clear(void)lockdep_assert_held (&rdtgroup_mutex);for_each_alloc_capable_rdt_resource (r) { -list_for_each_entry (dom, &r->ctrl_domains, hdr.list) +list_for_each_entry_rcu (dom, &r->ctrl_domains, hdr.list,lockdep_is_cpus_held ())memset (dom->staged_config,0 ,sizeof (dom->staged_config)); } }
@@ -1063,7 +1063,7 @@ static int rdt_bit_usage_show(struct kernfs_open_file *of,cpus_read_lock ();mutex_lock (&rdtgroup_mutex); -list_for_each_entry (dom, &r->ctrl_domains, hdr.list) { +list_for_each_entry_rcu (dom, &r->ctrl_domains, hdr.list,lockdep_is_cpus_held ()) {if (sep)seq_putc (seq,';' ); hw_shareable = r->cache.shareable_bits;
@@ -1415,7 +1415,7 @@ static bool rdtgroup_mode_test_exclusive(struct rdtgroup *rdtgrp)if (r->rid ==RDT_RESOURCE_MBA || r->rid ==RDT_RESOURCE_SMBA )continue ; has_cache = true; -list_for_each_entry (d, &r->ctrl_domains, hdr.list) { +list_for_each_entry_rcu (d, &r->ctrl_domains, hdr.list,lockdep_is_cpus_held ()) { ctrl =resctrl_arch_get_config (r, d, closid, s->conf_type);if (rdtgroup_cbm_overlaps (s, d, ctrl, closid, false)) {
@@ -1604,7 +1604,7 @@ static int rdtgroup_size_show(struct kernfs_open_file *of, type = schema->conf_type; sep = false;seq_printf (s,"%*s:" , max_name_width, schema->name); -list_for_each_entry (d, &r->ctrl_domains, hdr.list) { +list_for_each_entry_rcu (d, &r->ctrl_domains, hdr.list,lockdep_is_cpus_held ()) {if (sep)seq_putc (s,';' );if (rdtgrp->mode ==RDT_MODE_PSEUDO_LOCKSETUP ) {
@@ -1649,7 +1649,7 @@ static int mbm_config_show(struct seq_file *s, struct rdt_resource *r, u32 evtidcpus_read_lock ();mutex_lock (&rdtgroup_mutex); -list_for_each_entry (dom, &r->mon_domains, hdr.list) { +list_for_each_entry_rcu (dom, &r->mon_domains, hdr.list,lockdep_is_cpus_held ()) {if (sep)seq_puts (s,";" );
@@ -1763,7 +1763,7 @@ static int mon_config_write(struct rdt_resource *r, char *tok, u32 evtid)return -EINVAL ; } -list_for_each_entry (d, &r->mon_domains, hdr.list) { +list_for_each_entry_rcu (d, &r->mon_domains, hdr.list,lockdep_is_cpus_held ()) {if (d->hdr.id == dom_id) {mbm_config_write_domain (r, d, evtid, val); goto next;
@@ -2554,7 +2554,7 @@ static int set_mba_sc(bool mba_sc) rdtgroup_default.mba_mbps_event = mba_mbps_default_event; -list_for_each_entry (d, &r->ctrl_domains, hdr.list) { +list_for_each_entry_rcu (d, &r->ctrl_domains, hdr.list,lockdep_is_cpus_held ()) {for (i =0 ; i < num_closid; i++) d->mbps_val[i] =MBA_MAX_MBPS ; }
@@ -2879,7 +2879,7 @@ static int rdt_get_tree(struct fs_context *fc)if (resctrl_is_mbm_enabled ()) { r =resctrl_arch_get_resource (RDT_RESOURCE_L3 ); -list_for_each_entry (dom, &r->mon_domains, hdr.list) +list_for_each_entry_rcu (dom, &r->mon_domains, hdr.list,lockdep_is_cpus_held ())mbm_setup_overflow_handler (dom,MBM_OVERFLOW_INTERVAL ,RESCTRL_PICK_ANY_CPU ); }
@@ -3435,7 +3435,7 @@ static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn,/* Walking r->domains, ensure it can't race with cpuhp */ lockdep_assert_cpus_held (); -list_for_each_entry (hdr, &r->mon_domains, list) { +list_for_each_entry_rcu (hdr, &r->mon_domains, list,lockdep_is_cpus_held ()) { ret =mkdir_mondata_subdir (parent_kn, hdr, r, prgrp);if (ret)return ret;
@@ -3620,7 +3620,7 @@ int rdtgroup_init_cat(struct resctrl_schema *s, u32 closid)struct rdt_ctrl_domain *d;int ret; -list_for_each_entry (d, &s->res->ctrl_domains, hdr.list) { +list_for_each_entry_rcu (d, &s->res->ctrl_domains, hdr.list,lockdep_is_cpus_held ()) { ret =__init_one_rdt_domain (d, s, closid);if (ret <0 )return ret;
@@ -3635,7 +3635,7 @@ static void rdtgroup_init_mba(struct rdt_resource *r, u32 closid)struct resctrl_staged_config *cfg;struct rdt_ctrl_domain *d; -list_for_each_entry (d, &r->ctrl_domains, hdr.list) { +list_for_each_entry_rcu (d, &r->ctrl_domains, hdr.list,lockdep_is_cpus_held ()) {if (is_mba_sc (r)) { d->mbps_val[closid] =MBA_MAX_MBPS ;continue ;
@@ -4506,7 +4506,7 @@ static struct rdt_l3_mon_domain *get_mon_domain_from_cpu(int cpu,lockdep_assert_cpus_held (); -list_for_each_entry (d, &r->mon_domains, hdr.list) { +list_for_each_entry_rcu (d, &r->mon_domains, hdr.list,lockdep_is_cpus_held ()) {/* Find the domain that contains this CPU */ if (cpumask_test_cpu (cpu, &d->hdr.cpu_mask))return d;
Signature · 1 line
2.50.1
Reinette Chatre
maintainerPATCH v4 2/10 fs/resctrl: Move functions to avoid forward references in subsequent fixes
In reply to Reinette ChatreFrom: Tony Luck <[email protected]>
rdt_get_tree() manages resctrl fs mount and rdt_kill_sb() manages resctrl fs unmount.
There is significant overlap between error cleanup during resctrl mount failure and cleanup on resctrl unmount yet the cleanup is not done consistently in these two flows.
Pull some cleanup functions before rdt_get_tree() in preparation for a new helper that can be shared between mount and unmount.
---
Changes since V2:
- Rewrite changelog.
Changes since V3:
- Add Ben's Reviewed-by tag.
--- fs/resctrl/rdtgroup.c | 376 +++++++++++++++++++++--------------------- 1 file changed, 188 insertions(+), 188 deletions(-)
fs/resctrl/rdtgroup.c +188 -188
@@ -2792,6 +2792,194 @@ static void schemata_list_destroy(void) } } +/* +* Move tasks from one to the other group. If @from is NULL, then all tasks +* in the systems are moved unconditionally (used for teardown). +* +* If @mask is not NULL the cpus on which moved tasks are running are set +* in that mask so the update smp function call is restricted to affected +* cpus. +*/ +static void rdt_move_group_tasks (struct rdtgroup *from,struct rdtgroup *to, +struct cpumask *mask) +{ +struct task_struct *p, *t; + +read_lock (&tasklist_lock); +for_each_process_thread (p, t) { +if (!from ||is_closid_match (t, from) || +is_rmid_match (t, from)) { +resctrl_arch_set_closid_rmid (t, to->closid, + to->mon.rmid); + +/* +* Order the closid/rmid stores above before the loads +* in task_curr(). This pairs with the full barrier +* between the rq->curr update and +* resctrl_arch_sched_in() during context switch. +*/ +smp_mb (); + +/* +* If the task is on a CPU, set the CPU in the mask. +* The detection is inaccurate as tasks might move or +* schedule before the smp function call takes place. +* In such a case the function call is pointless, but +* there is no other side effect. +*/ +if (IS_ENABLED (CONFIG_SMP ) && mask &&task_curr (t)) +cpumask_set_cpu (task_cpu (t), mask); + } + } +read_unlock (&tasklist_lock); +} + +static void free_all_child_rdtgrp (struct rdtgroup *rdtgrp) +{ +struct rdtgroup *sentry, *stmp; +struct list_head *head; + + head = &rdtgrp->mon.crdtgrp_list; +list_for_each_entry_safe (sentry, stmp, head, mon.crdtgrp_list) { +rdtgroup_unassign_cntrs (sentry); +free_rmid (sentry->closid, sentry->mon.rmid); +list_del (&sentry->mon.crdtgrp_list); + +if (atomic_read (&sentry->waitcount) !=0 ) + sentry->flags =RDT_DELETED ; +else +rdtgroup_remove (sentry); + } +} + +/* +* Forcibly remove all of subdirectories under root. +*/ +static void rmdir_all_sub (void ) +{ +struct rdtgroup *rdtgrp, *tmp; + +/* Move all tasks to the default resource group */ +rdt_move_group_tasks (NULL , &rdtgroup_default,NULL ); + +list_for_each_entry_safe (rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) { +/* Free any child rmids */ +free_all_child_rdtgrp (rdtgrp); + +/* Remove each rdtgroup other than root */ +if (rdtgrp == &rdtgroup_default) +continue ; + +if (rdtgrp->mode ==RDT_MODE_PSEUDO_LOCKSETUP || + rdtgrp->mode ==RDT_MODE_PSEUDO_LOCKED ) +rdtgroup_pseudo_lock_remove (rdtgrp); + +/* +* Give any CPUs back to the default group. We cannot copy +* cpu_online_mask because a CPU might have executed the +* offline callback already, but is still marked online. +*/ +cpumask_or (&rdtgroup_default.cpu_mask, + &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); + +rdtgroup_unassign_cntrs (rdtgrp); + +free_rmid (rdtgrp->closid, rdtgrp->mon.rmid); + +kernfs_remove (rdtgrp->kn); +list_del (&rdtgrp->rdtgroup_list); + +if (atomic_read (&rdtgrp->waitcount) !=0 ) + rdtgrp->flags =RDT_DELETED ; +else +rdtgroup_remove (rdtgrp); + } +/* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */ +update_closid_rmid (cpu_online_mask, &rdtgroup_default); + +kernfs_remove (kn_info); +kernfs_remove (kn_mongrp); +kernfs_remove (kn_mondata); +} + +/** +* mon_get_kn_priv() - Get the mon_data priv data for this event. +* +* The same values are used across the mon_data directories of all control and +* monitor groups for the same event in the same domain. Keep a list of +* allocated structures and re-use an existing one with the same values for +* @rid, @domid, etc. +* +* @rid: The resource id for the event file being created. +* @domid: The domain id for the event file being created. +* @mevt: The type of event file being created. +* @do_sum: Whether SNC summing monitors are being created. Only set +* when @rid == RDT_RESOURCE_L3. +* +* Return: Pointer to mon_data private data of the event, NULL on failure. +*/ +static struct mon_data *mon_get_kn_priv (enum resctrl_res_level rid,int domid, +struct mon_evt *mevt, +bool do_sum) +{ +struct mon_data *priv; + +lockdep_assert_held (&rdtgroup_mutex); + +list_for_each_entry (priv, &mon_data_kn_priv_list, list) { +if (priv->rid == rid && priv->domid == domid && + priv->sum == do_sum && priv->evt == mevt) +return priv; + } + + priv =kzalloc_obj (*priv); +if (!priv) +return NULL ; + + priv->rid = rid; + priv->domid = domid; + priv->sum = do_sum; + priv->evt = mevt; +list_add_tail (&priv->list, &mon_data_kn_priv_list); + +return priv; +} + +/** +* mon_put_kn_priv() - Free all allocated mon_data structures. +* +* Called when resctrl file system is unmounted. +*/ +static void mon_put_kn_priv (void ) +{ +struct mon_data *priv, *tmp; + +lockdep_assert_held (&rdtgroup_mutex); + +list_for_each_entry_safe (priv, tmp, &mon_data_kn_priv_list, list) { +list_del (&priv->list); +kfree (priv); + } +} + +static void resctrl_fs_teardown (void ) +{ +lockdep_assert_held (&rdtgroup_mutex); + +/* Cleared by rdtgroup_destroy_root() */ +if (!rdtgroup_default.kn) +return ; + +rmdir_all_sub (); +rdtgroup_unassign_cntrs (&rdtgroup_default); +mon_put_kn_priv (); +rdt_pseudo_lock_release (); + rdtgroup_default.mode =RDT_MODE_SHAREABLE ; +closid_exit (); +schemata_list_destroy (); +rdtgroup_destroy_root (); +} +static int rdt_get_tree (struct fs_context *fc) {struct rdt_fs_context *ctx =rdt_fc2context (fc);
@@ -2991,194 +3179,6 @@ static int rdt_init_fs_context(struct fs_context *fc)return 0 ; } -/* -* Move tasks from one to the other group. If @from is NULL, then all tasks -* in the systems are moved unconditionally (used for teardown). -* -* If @mask is not NULL the cpus on which moved tasks are running are set -* in that mask so the update smp function call is restricted to affected -* cpus. -*/ -static void rdt_move_group_tasks (struct rdtgroup *from,struct rdtgroup *to, -struct cpumask *mask) -{ -struct task_struct *p, *t; - -read_lock (&tasklist_lock); -for_each_process_thread (p, t) { -if (!from ||is_closid_match (t, from) || -is_rmid_match (t, from)) { -resctrl_arch_set_closid_rmid (t, to->closid, - to->mon.rmid); - -/* -* Order the closid/rmid stores above before the loads -* in task_curr(). This pairs with the full barrier -* between the rq->curr update and -* resctrl_arch_sched_in() during context switch. -*/ -smp_mb (); - -/* -* If the task is on a CPU, set the CPU in the mask. -* The detection is inaccurate as tasks might move or -* schedule before the smp function call takes place. -* In such a case the function call is pointless, but -* there is no other side effect. -*/ -if (IS_ENABLED (CONFIG_SMP ) && mask &&task_curr (t)) -cpumask_set_cpu (task_cpu (t), mask); - } - } -read_unlock (&tasklist_lock); -} - -static void free_all_child_rdtgrp (struct rdtgroup *rdtgrp) -{ -struct rdtgroup *sentry, *stmp; -struct list_head *head; - - head = &rdtgrp->mon.crdtgrp_list; -list_for_each_entry_safe (sentry, stmp, head, mon.crdtgrp_list) { -rdtgroup_unassign_cntrs (sentry); -free_rmid (sentry->closid, sentry->mon.rmid); -list_del (&sentry->mon.crdtgrp_list); - -if (atomic_read (&sentry->waitcount) !=0 ) - sentry->flags =RDT_DELETED ; -else -rdtgroup_remove (sentry); - } -} - -/* -* Forcibly remove all of subdirectories under root. -*/ -static void rmdir_all_sub (void ) -{ -struct rdtgroup *rdtgrp, *tmp; - -/* Move all tasks to the default resource group */ -rdt_move_group_tasks (NULL , &rdtgroup_default,NULL ); - -list_for_each_entry_safe (rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) { -/* Free any child rmids */ -free_all_child_rdtgrp (rdtgrp); - -/* Remove each rdtgroup other than root */ -if (rdtgrp == &rdtgroup_default) -continue ; - -if (rdtgrp->mode ==RDT_MODE_PSEUDO_LOCKSETUP || - rdtgrp->mode ==RDT_MODE_PSEUDO_LOCKED ) -rdtgroup_pseudo_lock_remove (rdtgrp); - -/* -* Give any CPUs back to the default group. We cannot copy -* cpu_online_mask because a CPU might have executed the -* offline callback already, but is still marked online. -*/ -cpumask_or (&rdtgroup_default.cpu_mask, - &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); - -rdtgroup_unassign_cntrs (rdtgrp); - -free_rmid (rdtgrp->closid, rdtgrp->mon.rmid); - -kernfs_remove (rdtgrp->kn); -list_del (&rdtgrp->rdtgroup_list); - -if (atomic_read (&rdtgrp->waitcount) !=0 ) - rdtgrp->flags =RDT_DELETED ; -else -rdtgroup_remove (rdtgrp); - } -/* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */ -update_closid_rmid (cpu_online_mask, &rdtgroup_default); - -kernfs_remove (kn_info); -kernfs_remove (kn_mongrp); -kernfs_remove (kn_mondata); -} - -/** -* mon_get_kn_priv() - Get the mon_data priv data for this event. -* -* The same values are used across the mon_data directories of all control and -* monitor groups for the same event in the same domain. Keep a list of -* allocated structures and re-use an existing one with the same values for -* @rid, @domid, etc. -* -* @rid: The resource id for the event file being created. -* @domid: The domain id for the event file being created. -* @mevt: The type of event file being created. -* @do_sum: Whether SNC summing monitors are being created. Only set -* when @rid == RDT_RESOURCE_L3. -* -* Return: Pointer to mon_data private data of the event, NULL on failure. -*/ -static struct mon_data *mon_get_kn_priv (enum resctrl_res_level rid,int domid, -struct mon_evt *mevt, -bool do_sum) -{ -struct mon_data *priv; - -lockdep_assert_held (&rdtgroup_mutex); - -list_for_each_entry (priv, &mon_data_kn_priv_list, list) { -if (priv->rid == rid && priv->domid == domid && - priv->sum == do_sum && priv->evt == mevt) -return priv; - } - - priv =kzalloc_obj (*priv); -if (!priv) -return NULL ; - - priv->rid = rid; - priv->domid = domid; - priv->sum = do_sum; - priv->evt = mevt; -list_add_tail (&priv->list, &mon_data_kn_priv_list); - -return priv; -} - -/** -* mon_put_kn_priv() - Free all allocated mon_data structures. -* -* Called when resctrl file system is unmounted. -*/ -static void mon_put_kn_priv (void ) -{ -struct mon_data *priv, *tmp; - -lockdep_assert_held (&rdtgroup_mutex); - -list_for_each_entry_safe (priv, tmp, &mon_data_kn_priv_list, list) { -list_del (&priv->list); -kfree (priv); - } -} - -static void resctrl_fs_teardown (void ) -{ -lockdep_assert_held (&rdtgroup_mutex); - -/* Cleared by rdtgroup_destroy_root() */ -if (!rdtgroup_default.kn) -return ; - -rmdir_all_sub (); -rdtgroup_unassign_cntrs (&rdtgroup_default); -mon_put_kn_priv (); -rdt_pseudo_lock_release (); - rdtgroup_default.mode =RDT_MODE_SHAREABLE ; -closid_exit (); -schemata_list_destroy (); -rdtgroup_destroy_root (); -} -static void rdt_kill_sb (struct super_block *sb) {struct rdt_resource *r;
Signature · 1 line
2.50.1
Reinette Chatre
maintainerPATCH v4 3/10 fs/resctrl: Free mon_data structures on rdt_get_tree() failure
In reply to Reinette ChatreFrom: Tony Luck <[email protected]>
If mkdir_mondata_all() or a subsequent call in rdt_get_tree() fails, the mon_data structures allocated by mon_get_kn_priv() are leaked.
Add mon_put_kn_priv() to the out_mongrp error path to free the mon_data structures.
---
Changes since V2:
- Reword changelog.
Changes since V3:
- Add Chenyu's Reviewed-by tag that should have been added in V2.
- Add Ben's Reviewed-by tag.
- Add Closes: tag.
--- fs/resctrl/rdtgroup.c | 1 + 1 file changed, 1 insertion(+)
fs/resctrl/rdtgroup.c +1 -0
@@ -3081,6 +3081,7 @@ static int rdt_get_tree(struct fs_context *fc)kernfs_remove (kn_mondata); out_mongrp:if (resctrl_arch_mon_capable ()) { +mon_put_kn_priv ();rdtgroup_unassign_cntrs (&rdtgroup_default);kernfs_remove (kn_mongrp); }
Signature · 1 line
2.50.1
Reinette Chatre
maintainerPATCH v4 4/10 fs/resctrl: Fix use-after-free during unmount
In reply to Reinette ChatreFrom: Tony Luck <[email protected]>
During unmount or failure teardown all mon_data structures that contain monitoring event file private data are freed after which kernfs nodes are removed. However, the RDT_DELETED flag is never set for the statically allocated default resource group.
A concurrent reader of an event file associated with the default resource group may, after dropping kernfs active protection, block on rdtgroup_mutex while unmount proceeds to free the file private data and destroy the kernfs node without waiting for the reader.
When the mutex is released, the reader wakes up, observes that RDT_DELETED is not set for the default group, and dereferences the already-freed file private data.
The scenario can be depicted as follows:
CPU0 CPU1
/*
* Default resource group's
* monitoring data accessible via
* kernfs file with kernfs_node::priv
* pointing to a struct mon_data.
* User opens the file for reading.
*/
rdtgroup_mondata_show() /* arch encounters fatal error */
rdtgroup_kn_lock_live() resctrl_exit()
atomic_inc(&rdtgroup_default.waitcount) cpus_read_lock()
kernfs_break_active_protection(kn) mutex_lock(&rdtgroup_mutex)
cpus_read_lock() resctrl_fs_teardown()
mutex_lock(&rdtgroup_mutex) rmdir_all_sub()
mon_put_kn_priv()
/* Delete all mon_data structures */
rdtgroup_destroy_root()
kernfs_destroy_root()
rdtgroup_default.kn = NULL
mutex_unlock(&rdtgroup_mutex)
/*
* rdtgroup_default.flags is empty so
* rdtgroup_kn_lock_live() returns
* &rdtgroup_default
*/
md = of->kn->priv;
/* md points to freed mon_data */Set RDT_DELETED for the default group unconditionally since the flag does not lead to the freeing of this statically allocated group.
Do not allow a new resctrl mount if there are any waiters on default group of previous mount. A new mount will re-initialize the default group that would appear to waiters from previous mount as though the default group is accessible causing them to access the mon_data structures from the previous mount that have been removed.
---
Changes since V2:
- Rewrite changelog to not describe code as much.
- Rework changelog to switch to "Reported-by/Closes".
- Merge the duplicate rdtgroup_remove() comment with the function comment.
- Fix changelog to not mention that RDT_DELETED flag is set conditionally.
- Change "Fixes:" tag to point to commit that introduced dynamically allocated mon_data this bug involves.
Changes since V3:
- Depict the race. (Chenyu)
- Add Chenyu's Reviewed-by tag.
- Changelog grammar fixes.
--- fs/resctrl/rdtgroup.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
fs/resctrl/rdtgroup.c +16 -2
@@ -585,14 +585,20 @@ static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,* * On resource group creation via a mkdir, an extra kernfs_node reference is * taken to ensure that the rdtgroup structure remains accessible for the -* rdtgroup_kn_unlock() calls where it is removed. +* rdtgroup_kn_unlock() calls where it is removed. The default group is +* statically allocated: it does not have an extra reference but will have +* RDT_DELETED set on unmount to support safe access to its associated files +* via rdtgroup_kn_lock_live/rdtgroup_kn_unlock(). * -* Drop the extra reference here, then free the rdtgroup structure. +* For all but the default group: drop the extra reference, then free the +* rdtgroup structure. * * Return: void */ static void rdtgroup_remove (struct rdtgroup *rdtgrp) { +if (rdtgrp == &rdtgroup_default) +return ;kernfs_put (rdtgrp->kn);kfree (rdtgrp); }
@@ -2975,6 +2981,7 @@ static void resctrl_fs_teardown(void)mon_put_kn_priv ();rdt_pseudo_lock_release (); rdtgroup_default.mode =RDT_MODE_SHAREABLE ; + rdtgroup_default.flags =RDT_DELETED ;closid_exit ();schemata_list_destroy ();rdtgroup_destroy_root ();
@@ -3000,6 +3007,12 @@ static int rdt_get_tree(struct fs_context *fc) goto out; } +/* Avoid races from pending operations from a previous mount */ +if (atomic_read (&rdtgroup_default.waitcount) !=0 ) { + ret = -EBUSY ; + goto out; + } + ret =setup_rmid_lru_list ();if (ret) goto out;
@@ -4275,6 +4288,7 @@ static int rdtgroup_setup_root(struct rdt_fs_context *ctx) ctx->kfc.root = rdt_root; rdtgroup_default.kn =kernfs_root_to_node (rdt_root); + rdtgroup_default.flags =0 ;return 0 ; }
Signature · 1 line
2.50.1
Reinette Chatre
maintainerPATCH v4 5/10 fs/resctrl: Fix deadlock on errors during mount
In reply to Reinette Chatrerdt_get_tree() acquires rdtgroup_mutex before calling kernfs_get_tree(). If superblock setup fails inside kernfs_get_tree(), the VFS calls .kill_sb() (rdt_kill_sb()) on the same thread before kernfs_get_tree() returns. rdt_kill_sb() unconditionally attempts to acquire rdtgroup_mutex and deadlock occurs.
Since mount failure resulting from kernfs_get_tree() already calls the resctrl fs unmount handler (rdt_kill_sb()) let both call the same helper to make it clear both paths perform the same cleanup.
Call kernfs_get_tree() outside of locks. If kernfs_get_tree() fails and ctx->kfc.new_sb_created is set, then rdt_kill_sb() has already been called and no further cleanup is needed.
kernfs_get_tree() may set ctx->kfc.new_sb_created and then fail to obtain an inode for the new kn, causing the rdt_kill_sb() path to run with one fewer reference than required for the root to remain accessible in kernfs_kill_sb(). Add an extra hold on rdtgroup_default.kn to defend against this scenario and ensure the root can be dereferenced safely from kernfs_kill_sb().
Dropping locks before kernfs_get_tree() creates a window where CPU hotplug callbacks can race with the mount operation. Specifically, an online event observing resctrl_mounted == true could concurrently append directories to the unactivated kernfs tree, allocate mon_data structures, and arm background workers.
This concurrency is safe because the mount has not yet returned to the VFS, meaning userspace cannot interact with these transient files. If kernfs_get_tree() subsequently fails, the standard resctrl_unmount() teardown safely manages the concurrent modifications: any dynamically generated kernfs nodes are removed, and the associated memory is freed. Any background workers spawned by the hotplug event will naturally exit without re-arming when they acquire rdtgroup_mutex and observe resctrl_mounted == false.
---
Changes since V2:
- Switch to "Reported-by/Closes" in changelog
Changes since V3:
- Add Ben's Reviewed-by tag.
- Rework subject and changelog.
- s/root kn/root/ in comment. (Chenyu)
- Add Chenyu's Reviewed-by tag.
- Changelog grammar fixes.
- Add snippet to changelog about potential race with hotplug handlers.
--- fs/resctrl/rdtgroup.c | 83 +++++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 27 deletions(-)
fs/resctrl/rdtgroup.c +56 -27
@@ -2987,10 +2987,34 @@ static void resctrl_fs_teardown(void)rdtgroup_destroy_root (); } +static void resctrl_unmount (void ) +{ +struct rdt_resource *r; + +cpus_read_lock (); +mutex_lock (&rdtgroup_mutex); + +rdt_disable_ctx (); + +/* Put everything back to default values. */ +for_each_alloc_capable_rdt_resource (r) +resctrl_arch_reset_all_ctrls (r); + +resctrl_fs_teardown (); +if (resctrl_arch_alloc_capable ()) +resctrl_arch_disable_alloc (); +if (resctrl_arch_mon_capable ()) +resctrl_arch_disable_mon (); + resctrl_mounted = false; +mutex_unlock (&rdtgroup_mutex); +cpus_read_unlock (); +} +static int rdt_get_tree (struct fs_context *fc) {struct rdt_fs_context *ctx =rdt_fc2context (fc);unsigned long flags =RFTYPE_CTRL_BASE ; +struct kernfs_node *rdt_root_kn;struct rdt_l3_mon_domain *dom;struct rdt_resource *r;int ret;
@@ -3066,10 +3090,6 @@ static int rdt_get_tree(struct fs_context *fc)if (ret) goto out_mondata; - ret =kernfs_get_tree (fc); -if (ret <0 ) - goto out_psl; -if (resctrl_arch_alloc_capable ())resctrl_arch_enable_alloc ();if (resctrl_arch_mon_capable ())
@@ -3085,10 +3105,38 @@ static int rdt_get_tree(struct fs_context *fc) RESCTRL_PICK_ANY_CPU); } - goto out; +/* +* Ensure root remains accessible after mutex is unlocked so that +* kernfs_kill_sb() can run safely if called by kernfs_get_tree()'s +* failure path after creating a superblock but before taking reference +* on root kn (for example, if unable to get inode for root kn). +*/ +kernfs_get (rdtgroup_default.kn); + +/* +* Make backup of the current root kn being created to be used in +* kernfs_put(). The additional reference taken above will prevent the +* kn from being freed before kernfs_kill_sb() can run but +* rdtgroup_default.kn may be set to NULL via rdtgroup_destroy_root() +* and its backing root (rdt_root) could be overwritten before +* kernfs_put() can run. +*/ + rdt_root_kn = rdtgroup_default.kn; + +rdt_last_cmd_clear (); +mutex_unlock (&rdtgroup_mutex); +cpus_read_unlock (); + + ret =kernfs_get_tree (fc); +/* +* resctrl can only be mounted once, new superblock only expected +* to be created once. +*/ +if (!ctx->kfc.new_sb_created) +resctrl_unmount (); +kernfs_put (rdt_root_kn); +return ret; -out_psl: -rdt_pseudo_lock_release (); out_mondata:if (resctrl_arch_mon_capable ())kernfs_remove (kn_mondata);
@@ -3108,7 +3156,6 @@ static int rdt_get_tree(struct fs_context *fc) out_root:rdtgroup_destroy_root (); out: -rdt_last_cmd_clear ();mutex_unlock (&rdtgroup_mutex);cpus_read_unlock ();return ret;
@@ -3195,26 +3242,8 @@ static int rdt_init_fs_context(struct fs_context *fc)static void rdt_kill_sb (struct super_block *sb) { -struct rdt_resource *r; - -cpus_read_lock (); -mutex_lock (&rdtgroup_mutex); - -rdt_disable_ctx (); - -/* Put everything back to default values. */ -for_each_alloc_capable_rdt_resource (r) -resctrl_arch_reset_all_ctrls (r); - -resctrl_fs_teardown (); -if (resctrl_arch_alloc_capable ()) -resctrl_arch_disable_alloc (); -if (resctrl_arch_mon_capable ()) -resctrl_arch_disable_mon (); - resctrl_mounted = false; +resctrl_unmount ();kernfs_kill_sb (sb); -mutex_unlock (&rdtgroup_mutex); -cpus_read_unlock (); }static struct file_system_type rdt_fs_type = {
Signature · 1 line
2.50.1
Reinette Chatre
maintainerPATCH v4 6/10 fs/resctrl: Prevent use-after-free in rdtgroup_kn_put()
In reply to Reinette ChatreA struct rdtgroup is reference counted via rdtgroup::waitcount. Callers that need the structure to remain valid across a sleep (while waiting on acquiring rdtgroup_mutex) take a reference with rdtgroup_kn_get() and release it with rdtgroup_kn_put(). The release path is intended to serve as the fallback freer: if the count drops to zero and the group has already been marked RDT_DELETED, rdtgroup_kn_put() frees the structure. The bulk teardown paths free_all_child_rdtgrp() and rmdir_all_sub() resulting from a resctrl directory remove or resctrl fs unmount act as the primary freer: they hold rdtgroup_mutex and free each rdtgroup whose waitcount is zero, otherwise they set RDT_DELETED and leave the freeing to the last waiter.
These two freers race. rdtgroup_kn_put() commits waitcount == 0 with atomic_dec_and_test() outside rdtgroup_mutex, then reads rdtgroup::flags. Between those two operations a concurrent caller of free_all_child_rdtgrp() or rmdir_all_sub() (which holds the mutex) can observe waitcount == 0 via atomic_read(), call rdtgroup_remove(), and kfree() the structure. The subsequent read of rdtgroup::flags in rdtgroup_kn_put() is then a use-after-free, and the structure may even be freed twice if the freed memory happens to satisfy the RDT_DELETED flag check.
Replace the bare atomic_dec_and_test() with atomic_dec_and_mutex_lock() so that the decrement-to-zero takes rdtgroup_mutex before the count becomes globally visible. The inspection of rdtgroup::flags then runs under the same mutex held by the bulk freers, making the two paths mutually exclusive. The common case where the count does not reach zero remains lock-free. Defer kernfs_unbreak_active_protection() until after the mutex is dropped since kernfs active protections functionally wrap rdtgroup_mutex. Remove resource group, which in turn drops its kernfs reference, after kernfs protection is restored.
---
Changes since V2:
- New patch
Changes since V3:
- Add Ben's Reviewed-by tag.
- Add Tony's Reviewed-by tag.
--- fs/resctrl/rdtgroup.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)
fs/resctrl/rdtgroup.c +14 -5
@@ -2606,15 +2606,24 @@ static void rdtgroup_kn_get(struct rdtgroup *rdtgrp, struct kernfs_node *kn)static void rdtgroup_kn_put (struct rdtgroup *rdtgrp,struct kernfs_node *kn) { -if (atomic_dec_and_test (&rdtgrp->waitcount) && - (rdtgrp->flags &RDT_DELETED )) { +bool needs_free; + +if (!atomic_dec_and_mutex_lock (&rdtgrp->waitcount, &rdtgroup_mutex)) { +kernfs_unbreak_active_protection (kn); +return ; + } + + needs_free = rdtgrp->flags &RDT_DELETED ; + +mutex_unlock (&rdtgroup_mutex); + +kernfs_unbreak_active_protection (kn); + +if (needs_free) {if (rdtgrp->mode ==RDT_MODE_PSEUDO_LOCKSETUP || rdtgrp->mode ==RDT_MODE_PSEUDO_LOCKED )rdtgroup_pseudo_lock_remove (rdtgrp); -kernfs_unbreak_active_protection (kn);rdtgroup_remove (rdtgrp); - }else { -kernfs_unbreak_active_protection (kn); } }
Signature · 1 line
2.50.1
Reinette Chatre
maintainerPATCH v4 7/10 fs/resctrl: Fix double-add of pseudo-locked region's RMID to free list
In reply to Reinette ChatreA pseudo-locked group's RMID is freed when it is created. On unmount rmdir_all_sub() unconditionally frees all RMID of all groups, resulting in a double-free of the pseudo-locked group's RMID. The consequence of this is that the original free results in the pseudo-locked group's RMID being added to the rmid_free_lru linked list and the second free then attempts to add the same RMID entry to the rmid_free_lru again.
Do not double-free a pseudo-locked group's RMID.
---
Changes since V2:
- New patch
Changes since V3:
- Extract the double-add/double-free fix from all the other pseudo-locking fixes that will be deferred. This issue was uncovered during testing of the race fixes so drop all the Reported-by and Closes tags.
--- fs/resctrl/rdtgroup.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
fs/resctrl/rdtgroup.c +7 -5
@@ -2885,10 +2885,6 @@ static void rmdir_all_sub(void)if (rdtgrp == &rdtgroup_default)continue ; -if (rdtgrp->mode ==RDT_MODE_PSEUDO_LOCKSETUP || - rdtgrp->mode ==RDT_MODE_PSEUDO_LOCKED ) -rdtgroup_pseudo_lock_remove (rdtgrp); -/* * Give any CPUs back to the default group. We cannot copy * cpu_online_mask because a CPU might have executed the
@@ -2899,7 +2895,13 @@ static void rmdir_all_sub(void)rdtgroup_unassign_cntrs (rdtgrp); -free_rmid (rdtgrp->closid, rdtgrp->mon.rmid); +if (rdtgrp->mode ==RDT_MODE_PSEUDO_LOCKSETUP || + rdtgrp->mode ==RDT_MODE_PSEUDO_LOCKED ) { +rdtgroup_pseudo_lock_remove (rdtgrp); + }else { +/* Pseudo-locked group's RMID is freed during setup. */ +free_rmid (rdtgrp->closid, rdtgrp->mon.rmid); + }kernfs_remove (rdtgrp->kn);list_del (&rdtgrp->rdtgroup_list);
Signature · 1 line
2.50.1
Reinette Chatre
maintainerPATCH v4 8/10 fs/resctrl: Prevent deadlock and use-after-free in info file handlers
In reply to Reinette Chatreresctrl provides files under the info/ directory to expose global configuration and capabilities to userspace. These files are instantiated statically during filesystem mount and expose data associated with internal schema structures via kernfs private pointers.
A potential deadlock exists between userspace readers of these info files and the unmount filesystem teardown process. Reading an info file invokes kernfs which acquires an active reference, after which the handler typically attempts to acquire the rdtgroup_mutex. Concurrently, unmounting the filesystem holds the rdtgroup_mutex and then attempts to recursively remove the info kernfs nodes involving kernfs_drain() which blocks until all active references are released. Another problem exists where info files might be accessed from an outdated mount if the filesystem is unmounted and remounted during a reader's execution, leading to a use-after-free when reading the now-deleted private schema data.
Introduce info_kn_lock() and info_kn_unlock() helpers to coordinate locking across all info handlers. These helpers mirror similar logic used by resource group handlers by deliberately breaking the kernfs active protection before attempting to acquire the rdtgroup_mutex, preventing the deadlock. To guard against the vulnerability from rapid mount cycling, info_kn_lock() securely walks the parent lineage of the kernfs node under an RCU section to confirm the node belongs to the globally active root before permitting the operation to proceed. Convert all info file handlers to use this helper and only de-reference the schema after it is determined safe to do so.
Make no attempt to output an error message to last_cmd_status on failure since failure implies there is no filesystem with which to display the error to user space.
---
Changes since V2:
- New patch
Changes since V3:
- Add Tony's Reviewed-by tag.
- Changelog grammar fixes.
--- fs/resctrl/ctrlmondata.c | 38 ++++---- fs/resctrl/internal.h | 3 +- fs/resctrl/monitor.c | 48 +++++----- fs/resctrl/rdtgroup.c | 192 ++++++++++++++++++++++++++++++++------- 4 files changed, 203 insertions(+), 78 deletions(-)
fs/resctrl/ctrlmondata.c +20 -18
@@ -771,10 +771,12 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)int resctrl_io_alloc_show (struct kernfs_open_file *of,struct seq_file *seq,void *v) {struct resctrl_schema *s =rdt_kn_parent_priv (of->kn); -struct rdt_resource *r = s->res; +struct rdt_resource *r; -mutex_lock (&rdtgroup_mutex); +if (!info_kn_lock (of->kn)) +return -ENOENT ; + r = s->res;if (r->cache.io_alloc_capable) {if (resctrl_arch_get_io_alloc_enabled (r))seq_puts (seq,"enabled\n" );
@@ -784,7 +786,7 @@ int resctrl_io_alloc_show(struct kernfs_open_file *of, struct seq_file *seq, voiseq_puts (seq,"not supported\n" ); } -mutex_unlock (&rdtgroup_mutex); +info_kn_unlock (of->kn);return 0 ; }
@@ -849,7 +851,7 @@ ssize_t resctrl_io_alloc_write(struct kernfs_open_file *of, char *buf, size_t nbytes, loff_t off) {struct resctrl_schema *s =rdt_kn_parent_priv (of->kn); -struct rdt_resource *r = s->res; +struct rdt_resource *r;char const *grp_name;u32 io_alloc_closid;bool enable;
@@ -859,9 +861,10 @@ ssize_t resctrl_io_alloc_write(struct kernfs_open_file *of, char *buf,if (ret)return ret; -cpus_read_lock (); -mutex_lock (&rdtgroup_mutex); +if (!info_kn_lock (of->kn)) +return -ENOENT ; + r = s->res;rdt_last_cmd_clear ();if (!r->cache.io_alloc_capable) {
@@ -909,8 +912,7 @@ ssize_t resctrl_io_alloc_write(struct kernfs_open_file *of, char *buf, } out_unlock: -mutex_unlock (&rdtgroup_mutex); -cpus_read_unlock (); +info_kn_unlock (of->kn);return ret ?: nbytes; }
@@ -918,14 +920,15 @@ ssize_t resctrl_io_alloc_write(struct kernfs_open_file *of, char *buf,int resctrl_io_alloc_cbm_show (struct kernfs_open_file *of,struct seq_file *seq,void *v) {struct resctrl_schema *s =rdt_kn_parent_priv (of->kn); -struct rdt_resource *r = s->res; +struct rdt_resource *r;int ret =0 ; -cpus_read_lock (); -mutex_lock (&rdtgroup_mutex); +if (!info_kn_lock (of->kn)) +return -ENOENT ;rdt_last_cmd_clear (); + r = s->res;if (!r->cache.io_alloc_capable) {rdt_last_cmd_printf ("io_alloc is not supported on %s\n" , s->name); ret = -ENODEV ;
@@ -947,8 +950,7 @@ int resctrl_io_alloc_cbm_show(struct kernfs_open_file *of, struct seq_file *seq,show_doms (seq, s,NULL ,resctrl_io_alloc_closid (r)); out_unlock: -mutex_unlock (&rdtgroup_mutex); -cpus_read_unlock (); +info_kn_unlock (of->kn);return ret; }
@@ -1015,7 +1017,7 @@ ssize_t resctrl_io_alloc_cbm_write(struct kernfs_open_file *of, char *buf, size_t nbytes, loff_t off) {struct resctrl_schema *s =rdt_kn_parent_priv (of->kn); -struct rdt_resource *r = s->res; +struct rdt_resource *r;u32 io_alloc_closid;int ret =0 ;
@@ -1025,10 +1027,11 @@ ssize_t resctrl_io_alloc_cbm_write(struct kernfs_open_file *of, char *buf, buf[nbytes -1 ] ='\0' ; -cpus_read_lock (); -mutex_lock (&rdtgroup_mutex); +if (!info_kn_lock (of->kn)) +return -ENOENT ;rdt_last_cmd_clear (); + r = s->res;if (!r->cache.io_alloc_capable) {rdt_last_cmd_printf ("io_alloc is not supported on %s\n" , s->name); ret = -ENODEV ;
@@ -1053,8 +1056,7 @@ ssize_t resctrl_io_alloc_cbm_write(struct kernfs_open_file *of, char *buf, out_clear_configs:rdt_staged_configs_clear (); out_unlock: -mutex_unlock (&rdtgroup_mutex); -cpus_read_unlock (); +info_kn_unlock (of->kn);return ret ?: nbytes; }
fs/resctrl/internal.h +2 -1
@@ -335,8 +335,9 @@ __printf(1, 2)void rdt_last_cmd_printf (const char *fmt, ...);struct rdtgroup *rdtgroup_kn_lock_live (struct kernfs_node *kn); -void rdtgroup_kn_unlock (struct kernfs_node *kn); +bool info_kn_lock (struct kernfs_node *kn); +void info_kn_unlock (struct kernfs_node *kn);int rdtgroup_kn_mode_restrict (struct rdtgroup *r,const char *name);
fs/resctrl/monitor.c +24 -24
144 lines not on this page · raw message
fs/resctrl/rdtgroup.c +157 -35
395 lines not on this page · raw message
Signature · 1 line
2.50.1
Reinette Chatre
maintainerPATCH v4 9/10 x86/resctrl: Ensure domain fully initialized before placed on RCU list
In reply to Reinette ChatreA resctrl domain consists of the domain structure self that includes pointers to dynamically allocated filesystem as well as architecture specific data. For example, the L3 monitoring domain structure consists of the architecture specific struct rdt_hw_l3_mon_domain that contains the dynamically allocated rdt_hw_l3_mon_domain::arch_mbm_states architectural state and the embedded struct rdt_l3_mon_domain contains the dynamically allocated rdt_l3_mon_domain::mbm_states resctrl fs state.
The domains are added to and removed from an RCU protected list while cpus_write_lock() is held so that readers could access domains via cpus_read_lock() or from an RCU read-side critical section.
A reader accessing a domain via the RCU list expects that the domain and all its dynamically allocated data is accessible. Only place the domain on the RCU list when all its dynamically allocated data is ready, similarly unlink it from RCU list (again with cpus_write_lock() held) before removing any of its dynamically allocated data.
Calling resctrl_online_mon_domain() before adding the domain to the RCU list creates the kernfs files that expose the domain's monitoring data to user space before adding the domain to the RCU list. This is safe because rdtgroup_mondata_show() acquires cpus_read_lock() before it traverses the RCU list and will thus block until the domain is added to the RCU list.
There are no readers accessing a domain via RCU list. Ensure safety of access when such a reader arrives.
---
Changes since V2:
- New patch
Changes since V3:
- Add Tony's Reviewed-by tag.
- Add Chenyu's Reviewed-by tag.
- Grammar fixes in changelog.
- Add snippet to changelog about possible race with rdtgroup_mondata_show().
--- arch/x86/kernel/cpu/resctrl/core.c | 18 +++++++----------- arch/x86/kernel/cpu/resctrl/intel_aet.c | 5 ++--- 2 files changed, 9 insertions(+), 14 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +7 -11
@@ -515,14 +515,12 @@ static void domain_add_cpu_ctrl(int cpu, struct rdt_resource *r)return ; } -list_add_tail_rcu (&d->hdr.list, add_pos); - err =resctrl_online_ctrl_domain (r, d);if (err) { -list_del_rcu (&d->hdr.list); -synchronize_rcu ();ctrl_domain_free (hw_dom); +return ; } +list_add_tail_rcu (&d->hdr.list, add_pos); }static void l3_mon_domain_setup (int cpu,int id,struct rdt_resource *r,struct list_head *add_pos)
@@ -556,14 +554,12 @@ static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, structreturn ; } -list_add_tail_rcu (&d->hdr.list, add_pos); - err =resctrl_online_mon_domain (r, &d->hdr);if (err) { -list_del_rcu (&d->hdr.list); -synchronize_rcu ();l3_mon_domain_free (hw_dom); +return ; } +list_add_tail_rcu (&d->hdr.list, add_pos); }static void domain_add_cpu_mon (int cpu,struct rdt_resource *r)
@@ -642,9 +638,9 @@ static void domain_remove_cpu_ctrl(int cpu, struct rdt_resource *r) d = container_of(hdr, struct rdt_ctrl_domain, hdr); hw_dom =resctrl_to_arch_ctrl_dom (d); -resctrl_offline_ctrl_domain (r, d);list_del_rcu (&hdr->list);synchronize_rcu (); +resctrl_offline_ctrl_domain (r, d);/* * rdt_ctrl_domain "d" is going to be freed below, so clear
@@ -689,9 +685,9 @@ static void domain_remove_cpu_mon(int cpu, struct rdt_resource *r) d = container_of(hdr, struct rdt_l3_mon_domain, hdr); hw_dom =resctrl_to_arch_mon_dom (d); -resctrl_offline_mon_domain (r, hdr);list_del_rcu (&hdr->list);synchronize_rcu (); +resctrl_offline_mon_domain (r, hdr);l3_mon_domain_free (hw_dom);break ; }
@@ -702,9 +698,9 @@ static void domain_remove_cpu_mon(int cpu, struct rdt_resource *r)return ; pkgd = container_of(hdr, struct rdt_perf_pkg_mon_domain, hdr); -resctrl_offline_mon_domain (r, hdr);list_del_rcu (&hdr->list);synchronize_rcu (); +resctrl_offline_mon_domain (r, hdr);kfree (pkgd);break ; }
arch/x86/kernel/cpu/resctrl/intel_aet.c +2 -3
@@ -398,12 +398,11 @@ void intel_aet_mon_domain_setup(int cpu, int id, struct rdt_resource *r, d->hdr.type =RESCTRL_MON_DOMAIN ; d->hdr.rid =RDT_RESOURCE_PERF_PKG ;cpumask_set_cpu (cpu, &d->hdr.cpu_mask); -list_add_tail_rcu (&d->hdr.list, add_pos); err =resctrl_online_mon_domain (r, &d->hdr);if (err) { -list_del_rcu (&d->hdr.list); -synchronize_rcu ();kfree (d); +return ; } +list_add_tail_rcu (&d->hdr.list, add_pos); }
Signature · 1 line
2.50.1
Reinette Chatre
maintainerPATCH v4 10/10 fs/resctrl: Fix UAF from worker threads when domains are removed
In reply to Reinette ChatreThe mbm_handle_overflow() and cqm_handle_limbo() workers read event counters and may sleep while doing so. They are scheduled via delayed_work embedded in struct rdt_l3_mon_domain. Architecture allocates and frees these domains from CPU hotplug callbacks under cpus_write_lock(), and the workers acquire cpus_read_lock() to keep the domain alive across their access.
A use-after-free can occur when a worker is blocked waiting for cpus_read_lock() while the hotplug core holds cpus_write_lock():
the architecture frees the rdt_l3_mon_domain that contains the worker's work_struct. When the worker unblocks, the container_of() it performs on the embedded work pointer dereferences freed memory.
Drop cpus_read_lock() from the workers and instead drain pending and in-flight work synchronously before the architecture can free the domain. Since architecture offlines the domain under cpus_write_lock() after it has been unlinked from the RCU list and a grace period has elapsed, no new work can be scheduled. The cancel only needs to wait out existing work. Drop rdtgroup_mutex during CPU offline around cancel_delayed_work_sync() so that a worker waiting on the mutex can complete before re-pinning the work on a different CPU.
When offlining a CPU the architecture may iterate over resources in any order. For example, the MBA control domain may be offlined before or after a corresponding L3 monitor domain. Ensure that resctrl fs cancels the workers no matter what order the architecture offlines the domains.
---
Changes since v2:
- Rewrite changelog
- v2 attempted to solve the issue by using is_percpu_thread() within the
worker to learn if CPU worker was running on is going offline. A
Sashiko (https://sashiko.dev/#/patchset/20260515193944.15114-1-tony.luck%40intel.com?part=5)
pointed out that this would not be able to handle the scenario if one
of the hotplug handlers following the resctrl offline handlers failed.
- Some other fixes attempted that failed:
- Switch to accessing domain structure in handler via RCU so that CPU
hotplug lock no longer needed. Use cancel_delayed_work_sync() with
mutex dropped to cancel worker. Running worker from RCU read-side
critical section is a problem since the worker needs to be
able to sleep (mbm_handle_overflow()->mbm_update()->
mbm_update_one_event()->resctrl_arch_mon_ctx_alloc()-> might_sleep())
- Adding a reference count to the domain structure to avoid the worker needing to take CPU hotplug lock. This ended up being very complicated
with the architecture needing new APIs to manage the reference count
which cannot cleanly integrate into MPAM since it uses a single architecture domain structure to contain both the control and monitoring domain structures. Managing the references across mount, unmount,
online, offline, as well as worker self exit resulted in several asymmetrical and complicated paths that were error prone. Locking also proved to be complicated since architecture would need to initiate
domain free that will need to call back into resctrl that will take rdtgroup_mutex which means that references need to be taken/released without locking.
Changes since V3:
----------------
- Traverse mon_domains list using list_for_each_entry_rcu( ..., lockdep_is_cpus_held()) to document how CPU hotplug lock is required to be held (via architecture).
- Add snippet in changelog to motivate canceling work in monitor and control domain offline handlers.
--- fs/resctrl/monitor.c | 52 ++++++++++++++++++++++++++++++++++--------- fs/resctrl/rdtgroup.c | 52 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 89 insertions(+), 15 deletions(-)
fs/resctrl/monitor.c +42 -10
@@ -628,14 +628,22 @@ void mon_event_count(void *info) rr->err =0 ; } -static struct rdt_ctrl_domain *get_ctrl_domain_from_cpu (int cpu, -struct rdt_resource *r) +/* +* Find the software controller's ctrl domain that contains @cpu on resource @r. +* +* Only called from the mbm_over worker via update_mba_bw() where the returned +* domain is kept alive by cancel_delayed_work_sync() in +* resctrl_offline_ctrl_domain(). This drains this worker and then waits on +* rdtgroup_mutex held here before the architecture can free the ctrl domain. +* +* Context: Call from RCU read-side critical section. +*/ +static struct rdt_ctrl_domain *get_sc_ctrl_domain_from_cpu (int cpu, +struct rdt_resource *r) {struct rdt_ctrl_domain *d; -lockdep_assert_cpus_held (); - -list_for_each_entry (d, &r->ctrl_domains, hdr.list) { +list_for_each_entry_rcu (d, &r->ctrl_domains, hdr.list) {/* Find the domain that contains this CPU */ if (cpumask_test_cpu (cpu, &d->hdr.cpu_mask))return d;
@@ -696,7 +704,8 @@ static void update_mba_bw(struct rdtgroup *rgrp, struct rdt_l3_mon_domain *dom_mif (WARN_ON_ONCE (!pmbm_data))return ; - dom_mba =get_ctrl_domain_from_cpu (smp_processor_id (), r_mba); +guard (rcu)(); + dom_mba =get_sc_ctrl_domain_from_cpu (smp_processor_id (), r_mba);if (!dom_mba) {pr_warn_once ("Failure to get domain for MBA update\n" );return ;
@@ -799,9 +808,19 @@ void cqm_handle_limbo(struct work_struct *work)unsigned long delay =msecs_to_jiffies (CQM_LIMBOCHECK_INTERVAL );struct rdt_l3_mon_domain *d; -cpus_read_lock (); +/* +* Safe to run without CPU hotplug lock. Work is guaranteed to be +* canceled before the domain structure is removed. +*/ mutex_lock (&rdtgroup_mutex); +/* +* Ensure the worker is dedicated to a CPU as intended and not +* relocated by workqueue subsystem as part of CPU going offline. +*/ +if (!is_percpu_thread ()) + goto out_unlock; + d = container_of(work, struct rdt_l3_mon_domain, cqm_limbo.work);__check_limbo (d, false);
@@ -813,8 +832,8 @@ void cqm_handle_limbo(struct work_struct *work) delay); } +out_unlock:mutex_unlock (&rdtgroup_mutex); -cpus_read_unlock (); }/**
@@ -846,7 +865,10 @@ void mbm_handle_overflow(struct work_struct *work)struct list_head *head;struct rdt_resource *r; -cpus_read_lock (); +/* +* Safe to run without CPU hotplug lock. Work is guaranteed to be +* canceled before the domain structure is removed. +*/ mutex_lock (&rdtgroup_mutex);/*
@@ -856,6 +878,17 @@ void mbm_handle_overflow(struct work_struct *work)if (!resctrl_mounted || !resctrl_arch_mon_capable ()) goto out_unlock; +/* +* Ensure the worker is dedicated to a CPU and not relocated by +* workqueue subsystem as part of CPU going offline since reading +* events depend on smp_processor_id(). After passing this check +* smp_processor_id() is valid for entire duration of this worker +* since it runs with rdtgroup_mutex held and the offline handler needs +* rdtgroup_mutex to offline the CPU being run on here. +*/ +if (!is_percpu_thread ()) + goto out_unlock; + r =resctrl_arch_get_resource (RDT_RESOURCE_L3 ); d = container_of(work, struct rdt_l3_mon_domain, mbm_over.work);
@@ -880,7 +913,6 @@ void mbm_handle_overflow(struct work_struct *work) out_unlock:mutex_unlock (&rdtgroup_mutex); -cpus_read_unlock (); }/**
fs/resctrl/rdtgroup.c +47 -5
86 lines not on this page · raw message
Signature · 1 line
2.50.1
Reinette Chatre
maintainerHi Everybody,
Addressing the non patch specific Sashiko feedback from
https://sashiko.dev/#/patchset/cover.1780456704.git.reinette.chatre%40intel.com
I respond to patch specific Sashiko feedback in individual patches.
Quoted 9 lines · Reinette Chatre
v3: https://lore.kernel.org/lkml/[email protected]/
v2: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/While reviewing the AET series [1] Sashiko reported a deadlock during mount, and a use-after-free when an L3 domain is removed during CPU offline. More issues were uncovered as fixes were developed and reviewed. While the goal is to fix all issues the races surrounding pseudo-locked regions are not yet solved and have been removed from this version of fixes.
As anticipated Sashiko reported the issues surrounding pseudo-locking. There is one new concern raised between pseudo-locked regions and assigned counters but pseudo-locking is a model-specific feature on hardware that does not support assigned counters.
Sashiko did uncover one new issue related to the limbo handler when SNC is enabled. For this I am currently planning to add this patch to this series:
From 95da3282f94754e8840497be632314e542375e67 Mon Sep 17 00:00:00 2001
Message-ID: <95da3282f94754e8840497be632314e542375e67.1780586352.git.reinette.chatre@intel.com>
From: Reinette Chatre <[email protected]>
Date: Wed, 3 Jun 2026 13:37:15 -0700
Subject: [PATCH] x86,fs/resctrl: Prevent out-of-bounds access while offlining
CPU when SNC enabled
The architecture updates the cpu_mask in a domain's header to track which online CPUs are associated with the domain. When this mask becomes empty the architecture initiates offline of the domain that includes calling on resctrl fs to offline the domain. If it is a monitoring domain in which LLC occupancy is tracked resctrl fs forces the limbo handler to release all busy RMID.
The limbo handler reads the current event value associated with a busy RMID irrespective of it being checked as part of regular "is it still busy" check or whether it will be forced released anyway. When reading an RMID on a system with SNC enabled the "logical RMID" is converted to the "physical RMID" and this conversion requires the NUMA node ID of the resctrl monitoring domain that is in turn determined by querying the NUMA node ID of any CPU belonging to the monitoring domain.
When the monitoring domain is going offline its cpu_mask is empty causing the NUMA node ID query via cpu_to_node() to be done with "nr_cpu_ids" as argument resulting in an out-of-bounds access.
Refactor the limbo handler to skip reading the RMID when the RMID will just be forced released anyway. Add a safety check to the architecture's RMID reader to protect against this scenario.
---
Changes since v4:
- New patch
--- arch/x86/kernel/cpu/resctrl/monitor.c | 5 ++++ fs/resctrl/monitor.c | 39 +++++++++++++++------------ 2 files changed, 27 insertions(+), 17 deletions(-)
arch/x86/kernel/cpu/resctrl/monitor.c +5 -0
@@ -259,6 +259,11 @@ int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_domain_hdr *hdr,if (!domain_header_is_valid (hdr,RESCTRL_MON_DOMAIN ,RDT_RESOURCE_L3 ))return -EINVAL ; +if (cpumask_empty (&hdr->cpu_mask)) { +pr_warn_once ("Domain %d has no CPUs\n" , hdr->id); +return -EINVAL ; + } + d = container_of(hdr, struct rdt_l3_mon_domain, hdr); hw_dom =resctrl_to_arch_mon_dom (d); cpu =cpumask_any (&hdr->cpu_mask);
fs/resctrl/monitor.c +22 -17
@@ -135,10 +135,10 @@ void __check_limbo(struct rdt_l3_mon_domain *d, bool force_free)struct rdt_resource *r =resctrl_arch_get_resource (RDT_RESOURCE_L3 );u32 idx_limit =resctrl_arch_system_num_rmid_idx ();struct rmid_entry *entry; +bool rmid_dirty = true;u32 idx, cur_idx =1 ;void *arch_mon_ctx;void *arch_priv; -bool rmid_dirty;u64 val =0 ; arch_priv = mon_event_all[QOS_L3_OCCUP_EVENT_ID ].arch_priv;
@@ -161,22 +161,27 @@ void __check_limbo(struct rdt_l3_mon_domain *d, bool force_free)break ; entry =__rmid_entry (idx); -if (resctrl_arch_rmid_read (r, &d->hdr, entry->closid, entry->rmid, -QOS_L3_OCCUP_EVENT_ID , arch_priv, &val, - arch_mon_ctx)) { - rmid_dirty = true; - }else { - rmid_dirty = (val >= resctrl_rmid_realloc_threshold); - -/* -* x86's CLOSID and RMID are independent numbers, so the entry's -* CLOSID is an empty CLOSID (X86_RESCTRL_EMPTY_CLOSID). On Arm the -* RMID (PMG) extends the CLOSID (PARTID) space with bits that aren't -* used to select the configuration. It is thus necessary to track both -* CLOSID and RMID because there may be dependencies between them -* on some architectures. -*/ -trace_mon_llc_occupancy_limbo (entry->closid, entry->rmid, d->hdr.id, val); +if (!force_free) { +if (resctrl_arch_rmid_read (r, &d->hdr, entry->closid, + entry->rmid,QOS_L3_OCCUP_EVENT_ID , + arch_priv, &val, arch_mon_ctx)) { + rmid_dirty = true; + }else { + rmid_dirty = (val >= resctrl_rmid_realloc_threshold); + +/* +* x86's CLOSID and RMID are independent numbers, +* so the entry's CLOSID is an empty CLOSID +* (X86_RESCTRL_EMPTY_CLOSID). On Arm the RMID +* (PMG) extends the CLOSID (PARTID) space with +* bits that aren't used to select the configuration. +* It is thus necessary to track both CLOSID and +* RMID because there may be dependencies between +* them on some architectures. +*/ +trace_mon_llc_occupancy_limbo (entry->closid, entry->rmid, + d->hdr.id, val); + } }if (force_free || !rmid_dirty) {
Signature · 1 line
2.53.0
Reinette Chatre
maintainerRe: [PATCH v4 01/10] x86,fs/resctrl: Document safe RCU list traversal
In reply to Reinette Chatrearch/x86/kernel/cpu/resctrl/ctrlmondata.c int resctrl_arch_update_domains(struct rdt_resource *r, u32 closid) · line 59
Quoted 13 lines · Reinette Chatre
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +1
-1@@ -53,7 +53,7 @@ int resctrl_arch_update_domains(struct rdt_resource *r, u32 closid)/* Walking r->domains, ensure it can't race with cpuhp */ lockdep_assert_cpus_held (); -list_for_each_entry (d, &r->ctrl_domains, hdr.list) { +list_for_each_entry_rcu (d, &r->ctrl_domains, hdr.list,lockdep_is_cpus_held ()) { hw_dom =resctrl_to_arch_ctrl_dom (d); msr_param.res =NULL ;for (t =0 ; t <CDP_NUM_TYPES ; t++) {
Sashiko [1] pointed out that no stub exists for lockdep_is_cpus_held() when CONFIG_HOTPLUG_CPU is disabled.
Before this series lockdep_is_cpus_held() only has one caller and it protects the call with an IS_ENABLED(CONFIG_HOTPLUG_CPU) check. This caught me as I am more familiar with the style of lockdep_is_held(). I find it safer to not need to prefix all lockdep_is_cpus_held() calls with an IS_ENABLED(CONFIG_HOTPLUG_CPU) check so I submitted an independent change to CPU hotplug to add the stubs [2]. Thanks to Thomas this was just merged to smp/core branch of tip.
In the mean time, I believe it to be unlikely that anybody would disable CONFIG_SMP when using resctrl and CONFIG_HOTPLUG_CPU cannot be disabled by user and depends on CONFIG_SMP per:arch/x86/Kconfig:
config HOTPLUG_CPU
def_bool y
depends on SMP
Reinette
[1] https://sashiko.dev/#/patchset/cover.1780456704.git.reinette.chatre%40intel.com?part=1
[2] https://lore.kernel.org/lkml/[email protected]/
Reinette Chatre
maintainerRe: [PATCH v4 10/10] fs/resctrl: Fix UAF from worker threads when domains are removed
In reply to Reinette ChatreQuoted 27 lines · Reinette Chatre
The
mbm_handle_overflow()andcqm_handle_limbo()workers read event counters and may sleep while doing so. They are scheduled viadelayed_workembedded in structrdt_l3_mon_domain. Architecture allocates and frees these domains from CPU hotplug callbacks undercpus_write_lock(), and the workers acquirecpus_read_lock()to keep the domain alive across their access.A use-after-free can occur when a worker is blocked waiting for
cpus_read_lock()while the hotplug core holdscpus_write_lock():
the architecture frees therdt_l3_mon_domainthat contains the worker'swork_struct. When the worker unblocks, thecontainer_of()it performs on the embedded work pointer dereferences freed memory.Drop
cpus_read_lock()from the workers and instead drain pending and in-flight work synchronously before the architecture can free the domain. Since architecture offlines the domain undercpus_write_lock()after it has been unlinked from the RCU list and a grace period has elapsed, no new work can be scheduled. The cancel only needs to wait out existing work. Droprdtgroup_mutexduring CPU offline aroundcancel_delayed_work_sync()so that a worker waiting on the mutex can complete before re-pinning the work on a different CPU.When offlining a CPU the architecture may iterate over resources in any order. For example, the MBA control domain may be offlined before or after a corresponding L3 monitor domain. Ensure that resctrl fs cancels the workers no matter what order the architecture offlines the domains.
Quoted 7 lines
Fixes 24247aeeabe9 ("x86/intel_rdt/cqm: Improve limbo list processing")Reported-by Sashiko <[email protected]>Co-developed-by Tony Luck <[email protected]>Signed-off-by Tony Luck <[email protected]>Signed-off-by Reinette Chatre <[email protected]>---
Sashiko highlighted [1] that since this patch ensures the workers can complete as part of CPU offline the worker is more likely to run with on a domain while that domain's cpu_mask is empty. There are places where the workers peek into the domain's cpu_mask. For example,
- if SNC is enabled the limbo handler needs to know the NUMA node ID of the domain and uses a CPU from cpu_mask to determine that
- both workers always look into the cpu_mask to determine where to reschedule next.
To protect against this I plan to add a check for an empty cpu_mask at the start of both workers and just exit if the cpu_mask is empty.
Reinette
[1] https://sashiko.dev/#/patchset/cover.1780456704.git.reinette.chatre%40intel.com?part=10