[RFC PATCH v1 1/1] tracing/kprobes: Return ENAMESVRLSYMS when func matches several symbols
From: Francis Laniel
Date: Wed Aug 23 2023 - 12:14:58 EST
Previously to this commit, if func matches several symbols, a PMU kprobe would
be installed for the first matching address.
This could lead to some misunderstanding when some BPF code was never called
because it was attached to a function which was indeed not call, because the
effectively called one has no kprobes.
So, this commit introduces ENAMESVRLSYMS which is returned when func matches
several symbols.
This way, user needs to use addr to remove the ambiguity.
Suggested-by: Masami Hiramatsu <mhiramat@xxxxxxxxxx>
Signed-off-by: Francis Laniel <flaniel@xxxxxxxxxxxxxxxxxxx>
Link: https://lore.kernel.org/lkml/20230819101105.b0c104ae4494a7d1f2eea742@xxxxxxxxxx/
---
arch/alpha/include/uapi/asm/errno.h | 2 ++
arch/mips/include/uapi/asm/errno.h | 2 ++
arch/parisc/include/uapi/asm/errno.h | 2 ++
arch/sparc/include/uapi/asm/errno.h | 2 ++
include/uapi/asm-generic/errno.h | 2 ++
kernel/trace/trace_kprobe.c | 26 ++++++++++++++++++++++
tools/arch/alpha/include/uapi/asm/errno.h | 2 ++
tools/arch/mips/include/uapi/asm/errno.h | 2 ++
tools/arch/parisc/include/uapi/asm/errno.h | 2 ++
tools/arch/sparc/include/uapi/asm/errno.h | 2 ++
tools/include/uapi/asm-generic/errno.h | 2 ++
11 files changed, 46 insertions(+)
diff --git a/arch/alpha/include/uapi/asm/errno.h b/arch/alpha/include/uapi/asm/errno.h
index 3d265f6babaf..3d9686d915f9 100644
--- a/arch/alpha/include/uapi/asm/errno.h
+++ b/arch/alpha/include/uapi/asm/errno.h
@@ -125,4 +125,6 @@
#define EHWPOISON 139 /* Memory page has hardware error */
+#define ENAMESVRLSYMS 140 /* Name correspond to several symbols */
+
#endif
diff --git a/arch/mips/include/uapi/asm/errno.h b/arch/mips/include/uapi/asm/errno.h
index 2fb714e2d6d8..1fd64ee7b629 100644
--- a/arch/mips/include/uapi/asm/errno.h
+++ b/arch/mips/include/uapi/asm/errno.h
@@ -124,6 +124,8 @@
#define EHWPOISON 168 /* Memory page has hardware error */
+#define ENAMESVRLSYMS 169 /* Name correspond to several symbols */
+
#define EDQUOT 1133 /* Quota exceeded */
diff --git a/arch/parisc/include/uapi/asm/errno.h b/arch/parisc/include/uapi/asm/errno.h
index 87245c584784..c7845ceece26 100644
--- a/arch/parisc/include/uapi/asm/errno.h
+++ b/arch/parisc/include/uapi/asm/errno.h
@@ -124,4 +124,6 @@
#define EHWPOISON 257 /* Memory page has hardware error */
+#define ENAMESVRLSYMS 258 /* Name correspond to several symbols */
+
#endif
diff --git a/arch/sparc/include/uapi/asm/errno.h b/arch/sparc/include/uapi/asm/errno.h
index 81a732b902ee..1ed065943bab 100644
--- a/arch/sparc/include/uapi/asm/errno.h
+++ b/arch/sparc/include/uapi/asm/errno.h
@@ -115,4 +115,6 @@
#define EHWPOISON 135 /* Memory page has hardware error */
+#define ENAMESVRLSYMS 136 /* Name correspond to several symbols */
+
#endif
diff --git a/include/uapi/asm-generic/errno.h b/include/uapi/asm-generic/errno.h
index cf9c51ac49f9..3d5d5740c8da 100644
--- a/include/uapi/asm-generic/errno.h
+++ b/include/uapi/asm-generic/errno.h
@@ -120,4 +120,6 @@
#define EHWPOISON 133 /* Memory page has hardware error */
+#define ENAMESVRLSYMS 134 /* Name correspond to several symbols */
+
#endif
diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c
index 23dba01831f7..53b66db1ff53 100644
--- a/kernel/trace/trace_kprobe.c
+++ b/kernel/trace/trace_kprobe.c
@@ -1699,6 +1699,16 @@ static int unregister_kprobe_event(struct trace_kprobe *tk)
}
#ifdef CONFIG_PERF_EVENTS
+
+static int count_symbols(void *data, unsigned long unused)
+{
+ unsigned int *count = data;
+
+ (*count)++;
+
+ return 0;
+}
+
/* create a trace_kprobe, but don't add it to global lists */
struct trace_event_call *
create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
@@ -1709,6 +1719,22 @@ create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
int ret;
char *event;
+ /*
+ * If user specifies func, we check that the function name does not
+ * correspond to several symbols.
+ * If this is the case, we return with error code ENAMESVRLSYMS to
+ * indicate the user he/she should use addr and offs rather than func to
+ * remove the ambiguity.
+ */
+ if (func) {
+ unsigned int count;
+
+ count = 0;
+ kallsyms_on_each_match_symbol(count_symbols, func, &count);
+ if (count > 1)
+ return ERR_PTR(-ENAMESVRLSYMS);
+ }
+
/*
* local trace_kprobes are not added to dyn_event, so they are never
* searched in find_trace_kprobe(). Therefore, there is no concern of
diff --git a/tools/arch/alpha/include/uapi/asm/errno.h b/tools/arch/alpha/include/uapi/asm/errno.h
index 3d265f6babaf..3d9686d915f9 100644
--- a/tools/arch/alpha/include/uapi/asm/errno.h
+++ b/tools/arch/alpha/include/uapi/asm/errno.h
@@ -125,4 +125,6 @@
#define EHWPOISON 139 /* Memory page has hardware error */
+#define ENAMESVRLSYMS 140 /* Name correspond to several symbols */
+
#endif
diff --git a/tools/arch/mips/include/uapi/asm/errno.h b/tools/arch/mips/include/uapi/asm/errno.h
index 2fb714e2d6d8..1fd64ee7b629 100644
--- a/tools/arch/mips/include/uapi/asm/errno.h
+++ b/tools/arch/mips/include/uapi/asm/errno.h
@@ -124,6 +124,8 @@
#define EHWPOISON 168 /* Memory page has hardware error */
+#define ENAMESVRLSYMS 169 /* Name correspond to several symbols */
+
#define EDQUOT 1133 /* Quota exceeded */
diff --git a/tools/arch/parisc/include/uapi/asm/errno.h b/tools/arch/parisc/include/uapi/asm/errno.h
index 87245c584784..c7845ceece26 100644
--- a/tools/arch/parisc/include/uapi/asm/errno.h
+++ b/tools/arch/parisc/include/uapi/asm/errno.h
@@ -124,4 +124,6 @@
#define EHWPOISON 257 /* Memory page has hardware error */
+#define ENAMESVRLSYMS 258 /* Name correspond to several symbols */
+
#endif
diff --git a/tools/arch/sparc/include/uapi/asm/errno.h b/tools/arch/sparc/include/uapi/asm/errno.h
index 81a732b902ee..1ed065943bab 100644
--- a/tools/arch/sparc/include/uapi/asm/errno.h
+++ b/tools/arch/sparc/include/uapi/asm/errno.h
@@ -115,4 +115,6 @@
#define EHWPOISON 135 /* Memory page has hardware error */
+#define ENAMESVRLSYMS 136 /* Name correspond to several symbols */
+
#endif
diff --git a/tools/include/uapi/asm-generic/errno.h b/tools/include/uapi/asm-generic/errno.h
index cf9c51ac49f9..3d5d5740c8da 100644
--- a/tools/include/uapi/asm-generic/errno.h
+++ b/tools/include/uapi/asm-generic/errno.h
@@ -120,4 +120,6 @@
#define EHWPOISON 133 /* Memory page has hardware error */
+#define ENAMESVRLSYMS 134 /* Name correspond to several symbols */
+
#endif
--
2.34.1