mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3 00/11] perf tools: Enhance namespace and chroot handling
@ 2026-11-23 23:20 Namhyung Kim
  2026-11-23 23:20 ` [PATCH v3 01/11] perf tools: Add HEADER_BOOT_ID feature Namhyung Kim
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: Namhyung Kim @ 2026-11-23 23:20 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ian Rogers, Jiri Olsa, Adrian Hunter, James Clark,
	Peter Zijlstra, Ingo Molnar, LKML, linux-perf-users

Hello,

This series improves namespace and chroot handling when perf accesses
files from /proc.  It defers reading /proc until namespace info is
actually needed, verifies thread liveness, matching boot IDs, and
mount namespace inodes against PID/TID recycling, and avoids entering
target mount namespaces for host-side files such as the build-id cache.

It's assisted by LLM.

v3 changes:

 * Guard evlist before calling evlist__session() in write_boot_id().
 * Repipe PERF_RECORD_HEADER_FEATURE events in perf-inject pipe mode
   and add a boot ID pipe-injection shell test and unit test.
 * Pin a reference-counted host /proc descriptor across
   nsinfo__mountns_enter()/exit(), access relative /proc paths via
   stack-buffered nsinfo__proc_{open,stat,readlink}() helpers instead of
   heap-allocating mntns_path, and handle ENOENT/ESRCH on ns/mnt stat.
 * Skip filename_with_chroot() chroot resolution when
   nsinfo__need_setns() is set.
 * Add compiler context-analysis lock annotations (__guarded_by,
   __must_hold, __returns_ctx_lock) for struct nsinfo, host_proc_lock,
   thread->nsinfo, and __thread__reset_nsinfo(), plus locked accessors.
 * Expand mount namespace tests to verify overmounted /proc during
   nsinfo__mountns_enter() and own a private mount namespace in
   enter_private_userns() for clean nsinfo__mountns_exit() restoration.

v2: https://lore.kernel.org/r/20261116055731.707164-1-namhyung@kernel.org

 * Keep dso__filename_with_chroot() (guarded by nsinfo__check_proc())
   instead of calling chroot() inside nsinfo__mountns_enter().
 * Add a mutex lock to struct nsinfo and replace thread->nsinfo safely on
   exec/namespace events rather than mutating shared instances in place.
 * Mark offline sessions explicitly, propagate exit on fork replacement
   and perf_event__exit_del_thread(), and guard sub-thread fallback in
   thread__e_machine_endian().
 * Gate mount namespace entry for debuginfo/binary probing callers,
   include distro debuginfo types, and handle build-id lookups in
   symbol-minimal's dso__load_sym().
 * Add unit and shell tests for namespace, nsinfo, chroot, and boot ID
   handling.

v1: https://lore.kernel.org/r/20261023071141.134149-1-namhyung@kernel.org

Thanks,
Namhyung


Namhyung Kim (11):
  perf tools: Add HEADER_BOOT_ID feature
  perf tools: Add perf_env__is_same_boot()
  perf tools: Add nsinfo__new_thread() to track tid
  perf tools: Add lock to struct nsinfo
  perf tools: Defer /proc lookup until nsinfo is used
  perf tools: Verify thread status and mount namespace inode
  perf tools: Check thread liveness when accessing /proc
  perf tools: Export filename__read_build_id_ns()
  perf tools: Only enter mount namespace if necessary
  perf tools: Scope mount namespace entry in dso__load()
  perf tests: Add namespace, nsinfo, and boot ID tests

 .../Documentation/perf.data-file-format.txt   |    5 +
 tools/perf/builtin-inject.c                   |   29 +-
 tools/perf/builtin-trace.c                    |    2 +-
 tools/perf/tests/Build                        |    1 +
 tools/perf/tests/builtin-test.c               |    1 +
 tools/perf/tests/namespaces.c                 | 1188 +++++++++++++++++
 tools/perf/tests/shell/header.sh              |    9 +
 tools/perf/tests/shell/pipe_test.sh           |   21 +
 tools/perf/tests/tests.h                      |    1 +
 tools/perf/util/build-id.c                    |   15 +-
 tools/perf/util/capstone.c                    |    8 +-
 tools/perf/util/debuginfo.c                   |   19 +-
 tools/perf/util/disasm.c                      |    8 +-
 tools/perf/util/dso.c                         |   58 +-
 tools/perf/util/dso.h                         |   34 +
 tools/perf/util/dsos.c                        |   17 +-
 tools/perf/util/env.c                         |   69 +
 tools/perf/util/env.h                         |    4 +
 tools/perf/util/event.c                       |    1 +
 tools/perf/util/header.c                      |   28 +
 tools/perf/util/header.h                      |    1 +
 tools/perf/util/jitdump.c                     |    2 +-
 tools/perf/util/machine.c                     |   16 +-
 tools/perf/util/map.c                         |    2 +-
 tools/perf/util/namespaces.c                  |  439 +++++-
 tools/perf/util/namespaces.h                  |   23 +-
 tools/perf/util/session.c                     |    1 +
 tools/perf/util/symbol-minimal.c              |    4 +-
 tools/perf/util/symbol.c                      |   45 +-
 tools/perf/util/symbol.h                      |    3 +
 tools/perf/util/thread.c                      |   97 +-
 tools/perf/util/thread.h                      |   15 +-
 tools/perf/util/util.c                        |    7 +-
 33 files changed, 1998 insertions(+), 175 deletions(-)
 create mode 100644 tools/perf/tests/namespaces.c

-- 
2.56.1.809.ga0b61035b5-goog


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 01/11] perf tools: Add HEADER_BOOT_ID feature
  2026-11-23 23:20 [PATCH v3 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
@ 2026-11-23 23:20 ` Namhyung Kim
  2026-11-23 23:20 ` [PATCH v3 02/11] perf tools: Add perf_env__is_same_boot() Namhyung Kim
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Namhyung Kim @ 2026-11-23 23:20 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ian Rogers, Jiri Olsa, Adrian Hunter, James Clark,
	Peter Zijlstra, Ingo Molnar, LKML, linux-perf-users

Save /proc/sys/kernel/random/boot_id in the perf.data header and
struct perf_env so post-processing tools can identify if it's the
same kernel boot on which the data was recorded.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 .../Documentation/perf.data-file-format.txt   |  5 +++
 tools/perf/builtin-inject.c                   |  1 +
 tools/perf/util/env.c                         | 44 +++++++++++++++++++
 tools/perf/util/env.h                         |  3 ++
 tools/perf/util/header.c                      | 28 ++++++++++++
 tools/perf/util/header.h                      |  1 +
 tools/perf/util/session.c                     |  1 +
 7 files changed, 83 insertions(+)

diff --git a/tools/perf/Documentation/perf.data-file-format.txt b/tools/perf/Documentation/perf.data-file-format.txt
index 1f8e7d8e9ae21d73..359d6ef0c80aac26 100644
--- a/tools/perf/Documentation/perf.data-file-format.txt
+++ b/tools/perf/Documentation/perf.data-file-format.txt
@@ -491,6 +491,11 @@ struct memory_range {
 	u8 remote_region_id;
 }; [nr_memory_ranges]
 
+	HEADER_BOOT_ID = 36,
+
+	A perf_header_string with the kernel boot ID read from
+	/proc/sys/kernel/random/boot_id.
+
 	other bits are reserved and should be ignored for now
 	HEADER_FEAT_BITS	= 256,
 
diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index 3f3108fd39811017..0e01b14fa51676ab 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -2307,6 +2307,7 @@ static bool keep_feat(struct perf_inject *inject, int feat)
 	case HEADER_CPU_DOMAIN_INFO:
 	case HEADER_CLN_SIZE:
 	case HEADER_MEMORY_RANGES:
+	case HEADER_BOOT_ID:
 		return true;
 	/* Information that can be updated */
 	case HEADER_BUILD_ID:
diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c
index 9e8e5d1d822a55a6..f624ba98e23c6a77 100644
--- a/tools/perf/util/env.c
+++ b/tools/perf/util/env.c
@@ -12,6 +12,7 @@
 #include <linux/string.h>
 #include <linux/zalloc.h>
 #include "cgroup.h"
+#include <api/fs/fs.h>
 #include <errno.h>
 #include <sys/utsname.h>
 #include <stdlib.h>
@@ -258,6 +259,7 @@ void perf_env__exit(struct perf_env *env)
 	zfree(&env->os_release);
 	zfree(&env->version);
 	zfree(&env->arch);
+	zfree(&env->boot_id);
 	zfree(&env->cpu_desc);
 	zfree(&env->cpuid);
 	zfree(&env->cmdline);
@@ -401,6 +403,48 @@ const char *perf_env__os_release(struct perf_env *env)
 	return release;
 }
 
+static char *read_host_boot_id(void)
+{
+	char *buf = NULL;
+	size_t sz;
+
+	if (procfs__read_str("sys/kernel/random/boot_id", &buf, &sz) < 0)
+		return NULL;
+
+	strim(buf);
+	return buf;
+}
+
+const char *perf_env__boot_id(struct perf_env *env)
+{
+	const char *boot_id;
+
+	if (!env)
+		return NULL;
+
+	mutex_lock(&env->lock);
+	if (env->boot_id) {
+		boot_id = env->boot_id;
+		goto out;
+	}
+
+	/*
+	 * If this is an offline target environment (set by perf_session on
+	 * read or via HEADER_ARCH / HEADER_HOSTNAME), do not fall back to the
+	 * host's boot_id when boot_id is not populated in the file.
+	 */
+	if (env->offline || env->arch || env->hostname) {
+		boot_id = NULL;
+		goto out;
+	}
+
+	env->boot_id = read_host_boot_id();
+	boot_id = env->boot_id;
+out:
+	mutex_unlock(&env->lock);
+	return boot_id;
+}
+
 int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[])
 {
 	int i;
diff --git a/tools/perf/util/env.h b/tools/perf/util/env.h
index 6b2083e3640c375e..4180380f4533790a 100644
--- a/tools/perf/util/env.h
+++ b/tools/perf/util/env.h
@@ -81,6 +81,7 @@ struct perf_env {
 	char			*os_release;
 	char			*version;
 	char			*arch;
+	char			*boot_id;
 	/* e_machine expanded from 16 to 32-bits for alignment. */
 	u32			e_machine;
 	u32			e_flags;
@@ -154,6 +155,7 @@ struct perf_env {
 	/* For fast cpu to numa node lookup via perf_env__numa_node */
 	int			*numa_map;
 	int			 nr_numa_map;
+	bool			 offline;
 
 	/* For real clock time reference. */
 	struct {
@@ -187,6 +189,7 @@ void perf_env__exit(struct perf_env *env);
 int perf_env__kernel_is_64_bit(struct perf_env *env);
 bool perf_arch_is_big_endian(const char *arch);
 const char *perf_env__os_release(struct perf_env *env);
+const char *perf_env__boot_id(struct perf_env *env);
 
 int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[]);
 
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 9eec6deee190ff21..80fa9a896933a6ed 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -494,6 +494,27 @@ static int write_version(struct feat_fd *ff,
 	return do_write_string(ff, perf_version_string);
 }
 
+static int write_boot_id(struct feat_fd *ff, struct evlist *evlist)
+{
+	const char *boot_id = NULL;
+	char *buf = NULL;
+	size_t sz;
+	int ret;
+
+	if (evlist && evlist__session(evlist))
+		boot_id = perf_env__boot_id(perf_session__env(evlist__session(evlist)));
+
+	if (!boot_id) {
+		if (procfs__read_str("sys/kernel/random/boot_id", &buf, &sz) < 0)
+			return -1;
+		boot_id = strim(buf);
+	}
+
+	ret = do_write_string(ff, boot_id);
+	free(buf);
+	return ret;
+}
+
 static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
 {
 	FILE *file;
@@ -2079,6 +2100,11 @@ static void print_version(struct feat_fd *ff, FILE *fp)
 	fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
 }
 
+static void print_boot_id(struct feat_fd *ff, FILE *fp)
+{
+	fprintf(fp, "# boot id : %s\n", ff->ph->env.boot_id);
+}
+
 static void print_cmdline(struct feat_fd *ff, FILE *fp)
 {
 	int nr, i;
@@ -3022,6 +3048,7 @@ FEAT_PROCESS_STR_FUN(osrelease, os_release);
 FEAT_PROCESS_STR_FUN(version, version);
 FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
 FEAT_PROCESS_STR_FUN(cpuid, cpuid);
+FEAT_PROCESS_STR_FUN(boot_id, boot_id);
 
 static int process_arch(struct feat_fd *ff, void *data __maybe_unused)
 {
@@ -4496,6 +4523,7 @@ const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE] = {
 	FEAT_OPR(E_MACHINE,	e_machine,	false),
 	FEAT_OPR(CLN_SIZE,	cln_size,	false),
 	FEAT_OPR(MEMORY_RANGES,	memory_ranges,	false),
+	FEAT_OPR(BOOT_ID,	boot_id,	false),
 };
 
 struct header_print_data {
diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h
index 765037762758ca11..5bbf16342b4d26d8 100644
--- a/tools/perf/util/header.h
+++ b/tools/perf/util/header.h
@@ -57,6 +57,7 @@ enum {
 	HEADER_E_MACHINE,
 	HEADER_CLN_SIZE,
 	HEADER_MEMORY_RANGES,
+	HEADER_BOOT_ID,
 	HEADER_LAST_FEATURE,
 	HEADER_FEAT_BITS	= 256,
 };
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 4e11eabbbe6fde1b..4d9450ef5df8e9ce 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -179,6 +179,7 @@ struct perf_session *__perf_session__new(struct perf_data *data,
 		session->data = data;
 
 		if (perf_data__is_read(data)) {
+			session->header.env.offline = true;
 			ret = perf_session__open(session);
 			if (ret < 0)
 				goto out_delete;
-- 
2.56.1.809.ga0b61035b5-goog


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 02/11] perf tools: Add perf_env__is_same_boot()
  2026-11-23 23:20 [PATCH v3 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
  2026-11-23 23:20 ` [PATCH v3 01/11] perf tools: Add HEADER_BOOT_ID feature Namhyung Kim
@ 2026-11-23 23:20 ` Namhyung Kim
  2026-11-23 23:20 ` [PATCH v3 03/11] perf tools: Add nsinfo__new_thread() to track tid Namhyung Kim
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Namhyung Kim @ 2026-11-23 23:20 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ian Rogers, Jiri Olsa, Adrian Hunter, James Clark,
	Peter Zijlstra, Ingo Molnar, LKML, linux-perf-users

Add perf_env__is_same_boot() to compare the recorded boot_id in struct
perf_env against the running host's /proc/sys/kernel/random/boot_id.
For live sessions where no offline header was populated, it falls back
to reading the host boot_id and returns true.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/env.c | 25 +++++++++++++++++++++++++
 tools/perf/util/env.h |  1 +
 2 files changed, 26 insertions(+)

diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c
index f624ba98e23c6a77..ce1f9ab2c40a9599 100644
--- a/tools/perf/util/env.c
+++ b/tools/perf/util/env.c
@@ -445,6 +445,31 @@ const char *perf_env__boot_id(struct perf_env *env)
 	return boot_id;
 }
 
+static char *host_boot_id;
+
+static void init_host_boot_id(void)
+{
+	host_boot_id = read_host_boot_id();
+}
+
+bool perf_env__is_same_boot(struct perf_env *env)
+{
+	const char *env_boot_id;
+
+	DO_ONCE(init_host_boot_id);
+	if (!host_boot_id)
+		return false;
+
+	if (!env)
+		return true;
+
+	env_boot_id = perf_env__boot_id(env);
+	if (!env_boot_id)
+		return false;
+
+	return !strcmp(env_boot_id, host_boot_id);
+}
+
 int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[])
 {
 	int i;
diff --git a/tools/perf/util/env.h b/tools/perf/util/env.h
index 4180380f4533790a..2d0a67ae306fb362 100644
--- a/tools/perf/util/env.h
+++ b/tools/perf/util/env.h
@@ -190,6 +190,7 @@ int perf_env__kernel_is_64_bit(struct perf_env *env);
 bool perf_arch_is_big_endian(const char *arch);
 const char *perf_env__os_release(struct perf_env *env);
 const char *perf_env__boot_id(struct perf_env *env);
+bool perf_env__is_same_boot(struct perf_env *env);
 
 int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[]);
 
-- 
2.56.1.809.ga0b61035b5-goog


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 03/11] perf tools: Add nsinfo__new_thread() to track tid
  2026-11-23 23:20 [PATCH v3 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
  2026-11-23 23:20 ` [PATCH v3 01/11] perf tools: Add HEADER_BOOT_ID feature Namhyung Kim
  2026-11-23 23:20 ` [PATCH v3 02/11] perf tools: Add perf_env__is_same_boot() Namhyung Kim
@ 2026-11-23 23:20 ` Namhyung Kim
  2026-11-23 23:20 ` [PATCH v3 04/11] perf tools: Add lock to struct nsinfo Namhyung Kim
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Namhyung Kim @ 2026-11-23 23:20 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ian Rogers, Jiri Olsa, Adrian Hunter, James Clark,
	Peter Zijlstra, Ingo Molnar, LKML, linux-perf-users

Record the thread ID (tid) in struct nsinfo via nsinfo__new_thread() and
access /proc/<pid>/task/<tid>/ status and ns/mnt paths instead of only
/proc/<pid>/.  Also recreate thread->nsinfo when the pid of the thread
is updated.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/machine.c    |  3 +++
 tools/perf/util/namespaces.c | 22 ++++++++++++++++++----
 tools/perf/util/namespaces.h |  3 +++
 tools/perf/util/thread.c     |  2 +-
 4 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index 3c206c26bd62163e..62f3ee8488548ef6 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -19,6 +19,7 @@
 #include "branch.h"
 #include "mem-events.h"
 #include "mem-info.h"
+#include "namespaces.h"
 #include "path.h"
 #include "srcline.h"
 #include "symbol.h"
@@ -517,6 +518,8 @@ static void machine__update_thread_pid(struct machine *machine,
 		return;
 
 	thread__set_pid(th, pid);
+	nsinfo__zput(RC_CHK_ACCESS(th)->nsinfo);
+	RC_CHK_ACCESS(th)->nsinfo = nsinfo__new_thread(pid, thread__tid(th));
 
 	if (thread__pid(th) == thread__tid(th))
 		return;
diff --git a/tools/perf/util/namespaces.c b/tools/perf/util/namespaces.c
index 01502570b32d02ab..f426eba04d6ef449 100644
--- a/tools/perf/util/namespaces.c
+++ b/tools/perf/util/namespaces.c
@@ -106,7 +106,8 @@ int nsinfo__init(struct nsinfo *nsi)
 	if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
 		return rv;
 
-	if (asprintf(&newns, "/proc/%d/ns/mnt", nsinfo__pid(nsi)) == -1)
+	if (asprintf(&newns, "/proc/%d/task/%d/ns/mnt",
+		     nsinfo__pid(nsi), nsinfo__tid(nsi)) == -1)
 		return rv;
 
 	if (stat(oldns, &old_stat) < 0)
@@ -127,7 +128,8 @@ int nsinfo__init(struct nsinfo *nsi)
 	/* If we're dealing with a process that is in a different PID namespace,
 	 * attempt to work out the innermost tgid for the process.
 	 */
-	if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsinfo__pid(nsi)) >= PATH_MAX)
+	if (snprintf(spath, PATH_MAX, "/proc/%d/task/%d/status",
+		     nsinfo__pid(nsi), nsinfo__tid(nsi)) >= PATH_MAX)
 		goto out;
 
 	rv = nsinfo__get_nspid(&RC_CHK_ACCESS(nsi)->tgid, &RC_CHK_ACCESS(nsi)->nstgid,
@@ -150,11 +152,11 @@ static struct nsinfo *nsinfo__alloc(void)
 	return res;
 }
 
