libfs: walk offset directories in offset order
Christian Brauner
maintainerPATCH 1/1 libfs: walk offset directories in offset order
offset_iterate_dir() walks d_children from the head and only uses the maple tree to map ctx->pos back to a dentry when a getdents64() call resumes. offset_dir_lookup() finds the entry at ctx->pos, or the one with the next lower offset if it is gone, and the list walk continues from there.
That only works if list order and offset order agree and quite often they don't. A rename over an existing name gives the moved dentry the offset of the name it replaced but __d_move() puts it at the head of d_children. RENAME_EXCHANGE does the same to both dentries. And once the cyclic offset allocator wraps new entries get low offsets at the head of the list.
So if the entry at ctx->pos is unlinked between two getdents64() calls we resume at the entry with the next lower offset and that one can sit anywhere in the list:
(1) If it sits further down the list everything in between is skipped. Create y, t, a, b and rename b over t. The list is now t(4) a(5) y(3). Stop the listing at t and unlink t. The next call resumes at y and a is never returned although nobody touched it.
(2) If there is no lower entry we restart at the head and return the whole directory again. Nickolai reported this for the simple case of unlinking the oldest entry while a listing is about to reach it. Ending the walk there instead turns this into (1): create a, b, c, rename c over a, stop the listing at the renamed entry, unlink it, and b is lost.
Instead of the list let's walk the maple tree itself in descending offset order. Then ctx->pos really means that everything above it has been emitted and resuming at the highest offset at or below it is right no matter where the dentry sits in d_children. Entries created or renamed to a new name after the listing started get an offset above ctx->pos and aren't returned, same as with the list walk which never went back to the head. So a readdir loop that renames or creates an entry for each one it reads still terminates.
The maple tree can't change under us as all modifications happen with the directory's i_rwsem held exclusive and offset_readdir() holds it shared.
--- fs/libfs.c | 56 ++++++++++++++++++++++---------------------------------- 1 file changed, 22 insertions(+), 34 deletions(-)
fs/libfs.c +22 -34
@@ -446,31 +446,11 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence)return vfs_setpos (file, offset,LONG_MAX ); } -static struct dentry *find_positive_dentry (struct dentry *parent, -struct dentry *dentry, -bool next) -{ -struct dentry *found =NULL ; - -spin_lock (&parent->d_lock); -if (next) - dentry =d_next_sibling (dentry); -else if (!dentry) - dentry =d_first_child (parent); -hlist_for_each_entry_from (dentry, d_sib) { -if (!simple_positive (dentry)) -continue ; -spin_lock_nested (&dentry->d_lock,DENTRY_D_LOCK_NESTED ); -if (simple_positive (dentry)) - found =dget_dlock (dentry); -spin_unlock (&dentry->d_lock); -if (likely (found)) -break ; - } -spin_unlock (&parent->d_lock); -return found; -} - +/* +* Find the entry with the highest offset at or below @offset. Entries are +* emitted in descending offset order, so this is the next entry to emit +* when resuming at @offset. +*/ static noinline_for_stack struct dentry *offset_dir_lookup (struct dentry *parent,loff_t offset) {
@@ -480,14 +460,19 @@ offset_dir_lookup(struct dentry *parent, loff_t offset)MA_STATE (mas, &octx->mt, offset, offset); -if (offset ==DIR_OFFSET_FIRST ) - found =find_positive_dentry (parent,NULL , false); -else { -rcu_read_lock (); - child =mas_find_rev (&mas,DIR_OFFSET_MIN ); - found =find_positive_dentry (parent, child, false); -rcu_read_unlock (); +if (offset <DIR_OFFSET_MIN ) +return NULL ; + +rcu_read_lock (); +while ((child =mas_find_rev (&mas,DIR_OFFSET_MIN ))) { +spin_lock (&child->d_lock); +if (simple_positive (child)) + found =dget_dlock (child); +spin_unlock (&child->d_lock); +if (found) +break ; } +rcu_read_unlock ();return found; }
@@ -504,7 +489,10 @@ static void offset_iterate_dir(struct file *file, struct dir_context *ctx)struct dentry *dir = file->f_path.dentry;struct dentry *dentry; - dentry =offset_dir_lookup (dir, ctx->pos); +if (ctx->pos ==DIR_OFFSET_FIRST ) + dentry =offset_dir_lookup (dir,DIR_OFFSET_MAX ); +else + dentry =offset_dir_lookup (dir, ctx->pos);if (!dentry) goto out_eod;while (true) {
@@ -514,7 +502,7 @@ static void offset_iterate_dir(struct file *file, struct dir_context *ctx)if (!offset_dir_emit (ctx, dentry))break ; - next =find_positive_dentry (dir, dentry, true); + next =offset_dir_lookup (dir, ctx->pos -1 );dput (dentry);if (!next)
---
base-commit: b44341e39d856e4cc6ad8ed94ebffd440f39846c
change-id: 20261030-work-libfs-offset_readdir-807d3b774260
Quoted 8 lines · Christian Brauner
Instead of the list let's walk the maple tree itself in descending offset order. Then
ctx->posreally means that everything above it has been emitted and resuming at the highest offset at or below it is right no matter where the dentry sits ind_children. Entries created or renamed to a new name after the listing started get an offset abovectx->posand aren't returned, same as with the list walk which never went back to the head. So a readdir loop that renames or creates an entry for each one it reads still terminates.
The descending walk is what the 2023 offset-ordered iteration was missing. That one walked ascending, so every new entry landed ahead of the cursor, which is how generic/736 broke it. Walking down from DIR_OFFSET_MAX with cyclic allocation puts new entries behind the cursor until the offset space wraps. And because the allocator is unchanged, a cookie an NFS client presents after an unbounded delay still resolves to a nearby survivor rather than to a recycled entry. So, OK.
Rename-over comes out better than the list walk too: the surviving name keeps the victim's offset and now also keeps the victim's position in the listing, where __d_move() drags it to the head of d_children today.
Before I want to add an Ack or Reviewed-by:
1. Each emitted entry now costs a fresh MA_STATE and a full tree descent in offset_dir_lookup(), where the list walk was one pointer chase. Can you time an ls on a tmpfs directory with a million entries, before and after? If it is noticeable, carrying the mas across the loop is the obvious fix, though dput() and dir_emit() interleaving makes that especially fiddly.
Quoted 3 lines
The maple tree can't change under us as all modifications happen with the directory's
i_rwsemheld exclusive andoffset_readdir()holds it shared.
2. That's true today, but Jon Flynn's RFC from yesterday proposes create and unlink with the parent locked shared. The code is fine either way: it runs under rcu_read_lock() and takes d_lock before dget_dlock(), so the walk does not depend on i_rwsem. Should the patch description say that the RCU path is what makes the walk safe, and i_rwsem is belt-and-suspenders?
3. Please run generic/736 on tmpfs directly and on a tmpfs export over NFS, and Nickolai's reproducer from the Link: above. I used a seek-after-closedir case as a regression test for the NFS resume case too, but my memory is hazy as to whether it actually could demonstrate proper function vs. a bug.
Signature · 1 line
Chuck Lever (Come to NFS bake-a-thon! https://nfsv4bat.org)
Christian Brauner
maintainerQuoted 6 lines · Chuck Lever
1. Each emitted entry now costs a fresh
MA_STATEand a full tree descent inoffset_dir_lookup(), where the list walk was one pointer chase. Can you time an ls on a tmpfs directory with a million entries, before and after? If it is noticeable, carrying the mas across the loop is the obvious fix, thoughdput()anddir_emit()interleaving makes that especially fiddly.
Obviously it's noticable:
base v1 getdents64 (32k buf) 146 ms 226-235 ms ls -f 0.26 s 0.34 s ls 0.49 s 0.57 s
The fix would be to carry the mas state. If you combine this with some moderate batching than this would recover nearly everything but I hate it.
We don't need the refs at all though afaict. Every entry in the tree is a d_make_persistent() dentry. Only unlink, rmdir and rename kill it and they all hold the parent's iwrsem and offset_readdir() grabs the read side.
So we can keep one ma_state across the loop and take rcu around mas_find_rev() and emit straight from the dentry. That kills all of the really performance heavy stuff, fixes the bug, and should be faster.
Quoted 6 lines
2. That's true today, but Jon Flynn's RFC from yesterday proposes create and unlink with the parent locked shared. The code is fine either way: it runs under
rcu_read_lock()and takesd_lockbeforedget_dlock(), so the walk does not depend oni_rwsem. Should the patch description say that the RCU path is what makes the walk safe, andi_rwsemis belt-and-suspenders?
I don't think so? The offset tree is set up with mt_init_flags(&octx->mt, MT_FLAGS_ALLOC_RANGE) and not MT_FLAGS_USE_RCU?
So outside of RCU mode the maple tree modifies live nodes in place. A lockless reader racing a writer can see a torn node. rcu only keeps the dentries alive.
i_rwsem has always been what makes the walk safe.
I really don't think we should care about an RFC when fixing an existing bug. Specifically when it's rather hacky. Even if we adopated an early hack like that shmem would have to change for this to work as it keeps taking the parent exclusive for create, unlink and rename.
So if tmpfs wanted that thing it would need MT_FLAGS_USE_RCU and then readdir needs refs again and then you get the perf regression back.
So pick your poison. Future changes aren't necessarly a convincing argument though.
Quoted 5 lines
3. Please run generic/736 on tmpfs directly and on a tmpfs export over NFS, and Nickolai's reproducer from the Link: above. I used a seek-after-closedir case as a regression test for the NFS resume case too, but my memory is hazy as to whether it actually could demonstrate proper function vs. a bug.
All checks out and I wrote new tests as well.
Quoted 10 lines · Christian Brauner
Quoted 6 lines · Chuck Lever
1. Each emitted entry now costs a fresh
MA_STATEand a full tree descent inoffset_dir_lookup(), where the list walk was one pointer chase. Can you time an ls on a tmpfs directory with a million entries, before and after? If it is noticeable, carrying the mas across the loop is the obvious fix, thoughdput()anddir_emit()interleaving makes that especially fiddly.Obviously it's noticable:
Quoted 8 lines
base v1 getdents64 (32k buf) 146 ms 226-235 ms ls -f 0.26 s 0.34 s ls 0.49 s 0.57 sThe fix would be to carry the mas state. If you combine this with some moderate batching than this would recover nearly everything but I hate it.
lol
Quoted 21 lines
Quoted 6 lines
2. That's true today, but Jon Flynn's RFC from yesterday proposes create and unlink with the parent locked shared. The code is fine either way: it runs under
rcu_read_lock()and takesd_lockbeforedget_dlock(), so the walk does not depend oni_rwsem. Should the patch description say that the RCU path is what makes the walk safe, andi_rwsemis belt-and-suspenders?I don't think so? The offset tree is set up with
mt_init_flags(&octx->mt, MT_FLAGS_ALLOC_RANGE)and notMT_FLAGS_USE_RCU?So outside of RCU mode the maple tree modifies live nodes in place. A lockless reader racing a writer can see a torn node. rcu only keeps the dentries alive.
i_rwsemhas always been what makes the walk safe.I really don't think we should care about an RFC when fixing an existing bug. Specifically when it's rather hacky. Even if we adopated an early hack like that shmem would have to change for this to work as it keeps taking the parent exclusive for create, unlink and rename.
Quoted 5 lines
So if tmpfs wanted that thing it would need
MT_FLAGS_USE_RCUand then readdir needs refs again and then you get the perf regression back.So pick your poison. Future changes aren't necessarly a convincing argument though.
Fair. So I think I was advocating for thorough documentation of the serialization requirements rather than coding against an RFC. That would enable Jon and Neil to adapt accordingly when the time comes to consider it again.
Signature · 1 line
Chuck Lever (Come to NFS bake-a-thon! https://nfsv4bat.org)
This looks good to me -- thank you for the better fix!
Quoted 43 lines · Christian Brauner
offset_iterate_dir()walksd_childrenfrom the head and only uses the maple tree to mapctx->posback to a dentry when agetdents64()call resumes.offset_dir_lookup()finds the entry atctx->pos, or the one with the next lower offset if it is gone, and the list walk continues from there.That only works if list order and offset order agree and quite often they don't. A rename over an existing name gives the moved dentry the offset of the name it replaced but
__d_move()puts it at the head ofd_children.RENAME_EXCHANGEdoes the same to both dentries. And once the cyclic offset allocator wraps new entries get low offsets at the head of the list.So if the entry at
ctx->posis unlinked between twogetdents64()calls we resume at the entry with the next lower offset and that one can sit anywhere in the list:(1) If it sits further down the list everything in between is skipped. Create y, t, a, b and rename b over t. The list is now t(4) a(5) y(3). Stop the listing at t and unlink t. The next call resumes at y and a is never returned although nobody touched it.
(2) If there is no lower entry we restart at the head and return the whole directory again. Nickolai reported this for the simple case of unlinking the oldest entry while a listing is about to reach it. Ending the walk there instead turns this into (1): create a, b, c, rename c over a, stop the listing at the renamed entry, unlink it, and b is lost.
Instead of the list let's walk the maple tree itself in descending offset order. Then
ctx->posreally means that everything above it has been emitted and resuming at the highest offset at or below it is right no matter where the dentry sits ind_children. Entries created or renamed to a new name after the listing started get an offset abovectx->posand aren't returned, same as with the list walk which never went back to the head. So a readdir loop that renames or creates an entry for each one it reads still terminates.The maple tree can't change under us as all modifications happen with the directory's
i_rwsemheld exclusive andoffset_readdir()holds it shared.
Quoted 5 lines
Fixes b9b588f22a0c ("libfs: Use d_children list to iterate simple_offset directories")Fixes f70681e9e606 ("libfs: Fix duplicate directory entry in offset_dir_lookup")Reported-by Nickolai Zeldovich <[email protected]>Signed-off-by Christian Brauner (Amutable) <[email protected]>
Quoted 95 lines
--- fs/libfs.c | 56 ++++++++++++++++++++++---------------------------------- 1 file changed, 22 insertions(+), 34 deletions(-)fs/libfs.c +22
-35@@ -446,31 +446,11 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence)return vfs_setpos (file, offset,LONG_MAX ); } -static struct dentry *find_positive_dentry (struct dentry *parent, -struct dentry *dentry, -bool next) -{ -struct dentry *found =NULL ; - -spin_lock (&parent->d_lock); -if (next) - dentry =d_next_sibling (dentry); -else if (!dentry) - dentry =d_first_child (parent); -hlist_for_each_entry_from (dentry, d_sib) { -if (!simple_positive (dentry)) -continue ; -spin_lock_nested (&dentry->d_lock,DENTRY_D_LOCK_NESTED ); -if (simple_positive (dentry)) - found =dget_dlock (dentry); -spin_unlock (&dentry->d_lock); -if (likely (found)) -break ; - } -spin_unlock (&parent->d_lock); -return found; -} - +/* +* Find the entry with the highest offset at or below @offset. Entries are +* emitted in descending offset order, so this is the next entry to emit +* when resuming at @offset. +*/ static noinline_for_stack struct dentry *offset_dir_lookup (struct dentry *parent,loff_t offset) {@@ -480,14 +460,19 @@ offset_dir_lookup(struct dentry *parent, loff_t offset)MA_STATE (mas, &octx->mt, offset, offset); -if (offset ==DIR_OFFSET_FIRST ) - found =find_positive_dentry (parent,NULL , false); -else { -rcu_read_lock (); - child =mas_find_rev (&mas,DIR_OFFSET_MIN ); - found =find_positive_dentry (parent, child, false); -rcu_read_unlock (); +if (offset <DIR_OFFSET_MIN ) +return NULL ; + +rcu_read_lock (); +while ((child =mas_find_rev (&mas,DIR_OFFSET_MIN ))) { +spin_lock (&child->d_lock); +if (simple_positive (child)) + found =dget_dlock (child); +spin_unlock (&child->d_lock); +if (found) +break ; } +rcu_read_unlock ();return found; }@@ -504,7 +489,10 @@ static void offset_iterate_dir(struct file *file, struct dir_context *ctx)struct dentry *dir = file->f_path.dentry;struct dentry *dentry; - dentry =offset_dir_lookup (dir, ctx->pos); +if (ctx->pos ==DIR_OFFSET_FIRST ) + dentry =offset_dir_lookup (dir,DIR_OFFSET_MAX ); +else + dentry =offset_dir_lookup (dir, ctx->pos);if (!dentry) goto out_eod;while (true) {@@ -514,7 +502,7 @@ static void offset_iterate_dir(struct file *file, struct dir_context *ctx)if (!offset_dir_emit (ctx, dentry))break ; - next =find_positive_dentry (dir, dentry, true); + next =offset_dir_lookup (dir, ctx->pos -1 );dput (dentry);if (!next) ---
Quoted 3 lines
base-commit:
b44341e39d856e4cc6ad8ed94ebffd440f39846c
change-id: 20261030-work-libfs-offset_readdir-807d3b774260
Nickolai.