Skip to contentSkip to conversation

Lock warning cleanup

PATCH In review Jules Irenge 3 participants 8 messages Jan 24, 2020 – Feb 1, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Lock warning cleanup

1. TIME subsytem : patch 1, an __acquires(timer) annotation is added. as the function despite having a nested lock the outer one allows entry to critical section only.
2. Within futex.c file or path 2, a __releases() annotation is added. as the function releases the lock at exit.
3. MUTEX subsystem : patch 3, __acquires(lock) and __releases(lock) are added to mutex_lock() and mutex_unlock() to fix issues raised in other files.

Jules Irenge (3):
time: Add missing annotation to lock_hrtimer_base()
futex: Add missing annotation for wake_futex_pi()
mutex: Add missing annotations

 include/linux/mutex.h | 4 ++--
 kernel/futex.c        | 1 +
 kernel/time/hrtimer.c | 1 +
 3 files changed, 4 insertions(+), 2 deletions(-)
Signature · 1 line
2.24.1
#4 lore raw

PATCH 2/3 futex: Add missing annotation for wake_futex_pi()

Sparse reports a warning at wake_futex_pi()

|warning: context imbalance in wake_futex_pi() - unexpected unlock To fix this,
a __releases(&pi_state->pi_mutex.wait_lock) annotation is added Given that wake_futex_pi() does actually call raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock)
This not only fixes the warning
but also improves on the readability of the code.

---
 kernel/futex.c | 1 +
 1 file changed, 1 insertion(+)
kernel/futex.c +1 -0
index 03c518e9747e..8bc288a7187f 100644
@@ -1549,6 +1549,7 @@ static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q)
  * Caller must hold a reference on @pi_state.
  */
 static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_pi_state *pi_state)
+	__releases(&pi_state->pi_mutex.wait_lock)
 {
 	u32 uninitialized_var(curval), newval;
 	struct task_struct *new_owner;
Signature · 1 line
2.24.1
#5 lore raw

PATCH 3/3 mutex: Add missing annotations

In reply to Jules Irenge

Sparse reports false warnings and hide real warnings where mutex_lock() and mutex_unlock() are used within the kernel An example is within the kernel cgroup files
where the below warnings are found

|warning: context imbalance in cgroup_lock_and_drain_offline()
| - wrong count at exit
|warning: context imbalance in cgroup_procs_write_finish()
|- wrong count at exit
|warning: context imbalance in cgroup_procs_write_start()
|- wrong count at exit.

To fix these,
an __acquires(lock) is added to mutex_lock() declaration a __releases(lock) to mutex_unlock() declaration

---
 include/linux/mutex.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/mutex.h +2 -2
index aca8f36dfac9..a8ab4029913e 100644
@@ -162,7 +162,7 @@ do {									\
 } while (0)
 
 #else
-extern void mutex_lock(struct mutex *lock);
+extern void mutex_lock(struct mutex *lock) __acquires(lock);
 extern int __must_check mutex_lock_interruptible(struct mutex *lock);
 extern int __must_check mutex_lock_killable(struct mutex *lock);
 extern void mutex_lock_io(struct mutex *lock);
@@ -181,7 +181,7 @@ extern void mutex_lock_io(struct mutex *lock);
  * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
  */
 extern int mutex_trylock(struct mutex *lock);
-extern void mutex_unlock(struct mutex *lock);
+extern void mutex_unlock(struct mutex *lock) __releases(lock);
 
 extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
 
Signature · 1 line
2.24.1
#1 lore raw

PATCH 1/3 time: Add missing annotation to lock_hrtimer_base()

In reply to Jules Irenge

Sparse reports a warning at lock_hrtimer_base()

|warning: context imbalance in lock_hrtimer_base() - wrong count at exit
|warning: context imbalance in hrtimer_start_range_ns() - unexpected unlock
|warning: context imbalance in hrtimer_try_to_cancel() - unexpected unlock
|warning: context imbalance in __hrtimer_get_remaining()
|- unexpected unlock

To fix this , an __acquires(timer) annotation is added.
Given that lock_hrtimer_base() does actually call READ_ONCE(timer->base). This not only fixes the warnings
but also improves on readability of the code.

---
 kernel/time/hrtimer.c | 1 +
 1 file changed, 1 insertion(+)
kernel/time/hrtimer.c +1 -0
index 8de90ea31280..8f555b49395a 100644
@@ -160,6 +160,7 @@ static inline bool is_migration_base(struct hrtimer_clock_base *base)
 static
 struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
 					     unsigned long *flags)