-struct nsinfo *nsinfo__new(pid_t pid)
+struct nsinfo *nsinfo__new_thread(pid_t pid, pid_t tid)
 {
 	struct nsinfo *nsi;
 
-	if (pid == 0)
+	if (pid <= 0 || tid <= 0)
 		return NULL;
 
 	nsi = nsinfo__alloc();
@@ -162,6 +164,7 @@ struct nsinfo *nsinfo__new(pid_t pid)
 		return NULL;
 
 	RC_CHK_ACCESS(nsi)->pid = pid;
+	RC_CHK_ACCESS(nsi)->tid = tid;
 	RC_CHK_ACCESS(nsi)->tgid = pid;
 	RC_CHK_ACCESS(nsi)->nstgid = pid;
 	nsinfo__clear_need_setns(nsi);
@@ -176,6 +179,11 @@ struct nsinfo *nsinfo__new(pid_t pid)
 	return nsi;
 }
 
+struct nsinfo *nsinfo__new(pid_t pid)
+{
+	return nsinfo__new_thread(pid, pid);
+}
+
 static const char *nsinfo__mntns_path(const struct nsinfo *nsi)
 {
 	return RC_CHK_ACCESS(nsi)->mntns_path;
@@ -193,6 +201,7 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
 		return NULL;
 
 	RC_CHK_ACCESS(nnsi)->pid = nsinfo__pid(nsi);
+	RC_CHK_ACCESS(nnsi)->tid = nsinfo__tid(nsi);
 	RC_CHK_ACCESS(nnsi)->tgid = nsinfo__tgid(nsi);
 	RC_CHK_ACCESS(nnsi)->nstgid = nsinfo__nstgid(nsi);
 	RC_CHK_ACCESS(nnsi)->need_setns = nsinfo__need_setns(nsi);
@@ -265,6 +274,11 @@ pid_t nsinfo__pid(const struct nsinfo  *nsi)
 	return RC_CHK_ACCESS(nsi)->pid;
 }
 
