Skip to content

Commit 873feea

Browse files
author
Nick Piggin
committed
fs: dcache per-inode inode alias locking
dcache_inode_lock can be replaced with per-inode locking. Use existing inode->i_lock for this. This is slightly non-trivial because we sometimes need to find the inode from the dentry, which requires d_inode to be stabilised (either with refcount or d_lock). Signed-off-by: Nick Piggin <npiggin@kernel.dk>
1 parent ceb5bdc commit 873feea

File tree

9 files changed

+67
-60
lines changed

9 files changed

+67
-60
lines changed

fs/9p/vfs_inode.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,11 @@ static struct dentry *v9fs_dentry_from_dir_inode(struct inode *inode)
277277
{
278278
struct dentry *dentry;
279279

280-
spin_lock(&dcache_inode_lock);
280+
spin_lock(&inode->i_lock);
281281
/* Directory should have only one entry. */
282282
BUG_ON(S_ISDIR(inode->i_mode) && !list_is_singular(&inode->i_dentry));
283283
dentry = list_entry(inode->i_dentry.next, struct dentry, d_alias);
284-
spin_unlock(&dcache_inode_lock);
284+
spin_unlock(&inode->i_lock);
285285
return dentry;
286286
}
287287

fs/affs/amigaffs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ affs_fix_dcache(struct dentry *dentry, u32 entry_ino)
128128
void *data = dentry->d_fsdata;
129129
struct list_head *head, *next;
130130

131-
spin_lock(&dcache_inode_lock);
131+
spin_lock(&inode->i_lock);
132132
head = &inode->i_dentry;
133133
next = head->next;
134134
while (next != head) {
@@ -139,7 +139,7 @@ affs_fix_dcache(struct dentry *dentry, u32 entry_ino)
139139
}
140140
next = next->next;
141141
}
142-
spin_unlock(&dcache_inode_lock);
142+
spin_unlock(&inode->i_lock);
143143
}
144144

145145

fs/cifs/inode.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -809,14 +809,14 @@ inode_has_hashed_dentries(struct inode *inode)
809809
{
810810
struct dentry *dentry;
811811

812-
spin_lock(&dcache_inode_lock);
812+
spin_lock(&inode->i_lock);
813813
list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
814814
if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
815-
spin_unlock(&dcache_inode_lock);
815+
spin_unlock(&inode->i_lock);
816816
return true;
817817
}
818818
}
819-
spin_unlock(&dcache_inode_lock);
819+
spin_unlock(&inode->i_lock);
820820
return false;
821821
}
822822

fs/dcache.c

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939

4040
/*
4141
* Usage:
42-
* dcache_inode_lock protects:
43-
* - i_dentry, d_alias, d_inode
42+
* dcache->d_inode->i_lock protects:
43+
* - i_dentry, d_alias, d_inode of aliases
4444
* dcache_hash_bucket lock protects:
4545
* - the dcache hash table
4646
* s_anon bl list spinlock protects:
@@ -58,7 +58,7 @@
5858
* - d_alias, d_inode
5959
*
6060
* Ordering:
61-
* dcache_inode_lock
61+
* dentry->d_inode->i_lock
6262
* dentry->d_lock
6363
* dcache_lru_lock
6464
* dcache_hash_bucket lock
@@ -78,12 +78,10 @@
7878
int sysctl_vfs_cache_pressure __read_mostly = 100;
7979
EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
8080

81-
__cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_inode_lock);
8281
static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
8382
__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
8483

8584
EXPORT_SYMBOL(rename_lock);
86-
EXPORT_SYMBOL(dcache_inode_lock);
8785

8886
static struct kmem_cache *dentry_cache __read_mostly;
8987

@@ -196,14 +194,14 @@ static inline void dentry_rcuwalk_barrier(struct dentry *dentry)
196194
*/
197195
static void dentry_iput(struct dentry * dentry)
198196
__releases(dentry->d_lock)
199-
__releases(dcache_inode_lock)
197+
__releases(dentry->d_inode->i_lock)
200198
{
201199
struct inode *inode = dentry->d_inode;
202200
if (inode) {
203201
dentry->d_inode = NULL;
204202
list_del_init(&dentry->d_alias);
205203
spin_unlock(&dentry->d_lock);
206-
spin_unlock(&dcache_inode_lock);
204+
spin_unlock(&inode->i_lock);
207205
if (!inode->i_nlink)
208206
fsnotify_inoderemove(inode);
209207
if (dentry->d_op && dentry->d_op->d_iput)
@@ -212,7 +210,6 @@ static void dentry_iput(struct dentry * dentry)
212210
iput(inode);
213211
} else {
214212
spin_unlock(&dentry->d_lock);
215-
spin_unlock(&dcache_inode_lock);
216213
}
217214
}
218215

