From nobody Mon Nov 23 10:35:26 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795418367; b=utewf5CHPgCMdGdKkG2uqf0mAYw/86jOrEn/M+Vj+J1XG5o0+ZEZTd1syi9LdtFbk2Rm xU7AbNEkO1K8H24QsB8MLrh20SMGLocY6r2ceD9jFDVFAudpISWYqlrs8NkdZLJZlTBL/ ASXa4xNPiCI1qXIsxG56+b5xzURkFYu7+Da2sldOClprzBTZ0lHMNQP2o1ys5xq0T3SfD bYF6S/gv/rs3CNgyviUseEurYKzhG3JNi2EtaeF7UXoBPfxftN5NpY4UA3mzHzwGoBApc TBKeyEseaJyywdSEAopSPwCusuoq4VEzm7VW/azS+1TMciBGumEGoIefrx6SctovzPg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795418367; 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=pjSuvkyG3ApE0/4uLY8VIDkZ8BkkdRf/Widf/DCRqj4=; b=N+485U+v0+hoJNGwCs4BVmzXm7NbnqbHCrcy/EJuiv22VbDBFKJFnGabX0gMMqg0J3IT V/AAe8p8+NWLT/sVje7JG9GYFvoFRS+ftBzXnRJiHuryp3vZGLgdAu2Guxex77fEnykOU rgkjxS94aN/nAm5vlV+T5F50vG3TAyLGPEYsnc27WOwoZ6VVSIL/i7dl0KjHhucz53JoR R5GHwcRkEEWPGpw3w1Mjubs13c4kGrfeOHYOyZ1nYP9eLO9MqY4CAeTqgJHjZ+dcLnjyD JdPAVf8tdtlXERQKDCGhcIowuxqJcdj3lKUDm8C6P2v587r67XCcOYOZVIoj4f0y6HA== 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=Ic82nDw6; arc=none smtp.remote-ip=74.125.82.197 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.197]) (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 8D68E606A337 for ; Mon, 23 Nov 2026 07:19:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8D68E606A337 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=Ic82nDw6 Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-384c78aa0dbso4294585eec.0 for ; Sun, 22 Nov 2026 23:19:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795418366; x=1796023166; 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=pjSuvkyG3ApE0/4uLY8VIDkZ8BkkdRf/Widf/DCRqj4=; b=Ic82nDw6SXtwNow2QrzEjv2c2UPralvonAdxohHl9PYuZyS4hdkweqt9ImSxIr6W3P c0CEgs3kg7vw35v8bTPt4K6UCstQT6HP25XwEraLVj1vCVUWwNqCc60IdU+0EIx87fbN h5SrQ5rA8B6RazevCkEVs/Y4ue1k6gwJd42y7lj7c7KMBP+Ctm0nbLVk9WoBEf6jgHLX YzjNthi30Lqp6tXqoobJLE7kjGX4l9WPaxySGjY59t/VuGpgeCnh8Xe0/U9aoebvVJ6W XCUQeAtnSLq8uaDuEiVahkUSlYh+qQFaEkTQ1fobin6vJo6HiU3ipHShdqqsuGjDXnIN 8wqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795418366; x=1796023166; 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=pjSuvkyG3ApE0/4uLY8VIDkZ8BkkdRf/Widf/DCRqj4=; b=j5QR6oSK584y0FHGOc4URgyELItP9kSBzCYRaUr1g6BXd5pFLuliC8fKCE6F4AjHlF JUqfWeXNynj/9UhrgLl6EVIEcqwZnd6wTGd3PV4Vkt0hZAM+qjrpYcuDpmcb4azqX3to cTqF9LVpv6onK8SrLquaM5Klu+V3QuXDFPML98d4TgSqJu1qThshW0SK55ZyKRk5Llhv 35psq9slDeh231XzkpOq1aAv8cfdXMcmDCS2aFWVgC54hV34r2YMG8cArPLuw2WdoRZX 38Ebd1BEfCOdA0a9B1o8B9FhO4Z1GE6aQWFfHRC0npbIwTCCwe2AfiEGGreWfoKRJX98 ZduQ== X-Forwarded-Encrypted: i=1; AL+hErHYpUmCoLZQciwbRFpOV7itkf62b0/mFCoxQ17EHuDGLrgmaGPXntigKuUuS3Oc/C60HJjhdm13EPXkzTQ=@vger.kernel.org X-Gm-Message-State: APuU4Z+TIFIL928fTerx8PyLPZZS1BUOnqu1J6CBZJVa+lQchaGnOpxY cS/12rjk0rnRzWw7qwL93G1PC3fVesXA+KcnJRBPjbu1AAwdZl41bVIYmzFPGgYmzTYkys+9dtr IHMIcisPJbg== X-Received: from dyvo14-n1.prod.google.com ([2002:a05:693c:41ce:10b0:386:b4f1:dab3]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:6408:b0:36a:270:ce76 with SMTP id 5a478bee46e88-38570f2b27dmr11754304eec.33.1795418365162; Sun, 22 Nov 2026 23:19:25 -0800 (PST) Date: Sun, 22 Nov 2026 23:19:01 -0800 In-Reply-To: <20261123071913.653315-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: <20261123071913.653315-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261123071913.653315-2-irogers@google.com> Subject: [PATCH v1 01/13] perf off-cpu: Bound callchain length in off_cpu_write() From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark Cc: Howard Chu , Thomas Falcon , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" off_cpu_raw has MAX_STACKS + 5 entries and bpf_map_lookup_elem() copies up to MAX_STACKS frames into &off_cpu_raw[i + 2] (with i =3D=3D 2, occupying indices 4..35). When a stack trace fills all MAX_STACKS frames, the unbounded while (off_cpu_raw[i + 2 + len]) loop reads off_cpu_raw[36] (which holds the previous sample's cgroup_id) and then reads and writes past the end of off_cpu_raw. Zero the stack buffer before lookup in case bpf_map_lookup_elem() fails and bound len by MAX_STACKS. Fixes: 74069a01609e ("perf record --off-cpu: Dump the remaining PERF_SAMPLE= _ in sample_type from BPF's stack trace map") Signed-off-by: Ian Rogers --- tools/perf/util/bpf_off_cpu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c index 71933a71c8d1..a3fdbc3c907f 100644 --- a/tools/perf/util/bpf_off_cpu.c +++ b/tools/perf/util/bpf_off_cpu.c @@ -399,10 +399,10 @@ int off_cpu_write(struct perf_session *session) =20 /* off_cpu_raw[i] is callchain->nr (updated later) */ off_cpu_raw[i + 1] =3D PERF_CONTEXT_USER; - off_cpu_raw[i + 2] =3D 0; + memset(&off_cpu_raw[i + 2], 0, MAX_STACKS * sizeof(u64)); =20 bpf_map_lookup_elem(stack, &key.stack_id, &off_cpu_raw[i + 2]); - while (off_cpu_raw[i + 2 + len]) + while (len < MAX_STACKS && off_cpu_raw[i + 2 + len]) len++; =20 off_cpu_raw[i] =3D len + 1; --=20 2.56.1.750.gf70dc682cf-goog From nobody Mon Nov 23 10:35:26 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795418372; b=uAR/EyXh97xO8kaBB4PYXQimInLWkekIt5v+UjdOskICALEMspZvmieXIq2fgzsTzqZf RkfL5j5QCa49yHP7OKvfUhsDxVDAUiYI1RBnk+egOTODxhgyjD1vNmS/02+Bj/sx4EsJZ xoXxkZd8z3WZY8blx+nOSOsbn7Y5quSliY56u4b5qrLDtiDUi5aWqZL3Mbe658SI1V/WU ROSBC3Az5Qp2taHnpWTbTP79p76CgIDCtXfcs0OQsEdbLhNexbpOzBXzL4sqK6ANhvln2 MIJ4zJbgi69sjnimSkpP2ikRMqYY8F0Fvi+6GzKUuasVy0nGb3AvTcsRm63hReqGuAQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795418372; 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=qPfESMdrKb/MeidmJhok80X0fJ4udHOxnwEwH0xvgI8=; b=UKWCYgVTC1vhQNSkoxtUFO7Z8prYwQcFuj1223B+rfxd2qQY4ZvWZ9XDXKmfWkfLI4Hp 70com9jKsRXcaMEsd4Rqu6WCy4zZscYEHvK7V6rG8D0d07vT2/hWpJGLzqw32yt6TRbAN 93RLVOf3nw8mDX8QGJj7QnJlLiW7C2w4j4AfBr6zkqGAgzIYR1a/GWvEJ5VZPbojqgSQ0 QwfhJON6UPcoRXN0CjFWbsu3+hZKKTrA/hzJ+OQ4muuelo5BFnKzByIxnlXhNIhKistEC YDDwflfUWwwsPxjK3aH6o8MG8DVKg+6mHgNfztKIZU8WS8jQjzZo2mLLuOeEFQpkfHg== 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=ceGSKPT6; arc=none smtp.remote-ip=74.125.82.200 Received: from mail-dy1-f200.google.com (mail-dy1-f200.google.com [74.125.82.200]) (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 A9107606A32E for ; Mon, 23 Nov 2026 07:19:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A9107606A32E 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=ceGSKPT6 Received: by mail-dy1-f200.google.com with SMTP id 5a478bee46e88-3869accaac2so1485418eec.1 for ; Sun, 22 Nov 2026 23:19:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795418371; x=1796023171; 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=qPfESMdrKb/MeidmJhok80X0fJ4udHOxnwEwH0xvgI8=; b=ceGSKPT6Ue64iZGfrcLoQAQP9W9jwLkrL8EjeWSuWGCZ4pdN5LJPLjWF5o7BqzLPjc WQftEtSuUhXBZuseZ1rRJgozYSSWVIFf2uUBU2C9O2QCSquu6RzForlIZCV6lTGfMerY qEE736z0hQTTCkXE7VHHQWLP7qVTrpy4U0BisfiB5DyIRYOnGdhHUVZ2+AI8Y8QwVfQx xRR9sXkN3FV5euFUnliXFOk25Am7Y6FgbPCPjgjXcgU/cIW+nDJFM4M9mc4BQTyN8bVX pJf1UkQMbq0l0k9pWEc3hRqM4nCm6QnDgVHpUXg9cHzVwcIeyDIOZGQk8HWOZc/4gE/U y1CQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795418371; x=1796023171; 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=qPfESMdrKb/MeidmJhok80X0fJ4udHOxnwEwH0xvgI8=; b=rGrY/LpeD5aWKu1BiEBq0FAByF8+vdTdajXORHs/sPTbBK+ZR2Bxug3X4X3MzfyQAN 08CJz9lWonnvHHPfq8oNUx5nYiPpRV4UAbsWKjrl466xXg1dYkw2FvTidZdsGISCsl6I YWXN8VU1BM96wimnvs4AvdHv2hj9zmMMM+RqCTLZZOoeOGV6ni/2mFSnkB7lcsYJt6i5 SzRFzKNyFjPX8P5C4rd4fK9YYWTvK4LuiRLSi6iNJaTGvYvNOTnt5LeFK049aGukVbtD UDzgz2f5+f8PGzZLLsYqAUtHPMhtClFtUA7LOfHzmtA7CilMLawLwcZhEMJ2+l04hLEz mS5A== X-Forwarded-Encrypted: i=1; AL+hErHZ63zneiWLZm0GyWvbmrNCjnB6g/rzi1KTo+XlVWzkSyrDxhOel7dki6R3wkKuthVdt1NZi099bIvvpbc=@vger.kernel.org X-Gm-Message-State: APuU4Z+RSRmEen2ng1K0jRTaKOj0XCegxISDuQXqL8kasVljIOLiMcBl EVJkWJhbLgASRQitGiSArLHGGe9LObxNbzuiICY+4adJMNSDDPC91NZSbDW3ETSgBMXJVA1EmnY iGbalBiPzxw== X-Received: from dyls11.prod.google.com ([2002:a05:7300:6a8b:b0:386:af9c:96b6]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:51f8:b0:375:85ac:d14f with SMTP id 5a478bee46e88-384b3e786bdmr14639447eec.14.1795418370446; Sun, 22 Nov 2026 23:19:30 -0800 (PST) Date: Sun, 22 Nov 2026 23:19:02 -0800 In-Reply-To: <20261123071913.653315-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: <20261123071913.653315-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261123071913.653315-3-irogers@google.com> Subject: [PATCH v1 02/13] perf off-cpu: Fix swapped PID and TID in direct off_cpu_dump() samples From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark Cc: Howard Chu , Thomas Falcon , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" __set_offcpu_sample() unpacks the first u64 of an off-cpu raw sample as p32[0] =3D data->pid (process TGID) and p32[1] =3D data->tid (thread PID), matching off_cpu_write() which packs (u64)key.pid << 32 | key.tgid on little-endian hosts (and kernel BPF matches host endianness). However, off_cpu_dump() packed (u64)key->tgid << 32 | key->pid, swapping PID and TID for multi-threaded direct off-cpu samples exceeding the threshold. Pack (u64)key->pid << 32 | key->tgid to match off_cpu_write() and __set_offcpu_sample(). Fixes: d6948f2af24e ("perf record --off-cpu: Dump off-cpu samples in BPF") Signed-off-by: Ian Rogers --- tools/perf/util/bpf_skel/off_cpu.bpf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/util/bpf_skel/off_cpu.bpf.c b/tools/perf/util/bpf_s= kel/off_cpu.bpf.c index 72763bb8d1de..eeb2cb8ac0d8 100644 --- a/tools/perf/util/bpf_skel/off_cpu.bpf.c +++ b/tools/perf/util/bpf_skel/off_cpu.bpf.c @@ -236,7 +236,7 @@ static int off_cpu_dump(void *ctx, struct offcpu_data *= data, struct offcpu_key * { int n =3D 0, len =3D 0; =20 - data->array[n++] =3D (u64)key->tgid << 32 | key->pid; + data->array[n++] =3D (u64)key->pid << 32 | key->tgid; data->array[n++] =3D delta; =20 /* data->array[n] is callchain->nr (updated later) */ --=20 2.56.1.750.gf70dc682cf-goog From nobody Mon Nov 23 10:35:26 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795418377; b=QvPdhVNB16e0Uhb57ZoADFCdpD5dqKXPCm+toIAsm2B3wPg+fA23YcYFZ7eUO0rL45k1 xfwkx2ZmNc9tfmBgSHHr16MBqYU8XPXcxsXS2HFFCq1FdVk7zM29hR0I2o/SpZmOxpSZt GT0brOgI+Q4EX54VkGxWKHj5c+8rrWraVa42ghUlFh7MbtRW3ebogToIYL3fnmcSQoOn9 UF4hrIkIkWUEwExLZjhum85chrJnenKWRrOif06MzGTYG8c6aNq1yf3+g4GPc2oLMO97k kJaZet41c95YrCumLPv1T7OyMIf5Npa+c/nkct46jNkD5CpH/FmiSPd2n5WdmkZDHXg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795418377; 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=IMd33jYcNZqKzcubrxm3ZjVIehLYL/REobXhw2TBdkc=; b=GeUqoAmguFkrZyJ94lV2qQAz+Y1YKhyHNXB/6dmH85zfZk597SS9MBN1t0xcogg2f6ay JUHYL/IhMTfy8SYhDn/QXtBqsApdlpdANsBL0l6h5A4tcH7FMRoAp34M7NTyungX/Y0fq GLRWxScBkx7K0/3HAufNWH6cBmZfThyCeLrvUk0sYxQJ+8e4O7aEGKrbR7rRFBCls4aUa pF9zC0nYJgcELjmmCZ7O940ckSQH41UFw4k0dJEd0/pNpHo2hqnCXN/tY1l9v6unIVCkm 3ebXsT/BVAHFibt7ASb4rv8hp1d+WdMk52Fg1eAmsN6MwX2mBuUFcAHM12nXo7drGzA== 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=EHNqtqVX; arc=none smtp.remote-ip=209.85.214.199 Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.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 D7CAF606A321 for ; Mon, 23 Nov 2026 07:19:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D7CAF606A321 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=EHNqtqVX Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-2f0467a525eso21483735ad.2 for ; Sun, 22 Nov 2026 23:19:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795418376; x=1796023176; 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=IMd33jYcNZqKzcubrxm3ZjVIehLYL/REobXhw2TBdkc=; b=EHNqtqVX924a3sJc8UV+EMhdnAvjiJWuS4zowCnFH2JyG4pP/ljl47bjKQjpo7rOUT ZFQomX+RGXRmx0QtaKPIrvQgYHfIObJ/kxbLRkHJTBAGc/8qLzqe9V603QoLvU9eDAB/ 32maiYjrS0skMqmHVIQInu5+ZfhVVpaKJEFnFyljW0muVsuEyfsYKs9rDhw77bAOIqiZ tGaTVx/stPgcb82KxkuSKRQLPp8fT82n+8/fYqcjshDIlYm0ARx1ndc3RmaFgX2AVv3r lMIlt3BiSzao7BVlpt309ZGu/fPye67XZV3unWw9FSJY+rHuAjUKLjNKVYRAVSsFDwd8 V0fA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795418376; x=1796023176; 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=IMd33jYcNZqKzcubrxm3ZjVIehLYL/REobXhw2TBdkc=; b=GmoaD6MKgu9jAJOEyT6KY6VnnSKL68zPtVlDwdenUWOfynFHGK38yAQUZeP2m6KSFO VIFf7DcZlmX/l9DUfwwzyIAMmJVXR8mX03nOxQTG1doI0tbWMzjMbz96bM20xiFRbrEA D0lSRc05gT/hbBxeCdTI7nTYEAB2CcEuQv7rb1S4gbpa7gpGtOAQYHgav66pieH+cS81 CMTABL1ZDlg6G7lnRmfxyw73cOysOX2wYzNy6zmecz206N1TbS6zVxD2dFBOa6D04l2z j+1wNRR9gRMS/oO/GQ2HCb3VOSCCa8NkbRrGccVNR2AFxYuZv0jAiSsbCRlBaGFL+3i+ je4g== X-Forwarded-Encrypted: i=1; AL+hErGY3uO/UmEM8urN++TUq3r++rsGt6GDsKTCTkIMouuOViXiY2tlzyWztgJrlFHffYbXXgM5ep+HiC0/zJE=@vger.kernel.org X-Gm-Message-State: APuU4Z/enlw0V1LDXr/l1YlZIA6Pk8eRhBcUkyByu8otpf27v1EoeraV HthU5HWbZvFBPW1VnzVjvMe+HcgNzi+WWOV/YceDqB8kASMHzl4woD+U7KZiMEl9RE0/y+lOaRJ yMtkM+kVWWw== X-Received: from pjbiz18.prod.google.com ([2002:a17:90a:e792:b0:3b4:23bc:9fe5]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:442c:b0:3ac:a203:e0a1 with SMTP id 98e67ed59e1d1-3b41c92a902mr9665930a91.37.1795418376056; Sun, 22 Nov 2026 23:19:36 -0800 (PST) Date: Sun, 22 Nov 2026 23:19:03 -0800 In-Reply-To: <20261123071913.653315-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: <20261123071913.653315-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261123071913.653315-4-irogers@google.com> Subject: [PATCH v1 03/13] libperf evlist: Recompute CPU map when an evsel goes system wide From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark Cc: Howard Chu , Thomas Falcon , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" perf_evlist__go_system_wide() marks the evsel as system wide and then propagates the evlist's maps to it. __perf_evlist__propagate_maps() only assigns the PMU CPUs to evsel->cpus if the map is empty, but an evsel added after the evlist's maps were created already has the user requested CPUs propagated (e.g. the "any CPU" map of -1 in per-thread or workload mode). Going system wide then leaves the evsel opened on the wrong CPUs. Drop the existing CPU map before propagating so the evsel is given all of its PMU's CPUs, as is expected for a system wide event. Propagate the maps of all evsels, rather than just the one going system wide, so that evlist->all_cpus is rebuilt. Otherwise the evsel's previous CPUs, such as the "any CPU" -1, remain in evlist->all_cpus and may cause per-thread rather than per-CPU mmaps to be used. Fixes: 811082e4b668 ("perf parse-events: Support user CPUs mixed with threa= ds/processes") Reviewed-by: James Clark Signed-off-by: Ian Rogers --- tools/lib/perf/evlist.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tools/lib/perf/evlist.c b/tools/lib/perf/evlist.c index c1c5b0684eed..c6ce56b46ac1 100644 --- a/tools/lib/perf/evlist.c +++ b/tools/lib/perf/evlist.c @@ -835,7 +835,15 @@ void perf_evlist__go_system_wide(struct perf_evlist *e= vlist, struct perf_evsel * { if (!evsel->system_wide) { evsel->system_wide =3D true; - if (evlist->needs_map_propagation) - __perf_evlist__propagate_maps(evlist, evsel); + if (evlist->needs_map_propagation) { + perf_cpu_map__put(evsel->cpus); + evsel->cpus =3D NULL; + /* + * Propagate the maps of all evsels, rather than just + * this one, so that evlist->all_cpus is rebuilt and + * doesn't retain this evsel's previous CPUs. + */ + perf_evlist__propagate_maps(evlist); + } } } --=20 2.56.1.750.gf70dc682cf-goog From nobody Mon Nov 23 10:35:26 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795418383; b=M0C/6VWyLP/1JxNFXsmz24w5XpEBkCKIDjxr6GJQSVifB3USSra2dNswjQ+aQ7T57ot7 8Ed4wi9pJDYR1hgc6LTV1bvRIrG0d7vk2im9bLiDpLbQaF8/d8fnlMCnxTUPwRziQKNl1 lILdOWn6TI85AdRRLaK3JaJWB94YH/CdNdmn5av7YU5wf/tkr9kyuSZQsx29xZs9eIf+7 pfi1uGoqrqv866GMolnu2wEcCN7JHcWdVi7lluvb4jKv0ikatk9Vt8kv1h6K44mAATOzd AnDBLf2rz3Sd3YKyfMCB8yEuOZDl+iXzywdrbhyg3/93Z6I62V3EeABgsu6pz/4DTmg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795418383; 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=Wmm3nvM1W3EMLy5l9NtCIiGzwg3XfDVkSr3+iewnx60=; b=nduXS1P8koNLLIC9H6jKl5LahA1ZtRVm5RdgqDFR7FBEEOPF5FEhcRLMosg7tFqY9KP/ GfuB1IlZlbOjZwcJRrTwQN7eZZ6iCPBTc1Z7NS9BxV89xTzEdf0XlyJec1lXjX6uP9qdK 6SiHBVZms2egMTEkFQ0iWZKOwD1ZnsDsGMKY7iUsJr0tJ1f69WRoF9qmbe/64WvdJeMAB 6LyPVc0ucoK944hAwhbiU1wRBUONzRdH48/5G3rLBXotv3Ibi2xN9rxJmbZiggTW3eIGP BMdY7FvCL+d8tCl2jblQy63jtCIFQWsRa/NdiS9x+WMMmnTW4T0kRCZ0JaCnPTp+RaQ== 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="lItsg/g/"; arc=none smtp.remote-ip=74.125.82.197 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.197]) (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 BB11A606A32C for ; Mon, 23 Nov 2026 07:19:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BB11A606A32C 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=lItsg/g/ Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-3846d5f54c1so3804264eec.1 for ; Sun, 22 Nov 2026 23:19:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795418382; x=1796023182; 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=Wmm3nvM1W3EMLy5l9NtCIiGzwg3XfDVkSr3+iewnx60=; b=lItsg/g/E08jN2UTKt+YFPM+kzBxLSvGGobTNPyM2XglbEq6nxtoOkj/kfehwu4iWn ZFkiL0q/zgvdgcQXqz50gJWMyxDxFdf1mXBuYmunFQGjoktoawwL96Y3HZ65S0kwWBBb XkHnsY4r7ZRUVANseyjXCXDGZ5bDXkvQFVwLlDS8HsjgWX5rHhdx7M61Cl1nDQGUGJoO /wjLqC4Uc/1lWYbzg2Sr3EtXop6IWFJJIdxbDZaWPVSPXyV3uradW61FwCNYMQpAwy89 mJAdJRyJnECsVnBdonA58R/bD7peXwpyxAE2/6hA+nCVl63ndeyQv90chHBuuz46zdBI tc/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795418382; x=1796023182; 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=Wmm3nvM1W3EMLy5l9NtCIiGzwg3XfDVkSr3+iewnx60=; b=NIGqXfdAxBObXizci7tuukiZ+UxLgBSwmfLSsAI8tKLDuBb44IFu/gkIiQ19KtUw0A zWDRPiqrZPblerkieyp5tGcTr47+myb2osA9szcm10IPhnxfqq9W0djbvBNSY8Ik7jwl u/C4k0ec/fpYDwp2nC5EgVUt7xFBa/nTpbLdt6t+xlKChCilQvJ/iahcyBF/CMLwGuTr 67zfKErGirv43F8HnRA5mPL4uczbeyPihYbVkueJ5UyeyTe5DXkqAgUbDiRD7z6aUXkb vHywuyJ1lNuAY8JYqdaHoizA44CBZd8Rc1aixjNDWGcsYhxV4Ct55vnLzvL44veh8VCo 7pyQ== X-Forwarded-Encrypted: i=1; AL+hErG5Yu228a0JEdi5SVSMDU5MR5vSjL9MT2NnpojwcxMFvCTppiv/DezvjvqxMtldngjwcLrX5YbYQdrBErQ=@vger.kernel.org X-Gm-Message-State: APuU4Z+kYPTLff3SiqR7LLgG4k5MAb2NiW1BaqYE/gEz2V8pMZwMWPkH q/jnLGPLDXUJjVhFELAUoyvGXULxQtCD3S2BHtuQKmbHswBhu/rQUSir0sboU0ZXlAld2+YeDr/ 7I/qL/23OLQ== X-Received: from dybna20.prod.google.com ([2002:a05:7300:cc14:b0:386:95bc:ec27]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:2202:b0:36a:1e9e:ae61 with SMTP id 5a478bee46e88-3857170afa1mr16311717eec.38.1795418381412; Sun, 22 Nov 2026 23:19:41 -0800 (PST) Date: Sun, 22 Nov 2026 23:19:04 -0800 In-Reply-To: <20261123071913.653315-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: <20261123071913.653315-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261123071913.653315-5-irogers@google.com> Subject: [PATCH v1 04/13] perf stat: Give non-perf_event_open events unique IDs for stat record From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark Cc: Howard Chu , Thomas Falcon , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" store_evsel_ids() skips tool, hwmon, DRM and retire latency events as there is no perf event file descriptor to read an ID from. Their perf_sample_id entries are left with an ID of 0, so the stat events written by perf stat record all have an ID of 0. When reading the file, evlist__id2evsel() maps an ID of 0 to the first evsel, so the counts of all such events are attributed to the first evsel and the other events are reported as not counted. For example: $ perf stat record -e duration_time,num_cpus,task-clock true $ perf stat report 28 duration_time num_cpus 0.84 msec task-clock Give each CPU and thread of these events a unique synthetic ID. Kernel IDs count up from 1, so the synthetic IDs count down from the largest ID to be as far from them as possible; reaching them would need ~2^63 perf events to have been created. As a further guard, skip any ID that is already in use by the evlist. Fixes: d53b4996584b ("perf evsel: Skip store_evsel_ids for non-perf-event P= MUs") Signed-off-by: Ian Rogers --- tools/perf/util/evsel.c | 42 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 737a8680aaae..1f96c170d277 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -4727,15 +4727,51 @@ struct perf_env *evsel__env(struct evsel *evsel) return session ? perf_session__env(session) : NULL; } =20 +/* + * Get an ID for an event not opened with perf_event_open, such as a tool = PMU + * event. Kernel assigned IDs count up from 1, so count down from the larg= est + * ID to keep as far away from them as possible. Reaching the kernel's IDs + * would need ~2^63 perf events to have been created, but also skip any ID + * already in use by the evlist. + */ +static u64 evlist__next_synthetic_id(struct evlist *evlist) +{ + static u64 next_synthetic_id =3D ~0ULL; + u64 id; + + do { + id =3D next_synthetic_id--; + } while (evlist__id2sid(evlist, id)); + + return id; +} + static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist) { int cpu_map_idx, thread; =20 - if (evsel__is_retire_lat(evsel)) - return 0; + if (evsel__is_retire_lat(evsel) || + perf_pmu__kind(evsel->pmu) !=3D PERF_PMU_KIND_PE) { + struct xyarray *sample_id =3D evsel->core.sample_id; =20 - if (perf_pmu__kind(evsel->pmu) !=3D PERF_PMU_KIND_PE) + if (!sample_id) + return 0; + + /* + * There's no perf event to read an ID from. Give each + * CPU/thread a unique synthetic ID, otherwise all such events + * have ID 0 and stat events written by perf stat record are + * attributed to the first evsel. + */ + for (cpu_map_idx =3D 0; cpu_map_idx < xyarray__max_x(sample_id); cpu_map= _idx++) { + for (thread =3D 0; thread < xyarray__max_y(sample_id); thread++) { + perf_evlist__id_add(evlist__core(evlist), &evsel->core, + cpu_map_idx, thread, + evlist__next_synthetic_id(evlist)); + } + } return 0; + } =20 for (cpu_map_idx =3D 0; cpu_map_idx < xyarray__max_x(evsel->core.fd); cpu= _map_idx++) { for (thread =3D 0; thread < xyarray__max_y(evsel->core.fd); --=20 2.56.1.750.gf70dc682cf-goog From nobody Mon Nov 23 10:35:26 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795418385; b=g0qbAT2Zo/Us4pyhsVp+QK4uBapZevNsJkEqIgo6apLZXfPirEDZgqOmk+6220SEKooH WO33423gKCqK8fUXiR6bsMdqyUYNqbAHiEW+tt1Z4Tp2QaDHt026KyGYEPtNDnCeKno6v 6z8Gr/xfurUhJUXyIqBEBflIHJCKOrm+nuKJ4EhdUFUaz4+ulETJWvzxzdIos/JkaqsKm SLgIDfOANMNn3IzjhpnPVDRap5r4WKbz337KCOg+8+qYAcVzLvUPGS2N1Ji3GuFCLy06T WVkeI7LHs9ueuop9FVvMP7hRUSYUuyaztG6yK71tFEn/rrC6l4enN51mLwc7N1j6wsQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795418385; 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=uUik2QpbhM20Rc2adhMxY4bt3Xj1uba6ieJfcuhtQ5k=; b=OTnnBcEhp7ou2GnpVTpGJjlBd8QIGf3YXvjRq/5C/wgWujck8rmDxXxfFNrWdhQWj30P 6LDV8MvqN9XTT12xisk0g4SgYzguwmODIc944e1QCh2z45nBNrqaLlsl6GuwNdi3WyRUM axaAK1vUfAg/tiKyTIe9eMp+HTcrDeWHn8w9aF3QcVYLaw8IYFT6m6NihpzY5sFJUOTya jxYe1B3po2BtJsLcJazRZDW+o5IxTnWfJmuW4o8irsovR1czg/fNCrNRJVm4+/Nw3QeAn G1AFsFle5yWxt6d/WkcT3F1NEKe+Fk1/l3641SI4w2MqZMJt1GMl+diunySNNxRWQkw== 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=O71O27ef; arc=none smtp.remote-ip=209.85.215.198 Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.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 3E5AA606A335 for ; Mon, 23 Nov 2026 07:19:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3E5AA606A335 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=O71O27ef Received: by mail-pg1-f198.google.com with SMTP id 41be03b00d2f7-cdd97f89731so1699595a12.1 for ; Sun, 22 Nov 2026 23:19:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795418384; x=1796023184; 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=uUik2QpbhM20Rc2adhMxY4bt3Xj1uba6ieJfcuhtQ5k=; b=O71O27efqwUA8tE8hD5ntss+eWJEBW3b9RfdoUZjYh5XDqzGwp4ubj0122eXExWg53 kdtG4Od9PjgpTytG527C1QF6YeyoEpFWWg7v96Zmv2KppwraXevXWQbvbiIipBDvJ51u /B/QZFIqFGperEp5Ud1OtsVEyOd1r/rasVV/asyH7gWOD20xTna/xw7C1wOvEgVVIsZ4 FX+lwScviuToBhYIFm2atetHBchh8mQ2vQsV2pafMHcWAwZ4jgKgHb/wXjz3zK8Yasgy 4OvVwKtG+m6ZDILZcqoAN1skmFpPZQ+xlMbgpIE9yrGBLBtmMLEaTwfcvixgrHwgskbj byBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795418384; x=1796023184; 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=uUik2QpbhM20Rc2adhMxY4bt3Xj1uba6ieJfcuhtQ5k=; b=LKH5veJAjfOJGCzIVlzWwISvxsndT0Bq1oLYlRrl7eYtTh6M3eMuyXrr2StmZlcXTA 0qBHagBi6rcc1nWqZR59H6hsVnmL6QIaDAXiTp1nXYU7oS/kJGlcFvz6UEpQQO51wEJQ 5F6b0m3VhuyDzlSOPxFw+nnxuk0rGm9cKNqG5LNtMyOpzJNGQS9ZfMXbS7EDZUMhbUI5 1Od6Wd0M9V5NM4nMQ/JYy6EimGWO+bKyT4gNM0eDEhDOsXLD5DSwjlImag3EhRGYGvsH FOn42Mwa3r0pm7nzyTrK2GcVZDgNSb21V/Ok1HVSQmc6KKX5VVo6ABBi+/pg+y4dYsDG Jq8g== X-Forwarded-Encrypted: i=1; AL+hErGIg9lhgCMxsIejk+acKRX2SBbgPgcGvWTnr5YfH4DJdQY8QRMnAMNcfqXH8tU9F0IewIDuA16uBVb34qc=@vger.kernel.org X-Gm-Message-State: APuU4Z9mm9LgmwDT4Fa7MmOf3IjUzoz34NrX7L9dkd/qgiczm3Ocl8Ii iH3uvMOe/gOQRDJJ8ZlE/2OLIRWOU8XTU7pkIslxDnnUdw5izi1PyuqCQpOlJ5kBx/c99ZhCxEK xpd4eGzbw8g== X-Received: from pgij18.prod.google.com ([2002:a05:6a02:8412:b0:cde:7fbd:d614]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2fc4:b0:3b4:2353:5f89 with SMTP id 98e67ed59e1d1-3b42353601emr7365922a91.6.1795418384383; Sun, 22 Nov 2026 23:19:44 -0800 (PST) Date: Sun, 22 Nov 2026 23:19:05 -0800 In-Reply-To: <20261123071913.653315-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: <20261123071913.653315-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261123071913.653315-6-irogers@google.com> Subject: [PATCH v1 05/13] perf stat: Preserve per-evsel CPU maps across stat record and report From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark Cc: Howard Chu , Thomas Falcon , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Commit 6d765f5f7ec6 ("libperf evsel: Rename own_cpus to pmu_cpus") and commit 811082e4b668 ("perf parse-events: Support user CPUs mixed with threads/processes") separated an evsel's PMU cpumask (pmu_cpus) from the actual CPUs on which the event is opened (cpus) and taught __perf_evlist__propagate_maps() to collapse evsel->cpus to the "any CPU" (-1) map when user_requested_cpus is -1 and !evsel->requires_cpu. However, perf_event__synthesize_event_update_cpus() continued to serialize evsel->core.pmu_cpus instead of evsel->core.cpus, and perf_event__process_event_update() only populated evsel->core.pmu_cpus without setting evsel->core.cpus or evsel->core.requires_cpu. This broke perf stat record/report (and perf script) in two ways: 1. When recording an event opened per-CPU alongside a workload target (where user_requested_cpus is -1), such as an event with a cpu=3D term, an uncore counter, --bpf-counters, or a BPF-filtered counter, PERF_RECORD_STAT events are written with concrete CPU numbers, but perf stat report's perf_evlist__set_maps() collapses evsel->cpus to -1 and fails with: $ perf stat record -e 'task-clock/cpu=3D0/,task-clock' -- true $ perf stat report Invalid CPU 0 for event task-clock/cpu=3D0/. 2. In system-wide mode (-a), per-evsel CPU restrictions such as cpu=3D0 were overwritten by the full PMU cpumask during report, causing perf stat report -A to report bogus entries on every other CPU. Fix perf_event__synthesize_event_update_cpus() to serialize evsel->core.cpus, and update perf_event__process_event_update() to populate evsel->core.cpus and set requires_cpu when the recorded CPU map does not use the "any CPU" (-1) placeholder. For backward compatibility with data files recorded by older perf binaries that wrote pmu_cpus for per-task (-1) counters, fall back to the "any CPU" map in perf_event__process_stat_event() if a -1 CPU stat event arrives for an evsel without -1 in its CPU map. Update the event_update unit test and stat.sh record/report/script tests to cover per-evsel CPU maps on workload targets. Fixes: 6d765f5f7ec6 ("libperf evsel: Rename own_cpus to pmu_cpus") Fixes: 811082e4b668 ("perf parse-events: Support user CPUs mixed with threa= ds/processes") Signed-off-by: Ian Rogers --- tools/perf/tests/event_update.c | 43 ++++++++++++++++++++++++++---- tools/perf/tests/shell/stat.sh | 16 +++++++++++ tools/perf/util/evsel.c | 2 +- tools/perf/util/header.c | 5 ++++ tools/perf/util/stat.c | 18 +++++++++++++ tools/perf/util/synthetic-events.c | 7 +++-- 6 files changed, 83 insertions(+), 8 deletions(-) diff --git a/tools/perf/tests/event_update.c b/tools/perf/tests/event_updat= e.c index 220cc0347747..bacf3c44c8e2 100644 --- a/tools/perf/tests/event_update.c +++ b/tools/perf/tests/event_update.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include #include +#include #include #include "cpumap.h" #include "evlist.h" @@ -58,13 +59,20 @@ static int process_event_name(const struct perf_tool *t= ool, return 0; } =20 -static int process_event_cpus(const struct perf_tool *tool __maybe_unused, +struct event_cpus { + struct perf_tool tool; + struct evlist *evlist; +}; + +static int process_event_cpus(const struct perf_tool *tool, union perf_event *event, struct perf_sample *sample __maybe_unused, struct machine *machine __maybe_unused) { + struct event_cpus *tmp =3D container_of(tool, struct event_cpus, tool); struct perf_record_event_update *ev =3D (struct perf_record_event_update = *)event; - struct perf_cpu_map *map; + struct perf_cpu_map *map, *any_cpu; + struct evsel *evsel; =20 map =3D cpu_map__new_data(&ev->cpus.cpus); =20 @@ -75,6 +83,26 @@ static int process_event_cpus(const struct perf_tool *to= ol __maybe_unused, TEST_ASSERT_VAL("wrong cpus", perf_cpu_map__cpu(map, 1).cpu =3D=3D 2); TEST_ASSERT_VAL("wrong cpus", perf_cpu_map__cpu(map, 2).cpu =3D=3D 3); perf_cpu_map__put(map); + + evsel =3D evlist__first(tmp->evlist); + perf_cpu_map__put(evsel->core.cpus); + evsel->core.cpus =3D NULL; + perf_cpu_map__put(evsel->core.pmu_cpus); + evsel->core.pmu_cpus =3D NULL; + evsel->core.requires_cpu =3D false; + + TEST_ASSERT_VAL("failed to process attr update cpus", + !perf_event__process_event_update(tool, event, &tmp->evlist)); + + any_cpu =3D perf_cpu_map__new_any_cpu(); + TEST_ASSERT_VAL("failed to allocate any_cpu map", any_cpu); + perf_evlist__set_maps(evlist__core(tmp->evlist), any_cpu, NULL); + perf_cpu_map__put(any_cpu); + + TEST_ASSERT_VAL("wrong processed cpus", perf_cpu_map__nr(evsel->core.cpus= ) =3D=3D 3); + TEST_ASSERT_VAL("wrong processed cpus", perf_cpu_map__cpu(evsel->core.cpu= s, 0).cpu =3D=3D 1); + TEST_ASSERT_VAL("wrong processed cpus", perf_cpu_map__cpu(evsel->core.cpu= s, 1).cpu =3D=3D 2); + TEST_ASSERT_VAL("wrong processed cpus", perf_cpu_map__cpu(evsel->core.cpu= s, 2).cpu =3D=3D 3); return 0; } =20 @@ -82,6 +110,7 @@ static int test__event_update(struct test_suite *test __= maybe_unused, int subtes { struct evsel *evsel; struct event_name tmp; + struct event_cpus tmp_cpus; struct target target =3D {}; struct evlist *evlist =3D evlist__new_default(&target, /*sample_callchain= s=3D*/false); =20 @@ -111,11 +140,15 @@ static int test__event_update(struct test_suite *test= __maybe_unused, int subtes TEST_ASSERT_VAL("failed to synthesize attr update name", !perf_event__synthesize_event_update_name(&tmp.tool, evsel, process_eve= nt_name)); =20 - perf_cpu_map__put(evsel->core.pmu_cpus); - evsel->core.pmu_cpus =3D perf_cpu_map__new("1,2,3"); + perf_cpu_map__put(evsel->core.cpus); + evsel->core.cpus =3D perf_cpu_map__new("1,2,3"); + + perf_tool__init(&tmp_cpus.tool, /*ordered_events=3D*/false); + tmp_cpus.evlist =3D evlist; =20 TEST_ASSERT_VAL("failed to synthesize attr update cpus", - !perf_event__synthesize_event_update_cpus(&tmp.tool, evsel, process_eve= nt_cpus)); + !perf_event__synthesize_event_update_cpus(&tmp_cpus.tool, evsel, + process_event_cpus)); =20 evlist__put(evlist); return 0; diff --git a/tools/perf/tests/shell/stat.sh b/tools/perf/tests/shell/stat.sh index ac9e031831da..43a158ee3609 100755 --- a/tools/perf/tests/shell/stat.sh +++ b/tools/perf/tests/shell/stat.sh @@ -77,6 +77,14 @@ test_stat_record_report() { err=3D1 return fi + cpu=3D$(sed 's/[-,].*//' /sys/devices/system/cpu/online) + if ! perf stat record -e "task-clock/cpu=3D${cpu}/,task-clock" -o - true= | \ + perf stat report -i - 2>&1 | grep -E -q "task-clock/cpu=3D${cpu}/" + then + echo "stat record and report test [Failed per-evsel CPU]" + err=3D1 + return + fi echo "stat record and report test [Success]" } =20 @@ -89,6 +97,14 @@ test_stat_record_script() { err=3D1 return fi + cpu=3D$(sed 's/[-,].*//' /sys/devices/system/cpu/online) + if ! perf stat record -e "task-clock/cpu=3D${cpu}/,task-clock" -o - true= | \ + perf script -i - 2>&1 | grep -E -q "task-clock/cpu=3D${cpu}/" + then + echo "stat record and script test [Failed per-evsel CPU]" + err=3D1 + return + fi echo "stat record and script test [Success]" } =20 diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 1f96c170d277..64daef4cc345 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1822,7 +1822,7 @@ void evsel__config(struct evsel *evsel, const struct = record_opts *opts, attr->exclude_user =3D 1; } =20 - if (evsel->core.pmu_cpus || evsel->unit) + if (evsel->core.cpus || evsel->core.pmu_cpus || evsel->unit) evsel->core.attr.read_format |=3D PERF_FORMAT_ID; =20 /* diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index 9eec6deee190..e9af99b8c0f8 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -5962,6 +5962,11 @@ int perf_event__process_event_update(const struct pe= rf_tool *tool __maybe_unused if (map) { perf_cpu_map__put(evsel->core.pmu_cpus); evsel->core.pmu_cpus =3D map; + if (!evsel->counts) { + perf_cpu_map__put(evsel->core.cpus); + evsel->core.cpus =3D perf_cpu_map__get(map); + evsel->core.requires_cpu =3D !perf_cpu_map__has_any_cpu(map); + } } else pr_err("failed to get event_update cpus\n"); break; diff --git a/tools/perf/util/stat.c b/tools/perf/util/stat.c index 6da3dbbde0e2..429bfc339308 100644 --- a/tools/perf/util/stat.c +++ b/tools/perf/util/stat.c @@ -8,6 +8,7 @@ =20 #include #include +#include =20 #include "counts.h" #include "cpumap.h" @@ -674,6 +675,23 @@ int perf_event__process_stat_event(const struct perf_t= ool *tool __maybe_unused, return -EINVAL; } cpu_map_idx =3D perf_cpu_map__idx(evsel__cpus(counter), (struct perf_cpu)= {.cpu =3D st->cpu}); + if (cpu_map_idx =3D=3D -1 && (s32)st->cpu =3D=3D -1 && counter->counts && + evsel__nr_cpus(counter) > 0) { + /* + * Older perf tools wrote pmu_cpus instead of cpus in + * PERF_EVENT_UPDATE__CPUS, leaving per-task counters with a + * CPU list instead of the "any CPU" (-1) map used by their + * PERF_RECORD_STAT events. + */ + struct perf_cpu_map *any_cpu =3D perf_cpu_map__new_any_cpu(); + + if (any_cpu) { + perf_cpu_map__put(counter->core.cpus); + counter->core.cpus =3D any_cpu; + counter->core.requires_cpu =3D false; + cpu_map_idx =3D 0; + } + } if (cpu_map_idx =3D=3D -1) { pr_err("Invalid CPU %d for event %s.\n", st->cpu, evsel__name(counter)); return -EINVAL; diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic= -events.c index 3c8d29f27fd9..b87d15a5be6a 100644 --- a/tools/perf/util/synthetic-events.c +++ b/tools/perf/util/synthetic-events.c @@ -2234,10 +2234,13 @@ int perf_event__synthesize_event_update_name(const = struct perf_tool *tool, struc int perf_event__synthesize_event_update_cpus(const struct perf_tool *tool,= struct evsel *evsel, perf_event__handler_t process) { - struct synthesize_cpu_map_data syn_data =3D { .map =3D evsel->core.pmu_cp= us }; + struct synthesize_cpu_map_data syn_data =3D { .map =3D evsel->core.cpus }; struct perf_record_event_update *ev; int err; =20 + if (!evsel->core.cpus) + return 0; + ev =3D cpu_map_data__alloc(&syn_data, sizeof(struct perf_event_header) + = 2 * sizeof(u64)); if (!ev) return -ENOMEM; @@ -2315,7 +2318,7 @@ int perf_event__synthesize_extra_attr(const struct pe= rf_tool *tool, struct evlis } } =20 - if (evsel->core.pmu_cpus) { + if (evsel->core.cpus) { err =3D perf_event__synthesize_event_update_cpus(tool, evsel, process); if (err < 0) { pr_err("Couldn't synthesize evsel cpus.\n"); --=20 2.56.1.750.gf70dc682cf-goog From nobody Mon Nov 23 10:35:26 2026 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=1795418390; b=10xfDR/WnLmezUL+EVGMNQ/TUwPNVds+TWxMme/cFLunB9767V/Nif6jLP/9/wv480kD nvXNKnxR6LI3d+57HQjf9wWqQFOBx026YwaEZDpfAvojUs7XrXP2DpE5Gzz6ykRUv3qum OsDqG5m8I69q4h1twBSn56TK1ZMudBEvykL2fFde8HdSt3W1oc5Y3AlXSaEl2UrDpZXks 3HwzAf3xYJ710UjKhdZVeaknoBFdgjhSPz3UJh/iJuRiWra/BQWK4VLVJ7MMZX3DOEcYB 6a2ZTpRFdkHy+2nG09l/WYrucAzNG9lKbjjJHGFYt9HidOQFhaiqgvZ8gd+9tGwvZxQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795418390; 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=YodIznWGmX5HPtvn02byN4wNfgJjvly3P8NRcax/huo=; b=Vb9E0ZCiHcKyiEntAWWJTeg7qAf8RZId/gN7pqAeMYQ1mRUBw5Ot2PaNzXj6ZTvoRDZg ODLaAVW5eWfiHtXkr0Wr4QmRigLDvJPT0bUZL+CINd7h9mdtcnuaRYvKiLTN3iFz0lf3E 9gQpnZb4xCltNS6KC64b94AqAjNhFFoHFruJPN4USGzczJs3wI9r4HCO3sgHaX0+veZlI mdUITB6izxgNBzN2qKAv8vN/kCsX8d+6Zrg1BaVF0XZi3kVsHQ96gAiowARrpnOx7+KaU 3Uz3c6PLyXLYoINN10mbXBK/Vr7UOzJnzJ0w+6W9Y7oyGYx0MCOn7GuKBK+WKWsF14A== 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=lYqEMEpz; 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 6D07D606A327 for ; Mon, 23 Nov 2026 07:19:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6D07D606A327 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=lYqEMEpz Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-30bcb065bfdso3384918eec.0 for ; Sun, 22 Nov 2026 23:19:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795418389; x=1796023189; 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=YodIznWGmX5HPtvn02byN4wNfgJjvly3P8NRcax/huo=; b=lYqEMEpzNV7pV8uPb5bqdA48c0j8ya0I4q9fdSus6fOrOwmj94B3EeFjtSSLeAVqxQ xFth1nCTuaa/Q0dnvRhTPHNq4dfcxxmxtIwe9a5WZc83yQ2DRHnLxTjuhgJ7rlZQ3A+p PlmeeV2lunO+gW6T3GYVLQIaY4cX1p/u6Fz67HJMq2/AKbJPJxJ51xw9TGVQi2sVLql7 aOdzeOPO34+2R1yqi+5GhWM+Kzpn1OgpFsfdfxMY3W21gDp/p8+isAwBPMsXB3PH54AO ojrMoUzemMVxoJSiKw66iARaNSnFOk4g3xipaD6VXhnoOWYRfM+NKA47K+mFvPpReAex ysLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795418389; x=1796023189; 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=YodIznWGmX5HPtvn02byN4wNfgJjvly3P8NRcax/huo=; b=O7p4OVKmNo/GM0CPDY6xWKMwG2cp2d2iSIujgA3vIqpdG9id82f3sLVwldu5n6KshJ 3P4LRwiOLhxZUrT6qzNIiZGWwO9dBR5/Qs9PrMkNrFgb1KznLVPVZf/RtDiX3O8m9sdu 3vCOAmfJ9h6nLlT2a9XkJT6FMlVKmQMRmgbW5O51Ifyu5zx3HGkixtXrSr73yGjabrGw qc37w2UzRjYYCOF9yF3CFNSbO51xmeb9uvbqybxRAHRNLjqoYOX4SDLFhtuuX/7GZRMB jIBCUSVmhyv/MF4oJBIX9vf5Pv2s7WBsPs553VbuGHD8RLW3+jAyfqNQxyCPsjmJKthr gFHA== X-Forwarded-Encrypted: i=1; AL+hErHClTkU5ca0q/C9GaHEh0eYdwtLsUF3zr2FSUeFq4JOaA6EaFWeLQjhxC+KHFyyoixg4CKzAT5emdLfP4o=@vger.kernel.org X-Gm-Message-State: APuU4Z8N0AwOf5ExKBS2TWw22LS2B0sqwx0Qd22tZtkYNaB98IE16UDg 2TGEjZAhpZkrAOsrskl79N40Tq1w43swV9eSBEG0RpAJiJC9ZzCtCZlYdnkGhbO1tpoOSqe8hI6 +SvyGbsgroQ== X-Received: from dycue7.prod.google.com ([2002:a05:7300:87:b0:386:46f:406]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:693c:88cd:20b0:386:a16d:1bce with SMTP id 5a478bee46e88-386a16d1cb0mr5714521eec.36.1795418389048; Sun, 22 Nov 2026 23:19:49 -0800 (PST) Date: Sun, 22 Nov 2026 23:19:06 -0800 In-Reply-To: <20261123071913.653315-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: <20261123071913.653315-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261123071913.653315-7-irogers@google.com> Subject: [PATCH v1 06/13] perf pmu: Don't treat tool PMU as a kernel software PMU for event grouping From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark Cc: Howard Chu , Thomas Falcon , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" perf_pmu__is_software() identifies kernel software PMUs (such as PERF_TYPE_SOFTWARE, PERF_TYPE_TRACEPOINT, and PERF_TYPE_BREAKPOINT) whose events can share a perf_event_open() group_fd with a hardware PMU event. Userspace-implemented PMUs like the tool, hwmon, and drm PMUs do not open perf_event file descriptors (leaving FD(leader, ...) =3D=3D -1), so keeping= a userspace PMU event grouped with a kernel PMU event hits BUG_ON(fd =3D=3D -1 && leader->supported) in get_group_fd() (e.g. '{duration_time,cycles}' or '{duration_time,task-clock}') or fails in evsel__read_group() (e.g. '{cycles,duration_time}' or '{task-clock,duration_time}'). Return false for PERF_PMU_TYPE_TOOL in perf_pmu__is_software(), matching hwmon and drm PMUs, and ensure evsel__compute_group_pmu_name() only substitutes perf_event_open PMU names for software and AUX events so '{duration_time,task-clock}' does not adopt the tool PMU's group name while preserving same-PMU userspace groups such as '{hwmon_a/e1/, hwmon_a/e2/}' and '{duration_time,user_time}'. Also check the group leader's attr.disabled in evsel__tool_pmu_open() and guard get_group_fd() and evsel__group_read_nr_members() against non-perf_event_open PMUs. Fixes: 240505b2d0ad ("perf tool_pmu: Factor tool events into their own PMU") Signed-off-by: Ian Rogers --- tools/perf/builtin-stat.c | 7 +++- tools/perf/util/evsel.c | 76 +++++++++++++++++++++++++++------- tools/perf/util/parse-events.c | 24 +++++++---- tools/perf/util/pmu.c | 2 +- tools/perf/util/tool_pmu.c | 6 ++- 5 files changed, 87 insertions(+), 28 deletions(-) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 37752d6bfef4..4fe4da20aae8 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -721,7 +721,9 @@ static int create_perf_stat_counter(struct evsel *evsel, * the group read (for leader) and ID retrieval for all * members. */ - if (leader->core.nr_members > 1) + if (leader->core.nr_members > 1 && + !evsel__is_non_perf_event_open_pmu(leader) && + !evsel__is_non_perf_event_open_pmu(evsel)) attr->read_format |=3D PERF_FORMAT_ID|PERF_FORMAT_GROUP; =20 attr->inherit =3D !config->no_inherit && list_empty(&evsel->bpf_counter_l= ist); @@ -750,7 +752,8 @@ static int create_perf_stat_counter(struct evsel *evsel, * either manually by us or by kernel via enable_on_exec * set later. */ - if (evsel__is_group_leader(evsel)) { + if (evsel__is_group_leader(evsel) || + evsel__is_non_perf_event_open_pmu(leader)) { attr->disabled =3D 1; =20 if (target__enable_on_exec(&target)) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 64daef4cc345..babeccfbecb5 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -430,7 +430,8 @@ void evsel__set_sample_id(struct evsel *evsel, =20 bool evsel__is_non_perf_event_open_pmu(const struct evsel *evsel) { - return evsel->pmu && evsel->pmu->type > PERF_PMU_TYPE_PE_END; + return evsel->pmu && evsel->pmu->type > PERF_PMU_TYPE_PE_END && + !perf_pmu__is_fake(evsel->pmu); } =20 /** @@ -1916,13 +1917,33 @@ int evsel__enable_cpu(struct evsel *evsel, int cpu_= map_idx) if (evsel__is_non_perf_event_open_pmu(evsel) || evsel__is_non_perf_event_open_pmu(member)) { /* - * In a mixed PMU group, userspace PMUs are not - * grouped in the kernel (opened with group_fd =3D -1) - * and are skipped by the kernel when enabling the - * group leader. We must manually enable them in - * userspace. + * Userspace PMUs are not grouped in the kernel + * (opened with group_fd =3D -1) and may have + * different CPU maps (e.g. duration_time sets + * reads_only_on_cpu_idx0 while user_time has + * all CPUs). Enable matching member CPUs. */ - int mem_err =3D evsel__enable_cpu(member, cpu_map_idx); + int mem_err =3D 0; + + if (evsel->core.reads_only_on_cpu_idx0) { + if (cpu_map_idx =3D=3D 0) { + int nr =3D perf_cpu_map__nr(member->core.cpus); + + for (int m =3D 0; m < nr && !mem_err; m++) + mem_err =3D evsel__enable_cpu(member, m); + } + } else if (member->core.reads_only_on_cpu_idx0) { + if (cpu_map_idx =3D=3D 0 && + perf_cpu_map__nr(member->core.cpus) > 0) + mem_err =3D evsel__enable_cpu(member, 0); + } else { + struct perf_cpu cpu =3D perf_cpu_map__cpu(evsel->core.cpus, + cpu_map_idx); + int mem_idx =3D perf_cpu_map__idx(member->core.cpus, cpu); + + if (mem_idx >=3D 0) + mem_err =3D evsel__enable_cpu(member, mem_idx); + } =20 if (mem_err) return mem_err; @@ -1986,12 +2007,33 @@ int evsel__disable_cpu(struct evsel *evsel, int cpu= _map_idx) if (evsel__is_non_perf_event_open_pmu(evsel) || evsel__is_non_perf_event_open_pmu(member)) { /* - * In a mixed PMU group, userspace PMUs are not - * grouped in the kernel and are skipped by the - * kernel when disabling the group leader. We must - * manually disable them in userspace. + * Userspace PMUs are not grouped in the kernel + * and may have different CPU maps (e.g. + * duration_time sets reads_only_on_cpu_idx0 + * while user_time has all CPUs). Disable + * matching member CPUs. */ - int mem_err =3D evsel__disable_cpu(member, cpu_map_idx); + int mem_err =3D 0; + + if (evsel->core.reads_only_on_cpu_idx0) { + if (cpu_map_idx =3D=3D 0) { + int nr =3D perf_cpu_map__nr(member->core.cpus); + + for (int m =3D 0; m < nr && !mem_err; m++) + mem_err =3D evsel__disable_cpu(member, m); + } + } else if (member->core.reads_only_on_cpu_idx0) { + if (cpu_map_idx =3D=3D 0 && + perf_cpu_map__nr(member->core.cpus) > 0) + mem_err =3D evsel__disable_cpu(member, 0); + } else { + struct perf_cpu cpu =3D perf_cpu_map__cpu(evsel->core.cpus, + cpu_map_idx); + int mem_idx =3D perf_cpu_map__idx(member->core.cpus, cpu); + + if (mem_idx >=3D 0) + mem_err =3D evsel__disable_cpu(member, mem_idx); + } =20 if (mem_err) return mem_err; @@ -2167,7 +2209,8 @@ static bool evsel__group_has_tpebs(struct evsel *lead= er) struct evsel *evsel; =20 for_each_group_evsel(evsel, leader) { - if (evsel__is_retire_lat(evsel)) + if (evsel__is_retire_lat(evsel) || + evsel__is_non_perf_event_open_pmu(evsel)) return true; } return false; @@ -2179,7 +2222,8 @@ static u64 evsel__group_read_nr_members(struct evsel = *leader) struct evsel *evsel; =20 for_each_group_evsel(evsel, leader) { - if (evsel__is_retire_lat(evsel)) + if (evsel__is_retire_lat(evsel) || + evsel__is_non_perf_event_open_pmu(evsel)) nr--; } return nr; @@ -2367,7 +2411,9 @@ static int get_group_fd(struct evsel *evsel, int cpu_= map_idx, int thread) struct evsel *leader =3D evsel__leader(evsel); int fd; =20 - if (!evsel->supported || evsel__is_group_leader(evsel)) + if (!evsel->supported || evsel__is_group_leader(evsel) || + evsel__is_non_perf_event_open_pmu(leader) || + evsel__is_non_perf_event_open_pmu(evsel)) return -1; =20 /* diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index ca13fcc05db0..ba3e5015058c 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -2027,13 +2027,17 @@ static int evsel__compute_group_pmu_name(struct evs= el *evsel, group_pmu_name =3D pmu->name; /* * Software events may be in a group with other uncore PMU events. Use - * the pmu_name of the first non-software event to avoid breaking the - * software event out of the group. + * the pmu_name of the first non-software perf_event_open PMU event to + * avoid breaking the software event out of the group. Userspace PMUs + * (tool, hwmon, drm, bpf) do not open perf_event file descriptors and + * cannot share a kernel group_fd. * * Aux event leaders, like intel_pt, expect a group with events from * other PMUs, so substitute the AUX event's PMU in this case. */ - if (perf_pmu__is_software(pmu) || evsel__is_aux_event(leader)) { + if ((perf_pmu__kind(pmu) =3D=3D PERF_PMU_KIND_PE || + perf_pmu__kind(pmu) =3D=3D PERF_PMU_KIND_FAKE) && + (perf_pmu__is_software(pmu) || evsel__is_aux_event(leader))) { struct perf_pmu *leader_pmu =3D evsel__find_pmu(leader); =20 if (!leader_pmu) { @@ -2042,11 +2046,13 @@ static int evsel__compute_group_pmu_name(struct evs= el *evsel, } /* * Starting with the leader, find the first event with a named - * non-software PMU. for_each_group_(member|evsel) isn't used as - * the list isn't yet sorted putting evsel's in the same group - * together. + * non-software perf_event_open PMU. for_each_group_(member|evsel) + * isn't used as the list isn't yet sorted putting evsel's in + * the same group together. */ - if (leader_pmu && !perf_pmu__is_software(leader_pmu)) { + if (leader_pmu && !perf_pmu__is_software(leader_pmu) && + (perf_pmu__kind(leader_pmu) =3D=3D PERF_PMU_KIND_PE || + perf_pmu__kind(leader_pmu) =3D=3D PERF_PMU_KIND_FAKE)) { group_pmu_name =3D leader_pmu->name; } else if (leader->core.nr_members > 1) { list_for_each_entry(pos, head, core.node) { @@ -2059,7 +2065,9 @@ static int evsel__compute_group_pmu_name(struct evsel= *evsel, /* As with determining pmu above. */ pos_pmu =3D perf_pmus__scan_core(NULL); } - if (pos_pmu && !perf_pmu__is_software(pos_pmu)) { + if (pos_pmu && !perf_pmu__is_software(pos_pmu) && + (perf_pmu__kind(pos_pmu) =3D=3D PERF_PMU_KIND_PE || + perf_pmu__kind(pos_pmu) =3D=3D PERF_PMU_KIND_FAKE)) { group_pmu_name =3D pos_pmu->name; break; } diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 1ae8bcf71c68..ab91dac5d226 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -2461,7 +2461,7 @@ bool perf_pmu__is_software(const struct perf_pmu *pmu) case PERF_TYPE_HW_CACHE: return false; case PERF_TYPE_RAW: return false; case PERF_TYPE_BREAKPOINT: return true; - case PERF_PMU_TYPE_TOOL: return true; + case PERF_PMU_TYPE_TOOL: return false; default: break; } for (size_t i =3D 0; i < ARRAY_SIZE(known_sw_pmus); i++) { diff --git a/tools/perf/util/tool_pmu.c b/tools/perf/util/tool_pmu.c index d2f7591747d9..a62a7cb390ae 100644 --- a/tools/perf/util/tool_pmu.c +++ b/tools/perf/util/tool_pmu.c @@ -272,7 +272,8 @@ int evsel__tool_pmu_open(struct evsel *evsel, if (evsel->core.attr.sample_period) /* no sampling */ return -EINVAL; evsel->duration_time.accumulated_time =3D 0; - if (evsel->core.attr.disabled) { + if (evsel->core.attr.disabled || + evsel__leader(evsel)->core.attr.disabled) { evsel->disabled =3D true; evsel->duration_time.start_time =3D INVALID_START_TIME; } else { @@ -321,7 +322,8 @@ int evsel__tool_pmu_open(struct evsel *evsel, evsel->process_time.accumulated_times, idx, thread); *accumulated_time =3D 0; =20 - if (evsel->core.attr.disabled) { + if (evsel->core.attr.disabled || + evsel__leader(evsel)->core.attr.disabled) { evsel->disabled =3D true; *start_time =3D INVALID_START_TIME; } else { --=20 2.56.1.750.gf70dc682cf-goog From nobody Mon Nov 23 10:35:26 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.72 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795418396; b=Mch0CMN1GfoJgRpF01mNLydb6KL8/WhUJ0hooU5r2Tfy0VXRIpbIcBgjcVBpujXgHRe2 Z9hUag8UQfuOCKE30HfxjvyNEuhpXwTBycWCQ9M3m8MlAEeDiSNK9a7N96WHPk6/1b2Lb 63ybGiAUkP673cwI5KbYSxBPKGjw8kbRXRh49s3dZMYsN9Sdo5obIatwJj+ZUVDj3xE4E AoWQPPv3BGttdUrXDsGGM+0Iy7Qrjp3pcWu9VW0+LjjNWWGqdFJ7I3wHwbcLMRKjuH3KF SIZbJUmBtn1bWsUuSbOuVwaCr+KrVdIBPDknfszgUknxH9ASJ5yGz93ASyVxiJvlT5w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795418396; 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=yR0Cn8qSGWXI+eqarTOysMEcgExUMTEHpNE5KEC2kI4=; b=AY9yNfX50RwN/jOWVoiKC0DqQmNwV0m7BlQ7YiO8xahNNV1724RgHLvnvSOHH4DaeohS 5af8xAkl8d5rumh5KR3FL5U9wyL2tFnm+hRp8vsjV2/cZ38qZvwZsB+Z2Gq0xwswyPTAI AFs368t/Waw4GC5RWk7l/YnJ8zAmRHthPGWEuI5GRAVqeAAFE0KTPaYI6buE3dUFZSFHh zPRb1JMqROBjLBtpldUSBRRvLunsnQV5BudiFJ3kzjl/0GyU13FKIjmHGX2Lyq8DxI8I5 TVtCwojsHdXyddSzSYOKC1vZbVUsR5iko5fhmpGHvjUD7kNLlF2rVCoq1rbphZ1ofdQ== 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=XALQBGpL; arc=none smtp.remote-ip=209.85.216.72 Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) (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 69C90606A31F for ; Mon, 23 Nov 2026 07:19:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 69C90606A31F 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=XALQBGpL Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-3b18165dc53so3479681a91.3 for ; Sun, 22 Nov 2026 23:19:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795418396; x=1796023196; 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=yR0Cn8qSGWXI+eqarTOysMEcgExUMTEHpNE5KEC2kI4=; b=XALQBGpLfgHGrv5dwy+uCQQL0sTFiCl4xb1v3MmAaZOtSTi+AryB++T8bxnNAwB2Kr S2MBp0iIZYgn9FKPso1fI43/NpKonT0LSk7uHuMYFFpDEsfkAYCsFiiwQfVf6dvgKpJB qYuNqoErB4ivhia+n4dP33gj1OW7e9XubZGZAx82dkJBQ3PIC48POTppAOOAhCj4JbNC kTYSdiv93PnailRsrUk4q2OY7T6srWltSYRXzig0GcZnwAOQAbDnVJ3blIOGu4lExDCu ySi/DVz/lAqxu7QNLPUi+Ut6YAfH2tJKpegwo0wlxsl7Be50E4td33nHBNtEHzHvCVhe 6whQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795418396; x=1796023196; 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=yR0Cn8qSGWXI+eqarTOysMEcgExUMTEHpNE5KEC2kI4=; b=dLRH9XUvMGEEJ+TlVR5DJOcifC9MATeBWxD+2dTf5Kq6F0BI0upp2bPTBIaQ94Bwgr lXZz+gdoL5kSYQ/V1kJF2AVXCQrXRD74QyAP4xaQP0KaxrEF4fZ1h3B+sLmaD23ycc8e PVwZsNgzlsQNAuo9oJSMNedt/P6GYxzNmPag3TdwrgPDB2fGioiPNFroP1bF+iGWtpzj P9BuOoeGBCWMQGMYZZx+LwVtvaJORwpf3RF+FcdPjPgzFL4/g1Vlu/bpiBVGL26EyJZq be4ihzglIgyCIZxYc1Jr/P+dv8MeU4B3356TDiCpy1VIgj+xMjLfsHV1ePLM1sPk6Q3O J91w== X-Forwarded-Encrypted: i=1; AL+hErH6TFL0UoNu1RsU4RSnbVchN7FH1C4G6KPOVF9U7hVlh+qh0JXZYuu2OMpQYHixxoOt2IxSfAJZU/ZZ850=@vger.kernel.org X-Gm-Message-State: APuU4Z9CjUsziCSHueCyKtJjrQg0fTL5nIii9Z+atJTtfo+R/VaKENwW EPXlK9EXhUCaIOay9KF6J0R206g4t8wwRl0mMiISexKH9pB6LfAfqDe89WLWzl04cQtuWGUpMzi 62+O6fTL3Xg== X-Received: from pgig3.prod.google.com ([2002:a63:f403:0:b0:cc7:ad09:94b6]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3d8d:b0:3b3:b704:118c with SMTP id 98e67ed59e1d1-3b41cb4f321mr9578228a91.65.1795418395412; Sun, 22 Nov 2026 23:19:55 -0800 (PST) Date: Sun, 22 Nov 2026 23:19:07 -0800 In-Reply-To: <20261123071913.653315-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: <20261123071913.653315-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261123071913.653315-8-irogers@google.com> Subject: [PATCH v1 07/13] perf evlist: Enable tool PMU events when starting a workload From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark Cc: Howard Chu , Thomas Falcon , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When perf stat runs a workload, group leaders are opened disabled with enable_on_exec set so that the kernel enables them when the workload execs. Since tool PMU events were made to respect enable/disable they are opened disabled too, but there is no kernel event to be enabled on exec, so they are never enabled and count 0. For example: $ perf stat -e duration_time,user_time,task-clock -- perf test -w noploop 0 duration_time 0 user_time 1,013.74 msec task-clock Enable tool PMU events that are set to enable on exec when the workload is started. Fixes: 0b18bced5444 ("perf tool_pmu: Make tool PMU events respect enable/di= sable") Signed-off-by: Ian Rogers --- tools/perf/util/evlist.c | 15 +++++++++++++++ tools/perf/util/evsel.c | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index c2402e4791b6..ec26064183b6 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -69,6 +69,7 @@ #include "string2.h" #include "target.h" #include "thread_map.h" +#include "tool_pmu.h" #include "units.h" #include "util.h" =20 @@ -1909,6 +1910,20 @@ int evlist__start_workload(struct evlist *evlist) if (evlist__workload_cork_fd(evlist) >=3D 0) { char bf =3D 0; int ret; + struct evsel *pos; + + /* + * Tool PMU events aren't opened with perf_event_open and so + * aren't enabled by the kernel on exec. Enable them as the + * workload is started. + */ + evlist__for_each_entry(evlist, pos) { + if (evsel__is_tool(pos) && + (pos->core.attr.enable_on_exec || + evsel__leader(pos)->core.attr.enable_on_exec) && + pos->core.fd && pos->supported) + evsel__enable(pos); + } /* * Remove the cork, let it rip! */ diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index babeccfbecb5..d89087e295dc 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1914,6 +1914,8 @@ int evsel__enable_cpu(struct evsel *evsel, int cpu_ma= p_idx) struct evsel *member; =20 for_each_group_member(member, evsel) { + if (!member->core.fd) + continue; if (evsel__is_non_perf_event_open_pmu(evsel) || evsel__is_non_perf_event_open_pmu(member)) { /* @@ -1969,6 +1971,11 @@ int evsel__enable(struct evsel *evsel) struct evsel *member; =20 for_each_group_member(member, evsel) { + if (!member->core.fd) { + if (evsel__is_retire_lat(member)) + member->disabled =3D false; + continue; + } if (evsel__is_non_perf_event_open_pmu(evsel) || evsel__is_non_perf_event_open_pmu(member)) { /* @@ -2004,6 +2011,8 @@ int evsel__disable_cpu(struct evsel *evsel, int cpu_m= ap_idx) struct evsel *member; =20 for_each_group_member(member, evsel) { + if (!member->core.fd) + continue; if (evsel__is_non_perf_event_open_pmu(evsel) || evsel__is_non_perf_event_open_pmu(member)) { /* @@ -2065,6 +2074,11 @@ int evsel__disable(struct evsel *evsel) struct evsel *member; =20 for_each_group_member(member, evsel) { + if (!member->core.fd) { + if (evsel__is_retire_lat(member)) + member->disabled =3D true; + continue; + } if (evsel__is_non_perf_event_open_pmu(evsel) || evsel__is_non_perf_event_open_pmu(member)) { /* --=20 2.56.1.750.gf70dc682cf-goog From nobody Mon Nov 23 10:35:26 2026 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=1795418401; b=kyAGOVD82pBA7LIvLTmdF+mQbUZvuGu+0mIXbtwzKZheNGajoUsjNdDIPoLRe6tcLtBX 9H1YuRJTvPdZuaGsGoP3RcTyKNhso1v9mOaWdMqp2dqwze+6XREseHqBneIV2YC4aWaa4 Kfjxcg4D1yySyv/nhO+X064eK9dQ9/J6R6nkj91pkqAHmmOeUciTTbkQf1N/hbqz23hNt i8JhCxe1qzpm2NGi2r8DbOH7bi9TGYsyYulr+u73JJiZnQhOLoaI+nXMqjx6eJXG+T4Is z2xeMDT9UPfvli5i5x2n1Apr8EGUTonyYl1WeNcNrqAo4N7vnSJtmWzZu4P4LR/v9/g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795418401; 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=fiMjKbfzQ9qS5le+IrJC6UoOpWWUzBoBnXeh+seDCek=; b=F/Ts69aKtNdt2Wx6kyKTufMYSDenSf49KRdN9DzzGNA48kWjNpG51G9wnynXeJppWMuN YRwtrSaKCkSp34bduk4gUu/mipaH7mAJ8tYHeHea2E7ZAj1F9LB5zFA+Sr9iProZG6QBa iBGkdcsmpF0+LnjR0U2WOSRCKej4QGZix1Z2Nuzcbc/ZTU9w0w+0l/T1+vSeDHBrDEl+r hqdGlU8aP/5BJN8xNjxenyBXnqT/RJlBz95f95TxLcIuJTKXG7+QVN77nEHoAvmsnZZmZ scDn+S3SO4tprlEva8f3k8Y8OVUiU4hBVeaP+7RtZqdiPhXy4pzPxwPuNeA4qAvpRRA== 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=T5ESDTFA; 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 6D967606A31E for ; Mon, 23 Nov 2026 07:20:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6D967606A31E 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=T5ESDTFA Received: by mail-dy1-f198.google.com with SMTP id 5a478bee46e88-38355b03cb8so3049852eec.0 for ; Sun, 22 Nov 2026 23:20:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795418401; x=1796023201; 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=fiMjKbfzQ9qS5le+IrJC6UoOpWWUzBoBnXeh+seDCek=; b=T5ESDTFAUPVBTaI0SuUlG86npW9tk1aMdE/rNC/px4tlr00cR1+96RbJf6ySz4IzwB lzvFzFBCnyOBDWtQ16TV7d9INkPj4jF6SobVSAyL3FwL53+cGPC8SI6V7Om/D3Rh/Jkl xE1mRrG7FBvIGngdt0wcF2En8fnLuj1poas1oA5ucgDnlkz9rYQe1YGFetdM5Cxt77Zt 8FPnv3A2XdIerwxAUGvRRPkiOOd8Fzni08LktOQ17tt4zYunLQV+H6fVGkFNuqcNRI/r WtT4P5RS/AzY6fN3KDUu9Xe6AfqLytZCvdpbJuvuUh1tQ+OADG3VYpkN07DH9Qfjs8ci AEog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795418401; x=1796023201; 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=fiMjKbfzQ9qS5le+IrJC6UoOpWWUzBoBnXeh+seDCek=; b=aJSUdhn8ehI4K77Ml7+03XHHr5PdC5hprKK9NQ7+HZ9zKjTge5DqQeXD3LYZGpDCin h0ha7tcxYPpnbyXN1SjciMZW42HyOvktdqphezOTiatnEE0GlNQ+69AjRnWTHvMdxkGo jQIhm1WpoPvwMJijUL2XwJFULq6oaZJ/KIbFZ48VbDU6/r0AYx3mZEao8OaNo5pg2IRa bEkrXLiC75TCJwmEr9vMFmeDRrLVTsDo9+f9uH+7dXTrWScdhUIKdCM0rXVyK2tr5tzM rkD0abBIVONTgaM/wkjihAkg4u+rWHIOzhiohdUM+6qnxjTS6nxc9Hhr087NGlBiEi9n VSVA== X-Forwarded-Encrypted: i=1; AL+hErEao8BCito1lrmYVGzuYkIQ47BF66+rllbYwuuMHKPHGfs7XtFr9v/ndAHNULNvGhaGZxoR5vkrEzlyk5M=@vger.kernel.org X-Gm-Message-State: APuU4Z+JzUsAZP3FanaY8SMVIRyY7m9KHHhlMe3un1go1yd2a7ep9obS M3cVrOT7iKkwsfjQ7UPlTNVM/0ddhmsbPHEsQuZdjauVmCKcCCDrjyviVR6EVbAcZeYhXw3R5cF NX8VqwCpcIw== X-Received: from dybmf40.prod.google.com ([2002:a05:7301:928:b0:36c:9b55:8436]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:51f6:b0:384:7b4c:8564 with SMTP id 5a478bee46e88-3857131a749mr12658929eec.41.1795418400197; Sun, 22 Nov 2026 23:20:00 -0800 (PST) Date: Sun, 22 Nov 2026 23:19:08 -0800 In-Reply-To: <20261123071913.653315-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: <20261123071913.653315-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261123071913.653315-9-irogers@google.com> Subject: [PATCH v1 08/13] perf stat: Respect the enabled time of exited user_time/system_time events From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark Cc: Howard Chu , Thomas Falcon , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When a workload has exited and been reaped by wait4, user_time and system_time can no longer be read from /proc//stat and perf stat falls back to using the rusage values from wait4. These are the totals for the whole run of the workload and ignore when the event was enabled, such as after a delay. For example: $ perf stat -D 500 -e duration_time,user_time,task-clock -- perf test -w = noploop 524,466,208 duration_time 1,004,578,000 user_time 522.86 msec task-clock Add evsel__tool_pmu_exited_process_time() to compute the time the event was enabled for from the process's total time, the start time when it was last enabled and the accumulated time, and use it for the rusage fallback. Fixes: 0b18bced5444 ("perf tool_pmu: Make tool PMU events respect enable/di= sable") Signed-off-by: Ian Rogers --- tools/perf/builtin-stat.c | 3 +++ tools/perf/util/tool_pmu.c | 24 ++++++++++++++++++++++++ tools/perf/util/tool_pmu.h | 2 ++ 3 files changed, 29 insertions(+) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 4fe4da20aae8..964bf0864796 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -302,6 +302,9 @@ static int read_single_counter(struct evsel *counter, i= nt cpu_map_idx, int threa else val =3D ru_stats.ru_stime_usec_stat.mean; =20 + /* Only count the time the event was enabled, such as after a delay. */ + val =3D evsel__tool_pmu_exited_process_time(counter, cpu_map_idx, thread= , val); + count->val =3D val; if (old_count) { count->run =3D old_count->run + 1; diff --git a/tools/perf/util/tool_pmu.c b/tools/perf/util/tool_pmu.c index a62a7cb390ae..24ff27099d32 100644 --- a/tools/perf/util/tool_pmu.c +++ b/tools/perf/util/tool_pmu.c @@ -13,9 +13,11 @@ #include #include #include +#include #include #include #include +#include =20 #define INVALID_START_TIME ~0ULL =20 @@ -709,6 +711,28 @@ int evsel__tool_pmu_read(struct evsel *evsel, int cpu_= map_idx, int thread) return 0; } =20 +/* + * When the process has exited its user or system time can no longer be re= ad + * from /proc. Given the process's total time, such as from wait4, compute= the + * time the event was enabled for. + */ +u64 evsel__tool_pmu_exited_process_time(struct evsel *evsel, int cpu_map_i= dx, int thread, + u64 total_ns) +{ + u64 ns_per_tick =3D 1000000000ULL / sysconf(_SC_CLK_TCK); + __u64 start_time =3D *(__u64 *)xyarray__entry(evsel->process_time.start_t= imes, + cpu_map_idx, thread); + __u64 accumulated =3D *(__u64 *)xyarray__entry(evsel->process_time.accumu= lated_times, + cpu_map_idx, thread); + u64 val =3D accumulated * ns_per_tick; + + if (!evsel->disabled && start_time !=3D INVALID_START_TIME && + total_ns >=3D start_time * ns_per_tick) + val +=3D total_ns - start_time * ns_per_tick; + + return val; +} + struct perf_pmu *tool_pmu__new(void) { struct perf_pmu *tool =3D zalloc(sizeof(struct perf_pmu)); diff --git a/tools/perf/util/tool_pmu.h b/tools/perf/util/tool_pmu.h index f66d24cf3502..137b0a06a852 100644 --- a/tools/perf/util/tool_pmu.h +++ b/tools/perf/util/tool_pmu.h @@ -61,6 +61,8 @@ int evsel__tool_pmu_enable(struct evsel *evsel); int evsel__tool_pmu_disable_cpu(struct evsel *evsel, int cpu_map_idx); int evsel__tool_pmu_disable(struct evsel *evsel); int evsel__tool_pmu_read(struct evsel *evsel, int cpu_map_idx, int thread); +u64 evsel__tool_pmu_exited_process_time(struct evsel *evsel, int cpu_map_i= dx, int thread, + u64 total_ns); =20 struct perf_pmu *tool_pmu__new(void); =20 --=20 2.56.1.750.gf70dc682cf-goog From nobody Mon Nov 23 10:35:26 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795418409; b=gGken6qnIbyad+KTS08AB45dAtVXRCinaG4bDci7Qb4Mqjl8XoOmcM0vVuazTZFvVbAT gTBhW32TLcCKCcg6LnpSc59DMyWVlzu3ZMUsrR/UuVTFDHKS9OHM/uSQJNceryMgrMjPO yYkdtmz9/RwI7r1cz/Sh3WIlzL37y1wwFCwkKf60AhNBIVfUd/P1x157qQhqUmyQpq4Oc 4CkmMHvuG7WCrNfxuiYhGmb92h5zUj67XVBfV8KWZDiYl/ex22g8MNZvZqklz0AMZwqzx KECOGqSwa13QV9v865wM2y6uY2azROd2WH2DEHILSphennqpfzqqVfDjdPsAyeWxLMA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795418409; 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=+yMHdu8ZU+gDZ3LcDiJBOlO1BMwKVmt8Xr9NpwRp3Vs=; b=MxXu/4A7dV5RG5vIvBJvN8m4LGFWoIG9mrBxYwa9bnw0m608Mv0lLoRWY7uHAK/ZsLkZ K6sBJxy/Jqy57XJUT1bxYvaDRLehS0Wo7IlJt++BEbcU9qEiN/2aL2JIdZZQx1KS6nsoP hpyHsVT52z+FrCSorneNme1Z5m3xQQ15ZiCN4xjTMZJhCWwFFJq+9BTZ+NLuZmj3lm29M SHb6dFve7DqdZ3BvweOjRzxUR+1MpbR83LB+RzY7sMsmPkNZ328Jpvx5rIrRj7wXdFNCr njgfBHIZK83Jin+P2sc04/RPxq646NbZF5BFf9Cuk97BFqbdhPYo9B5v/ijDqxfzSaQ== 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=Vq9Lu+7f; arc=none smtp.remote-ip=74.125.82.200 Received: from mail-dy1-f200.google.com (mail-dy1-f200.google.com [74.125.82.200]) (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 D2D21606A31A for ; Mon, 23 Nov 2026 07:20:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D2D21606A31A 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=Vq9Lu+7f Received: by mail-dy1-f200.google.com with SMTP id 5a478bee46e88-33c35f5ca6cso4175326eec.1 for ; Sun, 22 Nov 2026 23:20:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795418408; x=1796023208; 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=+yMHdu8ZU+gDZ3LcDiJBOlO1BMwKVmt8Xr9NpwRp3Vs=; b=Vq9Lu+7fvozy2VhGuSDZEVOy5UsNgh2apKmMsn+FIeVkZ50cFP/hBSaXQnOqZzH+kQ ak2YaJEbP8LzPhwLDNG5UQ9td4jrxlRST39KmqXXd9hX822tqzj3Qra22skZW2V9PmC/ P+cnNNrey4jBe5cE8WZzoCoBy9toYe2jB7pX7lNi8weT1T2SGUBOZQP6w3LIPW3ttr5f +a64q3UJGx/54I8/4wsSZ0zuCT0SllEDLW2ts3UzITnn/xgE/fumjDtrfdE0cK2Gkx4G +/g/zQfXrF0UBfp1PMUd8HTkJhwVnkzJMYOoIw5NniAMSirrReQFqTjZp65kwwHQl0HP BEyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795418408; x=1796023208; 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=+yMHdu8ZU+gDZ3LcDiJBOlO1BMwKVmt8Xr9NpwRp3Vs=; b=A7T77mrffpuh/1+OKsh79oEnNmQbV6V0+XS18d7OJwLWZD8PKOcCC3/AxuGWsxpY5x DmaNdb7rklvjEAl9Y7BgVhcCixXi1xKzRjkbzHqB2PKLCaOHT+cdg2LrB52mzhFIrZj/ xZkQwXi4G5qYxYKbMGIPF9BS6W7CEPuWYRTdtBaJb/5ECYSMRJkPC4GpYc6Xzy6c+TiJ 3pK+oSv8uMeW7ovtoj6Fr66RVK0JeDRFH1peHcm3iBjuwmV3M/Mb8Lcpievbx9p9FwG7 8BrPMG3KqyBT7ZV/xsbyNS1Bl4AOzkAGylpOhNtQRH019tc2dvfsusH3y+mtRlVbxoOr dGSg== X-Forwarded-Encrypted: i=1; AL+hErHK10YOdYWYVlHpWMtjNOLB2s/WFLSIowEnyKWmMAhvP/7WctA3f0svwGca7oLF+CZ6oPfbyGrdHYH5eSc=@vger.kernel.org X-Gm-Message-State: APuU4Z9h89an8BdI51iGZcnwcZZJQo3jPfjDvDpQfRzYZTObQHRnnsZw 3vM8NZdju9hDJVetc3TaTlGKzdS7PLw0eVnmjIpJvw43/GgJ5WmD1PhwYitbVgK7UNNdT5BByBo G+6YeVLF1Tw== X-Received: from dyff26.prod.google.com ([2002:a05:693c:805a:b0:386:9be5:2d95]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:6e9a:b0:383:71a:2b14 with SMTP id 5a478bee46e88-3856f14bb69mr10306411eec.10.1795418407468; Sun, 22 Nov 2026 23:20:07 -0800 (PST) Date: Sun, 22 Nov 2026 23:19:09 -0800 In-Reply-To: <20261123071913.653315-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: <20261123071913.653315-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261123071913.653315-10-irogers@google.com> Subject: [PATCH v1 09/13] perf stat: Fix --bpf-counters with non-bperf, hybrid, and mixed events From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark Cc: Howard Chu , Thomas Falcon , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When `perf stat` runs with `--bpf-counters` (`target.use_bpf`), the `:b` event modifier (`evsel->bpf_counter`), or `stat.bpf-counter-events`, `bpf_counter__load()` unconditionally assigns `bperf_ops` (or `bperf_cgrp_ops` for `--for-each-cgroup`) and `__run_perf_stat()` skips `create_perf_stat_counter()` whenever `target.use_bpf` is set. This causes several failures when events that cannot be read through bperf's shared per-CPU `PERF_EVENT_ARRAY` are present: 1. Non-`perf_event_open` PMU events (`tool` events such as `duration_time` and `user_time`, `bpf`, `hwmon`, and `drm` PMUs), uncore PMUs, and retirement latency (`:R`) events have no CPU-local `perf_event` file descriptor for `bpf_perf_event_read_value()`, so bperf either reads 0, leaves `tool` state uninitialized (reading system uptime for `duration_time` on secondary cgroups or dereferencing NULL `process_time` xyarrays), or skips opening them. 2. Events with tracepoint, address, or BPF filters (`evsel->filter` or `evsel->bpf_filters`) and `-G` cgroup events share bperf's global pinned `/sys/fs/bpf/perf_attr_map` keyed solely on `struct perf_event_attr`, ignoring filters/cgroups or skipping `perf_bpf_filter__prepare()` in `evlist__apply_filters()`. 3. Grouped events under `--bpf-counters` (such as `Default` topdown metric groups) fail `bperf_check_target()` instead of falling back to regular grouped counters. 4. On hybrid systems, `-C ` can leave one PMU's `evsel->core.cpus` empty (hitting `assert()` in `bperf_reload_leader_program()` or dereferencing NULL `evsel->core.fd` in `store_evsel_ids()`) or give different hybrid evsels different CPU counts (clobbering the file-static `filter_entry_cnt` in `bpf_counter.c`). 5. In mixed sessions (`!all_counters_use_bpf`), `__evlist__disable()` and `__evlist__enable()` run `PERF_EVENT_IOC_DISABLE` / `ENABLE` on `evsel->core.fd` of the session that loaded `bperf_leader`, disabling the globally shared hardware PMCs for concurrent and repeat (`-r`) runs. Repeat runs also leaked `bperf_follower` `fexit` links and leader FDs, and left `bperf_cgrp` `cgrp_readings` accumulating across runs. Fix these issues by: - Adding `bpf_counter__can_use_bperf()` and `bpf_counter__can_use_bperf_cgrp()` to restrict `bperf` to compatible counters and fall back cleanly otherwise, rejecting explicit `:b` on grouped events before `evlist__create_maps()` splits mismatched group CPU maps, and leaving `tool` PMU events out of `bperf_cgrp_ops` so they open, toggle (`-D`), and reset (`-r`) through the regular tool PMU paths. - Gating the `create_perf_stat_counter()` loop on `!all_counters_use_bpf` instead of `!target.use_bpf`, resetting `all_counters_use_bpf` on each run, and only setting `requires_cpu` on counters that will use bperf. - Skipping `evsel__is_bperf()` counters in `evlist__is_enabled()`, `__evlist__disable()`, and `__evlist__enable()`, and guarding NULL `evsel->core.fd` in `evlist__apply_filters()` and `store_evsel_ids()`. - Reading per-evsel `accum_readings` map max entries instead of a global `filter_entry_cnt`, clearing `evsel->leader_skel` before freeing it, storing `skel->links.fexit_XXX`, closing `cgrp_switch` on teardown, and resetting follower / `bperf_cgrp` / `bpf_prog_profiler` state on repeat loads. Fixes: 7fac83aaf2ee ("perf stat: Introduce 'bperf' to share hardware PMCs w= ith BPF") Signed-off-by: Ian Rogers --- tools/perf/builtin-stat.c | 27 ++++- tools/perf/util/bpf_counter.c | 115 ++++++++++++++++----- tools/perf/util/bpf_counter.h | 14 +++ tools/perf/util/bpf_counter_cgroup.c | 145 ++++++++++++++++++++------- tools/perf/util/evlist.c | 17 +++- tools/perf/util/evsel.c | 8 +- 6 files changed, 254 insertions(+), 72 deletions(-) diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c index 964bf0864796..267808e23e69 100644 --- a/tools/perf/builtin-stat.c +++ b/tools/perf/builtin-stat.c @@ -802,6 +802,8 @@ static int __run_perf_stat(int argc, const char **argv,= int run_idx) child_pid =3D evlist__workload_pid(evsel_list); } =20 + all_counters_use_bpf =3D true; + evlist__for_each_entry(evsel_list, counter) { counter->reset_group =3D false; if (bpf_counter__load(counter, &target)) { @@ -818,7 +820,7 @@ static int __run_perf_stat(int argc, const char **argv,= int run_idx) * bperf calls evsel__open_per_cpu() in bperf__load(), so * no need to call it again here. */ - if (!target.use_bpf) { + if (!all_counters_use_bpf) { evlist__for_each_cpu(evlist_cpu_itr, evsel_list) { counter =3D evlist_cpu_itr.evsel; =20 @@ -2908,6 +2910,15 @@ int cmd_stat(int argc, const char **argv) goto out; } =20 + if (target.use_bpf) { + evlist__for_each_entry(evsel_list, counter) { + if (!bpf_counter__can_use_bperf_cgrp(counter)) { + target.use_bpf =3D false; + break; + } + } + } + if (evlist__expand_cgroup(evsel_list, stat_config.cgroup_list, !target.use_bpf) < 0) { parse_options_usage(stat_usage, stat_options, @@ -2927,6 +2938,18 @@ int cmd_stat(int argc, const char **argv) evlist__warn_user_requested_cpus(evsel_list, target.cpu_list); =20 evlist__for_each_entry(evsel_list, counter) { + bool needs_bperf_cpu =3D + (counter->bpf_counter || target.use_bpf || + evsel__match_bpf_counter_events(evsel__name(counter))) && + (stat_config.cgroup_list ? + (target.use_bpf && !evsel__is_non_perf_event_open_pmu(counter)) : + bpf_counter__can_use_bperf(counter)); + + if (counter->bpf_counter && counter->core.leader->nr_members > 1) { + pr_err("bpf managed perf events do not yet support groups.\n"); + goto out; + } + /* * Setup BPF counters to require CPUs as any(-1) isn't * supported. evlist__create_maps below will propagate this @@ -2934,7 +2957,7 @@ int cmd_stat(int argc, const char **argv) * set up, and this change must happen early, so directly use * the bpf_counter variable and target information. */ - if ((counter->bpf_counter || target.use_bpf) && !target__has_cpu(&target= )) + if (needs_bperf_cpu && !target__has_cpu(&target)) counter->core.requires_cpu =3D true; } =20 diff --git a/tools/perf/util/bpf_counter.c b/tools/perf/util/bpf_counter.c index 9362e45e17ce..d2ab19ec4366 100644 --- a/tools/perf/util/bpf_counter.c +++ b/tools/perf/util/bpf_counter.c @@ -4,6 +4,7 @@ =20 #include #include +#include #include #include #include @@ -22,7 +23,9 @@ #include "debug.h" #include "evsel.h" #include "evlist.h" +#include "pmu.h" #include "target.h" +#include "tool_pmu.h" #include "cgroup.h" #include "cpumap.h" #include "thread_map.h" @@ -230,6 +233,9 @@ static int bpf_program_profiler__load(struct evsel *evs= el, struct target *target u32 prog_id; int ret; =20 + if (!list_empty(&evsel->bpf_counter_list)) + bpf_program_profiler__destroy(evsel); + bpf_str_ =3D bpf_str =3D strdup(target->bpf_str); if (!bpf_str) return -1; @@ -240,6 +246,7 @@ static int bpf_program_profiler__load(struct evsel *evs= el, struct target *target (*p !=3D '\0' && *p !=3D ',')) { pr_err("Failed to parse bpf prog ids %s\n", target->bpf_str); + bpf_program_profiler__destroy(evsel); free(bpf_str_); return -1; } @@ -454,8 +461,6 @@ static int bperf_check_target(struct evsel *evsel, return 0; } =20 -static __u32 filter_entry_cnt; - static int bperf_reload_leader_program(struct evsel *evsel, int attr_map_f= d, struct perf_event_attr_map_entry *entry) { @@ -505,6 +510,7 @@ static int bperf_reload_leader_program(struct evsel *ev= sel, int attr_map_fd, perf_thread_map__put(threads); =20 out: + evsel->leader_skel =3D NULL; bperf_leader_bpf__destroy(skel); bpf_link__destroy(link); return err; @@ -523,19 +529,38 @@ static int bperf_attach_follower_program(struct bperf= _follower_bpf *skel, err =3D bperf_follower_bpf__attach(skel); else { link =3D bpf_program__attach(skel->progs.fexit_XXX); - if (IS_ERR(link)) - err =3D PTR_ERR(link); + err =3D libbpf_get_error(link); + if (!err) + skel->links.fexit_XXX =3D link; } =20 return err; } =20 +static int bperf__destroy(struct evsel *evsel) +{ + bperf_follower_bpf__destroy(evsel->follower_skel); + evsel->follower_skel =3D NULL; + if (evsel->bperf_leader_prog_fd >=3D 0) { + close(evsel->bperf_leader_prog_fd); + evsel->bperf_leader_prog_fd =3D -1; + } + if (evsel->bperf_leader_link_fd >=3D 0) { + close(evsel->bperf_leader_link_fd); + evsel->bperf_leader_link_fd =3D -1; + } + return 0; +} + static int bperf__load(struct evsel *evsel, struct target *target) { struct perf_event_attr_map_entry entry =3D {0xffffffff, 0xffffffff}; int attr_map_fd, diff_map_fd =3D -1, err; enum bperf_filter_type filter_type; - __u32 i; + __u32 filter_entry_cnt, i; + + if (evsel->follower_skel) + bperf__destroy(evsel); =20 if (bperf_check_target(evsel, target, &filter_type, &filter_entry_cnt)) return -1; @@ -641,10 +666,18 @@ static int bperf__load(struct evsel *evsel, struct ta= rget *target) target->inherit); =20 out: - if (err && evsel->bperf_leader_link_fd >=3D 0) - close(evsel->bperf_leader_link_fd); - if (err && evsel->bperf_leader_prog_fd >=3D 0) - close(evsel->bperf_leader_prog_fd); + if (err) { + bperf_follower_bpf__destroy(evsel->follower_skel); + evsel->follower_skel =3D NULL; + if (evsel->bperf_leader_link_fd >=3D 0) { + close(evsel->bperf_leader_link_fd); + evsel->bperf_leader_link_fd =3D -1; + } + if (evsel->bperf_leader_prog_fd >=3D 0) { + close(evsel->bperf_leader_prog_fd); + evsel->bperf_leader_prog_fd =3D -1; + } + } if (diff_map_fd >=3D 0) close(diff_map_fd); =20 @@ -697,11 +730,13 @@ static int bperf__read(struct evsel *evsel) struct bpf_perf_event_value values[num_cpu_bpf]; struct perf_counts_values *counts; int reading_map_fd, err =3D 0; + __u32 entry_cnt; =20 bperf_sync_counters(evsel); reading_map_fd =3D bpf_map__fd(skel->maps.accum_readings); + entry_cnt =3D bpf_map__max_entries(skel->maps.accum_readings); =20 - for (__u32 i =3D 0; i < filter_entry_cnt; i++) { + for (__u32 i =3D 0; i < entry_cnt; i++) { struct perf_cpu entry; __u32 cpu; =20 @@ -750,14 +785,6 @@ static int bperf__read(struct evsel *evsel) return err; } =20 -static int bperf__destroy(struct evsel *evsel) -{ - bperf_follower_bpf__destroy(evsel->follower_skel); - close(evsel->bperf_leader_prog_fd); - close(evsel->bperf_leader_link_fd); - return 0; -} - /* * bperf: share hardware PMCs with BPF * @@ -856,18 +883,56 @@ int bpf_counter__install_pe(struct evsel *evsel, int = cpu_map_idx, int fd) return evsel->bpf_counter_ops->install_pe(evsel, cpu_map_idx, fd); } =20 +bool bpf_counter__can_use_bperf(const struct evsel *evsel) +{ + return !evsel__is_non_perf_event_open_pmu(evsel) && + !evsel__is_retire_lat(evsel) && + !evsel->core.is_pmu_uncore && + !(evsel->pmu && evsel->pmu->is_uncore) && + !evsel->cgrp && + !evsel->filter && + list_empty(&evsel->bpf_filters) && + evsel->core.leader->nr_members <=3D 1; +} + +bool bpf_counter__can_use_bperf_cgrp(const struct evsel *evsel) +{ + if (evsel__is_tool(evsel)) { + enum tool_pmu_event ev =3D evsel__tool_event(evsel); + + return ev !=3D TOOL_PMU__EVENT_USER_TIME && + ev !=3D TOOL_PMU__EVENT_SYSTEM_TIME; + } + + return !evsel__is_non_perf_event_open_pmu(evsel) && + !evsel__is_retire_lat(evsel) && + !evsel->core.is_pmu_uncore && + !(evsel->pmu && evsel->pmu->is_uncore) && + !evsel->filter && + list_empty(&evsel->bpf_filters); +} + int bpf_counter__load(struct evsel *evsel, struct target *target) { - if (target->bpf_str) + if (target->bpf_str) { evsel->bpf_counter_ops =3D &bpf_program_profiler_ops; - else if (cgrp_event_expanded && target->use_bpf) - evsel->bpf_counter_ops =3D &bperf_cgrp_ops; - else if (target->use_bpf || evsel->bpf_counter || - evsel__match_bpf_counter_events(evsel->name)) + } else if (cgrp_event_expanded && target->use_bpf) { + if (!evsel__is_tool(evsel)) + evsel->bpf_counter_ops =3D &bperf_cgrp_ops; + } else if ((target->use_bpf || evsel->bpf_counter || + evsel__match_bpf_counter_events(evsel__name(evsel))) && + bpf_counter__can_use_bperf(evsel) && + !perf_cpu_map__has_any_cpu_or_is_empty(evsel->core.cpus)) { evsel->bpf_counter_ops =3D &bperf_ops; + } + + if (evsel->bpf_counter_ops) { + int err =3D evsel->bpf_counter_ops->load(evsel, target); =20 - if (evsel->bpf_counter_ops) - return evsel->bpf_counter_ops->load(evsel, target); + if (err) + bpf_counter__destroy(evsel); + return err; + } return 0; } =20 diff --git a/tools/perf/util/bpf_counter.h b/tools/perf/util/bpf_counter.h index 658d8e7d507e..08056254264e 100644 --- a/tools/perf/util/bpf_counter.h +++ b/tools/perf/util/bpf_counter.h @@ -2,6 +2,8 @@ #ifndef __PERF_BPF_COUNTER_H #define __PERF_BPF_COUNTER_H 1 =20 +#include + struct evsel; struct target; =20 @@ -30,6 +32,8 @@ int bpf_counter__disable(struct evsel *evsel); int bpf_counter__read(struct evsel *evsel); void bpf_counter__destroy(struct evsel *evsel); int bpf_counter__install_pe(struct evsel *evsel, int cpu_map_idx, int fd); +bool bpf_counter__can_use_bperf(const struct evsel *evsel); +bool bpf_counter__can_use_bperf_cgrp(const struct evsel *evsel); =20 int bperf_trigger_reading(int prog_fd, int cpu); void set_max_rlimit(void); @@ -70,6 +74,16 @@ static inline int bpf_counter__install_pe(struct evsel *= evsel __maybe_unused, return 0; } =20 +static inline bool bpf_counter__can_use_bperf(const struct evsel *evsel __= maybe_unused) +{ + return false; +} + +static inline bool bpf_counter__can_use_bperf_cgrp(const struct evsel *evs= el __maybe_unused) +{ + return false; +} + #endif /* HAVE_BPF_SKEL */ =20 #endif /* __PERF_BPF_COUNTER_H */ diff --git a/tools/perf/util/bpf_counter_cgroup.c b/tools/perf/util/bpf_cou= nter_cgroup.c index 4e5f4b9dd442..3b99b011b228 100644 --- a/tools/perf/util/bpf_counter_cgroup.c +++ b/tools/perf/util/bpf_counter_cgroup.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #include #include @@ -14,6 +16,7 @@ #include #include #include +#include #include =20 #include "affinity.h" @@ -23,7 +26,9 @@ #include "debug.h" #include "evsel.h" #include "evlist.h" +#include "pmu.h" #include "target.h" +#include "tool_pmu.h" #include "cpumap.h" #include "thread_map.h" =20 @@ -39,7 +44,9 @@ static struct perf_event_attr cgrp_switch_attr =3D { }; =20 static struct evsel *cgrp_switch; +static struct evsel *bperf_cgrp_leader; static struct bperf_cgroup_bpf *skel; +static bool bperf_loaded; =20 #define FD(evt, cpu) (*(int *)xyarray__entry(evt->core.fd, cpu, 0)) =20 @@ -150,25 +157,31 @@ static int bperf_load_program(struct evlist *evlist) =20 evlist__for_each_entry(evlist, evsel) { if (cgrp =3D=3D NULL || evsel->cgrp =3D=3D leader_cgrp) { - unsigned int j; - leader_cgrp =3D evsel->cgrp; - evsel->cgrp =3D NULL; =20 - /* open single copy of the events w/o cgroup */ - err =3D evsel__open_per_cpu(evsel, evsel->core.cpus, -1); - if (err =3D=3D 0) - evsel->supported =3D true; + if (!evsel__is_tool(evsel) && + !perf_cpu_map__has_any_cpu_or_is_empty(evsel->core.cpus)) { + unsigned int j; =20 - map_fd =3D bpf_map__fd(skel->maps.events); - perf_cpu_map__for_each_cpu(cpu, j, evsel->core.cpus) { - int fd =3D FD(evsel, j); - __u32 idx =3D evsel->core.idx * total_cpus + cpu.cpu; + evsel->cgrp =3D NULL; =20 - bpf_map_update_elem(map_fd, &idx, &fd, BPF_ANY); - } + /* open single copy of the events w/o cgroup */ + err =3D evsel__open_per_cpu(evsel, evsel->core.cpus, -1); + if (err =3D=3D 0) { + evsel->supported =3D true; + map_fd =3D bpf_map__fd(skel->maps.events); + perf_cpu_map__for_each_cpu(cpu, j, evsel->core.cpus) { + int fd =3D FD(evsel, j); + __u32 idx =3D evsel->core.idx * total_cpus + cpu.cpu; =20 - evsel->cgrp =3D leader_cgrp; + bpf_map_update_elem(map_fd, &idx, &fd, BPF_ANY); + } + } else { + evsel->supported =3D false; + } + + evsel->cgrp =3D leader_cgrp; + } } =20 if (evsel->cgrp =3D=3D cgrp) @@ -200,6 +213,8 @@ static int bperf_load_program(struct evlist *evlist) int num_events =3D evlist__nr_entries(evlist) / nr_cgroups; =20 evlist__for_each_entry(evlist, evsel) { + if (evsel__is_tool(evsel)) + continue; leader =3D evlist__find_evsel(evlist, evsel->core.idx % num_events); if (leader) evsel->supported =3D leader->supported; @@ -219,21 +234,81 @@ static int bperf_load_program(struct evlist *evlist) } =20 out: + if (err) { + bperf_cgroup_bpf__destroy(skel); + skel =3D NULL; + if (cgrp_switch) { + evsel__close(cgrp_switch); + evsel__put(cgrp_switch); + cgrp_switch =3D NULL; + } + } + return err; +} + +/* + * trigger the leader prog on each cpu, so the cgrp_reading map could get + * the latest results. + */ +static int bperf_cgrp__sync_counters(struct evlist *evlist) +{ + struct perf_cpu cpu; + unsigned int idx; + int prog_fd =3D bpf_program__fd(skel->progs.trigger_read); + + perf_cpu_map__for_each_cpu(cpu, idx, evlist__core(evlist)->all_cpus) + bperf_trigger_reading(prog_fd, cpu.cpu); + + return 0; +} + +static int bperf_cgrp__reset_readings(struct evlist *evlist) +{ + int nr_cpus =3D libbpf_num_possible_cpus(); + int total_cpus =3D cpu__max_cpu().cpu; + struct bpf_perf_event_value *values; + int reading_map_fd, err =3D 0; + __u32 nr_entries =3D evlist__nr_entries(evlist); + + if (nr_cpus > total_cpus) + total_cpus =3D nr_cpus; + if (total_cpus <=3D 0) + return -EINVAL; + + skel->bss->enabled =3D 0; + bperf_cgrp__sync_counters(evlist); + + values =3D calloc(total_cpus, sizeof(*values)); + if (!values) + return -ENOMEM; + + reading_map_fd =3D bpf_map__fd(skel->maps.cgrp_readings); + for (__u32 idx =3D 0; idx < nr_entries; idx++) { + err =3D bpf_map_update_elem(reading_map_fd, &idx, values, BPF_ANY); + if (err) + break; + } + + free(values); return err; } =20 static int bperf_cgrp__load(struct evsel *evsel, struct target *target __maybe_unused) { - static bool bperf_loaded =3D false; - evsel->bperf_leader_prog_fd =3D -1; evsel->bperf_leader_link_fd =3D -1; =20 - if (!bperf_loaded && bperf_load_program(evsel->evlist)) - return -1; + if (!bperf_loaded) { + if (bperf_load_program(evsel->evlist)) + return -1; + bperf_loaded =3D true; + bperf_cgrp_leader =3D evsel; + } else if (evsel =3D=3D bperf_cgrp_leader) { + if (bperf_cgrp__reset_readings(evsel->evlist)) + return -1; + } =20 - bperf_loaded =3D true; /* just to bypass bpf_counter_skip() */ evsel->follower_skel =3D (struct bperf_follower_bpf *)skel; =20 @@ -248,25 +323,9 @@ static int bperf_cgrp__install_pe(struct evsel *evsel = __maybe_unused, return 0; } =20 -/* - * trigger the leader prog on each cpu, so the cgrp_reading map could get - * the latest results. - */ -static int bperf_cgrp__sync_counters(struct evlist *evlist) -{ - struct perf_cpu cpu; - unsigned int idx; - int prog_fd =3D bpf_program__fd(skel->progs.trigger_read); - - perf_cpu_map__for_each_cpu(cpu, idx, evlist__core(evlist)->all_cpus) - bperf_trigger_reading(prog_fd, cpu.cpu); - - return 0; -} - static int bperf_cgrp__enable(struct evsel *evsel) { - if (evsel->core.idx) + if (evsel !=3D bperf_cgrp_leader) return 0; =20 bperf_cgrp__sync_counters(evsel->evlist); @@ -277,7 +336,7 @@ static int bperf_cgrp__enable(struct evsel *evsel) =20 static int bperf_cgrp__disable(struct evsel *evsel) { - if (evsel->core.idx) + if (evsel !=3D bperf_cgrp_leader) return 0; =20 bperf_cgrp__sync_counters(evsel->evlist); @@ -294,7 +353,7 @@ static int bperf_cgrp__read(struct evsel *evsel) struct bpf_perf_event_value *values; int reading_map_fd, err =3D 0; =20 - if (evsel->core.idx) + if (evsel !=3D bperf_cgrp_leader) return 0; =20 bperf_cgrp__sync_counters(evsel->evlist); @@ -310,6 +369,9 @@ static int bperf_cgrp__read(struct evsel *evsel) unsigned int i; struct perf_cpu cpu; =20 + if (evsel__is_tool(evsel)) + continue; + err =3D bpf_map_lookup_elem(reading_map_fd, &idx, values); if (err) { pr_err("bpf map lookup failed: idx=3D%u, event=3D%s, cgrp=3D%s\n", @@ -332,11 +394,16 @@ static int bperf_cgrp__read(struct evsel *evsel) =20 static int bperf_cgrp__destroy(struct evsel *evsel) { - if (evsel->core.idx) + if (evsel !=3D bperf_cgrp_leader) return 0; =20 bperf_cgroup_bpf__destroy(skel); + skel =3D NULL; + evsel__close(cgrp_switch); evsel__put(cgrp_switch); // it'll destroy on_switch progs too + cgrp_switch =3D NULL; + bperf_cgrp_leader =3D NULL; + bperf_loaded =3D false; =20 return 0; } diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index ec26064183b6..638d501a7531 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -754,7 +754,7 @@ static bool evlist__is_enabled(struct evlist *evlist) struct evsel *pos; =20 evlist__for_each_entry(evlist, pos) { - if (!pos->core.fd) + if (!pos->core.fd || evsel__is_bperf(pos)) continue; if (!evsel__is_group_leader(pos) && !pos->merged_hybrid_group) continue; @@ -782,7 +782,8 @@ static void __evlist__disable(struct evlist *evlist, ch= ar *evsel_name, bool excl if (!match) continue; if (pos->disabled || (!evsel__is_group_leader(pos) && - !pos->merged_hybrid_group) || !pos->core.fd) + !pos->merged_hybrid_group) || + !pos->core.fd || evsel__is_bperf(pos)) continue; if (excl_dummy && evsel__is_dummy_event(pos)) continue; @@ -802,7 +803,8 @@ static void __evlist__disable(struct evlist *evlist, ch= ar *evsel_name, bool excl match =3D !evsel__strcmp(evsel__leader(pos), evsel_name); if (!match) continue; - if ((!evsel__is_group_leader(pos) && !pos->merged_hybrid_group) || !pos-= >core.fd) + if ((!evsel__is_group_leader(pos) && !pos->merged_hybrid_group) || + !pos->core.fd || evsel__is_bperf(pos)) continue; if (excl_dummy && evsel__is_dummy_event(pos)) continue; @@ -847,7 +849,8 @@ static void __evlist__enable(struct evlist *evlist, cha= r *evsel_name, bool excl_ match =3D !evsel__strcmp(evsel__leader(pos), evsel_name); if (!match) continue; - if ((!evsel__is_group_leader(pos) && !pos->merged_hybrid_group) || !pos-= >core.fd) + if ((!evsel__is_group_leader(pos) && !pos->merged_hybrid_group) || + !pos->core.fd || evsel__is_bperf(pos)) continue; if (excl_dummy && evsel__is_dummy_event(pos)) continue; @@ -859,7 +862,8 @@ static void __evlist__enable(struct evlist *evlist, cha= r *evsel_name, bool excl_ match =3D !evsel__strcmp(evsel__leader(pos), evsel_name); if (!match) continue; - if ((!evsel__is_group_leader(pos) && !pos->merged_hybrid_group) || !pos-= >core.fd) + if ((!evsel__is_group_leader(pos) && !pos->merged_hybrid_group) || + !pos->core.fd || evsel__is_bperf(pos)) continue; if (excl_dummy && evsel__is_dummy_event(pos)) continue; @@ -1403,6 +1407,9 @@ int evlist__apply_filters(struct evlist *evlist, stru= ct evsel **err_evsel, int err =3D 0; =20 evlist__for_each_entry(evlist, evsel) { + if (!evsel->core.fd) + continue; + /* * filters only work for tracepoint event, which doesn't have cpu limit. * So evlist and evsel should always be same. diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index d89087e295dc..e0f3ca79c9cc 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -477,6 +477,8 @@ static void evsel__init(struct evsel *evsel, evsel->alternate_hw_config =3D PERF_COUNT_HW_MAX; evsel->script_output_type =3D -1; // FIXME: OUTPUT_TYPE_UNSET, see builti= n-script.c evsel->probe_type =3D PROBE__UNKNOWN; + evsel->bperf_leader_prog_fd =3D -1; + evsel->bperf_leader_link_fd =3D -1; } =20 struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx) @@ -557,6 +559,7 @@ struct evsel *evsel__clone(struct evsel *orig) evsel->core.requires_cpu =3D orig->core.requires_cpu; evsel->core.is_pmu_core =3D orig->core.is_pmu_core; evsel->core.is_pmu_uncore =3D orig->core.is_pmu_uncore; + evsel->core.reads_only_on_cpu_idx0 =3D orig->core.reads_only_on_cpu_idx0; =20 if (orig->name) { evsel->name =3D strdup(orig->name); @@ -4810,7 +4813,7 @@ static int store_evsel_ids(struct evsel *evsel, struc= t evlist *evlist) { int cpu_map_idx, thread; =20 - if (evsel__is_retire_lat(evsel) || + if (evsel__is_retire_lat(evsel) || evsel__is_bperf(evsel) || perf_pmu__kind(evsel->pmu) !=3D PERF_PMU_KIND_PE) { struct xyarray *sample_id =3D evsel->core.sample_id; =20 @@ -4833,6 +4836,9 @@ static int store_evsel_ids(struct evsel *evsel, struc= t evlist *evlist) return 0; } =20 + if (!evsel->core.fd) + return 0; + for (cpu_map_idx =3D 0; cpu_map_idx < xyarray__max_x(evsel->core.fd); cpu= _map_idx++) { for (thread =3D 0; thread < xyarray__max_y(evsel->core.fd); thread++) { --=20 2.56.1.750.gf70dc682cf-goog From nobody Mon Nov 23 10:35:26 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.69 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795418415; b=uPDacszOGR9YRq+NRVl5kYpSNbB2Kwxd2DIZ8kcOpqzgT6CyfvQ6Pq1iZ531Wjo7fyFr 4xt0ERClwfv2AdPpn7jnVhQj9iSgHSOSJ3YimbEcpWbeDJaSyDXlgK6bQW0nVeYJORpyI ZgwUOvVT4KPNs5ZzYUubVMucNMcL2Ukkm/KxaIWkovroifZXA5ojxLQcNOuVYG589dq9J UEV0MTou8wEUSXehuvG3OU9eALVUxN56P6jlM7m0mRoDYHE14oL5KtAzB6kspcC0wTYvD AY3VpmCKK3evsY4Th3RcmXOLC+dvXIQPN8zol7HgnJ/9myNvhJDHr0t028NQRniAElw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795418415; 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=3HTZdiqnHY4R9o8iTOMUd/fbqdsTCZLUwRTpRhTEHUE=; b=Me+ghcGZPgIU9Hf8dt1OMG8ayLpp49Y3u1+jnNvP3Zlrc3aPjQ6zX2BsFgFZ/3Es9AZ9 LSAGZgBiKzUUZO41voBZTC4jUFXENcytOuqEm/mQxIQknDn0EFvzaXCkrnj/gO/0Ha8FS 9R3hyhkng6rhG6rwm/7eZmHO81SzmAdZqFy7FqS8BRW0C2MJ+5DJKaobcADtQK26pWlx2 ry+1pZPQUKvXwbSQmm5KywXaemAbqM5C04u+sGHuSofEnZNGC+VNWBfYTOgtnKCZNZava hFEU8JpKxt5qVxP9nGz4OE21t16C9XSa/HR4B/DmKDz+VxTGAVTrRIWc3H6lLLSjICA== 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=mp24r9vo; arc=none smtp.remote-ip=74.125.82.69 Received: from mail-dl1-f69.google.com (mail-dl1-f69.google.com [74.125.82.69]) (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 8C5CF606A321 for ; Mon, 23 Nov 2026 07:20:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8C5CF606A321 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=mp24r9vo Received: by mail-dl1-f69.google.com with SMTP id a92af1059eb24-1b25d977b91so3193085c88.1 for ; Sun, 22 Nov 2026 23:20:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795418415; x=1796023215; 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=3HTZdiqnHY4R9o8iTOMUd/fbqdsTCZLUwRTpRhTEHUE=; b=mp24r9vo0jt/cMdki5/RTIOdGQiUUt2CuKgBvrN49/qNtWtEcv8XVkQXsKktJoA8y4 Brpl9arfrYedRUY7KaCVf1R8fB1Jjz50YMBj7N5m49dtbo4zLqQ/EZVjy67I1F5l9YmP VW/oN3W9fLUdcPefKnsFBSgF5Hn6fDomoaRNQccMn1i3pevMHrnh9fTKnyo0pc0goKyd hN13zg2Nxe1Od9OIhog/qHFIE5uppOX36ZRUbMi40tJoj3tWyEnHxnDwyhI5tm5VyjKi 6dWaXcY8NgyiRnn3/PVdH6YTEvHuxy7co/j+kkPWmCFpLGELYHmXImwLZLTuOgAM4HqE t7UQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795418415; x=1796023215; 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=3HTZdiqnHY4R9o8iTOMUd/fbqdsTCZLUwRTpRhTEHUE=; b=T+ytXcqc27do2O1sCTsgN5brc62U4GOfUEFVsnyhgomiZQNvVTLWrs+ot6qwOvaAfs 0qIK3wRDAdoxMyPzyMVS7rfQmizwrc1tncHwqDnU91ZcNZ53elHnXmukA2ReOzPWqhdI tMqchirqeh6IIu39xlm5kK32+cQmk29ZuqC5czDZw2ziCY5VivC9Vv5EdFf+TSiBaY5U jm1DYTw5BHqVr41QQcyvlvohoClZFinQz0r7Aej2R3xjYToPafmkNamHd6h/8NBG14Z3 EBqsykc4D0t7kZgN5+ibn6e3cEIT+dPjGXDfEQffQkED0kiyopqV7gqMmP/qfhnCkNbT XTfw== X-Forwarded-Encrypted: i=1; AL+hErFWaIDNUw1Kn3TUwzV3kvMyKDBUTQezr3R0pW2RAkIOCaW2xK9tEe4bJfUwO/enZocDQqJ3fLcFjeuxfUQ=@vger.kernel.org X-Gm-Message-State: APuU4Z8VNROYXm8MjZe7EaYW3d1mEzPwQiKruOjMABheFNkB8xNUweBd 9VVVUS4XQUByyW76fFfC2LvSXCiIoTPco/Tdfvb2KiaAB0uHAApNsHiy+xLFU8ENLimOx+hPIdi p5AG4kzUI9A== X-Received: from dled3-n2.prod.google.com ([2002:a05:701b:42c3:20b0:1b5:25e:958b]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:3f07:b0:19a:8e3e:530e with SMTP id a92af1059eb24-1b5568650c0mr11658374c88.8.1795418414324; Sun, 22 Nov 2026 23:20:14 -0800 (PST) Date: Sun, 22 Nov 2026 23:19:10 -0800 In-Reply-To: <20261123071913.653315-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: <20261123071913.653315-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261123071913.653315-11-irogers@google.com> Subject: [PATCH v1 10/13] perf bpf-filter: Fix bpf_link leak by using PERF_EVENT_IOC_SET_BPF From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark Cc: Howard Chu , Thomas Falcon , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In perf_bpf_filter__prepare(), the unpinned filter path attaches skel->progs.perf_sample_filter to each FD(evsel, x, y) using bpf_program__attach_perf_event_opts() and ignores the returned struct bpf_link *, leaking the allocated struct bpf_link_perf and its BPF_PERF_EVENT link fd. Furthermore, bpf_program__attach_perf_event_opts() is designed for callers that transfer ownership of the perf_event fd to the returned bpf_link: when bpf_link__destroy() is called, bpf_link_perf_detach() unconditionally disables and closes perf_link->perf_event_fd. Because evsel->core.fd owns FD(evsel, x, y) and closes them in evlist__close(), destroying the returned bpf_link would double-close the perf_event file descriptors. Fix this by attaching the program directly with ioctl(FD(evsel, x, y), PERF_EVENT_IOC_SET_BPF, fd), matching the pinned filter path right above it in perf_bpf_filter__prepare(). In the kernel, PERF_EVENT_IOC_SET_BPF takes a reference on the BPF program and _free_event() -> perf_event_free_bpf_prog() automatically detaches and releases it when the perf_event fd is closed. This also avoids enabling the event prematurely, allowing pe_opts (.dont_enable =3D true) and the LIBBPF_CURRENT_VERSION_GEQ(1, 7) guard to be dropped. Before (built with EXTRA_CFLAGS=3D"-fsanitize=3Daddress"): # perf record -e cycles --filter 'period > 1000' -- true [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.024 MB perf.data ] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D51328=3D=3DERROR: LeakSanitizer: detected memory leaks Direct leak of 1568 byte(s) in 28 object(s) allocated from: #0 0x7ffbe76ef24f in calloc (/lib64/libasan.so.8+0xef24f) #1 0x625df7 in bpf_program__attach_perf_event_opts (tools/perf/perf+0= x625df7) #2 0xbeddc0 in perf_bpf_filter__prepare util/bpf-filter.c:507 #3 0x8f3def in evlist__apply_filters util/evlist.c:1421 #4 0x44bbcf in record__open tools/perf/builtin-record.c:1469 #5 0x453f9e in __cmd_record tools/perf/builtin-record.c:2594 #6 0x45dadb in cmd_record tools/perf/builtin-record.c:4427 #7 0x5df56b in run_builtin tools/perf/perf.c:383 #8 0x5dfa0e in handle_internal_command tools/perf/perf.c:436 #9 0x5dfddc in run_argv tools/perf/perf.c:480 #10 0x5e0333 in main tools/perf/perf.c:581 SUMMARY: AddressSanitizer: 1568 byte(s) leaked in 28 allocation(s). After (built with EXTRA_CFLAGS=3D"-fsanitize=3Daddress"): # perf record -e cycles --filter 'period > 1000' -- true [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.024 MB perf.data ] Reported-by: Namhyung Kim Closes: https://lore.kernel.org/linux-perf-users/20261020053226.55936-1-nam= hyung@kernel.org/ Fixes: 56ec9457a4a2 ("perf bpf filter: Implement event sample filtering") Signed-off-by: Ian Rogers --- tools/perf/util/bpf-filter.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/tools/perf/util/bpf-filter.c b/tools/perf/util/bpf-filter.c index 72e83ac1d6a4..506414e5c407 100644 --- a/tools/perf/util/bpf-filter.c +++ b/tools/perf/util/bpf-filter.c @@ -40,6 +40,7 @@ * The 'dropped' map is to keep how many samples it dropped by the filter = and * it will be reported as lost samples. */ +#include #include #include #include @@ -423,16 +424,8 @@ int perf_bpf_filter__prepare(struct evsel *evsel, stru= ct target *target) { int i, x, y, fd, ret; struct sample_filter_bpf *skel =3D NULL; - struct bpf_program *prog; - struct bpf_link *link; struct perf_bpf_filter_entry *entry; bool needs_idx_hash =3D !target__has_cpu(target); -#if LIBBPF_CURRENT_VERSION_GEQ(1, 7) - DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts, - .dont_enable =3D true); -#else - DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts); -#endif =20 entry =3D calloc(MAX_FILTERS, sizeof(*entry)); if (entry =3D=3D NULL) @@ -501,14 +494,13 @@ int perf_bpf_filter__prepare(struct evsel *evsel, str= uct target *target) goto err; } =20 - prog =3D skel->progs.perf_sample_filter; + fd =3D bpf_program__fd(skel->progs.perf_sample_filter); for (x =3D 0; x < xyarray__max_x(evsel->core.fd); x++) { for (y =3D 0; y < xyarray__max_y(evsel->core.fd); y++) { - link =3D bpf_program__attach_perf_event_opts(prog, FD(evsel, x, y), - &pe_opts); - if (IS_ERR(link)) { + ret =3D ioctl(FD(evsel, x, y), PERF_EVENT_IOC_SET_BPF, fd); + if (ret < 0) { + ret =3D -errno; pr_err("Failed to attach perf sample-filter program\n"); - ret =3D PTR_ERR(link); goto err; } } --=20 2.56.1.750.gf70dc682cf-goog From nobody Mon Nov 23 10:35:26 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795418420; b=Si92Bj9StsVv/iFY1HaYTHpEMuduGZZCTg6V64Fo1ce8iF9uS6XOSyVx/Bt8i28ZpuO7 pltQCpw2nGaw+PLyhtuTANPnMwzOB/WouWmfsL9fq2C45vIzXzhstIsDsqYsQS2uQYdkb ScD69pXwJFBeGVV9dgSV2h3KF3Pc63s7Lrd7THi/avlGeSVIqN37JZu3MVumZwB+9KIuz X9OSzJZ4e6lTu4y0bILBAFGNQCbw+FOE7aCsJyFn7t90k4MYTXSXq0wzIck7f0InB15j6 9vgU3pkAfm0YzHlOdN6AfAEOPtlgSahXVeNCnmikPjGv4FdJ8t9U81wQNKqGJDckZyA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795418420; 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=MCOimdxXbQe2FArhB5kqVZlCGWrX/sfLuiT+1azFX3Y=; b=LabSXi6lxpkZDWesrHDsMPYAaaqeH5stjhL5122xvXkFA5SLVfrGRtp4DDfPAPvKp2Qw QdurcAeIq81sshwqSoJHUWer5rrI6Zd+B2Nu2GjV308Zbo6ezpTiVd9kSNtqQ272GffoM qJT+mIDPmSxKDTsBCIrI9FgmiC/Zj34irb/arv4VnhjDNZCygvKchZ7IXWM0qP9Vocx2j A6mILfGh/Y7JZdDq73B13kcb4NeulSc2JyKOPOhm2WtTOBI2+CnzKU7Rver0wYycBhkdm UsIsmOm+kgTFST1yPqjR2O1iK/A7q0OH0DGMeF2vlJRskUiM202kkDzoCXcY/KsNLbA== 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=d1bEnGC+; arc=none smtp.remote-ip=74.125.82.197 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.197]) (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 142AA606A327 for ; Mon, 23 Nov 2026 07:20:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 142AA606A327 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=d1bEnGC+ Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-3869accaac2so1485878eec.1 for ; Sun, 22 Nov 2026 23:20:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795418419; x=1796023219; 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=MCOimdxXbQe2FArhB5kqVZlCGWrX/sfLuiT+1azFX3Y=; b=d1bEnGC+jBAEK6M9Hn+38+0mZC8tU+2m50oXoYLtLiE5YthL4h333e8kfoFFSN7oE3 4j+Bjzt2Szk3nmKv6cvOf7czyew2MsPCyRaJjaPowvaHRYsIv2YdnQ7J/EN0wvC+iQZt pHS2ZygZtzX8NXb+GSJp4rvnE/xBGkBd6FBprvgCc23g9+VGU8sLpjRiHZPBMBDtpDh3 OWJ628DyjPD0i16A/EtSzgYe7kNR+8RAosBgE3icm1hOiOKV3Pe3jyMu4w6TfhBNvGrP M9CEiLQhoD8sC6efSs48LrW0F12twwut+DHEw8NlugUkXtR5SyfESDZPd7qvBuQfC0lZ p9Hg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795418419; x=1796023219; 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=MCOimdxXbQe2FArhB5kqVZlCGWrX/sfLuiT+1azFX3Y=; b=Hglbc3uQ81c4pdEczshYU4NMdgFlBTOghzUlWRy90rqo78aT+mWrlXnEDAEDKdNsiI PIZr+lKk1O8taYD8vGYlk2BuAwKV47dmpjRuQz0SnoxoH8tJw/KTor7e2VSwghee37D1 l6WQFN1Wj9qE5GEiGPm7tsXJqbU6Koo/dw3ovA+CwUeMJ0G3StDqEqtX4YaOFs4NqCOg gORILCZNqGn5QKmlZ29/NvjWzOCMWL6aytmcHfMwTetCJVs+GmgsY5RFrY1B6YA7nLVH qTPCZkun3T9aE55hIkIzJtFUqHhP4ta6CjWtdTJw0z+qMReHBqn5Q5N5oC+UvM1p/f2s vL5g== X-Forwarded-Encrypted: i=1; AL+hErEa66uCMz9R/CZTv1Z1NAsLql8cdFs8TOKCaffXSQg55ztQtWEZs2ebrP2Qw+p7nxH6KY0PsEbW3+HBHF4=@vger.kernel.org X-Gm-Message-State: APuU4Z+wykpiZUsS4qPF061sMrP5ZZmex4Fc8CfKGURmAJ3IMRFvtWtR I00ghXM4h5kb29m5uJdhvzYqHwutyNZStQAmjcO2sMzgfP3gXurDeuRrZ24S3xWdA5z15uGpOQd dpPSGNXOTzg== X-Received: from dyr3.prod.google.com ([2002:a05:693c:4003:b0:370:f640:2508]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:d804:b0:386:b076:a940 with SMTP id 5a478bee46e88-386b076a9e4mr2353190eec.26.1795418418870; Sun, 22 Nov 2026 23:20:18 -0800 (PST) Date: Sun, 22 Nov 2026 23:19:11 -0800 In-Reply-To: <20261123071913.653315-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: <20261123071913.653315-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261123071913.653315-12-irogers@google.com> Subject: [PATCH v1 11/13] perf bpf-filter: Copy bpf_filters in evsel__clone() and close on evsel__close() From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark Cc: Howard Chu , Thomas Falcon , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" evsel__clone() copies evsel->filter for tracepoint/hardware filters, but left evsel->bpf_filters empty on cloned evsels. When --for-each-cgroup clones template evsels per cgroup, the cloned evsels lost their parsed BPF filter expressions and ran unfiltered. Add perf_bpf_filter__copy() to deep-copy the BPF filter expression list (including OR-group children, which perf_bpf_filter__destroy() also leaked). In addition, separate runtime skeleton/pinned-map teardown into perf_bpf_filter__close() (without freeing the parsed filter expressions or another evsel's pinned_filters entries) and call it in evsel__close() so repeated runs (such as 'perf stat -r N') release the closed FDs' BPF filter state and attach cleanly on subsequent runs. Fixes: 56ec9457a4a2 ("perf bpf filter: Implement event sample filtering") Signed-off-by: Ian Rogers --- tools/perf/util/bpf-filter.c | 57 ++++++++++++++++++++++++++++++++++-- tools/perf/util/bpf-filter.h | 10 +++++++ tools/perf/util/evlist.c | 2 ++ tools/perf/util/evsel.c | 4 +++ 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/bpf-filter.c b/tools/perf/util/bpf-filter.c index 506414e5c407..affb75e7ff85 100644 --- a/tools/perf/util/bpf-filter.c +++ b/tools/perf/util/bpf-filter.c @@ -515,6 +515,8 @@ int perf_bpf_filter__prepare(struct evsel *evsel, struc= t target *target) struct pinned_filter_idx *pfi, *tmp; =20 list_for_each_entry_safe(pfi, tmp, &pinned_filters, list) { + if (pfi->evsel !=3D evsel) + continue; destroy_idx_hash(pfi); list_del(&pfi->list); free(pfi); @@ -524,22 +526,71 @@ int perf_bpf_filter__prepare(struct evsel *evsel, str= uct target *target) return ret; } =20 -int perf_bpf_filter__destroy(struct evsel *evsel) +static void free_filter_expr_list(struct list_head *head) { struct perf_bpf_filter_expr *expr, *tmp; - struct pinned_filter_idx *pfi, *pos; + struct perf_bpf_filter_expr *grp, *gtmp; =20 - list_for_each_entry_safe(expr, tmp, &evsel->bpf_filters, list) { + list_for_each_entry_safe(expr, tmp, head, list) { + list_for_each_entry_safe(grp, gtmp, &expr->groups, list) { + list_del(&grp->list); + free(grp); + } list_del(&expr->list); free(expr); } +} + +int perf_bpf_filter__copy(struct list_head *dst, const struct list_head *s= rc) +{ + const struct perf_bpf_filter_expr *expr, *grp; + + list_for_each_entry(expr, src, list) { + struct perf_bpf_filter_expr *copy; + + copy =3D perf_bpf_filter_expr__new(expr->term, expr->part, + expr->op, expr->val); + if (!copy) + goto err; + list_add_tail(©->list, dst); + + list_for_each_entry(grp, &expr->groups, list) { + struct perf_bpf_filter_expr *gcopy; + + gcopy =3D perf_bpf_filter_expr__new(grp->term, grp->part, + grp->op, grp->val); + if (!gcopy) + goto err; + list_add_tail(&gcopy->list, ©->groups); + } + } + return 0; + +err: + free_filter_expr_list(dst); + return -ENOMEM; +} + +void perf_bpf_filter__close(struct evsel *evsel) +{ + struct pinned_filter_idx *pfi, *pos; + sample_filter_bpf__destroy(evsel->bpf_skel); + evsel->bpf_skel =3D NULL; =20 list_for_each_entry_safe(pfi, pos, &pinned_filters, list) { + if (pfi->evsel !=3D evsel) + continue; destroy_idx_hash(pfi); list_del(&pfi->list); free(pfi); } +} + +int perf_bpf_filter__destroy(struct evsel *evsel) +{ + free_filter_expr_list(&evsel->bpf_filters); + perf_bpf_filter__close(evsel); return 0; } =20 diff --git a/tools/perf/util/bpf-filter.h b/tools/perf/util/bpf-filter.h index 818c554b91b2..cf016174bc5b 100644 --- a/tools/perf/util/bpf-filter.h +++ b/tools/perf/util/bpf-filter.h @@ -28,7 +28,9 @@ struct perf_bpf_filter_expr *perf_bpf_filter_expr__new(en= um perf_bpf_filter_term enum perf_bpf_filter_op op, unsigned long val); int perf_bpf_filter__parse(struct list_head *expr_head, const char *str); +int perf_bpf_filter__copy(struct list_head *dst, const struct list_head *s= rc); int perf_bpf_filter__prepare(struct evsel *evsel, struct target *target); +void perf_bpf_filter__close(struct evsel *evsel); int perf_bpf_filter__destroy(struct evsel *evsel); u64 perf_bpf_filter__lost_count(struct evsel *evsel); int perf_bpf_filter__pin(void); @@ -45,11 +47,19 @@ static inline int perf_bpf_filter__parse(struct list_he= ad *expr_head __maybe_unu "\tPlease make sure to build with libbpf and BPF skeleton.\n"); return -EOPNOTSUPP; } +static inline int perf_bpf_filter__copy(struct list_head *dst __maybe_unus= ed, + const struct list_head *src __maybe_unused) +{ + return 0; +} static inline int perf_bpf_filter__prepare(struct evsel *evsel __maybe_unu= sed, struct target *target __maybe_unused) { return -EOPNOTSUPP; } +static inline void perf_bpf_filter__close(struct evsel *evsel __maybe_unus= ed) +{ +} static inline int perf_bpf_filter__destroy(struct evsel *evsel __maybe_unu= sed) { return -EOPNOTSUPP; diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index 638d501a7531..5bcb5d0905ea 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -1708,6 +1708,8 @@ void evlist__close(struct evlist *evlist) } =20 evlist__for_each_entry_reverse(evlist, evsel) { + if (!evsel__is_bpf(evsel)) + perf_bpf_filter__close(evsel); perf_evsel__free_fd(&evsel->core); perf_evsel__free_id(&evsel->core); } diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index e0f3ca79c9cc..f36c9b746c84 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -581,6 +581,8 @@ struct evsel *evsel__clone(struct evsel *orig) if (evsel->filter =3D=3D NULL) goto out_err; } + if (perf_bpf_filter__copy(&evsel->bpf_filters, &orig->bpf_filters) < 0) + goto out_err; if (orig->metric_id) { evsel->metric_id =3D strdup(orig->metric_id); if (evsel->metric_id =3D=3D NULL) @@ -3261,6 +3263,8 @@ void evsel__close(struct evsel *evsel) { if (evsel__is_retire_lat(evsel)) evsel__tpebs_close(evsel); + if (!evsel__is_bpf(evsel)) + perf_bpf_filter__close(evsel); perf_evsel__close(&evsel->core); perf_evsel__free_id(&evsel->core); } --=20 2.56.1.750.gf70dc682cf-goog From nobody Mon Nov 23 10:35:26 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795418425; b=qeuKDspHK5gY4ZLK/1418VYaiTeesKMqfaXC5zVe4/uSJXCUuam0biDtJ76ZXJRTiKgI FyA6mAHGul++n/FIApGeTmeANZ16DCWimjqq6fd4IwNVbNi6i1KDrfh2mBpPxJs0ckQ8S hJQ+/TaUuCYqdYu9qdRJvnV74ZYqRna9jZ44+7PlrXZTVh0UFoteIVmaVmUWL69wRVO07 2YX9DrQrsJO0bgY3faZufNnbWRjUlSAud83ryCmKA8othzYJURMx62inyu65qsAaYW+U7 sZM0Nw1u6DOiIYbWJmFyAQx0qcg7QYHfVO8+GDbTpEEcn+F9gs2yEjjGMTT2kXnjDaA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795418425; 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=PY83dvEYzjfItmlyYLWJEIclB8aLqW3e/KxXiFj0wiQ=; b=INs75Pd6Z+YyTbnzK4sNHs8G1ULTfXtyMeksJuvl/CQr1TwgmCHu7REHUZtvDt0Ad69V QSdqeK0CHMMiaaZK+Cw4Af7A6TbsHr7nbJQg7gAOTovS5rNDLDnYx40KlD+p1YDiWCMVe nve2UHKED0Uyi0cwXZMsdUu/LYpO52hRYTLMd2aPcRRII0WrlkjA7tw5iD4uMetAQgqfl lQuqjdleTw60kxKrkfnLC4oTfW4VEZyv/YKNO1ik/8g4zibRQgOBAfjjhQbhtax9yg2L9 kKNg1DEqIsEMs86sOatGrPBErakWFXqCI6sctdwj1P08LziG8DFWr4z0CyeYjXeFN4Q== 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=anstpVv7; arc=none smtp.remote-ip=74.125.82.197 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.197]) (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 4874E606A31A for ; Mon, 23 Nov 2026 07:20:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4874E606A31A 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=anstpVv7 Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-386ad819860so2122846eec.0 for ; Sun, 22 Nov 2026 23:20:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795418424; x=1796023224; 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=PY83dvEYzjfItmlyYLWJEIclB8aLqW3e/KxXiFj0wiQ=; b=anstpVv71gJIAPWZQaPcB2b5wGNrW76PrmpD0vsFioqZo3lyWeUZf07ORqFeppRkOD +y58pvLOYyLwZ5knaaBpvIrrrpKbDEoKMljiHcAhqi2YMMXv2XupKs2m4aNP6fkoDTT6 AtSkyrIFHRIKUVoS4sVCm3D/YKya3QhrhzDWRLaVUXjRny/SRFOlTqALfK7f/wXVjuXO f2HCJjhkUngvb8GFHuB5N52D0nYvUmbQ83ITRT4A+cnxDtSWpQtbei42p5SjrQVh6e+M Z6WIuhkBkhdNozpbuXipomZJFHX18ewlnhum8LBmkZkukL7/9m1UtRkAUg3LK1bsc6Xb JVdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795418424; x=1796023224; 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=PY83dvEYzjfItmlyYLWJEIclB8aLqW3e/KxXiFj0wiQ=; b=p7g8M51VDEHFUbe1CEtTOY1Q3oJtBxKeZ8E7P8W6tkcwTTTa8/1kzozw7MG2Kzbw3K EAWJiPWmd3iEKGWPxLgHF1yDOU40Y25QbZtEkULkR3d3Msb/bzqK8rwLzHgFiCluKkr9 0G777qEXHV7WaGdfGcOGqSBcZBZ1ThzxFDeOy432YzYPzPKsnaDZVIXyuWIo/RKpWkG7 F1CCIyJAGsd+h3Y8MBYU8kdX+rC+e2FcqHnxuCrZVMqVb4do5S3KKhbf7nMGZphXL50J ZE+PvjIKvKhtiltuB7rydskbg1+3EDOqz1qLw+p2e/fpFwkzzFcV4gAbJd05bDwJwUBi 5Grw== X-Forwarded-Encrypted: i=1; AL+hErGQ4r57YF6VrkFSP2CK3ZSt6xyAxnXrBZ8DUfBsk4kVqEeVDHZWBfXBgPOdIm7GgCePe+lYIcskB5TrxoA=@vger.kernel.org X-Gm-Message-State: APuU4Z+GztuZvmVqLhGYsTnn7dTxYOYmnGS0GJ9Wbhl7blujpRFs80FF j7BvgBTJe8C8m/KzEbXZpmDg8n/+60PVovEApXBP2yCMFiHjg/lrZQkeFH094/MtnAIzLWD+RNv QPfSjv904kw== X-Received: from dyr3.prod.google.com ([2002:a05:693c:4003:b0:370:f640:2508]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:b597:b0:386:9e36:df0a with SMTP id 5a478bee46e88-3869e36e011mr10386849eec.13.1795418423867; Sun, 22 Nov 2026 23:20:23 -0800 (PST) Date: Sun, 22 Nov 2026 23:19:12 -0800 In-Reply-To: <20261123071913.653315-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: <20261123071913.653315-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261123071913.653315-13-irogers@google.com> Subject: [PATCH v1 12/13] perf bpf-filter: Support BPF sample filters on tracepoint events From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark Cc: Howard Chu , Thomas Falcon , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Commit 466db4275edd ("perf parse-events: Add parse_uid_filter helper") updated set_filter() to route non-tracepoint filter terms (such as uid and gid) on PERF_TYPE_TRACEPOINT events to evsel->bpf_filters instead of the ftrace tracepoint predicate filter. However, in the kernel's __perf_event_set_bpf_prog() (kernel/events/core.c), attaching a BPF program to a perf_event fd branches on perf_event_is_tracing(event): - Non-tracing events (hardware PMU, software events, breakpoints) call perf_event_set_bpf_handler(), which requires BPF_PROG_TYPE_PERF_EVENT (SEC("perf_event")). - Tracepoint events (event->pmu =3D=3D &perf_tracepoint) reject BPF_PROG_TYPE_PERF_EVENT with -EINVAL and instead require BPF_PROG_TYPE_TRACEPOINT (SEC("tracepoint")), which is attached to event->tp_event->prog_array and executed in perf_trace_run_bpf_submit() before perf_tp_event(), dropping the sample if the BPF program returns 0. Furthermore, the bpf_cast_to_kern_ctx() kfunc is only permitted in BPF_PROG_TYPE_PERF_EVENT programs. Add SEC("tracepoint") int perf_tp_sample_filter(void *ctx) in sample_filter.bpf.c to evaluate context-independent filter terms (uid, gid, tid, cpu, time, cgroup) using BPF helpers without calling bpf_cast_to_kern_ctx(), guarding bpf_get_current_cgroup_id() with CO-RE for !CONFIG_CGROUPS kernels. When preparing a BPF filter on a PERF_TYPE_TRACEPOINT evsel, autoload perf_tp_sample_filter instead of perf_sample_filter and attach it once to the shared trace_event_call. Check CAP_SYS_ADMIN || (CAP_BPF && CAP_PERFMON) when deciding whether the caller can load BPF filter programs directly. Because perf_bpf_filter__pin() and perf_bpf_filter__unpin() only load perf_sample_filter and disable autoload on perf_tp_sample_filter, introduce pin_filter_object() and unpin_filter_object() to pin/unpin the maps and only the loaded perf_sample_filter program. Fixes: 466db4275edd ("perf parse-events: Add parse_uid_filter helper") Signed-off-by: Ian Rogers --- tools/perf/util/bpf-filter.c | 158 ++++++++++++++++--- tools/perf/util/bpf_skel/sample_filter.bpf.c | 101 ++++++++++++ 2 files changed, 238 insertions(+), 21 deletions(-) diff --git a/tools/perf/util/bpf-filter.c b/tools/perf/util/bpf-filter.c index affb75e7ff85..0a07429642be 100644 --- a/tools/perf/util/bpf-filter.c +++ b/tools/perf/util/bpf-filter.c @@ -41,8 +41,10 @@ * it will be reported as lost samples. */ #include +#include #include #include +#include #include #include =20 @@ -120,19 +122,36 @@ static const struct perf_sample_info *get_sample_info= (enum perf_bpf_filter_term return NULL; } =20 +static bool can_load_bpf_filter(void) +{ + return perf_cap__capable(CAP_SYS_ADMIN) || + (perf_cap__capable(CAP_BPF) && perf_cap__capable(CAP_PERFMON)); +} + static int check_sample_flags(struct evsel *evsel, struct perf_bpf_filter_= expr *expr) { const struct perf_sample_info *info; =20 - if (expr->term >=3D PBF_TERM_SAMPLE_START && expr->term <=3D PBF_TERM_SAM= PLE_END && - (evsel->core.attr.sample_type & (1 << (expr->term - PBF_TERM_SAMPLE_S= TART)))) - return 0; - if (expr->term =3D=3D PBF_TERM_UID || expr->term =3D=3D PBF_TERM_GID) { /* Not dependent on the sample_type as computed from a BPF helper. */ return 0; } =20 + if (evsel->core.attr.type =3D=3D PERF_TYPE_TRACEPOINT) { + if (expr->term =3D=3D PBF_TERM_TID || expr->term =3D=3D PBF_TERM_CPU || + expr->term =3D=3D PBF_TERM_TIME || expr->term =3D=3D PBF_TERM_CGROUP) + return 0; + if (expr->op !=3D PBF_OP_GROUP_BEGIN) { + pr_err("Error: %s tracepoint event does not support BPF sample term %d\= n", + evsel__name(evsel), expr->term); + return -1; + } + } + + if (expr->term >=3D PBF_TERM_SAMPLE_START && expr->term <=3D PBF_TERM_SAM= PLE_END && + (evsel->core.attr.sample_type & (1 << (expr->term - PBF_TERM_SAMPLE_S= TART)))) + return 0; + if (expr->op =3D=3D PBF_OP_GROUP_BEGIN) { struct perf_bpf_filter_expr *group; =20 @@ -426,6 +445,11 @@ int perf_bpf_filter__prepare(struct evsel *evsel, stru= ct target *target) struct sample_filter_bpf *skel =3D NULL; struct perf_bpf_filter_entry *entry; bool needs_idx_hash =3D !target__has_cpu(target); + bool is_tracepoint =3D (evsel->core.attr.type =3D=3D PERF_TYPE_TRACEPOINT= ); + bool can_load =3D can_load_bpf_filter(); + + if (evsel->bpf_skel) + return 0; =20 entry =3D calloc(MAX_FILTERS, sizeof(*entry)); if (entry =3D=3D NULL) @@ -437,7 +461,13 @@ int perf_bpf_filter__prepare(struct evsel *evsel, stru= ct target *target) goto err; } =20 - if (needs_idx_hash && geteuid() !=3D 0) { + if (is_tracepoint && !can_load) { + pr_err("Error: Tracepoint BPF filter requires CAP_BPF and CAP_PERFMON (o= r CAP_SYS_ADMIN)\n"); + ret =3D -EPERM; + goto err; + } + + if (needs_idx_hash && !can_load) { int zero =3D 0; =20 /* The filters map is shared among other processes */ @@ -477,8 +507,17 @@ int perf_bpf_filter__prepare(struct evsel *evsel, stru= ct target *target) return 0; } =20 - skel =3D sample_filter_bpf__open_and_load(); + skel =3D sample_filter_bpf__open(); if (!skel) { + ret =3D -errno; + pr_err("Failed to open perf sample-filter BPF skeleton\n"); + goto err; + } + + bpf_program__set_autoload(skel->progs.perf_sample_filter, !is_tracepoint); + bpf_program__set_autoload(skel->progs.perf_tp_sample_filter, is_tracepoin= t); + + if (sample_filter_bpf__load(skel) < 0) { ret =3D -errno; pr_err("Failed to load perf sample-filter BPF skeleton\n"); goto err; @@ -494,14 +533,32 @@ int perf_bpf_filter__prepare(struct evsel *evsel, str= uct target *target) goto err; } =20 - fd =3D bpf_program__fd(skel->progs.perf_sample_filter); - for (x =3D 0; x < xyarray__max_x(evsel->core.fd); x++) { - for (y =3D 0; y < xyarray__max_y(evsel->core.fd); y++) { - ret =3D ioctl(FD(evsel, x, y), PERF_EVENT_IOC_SET_BPF, fd); - if (ret < 0) { - ret =3D -errno; - pr_err("Failed to attach perf sample-filter program\n"); - goto err; + if (is_tracepoint) { + /* + * For PERF_TYPE_TRACEPOINT, PERF_EVENT_IOC_SET_BPF attaches the + * BPF program to event->tp_event->prog_array on the shared + * trace_event_call, which perf_trace_run_bpf_submit() executes + * for all CPUs and tasks before iterating the per-CPU perf + * events. Attach once on the first FD so tp_event->prog_array + * does not get duplicate copies for each per-CPU FD. + */ + fd =3D bpf_program__fd(skel->progs.perf_tp_sample_filter); + ret =3D ioctl(FD(evsel, 0, 0), PERF_EVENT_IOC_SET_BPF, fd); + if (ret < 0) { + ret =3D -errno; + pr_err("Failed to attach perf tracepoint sample-filter program\n"); + goto err; + } + } else { + fd =3D bpf_program__fd(skel->progs.perf_sample_filter); + for (x =3D 0; x < xyarray__max_x(evsel->core.fd); x++) { + for (y =3D 0; y < xyarray__max_y(evsel->core.fd); y++) { + ret =3D ioctl(FD(evsel, x, y), PERF_EVENT_IOC_SET_BPF, fd); + if (ret < 0) { + ret =3D -errno; + pr_err("Failed to attach perf sample-filter program\n"); + goto err; + } } } } @@ -649,7 +706,7 @@ static bool check_bpf_filter_capable(void) { int fd; =20 - if (perf_cap__capable(CAP_BPF)) + if (can_load_bpf_filter()) return true; =20 /* Check if root already pinned the filter programs and maps */ @@ -659,7 +716,7 @@ static bool check_bpf_filter_capable(void) return true; } =20 - pr_err("Error: BPF filter only works for users with the CAP_BPF capabilit= y!\n" + pr_err("Error: BPF filter requires CAP_BPF and CAP_PERFMON (or CAP_SYS_AD= MIN)!\n" "\tPlease run 'perf record --setup-filter pin' as root first.\n"); =20 return false; @@ -684,6 +741,55 @@ int perf_bpf_filter__parse(struct list_head *expr_head= , const char *str) return ret; } =20 +/* + * perf_bpf_filter__pin() and perf_bpf_filter__unpin() only load + * perf_sample_filter and disable autoload on other programs in the skelet= on + * (such as perf_tp_sample_filter). While libbpf's bpf_object__pin_maps() = skips + * maps that were not created, bpf_object__pin_programs() and + * bpf_object__unpin_programs() iterate over every program in skel->obj wi= thout + * checking whether autoload was enabled and fail with -EINVAL when a prog= ram + * was not loaded. Pin and unpin the maps and the loaded perf_sample_filter + * program explicitly instead of calling bpf_object__pin() / bpf_object__u= npin(). + */ +static int pin_filter_object(struct sample_filter_bpf *skel, const char *p= ath) +{ + char *prog_path =3D NULL; + int ret; + + ret =3D bpf_object__pin_maps(skel->obj, path); + if (ret < 0) + return ret; + + if (asprintf(&prog_path, "%s/perf_sample_filter", path) < 0) { + bpf_object__unpin_maps(skel->obj, path); + return -ENOMEM; + } + + ret =3D bpf_program__pin(skel->progs.perf_sample_filter, prog_path); + if (ret < 0) + bpf_object__unpin_maps(skel->obj, path); + + free(prog_path); + return ret; +} + +static int unpin_filter_object(struct sample_filter_bpf *skel, const char = *path) +{ + char *prog_path =3D NULL; + int ret, map_ret; + + if (asprintf(&prog_path, "%s/perf_sample_filter", path) < 0) { + bpf_object__unpin_maps(skel->obj, path); + return -ENOMEM; + } + + ret =3D bpf_program__unpin(skel->progs.perf_sample_filter, prog_path); + free(prog_path); + + map_ret =3D bpf_object__unpin_maps(skel->obj, path); + return ret < 0 ? ret : map_ret; +} + int perf_bpf_filter__pin(void) { struct sample_filter_bpf *skel; @@ -698,6 +804,7 @@ int perf_bpf_filter__pin(void) } =20 /* pinned program will use pid-hash */ + bpf_program__set_autoload(skel->progs.perf_tp_sample_filter, false); bpf_map__set_max_entries(skel->maps.filters, MAX_FILTERS); bpf_map__set_max_entries(skel->maps.event_hash, MAX_EVT_HASH); bpf_map__set_max_entries(skel->maps.idx_hash, MAX_IDX_HASH); @@ -717,7 +824,7 @@ int perf_bpf_filter__pin(void) goto err; } =20 - ret =3D bpf_object__pin(skel->obj, path); + ret =3D pin_filter_object(skel, path); if (ret < 0) { pr_err("Failed to pin BPF filter objects\n"); goto err; @@ -726,8 +833,8 @@ int perf_bpf_filter__pin(void) /* setup access permissions for the pinned objects */ dir_fd =3D open(path, O_PATH); if (dir_fd < 0) { - bpf_object__unpin(skel->obj, path); - ret =3D dir_fd; + ret =3D -errno; + unpin_filter_object(skel, path); goto err; } =20 @@ -770,6 +877,8 @@ int perf_bpf_filter__pin(void) =20 err_close: close(dir_fd); + if (ret < 0) + unpin_filter_object(skel, path); =20 err: free(path); @@ -783,13 +892,20 @@ int perf_bpf_filter__unpin(void) char *path =3D NULL; int ret =3D -1; =20 - skel =3D sample_filter_bpf__open_and_load(); + skel =3D sample_filter_bpf__open(); if (!skel) { ret =3D -errno; pr_err("Failed to open perf sample-filter BPF skeleton\n"); goto err; } =20 + bpf_program__set_autoload(skel->progs.perf_tp_sample_filter, false); + if (sample_filter_bpf__load(skel) < 0) { + ret =3D -errno; + pr_err("Failed to load perf sample-filter BPF skeleton\n"); + goto err; + } + if (asprintf(&path, "%s/fs/bpf/%s", sysfs__mountpoint(), PERF_BPF_FILTER_PIN_PATH) < 0) { ret =3D -errno; @@ -797,7 +913,7 @@ int perf_bpf_filter__unpin(void) goto err; } =20 - ret =3D bpf_object__unpin(skel->obj, path); + ret =3D unpin_filter_object(skel, path); =20 err: free(path); diff --git a/tools/perf/util/bpf_skel/sample_filter.bpf.c b/tools/perf/util= /bpf_skel/sample_filter.bpf.c index 9ddac8e5cd78..ac6309728e14 100644 --- a/tools/perf/util/bpf_skel/sample_filter.bpf.c +++ b/tools/perf/util/bpf_skel/sample_filter.bpf.c @@ -206,6 +206,30 @@ static inline __u64 perf_get_sample(struct bpf_perf_ev= ent_data_kern *kctx, return 0; } =20 +static __always_inline __u64 perf_get_common_sample(struct perf_bpf_filter= _entry *entry) +{ + switch (entry->term) { + case PBF_TERM_TID: + if (entry->part) + return bpf_get_current_pid_tgid() >> 32; + return (__u32)bpf_get_current_pid_tgid(); + case PBF_TERM_CPU: + return bpf_get_smp_processor_id(); + case PBF_TERM_TIME: + return bpf_ktime_get_ns(); + case PBF_TERM_CGROUP: + if (bpf_core_field_exists(struct task_struct, cgroups)) + return bpf_get_current_cgroup_id(); + return 0; + case PBF_TERM_UID: + return bpf_get_current_uid_gid() & 0xFFFFFFFF; + case PBF_TERM_GID: + return bpf_get_current_uid_gid() >> 32; + default: + return 0; + } +} + #define CHECK_RESULT(data, op, val) \ if (!(data op val)) { \ if (!in_group) \ @@ -309,4 +333,81 @@ int perf_sample_filter(void *ctx) return 0; } =20 +/* + * BPF program to be called for tracepoint perf events. + * + * For PERF_TYPE_TRACEPOINT events, the kernel's __perf_event_set_bpf_prog= () + * rejects BPF_PROG_TYPE_PERF_EVENT and only accepts BPF_PROG_TYPE_TRACEPO= INT, + * attaching the program to the shared trace_event_call->prog_array execut= ed + * by perf_trace_run_bpf_submit() prior to perf_tp_event(). Because return= ing 0 + * filters the event at trace_event_call->prog_array (affecting the + * trace_event_call globally while attached), tracepoint BPF filters are n= ever + * pinned for unprivileged use and require CAP_BPF + CAP_PERFMON or + * CAP_SYS_ADMIN. + */ +SEC("tracepoint") +int perf_tp_sample_filter(void *ctx) +{ + struct perf_bpf_filter_entry *entry; + __u64 sample_data; + int in_group =3D 0; + int group_result =3D 0; + int i, k =3D 0; + int *losts; + + (void)ctx; + entry =3D bpf_map_lookup_elem(&filters, &k); + if (entry =3D=3D NULL) + goto drop; + + for (i =3D 0; i < MAX_FILTERS; i++) { + sample_data =3D perf_get_common_sample(&entry[i]); + + switch (entry[i].op) { + case PBF_OP_EQ: + CHECK_RESULT(sample_data, =3D=3D, entry[i].value) + break; + case PBF_OP_NEQ: + CHECK_RESULT(sample_data, !=3D, entry[i].value) + break; + case PBF_OP_GT: + CHECK_RESULT(sample_data, >, entry[i].value) + break; + case PBF_OP_GE: + CHECK_RESULT(sample_data, >=3D, entry[i].value) + break; + case PBF_OP_LT: + CHECK_RESULT(sample_data, <, entry[i].value) + break; + case PBF_OP_LE: + CHECK_RESULT(sample_data, <=3D, entry[i].value) + break; + case PBF_OP_AND: + CHECK_RESULT(sample_data, &, entry[i].value) + break; + case PBF_OP_GROUP_BEGIN: + in_group =3D 1; + group_result =3D 0; + break; + case PBF_OP_GROUP_END: + if (group_result =3D=3D 0) + goto drop; + in_group =3D 0; + break; + case PBF_OP_DONE: + return 1; + default: + break; + } + } + return 1; + +drop: + losts =3D bpf_map_lookup_elem(&dropped, &k); + if (losts !=3D NULL) + __sync_fetch_and_add(losts, 1); + + return 0; +} + char LICENSE[] SEC("license") =3D "Dual BSD/GPL"; --=20 2.56.1.750.gf70dc682cf-goog From nobody Mon Nov 23 10:35:26 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795418431; b=fDVOSR9DMJplfcAczENELuC5oBxN3hhLNZD08Gh8t/qKEuq6aJxPIRy+df/jYS03jYFZ wfeBqqO98GGXBoO+L55OBbUqgqu59MqzlpG390hC4UaGQakaFd4fqaXReHJ8Sx+pqFzLw vYbE6UrAkwoK/vqQRijLMH2Y5PRWe0hE0CFLl/t8eaYYWfZtzzf6xmD6Oyt0LUZup0iXd ATHO8TLZj2QPclK7AkPojgfZ0Kv2Q1qy+Wota/r9OOE5KmOfRbAFWpbD+2NGsduMhrXDf NiInjeCFFPMTjaGdly2HHLmOAvKq329pQKbbVDU9b61+BRdYveB2dbCKNAEwv4k/Z5w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795418431; 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=I7D/3DpE8G8dsmnSj5nDtghWamd39YsS2s0awEB1CqQ=; b=uvVDym/S+4+w30kR8nqC4T4zrLlRhXTQ74Duk2T7yz9hdxrhFYFdoA8pDB1SW9Zs1cdy PkjMUXNZ1VU5d/Lt94s57ATQwmml5N1RUV2KdCUplakG6Zz56CX0kIXOpMoYou/eWk4r8 ALqDzktij051Va4eMw9mYHScuIxBEDplFBsPH9F/1SN2Il09BTc3eM3iQEVhpahkY3z9N 0+teLKRdTwqyRZNrL9H88lfn/kGfw045yC5k5N88+059DkUg88IJMpS85L4mgBRv5Iecm m7N40UhUsqvquctcbGOR+A0v4TMXpR0cQUSKY6agwkW0m7JXxMRo5P15D0pE73sG36Q== 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=V9Hn9Kgw; arc=none smtp.remote-ip=74.125.82.200 Received: from mail-dy1-f200.google.com (mail-dy1-f200.google.com [74.125.82.200]) (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 E21DC606A32E for ; Mon, 23 Nov 2026 07:20:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E21DC606A32E 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=V9Hn9Kgw Received: by mail-dy1-f200.google.com with SMTP id 5a478bee46e88-30bcb065bfdso3385500eec.0 for ; Sun, 22 Nov 2026 23:20:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795418430; x=1796023230; 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=I7D/3DpE8G8dsmnSj5nDtghWamd39YsS2s0awEB1CqQ=; b=V9Hn9KgwC6tA0HZaU/7ADZDRNrIuYckw/FOl9Ri8EokdES317Ae35PUQaQm/SnSZNN wonjS6wXMRDSE02IUWOSwaGmDJBLUmmCbjZvoBRpUj63klsP6fxb6ourr6piHRGhCuO3 hpoJ8fQG7MYG0v1l6KasDhRXf0fWF4kbLAYRSaRorjJZP2CYrY7qpIJSOMxXoQWG2RYV 262wIs+5dlsxAjG7KrXjeo0nYAIL5/67WAzXuB4ysvXVRuDgS93vjAfZLBnZKG8t1rPC CD/EuUGOj3ZNea4u2Sy6qydKRu/sRA96VWBMsaH7014trL/jFQ+6YPq1XLBkg0vabFFY diSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795418430; x=1796023230; 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=I7D/3DpE8G8dsmnSj5nDtghWamd39YsS2s0awEB1CqQ=; b=qOQq9RDhMmsCtwkYc4HkuorjTtbF9ARVxJRwXCBFh4QFWtuRf3/mKctymhvl8MoJTV D6nf0ksyJvyk62wgHDaoeqaxP1tHYNiGm3dx0lJqVFHigX1tl0arkw6GoVSSpwXyaOkd YKY9we0yn/0km2ea1XoM0KOMlfXczmr0wnYq1PbS/Ziq/BX4af0i5JCPr7tPzpHskSGb a0B5KmKuy53XSWalb12haP4XosaLCNYLWI+ioIaOvI+Z9MTv7YygmtXoY1SrZfQW1NV4 XTvE+obAglkhB55k5Ry01eaREOFWaYEEFlgfflhKk8g1rv3yynH1LIwnjuXevqpkB1ym Bq/g== X-Forwarded-Encrypted: i=1; AL+hErE23GeApWKZiuMyVEt9eB4dh9LMhVV2v7QCFPw3O+sTr8A0571Y34r3aGSx6GYrrMbiKDNQyUz6q9Emk/Y=@vger.kernel.org X-Gm-Message-State: APuU4Z8TQlzTvGRsYA3eMcEohqR4HYxr/yZVFxpX6ip2kNoHwiXyb/Lj WI2AU8B6lAVdVV5rXil4Kpfc2wDhU4mmEH+XbimnjdCPO5C8xyLSNR36rDYkrNB9+YD5e5i4hcI N984KZNtuUw== X-Received: from dybnj5.prod.google.com ([2002:a05:7300:d085:b0:384:5243:f41d]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:693c:42cb:b0:386:b621:d8ec with SMTP id 5a478bee46e88-386b621dc16mr945943eec.19.1795418429634; Sun, 22 Nov 2026 23:20:29 -0800 (PST) Date: Sun, 22 Nov 2026 23:19:13 -0800 In-Reply-To: <20261123071913.653315-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: <20261123071913.653315-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261123071913.653315-14-irogers@google.com> Subject: [PATCH v1 13/13] perf parse-events: Route non-field tracepoint filter terms to BPF filter From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark Cc: Howard Chu , Thomas Falcon , Song Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Commit 466db4275edd ("perf parse-events: Add parse_uid_filter helper") only checked strstr(str, "uid") when routing tracepoint --filter strings to evsel->bpf_filters, missing gid/tid/pid/time/cgroup BPF terms, matching "uid" inside quoted strings or tracepoint field names, and silently mis-evaluating tracepoint fields named "pid" when mixed with a BPF term. Route a tracepoint --filter to the BPF filter when it references one of the BPF tracepoint filter terms (uid, gid, tid, pid, time, cgroup) that is not a field of the tracepoint, looking up fields with evsel__field() or, when built without libtraceevent, from the tracefs format file via tp_pmu__has_field() (retaining evsel->tp_sys and evsel->tp_name regardless of HAVE_LIBTRACEEVENT). Ignore words inside quoted string literals, keep "cpu" as a generic ftrace field filter when used alone, and reject filters that mix BPF terms with tracepoint fields of the same name. Before: # perf record -e kmem:mm_page_alloc --filter 'uid =3D=3D 0' -- true libbpf: prog 'perf_sample_filter': failed to attach to perf_event FD 3: I= nvalid argument Failed to attach perf sample-filter program failed to set filter "BPF" on event kmem:mm_page_alloc with 22 (Invalid a= rgument) After: # perf record -e kmem:mm_page_alloc --filter 'uid =3D=3D 0' -- true [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.024 MB perf.data (42 samples) ] Fixes: 466db4275edd ("perf parse-events: Add parse_uid_filter helper") Signed-off-by: Ian Rogers --- tools/perf/Documentation/perf-record.txt | 14 +- tools/perf/util/evsel.c | 8 +- tools/perf/util/evsel.h | 2 +- tools/perf/util/parse-events.c | 155 +++++++++++++++++++++-- tools/perf/util/tp_pmu.c | 55 ++++++++ tools/perf/util/tp_pmu.h | 1 + 6 files changed, 216 insertions(+), 19 deletions(-) diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Document= ation/perf-record.txt index 2351f7b4f5fc..e92672f45494 100644 --- a/tools/perf/Documentation/perf-record.txt +++ b/tools/perf/Documentation/perf-record.txt @@ -173,7 +173,19 @@ OPTIONS =20 A BPF filter can access the sample data and make a decision based on the data. Users need to set an appropriate sample type to use the BPF - filter. BPF filters need root privilege. + filter. BPF filters need CAP_BPF and CAP_PERFMON (or CAP_SYS_ADMIN, or + a pinned BPF filter for non-tracepoint events). On tracepoint events, + BPF filters support context-independent terms ('uid', 'gid', 'tid', + 'pid', 'cpu', 'time', 'cgroup') by attaching a + 'BPF_PROG_TYPE_TRACEPOINT' program to the tracepoint's shared + 'trace_event_call->prog_array', which filters the tracepoint before + 'perf_tp_event()' runs across concurrent perf_event listeners on that + tracepoint. A tracepoint filter is only treated as a BPF filter when + it uses one of these terms that is not a field of the tracepoint (a + filter only using 'cpu' remains an ftrace filter on the generic 'cpu' + field). Filters mixing such BPF terms with tracepoint fields of the + same name (e.g. 'pid' on sched:sched_wakeup) are rejected; use separate + --filter options instead. =20 The sample data field can be specified in lower case letter. Multiple filters can be separated with comma. For example, diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index f36c9b746c84..7be14596982c 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -589,7 +589,6 @@ struct evsel *evsel__clone(struct evsel *orig) goto out_err; } evsel->cgrp =3D cgroup__get(orig->cgrp); -#ifdef HAVE_LIBTRACEEVENT if (orig->tp_sys) { evsel->tp_sys =3D strdup(orig->tp_sys); if (evsel->tp_sys =3D=3D NULL) @@ -600,6 +599,7 @@ struct evsel *evsel__clone(struct evsel *orig) if (evsel->tp_name =3D=3D NULL) goto out_err; } +#ifdef HAVE_LIBTRACEEVENT evsel->tp_format =3D orig->tp_format; #endif evsel->handler =3D orig->handler; @@ -670,7 +670,6 @@ struct evsel *evsel__newtp_idx(const char *sys, const c= har *name, int idx, bool if (asprintf(&evsel->name, "%s:%s", sys, name) < 0) goto out_free; =20 -#ifdef HAVE_LIBTRACEEVENT evsel->tp_sys =3D strdup(sys); if (!evsel->tp_sys) goto out_free; @@ -678,7 +677,6 @@ struct evsel *evsel__newtp_idx(const char *sys, const c= har *name, int idx, bool evsel->tp_name =3D strdup(name); if (!evsel->tp_name) goto out_free; -#endif =20 event_attr_init(&attr); =20 @@ -696,10 +694,8 @@ struct evsel *evsel__newtp_idx(const char *sys, const = char *name, int idx, bool =20 out_free: zfree(&evsel->name); -#ifdef HAVE_LIBTRACEEVENT zfree(&evsel->tp_sys); zfree(&evsel->tp_name); -#endif free(evsel); out_err: return ERR_PTR(err); @@ -2146,10 +2142,8 @@ static void evsel__exit(struct evsel *evsel) perf_evsel__exit(&evsel->core); zfree(&evsel->group_name); zfree(&evsel->name); -#ifdef HAVE_LIBTRACEEVENT zfree(&evsel->tp_sys); zfree(&evsel->tp_name); -#endif zfree(&evsel->filter); zfree(&evsel->group_pmu_name); zfree(&evsel->unit); diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 53f3641b66e1..87b9373e7a51 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -64,9 +64,9 @@ struct evsel { char *name; char *group_name; const char *group_pmu_name; -#ifdef HAVE_LIBTRACEEVENT char *tp_sys; char *tp_name; +#ifdef HAVE_LIBTRACEEVENT struct tep_event *tp_format; #endif char *filter; diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index ba3e5015058c..5b284feccb08 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -4,7 +4,9 @@ #include #include #include +#include #include +#include #include #include #include "cpumap.h" @@ -2624,10 +2626,131 @@ foreach_evsel_in_last_glob(struct evlist *evlist, return 0; } =20 -/* Will a tracepoint filter work for str or should a BPF filter be used? */ -static bool is_possible_tp_filter(const char *str) +static bool is_filter_word_char(char c) { - return strstr(str, "uid") =3D=3D NULL; + return isalnum((unsigned char)c) || c =3D=3D '_'; +} + +/* + * Does str contain the identifier word, ignoring occurrences inside quoted + * string literals (e.g. prev_comm =3D=3D "cpu"), cgroup path operands, and + * predicate argument lists? + */ +static bool has_filter_word(const char *str, const char *word) +{ + size_t len =3D strlen(word); + const char *p =3D str; + + while (*p) { + const char *start, *q; + size_t tok_len; + + if (*p =3D=3D '"' || *p =3D=3D '\'') { + char quote =3D *p++; + + while (*p && *p !=3D quote) { + if (*p =3D=3D '\\' && p[1]) + p++; + p++; + } + if (*p) + p++; + continue; + } + if (!is_filter_word_char(*p)) { + p++; + continue; + } + start =3D p; + while (is_filter_word_char(*p)) + p++; + tok_len =3D (size_t)(p - start); + if (tok_len =3D=3D len && !strncmp(start, word, len)) + return true; + + /* Skip unquoted cgroup path operands (e.g. cgroup =3D=3D /test-pid). */ + if (tok_len =3D=3D 6 && !strncmp(start, "cgroup", 6)) { + q =3D p; + while (*q =3D=3D ' ' || *q =3D=3D '\t') + q++; + if (!strncmp(q, "=3D=3D", 2) || !strncmp(q, "!=3D", 2)) { + q +=3D 2; + while (*q =3D=3D ' ' || *q =3D=3D '\t') + q++; + if (*q && *q !=3D '"' && *q !=3D '\'') { + while (*q && !strchr(" \t\n,()!=3D>&|<", *q)) + q++; + p =3D q; + } + } + continue; + } + + /* Skip predicate argument lists (e.g. lock_held(pid)). */ + q =3D p; + while (*q =3D=3D ' ' || *q =3D=3D '\t') + q++; + if (*q =3D=3D '(') { + p =3D q + 1; + while (*p && *p !=3D ')') + p++; + if (*p =3D=3D ')') + p++; + } + } + return false; +} + +static bool tp_evsel_has_field(struct evsel *evsel, const char *name) +{ +#ifdef HAVE_LIBTRACEEVENT + if (evsel__field(evsel, name)) + return true; +#endif + if (evsel->tp_sys && evsel->tp_name) + return tp_pmu__has_field(evsel->tp_sys, evsel->tp_name, name); + return false; +} + +/* + * Will a tracepoint filter work for str or should a BPF filter be used? + * Returns 1 for a tracepoint (ftrace) filter, 0 for a BPF filter, or a + * negative error when str mixes BPF-only terms with tracepoint fields that + * share a BPF term name (such as "pid" on sched:sched_wakeup), which a BPF + * filter would otherwise silently evaluate against the current task. + */ +static int is_possible_tp_filter(struct evsel *evsel, const char *str) +{ + /* + * BPF terms evaluated by perf_tp_sample_filter. "cpu" is omitted as + * ftrace provides a generic "cpu" field for all tracepoints (see + * trace_define_generic_fields()). + */ + static const char *const bpf_tp_terms[] =3D { + "uid", "gid", "tid", "pid", "time", "cgroup", + }; + const char *bpf_term =3D NULL, *tp_field =3D NULL; + + for (size_t i =3D 0; i < ARRAY_SIZE(bpf_tp_terms); i++) { + if (!has_filter_word(str, bpf_tp_terms[i])) + continue; + if (tp_evsel_has_field(evsel, bpf_tp_terms[i])) + tp_field =3D bpf_tp_terms[i]; + else if (!bpf_term) + bpf_term =3D bpf_tp_terms[i]; + } + if (!bpf_term) + return 1; + if (!tp_field && has_filter_word(str, "cpu") && + tp_evsel_has_field(evsel, "cpu")) + tp_field =3D "cpu"; + if (tp_field) { + pr_err("Error: filter '%s' on %s mixes BPF term '%s' with tracepoint fie= ld '%s'.\n" + "Hint: use separate --filter options for tracepoint fields and BP= F terms.\n", + str, evsel__name(evsel), bpf_term, tp_field); + return -EINVAL; + } + return 0; } =20 static int set_filter(struct evsel *evsel, const void *arg) @@ -2635,6 +2758,7 @@ static int set_filter(struct evsel *evsel, const void= *arg) const char *str =3D arg; int nr_addr_filters =3D 0; struct perf_pmu *pmu; + int tp_filter =3D 0; =20 if (evsel =3D=3D NULL) { fprintf(stderr, @@ -2642,7 +2766,12 @@ static int set_filter(struct evsel *evsel, const voi= d *arg) return -1; } =20 - if (evsel->core.attr.type =3D=3D PERF_TYPE_TRACEPOINT && is_possible_tp_f= ilter(str)) { + if (evsel->core.attr.type =3D=3D PERF_TYPE_TRACEPOINT) { + tp_filter =3D is_possible_tp_filter(evsel, str); + if (tp_filter < 0) + return -1; + } + if (tp_filter) { if (evsel__append_tp_filter(evsel, str) < 0) { fprintf(stderr, "not enough memory to hold filter string\n"); @@ -2680,14 +2809,20 @@ int parse_filter(const struct option *opt, const ch= ar *str, =20 int parse_uid_filter(struct evlist *evlist, uid_t uid) { - struct option opt =3D { - .value =3D &evlist, - }; + struct evsel *evsel; char buf[128]; - int ret; + int ret =3D 0; =20 - snprintf(buf, sizeof(buf), "uid =3D=3D %d", uid); - ret =3D parse_filter(&opt, buf, /*unset=3D*/0); + if (!evlist || evlist__nr_entries(evlist) =3D=3D 0) { + ret =3D -EINVAL; + } else { + snprintf(buf, sizeof(buf), "uid =3D=3D %d", uid); + evlist__for_each_entry(evlist, evsel) { + ret =3D perf_bpf_filter__parse(&evsel->bpf_filters, buf); + if (ret) + break; + } + } if (ret) { if (use_browser >=3D 1) { /* diff --git a/tools/perf/util/tp_pmu.c b/tools/perf/util/tp_pmu.c index 5ac732e06841..fb3ff9435fa0 100644 --- a/tools/perf/util/tp_pmu.c +++ b/tools/perf/util/tp_pmu.c @@ -6,6 +6,7 @@ #include #include #include +#include #include =20 int tp_pmu__id(const char *sys, const char *name) @@ -26,6 +27,60 @@ int tp_pmu__id(const char *sys, const char *name) return id; } =20 +/* + * Does the tracefs format file of tracepoint sys:name declare a field cal= led + * field? Format lines are of the form "\tfield: [];\t...= ". + * Works without libtraceevent. + */ +bool tp_pmu__has_field(const char *sys, const char *name, const char *fiel= d) +{ + char *tp_dir =3D get_events_file(sys); + char path[PATH_MAX]; + char *format =3D NULL, *line, *saveptr =3D NULL; + size_t format_size, field_len =3D strlen(field); + bool found =3D false; + + if (!tp_dir) + return false; + + scnprintf(path, sizeof(path), "%s/%s/format", tp_dir, name); + put_events_file(tp_dir); + if (filename__read_str(path, &format, &format_size)) + return false; + + for (line =3D strtok_r(format, "\n", &saveptr); line && !found; + line =3D strtok_r(NULL, "\n", &saveptr)) { + char *decl =3D strstr(line, "field:"), *end; + + if (!decl) + continue; + decl +=3D strlen("field:"); + end =3D strchr(decl, ';'); + if (!end) + continue; + /* + * Drop a trailing array suffix, e.g. "char prev_comm[16]", but + * not the "[]" in the type of "__data_loc char[] name". + */ + while (end > decl && end[-1] =3D=3D ' ') + end--; + if (end > decl && end[-1] =3D=3D ']') { + while (end > decl && *end !=3D '[') + end--; + } + /* The field name is the last token of the declaration. */ + while (end > decl && end[-1] =3D=3D ' ') + end--; + if ((size_t)(end - decl) >=3D field_len && + !strncmp(end - field_len, field, field_len) && + (end - field_len =3D=3D decl || *(end - field_len - 1) =3D=3D ' ' || + *(end - field_len - 1) =3D=3D '*')) + found =3D true; + } + free(format); + return found; +} + =20 int tp_pmu__for_each_tp_event(const char *sys, void *state, tp_event_callb= ack cb) { diff --git a/tools/perf/util/tp_pmu.h b/tools/perf/util/tp_pmu.h index 30456bd6943d..a60ec0e8a176 100644 --- a/tools/perf/util/tp_pmu.h +++ b/tools/perf/util/tp_pmu.h @@ -8,6 +8,7 @@ typedef int (*tp_sys_callback)(void *state, const char *sys= _name); typedef int (*tp_event_callback)(void *state, const char *sys_name, const = char *evt_name); =20 int tp_pmu__id(const char *sys, const char *name); +bool tp_pmu__has_field(const char *sys, const char *name, const char *fiel= d); int tp_pmu__for_each_tp_event(const char *sys, void *state, tp_event_callb= ack cb); int tp_pmu__for_each_tp_sys(void *state, tp_sys_callback cb); =20 --=20 2.56.1.750.gf70dc682cf-goog