+	__acquires(timer)
 {
 	struct hrtimer_clock_base *base;
 
Signature · 1 line
2.24.1
#2 lore raw

Re: [PATCH 1/3] time: Add missing annotation to lock_hrtimer_base()

Jules,

Jules Irenge <[email protected]> writes:

Please use the proper subsystem prefixes when sending patches.

git log --oneline path/to/file

gives you usally a pretty good hint.

Quoted 3 lines

Sparse reports a warning at lock_hrtimer_base()

|warning: context imbalance in lock_hrtimer_base() - wrong count at exit

This leading '|' is pointless

Quoted 4 lines
|warning: context imbalance in hrtimer_start_range_ns() - unexpected unlock
|warning: context imbalance in hrtimer_try_to_cancel() - unexpected unlock
|warning: context imbalance in __hrtimer_get_remaining()
|- unexpected unlock

How are the last 3 related to:

Quoted 1 line

Sparse reports a warning at lock_hrtimer_base()

?

Quoted 1 line

To fix this , an __acquires(timer) annotation is added.

Add the missing __acquires(timer) annotation.

Is precise and follows the recommendations of Documentation/process/...

Quoted 1 line

Given that lock_hrtimer_base() does actually call READ_ONCE(timer->base).

Given that the above sentence uses 'Given that' it should not terminate right after explaining the 'Given'.

Quoted 2 lines

This not only fixes the warnings but also improves on readability of the code.

I tend to disagree. In fact the annotation disturbes the reading flow because it's on a separate line.

Can you please stop using this boilerplate which is neither helping review nor giving someone who looks at the commit later on any useful information?

Here is a suggestion for a change log for this:

Sparse reports several warnings;
warning: context imbalance in lock_hrtimer_base() - wrong count at exit
warning: context imbalance in hrtimer_start_range_ns() - unexpected unlock
warning: context imbalance in hrtimer_try_to_cancel() - unexpected unlock
warning: context imbalance in __hrtimer_get_remaining()- unexpected unlock

The root cause is a missing annotation of lock_hrtimer_base() which causes also the 'unexpected unlock' warnings.

Add the missing __acquires(timer) annotation.

Hmm?

The other 2 patches of this series have similar issues. The futex changelog is also horribly formatted.

Thanks,

tglx

#3 lore raw

Re: [PATCH 1/3] time: Add missing annotation to lock_hrtimer_base()

Thanks Thomas. I really appreciate your feedback, I take good note and I will send a different version with all the changes in reference to the email.

Thanks again,
Kind regards
Jules

Quoted 70 lines · Thomas Gleixner

Jules,

Jules Irenge <[email protected]> writes:

Please use the proper subsystem prefixes when sending patches.

git log --oneline path/to/file

gives you usally a pretty good hint.

Quoted 3 lines

Sparse reports a warning at lock_hrtimer_base()

|warning: context imbalance in lock_hrtimer_base() - wrong count at exit

This leading '|' is pointless

Quoted 4 lines
|warning: context imbalance in hrtimer_start_range_ns() - unexpected unlock
|warning: context imbalance in hrtimer_try_to_cancel() - unexpected unlock
|warning: context imbalance in __hrtimer_get_remaining()
|- unexpected unlock

How are the last 3 related to:

Quoted 1 line

Sparse reports a warning at lock_hrtimer_base()

?

Quoted 1 line

To fix this , an __acquires(timer) annotation is added.

Add the missing __acquires(timer) annotation.

Is precise and follows the recommendations of Documentation/process/...

Quoted 1 line

Given that lock_hrtimer_base() does actually call READ_ONCE(timer->base).

Given that the above sentence uses 'Given that' it should not terminate right after explaining the 'Given'.

Quoted 2 lines

This not only fixes the warnings but also improves on readability of the code.

I tend to disagree. In fact the annotation disturbes the reading flow because it's on a separate line.

Can you please stop using this boilerplate which is neither helping review nor giving someone who looks at the commit later on any useful information?

Here is a suggestion for a change log for this:

Sparse reports several warnings;
warning: context imbalance in lock_hrtimer_base() - wrong count at exit
warning: context imbalance in hrtimer_start_range_ns() - unexpected unlock
warning: context imbalance in hrtimer_try_to_cancel() - unexpected unlock
warning: context imbalance in __hrtimer_get_remaining()- unexpected unlock

The root cause is a missing annotation of lock_hrtimer_base() which causes also the 'unexpected unlock' warnings.

Add the missing __acquires(timer) annotation.

Hmm?

The other 2 patches of this series have similar issues. The futex changelog is also horribly formatted.

Thanks,

tglx

maintainer
#6 lore raw

Re: [PATCH 3/3] mutex: Add missing annotations

In reply to Jules Irenge
Quoted 34 lines · Jules Irenge

Sparse reports false warnings and hide real warnings where mutex_lock() and mutex_unlock() are used within the kernel An example is within the kernel cgroup files
where the below warnings are found

|warning: context imbalance in cgroup_lock_and_drain_offline()
| - wrong count at exit
|warning: context imbalance in cgroup_procs_write_finish()
|- wrong count at exit
|warning: context imbalance in cgroup_procs_write_start()
|- wrong count at exit.

To fix these,
an __acquires(lock) is added to mutex_lock() declaration a __releases(lock) to mutex_unlock() declaration

---
 include/linux/mutex.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/mutex.h +1 -1
index aca8f36dfac9..a8ab4029913e 100644
@@ -162,7 +162,7 @@ do {									\
 } while (0)
 
 #else
-extern void mutex_lock(struct mutex *lock);
+extern void mutex_lock(struct mutex *lock) __acquires(lock);
 extern int __must_check mutex_lock_interruptible(struct mutex *lock);
 extern int __must_check mutex_lock_killable(struct mutex *lock);
 extern void mutex_lock_io(struct mutex *lock);

include/linux/mutex.h extern void mutex_lock_io(struct mutex *lock); · line 186

Quoted 7 lines
include/linux/mutex.h +1 -1
@@ -181,7 +181,7 @@ extern void mutex_lock_io(struct mutex *lock);
  * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
  */
 extern int mutex_trylock(struct mutex *lock);
-extern void mutex_unlock(struct mutex *lock);
+extern void mutex_unlock(struct mutex *lock) __releases(lock);
 
 extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);

