Skip to content

Commit 905ef03

Browse files
Waiman-Longgregkh
authored andcommitted
locking/ww_mutex: Simplify use_ww_ctx & ww_ctx handling
[ Upstream commit 5de2055 ] The use_ww_ctx flag is passed to mutex_optimistic_spin(), but the function doesn't use it. The frequent use of the (use_ww_ctx && ww_ctx) combination is repetitive. In fact, ww_ctx should not be used at all if !use_ww_ctx. Simplify ww_mutex code by dropping use_ww_ctx from mutex_optimistic_spin() an clear ww_ctx if !use_ww_ctx. In this way, we can replace (use_ww_ctx && ww_ctx) by just (ww_ctx). Signed-off-by: Waiman Long <longman@redhat.com> Signed-off-by: Ingo Molnar <mingo@kernel.org> Acked-by: Davidlohr Bueso <dbueso@suse.de> Link: https://lore.kernel.org/r/20210316153119.13802-2-longman@redhat.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 1e2a75c commit 905ef03

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

‎kernel/locking/mutex.c‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ static inline int mutex_can_spin_on_owner(struct mutex *lock)
636636
*/
637637
static __always_inline bool
638638
mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
639-
const bool use_ww_ctx, struct mutex_waiter *waiter)
639+
struct mutex_waiter *waiter)
640640
{
641641
if (!waiter) {
642642
/*
@@ -712,7 +712,7 @@ mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
712712
#else
713713
static __always_inline bool
714714
mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
715-
const bool use_ww_ctx, struct mutex_waiter *waiter)
715+
struct mutex_waiter *waiter)
716716
{
717717
return false;
718718
}
@@ -932,14 +932,17 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
932932
struct ww_mutex *ww;
933933
int ret;
934934

935+
if (!use_ww_ctx)
936+
ww_ctx = NULL;
937+
935938
might_sleep();
936939

937940
#ifdef CONFIG_DEBUG_MUTEXES
938941
DEBUG_LOCKS_WARN_ON(lock->magic != lock);
939942
#endif
940943

941944
ww = container_of(lock, struct ww_mutex, base);
942-
if (use_ww_ctx && ww_ctx) {
945+
if (ww_ctx) {
943946
if (unlikely(ww_ctx == READ_ONCE(ww->ctx)))
944947
return -EALREADY;
945948

@@ -956,10 +959,10 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
956959
mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
957960

958961
if (__mutex_trylock(lock) ||
959-
mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, NULL)) {
962+
mutex_optimistic_spin(lock, ww_ctx, NULL)) {
960963
/* got the lock, yay! */
961964
lock_acquired(&lock->dep_map, ip);
962-
if (use_ww_ctx && ww_ctx)
965+
if (ww_ctx)
963966
ww_mutex_set_context_fastpath(ww, ww_ctx);
964967
preempt_enable();
965968
return 0;
@@ -970,7 +973,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
970973
* After waiting to acquire the wait_lock, try again.
971974
*/
972975
if (__mutex_trylock(lock)) {
973-
if (use_ww_ctx && ww_ctx)
976+
if (ww_ctx)
974977
__ww_mutex_check_waiters(lock, ww_ctx);
975978

976979
goto skip_wait;
@@ -1023,7 +1026,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
10231026
goto err;
10241027
}
10251028

1026-
if (use_ww_ctx && ww_ctx) {
1029+
if (ww_ctx) {
10271030
ret = __ww_mutex_check_kill(lock, &waiter, ww_ctx);
10281031
if (ret)
10291032
goto err;
@@ -1036,7 +1039,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
10361039
* ww_mutex needs to always recheck its position since its waiter
10371040
* list is not FIFO ordered.
10381041
*/
1039-
if ((use_ww_ctx && ww_ctx) || !first) {
1042+
if (ww_ctx || !first) {
10401043
first = __mutex_waiter_is_first(lock, &waiter);
10411044
if (first)
10421045
__mutex_set_flag(lock, MUTEX_FLAG_HANDOFF);
@@ -1049,7 +1052,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
10491052
* or we must see its unlock and acquire.
10501053
*/
10511054
if (__mutex_trylock(lock) ||
1052-
(first && mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, &waiter)))
1055+
(first && mutex_optimistic_spin(lock, ww_ctx, &waiter)))
10531056
break;
10541057

10551058
spin_lock(&lock->wait_lock);
@@ -1058,7 +1061,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
10581061
acquired:
10591062
__set_current_state(TASK_RUNNING);
10601063

1061-
if (use_ww_ctx && ww_ctx) {
1064+
if (ww_ctx) {
10621065
/*
10631066
* Wound-Wait; we stole the lock (!first_waiter), check the
10641067
* waiters as anyone might want to wound us.
@@ -1078,7 +1081,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
10781081
/* got the lock - cleanup and rejoice! */
10791082
lock_acquired(&lock->dep_map, ip);
10801083

1081-
if (use_ww_ctx && ww_ctx)
1084+
if (ww_ctx)
10821085
ww_mutex_lock_acquired(ww, ww_ctx);
10831086

10841087
spin_unlock(&lock->wait_lock);

0 commit comments

Comments
 (0)