Skip to content

Commit 7c17909

Browse files
mjguzikbrauner
authored andcommitted
fs: add predicts based on nd->depth
Stats from nd->depth usage during the venerable kernel build collected like so: bpftrace -e 'kprobe:terminate_walk,kprobe:walk_component,kprobe:legitimize_links { @[probe] = lhist(((struct nameidata *)arg0)->depth, 0, 8, 1); }' @[kprobe:legitimize_links]: [0, 1) 6554906 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [1, 2) 3534 | | @[kprobe:terminate_walk]: [0, 1) 12153664 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| @[kprobe:walk_component]: [0, 1) 53075749 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [1, 2) 971421 | | [2, 3) 84946 | | Additionally a custom probe was added for depth within link_path_walk(): bpftrace -e 'kprobe:link_path_walk_probe { @[probe] = lhist(arg0, 0, 8, 1); }' @[kprobe:link_path_walk_probe]: [0, 1) 7528231 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [1, 2) 407905 |@@ | Given these results: 1. terminate_walk() is called towards the end of the lookup and in this test it never had any links to clean up. 2. legitimize_links() is also called towards the end of lookup and most of the time there s 0 depth. Patch consumers to avoid calling into it in that case. 3. walk_component() is typically called with WALK_MORE and zero depth, checked in that order. Check depth first and predict it is 0. 4. link_path_walk() also does not deal with a symlink most of the time when !*name Signed-off-by: Mateusz Guzik <mjguzik@gmail.com> Link: https://patch.msgid.link/20251119142954.2909394-1-mjguzik@gmail.com Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent bfef6e1 commit 7c17909

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

fs/namei.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,8 @@ static void leave_rcu(struct nameidata *nd)
785785

786786
static void terminate_walk(struct nameidata *nd)
787787
{
788-
drop_links(nd);
788+
if (unlikely(nd->depth))
789+
drop_links(nd);
789790
if (!(nd->flags & LOOKUP_RCU)) {
790791
int i;
791792
path_put(&nd->path);
@@ -882,7 +883,7 @@ static bool try_to_unlazy(struct nameidata *nd)
882883

883884
BUG_ON(!(nd->flags & LOOKUP_RCU));
884885

885-
if (unlikely(!legitimize_links(nd)))
886+
if (unlikely(nd->depth && !legitimize_links(nd)))
886887
goto out1;
887888
if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
888889
goto out;
@@ -917,7 +918,7 @@ static bool try_to_unlazy_next(struct nameidata *nd, struct dentry *dentry)
917918
int res;
918919
BUG_ON(!(nd->flags & LOOKUP_RCU));
919920

920-
if (unlikely(!legitimize_links(nd)))
921+
if (unlikely(nd->depth && !legitimize_links(nd)))
921922
goto out2;
922923
res = __legitimize_mnt(nd->path.mnt, nd->m_seq);
923924
if (unlikely(res)) {
@@ -2179,7 +2180,7 @@ static const char *walk_component(struct nameidata *nd, int flags)
21792180
* parent relationships.
21802181
*/
21812182
if (unlikely(nd->last_type != LAST_NORM)) {
2182-
if (!(flags & WALK_MORE) && nd->depth)
2183+
if (unlikely(nd->depth) && !(flags & WALK_MORE))
21832184
put_link(nd);
21842185
return handle_dots(nd, nd->last_type);
21852186
}
@@ -2191,7 +2192,7 @@ static const char *walk_component(struct nameidata *nd, int flags)
21912192
if (IS_ERR(dentry))
21922193
return ERR_CAST(dentry);
21932194
}
2194-
if (!(flags & WALK_MORE) && nd->depth)
2195+
if (unlikely(nd->depth) && !(flags & WALK_MORE))
21952196
put_link(nd);
21962197
return step_into(nd, flags, dentry);
21972198
}
@@ -2544,7 +2545,7 @@ static int link_path_walk(const char *name, struct nameidata *nd)
25442545
if (unlikely(!*name)) {
25452546
OK:
25462547
/* pathname or trailing symlink, done */
2547-
if (!depth) {
2548+
if (likely(!depth)) {
25482549
nd->dir_vfsuid = i_uid_into_vfsuid(idmap, nd->inode);
25492550
nd->dir_mode = nd->inode->i_mode;
25502551
nd->flags &= ~LOOKUP_PARENT;

0 commit comments

Comments
 (0)