[PATCH v2] locking/mutex: remove redundant argument from __mutex_lock_common()

Michał Mirosław posted 1 patch 2 years, 3 months ago
There is a newer version of this series
kernel/locking/mutex.c    | 16 ++++++----------
kernel/locking/ww_mutex.h |  5 -----
2 files changed, 6 insertions(+), 15 deletions(-)
[PATCH v2] locking/mutex: remove redundant argument from __mutex_lock_common()
Posted by Michał Mirosław 2 years, 3 months ago
use_ww_ctx is equivalent to ww_ctx != NULL. The one case where
use_ww_ctx was true but ww_ctx == NULL leads to the same
__mutex_add_waiter() call via __ww_mutex_add_waiter().

Since now __ww_mutex_add_waiter() is called only with ww_ctx != NULL
(from both regular and PREEMPT_RT implementations), remove the
branch there.

Signed-off-by: Michał Mirosław <mirq-linux@rere.qmqm.pl>
---
 v2: extended commit message to note that PREEMPT_RT does not call
     __ww_mutex_add_waiter() with ww_ctx == NULL
---
 kernel/locking/mutex.c    | 16 ++++++----------
 kernel/locking/ww_mutex.h |  5 -----
 2 files changed, 6 insertions(+), 15 deletions(-)

diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
index d973fe6041bf..2f0e318233f5 100644
--- a/kernel/locking/mutex.c
+++ b/kernel/locking/mutex.c
@@ -568,15 +568,12 @@ EXPORT_SYMBOL(ww_mutex_unlock);
 static __always_inline int __sched
 __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclass,
 		    struct lockdep_map *nest_lock, unsigned long ip,
-		    struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
+		    struct ww_acquire_ctx *ww_ctx)
 {
 	struct mutex_waiter waiter;
 	struct ww_mutex *ww;
 	int ret;
 
-	if (!use_ww_ctx)
-		ww_ctx = NULL;
-
 	might_sleep();
 
 	MUTEX_WARN_ON(lock->magic != lock);
@@ -627,12 +624,11 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas
 
 	debug_mutex_lock_common(lock, &waiter);
 	waiter.task = current;
-	if (use_ww_ctx)
-		waiter.ww_ctx = ww_ctx;
+	waiter.ww_ctx = ww_ctx;
 
 	lock_contended(&lock->dep_map, ip);
 
-	if (!use_ww_ctx) {
+	if (!ww_ctx) {
 		/* add waiting tasks to the end of the waitqueue (FIFO): */
 		__mutex_add_waiter(lock, &waiter, &lock->wait_list);
 	} else {
@@ -744,14 +740,14 @@ static int __sched
 __mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass,
 	     struct lockdep_map *nest_lock, unsigned long ip)
 {
-	return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false);
+	return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL);
 }
 
 static int __sched
 __ww_mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass,
 		unsigned long ip, struct ww_acquire_ctx *ww_ctx)
 {
-	return __mutex_lock_common(lock, state, subclass, NULL, ip, ww_ctx, true);
+	return __mutex_lock_common(lock, state, subclass, NULL, ip, ww_ctx);
 }
 
 /**
@@ -831,7 +827,7 @@ mutex_lock_io_nested(struct mutex *lock, unsigned int subclass)
 
 	token = io_schedule_prepare();
 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
-			    subclass, NULL, _RET_IP_, NULL, 0);
+			    subclass, NULL, _RET_IP_, NULL);
 	io_schedule_finish(token);
 }
 EXPORT_SYMBOL_GPL(mutex_lock_io_nested);
diff --git a/kernel/locking/ww_mutex.h b/kernel/locking/ww_mutex.h
index 3ad2cc4823e5..11acb2efe976 100644
--- a/kernel/locking/ww_mutex.h
+++ b/kernel/locking/ww_mutex.h
@@ -493,11 +493,6 @@ __ww_mutex_add_waiter(struct MUTEX_WAITER *waiter,
 	struct MUTEX_WAITER *cur, *pos = NULL;
 	bool is_wait_die;
 
-	if (!ww_ctx) {
-		__ww_waiter_add(lock, waiter, NULL);
-		return 0;
-	}
-
 	is_wait_die = ww_ctx->is_wait_die;
 
 	/*
-- 
2.39.2

Re: [PATCH v2] locking/mutex: remove redundant argument from __mutex_lock_common()
Posted by Peter Zijlstra 2 years, 3 months ago
On Sun, Sep 03, 2023 at 12:33:19AM +0200, Michał Mirosław wrote:
> use_ww_ctx is equivalent to ww_ctx != NULL. The one case where
> use_ww_ctx was true but ww_ctx == NULL leads to the same
> __mutex_add_waiter() call via __ww_mutex_add_waiter().
> 
> Since now __ww_mutex_add_waiter() is called only with ww_ctx != NULL
> (from both regular and PREEMPT_RT implementations), remove the
> branch there.
> 

There were compilers that failed to constant propagate the ww_ctx==NULL
thing properly and generated crap code, the use_ww_ctx thing fixed that.

I can't remember which compilers that were (my brain is saying <gcc-6 or
something, but I could be totally wrong) and if we still care about
people using them (probably not).

Re: [PATCH v2] locking/mutex: remove redundant argument from __mutex_lock_common()
Posted by Ingo Molnar 2 years, 3 months ago
* Peter Zijlstra <peterz@infradead.org> wrote:

> On Sun, Sep 03, 2023 at 12:33:19AM +0200, Michał Mirosław wrote:
> > use_ww_ctx is equivalent to ww_ctx != NULL. The one case where
> > use_ww_ctx was true but ww_ctx == NULL leads to the same
> > __mutex_add_waiter() call via __ww_mutex_add_waiter().
> > 
> > Since now __ww_mutex_add_waiter() is called only with ww_ctx != NULL
> > (from both regular and PREEMPT_RT implementations), remove the
> > branch there.
> > 
> 
> There were compilers that failed to constant propagate the ww_ctx==NULL 
> thing properly and generated crap code, the use_ww_ctx thing fixed that.
> 
> I can't remember which compilers that were (my brain is saying <gcc-6 or 
> something, but I could be totally wrong) and if we still care about 
> people using them (probably not).

The changelog of the patch should probably include before/after generated 
code comparison & analysis - or at minimum a '/bin/size' comparison to 
quantify the changes to generated code.

Thanks,

	Ingo