Skip to content

Commit f03c659

Browse files
author
Al Viro
committed
sanitize vfsmount refcounting changes
Instead of splitting refcount between (per-cpu) mnt_count and (SMP-only) mnt_longrefs, make all references contribute to mnt_count again and keep track of how many are longterm ones. Accounting rules for longterm count: * 1 for each fs_struct.root.mnt * 1 for each fs_struct.pwd.mnt * 1 for having non-NULL ->mnt_ns * decrement to 0 happens only under vfsmount lock exclusive That allows nice common case for mntput() - since we can't drop the final reference until after mnt_longterm has reached 0 due to the rules above, mntput() can grab vfsmount lock shared and check mnt_longterm. If it turns out to be non-zero (which is the common case), we know that this is not the final mntput() and can just blindly decrement percpu mnt_count. Otherwise we grab vfsmount lock exclusive and do usual decrement-and-check of percpu mnt_count. For fs_struct.c we have mnt_make_longterm() and mnt_make_shortterm(); namespace.c uses the latter in places where we don't already hold vfsmount lock exclusive and opencodes a few remaining spots where we need to manipulate mnt_longterm. Note that we mostly revert the code outside of fs/namespace.c back to what we used to have; in particular, normal code doesn't need to care about two kinds of references, etc. And we get to keep the optimization Nick's variant had bought us... Signed-off-by: Al Viro <[email protected]>
1 parent 7b8a53f commit f03c659

File tree

10 files changed

+75
-117
lines changed

10 files changed

+75
-117
lines changed

drivers/mtd/mtdchar.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,7 @@ static int __init init_mtdchar(void)
12011201
static void __exit cleanup_mtdchar(void)
12021202
{
12031203
unregister_mtd_user(&mtdchar_notifier);
1204-
mntput_long(mtd_inode_mnt);
1204+
mntput(mtd_inode_mnt);
12051205
unregister_filesystem(&mtd_inodefs_type);
12061206
__unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
12071207
}

fs/anon_inodes.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ static int __init anon_inode_init(void)
233233
return 0;
234234

235235
err_mntput:
236-
mntput_long(anon_inode_mnt);
236+
mntput(anon_inode_mnt);
237237
err_unregister_filesystem:
238238
unregister_filesystem(&anon_inode_fs_type);
239239
err_exit:

fs/fs_struct.c

+24-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44
#include <linux/path.h>
55
#include <linux/slab.h>
66
#include <linux/fs_struct.h>
7+
#include "internal.h"
8+
9+
static inline void path_get_longterm(struct path *path)
10+
{
11+
path_get(path);
12+
mnt_make_longterm(path->mnt);
13+
}
14+
15+
static inline void path_put_longterm(struct path *path)
16+
{
17+
mnt_make_shortterm(path->mnt);
18+
path_put(path);
19+
}
720

