Re: [PATCH v4 00/11] perf tools: Enhance namespace and chroot handling

From: Athira Rajeev

Date: Sat Nov 28 2026 - 04:53:24 EST




> On 26 Nov 2026, at 2:58 AM, Namhyung Kim <namhyung@xxxxxxxxxx> wrote:
>
> 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.
>

Hi Namhyung,

Tested on powerpc (ppc64le, 7.4.0-rc4-next-20261124+), with the series
applied on git://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git
and an unpatched perf as baseline.

Boot ID (patches 1, 2):
- perf record writes the boot ID and it matches
/proc/sys/kernel/random/boot_id.
- perf inject keeps it, and perf inject on a file recorded by an older
perf does not add one.

Test suite:
75: Namespace and nsinfo tests
75.1: boot_id : Ok
75.2: init_and_copy : Ok
75.3: proc_and_recycled_tid : Ok
75.4: thread_liveness : Ok
75.5: dso_needs_ns : Ok
75.6: mountns_and_chroot : Ok
(6 subtests passed, 0 failed)

Lazy nsinfo init (patch 5):
For perf report on a 2-second system-wide recording
(perf record -a -- sleep 2), the number of /proc/<pid>/{ns/mnt,status}
accesses (counted with strace) dropped from 520 to 0, and the report
output is identical.

Two boots (patches 2, 6, 7):
I recorded system-wide while a busy process ran in its own mount
namespace, rebooted, and ran perf report on the old file.
- The report output on the new boot is identical to the one from before
the reboot, for both patched and unpatched perf.
- I then started a different process with the recorded pid in a private
mount namespace (via ns_last_pid) and ran perf report again, counting
with strace:
setns() calls touches of that pid
patched, different boot 0 0
unpatched, different boot 6 4
- Control on the same boot with the process still alive:
patched 68 17
unpatched 30 4
so the patched perf still enters the namespace of a live process, and
stops using /proc when the boot ID differs.


Tested-by: Athira Rajeev <atrajeev@xxxxxxxxxxxxx>

Namhyung,

FWIW, on the same boot with live processes in other mount namespaces the
patched perf enters the namespace about 2.5x as often as the unpatched
one (70 vs 28 enters, 140 vs 56 setns() calls on a 7844-sample
system-wide recording). It does the same 134 traced openat/stat calls
inside the namespaces, split across more enters, and the trace also
shows extra opens of /proc and reads of /proc/<pid>/task/<tid>/status
(patch 6). No setns() failed and the report output is identical. Is the
increase expected?

How I measured it (./perf is patched, ./save_1/perf is unpatched):

# a busy process in its own mount namespace, then record system-wide
unshare -m sh -c 'mount -t tmpfs none /mnt && cp /usr/bin/yes /mnt/myyes &&
exec /mnt/myyes > /dev/null' &
pid=$!; sleep 1
./perf record -a -e cycles:u -o ctl.data -- sleep 3
kill $pid

# trace the same file with each binary
strace -f -e trace=setns,openat,stat,newfstatat -o /tmp/c.new ./perf report -i ctl.data --stdio --sort comm,dso,sym
strace -f -e trace=setns,openat,stat,newfstatat -o /tmp/c.old ./save_1/perf report -i ctl.data --stdio --sort comm,dso,sym


grep -c 'setns(' /tmp/c.new
grep 'setns(' /tmp/c.new
awk '/setns\(/{ if (inside) { if (cnt==0) empty++; inside=0 }
else { inside=1; cnt=0; enters++ } next }
inside { cnt++; total++ }
END { printf "enters=%d empty_enters=%d lines_inside=%d\n",
enters, empty, total }' /tmp/c.new

grep -c 'setns(' /tmp/c.old
grep 'setns(' /tmp/c.old
awk '/setns\(/{ if (inside) { if (cnt==0) empty++; inside=0 }
else { inside=1; cnt=0; enters++ } next }
inside { cnt++; total++ }
END { printf "enters=%d empty_enters=%d lines_inside=%d\n",
enters, empty, total }' /tmp/c.old

Result:
setns failed enters empty lines inside
patched 140 0 70 6 134
unpatched 56 0 28 0 134

The report output is identical for both.
The report output is identical for both:
./perf report -i ctl.data --stdio --sort comm,dso,sym 2>&1 | grep -v '^#' > /tmp/r.new
./save_1/perf report -i ctl.data --stdio --sort comm,dso,sym 2>&1 | grep -v '^#' > /tmp/r.old
diff /tmp/r.new /tmp/r.old # no output

