Return ENAMESVRLSYMS when func matches several symbols during PMU kprobe creation
Subsystems (6)
cover Return ENAMESVRLSYMS when func matches several symbols during PMU kprobe creation
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/[email protected]/
[2]: https://lore.kernel.org/lkml/[email protected]/
--
2.34.1
RFC PATCH 1/1 tracing/kprobes: Return ENAMESVRLSYMS when func matches several symbols
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.
--- 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(+)
arch/alpha/include/uapi/asm/errno.h +2 -0
@@ -125,4 +125,6 @@#define EHWPOISON 139/* Memory page has hardware error */ +#define ENAMESVRLSYMS 140/* Name correspond to several symbols */ +#endif
arch/mips/include/uapi/asm/errno.h +2 -0
@@ -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 */
arch/parisc/include/uapi/asm/errno.h +2 -0
@@ -124,4 +124,6 @@#define EHWPOISON 257/* Memory page has hardware error */ +#define ENAMESVRLSYMS 258/* Name correspond to several symbols */ +#endif
arch/sparc/include/uapi/asm/errno.h +2 -0
@@ -115,4 +115,6 @@#define EHWPOISON 135/* Memory page has hardware error */ +#define ENAMESVRLSYMS 136/* Name correspond to several symbols */ +#endif
include/uapi/asm-generic/errno.h +2 -0
@@ -120,4 +120,6 @@#define EHWPOISON 133/* Memory page has hardware error */ +#define ENAMESVRLSYMS 134/* Name correspond to several symbols */ +#endif
kernel/trace/trace_kprobe.c +26 -0
@@ -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
tools/arch/alpha/include/uapi/asm/errno.h +2 -0
@@ -125,4 +125,6 @@#define EHWPOISON 139/* Memory page has hardware error */ +#define ENAMESVRLSYMS 140/* Name correspond to several symbols */ +#endif
tools/arch/mips/include/uapi/asm/errno.h +2 -0
@@ -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 */
tools/arch/parisc/include/uapi/asm/errno.h +2 -0
@@ -124,4 +124,6 @@#define EHWPOISON 257/* Memory page has hardware error */ +#define ENAMESVRLSYMS 258/* Name correspond to several symbols */ +#endif
tools/arch/sparc/include/uapi/asm/errno.h +2 -0
@@ -115,4 +115,6 @@#define EHWPOISON 135/* Memory page has hardware error */ +#define ENAMESVRLSYMS 136/* Name correspond to several symbols */ +#endif
tools/include/uapi/asm-generic/errno.h +2 -0
@@ -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)
maintainerRe: [RFC PATCH v1 1/1] tracing/kprobes: Return ENAMESVRLSYMS when func matches several symbols
Hi Francis,
Quoted 8 lines · On Wed, 23 Aug 2023 18:14:10 +0200 Francis Laniel…
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,
Quoted 180 lines
This way, user needs to use addr to remove the ambiguity.
Suggested-by Masami Hiramatsu <[email protected]>Signed-off-by Francis Laniel <[email protected]>--- 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(+)arch/alpha/include/uapi/asm/errno.h +2
-0@@ -125,4 +125,6 @@#define EHWPOISON 139/* Memory page has hardware error */ +#define ENAMESVRLSYMS 140/* Name correspond to several symbols */ +#endif arch/mips/include/uapi/asm/errno.h +2
-0@@ -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 */ arch/parisc/include/uapi/asm/errno.h +2
-0@@ -124,4 +124,6 @@#define EHWPOISON 257/* Memory page has hardware error */ +#define ENAMESVRLSYMS 258/* Name correspond to several symbols */ +#endif arch/sparc/include/uapi/asm/errno.h +2
-0@@ -115,4 +115,6 @@#define EHWPOISON 135/* Memory page has hardware error */ +#define ENAMESVRLSYMS 136/* Name correspond to several symbols */ +#endif include/uapi/asm-generic/errno.h +2
-0@@ -120,4 +120,6 @@#define EHWPOISON 133/* Memory page has hardware error */ +#define ENAMESVRLSYMS 134/* Name correspond to several symbols */ +#endif kernel/trace/trace_kprobe.c +26
-0@@ -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 tools/arch/alpha/include/uapi/asm/errno.h +2
-0@@ -125,4 +125,6 @@#define EHWPOISON 139/* Memory page has hardware error */ +#define ENAMESVRLSYMS 140/* Name correspond to several symbols */ +#endif tools/arch/mips/include/uapi/asm/errno.h +2
-0@@ -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 */ tools/arch/parisc/include/uapi/asm/errno.h +2
-0@@ -124,4 +124,6 @@#define EHWPOISON 257/* Memory page has hardware error */ +#define ENAMESVRLSYMS 258/* Name correspond to several symbols */ +#endif tools/arch/sparc/include/uapi/asm/errno.h +2
-0@@ -115,4 +115,6 @@#define EHWPOISON 135/* Memory page has hardware error */ +#define ENAMESVRLSYMS 136/* Name correspond to several symbols */ +#endif tools/include/uapi/asm-generic/errno.h +2
-1@@ -120,4 +120,6 @@#define EHWPOISON 133/* Memory page has hardware error */ +#define ENAMESVRLSYMS 134/* Name correspond to several symbols */ +#endif --2.34.1
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Re: [RFC PATCH v1 1/1] tracing/kprobes: Return ENAMESVRLSYMS when func matches several symbols
Hi.
Quoted 11 lines · 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
Quoted 6 lines · Francis Laniel
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.
Quoted 7 lines
Quoted 2 lines
So, this commit introduces ENAMESVRLSYMS which is returned when func matches several symbols.
The
trace_kprobepart 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!
Quoted 11 lines
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!
Quoted 124 lines
Thank you,
Quoted 121 lines
This way, user needs to use addr to remove the ambiguity.
Suggested-by Masami Hiramatsu <[email protected]>Signed-off-by Francis Laniel <[email protected]>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(+)(quoted diff) +0
-0
b/arch/alpha/include/uapi/asm/errno.hindex3d265f6babaf..3d9686d915f9100644arch/alpha/include/uapi/asm/errno.h +2
-0@@ -125,4 +125,6 @@#define EHWPOISON 139/* Memory page has hardware error */ +#define ENAMESVRLSYMS 140/* Name correspond to several symbols */ +#endif
(quoted diff) +0
-0
b/arch/mips/include/uapi/asm/errno.hindex2fb714e2d6d8..1fd64ee7b629100644arch/mips/include/uapi/asm/errno.h +2
-0@@ -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 */ (quoted diff) +0
-0
b/arch/parisc/include/uapi/asm/errno.hindex87245c584784..c7845ceece26100644arch/parisc/include/uapi/asm/errno.h +2
-0@@ -124,4 +124,6 @@#define EHWPOISON 257/* Memory page has hardware error */ +#define ENAMESVRLSYMS 258/* Name correspond to several symbols */ +#endif
(quoted diff) +0
-0
b/arch/sparc/include/uapi/asm/errno.hindex81a732b902ee..1ed065943bab100644arch/sparc/include/uapi/asm/errno.h +2
-0@@ -115,4 +115,6 @@#define EHWPOISON 135/* Memory page has hardware error */ +#define ENAMESVRLSYMS 136/* Name correspond to several symbols */ +#endif
(quoted diff) +0
-0
b/include/uapi/asm-generic/errno.hindexcf9c51ac49f9..3d5d5740c8da100644include/uapi/asm-generic/errno.h +2
-0@@ -120,4 +120,6 @@#define EHWPOISON 133/* Memory page has hardware error */ +#define ENAMESVRLSYMS 134/* Name correspond to several symbols */ +#endif
kernel/trace/trace_kprobe.c +0
-0@@ -1699,6 +1699,16 @@ static int unregister_kprobe_event(struct
trace_kprobe*tk)>
}#ifdef
CONFIG_PERF_EVENTSkernel/trace/trace_kprobe.c +10
-0+ +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,>
kernel/trace/trace_kprobe.c +0
-0int ret;char *event;
Quoted 5 lines
kernel/trace/trace_kprobe.c create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
Quoted 5 lines
kernel/trace/trace_kprobe.c +5
-0+/* +* 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
Quoted 82 lines
Quoted 82 lines
kernel/trace/trace_kprobe.c +11
-0+* 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 (quoted diff) +0
-0
b/tools/arch/alpha/include/uapi/asm/errno.hindex3d265f6babaf..3d9686d915f9100644tools/arch/alpha/include/uapi/asm/errno.h +2
-0@@ -125,4 +125,6 @@#define EHWPOISON 139/* Memory page has hardware error */ +#define ENAMESVRLSYMS 140/* Name correspond to several symbols */ +#endif
(quoted diff) +0
-0
b/tools/arch/mips/include/uapi/asm/errno.hindex2fb714e2d6d8..1fd64ee7b629100644tools/arch/mips/include/uapi/asm/errno.h +2
-0@@ -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 */ (quoted diff) +0
-0
b/tools/arch/parisc/include/uapi/asm/errno.hindex87245c584784..c7845ceece26100644tools/arch/parisc/include/uapi/asm/errno.h +2
-0@@ -124,4 +124,6 @@#define EHWPOISON 257/* Memory page has hardware error */ +#define ENAMESVRLSYMS 258/* Name correspond to several symbols */ +#endif
(quoted diff) +0
-0
b/tools/arch/sparc/include/uapi/asm/errno.hindex81a732b902ee..1ed065943bab100644tools/arch/sparc/include/uapi/asm/errno.h +2
-0@@ -115,4 +115,6 @@#define EHWPOISON 135/* Memory page has hardware error */ +#define ENAMESVRLSYMS 136/* Name correspond to several symbols */ +#endif
(quoted diff) +0
-0
b/tools/include/uapi/asm-generic/errno.hindexcf9c51ac49f9..3d5d5740c8da100644tools/include/uapi/asm-generic/errno.h +0
-0@@ -120,4 +120,6 @@#define EHWPOISON 133/* Memory page has hardware error */
Quoted 7 lines
Quoted 7 lines
tools/include/uapi/asm-generic/errno.h +2
-0+#define ENAMESVRLSYMS 134/* Name correspond to several symbols */ +#endif
--
2.34.1
Best regards.