kernfs: Build paths in linear time with respect to depth
Subsystems (6)
v3 cover kernfs: Build paths in linear time with respect to depth
kernfs_path_from_node() walks back up from the leaf for each name in the path, so the work grows with the square of the depth, and reading /proc/<pid>/cgroup does that with IRQs off and css_set_lock held. Patch 1 builds the path in a single walk instead. As Tejun suggested on v1, a path which doesn't fit now returns -E2BIG and keeps its end rather than its start, with "..." in front of it. Reads of /proc/<pid>/cgroup, and of /proc/<pid>/mountinfo with a cgroup mount rooted at such a path, fail again as they did before v6.8, when the name loop stopped reporting truncation. Patches 2 and 3 update sched/debug, which added its own "..." at the end, and cgroup_iter, which assumed that nothing would be left in the buffer.
Patches 2 and 3 depend on patch 1. Patch 2 will conflict with tip's sched/core, where acf8d1424c1a ("sched/fair: Use list_for_each_entry_rcu() in print_cfs_stats()") moves SEQ_printf_task_group_path() to kernel/sched/sched.h. The resolution is to make the same change to the macro there, since otherwise the path gets "..." at both ends.
v3:
- Put "..." at the start of a path which doesn't fit in kernfs_path_from_node() itself, so sched/debug only drops its suffix (Tejun)
- Have pr_cont_kernfs_path() print what's in the buffer when the path doesn't fit, instead of "(name too long)" (Tejun)
- Put the opening /* of the comment in cgroup_iter on a line of its own (Sashiko)
v2:
- Keep the end of the path and return -E2BIG when it doesn't fit, instead of wrapping around the buffer to keep the start (Tejun)
- Say which reads now fail when the path doesn't fit
- Stop walking as soon as the buffer is full
- Update the kernel-doc for the kernfs_path*() functions to match
- Add patches to move sched/debug's "..." to the start of a truncated path, and to update the comment in cgroup_iter's fdinfo
- Add numbers for more realistic depths, and for the effect on fork and exit on other CPUs (Tejun)
- Say that it's up to the parent to set cgroup.max.depth when delegating a subtree (Tejun)
- Drop Fixes: and Cc: stable, since this now changes behaviour as well as speed
- Rebase onto driver-core-next
v2: https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/r/[email protected]
Chris Down (3):
kernfs: Build paths in linear time with respect to depth sched/debug: Drop the "..." suffix from truncated group paths
bpf: Describe cgroup_iter's fdinfo path for paths which don't fit
fs/kernfs/dir.c | 83 +++++++++++++++++++++++++--------------- include/linux/kernfs.h | 7 ++-- kernel/bpf/cgroup_iter.c | 6 ++- kernel/sched/debug.c | 8 +--- 4 files changed, 63 insertions(+), 41 deletions(-)
base-commit: f62fba71a846ebe59423bfd057af023b418935a0
Signature · 1 line
2.56.1
PATCH v3 1/3 kernfs: Build paths in linear time with respect to depth
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 117usThe 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 "..." followed by 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. pr_cont_kernfs_path() now does the same, rather than print "(name too long)".
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 643usAt 15000 the path doesn't fit. Before this change it is truncated, and after it the read fails.
--- fs/kernfs/dir.c | 83 +++++++++++++++++++++++++++--------------- include/linux/kernfs.h | 7 ++-- 2 files changed, 57 insertions(+), 33 deletions(-)
fs/kernfs/dir.c +53 -30
@@ -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,10 @@ 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 "..." followed by 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 +146,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,30 +164,38 @@ 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, keep the end of it and put "..." at the start. +*/ + 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 )) + goto too_long; + } - 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 )) + goto too_long; } + len = buflen -1 - pos; +memmove (buf, buf + pos, len +1 ); +return len; + +too_long: +memcpy (buf,"..." ,min (buflen -1 ,3 )); +return -E2BIG ; }/**
@@ -217,9 +242,10 @@ 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 "..." followed by 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)
@@ -271,11 +297,8 @@ void pr_cont_kernfs_path(struct kernfs_node *kn) sz =kernfs_path_from_node (kn,NULL , kernfs_pr_cont_buf,sizeof (kernfs_pr_cont_buf)); -if (sz <0 ) { -if (sz == -E2BIG ) -pr_cont ("(name too long)" ); -else -pr_cont ("(error)" ); +if (sz <0 && sz != -E2BIG ) { +pr_cont ("(error)" ); goto out; }
include/linux/kernfs.h +4 -3
@@ -608,9 +608,10 @@ 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 "..." followed by 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) {
Signature · 1 line
2.56.1
PATCH v3 2/3 sched/debug: Drop the "..." suffix from truncated group paths
In reply to Chris DownWhen another reader holds sched_debug_lock, SEQ_printf_task_group_path() builds the group path in a 128 byte buffer on the stack, and writes "..." over the end of it in case the path filled it. Since the previous commit, cgroup_path() keeps the end of a path which doesn't fit and puts "..." at its start, so a long path now prints with "..." at both ends, which suggests the end was cut off when it wasn't.
Let's drop the suffix and leave marking the truncation to cgroup_path().
--- kernel/sched/debug.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
kernel/sched/debug.c +2 -6
@@ -941,9 +941,7 @@ static void task_group_path(struct task_group *tg, char *path, int plen)/* * Only 1 SEQ_printf_task_group_path() caller can use the full length * group_path[] for cgroup path. Other simultaneous callers will have -* to use a shorter stack buffer. A "..." suffix is appended at the end -* of the stack buffer so that it will show up in case the output length -* matches the given buffer size to indicate possible path name truncation. +* to use a shorter stack buffer. */ #define SEQ_printf_task_group_path(m, tg, fmt...) \ { \
@@ -953,9 +951,7 @@ static void task_group_path(struct task_group *tg, char *path, int plen)spin_unlock (&sched_debug_lock); \ }else { \char buf[128 ]; \ -char *bufend = buf +sizeof (buf) -3 ; \ -task_group_path (tg, buf, bufend - buf); \ -strcpy (bufend -1 ,"..." ); \ +task_group_path (tg, buf,sizeof (buf)); \SEQ_printf (m, fmt, buf); \ } \ }
Signature · 1 line
2.56.1
PATCH v3 3/3 bpf: Describe cgroup_iter's fdinfo path for paths which don't fit
In reply to Chris Downbpf_iter_cgroup_show_fdinfo() says that if cgroup_path_ns() fails, buf is an empty string and nothing is printed. That isn't the case for a path which doesn't fit in PATH_MAX: cgroup_path_ns() now returns -E2BIG with "..." followed by the end of the path in buf.
The end of the path is still useful to show, so let's update the comment to say so rather than change what is printed.
(Also since we're here, let's make the comment BPF style to avoid the wrath of Sashiko.)
--- kernel/bpf/cgroup_iter.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/bpf/cgroup_iter.c +4 -2
@@ -250,8 +250,10 @@ static void bpf_iter_cgroup_show_fdinfo(const struct bpf_iter_aux_info *aux, goto show_order; } -/* If cgroup_path_ns() fails, buf will be an empty string, cgroup_path -* will print nothing. +/* +* If cgroup_path_ns() fails, buf will be an empty string, cgroup_path +* will print nothing. The exception is a path which doesn't fit, where +* buf holds "..." followed by as much of the end of the path as fits. * * Path is in the calling process's cgroup namespace. */
Signature · 1 line
2.56.1
Quoted 11 lines · Chris Down
kernfs_path_from_node()walks back up from the leaf for each name in the path, so the work grows with the square of the depth, and reading /proc/<pid>/cgroup does that with IRQs off andcss_set_lockheld. Patch 1 builds the path in a single walk instead. As Tejun suggested on v1, a path which doesn't fit now returns-E2BIGand keeps its end rather than its start, with "..." in front of it. Reads of /proc/<pid>/cgroup, and of /proc/<pid>/mountinfo with a cgroup mount rooted at such a path, fail again as they did before v6.8, when the name loop stopped reporting truncation. Patches 2 and 3 update sched/debug, which added its own "..." at the end, andcgroup_iter, which assumed that nothing would be left in the buffer.
For the series,
Thanks.
--
tejun
Re: [PATCH v3 2/3] sched/debug: Drop the "..." suffix from truncated group paths
In reply to Chris DownQuoted 11 lines · Chris Down
When another reader holds
sched_debug_lock,SEQ_printf_task_group_path()builds the group path in a 128 byte buffer on the stack, and writes "..." over the end of it in case the path filled it. Since the previous commit,cgroup_path()keeps the end of a path which doesn't fit and puts "..." at its start, so a long path now prints with "..." at both ends, which suggests the end was cut off when it wasn't.Let's drop the suffix and leave marking the truncation to
cgroup_path().Signed-off-by Chris Down <[email protected]>
So truncated paths now look like: "...oo/bar", rather than "/foo/b..." ?
Quoted 33 lines
--- kernel/sched/debug.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)kernel/sched/debug.c +2
-6@@ -941,9 +941,7 @@ static void task_group_path(struct task_group *tg, char *path, int plen)/* * Only 1 SEQ_printf_task_group_path() caller can use the full length * group_path[] for cgroup path. Other simultaneous callers will have -* to use a shorter stack buffer. A "..." suffix is appended at the end -* of the stack buffer so that it will show up in case the output length -* matches the given buffer size to indicate possible path name truncation. +* to use a shorter stack buffer. */ #define SEQ_printf_task_group_path(m, tg, fmt...) \ { \@@ -953,9 +951,7 @@ static void task_group_path(struct task_group *tg, char *path, int plen)spin_unlock (&sched_debug_lock); \ }else { \char buf[128 ]; \ -char *bufend = buf +sizeof (buf) -3 ; \ -task_group_path (tg, buf, bufend - buf); \ -strcpy (bufend -1 ,"..." ); \ +task_group_path (tg, buf,sizeof (buf)); \SEQ_printf (m, fmt, buf); \ } \ }Signature · 1 line
2.56.1
Re: [PATCH v3 2/3] sched/debug: Drop the "..." suffix from truncated group paths
Hi Peter,
Quoted 1 line · Peter Zijlstra
So truncated paths now look like: "...oo/bar", rather than "/foo/b..." ?
That's correct :-) Since the leaf is usually the part one cares about when differentiating long paths, this seemed the more useful end to keep (thanks, Tejun).
Quoted 1 line
Thanks for the review!
Chris