From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.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 278443EDAA2 for ; Wed, 23 Sep 2026 07:14:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.197 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147657; cv=none; b=ZFEnnPAWb3VwK8MD6sTo8s6ainbybUg+V+CyDcPTB+5zPBx0WZDBrCtQLfXXzKT7tvgJ5Yl3vmHRUQPHe/ulRfSENXAVPLNh6ojPaHLZCqgxjhAF8rRlLLE7528NBNruRBqt7jqEvHrS3TP1EoOdqcZUPLuhKtNsJWoI2C5jKrk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147657; c=relaxed/simple; bh=LdtI4ge8t/WGXKJw6waxWaXwyAYO7yzwcGmaQXwxyK0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tAE/Hidny1tEjeJIFgx7+GGWlP5PVjCQrsH/S9cgCNKWgUNqgcGuw5/Yfl1vzbOH216FMFMYtIMtWLYbUiq4qiEWITNXuROU5KXZ+YBk8eIItu1byMrFOo3d2QyaHaNkjXosGshBMyZ3zv10PSCMUeU/4dCUsnMFdoL4U09+mMg= 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=qE3XF/12; arc=none smtp.client-ip=74.125.82.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="qE3XF/12" Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-3282d5302ffso653784eec.1 for ; Wed, 23 Sep 2026 00:14:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147655; x=1790752455; 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=KAkoHlkHmEth86m+gr1xtW3uL/QzU5kqVxXnVnfJnDI=; b=qE3XF/12aLNBCHDLW767GOfpyWUQ5t7TrLnUflBp8MT5VkjcX72xrmaLplxHB/Em2g ZXIDsmLt5uQukjzI61qNbLuJTpsvCQ2d1vcSlqbbRqU54HzppcThgPUOTEwTj2Fgy9Rj x3LI0dVy4j3b0fQWk23YXPKpyY0IsAQD0ezVdSMPWYTkktrNz3/43tzjJl6nD4KRA3v8 E4HWNKuhLZtmxcfiD/d6eS3bGC1XZAAHglohSYGwBxmaMJH14tmtLdZWMrBO/T45SSqV e/N9hLVI2Ej6PcutzuEJ42bYQikvRnrzMooHNn8RpNjGTSfZnk6kLCDReJPXw6+KTcms SDjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147655; x=1790752455; 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=KAkoHlkHmEth86m+gr1xtW3uL/QzU5kqVxXnVnfJnDI=; b=ktFDWbgUPfZHqMDJk/rOMdYJlD0Xdl6LoIip1GGPRT4lJ+hMGH56gKJ+PqgjHHfxH3 1znVE6EbrNt1CTQZLzK6RMznnWB5qenfrm4ucnV6kPuzVP9eSmCECKCrIU/3jEszrYY1 FpRwJgQO9u01Xr1vzpPNk/hfp2SDMFma5OaRmhZYC3kR/k6Et6Uf470q66zBWF+D3/Hv 0Avfl72ZwSpMqugofJv0aNKQAIfbHl2yRQisLJswsJ7hDK5Cu1uunLS+puoT3jUm8j2a bhamJVd0FT6Cz5gF3mTHWedDx8En03pASlnWj/jK8y1gHGml4NA1wcuERQWC3JEyH3Uf Wo8g== X-Forwarded-Encrypted: i=1; AKwUvBwgd89gFPLgrmfo0krlteL0r6yL/N36qUChGCXvZC6JY9j8kGXEO3yiEKng1JqaoRdxXiJaR46Ek7i7lL0=@vger.kernel.org X-Gm-Message-State: AFuF++kxP0PfY0NHQt2A7rGW+/AZRvIJAeSVX5lARSj3s2tj5EncMTJT BFbHOjm4h7EAud2PB1obKiAKRHEPzxl6au/Jm+SCEwL6206lSAhH2w81syZCUBWNIr5l7Xclcbv 5doRGTvZSZw== X-Received: from dyjf15.prod.google.com ([2002:a05:7300:680f:b0:33e:55a2:f3c0]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:152d:b0:33c:36e9:30d7 with SMTP id 5a478bee46e88-33e5c75de28mr1731181eec.4.1790147654853; Wed, 23 Sep 2026 00:14:14 -0700 (PDT) Date: Wed, 23 Sep 2026 00:13:41 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: Subject: [PATCH v5 01/23] perf trace: Set the augmented arg header in the augmenters that omit it From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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" An augmented argument is a struct augmented_arg header, holding the length of the payload and an error code, followed by the payload. The augmenters build it in augmented_args_tmp, a single entry per-CPU array reused by every syscall on that CPU, so a field left unassigned holds whatever the previous syscall on that CPU put there rather than anything about this one. The string augmenters get both fields from augmented_arg__read_str(), and sys_enter_perf_event_open() bails out when its read fails, but sys_enter_connect(), sys_enter_sendto(), sys_enter_clock_nanosleep() and sys_enter_nanosleep() copy the payload and output the record without ever describing it, and augment_arg() sets the length but not the error. This has gone unnoticed because the beautifiers for those payloads take a fixed sized type and so read the value without consulting the header: syscall_arg__scnprintf_augmented_sockaddr(), syscall_arg__scnprintf_augmented_timespec() and syscall_arg__scnprintf_augmented_perf_event_attr() all cast augmented.args->value directly. Nothing has yet read a length that was never written. Describe the payload everywhere one is produced, so that a reader can bound it by its length. These reads can fail, and none of the four checked whether they had. Rather than claim a payload that was not read, report a length of zero and the error, so the record describes what it holds and leaves the scratch out of it. A reader that bounds the payload by the header then shows the pointer, as it does for a syscall with no augmentation at all, rather than another task's data. The error is set for the same reason the length is, that the header is in a buffer the next syscall on this CPU will reuse and so carries the previous one's value if it is not written, rather than because anything reads it yet: beauty.h still calls the field int_arg and no beautifier looks at it. augment_arg() reported a string it could not read as an empty one before this and still does. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- .../bpf_skel/augmented_raw_syscalls.bpf.c | 53 ++++++++++++++++--- 1 file changed, 47 insertions(+), 6 deletions(-) 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..dd3aa5bd910b 100644 --- a/tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +++ b/tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c @@ -210,6 +210,7 @@ int sys_enter_connect(struct syscall_enter_args *args) const void *sockaddr_arg =3D (const void *)args->args[1]; unsigned int socklen =3D args->args[2]; unsigned int len =3D sizeof(u64) + sizeof(augmented_args->args); // the s= ize + err in all 'augmented_arg' structs + int err; =20 if (augmented_args =3D=3D NULL) return 1; /* Failure: don't filter */ @@ -217,9 +218,16 @@ int sys_enter_connect(struct syscall_enter_args *args) _Static_assert(is_power_of_2(sizeof(augmented_args->arg.saddr)), "sizeof(= augmented_args->arg.saddr) needs to be a power of two"); socklen &=3D sizeof(augmented_args->arg.saddr) - 1; =20 - bpf_probe_read_user(&augmented_args->arg.saddr, socklen, sockaddr_arg); + err =3D bpf_probe_read_user(&augmented_args->arg.saddr, socklen, sockaddr= _arg); + /* + * A failed read leaves the scratch holding whatever the previous + * syscall on this CPU put there, so say there is no payload and why, + * rather than describing another task's data as this task's sockaddr. + */ + if (err < 0) + socklen =3D 0; augmented_args->arg.size =3D socklen; - augmented_args->arg.err =3D 0; + augmented_args->arg.err =3D err < 0 ? err : 0; =20 return augmented__output(args, augmented_args, len + socklen); } @@ -231,13 +239,19 @@ int sys_enter_sendto(struct syscall_enter_args *args) const void *sockaddr_arg =3D (const void *)args->args[4]; unsigned int socklen =3D args->args[5]; unsigned int len =3D sizeof(u64) + sizeof(augmented_args->args); // the s= ize + err in all 'augmented_arg' structs + int err; =20 if (augmented_args =3D=3D NULL) return 1; /* Failure: don't filter */ =20 socklen &=3D sizeof(augmented_args->arg.saddr) - 1; =20 - bpf_probe_read_user(&augmented_args->arg.saddr, socklen, sockaddr_arg); + err =3D bpf_probe_read_user(&augmented_args->arg.saddr, socklen, sockaddr= _arg); + /* As in sys_enter_connect(), do not describe scratch as a sockaddr. */ + if (err < 0) + socklen =3D 0; + augmented_args->arg.size =3D socklen; + augmented_args->arg.err =3D err < 0 ? err : 0; =20 return augmented__output(args, augmented_args, len + socklen); } @@ -372,6 +386,9 @@ int sys_enter_perf_event_open(struct syscall_enter_args= *args) if (bpf_probe_read_user(&augmented_args->arg.value, size, attr) < 0) goto failure; =20 + augmented_args->arg.size =3D size; + augmented_args->arg.err =3D 0; + return augmented__output(args, augmented_args, len + size); failure: return 1; /* Failure: don't filter */ @@ -384,6 +401,7 @@ int sys_enter_clock_nanosleep(struct syscall_enter_args= *args) const void *rqtp_arg =3D (const void *)args->args[2]; unsigned int len =3D sizeof(u64) + sizeof(augmented_args->args); // the s= ize + err in all 'augmented_arg' structs __u32 size =3D sizeof(struct timespec64); + int err; =20 if (augmented_args =3D=3D NULL) goto failure; @@ -391,7 +409,12 @@ int sys_enter_clock_nanosleep(struct syscall_enter_arg= s *args) if (size > sizeof(augmented_args->arg.value)) goto failure; =20 - bpf_probe_read_user(&augmented_args->arg.value, size, rqtp_arg); + err =3D bpf_probe_read_user(&augmented_args->arg.value, size, rqtp_arg); + /* As in sys_enter_connect(), do not describe scratch as a timespec. */ + if (err < 0) + size =3D 0; + augmented_args->arg.size =3D size; + augmented_args->arg.err =3D err < 0 ? err : 0; =20 return augmented__output(args, augmented_args, len + size); failure: @@ -405,6 +428,7 @@ int sys_enter_nanosleep(struct syscall_enter_args *args) const void *req_arg =3D (const void *)args->args[0]; unsigned int len =3D sizeof(augmented_args->args); __u32 size =3D sizeof(struct timespec64); + int err; =20 if (augmented_args =3D=3D NULL) goto failure; @@ -412,7 +436,12 @@ int sys_enter_nanosleep(struct syscall_enter_args *arg= s) if (size > sizeof(augmented_args->arg.value)) goto failure; =20 - bpf_probe_read_user(&augmented_args->arg.value, size, req_arg); + err =3D bpf_probe_read_user(&augmented_args->arg.value, size, req_arg); + /* As in sys_enter_connect(), do not describe scratch as a timespec. */ + if (err < 0) + size =3D 0; + augmented_args->arg.size =3D size; + augmented_args->arg.err =3D err < 0 ? err : 0; =20 return augmented__output(args, augmented_args, len + size); failure: @@ -445,6 +474,7 @@ static inline int augment_arg(struct syscall_enter_args= *args, int i, struct beauty_payload_enter *payload, u64 offset) { int index, value_size =3D sizeof(struct augmented_arg) - offsetof(struct = augmented_arg, value); + int read_err =3D 0; struct augmented_arg *payload_offset; s64 aug_size, size; bool augmented; @@ -467,8 +497,18 @@ static inline int augment_arg(struct syscall_enter_arg= s *args, int i, if (size =3D=3D 1) { /* string */ aug_size =3D bpf_probe_read_user_str(payload_offset->value, value_size, = arg); /* minimum of 0 to pass the verifier */ - if (aug_size < 0) + if (aug_size < 0) { + /* + * Record why nothing was read. The header sits in + * scratch that the next syscall on this CPU reuses, + * so an error left unwritten is the previous one's. + * No beautifier reads it yet, beauty.h still calls + * the field int_arg, so the string is still shown + * as an empty one. + */ + read_err =3D aug_size; aug_size =3D 0; + } =20 augmented =3D true; } else if (size > 0 && size <=3D value_size) { /* struct */ @@ -498,6 +538,7 @@ static inline int augment_arg(struct syscall_enter_args= *args, int i, return -1; =20 payload_offset->size =3D aug_size; + payload_offset->err =3D read_err; return written; } =20 --=20 2.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dy1-f199.google.com (mail-dy1-f199.google.com [74.125.82.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 09603449B1B for ; Wed, 23 Sep 2026 07:14:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147659; cv=none; b=g3M71HbcnarCorj64Xx4gd5FF5wjNTdkVRUlbKCvKrHaHLlkCXZRA1xoItFh44yxzFJ6FZ1aX5XOzw+ax+rkgSdEBRhZiWHarDfIrQmUkygYo+S56z/xPdyIBLu4ogqLfHXVqzy2/MXeR+A6JkYIScP0xv0O0/gWzIoJUR6I65o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147659; c=relaxed/simple; bh=OWJ9liMtUqNhKqaHQ382r8ukgbu/MYHo9cgf8A5Mqyg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=db8yVZ1KfTngA4nnznkQKMDFDaHuHfv7S4LcEEzPSUjlsmdnDsQSgveKvNLiAR3OgbAGLcew9l0cvDfMM1NzzFl5i0QrWNkNJx/VIdQkYxXvUWsKb+QKk6Z4+a3m0tZ0U/CdMYLjzCmiAabxdKSh6klJivN8iG+KcLaCjsqC5GE= 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=lGOaThnZ; arc=none smtp.client-ip=74.125.82.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="lGOaThnZ" Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-33713e5e6daso732652eec.0 for ; Wed, 23 Sep 2026 00:14:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147657; x=1790752457; 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=qMeOUeotXgWQhh7PGWu6xio6F6uBeBIPshzRfQOnbNk=; b=lGOaThnZTHzBaxbULsSCt+XW/06AdMElawoZjUKmnRmti2MWPQmfbYQyeS+EQLhszy 6FerlfMvoBu0P36tYvvNMLUTv2G0GizIGM5bx4U8GLFK+i7ywJi80VCdKnWesLSO+1aF cBOoTmIEgUuR2xmspurZs9iTx1hY/ci0OwU6GuP2n9ZZbB2Gcv1634gcJ5ZgKrVt23B4 QgMRndF4Jl9AobHFFkl8zQUuEw8EKlmtBA8DWiTGx4/RZNk33BMz9Z9No2Fl+puygycF IvmN2+2mx2+XSXGR9LH9eicPruHw9SFmeQpAdtBWzH0ImY0YUtBATnCyHinYPpuD94yh zg6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147657; x=1790752457; 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=qMeOUeotXgWQhh7PGWu6xio6F6uBeBIPshzRfQOnbNk=; b=KDJN5KYyKuWKfkWdbbmJf2AZQaT2OsEar2xP+xknpapW5C0T2Mu1qRH9TrK+frDiwZ YIqKtpIQWCQFHCCAawWFueL09vC4zHqueDSo8moHmsqG6O2/f02NA3OXGEGSTrl6xvLC YwBpFfrlRfO2z4kpnzbOHh6tH7IWe2LjG/gaIID6mJ7QjvPiKimr/J/Cqccy3hVNEc9W ZDfqXRHd2AYA7e32RszvB/Odm+yBCGfm6Tn3F5axWgQrTgq+zrOXuMhlSNM4JrLYtZHa s/5ud7KnzwiWGuuLtfZDyT11mQby0fVCVSjyWor89xUP+2MNditvzOuLbtv6bcA5qBf3 KKYA== X-Forwarded-Encrypted: i=1; AKwUvBz4K+QSTZs/Qly1t4/UKtxZHy1mjMdoou/aHP2S09998NzoxavIp70WwjX/85anyz7iYIVl6ywfUfDSRcA=@vger.kernel.org X-Gm-Message-State: AFuF++mQ3GmiPmvcAQtYtoSFHMJTo0hP5e367bjRz9HrdGMTDNq35J+Q mX97xpEG40xwommJFO0X59j/6A/SMbbZx5R70guglXxHhWunOXA9lkdIIIzDmD5Z4CfURv0cxzm x0AK0xdhvGw== X-Received: from dlea7-n1.prod.google.com ([2002:a05:701b:4207:10b0:144:e5a6:137a]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:b044:10b0:144:eac6:cf37 with SMTP id a92af1059eb24-144f92ec87cmr1963768c88.47.1790147656883; Wed, 23 Sep 2026 00:14:16 -0700 (PDT) Date: Wed, 23 Sep 2026 00:13:42 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: Subject: [PATCH v5 02/23] perf trace: Include the augmented arg header in nanosleep's payload length From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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" sys_enter_nanosleep() sizes the record it outputs as unsigned int len =3D sizeof(augmented_args->args); where every other augmenter that appends a payload uses unsigned int len =3D sizeof(u64) + sizeof(augmented_args->args); the extra u64 being the size and err fields of the struct augmented_arg that the payload follows. The payload is copied to augmented_args->arg.value, which starts 8 bytes after the tracepoint arguments, but only len + size bytes are output. So the record stops 8 bytes short of the end of the timespec, and userspace reads the tail of tv_nsec from whatever follows in the ring buffer. Count the header, as the other augmenters do. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 dd3aa5bd910b..18904bebf672 100644 --- a/tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c +++ b/tools/perf/util/bpf_skel/augmented_raw_syscalls.bpf.c @@ -426,7 +426,8 @@ int sys_enter_nanosleep(struct syscall_enter_args *args) { struct augmented_args_payload *augmented_args =3D augmented_args_payload(= ); const void *req_arg =3D (const void *)args->args[0]; - unsigned int len =3D sizeof(augmented_args->args); + // the size + err in all 'augmented_arg' structs + unsigned int len =3D sizeof(u64) + sizeof(augmented_args->args); __u32 size =3D sizeof(struct timespec64); int err; =20 --=20 2.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dl1-f72.google.com (mail-dl1-f72.google.com [74.125.82.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 DEC5E44BCA1 for ; Wed, 23 Sep 2026 07:14:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.72 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147661; cv=none; b=Ihy9frWuM3mVfj74BA8UH43rh1GAvUgcQ5D3K0UiB3VFzlImVV9uBPahluTq/yu+3nl7opBaB+Khy6jm2RNe/D4Uz4dv1GDHHB/EAyQUl03xEsp36+PsXzLwUiler1kBnHEqYkUDd/0dFx0IW0i8yN7YYAqTWVlEIBgq6L1HXeY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147661; c=relaxed/simple; bh=EiRm2DH268TnCKvfI9DUZ7whv7X6RJy/wJDSopSl0ss=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=daMue/st3BBrQ0wOO6FbJWAmqm4lfR4T1wgqsfFNQ9+Qh1qzWDGcYL6MQW2K6759nP9iKgpPiRV+Wef0jp31Co5p8GdjLddrp8NcRvcCdrw6q7OKBbcTXYzRBTUvBOFIeTr/gxxBdTTYoEkteDk/8aqYC+297t8iAjPV4c0dd7w= 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=eyWXBcrM; arc=none smtp.client-ip=74.125.82.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="eyWXBcrM" Received: by mail-dl1-f72.google.com with SMTP id a92af1059eb24-142f4306744so1621701c88.1 for ; Wed, 23 Sep 2026 00:14:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147659; x=1790752459; 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=Yo0H4ETGVUkK9yLMjrNccVJ0N31e67zNNSIX4uaHhQk=; b=eyWXBcrMcrN71zGriDld1u5ko2VJNy9PYVPMIBoiNHouImIOZBfiJI+ozS4sp/zAq+ Bo5hHOUBwqwUtKn9NFhgC5nkFlDBKEyWka256nY6hDz4eWU8wnYucVM/6b7zzkp/UyJ8 JO1FSWqQDMPEwZP+AAGTYfEXZHUYT5mkXbBZu9jGZqZBwZ+F2eAHY49mxOWTIMeZzKAS WOBv7nytGfctbWncI04vkHZNZSbXTpPU3j+IN74tPGNMrT1KXXVNFFAIVEhyGVTOZukx PN+PSA3AHlBriUMDxxwGvSIxr5dR/O8ojc5TyMwXGmO2deuTAqyI2jIpcg0bqAlazWiy HTSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147659; x=1790752459; 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=Yo0H4ETGVUkK9yLMjrNccVJ0N31e67zNNSIX4uaHhQk=; b=QqnrCriHOzoSYFNYoCQD9DEv3xyfXrtlUvfgkaJZa9U3NDQHKtIsr4FlGuOID2pnex RBTxFpgZZ88WVJTyMxUk17coIJXtrekJxHOnDx5plT384XqRyZ1lMMJe2YxfJeaC0FkW qvYUO2axwJTA1BypHGqVe5WnYDqXp2K6rYyn5FFgzJUIYhGA7TmmXALr/LbjEFONHeUS ot4EdmuaUDvSRegpE6GlkDoZOcqeBp72h02O1eAEEcoTV5U6FLycb1k4AgoOO8Jf4YcA k6MKbdmSDMV5DtWU2O3SmLaQhEmiGefoz1dFqw33hbvjeIAuD0uRWai8p64q/pLy3WRQ xQ3Q== X-Forwarded-Encrypted: i=1; AKwUvByrhDhsFUnoedHkjIh/ndYta1CWGrKalx+VgcMqWNv6FJ9Lujc9go/Fe8oIQrNQMBZH9kpHXOsk9DFoGH0=@vger.kernel.org X-Gm-Message-State: AFuF++lV7cVYActIebkSLaFgIEZc92pZSBLu+yCyzy7DXn0D60Ge9mN/ jAxFJfO3BOAm6BhYUjWh7bFDVy0vieJdtZlH2Pj2gX2LMEHqWJhgqvPh8E/ti3tFMWevwKXzHJS QkDwK20QS0A== 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:a05:701b:4282:10b0:144:f4a0:a399 with SMTP id a92af1059eb24-144f91685bfmr2048914c88.1.1790147658536; Wed, 23 Sep 2026 00:14:18 -0700 (PDT) Date: Wed, 23 Sep 2026 00:13:43 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <4add8796a95e7cabee2a8094d6babdb39290bfe8.1790145937.git.irogers@google.com> Subject: [PATCH v5 03/23] perf trace: Include the headers declaring pid_t, strcmp and assert From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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, Arnaldo Carvalho de Melo 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 Signed-off-by: Arnaldo Carvalho de Melo --- 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.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dy1-f198.google.com (mail-dy1-f198.google.com [74.125.82.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 A001044E052 for ; Wed, 23 Sep 2026 07:14:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.198 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147663; cv=none; b=d+rFCOYR4aGDorVPsLAnU0pldnBIPLWRFRUFyirGmUNnDgJ8JKh7uVVm5rz7ie4fEunW2EruhkBdgmkpmomv6W/yJTHhh8rgoBPlg4xKiDYLd9PU0vRQIecM60+GI47UmjBBMK5FBWH80meXjZStF0M1AJpdTNp7C694OP90cwM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147663; c=relaxed/simple; bh=tfm1p2pRKzqOayDkke0ZfWjT6Us+27QzzqDaoPShW5s=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cpBglm9ALAtq8I0ISz6wRJMvZ7Wpd3Z1Sip7hN3RELxp086DUdWSElQtgxcULTKRN2KYxIEemHS6N4h/ckz2Db8dUGSuRmXVm3X+Fr1UP+fYc59SnfdZ+KcR3EH9R3BtTV8TI8ts6CFifyAAv4k66HwzaAZXkAU4J57u6o7VOQ0= 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=bZNbc5Sg; arc=none smtp.client-ip=74.125.82.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="bZNbc5Sg" Received: by mail-dy1-f198.google.com with SMTP id 5a478bee46e88-33310847fcaso1069529eec.1 for ; Wed, 23 Sep 2026 00:14:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147661; x=1790752461; 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=G1ZTCAAIe4gWr/dGyhrEdH001VOEkbOqkOfQNugcAEc=; b=bZNbc5SgA1njAeGS8yfFUjrbrjMHVPJly4zv9C1WtKp6AGKQz9DrlhScFeV3fP4sGU eFyB0X/c0vQuQa4kzXJhBYiGlZM5TjnPJRJ2dhH3RwDizXzUsPkGWGZ8NScCJ0J8wqXO pxvbj+uHcv4F3L4z/EFFD3xakWKgI2mEoJDH47vlcYmanScHTavI7mmRQm10BOfXQB7Y 2bTjIlMBiiHSnJo/o4e9ORxaWWqBz4FcGwEXKYB0KuzQ6uPDUqxtqe3OuKc+t92/P+j9 GqcZ/Pmv+WueQRMwnUdvOEaZssNHOjDK5s0aibgGb22+gdC3ZeFtueGmS0CBjdIZuWNb J2pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147661; x=1790752461; 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=G1ZTCAAIe4gWr/dGyhrEdH001VOEkbOqkOfQNugcAEc=; b=e3CkCCYyka28Nc7EqeORCI/x3y2VZr7jse8m1ZZ2JF7smxIUFIsbXw1crUduDQpyUo fGJuwhRxiS9thETeAGl5oLU2FzNYOMxZ14N+jtSSOPjyKTtGjtpZRetnbaDgfmAbguQq b1Pt9yNfkl/Rl7seGy8Zk1/kPclxD6DEYDdrJndVRUkSwO6C4/+MtDKRMs2j+1YXyUBL RovDpQlWQ9jIFI4wajlwiDh0gtEpX9A06oYLJ7/HzsvFcREVFdrsrD3C2LKjKit5vVPa AFQk0u2kaSPaxUtF+r0MLlDbXsrA4Ytgk+qPBm5RjnKIZz2eDOR1IYqQQc1tM+QTnE0v zuKA== X-Forwarded-Encrypted: i=1; AKwUvBx6Z7Q39E1U8bzTK75SQXiYisBhFlTkKX0LAQa4Fm3BxDFUoY2FVpdncBDr6HRm6Zu5xzrVaEt3vWJMSkk=@vger.kernel.org X-Gm-Message-State: AFuF++lGtwj94XlMslHUevLS9JXaoAePhSPOLif6UqWY741v9RXCE8L5 CZ6QzOf1CGQW+fsTc8UeRzo7DtvIDqDeItX99gsOypPnsn/EHRtZhKbkD7e7eXAd3SFLb7TCrs+ 3JldGeE5mYA== X-Received: from dymj19.prod.google.com ([2002:a05:7300:6b13:b0:33e:51c3:47b8]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:693c:4144:10b0:33b:e9a7:d819 with SMTP id 5a478bee46e88-33e8c05fe92mr1904022eec.14.1790147660249; Wed, 23 Sep 2026 00:14:20 -0700 (PDT) Date: Wed, 23 Sep 2026 00:13:44 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <3cda6a7189745fc6251f18f9308dca3b1a32a33d.1790145937.git.irogers@google.com> Subject: [PATCH v5 04/23] perf trace: Free the whole evsel_trace in evsel__put_and_free_priv From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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, Arnaldo Carvalho de Melo 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 Signed-off-by: Arnaldo Carvalho de Melo --- 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.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.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 B475744BC97 for ; Wed, 23 Sep 2026 07:14:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.197 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147666; cv=none; b=r8m1iPE2uXge0WGnV+qGLlVObxhAmAWI0m9nvzURQWt5jAi3CGgMrfF9Hz80avbgrT99m54l1qiWTNZ/C+UbUoqcxAvDJxViA3OQJDxvsXk5hTLfAt5Pt7EIOpqyjP61kKrvXOqbHXE+jsUI6ld1Rvc6ZrAonf/iP2n1JQZcWn4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147666; c=relaxed/simple; bh=NG61hDMAN/8MqjNmPkTpk+d48KbTt9ihnQruMIE3O58=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KlJD8LOaCpwdNF62icLNiEcee2D3+lZbP3pc5+EdFWPF3uW7qVTNWYW5Xwqx1vHiIeo0IzjOdytM/SfHn6UkEbu7rdJJKkPrey3cPTMHQLkLXHV5Q7v/CyapRigcu2au/tvoiq4IPsQNgNHjgti7B6YNTZoU3KvE9ZDpVNajms8= 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=WSB9c9YV; arc=none smtp.client-ip=74.125.82.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="WSB9c9YV" Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-30bcb065bfdso818112eec.0 for ; Wed, 23 Sep 2026 00:14:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147662; x=1790752462; 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=EXZwHe62caikgyPiwv6l+s4W+ytiH/MmGz+IbCWr/dc=; b=WSB9c9YVtZfV8l/P2utQYC+VThTXYxnlOsJDpae1yifMDe4bDeNY5Gk/zFHQbaTfcY zzh+T3xDyZc7PukBbE35InGlE7YgImyiNoiqunDBAlKAySvsv/AM1/e2t08WZsQ1WKAh 8tVplClvGUyG1iETPlfTYO2Tq7/FBt5rlCE87S2yG78Eg9SP2/l+IQIoMQLxpm6ZTeT0 iOMXuft/DyG96pdYUkhrkm0i0qd1Q8qvvGvcyUyBOwbeI12elHnKaLB+LSzYZ1BPDw+k 0ObnYUJ/Cer0M5COYSMTBtE9KcPqLI0nxm7iiZiUrLr08PzAHniY9Fs6w/b+QucwjMzc Gq6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147662; x=1790752462; 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=EXZwHe62caikgyPiwv6l+s4W+ytiH/MmGz+IbCWr/dc=; b=hl3dxngDOVJ0Ua7mRo5F8XPp5zMzLQUkuZ0aM9jZfT4zujrMzpMNiy0Oxp39GwxIB8 znBB2+TLF+AK8ytlFeYdnX6Brb75A9by1z4jmPqu2MgB5V7dkV2qOVpmb7EOZkW6KI7M Cwiga+9Kt5PONby/wEaDnLnmay6i1EOS8q3RHG3wE5XXqwQZAx9VBMt+9uqYg+Zmwdzw uwdxkb2Zby1AG41Or5f8YeKUdSGzbAocWJ4u25Yf4rx22oZ2FFwYSfqFegFy36pwJ50a fuy3Jr/zuGZzMmfHuEZ8vM+uPEwrmMCMV74FKDno9jIMNbX9Tgwe8e5UjKfap94GOWrw KQtw== X-Forwarded-Encrypted: i=1; AKwUvBzbzj6MfNV33sZtwmnuLgIPEiecEFQmNM1SM0XGdDNiku4zw2g+7+/AFO9oPqq3OymY4DEiljb8Yj4kq8g=@vger.kernel.org X-Gm-Message-State: AFuF++nOe5zNpdXwbxxnZcNsDybiPSi3uhNwUDV2q9MYuKU+FEbwpVYq dEDFaLD1VCh3lvisxNkLs9lKhW/EwrBisiDEtks295sPvCYFivVo0fwdtE/rsTZa94raotLXVq+ FTV+/4i9lJw== X-Received: from dybvu25.prod.google.com ([2002:a05:7300:f819:b0:33e:54b9:7324]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:693c:88c8:20b0:33b:e3c8:aef4 with SMTP id 5a478bee46e88-33e95f69048mr1489523eec.0.1790147662251; Wed, 23 Sep 2026 00:14:22 -0700 (PDT) Date: Wed, 23 Sep 2026 00:13:45 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <89815a65029985761ed8356f8a648ab128fdebbc.1790145937.git.irogers@google.com> Subject: [PATCH v5 05/23] perf evsel: Report an allocation failure as ENOMEM when setting filters From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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, Arnaldo Carvalho de Melo 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 Signed-off-by: Arnaldo Carvalho de Melo --- 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 22eb01116992..9392d912d254 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -1432,8 +1432,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) @@ -1452,8 +1456,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 0d0b2804d238..dd468716f424 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.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dl1-f70.google.com (mail-dl1-f70.google.com [74.125.82.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 C613E455603 for ; Wed, 23 Sep 2026 07:14:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.70 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147667; cv=none; b=ZbJAfScI7oxYzI5av56+vSsnAOyebVhV0DlilvogfObAkFh2+xRXL7aQFNBgpz7z+65wNaCulZzaHLuAsqDDsX/A8nN7EyPhHwwhVqFnEYo9RJXuLKiWow4I0UHuLID29hLzBfgd9WblaMaFWhzoz+oWRufrQEnUgk2u6bsAYbk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147667; c=relaxed/simple; bh=ZFu28bTIZtHoVVmRm8bIPfJIFnqmIuxAuxjddZbdwck=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BGO9rjW9+VFEXUpjuLRaYMWvOaszQQlg8nhnoknYUUDL0Wa50C/qM6/B7kryTECkG2wJ3gJ9ZDEejLK3kyAp30wKYJJjSAYlzmYkhkv9Wx0N2B3daLETVX+HSusFD92UbAINhzLRusGcIn6JlxwaKp5Lhd4D4BfcwjVKVs9QhxQ= 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=Cuq4fRJ+; arc=none smtp.client-ip=74.125.82.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="Cuq4fRJ+" Received: by mail-dl1-f70.google.com with SMTP id a92af1059eb24-14371f7ba1eso942390c88.0 for ; Wed, 23 Sep 2026 00:14:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147665; x=1790752465; 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=9AtiWY+mIovc0ii7uMQ428LIkWxsAI+yH5okNrkOa1c=; b=Cuq4fRJ+wrr22Se61VE0RWjHB17vY+CvOZU/LQEk24Djm9WfdhQXhyoxa/qCfP5V0X W6qsYxjnXd3qpF5uNSOM664Fe/ywTALqxbrSEc+dABwWYvaigCGxfdXcZl2gXBVKekGv 1XlyZaS+XnYGcbvbcLI1l3n0XgLlrukwWLjUirBHUZHwdRyiOcd7Jb/wmoV++y6GNd7d NPtePQcwbt8DGmP20IcC9E4jemlGsydrzI1RbIPssmHNVVJn6mGDCgUpvV0CTN/jSs9T ly0LF7WLR17AQ1arVEP3BQQnsvNq88IMljrELaR2YE4pFnMZKIrdiot7K0yqnyU7az3v WrtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147665; x=1790752465; 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=9AtiWY+mIovc0ii7uMQ428LIkWxsAI+yH5okNrkOa1c=; b=yucya5vB8VdA5+LF8xLL8apw9YGeJPZYRGoB+8CC8osS98bn+B190S2OytRztGyLhe 9SYwil0TfcSeSbhjtmZbKncvGSdmTeBLDEctRTKlqmBD4ahvGQJhBX6XiWmktFPhKHkX Z2obqDY0HRSIsemYnWaxVEY+X5C8OIDaS81VhKTKWTZi7bt3NAK2L3i5Xtn+Q8WsbPQA mTMPRNq/OSClZdmHHmLP3CI8ZbxW+9eNkdoTyLRtfXnkHPePoWqil+Gh543bE+K+z9Jl 5sZXCfDv/fXpJxFymDD3lDmZQm1aLgyO+ThH+y1YmZE8X0NckEdu92PvZsbm28xaYacM GnjQ== X-Forwarded-Encrypted: i=1; AKwUvBzlIRlaB6H8kYlbcS+aXlVVM2E2k4CirECH8wYS/KXTNp86PhPcVAL07QLRqYDW0fDfoN+ATLOqI+8xDfg=@vger.kernel.org X-Gm-Message-State: AFuF++l+FZe5slPa5wIfI+3mDwLuQGr0FqqhbakC8xzQERAZXOTPen2B srpJEqHCVYTtsjI3pL6pyhtnybayJJTYoYgmijVFl6yg9Eq5HdfLd4eARopKtQNJb2Til10FiaT V0cy/H19ddA== X-Received: from dlae4.prod.google.com ([2002:a05:701b:2304:b0:144:db76:3e5f]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:6298:b0:144:f096:4dda with SMTP id a92af1059eb24-144f916f00cmr3314921c88.43.1790147664407; Wed, 23 Sep 2026 00:14:24 -0700 (PDT) Date: Wed, 23 Sep 2026 00:13:46 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <5289bf094c7e9d37990d2d36fdf2deb025478c1f.1790145937.git.irogers@google.com> Subject: [PATCH v5 06/23] perf trace: Start BPF summary before starting workload From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dl1-f69.google.com (mail-dl1-f69.google.com [74.125.82.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 AAEE5457E5C for ; Wed, 23 Sep 2026 07:14:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.69 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147670; cv=none; b=KwWCtcrlD5B0gsBY3Zr/Fl1IwbHjRqfeM+Nj3ZOUfZL3wPgvq/oHi1XnkzqThTsyqhmv27LZKfPJUS6CZXL4Wl5cqkvV1WX+b09lLT4AGHG1eQWyd6OO7g0p2wBfnCkBdFsHa8HDJ30CcGFkx9MW2g9d4kUqPCTmqBv+On7V8Dw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147670; c=relaxed/simple; bh=Tj3yDs8rwCDj/BB1fmxJiip60F8wgE92QfWT3F32Oec=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sv2wjGcxBb6RhB8yiaPsvQAtAFupIg9WumQso2AwklpQEPDaP0igxog+QugRdxWZ+6j+rJmseNETOgrI06/nVNPe9RJEASmGptq7yXR/eHYWMY0jTrfSN4u3aEwU1gHm5KyOs6KBGhtyekrmnr9bHeXtvva6ETTg+bvKF2oPqIg= 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=FofFd/iB; arc=none smtp.client-ip=74.125.82.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="FofFd/iB" Received: by mail-dl1-f69.google.com with SMTP id a92af1059eb24-1438fef603cso893216c88.0 for ; Wed, 23 Sep 2026 00:14:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147667; x=1790752467; 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=liyi0vSB4PI71vdWOqs25tQFPCa2XdpECr64MsD1W7w=; b=FofFd/iBefhoZ9ZV/HiD3eE32TTbKoGDkFyCUL3S1TomXOgnJC43ajGCTN21a7EEX7 LPTTRqcvxgUiNm65RWATRqLB6l8WXA+0rV0SkmLhP+1LSuGCfnJj8+NSq9laUmE4KKxs Qin/1NV+sXZEc94R7AbujNnvnT4pXOrBjujfVWq13i/Rk3+5BMV7x2Izrych75QYSNo4 uTaIZE3fiAETxatS3xVdCjM60pfv8xrh3SZu3GY57EI6BzQQkHWyeXW9UJyqxV6eKCBz l/l9Q6iqbDQxPJ6kyDAg5cjDe9dZbKuEO71uDiyMlaX6tJ6PsnMg6GiKrMKrub3/F2nl r9dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147667; x=1790752467; 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=liyi0vSB4PI71vdWOqs25tQFPCa2XdpECr64MsD1W7w=; b=ACyDJh1dhP9hVZ21AryBVEolrSSv4p2/0LRjRYM5D4jh1XCPtLflqVVisJpUmFXwBR 4Wn4EXu5QcUXuhwvctoG3XjvSDbNqnciXJWOsXTa++6D3iWzSyRHZ5h0WwNqLhG4OaUL TA8yEsGqk6vr64RkyfjlLRZJ3oTa0jPaglhpwbcR9SqPa2knsuYSlYBwGPb4dWTh6WOK p0j7rXp7v/erdrayjqQaHodwupvRl44t4J4DPW9JpYFl3u3h8QAc7XKB8DPiQbfii4MA LxFAUE3b3rRqwSietf4y3HzQ24Cms6/8Nzc01crBv1RPyLsG7ciSvAFiVAfVZT4YgCUF I0Bg== X-Forwarded-Encrypted: i=1; AKwUvByWIJNKV7eV95f/uzzcbdj2xt2dUBydVknpKHMK/EYpMYQR86xEaOiLkrEL9imzocOjrPirZXmS/E0VBAA=@vger.kernel.org X-Gm-Message-State: AFuF++lz6LiWRjbRJim1Z/nX0eSXzV+YZJxCz4KvS6v5YI8L3yaKK27U V5mWqS8ao1CLlkignpwZcwjStPHpl2wEtY77eBlJ5pRmfM3icoD/7epUFlkV3wDBNMryaECMaXR moPGIwaU8vw== X-Received: from dlam20-n2.prod.google.com ([2002:a05:701b:2094:20b0:144:ece5:c0c4]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:2044:10b0:144:c112:5f23 with SMTP id a92af1059eb24-144f9179515mr2172461c88.24.1790147666238; Wed, 23 Sep 2026 00:14:26 -0700 (PDT) Date: Wed, 23 Sep 2026 00:13:47 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <4315f435177b66e0b25ca9b74a89cc2f51d3801d.1790145937.git.irogers@google.com> Subject: [PATCH v5 07/23] perf trace: Skip internal tracepoint fields in formatting and beauty map From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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. Leaving the slot out is only right for syscall tracepoints, where syscall__scnprintf_args() indexes the array the same packed way and sc->nr_args is counted to match. Every other tracepoint reaches the array through trace__fprintf_tp_fields(), which advances the field list and the array in lockstep, and for those a __data_loc char[] is an ordinary dynamic string to be shown rather than something the kernel appended to a syscall behind perf's back. Add a skip_internal argument so that each caller gets the layout it indexes. 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 | 194 ++++++++++++++++++++++++++++--------- 1 file changed, 148 insertions(+), 46 deletions(-) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index f90c6bb4d8b4..6ffd4a0718ca 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -2276,22 +2276,40 @@ static bool field_is_ptr_sized(const struct tep_for= mat_field *field) return field->size =3D=3D ptr_size || field->size =3D=3D sizeof(u64); } =20 +/* + * @skip_internal: whether an internal field is left out of the array rath= er + * than given a slot of its own. + * + * Syscall tracepoints want it left out, as syscall__scnprintf_args() walk= s the + * array with an index that skips internal fields too and sc->nr_args is c= ounted + * the same way. + * + * Other tracepoints do not: trace__fprintf_tp_fields() advances the field= list + * and this array in lockstep, so every field needs a slot or the fields a= fter + * an internal one are formatted with the wrong entry. For them a + * __data_loc char[] is also an ordinary dynamic string to be shown, rathe= r than + * something the kernel appended to a syscall tracepoint behind perf's bac= k. + */ static struct tep_format_field * syscall_arg_fmt__init_array(struct syscall_arg_fmt *arg, struct tep_format= _field *field, - bool *use_btf) + bool *use_btf, bool skip_internal) { struct tep_format_field *last_field =3D NULL; int len; =20 - for (; field; field =3D field->next, ++arg) { - /* assume it's the last argument */ - if (is_internal_field(field)) + for (; field; field =3D field->next) { + if (is_internal_field(field)) { + if (!skip_internal) + ++arg; continue; + } =20 last_field =3D field; =20 - if (arg->scnprintf) + if (arg->scnprintf) { + ++arg; continue; + } =20 len =3D strlen(field->name); =20 @@ -2348,6 +2366,7 @@ syscall_arg_fmt__init_array(struct syscall_arg_fmt *a= rg, struct tep_format_field } } } + ++arg; } =20 return last_field; @@ -2356,7 +2375,8 @@ syscall_arg_fmt__init_array(struct syscall_arg_fmt *a= rg, struct tep_format_field static int syscall__set_arg_fmts(struct syscall *sc) { struct tep_format_field *last_field =3D syscall_arg_fmt__init_array(sc->a= rg_fmt, sc->args, - &sc->use_btf); + &sc->use_btf, + /*skip_internal=3D*/true); =20 if (last_field) sc->args_size =3D last_field->offset + last_field->size; @@ -2368,7 +2388,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 +2428,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)) @@ -2452,7 +2484,8 @@ static int evsel__init_tp_arg_scnprintf(struct evsel = *evsel, bool *use_btf) const struct tep_event *tp_format =3D evsel__tp_format(evsel); =20 if (tp_format) { - syscall_arg_fmt__init_array(fmt, tp_format->format.fields, use_btf); + syscall_arg_fmt__init_array(fmt, tp_format->format.fields, use_btf, + /*skip_internal=3D*/false); return 0; } } @@ -2642,10 +2675,16 @@ 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; + + if (arg.mask & bit) { + ++arg.idx; + bit <<=3D 1; continue; + } =20 arg.fmt =3D &sc->arg_fmt[arg.idx]; val =3D syscall_arg__val(&arg, arg.idx); @@ -2664,8 +2703,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 +2722,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 +2992,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 +3012,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 +3088,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 +3139,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 +3157,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 +4190,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 +4218,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 +4247,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 +4259,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 +4271,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 +4291,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 +4309,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 +4354,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 +4367,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.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.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 83F5745A28E for ; Wed, 23 Sep 2026 07:14:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.197 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147671; cv=none; b=A3P7Ji9OXmiaLE/vEG22f64AOdjlRe7L9nq/2u+1NNLYUL0Srr/opc9Xi4v+ePeRrGQxMn66ubzC2Jch4nmIgmLmuUBjU0wnJEq9BxpEtw8myT58k0XqgPT6xcXRPywan8VxLM9vQ7FLAGxG8R+hvK3OBu2WKIYfwYMLJHMpDa0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147671; c=relaxed/simple; bh=5k0rS1+XJCFv2HYR1dRWlXW00OtVoNzmb+6fvtbV4C8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hbUICJ87zMSqOyuDUMNs3JSw35s93q70YUgE8a9Gs8vUUsG0+xtbhnFAyIN3hmhOMygXh435XmP+lX2rPbOEv1TLCGMLCtiACT6qtZJmBn3VZMO1jSUnU8vaPloG7DIyqKt3h5HlCu7hEV51FFfruT7oUvQ9LHzPA9rK7XvF1T4= 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=u17uAwSU; arc=none smtp.client-ip=74.125.82.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="u17uAwSU" Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-33f01b74124so139416eec.1 for ; Wed, 23 Sep 2026 00:14:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147669; x=1790752469; 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=B5G/9q1PZ/l1tPYX6weOFHFsE+Y/Z2BolEBxDoK3efw=; b=u17uAwSUpI+7i+cpxcnJezChsy3dSQMsmBdJ62502nd5lzpdDRa5+/97XEbLvCqf31 wyPmQBlpW8L2dt/L451bEimQzbb5ZpiPOJQDUF7PdoEL2qlE6wAzxK3WAfVHkVRs2qYo 5ZUqMkEjYdqFsjIGwyDXK6T1mgJ9p8Ifn7yBB6BQNaP/3VRinPSPC9oMcP4BxrwXVy/O Sm8eAQnkWon7sthHQS/FqnsWAwlJiBLTUj5SmBPYfpxNQFe8ETsbBEs27YNCpzX7EYnh lFG0mwWx6W+g76Wi0YbdbvXgmG4uvD2lUqZJTLyhDV5vnrjLuA6WE0pvE/zxWl+OFX+C fqNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147669; x=1790752469; 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=B5G/9q1PZ/l1tPYX6weOFHFsE+Y/Z2BolEBxDoK3efw=; b=wngKcfbt9gZFq4NbrpH56eUDpMlZfQ+RubYrtK4gej/E9PyXNxXlMOcsc1niU3+J2/ U2YdNw9NjDgq6zxA+BakQDux5bSQeCZYcqtP89vpoYCarEh+m6dcUaFoXONTItW4cXEh +f8SpF+3nxFAbzG/fMq9yKDB4M19Pi+51mBryLE/lEoMCP8uWYS/cVhfd99vEVQe1nIU jGuarHBIxt9iLWdDmmFnnehibVP2lVMk/6EVE+OwNuY3n3G4cjkZYcPChPuVDVuXaZGA FztTUOjlFYU5cxFVT3/hQAGGJqxYz+i1pasmdKAhZALdB5u15BSeXm8hasYkO4Qs+6sq ADbQ== X-Forwarded-Encrypted: i=1; AKwUvBwdso7M5Qf8c8vsPly8LyLb9z+O+LV+hKgYEfzlXamWq/zNO7vWJ1TEzrOSeRpuctpH/85ftu1x771CYMQ=@vger.kernel.org X-Gm-Message-State: AFuF++nePQ73jJXwpJnly9xFK4X11Obeon8yYoujMuDuk5aAGxMVwypv CPHT/4M1nFsB5Hjwfikjhxa0bQqSZLOOTKtCcs0Ww3whnIwfAr/ItpZzLqoeM4J/Rqc8psgJg9p Ht85zikDcfA== X-Received: from dlbtp12.prod.google.com ([2002:a05:7022:3b8c:b0:144:e5c8:7eb5]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:3c0b:b0:143:1bb1:b3cc with SMTP id a92af1059eb24-144f8e175cdmr1801592c88.23.1790147668250; Wed, 23 Sep 2026 00:14:28 -0700 (PDT) Date: Wed, 23 Sep 2026 00:13:48 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <99ab722ef23fa269a0c6a95381a7493fcd5d6e0a.1790145937.git.irogers@google.com> Subject: [PATCH v5 08/23] perf trace: Bounds check augmented arguments before reading them From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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" Augmented arguments are length prefixed payloads that the BPF augmenter appends to a sample: a struct augmented_arg header holding the payload length, followed by the payload. arg->augmented.args points at the next unconsumed one and arg->augmented.size says how many bytes are left. Both the length and the number of bytes remaining come from the sample, but syscall_arg__scnprintf_buf() takes the length on trust and loops over it with no bound at all, so a bogus value walks straight off the end of the static argbuf that syscall__augmented_args() copies into: #0 0x73104f in dump_stack debug.c:366 #1 0x7310c5 in sighandler_dump_stack debug.c:378 #2 0x7f749ea66fb0 in __restore_rt libc.so.6[19fb0] #3 0x4c0fa0 in syscall_arg__scnprintf_buf builtin-trace.c:1955 #4 0x4c2f3d in syscall_arg_fmt__scnprintf_val builtin-trace.c:2632 #5 0x4c33ae in syscall__scnprintf_args builtin-trace.c:2722 #6 0x4c43d3 in trace__sys_enter builtin-trace.c:3094 #7 0x4c7865 in trace__handle_event builtin-trace.c:4013 syscall_arg__scnprintf_augmented_string() likewise prints the payload with "%.*s" and a length it has not checked, and both dereference the header before testing it for NULL. btf_struct_scnprintf() already gets this right. Lift its check into a shared syscall_arg__augmented_args_valid() in beauty.h, which verifies that a whole header is present and that the payload it advertises fits in what is left, taking a @need argument for callers that go on to read a fixed sized type, and use it at each of these sites. An unusable payload now makes syscall_arg__scnprintf_filename() fall back to the vfs_getname probe or the raw pointer, as it does when there is no augmented data at all, rather than printing nothing. Reported-by: Arnaldo Carvalho de Melo Closes: https://lore.kernel.org/linux-perf-users/arJ-gpzqOHk-gF8T@x2/ Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/builtin-trace.c | 29 ++++++++++++++------- tools/perf/trace/beauty/beauty.h | 43 ++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 9 deletions(-) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index 6ffd4a0718ca..eeaaab44016c 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -1139,14 +1139,12 @@ static size_t btf_struct_scnprintf(const struct btf= _type *type, struct btf *btf, LIBBPF_OPTS(btf_dump_opts, dump_opts); LIBBPF_OPTS(btf_dump_type_data_opts, dump_data_opts); =20 - if (arg =3D=3D NULL || arg->augmented.args =3D=3D NULL || arg->augmented.= size < (int)sizeof(*augmented_arg) || + /* btf_dump__dump_type_data() below reads type->size bytes of the payload= . */ + if (!syscall_arg__augmented_args_valid(arg, type->size) || arg->fmt =3D=3D NULL || !arg->fmt->from_user) return 0; =20 augmented_arg =3D arg->augmented.args; - if (augmented_arg->size <=3D 0 || augmented_arg->size > arg->augmented.si= ze - (int)sizeof(*augmented_arg) || - (size_t)augmented_arg->size < type->size) - return 0; =20 dump_data_opts.compact =3D true; dump_data_opts.skip_names =3D !arg->trace->show_arg_names; @@ -1904,12 +1902,18 @@ static void thread__set_filename_pos(struct thread = *thread, const char *bf, static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg *= arg, char *bf, size_t size) { struct augmented_arg *augmented_arg =3D arg->augmented.args; - size_t printed =3D scnprintf(bf, size, "\"%.*s\"", augmented_arg->size, a= ugmented_arg->value); + size_t printed; + int consumed; + + if (!syscall_arg__augmented_args_valid(arg, 0)) + return 0; + + printed =3D scnprintf(bf, size, "\"%.*s\"", augmented_arg->size, augmente= d_arg->value); /* * So that the next arg with a payload can consume its augmented arg, i.e= . for rename* syscalls * we would have two strings, each prefixed by its size. */ - int consumed =3D sizeof(*augmented_arg) + augmented_arg->size; + consumed =3D sizeof(*augmented_arg) + augmented_arg->size; =20 arg->augmented.args =3D ((void *)arg->augmented.args) + consumed; arg->augmented.size -=3D consumed; @@ -1922,7 +1926,12 @@ static size_t syscall_arg__scnprintf_filename(char *= bf, size_t size, { unsigned long ptr =3D arg->val; =20 - if (arg->augmented.args) + /* + * A truncated or otherwise unusable augmented payload is no better than + * not having one at all, so test it here and let the pointer or the + * vfs_getname probe name the file instead of printing nothing. + */ + if (syscall_arg__augmented_args_valid(arg, 0)) return syscall_arg__scnprintf_augmented_string(arg, bf, size); =20 if (!arg->trace->vfs_getname) @@ -1938,13 +1947,15 @@ static size_t syscall_arg__scnprintf_filename(char = *bf, size_t size, static size_t syscall_arg__scnprintf_buf(char *bf, size_t size, struct sys= call_arg *arg) { struct augmented_arg *augmented_arg =3D arg->augmented.args; - unsigned char *orig =3D (unsigned char *)augmented_arg->value; size_t printed =3D 0; + unsigned char *orig; int consumed; =20 - if (augmented_arg =3D=3D NULL) + if (!syscall_arg__augmented_args_valid(arg, 0)) return 0; =20 + orig =3D (unsigned char *)augmented_arg->value; + for (int j =3D 0; j < augmented_arg->size; ++j) { bool control_char =3D orig[j] <=3D MAX_CONTROL_CHAR || orig[j] >=3D MAX_= ASCII; /* print control characters (0~31 and 127), and non-ascii characters in = \(digits) */ diff --git a/tools/perf/trace/beauty/beauty.h b/tools/perf/trace/beauty/bea= uty.h index 0f4801c61a5b..71f93372c34e 100644 --- a/tools/perf/trace/beauty/beauty.h +++ b/tools/perf/trace/beauty/beauty.h @@ -124,6 +124,49 @@ struct syscall_arg { =20 unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx); =20 +/** + * syscall_arg__augmented_args_valid: can the next augmented arg be read? + * + * Augmented arguments are a series of length prefixed payloads that the B= PF + * augmenter appends to a sample, with arg->augmented.args pointing at the= next + * unconsumed one and arg->augmented.size saying how many bytes are left. + * + * Both the remaining byte count and the length prefix come from the sampl= e, so + * neither can be trusted: a sample that carries no augmented arguments at= all + * can still have a few bytes of ring buffer padding after the tracepoint + * payload, and those bytes then look like a struct augmented_arg holding = an + * arbitrary length. + * + * Check that a whole header is present and that the payload it advertises= fits + * in what is left, so that a beautifier reading augmented_arg->value stays + * inside the sample. + * + * @need: how many bytes of payload the caller goes on to read, zero when = it is + * driven by the length prefix itself rather than by a fixed sized type. + */ +static inline bool syscall_arg__augmented_args_valid(struct syscall_arg *a= rg, size_t need) +{ + const struct augmented_arg *augmented_arg; + + if (arg =3D=3D NULL || arg->augmented.args =3D=3D NULL) + return false; + + /* The header has to be there before its size field can be read. */ + if (arg->augmented.size < (int)sizeof(*augmented_arg)) + return false; + + augmented_arg =3D arg->augmented.args; + + /* A negative length would make the bounds below meaningless. */ + if (augmented_arg->size < 0) + return false; + + if (augmented_arg->size > arg->augmented.size - (int)sizeof(*augmented_ar= g)) + return false; + + return (size_t)augmented_arg->size >=3D need; +} + size_t syscall_arg__scnprintf_strarray_flags(char *bf, size_t size, struct= syscall_arg *arg); #define SCA_STRARRAY_FLAGS syscall_arg__scnprintf_strarray_flags =20 --=20 2.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dy1-f199.google.com (mail-dy1-f199.google.com [74.125.82.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 5935B456E1E for ; Wed, 23 Sep 2026 07:14:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147673; cv=none; b=RHTlNGAZparwqMx9a73d3TrGmX/LcMWKK6j7ddSZpJzf0H46MVEHYYJVpviSY3Pe7Zlz7LLDbq+7csbRFXzLvs8dabWX4bwOmOCFBBPhuLCAsUi25Od7UlUrCXQfhVJdYCQ6edlC6T/sG62vTMqfPnilGyEi5P28HYQT29kRY5c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147673; c=relaxed/simple; bh=ClBRPt0PX+BdDlotzkipLlfgDTGCEc0HqYURNxfWCBQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ai1Jyq2ahDaMIzE+de5MBCP+V+nG+xqF66B4Dsg9sxNU4+oG1UTWmDi1ExbqkRhulj48AhxDdgEm6z7I1IG5pBTMZejRdzVkooul8hxFDOTRNl1hJmo5ODAi0mQqkV/npGW5Vgkax72d30GdOC36ACmPi8OZYVHspLRIXTsWSAM= 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=UP8ucDgs; arc=none smtp.client-ip=74.125.82.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="UP8ucDgs" Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-30bcb065bfdso818222eec.0 for ; Wed, 23 Sep 2026 00:14:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147670; x=1790752470; 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=gy5ZJj44yW2Y41JxZ137X9PGWzszng8gimxO0G3/Sm8=; b=UP8ucDgs49jUZkREYruFq1fs9MGKhtu7CWqYYZiC9/z5Lw6C4vC+XA9wvtGf4i8g9g gxI/0tVnQsqFDDv6iS7DIzi0kZM7G7GMft8Fb/y415AJd4j/0p9Yba5ZA9kplM4s9U8C D/9ABK/tryQobTDnYUkRd+/qoOQUj+OJmE7jUfXcSFoJ+dySWVwacjT2kKxOoSRmF33V cYYpNQF1KL/jeT6aaMC5BPdkHURg+LaGeJNgRnoyWIpk7/f29j35KNpkLE8QafLotl8H wr8IGDzwK09xVQHmTmSoJJs62N3YPM9o6or/k7+59g6jvm66zSVk19s2qjj0bIjHieTp 4fOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147670; x=1790752470; 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=gy5ZJj44yW2Y41JxZ137X9PGWzszng8gimxO0G3/Sm8=; b=cAQc6Ua8A2PnJ7doxDerp//xm37Go1/MIj/qqcu/hZvV0h6/2kCPBWMk1beTV10xcL HK9ScPLmq+WuxHVPlIV/uk8ynnbM7AHqMfzJvmH/C8UUdnsVGWpArTccUKqESNkeFzd6 mftCnTbldhO+pDlPj+pL6+Opf8rtb4i01LMCm0stoUPDDUcFPFsls7QAzDC6MIJxYH+Y IzD1wlDUh3iR+mik64fSqISdmeHslUsIzaUk83Jhm2W1LezBwMFfaT+NJbzNE9oZlJ35 RefdHKmIy1zTdf+7jTEaIeWJZ0U+HT1hrp+q4CL7fK4mcr+Gv7vKY3RJH6hPIn/+Oz3D e1BA== X-Forwarded-Encrypted: i=1; AKwUvByetNpiwTm/4dHz676XnxmP+t57I4GNbf9Xo+Ln5nJR6eCCXjpdgEs7jRL4xqzN25q4fqQaQ0JetNObiIM=@vger.kernel.org X-Gm-Message-State: AFuF++mGqlE57veZ2hjV6NOkj61+3Ff+cpZvLpuok289e8MXd8lyOC4K ZJ07Td1K+Wo5ns+IpXI06SYzBTGo6So4XLGABSKXJeogUwg6Ht0iylllMJqC9EmBxuh1Ys/kiFe KB8o7/jHfQg== X-Received: from dyrx23.prod.google.com ([2002:a05:7300:3a97:b0:33b:c09b:a189]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:693c:87d3:20b0:339:7572:7b2b with SMTP id 5a478bee46e88-33ee381562bmr864132eec.24.1790147669961; Wed, 23 Sep 2026 00:14:29 -0700 (PDT) Date: Wed, 23 Sep 2026 00:13:49 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <8a2a96e2f9c8511fa5f162682acac29807243972.1790145937.git.irogers@google.com> Subject: [PATCH v5 09/23] perf trace: Bound the fixed size augmented argument beautifiers From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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 timespec, sockaddr and perf_event_attr beautifiers read a fixed sized type out of the augmented payload without checking that one is there. The payload is only as long as the augmenter made it, which for a sockaddr is the addrlen the tracee passed and so can be as little as two bytes, and which for a perf_event_attr is the attr size the tracee chose. Gate each of them on syscall_arg__augmented_args_valid() with the size it needs before its first read, so that a payload too short for the type leaves the raw pointer printed instead, as happens when there is no augmented data at all. Two reads that are unbounded for reasons other than the header go with them: - perf_event_attr__fprintf() always reads a whole struct perf_event_attr, so an attr from an older version is read past its end. Copy the payload into a zero padded local instead of reading it in place, which also replaces the local copy that was only made to fix up a zero size field. - af_local__scnprintf() prints sun_path as a plain string, but the payload holds only addrlen bytes and the path need not be NUL terminated. Pass the payload length to the address family printers so the path is printed with a bound, and so that the AF_INET and AF_INET6 printers do not read a sockaddr_in or sockaddr_in6 out of a payload too short to hold one. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/trace/beauty/perf_event_open.c | 38 +++++++++------- tools/perf/trace/beauty/sockaddr.c | 55 ++++++++++++++++++----- tools/perf/trace/beauty/timespec.c | 2 +- 3 files changed, 67 insertions(+), 28 deletions(-) diff --git a/tools/perf/trace/beauty/perf_event_open.c b/tools/perf/trace/b= eauty/perf_event_open.c index 6315b46bcdf0..fba8993f3b93 100644 --- a/tools/perf/trace/beauty/perf_event_open.c +++ b/tools/perf/trace/beauty/perf_event_open.c @@ -81,33 +81,39 @@ static size_t perf_event_attr___scnprintf(struct perf_e= vent_attr *attr, char *bf =20 static size_t syscall_arg__scnprintf_augmented_perf_event_attr(struct sysc= all_arg *arg, char *bf, size_t size) { - struct perf_event_attr *attr =3D (void *)arg->augmented.args->value; + const struct augmented_arg *augmented_arg =3D arg->augmented.args; struct perf_event_attr local_attr; + size_t copied =3D (size_t)augmented_arg->size; =20 /* - * augmented_raw_syscalls.bpf.c (shipped with perf) copies - * PERF_ATTR_SIZE_VER0 bytes when the tracee passes size=3D0, - * but leaves the size field as 0. The payload size is - * guaranteed by perf's own BPF program, not externally - * controllable. Copy to a local so we can fix up size - * without writing to the potentially read-only augmented - * args buffer. + * The tracee picks attr.size, so the payload that + * augmented_raw_syscalls.bpf.c copied can be anything from + * PERF_ATTR_SIZE_VER0 up to the size this build of perf knows about, + * while perf_event_attr__fprintf() below always reads a whole struct + * perf_event_attr. Work on a zero padded local copy so that a payload + * from an older attr is not read past its end, which also lets the size + * be fixed up without writing to the augmented args buffer. */ - if (!attr->size) { - memcpy(&local_attr, attr, PERF_ATTR_SIZE_VER0); - memset((void *)&local_attr + PERF_ATTR_SIZE_VER0, 0, - sizeof(local_attr) - PERF_ATTR_SIZE_VER0); + if (copied > sizeof(local_attr)) + copied =3D sizeof(local_attr); + + memcpy(&local_attr, augmented_arg->value, copied); + memset((void *)&local_attr + copied, 0, sizeof(local_attr) - copied); + + /* + * The BPF program copies PERF_ATTR_SIZE_VER0 bytes when the tracee + * passes size=3D0, but leaves the size field as it found it. + */ + if (!local_attr.size) local_attr.size =3D PERF_ATTR_SIZE_VER0; - attr =3D &local_attr; - } =20 - return perf_event_attr___scnprintf(attr, bf, size, + return perf_event_attr___scnprintf(&local_attr, bf, size, trace__show_zeros(arg->trace)); } =20 size_t syscall_arg__scnprintf_perf_event_attr(char *bf, size_t size, struc= t syscall_arg *arg) { - if (arg->augmented.args) + if (syscall_arg__augmented_args_valid(arg, PERF_ATTR_SIZE_VER0)) return syscall_arg__scnprintf_augmented_perf_event_attr(arg, bf, size); =20 return scnprintf(bf, size, "%#lx", arg->val); diff --git a/tools/perf/trace/beauty/sockaddr.c b/tools/perf/trace/beauty/s= ockaddr.c index a17a27ac2a6f..173402400018 100644 --- a/tools/perf/trace/beauty/sockaddr.c +++ b/tools/perf/trace/beauty/sockaddr.c @@ -10,21 +10,36 @@ #include "trace/beauty/generated/sockaddr.c" DEFINE_STRARRAY(socket_families, "PF_"); =20 -static size_t af_inet__scnprintf(struct sockaddr *sa, char *bf, size_t siz= e) +/* + * The augmented payload is only as long as the addrlen the tracee passed,= so + * each of these gets that length and prints nothing rather than reading a + * struct that isn't fully there. + */ +static size_t af_inet__scnprintf(struct sockaddr *sa, size_t sa_size, char= *bf, size_t size) { struct sockaddr_in *sin =3D (struct sockaddr_in *)sa; char tmp[16]; + + if (sa_size < sizeof(*sin)) + return 0; + return scnprintf(bf, size, ", port: %d, addr: %s", ntohs(sin->sin_port), inet_ntop(sin->sin_family, &sin->sin_addr, tmp, sizeof(tmp))); } =20 -static size_t af_inet6__scnprintf(struct sockaddr *sa, char *bf, size_t si= ze) +static size_t af_inet6__scnprintf(struct sockaddr *sa, size_t sa_size, cha= r *bf, size_t size) { struct sockaddr_in6 *sin6 =3D (struct sockaddr_in6 *)sa; - u32 flowinfo =3D ntohl(sin6->sin6_flowinfo); + u32 flowinfo; char tmp[512]; - size_t printed =3D scnprintf(bf, size, ", port: %d, addr: %s", ntohs(sin6= ->sin6_port), - inet_ntop(sin6->sin6_family, &sin6->sin6_addr, tmp, sizeof(tmp))); + size_t printed; + + if (sa_size < sizeof(*sin6)) + return 0; + + flowinfo =3D ntohl(sin6->sin6_flowinfo); + printed =3D scnprintf(bf, size, ", port: %d, addr: %s", ntohs(sin6->sin6_= port), + inet_ntop(sin6->sin6_family, &sin6->sin6_addr, tmp, sizeof(tmp))); if (flowinfo !=3D 0) printed +=3D scnprintf(bf + printed, size - printed, ", flowinfo: %lu", = flowinfo); if (sin6->sin6_scope_id !=3D 0) @@ -33,13 +48,28 @@ static size_t af_inet6__scnprintf(struct sockaddr *sa, = char *bf, size_t size) return printed; } =20 -static size_t af_local__scnprintf(struct sockaddr *sa, char *bf, size_t si= ze) +static size_t af_local__scnprintf(struct sockaddr *sa, size_t sa_size, cha= r *bf, size_t size) { struct sockaddr_un *sun =3D (struct sockaddr_un *)sa; - return scnprintf(bf, size, ", path: %s", sun->sun_path); + size_t path_size; + + if (sa_size <=3D offsetof(struct sockaddr_un, sun_path)) + return 0; + + /* + * Unlike the other families the path is variable length, and it need + * not be NUL terminated, so print at most what the payload holds. An + * abstract socket's name starts with a NUL, which %.*s stops at, as + * printing it as a plain string used to. + */ + path_size =3D sa_size - offsetof(struct sockaddr_un, sun_path); + if (path_size > sizeof(sun->sun_path)) + path_size =3D sizeof(sun->sun_path); + + return scnprintf(bf, size, ", path: %.*s", (int)path_size, sun->sun_path); } =20 -static size_t (*af_scnprintfs[])(struct sockaddr *sa, char *bf, size_t siz= e) =3D { +static size_t (*af_scnprintfs[])(struct sockaddr *sa, size_t sa_size, char= *bf, size_t size) =3D { [AF_LOCAL] =3D af_local__scnprintf, [AF_INET] =3D af_inet__scnprintf, [AF_INET6] =3D af_inet6__scnprintf, @@ -47,7 +77,9 @@ static size_t (*af_scnprintfs[])(struct sockaddr *sa, cha= r *bf, size_t size) =3D { =20 static size_t syscall_arg__scnprintf_augmented_sockaddr(struct syscall_arg= *arg, char *bf, size_t size) { - struct sockaddr *sa =3D (struct sockaddr *)&arg->augmented.args->value; + const struct augmented_arg *augmented_arg =3D arg->augmented.args; + struct sockaddr *sa =3D (struct sockaddr *)&augmented_arg->value; + size_t sa_size =3D (size_t)augmented_arg->size; char family[32]; size_t printed; =20 @@ -55,14 +87,15 @@ static size_t syscall_arg__scnprintf_augmented_sockaddr= (struct syscall_arg *arg, printed =3D scnprintf(bf, size, "{ .family: %s", family); =20 if (sa->sa_family < ARRAY_SIZE(af_scnprintfs) && af_scnprintfs[sa->sa_fam= ily]) - printed +=3D af_scnprintfs[sa->sa_family](sa, bf + printed, size - print= ed); + printed +=3D af_scnprintfs[sa->sa_family](sa, sa_size, bf + printed, siz= e - printed); =20 return printed + scnprintf(bf + printed, size - printed, " }"); } =20 size_t syscall_arg__scnprintf_sockaddr(char *bf, size_t size, struct sysca= ll_arg *arg) { - if (arg->augmented.args) + /* Only the family is read unconditionally, the rest is up to the family = printer. */ + if (syscall_arg__augmented_args_valid(arg, sizeof(sa_family_t))) return syscall_arg__scnprintf_augmented_sockaddr(arg, bf, size); =20 return scnprintf(bf, size, "%#lx", arg->val); diff --git a/tools/perf/trace/beauty/timespec.c b/tools/perf/trace/beauty/t= imespec.c index b14ab72a2738..8da0b28be0af 100644 --- a/tools/perf/trace/beauty/timespec.c +++ b/tools/perf/trace/beauty/timespec.c @@ -14,7 +14,7 @@ static size_t syscall_arg__scnprintf_augmented_timespec(s= truct syscall_arg *arg, =20 size_t syscall_arg__scnprintf_timespec(char *bf, size_t size, struct sysca= ll_arg *arg) { - if (arg->augmented.args) + if (syscall_arg__augmented_args_valid(arg, sizeof(struct timespec))) return syscall_arg__scnprintf_augmented_timespec(arg, bf, size); =20 return scnprintf(bf, size, "%#lx", arg->val); --=20 2.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dl1-f71.google.com (mail-dl1-f71.google.com [74.125.82.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 5561B45C715 for ; Wed, 23 Sep 2026 07:14:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.71 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147676; cv=none; b=E5g2HxIsmCI4s72K1jlyf7odkLsYnTzit7cFTRHZoPRTqY6qoUX4/gPLeMBvs5zzMRAwMZw4kF8mrMNNqUPeZ5EF2ya8bjOBIPcBB6/Z2dUSoh76KFYS1R+NZ4bsPGTWDznBUU6iNq5MHDKOEk3p5Y29KyGcxE5Ca8Hhx8/vXOE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147676; c=relaxed/simple; bh=tkU5/HW0EJVFlf7xMc3oLa1n9EN1rmNyxsbrNxyo5aY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GAyk6mGKlkMDpONjCj0P727hVk5voQgbHnTFVfgC2DZBWtBl55NsFVFUdV+fiyPhTWbH5R42C3wdsDGnUleLSh/pF0eVakAnrAmEV+DCUGyKNgda89TaHO39RlqZeEvlNOZ/Y2A2EWF5+RsTA186OR++0JntEBoS538AdpFI34A= 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=AtSWq2T7; arc=none smtp.client-ip=74.125.82.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="AtSWq2T7" Received: by mail-dl1-f71.google.com with SMTP id a92af1059eb24-14383177746so903978c88.1 for ; Wed, 23 Sep 2026 00:14:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147672; x=1790752472; 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=4s3o3BN7rRyced2L+PC1QB2nCzMDA7Y6t5Oq1+KBNAY=; b=AtSWq2T7TBwqwRA8/zE96gCZD6AHZuAy/avGccFNvtlS5uxo1d1Tz1MI6hWS3khzko 5duzlo4CwWMmdUFzDmEIWPXdDD5v4gDRyyR1p9rYQa316K4lvjtiywd57irCG9FgrLfm jWhhRqGxEni3F+Homq5LJcbN7lbLmLJv6ImxW/zQV5JVSFrGv7AHgnbM26JIsZYufwjB 0F5hHc/ajtr4vOR5RdVBDlnlN9eMESTSBtm1L9FCk82E1w3vzTDAFwrUSM4VnELkFqfG Cs5ECfQOVwiGYPxpoWjI8SLtPEcOyJM/SJ++COq3TISWdOEo0NXOPVfu/3/ZhYhB4EgH mh3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147672; x=1790752472; 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=4s3o3BN7rRyced2L+PC1QB2nCzMDA7Y6t5Oq1+KBNAY=; b=tvcc7GIHh2DH7VUydh3V4fL5WCOlk8vl1uqpWKJTIsdHmXHSb58VVoGrmHn1Omii4I 1clo6OEiAop7hpEC3DPk4XJKLwl8RTjQ1iHUDMNeRkSGZdx/S3QDWBGU60FyscbOsTvK LnEP8YoDhY1oeZ2QO4BkR8y1bACFQufG5Qy4x8+2K2vnCJ32OOJs8xYpnginGst2R+3I 8aQRwbmEKTrpJjRrtXNc1MBScihY7fwbTjZEzPL+ierYsJtqpSfaOjVkkv99o9uDpUfQ k0y2UHaUIOSGL8r5HYyUUvfRZMuRqzd87ZGZFX+dNHgP3ChWxo8vBiso93Y/AZlhcj7p +1MQ== X-Forwarded-Encrypted: i=1; AKwUvBxioCH0O7vAmu7ECu5P5MheUY1rKN/z9cSINTcueLVRqHiS6eCXbtlhJSSUhAqkOGNyAMBo1hWYIlgtZZU=@vger.kernel.org X-Gm-Message-State: AFuF++k8V3egsP8XIPa+51vc8f5GTGkkxg3LLUAPrYpcKAFxSk8usSDE tpboRnrgwPXgTt0d+ZYaWg6iJjFTkCAc2kr5MD/Jr6hMC2Z7dlGpCXnXWCX7vKz489k9M6x4m00 2urFdqQAxvg== X-Received: from dlbrn9.prod.google.com ([2002:a05:7022:1509:b0:144:bcd9:aa06]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:b047:20b0:144:ff47:60d2 with SMTP id a92af1059eb24-144ff476110mr249479c88.13.1790147671975; Wed, 23 Sep 2026 00:14:31 -0700 (PDT) Date: Wed, 23 Sep 2026 00:13:50 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: Subject: [PATCH v5 10/23] perf trace: Do not read sample padding as an augmented argument From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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" syscall__augmented_args() treats everything after the fixed tracepoint payload as augmented arguments, taking any non-zero trailing length as a struct augmented_arg. A record with no augmented arguments at all still has a trailing run, because the raw payload is padded. perf_sample_save_raw_data() sizes the raw data as: size =3D round_up(sum + sizeof(u32), sizeof(u64)); raw->size =3D size - sizeof(u32); frag->pad =3D raw->size - sum; and the kernel writes that padding with __output_skip(), which advances over it rather than zeroing it, so the bytes are whatever the ring buffer last held there. A 64 byte struct syscall_enter_args therefore arrives with raw_size of 68, and the 4 bytes past the end are stale memory that syscall__augmented_args() copies out and hands to a beautifier as the size and int_arg of an augmented argument. A trailing run shorter than a struct augmented_arg cannot be one, so recognise it as the padding it is. The length prefix and the payload it describes are checked separately by syscall_arg__augmented_args_valid(); this stops the padding being offered as augmented data in the first place, so that the syscall appears with no augmented arguments as it should rather than with one whose contents happen to pass validation. Reported-by: Arnaldo Carvalho de Melo Closes: https://lore.kernel.org/linux-perf-users/arJ-gpzqOHk-gF8T@x2/ Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/builtin-trace.c | 47 ++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index eeaaab44016c..aa2d64eb56bd 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -3022,6 +3022,7 @@ static void *syscall__augmented_args(struct trace *tr= ace, struct syscall *sc, * traffic to just what is needed for each syscall. */ int args_size =3D raw_augmented_args_size ?: sc->args_size; + static uintptr_t argbuf[1024]; /* assuming single-threaded */ =20 /* * Augmented arguments are a perf trace specific payload, they are only @@ -3042,24 +3043,40 @@ static void *syscall__augmented_args(struct trace *= trace, struct syscall *sc, return NULL; =20 *augmented_args_size =3D sample->raw_size - args_size; - if (*augmented_args_size > 0) { - static uintptr_t argbuf[1024]; /* assuming single-threaded */ =20 - if ((size_t)(*augmented_args_size) > sizeof(argbuf)) - return NULL; - - /* - * The perf ring-buffer is 8-byte aligned but sample->raw_data - * is not because it's preceded by u32 size. Later, beautifier - * will use the augmented args with stricter alignments like in - * some struct. To make sure it's aligned, let's copy the args - * into a static buffer as it's single-threaded for now. - */ - memcpy(argbuf, sample->raw_data + args_size, *augmented_args_size); + /* + * perf_sample_save_raw_data() rounds the raw payload up to a multiple + * of 8 bytes less the u32 that holds its size, and the kernel skips + * over that padding rather than zeroing it, so those bytes are stale + * ring buffer contents. + * + * A record that carries no augmented arguments at all therefore still + * arrives with up to 7 trailing bytes, e.g. the 64 byte struct + * syscall_enter_args that an unaugmented syscall emits comes back with + * raw_size of 68. Anything shorter than a struct augmented_arg cannot + * be one, so drop it instead of letting a beautifier read a length out + * of uninitialised memory. + */ + if (*augmented_args_size < (int)sizeof(struct augmented_arg)) { + *augmented_args_size =3D 0; + return NULL; + } =20 - return argbuf; + if ((size_t)(*augmented_args_size) > sizeof(argbuf)) { + *augmented_args_size =3D 0; + return NULL; } - return NULL; + + /* + * The perf ring-buffer is 8-byte aligned but sample->raw_data + * is not because it's preceded by u32 size. Later, beautifier + * will use the augmented args with stricter alignments like in + * some struct. To make sure it's aligned, let's copy the args + * into a static buffer as it's single-threaded for now. + */ + memcpy(argbuf, sample->raw_data + args_size, *augmented_args_size); + + return argbuf; } =20 static int trace__sys_enter(struct trace *trace, --=20 2.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dl1-f72.google.com (mail-dl1-f72.google.com [74.125.82.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 2A66544E65A for ; Wed, 23 Sep 2026 07:14:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.72 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147676; cv=none; b=tFBYjIKygsnB5s6vstVid39fPeS/JkeQxlhebYf58ko1HymxPgciTlyaX7O6neFGMpGtGnYKGocTdQEIGJ6Egu2QRGnWKM2oZp2QKzH1IkmnLcmRCIIranQpFEEogjkVglx3bFeZe4UAQXe9unx8k+ir+Femy4A0TOTnQ6ptVHs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147676; c=relaxed/simple; bh=sWDjpmqQFHNxbOrqetG/mdGySdzadyLLEakaJ4zMYI0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ot0Zx3aKVL0zkD/l6C/05HmxmI+z9sHhLw6jCLlGtssqCwrHz8As10/OHlNyw58hkfQq/0SV1T8NVx2Rpv/7eY2v3f6s8d6EQcxSFtjvxwpqmyRWpL/8kdV4OGWZvVX8F8NrIxe/Qe8yutjEudIbKvbT3hcuhhlJFsle1LzNioc= 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=Ym3RI0mn; arc=none smtp.client-ip=74.125.82.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="Ym3RI0mn" Received: by mail-dl1-f72.google.com with SMTP id a92af1059eb24-1437272611eso950465c88.1 for ; Wed, 23 Sep 2026 00:14:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147674; x=1790752474; 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=2RXz5qApTCo7+4xTK6ChTxiYdBPBy8PFXhCMYPC9khM=; b=Ym3RI0mnwn3lCknw4VSr7jIc0QyJ3n2D71+CnqEQomcOckUIVDSQpy0+0j8v/cYH+N 89LOYmqx4IHwFrZkHmgUe5iXQC3kOsj1cLAuw5AgtBsq7d7ZK+3nTP5qJlIuOy591X1I 6yGby/AIkVB9RLaC5fyYRYUbgbAIQpTH2zbvdPvtfZHo3f3LvJzwhwROD2dv1ahpId80 oL4J7T9GZUr6Xg5fXP36mMCdlMt3XwY2k3QBNispL8Cwql9wbnwDmg73CjECsZ+yMnUF xOHIVcKc+WDm6Y5jIeRKh+UoMV3cdfR38iMYV6Cz66zIForo6+NL6LmM8jGr2yIlxYjq phAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147674; x=1790752474; 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=2RXz5qApTCo7+4xTK6ChTxiYdBPBy8PFXhCMYPC9khM=; b=hUNCW71d/slOux6250Bzn3POubIu4Gg65VVGA+vupM/hLbLGWYMp32P+rp6NEKGbd9 BQsk5rSM4ne7ev03NeOaw5laMi4Nic45TbxgcviG5qUfx0bmj5hNoXjLyYD6ybaV/1BT KuaDyUkDWP6JGZJu9t32SRvRoyo4gp9ux9JJUxK33/sYjnn7RrLUBSIC5Tt1SGHEDv2h QXbMsjLFfuSNnuPlgLP1uWlkTDIh/lAyWMmh1xKdEBE6NXYA9svgN7Z7LCrF5wMYf2XN mnqixHbWPMnmY1pFPpDPu2iCteKchuypkhmRamdieWsXl27FCK0qwyqRHH77L8BGR7n3 eeXQ== X-Forwarded-Encrypted: i=1; AKwUvByJv8OEps0CHke8XpP1pQPzhZm/IHXAqAP3HS3MuJG4wH+l/zC0pBnvG7TdmNL0lKtbLl0vURp7Omxn5Fo=@vger.kernel.org X-Gm-Message-State: AFuF++mpXOoSmELvMSsCaTRtyghGOt6pi5/SUMGXHkpZmHY8WYvjBEk1 h2HuVgDoAjM2niJ+FyrLSSu+N2jrMSHUCPGp3z2Vv/r4wHgFgeZHCBUQue0Eak4RyyWYq0nmiW3 vfkY6aAhygg== X-Received: from dlbps14.prod.google.com ([2002:a05:7023:88e:b0:144:db24:c0ba]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:4247:20b0:143:297c:82e1 with SMTP id a92af1059eb24-144f931bbf3mr2024267c88.33.1790147673875; Wed, 23 Sep 2026 00:14:33 -0700 (PDT) Date: Wed, 23 Sep 2026 00:13:51 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <42dc47785bdb78054500591b193a1cb653ef39b7.1790145937.git.irogers@google.com> Subject: [PATCH v5 11/23] perf trace: Do not set unaugmented BPF program on sys_exit map From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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 aa2d64eb56bd..0ae14ecd9f00 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -4176,7 +4176,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) @@ -4199,7 +4204,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) @@ -4454,16 +4459,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.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dl1-f71.google.com (mail-dl1-f71.google.com [74.125.82.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 4AADA466B77 for ; Wed, 23 Sep 2026 07:14:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.71 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147680; cv=none; b=YlgIxNnrWayKYmEo5z1tqOv5eItGVN3QqBP/PpYqd8K9T4NsvBTvyDr4b7GmBkWMfBCXgXh3q/fg3i+l4KotCyjrfg66xWLp6DxMwrerp1DOlNYHyVwE0zEKfy+PQNkwEfh2Zb+CEvvUso6rniyzYXChmcV8lVR/TRLVOqd98Pk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147680; c=relaxed/simple; bh=6NXYgVbBKfqCfcVMIKnNDIxrDBitLslxWUIkXFXYk1I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=H8tlIzi5e391quVELayA/OlGVIasCdip2Nr/S2BkTCmMjmbes468CMfXUC/etXL4WablxpDqdoTq42piVCQPZotO2TOTDX1DqyrhVSP8vWhBHZbWVOkBJjusjnNQaZCBE9UEO/7/pzRUrAEgMBSSde8tPpKIOTojJpszOuAGK0Q= 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=IUMl8+B2; arc=none smtp.client-ip=74.125.82.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="IUMl8+B2" Received: by mail-dl1-f71.google.com with SMTP id a92af1059eb24-14318be7d79so666903c88.0 for ; Wed, 23 Sep 2026 00:14:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147676; x=1790752476; 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=gnYjrH1kOCcZOywb8Ze+N0TwGWd8AGUzJqrfWunukY8=; b=IUMl8+B2QzKgqqmmGWTCWhwOSYQgW6v8xTLX9gTjFET1MwXe1QJ/VwSQB+QF7uvf66 aEgjIH5CtGBgdZ2LTNUCC+Myj99L4WqnO1R2UNiYtIy9NhZ84nlYtcf8sM7u1Gv9xqxk 85tqKyprfAFzjwW1a88HfPesVs9casusKGetfMNDjH9ZBQXVaCxv1GtFvmhVdXHO7y/U nQnAPh83XFBFl/dfwUJNbHEgS07Gpmp79fFDYVZcVZROQKgNClvWJ5vayVEgsNfN54zZ gddsQ7+WDfTUfg21syJ/y3LAkanLrP0XIpVeUz3aXWk9w7dYQyGGIA138kk33qqJ5wyX 7ddw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147676; x=1790752476; 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=gnYjrH1kOCcZOywb8Ze+N0TwGWd8AGUzJqrfWunukY8=; b=mijqEmwGE/gx9/RsM075FNwbCsYpaCAWPda5G0l4Ng/Hsb10LGJtitsZZ8VdvQvXnd feT0rqYy/u4EaBmDGQZAtCRLV06RUcuk+XioixZMBiOlKLD4ZwUlCPNWFYsvCJZcQzLi OqVhRacUeEsTyPFyLVTTfBYtFFhiACqpedVSkhhQKi40CQRCPjrq4MuS+xqfpnsxQ82g xOQalYMfe3ou29V4GNZusND4iQiDjRPz0YYbbOFxF5dGSr7vSpDVN3M/+j8suWJSf68i akRzKEsIYzMIj6oopKQQTKfMWHDRhTTh69QCWxFEcPHpCkcUGYets03KzwCp0fPOyjHv NvGA== X-Forwarded-Encrypted: i=1; AKwUvBxWdg9KByj3sl0ivyATA9L0OxApuFjRW8bbfWRJ8gDnZ9Vt6JbGgIQYHfqQYbCl05F/S665kZfPrPfJlYI=@vger.kernel.org X-Gm-Message-State: AFuF++lnrekTVw09DvQ9kDabRUGj4MpoJlHIPKtGvXepfY/KxSHJIjrA 1/HVBVj3Tqh8qQfMu3JIeRZW1JLLtDY82X46u/2FzRZv5W0yPtWqzpWi//sVm7a3HxXu2vdHkHq tZsxQZMXs7A== X-Received: from dybvk2.prod.google.com ([2002:a05:7301:53c2:b0:339:69b7:3cf7]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:3c03:b0:144:c90f:2583 with SMTP id a92af1059eb24-144f90da14emr1952624c88.6.1790147675949; Wed, 23 Sep 2026 00:14:35 -0700 (PDT) Date: Wed, 23 Sep 2026 00:13:52 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: Subject: [PATCH v5 12/23] perf trace: Filter events in BPF and avoid tracepoint vetoes From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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 0ae14ecd9f00..f7226331c311 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; @@ -2071,6 +2072,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; @@ -4102,7 +4120,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, @@ -4111,15 +4129,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; } @@ -4561,7 +4591,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; } @@ -4602,13 +4652,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); @@ -4616,10 +4674,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 @@ -4846,7 +4931,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) @@ -4941,7 +5039,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 @@ -4952,7 +5050,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", @@ -5132,14 +5230,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) @@ -5854,6 +5970,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, @@ -5909,6 +6026,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, @@ -6031,7 +6150,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)) { @@ -6053,11 +6172,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; @@ -6113,7 +6255,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) { @@ -6121,28 +6265,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 18904bebf672..f16299c1d311 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 /* @@ -454,11 +500,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 /* @@ -604,6 +680,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) { @@ -618,8 +699,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) @@ -627,25 +711,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); /* @@ -655,9 +755,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.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dl1-f71.google.com (mail-dl1-f71.google.com [74.125.82.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 6ECA246985B for ; Wed, 23 Sep 2026 07:14:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.71 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147682; cv=none; b=P6e6m/47S0QyQGQkfiZ0dDqAffQqcTt7vaX/RKSryUR4eUGFeD9nQgcw9vSen5LDI4NvULbBafrLNOQlOEPiEwfwn+U2gfw0qy5dpiD402GsRLqTbifF30ORIK1h/Ujn1C1tI+kKfcPmY4I0hHR5UkFMKsPFT/2342hjflRrMnE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147682; c=relaxed/simple; bh=Y4ajJ+BU0vxv8rdnvP5qVP30cZdr2ZTc4fdaGV6qE2A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EDARTeYcCkTBHL7kdgd0D8h28FSIWH/R3r4+CbXfDrym8foMJDOPoAuYr/yrZsGmvUDYZBmCaFn5wmMoAj/8D5Ax+90/f9ryshXeLsuZqUBRGN53qmwDJckWOxHnwtwnyC8LbzlmypzTKrq4I2iWgNwjUrvmoHC9v40VvNXr9E8= 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=vJvsoFQr; arc=none smtp.client-ip=74.125.82.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="vJvsoFQr" Received: by mail-dl1-f71.google.com with SMTP id a92af1059eb24-142f4306744so1622149c88.1 for ; Wed, 23 Sep 2026 00:14:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147678; x=1790752478; 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=lvSLyvFwNKbwasfZzax8LF75fWyS1KGuwiAWL8hmWVg=; b=vJvsoFQrWlUrA5ztTUV4e8ElNnGxzRqjsvNAKDroz0qGiRHxp4JQj7mX8RzkK6rxrR 3abdhNQJdTW3l6PfBTDLPsJEmrVcV0Yr6jgDPUqLwySUVZjEqPYF2f/5gqCDr05npDhd rRQvS6PyVe5DQ29+euF/r1CmD9esREs08cGN439NOCHYrC3EhGSLysOHLQlT23HJ9PrR rtlAEMSYEB0mpWAlXeGdEJ3PqYtANhH4LSkzDVl27iBShdX4mDZ+8+1hje688XmtDzzF 0As4+4sTvEAY4Xt1eYvO91E7CzVrFrqBN7aBIv5KYbDisL16NAY2cbV6AbyKACXW4mz6 Wwjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147678; x=1790752478; 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=lvSLyvFwNKbwasfZzax8LF75fWyS1KGuwiAWL8hmWVg=; b=OrCDUk/zHsAy+AZCTjjqG2J61rEqSf0Vd/9FjUe0l3gnL99qHlJV+r8SMJ+oCovMjF 9Hq2mvD/YUgYe+l7Mlnp6ezFMhn6pSLIOkl94Slh3awmYMeITxqMJhLFMOHypcIugCAu jdhS4Y4AzsNKKmGnRVbFtR8bAQ6NBvldsaTxkSw2JNaLr0dQFeELLlSteGn22Wc0CSXQ V/HRAi8o5ts/bBD0umlEBTFcK4qq+1UPJtyODORuyUgcErKmPTsCp7HEK+HKQRM6VPvQ 0aYfa7Atjek3z/XbSPEa5M8BHotUuoo0bqHEA9an1MVy211w5vejb5AXNexhM3QEk6kK tYgQ== X-Forwarded-Encrypted: i=1; AKwUvBwQYwUHWZGfdofDwuOgC6UBlZhXzozlCT3RYB6DHIHvUJuIv2hPp1HIiFVzIDcw4yLlI5/RCpoAKEb301s=@vger.kernel.org X-Gm-Message-State: AFuF++n5w3M1lthXKuWLpYWMxPg7T9VD/4QJ6ZoD5a5ficXN+lvsj1Wm 56/ymNW2CZNcAv0JQoBFsNa74cgvxQCCAyIlGXAEAZ3chIJXxLTb0mZPbqISv2mIWj1u4exZv+W kexof05+XUg== X-Received: from dly25-n2.prod.google.com ([2002:a05:701b:2059:20b0:144:cf85:b103]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:4209:10b0:143:54b5:5dae with SMTP id a92af1059eb24-144f92e1e6dmr1899658c88.27.1790147678038; Wed, 23 Sep 2026 00:14:38 -0700 (PDT) Date: Wed, 23 Sep 2026 00:13:53 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <8e1a57cf9d14205293000aa5b0508a9849178dfc.1790145937.git.irogers@google.com> Subject: [PATCH v5 13/23] perf trace: Handle fork and exit directly in BPF filter maps From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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. old_pid can also have been handed to somebody else before the move runs: de_thread() frees it as it releases the leader, which is inside the binfmt loader, while the tracepoint only fires once the whole of exec_binprm() is done. A task created in that gap that is given the pid, and that is itself traced or filtered, is inserted under the same key and has its entry carried across instead. Nothing here can separate the two, an entry saying only that some task with that pid was of interest, and the reuse needs the cyclic pid allocator to come the whole way round inside an exec, with clone3()'s set_tid gated on CAP_CHECKPOINT_RESTORE rather than free for the taking. Both tasks are ones the session was already following, so the cost is which of them is reported, not tracing reaching past what was asked for. 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. Descendants that sched_process_fork() adds to pids_filtered can still have a sys_exit reported without the matching enter. perf trace keeps the raw_syscalls:sys_exit evsel in the evlist, and the pids named by --filter-pids are kept out of it by that evsel's own tracepoint filter, which is set once and never learns of the descendants. sys_exit returning 1 rather than vetoing is what makes this visible, but vetoing is precisely what this series is removing, and a tracepoint program has no other way to suppress an event. It takes --filter-pids naming a process that then forks; the self-exclusion path, where perf trace filters its own pid, only runs without a target and spawns nothing. Routing sys_exit through __augmented_syscalls__ the way sys_enter now is would close it. 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 | 238 +++++++++++++++++- tools/perf/util/bpf_trace_augment.c | 120 +++++---- tools/perf/util/trace_augment.h | 28 +-- 4 files changed, 343 insertions(+), 98 deletions(-) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index f7226331c311..ddbe1c8ec1dd 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -2072,23 +2072,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; @@ -5058,6 +5041,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 @@ -5252,6 +5251,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"); @@ -6168,7 +6177,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 @@ -6191,11 +6200,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 f16299c1d311..d400d3d423bc 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) @@ -501,24 +528,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 /* @@ -744,10 +782,23 @@ int sys_exit(struct syscall_exit_args *args) if (has_pids_to_trace && !pid_to_trace__has(getpid())) return 1; =20 + /* + * Returning 1 leaves the record in place rather than vetoing the + * tracepoint for every other listener. The pids named by + * --filter-pids are also excluded by the raw_syscalls:sys_exit + * evsel's own tracepoint filter, so they are dropped there. + * Descendants added to pids_filtered by sched_process_fork() below + * are not in that static filter, so their sys_exit is still + * delivered and printed without a matching enter. A tracepoint + * program can only suppress an event by vetoing it, which is what + * this series removes; routing sys_exit through + * __augmented_syscalls__ the way sys_enter now is would close this. + */ if (pid_filter__has(&pids_filtered, getpid())) 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 @@ -763,4 +814,171 @@ 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. + * + * Nothing else takes old_pid out of the maps. sched_process_exit() ran + * for the group leader under the leader's own pid, before de_thread() + * swapped the two, so skipping the delete would leave old_pid behind + * as an entry for a task that no longer holds it. + * + * old_pid may already belong to somebody else by the time this runs. A + * non leader thread getting here has been through de_thread(), which + * hands it the leader's pid with exchange_tids() and then + * release_task()s the leader, freeing the pid the thread started with. + * That is inside the binfmt loader, whereas the tracepoint only fires + * once the whole of exec_binprm() is done, so the pid is up for reuse + * for the length of an exec. Should a task created in that window be + * given it, and be one that is traced or filtered itself, it is + * inserted under this same key and the move below carries its entry + * across rather than this task's: it stops being reported and this task + * takes its place. + * + * That is left as it is. An entry records only that some task with that + * pid was of interest, and the tracepoint offers no other handle on the + * task old_pid was taken from, so the two cannot be told apart here. + * Reuse needs the cyclic pid allocator to come the whole way round to + * old_pid inside that window, and a task cannot simply ask for the pid + * it wants: clone3()'s set_tid is gated on CAP_CHECKPOINT_RESTORE. + * Whichever task wins, both are ones the session was already following, + * so what is at stake is which of them gets reported rather than + * whether tracing reaches beyond what was asked for. + */ + 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.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dl1-f69.google.com (mail-dl1-f69.google.com [74.125.82.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 3817244F540 for ; Wed, 23 Sep 2026 07:14:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.69 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147683; cv=none; b=ddeZ0jJP0MxC+N969AtIaBGt7e1h/F7ygFEKeVXQ/Pqp4EVXUc7Fk0yO7MmA5GLFmLRx5BwcpzrR+bYtvLWrW8CeJ8d4JOBpwbNDwKW6vtystgL2Q8hIbBHrcdTctjIBCSg52wwclo7r95ElN3+bVjQtdRz56z9ytyGDDhsbcjM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147683; c=relaxed/simple; bh=UErZqQ9j7me1oIkuO6eMwTA87T5PP3xzy/8bq1xnvww=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qVJsGUjA0xly1YrDFp58vms2gX4ysl4XT+c671vTMP0BaIliRMFvlWDCkhaYsXccK1xwD5+E8lhS6+ylcnpLGvae20q1vo8LbfayTohoV2xKfI1bHjOU+rKooza9Vl9u5NFt3W6ysITY99ruQVhLk3YXmytCcFsM7gctDW3qIrI= 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=PfU0OzhT; arc=none smtp.client-ip=74.125.82.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="PfU0OzhT" Received: by mail-dl1-f69.google.com with SMTP id a92af1059eb24-143580417b3so2224744c88.0 for ; Wed, 23 Sep 2026 00:14:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147680; x=1790752480; 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=VaRcn4A7qfrwm2ef7PgU8V/13ksisQ//6IQWqAFGfM8=; b=PfU0OzhTbzDcd/+rkXXT7FywFyL5DI9RZjmaz3EQtlGaczW4PsLYEdVekVz5ZR6m0B SAmJocBQv7rH91qnRRL5OKQaGEva0Xzw8KFoOoCFASmYU22zIfWy+m/3roRtzZPHmOzG G0CeRX+vVy57awdTnPmCgySWh8artHaZ1t7YoE9jIzZlxoVvR7NvIRQusefxwkAszmXv fnb1vtRXseBVMksP1UvEQ5yfm3JB5sooaJr6iAUcld7mvO94VXrhVkuiR3BX3XPHvV9m fqBoLw18eABfZ/9evnl4inoKDvcwtuqb3K8YcAp/EDPyhwzChOvdS75cw3AllFFjGnXy QALA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147680; x=1790752480; 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=VaRcn4A7qfrwm2ef7PgU8V/13ksisQ//6IQWqAFGfM8=; b=AQ5D0vHAayY9extsY9/ZYAlo/qaIYCcAx0qbBRGUU0qLS5Ntvc+OPd9JQ/ff0tG7+V 2Nl9HiK2XC024rqYF0EIEdT9wJ2KDE6/FllNwHIRkOvgwuhjOeXybsGtrivir36ITaX8 qI+laryLOjK9gZIebTXKTVX6wEGd981ozwROnRb44gJjLUynRCqbJbhgUBnhm4xwIrxW pqZNvyufbvoHNSy1sXeWzSiFwBSdlqsyIEgwhIxYUjqFyg1WKekH6/x9qWsgFRpa6S6M kuLUeN84Jv5RnTPraSQjzy2mMTYiPz+alzJ6qfhJwkfLq/Nm7shYxqiFJmkR9GdY5xtG S2Kw== X-Forwarded-Encrypted: i=1; AKwUvBw0uv3yeW5Hk/Aq2y0kfGB3JyRQcFm6HIhUOq45apKQM8QUinCWOuQkyfVY6rH2IbM4ieLX3UwOGpbUYCQ=@vger.kernel.org X-Gm-Message-State: AFuF++lXBKfA41bFanDC4IVB6zlWAwk6pK6edRgDLZU8M9+9OlEG2qC1 2oEbN7a0T001ub+yVBimjJvhbxS4ZXklX1beZmXjSsarzRHgkdLyETYEYXVM9CcKoYKTdvXRcJz 30Uzeo7ppyw== X-Received: from dlai24.prod.google.com ([2002:a05:701b:2718:b0:144:c124:514e]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:42cb:20b0:143:298f:33ba with SMTP id a92af1059eb24-144f931c06emr2030634c88.46.1790147679699; Wed, 23 Sep 2026 00:14:39 -0700 (PDT) Date: Wed, 23 Sep 2026 00:13:54 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: Subject: [PATCH v5 14/23] perf trace: Enumerate the target again once BPF is attached From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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 | 256 +++++++++++++++++++++++++++++++++++++ 1 file changed, 256 insertions(+) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index ddbe1c8ec1dd..6b52f0d408dd 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 @@ -4691,6 +4696,247 @@ 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. + * + * A thread the target started during the window is left out with them. + * copy_process() makes a CLONE_THREAD child a sibling of its creator + * rather than a child, so it is not in children, and /proc does not + * record which task in a group created which, leaving it + * indistinguishable from a sibling that was never a target. + * sched_process_fork() does pick it up once BPF is attached, as it is + * told the creator rather than having to infer it. + */ + 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; @@ -5057,6 +5303,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.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dy1-f199.google.com (mail-dy1-f199.google.com [74.125.82.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 9591E46AA6F for ; Wed, 23 Sep 2026 07:14:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147686; cv=none; b=dc1rck+/CondkfnoktiOYaJRvADOOLq9afK0Nj2E1PoXSWy4T7+1FXMGOloeJlq/yJwYD6jukSkDFAllm/itslUV37tuFO4RLlmFz+oM0a8jq334pu0Yk/Q2dZzmVPJ7Fbr/UOtYzm7AiFqKCXilw+0S1WN89rvll6grosEzvyI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147686; c=relaxed/simple; bh=+xrSY7I9b4Gac4p3+5CfHpDGtn48eilsfL60mTFY8b0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=V8pXuqyLHkyY6e7p96PEmlP5aKQKlvdKDG/gi0QFd0CmbXkYVFFvvkYYKt2tSxLJdB0kzKRusu+xRIuVHZ8hVd1foCwcNTpLsaiBtOpvpfwucPV4KDr3rzq0egKWVoA2yCHQPWSj1QwSAbM6VPylnqCbbkoshs3nOUAE0/hLGGk= 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=vPzt754l; arc=none smtp.client-ip=74.125.82.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="vPzt754l" Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-32850005b6bso759840eec.0 for ; Wed, 23 Sep 2026 00:14:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147682; x=1790752482; 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=VYyndSuCACAlZ1/aQblNPpqm/b+goCKk68wk6q62PbI=; b=vPzt754lBXCHT4qY+khnXN0pP3Vo9WCnKLfX2W2TmZg8xLix5m4ENAgglqux73A/XM oqOt/+vbvl4wfPlGLoLI9ze48lrDT7nJaLmm1JmGR8moQ2GYh2dckdxd9wncvJseZtcP iF1RskbhWNEPCllIdmB0zufkjC8dGI3oseojHiyg3amj3yTq8/+fVdYqcxgJ1z75QlUR WLezGcA4wSz5i3//tT7Fs0lk9HaqXqvndHvhC0naJfHwJa1CG+DC7MHHkP5vyepiThjl K6ziIlhviQHWeHih0cpdc0d0yZm/CwlmhXmpMeYpN34TpZxRADCKYdj//nnp1iFys1WX vfjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147682; x=1790752482; 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=VYyndSuCACAlZ1/aQblNPpqm/b+goCKk68wk6q62PbI=; b=beWJKgPOpls2mnVsaOqR7srbJrZ0LderqVhz985mxMYNO7gTnn0SjTZ3HSwEa/wWsw fd9rBGuB6tlIRVKvLt/RvG35DLPJ2QpPQOTxkUuY8vbbD5fS6hEg0JmQ5s58VqVYvnHi Og/3OuXDXR3Znvb/nbP48BktYKzeVypL3cM3nHAsnhyBsAC7NlaoblxMYBccI/ssqogY 14Kjio85vSBsj/Sufm5EWcKyJ6zYw1YWrEp6WUXunp9b9E2lv1YXxuR8sg5nsd3x8ovW 91Xj9CobKUZpeYp0gzA2RZVJO96rBUoacZNQXwhS0IRnhPAH8Swh1bVUpB3wIZ/+4XAD yqJQ== X-Forwarded-Encrypted: i=1; AKwUvBztLktdi8ybjxWizoNWm5oz2Yr9Ja8m9E5C+bPRVcP0lRAzyvn114Y4s/oKoCcSphvrZpoaI/JfzerQtBM=@vger.kernel.org X-Gm-Message-State: AFuF++kukZtg5E8nw6SOd46Ci7bOark+hjZMJUnexZPgd2fSO+IDMh0e AoPtR/3LJAzASQdIi9+i3wUgKKimdv3Nkb1E2K5OSefLGJ1lsGbgTTT/qHDxuMqENUFk4PZ8tF9 /s7ozYGaUbA== X-Received: from dybun8.prod.google.com ([2002:a05:7300:f3c8:b0:32d:1a84:9293]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:6197:10b0:33c:13a1:84ea with SMTP id 5a478bee46e88-33e8e3ae194mr1731651eec.39.1790147682058; Wed, 23 Sep 2026 00:14:42 -0700 (PDT) Date: Wed, 23 Sep 2026 00:13:55 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: Subject: [PATCH v5 15/23] perf trace: Drop targets that died before they were filtered From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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 6b52f0d408dd..b318623486e2 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -4897,6 +4897,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. * @@ -4919,19 +4972,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.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dy1-f200.google.com (mail-dy1-f200.google.com [74.125.82.200]) (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 1C3B1466B77 for ; Wed, 23 Sep 2026 07:14:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147694; cv=none; b=RE5JHXLAqIYsztebQyOfBIEDsS++vDxLr4dyEr2/a5+UUfkju53V+qnkq07rtDZmBpm4OtIFh4OtN3T07ttExh8t1OPf7M1qfQY0kNKVIzCeAEo6WPbY7yvgqEZIs4X0i8X9/nd8BTri/l3AKg7r9aDV3znFuk5Gh+0ZfaJ1TWU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147694; c=relaxed/simple; bh=HGRs9eE+a+hJaFRu3Ek+1Q83wFRO/38c+2fbh/2XvQk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FjapC6Xg5RZrSATLhQ7nJxQCYkhLYLww3Tk4ylYFELxVDuHnZsR4IhVMzBJs7UHEtzPU4oO2xYnuCNYK6GwoV6X8DGcv4l8/v2+RAmeTZ+f4tbhjGiUvXuzPXkamAuRdAItM2OFQDBTTRl601cnFNqSNFi7otu+/l0Mvd7CkKnA= 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=QkvDPFS6; arc=none smtp.client-ip=74.125.82.200 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="QkvDPFS6" Received: by mail-dy1-f200.google.com with SMTP id 5a478bee46e88-334f782d1abso2068231eec.0 for ; Wed, 23 Sep 2026 00:14:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147691; x=1790752491; 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=ooYn38WMvkypRVHD0ux8njXnA46P1bfoYCxR163N8YI=; b=QkvDPFS6iTwtSaDPmrIF9O5gkOWvedF89Zir74ZVO5ZDftoBm24htcZz6AJ2JvCmYT mOpe5wPigyvpGcGjnhmPiWg/Fp+D3je1bEw2jC4Zh+cxcoRJ+pwNuoUlw856H/ZHFLHw LZIJ3yM3mKkNoPyaKKu2sbQOykmLlYF8u0710kEoO7mjZMxoCnHzIjWun5+VGCGP2jRz u+iwUSJcssE2D37Cy6PxoqP44LSdXYoH2rqheoui0rxccTR/Qw/mfUgUKIh4U3Evk254 bRxcr88rmue5QforQEV93/MWF6QMPiiwBw/A0pLf+Jn4NwDTnfRzCWQA/bif9wLqmm81 J4QQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147691; x=1790752491; 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=ooYn38WMvkypRVHD0ux8njXnA46P1bfoYCxR163N8YI=; b=08ODyk8Nl5Egr7f9qF+9pzQZZ+wMn3gG/QR8h+59BEOr7Jmf7SFNyDCLxozpnfcWUE fZaZwK62IIk9mzB61Q7YuHZc/vSzc6cDSvsC0SBW5TP+vC3QeigVHVSYJsoHc/qQ5NI4 SGdccm1FG0EYeUVbgqr/XdfVAKaYqh1jxVOu2TutCA3ztHIT3leIopan/R9N7B91mQPJ R10hTABiReyLKDuflTuSsY9+fMurRcivbCOpROn/9lwjeyVhLKYz8wqqgELlqE5h35n0 AwXNTlyU0oz+xXD88jzOfQkBxG4kYFejVFIyp5CsyUWsPoYm5KplBE5ZxA+O2WDg+dZ2 pBKQ== X-Forwarded-Encrypted: i=1; AKwUvByZBJzvQHV6K2C92BkCSm0ZFnbKtvpUUeSLlPbyjnjcI9niAM+8VV8COhuGAFJ3fJZaPAAfej45rfRwSMs=@vger.kernel.org X-Gm-Message-State: AFuF++mILZhZxTijMfWErIZEpwfrv6x6smRXv7qaZRbny7GC+qYTk+Tf yURXJLZrMtyj1M1i3px94PY+Zsbbjb+b63dbGw1QzmyM63kWf9XaQDpnnZIDuTeabk2RhGDmg8f fbNdZnQfaQA== X-Received: from dli6-n1.prod.google.com ([2002:a05:7022:286:10b0:144:e53d:6fa6]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:b903:b0:144:ed03:7c6a with SMTP id a92af1059eb24-144f90c371fmr2036646c88.15.1790147683855; Wed, 23 Sep 2026 00:14:43 -0700 (PDT) Date: Wed, 23 Sep 2026 00:13:56 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: Subject: [PATCH v5 16/23] perf test test_task_analyzer: Isolate in temporary directory and make non-exclusive From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dl1-f69.google.com (mail-dl1-f69.google.com [74.125.82.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 A543446AF1B for ; Wed, 23 Sep 2026 07:14:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.69 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147690; cv=none; b=myrz4/RnsRwR4iIaOf6pmivCTXtVGtccC54SbywJ0IgqiBQ690oe/x5b2MuBjDlygdrUpxlP5iVjd6XBfuEp2J6+inJcdfhbzvAGCx7SFZObQSG4ro77z8Xd4PJOXnfwa9G5+rYytNLaNw0trADosxBB08wwB4kGIclrfxKfO84= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147690; c=relaxed/simple; bh=BwFqoUR08sVPCAz8nNT2kUsBLmhuj0nCuycamDAIf9A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Ebj0n1o8lF7aQyZVRV4bvu/gbtZlwPr284+Ow305u6apv8jNKerOk0eLEwLMiC86LjZbPUP1QzUz3tbQ51qTwO21kFsNbhaT3wSZ3nahQ5orlJNp7oi9zx7iM56YNdPxjhiMMpEUNEz0T03L/zxdtstjYL0YdEUQKCCtnS9+7EE= 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=Q8iOISXN; arc=none smtp.client-ip=74.125.82.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="Q8iOISXN" Received: by mail-dl1-f69.google.com with SMTP id a92af1059eb24-1438fef603cso894008c88.0 for ; Wed, 23 Sep 2026 00:14:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147687; x=1790752487; 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=ppc4dThPLC0xYtcVyncsGQzpEFgp4LgZmFvaWqdw6Ts=; b=Q8iOISXN7mvHWD0Vp+KCipTG03OQUDzES+ghXuok2dJyMm81ixHdiOBbEe2SuDdFZH 8cZwmTS9VIRIbOLzxhBqrKOO7zrs8W0DiVAmPj4oHjz7IlhK2zq/PHX6rEKm4VgCLiAX LQiPJLxlzKMqFqd6YKRi7vkbfqTgeVsUO19qA8N9xIS2yyzZk6Ia7+pqeyHYdEkMXqPD UYjVQAYgvdi5Ek2I4HnlJ9HCj6Yor1+m6lskaOLtUuMgTU1EjZqM1Lkj1DSIDW5Yg+yH f0StlqV+6gYjGu1Nz/oY6hrMehMohlP2mGB1Ffe+L6g2qxf820igU/S8mvObKufq5oK2 P8vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147687; x=1790752487; 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=ppc4dThPLC0xYtcVyncsGQzpEFgp4LgZmFvaWqdw6Ts=; b=pNjI0AjTEDonyFc7fWfPK4vh4Hv5TNGaRcFYKM5Wmz0sO/ihK/52Lk8urJo6GbZuX1 c3amOj8nzCQDX7Ua5PeseFG+UZIKCNSesc/XyQw9YKBIT0psqAFu/8sbYIjCbGY1sqoA NYiUXLIMcE7Be4nCVYGN4e4j8BNku/7+q2Qcc42/08DiUmy6rmx98NWuD5gmqjHpBdZX YlP+At9Aa43sexPfFDKww3wYw57T6NdvvADLDNoOJ4gNuMi3/NZLnO+rZ3Uiaa0/cW4S 7ZSH7MaXgTO6QlYN6OI94eQ7CBK9UgUFNc+oBsWXovL7gblDK7ZGX5JsjcEKBX1a02GQ O2UQ== X-Forwarded-Encrypted: i=1; AKwUvBzrFtSTovOXTrlLtXoE0dH0G90blgtB1eTS+ns821+yHOmTvQdfzGT3gAsHMCZ3et/oxYLrHu2P+7wtygo=@vger.kernel.org X-Gm-Message-State: AFuF++nyTCRqyz8QqtZ224GYKNH6rlAh0/fs/gT9sjP/Qwdi5LZ+sWNI p/I3MSGnnsini/wE1HkEMb6a+oOrqqfPzVohstUyJyswYX7s2ESSshYTFsTSAFg5XdAPRRW83o+ CjDvj1eI+ng== X-Received: from dleb20-n2.prod.google.com ([2002:a05:701b:4254:20b0:144:d76d:635b]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:b042:20b0:144:fedf:5789 with SMTP id a92af1059eb24-144fedf57ddmr311692c88.15.1790147686498; Wed, 23 Sep 2026 00:14:46 -0700 (PDT) Date: Wed, 23 Sep 2026 00:13:57 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <2b33d9a499901d59d15cad2ed84f5ed181cb7e2a.1790145937.git.irogers@google.com> Subject: [PATCH v5 17/23] perf test common: Only disable probes in clear_all_probes From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dy1-f200.google.com (mail-dy1-f200.google.com [74.125.82.200]) (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 A285D46AF21 for ; Wed, 23 Sep 2026 07:14:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147691; cv=none; b=ZPJHf1LvJJ18ntI9Bf/e5tkH91W6eaEqmMmcnoYdPSPGdRRcFBJGevHSjYZ38JPe2d3+ZtucU7QDUi+k1zlZI5mTzxFZwDgKFSAVf8SzcTjzVmtATvgrw6bxdQqYNLrvsd0KdkIPorMXU197NGfxCjnCLtj0ykvV0ztrq5i20nM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147691; c=relaxed/simple; bh=QsG4MJ+9d8Gw2rlu7vt+kuuYK5BwpCbNXWox8MIEeP0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rvNHs4ynkCdoKE5QGRFmoxq0jE3k35HiNNAkg3wdrnubCFNMMjuvbKzRlros8p1/AULg5JqzER5Y1WGljkFrO6KfSsQPriZkGgOBeti41zkUp8F8+OFHGSQM9HUi43i6XQQDu5hCAmgfLbmo9YZ8yFd/Onzc5ovjaGhb7QEjIrg= 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=flGhhIPN; arc=none smtp.client-ip=74.125.82.200 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="flGhhIPN" Received: by mail-dy1-f200.google.com with SMTP id 5a478bee46e88-3284bbaa89dso2293964eec.0 for ; Wed, 23 Sep 2026 00:14:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147689; x=1790752489; 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=MWrqEnEjaqzlFKJHYlIpmLW2qV50R+anCoJc7h1aKtE=; b=flGhhIPNensCyjHYKT6RSyXR0SfvutCF0AsEjMObVbJ640YmBcDo6CJi2D2k4nyxhz KZXtT7+SzWXf+HMXE98+tsjLj/eerAsEC6mqKeY4Vv8iTkC3xuNFoW4TAMGl75raCo7Z QQQptoyiVBJUGpwrmgnRdcmqBD3BUjcZYOvHncuHAR++K0xl1nbxW9VjLCWj/pAktTkk VXlwKMT/s1CtpI5ThAlQ/nMNTo4oaZjCNoKRUT995VqVDOcJ+x8HPL0Wm7Dt7SLRRR/3 epH0Ht0xYGpFh+0QFXDP3JxwTQ5kSb9HId0a2hv6JfFTEuuwY7Wd1ROfQl4wR2pEEnI4 9YRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147689; x=1790752489; 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=MWrqEnEjaqzlFKJHYlIpmLW2qV50R+anCoJc7h1aKtE=; b=dGy89TGm1IRCpFyDk6u1m+nKJFncJJeTzHWc2RtBpqOPWma1AYb70TJ9eVnH8DZCLJ sx8daV9ZnEP+ABnj8tDesXyVYZDmZ/04jPySX8GEw4UtQUQvEp28/9Q+9UqUxt6RkRtw 2WXxmgKyCERz1Hwj3Tiy26Tq8mWCStCwK8iJ/ggTrMmqYHXxhBZxX5OzDtW2n378sL0+ Zd2JOm72EAL645Zun30azpKMFsiQO7kvmJj2cx/BHEto1Mlh4K0JNIyauBfMh9/xXpxm oxHS02ityvquEsQUvaD0cKQade7sYsBA9kVxZ1b6bVo7CGk0K7+KS7msS/OiaB0jvMQd F5Hw== X-Forwarded-Encrypted: i=1; AKwUvBxSo9yhrXYYW0x8YTKcvfGbpXBtuRJuy2oQhoQfTSuJ2SnWVHnXMCD1LKrFrM7A//pHbWz1AXFjy2CXqFw=@vger.kernel.org X-Gm-Message-State: AFuF++kOSIiVs/C8bWMxkbjwuWMah1VjorGUzY2l78nVAuCKoLKGzCLA zXV/Hj3oS1H7YmRVfRAXX1CFbBBcQn5+P/eixk8aw+RG41fXbsMcUAyZWKHMV0MPgqOqHTzIivB efWs2RmzWlQ== X-Received: from dybcr14.prod.google.com ([2002:a05:7300:ac8e:b0:314:7337:26d9]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:693c:23ca:b0:33b:944f:7e8e with SMTP id 5a478bee46e88-33e8dacd12amr1748482eec.21.1790147688466; Wed, 23 Sep 2026 00:14:48 -0700 (PDT) Date: Wed, 23 Sep 2026 00:13:58 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: Subject: [PATCH v5 18/23] perf test probe_vfs_getname: Scope probe name to PID and make non-exclusive From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dl1-f71.google.com (mail-dl1-f71.google.com [74.125.82.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 CDCB046AA6F for ; Wed, 23 Sep 2026 07:14:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.71 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147694; cv=none; b=lROBAbzv0FEqt5qtTV3j3DdAenkIm/mbVLpwXLyOD4JwoYs77QWHJRjN7YaLqXPedspWZUC3SK1A0qNN7f9Jc35Lp0fXbQKKaYPOW/LI+Aw5cmU67vhOpodghpdh+lpIIaYZh5mrbUHutkCQYoAml+V8xaxe2GjP+XNkwVyyOAw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147694; c=relaxed/simple; bh=0ZhD8bssDZ7dlaIyvyn5ilk6eduNKlg/RQWYWTYY4Q4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rqhx8/qoP2htu4rHuTuPp6WlY9K4xtVI75Y678wePcyT1UXAuIP9a/hAE4X488gFSyQyQV0kSor9ccjuKHbBLSgAEEWog2uDeGjEEaYd92kH5jicCZpL5p/3MAJKBxv7fnWuSIS54wCnP+8VngawVmf4AYNOJF0/OfbO+cc9BK4= 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=flHIBsEm; arc=none smtp.client-ip=74.125.82.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="flHIBsEm" Received: by mail-dl1-f71.google.com with SMTP id a92af1059eb24-1438719cc1eso882882c88.0 for ; Wed, 23 Sep 2026 00:14:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147691; x=1790752491; 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=ci+qALH0Ym3zO0P0k8BKqSlfohnst/RmWwz2GuRC2j0=; b=flHIBsEm2R2lVeP8B6ZXU4X8zTtBwa0Rz08ets/1iBN11Ly2S5rXaDxaF5yCsQ+Dbi vHBCpShG8j20YZvompzMw3J/RFNs3EAbKqVL1K3wUWKCvjYoP5VhFJGDVkrbrDN5tzgz Jvu7yruBuH78uFgqn7bYcAAXy2jLeZiui1C9bBRvkdrnakoKGSsCu1zeU7fOaV/7nxG+ yLYQ3ixgyqEsv7iTmGvsR16sIeW5NFsu9LssgRf63UD5dbXaOQ1tM0vSbW+1ctF4sp8E ti4r1UeCS8vZKlFtsi+lvZXipTTDgdPGvHC7MNbXpoz1EFQdoyEDb1i8HRz3v/V+jk1D BymA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147691; x=1790752491; 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=ci+qALH0Ym3zO0P0k8BKqSlfohnst/RmWwz2GuRC2j0=; b=zgqvDkiHzkPe4l2xpuQ9a2TuZSe2ukcXijA0Xvzud5qMgmr+RXrO7seePdIRovnzQi 8KZTAQA2liQ92M2kCYlgrkAT5G7dwZmokIoyUEWj94h49+BPV7/5AC/ThY5MyIsFoTqD tZHH0GRNTGoVRte3afw0IXXTVrYthShBzPm6XfdehQxYxp5tlyy7DHBkuD4lz7dAuuu/ YTzrrzgrhqP0LN7LRauV/GucpkRpGoMHZDaeyz61VEfFo4ehHg/jYtUXMkswea6YXJZp BiWUzXGLOQjpITjGB3INAzOKhMVsdq33vZ5pcqeFx82eWTgt6ZDZrI25ROMczdPNiLVq aG4w== X-Forwarded-Encrypted: i=1; AKwUvByu2mwZsogdrtBEJcaLhD+hzQW1f/6mKtKFJ7/rf+Ga/MCxyVoZ25VRSOK1tgeRgERk5wdzltfQaJ7LaM8=@vger.kernel.org X-Gm-Message-State: AFuF++kNFkzHxR/tS+ym1hpONCYmWjpG7M6Hy6gjnaPB8t7SKcXOKqvk Hq2ujcbnFMCsWsKoyJYYJXo4rD0YuNqbqMIZOETDI7g2tPXtApiIJVyXjJwwq7QdfwhXfzhgKFW zPhPeJKSrqw== X-Received: from dlag1.prod.google.com ([2002:a05:701b:2501:b0:144:ce1e:6f56]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:4546:10b0:143:298b:ba79 with SMTP id a92af1059eb24-144f91ed3eemr1829369c88.40.1790147690400; Wed, 23 Sep 2026 00:14:50 -0700 (PDT) Date: Wed, 23 Sep 2026 00:13:59 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <0ef35340076ebce0f85b453c4e30bcdf6a660818.1790145937.git.irogers@google.com> Subject: [PATCH v5 19/23] 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, howardchu95@gmail.com, namhyung@kernel.org 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.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dl1-f72.google.com (mail-dl1-f72.google.com [74.125.82.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 AC893470121 for ; Wed, 23 Sep 2026 07:14:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.72 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147695; cv=none; b=OoA2Oz3MOniRWg+LnyFMxy+fyRT9syUayIfa6QQBfxufib7uuM4PksJwULnZBPc8bFkKpXKkihcHJ6VLEoH91rhvvQtd6lgBtgiaDG5YocpB88JUPQz5rAIfjm1sbreX/mgnOe24uoz+6t9Umr5q5GccJmbbskp6ngtr6PLk9X4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147695; c=relaxed/simple; bh=KGH4cgsh9c82rZCy3EbUZ1Nz6yfT4Ky/9/p1lniOs14=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pQZwKXpL/LfpC60UQQR72+F8oHR6c3RjpyyqpWm001Xg4CjQ6wAf1/gnEf6dyiPYxLN+lR2sSGe38IeeLt4+bAC+Tu2Zno4ULI3YxDr8aTI+o46zUd+QTSQU3mAZSCS8BqpdnHg/h7D0UTcCms9caYX0GltX6/NEqrrqROD4XDw= 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=q7nP1fY+; arc=none smtp.client-ip=74.125.82.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="q7nP1fY+" Received: by mail-dl1-f72.google.com with SMTP id a92af1059eb24-143580417b3so2225192c88.0 for ; Wed, 23 Sep 2026 00:14:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147693; x=1790752493; 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=KBEeTTvqxnVeietl/aALNcp5Orw3ILkwnjTixQhwTwA=; b=q7nP1fY+EcRkEITFhknXCmHLg8IET0ESHRLQmYv11JBSeVNCF69IbONIlcIonQNCof xgOUjv5L2m2cPPIexgVTywDRYgDYeT1/DECON6a5+Z6cVvEJPgfKylhwsW5R6IOANTze giZ3x9ZywAuWAAJvkGZYKmTBpU21A7BbE5vs4zOYxZsitJL4iGr4agYsYeqQkHQNmgAp 7v1gnUNp8RnF5fs+/c3RWdPwjd2izih+SbZ4Dme/uSYJN7S47xVvzoWhIkR1v6zcCD50 uO3m/moB1ztzOuOzPs8YMZRYWe0W+CMr5mHxONPYDPxvDTWyJijR1km4YSOECOXonxYn 3+RQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147693; x=1790752493; 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=KBEeTTvqxnVeietl/aALNcp5Orw3ILkwnjTixQhwTwA=; b=kGrr2bcIR0cx88lrwcy8TueXS0JEiNgHziGOjZuaiiCFsLMOrPM/LrNG9JKGYos6CS 641GOuEjGhoGBP93aPIQTkbJE4xfSr8lSe+n1/6qO3r4WZfIZ9eUDZDh6G41DeCNZeJu VL0YeugMgliZBUO0qS1We/NNFx9cozA9RWA5eQHYozw3cU+FuVDyypbFaDo9dzdE7AYQ NBHNeBG02h5VS/H3UmJerrjSsD5vPpl1s3C6JuPcbNBy7O7+FtDeFXwlwtfb3HA9E0RD 9x7eXunz4OX6P+TfNL+Rrz3w7O82ft3FiXPTBMqQmX8mN6etK+s2V9+Dh9Ik7iIp9HHu NsqQ== X-Forwarded-Encrypted: i=1; AKwUvBxTnM0n4htTlyGnGl6Ppw3nHg4pnyHMzrEHVmS4xlhFWd0wISqmEnDa7lespuZjI5szkXZlwNKre2mKHFE=@vger.kernel.org X-Gm-Message-State: AFuF++lPv06Odj5f8MvhwEHbMz0t01ubkma8VYvkMGrqENe1ttIH7REg P5yHBXoKfH/uS9UNDB5G7z9AG87OBOLrZOpvrIxfUB2oy15IPj3KUpHfdtDjUhKhCTXLSK0x2/C ua24IE8ROoA== X-Received: from dleg6-n2.prod.google.com ([2002:a05:701b:4306:20b0:143:749c:77f6]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:230c:b0:143:342a:3d3a with SMTP id a92af1059eb24-144f9062a9fmr2012442c88.0.1790147692357; Wed, 23 Sep 2026 00:14:52 -0700 (PDT) Date: Wed, 23 Sep 2026 00:14:00 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: Subject: [PATCH v5 20/23] perf test trace_summary: Improve error diagnostics From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.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 3A03B470433 for ; Wed, 23 Sep 2026 07:14:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.197 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147696; cv=none; b=jWpKaXdYQ5bdMBSu0DNVLFCUr1wMfzk7m7kWJIJDYcWm6XMdV63KJidDPcXc1Zy7kaRKNeg2IOcYKBSIza5mrG5b+w/HGhlp933TqsFoT6119DA9kfdZmiEmEIElqIQ64wUOgF+D9x1lAdAC7VFkbydacR/YgZlooRPSUHf7pPI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147696; c=relaxed/simple; bh=/vtMrX6kKbRn4D0NVcAOubZG8b0SLqkHopqx6PWtzsU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Nfxew7mdd92UMytmtZyJ63yUFublgbIAWNb2G1OOK7xjTWBhDF4AMGUOju0nhgjGWRwxfRq38xDOvsrF5qDSLB+3IF/2Q0aOGQL3pn+U6lwbGDuwqFM8EdQlhYA9F+r4K5px+J/545hV/nm2GPmeBQjyijr+ErLFV6PCwZhSj4Q= 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=QwfotuYC; arc=none smtp.client-ip=74.125.82.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="QwfotuYC" Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-3282d5302ffso654151eec.1 for ; Wed, 23 Sep 2026 00:14:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147694; x=1790752494; 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=Ckd6M0/kQaXv4EzKStJvjOPHWYUMstEuXJOS0IsTFYs=; b=QwfotuYCiCvb6Wp82FQQE2y4Vle13dZaf3kVy66L9RzDyJKLNFTxfWtvDyCq0QO22c hWMChlmrBsDd6bu9fhXMpidIue6g7TwKIGrdaGVnagP7oaR6E7fTsrJn+tsaUPlj3KyS DQePXkCukUWaxcXwpHEH1ZsrBkffOnUF+Oaa9bIdH8s4vP96nh5e4K/IiF9v1dCTYsvC RCtF6ehRE5shaMVRJW8Kb6+dg5ydfOX4TAQQpV4WlPXNqbzPRVDMFvsBotBqIGUm9Hlq WfF9Aiz0klOuPcG1z88JNtx8JI43PdvZq9HmPDe8p554DopSJYV17B4v8+2HSMixcvIZ gXOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147694; x=1790752494; 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=Ckd6M0/kQaXv4EzKStJvjOPHWYUMstEuXJOS0IsTFYs=; b=u/gNMdwvq4ADL97MBiSJG4KecOMqPTtzvYO2Yz0VVyM+4c3QtNkgbjGTGf6DwbGKEz Zxf/t1ALhY34kzc7iHOn2tbuaJlGVbh6kgr+jTdgW4OUinM0vY9W5ZbwWzKnBKGjT5bV uX31XhUHb1HJCcn+aSANA+/l+qSFlISJ7v3HcJbOedFzGgZZTVPpay8jKBD8dIzTKDTv VWd69sGr3CbLm23Url0uddBhws+U87hG5i37zbImfd38NFODkqSvCQvj68/Tw6uAk2hE 7PwWArT+pp0dXRN8rMdAv/3kJmRNqHgCikHPNezt7tNI2GFS3gzICPFG53U83H+uDAIa cZxw== X-Forwarded-Encrypted: i=1; AKwUvBz+nXEsX4dNjVZ8NrdGoO4TsOTaAIl8E50EuiPPwm25NSgjjyMajHzNwzotZegm2m9wGy6k+gtv1yViJT4=@vger.kernel.org X-Gm-Message-State: AFuF++lp27tiRe6ysQPKo6hRUPj0PLdu7jifA2cCXkh648wyY7RqGDGf aVxfz8g2Ou4vzhQLh11WSxl/8R0Cj7/R93HfOwplX6fw/xKJ44t+60simrhc6XemVelngCm3/Ak /LG41vDwipw== X-Received: from dymj19.prod.google.com ([2002:a05:7300:6b13:b0:33e:51c3:47b8]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:693c:810c:b0:33c:1449:bfda with SMTP id 5a478bee46e88-33e7a9b03a5mr2125812eec.7.1790147694066; Wed, 23 Sep 2026 00:14:54 -0700 (PDT) Date: Wed, 23 Sep 2026 00:14:01 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <58ce3260eeba03521a8104db96dea1756a4c00e8.1790145937.git.irogers@google.com> Subject: [PATCH v5 21/23] perf test trace_btf_general: Drop --max-events=1 and make non-exclusive From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dy1-f199.google.com (mail-dy1-f199.google.com [74.125.82.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 D677E47142D for ; Wed, 23 Sep 2026 07:14:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147698; cv=none; b=D84h6e7SK9cNnquxdfIB//EC2ccQYKvWkip64xCPntJi3YUIu3jXc4rV/TKQlrbsN89IFLf3lmfogogqf3va7dbtsvLGradCnmbLcn85ZcYd9EVliU4wv/NUM7TtT7A3WiNrEPkwxJJSvXjJhtaKAGmSs2QsO2am7hj81ZDag2c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147698; c=relaxed/simple; bh=HXPRvYjfzayd9k3WeILA2fGzkmHxlIg55xw4JorqTvE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Iz64KarQ1sAbDwBNoOOTQU9TRhDz95Lst6s1s2sikMVZkWyBginwMSKLifih7mfXunQyhn8wJog1kagrsUHEygF9lm/dh7kxkga8f46H8J3xWGOuWFFFmF1bCq3oSiIOi7LCxNxugVfui+FbKBPQ1JGR85DEi3sMNc5iMkOW5U0= 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=oqdw4uWi; arc=none smtp.client-ip=74.125.82.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="oqdw4uWi" Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-32861f5448cso991895eec.0 for ; Wed, 23 Sep 2026 00:14:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147696; x=1790752496; 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=NE2Z5tiC9+s9sK2gJilhCuDRbiG/ftNGrhIItwaECA4=; b=oqdw4uWiN/8ce85VGL6vlldti9UxiC1zF/f0x7GVwQmqkOnbxnomY87R8qC38LMm9G qKAmGKDtNfLRlNBLh3Gr+GuVLRJ0+/myt/0cTyaIeCnT0QfmGQ9stqZgaXW84Z7dR9ol gA0WXv6q4+cuHttg5xbLkbR5jdX7qrtFT7SJQ+rqMiq/yzBQuFdIMN3X4a+Zx643lcSL oEC4X3ewJEG9ckgXrHm6gB7PTfYSFs5/xeOi1Mi70sKhmW0DBe8A+o9jlA2fo5BfxVg6 jGhUZkWlxuar1bQtug+YdKLCX4kDNZ020Y1350wfSMRaulq4WFk8L5fw5lSUoX6uxeqf wOUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147696; x=1790752496; 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=NE2Z5tiC9+s9sK2gJilhCuDRbiG/ftNGrhIItwaECA4=; b=aRZ9ZM0uhlxJsbB8FSGdxgFFK+5ZWRTK7FzAA7KozjBqCd0lqeuqDET/Mzaeud38eJ dP7CsAGO/RbZeDrqe3seRyca1mk42S52lVFEEssBrXhQL0m+D1ewmfH+rNn7pQVMDe+X hCj9APhze8bqEDlT/W6VFroVSe+R5CKjkl72nR0jR83cJwAQ5ldAhrIZPQ0mD2f5l6dT P+Z++8L+xs0iEi5NZyIf0+GTM8V6nBbaJKyIspw2lWLcnXMuib7hHQnFmBN/Y/lr3/x/ ZalTg2Gs6zTqdudls/oLPrrQRmt52Tnd5xdisqvM4+OIO24MzeDBKs8hXXzxDKkv+zya IItA== X-Forwarded-Encrypted: i=1; AKwUvBwMD2X49zAhm/ekyShN1nt9wPPtpFJbvVYqk1NJK6vTWLyN6X0Oh3EGq9vVLAR8wrzwO1L0UX1U9k1TBlk=@vger.kernel.org X-Gm-Message-State: AFuF++lFEQNAaW5r5YbOpKIMjvC5Bd+ZY4LIfrj1+jOTVf8gNIGS4CA+ NzKx+BKZoZ2M14QtkNkW6knx8KfpQb8y2dvACnQFUjBaYrmkqW/HTTXoIxIhSVrqWdlDngzZxAu lBp+PoYuwxw== X-Received: from dybut1.prod.google.com ([2002:a05:7301:e0c1:b0:33c:3034:dd11]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:de4a:b0:33e:6a58:b03 with SMTP id 5a478bee46e88-33e8d6cff72mr1692094eec.21.1790147695744; Wed, 23 Sep 2026 00:14:55 -0700 (PDT) Date: Wed, 23 Sep 2026 00:14:02 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <68b5cb9502935fd5bfa3acd5332bb78fe8793388.1790145937.git.irogers@google.com> Subject: [PATCH v5 22/23] perf test trace_summary: Make non-exclusive From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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.56.0.rc1.315.gc6ed9934b7-goog From nobody Thu Sep 24 13:36:57 2026 Received: from mail-dy1-f200.google.com (mail-dy1-f200.google.com [74.125.82.200]) (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 2AA4F472522 for ; Wed, 23 Sep 2026 07:14:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147700; cv=none; b=AEv/p1w5fGUSHl15uJXRf9Qk80uapMSaUqjEi+0QOHUDLetfEUM9niGVProioH/jQKF7/rEQJHzh5ux14BtLtdmFQHmHoUn3xCglH+hxdpRC2estcAbDhuL4H7QEzO8OqLfSszsIiuZUkhM31GfFUmcaSgrhhdcaDgzUfmppayU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790147700; c=relaxed/simple; bh=UCqIOtZD2Jk+dE2LdYvz/qydnzUWZKhTuMhrInWOg2A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jY1IdzG3ia5mzKROTniuUwMjTOlweCatFoWGxoNQ10GiDV0BhT3hoB7PJgCuPsdB+upD9V9dXdrx9HiFK0FAwYLurciPUhiEN285havo2KUI7iQow7DuyFn4z7tdgPDqvUJbnCxvKq3T0LJKsLZK8qrlzKiCPbPXALWttgRLUBo= 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=n/XfQGo5; arc=none smtp.client-ip=74.125.82.200 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="n/XfQGo5" Received: by mail-dy1-f200.google.com with SMTP id 5a478bee46e88-32b8c2b30c4so897947eec.1 for ; Wed, 23 Sep 2026 00:14:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790147698; x=1790752498; 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=/S2yM1Svvahmk7vRSW6vJVb9Ri5nPE1b8txEEKj5AbM=; b=n/XfQGo5zd48p7KYb5lH6haQ1Qjg/kJP36NWOrMv7GDa0blITIHhpM9FfPWKoimo9P rsv2g6V8vefS4B3TkGySagt+dQdFLWH5ZK4MWcRRVa2/r84K+0VJQN2TQUdbt6s5u6Pf qEpx9CAiEhwlc/iybJoRSt+br/vMKtXwf3WZ9NB8Wcfh/n2keIuXBJ841tapZQlf1/lG i62Qct57ABYRM/QZh6QziLApMTCxbfO5QGL5AxD0AaXgWIoxhsGu5E8rrBdxIA7puE/m i84XBRL6aWFZMXt4iqqE4+dz5IHv3X2dPRvb4KVv/nUn/AygsO5Yet+2cLglFljjBUls ZpqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790147698; x=1790752498; 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=/S2yM1Svvahmk7vRSW6vJVb9Ri5nPE1b8txEEKj5AbM=; b=GtreLBzYN4T1SNe+BF0r61O8BXrIhLSV6MdKGXI+NwOPO0gsf8HxbPMwHzhDoqJ9qZ lYqONx2EewwJv4GUyDPhLZNCt/SvShQHeZUS2x15F2jSvEe2sBvHo+yL2EHLvzlKjCbn i+zy7SgK4ORy3IyrFbhC4QEOs7bwe2V8DFmErfJv0zA6I7JtZySDug9ZC9R8fUG08uPk XKpByi9rll2j25J+JJ6yI/yeViFPZzl9rqT4aL2jqJNz9L9wfo1YcPSdYmfUCGUT1i+y r5rSWMbeF0cL3d+zDtV5VUA1Ww1pxSSr6iDH/qtbrtrvHlCvWBBbyPpx7yKbTuHz9Cj7 4pkw== X-Forwarded-Encrypted: i=1; AKwUvBybV5ppzdcfWqoe8Mh+SQljLOUy4dNnEFswSxA0hbD7HxzIq8V0gSvTOH4siwm3LY4CQB587eIZ/evJenw=@vger.kernel.org X-Gm-Message-State: AFuF++k6KQE8Ha3pEneMPHx+OIJOvEFjJFVZ4TdOjtdsxgba8z1ejpx2 XN5DiEQUMWE1AGfXdruggTbOGC2jgvax5zrH2icGxyXYhZAR4P+lmL7ZeMsNQIWYjo600Ir9/xp SRUmi+DNrFw== X-Received: from dybut4.prod.google.com ([2002:a05:7301:e0c4:b0:33f:53bb:bb62]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:693c:60d2:b0:33c:1f21:2337 with SMTP id 5a478bee46e88-33e8d9cd47emr2040530eec.25.1790147697744; Wed, 23 Sep 2026 00:14:57 -0700 (PDT) Date: Wed, 23 Sep 2026 00:14:03 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <0cb69731888e5d32afa9b517d3456d66a99dedb5.1790145937.git.irogers@google.com> Subject: [PATCH v5 23/23] perf test uprobe_from_different_cu: Scope probe name to PID From: Ian Rogers To: irogers@google.com, acme@kernel.org, howardchu95@gmail.com, namhyung@kernel.org 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.56.0.rc1.315.gc6ed9934b7-goog