All of lore.kernel.org
 help / color / mirror / Atom feed
From: Namhyung Kim <namhyung@kernel.org>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Ian Rogers <irogers@google.com>, Jiri Olsa <jolsa@kernel.org>,
	Adrian Hunter <adrian.hunter@intel.com>,
	James Clark <james.clark@linaro.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@kernel.org>,
	LKML <linux-kernel@vger.kernel.org>,
	linux-perf-users@vger.kernel.org,
	Athira Rajeev <atrajeev@linux.ibm.com>
Subject: [PATCH v5 09/11] perf tools: Only enter mount namespace if necessary
Date: Sun, 29 Nov 2026 16:20:43 -0800	[thread overview]
Message-ID: <20261130002045.1964237-10-namhyung@kernel.org> (raw)
In-Reply-To: <20261130002045.1964237-1-namhyung@kernel.org>

Add dso_binary_type__needs_ns() to distinguish DSO binary and debuginfo
types that reside inside the target process's mount namespace from
host-side paths.

Gate mount namespace entry in open_dso(), dso__find_binary_type_locked(),
dso__find_dbginfo_type(), __dso__read_symbol(), debuginfo__new(),
symbol__disassemble(), and symbol__disassemble_capstone_powerpc() using
dso_binary_type__needs_ns(), and use __dso__get_filename() in
dso__find_binary_type_locked() and dso__find_dbginfo_type() to avoid
empty mount namespace entries for non-DEBUGLINK candidates that fail to
build a filename.

Tested-by: Athira Rajeev <atrajeev@linux.ibm.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/capstone.c  |  8 +++--
 tools/perf/util/debuginfo.c | 19 ++++++----
 tools/perf/util/disasm.c    |  8 ++++-
 tools/perf/util/dso.c       | 71 ++++++++++++++++++++++++++++++-------
 tools/perf/util/dso.h       | 34 ++++++++++++++++++
 5 files changed, 118 insertions(+), 22 deletions(-)