+pid_t nsinfo__tid(const struct nsinfo  *nsi)
+{
+	return RC_CHK_ACCESS(nsi)->tid;
+}
+
 bool nsinfo__in_pidns(const struct nsinfo *nsi)
 {
 	return RC_CHK_ACCESS(nsi)->in_pidns;
diff --git a/tools/perf/util/namespaces.h b/tools/perf/util/namespaces.h
index e95c79b80e27c8fc..ea645954944de4be 100644
--- a/tools/perf/util/namespaces.h
+++ b/tools/perf/util/namespaces.h
@@ -32,6 +32,7 @@ void namespaces__free(struct namespaces *namespaces);
 
 DECLARE_RC_STRUCT(nsinfo) {
 	pid_t			pid;
+	pid_t			tid;
 	pid_t			tgid;
 	pid_t			nstgid;
 	bool			need_setns;
@@ -48,6 +49,7 @@ struct nscookie {
 
 int nsinfo__init(struct nsinfo *nsi);
 struct nsinfo *nsinfo__new(pid_t pid);
+struct nsinfo *nsinfo__new_thread(pid_t pid, pid_t tid);
 struct nsinfo *nsinfo__copy(const struct nsinfo *nsi);
 
 struct nsinfo *nsinfo__get(struct nsinfo *nsi);
@@ -58,6 +60,7 @@ void nsinfo__clear_need_setns(struct nsinfo *nsi);
 pid_t nsinfo__tgid(const struct nsinfo  *nsi);
 pid_t nsinfo__nstgid(const struct nsinfo  *nsi);
 pid_t nsinfo__pid(const struct nsinfo  *nsi);
+pid_t nsinfo__tid(const struct nsinfo  *nsi);
 bool nsinfo__in_pidns(const struct nsinfo  *nsi);
 void nsinfo__set_in_pidns(struct nsinfo *nsi);
 
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index af761b1fc5d57649..8d8427b561ddde11 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -71,7 +71,7 @@ struct thread *thread__new(pid_t pid, pid_t tid)
 		list_add(&comm->list, thread__comm_list(thread));
 		refcount_set(thread__refcnt(thread), 1);
 		/* Thread holds first ref to nsdata. */
-		RC_CHK_ACCESS(thread)->nsinfo = nsinfo__new(pid);
+		RC_CHK_ACCESS(thread)->nsinfo = nsinfo__new_thread(pid, tid);
 		srccode_state_init(thread__srccode_state(thread));
 	}
 
-- 
2.56.1.809.ga0b61035b5-goog


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 04/11] perf tools: Add lock to struct nsinfo
  2026-11-23 23:20 [PATCH v3 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
                   ` (2 preceding siblings ...)
  2026-11-23 23:20 ` [PATCH v3 03/11] perf tools: Add nsinfo__new_thread() to track tid Namhyung Kim
@ 2026-11-23 23:20 ` Namhyung Kim
  2026-11-23 23:20 ` [PATCH v3 05/11] perf tools: Defer /proc lookup until nsinfo is used Namhyung Kim
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Namhyung Kim @ 2026-11-23 23:20 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ian Rogers, Jiri Olsa, Adrian Hunter, James Clark,
	Peter Zijlstra, Ingo Molnar, LKML, linux-perf-users

A struct nsinfo instance can be shared across threads via
thread->nsinfo and dso->nsinfo.  Add a mutex lock inside struct nsinfo
to synchronize access to its fields, initialization, copying, and mount
namespace path duplication in nsinfo__mountns_enter().

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/namespaces.c | 135 ++++++++++++++++++++++++++++-------
 tools/perf/util/namespaces.h |  12 ++--
 2 files changed, 115 insertions(+), 32 deletions(-)

diff --git a/tools/perf/util/namespaces.c b/tools/perf/util/namespaces.c
index f426eba04d6ef449..75c2b0ec778ab25b 100644
--- a/tools/perf/util/namespaces.c
+++ b/tools/perf/util/namespaces.c
@@ -94,7 +94,14 @@ static int nsinfo__get_nspid(pid_t *tgid, pid_t *nstgid, bool *in_pidns, const c
 	return 0;
 }
 
-int nsinfo__init(struct nsinfo *nsi)
+static struct mutex *nsinfo__lock(const struct nsinfo *nsi)
+	__returns_ctx_lock(&RC_CHK_ACCESS(nsi)->lock)
+{
+	return (struct mutex *)&RC_CHK_ACCESS(nsi)->lock;
+}
+
+static int nsinfo__init_locked(struct nsinfo *nsi)
+	__must_hold(nsinfo__lock(nsi))
 {
 	char oldns[PATH_MAX];
 	char spath[PATH_MAX];
@@ -140,14 +147,29 @@ int nsinfo__init(struct nsinfo *nsi)
 	return rv;
 }
 
+int nsinfo__init(struct nsinfo *nsi)
+{
+	int rv;
+
+	if (!nsi)
+		return -1;
+
+	mutex_lock(nsinfo__lock(nsi));
+	rv = nsinfo__init_locked(nsi);
+	mutex_unlock(nsinfo__lock(nsi));
+	return rv;
+}
+
 static struct nsinfo *nsinfo__alloc(void)
 {
 	struct nsinfo *res;
 	RC_STRUCT(nsinfo) *nsi;
 
 	nsi = calloc(1, sizeof(*nsi));
-	if (ADD_RC_CHK(res, nsi))
+	if (ADD_RC_CHK(res, nsi)) {
 		refcount_set(&nsi->refcnt, 1);
+		mutex_init(&nsi->lock);
+	}
 
 	return res;
 }
@@ -165,10 +187,12 @@ struct nsinfo *nsinfo__new_thread(pid_t pid, pid_t tid)
 
 	RC_CHK_ACCESS(nsi)->pid = pid;
 	RC_CHK_ACCESS(nsi)->tid = tid;
+	mutex_lock(nsinfo__lock(nsi));
 	RC_CHK_ACCESS(nsi)->tgid = pid;
 	RC_CHK_ACCESS(nsi)->nstgid = pid;
-	nsinfo__clear_need_setns(nsi);
 	RC_CHK_ACCESS(nsi)->in_pidns = false;
+	mutex_unlock(nsinfo__lock(nsi));
+	nsinfo__clear_need_setns(nsi);
 	/* Init may fail if the process exits while we're trying to look at its
 	 * proc information. In that case, save the pid but don't try to enter
 	 * the namespace.
@@ -184,14 +208,12 @@ struct nsinfo *nsinfo__new(pid_t pid)
 	return nsinfo__new_thread(pid, pid);
 }
 
-static const char *nsinfo__mntns_path(const struct nsinfo *nsi)
-{
-	return RC_CHK_ACCESS(nsi)->mntns_path;
-}
-
 struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
 {
 	struct nsinfo *nnsi;
+	pid_t pid, tid, tgid, nstgid;
+	bool need_setns, in_pidns;
+	char *mntns_path = NULL;
 
 	if (nsi == NULL)
 		return NULL;
@@ -200,19 +222,32 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
 	if (!nnsi)
 		return NULL;
 
-	RC_CHK_ACCESS(nnsi)->pid = nsinfo__pid(nsi);
-	RC_CHK_ACCESS(nnsi)->tid = nsinfo__tid(nsi);
-	RC_CHK_ACCESS(nnsi)->tgid = nsinfo__tgid(nsi);
-	RC_CHK_ACCESS(nnsi)->nstgid = nsinfo__nstgid(nsi);
-	RC_CHK_ACCESS(nnsi)->need_setns = nsinfo__need_setns(nsi);
-	RC_CHK_ACCESS(nnsi)->in_pidns = nsinfo__in_pidns(nsi);
-	if (nsinfo__mntns_path(nsi)) {
-		RC_CHK_ACCESS(nnsi)->mntns_path = strdup(nsinfo__mntns_path(nsi));
-		if (!RC_CHK_ACCESS(nnsi)->mntns_path) {
+	mutex_lock(nsinfo__lock(nsi));
+	pid = RC_CHK_ACCESS(nsi)->pid;
+	tid = RC_CHK_ACCESS(nsi)->tid;
+	tgid = RC_CHK_ACCESS(nsi)->tgid;
+	nstgid = RC_CHK_ACCESS(nsi)->nstgid;
+	need_setns = RC_CHK_ACCESS(nsi)->need_setns;
+	in_pidns = RC_CHK_ACCESS(nsi)->in_pidns;
+	if (RC_CHK_ACCESS(nsi)->mntns_path) {
+		mntns_path = strdup(RC_CHK_ACCESS(nsi)->mntns_path);
+		if (!mntns_path) {
+			mutex_unlock(nsinfo__lock(nsi));
 			nsinfo__put(nnsi);
 			return NULL;
 		}
 	}
+	mutex_unlock(nsinfo__lock(nsi));
+
+	RC_CHK_ACCESS(nnsi)->pid = pid;
+	RC_CHK_ACCESS(nnsi)->tid = tid;
+	mutex_lock(nsinfo__lock(nnsi));
+	RC_CHK_ACCESS(nnsi)->tgid = tgid;
+	RC_CHK_ACCESS(nnsi)->nstgid = nstgid;
+	RC_CHK_ACCESS(nnsi)->need_setns = need_setns;
+	RC_CHK_ACCESS(nnsi)->in_pidns = in_pidns;
+	RC_CHK_ACCESS(nnsi)->mntns_path = mntns_path;
+	mutex_unlock(nsinfo__lock(nnsi));
 
 	return nnsi;
 }
@@ -226,7 +261,10 @@ static void nsinfo__delete(struct nsinfo *nsi)
 {
 	if (nsi) {
 		WARN_ONCE(refcount_read(nsinfo__refcnt(nsi)) != 0, "nsinfo refcnt unbalanced\n");
+		mutex_lock(nsinfo__lock(nsi));
 		zfree(&RC_CHK_ACCESS(nsi)->mntns_path);
+		mutex_unlock(nsinfo__lock(nsi));
+		mutex_destroy(&RC_CHK_ACCESS(nsi)->lock);
 		RC_CHK_FREE(nsi);
 	}
 }
@@ -251,22 +289,46 @@ void nsinfo__put(struct nsinfo *nsi)
 
 bool nsinfo__need_setns(const struct nsinfo *nsi)
 {
-	return RC_CHK_ACCESS(nsi)->need_setns;
+	bool ret;
+
+	if (!nsi)
+		return false;
+
+	mutex_lock(nsinfo__lock(nsi));
+	ret = RC_CHK_ACCESS(nsi)->need_setns;
+	mutex_unlock(nsinfo__lock(nsi));
+	return ret;
 }
 
 void nsinfo__clear_need_setns(struct nsinfo *nsi)
 {
+	if (!nsi)
+		return;
+
+	mutex_lock(nsinfo__lock(nsi));
 	RC_CHK_ACCESS(nsi)->need_setns = false;
+	zfree(&RC_CHK_ACCESS(nsi)->mntns_path);
+	mutex_unlock(nsinfo__lock(nsi));
 }
 
-pid_t nsinfo__tgid(const struct nsinfo  *nsi)
+pid_t nsinfo__tgid(const struct nsinfo *nsi)
 {
-	return RC_CHK_ACCESS(nsi)->tgid;
+	pid_t ret;
+
+	mutex_lock(nsinfo__lock(nsi));
+	ret = RC_CHK_ACCESS(nsi)->tgid;
+	mutex_unlock(nsinfo__lock(nsi));
+	return ret;
 }
 
-pid_t nsinfo__nstgid(const struct nsinfo  *nsi)
+pid_t nsinfo__nstgid(const struct nsinfo *nsi)
 {
-	return RC_CHK_ACCESS(nsi)->nstgid;
+	pid_t ret;
+
+	mutex_lock(nsinfo__lock(nsi));
+	ret = RC_CHK_ACCESS(nsi)->nstgid;
+	mutex_unlock(nsinfo__lock(nsi));
+	return ret;
 }
 
 pid_t nsinfo__pid(const struct nsinfo  *nsi)
@@ -281,18 +343,26 @@ pid_t nsinfo__tid(const struct nsinfo  *nsi)
 
 bool nsinfo__in_pidns(const struct nsinfo *nsi)
 {
-	return RC_CHK_ACCESS(nsi)->in_pidns;
+	bool ret;
+
+	mutex_lock(nsinfo__lock(nsi));
+	ret = RC_CHK_ACCESS(nsi)->in_pidns;
+	mutex_unlock(nsinfo__lock(nsi));
+	return ret;
 }
 
 void nsinfo__set_in_pidns(struct nsinfo *nsi)
 {
+	mutex_lock(nsinfo__lock(nsi));
 	RC_CHK_ACCESS(nsi)->in_pidns = true;
+	mutex_unlock(nsinfo__lock(nsi));
 }
 
 void nsinfo__mountns_enter(struct nsinfo *nsi,
 				  struct nscookie *nc)
 {
 	char curpath[PATH_MAX];
+	char *mntns_path = NULL;
 	int oldns = -1;
 	int newns = -1;
 	char *oldcwd = NULL;
@@ -303,27 +373,36 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
 	nc->oldns = -1;
 	nc->newns = -1;
 
-	if (!nsi || !nsinfo__need_setns(nsi))
+	if (!nsi)
 		return;
 
-	if (!getcwd(curpath, sizeof(curpath)))
+	mutex_lock(nsinfo__lock(nsi));
+	if (RC_CHK_ACCESS(nsi)->need_setns && RC_CHK_ACCESS(nsi)->mntns_path)
+		mntns_path = strdup(RC_CHK_ACCESS(nsi)->mntns_path);
+	mutex_unlock(nsinfo__lock(nsi));
+
+	if (!mntns_path)
 		return;
 
+	if (!getcwd(curpath, sizeof(curpath)))
+		goto out;
+
 	oldcwd = strdup(curpath);
 	if (!oldcwd)
-		return;
+		goto out;
 
 	oldns = open("/proc/self/ns/mnt", O_RDONLY);
 	if (oldns < 0)
 		goto errout;
 
-	newns = open(nsinfo__mntns_path(nsi), O_RDONLY);
+	newns = open(mntns_path, O_RDONLY);
 	if (newns < 0)
 		goto errout;
 
 	if (setns(newns, CLONE_NEWNS) < 0)
 		goto errout;
 
+	free(mntns_path);
 	nc->oldcwd = oldcwd;
 	nc->oldns = oldns;
 	nc->newns = newns;
@@ -335,6 +414,8 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
 		close(oldns);
 	if (newns > -1)
 		close(newns);
+out:
+	free(mntns_path);
 }
 
 void nsinfo__mountns_exit(struct nscookie *nc)
diff --git a/tools/perf/util/namespaces.h b/tools/perf/util/namespaces.h
index ea645954944de4be..c76090f54a819284 100644
--- a/tools/perf/util/namespaces.h
+++ b/tools/perf/util/namespaces.h
@@ -14,6 +14,7 @@
 #include <linux/refcount.h>
 #include <linux/types.h>
 #include <internal/rc_check.h>
+#include "mutex.h"
 
 #ifndef HAVE_SETNS_SUPPORT
 int setns(int fd, int nstype);
@@ -31,13 +32,14 @@ struct namespaces *namespaces__new(struct perf_record_namespaces *event);
 void namespaces__free(struct namespaces *namespaces);
 
 DECLARE_RC_STRUCT(nsinfo) {
+	struct mutex		lock;
 	pid_t			pid;
 	pid_t			tid;
-	pid_t			tgid;
-	pid_t			nstgid;
-	bool			need_setns;
-	bool			in_pidns;
-	char			*mntns_path;
+	pid_t			tgid __guarded_by(&lock);
+	pid_t			nstgid __guarded_by(&lock);
+	bool			need_setns __guarded_by(&lock);
+	bool			in_pidns __guarded_by(&lock);
+	char			*mntns_path __guarded_by(&lock);
 	refcount_t		refcnt;
 };
 
-- 
2.56.1.809.ga0b61035b5-goog


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 05/11] perf tools: Defer /proc lookup until nsinfo is used
  2026-11-23 23:20 [PATCH v3 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
                   ` (3 preceding siblings ...)
  2026-11-23 23:20 ` [PATCH v3 04/11] perf tools: Add lock to struct nsinfo Namhyung Kim
@ 2026-11-23 23:20 ` Namhyung Kim
  2026-11-23 23:20 ` [PATCH v3 06/11] perf tools: Verify thread status and mount namespace inode Namhyung Kim
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Namhyung Kim @ 2026-11-23 23:20 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ian Rogers, Jiri Olsa, Adrian Hunter, James Clark,
	Peter Zijlstra, Ingo Molnar, LKML, linux-perf-users

Do not read /proc/<pid>/task/<tid>/ eagerly in nsinfo__new_thread().
Instead, initialize struct nsinfo on demand via
nsinfo__ensure_init_locked() when namespace information is queried or
entered, protected by nsinfo->lock.

Replace thread__nsinfo() with thread__get_nsinfo() and add
thread__reset_nsinfo() to replace thread->nsinfo safely under
thread->namespaces_lock on exec and PERF_RECORD_NAMESPACES events so
namespace changes are picked up without racing existing readers.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/builtin-inject.c  |  2 +-
 tools/perf/util/jitdump.c    |  2 +-
 tools/perf/util/map.c        |  2 +-
 tools/perf/util/namespaces.c | 28 ++++++++++++++++++----------
 tools/perf/util/namespaces.h |  1 +
 tools/perf/util/thread.c     | 34 +++++++++++++++++++++++++++++++++-
 tools/perf/util/thread.h     |  8 +++-----
 7 files changed, 58 insertions(+), 19 deletions(-)

diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index 0e01b14fa51676ab..05bf1aae61778952 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -626,7 +626,7 @@ static struct dso *findnew_dso(int pid, int tid, const char *filename,
 	}
 
 	vdso = is_vdso_map(filename);
-	nsi = nsinfo__get(thread__nsinfo(thread));
+	nsi = thread__get_nsinfo(thread);
 
 	if (vdso) {
 		/* The vdso maps are always on the host and not the
diff --git a/tools/perf/util/jitdump.c b/tools/perf/util/jitdump.c
index 45a05316b3ca65b7..95a08ead1f380747 100644
--- a/tools/perf/util/jitdump.c
+++ b/tools/perf/util/jitdump.c
@@ -985,7 +985,7 @@ jit_process(struct perf_session *session,
 		return 0;
 	}
 
-	nsi = nsinfo__get(thread__nsinfo(thread));
+	nsi = thread__get_nsinfo(thread);
 	thread__put(thread);
 
 	/*
diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c
index 38b116d230d0b913..496aab188bd549ef 100644
--- a/tools/perf/util/map.c
+++ b/tools/perf/util/map.c
@@ -139,7 +139,7 @@ struct map *map__new(struct machine *machine, u64 start, u64 len,
 		anon = is_anon_memory(filename) || flags & MAP_HUGETLB;
 		vdso = is_vdso_map(filename);
 		no_dso = is_no_dso_memory(filename);
-		nsi = nsinfo__get(thread__nsinfo(thread));
+		nsi = thread__get_nsinfo(thread);
 
 		if ((anon || no_dso) && nsi && (prot & PROT_EXEC)) {
 			snprintf(newfilename, sizeof(newfilename),
diff --git a/tools/perf/util/namespaces.c b/tools/perf/util/namespaces.c
index 75c2b0ec778ab25b..940132d3458be4c7 100644
--- a/tools/perf/util/namespaces.c
+++ b/tools/perf/util/namespaces.c
@@ -110,6 +110,8 @@ static int nsinfo__init_locked(struct nsinfo *nsi)
 	struct stat new_stat;
 	int rv = -1;
 
+	RC_CHK_ACCESS(nsi)->inited = true;
+
 	if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
 		return rv;
 
@@ -160,6 +162,13 @@ int nsinfo__init(struct nsinfo *nsi)
 	return rv;
 }
 
+static void nsinfo__ensure_init_locked(struct nsinfo *nsi)
+	__must_hold(nsinfo__lock(nsi))
+{
+	if (!RC_CHK_ACCESS(nsi)->inited)
+		nsinfo__init_locked(nsi);
+}
+
 static struct nsinfo *nsinfo__alloc(void)
 {
 	struct nsinfo *res;
@@ -190,16 +199,7 @@ struct nsinfo *nsinfo__new_thread(pid_t pid, pid_t tid)
 	mutex_lock(nsinfo__lock(nsi));
 	RC_CHK_ACCESS(nsi)->tgid = pid;
 	RC_CHK_ACCESS(nsi)->nstgid = pid;
-	RC_CHK_ACCESS(nsi)->in_pidns = false;
 	mutex_unlock(nsinfo__lock(nsi));
-	nsinfo__clear_need_setns(nsi);
-	/* Init may fail if the process exits while we're trying to look at its
-	 * proc information. In that case, save the pid but don't try to enter
-	 * the namespace.
-	 */
-	if (nsinfo__init(nsi) == -1)
-		nsinfo__clear_need_setns(nsi);
-
 	return nsi;
 }
 
@@ -212,7 +212,7 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
 {
 	struct nsinfo *nnsi;
 	pid_t pid, tid, tgid, nstgid;
-	bool need_setns, in_pidns;
+	bool inited, need_setns, in_pidns;
 	char *mntns_path = NULL;
 
 	if (nsi == NULL)
@@ -227,6 +227,7 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
 	tid = RC_CHK_ACCESS(nsi)->tid;
 	tgid = RC_CHK_ACCESS(nsi)->tgid;
 	nstgid = RC_CHK_ACCESS(nsi)->nstgid;
+	inited = RC_CHK_ACCESS(nsi)->inited;
 	need_setns = RC_CHK_ACCESS(nsi)->need_setns;
 	in_pidns = RC_CHK_ACCESS(nsi)->in_pidns;
 	if (RC_CHK_ACCESS(nsi)->mntns_path) {
@@ -244,6 +245,7 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
 	mutex_lock(nsinfo__lock(nnsi));
 	RC_CHK_ACCESS(nnsi)->tgid = tgid;
 	RC_CHK_ACCESS(nnsi)->nstgid = nstgid;
+	RC_CHK_ACCESS(nnsi)->inited = inited;
 	RC_CHK_ACCESS(nnsi)->need_setns = need_setns;
 	RC_CHK_ACCESS(nnsi)->in_pidns = in_pidns;
 	RC_CHK_ACCESS(nnsi)->mntns_path = mntns_path;
@@ -295,6 +297,7 @@ bool nsinfo__need_setns(const struct nsinfo *nsi)
 		return false;
 
 	mutex_lock(nsinfo__lock(nsi));
+	nsinfo__ensure_init_locked((struct nsinfo *)nsi);
 	ret = RC_CHK_ACCESS(nsi)->need_setns;
 	mutex_unlock(nsinfo__lock(nsi));
 	return ret;
@@ -306,6 +309,7 @@ void nsinfo__clear_need_setns(struct nsinfo *nsi)
 		return;
 
 	mutex_lock(nsinfo__lock(nsi));
+	RC_CHK_ACCESS(nsi)->inited = true;
 	RC_CHK_ACCESS(nsi)->need_setns = false;
 	zfree(&RC_CHK_ACCESS(nsi)->mntns_path);
 	mutex_unlock(nsinfo__lock(nsi));
@@ -316,6 +320,7 @@ pid_t nsinfo__tgid(const struct nsinfo *nsi)
 	pid_t ret;
 
 	mutex_lock(nsinfo__lock(nsi));
+	nsinfo__ensure_init_locked((struct nsinfo *)nsi);
 	ret = RC_CHK_ACCESS(nsi)->tgid;
 	mutex_unlock(nsinfo__lock(nsi));
 	return ret;
@@ -326,6 +331,7 @@ pid_t nsinfo__nstgid(const struct nsinfo *nsi)
 	pid_t ret;
 
 	mutex_lock(nsinfo__lock(nsi));
+	nsinfo__ensure_init_locked((struct nsinfo *)nsi);
 	ret = RC_CHK_ACCESS(nsi)->nstgid;
 	mutex_unlock(nsinfo__lock(nsi));
 	return ret;
@@ -346,6 +352,7 @@ bool nsinfo__in_pidns(const struct nsinfo *nsi)
 	bool ret;
 
 	mutex_lock(nsinfo__lock(nsi));
+	nsinfo__ensure_init_locked((struct nsinfo *)nsi);
 	ret = RC_CHK_ACCESS(nsi)->in_pidns;
 	mutex_unlock(nsinfo__lock(nsi));
 	return ret;
@@ -377,6 +384,7 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
 		return;
 
 	mutex_lock(nsinfo__lock(nsi));
+	nsinfo__ensure_init_locked(nsi);
 	if (RC_CHK_ACCESS(nsi)->need_setns && RC_CHK_ACCESS(nsi)->mntns_path)
 		mntns_path = strdup(RC_CHK_ACCESS(nsi)->mntns_path);
 	mutex_unlock(nsinfo__lock(nsi));
diff --git a/tools/perf/util/namespaces.h b/tools/perf/util/namespaces.h
index c76090f54a819284..4e30b015db40caf5 100644
--- a/tools/perf/util/namespaces.h
+++ b/tools/perf/util/namespaces.h
@@ -37,6 +37,7 @@ DECLARE_RC_STRUCT(nsinfo) {
 	pid_t			tid;
 	pid_t			tgid __guarded_by(&lock);
 	pid_t			nstgid __guarded_by(&lock);
+	bool			inited __guarded_by(&lock);
 	bool			need_setns __guarded_by(&lock);
 	bool			in_pidns __guarded_by(&lock);
 	char			*mntns_path __guarded_by(&lock);
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index 8d8427b561ddde11..c03c739e47191d1e 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -108,6 +108,7 @@ void thread__delete(struct thread *thread)
 		list_del_init(&namespaces->list);
 		namespaces__free(namespaces);
 	}
+	nsinfo__zput(RC_CHK_ACCESS(thread)->nsinfo);
 	up_write(thread__namespaces_lock(thread));
 
 	down_write(thread__comm_lock(thread));
@@ -117,7 +118,6 @@ void thread__delete(struct thread *thread)
 	}
 	up_write(thread__comm_lock(thread));
 
-	nsinfo__zput(RC_CHK_ACCESS(thread)->nsinfo);
 	srccode_state_free(thread__srccode_state(thread));
 
 	exit_rwsem(thread__namespaces_lock(thread));
@@ -193,6 +193,34 @@ static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
 	return 0;
 }
 
+static void __thread__reset_nsinfo(struct thread *thread)
+	__must_hold(thread__namespaces_lock(thread))
+{
+	nsinfo__zput(RC_CHK_ACCESS(thread)->nsinfo);
+	RC_CHK_ACCESS(thread)->nsinfo = nsinfo__new_thread(thread__pid(thread),
+							   thread__tid(thread));
+}
+
+void thread__reset_nsinfo(struct thread *thread)
+{
+	down_write(thread__namespaces_lock(thread));
+	__thread__reset_nsinfo(thread);
+	up_write(thread__namespaces_lock(thread));
+}
+
+struct nsinfo *thread__get_nsinfo(struct thread *thread)
+{
+	struct nsinfo *nsi;
+
+	if (!thread)
+		return NULL;
+
+	down_read(thread__namespaces_lock(thread));
+	nsi = nsinfo__get(RC_CHK_ACCESS(thread)->nsinfo);
+	up_read(thread__namespaces_lock(thread));
+	return nsi;
+}
+
 int thread__set_namespaces(struct thread *thread, u64 timestamp,
 			   struct perf_record_namespaces *event)
 {
@@ -200,6 +228,8 @@ int thread__set_namespaces(struct thread *thread, u64 timestamp,
 
 	down_write(thread__namespaces_lock(thread));
 	ret = __thread__set_namespaces(thread, timestamp, event);
+	if (!ret && event)
+		__thread__reset_nsinfo(thread);
 	up_write(thread__namespaces_lock(thread));
 	return ret;
 }
@@ -284,6 +314,8 @@ int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
 	down_write(thread__comm_lock(thread));
 	ret = ____thread__set_comm(thread, str, timestamp, exec);
 	up_write(thread__comm_lock(thread));
+	if (!ret && exec)
+		thread__reset_nsinfo(thread);
 	return ret;
 }
 
diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h
index a7850eebce4eb83e..68d63f9c37cc4240 100644
--- a/tools/perf/util/thread.h
+++ b/tools/perf/util/thread.h
@@ -56,7 +56,7 @@ DECLARE_RC_STRUCT(thread) {
 
 	void			*priv;
 	struct thread_stack	*ts;
-	struct nsinfo		*nsinfo;
+	struct nsinfo		*nsinfo __guarded_by(&namespaces_lock);
 	struct srccode_state	srccode_state;
 	bool			filter;
 	int			filter_entry_depth;
@@ -292,10 +292,8 @@ static inline void thread__set_ts(struct thread *thread, struct thread_stack *ts
 	RC_CHK_ACCESS(thread)->ts = ts;
 }
 
-static inline struct nsinfo *thread__nsinfo(struct thread *thread)
-{
-	return RC_CHK_ACCESS(thread)->nsinfo;
-}
+struct nsinfo *thread__get_nsinfo(struct thread *thread);
+void thread__reset_nsinfo(struct thread *thread);
 
 static inline struct srccode_state *thread__srccode_state(struct thread *thread)
 {
-- 
2.56.1.809.ga0b61035b5-goog


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 06/11] perf tools: Verify thread status and mount namespace inode
  2026-11-23 23:20 [PATCH v3 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
                   ` (4 preceding siblings ...)
  2026-11-23 23:20 ` [PATCH v3 05/11] perf tools: Defer /proc lookup until nsinfo is used Namhyung Kim
@ 2026-11-23 23:20 ` Namhyung Kim
  2026-11-23 23:20 ` [PATCH v3 07/11] perf tools: Check thread liveness when accessing /proc Namhyung Kim
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Namhyung Kim @ 2026-11-23 23:20 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ian Rogers, Jiri Olsa, Adrian Hunter, James Clark,
	Peter Zijlstra, Ingo Molnar, LKML, linux-perf-users

Verify /proc/<pid>/task/<tid>/status exists and has Tgid == pid before
reading or entering a thread's namespaces to guard against PID/TID
recycling.  Pin a reference-counted host /proc file descriptor across
nsinfo__mountns_enter()/exit() and access relative /proc paths through
nsinfo__proc_open(), nsinfo__proc_stat(), and nsinfo__proc_readlink()
using stack buffers instead of heap-allocating mntns_path or keeping a
permanent /proc descriptor open.

Also record the mount namespace inode (mntns_ino) during initialization,
mark nsinfo exited if stating ns/mnt fails with ENOENT or ESRCH, and
verify via fstat() that the opened namespace file descriptor still
refers to the same mount namespace before calling setns().

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/namespaces.c | 330 +++++++++++++++++++++++++++--------
 tools/perf/util/namespaces.h |   9 +-
 2 files changed, 267 insertions(+), 72 deletions(-)

diff --git a/tools/perf/util/namespaces.c b/tools/perf/util/namespaces.c
index 940132d3458be4c7..704f69663129d113 100644
--- a/tools/perf/util/namespaces.c
+++ b/tools/perf/util/namespaces.c
@@ -6,6 +6,7 @@
 
 #include "namespaces.h"
 #include "event.h"
+#include <errno.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -59,22 +60,102 @@ void namespaces__free(struct namespaces *namespaces)
 	free(namespaces);
 }
 
-static int nsinfo__get_nspid(pid_t *tgid, pid_t *nstgid, bool *in_pidns, const char *path)
+static DEFINE_MUTEX(host_proc_lock);
+static int host_proc_fd __guarded_by(&host_proc_lock) = -1;
+static unsigned int host_proc_refcnt __guarded_by(&host_proc_lock);
+
+static void nsinfo__host_proc_get(void)
+{
+	mutex_lock(&host_proc_lock);
+	if (host_proc_refcnt++ == 0)
+		host_proc_fd = open("/proc", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+	mutex_unlock(&host_proc_lock);
+}
+
+static void nsinfo__host_proc_put(void)
+{
+	mutex_lock(&host_proc_lock);
+	if (host_proc_refcnt > 0 && --host_proc_refcnt == 0 &&
+	    host_proc_fd >= 0) {
+		close(host_proc_fd);
+		host_proc_fd = -1;
+	}
+	mutex_unlock(&host_proc_lock);
+}
+
+static int nsinfo__proc_open(const char *rel_path, int flags)
+{
+	char path[64];
+	int ret;
+
+	mutex_lock(&host_proc_lock);
+	if (host_proc_fd >= 0) {
+		ret = openat(host_proc_fd, rel_path, flags);
+	} else {
+		scnprintf(path, sizeof(path), "/proc/%s", rel_path);
+		ret = open(path, flags);
+	}
+	mutex_unlock(&host_proc_lock);
+	return ret;
+}
+
+static int nsinfo__proc_stat(const char *rel_path, struct stat *st)
+{
+	char path[64];
+	int ret;
+
+	mutex_lock(&host_proc_lock);
+	if (host_proc_fd >= 0) {
+		ret = fstatat(host_proc_fd, rel_path, st, 0);
+	} else {
+		scnprintf(path, sizeof(path), "/proc/%s", rel_path);
+		ret = stat(path, st);
+	}
+	mutex_unlock(&host_proc_lock);
+	return ret;
+}
+
+ssize_t nsinfo__proc_readlink(const char *rel_path, char *buf, size_t size)
+{
+	char path[64];
+	ssize_t ret;
+
+	mutex_lock(&host_proc_lock);
+	if (host_proc_fd >= 0) {
+		ret = readlinkat(host_proc_fd, rel_path, buf, size);
+	} else {
+		scnprintf(path, sizeof(path), "/proc/%s", rel_path);
+		ret = readlink(path, buf, size);
+	}
+	mutex_unlock(&host_proc_lock);
+	return ret;
+}
+
+static int nsinfo__get_nspid(pid_t *tgid, pid_t *nstgid, bool *in_pidns,
+			     const char *rel_path)
 {
 	FILE *f = NULL;
 	char *statln = NULL;
 	size_t linesz = 0;
 	char *nspid;
+	int fd;
+	bool got_tgid = false;
 
-	f = fopen(path, "r");
-	if (f == NULL)
+	fd = nsinfo__proc_open(rel_path, O_RDONLY | O_CLOEXEC);
+	if (fd < 0)
 		return -1;
+	f = fdopen(fd, "r");
+	if (f == NULL) {
+		close(fd);
+		return -1;
+	}
 
 	while (getline(&statln, &linesz, f) != -1) {
 		/* Use tgid if CONFIG_PID_NS is not defined. */
 		if (strstr(statln, "Tgid:") != NULL) {
 			*tgid = (pid_t)strtol(strrchr(statln, '\t'), NULL, 10);
 			*nstgid = *tgid;
+			got_tgid = true;
 		}
 
 		if (strstr(statln, "NStgid:") != NULL) {
@@ -91,7 +172,17 @@ static int nsinfo__get_nspid(pid_t *tgid, pid_t *nstgid, bool *in_pidns, const c
 
 	fclose(f);
 	free(statln);
-	return 0;
+	return got_tgid ? 0 : -1;
+}
+
+static int nsinfo__get_task_nspid(const struct nsinfo *nsi,
+				  pid_t *tgid, pid_t *nstgid, bool *in_pidns)
+{
+	char spath[64];
+
+	scnprintf(spath, sizeof(spath), "%d/task/%d/status",
+		  nsinfo__pid(nsi), nsinfo__tid(nsi));
+	return nsinfo__get_nspid(tgid, nstgid, in_pidns, spath);
 }
 
 static struct mutex *nsinfo__lock(const struct nsinfo *nsi)
@@ -100,53 +191,69 @@ static struct mutex *nsinfo__lock(const struct nsinfo *nsi)
 	return (struct mutex *)&RC_CHK_ACCESS(nsi)->lock;
 }
 
+static void nsinfo__clear_need_setns_locked(struct nsinfo *nsi)
+	__must_hold(nsinfo__lock(nsi))
+{
+	RC_CHK_ACCESS(nsi)->inited = true;
+	RC_CHK_ACCESS(nsi)->need_setns = false;
+}
+
+static void nsinfo__set_exited_locked(struct nsinfo *nsi)
+	__must_hold(nsinfo__lock(nsi))
+{
+	RC_CHK_ACCESS(nsi)->exited = true;
+	nsinfo__clear_need_setns_locked(nsi);
+}
+
 static int nsinfo__init_locked(struct nsinfo *nsi)
 	__must_hold(nsinfo__lock(nsi))
 {
-	char oldns[PATH_MAX];
-	char spath[PATH_MAX];
-	char *newns = NULL;
+	char newns[64];
 	struct stat old_stat;
 	struct stat new_stat;
-	int rv = -1;
+	pid_t tgid = 0, nstgid = 0;
+	bool in_pidns = false;
+
+	if (RC_CHK_ACCESS(nsi)->exited)
+		return -1;
 
 	RC_CHK_ACCESS(nsi)->inited = true;
 
-	if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
-		return rv;
+	/*
+	 * Verify that the thread (pid, tid) still exists in /proc and its Tgid
+	 * matches pid (guarding against PID/TID recycling), while resolving the
+	 * innermost tgid if the process is in a different PID namespace.
+	 */
+	if (nsinfo__get_task_nspid(nsi, &tgid, &nstgid, &in_pidns) < 0 ||
+	    tgid != nsinfo__pid(nsi)) {
+		nsinfo__set_exited_locked(nsi);
+		return -1;
+	}
 
-	if (asprintf(&newns, "/proc/%d/task/%d/ns/mnt",
-		     nsinfo__pid(nsi), nsinfo__tid(nsi)) == -1)
-		return rv;
+	RC_CHK_ACCESS(nsi)->tgid = tgid;
+	RC_CHK_ACCESS(nsi)->nstgid = nstgid;
+	RC_CHK_ACCESS(nsi)->in_pidns = in_pidns;
 
-	if (stat(oldns, &old_stat) < 0)
-		goto out;
+	if (nsinfo__proc_stat("self/ns/mnt", &old_stat) < 0)
+		return -1;
 
-	if (stat(newns, &new_stat) < 0)
-		goto out;
+	scnprintf(newns, sizeof(newns), "%d/task/%d/ns/mnt",
+		  nsinfo__pid(nsi), nsinfo__tid(nsi));
+	if (nsinfo__proc_stat(newns, &new_stat) < 0) {
+		if (errno == ENOENT || errno == ESRCH)
+			nsinfo__set_exited_locked(nsi);
+		return -1;
+	}
 
 	/* Check if the mount namespaces differ, if so then indicate that we
 	 * want to switch as part of looking up dso/map data.
 	 */
 	if (old_stat.st_ino != new_stat.st_ino) {
 		RC_CHK_ACCESS(nsi)->need_setns = true;
-		RC_CHK_ACCESS(nsi)->mntns_path = newns;
-		newns = NULL;
+		RC_CHK_ACCESS(nsi)->mntns_ino = new_stat.st_ino;
 	}
 
-	/* If we're dealing with a process that is in a different PID namespace,
-	 * attempt to work out the innermost tgid for the process.
-	 */
-	if (snprintf(spath, PATH_MAX, "/proc/%d/task/%d/status",
-		     nsinfo__pid(nsi), nsinfo__tid(nsi)) >= PATH_MAX)
-		goto out;
-
-	rv = nsinfo__get_nspid(&RC_CHK_ACCESS(nsi)->tgid, &RC_CHK_ACCESS(nsi)->nstgid,
-			       &RC_CHK_ACCESS(nsi)->in_pidns, spath);
-
-out:
-	free(newns);
-	return rv;
+	return 0;
 }
 
 int nsinfo__init(struct nsinfo *nsi)
@@ -165,7 +272,7 @@ int nsinfo__init(struct nsinfo *nsi)
 static void nsinfo__ensure_init_locked(struct nsinfo *nsi)
 	__must_hold(nsinfo__lock(nsi))
 {
-	if (!RC_CHK_ACCESS(nsi)->inited)
+	if (!RC_CHK_ACCESS(nsi)->inited && !RC_CHK_ACCESS(nsi)->exited)
 		nsinfo__init_locked(nsi);
 }
 
@@ -212,8 +319,8 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
 {
 	struct nsinfo *nnsi;
 	pid_t pid, tid, tgid, nstgid;
-	bool inited, need_setns, in_pidns;
-	char *mntns_path = NULL;
+	ino_t mntns_ino;
+	bool inited, exited, need_setns, in_pidns;
 
 	if (nsi == NULL)
 		return NULL;
@@ -227,17 +334,11 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
 	tid = RC_CHK_ACCESS(nsi)->tid;
 	tgid = RC_CHK_ACCESS(nsi)->tgid;
 	nstgid = RC_CHK_ACCESS(nsi)->nstgid;
+	mntns_ino = RC_CHK_ACCESS(nsi)->mntns_ino;
 	inited = RC_CHK_ACCESS(nsi)->inited;
+	exited = RC_CHK_ACCESS(nsi)->exited;
 	need_setns = RC_CHK_ACCESS(nsi)->need_setns;
 	in_pidns = RC_CHK_ACCESS(nsi)->in_pidns;
-	if (RC_CHK_ACCESS(nsi)->mntns_path) {
-		mntns_path = strdup(RC_CHK_ACCESS(nsi)->mntns_path);
-		if (!mntns_path) {
-			mutex_unlock(nsinfo__lock(nsi));
-			nsinfo__put(nnsi);
-			return NULL;
-		}
-	}
 	mutex_unlock(nsinfo__lock(nsi));
 
 	RC_CHK_ACCESS(nnsi)->pid = pid;
@@ -245,10 +346,11 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
 	mutex_lock(nsinfo__lock(nnsi));
 	RC_CHK_ACCESS(nnsi)->tgid = tgid;
 	RC_CHK_ACCESS(nnsi)->nstgid = nstgid;
+	RC_CHK_ACCESS(nnsi)->mntns_ino = mntns_ino;
 	RC_CHK_ACCESS(nnsi)->inited = inited;
+	RC_CHK_ACCESS(nnsi)->exited = exited;
 	RC_CHK_ACCESS(nnsi)->need_setns = need_setns;
 	RC_CHK_ACCESS(nnsi)->in_pidns = in_pidns;
-	RC_CHK_ACCESS(nnsi)->mntns_path = mntns_path;
 	mutex_unlock(nsinfo__lock(nnsi));
 
 	return nnsi;
@@ -263,9 +365,6 @@ static void nsinfo__delete(struct nsinfo *nsi)
 {
 	if (nsi) {
 		WARN_ONCE(refcount_read(nsinfo__refcnt(nsi)) != 0, "nsinfo refcnt unbalanced\n");
-		mutex_lock(nsinfo__lock(nsi));
-		zfree(&RC_CHK_ACCESS(nsi)->mntns_path);
-		mutex_unlock(nsinfo__lock(nsi));
 		mutex_destroy(&RC_CHK_ACCESS(nsi)->lock);
 		RC_CHK_FREE(nsi);
 	}
@@ -289,6 +388,46 @@ void nsinfo__put(struct nsinfo *nsi)
 		RC_CHK_PUT(nsi);
 }
 
+bool nsinfo__inited(const struct nsinfo *nsi)
+{
+	bool ret;
+
+	if (!nsi)
+		return false;
+
+	mutex_lock(nsinfo__lock(nsi));
+	ret = RC_CHK_ACCESS(nsi)->inited;
+	mutex_unlock(nsinfo__lock(nsi));
+	return ret;
+}
+
+bool nsinfo__exited(const struct nsinfo *nsi)
+{
+	bool ret;
+
+	if (!nsi)
+		return false;
+
+	mutex_lock(nsinfo__lock(nsi));
+	ret = RC_CHK_ACCESS(nsi)->exited;
+	mutex_unlock(nsinfo__lock(nsi));
+	return ret;
+}
+
+ino_t nsinfo__mntns_ino(const struct nsinfo *nsi)
+{
+	ino_t ret;
+
+	if (!nsi)
+		return 0;
+
+	mutex_lock(nsinfo__lock(nsi));
+	nsinfo__ensure_init_locked((struct nsinfo *)nsi);
+	ret = RC_CHK_ACCESS(nsi)->mntns_ino;
+	mutex_unlock(nsinfo__lock(nsi));
+	return ret;
+}
+
 bool nsinfo__need_setns(const struct nsinfo *nsi)
 {
 	bool ret;
@@ -298,7 +437,7 @@ bool nsinfo__need_setns(const struct nsinfo *nsi)
 
 	mutex_lock(nsinfo__lock(nsi));
 	nsinfo__ensure_init_locked((struct nsinfo *)nsi);
-	ret = RC_CHK_ACCESS(nsi)->need_setns;
+	ret = !RC_CHK_ACCESS(nsi)->exited && RC_CHK_ACCESS(nsi)->need_setns;
 	mutex_unlock(nsinfo__lock(nsi));
 	return ret;
 }
@@ -309,12 +448,44 @@ void nsinfo__clear_need_setns(struct nsinfo *nsi)
 		return;
 
 	mutex_lock(nsinfo__lock(nsi));
-	RC_CHK_ACCESS(nsi)->inited = true;
-	RC_CHK_ACCESS(nsi)->need_setns = false;
-	zfree(&RC_CHK_ACCESS(nsi)->mntns_path);
+	nsinfo__clear_need_setns_locked(nsi);
 	mutex_unlock(nsinfo__lock(nsi));
 }
 
+void nsinfo__set_exited(struct nsinfo *nsi)
+{
+	if (!nsi)
+		return;
+
+	mutex_lock(nsinfo__lock(nsi));
+	nsinfo__set_exited_locked(nsi);
+	mutex_unlock(nsinfo__lock(nsi));
+}
+
+bool nsinfo__check_proc(struct nsinfo *nsi)
+{
+	pid_t tgid = 0, nstgid = 0;
+	bool in_pidns = false;
+	bool exited;
+
+	if (!nsi)
+		return false;
+
+	mutex_lock(nsinfo__lock(nsi));
+	exited = RC_CHK_ACCESS(nsi)->exited;
+	mutex_unlock(nsinfo__lock(nsi));
+	if (exited)
+		return false;
+
+	if (nsinfo__get_task_nspid(nsi, &tgid, &nstgid, &in_pidns) < 0 ||
+	    tgid != nsinfo__pid(nsi)) {
+		nsinfo__set_exited(nsi);
+		return false;
+	}
+
+	return true;
+}
+
 pid_t nsinfo__tgid(const struct nsinfo *nsi)
 {
 	pid_t ret;
@@ -369,7 +540,10 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
 				  struct nscookie *nc)
 {
 	char curpath[PATH_MAX];
-	char *mntns_path = NULL;
+	char mntns_path[64];
+	ino_t mntns_ino = 0;
+	bool need_setns = false;
+	struct stat st;
 	int oldns = -1;
 	int newns = -1;
 	char *oldcwd = NULL;
@@ -379,66 +553,74 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
 
 	nc->oldns = -1;
 	nc->newns = -1;
+	nc->oldcwd = NULL;
 
 	if (!nsi)
 		return;
 
 	mutex_lock(nsinfo__lock(nsi));
 	nsinfo__ensure_init_locked(nsi);
-	if (RC_CHK_ACCESS(nsi)->need_setns && RC_CHK_ACCESS(nsi)->mntns_path)
-		mntns_path = strdup(RC_CHK_ACCESS(nsi)->mntns_path);
+	if (!RC_CHK_ACCESS(nsi)->exited && RC_CHK_ACCESS(nsi)->need_setns) {
+		need_setns = true;
+		mntns_ino = RC_CHK_ACCESS(nsi)->mntns_ino;
+	}
 	mutex_unlock(nsinfo__lock(nsi));
 
-	if (!mntns_path)
+	if (!need_setns)
+		return;
+
+	if (!nsinfo__check_proc(nsi))
 		return;
 
 	if (!getcwd(curpath, sizeof(curpath)))
-		goto out;
+		return;
 
 	oldcwd = strdup(curpath);
 	if (!oldcwd)
-		goto out;
+		return;
+
+	/* Pin host /proc before entering any target mount namespace. */
+	nsinfo__host_proc_get();
 
-	oldns = open("/proc/self/ns/mnt", O_RDONLY);
+	oldns = nsinfo__proc_open("self/ns/mnt", O_RDONLY | O_CLOEXEC);
 	if (oldns < 0)
 		goto errout;
 
-	newns = open(mntns_path, O_RDONLY);
+	scnprintf(mntns_path, sizeof(mntns_path), "%d/task/%d/ns/mnt",
+		  nsinfo__pid(nsi), nsinfo__tid(nsi));
+	newns = nsinfo__proc_open(mntns_path, O_RDONLY | O_CLOEXEC);
 	if (newns < 0)
 		goto errout;
 
+	if (fstat(newns, &st) < 0 || st.st_ino != mntns_ino) {
+		nsinfo__clear_need_setns(nsi);
+		goto errout;
+	}
+
 	if (setns(newns, CLONE_NEWNS) < 0)
 		goto errout;
 
-	free(mntns_path);
 	nc->oldcwd = oldcwd;
 	nc->oldns = oldns;
 	nc->newns = newns;
 	return;
 
 errout:
+	nsinfo__host_proc_put();
 	free(oldcwd);
 	if (oldns > -1)
 		close(oldns);
 	if (newns > -1)
 		close(newns);
-out:
-	free(mntns_path);
 }
 
 void nsinfo__mountns_exit(struct nscookie *nc)
 {
-	if (nc == NULL || nc->oldns == -1 || nc->newns == -1 || !nc->oldcwd)
+	if (nc == NULL || !nc->oldcwd)
 		return;
 
-	setns(nc->oldns, CLONE_NEWNS);
-
-	if (nc->oldcwd) {
-		WARN_ON_ONCE(chdir(nc->oldcwd));
-		zfree(&nc->oldcwd);
-	}
-
 	if (nc->oldns > -1) {
+		setns(nc->oldns, CLONE_NEWNS);
 		close(nc->oldns);
 		nc->oldns = -1;
 	}
@@ -447,6 +629,12 @@ void nsinfo__mountns_exit(struct nscookie *nc)
 		close(nc->newns);
 		nc->newns = -1;
 	}
+
+	if (nc->oldcwd) {
+		WARN_ON_ONCE(chdir(nc->oldcwd));
+		zfree(&nc->oldcwd);
+	}
+	nsinfo__host_proc_put();
 }
 
 char *nsinfo__realpath(const char *path, struct nsinfo *nsi)
@@ -478,6 +666,6 @@ bool nsinfo__is_in_root_namespace(void)
 	pid_t tgid = 0, nstgid = 0;
 	bool in_pidns = false;
 
-	nsinfo__get_nspid(&tgid, &nstgid, &in_pidns, "/proc/self/status");
+	nsinfo__get_nspid(&tgid, &nstgid, &in_pidns, "self/status");
 	return !in_pidns;
 }
diff --git a/tools/perf/util/namespaces.h b/tools/perf/util/namespaces.h
index 4e30b015db40caf5..441cab597ae45e80 100644
--- a/tools/perf/util/namespaces.h
+++ b/tools/perf/util/namespaces.h
@@ -37,10 +37,11 @@ DECLARE_RC_STRUCT(nsinfo) {
 	pid_t			tid;
 	pid_t			tgid __guarded_by(&lock);
 	pid_t			nstgid __guarded_by(&lock);
+	ino_t			mntns_ino __guarded_by(&lock);
 	bool			inited __guarded_by(&lock);
+	bool			exited __guarded_by(&lock);
 	bool			need_setns __guarded_by(&lock);
 	bool			in_pidns __guarded_by(&lock);
-	char			*mntns_path __guarded_by(&lock);
 	refcount_t		refcnt;
 };
 
@@ -58,8 +59,14 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi);
 struct nsinfo *nsinfo__get(struct nsinfo *nsi);
 void nsinfo__put(struct nsinfo *nsi);
 
+bool nsinfo__inited(const struct nsinfo *nsi);
+bool nsinfo__exited(const struct nsinfo *nsi);
+ino_t nsinfo__mntns_ino(const struct nsinfo *nsi);
 bool nsinfo__need_setns(const struct nsinfo *nsi);
 void nsinfo__clear_need_setns(struct nsinfo *nsi);
+void nsinfo__set_exited(struct nsinfo *nsi);
+bool nsinfo__check_proc(struct nsinfo *nsi);
+ssize_t nsinfo__proc_readlink(const char *rel_path, char *buf, size_t size);
 pid_t nsinfo__tgid(const struct nsinfo  *nsi);
 pid_t nsinfo__nstgid(const struct nsinfo  *nsi);
 pid_t nsinfo__pid(const struct nsinfo  *nsi);
-- 
2.56.1.809.ga0b61035b5-goog


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 07/11] perf tools: Check thread liveness when accessing /proc
  2026-11-23 23:20 [PATCH v3 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
                   ` (5 preceding siblings ...)
  2026-11-23 23:20 ` [PATCH v3 06/11] perf tools: Verify thread status and mount namespace inode Namhyung Kim
@ 2026-11-23 23:20 ` Namhyung Kim
  2026-11-23 23:20 ` [PATCH v3 08/11] perf tools: Export filename__read_build_id_ns() Namhyung Kim
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Namhyung Kim @ 2026-11-23 23:20 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ian Rogers, Jiri Olsa, Adrian Hunter, James Clark,
	Peter Zijlstra, Ingo Molnar, LKML, linux-perf-users

Add thread__exited() and thread__is_alive(), and mark threads as exited
on PERF_RECORD_EXIT, replaced parent and child threads on
PERF_RECORD_FORK, and perf_event__exit_del_thread().

Propagate thread exit and boot ID mismatch to thread->nsinfo, guard
/proc accesses with thread__is_alive() and nsinfo__check_proc() (for
dso__filename_with_chroot(), while skipping chroot fallback when
nsinfo__need_setns() is set), read /proc/<pid>/root via
nsinfo__proc_readlink() in filename_with_chroot(), and avoid
synthesizing a live parent thread in thread__e_machine_endian() when a
sub-thread is no longer alive.

Also process PERF_RECORD_HEADER_FEATURE events in perf-inject so
pipe-mode sessions populate session->header.env (including boot_id) for
perf_env__is_same_boot().

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/builtin-inject.c | 14 +++++++-
 tools/perf/builtin-trace.c  |  2 +-
 tools/perf/util/dso.c       |  7 +++-
 tools/perf/util/event.c     |  1 +
 tools/perf/util/machine.c   | 17 +++++++---
 tools/perf/util/thread.c    | 65 ++++++++++++++++++++++++++++++++++---
 tools/perf/util/thread.h    |  7 ++--
 tools/perf/util/util.c      |  7 ++--
 8 files changed, 103 insertions(+), 17 deletions(-)

diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index 05bf1aae61778952..de3a74d2bab9220f 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -208,6 +208,18 @@ static int perf_event__repipe_op2_synth(const struct perf_tool *tool,
 	return perf_event__repipe_synth(tool, event);
 }
 
+static int perf_event__repipe_feature(const struct perf_tool *tool,
+				      struct perf_session *session,
+				      union perf_event *event)
+{
+	int ret = perf_event__process_feature(tool, session, event);
+
+	if (ret)
+		return ret;
+
+	return perf_event__repipe_synth(tool, event);
+}
+
 static int perf_event__repipe_op4_synth(const struct perf_tool *tool,
 					struct perf_session *session __maybe_unused,
 					union perf_event *event,
@@ -2853,7 +2865,7 @@ int cmd_inject(int argc, const char **argv)
 	inject.tool.stat_config		= perf_event__repipe_op2_synth;
 	inject.tool.stat		= perf_event__repipe_op2_synth;
 	inject.tool.stat_round		= perf_event__repipe_op2_synth;
-	inject.tool.feature		= perf_event__repipe_op2_synth;
+	inject.tool.feature		= perf_event__repipe_feature;
 	inject.tool.finished_init	= perf_event__repipe_op2_synth;
 	inject.tool.compressed		= perf_event__repipe_op4_synth;
 	inject.tool.auxtrace		= perf_event__repipe_auxtrace;
diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
index c08f2c3a8128bc91..101511c79c639fb3 100644
--- a/tools/perf/builtin-trace.c
+++ b/tools/perf/builtin-trace.c
@@ -1858,7 +1858,7 @@ static const char *thread__fd_path(struct thread *thread, int fd,
 		return NULL;
 
 	if ((fd > ttrace->files.max || ttrace->files.table[fd].pathname == NULL)) {
-		if (!trace->live)
+		if (!trace->live || !thread__is_alive(thread))
 			return NULL;
 		++trace->stats.proc_getname;
 		if (thread__read_fd_path(thread, fd))
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index 287f6812139c59ab..dafd5acd6be41908 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -567,7 +567,12 @@ static int do_open(char *name) __must_hold(&dso__data_open_lock)
 
 char *dso__filename_with_chroot(const struct dso *dso, const char *filename)
 {
-	return filename_with_chroot(nsinfo__pid(dso__nsinfo_const(dso)), filename);
+	struct nsinfo *nsi = (struct nsinfo *)dso__nsinfo_const(dso);
+
+	if (nsinfo__need_setns(nsi) || !nsinfo__check_proc(nsi))
+		return NULL;
+
+	return filename_with_chroot(nsinfo__pid(nsi), filename);
 }
 
 char *dso__get_filename(struct dso *dso, const char *root_dir, bool *decomp,
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index 2f80e364659fea71..ae0ebda42fa47e8c 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -447,6 +447,7 @@ int perf_event__exit_del_thread(const struct perf_tool *tool __maybe_unused,
 		    event->fork.ppid, event->fork.ptid);
 
 	if (thread) {
+		thread__set_exited(thread, /*exited=*/true);
 		machine__remove_thread(machine, thread);
 		thread__put(thread);
 	}
diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index 62f3ee8488548ef6..2750d507aecc29d0 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -518,8 +518,14 @@ static void machine__update_thread_pid(struct machine *machine,
 		return;
 
 	thread__set_pid(th, pid);
-	nsinfo__zput(RC_CHK_ACCESS(th)->nsinfo);
-	RC_CHK_ACCESS(th)->nsinfo = nsinfo__new_thread(pid, thread__tid(th));
+	thread__reset_nsinfo(th);
+	if (thread__exited(th) || !machine__is_host(machine) ||
+	    !perf_env__is_same_boot(machine->env)) {
+		struct nsinfo *nsi = thread__get_nsinfo(th);
+
+		nsinfo__set_exited(nsi);
+		nsinfo__put(nsi);
+	}
 
 	if (thread__pid(th) == thread__tid(th))
 		return;
@@ -1978,6 +1984,7 @@ int machine__process_fork_event(struct machine *machine, union perf_event *event
 	    thread__pid(parent) != (pid_t)event->fork.ppid) {
 		dump_printf("removing erroneous parent thread %d/%d\n",
 			    thread__pid(parent), thread__tid(parent));
+		thread__set_exited(parent, /*exited=*/true);
 		machine__remove_thread(machine, parent);
 		thread__put(parent);
 		parent = machine__findnew_thread(machine, event->fork.ppid,
@@ -1986,6 +1993,7 @@ int machine__process_fork_event(struct machine *machine, union perf_event *event
 
 	/* if a thread currently exists for the thread id remove it */
 	if (thread != NULL) {
+		thread__set_exited(thread, /*exited=*/true);
 		machine__remove_thread(machine, thread);
 		thread__put(thread);
 	}
@@ -2033,9 +2041,8 @@ int machine__process_exit_event(struct machine *machine, union perf_event *event
 	/* There is no context switch out before exit, so we decrement here. */
 	machine->parallelism--;
 	if (thread != NULL) {
-		if (symbol_conf.keep_exited_threads)
-			thread__set_exited(thread, /*exited=*/true);
-		else
+		thread__set_exited(thread, /*exited=*/true);
+		if (!symbol_conf.keep_exited_threads)
 			machine__remove_thread(machine, thread);
 	}
 	thread__put(thread);
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index c03c739e47191d1e..8fa5fac9a8e7114f 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -37,6 +37,16 @@ int thread__init_maps(struct thread *thread, struct machine *machine)
 		}
 	}
 
+	if (thread__pid(thread) != 0 || thread__tid(thread) != 0) {
+		if (!machine || !machine__is_host(machine) ||
+		    !perf_env__is_same_boot(machine->env)) {
+			struct nsinfo *nsi = thread__get_nsinfo(thread);
+
+			nsinfo__set_exited(nsi);
+			nsinfo__put(nsi);
+		}
+	}
+
 	return thread__maps(thread) ? 0 : -1;
 }
 
@@ -196,9 +206,17 @@ static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
 static void __thread__reset_nsinfo(struct thread *thread)
 	__must_hold(thread__namespaces_lock(thread))
 {
+	struct maps *maps = thread__maps(thread);
+	struct machine *machine = maps ? maps__machine(maps) : NULL;
+	struct nsinfo *nsi;
+
 	nsinfo__zput(RC_CHK_ACCESS(thread)->nsinfo);
-	RC_CHK_ACCESS(thread)->nsinfo = nsinfo__new_thread(thread__pid(thread),
-							   thread__tid(thread));
+	nsi = nsinfo__new_thread(thread__pid(thread), thread__tid(thread));
+	if (nsi && (thread__exited(thread) ||
+		    (machine && (!machine__is_host(machine) ||
+				 !perf_env__is_same_boot(machine->env)))))
+		nsinfo__set_exited(nsi);
+	RC_CHK_ACCESS(thread)->nsinfo = nsi;
 }
 
 void thread__reset_nsinfo(struct thread *thread)
@@ -319,6 +337,39 @@ int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
 	return ret;
 }
 
+void thread__set_exited(struct thread *thread, bool exited)
+{
+	RC_CHK_ACCESS(thread)->exited = exited;
+	if (exited) {
+		struct nsinfo *nsi = thread__get_nsinfo(thread);
+
+		nsinfo__set_exited(nsi);
+		nsinfo__put(nsi);
+	}
+}
+
+bool thread__is_alive(struct thread *thread)
+{
+	struct maps *maps;
+	struct machine *machine;
+	struct nsinfo *nsi;
+	bool ret;
+
+	if (!thread || thread__exited(thread))
+		return false;
+
+	maps = thread__maps(thread);
+	machine = maps ? maps__machine(maps) : NULL;
+	if (machine && (!machine__is_host(machine) ||
+			!perf_env__is_same_boot(machine->env)))
+		return false;
+
+	nsi = thread__get_nsinfo(thread);
+	ret = nsinfo__check_proc(nsi);
+	nsinfo__put(nsi);
+	return ret;
+}
+
 int thread__set_comm_from_proc(struct thread *thread)
 {
 	char path[64];
@@ -326,6 +377,9 @@ int thread__set_comm_from_proc(struct thread *thread)
 	size_t sz;
 	int err = -1;
 
+	if (!thread__is_alive(thread))
+		return -1;
+
 	if (!(snprintf(path, sizeof(path), "%d/task/%d/comm",
 		       thread__pid(thread), thread__tid(thread)) >= (int)sizeof(path)) &&
 	    procfs__read_str(path, &comm, &sz) == 0) {
@@ -544,7 +598,10 @@ uint16_t thread__e_machine_endian(struct thread *thread, struct machine *machine
 	tid = thread__tid(thread);
 	pid = thread__pid(thread);
 	if (pid != tid) {
-		struct thread *parent = machine__findnew_thread(machine, pid, pid);
+		struct thread *parent = machine__find_thread(machine, pid, pid);
+
+		if (!parent && thread__is_alive(thread))
+			parent = machine__findnew_thread(machine, pid, pid);
 
 		if (parent) {
 			e_machine = thread__e_machine_endian(parent, machine, &local_e_flags,
@@ -571,7 +628,7 @@ uint16_t thread__e_machine_endian(struct thread *thread, struct machine *machine
 			is_live = !session || !session->data;
 		}
 		/* Read from /proc/pid/exe if live. */
-		if (is_live) {
+		if (is_live && thread__is_alive(thread)) {
 			e_machine = read_proc_e_machine_for_pid(pid, &local_e_flags,
 								&args.is_big_endian);
 		} else if (machine && machine->env) {
diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h
index 68d63f9c37cc4240..11b4731d345ab5e1 100644
--- a/tools/perf/util/thread.h
+++ b/tools/perf/util/thread.h
@@ -205,11 +205,14 @@ static inline refcount_t *thread__refcnt(struct thread *thread)
 	return &RC_CHK_ACCESS(thread)->refcnt;
 }
 
-static inline void thread__set_exited(struct thread *thread, bool exited)
+static inline bool thread__exited(const struct thread *thread)
 {
-	RC_CHK_ACCESS(thread)->exited = exited;
+	return RC_CHK_ACCESS(thread)->exited;
 }
 
+void thread__set_exited(struct thread *thread, bool exited);
+bool thread__is_alive(struct thread *thread);
+
 static inline bool thread__comm_set(const struct thread *thread)
 {
 	return RC_CHK_ACCESS(thread)->comm_set;
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 8f7cd32f524dc10e..b513a049417ca0fe 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -22,6 +22,7 @@
 #include <linux/overflow.h>
 #include <unistd.h>
 #include "cap.h"
+#include "namespaces.h"
 #include "strlist.h"
 #include "string2.h"
 
@@ -460,10 +461,10 @@ char *filename_with_chroot(int pid, const char *filename)
 	char buf[PATH_MAX];
 	char proc_root[32];
 	char *new_name = NULL;
-	int ret;
+	ssize_t ret;
 
-	scnprintf(proc_root, sizeof(proc_root), "/proc/%d/root", pid);
-	ret = readlink(proc_root, buf, sizeof(buf) - 1);
+	scnprintf(proc_root, sizeof(proc_root), "%d/root", pid);
+	ret = nsinfo__proc_readlink(proc_root, buf, sizeof(buf) - 1);
 	if (ret <= 0)
 		return NULL;
 
-- 
2.56.1.809.ga0b61035b5-goog


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 08/11] perf tools: Export filename__read_build_id_ns()
  2026-11-23 23:20 [PATCH v3 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
                   ` (6 preceding siblings ...)
  2026-11-23 23:20 ` [PATCH v3 07/11] perf tools: Check thread liveness when accessing /proc Namhyung Kim
@ 2026-11-23 23:20 ` Namhyung Kim
  2026-11-23 23:20 ` [PATCH v3 09/11] perf tools: Only enter mount namespace if necessary Namhyung Kim
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Namhyung Kim @ 2026-11-23 23:20 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ian Rogers, Jiri Olsa, Adrian Hunter, James Clark,
	Peter Zijlstra, Ingo Molnar, LKML, linux-perf-users

Export filename__read_build_id_ns(), including a chroot fallback guarded
by !nsinfo__need_setns() && nsinfo__check_proc(), and use it in the dsos
callback and perf-inject's dso__read_build_id() instead of open-coding
nsinfo__mountns_enter() and dso__filename_with_chroot() fallbacks.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/builtin-inject.c | 12 +-----------
 tools/perf/util/build-id.c  | 15 ++++++++++++---
 tools/perf/util/dsos.c      | 17 +++--------------
 tools/perf/util/symbol.h    |  3 +++
 4 files changed, 19 insertions(+), 28 deletions(-)

diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index de3a74d2bab9220f..31e23169e25a5af1 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -906,24 +906,14 @@ static int perf_event__repipe_tracing_data(const struct perf_tool *tool,
 
 static int dso__read_build_id(struct dso *dso)
 {
-	struct nscookie nsc;
 	struct build_id bid = { .size = 0, };
 
 	if (dso__has_build_id(dso))
 		return 0;
 
 	mutex_lock(dso__lock(dso));
-	nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
-	if (filename__read_build_id(dso__long_name(dso), &bid) > 0)
+	if (filename__read_build_id_ns(dso__long_name(dso), &bid, dso__nsinfo(dso)) > 0)
 		dso__set_build_id(dso, &bid);
-	else if (dso__nsinfo(dso)) {
-		char *new_name = dso__filename_with_chroot(dso, dso__long_name(dso));
-
-		if (new_name && filename__read_build_id(new_name, &bid) > 0)
-			dso__set_build_id(dso, &bid);
-		free(new_name);
-	}
-	nsinfo__mountns_exit(&nsc);
 	mutex_unlock(dso__lock(dso));
 
 	return dso__has_build_id(dso) ? 0 : -1;
diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c
index c792448cc3442db9..395fc5b7bf2818e3 100644
--- a/tools/perf/util/build-id.c
+++ b/tools/perf/util/build-id.c
@@ -850,15 +850,24 @@ int build_id_cache__remove_s(const char *sbuild_id)
 	return err;
 }
 
-static int filename__read_build_id_ns(const char *filename,
-				      struct build_id *bid,
-				      struct nsinfo *nsi)
+int filename__read_build_id_ns(const char *filename,
+			       struct build_id *bid,
+			       struct nsinfo *nsi)
 {
 	struct nscookie nsc;
 	int ret;
 
 	nsinfo__mountns_enter(nsi, &nsc);
 	ret = filename__read_build_id(filename, bid);
+	if (ret < 0 && errno == ENOENT && !nsinfo__need_setns(nsi) &&
+	    nsinfo__check_proc(nsi)) {
+		char *new_name = filename_with_chroot(nsinfo__pid(nsi), filename);
+
+		if (new_name) {
+			ret = filename__read_build_id(new_name, bid);
+			free(new_name);
+		}
+	}
 	nsinfo__mountns_exit(&nsc);
 
 	return ret;
diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c
index 7a56577f4ec86a18..9ae27ee934135c48 100644
--- a/tools/perf/util/dsos.c
+++ b/tools/perf/util/dsos.c
@@ -4,11 +4,11 @@
 #include "dso.h"
 #include "util.h"
 #include "vdso.h"
-#include "namespaces.h"
 #include <errno.h>
+#include <linux/perf_event.h>
 #include <stdlib.h>
 #include <string.h>
-#include <symbol.h> // filename__read_build_id
+#include <symbol.h> // filename__read_build_id_ns
 #include <unistd.h>
 
 void dsos__init(struct dsos *dsos)
@@ -72,7 +72,6 @@ struct dsos__read_build_ids_cb_args {
 static int dsos__read_build_ids_cb(struct dso *dso, void *data)
 {
 	struct dsos__read_build_ids_cb_args *args = data;
-	struct nscookie nsc;
 	struct build_id bid = { .size = 0, };
 
 	if (args->with_hits && !dso__hit(dso) && !dso__is_vdso(dso))
@@ -81,20 +80,10 @@ static int dsos__read_build_ids_cb(struct dso *dso, void *data)
 		args->have_build_id = true;
 		return 0;
 	}
-	nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
-	if (filename__read_build_id(dso__long_name(dso), &bid) > 0) {
+	if (filename__read_build_id_ns(dso__long_name(dso), &bid, dso__nsinfo(dso)) > 0) {
 		dso__set_build_id(dso, &bid);
 		args->have_build_id = true;
-	} else if (errno == ENOENT && dso__nsinfo(dso)) {
-		char *new_name = dso__filename_with_chroot(dso, dso__long_name(dso));
-
-		if (new_name && filename__read_build_id(new_name, &bid) > 0) {
-			dso__set_build_id(dso, &bid);
-			args->have_build_id = true;
-		}
-		free(new_name);
 	}
-	nsinfo__mountns_exit(&nsc);
 	return 0;
 }
 
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 8b8ab7e1e1481999..455b45ca68e705d8 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -29,6 +29,7 @@
 struct dso;
 struct map;
 struct maps;
+struct nsinfo;
 struct option;
 struct build_id;
 struct perf_env;
@@ -238,6 +239,8 @@ struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name, size_
 enum dso_type dso__type_fd(int fd);
 
 int filename__read_build_id(const char *filename, struct build_id *id);
+int filename__read_build_id_ns(const char *filename, struct build_id *bid,
+			       struct nsinfo *nsi);
 int sysfs__read_build_id(const char *filename, struct build_id *bid);
 int modules__parse(const char *filename, void *arg,
 		   int (*process_module)(void *arg, const char *name,
-- 
2.56.1.809.ga0b61035b5-goog


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 09/11] perf tools: Only enter mount namespace if necessary
  2026-11-23 23:20 [PATCH v3 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
                   ` (7 preceding siblings ...)
  2026-11-23 23:20 ` [PATCH v3 08/11] perf tools: Export filename__read_build_id_ns() Namhyung Kim
@ 2026-11-23 23:20 ` Namhyung Kim
  2026-11-23 23:20 ` [PATCH v3 10/11] perf tools: Scope mount namespace entry in dso__load() Namhyung Kim
  2026-11-23 23:20 ` [PATCH v3 11/11] perf tests: Add namespace, nsinfo, and boot ID tests Namhyung Kim
  10 siblings, 0 replies; 12+ messages in thread
From: Namhyung Kim @ 2026-11-23 23:20 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ian Rogers, Jiri Olsa, Adrian Hunter, James Clark,
	Peter Zijlstra, Ingo Molnar, LKML, linux-perf-users

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().

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       | 51 +++++++++++++++++++++++++++++--------
 tools/perf/util/dso.h       | 34 +++++++++++++++++++++++++
 5 files changed, 100 insertions(+), 20 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..fdade14520d869bd 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -692,9 +692,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,12 +873,22 @@ 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 = dso_binary_type__needs_ns(type);
+
+		dso__set_binary_type(dso, type);
+
+		if (nsenter)
+			nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
 
 		errno = 0;
-		path = dso__get_filename(dso, "", &decomp, dso__binary_type(dso));
+		path = dso__get_filename(dso, "", &decomp, type);
 		if (path == NULL) {
-			if (errno == EMFILE)
+			saved_errno = errno;
+			if (nsenter)
+				nsinfo__mountns_exit(&nsc);
+			if (saved_errno == EMFILE)
 				goto open_err;
 			continue;
 		}
@@ -890,9 +898,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 +2127,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 +2255,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;
 
 	if (dso__dbginfo_type(dso) != DSO_BINARY_TYPE__NOT_FOUND)
 		return;
@@ -2253,28 +2267,43 @@ 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 */
+	nsenter = dso_binary_type__needs_ns(dso__symtab_type(dso));
+	if (nsenter)
+		nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
 	path = dso__get_filename(dso, "", &decomp, dso__symtab_type(dso));
 	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;
 		}
+	} else if (nsenter) {
+		nsinfo__mountns_exit(&nsc);
 	}
 
 	/* Otherwise check distro debug locations */
 	for (type = dbginfo_types; *type != DSO_BINARY_TYPE__NOT_FOUND; type++) {
+		nsenter = dso_binary_type__needs_ns(*type);
+		if (nsenter)
+			nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
 		path = dso__get_filename(dso, "", &decomp, *type);
-		if (path == NULL)
+		if (path == NULL) {
+			if (nsenter)
+				nsinfo__mountns_exit(&nsc);
 			continue;
+		}
 
 		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)
 			break;
 	}
diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index 63c1b7d6f6f84bca..b446ed181bc16536 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.809.ga0b61035b5-goog


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 10/11] perf tools: Scope mount namespace entry in dso__load()
  2026-11-23 23:20 [PATCH v3 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
                   ` (8 preceding siblings ...)
  2026-11-23 23:20 ` [PATCH v3 09/11] perf tools: Only enter mount namespace if necessary Namhyung Kim
@ 2026-11-23 23:20 ` Namhyung Kim
  2026-11-23 23:20 ` [PATCH v3 11/11] perf tests: Add namespace, nsinfo, and boot ID tests Namhyung Kim
  10 siblings, 0 replies; 12+ messages in thread
From: Namhyung Kim @ 2026-11-23 23:20 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ian Rogers, Jiri Olsa, Adrian Hunter, James Clark,
	Peter Zijlstra, Ingo Molnar, LKML, linux-perf-users

Enter the target mount namespace only for the specific binary types
that need it in dso__load(), and use filename__read_build_id_ns() for
BUILDID_DEBUGINFO lookup and in symbol-minimal's dso__load_sym() so it
does not read the wrong build-id from the host filesystem.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/symbol-minimal.c |  4 ++-
 tools/perf/util/symbol.c         | 45 +++++++++++++++-----------------
 2 files changed, 24 insertions(+), 25 deletions(-)

diff --git a/tools/perf/util/symbol-minimal.c b/tools/perf/util/symbol-minimal.c
index 0a71d146395271a6..5beb73fd508ad079 100644
--- a/tools/perf/util/symbol-minimal.c
+++ b/tools/perf/util/symbol-minimal.c
@@ -342,7 +342,9 @@ int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
 	if (ret >= 0)
 		RC_CHK_ACCESS(dso)->is_64_bit = ret;
 
-	if (filename__read_build_id(ss->name, &bid) > 0)
+	if (filename__read_build_id_ns(ss->name, &bid,
+				       dso_binary_type__needs_ns(ss->type) ?
+				       dso__nsinfo(dso) : NULL) > 0)
 		dso__set_build_id(dso, &bid);
 	return 0;
 }
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index 35b08c05a2fd00e1..8e03d335ffb25c21 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -2209,17 +2209,6 @@ int dso__load(struct dso *dso, struct map *map)
 	const char *map_path = dso__long_name(dso);
 
 	mutex_lock(dso__lock(dso));
-	perfmap = is_perf_pid_map_name(map_path);
-
-	if (perfmap) {
-		if (dso__nsinfo(dso) &&
-		    (dso__find_perf_map(newmapname, sizeof(newmapname),
-					dso__nsinfo_ptr(dso)) == 0)) {
-			map_path = newmapname;
-		}
-	}
-
-	nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
 
 	/* check again under the dso->lock */
 	if (dso__loaded(dso)) {
@@ -2243,8 +2232,16 @@ int dso__load(struct dso *dso, struct map *map)
 
 	dso__set_adjust_symbols(dso, false);
 
+	perfmap = is_perf_pid_map_name(map_path);
 	if (perfmap) {
+		if (dso__nsinfo(dso) &&
+		    (dso__find_perf_map(newmapname, sizeof(newmapname),
+					dso__nsinfo_ptr(dso)) == 0)) {
+			map_path = newmapname;
+		}
+		nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
 		ret = dso__load_perf_map(map_path, dso);
+		nsinfo__mountns_exit(&nsc);
 		dso__set_binary_type(dso, DSO_BINARY_TYPE__JAVA_JIT);
 		dso__set_symtab_type(dso, ret > 0
 				? DSO_BINARY_TYPE__JAVA_JIT
@@ -2267,7 +2264,7 @@ int dso__load(struct dso *dso, struct map *map)
 		struct build_id bid = { .size = 0, };
 
 		__symbol__join_symfs(name, PATH_MAX, dso__long_name(dso));
-		if (filename__read_build_id(name, &bid) > 0)
+		if (filename__read_build_id_ns(name, &bid, dso__nsinfo(dso)) > 0)
 			dso__set_build_id(dso, &bid);
 	}
 
@@ -2280,27 +2277,28 @@ int dso__load(struct dso *dso, struct map *map)
 		struct symsrc *ss = &ss_[ss_pos];
 		bool next_slot = false;
 		bool is_reg;
-		bool nsexit;
+		bool nsenter;
 		int bfdrc = -1;
 		int sirc = -1;
 
 		enum dso_binary_type symtab_type = binary_type_symtab[i];
 
-		nsexit = (symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE ||
-		    symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO);
-
 		if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
 			continue;
 
+		nsenter = dso_binary_type__needs_ns(symtab_type);
+		if (nsenter)
+			nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
+
 		if (dso__read_binary_type_filename(dso, symtab_type,
-						   root_dir, name, PATH_MAX))
+						   root_dir, name, PATH_MAX)) {
+			if (nsenter)
+				nsinfo__mountns_exit(&nsc);
 			continue;
-
-		if (nsexit)
-			nsinfo__mountns_exit(&nsc);
+		}
 
 		is_reg = is_regular_file(name);
-		if (!is_reg && errno == ENOENT && dso__nsinfo(dso)) {
+		if (!is_reg && errno == ENOENT && nsenter && dso__nsinfo(dso)) {
 			char *new_name = dso__filename_with_chroot(dso, name);
 			if (new_name) {
 				is_reg = is_regular_file(new_name);
@@ -2316,8 +2314,8 @@ int dso__load(struct dso *dso, struct map *map)
 		if (is_reg && bfdrc < 0)
 			sirc = symsrc__init(ss, dso, name, symtab_type);
 
-		if (nsexit)
-			nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
+		if (nsenter)
+			nsinfo__mountns_exit(&nsc);
 
 		if (bfdrc > 0) {
 			dso__set_symtab_type(dso, symtab_type);
@@ -2384,7 +2382,6 @@ int dso__load(struct dso *dso, struct map *map)
 out:
 	dso__set_loaded(dso);
 	mutex_unlock(dso__lock(dso));
-	nsinfo__mountns_exit(&nsc);
 
 	return ret;
 }
-- 
2.56.1.809.ga0b61035b5-goog


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 11/11] perf tests: Add namespace, nsinfo, and boot ID tests
  2026-11-23 23:20 [PATCH v3 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
                   ` (9 preceding siblings ...)
  2026-11-23 23:20 ` [PATCH v3 10/11] perf tools: Scope mount namespace entry in dso__load() Namhyung Kim
@ 2026-11-23 23:20 ` Namhyung Kim
  10 siblings, 0 replies; 12+ messages in thread
From: Namhyung Kim @ 2026-11-23 23:20 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ian Rogers, Jiri Olsa, Adrian Hunter, James Clark,
	Peter Zijlstra, Ingo Molnar, LKML, linux-perf-users

Add a "Namespace and nsinfo tests" suite covering HEADER_BOOT_ID,
perf_env__is_same_boot(), deferred /proc lookup, raw nsinfo__copy(),
PID/TID recycling detection, thread exit and fork liveness tracking,
dso_binary_type__needs_ns(), and isolated mountns, overmounted /proc,
and chroot resolution.

Also verify the boot ID header matches /proc/sys/kernel/random/boot_id
in the perf header and perf pipe shell tests.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/tests/Build              |    1 +
 tools/perf/tests/builtin-test.c     |    1 +
 tools/perf/tests/namespaces.c       | 1188 +++++++++++++++++++++++++++
 tools/perf/tests/shell/header.sh    |    9 +
 tools/perf/tests/shell/pipe_test.sh |   21 +
 tools/perf/tests/tests.h            |    1 +
 6 files changed, 1221 insertions(+)
 create mode 100644 tools/perf/tests/namespaces.c

diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 5eebb2ec5ddefcfd..7f2e926eb0bc7674 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -74,6 +74,7 @@ perf-test-y += hwmon_pmu.o
 perf-test-y += tool_pmu.o
 perf-test-y += subcmd-help.o
 perf-test-y += kallsyms-split.o
+perf-test-y += namespaces.o
 
 ifeq ($(SRCARCH),$(filter $(SRCARCH),x86 arm arm64 powerpc riscv))
 perf-test-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 01c78ef20b97dec9..4f087851c790c7c6 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -192,6 +192,7 @@ static struct test_suite *generic_tests[] = {
 	&suite__util,
 	&suite__subcmd_help,
 	&suite__kallsyms_split,
+	&suite__namespaces,
 	NULL,
 };
 
diff --git a/tools/perf/tests/namespaces.c b/tools/perf/tests/namespaces.c
new file mode 100644
index 0000000000000000..2e332c333960e57a
--- /dev/null
+++ b/tools/perf/tests/namespaces.c
@@ -0,0 +1,1188 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <pthread.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <api/fs/fs.h>
+#include <linux/compiler.h>
+#include <linux/err.h>
+#include <linux/kernel.h>
+#include <linux/perf_event.h>
+#include <linux/string.h>
+#include <linux/zalloc.h>
+
+#include "debug.h"
+#include "tests.h"
+#include "util/build-id.h"
+#include "util/dso.h"
+#include "util/env.h"
+#include "util/event.h"
+#include "util/header.h"
+#include "util/machine.h"
+#include "util/map.h"
+#include "util/namespaces.h"
+#include "util/session.h"
+#include "util/symbol.h"
+#include "util/synthetic-events.h"
+#include "util/thread.h"
+#include "util/tool.h"
+#include "util/util.h"
+
+#define CHILD_EXIT_SKIP 2
+
+static int read_host_boot_id_str(char *buf, size_t sz)
+{
+	char *tmp = NULL;
+	size_t len = 0;
+
+	if (procfs__read_str("sys/kernel/random/boot_id", &tmp, &len) < 0)
+		return -1;
+
+	strlcpy(buf, strim(tmp), sz);
+	free(tmp);
+	return buf[0] ? 0 : -1;
+}
+
+struct boot_id_feature_tool {
+	struct perf_tool	tool;
+	struct perf_session	*session;
+};
+
+static int process_boot_id_feature(const struct perf_tool *tool,
+				   union perf_event *event,
+				   struct perf_sample *sample __maybe_unused,
+				   struct machine *machine __maybe_unused)
+{
+	struct boot_id_feature_tool *ftool =
+		container_of(tool, struct boot_id_feature_tool, tool);
+
+	return perf_event__process_feature(tool, ftool->session, event);
+}
+
+static int test_boot_id_feature_event(struct perf_env *host_env)
+{
+	struct boot_id_feature_tool ftool = {};
+	struct perf_session *session;
+	int ret = -1;
+
+	perf_tool__init(&ftool.tool, false);
+	session = __perf_session__new(NULL, &ftool.tool, false, host_env);
+	if (IS_ERR(session))
+		return -1;
+
+	ftool.session = session;
+	session->header.env.offline = true;
+	if (perf_env__is_same_boot(&session->header.env)) {
+		pr_debug("empty offline session unexpectedly matched host boot_id\n");
+		goto out;
+	}
+
+	perf_header__set_feat(&session->header, HEADER_BOOT_ID);
+	if (perf_event__synthesize_features(&ftool.tool, session, NULL,
+					    process_boot_id_feature) < 0) {
+		pr_debug("perf_event__synthesize_features(HEADER_BOOT_ID) failed\n");
+		goto out;
+	}
+
+	if (!session->header.env.boot_id ||
+	    !perf_env__is_same_boot(&session->header.env)) {
+		pr_debug("processed HEADER_BOOT_ID feature failed to match host boot_id\n");
+		goto out;
+	}
+
+	ret = 0;
+out:
+	perf_session__delete(session);
+	return ret;
+}
+
+static int test__nsinfo_boot_id(struct test_suite *test __maybe_unused,
+				int subtest __maybe_unused)
+{
+	struct perf_env live_env, off_env;
+	char host_boot_id[64];
+	const char *bid;
+	int ret = TEST_FAIL;
+
+	if (read_host_boot_id_str(host_boot_id, sizeof(host_boot_id)) < 0) {
+		pr_debug("SKIP: cannot read host boot_id from procfs\n");
+		return TEST_SKIP;
+	}
+
+	perf_env__init(&live_env);
+	perf_env__init(&off_env);
+
+	if (!perf_env__is_same_boot(NULL)) {
+		pr_debug("NULL env should match current host boot\n");
+		goto out;
+	}
+
+	if (!perf_env__is_same_boot(&live_env)) {
+		pr_debug("live env should match current host boot\n");
+		goto out;
+	}
+	bid = perf_env__boot_id(&live_env);
+	if (!bid || strcmp(bid, host_boot_id) != 0) {
+		pr_debug("unexpected live env boot_id: %s vs %s\n",
+			 bid ? bid : "(null)", host_boot_id);
+		goto out;
+	}
+
+	/* Offline environments must not fall back to or cache the host boot_id. */
+	off_env.offline = true;
+	if (perf_env__boot_id(&off_env) != NULL) {
+		pr_debug("offline env without boot_id unexpectedly returned host boot_id\n");
+		goto out;
+	}
+	if (off_env.boot_id != NULL) {
+		pr_debug("offline env->boot_id was poisoned\n");
+		goto out;
+	}
+	if (perf_env__is_same_boot(&off_env)) {
+		pr_debug("offline env without boot_id should not match current boot\n");
+		goto out;
+	}
+
+	off_env.boot_id = strdup(host_boot_id);
+	if (!off_env.boot_id)
+		goto out;
+	if (!perf_env__is_same_boot(&off_env)) {
+		pr_debug("offline env with matching boot_id failed to match\n");
+		goto out;
+	}
+
+	zfree(&off_env.boot_id);
+	off_env.boot_id = strdup("00000000-0000-0000-0000-000000000000");
+	if (!off_env.boot_id)
+		goto out;
+	if (perf_env__is_same_boot(&off_env)) {
+		pr_debug("offline env with mismatched boot_id unexpectedly matched\n");
+		goto out;
+	}
+
+	if (test_boot_id_feature_event(&live_env) < 0)
+		goto out;
+
+	ret = TEST_OK;
+out:
+	perf_env__exit(&off_env);
+	perf_env__exit(&live_env);
+	return ret;
+}
+
+static int test__nsinfo_init_and_copy(struct test_suite *test __maybe_unused,
+				      int subtest __maybe_unused)
+{
+	struct nsinfo *nsi = NULL, *copy = NULL, *copy2 = NULL;
+	pid_t pid = getpid();
+	int ret = TEST_FAIL;
+
+	if (nsinfo__new_thread(0, pid) != NULL ||
+	    nsinfo__new_thread(pid, 0) != NULL ||
+	    nsinfo__new_thread(-1, -1) != NULL ||
+	    nsinfo__copy(NULL) != NULL ||
+	    nsinfo__need_setns(NULL) ||
+	    nsinfo__check_proc(NULL)) {
+		pr_debug("invalid nsinfo boundary condition succeeded\n");
+		return TEST_FAIL;
+	}
+
+	nsi = nsinfo__new_thread(pid, pid);
+	if (!nsi)
+		return TEST_FAIL;
+
+	if (nsinfo__inited(nsi) || nsinfo__exited(nsi)) {
+		pr_debug("nsinfo__new_thread() unexpectedly ran /proc init eagerly\n");
+		goto out;
+	}
+	if (nsinfo__pid(nsi) != pid || nsinfo__tid(nsi) != pid) {
+		pr_debug("unexpected pid/tid on new nsinfo\n");
+		goto out;
+	}
+
+	/*
+	 * Copying before initialization (e.g., for [vdso] mappings) must not
+	 * force eager /proc lookups on the source nsinfo.
+	 */
+	copy = nsinfo__copy(nsi);
+	if (!copy)
+		goto out;
+	if (nsinfo__inited(nsi) || nsinfo__inited(copy)) {
+		pr_debug("nsinfo__copy() unexpectedly triggered lazy init\n");
+		goto out;
+	}
+
+	nsinfo__clear_need_setns(copy);
+	if (!nsinfo__inited(copy) || nsinfo__exited(copy) || nsinfo__need_setns(copy)) {
+		pr_debug("nsinfo__clear_need_setns() state mismatch\n");
+		goto out;
+	}
+
+	/* Querying triggers lazy initialization cleanly. */
+	if (nsinfo__need_setns(nsi)) {
+		pr_debug("same-namespace task unexpectedly needs setns\n");
+		goto out;
+	}
+	if (!nsinfo__inited(nsi) || nsinfo__exited(nsi) ||
+	    nsinfo__tgid(nsi) != pid || nsinfo__nstgid(nsi) <= 0) {
+		pr_debug("lazy init failed to populate expected fields\n");
+		goto out;
+	}
+
+	copy2 = nsinfo__copy(nsi);
+	if (!copy2)
+		goto out;
+	if (!nsinfo__inited(copy2) ||
+	    nsinfo__tgid(copy2) != nsinfo__tgid(nsi) ||
+	    nsinfo__nstgid(copy2) != nsinfo__nstgid(nsi) ||
+	    nsinfo__in_pidns(copy2) != nsinfo__in_pidns(nsi)) {
+		pr_debug("nsinfo__copy() did not preserve initialized fields\n");
+		goto out;
+	}
+
+	ret = TEST_OK;
+out:
+	nsinfo__put(copy2);
+	nsinfo__put(copy);
+	nsinfo__put(nsi);
+	return ret;
+}
+
+struct worker_ctx {
+	pthread_mutex_t lock;
+	pthread_cond_t cond;
+	bool ready;
+	bool stop;
+	pid_t tid;
+};
+
+static void *worker_thread_fn(void *arg)
+{
+	struct worker_ctx *ctx = arg;
+
+	pthread_mutex_lock(&ctx->lock);
+	ctx->tid = syscall(SYS_gettid);
+	ctx->ready = true;
+	pthread_cond_broadcast(&ctx->cond);
+	while (!ctx->stop)
+		pthread_cond_wait(&ctx->cond, &ctx->lock);
+	pthread_mutex_unlock(&ctx->lock);
+
+	return NULL;
+}
+
+static pid_t spawn_reaped_child(void)
+{
+	pid_t child;
+	int status;
+
+	child = fork();
+	if (child < 0)
+		return -1;
+	if (child == 0)
+		_exit(0);
+
+	if (waitpid(child, &status, 0) != child)
+		return -1;
+
+	return child;
+}
+
+static int test__nsinfo_proc_and_recycled_tid(struct test_suite *test __maybe_unused,
+					      int subtest __maybe_unused)
+{
+	struct worker_ctx wctx = {
+		.lock = PTHREAD_MUTEX_INITIALIZER,
+		.cond = PTHREAD_COND_INITIALIZER,
+	};
+	struct nsinfo *dead1 = NULL, *dead2 = NULL;
+	struct nsinfo *valid = NULL, *recycled1 = NULL, *recycled2 = NULL;
+	pthread_t thr;
+	bool started = false;
+	pid_t dead_pid, self_pid = getpid();
+	int ret = TEST_FAIL;
+
+	dead_pid = spawn_reaped_child();
+	if (dead_pid <= 0) {
+		pr_debug("SKIP: cannot fork dead child\n");
+		return TEST_SKIP;
+	}
+
+	dead1 = nsinfo__new_thread(dead_pid, dead_pid);
+	dead2 = nsinfo__new_thread(dead_pid, dead_pid);
+	if (!dead1 || !dead2)
+		goto out;
+
+	if (nsinfo__check_proc(dead1)) {
+		pr_debug("reaped child unexpectedly passed nsinfo__check_proc()\n");
+		goto out;
+	}
+	if (!nsinfo__exited(dead1) || !nsinfo__inited(dead1) ||
+	    nsinfo__need_setns(dead1)) {
+		pr_debug("failed nsinfo__check_proc() did not mark dead1 exited\n");
+		goto out;
+	}
+
+	if (nsinfo__need_setns(dead2)) {
+		pr_debug("reaped child unexpectedly needed setns\n");
+		goto out;
+	}
+	if (!nsinfo__exited(dead2) || !nsinfo__inited(dead2) ||
+	    nsinfo__tgid(dead2) != dead_pid || nsinfo__nstgid(dead2) != dead_pid ||
+	    nsinfo__in_pidns(dead2)) {
+		pr_debug("failed lazy init corrupted fallback fields on dead nsinfo\n");
+		goto out;
+	}
+
+	if (pthread_create(&thr, NULL, worker_thread_fn, &wctx) != 0)
+		goto out;
+	started = true;
+
+	pthread_mutex_lock(&wctx.lock);
+	while (!wctx.ready)
+		pthread_cond_wait(&wctx.cond, &wctx.lock);
+	pthread_mutex_unlock(&wctx.lock);
+
+	if (wctx.tid == self_pid) {
+		pr_debug("worker tid unexpectedly matched tgid\n");
+		goto out;
+	}
+
+	valid = nsinfo__new_thread(self_pid, wctx.tid);
+	if (!valid)
+		goto out;
+	if (!nsinfo__check_proc(valid) || nsinfo__need_setns(valid) ||
+	    nsinfo__exited(valid) || nsinfo__tgid(valid) != self_pid) {
+		pr_debug("valid live thread (pid, tid) failed proc checks\n");
+		goto out;
+	}
+
+	/*
+	 * Simulate TID recycling: /proc/<tid>/task/<tid>/status exists in
+	 * procfs for our live worker thread, but its Tgid is self_pid != tid.
+	 * Both check_proc and lazy init must reject it and preserve fallback
+	 * fields without overwriting tgid/nstgid/in_pidns.
+	 */
+	recycled1 = nsinfo__new_thread(wctx.tid, wctx.tid);
+	recycled2 = nsinfo__new_thread(wctx.tid, wctx.tid);
+	if (!recycled1 || !recycled2)
+		goto out;
+
+	if (nsinfo__check_proc(recycled1) || !nsinfo__exited(recycled1)) {
+		pr_debug("mismatched Tgid passed nsinfo__check_proc()\n");
+		goto out;
+	}
+	if (nsinfo__need_setns(recycled2) || !nsinfo__exited(recycled2)) {
+		pr_debug("mismatched Tgid passed lazy init\n");
+		goto out;
+	}
+	if (nsinfo__tgid(recycled2) != wctx.tid ||
+	    nsinfo__nstgid(recycled2) != wctx.tid ||
+	    nsinfo__in_pidns(recycled2)) {
+		pr_debug("mismatched Tgid overwrote nsinfo tgid/pidns fields\n");
+		goto out;
+	}
+
+	ret = TEST_OK;
+out:
+	if (started) {
+		pthread_mutex_lock(&wctx.lock);
+		wctx.stop = true;
+		pthread_cond_broadcast(&wctx.cond);
+		pthread_mutex_unlock(&wctx.lock);
+		pthread_join(thr, NULL);
+	}
+	nsinfo__put(recycled2);
+	nsinfo__put(recycled1);
+	nsinfo__put(valid);
+	nsinfo__put(dead2);
+	nsinfo__put(dead1);
+	return ret;
+}
+
+static int test__nsinfo_thread_liveness(struct test_suite *test __maybe_unused,
+					int subtest __maybe_unused)
+{
+	struct machines live_machines, off_machines;
+	struct machine *live_m, *off_m;
+	struct perf_env live_env, off_env;
+	struct thread *idle_th = NULL, *off_th = NULL, *off_sub = NULL;
+	struct thread *th = NULL, *bad_parent = NULL;
+	struct nsinfo *nsi1 = NULL, *nsi2 = NULL, *nsi3 = NULL;
+	struct nsinfo *old_parent_nsi = NULL, *del_nsi = NULL, *off_nsi = NULL;
+	struct perf_record_namespaces *ns_ev;
+	struct perf_sample sample = { 0 };
+	struct perf_tool tool;
+	union perf_event ev;
+	pid_t self_pid = getpid();
+	int ret = TEST_FAIL;
+
+	perf_env__init(&live_env);
+	perf_env__init(&off_env);
+	off_env.offline = true;
+	perf_tool__init(&tool, /*ordered_events=*/false);
+
+	if (machines__init(&live_machines, NULL) != 0)
+		goto out_env;
+	if (machines__init(&off_machines, NULL) != 0) {
+		machines__exit(&live_machines);
+		goto out_env;
+	}
+
+	live_m = &live_machines.host;
+	live_m->env = &live_env;
+	off_m = &off_machines.host;
+	off_m->env = &off_env;
+
+	/* Idle (0, 0) thread must not evaluate boot_id or populate env->boot_id. */
+	idle_th = machine__findnew_thread(off_m, 0, 0);
+	if (!idle_th)
+		goto out;
+	if (off_env.boot_id != NULL) {
+		pr_debug("idle thread init poisoned offline env->boot_id\n");
+		goto out;
+	}
+	thread__put(idle_th);
+	idle_th = NULL;
+
+	/*
+	 * Create both leader and sub-thread before removing the leader so
+	 * thread__init_maps() does not re-create (self_pid, self_pid) later,
+	 * then verify thread__e_machine_endian() does not recreate it either.
+	 */
+	off_th = machine__findnew_thread(off_m, self_pid, self_pid);
+	off_sub = machine__findnew_thread(off_m, self_pid, self_pid + 1);
+	if (!off_th || !off_sub)
+		goto out;
+	off_nsi = thread__get_nsinfo(off_th);
+	if (!off_nsi || !nsinfo__exited(off_nsi) || thread__is_alive(off_th)) {
+		pr_debug("offline session thread stayed alive\n");
+		goto out;
+	}
+	machine__remove_thread(off_m, off_th);
+	thread__put(off_th);
+	off_th = NULL;
+
+	thread__e_machine_endian(off_sub, off_m, NULL, NULL);
+	if (machine__find_thread(off_m, self_pid, self_pid) != NULL) {
+		pr_debug("thread__e_machine_endian() unexpectedly recreated exited leader\n");
+		goto out;
+	}
+	machine__remove_thread(off_m, off_sub);
+	thread__put(off_sub);
+	off_sub = NULL;
+
+	/* Live thread tracking, reset on exec/namespaces, and exit propagation. */
+	th = machine__findnew_thread(live_m, self_pid, self_pid);
+	if (!th || !thread__is_alive(th)) {
+		pr_debug("current live thread reported as dead\n");
+		goto out;
+	}
+
+	nsi1 = thread__get_nsinfo(th);
+	if (!nsi1)
+		goto out;
+
+	if (__thread__set_comm(th, "execed", 1000, /*exec=*/true) != 0)
+		goto out;
+	nsi2 = thread__get_nsinfo(th);
+	if (!nsi2 || RC_CHK_EQUAL(nsi2, nsi1)) {
+		pr_debug("exec did not replace thread->nsinfo\n");
+		goto out;
+	}
+
+	ns_ev = calloc(1, sizeof(*ns_ev) + sizeof(ns_ev->link_info[0]));
+	if (!ns_ev)
+		goto out;
+	ns_ev->nr_namespaces = 1;
+	if (thread__set_namespaces(th, 2000, ns_ev) != 0) {
+		free(ns_ev);
+		goto out;
+	}
+	free(ns_ev);
+
+	nsi3 = thread__get_nsinfo(th);
+	if (!nsi3 || RC_CHK_EQUAL(nsi3, nsi2)) {
+		pr_debug("PERF_RECORD_NAMESPACES did not replace thread->nsinfo\n");
+		goto out;
+	}
+
+	thread__set_exited(th, true);
+	if (!thread__exited(th) || thread__is_alive(th) ||
+	    !nsinfo__exited(nsi3) || nsinfo__check_proc(nsi3) ||
+	    nsinfo__exited(nsi1)) {
+		pr_debug("exit propagation or prior DSO nsinfo isolation failed\n");
+		goto out;
+	}
+
+	machine__remove_thread(live_m, th);
+	thread__put(th);
+	th = NULL;
+
+	/* Replaced erroneous parent in machine__process_fork_event() gets marked exited. */
+	bad_parent = machine__findnew_thread(live_m, self_pid + 1, self_pid);
+	if (!bad_parent)
+		goto out;
+	old_parent_nsi = thread__get_nsinfo(bad_parent);
+	thread__put(bad_parent);
+	bad_parent = NULL;
+	if (!old_parent_nsi)
+		goto out;
+
+	memset(&ev, 0, sizeof(ev));
+	ev.fork.header.type = PERF_RECORD_FORK;
+	ev.fork.header.size = sizeof(ev.fork);
+	ev.fork.ppid = self_pid;
+	ev.fork.ptid = self_pid;
+	ev.fork.pid = self_pid + 2;
+	ev.fork.tid = self_pid + 2;
+	if (machine__process_fork_event(live_m, &ev, &sample) != 0)
+		goto out;
+	if (!nsinfo__exited(old_parent_nsi) || nsinfo__check_proc(old_parent_nsi)) {
+		pr_debug("replaced parent thread nsinfo was not marked exited\n");
+		goto out;
+	}
+
+	/* perf_event__exit_del_thread() also marks removed thread's nsinfo exited. */
+	th = machine__find_thread(live_m, self_pid, self_pid);
+	if (!th)
+		goto out;
+	del_nsi = thread__get_nsinfo(th);
+	thread__put(th);
+	th = NULL;
+	if (!del_nsi)
+		goto out;
+
+	memset(&ev, 0, sizeof(ev));
+	ev.fork.header.type = PERF_RECORD_EXIT;
+	ev.fork.header.size = sizeof(ev.fork);
+	ev.fork.pid = self_pid;
+	ev.fork.tid = self_pid;
+	ev.fork.ppid = 1;
+	ev.fork.ptid = 1;
+	if (perf_event__exit_del_thread(&tool, &ev, &sample, live_m) != 0)
+		goto out;
+	th = machine__find_thread(live_m, self_pid, self_pid);
+	if (!nsinfo__exited(del_nsi) || nsinfo__check_proc(del_nsi) || th != NULL) {
+		pr_debug("perf_event__exit_del_thread() did not mark nsinfo exited\n");
+		goto out;
+	}
+
+	ret = TEST_OK;
+out:
+	thread__put(th);
+	thread__put(off_sub);
+	thread__put(off_th);
+	thread__put(idle_th);
+	thread__put(bad_parent);
+	nsinfo__put(del_nsi);
+	nsinfo__put(old_parent_nsi);
+	nsinfo__put(off_nsi);
+	nsinfo__put(nsi3);
+	nsinfo__put(nsi2);
+	nsinfo__put(nsi1);
+	machines__exit(&off_machines);
+	machines__exit(&live_machines);
+out_env:
+	perf_env__exit(&off_env);
+	perf_env__exit(&live_env);
+	return ret;
+}
+
+static int test__nsinfo_dso_needs_ns(struct test_suite *test __maybe_unused,
+				     int subtest __maybe_unused)
+{
+	static const enum dso_binary_type needs_ns_types[] = {
+		DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
+		DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
+		DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
+		DSO_BINARY_TYPE__DEBUGLINK,
+		DSO_BINARY_TYPE__GNU_DEBUGDATA,
+		DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
+		DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
+		DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO,
+		DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
+		DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
+	};
+	static const enum dso_binary_type host_only_types[] = {
+		DSO_BINARY_TYPE__KALLSYMS,
+		DSO_BINARY_TYPE__GUEST_KALLSYMS,
+		DSO_BINARY_TYPE__VMLINUX,
+		DSO_BINARY_TYPE__GUEST_VMLINUX,
+		DSO_BINARY_TYPE__JAVA_JIT,
+		DSO_BINARY_TYPE__BUILD_ID_CACHE,
+		DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO,
+		DSO_BINARY_TYPE__GUEST_KMODULE,
+		DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
+		DSO_BINARY_TYPE__KCORE,
+		DSO_BINARY_TYPE__GUEST_KCORE,
+		DSO_BINARY_TYPE__BPF_PROG_INFO,
+		DSO_BINARY_TYPE__BPF_IMAGE,
+		DSO_BINARY_TYPE__OOL,
+		DSO_BINARY_TYPE__NOT_FOUND,
+	};
+	struct dso *dso = NULL;
+	struct nsinfo *nsi = NULL;
+	struct build_id bid = { .size = 0 };
+	char *resolved = NULL;
+	int ret = TEST_FAIL;
+
+	for (size_t i = 0; i < ARRAY_SIZE(needs_ns_types); i++) {
+		if (!dso_binary_type__needs_ns(needs_ns_types[i])) {
+			pr_debug("binary type %d should need ns\n", needs_ns_types[i]);
+			return TEST_FAIL;
+		}
+	}
+
+	for (size_t i = 0; i < ARRAY_SIZE(host_only_types); i++) {
+		if (dso_binary_type__needs_ns(host_only_types[i])) {
+			pr_debug("binary type %d should not need ns\n", host_only_types[i]);
+			return TEST_FAIL;
+		}
+	}
+
+	/* Guarded chroot/build-id lookups must bail out immediately when exited. */
+	dso = dso__new("/bin/sh");
+	nsi = nsinfo__new_thread(getpid(), getpid());
+	if (!dso || !nsi)
+		goto out;
+	nsinfo__set_exited(nsi);
+	dso__set_nsinfo(dso, nsinfo__get(nsi));
+
+	resolved = dso__filename_with_chroot(dso, "/bin/sh");
+	if (resolved != NULL) {
+		pr_debug("dso__filename_with_chroot() did not respect exited nsinfo\n");
+		free(resolved);
+		goto out;
+	}
+	if (filename__read_build_id_ns("/nonexistent_perf_chroot_test", &bid, nsi) >= 0) {
+		pr_debug("filename__read_build_id_ns() succeeded on nonexistent path\n");
+		goto out;
+	}
+
+	ret = TEST_OK;
+out:
+	nsinfo__put(nsi);
+	dso__put(dso);
+	return ret;
+}
+
+static int copy_file_mode(const char *src, const char *dst, mode_t mode)
+{
+	char buf[4096];
+	int in_fd = -1, out_fd = -1;
+	ssize_t n;
+	int err = -1;
+
+	in_fd = open(src, O_RDONLY);
+	if (in_fd < 0)
+		return -1;
+	out_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode);
+	if (out_fd < 0)
+		goto out;
+
+	while ((n = read(in_fd, buf, sizeof(buf))) > 0) {
+		ssize_t off = 0;
+
+		while (off < n) {
+			ssize_t w = write(out_fd, buf + off, n - off);
+
+			if (w <= 0)
+				goto out;
+			off += w;
+		}
+	}
+	if (n == 0)
+		err = 0;
+out:
+	if (out_fd >= 0)
+		close(out_fd);
+	close(in_fd);
+	return err;
+}
+
+static int write_proc_map(const char *path, const char *text)
+{
+	int fd = open(path, O_WRONLY);
+	ssize_t len = strlen(text);
+	ssize_t n;
+
+	if (fd < 0)
+		return -1;
+	n = write(fd, text, len);
+	close(fd);
+	return n == len ? 0 : -1;
+}
+
+static int enter_private_userns(void)
+{
+	uid_t uid = getuid();
+	gid_t gid = getgid();
+	char map_buf[64];
+
+	if (unshare(CLONE_NEWUSER) < 0)
+		return -1;
+
+	/* Required before writing gid_map in unprivileged user namespaces. */
+	if (write_proc_map("/proc/self/setgroups", "deny\n") < 0)
+		return -1;
+	snprintf(map_buf, sizeof(map_buf), "0 %u 1\n", uid);
+	if (write_proc_map("/proc/self/uid_map", map_buf) < 0)
+		return -1;
+	snprintf(map_buf, sizeof(map_buf), "0 %u 1\n", gid);
+	if (write_proc_map("/proc/self/gid_map", map_buf) < 0)
+		return -1;
+
+	/*
+	 * Create a private mount namespace owned by the new user namespace so
+	 * nsinfo__mountns_exit() has CAP_SYS_ADMIN to restore nc->oldns.
+	 */
+	if (unshare(CLONE_NEWNS) < 0)
+		return -1;
+
+	return 0;
+}
+
+static int test_chroot_resolution(const char *chroot_dir, const char *chroot_bin,
+				  bool has_exe_bid, const struct build_id *exe_bid)
+{
+	int cpipe[2] = { -1, -1 };
+	struct nsinfo *cnsi = NULL;
+	struct dso *chroot_dso = NULL;
+	char *resolved = NULL;
+	struct build_id ns_bid;
+	struct stat st1, st2;
+	pid_t cpid = -1;
+	char sig = 0;
+	int ret = -1;
+
+	if (geteuid() != 0 || access("/chroot_elf", F_OK) == 0)
+		return 0;
+
+	if (pipe(cpipe) < 0)
+		return -1;
+
+	cpid = fork();
+	if (cpid < 0)
+		goto out;
+
+	if (cpid == 0) {
+		close(cpipe[0]);
+		if (chroot(chroot_dir) < 0 || chdir("/") < 0) {
+			sig = 'S';
+			if (write(cpipe[1], &sig, 1) != 1)
+				_exit(1);
+			_exit(CHILD_EXIT_SKIP);
+		}
+		sig = '1';
+		if (write(cpipe[1], &sig, 1) != 1)
+			_exit(1);
+		pause();
+		_exit(0);
+	}
+
+	close(cpipe[1]);
+	cpipe[1] = -1;
+
+	if (read(cpipe[0], &sig, 1) != 1 || sig != '1') {
+		ret = sig == 'S' ? 0 : -1;
+		goto out;
+	}
+
+	cnsi = nsinfo__new_thread(cpid, cpid);
+	chroot_dso = dso__new("/chroot_elf");
+	if (!chroot_dso || !cnsi)
+		goto out;
+
+	dso__set_nsinfo(chroot_dso, nsinfo__get(cnsi));
+	resolved = dso__filename_with_chroot(chroot_dso, "/chroot_elf");
+	if (!resolved ||
+	    stat(resolved, &st1) < 0 ||
+	    stat(chroot_bin, &st2) < 0 ||
+	    st1.st_dev != st2.st_dev ||
+	    st1.st_ino != st2.st_ino) {
+		pr_debug("chroot resolution mismatch: %s vs %s\n",
+			 resolved ? resolved : "(null)", chroot_bin);
+		goto out;
+	}
+	zfree(&resolved);
+
+	if (has_exe_bid) {
+		memset(&ns_bid, 0, sizeof(ns_bid));
+		if (filename__read_build_id_ns("/chroot_elf", &ns_bid, cnsi) <= 0 ||
+		    memcmp(exe_bid, &ns_bid, sizeof(*exe_bid)) != 0) {
+			pr_debug("chroot build-id mismatch\n");
+			goto out;
+		}
+	}
+
+	kill(cpid, SIGKILL);
+	waitpid(cpid, NULL, 0);
+	cpid = -1;
+
+	resolved = dso__filename_with_chroot(chroot_dso, "/chroot_elf");
+	if (resolved != NULL) {
+		pr_debug("exited chroot task resolved /proc/<pid>/root\n");
+		goto out;
+	}
+
+	ret = 0;
+out:
+	if (cpid > 0) {
+		kill(cpid, SIGKILL);
+		waitpid(cpid, NULL, 0);
+	}
+	if (cpipe[0] >= 0)
+		close(cpipe[0]);
+	if (cpipe[1] >= 0)
+		close(cpipe[1]);
+	free(resolved);
+	dso__put(chroot_dso);
+	nsinfo__put(cnsi);
+	return ret;
+}
+
+static int run_mountns_and_chroot_tests(void)
+{
+	char tmpdir[] = "/tmp/perf-test-mntns-XXXXXX";
+	char exe_path[PATH_MAX], target_bin[PATH_MAX];
+	char chroot_dir[PATH_MAX], chroot_bin[PATH_MAX];
+	struct build_id exe_bid = { .size = 0 }, ns_bid = { .size = 0 };
+	int to_child[2] = { -1, -1 }, to_parent[2] = { -1, -1 };
+	struct machine machine;
+	struct nscookie nsc;
+	struct dso *dso = NULL;
+	struct nsinfo *nsi = NULL;
+	bool has_exe_bid = false;
+	pid_t child = -1;
+	char sig = 0;
+	u8 magic[4];
+	int status;
+	int ret = TEST_FAIL;
+
+	if (geteuid() != 0 && enter_private_userns() < 0) {
+		pr_debug("SKIP: user namespace not permitted\n");
+		return TEST_SKIP;
+	}
+
+	if (mkdtemp(tmpdir) == NULL)
+		return TEST_FAIL;
+
+	perf_exe(exe_path, sizeof(exe_path));
+	has_exe_bid = filename__read_build_id(exe_path, &exe_bid) > 0;
+	snprintf(target_bin, sizeof(target_bin), "%s/testbin", tmpdir);
+	snprintf(chroot_dir, sizeof(chroot_dir), "%s/chroot", tmpdir);
+	snprintf(chroot_bin, sizeof(chroot_bin), "%s/chroot/chroot_elf", tmpdir);
+
+	if (mkdir(chroot_dir, 0700) < 0 ||
+	    copy_file_mode(exe_path, chroot_bin, 0700) < 0)
+		goto out_rmdir;
+
+	if (pipe(to_child) < 0 || pipe(to_parent) < 0)
+		goto out_rmdir;
+
+	child = fork();
+	if (child < 0)
+		goto out_rmdir;
+
+	if (child == 0) {
+		char cmd = 0;
+
+		close(to_child[1]);
+		close(to_parent[0]);
+
+		if (unshare(CLONE_NEWNS) < 0 ||
+		    mount(NULL, "/", NULL, MS_REC | MS_SLAVE, NULL) < 0 ||
+		    mount("tmpfs", tmpdir, "tmpfs", 0, NULL) < 0 ||
+		    copy_file_mode(exe_path, target_bin, 0700) < 0) {
+			sig = 'S';
+			if (write(to_parent[1], &sig, 1) != 1)
+				_exit(1);
+			_exit(CHILD_EXIT_SKIP);
+		}
+
+		sig = '1';
+		if (write(to_parent[1], &sig, 1) != 1)
+			_exit(1);
+
+		/* Phase 1.5: overmount /proc if permitted to simulate container /proc. */
+		if (read(to_child[0], &cmd, 1) != 1)
+			_exit(1);
+		if (mount("tmpfs", "/proc", "tmpfs", 0, NULL) == 0)
+			sig = 'p';
+		else
+			sig = 's';
+		if (write(to_parent[1], &sig, 1) != 1)
+			_exit(1);
+
+		/* Phase 2: switch to another mount namespace to test inode check. */
+		if (read(to_child[0], &cmd, 1) != 1)
+			_exit(1);
+		if (unshare(CLONE_NEWNS) < 0) {
+			sig = 'S';
+			if (write(to_parent[1], &sig, 1) != 1)
+				_exit(1);
+			_exit(CHILD_EXIT_SKIP);
+		}
+		sig = '2';
+		if (write(to_parent[1], &sig, 1) != 1)
+			_exit(1);
+
+		/* Wait until parent releases us. */
+		if (read(to_child[0], &cmd, 1) < 0)
+			_exit(1);
+		_exit(0);
+	}
+
+	close(to_child[0]);
+	to_child[0] = -1;
+	close(to_parent[1]);
+	to_parent[1] = -1;
+
+	if (read(to_parent[0], &sig, 1) != 1)
+		goto out_wait;
+	if (sig == 'S') {
+		pr_debug("SKIP: mount namespace operations not permitted\n");
+		ret = TEST_SKIP;
+		goto out_wait;
+	}
+	if (sig != '1')
+		goto out_wait;
+
+	/* File in child's tmpfs must not be visible on the host filesystem. */
+	if (access(target_bin, F_OK) == 0) {
+		pr_debug("child mount leaked to host namespace\n");
+		goto out_wait;
+	}
+
+	nsi = nsinfo__new_thread(child, child);
+	if (!nsi)
+		goto out_wait;
+	if (!nsinfo__need_setns(nsi) || nsinfo__mntns_ino(nsi) == 0) {
+		pr_debug("nsinfo did not detect differing mount namespace or inode\n");
+		goto out_wait;
+	}
+
+	if (has_exe_bid) {
+		if (filename__read_build_id_ns(target_bin, &ns_bid, nsi) <= 0 ||
+		    memcmp(&exe_bid, &ns_bid, sizeof(exe_bid)) != 0) {
+			pr_debug("failed to read build-id inside child mountns\n");
+			goto out_wait;
+		}
+	}
+
+	memset(&machine, 0, sizeof(machine));
+	if (machine__init(&machine, "", HOST_KERNEL_ID) != 0)
+		goto out_wait;
+
+	dso = machine__findnew_dso(&machine, target_bin);
+	if (!dso) {
+		machine__exit(&machine);
+		goto out_wait;
+	}
+	dso__set_nsinfo(dso, nsinfo__get(nsi));
+#ifndef HAVE_LIBELF_SUPPORT
+	/* Without libelf, filename__has_section() cannot auto-detect binary_type. */
+	dso__set_binary_type(dso, DSO_BINARY_TYPE__SYSTEM_PATH_DSO);
+	dso__set_bintype_searched(dso);
+#endif
+	if (dso__data_read_offset(dso, &machine, 0, magic, sizeof(magic)) != sizeof(magic) ||
+	    memcmp(magic, "\x7f" "ELF", 4) != 0) {
+		pr_debug("failed to read ELF header from binary inside mountns\n");
+		dso__data_close(dso);
+		dso__put(dso);
+		dso = NULL;
+		machine__exit(&machine);
+		goto out_wait;
+	}
+	dso__data_close(dso);
+	dso__put(dso);
+	dso = NULL;
+
+	{
+		struct thread *cth = machine__findnew_thread(&machine, child, child);
+		struct map *map = NULL;
+		struct dso *ldso;
+
+		if (!cth) {
+			machine__exit(&machine);
+			goto out_wait;
+		}
+		map = map__new(&machine, 0x100000, 0xffffffff, 0, &dso_id_empty,
+			       PROT_EXEC, /*flags=*/0, target_bin, cth);
+		thread__put(cth);
+		if (!map) {
+			machine__exit(&machine);
+			goto out_wait;
+		}
+		ldso = map__dso(map);
+		if (dso__load(ldso, map) < 0 ||
+		    (has_exe_bid && (!dso__has_build_id(ldso) ||
+				     !dso__build_id_equal(ldso, &exe_bid)))) {
+			pr_debug("dso__load() failed inside child mountns\n");
+			map__put(map);
+			machine__exit(&machine);
+			goto out_wait;
+		}
+		map__put(map);
+	}
+	machine__exit(&machine);
+
+	/* Test ENOENT + nsinfo__check_proc() when child mountns overmounts /proc. */
+	sig = 'p';
+	if (write(to_child[1], &sig, 1) != 1 || read(to_parent[0], &sig, 1) != 1)
+		goto out_wait;
+	if (sig == 'p') {
+		bool check_ok;
+		char *resolved;
+
+		if (filename__read_build_id_ns("/nonexistent_proc_overmount_test",
+					       &ns_bid, nsi) >= 0) {
+			pr_debug("filename__read_build_id_ns() unexpectedly succeeded\n");
+			goto out_wait;
+		}
+		if (nsinfo__exited(nsi) || !nsinfo__need_setns(nsi) ||
+		    !nsinfo__check_proc(nsi)) {
+			pr_debug("overmounted /proc inside mountns falsely marked nsinfo exited\n");
+			goto out_wait;
+		}
+		nsinfo__mountns_enter(nsi, &nsc);
+		check_ok = nsinfo__check_proc(nsi);
+		nsinfo__mountns_exit(&nsc);
+		if (!check_ok || nsinfo__exited(nsi) ||
+		    !nsinfo__need_setns(nsi)) {
+			pr_debug("nsinfo__check_proc() failed after entering overmounted /proc\n");
+			goto out_wait;
+		}
+		dso = dso__new("/nonexistent_proc_overmount_test");
+		if (!dso)
+			goto out_wait;
+		dso__set_nsinfo(dso, nsinfo__get(nsi));
+		resolved = dso__filename_with_chroot(dso, "/nonexistent_proc_overmount_test");
+		dso__put(dso);
+		dso = NULL;
+		if (resolved != NULL) {
+			pr_debug("unexpected chroot prefix with overmounted /proc: %s\n",
+				 resolved);
+			free(resolved);
+			goto out_wait;
+		}
+		if (nsinfo__exited(nsi) || !nsinfo__need_setns(nsi)) {
+			pr_debug("dso__filename_with_chroot() falsely marked nsinfo exited\n");
+			goto out_wait;
+		}
+	}
+
+	/* Have the child unshare again so mntns_ino changes while staying alive. */
+	sig = 'n';
+	if (write(to_child[1], &sig, 1) != 1 || read(to_parent[0], &sig, 1) != 1)
+		goto out_wait;
+	if (sig == 'S') {
+		ret = TEST_SKIP;
+		goto out_wait;
+	}
+
+	nsinfo__mountns_enter(nsi, &nsc);
+	nsinfo__mountns_exit(&nsc);
+	if (nsinfo__need_setns(nsi) || nsinfo__exited(nsi) || !nsinfo__check_proc(nsi)) {
+		pr_debug("mntns_ino change should clear need_setns without setting exited\n");
+		goto out_wait;
+	}
+
+	sig = 'q';
+	if (write(to_child[1], &sig, 1) != 1)
+		goto out_wait;
+	waitpid(child, &status, 0);
+	child = -1;
+
+	/* Once reaped, nsinfo__check_proc() must detect exit. */
+	if (nsinfo__check_proc(nsi) || !nsinfo__exited(nsi)) {
+		pr_debug("exited child still passed nsinfo__check_proc()\n");
+		goto out_wait;
+	}
+
+	/* Also verify chroot resolution when chroot(2) is permitted. */
+	if (test_chroot_resolution(chroot_dir, chroot_bin, has_exe_bid, &exe_bid) < 0)
+		goto out_wait;
+
+	ret = TEST_OK;
+out_wait:
+	if (child > 0) {
+		kill(child, SIGKILL);
+		waitpid(child, &status, 0);
+	}
+	nsinfo__put(nsi);
+	dso__put(dso);
+out_rmdir:
+	if (to_child[0] >= 0)
+		close(to_child[0]);
+	if (to_child[1] >= 0)
+		close(to_child[1]);
+	if (to_parent[0] >= 0)
+		close(to_parent[0]);
+	if (to_parent[1] >= 0)
+		close(to_parent[1]);
+	unlink(chroot_bin);
+	rmdir(chroot_dir);
+	unlink(target_bin);
+	rmdir(tmpdir);
+	return ret;
+}
+
+static int test__nsinfo_mountns(struct test_suite *test __maybe_unused,
+				int subtest __maybe_unused)
+{
+	int status;
+	pid_t pid;
+
+	if (geteuid() == 0)
+		return run_mountns_and_chroot_tests();
+
+	/* Run in a dedicated child so unshare(CLONE_NEWUSER) never mutates the caller. */
+	pid = fork();
+	if (pid < 0)
+		return TEST_FAIL;
+	if (pid == 0) {
+		int rc = run_mountns_and_chroot_tests();
+
+		if (rc == TEST_SKIP)
+			_exit(CHILD_EXIT_SKIP);
+		_exit(rc == TEST_OK ? 0 : 1);
+	}
+
+	if (waitpid(pid, &status, 0) != pid)
+		return TEST_FAIL;
+	if (!WIFEXITED(status))
+		return TEST_FAIL;
+	if (WEXITSTATUS(status) == CHILD_EXIT_SKIP)
+		return TEST_SKIP;
+	return WEXITSTATUS(status) == 0 ? TEST_OK : TEST_FAIL;
+}
+
+static struct test_case tests__namespaces[] = {
+	TEST_CASE("boot_id", nsinfo_boot_id),
+	TEST_CASE("init_and_copy", nsinfo_init_and_copy),
+	TEST_CASE("proc_and_recycled_tid", nsinfo_proc_and_recycled_tid),
+	TEST_CASE("thread_liveness", nsinfo_thread_liveness),
+	TEST_CASE("dso_needs_ns", nsinfo_dso_needs_ns),
+	TEST_CASE("mountns_and_chroot", nsinfo_mountns),
+	{ .name = NULL, }
+};
+
+struct test_suite suite__namespaces = {
+	.desc = "Namespace and nsinfo tests",
+	.test_cases = tests__namespaces,
+};
diff --git a/tools/perf/tests/shell/header.sh b/tools/perf/tests/shell/header.sh
index e1628ac0a61401a7..ae3cd2b50d1c3437 100755
--- a/tools/perf/tests/shell/header.sh
+++ b/tools/perf/tests/shell/header.sh
@@ -37,6 +37,7 @@ check_header_output() {
     "sibling (cores|dies|threads)"
     "sibling threads"
     "total memory"
+    "boot id"
   )
   for i in "${fields[@]}"
   do
@@ -46,6 +47,14 @@ check_header_output() {
       err=1
     fi
   done
+
+  if [ -r /proc/sys/kernel/random/boot_id ]; then
+    host_boot_id=$(tr -d '\n' < /proc/sys/kernel/random/boot_id)
+    if ! grep -q "boot id : ${host_boot_id}" "${script_output}"; then
+      echo "Failed to find matching host boot id ${host_boot_id} in output"
+      err=1
+    fi
+  fi
 }
 
 test_file() {
diff --git a/tools/perf/tests/shell/pipe_test.sh b/tools/perf/tests/shell/pipe_test.sh
index f4eb129afda2040f..9552bdf904656879 100755
--- a/tools/perf/tests/shell/pipe_test.sh
+++ b/tools/perf/tests/shell/pipe_test.sh
@@ -115,11 +115,32 @@ test_inject_bids() {
   echo "Inject ${inject_opt} build-ids test [Success]"
 }
 
+test_inject_boot_id() {
+  echo
+  echo "Inject boot-id pipe test"
+
+  if [ ! -r /proc/sys/kernel/random/boot_id ]; then
+    echo "Inject boot-id pipe test [Skipped - no /proc/sys/kernel/random/boot_id]"
+    return
+  fi
+
+  boot_id=$(cat /proc/sys/kernel/random/boot_id)
+  if ! perf record -e task-clock:u -o - ${prog} | perf inject -b | perf report -i - --header-only -I | grep -q "boot id : ${boot_id}"
+  then
+    echo "Inject boot-id pipe test [Failed - boot_id mismatch in pipe]"
+    err=1
+    return
+  fi
+
+  echo "Inject boot-id pipe test [Success]"
+}
+
 test_record_report
 test_inject_bids -B
 test_inject_bids -b
 test_inject_bids --buildid-all
 test_inject_bids --mmap2-buildid-all
+test_inject_boot_id
 
 cleanup
 exit $err
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index af822885890dda73..b75fc1ef1c13bdd2 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -203,6 +203,7 @@ DECLARE_SUITE(util);
 DECLARE_SUITE(uncore_event_sorting);
 DECLARE_SUITE(subcmd_help);
 DECLARE_SUITE(kallsyms_split);
+DECLARE_SUITE(namespaces);
 
 /*
  * PowerPC and S390 do not support creation of instruction breakpoints using the
-- 
2.56.1.809.ga0b61035b5-goog


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2026-11-23 23:20 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23 23:20 [PATCH v3 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
2026-11-23 23:20 ` [PATCH v3 01/11] perf tools: Add HEADER_BOOT_ID feature Namhyung Kim
2026-11-23 23:20 ` [PATCH v3 02/11] perf tools: Add perf_env__is_same_boot() Namhyung Kim
2026-11-23 23:20 ` [PATCH v3 03/11] perf tools: Add nsinfo__new_thread() to track tid Namhyung Kim
2026-11-23 23:20 ` [PATCH v3 04/11] perf tools: Add lock to struct nsinfo Namhyung Kim
2026-11-23 23:20 ` [PATCH v3 05/11] perf tools: Defer /proc lookup until nsinfo is used Namhyung Kim
2026-11-23 23:20 ` [PATCH v3 06/11] perf tools: Verify thread status and mount namespace inode Namhyung Kim
2026-11-23 23:20 ` [PATCH v3 07/11] perf tools: Check thread liveness when accessing /proc Namhyung Kim
2026-11-23 23:20 ` [PATCH v3 08/11] perf tools: Export filename__read_build_id_ns() Namhyung Kim
2026-11-23 23:20 ` [PATCH v3 09/11] perf tools: Only enter mount namespace if necessary Namhyung Kim
2026-11-23 23:20 ` [PATCH v3 10/11] perf tools: Scope mount namespace entry in dso__load() Namhyung Kim
2026-11-23 23:20 ` [PATCH v3 11/11] perf tests: Add namespace, nsinfo, and boot ID tests Namhyung Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®