1 mbcache2: reimplement mbcache
3 From: Jan Kara <jack@suse.cz>
5 Original mbcache was designed to have more features than what ext?
6 filesystems ended up using. It supported entry being in more hashes, it
7 had a home-grown rwlocking of each entry, and one cache could cache
8 entries from multiple filesystems. This genericity also resulted in more
9 complex locking, larger cache entries, and generally more code
12 This is reimplementation of the mbcache functionality to exactly fit the
13 purpose ext? filesystems use it for. Cache entries are now considerably
14 smaller (7 instead of 13 longs), the code is considerably smaller as
15 well (432 vs 913 lines of code), and IMO also simpler. The new code is
16 also much more lightweight.
18 I have measured the speed using artificial xattr-bench benchmark, which
19 spawns P processes, each process sets xattr for F different files, and
20 the value of xattr is randomly chosen from a pool of V values. Averages
21 of runtimes for 5 runs for various combinations of parameters are below.
22 The first value in each cell is old mbache, the second value is the new
27 10 0.158,0.157 0.208,0.196 0.500,0.277 0.798,0.400 3.258,0.584 13.807,1.047 61.339,2.803
28 100 0.172,0.167 0.279,0.222 0.520,0.275 0.825,0.341 2.981,0.505 12.022,1.202 44.641,2.943
29 1000 0.185,0.174 0.297,0.239 0.445,0.283 0.767,0.340 2.329,0.480 6.342,1.198 16.440,3.888
33 10 0.162,0.153 0.200,0.186 0.362,0.257 0.671,0.496 1.433,0.943 3.801,1.345 7.938,2.501
34 100 0.153,0.160 0.221,0.199 0.404,0.264 0.945,0.379 1.556,0.485 3.761,1.156 7.901,2.484
35 1000 0.215,0.191 0.303,0.246 0.471,0.288 0.960,0.347 1.647,0.479 3.916,1.176 8.058,3.160
39 10 0.151,0.129 0.210,0.163 0.326,0.245 0.685,0.521 1.284,0.859 3.087,2.251 6.451,4.801
40 100 0.154,0.153 0.211,0.191 0.276,0.282 0.687,0.506 1.202,0.877 3.259,1.954 8.738,2.887
41 1000 0.145,0.179 0.202,0.222 0.449,0.319 0.899,0.333 1.577,0.524 4.221,1.240 9.782,3.579
45 10 0.161,0.154 0.198,0.190 0.296,0.256 0.662,0.480 1.192,0.818 2.989,2.200 6.362,4.746
46 100 0.176,0.174 0.236,0.203 0.326,0.255 0.696,0.511 1.183,0.855 4.205,3.444 19.510,17.760
47 1000 0.199,0.183 0.240,0.227 1.159,1.014 2.286,2.154 6.023,6.039 10.933,--- 36.620,---
51 10 0.171,0.162 0.204,0.198 0.285,0.230 0.692,0.500 1.225,0.881 2.990,2.243 6.379,4.771
52 100 0.151,0.171 0.220,0.210 0.295,0.255 0.720,0.518 1.226,0.844 3.423,2.831 19.234,17.544
53 1000 0.192,0.189 0.249,0.225 1.162,1.043 2.257,2.093 5.853,4.997 10.399,--- 32.198,---
55 We see that the new code is faster in pretty much all the cases and
56 starting from 4 processes there are significant gains with the new code
57 resulting in upto 20-times shorter runtimes. Also for large numbers of
58 cached entries all values for the old code could not be measured as the
59 kernel started hitting softlockups and died before the test completed.
61 Signed-off-by: Jan Kara <jack@suse.cz>
62 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
65 fs/mbcache2.c | 388 +++++++++++++++++++++++++++++++++++++++++++++++
66 include/linux/mbcache2.h | 54 +++++++
67 3 files changed, 443 insertions(+), 1 deletion(-)
68 create mode 100644 fs/mbcache2.c
69 create mode 100644 include/linux/mbcache2.h
71 diff --git a/fs/Makefile b/fs/Makefile
72 index 79f522575cba..15b3d6c4e46a 100644
75 @@ -41,7 +41,7 @@ obj-$(CONFIG_COMPAT_BINFMT_ELF) += compat_binfmt_elf.o
76 obj-$(CONFIG_BINFMT_ELF_FDPIC) += binfmt_elf_fdpic.o
77 obj-$(CONFIG_BINFMT_FLAT) += binfmt_flat.o
79 -obj-$(CONFIG_FS_MBCACHE) += mbcache.o
80 +obj-$(CONFIG_FS_MBCACHE) += mbcache.o mbcache2.o
81 obj-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
82 obj-$(CONFIG_NFS_COMMON) += nfs_common/
83 obj-$(CONFIG_COREDUMP) += coredump.o
84 diff --git a/fs/mbcache2.c b/fs/mbcache2.c
86 index 000000000000..4ccf0752c6d1
90 +#include <linux/spinlock.h>
91 +#include <linux/slab.h>
92 +#include <linux/list.h>
93 +#include <linux/list_bl.h>
94 +#include <linux/module.h>
95 +#include <linux/sched.h>
96 +#include <linux/mbcache2.h>
99 + * Mbcache is a simple key-value store. Keys need not be unique, however
100 + * key-value pairs are expected to be unique (we use this in
101 + * mb2_cache_entry_delete_block()).
103 + * We provide functions for creation and removal of entries, search by key,
104 + * and a special "delete entry with given key-value pair" operation. Fixed
105 + * size hash table is used for fast key lookups.
109 + /* Hash table of entries */
110 + struct hlist_bl_head *c_hash;
111 + /* log2 of hash table size */
113 + /* Protects c_lru_list, c_entry_count */
114 + spinlock_t c_lru_list_lock;
115 + struct list_head c_lru_list;
116 + /* Number of entries in cache */
117 + unsigned long c_entry_count;
118 + struct shrinker c_shrink;
121 +static struct kmem_cache *mb2_entry_cache;
124 + * mb2_cache_entry_create - create entry in cache
125 + * @cache - cache where the entry should be created
126 + * @mask - gfp mask with which the entry should be allocated
127 + * @key - key of the entry
128 + * @block - block that contains data
130 + * Creates entry in @cache with key @key and records that data is stored in
131 + * block @block. The function returns -EBUSY if entry with the same key
132 + * and for the same block already exists in cache. Otherwise reference to
133 + * the created entry is returned.
135 +struct mb2_cache_entry *mb2_cache_entry_create(struct mb2_cache *cache,
140 + struct mb2_cache_entry *entry, *dup;
141 + struct hlist_bl_node *dup_node;
142 + struct hlist_bl_head *head;
144 + entry = kmem_cache_alloc(mb2_entry_cache, mask);
146 + return ERR_PTR(-ENOMEM);
148 + INIT_LIST_HEAD(&entry->e_lru_list);
149 + /* One ref for hash, one ref returned */
150 + atomic_set(&entry->e_refcnt, 2);
151 + entry->e_key = key;
152 + entry->e_block = block;
153 + head = &cache->c_hash[hash_32(key, cache->c_bucket_bits)];
154 + entry->e_hash_list_head = head;
155 + hlist_bl_lock(head);
156 + hlist_bl_for_each_entry(dup, dup_node, head, e_hash_list) {
157 + if (dup->e_key == key && dup->e_block == block) {
158 + hlist_bl_unlock(head);
159 + kmem_cache_free(mb2_entry_cache, entry);
160 + return ERR_PTR(-EBUSY);
163 + hlist_bl_add_head(&entry->e_hash_list, head);
164 + hlist_bl_unlock(head);
166 + spin_lock(&cache->c_lru_list_lock);
167 + list_add_tail(&entry->e_lru_list, &cache->c_lru_list);
168 + /* Grab ref for LRU list */
169 + atomic_inc(&entry->e_refcnt);
170 + cache->c_entry_count++;
171 + spin_unlock(&cache->c_lru_list_lock);
175 +EXPORT_SYMBOL(mb2_cache_entry_create);
177 +void __mb2_cache_entry_free(struct mb2_cache_entry *entry)
179 + kmem_cache_free(mb2_entry_cache, entry);
181 +EXPORT_SYMBOL(__mb2_cache_entry_free);
184 + * mb2_cache_entry_delete - delete entry from cache
185 + * @cache - cache where the entry is
186 + * @entry - entry to delete
188 + * Delete entry from cache. The entry is unhashed and deleted from the lru list
189 + * so it cannot be found. We also drop the reference to @entry caller gave us.
190 + * However entry need not be freed if there's someone else still holding a
191 + * reference to it. Freeing happens when the last reference is dropped.
193 +void mb2_cache_entry_delete(struct mb2_cache *cache,
194 + struct mb2_cache_entry *entry)
196 + struct hlist_bl_head *head = entry->e_hash_list_head;
198 + hlist_bl_lock(head);
199 + if (!hlist_bl_unhashed(&entry->e_hash_list)) {
200 + hlist_bl_del_init(&entry->e_hash_list);
201 + atomic_dec(&entry->e_refcnt);
203 + hlist_bl_unlock(head);
204 + spin_lock(&cache->c_lru_list_lock);
205 + if (!list_empty(&entry->e_lru_list)) {
206 + list_del_init(&entry->e_lru_list);
207 + cache->c_entry_count--;
208 + atomic_dec(&entry->e_refcnt);
210 + spin_unlock(&cache->c_lru_list_lock);
211 + mb2_cache_entry_put(cache, entry);
213 +EXPORT_SYMBOL(mb2_cache_entry_delete);
215 +static struct mb2_cache_entry *__entry_find(struct mb2_cache *cache,
216 + struct mb2_cache_entry *entry,
219 + struct mb2_cache_entry *old_entry = entry;
220 + struct hlist_bl_node *node;
221 + struct hlist_bl_head *head;
224 + head = entry->e_hash_list_head;
226 + head = &cache->c_hash[hash_32(key, cache->c_bucket_bits)];
227 + hlist_bl_lock(head);
228 + if (entry && !hlist_bl_unhashed(&entry->e_hash_list))
229 + node = entry->e_hash_list.next;
231 + node = hlist_bl_first(head);
233 + entry = hlist_bl_entry(node, struct mb2_cache_entry,
235 + if (entry->e_key == key) {
236 + atomic_inc(&entry->e_refcnt);
243 + hlist_bl_unlock(head);
245 + mb2_cache_entry_put(cache, old_entry);
251 + * mb2_cache_entry_find_first - find the first entry in cache with given key
252 + * @cache: cache where we should search
253 + * @key: key to look for
255 + * Search in @cache for entry with key @key. Grabs reference to the first
256 + * entry found and returns the entry.
258 +struct mb2_cache_entry *mb2_cache_entry_find_first(struct mb2_cache *cache,
261 + return __entry_find(cache, NULL, key);
263 +EXPORT_SYMBOL(mb2_cache_entry_find_first);
266 + * mb2_cache_entry_find_next - find next entry in cache with the same
267 + * @cache: cache where we should search
268 + * @entry: entry to start search from
270 + * Finds next entry in the hash chain which has the same key as @entry.
271 + * If @entry is unhashed (which can happen when deletion of entry races
272 + * with the search), finds the first entry in the hash chain. The function
273 + * drops reference to @entry and returns with a reference to the found entry.
275 +struct mb2_cache_entry *mb2_cache_entry_find_next(struct mb2_cache *cache,
276 + struct mb2_cache_entry *entry)
278 + return __entry_find(cache, entry, entry->e_key);
280 +EXPORT_SYMBOL(mb2_cache_entry_find_next);
282 +/* mb2_cache_entry_delete_block - remove information about block from cache
283 + * @cache - cache we work with
284 + * @key - key of the entry to remove
285 + * @block - block containing data for @key
287 + * Remove entry from cache @cache with key @key with data stored in @block.
289 +void mb2_cache_entry_delete_block(struct mb2_cache *cache, unsigned int key,
292 + struct hlist_bl_node *node;
293 + struct hlist_bl_head *head;
294 + struct mb2_cache_entry *entry;
296 + head = &cache->c_hash[hash_32(key, cache->c_bucket_bits)];
297 + hlist_bl_lock(head);
298 + hlist_bl_for_each_entry(entry, node, head, e_hash_list) {
299 + if (entry->e_key == key && entry->e_block == block) {
300 + /* We keep hash list reference to keep entry alive */
301 + hlist_bl_del_init(&entry->e_hash_list);
302 + hlist_bl_unlock(head);
303 + spin_lock(&cache->c_lru_list_lock);
304 + if (!list_empty(&entry->e_lru_list)) {
305 + list_del_init(&entry->e_lru_list);
306 + cache->c_entry_count--;
307 + atomic_dec(&entry->e_refcnt);
309 + spin_unlock(&cache->c_lru_list_lock);
310 + mb2_cache_entry_put(cache, entry);
314 + hlist_bl_unlock(head);
316 +EXPORT_SYMBOL(mb2_cache_entry_delete_block);
318 +/* mb2_cache_entry_touch - cache entry got used
319 + * @cache - cache the entry belongs to
320 + * @entry - entry that got used
322 + * Move entry in lru list to reflect the fact that it was used.
324 +void mb2_cache_entry_touch(struct mb2_cache *cache,
325 + struct mb2_cache_entry *entry)
327 + spin_lock(&cache->c_lru_list_lock);
328 + if (!list_empty(&entry->e_lru_list))
329 + list_move_tail(&cache->c_lru_list, &entry->e_lru_list);
330 + spin_unlock(&cache->c_lru_list_lock);
332 +EXPORT_SYMBOL(mb2_cache_entry_touch);
334 +static unsigned long mb2_cache_count(struct shrinker *shrink,
335 + struct shrink_control *sc)
337 + struct mb2_cache *cache = container_of(shrink, struct mb2_cache,
340 + return cache->c_entry_count;
343 +/* Shrink number of entries in cache */
344 +static unsigned long mb2_cache_scan(struct shrinker *shrink,
345 + struct shrink_control *sc)
347 + int nr_to_scan = sc->nr_to_scan;
348 + struct mb2_cache *cache = container_of(shrink, struct mb2_cache,
350 + struct mb2_cache_entry *entry;
351 + struct hlist_bl_head *head;
352 + unsigned int shrunk = 0;
354 + spin_lock(&cache->c_lru_list_lock);
355 + while (nr_to_scan-- && !list_empty(&cache->c_lru_list)) {
356 + entry = list_first_entry(&cache->c_lru_list,
357 + struct mb2_cache_entry, e_lru_list);
358 + list_del_init(&entry->e_lru_list);
359 + cache->c_entry_count--;
361 + * We keep LRU list reference so that entry doesn't go away
364 + spin_unlock(&cache->c_lru_list_lock);
365 + head = entry->e_hash_list_head;
366 + hlist_bl_lock(head);
367 + if (!hlist_bl_unhashed(&entry->e_hash_list)) {
368 + hlist_bl_del_init(&entry->e_hash_list);
369 + atomic_dec(&entry->e_refcnt);
371 + hlist_bl_unlock(head);
372 + if (mb2_cache_entry_put(cache, entry))
375 + spin_lock(&cache->c_lru_list_lock);
377 + spin_unlock(&cache->c_lru_list_lock);
383 + * mb2_cache_create - create cache
384 + * @bucket_bits: log2 of the hash table size
386 + * Create cache for keys with 2^bucket_bits hash entries.
388 +struct mb2_cache *mb2_cache_create(int bucket_bits)
390 + struct mb2_cache *cache;
391 + int bucket_count = 1 << bucket_bits;
394 + if (!try_module_get(THIS_MODULE))
397 + cache = kzalloc(sizeof(struct mb2_cache), GFP_KERNEL);
400 + cache->c_bucket_bits = bucket_bits;
401 + INIT_LIST_HEAD(&cache->c_lru_list);
402 + spin_lock_init(&cache->c_lru_list_lock);
403 + cache->c_hash = kmalloc(bucket_count * sizeof(struct hlist_bl_head),
405 + if (!cache->c_hash) {
409 + for (i = 0; i < bucket_count; i++)
410 + INIT_HLIST_BL_HEAD(&cache->c_hash[i]);
412 + cache->c_shrink.count_objects = mb2_cache_count;
413 + cache->c_shrink.scan_objects = mb2_cache_scan;
414 + cache->c_shrink.seeks = DEFAULT_SEEKS;
415 + register_shrinker(&cache->c_shrink);
420 + module_put(THIS_MODULE);
423 +EXPORT_SYMBOL(mb2_cache_create);
426 + * mb2_cache_destroy - destroy cache
427 + * @cache: the cache to destroy
429 + * Free all entries in cache and cache itself. Caller must make sure nobody
430 + * (except shrinker) can reach @cache when calling this.
432 +void mb2_cache_destroy(struct mb2_cache *cache)
434 + struct mb2_cache_entry *entry, *next;
436 + unregister_shrinker(&cache->c_shrink);
439 + * We don't bother with any locking. Cache must not be used at this
442 + list_for_each_entry_safe(entry, next, &cache->c_lru_list, e_lru_list) {
443 + if (!hlist_bl_unhashed(&entry->e_hash_list)) {
444 + hlist_bl_del_init(&entry->e_hash_list);
445 + atomic_dec(&entry->e_refcnt);
448 + list_del(&entry->e_lru_list);
449 + WARN_ON(atomic_read(&entry->e_refcnt) != 1);
450 + mb2_cache_entry_put(cache, entry);
452 + kfree(cache->c_hash);
454 + module_put(THIS_MODULE);
456 +EXPORT_SYMBOL(mb2_cache_destroy);
458 +static int __init mb2cache_init(void)
460 + mb2_entry_cache = kmem_cache_create("mbcache",
461 + sizeof(struct mb2_cache_entry), 0,
462 + SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL);
463 + BUG_ON(!mb2_entry_cache);
467 +static void __exit mb2cache_exit(void)
469 + kmem_cache_destroy(mb2_entry_cache);
472 +module_init(mb2cache_init)
473 +module_exit(mb2cache_exit)
475 +MODULE_AUTHOR("Jan Kara <jack@suse.cz>");
476 +MODULE_DESCRIPTION("Meta block cache (for extended attributes)");
477 +MODULE_LICENSE("GPL");
478 diff --git a/include/linux/mbcache2.h b/include/linux/mbcache2.h
480 index 000000000000..2a58c51c3a0a
482 +++ b/include/linux/mbcache2.h
484 +#ifndef _LINUX_MB2CACHE_H
485 +#define _LINUX_MB2CACHE_H
487 +#include <linux/hash.h>
488 +#include <linux/list_bl.h>
489 +#include <linux/list.h>
490 +#include <linux/atomic.h>
491 +#include <linux/fs.h>
495 +struct mb2_cache_entry {
496 + /* LRU list - protected by cache->c_lru_list_lock */
497 + struct list_head e_lru_list;
498 + /* Hash table list - protected by bitlock in e_hash_list_head */
499 + struct hlist_bl_node e_hash_list;
501 + /* Key in hash - stable during lifetime of the entry */
502 + unsigned int e_key;
503 + /* Block number of hashed block - stable during lifetime of the entry */
505 + /* Head of hash list (for list bit lock) - stable */
506 + struct hlist_bl_head *e_hash_list_head;
509 +struct mb2_cache *mb2_cache_create(int bucket_bits);
510 +void mb2_cache_destroy(struct mb2_cache *cache);
512 +struct mb2_cache_entry *mb2_cache_entry_create(struct mb2_cache *cache,
516 +void mb2_cache_entry_delete(struct mb2_cache *cache,
517 + struct mb2_cache_entry *entry);
518 +void __mb2_cache_entry_free(struct mb2_cache_entry *entry);
519 +static inline int mb2_cache_entry_put(struct mb2_cache *cache,
520 + struct mb2_cache_entry *entry)
522 + if (!atomic_dec_and_test(&entry->e_refcnt))
524 + __mb2_cache_entry_free(entry);
528 +void mb2_cache_entry_delete_block(struct mb2_cache *cache, unsigned int key,
530 +struct mb2_cache_entry *mb2_cache_entry_find_first(struct mb2_cache *cache,
532 +struct mb2_cache_entry *mb2_cache_entry_find_next(struct mb2_cache *cache,
533 + struct mb2_cache_entry *entry);
534 +void mb2_cache_entry_touch(struct mb2_cache *cache,
535 + struct mb2_cache_entry *entry);
537 +#endif /* _LINUX_MB2CACHE_H */