locking/mutex: clear MUTEX_FLAGS if wait_list is empty due to signal
PATCH 1/1 locking/mutex: clear MUTEX_FLAGS if wait_list is empty due to signal
From: Zqiang <[email protected]>
Clear MUTEX_FLAGS when call mutex_lock_interruptible() interrupted by a signal and the lock->wait_list is empty.
--- kernel/locking/mutex.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/locking/mutex.c +2 -0
@@ -1081,6 +1081,8 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, err:__set_current_state (TASK_RUNNING );mutex_remove_waiter (lock, &waiter, current); +if (likely (list_empty (&lock->wait_list))) +__mutex_clear_flag (lock,MUTEX_FLAGS ); err_early_kill:spin_unlock (&lock->wait_lock);debug_mutex_free_waiter (&waiter);
Signature · 1 line
2.25.1
Peter Zijlstra
maintainerQuoted 4 lines · On Sat, May 15, 2021 at 10:30:10AM +0800, [email protected] wrote:
From: Zqiang <[email protected]>
Clear
MUTEX_FLAGSwhen callmutex_lock_interruptible()interrupted by a signal and thelock->wait_listis empty.
That's what the patches does; and I can read the C code perfectly fine, thank you. What the C code doesn't tell me is why, nor how you came to write this patch, was there an actual problem that is solved? Were you bored and just reading the code?
That is, your Changelog tells me absolutely nothing.
Quoted 10 lines
Signed-off-by Zqiang <[email protected]>--- kernel/locking/mutex.c | 2 ++ 1 file changed, 2 insertions(+)kernel/locking/mutex.c +0
-0
kernel/locking/mutex.c __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, · line 1085
Quoted 5 lines
kernel/locking/mutex.c +2
-0@@ -1081,6 +1081,8 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, err:__set_current_state (TASK_RUNNING );mutex_remove_waiter (lock, &waiter, current); +if (likely (list_empty (&lock->wait_list))) +__mutex_clear_flag (lock,MUTEX_FLAGS );
Would not the nicer patch be something like this?
--- kernel/locking/mutex-debug.c | 6 +++--- kernel/locking/mutex-debug.h | 4 ++-- kernel/locking/mutex.c | 17 ++++++++++++----- kernel/locking/mutex.h | 4 +--- 4 files changed, 18 insertions(+), 13 deletions(-)
kernel/locking/mutex-debug.c +3 -3
@@ -57,15 +57,15 @@ void debug_mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter, task->blocked_on = waiter; } -void mutex_remove_waiter (struct mutex *lock,struct mutex_waiter *waiter, -struct task_struct *task) +void debug_mutex_remove_waiter (struct mutex *lock,struct mutex_waiter *waiter, +struct task_struct *task) {DEBUG_LOCKS_WARN_ON (list_empty (&waiter->list));DEBUG_LOCKS_WARN_ON (waiter->task != task);DEBUG_LOCKS_WARN_ON (task->blocked_on != waiter); task->blocked_on =NULL ; -list_del_init (&waiter->list); +INIT_LIST_HEAD (&waiter->list); waiter->task =NULL ; }
kernel/locking/mutex-debug.h +2 -2
@@ -22,8 +22,8 @@ extern void debug_mutex_free_waiter(struct mutex_waiter *waiter);extern void debug_mutex_add_waiter (struct mutex *lock,struct mutex_waiter *waiter,struct task_struct *task); -extern void mutex_remove_waiter (struct mutex *lock,struct mutex_waiter *waiter, -struct task_struct *task); +extern void debug_mutex_remove_waiter (struct mutex *lock,struct mutex_waiter *waiter, +struct task_struct *task);extern void debug_mutex_unlock (struct mutex *lock);extern void debug_mutex_init (struct mutex *lock,const char *name,struct lock_class_key *key);
kernel/locking/mutex.c +12 -5
@@ -205,6 +205,16 @@ __mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter,__mutex_set_flag (lock,MUTEX_FLAG_WAITERS ); } +static void +__mutex_remove_waiter (struct mutex *lock,struct mutex_waiter *waiter) +{ +__list_del (waiter->list.prev, waiter->list.next); +debug_mutex_remove_waiter (lock, &waiter, current); +if (likely (list_empty (&lock->wait_list))) +__mutex_clear_flag (lock,MUTEX_FLAGS ); + +} +/* * Give up ownership to a specific task, when @task = NULL, this is equivalent * to a regular unlock. Sets PICKUP on a handoff, clears HANDOFF, preserves
@@ -1061,10 +1071,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,__ww_mutex_check_waiters (lock, ww_ctx); } -mutex_remove_waiter (lock, &waiter, current); -if (likely (list_empty (&lock->wait_list))) -__mutex_clear_flag (lock,MUTEX_FLAGS ); - +__mutex_remove_waiter (lock, &waiter);debug_mutex_free_waiter (&waiter); skip_wait:
@@ -1080,7 +1087,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, err:__set_current_state (TASK_RUNNING ); -mutex_remove_waiter (lock, &waiter, current); +__mutex_remove_waiter (lock, &waiter); err_early_kill:spin_unlock (&lock->wait_lock);debug_mutex_free_waiter (&waiter);
kernel/locking/mutex.h +1 -3
@@ -10,12 +10,10 @@* !CONFIG_DEBUG_MUTEXES case. Most of them are NOPs: */ -#define mutex_remove_waiter (lock, waiter, task) \ - __list_del((waiter)->list.prev, (waiter)->list.next) -#define debug_mutex_wake_waiter (lock, waiter) do { } while (0)#define debug_mutex_free_waiter (waiter) do { } while (0)#define debug_mutex_add_waiter (lock, waiter, ti) do { } while (0) +#define debug_mutex_remove_waiter (lock, waiter, ti) do { } while (0)#define debug_mutex_unlock (lock) do { } while (0)#define debug_mutex_init (lock, name, key) do { } while (0)
Quoted 15 lines · On 5/14/21 10:30 PM, [email protected] wrote:
From: Zqiang <[email protected]>
Clear
MUTEX_FLAGSwhen callmutex_lock_interruptible()interrupted by a signal and thelock->wait_listis empty.Signed-off-by Zqiang <[email protected]>--- kernel/locking/mutex.c | 2 ++ 1 file changed, 2 insertions(+)kernel/locking/mutex.c +0
-0
kernel/locking/mutex.c __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, · line 1088
Quoted 8 lines
kernel/locking/mutex.c +2
-0@@ -1081,6 +1081,8 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, err:__set_current_state (TASK_RUNNING );mutex_remove_waiter (lock, &waiter, current); +if (likely (list_empty (&lock->wait_list))) +__mutex_clear_flag (lock,MUTEX_FLAGS ); err_early_kill:spin_unlock (&lock->wait_lock);debug_mutex_free_waiter (&waiter);
I can see that the error path is missing the flag clearing code. As Peter had said, you have to be more clear of what problem you are trying to fix. Do you have any reproducer? How often do you see this kind of problem?
Cheers,
Longman