Skip to contentSkip to conversation

btf_encoder: Check var type after checking var addr.

PATCH In review Hao Luo 4 participants 4 messages Oct 26, 2020 – Oct 27, 2020

lore
Share & tools
lore

Hao Luo

author
#0 lore raw

PATCH 1/1 btf_encoder: Check var type after checking var addr.

Commit 2e719cc ("btf_encoder: revamp how per-CPU variables are encoded") adds percpu_var_exists() to filter out the symbols that are not percpu var. However, the check comes after checking the var's type. There can be symbols that are of zero type. If we hit that, btf_encoder will not work without '--btf_encode_force'. So we should check percpu_var_exists before checking var's type.

Tested:

 haoluo@haoluo:~/kernel/tip$ gcc --version
 gcc (GCC) 10.2.0
 Copyright (C) 2020 Free Software Foundation, Inc.
 This is free software; see the source for copying conditions.  There is NO
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Before:

 haoluo@haoluo:~/kernel/tip$ make clean -s
 haoluo@haoluo:~/kernel/tip$ make -j 32 -s
   LINK     resolve_btfids
 error: found variable in CU 'kernel/bpf/btf.c' that has void type
 Encountered error while encoding BTF.
 FAILED: load BTF from vmlinux: Unknown error -2make: *** [Makefile:1164: vmlinux] Error 255

After:

 haoluo@haoluo:~/kernel/tip$ make clean -s
 haoluo@haoluo:~/kernel/tip$ make -j 32 -s
   LINK     resolve_btfids
 haoluo@haoluo:~/kernel/tip$
Fixes 2e719cc ("btf_encoder: revamp how per-CPU variables are encoded")
---
 btf_encoder.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
btf_encoder.c +5 -4
index 2a6455b..b5f8f3e 100644
@@ -441,9 +441,12 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,
 		if (var->spec)
 			var = var->spec;
 
