Skip to content

Commit 8b19e34

Browse files
committed
vfs: make getcwd() get the root and pwd path under rcu
This allows us to skip all the crazy spinlocks and reference count updates, and instead use the fs sequence read-lock to get an atomic snapshot of the root and cwd information. We might want to make the rule that "prepend_path()" is always called with the RCU lock held, but the RCU lock nests fine and this is the minimal fix. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5762482 commit 8b19e34

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

‎fs/dcache.c‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3015,15 +3015,16 @@ char *dentry_path(struct dentry *dentry, char *buf, int buflen)
30153015
return ERR_PTR(-ENAMETOOLONG);
30163016
}
30173017

3018-
static inline void get_fs_root_and_pwd(struct fs_struct *fs, struct path *root,
3019-
struct path *pwd)
3018+
static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root,
3019+
struct path *pwd)
30203020
{
3021-
spin_lock(&fs->lock);
3022-
*root = fs->root;
3023-
path_get(root);
3024-
*pwd = fs->pwd;
3025-
path_get(pwd);
3026-
spin_unlock(&fs->lock);
3021+
unsigned seq;
3022+
3023+
do {
3024+
seq = read_seqcount_begin(&fs->seq);
3025+
*root = fs->root;
3026+
*pwd = fs->pwd;
3027+
} while (read_seqcount_retry(&fs->seq, seq));
30273028
}
30283029

30293030
/*
@@ -3053,7 +3054,8 @@ SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
30533054
if (!page)
30543055
return -ENOMEM;
30553056

3056-
get_fs_root_and_pwd(current->fs, &root, &pwd);
3057+
rcu_read_lock();
3058+
get_fs_root_and_pwd_rcu(current->fs, &root, &pwd);
30573059

30583060
error = -ENOENT;
30593061
br_read_lock(&vfsmount_lock);
@@ -3088,8 +3090,7 @@ SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
30883090
}
30893091

30903092
out:
3091-
path_put(&pwd);
3092-
path_put(&root);
3093+
rcu_read_unlock();
30933094
free_page((unsigned long) page);
30943095
return error;
30953096
}

0 commit comments

Comments
 (0)