linux-trace-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH v1 0/1] Return ENAMESVRLSYMS when func matches several symbols during PMU kprobe creation
@ 2023-08-23 16:14 Francis Laniel
  2023-08-23 16:14 ` [RFC PATCH v1 1/1] tracing/kprobes: Return ENAMESVRLSYMS when func matches several symbols Francis Laniel
  0 siblings, 1 reply; 4+ messages in thread
From: Francis Laniel @ 2023-08-23 16:14 UTC (permalink / raw)
  To: linux-kernel; +Cc: Masami Hiramatsu, linux-trace-kernel, Francis Laniel

Hi.


In the kernel source code, it exists different functions which share the same
name but which have, of course, different addresses as they can be defined in
different modules:
root@vm-amd64:~# grep ntfs_file_write_iter /proc/kallsyms
ffffffff814ce3c0 t __pfx_ntfs_file_write_iter
ffffffff814ce3d0 t ntfs_file_write_iter
ffffffff814fc8a0 t __pfx_ntfs_file_write_iter
ffffffff814fc8b0 t ntfs_file_write_iter
This can be source of troubles when you create a PMU kprobe for such a function,
as it will only install one for the first address (e.g. 0xffffffff814ce3d0 in
the above).
This could lead to some troubles were BPF based tools does not report any event
because the second function is not called:
root@vm-amd64:/mnt# mount | grep /mnt
/foo.img on /mnt type ntfs3 (rw,relatime,uid=0,gid=0,iocharset=utf8)
# ig is a tool which installs a PMU kprobe on ntfs_file_write_iter().
root@vm-amd64:/mnt# ig trace fsslower -m 0 -f ntfs3 --host &> /tmp/foo &
[1] 207
root@vm-amd64:/mnt# dd if=./foo of=./bar count=3
3+0 records in
3+0 records out
1536 bytes (1.5 kB, 1.5 KiB) copied, 0.00543323 s, 283 kB/s
root@vm-amd64:/mnt# fg
ig trace fsslower -m 0 -f ntfs3 --host &> /tmp/foo
^Croot@vm-amd64:/mnt# more /tmp/foo
RUNTIME.CONTAINERNAME          RUNTIME.CONTAIN… PID              COMM
  T      BYTES     OFFSET        LAT FILE
                                                214              dd
  R        512          0        766 foo
                                                214              dd
  R        512        512          9 foo
                                                214              dd
As you can see in the above, only read events are reported and no write because
the kprobe is installed for the old ntfs_file_write_iter() and not the ntfs3
one.

In this contribution, I modified create_local_trace_kprobe() to test if the
function name given as argument matches several symbols.
If this is the case ENAMESVRLSYMS, a new error code, is returned to indicate the
user to use addr and offs to remove this ambiguity.
So, when the above BPF tool is run, the following error message is printed:
root@vm-amd64:~# ig trace fsslower -m 0 -f ntfs3 --host &> /tmp/foo
root@vm-amd64:~# more /tmp/foo
RUNTIME.CONTAINERNAME          RUNTIME.CONTAIN… PID              COMM
  T      BYTES     OFFSET        LAT FILE
Error: running gadget: running gadget: installing tracer: attaching kprobe: crea
ting perf_kprobe PMU (arch-specific fallback for "ntfs_file_write_iter"): token
ntfs_file_write_iter: opening perf event: errno 134

Note that, this contribution is the conclusion of a previous RFC which intended
to install a PMU kprobe for all matching symbols [1, 2].

If you see any way to improve this contribution, particularly if you have an
idea to add a test for this behavior, please share your feedback.

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

 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(+)


Best regards and thank you in advance.
---
[1]: https://lore.kernel.org/lkml/20230816163517.112518-1-flaniel@linux.microsoft.com/
[2]: https://lore.kernel.org/lkml/20230819101105.b0c104ae4494a7d1f2eea742@kernel.org/
--
2.34.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [RFC PATCH v1 1/1] tracing/kprobes: Return ENAMESVRLSYMS when func matches several symbols
  2023-08-23 16:14 [RFC PATCH v1 0/1] Return ENAMESVRLSYMS when func matches several symbols during PMU kprobe creation Francis Laniel
@ 2023-08-23 16:14 ` Francis Laniel
  2023-08-23 23:53   ` Masami Hiramatsu
  0 siblings, 1 reply; 4+ messages in thread
From: Francis Laniel @ 2023-08-23 16:14 UTC (permalink / raw)
  To: linux-kernel
  Cc: Masami Hiramatsu, linux-trace-kernel, Francis Laniel,
	Richard Henderson, Ivan Kokshaysky, Matt Turner,
	Thomas Bogendoerfer, James E.J. Bottomley, Helge Deller,
	David S. Miller, Arnd Bergmann, Steven Rostedt, linux-alpha,
	linux-mips, linux-parisc, sparclinux, linux-arch

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@kernel.org>
Signed-off-by: Francis Laniel <flaniel@linux.microsoft.com>
Link: https://lore.kernel.org/lkml/20230819101105.b0c104ae4494a7d1f2eea742@kernel.org/
---
 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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [RFC PATCH v1 1/1] tracing/kprobes: Return ENAMESVRLSYMS when func matches several symbols
  2023-08-23 16:14 ` [RFC PATCH v1 1/1] tracing/kprobes: Return ENAMESVRLSYMS when func matches several symbols Francis Laniel