@@ -222,14 +219,14 @@ static void dentry_iput(struct dentry * dentry)
222219
*/
223220
static void dentry_unlink_inode(struct dentry * dentry)
224221
__releases(dentry->d_lock)
225-
__releases(dcache_inode_lock)
222+
__releases(dentry->d_inode->i_lock)
226223
{
227224
struct inode *inode = dentry->d_inode;
228225
dentry->d_inode = NULL;
229226
list_del_init(&dentry->d_alias);
230227
dentry_rcuwalk_barrier(dentry);
231228
spin_unlock(&dentry->d_lock);
232-
spin_unlock(&dcache_inode_lock);
229+
spin_unlock(&inode->i_lock);
233230
if (!inode->i_nlink)
234231
fsnotify_inoderemove(inode);
235232
if (dentry->d_op && dentry->d_op->d_iput)
@@ -295,7 +292,7 @@ static void dentry_lru_move_tail(struct dentry *dentry)
295292
static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
296293
__releases(dentry->d_lock)
297294
__releases(parent->d_lock)
298-
__releases(dcache_inode_lock)
295+
__releases(dentry->d_inode->i_lock)
299296
{
300297
dentry->d_parent = NULL;
301298
list_del(&dentry->d_u.d_child);
@@ -370,9 +367,11 @@ EXPORT_SYMBOL(d_drop);
370367
static inline struct dentry *dentry_kill(struct dentry *dentry, int ref)
371368
__releases(dentry->d_lock)
372369
{
370+
struct inode *inode;
373371
struct dentry *parent;
374372

375-
if (!spin_trylock(&dcache_inode_lock)) {
373+
inode = dentry->d_inode;
374+
if (inode && !spin_trylock(&inode->i_lock)) {
376375
relock:
377376
spin_unlock(&dentry->d_lock);
378377
cpu_relax();
@@ -383,7 +382,8 @@ static inline struct dentry *dentry_kill(struct dentry *dentry, int ref)
383382
else
384383
parent = dentry->d_parent;
385384
if (parent && !spin_trylock(&parent->d_lock)) {
386-
spin_unlock(&dcache_inode_lock);
385+
if (inode)
386+
spin_unlock(&inode->i_lock);
387387
goto relock;
388388
}
389389

@@ -618,9 +618,9 @@ struct dentry *d_find_alias(struct inode *inode)
618618
struct dentry *de = NULL;
619619

620620
if (!list_empty(&inode->i_dentry)) {
621-
spin_lock(&dcache_inode_lock);
621+
spin_lock(&inode->i_lock);
622622
de = __d_find_alias(inode, 0);
623-
spin_unlock(&dcache_inode_lock);
623+
spin_unlock(&inode->i_lock);
624624
}
625625
return de;
626626
}
@@ -634,20 +634,20 @@ void d_prune_aliases(struct inode *inode)
634634
{
635635
struct dentry *dentry;
636636
restart:
637-
spin_lock(&dcache_inode_lock);
637+
spin_lock(&inode->i_lock);
638638
list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
639639
spin_lock(&dentry->d_lock);
640640
if (!dentry->d_count) {
641641
__dget_dlock(dentry);
642642
__d_drop(dentry);
643643
spin_unlock(&dentry->d_lock);
644-
spin_unlock(&dcache_inode_lock);
644+
spin_unlock(&inode->i_lock);
645645
dput(dentry);
646646
goto restart;
647647
}
648648
spin_unlock(&dentry->d_lock);
649649
}
650-
spin_unlock(&dcache_inode_lock);
650+
spin_unlock(&inode->i_lock);
651651
}
652652
EXPORT_SYMBOL(d_prune_aliases);
653653

@@ -1392,9 +1392,11 @@ static void __d_instantiate(struct dentry *dentry, struct inode *inode)
13921392
void d_instantiate(struct dentry *entry, struct inode * inode)
13931393
{
13941394
BUG_ON(!list_empty(&entry->d_alias));
1395-
spin_lock(&dcache_inode_lock);
1395+
if (inode)
1396+
spin_lock(&inode->i_lock);
13961397
__d_instantiate(entry, inode);
1397-
spin_unlock(&dcache_inode_lock);
1398+
if (inode)
1399+
spin_unlock(&inode->i_lock);
13981400
security_d_instantiate(entry, inode);
13991401
}
14001402
EXPORT_SYMBOL(d_instantiate);
@@ -1458,9 +1460,11 @@ struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
14581460

14591461
BUG_ON(!list_empty(&entry->d_alias));
14601462

1461-
spin_lock(&dcache_inode_lock);
1463+
if (inode)
1464+
spin_lock(&inode->i_lock);
14621465
result = __d_instantiate_unique(entry, inode);
1463-
spin_unlock(&dcache_inode_lock);
1466+
if (inode)
1467+
spin_unlock(&inode->i_lock);
14641468

14651469
if (!result) {
14661470
security_d_instantiate(entry, inode);
@@ -1542,10 +1546,10 @@ struct dentry *d_obtain_alias(struct inode *inode)
15421546
tmp->d_parent = tmp; /* make sure dput doesn't croak */
15431547

15441548

1545-
spin_lock(&dcache_inode_lock);
1549+
spin_lock(&inode->i_lock);
15461550
res = __d_find_alias(inode, 0);
15471551
if (res) {
1548-
spin_unlock(&dcache_inode_lock);
1552+
spin_unlock(&inode->i_lock);
15491553
dput(tmp);
15501554
goto out_iput;
15511555
}
@@ -1561,7 +1565,7 @@ struct dentry *d_obtain_alias(struct inode *inode)
15611565
hlist_bl_add_head(&tmp->d_hash, &tmp->d_sb->s_anon);
15621566
__bit_spin_unlock(0, (unsigned long *)&tmp->d_sb->s_anon.first);
15631567
spin_unlock(&tmp->d_lock);
1564-
spin_unlock(&dcache_inode_lock);
1568+
spin_unlock(&inode->i_lock);
15651569

15661570
return tmp;
15671571

@@ -1592,18 +1596,18 @@ struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
15921596
struct dentry *new = NULL;
15931597

15941598
if (inode && S_ISDIR(inode->i_mode)) {
1595-
spin_lock(&dcache_inode_lock);
1599+
spin_lock(&inode->i_lock);
15961600
new = __d_find_alias(inode, 1);
15971601
if (new) {
15981602
BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1599-
spin_unlock(&dcache_inode_lock);
1603+
spin_unlock(&inode->i_lock);
16001604
security_d_instantiate(new, inode);
16011605
d_move(new, dentry);
16021606
iput(inode);
16031607
} else {
1604-
/* already got dcache_inode_lock, so d_add() by hand */
1608+
/* already taking inode->i_lock, so d_add() by hand */
16051609
__d_instantiate(dentry, inode);
1606-
spin_unlock(&dcache_inode_lock);
1610+
spin_unlock(&inode->i_lock);
16071611
security_d_instantiate(dentry, inode);
16081612
d_rehash(dentry);
16091613
}
@@ -1676,10 +1680,10 @@ struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
16761680
* Negative dentry: instantiate it unless the inode is a directory and
16771681
* already has a dentry.
16781682
*/
1679-
spin_lock(&dcache_inode_lock);
1683+
spin_lock(&inode->i_lock);
16801684
if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
16811685
__d_instantiate(found, inode);
1682-
spin_unlock(&dcache_inode_lock);
1686+
spin_unlock(&inode->i_lock);
16831687
security_d_instantiate(found, inode);
16841688
return found;
16851689
}
@@ -1690,7 +1694,7 @@ struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
16901694
*/
16911695
new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
16921696
__dget(new);
1693-
spin_unlock(&dcache_inode_lock);
1697+
spin_unlock(&inode->i_lock);
16941698
security_d_instantiate(found, inode);
16951699
d_move(new, found);
16961700
iput(inode);
@@ -2004,15 +2008,17 @@ EXPORT_SYMBOL(d_validate);
20042008

20052009
void d_delete(struct dentry * dentry)
20062010
{
2011+
struct inode *inode;
20072012
int isdir = 0;
20082013
/*
20092014
* Are we the only user?
20102015
*/
20112016
again:
20122017
spin_lock(&dentry->d_lock);
2013-
isdir = S_ISDIR(dentry->d_inode->i_mode);
2018+
inode = dentry->d_inode;
2019+
isdir = S_ISDIR(inode->i_mode);
20142020
if (dentry->d_count == 1) {
2015-
if (!spin_trylock(&dcache_inode_lock)) {
2021+
if (inode && !spin_trylock(&inode->i_lock)) {
20162022
spin_unlock(&dentry->d_lock);
20172023
cpu_relax();
20182024
goto again;
@@ -2266,13 +2272,13 @@ struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
22662272
* This helper attempts to cope with remotely renamed directories
22672273
*
22682274
* It assumes that the caller is already holding
2269-
* dentry->d_parent->d_inode->i_mutex and the dcache_inode_lock
2275+
* dentry->d_parent->d_inode->i_mutex and the inode->i_lock
22702276
*
22712277
* Note: If ever the locking in lock_rename() changes, then please
22722278
* remember to update this too...
22732279
*/
2274-
static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
2275-
__releases(dcache_inode_lock)
2280+
static struct dentry *__d_unalias(struct inode *inode,
2281+
struct dentry *dentry, struct dentry *alias)
22762282
{
22772283
struct mutex *m1 = NULL, *m2 = NULL;
22782284
struct dentry *ret;
@@ -2298,7 +2304,7 @@ static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
22982304
d_move(alias, dentry);
22992305
ret = alias;
23002306
out_err:
2301-
spin_unlock(&dcache_inode_lock);
2307+
spin_unlock(&inode->i_lock);
23022308
if (m2)
23032309
mutex_unlock(m2);
23042310
if (m1)
@@ -2371,7 +2377,7 @@ struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
23712377
goto out_nolock;
23722378
}
23732379

2374-
spin_lock(&dcache_inode_lock);
2380+
spin_lock(&inode->i_lock);
23752381

23762382
if (S_ISDIR(inode->i_mode)) {
23772383
struct dentry *alias;
@@ -2388,7 +2394,7 @@ struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
23882394
goto found;
23892395
}
23902396
/* Nope, but we must(!) avoid directory aliasing */
2391-
actual = __d_unalias(dentry, alias);
2397+
actual = __d_unalias(inode, dentry, alias);
23922398
if (IS_ERR(actual))
23932399
dput(alias);
23942400
goto out_nolock;
@@ -2406,7 +2412,7 @@ struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
24062412
found:
24072413
_d_rehash(actual);
24082414
spin_unlock(&actual->d_lock);
2409-
spin_unlock(&dcache_inode_lock);
2415+
spin_unlock(&inode->i_lock);
24102416
out_nolock:
24112417
if (actual == dentry) {
24122418
security_d_instantiate(dentry, inode);

fs/exportfs/expfs.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,26 @@ find_acceptable_alias(struct dentry *result,
4343
void *context)
4444
{
4545
struct dentry *dentry, *toput = NULL;
46+
struct inode *inode;
4647

4748
if (acceptable(context, result))
4849
return result;
4950

50-
spin_lock(&dcache_inode_lock);
51-
list_for_each_entry(dentry, &result->d_inode->i_dentry, d_alias) {
51+
inode = result->d_inode;
52+
spin_lock(&inode->i_lock);
53+
list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
5254
dget(dentry);
53-
spin_unlock(&dcache_inode_lock);
55+
spin_unlock(&inode->i_lock);
5456
if (toput)
5557
dput(toput);
5658
if (dentry != result && acceptable(context, dentry)) {
5759
dput(result);
5860
return dentry;
5961
}
60-
spin_lock(&dcache_inode_lock);
62+
spin_lock(&inode->i_lock);
6163
toput = dentry;
6264
}
63-
spin_unlock(&dcache_inode_lock);
65+
spin_unlock(&inode->i_lock);
6466

6567
if (toput)
6668
dput(toput);

0 commit comments

Comments
 (0)