All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yuyang Du <duyuyang@gmail.com>
To: peterz@infradead.org, will.deacon@arm.com, mingo@kernel.org
Cc: bvanassche@acm.org, ming.lei@redhat.com, frederic@kernel.org,
	tglx@linutronix.de, linux-kernel@vger.kernel.org,
	Yuyang Du <duyuyang@gmail.com>
Subject: [PATCH v2 19/23] locking/lockdep: Refactorize check_noncircular and check_redundant
Date: Mon,  6 May 2019 16:19:35 +0800	[thread overview]
Message-ID: <20190506081939.74287-20-duyuyang@gmail.com> (raw)
In-Reply-To: <20190506081939.74287-1-duyuyang@gmail.com>

These two functions now handle different check results themselves. A new
check_path function is added to check whether there is a path in the
dependency graph. No functional change.

Signed-off-by: Yuyang Du <duyuyang@gmail.com>
---
 kernel/locking/lockdep.c | 118 +++++++++++++++++++++++++++++------------------
 1 file changed, 74 insertions(+), 44 deletions(-)

diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
index ebfa42a..2502ea4 100644
--- a/kernel/locking/lockdep.c
+++ b/kernel/locking/lockdep.c
@@ -1678,33 +1678,90 @@ unsigned long lockdep_count_backward_deps(struct lock_class *class)
 }
 
 /*
- * Prove that the dependency graph starting at <entry> can not
- * lead to <target>. Print an error and return 0 if it does.
+ * Check that the dependency graph starting at <src> can lead to
+ * <target> or not. Print an error and return 0 if it does.
  */
 static noinline int
-check_noncircular(struct lock_list *root, struct lock_class *target,
-		struct lock_list **target_entry)
+check_path(struct lock_class *target, struct lock_list *src_entry,
+	   struct lock_list **target_entry)
 {
-	int result;
+	int ret;
+
+	ret = __bfs_forwards(src_entry, (void *)target, class_equal,
+			     target_entry);
+
+	if (unlikely(ret < 0))
+		print_bfs_bug(ret);
+
+	return ret;
+}
+
+/*
+ * Prove that the dependency graph starting at <src> can not
+ * lead to <target>. If it can, there is a circle when adding
+ * <target> -> <src> dependency.
+ *
+ * Print an error and return 0 if it does.
+ */
+static noinline int
+check_noncircular(struct held_lock *src, struct held_lock *target,
+		  struct lock_trace *trace)
+{
+	int ret;
+	struct lock_list *uninitialized_var(target_entry);
+	struct lock_list src_entry = {
+		.class = hlock_class(src),
+		.parent = NULL,
+	};
 
 	debug_atomic_inc(nr_cyclic_checks);
 
-	result = __bfs_forwards(root, target, class_equal, target_entry);
+	ret = check_path(hlock_class(target), &src_entry, &target_entry);
 
-	return result;
+	if (unlikely(!ret)) {
+		if (!trace->nr_entries) {
+			/*
+			 * If save_trace fails here, the printing might
+			 * trigger a WARN but because of the !nr_entries it
+			 * should not do bad things.
+			 */
+			save_trace(trace);
+		}
+
+		print_circular_bug(&src_entry, target_entry, src, target);
+	}
+
+	return ret;
 }
 
+/*
+ * Check that the dependency graph starting at <src> can lead to
+ * <target> or not. If it can, <src> -> <target> dependency is already
+ * in the graph.
+ *
+ * Print an error and return 2 if it does or 1 if it does not.
+ */
 static noinline int
-check_redundant(struct lock_list *root, struct lock_class *target,
-		struct lock_list **target_entry)
+check_redundant(struct held_lock *src, struct held_lock *target)
 {
-	int result;
+	int ret;
+	struct lock_list *uninitialized_var(target_entry);
+	struct lock_list src_entry = {
+		.class = hlock_class(src),
+		.parent = NULL,
+	};
 
 	debug_atomic_inc(nr_redundant_checks);
 
-	result = __bfs_forwards(root, target, class_equal, target_entry);
+	ret = check_path(hlock_class(target), &src_entry, &target_entry);
 
-	return result;
+	if (!ret) {
+		debug_atomic_inc(nr_redundant);
+		ret = 2;
+	} else if (ret < 0)
+		ret = 0;
+
+	return ret;
 }
 
 #ifdef CONFIG_TRACE_IRQFLAGS
