fs: improve codegen in link_path_walk()
authorMateusz Guzik <mjguzik@gmail.com>
Sat, 12 Apr 2025 11:09:35 +0000 (13:09 +0200)
committerChristian Brauner <brauner@kernel.org>
Mon, 21 Apr 2025 08:27:58 +0000 (10:27 +0200)
commit8564124c36d6bf97199125dd542710c2814b4017
treea6394d3b7f8aa1b720c7fc668e296a070b2233d2
parentef181fa11d9e09d3741525937d7bab4c5ab91ef7
fs: improve codegen in link_path_walk()

Looking at the asm produced by gcc 13.3 for x86-64:
1. may_lookup() usage was not optimized for succeeding, despite the
   routine being inlined and rightfully starting with likely(!err)
2. the compiler assumed the path will have an indefinite amount of
   slashes to skip, after which the result will be an empty name

As such:
1. predict may_lookup() succeeding
2. check for one slash, no explicit predicts. do roll forward with
   skipping more slashes while predicting there is only one
3. predict the path to find was not a mere slash

This also has a side effect of shrinking the file:
add/remove: 1/1 grow/shrink: 0/3 up/down: 934/-1012 (-78)
Function                                     old     new   delta
link_path_walk                                 -     934    +934
path_parentat                                138     112     -26
path_openat                                 4864    4823     -41
path_lookupat                                418     374     -44
link_path_walk.part.constprop                901       -    -901
Total: Before=46639, After=46561, chg -0.17%

Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
Link: https://lore.kernel.org/20250412110935.2267703-1-mjguzik@gmail.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/namei.c