add patch move-trans-handling-and-completion-deferal-out-of-_ext4_get_block
[ext4-patch-queue.git] / mbcache2-limit-cache-size
blobced2081df90191b39ea23a11cbcb48e909ba9724
1 mbcache2: limit cache size
3 From: Jan Kara <jack@suse.cz>
5 So far number of entries in mbcache is limited only by the pressure from
6 the shrinker. Since too many entries degrade the hash table and
7 generally we expect that caching more entries has diminishing returns,
8 limit number of entries the same way as in the old mbcache to 16 * hash
9 table size.
11 Once we exceed the desired maximum number of entries, we schedule a
12 backround work to reclaim entries. If the background work cannot keep up
13 and the number of entries exceeds two times the desired maximum, we
14 reclaim some entries directly when allocating a new entry.
16 Signed-off-by: Jan Kara <jack@suse.cz>
17 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
18 ---
19  fs/mbcache2.c | 50 +++++++++++++++++++++++++++++++++++++++++++++-----
20  1 file changed, 45 insertions(+), 5 deletions(-)
22 diff --git a/fs/mbcache2.c b/fs/mbcache2.c
23 index 5c3e1a8c38f6..3e3198d6b9d6 100644
24 --- a/fs/mbcache2.c
25 +++ b/fs/mbcache2.c
26 @@ -4,6 +4,7 @@
27  #include <linux/list_bl.h>
28  #include <linux/module.h>
29  #include <linux/sched.h>
30 +#include <linux/workqueue.h>
31  #include <linux/mbcache2.h>
33  /*
34 @@ -27,16 +28,29 @@ struct mb2_cache {
35         struct hlist_bl_head    *c_hash;
36         /* log2 of hash table size */
37         int                     c_bucket_bits;
38 +       /* Maximum entries in cache to avoid degrading hash too much */
39 +       int                     c_max_entries;
40         /* Protects c_lru_list, c_entry_count */
41         spinlock_t              c_lru_list_lock;
42         struct list_head        c_lru_list;
43         /* Number of entries in cache */
44         unsigned long           c_entry_count;
45         struct shrinker         c_shrink;
46 +       /* Work for shrinking when the cache has too many entries */
47 +       struct work_struct      c_shrink_work;
48  };
50  static struct kmem_cache *mb2_entry_cache;
52 +static unsigned long mb2_cache_shrink(struct mb2_cache *cache,
53 +                                     unsigned int nr_to_scan);
55 +/*
56 + * Number of entries to reclaim synchronously when there are too many entries
57 + * in cache
58 + */
59 +#define SYNC_SHRINK_BATCH 64
61  /*
62   * mb2_cache_entry_create - create entry in cache
63   * @cache - cache where the entry should be created
64 @@ -55,6 +69,13 @@ int mb2_cache_entry_create(struct mb2_cache *cache, gfp_t mask, u32 key,
65         struct hlist_bl_node *dup_node;
66         struct hlist_bl_head *head;
68 +       /* Schedule background reclaim if there are too many entries */
69 +       if (cache->c_entry_count >= cache->c_max_entries)
70 +               schedule_work(&cache->c_shrink_work);
71 +       /* Do some sync reclaim if background reclaim cannot keep up */
72 +       if (cache->c_entry_count >= 2*cache->c_max_entries)
73 +               mb2_cache_shrink(cache, SYNC_SHRINK_BATCH);
75         entry = kmem_cache_alloc(mb2_entry_cache, mask);
76         if (!entry)
77                 return -ENOMEM;
78 @@ -223,12 +244,9 @@ static unsigned long mb2_cache_count(struct shrinker *shrink,
79  }
81  /* Shrink number of entries in cache */
82 -static unsigned long mb2_cache_scan(struct shrinker *shrink,
83 -                                   struct shrink_control *sc)
84 +static unsigned long mb2_cache_shrink(struct mb2_cache *cache,
85 +                                     unsigned int nr_to_scan)
86  {
87 -       int nr_to_scan = sc->nr_to_scan;
88 -       struct mb2_cache *cache = container_of(shrink, struct mb2_cache,
89 -                                             c_shrink);
90         struct mb2_cache_entry *entry;
91         struct hlist_bl_head *head;
92         unsigned int shrunk = 0;
93 @@ -261,6 +279,25 @@ static unsigned long mb2_cache_scan(struct shrinker *shrink,
94         return shrunk;
95  }
97 +static unsigned long mb2_cache_scan(struct shrinker *shrink,
98 +                                   struct shrink_control *sc)
100 +       int nr_to_scan = sc->nr_to_scan;
101 +       struct mb2_cache *cache = container_of(shrink, struct mb2_cache,
102 +                                             c_shrink);
103 +       return mb2_cache_shrink(cache, nr_to_scan);
106 +/* We shrink 1/X of the cache when we have too many entries in it */
107 +#define SHRINK_DIVISOR 16
109 +static void mb2_cache_shrink_worker(struct work_struct *work)
111 +       struct mb2_cache *cache = container_of(work, struct mb2_cache,
112 +                                              c_shrink_work);
113 +       mb2_cache_shrink(cache, cache->c_max_entries / SHRINK_DIVISOR);
116  /*
117   * mb2_cache_create - create cache
118   * @bucket_bits: log2 of the hash table size
119 @@ -280,6 +317,7 @@ struct mb2_cache *mb2_cache_create(int bucket_bits)
120         if (!cache)
121                 goto err_out;
122         cache->c_bucket_bits = bucket_bits;
123 +       cache->c_max_entries = bucket_count << 4;
124         INIT_LIST_HEAD(&cache->c_lru_list);
125         spin_lock_init(&cache->c_lru_list_lock);
126         cache->c_hash = kmalloc(bucket_count * sizeof(struct hlist_bl_head),
127 @@ -296,6 +334,8 @@ struct mb2_cache *mb2_cache_create(int bucket_bits)
128         cache->c_shrink.seeks = DEFAULT_SEEKS;
129         register_shrinker(&cache->c_shrink);
131 +       INIT_WORK(&cache->c_shrink_work, mb2_cache_shrink_worker);
133         return cache;
135  err_out:
136 -- 
137 2.6.2