Skip to content

Commit ceb5bdc

Browse files
author
Nick Piggin
committed
fs: dcache per-bucket dcache hash locking
We can turn the dcache hash locking from a global dcache_hash_lock into per-bucket locking. Signed-off-by: Nick Piggin <npiggin@kernel.dk>
1 parent 626d607 commit ceb5bdc

File tree

4 files changed

+89
-53
lines changed

4 files changed

+89
-53
lines changed

fs/dcache.c

Lines changed: 83 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,18 @@
3333
#include <linux/bootmem.h>
3434
#include <linux/fs_struct.h>
3535
#include <linux/hardirq.h>
36+
#include <linux/bit_spinlock.h>
37+
#include <linux/rculist_bl.h>
3638
#include "internal.h"
3739

3840
/*
3941
* Usage:
4042
* dcache_inode_lock protects:
4143
* - i_dentry, d_alias, d_inode
42-
* dcache_hash_lock protects:
43-
* - the dcache hash table, s_anon lists
44+
* dcache_hash_bucket lock protects:
45+
* - the dcache hash table
46+
* s_anon bl list spinlock protects:
47+
* - the s_anon list (see __d_drop)
4448
* dcache_lru_lock protects:
4549
* - the dcache lru lists and counters
4650
* d_lock protects:
@@ -57,7 +61,8 @@
5761
* dcache_inode_lock
5862
* dentry->d_lock
5963
* dcache_lru_lock
60-
* dcache_hash_lock
64+
* dcache_hash_bucket lock
65+
* s_anon lock
6166
*
6267
* If there is an ancestor relationship:
6368
* dentry->d_parent->...->d_parent->d_lock
@@ -74,7 +79,6 @@ int sysctl_vfs_cache_pressure __read_mostly = 100;
7479
EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
7580

7681
__cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_inode_lock);
77-
static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_hash_lock);
7882
static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
7983
__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
8084

@@ -96,7 +100,29 @@ static struct kmem_cache *dentry_cache __read_mostly;
96100

97101
static unsigned int d_hash_mask __read_mostly;
98102
static unsigned int d_hash_shift __read_mostly;
99-
static struct hlist_head *dentry_hashtable __read_mostly;
103+
104+
struct dcache_hash_bucket {
105+
struct hlist_bl_head head;
106+
};
107+
static struct dcache_hash_bucket *dentry_hashtable __read_mostly;
108+
109+
static inline struct dcache_hash_bucket *d_hash(struct dentry *parent,
110+
unsigned long hash)
111+
{
112+
hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
113+
hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
114+
return dentry_hashtable + (hash & D_HASHMASK);
115+
}
116+
117+
static inline void spin_lock_bucket(struct dcache_hash_bucket *b)
118+
{
119+
bit_spin_lock(0, (unsigned long *)&b->head.first);
120+
}
121+
122+
static inline void spin_unlock_bucket(struct dcache_hash_bucket *b)
123+
{
124+
__bit_spin_unlock(0, (unsigned long *)&b->head.first);
125+
}
100126

101127
/* Statistics gathering. */
102128
struct dentry_stat_t dentry_stat = {
@@ -144,7 +170,7 @@ static void d_free(struct dentry *dentry)
144170
dentry->d_op->d_release(dentry);
145171

146172
/* if dentry was never inserted into hash, immediate free is OK */
147-
if (hlist_unhashed(&dentry->d_hash))
173+
if (hlist_bl_unhashed(&dentry->d_hash))
148174
__d_free(&dentry->d_u.d_rcu);
149175
else
150176
call_rcu(&dentry->d_u.d_rcu, __d_free);
@@ -302,11 +328,27 @@ static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
302328
void __d_drop(struct dentry *dentry)
303329
{
304330
if (!(dentry->d_flags & DCACHE_UNHASHED)) {
305-
dentry->d_flags |= DCACHE_UNHASHED;
306-
spin_lock(&dcache_hash_lock);
307-
hlist_del_rcu(&dentry->d_hash);
308-
spin_unlock(&dcache_hash_lock);
309-
dentry_rcuwalk_barrier(dentry);
331+
if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED)) {
332+
bit_spin_lock(0,
333+
(unsigned long *)&dentry->d_sb->s_anon.first);
334+
dentry->d_flags |= DCACHE_UNHASHED;
335+
hlist_bl_del_init(&dentry->d_hash);
336+
__bit_spin_unlock(0,
337+
(unsigned long *)&dentry->d_sb->s_anon.first);
338+
} else {
339+
struct dcache_hash_bucket *b;
340+
b = d_hash(dentry->d_parent, dentry->d_name.hash);
341+
spin_lock_bucket(b);
342+
/*
343+
* We may not actually need to put DCACHE_UNHASHED
344+
* manipulations under the hash lock, but follow
345+
* the principle of least surprise.
346+
*/
347+
dentry->d_flags |= DCACHE_UNHASHED;
348+
hlist_bl_del_rcu(&dentry->d_hash);
349+
spin_unlock_bucket(b);
350+
dentry_rcuwalk_barrier(dentry);
351+
}
310352
}
311353
}
312354
EXPORT_SYMBOL(__d_drop);
@@ -961,8 +1003,8 @@ void shrink_dcache_for_umount(struct super_block *sb)
9611003
spin_unlock(&dentry->d_lock);
9621004
shrink_dcache_for_umount_subtree(dentry);
9631005

