Skip to contentSkip to conversation

perf symbol: Fix ENOENT case for filename__read_build_id

PATCH Applied Ian Rogers 3 participants 4 messages Dec 7, 2025 – Dec 23, 2025 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf symbol: Fix ENOENT case for filename__read_build_id

Some callers of filename__read_build_id assume the error value must be -1, fix by making them handle all < 0 values.

If is_regular_file fails in filename__read_build_id then it could be the file is missing (ENOENT) and it would be wrong to return -EWOULDBLOCK in that case. Fix the logic so -EWOULDBLOCK is only reported if other errors with stat haven't occurred.

Fixes 834ebb5678d7 ("perf tools: Don't read build-ids from non-regular files")

---
Note, a shorter version of this patch appeared in a series cleaning/refactoring the build id reading code:
https://lore.kernel.org/lkml/[email protected]/
It is sent without the refactor for the sake of fixing the bug in an earlier release.

---
 tools/perf/builtin-buildid-cache.c | 6 ++++--
 tools/perf/util/libbfd.c           | 4 +++-
 tools/perf/util/symbol-elf.c       | 4 +++-
 tools/perf/util/symbol-minimal.c   | 4 +++-
 4 files changed, 13 insertions(+), 5 deletions(-)
tools/perf/builtin-buildid-cache.c +4 -2
index c98104481c8a..539e779e3268 100644
@@ -276,12 +276,14 @@ static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
 {
 	char filename[PATH_MAX];
 	struct build_id bid = { .size = 0, };
+	int err;
 
 	if (!dso__build_id_filename(dso, filename, sizeof(filename), false))
 		return true;
 
-	if (filename__read_build_id(filename, &bid) == -1) {
-		if (errno == ENOENT)
+	err = filename__read_build_id(filename, &bid);
+	if (err < 0) {
+		if (err == -ENOENT)
 			return false;
 
 		pr_warning("Problems with %s file, consider removing it from the cache\n",
tools/perf/util/libbfd.c +3 -1
index 0099b415decc..edf0829e9635 100644
@@ -391,8 +391,10 @@ int libbfd__read_build_id(const char *filename, struct build_id *bid)
 
 	if (!filename)
 		return -EFAULT;
+
+	errno = 0;
 	if (!is_regular_file(filename))
-		return -EWOULDBLOCK;
+		return errno == 0 ? -EWOULDBLOCK : -errno;
 
 	fd = open(filename, O_RDONLY);
 	if (fd < 0)
tools/perf/util/symbol-elf.c +3 -1
index 957143fbf8a0..d1dcafa4b3b8 100644
@@ -902,8 +902,10 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
 
 	if (!filename)
 		return -EFAULT;
+
+	errno = 0;
 	if (!is_regular_file(filename))
-		return -EWOULDBLOCK;
+		return errno == 0 ? -EWOULDBLOCK : -errno;
 
 	err = kmod_path__parse(&m, filename);
 	if (err)
tools/perf/util/symbol-minimal.c +3 -1
index c6b17c14a2e9..8221dc9868f7 100644
@@ -104,8 +104,10 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
 
 	if (!filename)
 		return -EFAULT;
+
+	errno = 0;
 	if (!is_regular_file(filename))
-		return -EWOULDBLOCK;
+		return errno == 0 ? -EWOULDBLOCK : -errno;
 
 	fd = open(filename, O_RDONLY);
 	if (fd < 0)
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Ian Rogers

Some callers of filename__read_build_id assume the error value must be -1, fix by making them handle all < 0 values.

If is_regular_file fails in filename__read_build_id then it could be the file is missing (ENOENT) and it would be wrong to return -EWOULDBLOCK in that case. Fix the logic so -EWOULDBLOCK is only reported if other errors with stat haven't occurred.

Quoted 8 lines
Fixes 834ebb5678d7 ("perf tools: Don't read build-ids from non-regular files")

---
Note, a shorter version of this patch appeared in a series cleaning/refactoring the build id reading code:
https://lore.kernel.org/lkml/[email protected]/
It is sent without the refactor for the sake of fixing the bug in an earlier release.

Quoted 76 lines
---
  tools/perf/builtin-buildid-cache.c | 6 ++++--
  tools/perf/util/libbfd.c           | 4 +++-
  tools/perf/util/symbol-elf.c       | 4 +++-
  tools/perf/util/symbol-minimal.c   | 4 +++-
  4 files changed, 13 insertions(+), 5 deletions(-)
tools/perf/builtin-buildid-cache.c +4 -2
index c98104481c8a..539e779e3268 100644
@@ -276,12 +276,14 @@ static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
  {
  	char filename[PATH_MAX];
  	struct build_id bid = { .size = 0, };
+	int err;
 
  	if (!dso__build_id_filename(dso, filename, sizeof(filename), false))
  		return true;
 
-	if (filename__read_build_id(filename, &bid) == -1) {
-		if (errno == ENOENT)
+	err = filename__read_build_id(filename, &bid);
+	if (err < 0) {
+		if (err == -ENOENT)
  			return false;
 
  		pr_warning("Problems with %s file, consider removing it from the cache\n",
tools/perf/util/libbfd.c +3 -1
index 0099b415decc..edf0829e9635 100644
@@ -391,8 +391,10 @@ int libbfd__read_build_id(const char *filename, struct build_id *bid)
 
  	if (!filename)
  		return -EFAULT;
+
+	errno = 0;
  	if (!is_regular_file(filename))
-		return -EWOULDBLOCK;
+		return errno == 0 ? -EWOULDBLOCK : -errno;
 
  	fd = open(filename, O_RDONLY);
  	if (fd < 0)
tools/perf/util/symbol-elf.c +3 -1
index 957143fbf8a0..d1dcafa4b3b8 100644
@@ -902,8 +902,10 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
 
  	if (!filename)
  		return -EFAULT;
+
+	errno = 0;
  	if (!is_regular_file(filename))
-		return -EWOULDBLOCK;
+		return errno == 0 ? -EWOULDBLOCK : -errno;
 
  	err = kmod_path__parse(&m, filename);
  	if (err)
tools/perf/util/symbol-minimal.c +3 -1
index c6b17c14a2e9..8221dc9868f7 100644
@@ -104,8 +104,10 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
 
  	if (!filename)
  		return -EFAULT;
+
+	errno = 0;
  	if (!is_regular_file(filename))
-		return -EWOULDBLOCK;
+		return errno == 0 ? -EWOULDBLOCK : -errno;
 
  	fd = open(filename, O_RDONLY);
  	if (fd < 0)
maintainer
#2 lore raw
In reply to Ian Rogers

Hi Ian,

Quoted 8 lines · Ian Rogers

Some callers of filename__read_build_id assume the error value must be -1, fix by making them handle all < 0 values.

If is_regular_file fails in filename__read_build_id then it could be the file is missing (ENOENT) and it would be wrong to return -EWOULDBLOCK in that case. Fix the logic so -EWOULDBLOCK is only reported if other errors with stat haven't occurred.

Quoted 8 lines
Fixes 834ebb5678d7 ("perf tools: Don't read build-ids from non-regular files")

---
Note, a shorter version of this patch appeared in a series cleaning/refactoring the build id reading code:
https://lore.kernel.org/lkml/[email protected]/
It is sent without the refactor for the sake of fixing the bug in an earlier release.

Thanks for the fix, I'll apply this to perf-tools for v6.19.

Namhyung

Quoted 79 lines
---
 tools/perf/builtin-buildid-cache.c | 6 ++++--
 tools/perf/util/libbfd.c           | 4 +++-
 tools/perf/util/symbol-elf.c       | 4 +++-
 tools/perf/util/symbol-minimal.c   | 4 +++-
 4 files changed, 13 insertions(+), 5 deletions(-)
tools/perf/builtin-buildid-cache.c +4 -2
index c98104481c8a..539e779e3268 100644
@@ -276,12 +276,14 @@ static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
 {
 	char filename[PATH_MAX];
 	struct build_id bid = { .size = 0, };
+	int err;
 
 	if (!dso__build_id_filename(dso, filename, sizeof(filename), false))
 		return true;
 
-	if (filename__read_build_id(filename, &bid) == -1) {
-		if (errno == ENOENT)
+	err = filename__read_build_id(filename, &bid);
+	if (err < 0) {
+		if (err == -ENOENT)
 			return false;
 
 		pr_warning("Problems with %s file, consider removing it from the cache\n",
tools/perf/util/libbfd.c +3 -1
index 0099b415decc..edf0829e9635 100644
@@ -391,8 +391,10 @@ int libbfd__read_build_id(const char *filename, struct build_id *bid)
 
 	if (!filename)
 		return -EFAULT;
+
+	errno = 0;
 	if (!is_regular_file(filename))
-		return -EWOULDBLOCK;
+		return errno == 0 ? -EWOULDBLOCK : -errno;
 
 	fd = open(filename, O_RDONLY);
 	if (fd < 0)
tools/perf/util/symbol-elf.c +3 -1
index 957143fbf8a0..d1dcafa4b3b8 100644
@@ -902,8 +902,10 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
 
 	if (!filename)
 		return -EFAULT;
+
+	errno = 0;
 	if (!is_regular_file(filename))
-		return -EWOULDBLOCK;
+		return errno == 0 ? -EWOULDBLOCK : -errno;
 
 	err = kmod_path__parse(&m, filename);
 	if (err)
tools/perf/util/symbol-minimal.c +3 -1
index c6b17c14a2e9..8221dc9868f7 100644
@@ -104,8 +104,10 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
 
 	if (!filename)
 		return -EFAULT;
+
+	errno = 0;
 	if (!is_regular_file(filename))
-		return -EWOULDBLOCK;
+		return errno == 0 ? -EWOULDBLOCK : -errno;
 
 	fd = open(filename, O_RDONLY);
 	if (fd < 0)
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
maintainer Applied
#3 lore raw
In reply to Ian Rogers
Quoted 9 lines · Ian Rogers

Some callers of filename__read_build_id assume the error value must be -1, fix by making them handle all < 0 values.

If is_regular_file fails in filename__read_build_id then it could be the file is missing (ENOENT) and it would be wrong to return -EWOULDBLOCK in that case. Fix the logic so -EWOULDBLOCK is only reported if other errors with stat haven't occurred.

[...]

Applied to perf-tools, thanks!

Best regards,
Namhyung