From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796167294; b=BDRM05L34sM3FeZ74irbiydX5BHolLgQDHUFKu4cSXWaLY4d6IxpidnkxCRVDcKmdRCW 7SendLUnf3vSU+ANFZ63cO16jPC8tN2f9XvXKSv2L1LQwz3o/Jvquo6dnqro/m05F6n6+ pdAuXj+CgZiP22wXQMtJXk3/CrpM/tWJZjZjARsJaqG81C0oMd1nSCg+YtWCcC4LhXPvn nD1qqe6cqyHBG3vmzoqLaB0lKCmsZTsR/WsxULUazFerhLHb1qzTD7H+kB1bWQsaTuhk6 3GEVSlq5mzxo5mRuhdhGMGQ4N6uKr9ex9uNerbxmA3uDCnuotSN0oxIur1exPBV1XGw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796167294; 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=Rtm+gnc/aytnQuSUhTh3x1p2hd65zCnN1Z2VGT6Ukzo=; b=tw6eZzMFhDN4ren4/iX4qjK2TDJo9GPy/UD/RnS6naws3MP/1a8EMli3Sp4wanWKWh9d DUWQCw31xNjN4/Jo083SEYGUGQudoRIa1/4eDa+lBTpbjwI0c8Ztl8kSgZR4JPMu/jHfI rZZTJr5owgBrjAMlJOZyd5TchZ2OFYcEjz72qesa0hKROdMDW+s6WcmPtEKgU10js2vbj LtnbrlxXswhhushViAnAbAHbhb5p1dvXrFJvLWBwXP2PdPZEsIJHNr6GAP4YbPmACVj0i OrnlFKdtztfjotkUqElI+3/GR12gixgiEOueLa1UNl1KCiCZ3v+XsKbROISmS0fpdEA== 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=LVQkoRC3; arc=none smtp.remote-ip=74.125.82.199 Received: from mail-dy1-f199.google.com (mail-dy1-f199.google.com [74.125.82.199]) (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 1D3D8606A6A4 for ; Tue, 1 Dec 2026 23:21:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1D3D8606A6A4 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=LVQkoRC3 Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-3846d5f54c1so13433763eec.1 for ; Tue, 01 Dec 2026 15:21:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796167293; x=1796772093; 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=Rtm+gnc/aytnQuSUhTh3x1p2hd65zCnN1Z2VGT6Ukzo=; b=LVQkoRC3izJ44Qst+N3EmUTznTdzobAXArL2QwcSTQwLBhBRHq+fmOyC6+Ey7kdYuV AcFgaIir8fVHkbLYescKkZzmpQT8pEk0qOl9bfnqRRIFwKzpLCk3xwgsLqbSHAOD5OAv gEoS/y4/Dub150V3VQYMVnrJ7GqYETByScMpES+CVDT9Oh32uEvDATeiwlfEKNg6uh1a +hZyhLSyhIFTdDt1nriUehDSllYwWPWLoulXr9xC/qQYoIixmwG6jNsDJiHTRti5jZvD /y53KXVUh4GQeWj5lvoAxQQQbn8tP0eP0bqFjjQ1eIJMrit9O4um7qGPyiqxiz2RVmGd HvtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796167293; x=1796772093; 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=Rtm+gnc/aytnQuSUhTh3x1p2hd65zCnN1Z2VGT6Ukzo=; b=HxHH8KSqdE4OOZPOR2KqisFZdlbD5lmn2gTgbPG1ddBQb34BNyG6LJt82hISpf6ifZ QGfAjPCzPLESpLI9Axww0PPwdlovMCGGKnHOUxwxsIUhhJ932Y1m3htKAq+NbWa1a1J+ MGjhCqpKq+lYEveu+yjA69i0jVhe9ak+KL0qjbnk/L9Vhj12X85oZvbvyzbFFuN9NV4l imLJnOkjMyVX+xt7sd0plyjM7661mbEpAdOGugTzA/Uiug8HpCIGQaQOdH/jx8Wcl3tW ZM6ri6W/lSe1OW3XKWflu111FhR6S9kyTmDKSgQWw8aAwo+MIh91oCXnDR3iG58nDH2e tjEw== X-Forwarded-Encrypted: i=1; AL+hErGURli764U7xRZms1HgrC7mDHS6r8qLMm7Md6jf/QtsFZnEht5mV/VcV9j9kbnbmho6Q3Y58NVfJkdJSXg=@vger.kernel.org X-Gm-Message-State: APuU4Z8INDaRZlgQw7F2Y0c9eovB87XDFbmB1jkknJZMXNnRmgqqNdEw LibLKT36IHcv7Gyy40XrWGo+0iQ3yt9e+dPI026VmJ9xnsAt9MQQo73QKRfsTEof23wvRP+TGTq LKn+FQ6csOQ== X-Received: from dyw11.prod.google.com ([2002:a05:7300:880b:b0:36a:de0:d28a]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:f401:b0:385:e4d9:e0e7 with SMTP id 5a478bee46e88-385e4d9e3f5mr29894693eec.5.1796167292653; Tue, 01 Dec 2026 15:21:32 -0800 (PST) Date: Tue, 1 Dec 2026 15:21:02 -0800 In-Reply-To: <20261201232116.1988791-1-irogers@google.com> X-Mailing-List: linux-kernel@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-3-irogers@google.com> Subject: [RFC PATCH 02/16] perf/core, bpf: Support BPF filtering of mmap_data events 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 attr.mmap_bpf to struct perf_event_attr and expose the target struct vm_area_struct *vma in struct bpf_perf_event_data_kern. When attr.mmap_data and attr.mmap_bpf are both set on an event, perf_event_mmap_match() invokes the attached BPF_PROG_TYPE_PERF_EVENT program before emitting non-executable PERF_RECORD_MMAP/MMAP2 records. This allows tools such as perf record --host-ip to capture file-backed mappings in hypervisor processes without flooding the ring buffer with anonymous data mappings across the system. Signed-off-by: Ian Rogers --- include/linux/perf_event.h | 3 ++ include/uapi/linux/perf_event.h | 3 +- kernel/events/core.c | 87 +++++++++++++++++++++++++++++++-- 3 files changed, 88 insertions(+), 5 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 8ce5383e4b0a..c31e1bf278ec 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1167,10 +1167,13 @@ struct perf_output_handle { int page; }; +struct vm_area_struct; + struct bpf_perf_event_data_kern { bpf_user_pt_regs_t *regs; struct perf_sample_data *data; struct perf_event *event; + struct vm_area_struct *vma; }; #ifdef CONFIG_CGROUP_PERF diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h index 75ed8e968d30..146df3041c31 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -469,7 +469,8 @@ struct perf_event_attr { sigtrap : 1, /* send synchronous SIGTRAP on event */ defer_callchain: 1, /* request PERF_RECORD_CALLCHAIN_DEFERRED records */ defer_output : 1, /* output PERF_RECORD_CALLCHAIN_DEFERRED records */ - __reserved_1 : 24; + mmap_bpf : 1, /* filter mmap_data events with BPF prog */ + __reserved_1 : 23; union { __u32 wakeup_events; /* wake up every n events */ diff --git a/kernel/events/core.c b/kernel/events/core.c index 6da956ceead0..d32aaf141c9e 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -9880,6 +9880,9 @@ struct perf_mmap_event { } event_id; }; +static int bpf_mmap_handler(struct perf_event *event, + struct vm_area_struct *vma); + static int perf_event_mmap_match(struct perf_event *event, void *data) { @@ -9887,8 +9890,16 @@ static int perf_event_mmap_match(struct perf_event *event, struct vm_area_struct *vma = mmap_event->vma; int executable = vma->vm_flags & VM_EXEC; - return (!executable && event->attr.mmap_data) || - (executable && (event->attr.mmap || event->attr.mmap2)); + if (executable) + return event->attr.mmap || event->attr.mmap2; + + if (!event->attr.mmap_data) + return 0; + + if (event->attr.mmap_bpf && !bpf_mmap_handler(event, vma)) + return 0; + + return 1; } static void perf_event_mmap_output(struct perf_event *event, @@ -10878,6 +10889,58 @@ static int bpf_overflow_handler(struct perf_event *event, return ret; } +static int bpf_mmap_handler(struct perf_event *event, + struct vm_area_struct *vma) +{ + struct pt_regs caller_regs = {}; + struct pt_regs *regs = &caller_regs; + struct perf_sample_data data = { + .addr = vma->vm_start, + .period = vma->vm_end - vma->vm_start, + .sample_flags = PERF_SAMPLE_ADDR | PERF_SAMPLE_PERIOD, + }; + struct bpf_perf_event_data_kern ctx = { + .data = &data, + .event = event, + .vma = vma, + }; + struct bpf_prog *prog; + int ret = 0; + + perf_fetch_caller_regs(regs); + ctx.regs = perf_arch_bpf_user_pt_regs(regs); + + preempt_disable(); + if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) + goto out; + rcu_read_lock(); + /* Pairs with smp_store_release() in perf_event_set_bpf_handler() */ + prog = smp_load_acquire(&event->prog); + if (prog && prog->type == BPF_PROG_TYPE_PERF_EVENT) { + if (prog->call_get_stack && + (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)) { + data.callchain = get_perf_callchain(regs, + !event->attr.exclude_callchain_kernel, + !event->attr.exclude_callchain_user && + is_user_task(current) && current->mm, + event->attr.sample_max_stack, + event->ctx->task && + event->ctx->task != current, + true, 0); + if (!data.callchain) + data.callchain = &__empty_callchain; + data.sample_flags |= PERF_SAMPLE_CALLCHAIN; + } + ret = bpf_prog_run(prog, &ctx); + } + rcu_read_unlock(); +out: + __this_cpu_dec(bpf_prog_active); + preempt_enable(); + + return ret; +} + static inline int perf_event_set_bpf_handler(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie) @@ -10934,6 +10997,12 @@ static inline int bpf_overflow_handler(struct perf_event *event, return 1; } +static inline int bpf_mmap_handler(struct perf_event *event, + struct vm_area_struct *vma) +{ + return 1; +} + static inline int perf_event_set_bpf_handler(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie) @@ -10978,8 +11047,8 @@ static int __perf_event_overflow(struct perf_event *event, if (event->attr.aux_pause) perf_event_aux_pause(event->aux_event, true); - if (!perf_event_is_tracing(event) && READ_ONCE(event->prog) && - !bpf_overflow_handler(event, data, regs)) + if (!event->attr.mmap_bpf && !perf_event_is_tracing(event) && + READ_ONCE(event->prog) && !bpf_overflow_handler(event, data, regs)) goto out; /* @@ -11917,6 +11986,9 @@ static int __perf_event_set_bpf_prog(struct perf_event *event, if (!perf_event_is_tracing(event)) return perf_event_set_bpf_handler(event, prog, bpf_cookie); + if (event->attr.mmap_bpf) + return -EINVAL; + is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_KPROBE; is_uprobe = event->tp_event->flags & TRACE_EVENT_FL_UPROBE; is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT; @@ -13958,6 +14030,13 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr, (attr->sample_type & PERF_SAMPLE_WEIGHT_STRUCT)) return -EINVAL; +#if !defined(CONFIG_BPF_SYSCALL) || !defined(CONFIG_EVENT_TRACING) + if (attr->mmap_bpf) + return -EOPNOTSUPP; +#endif + if (attr->mmap_bpf && !attr->mmap_data) + return -EINVAL; + if (!attr->inherit && attr->inherit_thread) return -EINVAL; -- 2.56.1.915.g69d5ec7298-goog