From 80091a2681577e1100567181678fb76de978dfe5 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Sun, 24 Apr 2022 14:52:34 +0900 Subject: community/linux-tools: add bpftool compile and install bpftool in its own package. bpftool isn't compatible with musl, so a few patches have been added. The patches have been sent upstream here: https://lkml.kernel.org/r/20220424051022.2619648-1-asmadeus@codewreck.org and have already been merged without modification into linux v5.19-rc1 --- community/linux-tools/APKBUILD | 35 ++-- .../linux-tools/bpftool-include-fcntl.h.patch | 33 ++++ community/linux-tools/bpftool-nftw.patch | 183 +++++++++++++++++++++ 3 files changed, 241 insertions(+), 10 deletions(-) create mode 100644 community/linux-tools/bpftool-include-fcntl.h.patch create mode 100644 community/linux-tools/bpftool-nftw.patch diff --git a/community/linux-tools/APKBUILD b/community/linux-tools/APKBUILD index f7f27614303..a4d49e43a1b 100644 --- a/community/linux-tools/APKBUILD +++ b/community/linux-tools/APKBUILD @@ -5,7 +5,7 @@ pkgname=linux-tools pkgver=5.15.41 _kernver=${pkgver%.*} -pkgrel=1 +pkgrel=2 pkgdesc="Linux kernel tools meta package" url="https://www.kernel.org/" arch="all" @@ -14,15 +14,19 @@ depends="perf cpupower $pkgname-gpio $pkgname-iio" depends_dev="pciutils-dev readline-dev gettext-dev" makedepends="$depends_dev elfutils-dev bash linux-headers flex bison diffutils zlib-dev findutils python3 ncurses-dev - automake autoconf libtool libudev-zero-dev sysfsutils-dev glib-dev" + automake autoconf libtool libudev-zero-dev sysfsutils-dev glib-dev + libbpf binutils-dev clang llvm libcap-dev" subpackages="perf perf-bash-completion:perf_bashcomp:noarch cpupower cpupower-openrc cpupower-bash-completion:cpupower_bashcomp:noarch $pkgname-gpio $pkgname-iio $pkgname-doc $pkgname-dev - $pkgname-tmon $pkgname-usbip $pkgname-usbip-openrc" + $pkgname-tmon $pkgname-usbip $pkgname-usbip-openrc + bpftool bpftool-bash-completion:bpftool_bashcomp:noarch" source="https://kernel.org/pub/linux/kernel/v5.x/linux-$_kernver.tar.xz https://kernel.org/pub/linux/kernel/v5.x/patch-$pkgver.xz tmon-musl-includes.patch cpupower-libs.patch + bpftool-nftw.patch + bpftool-include-fcntl.h.patch cpupower.initd cpupower.confd usbip.initd @@ -50,7 +54,7 @@ _make_tools() { } build() { - _make_tools perf cpupower gpio iio tmon + _make_tools perf cpupower gpio iio tmon bpf/bpftool ( cd tools/usb/usbip ; ./autogen.sh sed -i 's,-Wall -Werror -Wextra,-fcommon,' configure.ac ; @@ -70,6 +74,8 @@ package() { mkdir -p "$pkgdir" _make_tools DESTDIR="$pkgdir" \ perf_install cpupower_install gpio_install iio_install + make -C "$builddir/tools/bpf/bpftool" prefix=/usr \ + DESTDIR="$pkgdir" install ( cd tools/thermal/tmon; make install INSTALL_ROOT="$pkgdir"; ) ( cd tools/usb/usbip ; @@ -172,19 +178,28 @@ tmon() { mv "$pkgdir"/usr/bin/tmon "$subpkgdir"/usr/bin } -bashcomp() { - replaces="$pkgname-bash-completion" # Backward compatibility - pkgdesc="Bash autocompletion for $pkgname" - install_if="perf=$pkgver-r$pkgrel bash-completion" - mkdir -p "$subpkgdir"/usr/share/ - mv "$pkgdir"/usr/share/bash-completion "$subpkgdir"/usr/share/ +bpftool() { + pkgdesc="Linux kernel bpf manipulation tool" + mkdir -p "$subpkgdir"/usr/sbin + mv "$pkgdir"/usr/sbin/bpftool "$subpkgdir"/usr/sbin/ } +bpftool_bashcomp() { + pkgdesc="Bash completion for bpftool" + install_if="bpftool=$pkgver-r$pkgrel bash-completion" + mkdir -p "$subpkgdir"/usr/share/bash-completion/completions + mv "$pkgdir"/usr/share/bash-completion/completions/bpftool \ + "$subpkgdir"/usr/share/bash-completion/completions/ +} + + sha512sums=" d25ad40b5bcd6a4c6042fd0fd84e196e7a58024734c3e9a484fd0d5d54a0c1d87db8a3c784eff55e43b6f021709dc685eb0efa18d2aec327e4f88a79f405705a linux-5.15.tar.xz 1c7ece228174f5c3049ec44cafa588117d6c470567a95ba4c926fa370b17b8a90256c40df657fc923ec32303268abeeb6249943eee69cad73be38156628aab6f patch-5.15.41.xz 15d6d80e9ed2cdea3bb09574f1cf524007b088699204cfc9d326edd9cf461a38300cfafd82dae4a5a78a179944ef10f51ed7d51c3a8aeb8c08e7c6754d9e123b tmon-musl-includes.patch a46e3a84b00a39a356618831d0ddfb7f0d10f0a3799d1307ba2cc832e73c01f8d637a4e801a6dd25025f6f13155c6ad8b836422ff72d365e51063ac0bf907f52 cpupower-libs.patch +b2e91d3ce8154a5515bb6f41c02ea73d74f1e3cf4e05c7d902e820803658fa1a2e3d4d6654c35661d95e4f24f923ba84723e9ffdf85583fa3db187f9c51e480f bpftool-nftw.patch +13201d1915413091a5974c1a3c1d70bc79f02923357b0a37ac8fcf8baf52f90a152b046b0b69ebc8558e49ca1be89a2d370a7aa4fe6d2821d9e79c2a9dc2bfd4 bpftool-include-fcntl.h.patch 97ecfeaaaa8efca7af585316b843bf7be885e88095bbdfbf52bc9a96b14dc862435482781e205ea022ab208978aaa4aabb317354fab890ed514a5088ae5045c8 cpupower.initd 63cd12a4d8782198c71662e9205e346e064b65bae5e3e175b5bc43bdf7ec6ddf675b60349750afe3c37c8a925f9ae9a93530671821342581bd282a69e0773b8c cpupower.confd fcbd64d844c9bc187d08cef5995e91a46c0df78deb24e96ac9210c0e2c730eca0301970d9b8ffbf003df274682d05072431a26b59d8c491f396618268a12ec92 usbip.initd diff --git a/community/linux-tools/bpftool-include-fcntl.h.patch b/community/linux-tools/bpftool-include-fcntl.h.patch new file mode 100644 index 00000000000..1a0e41df00c --- /dev/null +++ b/community/linux-tools/bpftool-include-fcntl.h.patch @@ -0,0 +1,33 @@ +From bf69fe0ed2d5e09cccfcd78062a974a0e761033b Mon Sep 17 00:00:00 2001 +From: Dominique Martinet +Date: Sat, 23 Apr 2022 23:03:57 +0900 +Subject: [PATCH 4/4] tools/bpf: replace sys/fcntl.h by fcntl.h + +musl does not like including sys/fcntl.h directly: + 1 | #warning redirecting incorrect #include to + +Signed-off-by: Dominique Martinet +--- +accepted by upstream without modification, can be dropped after update +to v5.19 or higher (merged in v5.19-rc1 as +246bdfa52f33ecfa52546ed67287de4eab165b2e ) + + tools/bpf/bpftool/tracelog.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/tools/bpf/bpftool/tracelog.c b/tools/bpf/bpftool/tracelog.c +index e80a5c79b38f..bf1f02212797 100644 +--- a/tools/bpf/bpftool/tracelog.c ++++ b/tools/bpf/bpftool/tracelog.c +@@ -9,7 +9,7 @@ + #include + #include + #include +-#include ++#include + #include + + #include "main.h" +-- +2.35.1 + diff --git a/community/linux-tools/bpftool-nftw.patch b/community/linux-tools/bpftool-nftw.patch new file mode 100644 index 00000000000..70de27f64d4 --- /dev/null +++ b/community/linux-tools/bpftool-nftw.patch @@ -0,0 +1,183 @@ +From 56cbc4a5e7d98901a13d1e662c794133036ba25a Mon Sep 17 00:00:00 2001 +From: Dominique Martinet +Date: Sat, 23 Apr 2022 23:03:11 +0900 +Subject: [PATCH 3/4] tools/bpf: musl compat: replace nftw with + FTW_ACTIONRETVAL + +musl nftw implementation does not support FTW_ACTIONRETVAL. + +There have been multiple attempts at pushing the feature in musl +upstream but it has been refused or ignored all the times: + +In this case we only care about /proc//fd/, so it's not +too difficult to reimplement directly instead, and the new +implementation makes 'bpftool perf' slightly faster because it doesn't +needlessly stat/readdir unneeded directories (54ms -> 13ms on my machine) + +https://www.openwall.com/lists/musl/2021/03/26/1 +https://www.openwall.com/lists/musl/2022/01/22/1 +Signed-off-by: Dominique Martinet +--- +accepted by upstream without modification, can be dropped after update +to v5.19 or higher (merged in v5.19-rc1 as +93bc2e9e943d20a51473a49009db3243de6e098d ) + + tools/bpf/bpftool/perf.c | 116 ++++++++++++++++++++------------------- + 1 file changed, 59 insertions(+), 57 deletions(-) + +diff --git a/tools/bpf/bpftool/perf.c b/tools/bpf/bpftool/perf.c +index 50de087b0db7..de793872544e 100644 +--- a/tools/bpf/bpftool/perf.c ++++ b/tools/bpf/bpftool/perf.c +@@ -11,7 +11,7 @@ + #include + #include + #include +-#include ++#include + + #include + +@@ -147,81 +147,83 @@ static void print_perf_plain(int pid, int fd, __u32 prog_id, __u32 fd_type, + } + } + +-static int show_proc(const char *fpath, const struct stat *sb, +- int tflag, struct FTW *ftwbuf) ++static int show_proc(void) + { + __u64 probe_offset, probe_addr; + __u32 len, prog_id, fd_type; +- int err, pid = 0, fd = 0; ++ int err, pid, fd; ++ DIR *proc, *pid_fd; ++ struct dirent *proc_de, *pid_fd_de; + const char *pch; + char buf[4096]; + +- /* prefix always /proc */ +- pch = fpath + 5; +- if (*pch == '\0') +- return 0; ++ proc = opendir("/proc"); ++ if (!proc) ++ return -1; ++ while ((proc_de = readdir(proc))) { ++ pid = 0; ++ pch = proc_de->d_name; + +- /* pid should be all numbers */ +- pch++; +- while (isdigit(*pch)) { +- pid = pid * 10 + *pch - '0'; +- pch++; ++ /* pid should be all numbers */ ++ while (isdigit(*pch)) { ++ pid = pid * 10 + *pch - '0'; ++ pch++; ++ } ++ if (*pch != '\0') ++ continue; ++ ++ err = snprintf(buf, sizeof(buf), "/proc/%s/fd", proc_de->d_name); ++ if (err < 0 || err >= (int)sizeof(buf)) ++ continue; ++ ++ pid_fd = opendir(buf); ++ if (!pid_fd) ++ continue; ++ ++ while ((pid_fd_de = readdir(pid_fd))) { ++ fd = 0; ++ pch = pid_fd_de->d_name; ++ ++ /* fd should be all numbers */ ++ while (isdigit(*pch)) { ++ fd = fd * 10 + *pch - '0'; ++ pch++; ++ } ++ if (*pch != '\0') ++ continue; ++ ++ /* query (pid, fd) for potential perf events */ ++ len = sizeof(buf); ++ err = bpf_task_fd_query(pid, fd, 0, buf, &len, &prog_id, &fd_type, ++ &probe_offset, &probe_addr); ++ if (err < 0) ++ continue; ++ ++ if (json_output) ++ print_perf_json(pid, fd, prog_id, fd_type, buf, probe_offset, ++ probe_addr); ++ else ++ print_perf_plain(pid, fd, prog_id, fd_type, buf, probe_offset, ++ probe_addr); ++ } ++ closedir(pid_fd); + } +- if (*pch == '\0') +- return 0; +- if (*pch != '/') +- return FTW_SKIP_SUBTREE; +- +- /* check /proc//fd directory */ +- pch++; +- if (strncmp(pch, "fd", 2)) +- return FTW_SKIP_SUBTREE; +- pch += 2; +- if (*pch == '\0') +- return 0; +- if (*pch != '/') +- return FTW_SKIP_SUBTREE; +- +- /* check /proc//fd/ */ +- pch++; +- while (isdigit(*pch)) { +- fd = fd * 10 + *pch - '0'; +- pch++; +- } +- if (*pch != '\0') +- return FTW_SKIP_SUBTREE; +- +- /* query (pid, fd) for potential perf events */ +- len = sizeof(buf); +- err = bpf_task_fd_query(pid, fd, 0, buf, &len, &prog_id, &fd_type, +- &probe_offset, &probe_addr); +- if (err < 0) +- return 0; +- +- if (json_output) +- print_perf_json(pid, fd, prog_id, fd_type, buf, probe_offset, +- probe_addr); +- else +- print_perf_plain(pid, fd, prog_id, fd_type, buf, probe_offset, +- probe_addr); +- ++ closedir(proc); + return 0; + } + + static int do_show(int argc, char **argv) + { +- int flags = FTW_ACTIONRETVAL | FTW_PHYS; +- int err = 0, nopenfd = 16; ++ int err; + + if (!has_perf_query_support()) + return -1; + + if (json_output) + jsonw_start_array(json_wtr); +- if (nftw("/proc", show_proc, nopenfd, flags) == -1) { +- p_err("%s", strerror(errno)); +- err = -1; +- } ++ ++ err = show_proc(); ++ + if (json_output) + jsonw_end_array(json_wtr); + +-- +2.35.1 + -- cgit v1.2.3