821
/*
922
* Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
@@ -17,11 +30,11 @@ void set_fs_root(struct fs_struct *fs, struct path *path)
1730
write_seqcount_begin(&fs->seq);
1831
old_root = fs->root;
1932
fs->root = *path;
20-
path_get_long(path);
33+
path_get_longterm(path);
2134
write_seqcount_end(&fs->seq);
2235
spin_unlock(&fs->lock);
2336
if (old_root.dentry)
24-
path_put_long(&old_root);
37+
path_put_longterm(&old_root);
2538
}
2639

2740
/*
@@ -36,12 +49,12 @@ void set_fs_pwd(struct fs_struct *fs, struct path *path)
3649
write_seqcount_begin(&fs->seq);
3750
old_pwd = fs->pwd;
3851
fs->pwd = *path;
39-
path_get_long(path);
52+
path_get_longterm(path);
4053
write_seqcount_end(&fs->seq);
4154
spin_unlock(&fs->lock);
4255

4356
if (old_pwd.dentry)
44-
path_put_long(&old_pwd);
57+
path_put_longterm(&old_pwd);
4558
}
4659

4760
void chroot_fs_refs(struct path *old_root, struct path *new_root)
@@ -59,13 +72,13 @@ void chroot_fs_refs(struct path *old_root, struct path *new_root)
5972
write_seqcount_begin(&fs->seq);
6073
if (fs->root.dentry == old_root->dentry
6174
&& fs->root.mnt == old_root->mnt) {
62-
path_get_long(new_root);
75+
path_get_longterm(new_root);
6376
fs->root = *new_root;
6477
count++;
6578
}
6679
if (fs->pwd.dentry == old_root->dentry
6780
&& fs->pwd.mnt == old_root->mnt) {
68-
path_get_long(new_root);
81+
path_get_longterm(new_root);
6982
fs->pwd = *new_root;
7083
count++;
7184
}
@@ -76,13 +89,13 @@ void chroot_fs_refs(struct path *old_root, struct path *new_root)
7689
} while_each_thread(g, p);
7790
read_unlock(&tasklist_lock);
7891
while (count--)
79-
path_put_long(old_root);
92+
path_put_longterm(old_root);
8093
}
8194

8295
void free_fs_struct(struct fs_struct *fs)
8396
{
84-
path_put_long(&fs->root);
85-
path_put_long(&fs->pwd);
97+
path_put_longterm(&fs->root);
98+
path_put_longterm(&fs->pwd);
8699
kmem_cache_free(fs_cachep, fs);
87100
}
88101

@@ -118,9 +131,9 @@ struct fs_struct *copy_fs_struct(struct fs_struct *old)
118131

119132
spin_lock(&old->lock);
120133
fs->root = old->root;
121-
path_get_long(&fs->root);
134+
path_get_longterm(&fs->root);
122135
fs->pwd = old->pwd;
123-
path_get_long(&fs->pwd);
136+
path_get_longterm(&fs->pwd);
124137
spin_unlock(&old->lock);
125138
}
126139
return fs;

fs/internal.h

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ extern struct vfsmount *copy_tree(struct vfsmount *, struct dentry *, int);
7373
extern int do_add_mount(struct vfsmount *, struct path *, int);
7474
extern void mnt_clear_expiry(struct vfsmount *);
7575

76+
extern void mnt_make_longterm(struct vfsmount *);
77+
extern void mnt_make_shortterm(struct vfsmount *);
78+
7679
extern void __init mnt_init(void);
7780

7881
DECLARE_BRLOCK(vfsmount_lock);

fs/namei.c

-24
Original file line numberDiff line numberDiff line change
@@ -367,18 +367,6 @@ void path_get(struct path *path)
367367
}
368368
EXPORT_SYMBOL(path_get);
369369

370-
/**
371-
* path_get_long - get a long reference to a path
372-
* @path: path to get the reference to
373-
*
374-
* Given a path increment the reference count to the dentry and the vfsmount.
375-
*/
376-
void path_get_long(struct path *path)
377-
{
378-
mntget_long(path->mnt);
379-
dget(path->dentry);
380-
}
381-
382370
/**
383371
* path_put - put a reference to a path
384372
* @path: path to put the reference to
@@ -392,18 +380,6 @@ void path_put(struct path *path)
392380
}
393381
EXPORT_SYMBOL(path_put);
394382

395-
/**
396-
* path_put_long - put a long reference to a path
397-
* @path: path to put the reference to
398-
*
399-
* Given a path decrement the reference count to the dentry and the vfsmount.
400-
*/
401-
void path_put_long(struct path *path)
402-
{
403-
dput(path->dentry);
404-
mntput_long(path->mnt);
405-
}
406-
407383
/**
408384
* nameidata_drop_rcu - drop this nameidata out of rcu-walk
409385
* @nd: nameidata pathwalk data to drop

fs/namespace.c

+43-73
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ static inline void mnt_dec_count(struct vfsmount *mnt)
183183
unsigned int mnt_get_count(struct vfsmount *mnt)
184184
{
185185
#ifdef CONFIG_SMP
186-
unsigned int count = atomic_read(&mnt->mnt_longrefs);
186+
unsigned int count = 0;
187187
int cpu;
188188

189189
for_each_possible_cpu(cpu) {
@@ -217,7 +217,7 @@ struct vfsmount *alloc_vfsmnt(const char *name)
217217
if (!mnt->mnt_pcp)
218218
goto out_free_devname;
219219

220-
atomic_set(&mnt->mnt_longrefs, 1);
220+
this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
221221
#else
222222
mnt->mnt_count = 1;
223223
mnt->mnt_writers = 0;
@@ -624,8 +624,11 @@ static void commit_tree(struct vfsmount *mnt)
624624
BUG_ON(parent == mnt);
625625

626626
list_add_tail(&head, &mnt->mnt_list);
627-
list_for_each_entry(m, &head, mnt_list)
627+
list_for_each_entry(m, &head, mnt_list) {
628628
m->mnt_ns = n;
629+
atomic_inc(&m->mnt_longterm);
630+
}
631+
629632
list_splice(&head, n->list.prev);
630633

631634
list_add_tail(&mnt->mnt_hash, mount_hashtable +
@@ -734,51 +737,30 @@ static inline void mntfree(struct vfsmount *mnt)
734737
deactivate_super(sb);
735738
}
736739

737-
#ifdef CONFIG_SMP
738-
static inline void __mntput(struct vfsmount *mnt, int longrefs)
740+
static void mntput_no_expire(struct vfsmount *mnt)
739741
{
740-
if (!longrefs) {
741742
put_again:
742-
br_read_lock(vfsmount_lock);
743-
if (likely(atomic_read(&mnt->mnt_longrefs))) {
744-
mnt_dec_count(mnt);
745-
br_read_unlock(vfsmount_lock);
746-
return;
747-
}
743+
#ifdef CONFIG_SMP
744+
br_read_lock(vfsmount_lock);
745+
if (likely(atomic_read(&mnt->mnt_longterm))) {
746+
mnt_dec_count(mnt);
748747
br_read_unlock(vfsmount_lock);
749-
} else {
750-
BUG_ON(!atomic_read(&mnt->mnt_longrefs));
751-
if (atomic_add_unless(&mnt->mnt_longrefs, -1, 1))
752-
return;
748+
return;
753749
}
750+
br_read_unlock(vfsmount_lock);
754751

755752
br_write_lock(vfsmount_lock);
756-
if (!longrefs)
757-
mnt_dec_count(mnt);
758-
else
759-
atomic_dec(&mnt->mnt_longrefs);
753+
mnt_dec_count(mnt);
760754
if (mnt_get_count(mnt)) {
761755
br_write_unlock(vfsmount_lock);
762756
return;
763757
}
764-
if (unlikely(mnt->mnt_pinned)) {
765-
mnt_add_count(mnt, mnt->mnt_pinned + 1);
766-
mnt->mnt_pinned = 0;
767-
br_write_unlock(vfsmount_lock);
768-
acct_auto_close_mnt(mnt);
769-
goto put_again;
770-
}
771-
br_write_unlock(vfsmount_lock);
772-
mntfree(mnt);
773-
}
774758
#else
775-
static inline void __mntput(struct vfsmount *mnt, int longrefs)
776-
{
777-
put_again:
778759
mnt_dec_count(mnt);
779760
if (likely(mnt_get_count(mnt)))
780761
return;
781762
br_write_lock(vfsmount_lock);
763+
#endif
782764
if (unlikely(mnt->mnt_pinned)) {
783765
mnt_add_count(mnt, mnt->mnt_pinned + 1);
784766
mnt->mnt_pinned = 0;
@@ -789,20 +771,14 @@ static inline void __mntput(struct vfsmount *mnt, int longrefs)
789771
br_write_unlock(vfsmount_lock);
790772
mntfree(mnt);
791773
}
792-
#endif
793-
794-
static void mntput_no_expire(struct vfsmount *mnt)
795-
{
796-
__mntput(mnt, 0);
797-
}
798774

799775
void mntput(struct vfsmount *mnt)
800776
{
801777
if (mnt) {
802778
/* avoid cacheline pingpong, hope gcc doesn't get "smart" */
803779
if (unlikely(mnt->mnt_expiry_mark))
804780
mnt->mnt_expiry_mark = 0;
805-
__mntput(mnt, 0);
781+
mntput_no_expire(mnt);
806782
}
807783
}
808784
EXPORT_SYMBOL(mntput);
@@ -815,33 +791,6 @@ struct vfsmount *mntget(struct vfsmount *mnt)
815791
}
816792
EXPORT_SYMBOL(mntget);
817793

