Skip to content

Commit 2fd6b7f

Browse files
author
Nick Piggin
committed
fs: dcache scale subdirs
Protect d_subdirs and d_child with d_lock, except in filesystems that aren't using dcache_lock for these anyway (eg. using i_mutex). Note: if we change the locking rule in future so that ->d_child protection is provided only with ->d_parent->d_lock, it may allow us to reduce some locking. But it would be an exception to an otherwise regular locking scheme, so we'd have to see some good results. Probably not worthwhile. Signed-off-by: Nick Piggin <npiggin@kernel.dk>
1 parent da50295 commit 2fd6b7f

16 files changed

Lines changed: 339 additions & 160 deletions

File tree

‎drivers/staging/smbfs/cache.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ smb_invalidate_dircache_entries(struct dentry *parent)
6363
struct dentry *dentry;
6464

6565
spin_lock(&dcache_lock);
66+
spin_lock(&parent->d_lock);
6667
next = parent->d_subdirs.next;
6768
while (next != &parent->d_subdirs) {
6869
dentry = list_entry(next, struct dentry, d_u.d_child);
6970
dentry->d_fsdata = NULL;
7071
smb_age_dentry(server, dentry);
7172
next = next->next;
7273
}
74+
spin_unlock(&parent->d_lock);
7375
spin_unlock(&dcache_lock);
7476
}
7577

@@ -97,6 +99,7 @@ smb_dget_fpos(struct dentry *dentry, struct dentry *parent, unsigned long fpos)
9799

98100
/* If a pointer is invalid, we search the dentry. */
99101
spin_lock(&dcache_lock);
102+
spin_lock(&parent->d_lock);
100103
next = parent->d_subdirs.next;
101104
while (next != &parent->d_subdirs) {
102105
dent = list_entry(next, struct dentry, d_u.d_child);
@@ -111,6 +114,7 @@ smb_dget_fpos(struct dentry *dentry, struct dentry *parent, unsigned long fpos)
111114
}
112115
dent = NULL;
113116
out_unlock:
117+
spin_unlock(&parent->d_lock);
114118
spin_unlock(&dcache_lock);
115119
return dent;
116120
}

‎drivers/usb/core/inode.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,18 +344,20 @@ static int usbfs_empty (struct dentry *dentry)
344344
struct list_head *list;
345345

346346
spin_lock(&dcache_lock);
347-
347+
spin_lock(&dentry->d_lock);
348348
list_for_each(list, &dentry->d_subdirs) {
349349
struct dentry *de = list_entry(list, struct dentry, d_u.d_child);
350-
spin_lock(&de->d_lock);
350+
351+
spin_lock_nested(&de->d_lock, DENTRY_D_LOCK_NESTED);
351352
if (usbfs_positive(de)) {
352353
spin_unlock(&de->d_lock);
354+
spin_unlock(&dentry->d_lock);
353355
spin_unlock(&dcache_lock);
354356
return 0;
355357
}
356358
spin_unlock(&de->d_lock);
357359
}
358-
360+
spin_unlock(&dentry->d_lock);
359361
spin_unlock(&dcache_lock);
360362
return 1;
361363
}

‎fs/autofs4/autofs_i.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,17 @@ static inline int simple_positive(struct dentry *dentry)
254254
return dentry->d_inode && !d_unhashed(dentry);
255255
}
256256

257+
static inline void __autofs4_add_expiring(struct dentry *dentry)
258+
{
259+
struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
260+
struct autofs_info *ino = autofs4_dentry_ino(dentry);
261+
if (ino) {
262+
if (list_empty(&ino->expiring))
263+
list_add(&ino->expiring, &sbi->expiring_list);
264+
}
265+
return;
266+
}
267+
257268
static inline void autofs4_add_expiring(struct dentry *dentry)
258269
{
259270
struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);

‎fs/autofs4/expire.c‎

Lines changed: 60 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,64 @@ static int autofs4_mount_busy(struct vfsmount *mnt, struct dentry *dentry)
9191
}
9292

