Skip to content

libfs: fix readdir of offset directories

Christian Brauner linux-fsdevel stable 2 versions – In review

Get series with b4
b4 am [email protected] change-id 20261030-work-libfs-offset_readdir-807d3b774260

Versions newest first

  1. v2 Latest3 patches 1 replyIn review
  2. v11 patch 4 repliesSuperseded

Changes in v2 from the cover letter

- Make i_rwsem the lock of the offset tree.
- Keep one ma_state across the walk and take no references (Chuck).
- Don't return the whole directory again after an lseek() past
DIR_OFFSET_EOD.
- Link to v1: https://patch.msgid.link/[email protected]

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3libfs: make i_rwsem the lock of the offset treefs/libfs.c include/linux/fs.h mm/shmem.c+31 -190 replies
2/3libfs: walk offset directories in offset orderfs/libfs.c+22 -630 replies
3/3libfs: end offset readdir at positions past DIR_OFFSET_EODfs/libfs.c+1 -10 replies

Reviews trailers per patch

None of the 3 patches has a Reviewed-by or Acked-by of their own; the series has one for all of them.

PatchChuck Lever
SeriesR-b
1/3
2/3
3/3

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH v2 0/3] libfs: fix readdir of offset directories

readdir on tmpfs walks d_children and only uses the offset tree to map the file position back to a dentry. d_children and offset order disagree after a rename over a name, an exchange or a wrap of the offset allocator. An unlink between two getdents64() calls then makes the walk skip entries or return the whole directory again. So walk the offset tree itself in descending offset order.

v1 did that with a full tree descent and a reference for every entry and was quite a bit slower. Now the walk keeps one ma_state for the whole call and takes no references at all. That relies on all writers holding i_rwsem exclusive while readdir holds it shared, and the maple tree only allows that if i_rwsem is the lock of the tree. The first patch makes it so, and lockdep now checks the writers. The last patch stops an lseek() past the end from returning the whole directory again.

1M entries on tmpfs, getdents64() with a 32k buffer: 146ms before, about 230ms with v1, 114ms now. Four processes listing the directory at the same time: 470ms per listing before, 113ms now.

Tested with Nickolai's reproducer, generic/736 and a seek-after-closedir test, on tmpfs and on tmpfs exported over NFSv3 and NFSv4.2, on KASAN and lockdep kernels. I also wrote a TLA+ model of the walk (cf. [1]).

[1]: https://github.com/brauner/linux-formal/tree/main/kernel/libfs/offset-readdir

---
Changes in v2:
- Make i_rwsem the lock of the offset tree.
- Keep one ma_state across the walk and take no references (Chuck).
- Don't return the whole directory again after an lseek() past
DIR_OFFSET_EOD.
- Link to v1: https://patch.msgid.link/[email protected]

---
Christian Brauner (3):
libfs: make i_rwsem the lock of the offset tree
libfs: walk offset directories in offset order
libfs: end offset readdir at positions past DIR_OFFSET_EOD

 fs/libfs.c         | 133 +++++++++++++++++++++--------------------------------
 include/linux/fs.h |   2 +-
 mm/shmem.c         |   2 +-
 3 files changed, 54 insertions(+), 83 deletions(-)

---
base-commit: b44341e39d856e4cc6ad8ed94ebffd440f39846c
change-id: 20261030-work-libfs-offset_readdir-807d3b774260