Skip to contentSkip to conversation

Backport fix commit for kprobe_non_uniq_symbol.tc test failure

PATCH Applied Sherry Yang 5 participants 11 messages Oct 8, 2024 – Oct 11, 2024 Stable branch

lore
Share & tools
lore
#0 lore raw

cover Backport fix commit for kprobe_non_uniq_symbol.tc test failure

5.10.y backported the commit
09bcf9254838 ("selftests/ftrace: Add new test case which checks non unique symbol")
which added a new test case to check non-unique symbol. However, 5.10.y
didn't backport the kernel commit
b022f0c7e404 ("tracing/kprobes: Return EADDRNOTAVAIL when func matches several symbols")to support the functionality from kernel side. Backport it in this patch series.

The first two patches are presiquisites. The 4th commit is a fix commit for the 3rd one.

Build and test case passed.
[73] Test failure of registering kprobe on non unique symbol

	[PASS]

Andrii Nakryiko (1):
tracing/kprobes: Fix symbol counting logic by looking at modules as
well

Francis Laniel (1):
tracing/kprobes: Return EADDRNOTAVAIL when func matches several
symbols

Jiri Olsa (2):
kallsyms: Make kallsyms_on_each_symbol generally available
kallsyms: Make module_kallsyms_on_each_symbol generally available

 include/linux/kallsyms.h    |  7 +++-
 include/linux/module.h      |  9 +++++
 kernel/kallsyms.c           |  2 -
 kernel/module.c             |  2 -
 kernel/trace/trace_kprobe.c | 76 +++++++++++++++++++++++++++++++++++++
 kernel/trace/trace_probe.h  |  1 +
 6 files changed, 92 insertions(+), 5 deletions(-)
Signature · 1 line
2.46.0
#1 lore raw

PATCH 1/4 kallsyms: Make kallsyms_on_each_symbol generally available

From: Jiri Olsa <[email protected]>

[ Upstream commit d721def7392a7348ffb9f3583b264239cbd3702c ]

Making kallsyms_on_each_symbol generally available, so it can be used outside CONFIG_LIVEPATCH option in following changes.

Rather than adding another ifdef option let's make the function generally available (when CONFIG_KALLSYMS option is defined).

Stable-dep-of: b022f0c7e404 ("tracing/kprobes: Return EADDRNOTAVAIL when func matches several symbols")

---
 include/linux/kallsyms.h | 7 ++++++-
 kernel/kallsyms.c        | 2 --
 2 files changed, 6 insertions(+), 3 deletions(-)
include/linux/kallsyms.h +6 -1
index 465060acc981..430f1cefbb9e 100644
@@ -71,11 +71,11 @@ static inline void *dereference_symbol_descriptor(void *ptr)
 	return ptr;
 }
 
+#ifdef CONFIG_KALLSYMS
 int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
 				      unsigned long),
 			    void *data);
 
-#ifdef CONFIG_KALLSYMS
 /* Lookup the address for a symbol. Returns 0 if not found. */
 unsigned long kallsyms_lookup_name(const char *name);
 
@@ -155,6 +155,11 @@ static inline bool kallsyms_show_value(const struct cred *cred)
 	return false;
 }
 
+static inline int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
+					  unsigned long), void *data)
+{
+	return -EOPNOTSUPP;
+}
 #endif /*CONFIG_KALLSYMS*/
 
 static inline void print_ip_sym(const char *loglvl, unsigned long ip)
kernel/kallsyms.c +0 -2
index 8043a90aa50e..a0d3f0865916 100644
@@ -177,7 +177,6 @@ unsigned long kallsyms_lookup_name(const char *name)
 	return module_kallsyms_lookup_name(name);
 }
 