diff --git a/tools/perf/util/capstone.c b/tools/perf/util/capstone.c
index 95881f15324aa614..e1de77879c5e5f97 100644
--- a/tools/perf/util/capstone.c
+++ b/tools/perf/util/capstone.c
@@ -649,13 +649,17 @@ int symbol__disassemble_capstone_powerpc(const char *filename __maybe_unused,
 	bool disassembler_style = false;
 	uint16_t e_machine = EM_NONE;
 	bool is_big_endian = false;
+	bool nsenter;
 
 	if (args->options->objdump_path)
 		return -1;
 
-	nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
+	nsenter = dso_binary_type__needs_ns(dso__binary_type(dso));
+	if (nsenter)
+		nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
 	fd = open(filename, O_RDONLY);
-	nsinfo__mountns_exit(&nsc);
+	if (nsenter)
+		nsinfo__mountns_exit(&nsc);
 	if (fd < 0)
 		return -1;
 
diff --git a/tools/perf/util/debuginfo.c b/tools/perf/util/debuginfo.c
index db188c397d63880c..d339906c28b41425 100644
--- a/tools/perf/util/debuginfo.c
+++ b/tools/perf/util/debuginfo.c
@@ -17,6 +17,7 @@
 #include "dso.h"
 #include "debug.h"
 #include "debuginfo.h"
+#include "namespaces.h"
 #include "symbol.h"
 
 #ifdef HAVE_DEBUGINFOD_SUPPORT
@@ -93,17 +94,23 @@ static struct debuginfo *__debuginfo__new(const char *path)
 
 struct debuginfo *debuginfo__new(struct dso *dso)
 {
-	struct debuginfo *dinfo;
+	struct debuginfo *dinfo = NULL;
+	struct nscookie nsc;
 	char *path;
 	bool decomp = false;
+	bool nsenter;
 
 	dso__find_dbginfo_type(dso);
+	nsenter = dso_binary_type__needs_ns(dso__dbginfo_type(dso));
+	if (nsenter)
+		nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
 	path = dso__get_filename(dso, "", &decomp, dso__dbginfo_type(dso));
-	if (path == NULL)
-		return NULL;
-
-	dinfo = __debuginfo__new(path);
-	dso__put_filename(dso, path, decomp);
+	if (path != NULL) {
+		dinfo = __debuginfo__new(path);
+		dso__put_filename(dso, path, decomp);
+	}
+	if (nsenter)
+		nsinfo__mountns_exit(&nsc);
 	return dinfo;
 }
 
diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
index ca478191813cf46f..5b9bc94be55ef088 100644
--- a/tools/perf/util/disasm.c
+++ b/tools/perf/util/disasm.c
@@ -1499,7 +1499,8 @@ int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
 	bool delete_extract = false;
 	struct kcore_extract kce;
 	enum dso_binary_type dbt;
-	bool decomp = false;
+	struct nscookie nsc;
+	bool decomp = false, nsenter;
 	int err;
 
 	dbt = dso__find_binary_type(dso);
@@ -1507,8 +1508,13 @@ int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
 	if (dbt == DSO_BINARY_TYPE__KALLSYMS)
 		return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
 
+	nsenter = dso_binary_type__needs_ns(dbt);
 	mutex_lock(dso__lock(dso));
+	if (nsenter)
+		nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
 	symfs_filename = dso__get_filename(dso, "", &decomp, dbt);
+	if (nsenter)
+		nsinfo__mountns_exit(&nsc);
 	mutex_unlock(dso__lock(dso));
 
 	if (symfs_filename == NULL) {
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index dafd5acd6be41908..5f10238a3097f651 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -575,19 +575,38 @@ char *dso__filename_with_chroot(const struct dso *dso, const char *filename)
 	return filename_with_chroot(nsinfo__pid(nsi), filename);
 }
 
-char *dso__get_filename(struct dso *dso, const char *root_dir, bool *decomp,
-			enum dso_binary_type type)
+static char *__dso__get_filename(struct dso *dso, const char *root_dir,
+				 bool *decomp, enum dso_binary_type type,
+				 struct nscookie *nsc, bool *nsentered)
 {
 	char *name = malloc(PATH_MAX);
+	bool nsenter = nsc && nsentered && dso_binary_type__needs_ns(type);
 
+	if (nsentered)
+		*nsentered = false;
 	*decomp = false;
 
 	if (name == NULL)
 		return NULL;
 
+	/*
+	 * Only DEBUGLINK accesses files when building the filename; defer
+	 * mount namespace entry until after reading the filename for other
+	 * types to skip empty namespace entries.
+	 */
+	if (nsenter && type == DSO_BINARY_TYPE__DEBUGLINK) {
+		nsinfo__mountns_enter(dso__nsinfo(dso), nsc);
+		*nsentered = true;
+	}
+
 	if (dso__read_binary_type_filename(dso, type, root_dir, name, PATH_MAX))
 		goto out;
 
+	if (nsenter && !*nsentered) {
+		nsinfo__mountns_enter(dso__nsinfo(dso), nsc);
+		*nsentered = true;
+	}
+
 	if (!is_regular_file(name)) {
 		struct stat st;
 		char *new_name;
@@ -642,10 +661,23 @@ char *dso__get_filename(struct dso *dso, const char *root_dir, bool *decomp,
 	return name;
 
 out:
+	if (nsentered && *nsentered) {
+		int saved_errno = errno;
+
+		nsinfo__mountns_exit(nsc);
+		*nsentered = false;
+		errno = saved_errno;
+	}
 	free(name);
 	return NULL;
 }
 
+char *dso__get_filename(struct dso *dso, const char *root_dir, bool *decomp,
+			enum dso_binary_type type)
+{
+	return __dso__get_filename(dso, root_dir, decomp, type, NULL, NULL);
+}
+
 void dso__put_filename(struct dso *dso __maybe_unused, char *filename, bool decomp)
 {
 	if (decomp)
@@ -692,9 +724,7 @@ static int open_dso(struct dso *dso, struct machine *machine)
 {
 	int fd;
 	struct nscookie nsc;
-	enum dso_binary_type bt = dso__binary_type(dso);
-	bool nsenter = bt != DSO_BINARY_TYPE__BUILD_ID_CACHE &&
-		       bt != DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO;
+	bool nsenter = dso_binary_type__needs_ns(dso__binary_type(dso));
 
 	if (nsenter) {
 		mutex_lock(dso__lock(dso));
@@ -875,10 +905,14 @@ static int dso__find_binary_type_locked(struct dso *dso)
 	}
 
 	do {
-		dso__set_binary_type(dso, binary_type_data[i++]);
+		struct nscookie nsc;
+		enum dso_binary_type type = binary_type_data[i++];
+		bool nsenter = false;
+
+		dso__set_binary_type(dso, type);
 
 		errno = 0;
-		path = dso__get_filename(dso, "", &decomp, dso__binary_type(dso));
+		path = __dso__get_filename(dso, "", &decomp, type, &nsc, &nsenter);
 		if (path == NULL) {
 			if (errno == EMFILE)
 				goto open_err;
@@ -890,9 +924,11 @@ static int dso__find_binary_type_locked(struct dso *dso)
 		if (!found)
 			saved_errno = errno;
 		dso__put_filename(dso, path, decomp);
+		if (nsenter)
+			nsinfo__mountns_exit(&nsc);
 
 		if (found)
-			return dso__binary_type(dso);
+			return type;
 		else if (saved_errno == EMFILE)
 			goto open_err;
 
@@ -2117,11 +2153,14 @@ static const u8 *__dso__read_symbol(struct dso *dso, const char *symfs_filename,
 	};
 	u8 *code_buf = NULL;
 	int saved_errno;
+	bool nsenter = dso_binary_type__needs_ns(dso__binary_type(dso));
 
-	nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
+	if (nsenter)
+		nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
 	fd = open(symfs_filename, O_RDONLY | O_CLOEXEC);
 	saved_errno = errno;
-	nsinfo__mountns_exit(&nsc);
+	if (nsenter)
+		nsinfo__mountns_exit(&nsc);
 	if (fd < 0) {
 		errno = saved_errno;
 		return NULL;
@@ -2242,8 +2281,9 @@ void dso__find_dbginfo_type(struct dso *dso)
 		DSO_BINARY_TYPE__NOT_FOUND,
 	};
 	const enum dso_binary_type *type;
+	struct nscookie nsc;
 	char *path;
-	bool found, decomp = false;
+	bool found, decomp = false, nsenter = false;
 
 	if (dso__dbginfo_type(dso) != DSO_BINARY_TYPE__NOT_FOUND)
 		return;
@@ -2253,12 +2293,15 @@ void dso__find_dbginfo_type(struct dso *dso)
 	dso__set_debuginfo_searched(dso);
 
 	/* Maybe debug info is in the same file with the symbol table */
-	path = dso__get_filename(dso, "", &decomp, dso__symtab_type(dso));
+	path = __dso__get_filename(dso, "", &decomp, dso__symtab_type(dso),
+				   &nsc, &nsenter);
 	if (path) {
 		found = filename__has_section(path, ".debug_info") ||
 			filename__has_section(path, ".zdebug_info");
 
 		dso__put_filename(dso, path, decomp);
+		if (nsenter)
+			nsinfo__mountns_exit(&nsc);
 		if (found) {
 			dso__set_dbginfo_type(dso, dso__symtab_type(dso));
 			return;
@@ -2267,7 +2310,7 @@ void dso__find_dbginfo_type(struct dso *dso)
 
 	/* Otherwise check distro debug locations */
 	for (type = dbginfo_types; *type != DSO_BINARY_TYPE__NOT_FOUND; type++) {
-		path = dso__get_filename(dso, "", &decomp, *type);
+		path = __dso__get_filename(dso, "", &decomp, *type, &nsc, &nsenter);
 		if (path == NULL)
 			continue;
 
@@ -2275,6 +2318,8 @@ void dso__find_dbginfo_type(struct dso *dso)
 			filename__has_section(path, ".zdebug_info");
 
 		dso__put_filename(dso, path, decomp);
+		if (nsenter)
+			nsinfo__mountns_exit(&nsc);
 		if (found)
 			break;
 	}
diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index ebd8230b9895dcf1..75483598e093fb1b 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -142,6 +142,40 @@ enum dso_binary_type {
 	DSO_BINARY_TYPE__NOT_FOUND,
 };
 
+static inline bool dso_binary_type__needs_ns(enum dso_binary_type type)
+{
+	switch (type) {
+	case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
+	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
+	case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
+	case DSO_BINARY_TYPE__DEBUGLINK:
+	case DSO_BINARY_TYPE__GNU_DEBUGDATA:
+	case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
+	case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
+	case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
+	case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
+	case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
+		return true;
+	case DSO_BINARY_TYPE__KALLSYMS:
+	case DSO_BINARY_TYPE__GUEST_KALLSYMS:
+	case DSO_BINARY_TYPE__VMLINUX:
+	case DSO_BINARY_TYPE__GUEST_VMLINUX:
+	case DSO_BINARY_TYPE__JAVA_JIT:
+	case DSO_BINARY_TYPE__BUILD_ID_CACHE:
+	case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
+	case DSO_BINARY_TYPE__GUEST_KMODULE:
+	case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
+	case DSO_BINARY_TYPE__KCORE:
+	case DSO_BINARY_TYPE__GUEST_KCORE:
+	case DSO_BINARY_TYPE__BPF_PROG_INFO:
+	case DSO_BINARY_TYPE__BPF_IMAGE:
+	case DSO_BINARY_TYPE__OOL:
+	case DSO_BINARY_TYPE__NOT_FOUND:
+	default:
+		return false;
+	}
+}
+
 enum dso_space_type {
 	DSO_SPACE__USER = 0,
 	DSO_SPACE__KERNEL,
-- 
2.56.1.839.g22a8290db9-goog


  parent reply	other threads:[~2026-11-30  0:20 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  0:20 [PATCH v5 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
2026-11-30  0:20 ` [PATCH v5 01/11] perf tools: Add HEADER_BOOT_ID feature Namhyung Kim
2026-11-30 14:14   ` Athira Rajeev
2026-11-30  0:20 ` [PATCH v5 02/11] perf tools: Add perf_env__is_same_boot() Namhyung Kim
2026-11-30  0:20 ` [PATCH v5 03/11] perf tools: Add nsinfo__new_thread() to track tid Namhyung Kim
2026-11-30  0:20 ` [PATCH v5 04/11] perf tools: Add lock to struct nsinfo Namhyung Kim
2026-11-30  0:20 ` [PATCH v5 05/11] perf tools: Defer /proc lookup until nsinfo is used Namhyung Kim
2026-11-30  0:20 ` [PATCH v5 06/11] perf tools: Verify thread status and mount namespace inode Namhyung Kim
2026-11-30  0:20 ` [PATCH v5 07/11] perf tools: Check thread liveness when accessing /proc Namhyung Kim
2026-11-30  0:20 ` [PATCH v5 08/11] perf tools: Export filename__read_build_id_ns() Namhyung Kim
2026-11-30  0:20 ` Namhyung Kim [this message]
2026-11-30  0:20 ` [PATCH v5 10/11] perf tools: Scope mount namespace entry in dso__load() Namhyung Kim
2026-11-30  0:20 ` [PATCH v5 11/11] perf tests: Add namespace, nsinfo, and boot ID tests Namhyung Kim
2026-11-30  0:34 ` [PATCH v5 00/11] perf tools: Enhance namespace and chroot handling sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130002045.1964237-10-namhyung@kernel.org \
    --to=namhyung@kernel.org \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=atrajeev@linux.ibm.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.