Linux Perf Users
 help / color / mirror / Atom feed
* [PATCH v2 00/11] perf tools: Enhance namespace and chroot handling
@ 2026-11-16  5:57 Namhyung Kim
  2026-11-16  5:57 ` [PATCH v2 01/11] perf tools: Add HEADER_BOOT_ID feature Namhyung Kim
                   ` (10 more replies)
  0 siblings, 11 replies; 14+ messages in thread
From: Namhyung Kim @ 2026-11-16  5:57 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.

v2 changes:

 * 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                   |   15 +-
 tools/perf/builtin-trace.c                    |    2 +-
 tools/perf/tests/Build                        |    1 +
 tools/perf/tests/builtin-test.c               |    1 +
 tools/perf/tests/namespaces.c                 | 1035 +++++++++++++++++
 tools/perf/tests/shell/header.sh              |    9 +
 tools/perf/tests/tests.h                      |    1 +
 tools/perf/util/build-id.c                    |   14 +-
 tools/perf/util/capstone.c                    |    8 +-
 tools/perf/util/debuginfo.c                   |   19 +-
 tools/perf/util/disasm.c                      |    8 +-
 tools/perf/util/dso.c                         |   59 +-
 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                  |  273 ++++-
 tools/perf/util/namespaces.h                  |   10 +
 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                      |   95 +-
 tools/perf/util/thread.h                      |   13 +-
 31 files changed, 1651 insertions(+), 144 deletions(-)
 create mode 100644 tools/perf/tests/namespaces.c

-- 
2.56.0.611.g1030f0b940-goog


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

* [PATCH v2 01/11] perf tools: Add HEADER_BOOT_ID feature
  2026-11-16  5:57 [PATCH v2 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
@ 2026-11-16  5:57 ` Namhyung Kim
  2026-11-16  6:09   ` sashiko-bot
  2026-11-16  5:57 ` [PATCH v2 02/11] perf tools: Add perf_env__is_same_boot() Namhyung Kim
                   ` (9 subsequent siblings)
  10 siblings, 1 reply; 14+ messages in thread
From: Namhyung Kim @ 2026-11-16  5:57 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 50993b0dedc577c7..24de685486af9bd5 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);
@@ -400,6 +402,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 23d35425efe38b24..aafd213c7523c018 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..3e0b6fce55973dd1 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__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 a58b4fe44b62e3bb..578abcb14de84a39 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.0.611.g1030f0b940-goog


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

