Skip to content

Commit c0bed69

Browse files
Kefeng WangPeter Zijlstra
authored andcommitted
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. Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lore.kernel.org/r/20211203075935.136808-2-wangkefeng.wang@huawei.com
1 parent 9a75bd0 commit c0bed69

4 files changed

Lines changed: 13 additions & 21 deletions

File tree

include/linux/sched.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,6 +2171,15 @@ extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
21712171
#endif
21722172

21732173
#ifdef CONFIG_SMP
2174+
static inline bool owner_on_cpu(struct task_struct *owner)
2175+
{
2176+
/*
2177+
* As lock holder preemption issue, we both skip spinning if
2178+
* task is not on cpu or its cpu is preempted
2179+
*/
2180+
return owner->on_cpu && !vcpu_is_preempted(task_cpu(owner));
2181+
}
2182+
21742183
/* Returns effective CPU energy utilization, as seen by the scheduler */
21752184
unsigned long sched_cpu_util(int cpu, unsigned long max);
21762185
#endif /* CONFIG_SMP */

kernel/locking/mutex.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,7 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,
367367
/*
368368
* Use vcpu_is_preempted to detect lock holder preemption issue.
369369
*/
370-
if (!owner->on_cpu || need_resched() ||
371-
vcpu_is_preempted(task_cpu(owner))) {
370+
if (!owner_on_cpu(owner) || need_resched()) {
372371
ret = false;
373372
break;
374373
}
@@ -403,14 +402,8 @@ static inline int mutex_can_spin_on_owner(struct mutex *lock)
403402
* structure won't go away during the spinning period.
404403
*/
405404
owner = __mutex_owner(lock);
406-
407-
/*
408-
* As lock holder preemption issue, we both skip spinning if task is not
409-
* on cpu or its cpu is preempted
410-
*/
411-
412405
if (owner)
413-
retval = owner->on_cpu && !vcpu_is_preempted(task_cpu(owner));
406+
retval = owner_on_cpu(owner);
414407

415408
/*
416409
* If lock->owner is not set, the mutex has been released. Return true

kernel/locking/rtmutex.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,9 +1382,8 @@ static bool rtmutex_spin_on_owner(struct rt_mutex_base *lock,
13821382
* for CONFIG_PREEMPT_RCU=y)
13831383
* - the VCPU on which owner runs is preempted
13841384
*/
1385-
if (!owner->on_cpu || need_resched() ||
1386-
rt_mutex_waiter_is_top_waiter(lock, waiter) ||
1387-
vcpu_is_preempted(task_cpu(owner))) {
1385+
if (!owner_on_cpu(owner) || need_resched() ||
1386+
rt_mutex_waiter_is_top_waiter(lock, waiter)) {
13881387
res = false;
13891388
break;
13901389
}

kernel/locking/rwsem.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -613,15 +613,6 @@ static inline bool rwsem_try_write_lock_unqueued(struct rw_semaphore *sem)
613613
return false;
614614
}
615615

616-
static inline bool owner_on_cpu(struct task_struct *owner)
617-
{
618-
/*
619-
* As lock holder preemption issue, we both skip spinning if
620-
* task is not on cpu or its cpu is preempted
621-
*/
622-
return owner->on_cpu && !vcpu_is_preempted(task_cpu(owner));
623-
}
624-
625616
static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem)
626617
{
627618
struct task_struct *owner;

0 commit comments

Comments
 (0)