[PATCH RFC 05/33] locking/mutex: Change the atomic_dec_and_mutex_lock() return type

From: Bart Van Assche
Date: Thu Feb 06 2025 - 12:52:14 EST


Change the return type of atomic_dec_and_mutex_lock() from int into
bool. This will make it easier to add a thread-safety annotation to
this function. This patch does not change the behavior of any kernel
code because all atomic_dec_and_mutex_lock() callers either check whether
the return value of this function is zero or that it differs from zero.

This patch makes the atomic_dec_and_mutex_lock() signature consistent
with that of refcount_dec_and_mutex_lock().

Signed-off-by: Bart Van Assche <bvanassche@xxxxxxx>
---
Documentation/locking/mutex-design.rst | 2 +-
include/linux/mutex.h | 2 +-
kernel/locking/mutex.c | 8 ++++----
3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/Documentation/locking/mutex-design.rst b/Documentation/locking/mutex-design.rst
index 7c30b4aa5e28..ec02e47b6e9e 100644
--- a/Documentation/locking/mutex-design.rst
+++ b/Documentation/locking/mutex-design.rst
@@ -144,7 +144,7 @@ Acquire the mutex, interruptible::

Acquire the mutex, interruptible, if dec to 0::

- int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
+ bool atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);

Unlock the mutex::

diff --git a/include/linux/mutex.h b/include/linux/mutex.h
index 2bf91b57591b..6c0a8a843a29 100644
--- a/include/linux/mutex.h
+++ b/include/linux/mutex.h
@@ -196,7 +196,7 @@ extern void mutex_lock_io(struct mutex *lock);
extern int mutex_trylock(struct mutex *lock);
extern void mutex_unlock(struct mutex *lock);

-extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
+bool atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);

DEFINE_GUARD(mutex, struct mutex *, mutex_lock(_T), mutex_unlock(_T))
DEFINE_GUARD_COND(mutex, _try, mutex_trylock(_T))
diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
index b36f23de48f1..0af175f5f031 100644
--- a/kernel/locking/mutex.c
+++ b/kernel/locking/mutex.c
@@ -1118,19 +1118,19 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(contention_end);
*
* return true and hold lock if we dec to 0, return false otherwise
*/
-int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
+bool atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
{
/* dec if we can't possibly hit 0 */
if (atomic_add_unless(cnt, -1, 1))
- return 0;
+ return false;
/* we might hit 0, so take the lock */
mutex_lock(lock);
if (!atomic_dec_and_test(cnt)) {
/* when we actually did the dec, we didn't hit 0 */
mutex_unlock(lock);
- return 0;
+ return false;
}
/* we hit 0, and we hold the lock */
- return 1;
+ return true;
}
EXPORT_SYMBOL(atomic_dec_and_mutex_lock);