* [PATCH v2 02/11] perf tools: Add perf_env__is_same_boot()
  2026-11-16  5:57 [PATCH v2 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
  2026-11-16  5:57 ` [PATCH v2 01/11] perf tools: Add HEADER_BOOT_ID feature Namhyung Kim
@ 2026-11-16  5:57 ` Namhyung Kim
  2026-11-16  6:02   ` sashiko-bot
  2026-11-16  5:57 ` [PATCH v2 03/11] perf tools: Add nsinfo__new_thread() to track tid Namhyung Kim
                   ` (8 subsequent siblings)
  10 siblings, 1 reply; 14+ messages in thread
From: Namhyung Kim @ 2026-11-16  5:57 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 24de685486af9bd5..91ab4ccaf5a9475c 100644
--- a/tools/perf/util/env.c
+++ b/tools/perf/util/env.c
@@ -444,6 +444,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 aafd213c7523c018..856385624cee6adf 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.0.611.g1030f0b940-goog


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

* [PATCH v2 03/11] perf tools: Add nsinfo__new_thread() to track tid
  2026-11-16  5:57 [PATCH v2 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
  2026-11-16  5:57 ` [PATCH v2 01/11] perf tools: Add HEADER_BOOT_ID feature Namhyung Kim
  2026-11-16  5:57 ` [PATCH v2 02/11] perf tools: Add perf_env__is_same_boot() Namhyung Kim
@ 2026-11-16  5:57 ` Namhyung Kim
  2026-11-16  5:57 ` [PATCH v2 04/11] perf tools: Add lock to struct nsinfo Namhyung Kim
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Namhyung Kim @ 2026-11-16  5:57 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 5b4550e97172db54..ac857b395882f569 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"
@@ -516,6 +517,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 91118c301913b834..d12eb6bc63313b00 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.0.611.g1030f0b940-goog


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

* [PATCH v2 04/11] perf tools: Add lock to struct nsinfo
  2026-11-16  5:57 [PATCH v2 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
                   ` (2 preceding siblings ...)
  2026-11-16  5:57 ` [PATCH v2 03/11] perf tools: Add nsinfo__new_thread() to track tid Namhyung Kim
@ 2026-11-16  5:57 ` Namhyung Kim
  2026-11-16  5:57 ` [PATCH v2 05/11] perf tools: Defer /proc lookup until nsinfo is used Namhyung Kim
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Namhyung Kim @ 2026-11-16  5:57 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 | 106 +++++++++++++++++++++++++++--------
 tools/perf/util/namespaces.h |   2 +
 2 files changed, 86 insertions(+), 22 deletions(-)

diff --git a/tools/perf/util/namespaces.c b/tools/perf/util/namespaces.c
index f426eba04d6ef449..845a62045405c80a 100644
--- a/tools/perf/util/namespaces.c
+++ b/tools/perf/util/namespaces.c
@@ -94,7 +94,7 @@ 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 int nsinfo__init_locked(struct nsinfo *nsi)
 {
 	char oldns[PATH_MAX];
 	char spath[PATH_MAX];
@@ -140,14 +140,29 @@ int nsinfo__init(struct nsinfo *nsi)
 	return rv;
 }
 
+int nsinfo__init(struct nsinfo *nsi)
+{
+	int rv;
+
+	if (!nsi)
+		return -1;
+
+	mutex_lock(&RC_CHK_ACCESS(nsi)->lock);
+	rv = nsinfo__init_locked(nsi);
+	mutex_unlock(&RC_CHK_ACCESS(nsi)->lock);
+	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;
 }
@@ -184,9 +199,9 @@ struct nsinfo *nsinfo__new(pid_t pid)
 	return nsinfo__new_thread(pid, pid);
 }
 
-static const char *nsinfo__mntns_path(const struct nsinfo *nsi)
+static struct mutex *nsinfo__lock(const struct nsinfo *nsi)
 {
-	return RC_CHK_ACCESS(nsi)->mntns_path;
+	return (struct mutex *)&RC_CHK_ACCESS(nsi)->lock;
 }
 
 struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
@@ -200,19 +215,22 @@ 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));
+	mutex_lock(nsinfo__lock(nsi));
+	RC_CHK_ACCESS(nnsi)->pid = RC_CHK_ACCESS(nsi)->pid;
+	RC_CHK_ACCESS(nnsi)->tid = RC_CHK_ACCESS(nsi)->tid;
+	RC_CHK_ACCESS(nnsi)->tgid = RC_CHK_ACCESS(nsi)->tgid;
+	RC_CHK_ACCESS(nnsi)->nstgid = RC_CHK_ACCESS(nsi)->nstgid;
+	RC_CHK_ACCESS(nnsi)->need_setns = RC_CHK_ACCESS(nsi)->need_setns;
+	RC_CHK_ACCESS(nnsi)->in_pidns = RC_CHK_ACCESS(nsi)->in_pidns;
+	if (RC_CHK_ACCESS(nsi)->mntns_path) {
+		RC_CHK_ACCESS(nnsi)->mntns_path = strdup(RC_CHK_ACCESS(nsi)->mntns_path);
 		if (!RC_CHK_ACCESS(nnsi)->mntns_path) {
+			mutex_unlock(nsinfo__lock(nsi));
 			nsinfo__put(nnsi);
 			return NULL;
 		}
 	}
+	mutex_unlock(nsinfo__lock(nsi));
 
 	return nnsi;
 }
@@ -227,6 +245,7 @@ static void nsinfo__delete(struct nsinfo *nsi)
 	if (nsi) {
 		WARN_ONCE(refcount_read(nsinfo__refcnt(nsi)) != 0, "nsinfo refcnt unbalanced\n");
 		zfree(&RC_CHK_ACCESS(nsi)->mntns_path);
+		mutex_destroy(&RC_CHK_ACCESS(nsi)->lock);
 		RC_CHK_FREE(nsi);
 	}
 }
@@ -251,22 +270,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 +324,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(&RC_CHK_ACCESS(nsi)->lock);
 	RC_CHK_ACCESS(nsi)->in_pidns = true;
+	mutex_unlock(&RC_CHK_ACCESS(nsi)->lock);
 }
 
 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 +354,36 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
 	nc->oldns = -1;
 	nc->newns = -1;
 
-	if (!nsi || !nsinfo__need_setns(nsi))
+	if (!nsi)
+		return;
+
+	mutex_lock(&RC_CHK_ACCESS(nsi)->lock);
+	if (RC_CHK_ACCESS(nsi)->need_setns && RC_CHK_ACCESS(nsi)->mntns_path)
+		mntns_path = strdup(RC_CHK_ACCESS(nsi)->mntns_path);
+	mutex_unlock(&RC_CHK_ACCESS(nsi)->lock);
+
+	if (!mntns_path)
 		return;
 
 	if (!getcwd(curpath, sizeof(curpath)))
-		return;
+		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 +395,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..bbb47a6f9e8323f2 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,6 +32,7 @@ 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;
-- 
2.56.0.611.g1030f0b940-goog


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

* [PATCH v2 05/11] perf tools: Defer /proc lookup until nsinfo is used
  2026-11-16  5:57 [PATCH v2 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
                   ` (3 preceding siblings ...)
  2026-11-16  5:57 ` [PATCH v2 04/11] perf tools: Add lock to struct nsinfo Namhyung Kim
@ 2026-11-16  5:57 ` Namhyung Kim
  2026-11-16  5:57 ` [PATCH v2 06/11] perf tools: Verify thread status and mount namespace inode Namhyung Kim
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Namhyung Kim @ 2026-11-16  5:57 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 | 24 +++++++++++++++---------
 tools/perf/util/namespaces.h |  1 +
 tools/perf/util/thread.c     | 32 ++++++++++++++++++++++++++++++++
 tools/perf/util/thread.h     |  6 ++----
 7 files changed, 53 insertions(+), 16 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 d5319ffa88a7477c..91c1fbeff9d2446f 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 845a62045405c80a..afd8ad7f843ec09d 100644
--- a/tools/perf/util/namespaces.c
+++ b/tools/perf/util/namespaces.c
@@ -103,6 +103,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;
 
@@ -153,6 +155,12 @@ int nsinfo__init(struct nsinfo *nsi)
 	return rv;
 }
 
+static void nsinfo__ensure_init_locked(struct nsinfo *nsi)
+{
+	if (!RC_CHK_ACCESS(nsi)->inited)
+		nsinfo__init_locked(nsi);
+}
+
 static struct nsinfo *nsinfo__alloc(void)
 {
 	struct nsinfo *res;
@@ -182,15 +190,6 @@ struct nsinfo *nsinfo__new_thread(pid_t pid, pid_t tid)
 	RC_CHK_ACCESS(nsi)->tid = tid;
 	RC_CHK_ACCESS(nsi)->tgid = pid;
 	RC_CHK_ACCESS(nsi)->nstgid = pid;
-	nsinfo__clear_need_setns(nsi);
-	RC_CHK_ACCESS(nsi)->in_pidns = false;
-	/* 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;
 }
 
@@ -220,6 +219,7 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
 	RC_CHK_ACCESS(nnsi)->tid = RC_CHK_ACCESS(nsi)->tid;
 	RC_CHK_ACCESS(nnsi)->tgid = RC_CHK_ACCESS(nsi)->tgid;
 	RC_CHK_ACCESS(nnsi)->nstgid = RC_CHK_ACCESS(nsi)->nstgid;
+	RC_CHK_ACCESS(nnsi)->inited = RC_CHK_ACCESS(nsi)->inited;
 	RC_CHK_ACCESS(nnsi)->need_setns = RC_CHK_ACCESS(nsi)->need_setns;
 	RC_CHK_ACCESS(nnsi)->in_pidns = RC_CHK_ACCESS(nsi)->in_pidns;
 	if (RC_CHK_ACCESS(nsi)->mntns_path) {
@@ -276,6 +276,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;
@@ -287,6 +288,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));
@@ -297,6 +299,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;
@@ -307,6 +310,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;
@@ -327,6 +331,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;
@@ -358,6 +363,7 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
 		return;
 
 	mutex_lock(&RC_CHK_ACCESS(nsi)->lock);
+	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(&RC_CHK_ACCESS(nsi)->lock);
diff --git a/tools/perf/util/namespaces.h b/tools/perf/util/namespaces.h
index bbb47a6f9e8323f2..42dcca13b002f153 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;
 	pid_t			nstgid;
+	bool			inited;
 	bool			need_setns;
 	bool			in_pidns;
 	char			*mntns_path;
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index d12eb6bc63313b00..5d1c886add3e61eb 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -191,6 +191,34 @@ static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
 	return 0;
 }
 
+static void __thread__reset_nsinfo(struct thread *thread)
+	EXCLUSIVE_LOCKS_REQUIRED(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)
 {
@@ -198,6 +226,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;
 }
@@ -282,6 +312,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 d82fce8173aefedc..36f2932422df8988 100644
--- a/tools/perf/util/thread.h
+++ b/tools/perf/util/thread.h
@@ -286,10 +286,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.0.611.g1030f0b940-goog


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

* [PATCH v2 06/11] perf tools: Verify thread status and mount namespace inode
  2026-11-16  5:57 [PATCH v2 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
                   ` (4 preceding siblings ...)
  2026-11-16  5:57 ` [PATCH v2 05/11] perf tools: Defer /proc lookup until nsinfo is used Namhyung Kim
@ 2026-11-16  5:57 ` Namhyung Kim
  2026-11-16  5:57 ` [PATCH v2 07/11] perf tools: Check thread liveness when accessing /proc Namhyung Kim
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Namhyung Kim @ 2026-11-16  5:57 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.

Also record the mount namespace inode (mntns_ino) during initialization
and verify via fstat() that the opened namespace file descriptor still
refers to the same mount namespace.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/namespaces.c | 139 +++++++++++++++++++++++++++--------
 tools/perf/util/namespaces.h |   4 +
 2 files changed, 114 insertions(+), 29 deletions(-)

diff --git a/tools/perf/util/namespaces.c b/tools/perf/util/namespaces.c
index afd8ad7f843ec09d..860b90e8384ed1f2 100644
--- a/tools/perf/util/namespaces.c
+++ b/tools/perf/util/namespaces.c
@@ -65,6 +65,7 @@ static int nsinfo__get_nspid(pid_t *tgid, pid_t *nstgid, bool *in_pidns, const c
 	char *statln = NULL;
 	size_t linesz = 0;
 	char *nspid;
+	bool got_tgid = false;
 
 	f = fopen(path, "r");
 	if (f == NULL)
@@ -75,6 +76,7 @@ static int nsinfo__get_nspid(pid_t *tgid, pid_t *nstgid, bool *in_pidns, const c
 		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 +93,20 @@ 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 void nsinfo__clear_need_setns_locked(struct nsinfo *nsi)
+{
+	RC_CHK_ACCESS(nsi)->inited = true;
+	RC_CHK_ACCESS(nsi)->need_setns = false;
+	zfree(&RC_CHK_ACCESS(nsi)->mntns_path);
+}
+
+static void nsinfo__set_exited_locked(struct nsinfo *nsi)
+{
+	RC_CHK_ACCESS(nsi)->exited = true;
+	nsinfo__clear_need_setns_locked(nsi);
 }
 
 static int nsinfo__init_locked(struct nsinfo *nsi)
@@ -101,16 +116,41 @@ static int nsinfo__init_locked(struct nsinfo *nsi)
 	char *newns = NULL;
 	struct stat old_stat;
 	struct stat new_stat;
+	pid_t tgid = 0, nstgid = 0;
+	bool in_pidns = false;
 	int rv = -1;
 
+	if (RC_CHK_ACCESS(nsi)->exited)
+		return -1;
+
 	RC_CHK_ACCESS(nsi)->inited = true;
 
+	/*
+	 * 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 (snprintf(spath, PATH_MAX, "/proc/%d/task/%d/status",
+		     nsinfo__pid(nsi), nsinfo__tid(nsi)) >= PATH_MAX)
+		goto out;
+
+	rv = nsinfo__get_nspid(&tgid, &nstgid, &in_pidns, spath);
+	if (rv < 0 || tgid != nsinfo__pid(nsi)) {
+		nsinfo__set_exited_locked(nsi);
+		rv = -1;
+		goto out;
+	}
+
+	RC_CHK_ACCESS(nsi)->tgid = tgid;
+	RC_CHK_ACCESS(nsi)->nstgid = nstgid;
+	RC_CHK_ACCESS(nsi)->in_pidns = in_pidns;
+
 	if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
-		return rv;
+		return -1;
 
 	if (asprintf(&newns, "/proc/%d/task/%d/ns/mnt",
 		     nsinfo__pid(nsi), nsinfo__tid(nsi)) == -1)
-		return rv;
+		return -1;
 
 	if (stat(oldns, &old_stat) < 0)
 		goto out;
@@ -124,19 +164,10 @@ static int nsinfo__init_locked(struct nsinfo *nsi)
 	if (old_stat.st_ino != new_stat.st_ino) {
 		RC_CHK_ACCESS(nsi)->need_setns = true;
 		RC_CHK_ACCESS(nsi)->mntns_path = newns;
+		RC_CHK_ACCESS(nsi)->mntns_ino = new_stat.st_ino;
 		newns = NULL;
 	}
 
-	/* 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;
@@ -157,7 +188,7 @@ int nsinfo__init(struct nsinfo *nsi)
 
 static void nsinfo__ensure_init_locked(struct nsinfo *nsi)
 {
-	if (!RC_CHK_ACCESS(nsi)->inited)
+	if (!RC_CHK_ACCESS(nsi)->inited && !RC_CHK_ACCESS(nsi)->exited)
 		nsinfo__init_locked(nsi);
 }
 
@@ -219,7 +250,9 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
 	RC_CHK_ACCESS(nnsi)->tid = RC_CHK_ACCESS(nsi)->tid;
 	RC_CHK_ACCESS(nnsi)->tgid = RC_CHK_ACCESS(nsi)->tgid;
 	RC_CHK_ACCESS(nnsi)->nstgid = RC_CHK_ACCESS(nsi)->nstgid;
+	RC_CHK_ACCESS(nnsi)->mntns_ino = RC_CHK_ACCESS(nsi)->mntns_ino;
 	RC_CHK_ACCESS(nnsi)->inited = RC_CHK_ACCESS(nsi)->inited;
+	RC_CHK_ACCESS(nnsi)->exited = RC_CHK_ACCESS(nsi)->exited;
 	RC_CHK_ACCESS(nnsi)->need_setns = RC_CHK_ACCESS(nsi)->need_setns;
 	RC_CHK_ACCESS(nnsi)->in_pidns = RC_CHK_ACCESS(nsi)->in_pidns;
 	if (RC_CHK_ACCESS(nsi)->mntns_path) {
@@ -277,7 +310,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;
 }
@@ -288,12 +321,47 @@ 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)
+{
+	char spath[64];
+	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;
+
+	scnprintf(spath, sizeof(spath), "/proc/%d/task/%d/status",
+		  nsinfo__pid(nsi), nsinfo__tid(nsi));
+	if (nsinfo__get_nspid(&tgid, &nstgid, &in_pidns, spath) < 0 ||
+	    tgid != nsinfo__pid(nsi)) {
+		nsinfo__set_exited(nsi);
+		return false;
+	}
+
+	return true;
+}
+
 pid_t nsinfo__tgid(const struct nsinfo *nsi)
 {
 	pid_t ret;
@@ -349,6 +417,8 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
 {
 	char curpath[PATH_MAX];
 	char *mntns_path = NULL;
+	ino_t mntns_ino = 0;
+	struct stat st;
 	int oldns = -1;
 	int newns = -1;
 	char *oldcwd = NULL;
@@ -358,19 +428,26 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
 
 	nc->oldns = -1;
 	nc->newns = -1;
+	nc->oldcwd = NULL;
 
 	if (!nsi)
 		return;
 
 	mutex_lock(&RC_CHK_ACCESS(nsi)->lock);
 	nsinfo__ensure_init_locked(nsi);
-	if (RC_CHK_ACCESS(nsi)->need_setns && RC_CHK_ACCESS(nsi)->mntns_path)
+	if (!RC_CHK_ACCESS(nsi)->exited && RC_CHK_ACCESS(nsi)->need_setns &&
+	    RC_CHK_ACCESS(nsi)->mntns_path) {
 		mntns_path = strdup(RC_CHK_ACCESS(nsi)->mntns_path);
+		mntns_ino = RC_CHK_ACCESS(nsi)->mntns_ino;
+	}
 	mutex_unlock(&RC_CHK_ACCESS(nsi)->lock);
 
 	if (!mntns_path)
 		return;
 
+	if (!nsinfo__check_proc(nsi))
+		goto out;
+
 	if (!getcwd(curpath, sizeof(curpath)))
 		goto out;
 
@@ -378,14 +455,19 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
 	if (!oldcwd)
 		goto out;
 
-	oldns = open("/proc/self/ns/mnt", O_RDONLY);
+	oldns = open("/proc/self/ns/mnt", O_RDONLY | O_CLOEXEC);
 	if (oldns < 0)
 		goto errout;
 
-	newns = open(mntns_path, O_RDONLY);
+	newns = 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;
 
@@ -407,17 +489,11 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
 
 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;
 	}
@@ -426,6 +502,11 @@ 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);
+	}
 }
 
 char *nsinfo__realpath(const char *path, struct nsinfo *nsi)
diff --git a/tools/perf/util/namespaces.h b/tools/perf/util/namespaces.h
index 42dcca13b002f153..7bba217aa3fde3d7 100644
--- a/tools/perf/util/namespaces.h
+++ b/tools/perf/util/namespaces.h
@@ -37,7 +37,9 @@ DECLARE_RC_STRUCT(nsinfo) {
 	pid_t			tid;
 	pid_t			tgid;
 	pid_t			nstgid;
+	ino_t			mntns_ino;
 	bool			inited;
+	bool			exited;
 	bool			need_setns;
 	bool			in_pidns;
 	char			*mntns_path;
@@ -60,6 +62,8 @@ void nsinfo__put(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);
 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.0.611.g1030f0b940-goog


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

* [PATCH v2 07/11] perf tools: Check thread liveness when accessing /proc
  2026-11-16  5:57 [PATCH v2 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
                   ` (5 preceding siblings ...)
  2026-11-16  5:57 ` [PATCH v2 06/11] perf tools: Verify thread status and mount namespace inode Namhyung Kim
@ 2026-11-16  5:57 ` Namhyung Kim
  2026-11-16  5:57 ` [PATCH v2 08/11] perf tools: Export filename__read_build_id_ns() Namhyung Kim
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Namhyung Kim @ 2026-11-16  5:57 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()), and avoid synthesizing a live parent
thread in thread__e_machine_endian() when a sub-thread is no longer
alive.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 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 ++--
 6 files changed, 86 insertions(+), 13 deletions(-)

diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
index 4094141461f91c32..a21bbf5926617633 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 e46cd614eb21184b..36e6c45dd0383073 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -587,7 +587,12 @@ static int do_open(char *name) EXCLUSIVE_LOCKS_REQUIRED(_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__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 ac857b395882f569..53cda0046d24799c 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -517,8 +517,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;
@@ -1975,6 +1981,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,
@@ -1983,6 +1990,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);
 	}
@@ -2030,9 +2038,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 5d1c886add3e61eb..fda718ea5d7e6276 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;
 }
 
@@ -194,9 +204,17 @@ static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
 static void __thread__reset_nsinfo(struct thread *thread)
 	EXCLUSIVE_LOCKS_REQUIRED(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)
@@ -317,6 +335,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];
@@ -324,6 +375,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) {
@@ -542,7 +596,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,
@@ -569,7 +626,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 36f2932422df8988..927b2ddccfc8433d 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;
-- 
2.56.0.611.g1030f0b940-goog


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

* [PATCH v2 08/11] perf tools: Export filename__read_build_id_ns()
  2026-11-16  5:57 [PATCH v2 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
                   ` (6 preceding siblings ...)
  2026-11-16  5:57 ` [PATCH v2 07/11] perf tools: Check thread liveness when accessing /proc Namhyung Kim
@ 2026-11-16  5:57 ` Namhyung Kim
  2026-11-16  5:57 ` [PATCH v2 09/11] perf tools: Only enter mount namespace if necessary Namhyung Kim
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Namhyung Kim @ 2026-11-16  5:57 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__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  | 14 +++++++++++---
 tools/perf/util/dsos.c      | 17 +++--------------
 tools/perf/util/symbol.h    |  3 +++
 4 files changed, 18 insertions(+), 28 deletions(-)

diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index 05bf1aae61778952..71ebb5b9ab84cbf7 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -894,24 +894,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 02233ef0eafff2c2..ff083a594b405208 100644
--- a/tools/perf/util/build-id.c
+++ b/tools/perf/util/build-id.c
@@ -847,15 +847,23 @@ 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__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 e927e707abace9c6..a97fed55286aefca 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)
@@ -70,7 +70,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))
@@ -79,20 +78,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 6347bd218ad5eac4..7ac362b5d53530b0 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -28,6 +28,7 @@
 struct dso;
 struct map;
 struct maps;
+struct nsinfo;
 struct option;
 struct build_id;
 struct perf_env;
@@ -211,6 +212,8 @@ struct symbol *dso__next_symbol(struct symbol *sym);
 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.0.611.g1030f0b940-goog


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

* [PATCH v2 09/11] perf tools: Only enter mount namespace if necessary
  2026-11-16  5:57 [PATCH v2 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
                   ` (7 preceding siblings ...)
  2026-11-16  5:57 ` [PATCH v2 08/11] perf tools: Export filename__read_build_id_ns() Namhyung Kim
@ 2026-11-16  5:57 ` Namhyung Kim
  2026-11-16  5:57 ` [PATCH v2 10/11] perf tools: Scope mount namespace entry in dso__load() Namhyung Kim
  2026-11-16  5:57 ` [PATCH v2 11/11] perf tests: Add namespace, nsinfo, and boot ID tests Namhyung Kim
  10 siblings, 0 replies; 14+ messages in thread
From: Namhyung Kim @ 2026-11-16  5:57 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       | 52 ++++++++++++++++++++++++++++++-------
 tools/perf/util/dso.h       | 34 ++++++++++++++++++++++++
 5 files changed, 102 insertions(+), 19 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 36e6c45dd0383073..d4fc702b4c338d96 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -712,14 +712,15 @@ static int open_dso(struct dso *dso, struct machine *machine)
 {
 	int fd;
 	struct nscookie nsc;
+	bool nsenter = dso_binary_type__needs_ns(dso__binary_type(dso));
 
-	if (dso__binary_type(dso) != DSO_BINARY_TYPE__BUILD_ID_CACHE) {
+	if (nsenter) {
 		mutex_lock(dso__lock(dso));
 		nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
 		mutex_unlock(dso__lock(dso));
 	}
 	fd = __open_dso(dso, machine);
-	if (dso__binary_type(dso) != DSO_BINARY_TYPE__BUILD_ID_CACHE)
+	if (nsenter)
 		nsinfo__mountns_exit(&nsc);
 
 	if (fd >= 0) {
@@ -897,12 +898,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;
 		}
@@ -912,9 +923,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;
 
@@ -2119,11 +2132,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;
@@ -2244,8 +2260,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;
@@ -2255,28 +2272,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 d5f7382d4e13a371..7cac0960e1a6dd32 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -141,6 +141,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.0.611.g1030f0b940-goog


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

* [PATCH v2 10/11] perf tools: Scope mount namespace entry in dso__load()
  2026-11-16  5:57 [PATCH v2 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
                   ` (8 preceding siblings ...)
  2026-11-16  5:57 ` [PATCH v2 09/11] perf tools: Only enter mount namespace if necessary Namhyung Kim
@ 2026-11-16  5:57 ` Namhyung Kim
  2026-11-16  5:57 ` [PATCH v2 11/11] perf tests: Add namespace, nsinfo, and boot ID tests Namhyung Kim
  10 siblings, 0 replies; 14+ messages in thread
From: Namhyung Kim @ 2026-11-16  5:57 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 dc70bbe8514f4d37..4c0ff9f0c32f8293 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -1811,17 +1811,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)) {
@@ -1845,8 +1834,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
@@ -1869,7 +1866,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);
 	}
 
@@ -1882,27 +1879,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);
@@ -1918,8 +1916,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);
@@ -1986,7 +1984,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.0.611.g1030f0b940-goog


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

