[PATCH] kernel: count warnings and make count accessible to userspace

From: John Sperbeck
Date: Tue Jan 18 2022 - 01:05:18 EST


When testing, it's common to consider a warning to be a test failure,
but it's currently awkward to determine which of multiple sequential
tests is responsible for triggering a warning. Scraping dmesg or
/var/log/messages is somewhat expensive and error-prone. Setting
panic_on_warn is reliable, but spoils test runs for minor issues.
Looking at the taint bit is also reliable, but only works for a single
warning.

We can track the warning count and expose it as a sysfs file. Test
infrastructures can snapshot the value before and after a test. If
the value changes, they can do more expensive things like extracting
logs.

Signed-off-by: John Sperbeck <jsperbeck@xxxxxxxxxx>
---
kernel/panic.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/kernel/panic.c b/kernel/panic.c
index cefd7d82366f..5262c2a0ebf4 100644
--- a/kernel/panic.c
+++ b/kernel/panic.c
@@ -571,6 +571,8 @@ struct warn_args {
va_list args;
};

+static atomic_t __maybe_unused warn_counter;
+
void __warn(const char *file, int line, void *caller, unsigned taint,
struct pt_regs *regs, struct warn_args *args)
{
@@ -612,6 +614,8 @@ void __warn(const char *file, int line, void *caller, unsigned taint,

/* Just a warning, don't kill lockdep. */
add_taint(taint, LOCKDEP_STILL_OK);
+
+ atomic_inc(&warn_counter);
}

#ifndef __WARN_FLAGS
@@ -667,6 +671,7 @@ static __init int register_warn_debugfs(void)
/* Don't care about failure */
debugfs_create_file_unsafe("clear_warn_once", 0200, NULL, NULL,
&clear_warn_once_fops);
+ debugfs_create_atomic_t("warn_count", 0444, NULL, &warn_counter);
return 0;
}

--
2.34.1.703.g22d0c6ccf7-goog