Miscellaneous fixes for BPF (perf tree)
Subsystems (4)
v3 cover Miscellaneous fixes for BPF (perf tree)
This series brings some fixes and small improvements to the BPF library and samples.
This is intended for the perf tree and apply on e06094ab6755 ("Merge remote-tracking branch 'tip/perf/urgent' into perf/core").
Changes since v2:
* add this cover letter
Changes since v1:
* exclude patches not intended for the perf tree
Regards,
Mickaël Salaün (5):
bpf: Add missing header to the library
bpf: Simplify bpf_load_program() error handling in the library samples/bpf: Ignore already processed ELF sections samples/bpf: Reset global variables
samples/bpf: Add missing header
samples/bpf/bpf_load.c | 7 +++++++ samples/bpf/tracex5_kern.c | 1 + tools/lib/bpf/bpf.c | 18 ++++++------------ tools/lib/bpf/bpf.h | 1 + 4 files changed, 15 insertions(+), 12 deletions(-)
Signature · 1 line
2.11.0
PATCH v3 1/5 bpf: Add missing header to the library
Include stddef.h to define size_t.
--- tools/lib/bpf/bpf.h | 1 + 1 file changed, 1 insertion(+)
tools/lib/bpf/bpf.h +1 -0
@@ -22,6 +22,7 @@#define __BPF_BPF_H#include <linux/bpf.h> +#include <stddef.h> int bpf_create_map (enum bpf_map_type map_type,int key_size,int value_size,int max_entries,__u32 map_flags);
Signature · 1 line
2.11.0
Re: [PATCH v3 1/5] bpf: Add missing header to the library
Quoted 16 lines · Mickaël Salaün
Include
stddef.hto definesize_t.Signed-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Wang Nan <[email protected]>--- tools/lib/bpf/bpf.h | 1 + 1 file changed, 1 insertion(+)tools/lib/bpf/bpf.h +0
-0
tools/lib/bpf/bpf.h line 28
Quoted 7 lines
tools/lib/bpf/bpf.h +1
-0@@ -22,6 +22,7 @@#define __BPF_BPF_H#include <linux/bpf.h> +#include <stddef.h> int bpf_create_map (enum bpf_map_type map_type,int key_size,int value_size,int max_entries,__u32 map_flags);
Looks good to me.
Thank you.
Arnaldo Carvalho de Melo
AppliedRe: [PATCH v3 1/5] bpf: Add missing header to the library
Em Wed, Feb 08, 2017 at 10:47:18AM +0800, Wangnan (F) escreveu:
Quoted 10 lines
tools/lib/bpf/bpf.h line 26
Quoted 7 lines
+++
b/tools/lib/bpf/bpf.htools/lib/bpf/bpf.h +1
-0@@ -22,6 +22,7 @@#define __BPF_BPF_H#include <linux/bpf.h> +#include <stddef.h> int bpf_create_map (enum bpf_map_type map_type,int key_size,int value_size,int max_entries,__u32 map_flags);Looks good to me.
Thank you.
Applied, took the "Thank you" as an "Acked-by: Wang",
Regards,
- Arnaldo
PATCH v3 2/5 bpf: Simplify bpf_load_program() error handling in the library
In reply to Mickaël SalaünDo not call a second time bpf(2) when a program load failed.
--- tools/lib/bpf/bpf.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-)
tools/lib/bpf/bpf.c +6 -12
@@ -73,7 +73,6 @@ int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns, size_t insns_cnt, char *license, __u32 kern_version, char *log_buf, size_t log_buf_sz) { - int fd; union bpf_attr attr;bzero (&attr,sizeof (attr));
@@ -81,20 +80,15 @@ int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns, attr.insn_cnt = (__u32 )insns_cnt; attr.insns =ptr_to_u64 (insns); attr.license =ptr_to_u64 (license); - attr.log_buf =ptr_to_u64 (NULL ); - attr.log_size =0 ; - attr.log_level =0 ; + attr.log_buf =ptr_to_u64 (log_buf); + attr.log_size = log_buf_sz; attr.kern_version = kern_version; - fd =sys_bpf (BPF_PROG_LOAD , &attr,sizeof (attr)); -if (fd >=0 || !log_buf || !log_buf_sz) -return fd; +if (log_buf && log_buf_sz >0 ) { + attr.log_level =1 ; + log_buf[0 ] =0 ; + } -/* Try again with log */ - attr.log_buf =ptr_to_u64 (log_buf); - attr.log_size = log_buf_sz; - attr.log_level =1 ; - log_buf[0 ] =0 ;return sys_bpf (BPF_PROG_LOAD , &attr,sizeof (attr)); }
Signature · 1 line
2.11.0
Re: [PATCH v3 2/5] bpf: Simplify bpf_load_program() error handling in the library
Quoted 1 line · Mickaël Salaün
Do not call a second time bpf(2) when a program load failed.
BPF_PROG_LOAD should success most of the time. Setting log_level to 0 by default and require log buffer when failure can make it faster in normal case.
Thank you.
Quoted 48 lines
Signed-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Wang Nan <[email protected]>--- tools/lib/bpf/bpf.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-)tools/lib/bpf/bpf.c +6
-12@@ -73,7 +73,6 @@ int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns, size_t insns_cnt, char *license, __u32 kern_version, char *log_buf, size_t log_buf_sz) { - int fd; union bpf_attr attr;bzero (&attr,sizeof (attr));@@ -81,20 +80,15 @@ int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns, attr.insn_cnt = (__u32 )insns_cnt; attr.insns =ptr_to_u64 (insns); attr.license =ptr_to_u64 (license); - attr.log_buf =ptr_to_u64 (NULL ); - attr.log_size =0 ; - attr.log_level =0 ; + attr.log_buf =ptr_to_u64 (log_buf); + attr.log_size = log_buf_sz; attr.kern_version = kern_version; - fd =sys_bpf (BPF_PROG_LOAD , &attr,sizeof (attr)); -if (fd >=0 || !log_buf || !log_buf_sz) -return fd; +if (log_buf && log_buf_sz >0 ) { + attr.log_level =1 ; + log_buf[0 ] =0 ; + } -/* Try again with log */ - attr.log_buf =ptr_to_u64 (log_buf); - attr.log_size = log_buf_sz; - attr.log_level =1 ; - log_buf[0 ] =0 ;return sys_bpf (BPF_PROG_LOAD , &attr,sizeof (attr)); }
Re: [PATCH v3 2/5] bpf: Simplify bpf_load_program() error handling in the library
Quoted 8 lines · Wangnan (F)
Quoted 1 line · Mickaël Salaün
Do not call a second time bpf(2) when a program load failed.
BPF_PROG_LOADshould success most of the time. Settinglog_levelto 0 by default and require log buffer when failure can make it faster in normal case.
OK, I'm not sure if this is relevant for a test but I'm going to remove this patch.
Thanks
Quoted 53 lines
Thank you.
Quoted 47 lines
Signed-off-by Mickaël Salaün <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Wang Nan <[email protected]>--- tools/lib/bpf/bpf.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-)tools/lib/bpf/bpf.c +6
-10@@ -73,7 +73,6 @@ int bpf_load_program(enum bpf_prog_type type, structbpf_insn *insns, size_t insns_cnt, char *license, __u32 kern_version, char *log_buf, size_t log_buf_sz) { - int fd; union bpf_attr attr;bzero (&attr,sizeof (attr));@@ -81,20 +80,15 @@ int bpf_load_program(enum bpf_prog_type type,struct bpf_insn *insns, attr.insn_cnt = (__u32)insns_cnt; attr.insns =ptr_to_u64 (insns); attr.license =ptr_to_u64 (license); - attr.log_buf =ptr_to_u64 (NULL ); - attr.log_size =0 ; - attr.log_level =0 ; + attr.log_buf =ptr_to_u64 (log_buf); + attr.log_size = log_buf_sz; attr.kern_version = kern_version; - fd =sys_bpf (BPF_PROG_LOAD , &attr,sizeof (attr)); -if (fd >=0 || !log_buf || !log_buf_sz) -return fd; +if (log_buf && log_buf_sz >0 ) { + attr.log_level =1 ; + log_buf[0 ] =0 ; + } -/* Try again with log */ - attr.log_buf =ptr_to_u64 (log_buf); - attr.log_size = log_buf_sz; - attr.log_level =1 ; - log_buf[0 ] =0 ; return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr)); }
PATCH v3 3/5 samples/bpf: Ignore already processed ELF sections
In reply to Mickaël SalaünAdd a missing check for the map fixup loop.
--- samples/bpf/bpf_load.c | 2 ++ 1 file changed, 2 insertions(+)
samples/bpf/bpf_load.c +2 -0
@@ -328,6 +328,8 @@ int load_bpf_file(char *path)/* load programs that need map fixup (relocations) */ for (i =1 ; i < ehdr.e_shnum; i++) { +if (processed_sec[i]) +continue ;if (get_sec (elf, i, &ehdr, &shname, &shdr, &data))continue ;
Signature · 1 line
2.11.0
PATCH v3 4/5 samples/bpf: Reset global variables
In reply to Mickaël SalaünBefore loading a new ELF, clean previous kernel version, license and processed sections.
--- samples/bpf/bpf_load.c | 5 +++++ 1 file changed, 5 insertions(+)
samples/bpf/bpf_load.c +5 -0
@@ -277,6 +277,11 @@ int load_bpf_file(char *path)Elf_Data *data, *data_prog, *symbols =NULL ;char *shname, *shname_prog; +/* reset global variables */ + kern_version =0 ; +memset (license,0 ,sizeof (license)); +memset (processed_sec,0 ,sizeof (processed_sec)); +if (elf_version (EV_CURRENT ) ==EV_NONE )return 1 ;
Signature · 1 line
2.11.0
PATCH v3 5/5 samples/bpf: Add missing header
In reply to Mickaël SalaünInclude unistd.h to define __NR_getuid and __NR_getsid.
--- samples/bpf/tracex5_kern.c | 1 + 1 file changed, 1 insertion(+)
samples/bpf/tracex5_kern.c +1 -0
@@ -8,6 +8,7 @@#include <linux/version.h> #include <uapi/linux/bpf.h> #include <uapi/linux/seccomp.h> +#include <uapi/linux/unistd.h> #include "bpf_helpers.h" #define PROG (F ) SEC("kprobe/"__stringify(F)) int bpf_func_##F
Signature · 1 line
2.11.0