[PATCH v4 03/18] perf evsel: Report an allocation failure as ENOMEM when setting filters

From: Ian Rogers

Date: Fri Sep 18 2026 - 17:21:10 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>
---
tools/perf/util/evlist.c | 12 ++++++++++--
tools/perf/util/evsel.c | 4 ++--
2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index c3d784727810..d403ab610bd3 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -1232,8 +1232,12 @@ int evlist__set_tp_filter(struct evlist *evlist, 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)
@@ -1252,8 +1256,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 c663aafa88b2..6836156c2a7d 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1872,7 +1872,7 @@ int evsel__set_filter(struct evsel *evsel, const char *filter)
return 0;
}

- 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.55.0.1082.g2b9226bbc0-goog