From: Namhyung Kim <namhyung@kernel.org>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Ian Rogers <irogers@google.com>, Jiri Olsa <jolsa@kernel.org>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@kernel.org>,
LKML <linux-kernel@vger.kernel.org>,
linux-perf-users@vger.kernel.org
Subject: [PATCH v1 07/11] perf tools: Handle chroot inside nsinfo__mountns_enter()
Date: Fri, 23 Oct 2026 00:11:37 -0700 [thread overview]
Message-ID: <20261023071141.134149-8-namhyung@kernel.org> (raw)
In-Reply-To: <20261023071141.134149-1-namhyung@kernel.org>
Read /proc/<pid>/task/<tid>/root in nsinfo__init() and enter/exit the
chroot directory alongside the mount namespace inside
nsinfo__mountns_enter() and nsinfo__mountns_exit().
Assisted-by: LLM
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
tools/perf/util/namespaces.c | 60 +++++++++++++++++++++++++++++++++++-
tools/perf/util/namespaces.h | 3 ++
2 files changed, 62 insertions(+), 1 deletion(-)
diff --git a/tools/perf/util/namespaces.c b/tools/perf/util/namespaces.c
index bb119a6cc6064d89..1ec6b1ed4db23109 100644
--- a/tools/perf/util/namespaces.c
+++ b/tools/perf/util/namespaces.c
@@ -100,10 +100,12 @@ int nsinfo__init(struct nsinfo *nsi)
{
char oldns[PATH_MAX];
char spath[PATH_MAX];
+ char rpath[64];
char *newns = NULL;
struct stat old_stat;
struct stat new_stat;
int rv = -1;
+ ssize_t ret;
if (!nsi || RC_CHK_ACCESS(nsi)->exited)
return -1;
@@ -150,6 +152,19 @@ int nsinfo__init(struct nsinfo *nsi)
newns = NULL;
}
+ /* Check if the task is in a chroot directory. */
+ scnprintf(rpath, sizeof(rpath), "/proc/%d/task/%d/root",
+ nsinfo__pid(nsi), nsinfo__tid(nsi));
+ ret = readlink(rpath, spath, sizeof(spath) - 1);
+ if (ret > 0) {
+ spath[ret] = '\0';
+ if (strcmp(spath, "/") && !strstr(spath, "(deleted)")) {
+ RC_CHK_ACCESS(nsi)->chroot = strdup(spath);
+ if (RC_CHK_ACCESS(nsi)->chroot)
+ RC_CHK_ACCESS(nsi)->in_chroot = true;
+ }
+ }
+
out:
free(newns);
return rv;
@@ -201,6 +216,11 @@ static const char *nsinfo__mntns_path(const struct nsinfo *nsi)
return RC_CHK_ACCESS(nsi)->mntns_path;
}
+static const char *nsinfo__chroot(const struct nsinfo *nsi)
+{
+ return RC_CHK_ACCESS(nsi)->chroot;
+}
+
struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
{
struct nsinfo *nnsi;
@@ -222,6 +242,7 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
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_chroot = RC_CHK_ACCESS(nsi)->in_chroot;
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));
@@ -230,6 +251,13 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
return NULL;
}
}
+ if (nsinfo__chroot(nsi)) {
+ RC_CHK_ACCESS(nnsi)->chroot = strdup(nsinfo__chroot(nsi));
+ if (!RC_CHK_ACCESS(nnsi)->chroot) {
+ nsinfo__put(nnsi);
+ return NULL;
+ }
+ }
return nnsi;
}
@@ -244,6 +272,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);
+ zfree(&RC_CHK_ACCESS(nsi)->chroot);
RC_CHK_FREE(nsi);
}
}
@@ -272,7 +301,8 @@ bool nsinfo__need_setns(const struct nsinfo *nsi)
return false;
nsinfo__ensure_init((struct nsinfo *)nsi);
- return !RC_CHK_ACCESS(nsi)->exited && RC_CHK_ACCESS(nsi)->need_setns;
+ return !RC_CHK_ACCESS(nsi)->exited &&
+ (RC_CHK_ACCESS(nsi)->need_setns || RC_CHK_ACCESS(nsi)->in_chroot);
}
void nsinfo__clear_need_setns(struct nsinfo *nsi)
@@ -282,7 +312,9 @@ void nsinfo__clear_need_setns(struct nsinfo *nsi)
RC_CHK_ACCESS(nsi)->inited = true;
RC_CHK_ACCESS(nsi)->need_setns = false;
+ RC_CHK_ACCESS(nsi)->in_chroot = false;
zfree(&RC_CHK_ACCESS(nsi)->mntns_path);
+ zfree(&RC_CHK_ACCESS(nsi)->chroot);
}
void nsinfo__set_exited(struct nsinfo *nsi)
@@ -362,6 +394,7 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
char curpath[PATH_MAX];
int oldns = -1;
int newns = -1;
+ int oldroot = -1;
char *oldcwd = NULL;
if (nc == NULL)
@@ -369,6 +402,7 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
nc->oldns = -1;
nc->newns = -1;
+ nc->oldroot = -1;
nc->oldcwd = NULL;
if (!nsi || !nsinfo__need_setns(nsi))
@@ -405,13 +439,30 @@ void nsinfo__mountns_enter(struct nsinfo *nsi,
goto errout;
}
+ if (RC_CHK_ACCESS(nsi)->in_chroot) {
+ oldroot = open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+ if (oldroot < 0)
+ goto errout_setns;
+
+ if (chroot(nsinfo__chroot(nsi)) < 0)
+ goto errout_setns;
+ }
+
nc->oldcwd = oldcwd;
nc->oldns = oldns;
nc->newns = newns;
+ nc->oldroot = oldroot;
return;
+errout_setns:
+ if (oldns > -1) {
+ setns(oldns, CLONE_NEWNS);
+ WARN_ON_ONCE(chdir(oldcwd));
+ }
errout:
free(oldcwd);
+ if (oldroot > -1)
+ close(oldroot);
if (oldns > -1)
close(oldns);
if (newns > -1)
@@ -423,6 +474,13 @@ void nsinfo__mountns_exit(struct nscookie *nc)
if (nc == NULL || !nc->oldcwd)
return;
+ if (nc->oldroot > -1) {
+ WARN_ON_ONCE(fchdir(nc->oldroot));
+ WARN_ON_ONCE(chroot("."));
+ close(nc->oldroot);
+ nc->oldroot = -1;
+ }
+
if (nc->oldns > -1) {
setns(nc->oldns, CLONE_NEWNS);
close(nc->oldns);
diff --git a/tools/perf/util/namespaces.h b/tools/perf/util/namespaces.h
index 165e9c9994461595..640ce537295a88e5 100644
--- a/tools/perf/util/namespaces.h
+++ b/tools/perf/util/namespaces.h
@@ -39,14 +39,17 @@ DECLARE_RC_STRUCT(nsinfo) {
bool inited;
bool exited;
bool need_setns;
+ bool in_chroot;
bool in_pidns;
char *mntns_path;
+ char *chroot;
refcount_t refcnt;
};
struct nscookie {
int oldns;
int newns;
+ int oldroot;
char *oldcwd;
};
--
2.56.0.257.gf7497c7d27-goog
next prev parent reply other threads:[~2026-10-23 7:11 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-23 7:11 [PATCH v1 00/11] perf tools: Enhance namespace and chroot handling Namhyung Kim
2026-10-23 7:11 ` [PATCH v1 01/11] perf tools: Add HEADER_BOOT_ID feature Namhyung Kim
2026-10-23 7:11 ` [PATCH v1 02/11] perf tools: Add perf_env__is_same_boot() Namhyung Kim
2026-10-23 7:11 ` [PATCH v1 03/11] perf tools: Add nsinfo__new_thread() to track tid Namhyung Kim
2026-10-23 7:11 ` [PATCH v1 04/11] perf tools: Defer /proc lookup until nsinfo is used Namhyung Kim
2026-10-23 7:11 ` [PATCH v1 05/11] perf tools: Verify thread status and mount namespace inode Namhyung Kim
2026-10-23 7:11 ` [PATCH v1 06/11] perf tools: Check thread liveness when accessing /proc Namhyung Kim
2026-10-23 7:11 ` Namhyung Kim [this message]
2026-10-23 7:11 ` [PATCH v1 08/11] perf tools: Export filename__read_build_id_ns() Namhyung Kim
2026-10-23 7:11 ` [PATCH v1 09/11] perf tools: Remove redundant chroot handling Namhyung Kim
2026-10-23 7:11 ` [PATCH v1 10/11] perf tools: Only enter mount namespace if necessary Namhyung Kim
2026-10-23 7:11 ` [PATCH v1 11/11] perf tools: Scope mount namespace entry in dso__load() Namhyung Kim
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261023071141.134149-8-namhyung@kernel.org \
--to=namhyung@kernel.org \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®