@@ -2302,9 +2359,7 @@ static inline void inc_chains(void)
 check_prev_add(struct task_struct *curr, struct held_lock *prev,
 	       struct held_lock *next, int distance, struct lock_trace *trace)
 {
-	struct lock_list *uninitialized_var(target_entry);
 	struct lock_list *entry;
-	struct lock_list this;
 	int ret;
 
 	if (!hlock_class(prev)->key || !hlock_class(next)->key) {
@@ -2335,25 +2390,9 @@ static inline void inc_chains(void)
 	 * MAX_CIRCULAR_QUEUE_SIZE) which keeps track of a breadth of nodes
 	 * in the graph whose neighbours are to be checked.
 	 */
-	this.class = hlock_class(next);
-	this.parent = NULL;
-	ret = check_noncircular(&this, hlock_class(prev), &target_entry);
-	if (unlikely(!ret)) {
-		if (!trace->nr_entries) {
-			/*
-			 * If save_trace fails here, the printing might
-			 * trigger a WARN but because of the !nr_entries it
-			 * should not do bad things.
-			 */
-			save_trace(trace);
-		}
-		print_circular_bug(&this, target_entry, next, prev);
+	ret = check_noncircular(next, prev, trace);
+	if (unlikely(ret <= 0))
 		return 0;
-	}
-	else if (unlikely(ret < 0)) {
-		print_bfs_bug(ret);
-		return 0;
-	}
 
 	if (!check_irq_usage(curr, prev, next))
 		return 0;
@@ -2387,18 +2426,9 @@ static inline void inc_chains(void)
 	/*
 	 * Is the <prev> -> <next> link redundant?
 	 */
-	this.class = hlock_class(prev);
-	this.parent = NULL;
-	ret = check_redundant(&this, hlock_class(next), &target_entry);
-	if (!ret) {
-		debug_atomic_inc(nr_redundant);
-		return 2;
-	}
-	if (ret < 0) {
-		print_bfs_bug(ret);
-		return 0;
-	}
-
+	ret = check_redundant(prev, next);
+	if (ret != 1)
+		return ret;
 
 	if (!trace->nr_entries && !save_trace(trace))
 		return 0;
-- 
1.8.3.1


  parent reply	other threads:[~2019-05-06  8:21 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-06  8:19 [PATCH v2 00/23] locking/lockdep: Small improvements Yuyang Du
2019-05-06  8:19 ` [PATCH v2 01/23] locking/lockdep: Change all print_*() return type to void Yuyang Du
2019-06-03 13:06   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 02/23] locking/lockdep: Add description and explanation in lockdep design doc Yuyang Du
2019-06-03 13:07   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 03/23] locking/lockdep: Adjust lock usage bit character checks Yuyang Du
2019-06-03 13:07   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 04/23] locking/lockdep: Remove useless conditional macro Yuyang Du
2019-06-03 13:08   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 05/23] locking/lockdep: Print the right depth for chain key colission Yuyang Du
2019-06-03 13:09   ` [tip:locking/core] locking/lockdep: Print the right depth for chain key collision tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 06/23] locking/lockdep: Update obsolete struct field description Yuyang Du
2019-06-03 13:09   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 07/23] locking/lockdep: Use lockdep_init_task for task initiation consistently Yuyang Du
2019-06-03 13:10   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 08/23] locking/lockdep: Define INITIAL_CHAIN_KEY for chain keys to start with Yuyang Du
2019-06-03 13:11   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 09/23] locking/lockdep: Change the range of class_idx in held_lock struct Yuyang Du
2019-06-03 13:12   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 10/23] locking/lockdep: Remove unused argument in validate_chain() and check_deadlock() Yuyang Du
2019-06-03 13:12   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 11/23] locking/lockdep: Update comment Yuyang Du
2019-06-03 13:13   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 12/23] locking/lockdep: Change type of the element field in circular_queue Yuyang Du
2019-06-03 13:14   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 13/23] locking/lockdep: Change the return type of __cq_dequeue() Yuyang Du
2019-06-03 13:15   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 14/23] locking/lockdep: Avoid constant checks in __bfs by using offset reference Yuyang Du
2019-06-03 13:15   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 15/23] locking/lockdep: Update comments on dependency search Yuyang Du
2019-06-03 13:16   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 16/23] locking/lockdep: Add explanation to lock usage rules in lockdep design doc Yuyang Du
2019-06-03 13:17   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 17/23] locking/lockdep: Remove redundant argument in check_deadlock Yuyang Du
2019-06-03 13:18   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 18/23] locking/lockdep: Remove unused argument in __lock_release Yuyang Du
2019-06-03 13:18   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` Yuyang Du [this message]
2019-06-03 13:19   ` [tip:locking/core] locking/lockdep: Refactorize check_noncircular and check_redundant tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 20/23] locking/lockdep: Check redundant dependency only when CONFIG_LOCKDEP_SMALL Yuyang Du
2019-06-03 13:20   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 21/23] locking/lockdep: Consolidate lock usage bit initialization Yuyang Du
2019-06-03 13:20   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 22/23] locking/lockdep: Adjust new bit cases in mark_lock Yuyang Du
2019-06-03 13:21   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-06  8:19 ` [PATCH v2 23/23] locking/lockdep: Remove !dir in lock irq usage check Yuyang Du
2019-06-03 13:22   ` [tip:locking/core] " tip-bot for Yuyang Du
2019-05-08  8:55 ` [PATCH v2 00/23] locking/lockdep: Small improvements Peter Zijlstra
2019-05-09  0:50   ` Yuyang Du

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190506081939.74287-20-duyuyang@gmail.com \
    --to=duyuyang@gmail.com \
    --cc=bvanassche@acm.org \
    --cc=frederic@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ming.lei@redhat.com \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=will.deacon@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.