Skip to contentSkip to conversation

Lockdep: Reduce stack trace memory usage

PATCH In review Bart Van Assche 4 participants 12 messages Jul 22, 2019 – Jul 25, 2019 Locking primitives

lore
Share & tools
lore
#0 lore raw

cover Lockdep: Reduce stack trace memory usage

Hi Peter,

An unfortunate side effect of commit 669de8bda87b ("kernel/workqueue: Use dynamic lockdep keys for workqueues") is that all stack traces associated with the lockdep key are leaked when a workqueue is destroyed. Fix this by storing each unique stack trace once. Please consider this patch series for Linux kernel v5.4.

Thanks,

Bart.

Bart Van Assche (4):
locking/lockdep: Make it clear that what lock_class::key points at is
not modified
stacktrace: Constify 'entries' arguments
locking/lockdep: Reduce space occupied by stack traces locking/lockdep: Report more stack trace statistics

 include/linux/lockdep.h            |  11 +-
 include/linux/stacktrace.h         |   4 +-
 kernel/locking/lockdep.c           | 159 ++++++++++++++++++++++-------
 kernel/locking/lockdep_internals.h |   9 +-
 kernel/locking/lockdep_proc.c      |   8 +-
 kernel/stacktrace.c                |   4 +-
 6 files changed, 143 insertions(+), 52 deletions(-)
Signature · 1 line
2.22.0.657.g960e92d24f-goog
#1 lore raw

PATCH 1/4 locking/lockdep: Make it clear that what lock_class::key points at is not modified

This patch does not change the behavior of the lockdep code.

Cc Thomas Gleixner <[email protected]>
Cc Will Deacon <[email protected]>
Cc Waiman Long <[email protected]>
---
 include/linux/lockdep.h            | 2 +-
 kernel/locking/lockdep.c           | 2 +-
 kernel/locking/lockdep_internals.h | 3 ++-
 kernel/locking/lockdep_proc.c      | 2 +-
 4 files changed, 5 insertions(+), 4 deletions(-)
include/linux/lockdep.h +1 -1
index 0b0d7259276d..cdb3c2f06092 100644
@@ -97,7 +97,7 @@ struct lock_class {
 	 */
 	struct list_head		locks_after, locks_before;
 
-	struct lockdep_subclass_key	*key;
+	const struct lockdep_subclass_key *key;
 	unsigned int			subclass;
 	unsigned int			dep_gen_id;
 
kernel/locking/lockdep.c +1 -1
index 341f52117f88..f4127c5a495d 100644
@@ -511,7 +511,7 @@ static const char *usage_str[] =
 };
 #endif
 
-const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
+const char *__get_key_name(const struct lockdep_subclass_key *key, char *str)
 {
 	return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
 }
kernel/locking/lockdep_internals.h +2 -1
index cc83568d5012..2e518369add4 100644
@@ -116,7 +116,8 @@ extern struct lock_chain lock_chains[];
 extern void get_usage_chars(struct lock_class *class,
 			    char usage[LOCK_USAGE_CHARS]);
 
-extern const char * __get_key_name(struct lockdep_subclass_key *key, char *str);
+extern const char *__get_key_name(const struct lockdep_subclass_key *key,
+				  char *str);
 
 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i);
 
