[RFC PATCH 14/16] perf bpf-filter: Support host_ip in BPF sample filters

From: Ian Rogers

Date: Tue Dec 01 2026 - 18:22:56 EST


Add the host_ip term to the BPF sample filter lexer, sample flag
validation, sample-filter.h / sample_filter.bpf.c (using 1ULL shifts
for sample bit checks), and vmlinux.h, and extend record_bpf_filter.sh
to verify host_ip filter sample-flag validation.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/tests/shell/record_bpf_filter.sh | 8 ++++++++
tools/perf/util/bpf-filter.c | 3 ++-
tools/perf/util/bpf-filter.l | 1 +
tools/perf/util/bpf_skel/sample-filter.h | 3 ++-
tools/perf/util/bpf_skel/sample_filter.bpf.c | 9 +++++++--
tools/perf/util/bpf_skel/vmlinux/vmlinux.h | 1 +
6 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/tools/perf/tests/shell/record_bpf_filter.sh b/tools/perf/tests/shell/record_bpf_filter.sh
index 383574cb3bd3..5c898b52b263 100755
--- a/tools/perf/tests/shell/record_bpf_filter.sh
+++ b/tools/perf/tests/shell/record_bpf_filter.sh
@@ -113,6 +113,14 @@ test_bpf_filter_group() {
return
fi

+ if ! perf record -e task-clock --filter 'period > 0 || host_ip > 0' \
+ -o /dev/null true 2>&1 | grep -q PERF_SAMPLE_HOST_IP
+ then
+ echo "Group bpf-filter test [Failed forbidden HOST_IP]"
+ err=1
+ return
+ fi
+
echo "Group bpf-filter test [Success]"
}

diff --git a/tools/perf/util/bpf-filter.c b/tools/perf/util/bpf-filter.c
index 72e83ac1d6a4..698fcd7c2d09 100644
--- a/tools/perf/util/bpf-filter.c
+++ b/tools/perf/util/bpf-filter.c
@@ -104,6 +104,7 @@ static const struct perf_sample_info {
PERF_SAMPLE_TYPE(CODE_PAGE_SIZE, "--code-page-size"),
PERF_SAMPLE_TYPE(DATA_PAGE_SIZE, "--data-page-size"),
PERF_SAMPLE_TYPE(CGROUP, "--all-cgroups"),
+ PERF_SAMPLE_TYPE(HOST_IP, "--host-ip"),
};

static int get_pinned_fd(const char *name);
@@ -124,7 +125,7 @@ static int check_sample_flags(struct evsel *evsel, struct perf_bpf_filter_expr *
const struct perf_sample_info *info;

if (expr->term >= PBF_TERM_SAMPLE_START && expr->term <= PBF_TERM_SAMPLE_END &&
- (evsel->core.attr.sample_type & (1 << (expr->term - PBF_TERM_SAMPLE_START))))
+ (evsel->core.attr.sample_type & (1ULL << (expr->term - PBF_TERM_SAMPLE_START))))
return 0;

