Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
lru_cache.c
Go to the documentation of this file.
1 /*
2  lru_cache.c
3 
4  This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5 
6  Copyright (C) 2003-2008, LINBIT Information Technologies GmbH.
7  Copyright (C) 2003-2008, Philipp Reisner <[email protected]>.
8  Copyright (C) 2003-2008, Lars Ellenberg <[email protected]>.
9 
10  drbd is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2, or (at your option)
13  any later version.
14 
15  drbd is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with drbd; see the file COPYING. If not, write to
22  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 
24  */
25 
26 #include <linux/module.h>
27 #include <linux/bitops.h>
28 #include <linux/slab.h>
29 #include <linux/string.h> /* for memset */
30 #include <linux/seq_file.h> /* for seq_printf */
31 #include <linux/lru_cache.h>
32 
33 MODULE_AUTHOR("Philipp Reisner <[email protected]>, "
34  "Lars Ellenberg <[email protected]>");
35 MODULE_DESCRIPTION("lru_cache - Track sets of hot objects");
36 MODULE_LICENSE("GPL");
37 
38 /* this is developers aid only.
39  * it catches concurrent access (lack of locking on the users part) */
40 #define PARANOIA_ENTRY() do { \
41  BUG_ON(!lc); \
42  BUG_ON(!lc->nr_elements); \
43  BUG_ON(test_and_set_bit(__LC_PARANOIA, &lc->flags)); \
44 } while (0)
45 
46 #define RETURN(x...) do { \
47  clear_bit(__LC_PARANOIA, &lc->flags); \
48  smp_mb__after_clear_bit(); return x ; } while (0)
49 
50 /* BUG() if e is not one of the elements tracked by lc */
51 #define PARANOIA_LC_ELEMENT(lc, e) do { \
52  struct lru_cache *lc_ = (lc); \
53  struct lc_element *e_ = (e); \
54  unsigned i = e_->lc_index; \
55  BUG_ON(i >= lc_->nr_elements); \
56  BUG_ON(lc_->lc_element[i] != e_); } while (0)
57 
68 struct lru_cache *lc_create(const char *name, struct kmem_cache *cache,
69  unsigned e_count, size_t e_size, size_t e_off)
70 {
71  struct hlist_head *slot = NULL;
72  struct lc_element **element = NULL;
73  struct lru_cache *lc;
74  struct lc_element *e;
75  unsigned cache_obj_size = kmem_cache_size(cache);
76  unsigned i;
77 
78  WARN_ON(cache_obj_size < e_size);
79  if (cache_obj_size < e_size)
80  return NULL;
81 
82  /* e_count too big; would probably fail the allocation below anyways.
83  * for typical use cases, e_count should be few thousand at most. */
84  if (e_count > LC_MAX_ACTIVE)
85  return NULL;
86 
87  slot = kcalloc(e_count, sizeof(struct hlist_head), GFP_KERNEL);
88  if (!slot)
89  goto out_fail;
90  element = kzalloc(e_count * sizeof(struct lc_element *), GFP_KERNEL);
91  if (!element)
92  goto out_fail;
93 
94  lc = kzalloc(sizeof(*lc), GFP_KERNEL);
95  if (!lc)
96  goto out_fail;
97 
98  INIT_LIST_HEAD(&lc->in_use);
99  INIT_LIST_HEAD(&lc->lru);
100  INIT_LIST_HEAD(&lc->free);
101 
102  lc->name = name;
103  lc->element_size = e_size;
104  lc->element_off = e_off;
105  lc->nr_elements = e_count;
106  lc->new_number = LC_FREE;
107  lc->lc_cache = cache;
108  lc->lc_element = element;
109  lc->lc_slot = slot;
110 
111  /* preallocate all objects */
112  for (i = 0; i < e_count; i++) {
113  void *p = kmem_cache_alloc(cache, GFP_KERNEL);
114  if (!p)
115  break;
116  memset(p, 0, lc->element_size);
117  e = p + e_off;
118  e->lc_index = i;
119  e->lc_number = LC_FREE;
120  list_add(&e->list, &lc->free);
121  element[i] = e;
122  }
123  if (i == e_count)
124  return lc;
125 
126  /* else: could not allocate all elements, give up */
127  for (i--; i; i--) {
128  void *p = element[i];
129  kmem_cache_free(cache, p - e_off);
130  }
131  kfree(lc);
132 out_fail:
133  kfree(element);
134  kfree(slot);
135  return NULL;
136 }
137 
138 void lc_free_by_index(struct lru_cache *lc, unsigned i)
139 {
140  void *p = lc->lc_element[i];
141  WARN_ON(!p);
142  if (p) {
143  p -= lc->element_off;
144  kmem_cache_free(lc->lc_cache, p);
145  }
146 }
147 
152 void lc_destroy(struct lru_cache *lc)
153 {
154  unsigned i;
155  if (!lc)
156  return;
157  for (i = 0; i < lc->nr_elements; i++)
158  lc_free_by_index(lc, i);
159  kfree(lc->lc_element);
160  kfree(lc->lc_slot);
161  kfree(lc);
162 }
163 
171 void lc_reset(struct lru_cache *lc)
172 {
173  unsigned i;
174 
175  INIT_LIST_HEAD(&lc->in_use);
176  INIT_LIST_HEAD(&lc->lru);
177  INIT_LIST_HEAD(&lc->free);
178  lc->used = 0;
179  lc->hits = 0;
180  lc->misses = 0;
181  lc->starving = 0;
182  lc->dirty = 0;
183  lc->changed = 0;
184  lc->flags = 0;
185  lc->changing_element = NULL;
186  lc->new_number = LC_FREE;
187  memset(lc->lc_slot, 0, sizeof(struct hlist_head) * lc->nr_elements);
188 
189  for (i = 0; i < lc->nr_elements; i++) {
190  struct lc_element *e = lc->lc_element[i];
191  void *p = e;
192  p -= lc->element_off;
193  memset(p, 0, lc->element_size);
194  /* re-init it */
195  e->lc_index = i;
196  e->lc_number = LC_FREE;
197  list_add(&e->list, &lc->free);
198  }
199 }
200 
206 size_t lc_seq_printf_stats(struct seq_file *seq, struct lru_cache *lc)
207 {
208  /* NOTE:
209  * total calls to lc_get are
210  * (starving + hits + misses)
211  * misses include "dirty" count (update from an other thread in
212  * progress) and "changed", when this in fact lead to an successful
213  * update of the cache.
214  */
215  return seq_printf(seq, "\t%s: used:%u/%u "
216  "hits:%lu misses:%lu starving:%lu dirty:%lu changed:%lu\n",
217  lc->name, lc->used, lc->nr_elements,
218  lc->hits, lc->misses, lc->starving, lc->dirty, lc->changed);
219 }
220 
221 static struct hlist_head *lc_hash_slot(struct lru_cache *lc, unsigned int enr)
222 {
223  return lc->lc_slot + (enr % lc->nr_elements);
224 }
225 
226 
236 struct lc_element *lc_find(struct lru_cache *lc, unsigned int enr)
237 {
238  struct hlist_node *n;
239  struct lc_element *e;
240 
241  BUG_ON(!lc);
242  BUG_ON(!lc->nr_elements);
243  hlist_for_each_entry(e, n, lc_hash_slot(lc, enr), colision) {
244  if (e->lc_number == enr)
245  return e;
246  }
247  return NULL;
248 }
249 
250 /* returned element will be "recycled" immediately */
251 static struct lc_element *lc_evict(struct lru_cache *lc)
252 {
253  struct list_head *n;
254  struct lc_element *e;
255 
256  if (list_empty(&lc->lru))
257  return NULL;
258 
259  n = lc->lru.prev;
260  e = list_entry(n, struct lc_element, list);
261 
262  PARANOIA_LC_ELEMENT(lc, e);
263 
264  list_del(&e->list);
265  hlist_del(&e->colision);
266  return e;
267 }
268 
277 void lc_del(struct lru_cache *lc, struct lc_element *e)
278 {
279  PARANOIA_ENTRY();
280  PARANOIA_LC_ELEMENT(lc, e);
281  BUG_ON(e->refcnt);
282 
283  e->lc_number = LC_FREE;
284  hlist_del_init(&e->colision);
285  list_move(&e->list, &lc->free);
286  RETURN();
287 }
288 
289 static struct lc_element *lc_get_unused_element(struct lru_cache *lc)
290 {
291  struct list_head *n;
292 
293  if (list_empty(&lc->free))
294  return lc_evict(lc);
295 
296  n = lc->free.next;
297  list_del(n);
298  return list_entry(n, struct lc_element, list);
299 }
300 
301 static int lc_unused_element_available(struct lru_cache *lc)
302 {
303  if (!list_empty(&lc->free))
304  return 1; /* something on the free list */
305  if (!list_empty(&lc->lru))
306  return 1; /* something to evict */
307 
308  return 0;
309 }
310 
311 
349 struct lc_element *lc_get(struct lru_cache *lc, unsigned int enr)
350 {
351  struct lc_element *e;
352 
353  PARANOIA_ENTRY();
354  if (lc->flags & LC_STARVING) {
355  ++lc->starving;
356  RETURN(NULL);
357  }
358 
359  e = lc_find(lc, enr);
360  if (e) {
361  ++lc->hits;
362  if (e->refcnt++ == 0)
363  lc->used++;
364  list_move(&e->list, &lc->in_use); /* Not evictable... */
365  RETURN(e);
366  }
367 
368  ++lc->misses;
369 
370  /* In case there is nothing available and we can not kick out
371  * the LRU element, we have to wait ...
372  */
373  if (!lc_unused_element_available(lc)) {
375  RETURN(NULL);
376  }
377 
378  /* it was not present in the active set.
379  * we are going to recycle an unused (or even "free") element.
380  * user may need to commit a transaction to record that change.
381  * we serialize on flags & TF_DIRTY */
382  if (test_and_set_bit(__LC_DIRTY, &lc->flags)) {
383  ++lc->dirty;
384  RETURN(NULL);
385  }
386 
387  e = lc_get_unused_element(lc);
388  BUG_ON(!e);
389 
391  BUG_ON(++e->refcnt != 1);
392  lc->used++;
393 
394  lc->changing_element = e;
395  lc->new_number = enr;
396 
397  RETURN(e);
398 }
399 
400 /* similar to lc_get,
401  * but only gets a new reference on an existing element.
402  * you either get the requested element, or NULL.
403  * will be consolidated into one function.
404  */
405 struct lc_element *lc_try_get(struct lru_cache *lc, unsigned int enr)
406 {
407  struct lc_element *e;
408 
409  PARANOIA_ENTRY();
410  if (lc->flags & LC_STARVING) {
411  ++lc->starving;
412  RETURN(NULL);
413  }
414 
415  e = lc_find(lc, enr);
416  if (e) {
417  ++lc->hits;
418  if (e->refcnt++ == 0)
419  lc->used++;
420  list_move(&e->list, &lc->in_use); /* Not evictable... */
421  }
422  RETURN(e);
423 }
424 
430 void lc_changed(struct lru_cache *lc, struct lc_element *e)
431 {
432  PARANOIA_ENTRY();
433  BUG_ON(e != lc->changing_element);
434  PARANOIA_LC_ELEMENT(lc, e);
435  ++lc->changed;
436  e->lc_number = lc->new_number;
437  list_add(&e->list, &lc->in_use);
438  hlist_add_head(&e->colision, lc_hash_slot(lc, lc->new_number));
439  lc->changing_element = NULL;
440  lc->new_number = LC_FREE;
441  clear_bit(__LC_DIRTY, &lc->flags);
443  RETURN();
444 }
445 
446 
456 unsigned int lc_put(struct lru_cache *lc, struct lc_element *e)
457 {
458  PARANOIA_ENTRY();
459  PARANOIA_LC_ELEMENT(lc, e);
460  BUG_ON(e->refcnt == 0);
461  BUG_ON(e == lc->changing_element);
462  if (--e->refcnt == 0) {
463  /* move it to the front of LRU. */
464  list_move(&e->list, &lc->lru);
465  lc->used--;
468  }
469  RETURN(e->refcnt);
470 }
471 
477 struct lc_element *lc_element_by_index(struct lru_cache *lc, unsigned i)
478 {
479  BUG_ON(i >= lc->nr_elements);
480  BUG_ON(lc->lc_element[i] == NULL);
481  BUG_ON(lc->lc_element[i]->lc_index != i);
482  return lc->lc_element[i];
483 }
484 
490 unsigned int lc_index_of(struct lru_cache *lc, struct lc_element *e)
491 {
492  PARANOIA_LC_ELEMENT(lc, e);
493  return e->lc_index;
494 }
495 
504 void lc_set(struct lru_cache *lc, unsigned int enr, int index)
505 {
506  struct lc_element *e;
507 
508  if (index < 0 || index >= lc->nr_elements)
509  return;
510 
511  e = lc_element_by_index(lc, index);
512  e->lc_number = enr;
513 
514  hlist_del_init(&e->colision);
515  hlist_add_head(&e->colision, lc_hash_slot(lc, enr));
516  list_move(&e->list, e->refcnt ? &lc->in_use : &lc->lru);
517 }
518 
527 void lc_seq_dump_details(struct seq_file *seq, struct lru_cache *lc, char *utext,
528  void (*detail) (struct seq_file *, struct lc_element *))
529 {
530  unsigned int nr_elements = lc->nr_elements;
531  struct lc_element *e;
532  int i;
533 
534  seq_printf(seq, "\tnn: lc_number refcnt %s\n ", utext);
535  for (i = 0; i < nr_elements; i++) {
536  e = lc_element_by_index(lc, i);
537  if (e->lc_number == LC_FREE) {
538  seq_printf(seq, "\t%2d: FREE\n", i);
539  } else {
540  seq_printf(seq, "\t%2d: %4u %4u ", i,
541  e->lc_number, e->refcnt);
542  detail(seq, e);
543  }
544  }
545 }
546