[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID:
<170786028847.11135.14775608389430603086.stgit@91.116.238.104.host.secureserver.net>
Date: Tue, 13 Feb 2024 16:38:08 -0500
From: Chuck Lever <cel@...nel.org>
To: viro@...iv.linux.org.uk, brauner@...nel.org, jack@...e.cz,
hughd@...gle.com, akpm@...ux-foundation.org, Liam.Howlett@...cle.com,
oliver.sang@...el.com, feng.tang@...el.com
Cc: linux-kernel@...r.kernel.org, linux-fsdevel@...r.kernel.org,
maple-tree@...ts.infradead.org, linux-mm@...ck.org, lkp@...el.com
Subject: [PATCH RFC 7/7] libfs: Re-arrange locking in offset_iterate_dir()
From: Chuck Lever <chuck.lever@...cle.com>
Liam says that, unlike with xarray, once the RCU read lock is
released ma_state is not safe to re-use for the next mas_find() call.
But the RCU read lock has to be released on each loop iteration so
that dput() can be called safely.
Thus we are forced to walk the offset tree with fresh state for each
directory entry. mt_find() can do this for us, though it might be a
little less efficient than maintaining ma_state locally.
Since offset_iterate_dir() doesn't build ma_state locally any more,
there's no longer a strong need for offset_find_next(). Clean up by
rolling these two helpers together.
Signed-off-by: Chuck Lever <chuck.lever@...cle.com>
---
fs/libfs.c | 39 +++++++++++++++++----------------------
1 file changed, 17 insertions(+), 22 deletions(-)
diff --git a/fs/libfs.c b/fs/libfs.c
index f073e9aeb2bf..6e01fde1cf95 100644
--- a/fs/libfs.c
+++ b/fs/libfs.c
@@ -436,23 +436,6 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence)
return vfs_setpos(file, offset, MAX_LFS_FILESIZE);
}
-static struct dentry *offset_find_next(struct ma_state *mas)
-{
- struct dentry *child, *found = NULL;
-
- rcu_read_lock();
- child = mas_find(mas, ULONG_MAX);
- if (!child)
- goto out;
- spin_lock(&child->d_lock);
- if (simple_positive(child))
- found = dget_dlock(child);
- spin_unlock(&child->d_lock);
-out:
- rcu_read_unlock();
- return found;
-}
-
static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry)
{
unsigned long offset = dentry2offset(dentry);
@@ -465,13 +448,22 @@ static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry)
static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
{
struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode);
- MA_STATE(mas, &octx->mt, ctx->pos, ctx->pos);
- struct dentry *dentry;
+ struct dentry *dentry, *found;
+ unsigned long offset;
+ offset = ctx->pos;
while (true) {
- dentry = offset_find_next(&mas);
+ found = mt_find(&octx->mt, &offset, ULONG_MAX);
+ if (!found)
+ goto out_noent;
+
+ dentry = NULL;
+ spin_lock(&found->d_lock);
+ if (simple_positive(found))
+ dentry = dget_dlock(found);
+ spin_unlock(&found->d_lock);
if (!dentry)
- return ERR_PTR(-ENOENT);
+ goto out_noent;
if (!offset_dir_emit(ctx, dentry)) {
dput(dentry);
@@ -479,9 +471,12 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
}
dput(dentry);
- ctx->pos = mas.index + 1;
+ ctx->pos = offset;
}
return NULL;
+
+out_noent:
+ return ERR_PTR(-ENOENT);
}
/**
Powered by blists - more mailing lists