diff mbox series

[bpf-next] selftests/bpf: Ignore .llvm.<hash> suffix in kallsyms_find()

Message ID 20240604175546.1339303-1-yonghong.song@linux.dev (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series [bpf-next] selftests/bpf: Ignore .llvm.<hash> suffix in kallsyms_find() | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
netdev/tree_selection success Clearly marked for bpf-next
netdev/apply success Patch already applied to bpf-next-0

Commit Message

Yonghong Song June 4, 2024, 5:55 p.m. UTC
I hit the following failure when running selftests with
internal backported upstream kernel:
  test_ksyms:PASS:kallsyms_fopen 0 nsec
  test_ksyms:FAIL:ksym_find symbol 'bpf_link_fops' not found
  #123     ksyms:FAIL

In /proc/kallsyms, we have
  $ cat /proc/kallsyms | grep bpf_link_fops
  ffffffff829f0cb0 d bpf_link_fops.llvm.12608678492448798416
The CONFIG_LTO_CLANG_THIN is enabled in the kernel which is responsible
for bpf_link_fops.llvm.12608678492448798416 symbol name.

In prog_tests/ksyms.c we have
  kallsyms_find("bpf_link_fops", &link_fops_addr)
and kallsyms_find() compares "bpf_link_fops" with symbols
in /proc/kallsyms in order to find the entry. With
bpf_link_fops.llvm.<hash> in /proc/kallsyms, the kallsyms_find()
failed.

To fix the issue, in kallsyms_find(), if a symbol has suffix
.llvm.<hash>, that suffix will be ignored for comparison.
This fixed the test failure.

Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
 tools/testing/selftests/bpf/trace_helpers.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

Comments

Yonghong Song June 4, 2024, 5:58 p.m. UTC | #1
Sorry, messed up the git submit with two unrelated patches. Please ignore this.
Will submit the correct one soon.

On 6/4/24 10:55 AM, Yonghong Song wrote:
> I hit the following failure when running selftests with
> internal backported upstream kernel:
>    test_ksyms:PASS:kallsyms_fopen 0 nsec
>    test_ksyms:FAIL:ksym_find symbol 'bpf_link_fops' not found
>    #123     ksyms:FAIL
>
> In /proc/kallsyms, we have
>    $ cat /proc/kallsyms | grep bpf_link_fops
>    ffffffff829f0cb0 d bpf_link_fops.llvm.12608678492448798416
> The CONFIG_LTO_CLANG_THIN is enabled in the kernel which is responsible
> for bpf_link_fops.llvm.12608678492448798416 symbol name.
>
> In prog_tests/ksyms.c we have
>    kallsyms_find("bpf_link_fops", &link_fops_addr)
> and kallsyms_find() compares "bpf_link_fops" with symbols
> in /proc/kallsyms in order to find the entry. With
> bpf_link_fops.llvm.<hash> in /proc/kallsyms, the kallsyms_find()
> failed.
>
> To fix the issue, in kallsyms_find(), if a symbol has suffix
> .llvm.<hash>, that suffix will be ignored for comparison.
> This fixed the test failure.
>
> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
> ---
>   tools/testing/selftests/bpf/trace_helpers.c | 12 ++++++++++++
>   1 file changed, 12 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c
> index 70e29f316fe7..dc871e642ed5 100644
> --- a/tools/testing/selftests/bpf/trace_helpers.c
> +++ b/tools/testing/selftests/bpf/trace_helpers.c
> @@ -221,6 +221,18 @@ int kallsyms_find(const char *sym, unsigned long long *addr)
>   		return -EINVAL;
>   
>   	while (fscanf(f, "%llx %c %499s%*[^\n]\n", &value, &type, name) > 0) {
> +		/* If CONFIG_LTO_CLANG_THIN is enabled, static variable/function
> +		 * symbols could be promoted to global due to cross-file inlining.
> +		 * For such cases, clang compiler will add .llvm.<hash> suffix
> +		 * to those symbols to avoid potential naming conflict.
> +		 * Let us ignore .llvm.<hash> suffix during symbol comparison.
> +		 */
> +		if (type == 'd') {
> +			char *res = strstr(name, ".llvm.");
> +
> +			if (res)
> +				*res = '\0';
> +		}
>   		if (strcmp(name, sym) == 0) {
>   			*addr = value;
>   			goto out;
Andrii Nakryiko June 4, 2024, 8:01 p.m. UTC | #2
On Tue, Jun 4, 2024 at 10:56 AM Yonghong Song <yonghong.song@linux.dev> wrote:
>
> I hit the following failure when running selftests with
> internal backported upstream kernel:
>   test_ksyms:PASS:kallsyms_fopen 0 nsec
>   test_ksyms:FAIL:ksym_find symbol 'bpf_link_fops' not found
>   #123     ksyms:FAIL
>
> In /proc/kallsyms, we have
>   $ cat /proc/kallsyms | grep bpf_link_fops
>   ffffffff829f0cb0 d bpf_link_fops.llvm.12608678492448798416
> The CONFIG_LTO_CLANG_THIN is enabled in the kernel which is responsible
> for bpf_link_fops.llvm.12608678492448798416 symbol name.
>
> In prog_tests/ksyms.c we have
>   kallsyms_find("bpf_link_fops", &link_fops_addr)
> and kallsyms_find() compares "bpf_link_fops" with symbols
> in /proc/kallsyms in order to find the entry. With
> bpf_link_fops.llvm.<hash> in /proc/kallsyms, the kallsyms_find()
> failed.
>
> To fix the issue, in kallsyms_find(), if a symbol has suffix
> .llvm.<hash>, that suffix will be ignored for comparison.
> This fixed the test failure.
>
> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
> ---
>  tools/testing/selftests/bpf/trace_helpers.c | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c
> index 70e29f316fe7..dc871e642ed5 100644
> --- a/tools/testing/selftests/bpf/trace_helpers.c
> +++ b/tools/testing/selftests/bpf/trace_helpers.c
> @@ -221,6 +221,18 @@ int kallsyms_find(const char *sym, unsigned long long *addr)
>                 return -EINVAL;
>
>         while (fscanf(f, "%llx %c %499s%*[^\n]\n", &value, &type, name) > 0) {
> +               /* If CONFIG_LTO_CLANG_THIN is enabled, static variable/function
> +                * symbols could be promoted to global due to cross-file inlining.
> +                * For such cases, clang compiler will add .llvm.<hash> suffix
> +                * to those symbols to avoid potential naming conflict.
> +                * Let us ignore .llvm.<hash> suffix during symbol comparison.
> +                */
> +               if (type == 'd') {
> +                       char *res = strstr(name, ".llvm.");

I declared this variable at the top of the function to remove this
empty line (and renamed it to "match"), applied to bpf-next, thanks!

> +
> +                       if (res)
> +                               *res = '\0';
> +               }
>                 if (strcmp(name, sym) == 0) {
>                         *addr = value;
>                         goto out;
> --
> 2.43.0
>
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c
index 70e29f316fe7..dc871e642ed5 100644
--- a/tools/testing/selftests/bpf/trace_helpers.c
+++ b/tools/testing/selftests/bpf/trace_helpers.c
@@ -221,6 +221,18 @@  int kallsyms_find(const char *sym, unsigned long long *addr)
 		return -EINVAL;
 
 	while (fscanf(f, "%llx %c %499s%*[^\n]\n", &value, &type, name) > 0) {
+		/* If CONFIG_LTO_CLANG_THIN is enabled, static variable/function
+		 * symbols could be promoted to global due to cross-file inlining.
+		 * For such cases, clang compiler will add .llvm.<hash> suffix
+		 * to those symbols to avoid potential naming conflict.
+		 * Let us ignore .llvm.<hash> suffix during symbol comparison.
+		 */
+		if (type == 'd') {
+			char *res = strstr(name, ".llvm.");
+
+			if (res)
+				*res = '\0';
+		}
 		if (strcmp(name, sym) == 0) {
 			*addr = value;
 			goto out;