kernel/locking/lockdep_proc.c +1 -1
index 65b6a1600c8f..1ba5780182e7 100644
@@ -398,7 +398,7 @@ static void seq_lock_time(struct seq_file *m, struct lock_time *lt)
 
 static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
 {
-	struct lockdep_subclass_key *ckey;
+	const struct lockdep_subclass_key *ckey;
 	struct lock_class_stats *stats;
 	struct lock_class *class;
 	const char *cname;
Signature · 1 line
2.22.0.657.g960e92d24f-goog
#2 lore raw

[tip:locking/core] locking/lockdep: Make it clear that what lock_class::key points at is not modified

locking/lockdep: Make it clear that what lock_class::key points at is not modified
Commit-ID:  364f6afc4f5537b79cf454eb35cae92920676075
Gitweb:     https://git.kernel.org/tip/364f6afc4f5537b79cf454eb35cae92920676075
Author:     Bart Van Assche <[email protected]>
AuthorDate: Mon, 22 Jul 2019 11:24:40 -0700
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 25 Jul 2019 15:43:26 +0200

locking/lockdep: Make it clear that what lock_class::key points at is not modified

This patch does not change the behavior of the lockdep code.

---
 include/linux/lockdep.h            | 2 +-
 kernel/locking/lockdep.c           | 2 +-
 kernel/locking/lockdep_internals.h | 3 ++-
 kernel/locking/lockdep_proc.c      | 2 +-
 4 files changed, 5 insertions(+), 4 deletions(-)
include/linux/lockdep.h +1 -1
index 0b0d7259276d..cdb3c2f06092 100644
@@ -97,7 +97,7 @@ struct lock_class {
 	 */
 	struct list_head		locks_after, locks_before;
 
-	struct lockdep_subclass_key	*key;
+	const struct lockdep_subclass_key *key;
 	unsigned int			subclass;
 	unsigned int			dep_gen_id;
 
kernel/locking/lockdep.c +1 -1
index 4861cf8e274b..af6627866191 100644
@@ -511,7 +511,7 @@ static const char *usage_str[] =
 };
 #endif
 
-const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
+const char *__get_key_name(const struct lockdep_subclass_key *key, char *str)
 {
 	return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
 }
kernel/locking/lockdep_internals.h +2 -1
index cc83568d5012..2e518369add4 100644
@@ -116,7 +116,8 @@ extern struct lock_chain lock_chains[];
 extern void get_usage_chars(struct lock_class *class,
 			    char usage[LOCK_USAGE_CHARS]);
 
-extern const char * __get_key_name(struct lockdep_subclass_key *key, char *str);
+extern const char *__get_key_name(const struct lockdep_subclass_key *key,
+				  char *str);
 
 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i);
 
