From nobody Thu Sep 24 22:56:31 2026 Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D62C9528435 for ; Fri, 18 Sep 2026 21:19:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.70 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766388; cv=none; b=l90tpqQT94TyhmQr+ge3K1TL2BWrwr4D9TRBFaQ2i7L4/NDL6Ij9c78+zyPauFh5q34h2/dAD/ryWUk7g1bGtUCreDnpf5SYhe4IAjaxGEMJ62vdekrNbdUPFxXGvBpeBOLiIoeWEGBdhwHPLxWXyL7U14Hy7j6TS3DFtcA9iKc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766388; c=relaxed/simple; bh=Bl7p5fPq1zzv8earO2MgxKfTQKk3Ks5v1cj5G/bckVs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VvYZqPSOUuHrDJBbcogskObmmJOdUfxKunm1h8I2nY3ns8J+Yvz5e0gUTSeffRmqjyc6pzxQQZsHK9OKvBnA2uhleiu0UP2rbpxndAqRmKj/iPPjOFh3QFTXLatZa/IU+aOJSwrk9QNrlavnYo5cA4d2KgM3y1lHxsA+qNe39MY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=p4bBk+GQ; arc=none smtp.client-ip=209.85.216.70 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.b="p4bBk+GQ" Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-39de1f85cb2so1518175a91.0 for ; Fri, 18 Sep 2026 14:19:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789766386; x=1790371186; 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=q198WRfVuFdwy23wGpDOJAEDHlwObTAenaQ9DfUue+I=; b=p4bBk+GQXz8+40B3xQ7ywYnp376nYIN/jJYpMclPYueuHMJk1Uf3AkzPj1OgjU8MX0 qRgLwViCC7y9pRmx/5r/L5KBiddEClU67X7wp5+cKkA6xyw8VM/k99rsSApuHZexqq3V 3g6a0/CMkfMIjgobWjFTcTgSdTS8exAFjiHI8CetLhW8bis3zBnLv8xkPHtAKMdTZvqj uS8LGrPTJj8W4PL/9WsRFzM/LFD30WCZSeDUASDsBjLMRDfyglxwHVfIDNCGx0vm/gJO vxIgPJLM/MStvneQTHFb4sMLrnxg31NYRGAsUGMs7hMn8yYhBblzPcqnAffRZ7OLvC7O 3jkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789766386; x=1790371186; 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=q198WRfVuFdwy23wGpDOJAEDHlwObTAenaQ9DfUue+I=; b=dEUiMq7K5OEyxHHacsRbrE3uB6QSjQxto1f/5EeVgspgzuK1CDOhYFkawboZYhKnKU X8M3v4LN+nvI2HBv9frn3ipSn6hWrhV11fbjnrbxBXlLs7srKHFoS0prCq7cQaljZ5lH 6BlAxpch7CTzEuOFKl+847QbdfGZTw9gCP2lRKiHXG3+1d4U/3GlHlV3fnJhIGYTYqYz 9tO1yOJhwaZzSNQ9h89VAk2HjweD5ec9yhppwFfaY89cNUU/OjL1UQpDTD1WkMMeYaiC rRl5CRRmol0GIHY6gp4cx7s/+RCQ6dGS7eIFqBAdrT9Y3P3qj9VaWR3iUJCp14VLoDTt qSIg== X-Forwarded-Encrypted: i=1; AKwUvBxH1VQ6b7TTjbCZivGatxRelvP7FgXGtb7E2Xjim1jb0Haow6+ebr8QFx/BQnAt8/kM4nJBhOvzVspH8EE=@vger.kernel.org X-Gm-Message-State: AFuF++lZf8z7glkBCwSoi8lgdFoXyjyXwQYoQP3rum2vhJwseS/PYh0T Xc2fxfxzjaXjGruij7MCuThgQfiabIvqkPAZy7rx75poiviIbdZN6eGqY4g7vUnK76gE/zHQBOZ 18+rFFSUhhg== X-Received: from dlbqq10.prod.google.com ([2002:a05:7022:ed0a:b0:143:7c9b:184f]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2ecb:b0:39e:31ec:ba55 with SMTP id 98e67ed59e1d1-39e54d36b09mr8589531a91.7.1789766386140; Fri, 18 Sep 2026 14:19:46 -0700 (PDT) Date: Fri, 18 Sep 2026 14:19:15 -0700 In-Reply-To: <20260918211932.2966061-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918140659.2501976-1-irogers@google.com> <20260918211932.2966061-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918211932.2966061-2-irogers@google.com> Subject: [PATCH v4 01/18] perf trace: Include the headers declaring pid_t, strcmp and assert From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org, Howard Chu Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" trace_augment.h uses pid_t in the augmented_syscalls__set_filter_pids() prototype and in the !HAVE_BPF_SKEL stub. bpf_trace_augment.c calls strcmp() in augmented_syscalls__find_by_title() and assert() in augmented_syscalls__create_bpf_output(), but neither pulls in the header that declares what it uses. Both happen to build today only because something else in the include chain drags , and in first, which is not guaranteed and does not hold on libcs such as musl that keep the POSIX namespaces strictly separated. Include , and explicitly. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/util/bpf_trace_augment.c | 2 ++ tools/perf/util/trace_augment.h | 1 + 2 files changed, 3 insertions(+) diff --git a/tools/perf/util/bpf_trace_augment.c b/tools/perf/util/bpf_trac= e_augment.c index a9cf2a77ded1..ebb26225fb04 100644 --- a/tools/perf/util/bpf_trace_augment.c +++ b/tools/perf/util/bpf_trace_augment.c @@ -1,5 +1,7 @@ +#include #include #include +#include =20 #include "bpf_skel/augmented_raw_syscalls.skel.h" #include "debug.h" diff --git a/tools/perf/util/trace_augment.h b/tools/perf/util/trace_augmen= t.h index 4f729bc67753..a1cd9a5e0213 100644 --- a/tools/perf/util/trace_augment.h +++ b/tools/perf/util/trace_augment.h @@ -2,6 +2,7 @@ #define TRACE_AUGMENT_H =20 #include +#include =20 struct bpf_program; struct evlist; --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Thu Sep 24 22:56:31 2026 Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 98E3552CCEC for ; Fri, 18 Sep 2026 21:19:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.71 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766390; cv=none; b=UZAa98xWqI9Dj5GYhyTZg1V5c256c3d7LltmzGnhlUPcZxCXScZokvNdaqmd1gFg7nEreZgIL6fT7cnDKnxIJT8ZQKzjN4H5PNkhV34lkufhhZSGYp52OAFWKNfKg9/VclkiPzgzBEOAC2gNAAlqMGMrt6cOadZSPxnKX+3Rdjg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766390; c=relaxed/simple; bh=GPdTrvCkiHU3/coaruf9IhbjFFcf1z7GDHB3eC+nIgM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XTJfCiZo274/Q91uGul+LDLPNuoDWDsAUQv4UuRfd5gLDCCaClAI1bkL0XQcZRLEZbQrJ5ngAKhy539eioxxFmapKZ/w7ctPWd9NQralvgsawYNa6+v+hbAQATebGIhm38OoNARrteuROGgHiL3F0Xaq3wHexGTwbqF1eXjOXQo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cI46M6o+; arc=none smtp.client-ip=209.85.216.71 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.b="cI46M6o+" Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-39deda201bcso531091a91.2 for ; Fri, 18 Sep 2026 14:19:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789766389; x=1790371189; 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=Ajb49arDbZGSllkSDDxIszaTKHNJOa78okU0Ays49S8=; b=cI46M6o+DhpnnuJiYG9Fh/4gS52Rev41Q8/7iZR+vE1bs37u3UPqofHerJl5pmzOHO jYYkcu+5RToJGiAdnrg3HvVrVZGqUFOB6D26sJcN6O9Tb3fdY+OWuABw4eWyB0jHyaZP FJ1UCIq45EEk/IyJ1r+iQlZF8ON+kS+3kasB3Qdf6ksdmm5F5LaB9hADJjrn6Xjit+3f hwSNz2DmEL/AtKvnk6Tfqm7ydGLJdOvgsCHN6O8C+mEnn0t6IDBzqNOpVVsQhdeHDmJ9 BbpRM3A8iduOu/C/cilw7lfbK/ve5Bb8nOdmU+/3EZK+GQ2kM9PrCDv3rZMq8u9RHsj5 anVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789766389; x=1790371189; 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=Ajb49arDbZGSllkSDDxIszaTKHNJOa78okU0Ays49S8=; b=H15cg7HPhKnL1QD72P+qE0mF6fLMn57fYY0lPnuVCXaVnNkXXNa9NTX7U669Nw3TrF ZNNzXig1t1I+ifKGY04KanDoCO2evj6DPxteuSPm2M884NmI7PVJ4OhUDACTlIdSYDXk fGPXDddrYJKZXt6Prm4ha/PDPix60El2xGJrbeDd/uH3DX/DWUMIF4gYNquqOh2aGWWj Pl9ppUL75Q1zFH2/hWpi5kxiN7m7MRAmsImg3lqFbYnao+QlxciVCLunBqlY0q/A4ZFo cSiTnnytLrbVVwrkrjCqhL67vMuyjtn33a4ODyfML6zqAh364O2Zld1p3n4TA1c33crb mZgA== X-Forwarded-Encrypted: i=1; AKwUvBwZSw2iJwtoLEMoN2SydqMDCaBwG1YA+xOnQS6V431gUx0CKinHWhDbVLeaFv5mgrKHG/5MrUKJm5P9PVE=@vger.kernel.org X-Gm-Message-State: AFuF++nXsNnpX/cBgnBKerVGHM4AFLD/d/3mDVwWI34ZFHGziN3Bk7P/ eHGdNc7rnYuMJpCwFcKUFi+zArISdQ1oKGlVfNVzoTJ/rnJKH0fdvx+jRRkqL9cRQSUHLsb/tRh y8yEpah5D7Q== X-Received: from dleb12-n2.prod.google.com ([2002:a05:701b:424c:20b0:13c:fe05:89a3]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2d0c:b0:39e:6c68:c77d with SMTP id 98e67ed59e1d1-39e6c68ca07mr1266795a91.51.1789766388741; Fri, 18 Sep 2026 14:19:48 -0700 (PDT) Date: Fri, 18 Sep 2026 14:19:16 -0700 In-Reply-To: <20260918211932.2966061-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918140659.2501976-1-irogers@google.com> <20260918211932.2966061-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918211932.2966061-3-irogers@google.com> Subject: [PATCH v4 02/18] perf trace: Free the whole evsel_trace in evsel__put_and_free_priv From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org, Howard Chu Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Every evsel->priv in builtin-trace.c is a struct evsel_trace, allocated by evsel_trace__new(). It holds a syscall_arg_fmt array in its fmt member, which evsel__syscall_arg_fmt() allocates on demand for the syscalls:sys_{enter,exit}_SYSCALL tracepoints and for every other tracepoint that gets its arguments pretty printed. evsel__put_and_free_priv() only did zfree(&evsel->priv), releasing the evsel_trace itself and leaking that array. Use evsel_trace__delete(), which frees fmt first, exactly as the out_delete path of evsel__syscall_arg_fmt() already does. The current callers are all error paths that run before fmt can have been allocated, so nothing leaks in practice today, but the helper is the obvious thing to reach for whenever an evsel is discarded and it should be safe for that. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/builtin-trace.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index 20fffc24507b..f67557e7a254 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -464,7 +464,13 @@ static int evsel__init_tp_ptr_field(struct evsel *evse= l, struct tp_field *field, =20 static void evsel__put_and_free_priv(struct evsel *evsel) { - zfree(&evsel->priv); + /* + * evsel->priv is always a struct evsel_trace here, so it has to go + * through evsel_trace__delete(): zfree() on its own would release the + * struct while leaking the syscall_arg_fmt array hanging off it. + */ + evsel_trace__delete(evsel->priv); + evsel->priv =3D NULL; evsel__put(evsel); } =20 --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Thu Sep 24 22:56:31 2026 Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1D784524AE5 for ; Fri, 18 Sep 2026 21:19:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.71 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766393; cv=none; b=PO6RK1eRXcQjgQQfe4RPm6/oI93zLTLywCpTxXoEdoXb6gQVO0Z3obSkkIHIuyJUum1o//RsB77iG2BQnZiwl0mnrEC9o2xrPlxjUAjgVwoxC7MNSyOjL+1wXLMydMnBTWBYy6wRXawZScFzCMLjcnv+RkzvrfAWfxy7lygtkps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766393; c=relaxed/simple; bh=TsjciXmWqrprKQmPUDQvCle3hS8U2TnFcaL8HSWbgjQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cfpzw9/MkpfVBJUUvafXy7uKWbZsZShHuq6G//uKdSVYDBYCaocNGiA2NXF2cm+LMQo8szX0JbiStWkiH5OoBgeGGkBqAXf0yDOaB9GOt4G+4/BVzSHpOmjUZtmiFH9ST6IOrLo25oFlbPeWkwISPeMmUmJN9/cjx+VOIujd8u4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=GRjemjTO; arc=none smtp.client-ip=209.85.216.71 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.b="GRjemjTO" Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-38e8e864ef0so2169737a91.0 for ; Fri, 18 Sep 2026 14:19:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789766391; x=1790371191; 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=/5xBOq/t+pD7MkHskvUYWbSjmSj1yyzqP7nUm5783BE=; b=GRjemjTO1G9Jtdw8NpXPAgHESLLS9s/lGKY7diF0kMXlCeOdUIA6kQIzg4CId/nRkl rEDPhdj+N1DTVLxAIB/uwT62+HkYV5dbv9ezLeYHkihTnE3XWnEXc1GCK6tUaK5Q63ET OP0NwhqFFR3eFOMcg1qDJJDdkGKgcWN+Fp9+hG/2yJg98g+Emp1rSGMS/X+77gm6+FVR WNUjT69tmmzlPZGBBppWETUFpvMLMbjKwyMY+fbmUaUQH9n2Lhyhi1vxjuaaA+C7/utK aqpZzWBHwQxroGuq8j2cLPUTAN3iBOoOW2l+594UNZsyX1a0k9faheNokTdwozxA9uY6 krKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789766391; x=1790371191; 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=/5xBOq/t+pD7MkHskvUYWbSjmSj1yyzqP7nUm5783BE=; b=MLeSyBagNffLsY3pKzgvDme4tdiwiNyKX6Du47EZSy5LVAqLssi4Rg5stnag+KYRa2 n92MOkiFdwCTqTb6Bm/vRQH2W6ZN4E6/1G0uqj/WSi5mPCD/BbZ6gQhYn0AB5AztX0Mt HTiFYBVmZk8bOflerJ7rVG5NOjJskpO0tTrLCFPzyc5EhoTBVdPeQMz5DCoKkh7t4Kcs KyiNpuafuuaIeE6T/GK7QRU0wH8mKbtYxeHFwkon72G4c3OeTG9ZX8QSKx/ijsq3BUEg AAn3VdKkQxVMqBqxbtRFaIWO9cbLgSKU/RUNhHNJTBF+xb+CPF2wP5+v7mUfF7D1+D7Y 550Q== X-Forwarded-Encrypted: i=1; AKwUvByQCHqOcgKLGv396LqjeOeISWN39MwocSPep+Irbv6w8gUW1KBfEQygJXd7mvCgC8v4Q0PkzkIR9KNFZMs=@vger.kernel.org X-Gm-Message-State: AFuF++lCIbM5fZ2GTG+Nmjn1V9MnRxGVRZmm1e70zUUo1C4U6NAZzywq FOjX8A4/z6kNEmZ680M7U/Bk8zRrl2EjDo2FRQOlM+znQx/xkh/cb42cILnrSQ6o2l4GRmC2juS rbqstgzamEw== X-Received: from dldyr1-n2.prod.google.com ([2002:a05:701b:4501:20b0:144:c737:6f14]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5305:b0:381:6c5:3f63 with SMTP id 98e67ed59e1d1-39e54dcd917mr13198461a91.6.1789766391234; Fri, 18 Sep 2026 14:19:51 -0700 (PDT) Date: Fri, 18 Sep 2026 14:19:17 -0700 In-Reply-To: <20260918211932.2966061-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918140659.2501976-1-irogers@google.com> <20260918211932.2966061-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918211932.2966061-4-irogers@google.com> Subject: [PATCH v4 03/18] perf evsel: Report an allocation failure as ENOMEM when setting filters From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org, Howard Chu Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" evsel__set_filter() and evsel__append_filter() return -1 when the strdup() or asprintf() that builds the new filter string fails, and evlist__set_tp_filter() and evlist__append_tp_filter() do the same when handed the NULL that asprintf__tp_filter_pids() returns for the same reason. In every case the only thing that can have gone wrong is an allocation. Callers all test the result with "< 0" or for being non-zero, so -1 has been as good as any other error so far, but it is not an errno and so cannot be printed as one. A caller that does, such as str_error_r(-err, errbuf, sizeof(errbuf)) turns it into EPERM and reports a failure to allocate as "Operation not permitted", which is no help to anyone trying to work out what happened. Return -ENOMEM instead, which is what these failures are. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/util/evlist.c | 12 ++++++++++-- tools/perf/util/evsel.c | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index c3d784727810..d403ab610bd3 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -1232,8 +1232,12 @@ int evlist__set_tp_filter(struct evlist *evlist, con= st char *filter) struct evsel *evsel; int err =3D 0; =20 + /* + * The only caller that passes NULL is evlist__set_tp_filter_pids(), + * where it means asprintf__tp_filter_pids() failed to allocate. + */ if (filter =3D=3D NULL) - return -1; + return -ENOMEM; =20 evlist__for_each_entry(evlist, evsel) { if (evsel->core.attr.type !=3D PERF_TYPE_TRACEPOINT) @@ -1252,8 +1256,12 @@ int evlist__append_tp_filter(struct evlist *evlist, = const char *filter) struct evsel *evsel; int err =3D 0; =20 + /* + * As above, a NULL filter is asprintf__tp_filter_pids() having failed + * to allocate in evlist__append_tp_filter_pids(). + */ if (filter =3D=3D NULL) - return -1; + return -ENOMEM; =20 evlist__for_each_entry(evlist, evsel) { if (evsel->core.attr.type !=3D PERF_TYPE_TRACEPOINT) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index c663aafa88b2..6836156c2a7d 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1872,7 +1872,7 @@ int evsel__set_filter(struct evsel *evsel, const char= *filter) return 0; } =20 - return -1; + return -ENOMEM; } =20 static int evsel__append_filter(struct evsel *evsel, const char *fmt, cons= t char *filter) @@ -1888,7 +1888,7 @@ static int evsel__append_filter(struct evsel *evsel, = const char *fmt, const char return 0; } =20 - return -1; + return -ENOMEM; } =20 int evsel__append_tp_filter(struct evsel *evsel, const char *filter) --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Thu Sep 24 22:56:31 2026 Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DD99C52B1FD for ; Fri, 18 Sep 2026 21:19:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.198 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766396; cv=none; b=A0QK4sBpdk26ui1BGwRSFmGXlFekXiw5T6jJJ5l6f+SqtpuDrSUV7GWR6bpZd/dpw2DJcfL789wgbVxp3D9xxklzhFanEuogKW3532XQvRWVGSQOJ3UGO3OlOW+wHU/MF/ofUTSsEmN3jq2fpS5f2iqf82aTGLXHo1Nsz2eAPsI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766396; c=relaxed/simple; bh=9No6g42lrB6QKcCBOOEFSgMqI1po/ZrRv9DIdc2oFZ8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=T9aGJZ49iRd/srgIguis7EUoQSnAA4CLwssnnXmeKIenO1zDXRIH0K3LL8kUBYavCGh9Hp+DBsRrTWJErFft47GF1HKEbr676fFut/yN9ViCp773KgMLgMmnndbtE4EEkPd0CkiNXsxNOuA44uDh4f6ithFzBQhzU0ST9VLb0uA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=hZ2T/RJh; arc=none smtp.client-ip=209.85.214.198 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.b="hZ2T/RJh" Received: by mail-pl1-f198.google.com with SMTP id d9443c01a7336-2dc92350888so19087365ad.3 for ; Fri, 18 Sep 2026 14:19:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789766394; x=1790371194; 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=dWUdAqwnRNViqY1JbSim/N3szd89pQRjSVQzSYadKOc=; b=hZ2T/RJhWLGJxCF7SzGOO3lOD7TNIrABQojCxYpGfRTbcwqxsYGQ75UrHEiPp/3GEE 34UmapBC1DzxyKaSZyTunB7cCsNEmINJUqiliCKA/Du1f+v/5lnLvBg3uIK+i0B5DkgO 00U3rrbk8jRWHyuPErzY6GICH+WARln6/e/Itq1O8U7SlrvX+fkJrpwSa/3Maq1BiPLG q033YPi4LPPw9uEDCM/vuoldPihpAn1INAUHKGi5GPmNhKql2KNAJUoh2CiYWIIL5QsW nFSHHyEDjAPBWpdZRQrfXJN7JxtZw4kwvXiiG5VEXue41F+2iI76cUCHwgDEUS4w2M74 tWnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789766394; x=1790371194; 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=dWUdAqwnRNViqY1JbSim/N3szd89pQRjSVQzSYadKOc=; b=UslcTNBmvvX+fcgXwXcoKAW22BTCosu5YBhZnu3t0TPbZFFYIMNuVwaqA1Bg7j3Uu5 BTK+zoTDc42iJuBdqxVggPifXq1+T6rZ/sennTWOvkodeIWIo/uVk2D/C9ViHqz6j2UP qxcETktauwYgp0u0CgO7gHHOKbmeQsLHWy9468LMqTyj7B00wq8WdO69l1jGsaBwgdsE sKuYDeb+h2UnVE9hEqP+yAKNzV8agnzOJFPb+As4b0Dfl1XOdhDI7MliM8s3dZeouPiK csJEh1qcQX1JOLsIa7SkG5xVkXuAgE5RsRQxm7g68kx9/27Q+gmcaz57zAGBpClBi+ef VOVg== X-Forwarded-Encrypted: i=1; AKwUvBy46x0UX/22nFgRrYC+aG4Hd/rRbO+dcM3odUCu7yygsv1vHqEBUIfVANJQHdV4fPHt8o3ByHtHVQIJhiA=@vger.kernel.org X-Gm-Message-State: AFuF++l1hZV+/xdLM3kd7xAo1rTus3AUjH9CNL1JrzawRONkklQ1wo2B 1BagQFACFZlbUfnxSkNcC5PRM7zjMPI8jv1LsQb96O6/1e6tKzIythkZaBb0j5DGR40JVlwXa6H IXX7W6IHh0A== X-Received: from dyu6.prod.google.com ([2002:a05:693c:8106:b0:32b:7182:c3b7]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2f0f:b0:2dd:88b4:6920 with SMTP id d9443c01a7336-2ddb1b9818dmr77699965ad.22.1789766394028; Fri, 18 Sep 2026 14:19:54 -0700 (PDT) Date: Fri, 18 Sep 2026 14:19:18 -0700 In-Reply-To: <20260918211932.2966061-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918140659.2501976-1-irogers@google.com> <20260918211932.2966061-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918211932.2966061-5-irogers@google.com> Subject: [PATCH v4 04/18] perf trace: Start BPF summary before starting workload From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org, Howard Chu Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When using --bpf-summary, trace_start_bpf_summary() sets skel->bss->enabled =3D 1. In trace__run(), trace_start_bpf_summary() was previously invoked after evlist__start_workload(). Because evlist__start_workload() immediately unblocks the child process by writing to its go_pipe, short-lived workloads (such as `cat /dev/null`) can execute and complete their initial system calls before trace_start_bpf_summary() is reached by the parent process. Furthermore, under high system load, the child process may finish before the BPF summary tracking is enabled in the kernel at all, causing syscall summary tests to fail. Additionally, if initial_delay was configured, the workload was started before sleeping. Move trace_start_bpf_summary() to be invoked before evlist__start_workload(), matching evlist__enable(), and ensure it respects target.initial_delay. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/builtin-trace.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index f67557e7a254..f90c6bb4d8b4 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -4844,17 +4844,19 @@ static int trace__run(struct trace *trace, int argc= , const char **argv) if (!target__none(&trace->opts.target) && !trace->opts.target.initial_del= ay) evlist__enable(evlist); =20 + if (trace->summary_bpf && !trace->opts.target.initial_delay) + trace_start_bpf_summary(); + if (forks) evlist__start_workload(evlist); =20 if (trace->opts.target.initial_delay) { usleep(trace->opts.target.initial_delay * 1000); evlist__enable(evlist); + if (trace->summary_bpf) + trace_start_bpf_summary(); } =20 - if (trace->summary_bpf) - trace_start_bpf_summary(); - trace->multiple_threads =3D perf_thread_map__pid(evlist__core(evlist)->th= reads, 0) =3D=3D -1 || perf_thread_map__nr(evlist__core(evlist)->threads) > 1 || evlist__first(evlist)->core.attr.inherit; --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Thu Sep 24 22:56:31 2026 Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7FFF152BE5F for ; Fri, 18 Sep 2026 21:19:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.69 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766399; cv=none; b=mPhj9UBJhDD7asmnwh+nJkrMxLYjagKZ2egGUfEx0i5cvA3IfGU/wWlOq7StiP+et7C3fmNnoN+bScr4m9TTREnrL8SEtXE2uRmZWm3eQZ8jva9lfcSvX6QSiQMdZu07dJZqgXbziVPjzjgUHQuMQTqPZRcvcJKT4srtzvRyDtk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766399; c=relaxed/simple; bh=LJ8nYSQqq28gI2I051DArOUVp3BmzcpKdNOfUaM0siM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Fcu6bTVDSXlS0PABQMnSAOcElWI+v11h4kMyUIH+LsvA9NE7UZJbUg+Znf3/KBzGTpRgUSfOUS9DQdtk6kvZEMlxVEfvsHQnJqkqbvOoH1f4rq3dryJShXTGu8BTKkXXNTDJaaGWx3TlGUwZE8HFymuz+rnQecmlUiczaTrp4q0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=QXbVXr0E; arc=none smtp.client-ip=209.85.216.69 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.b="QXbVXr0E" Received: by mail-pj1-f69.google.com with SMTP id 98e67ed59e1d1-38f97b3f853so2001647a91.3 for ; Fri, 18 Sep 2026 14:19:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789766397; x=1790371197; 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=+5W8G3HjPfwr7Y9dW0STP1ylXOPLDoEE8Bbh/qLvuqY=; b=QXbVXr0ExTiVF3vFpJZQzNK6AZ5lC80ry3OfwLMkIxGpDgAzNBW/6rmmNV7XaBoK0c ugvqBYoRKCShByVh8zeezhKEzin83wLZmoZYOJ9g69Lp43lVtOrTw3IjveYmT2jmE48/ iOafp0IkjqBIaWRNZCgzHai1HGPJ+dFiFq/Z+Fh68igdzWIeWVCRKX150zwve5bNWoAo oBKFClj0c9qi0Z487E+Osz5VT+pfAmYb9e4z22/xr45PAqR+MZTWH/PlzoUjPjr519sc kUMXCRViAFeRJGgrMf0TLPKonaSHjC/jNI3jArukN1Hc09fWD53Xjg4vMjYosw3p2Yws QxGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789766397; x=1790371197; 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=+5W8G3HjPfwr7Y9dW0STP1ylXOPLDoEE8Bbh/qLvuqY=; b=1n/7t0Al6w2HSVItCQOOBfxctBpgFR3f3u3xkANqnkrs4JnSBqVHweJwANqjFiXC5L SDYFDpKCPNkrOaVAI6plzYKPtAjEWHehiB2v/1SoVMLnEsp6YHynW7LD68FIRB85bsVY F1NBQCaUW4tUiJTkHqaEfP6ljACnKj3O++leoucJ+YHt1Epr6S8sRgFGWXza0YyOCtN2 3iBEvQdKteNlDsnr3vbo8TQLq84KxD7bTsYbW8lkcJSOhx9cCKxQ5IaunA4SeY6f5Hjn ELhyQQY+JqieDnIat3tEvWS8pPuk4NAIEysE94UUSOje4l34MOWCSTMednz46d11yl84 CHtQ== X-Forwarded-Encrypted: i=1; AKwUvBxqqXVVfl1yN4QhIJwGPgpnvzc0d3DC1g5kUZ+7muHWybrw1imDhqXWAlHNAddHDBQ7x2AXWyeZ1gDblQ8=@vger.kernel.org X-Gm-Message-State: AFuF++lqL4VVaFCWRlGa7jKDGkI0nEdJIRA1+H1GHdXeIHYka4Lw4+C+ FoqiJ6lPSsL4tbj5dfvYbUI0v9GhgdSK+FGpdP+ZRIeEPezRX94IPyoha/GFwig+HkDikBPYT65 CDN6YXMHT0Q== X-Received: from dlbbu27.prod.google.com ([2002:a05:7022:221b:b0:144:c088:eba]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:164c:b0:39e:6a80:b79a with SMTP id 98e67ed59e1d1-39e6a80b880mr1753886a91.42.1789766396622; Fri, 18 Sep 2026 14:19:56 -0700 (PDT) Date: Fri, 18 Sep 2026 14:19:19 -0700 In-Reply-To: <20260918211932.2966061-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918140659.2501976-1-irogers@google.com> <20260918211932.2966061-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918211932.2966061-6-irogers@google.com> Subject: [PATCH v4 05/18] perf trace: Skip internal tracepoint fields in formatting and beauty map From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org, Howard Chu Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Linux 6.19+ added __data_loc char[] internal fields for string arguments in syscalls:sys_enter_ tracepoints (e.g., __data_loc_oldname in sys_enter_renameat2). While is_internal_field() was added to detect them, several places did not properly account for them: 1. In syscall_arg_fmt__init_array(), when an internal field was skipped, the arg pointer was still incremented, causing the subsequent argument formatters to be mismatched. 2. In syscall__scnprintf_args(), internal fields were not skipped, causing spurious trailing arguments like ", 0, 16" to be formatted and printed. 3. In trace__bpf_sys_enter_beauty_map(), internal fields were not skipped, offsetting beauty array argument indices and breaking string and buffer augmentation. 4. In trace__find_usable_bpf_prog_entry(), candidate pointer checks matched on internal pointer fields, breaking signature compatibility matching between syscalls for augmenter sharing. Introduce next_user_arg() and advance both cursors with it, so that the two argument lists are always compared at a real argument and the walk ends when one syscall runs out of arguments rather than when one happens to have trailing internal fields. 5. syscall__augmented_args() computed the augmented payload as sample->raw_size - sc->args_size for any sys_enter style sample. sc->args_size deliberately stops at the last non-internal field, so on 6.19+ a native syscalls:sys_enter_ record leaves the __data_loc words and their string payloads in the remainder. Those bytes are not a struct augmented_arg, so syscall_arg__scnprintf_augmented_string() read a bogus length and walked arg->augmented.args out of bounds. This is reachable from trace__event_handler(), which calls trace__fprintf_sys_enter() for any evsel whose tracepoint name starts with "sys_enter_". 6. In syscall__read_info(), syscall__alloc_arg_fmts() was called before checking and dropping the leading __syscall_nr (or nr) field, using nr_fields - 1 unconditionally. If a tracepoint format lacks that leading field, the allocated arg_fmt array is one entry too small and syscall_arg_fmt__init_array() writes one entry past the end of the heap buffer. Drop __syscall_nr/nr first and size the allocation from the remaining fields. Update these functions to check and skip is_internal_field() so that arguments are correctly formatted and beauty map entries match the expected syscall signatures, restrict syscall__augmented_args() to the __augmented_syscalls__ bpf-output evsel, and size arg_fmt after dropping the syscall number field. Publish sc->args only once that allocation has succeeded. sc->name is set earlier in syscall__read_info(), and a later call takes a syscall with a name to have been read already and returns it as it stands, so a syscall left with arguments and no arg_fmt to describe them would be printed by walking the arguments and indexing an array that was never allocated. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/builtin-trace.c | 171 ++++++++++++++++++++++++++++--------- 1 file changed, 129 insertions(+), 42 deletions(-) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index f90c6bb4d8b4..de3108dcef8a 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -2283,15 +2283,20 @@ syscall_arg_fmt__init_array(struct syscall_arg_fmt = *arg, struct tep_format_field struct tep_format_field *last_field =3D NULL; int len; =20 - for (; field; field =3D field->next, ++arg) { - /* assume it's the last argument */ + for (; field; field =3D field->next) { + /* + * Skip internal tracepoint fields (e.g., __data_loc strings in + * Linux 6.19+) so they do not advance the syscall arg array index. + */ if (is_internal_field(field)) continue; =20 last_field =3D field; =20 - if (arg->scnprintf) + if (arg->scnprintf) { + ++arg; continue; + } =20 len =3D strlen(field->name); =20 @@ -2348,6 +2353,7 @@ syscall_arg_fmt__init_array(struct syscall_arg_fmt *a= rg, struct tep_format_field } } } + ++arg; } =20 return last_field; @@ -2368,7 +2374,8 @@ static int syscall__read_info(struct syscall *sc, str= uct trace *trace) { char tp_name[128]; const char *name; - struct tep_format_field *field; + struct tep_format_field *args, *field; + int nr_args; int err; =20 if (sc->nonexistent) @@ -2407,24 +2414,35 @@ static int syscall__read_info(struct syscall *sc, s= truct trace *trace) return err; } =20 - /* - * The tracepoint format contains __syscall_nr field, so it's one more - * than the actual number of syscall arguments. - */ - if (syscall__alloc_arg_fmts(sc, sc->tp_format->format.nr_fields - 1)) - return -ENOMEM; - - sc->args =3D sc->tp_format->format.fields; + args =3D sc->tp_format->format.fields; + nr_args =3D sc->tp_format->format.nr_fields; /* * We need to check and discard the first variable '__syscall_nr' * or 'nr' that mean the syscall number. It is needless here. * So drop '__syscall_nr' or 'nr' field but does not exist on older kerne= ls. + * + * Do this before allocating, and size the array from what is left, so + * that a format without the field does not leave + * syscall_arg_fmt__init_array() walking one entry past the end. */ - if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->a= rgs->name, "nr"))) { - sc->args =3D sc->args->next; - --sc->nr_args; + if (args && (!strcmp(args->name, "__syscall_nr") || !strcmp(args->name, "= nr"))) { + args =3D args->next; + --nr_args; } =20 + if (syscall__alloc_arg_fmts(sc, nr_args)) + return -ENOMEM; + + /* + * Only now that there is an arg_fmt for each of them are the arguments + * published. sc->name was set above, so a later syscall__read_info() + * takes this syscall to be read already and returns it as it stands; + * were sc->args set with sc->arg_fmt still NULL, the printing of that + * syscall would walk the arguments and index an array that does not + * exist. + */ + sc->args =3D args; + field =3D sc->args; while (field) { if (is_internal_field(field)) @@ -2642,11 +2660,17 @@ static size_t syscall__scnprintf_args(struct syscal= l *sc, char *bf, size_t size, if (sc->args !=3D NULL) { struct tep_format_field *field; =20 - for (field =3D sc->args; field; - field =3D field->next, ++arg.idx, bit <<=3D 1) { - if (arg.mask & bit) + for (field =3D sc->args; field; field =3D field->next) { + /* Skip internal fields so they are not printed as spurious arguments */ + if (is_internal_field(field)) continue; =20 + if (arg.mask & bit) { + ++arg.idx; + bit <<=3D 1; + continue; + } + arg.fmt =3D &sc->arg_fmt[arg.idx]; val =3D syscall_arg__val(&arg, arg.idx); /* @@ -2664,8 +2688,11 @@ static size_t syscall__scnprintf_args(struct syscall= *sc, char *bf, size_t size, */ if (val =3D=3D 0 && !trace->show_zeros && !(sc->arg_fmt && sc->arg_fmt[arg.idx].show_zero) && - !(sc->arg_fmt && sc->arg_fmt[arg.idx].strtoul =3D=3D STUL_BTF_TYPE)) + !(sc->arg_fmt && sc->arg_fmt[arg.idx].strtoul =3D=3D STUL_BTF_TYPE)= ) { + ++arg.idx; + bit <<=3D 1; continue; + } =20 printed +=3D scnprintf(bf + printed, size - printed, "%s", printed ? ",= " : ""); =20 @@ -2680,12 +2707,16 @@ static size_t syscall__scnprintf_args(struct syscal= l *sc, char *bf, size_t size, size - printed, val, field->type); if (btf_printed) { printed +=3D btf_printed; + ++arg.idx; + bit <<=3D 1; continue; } } =20 printed +=3D syscall_arg_fmt__scnprintf_val(&sc->arg_fmt[arg.idx], bf + printed, size - printed, &arg, val); + ++arg.idx; + bit <<=3D 1; } } else if (IS_ERR(sc->tp_format)) { /* @@ -2946,7 +2977,9 @@ static int trace__fprintf_sample(struct trace *trace,= struct perf_sample *sample return printed; } =20 -static void *syscall__augmented_args(struct syscall *sc, struct perf_sampl= e *sample, int *augmented_args_size, int raw_augmented_args_size) +static void *syscall__augmented_args(struct trace *trace, struct syscall *= sc, + struct perf_sample *sample, + int *augmented_args_size, int raw_augmented_args_size) { /* * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter @@ -2964,6 +2997,24 @@ static void *syscall__augmented_args(struct syscall = *sc, struct perf_sample *sam */ int args_size =3D raw_augmented_args_size ?: sc->args_size; =20 + /* + * Augmented arguments are a perf trace specific payload, they are only + * ever appended to samples emitted by the BPF __augmented_syscalls__ + * bpf-output event. + * + * Native syscalls:sys_enter_NAME tracepoints may also carry trailing + * data of their own: since Linux 6.19 they append __data_loc char[] + * fields plus the string payloads they point at. Those bytes are not a + * struct augmented_arg, so treating them as one would make + * syscall_arg__scnprintf_augmented_string() read a bogus length and + * walk arg->augmented.args far out of bounds. + * + * So only look for augmented arguments on the event that can actually + * produce them. + */ + if (sample->evsel !=3D trace->syscalls.events.bpf_output) + return NULL; + *augmented_args_size =3D sample->raw_size - args_size; if (*augmented_args_size > 0) { static uintptr_t argbuf[1024]; /* assuming single-threaded */ @@ -3022,17 +3073,13 @@ static int trace__sys_enter(struct trace *trace, if (!(trace->duration_filter || trace->summary_only || trace->min_stack)) trace__printf_interrupted_entry(trace); /* - * If this is raw_syscalls.sys_enter, then it always comes with the 6 pos= sible - * arguments, even if the syscall being handled, say "openat", uses only = 4 arguments - * this breaks syscall__augmented_args() check for augmented args, as we = calculate - * syscall->args_size using each syscalls:sys_enter_NAME tracefs format f= ile, - * so when handling, say the openat syscall, we end up getting 6 args for= the - * raw_syscalls:sys_enter event, when we expected just 4, we end up mista= kenly - * thinking that the extra 2 u64 args are the augmented filename, so just= check - * here and avoid using augmented syscalls when the evsel is the raw_sysc= alls one. + * syscall__augmented_args() only returns a payload for the BPF + * __augmented_syscalls__ event, so raw_syscalls:sys_enter (which always + * carries all 6 possible arguments rather than sc->args_size worth) and + * the native syscalls:sys_enter_NAME tracepoints are both handled there. */ - if (evsel !=3D trace->syscalls.events.sys_enter) - augmented_args =3D syscall__augmented_args(sc, sample, &augmented_args_s= ize, trace->raw_augmented_syscalls_args_size); + augmented_args =3D syscall__augmented_args(trace, sc, sample, &augmented_= args_size, + trace->raw_augmented_syscalls_args_size); ttrace->entry_time =3D sample->time; ttrace->entry_cpu =3D sample->cpu; msg =3D ttrace->entry_str; @@ -3077,7 +3124,7 @@ static int trace__fprintf_sys_enter(struct trace *tra= ce, struct perf_sample *sam struct syscall *sc; char msg[1024]; void *args, *augmented_args =3D NULL; - int augmented_args_size, e_machine; + int augmented_args_size =3D 0, e_machine; size_t printed =3D 0; =20 =20 @@ -3095,7 +3142,8 @@ static int trace__fprintf_sys_enter(struct trace *tra= ce, struct perf_sample *sam goto out_put; =20 args =3D perf_evsel__sc_tp_ptr(args, sample); - augmented_args =3D syscall__augmented_args(sc, sample, &augmented_args_si= ze, trace->raw_augmented_syscalls_args_size); + augmented_args =3D syscall__augmented_args(trace, sc, sample, &augmented_= args_size, + trace->raw_augmented_syscalls_args_size); printed +=3D syscall__scnprintf_args(sc, msg, sizeof(msg), args, augmente= d_args, augmented_args_size, trace, thread); fprintf(trace->output, "%.*s", (int)printed, msg); err =3D 0; @@ -4127,10 +4175,16 @@ static int trace__bpf_sys_enter_beauty_map(struct t= race *trace, int e_machine, i if (trace->btf =3D=3D NULL) return -1; =20 - for (i =3D 0, field =3D sc->args; field; ++i, field =3D field->next) { + for (i =3D 0, field =3D sc->args; field; field =3D field->next) { + /* Skip internal fields to keep beauty array index aligned with syscall = arguments */ + if (is_internal_field(field)) + continue; + // XXX We're only collecting pointer payloads _from_ user space - if (!sc->arg_fmt[i].from_user) + if (!sc->arg_fmt[i].from_user) { + ++i; continue; + } =20 struct_offset =3D strstr(field->type, "struct "); if (struct_offset =3D=3D NULL) @@ -4149,8 +4203,10 @@ static int trace__bpf_sys_enter_beauty_map(struct tr= ace *trace, int e_machine, i name[cnt] =3D '\0'; =20 /* cache struct's btf_type and type_id */ - if (syscall_arg_fmt__cache_btf_struct(&sc->arg_fmt[i], trace->btf, name= )) + if (syscall_arg_fmt__cache_btf_struct(&sc->arg_fmt[i], trace->btf, name= )) { + ++i; continue; + } =20 bt =3D sc->arg_fmt[i].type; beauty_array[i] =3D bt->size; @@ -4176,7 +4232,9 @@ static int trace__bpf_sys_enter_beauty_map(struct tra= ce *trace, int e_machine, i struct tep_format_field *field_tmp; =20 /* find the size of the buffer that appears in pairs with buf */ - for (j =3D 0, field_tmp =3D sc->args; field_tmp; ++j, field_tmp =3D fie= ld_tmp->next) { + for (j =3D 0, field_tmp =3D sc->args; field_tmp; field_tmp =3D field_tm= p->next) { + if (is_internal_field(field_tmp)) + continue; if (!(field_tmp->flags & TEP_FIELD_IS_POINTER) && /* only integers */ (strstr(field_tmp->name, "count") || strstr(field_tmp->name, "siz") || /* size, bufsiz */ @@ -4186,8 +4244,10 @@ static int trace__bpf_sys_enter_beauty_map(struct tr= ace *trace, int e_machine, i can_augment =3D true; break; } + ++j; } } + ++i; } =20 if (can_augment) @@ -4196,6 +4256,19 @@ static int trace__bpf_sys_enter_beauty_map(struct tr= ace *trace, int e_machine, i return -1; } =20 +/* + * Advance to the first field that is a real syscall argument, so that cal= lers + * walking two argument lists in step never have to reason about internal + * fields appearing in one list but not the other. + */ +static struct tep_format_field *next_user_arg(struct tep_format_field *fie= ld) +{ + while (field && is_internal_field(field)) + field =3D field->next; + + return field; +} + static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace = *trace, struct syscall *sc) { @@ -4203,7 +4276,7 @@ static struct bpf_program *trace__find_usable_bpf_pro= g_entry(struct trace *trace /* * We're only interested in syscalls that have a pointer: */ - for (field =3D sc->args; field; field =3D field->next) { + for (field =3D next_user_arg(sc->args); field; field =3D next_user_arg(fi= eld->next)) { if (field->flags & TEP_FIELD_IS_POINTER) goto try_to_find_pair; } @@ -4221,21 +4294,31 @@ static struct bpf_program *trace__find_usable_bpf_p= rog_entry(struct trace *trace pair->bpf_prog.sys_enter =3D=3D unaugmented_prog) continue; =20 - for (field =3D sc->args, candidate_field =3D pair->args; - field && candidate_field; field =3D field->next, candidate_field = =3D candidate_field->next) { + /* + * Both cursors only ever point at real arguments, so the loop + * ends when one of the two syscalls runs out of them, rather + * than when one happens to have trailing internal fields. + */ + field =3D next_user_arg(sc->args); + candidate_field =3D next_user_arg(pair->args); + while (field && candidate_field) { bool is_pointer =3D field->flags & TEP_FIELD_IS_POINTER, candidate_is_pointer =3D candidate_field->flags & TEP_FIELD_IS_POI= NTER; =20 if (is_pointer) { - if (!candidate_is_pointer) { + if (!candidate_is_pointer) { // The candidate just doesn't copies our pointer arg, might copy othe= r pointers we want. + field =3D next_user_arg(field->next); + candidate_field =3D next_user_arg(candidate_field->next); continue; - } + } } else { if (candidate_is_pointer) { // The candidate might copy a pointer we don't have, skip it. goto next_candidate; } + field =3D next_user_arg(field->next); + candidate_field =3D next_user_arg(candidate_field->next); continue; } =20 @@ -4256,6 +4339,8 @@ static struct bpf_program *trace__find_usable_bpf_pro= g_entry(struct trace *trace goto next_candidate; =20 is_candidate =3D true; + field =3D next_user_arg(field->next); + candidate_field =3D next_user_arg(candidate_field->next); } =20 if (!is_candidate) @@ -4267,7 +4352,9 @@ static struct bpf_program *trace__find_usable_bpf_pro= g_entry(struct trace *trace * more than what is common to the two syscalls. */ if (candidate_field) { - for (candidate_field =3D candidate_field->next; candidate_field; candid= ate_field =3D candidate_field->next) + candidate_field =3D next_user_arg(candidate_field->next); + for (; candidate_field; + candidate_field =3D next_user_arg(candidate_field->next)) if (candidate_field->flags & TEP_FIELD_IS_POINTER) goto next_candidate; } --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Thu Sep 24 22:56:31 2026 Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0FF80479890 for ; Fri, 18 Sep 2026 21:19:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.70 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766401; cv=none; b=G3nzurLys7JiPc/UnblxdcLGKYFbbwTI4x0AOaf4zEW9xLDe9kUbDwQ1xjJC2Hlue99KRVKKraoR2GxvZFEZtndH4EqvOFXH62kFMTfWMQzBaIo9ISt9sGTxWaHmS4KU+bERVpC3J1wgOA00LtaRPNYquKtl4BdaohEPp8md+P0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766401; c=relaxed/simple; bh=wTN188sd46YvyIQC7dEL12UIN+qtz6SqD1SpbXW7xD0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gMS5T/fRrTn4aaXL87bpVgfQ+pQ4pLkWfQZiID4IOxMzSoyR3hJ6UonXyd5BBgkVYlzFLveVCH525WzL5S2ZY2ke4mjEscpQhnevslfYCnLxYaqiNjtRQ0v7nb6kGtedA8pIqyC9LiI6tfLnAwu2Y2qwA4xKsBBZmgO0zDIH9es= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=W21C614h; arc=none smtp.client-ip=209.85.216.70 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.b="W21C614h" Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-396901263b6so2128695a91.2 for ; Fri, 18 Sep 2026 14:19:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789766399; x=1790371199; 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=ZCuQJlBx4iOYW61TQncdFtiIp5eHB2qyRCxal5DDox4=; b=W21C614h0DODeJh0XpD+LMEo6pbER//NfVh2anSOW0lTJFajWRVtVtHanShOPWu/gO 4vdNCYhOIBW8lOTrmffD3sgayM9AGlCg+p66CGW4otPJfqGkwC6dluKYEbDiLTeqcICH 3OfqNqdqXTCQOAE7e0XI9omKOP79P9g7rmHX9b8vKLfLfBSBjO6WNc5CTxXzc5Jj6Zzt c//F/co/YQMZqymAIp9skjFy64OAKO+IvQRmacORNIwZ8oqUc7evp/vbKEl6fkd4fWf3 my7n3zGmOipm7ezcueRtEjynFo5evzX0FP0lWSutP7Morg0SaS1KknyHUy+bBlfUBSdc S4eA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789766399; x=1790371199; 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=ZCuQJlBx4iOYW61TQncdFtiIp5eHB2qyRCxal5DDox4=; b=J2kU3dyEdeYORDasdWmC4bWxDM3mAgAQfCiWk38jXvIKm+G41vsS9IZI/Rgdprp8U2 oKH/Oyqs39EP6l0MHKYUMxKhqU0XYahj6pHW9TLM8dZ1uTqRZT+0LjSe1fGZqBZB7SHT IyE2J1sOvy45WWM7iNjzLzcxCDX6QPCYX8glMx94+3PNqHZPtMrmEQnn2Cd6g2Kvf2Ce 6enARDgFIgQfAb2COyj7OVZqll7zFso/qyoscQN6D5+F++Zan8yGTI5gfMvblmNBpqyE xsimIvjpOIbmGU4IbUU//xYCCYxpNRhrQWs6AhuWiCx2aVuyF/rbve4sg+DQQCJVkiZe UtTQ== X-Forwarded-Encrypted: i=1; AKwUvBywbkgNFBCo/nThAh72LtlNbeXi/J/TEWwiczDrG9rGELSGhZPUAhVpOoqJMIBHQwX0p26EQ0VFS4S6PYs=@vger.kernel.org X-Gm-Message-State: AFuF++moVH9wxfLtIx4ATzKMQudk/quHLDliL8iy8aXXhCW1uh/nmRfR YNumJkyc8vK3suU5PXfUIDe0984rCE0t9ZuU9OT2ksd55An4ZxLaOz/6j4IMrxlJze1oiif6T3B bAWj/gC7eHA== X-Received: from dled9-n1.prod.google.com ([2002:a05:701b:42c9:10b0:143:8a67:84b5]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:57e8:b0:39e:6c69:7774 with SMTP id 98e67ed59e1d1-39e6c697961mr2142236a91.29.1789766399150; Fri, 18 Sep 2026 14:19:59 -0700 (PDT) Date: Fri, 18 Sep 2026 14:19:20 -0700 In-Reply-To: <20260918211932.2966061-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918140659.2501976-1-irogers@google.com> <20260918211932.2966061-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918211932.2966061-7-irogers@google.com> Subject: [PATCH v4 06/18] perf trace: Do not set unaugmented BPF program on sys_exit map From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org, Howard Chu Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In trace__init_syscalls_bpf_prog_array_maps(), the BPF program array map for sys_exit (syscalls_sys_exit) was populated with the result of trace__bpf_prog_sys_exit_fd(). When a syscall had no specific exit augmenter, trace__find_syscall_bpf_prog() fell back to unaugmented_prog (syscall_unaugmented). However, syscall_unaugmented is a sys_enter program that outputs enter arguments to __augmented_syscalls__. As a consequence, when an unaugmented syscall exited, sys_exit tail-called syscall_unaugmented, which interpreted the exit arguments as enter arguments and emitted a duplicate, corrupt sys_enter event into __augmented_syscalls__ right as the syscall completed. Fix this by: 1. Returning NULL from trace__find_syscall_bpf_prog() when looking up exit augmenters and none is found. 2. Returning -1 from trace__bpf_prog_sys_exit_fd() when no exit program is present. 3. Only updating map_exit_fd when prog_fd >=3D 0. 4. Clearing err =3D 0 when trace__bpf_sys_enter_beauty_map() returns non-zero (indicating the syscall has no augmentable pointer arguments) before continuing the loop, so a trailing run of such syscalls (e.g. 'perf trace -e close') does not leave err non-zero on return and abort the session. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/builtin-trace.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index de3108dcef8a..f6fb6ffd654a 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -4133,7 +4133,12 @@ static struct bpf_program *trace__find_syscall_bpf_p= rog(struct trace *trace __ma pr_debug("Couldn't find BPF prog \"%s\" to associate with syscalls:sys_%s= _%s, not augmenting it\n", prog_name, type, sc->name); out_unaugmented: - return unaugmented_prog; + /* + * Do not set unaugmented_prog for exit: syscall_unaugmented is a + * sys_enter program that outputs enter arguments. Exit without a + * specialized return augmenter returns 1 directly from sys_exit. + */ + return !strcmp(type, "exit") ? NULL : unaugmented_prog; } =20 static void trace__init_syscall_bpf_progs(struct trace *trace, int e_machi= ne, int id) @@ -4156,7 +4161,7 @@ static int trace__bpf_prog_sys_enter_fd(struct trace = *trace, int e_machine, int static int trace__bpf_prog_sys_exit_fd(struct trace *trace, int e_machine,= int id) { struct syscall *sc =3D trace__syscall_info(trace, NULL, e_machine, id); - return sc ? bpf_program__fd(sc->bpf_prog.sys_exit) : bpf_program__fd(unau= gmented_prog); + return sc && sc->bpf_prog.sys_exit ? bpf_program__fd(sc->bpf_prog.sys_exi= t) : -1; } =20 static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_mach= ine, int key, unsigned int *beauty_array) @@ -4411,16 +4416,27 @@ static int trace__init_syscalls_bpf_prog_array_maps= (struct trace *trace, int e_m err =3D bpf_map_update_elem(map_enter_fd, &key, &prog_fd, BPF_ANY); if (err) break; + /* Only update the exit prog array map if an exit augmenter exists */ prog_fd =3D trace__bpf_prog_sys_exit_fd(trace, e_machine, key); - err =3D bpf_map_update_elem(map_exit_fd, &key, &prog_fd, BPF_ANY); - if (err) - break; + if (prog_fd >=3D 0) { + err =3D bpf_map_update_elem(map_exit_fd, &key, &prog_fd, BPF_ANY); + if (err) + break; + } =20 /* use beauty_map to tell BPF how many bytes to collect, set beauty_map'= s value here */ memset(beauty_array, 0, sizeof(beauty_array)); err =3D trace__bpf_sys_enter_beauty_map(trace, e_machine, key, (unsigned= int *)beauty_array); - if (err) + if (err) { + /* + * Not a failure: the syscall just has no augmentable + * arguments. Clear err, or a trailing run of such + * syscalls, e.g. all of them for 'perf trace -e close', + * would leave it set on return and abort the session. + */ + err =3D 0; continue; + } err =3D bpf_map_update_elem(beauty_map_fd, &key, beauty_array, BPF_ANY); if (err) break; --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Thu Sep 24 22:56:31 2026 Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B2F4552CCE9 for ; Fri, 18 Sep 2026 21:20:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.197 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766406; cv=none; b=GsSWjH3NTRl4R6iszyNGEENlR7A4ovtiWnZpukolNWVDDSXTdNs1lYh2BNP/uH0QHYlxsmGmDWulEPucO98ed+/3uJZnlMq0Me4ADQa+kaeynLcFkiv/FvRaq9m8pGwxY0DQ+zBAzasbPukekwVm62SKbjSGD33WpgxeDAa3S+8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766406; c=relaxed/simple; bh=YoLjGit6K0MeeDD7LUqu8ZSYIR4l8GnMUpD8HbUxiJk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=guzwjYrcfgkY/9e+kiDi3jeILDex8TX3SfVQNFHzTa0+k4QDHoB8yv+iVdwhsEZmXAYfjfvbKoU5pNiED5r1BiE7OMUIY0yj27siPm1eN2ZbOd3YBtLgnmZIou+1mZ47/5k2loXIpKG00szPMRtBXU/YcwBIRZFnCKQT1HBNYVg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=TpbB8Tt8; arc=none smtp.client-ip=209.85.215.197 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.b="TpbB8Tt8" Received: by mail-pg1-f197.google.com with SMTP id 41be03b00d2f7-cbedbd182f5so1076398a12.1 for ; Fri, 18 Sep 2026 14:20:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789766402; x=1790371202; 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=104X5wo7yQkILIFw+J6eQp79zBLiDOMXd7ygwwepPVY=; b=TpbB8Tt8bA+ehwhUMr4J26OybrVGdgyz3Ci7KUl1w0pS9gTdUU8RGJdHkdOhSjDt1O xDspHqg1jTPWBeGaj8HPp39dLYfGGNCKR5TjJrVxMc7VYMtN+IlCWNPWqMlseFUpNCtj F29NVjKi9vrbRfSBOqhuO1gLLXs9ASvwXwOatJW15GHrrrt5buSEONprCZoi3rGChgrC zTT56m1TMGmEJhTgyNlhSj1m7rR45p3TMWpcbrC32Rt8L5hrg/HcmqCFD2Ql3OPuj/cw Tp6bcCpW1jQbyKngpLlxZ0pCIssjfZHx4stEHhuwuJFR3DulPNLWKycfQPBLxyYlP+gA eufg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789766402; x=1790371202; 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=104X5wo7yQkILIFw+J6eQp79zBLiDOMXd7ygwwepPVY=; b=fEYYJtcdB/kFJbwXA9222/6e1xLn0acUg63nm43eDTsBKsghnYHDctlE10BMnM7mRu vfEnCC8AhDMm8NnOijfiBwXiLD971/wTYMT+YOMcm+k6M5l8/8hvn2wdCNYwJxCmyUS9 mi4tKkVJBPlnzrbBvW2lxVccwlHzDUCJ5ixq/FUjRf9ScGApJHw2mfFok51Nj48y/njO 2OP8XJfNYybdrqCnnM+m99GwF8tc5d/Txb1L72RXi0MXw5clPh1zOk2EcrjZY0ml+6tx vcOAZA6EMdUX4FZiUM4c9KvKarbwJm95WBCAHDwq+kWMdoQO6v9PUd48EwHROQKrwGt8 FdsA== X-Forwarded-Encrypted: i=1; AKwUvBzFO7yIAU7yfCI7biPOhk/bQ4SqIasYkWjkktYiDhJ6DfMYIQrtZnpRjQz9oa731JqYKGNFqGvwFyu0F+0=@vger.kernel.org X-Gm-Message-State: AFuF++n8EDEUxwkD2F3+bitllEKC4bgCKTBz9IrUXa5RLzqy9migk7n/ xDDYQ2bVGxtKJq4e5sfjh1nSe3tPGMvR4NtYmgHpDj54o7EQCsu5smgttmIykyecl7yxum1e+xz cvBoDdhRIRA== X-Received: from dyb19.prod.google.com ([2002:a05:693c:6313:b0:33b:beb3:6c8c]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:8887:b0:3b3:3506:cf3f with SMTP id adf61e73a8af0-3dd724bb2c8mr10123349637.1.1789766401631; Fri, 18 Sep 2026 14:20:01 -0700 (PDT) Date: Fri, 18 Sep 2026 14:19:21 -0700 In-Reply-To: <20260918211932.2966061-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918140659.2501976-1-irogers@google.com> <20260918211932.2966061-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918211932.2966061-8-irogers@google.com> Subject: [PATCH v4 07/18] perf trace: Filter events in BPF and avoid tracepoint vetoes From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org, Howard Chu Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The BPF augmented_raw_syscalls sys_enter and sys_exit programs returned 0 for syscalls that were not of interest. Returning 0 from a tracepoint BPF program vetoes the event for the whole system, so an unrelated concurrent perf trace, perf record or ftrace session listening to raw_syscalls would silently lose events. This is a cross-session side effect and shows up as flaky failures when perf tests run in parallel. Furthermore, syscall_unaugmented previously returned 1 without writing anything to the __augmented_syscalls__ ring buffer. This forced userspace perf trace to listen to both raw_syscalls:sys_enter and __augmented_syscalls__ in its evlist, requiring userspace event deduplication. Address these issues: 1. In augmented_raw_syscalls.bpf.c, never return 0 from tracepoint handlers: return 1 so non-traced syscalls pass through without vetoing other concurrent listeners. 2. Introduce pids_to_trace and syscalls_to_trace BPF hash maps to perform targeted filtering directly in BPF. Unselected syscalls or PIDs return 1 immediately without writing to the buffer. 3. In syscall_unaugmented, output the unaugmented enter payload into __augmented_syscalls__ and return 1. Change its section from SEC("tp/raw_syscalls/sys_enter") to SEC("tp/syscalls/sys_enter_unaugmented") so libbpf does not attempt to auto-attach it to raw_syscalls:sys_enter. 4. In bpf_trace_augment.c, add helpers to configure target PIDs and syscalls in the BPF maps, setting the activation flags (has_pids_to_trace, has_syscalls_to_trace) only after the maps are fully populated so already-attached BPF programs do not filter against a half-filled map. Explicitly attach only sys_enter and sys_exit via an attach_prog() helper that saves -errno before calling pr_debug() or bpf_link__destroy(). Destroy the skeleton on every failure path. Leaving a loaded but unusable skeleton behind is not inert: the setters called later from trace__run() would program its maps, and a partial attach would leave a BPF program live on raw_syscalls for a session that never starts. Since augmented_syscalls__{prepare,create_bpf_output}() failures fall back to unaugmented tracing rather than aborting, those setters have to become no-ops, which they only do once skel is NULL again. errno is used directly here, so include rather than relying on it arriving via another header, which it does not under musl. 5. In builtin-trace.c, hook trace__set_ev_qualifier_filter() and PID filtering into the BPF maps. When __augmented_syscalls__ is active, remove raw_syscalls:sys_enter from trace.evlist since all traced enter events (both augmented and unaugmented) are now emitted by BPF into __augmented_syscalls__. Identify that evsel by comparing against trace.syscalls.events.sys_enter rather than by a strstr() of its name. The substring "syscalls:sys_enter" also matches the per syscall syscalls:sys_enter_SYSCALL tracepoints, which a user can ask for by name, and now that the match decides what is taken out of the evlist, claiming one of those would drop an event that was asked for and would describe __augmented_syscalls__ with its format rather than the raw one's. Restore tracking on the remaining evsel via evlist__set_tracking_event() so PERF_RECORD_COMM and tracking events continue to be recorded. Errors from augmented_syscalls__set_target_syscalls() are reported and propagated, the tracepoint filter string is freed on every exit path, and an allocation failure in trace__set_filter_pids() now returns -ENOMEM instead of being silently ignored. Note that in trace__set_filter_pids() the target pids and the filtered pids are two independent axes and both have to be programmed. Naming pids to leave out with --filter-pids does not widen -p/-t or a workload to the whole system, and a BPF tracepoint program is attached system wide rather than to the target's file descriptors, so pids_to_trace is the only thing keeping other tasks out. 6. Add --syscall-augment option (defaulting to true) to allow users to explicitly use --no-syscall-augment to run perf trace in the classic unaugmented tracepoint mode without BPF. When BPF is unavailable or disabled, ensure the non-augmented tracepoint path cleanly configures sys_enter and sys_exit without duplicate entries. In trace__run(), add those tracepoints only when neither of them is in the evlist already. trace__add_syscall_newtp() adds the pair whatever is there, and cmd_trace() may have left either or both behind: both when it prepared augmentation and then gave up on it, the exit one alone when __augmented_syscalls__ took over the enters. Neither is covered by trace.raw_augmented_syscalls, which is only set when the exit evsel is named raw_syscalls:sys_exit, and not on a kernel old enough for perf_evsel__raw_syscall_newtp() to have fallen back to syscalls:sys_exit. Adding the pair again reports the events that were there twice, which the fallback after a bpf-output failure already did before this. Act on trace__add_syscall_newtp()'s result in cmd_trace() rather than discarding it. Those tracepoints are what the augmented events are described and paired with, so without them there is nothing to augment: the skeleton is dropped and the session carries on with plain tracepoints, which is what the bpf-output failure beside it already does, and what lets the evlist test above rely on the exit tracepoint being there whenever the bpf-output event is. A target that does not fit in the map is not a reason to give up on the session. bpf_map__update_elem() answers -E2BIG once max_entries keys are present, so a target with more threads than pids_to_trace has room for would take the whole of perf trace down with it, on exactly the large workloads where there is least else to reach for. The tasks that fit are added, a warning says how many did not, and the ones left out are in the position they would have been in had they been created once the map was already full, which sched_process_fork() has to allow for regardless. Report a failure to program the pid filters with the error that caused it. trace__run() sent everything trace__set_filter_pids() returned to out_error_mem, which prints "Not enough memory to run!". That was already a guess, and becomes a wrong one now that the function also writes BPF maps, which fail for reasons of their own that have nothing to do with memory and that the message gives the user no way to act on. Report a failure to program the syscall filters the same way. The pr_err() in trace__set_ev_qualifier_filter() ran before trace__run() reached out_errno and printed "%m", and anything called in between could have changed errno by then, so the two did not have to agree. The inner report becomes a pr_debug() and the caller prints the error it was given, which leaves out_errno without a user and it is removed. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/Documentation/perf-trace.txt | 5 + tools/perf/builtin-trace.c | 222 +++++++++++++++--- .../bpf_skel/augmented_raw_syscalls.bpf.c | 133 +++++++++-- tools/perf/util/bpf_trace_augment.c | 169 ++++++++++++- tools/perf/util/trace_augment.h | 34 +++ 5 files changed, 511 insertions(+), 52 deletions(-) diff --git a/tools/perf/Documentation/perf-trace.txt b/tools/perf/Documenta= tion/perf-trace.txt index d20b43ea3d37..4680c69160d7 100644 --- a/tools/perf/Documentation/perf-trace.txt +++ b/tools/perf/Documentation/perf-trace.txt @@ -260,6 +260,11 @@ the thread executes on the designated CPUs. Default is= to monitor all CPUs. Maximum number of lines in the summary mode. Note that this applies to each entry (thread or cgroup). =20 +--syscall-augment:: + Augment syscalls with BPF. Enabled by default when BPF support is availab= le. + Use --no-syscall-augment to disable BPF augmentation and fall back to the + unaugmented tracepoint approach. + =20 PAGEFAULTS ---------- diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index f6fb6ffd654a..d403f1a318e3 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -200,6 +200,7 @@ struct trace { int max_summary; int raw_augmented_syscalls_args_size; bool raw_augmented_syscalls; + bool syscall_augment; bool fd_path_disabled; bool sort_events; bool not_ev_qualifier; @@ -2060,6 +2061,23 @@ static int trace__process_event(struct trace *trace,= struct machine *machine, "LOST %" PRIu64 " events!\n", (u64)event->lost.lost); ret =3D machine__process_lost_event(machine, event, sample); break; + case PERF_RECORD_FORK: + if (trace->raw_augmented_syscalls && + (augmented_syscalls__has_target_pid(event->fork.ppid) || + augmented_syscalls__has_target_pid(event->fork.ptid))) { + augmented_syscalls__add_target_pid(event->fork.pid); + } + ret =3D machine__process_fork_event(machine, event, sample); + break; + case PERF_RECORD_EXIT: + if (trace->raw_augmented_syscalls) { + if (event->fork.pid =3D=3D event->fork.tid) + augmented_syscalls__del_target_pid(event->fork.pid); + else + augmented_syscalls__del_target_pid(event->fork.tid); + } + ret =3D machine__process_exit_event(machine, event, sample); + break; default: ret =3D machine__process_event(machine, event, sample); break; @@ -4059,7 +4077,7 @@ static int trace__add_syscall_newtp(struct trace *tra= ce) =20 static int trace__set_ev_qualifier_tp_filter(struct trace *trace) { - int err =3D -1; + int err =3D 0; struct evsel *sys_exit; char *filter =3D asprintf_expr_inout_ints("id", !trace->not_ev_qualifier, trace->ev_qualifier_ids.nr, @@ -4068,15 +4086,27 @@ static int trace__set_ev_qualifier_tp_filter(struct= trace *trace) if (filter =3D=3D NULL) goto out_enomem; =20 - if (!evsel__append_tp_filter(trace->syscalls.events.sys_enter, filter)) { - sys_exit =3D trace->syscalls.events.sys_exit; + /* + * With BPF augmentation sys_enter is filtered in BPF and removed from + * the evlist, so only apply the tracepoint filter to the events that + * are actually present. + */ + if (trace->syscalls.events.sys_enter) + err =3D evsel__append_tp_filter(trace->syscalls.events.sys_enter, filter= ); + + sys_exit =3D trace->syscalls.events.sys_exit; + if (!err && sys_exit) err =3D evsel__append_tp_filter(sys_exit, filter); - } =20 free(filter); out: return err; out_enomem: + /* + * err defaults to 0 because either tracepoint may legitimately be + * absent, so the error has to be set explicitly here. + */ + err =3D -ENOMEM; errno =3D ENOMEM; goto out; } @@ -4518,7 +4548,27 @@ static int trace__init_syscalls_bpf_prog_array_maps(= struct trace *trace __maybe_ =20 static int trace__set_ev_qualifier_filter(struct trace *trace) { - if (trace->syscalls.events.sys_enter) + /* + * Synchronize syscall filter with BPF augmenter map: + * Pass trace->not_ev_qualifier to indicate blacklist mode ('!' prefix, + * e.g., -e !open,close) vs whitelist mode (-e open,close). + * + * A failure here would leave the BPF program filtering on a partially + * populated map, silently dropping or emitting the wrong syscalls, so + * propagate the error rather than continuing. + */ + if (trace->ev_qualifier_ids.nr > 0) { + int err =3D augmented_syscalls__set_target_syscalls(trace->ev_qualifier_= ids.nr, + trace->ev_qualifier_ids.entries, + trace->not_ev_qualifier); + + if (err) { + pr_debug("Failed to set the syscalls to trace in the BPF map: %d\n", er= r); + return err; + } + } + + if (trace->syscalls.events.sys_enter || trace->syscalls.events.sys_exit) return trace__set_ev_qualifier_tp_filter(trace); return 0; } @@ -4559,13 +4609,21 @@ static int trace__set_filter_loop_pids(struct trace= *trace) =20 static int trace__set_filter_pids(struct trace *trace) { - int err =3D 0; + struct perf_thread_map *threads =3D evlist__core(trace->evlist)->threads; /* * Better not use !target__has_task() here because we need to cover the * case where no threads were specified in the command line, but a * workload was, and in that case we will fill in the thread_map when * we fork the workload in evlist__prepare_workload. */ + bool has_target =3D perf_thread_map__pid(threads, 0) !=3D -1; + int err =3D 0; + + /* + * The exclusion list: --filter-pids names tasks to never report, and + * with no target at all we instead exclude perf itself so that tracing + * does not feed back into itself. + */ if (trace->filter_pids.nr > 0) { err =3D evlist__append_tp_filter_pids(trace->evlist, trace->filter_pids.= nr, trace->filter_pids.entries); @@ -4573,10 +4631,37 @@ static int trace__set_filter_pids(struct trace *tra= ce) err =3D augmented_syscalls__set_filter_pids(trace->filter_pids.nr, trace->filter_pids.entries); } - } else if (perf_thread_map__pid(evlist__core(trace->evlist)->threads, 0) = =3D=3D -1) { + } else if (!has_target) { err =3D trace__set_filter_loop_pids(trace); } =20 + if (err) + return err; + + /* + * The inclusion list, which is a separate axis from the exclusion list + * above and so must be programmed even when --filter-pids was given: + * naming tasks to leave out does not widen -p/-t or a workload to the + * whole system. + * + * This matters more than it does on the tracepoint only path. A BPF + * tracepoint program is attached system wide rather than to the + * target's file descriptors, so pids_to_trace is the only thing + * keeping other tasks out. + */ + if (has_target) { + int nr =3D perf_thread_map__nr(threads); + pid_t *pids =3D malloc(nr * sizeof(pid_t)); + + if (pids =3D=3D NULL) + return -ENOMEM; + + for (int i =3D 0; i < nr; i++) + pids[i] =3D perf_thread_map__pid(threads, i); + err =3D augmented_syscalls__set_target_pids(nr, pids); + free(pids); + } + return err; } =20 @@ -4803,7 +4888,20 @@ static int trace__run(struct trace *trace, int argc,= const char **argv) } =20 if (!trace->raw_augmented_syscalls) { - if (trace->trace_syscalls && trace__add_syscall_newtp(trace)) + /* + * The syscall tracepoints may be in the evlist already: + * cmd_trace() adds the pair whenever it prepares BPF + * augmentation, and it then either keeps them, gives up on + * augmentation and leaves them behind, or has + * __augmented_syscalls__ take over the enters and removes only + * the enter one. trace__add_syscall_newtp() adds the pair + * whatever is there, so it is called only when neither is, and + * every event it adds is one the evlist does not already have. + */ + bool have_syscall_tp =3D trace->syscalls.events.sys_enter !=3D NULL || + trace->syscalls.events.sys_exit !=3D NULL; + + if (trace->trace_syscalls && !have_syscall_tp && trace__add_syscall_newt= p(trace)) goto out_error_raw_syscalls; =20 if (trace->trace_syscalls) @@ -4898,7 +4996,7 @@ static int trace__run(struct trace *trace, int argc, = const char **argv) =20 err =3D trace__set_filter_pids(trace); if (err < 0) - goto out_error_mem; + goto out_error_filter_pids; =20 /* * TODO: Initialize for all host binary machine types, not just @@ -4909,7 +5007,7 @@ static int trace__run(struct trace *trace, int argc, = const char **argv) if (trace->ev_qualifier_ids.nr > 0) { err =3D trace__set_ev_qualifier_filter(trace); if (err < 0) - goto out_errno; + goto out_error_ev_qualifier; =20 if (trace->syscalls.events.sys_exit) { pr_debug("event qualifier tracepoint filter: %s\n", @@ -5089,14 +5187,32 @@ static int trace__run(struct trace *trace, int argc= , const char **argv) "Failed to set filter \"%s\" on event %s: %m\n", evsel->filter, evsel__name(evsel)); goto out_put_evlist; + +out_error_filter_pids: + /* + * Report what actually went wrong. Programming the pid filters + * allocates, but it also writes BPF maps, which fails for reasons of + * its own: -E2BIG when the target has more threads than pids_to_trace + * has room for, say. + */ + fprintf(trace->output, "Failed to set the pid filters: %s\n", + str_error_r(-err, errbuf, sizeof(errbuf))); + goto out_put_evlist; + +out_error_ev_qualifier: + /* + * Use the returned error, not errno. Reporting the failure on the way + * out of trace__set_ev_qualifier_filter() goes through the formatted + * output functions, which are free to leave errno describing + * something else by the time it is read here. + */ + fprintf(trace->output, "Failed to set the syscall filters: %s\n", + str_error_r(-err, errbuf, sizeof(errbuf))); + goto out_put_evlist; } out_error_mem: fprintf(trace->output, "Not enough memory to run!\n"); goto out_put_evlist; - -out_errno: - fprintf(trace->output, "%m\n"); - goto out_put_evlist; } =20 static int trace__replay(struct trace *trace) @@ -5811,6 +5927,7 @@ int cmd_trace(int argc, const char **argv) .show_arg_names =3D true, .args_alignment =3D 70, .trace_syscalls =3D false, + .syscall_augment =3D true, .kernel_syscallchains =3D false, .max_stack =3D UINT_MAX, .max_events =3D ULONG_MAX, @@ -5866,6 +5983,8 @@ int cmd_trace(int argc, const char **argv) OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min", "Trace pagefaults", parse_pagefaults, "maj"), OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"), + OPT_BOOLEAN(0, "syscall-augment", &trace.syscall_augment, + "Augment syscalls with BPF"), OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"), OPT_CALLBACK(0, "call-graph", &trace.opts, "record_mode[,record_size]", record_callchain_help, @@ -5988,7 +6107,7 @@ int cmd_trace(int argc, const char **argv) "cgroup monitoring only available in system-wide mode"); } =20 - if (!trace.trace_syscalls) + if (!trace.trace_syscalls || !trace.syscall_augment) goto skip_augmentation; =20 if ((argc >=3D 1) && (strcmp(argv[0], "record") =3D=3D 0)) { @@ -6010,11 +6129,34 @@ int cmd_trace(int argc, const char **argv) if (err < 0) goto skip_augmentation; =20 - trace__add_syscall_newtp(&trace); + /* + * The syscall tracepoints are what the augmented events are described + * and paired with: the enter one supplies the format + * __augmented_syscalls__ is read with, and the exit one reports the + * returns, since BPF only takes over the enters. Without them there is + * nothing to augment, so drop the skeleton and carry on with plain + * tracepoints, as the bpf-output failure below does. + */ + if (trace__add_syscall_newtp(&trace)) { + pr_debug("Failed to set up the syscall tracepoints, disabling augmentati= on\n"); + augmented_syscalls__cleanup(); + goto skip_augmentation; + } =20 err =3D augmented_syscalls__create_bpf_output(trace.evlist); - if (err =3D=3D 0) + if (err =3D=3D 0) { trace.syscalls.events.bpf_output =3D evlist__last(trace.evlist); + } else { + /* + * augmented_syscalls__prepare() already attached sys_enter and + * sys_exit, which are system wide. Falling through to + * skip_augmentation without undoing that would run a BPF + * program for every syscall on the machine, for the whole + * session, with nothing consuming the output. + */ + pr_debug("Failed to create the augmented syscalls bpf-output event, disa= bling augmentation\n"); + augmented_syscalls__cleanup(); + } =20 skip_augmentation: err =3D -1; @@ -6070,7 +6212,9 @@ int cmd_trace(int argc, const char **argv) * syscall. */ if (trace.syscalls.events.bpf_output) { - evlist__for_each_entry(trace.evlist, evsel) { + struct evsel *n; + + evlist__for_each_entry_safe(trace.evlist, n, evsel) { bool raw_syscalls_sys_exit =3D evsel__name_is(evsel, "raw_syscalls:sys_= exit"); =20 if (raw_syscalls_sys_exit) { @@ -6078,28 +6222,44 @@ int cmd_trace(int argc, const char **argv) goto init_augmented_syscall_tp; } =20 - if (trace.syscalls.events.bpf_output->priv =3D=3D NULL && - strstr(evsel__name(evsel), "syscalls:sys_enter")) { + /* + * Match the evsel trace__add_syscall_newtp() made by + * identity rather than by name. It is called + * raw_syscalls:sys_enter, or syscalls:sys_enter on + * kernels too old to have the raw variant, and a + * substring test for the latter also matches the + * per syscall syscalls:sys_enter_SYSCALL tracepoints + * a user can ask for by name. Claiming one of those + * here would take the event the user asked for out of + * the evlist below and describe __augmented_syscalls__ + * with the wrong tracefs format. + */ + if (evsel =3D=3D trace.syscalls.events.sys_enter) { struct evsel *augmented =3D trace.syscalls.events.bpf_output; if (evsel__init_augmented_syscall_tp(augmented, evsel) || evsel__init_augmented_syscall_tp_args(augmented)) goto out; /* - * Augmented is __augmented_syscalls__ BPF_OUTPUT event + * Augmented is __augmented_syscalls__ BPF_OUTPUT event. * Above we made sure we can get from the payload the tp fields * that we get from syscalls:sys_enter tracefs format file. + * Since BPF outputs all enter events (both augmented and + * unaugmented) into __augmented_syscalls__, we remove the raw + * sys_enter evsel from evlist so that perf trace only listens + * to __augmented_syscalls__, avoiding duplicate events and + * avoiding kernel tracepoint vetoes. + * + * Because evlist__remove() removes the first evsel (which had + * tracking=3Dtrue by default), re-designate the tracking event + * so PERF_RECORD_COMM and fork tracking continue to be enabled. */ augmented->handler =3D trace__sys_enter; - /* - * Now we do the same for the *syscalls:sys_enter event so that - * if we handle it directly, i.e. if the BPF prog returns 0 so - * as not to filter it, then we'll handle it just like we would - * for the BPF_OUTPUT one: - */ - if (evsel__init_augmented_syscall_tp(evsel, evsel) || - evsel__init_augmented_syscall_tp_args(evsel)) - goto out; - evsel->handler =3D trace__sys_enter; + evlist__remove(trace.evlist, evsel); + evsel__put_and_free_priv(evsel); + trace.syscalls.events.sys_enter =3D NULL; + evlist__set_tracking_event(trace.evlist, + trace.syscalls.events.sys_exit ?: augmented); + continue; } =20 if (strstarts(evsel__name(evsel), "syscalls:sys_exit_")) { diff --git a/tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c b/tools/= perf/util/bpf_skel/augmented_raw_syscalls.bpf.c index 3bc9e28a9b8a..6ca9507ecc02 100644 --- a/tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +++ b/tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c @@ -114,6 +114,41 @@ struct pids_filtered { __uint(max_entries, 64); } pids_filtered SEC(".maps"); =20 +/* + * Optional hash map containing specific PIDs/TGIDs to trace (e.g., when + * attached to a process with -p or tracing a specific command workload). + * + * has_pids_to_trace: Set to true if target PID filtering is active. + * When false, all processes are eligible for tracing. + */ +struct pids_to_trace { + __uint(type, BPF_MAP_TYPE_HASH); + __type(key, pid_t); + __type(value, bool); + __uint(max_entries, 1024); +} pids_to_trace SEC(".maps"); + +bool has_pids_to_trace; + +/* + * Hash map storing syscall IDs for filtering (via 'perf trace -e ...'). + * + * has_syscalls_to_trace: Set to true if any syscall filter is active. + * not_syscalls_to_trace: Inverts matching when '!' prefix is used in -e + * (e.g., -e !open,close means trace everything EXC= EPT + * open and close; an exclusion blacklist rather th= an + * an inclusion whitelist). + */ +struct syscalls_to_trace { + __uint(type, BPF_MAP_TYPE_HASH); + __type(key, int); + __type(value, bool); + __uint(max_entries, 1024); +} syscalls_to_trace SEC(".maps"); + +bool has_syscalls_to_trace; +bool not_syscalls_to_trace; + struct augmented_args_payload { struct syscall_enter_args args; struct augmented_arg arg, arg2; // We have to reserve space for two argum= ents (rename, etc) @@ -154,8 +189,8 @@ static inline struct augmented_args_payload *augmented_= args_payload(void) =20 static inline int augmented__output(void *ctx, struct augmented_args_paylo= ad *args, int len) { - /* If perf_event_output fails, return non-zero so that it gets recorded u= naugmented */ - return bpf_perf_event_output(ctx, &__augmented_syscalls__, BPF_F_CURRENT_= CPU, args, len); + bpf_perf_event_output(ctx, &__augmented_syscalls__, BPF_F_CURRENT_CPU, ar= gs, len); + return 1; } =20 static inline int augmented__beauty_output(void *ctx, void *data, int len) @@ -191,10 +226,21 @@ unsigned int augmented_arg__read_str(struct augmented= _arg *augmented_arg, const return augmented_len; } =20 -SEC("tp/raw_syscalls/sys_enter") +/* + * Default sys_enter program for syscalls without pointer argument augment= ation. + * Writes the raw struct syscall_enter_args payload into __augmented_sysca= lls__ + * and returns 1 so the tracepoint is never vetoed in the kernel. + */ +SEC("tp/syscalls/sys_enter_unaugmented") int syscall_unaugmented(struct syscall_enter_args *args) { - return 1; + struct augmented_args_payload *augmented_args =3D augmented_args_payload(= ); + + if (augmented_args =3D=3D NULL) + return 1; + + bpf_probe_read_kernel(&augmented_args->args, sizeof(augmented_args->args)= , args); + return augmented__output(args, augmented_args, sizeof(augmented_args->arg= s)); } =20 /* @@ -424,11 +470,41 @@ static pid_t getpid(void) return bpf_get_current_pid_tgid(); } =20 +/* + * Returns true if a PID is explicitly excluded/filtered out (e.g., via --= filter-pids). + */ static bool pid_filter__has(struct pids_filtered *pids, pid_t pid) { return bpf_map_lookup_elem(pids, &pid) !=3D NULL; } =20 +/* + * Checks if the current task (thread PID or process TGID) is targeted for= tracing. + * Checks both PID (thread ID) and TGID (process ID) so that all threads o= f a + * target process match. + */ +static inline bool pid_to_trace__has(pid_t pid) +{ + pid_t tgid =3D bpf_get_current_pid_tgid() >> 32; + + return bpf_map_lookup_elem(&pids_to_trace, &pid) !=3D NULL || + bpf_map_lookup_elem(&pids_to_trace, &tgid) !=3D NULL; +} + +/* + * Determines if a syscall should be traced based on the filter map: + * - When not_syscalls_to_trace is true: blacklist mode (trace if NOT in m= ap). + * - When not_syscalls_to_trace is false: whitelist mode (trace ONLY if IN= map). + */ +static inline bool syscall_to_trace__enabled(int id) +{ + bool in_map =3D bpf_map_lookup_elem(&syscalls_to_trace, &id) !=3D NULL; + + if (not_syscalls_to_trace) + return !in_map; + return in_map; +} + u64 ZERO =3D 0; =20 /* @@ -562,6 +638,11 @@ static int augment_sys_enter(void *ctx, struct syscall= _enter_args *args) return augmented__beauty_output(ctx, payload, sizeof(struct syscall_enter= _args) + output); } =20 +/* + * Main raw_syscalls:sys_enter tracepoint handler. + * Always returns 1 so the tracepoint is never vetoed in the kernel for + * other concurrent listeners. Filtered events simply do not output to the= ring buffer. + */ SEC("tp/raw_syscalls/sys_enter") int sys_enter(struct syscall_enter_args *args) { @@ -576,8 +657,11 @@ int sys_enter(struct syscall_enter_args *args) * initial, non-augmented raw_syscalls:sys_enter payload. */ =20 + if (has_pids_to_trace && !pid_to_trace__has(getpid())) + return 1; + if (pid_filter__has(&pids_filtered, getpid())) - return 0; + return 1; =20 augmented_args =3D augmented_args_payload(); if (augmented_args =3D=3D NULL) @@ -585,25 +669,41 @@ int sys_enter(struct syscall_enter_args *args) =20 bpf_probe_read_kernel(&augmented_args->args, sizeof(augmented_args->args)= , args); =20 + if (has_syscalls_to_trace && !syscall_to_trace__enabled(augmented_args->a= rgs.syscall_nr)) + return 1; + /* - * Jump to syscall specific augmenter, even if the default one, - * "!raw_syscalls:unaugmented" that will just return 1 to return the - * unaugmented tracepoint payload. + * Jump to syscall specific augmenter. If augmented, augment_sys_enter() + * outputs the payload to __augmented_syscalls__ and returns 0. + * Return 1 so we never veto the kernel tracepoint for other listeners. */ - if (augment_sys_enter(args, &augmented_args->args)) - bpf_tail_call(args, &syscalls_sys_enter, augmented_args->args.syscall_nr= ); + if (augment_sys_enter(args, &augmented_args->args) =3D=3D 0) + return 1; =20 - // If not found on the PROG_ARRAY syscalls map, then we're filtering it: - return 0; + bpf_tail_call(args, &syscalls_sys_enter, augmented_args->args.syscall_nr); + + /* + * If not found on the PROG_ARRAY syscalls map, return 1 so we + * don't veto the tracepoint event system-wide for other concurrent + * listeners. + */ + return 1; } =20 +/* + * Main raw_syscalls:sys_exit tracepoint handler. + * Always returns 1 so the tracepoint is never vetoed in the kernel. + */ SEC("tp/raw_syscalls/sys_exit") int sys_exit(struct syscall_exit_args *args) { struct syscall_exit_args exit_args; =20 + if (has_pids_to_trace && !pid_to_trace__has(getpid())) + return 1; + if (pid_filter__has(&pids_filtered, getpid())) - return 0; + return 1; =20 bpf_probe_read_kernel(&exit_args, sizeof(exit_args), args); /* @@ -613,9 +713,12 @@ int sys_exit(struct syscall_exit_args *args) */ bpf_tail_call(args, &syscalls_sys_exit, exit_args.syscall_nr); /* - * If not found on the PROG_ARRAY syscalls map, then we're filtering it: + * If not found on the PROG_ARRAY syscalls map, return 1 so we + * don't veto the tracepoint event system-wide for other concurrent + * listeners. perf trace's own evsel filter will discard non-matching + * syscalls. */ - return 0; + return 1; } =20 char _license[] SEC("license") =3D "GPL"; diff --git a/tools/perf/util/bpf_trace_augment.c b/tools/perf/util/bpf_trac= e_augment.c index ebb26225fb04..5f15b27264e9 100644 --- a/tools/perf/util/bpf_trace_augment.c +++ b/tools/perf/util/bpf_trace_augment.c @@ -1,5 +1,6 @@ #include #include +#include #include #include =20 @@ -12,6 +13,23 @@ static struct augmented_raw_syscalls_bpf *skel; static struct evsel *bpf_output; =20 +/* Set by attach_prog() so the first failure is what gets reported. */ +static int attach_err; + +static int attach_prog(struct bpf_link **link, struct bpf_program *prog, c= onst char *name) +{ + *link =3D bpf_program__attach(prog); + if (*link) + return 0; + /* + * Save errno before pr_debug(), which formats and writes output and so + * can overwrite it. + */ + attach_err =3D -errno; + pr_debug("Failed to attach %s BPF program\n", name); + return attach_err; +} + int augmented_syscalls__prepare(void) { struct bpf_program *prog; @@ -37,11 +55,35 @@ int augmented_syscalls__prepare(void) if (err < 0) { libbpf_strerror(err, buf, sizeof(buf)); pr_debug("Failed to load augmented syscalls BPF skeleton: %s\n", buf); + /* + * Tear the skeleton down rather than leaving a half initialized + * one behind. The caller falls back to unaugmented tracing and + * still calls the setters below, which must then do nothing + * instead of failing against a skeleton with no maps. + */ + augmented_syscalls__cleanup(); return err; } =20 - augmented_raw_syscalls_bpf__attach(skel); + /* + * Only sys_enter and sys_exit are attached, the remaining programs are + * reached by tail calls. Attach them explicitly and, on failure, undo + * any partial attachment: leaving sys_enter live on + * raw_syscalls:sys_enter would keep running a BPF program for every + * syscall on the system for a perf trace session that never starts. + */ + if (attach_prog(&skel->links.sys_enter, skel->progs.sys_enter, "sys_enter= ")) + goto out_cleanup; + if (attach_prog(&skel->links.sys_exit, skel->progs.sys_exit, "sys_exit")) + goto out_cleanup; + return 0; + +out_cleanup: + err =3D attach_err; + /* Destroys every link attached above along with the skeleton. */ + augmented_syscalls__cleanup(); + return err; } =20 int augmented_syscalls__create_bpf_output(struct evlist *evlist) @@ -82,22 +124,136 @@ void augmented_syscalls__setup_bpf_output(void) } } =20 +/* + * Add every pid to a pid keyed filter map. + * + * A map with no room left is not treated as a failure. bpf_map__update_el= em() + * answers -E2BIG once max_entries keys are present, and a task that did n= ot + * fit is then one the filter does not know about, which is exactly the + * position it would be in had it been created after the map filled up. + * Refusing to trace at all instead would make perf trace unusable against= the + * very large targets that are the only way to reach the limit, so say how= many + * missed out and carry on with those that did fit. + */ +static int add_pids_to_map(struct bpf_map *map, const char *missing_out_on, + unsigned int nr, pid_t *pids) +{ + unsigned int nr_no_room =3D 0; + bool value =3D true; + + for (unsigned int i =3D 0; i < nr; ++i) { + int err =3D bpf_map__update_elem(map, &pids[i], sizeof(*pids), + &value, sizeof(value), BPF_ANY); + + if (err =3D=3D -E2BIG) { + nr_no_room++; + continue; + } + if (err) + return err; + } + + if (nr_no_room) { + pr_warning("Only %u of %u tasks fit in the %s BPF map, %u will not be %s= .\n", + nr - nr_no_room, nr, bpf_map__name(map), nr_no_room, + missing_out_on); + } + + return 0; +} + int augmented_syscalls__set_filter_pids(unsigned int nr, pid_t *pids) +{ + if (skel =3D=3D NULL) + return 0; + + return add_pids_to_map(skel->maps.pids_filtered, "filtered out", nr, pids= ); +} + +/* + * Populate target PIDs in the BPF pids_to_trace map (e.g., for -p or + * when tracing a specified command workload). + */ +int augmented_syscalls__set_target_pids(unsigned int nr, pid_t *pids) +{ + int err; + + if (skel =3D=3D NULL || nr =3D=3D 0) + return 0; + + err =3D add_pids_to_map(skel->maps.pids_to_trace, "traced", nr, pids); + if (err) + return err; + + /* + * Set the flag only once every target is in the map. The BPF programs + * are attached by this point, so flipping it first would have them + * filter against a partially populated map and drop syscalls made by + * the targets that had not been added yet. + */ + skel->bss->has_pids_to_trace =3D true; + return 0; +} + +int augmented_syscalls__add_target_pid(pid_t pid) +{ + bool value =3D true; + + if (skel =3D=3D NULL || !skel->bss->has_pids_to_trace || skel->maps.pids_= to_trace =3D=3D NULL) + return 0; + + return bpf_map__update_elem(skel->maps.pids_to_trace, &pid, sizeof(pid), + &value, sizeof(value), BPF_ANY); +} + +int augmented_syscalls__del_target_pid(pid_t pid) +{ + if (skel =3D=3D NULL || !skel->bss->has_pids_to_trace || skel->maps.pids_= to_trace =3D=3D NULL) + return 0; + + return bpf_map__delete_elem(skel->maps.pids_to_trace, &pid, sizeof(pid), = 0); +} + +bool augmented_syscalls__has_target_pid(pid_t pid) +{ + bool value; + + if (skel =3D=3D NULL || !skel->bss->has_pids_to_trace || skel->maps.pids_= to_trace =3D=3D NULL) + return false; + + return bpf_map__lookup_elem(skel->maps.pids_to_trace, &pid, sizeof(pid), + &value, sizeof(value), 0) =3D=3D 0; +} + +/* + * Populate syscalls in the BPF syscalls_to_trace map: + * - not_syscalls: true if '!' prefix was specified (blacklist mode: trace + * all syscalls EXCEPT these). + * false if whitelist mode (trace ONLY these syscalls). + */ +int augmented_syscalls__set_target_syscalls(unsigned int nr, int *syscall_= ids, bool not_syscalls) { bool value =3D true; int err =3D 0; =20 - if (skel =3D=3D NULL) + if (skel =3D=3D NULL || nr =3D=3D 0) return 0; =20 + skel->bss->not_syscalls_to_trace =3D not_syscalls; for (size_t i =3D 0; i < nr; ++i) { - err =3D bpf_map__update_elem(skel->maps.pids_filtered, &pids[i], - sizeof(*pids), &value, sizeof(value), + err =3D bpf_map__update_elem(skel->maps.syscalls_to_trace, &syscall_ids[= i], + sizeof(int), &value, sizeof(value), BPF_ANY); if (err) - break; + return err; } - return err; + /* + * As for the pid maps, publish the filter only once it is complete: + * in whitelist mode a half filled map would drop syscalls that were + * asked for but not added yet. + */ + skel->bss->has_syscalls_to_trace =3D true; + return 0; } =20 int augmented_syscalls__get_map_fds(int *enter_fd, int *exit_fd, int *beau= ty_fd) @@ -142,4 +298,5 @@ struct bpf_program *augmented_syscalls__find_by_title(c= onst char *name) void augmented_syscalls__cleanup(void) { augmented_raw_syscalls_bpf__destroy(skel); + skel =3D NULL; } diff --git a/tools/perf/util/trace_augment.h b/tools/perf/util/trace_augmen= t.h index a1cd9a5e0213..5702eda3b469 100644 --- a/tools/perf/util/trace_augment.h +++ b/tools/perf/util/trace_augment.h @@ -2,6 +2,7 @@ #define TRACE_AUGMENT_H =20 #include +#include #include =20 struct bpf_program; @@ -13,6 +14,11 @@ int augmented_syscalls__prepare(void); int augmented_syscalls__create_bpf_output(struct evlist *evlist); void augmented_syscalls__setup_bpf_output(void); int augmented_syscalls__set_filter_pids(unsigned int nr, pid_t *pids); +int augmented_syscalls__set_target_pids(unsigned int nr, pid_t *pids); +int augmented_syscalls__add_target_pid(pid_t pid); +int augmented_syscalls__del_target_pid(pid_t pid); +bool augmented_syscalls__has_target_pid(pid_t pid); +int augmented_syscalls__set_target_syscalls(unsigned int nr, int *syscall_= ids, bool not_syscalls); int augmented_syscalls__get_map_fds(int *enter_fd, int *exit_fd, int *beau= ty_fd); struct bpf_program *augmented_syscalls__find_by_title(const char *name); struct bpf_program *augmented_syscalls__unaugmented(void); @@ -40,6 +46,34 @@ static inline int augmented_syscalls__set_filter_pids(un= signed int nr __maybe_un return 0; } =20 +static inline int augmented_syscalls__set_target_pids(unsigned int nr __ma= ybe_unused, + pid_t *pids __maybe_unused) +{ + return 0; +} + +static inline int augmented_syscalls__add_target_pid(pid_t pid __maybe_unu= sed) +{ + return 0; +} + +static inline int augmented_syscalls__del_target_pid(pid_t pid __maybe_unu= sed) +{ + return 0; +} + +static inline bool augmented_syscalls__has_target_pid(pid_t pid __maybe_un= used) +{ + return false; +} + +static inline int augmented_syscalls__set_target_syscalls(unsigned int nr = __maybe_unused, + int *syscall_ids __maybe_unused, + bool not_syscalls __maybe_unused) +{ + return 0; +} + static inline int augmented_syscalls__get_map_fds(int *enter_fd __maybe_un= used, int *exit_fd __maybe_unused, int *beauty_fd __maybe_unused) --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Thu Sep 24 22:56:31 2026 Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 51CC452BE57 for ; Fri, 18 Sep 2026 21:20:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.71 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766412; cv=none; b=OLc9UN5ciwZPaO5OyxWgkITeYPc8+tRv5B2Oe+Wpiiigtd36F07iAeC9zOk0FpHvYgomX6qz29m/K9rwQk1GMcIES2NWViKxc/GL1krFijygCXhfeCbeOqWt/zIAVSrSndhSgpSqvO+KbiAyb0JwJg8XRud1ETbyyAasNwbem/Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766412; c=relaxed/simple; bh=wm/dJADonyvD4cFObm02BdaP/RfGDu3YVgHWEHU0UDY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hOTne6rWQ1pOQBqfdsvagBTucLBTzYg2SJIcbQ3pNRqLbgD/vAT/B5uAZK26ALw99WNbiNxlsjb274hO2+ipoGgx4Fu4aOMXQEgaZQvMu4KfU/PRxNzGobT1ZtgmOryLvYoL85guz3gzHCn/8NilIqKJlNPL72phxb3Y8IQIABU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=nZn43p6Z; arc=none smtp.client-ip=209.85.216.71 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.b="nZn43p6Z" Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-398dcfabbf8so3625670a91.0 for ; Fri, 18 Sep 2026 14:20:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789766408; x=1790371208; 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=nWHx/HjN8oBpR9YlC24oLHFqH72BxzeHZl5JQQpESlU=; b=nZn43p6ZrJcEIk0C1QMg2TaGuKBuL/5D0v6Z38FFqnGoZIJCjYDMoXZmJefu8WT9Bc BvGHoB8arpEKK1lKrYHnZZ5xBuXrCTkzNuzBTOntvDTD1mRSKvifeMgdYd8vu8ViOI2Y vfkSv+a+DMgKjx2FiZKwCEX4oc4y/100NKfJ5iOJthru1NTwphUUJJ17eHfv/rqSpj6u KvA55AnHNw7e70yBrNmBnpA94jBJeoZIAWViB52ZdmaMWcuN4zEKHOljmJR8tfKxrQhh Bjoy24oplBa+/INcFKfPfq8vf8lQe69ulUpkcdiy+Aw5toAr4UzKNyxfy4unx3OwJEAZ GyUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789766408; x=1790371208; 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=nWHx/HjN8oBpR9YlC24oLHFqH72BxzeHZl5JQQpESlU=; b=mhyJNpTpItnNSFjf6nw50WRr2fc3qXVQg2IuQuH+d8SsKjSUnSkwgStDKQvMWcBluA xegoeWwfLxAQAh8BP5VUAOYqQKSRdBL1AoZj5oQAr1ChcqgWw2xmsQqXhTS2piSfTy6x kJqx1KuWaIY7bI3STFTNIhdcNEsbWobvSCiLVN28KvxiVOIaxq0K3g+zLCuwLzZHUtSU EztYxxYbHumNw/oO9+8tgfuHjwrgm0TMUfdpYV5UaqmM6spfqadyBzIukPMlgQxgbXyO WKnZGUNxL62qaq2mGA1Wsb4+vqTztHJwB24EN1nO3EziSwrc8IqF7h1FZtYEtJ2sT8nP Hg0g== X-Forwarded-Encrypted: i=1; AKwUvBxLKyFm2uV2TV3APdVYARAevE0ZwNwSBukzs/sbLPd0Epb8OQiemgFo8b67J/um5TE25eM2f7c3oOe/HKg=@vger.kernel.org X-Gm-Message-State: AFuF++kuvlrHWQ9GgQEMHG3ima+aG3674M2NhN/ZZl9S+LXwPcIxmMvl cQsPcv/CBHM5LSELXBeX1Yu7yxGeUGzs9cUyx6PJCck5e5Qf05BZGWbVOiKOezLUHZ8FVb6gmFK 2Qhizqh2fgQ== X-Received: from dlxx3.prod.google.com ([2002:a05:7022:4083:b0:143:9303:fb4c]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:558b:b0:39e:6c69:9b93 with SMTP id 98e67ed59e1d1-39e6c69b270mr1277545a91.56.1789766408236; Fri, 18 Sep 2026 14:20:08 -0700 (PDT) Date: Fri, 18 Sep 2026 14:19:22 -0700 In-Reply-To: <20260918211932.2966061-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918140659.2501976-1-irogers@google.com> <20260918211932.2966061-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918211932.2966061-9-irogers@google.com> Subject: [PATCH v4 08/18] perf trace: Handle fork and exit directly in BPF filter maps From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org, Howard Chu Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Updating target or filtered PIDs in userspace upon processing PERF_RECORD_FORK and PERF_RECORD_EXIT events introduces latency between event occurrence and userspace BPF map updates. If a newly forked child executes system calls before userspace processes PERF_RECORD_FORK, those syscalls may be dropped by BPF PID filtering. Conversely, if userspace evicts PIDs asynchronously on PERF_RECORD_EXIT, the kernel may recycle a PID before userspace processes the exit event, causing the late eviction to silently drop a newly created task that received the recycled PID. Address this by attaching BTF-typed raw tracepoint BPF programs directly to the scheduler task lifetime tracepoints: 1. Attach SEC("tp_btf/sched_process_fork") (sched_process_fork), which runs in copy_process() in the parent's context before wake_up_new_task() wakes the child. Using tp_btf rather than SEC("tp/sched/sched_process_fork") receives the stable TP_PROTO arguments (struct task_struct *parent, struct task_struct *child) rather than the tracepoint ring-buffer record (TP_STRUCT__entry), whose layout changed in Linux 6.16 when parent_comm and child_comm were converted from 16-byte arrays to 4-byte __data_loc strings. When inherit is enabled and the pid of the thread that called clone() is in pids_to_trace or pids_filtered, insert child->pid into the corresponding map immediately. Because child->pid is task_struct.pid (the global initial-namespace PID), this works accurately across PID namespaces without aliasing host PIDs, and covers both new processes and CLONE_THREAD threads without needing real_parent CO-RE walks or syscall-return heuristics. 2. Attach SEC("tp_btf/sched_process_exit") (sched_process_exit), which runs in do_exit() for every task in its own context, including tasks killed by signals (SIGKILL, SIGSEGV, etc.) and secondary threads torn down implicitly by exit_group. Delete the dying task's PID from pids_to_trace and pids_filtered immediately in kernel space, eliminating both map leaks and any asynchronous userspace eviction window where PID recycling could occur. 3. Attach SEC("tp_btf/sched_process_exec") (sched_process_exec) to follow the one case where a live task's pid changes underneath the maps. When a thread that is not the group leader execs, de_thread() kills the leader and hands the leader's pid, which is the tgid, to the exec'ing thread. The leader dies first, so sched_process_exit() has already dropped exactly the pid the survivor now holds, and the survivor's old entry would be stranded in the map for good. Move the entry from old_pid to p->pid. old_pid is sampled in bprm_execve() before de_thread() runs, so the ordinary group leader exec is a no-op here. Drop the old key before inserting the new one: the move is only a rename, but holding both keys at once needs a spare slot, and on a full map the insert would fail with -E2BIG while the delete still succeeded, losing the task instead of moving it. 4. With every live task registered before its first syscall and evicted in do_exit(), simplify pid_to_trace__has() and pid_filter__has() to single BPF hash map lookups, and move bpf_probe_read_kernel() in sys_exit back after the PID filter checks. 5. Pass the inherit flag from userspace to BPF .rodata via augmented_syscalls__prepare(!trace.opts.no_inherit), and split attaching out of it into augmented_syscalls__attach(), called from trace__run() once the pid, syscall and program array maps have all been programmed. These are system wide programs, so from the instant they attach they alone decide what is traced: attaching at load time, as before, left a window in which a target could fork without sched_process_fork() knowing the parent was a target, and with the userspace fork handling gone there was nothing left to recover it. The scheduler programs are attached ahead of sys_enter and sys_exit for the same reason. Set has_pids_filtered only after populating pids_filtered. A failure to attach ends the session, reporting the error that caused it. There is no falling back to unaugmented tracing by that point: cmd_trace() built the evlist around __augmented_syscalls__ and dropped the sys_enter evsel, so a session that carried on would report nothing at all. Neither did the code this replaces, which ignored the result of augmented_raw_syscalls_bpf__attach() altogether and ran on with programs that had never been attached. 6. Remove the userspace BPF map updates from PERF_RECORD_FORK and PERF_RECORD_EXIT in trace__process_event(), and delete the now-unused augmented_syscalls__{add,del,has}_target_pid() helpers. No coverage is lost with them: those records only come into being once the ring buffers are mapped by evlist__do_mmap() and the events are switched on by evlist__enable(), both of which run after augmented_syscalls__attach() in trace__run(), and they are then acted on later still, whenever the poll loop gets round to them. The scheduler programs therefore go live strictly earlier than the userspace path could ever have reacted. 7. Gate pid_filter__has() on a has_pids_filtered flag in .bss so the common case without --filter-pids performs no map lookups, and size pids_to_trace and pids_filtered at 16384 entries. pids_filtered is grown from 64 because it is no longer just the handful of pids userspace names: sched_process_fork() adds every descendant of those, so a --filter-pids target that forks or is heavily threaded needs the same headroom as a traced one. A fork or exit is still not seen if it happens before the programs are attached, that is between evlist__create_maps() scanning /proc for a -p target and augmented_syscalls__attach(). Such a window is inherent in programming a system wide filter before switching it on, and as above the userspace handling did not cover it either. The cost is not small though. Once the bpf-output event exists cmd_trace() removes the sys_enter evsel from the evlist, so __augmented_syscalls__ is the only source of enter events and a pid that is missing from pids_to_trace is not reported at all. sys_enter returning 1 keeps the kernel tracepoint alive for other subscribers, it does not give perf trace a second path to the event. A target that forks during perf trace's own startup can therefore have that child, and in turn everything the child forks, go untraced for the whole run. The tgid fallback that pid_to_trace__has() used to have would have masked part of this, since a thread missed in the window still shares the tgid of a target userspace did insert. It is not kept because it would also re-admit tasks that sched_process_fork() deliberately skipped: under --no-inherit a new thread of the target is not added to the map, yet it shares the target's tgid and a tgid test cannot tell it apart from one that was. sched_process_fork() does not consult the parent's tgid either, for the same reason. Every thread of a -p target is enumerated from /proc//task and inserted under its own pid, and -t names a single thread, so a tgid test adds no reach. What it would add is a child inheriting from a thread that is not traced itself: a sibling of the thread -t selected, or one missed in the attach window whose own syscalls go unreported. Inheritance keys off the pid of the thread that called clone(), exactly as the lookups do. Two of the three ways of selecting what to trace are unaffected: 'perf trace -- cmd' cannot hit this because evlist__prepare_workload() leaves the child blocked on a pipe until evlist__start_workload(), well after the attach, and 'perf trace -a' never sets has_pids_to_trace so it does not filter at all. It is 'perf trace -p' against an already running target that is exposed. Closing that too needs the descendants of the target re-enumerated from /proc after the attach, which the next change does. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/builtin-trace.c | 55 +++-- .../bpf_skel/augmented_raw_syscalls.bpf.c | 199 +++++++++++++++++- tools/perf/util/bpf_trace_augment.c | 120 +++++++---- tools/perf/util/trace_augment.h | 28 +-- 4 files changed, 304 insertions(+), 98 deletions(-) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index d403f1a318e3..003fc13ab6d5 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -2061,23 +2061,6 @@ static int trace__process_event(struct trace *trace,= struct machine *machine, "LOST %" PRIu64 " events!\n", (u64)event->lost.lost); ret =3D machine__process_lost_event(machine, event, sample); break; - case PERF_RECORD_FORK: - if (trace->raw_augmented_syscalls && - (augmented_syscalls__has_target_pid(event->fork.ppid) || - augmented_syscalls__has_target_pid(event->fork.ptid))) { - augmented_syscalls__add_target_pid(event->fork.pid); - } - ret =3D machine__process_fork_event(machine, event, sample); - break; - case PERF_RECORD_EXIT: - if (trace->raw_augmented_syscalls) { - if (event->fork.pid =3D=3D event->fork.tid) - augmented_syscalls__del_target_pid(event->fork.pid); - else - augmented_syscalls__del_target_pid(event->fork.tid); - } - ret =3D machine__process_exit_event(machine, event, sample); - break; default: ret =3D machine__process_event(machine, event, sample); break; @@ -5015,6 +4998,22 @@ static int trace__run(struct trace *trace, int argc,= const char **argv) } } =20 + /* + * Everything the BPF programs filter on is now in their maps, so it is + * safe to let them run. They are attached system wide, so anything + * before this point would have been filtered against a map that was + * still being built up. + * + * Falling back to unaugmented tracing is no longer possible here: the + * evlist was built around __augmented_syscalls__ back in cmd_trace(), + * which is where that decision is taken and where the sys_enter evsel + * was dropped. Fail the session rather than run one that can report + * nothing. + */ + err =3D augmented_syscalls__attach(); + if (err < 0) + goto out_error_attach; + /* * If the "close" syscall is not traced, then we will not have the * opportunity to, in syscall_arg__scnprintf_close_fd() invalidate the @@ -5209,6 +5208,16 @@ static int trace__run(struct trace *trace, int argc,= const char **argv) fprintf(trace->output, "Failed to set the syscall filters: %s\n", str_error_r(-err, errbuf, sizeof(errbuf))); goto out_put_evlist; + +out_error_attach: + /* + * Use the returned error rather than errno: the failing attach is + * unwound before returning, and the libbpf calls that does can leave + * errno describing something else entirely. + */ + fprintf(trace->output, "Failed to attach the augmented syscalls BPF progr= ams: %s\n", + str_error_r(-err, errbuf, sizeof(errbuf))); + goto out_put_evlist; } out_error_mem: fprintf(trace->output, "Not enough memory to run!\n"); @@ -6125,7 +6134,7 @@ int cmd_trace(int argc, const char **argv) goto skip_augmentation; } =20 - err =3D augmented_syscalls__prepare(); + err =3D augmented_syscalls__prepare(!trace.opts.no_inherit); if (err < 0) goto skip_augmentation; =20 @@ -6148,11 +6157,11 @@ int cmd_trace(int argc, const char **argv) trace.syscalls.events.bpf_output =3D evlist__last(trace.evlist); } else { /* - * augmented_syscalls__prepare() already attached sys_enter and - * sys_exit, which are system wide. Falling through to - * skip_augmentation without undoing that would run a BPF - * program for every syscall on the machine, for the whole - * session, with nothing consuming the output. + * Drop the loaded skeleton before falling back to unaugmented + * tracing. Otherwise the setters called from trace__run() would + * still program its maps, and augmented_syscalls__attach() would + * then put system wide BPF programs on raw_syscalls for a + * session with nothing consuming their output. */ pr_debug("Failed to create the augmented syscalls bpf-output event, disa= bling augmentation\n"); augmented_syscalls__cleanup(); diff --git a/tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c b/tools/= perf/util/bpf_skel/augmented_raw_syscalls.bpf.c index 6ca9507ecc02..af04c4b3f445 100644 --- a/tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +++ b/tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c @@ -9,6 +9,7 @@ #include "vmlinux.h" =20 #include +#include #include =20 #define PERF_ALIGN(x, a) __PERF_ALIGN_MASK(x, (typeof(x))(a)-1) @@ -107,25 +108,48 @@ struct augmented_arg { }; }; =20 +/* + * Hash map of PIDs/TGIDs whose events must be discarded, e.g. perf trace'= s own + * pid, so that tracing doesn't feed back on itself. + * + * has_pids_filtered: set to true only when the map is populated. Checking= a + * boolean is much cheaper than a map lookup, and sys_e= nter + * runs for every syscall on the system, so the common + * "no pids filtered" case must stay on a fast path. + * + * max_entries matches pids_to_trace: userspace only ever names a handful = of + * pids here, but sched_process_fork() below adds every descendant of thos= e, + * so a --filter-pids target that forks or is heavily threaded needs the s= ame + * headroom as a traced one. + */ struct pids_filtered { __uint(type, BPF_MAP_TYPE_HASH); __type(key, pid_t); __type(value, bool); - __uint(max_entries, 64); + __uint(max_entries, 16384); } pids_filtered SEC(".maps"); =20 +bool has_pids_filtered; + /* * Optional hash map containing specific PIDs/TGIDs to trace (e.g., when * attached to a process with -p or tracing a specific command workload). * * has_pids_to_trace: Set to true if target PID filtering is active. * When false, all processes are eligible for tracing. + * + * max_entries bounds how many tasks can be tracked at once. sched_process= _exit + * below evicts a task as it dies, whatever it died of, so the map holds l= ive + * tasks rather than growing without bound. It is sized well + * above the thread count of realistic traced workloads; should a workload + * still exceed it, bpf_map_update_elem() fails with -E2BIG and the extra + * tasks are simply not traced rather than anything being corrupted. */ struct pids_to_trace { __uint(type, BPF_MAP_TYPE_HASH); __type(key, pid_t); __type(value, bool); - __uint(max_entries, 1024); + __uint(max_entries, 16384); } pids_to_trace SEC(".maps"); =20 bool has_pids_to_trace; @@ -149,6 +173,9 @@ struct syscalls_to_trace { bool has_syscalls_to_trace; bool not_syscalls_to_trace; =20 +/* Inherit tracing for child tasks (set to false if --no-inherit is specif= ied) */ +const volatile bool inherit =3D true; + struct augmented_args_payload { struct syscall_enter_args args; struct augmented_arg arg, arg2; // We have to reserve space for two argum= ents (rename, etc) @@ -471,24 +498,35 @@ static pid_t getpid(void) } =20 /* - * Returns true if a PID is explicitly excluded/filtered out (e.g., via --= filter-pids). + * Checks if a PID is explicitly excluded/filtered out (e.g., via --filter= -pids). + * + * Children of a filtered task are added to the map by sched_process_fork() + * below, so a plain lookup is all that is needed here. */ static bool pid_filter__has(struct pids_filtered *pids, pid_t pid) { + /* + * Fast path: this runs for every syscall on the system, so when no pid + * is filtered do no work at all rather than failing a lookup. + */ + if (!has_pids_filtered) + return false; + return bpf_map_lookup_elem(pids, &pid) !=3D NULL; } =20 /* - * Checks if the current task (thread PID or process TGID) is targeted for= tracing. - * Checks both PID (thread ID) and TGID (process ID) so that all threads o= f a - * target process match. + * Checks if the current task is targeted for tracing. + * + * Every thread that existed when tracing started was named by the target = and + * inserted from userspace, and every task created since was inserted by + * sched_process_fork() below, before it was able to run. So there is noth= ing + * to derive here, and in particular no need to consult the tgid or walk t= o the + * parent: a task is traced if and only if it is in the map. */ static inline bool pid_to_trace__has(pid_t pid) { - pid_t tgid =3D bpf_get_current_pid_tgid() >> 32; - - return bpf_map_lookup_elem(&pids_to_trace, &pid) !=3D NULL || - bpf_map_lookup_elem(&pids_to_trace, &tgid) !=3D NULL; + return bpf_map_lookup_elem(&pids_to_trace, &pid) !=3D NULL; } =20 /* @@ -706,6 +744,7 @@ int sys_exit(struct syscall_exit_args *args) return 1; =20 bpf_probe_read_kernel(&exit_args, sizeof(exit_args), args); + /* * Jump to syscall specific return augmenter, even if the default one, * "!raw_syscalls:unaugmented" that will just return 1 to return the @@ -721,4 +760,144 @@ int sys_exit(struct syscall_exit_args *args) return 1; } =20 +/* + * Propagate tracing to a newly created task. + * + * tp_btf/sched_process_fork is raised by copy_process(), in the parent's + * context and before the child is woken, so the child is in the maps befo= re it + * can issue its first syscall. That removes the need to inspect real_pare= nt + * when a syscall is seen from an unknown task, which could neither tell a + * genuine descendant from a task merely reparented to a traced init, nor = keep + * following a descendant whose parent had already exited. + * + * Using tp_btf rather than tp/sched/sched_process_fork avoids depending o= n the + * tracepoint ring-buffer record layout (TP_STRUCT__entry), which changed = in + * Linux 6.16 when parent_comm and child_comm were converted from fixed 16= -byte + * arrays to 4-byte __data_loc strings (shrinking the tracepoint context f= rom + * 48 to 24 bytes and causing BPF_PROG_TYPE_TRACEPOINT attachment to fail = with + * -EACCES when accessing higher offsets). Instead, tp_btf receives the st= able + * TP_PROTO arguments (struct task_struct *parent, struct task_struct *chi= ld) + * directly. + * + * child->pid is task_struct.pid, i.e. the pid in the initial namespace, w= hich + * is what the maps are keyed by. A clone() return value, in contrast, is = the + * pid in the caller's namespace and would alias an unrelated host task wh= en a + * containerised workload is traced. + * + * CLONE_THREAD needs no special handling: a new thread arrives here like = any + * other task and is inserted under its own pid. + */ +SEC("tp_btf/sched_process_fork") +int BPF_PROG(sched_process_fork, struct task_struct *parent, struct task_s= truct *child) +{ + pid_t parent_pid, child_pid; + bool val =3D true; + + if (!inherit) + return 0; + + /* + * Inherit from the thread that called clone() and from nothing else. + * The maps name individual tasks: pid_to_trace__has() and + * pid_filter__has() look up a task's own pid and nothing more, and + * every thread of a -p target is enumerated from /proc//task and + * inserted in its own right, so a traced thread is always here under + * its own key. Consulting the parent's tgid as well would let a child + * inherit from a thread that is not itself traced, which is precisely + * what 'perf trace -t ' asked to leave out, and would re-admit + * descendants of a thread that sched_process_fork() skipped or that + * was missed while the programs were being attached, while still not + * tracing that thread itself. + */ + parent_pid =3D parent->pid; + child_pid =3D child->pid; + + if (has_pids_to_trace && + bpf_map_lookup_elem(&pids_to_trace, &parent_pid) !=3D NULL) + bpf_map_update_elem(&pids_to_trace, &child_pid, &val, BPF_ANY); + + if (has_pids_filtered && + bpf_map_lookup_elem(&pids_filtered, &parent_pid) !=3D NULL) + bpf_map_update_elem(&pids_filtered, &child_pid, &val, BPF_ANY); + + return 0; +} + +/* + * Drop a dying task from the maps. + * + * tp_btf/sched_process_exit is raised by do_exit() for every task, in its= own + * context, so unlike hooking the exit and exit_group syscalls this also c= overs + * tasks killed by a signal and threads torn down implicitly by exit_group. + * + * Doing it here rather than from the userspace PERF_RECORD_EXIT handler a= lso + * means there is no window between the task dying and the map being updat= ed, + * during which the kernel could recycle the pid and the late eviction sil= ently + * stop tracing whichever new task received it. + * + * Each thread is reported separately, including the group leader, whose p= id is + * the thread group's tgid, so one delete per map covers both uses of the = key. + */ +SEC("tp_btf/sched_process_exit") +int BPF_PROG(sched_process_exit, struct task_struct *p) +{ + pid_t pid =3D p->pid; + + bpf_map_delete_elem(&pids_to_trace, &pid); + bpf_map_delete_elem(&pids_filtered, &pid); + + return 0; +} + +/* + * Follow a task whose pid changed under it. + * + * When a thread that is not the thread group leader execs, de_thread() ki= lls + * the rest of the group and then hands the leader's pid, which is the tgi= d, to + * the exec'ing thread. The leader dies first, so sched_process_exit() abo= ve + * has already dropped that pid from the maps, and the survivor is now key= ed by + * a pid nothing knows about while its original entry is left behind for g= ood. + * + * Move the entry across so the task stays tracked and nothing is leaked. + * old_pid is sampled in bprm_execve() before de_thread() runs, so for the + * common case of the group leader exec'ing it simply equals p->pid and th= ere + * is nothing to do. + */ +SEC("tp_btf/sched_process_exec") +int BPF_PROG(sched_process_exec, struct task_struct *p, pid_t old_pid) +{ + pid_t pid =3D p->pid; + bool val =3D true; + + if (pid =3D=3D old_pid) + return 0; + + /* + * Drop the old key before adding the new one. The maps are bounded and + * the move is only ever a rename, but inserting first needs a spare + * slot for as long as both keys are present: on a full map that insert + * fails with -E2BIG while the delete still succeeds, which would lose + * the task rather than move it. Deleting first frees the slot the + * insert goes on to use. The task is mid exec and issues no syscalls in + * between, so the gap is not observable. + * + * There is no atomic rename for a hash map, so on a map that is exactly + * full this narrows the window rather than closing it: a fork on + * another CPU can still take the freed slot before the insert below + * runs, and the task is then dropped just as any other task is once the + * map is full. + */ + if (bpf_map_lookup_elem(&pids_to_trace, &old_pid) !=3D NULL) { + bpf_map_delete_elem(&pids_to_trace, &old_pid); + bpf_map_update_elem(&pids_to_trace, &pid, &val, BPF_ANY); + } + + if (bpf_map_lookup_elem(&pids_filtered, &old_pid) !=3D NULL) { + bpf_map_delete_elem(&pids_filtered, &old_pid); + bpf_map_update_elem(&pids_filtered, &pid, &val, BPF_ANY); + } + + return 0; +} + char _license[] SEC("license") =3D "GPL"; diff --git a/tools/perf/util/bpf_trace_augment.c b/tools/perf/util/bpf_trac= e_augment.c index 5f15b27264e9..44f30dba5469 100644 --- a/tools/perf/util/bpf_trace_augment.c +++ b/tools/perf/util/bpf_trace_augment.c @@ -30,7 +30,7 @@ static int attach_prog(struct bpf_link **link, struct bpf= _program *prog, const c return attach_err; } =20 -int augmented_syscalls__prepare(void) +int augmented_syscalls__prepare(bool inherit) { struct bpf_program *prog; char buf[128]; @@ -42,12 +42,18 @@ int augmented_syscalls__prepare(void) return -errno; } =20 + skel->rodata->inherit =3D inherit; + /* - * Disable attaching the BPF programs except for sys_enter and - * sys_exit that tail call into this as necessary. + * Disable attaching the BPF programs other than those attached + * explicitly by augmented_syscalls__attach(), the rest are reached by + * tail calls. */ bpf_object__for_each_program(prog, skel->obj) { - if (prog !=3D skel->progs.sys_enter && prog !=3D skel->progs.sys_exit) + if (prog !=3D skel->progs.sys_enter && prog !=3D skel->progs.sys_exit && + prog !=3D skel->progs.sched_process_fork && + prog !=3D skel->progs.sched_process_exit && + prog !=3D skel->progs.sched_process_exec) bpf_program__set_autoattach(prog, /*autoattach=3D*/false); } =20 @@ -65,13 +71,42 @@ int augmented_syscalls__prepare(void) return err; } =20 + return 0; +} + +int augmented_syscalls__attach(void) +{ + int err; + + if (skel =3D=3D NULL) + return 0; + /* - * Only sys_enter and sys_exit are attached, the remaining programs are - * reached by tail calls. Attach them explicitly and, on failure, undo - * any partial attachment: leaving sys_enter live on - * raw_syscalls:sys_enter would keep running a BPF program for every - * syscall on the system for a perf trace session that never starts. + * Attaching is deliberately separate from, and a lot later than, + * loading: these are system wide tracepoint programs, so from the + * moment they are attached they are the only thing deciding which + * tasks and syscalls are traced. Going live before the pid and + * syscall maps are populated would mean a target that forked in the + * meantime was never picked up by sched_process_fork() below. + * + * Attach explicitly, so that a failure part way through can undo what + * came before it: leaving sys_enter live on raw_syscalls:sys_enter + * would keep running a BPF program for every syscall on the system for + * a perf trace session that never starts. + * + * The scheduler programs maintain the pid maps, and are attached first + * so that no fork, exit or exec can be missed between sys_enter going + * live and the maps being maintained. */ + if (attach_prog(&skel->links.sched_process_fork, skel->progs.sched_proces= s_fork, + "sched_process_fork")) + goto out_cleanup; + if (attach_prog(&skel->links.sched_process_exit, skel->progs.sched_proces= s_exit, + "sched_process_exit")) + goto out_cleanup; + if (attach_prog(&skel->links.sched_process_exec, skel->progs.sched_proces= s_exec, + "sched_process_exec")) + goto out_cleanup; if (attach_prog(&skel->links.sys_enter, skel->progs.sys_enter, "sys_enter= ")) goto out_cleanup; if (attach_prog(&skel->links.sys_exit, skel->progs.sys_exit, "sys_exit")) @@ -83,6 +118,13 @@ int augmented_syscalls__prepare(void) err =3D attach_err; /* Destroys every link attached above along with the skeleton. */ augmented_syscalls__cleanup(); + /* + * Tearing the skeleton down closes file descriptors and frees memory, + * either of which may overwrite errno. Restore it so that a caller + * reporting this with "%m" describes the attach failure rather than + * whatever the teardown happened to do last. + */ + errno =3D -err; return err; } =20 @@ -164,10 +206,28 @@ static int add_pids_to_map(struct bpf_map *map, const= char *missing_out_on, =20 int augmented_syscalls__set_filter_pids(unsigned int nr, pid_t *pids) { - if (skel =3D=3D NULL) + int err; + + if (skel =3D=3D NULL || nr =3D=3D 0) return 0; =20 - return add_pids_to_map(skel->maps.pids_filtered, "filtered out", nr, pids= ); + err =3D add_pids_to_map(skel->maps.pids_filtered, "filtered out", nr, pid= s); + if (err) + return err; + + /* + * Publish the filter only now that the map is populated. + * augmented_syscalls__attach() has not run yet, so nothing is reading + * either of them, but keeping the flag and the map consistent means + * the ordering stays correct however the callers are rearranged. + * + * The flag also tells the BPF program that the pids_filtered map is in + * use. Without it the program would have to look up every task in an + * empty map, on every syscall on the system, to find out that nothing + * is filtered. + */ + skel->bss->has_pids_filtered =3D true; + return 0; } =20 /* @@ -186,45 +246,15 @@ int augmented_syscalls__set_target_pids(unsigned int = nr, pid_t *pids) return err; =20 /* - * Set the flag only once every target is in the map. The BPF programs - * are attached by this point, so flipping it first would have them - * filter against a partially populated map and drop syscalls made by - * the targets that had not been added yet. + * Set the flag only once every target is in the map, so that the two + * are never inconsistent. Publishing it first would, once the + * programs are attached, have them filter against a partially + * populated map and drop syscalls made by targets not yet added. */ skel->bss->has_pids_to_trace =3D true; return 0; } =20 -int augmented_syscalls__add_target_pid(pid_t pid) -{ - bool value =3D true; - - if (skel =3D=3D NULL || !skel->bss->has_pids_to_trace || skel->maps.pids_= to_trace =3D=3D NULL) - return 0; - - return bpf_map__update_elem(skel->maps.pids_to_trace, &pid, sizeof(pid), - &value, sizeof(value), BPF_ANY); -} - -int augmented_syscalls__del_target_pid(pid_t pid) -{ - if (skel =3D=3D NULL || !skel->bss->has_pids_to_trace || skel->maps.pids_= to_trace =3D=3D NULL) - return 0; - - return bpf_map__delete_elem(skel->maps.pids_to_trace, &pid, sizeof(pid), = 0); -} - -bool augmented_syscalls__has_target_pid(pid_t pid) -{ - bool value; - - if (skel =3D=3D NULL || !skel->bss->has_pids_to_trace || skel->maps.pids_= to_trace =3D=3D NULL) - return false; - - return bpf_map__lookup_elem(skel->maps.pids_to_trace, &pid, sizeof(pid), - &value, sizeof(value), 0) =3D=3D 0; -} - /* * Populate syscalls in the BPF syscalls_to_trace map: * - not_syscalls: true if '!' prefix was specified (blacklist mode: trace diff --git a/tools/perf/util/trace_augment.h b/tools/perf/util/trace_augmen= t.h index 5702eda3b469..ad992f5fa726 100644 --- a/tools/perf/util/trace_augment.h +++ b/tools/perf/util/trace_augment.h @@ -10,14 +10,12 @@ struct evlist; =20 #ifdef HAVE_BPF_SKEL =20 -int augmented_syscalls__prepare(void); +int augmented_syscalls__prepare(bool inherit); +int augmented_syscalls__attach(void); int augmented_syscalls__create_bpf_output(struct evlist *evlist); void augmented_syscalls__setup_bpf_output(void); int augmented_syscalls__set_filter_pids(unsigned int nr, pid_t *pids); int augmented_syscalls__set_target_pids(unsigned int nr, pid_t *pids); -int augmented_syscalls__add_target_pid(pid_t pid); -int augmented_syscalls__del_target_pid(pid_t pid); -bool augmented_syscalls__has_target_pid(pid_t pid); int augmented_syscalls__set_target_syscalls(unsigned int nr, int *syscall_= ids, bool not_syscalls); int augmented_syscalls__get_map_fds(int *enter_fd, int *exit_fd, int *beau= ty_fd); struct bpf_program *augmented_syscalls__find_by_title(const char *name); @@ -26,11 +24,16 @@ void augmented_syscalls__cleanup(void); =20 #else /* !HAVE_BPF_SKEL */ =20 -static inline int augmented_syscalls__prepare(void) +static inline int augmented_syscalls__prepare(bool inherit __maybe_unused) { return -1; } =20 +static inline int augmented_syscalls__attach(void) +{ + return 0; +} + static inline int augmented_syscalls__create_bpf_output(struct evlist *evl= ist __maybe_unused) { return -1; @@ -52,21 +55,6 @@ static inline int augmented_syscalls__set_target_pids(un= signed int nr __maybe_un return 0; } =20 -static inline int augmented_syscalls__add_target_pid(pid_t pid __maybe_unu= sed) -{ - return 0; -} - -static inline int augmented_syscalls__del_target_pid(pid_t pid __maybe_unu= sed) -{ - return 0; -} - -static inline bool augmented_syscalls__has_target_pid(pid_t pid __maybe_un= used) -{ - return false; -} - static inline int augmented_syscalls__set_target_syscalls(unsigned int nr = __maybe_unused, int *syscall_ids __maybe_unused, bool not_syscalls __maybe_unused) --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Thu Sep 24 22:56:31 2026 Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9767952CCE7 for ; Fri, 18 Sep 2026 21:20:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.70 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766414; cv=none; b=R1E9XKhM+MLc4i1buv/OZFEWWjN8yH4mu4Ywt80ifGkQen3SeC4+SA5DZ9dwGO4ldxuADMyJPqLaVJHYKOWUULpGcW0ROo8b03ur0vhJFK0piXuVTfWIOnCenmErbFwX8w9NwaTvDhOiLBWeZOvlcSCXUH4cvQAeYhrFrXl7d54= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766414; c=relaxed/simple; bh=y52I0+2wC2aeCPJBxrpEXfYWJ70b57Evf1tboydSXi8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gniu0vWKeve13EjdaAMLIGDNpY6Go3Y5VKhRsnAR1uAMd9zVxu/YUSmXmQ+mFrguImmu5rzx5S/MVvW5sC58snqBxxecJXBDMuXLQECQocnTVJudTHBmY1LpY13Ca1YOEKQCYIR+a6PfasI24l3+Ou6+MDqG4SMRDlxdCy6nfzU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=sUcftpce; arc=none smtp.client-ip=209.85.216.70 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.b="sUcftpce" Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-398dc3d8f0fso2281007a91.0 for ; Fri, 18 Sep 2026 14:20:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789766411; x=1790371211; 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=67VxQxiws94OnJN3ty0YuYbwH2hWPNDE/oCs9I9YxvM=; b=sUcftpceklWMcKOp453v5mXiak+uR9FVR5pytDc6ptI9EItIuJVJSq3tTV5jj9bkQm iOaL6a0ut8PabEjlZowWCgVdLAjBZaqEmquomOvt6EWKm9VVahbU5yvExOSELNBvzsfd abjsNItJyIiZgPrn/pIxJTVcyu037qWQE0cqH+tLA4z82qzA4pidaAWfGkvC7vLR1ad7 Dr5PT7BbfB1wfpIdxBvRgkGdPMJD7/TbON5qOAgFNbo5Y6M0DpsxktdSEvZXJDcDDbvC W/6u+cJ9t0xvY2emkhCGPIcvRHlKesEA5S/MXqaBJIp1RlA9+8QXYeNzEsvc7LT/wBZ/ ocZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789766411; x=1790371211; 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=67VxQxiws94OnJN3ty0YuYbwH2hWPNDE/oCs9I9YxvM=; b=Z+oymKkW2l6SDIi2xbojtcbi5cpkYBMhXu7imi6DaxIVtBEjhoRq5auO48/2agrkMV 3dB3jXl/OpQYv9qz6eytKeRf7B3ZZPtJ/OZz4oKJBFhqpmYES98UDQ74HbFIvLX1guJ3 RHiNo6mZBOuQzstYcH5jVbZnbwB5KOWZe4TiYvetPZjh5tiAGlmb+EdOEJ1VuYCoW5dx H43BM5rl4v9uAUXaoI1vkaoU2itJr1w2MTPUFwc3Tl0HEZY3B9cMmLfNDYU/V8Sj7gBc ERw42hSOuFkh8OhXQ4AmnBSUWUbhIH1QSyPzmL5v6SE8oR4YOI/oWlkIyyJ+wb0Sw54I WbZg== X-Forwarded-Encrypted: i=1; AKwUvBzgQ7DsCKkzxN0cioHbP3hH3nSYf+8MODLn9MKuOu6XiBGh99g0Q+dgN5XlWggNjr5sxzMu3Q4KeIAxPZA=@vger.kernel.org X-Gm-Message-State: AFuF++nKEUhsTbXd2EMN0Q3DnY07m0dG8QMKtlCNr9no714aGg2E1pP/ DIDNFYPcMVFS37WLPG2EqPpNcA5cm+gYipOVkrljfGgSyGAA82DcbTMfijc2APc0CvSkhM90Kv2 ToZehxusZIA== X-Received: from dlaf10.prod.google.com ([2002:a05:701b:240a:b0:144:be75:f389]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4d0e:b0:39d:feca:f48d with SMTP id 98e67ed59e1d1-39e5546a95cmr4216833a91.3.1789766410712; Fri, 18 Sep 2026 14:20:10 -0700 (PDT) Date: Fri, 18 Sep 2026 14:19:23 -0700 In-Reply-To: <20260918211932.2966061-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918140659.2501976-1-irogers@google.com> <20260918211932.2966061-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918211932.2966061-10-irogers@google.com> Subject: [PATCH v4 09/18] perf trace: Enumerate the target again once BPF is attached From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org, Howard Chu Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" evlist__create_maps() reads the target out of /proc, and the BPF sched_process_fork() program only sees what is cloned once it is attached. A task the target creates between the two is in neither, and since cmd_trace() drops the sys_enter evsel in favour of __augmented_syscalls__ there is no other source of enter events. Such a task, and in turn everything it forks, goes unreported for the rest of the session. Read the target out of /proc once more, after the attach. Whatever existed before the programs went live is there to be found, and whatever is created after it is sched_process_fork()'s to add, so between them nothing is left out. Doing this before the attach instead would only move the window rather than close it. The enumeration follows the same rule as the BPF programs, which is that the maps name individual tasks: - -p names a process, so its thread group is read from /proc//task. - -t names a thread, which is taken on its own. Expanding it to its thread group would trace the siblings it asked to leave out. - Descendants come from task->children, read through /proc//task//children. A forked task leads a thread group of its own, so each one found is walked in turn and a tree of any depth is covered. New threads are not listed there, copy_process() gives a CLONE_THREAD child the real_parent of its creator rather than the creator itself, but the thread group walk above has them. The other two ways of choosing what to trace need nothing, for the same reasons the window never affected them: 'perf trace -a' does not filter on pid at all, and evlist__prepare_workload() keeps a workload blocked on a pipe until evlist__start_workload(), well after the attach. A target that exits during startup is not an error. Reading /proc for a task that has gone fails with ENOENT, and a task directory that is read but has nothing in it sets nothing at all, so errno is cleared before the enumeration and only an allocation failure is passed back. Anything else leaves the tasks that evlist__create_maps() already found in the map, which sched_process_exit() takes out again as they die, and the session runs on rather than being ended over a target that was going to stop producing events anyway. What is left is smaller and no longer lasts. A task found here may have made syscalls between sys_enter going live and it being added to the map, and those are not reported, but it is traced from that point on. On a kernel built without CONFIG_PROC_CHILDREN the children files are absent and descendants cannot be named, leaving the threads of the target, which are still picked up. pid_t, PATH_MAX, FILE and the directory reading are all used directly by the new code, so , , and are included rather than relied upon to arrive through another header. Nothing is read out of /proc twice. A pid is in the collection only because the task directory holding it was read, and that directory holds the whole thread group, so both the directory and the children files of everything in it have been read already. 'perf trace -p' needs this: the thread map names every thread of the target, each of them is queued as something to expand, and they all stand for the same directory, so a target of N threads was read N times over and a children file was opened N squared times. On a 64 thread target that is 4097 of them, against 65 once the ones already read are left alone. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/builtin-trace.c | 248 +++++++++++++++++++++++++++++++++++++ 1 file changed, 248 insertions(+) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index 003fc13ab6d5..8a5dbf144540 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -15,6 +15,7 @@ */ =20 #include "util/record.h" +#include #include #ifdef HAVE_LIBBPF_SUPPORT #include @@ -65,11 +66,15 @@ #include "trace_augment.h" #include "dwarf-regs.h" =20 +#include #include #include +#include #include +#include #include #include +#include #include #include #include @@ -4648,6 +4653,239 @@ static int trace__set_filter_pids(struct trace *tra= ce) return err; } =20 +/* A list of pids that grows as it is added to, holding each pid once. */ +struct pid_list { + pid_t *entries; + size_t nr; + size_t allocated; +}; + +static bool pid_list__has(const struct pid_list *list, pid_t pid) +{ + for (size_t i =3D 0; i < list->nr; i++) { + if (list->entries[i] =3D=3D pid) + return true; + } + return false; +} + +/* Append pid, unless it is already there. */ +static int pid_list__add(struct pid_list *list, pid_t pid) +{ + if (pid_list__has(list, pid)) + return 0; + + if (list->nr =3D=3D list->allocated) { + size_t allocated =3D list->allocated ? list->allocated * 2 : 32; + pid_t *entries =3D realloc(list->entries, allocated * sizeof(*entries)); + + if (entries =3D=3D NULL) + return -ENOMEM; + + list->entries =3D entries; + list->allocated =3D allocated; + } + + list->entries[list->nr++] =3D pid; + return 0; +} + +static void pid_list__exit(struct pid_list *list) +{ + zfree(&list->entries); + list->nr =3D 0; + list->allocated =3D 0; +} + +/* + * Append the tasks tid has forked to tgids. + * + * task->children holds what a task forked, and a forked task leads a thre= ad + * group of its own, so each is something to expand in turn. New threads a= re + * not listed: copy_process() gives a CLONE_THREAD child the real_parent of + * its creator rather than the creator itself, so a thread is a sibling of= the + * task that created it. Those are enumerated from the task directory inst= ead. + */ +static int pid_list__add_children(struct pid_list *tgids, pid_t tid) +{ + char path[PATH_MAX]; + pid_t child; + FILE *fp; + int err =3D 0; + + scnprintf(path, sizeof(path), "%s/%d/task/%d/children", + procfs__mountpoint(), tid, tid); + fp =3D fopen(path, "r"); + /* + * Absent if the task exited, and on a kernel built without + * CONFIG_PROC_CHILDREN. Neither is worth failing for: what is missed + * is a task that has gone away, or descendants the kernel will not + * name. + */ + if (fp =3D=3D NULL) + return 0; + + while (fscanf(fp, "%d", &child) =3D=3D 1) { + err =3D pid_list__add(tgids, child); + if (err) + break; + } + + fclose(fp); + return err; +} + +/* + * Collect the tasks to trace: the target, its threads, and everything they + * have forked. + * + * tgids is the queue of thread groups still to expand. It is walked as it + * grows, so a child found here has its own children picked up in a later + * pass and the depth of the tree does not matter. The walk terminates + * because a task cannot be its own ancestor and pid_list__add() ignores a + * pid that is already listed. + */ +static int trace__collect_target_pids(struct trace *trace, struct pid_list= *pids) +{ + struct target *target =3D &trace->opts.target; + /* + * -p names processes, so the whole thread group is a target. -t names + * threads, and expanding one to its group would trace the siblings + * that were deliberately left out. + */ + bool whole_group =3D target->pid !=3D NULL; + struct perf_thread_map *threads; + struct pid_list tgids =3D {}; + int err =3D 0; + + /* Enumerate the target as evlist__create_maps() did, but now. */ + errno =3D 0; + threads =3D thread_map__new_str(target->pid, target->tid, target->per_thr= ead); + if (threads =3D=3D NULL) { + char bf[128]; + + /* + * A target that exited while perf trace was starting up shows + * up here as a failure to read /proc//task, with scandir() + * setting ENOENT, or as a task directory that is read but has + * nothing in it, which sets nothing at all and is why errno is + * cleared above. Neither is worth ending the session for: the + * tasks the target had are already in the map from + * evlist__create_maps() and sched_process_exit() takes them out + * again as they die. Carry on with what is known and let only + * an allocation failure through, matching how the pid_list + * additions below are treated. + */ + if (errno =3D=3D ENOMEM) + return -ENOMEM; + + pr_debug("Couldn't enumerate the target again (%s), tracing the tasks al= ready known\n", + errno =3D=3D 0 ? "it exited" : str_error_r(errno, bf, sizeof(bf))); + return 0; + } + + for (int i =3D 0; i < perf_thread_map__nr(threads); i++) { + pid_t pid =3D perf_thread_map__pid(threads, i); + + err =3D pid_list__add(whole_group ? &tgids : pids, pid); + /* A thread named by -t is not expanded, but its children are. */ + if (!err && !whole_group) + err =3D pid_list__add_children(&tgids, pid); + if (err) + goto out; + } + + for (size_t i =3D 0; i < tgids.nr; i++) { + pid_t tgid =3D tgids.entries[i]; + char path[PATH_MAX]; + struct dirent *dent; + DIR *tasks; + + /* + * A pid is in pids only because the task directory holding it + * was read, and that directory holds the whole thread group, + * so this one has been read already. 'perf trace -p' relies on + * this: the thread map names every thread of the target, each + * of them is queued here, and they all stand for the same + * directory, which would otherwise be read once per thread. + */ + if (pid_list__has(pids, tgid)) + continue; + + scnprintf(path, sizeof(path), "%s/%d/task", procfs__mountpoint(), tgid); + tasks =3D opendir(path); + if (tasks =3D=3D NULL) + continue; /* Exited between being named and being read. */ + + while ((dent =3D readdir(tasks)) !=3D NULL) { + char *end; + pid_t tid =3D strtol(dent->d_name, &end, 10); + + /* Skip "." and "..". */ + if (*end !=3D '\0') + continue; + + /* Seen before, so its children have been read too. */ + if (pid_list__has(pids, tid)) + continue; + + err =3D pid_list__add(pids, tid); + if (!err) + err =3D pid_list__add_children(&tgids, tid); + if (err) + break; + } + + closedir(tasks); + if (err) + goto out; + } +out: + perf_thread_map__put(threads); + pid_list__exit(&tgids); + return err; +} + +/* + * Trace the tasks that appeared while perf trace was starting up. + * + * evlist__create_maps() enumerated the target from /proc, and + * sched_process_fork() only sees what is cloned once it is attached. A ta= sk + * created in between is in neither, and since cmd_trace() drops the sys_e= nter + * evsel in favour of __augmented_syscalls__ it would go unreported for the + * whole session. + * + * Read the target out of /proc again now that the programs are attached. + * Whatever came before the attach is in /proc to be found, and whatever c= omes + * after it is sched_process_fork()'s to add, so between them nothing is l= eft + * out. + * + * Doing this after the attach rather than before it is what makes that tr= ue; + * before it would only move the window. The cost is that a task found here + * may have made syscalls between sys_enter going live and it being added + * below, and those are not reported. It is traced from that point on. + */ +static int trace__set_startup_pids(struct trace *trace) +{ + struct pid_list pids =3D {}; + int err; + + /* + * Nothing to do without a target: 'perf trace -a' does not filter on + * pid at all, and evlist__prepare_workload() keeps a workload blocked + * on a pipe until evlist__start_workload(), well after the attach. + */ + if (!target__has_task(&trace->opts.target)) + return 0; + + err =3D trace__collect_target_pids(trace, &pids); + if (!err) + err =3D augmented_syscalls__set_target_pids(pids.nr, pids.entries); + + pid_list__exit(&pids); + return err; +} + static int __trace__deliver_event(struct trace *trace, union perf_event *e= vent) { struct evlist *evlist =3D trace->evlist; @@ -5014,6 +5252,16 @@ static int trace__run(struct trace *trace, int argc,= const char **argv) if (err < 0) goto out_error_attach; =20 + /* + * With the programs live, anything the target created while they were + * being set up is now sched_process_fork()'s to keep track of, but it + * was not there to see it appear. Enumerate the target once more to + * pick those up. + */ + err =3D trace__set_startup_pids(trace); + if (err < 0) + goto out_error_filter_pids; + /* * If the "close" syscall is not traced, then we will not have the * opportunity to, in syscall_arg__scnprintf_close_fd() invalidate the --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Thu Sep 24 22:56:31 2026 Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EA5DB52CCF3 for ; Fri, 18 Sep 2026 21:20:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.70 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766416; cv=none; b=ceRtQjFkkKb/5YVedsceVXrdSqY02JDDbC4k7VVenvSDWHn8147w83uwCSJaIcWvIAT3Hwm27J20zxmD6LPjrbRopK+WoNZu41819KxVRgMRK1IJ+9seELIJ/LD2Klf9FJmf1wEyO3A0muvtMkmWSqFQ1K0hwB4rSC0gPb3cTzI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766416; c=relaxed/simple; bh=Zhvfi51yykr7PWTmA1NhO3U3DU27Kk6yVoyW1tyqw7M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dZ7vzGYkxEy/mqvQMMGo6ukxew+oIY1JGL6KcG+AsyV2lcHh+f7SgSd+/gPlH3an9nICAci/ZbFeXlonios0tNRprCJQKvcvfgkP+WrbgImr2u7M76DqOtBqkGziIWszsTVxUzuEuLNdYh7iNoulg0QAdLqT5PIeauOvWr8q9k4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=C0p0ygt1; arc=none smtp.client-ip=209.85.216.70 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.b="C0p0ygt1" Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-39b77130a7fso2192335a91.2 for ; Fri, 18 Sep 2026 14:20:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789766413; x=1790371213; 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=6hvZXo3pCgiX/XPpeXdh9Ez/iC9+6VQipJ3Ls30PyTI=; b=C0p0ygt1fAKWEHYQNw1ylS9dzqWL+b7+JP63DC9Q7lZ1n2VzCinMb60YqyivGuZZ5s IxEVimGRpFUnzT4jC3+1Qv9C2VTpnVf4PwfwHhVjxOJSfAKifCj5yj4/tatF1zBz+lnE gwoUe9miG0D0JnA/RTcRYHpsyw2d6K3882QOpPFAJtC+aialmotR3Y3GBCYPgtO5MIX7 HZLdqFWiMZHNpET9N/m/Ch/Iyqk7FN5eRTytg+oMxXSmuLl1iz6rmXKf/ABAHGjbkurl fPaTnWJQ+QZ86IZznUEVdYh+5NvUAhjTn1VurUrFoCtjWCv1+U6CKBizlH9sXBiEKdrp Seig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789766413; x=1790371213; 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=6hvZXo3pCgiX/XPpeXdh9Ez/iC9+6VQipJ3Ls30PyTI=; b=voeRvfbb7kLDUmyTCvppff48ltkXeLOf0b10j9HAK8byz7TNrYdaeDFMr1C87//4Nw vg4B5HXkTbiiXWZE1a97JSFGFCNuePBPB6rkrs5C/Nsz8TgYB4ul+TrUbzK9QDfha545 JpWkNPy1N2dsQ9/vkpuTL9RuIxKsfqv2DH3YaYRi2t+3L9Fe+1WRrXFEpFK5DkMtFoUH RehvgRqOgbxCO1RXlm9+AyGXt+ZyKN0hCwWxKig2gBhsVFniRShuv7+zSfAD4425qbvr TF6ooCbPIyp24RTk2cqfMykjnZgNsNG9JgNwUeh3N2yal+gUAicTLZP4VtS/h1vDa8EH CglQ== X-Forwarded-Encrypted: i=1; AKwUvBypt6QnwT0eQvDDKEa4gC5PoJlAM6v/LyMsGKjm3jB1fOk3VwGI7H1MMhnjNEH3OWgYs+KFYeLCyfF2wV8=@vger.kernel.org X-Gm-Message-State: AFuF++kNBMT9ODhbmBZdfIuMJ7uYKoTOn2RroKWhn06OEcde+C8aQe86 sYVkzVqtX1nEIDlkXX1xL14B7B2+6SIQgMeHjQYHC40a4w/JU0J4sPYFRu7L2iPt0Ao2anPa6Ki QVzOLOecD4g== X-Received: from dloo7-n1.prod.google.com ([2002:a05:7023:a47:10b0:144:bf4d:c486]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2ecb:b0:39e:4c7f:8b1c with SMTP id 98e67ed59e1d1-39e54d4cb89mr7971941a91.33.1789766413010; Fri, 18 Sep 2026 14:20:13 -0700 (PDT) Date: Fri, 18 Sep 2026 14:19:24 -0700 In-Reply-To: <20260918211932.2966061-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918140659.2501976-1-irogers@google.com> <20260918211932.2966061-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918211932.2966061-11-irogers@google.com> Subject: [PATCH v4 10/18] perf trace: Drop targets that died before they were filtered From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org, Howard Chu Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" sched_process_exit() takes a task out of pids_to_trace and pids_filtered as it dies, which keeps the maps holding live tasks rather than growing for the length of the session. It only finds a task there if userspace put it there first, and userspace writes a pid some time after reading it: - trace__set_filter_pids() writes the thread map and the --filter-pids list before the programs are attached, so for those there is nothing watching them die at all. - trace__set_startup_pids() writes what it enumerated after the attach, which is a much smaller gap but still a gap. A task that dies inside one of those windows is never evicted, because the delete that would have done it ran while the map had nothing to delete. Its pid then sits in the map for the rest of the session: it holds one of a fixed number of entries, and pids are reused, so the unrelated task that eventually receives it is treated as the target, or as a task the user asked to leave out and is then silently not reported. Walk both maps once the last write to them is done and remove the tasks that are not alive. Sweeping the maps rather than the lists that were written to them covers every way a pid can get in, including the pid of perf itself and of the terminal it was started from, which trace__set_filter_loop_pids() adds without recording anywhere. A task found alive here and dying later is one sched_process_exit() can see and evict, so a single pass is enough, and the tasks the BPF programs add for themselves need nothing: sched_process_fork() inserts a task before it has run, so it cannot have died beforehand. Deleting the key the walk is standing on leaves bpf_map__get_next_key() to resume from a key that is no longer there, which htab_map_get_next_key() answers with the first key of the whole map, starting the walk over. The walk therefore only ever stands on a key it has found alive, which is one it has just decided to keep, so nothing it deletes is ever the key it would go on to ask from. A key deleted while the walk stands on nothing is not a problem either: the next request is for the first key of the map, and the deleted one is no longer it. sched_process_exit() deletes keys while this runs and can take out the key the walk is standing on, which there is no way to prevent or to notice. The walk is bounded at max_entries steps so that it ends however often that happens: the map holds no more than that many keys, so a walk that has taken as many steps has either seen them all or been restarted, and the bound is what stops a walk that keeps being restarted from going round for ever. Ending on the bound can leave a dead task behind, which costs one entry of a great many until the session ends, where not ending would cost the session itself. A task is taken to be gone when its /proc entry is not there, and also when /proc reports it as Z or X. Both mean it is in or past do_exit(), which is where sched_process_exit() runs, so waiting for a zombie to be reaped before dropping it would only keep a dead task in the map for longer. Everything else, including not being able to tell, leaves the task in the map. The two mistakes are not equal: a dead task left there holds an entry nothing needs, while a live one taken out stops being traced for the rest of the session, so a read that fails for perf's own reasons, being out of file descriptors or of memory, is not read as the task having exited. Reading that state means finding the end of the command name, which do_task_stat() writes out unescaped, unlike /proc//status: it holds whatever the task called itself, ')' and newlines included. The whole of /proc//stat is read rather than a line of it, because a line stops at the first embedded newline, short of the name's closing ')', and the last ')' in the file is taken as that one, since no field after the name has parentheses in it. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/builtin-trace.c | 76 ++++++++++++++++++++++++--- tools/perf/util/bpf_trace_augment.c | 79 +++++++++++++++++++++++++++++ tools/perf/util/trace_augment.h | 6 +++ 3 files changed, 153 insertions(+), 8 deletions(-) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index 8a5dbf144540..f2425a291eeb 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -4846,6 +4846,59 @@ static int trace__collect_target_pids(struct trace *= trace, struct pid_list *pids return err; } =20 +/* + * Is the task still running? + * + * A pid with no /proc entry is gone, and one reported as Z (zombie) or X = (dead) + * is in or past do_exit(), which is where sched_process_exit() runs. Eith= er way + * the BPF programs will never hear about it again. + * + * Anything that is not one of those, including being unable to tell, is t= aken + * as alive. The two mistakes are not equal: a dead task left in the map h= olds + * an entry that nothing needs, while a live one taken out of it stops bei= ng + * traced for the rest of the session. + */ +static bool trace__task_is_alive(pid_t pid) +{ + char path[PATH_MAX]; + const char *state; + char *stat =3D NULL; + bool alive =3D true; + size_t len; + int err; + + scnprintf(path, sizeof(path), "%s/%d/stat", procfs__mountpoint(), pid); + err =3D filename__read_str(path, &stat, &len); + if (err) { + /* + * ENOENT and ESRCH are the task being gone, which is what this + * is looking for. Any other error is perf's own, running out + * of file descriptors or of memory, and says nothing about the + * task. + */ + return err !=3D -ENOENT && err !=3D -ESRCH; + } + + /* + * The state is the field after the command name. do_task_stat() writes + * that name out as it is, so it can hold anything a task cares to call + * itself, ')' and newlines included. Nothing after it has parentheses, + * so the last ')' in the file is the one that closes it; the whole file + * is read rather than a line of it because a line stops at the first of + * those newlines, short of the ')' that is being looked for. + * + * A read that returns something the state cannot be read out of, such + * as the empty result of the task exiting midway through it, leaves the + * task alive by the rule above. + */ + state =3D strrchr(stat, ')'); + if (state !=3D NULL && state[1] =3D=3D ' ') + alive =3D state[2] !=3D 'Z' && state[2] !=3D 'X'; + + free(stat); + return alive; +} + /* * Trace the tasks that appeared while perf trace was starting up. * @@ -4868,19 +4921,26 @@ static int trace__collect_target_pids(struct trace = *trace, struct pid_list *pids static int trace__set_startup_pids(struct trace *trace) { struct pid_list pids =3D {}; - int err; + int err =3D 0; =20 /* - * Nothing to do without a target: 'perf trace -a' does not filter on - * pid at all, and evlist__prepare_workload() keeps a workload blocked - * on a pipe until evlist__start_workload(), well after the attach. + * Only a target is enumerated: 'perf trace -a' does not filter on pid + * at all, and evlist__prepare_workload() keeps a workload blocked on a + * pipe until evlist__start_workload(), well after the attach. */ - if (!target__has_task(&trace->opts.target)) - return 0; + if (target__has_task(&trace->opts.target)) { + err =3D trace__collect_target_pids(trace, &pids); + if (!err) + err =3D augmented_syscalls__set_target_pids(pids.nr, pids.entries); + } =20 - err =3D trace__collect_target_pids(trace, &pids); + /* + * Every session gets the sweep, target or not: --filter-pids names + * tasks to leave out with no target of its own, and those are written + * to a map of their own, before the attach and with the same race. + */ if (!err) - err =3D augmented_syscalls__set_target_pids(pids.nr, pids.entries); + err =3D augmented_syscalls__prune_dead_pids(trace__task_is_alive); =20 pid_list__exit(&pids); return err; diff --git a/tools/perf/util/bpf_trace_augment.c b/tools/perf/util/bpf_trac= e_augment.c index 44f30dba5469..4f93f5c062c1 100644 --- a/tools/perf/util/bpf_trace_augment.c +++ b/tools/perf/util/bpf_trace_augment.c @@ -255,6 +255,85 @@ int augmented_syscalls__set_target_pids(unsigned int n= r, pid_t *pids) return 0; } =20 +/* + * Remove from a pid keyed map every task that is no longer alive. + * + * Deleting the key the walk is standing on leaves bpf_map__get_next_key()= to + * resume from a key that is no longer there, which htab_map_get_next_key() + * answers with the first key of the whole map, starting the walk over. The + * cursor is therefore only ever moved onto a key that has been found aliv= e, + * which is a key this function has just decided to keep, so nothing it do= es + * can send itself back to the beginning. + * + * sched_process_exit() deletes keys while this runs and can take out the + * cursor, which there is no way to prevent or to notice. The walk is boun= ded + * at max_entries steps so that it ends however often that happens: the map + * holds no more than that many keys, so a walk that has taken as many ste= ps + * has either seen them all or been restarted, and the bound is what stops= a + * repeatedly restarted walk from going round for ever. Stopping there can + * leave a dead task behind, which costs one entry of a great many until t= he + * session ends, where not stopping would cost the session itself. + */ +static int prune_dead_map_pids(struct bpf_map *map, bool (*is_alive)(pid_t= pid)) +{ + size_t max_entries =3D bpf_map__max_entries(map); + pid_t cursor, key; + bool have_cursor =3D false; + + for (size_t step =3D 0; step < max_entries; step++) { + int err; + + /* Anything other than success means there is no next key. */ + if (bpf_map__get_next_key(map, have_cursor ? &cursor : NULL, + &key, sizeof(key)) !=3D 0) + break; + + if (is_alive(key)) { + cursor =3D key; + have_cursor =3D true; + continue; + } + + err =3D bpf_map__delete_elem(map, &key, sizeof(key), /*flags=3D*/0); + /* + * ENOENT means the key went away while this was running, which + * is sched_process_exit() doing the same job. + */ + if (err && err !=3D -ENOENT) + return err; + + /* + * The cursor stays where it was, which is either a key that is + * still in the map or unset. Unset asks the next step for the + * first key of the map, and the one just deleted is no longer + * it, so the walk moves on either way. + */ + } + + return 0; +} + +/* + * Remove the tasks that are no longer alive from both pid maps. + * + * has_pids_to_trace and has_pids_filtered are left alone. They say that t= he + * maps are in use rather than that they have anything in them, and cleari= ng + * the first would turn a targeted session into a system wide one. + */ +int augmented_syscalls__prune_dead_pids(bool (*is_alive)(pid_t pid)) +{ + int err; + + if (skel =3D=3D NULL) + return 0; + + err =3D prune_dead_map_pids(skel->maps.pids_to_trace, is_alive); + if (!err) + err =3D prune_dead_map_pids(skel->maps.pids_filtered, is_alive); + + return err; +} + /* * Populate syscalls in the BPF syscalls_to_trace map: * - not_syscalls: true if '!' prefix was specified (blacklist mode: trace diff --git a/tools/perf/util/trace_augment.h b/tools/perf/util/trace_augmen= t.h index ad992f5fa726..87dbf8c46c1e 100644 --- a/tools/perf/util/trace_augment.h +++ b/tools/perf/util/trace_augment.h @@ -16,6 +16,7 @@ int augmented_syscalls__create_bpf_output(struct evlist *= evlist); void augmented_syscalls__setup_bpf_output(void); int augmented_syscalls__set_filter_pids(unsigned int nr, pid_t *pids); int augmented_syscalls__set_target_pids(unsigned int nr, pid_t *pids); +int augmented_syscalls__prune_dead_pids(bool (*is_alive)(pid_t pid)); int augmented_syscalls__set_target_syscalls(unsigned int nr, int *syscall_= ids, bool not_syscalls); int augmented_syscalls__get_map_fds(int *enter_fd, int *exit_fd, int *beau= ty_fd); struct bpf_program *augmented_syscalls__find_by_title(const char *name); @@ -55,6 +56,11 @@ static inline int augmented_syscalls__set_target_pids(un= signed int nr __maybe_un return 0; } =20 +static inline int augmented_syscalls__prune_dead_pids(bool (*is_alive)(pid= _t pid) __maybe_unused) +{ + return 0; +} + static inline int augmented_syscalls__set_target_syscalls(unsigned int nr = __maybe_unused, int *syscall_ids __maybe_unused, bool not_syscalls __maybe_unused) --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Thu Sep 24 22:56:31 2026 Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0307652CCFA for ; Fri, 18 Sep 2026 21:20:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.69 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766417; cv=none; b=WHj6W8pYbWl26LT+C0kuGmhUkyJyTdJpSDO5ulx3OvT0worzgabWuZwYKY1ZsgQtpidNCugF74GehRkHO3R2wEoLHLTPhLbxp9Ah5xt+JLjbOr6RX1WzN1McwgFQNX5xXJMwhiZNK/Kt6poiO41M8C/3jEt6y26llq7aUiMxJAw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766417; c=relaxed/simple; bh=l5TAbWEU6B9V61riUhgaEbX3bYek64hdeMlGHIz+XJc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=D9Cz0d3cq61+2HjeW1kd0gsOpJ0tcoC38a+rIVWXu9soVKPnxHh38fp8xBwkCRb+VCyj4PwhWEd5ekM4NabVFWFE00w+V94KUWn285cT33lrh8HOLe7U0v0g1cRGQdGhfaVQ84rwzozSknMotzLYe27xRdOQhWd079z5NPsDQXo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=euyjkDfd; arc=none smtp.client-ip=209.85.216.69 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.b="euyjkDfd" Received: by mail-pj1-f69.google.com with SMTP id 98e67ed59e1d1-396901263b6so2128958a91.2 for ; Fri, 18 Sep 2026 14:20:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789766415; x=1790371215; 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=CQS0ArZiNsMn8IJnh7vbzYLZ5vZtatDhvYlp4Dv89Ww=; b=euyjkDfd2uaIUEv4jkGE9LiFgj234TiMtSeXAL8j3v6RkqnP0Qk5QzzVYm0cNmq+bn Jjzmwbg/XOOtLiGeaPoAKhVYmDtNB/dh/aIHcon12AEduKMfI6auIsuXXPEyHK5kg+e+ Rfc0ylz3AifyZJtCgg4QxOrP9NyG+JLjrQSUc6O0ltSMfNWW9RnPsZzKzd+E/7bgnjrN TXJSeYIoEtVBeofYO4HTaWQ1X1iOSY7db+3bYrB63W5hsamkXtpaKmBKJkEhBUVUF+ob mC/HVvLkK4gXgHSgYFz+Ls4HTqVgsq4M9CzqUCKTmPtpSdsABvgZnVyox2htmxqXAKPa bGhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789766415; x=1790371215; 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=CQS0ArZiNsMn8IJnh7vbzYLZ5vZtatDhvYlp4Dv89Ww=; b=pI8T75kuubBmm10oyUEMP+PC4r82FI+59yUkGl35Dv/1ZorjouwLG71wmspIwQQ8AU vjP1H3FAkVai5lFPuJzlDj/s5y7QMlHeNajwDQzg7C3xUDMUdtJA4lB3v5BGeXmoum/8 mUf7UZA3KTPNYFeLYA0E9CYpGZn7K9zfdlNafaJhxw0ORb9mfD/AM7R7O81jUCMwHHJA 7UYboF6y6KkD5yBSn423EeRc/5YIjehfJMe3VaUZkV0YBB1EEAdHHo/0mspZg3EKYh2N u+wrQC0XN/teljJB8t/2NCsexifzlEjAtqZJv5+BG+9dJxvML3UuWOOdG2b2cH/g05GC jdAw== X-Forwarded-Encrypted: i=1; AKwUvBxnbi0OOSWQk/+7gKJzsETxZWoic2YpPi4E3q7PDxwQZu7ge8SZm+Vbh/IaQeXzWfxVkP/1Y1wO4pLPUMA=@vger.kernel.org X-Gm-Message-State: AFuF++nU3T44/QMNDu8UD9sp8iD3/QDD+055xLmUAC9fnxAFM6UXDWtK 5sxYpZev5rP48wBbuC+T2894kHBJ5j6ahwAy89JMjSTpO4iXuT8v4OpFeq50b/7feDnhqjeEj4Y rPISCnc16eA== X-Received: from dybbs27.prod.google.com ([2002:a05:7300:a21b:b0:337:a3b2:d113]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5305:b0:381:6c5:3f63 with SMTP id 98e67ed59e1d1-39e54dcd917mr13200518a91.6.1789766415217; Fri, 18 Sep 2026 14:20:15 -0700 (PDT) Date: Fri, 18 Sep 2026 14:19:25 -0700 In-Reply-To: <20260918211932.2966061-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918140659.2501976-1-irogers@google.com> <20260918211932.2966061-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918211932.2966061-12-irogers@google.com> Subject: [PATCH v4 11/18] perf test test_task_analyzer: Isolate in temporary directory and make non-exclusive From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org, Howard Chu Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" test_task_analyzer.sh writes perf.data and temporary files directly into the current working directory, causing collisions when running tests in parallel. As a temporary measure until `perf script report` supports an input file option, resolve perfdir to an absolute path, change directory into $tmpdir for the test duration, and clean up in the exit trap. Remove the (exclusive) tag so the test runs in parallel. perfdir is derived from $0, which may be relative, so it has to be resolved before the cd into $tmpdir, otherwise both PERF_EXEC_PATH and the cleanup trap point at paths that no longer exist. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/tests/shell/test_task_analyzer.sh | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/tools/perf/tests/shell/test_task_analyzer.sh b/tools/perf/test= s/shell/test_task_analyzer.sh index 0314412e63b4..443b88957b17 100755 --- a/tools/perf/tests/shell/test_task_analyzer.sh +++ b/tools/perf/tests/shell/test_task_analyzer.sh @@ -1,8 +1,18 @@ #!/bin/bash -# perf script task-analyzer tests (exclusive) +# perf script task-analyzer tests # SPDX-License-Identifier: GPL-2.0 =20 -tmpdir=3D$(mktemp -d /tmp/perf-script-task-analyzer-XXXXX) +# Resolve the source directory before changing the working directory below, +# $0 may be a relative path and would no longer resolve from $tmpdir. +perfdir=3D$(cd "$(dirname "$0")/../.." && pwd) + +tmpdir=3D$(mktemp -d /tmp/perf-script-task-analyzer-XXXXX) || exit 1 +# The cleanup trap is only installed further down, once the functions it +# calls have been defined, so tidy up by hand if this cd fails. +cd "$tmpdir" || { + rmdir "$tmpdir" + exit 1 +} # TODO: perf script report only supports input from the CWD perf.data file= , make # it support input from any file. perfdata=3D"perf.data" @@ -11,7 +21,6 @@ csvsummary=3D"$tmpdir/csvsummary" err=3D0 =20 # set PERF_EXEC_PATH to find scripts in the source directory -perfdir=3D$(dirname "$0")/../.. if [ -e "$perfdir/scripts/python/Perf-Trace-Util" ]; then export PERF_EXEC_PATH=3D$perfdir fi @@ -20,8 +29,9 @@ fi export ASAN_OPTIONS=3Ddetect_leaks=3D0 =20 cleanup() { - rm -f "${perfdata}" - rm -f "${perfdata}".old + # Step out of $tmpdir before removing it. The removal uses an absolute + # path and works from anywhere, so a failure to cd must not skip it. + cd "$perfdir" || cd / || true rm -rf "$tmpdir" =20 trap - exit term int --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Thu Sep 24 22:56:31 2026 Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8822449AA20 for ; Fri, 18 Sep 2026 21:20:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.69 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766420; cv=none; b=O1QMR63pdpYCl9JFD+b1XXoGNDer1cxZ1RKZ4ZkhM/6xSNL+lSDYGklekfsFZFGceoUtARYiYeeM7Ie77QL2+05Td1o1lubnP27lW+AD/oRCjtT4YyAsK0JkvApDkdK7JkPJUJghEsvTIfAeQ4n48zpEMdjDWtLf39skjEzA2W0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766420; c=relaxed/simple; bh=3qyDKSUHoAXJBVvMweoPwov6DmzqcY9odxEwkd1kTIY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Ni6QmV5Ah+UK6fXG508D0YnFcbuSnjTXmLoGDPIeusYY4btlC4ZYrIQLaO9n/xv/R5Kdn0jl8xh2YExUOu+sBShWhIyz46l5qoSQcOcB3Nn0kWXoOLsbhOnVdVXBysRsipMewHA6JKSeOnJxRzV5va9bNx4I/6REiwYizUrgMd8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bQXifnMc; arc=none smtp.client-ip=209.85.216.69 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.b="bQXifnMc" Received: by mail-pj1-f69.google.com with SMTP id 98e67ed59e1d1-38f97b3f853so2001934a91.3 for ; Fri, 18 Sep 2026 14:20:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789766418; x=1790371218; 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=PdvT9XF5drvxnf/MfPum/Gi0QBj0faFgeFxRBLMV2Co=; b=bQXifnMcVTAEg4DOIqCxQNrZpSDmEZP2aWgIJ45KhcN3/GwCxBvBpH0BsYcxVHFyXu Iv00W+S5UHB0a8Pn9r8yHDYg2CnD2kyGjNjud2KHgDWfLKTMEOwReiFZxu8626Kx6Zkb 5kx+1FPy/xDXNUqGuBmUj9mRwci9oCu1ejwW1rfZTOq8v16ezLYcS2YJuVlZtFjTL2OM 5YWTPt+UcvzZjtbeVuiLT7Qc3IZrWKcOb7Z4Y3+jfKmgffaADwFgeYKLJUz5kl/kQrfA 4D3ZBoOd0FWp5mb352rlylDF3U00jGRSndzreqvGC7Za7xph+EVEQDzE/PtrFJcjyobF f3Zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789766418; x=1790371218; 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=PdvT9XF5drvxnf/MfPum/Gi0QBj0faFgeFxRBLMV2Co=; b=L+VIEAt1M2q87usfPDypVpwu1S7Clo9LaC4LX7SJsfTWbwGE9DMZv+4Grvf22o34so 8xMVyiAekwIPAMxKukcaJ3ACl1zSSwHMNbxtKxJcfAbYMDJzFlk82xTaLIXYNQ2baL51 OSPDJFpnD6NrvrY7rCL5OwNA3l2+8kJVCMLWG4ocSMBIzPDS/Nvnlbb7hoUdXSDW8+KH OiYYObFMwms/JWlVbJpSeje+yipCHGLH9q6lbP0yqu9iRJboa/C/gwvXkqtH8LEiwVIc 9J74xFZxum4BTdOVKvQilJ8GlC/RBfVfwFILEulrPyepFs9uO2l6CPfFDT8VKnS3xz1c y1kQ== X-Forwarded-Encrypted: i=1; AKwUvBwXT75IuoBL51YOKE3jKF69jFZRAYbL4wJH7FF+nYI/JVhaAgBR7jZ4IWP8twzNUnSm+IVuGmqdo0Ib04c=@vger.kernel.org X-Gm-Message-State: AFuF++kCBs7OcLbDp92wrGk1QPiwHB7wTUEJcGmHieL4VCKS2eIH+Gwt 0z2EQrdzjqqB6BnoS1sJVr218jlBTNEOSgmHf/B7yDkb8DYHKwSAG5z2ABUVLFTcN/Gc0gUnQIJ vgSuPbY2Tsw== X-Received: from dlec1-n1.prod.google.com ([2002:a05:701b:4281:10b0:144:cfa3:9a66]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4c03:b0:39e:4c80:44ba with SMTP id 98e67ed59e1d1-39e54dcc3ccmr7595486a91.29.1789766417734; Fri, 18 Sep 2026 14:20:17 -0700 (PDT) Date: Fri, 18 Sep 2026 14:19:26 -0700 In-Reply-To: <20260918211932.2966061-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918140659.2501976-1-irogers@google.com> <20260918211932.2966061-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918211932.2966061-13-irogers@google.com> Subject: [PATCH v4 12/18] perf test common: Only disable probes in clear_all_probes From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org, Howard Chu Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" clear_all_probes() began by writing 0 to /sys/kernel/debug/tracing/events/enable, which disables every tracepoint on the system rather than just the probes the probe tests created. When tests run in parallel that also silences the events of any concurrent perf record, perf trace or ftrace session, so unrelated tests lose the events they are waiting for and fail. The write cannot simply be dropped. Clearing kprobe_events or uprobe_events is all or nothing: dyn_events_release_all() walks every probe of that type first and returns -EBUSY without removing any of them if it finds one that is still enabled, where enabled means TP_FLAG_TRACE from tracefs or TP_FLAG_PROFILE from a perf session. A probe left enabled through tracefs would therefore block the whole clear and leave stale probes behind to collide with the next run. Disable the probes and only the probes. The set to disable is taken from the kprobe_events and uprobe_events listings rather than from a guess at which groups perf uses, so it matches what dyn_events_release_all() is going to inspect: a probe an unrelated session left enabled in, say, the kprobes group would otherwise still abort the clear. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/tests/shell/common/init.sh | 33 ++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tools/perf/tests/shell/common/init.sh b/tools/perf/tests/shell= /common/init.sh index cbfc78bec974..7c2ca74298ca 100644 --- a/tools/perf/tests/shell/common/init.sh +++ b/tools/perf/tests/shell/common/init.sh @@ -130,9 +130,40 @@ check_uprobes_available() test -e /sys/kernel/debug/tracing/uprobe_events } =20 +# Disable every kprobe and uprobe event. The listings name each probe as +# "TYPE:GROUP/EVENT ARGS...", for instance "p:probe/vfs_read vfs_read", and +# events/GROUP/EVENT/enable is the switch for it. +disable_all_probes() +{ + PROBE_SPECS=3D`cat /sys/kernel/debug/tracing/kprobe_events \ + /sys/kernel/debug/tracing/uprobe_events 2> /dev/null | + cut -d ' ' -f 1` + for PROBE_SPEC in $PROBE_SPECS + do + case "$PROBE_SPEC" in + *:*/*) ;; + *) continue ;; + esac + PROBE_ENABLE=3D"/sys/kernel/debug/tracing/events/${PROBE_SPEC#*:}/enable" + test -e "$PROBE_ENABLE" && echo 0 > "$PROBE_ENABLE" + done +} + clear_all_probes() { - echo 0 > /sys/kernel/debug/tracing/events/enable + # Disable the probes before removing them. Writing to kprobe_events or + # uprobe_events is all or nothing: dyn_events_release_all() walks every + # probe of that type first and returns -EBUSY without removing any of + # them if it finds one that is still enabled, which would leave stale + # probes behind to collide with the next run. That covers probes this + # test suite never created, so disable all of them and not just the + # ones in the groups perf uses. + # + # Only probes are disabled. Writing to events/enable would also silence + # the tracepoints of any perf record, perf trace or ftrace session + # sharing the machine, which breaks those tests when they run in + # parallel with this one. + disable_all_probes check_kprobes_available && echo > /sys/kernel/debug/tracing/kprobe_events check_uprobes_available && echo > /sys/kernel/debug/tracing/uprobe_events } --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Thu Sep 24 22:56:31 2026 Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4E03452CCE7 for ; Fri, 18 Sep 2026 21:20:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.72 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766431; cv=none; b=jWRch1zxREu1Jx/PhRutca5DrtGxcfQWesYDPSezer7+4oOLD2L8GriuP6mC+wuFm83Zdd5oRPn26LE1RaAb0BEO/uSW0dVwhSsVplmVH6sUE0ZL85OOodZarRovZiLMj+q90sADPDaKOUCmcygjRx47YUq+Nq1AQ7Hd3dJKRlM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766431; c=relaxed/simple; bh=js3MmCu39/cj46Xs9OQpjW7xXWJ5rM3UT+1/Hq5HO/4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Y3euLXohm/IFLocC2BFg5LSwCBQdu/BMymhX5xfAg+ExeU7E3KyawzIuc9MMNW2bIUmBBNZy3NIm2nLydaLSE+Bt4j3HXE7tz7mMj/7TAZiWP+QSyGqp7kGs5BrcUOL4FcnNl3xh5KUTPxSGApILlijAicOj5jbC+rDGAoubKRs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Ck6JLay6; arc=none smtp.client-ip=209.85.216.72 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.b="Ck6JLay6" Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-398d0010cfaso1959534a91.3 for ; Fri, 18 Sep 2026 14:20:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789766421; x=1790371221; 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=p1wleqEAHJWzd5Fq3MOQlRkWIUYoY+UnMLT1UHUF+dc=; b=Ck6JLay6pXG1vlUFJv7h415OQq5Z4mjma7s/ZNBtus5hWgGh3RihOLFKHR+V4lNQ1z ps2+UPMeVu4RoSvrrxJOPDtBILWitotD24WoBcIlANMG8PWs3CaCRxjVvEHUiGybsCiN 9BiDQlu+JjhO+L/hFTqZbjhYNM6fjaUjD7S9lhUk/BIZOc8ZG6wZWPeQgInAN8UPyE2/ 2sszLFtaduD1anAk2XrPzxrE9QOAyWKlqWY1JUF5HnZrRnrMrGiGwTSTLn19/JHvfT/q NGNDAMZdPXx+VjHozsGw0NNIJn03cWfyEBb/wBI9SmqdEa/zmoma4Q0eld4j2eBZhmp8 4OqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789766421; x=1790371221; 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=p1wleqEAHJWzd5Fq3MOQlRkWIUYoY+UnMLT1UHUF+dc=; b=GTjK/HhbPMUIa+eKptiOJY19IRQncMl6/CJPV/8ggx/mdB/EGK58ieJx9Jry3Q3tEp IKuLgEmZ5VWu18BTK29ryl1B9gydMZh2bZ5uECsmGE9bgi1nz1zM/PNzWM+NTRVMp+sP nyGrIpHh1oY+eCm0d7L4ew1gwF4q/rzTILAvw5wfB1S8/maCdrAOTDWjqKqK0e0pZ0mZ LhWvhPXLwZ47YpVcdvIKwkPly7iGqqNma8TIsU+rP3qBJZL1Y9FQYR8W3Oip6DsPqt/u KggDsXTR+pdEhHUcMHilt3zeXQILJSzcxtj9jVUsoIgrQOn4f2gXYRPBAsz+7lHZG6iF EDdA== X-Forwarded-Encrypted: i=1; AKwUvBz/vRQu+K4a1RkKX3y6s7PpRPlq5iJzmpxIYr+PH1cep9IyynANnUmS6sFFEuV4OaUeK/BEq6D6d8E/UZ8=@vger.kernel.org X-Gm-Message-State: AFuF++nrsoIibBwd0l79uhNRZb7WIuO/ynpYKBFJBKGuY9ml1oalMVHV NK3kZYf+UkHHoLeurE9Y5ijYFYuTsVIwvYhHWH99h9G1lrI1KclLgyPn+0IFJJi6o2q6Qzhd1Ua q4g7Xd+ceew== X-Received: from dlbsn6.prod.google.com ([2002:a05:7022:b906:b0:143:6c23:2cab]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5346:b0:39e:6c69:7779 with SMTP id 98e67ed59e1d1-39e6c697a4fmr2208555a91.34.1789766420442; Fri, 18 Sep 2026 14:20:20 -0700 (PDT) Date: Fri, 18 Sep 2026 14:19:27 -0700 In-Reply-To: <20260918211932.2966061-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918140659.2501976-1-irogers@google.com> <20260918211932.2966061-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918211932.2966061-14-irogers@google.com> Subject: [PATCH v4 13/18] perf test probe_vfs_getname: Scope probe name to PID and make non-exclusive From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org, Howard Chu Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The probe name `vfs_getname` was hardcoded, causing collisions when tests ran concurrently. Furthermore, `cleanup_probe_vfs_getname()` used `perf probe -d probe:vfs_getname*`, deleting probes registered by other parallel tests. Scope the probe name to the pid, and rename it to `getname_flags_$$` so that it no longer begins with "vfs_getname". perf trace calls evlist__add_vfs_getname(), which opens every event matching a hardcoded "probe:vfs_getname*" wildcard, so a perf trace run by any other test would otherwise pin this probe and make `perf probe -d` fail with -EBUSY. That also unblocks making the perf trace tests non-exclusive later in this series. Enumerate the probes to record and to delete from `perf probe -l`, matching `^probe:${vfs_getname}(_[[:digit:]]+)?$` exactly, rather than globbing on `${vfs_getname}*`. perf probe appends _1, _2, ... when getname_flags is inlined at more than one call site, so the variants do have to be matched, but since the name now ends in a pid a trailing wildcard would also match the probes of a test whose pid merely starts with this one's, e.g. 123 and 1234. Remove the `(exclusive)` tag from probe_vfs_getname.sh and record+script_probe_vfs_getname.sh so they run concurrently in pass 1. trace+probe_vfs_getname.sh has to stay exclusive: it is the one test that wants to be discovered by that wildcard, so it sets vfs_getname to a "vfs_getname_$$" name before sourcing the library, and would then pin its siblings' probes if it ran alongside them. A comment in the test records this. Delete the probes from an exit trap as well as on the way out. A pid scoped name is never seen again, so a run interrupted before cleanup_probe_vfs_getname() would leave its probes behind for good, accumulating a set per run. The fixed name at least meant the next run found, and went on to reuse, whatever the last one left. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- .../perf/tests/shell/lib/probe_vfs_getname.sh | 51 +++++++++++++++++-- tools/perf/tests/shell/probe_vfs_getname.sh | 3 +- .../shell/record+script_probe_vfs_getname.sh | 18 +++++-- .../tests/shell/trace+probe_vfs_getname.sh | 9 ++++ 4 files changed, 71 insertions(+), 10 deletions(-) diff --git a/tools/perf/tests/shell/lib/probe_vfs_getname.sh b/tools/perf/t= ests/shell/lib/probe_vfs_getname.sh index 88cd0e26d5f6..4e915b6aefc8 100644 --- a/tools/perf/tests/shell/lib/probe_vfs_getname.sh +++ b/tools/perf/tests/shell/lib/probe_vfs_getname.sh @@ -1,15 +1,58 @@ #!/bin/bash # Arnaldo Carvalho de Melo , 2017 =20 -perf probe -l 2>&1 | grep -q probe:vfs_getname +# The name of the getname_flags probe added and removed below. +# +# It is scoped to the pid so that tests running in parallel do not collide, +# and it deliberately does not start with "vfs_getname": perf trace calls +# evlist__add_vfs_getname(), which opens everything matching the hardcoded +# "probe:vfs_getname*" wildcard, so a perf trace running in another test w= ould +# otherwise pin this probe and make the 'perf probe -d' below fail with -E= BUSY. +# +# trace+probe_vfs_getname.sh is the one test that does want to be found th= at +# way, so it sets vfs_getname itself before sourcing this file, and is +# (exclusive) as a result. +: "${vfs_getname:=3Dgetname_flags_$$}" + +# Print the probes add_probe_vfs_getname() created. perf probe appends _1,= _2, +# ... when getname_flags is inlined at more than one call site, so there c= an be +# several. Match them exactly rather than with a "${vfs_getname}*" glob: t= he +# name ends in a pid, so such a glob would also match the probes of a test +# whose pid merely starts with this one's, e.g. 123 and 1234. +probes_vfs_getname() { + perf probe -l 2>/dev/null | awk '{print $1}' | + grep -E "^probe:${vfs_getname}(_[[:digit:]]+)?$" +} + +[ -n "$(probes_vfs_getname)" ] had_vfs_getname=3D$? =20 cleanup_probe_vfs_getname() { if [ $had_vfs_getname -eq 1 ] ; then - perf probe -q -d probe:vfs_getname* + local probe + for probe in $(probes_vfs_getname); do + perf probe -q -d "$probe" + done fi } =20 +# Delete the probes however the test ends, not just when it runs to +# completion. The name is scoped to the pid, so nothing that runs later +# reuses or tidies up a probe an interrupted test left behind, and they +# would accumulate one set per run. The fixed name used before was at least +# picked up again by the next run. +# +# Tests may still call cleanup_probe_vfs_getname directly. Doing so leaves +# nothing for probes_vfs_getname to find, so the trap below then does +# nothing. A test needing cleanup of its own should call +# cleanup_probe_vfs_getname from its own exit trap, since installing one +# replaces this rather than adding to it. +trap cleanup_probe_vfs_getname exit +# Turn a signal into an ordinary exit so that the exit trap above runs. An +# exit trap that returns leaves the exit status alone, so a test exiting 2 +# to skip still skips. +trap 'exit 1' term int + add_probe_vfs_getname() { add_probe_verbose=3D$1 if [ $had_vfs_getname -eq 1 ] ; then @@ -33,8 +76,8 @@ add_probe_vfs_getname() { return 2 fi =20 - perf probe -q "vfs_getname=3Dgetname_flags:${line} pathname=3Dresu= lt->name:string" || \ - perf probe $add_probe_verbose "vfs_getname=3Dgetname_flags:${line} pathn= ame=3Dfilename:ustring" || return 1 + perf probe -q "${vfs_getname}=3Dgetname_flags:${line} pathname=3Dr= esult->name:string" || \ + perf probe $add_probe_verbose "${vfs_getname}=3Dgetname_flags:${line} pa= thname=3Dfilename:ustring" || return 1 fi } =20 diff --git a/tools/perf/tests/shell/probe_vfs_getname.sh b/tools/perf/tests= /shell/probe_vfs_getname.sh index 5fe5682c28ce..05f1d50732b6 100755 --- a/tools/perf/tests/shell/probe_vfs_getname.sh +++ b/tools/perf/tests/shell/probe_vfs_getname.sh @@ -1,6 +1,5 @@ #!/bin/bash -# Add vfs_getname probe to get syscall args filenames (exclusive) - +# Add vfs_getname probe to get syscall args filenames # SPDX-License-Identifier: GPL-2.0 # Arnaldo Carvalho de Melo , 2017 =20 diff --git a/tools/perf/tests/shell/record+script_probe_vfs_getname.sh b/to= ols/perf/tests/shell/record+script_probe_vfs_getname.sh index 002f7037f182..1d4fb4a4fbfe 100755 --- a/tools/perf/tests/shell/record+script_probe_vfs_getname.sh +++ b/tools/perf/tests/shell/record+script_probe_vfs_getname.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Use vfs_getname probe to get syscall args filenames (exclusive) +# Use vfs_getname probe to get syscall args filenames =20 # Uses the 'perf test shell' library to add probe:vfs_getname to the system # then use it with 'perf record' using 'touch' to write to a temp file, th= en @@ -17,22 +17,32 @@ skip_if_no_perf_probe || exit 2 =20 # shellcheck source=3Dlib/probe_vfs_getname.sh . "$(dirname "$0")/lib/probe_vfs_getname.sh" +# shellcheck disable=3DSC2154 # vfs_getname is assigned in lib/probe_vfs_g= etname.sh =20 record_open_file() { echo "Recording open file:" # Check presence of libtraceevent support to run perf record - skip_no_probe_record_support "probe:vfs_getname*" + skip_no_probe_record_support if [ $? -eq 2 ]; then echo "WARN: Skipping test record_open_file. No libtraceevent support" return 2 fi - perf record -o ${perfdata} -e probe:vfs_getname\* touch $file + # Record every probe the inlining of getname_flags produced, naming + # them exactly rather than with a "${vfs_getname}*" glob, which would + # also match the probes of a test whose pid starts with this one's. + local events + events=3D$(probes_vfs_getname | paste -sd, -) + if [ -z "${events}" ] ; then + echo "FAIL: no ${vfs_getname} probe to record" + return 1 + fi + perf record -o ${perfdata} -e "${events}" touch $file } =20 perf_script_filenames() { echo "Looking at perf.data file for vfs_getname records for the file we t= ouched:" perf script -i ${perfdata} | \ - grep -E " +touch +[0-9]+ +\[[0-9]+\] +[0-9]+\.[0-9]+: +probe:vfs_getname[= _0-9]*: +\([[:xdigit:]]+\) +pathname=3D\"${file}\"" + grep -E " +touch +[0-9]+ +\[[0-9]+\] +[0-9]+\.[0-9]+: +probe:${vfs_getnam= e}(_[0-9]+)?: +\([[:xdigit:]]+\) +pathname=3D\"${file}\"" } =20 add_probe_vfs_getname diff --git a/tools/perf/tests/shell/trace+probe_vfs_getname.sh b/tools/perf= /tests/shell/trace+probe_vfs_getname.sh index 7a0b1145d0cd..146305f4d549 100755 --- a/tools/perf/tests/shell/trace+probe_vfs_getname.sh +++ b/tools/perf/tests/shell/trace+probe_vfs_getname.sh @@ -10,6 +10,13 @@ # SPDX-License-Identifier: GPL-2.0 # Arnaldo Carvalho de Melo , 2017 =20 +# This test must stay exclusive, and is the only one of the probe tests th= at +# does: it does not name the event it uses. perf trace discovers it with t= he +# hardcoded "probe:vfs_getname*" wildcard in evlist__add_vfs_getname(), so= the +# probe has to carry that prefix, and a parallel run of this test would th= en +# also match, and pin, the probes of the other tests. The sibling tests av= oid +# all of this by using a name that the wildcard cannot reach. + # shellcheck source=3Dlib/probe.sh . "$(dirname $0)"/lib/probe.sh =20 @@ -17,6 +24,8 @@ skip_if_no_perf_probe || exit 2 skip_if_no_perf_trace || exit 2 [ "$(id -u)" =3D 0 ] || exit 2 =20 +# shellcheck disable=3DSC2034 # consumed by lib/probe_vfs_getname.sh +vfs_getname=3D"vfs_getname_$$" . "$(dirname $0)"/lib/probe_vfs_getname.sh =20 trace_open_vfs_getname() { --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Thu Sep 24 22:56:31 2026 Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 90EBD52CCF7 for ; Fri, 18 Sep 2026 21:20:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.70 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766425; cv=none; b=IJ3vYftBOcKkfBomkHmx8BDg3X3da3D6zRiAZMmcSa0lA4raI28RjT1xzAvuopbd4CKx4S+RT1VSexdEX1Qj3N8GpFwn/3GWA1p2yXgjNJqZMUnOJ/h1/PHqp9m9iFbF+YrHAUexyypwtnbFYXVFkHMzJcSqhWtwVsKqV2O5ZCs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766425; c=relaxed/simple; bh=BsS5nsnN3cXmHtCEI7BqlglY2nFdt11YaoNoFK3pjrw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TAqfpHLbtdkInm95Izerqn9ZlgGQbafYslA26IZS43ZcYQ3IJLbBCAV8BcoegX84u7BWnSTaJtUsSFuktbjAcKf3TNCtFuJquJu7v5SR3YGXADuEv1b3hGedPo/1PNePlqPYVWvV/fdUhe2d9yGO4W1wddDg1wAP2TTt23nHzP4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=mr1XrMRW; arc=none smtp.client-ip=209.85.216.70 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.b="mr1XrMRW" Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-398d0010cfaso1959579a91.3 for ; Fri, 18 Sep 2026 14:20:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789766423; x=1790371223; 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=MEe9D2w+4Dyx7hV+twX4quw1LE1KttrX05zEETQXjKg=; b=mr1XrMRWzNnz6REtqoTyADlBROCsnv5S1cqzeH4qhy2z/Xl/j3O03wLqmpzt3qKf1q 4WbOuUtmeZnBr56CdpT5LReFJpU3NAr1FwixOF+uNiVFo95Ea8A3ToaItb8UJpOMVfJ0 ryvZ+5np+z4mOkggzN5B7Ht/TIOAMeZMNmmosrxDUo/DZQF5JXy6rljAMSdOtNcLwDzD mZHivr+1XgCPf/h9or/oupu+X57TL0sGFwSf3GMrJQx+cL6pz7hMP0WdRTz6cfZHig4v kYMgNb50nzMumajcVe9sNaNk32qnrch1nM2UQwfsJ3S2y1iqNKmb/tW8yM6c2SXyDKaW y5YQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789766423; x=1790371223; 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=MEe9D2w+4Dyx7hV+twX4quw1LE1KttrX05zEETQXjKg=; b=eNE9YwsLCJUeV1aMx4TB94BfCzjcnM0DPOf9h/gjzZ0gioXW0WT7HVBcqBKR9urx9x LdK4sRs43Jh0aBGlX4VeIpeRK1AqxLpXiJGgkicC/U/VOpYxPJXim+7s+4Z2ALuXAVXH Ie3Vsl58NsK0kXc1VNt+ugmIv1tzeir9EEynnBlEI5mCtFiTRPIZkmEEJdun5PZ+/g9S IZ6wlExDADYWN29ficLvgkf+/QX06+L2On7zyRJ2AlRkY+jtc/IIM57SMh1RNNfnI2Kf UwdKt3XDc4jXB/LoJt83Lv71CFV42aP4+UnR3XTtJZPsUvdSwSseg2R3cdcLfXqDB9Fq ZgqA== X-Forwarded-Encrypted: i=1; AKwUvBzPWm4IN1pmeIE3R2zGFQBSiOctFTgs9NmadsH/3qpLqvJjolPHIxAICs7VTjTUkX5NuQrRikL+9HWXPfA=@vger.kernel.org X-Gm-Message-State: AFuF++lUQ2mko1sJFiC+fy2LigXy4fxU2ynztwl9TvyNhDrjEnX4UHxr 2LvgDx+rGQ6Ibwuf5gzL2vmL4u+wgkeGXp91Fj0gPvDiiW51snQF+r72WqFRxAyf3K8xrPb0S/X jxyzTAJ7zxA== X-Received: from dlbut1.prod.google.com ([2002:a05:7022:7e01:b0:144:ce33:3556]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5251:b0:39e:6a7f:eeef with SMTP id 98e67ed59e1d1-39e6a7ff09amr2805541a91.20.1789766422872; Fri, 18 Sep 2026 14:20:22 -0700 (PDT) Date: Fri, 18 Sep 2026 14:19:28 -0700 In-Reply-To: <20260918211932.2966061-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918140659.2501976-1-irogers@google.com> <20260918211932.2966061-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918211932.2966061-15-irogers@google.com> Subject: [PATCH v4 14/18] perf test record+probe_libc_inet_pton: Scope event to PID, add retries, and make non-exclusive From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org, Howard Chu Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The uprobe name was not scoped to PID, and concurrent writes to `/sys/kernel/debug/tracing/uprobe_events` can occasionally return `-EBUSY` when another process holds the tracefs inode lock. Scope the probe event name with `$$` (`inet_pton_$$=3Dinet_pton`) and add a retry loop with backoff for uprobe addition. Drop the `(exclusive)` tag so the test can run in parallel during pass 1. A PID scoped probe is no longer cleaned up by any other test, so add an EXIT/TERM/INT trap to delete it, otherwise an interrupted run leaks the uprobe into the system. The trap is installed only after the root and IPv6 checks that `exit 2` to skip the test, as trap_cleanup() exits 1 and would otherwise turn those skips into failures. Deletion enumerates the probes from `perf probe -l`, matching `^probe_libc:inet_pton_$$(_[[:digit:]]+)?$` exactly, rather than reading $event_name: a signal arriving after perf probe injected the uprobe but before the assignment completed would leave that variable empty and leak the probe, and an `inet_pton_$$*` glob would reach the probe of a test whose pid merely starts with this one's. While here use mktemp rather than mktemp -u for the temporary files: this test runs as root in a world writable /tmp, and predicting a name without creating it allows another user to win the race and plant a symlink. The perf.data check becomes -s rather than -e as mktemp now pre-creates an empty file. Pre-create the temporary files with mktemp rather than reserving names with mktemp -u, and bail out if mktemp fails. The emptiness check on the recorded data quotes its path for the same reason: unquoted, an empty value would leave [ ! -s ] testing the string "-s", which is true, so the negation would skip the failure path and the test would go on to pass without having recorded anything. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- .../shell/record+probe_libc_inet_pton.sh | 107 ++++++++++++++---- 1 file changed, 87 insertions(+), 20 deletions(-) diff --git a/tools/perf/tests/shell/record+probe_libc_inet_pton.sh b/tools/= perf/tests/shell/record+probe_libc_inet_pton.sh index eca629ee83f0..00367f26bfae 100755 --- a/tools/perf/tests/shell/record+probe_libc_inet_pton.sh +++ b/tools/perf/tests/shell/record+probe_libc_inet_pton.sh @@ -1,5 +1,5 @@ #!/bin/bash -# probe libc's inet_pton & backtrace it with ping (exclusive) +# probe libc's inet_pton & backtrace it with ping =20 # Installs a probe on libc's inet_pton function, that will use uprobes, # then use 'perf trace' on a ping to localhost asking for just one packet @@ -21,20 +21,30 @@ nm -Dg $libc 2>/dev/null | grep -F -q inet_pton || exit= 254 event_pattern=3D'probe_libc:inet_pton(_[[:digit:]]+)?' =20 add_libc_inet_pton_event() { + local attempts=3D0 + while [ $attempts -lt 3 ]; do + event_name=3D$(perf probe -f -x $libc -a "inet_pton_$$=3Dinet_pton" 2>&1= | \ + awk -v ep=3D"$event_pattern" -v l=3D"$libc" '$0 ~ ep && $0 ~ \ + ("\\(on inet_pton in " l "\\)") {print $1}' | head -n 1) + + if [ -n "$event_name" ]; then + return 0 + fi + attempts=3D$((attempts + 1)) + sleep 0.1 + done =20 - event_name=3D$(perf probe -f -x $libc -a inet_pton 2>&1 | \ - awk -v ep=3D"$event_pattern" -v l=3D"$libc" '$0 ~ ep && $0 ~ \ - ("\\(on inet_pton in " l "\\)") {print $1}' | head -n 1) - - if [ $? -ne 0 ] || [ -z "$event_name" ] ; then - printf "FAIL: could not add event\n" - return 1 - fi + printf "FAIL: could not add event\n" + return 1 } =20 trace_libc_inet_pton_backtrace() { =20 - expected=3D`mktemp -u /tmp/expected.XXX` + # Create the files rather than just reserving names with mktemp -u: + # this runs as root and /tmp is world writable, so a predictable name + # that is written to later can be pre-created as a symlink by an + # unprivileged user and used to clobber an arbitrary file. + expected=3D$(mktemp /tmp/expected.XXX) || return 1 =20 echo "ping[][0-9 \.:]+$event_name: \([[:xdigit:]]+\)" > $expected echo ".*inet_pton\+0x[[:xdigit:]]+[[:space:]]\($libc|inlined\)$" >> $expe= cted @@ -50,8 +60,8 @@ trace_libc_inet_pton_backtrace() { ;; esac =20 - perf_data=3D`mktemp -u /tmp/perf.data.XXX` - perf_script=3D`mktemp -u /tmp/perf.script.XXX` + perf_data=3D$(mktemp /tmp/perf.data.XXX) || return 1 + perf_script=3D$(mktemp /tmp/perf.script.XXX) || return 1 =20 # Check presence of libtraceevent support to run perf record skip_no_probe_record_support "$event_name/$eventattr/" @@ -61,9 +71,12 @@ trace_libc_inet_pton_backtrace() { fi =20 perf record -e $event_name/$eventattr/ -o $perf_data ping -6 -c 1 ::1 > /= dev/null 2>&1 - # check if perf data file got created in above step. - if [ ! -e $perf_data ]; then - printf "FAIL: perf record failed to create \"%s\" \n" "$perf_data" + # Check perf record actually wrote data. mktemp already created the + # file, so test that it is non-empty rather than that it exists. Quote + # the path: were it ever empty, [ ! -s ] would test the string "-s" + # instead and report success. + if [ ! -s "$perf_data" ]; then + printf "FAIL: perf record failed to write \"%s\" \n" "$perf_data" return 1 fi perf script -i $perf_data | tac | grep -m1 ^ping -B9 | tac > $perf_script @@ -97,21 +110,75 @@ trace_libc_inet_pton_backtrace() { # even if the perf script output does not match. } =20 +# Print the pid scoped uprobes this test may have created. perf probe appe= nds +# _1, _2, ... when the name is already taken, so match those too, but anch= or +# the match: an "inet_pton_$$*" glob would also match the probe of a test = whose +# pid merely starts with this one's, e.g. 123 and 1234. +libc_inet_pton_events() { + perf probe -l 2>/dev/null | awk '{print $1}' | + grep -E "^probe_libc:inet_pton_$$(_[[:digit:]]+)?$" +} + delete_libc_inet_pton_event() { + # Ask the kernel what is actually there rather than trusting + # $event_name: a signal arriving after perf probe injected the uprobe + # but before the assignment to event_name completed would otherwise + # leave the variable empty and leak the probe. + # + # Retry as the addition does. Deleting writes to uprobe_events just as + # adding does, so it can lose the same race with a concurrent test and + # fail with -EBUSY. Re-list rather than assume the delete worked, and + # only give up once the probes are really gone: the name is pid + # scoped, so one left behind here is never reused or overwritten by a + # later run and would sit in the kernel until reboot. + local attempts=3D0 + local probe + + while [ $attempts -lt 3 ]; do + for probe in $(libc_inet_pton_events); do + perf probe -q -d "$probe" + done =20 - if [ -n "$event_name" ] ; then - perf probe -q -d $event_name - fi + if [ -z "$(libc_inet_pton_events)" ]; then + return 0 + fi + + attempts=3D$((attempts + 1)) + sleep 0.1 + done + + printf "WARN: could not delete event(s): %s\n" \ + "$(libc_inet_pton_events | tr '\n' ' ')" + return 1 +} + +cleanup() { + rm -f ${perf_data} ${perf_script} ${expected} + delete_libc_inet_pton_event + + trap - EXIT TERM INT +} + +trap_cleanup() { + cleanup + exit 1 } =20 # Check for IPv6 interface existence ip a sh lo | grep -F -q inet6 || exit 2 [ "$(id -u)" =3D 0 ] || exit 2 =20 +# Install the trap only now that the skips above are out of the way: it ex= its +# 1, so arming it any earlier would turn an 'exit 2' skip into a failure. +# +# The event name is pid scoped, so unlike the old fixed name an orphan left +# behind by an interrupted run is never overwritten by a later run: it wou= ld +# stay in the kernel forever. Always clean up, including on a signal. +trap trap_cleanup EXIT TERM INT + skip_if_no_perf_probe && \ add_libc_inet_pton_event && \ trace_libc_inet_pton_backtrace err=3D$? -rm -f ${perf_data} ${perf_script} ${expected} -delete_libc_inet_pton_event +cleanup exit $err --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Thu Sep 24 22:56:31 2026 Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB16752CCEB for ; Fri, 18 Sep 2026 21:20:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.69 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766427; cv=none; b=JuNgr7b16fv/x2j+5jokwBjhvxcQUvmAvj/jd3xTbTZ7xN6ShAjWWYSIZWmwM5v6UGNH/RdI0m3YEYo9opJaFg5Kxm7d5y3/gNWJ7mi8SXfeoXSR8Ov24egEYq9AmpdKZuiR+/LpYJ+ZBxTYgE3yZYOWUKM1cjo67zBaq/DlxYk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766427; c=relaxed/simple; bh=AoVCVBDnSH1tZsUSOAX8SBrL0Tds9gAMjAESj9HjNRU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VpSgXGngktKkxRfdmREVKGNKxsTYgDaCVEpad9VJvlrHqCVr6RgIHpr9IfBwZsAIrHIMw/kwVjg3KYGTzGLlSYuqnkRTnpw2jlN+mccvd02yEnrkbUk04Y4fWBAch43gMZL12p8KgyGvXWAGGuJOtNY6boPZ/unptaEBXOIAGdU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=eE0ilMMJ; arc=none smtp.client-ip=209.85.216.69 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.b="eE0ilMMJ" Received: by mail-pj1-f69.google.com with SMTP id 98e67ed59e1d1-39de1f85cb2so1518582a91.0 for ; Fri, 18 Sep 2026 14:20:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789766426; x=1790371226; 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=CjdQcI57qkny14hvcDqUMxxP8hpzBsCr23TXxvKZDyo=; b=eE0ilMMJVMiGWt701exW0BaNsq8RTgIWxDfKeqiiNL9kuzWnyNloVE5/pkEVXFonrg uQCFQvQNJE0+GIpsZr/eOJox7fmfD/cf01Wno5zwAPfWBbaIWNzLPBaOWctJN4IbRdNr B6iV2lFA2PXhtkyVwIPnLUbTa67gzR7v4Czp+zMO5fORWU9jRkr3wLquI7ten/SdpxgZ 0GdCXOtuwSMwsr8a/Qz6w8AqnWXLLvFpERSV8q9Dyzh4QZ0zmEyjB8y9bQuHYcVADUHC Pcm0FppBLr5pBajVkSDnLudmQ2R4Uk+LqhFMfN3F2QBmyfM/JBh0XP5xpZSiMJRkyXVW B4pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789766426; x=1790371226; 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=CjdQcI57qkny14hvcDqUMxxP8hpzBsCr23TXxvKZDyo=; b=VejVH5XAM3ZloOEtuoYYkQaVvfhJDKOfLGrbF0UZRnlT5WWZwSpz03zyxvR1WNEqs1 WVLi6SawT4KEQ/euhe/Ky/gUoJ9md9ninCS8tZao+QpsCNNtotuZCsbvfSUWERu6xacF kjF6BcQGVHzTzFOjplPKOd06wQyiz+ABkxzzAKR/XrZBVwKcqUEqu4SzoxFFYQhuYfvk utOQCDp/iQd0MHJ6TSRyGRsl5aZuawsBsjEEmq7ZKrgoKBbdIM9WQkgtcUAgrPc68wI7 0eiIbXIe81Brp+pPyCj1ce+3i6Vr1N+jBDbU9oOcbg+aP2kfJO2LAp6SJ8fnSX2WBwt8 wGlA== X-Forwarded-Encrypted: i=1; AKwUvBzWl8fDQtEYO+5kQmYMZd6JAiOcR2h7kvd0lka1Oi78r9cWcuEIZ7LhBTeuzaJQ3epyg79zArpUX34L9ws=@vger.kernel.org X-Gm-Message-State: AFuF++lKxoT+AN91MYomjJrRfraHIQ5Cvqk5GcE4+Ol1U0ibI60gOlfV 5r3o3ll9660sHIyD7PGEr580pHciYHKLHOxsFK1jys9BFITedVceReK4ZR48WElbv+ATYD9yWXe JE5knHLPIsw== X-Received: from dlbrj1.prod.google.com ([2002:a05:7022:f401:b0:143:9658:f913]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2243:b0:398:c3c9:d0ae with SMTP id 98e67ed59e1d1-39e549bb69cmr7314005a91.0.1789766425636; Fri, 18 Sep 2026 14:20:25 -0700 (PDT) Date: Fri, 18 Sep 2026 14:19:29 -0700 In-Reply-To: <20260918211932.2966061-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918140659.2501976-1-irogers@google.com> <20260918211932.2966061-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918211932.2966061-16-irogers@google.com> Subject: [PATCH v4 15/18] perf test trace_summary: Improve error diagnostics From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org, Howard Chu Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When pattern matching fails in test_perf_trace(), print the command that failed along with the actual match count, the matching lines found, and the last 20 lines of output to aid debugging. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/tests/shell/trace_summary.sh | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tools/perf/tests/shell/trace_summary.sh b/tools/perf/tests/she= ll/trace_summary.sh index b80dea77cec6..4589b4581419 100755 --- a/tools/perf/tests/shell/trace_summary.sh +++ b/tools/perf/tests/shell/trace_summary.sh @@ -28,10 +28,16 @@ test_perf_trace() { =20 count=3D$(grep -E -c -m 3 "${search}" ${OUTPUT}) if [ "${count}" !=3D "3" ]; then - echo "Error: cannot find enough pattern ${search} in the output" - cat ${OUTPUT} - rm -f ${OUTPUT} - exit 1 + echo "Error: cannot find enough pattern ${search} (count=3D${count= }) in output of:" + echo "Error: perf trace ${args} -- ${workload}" + echo "Error: matched lines:" + grep -E "${search}" ${OUTPUT} || echo "none" + echo "Error: last 20 lines of output:" + # The summary is printed after any trace output, so show the end of + # the file: with -S the head of it is the trace, not what is match= ed. + tail -n 20 ${OUTPUT} + rm -f ${OUTPUT} + exit 1 fi } =20 --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Thu Sep 24 22:56:31 2026 Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5F40852E065 for ; Fri, 18 Sep 2026 21:20:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.71 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766431; cv=none; b=aLW0b27dF/ikHr359q0W753CkYXHsDXNvbs9xEe/JygMLg7oe0mzc8dVqXPrtVeAZJ+iCfW0+ajDGBWgTI4Q4J5FsqLsp/xlzlRpFlmhaTpWKymDMFBbBZVy7mM937pAeAa2eOrp5w9NRej4ZmZopqpZTrokVtf7y2BDqtU1WAE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766431; c=relaxed/simple; bh=uXUyVsWXdGRR9r0Yf+u2xCJJUCEXrufHCD6FF1dYRUw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=g9H41N7Yj7ckAjqZv00BHeONtC/A5H8PTZ1Ghanfwhjo8yX7m4x1WGfPVRKUHMJRSKB8WYCsnjKdD4sqSadSdDJ1Yo12hUX+HwG235GTcMXYif1GXivh1EM7i/CWaDLbKciTw87zygdX5K0XsHdzq2KCzGd/3Gdm0nZfcA8PKks= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=g0/FSayE; arc=none smtp.client-ip=209.85.216.71 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.b="g0/FSayE" Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-38f57e31b6eso1138789a91.1 for ; Fri, 18 Sep 2026 14:20:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789766429; x=1790371229; 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=0q/ZlHbTsYZyN2/8M0P/yh3YerzxVzD8tkGrmWQ37IA=; b=g0/FSayEqqeFhQ/karCUQz6ito/iX7Dybm3EVrLz+EJ+Bz6G5dp4kbHLfpQ+eAIb4A IAdIa01tBLPnrOIeOyToG7C/7/coVHVh+nxbd4Wu7CSvfmKSf0x+zBDXuXmO1aeWlPvJ A195xlj6l9jncvFkQwNdQlLS1HiYjkU7mGwaR9ikLa6GgQtA1x1ZCzV8FDtMeEP7h6MU YgMEPrE6WEqNDSqccfm52wsIhy9y0Nb/BIld+HWgmsl2YH7RS2ZCyY8hMxbR46E4o2rW lKQTgJhOw8+8YIUWp7YgwxPx5l5s+4MTYKh0dXxdFFP1NB8AU+ij5qUSg43LA2ShX5Wa zW+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789766429; x=1790371229; 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=0q/ZlHbTsYZyN2/8M0P/yh3YerzxVzD8tkGrmWQ37IA=; b=Zz5zOfh4lnUlDzo2rqmru7H5iovT4zRMkr9d/cESQMYQQMbaAKLFda3DzWMqb5l+GM DK3djXQy1p0xBMZDh5oFFpc2oVIMtDhXEAhjbqn3WHG6VW/ZE6AiIcIzdDuo5MwzrbYK siaDtgw/UnIXw1fSrFb18sW3mQMSL11EtF2V3EPf4c/2JA8dyPZmhZUdaV3/5nftwTR3 8RNNQN4BpyJCKsLtTOWBBStEAEO6MTAoMEFJyTZAa5OawGDmMpqpwqFm69warVkjwiCz EzMA0jjTdD4ixeWECnXkWpgM3qVTLewaI4WLz3LR2+ianGc29wN11egtAepIV/EM1IaG szvg== X-Forwarded-Encrypted: i=1; AKwUvBwhcoCHOE1huE7gZacxvXXhnC0Eg5Y9zpSZz+xSuujzRCRvOJ37PxQLZE2iVw8RbctqO61DcsTUKHWXvw4=@vger.kernel.org X-Gm-Message-State: AFuF++mXLb+2JG2xNRHQFzgL84wjbjBHCZtI5MNGI8RZRjr2G7Ge3MGq amiPyMeAfTPNl8IXZHpaTMoqYnHoSFW/nm8XOB0b9SIRD9We6eJuCf0+pVbvdrTl8R0M3T+wO68 iauJN7XnbSQ== X-Received: from pjbct5.prod.google.com ([2002:a17:90a:f585:b0:39d:ff1c:bcbe]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:141:b0:39e:5ade:f9de with SMTP id 98e67ed59e1d1-39e5adf0579mr4314793a91.26.1789766428532; Fri, 18 Sep 2026 14:20:28 -0700 (PDT) Date: Fri, 18 Sep 2026 14:19:30 -0700 In-Reply-To: <20260918211932.2966061-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918140659.2501976-1-irogers@google.com> <20260918211932.2966061-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918211932.2966061-17-irogers@google.com> Subject: [PATCH v4 16/18] perf test trace_btf_general: Drop --max-events=1 and make non-exclusive From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org, Howard Chu Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" trace_btf_general.sh used `--max-events=3D1` with `perf trace` on commands such as `mv`, `echo`, and `sleep`. When background activity occurs or tests run in parallel, `perf trace` can capture an event from an unrelated process and exit prematurely before recording the target command's syscalls. Drop `--max-events=3D1` and let tracing run until the command completes, checking for the expected output with grep (matching trace_btf_enum.sh). Remove the (exclusive) tag so the test runs in parallel. Running perf trace in parallel is only safe now that the probe tests no longer name their probes "vfs_getname...": perf trace opens everything matching a hardcoded "probe:vfs_getname*" wildcard, which used to pin those probes and make their cleanup fail with -EBUSY. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/tests/shell/trace_btf_general.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/perf/tests/shell/trace_btf_general.sh b/tools/perf/tests= /shell/trace_btf_general.sh index 7a94a5743924..4d654b687a4e 100755 --- a/tools/perf/tests/shell/trace_btf_general.sh +++ b/tools/perf/tests/shell/trace_btf_general.sh @@ -1,5 +1,5 @@ #!/bin/bash -# perf trace BTF general tests (exclusive) +# perf trace BTF general tests # SPDX-License-Identifier: GPL-2.0 =20 err=3D0 @@ -27,7 +27,7 @@ check_vmlinux() { =20 trace_test_string() { echo "Testing perf trace's string augmentation" - output=3D"$(perf trace --sort-events -e renameat* --max-events=3D1 -- mv= ${file1} ${file2} 2>&1)" + output=3D"$(perf trace --sort-events -e renameat* -- mv ${file1} ${file2= } 2>&1)" if ! echo "$output" | grep -q -E "^mv/[0-9]+ renameat(2)?\(.*, \"${file1= }\", .*, \"${file2}\", .*\) +=3D +[0-9]+$" then printf "String augmentation test failed, output:\n$output\n" @@ -38,7 +38,7 @@ trace_test_string() { trace_test_buffer() { echo "Testing perf trace's buffer augmentation" # echo will insert a newline (\10) at the end of the buffer - output=3D"$(perf trace --sort-events -e write --max-events=3D1 -- echo "= ${buffer}" 2>&1)" + output=3D"$(perf trace --sort-events -e write -- echo "${buffer}" 2>&1)" if ! echo "$output" | grep -q -E "^echo/[0-9]+ write\([0-9]+, ${buffer}.= *, [0-9]+\) +=3D +[0-9]+$" then printf "Buffer augmentation test failed, output:\n$output\n" @@ -48,7 +48,7 @@ trace_test_buffer() { =20 trace_test_struct_btf() { echo "Testing perf trace's struct augmentation" - output=3D"$(perf trace --sort-events -e clock_nanosleep --force-btf --ma= x-events=3D1 -- sleep 1 2>&1)" + output=3D"$(perf trace --sort-events -e clock_nanosleep --force-btf -- s= leep 1 2>&1)" if ! echo "$output" | grep -q -E "^sleep/[0-9]+ clock_nanosleep\(0, 0, \= {1,.*\}, 0x[0-9a-f]+\) +=3D +[0-9]+$" then printf "BTF struct augmentation test failed, output:\n$output\n" --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Thu Sep 24 22:56:31 2026 Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1783C52CCE6 for ; Fri, 18 Sep 2026 21:20:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.197 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766433; cv=none; b=e23Q8mbk+eV23MxcNab+MoRUXLFK8+sjGYPoBk0UQJA9yLEQwZ9u6/9q5OfX2POGgduJHJ5VgnHYYTlxaApJoUudk7EAhgljpJh69X2dcZjAdAElpskTmiTV1bBQgKgakpgZodTTWeZ3aZamLn+HYf6RWRGPaR0ZDTnCKtPhyIo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766433; c=relaxed/simple; bh=cUOLtOGC1sSr8NqERNj02XhiosC+nBk1W+33Ik/UsHw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=j5XicgJ4judU937dr0bX9v5tXfWzDbDEW+VOa7sSRjfqMWpBlIVjXFuIpHWucztDHC47U2Hw7CzDLLR32kwnNrTOWAfz07sr7r2NqFsteaWSNhoj3nGfPZ522HoAf5Cv0G3qP+IQux2nbWo+bsgmkiAMbNdadkbAUdTJIAIglrY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=NUQmKjMT; arc=none smtp.client-ip=209.85.215.197 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.b="NUQmKjMT" Received: by mail-pg1-f197.google.com with SMTP id 41be03b00d2f7-cbb20f82a0eso859981a12.0 for ; Fri, 18 Sep 2026 14:20:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789766431; x=1790371231; 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=Y1WBF+lhdsNyuuMsRQ/q34du1kWAD6BuZOicdGwPzPk=; b=NUQmKjMTubi61Jud9fTtJycULxso2hff1VGta3g6yhGPFrUKoplktsZVE/91RrfHHX 210kDUmkO61jB6RIJNEfl4C7SaIpZEYebP0AosPZfvUSjZ7j8Tozme+f6DtdXNhsfOaa 5+4OeUoKrK6ploa/ZoRdDyhdENeyYRnu4RH/X15FftEeqUbFbrtCW1MJpcQFR/FMOVDp IA8I+5zvo7T/DpXOO9/Dkq88Bi9rmXfWeNxW/pK37XWFGtLqfB0ZPSZY0WMbY4+rKA9d ljVyLv0ZQqzB1NMCjju2rNhGgONSRFt3lLfUixwvLj+1si8q9qSJ6Y9OmQTVAbSlaLFG VyXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789766431; x=1790371231; 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=Y1WBF+lhdsNyuuMsRQ/q34du1kWAD6BuZOicdGwPzPk=; b=xHgG5TgcqyF0R/DbK9gDoDFN/oX64L+NO/uy63SmU8hV7SH0+xaXgREQMWgmxf+kTy b3wM2QK7yVFr4gfqllVxoa9ZkJW5HtU3R0bBDp6NpDK787iZOKYl5Np9HEKQpkz/taS6 RRFhFds8Vpa2KEvyyWLDMZ7GQh08swmEvZLmRDqo5exwREbfV7YEg/qzreW3OZPVVDtE /N1FnQ9OOSArgPtXrAaElMatZ758s/1UFoMf6NxJm0FJybOx0SnP7e2LDP1jQnMBxIu7 iCH+JoYj2H8qMu6RGLY/qvB78e9LN838GYADR13Q3vSAJxu+12i9FUmCbP46wY7F/eOe 04Ag== X-Forwarded-Encrypted: i=1; AKwUvBwQyFVjeZeDz9+ZlaBUYdjS3O9lvfONpD5/dr+kCkOTkSVvZ7w/0LRNUJ64NBMtxX0JT6xitldmLYMMdK0=@vger.kernel.org X-Gm-Message-State: AFuF++nrUXare4QIfW0s9UZx0tDzYY4SNyUOnaFezeb0Wz8glh1hkHJU sbgt5VFGlF63J+6bGwpzHNpSaykMHMFWpY5+u4J5TPC8IlXTXh3bgeE8UI8Ca+AXvk4JSCmXCuP 2Lch19QCSlw== X-Received: from dlbds34.prod.google.com ([2002:a05:7022:2522:b0:144:c402:cd22]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:1f88:b0:3da:bea4:6219 with SMTP id adf61e73a8af0-3dd8c33fc47mr6762375637.20.1789766431253; Fri, 18 Sep 2026 14:20:31 -0700 (PDT) Date: Fri, 18 Sep 2026 14:19:31 -0700 In-Reply-To: <20260918211932.2966061-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918140659.2501976-1-irogers@google.com> <20260918211932.2966061-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918211932.2966061-18-irogers@google.com> Subject: [PATCH v4 17/18] perf test trace_summary: Make non-exclusive From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org, Howard Chu Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" trace_summary.sh tests various summary modes of `perf trace`. It already directs output to a unique temporary file without polluting the current working directory. Remove the (exclusive) tag so it can run concurrently in parallel test runs. Running perf trace in parallel is only safe now that the probe tests no longer name their probes "vfs_getname...": perf trace opens everything matching a hardcoded "probe:vfs_getname*" wildcard, which used to pin those probes and make their cleanup fail with -EBUSY. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/tests/shell/trace_summary.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/tests/shell/trace_summary.sh b/tools/perf/tests/she= ll/trace_summary.sh index 4589b4581419..d0196351243d 100755 --- a/tools/perf/tests/shell/trace_summary.sh +++ b/tools/perf/tests/shell/trace_summary.sh @@ -1,5 +1,5 @@ #!/bin/bash -# perf trace summary (exclusive) +# perf trace summary # SPDX-License-Identifier: GPL-2.0 =20 # Check that perf trace works with various summary mode --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Thu Sep 24 22:56:31 2026 Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4AF8952CCFE for ; Fri, 18 Sep 2026 21:20:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766435; cv=none; b=UBBMR1TZt/t+LfG6vpfOdV6gFdItC6F7J+266VXFMFd9mlHrjWjejwWGR4HrxMAuaU3S4wm5OO+xktOB9jZ+r8uF2K8kvJCvF3XyI/U1zMPiIDu/fgqQXxvbF4/IuvggzULHwPlWp/7zCpNgooGROh6/8MiWeMMQZcD8VxBzBfY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789766435; c=relaxed/simple; bh=Ndq3XZzi/EfqSQhUKNfVgunzhKMGpjb6wAnuZVaabKw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mjMmOx4zYuGp5NMpmZgTZ8IIdJei6pXaOmv+vKe6F1CngDcOtqTqFHYixcJ2WdaHmrm0mdNA/64SVsslS60A29tr7Z4+LgfNK8wA1seUsqi6Et0AXFO4tUMc1rdKU7RvrlqwiZLH6StAYe9H2OrYROz7pmzKfd6pkQeV7WF5UxE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=V3/PeDJJ; arc=none smtp.client-ip=209.85.215.199 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.b="V3/PeDJJ" Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-ca8aee88725so1598032a12.3 for ; Fri, 18 Sep 2026 14:20:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789766433; x=1790371233; 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=rtkpF+2hXoqnckm+Z523eWfhpUjaBTRb2bRSM1mpO+k=; b=V3/PeDJJ91tsY35rw+sKxI4ouBGGSmUpP+pNI8zvlnDgidq4g/xEaFKdZ7e3RJa5r1 rKaZGFLPRhTNQFzkBeW53yGluVhLl6eqUHC70k/roITRIuz5pudkYE1XXwfnDXehdJhw w9zqnEn/EuXhVxIzpQd7vCbvaXN+nybEj3J348NJgrZuR/X2xSxmzDSmb6bs/dh0xl9z z7k1CpqnsG2n3tDRezdXnpSwydMRONIMEwXKmvC/DrMbaHt4cmfAREOFC+cJzIQ705mr d0Nr+dcOpGMItDIJsrVUfBvORoxUoWzDwcNBnxhTC6WD0Gcxu5pj6kELU82RhYcUkJcX Cdjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789766433; x=1790371233; 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=rtkpF+2hXoqnckm+Z523eWfhpUjaBTRb2bRSM1mpO+k=; b=wUduyBsfmX+goI7HKARRuQflP8yHb4yBTNZ9Io5dsQTUlnkA2dN8vtAW8zJQrk/Oi6 ZGrRdDsjnGWMNys5HD9d2xo4rtpdB+w5HRQAqiiV1lPtYi2tDEHUx0Dfb3wIx7QCgMdA pzXA1cQIGFUvGAmJ6RjxtZ6vOFNbnVzSFThjOl55RtboaHOnrE6chcVEuhR9kf5J80cM flJpllQx4rPRMCeAKQtoDl9TGIjJ1IYfIIsf9hiEZSsUaRy75c/DAxu9x51N1wPBastr kkJdy//gyg7xgPQbDqLzkzW/A8IjJo3yMadRKT9TH0k/AJb8ElY1VpwoVOYaiQ2K+BuG ixig== X-Forwarded-Encrypted: i=1; AKwUvByxomU3+kNkM1JKlxDs7hAmeWnQF73nCzNxMJCdmUmIicHBbjXazZO8ChvYxKfWgMEwLJM16v5l/S3Iu2U=@vger.kernel.org X-Gm-Message-State: AFuF++mH3cAZMQYrweLd/xMIS6OLLyz3jDFeJGwFeuXBWcic6VYyJStr 5Vg6hzny9Yrkm/CB9MlGydwytYkQNGcrP88igzyAKW2PdqcSSUJVxhMN5MHWxjzMUKOEnDpvBpb wmBXxnrwLXQ== X-Received: from dlec1-n1.prod.google.com ([2002:a05:701b:4281:10b0:144:cfa3:9a66]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:140a:b0:3dd:a197:cf32 with SMTP id adf61e73a8af0-3dda197d889mr1022801637.86.1789766433437; Fri, 18 Sep 2026 14:20:33 -0700 (PDT) Date: Fri, 18 Sep 2026 14:19:32 -0700 In-Reply-To: <20260918211932.2966061-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918140659.2501976-1-irogers@google.com> <20260918211932.2966061-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918211932.2966061-19-irogers@google.com> Subject: [PATCH v4 18/18] perf test uprobe_from_different_cu: Scope probe name to PID From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org, Howard Chu Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The test builds a binary in a per-run temporary directory and probes its foo function. The directory name is unique, but perf probe derives the event name from the probed function and the group name from the binary's basename, so every run registers the same probe_testfile:foo event. Running the test concurrently with itself, as 'perf test -r3' does, therefore fails in all but one of the runs with: Error: event "foo" already exists. Hint: Remove existing event by 'perf probe -d' and a losing run's cleanup goes on to delete the winning run's probe out from under it. Name the event after the pid, foo_$$, so that parallel runs no longer collide. This lets the test stay in the parallel pass rather than having to be marked (exclusive). Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- .../perf/tests/shell/test_uprobe_from_different_cu.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tools/perf/tests/shell/test_uprobe_from_different_cu.sh b/tool= s/perf/tests/shell/test_uprobe_from_different_cu.sh index 7adf9755d6de..47c99d93436b 100755 --- a/tools/perf/tests/shell/test_uprobe_from_different_cu.sh +++ b/tools/perf/tests/shell/test_uprobe_from_different_cu.sh @@ -18,12 +18,19 @@ fi =20 temp_dir=3D$(mktemp -d /tmp/perf-uprobe-different-cu-sh.XXXXXXXXXX) =20 +# The name of the uprobe added and removed below. The probe is placed on +# ${temp_dir}/testfile, but perf probe derives the event name from the pro= bed +# function and the group name from the binary's basename, so every run wou= ld +# otherwise share one probe_testfile:foo event, and a concurrent run would +# fail with 'event "foo" already exists'. Scope the event name to the pid. +probe_name=3D"foo_$$" + cleanup() { trap - EXIT TERM INT if [[ "${temp_dir}" =3D~ ^/tmp/perf-uprobe-different-cu-sh.*$ ]]; then echo "--- Cleaning up ---" - perf probe -x ${temp_dir}/testfile -d foo || true + perf probe -x ${temp_dir}/testfile -d ${probe_name} || true rm -f "${temp_dir}/"* rmdir "${temp_dir}" fi @@ -84,6 +91,6 @@ gcc -g -Og -c ${temp_dir}/testfile-main.c -o ${temp_dir}/= testfile-main.o gcc -g -Og -o ${temp_dir}/testfile ${temp_dir}/testfile-foo.o ${temp_dir}/= testfile-main.o =20 perf probe -x ${temp_dir}/testfile --funcs foo | grep "foo" -perf probe -x ${temp_dir}/testfile foo +perf probe -x ${temp_dir}/testfile ${probe_name}=3Dfoo =20 cleanup --=20 2.55.0.1082.g2b9226bbc0-goog