From nobody Fri Sep 25 00:40:11 2026 Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 597933A963E for ; Fri, 18 Sep 2026 06:32:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.71 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789713177; cv=none; b=HHocanC+OAamX8r6ShD+LxfFvWXDEK4mstxoPi0Ib+GAwoTFHEHq4xPsXpLGJK7cEDwOxIQrRfL+xNZHjiT9NNeXa9sVSjQar45WrQCfLFYqp0R+iNu7aDY0lT7XDimwTl+uM6cVBqdrKi85DB6jBfKsl7p10ZC7zHIw4/YOOMQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789713177; c=relaxed/simple; bh=dIPvBV5m1XYpsGMWImgGjBXKGPvtWXXZWXBA2YSCgUM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IxSeLfPQ2MZgnVSrMLJ3k+j9pkeA0hszIAJomamV2+mjgZMDvEmGZI+spuW6Xz3mPp3YF2TJoqJPtsk+1QJhPiK3634qZEDLGKUhNMT+Yz+kYlHr0WE5sxOsQ5t6TWnM6wcBif+Ajl3FaUFQvJ/ywhWMCu7OLY0S/VGZ/lyXE1w= 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=n2xIhL/r; arc=none smtp.client-ip=209.85.216.71 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="n2xIhL/r" Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-39e3c10ac70so1205397a91.2 for ; Thu, 17 Sep 2026 23:32:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789713175; x=1790317975; 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=BpesHv/rYkWgPvEqpfqRdL4BWaPoNf2A+UdcBx5dbmY=; b=n2xIhL/rox3UX9O3pakSCFUorqRAkvsPefgZ5eXSG1sWMUwjjRfCBX79GU5dxM3AWO 27yfWyXZG8aj/yM1/eKApsbmbUQNBKHmvJzzG0g+61jefPYYr/+45mQKnPe24EVNUjyF 4zdrQ0lQg9iDoAww2drYeZRDXv2OHJxtXQK4laA7OxUBshvDaA4j45Bftd29k3XpkmAJ nW9xnJJN1VmneR73M0QU0u2lNlHNKN1pyrH0vX75PXI3WF19EyElAsaNQnsd97LUCMXb y7fsHg4cyQdKzhPr52trVx+rLVaTOGtqr05bTqs9XyM4a/ODlakrzCyXTlFpDJi2Nb2+ Xb0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789713175; x=1790317975; 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=BpesHv/rYkWgPvEqpfqRdL4BWaPoNf2A+UdcBx5dbmY=; b=Ywwr3BY5op7lKrr2+HlvZdrn+N0YqoELrCRb8wcyxJRp/+ZUgrEBv+RKdPJmaiZug6 fvkv2DGup0bHb51hv/c42y7iLs3U7mRlTGxhFQCXAiPSVLh/39N4TNu/+RoU6ftFMPt0 gbU5GaUwlpzeu9GM6yGEVXmIj9VoBDCa03zTPouCltfXj973mBUY9we0S3PuL4t+P8M4 K5Y8EvHBrcopGkwWzJSufrnEljkQHHaxUSyJalMHuDJs7Au6usB/TIXlRcOCxS7u0QLw 8nQ54WwPuqjvkS2ylztDPe/vBca3zkn0TjCTj/nZGoDI+stZ57ST5wcsQt0A2n2mXtVB KM/A== X-Forwarded-Encrypted: i=1; AKwUvBwxbF0okc2TViY6mCSf9fdkjVHs/wt9nGRuzxfDguoxwLqfvflaDuMCaH6SYWruM3+SxurZdS0y4/LRqIM=@vger.kernel.org X-Gm-Message-State: AFuF++kSEvRFuS3qwDd4pGBmy7p0L8FRodiDGvi9h/xYF3vNCW8EHYit /0bT0KAmoH1coqB6rXz/chCafXEbddJJHntdIc5yt1cWodrpyt6NyesoGjuX5d8ybfTSWUusJbO 5d0kJXGf+Dg== X-Received: from dlan4-n2.prod.google.com ([2002:a05:7022:eb44:20b0:144:d065:6639]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:da88:b0:39e:4c81:6c97 with SMTP id 98e67ed59e1d1-39e54f91d31mr3537519a91.29.1789713174549; Thu, 17 Sep 2026 23:32:54 -0700 (PDT) Date: Thu, 17 Sep 2026 23:32:45 -0700 In-Reply-To: <20260918063249.2172589-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918063249.2172589-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918063249.2172589-2-irogers@google.com> Subject: [PATCH v1 1/5] perf trace-event: Report tracepoint format errors with NULL and errno From: Ian Rogers To: Arnaldo Carvalho de Melo , Namhyung Kim Cc: Peter Zijlstra , Ingo Molnar , Jiri Olsa , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" trace_event__tp_format() encoded failure with ERR_PTR() while trace_event__tp_format_id() returned a plain NULL when tep_find_event() found nothing, and tp_format() itself returned NULL when tep_parse_format() failed, as its return value was discarded. Callers test with IS_ERR(), which NULL does not satisfy, so those two failures were taken for success. In syscall__read_info() that leads straight to: if (IS_ERR(sc->tp_format)) { ... return err; } if (syscall__alloc_arg_fmts(sc, sc->tp_format->format.nr_fields - 1)) which dereferences NULL when a format file fails to parse. Mixing encoded error pointers with pointers that are compared against NULL is what allows that to happen, so drop ERR_PTR() here and report failures the way the rest of these paths already expect, by returning NULL with errno set. evsel__tp_format() no longer has to translate the error back into errno before printing it with %m, and the remaining callers become NULL tests. syscall__scnprintf_args() gains back its fallback of printing raw arguments: it asked for IS_ERR(sc->tp_format), but syscall__read_info() had already replaced the error pointer with NULL, so the branch could never be taken. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/builtin-kmem.c | 3 +- tools/perf/builtin-sched.c | 5 ++-- tools/perf/builtin-trace.c | 10 +++---- tools/perf/util/evsel.c | 5 +--- tools/perf/util/trace-event.c | 52 ++++++++++++++++++++++++++--------- 5 files changed, 47 insertions(+), 28 deletions(-) diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c index e1b2f5bc1ba8..8693c6b135ca 100644 --- a/tools/perf/builtin-kmem.c +++ b/tools/perf/builtin-kmem.c @@ -1870,8 +1870,7 @@ static bool slab_legacy_tp_is_exposed(void) * means the tool is running on an old kernel, we need to * rollback to support these legacy tracepoints. */ - return IS_ERR(trace_event__tp_format("kmem", "kmalloc_node")) ? - false : true; + return trace_event__tp_format("kmem", "kmalloc_node"); } =20 static int __cmd_record(int argc, const char **argv) diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index dd39a4fb6c7a..5b4092ae33ee 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -5182,8 +5182,7 @@ static bool schedstat_events_exposed(void) * Select "sched:sched_stat_wait" event to check * whether schedstat tracepoints are exposed. */ - return IS_ERR(trace_event__tp_format("sched", "sched_stat_wait")) ? - false : true; + return trace_event__tp_format("sched", "sched_stat_wait"); } =20 static int __cmd_record(int argc, const char **argv) @@ -5240,7 +5239,7 @@ static int __cmd_record(int argc, const char **argv) =20 rec_argv[i++] =3D strdup("-e"); waking_event =3D trace_event__tp_format("sched", "sched_waking"); - if (!IS_ERR(waking_event)) + if (waking_event) rec_argv[i++] =3D strdup("sched:sched_waking"); else rec_argv[i++] =3D strdup("sched:sched_wakeup"); diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index 20fffc24507b..5bd62b61287e 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -2385,7 +2385,7 @@ static int syscall__read_info(struct syscall *sc, str= uct trace *trace) snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name); sc->tp_format =3D trace_event__tp_format("syscalls", tp_name); =20 - if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) { + if (!sc->tp_format && sc->fmt && sc->fmt->alias) { snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias); sc->tp_format =3D trace_event__tp_format("syscalls", tp_name); } @@ -2394,11 +2394,9 @@ static int syscall__read_info(struct syscall *sc, st= ruct trace *trace) * Fails to read trace point format via sysfs node, so the trace point * doesn't exist. Set the 'nonexistent' flag as true. */ - if (IS_ERR(sc->tp_format)) { + if (!sc->tp_format) { sc->nonexistent =3D true; - err =3D PTR_ERR(sc->tp_format); - sc->tp_format =3D NULL; - return err; + return -errno; } =20 /* @@ -2681,7 +2679,7 @@ static size_t syscall__scnprintf_args(struct syscall = *sc, char *bf, size_t size, printed +=3D syscall_arg_fmt__scnprintf_val(&sc->arg_fmt[arg.idx], bf + printed, size - printed, &arg, val); } - } else if (IS_ERR(sc->tp_format)) { + } else if (!sc->tp_format) { /* * If we managed to read the tracepoint /format file, then we * may end up not having any args, like with gettid(), so only diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index c663aafa88b2..2570ea8d5d7b 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -720,10 +720,7 @@ struct tep_event *evsel__tp_format(struct evsel *evsel) else tp_format =3D trace_event__tp_format(evsel->tp_sys, evsel->tp_name); =20 - if (IS_ERR(tp_format)) { - int err =3D -PTR_ERR(tp_format); - - errno =3D err; + if (!tp_format) { pr_err("Error getting tracepoint format '%s': %m\n", evsel__name(evsel)); return NULL; diff --git a/tools/perf/util/trace-event.c b/tools/perf/util/trace-event.c index 000c1e1d68c1..10a7652f0305 100644 --- a/tools/perf/util/trace-event.c +++ b/tools/perf/util/trace-event.c @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -77,7 +76,7 @@ void trace_event__cleanup(struct trace_event *t) } =20 /* - * Returns pointer with encoded error via interface. + * Returns NULL and sets errno on failure. */ static struct tep_event* tp_format(const char *sys, const char *name) @@ -90,38 +89,65 @@ tp_format(const char *sys, const char *name) char *data; int err; =20 - if (!tp_dir) - return ERR_PTR(-errno); + if (!tp_dir) { + errno =3D ENOMEM; + return NULL; + } =20 scnprintf(path, PATH_MAX, "%s/%s/format", tp_dir, name); put_events_file(tp_dir); =20 err =3D filename__read_str(path, &data, &size); - if (err) - return ERR_PTR(err); + if (err) { + errno =3D -err; + return NULL; + } =20 - tep_parse_format(pevent, &event, data, size, sys); + err =3D tep_parse_format(pevent, &event, data, size, sys); =20 free(data); + + /* + * A parse failure leaves no event behind, report it rather than + * letting a NULL be mistaken for a successfully parsed format. + */ + if (err !=3D TEP_ERRNO__SUCCESS || !event) { + errno =3D EINVAL; + return NULL; + } + return event; } =20 /* - * Returns pointer with encoded error via interface. + * Returns NULL and sets errno on failure. */ struct tep_event* trace_event__tp_format(const char *sys, const char *name) { - if (!tevent_initialized && trace_event__init2()) - return ERR_PTR(-ENOMEM); + if (!tevent_initialized && trace_event__init2()) { + errno =3D ENOMEM; + return NULL; + } =20 return tp_format(sys, name); } =20 +/* + * Returns NULL and sets errno on failure. + */ struct tep_event *trace_event__tp_format_id(int id) { - if (!tevent_initialized && trace_event__init2()) - return ERR_PTR(-ENOMEM); + struct tep_event *event; =20 - return tep_find_event(tevent.pevent, id); + if (!tevent_initialized && trace_event__init2()) { + errno =3D ENOMEM; + return NULL; + } + + event =3D tep_find_event(tevent.pevent, id); + if (!event) + errno =3D ENOENT; + + return event; } --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Fri Sep 25 00:40:11 2026 Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.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 57AFE48D890 for ; Fri, 18 Sep 2026 06:32:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789713178; cv=none; b=h28PH9qSnpqFNllGrWBCCWQ16XO82XsyR9Gv0RscRWB2vAo4+rppLFFVX9+2ch0NbpchUOhlo0/UcTBCaCo55DoaeaFeycbzMyZjCMBTbpttWhBMg1G4SRWoTIG2eJouzXoLYGYVbR0yMQaWwJegfHUtZTPEf7ZqEz5ty2V1AKA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789713178; c=relaxed/simple; bh=QmcWkstS/VbcAPK9L4G/fLdvRUQbvBjPfNeBDPtpFz4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZmqcwIls6/LDoG2FrouwRO6UqmtKOMaL8piKxa5eHMAlq/6ZCwrC/S5RhT+behpOd9rw/M2H/PrwojGoYmDgOJ/RBHbbUKX1aNkake3ohivsy9fGK2Vv9gIuWBng79KS7y2jesIDsXG/aTR24A4v1MhSidcp6oEJV2wbJcK3ibc= 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=Spz/pbu8; arc=none smtp.client-ip=209.85.214.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="Spz/pbu8" Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-2d6fb956002so6728465ad.1 for ; Thu, 17 Sep 2026 23:32:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789713176; x=1790317976; 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=C7OaLonc0zyBcjmW5hXHqgzNGrY6TsdzALumXRwiAzU=; b=Spz/pbu8GT59okuMvd1xGBox/t1YxDxshpS4v8DzqgxILc8dPqYX1vB/FbvlxkaOdG 3LimXWY2pQzZHolRZHpxuemojuq3ztY74napXw/NYWxL/ekKI4vgmRzN0ut4uAvq6MId 7JkspH3LqlUKpTEhY4O2t3qIvEvc3W/ZpbI9dgm3wh/HAVDorT/8ZtxWvMy4YVTsQsPB LKfOXNXG00zi345NnKelmEvnb9YBJRX6L1i4yl6DXnQ/3VT+q/RLkZiUVRC3X+fYnbUE YducRHxvjtXTB71KrK8RkNcOAfkRN4tAEsN17kfK+gtgVu2k59lBy/3GGowmIzxobVmh cQNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789713176; x=1790317976; 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=C7OaLonc0zyBcjmW5hXHqgzNGrY6TsdzALumXRwiAzU=; b=2PMupJtdXDb/+ZMSoXM7/jvYarK2y2ZuvJYIeWxlt1MnvqDDzLMUCv3GemChC12x0p 3/9u3x4Z5x8KMmcWHcEa+e3ZiMXIuQHG/tOpzhR9rKs1YKZ+MvoBnpWW6cQF/rIvvc9y ypcKxEri8XQOTFuS5XCLGAqHKA4XoUQ3G+HCWjIAWd3OB1DqwAomxt0JY+dbRJf1I/0C 5HSv+N13xJlgz5R5sqxtKwVtlbMefeMzQy4cXYXnhq2MpwH5pmLjfTSMp3XNgXFldf+3 BrjZbhc0R2u4b94akr+fsuJiHhfvn/NZ9xnz4Qh4O/ewtBU8b/suxV9/7gKmKpxD8PFX H5Jw== X-Forwarded-Encrypted: i=1; AKwUvBynHd/t+5E7D+Gpg3wuKfnlGGvBW/qXnUFE+2wcCsrA8vOEn/oSdbax5DJrPimeGRgkH2K/oUhAvVWaCT0=@vger.kernel.org X-Gm-Message-State: AFuF++maQrecLPmRbZOuqn+N3yA8wOLNCmwF+9G7pqZNKYLW4QKh9zvG 50t+9z1/8W4Ke9qRkajdDqhp2E6lCjNP04mKgXXI3OKtxPG0GwmE5fT7UvegY8UOBLUPX2JwFza I3eqS0OFUzQ== X-Received: from dldyr17-n2.prod.google.com ([2002:a05:701b:4511:20b0:144:c590:e885]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3d8e:b0:39e:2065:5f60 with SMTP id 98e67ed59e1d1-39e54f9d37bmr3632270a91.18.1789713176440; Thu, 17 Sep 2026 23:32:56 -0700 (PDT) Date: Thu, 17 Sep 2026 23:32:46 -0700 In-Reply-To: <20260918063249.2172589-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918063249.2172589-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918063249.2172589-3-irogers@google.com> Subject: [PATCH v1 2/5] perf trace-event: Reuse an already parsed tracepoint format From: Ian Rogers To: Arnaldo Carvalho de Melo , Namhyung Kim Cc: Peter Zijlstra , Ingo Molnar , Jiri Olsa , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" tp_format() read and parsed the format file on every call. Each parse registers another tep_event with the global tep handle, and libtraceevent has no way to free an individual event, only the whole handle, so a repeated lookup of the same tracepoint both redoes the work of reading and parsing the file and leaves a duplicate behind for the rest of the session. Ask the handle for the event first with tep_find_event_by_name() and only fall back to reading the format file when it has not been parsed yet. The handle holds formats for the running kernel alone, so a name that is found is the same format that would have been read. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/util/trace-event.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/trace-event.c b/tools/perf/util/trace-event.c index 10a7652f0305..826f75464171 100644 --- a/tools/perf/util/trace-event.c +++ b/tools/perf/util/trace-event.c @@ -81,14 +81,25 @@ void trace_event__cleanup(struct trace_event *t) static struct tep_event* tp_format(const char *sys, const char *name) { - char *tp_dir =3D get_events_file(sys); struct tep_handle *pevent =3D tevent.pevent; - struct tep_event *event =3D NULL; + struct tep_event *event; + char *tp_dir; char path[PATH_MAX]; size_t size; char *data; int err; =20 + /* + * Each parse adds an event to the tep handle that can only be freed + * by freeing the whole handle, so re-reading a format file both + * repeats the work and grows the handle with a duplicate. Reuse the + * event if it was already parsed. + */ + event =3D tep_find_event_by_name(pevent, sys, name); + if (event) + return event; + + tp_dir =3D get_events_file(sys); if (!tp_dir) { errno =3D ENOMEM; return NULL; @@ -103,6 +114,7 @@ tp_format(const char *sys, const char *name) return NULL; } =20 + event =3D NULL; err =3D tep_parse_format(pevent, &event, data, size, sys); =20 free(data); --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Fri Sep 25 00:40:11 2026 Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 29A1A3C5DC5 for ; Fri, 18 Sep 2026 06:32:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.71 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789713180; cv=none; b=nzEhdLgZnIOftZpedTwyCC1SXzwxfV9Z7JmmEdl8chccXQPEm+0uMKJL6QeaKREOhEDlQMpYr4gMKjW5eRed43m0GcAfukxbaIrtZYVV0hNRd1qSiPUi702z/w296gx/C8aaswIDS+e3dfeDVkymG1jGpAod2CzzNZHYtZe3xto= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789713180; c=relaxed/simple; bh=6piizERCZzYOWq5jXV2XG9t3u22hRs7JWPhh6sg00Fc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aFphQUjziNLRtSPJ4puS+bUnrMyuYyIzZVDLa3ZOSLPKrXbOpyMXE3tGszlT0qWo1GNlTEinQACamg97dx6g2V/lhajvJVlC47ShstJihkekxMDpgO+JLadtzG/eAurQWw4UHkO2M7PKwNtqwfX/IMo858cx1lZIG8hv9eBYQ+w= 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=EYsr0Jp9; arc=none smtp.client-ip=209.85.216.71 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="EYsr0Jp9" Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-39e087a17dfso875049a91.3 for ; Thu, 17 Sep 2026 23:32:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789713178; x=1790317978; 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=4wzxGE12kx6/6XKkvEhnF8dFgypzNeSmABr+sADCRnA=; b=EYsr0Jp9nt3XelH3o9nEh94Lruh9eZUsV3v3A6hY7EcWuwgGMcZdVZWN0WU/kiCPkr qoBBwpE2P3hXRukbmTIqFxrUCAayE6cdSQ+VEebmUSCdOLTaMbLDYRB6JO8u5x/XkRRb VbOqCGZv+RGiEC5CyjiPO9Jh0dADLZBluN8cufFsZqGYVigXqDQ2mDGJdyo+XzIuErZT uWQdyUWDKnF9XL3LRAyNHeTSxvq7M4EOYpX21e6pohPxF6nxPwJh7rDtY/D4V+CNTZLq d2SOqoBnC4ojs6LxNEoTf/QBz4Xud5rpeFTWH/saC7SGxbgA93HfXCQJd3gX+rco2a2j /Uow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789713178; x=1790317978; 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=4wzxGE12kx6/6XKkvEhnF8dFgypzNeSmABr+sADCRnA=; b=1MyJBxqqm7zE9m1+aCSkvKXx0Cq9aswp5kLz4R2KLaS5IM5EmXdbI+ZJfq95hetaH8 40NuWO6Re7NhKjNUkWb4xHM551TdX72SbIV0G2examQIWuEERuYGWG2vZBnrIAt6iqI8 6isutcSfq9KDyKeXmh2Pxfebu1phbuP/hgM0+4nzmqERvLy3Wj94goA4shH7apb7gxB/ +W9EIduEchVjDTHFGYoLUq85v8Zp2K8kcvPVbDEcftLQH9+lsnoX21obvPxv6e6e3fnm 4/EH5eTtz6XrPdkPLoPit0h63fHOMiJFyJbk4tTwdXcPg6T7oxOhvroC1gLbBt/LAzFz ifww== X-Forwarded-Encrypted: i=1; AKwUvBzi/6F6wWGGpmFpPLqnV5m55KpFhAXfL21OnEBdpkLx2quRCBWgQyGbQhJMvb1HcsbA7jOnu5tL77ajBJI=@vger.kernel.org X-Gm-Message-State: AFuF++nQAkYhKwZekKltMK8Rvjvf2NG5fOMnD08taFsoi6rFHimLfoei UQIaeE07I5V80bpmpxlDulm8JPAas4qF48jLxNYkgrznKfcXd8NDfhBj8iq7ZQRXQZnxxiDLDsA P0ngVxH+rew== X-Received: from dleb12-n2.prod.google.com ([2002:a05:701b:424c:20b0:13c:fe05:89a3]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:224c:b0:39b:61f1:8032 with SMTP id 98e67ed59e1d1-39e54e7c18emr3356695a91.16.1789713178050; Thu, 17 Sep 2026 23:32:58 -0700 (PDT) Date: Thu, 17 Sep 2026 23:32:47 -0700 In-Reply-To: <20260918063249.2172589-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918063249.2172589-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918063249.2172589-4-irogers@google.com> Subject: [PATCH v1 3/5] perf trace-event: Free the global trace_event when a command ends From: Ian Rogers To: Arnaldo Carvalho de Melo , Namhyung Kim Cc: Peter Zijlstra , Ingo Molnar , Jiri Olsa , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The tep handle behind trace_event__tp_format() is a process wide cache of the running kernel's tracepoint formats and nothing ever released it, as the comment above it had noted since the code was added. Add trace_event__exit() and call it from run_builtin() next to the existing perf_config__exit(). By then the builtin has returned, so the tep_event pointers it handed out, such as the ones cached in evsel->tp_format, are no longer reachable. Note that this does not silence every libtraceevent report under leak sanitizer. Parsing a print fmt drops a token inside the library itself, and as those allocations are not linked into the handle, freeing the handle cannot reclaim them. Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/perf.c | 6 ++++++ tools/perf/util/trace-event.c | 22 +++++++++++++++++----- tools/perf/util/trace-event.h | 1 + 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/tools/perf/perf.c b/tools/perf/perf.c index 6c5baa285b13..d60bc929c257 100644 --- a/tools/perf/perf.c +++ b/tools/perf/perf.c @@ -32,6 +32,9 @@ #include "util/build-id.h" #include "util/config.h" #include "util/debug.h" +#ifdef HAVE_LIBTRACEEVENT +#include "util/trace-event.h" +#endif =20 const char perf_usage_string[] =3D "perf [--version] [--help] [OPTIONS] COMMAND [ARGS]"; @@ -379,6 +382,9 @@ static int run_builtin(const struct cmd_struct *p, int = argc, const char **argv) =20 status =3D p->fn(argc, argv); perf_config__exit(); +#ifdef HAVE_LIBTRACEEVENT + trace_event__exit(); +#endif exit_browser(status); =20 if (status) diff --git a/tools/perf/util/trace-event.c b/tools/perf/util/trace-event.c index 826f75464171..819542a73f55 100644 --- a/tools/perf/util/trace-event.c +++ b/tools/perf/util/trace-event.c @@ -14,11 +14,9 @@ #include "machine.h" =20 /* - * global trace_event object used by trace_event__tp_format - * - * TODO There's no cleanup call for this. Add some sort of - * __exit function support and call trace_event__cleanup - * there. + * Global trace_event object used by trace_event__tp_format. It caches the + * tracepoint formats of the running kernel for the lifetime of the command + * and is released by trace_event__exit. */ static struct trace_event tevent; static bool tevent_initialized; @@ -75,6 +73,20 @@ void trace_event__cleanup(struct trace_event *t) t->plugin_list =3D NULL; } =20 +/* + * Release the global trace_event. Called once the command is done, when t= he + * tep_event pointers handed out by trace_event__tp_format are no longer in + * use. + */ +void trace_event__exit(void) +{ + if (!tevent_initialized) + return; + + trace_event__cleanup(&tevent); + tevent_initialized =3D false; +} + /* * Returns NULL and sets errno on failure. */ diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h index 720121c74f1d..1c342fce36bb 100644 --- a/tools/perf/util/trace-event.h +++ b/tools/perf/util/trace-event.h @@ -32,6 +32,7 @@ bool have_tracepoints(struct list_head *evlist); =20 int trace_event__init(struct trace_event *t); void trace_event__cleanup(struct trace_event *t); +void trace_event__exit(void); int trace_event__register_resolver(struct machine *machine, tep_func_resolver_t *func); struct tep_event* --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Fri Sep 25 00:40:11 2026 Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC45E48F02F for ; Fri, 18 Sep 2026 06:33:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.70 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789713182; cv=none; b=s24hi1o+NYGkYuR5+gzYiJ7GAnNNIx8RBcMmbryh0MdLoATugPoTAfFvzVjwFzrxF0hn3c3Nd5K08kcg/KhYtofE2JArnEWKudfaIPvnPvMFpT2G2CyXY82ZVBP5VK7o6r9ioavLKCjyYaRF/JuetbPOhB8zi567oANpNBjrylw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789713182; c=relaxed/simple; bh=TopP8I0l5bgoJ7wE/9/c2Q2h1c69lwUM5/uLGyU1urw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tIS6rV17cQy7/j78lzSa/n65Y40/Rku+nBpW7VY0dSTjx7Up//QD/qcKos9GhXEPGgezcPTaj8jWuyLwWeAdCu/rm2Tk4C7zzQIuENEwhW5k3ud68iYdWVahzTq0ngdhpXW19NM86xmLbkub4mNdXCife7dcMb9WfTQnG95/Ifs= 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=nbOGpy6u; arc=none smtp.client-ip=209.85.216.70 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="nbOGpy6u" Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-39deb05ef51so715672a91.3 for ; Thu, 17 Sep 2026 23:33:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789713180; x=1790317980; 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=YrrBnDof9dzk7iz3soaxRSgz7WHkwxWpYdufXgugnk4=; b=nbOGpy6uW8myzgPmuO/23ZW/xZBhgOpybuuxZY9xeJ7GmGYL5/MZkRuFSGkB4+8IO7 5v8/5wjuD/jRdWSzbn4INmeIW3rz8rKZdmcDu4+mpdKPNtzitoYObLuVk1SpjYzH6T3D UyBAHht2Mb+YjqUHsf/xLzrAnL2mPSp6slui06JbQtceYaXZWLlQNHaCm/As73dNdX3P ylQwTbytlfyPjMoZTw9o1VVnYKhYaY32cvuhW1GXk8Uzz1gbjxoMDr8v2LXxIwrbkQs6 iuIjK78YmuNCGI5YzZXO8qrabm25PWooFR6t+bkkpUq8Fe0BSWSaFOx8Nxp9uBU4ojxu wZwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789713180; x=1790317980; 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=YrrBnDof9dzk7iz3soaxRSgz7WHkwxWpYdufXgugnk4=; b=w5k9PSbcjgvFSklP+NapxOcmaEi10VIGIyKMCJnQURXNPS04jdyrqg09cvFKfspA2R +mmpjW4uBBCYZZgIigwdU/KHYFG8wRdm6enyXQd7tEmVJ7xsdaxfxBq2lJ3oGN/Z4tWa 6ey610hil6yvilQxMMgjfW490azwLunVbyK6IZOACN5ryQks+w7V4uVBoPOZ03+SnsEH ImQUKnAdSJGZCkx2pSJ3udtAyuc9VtbjcO55YFBFXwq92VKWpgB8MBAzkLj8U5E56PH9 60KunY3QkDp+D5e3VOrUmge7YyiPBWbPHB+c2t1Ac3Oa6miaQgnGd9IkSQDBaQ7dyQjW rAtA== X-Forwarded-Encrypted: i=1; AKwUvByLNxiGP27rruUYn8DE67WGdw3z5oaJgPolr5kpES9kfxeSO/cBILHiB7gucu+6OQlekUu1snmi+Q1oKv4=@vger.kernel.org X-Gm-Message-State: AFuF++kqzmM9MplETn36PVSYwD5Xlh362teB9/qGUziJCkUcxZv7bpuw DI0C76QON9C7HJMEHU6QQQVwbUuWQdrZ41GGIotTJDA1JJlqmNj6718SZMi8+i2LQAf6rOxEDtt kd/sDFQx4vg== X-Received: from dlbpv7.prod.google.com ([2002:a05:7023:907:b0:144:bbd4:62e0]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:224c:b0:39d:f317:44cd with SMTP id 98e67ed59e1d1-39e54eab3dbmr5464906a91.14.1789713180035; Thu, 17 Sep 2026 23:33:00 -0700 (PDT) Date: Thu, 17 Sep 2026 23:32:48 -0700 In-Reply-To: <20260918063249.2172589-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918063249.2172589-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918063249.2172589-5-irogers@google.com> Subject: [PATCH v1 4/5] perf trace: Free the host machine allocation From: Ian Rogers To: Arnaldo Carvalho de Melo , Namhyung Kim Cc: Peter Zijlstra , Ingo Molnar , Jiri Olsa , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" trace__symbols_init() creates the host machine with machine__new_host(), which allocates a struct machine, but trace__symbols__exit() released it with machine__exit(). That tears down the contents of the machine and leaves the allocation itself behind, so use machine__delete(), which does both. The replay path points trace->host at the machine embedded in the session and never reaches here, so nothing else is affected. The leak was hidden from leak sanitizer because trace__symbols_init() passes the machine to trace_event__register_resolver(), which stores it as the private pointer of the global tep handle. The handle kept the machine reachable for as long as the process lived, so it was only once the handle started being freed that this was reported: Direct leak of 1256 byte(s) in 1 object(s) allocated from: #1 __machine__new_host util/machine.c:135 #2 machine__new_host util/machine.c:155 #3 trace__symbols_init builtin-trace.c:2105 #4 trace__run builtin-trace.c:4759 #5 cmd_trace builtin-trace.c:6091 Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/builtin-trace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index 5bd62b61287e..e1a4abf44673 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -2132,7 +2132,7 @@ static int trace__symbols_init(struct trace *trace, i= nt argc, const char **argv, =20 static void trace__symbols__exit(struct trace *trace) { - machine__exit(trace->host); + machine__delete(trace->host); trace->host =3D NULL; =20 perf_env__exit(&trace->host_env); --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Fri Sep 25 00:40:11 2026 Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B91A1491595 for ; Fri, 18 Sep 2026 06:33:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.71 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789713184; cv=none; b=SBs4aVdrr/tyDfR6bku8vt1bP2OumePZWby9Hc3iB/kpGq9flV4bapPRoHKlVaqxXKH27RmRs8UZbj9+jS4+JAMcHjJbrN7xVOup7k+UMdMBwe56ZGz9dzWRFgBS+q60IHNZvBRtw/c3bKzW470bTuP27nBNak/wG5TGerwxAcQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789713184; c=relaxed/simple; bh=MwAv9TGcBfBkIFh2Lg53bIiDPZFHBPI3huLnLmnG88E=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CleKAsELyn+ghuELQYGuBPHG0qdYWfHe1Ml0180OfGOtC5vlZKVITpnPmMqj0iOVzXq/6nGErvZUjeOxfPI9aOxaIqip/EWB3WPPxxZxSSI6gn5HRxgVYiDEphnhUcA6bQy0hJ63lTzE4hRdDQF/vqIlKeAn0FJ/07VrWYxXq+A= 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=iAtkAufC; arc=none smtp.client-ip=209.85.216.71 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iAtkAufC" Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-39deda201bcso918750a91.2 for ; Thu, 17 Sep 2026 23:33:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789713182; x=1790317982; 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=TbX4sDK3DBzXsbYTT8liO4dVTVuIt9yGIC0SXD7xm8w=; b=iAtkAufCj01/P2sSI6kLnfrYqP9IFjLpGJwPM3lWPTzXvrtepBN59yScnDiP+vif9R Tej7HJO43AtpQxprZWpleq9QY8/AbwE5ITbThFXmlZW/UODyA77j9Q98y/vZMsKGTO2o Hv94wABhsbQNVnBuI2zo8k2r7vL2sQv33AYJHzbYm/MnNPzF1v0SHa1ZQeozTL4UOHCU dogTxSvBHv9HpilWZatqDRbHXGJ3vYqwIfjkges+fKzisaPgltwE7PRAI7Fx1pdeKRzd RAnE6r8nLxKdUQfvNS2yphrol2y/shTP64IamCkOXSMzqx8lWhiEodFW8OkQiMPFfDgh 9RSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789713182; x=1790317982; 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=TbX4sDK3DBzXsbYTT8liO4dVTVuIt9yGIC0SXD7xm8w=; b=LFEw343Lu/iKSA1aOzCbWXjFQnW833JLNCRTxdJQrXYn0q3ZpEz3NPbD/ZJ6KY2vO4 RVoK924DAO0z4tgJeNb+RYuJ+H+upf7/HAH5870F2/ePVzJeMfv/4b6y5QmWvVuUMkPP zDHQkXqd+j1xT1yq/wEW8AWL351oqPa3Q3pcwtyrU/YDeqvC1niU67OP0z5+kk6ktZ+9 sK0LhTjTk4OHKUd94RCtoaz4XcbmgTaSNcJb8ziOzj+T5MAtO5iViMvm9k0l/tHr9wfX Vi+lETvS75Mh1n6pRQA/3qsLWKwSwlUWHDOs5HGuzjntm5arowUKRluMgGDTn0AiRB0i 6d7Q== X-Forwarded-Encrypted: i=1; AKwUvBzkBAPEnR638KUFdD7ODw4Ja7EtzNh9k+7Qz4ktp0Z18CXcgqmlIdffkiejp4NzILzZsbGHI0B7B02MDRc=@vger.kernel.org X-Gm-Message-State: AFuF++mjbKZOiXcRXzg9/AOcCwFimKcmkIpePkAmH82Bk3pQJiGGiBcd snhgXm9oRmB/y3s2tllovoflC7bSWgQBltJRbC6hRQwU9xThML1rsRbZuRx9HgcqB9O8S+HePgK QtKj6gkCrKw== X-Received: from dybgl36.prod.google.com ([2002:a05:7300:e024:b0:338:9523:cc97]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:d410:b0:39d:f024:c7e2 with SMTP id 98e67ed59e1d1-39e54cb099bmr3457757a91.4.1789713181901; Thu, 17 Sep 2026 23:33:01 -0700 (PDT) Date: Thu, 17 Sep 2026 23:32:49 -0700 In-Reply-To: <20260918063249.2172589-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260918063249.2172589-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260918063249.2172589-6-irogers@google.com> Subject: [PATCH v1 5/5] perf thread: Free the comm read from procfs From: Ian Rogers To: Arnaldo Carvalho de Melo , Namhyung Kim Cc: Peter Zijlstra , Ingo Molnar , Jiri Olsa , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" procfs__read_str() allocates the buffer it hands back and thread__set_comm() only stores an interned copy of the string, so the caller owns the buffer. thread__set_comm_from_proc() freed it when the read came back empty but not once it had been used, leaking it on every successful call. Free it on both paths. The empty read still returns -1, err being untouched in that case. Found with leak sanitizer while running 'perf trace': Direct leak of 7 byte(s) in 1 object(s) allocated from: #1 io__getdelim fs/../io.h:179 #2 filename__read_str fs/fs.c:365 #3 procfs__read_str fs/fs.c:402 #4 thread__set_comm_from_proc util/thread.c:297 #5 syscall_arg__scnprintf_pid trace/beauty/pid.c:15 Assisted-by: Antigravity:gemini-3.1-pro Signed-off-by: Ian Rogers --- tools/perf/util/thread.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index f0d3773d87db..fcf7c78ab767 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -296,12 +296,12 @@ int thread__set_comm_from_proc(struct thread *thread) thread__pid(thread), thread__tid(thread)) >=3D (int)sizeof(path))= && procfs__read_str(path, &comm, &sz) =3D=3D 0) { /* sz=3D=3D0: read got nothing, e.g. race during exit teardown */ - if (sz =3D=3D 0) { - free(comm); - return -1; + if (sz > 0) { + comm[sz - 1] =3D '\0'; + err =3D thread__set_comm(thread, comm, 0); } - comm[sz - 1] =3D '\0'; - err =3D thread__set_comm(thread, comm, 0); + /* thread__set_comm() copies the string, so release the buffer. */ + free(comm); } =20 return err; --=20 2.55.0.1082.g2b9226bbc0-goog