x86,fs/resctrl: Fix long-standing issues
Subsystems (2)
Reinette Chatre
maintainerv3 cover x86,fs/resctrl: Fix long-standing issues
V3: https://lore.kernel.org/lkml/[email protected]/
Sashiko encountered failure while reviewing the original V3 submission of this series. This partial submission is only intended for Sashiko so that it can consider the patches missed during V3 review.
Reinette Chatre (3):
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
arch/x86/kernel/cpu/resctrl/core.c | 18 +- arch/x86/kernel/cpu/resctrl/intel_aet.c | 5 +- fs/resctrl/ctrlmondata.c | 38 ++-- fs/resctrl/internal.h | 3 +- fs/resctrl/monitor.c | 100 ++++++---- fs/resctrl/rdtgroup.c | 244 ++++++++++++++++++++---- 6 files changed, 301 insertions(+), 107 deletions(-)
Signature · 1 line
2.50.1
Reinette Chatre
maintainerPATCH RESEND v3 7/9 fs/resctrl: Prevent deadlock and use-after-free in info file handlers
resctrl 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 determined safe to do so.
Make no attempt to output error message to last_cmd_status on failure since failure implies there is no filesystem with which to display error to user space.
---
Changes since V2:
- New patch
--- 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
@@ -769,10 +769,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" );
@@ -782,7 +784,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 ; }
@@ -847,7 +849,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;
@@ -857,9 +859,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) {
@@ -907,8 +910,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; }
@@ -916,14 +918,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 ;
@@ -945,8 +948,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; }
@@ -1013,7 +1015,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 ;
@@ -1023,10 +1025,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 ;
@@ -1051,8 +1054,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
@@ -345,8 +345,9 @@ void rdt_last_cmd_printf(const char *fmt, ...);void rdtgroup_remove (struct rdtgroup *rdtgrp);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 RESEND v3 8/9 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 placed on an RCU protected list 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 domain on RCU list when all its dynamically allocated data is ready, similarly unlink from RCU list before removing any of its dynamically allocated data.
There are no readers accessing a domain via RCU list. Ensure safety of access when such reader arrives.
---
Changes since V2:
- New patch
--- 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 RESEND v3 9/9 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.
---
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.
--- fs/resctrl/monitor.c | 52 ++++++++++++++++++++++++++++++++++--------- fs/resctrl/rdtgroup.c | 52 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 89 insertions(+), 15 deletions(-)
fs/resctrl/monitor.c +42 -10
@@ -623,14 +623,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;
@@ -691,7 +699,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 ;
@@ -794,9 +803,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);
@@ -808,8 +827,8 @@ void cqm_handle_limbo(struct work_struct *work) delay); } +out_unlock:mutex_unlock (&rdtgroup_mutex); -cpus_read_unlock (); }/**
@@ -841,7 +860,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);/*
@@ -851,6 +873,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);
@@ -875,7 +908,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