9393
/*
94-
* Calculate next entry in top down tree traversal.
95-
* From next_mnt in namespace.c - elegant.
94+
* Calculate and dget next entry in top down tree traversal.
9695
*/
97-
static struct dentry *next_dentry(struct dentry *p, struct dentry *root)
96+
static struct dentry *get_next_positive_dentry(struct dentry *prev,
97+
struct dentry *root)
9898
{
99-
struct list_head *next = p->d_subdirs.next;
99+
struct list_head *next;
100+
struct dentry *p, *ret;
101+
102+
if (prev == NULL)
103+
return dget(prev);
100104

105+
spin_lock(&dcache_lock);
106+
relock:
107+
p = prev;
108+
spin_lock(&p->d_lock);
109+
again:
110+
next = p->d_subdirs.next;
101111
if (next == &p->d_subdirs) {
102112
while (1) {
103-
if (p == root)
113+
struct dentry *parent;
114+
115+
if (p == root) {
116+
spin_unlock(&p->d_lock);
117+
spin_unlock(&dcache_lock);
118+
dput(prev);
104119
return NULL;
120+
}
121+
122+
parent = p->d_parent;
123+
if (!spin_trylock(&parent->d_lock)) {
124+
spin_unlock(&p->d_lock);
125+
cpu_relax();
126+
goto relock;
127+
}
128+
spin_unlock(&p->d_lock);
105129
next = p->d_u.d_child.next;
106-
if (next != &p->d_parent->d_subdirs)
130+
p = parent;
131+
if (next != &parent->d_subdirs)
107132
break;
108-
p = p->d_parent;
109133
}
110134
}
111-
return list_entry(next, struct dentry, d_u.d_child);
135+
ret = list_entry(next, struct dentry, d_u.d_child);
136+
137+
spin_lock_nested(&ret->d_lock, DENTRY_D_LOCK_NESTED);
138+
/* Negative dentry - try next */
139+
if (!simple_positive(ret)) {
140+
spin_unlock(&ret->d_lock);
141+
p = ret;
142+
goto again;
143+
}
144+
dget_dlock(ret);
145+
spin_unlock(&ret->d_lock);
146+
spin_unlock(&p->d_lock);
147+
spin_unlock(&dcache_lock);
148+
149+
dput(prev);
150+
151+
return ret;
112152
}
113153

