Skip to content

Commit c28cc36

Browse files
author
Nick Piggin
committed
fs: fs_struct use seqlock
Use a seqlock in the fs_struct to enable us to take an atomic copy of the complete cwd and root paths. Use this in the RCU lookup path to avoid a thread-shared spinlock in RCU lookup operations. Multi-threaded apps may now perform path lookups with scalability matching multi-process apps. Operations such as stat(2) become very scalable for multi-threaded workload. Signed-off-by: Nick Piggin <npiggin@kernel.dk>
1 parent 31e6b01 commit c28cc36

3 files changed

Lines changed: 34 additions & 13 deletions

File tree

‎fs/fs_struct.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ void set_fs_root(struct fs_struct *fs, struct path *path)
1414
struct path old_root;
1515

1616
spin_lock(&fs->lock);
17+
write_seqcount_begin(&fs->seq);
1718
old_root = fs->root;
1819
fs->root = *path;
1920
path_get(path);
21+
write_seqcount_end(&fs->seq);
2022
spin_unlock(&fs->lock);
2123
if (old_root.dentry)
2224
path_put(&old_root);
@@ -31,9 +33,11 @@ void set_fs_pwd(struct fs_struct *fs, struct path *path)
3133
struct path old_pwd;
3234

3335
spin_lock(&fs->lock);
36+
write_seqcount_begin(&fs->seq);
3437
old_pwd = fs->pwd;
3538
fs->pwd = *path;
3639
path_get(path);
40+
write_seqcount_end(&fs->seq);
3741
spin_unlock(&fs->lock);
3842

3943
if (old_pwd.dentry)
@@ -52,6 +56,7 @@ void chroot_fs_refs(struct path *old_root, struct path *new_root)
5256
fs = p->fs;
5357
if (fs) {
5458
spin_lock(&fs->lock);
59+
write_seqcount_begin(&fs->seq);
5560
if (fs->root.dentry == old_root->dentry
5661
&& fs->root.mnt == old_root->mnt) {
5762
path_get(new_root);
@@ -64,6 +69,7 @@ void chroot_fs_refs(struct path *old_root, struct path *new_root)
6469
fs->pwd = *new_root;
6570
count++;
6671
}
72+
write_seqcount_end(&fs->seq);
6773
spin_unlock(&fs->lock);
6874
}
6975
task_unlock(p);
@@ -88,8 +94,10 @@ void exit_fs(struct task_struct *tsk)
8894
int kill;
8995
task_lock(tsk);
9096
spin_lock(&fs->lock);
97+
write_seqcount_begin(&fs->seq);
9198
tsk->fs = NULL;
9299
kill = !--fs->users;
100+
write_seqcount_end(&fs->seq);
93101
spin_unlock(&fs->lock);
94102
task_unlock(tsk);
95103
if (kill)
@@ -105,6 +113,7 @@ struct fs_struct *copy_fs_struct(struct fs_struct *old)
105113
fs->users = 1;
106114
fs->in_exec = 0;
107115
spin_lock_init(&fs->lock);
116+
seqcount_init(&fs->seq);
108117
fs->umask = old->umask;
109118
get_fs_root_and_pwd(old, &fs->root, &fs->pwd);
110119
}
@@ -144,6 +153,7 @@ EXPORT_SYMBOL(current_umask);
144153
struct fs_struct init_fs = {
145154
.users = 1,
146155
.lock = __SPIN_LOCK_UNLOCKED(init_fs.lock),
156+
.seq = SEQCNT_ZERO,
147157
.umask = 0022,
148158
};
149159

‎fs/namei.c‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -684,9 +684,12 @@ static __always_inline void set_root_rcu(struct nameidata *nd)
684684
{
685685
if (!nd->root.mnt) {
686686
struct fs_struct *fs = current->fs;
687-
spin_lock(&fs->lock);
688-
nd->root = fs->root;
689-
spin_unlock(&fs->lock);
687+
unsigned seq;
688+
689+
do {
690+
seq = read_seqcount_begin(&fs->seq);
691+
nd->root = fs->root;
692+
} while (read_seqcount_retry(&fs->seq, seq));
690693
}
691694
}
692695

@@ -1369,26 +1372,31 @@ static int path_init_rcu(int dfd, const char *name, unsigned int flags, struct n
13691372

13701373
if (*name=='/') {
13711374
struct fs_struct *fs = current->fs;
1375+
unsigned seq;
13721376

13731377
br_read_lock(vfsmount_lock);
13741378
rcu_read_lock();
13751379

1376-
spin_lock(&fs->lock);
1377-
nd->root = fs->root;
1378-
nd->path = nd->root;
1379-
nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1380-
spin_unlock(&fs->lock);
1380+
do {
1381+
seq = read_seqcount_begin(&fs->seq);
1382+
nd->root = fs->root;
1383+
nd->path = nd->root;
1384+
nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1385+
} while (read_seqcount_retry(&fs->seq, seq));
13811386

13821387
} else if (dfd == AT_FDCWD) {
13831388
struct fs_struct *fs = current->fs;
1389+
unsigned seq;
13841390

13851391
br_read_lock(vfsmount_lock);
13861392
rcu_read_lock();
13871393

1388-
spin_lock(&fs->lock);
1389-
nd->path = fs->pwd;
1390-
nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1391-
spin_unlock(&fs->lock);
1394+
do {
1395+
seq = read_seqcount_begin(&fs->seq);
1396+
nd->path = fs->pwd;
1397+
nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1398+
} while (read_seqcount_retry(&fs->seq, seq));
1399+
13921400
} else {
13931401
struct dentry *dentry;
13941402

@@ -1411,7 +1419,7 @@ static int path_init_rcu(int dfd, const char *name, unsigned int flags, struct n
14111419
if (fput_needed)
14121420
nd->file = file;
14131421

1414-
nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1422+
nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
14151423
br_read_lock(vfsmount_lock);
14161424
rcu_read_lock();
14171425
}

‎include/linux/fs_struct.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
#define _LINUX_FS_STRUCT_H
33

44
#include <linux/path.h>
5+
#include <linux/spinlock.h>
6+
#include <linux/seqlock.h>
57

68
struct fs_struct {
79
int users;
810
spinlock_t lock;
11+
seqcount_t seq;
912
int umask;
1013
int in_exec;
1114
struct path root, pwd;

0 commit comments

Comments
 (0)