@ 2023-08-23 23:53   ` Masami Hiramatsu
  2023-08-24 10:39     ` Francis Laniel
  0 siblings, 1 reply; 4+ messages in thread
From: Masami Hiramatsu @ 2023-08-23 23:53 UTC (permalink / raw)
  To: Francis Laniel
  Cc: linux-kernel, Masami Hiramatsu, linux-trace-kernel,
	Richard Henderson, Ivan Kokshaysky, Matt Turner,
	Thomas Bogendoerfer, James E.J. Bottomley, Helge Deller,
	David S. Miller, Arnd Bergmann, Steven Rostedt, linux-alpha,
	linux-mips, linux-parisc, sparclinux, linux-arch

Hi Francis,

On Wed, 23 Aug 2023 18:14:10 +0200
Francis Laniel <flaniel@linux.microsoft.com> wrote:

> 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.

The trace_kprobe part looks good to me.
But sorry, I mislead you. I meant using an existing error code as a metaphor.
EINVAL is used everywhere, so choose another error code, e.g. EADDRNOTAVAIL.

Also, can you add this check in __trace_kprobe_create()?
I think right before below code, at that point, 'symbol' has the symbol name.

        trace_probe_log_set_index(0);
        if (event) {
                ret = traceprobe_parse_event_name(&event, &group, gbuf,
                                                  event - argv[0]);
                if (ret)
                        goto parse_error;
        }


Thank you,

> This way, user needs to use addr to remove the ambiguity.
> 
> Suggested-by: Masami Hiramatsu <mhiramat@kernel.org>
> Signed-off-by: Francis Laniel <flaniel@linux.microsoft.com>
> Link: https://lore.kernel.org/lkml/20230819101105.b0c104ae4494a7d1f2eea742@kernel.org/
> ---
>  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
> 


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC PATCH v1 1/1] tracing/kprobes: Return ENAMESVRLSYMS when func matches several symbols
  2023-08-23 23:53   ` Masami Hiramatsu
@ 2023-08-24 10:39     ` Francis Laniel
  0 siblings, 0 replies; 4+ messages in thread
From: Francis Laniel @ 2023-08-24 10:39 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: linux-kernel, Masami Hiramatsu, linux-trace-kernel,
	Richard Henderson, Ivan Kokshaysky, Matt Turner,
	Thomas Bogendoerfer, James E.J. Bottomley, Helge Deller,
	David S. Miller, Arnd Bergmann, Steven Rostedt, linux-alpha,
	linux-mips, linux-parisc, sparclinux, linux-arch

Hi.

Le jeudi 24 août 2023, 01:53:55 CEST Masami Hiramatsu a écrit :
> Hi Francis,
> 
> On Wed, 23 Aug 2023 18:14:10 +0200
> 
> Francis Laniel <flaniel@linux.microsoft.com> wrote:
> > 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.
> 
> The trace_kprobe part looks good to me.
> But sorry, I mislead you. I meant using an existing error code as a
> metaphor. EINVAL is used everywhere, so choose another error code, e.g.
> EADDRNOTAVAIL.

No problem, I was a bit in doubt regarding adding a new error code, but at 
least I learnt how to do it if one day I need to do so!
But yes, for this case, better to use an existing one!

> Also, can you add this check in __trace_kprobe_create()?
> I think right before below code, at that point, 'symbol' has the symbol
> name.
> 
>         trace_probe_log_set_index(0);
>         if (event) {
>                 ret = traceprobe_parse_event_name(&event, &group, gbuf,
>                                                   event - argv[0]);
>                 if (ret)
>                         goto parse_error;
>         }

Addressed in v2, thank you!

> 
> Thank you,
> 
> > This way, user needs to use addr to remove the ambiguity.
> > 
> > Suggested-by: Masami Hiramatsu <mhiramat@kernel.org>
> > Signed-off-by: Francis Laniel <flaniel@linux.microsoft.com>
> > Link:
> > https://lore.kernel.org/lkml/20230819101105.b0c104ae4494a7d1f2eea742@kern
> > el.org/ ---
> > 
> >  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

Best regards.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-08-24 10:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-23 16:14 [RFC PATCH v1 0/1] Return ENAMESVRLSYMS when func matches several symbols during PMU kprobe creation Francis Laniel
2023-08-23 16:14 ` [RFC PATCH v1 1/1] tracing/kprobes: Return ENAMESVRLSYMS when func matches several symbols Francis Laniel
2023-08-23 23:53   ` Masami Hiramatsu
2023-08-24 10:39     ` Francis Laniel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).