add patch do-not-fail-journal-because-of-frozen_buffer-allocation-failure
[ext4-patch-queue.git] / reimplement-mbcache
blob5361de0fe7ac3fc4ee21ee4d9d06c15faa620f9f
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
10 complexity.
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 (414 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
23 mbcache.
25 V=10
26 F\P     1               2               4               8               16              32              64
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
31 V=100
32 F\P     1               2               4               8               16              32              64
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
37 V=1000
38 F\P     1               2               4               8               16              32              64
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
43 V=10000
44 F\P     1               2               4               8               16              32              64
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
49 V=100000
50 F\P     1               2               4               8               16              32              64
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>
63 ---
64  fs/Makefile              |   2 +-
65  fs/mbcache2.c            | 359 +++++++++++++++++++++++++++++++++++++++++++++++
66  include/linux/mbcache2.h |  50 +++++++
67  3 files changed, 410 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
73 --- a/fs/Makefile
74 +++ b/fs/Makefile
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
85 new file mode 100644
86 index 000000000000..5c3e1a8c38f6
87 --- /dev/null
88 +++ b/fs/mbcache2.c
89 @@ -0,0 +1,359 @@
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>
98 +/*
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 fact in
101 + * mb2_cache_entry_delete_block()).
102 + *
103 + * Ext2 and ext4 use this cache for deduplication of extended attribute blocks.
104 + * They use hash of a block contents as a key and block number as a value.
105 + * That's why keys need not be unique (different xattr blocks may end up having
106 + * the same hash). However block number always uniquely identifies a cache
107 + * entry.
108 + *
109 + * We provide functions for creation and removal of entries, search by key,
110 + * and a special "delete entry with given key-value pair" operation. Fixed
111 + * size hash table is used for fast key lookups.
112 + */
114 +struct mb2_cache {
115 +       /* Hash table of entries */
116 +       struct hlist_bl_head    *c_hash;
117 +       /* log2 of hash table size */
118 +       int                     c_bucket_bits;
119 +       /* Protects c_lru_list, c_entry_count */
120 +       spinlock_t              c_lru_list_lock;
121 +       struct list_head        c_lru_list;
122 +       /* Number of entries in cache */
123 +       unsigned long           c_entry_count;
124 +       struct shrinker         c_shrink;
127 +static struct kmem_cache *mb2_entry_cache;
130 + * mb2_cache_entry_create - create entry in cache
131 + * @cache - cache where the entry should be created
132 + * @mask - gfp mask with which the entry should be allocated
133 + * @key - key of the entry
134 + * @block - block that contains data
135 + *
136 + * Creates entry in @cache with key @key and records that data is stored in
137 + * block @block. The function returns -EBUSY if entry with the same key
138 + * and for the same block already exists in cache. Otherwise 0 is returned.
139 + */
140 +int mb2_cache_entry_create(struct mb2_cache *cache, gfp_t mask, u32 key,
141 +                          sector_t block)
143 +       struct mb2_cache_entry *entry, *dup;
144 +       struct hlist_bl_node *dup_node;
145 +       struct hlist_bl_head *head;
147 +       entry = kmem_cache_alloc(mb2_entry_cache, mask);
148 +       if (!entry)
149 +               return -ENOMEM;
151 +       INIT_LIST_HEAD(&entry->e_lru_list);
152 +       /* One ref for hash, one ref returned */
153 +       atomic_set(&entry->e_refcnt, 1);
154 +       entry->e_key = key;
155 +       entry->e_block = block;
156 +       head = &cache->c_hash[hash_32(key, cache->c_bucket_bits)];
157 +       entry->e_hash_list_head = head;
158 +       hlist_bl_lock(head);
159 +       hlist_bl_for_each_entry(dup, dup_node, head, e_hash_list) {
160 +               if (dup->e_key == key && dup->e_block == block) {
161 +                       hlist_bl_unlock(head);
162 +                       kmem_cache_free(mb2_entry_cache, entry);
163 +                       return -EBUSY;
164 +               }
165 +       }
166 +       hlist_bl_add_head(&entry->e_hash_list, head);
167 +       hlist_bl_unlock(head);
169 +       spin_lock(&cache->c_lru_list_lock);
170 +       list_add_tail(&entry->e_lru_list, &cache->c_lru_list);
171 +       /* Grab ref for LRU list */
172 +       atomic_inc(&entry->e_refcnt);
173 +       cache->c_entry_count++;
174 +       spin_unlock(&cache->c_lru_list_lock);
176 +       return 0;
178 +EXPORT_SYMBOL(mb2_cache_entry_create);
180 +void __mb2_cache_entry_free(struct mb2_cache_entry *entry)
182 +       kmem_cache_free(mb2_entry_cache, entry);
184 +EXPORT_SYMBOL(__mb2_cache_entry_free);
186 +static struct mb2_cache_entry *__entry_find(struct mb2_cache *cache,
187 +                                           struct mb2_cache_entry *entry,
188 +                                           u32 key)
190 +       struct mb2_cache_entry *old_entry = entry;
191 +       struct hlist_bl_node *node;
192 +       struct hlist_bl_head *head;
194 +       if (entry)
195 +               head = entry->e_hash_list_head;
196 +       else
197 +               head = &cache->c_hash[hash_32(key, cache->c_bucket_bits)];
198 +       hlist_bl_lock(head);
199 +       if (entry && !hlist_bl_unhashed(&entry->e_hash_list))
200 +               node = entry->e_hash_list.next;
201 +       else
202 +               node = hlist_bl_first(head);
203 +       while (node) {
204 +               entry = hlist_bl_entry(node, struct mb2_cache_entry,
205 +                                      e_hash_list);
206 +               if (entry->e_key == key) {
207 +                       atomic_inc(&entry->e_refcnt);
208 +                       goto out;
209 +               }
210 +               node = node->next;
211 +       }
212 +       entry = NULL;
213 +out:
214 +       hlist_bl_unlock(head);
215 +       if (old_entry)
216 +               mb2_cache_entry_put(cache, old_entry);
218 +       return entry;
222 + * mb2_cache_entry_find_first - find the first entry in cache with given key
223 + * @cache: cache where we should search
224 + * @key: key to look for
225 + *
226 + * Search in @cache for entry with key @key. Grabs reference to the first
227 + * entry found and returns the entry.
228 + */
229 +struct mb2_cache_entry *mb2_cache_entry_find_first(struct mb2_cache *cache,
230 +                                                  u32 key)
232 +       return __entry_find(cache, NULL, key);
234 +EXPORT_SYMBOL(mb2_cache_entry_find_first);
237 + * mb2_cache_entry_find_next - find next entry in cache with the same
238 + * @cache: cache where we should search
239 + * @entry: entry to start search from
240 + *
241 + * Finds next entry in the hash chain which has the same key as @entry.
242 + * If @entry is unhashed (which can happen when deletion of entry races
243 + * with the search), finds the first entry in the hash chain. The function
244 + * drops reference to @entry and returns with a reference to the found entry.
245 + */
246 +struct mb2_cache_entry *mb2_cache_entry_find_next(struct mb2_cache *cache,
247 +                                                 struct mb2_cache_entry *entry)
249 +       return __entry_find(cache, entry, entry->e_key);
251 +EXPORT_SYMBOL(mb2_cache_entry_find_next);
253 +/* mb2_cache_entry_delete_block - remove information about block from cache
254 + * @cache - cache we work with
255 + * @key - key of the entry to remove
256 + * @block - block containing data for @key
257 + *
258 + * Remove entry from cache @cache with key @key with data stored in @block.
259 + */
260 +void mb2_cache_entry_delete_block(struct mb2_cache *cache, u32 key,
261 +                                 sector_t block)
263 +       struct hlist_bl_node *node;
264 +       struct hlist_bl_head *head;
265 +       struct mb2_cache_entry *entry;
267 +       head = &cache->c_hash[hash_32(key, cache->c_bucket_bits)];
268 +       hlist_bl_lock(head);
269 +       hlist_bl_for_each_entry(entry, node, head, e_hash_list) {
270 +               if (entry->e_key == key && entry->e_block == block) {
271 +                       /* We keep hash list reference to keep entry alive */
272 +                       hlist_bl_del_init(&entry->e_hash_list);
273 +                       hlist_bl_unlock(head);
274 +                       spin_lock(&cache->c_lru_list_lock);
275 +                       if (!list_empty(&entry->e_lru_list)) {
276 +                               list_del_init(&entry->e_lru_list);
277 +                               cache->c_entry_count--;
278 +                               atomic_dec(&entry->e_refcnt);
279 +                       }
280 +                       spin_unlock(&cache->c_lru_list_lock);
281 +                       mb2_cache_entry_put(cache, entry);
282 +                       return;
283 +               }
284 +       }
285 +       hlist_bl_unlock(head);
287 +EXPORT_SYMBOL(mb2_cache_entry_delete_block);
289 +/* mb2_cache_entry_touch - cache entry got used
290 + * @cache - cache the entry belongs to
291 + * @entry - entry that got used
292 + *
293 + * Move entry in lru list to reflect the fact that it was used.
294 + */
295 +void mb2_cache_entry_touch(struct mb2_cache *cache,
296 +                          struct mb2_cache_entry *entry)
298 +       spin_lock(&cache->c_lru_list_lock);
299 +       if (!list_empty(&entry->e_lru_list))
300 +               list_move_tail(&cache->c_lru_list, &entry->e_lru_list);
301 +       spin_unlock(&cache->c_lru_list_lock);
303 +EXPORT_SYMBOL(mb2_cache_entry_touch);
305 +static unsigned long mb2_cache_count(struct shrinker *shrink,
306 +                                    struct shrink_control *sc)
308 +       struct mb2_cache *cache = container_of(shrink, struct mb2_cache,
309 +                                              c_shrink);
311 +       return cache->c_entry_count;
314 +/* Shrink number of entries in cache */
315 +static unsigned long mb2_cache_scan(struct shrinker *shrink,
316 +                                   struct shrink_control *sc)
318 +       int nr_to_scan = sc->nr_to_scan;
319 +       struct mb2_cache *cache = container_of(shrink, struct mb2_cache,
320 +                                             c_shrink);
321 +       struct mb2_cache_entry *entry;
322 +       struct hlist_bl_head *head;
323 +       unsigned int shrunk = 0;
325 +       spin_lock(&cache->c_lru_list_lock);
326 +       while (nr_to_scan-- && !list_empty(&cache->c_lru_list)) {
327 +               entry = list_first_entry(&cache->c_lru_list,
328 +                                        struct mb2_cache_entry, e_lru_list);
329 +               list_del_init(&entry->e_lru_list);
330 +               cache->c_entry_count--;
331 +               /*
332 +                * We keep LRU list reference so that entry doesn't go away
333 +                * from under us.
334 +                */
335 +               spin_unlock(&cache->c_lru_list_lock);
336 +               head = entry->e_hash_list_head;
337 +               hlist_bl_lock(head);
338 +               if (!hlist_bl_unhashed(&entry->e_hash_list)) {
339 +                       hlist_bl_del_init(&entry->e_hash_list);
340 +                       atomic_dec(&entry->e_refcnt);
341 +               }
342 +               hlist_bl_unlock(head);
343 +               if (mb2_cache_entry_put(cache, entry))
344 +                       shrunk++;
345 +               cond_resched();
346 +               spin_lock(&cache->c_lru_list_lock);
347 +       }
348 +       spin_unlock(&cache->c_lru_list_lock);
350 +       return shrunk;
354 + * mb2_cache_create - create cache
355 + * @bucket_bits: log2 of the hash table size
356 + *
357 + * Create cache for keys with 2^bucket_bits hash entries.
358 + */
359 +struct mb2_cache *mb2_cache_create(int bucket_bits)
361 +       struct mb2_cache *cache;
362 +       int bucket_count = 1 << bucket_bits;
363 +       int i;
365 +       if (!try_module_get(THIS_MODULE))
366 +               return NULL;
368 +       cache = kzalloc(sizeof(struct mb2_cache), GFP_KERNEL);
369 +       if (!cache)
370 +               goto err_out;
371 +       cache->c_bucket_bits = bucket_bits;
372 +       INIT_LIST_HEAD(&cache->c_lru_list);
373 +       spin_lock_init(&cache->c_lru_list_lock);
374 +       cache->c_hash = kmalloc(bucket_count * sizeof(struct hlist_bl_head),
375 +                               GFP_KERNEL);
376 +       if (!cache->c_hash) {
377 +               kfree(cache);
378 +               goto err_out;
379 +       }
380 +       for (i = 0; i < bucket_count; i++)
381 +               INIT_HLIST_BL_HEAD(&cache->c_hash[i]);
383 +       cache->c_shrink.count_objects = mb2_cache_count;
384 +       cache->c_shrink.scan_objects = mb2_cache_scan;
385 +       cache->c_shrink.seeks = DEFAULT_SEEKS;
386 +       register_shrinker(&cache->c_shrink);
388 +       return cache;
390 +err_out:
391 +       module_put(THIS_MODULE);
392 +       return NULL;
394 +EXPORT_SYMBOL(mb2_cache_create);
397 + * mb2_cache_destroy - destroy cache
398 + * @cache: the cache to destroy
399 + *
400 + * Free all entries in cache and cache itself. Caller must make sure nobody
401 + * (except shrinker) can reach @cache when calling this.
402 + */
403 +void mb2_cache_destroy(struct mb2_cache *cache)
405 +       struct mb2_cache_entry *entry, *next;
407 +       unregister_shrinker(&cache->c_shrink);
409 +       /*
410 +        * We don't bother with any locking. Cache must not be used at this
411 +        * point.
412 +        */
413 +       list_for_each_entry_safe(entry, next, &cache->c_lru_list, e_lru_list) {
414 +               if (!hlist_bl_unhashed(&entry->e_hash_list)) {
415 +                       hlist_bl_del_init(&entry->e_hash_list);
416 +                       atomic_dec(&entry->e_refcnt);
417 +               } else
418 +                       WARN_ON(1);
419 +               list_del(&entry->e_lru_list);
420 +               WARN_ON(atomic_read(&entry->e_refcnt) != 1);
421 +               mb2_cache_entry_put(cache, entry);
422 +       }
423 +       kfree(cache->c_hash);
424 +       kfree(cache);
425 +       module_put(THIS_MODULE);
427 +EXPORT_SYMBOL(mb2_cache_destroy);
429 +static int __init mb2cache_init(void)
431 +       mb2_entry_cache = kmem_cache_create("mbcache",
432 +                               sizeof(struct mb2_cache_entry), 0,
433 +                               SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL);
434 +       BUG_ON(!mb2_entry_cache);
435 +       return 0;
438 +static void __exit mb2cache_exit(void)
440 +       kmem_cache_destroy(mb2_entry_cache);
443 +module_init(mb2cache_init)
444 +module_exit(mb2cache_exit)
446 +MODULE_AUTHOR("Jan Kara <jack@suse.cz>");
447 +MODULE_DESCRIPTION("Meta block cache (for extended attributes)");
448 +MODULE_LICENSE("GPL");
449 diff --git a/include/linux/mbcache2.h b/include/linux/mbcache2.h
450 new file mode 100644
451 index 000000000000..b6f160ff2533
452 --- /dev/null
453 +++ b/include/linux/mbcache2.h
454 @@ -0,0 +1,50 @@
455 +#ifndef _LINUX_MB2CACHE_H
456 +#define _LINUX_MB2CACHE_H
458 +#include <linux/hash.h>
459 +#include <linux/list_bl.h>
460 +#include <linux/list.h>
461 +#include <linux/atomic.h>
462 +#include <linux/fs.h>
464 +struct mb2_cache;
466 +struct mb2_cache_entry {
467 +       /* LRU list - protected by cache->c_lru_list_lock */
468 +       struct list_head        e_lru_list;
469 +       /* Hash table list - protected by bitlock in e_hash_list_head */
470 +       struct hlist_bl_node    e_hash_list;
471 +       atomic_t                e_refcnt;
472 +       /* Key in hash - stable during lifetime of the entry */
473 +       u32                     e_key;
474 +       /* Block number of hashed block - stable during lifetime of the entry */
475 +       sector_t                e_block;
476 +       /* Head of hash list (for list bit lock) - stable */
477 +       struct hlist_bl_head    *e_hash_list_head;
480 +struct mb2_cache *mb2_cache_create(int bucket_bits);
481 +void mb2_cache_destroy(struct mb2_cache *cache);
483 +int mb2_cache_entry_create(struct mb2_cache *cache, gfp_t mask, u32 key,
484 +                          sector_t block);
485 +void __mb2_cache_entry_free(struct mb2_cache_entry *entry);
486 +static inline int mb2_cache_entry_put(struct mb2_cache *cache,
487 +                                     struct mb2_cache_entry *entry)
489 +       if (!atomic_dec_and_test(&entry->e_refcnt))
490 +               return 0;
491 +       __mb2_cache_entry_free(entry);
492 +       return 1;
495 +void mb2_cache_entry_delete_block(struct mb2_cache *cache, u32 key,
496 +                                 sector_t block);
497 +struct mb2_cache_entry *mb2_cache_entry_find_first(struct mb2_cache *cache,
498 +                                                  u32 key);
499 +struct mb2_cache_entry *mb2_cache_entry_find_next(struct mb2_cache *cache,
500 +                                                 struct mb2_cache_entry *entry);
501 +void mb2_cache_entry_touch(struct mb2_cache *cache,
502 +                          struct mb2_cache_entry *entry);
504 +#endif /* _LINUX_MB2CACHE_H */
505 -- 
506 2.6.2