-#ifdef CONFIG_LIVEPATCH
 /*
  * Iterate over all symbols in vmlinux.  For symbols from modules use
  * module_kallsyms_on_each_symbol instead.
@@ -199,7 +198,6 @@ int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
 	}
 	return 0;
 }
-#endif /* CONFIG_LIVEPATCH */
 
 static unsigned long get_symbol_pos(unsigned long addr,
 				    unsigned long *symbolsize,
Signature · 1 line
2.46.0
#2 lore raw

PATCH 2/4 kallsyms: Make module_kallsyms_on_each_symbol generally available

In reply to Sherry Yang

From: Jiri Olsa <[email protected]>

commit 73feb8d5fa3b755bb51077c0aabfb6aa556fd498 upstream.

Making module_kallsyms_on_each_symbol generally available, so it can be used outside CONFIG_LIVEPATCH option in following changes.

Rather than adding another ifdef option let's make the function generally available (when CONFIG_KALLSYMS and CONFIG_MODULES options are defined).

Stable-dep-of: 926fe783c8a6 ("tracing/kprobes: Fix symbol counting logic by looking at modules as well")

Stable-dep-of: 329197033bb0 ("tracing/kprobes: Fix symbol counting logic by looking at modules as well")

---
 include/linux/module.h | 9 +++++++++
 kernel/module.c        | 2 --
 2 files changed, 9 insertions(+), 2 deletions(-)
include/linux/module.h +9 -0
index a55a40c28568..63fe94e6ae6f 100644
@@ -875,8 +875,17 @@ static inline bool module_sig_ok(struct module *module)
 }
 #endif	/* CONFIG_MODULE_SIG */
 
+#if defined(CONFIG_MODULES) && defined(CONFIG_KALLSYMS)
 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
 					     struct module *, unsigned long),
 				   void *data);
+#else
+static inline int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
+						 struct module *, unsigned long),
+						 void *data)
+{
+	return -EOPNOTSUPP;
+}
+#endif  /* CONFIG_MODULES && CONFIG_KALLSYMS */
 
 #endif /* _LINUX_MODULE_H */
kernel/module.c +0 -2
index edc7b99cb16f..7f3ba597af6c 100644
@@ -4444,7 +4444,6 @@ unsigned long module_kallsyms_lookup_name(const char *name)
 	return ret;
 }
 
-#ifdef CONFIG_LIVEPATCH
 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
 					     struct module *, unsigned long),
 				   void *data)
@@ -4475,7 +4474,6 @@ int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
 	mutex_unlock(&module_mutex);
 	return ret;
 }
-#endif /* CONFIG_LIVEPATCH */
 #endif /* CONFIG_KALLSYMS */
 
 /* Maximum number of characters written by module_flags() */
Signature · 1 line
2.46.0
#3 lore raw

PATCH 3/4 tracing/kprobes: Return EADDRNOTAVAIL when func matches several symbols

In reply to Sherry Yang

From: Francis Laniel <[email protected]>

commit b022f0c7e404887a7c5229788fc99eff9f9a80d5 upstream.

When a kprobe is attached to a function that's name is not unique (is static and shares the name with other functions in the kernel), the kprobe is attached to the first function it finds. This is a bug as the function that it is attaching to is not necessarily the one that the user wants to attach to.

Instead of blindly picking a function to attach to what is ambiguous, error with EADDRNOTAVAIL to let the user know that this function is not unique, and that the user must use another unique function with an address offset to get to the function they want to attach to.

[Sherry: 5.10.y added a new kselftest kprobe_non_uniq_symbol.tc by backporting commit 09bcf9254838 ("selftests/ftrace: Add new test case which checks non unique symbol"). However, 5.10.y didn't backport this commit which provides unique symbol check suppport from kernel side. Minor conflicts due to context change, ignore context change]

---
 kernel/trace/trace_kprobe.c | 74 +++++++++++++++++++++++++++++++++++++
 kernel/trace/trace_probe.h  |  1 +
 2 files changed, 75 insertions(+)
kernel/trace/trace_kprobe.c +74 -0
index 5453af26ff76..91dfe8cf1ce8 100644
@@ -715,6 +715,36 @@ static inline void sanitize_event_name(char *name)
 			*name = '_';
 }
 