Thanks
Athira

> It's assisted by LLM.
>
> v4 changes:
>
> * Fix a potential struct thread reference leak on failure in
> test__nsinfo_thread_liveness().
> * Rebase onto the latest tmp.perf-tools-next (preserving chroot
> tracking for --lazy-load-symbols in dso__load()).
>
> v3: https://lore.kernel.org/r/20261123232025.1406560-1-namhyung@xxxxxxxxxx
>
> * Guard evlist before calling evlist__session() in write_boot_id().
> * Repipe PERF_RECORD_HEADER_FEATURE events in perf-inject pipe mode
> and add a boot ID pipe-injection shell test and unit test.
> * Pin a reference-counted host /proc descriptor across
> nsinfo__mountns_enter()/exit(), access relative /proc paths via
> stack-buffered nsinfo__proc_{open,stat,readlink}() helpers instead of
> heap-allocating mntns_path, and handle ENOENT/ESRCH on ns/mnt stat.
> * Skip filename_with_chroot() chroot resolution when
> nsinfo__need_setns() is set.
> * Add compiler context-analysis lock annotations (__guarded_by,
> __must_hold, __returns_ctx_lock) for struct nsinfo, host_proc_lock,
> thread->nsinfo, and __thread__reset_nsinfo(), plus locked accessors.
> * Expand mount namespace tests to verify overmounted /proc during
> nsinfo__mountns_enter() and own a private mount namespace in
> enter_private_userns() for clean nsinfo__mountns_exit() restoration.
>
> v2: https://lore.kernel.org/r/20261116055731.707164-1-namhyung@xxxxxxxxxx
>
> * 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@xxxxxxxxxx
>
> Thanks,
> Namhyung
>
>
> Namhyung Kim (11):
> perf tools: Add HEADER_BOOT_ID feature
> perf tools: Add perf_env__is_same_boot()
> perf tools: Add nsinfo__new_thread() to track tid
> perf tools: Add lock to struct nsinfo
> perf tools: Defer /proc lookup until nsinfo is used
> perf tools: Verify thread status and mount namespace inode
> perf tools: Check thread liveness when accessing /proc
> perf tools: Export filename__read_build_id_ns()
> perf tools: Only enter mount namespace if necessary
> perf tools: Scope mount namespace entry in dso__load()
> perf tests: Add namespace, nsinfo, and boot ID tests
>
> .../Documentation/perf.data-file-format.txt | 5 +
> tools/perf/builtin-inject.c | 29 +-
> tools/perf/builtin-trace.c | 2 +-
> tools/perf/tests/Build | 1 +
> tools/perf/tests/builtin-test.c | 1 +
> tools/perf/tests/namespaces.c | 1189 +++++++++++++++++
> tools/perf/tests/shell/header.sh | 9 +
> tools/perf/tests/shell/pipe_test.sh | 21 +
> tools/perf/tests/tests.h | 1 +
> tools/perf/util/build-id.c | 15 +-
> tools/perf/util/capstone.c | 8 +-
> tools/perf/util/debuginfo.c | 19 +-
> tools/perf/util/disasm.c | 8 +-
> tools/perf/util/dso.c | 58 +-
> tools/perf/util/dso.h | 34 +
> tools/perf/util/dsos.c | 17 +-
> tools/perf/util/env.c | 69 +
> tools/perf/util/env.h | 4 +
> tools/perf/util/event.c | 1 +
> tools/perf/util/header.c | 28 +
> tools/perf/util/header.h | 1 +
> tools/perf/util/jitdump.c | 2 +-
> tools/perf/util/machine.c | 16 +-
> tools/perf/util/map.c | 2 +-
> tools/perf/util/namespaces.c | 439 +++++-
> tools/perf/util/namespaces.h | 23 +-
> tools/perf/util/session.c | 1 +
> tools/perf/util/symbol-minimal.c | 4 +-
> tools/perf/util/symbol.c | 45 +-
> tools/perf/util/symbol.h | 3 +
> tools/perf/util/thread.c | 97 +-
> tools/perf/util/thread.h | 15 +-
> tools/perf/util/util.c | 7 +-
> 33 files changed, 1999 insertions(+), 175 deletions(-)
> create mode 100644 tools/perf/tests/namespaces.c
>
> --
> 2.56.1.831.g720f3f5937-goog
>
>