* [PATCH v2 11/11] perf tests: Add namespace, nsinfo, and boot ID tests
  2026-11-16  5:57 [PATCH v2 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
                   ` (9 preceding siblings ...)
  2026-11-16  5:57 ` [PATCH v2 10/11] perf tools: Scope mount namespace entry in dso__load() Namhyung Kim
@ 2026-11-16  5:57 ` Namhyung Kim
  10 siblings, 0 replies; 14+ messages in thread
From: Namhyung Kim @ 2026-11-16  5:57 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 and chroot resolution.

Also verify the boot ID header matches /proc/sys/kernel/random/boot_id
in the perf header shell test for both file and pipe modes.

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    | 1035 ++++++++++++++++++++++++++++++
 tools/perf/tests/shell/header.sh |    9 +
 tools/perf/tests/tests.h         |    1 +
 5 files changed, 1047 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 327483f96401b580..f3c041ff1acf0289 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..cfc654db48f92292
--- /dev/null
+++ b/tools/perf/tests/namespaces.c
@@ -0,0 +1,1035 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <pthread.h>
+#include <sched.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/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/machine.h"
+#include "util/map.h"
+#include "util/namespaces.h"
+#include "util/symbol.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;
+}
+
+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);
+
+	TEST_ASSERT_VAL("NULL env should match current host boot",
+			perf_env__is_same_boot(NULL));
+
+	TEST_ASSERT_VAL("live env should match current host boot",
+			perf_env__is_same_boot(&live_env));
+	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;
+	}
+
+	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 (RC_CHK_ACCESS(nsi)->inited || RC_CHK_ACCESS(nsi)->exited) {
+		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 (RC_CHK_ACCESS(nsi)->inited || RC_CHK_ACCESS(copy)->inited) {
+		pr_debug("nsinfo__copy() unexpectedly triggered lazy init\n");
+		goto out;
+	}
+
+	nsinfo__clear_need_setns(copy);
+	if (!RC_CHK_ACCESS(copy)->inited || RC_CHK_ACCESS(copy)->need_setns) {
+		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 (!RC_CHK_ACCESS(nsi)->inited || RC_CHK_ACCESS(nsi)->exited ||
+	    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 (!RC_CHK_ACCESS(copy2)->inited ||
+	    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 (!RC_CHK_ACCESS(dead1)->exited || RC_CHK_ACCESS(dead1)->need_setns) {
+		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 (!RC_CHK_ACCESS(dead2)->exited || !RC_CHK_ACCESS(dead2)->inited ||
+	    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) ||
+	    RC_CHK_ACCESS(valid)->exited || 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) || !RC_CHK_ACCESS(recycled1)->exited) {
+		pr_debug("mismatched Tgid passed nsinfo__check_proc()\n");
+		goto out;
+	}
+	if (nsinfo__need_setns(recycled2) || !RC_CHK_ACCESS(recycled2)->exited) {
+		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 || !RC_CHK_ACCESS(off_nsi)->exited || 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 || 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 || 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) ||
+	    !RC_CHK_ACCESS(nsi3)->exited || nsinfo__check_proc(nsi3) ||
+	    RC_CHK_ACCESS(nsi1)->exited) {
+		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 (!RC_CHK_ACCESS(old_parent_nsi)->exited || 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;
+	if (!RC_CHK_ACCESS(del_nsi)->exited || nsinfo__check_proc(del_nsi) ||
+	    machine__find_thread(live_m, self_pid, self_pid) != 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 };
+	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));
+
+	if (dso__filename_with_chroot(dso, "/bin/sh") != NULL) {
+		pr_debug("dso__filename_with_chroot() did not respect exited nsinfo\n");
+		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;
+
+	return 0;
+}
+
+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, *chroot_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 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) || RC_CHK_ACCESS(nsi)->mntns_ino == 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 = dso__new(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);
+
+	/* 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) || RC_CHK_ACCESS(nsi)->exited || !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) || !RC_CHK_ACCESS(nsi)->exited) {
+		pr_debug("exited child still passed nsinfo__check_proc()\n");
+		goto out_wait;
+	}
+
+	/* Also verify chroot resolution when chroot(2) is permitted. */
+	if (geteuid() == 0 && access("/chroot_elf", F_OK) < 0) {
+		int cpipe[2] = { -1, -1 };
+
+		if (pipe(cpipe) == 0) {
+			pid_t cpid = fork();
+
+			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]);
+			if (cpid > 0 && read(cpipe[0], &sig, 1) == 1 && sig == '1') {
+				struct nsinfo *cnsi = nsinfo__new_thread(cpid, cpid);
+				struct stat st1, st2;
+				char *resolved = NULL;
+
+				chroot_dso = dso__new("/chroot_elf");
+				if (chroot_dso && cnsi) {
+					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);
+						free(resolved);
+						kill(cpid, SIGKILL);
+						waitpid(cpid, NULL, 0);
+						nsinfo__put(cnsi);
+						close(cpipe[0]);
+						goto out_wait;
+					}
+					free(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");
+							kill(cpid, SIGKILL);
+							waitpid(cpid, NULL, 0);
+							nsinfo__put(cnsi);
+							close(cpipe[0]);
+							goto out_wait;
+						}
+					}
+				}
+				kill(cpid, SIGKILL);
+				waitpid(cpid, NULL, 0);
+				if (chroot_dso &&
+				    dso__filename_with_chroot(chroot_dso, "/chroot_elf") != NULL) {
+					pr_debug("exited chroot task resolved /proc/<pid>/root\n");
+					nsinfo__put(cnsi);
+					close(cpipe[0]);
+					goto out_wait;
+				}
+				nsinfo__put(cnsi);
+			} else if (cpid > 0) {
+				waitpid(cpid, NULL, 0);
+			}
+			close(cpipe[0]);
+		}
+	}
+
+	ret = TEST_OK;
+out_wait:
+	if (child > 0) {
+		kill(child, SIGKILL);
+		waitpid(child, &status, 0);
+	}
+	nsinfo__put(nsi);
+	dso__put(chroot_dso);
+	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/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.0.611.g1030f0b940-goog


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

* Re: [PATCH v2 02/11] perf tools: Add perf_env__is_same_boot()
  2026-11-16  5:57 ` [PATCH v2 02/11] perf tools: Add perf_env__is_same_boot() Namhyung Kim
@ 2026-11-16  6:02   ` sashiko-bot
  0 siblings, 0 replies; 14+ messages in thread
From: sashiko-bot @ 2026-11-16  6:02 UTC (permalink / raw)
  To: Namhyung Kim; +Cc: 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>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116055731.707164-1-namhyung@kernel.org?part=2


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

* Re: [PATCH v2 01/11] perf tools: Add HEADER_BOOT_ID feature
  2026-11-16  5:57 ` [PATCH v2 01/11] perf tools: Add HEADER_BOOT_ID feature Namhyung Kim
@ 2026-11-16  6:09   ` sashiko-bot
  0 siblings, 0 replies; 14+ messages in thread
From: sashiko-bot @ 2026-11-16  6:09 UTC (permalink / raw)
  To: Namhyung Kim; +Cc: 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>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116055731.707164-1-namhyung@kernel.org?part=1


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

end of thread, other threads:[~2026-11-16  6:09 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16  5:57 [PATCH v2 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
2026-11-16  5:57 ` [PATCH v2 01/11] perf tools: Add HEADER_BOOT_ID feature Namhyung Kim
2026-11-16  6:09   ` sashiko-bot
2026-11-16  5:57 ` [PATCH v2 02/11] perf tools: Add perf_env__is_same_boot() Namhyung Kim
2026-11-16  6:02   ` sashiko-bot
2026-11-16  5:57 ` [PATCH v2 03/11] perf tools: Add nsinfo__new_thread() to track tid Namhyung Kim
2026-11-16  5:57 ` [PATCH v2 04/11] perf tools: Add lock to struct nsinfo Namhyung Kim
2026-11-16  5:57 ` [PATCH v2 05/11] perf tools: Defer /proc lookup until nsinfo is used Namhyung Kim
2026-11-16  5:57 ` [PATCH v2 06/11] perf tools: Verify thread status and mount namespace inode Namhyung Kim
2026-11-16  5:57 ` [PATCH v2 07/11] perf tools: Check thread liveness when accessing /proc Namhyung Kim
2026-11-16  5:57 ` [PATCH v2 08/11] perf tools: Export filename__read_build_id_ns() Namhyung Kim
2026-11-16  5:57 ` [PATCH v2 09/11] perf tools: Only enter mount namespace if necessary Namhyung Kim
2026-11-16  5:57 ` [PATCH v2 10/11] perf tools: Scope mount namespace entry in dso__load() Namhyung Kim
2026-11-16  5:57 ` [PATCH v2 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