fs/resctrl: Fix four long-standing issues
Tony Luck
maintainerv2 cover fs/resctrl: Fix four long-standing issues
Sashiko reported a deadlock during mount, and a use-after-free when an L3 domain is removed during CPU offline. Reinette found a memory leak in the mount error path while refactoring code for a solution to the mount hang. Sashiko found the unmount issue in review of v1 of this series.
First version of series posted here:
Numerous changes based on comments to v1.
Patch 1 just reorders some code so that fixes can be applied without adding additional forward declaration of functions.
Patch 2 fixes the memory leak found by Reinette
Patch 3 fixes use-after-free during unmount
Patch 4 fixes the mount deadlock
Patch 5 fixes issues with CPU offline
N.B. Reinette did all the work for patches 4 & 5, so I listed her as author an me as Co-developer. Those patches need her sign-off.
Reinette Chatre (2):
fs/resctrl: Fix deadlock for errors during mount fs/resctrl: Fix issues with worker threads when CPUs are taken offline
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
fs/resctrl/monitor.c | 55 +++++ fs/resctrl/rdtgroup.c | 473 +++++++++++++++++++++++------------------- 2 files changed, 320 insertions(+), 208 deletions(-)
base-commit: 5d6919055dec134de3c40167a490f33c74c12581
Signature · 1 line
2.54.0
Tony Luck
maintainerPATCH v2 1/5 fs/resctrl: Move functions to avoid forward references in subsequent fixes
No functional change. Just pull some functions before rdt_get_tree().
--- 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
Babu Moger
reviewerRe: [PATCH v2 1/5] fs/resctrl: Move functions to avoid forward references in subsequent fixes
Hi Tony,
Quoted 1 line · Tony Luck
No functional change. Just pull some functions before
rdt_get_tree().
Would it be better to list functions instead of "some functions" ?
Thanks
Babu
Quoted 400 lines
Signed-off-by Tony Luck <[email protected]>--- 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;
Tony Luck
maintainerPATCH v2 2/5 fs/resctrl: Free mon_data structures on rdt_get_tree() failure
In reply to Tony LuckIf mkdir_mondata_all() succeeds but 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_mondata error path to free them.
--- 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
Re: [PATCH v2 2/5] fs/resctrl: Free mon_data structures on rdt_get_tree() failure
Quoted 8 lines · Tony Luck
If
mkdir_mondata_all()succeeds but a subsequent call inrdt_get_tree()fails, themon_datastructures allocated bymon_get_kn_priv()are leaked. Addmon_put_kn_priv()to theout_mondataerror path to free them.Fixes 2a6566038544 ("x86/resctrl: Expand the width of domid by replacing mon_data_bits")Reported-by Reinette Chatre <[email protected]>Signed-off-by Tony Luck <[email protected]>
thanks,
Chenyu
Babu Moger
reviewerRe: [PATCH v2 2/5] fs/resctrl: Free mon_data structures on rdt_get_tree() failure
In reply to Tony LuckHi Tony,
Quoted 3 lines · Tony Luck
If
mkdir_mondata_all()succeeds but a subsequent call inrdt_get_tree()fails, themon_datastructures allocated bymon_get_kn_priv()are leaked. Addmon_put_kn_priv()to theout_mondataerror path to free
out_mondata -> out_mongrp ?
Thanks
Babu
Quoted 21 lines
them.
Fixes 2a6566038544 ("x86/resctrl: Expand the width of domid by replacing mon_data_bits")Reported-by Reinette Chatre <[email protected]>Signed-off-by Tony Luck <[email protected]>--- 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); }
Tony Luck
maintainerPATCH v2 3/5 fs/resctrl: Fix use-after-free during unmount
In reply to Tony LuckSashiko reported[1] this issue:
During unmount or failure teardown, resctrl_fs_teardown() calls mon_put_kn_priv() (which frees all mon_data structures) followed by rdtgroup_destroy_root() (which destroys kernfs nodes). However, the RDT_DELETED flag is never set for rdtgroup_default.
If a concurrent reader (e.g., rdtgroup_mondata_show()) invokes rdtgroup_kn_lock_live(), it drops kernfs active protection and blocks on rdtgroup_mutex. resctrl_fs_teardown() (holding the mutex) proceeds to free the private data and destroy the nodes 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 of->kn->priv pointer.
Set RDT_DELETED for the default group (if there are any tasks waiting).
--- fs/resctrl/rdtgroup.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
fs/resctrl/rdtgroup.c +15 -0
@@ -593,6 +593,13 @@ static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,*/ static void rdtgroup_remove (struct rdtgroup *rdtgrp) { +/* +* Groups created with mkdir() have an extra hold, that doesn't +* apply to the default group. It is stacically allocated, so +* does not need to be freed. +*/ +if (rdtgrp == &rdtgroup_default) +return ;kernfs_put (rdtgrp->kn);kfree (rdtgrp); }
@@ -2965,6 +2972,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 +2998,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 +4279,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
Babu Moger
reviewerRe: [PATCH v2 3/5] fs/resctrl: Fix use-after-free during unmount
Hi Tony,
Quoted 12 lines · Tony Luck
Sashiko reported[1] this issue:
During unmount or failure teardown,
resctrl_fs_teardown()callsmon_put_kn_priv()(which frees allmon_datastructures) followed byrdtgroup_destroy_root()(which destroys kernfs nodes). However, theRDT_DELETEDflag is never set forrdtgroup_default.If a concurrent reader (e.g.,
rdtgroup_mondata_show()) invokesrdtgroup_kn_lock_live(), it drops kernfs active protection and blocks onrdtgroup_mutex.resctrl_fs_teardown()(holding the mutex) proceeds to free the private data and destroy the nodes without waiting for the reader.
Quoted 5 lines
When the mutex is released, the reader wakes up, observes that
RDT_DELETEDis not set for the default group, and dereferences the already-freedof->kn->privpointer.Set
RDT_DELETEDfor the default group (if there are any tasks waiting).
It only mentions about the one fix. It is better to add a line about fixing "Avoid races from pending operations from a previous mount".
Quoted 13 lines
Fixes 60cf5e101fd4 ("x86/intel_rdt: Add mkdir to resctrl file system")Signed-off-by Tony Luck <[email protected]>--- fs/resctrl/rdtgroup.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)fs/resctrl/rdtgroup.c +0
-0
fs/resctrl/rdtgroup.c static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of, · line 598
Quoted 6 lines
fs/resctrl/rdtgroup.c +3
-0@@ -593,6 +593,13 @@ static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,*/ static void rdtgroup_remove (struct rdtgroup *rdtgrp) { +/* +* Groups created with mkdir() have an extra hold, that doesn't +* apply to the default group. It is stacically allocated, so
stacically -> statically
Thanks
Babu
Quoted 36 lines
fs/resctrl/rdtgroup.c +12
-0+* does not need to be freed. +*/ +if (rdtgrp == &rdtgroup_default) +return ;kernfs_put (rdtgrp->kn);kfree (rdtgrp); }@@ -2965,6 +2972,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 +2998,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 +4279,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 ; }
Tony Luck
maintainerPATCH v2 4/5 fs/resctrl: Fix deadlock for errors during mount
In reply to Tony LuckFrom: Reinette Chatre <[email protected]>
Sashiko noticed[1] a deadlock in the resctrl mount code.
rdt_get_tree() acquires rdtgroup_mutex before calling kernfs_get_tree(). If superblock setup fails inside kernfs_get_tree(), the VFS calls kill_sb on the same thread before the call returns. rdt_kill_sb() unconditionally attempts to acquire rdtgroup_mutex and deadlock occurs.
Move the call to kernfs_get_tree() outside of locks.
Add resctrl_unmount() helper to keep code consistent between the rdt_get_tree() failure path and a normal unmount.
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.
Add an extra hold in this error path on rdtgroup_default.kn to defend against other races destroying the root which is then dereferenced in kernfs_kill_sb()
--- fs/resctrl/rdtgroup.c | 82 +++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 27 deletions(-)
fs/resctrl/rdtgroup.c +55 -27
@@ -2978,10 +2978,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;
@@ -3057,10 +3081,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 ())
@@ -3076,10 +3096,37 @@ static int rdt_get_tree(struct fs_context *fc) RESCTRL_PICK_ANY_CPU); } - goto out; +/* +* Ensure root kn 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. +*/ +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);
@@ -3099,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;
@@ -3186,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
Babu Moger
reviewerRe: [PATCH v2 4/5] fs/resctrl: Fix deadlock for errors during mount
Hi Tony,
Quoted 17 lines · Tony Luck
From: Reinette Chatre <[email protected]>
Sashiko noticed[1] a deadlock in the resctrl mount code.
rdt_get_tree()acquiresrdtgroup_mutexbefore callingkernfs_get_tree(). If superblock setup fails insidekernfs_get_tree(), the VFS callskill_sbon the same thread before the call returns.rdt_kill_sb()unconditionally attempts to acquirerdtgroup_mutexand deadlock occurs.Move the call to
kernfs_get_tree()outside of locks.Add
resctrl_unmount()helper to keep code consistent between therdt_get_tree()failure path and a normal unmount.If
kernfs_get_tree()fails andctx->kfc.new_sb_createdis set, thenrdt_kill_sb()has already been called and no further cleanup is needed.
Quoted 6 lines
Add an extra hold in this error path on rdtgroup_default.kn to defend against other races destroying the root which is then dereferenced in
kernfs_kill_sb()Fixes 5ff193fbde20 ("x86/intel_rdt: Add basic resctrl filesystem support")Co-developed-by Tony Luck <[email protected]>Signed-off-by Tony Luck <[email protected]>
This should be
Thanks
Babu
Quoted 132 lines
--- fs/resctrl/rdtgroup.c | 82 +++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 27 deletions(-)fs/resctrl/rdtgroup.c +55
-27@@ -2978,10 +2978,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;@@ -3057,10 +3081,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 ())@@ -3076,10 +3096,37 @@ static int rdt_get_tree(struct fs_context *fc) RESCTRL_PICK_ANY_CPU); } - goto out; +/* +* Ensure root kn 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. +*/ +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);@@ -3099,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;@@ -3186,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 = {
Reinette Chatre
maintainerRe: [PATCH v2 4/5] fs/resctrl: Fix deadlock for errors during mount
Quoted 28 lines · Babu Moger
Hi Tony,
Quoted 17 lines · Tony Luck
From: Reinette Chatre <[email protected]>
Sashiko noticed[1] a deadlock in the resctrl mount code.
rdt_get_tree()acquiresrdtgroup_mutexbefore callingkernfs_get_tree(). If superblock setup fails insidekernfs_get_tree(), the VFS callskill_sbon the same thread before the call returns.rdt_kill_sb()unconditionally attempts to acquirerdtgroup_mutexand deadlock occurs.Move the call to
kernfs_get_tree()outside of locks.Add
resctrl_unmount()helper to keep code consistent between therdt_get_tree()failure path and a normal unmount.If
kernfs_get_tree()fails andctx->kfc.new_sb_createdis set, thenrdt_kill_sb()has already been called and no further cleanup is needed.Quoted 6 lines
Add an extra hold in this error path on rdtgroup_default.kn to defend against other races destroying the root which is then dereferenced in
kernfs_kill_sb()Fixes 5ff193fbde20 ("x86/intel_rdt: Add basic resctrl filesystem support")Co-developed-by Tony Luck <[email protected]>Signed-off-by Tony Luck <[email protected]>
Quoted 5 lines
This should be
Co-developed-by Tony Luck <[email protected]>Signed-off-by Tony Luck <[email protected]>Signed-off-by Reinette Chatre <[email protected]>
I have not yet given my Signed-off-by to these patches. I am still wrangling with sashiko's most recent concerns with the areas touched by this submission
https://sashiko.dev/#/patchset/20260515193944.15114-1-tony.luck%40intel.com
Reinette
Tony Luck
maintainerPATCH v2 5/5 fs/resctrl: Fix issues with worker threads when CPUs are taken offline
In reply to Tony LuckFrom: Reinette Chatre <[email protected]>
Sashiko noticed[1] a user-after-free in the resctrl worker thread code where the rdt_l3_mon_domain structure was freed while the worker was blocked waiting for locks.
The root issue is that cancel_delayed_work() does not block in the case where the worker thread is executing. This results in the race that Sashiko noticed, but also causes problems when the CPU that has been chosen to service the worker thread is taken offline.
Note that worker threads are allowed to delete their own work_struct (see comment in kernel/workqueue.c:process_one_work()) so there can't be any problems on the return path from the worker in this case where the work_struct was deleted by other code while the worker was executing.
Indicate failure of cancel_delayed_work() calls in resctrl_offline_cpu() by setting d->mbm_work_cpu or d->cqm_work_cpu to nr_cpu_ids. Make the worker threads check to see if they are no longer bound to the right CPU. In this case search the L3 domain list for any domain(s) with the work cpu set to nr_cpu_ids. In the case where the last CPU was removed from a domain, the domain has been removed from the list and there is nothing to do. If the domain still exists, then restart the worker on any of the remaining CPUs.
Remove redundant cancel_delayed_work() calls from resctrl_offline_mon_domain().
--- fs/resctrl/monitor.c | 55 +++++++++++++++++++++++++++++++++++++++++++ fs/resctrl/rdtgroup.c | 27 +++++++++++++++------ 2 files changed, 75 insertions(+), 7 deletions(-)
fs/resctrl/monitor.c +55 -0
@@ -791,12 +791,38 @@ static void mbm_update(struct rdt_resource *r, struct rdt_l3_mon_domain *d,*/ void cqm_handle_limbo (struct work_struct *work) { +struct rdt_resource *r =resctrl_arch_get_resource (RDT_RESOURCE_L3 );unsigned long delay =msecs_to_jiffies (CQM_LIMBOCHECK_INTERVAL );struct rdt_l3_mon_domain *d;cpus_read_lock ();mutex_lock (&rdtgroup_mutex); +/* +* Worker was blocked waiting for the CPU it was running on to go +* offline. Handle two scenarios: +* - Worker was running on the last CPU of a domain. The domain and +* thus the work_struct has been freed so do not attempt to obtain +* domain via container_of(). All remaining domains have limbo +* handlers so the loop will not find any domains needing a +* limbo handler. Just exit. +* - Worker was running on CPU that just went offline with other +* CPUs in domain still running and available to take over the +* worker. Offline handler could not schedule a new worker on +* another CPU in the domain but signaled that this needs to be +* done by setting cqm_work_cpu to nr_cpu_ids. Find the domain +* that needs a worker and schedule it after the normal CQM +* interval. +*/ +if (!is_percpu_thread ()) { +list_for_each_entry (d, &r->mon_domains, hdr.list) { +if (d->cqm_work_cpu == nr_cpu_ids) +cqm_setup_limbo_handler (d,CQM_LIMBOCHECK_INTERVAL , +RESCTRL_PICK_ANY_CPU ); + } + goto out_unlock; + } + d = container_of(work, struct rdt_l3_mon_domain, cqm_limbo.work);__check_limbo (d, false);
@@ -808,6 +834,7 @@ void cqm_handle_limbo(struct work_struct *work) delay); } +out_unlock:mutex_unlock (&rdtgroup_mutex);cpus_read_unlock (); }
@@ -852,6 +879,34 @@ void mbm_handle_overflow(struct work_struct *work) goto out_unlock; r =resctrl_arch_get_resource (RDT_RESOURCE_L3 ); + +/* +* Worker was blocked waiting for the CPU it was running on to go +* offline. Handle two scenarios: +* - Worker was running on the last CPU of a domain. The domain and +* thus the work_struct has been freed so do not attempt to obtain +* domain via container_of(). All remaining domains have overflow +* handlers so the loop will not find any domains needing an +* overflow handler. Just exit. +* - Worker was running on CPU that just went offline with other +* CPUs in domain still running and available to take over the +* worker. Offline handler could not schedule a new worker on +* another CPU in the domain but signaled that this needs to be +* done by setting mbm_work_cpu to nr_cpu_ids. Find the domain +* that needs a worker and schedule it to run after the normal +* MBM interval. This is completely safe on CPUs with wide MBM +* counters. Likely OK for old CPUs with narrow counters as the +* MBM_OVERFLOW_INTERVAL was picked conservatively. +*/ +if (!is_percpu_thread ()) { +list_for_each_entry (d, &r->mon_domains, hdr.list) { +if (d->mbm_work_cpu == nr_cpu_ids) +mbm_setup_overflow_handler (d,MBM_OVERFLOW_INTERVAL , +RESCTRL_PICK_ANY_CPU ); + } + goto out_unlock; + } + d = container_of(work, struct rdt_l3_mon_domain, mbm_over.work);list_for_each_entry (prgrp, &rdt_all_groups, rdtgroup_list) {
fs/resctrl/rdtgroup.c +20 -7
@@ -4376,8 +4376,7 @@ void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_domain_hdr *h goto out_unlock; d = container_of(hdr, struct rdt_l3_mon_domain, hdr); -if (resctrl_is_mbm_enabled ()) -cancel_delayed_work (&d->mbm_over); +if (resctrl_is_mon_event_enabled (QOS_L3_OCCUP_EVENT_ID ) &&has_busy_rmid (d)) {/* * When a package is going down, forcefully
@@ -4388,7 +4387,6 @@ void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_domain_hdr *h* package never comes back. */ __check_limbo (d, true); -cancel_delayed_work (&d->cqm_limbo); }domain_destroy_l3_mon_state (d);
@@ -4569,13 +4567,28 @@ void resctrl_offline_cpu(unsigned int cpu) d =get_mon_domain_from_cpu (cpu, l3);if (d) {if (resctrl_is_mbm_enabled () && cpu == d->mbm_work_cpu) { -cancel_delayed_work (&d->mbm_over); -mbm_setup_overflow_handler (d,0 , cpu); +if (cancel_delayed_work (&d->mbm_over)) { +mbm_setup_overflow_handler (d,0 , cpu); + }else { +/* +* Unable to schedule work on new CPU if it +* is currently running since the re-schedule +* will just force new work to run on +* current CPU. Mark domain's worker as +* needing to be rescheduled to be handled +* by worker itself. +*/ + d->mbm_work_cpu = nr_cpu_ids; + } }if (resctrl_is_mon_event_enabled (QOS_L3_OCCUP_EVENT_ID ) && cpu == d->cqm_work_cpu &&has_busy_rmid (d)) { -cancel_delayed_work (&d->cqm_limbo); -cqm_setup_limbo_handler (d,0 , cpu); +if (cancel_delayed_work (&d->cqm_limbo)) { +cqm_setup_limbo_handler (d,0 , cpu); + }else { +/* Same as mbm_work_cpu case above */ + d->cqm_work_cpu = nr_cpu_ids; + } } }
Signature · 1 line
2.54.0
Babu Moger
reviewerRe: [PATCH v2 5/5] fs/resctrl: Fix issues with worker threads when CPUs are taken offline
Hi Tony,
Quoted 3 lines · Tony Luck
From: Reinette Chatre <[email protected]>
Sashiko noticed[1] a user-after-free in the resctrl worker thread code
user-after-free -> use-after-free
Quoted 21 lines
where the
rdt_l3_mon_domainstructure was freed while the worker was blocked waiting for locks.The root issue is that
cancel_delayed_work()does not block in the case where the worker thread is executing. This results in the race that Sashiko noticed, but also causes problems when the CPU that has been chosen to service the worker thread is taken offline.Note that worker threads are allowed to delete their own
work_struct(see comment in kernel/workqueue.c:process_one_work()) so there can't be any problems on the return path from the worker in this case where thework_structwas deleted by other code while the worker was executing.Indicate failure of
cancel_delayed_work()calls inresctrl_offline_cpu()by settingd->mbm_work_cpuord->cqm_work_cputonr_cpu_ids. Make the worker threads check to see if they are no longer bound to the right CPU. In this case search the L3 domain list for any domain(s) with the work cpu set tonr_cpu_ids. In the case where the last CPU was removed from a domain, the domain has been removed from the list and there is nothing to do. If the domain still exists, then restart the worker on any of the remaining CPUs.
Quoted 5 lines
Remove redundant
cancel_delayed_work()calls fromresctrl_offline_mon_domain().Fixes 24247aeeabe9 ("x86/intel_rdt/cqm: Improve limbo list processing")Co-developed-by Tony Luck <[email protected]>Signed-off-by Tony Luck <[email protected]>
This should be
Thanks
Babu
Quoted 147 lines
--- fs/resctrl/monitor.c | 55 +++++++++++++++++++++++++++++++++++++++++++ fs/resctrl/rdtgroup.c | 27 +++++++++++++++------ 2 files changed, 75 insertions(+), 7 deletions(-)fs/resctrl/monitor.c +55
-0@@ -791,12 +791,38 @@ static void mbm_update(struct rdt_resource *r, struct rdt_l3_mon_domain *d,*/ void cqm_handle_limbo (struct work_struct *work) { +struct rdt_resource *r =resctrl_arch_get_resource (RDT_RESOURCE_L3 );unsigned long delay =msecs_to_jiffies (CQM_LIMBOCHECK_INTERVAL );struct rdt_l3_mon_domain *d;cpus_read_lock ();mutex_lock (&rdtgroup_mutex); +/* +* Worker was blocked waiting for the CPU it was running on to go +* offline. Handle two scenarios: +* - Worker was running on the last CPU of a domain. The domain and +* thus the work_struct has been freed so do not attempt to obtain +* domain via container_of(). All remaining domains have limbo +* handlers so the loop will not find any domains needing a +* limbo handler. Just exit. +* - Worker was running on CPU that just went offline with other +* CPUs in domain still running and available to take over the +* worker. Offline handler could not schedule a new worker on +* another CPU in the domain but signaled that this needs to be +* done by setting cqm_work_cpu to nr_cpu_ids. Find the domain +* that needs a worker and schedule it after the normal CQM +* interval. +*/ +if (!is_percpu_thread ()) { +list_for_each_entry (d, &r->mon_domains, hdr.list) { +if (d->cqm_work_cpu == nr_cpu_ids) +cqm_setup_limbo_handler (d,CQM_LIMBOCHECK_INTERVAL , +RESCTRL_PICK_ANY_CPU ); + } + goto out_unlock; + } + d = container_of(work, struct rdt_l3_mon_domain, cqm_limbo.work);__check_limbo (d, false);@@ -808,6 +834,7 @@ void cqm_handle_limbo(struct work_struct *work) delay); } +out_unlock:mutex_unlock (&rdtgroup_mutex);cpus_read_unlock (); }@@ -852,6 +879,34 @@ void mbm_handle_overflow(struct work_struct *work) goto out_unlock; r =resctrl_arch_get_resource (RDT_RESOURCE_L3 ); + +/* +* Worker was blocked waiting for the CPU it was running on to go +* offline. Handle two scenarios: +* - Worker was running on the last CPU of a domain. The domain and +* thus the work_struct has been freed so do not attempt to obtain +* domain via container_of(). All remaining domains have overflow +* handlers so the loop will not find any domains needing an +* overflow handler. Just exit. +* - Worker was running on CPU that just went offline with other +* CPUs in domain still running and available to take over the +* worker. Offline handler could not schedule a new worker on +* another CPU in the domain but signaled that this needs to be +* done by setting mbm_work_cpu to nr_cpu_ids. Find the domain +* that needs a worker and schedule it to run after the normal +* MBM interval. This is completely safe on CPUs with wide MBM +* counters. Likely OK for old CPUs with narrow counters as the +* MBM_OVERFLOW_INTERVAL was picked conservatively. +*/ +if (!is_percpu_thread ()) { +list_for_each_entry (d, &r->mon_domains, hdr.list) { +if (d->mbm_work_cpu == nr_cpu_ids) +mbm_setup_overflow_handler (d,MBM_OVERFLOW_INTERVAL , +RESCTRL_PICK_ANY_CPU ); + } + goto out_unlock; + } + d = container_of(work, struct rdt_l3_mon_domain, mbm_over.work);list_for_each_entry (prgrp, &rdt_all_groups, rdtgroup_list) {fs/resctrl/rdtgroup.c +20
-7@@ -4376,8 +4376,7 @@ void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_domain_hdr *h goto out_unlock; d = container_of(hdr, struct rdt_l3_mon_domain, hdr); -if (resctrl_is_mbm_enabled ()) -cancel_delayed_work (&d->mbm_over); +if (resctrl_is_mon_event_enabled (QOS_L3_OCCUP_EVENT_ID ) &&has_busy_rmid (d)) {/* * When a package is going down, forcefully @@ -4388,7 +4387,6 @@ void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_domain_hdr *h* package never comes back. */ __check_limbo (d, true); -cancel_delayed_work (&d->cqm_limbo); }domain_destroy_l3_mon_state (d);@@ -4569,13 +4567,28 @@ void resctrl_offline_cpu(unsigned int cpu) d =get_mon_domain_from_cpu (cpu, l3);if (d) {if (resctrl_is_mbm_enabled () && cpu == d->mbm_work_cpu) { -cancel_delayed_work (&d->mbm_over); -mbm_setup_overflow_handler (d,0 , cpu); +if (cancel_delayed_work (&d->mbm_over)) { +mbm_setup_overflow_handler (d,0 , cpu); + }else { +/* +* Unable to schedule work on new CPU if it +* is currently running since the re-schedule +* will just force new work to run on +* current CPU. Mark domain's worker as +* needing to be rescheduled to be handled +* by worker itself. +*/ + d->mbm_work_cpu = nr_cpu_ids; + } }if (resctrl_is_mon_event_enabled (QOS_L3_OCCUP_EVENT_ID ) && cpu == d->cqm_work_cpu &&has_busy_rmid (d)) { -cancel_delayed_work (&d->cqm_limbo); -cqm_setup_limbo_handler (d,0 , cpu); +if (cancel_delayed_work (&d->cqm_limbo)) { +cqm_setup_limbo_handler (d,0 , cpu); + }else { +/* Same as mbm_work_cpu case above */ + d->cqm_work_cpu = nr_cpu_ids; + } } }