Unbreak LTTng for kernel 5.7
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 4 May 2020 19:00:53 +0000 (15:00 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 8 May 2020 17:56:40 +0000 (13:56 -0400)
Linux commit 0bd476e6c67190b5eb7b6e105c8db8ff61103281 ("kallsyms:
unexport kallsyms_lookup_name() and kallsyms_on_each_symbol()") breaks
LTTng-modules by removing symbols used by the LTTng-modules out-of-tree
tracer.

I pointed this out when the change was originally considered before the
5.7 merge window. This generated some discussion but it did not lead to
any concrete proposal to fix the issue. [1]

The commit has been merged in the 5.7 merge window. At that point, as
maintainer of LTTng, I immediately raised a flag about this issue,
proposing an alternative approach to solve this: expose the few symbols
needed by LTTng to GPL modules. This was NACKed on the ground that the
Linux kernel cannot export GPL symbols when there are no in-tree
users. [2]

Steven Rostedt has shown interest in merging LTTng-modules upstream.
LTTng-modules being LGPL, this is very much doable. I have prepared a
tree of LTTng-modules "for upstreaming" and sent it to him privately so
he can review it. Even if in an ideal scenario LTTng-modules is merged
for the following merge window, it leaves LTTng-modules broken on the
5.7 kernel.

In order to ensure that the LTTng-modules kernel tracer continues working
for my end users on kernels 5.7 onwards, as a very last resort, this is
with great reluctance that I created this fix for LTTng modules. It
basically uses kprobes to lookup the kallsyms_lookup_name symbol, and
continues using kallsyms_lookup_name as before.

Link: https://lore.kernel.org/r/20200302192811.n6o5645rsib44vco@localhost
Link: https://lore.kernel.org/r/20200409193543.18115-1-mathieu.desnoyers@efficios.com
Link: https://lwn.net/Articles/817988/
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
CC: Thomas Gleixner <tglx@linutronix.de>
CC: Will Deacon <will@kernel.org>
CC: akpm@linux-foundation.org
CC: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
CC: Masami Hiramatsu <mhiramat@kernel.org>
CC: rostedt@goodmis.org
CC: Alexei Starovoitov <ast@kernel.org>
Makefile
README.md
wrapper/kallsyms.c [new file with mode: 0644]
wrapper/kallsyms.h

index b487f2672a3f842878881d7dfba7bcb3e32b4a71..c30580376c4215ad3a2f65584c17edc4a02df99c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -65,6 +65,7 @@ ifneq ($(KERNELRELEASE),)
   lttng-wrapper-objs := wrapper/page_alloc.o \
                         wrapper/random.o \
                         wrapper/trace-clock.o \
+                        wrapper/kallsyms.o \
                         wrapper/irqdesc.o \
                         wrapper/fdtable.o \
                         lttng-wrapper-impl.o
index 201a579c7a35a717320ae3c11f67cdb2ed22417d..be8aa5f89cb8721e3a07bfc12c720bdbd22729d3 100644 (file)
--- a/README.md
+++ b/README.md
@@ -65,7 +65,8 @@ Make sure your target kernel has the following config options enabled:
   - `CONFIG_HIGH_RES_TIMERS`: needed for LTTng 2.x clock source
   - `CONFIG_TRACEPOINTS`: kernel tracepoint instrumentation
      (enabled as a side-effect of any of the perf/ftrace/blktrace
-     instrumentation features)
+     instrumentation features).
+  - `CONFIG_KPROBES` (5.7+): use kallsyms for kernel 5.7 and newer.
 
 
 ### Supported (optional) kernel config options
diff --git a/wrapper/kallsyms.c b/wrapper/kallsyms.c
new file mode 100644 (file)
index 0000000..6af77f5
--- /dev/null
@@ -0,0 +1,70 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
+ *
+ * wrapper/kallsyms.c
+ *
+ * Wrapper around kallsyms. Using kprobes to get its address when available.
+ *
+ * Can we mainline LTTng already so we don't have to waste our time doing this
+ * kind of hack ?
+ *
+ * Copyright (C) 2020 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ */
+
+#include <linux/kprobes.h>
+#include <linux/module.h>
+#include <wrapper/kallsyms.h>
+
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,7,0))
+
+#ifndef CONFIG_KPROBES
+# error "LTTng-modules requires CONFIG_KPROBES on kernels >= 5.7.0"
+#endif
+
+static
+unsigned long (*kallsyms_lookup_name_sym)(const char *name);
+
+static
+int dummy_kprobe_handler(struct kprobe *p, struct pt_regs *regs)
+{
+       return 0;
+}
+
+static
+unsigned long do_get_kallsyms(void)
+{
+       struct kprobe probe;
+       int ret;
+       unsigned long addr;
+
+       memset(&probe, 0, sizeof(probe));
+       probe.pre_handler = dummy_kprobe_handler;
+       probe.symbol_name = "kallsyms_lookup_name";
+       ret = register_kprobe(&probe);
+       if (ret)
+               return 0;
+       addr = (unsigned long)probe.addr;
+#ifdef CONFIG_ARM
+#ifdef CONFIG_THUMB2_KERNEL
+       if (addr)
+               addr |= 1; /* set bit 0 in address for thumb mode */
+#endif
+#endif
+       unregister_kprobe(&probe);
+       return addr;
+}
+
+unsigned long wrapper_kallsyms_lookup_name(const char *name)
+{
+       if (!kallsyms_lookup_name_sym) {
+               kallsyms_lookup_name_sym = (void *)do_get_kallsyms();
+       }
+       if (kallsyms_lookup_name_sym)
+               return kallsyms_lookup_name_sym(name);
+       else {
+               printk_once(KERN_WARNING "LTTng requires kallsyms_lookup_name\n");
+               return 0;
+       }
+}
+EXPORT_SYMBOL_GPL(wrapper_kallsyms_lookup_name);
+
+#endif
index e60fe363818243d78885aa15961256d66f3ec4ec..5f48847fb22a936595c8f637b3e552194a07062e 100644 (file)
@@ -14,6 +14,7 @@
 #define _LTTNG_WRAPPER_KALLSYMS_H
 
 #include <linux/kallsyms.h>
+#include <linux/version.h>
 
 /*
  * PowerPC ABIv1 needs KALLSYMS_ALL to get the function descriptor,
 # endif
 #endif
 
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,7,0))
+
+unsigned long wrapper_kallsyms_lookup_name(const char *name);
+
+#else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,7,0)) */
+
+static inline
+unsigned long wrapper_kallsyms_lookup_name(const char *name)
+{
+       return kallsyms_lookup_name(name);
+}
+
+#endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,7,0)) */
+
 static inline
 unsigned long kallsyms_lookup_funcptr(const char *name)
 {
        unsigned long addr;
 
-       addr = kallsyms_lookup_name(name);
+       addr = wrapper_kallsyms_lookup_name(name);
 #ifdef CONFIG_ARM
 #ifdef CONFIG_THUMB2_KERNEL
        if (addr)
@@ -43,6 +58,7 @@ unsigned long kallsyms_lookup_funcptr(const char *name)
 static inline
 unsigned long kallsyms_lookup_dataptr(const char *name)
 {
-       return kallsyms_lookup_name(name);
+       return wrapper_kallsyms_lookup_name(name);
 }
+
 #endif /* _LTTNG_WRAPPER_KALLSYMS_H */
This page took 0.031021 seconds and 4 git commands to generate.