if (expr->term == PBF_TERM_UID || expr->term == PBF_TERM_GID) {
diff --git a/tools/perf/util/bpf-filter.l b/tools/perf/util/bpf-filter.l
index 1be9df6550fc..ade6d88bb964 100644
--- a/tools/perf/util/bpf-filter.l
+++ b/tools/perf/util/bpf-filter.l
@@ -86,6 +86,7 @@ ident [_a-zA-Z][_a-zA-Z0-9]+
{space} { }

ip { return sample(PBF_TERM_IP); }
+host_ip { return sample(PBF_TERM_HOST_IP); }
id { return sample(PBF_TERM_ID); }
tid { return sample(PBF_TERM_TID); }
pid { return sample_part(PBF_TERM_TID, 1); }
diff --git a/tools/perf/util/bpf_skel/sample-filter.h b/tools/perf/util/bpf_skel/sample-filter.h
index 683fec85e71e..04d0314e8f53 100644
--- a/tools/perf/util/bpf_skel/sample-filter.h
+++ b/tools/perf/util/bpf_skel/sample-filter.h
@@ -49,7 +49,8 @@ enum perf_bpf_filter_term {
PBF_TERM_DATA_PAGE_SIZE = PBF_TERM_SAMPLE_START + 22, /* SAMPLE_DATA_PAGE_SIZE = 1U << 22 */
PBF_TERM_CODE_PAGE_SIZE = PBF_TERM_SAMPLE_START + 23, /* SAMPLE_CODE_PAGE_SIZE = 1U << 23 */
PBF_TERM_WEIGHT_STRUCT = PBF_TERM_SAMPLE_START + 24, /* SAMPLE_WEIGHT_STRUCT = 1U << 24 */
- PBF_TERM_SAMPLE_END = PBF_TERM_WEIGHT_STRUCT,
+ PBF_TERM_HOST_IP = PBF_TERM_SAMPLE_START + 25, /* SAMPLE_HOST_IP = 1U << 25 */
+ PBF_TERM_SAMPLE_END = PBF_TERM_HOST_IP,
/* Terms computed from BPF helpers. */
PBF_TERM_UID,
PBF_TERM_GID,
diff --git a/tools/perf/util/bpf_skel/sample_filter.bpf.c b/tools/perf/util/bpf_skel/sample_filter.bpf.c
index e5666d4c1722..c22d9c867c7e 100644
--- a/tools/perf/util/bpf_skel/sample_filter.bpf.c
+++ b/tools/perf/util/bpf_skel/sample_filter.bpf.c
@@ -80,7 +80,7 @@ static inline __u64 perf_get_sample(struct bpf_perf_event_data_kern *kctx,
return 0;

#define BUILD_CHECK_SAMPLE(x) \
- _Static_assert((1 << (PBF_TERM_##x - PBF_TERM_SAMPLE_START)) == PERF_SAMPLE_##x, \
+ _Static_assert((1ULL << (PBF_TERM_##x - PBF_TERM_SAMPLE_START)) == PERF_SAMPLE_##x, \
"Mismatched PBF term to sample bit " #x)
BUILD_CHECK_SAMPLE(IP);
BUILD_CHECK_SAMPLE(TID);
@@ -97,16 +97,21 @@ static inline __u64 perf_get_sample(struct bpf_perf_event_data_kern *kctx,
BUILD_CHECK_SAMPLE(DATA_PAGE_SIZE);
BUILD_CHECK_SAMPLE(CODE_PAGE_SIZE);
BUILD_CHECK_SAMPLE(WEIGHT_STRUCT);
+ BUILD_CHECK_SAMPLE(HOST_IP);
#undef BUILD_CHECK_SAMPLE

/* For sample terms check the sample bit is set. */
if (entry->term >= PBF_TERM_SAMPLE_START && entry->term <= PBF_TERM_SAMPLE_END &&
- (data->sample_flags & (1 << (entry->term - PBF_TERM_SAMPLE_START))) == 0)
+ (data->sample_flags & (1ULL << (entry->term - PBF_TERM_SAMPLE_START))) == 0)
return 0;

switch (entry->term) {
case PBF_TERM_IP:
return kctx->data->ip;
+ case PBF_TERM_HOST_IP:
+ if (bpf_core_field_exists(kctx->data->host_ip))
+ return kctx->data->host_ip;
+ return 0;
case PBF_TERM_ID:
return kctx->data->id;
case PBF_TERM_TID:
diff --git a/tools/perf/util/bpf_skel/vmlinux/vmlinux.h b/tools/perf/util/bpf_skel/vmlinux/vmlinux.h
index 7d07c565762c..2d7659d4cb7e 100644
--- a/tools/perf/util/bpf_skel/vmlinux/vmlinux.h
+++ b/tools/perf/util/bpf_skel/vmlinux/vmlinux.h
@@ -206,6 +206,7 @@ struct perf_sample_data {
u64 cgroup;
u64 data_page_size;
u64 code_page_size;
+ u64 host_ip;
} __attribute__((__aligned__(64))) __attribute__((preserve_access_index));

struct perf_event {
--
2.56.1.915.g69d5ec7298-goog