From: Chris Down <chris@chrisdown.name>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Tejun Heo <tj@kernel.org>
Cc: "Johannes Weiner" <hannes@cmpxchg.org>,
"Michal Koutný" <mkoutny@suse.com>,
"Serge Hallyn" <serge.hallyn@ubuntu.com>,
"Ingo Molnar" <mingo@redhat.com>,
"Peter Zijlstra" <peterz@infradead.org>,
"Juri Lelli" <juri.lelli@redhat.com>,
"Vincent Guittot" <vincent.guittot@linaro.org>,
"Dietmar Eggemann" <dietmar.eggemann@arm.com>,
"Steven Rostedt" <rostedt@goodmis.org>,
"Ben Segall" <bsegall@google.com>, "Mel Gorman" <mgorman@suse.de>,
"Valentin Schneider" <vschneid@redhat.com>,
"K Prateek Nayak" <kprateek.nayak@amd.com>,
"Yonghong Song" <yonghong.song@linux.dev>,
"Alexei Starovoitov" <ast@kernel.org>,
"Daniel Borkmann" <daniel@iogearbox.net>,
"Andrii Nakryiko" <andrii@kernel.org>,
"Eduard Zingerman" <eddyz87@gmail.com>,
"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
driver-core@lists.linux.dev, cgroups@vger.kernel.org,
bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
kernel-team@meta.com
Subject: [PATCH v2 1/3] kernfs: Build paths in linear time with respect to depth
Date: Sat, 28 Nov 2026 21:38:22 -0800 [thread overview]
Message-ID: <585ed83ddec44c113953c0c42bcae759b3074e9b.1795930577.git.chris@chrisdown.name> (raw)
In-Reply-To: <cover.1795930577.git.chris@chrisdown.name>
kernfs nodes only point to their parent. To build a path top-down,
kernfs_path_from_node_locked() walks back up from the leaf for each
name, so the work grows with the square of the depth. Reading
/proc/<pid>/cgroup does this with IRQs off and css_set_lock held, so a
read from a deep delegated subtree can hold up fork and exit elsewhere
on the machine, which is certainly suboptimal.
Nobody needs a hierarchy that deep, and it's up to whoever delegates a
subtree to set cgroup.max.depth on it. But cgroup.max.depth defaults to
max, so where that hasn't been done, anyone with a delegated subtree can
make one. As an example, take a 4 CPU VM where one task reads
/proc/self/cgroup in a loop from the bottom of a chain of cgroups. The
median time for a fork() and exit on another CPU is:
depth of reader before after
(no reader) 51us 52us
64 56us 53us
256 363us 60us
1024 5.0ms 93us
2046 21.9ms 117us
The names are a single character, so at 2046 the path is just short of
PATH_MAX.
Let's fill the buffer from the end as we walk up instead, and move the
path to the front once it's complete.
This also changes what happens when the path doesn't fit: we now return
-E2BIG and leave the end of the path in the buffer. Reads of
/proc/<pid>/cgroup then fail with ENAMETOOLONG instead of getting the
first 4095 bytes, and reads of /proc/<pid>/mountinfo fail with ERANGE
once they reach a cgroup mount whose root is such a path. That's how
both behaved until commit ff6d413b0b59 ("kernfs: Convert
kernfs_path_from_node_locked() from strlcpy() to strscpy()"), which
changed these callers to check for -E2BIG but copied the names with
scnprintf(), so it was never returned. Callers which ignore the return
value, like the tracepoints, print the end of the path, which is the
more useful part.
We also now return -E2BIG for a zero length buffer instead of writing to
it, although no caller passes one.
In the same VM, the median time for a read of /proc/self/cgroup from the
bottom of the chain is:
depth before after
4 304ns 258ns
16 549ns 396ns
64 2.8us 1.0us
256 69us 4.2us
1024 2.5ms 20us
2046 12.0ms 43us
15000 3.3s 643us
At 15000 the path doesn't fit. Before this change it is truncated, and
after it the read fails.
Signed-off-by: Chris Down <chris@chrisdown.name>
---
fs/kernfs/dir.c | 70 +++++++++++++++++++++++++++---------------
include/linux/kernfs.h | 6 ++--
2 files changed, 48 insertions(+), 28 deletions(-)
diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 800af10d9511..92270c9ac143 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -94,6 +94,21 @@ static struct kernfs_node *kernfs_common_ancestor(struct kernfs_node *a,
return a;
}
+/*
+ * Put @str in front of the path which starts at @buf + *@pos. If it doesn't
+ * all fit, keep the end of it and return false.
+ */
+static bool kernfs_path_prepend(char *buf, size_t *pos, const char *str,
+ size_t len)
+{
+ size_t copy = min(len, *pos);
+
+ *pos -= copy;
+ memcpy(buf + *pos, str + len - copy, copy);
+
+ return copy == len;
+}
+
/**
* kernfs_path_from_node_locked - find a pseudo-absolute path to @kn_to,
* where kn_from is treated as root of the path.
@@ -120,9 +135,9 @@ static struct kernfs_node *kernfs_common_ancestor(struct kernfs_node *a,
*
* [3] when @kn_to is %NULL result will be "(null)"
*
- * Return: the length of the constructed path. If the path would have been
- * greater than @buflen, @buf contains the truncated path with the trailing
- * '\0'. On error, -errno is returned.
+ * Return: the length of the constructed path. If the path doesn't fit in
+ * @buf, -E2BIG is returned and @buf contains as much of the end of the path
+ * as fits, with the trailing '\0'. On other errors, -errno is returned.
*/
static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
struct kernfs_node *kn_from,
@@ -130,9 +145,10 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
{
struct kernfs_node *kn, *common;
const char parent_str[] = "/..";
- size_t depth_from, depth_to, len = 0;
- ssize_t copied;
- int i, j;
+ size_t depth_from, pos, len, i;
+
+ if (!buflen)
+ return -E2BIG;
if (!kn_to)
return strscpy(buf, "(null)", buflen);
@@ -147,29 +163,33 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
if (WARN_ON(!common))
return -EINVAL;
- depth_to = kernfs_depth(common, kn_to);
depth_from = kernfs_depth(common, kn_from);
- buf[0] = '\0';
-
- for (i = 0; i < depth_from; i++) {
- copied = strscpy(buf + len, parent_str, buflen - len);
- if (copied < 0)
- return copied;
- len += copied;
- }
+ /*
+ * We can only walk towards the root, so fill @buf from the end and
+ * move the path to the front once it's complete. If the path is too
+ * long, we stop and keep the end of it.
+ */
+ pos = buflen - 1;
+ buf[pos] = '\0';
- /* Calculate how many bytes we need for the rest */
- for (i = depth_to - 1; i >= 0; i--) {
- const char *name;
+ for (kn = kn_to; kn != common; kn = rcu_dereference(kn->__parent)) {
+ const char *name = rcu_dereference(kn->name);
- for (kn = kn_to, j = 0; j < i; j++)
- kn = rcu_dereference(kn->__parent);
+ if (!kernfs_path_prepend(buf, &pos, name, strlen(name)) ||
+ !kernfs_path_prepend(buf, &pos, "/", 1))
+ return -E2BIG;
+ }
- name = rcu_dereference(kn->name);
- len += scnprintf(buf + len, buflen - len, "/%s", name);
+ for (i = 0; i < depth_from; i++) {
+ if (!kernfs_path_prepend(buf, &pos, parent_str,
+ sizeof(parent_str) - 1))
+ return -E2BIG;
}
+ len = buflen - 1 - pos;
+ memmove(buf, buf + pos, len + 1);
+
return len;
}
@@ -217,9 +237,9 @@ int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
* path (which includes '..'s) as needed to reach from @from to @to is
* returned.
*
- * Return: the length of the constructed path. If the path would have been
- * greater than @buflen, @buf contains the truncated path with the trailing
- * '\0'. On error, -errno is returned.
+ * Return: the length of the constructed path. If the path doesn't fit in
+ * @buf, -E2BIG is returned and @buf contains as much of the end of the path
+ * as fits, with the trailing '\0'. On other errors, -errno is returned.
*/
int kernfs_path_from_node(struct kernfs_node *to, struct kernfs_node *from,
char *buf, size_t buflen)
diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h
index 73786b567ccd..056a61edfd62 100644
--- a/include/linux/kernfs.h
+++ b/include/linux/kernfs.h
@@ -608,9 +608,9 @@ static inline void kernfs_init(void) { }
*
* If @kn is NULL result will be "(null)".
*
- * Returns the length of the full path. If the full length is equal to or
- * greater than @buflen, @buf contains the truncated path with the trailing
- * '\0'. On error, -errno is returned.
+ * Return: the length of the constructed path. If the path doesn't fit in
+ * @buf, -E2BIG is returned and @buf contains as much of the end of the path
+ * as fits, with the trailing '\0'. On other errors, -errno is returned.
*/
static inline int kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen)
{
--
2.56.1
next prev parent reply other threads:[~2026-11-29 5:38 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-29 5:38 [PATCH v2 0/3] " Chris Down
2026-11-29 5:38 ` Chris Down [this message]
2026-11-29 5:38 ` [PATCH v2 2/3] sched/debug: Mark a truncated group path at the start Chris Down
2026-11-29 5:38 ` [PATCH v2 3/3] bpf: Describe cgroup_iter's fdinfo path for paths which don't fit Chris Down
2026-11-29 16:36 ` [PATCH v2 0/3] kernfs: Build paths in linear time with respect to depth Tejun Heo
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=585ed83ddec44c113953c0c42bcae759b3074e9b.1795930577.git.chris@chrisdown.name \
--to=chris@chrisdown.name \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=bsegall@google.com \
--cc=cgroups@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=dietmar.eggemann@arm.com \
--cc=driver-core@lists.linux.dev \
--cc=eddyz87@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=hannes@cmpxchg.org \
--cc=juri.lelli@redhat.com \
--cc=kernel-team@meta.com \
--cc=kprateek.nayak@amd.com \
--cc=linux-kernel@vger.kernel.org \
--cc=memxor@gmail.com \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=mkoutny@suse.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=serge.hallyn@ubuntu.com \
--cc=tj@kernel.org \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
--cc=yonghong.song@linux.dev \
/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®