818-
void mntput_long(struct vfsmount *mnt)
819-
{
820-
#ifdef CONFIG_SMP
821-
if (mnt) {
822-
/* avoid cacheline pingpong, hope gcc doesn't get "smart" */
823-
if (unlikely(mnt->mnt_expiry_mark))
824-
mnt->mnt_expiry_mark = 0;
825-
__mntput(mnt, 1);
826-
}
827-
#else
828-
mntput(mnt);
829-
#endif
830-
}
831-
EXPORT_SYMBOL(mntput_long);
832-
833-
struct vfsmount *mntget_long(struct vfsmount *mnt)
834-
{
835-
#ifdef CONFIG_SMP
836-
if (mnt)
837-
atomic_inc(&mnt->mnt_longrefs);
838-
return mnt;
839-
#else
840-
return mntget(mnt);
841-
#endif
842-
}
843-
EXPORT_SYMBOL(mntget_long);
844-
845794
void mnt_pin(struct vfsmount *mnt)
846795
{
847796
br_write_lock(vfsmount_lock);
@@ -1216,7 +1165,7 @@ void release_mounts(struct list_head *head)
12161165
dput(dentry);
12171166
mntput(m);
12181167
}
1219-
mntput_long(mnt);
1168+
mntput(mnt);
12201169
}
12211170
}
12221171

