netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next] selftests/bpf: guess function end for test_get_branch_snapshot
@ 2021-10-22 23:48 Song Liu
  2021-10-26  4:46 ` Andrii Nakryiko
  0 siblings, 1 reply; 2+ messages in thread
From: Song Liu @ 2021-10-22 23:48 UTC (permalink / raw)
  To: bpf, netdev; +Cc: ast, daniel, andrii, kernel-team, Song Liu

Function in modules could appear in /proc/kallsyms in random order.

ffffffffa02608a0 t bpf_testmod_loop_test
ffffffffa02600c0 t __traceiter_bpf_testmod_test_writable_bare
ffffffffa0263b60 d __tracepoint_bpf_testmod_test_write_bare
ffffffffa02608c0 T bpf_testmod_test_read
ffffffffa0260d08 t __SCT__tp_func_bpf_testmod_test_writable_bare
ffffffffa0263300 d __SCK__tp_func_bpf_testmod_test_read
ffffffffa0260680 T bpf_testmod_test_write
ffffffffa0260860 t bpf_testmod_test_mod_kfunc

Therefore, we cannot reliably use kallsyms_find_next() to find the end of
a function. Replace it with a simple guess (start + 128). This is good
enough for this test.

Signed-off-by: Song Liu <songliubraving@fb.com>
---
 .../bpf/prog_tests/get_branch_snapshot.c      |  7 ++--
 tools/testing/selftests/bpf/trace_helpers.c   | 36 -------------------
 tools/testing/selftests/bpf/trace_helpers.h   |  5 ---
 3 files changed, 4 insertions(+), 44 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c b/tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c
index d6d70a359aeb5..bc2b6e6d167d4 100644
--- a/tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c
+++ b/tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c
@@ -91,9 +91,10 @@ void serial_test_get_branch_snapshot(void)
 	if (!ASSERT_OK(err, "kallsyms_find"))
 		goto cleanup;
 
-	err = kallsyms_find_next("bpf_testmod_loop_test", &skel->bss->address_high);
-	if (!ASSERT_OK(err, "kallsyms_find_next"))
-		goto cleanup;
+	/* Just a guess for the end of this function, as module functions
+	 * in /proc/kallsyms could come in any order.
+	 */
+	skel->bss->address_high = skel->bss->address_low + 128;
 
 	err = get_branch_snapshot__attach(skel);
 	if (!ASSERT_OK(err, "get_branch_snapshot__attach"))
diff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c
index 5100a169b72b1..7b7f918eda776 100644
--- a/tools/testing/selftests/bpf/trace_helpers.c
+++ b/tools/testing/selftests/bpf/trace_helpers.c
@@ -118,42 +118,6 @@ int kallsyms_find(const char *sym, unsigned long long *addr)
 	return err;
 }
 
-/* find the address of the next symbol of the same type, this can be used
- * to determine the end of a function.
- */
-int kallsyms_find_next(const char *sym, unsigned long long *addr)
-{
-	char type, found_type, name[500];
-	unsigned long long value;
-	bool found = false;
-	int err = 0;
-	FILE *f;
-
-	f = fopen("/proc/kallsyms", "r");
-	if (!f)
-		return -EINVAL;
-
-	while (fscanf(f, "%llx %c %499s%*[^\n]\n", &value, &type, name) > 0) {
-		/* Different types of symbols in kernel modules are mixed
-		 * in /proc/kallsyms. Only return the next matching type.
-		 * Use tolower() for type so that 'T' matches 't'.
-		 */
-		if (found && found_type == tolower(type)) {
-			*addr = value;
-			goto out;
-		}
-		if (strcmp(name, sym) == 0) {
-			found = true;
-			found_type = tolower(type);
-		}
-	}
-	err = -ENOENT;
-
-out:
-	fclose(f);
-	return err;
-}
-
 void read_trace_pipe(void)
 {
 	int trace_fd;
diff --git a/tools/testing/selftests/bpf/trace_helpers.h b/tools/testing/selftests/bpf/trace_helpers.h
index bc8ed86105d94..d907b445524d5 100644
--- a/tools/testing/selftests/bpf/trace_helpers.h
+++ b/tools/testing/selftests/bpf/trace_helpers.h
@@ -16,11 +16,6 @@ long ksym_get_addr(const char *name);
 /* open kallsyms and find addresses on the fly, faster than load + search. */
 int kallsyms_find(const char *sym, unsigned long long *addr);
 
-/* find the address of the next symbol, this can be used to determine the
- * end of a function
- */
-int kallsyms_find_next(const char *sym, unsigned long long *addr);
-
 void read_trace_pipe(void);
 
 ssize_t get_uprobe_offset(const void *addr, ssize_t base);
-- 
2.30.2


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

* Re: [PATCH bpf-next] selftests/bpf: guess function end for test_get_branch_snapshot
  2021-10-22 23:48 [PATCH bpf-next] selftests/bpf: guess function end for test_get_branch_snapshot Song Liu
@ 2021-10-26  4:46 ` Andrii Nakryiko
  0 siblings, 0 replies; 2+ messages in thread
From: Andrii Nakryiko @ 2021-10-26  4:46 UTC (permalink / raw)
  To: Song Liu
  Cc: bpf, Networking, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Kernel Team

On Fri, Oct 22, 2021 at 4:48 PM Song Liu <songliubraving@fb.com> wrote:
>
> Function in modules could appear in /proc/kallsyms in random order.
>
> ffffffffa02608a0 t bpf_testmod_loop_test
> ffffffffa02600c0 t __traceiter_bpf_testmod_test_writable_bare
> ffffffffa0263b60 d __tracepoint_bpf_testmod_test_write_bare
> ffffffffa02608c0 T bpf_testmod_test_read
> ffffffffa0260d08 t __SCT__tp_func_bpf_testmod_test_writable_bare
> ffffffffa0263300 d __SCK__tp_func_bpf_testmod_test_read
> ffffffffa0260680 T bpf_testmod_test_write
> ffffffffa0260860 t bpf_testmod_test_mod_kfunc
>
> Therefore, we cannot reliably use kallsyms_find_next() to find the end of
> a function. Replace it with a simple guess (start + 128). This is good
> enough for this test.

It's so annoying that sizes of all those symbols are known and
available in ELF, yet kallsyms don't expose them :( I've applied this
"fix", but it makes me very sad.

>
> Signed-off-by: Song Liu <songliubraving@fb.com>
> ---
>  .../bpf/prog_tests/get_branch_snapshot.c      |  7 ++--
>  tools/testing/selftests/bpf/trace_helpers.c   | 36 -------------------
>  tools/testing/selftests/bpf/trace_helpers.h   |  5 ---
>  3 files changed, 4 insertions(+), 44 deletions(-)
>

[...]

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

end of thread, other threads:[~2021-10-26  4:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-22 23:48 [PATCH bpf-next] selftests/bpf: guess function end for test_get_branch_snapshot Song Liu
2021-10-26  4:46 ` Andrii Nakryiko

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