locking: Fix racy reads of owner->on_cpu
Subsystems (2)
v2 cover locking: Fix racy reads of owner->on_cpu
v2:
- adding owner_on_cpu() refactor, shared by mutex/rtmutex/rwsem
v1: https://lore.kernel.org/all/[email protected]/
Kefeng Wang (1):
locking: Make owner_on_cpu() into <linux/sched.h>
Marco Elver (1):
locking: Mark racy reads of owner->on_cpu
include/linux/sched.h | 9 +++++++++ kernel/locking/mutex.c | 11 ++--------- kernel/locking/rtmutex.c | 5 ++--- kernel/locking/rwsem.c | 9 --------- 4 files changed, 13 insertions(+), 21 deletions(-)
Signature · 1 line
2.26.2
PATCH v2 1/2 locking: Make owner_on_cpu() into <linux/sched.h>
Move the owner_on_cpu() from kernel/locking/rwsem.c into include/linux/sched.h with under CONFIG_SMP, then use it in the mutex/rwsem/rtmutex to simplify the code.
--- include/linux/sched.h | 9 +++++++++ kernel/locking/mutex.c | 11 ++--------- kernel/locking/rtmutex.c | 5 ++--- kernel/locking/rwsem.c | 9 --------- 4 files changed, 13 insertions(+), 21 deletions(-)
include/linux/sched.h +9 -0
@@ -2171,6 +2171,15 @@ extern long sched_getaffinity(pid_t pid, struct cpumask *mask);#endif #ifdef CONFIG_SMP +static inline bool owner_on_cpu (struct task_struct *owner) +{ +/* +* As lock holder preemption issue, we both skip spinning if +* task is not on cpu or its cpu is preempted +*/ +return owner->on_cpu && !vcpu_is_preempted (task_cpu (owner)); +} +/* Returns effective CPU energy utilization, as seen by the scheduler */ unsigned long sched_cpu_util (int cpu,unsigned long max);#endif /* CONFIG_SMP */
kernel/locking/mutex.c +2 -9
@@ -367,8 +367,7 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,/* * Use vcpu_is_preempted to detect lock holder preemption issue. */ -if (!owner->on_cpu ||need_resched () || -vcpu_is_preempted (task_cpu (owner))) { +if (!owner_on_cpu (owner) ||need_resched ()) { ret = false;break ; }
@@ -403,14 +402,8 @@ static inline int mutex_can_spin_on_owner(struct mutex *lock)* structure won't go away during the spinning period. */ owner =__mutex_owner (lock); - -/* -* As lock holder preemption issue, we both skip spinning if task is not -* on cpu or its cpu is preempted -*/ -if (owner) - retval = owner->on_cpu && !vcpu_is_preempted (task_cpu (owner)); + retval =owner_on_cpu (owner);/* * If lock->owner is not set, the mutex has been released. Return true
kernel/locking/rtmutex.c +2 -3
@@ -1379,9 +1379,8 @@ static bool rtmutex_spin_on_owner(struct rt_mutex_base *lock,* for CONFIG_PREEMPT_RCU=y) * - the VCPU on which owner runs is preempted */ -if (!owner->on_cpu ||need_resched () || -rt_mutex_waiter_is_top_waiter (lock, waiter) || -vcpu_is_preempted (task_cpu (owner))) { +if (!owner_on_cpu (owner) ||need_resched () || +rt_mutex_waiter_is_top_waiter (lock, waiter)) { res = false;break ; }
kernel/locking/rwsem.c +0 -9
@@ -658,15 +658,6 @@ static inline bool rwsem_try_write_lock_unqueued(struct rw_semaphore *sem)return false; } -static inline bool owner_on_cpu (struct task_struct *owner) -{ -/* -* As lock holder preemption issue, we both skip spinning if -* task is not on cpu or its cpu is preempted -*/ -return owner->on_cpu && !vcpu_is_preempted (task_cpu (owner)); -} -static inline bool rwsem_can_spin_on_owner (struct rw_semaphore *sem) {struct task_struct *owner;
Signature · 1 line
2.26.2
[tip: locking/core] locking: Make owner_on_cpu() into <linux/sched.h>
The following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: c0bed69daf4b67809b58cc7cd81a8fa4f45bc161 Gitweb: https://git.kernel.org/tip/c0bed69daf4b67809b58cc7cd81a8fa4f45bc161 Author: Kefeng Wang <[email protected]> AuthorDate: Fri, 03 Dec 2021 15:59:34 +08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Sat, 04 Dec 2021 10:56:25 +01:00
locking: Make owner_on_cpu() into <linux/sched.h>
Move the owner_on_cpu() from kernel/locking/rwsem.c into include/linux/sched.h with under CONFIG_SMP, then use it in the mutex/rwsem/rtmutex to simplify the code.
--- include/linux/sched.h | 9 +++++++++ kernel/locking/mutex.c | 11 ++--------- kernel/locking/rtmutex.c | 5 ++--- kernel/locking/rwsem.c | 9 --------- 4 files changed, 13 insertions(+), 21 deletions(-)
include/linux/sched.h +9 -0
@@ -2171,6 +2171,15 @@ extern long sched_getaffinity(pid_t pid, struct cpumask *mask);#endif #ifdef CONFIG_SMP +static inline bool owner_on_cpu (struct task_struct *owner) +{ +/* +* As lock holder preemption issue, we both skip spinning if +* task is not on cpu or its cpu is preempted +*/ +return owner->on_cpu && !vcpu_is_preempted (task_cpu (owner)); +} +/* Returns effective CPU energy utilization, as seen by the scheduler */ unsigned long sched_cpu_util (int cpu,unsigned long max);#endif /* CONFIG_SMP */
kernel/locking/mutex.c +2 -9
@@ -367,8 +367,7 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,/* * Use vcpu_is_preempted to detect lock holder preemption issue. */ -if (!owner->on_cpu ||need_resched () || -vcpu_is_preempted (task_cpu (owner))) { +if (!owner_on_cpu (owner) ||need_resched ()) { ret = false;break ; }
@@ -403,14 +402,8 @@ static inline int mutex_can_spin_on_owner(struct mutex *lock)* structure won't go away during the spinning period. */ owner =__mutex_owner (lock); - -/* -* As lock holder preemption issue, we both skip spinning if task is not -* on cpu or its cpu is preempted -*/ -if (owner) - retval = owner->on_cpu && !vcpu_is_preempted (task_cpu (owner)); + retval =owner_on_cpu (owner);/* * If lock->owner is not set, the mutex has been released. Return true
kernel/locking/rtmutex.c +2 -3
@@ -1382,9 +1382,8 @@ static bool rtmutex_spin_on_owner(struct rt_mutex_base *lock,* for CONFIG_PREEMPT_RCU=y) * - the VCPU on which owner runs is preempted */ -if (!owner->on_cpu ||need_resched () || -rt_mutex_waiter_is_top_waiter (lock, waiter) || -vcpu_is_preempted (task_cpu (owner))) { +if (!owner_on_cpu (owner) ||need_resched () || +rt_mutex_waiter_is_top_waiter (lock, waiter)) { res = false;break ; }
kernel/locking/rwsem.c +0 -9
@@ -613,15 +613,6 @@ static inline bool rwsem_try_write_lock_unqueued(struct rw_semaphore *sem)return false; } -static inline bool owner_on_cpu (struct task_struct *owner) -{ -/* -* As lock holder preemption issue, we both skip spinning if -* task is not on cpu or its cpu is preempted -*/ -return owner->on_cpu && !vcpu_is_preempted (task_cpu (owner)); -} -static inline bool rwsem_can_spin_on_owner (struct rw_semaphore *sem) {struct task_struct *owner;
PATCH v2 2/2 locking: Mark racy reads of owner->on_cpu
In reply to Kefeng WangFrom: Marco Elver <[email protected]>
One of the more frequent data races reported by KCSAN is the racy read in mutex_spin_on_owner(), which is usually reported as "race of unknown origin" without showing the writer. This is due to the racing write occurring in kernel/sched. Locally enabling KCSAN in kernel/sched shows:
| write (marked) to 0xffff97f205079934 of 4 bytes by task 316 on cpu 6: | finish_task kernel/sched/core.c:4632 [inline] | finish_task_switch kernel/sched/core.c:4848 | context_switch kernel/sched/core.c:4975 [inline] | __schedule kernel/sched/core.c:6253 | schedule kernel/sched/core.c:6326 | schedule_preempt_disabled kernel/sched/core.c:6385 | __mutex_lock_common kernel/locking/mutex.c:680 | __mutex_lock kernel/locking/mutex.c:740 [inline] | __mutex_lock_slowpath kernel/locking/mutex.c:1028 | mutex_lock kernel/locking/mutex.c:283 | tty_open_by_driver drivers/tty/tty_io.c:2062 [inline] | ... | | read to 0xffff97f205079934 of 4 bytes by task 322 on cpu 3: | mutex_spin_on_owner kernel/locking/mutex.c:370 | mutex_optimistic_spin kernel/locking/mutex.c:480 | __mutex_lock_common kernel/locking/mutex.c:610 | __mutex_lock kernel/locking/mutex.c:740 [inline] | __mutex_lock_slowpath kernel/locking/mutex.c:1028 | mutex_lock kernel/locking/mutex.c:283 | tty_open_by_driver drivers/tty/tty_io.c:2062 [inline] | ... | | value changed: 0x00000001 -> 0x00000000
This race is clearly intentional, and the potential for miscompilation is slim due to surrounding barrier() and cpu_relax(), and the value being used as a boolean.
Nevertheless, marking this reader would more clearly denote intent and make it obvious that concurrency is expected. Use READ_ONCE() to avoid having to reason about compiler optimizations now and in future.
With previous refactor, mark the read to owner->on_cpu in owner_on_cpu(), which immediately precedes the loop executing mutex_spin_on_owner().
--- include/linux/sched.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/sched.h +1 -1
@@ -2177,7 +2177,7 @@ static inline bool owner_on_cpu(struct task_struct *owner)* As lock holder preemption issue, we both skip spinning if * task is not on cpu or its cpu is preempted */ -return owner->on_cpu && !vcpu_is_preempted (task_cpu (owner)); +return READ_ONCE (owner->on_cpu) && !vcpu_is_preempted (task_cpu (owner)); }/* Returns effective CPU energy utilization, as seen by the scheduler */
Signature · 1 line
2.26.2
[tip: locking/core] locking: Mark racy reads of owner->on_cpu
The following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 4cf75fd4a2545ca4deea992f929602c9fdbe8058 Gitweb: https://git.kernel.org/tip/4cf75fd4a2545ca4deea992f929602c9fdbe8058 Author: Marco Elver <[email protected]> AuthorDate: Fri, 03 Dec 2021 15:59:35 +08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Sat, 04 Dec 2021 10:56:25 +01:00
locking: Mark racy reads of owner->on_cpu
One of the more frequent data races reported by KCSAN is the racy read in mutex_spin_on_owner(), which is usually reported as "race of unknown origin" without showing the writer. This is due to the racing write occurring in kernel/sched. Locally enabling KCSAN in kernel/sched shows:
| write (marked) to 0xffff97f205079934 of 4 bytes by task 316 on cpu 6: | finish_task kernel/sched/core.c:4632 [inline] | finish_task_switch kernel/sched/core.c:4848 | context_switch kernel/sched/core.c:4975 [inline] | __schedule kernel/sched/core.c:6253 | schedule kernel/sched/core.c:6326 | schedule_preempt_disabled kernel/sched/core.c:6385 | __mutex_lock_common kernel/locking/mutex.c:680 | __mutex_lock kernel/locking/mutex.c:740 [inline] | __mutex_lock_slowpath kernel/locking/mutex.c:1028 | mutex_lock kernel/locking/mutex.c:283 | tty_open_by_driver drivers/tty/tty_io.c:2062 [inline] | ... | | read to 0xffff97f205079934 of 4 bytes by task 322 on cpu 3: | mutex_spin_on_owner kernel/locking/mutex.c:370 | mutex_optimistic_spin kernel/locking/mutex.c:480 | __mutex_lock_common kernel/locking/mutex.c:610 | __mutex_lock kernel/locking/mutex.c:740 [inline] | __mutex_lock_slowpath kernel/locking/mutex.c:1028 | mutex_lock kernel/locking/mutex.c:283 | tty_open_by_driver drivers/tty/tty_io.c:2062 [inline] | ... | | value changed: 0x00000001 -> 0x00000000
This race is clearly intentional, and the potential for miscompilation is slim due to surrounding barrier() and cpu_relax(), and the value being used as a boolean.
Nevertheless, marking this reader would more clearly denote intent and make it obvious that concurrency is expected. Use READ_ONCE() to avoid having to reason about compiler optimizations now and in future.
With previous refactor, mark the read to owner->on_cpu in owner_on_cpu(), which immediately precedes the loop executing mutex_spin_on_owner().
--- include/linux/sched.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/sched.h +1 -1
@@ -2177,7 +2177,7 @@ static inline bool owner_on_cpu(struct task_struct *owner)* As lock holder preemption issue, we both skip spinning if * task is not on cpu or its cpu is preempted */ -return owner->on_cpu && !vcpu_is_preempted (task_cpu (owner)); +return READ_ONCE (owner->on_cpu) && !vcpu_is_preempted (task_cpu (owner)); }/* Returns effective CPU energy utilization, as seen by the scheduler */
Peter Zijlstra
maintainerQuoted 8 lines · Kefeng Wang
v2:
- addingowner_on_cpu()refactor, shared by mutex/rtmutex/rwsemv1: https://lore.kernel.org/all/[email protected]/
Kefeng Wang (1):
locking: Makeowner_on_cpu()into <linux/sched.h>
Quoted 8 lines
Marco Elver (1):
locking: Mark racy reads ofowner->on_cpuinclude/linux/sched.h | 9 +++++++++ kernel/locking/mutex.c | 11 ++--------- kernel/locking/rtmutex.c | 5 ++--- kernel/locking/rwsem.c | 9 --------- 4 files changed, 13 insertions(+), 21 deletions(-)
Thanks!
Quoted 8 lines · Kefeng Wang
v2:
- addingowner_on_cpu()refactor, shared by mutex/rtmutex/rwsemv1: https://lore.kernel.org/all/[email protected]/
Kefeng Wang (1):
locking: Makeowner_on_cpu()into <linux/sched.h>
Quoted 9 lines
Marco Elver (1):
locking: Mark racy reads ofowner->on_cpuinclude/linux/sched.h | 9 +++++++++ kernel/locking/mutex.c | 11 ++--------- kernel/locking/rtmutex.c | 5 ++--- kernel/locking/rwsem.c | 9 --------- 4 files changed, 13 insertions(+), 21 deletions(-)
LGTM