964-
while (!hlist_empty(&sb->s_anon)) {
965-
dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
1006+
while (!hlist_bl_empty(&sb->s_anon)) {
1007+
dentry = hlist_bl_entry(hlist_bl_first(&sb->s_anon), struct dentry, d_hash);
9661008
shrink_dcache_for_umount_subtree(dentry);
9671009
}
9681010
}
@@ -1263,7 +1305,7 @@ struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
12631305
dentry->d_sb = NULL;
12641306
dentry->d_op = NULL;
12651307
dentry->d_fsdata = NULL;
1266-
INIT_HLIST_NODE(&dentry->d_hash);
1308+
INIT_HLIST_BL_NODE(&dentry->d_hash);
12671309
INIT_LIST_HEAD(&dentry->d_lru);
12681310
INIT_LIST_HEAD(&dentry->d_subdirs);
12691311
INIT_LIST_HEAD(&dentry->d_alias);
@@ -1459,14 +1501,6 @@ struct dentry * d_alloc_root(struct inode * root_inode)
14591501
}
14601502
EXPORT_SYMBOL(d_alloc_root);
14611503

1462-
static inline struct hlist_head *d_hash(struct dentry *parent,
1463-
unsigned long hash)
1464-
{
1465-
hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1466-
hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1467-
return dentry_hashtable + (hash & D_HASHMASK);
1468-
}
1469-
14701504
/**
14711505
* d_obtain_alias - find or allocate a dentry for a given inode
14721506
* @inode: inode to allocate the dentry for
@@ -1521,11 +1555,11 @@ struct dentry *d_obtain_alias(struct inode *inode)
15211555
tmp->d_sb = inode->i_sb;
15221556
tmp->d_inode = inode;
15231557
tmp->d_flags |= DCACHE_DISCONNECTED;
1524-
tmp->d_flags &= ~DCACHE_UNHASHED;
15251558
list_add(&tmp->d_alias, &inode->i_dentry);
1526-
spin_lock(&dcache_hash_lock);
1527-
hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
1528-
spin_unlock(&dcache_hash_lock);
1559+
bit_spin_lock(0, (unsigned long *)&tmp->d_sb->s_anon.first);
1560+
tmp->d_flags &= ~DCACHE_UNHASHED;
1561+
hlist_bl_add_head(&tmp->d_hash, &tmp->d_sb->s_anon);
1562+
__bit_spin_unlock(0, (unsigned long *)&tmp->d_sb->s_anon.first);
15291563
spin_unlock(&tmp->d_lock);
15301564
spin_unlock(&dcache_inode_lock);
15311565

@@ -1567,7 +1601,7 @@ struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
15671601
d_move(new, dentry);
15681602
iput(inode);
15691603
} else {
1570-
/* already taking dcache_inode_lock, so d_add() by hand */
1604+
/* already got dcache_inode_lock, so d_add() by hand */
15711605
__d_instantiate(dentry, inode);
15721606
spin_unlock(&dcache_inode_lock);
15731607
security_d_instantiate(dentry, inode);
@@ -1702,8 +1736,8 @@ struct dentry *__d_lookup_rcu(struct dentry *parent, struct qstr *name,
17021736
unsigned int len = name->len;
17031737
unsigned int hash = name->hash;
17041738
const unsigned char *str = name->name;
1705-
struct hlist_head *head = d_hash(parent, hash);
1706-
struct hlist_node *node;
1739+
struct dcache_hash_bucket *b = d_hash(parent, hash);
1740+
struct hlist_bl_node *node;
17071741
struct dentry *dentry;
17081742

17091743
/*
@@ -1726,7 +1760,7 @@ struct dentry *__d_lookup_rcu(struct dentry *parent, struct qstr *name,
17261760
*
17271761
* See Documentation/vfs/dcache-locking.txt for more details.
17281762
*/
1729-
hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1763+
hlist_bl_for_each_entry_rcu(dentry, node, &b->head, d_hash) {
17301764
struct inode *i;
17311765
const char *tname;
17321766
int tlen;
@@ -1820,8 +1854,8 @@ struct dentry *__d_lookup(struct dentry *parent, struct qstr *name)
18201854
unsigned int len = name->len;
18211855
unsigned int hash = name->hash;
18221856
const unsigned char *str = name->name;
1823-
struct hlist_head *head = d_hash(parent,hash);
1824-
struct hlist_node *node;
1857+
struct dcache_hash_bucket *b = d_hash(parent, hash);
1858+
struct hlist_bl_node *node;
18251859
struct dentry *found = NULL;
18261860
struct dentry *dentry;
18271861

@@ -1847,7 +1881,7 @@ struct dentry *__d_lookup(struct dentry *parent, struct qstr *name)
18471881
*/
18481882
rcu_read_lock();
18491883

1850-
hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1884+
hlist_bl_for_each_entry_rcu(dentry, node, &b->head, d_hash) {
18511885
const char *tname;
18521886
int tlen;
18531887

@@ -1998,11 +2032,13 @@ void d_delete(struct dentry * dentry)
19982032
}
19992033
EXPORT_SYMBOL(d_delete);
20002034

2001-
static void __d_rehash(struct dentry * entry, struct hlist_head *list)
2035+
static void __d_rehash(struct dentry * entry, struct dcache_hash_bucket *b)
20022036
{
2003-
2037+
BUG_ON(!d_unhashed(entry));
2038+
spin_lock_bucket(b);
20042039
entry->d_flags &= ~DCACHE_UNHASHED;
2005-
hlist_add_head_rcu(&entry->d_hash, list);
2040+
hlist_bl_add_head_rcu(&entry->d_hash, &b->head);
2041+
spin_unlock_bucket(b);
20062042
}
20072043

20082044
static void _d_rehash(struct dentry * entry)
@@ -2020,9 +2056,7 @@ static void _d_rehash(struct dentry * entry)
20202056
void d_rehash(struct dentry * entry)
20212057
{
20222058
spin_lock(&entry->d_lock);
2023-
spin_lock(&dcache_hash_lock);
20242059
_d_rehash(entry);
2025-
spin_unlock(&dcache_hash_lock);
20262060
spin_unlock(&entry->d_lock);
20272061
}
20282062
EXPORT_SYMBOL(d_rehash);
@@ -2165,15 +2199,16 @@ void d_move(struct dentry * dentry, struct dentry * target)
21652199
write_seqcount_begin(&dentry->d_seq);
21662200
write_seqcount_begin(&target->d_seq);
21672201

2168-
/* Move the dentry to the target hash queue, if on different bucket */
2169-
spin_lock(&dcache_hash_lock);
2170-
if (!d_unhashed(dentry))
2171-
hlist_del_rcu(&dentry->d_hash);
2202+
/* __d_drop does write_seqcount_barrier, but they're OK to nest. */
2203+
2204+
/*
2205+
* Move the dentry to the target hash queue. Don't bother checking
2206+
* for the same hash queue because of how unlikely it is.
2207+
*/
2208+
__d_drop(dentry);
21722209
__d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
2173-
spin_unlock(&dcache_hash_lock);
21742210

21752211
/* Unhash the target: dput() will then get rid of it */
2176-
/* __d_drop does write_seqcount_barrier, but they're OK to nest. */
21772212
__d_drop(target);
21782213

21792214
list_del(&dentry->d_u.d_child);
@@ -2369,9 +2404,7 @@ struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
23692404

23702405
spin_lock(&actual->d_lock);
23712406
found:
2372-
spin_lock(&dcache_hash_lock);
23732407
_d_rehash(actual);
2374-
spin_unlock(&dcache_hash_lock);
23752408
spin_unlock(&actual->d_lock);
23762409
spin_unlock(&dcache_inode_lock);
23772410
out_nolock:
@@ -2953,7 +2986,7 @@ static void __init dcache_init_early(void)
29532986

29542987
dentry_hashtable =
29552988
alloc_large_system_hash("Dentry cache",
2956-
sizeof(struct hlist_head),
2989+
sizeof(struct dcache_hash_bucket),
29572990
dhash_entries,
29582991
13,
29592992
HASH_EARLY,
@@ -2962,7 +2995,7 @@ static void __init dcache_init_early(void)
29622995
0);
29632996

29642997
for (loop = 0; loop < (1 << d_hash_shift); loop++)
2965-
INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2998+
INIT_HLIST_BL_HEAD(&dentry_hashtable[loop].head);
29662999
}
29673000

29683001
static void __init dcache_init(void)
@@ -2985,7 +3018,7 @@ static void __init dcache_init(void)
29853018

29863019
dentry_hashtable =
29873020
alloc_large_system_hash("Dentry cache",
2988-
sizeof(struct hlist_head),
3021+
sizeof(struct dcache_hash_bucket),
29893022
dhash_entries,
29903023
13,
29913024
0,
@@ -2994,7 +3027,7 @@ static void __init dcache_init(void)
29943027
0);
29953028

29963029
for (loop = 0; loop < (1 << d_hash_shift); loop++)
2997-
INIT_HLIST_HEAD(&dentry_hashtable[loop]);
3030+
INIT_HLIST_BL_HEAD(&dentry_hashtable[loop].head);
29983031
}
29993032

