perf bpf-filter: Improve error messages
authorNamhyung Kim <namhyung@kernel.org>
Wed, 4 Jun 2025 17:48:35 +0000 (10:48 -0700)
committerNamhyung Kim <namhyung@kernel.org>
Mon, 9 Jun 2025 18:18:17 +0000 (11:18 -0700)
The BPF filter needs libbpf/BPF-skeleton support and root privilege.
Add error messages to help users understand the problem easily.

When it's not build with BPF support (make BUILD_BPF_SKEL=0).

  $ sudo perf record -e cycles --filter "pid != 0" true
  Error: BPF filter is requested but perf is not built with BPF.
   Please make sure to build with libbpf and BPF skeleton.

   Usage: perf record [<options>] [<command>]
      or: perf record [<options>] -- <command> [<options>]

          --filter <filter>
                            event filter

When it supports BPF but runs without root or CAP_BPF.  Note that it
also checks pinned BPF filters.

  $ perf record -e cycles --filter "pid != 0" -o /dev/null true
  Error: BPF filter only works for users with the CAP_BPF capability!
   Please run 'perf record --setup-filter pin' as root first.

   Usage: perf record [<options>] [<command>]
      or: perf record [<options>] -- <command> [<options>]

          --filter <filter>
                            event filter

Reviewed-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20250604174835.1852481-1-namhyung@kernel.org
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/util/bpf-filter.c
tools/perf/util/bpf-filter.h
tools/perf/util/cap.c
tools/perf/util/cap.h

index a4fdf6911ec1c32e799983c0951f39b16f77b7b7..92e2f054b45e91ddb7e7c86098f8657734188385 100644 (file)
@@ -52,6 +52,7 @@
 #include <internal/xyarray.h>
 #include <perf/threadmap.h>
 
+#include "util/cap.h"
 #include "util/debug.h"
 #include "util/evsel.h"
 #include "util/target.h"
@@ -618,11 +619,38 @@ struct perf_bpf_filter_expr *perf_bpf_filter_expr__new(enum perf_bpf_filter_term
        return expr;
 }
 
+static bool check_bpf_filter_capable(void)
+{
+       bool used_root;
+
+       if (perf_cap__capable(CAP_BPF, &used_root))
+               return true;
+
+       if (!used_root) {
+               /* Check if root already pinned the filter programs and maps */
+               int fd = get_pinned_fd("filters");
+
+               if (fd >= 0) {
+                       close(fd);
+                       return true;
+               }
+       }
+
+       pr_err("Error: BPF filter only works for %s!\n"
+              "\tPlease run 'perf record --setup-filter pin' as root first.\n",
+              used_root ? "root" : "users with the CAP_BPF capability");
+
+       return false;
+}
+
 int perf_bpf_filter__parse(struct list_head *expr_head, const char *str)
 {
        YY_BUFFER_STATE buffer;
        int ret;
 
+       if (!check_bpf_filter_capable())
+               return -EPERM;
+
        buffer = perf_bpf_filter__scan_string(str);
 
        ret = perf_bpf_filter_parse(expr_head);
index 916ed7770b734f1533b9abe11bae18919a729762..122477f2de44bb60515b9481d2aa03bf62ebf33f 100644 (file)
@@ -5,6 +5,7 @@
 #include <linux/list.h>
 
 #include "bpf_skel/sample-filter.h"
+#include "util/debug.h"
 
 struct perf_bpf_filter_expr {
        struct list_head list;
@@ -38,6 +39,8 @@ int perf_bpf_filter__unpin(void);
 static inline int perf_bpf_filter__parse(struct list_head *expr_head __maybe_unused,
                                         const char *str __maybe_unused)
 {
+       pr_err("Error: BPF filter is requested but perf is not built with BPF.\n"
+               "\tPlease make sure to build with libbpf and BPF skeleton.\n");
        return -EOPNOTSUPP;
 }
 static inline int perf_bpf_filter__prepare(struct evsel *evsel __maybe_unused,
index 69d9a2bcd40bfdd105c788644a4808460dcc16b7..24a0ea7e6d97749b668f1d2cf63c482f06818799 100644 (file)
@@ -7,7 +7,6 @@
 #include "debug.h"
 #include <errno.h>
 #include <string.h>
-#include <linux/capability.h>
 #include <sys/syscall.h>
 #include <unistd.h>
 
index 0c6a1ff55f07340a68538f4eb8f4e4a73d11c5f6..c1b8ac033ccc582664c8661055bb29833c68e01d 100644 (file)
@@ -3,6 +3,7 @@
 #define __PERF_CAP_H
 
 #include <stdbool.h>
+#include <linux/capability.h>
 
 /* For older systems */
 #ifndef CAP_SYSLOG
 #define CAP_PERFMON    38
 #endif
 
+#ifndef CAP_BPF
+#define CAP_BPF                39
+#endif
+
 /* Query if a capability is supported, used_root is set if the fallback root check was used. */
 bool perf_cap__capable(int cap, bool *used_root);