Allow AET to use PMT as loadable module
Subsystems (6)
Tony Luck
maintainerv8 cover Allow AET to use PMT as loadable module
Requiring INTEL_PMT_TELEMETRY=y to enable AET is a functional workaround to enable enumeration of Application Energy Telemetry (AET) events, but unacceptable to many users. It results in increased configration complexity, increased kernel memory footprint and inability to patch problems by unloading a module and loading an updated version.
Add a registration function to the AET code that can be used by INTEL_PMT_TELEMETRY to provide the enumeration functions.
INTEL_PMT_TELEMETRY can be loaded/unloaded independently of resctrl file system mount/unmount. Perform enumeration on every mount and cleanup on every unmount.
---
Changes since v7:
Rebased to v7.1
Patches 1-4 updated from latest (v5) series from Reinette:
Those four patches had their own review and (hopefully) will be included in TIP tree during this v7.2-rc* cycle.
Patch 5 is new to resolve the issue that the PMT driver could be unbound from the pmt_telemtry module which would unmap all the MMIO register space. Discussed offline with David Box, but needs his Ack/Review tag here.
Patches 6-14 and 16 all have updates based on review comments see the individual patches for change log after the "---"
Reinette Chatre (1):
fs/resctrl: Fix deadlock on errors during mount
Tony Luck (15):
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 platform/x86/intel/pmt: Prevent unbind of PMT telemetry driver fs/resctrl: Remove redundant calls to resctrl_arch_mon_capable() x86/resctrl: Honor rdt=perf option to force enable AET perf events fs/resctrl: Add interface to disable a monitor event x86/resctrl: Drop global 'rdt_mon_capable' flag arm,x86,fs/resctrl: Handle change in number of RMIDs on each mount x86/resctrl: Add PMT registration API for AET enumeration callbacks platform/x86/intel/pmt: Register enumeration functions with resctrl arm,x86/resctrl: Resolve INTEL_PMT_TELEMETRY symbols at runtime fs/resctrl: Call architecture hooks for every mount/unmount x86/resctrl: Simplify Kconfig options for resctrl Documentation/filesystems/resctrl: Add footnote for telemetry fstab
mount caveat
Documentation/filesystems/resctrl.rst | 7 +- include/linux/resctrl.h | 47 ++- arch/x86/include/asm/resctrl.h | 21 +- arch/x86/kernel/cpu/resctrl/internal.h | 24 +- arch/x86/kernel/cpu/resctrl/core.c | 69 ++- arch/x86/kernel/cpu/resctrl/intel_aet.c | 91 +++- arch/x86/kernel/cpu/resctrl/monitor.c | 6 - drivers/platform/x86/intel/pmt/telemetry.c | 9 + drivers/resctrl/mpam_resctrl.c | 9 + fs/resctrl/monitor.c | 93 +++-- fs/resctrl/rdtgroup.c | 464 ++++++++++++--------- arch/x86/Kconfig | 15 +- arch/x86/kernel/cpu/resctrl/Makefile | 2 +- 13 files changed, 565 insertions(+), 292 deletions(-)
base-commit: 8cd9520d35a6c38db6567e97dd93b1f11f185dc6
Signature · 1 line
2.54.0
Tony Luck
maintainerPATCH v8 1/16 fs/resctrl: Move functions to avoid forward references in subsequent fixes
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.
--- fs/resctrl/rdtgroup.c | 376 +++++++++++++++++++++--------------------- 1 file changed, 188 insertions(+), 188 deletions(-)
fs/resctrl/rdtgroup.c +188 -188
@@ -2782,6 +2782,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);
@@ -2981,194 +3169,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.54.0
Tony Luck
maintainerPATCH v8 2/16 fs/resctrl: Free mon_data structures on rdt_get_tree() failure
In reply to Tony LuckIf 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.
--- fs/resctrl/rdtgroup.c | 1 + 1 file changed, 1 insertion(+)
fs/resctrl/rdtgroup.c +1 -0
@@ -3071,6 +3071,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.54.0
Tony Luck
maintainerPATCH v8 3/16 fs/resctrl: Fix use-after-free during unmount
In reply to Tony LuckDuring 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.
--- 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); }
@@ -2965,6 +2971,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 ();
@@ -2990,6 +2997,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;
@@ -4265,6 +4278,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.54.0
Tony Luck
maintainerPATCH v8 4/16 fs/resctrl: Fix deadlock on errors during mount
In reply to Tony LuckFrom: Reinette Chatre <[email protected]>
rdt_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.
--- fs/resctrl/rdtgroup.c | 83 +++++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 27 deletions(-)
fs/resctrl/rdtgroup.c +56 -27
@@ -2977,10 +2977,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;
@@ -3056,10 +3080,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 ())
@@ -3075,10 +3095,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);
@@ -3098,7 +3146,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;
@@ -3185,26 +3232,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.54.0
Tony Luck
maintainerPATCH v8 5/16 platform/x86/intel/pmt: Prevent unbind of PMT telemetry driver
In reply to Tony LuckThe resctrl AET code uses intel_pmt_get_regions_by_feature() (exported by the pmt_telemetry module) to obtain pmt_feature_group instances that are populated using data from the pmt_discovery module. The returned telemetry_region::addr values are MMIO mappings created in the telemetry driver's probe path, and resctrl reads from them while a mount is active.
However, a user can unbind the auxiliary driver from its device via the sysfs "unbind" attribute. Unbinding the telemetry driver would tear down the MMIO mappings, leaving resctrl with stale pointers and causing a fault on the next access.
It is safe for the discovery driver to be unbound. It does all its work at initial probe time and saves a copy of the features and RMID count.
Set device_driver::suppress_bind_attrs in the telemetry driver so that the "bind" and "unbind" sysfs attributes are not exposed and the user cannot detach the driver from its devices while resctrl (or any other in-kernel consumer) may be using the resources.
--
v8:
New patch
--- drivers/platform/x86/intel/pmt/telemetry.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/platform/x86/intel/pmt/telemetry.c +3 -0
@@ -421,6 +421,9 @@ static struct auxiliary_driver pmt_telem_aux_driver = { .id_table = pmt_telem_id_table, .remove = pmt_telem_remove, .probe = pmt_telem_probe, + .driver = { + .suppress_bind_attrs = true, + }, };static int __init pmt_telem_init (void )
Signature · 1 line
2.54.0
Tony Luck
maintainerPATCH v8 6/16 fs/resctrl: Remove redundant calls to resctrl_arch_mon_capable()
In reply to Tony LuckArchitecture code provides resctrl_arch_mon_capable() so that file system code knows whether any monitor features are supported. This is used to decide whether to create "mon_data" directories etc.
Initially resctrl_arch_mon_capable() was an inline function providing the value of the x86 architecture variable "rdt_mon_capable". I.e. extremely low overhead and so it was used liberally as a sanity check.
The ARM implementation of resctrl_arch_mon_capable() is not inline and chases a couple of pointers. Future changes to x86 implementation will also make resctrl_arch_mon_capable() more expensive.
Redundant calls to resctrl_arch_mon_capable() in hot code paths should be eliminated.
1) mbm_handle_overflow()
This function is called once per second from worker threads running on each L3 domain. The call is clearly redundant because worker threads are only created if the MBM monitoring feature is enabled.
2) is_rmid_match()
This function is called when a user reads a "tasks" file of a MON_GROUP. The call is redundant because the function also checks "r->type == RDTMON_GROUP" and this type of group can only be created on systems that support monitoring.
Remove the call to resctrl_arch_mon_capable() from both of these functions.
---
v8:
New patch
fs/resctrl/monitor.c | 2 +- fs/resctrl/rdtgroup.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
fs/resctrl/monitor.c +1 -1
@@ -848,7 +848,7 @@ void mbm_handle_overflow(struct work_struct *work)* If the filesystem has been unmounted this work no longer needs to * run. */ -if (!resctrl_mounted || !resctrl_arch_mon_capable ()) +if (!resctrl_mounted) goto out_unlock; r =resctrl_arch_get_resource (RDT_RESOURCE_L3 );
fs/resctrl/rdtgroup.c +1 -1
@@ -691,7 +691,7 @@ static bool is_closid_match(struct task_struct *t, struct rdtgroup *r)static bool is_rmid_match (struct task_struct *t,struct rdtgroup *r) { -return (resctrl_arch_mon_capable () && (r->type ==RDTMON_GROUP ) && +return ((r->type ==RDTMON_GROUP ) &&resctrl_arch_match_rmid (t, r->mon.parent->closid, r->mon.rmid)); }
Signature · 1 line
2.54.0
Tony Luck
maintainerPATCH v8 7/16 x86/resctrl: Honor rdt=perf option to force enable AET perf events
In reply to Tony LuckThe kernel command line option "rdt=" is used to force enable or disable individual resctrl features.
Linux only enumerates AET (Application Energy Telemetry) once on first mount. But that is going to change to enumerate on each mount to allow the pmt_telemetry driver to be configured as a module, and allow unloading when not in use.
The following scenario will be a problem:
1) User mounts the resctrl file system. all_regions_have_sufficient_rmid() notes that a perf telemetry region supports fewer RMIDs than expected for the event_group and sets e->force_off = true. But the user had specified "rdt=perf" on the kernel command line so perf events are enabled for this first mount.
2) Resctrl file system is unmounted and later remounted.
3) During this new mount cycle enable_events() sees that e->force_off is set and stops enumeration for this event group. So on this second, and all subsequent, mounts perf events are not enabled.
Fix by checking the state of e->force_on at the start of enable_events().
---
v8:
New subject and commit comment
Fix by changing test at start of enable_events() instead of changing all_regions_have_sufficient_rmid()
arch/x86/kernel/cpu/resctrl/intel_aet.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/resctrl/intel_aet.c +3 -3
@@ -60,8 +60,8 @@ struct pmt_event {* data for all telemetry regions of type @pfname. * Valid if the system supports the event group, * NULL otherwise. -* @force_off: True when "rdt" command line or architecture code disables -* this event group due to insufficient RMIDs. +* @force_off: True when "rdt" command line disables this event group +* to avoid system limitations due to insufficient RMIDs. * @force_on: True when "rdt" command line overrides disable of this * event group. * @guid: Unique number per XML description file.
@@ -228,7 +228,7 @@ static bool enable_events(struct event_group *e, struct pmt_feature_group *p)struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_PERF_PKG ].r_resctrl;int skipped_events =0 ; -if (e->force_off) +if (e->force_off && !e->force_on)return false;if (!group_has_usable_regions (e, p))
Signature · 1 line
2.54.0
Tony Luck
maintainerPATCH v8 8/16 fs/resctrl: Add interface to disable a monitor event
In reply to Tony Luckresctrl currently assumes all monitor events are enabled before any domain is created, because per-domain state is allocated by the architecture's CPU hotplug callbacks. There is no way to disable an event once registered.
AET events are enumerated by the INTEL_PMT_TELEMETRY driver. To allow that driver to be a loadable module, resctrl must tolerate AET events appearing and disappearing, which requires the ability to disable an event when the driver is unloaded.
Add resctrl_disable_mon_event(). The architecture owns domain lifetime and knows mount state, so it is responsible for calling this only while resctrl is unmounted and for cleaning up any per-domain state. Document those requirements in the kerneldoc since they are not enforced in code.
---
v8:
Updated commit message
Clear all architecture controlled mon_event::* fields.
include/linux/resctrl.h | 34 ++++++++++++++++++++++++++++++++++ fs/resctrl/monitor.c | 15 +++++++++++++++ 2 files changed, 49 insertions(+)
include/linux/resctrl.h +34 -0
@@ -414,9 +414,43 @@ u32 resctrl_arch_get_num_closid(struct rdt_resource *r);u32 resctrl_arch_system_num_rmid_idx (void );int resctrl_arch_update_domains (struct rdt_resource *r,u32 closid); +/** +* resctrl_enable_mon_event() - Enable monitoring event +* @eventid: ID of the event +* @any_cpu: True if event data can be read from any CPU. +* @binary_bits: Number of binary places of the fixed-point value expected to +* back a floating point event. Can only be set for floating point +* events. +* @arch_priv: Architecture private data associated with event. Passed back to +* architecture when reading the event via resctrl_arch_rmid_read(). +* +* The file system must not be mounted when enabling an event. +* +* Events that require per-domain (architectural and/or filesystem) state must +* be enabled before the domain structures are allocated. For example before +* CPU hotplug callbacks that allocate domain structures are registered. If the +* architecture discovers a resource after initialization it should enable +* events needing per-domain state before any domain structure allocation which +* should be coordinated with the CPU hotplug callbacks. +* +* Return: +* true if event was successfully enabled, false otherwise. +*/ bool resctrl_enable_mon_event (enum resctrl_event_id eventid,bool any_cpu,unsigned int binary_bits,void *arch_priv); +/** +* resctrl_disable_mon_event() - Disable monitoring event +* @eventid: ID of the event +* +* The file system must not be mounted when disabling an event. +* +* Events that require per-domain (architectural and/or filesystem) state +* will require additional cleanup which should be coordinated with the CPU +* hotplug callbacks. +*/ +void resctrl_disable_mon_event (enum resctrl_event_id eventid); +bool resctrl_is_mon_event_enabled (enum resctrl_event_id eventid);bool resctrl_arch_is_evt_configurable (enum resctrl_event_id evt);
fs/resctrl/monitor.c +15 -0
@@ -1012,6 +1012,21 @@ bool resctrl_enable_mon_event(enum resctrl_event_id eventid, bool any_cpu,return true; } +void resctrl_disable_mon_event (enum resctrl_event_id eventid) +{ +if (WARN_ON_ONCE (eventid <QOS_FIRST_EVENT || eventid >=QOS_NUM_EVENTS )) +return ; +if (!mon_event_all[eventid].enabled) { +pr_warn ("Repeat disable for event %d\n" , eventid); +return ; + } + + mon_event_all[eventid].any_cpu = false; + mon_event_all[eventid].binary_bits =0 ; + mon_event_all[eventid].arch_priv =NULL ; + mon_event_all[eventid].enabled = false; +} +bool resctrl_is_mon_event_enabled (enum resctrl_event_id eventid) { return eventid >= QOS_FIRST_EVENT && eventid < QOS_NUM_EVENTS &&
Signature · 1 line
2.54.0
Tony Luck
maintainerPATCH v8 9/16 x86/resctrl: Drop global 'rdt_mon_capable' flag
In reply to Tony LuckFile system code calls resctrl_arch_mon_capable() to determine if the system supports any monitoring features.
x86 architecture code sets a global flag to support implementation of resctrl_arch_mon_capable() but the upcoming change to enumerate AET (Application Energy Telemetry) features on each mount introduces a new corner case.
Specifically when AET is the only monitoring feature, loading and unloading the pmt_telemetry module between resctrl mounts may result in monitoring support enabled on some mounts, but not on others.
Replace the global flag with a scan of resources to check if any are marked mon_capable.
---
v8:
Change from a count of monitor features to a scan of the rdt_resources checking for any that are mon_capable.
arch/x86/include/asm/resctrl.h | 6 +----- arch/x86/kernel/cpu/resctrl/core.c | 18 +++++++++++++++--- arch/x86/kernel/cpu/resctrl/monitor.c | 6 ------ 3 files changed, 16 insertions(+), 14 deletions(-)
arch/x86/include/asm/resctrl.h +1 -5
@@ -43,7 +43,6 @@ struct resctrl_pqr_state { DECLARE_PER_CPU(struct resctrl_pqr_state, pqr_state);extern bool rdt_alloc_capable; -extern bool rdt_mon_capable;DECLARE_STATIC_KEY_FALSE (rdt_enable_key);DECLARE_STATIC_KEY_FALSE (rdt_alloc_enable_key);
@@ -66,10 +65,7 @@ static inline void resctrl_arch_disable_alloc(void)static_branch_dec_cpuslocked (&rdt_enable_key); } -static inline bool resctrl_arch_mon_capable (void ) -{ -return rdt_mon_capable; -} +bool resctrl_arch_mon_capable (void );static inline void resctrl_arch_enable_mon (void ) {
arch/x86/kernel/cpu/resctrl/core.c +15 -3
@@ -50,6 +50,20 @@ DEFINE_PER_CPU(struct resctrl_pqr_state, pqr_state);*/ bool rdt_alloc_capable; +/* +* Need to re-evaluate on each mount whether any mon_capable resources +* are enabled. +*/ +bool resctrl_arch_mon_capable (void ) +{ +struct rdt_resource *r; + +for_each_mon_capable_rdt_resource (r) +return true; + +return false; +} +static void mba_wrmsr_intel (struct msr_param *m);static void cat_wrmsr (struct msr_param *m);static void mba_wrmsr_amd (struct msr_param *m);
@@ -779,7 +793,6 @@ void resctrl_arch_pre_mount(void)cpus_read_lock ();mutex_lock (&domain_list_lock); r->mon_capable = true; - rdt_mon_capable = true;for_each_online_cpu (cpu)domain_add_cpu_mon (cpu, r);mutex_unlock (&domain_list_lock);
@@ -1013,9 +1026,8 @@ static __init void check_quirks(void)static __init bool get_rdt_resources (void ) { rdt_alloc_capable =get_rdt_alloc_resources (); - rdt_mon_capable =get_rdt_mon_resources (); -return (rdt_mon_capable || rdt_alloc_capable); +return get_rdt_mon_resources () || rdt_alloc_capable; }static __init void rdt_init_res_defs_intel (void )
arch/x86/kernel/cpu/resctrl/monitor.c +0 -6
@@ -25,12 +25,6 @@#include "internal.h" -/* -* Global boolean for rdt_monitor which is true if any -* resource monitoring is enabled. -*/ -bool rdt_mon_capable; -#define CF (cf) ((unsigned long)(1048576 * (cf) + 0.5))static int snc_nodes_per_l3_cache =1 ;
Signature · 1 line
2.54.0
Tony Luck
maintainerPATCH v8 10/16 arm,x86,fs/resctrl: Handle change in number of RMIDs on each mount
In reply to Tony LuckApplication Energy Telemetry (AET) event enumeration takes place asynchronously. Linux builds the pmt_telemetry module into the kernel to kick off enumeration early enough that it completes before first mount of the resctrl file system.
Allowing pmt_telemetry to be a loadable module means that it is possible for different numbers of RMIDs to be supported on each mount, depending on whether pmt_telemetry module is loaded.
For simplicity, calculate the maximum possible number of RMIDs and use that value to allocate the rmid_ptrs[] array just once. Also use this maximum RMID value when allocating rdt_l3_mon_domain::rmid_busy_llc bitmap and rdt_l3_mon_domain::mbm_states.
The limbo code must deal with changes in the number of RMIDs from one mount to the next because some RMIDs may still be "busy" when the file system is unmounted, but be above resctrl_arch_system_num_rmid_idx() for the remount. In this case RMIDs that can be released are not put onto the rmid_free_lru list.
---
v8:
Pick arm,x86,fs/resctrl as standard tag syntax
Update kerneldoc for rdt_l3_mon_domain::rmid_busy_llc and rdt_l3_mon_domain::mbm_states to say they are sized for max RMID. Rename local variable idx_limit. Use max_idx_limit when dealing with the maximum possible RMID value and min_idx_limit when dealing with the minimum value across resources for the current mount. Compute index of reserved RMID instead of assuming "0" in setup_rmid_lru_list()
Update commit comment to provide overview of the change instead of details of the code change.
include/linux/resctrl.h | 5 +- arch/x86/kernel/cpu/resctrl/core.c | 14 ++++++ drivers/resctrl/mpam_resctrl.c | 5 ++ fs/resctrl/monitor.c | 76 +++++++++++++++++++----------- fs/resctrl/rdtgroup.c | 6 +-- 5 files changed, 75 insertions(+), 31 deletions(-)
include/linux/resctrl.h +4 -1
@@ -182,10 +182,12 @@ struct mbm_cntr_cfg {* struct rdt_l3_mon_domain - group of CPUs sharing RDT_RESOURCE_L3 monitoring * @hdr: common header for different domain types * @ci_id: cache info id for this domain -* @rmid_busy_llc: bitmap of which limbo RMIDs are above threshold +* @rmid_busy_llc: bitmap of which limbo RMIDs are above threshold. Sized for +* resctrl_arch_system_max_rmid_idx() RMIDs * @mbm_states: Per-event pointer to the MBM event's saved state. * An MBM event's state is an array of struct mbm_state * indexed by RMID on x86 or combined CLOSID, RMID on Arm. +* Also sized for resctrl_arch_system_max_rmid_idx() RMIDs * @mbm_over: worker to periodically read MBM h/w counters * @cqm_limbo: worker to periodically read CQM h/w counters * @mbm_work_cpu: worker CPU for MBM h/w counters
@@ -412,6 +414,7 @@ static inline u32 resctrl_get_default_ctrl(struct rdt_resource *r)/* The number of closid supported by this resource regardless of CDP */ u32 resctrl_arch_get_num_closid (struct rdt_resource *r);u32 resctrl_arch_system_num_rmid_idx (void ); +u32 resctrl_arch_system_max_rmid_idx (void );int resctrl_arch_update_domains (struct rdt_resource *r,u32 closid);/**
arch/x86/kernel/cpu/resctrl/core.c +14 -0
@@ -143,6 +143,20 @@ u32 resctrl_arch_system_num_rmid_idx(void)return num_rmids ==U32_MAX ?0 : num_rmids; } +/** +* resctrl_arch_system_max_rmid_idx - Largest possible number of RMIDs +* +* Return: If L3 monitoring is supported, largest possible comes from L3 based +* on CPUID(0xf,0x0).EBX (scaled down on Sub-NUMA Cluster systems). Otherwise +* maximum from any other mon_capable resources. +*/ +u32 resctrl_arch_system_max_rmid_idx (void ) +{ +struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3 ].r_resctrl; + +return r->mon_capable ? r->mon.num_rmid :resctrl_arch_system_num_rmid_idx (); +} +struct rdt_resource *resctrl_arch_get_resource (enum resctrl_res_level l) {if (l >=RDT_NUM_RESOURCES )
drivers/resctrl/mpam_resctrl.c +5 -0
@@ -272,6 +272,11 @@ u32 resctrl_arch_system_num_rmid_idx(void)return (mpam_pmg_max +1 ) * (mpam_partid_max +1 ); } +u32 resctrl_arch_system_max_rmid_idx (void ) +{ +return resctrl_arch_system_num_rmid_idx (); +} +u32 resctrl_arch_rmid_idx_encode (u32 closid,u32 rmid) {return closid * (mpam_pmg_max +1 ) + rmid;
fs/resctrl/monitor.c +49 -27
147 lines not on this page · raw message
fs/resctrl/rdtgroup.c +3 -3
@@ -4417,13 +4417,13 @@ void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_domain_hdr *h*/ static int domain_setup_l3_mon_state (struct rdt_resource *r,struct rdt_l3_mon_domain *d) { -u32 idx_limit =resctrl_arch_system_num_rmid_idx (); +u32 max_idx_limit =resctrl_arch_system_max_rmid_idx ();size_t tsize =sizeof (*d->mbm_states[0 ]);enum resctrl_event_id eventid;int idx;if (resctrl_is_mon_event_enabled (QOS_L3_OCCUP_EVENT_ID )) { - d->rmid_busy_llc =bitmap_zalloc (idx_limit,GFP_KERNEL ); + d->rmid_busy_llc =bitmap_zalloc (max_idx_limit,GFP_KERNEL );if (!d->rmid_busy_llc)return -ENOMEM ; }
@@ -4432,7 +4432,7 @@ static int domain_setup_l3_mon_state(struct rdt_resource *r, struct rdt_l3_mon_dif (!resctrl_is_mon_event_enabled (eventid))continue ; idx =MBM_STATE_IDX (eventid); - d->mbm_states[idx] =kcalloc (idx_limit, tsize,GFP_KERNEL ); + d->mbm_states[idx] =kcalloc (max_idx_limit, tsize,GFP_KERNEL );if (!d->mbm_states[idx]) goto cleanup; }
Signature · 1 line
2.54.0
Tony Luck
maintainerPATCH v8 11/16 x86/resctrl: Add PMT registration API for AET enumeration callbacks
In reply to Tony Luckresctrl is always built-in; INTEL_PMT_TELEMETRY may be a module. Add, and export, register/unregister functions so the PMT module can supply/clear enumeration callback functions when loaded/unloaded.
---
v8:
Add comment for what aet_register_lock will protect
arch/x86/include/asm/resctrl.h | 22 ++++++++++++++++ arch/x86/kernel/cpu/resctrl/intel_aet.c | 34 +++++++++++++++++++++++++ 2 files changed, 56 insertions(+)
arch/x86/include/asm/resctrl.h +22 -0
@@ -4,6 +4,8 @@#ifdef CONFIG_X86_CPU_RESCTRL +#include <linux/intel_pmt_features.h> +#include <linux/intel_vsec.h> #include <linux/jump_label.h> #include <linux/percpu.h> #include <linux/resctrl_types.h>
@@ -189,11 +191,31 @@ static inline void resctrl_arch_mon_ctx_free(struct rdt_resource *r,void resctrl_cpu_detect (struct cpuinfo_x86 *c); +#ifdef CONFIG_X86_CPU_RESCTRL_INTEL_AET +void intel_aet_register_enumeration (struct module *module, +struct pmt_feature_group *(*get)(enum pmt_feature_id id), +void (*put)(struct pmt_feature_group *p)); +void intel_aet_unregister_enumeration (void );#else +static inline void intel_aet_register_enumeration (struct module *module, +struct pmt_feature_group *(*get)(enum pmt_feature_id id), +void (*put)(struct pmt_feature_group *p)) { } +static inline void intel_aet_unregister_enumeration (void ) { } +#endif /* CONFIG_X86_CPU_RESCTRL_INTEL_AET */ + +#else + +#include <linux/intel_pmt_features.h> +#include <linux/intel_vsec.h> static inline void resctrl_arch_sched_in (struct task_struct *tsk) {}static inline void resctrl_cpu_detect (struct cpuinfo_x86 *c) {} +static inline void intel_aet_register_enumeration (struct module *module, +struct pmt_feature_group *(*get)(enum pmt_feature_id id), +void (*put)(struct pmt_feature_group *p)) { } +static inline void intel_aet_unregister_enumeration (void ) { } +#endif /* CONFIG_X86_CPU_RESCTRL */ #endif /* _ASM_X86_RESCTRL_H */
arch/x86/kernel/cpu/resctrl/intel_aet.c +34 -0
@@ -12,17 +12,21 @@#define pr_fmt (fmt) "resctrl: " fmt#include <linux/bits.h> +#include <linux/cleanup.h> #include <linux/compiler_types.h> #include <linux/container_of.h> #include <linux/cpumask.h> #include <linux/err.h> #include <linux/errno.h> +#include <linux/export.h> #include <linux/gfp_types.h> #include <linux/init.h> #include <linux/intel_pmt_features.h> #include <linux/intel_vsec.h> #include <linux/io.h> #include <linux/minmax.h> +#include <linux/mutex.h> +#include <linux/module.h> #include <linux/printk.h> #include <linux/rculist.h> #include <linux/rcupdate.h>
@@ -291,6 +295,10 @@ static enum pmt_feature_id lookup_pfid(const char *pfname)return FEATURE_INVALID ; } +static struct module *pmt_module; +static struct pmt_feature_group *(*get_feature)(enum pmt_feature_id id); +static void (*put_feature)(struct pmt_feature_group *p); +/* * Request a copy of struct pmt_feature_group for each event group. If there is * one, the returned structure has an array of telemetry_region structures,
@@ -325,6 +333,32 @@ bool intel_aet_get_events(void)return ret; } +/* +* Defend against races between pmt_telemetry register/unregister and +* resctrl file system mount/unmount. +*/ +static DEFINE_MUTEX (aet_register_lock); + +void intel_aet_register_enumeration (struct module *module, +struct pmt_feature_group *(*get)(enum pmt_feature_id id), +void (*put)(struct pmt_feature_group *p)) +{ +guard (mutex)(&aet_register_lock); + get_feature = get; + put_feature = put; + pmt_module = module; +} +EXPORT_SYMBOL_NS_GPL (intel_aet_register_enumeration,"INTEL_PMT" ); + +void intel_aet_unregister_enumeration (void ) +{ +guard (mutex)(&aet_register_lock); + pmt_module =NULL ; + get_feature =NULL ; + put_feature =NULL ; +} +EXPORT_SYMBOL_NS_GPL (intel_aet_unregister_enumeration,"INTEL_PMT" ); + void __exit intel_aet_exit(void) {struct event_group **peg;
Signature · 1 line
2.54.0
Tony Luck
maintainerPATCH v8 12/16 platform/x86/intel/pmt: Register enumeration functions with resctrl
In reply to Tony LuckINTEL_PMT_TELEMETRY is a loadable module, but resctrl is built-in and cannot call PMT functions directly. Register the telemetry enumeration function pointers at pmt_telemetry module .probe(), and unregister them at module .remove().
Note that checkpatch complains about the #include of <asm/resctrl.h>. This is needed rather than <linux/resctrl.h> to get the function stub definitions when CONFIG_X86_CPU_RESCTRL=n.
---
v8:
Move register/unregister calls to .probe()/.remove()
drivers/platform/x86/intel/pmt/telemetry.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/platform/x86/intel/pmt/telemetry.c +6 -0
@@ -25,6 +25,8 @@#include <linux/uaccess.h> #include <linux/xarray.h> +#include <asm/resctrl.h> +#include "class.h" #define TELEM_SIZE_OFFSET 0x0
@@ -365,6 +367,8 @@ static void pmt_telem_remove(struct auxiliary_device *auxdev)struct pmt_telem_priv *priv =auxiliary_get_drvdata (auxdev);int i; +intel_aet_unregister_enumeration (); +mutex_lock (&ep_lock);for (i =0 ; i < priv->num_entries; i++) {struct intel_pmt_entry *entry = &priv->entry[i];
@@ -405,6 +409,8 @@ static int pmt_telem_probe(struct auxiliary_device *auxdev, const struct auxiliaintel_pmt_get_features (entry); } +intel_aet_register_enumeration (THIS_MODULE , intel_pmt_get_regions_by_feature, + intel_pmt_put_feature_group);return 0 ; abort_probe:pmt_telem_remove (auxdev);
Signature · 1 line
2.54.0
Tony Luck
maintainerPATCH v8 13/16 arm,x86/resctrl: Resolve INTEL_PMT_TELEMETRY symbols at runtime
In reply to Tony Luckresctrl is always built-in, but INTEL_PMT_TELEMETRY and INTEL_TPMI are logically independent and should be loadable modules. Switch AET to use the function-pointer registration API instead of direct link-time references to PMT symbols.
Prepare for the file system to call resctrl_arch_pre_mount() on every mount by moving AET enumeration into resctrl_arch_pre_mount() and cleanup into resctrl_arch_unmount(). This allows the PMT module to be unloaded whenever the filesystem is not mounted.
Remove intel_aet_exit because all cleanup now happens in the unmount path.
Note that the Linux file system code does not serialize calls to fs_context_operations::get_tree(), so there may be arbitrarily many parallel calls if users invoke mount(2) multiple times.
Use an atomic counter to detect and ignore nested mount attempts. AET doesn't need to do anything for these. File system will fail them with an -EBUSY error code.
event_group::num_rmid may be reset (reduced) during enumeration. This is not worth resetting on unmount because the same reduction would occur on each subsequent mount.
---
v8:
Drop reference to preceding patch in commit
Drop resctrl_arch_mount_lock and replace with atomic ops Fix comment about "have_pmt_hold" in intel_aet_pre_mount() Rename have_pmt_hold to pmt_in_use and simplify mount/unmount Add intel_aet_try_module_get() and intel_aet_module_put() helpers with stubs for the CONFIG_INTEL_PMT_TELEMETRY=y case.
include/linux/resctrl.h | 6 +++ arch/x86/kernel/cpu/resctrl/internal.h | 23 +++++++++-- arch/x86/kernel/cpu/resctrl/core.c | 37 +++++++++++++++-- arch/x86/kernel/cpu/resctrl/intel_aet.c | 53 +++++++++++++++++++++---- drivers/resctrl/mpam_resctrl.c | 4 ++ 5 files changed, 108 insertions(+), 15 deletions(-)
include/linux/resctrl.h +6 -0
@@ -559,6 +559,12 @@ void resctrl_offline_cpu(unsigned int cpu);*/ void resctrl_arch_pre_mount (void ); +/* +* Architecture hook called when mount fails, or on unmount. +* No locks are held. +*/ +void resctrl_arch_unmount (void ); +/** * resctrl_arch_rmid_read() - Read the eventid counter corresponding to rmid * for this resource and domain.
arch/x86/kernel/cpu/resctrl/internal.h +19 -4
@@ -234,15 +234,15 @@ void rdt_domain_reconfigure_cdp(struct rdt_resource *r);void resctrl_arch_mbm_cntr_assign_set_one (struct rdt_resource *r);#ifdef CONFIG_X86_CPU_RESCTRL_INTEL_AET -bool intel_aet_get_events (void ); -void __exit intel_aet_exit(void); +bool intel_aet_pre_mount (void ); +void intel_aet_unmount (void );int intel_aet_read_event (int domid,u32 rmid,void *arch_priv,u64 *val);void intel_aet_mon_domain_setup (int cpu,int id,struct rdt_resource *r,struct list_head *add_pos);bool intel_handle_aet_option (bool force_off,char *tok);#else -static inline bool intel_aet_get_events (void ) {return false; } -static inline void __exit intel_aet_exit(void) { } +static inline bool intel_aet_pre_mount (void ) {return false; } +static inline void intel_aet_unmount (void ) { }static inline int intel_aet_read_event (int domid,u32 rmid,void *arch_priv,u64 *val) {return -EINVAL ;
@@ -253,4 +253,19 @@ static inline void intel_aet_mon_domain_setup(int cpu, int id, struct rdt_resourstatic inline bool intel_handle_aet_option (bool force_off,char *tok) {return false; }#endif +#ifdef CONFIG_INTEL_PMT_TELEMETRY_MODULE +static inline bool intel_aet_try_module_get (struct module *mod) +{ +return try_module_get (mod); +} + +static inline void intel_aet_module_put (struct module *mod) +{ +module_put (mod); +} +#else +static inline bool intel_aet_try_module_get (struct module *mod) {return true; } +static inline void intel_aet_module_put (struct module *mod) { } +#endif +#endif /* _ASM_X86_RESCTRL_INTERNAL_H */
arch/x86/kernel/cpu/resctrl/core.c +34 -3
@@ -16,10 +16,13 @@#define pr_fmt (fmt) "resctrl: " fmt +#include <linux/atomic.h> +#include <linux/cleanup.h> #include <linux/cpu.h> #include <linux/slab.h> #include <linux/err.h> #include <linux/cpuhotplug.h> +#include <linux/mutex.h> #include <asm/cpu_device_id.h> #include <asm/msr.h>
@@ -792,12 +795,17 @@ static int resctrl_arch_offline_cpu(unsigned int cpu)return 0 ; } +static atomic_t resctrl_arch_mount_entries; +void resctrl_arch_pre_mount (void ) {struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_PERF_PKG ].r_resctrl;int cpu; -if (!intel_aet_get_events ()) +if (atomic_inc_return (&resctrl_arch_mount_entries) >1 ) +return ; + +if (!intel_aet_pre_mount ())return ;/*
@@ -813,6 +821,31 @@ void resctrl_arch_pre_mount(void)cpus_read_unlock (); } +void resctrl_arch_unmount (void ) +{ +struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_PERF_PKG ].r_resctrl; +int n =atomic_dec_return (&resctrl_arch_mount_entries); +int cpu; + +if (n >0 ) +return ; + +WARN_ON_ONCE (n <0 ); + +intel_aet_unmount (); + +if (!r->mon_capable) +return ; + +cpus_read_lock (); +mutex_lock (&domain_list_lock); +for_each_online_cpu (cpu) +domain_remove_cpu_mon (cpu, r); + r->mon_capable = false; +mutex_unlock (&domain_list_lock); +cpus_read_unlock (); +} +enum {RDT_FLAG_CMT ,RDT_FLAG_MBM_TOTAL ,
@@ -1186,8 +1219,6 @@ late_initcall(resctrl_arch_late_init); static void __exit resctrl_arch_exit(void) { -intel_aet_exit (); -cpuhp_remove_state (rdt_online);resctrl_exit ();
arch/x86/kernel/cpu/resctrl/intel_aet.c +45 -8
90 lines not on this page · raw message
drivers/resctrl/mpam_resctrl.c +4 -0
@@ -162,6 +162,10 @@ void resctrl_arch_pre_mount(void) { } +void resctrl_arch_unmount (void ) +{ +} +bool resctrl_arch_get_cdp_enabled (enum resctrl_res_level rid) {return mpam_resctrl_controls[rid].cdp_enabled;
Signature · 1 line
2.54.0
Tony Luck
maintainerPATCH v8 14/16 fs/resctrl: Call architecture hooks for every mount/unmount
In reply to Tony LuckThe intel_pmt module should only be pinned while the resctrl file system is mounted.
Add hooks for every mount/unmount of the resctrl file system so that architecture code can increment the intel_pmt module reference count on mount and decrement on unmount.
---
v8:
Remove check for "ret" in rdt_get_tree() error path
include/linux/resctrl.h | 2 +- fs/resctrl/rdtgroup.c | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-)
include/linux/resctrl.h +1 -1
@@ -554,7 +554,7 @@ void resctrl_online_cpu(unsigned int cpu);void resctrl_offline_cpu (unsigned int cpu);/* -* Architecture hook called at beginning of first file system mount attempt. +* Architecture hook called before attempting to mount the file system. * No locks are held. */ void resctrl_arch_pre_mount (void );
fs/resctrl/rdtgroup.c +6 -2
@@ -18,7 +18,6 @@#include <linux/fs_parser.h> #include <linux/sysfs.h> #include <linux/kernfs.h> -#include <linux/once.h> #include <linux/resctrl.h> #include <linux/seq_buf.h> #include <linux/seq_file.h>
@@ -2998,6 +2997,8 @@ static void resctrl_unmount(void) resctrl_mounted = false;mutex_unlock (&rdtgroup_mutex);cpus_read_unlock (); + +resctrl_arch_unmount (); }static int rdt_get_tree (struct fs_context *fc)
@@ -3009,7 +3010,7 @@ static int rdt_get_tree(struct fs_context *fc)struct rdt_resource *r;int ret; -DO_ONCE_SLEEPABLE (resctrl_arch_pre_mount); +resctrl_arch_pre_mount ();cpus_read_lock ();mutex_lock (&rdtgroup_mutex);
@@ -3148,6 +3149,9 @@ static int rdt_get_tree(struct fs_context *fc) out:mutex_unlock (&rdtgroup_mutex);cpus_read_unlock (); + +resctrl_arch_unmount (); +return ret; }
Signature · 1 line
2.54.0
Tony Luck
maintainerPATCH v8 15/16 x86/resctrl: Simplify Kconfig options for resctrl
In reply to Tony LuckLinus Torvalds complained[1] about Kconfig complexity making it too hard for "random people to build their own kernels".
CONFIG_X86_CPU_RESCTRL_INTEL_AET has been causing problems since it was first added as it required other config options to be set to "built-in".
AET now resolves PMT symbols at runtime via the registration API, so INTEL_PMT_TELEMETRY no longer needs to be built-in. This means that AET can be included unconditionally as part of X86_CPU_RESCTRL.
CONFIG_X86_CPU_RESCTRL now depends on X86_64 because reading telemetry data needs readq().
--- arch/x86/include/asm/resctrl.h | 7 ------- arch/x86/kernel/cpu/resctrl/internal.h | 13 ------------- arch/x86/Kconfig | 15 +-------------- arch/x86/kernel/cpu/resctrl/Makefile | 2 +- 4 files changed, 2 insertions(+), 35 deletions(-)
arch/x86/include/asm/resctrl.h +0 -7
@@ -191,17 +191,10 @@ static inline void resctrl_arch_mon_ctx_free(struct rdt_resource *r,void resctrl_cpu_detect (struct cpuinfo_x86 *c); -#ifdef CONFIG_X86_CPU_RESCTRL_INTEL_AET void intel_aet_register_enumeration (struct module *module,struct pmt_feature_group *(*get)(enum pmt_feature_id id),void (*put)(struct pmt_feature_group *p));void intel_aet_unregister_enumeration (void ); -#else -static inline void intel_aet_register_enumeration (struct module *module, -struct pmt_feature_group *(*get)(enum pmt_feature_id id), -void (*put)(struct pmt_feature_group *p)) { } -static inline void intel_aet_unregister_enumeration (void ) { } -#endif /* CONFIG_X86_CPU_RESCTRL_INTEL_AET */ #else
arch/x86/kernel/cpu/resctrl/internal.h +0 -13
@@ -233,25 +233,12 @@ void __init intel_rdt_mbm_apply_quirk(void);void rdt_domain_reconfigure_cdp (struct rdt_resource *r);void resctrl_arch_mbm_cntr_assign_set_one (struct rdt_resource *r); -#ifdef CONFIG_X86_CPU_RESCTRL_INTEL_AET bool intel_aet_pre_mount (void );void intel_aet_unmount (void );int intel_aet_read_event (int domid,u32 rmid,void *arch_priv,u64 *val);void intel_aet_mon_domain_setup (int cpu,int id,struct rdt_resource *r,struct list_head *add_pos);bool intel_handle_aet_option (bool force_off,char *tok); -#else -static inline bool intel_aet_pre_mount (void ) {return false; } -static inline void intel_aet_unmount (void ) { } -static inline int intel_aet_read_event (int domid,u32 rmid,void *arch_priv,u64 *val) -{ -return -EINVAL ; -} - -static inline void intel_aet_mon_domain_setup (int cpu,int id,struct rdt_resource *r, -struct list_head *add_pos) { } -static inline bool intel_handle_aet_option (bool force_off,char *tok) {return false; } -#endif #ifdef CONFIG_INTEL_PMT_TELEMETRY_MODULE static inline bool intel_aet_try_module_get (struct module *mod)
arch/x86/Kconfig +1 -14
@@ -515,7 +515,7 @@ config X86_MPPARSE config X86_CPU_RESCTRL bool "x86 CPU resource control support" - depends on X86 && (CPU_SUP_INTEL || CPU_SUP_AMD) + depends on X86_64 && (CPU_SUP_INTEL || CPU_SUP_AMD) depends on MISC_FILESYSTEMS select ARCH_HAS_CPU_RESCTRL select RESCTRL_FS
@@ -536,19 +536,6 @@ config X86_CPU_RESCTRL Say N if unsure. -config X86_CPU_RESCTRL_INTEL_AET - bool "Intel Application Energy Telemetry" - depends on X86_64 && X86_CPU_RESCTRL && CPU_SUP_INTEL && INTEL_PMT_TELEMETRY=y && INTEL_TPMI=y - help - Enable per-RMID telemetry events in resctrl. - - Intel feature that collects per-RMID execution data - about energy consumption, measure of frequency independent - activity and other performance metrics. Data is aggregated - per package. - - Say N if unsure. - config X86_FRED bool "Flexible Return and Event Delivery" depends on X86_64
arch/x86/kernel/cpu/resctrl/Makefile +1 -1
@@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_X86_CPU_RESCTRL) += core.o rdtgroup.o monitor.o obj-$(CONFIG_X86_CPU_RESCTRL) += ctrlmondata.o -obj-$(CONFIG_X86_CPU_RESCTRL_INTEL_AET) += intel_aet.o +obj-$(CONFIG_X86_CPU_RESCTRL) += intel_aet.o obj-$(CONFIG_RESCTRL_FS_PSEUDO_LOCK) += pseudo_lock.o # To allow define_trace.h's recursive include:
Signature · 1 line
2.54.0
Tony Luck
maintainerPATCH v8 16/16 Documentation/filesystems/resctrl: Add footnote for telemetry fstab mount caveat
In reply to Tony LuckAdd a footnote to the 'If telemetry monitoring is enabled' sentence noting that the PMT driver enumerates telemetry features asynchronously, so an automatic mount of resctrl from /etc/fstab at boot may occur before those features are available, resulting in them not being enabled.
The time for enumeration is only a few seconds, so retrying the remount should work.
---
v8:
Added to footnote that a remount should fix
Documentation/filesystems/resctrl.rst | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
Documentation/filesystems/resctrl.rst +6 -1
@@ -620,7 +620,7 @@ When monitoring is enabled all MON groups will also contain: each instance of an L3 cache. Each directory contains files for the enabled L3 events (e.g. "llc_occupancy", "mbm_total_bytes", and "mbm_local_bytes"). - If telemetry monitoring is enabled, there will be a "mon_PERF_PKG_YY" + If telemetry monitoring is enabled [#]_, there will be a "mon_PERF_PKG_YY" directory for each physical processor package. Each directory contains files for the enabled telemetry events (e.g. "core_energy". "activity", "uops_retired", etc.)
@@ -659,6 +659,11 @@ When monitoring is enabled all MON groups will also contain: returned if the MBM event does not have an assigned counter in the CTRL_MON group nor in any of its associated MON groups. +.. [#] Telemetry features are enumerated asynchronously by the PMT driver. If + resctrl is automatically mounted from ``/etc/fstab`` at boot, the telemetry + features may not yet be available at mount time and will therefore not be + enabled. Remounting a few seconds later should fix this. + "mon_hw_id": Available only with debug option. The identifier used by hardware for the monitor group. On x86 this is the RMID.
Signature · 1 line
2.54.0
Luck, Tony
maintainerQuoted 6 lines · Tony Luck
Requiring INTEL_PMT_TELEMETRY=y to enable AET is a functional workaround to enable enumeration of Application Energy Telemetry (AET) events, but unacceptable to many users. It results in increased configration complexity, increased kernel memory footprint and inability to patch problems by unloading a module and loading an updated version.
Quoted 6 lines
Add a registration function to the AET code that can be used by
INTEL_PMT_TELEMETRYto provide the enumeration functions.
INTEL_PMT_TELEMETRYcan be loaded/unloaded independently of resctrl file system mount/unmount. Perform enumeration on every mount and cleanup on every unmount.
Sashiko complained about some patches.
https://sashiko.dev/#/patchset/20260615182457.14725-1-tony.luck%40intel.com
Patch 4: Complains about hotplug and worker threads. Dealt with by other patches in Reinette's series that I didn't include.
Patch 5: Question on whether unbinding the parent of the pmt_telemetry driver (intel_vsec) will cause problems. I'll check this.
Patch 6: Repeat of patch 4 worker thread issue, handled by Reinette.
Patch 8: Race between unmount and subsequent mount relating to disabling events. Will investigate.
Patch 9: Same unbind intel_vsec issue as reported for patch 5
Patch 10: Same worker thread issue reported for patch 4, 6
Patch 12: Sashiko concerned about multiple telemetry devices (which there are). But for the resctrl use case it doesn't matter if some subset are offline, or broken. Resctrl needs all of the, to work. I think I just need commit and code comments to document that it is okay for first .remove() to unregister etc.
Patch 13: My atomic operations in resctrl_arch_pre_mount() and resctrl_arch_unmount() are garbage (sashiko was more polite, just pointing out the obvious race). I'll drop these from next version and handle the nested mount case in intel_aet_pre_mount().
Repeat of patch 12 complaint about single PMT devices being removed.
Patch 14: Repeat of patch 13 race condition complaint.
Patch 15: Repeat of patch 12 complaint about handling mutliple telemetry devices.
Complaint they stubs for intel_aet_try_module_get() and intel_aet_module_put() don't handle out-of-tree telemetry enumeration modules - Linux in general doesn't care about out
of tree code.
-Tony