kernel/locking/lockdep_proc.c +1 -1
index bda006f8a88b..ed9842425cac 100644
@@ -399,7 +399,7 @@ static void seq_lock_time(struct seq_file *m, struct lock_time *lt)
 
 static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
 {
-	struct lockdep_subclass_key *ckey;
+	const struct lockdep_subclass_key *ckey;
 	struct lock_class_stats *stats;
 	struct lock_class *class;
 	const char *cname;
#3 lore raw

PATCH 2/4 stacktrace: Constify 'entries' arguments

In reply to Bart Van Assche

Make it clear to humans and to the compiler that the stack trace ('entries') arguments are not modified.

Cc Thomas Gleixner <[email protected]>
Cc Will Deacon <[email protected]>
Cc Waiman Long <[email protected]>
---
 include/linux/stacktrace.h | 4 ++--
 kernel/stacktrace.c        | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
include/linux/stacktrace.h +2 -2
index f0cfd12cb45e..83bd8cb475d7 100644
@@ -9,9 +9,9 @@ struct task_struct;
 struct pt_regs;
 
 #ifdef CONFIG_STACKTRACE
-void stack_trace_print(unsigned long *trace, unsigned int nr_entries,
+void stack_trace_print(const unsigned long *trace, unsigned int nr_entries,
 		       int spaces);
-int stack_trace_snprint(char *buf, size_t size, unsigned long *entries,
+int stack_trace_snprint(char *buf, size_t size, const unsigned long *entries,
 			unsigned int nr_entries, int spaces);
 unsigned int stack_trace_save(unsigned long *store, unsigned int size,
 			      unsigned int skipnr);
kernel/stacktrace.c +2 -2
index f5440abb7532..6d1f68b7e528 100644
@@ -20,7 +20,7 @@
  * @nr_entries:	Number of entries in the storage array
  * @spaces:	Number of leading spaces to print
  */
-void stack_trace_print(unsigned long *entries, unsigned int nr_entries,
+void stack_trace_print(const unsigned long *entries, unsigned int nr_entries,
 		       int spaces)
 {
 	unsigned int i;
@@ -43,7 +43,7 @@ EXPORT_SYMBOL_GPL(stack_trace_print);
  *
  * Return: Number of bytes printed.
  */
-int stack_trace_snprint(char *buf, size_t size, unsigned long *entries,
+int stack_trace_snprint(char *buf, size_t size, const unsigned long *entries,
 			unsigned int nr_entries, int spaces)
 {
 	unsigned int generated, i, total = 0;
Signature · 1 line
2.22.0.657.g960e92d24f-goog
#4 lore raw

[tip:locking/core] stacktrace: Constify 'entries' arguments

stacktrace: Constify 'entries' arguments
Commit-ID:  a2970421640bd9b6a78f2685d7750a791abdfd4e
Gitweb:     https://git.kernel.org/tip/a2970421640bd9b6a78f2685d7750a791abdfd4e
Author:     Bart Van Assche <[email protected]>
AuthorDate: Mon, 22 Jul 2019 11:24:41 -0700
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 25 Jul 2019 15:43:26 +0200

stacktrace: Constify 'entries' arguments

Make it clear to humans and to the compiler that the stack trace ('entries') arguments are not modified.

---
 include/linux/stacktrace.h | 4 ++--
 kernel/stacktrace.c        | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
include/linux/stacktrace.h +2 -2
index f0cfd12cb45e..83bd8cb475d7 100644
@@ -9,9 +9,9 @@ struct task_struct;
 struct pt_regs;
 
 #ifdef CONFIG_STACKTRACE
-void stack_trace_print(unsigned long *trace, unsigned int nr_entries,
+void stack_trace_print(const unsigned long *trace, unsigned int nr_entries,
 		       int spaces);
-int stack_trace_snprint(char *buf, size_t size, unsigned long *entries,
+int stack_trace_snprint(char *buf, size_t size, const unsigned long *entries,
 			unsigned int nr_entries, int spaces);
 unsigned int stack_trace_save(unsigned long *store, unsigned int size,
 			      unsigned int skipnr);
kernel/stacktrace.c +2 -2
index f5440abb7532..6d1f68b7e528 100644
@@ -20,7 +20,7 @@
  * @nr_entries:	Number of entries in the storage array
  * @spaces:	Number of leading spaces to print
  */
-void stack_trace_print(unsigned long *entries, unsigned int nr_entries,
+void stack_trace_print(const unsigned long *entries, unsigned int nr_entries,
 		       int spaces)
 {
 	unsigned int i;
@@ -43,7 +43,7 @@ EXPORT_SYMBOL_GPL(stack_trace_print);
  *
  * Return: Number of bytes printed.
  */
-int stack_trace_snprint(char *buf, size_t size, unsigned long *entries,
+int stack_trace_snprint(char *buf, size_t size, const unsigned long *entries,
 			unsigned int nr_entries, int spaces)
 {
 	unsigned int generated, i, total = 0;
#5 lore raw

PATCH 3/4 locking/lockdep: Reduce space occupied by stack traces

In reply to Bart Van Assche

Although commit 669de8bda87b ("kernel/workqueue: Use dynamic lockdep keys for workqueues") unregisters dynamic lockdep keys when a workqueue is destroyed, a side effect of that commit is that all stack traces associated with the lockdep key are leaked when a workqueue is destroyed. Fix this by storing each unique stack trace once. Other changes in this patch are:
- Use NULL instead of { .nr_entries = 0 } to represent 'no trace'.
- Store a pointer to a stack trace in struct lock_class and struct lock_list instead of storing 'nr_entries' and 'offset'.

This patch avoids that the following program triggers the "BUG:
MAX_STACK_TRACE_ENTRIES too low!" complaint:

	#include <fcntl.h>
	#include <unistd.h>

	int main()
	{
		for (;;) {
			int fd = open("/dev/infiniband/rdma_cm", O_RDWR);
			close(fd);
		}
	}
---
 include/linux/lockdep.h            |   9 +-
 kernel/locking/lockdep.c           | 128 ++++++++++++++++++++---------
 kernel/locking/lockdep_internals.h |   2 +
 3 files changed, 95 insertions(+), 44 deletions(-)
include/linux/lockdep.h +3 -6
index cdb3c2f06092..b8a835fd611b 100644
@@ -66,10 +66,7 @@ struct lock_class_key {
 
 extern struct lock_class_key __lockdep_no_validate__;
 
-struct lock_trace {
-	unsigned int		nr_entries;
-	unsigned int		offset;
-};
+struct lock_trace;
 
 #define LOCKSTAT_POINTS		4
 
@@ -105,7 +102,7 @@ struct lock_class {
 	 * IRQ/softirq usage tracking bits:
 	 */
 	unsigned long			usage_mask;
-	struct lock_trace		usage_traces[XXX_LOCK_USAGE_STATES];
+	const struct lock_trace		*usage_traces[XXX_LOCK_USAGE_STATES];
 
 	/*
 	 * Generation counter, when doing certain classes of graph walking,
@@ -193,7 +190,7 @@ struct lock_list {
 	struct list_head		entry;
 	struct lock_class		*class;
 	struct lock_class		*links_to;
-	struct lock_trace		trace;
+	const struct lock_trace		*trace;
 	int				distance;
 
 	/*
kernel/locking/lockdep.c +90 -38

286 lines not on this page · raw message

kernel/locking/lockdep_internals.h +2 -0
index 2e518369add4..93a008bf77db 100644
@@ -92,6 +92,7 @@ static const unsigned long LOCKF_USED_IN_IRQ_READ =
 #define MAX_LOCKDEP_ENTRIES	16384UL
 #define MAX_LOCKDEP_CHAINS_BITS	15
 #define MAX_STACK_TRACE_ENTRIES	262144UL
+#define STACK_TRACE_HASH_SIZE	8192
 #else
 #define MAX_LOCKDEP_ENTRIES	32768UL
 
@@ -102,6 +103,7 @@ static const unsigned long LOCKF_USED_IN_IRQ_READ =
  * addresses. Protected by the hash_lock.
  */
 #define MAX_STACK_TRACE_ENTRIES	524288UL
+#define STACK_TRACE_HASH_SIZE	16384
 #endif
 
 #define MAX_LOCKDEP_CHAINS	(1UL << MAX_LOCKDEP_CHAINS_BITS)
Signature · 1 line
2.22.0.657.g960e92d24f-goog
#6 lore raw

Re: [PATCH 3/4] locking/lockdep: Reduce space occupied by stack traces

Quoted 10 lines · Bart Van Assche

Although commit 669de8bda87b ("kernel/workqueue: Use dynamic lockdep keys for workqueues") unregisters dynamic lockdep keys when a workqueue is destroyed, a side effect of that commit is that all stack traces associated with the lockdep key are leaked when a workqueue is destroyed. Fix this by storing each unique stack trace once. Other changes in this patch are:
- Use NULL instead of { .nr_entries = 0 } to represent 'no trace'.
- Store a pointer to a stack trace in struct lock_class and struct lock_list instead of storing 'nr_entries' and 'offset'.

Quoted 2 lines

This patch avoids that the following program triggers the "BUG:
MAX_STACK_TRACE_ENTRIES too low!" complaint:

Does this also fix any of the other bugs listed at
https://lore.kernel.org/lkml/[email protected]/
?

BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!
BUG: MAX_LOCKDEP_CHAINS too low!
BUG: MAX_LOCK_DEPTH too low! (2)
BUG: MAX_LOCKDEP_ENTRIES too low!
Quoted 12 lines
	#include <fcntl.h>
	#include <unistd.h>

	int main()
	{
		for (;;) {
			int fd = open("/dev/infiniband/rdma_cm", O_RDWR);
			close(fd);
		}
	}
Quoted 6 lines
Cc Ingo Molnar <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Will Deacon <[email protected]>
Cc Yuyang Du <[email protected]>
Cc Waiman Long <[email protected]>

Can you please add:

Reported-by [email protected]

Thanks,

- Eric

#7 lore raw

Re: [PATCH 3/4] locking/lockdep: Reduce space occupied by stack traces

Quoted 8 lines · Eric Biggers

Does this also fix any of the other bugs listed at
https://lore.kernel.org/lkml/[email protected]/
?

BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!
BUG: MAX_LOCKDEP_CHAINS too low!
BUG: MAX_LOCK_DEPTH too low! (2)
BUG: MAX_LOCKDEP_ENTRIES too low!

Hi Eric,

I don't think so. This patch only affects the space occupied by stack traces and not the space occupied by any of the other lockdep data strutures. BTW, in that report I found the following:

Title:              BUG: MAX_LOCKDEP_CHAINS too low!
Last occurred:      5 days ago
Reported:           284 days ago
Title:              BUG: MAX_LOCK_DEPTH too low! (2)
Last occurred:      362 days ago
Reported:           392 days ago

Since these bugs were reported more than 150 days ago these bugs are older than my lockdep changes and hence not a regression due to my lockdep changes.

My patch series did not increase the number of "list_entries" tracked by lockdep so I don't think that the "BUG: MAX_LOCKDEP_ENTRIES too low" behavior can be triggered more easily due to my lockdep changes.

The "BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!" complaint however may be related. I will check whether it is possible to reduce the space occupied by held lock chains again to what was needed before my patch series.

Bart.

#8 lore raw

[tip:locking/core] locking/lockdep: Reduce space occupied by stack traces

In reply to Bart Van Assche
locking/lockdep: Reduce space occupied by stack traces
Commit-ID:  12593b7467f9130b64a6d4b6a26ed4ec217b6784
Gitweb:     https://git.kernel.org/tip/12593b7467f9130b64a6d4b6a26ed4ec217b6784
Author:     Bart Van Assche <[email protected]>
AuthorDate: Mon, 22 Jul 2019 11:24:42 -0700
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 25 Jul 2019 15:43:27 +0200

locking/lockdep: Reduce space occupied by stack traces

Although commit 669de8bda87b ("kernel/workqueue: Use dynamic lockdep keys for workqueues") unregisters dynamic lockdep keys when a workqueue is destroyed, a side effect of that commit is that all stack traces associated with the lockdep key are leaked when a workqueue is destroyed. Fix this by storing each unique stack trace once. Other changes in this patch are:

- Use NULL instead of { .nr_entries = 0 } to represent 'no trace'.
- Store a pointer to a stack trace in struct lock_class and struct lock_list instead of storing 'nr_entries' and 'offset'.

This patch avoids that the following program triggers the "BUG:
MAX_STACK_TRACE_ENTRIES too low!" complaint:

	#include <fcntl.h>
	#include <unistd.h>

	int main()
	{
		for (;;) {
			int fd = open("/dev/infiniband/rdma_cm", O_RDWR);
			close(fd);
		}
	}
---
 include/linux/lockdep.h            |   9 +--
 kernel/locking/lockdep.c           | 128 ++++++++++++++++++++++++++-----------
 kernel/locking/lockdep_internals.h |   2 +
 3 files changed, 95 insertions(+), 44 deletions(-)
include/linux/lockdep.h +3 -6
index cdb3c2f06092..b8a835fd611b 100644
@@ -66,10 +66,7 @@ struct lock_class_key {
 
 extern struct lock_class_key __lockdep_no_validate__;
 
-struct lock_trace {
-	unsigned int		nr_entries;
-	unsigned int		offset;
-};
+struct lock_trace;
 
 #define LOCKSTAT_POINTS		4
 
@@ -105,7 +102,7 @@ struct lock_class {
 	 * IRQ/softirq usage tracking bits:
 	 */
 	unsigned long			usage_mask;
-	struct lock_trace		usage_traces[XXX_LOCK_USAGE_STATES];
+	const struct lock_trace		*usage_traces[XXX_LOCK_USAGE_STATES];
 
 	/*
 	 * Generation counter, when doing certain classes of graph walking,
@@ -193,7 +190,7 @@ struct lock_list {
 	struct list_head		entry;
 	struct lock_class		*class;
 	struct lock_class		*links_to;
-	struct lock_trace		trace;
+	const struct lock_trace		*trace;
 	int				distance;
 
 	/*
kernel/locking/lockdep.c +90 -38

286 lines not on this page · raw message

kernel/locking/lockdep_internals.h +2 -0
index 2e518369add4..93a008bf77db 100644
@@ -92,6 +92,7 @@ static const unsigned long LOCKF_USED_IN_IRQ_READ =
 #define MAX_LOCKDEP_ENTRIES	16384UL
 #define MAX_LOCKDEP_CHAINS_BITS	15
 #define MAX_STACK_TRACE_ENTRIES	262144UL
+#define STACK_TRACE_HASH_SIZE	8192
 #else
 #define MAX_LOCKDEP_ENTRIES	32768UL
 
@@ -102,6 +103,7 @@ static const unsigned long LOCKF_USED_IN_IRQ_READ =
  * addresses. Protected by the hash_lock.
  */
 #define MAX_STACK_TRACE_ENTRIES	524288UL
+#define STACK_TRACE_HASH_SIZE	16384
 #endif
 
 #define MAX_LOCKDEP_CHAINS	(1UL << MAX_LOCKDEP_CHAINS_BITS)
#9 lore raw

PATCH 4/4 locking/lockdep: Report more stack trace statistics

In reply to Bart Van Assche

Report the number of stack traces and the number of stack trace hash chains. These two numbers are useful because these allow to estimate the number of stack trace hash collisions.

Cc Thomas Gleixner <[email protected]>
Cc Will Deacon <[email protected]>
Cc Waiman Long <[email protected]>
---
 kernel/locking/lockdep.c           | 29 +++++++++++++++++++++++++++++
 kernel/locking/lockdep_internals.h |  4 ++++
 kernel/locking/lockdep_proc.c      |  6 ++++++
 3 files changed, 39 insertions(+)
kernel/locking/lockdep.c +29 -0
index 19eace34b5fa..320346b3bdbb 100644
@@ -516,6 +516,35 @@ static struct lock_trace *save_trace(void)
 
 	return trace;
 }
+
+/* Return the number of stack traces in the stack_trace[] array. */
+u64 lockdep_stack_trace_count(void)
+{
+	struct lock_trace *trace;
+	u64 c = 0;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++) {
+		hlist_for_each_entry(trace, &stack_trace_hash[i], hash_entry) {
+			c++;
+		}
+	}
+
+	return c;
+}
+
+/* Return the number of stack hash chains that have at least one stack trace. */
+u64 lockdep_stack_hash_count(void)
+{
+	u64 c = 0;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++)
+		if (!hlist_empty(&stack_trace_hash[i]))
+			c++;
+
+	return c;
+}
 #endif
 
 unsigned int nr_hardirq_chains;
kernel/locking/lockdep_internals.h +4 -0
index 93a008bf77db..18d85aebbb57 100644
@@ -140,6 +140,10 @@ extern unsigned int max_bfs_queue_depth;
 #ifdef CONFIG_PROVE_LOCKING
 extern unsigned long lockdep_count_forward_deps(struct lock_class *);
 extern unsigned long lockdep_count_backward_deps(struct lock_class *);
+#ifdef CONFIG_TRACE_IRQFLAGS
+u64 lockdep_stack_trace_count(void);
+u64 lockdep_stack_hash_count(void);
+#endif
 #else
 static inline unsigned long
 lockdep_count_forward_deps(struct lock_class *class)
kernel/locking/lockdep_proc.c +6 -0
index 1ba5780182e7..64b5b47ab117 100644
@@ -284,6 +284,12 @@ static int lockdep_stats_show(struct seq_file *m, void *v)
 			nr_process_chains);
 	seq_printf(m, " stack-trace entries:           %11lu [max: %lu]\n",
 			nr_stack_trace_entries, MAX_STACK_TRACE_ENTRIES);
+#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
+	seq_printf(m, " number of stack traces:        %llu\n",
+		   lockdep_stack_trace_count());
+	seq_printf(m, " number of stack hash chains:   %llu\n",
+		   lockdep_stack_hash_count());
+#endif
 	seq_printf(m, " combined max dependencies:     %11u\n",
 			(nr_hardirq_chains + 1) *
 			(nr_softirq_chains + 1) *
Signature · 1 line
2.22.0.657.g960e92d24f-goog
#10 lore raw

[tip:locking/core] locking/lockdep: Report more stack trace statistics

locking/lockdep: Report more stack trace statistics
Commit-ID:  8c779229d0f4fe83ead90bdcbbf08b02989aa200
Gitweb:     https://git.kernel.org/tip/8c779229d0f4fe83ead90bdcbbf08b02989aa200
Author:     Bart Van Assche <[email protected]>
AuthorDate: Mon, 22 Jul 2019 11:24:43 -0700
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 25 Jul 2019 15:43:28 +0200

locking/lockdep: Report more stack trace statistics

Report the number of stack traces and the number of stack trace hash chains. These two numbers are useful because these allow to estimate the number of stack trace hash collisions.

---
 kernel/locking/lockdep.c           | 29 +++++++++++++++++++++++++++++
 kernel/locking/lockdep_internals.h |  4 ++++
 kernel/locking/lockdep_proc.c      |  6 ++++++
 3 files changed, 39 insertions(+)
kernel/locking/lockdep.c +29 -0
index 1a96869cb2f0..3c3902c40a0e 100644
@@ -516,6 +516,35 @@ static struct lock_trace *save_trace(void)
 
 	return trace;
 }
+
+/* Return the number of stack traces in the stack_trace[] array. */
+u64 lockdep_stack_trace_count(void)
+{
+	struct lock_trace *trace;
+	u64 c = 0;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++) {
+		hlist_for_each_entry(trace, &stack_trace_hash[i], hash_entry) {
+			c++;
+		}
+	}
+
+	return c;
+}
+
+/* Return the number of stack hash chains that have at least one stack trace. */
+u64 lockdep_stack_hash_count(void)
+{
+	u64 c = 0;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++)
+		if (!hlist_empty(&stack_trace_hash[i]))
+			c++;
+
+	return c;
+}
 #endif
 
 unsigned int nr_hardirq_chains;
kernel/locking/lockdep_internals.h +4 -0
index 93a008bf77db..18d85aebbb57 100644
@@ -140,6 +140,10 @@ extern unsigned int max_bfs_queue_depth;
 #ifdef CONFIG_PROVE_LOCKING
 extern unsigned long lockdep_count_forward_deps(struct lock_class *);
 extern unsigned long lockdep_count_backward_deps(struct lock_class *);
+#ifdef CONFIG_TRACE_IRQFLAGS
+u64 lockdep_stack_trace_count(void);
+u64 lockdep_stack_hash_count(void);
+#endif
 #else
 static inline unsigned long
 lockdep_count_forward_deps(struct lock_class *class)
kernel/locking/lockdep_proc.c +6 -0
index ed9842425cac..dadb7b7fba37 100644
@@ -285,6 +285,12 @@ static int lockdep_stats_show(struct seq_file *m, void *v)
 			nr_process_chains);
 	seq_printf(m, " stack-trace entries:           %11lu [max: %lu]\n",
 			nr_stack_trace_entries, MAX_STACK_TRACE_ENTRIES);
+#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
+	seq_printf(m, " number of stack traces:        %llu\n",
+		   lockdep_stack_trace_count());
+	seq_printf(m, " number of stack hash chains:   %llu\n",
+		   lockdep_stack_hash_count());
+#endif
 	seq_printf(m, " combined max dependencies:     %11u\n",
 			(nr_hardirq_chains + 1) *
 			(nr_softirq_chains + 1) *
maintainer
#11 lore raw
In reply to Bart Van Assche
Quoted 19 lines · Bart Van Assche

Hi Peter,

An unfortunate side effect of commit 669de8bda87b ("kernel/workqueue: Use dynamic lockdep keys for workqueues") is that all stack traces associated with the lockdep key are leaked when a workqueue is destroyed. Fix this by storing each unique stack trace once. Please consider this patch series for Linux kernel v5.4.

Thanks,

Bart.

Bart Van Assche (4):
locking/lockdep: Make it clear that what lock_class::key points at is
not modified
stacktrace: Constify 'entries' arguments
locking/lockdep: Reduce space occupied by stack traces locking/lockdep: Report more stack trace statistics

Quoted 7 lines
 include/linux/lockdep.h            |  11 +-
 include/linux/stacktrace.h         |   4 +-
 kernel/locking/lockdep.c           | 159 ++++++++++++++++++++++-------
 kernel/locking/lockdep_internals.h |   9 +-
 kernel/locking/lockdep_proc.c      |   8 +-
 kernel/stacktrace.c                |   4 +-
 6 files changed, 143 insertions(+), 52 deletions(-)

Thanks a lot for doing this Bart, excellent stuff!