From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796109433; b=CSy5QwBP4ZokkxQplsjJctTEZIsORgN80VWnxmvCK5pfJM9rp01aidECd7FQQKGL+NJl 8Ek2zA0RpaUMnpXy/eFrUHYub4wLIr18ufcAVnjwb3qqAgAaiH8jeCGTafmYT8H8t7qHn yBibogcLqZRdtSntf6k6vZSGQ0aAL4+xWPPAvnueTESpaeMJnSSWAXqwgiYut+CP6M3z0 D/GbM+43UJg1s5R82OWq/qwC0gulZ1BZ6/drGT/dGe4+Az4ebBhx68sOVmM9c2VCS7X7s 9ggd8pRleJ4n1QI2jZgKdKMVSVnCKrkAssBlkjF2c5DuVRa+cqoZSX7VgKW5hrqrPAg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796109433; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:Mime-Version: X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=KXCTsRp7Z37OAv8qQjZfOyrXSd8eOs5tpuYp56Zo53k=; b=l5aR4LwCWk7DBXDqZQ9s2WOFSyZvyNqMZSllwpD0pm3KiU8XsZhhNMTCXooOS2EebUD7 W71zrpBHrS8Cmegy05LFan/AJbQAd0HVYP0TFOZzxr3iPfMo3ccQK4BuydSi2O45mIeL9 tzhKzaNzZ20abMa7zH2suebDaPRZ20AxTTXbGI2tC90q+DsNEGPOw9Cl9dykT+PPVc5ir /ijU2ZpfsCXdAw3qffpR50HNwm9jSX2VP23a8tA8KTe6KRkIrxdpSgFo+YOyMFScYleXu gWtYHtrdmdKQeig8dUjMeo/4NxE9gMb2ppPkpEyksul/eJtMzardIBSoEVFVkw/1IMQ== 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="F5Xz/Wz6"; arc=none smtp.remote-ip=74.125.82.198 Received: from mail-dy1-f198.google.com (mail-dy1-f198.google.com [74.125.82.198]) (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 15997606A32F for ; Tue, 1 Dec 2026 07:17:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 15997606A32F 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=F5Xz/Wz6 Received: by mail-dy1-f198.google.com with SMTP id 5a478bee46e88-375bac6bd21so10887558eec.0 for ; Mon, 30 Nov 2026 23:17:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796109421; x=1796714221; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=KXCTsRp7Z37OAv8qQjZfOyrXSd8eOs5tpuYp56Zo53k=; b=F5Xz/Wz6xGdH9LyqnAqH/1Fjhfqw7kptMmUKcX1c+FY4/cYzHB7vgAQ+09bf7K2691 lYn5ehgOFeFwQpIvcyHFwIDM4+W3x4ujau7Muxa+7wlmuTU3D2gzF8C3Vi/my5pldO/u UEkt99l4dh2Fk0teLysj2M67HfW1M34M5jizEEKgwdc7EAD6VXRrtRBx8F0vljjeLN1Y ki5KXrIxT5vtc/fmqkj77goYZEVI3YexwTz6GjgEbGAMr0RN5D6eAmyY5XDvC/oxxnql hBTYkOG+CqVV4iHKcLPAoYw8JyOg7dWCrpr6UiypswGcLNTuq8RQSIQsto2qyLF7BgvL ycqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796109421; x=1796714221; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=KXCTsRp7Z37OAv8qQjZfOyrXSd8eOs5tpuYp56Zo53k=; b=YqedWUGExBfXuemcWUnV91eM3iTf6n6EKIiihVR0HSK4SttBE3KdcLykr7iTsj+bal abHkYhN/QLww3wl6VhT0oYUDcSoFkZ3bX3vgHnAelrYKF7iH2POgZeHzGiVb+aJcTvvt WQOfCgW+0yjJ7l/zKZLXCmdHsiP76tOItfA2eBcd8UCY8giCjmc4rpCUtDHyOejXho/D llXtQ5O5rsB2x/M6Q8NZV4/fVPyku8ABrWTp55YIYNiL0i7GG21krIDi3ST/UxrEw0ht QaBKV9HBZ/+RvwZDHx7r6F4t4PPnR7fSqTimVvn61nRPfhdWusdsENfsqJzT5hxJb9CO +2ew== X-Forwarded-Encrypted: i=1; AL+hErGjqxTYwf7dJS6zWOIDcajSY76+Lul1zfR81MMf3Ot2JK9nxQ9+mKDudW1mscFSxefPKeh9IKXTePJ548jBv47n@vger.kernel.org X-Gm-Message-State: APuU4Z9JdlUZJF/Ls59gQ5CGZBzmHROngM9puusT0BsYGtx/2miA3+yI RJ4GM35RX8jx8sYxTmiyNd0T5DRtXL9ottJ3PwjfliKzE5DKxKv1CmLsiAN5rcmvU7DkKROyhii DhmUzm241Vw== X-Received: from dlev19.prod.google.com ([2002:a05:701b:4653:b0:196:8199:c26c]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:51d4:b0:384:fef7:5876 with SMTP id 5a478bee46e88-384fef75c78mr39799417eec.24.1796109420923; Mon, 30 Nov 2026 23:17:00 -0800 (PST) Date: Mon, 30 Nov 2026 23:16:51 -0800 X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261201071653.827828-1-irogers@google.com> Subject: [PATCH v1 0/2] perf/bpf: Fix BPF program inheritance and overflow races From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Eduard Zingerman , Kumar Kartikeya Dwivedi , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , James Clark , Song Liu , Yonghong Song , linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Type: text/plain; charset="UTF-8" This two-patch series fixes pre-existing races, cookie handling, and a reference leak in how perf_event handles BPF programs attached to non-tracing and tracing events: 1. perf_event_alloc() / perf_event_set_bpf_handler() / perf_event_free_bpf_handler(): - Skip inheriting parent_event->prog on tracing events (tracepoints, kprobes, uprobes), where copying event->prog without attaching to tp_event->prog_array caused child teardown to detach the parent's program. - Protect lockless inheritance of parent_event->prog with rcu_tasks_trace + rcu read locks, smp_load_acquire()/ smp_store_release(), xchg(), synchronize_rcu() for sleepable programs, and bpf_prog_inc_not_zero(). - Preserve parent_event->bpf_cookie across event inheritance. - Move perf_event_free_bpf_prog() into __free_event() so perf_event_alloc() error paths cannot leak the inherited bpf_prog reference. 2. __perf_event_overflow() / bpf_overflow_handler(): - Avoid dereferencing event->prog->type outside an RCU read-side critical section in __perf_event_overflow(). - Check prog->type == BPF_PROG_TYPE_PERF_EVENT under rcu_read_lock() using smp_load_acquire(), returning 1 if event->prog was concurrently cleared so samples are not dropped. These issues were identified via code audit and Sashiko automated review while developing perf/BPF changes; the fixes, regression tests, and changelogs were drafted with an AI coding assistant. Testing: - Build-tested kernel/events/core.o across x86_64, arm64, riscv, loongarch, CONFIG_BPF_SYSCALL=n (W=1 clean), and CONFIG_BPF_SYSCALL=y + CONFIG_EVENT_TRACING=y + CONFIG_BPF_EVENTS=n. - Runtime-tested in x86_64 QEMU/KVM with CONFIG_PROVE_LOCKING=y and CONFIG_PROVE_RCU=y using targeted C regression tests covering: (1) inherited tracepoint BPF program continuing to fire after an inherited child exits, (2) bpf_get_attach_cookie_pe() returning the parent's cookie on an inherited child event, and (3) concurrent sampling overflow and bpf_perf_link_release() detach stress loops (40 iterations, zero lockdep/RCU splats). Both patches are based on torvalds/master (and apply cleanly to tip/perf/urgent). Ian Rogers (2): perf/bpf: Fix BPF program inheritance races, cookie, and leak in perf_event_alloc() perf/bpf: Fix racy event->prog dereference in __perf_event_overflow() kernel/events/core.c | 84 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 19 deletions(-) base-commit: 19cc08c1a6a650f8e54c0461ff198be6448f425d -- 2.56.1.839.g22a8290db9-goog