Skip to contentSkip to conversation

some improvements for lockdep

PATCH Superseded Xiongwei Song 3 participants 6 messages Jun 17, 2021 – Jun 18, 2021 Locking primitives

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover some improvements for lockdep

Unlikely the checks of return values of graph walk will improve the performance to some degree, patch 1 and patch 2 are for this.

The patch 3 will print a warning after counting lock deps when hitting bfs errors.

Xiongwei Song (3):
locking/lockdep: unlikely bfs_error function locking/lockdep: unlikely conditons about BFS_RMATCH locking/lockdep: print possible warning after counting deps

 kernel/locking/lockdep.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/3 locking/lockdep: unlikely bfs_error() inside

From: Xiongwei Song <[email protected]>

The error from graph walk is small probability event, and there are some bfs_error calls during lockdep detection, so unlikely bfs_error inside can improve performance a little bit.

---
 kernel/locking/lockdep.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/locking/lockdep.c +3 -3
index 7641bd407239..a8a66a2a9bc1 100644
@@ -1540,7 +1540,7 @@ enum bfs_result {
  */
 static inline bool bfs_error(enum bfs_result res)
 {
-	return res < 0;
+	return unlikely(res < 0);
 }
 
 /*
@@ -2089,7 +2089,7 @@ check_path(struct held_lock *target, struct lock_list *src_entry,
 
 	ret = __bfs_forwards(src_entry, target, match, skip, target_entry);
 
-	if (unlikely(bfs_error(ret)))
+	if (bfs_error(ret))
 		print_bfs_bug(ret);
 
 	return ret;
@@ -2936,7 +2936,7 @@ check_prev_add(struct task_struct *curr, struct held_lock *prev,
 	 * in the graph whose neighbours are to be checked.
 	 */
 	ret = check_noncircular(next, prev, trace);
-	if (unlikely(bfs_error(ret) || ret == BFS_RMATCH))
+	if (bfs_error(ret) || unlikely(ret == BFS_RMATCH))
 		return 0;
 
 	if (!check_irq_usage(curr, prev, next))
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/3 locking/lockdep: unlikely conditons about BFS_RMATCH

In reply to Xiongwei Song

From: Xiongwei Song <[email protected]>

The probability that graph walk will return BFS_RMATCH is slim, so unlikey conditons about BFS_RMATCH can improve performance a little bit.

---
 kernel/locking/lockdep.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/locking/lockdep.c +2 -2
index a8a66a2a9bc1..cb94097014d8 100644
@@ -2750,7 +2750,7 @@ check_redundant(struct held_lock *src, struct held_lock *target)
 	 */
 	ret = check_path(target, &src_entry, hlock_equal, usage_skip, &target_entry);
 
-	if (ret == BFS_RMATCH)
+	if (unlikely(ret == BFS_RMATCH))
 		debug_atomic_inc(nr_redundant);
 
 	return ret;
@@ -2992,7 +2992,7 @@ check_prev_add(struct task_struct *curr, struct held_lock *prev,
 	ret = check_redundant(prev, next);
 	if (bfs_error(ret))
 		return 0;
-	else if (ret == BFS_RMATCH)
+	else if (unlikely(ret == BFS_RMATCH))
 		return 2;
 
 	if (!*trace) {
Signature · 1 line
2.30.2
#3 lore raw

PATCH 3/3 locking/lockdep: print possible warning after counting deps

In reply to Xiongwei Song

From: Xiongwei Song <[email protected]>

The graph walk might hit error when counting dependencies. Once the return value is negative, print a warning to reminder users.

---
 kernel/locking/lockdep.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
kernel/locking/lockdep.c +10 -2
index cb94097014d8..cfe0f4374594 100644
@@ -2028,8 +2028,12 @@ static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
 {
 	unsigned long  count = 0;
 	struct lock_list *target_entry;
+	enum bfs_result ret;
+
+	ret = __bfs_forwards(this, (void *)&count, noop_count, NULL, &target_entry);
 
-	__bfs_forwards(this, (void *)&count, noop_count, NULL, &target_entry);
+	if (bfs_error(ret))
+		print_bfs_bug(ret);
 
 	return count;
 }
@@ -2053,8 +2057,12 @@ static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
 {
 	unsigned long  count = 0;
 	struct lock_list *target_entry;
+	enum bfs_result ret;
+
+	ret = __bfs_backwards(this, (void *)&count, noop_count, NULL, &target_entry);
 
-	__bfs_backwards(this, (void *)&count, noop_count, NULL, &target_entry);
+	if (bfs_error(ret))
+		print_bfs_bug(ret);
 
 	return count;
 }
Signature · 1 line
2.30.2
#4 lore raw

Re: [PATCH 3/3] locking/lockdep: print possible warning after counting deps

Hi,

Quoted 5 lines · Xiongwei Song

From: Xiongwei Song <[email protected]>

The graph walk might hit error when counting dependencies. Once the return value is negative, print a warning to reminder users.

Thanks for the improvement, but please see below:

Quoted 11 lines
---
 kernel/locking/lockdep.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
kernel/locking/lockdep.c +0 -0
index cb94097014d8..cfe0f4374594 100644

kernel/locking/lockdep.c static unsigned long __lockdep_count_forward_deps(struct lock_list *this) · line 2036

Quoted 10 lines
kernel/locking/lockdep.c +5 -1
@@ -2028,8 +2028,12 @@ static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
 {
 	unsigned long  count = 0;
 	struct lock_list *target_entry;
+	enum bfs_result ret;
+
+	ret = __bfs_forwards(this, (void *)&count, noop_count, NULL, &target_entry);
 
-	__bfs_forwards(this, (void *)&count, noop_count, NULL, &target_entry);
+	if (bfs_error(ret))
+		print_bfs_bug(ret);

Here print_bfs_bug() will eventually call debug_locks_off_graph_unlock() to release the graph lock, and the caller (lockdep_count_fowards_deps()) will also call graph_unlock() afterwards, and that means we unlock *twice* if a BFS error happens... although in that case, lockdep should stop working so messing up with the graph lock may not hurt anything, but still, I don't think we want to do that.

So probably you can open-code __lockdep_count_forward_deps() into lockdep_count_forwards_deps(), and call print_bfs_bug() or graph_unlock() accordingly. The body of __lockdep_count_forward_deps() is really small, so I think it's OK to open-code it into its caller.

Regards,
Boqun

Quoted 20 lines
kernel/locking/lockdep.c +5 -1
 	return count;
 }
@@ -2053,8 +2057,12 @@ static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
 {
 	unsigned long  count = 0;
 	struct lock_list *target_entry;
+	enum bfs_result ret;
+
+	ret = __bfs_backwards(this, (void *)&count, noop_count, NULL, &target_entry);
 
-	__bfs_backwards(this, (void *)&count, noop_count, NULL, &target_entry);
+	if (bfs_error(ret))
+		print_bfs_bug(ret);
 
 	return count;
 }
Signature · 1 line
2.30.2
#5 lore raw

Re: [PATCH 3/3] locking/lockdep: print possible warning after counting deps

Quoted 41 lines · Boqun Feng

Hi,

Quoted 5 lines · Xiongwei Song

From: Xiongwei Song <[email protected]>

The graph walk might hit error when counting dependencies. Once the return value is negative, print a warning to reminder users.

Thanks for the improvement, but please see below:

Quoted 11 lines
---
 kernel/locking/lockdep.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
kernel/locking/lockdep.c +0 -0
index cb94097014d8..cfe0f4374594 100644

kernel/locking/lockdep.c static unsigned long __lockdep_count_forward_deps(struct lock_list *this) · line 2036

Quoted 10 lines
kernel/locking/lockdep.c +5 -1
@@ -2028,8 +2028,12 @@ static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
 {
      unsigned long  count = 0;
      struct lock_list *target_entry;
+     enum bfs_result ret;
+
+     ret = __bfs_forwards(this, (void *)&count, noop_count, NULL, &target_entry);
 
-     __bfs_forwards(this, (void *)&count, noop_count, NULL, &target_entry);
+     if (bfs_error(ret))
+             print_bfs_bug(ret);

Here print_bfs_bug() will eventually call debug_locks_off_graph_unlock() to release the graph lock, and the caller (lockdep_count_fowards_deps()) will also call graph_unlock() afterwards, and that means we unlock *twice* if a BFS error happens... although in that case, lockdep should stop working so messing up with the graph lock may not hurt anything, but still, I don't think we want to do that.

Quoted 4 lines

So probably you can open-code __lockdep_count_forward_deps() into lockdep_count_forwards_deps(), and call print_bfs_bug() or graph_unlock() accordingly. The body of __lockdep_count_forward_deps() is really small, so I think it's OK to open-code it into its caller.

Thank you so much for the detailed comments. Let me improve and update the patch.

Regards,
Xiongwei

Quoted 24 lines

Regards,
Boqun

Quoted 20 lines
kernel/locking/lockdep.c +5 -2
      return count;
 }
@@ -2053,8 +2057,12 @@ static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
 {
      unsigned long  count = 0;
      struct lock_list *target_entry;
+     enum bfs_result ret;
+
+     ret = __bfs_backwards(this, (void *)&count, noop_count, NULL, &target_entry);
 
-     __bfs_backwards(this, (void *)&count, noop_count, NULL, &target_entry);
+     if (bfs_error(ret))
+             print_bfs_bug(ret);
 
      return count;
 }
--

2.30.2