30003033
/* SLAB cache for __getname() consumers */

fs/super.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <linux/idr.h>
3131
#include <linux/mutex.h>
3232
#include <linux/backing-dev.h>
33+
#include <linux/rculist_bl.h>
3334
#include "internal.h"
3435

3536

@@ -71,7 +72,7 @@ static struct super_block *alloc_super(struct file_system_type *type)
7172
INIT_LIST_HEAD(&s->s_files);
7273
#endif
7374
INIT_LIST_HEAD(&s->s_instances);
74-
INIT_HLIST_HEAD(&s->s_anon);
75+
INIT_HLIST_BL_HEAD(&s->s_anon);
7576
INIT_LIST_HEAD(&s->s_inodes);
7677
INIT_LIST_HEAD(&s->s_dentry_lru);
7778
init_rwsem(&s->s_umount);

include/linux/dcache.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <asm/atomic.h>
55
#include <linux/list.h>
66
#include <linux/rculist.h>
7+
#include <linux/rculist_bl.h>
78
#include <linux/spinlock.h>
89
#include <linux/seqlock.h>
910
#include <linux/cache.h>
@@ -95,7 +96,7 @@ struct dentry {
9596
/* RCU lookup touched fields */
9697
unsigned int d_flags; /* protected by d_lock */
9798
seqcount_t d_seq; /* per dentry seqlock */
98-
struct hlist_node d_hash; /* lookup hash list */
99+
struct hlist_bl_node d_hash; /* lookup hash list */
99100
struct dentry *d_parent; /* parent directory */
100101
struct qstr d_name;
101102
struct inode *d_inode; /* Where the name belongs to - NULL is

include/linux/fs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ struct inodes_stat_t {
392392
#include <linux/capability.h>
393393
#include <linux/semaphore.h>
394394
#include <linux/fiemap.h>
395+
#include <linux/rculist_bl.h>
395396

396397
#include <asm/atomic.h>
397398
#include <asm/byteorder.h>
@@ -1377,7 +1378,7 @@ struct super_block {
13771378
const struct xattr_handler **s_xattr;
13781379

13791380
struct list_head s_inodes; /* all inodes */
1380-
struct hlist_head s_anon; /* anonymous dentries for (nfs) exporting */
1381+
struct hlist_bl_head s_anon; /* anonymous dentries for (nfs) exporting */
13811382
#ifdef CONFIG_SMP
13821383
struct list_head __percpu *s_files;
13831384
#else

0 commit comments

Comments
 (0)