[PATCH 3/3] lib/bug: add debugfs interface to list all BUG/WARN sites

Josh Law posted 3 patches 3 weeks, 1 day ago
[PATCH 3/3] lib/bug: add debugfs interface to list all BUG/WARN sites
Posted by Josh Law 3 weeks, 1 day ago
Currently there is no way to inspect the runtime state of WARN_ONCE
sites.  The existing clear_warn_once debugfs file lets operators reset
them, but there is no counterpart to see which sites exist or which
have already fired.  On production systems this matters: when a
WARN_ONCE fires during a transient event, the only evidence is a
single dmesg line that may have already rotated out.  Operators
investigating later have no way to tell which WARN_ONCE sites have
tripped without rebooting or reproducing the issue.

Add /sys/kernel/debug/bug_sites which lists every registered BUG()
and WARN() site in the kernel, including those from loaded modules.
Each line shows the symbolized address, source location (file:line
when CONFIG_DEBUG_BUGVERBOSE is enabled), type and state flags, and
the module name if applicable.  Example output:

  func+0x10/0x20  kernel/foo.c:123  warn,once,done
  bar+0x5/0x10    drivers/baz.c:456 warn         [baz_mod]

The "done" flag indicates that a WARN_ONCE site has fired at least
once since boot (or since the last clear_warn_once), giving operators
a reliable way to audit warning state on a running system without
depending on log retention.

Signed-off-by: Josh Law <objecting@objecting.org>
---
 lib/bug.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)

diff --git a/lib/bug.c b/lib/bug.c
index 9d76703ff7d1..ddf4531d6661 100644
--- a/lib/bug.c
+++ b/lib/bug.c
@@ -48,6 +48,8 @@
 #include <linux/rculist.h>
 #include <linux/ftrace.h>
 #include <linux/context_tracking.h>
+#include <linux/debugfs.h>
+#include <linux/seq_file.h>
 
 extern struct bug_entry __start___bug_table[], __stop___bug_table[];
 
@@ -311,3 +313,68 @@ void generic_bug_clear_once(void)
 
 	clear_once_table(__start___bug_table, __stop___bug_table);
 }
+
+#ifdef CONFIG_DEBUG_FS
+static void bug_show_entry(struct seq_file *m, const struct bug_entry *bug,
+			   const char *modname)
+{
+	const char *file;
+	unsigned int line;
+	unsigned short flags = READ_ONCE(bug->flags);
+
+	bug_get_file_line(bug, &file, &line);
+
+	seq_printf(m, "%pS\t", (void *)bug_addr(bug));
+
+	if (file)
+		seq_printf(m, "%s:%u\t", file, line);
+	else
+		seq_puts(m, "-\t");
+
+	if (flags & BUGFLAG_WARNING)
+		seq_puts(m, "warn");
+	else
+		seq_puts(m, "bug");
+	if (flags & BUGFLAG_ONCE)
+		seq_puts(m, ",once");
+	if (flags & BUGFLAG_DONE)
+		seq_puts(m, ",done");
+
+	if (modname)
+		seq_printf(m, "\t[%s]", modname);
+
+	seq_putc(m, '\n');
+}
+
+static int bug_sites_show(struct seq_file *m, void *v)
+{
+	struct bug_entry *bug;
+
+	for (bug = __start___bug_table; bug < __stop___bug_table; bug++)
+		bug_show_entry(m, bug, NULL);
+
+#ifdef CONFIG_MODULES
+	{
+		struct module *mod;
+		unsigned int i;
+
+		rcu_read_lock();
+		list_for_each_entry_rcu(mod, &module_bug_list, bug_list)
+			for (i = 0; i < mod->num_bugs; i++)
+				bug_show_entry(m, &mod->bug_table[i],
+					       mod->name);
+		rcu_read_unlock();
+	}
+#endif
+
+	return 0;
+}
+DEFINE_SHOW_ATTRIBUTE(bug_sites);
+
+static int __init bug_debugfs_init(void)
+{
+	debugfs_create_file("bug_sites", 0444, NULL, NULL, &bug_sites_fops);
+	return 0;
+}
+device_initcall(bug_debugfs_init);
+#endif /* CONFIG_DEBUG_FS */
-- 
2.34.1