Skip to contentSkip to conversation

locking/lockdep: Add debug_locks check in __lock_downgrade()

PATCH In review Waiman Long 5 participants 8 messages Jan 10, 2019 – Feb 4, 2019 Locking primitives

lore
Share & tools
lore

Waiman Long

author reviewer
#0 lore raw

PATCH 1/1 locking/lockdep: Add debug_locks check in __lock_downgrade()

Tetsuo Handa had reported he saw an incorrect "downgrading a read lock" warning right after a previous lockdep warning. It is likely that the previous warning turned off lock debugging causing the lockdep to have inconsistency states leading to the lock downgrade warning.

Fix that by add a check for debug_locks at the beginning of __lock_downgrade().

---
 kernel/locking/lockdep.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/locking/lockdep.c +3 -0
index 9593233..e805fe3 100644
@@ -3535,6 +3535,9 @@ static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
 	unsigned int depth;
 	int i;
 
+	if (unlikely(!debug_locks))
+		return 0;
+
 	depth = curr->lockdep_depth;
 	/*
 	 * This function is about (re)setting the class of a held lock,
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 11 lines · Waiman Long

Tetsuo Handa had reported he saw an incorrect "downgrading a read lock" warning right after a previous lockdep warning. It is likely that the previous warning turned off lock debugging causing the lockdep to have inconsistency states leading to the lock downgrade warning.

Fix that by add a check for debug_locks at the beginning of __lock_downgrade().

Please also add:

Reported-by [email protected]

for tracking purposes. But Tetsuo deserves lots of credit for debugging it.

Quoted 9 lines
---
 kernel/locking/lockdep.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/locking/lockdep.c +0 -0
index 9593233..e805fe3 100644

kernel/locking/lockdep.c static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip) · line 3540

Quoted 6 lines
kernel/locking/lockdep.c +3 -0
@@ -3535,6 +3535,9 @@ static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
        unsigned int depth;
        int i;
 
+       if (unlikely(!debug_locks))
+               return 0;
+

Are we sure this resolves the problem rather than makes the inconsistency window smaller?
I don't understand all surrounding code, but looking just at this function it looks like it may just pepper over the problem. Say, we pass this check when lockdep was still turned on. Then this thread is preempted for some time (e.g. a virtual CPU), then another thread started reporting a warning, turned lockdep off, some information wasn't collected, and this this task resumes and reports a false warning.
Or we are holding the mutex here, and the fact that we are holding it ensures that no other task will take it and no information will be lost?
Quite a tricky moment, perhaps deserves a comment.

Quoted 6 lines
kernel/locking/lockdep.c +0 -1
        depth = curr->lockdep_depth;
        /*
         * This function is about (re)setting the class of a held lock,
--

1.8.3.1

#2 lore raw
Quoted 10 lines · Dmitry Vyukov

kernel/locking/lockdep.c static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip) · line 3540

Quoted 7 lines
kernel/locking/lockdep.c +3 -0
@@ -3535,6 +3535,9 @@ static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
        unsigned int depth;
        int i;
 
+       if (unlikely(!debug_locks))
+               return 0;
+

Are we sure this resolves the problem rather than makes the inconsistency window smaller?

As far as I know, this should resolve the problem.

Quoted 7 lines

I don't understand all surrounding code, but looking just at this function it looks like it may just pepper over the problem. Say, we pass this check when lockdep was still turned on. Then this thread is preempted for some time (e.g. a virtual CPU), then another thread started reporting a warning, turned lockdep off, some information wasn't collected, and this this task resumes and reports a false warning.

What this function checks is whether current thread is holding rw_semaphore for write. Since the information of held locks are per "struct task_struct" record, if lockdep is still enabled as of entry of this function, there must be a lockdep record that current thread is holding rw_semaphore for write if current thread is actually holding rw_semaphore for write. Therefore, preemption/interrupts can't erase the lockdep record that current thread is holding rw_semaphore, even if lockdep is turned off after passing this check.

Quoted 4 lines

Or we are holding the mutex here, and the fact that we are holding it ensures that no other task will take it and no information will be lost?
Quite a tricky moment, perhaps deserves a comment.

I think many other functions check debug_locks upon entry of functions.

Quoted 4 lines
Quoted 4 lines
kernel/locking/lockdep.c +0 -0
        depth = curr->lockdep_depth;
        /*
         * This function is about (re)setting the class of a held lock,
maintainer
#3 lore raw
In reply to Dmitry Vyukov
Quoted 10 lines · Dmitry Vyukov
Quoted 9 lines · Waiman Long

Tetsuo Handa had reported he saw an incorrect "downgrading a read lock" warning right after a previous lockdep warning. It is likely that the previous warning turned off lock debugging causing the lockdep to have inconsistency states leading to the lock downgrade warning.

Fix that by add a check for debug_locks at the beginning of __lock_downgrade().

Quoted 8 lines
Quoted 2 lines

Please also add:

Reported-by [email protected]

for tracking purposes. But Tetsuo deserves lots of credit for debugging it.

I made that:

Quoted 7 lines
Quoted 7 lines

index 9593233..e805fe3 100644

kernel/locking/lockdep.c +0 -0
@@ -3535,6 +3535,9 @@ static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
        unsigned int depth;
        int i;
 
Quoted 13 lines

kernel/locking/lockdep.c static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip) · line 3540

Quoted 3 lines
kernel/locking/lockdep.c +3 -0
+       if (unlikely(!debug_locks))
+               return 0;
+

Are we sure this resolves the problem rather than makes the inconsistency window smaller?
I don't understand all surrounding code, but looking just at this function it looks like it may just pepper over the problem. Say, we pass this check when lockdep was still turned on. Then this thread is preempted for some time (e.g. a virtual CPU), then another thread started reporting a warning, turned lockdep off, some information wasn't collected, and this this task resumes and reports a false warning.

Theoretically possible I suppose; but this is analogous to many of the other lockdep hooks.

Quoted 3 lines

Or we are holding the mutex here, and the fact that we are holding it ensures that no other task will take it and no information will be lost?

There is no lock here; for performance reasons we prefer not to acquire a global spinlock on every lockdep hook, that would be horrific.

#4 lore raw
Quoted 51 lines · Peter Zijlstra
Quoted 10 lines · Dmitry Vyukov
Quoted 9 lines · Waiman Long

Tetsuo Handa had reported he saw an incorrect "downgrading a read lock" warning right after a previous lockdep warning. It is likely that the previous warning turned off lock debugging causing the lockdep to have inconsistency states leading to the lock downgrade warning.

Fix that by add a check for debug_locks at the beginning of __lock_downgrade().

Quoted 8 lines
Quoted 2 lines

Please also add:

Reported-by [email protected]

for tracking purposes. But Tetsuo deserves lots of credit for debugging it.

I made that:

Quoted 7 lines
Quoted 7 lines

index 9593233..e805fe3 100644

kernel/locking/lockdep.c +0 -0
@@ -3535,6 +3535,9 @@ static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
        unsigned int depth;
        int i;
 
Quoted 13 lines

kernel/locking/lockdep.c static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip) · line 3540

Quoted 3 lines
kernel/locking/lockdep.c +3 -0
+       if (unlikely(!debug_locks))
+               return 0;
+

Are we sure this resolves the problem rather than makes the inconsistency window smaller?
I don't understand all surrounding code, but looking just at this function it looks like it may just pepper over the problem. Say, we pass this check when lockdep was still turned on. Then this thread is preempted for some time (e.g. a virtual CPU), then another thread started reporting a warning, turned lockdep off, some information wasn't collected, and this this task resumes and reports a false warning.

Theoretically possible I suppose; but this is analogous to many of the other lockdep hooks.

Quoted 6 lines
Quoted 3 lines

Or we are holding the mutex here, and the fact that we are holding it ensures that no other task will take it and no information will be lost?

There is no lock here; for performance reasons we prefer not to acquire a global spinlock on every lockdep hook, that would be horrific.

I mean the user mutex itself. Some invariants may hold while we are holding it as Tetsuo noted.

#5 lore raw
In reply to Peter Zijlstra

Peter, will this fix be sent to linux.git shortly? syzbot is generating many corrupted reports due to this race.

Quoted 25 lines · Peter Zijlstra
Quoted 10 lines · Dmitry Vyukov
Quoted 9 lines · Waiman Long

Tetsuo Handa had reported he saw an incorrect "downgrading a read lock" warning right after a previous lockdep warning. It is likely that the previous warning turned off lock debugging causing the lockdep to have inconsistency states leading to the lock downgrade warning.

Fix that by add a check for debug_locks at the beginning of __lock_downgrade().

Quoted 8 lines
Quoted 2 lines

Please also add:

Reported-by [email protected]

for tracking purposes. But Tetsuo deserves lots of credit for debugging it.

I made that:

#6 lore raw
In reply to Waiman Long
locking/lockdep: Add debug_locks check in __lock_downgrade()
Commit-ID:  71492580571467fb7177aade19c18ce7486267f5
Gitweb:     https://git.kernel.org/tip/71492580571467fb7177aade19c18ce7486267f5
Author:     Waiman Long <[email protected]>
AuthorDate: Wed, 9 Jan 2019 23:03:25 -0500
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 21 Jan 2019 11:18:51 +0100

locking/lockdep: Add debug_locks check in __lock_downgrade()

Tetsuo Handa had reported he saw an incorrect "downgrading a read lock" warning right after a previous lockdep warning. It is likely that the previous warning turned off lock debugging causing the lockdep to have inconsistency states leading to the lock downgrade warning.

Fix that by add a check for debug_locks at the beginning of __lock_downgrade().

---
 kernel/locking/lockdep.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/locking/lockdep.c +3 -0
index 95932333a48b..e805fe3bf87f 100644
@@ -3535,6 +3535,9 @@ static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
 	unsigned int depth;
 	int i;
 
+	if (unlikely(!debug_locks))
+		return 0;
+
 	depth = curr->lockdep_depth;
 	/*
 	 * This function is about (re)setting the class of a held lock,
#7 lore raw
In reply to Waiman Long
locking/lockdep: Add debug_locks check in __lock_downgrade()
Commit-ID:  513e1073d52e55b8024b4f238a48de7587c64ccf
Gitweb:     https://git.kernel.org/tip/513e1073d52e55b8024b4f238a48de7587c64ccf
Author:     Waiman Long <[email protected]>
AuthorDate: Wed, 9 Jan 2019 23:03:25 -0500
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 4 Feb 2019 09:03:27 +0100

locking/lockdep: Add debug_locks check in __lock_downgrade()

Tetsuo Handa had reported he saw an incorrect "downgrading a read lock" warning right after a previous lockdep warning. It is likely that the previous warning turned off lock debugging causing the lockdep to have inconsistency states leading to the lock downgrade warning.

Fix that by add a check for debug_locks at the beginning of __lock_downgrade().

---
 kernel/locking/lockdep.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/locking/lockdep.c +3 -0
index 608f74ed8bb9..7f7db23fc002 100644
@@ -3479,6 +3479,9 @@ __lock_set_class(struct lockdep_map *lock, const char *name,
 	unsigned int depth;
 	int i;
 
+	if (unlikely(!debug_locks))
+		return 0;
+
 	depth = curr->lockdep_depth;
 	/*
 	 * This function is about (re)setting the class of a held lock,