114154
/*
@@ -158,22 +198,11 @@ static int autofs4_tree_busy(struct vfsmount *mnt,
158198
if (!simple_positive(top))
159199
return 1;
160200

161-
spin_lock(&dcache_lock);
162-
for (p = top; p; p = next_dentry(p, top)) {
163-
spin_lock(&p->d_lock);
164-
/* Negative dentry - give up */
165-
if (!simple_positive(p)) {
166-
spin_unlock(&p->d_lock);
167-
continue;
168-
}
169-
201+
p = NULL;
202+
while ((p = get_next_positive_dentry(p, top))) {
170203
DPRINTK("dentry %p %.*s",
171204
p, (int) p->d_name.len, p->d_name.name);
172205

173-
p = dget_dlock(p);
174-
spin_unlock(&p->d_lock);
175-
spin_unlock(&dcache_lock);
176-
177206
/*
178207
* Is someone visiting anywhere in the subtree ?
179208
* If there's no mount we need to check the usage
@@ -208,10 +237,7 @@ static int autofs4_tree_busy(struct vfsmount *mnt,
208237
return 1;
209238
}
210239
}
211-
dput(p);
212-
spin_lock(&dcache_lock);
213240
}
214-
spin_unlock(&dcache_lock);
215241

216242
/* Timeout of a tree mount is ultimately determined by its top dentry */
217243
if (!autofs4_can_expire(top, timeout, do_now))
@@ -230,36 +256,21 @@ static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
230256
DPRINTK("parent %p %.*s",
231257
parent, (int)parent->d_name.len, parent->d_name.name);
232258

233-
spin_lock(&dcache_lock);
234-
for (p = parent; p; p = next_dentry(p, parent)) {
235-
spin_lock(&p->d_lock);
236-
/* Negative dentry - give up */
237-
if (!simple_positive(p)) {
238-
spin_unlock(&p->d_lock);
239-
continue;
240-
}
241-
259+
p = NULL;
260+
while ((p = get_next_positive_dentry(p, parent))) {
242261
DPRINTK("dentry %p %.*s",
243262
p, (int) p->d_name.len, p->d_name.name);
244263

245-
p = dget_dlock(p);
246-
spin_unlock(&p->d_lock);
247-
spin_unlock(&dcache_lock);
248-
249264
if (d_mountpoint(p)) {
250265
/* Can we umount this guy */
251266
if (autofs4_mount_busy(mnt, p))
252-
goto cont;
267+
continue;
253268

254269
/* Can we expire this guy */
255270
if (autofs4_can_expire(p, timeout, do_now))
256271
return p;
257272
}
258-
cont:
259-
dput(p);
260-
spin_lock(&dcache_lock);
261273
}
262-
spin_unlock(&dcache_lock);
263274
return NULL;
264275
}
265276

@@ -310,8 +321,8 @@ struct dentry *autofs4_expire_indirect(struct super_block *sb,
310321
{
311322
unsigned long timeout;
312323
struct dentry *root = sb->s_root;
324+
struct dentry *dentry;
313325
struct dentry *expired = NULL;
314-
struct list_head *next;
315326
int do_now = how & AUTOFS_EXP_IMMEDIATE;
316327
int exp_leaves = how & AUTOFS_EXP_LEAVES;
317328
struct autofs_info *ino;
@@ -323,26 +334,8 @@ struct dentry *autofs4_expire_indirect(struct super_block *sb,
323334
now = jiffies;
324335
timeout = sbi->exp_timeout;
325336

326-
spin_lock(&dcache_lock);
327-
next = root->d_subdirs.next;
328-
329-
/* On exit from the loop expire is set to a dgot dentry
330-
* to expire or it's NULL */
331-
while ( next != &root->d_subdirs ) {
332-
struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
333-
334-
/* Negative dentry - give up */
335-
spin_lock(&dentry->d_lock);
336-
if (!simple_positive(dentry)) {
337-
next = next->next;
338-
spin_unlock(&dentry->d_lock);
339-
continue;
340-
}
341-
342-
dentry = dget_dlock(dentry);
343-
spin_unlock(&dentry->d_lock);
344-
spin_unlock(&dcache_lock);
345-
337+
dentry = NULL;
338+
while ((dentry = get_next_positive_dentry(dentry, root))) {
346339
spin_lock(&sbi->fs_lock);
347340
ino = autofs4_dentry_ino(dentry);
348341

@@ -405,11 +398,7 @@ struct dentry *autofs4_expire_indirect(struct super_block *sb,
405398
}
406399
next:
407400
spin_unlock(&sbi->fs_lock);
408-
dput(dentry);
409-
spin_lock(&dcache_lock);
410-
next = next->next;
411401
}
412-
spin_unlock(&dcache_lock);
413402
return NULL;
414403

415404
found:
@@ -420,7 +409,11 @@ struct dentry *autofs4_expire_indirect(struct super_block *sb,
420409
init_completion(&ino->expire_complete);
421410
spin_unlock(&sbi->fs_lock);
422411
spin_lock(&dcache_lock);
412+
spin_lock(&expired->d_parent->d_lock);
413+
spin_lock_nested(&expired->d_lock, DENTRY_D_LOCK_NESTED);
423414
list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
415+
spin_unlock(&expired->d_lock);
416+
spin_unlock(&expired->d_parent->d_lock);
424417
spin_unlock(&dcache_lock);
425418
return expired;
426419
}

‎fs/autofs4/root.c‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,13 @@ static int autofs4_dir_open(struct inode *inode, struct file *file)
143143
* it.
144144
*/
145145
spin_lock(&dcache_lock);
146+
spin_lock(&dentry->d_lock);
146147
if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
148+
spin_unlock(&dentry->d_lock);
147149
spin_unlock(&dcache_lock);
148150
return -ENOENT;
149151
}
152+
spin_unlock(&dentry->d_lock);
150153
spin_unlock(&dcache_lock);
151154

152155
out:
@@ -253,7 +256,9 @@ static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
253256
lookup_type = autofs4_need_mount(nd->flags);
254257
spin_lock(&sbi->fs_lock);
255258
spin_lock(&dcache_lock);
259+
spin_lock(&dentry->d_lock);
256260
if (!(lookup_type || ino->flags & AUTOFS_INF_PENDING)) {
261+
spin_unlock(&dentry->d_lock);
257262
spin_unlock(&dcache_lock);
258263
spin_unlock(&sbi->fs_lock);
259264
goto follow;
@@ -266,6 +271,7 @@ static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
266271
*/
267272
if (ino->flags & AUTOFS_INF_PENDING ||
268273
(!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs))) {
274+
spin_unlock(&dentry->d_lock);
269275
spin_unlock(&dcache_lock);
270276
spin_unlock(&sbi->fs_lock);
271277

@@ -275,6 +281,7 @@ static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
275281

276282
goto follow;
277283
}
284+
spin_unlock(&dentry->d_lock);
278285
spin_unlock(&dcache_lock);
279286
spin_unlock(&sbi->fs_lock);
280287
follow:
@@ -347,10 +354,12 @@ static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
347354

348355
/* Check for a non-mountpoint directory with no contents */
349356
spin_lock(&dcache_lock);
357+
spin_lock(&dentry->d_lock);
350358
if (S_ISDIR(dentry->d_inode->i_mode) &&
351359
!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
352360
DPRINTK("dentry=%p %.*s, emptydir",
353361
dentry, dentry->d_name.len, dentry->d_name.name);
362+
spin_unlock(&dentry->d_lock);
354363
spin_unlock(&dcache_lock);
355364

356365
/* The daemon never causes a mount to trigger */
@@ -367,6 +376,7 @@ static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
367376

368377
return status;
369378
}
379+
spin_unlock(&dentry->d_lock);
370380
spin_unlock(&dcache_lock);
371381

372382
return 1;
@@ -776,12 +786,16 @@ static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
776786
return -EACCES;
777787

778788
spin_lock(&dcache_lock);
789+
spin_lock(&sbi->lookup_lock);
790+
spin_lock(&dentry->d_lock);
779791
if (!list_empty(&dentry->d_subdirs)) {
792+
spin_unlock(&dentry->d_lock);
793+
spin_unlock(&sbi->lookup_lock);
780794
spin_unlock(&dcache_lock);
781795
return -ENOTEMPTY;
782796
}
783-
autofs4_add_expiring(dentry);
784-
spin_lock(&dentry->d_lock);
797+
__autofs4_add_expiring(dentry);
798+
spin_unlock(&sbi->lookup_lock);
785799
__d_drop(dentry);
786800
spin_unlock(&dentry->d_lock);
787801
spin_unlock(&dcache_lock);

0 commit comments

Comments
 (0)