tools/bpftool: fix a bug in bpftool perf
Subsystems (4)
PATCH 1/1 tools/bpftool: fix a bug in bpftool perf
Commit b04df400c302 ("tools/bpftool: add perf subcommand") introduced bpftool subcommand perf to query bpf program kuprobe and tracepoint attachments.
The perf subcommand will first test whether bpf subcommand BPF_TASK_FD_QUERY is supported in kernel or not. It does it by opening a file with argv[0] and feeds the file descriptor and current task pid to the kernel for querying.
Such an approach won't work if the argv[0] cannot be opened successfully in the current directory. This is especially true when bpftool is accessible through PATH env variable. The error below reflects the open failure for file argv[0] at home directory.
[yhs@localhost ~]$ which bpftool /usr/local/sbin/bpftool
[yhs@localhost ~]$ bpftool perf
Error: perf_query_support: No such file or directory
To fix the issue, let us open root directory ("/") which exists in every linux system. With the fix, the error message will correctly reflect the permission issue.
[yhs@localhost ~]$ which bpftool /usr/local/sbin/bpftool
[yhs@localhost ~]$ bpftool perf
Error: perf_query_support: Operation not permitted
HINT: non root or kernel doesn't support TASK_FD_QUERY
--- tools/bpf/bpftool/perf.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
tools/bpf/bpftool/perf.c +4 -3
@@ -29,9 +29,10 @@ static bool has_perf_query_support(void)if (perf_query_supported) goto out; - fd =open (bin_name,O_RDONLY ); -if (fd <0 ) { -p_err ("perf_query_support: %s" ,strerror (errno)); + fd =open ("/" ,O_RDONLY ); +if (fd >0 ) { +p_err ("perf_query_support: cannot open directory \"/\" (%s)\n" , +strerror (errno)); goto out; }
Signature · 1 line
2.14.3
Quoted 42 lines · Yonghong Song
Commit
b04df400c302("tools/bpftool: add perf subcommand") introduced bpftool subcommand perf to query bpf program kuprobe and tracepoint attachments.The perf subcommand will first test whether bpf subcommand
BPF_TASK_FD_QUERYis supported in kernel or not. It does it by opening a file with argv[0] and feeds the file descriptor and current task pid to the kernel for querying.Such an approach won't work if the argv[0] cannot be opened successfully in the current directory. This is especially true when bpftool is accessible through PATH env variable. The error below reflects the open failure for file argv[0] at home directory.
[yhs@localhost ~]$ which bpftool
/usr/local/sbin/bpftool
[yhs@localhost ~]$ bpftool perf
Error:perf_query_support: No such file or directoryTo fix the issue, let us open root directory ("/") which exists in every linux system. With the fix, the error message will correctly reflect the permission issue.
[yhs@localhost ~]$ which bpftool
/usr/local/sbin/bpftool
[yhs@localhost ~]$ bpftool perf
Error:perf_query_support: Operation not permitted
HINT: non root or kernel doesn't supportTASK_FD_QUERYFixes b04df400c302 ("tools/bpftool: add perf subcommand")Reported-by Alexei Starovoitov <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- tools/bpf/bpftool/perf.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)tools/bpf/bpftool/perf.c +0
-0
tools/bpf/bpftool/perf.c static bool has_perf_query_support(void) · line 34
Quoted 9 lines
tools/bpf/bpftool/perf.c +3
-3@@ -29,9 +29,10 @@ static bool has_perf_query_support(void)if (perf_query_supported) goto out; - fd =open (bin_name,O_RDONLY ); -if (fd <0 ) { -p_err ("perf_query_support: %s" ,strerror (errno)); + fd =open ("/" ,O_RDONLY ); +if (fd >0 ) { + p_err("perf_query_support: cannot open directory \"/\" (%s)\n",
nit: no \n at the end of p_err() format, because it breaks JSON :(
Quoted 4 lines
tools/bpf/bpftool/perf.c +1
-0+ strerror(errno)); goto out; }
Quoted 46 lines · Jakub Kicinski
Quoted 45 lines · Yonghong Song
Commit
b04df400c302("tools/bpftool: add perf subcommand") introduced bpftool subcommand perf to query bpf program kuprobe and tracepoint attachments.The perf subcommand will first test whether bpf subcommand
BPF_TASK_FD_QUERYis supported in kernel or not. It does it by opening a file with argv[0] and feeds the file descriptor and current task pid to the kernel for querying.Such an approach won't work if the argv[0] cannot be opened successfully in the current directory. This is especially true when bpftool is accessible through PATH env variable. The error below reflects the open failure for file argv[0] at home directory.
[yhs@localhost ~]$ which bpftool
/usr/local/sbin/bpftool
[yhs@localhost ~]$ bpftool perf
Error:perf_query_support: No such file or directoryTo fix the issue, let us open root directory ("/") which exists in every linux system. With the fix, the error message will correctly reflect the permission issue.
[yhs@localhost ~]$ which bpftool
/usr/local/sbin/bpftool
[yhs@localhost ~]$ bpftool perf
Error:perf_query_support: Operation not permitted
HINT: non root or kernel doesn't supportTASK_FD_QUERYFixes b04df400c302 ("tools/bpftool: add perf subcommand")Reported-by Alexei Starovoitov <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- tools/bpf/bpftool/perf.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)tools/bpf/bpftool/perf.c +0
-0@@ -29,9 +29,10 @@ static bool has_perf_query_support(void)if (perf_query_supported) goto out;
Quoted 8 lines
tools/bpf/bpftool/perf.c static bool has_perf_query_support(void) · line 34
Quoted 6 lines
tools/bpf/bpftool/perf.c +3
-3- fd =open (bin_name,O_RDONLY ); -if (fd <0 ) { -p_err ("perf_query_support: %s" ,strerror (errno)); + fd =open ("/" ,O_RDONLY ); +if (fd >0 ) { + p_err("perf_query_support: cannot open directory \"/\" (%s)\n",nit: no \n at the end of
p_err()format, because it breaks JSON :(
Thanks for letting me know. Will send v2 to fix this!
Quoted 6 lines
Quoted 5 lines
tools/bpf/bpftool/perf.c +1
-0+ strerror(errno)); goto out; }