perf tools: Enhance namespace and chroot handling
Namhyung Kim
maintainerv3 cover perf tools: Enhance namespace and chroot handling
Hello,
This series improves namespace and chroot handling when perf accesses files from /proc. It defers reading /proc until namespace info is actually needed, verifies thread liveness, matching boot IDs, and mount namespace inodes against PID/TID recycling, and avoids entering target mount namespaces for host-side files such as the build-id cache.
It's assisted by LLM.
v3 changes:
* Guard evlist before calling evlist__session() in write_boot_id().
* Repipe PERF_RECORD_HEADER_FEATURE events in perf-inject pipe mode and add a boot ID pipe-injection shell test and unit test.
* Pin a reference-counted host /proc descriptor across nsinfo__mountns_enter()/exit(), access relative /proc paths via stack-buffered nsinfo__proc_{open,stat,readlink}() helpers instead of heap-allocating mntns_path, and handle ENOENT/ESRCH on ns/mnt stat.
* Skip filename_with_chroot() chroot resolution when nsinfo__need_setns() is set.
* Add compiler context-analysis lock annotations (__guarded_by, __must_hold, __returns_ctx_lock) for struct nsinfo, host_proc_lock, thread->nsinfo, and __thread__reset_nsinfo(), plus locked accessors.
* Expand mount namespace tests to verify overmounted /proc during nsinfo__mountns_enter() and own a private mount namespace in enter_private_userns() for clean nsinfo__mountns_exit() restoration.
v2: https://lore.kernel.org/r/[email protected]
* 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/[email protected]
Thanks,
Namhyung
Namhyung Kim (11):
perf tools: Add HEADER_BOOT_ID feature
perf tools: Add perf_env__is_same_boot()
perf tools: Add nsinfo__new_thread() to track tid perf tools: Add lock to struct nsinfo
perf tools: Defer /proc lookup until nsinfo is used perf tools: Verify thread status and mount namespace inode perf tools: Check thread liveness when accessing /proc perf tools: Export filename__read_build_id_ns() perf tools: Only enter mount namespace if necessary perf tools: Scope mount namespace entry in dso__load() perf tests: Add namespace, nsinfo, and boot ID tests
.../Documentation/perf.data-file-format.txt | 5 + tools/perf/builtin-inject.c | 29 +- tools/perf/builtin-trace.c | 2 +- tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/namespaces.c | 1188 +++++++++++++++++ tools/perf/tests/shell/header.sh | 9 + tools/perf/tests/shell/pipe_test.sh | 21 + tools/perf/tests/tests.h | 1 + tools/perf/util/build-id.c | 15 +- tools/perf/util/capstone.c | 8 +- tools/perf/util/debuginfo.c | 19 +- tools/perf/util/disasm.c | 8 +- tools/perf/util/dso.c | 58 +- tools/perf/util/dso.h | 34 + tools/perf/util/dsos.c | 17 +- tools/perf/util/env.c | 69 + tools/perf/util/env.h | 4 + tools/perf/util/event.c | 1 + tools/perf/util/header.c | 28 + tools/perf/util/header.h | 1 + tools/perf/util/jitdump.c | 2 +- tools/perf/util/machine.c | 16 +- tools/perf/util/map.c | 2 +- tools/perf/util/namespaces.c | 439 +++++- tools/perf/util/namespaces.h | 23 +- tools/perf/util/session.c | 1 + tools/perf/util/symbol-minimal.c | 4 +- tools/perf/util/symbol.c | 45 +- tools/perf/util/symbol.h | 3 + tools/perf/util/thread.c | 97 +- tools/perf/util/thread.h | 15 +- tools/perf/util/util.c | 7 +- 33 files changed, 1998 insertions(+), 175 deletions(-) create mode 100644 tools/perf/tests/namespaces.c
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Namhyung Kim
maintainerPATCH v3 1/11 perf tools: Add HEADER_BOOT_ID feature
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.
--- .../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(+)
tools/perf/Documentation/perf.data-file-format.txt +5 -0
@@ -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,
tools/perf/builtin-inject.c +1 -0
@@ -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 :
tools/perf/util/env.c +44 -0
62 lines not on this page · raw message
tools/perf/util/env.h +3 -0
@@ -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[]);
tools/perf/util/header.c +28 -0
@@ -494,6 +494,27 @@ static int write_version(struct feat_fd *ff,return do_write_string (ff, perf_version_string); } +static int write_boot_id (struct feat_fd *ff,struct evlist *evlist) +{ +const char *boot_id =NULL ; +char *buf =NULL ; +size_t sz; +int ret; + +if (evlist &&evlist__session (evlist)) + boot_id =perf_env__boot_id (perf_session__env (evlist__session (evlist))); + +if (!boot_id) { +if (procfs__read_str ("sys/kernel/random/boot_id" , &buf, &sz) <0 ) +return -1 ; + boot_id =strim (buf); + } + + ret =do_write_string (ff, boot_id); +free (buf); +return ret; +} +static int __write_cpudesc (struct feat_fd *ff,const char *cpuinfo_proc) {FILE *file;
@@ -2079,6 +2100,11 @@ static void print_version(struct feat_fd *ff, FILE *fp)fprintf (fp,"# perf version : %s\n" , ff->ph->env.version); } +static void print_boot_id (struct feat_fd *ff,FILE *fp) +{ +fprintf (fp,"# boot id : %s\n" , ff->ph->env.boot_id); +} +static void print_cmdline (struct feat_fd *ff,FILE *fp) {int nr, i;
@@ -3022,6 +3048,7 @@ FEAT_PROCESS_STR_FUN(osrelease, os_release);FEAT_PROCESS_STR_FUN (version, version);FEAT_PROCESS_STR_FUN (cpudesc, cpu_desc);FEAT_PROCESS_STR_FUN (cpuid, cpuid); +FEAT_PROCESS_STR_FUN (boot_id, boot_id);static int process_arch (struct feat_fd *ff,void *data__maybe_unused ) {
@@ -4496,6 +4523,7 @@ const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE] = {FEAT_OPR (E_MACHINE , e_machine, false),FEAT_OPR (CLN_SIZE , cln_size, false),FEAT_OPR (MEMORY_RANGES , memory_ranges, false), + FEAT_OPR(BOOT_ID, boot_id, false), }; struct header_print_data {
tools/perf/util/header.h +1 -0
@@ -57,6 +57,7 @@ enum {HEADER_E_MACHINE ,HEADER_CLN_SIZE ,HEADER_MEMORY_RANGES , +HEADER_BOOT_ID ,HEADER_LAST_FEATURE , HEADER_FEAT_BITS = 256, };
tools/perf/util/session.c +1 -0
@@ -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;
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Namhyung Kim
maintainerPATCH v3 2/11 perf tools: Add perf_env__is_same_boot()
In reply to Namhyung KimAdd 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.
--- tools/perf/util/env.c | 25 +++++++++++++++++++++++++ tools/perf/util/env.h | 1 + 2 files changed, 26 insertions(+)
tools/perf/util/env.c +25 -0
@@ -445,6 +445,31 @@ const char *perf_env__boot_id(struct perf_env *env)return boot_id; } +static char *host_boot_id; + +static void init_host_boot_id (void ) +{ + host_boot_id =read_host_boot_id (); +} + +bool perf_env__is_same_boot (struct perf_env *env) +{ +const char *env_boot_id; + +DO_ONCE (init_host_boot_id); +if (!host_boot_id) +return false; + +if (!env) +return true; + + env_boot_id =perf_env__boot_id (env); +if (!env_boot_id) +return false; + +return !strcmp (env_boot_id, host_boot_id); +} +int perf_env__set_cmdline (struct perf_env *env,int argc,const char *argv[]) {int i;
tools/perf/util/env.h +1 -0
@@ -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[]);
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Namhyung Kim
maintainerPATCH v3 3/11 perf tools: Add nsinfo__new_thread() to track tid
In reply to Namhyung KimRecord 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.
--- 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(-)
tools/perf/util/machine.c +3 -0
@@ -19,6 +19,7 @@#include "branch.h" #include "mem-events.h" #include "mem-info.h" +#include "namespaces.h" #include "path.h" #include "srcline.h" #include "symbol.h"
@@ -517,6 +518,8 @@ static void machine__update_thread_pid(struct machine *machine,return ;thread__set_pid (th, pid); +nsinfo__zput (RC_CHK_ACCESS (th)->nsinfo); +RC_CHK_ACCESS (th)->nsinfo =nsinfo__new_thread (pid,thread__tid (th));if (thread__pid (th) ==thread__tid (th))return ;
tools/perf/util/namespaces.c +18 -4
@@ -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;
tools/perf/util/namespaces.h +3 -0
@@ -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);
tools/perf/util/thread.c +1 -1
@@ -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)); }
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Namhyung Kim
maintainerPATCH v3 4/11 perf tools: Add lock to struct nsinfo
In reply to Namhyung KimA 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().
--- tools/perf/util/namespaces.c | 135 ++++++++++++++++++++++++++++------- tools/perf/util/namespaces.h | 12 ++-- 2 files changed, 115 insertions(+), 32 deletions(-)
tools/perf/util/namespaces.c +108 -27
@@ -94,7 +94,14 @@ static int nsinfo__get_nspid(pid_t *tgid, pid_t *nstgid, bool *in_pidns, const creturn 0 ; } -int nsinfo__init (struct nsinfo *nsi) +static struct mutex *nsinfo__lock (const struct nsinfo *nsi) + __returns_ctx_lock(&RC_CHK_ACCESS(nsi)->lock) +{ +return (struct mutex *)&RC_CHK_ACCESS (nsi)->lock; +} + +static int nsinfo__init_locked (struct nsinfo *nsi) +__must_hold (nsinfo__lock (nsi)) {char oldns[PATH_MAX ];char spath[PATH_MAX ];
@@ -140,14 +147,29 @@ int nsinfo__init(struct nsinfo *nsi)return rv; } +int nsinfo__init (struct nsinfo *nsi) +{ +int rv; + +if (!nsi) +return -1 ; + +mutex_lock (nsinfo__lock (nsi)); + rv =nsinfo__init_locked (nsi); +mutex_unlock (nsinfo__lock (nsi)); +return rv; +} +static struct nsinfo *nsinfo__alloc (void ) {struct nsinfo *res;RC_STRUCT (nsinfo) *nsi; nsi =calloc (1 ,sizeof (*nsi)); -if (ADD_RC_CHK (res, nsi)) +if (ADD_RC_CHK (res, nsi)) {refcount_set (&nsi->refcnt,1 ); +mutex_init (&nsi->lock); + }return res; }
@@ -165,10 +187,12 @@ struct nsinfo *nsinfo__new_thread(pid_t pid, pid_t tid)RC_CHK_ACCESS (nsi)->pid = pid;RC_CHK_ACCESS (nsi)->tid = tid; +mutex_lock (nsinfo__lock (nsi));RC_CHK_ACCESS (nsi)->tgid = pid;RC_CHK_ACCESS (nsi)->nstgid = pid; -nsinfo__clear_need_setns (nsi);RC_CHK_ACCESS (nsi)->in_pidns = false; +mutex_unlock (nsinfo__lock (nsi)); +nsinfo__clear_need_setns (nsi);/* Init may fail if the process exits while we're trying to look at its * proc information. In that case, save the pid but don't try to enter * the namespace.
@@ -184,14 +208,12 @@ struct nsinfo *nsinfo__new(pid_t pid)return nsinfo__new_thread (pid, pid); } -static const char *nsinfo__mntns_path (const struct nsinfo *nsi) -{ -return RC_CHK_ACCESS (nsi)->mntns_path; -} -struct nsinfo *nsinfo__copy (const struct nsinfo *nsi) {struct nsinfo *nnsi; +pid_t pid, tid, tgid, nstgid; +bool need_setns, in_pidns; +char *mntns_path =NULL ;if (nsi ==NULL )return NULL ;
@@ -200,19 +222,32 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)if (!nnsi)return NULL ; -RC_CHK_ACCESS (nnsi)->pid =nsinfo__pid (nsi); -RC_CHK_ACCESS (nnsi)->tid =nsinfo__tid (nsi); -RC_CHK_ACCESS (nnsi)->tgid =nsinfo__tgid (nsi); -RC_CHK_ACCESS (nnsi)->nstgid =nsinfo__nstgid (nsi); -RC_CHK_ACCESS (nnsi)->need_setns =nsinfo__need_setns (nsi); -RC_CHK_ACCESS (nnsi)->in_pidns =nsinfo__in_pidns (nsi); -if (nsinfo__mntns_path (nsi)) { -RC_CHK_ACCESS (nnsi)->mntns_path =strdup (nsinfo__mntns_path (nsi)); -if (!RC_CHK_ACCESS (nnsi)->mntns_path) { +mutex_lock (nsinfo__lock (nsi)); + pid =RC_CHK_ACCESS (nsi)->pid; + tid =RC_CHK_ACCESS (nsi)->tid; + tgid =RC_CHK_ACCESS (nsi)->tgid; + nstgid =RC_CHK_ACCESS (nsi)->nstgid; + need_setns =RC_CHK_ACCESS (nsi)->need_setns; + in_pidns =RC_CHK_ACCESS (nsi)->in_pidns; +if (RC_CHK_ACCESS (nsi)->mntns_path) { + mntns_path =strdup (RC_CHK_ACCESS (nsi)->mntns_path); +if (!mntns_path) { +mutex_unlock (nsinfo__lock (nsi));nsinfo__put (nnsi);return NULL ; } } +mutex_unlock (nsinfo__lock (nsi)); + +RC_CHK_ACCESS (nnsi)->pid = pid; +RC_CHK_ACCESS (nnsi)->tid = tid; +mutex_lock (nsinfo__lock (nnsi)); +RC_CHK_ACCESS (nnsi)->tgid = tgid; +RC_CHK_ACCESS (nnsi)->nstgid = nstgid; +RC_CHK_ACCESS (nnsi)->need_setns = need_setns; +RC_CHK_ACCESS (nnsi)->in_pidns = in_pidns; +RC_CHK_ACCESS (nnsi)->mntns_path = mntns_path; +mutex_unlock (nsinfo__lock (nnsi));return nnsi; }
@@ -226,7 +261,10 @@ static void nsinfo__delete(struct nsinfo *nsi) {if (nsi) {WARN_ONCE (refcount_read (nsinfo__refcnt (nsi)) !=0 ,"nsinfo refcnt unbalanced\n" ); +mutex_lock (nsinfo__lock (nsi));zfree (&RC_CHK_ACCESS (nsi)->mntns_path); +mutex_unlock (nsinfo__lock (nsi)); +mutex_destroy (&RC_CHK_ACCESS (nsi)->lock);RC_CHK_FREE (nsi); } }
@@ -251,22 +289,46 @@ void nsinfo__put(struct nsinfo *nsi)bool nsinfo__need_setns (const struct nsinfo *nsi) { -return RC_CHK_ACCESS (nsi)->need_setns; +bool ret; + +if (!nsi) +return false; + +mutex_lock (nsinfo__lock (nsi)); + ret =RC_CHK_ACCESS (nsi)->need_setns; +mutex_unlock (nsinfo__lock (nsi)); +return ret; }void nsinfo__clear_need_setns (struct nsinfo *nsi) { +if (!nsi) +return ; + +mutex_lock (nsinfo__lock (nsi));RC_CHK_ACCESS (nsi)->need_setns = false; +zfree (&RC_CHK_ACCESS (nsi)->mntns_path); +mutex_unlock (nsinfo__lock (nsi)); } -pid_t nsinfo__tgid (const struct nsinfo *nsi) +pid_t nsinfo__tgid (const struct nsinfo *nsi) { -return RC_CHK_ACCESS (nsi)->tgid; +pid_t ret; + +mutex_lock (nsinfo__lock (nsi)); + ret =RC_CHK_ACCESS (nsi)->tgid; +mutex_unlock (nsinfo__lock (nsi)); +return ret; } -pid_t nsinfo__nstgid (const struct nsinfo *nsi) +pid_t nsinfo__nstgid (const struct nsinfo *nsi) { -return RC_CHK_ACCESS (nsi)->nstgid; +pid_t ret; + +mutex_lock (nsinfo__lock (nsi)); + ret =RC_CHK_ACCESS (nsi)->nstgid; +mutex_unlock (nsinfo__lock (nsi)); +return ret; }pid_t nsinfo__pid (const struct nsinfo *nsi)
@@ -281,18 +343,26 @@ pid_t nsinfo__tid(const struct nsinfo *nsi)bool nsinfo__in_pidns (const struct nsinfo *nsi) { -return RC_CHK_ACCESS (nsi)->in_pidns; +bool ret; + +mutex_lock (nsinfo__lock (nsi)); + ret =RC_CHK_ACCESS (nsi)->in_pidns; +mutex_unlock (nsinfo__lock (nsi)); +return ret; }void nsinfo__set_in_pidns (struct nsinfo *nsi) { +mutex_lock (nsinfo__lock (nsi));RC_CHK_ACCESS (nsi)->in_pidns = true; +mutex_unlock (nsinfo__lock (nsi)); }void nsinfo__mountns_enter (struct nsinfo *nsi,struct nscookie *nc) {char curpath[PATH_MAX ]; +char *mntns_path =NULL ;int oldns =-1 ;int newns =-1 ;char *oldcwd =NULL ;
@@ -303,27 +373,36 @@ void nsinfo__mountns_enter(struct nsinfo *nsi, nc->oldns =-1 ; nc->newns =-1 ; -if (!nsi || !nsinfo__need_setns (nsi)) +if (!nsi)return ; -if (!getcwd (curpath,sizeof (curpath))) +mutex_lock (nsinfo__lock (nsi)); +if (RC_CHK_ACCESS (nsi)->need_setns &&RC_CHK_ACCESS (nsi)->mntns_path) + mntns_path =strdup (RC_CHK_ACCESS (nsi)->mntns_path); +mutex_unlock (nsinfo__lock (nsi)); + +if (!mntns_path)return ; +if (!getcwd (curpath,sizeof (curpath))) + goto out; + oldcwd =strdup (curpath);if (!oldcwd) -return ; + goto out; oldns =open ("/proc/self/ns/mnt" ,O_RDONLY );if (oldns <0 ) goto errout; - newns =open (nsinfo__mntns_path (nsi),O_RDONLY ); + newns =open (mntns_path,O_RDONLY );if (newns <0 ) goto errout;if (setns (newns,CLONE_NEWNS ) <0 ) goto errout; +free (mntns_path); nc->oldcwd = oldcwd; nc->oldns = oldns; nc->newns = newns;
@@ -335,6 +414,8 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,close (oldns);if (newns >-1 )close (newns); +out: +free (mntns_path); }void nsinfo__mountns_exit (struct nscookie *nc)
tools/perf/util/namespaces.h +7 -5
@@ -14,6 +14,7 @@#include <linux/refcount.h> #include <linux/types.h> #include <internal/rc_check.h> +#include "mutex.h" #ifndef HAVE_SETNS_SUPPORT int setns (int fd,int nstype);
@@ -31,13 +32,14 @@ struct namespaces *namespaces__new(struct perf_record_namespaces *event);void namespaces__free (struct namespaces *namespaces);DECLARE_RC_STRUCT (nsinfo) { +struct mutex lock;pid_t pid;pid_t tid; -pid_t tgid; -pid_t nstgid; -bool need_setns; -bool in_pidns; -char *mntns_path; + pid_t tgid __guarded_by(&lock); + pid_t nstgid __guarded_by(&lock); + bool need_setns __guarded_by(&lock); + bool in_pidns __guarded_by(&lock); + char *mntns_path __guarded_by(&lock);refcount_t refcnt; };
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Namhyung Kim
maintainerPATCH v3 5/11 perf tools: Defer /proc lookup until nsinfo is used
In reply to Namhyung KimDo 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.
--- tools/perf/builtin-inject.c | 2 +- tools/perf/util/jitdump.c | 2 +- tools/perf/util/map.c | 2 +- tools/perf/util/namespaces.c | 28 ++++++++++++++++++---------- tools/perf/util/namespaces.h | 1 + tools/perf/util/thread.c | 34 +++++++++++++++++++++++++++++++++- tools/perf/util/thread.h | 8 +++----- 7 files changed, 58 insertions(+), 19 deletions(-)
tools/perf/builtin-inject.c +1 -1
@@ -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
tools/perf/util/jitdump.c +1 -1
@@ -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);/*
tools/perf/util/map.c +1 -1
@@ -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),
tools/perf/util/namespaces.c +18 -10
@@ -110,6 +110,8 @@ static int nsinfo__init_locked(struct nsinfo *nsi)struct stat new_stat;int rv =-1 ; +RC_CHK_ACCESS (nsi)->inited = true; +if (snprintf (oldns,PATH_MAX ,"/proc/self/ns/mnt" ) >=PATH_MAX )return rv;
@@ -160,6 +162,13 @@ int nsinfo__init(struct nsinfo *nsi)return rv; } +static void nsinfo__ensure_init_locked (struct nsinfo *nsi) +__must_hold (nsinfo__lock (nsi)) +{ +if (!RC_CHK_ACCESS (nsi)->inited) +nsinfo__init_locked (nsi); +} +static struct nsinfo *nsinfo__alloc (void ) {struct nsinfo *res;
@@ -190,16 +199,7 @@ struct nsinfo *nsinfo__new_thread(pid_t pid, pid_t tid)mutex_lock (nsinfo__lock (nsi));RC_CHK_ACCESS (nsi)->tgid = pid;RC_CHK_ACCESS (nsi)->nstgid = pid; -RC_CHK_ACCESS (nsi)->in_pidns = false;mutex_unlock (nsinfo__lock (nsi)); -nsinfo__clear_need_setns (nsi); -/* Init may fail if the process exits while we're trying to look at its -* proc information. In that case, save the pid but don't try to enter -* the namespace. -*/ -if (nsinfo__init (nsi) ==-1 ) -nsinfo__clear_need_setns (nsi); -return nsi; }
@@ -212,7 +212,7 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi) {struct nsinfo *nnsi;pid_t pid, tid, tgid, nstgid; -bool need_setns, in_pidns; +bool inited, need_setns, in_pidns;char *mntns_path =NULL ;if (nsi ==NULL )
@@ -227,6 +227,7 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi) tid =RC_CHK_ACCESS (nsi)->tid; tgid =RC_CHK_ACCESS (nsi)->tgid; nstgid =RC_CHK_ACCESS (nsi)->nstgid; + inited =RC_CHK_ACCESS (nsi)->inited; need_setns =RC_CHK_ACCESS (nsi)->need_setns; in_pidns =RC_CHK_ACCESS (nsi)->in_pidns;if (RC_CHK_ACCESS (nsi)->mntns_path) {
@@ -244,6 +245,7 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)mutex_lock (nsinfo__lock (nnsi));RC_CHK_ACCESS (nnsi)->tgid = tgid;RC_CHK_ACCESS (nnsi)->nstgid = nstgid; +RC_CHK_ACCESS (nnsi)->inited = inited;RC_CHK_ACCESS (nnsi)->need_setns = need_setns;RC_CHK_ACCESS (nnsi)->in_pidns = in_pidns;RC_CHK_ACCESS (nnsi)->mntns_path = mntns_path;
@@ -295,6 +297,7 @@ bool nsinfo__need_setns(const struct nsinfo *nsi)return false;mutex_lock (nsinfo__lock (nsi)); +nsinfo__ensure_init_locked ((struct nsinfo *)nsi); ret =RC_CHK_ACCESS (nsi)->need_setns;mutex_unlock (nsinfo__lock (nsi));return ret;
@@ -306,6 +309,7 @@ void nsinfo__clear_need_setns(struct nsinfo *nsi)return ;mutex_lock (nsinfo__lock (nsi)); +RC_CHK_ACCESS (nsi)->inited = true;RC_CHK_ACCESS (nsi)->need_setns = false;zfree (&RC_CHK_ACCESS (nsi)->mntns_path);mutex_unlock (nsinfo__lock (nsi));
@@ -316,6 +320,7 @@ pid_t nsinfo__tgid(const struct nsinfo *nsi)pid_t ret;mutex_lock (nsinfo__lock (nsi)); +nsinfo__ensure_init_locked ((struct nsinfo *)nsi); ret =RC_CHK_ACCESS (nsi)->tgid;mutex_unlock (nsinfo__lock (nsi));return ret;
@@ -326,6 +331,7 @@ pid_t nsinfo__nstgid(const struct nsinfo *nsi)pid_t ret;mutex_lock (nsinfo__lock (nsi)); +nsinfo__ensure_init_locked ((struct nsinfo *)nsi); ret =RC_CHK_ACCESS (nsi)->nstgid;mutex_unlock (nsinfo__lock (nsi));return ret;
@@ -346,6 +352,7 @@ bool nsinfo__in_pidns(const struct nsinfo *nsi)bool ret;mutex_lock (nsinfo__lock (nsi)); +nsinfo__ensure_init_locked ((struct nsinfo *)nsi); ret =RC_CHK_ACCESS (nsi)->in_pidns;mutex_unlock (nsinfo__lock (nsi));return ret;
@@ -377,6 +384,7 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,return ;mutex_lock (nsinfo__lock (nsi)); +nsinfo__ensure_init_locked (nsi);if (RC_CHK_ACCESS (nsi)->need_setns &&RC_CHK_ACCESS (nsi)->mntns_path) mntns_path =strdup (RC_CHK_ACCESS (nsi)->mntns_path);mutex_unlock (nsinfo__lock (nsi));
tools/perf/util/namespaces.h +1 -0
@@ -37,6 +37,7 @@ DECLARE_RC_STRUCT(nsinfo) {pid_t tid; pid_t tgid __guarded_by(&lock); pid_t nstgid __guarded_by(&lock); + bool inited __guarded_by(&lock); bool need_setns __guarded_by(&lock); bool in_pidns __guarded_by(&lock); char *mntns_path __guarded_by(&lock);
tools/perf/util/thread.c +33 -1
@@ -108,6 +108,7 @@ void thread__delete(struct thread *thread)list_del_init (&namespaces->list);namespaces__free (namespaces); } +nsinfo__zput (RC_CHK_ACCESS (thread)->nsinfo);up_write (thread__namespaces_lock (thread));down_write (thread__comm_lock (thread));
@@ -117,7 +118,6 @@ void thread__delete(struct thread *thread) }up_write (thread__comm_lock (thread)); -nsinfo__zput (RC_CHK_ACCESS (thread)->nsinfo);srccode_state_free (thread__srccode_state (thread));exit_rwsem (thread__namespaces_lock (thread));
@@ -193,6 +193,34 @@ static int __thread__set_namespaces(struct thread *thread, u64 timestamp,return 0 ; } +static void __thread__reset_nsinfo (struct thread *thread) +__must_hold (thread__namespaces_lock (thread)) +{ +nsinfo__zput (RC_CHK_ACCESS (thread)->nsinfo); +RC_CHK_ACCESS (thread)->nsinfo =nsinfo__new_thread (thread__pid (thread), +thread__tid (thread)); +} + +void thread__reset_nsinfo (struct thread *thread) +{ +down_write (thread__namespaces_lock (thread)); +__thread__reset_nsinfo (thread); +up_write (thread__namespaces_lock (thread)); +} + +struct nsinfo *thread__get_nsinfo (struct thread *thread) +{ +struct nsinfo *nsi; + +if (!thread) +return NULL ; + +down_read (thread__namespaces_lock (thread)); + nsi =nsinfo__get (RC_CHK_ACCESS (thread)->nsinfo); +up_read (thread__namespaces_lock (thread)); +return nsi; +} +int thread__set_namespaces (struct thread *thread,u64 timestamp,struct perf_record_namespaces *event) {
@@ -200,6 +228,8 @@ int thread__set_namespaces(struct thread *thread, u64 timestamp,down_write (thread__namespaces_lock (thread)); ret =__thread__set_namespaces (thread, timestamp, event); +if (!ret && event) +__thread__reset_nsinfo (thread);up_write (thread__namespaces_lock (thread));return ret; }
@@ -284,6 +314,8 @@ int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,down_write (thread__comm_lock (thread)); ret =____thread__set_comm (thread, str, timestamp, exec);up_write (thread__comm_lock (thread)); +if (!ret && exec) +thread__reset_nsinfo (thread);return ret; }
tools/perf/util/thread.h +3 -5
@@ -56,7 +56,7 @@ DECLARE_RC_STRUCT(thread) {void *priv;struct thread_stack *ts; -struct nsinfo *nsinfo; + struct nsinfo *nsinfo __guarded_by(&namespaces_lock);struct srccode_state srccode_state;bool filter;int filter_entry_depth;
@@ -292,10 +292,8 @@ static inline void thread__set_ts(struct thread *thread, struct thread_stack *tsRC_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) {
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Namhyung Kim
maintainerPATCH v3 6/11 perf tools: Verify thread status and mount namespace inode
In reply to Namhyung KimVerify /proc/<pid>/task/<tid>/status exists and has Tgid == pid before reading or entering a thread's namespaces to guard against PID/TID recycling. Pin a reference-counted host /proc file descriptor across nsinfo__mountns_enter()/exit() and access relative /proc paths through nsinfo__proc_open(), nsinfo__proc_stat(), and nsinfo__proc_readlink() using stack buffers instead of heap-allocating mntns_path or keeping a permanent /proc descriptor open.
Also record the mount namespace inode (mntns_ino) during initialization, mark nsinfo exited if stating ns/mnt fails with ENOENT or ESRCH, and verify via fstat() that the opened namespace file descriptor still refers to the same mount namespace before calling setns().
--- tools/perf/util/namespaces.c | 330 +++++++++++++++++++++++++++-------- tools/perf/util/namespaces.h | 9 +- 2 files changed, 267 insertions(+), 72 deletions(-)
tools/perf/util/namespaces.c +259 -71
@@ -6,6 +6,7 @@#include "namespaces.h" #include "event.h" +#include <errno.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>
@@ -59,22 +60,102 @@ void namespaces__free(struct namespaces *namespaces)free (namespaces); } -static int nsinfo__get_nspid (pid_t *tgid,pid_t *nstgid,bool *in_pidns,const char *path) +static DEFINE_MUTEX (host_proc_lock); +static int host_proc_fd __guarded_by(&host_proc_lock) = -1; +static unsigned int host_proc_refcnt __guarded_by(&host_proc_lock); + +static void nsinfo__host_proc_get (void ) +{ +mutex_lock (&host_proc_lock); +if (host_proc_refcnt++ ==0 ) + host_proc_fd =open ("/proc" ,O_RDONLY |O_DIRECTORY |O_CLOEXEC ); +mutex_unlock (&host_proc_lock); +} + +static void nsinfo__host_proc_put (void ) +{ +mutex_lock (&host_proc_lock); +if (host_proc_refcnt >0 && --host_proc_refcnt ==0 && + host_proc_fd >=0 ) { +close (host_proc_fd); + host_proc_fd =-1 ; + } +mutex_unlock (&host_proc_lock); +} + +static int nsinfo__proc_open (const char *rel_path,int flags) +{ +char path[64 ]; +int ret; + +mutex_lock (&host_proc_lock); +if (host_proc_fd >=0 ) { + ret =openat (host_proc_fd, rel_path, flags); + }else { +scnprintf (path,sizeof (path),"/proc/%s" , rel_path); + ret =open (path, flags); + } +mutex_unlock (&host_proc_lock); +return ret; +} + +static int nsinfo__proc_stat (const char *rel_path,struct stat *st) +{ +char path[64 ]; +int ret; + +mutex_lock (&host_proc_lock); +if (host_proc_fd >=0 ) { + ret =fstatat (host_proc_fd, rel_path, st,0 ); + }else { +scnprintf (path,sizeof (path),"/proc/%s" , rel_path); + ret =stat (path, st); + } +mutex_unlock (&host_proc_lock); +return ret; +} + +ssize_t nsinfo__proc_readlink (const char *rel_path,char *buf,size_t size) +{ +char path[64 ]; +ssize_t ret; + +mutex_lock (&host_proc_lock); +if (host_proc_fd >=0 ) { + ret =readlinkat (host_proc_fd, rel_path, buf, size); + }else { +scnprintf (path,sizeof (path),"/proc/%s" , rel_path); + ret =readlink (path, buf, size); + } +mutex_unlock (&host_proc_lock); +return ret; +} + +static int nsinfo__get_nspid (pid_t *tgid,pid_t *nstgid,bool *in_pidns, +const char *rel_path) {FILE *f =NULL ;char *statln =NULL ;size_t linesz =0 ;char *nspid; +int fd; +bool got_tgid = false; - f =fopen (path,"r" ); -if (f ==NULL ) + fd =nsinfo__proc_open (rel_path,O_RDONLY |O_CLOEXEC ); +if (fd <0 )return -1 ; + f =fdopen (fd,"r" ); +if (f ==NULL ) { +close (fd); +return -1 ; + }while (getline (&statln, &linesz, f) !=-1 ) {/* Use tgid if CONFIG_PID_NS is not defined. */ if (strstr (statln,"Tgid:" ) !=NULL ) { *tgid = (pid_t )strtol (strrchr (statln,'\t' ),NULL ,10 ); *nstgid = *tgid; + got_tgid = true; }if (strstr (statln,"NStgid:" ) !=NULL ) {
@@ -91,7 +172,17 @@ static int nsinfo__get_nspid(pid_t *tgid, pid_t *nstgid, bool *in_pidns, const cfclose (f);free (statln); -return 0 ; +return got_tgid ?0 :-1 ; +} + +static int nsinfo__get_task_nspid (const struct nsinfo *nsi, +pid_t *tgid,pid_t *nstgid,bool *in_pidns) +{ +char spath[64 ]; + +scnprintf (spath,sizeof (spath),"%d/task/%d/status" , +nsinfo__pid (nsi),nsinfo__tid (nsi)); +return nsinfo__get_nspid (tgid, nstgid, in_pidns, spath); }static struct mutex *nsinfo__lock (const struct nsinfo *nsi)
@@ -100,53 +191,69 @@ static struct mutex *nsinfo__lock(const struct nsinfo *nsi)return (struct mutex *)&RC_CHK_ACCESS (nsi)->lock; } +static void nsinfo__clear_need_setns_locked (struct nsinfo *nsi) +__must_hold (nsinfo__lock (nsi)) +{ +RC_CHK_ACCESS (nsi)->inited = true; +RC_CHK_ACCESS (nsi)->need_setns = false; +} + +static void nsinfo__set_exited_locked (struct nsinfo *nsi) +__must_hold (nsinfo__lock (nsi)) +{ +RC_CHK_ACCESS (nsi)->exited = true; +nsinfo__clear_need_setns_locked (nsi); +} +static int nsinfo__init_locked (struct nsinfo *nsi)__must_hold (nsinfo__lock (nsi)) { -char oldns[PATH_MAX ]; -char spath[PATH_MAX ]; -char *newns =NULL ; +char newns[64 ];struct stat old_stat;struct stat new_stat; -int rv =-1 ; +pid_t tgid =0 , nstgid =0 ; +bool in_pidns = false; + +if (RC_CHK_ACCESS (nsi)->exited) +return -1 ;RC_CHK_ACCESS (nsi)->inited = true; -if (snprintf (oldns,PATH_MAX ,"/proc/self/ns/mnt" ) >=PATH_MAX ) -return rv; +/* +* Verify that the thread (pid, tid) still exists in /proc and its Tgid +* matches pid (guarding against PID/TID recycling), while resolving the +* innermost tgid if the process is in a different PID namespace. +*/ +if (nsinfo__get_task_nspid (nsi, &tgid, &nstgid, &in_pidns) <0 || + tgid !=nsinfo__pid (nsi)) { +nsinfo__set_exited_locked (nsi); +return -1 ; + } -if (asprintf (&newns,"/proc/%d/task/%d/ns/mnt" , -nsinfo__pid (nsi),nsinfo__tid (nsi)) ==-1 ) -return rv; +RC_CHK_ACCESS (nsi)->tgid = tgid; +RC_CHK_ACCESS (nsi)->nstgid = nstgid; +RC_CHK_ACCESS (nsi)->in_pidns = in_pidns; -if (stat (oldns, &old_stat) <0 ) - goto out; +if (nsinfo__proc_stat ("self/ns/mnt" , &old_stat) <0 ) +return -1 ; -if (stat (newns, &new_stat) <0 ) - goto out; +scnprintf (newns,sizeof (newns),"%d/task/%d/ns/mnt" , +nsinfo__pid (nsi),nsinfo__tid (nsi)); +if (nsinfo__proc_stat (newns, &new_stat) <0 ) { +if (errno ==ENOENT || errno ==ESRCH ) +nsinfo__set_exited_locked (nsi); +return -1 ; + }/* Check if the mount namespaces differ, if so then indicate that we * want to switch as part of looking up dso/map data. */ if (old_stat.st_ino != new_stat.st_ino) {RC_CHK_ACCESS (nsi)->need_setns = true; -RC_CHK_ACCESS (nsi)->mntns_path = newns; - newns =NULL ; +RC_CHK_ACCESS (nsi)->mntns_ino = new_stat.st_ino; } -/* If we're dealing with a process that is in a different PID namespace, -* attempt to work out the innermost tgid for the process. -*/ -if (snprintf (spath,PATH_MAX ,"/proc/%d/task/%d/status" , -nsinfo__pid (nsi),nsinfo__tid (nsi)) >=PATH_MAX ) - goto out; - - rv =nsinfo__get_nspid (&RC_CHK_ACCESS (nsi)->tgid, &RC_CHK_ACCESS (nsi)->nstgid, - &RC_CHK_ACCESS (nsi)->in_pidns, spath); - -out: -free (newns); -return rv; +return 0 ; }int nsinfo__init (struct nsinfo *nsi)
@@ -165,7 +272,7 @@ int nsinfo__init(struct nsinfo *nsi)static void nsinfo__ensure_init_locked (struct nsinfo *nsi)__must_hold (nsinfo__lock (nsi)) { -if (!RC_CHK_ACCESS (nsi)->inited) +if (!RC_CHK_ACCESS (nsi)->inited && !RC_CHK_ACCESS (nsi)->exited)nsinfo__init_locked (nsi); }
@@ -212,8 +319,8 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi) {struct nsinfo *nnsi;pid_t pid, tid, tgid, nstgid; -bool inited, need_setns, in_pidns; -char *mntns_path =NULL ; +ino_t mntns_ino; +bool inited, exited, need_setns, in_pidns;if (nsi ==NULL )return NULL ;
@@ -227,17 +334,11 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi) tid =RC_CHK_ACCESS (nsi)->tid; tgid =RC_CHK_ACCESS (nsi)->tgid; nstgid =RC_CHK_ACCESS (nsi)->nstgid; + mntns_ino =RC_CHK_ACCESS (nsi)->mntns_ino; inited =RC_CHK_ACCESS (nsi)->inited; + exited =RC_CHK_ACCESS (nsi)->exited; need_setns =RC_CHK_ACCESS (nsi)->need_setns; in_pidns =RC_CHK_ACCESS (nsi)->in_pidns; -if (RC_CHK_ACCESS (nsi)->mntns_path) { - mntns_path =strdup (RC_CHK_ACCESS (nsi)->mntns_path); -if (!mntns_path) { -mutex_unlock (nsinfo__lock (nsi)); -nsinfo__put (nnsi); -return NULL ; - } - }mutex_unlock (nsinfo__lock (nsi));RC_CHK_ACCESS (nnsi)->pid = pid;
@@ -245,10 +346,11 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)mutex_lock (nsinfo__lock (nnsi));RC_CHK_ACCESS (nnsi)->tgid = tgid;RC_CHK_ACCESS (nnsi)->nstgid = nstgid; +RC_CHK_ACCESS (nnsi)->mntns_ino = mntns_ino;RC_CHK_ACCESS (nnsi)->inited = inited; +RC_CHK_ACCESS (nnsi)->exited = exited;RC_CHK_ACCESS (nnsi)->need_setns = need_setns;RC_CHK_ACCESS (nnsi)->in_pidns = in_pidns; -RC_CHK_ACCESS (nnsi)->mntns_path = mntns_path;mutex_unlock (nsinfo__lock (nnsi));return nnsi;
@@ -263,9 +365,6 @@ static void nsinfo__delete(struct nsinfo *nsi) {if (nsi) {WARN_ONCE (refcount_read (nsinfo__refcnt (nsi)) !=0 ,"nsinfo refcnt unbalanced\n" ); -mutex_lock (nsinfo__lock (nsi)); -zfree (&RC_CHK_ACCESS (nsi)->mntns_path); -mutex_unlock (nsinfo__lock (nsi));mutex_destroy (&RC_CHK_ACCESS (nsi)->lock);RC_CHK_FREE (nsi); }
@@ -289,6 +388,46 @@ void nsinfo__put(struct nsinfo *nsi)RC_CHK_PUT (nsi); } +bool nsinfo__inited (const struct nsinfo *nsi) +{ +bool ret; + +if (!nsi) +return false; + +mutex_lock (nsinfo__lock (nsi)); + ret =RC_CHK_ACCESS (nsi)->inited; +mutex_unlock (nsinfo__lock (nsi)); +return ret; +} + +bool nsinfo__exited (const struct nsinfo *nsi) +{ +bool ret; + +if (!nsi) +return false; + +mutex_lock (nsinfo__lock (nsi)); + ret =RC_CHK_ACCESS (nsi)->exited; +mutex_unlock (nsinfo__lock (nsi)); +return ret; +} + +ino_t nsinfo__mntns_ino (const struct nsinfo *nsi) +{ +ino_t ret; + +if (!nsi) +return 0 ; + +mutex_lock (nsinfo__lock (nsi)); +nsinfo__ensure_init_locked ((struct nsinfo *)nsi); + ret =RC_CHK_ACCESS (nsi)->mntns_ino; +mutex_unlock (nsinfo__lock (nsi)); +return ret; +} +bool nsinfo__need_setns (const struct nsinfo *nsi) {bool ret;
@@ -298,7 +437,7 @@ bool nsinfo__need_setns(const struct nsinfo *nsi)mutex_lock (nsinfo__lock (nsi));nsinfo__ensure_init_locked ((struct nsinfo *)nsi); - ret =RC_CHK_ACCESS (nsi)->need_setns; + ret = !RC_CHK_ACCESS (nsi)->exited &&RC_CHK_ACCESS (nsi)->need_setns;mutex_unlock (nsinfo__lock (nsi));return ret; }
@@ -309,12 +448,44 @@ void nsinfo__clear_need_setns(struct nsinfo *nsi)return ;mutex_lock (nsinfo__lock (nsi)); -RC_CHK_ACCESS (nsi)->inited = true; -RC_CHK_ACCESS (nsi)->need_setns = false; -zfree (&RC_CHK_ACCESS (nsi)->mntns_path); +nsinfo__clear_need_setns_locked (nsi);mutex_unlock (nsinfo__lock (nsi)); } +void nsinfo__set_exited (struct nsinfo *nsi) +{ +if (!nsi) +return ; + +mutex_lock (nsinfo__lock (nsi)); +nsinfo__set_exited_locked (nsi); +mutex_unlock (nsinfo__lock (nsi)); +} + +bool nsinfo__check_proc (struct nsinfo *nsi) +{ +pid_t tgid =0 , nstgid =0 ; +bool in_pidns = false; +bool exited; + +if (!nsi) +return false; + +mutex_lock (nsinfo__lock (nsi)); + exited =RC_CHK_ACCESS (nsi)->exited; +mutex_unlock (nsinfo__lock (nsi)); +if (exited) +return false; + +if (nsinfo__get_task_nspid (nsi, &tgid, &nstgid, &in_pidns) <0 || + tgid !=nsinfo__pid (nsi)) { +nsinfo__set_exited (nsi); +return false; + } + +return true; +} +pid_t nsinfo__tgid (const struct nsinfo *nsi) {pid_t ret;
@@ -369,7 +540,10 @@ void nsinfo__mountns_enter(struct nsinfo *nsi, struct nscookie *nc) {char curpath[PATH_MAX ]; -char *mntns_path =NULL ; +char mntns_path[64 ]; +ino_t mntns_ino =0 ; +bool need_setns = false; +struct stat st;int oldns =-1 ;int newns =-1 ;char *oldcwd =NULL ;
@@ -379,66 +553,74 @@ void nsinfo__mountns_enter(struct nsinfo *nsi, nc->oldns =-1 ; nc->newns =-1 ; + nc->oldcwd =NULL ;if (!nsi)return ;mutex_lock (nsinfo__lock (nsi));nsinfo__ensure_init_locked (nsi); -if (RC_CHK_ACCESS (nsi)->need_setns &&RC_CHK_ACCESS (nsi)->mntns_path) - mntns_path =strdup (RC_CHK_ACCESS (nsi)->mntns_path); +if (!RC_CHK_ACCESS (nsi)->exited &&RC_CHK_ACCESS (nsi)->need_setns) { + need_setns = true; + mntns_ino =RC_CHK_ACCESS (nsi)->mntns_ino; + }mutex_unlock (nsinfo__lock (nsi)); -if (!mntns_path) +if (!need_setns) +return ; + +if (!nsinfo__check_proc (nsi))return ;if (!getcwd (curpath,sizeof (curpath))) - goto out; +return ; oldcwd =strdup (curpath);if (!oldcwd) - goto out; +return ; + +/* Pin host /proc before entering any target mount namespace. */ +nsinfo__host_proc_get (); - oldns =open ("/proc/self/ns/mnt" ,O_RDONLY ); + oldns =nsinfo__proc_open ("self/ns/mnt" ,O_RDONLY |O_CLOEXEC );if (oldns <0 ) goto errout; - newns =open (mntns_path,O_RDONLY ); +scnprintf (mntns_path,sizeof (mntns_path),"%d/task/%d/ns/mnt" , +nsinfo__pid (nsi),nsinfo__tid (nsi)); + newns =nsinfo__proc_open (mntns_path,O_RDONLY |O_CLOEXEC );if (newns <0 ) goto errout; +if (fstat (newns, &st) <0 || st.st_ino != mntns_ino) { +nsinfo__clear_need_setns (nsi); + goto errout; + } +if (setns (newns,CLONE_NEWNS ) <0 ) goto errout; -free (mntns_path); nc->oldcwd = oldcwd; nc->oldns = oldns; nc->newns = newns;return ; errout: +nsinfo__host_proc_put ();free (oldcwd);if (oldns >-1 )close (oldns);if (newns >-1 )close (newns); -out: -free (mntns_path); }void nsinfo__mountns_exit (struct nscookie *nc) { -if (nc ==NULL || nc->oldns ==-1 || nc->newns ==-1 || !nc->oldcwd) +if (nc ==NULL || !nc->oldcwd)return ; -setns (nc->oldns,CLONE_NEWNS ); - -if (nc->oldcwd) { -WARN_ON_ONCE (chdir (nc->oldcwd)); -zfree (&nc->oldcwd); - } -if (nc->oldns >-1 ) { +setns (nc->oldns,CLONE_NEWNS );close (nc->oldns); nc->oldns =-1 ; }
@@ -447,6 +629,12 @@ void nsinfo__mountns_exit(struct nscookie *nc)close (nc->newns); nc->newns =-1 ; } + +if (nc->oldcwd) { +WARN_ON_ONCE (chdir (nc->oldcwd)); +zfree (&nc->oldcwd); + } +nsinfo__host_proc_put (); }char *nsinfo__realpath (const char *path,struct nsinfo *nsi)
@@ -478,6 +666,6 @@ bool nsinfo__is_in_root_namespace(void)pid_t tgid =0 , nstgid =0 ;bool in_pidns = false; -nsinfo__get_nspid (&tgid, &nstgid, &in_pidns,"/proc/self/status" ); +nsinfo__get_nspid (&tgid, &nstgid, &in_pidns,"self/status" );return !in_pidns; }
tools/perf/util/namespaces.h +8 -1
@@ -37,10 +37,11 @@ DECLARE_RC_STRUCT(nsinfo) {pid_t tid; pid_t tgid __guarded_by(&lock); pid_t nstgid __guarded_by(&lock); + ino_t mntns_ino __guarded_by(&lock); bool inited __guarded_by(&lock); + bool exited __guarded_by(&lock); bool need_setns __guarded_by(&lock); bool in_pidns __guarded_by(&lock); - char *mntns_path __guarded_by(&lock);refcount_t refcnt; };
@@ -58,8 +59,14 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi);struct nsinfo *nsinfo__get (struct nsinfo *nsi);void nsinfo__put (struct nsinfo *nsi); +bool nsinfo__inited (const struct nsinfo *nsi); +bool nsinfo__exited (const struct nsinfo *nsi); +ino_t nsinfo__mntns_ino (const struct nsinfo *nsi);bool nsinfo__need_setns (const struct nsinfo *nsi);void nsinfo__clear_need_setns (struct nsinfo *nsi); +void nsinfo__set_exited (struct nsinfo *nsi); +bool nsinfo__check_proc (struct nsinfo *nsi); +ssize_t nsinfo__proc_readlink (const char *rel_path,char *buf,size_t size);pid_t nsinfo__tgid (const struct nsinfo *nsi);pid_t nsinfo__nstgid (const struct nsinfo *nsi);pid_t nsinfo__pid (const struct nsinfo *nsi);
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Namhyung Kim
maintainerPATCH v3 7/11 perf tools: Check thread liveness when accessing /proc
In reply to Namhyung KimAdd thread__exited() and thread__is_alive(), and mark threads as exited on PERF_RECORD_EXIT, replaced parent and child threads on PERF_RECORD_FORK, and perf_event__exit_del_thread().
Propagate thread exit and boot ID mismatch to thread->nsinfo, guard /proc accesses with thread__is_alive() and nsinfo__check_proc() (for dso__filename_with_chroot(), while skipping chroot fallback when nsinfo__need_setns() is set), read /proc/<pid>/root via nsinfo__proc_readlink() in filename_with_chroot(), and avoid synthesizing a live parent thread in thread__e_machine_endian() when a sub-thread is no longer alive.
Also process PERF_RECORD_HEADER_FEATURE events in perf-inject so pipe-mode sessions populate session->header.env (including boot_id) for perf_env__is_same_boot().
--- tools/perf/builtin-inject.c | 14 +++++++- tools/perf/builtin-trace.c | 2 +- tools/perf/util/dso.c | 7 +++- tools/perf/util/event.c | 1 + tools/perf/util/machine.c | 17 +++++++--- tools/perf/util/thread.c | 65 ++++++++++++++++++++++++++++++++++--- tools/perf/util/thread.h | 7 ++-- tools/perf/util/util.c | 7 ++-- 8 files changed, 103 insertions(+), 17 deletions(-)
tools/perf/builtin-inject.c +13 -1
@@ -208,6 +208,18 @@ static int perf_event__repipe_op2_synth(const struct perf_tool *tool,return perf_event__repipe_synth (tool, event); } +static int perf_event__repipe_feature (const struct perf_tool *tool, +struct perf_session *session, +union perf_event *event) +{ +int ret =perf_event__process_feature (tool, session, event); + +if (ret) +return ret; + +return perf_event__repipe_synth (tool, event); +} + static int perf_event__repipe_op4_synth(const struct perf_tool *tool, struct perf_session *session __maybe_unused, union perf_event *event,
@@ -2853,7 +2865,7 @@ int cmd_inject(int argc, const char **argv) inject.tool.stat_config = perf_event__repipe_op2_synth; inject.tool.stat = perf_event__repipe_op2_synth; inject.tool.stat_round = perf_event__repipe_op2_synth; - inject.tool.feature = perf_event__repipe_op2_synth; + inject.tool.feature = perf_event__repipe_feature; inject.tool.finished_init = perf_event__repipe_op2_synth; inject.tool.compressed = perf_event__repipe_op4_synth; inject.tool.auxtrace = perf_event__repipe_auxtrace;
tools/perf/builtin-trace.c +1 -1
@@ -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))
tools/perf/util/dso.c +6 -1
@@ -567,7 +567,12 @@ static int do_open(char *name) __must_hold(&dso__data_open_lock)char *dso__filename_with_chroot (const struct dso *dso,const char *filename) { -return filename_with_chroot (nsinfo__pid (dso__nsinfo_const (dso)), filename); +struct nsinfo *nsi = (struct nsinfo *)dso__nsinfo_const (dso); + +if (nsinfo__need_setns (nsi) || !nsinfo__check_proc (nsi)) +return NULL ; + +return filename_with_chroot (nsinfo__pid (nsi), filename); } char *dso__get_filename(struct dso *dso, const char *root_dir, bool *decomp,
tools/perf/util/event.c +1 -0
@@ -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); }
tools/perf/util/machine.c +12 -5
@@ -518,8 +518,14 @@ static void machine__update_thread_pid(struct machine *machine,return ;thread__set_pid (th, pid); -nsinfo__zput (RC_CHK_ACCESS (th)->nsinfo); -RC_CHK_ACCESS (th)->nsinfo =nsinfo__new_thread (pid,thread__tid (th)); +thread__reset_nsinfo (th); +if (thread__exited (th) || !machine__is_host (machine) || + !perf_env__is_same_boot (machine->env)) { +struct nsinfo *nsi =thread__get_nsinfo (th); + +nsinfo__set_exited (nsi); +nsinfo__put (nsi); + }if (thread__pid (th) ==thread__tid (th))return ;
@@ -1978,6 +1984,7 @@ int machine__process_fork_event(struct machine *machine, union perf_event *event thread__pid(parent) != (pid_t)event->fork.ppid) {dump_printf ("removing erroneous parent thread %d/%d\n" ,thread__pid (parent),thread__tid (parent)); +thread__set_exited (parent,/*exited=*/ true);machine__remove_thread (machine, parent);thread__put (parent); parent = machine__findnew_thread(machine, event->fork.ppid,
@@ -1986,6 +1993,7 @@ int machine__process_fork_event(struct machine *machine, union perf_event *event/* if a thread currently exists for the thread id remove it */ if (thread !=NULL ) { +thread__set_exited (thread,/*exited=*/ true);machine__remove_thread (machine, thread);thread__put (thread); }
@@ -2033,9 +2041,8 @@ int machine__process_exit_event(struct machine *machine, union perf_event *event/* There is no context switch out before exit, so we decrement here. */ machine->parallelism--;if (thread !=NULL ) { -if (symbol_conf.keep_exited_threads) -thread__set_exited (thread,/*exited=*/ true); -else +thread__set_exited (thread,/*exited=*/ true); +if (!symbol_conf.keep_exited_threads)machine__remove_thread (machine, thread); }thread__put (thread);
tools/perf/util/thread.c +61 -4
102 lines not on this page · raw message
tools/perf/util/thread.h +5 -2
@@ -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;
tools/perf/util/util.c +4 -3
@@ -22,6 +22,7 @@#include <linux/overflow.h> #include <unistd.h> #include "cap.h" +#include "namespaces.h" #include "strlist.h" #include "string2.h"
@@ -460,10 +461,10 @@ char *filename_with_chroot(int pid, const char *filename)char buf[PATH_MAX ];char proc_root[32 ];char *new_name =NULL ; -int ret; +ssize_t ret; -scnprintf (proc_root,sizeof (proc_root),"/proc/%d/root" , pid); - ret =readlink (proc_root, buf,sizeof (buf) -1 ); +scnprintf (proc_root,sizeof (proc_root),"%d/root" , pid); + ret =nsinfo__proc_readlink (proc_root, buf,sizeof (buf) -1 );if (ret <=0 )return NULL ;
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Namhyung Kim
maintainerPATCH v3 8/11 perf tools: Export filename__read_build_id_ns()
In reply to Namhyung KimExport filename__read_build_id_ns(), including a chroot fallback guarded by !nsinfo__need_setns() && nsinfo__check_proc(), and use it in the dsos callback and perf-inject's dso__read_build_id() instead of open-coding nsinfo__mountns_enter() and dso__filename_with_chroot() fallbacks.
--- tools/perf/builtin-inject.c | 12 +----------- tools/perf/util/build-id.c | 15 ++++++++++++--- tools/perf/util/dsos.c | 17 +++-------------- tools/perf/util/symbol.h | 3 +++ 4 files changed, 19 insertions(+), 28 deletions(-)
tools/perf/builtin-inject.c +1 -11
@@ -906,24 +906,14 @@ static int perf_event__repipe_tracing_data(const struct perf_tool *tool,static int dso__read_build_id (struct dso *dso) { -struct nscookie nsc;struct build_id bid = { .size =0 , };if (dso__has_build_id (dso))return 0 ;mutex_lock (dso__lock (dso)); -nsinfo__mountns_enter (dso__nsinfo (dso), &nsc); -if (filename__read_build_id (dso__long_name (dso), &bid) >0 ) +if (filename__read_build_id_ns (dso__long_name (dso), &bid,dso__nsinfo (dso)) >0 )dso__set_build_id (dso, &bid); -else if (dso__nsinfo (dso)) { -char *new_name =dso__filename_with_chroot (dso,dso__long_name (dso)); - -if (new_name &&filename__read_build_id (new_name, &bid) >0 ) -dso__set_build_id (dso, &bid); -free (new_name); - } -nsinfo__mountns_exit (&nsc);mutex_unlock (dso__lock (dso));return dso__has_build_id (dso) ?0 :-1 ;
tools/perf/util/build-id.c +12 -3
@@ -850,15 +850,24 @@ int build_id_cache__remove_s(const char *sbuild_id)return err; } -static int filename__read_build_id_ns (const char *filename, -struct build_id *bid, -struct nsinfo *nsi) +int filename__read_build_id_ns (const char *filename, +struct build_id *bid, +struct nsinfo *nsi) {struct nscookie nsc;int ret;nsinfo__mountns_enter (nsi, &nsc); ret =filename__read_build_id (filename, bid); +if (ret <0 && errno ==ENOENT && !nsinfo__need_setns (nsi) && +nsinfo__check_proc (nsi)) { +char *new_name =filename_with_chroot (nsinfo__pid (nsi), filename); + +if (new_name) { + ret =filename__read_build_id (new_name, bid); +free (new_name); + } + }nsinfo__mountns_exit (&nsc);return ret;
tools/perf/util/dsos.c +3 -14
@@ -4,11 +4,11 @@#include "dso.h" #include "util.h" #include "vdso.h" -#include "namespaces.h" #include <errno.h> +#include <linux/perf_event.h> #include <stdlib.h> #include <string.h> -#include <symbol.h> // filename__read_build_id +#include <symbol.h> // filename__read_build_id_ns #include <unistd.h> void dsos__init (struct dsos *dsos)
@@ -72,7 +72,6 @@ struct dsos__read_build_ids_cb_args {static int dsos__read_build_ids_cb (struct dso *dso,void *data) {struct dsos__read_build_ids_cb_args *args = data; -struct nscookie nsc;struct build_id bid = { .size =0 , };if (args->with_hits && !dso__hit (dso) && !dso__is_vdso (dso))
@@ -81,20 +80,10 @@ static int dsos__read_build_ids_cb(struct dso *dso, void *data) args->have_build_id = true;return 0 ; } -nsinfo__mountns_enter (dso__nsinfo (dso), &nsc); -if (filename__read_build_id (dso__long_name (dso), &bid) >0 ) { +if (filename__read_build_id_ns (dso__long_name (dso), &bid,dso__nsinfo (dso)) >0 ) {dso__set_build_id (dso, &bid); args->have_build_id = true; - }else if (errno ==ENOENT &&dso__nsinfo (dso)) { -char *new_name =dso__filename_with_chroot (dso,dso__long_name (dso)); - -if (new_name &&filename__read_build_id (new_name, &bid) >0 ) { -dso__set_build_id (dso, &bid); - args->have_build_id = true; - } -free (new_name); } -nsinfo__mountns_exit (&nsc);return 0 ; }
tools/perf/util/symbol.h +3 -0
@@ -29,6 +29,7 @@struct dso ;struct map ;struct maps ; +struct nsinfo ;struct option ;struct build_id ;struct perf_env ;
@@ -238,6 +239,8 @@ struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name, size_enum dso_type dso__type_fd (int fd);int filename__read_build_id (const char *filename,struct build_id *id); +int filename__read_build_id_ns (const char *filename,struct build_id *bid, +struct nsinfo *nsi);int sysfs__read_build_id (const char *filename,struct build_id *bid); int modules__parse(const char *filename, void *arg, int (*process_module)(void *arg, const char *name,
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Namhyung Kim
maintainerPATCH v3 9/11 perf tools: Only enter mount namespace if necessary
In reply to Namhyung KimAdd 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().
--- tools/perf/util/capstone.c | 8 ++++-- tools/perf/util/debuginfo.c | 19 +++++++++----- tools/perf/util/disasm.c | 8 +++++- tools/perf/util/dso.c | 51 +++++++++++++++++++++++++++++-------- tools/perf/util/dso.h | 34 +++++++++++++++++++++++++ 5 files changed, 100 insertions(+), 20 deletions(-)
tools/perf/util/capstone.c +6 -2
@@ -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 ;
tools/perf/util/debuginfo.c +13 -6
@@ -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; }
tools/perf/util/disasm.c +7 -1
@@ -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 ) {
tools/perf/util/dso.c +40 -11
117 lines not on this page · raw message
tools/perf/util/dso.h +34 -0
@@ -142,6 +142,40 @@ enum dso_binary_type { DSO_BINARY_TYPE__NOT_FOUND, }; +static inline bool dso_binary_type__needs_ns (enum dso_binary_type type) +{ +switch (type) { +case DSO_BINARY_TYPE__SYSTEM_PATH_DSO : +case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE : +case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP : +case DSO_BINARY_TYPE__DEBUGLINK : +case DSO_BINARY_TYPE__GNU_DEBUGDATA : +case DSO_BINARY_TYPE__FEDORA_DEBUGINFO : +case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO : +case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO : +case DSO_BINARY_TYPE__BUILDID_DEBUGINFO : +case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO : +return true; +case DSO_BINARY_TYPE__KALLSYMS : +case DSO_BINARY_TYPE__GUEST_KALLSYMS : +case DSO_BINARY_TYPE__VMLINUX : +case DSO_BINARY_TYPE__GUEST_VMLINUX : +case DSO_BINARY_TYPE__JAVA_JIT : +case DSO_BINARY_TYPE__BUILD_ID_CACHE : +case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO : +case DSO_BINARY_TYPE__GUEST_KMODULE : +case DSO_BINARY_TYPE__GUEST_KMODULE_COMP : +case DSO_BINARY_TYPE__KCORE : +case DSO_BINARY_TYPE__GUEST_KCORE : +case DSO_BINARY_TYPE__BPF_PROG_INFO : +case DSO_BINARY_TYPE__BPF_IMAGE : +case DSO_BINARY_TYPE__OOL : +case DSO_BINARY_TYPE__NOT_FOUND : +default : +return false; + } +} +enum dso_space_type {DSO_SPACE__USER =0 ,DSO_SPACE__KERNEL ,
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Namhyung Kim
maintainerPATCH v3 10/11 perf tools: Scope mount namespace entry in dso__load()
In reply to Namhyung KimEnter 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.
--- tools/perf/util/symbol-minimal.c | 4 ++- tools/perf/util/symbol.c | 45 +++++++++++++++----------------- 2 files changed, 24 insertions(+), 25 deletions(-)
tools/perf/util/symbol-minimal.c +3 -1
@@ -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 ; }
tools/perf/util/symbol.c +21 -24
95 lines not on this page · raw message
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Namhyung Kim
maintainerPATCH v3 11/11 perf tests: Add namespace, nsinfo, and boot ID tests
In reply to Namhyung KimAdd a "Namespace and nsinfo tests" suite covering HEADER_BOOT_ID, perf_env__is_same_boot(), deferred /proc lookup, raw nsinfo__copy(), PID/TID recycling detection, thread exit and fork liveness tracking, dso_binary_type__needs_ns(), and isolated mountns, overmounted /proc, and chroot resolution.
Also verify the boot ID header matches /proc/sys/kernel/random/boot_id in the perf header and perf pipe shell tests.
--- tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/namespaces.c | 1188 +++++++++++++++++++++++++++ tools/perf/tests/shell/header.sh | 9 + tools/perf/tests/shell/pipe_test.sh | 21 + tools/perf/tests/tests.h | 1 + 6 files changed, 1221 insertions(+) create mode 100644 tools/perf/tests/namespaces.c
tools/perf/tests/Build +1 -0
@@ -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
tools/perf/tests/builtin-test.c +1 -0
@@ -192,6 +192,7 @@ static struct test_suite *generic_tests[] = { &suite__util, &suite__subcmd_help, &suite__kallsyms_split, + &suite__namespaces, NULL, };
tools/perf/tests/namespaces.c new file +1188 -0
1,188 lines not on this page · raw message
tools/perf/tests/shell/header.sh +9 -0
@@ -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() {
tools/perf/tests/shell/pipe_test.sh +21 -0
@@ -115,11 +115,32 @@ test_inject_bids() { echo "Inject ${inject_opt} build-ids test [Success]" } +test_inject_boot_id() { + echo + echo "Inject boot-id pipe test" + + if [ ! -r /proc/sys/kernel/random/boot_id ]; then + echo "Inject boot-id pipe test [Skipped - no /proc/sys/kernel/random/boot_id]" + return + fi + + boot_id=$(cat /proc/sys/kernel/random/boot_id) + if ! perf record -e task-clock:u -o - ${prog} | perf inject -b | perf report -i - --header-only -I | grep -q "boot id : ${boot_id}" + then + echo "Inject boot-id pipe test [Failed - boot_id mismatch in pipe]" + err=1 + return + fi + + echo "Inject boot-id pipe test [Success]" +} + test_record_report test_inject_bids -B test_inject_bids -b test_inject_bids --buildid-all test_inject_bids --mmap2-buildid-all +test_inject_boot_id cleanup exit $err
tools/perf/tests/tests.h +1 -0
@@ -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
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
[email protected]
AI reviewRe: [PATCH v3 11/11] perf tests: Add namespace, nsinfo, and boot ID tests
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] perf tests: leaked thread ref on failure in test__nsinfo_thread_liveness() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] perf tests: leaked thread ref on failure in test__nsinfo_thread_liveness()
--
commit bd9ea809a4bbf37b8dd8dcc52bfc3183504775fe
Author: Namhyung Kim <[email protected]>
perf tests: Add namespace, nsinfo, and boot ID tests
This commit adds a namespace and nsinfo unit test suite and extends the header and pipe shell tests to verify boot ID handling.
Quoted 5 lines
tools/perf/tests/namespaces.c new file +0
-0
[ ... ]
Quoted 3 lines
tools/perf/tests/namespaces.c new file +3
-0+static int test__nsinfo_thread_liveness (struct test_suite *test__maybe_unused , +int subtest__maybe_unused ) +{
[ ... ]
tools/perf/tests/namespaces.c line 483
Quoted 9 lines
tools/perf/tests/namespaces.c new file +9
-0+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; + }
[Severity: Low]
Does this leak a struct thread reference if the check fails? machine__find_thread() returns a refcounted pointer that must be released with thread__put(), so assigning the result to off_th (as done with th in the later perf_event__exit_del_thread() check) would let thread__put(off_th) at the out label release it on failure.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
[email protected]
AI reviewSashiko has finished reviewing this 11-patch series:
Sashiko has finished reviewing this 11-patch series:
- Patches 1-10: no issues found.
- Patch 11 ("perf tests: Add namespace, nsinfo, and boot ID tests"): 1 potential issue (1 Low)
A detailed review has been sent as a separate reply to patch 11.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]