Expose targeted sampling through the KCSAN debugfs file. Accept address
and kallsyms targets, including module:symbol names, and allow sampling
to be disabled with target=off.

Use KSYM_SYMBOL_LEN for the input buffer so documented module symbols fit,
and return -E2BIG for oversized writes.

Signed-off-by: Hui Su <[email protected]>
---
 kernel/kcsan/debugfs.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/kernel/kcsan/debugfs.c b/kernel/kcsan/debugfs.c
index 2af39ba5b70b..47becde9f1ae 100644
--- a/kernel/kcsan/debugfs.c
+++ b/kernel/kcsan/debugfs.c
@@ -191,6 +191,7 @@ static ssize_t insert_report_filterlist(const char *func)
 static int show_info(struct seq_file *file, void *v)
 {
        int i;
+       unsigned long target;
        unsigned long flags;
 
        /* show stats */
@@ -199,6 +200,12 @@ static int show_info(struct seq_file *file, void *v)
                seq_printf(file, "%s: %ld\n", counter_names[i],
                           atomic_long_read(&kcsan_counters[i]));
        }
+       target = kcsan_target_get();
+       if (target)
+               seq_printf(file, "\ntarget: %pK (%pS)\n", (void *)target,
+                          (void *)target);
+       else
+               seq_puts(file, "\ntarget: none\n");
 
        /* show filter functions, and filter type */
        raw_spin_lock_irqsave(&report_filterlist_lock, flags);
@@ -220,10 +227,13 @@ static int debugfs_open(struct inode *inode, struct file 
*file)
 static ssize_t
 debugfs_write(struct file *file, const char __user *buf, size_t count, loff_t 
*off)
 {
-       char kbuf[KSYM_NAME_LEN];
+       char kbuf[KSYM_SYMBOL_LEN + sizeof("target=")];
        char *arg;
        const size_t read_len = min(count, sizeof(kbuf) - 1);
 
+       if (count > sizeof(kbuf) - 1)
+               return -E2BIG;
+
        if (copy_from_user(kbuf, buf, read_len))
                return -EFAULT;
        kbuf[read_len] = '\0';
@@ -239,6 +249,22 @@ debugfs_write(struct file *file, const char __user *buf, 
size_t count, loff_t *o
                if (kstrtoul(&arg[strlen("microbench=")], 0, &iters))
                        return -EINVAL;
                microbenchmark(iters);
+       } else if (str_has_prefix(arg, "target=")) {
+               char *target = &arg[strlen("target=")];
+               unsigned long addr;
+
+               if (!strcmp(target, "off")) {
+                       kcsan_target_set(0);
+               } else if (!kstrtoul(target, 0, &addr)) {
+                       if (!addr)
+                               return -EINVAL;
+                       kcsan_target_set(addr);
+               } else {
+                       addr = kallsyms_lookup_name(target);
+                       if (!addr)
+                               return -ENOENT;
+                       kcsan_target_set(addr);
+               }
        } else if (!strcmp(arg, "whitelist")) {
                set_report_filterlist_whitelist(true);
        } else if (!strcmp(arg, "blacklist")) {
-- 
2.55.0


Reply via email to