libbpf: Return btf_fd in libbpf__probe_raw_btf
Subsystems (4)
PATCH 1/1 libbpf: Return btf_fd in libbpf__probe_raw_btf
Function load_sk_storage_btf expects that libbpf__probe_raw_btf is returning a btf descriptor, but before this change it was returning an information about whether the probe was successful (0 or 1). load_sk_storage_btf was using that value as an argument to the close function, which was resulting in closing stdout and thus terminating the process which used that dunction.
That bug was visible in bpftool. bpftool feature subcommand was always exiting too early (because of closed stdout) and it didn't display all requested probes. bpftool -j feature or bpftool -p feature were not returning a valid json object.
--- tools/lib/bpf/libbpf.c | 36 +++++++++++++++++++++-------------- tools/lib/bpf/libbpf_probes.c | 7 +------ 2 files changed, 23 insertions(+), 20 deletions(-)
tools/lib/bpf/libbpf.c +22 -14
@@ -1645,15 +1645,19 @@ static int bpf_object__probe_btf_func(struct bpf_object *obj)/* FUNC x */ /* [3] */ BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2), }; -int res; +int btf_fd; +int ret; - res =libbpf__probe_raw_btf ((char *)types,sizeof (types), - strs,sizeof (strs)); -if (res <0 ) -return res; -if (res >0 ) + btf_fd =libbpf__probe_raw_btf ((char *)types,sizeof (types), + strs,sizeof (strs)); +if (btf_fd <0 ) + ret =0 ; +else { + ret =1 ; obj->caps.btf_func =1 ; -return 0 ; + } +close (btf_fd); +return ret; }static int bpf_object__probe_btf_datasec (struct bpf_object *obj)
@@ -1670,15 +1674,19 @@ static int bpf_object__probe_btf_datasec(struct bpf_object *obj)BTF_TYPE_ENC (3 ,BTF_INFO_ENC (BTF_KIND_DATASEC ,0 ,1 ),4 ), BTF_VAR_SECINFO_ENC(2, 0, 4), }; -int res; +int btf_fd; +int ret; - res =libbpf__probe_raw_btf ((char *)types,sizeof (types), - strs,sizeof (strs)); -if (res <0 ) -return res; -if (res >0 ) + btf_fd =libbpf__probe_raw_btf ((char *)types,sizeof (types), + strs,sizeof (strs)); +if (btf_fd <0 ) + ret =0 ; +else { + ret =1 ; obj->caps.btf_datasec =1 ; -return 0 ; + } +close (btf_fd); +return ret; } static int
tools/lib/bpf/libbpf_probes.c +1 -6
@@ -157,14 +157,9 @@ int libbpf__probe_raw_btf(const char *raw_types, size_t types_len,memcpy (raw_btf + hdr.hdr_len + hdr.type_len, str_sec, hdr.str_len); btf_fd =bpf_load_btf (raw_btf, btf_len,NULL ,0 , false); -if (btf_fd <0 ) { -free (raw_btf); -return 0 ; - } -close (btf_fd);free (raw_btf); -return 1 ; +return btf_fd; }static int load_sk_storage_btf (void )
Signature · 1 line
2.21.0
Hello!
Quoted 6 lines · Michal Rostecki
Function
load_sk_storage_btfexpects thatlibbpf__probe_raw_btfis returning a btf descriptor, but before this change it was returning an information about whether the probe was successful (0 or 1).load_sk_storage_btfwas using that value as an argument to the close function, which was resulting in closing stdout and thus terminating the process which used that dunction.
Function? :-)
Quoted 7 lines
That bug was visible in bpftool.
bpftool featuresubcommand was always exiting too early (because of closed stdout) and it didn't display all requested probes.bpftool -j featureorbpftool -p featurewere not returning a valid json object.Fixes d7c4b3980c18 ("libbpf: detect supported kernel BTF features and sanitize BTF")Signed-off-by Michal Rostecki <[email protected]>
[...]
MBR, Sergei
Quoted 4 lines · Sergei Shtylyov
Hello!
On 29.05.2019 11:29, Michal Rostecki wrote:
Quoted 8 lines
Quoted 6 lines
Function
load_sk_storage_btfexpects thatlibbpf__probe_raw_btfis returning a btf descriptor, but before this change it was returning an information about whether the probe was successful (0 or 1).load_sk_storage_btfwas using that value as an argument to the close function, which was resulting in closing stdout and thus terminating the process which used that dunction.Function? :-)
Opps! I will fix in v2. Thanks!
Quoted 8 lines · Michal Rostecki
Function
load_sk_storage_btfexpects thatlibbpf__probe_raw_btfis returning a btf descriptor, but before this change it was returning an information about whether the probe was successful (0 or 1).load_sk_storage_btfwas using that value as an argument to the close function, which was resulting in closing stdout and thus terminating the process which used that dunction.
Quoted 5 lines
That bug was visible in bpftool.
bpftool featuresubcommand was always exiting too early (because of closed stdout) and it didn't display all requested probes.bpftool -j featureorbpftool -p featurewere not returning a valid json object.
Thanks for the fix!
Quoted 20 lines
Fixes d7c4b3980c18 ("libbpf: detect supported kernel BTF features and sanitize BTF")Signed-off-by Michal Rostecki <[email protected]>--- tools/lib/bpf/libbpf.c | 36 +++++++++++++++++++++-------------- tools/lib/bpf/libbpf_probes.c | 7 +------ 2 files changed, 23 insertions(+), 20 deletions(-)tools/lib/bpf/libbpf.c +2
-2@@ -1645,15 +1645,19 @@ static int bpf_object__probe_btf_func(struct bpf_object *obj)/* FUNC x */ /* [3] */ BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2), }; -int res; +int btf_fd; +int ret; - res = libbpf__probe_raw_btf((char *)types, sizeof(types),
tools/lib/bpf/libbpf.c static int bpf_object__probe_btf_func(struct bpf_object *obj) · line 1656
Quoted 10 lines
tools/lib/bpf/libbpf.c +6
-4- strs, sizeof(strs)); -if (res <0 ) -return res; -if (res >0 ) + btf_fd =libbpf__probe_raw_btf ((char *)types,sizeof (types), + strs,sizeof (strs)); +if (btf_fd <0 ) + ret =0 ; +else { + ret =1 ;
This whole ret variable seems unnecessary. Also if btf_fd is invalid, we probably shouldn't close it. So just this should work:
btf_fd = libbpf__probe_raw_btf(...);
if (btf_fd >= 0) {
obj->caps.btf_func = 1;
close(btf_fd);
}return btf_fd >= 0;
Quoted 21 lines
tools/lib/bpf/libbpf.c +5
-7obj->caps.btf_func =1 ; -return 0 ; + } +close (btf_fd); +return ret; }static int bpf_object__probe_btf_datasec (struct bpf_object *obj)@@ -1670,15 +1674,19 @@ static int bpf_object__probe_btf_datasec(struct bpf_object *obj)BTF_TYPE_ENC (3 ,BTF_INFO_ENC (BTF_KIND_DATASEC ,0 ,1 ),4 ), BTF_VAR_SECINFO_ENC(2, 0, 4), }; -int res; +int btf_fd; +int ret; - res =libbpf__probe_raw_btf ((char *)types,sizeof (types), - strs,sizeof (strs)); -if (res <0 ) -return res; -if (res >0 )
tools/lib/bpf/libbpf.c static int bpf_object__probe_btf_datasec(struct bpf_object *obj) · line 1688
Quoted 10 lines
tools/lib/bpf/libbpf.c +8
-1+ btf_fd =libbpf__probe_raw_btf ((char *)types,sizeof (types), + strs,sizeof (strs)); +if (btf_fd <0 ) + ret =0 ; +else { + ret =1 ; obj->caps.btf_datasec =1 ; -return 0 ; + } +close (btf_fd);
Same as above.
tools/lib/bpf/libbpf_probes.c int libbpf__probe_raw_btf(const char *raw_types, size_t types_len,
Quoted 9 lines
tools/lib/bpf/libbpf.c +1
-0+return ret; } static inttools/lib/bpf/libbpf_probes.c +0
-0@@ -157,14 +157,9 @@ int libbpf__probe_raw_btf(const char *raw_types, size_t types_len,
I'm wondering if it's better to rename this function to something like libbpf__load_raw_btf? probe (at least to me) implies true/false result, so feels like it might be easily misused.
Quoted 18 lines
tools/lib/bpf/libbpf_probes.c +1
-7memcpy (raw_btf + hdr.hdr_len + hdr.type_len, str_sec, hdr.str_len); btf_fd =bpf_load_btf (raw_btf, btf_len,NULL ,0 , false); -if (btf_fd <0 ) { -free (raw_btf); -return 0 ; - } -close (btf_fd);free (raw_btf); -return 1 ; +return btf_fd; }static int load_sk_storage_btf (void ) --2.21.0
Quoted 51 lines · Andrii Nakryiko
Quoted 8 lines · Michal Rostecki
Function
load_sk_storage_btfexpects thatlibbpf__probe_raw_btfis returning a btf descriptor, but before this change it was returning an information about whether the probe was successful (0 or 1).load_sk_storage_btfwas using that value as an argument to the close function, which was resulting in closing stdout and thus terminating the process which used that dunction.Quoted 5 lines
That bug was visible in bpftool.
bpftool featuresubcommand was always exiting too early (because of closed stdout) and it didn't display all requested probes.bpftool -j featureorbpftool -p featurewere not returning a valid json object.Thanks for the fix!
Quoted 20 lines
Fixes d7c4b3980c18 ("libbpf: detect supported kernel BTF features and sanitize BTF")Signed-off-by Michal Rostecki <[email protected]>--- tools/lib/bpf/libbpf.c | 36 +++++++++++++++++++++-------------- tools/lib/bpf/libbpf_probes.c | 7 +------ 2 files changed, 23 insertions(+), 20 deletions(-)tools/lib/bpf/libbpf.c +2
-2@@ -1645,15 +1645,19 @@ static int bpf_object__probe_btf_func(struct bpf_object *obj)/* FUNC x */ /* [3] */ BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2), }; -int res; +int btf_fd; +int ret; - res = libbpf__probe_raw_btf((char *)types, sizeof(types),tools/lib/bpf/libbpf.c static int bpf_object__probe_btf_func(struct bpf_object *obj) · line 1656
Quoted 10 lines
tools/lib/bpf/libbpf.c +6
-4- strs, sizeof(strs)); -if (res <0 ) -return res; -if (res >0 ) + btf_fd =libbpf__probe_raw_btf ((char *)types,sizeof (types), + strs,sizeof (strs)); +if (btf_fd <0 ) + ret =0 ; +else { + ret =1 ;This whole ret variable seems unnecessary. Also if
btf_fdis invalid, we probably shouldn't close it. So just this should work:
Quoted 7 lines
btf_fd=libbpf__probe_raw_btf(...);if (btf_fd >= 0) { obj->caps.btf_func = 1; close(btf_fd); }return
btf_fd>= 0;
Makes sense, I will do it in v3.
Quoted 37 lines
Quoted 21 lines
tools/lib/bpf/libbpf.c +5
-7obj->caps.btf_func =1 ; -return 0 ; + } +close (btf_fd); +return ret; }static int bpf_object__probe_btf_datasec (struct bpf_object *obj)@@ -1670,15 +1674,19 @@ static int bpf_object__probe_btf_datasec(struct bpf_object *obj)BTF_TYPE_ENC (3 ,BTF_INFO_ENC (BTF_KIND_DATASEC ,0 ,1 ),4 ), BTF_VAR_SECINFO_ENC(2, 0, 4), }; -int res; +int btf_fd; +int ret; - res =libbpf__probe_raw_btf ((char *)types,sizeof (types), - strs,sizeof (strs)); -if (res <0 ) -return res; -if (res >0 )tools/lib/bpf/libbpf.c static int bpf_object__probe_btf_datasec(struct bpf_object *obj) · line 1688
Quoted 10 lines
tools/lib/bpf/libbpf.c +8
-1+ btf_fd =libbpf__probe_raw_btf ((char *)types,sizeof (types), + strs,sizeof (strs)); +if (btf_fd <0 ) + ret =0 ; +else { + ret =1 ; obj->caps.btf_datasec =1 ; -return 0 ; + } +close (btf_fd);Same as above.
Quoted 3 lines
tools/lib/bpf/libbpf.c +1
-0+return ret; }
Quoted 11 lines
tools/lib/bpf/libbpf_probes.c int libbpf__probe_raw_btf(const char *raw_types, size_t types_len,
Quoted 6 lines
tools/lib/bpf/libbpf.c +0
-0static inttools/lib/bpf/libbpf_probes.c +0
-0@@ -157,14 +157,9 @@ int libbpf__probe_raw_btf(const char *raw_types, size_t types_len,I'm wondering if it's better to rename this function to something like
libbpf__load_raw_btf? probe (at least to me) implies true/false result, so feels like it might be easily misused.
Good idea.
Quoted 18 lines
Quoted 18 lines
tools/lib/bpf/libbpf_probes.c +1
-7memcpy (raw_btf + hdr.hdr_len + hdr.type_len, str_sec, hdr.str_len); btf_fd =bpf_load_btf (raw_btf, btf_len,NULL ,0 , false); -if (btf_fd <0 ) { -free (raw_btf); -return 0 ; - } -close (btf_fd);free (raw_btf); -return 1 ; +return btf_fd; }static int load_sk_storage_btf (void ) --2.21.0