@@ -1240,6 +1189,7 @@ void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
12401189
list_del_init(&p->mnt_list);
12411190
__touch_mnt_namespace(p->mnt_ns);
12421191
p->mnt_ns = NULL;
1192+
atomic_dec(&p->mnt_longterm);
12431193
list_del_init(&p->mnt_child);
12441194
if (p->mnt_parent != p) {
12451195
p->mnt_parent->mnt_ghosts++;
@@ -1969,7 +1919,7 @@ int do_add_mount(struct vfsmount *newmnt, struct path *path, int mnt_flags)
19691919

19701920
unlock:
19711921
up_write(&namespace_sem);
1972-
mntput_long(newmnt);
1922+
mntput(newmnt);
19731923
return err;
19741924
}
19751925

@@ -2291,6 +2241,20 @@ static struct mnt_namespace *alloc_mnt_ns(void)
22912241
return new_ns;
22922242
}
22932243

2244+
void mnt_make_longterm(struct vfsmount *mnt)
2245+
{
2246+
atomic_inc(&mnt->mnt_longterm);
2247+
}
2248+
2249+
void mnt_make_shortterm(struct vfsmount *mnt)
2250+
{
2251+
if (atomic_add_unless(&mnt->mnt_longterm, -1, 1))
2252+
return;
2253+
br_write_lock(vfsmount_lock);
2254+
atomic_dec(&mnt->mnt_longterm);
2255+
br_write_unlock(vfsmount_lock);
2256+
}
2257+
22942258
/*
22952259
* Allocate a new namespace structure and populate it with contents
22962260
* copied from the namespace of the passed in task structure.
@@ -2328,14 +2292,19 @@ static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
23282292
q = new_ns->root;
23292293
while (p) {
23302294
q->mnt_ns = new_ns;
2295+
atomic_inc(&q->mnt_longterm);
23312296
if (fs) {
23322297
if (p == fs->root.mnt) {
2298+
fs->root.mnt = mntget(q);
2299+
atomic_inc(&q->mnt_longterm);
2300+
mnt_make_shortterm(p);
23332301
rootmnt = p;
2334-
fs->root.mnt = mntget_long(q);
23352302
}
23362303
if (p == fs->pwd.mnt) {
2304+
fs->pwd.mnt = mntget(q);
2305+
atomic_inc(&q->mnt_longterm);
2306+
mnt_make_shortterm(p);
23372307
pwdmnt = p;
2338-
fs->pwd.mnt = mntget_long(q);
23392308
}
23402309
}
23412310
p = next_mnt(p, mnt_ns->root);
@@ -2344,9 +2313,9 @@ static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
23442313
up_write(&namespace_sem);
23452314

23462315
if (rootmnt)
2347-
mntput_long(rootmnt);
2316+
mntput(rootmnt);
23482317
if (pwdmnt)
2349-
mntput_long(pwdmnt);
2318+
mntput(pwdmnt);
23502319

23512320
return new_ns;
23522321
}
@@ -2379,6 +2348,7 @@ struct mnt_namespace *create_mnt_ns(struct vfsmount *mnt)
23792348
new_ns = alloc_mnt_ns();
23802349
if (!IS_ERR(new_ns)) {
23812350
mnt->mnt_ns = new_ns;
2351+
atomic_inc(&mnt->mnt_longterm);
23822352
new_ns->root = mnt;
23832353
list_add(&new_ns->list, &new_ns->root->mnt_list);
23842354
}

0 commit comments

Comments
 (0)