[PATCH v5 05/23] perf evsel: Report an allocation failure as ENOMEM when setting filters
From: Ian Rogers
Date: Wed Sep 23 2026 - 03:19:14 EST
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 <irogers@xxxxxxxxxx>
Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
---
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, const char *filter)
struct evsel *evsel;
int err = 0;
+ /*
+ * The only caller that passes NULL is evlist__set_tp_filter_pids(),
+ * where it means asprintf__tp_filter_pids() failed to allocate.
+ */
if (filter == NULL)
- return -1;
+ return -ENOMEM;
evlist__for_each_entry(evlist, evsel) {
if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
@@ -1452,8 +1456,12 @@ int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
struct evsel *evsel;
int err = 0;
+ /*
+ * As above, a NULL filter is asprintf__tp_filter_pids() having failed
+ * to allocate in evlist__append_tp_filter_pids().
+ */
if (filter == NULL)
- return -1;
+ return -ENOMEM;
evlist__for_each_entry(evlist, evsel) {
if (evsel->core.attr.type != 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;
}
- return -1;
+ return -ENOMEM;
}
static int evsel__append_filter(struct evsel *evsel, const char *fmt, const char *filter)
@@ -1888,7 +1888,7 @@ static int evsel__append_filter(struct evsel *evsel, const char *fmt, const char
return 0;
}
- return -1;
+ return -ENOMEM;
}
int evsel__append_tp_filter(struct evsel *evsel, const char *filter)
--
2.56.0.rc1.315.gc6ed9934b7-goog