+		if (!percpu_var_exists(addr, &size, &name))
+			continue; /* not a per-CPU variable */
+
 		if (var->ip.tag.type == 0) {
-			fprintf(stderr, "error: found variable in CU '%s' that has void type\n",
-				cu->name);
+			fprintf(stderr, "error: found variable '%s' in CU '%s' that has void type\n",
+				name, cu->name);
 			if (force)
 				continue;
 			err = -1;
@@ -452,8 +455,6 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,
 
 		type = var->ip.tag.type + type_id_off;
 		linkage = var->external ? BTF_VAR_GLOBAL_ALLOCATED : BTF_VAR_STATIC;
-		if (!percpu_var_exists(addr, &size, &name))
-			continue; /* not a per-CPU variable */
 
 		if (btf_elf__verbose) {
 			printf("Variable '%s' from CU '%s' at address 0x%lx encoded\n",
Signature · 1 line
2.29.0.rc1.297.gfa9743e501-goog
Acked-by on v1 1/1
#1 lore raw
Quoted 26 lines · Hao Luo

Commit 2e719cc ("btf_encoder: revamp how per-CPU variables are encoded") adds percpu_var_exists() to filter out the symbols that are not percpu var. However, the check comes after checking the var's type. There can be symbols that are of zero type. If we hit that, btf_encoder will not work without '--btf_encode_force'. So we should check percpu_var_exists before checking var's type.

Tested:

 haoluo@haoluo:~/kernel/tip$ gcc --version
 gcc (GCC) 10.2.0
 Copyright (C) 2020 Free Software Foundation, Inc.
 This is free software; see the source for copying conditions.  There is NO
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Before:

 haoluo@haoluo:~/kernel/tip$ make clean -s
 haoluo@haoluo:~/kernel/tip$ make -j 32 -s
   LINK     resolve_btfids
 error: found variable in CU 'kernel/bpf/btf.c' that has void type
 Encountered error while encoding BTF.
 FAILED: load BTF from vmlinux: Unknown error -2make: *** [Makefile:1164: vmlinux] Error 255

After:

Quoted 8 lines
 haoluo@haoluo:~/kernel/tip$ make clean -s
 haoluo@haoluo:~/kernel/tip$ make -j 32 -s
   LINK     resolve_btfids
 haoluo@haoluo:~/kernel/tip$
Fixes 2e719cc ("btf_encoder: revamp how per-CPU variables are encoded")

---

Looks good, thanks for the prompt fix!

Quoted 34 lines
 btf_encoder.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
btf_encoder.c +5 -5
index 2a6455b..b5f8f3e 100644
@@ -441,9 +441,12 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,
                if (var->spec)
                        var = var->spec;
 
+               if (!percpu_var_exists(addr, &size, &name))
+                       continue; /* not a per-CPU variable */
+
                if (var->ip.tag.type == 0) {
-                       fprintf(stderr, "error: found variable in CU '%s' that has void type\n",
-                               cu->name);
+                       fprintf(stderr, "error: found variable '%s' in CU '%s' that has void type\n",
+                               name, cu->name);
                        if (force)
                                continue;
                        err = -1;
@@ -452,8 +455,6 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,
 
                type = var->ip.tag.type + type_id_off;
                linkage = var->external ? BTF_VAR_GLOBAL_ALLOCATED : BTF_VAR_STATIC;
-               if (!percpu_var_exists(addr, &size, &name))
-                       continue; /* not a per-CPU variable */
 
                if (btf_elf__verbose) {
                        printf("Variable '%s' from CU '%s' at address 0x%lx encoded\n",
--

2.29.0.rc1.297.gfa9743e501-goog

#2 lore raw
In reply to Hao Luo

Em Mon, Oct 26, 2020 at 03:18:22PM -0700, Hao Luo escreveu:

Quoted 23 lines

Commit 2e719cc ("btf_encoder: revamp how per-CPU variables are encoded") adds percpu_var_exists() to filter out the symbols that are not percpu var. However, the check comes after checking the var's type. There can be symbols that are of zero type. If we hit that, btf_encoder will not work without '--btf_encode_force'. So we should check percpu_var_exists before checking var's type.

Tested:

 haoluo@haoluo:~/kernel/tip$ gcc --version
 gcc (GCC) 10.2.0
 Copyright (C) 2020 Free Software Foundation, Inc.
 This is free software; see the source for copying conditions.  There is NO
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Before:

 haoluo@haoluo:~/kernel/tip$ make clean -s
 haoluo@haoluo:~/kernel/tip$ make -j 32 -s
   LINK     resolve_btfids
 error: found variable in CU 'kernel/bpf/btf.c' that has void type
 Encountered error while encoding BTF.
 FAILED: load BTF from vmlinux: Unknown error -2make: *** [Makefile:1164: vmlinux] Error 255
Quoted 6 lines

After:

 haoluo@haoluo:~/kernel/tip$ make clean -s
 haoluo@haoluo:~/kernel/tip$ make -j 32 -s
   LINK     resolve_btfids
 haoluo@haoluo:~/kernel/tip$

Thanks, tested and applied.

- Arnaldo

Quoted 37 lines
Fixes 2e719cc ("btf_encoder: revamp how per-CPU variables are encoded")
---
 btf_encoder.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
btf_encoder.c +5 -4
index 2a6455b..b5f8f3e 100644
@@ -441,9 +441,12 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,
 		if (var->spec)
 			var = var->spec;
 
+		if (!percpu_var_exists(addr, &size, &name))
+			continue; /* not a per-CPU variable */
+
 		if (var->ip.tag.type == 0) {
-			fprintf(stderr, "error: found variable in CU '%s' that has void type\n",
-				cu->name);
+			fprintf(stderr, "error: found variable '%s' in CU '%s' that has void type\n",
+				name, cu->name);
 			if (force)
 				continue;
 			err = -1;
@@ -452,8 +455,6 @@ int cu__encode_btf(struct cu *cu, int verbose, bool force,
 
 		type = var->ip.tag.type + type_id_off;
 		linkage = var->external ? BTF_VAR_GLOBAL_ALLOCATED : BTF_VAR_STATIC;
-		if (!percpu_var_exists(addr, &size, &name))
-			continue; /* not a per-CPU variable */
 
 		if (btf_elf__verbose) {
 			printf("Variable '%s' from CU '%s' at address 0x%lx encoded\n",
Signature · 1 line
2.29.0.rc1.297.gfa9743e501-goog
Signature · 1 line
- Arnaldo
Tested-by on v1 1/1
#3 lore raw
In reply to Hao Luo
Quoted 25 lines · Hao Luo

Commit 2e719cc ("btf_encoder: revamp how per-CPU variables are encoded") adds percpu_var_exists() to filter out the symbols that are not percpu var. However, the check comes after checking the var's type. There can be symbols that are of zero type. If we hit that, btf_encoder will not work without '--btf_encode_force'. So we should check percpu_var_exists before checking var's type.

Tested:

 haoluo@haoluo:~/kernel/tip$ gcc --version
 gcc (GCC) 10.2.0
 Copyright (C) 2020 Free Software Foundation, Inc.
 This is free software; see the source for copying conditions.  There is NO
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Before:

 haoluo@haoluo:~/kernel/tip$ make clean -s
 haoluo@haoluo:~/kernel/tip$ make -j 32 -s
   LINK     resolve_btfids
 error: found variable in CU 'kernel/bpf/btf.c' that has void type
 Encountered error while encoding BTF.
 FAILED: load BTF from vmlinux: Unknown error -2make: *** [Makefile:1164: vmlinux] Error 255

After:

Quoted 7 lines
 haoluo@haoluo:~/kernel/tip$ make clean -s
 haoluo@haoluo:~/kernel/tip$ make -j 32 -s
   LINK     resolve_btfids
 haoluo@haoluo:~/kernel/tip$
Fixes 2e719cc ("btf_encoder: revamp how per-CPU variables are encoded")

it fixes the build issue for me

thanks,
jirka