[PATCH] clocksource: Add debugfs support

From: Thierry Reding
Date: Tue Mar 31 2020 - 17:40:53 EST


From: Thierry Reding <treding@xxxxxxxxxx>

Add a top-level "clocksource" directory to debugfs. For each clocksource
registered with the system, a subdirectory will be added with attributes
that can be queried to obtain information about the clocksource.

Signed-off-by: Thierry Reding <treding@xxxxxxxxxx>
---
include/linux/clocksource.h | 3 ++
kernel/time/clocksource.c | 60 +++++++++++++++++++++++++++++++++++++
2 files changed, 63 insertions(+)

diff --git a/include/linux/clocksource.h b/include/linux/clocksource.h
index 86d143db6523..89424da76244 100644
--- a/include/linux/clocksource.h
+++ b/include/linux/clocksource.h
@@ -118,6 +118,9 @@ struct clocksource {
u64 wd_last;
#endif
struct module *owner;
+#ifdef CONFIG_DEBUG_FS
+ struct dentry *debugfs;
+#endif
};

/*
diff --git a/kernel/time/clocksource.c b/kernel/time/clocksource.c
index 7cb09c4cf21c..51266f53df83 100644
--- a/kernel/time/clocksource.c
+++ b/kernel/time/clocksource.c
@@ -911,6 +911,63 @@ void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq
}
EXPORT_SYMBOL_GPL(__clocksource_update_freq_scale);

+#ifdef CONFIG_DEBUG_FS
+#include <linux/debugfs.h>
+
+static struct dentry *debugfs_root;
+
+static int clocksource_debugfs_counter_show(struct seq_file *s, void *data)
+{
+ struct clocksource *cs = s->private;
+
+ seq_printf(s, "%llu\n", cs->read(cs));
+
+ return 0;
+}
+DEFINE_SHOW_ATTRIBUTE(clocksource_debugfs_counter);
+
+static void clocksource_debugfs_add(struct clocksource *cs)
+{
+ if (!debugfs_root)
+ return;
+
+ cs->debugfs = debugfs_create_dir(cs->name, debugfs_root);
+
+ debugfs_create_file("counter", 0444, cs->debugfs, cs,
+ &clocksource_debugfs_counter_fops);
+}
+
+static void clocksource_debugfs_remove(struct clocksource *cs)
+{
+ debugfs_remove_recursive(cs->debugfs);
+}
+
+static int __init init_clocksource_debugfs(void)
+{
+ struct clocksource *cs;
+
+ debugfs_root = debugfs_create_dir("clocksource", NULL);
+
+ mutex_lock(&clocksource_mutex);
+
+ list_for_each_entry(cs, &clocksource_list, list)
+ clocksource_debugfs_add(cs);
+
+ mutex_unlock(&clocksource_mutex);
+
+ return 0;
+}
+late_initcall(init_clocksource_debugfs);
+#else
+static inline void clocksource_debugfs_add(struct clocksource *cs)
+{
+}
+
+static inline void clocksource_debugfs_remove(struct clocksource *cs)
+{
+}
+#endif
+
/**
* __clocksource_register_scale - Used to install new clocksources
* @cs: clocksource to be registered
@@ -951,6 +1008,7 @@ int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
clocksource_select();
clocksource_select_watchdog(false);
__clocksource_suspend_select(cs);
+ clocksource_debugfs_add(cs);
mutex_unlock(&clocksource_mutex);
return 0;
}
@@ -991,6 +1049,8 @@ static int clocksource_unbind(struct clocksource *cs)
{
unsigned long flags;

+ clocksource_debugfs_remove(cs);
+
if (clocksource_is_watchdog(cs)) {
/* Select and try to install a replacement watchdog. */
clocksource_select_watchdog(true);
--
2.24.1