sched/isolation: Enable cpuset_mutex/cpus_read_lock synchronization before freeing old housekeeping cpumask
Subsystems (3)
Waiman Long
maintainerPATCH v2 1/1 sched/isolation: Enable cpuset_mutex/cpus_read_lock synchronization before freeing old housekeeping cpumask
Commit a84097e625f2 ("cgroup/cpuset: Call housekeeping_update() without holding cpus_read_lock") adds a new cpuset_top_mutex on top but drops cpus_read_lock and cpuset_mutex before calling housekeeping_update() which calls other functions that may acquire cpus_read_lock leading to a possibility of deadlock if cpus_read_lock isn't dropped before calling housekeeping_update().
This commit also changes lockdep_is_cpuset_held() to return true if either cpuset_mutex or cpuset_top_mutex is held. lockdep_is_cpuset_held() is used by housekeeping_dereference_check() as one of the conditions that rcu_read_lock() is not needed to access HK_TYPE_DOMAIN cpumask which can be changed by housekeeping_update() at run time. The housekeeping_dereference_check() function also uses lockdep_is_cpus_write_held() as another condition that can skip the use of rcu_read_lock().
In reality, both cpuset_mutex and cpus_write_lock are now no longer valid conditions to skip rcu_read_lock(). It is certainly possible that there can be a parallel housekeeping_update() call going on if only cpuset_mutex or cpus_write_lock is held. This can lead to a potential use-after-free situation when HK_TYPE_DOMAIN housekeeping cpumask is being accessed.
One possible solution is to hold rcu_read_lock for functions that may be called with only cpuset_mutex or cpus_write_lock is held. Quite a large number of functions will need to be modified if rcu_read_lock() has to be added. A far simpler solution is to synchronize with other cpuset_mutex and cpus_write_lock critical sections before freeing the old housekeeping cpumask. This will ensure that code within those critical sections will not have a UaF situation when accessing housekeeping cpumask. This commit implements the latter solution and make the code in lockdep_is_cpuset_held() and housekeeping_dereference_check() correct.
--- kernel/sched/isolation.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
kernel/sched/isolation.c +10 -0
@@ -159,6 +159,16 @@ int housekeeping_update(struct cpumask *isol_mask) err =kthreads_update_housekeeping ();WARN_ON_ONCE (err <0 ); +/* +* Synchronize with cpuset_mutex and cpus_read_lock to make sure that +* cpuset_mutex and cpus_write_lock critical sections afterward will +* not have access to the old housekeeping cpumask. +*/ +cpus_read_lock (); +cpuset_lock (); +cpuset_unlock (); +cpus_read_unlock (); +kfree (old);return 0 ;
Signature · 1 line
2.56.0
Quoted 50 lines · Waiman Long
Commit
a84097e625f2("cgroup/cpuset: Callhousekeeping_update()without holdingcpus_read_lock") adds a newcpuset_top_mutexon top but dropscpus_read_lockandcpuset_mutexbefore callinghousekeeping_update()which calls other functions that may acquirecpus_read_lockleading to a possibility of deadlock ifcpus_read_lockisn't dropped before callinghousekeeping_update().This commit also changes
lockdep_is_cpuset_held()to return true if eithercpuset_mutexorcpuset_top_mutexis held.lockdep_is_cpuset_held()is used byhousekeeping_dereference_check()as one of the conditions thatrcu_read_lock()is not needed to accessHK_TYPE_DOMAINcpumask which can be changed byhousekeeping_update()at run time. Thehousekeeping_dereference_check()function also useslockdep_is_cpus_write_held()as another condition that can skip the use ofrcu_read_lock().In reality, both
cpuset_mutexandcpus_write_lockare now no longer valid conditions to skiprcu_read_lock(). It is certainly possible that there can be a parallelhousekeeping_update()call going on if onlycpuset_mutexorcpus_write_lockis held. This can lead to a potential use-after-free situation whenHK_TYPE_DOMAINhousekeeping cpumask is being accessed.One possible solution is to hold
rcu_read_lockfor functions that may be called with onlycpuset_mutexorcpus_write_lockis held. Quite a large number of functions will need to be modified ifrcu_read_lock()has to be added. A far simpler solution is to synchronize with othercpuset_mutexandcpus_write_lockcritical sections before freeing the old housekeeping cpumask. This will ensure that code within those critical sections will not have a UaF situation when accessing housekeeping cpumask. This commit implements the latter solution and make the code inlockdep_is_cpuset_held()andhousekeeping_dereference_check()correct.Fixes a84097e625f2 ("cgroup/cpuset: Call housekeeping_update() without holding cpus_read_lock")Signed-off-by Waiman Long <[email protected]>--- kernel/sched/isolation.c | 10 ++++++++++ 1 file changed, 10 insertions(+)kernel/sched/isolation.c +3
-0@@ -159,6 +159,16 @@ int housekeeping_update(struct cpumask *isol_mask) err =kthreads_update_housekeeping ();WARN_ON_ONCE (err <0 ); +/* +* Synchronize with cpuset_mutex and cpus_read_lock to make sure that +* cpuset_mutex and cpus_write_lock critical sections afterward will
kernel/sched/isolation.c int housekeeping_update(struct cpumask *isol_mask) · line 174
Quoted 10 lines
kernel/sched/isolation.c +7
-0+* not have access to the old housekeeping cpumask. +*/ +cpus_read_lock (); +cpuset_lock (); +cpuset_unlock (); +cpus_read_unlock (); +kfree (old);return 0 ;
Hi Waiman,
This looks good to me.
Nit: Could the comments in housekeeping_dereference_check() be updated as well? They seem to be outdated after the locking changes.
Thanks,
Guopeng
Ridong Chen
reviewerQuoted 50 lines · Waiman Long
Commit
a84097e625f2("cgroup/cpuset: Callhousekeeping_update()without holdingcpus_read_lock") adds a newcpuset_top_mutexon top but dropscpus_read_lockandcpuset_mutexbefore callinghousekeeping_update()which calls other functions that may acquirecpus_read_lockleading to a possibility of deadlock ifcpus_read_lockisn't dropped before callinghousekeeping_update().This commit also changes
lockdep_is_cpuset_held()to return true if eithercpuset_mutexorcpuset_top_mutexis held.lockdep_is_cpuset_held()is used byhousekeeping_dereference_check()as one of the conditions thatrcu_read_lock()is not needed to accessHK_TYPE_DOMAINcpumask which can be changed byhousekeeping_update()at run time. Thehousekeeping_dereference_check()function also useslockdep_is_cpus_write_held()as another condition that can skip the use ofrcu_read_lock().In reality, both
cpuset_mutexandcpus_write_lockare now no longer valid conditions to skiprcu_read_lock(). It is certainly possible that there can be a parallelhousekeeping_update()call going on if onlycpuset_mutexorcpus_write_lockis held. This can lead to a potential use-after-free situation whenHK_TYPE_DOMAINhousekeeping cpumask is being accessed.One possible solution is to hold
rcu_read_lockfor functions that may be called with onlycpuset_mutexorcpus_write_lockis held. Quite a large number of functions will need to be modified ifrcu_read_lock()has to be added. A far simpler solution is to synchronize with othercpuset_mutexandcpus_write_lockcritical sections before freeing the old housekeeping cpumask. This will ensure that code within those critical sections will not have a UaF situation when accessing housekeeping cpumask. This commit implements the latter solution and make the code inlockdep_is_cpuset_held()andhousekeeping_dereference_check()correct.Fixes a84097e625f2 ("cgroup/cpuset: Call housekeeping_update() without holding cpus_read_lock")Signed-off-by Waiman Long <[email protected]>--- kernel/sched/isolation.c | 10 ++++++++++ 1 file changed, 10 insertions(+)kernel/sched/isolation.c +3
-0@@ -159,6 +159,16 @@ int housekeeping_update(struct cpumask *isol_mask) err =kthreads_update_housekeeping ();WARN_ON_ONCE (err <0 ); +/* +* Synchronize with cpuset_mutex and cpus_read_lock to make sure that +* cpuset_mutex and cpus_write_lock critical sections afterward will
kernel/sched/isolation.c int housekeeping_update(struct cpumask *isol_mask) · line 174
Quoted 10 lines
kernel/sched/isolation.c +7
-0+* not have access to the old housekeeping cpumask. +*/ +cpus_read_lock (); +cpuset_lock (); +cpuset_unlock (); +cpus_read_unlock (); +kfree (old);return 0 ;
Hi Waiman,
Taking the locks and releasing them right away looks a bit odd to me. It is not obvious what the empty critical section protects until one reads the commit message.
Let me check that I understand the problem correctly. housekeeping_dereference_check() lets readers holding cpus_write_lock or cpuset_mutex access HK_TYPE_DOMAIN without rcu_read_lock(). After commit a84097e625f2, the pointer is switched and the old cpumask is freed after cpuset_mutex and cpus_read_lock have been dropped, and synchronize_rcu() does not wait for those readers. For example:
CPU A CPU B
rebuild_sched_domains() cpuset_update_sd_hk_unlock()
mutex_unlock(&cpuset_mutex) cpus_read_unlock()
mutex_lock(&cpuset_mutex)
p = housekeeping_cpumask(DOMAIN)
housekeeping_update()
rcu_assign_pointer() synchronize_rcu() kfree(old)
access p <- UAF
Here CPU A reads the mask in asym_cpu_capacity_scan() or dl_get_task_effective_cpus().
If that is right, the root cause is that the pointer is now switched outside of those locks. So instead of adding a lock barrier before kfree(), how about switching the pointer before the locks are dropped? housekeeping_update() could be split into two parts:
- housekeeping_publish(): allocate, check and publish the new mask, called with cpus_read_lock and cpuset_mutex held.
- housekeeping_update(): synchronize_rcu(), the workqueue flushes and kfree(old), called after the locks are dropped as it is now.
Then a reader holding cpuset_mutex either finishes before the pointer is switched, or only sees the new mask:
CPU A CPU B
cpuset_update_sd_hk_unlock()
housekeeping_publish()
rcu_assign_pointer()
mutex_lock(&cpuset_mutex) <- wait
mutex_unlock(&cpuset_mutex) cpus_read_unlock()
p = housekeeping_cpumask(DOMAIN) housekeeping_update()
<- new cpumask synchronize_rcu()
kfree(old)
The same applies to cpus_write_lock readers, as cpus_read_lock is held until the new mask is published. The workqueue flushes still run without those locks, so the deadlock fixed by a84097e625f2 does not come back.
I will send a patch in reply to this mail. Could you take a look and tell me whether this approach works for you?
Signature · 2 lines
Best regards Ridong
Ridong
reviewerPATCH 1/1 sched/isolation: Publish new HK_TYPE_DOMAIN cpumask under cpuset_mutex
From: Ridong Chen <[email protected]>
Commit a84097e625f2 ("cgroup/cpuset: Call housekeeping_update() without holding cpus_read_lock") calls housekeeping_update() after dropping cpus_read_lock and cpuset_mutex, so the HK_TYPE_DOMAIN cpumask is now switched and freed outside of those locks. But readers holding either lock may still access it without rcu_read_lock() as allowed by housekeeping_dereference_check(), and synchronize_rcu() doesn't wait for them:
CPU A CPU B
rebuild_sched_domains() cpuset_update_sd_hk_unlock()
mutex_unlock(&cpuset_mutex) cpus_read_unlock()
mutex_lock(&cpuset_mutex)
p = housekeeping_cpumask(DOMAIN)
housekeeping_update()
rcu_assign_pointer() synchronize_rcu() kfree(old)
access p <- UAF
Fix it by moving the pointer switch into housekeeping_publish(), which is called before dropping the locks. Then CPU A either finishes before CPU B switches the pointer, or only sees the new cpumask:
CPU A CPU B
cpuset_update_sd_hk_unlock()
housekeeping_publish()
rcu_assign_pointer()
mutex_lock(&cpuset_mutex) <- wait
mutex_unlock(&cpuset_mutex) cpus_read_unlock()
p = housekeeping_cpumask(DOMAIN) housekeeping_update()
<- new cpumask synchronize_rcu()
kfree(old)
The rest of housekeeping_update(), including the workqueue flushes, still runs without those locks.
--- include/linux/sched/isolation.h | 6 ++++-- kernel/cgroup/cpuset.c | 12 ++++++++---- kernel/sched/isolation.c | 23 ++++++++++++++++------- 3 files changed, 28 insertions(+), 13 deletions(-)
include/linux/sched/isolation.h +4 -2
@@ -43,7 +43,8 @@ extern const struct cpumask *housekeeping_cpumask(enum hk_type type);extern bool housekeeping_enabled (enum hk_type type);extern void housekeeping_affine (struct task_struct *t,enum hk_type type);extern bool housekeeping_test_cpu (int cpu,enum hk_type type); -extern int housekeeping_update (struct cpumask *isol_mask); +extern struct cpumask *housekeeping_publish (struct cpumask *isol_mask); +extern void housekeeping_update (struct cpumask *isol_mask,struct cpumask *old);extern void __init housekeeping_init (void );#else
@@ -71,7 +72,8 @@ static inline bool housekeeping_test_cpu(int cpu, enum hk_type type) return true; } -static inline int housekeeping_update (struct cpumask *isol_mask) {return 0 ; } +static inline struct cpumask *housekeeping_publish (struct cpumask *isol_mask) {return NULL ; } +static inline void housekeeping_update (struct cpumask *isol_mask,struct cpumask *old) { }static inline void housekeeping_init (void ) { }#endif /* CONFIG_CPU_ISOLATION */
kernel/cgroup/cpuset.c +8 -4
@@ -1472,17 +1472,21 @@ static void cpuset_update_sd_hk_unlock(void)rebuild_sched_domains_locked ();if (update_housekeeping) { +struct cpumask *old; + update_housekeeping = false;cpumask_copy (isolated_hk_cpus, isolated_cpus);/* -* housekeeping_update() is now called without holding -* cpus_read_lock and cpuset_mutex. Only cpuset_top_mutex -* is still being held for mutual exclusion. +* Publish the new cpumask with cpus_read_lock and cpuset_mutex +* held, but call housekeeping_update() without them. Only +* cpuset_top_mutex is still being held for mutual exclusion. */ + old =housekeeping_publish (isolated_hk_cpus);mutex_unlock (&cpuset_mutex);cpus_read_unlock (); -WARN_ON_ONCE (housekeeping_update (isolated_hk_cpus)); +if (!WARN_ON_ONCE (IS_ERR (old))) +housekeeping_update (isolated_hk_cpus, old);mutex_unlock (&cpuset_top_mutex); }else {cpuset_full_unlock ();
kernel/sched/isolation.c +16 -7
@@ -120,23 +120,26 @@ bool housekeeping_test_cpu(int cpu, enum hk_type type) }EXPORT_SYMBOL_GPL (housekeeping_test_cpu); -int housekeeping_update (struct cpumask *isol_mask) +/* Called with cpus_read_lock and cpuset_mutex held */ +struct cpumask *housekeeping_publish (struct cpumask *isol_mask) {struct cpumask *trial, *old =NULL ; -int err; + +lockdep_assert_cpus_held (); +lockdep_assert_cpuset_lock_held (); trial =kmalloc (cpumask_size (),GFP_KERNEL );if (!trial) -return -ENOMEM ; +return ERR_PTR (-ENOMEM );cpumask_andnot (trial,housekeeping_cpumask (HK_TYPE_DOMAIN_BOOT ), isol_mask);if (!cpumask_intersects (trial, cpu_online_mask)) {kfree (trial); -return -EINVAL ; +return ERR_PTR (-EINVAL ); }if (!housekeeping.flags) -static_branch_enable (&housekeeping_overridden); +static_branch_enable_cpuslocked (&housekeeping_overridden);if (housekeeping.flags &HK_FLAG_DOMAIN ) old =housekeeping_cpumask_dereference (HK_TYPE_DOMAIN );
@@ -144,6 +147,14 @@ int housekeeping_update(struct cpumask *isol_mask)WRITE_ONCE (housekeeping.flags, housekeeping.flags |HK_FLAG_DOMAIN );rcu_assign_pointer (housekeeping.cpumasks[HK_TYPE_DOMAIN ], trial); +return old; +} + +/* Called without cpus_read_lock and cpuset_mutex held */ +void housekeeping_update (struct cpumask *isol_mask,struct cpumask *old) +{ +int err; +synchronize_rcu ();pci_probe_flush_workqueue ();
@@ -160,8 +171,6 @@ int housekeeping_update(struct cpumask *isol_mask)WARN_ON_ONCE (err <0 );kfree (old); - -return 0 ; }void __init housekeeping_init (void )
Signature · 1 line
2.34.1
Re: [PATCH] sched/isolation: Publish new HK_TYPE_DOMAIN cpumask under cpuset_mutex
Quoted 154 lines · Ridong
From: Ridong Chen <[email protected]>
Commit
a84097e625f2("cgroup/cpuset: Callhousekeeping_update()without holdingcpus_read_lock") callshousekeeping_update()after droppingcpus_read_lockandcpuset_mutex, so theHK_TYPE_DOMAINcpumask is now switched and freed outside of those locks. But readers holding either lock may still access it withoutrcu_read_lock()as allowed byhousekeeping_dereference_check(), andsynchronize_rcu()doesn't wait for them:CPU A CPU B
rebuild_sched_domains()cpuset_update_sd_hk_unlock()
mutex_unlock(&cpuset_mutex)cpus_read_unlock()
mutex_lock(&cpuset_mutex)
p =housekeeping_cpumask(DOMAIN)
housekeeping_update()
rcu_assign_pointer()synchronize_rcu()kfree(old)
access p <- UAFFix it by moving the pointer switch into
housekeeping_publish(), which is called before dropping the locks. Then CPU A either finishes before CPU B switches the pointer, or only sees the new cpumask:CPU A CPU B
cpuset_update_sd_hk_unlock()
housekeeping_publish()
rcu_assign_pointer()
mutex_lock(&cpuset_mutex)<- wait
mutex_unlock(&cpuset_mutex)cpus_read_unlock()
p =housekeeping_cpumask(DOMAIN)housekeeping_update()
<- new cpumasksynchronize_rcu()
kfree(old)The rest of
housekeeping_update(), including the workqueue flushes, still runs without those locks.Fixes a84097e625f2 ("cgroup/cpuset: Call housekeeping_update() without holding cpus_read_lock")Signed-off-by Ridong Chen <[email protected]>--- include/linux/sched/isolation.h | 6 ++++-- kernel/cgroup/cpuset.c | 12 ++++++++---- kernel/sched/isolation.c | 23 ++++++++++++++++------- 3 files changed, 28 insertions(+), 13 deletions(-)include/linux/sched/isolation.h +4
-2@@ -43,7 +43,8 @@ extern const struct cpumask *housekeeping_cpumask(enum hk_type type);extern bool housekeeping_enabled (enum hk_type type);extern void housekeeping_affine (struct task_struct *t,enum hk_type type);extern bool housekeeping_test_cpu (int cpu,enum hk_type type); -extern int housekeeping_update (struct cpumask *isol_mask); +extern struct cpumask *housekeeping_publish (struct cpumask *isol_mask); +extern void housekeeping_update (struct cpumask *isol_mask,struct cpumask *old);extern void __init housekeeping_init (void );#else @@ -71,7 +72,8 @@ static inline bool housekeeping_test_cpu(int cpu, enum hk_type type) return true; } -static inline int housekeeping_update (struct cpumask *isol_mask) {return 0 ; } +static inline struct cpumask *housekeeping_publish (struct cpumask *isol_mask) {return NULL ; } +static inline void housekeeping_update (struct cpumask *isol_mask,struct cpumask *old) { }static inline void housekeeping_init (void ) { }#endif /* CONFIG_CPU_ISOLATION */ kernel/cgroup/cpuset.c +8
-4@@ -1472,17 +1472,21 @@ static void cpuset_update_sd_hk_unlock(void)rebuild_sched_domains_locked ();if (update_housekeeping) { +struct cpumask *old; + update_housekeeping = false;cpumask_copy (isolated_hk_cpus, isolated_cpus);/* -* housekeeping_update() is now called without holding -* cpus_read_lock and cpuset_mutex. Only cpuset_top_mutex -* is still being held for mutual exclusion. +* Publish the new cpumask with cpus_read_lock and cpuset_mutex +* held, but call housekeeping_update() without them. Only +* cpuset_top_mutex is still being held for mutual exclusion. */ + old =housekeeping_publish (isolated_hk_cpus);mutex_unlock (&cpuset_mutex);cpus_read_unlock (); -WARN_ON_ONCE (housekeeping_update (isolated_hk_cpus)); +if (!WARN_ON_ONCE (IS_ERR (old))) +housekeeping_update (isolated_hk_cpus, old);mutex_unlock (&cpuset_top_mutex); }else {cpuset_full_unlock ();kernel/sched/isolation.c +16
-5@@ -120,23 +120,26 @@ bool housekeeping_test_cpu(int cpu, enum hk_type type) }EXPORT_SYMBOL_GPL (housekeeping_test_cpu); -int housekeeping_update (struct cpumask *isol_mask) +/* Called with cpus_read_lock and cpuset_mutex held */ +struct cpumask *housekeeping_publish (struct cpumask *isol_mask) {struct cpumask *trial, *old =NULL ; -int err; + +lockdep_assert_cpus_held (); +lockdep_assert_cpuset_lock_held (); trial =kmalloc (cpumask_size (),GFP_KERNEL );if (!trial) -return -ENOMEM ; +return ERR_PTR (-ENOMEM );cpumask_andnot (trial,housekeeping_cpumask (HK_TYPE_DOMAIN_BOOT ), isol_mask);if (!cpumask_intersects (trial, cpu_online_mask)) {kfree (trial); -return -EINVAL ; +return ERR_PTR (-EINVAL ); }if (!housekeeping.flags) -static_branch_enable (&housekeeping_overridden); +static_branch_enable_cpuslocked (&housekeeping_overridden);if (housekeeping.flags &HK_FLAG_DOMAIN ) old =housekeeping_cpumask_dereference (HK_TYPE_DOMAIN );@@ -144,6 +147,14 @@ int housekeeping_update(struct cpumask *isol_mask)WRITE_ONCE (housekeeping.flags, housekeeping.flags |HK_FLAG_DOMAIN );rcu_assign_pointer (housekeeping.cpumasks[HK_TYPE_DOMAIN ], trial); +return old; +} + +/* Called without cpus_read_lock and cpuset_mutex held */ +void housekeeping_update (struct cpumask *isol_mask,struct cpumask *old) +{ +int err; +synchronize_rcu ();pci_probe_flush_workqueue ();
kernel/sched/isolation.c int housekeeping_update(struct cpumask *isol_mask) · line 176
Quoted 8 lines
kernel/sched/isolation.c +0
-2@@ -160,8 +171,6 @@ int housekeeping_update(struct cpumask *isol_mask)WARN_ON_ONCE (err <0 );kfree (old); - -return 0 ; }void __init housekeeping_init (void )
Yes, that should work too.
I will let other decide which one do they want.
Cheers,
Longman
Ridong Chen
reviewerRe: [PATCH] sched/isolation: Publish new HK_TYPE_DOMAIN cpumask under cpuset_mutex
Quoted 156 lines · Waiman Long
Quoted 146 lines · Ridong
From: Ridong Chen <[email protected]>
Commit
a84097e625f2("cgroup/cpuset: Callhousekeeping_update()without holdingcpus_read_lock") callshousekeeping_update()after droppingcpus_read_lockandcpuset_mutex, so theHK_TYPE_DOMAINcpumask is now switched and freed outside of those locks. But readers holding either lock may still access it withoutrcu_read_lock()as allowed byhousekeeping_dereference_check(), andsynchronize_rcu()doesn't wait for them:CPU A CPU B rebuild_sched_domains()
cpuset_update_sd_hk_unlock()mutex_unlock(&cpuset_mutex)cpus_read_unlock()mutex_lock(&cpuset_mutex)
p =housekeeping_cpumask(DOMAIN)
housekeeping_update()rcu_assign_pointer()synchronize_rcu()kfree(old) access p <- UAFFix it by moving the pointer switch into
housekeeping_publish(), which is called before dropping the locks. Then CPU A either finishes before CPU B switches the pointer, or only sees the new cpumask:CPU A CPU B
cpuset_update_sd_hk_unlock()housekeeping_publish()rcu_assign_pointer()mutex_lock(&cpuset_mutex)<- waitmutex_unlock(&cpuset_mutex)cpus_read_unlock()p = housekeeping_cpumask(DOMAIN)housekeeping_update()<- new cpumasksynchronize_rcu()kfree(old)The rest of
housekeeping_update(), including the workqueue flushes, still runs without those locks.Fixes a84097e625f2 ("cgroup/cpuset: Call housekeeping_update() without holding cpus_read_lock")Signed-off-by Ridong Chen <[email protected]>---
include/linux/sched/isolation.h| 6 ++++-- kernel/cgroup/cpuset.c | 12 ++++++++---- kernel/sched/isolation.c | 23 ++++++++++++++++-------3 files changed, 28 insertions(+), 13 deletions(-)include/linux/sched/isolation.h +4
-2@@ -43,7 +43,8 @@ extern const struct cpumask *housekeeping_cpumask(enum hk_type type); extern bool housekeeping_enabled(enum hk_type type); extern void housekeeping_affine(struct task_struct *t, enum hk_type type); extern bool housekeeping_test_cpu(int cpu, enum hk_type type); -extern int housekeeping_update (struct cpumask *isol_mask); +extern struct cpumask *housekeeping_publish (struct cpumask *isol_mask); +extern void housekeeping_update (struct cpumask *isol_mask,struct cpumask *old); extern void __init housekeeping_init(void); #else@@ -71,7 +72,8 @@ static inline bool housekeeping_test_cpu(int cpu, enum hk_type type) return true; } -static inline int housekeeping_update (struct cpumask *isol_mask) {return 0 ; } +static inline struct cpumask *housekeeping_publish (struct cpumask *isol_mask) {return NULL ; } +static inline void housekeeping_update (struct cpumask *isol_mask,struct cpumask *old) { } static inline void housekeeping_init(void) { }#endif /*
CONFIG_CPU_ISOLATION*/kernel/cgroup/cpuset.c +8
-4@@ -1472,17 +1472,21 @@ static void cpuset_update_sd_hk_unlock(void) rebuild_sched_domains_locked(); if (update_housekeeping) { + struct cpumask *old; + update_housekeeping = false; cpumask_copy(isolated_hk_cpus, isolated_cpus); /* - * housekeeping_update() is now called without holding - * cpus_read_lock and cpuset_mutex. Only cpuset_top_mutex - * is still being held for mutual exclusion. + * Publish the new cpumask with cpus_read_lock and cpuset_mutex + * held, but call housekeeping_update() without them. Only + * cpuset_top_mutex is still being held for mutual exclusion. */ + old = housekeeping_publish(isolated_hk_cpus); mutex_unlock(&cpuset_mutex); cpus_read_unlock(); - WARN_ON_ONCE(housekeeping_update(isolated_hk_cpus)); + if (!WARN_ON_ONCE(IS_ERR(old))) + housekeeping_update(isolated_hk_cpus, old); mutex_unlock(&cpuset_top_mutex); } else { cpuset_full_unlock();kernel/sched/isolation.c +16
-5@@ -120,23 +120,26 @@ bool housekeeping_test_cpu(int cpu, enum hk_type type) } EXPORT_SYMBOL_GPL(housekeeping_test_cpu); -int housekeeping_update (struct cpumask *isol_mask) +/* Called with cpus_read_lock and cpuset_mutex held */ +struct cpumask *housekeeping_publish (struct cpumask *isol_mask) { struct cpumask *trial, *old = NULL; - int err; + + lockdep_assert_cpus_held(); + lockdep_assert_cpuset_lock_held(); trial = kmalloc(cpumask_size(), GFP_KERNEL); if (!trial) - return -ENOMEM; + return ERR_PTR(-ENOMEM); cpumask_andnot(trial, housekeeping_cpumask(HK_TYPE_DOMAIN_BOOT), isol_mask); if (!cpumask_intersects(trial, cpu_online_mask)) { kfree(trial); - return -EINVAL; + return ERR_PTR(-EINVAL); } if (!housekeeping.flags) - static_branch_enable(&housekeeping_overridden); + static_branch_enable_cpuslocked(&housekeeping_overridden); if (housekeeping.flags & HK_FLAG_DOMAIN) old = housekeeping_cpumask_dereference(HK_TYPE_DOMAIN);@@ -144,6 +147,14 @@ int housekeeping_update(struct cpumask *isol_mask) WRITE_ONCE(housekeeping.flags, housekeeping.flags | HK_FLAG_DOMAIN); rcu_assign_pointer(housekeeping.cpumasks[HK_TYPE_DOMAIN], trial); + return old; +} + +/* Called without cpus_read_lock and cpuset_mutex held */ +void housekeeping_update (struct cpumask *isol_mask,struct cpumask *old) +{ + int err; + synchronize_rcu(); pci_probe_flush_workqueue();kernel/sched/isolation.c int housekeeping_update(struct cpumask *isol_mask) · line 174
Quoted 6 lines
kernel/sched/isolation.c +0
-2@@ -160,8 +171,6 @@ int housekeeping_update(struct cpumask *isol_mask) WARN_ON_ONCE(err < 0); kfree(old); - - return 0; } void __init housekeeping_init(void)Yes, that should work too.
Quoted 4 lines
Acked-by Waiman Long <[email protected]>I will let other decide which one do they want.
Thanks.
Signature · 2 lines
Best regards Ridong
Re: [PATCH] sched/isolation: Publish new HK_TYPE_DOMAIN cpumask under cpuset_mutex
In reply to RidongQuoted 35 lines · Ridong
Commit
a84097e625f2("cgroup/cpuset: Callhousekeeping_update()without holdingcpus_read_lock") callshousekeeping_update()after droppingcpus_read_lockandcpuset_mutex, so theHK_TYPE_DOMAINcpumask is now switched and freed outside of those locks. But readers holding either lock may still access it withoutrcu_read_lock()as allowed byhousekeeping_dereference_check(), andsynchronize_rcu()doesn't wait for them:CPU A CPU B
rebuild_sched_domains()cpuset_update_sd_hk_unlock()
mutex_unlock(&cpuset_mutex)cpus_read_unlock()
mutex_lock(&cpuset_mutex)
p =housekeeping_cpumask(DOMAIN)
housekeeping_update()
rcu_assign_pointer()synchronize_rcu()kfree(old)
access p <- UAFFix it by moving the pointer switch into
housekeeping_publish(), which is called before dropping the locks. Then CPU A either finishes before CPU B switches the pointer, or only sees the new cpumask:CPU A CPU B
cpuset_update_sd_hk_unlock()
housekeeping_publish()
rcu_assign_pointer()
mutex_lock(&cpuset_mutex)<- wait
mutex_unlock(&cpuset_mutex)cpus_read_unlock()
p =housekeeping_cpumask(DOMAIN)housekeeping_update()
<- new cpumasksynchronize_rcu()
kfree(old)
Quoted 5 lines
The rest of
housekeeping_update(), including the workqueue flushes, still runs without those locks.Fixes a84097e625f2 ("cgroup/cpuset: Call housekeeping_update() without holding cpus_read_lock")Signed-off-by Ridong Chen <[email protected]>
Thanks.
--
tejun
Re: [PATCH] sched/isolation: Publish new HK_TYPE_DOMAIN cpumask under cpuset_mutex
In reply to RidongQuoted 37 lines · Ridong
From: Ridong Chen <[email protected]>
Commit
a84097e625f2("cgroup/cpuset: Callhousekeeping_update()without holdingcpus_read_lock") callshousekeeping_update()after droppingcpus_read_lockandcpuset_mutex, so theHK_TYPE_DOMAINcpumask is now switched and freed outside of those locks. But readers holding either lock may still access it withoutrcu_read_lock()as allowed byhousekeeping_dereference_check(), andsynchronize_rcu()doesn't wait for them:CPU A CPU B
rebuild_sched_domains()cpuset_update_sd_hk_unlock()
mutex_unlock(&cpuset_mutex)cpus_read_unlock()
mutex_lock(&cpuset_mutex)
p =housekeeping_cpumask(DOMAIN)
housekeeping_update()
rcu_assign_pointer()synchronize_rcu()kfree(old)
access p <- UAFFix it by moving the pointer switch into
housekeeping_publish(), which is called before dropping the locks. Then CPU A either finishes before CPU B switches the pointer, or only sees the new cpumask:CPU A CPU B
cpuset_update_sd_hk_unlock()
housekeeping_publish()
rcu_assign_pointer()
mutex_lock(&cpuset_mutex)<- wait
mutex_unlock(&cpuset_mutex)cpus_read_unlock()
p =housekeeping_cpumask(DOMAIN)housekeeping_update()
<- new cpumasksynchronize_rcu()
kfree(old)
Quoted 5 lines
The rest of
housekeeping_update(), including the workqueue flushes, still runs without those locks.Fixes a84097e625f2 ("cgroup/cpuset: Call housekeeping_update() without holding cpus_read_lock")Signed-off-by Ridong Chen <[email protected]>
Ok that should work but, IIRC this whole locking puzzle is there because cpuset_handle_hotplug() can change isolated partitions. Why is it the case since cpuset.cpus shouldn't care about offline CPUs?
I remember Waiman said it could be fixed in the long run. I don't remember the details though.
Thanks.
Signature · 2 lines
Frederic Weisbecker SUSE Labs
Waiman Long
maintainerRe: [PATCH] sched/isolation: Publish new HK_TYPE_DOMAIN cpumask under cpuset_mutex
Quoted 41 lines · Frederic Weisbecker
Quoted 40 lines · Ridong
From: Ridong Chen <[email protected]>
Commit
a84097e625f2("cgroup/cpuset: Callhousekeeping_update()without holdingcpus_read_lock") callshousekeeping_update()after droppingcpus_read_lockandcpuset_mutex, so theHK_TYPE_DOMAINcpumask is now switched and freed outside of those locks. But readers holding either lock may still access it withoutrcu_read_lock()as allowed byhousekeeping_dereference_check(), andsynchronize_rcu()doesn't wait for them:CPU A CPU B
rebuild_sched_domains()cpuset_update_sd_hk_unlock()
mutex_unlock(&cpuset_mutex)cpus_read_unlock()
mutex_lock(&cpuset_mutex)
p =housekeeping_cpumask(DOMAIN)
housekeeping_update()
rcu_assign_pointer()synchronize_rcu()kfree(old)
access p <- UAFFix it by moving the pointer switch into
housekeeping_publish(), which is called before dropping the locks. Then CPU A either finishes before CPU B switches the pointer, or only sees the new cpumask:CPU A CPU B
cpuset_update_sd_hk_unlock()
housekeeping_publish()
rcu_assign_pointer()
mutex_lock(&cpuset_mutex)<- wait
mutex_unlock(&cpuset_mutex)cpus_read_unlock()
p =housekeeping_cpumask(DOMAIN)housekeeping_update()
<- new cpumasksynchronize_rcu()
kfree(old)The rest of
housekeeping_update(), including the workqueue flushes, still runs without those locks.
Quoted 8 lines
Quoted 2 lines
Fixes a84097e625f2 ("cgroup/cpuset: Call housekeeping_update() without holding cpus_read_lock")Signed-off-by Ridong Chen <[email protected]>Ok that should work but, IIRC this whole locking puzzle is there because
cpuset_handle_hotplug()can change isolated partitions. Why is it the case since cpuset.cpus shouldn't care about offline CPUs?I remember Waiman said it could be fixed in the long run. I don't remember the details though.
cpuset_handle_hotplug() has to remove the newly offlined CPU or to add back the newly online CPUs to the effective_cpus of all the cpusets that include the affected CPUs whether it is in an isolated or root partition or not. In the worst case that the last online CPU in an isolated partition is offline, we have to disable it. That is the main reason why hotplug can affect isolated partitions. I don't remember saying it is something that can be fixed long term. Perhaps, it is not something related but not exactly this.
Cheers,
Longman
Waiman Long
maintainerQuoted 89 lines · Ridong Chen
Quoted 52 lines · Waiman Long
Commit
a84097e625f2("cgroup/cpuset: Callhousekeeping_update()without holdingcpus_read_lock") adds a newcpuset_top_mutexon top but dropscpus_read_lockandcpuset_mutexbefore callinghousekeeping_update()which calls other functions that may acquirecpus_read_lockleading to a possibility of deadlock ifcpus_read_lockisn't dropped before callinghousekeeping_update().This commit also changes
lockdep_is_cpuset_held()to return true if eithercpuset_mutexorcpuset_top_mutexis held.lockdep_is_cpuset_held()is used byhousekeeping_dereference_check()as one of the conditions thatrcu_read_lock()is not needed to accessHK_TYPE_DOMAINcpumask which can be changed byhousekeeping_update()at run time. Thehousekeeping_dereference_check()function also useslockdep_is_cpus_write_held()as another condition that can skip the use ofrcu_read_lock().In reality, both
cpuset_mutexandcpus_write_lockare now no longer valid conditions to skiprcu_read_lock(). It is certainly possible that there can be a parallelhousekeeping_update()call going on if onlycpuset_mutexorcpus_write_lockis held. This can lead to a potential use-after-free situation whenHK_TYPE_DOMAINhousekeeping cpumask is being accessed.One possible solution is to hold
rcu_read_lockfor functions that may be called with onlycpuset_mutexorcpus_write_lockis held. Quite a large number of functions will need to be modified ifrcu_read_lock()has to be added. A far simpler solution is to synchronize with othercpuset_mutexandcpus_write_lockcritical sections before freeing the old housekeeping cpumask. This will ensure that code within those critical sections will not have a UaF situation when accessing housekeeping cpumask. This commit implements the latter solution and make the code inlockdep_is_cpuset_held()andhousekeeping_dereference_check()correct.Fixes a84097e625f2 ("cgroup/cpuset: Call housekeeping_update() without holding cpus_read_lock")Signed-off-by Waiman Long <[email protected]>---
kernel/sched/isolation.c| 10 ++++++++++1 file changed, 10 insertions(+)kernel/sched/isolation.c +0
-0@@ -159,6 +159,16 @@ int housekeeping_update(struct cpumask *isol_mask) err = kthreads_update_housekeeping(); WARN_ON_ONCE(err < 0);+ /*
+ * Synchronize withcpuset_mutexandcpus_read_lockto make sure that
+ *cpuset_mutexandcpus_write_lockcritical sections afterward will
+ * not have access to the old housekeeping cpumask.Quoted 8 lines
+ */
+cpus_read_lock();
+cpuset_lock();
+cpuset_unlock();
+cpus_read_unlock();
+
kfree(old);
return 0;Hi Waiman,
Taking the locks and releasing them right away looks a bit odd to me. It is not obvious what the empty critical section protects until one reads the commit message.
Let me check that I understand the problem correctly.
housekeeping_dereference_check()lets readers holdingcpus_write_lockorcpuset_mutexaccessHK_TYPE_DOMAINwithoutrcu_read_lock(). After commita84097e625f2, the pointer is switched and the old cpumask is freed aftercpuset_mutexandcpus_read_lockhave been dropped, andsynchronize_rcu()does not wait for those readers. For example:CPU A CPU B
rebuild_sched_domains()cpuset_update_sd_hk_unlock()mutex_unlock(&cpuset_mutex)
cpus_read_unlock()mutex_lock(&cpuset_mutex)
p =housekeeping_cpumask(DOMAIN)housekeeping_update()rcu_assign_pointer()synchronize_rcu()kfree(old) access p <- UAF
Quoted 5 lines
Here CPU A reads the mask in
asym_cpu_capacity_scan()ordl_get_task_effective_cpus().If that is right, the root cause is that the pointer is now switched outside of those locks.
The way that housekeeping cpumask is being accessed is that you are going to either get the old cpumask pointer or the new cpumask pointer. It won't be a mess-up version. So lock protection is not essential in this regard.
In the case of old pointer being used, the important point is that it cannot be freed while its content is being accessed.
Quoted 24 lines
So instead of adding a lock barrier before
kfree(), how about switching the pointer before the locks are dropped?housekeeping_update()could be split into two parts:-
housekeeping_publish(): allocate, check and publish the new mask, called withcpus_read_lockandcpuset_mutexheld.-
housekeeping_update():synchronize_rcu(), the workqueue flushes and kfree(old), called after the locks are dropped as it is now.Then a reader holding
cpuset_mutexeither finishes before the pointer is switched, or only sees the new mask:CPU A CPU B
cpuset_update_sd_hk_unlock()
housekeeping_publish()rcu_assign_pointer()mutex_lock(&cpuset_mutex)<- waitmutex_unlock(&cpuset_mutex)
cpus_read_unlock()p = housekeeping_cpumask(DOMAIN)housekeeping_update()<- new cpumasksynchronize_rcu()kfree(old)
Quoted 4 lines
The same applies to
cpus_write_lockreaders, ascpus_read_lockis held until the new mask is published. The workqueue flushes still run without those locks, so the deadlock fixed bya84097e625f2does not come back.
That will certainly work too. I choose the current solution as it is more localized instead of spreading out to a large code section. Either one will work.
Cheers,
Longman