*groan*, I despise these sparse things.

The proposed patch only annotates a tiny part of the mutex interface, and will thereby generate a flood of new (pointless) warnings. Worse, annotating them all properly will require that __cond_lock() trainwreck.

#7 lore raw

Re: [PATCH 3/3] mutex: Add missing annotations

Thanks for the feedback, I did not have a thorough analyse.

I just realised it. Yes, some warnings generated after are pointless others look genuine. Next time I will have a thorough analyse and test before sending.

I apologise for this.

I really appreciate your feedback.

Kind regards,
Jules

Quoted 50 lines · Peter Zijlstra
Quoted 34 lines · Jules Irenge

Sparse reports false warnings and hide real warnings where mutex_lock() and mutex_unlock() are used within the kernel An example is within the kernel cgroup files
where the below warnings are found

|warning: context imbalance in cgroup_lock_and_drain_offline()
| - wrong count at exit
|warning: context imbalance in cgroup_procs_write_finish()
|- wrong count at exit
|warning: context imbalance in cgroup_procs_write_start()
|- wrong count at exit.

To fix these,
an __acquires(lock) is added to mutex_lock() declaration a __releases(lock) to mutex_unlock() declaration

---
 include/linux/mutex.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/mutex.h +1 -1
index aca8f36dfac9..a8ab4029913e 100644
@@ -162,7 +162,7 @@ do {									\
 } while (0)
 
 #else
-extern void mutex_lock(struct mutex *lock);
+extern void mutex_lock(struct mutex *lock) __acquires(lock);
 extern int __must_check mutex_lock_interruptible(struct mutex *lock);
 extern int __must_check mutex_lock_killable(struct mutex *lock);
 extern void mutex_lock_io(struct mutex *lock);

include/linux/mutex.h extern void mutex_lock_io(struct mutex *lock); · line 186

Quoted 7 lines
include/linux/mutex.h +1 -1
@@ -181,7 +181,7 @@ extern void mutex_lock_io(struct mutex *lock);
  * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
  */
 extern int mutex_trylock(struct mutex *lock);
-extern void mutex_unlock(struct mutex *lock);
+extern void mutex_unlock(struct mutex *lock) __releases(lock);
 
 extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);

*groan*, I despise these sparse things.

The proposed patch only annotates a tiny part of the mutex interface, and will thereby generate a flood of new (pointless) warnings. Worse, annotating them all properly will require that __cond_lock() trainwreck.