Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796167319; b=RBn1REWQj5ISBWRVfOfSX+s/whhy8Rlaq03VYKqJ3XHR9CjBGeQZNGf+dNKYeN/c+sDI oMKe1KwnPjNU+hVmotGe6YfJUnkSpBTAJqkTuX1F81I/X3l72r/fDOnoHIg/6zdnx6g5z RIpkLR03nw840rgEJchQ8Gmkh5FN3fe4eAvnFAS5N++a0qSIqXxWkOc5YLgWT1vYLAhZ4 Ks4mo3ohuXxxXynl7mzTluLnDwPWZDS9hDT2+3Hgi3ngZVLGplM1I8al0hzlbDQxru+A4 OV2bCOGkp09/UueOqdYlAevq6K2frsuxThiAfm9Y311wG0P699IDEow0YfMFPJDDPiw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796167319; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=2JF8YXL49XWWVXOtB3PrOP4dL33expJSyWY1gbQFBhg=; b=bJcHkXodBmyxYfmqXC8dsMocSZWQrNkgzpyyWUh4+ZivRWFR6VLxESyJU/p6T4Is0mL5 8vsR5Pl2p63UYdOVlNVXGnfZle6ZGiL1RXI+ZWbYbZnEEfkFFDWSUo7vKZiwIGQ1rXn6B cnQ6tNfytil0kW1nkWlUEJfjTceH03j/3DD2JwLMrh1c1QJ7v0sgDp3GBywfj4kmAdvas 3v10Y1h1JCjcc8P8qEesdU4JPzEyN94QIW0lPNhSYt7kGWg3GDyZLyP/dwLR2BmzGJIvw oBum2k4WVUqt+dsAv6KS+6sYsfYgez96AJIXHvQ+k1nPwdsmOsSuEWWq5T4Cazr3P7Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=On8Nz8TZ; arc=none smtp.remote-ip=74.125.82.70 Received: from mail-dl1-f70.google.com (mail-dl1-f70.google.com [74.125.82.70]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B6ABF60738E5 for ; Tue, 1 Dec 2026 23:21:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B6ABF60738E5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=On8Nz8TZ Received: by mail-dl1-f70.google.com with SMTP id a92af1059eb24-14373747468so11825939c88.1 for ; Tue, 01 Dec 2026 15:21:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796167318; x=1796772118; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=2JF8YXL49XWWVXOtB3PrOP4dL33expJSyWY1gbQFBhg=; b=On8Nz8TZA5i09/MiRXgsDD8U4F89f89ALyVEimrLJecc0nFuXZV9RYeVR2dLsE3Jfd t8rJWvuEQa+ToLAWxN/B71Ho48MQNqnrtU9qmTH0Arvx8Iz99WNAL3Jp52WZ7hurbiI9 mT7S3D0KrAaZnzsB5uO/4G5Xs2ybxZtvl3AfCG9u9U1hkn3VG110UXbPu7kzwPKTkvl0 08sGmJtuCEaf7pLS9zGXUrtiYwNV8jt6NnOoHp6MZd+5wWiNaguupyO/Bi6nLs+pAJbl SSBxdJjrV7n1AZ74INQ/jqqyPROkwEoy0SWEQ1gK4L5TDkpQaN4tUHJeL1GF18AuTfIK MTFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796167318; x=1796772118; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=2JF8YXL49XWWVXOtB3PrOP4dL33expJSyWY1gbQFBhg=; b=jihdou28gyOcz94sBpqpZpkOCBqDV4XwUKOweg2I9I98oiy8JujLO0Z/oT1aVmoV5n EkeDSYf/cXmUUC4pbIsIs63duVhClUIE/RTU89HA4Cg0GozHr3koohHNIpv+UXbRxDQM Wtx7xRGY45scfmkF8AdNxU6I7QZpMlCk5BianivNqQjv6lP5lyktBS3XDdzlSBqJORko jXQNB5FjUCWmQL0fnSRsn8Tz/xemLINP20jNLzaUFWCrhF7eoMbSYqx3LKwBhzMwGF+M k/h7xkYO7mSH2WIj3gzYChE5gZCdUd1mdk93tVymu1HDUtRAaFG2X5soxKyrkZyCjm9h F4AQ== X-Forwarded-Encrypted: i=1; AL+hErEfcZw5urT6d1ZCn1uuIRE8Rl//J21UokWsYIdfdL+Hbe3kvkOBbkSWGxS1M1Xsz/El6JI=@vger.kernel.org X-Gm-Message-State: APuU4Z+xRGfKVYDNQA25xd+QaLLyNSPXrIdsdj07rd1LZvYXciHrlFSA HfB5A+YGCdc6981RFHHCrRoIgB2qv64oGPdLgwPzYK3q9CvIr/0ovEQPgcmQZfYZR7PxTaKT2VR fJBMuF3DlgA== X-Received: from dlam23-n1.prod.google.com ([2002:a05:701b:2097:10b0:1b2:4da6:71c1]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7023:a41:b0:1a3:87c8:7efc with SMTP id a92af1059eb24-1b5569026dcmr32546912c88.39.1796167317591; Tue, 01 Dec 2026 15:21:57 -0800 (PST) Date: Tue, 1 Dec 2026 15:21:14 -0800 In-Reply-To: <20261201232116.1988791-1-irogers@google.com> X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261201232116.1988791-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261201232116.1988791-15-irogers@google.com> Subject: [RFC PATCH 14/16] perf bpf-filter: Support host_ip in BPF sample filters From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Sean Christopherson , Paolo Bonzini Cc: Marc Zyngier , Alexei Starovoitov , linux-perf-users@vger.kernel.org, kvm@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Type: text/plain; charset="UTF-8" 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 --- 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