+struct count_symbols_struct {
+	const char *func_name;
+	unsigned int count;
+};
+
+static int count_symbols(void *data, const char *name, struct module *unused0,
+			 unsigned long unused1)
+{
+	struct count_symbols_struct *args = data;
+
+	if (strcmp(args->func_name, name))
+		return 0;
+
+	args->count++;
+
+	return 0;
+}
+
+static unsigned int number_of_same_symbols(char *func_name)
+{
+	struct count_symbols_struct args = {
+		.func_name = func_name,
+		.count = 0,
+	};
+
+	kallsyms_on_each_symbol(count_symbols, &args);
+
+	return args.count;
+}
+
 static int trace_kprobe_create(int argc, const char *argv[])
 {
 	/*
@@ -842,6 +872,31 @@ static int trace_kprobe_create(int argc, const char *argv[])
 		}
 	}
 
+	if (symbol && !strchr(symbol, ':')) {
+		unsigned int count;
+
+		count = number_of_same_symbols(symbol);
+		if (count > 1) {
+			/*
+			 * Users should use ADDR to remove the ambiguity of
+			 * using KSYM only.
+			 */
+			trace_probe_log_err(0, NON_UNIQ_SYMBOL);
+			ret = -EADDRNOTAVAIL;
+
+			goto error;
+		} else if (count == 0) {
+			/*
+			 * We can return ENOENT earlier than when register the
+			 * kprobe.
+			 */
+			trace_probe_log_err(0, BAD_PROBE_ADDR);
+			ret = -ENOENT;
+
+			goto error;
+		}
+	}
+
 	trace_probe_log_set_index(0);
 	if (event) {
 		ret = traceprobe_parse_event_name(&event, &group, buf,
@@ -1805,6 +1860,7 @@ static int unregister_kprobe_event(struct trace_kprobe *tk)
 }
 
 #ifdef CONFIG_PERF_EVENTS
+
 /* 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,
@@ -1814,6 +1870,24 @@ create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
 	int ret;
 	char *event;
 
+	if (func) {
+		unsigned int count;
+
+		count = number_of_same_symbols(func);
+		if (count > 1)
+			/*
+			 * Users should use addr to remove the ambiguity of
+			 * using func only.
+			 */
+			return ERR_PTR(-EADDRNOTAVAIL);
+		else if (count == 0)
+			/*
+			 * We can return ENOENT earlier than when register the
+			 * kprobe.
+			 */
+			return ERR_PTR(-ENOENT);
+	}
+
 	/*
 	 * local trace_kprobes are not added to dyn_event, so they are never
 	 * searched in find_trace_kprobe(). Therefore, there is no concern of
kernel/trace/trace_probe.h +1 -0
index d4a69b83902e..22c05ca97758 100644
@@ -390,6 +390,7 @@ extern int traceprobe_define_arg_fields(struct trace_event_call *event_call,
 	C(BAD_MAXACT,		"Invalid maxactive number"),		\
 	C(MAXACT_TOO_BIG,	"Maxactive is too big"),		\
 	C(BAD_PROBE_ADDR,	"Invalid probed address or symbol"),	\
+	C(NON_UNIQ_SYMBOL,	"The symbol is not unique"),		\
 	C(BAD_RETPROBE,		"Retprobe address must be an function entry"), \
 	C(BAD_ADDR_SUFFIX,	"Invalid probed address suffix"), \
 	C(NO_GROUP_NAME,	"Group name is not specified"),		\
Signature · 1 line
2.46.0
#4 lore raw

PATCH 4/4 tracing/kprobes: Fix symbol counting logic by looking at modules as well

In reply to Sherry Yang

From: Andrii Nakryiko <[email protected]>

commit 926fe783c8a64b33997fec405cf1af3e61aed441 upstream.

Recent changes to count number of matching symbols when creating a kprobe event failed to take into account kernel modules. As such, it breaks kprobes on kernel module symbols, by assuming there is no match.

Fix this my calling module_kallsyms_on_each_symbol() in addition to kallsyms_on_each_match_symbol() to perform a proper counting.

Cc Francis Laniel <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Steven Rostedt <[email protected]>
Fixes b022f0c7e404 ("tracing/kprobes: Return EADDRNOTAVAIL when func matches several symbols")
[Sherry: It's a fix for previous backport, thus backport together]
---
 kernel/trace/trace_kprobe.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/trace/trace_kprobe.c +2 -0
index 91dfe8cf1ce8..ae059345ddf4 100644
@@ -742,6 +742,8 @@ static unsigned int number_of_same_symbols(char *func_name)
 
 	kallsyms_on_each_symbol(count_symbols, &args);
 
+	module_kallsyms_on_each_symbol(count_symbols, &args);
+
 	return args.count;
 }
 
Signature · 1 line
2.46.0
maintainer
#5 lore raw
In reply to Sherry Yang
Quoted 8 lines · Sherry Yang

5.10.y backported the commit
09bcf9254838 ("selftests/ftrace: Add new test case which checks non unique symbol")
which added a new test case to check non-unique symbol. However, 5.10.y
didn't backport the kernel commit
b022f0c7e404 ("tracing/kprobes: Return EADDRNOTAVAIL when func matches several symbols")to support the functionality from kernel side. Backport it in this patch series.

The first two patches are presiquisites. The 4th commit is a fix commit for the 3rd one.

Should we just revert the selftest test instead? That seems simpler instead of adding a new feature to this old and obsolete kernel tree, right?

thanks,

greg k-h

#6 lore raw
Quoted 7 lines · On Wed, 9 Oct 2024 15:36:42 +0200 Greg KH <[email protected]> wrote:
Quoted 6 lines · Sherry Yang

5.10.y backported the commit
09bcf9254838 ("selftests/ftrace: Add new test case which checks non unique symbol")
which added a new test case to check non-unique symbol. However, 5.10.y
didn't backport the kernel commit
b022f0c7e404 ("tracing/kprobes: Return EADDRNOTAVAIL when func matches several symbols")to support the functionality from kernel side. Backport it in this patch series.

Quoted 6 lines
Quoted 2 lines

The first two patches are presiquisites. The 4th commit is a fix commit for the 3rd one.

Should we just revert the selftest test instead? That seems simpler instead of adding a new feature to this old and obsolete kernel tree, right?

The selftest is just testing to see if the kernel has the bug. The bug is that if there's more than one function with the same name, and someone attaches a kprobe to one of them, the user doesn't know which function it is attaching to, and the kernel doesn't tell the user that it's picking some random function. This can have undesirable results.

I don't know how much this kernel is still used. But if it's supported and still taking fixes, I would consider this a fix and not a feature.

-- Steve

maintainer
#7 lore raw
In reply to Greg KH
Quoted 7 lines · Greg KH
Quoted 6 lines · Sherry Yang

5.10.y backported the commit
09bcf9254838 ("selftests/ftrace: Add new test case which checks non unique symbol")
which added a new test case to check non-unique symbol. However, 5.10.y
didn't backport the kernel commit
b022f0c7e404 ("tracing/kprobes: Return EADDRNOTAVAIL when func matches several symbols")to support the functionality from kernel side. Backport it in this patch series.

Quoted 6 lines
Quoted 2 lines

The first two patches are presiquisites. The 4th commit is a fix commit for the 3rd one.

Should we just revert the selftest test instead? That seems simpler instead of adding a new feature to this old and obsolete kernel tree, right?

Greg, I read the cover letter the same way and asked myself the same question, but looking at the patches it's actually a fix for something that's broken on 5.10 and was just uncovered by the selftest.

I think we should take it.

Signature · 2 lines
Thanks,
Sasha
#8 lore raw
In reply to Greg KH
Quoted 11 lines · Greg KH
Quoted 6 lines · Sherry Yang

5.10.y backported the commit
09bcf9254838 ("selftests/ftrace: Add new test case which checks non unique symbol")
which added a new test case to check non-unique symbol. However, 5.10.y
didn't backport the kernel commit
b022f0c7e404 ("tracing/kprobes: Return EADDRNOTAVAIL when func matches several symbols")to support the functionality from kernel side. Backport it in this patch series.

Quoted 6 lines
Quoted 2 lines

The first two patches are presiquisites. The 4th commit is a fix commit for the 3rd one.

Should we just revert the selftest test instead? That seems simpler instead of adding a new feature to this old and obsolete kernel tree, right?

Sorry about the confusion. If kprobe attaches a function which is not the user wants to attach to, I would say it’s a bug. The test case uncovers the bug, so it’s a fix.

Sherry

#9 lore raw

Hi!

Quoted 7 lines · Le jeudi 10 octobre 2024, 18:11:51 CEST Sherry Yang a écrit :
Quoted 7 lines · Greg KH
Quoted 3 lines · Sherry Yang

5.10.y backported the commit
09bcf9254838 ("selftests/ftrace: Add new test case which checks non unique symbol")

which added a new test case to check non-unique symbol.

Quoted 14 lines
Quoted 5 lines
Quoted 4 lines

However, 5.10.y didn't backport the kernel commit b022f0c7e404 ("tracing/kprobes: Return EADDRNOTAVAIL when func matches several symbols")to support the functionality from kernel side. Backport it in this patch series.

Quoted 7 lines
Quoted 2 lines

The first two patches are presiquisites. The 4th commit is a fix commit for the 3rd one.

Should we just revert the selftest test instead? That seems simpler instead of adding a new feature to this old and obsolete kernel tree, right?

Quoted 7 lines

Sorry about the confusion. If kprobe attaches a function which is not the user wants to attach to, I would say it’s a bug. The test case uncovers the bug, so it’s a fix.

Sherry

Let me add a bit of context as I wrote the third patch of this set.

It all started with a problem I had when trying to trace symbol names corresponding to different functions [1].
The patch was accepted to upstream and I wanted to backport it to stables. Sadly, the patch itself was relying on other patches which were not present in some stable kernels, which leaded to various problems while releasing the new stable kernels (once again: sorry about having caused troubles here) [2]...

The current series seems to hold all the patches for the third one to work, so I guess we can now have it merged to stable without problems.

Best regards.
---
[1]: https://lore.kernel.org/all/[email protected]/T/
[2]: https://lore.kernel.org/all/2024010402-commerce-variably-ef86@gregkh/

maintainer Applied
#10 lore raw
Quoted 42 lines · Francis Laniel

Hi!

Quoted 7 lines · Le jeudi 10 octobre 2024, 18:11:51 CEST Sherry Yang a écrit :
Quoted 7 lines · Greg KH
Quoted 3 lines · Sherry Yang

5.10.y backported the commit
09bcf9254838 ("selftests/ftrace: Add new test case which checks non unique symbol")

which added a new test case to check non-unique symbol.

Quoted 14 lines
Quoted 5 lines
Quoted 4 lines

However, 5.10.y didn't backport the kernel commit b022f0c7e404 ("tracing/kprobes: Return EADDRNOTAVAIL when func matches several symbols")to support the functionality from kernel side. Backport it in this patch series.

Quoted 7 lines
Quoted 2 lines

The first two patches are presiquisites. The 4th commit is a fix commit for the 3rd one.

Should we just revert the selftest test instead? That seems simpler instead of adding a new feature to this old and obsolete kernel tree, right?

Quoted 7 lines

Sorry about the confusion. If kprobe attaches a function which is not the user wants to attach to, I would say it’s a bug. The test case uncovers the bug, so it’s a fix.

Sherry

Let me add a bit of context as I wrote the third patch of this set.

It all started with a problem I had when trying to trace symbol names corresponding to different functions [1].
The patch was accepted to upstream and I wanted to backport it to stables. Sadly, the patch itself was relying on other patches which were not present in some stable kernels, which leaded to various problems while releasing the new stable kernels (once again: sorry about having caused troubles here) [2]...

Quoted 2 lines

The current series seems to hold all the patches for the third one to work, so I guess we can now have it merged to stable without problems.

Queued up, thanks!

Signature · 2 lines
Thanks,
Sasha