]>
Commit | Line | Data |
---|---|---|
b411b363 PR |
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 { \ | |
4738fa16 LE |
47 | clear_bit_unlock(__LC_PARANOIA, &lc->flags); \ |
48 | return x ; } while (0) | |
b411b363 PR |
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 | ||
46a15bc3 LE |
58 | |
59 | /* We need to atomically | |
60 | * - try to grab the lock (set LC_LOCKED) | |
61 | * - only if there is no pending transaction | |
62 | * (neither LC_DIRTY nor LC_STARVING is set) | |
63 | * Because of PARANOIA_ENTRY() above abusing lc->flags as well, | |
64 | * it is not sufficient to just say | |
65 | * return 0 == cmpxchg(&lc->flags, 0, LC_LOCKED); | |
66 | */ | |
67 | int lc_try_lock(struct lru_cache *lc) | |
68 | { | |
69 | unsigned long val; | |
70 | do { | |
71 | val = cmpxchg(&lc->flags, 0, LC_LOCKED); | |
72 | } while (unlikely (val == LC_PARANOIA)); | |
73 | /* Spin until no-one is inside a PARANOIA_ENTRY()/RETURN() section. */ | |
74 | return 0 == val; | |
75 | #if 0 | |
76 | /* Alternative approach, spin in case someone enters or leaves a | |
77 | * PARANOIA_ENTRY()/RETURN() section. */ | |
78 | unsigned long old, new, val; | |
79 | do { | |
80 | old = lc->flags & LC_PARANOIA; | |
81 | new = old | LC_LOCKED; | |
82 | val = cmpxchg(&lc->flags, old, new); | |
83 | } while (unlikely (val == (old ^ LC_PARANOIA))); | |
84 | return old == val; | |
85 | #endif | |
86 | } | |
87 | ||
b411b363 PR |
88 | /** |
89 | * lc_create - prepares to track objects in an active set | |
90 | * @name: descriptive name only used in lc_seq_printf_stats and lc_seq_dump_details | |
46a15bc3 | 91 | * @max_pending_changes: maximum changes to accumulate until a transaction is required |
b411b363 PR |
92 | * @e_count: number of elements allowed to be active simultaneously |
93 | * @e_size: size of the tracked objects | |
94 | * @e_off: offset to the &struct lc_element member in a tracked object | |
95 | * | |
96 | * Returns a pointer to a newly initialized struct lru_cache on success, | |
97 | * or NULL on (allocation) failure. | |
98 | */ | |
99 | struct lru_cache *lc_create(const char *name, struct kmem_cache *cache, | |
46a15bc3 | 100 | unsigned max_pending_changes, |
b411b363 PR |
101 | unsigned e_count, size_t e_size, size_t e_off) |
102 | { | |
103 | struct hlist_head *slot = NULL; | |
104 | struct lc_element **element = NULL; | |
105 | struct lru_cache *lc; | |
106 | struct lc_element *e; | |
107 | unsigned cache_obj_size = kmem_cache_size(cache); | |
108 | unsigned i; | |
109 | ||
110 | WARN_ON(cache_obj_size < e_size); | |
111 | if (cache_obj_size < e_size) | |
112 | return NULL; | |
113 | ||
114 | /* e_count too big; would probably fail the allocation below anyways. | |
115 | * for typical use cases, e_count should be few thousand at most. */ | |
116 | if (e_count > LC_MAX_ACTIVE) | |
117 | return NULL; | |
118 | ||
a08aa355 | 119 | slot = kcalloc(e_count, sizeof(struct hlist_head), GFP_KERNEL); |
b411b363 PR |
120 | if (!slot) |
121 | goto out_fail; | |
122 | element = kzalloc(e_count * sizeof(struct lc_element *), GFP_KERNEL); | |
123 | if (!element) | |
124 | goto out_fail; | |
125 | ||
126 | lc = kzalloc(sizeof(*lc), GFP_KERNEL); | |
127 | if (!lc) | |
128 | goto out_fail; | |
129 | ||
130 | INIT_LIST_HEAD(&lc->in_use); | |
131 | INIT_LIST_HEAD(&lc->lru); | |
132 | INIT_LIST_HEAD(&lc->free); | |
46a15bc3 | 133 | INIT_LIST_HEAD(&lc->to_be_changed); |
b411b363 PR |
134 | |
135 | lc->name = name; | |
136 | lc->element_size = e_size; | |
137 | lc->element_off = e_off; | |
138 | lc->nr_elements = e_count; | |
46a15bc3 | 139 | lc->max_pending_changes = max_pending_changes; |
b411b363 PR |
140 | lc->lc_cache = cache; |
141 | lc->lc_element = element; | |
142 | lc->lc_slot = slot; | |
143 | ||
144 | /* preallocate all objects */ | |
145 | for (i = 0; i < e_count; i++) { | |
146 | void *p = kmem_cache_alloc(cache, GFP_KERNEL); | |
147 | if (!p) | |
148 | break; | |
149 | memset(p, 0, lc->element_size); | |
150 | e = p + e_off; | |
151 | e->lc_index = i; | |
152 | e->lc_number = LC_FREE; | |
46a15bc3 | 153 | e->lc_new_number = LC_FREE; |
b411b363 PR |
154 | list_add(&e->list, &lc->free); |
155 | element[i] = e; | |
156 | } | |
157 | if (i == e_count) | |
158 | return lc; | |
159 | ||
160 | /* else: could not allocate all elements, give up */ | |
161 | for (i--; i; i--) { | |
162 | void *p = element[i]; | |
163 | kmem_cache_free(cache, p - e_off); | |
164 | } | |
165 | kfree(lc); | |
166 | out_fail: | |
167 | kfree(element); | |
168 | kfree(slot); | |
169 | return NULL; | |
170 | } | |
171 | ||
8ce953aa | 172 | static void lc_free_by_index(struct lru_cache *lc, unsigned i) |
b411b363 PR |
173 | { |
174 | void *p = lc->lc_element[i]; | |
175 | WARN_ON(!p); | |
176 | if (p) { | |
177 | p -= lc->element_off; | |
178 | kmem_cache_free(lc->lc_cache, p); | |
179 | } | |
180 | } | |
181 | ||
182 | /** | |
183 | * lc_destroy - frees memory allocated by lc_create() | |
184 | * @lc: the lru cache to destroy | |
185 | */ | |
186 | void lc_destroy(struct lru_cache *lc) | |
187 | { | |
188 | unsigned i; | |
189 | if (!lc) | |
190 | return; | |
191 | for (i = 0; i < lc->nr_elements; i++) | |
192 | lc_free_by_index(lc, i); | |
193 | kfree(lc->lc_element); | |
194 | kfree(lc->lc_slot); | |
195 | kfree(lc); | |
196 | } | |
197 | ||
198 | /** | |
199 | * lc_reset - does a full reset for @lc and the hash table slots. | |
200 | * @lc: the lru cache to operate on | |
201 | * | |
202 | * It is roughly the equivalent of re-allocating a fresh lru_cache object, | |
203 | * basically a short cut to lc_destroy(lc); lc = lc_create(...); | |
204 | */ | |
205 | void lc_reset(struct lru_cache *lc) | |
206 | { | |
207 | unsigned i; | |
208 | ||
209 | INIT_LIST_HEAD(&lc->in_use); | |
210 | INIT_LIST_HEAD(&lc->lru); | |
211 | INIT_LIST_HEAD(&lc->free); | |
46a15bc3 | 212 | INIT_LIST_HEAD(&lc->to_be_changed); |
b411b363 PR |
213 | lc->used = 0; |
214 | lc->hits = 0; | |
215 | lc->misses = 0; | |
216 | lc->starving = 0; | |
46a15bc3 | 217 | lc->locked = 0; |
b411b363 | 218 | lc->changed = 0; |
46a15bc3 | 219 | lc->pending_changes = 0; |
b411b363 | 220 | lc->flags = 0; |
b411b363 PR |
221 | memset(lc->lc_slot, 0, sizeof(struct hlist_head) * lc->nr_elements); |
222 | ||
223 | for (i = 0; i < lc->nr_elements; i++) { | |
224 | struct lc_element *e = lc->lc_element[i]; | |
225 | void *p = e; | |
226 | p -= lc->element_off; | |
227 | memset(p, 0, lc->element_size); | |
228 | /* re-init it */ | |
229 | e->lc_index = i; | |
230 | e->lc_number = LC_FREE; | |
46a15bc3 | 231 | e->lc_new_number = LC_FREE; |
b411b363 PR |
232 | list_add(&e->list, &lc->free); |
233 | } | |
234 | } | |
235 | ||
236 | /** | |
237 | * lc_seq_printf_stats - print stats about @lc into @seq | |
238 | * @seq: the seq_file to print into | |
239 | * @lc: the lru cache to print statistics of | |
240 | */ | |
241 | size_t lc_seq_printf_stats(struct seq_file *seq, struct lru_cache *lc) | |
242 | { | |
243 | /* NOTE: | |
244 | * total calls to lc_get are | |
245 | * (starving + hits + misses) | |
46a15bc3 | 246 | * misses include "locked" count (update from an other thread in |
b411b363 PR |
247 | * progress) and "changed", when this in fact lead to an successful |
248 | * update of the cache. | |
249 | */ | |
d50f8f8d JP |
250 | seq_printf(seq, "\t%s: used:%u/%u hits:%lu misses:%lu starving:%lu locked:%lu changed:%lu\n", |
251 | lc->name, lc->used, lc->nr_elements, | |
252 | lc->hits, lc->misses, lc->starving, lc->locked, lc->changed); | |
253 | ||
254 | return 0; | |
b411b363 PR |
255 | } |
256 | ||
257 | static struct hlist_head *lc_hash_slot(struct lru_cache *lc, unsigned int enr) | |
258 | { | |
259 | return lc->lc_slot + (enr % lc->nr_elements); | |
260 | } | |
261 | ||
262 | ||
46a15bc3 LE |
263 | static struct lc_element *__lc_find(struct lru_cache *lc, unsigned int enr, |
264 | bool include_changing) | |
b411b363 | 265 | { |
b411b363 PR |
266 | struct lc_element *e; |
267 | ||
268 | BUG_ON(!lc); | |
269 | BUG_ON(!lc->nr_elements); | |
b67bfe0d | 270 | hlist_for_each_entry(e, lc_hash_slot(lc, enr), colision) { |
46a15bc3 LE |
271 | /* "about to be changed" elements, pending transaction commit, |
272 | * are hashed by their "new number". "Normal" elements have | |
273 | * lc_number == lc_new_number. */ | |
274 | if (e->lc_new_number != enr) | |
275 | continue; | |
276 | if (e->lc_new_number == e->lc_number || include_changing) | |
b411b363 | 277 | return e; |
46a15bc3 | 278 | break; |
b411b363 PR |
279 | } |
280 | return NULL; | |
281 | } | |
282 | ||
46a15bc3 LE |
283 | /** |
284 | * lc_find - find element by label, if present in the hash table | |
285 | * @lc: The lru_cache object | |
286 | * @enr: element number | |
287 | * | |
288 | * Returns the pointer to an element, if the element with the requested | |
289 | * "label" or element number is present in the hash table, | |
290 | * or NULL if not found. Does not change the refcnt. | |
291 | * Ignores elements that are "about to be used", i.e. not yet in the active | |
292 | * set, but still pending transaction commit. | |
293 | */ | |
294 | struct lc_element *lc_find(struct lru_cache *lc, unsigned int enr) | |
b411b363 | 295 | { |
46a15bc3 LE |
296 | return __lc_find(lc, enr, 0); |
297 | } | |
b411b363 | 298 | |
46a15bc3 LE |
299 | /** |
300 | * lc_is_used - find element by label | |
301 | * @lc: The lru_cache object | |
302 | * @enr: element number | |
303 | * | |
304 | * Returns true, if the element with the requested "label" or element number is | |
305 | * present in the hash table, and is used (refcnt > 0). | |
306 | * Also finds elements that are not _currently_ used but only "about to be | |
307 | * used", i.e. on the "to_be_changed" list, pending transaction commit. | |
308 | */ | |
309 | bool lc_is_used(struct lru_cache *lc, unsigned int enr) | |
310 | { | |
311 | struct lc_element *e = __lc_find(lc, enr, 1); | |
312 | return e && e->refcnt; | |
b411b363 PR |
313 | } |
314 | ||
315 | /** | |
316 | * lc_del - removes an element from the cache | |
317 | * @lc: The lru_cache object | |
318 | * @e: The element to remove | |
319 | * | |
320 | * @e must be unused (refcnt == 0). Moves @e from "lru" to "free" list, | |
321 | * sets @e->enr to %LC_FREE. | |
322 | */ | |
323 | void lc_del(struct lru_cache *lc, struct lc_element *e) | |
324 | { | |
325 | PARANOIA_ENTRY(); | |
326 | PARANOIA_LC_ELEMENT(lc, e); | |
327 | BUG_ON(e->refcnt); | |
328 | ||
46a15bc3 | 329 | e->lc_number = e->lc_new_number = LC_FREE; |
b411b363 PR |
330 | hlist_del_init(&e->colision); |
331 | list_move(&e->list, &lc->free); | |
332 | RETURN(); | |
333 | } | |
334 | ||
46a15bc3 | 335 | static struct lc_element *lc_prepare_for_change(struct lru_cache *lc, unsigned new_number) |
b411b363 PR |
336 | { |
337 | struct list_head *n; | |
46a15bc3 LE |
338 | struct lc_element *e; |
339 | ||
340 | if (!list_empty(&lc->free)) | |
341 | n = lc->free.next; | |
342 | else if (!list_empty(&lc->lru)) | |
343 | n = lc->lru.prev; | |
344 | else | |
345 | return NULL; | |
346 | ||
347 | e = list_entry(n, struct lc_element, list); | |
348 | PARANOIA_LC_ELEMENT(lc, e); | |
b411b363 | 349 | |
46a15bc3 LE |
350 | e->lc_new_number = new_number; |
351 | if (!hlist_unhashed(&e->colision)) | |
352 | __hlist_del(&e->colision); | |
353 | hlist_add_head(&e->colision, lc_hash_slot(lc, new_number)); | |
354 | list_move(&e->list, &lc->to_be_changed); | |
b411b363 | 355 | |
46a15bc3 | 356 | return e; |
b411b363 PR |
357 | } |
358 | ||
359 | static int lc_unused_element_available(struct lru_cache *lc) | |
360 | { | |
361 | if (!list_empty(&lc->free)) | |
362 | return 1; /* something on the free list */ | |
363 | if (!list_empty(&lc->lru)) | |
364 | return 1; /* something to evict */ | |
365 | ||
366 | return 0; | |
367 | } | |
368 | ||
cbe5e610 LE |
369 | /* used as internal flags to __lc_get */ |
370 | enum { | |
371 | LC_GET_MAY_CHANGE = 1, | |
372 | LC_GET_MAY_USE_UNCOMMITTED = 2, | |
373 | }; | |
374 | ||
375 | static struct lc_element *__lc_get(struct lru_cache *lc, unsigned int enr, unsigned int flags) | |
b411b363 PR |
376 | { |
377 | struct lc_element *e; | |
378 | ||
379 | PARANOIA_ENTRY(); | |
380 | if (lc->flags & LC_STARVING) { | |
381 | ++lc->starving; | |
382 | RETURN(NULL); | |
383 | } | |
384 | ||
46a15bc3 LE |
385 | e = __lc_find(lc, enr, 1); |
386 | /* if lc_new_number != lc_number, | |
387 | * this enr is currently being pulled in already, | |
388 | * and will be available once the pending transaction | |
389 | * has been committed. */ | |
cbe5e610 LE |
390 | if (e) { |
391 | if (e->lc_new_number != e->lc_number) { | |
392 | /* It has been found above, but on the "to_be_changed" | |
393 | * list, not yet committed. Don't pull it in twice, | |
394 | * wait for the transaction, then try again... | |
395 | */ | |
396 | if (!(flags & LC_GET_MAY_USE_UNCOMMITTED)) | |
397 | RETURN(NULL); | |
398 | /* ... unless the caller is aware of the implications, | |
399 | * probably preparing a cumulative transaction. */ | |
400 | ++e->refcnt; | |
401 | ++lc->hits; | |
402 | RETURN(e); | |
403 | } | |
404 | /* else: lc_new_number == lc_number; a real hit. */ | |
b411b363 PR |
405 | ++lc->hits; |
406 | if (e->refcnt++ == 0) | |
407 | lc->used++; | |
408 | list_move(&e->list, &lc->in_use); /* Not evictable... */ | |
409 | RETURN(e); | |
410 | } | |
cbe5e610 | 411 | /* e == NULL */ |
b411b363 PR |
412 | |
413 | ++lc->misses; | |
cbe5e610 | 414 | if (!(flags & LC_GET_MAY_CHANGE)) |
46a15bc3 LE |
415 | RETURN(NULL); |
416 | ||
417 | /* To avoid races with lc_try_lock(), first, mark us dirty | |
418 | * (using test_and_set_bit, as it implies memory barriers), ... */ | |
419 | test_and_set_bit(__LC_DIRTY, &lc->flags); | |
420 | ||
421 | /* ... only then check if it is locked anyways. If lc_unlock clears | |
422 | * the dirty bit again, that's not a problem, we will come here again. | |
423 | */ | |
424 | if (test_bit(__LC_LOCKED, &lc->flags)) { | |
425 | ++lc->locked; | |
426 | RETURN(NULL); | |
427 | } | |
428 | ||
b411b363 PR |
429 | /* In case there is nothing available and we can not kick out |
430 | * the LRU element, we have to wait ... | |
431 | */ | |
432 | if (!lc_unused_element_available(lc)) { | |
433 | __set_bit(__LC_STARVING, &lc->flags); | |
434 | RETURN(NULL); | |
435 | } | |
436 | ||
46a15bc3 LE |
437 | /* It was not present in the active set. We are going to recycle an |
438 | * unused (or even "free") element, but we won't accumulate more than | |
439 | * max_pending_changes changes. */ | |
440 | if (lc->pending_changes >= lc->max_pending_changes) | |
b411b363 | 441 | RETURN(NULL); |
b411b363 | 442 | |
46a15bc3 | 443 | e = lc_prepare_for_change(lc, enr); |
b411b363 PR |
444 | BUG_ON(!e); |
445 | ||
446 | clear_bit(__LC_STARVING, &lc->flags); | |
447 | BUG_ON(++e->refcnt != 1); | |
448 | lc->used++; | |
46a15bc3 | 449 | lc->pending_changes++; |
b411b363 PR |
450 | |
451 | RETURN(e); | |
452 | } | |
453 | ||
a9efc748 LE |
454 | /** |
455 | * lc_get - get element by label, maybe change the active set | |
456 | * @lc: the lru cache to operate on | |
457 | * @enr: the label to look up | |
458 | * | |
459 | * Finds an element in the cache, increases its usage count, | |
460 | * "touches" and returns it. | |
461 | * | |
462 | * In case the requested number is not present, it needs to be added to the | |
463 | * cache. Therefore it is possible that an other element becomes evicted from | |
464 | * the cache. In either case, the user is notified so he is able to e.g. keep | |
465 | * a persistent log of the cache changes, and therefore the objects in use. | |
466 | * | |
467 | * Return values: | |
468 | * NULL | |
469 | * The cache was marked %LC_STARVING, | |
470 | * or the requested label was not in the active set | |
471 | * and a changing transaction is still pending (@lc was marked %LC_DIRTY). | |
472 | * Or no unused or free element could be recycled (@lc will be marked as | |
473 | * %LC_STARVING, blocking further lc_get() operations). | |
474 | * | |
475 | * pointer to the element with the REQUESTED element number. | |
476 | * In this case, it can be used right away | |
477 | * | |
478 | * pointer to an UNUSED element with some different element number, | |
479 | * where that different number may also be %LC_FREE. | |
480 | * | |
46a15bc3 LE |
481 | * In this case, the cache is marked %LC_DIRTY, |
482 | * so lc_try_lock() will no longer succeed. | |
483 | * The returned element pointer is moved to the "to_be_changed" list, | |
484 | * and registered with the new element number on the hash collision chains, | |
485 | * so it is possible to pick it up from lc_is_used(). | |
486 | * Up to "max_pending_changes" (see lc_create()) can be accumulated. | |
487 | * The user now should do whatever housekeeping is necessary, | |
488 | * typically serialize on lc_try_lock_for_transaction(), then call | |
489 | * lc_committed(lc) and lc_unlock(), to finish the change. | |
a9efc748 LE |
490 | * |
491 | * NOTE: The user needs to check the lc_number on EACH use, so he recognizes | |
492 | * any cache set change. | |
b411b363 | 493 | */ |
a9efc748 | 494 | struct lc_element *lc_get(struct lru_cache *lc, unsigned int enr) |
b411b363 | 495 | { |
cbe5e610 LE |
496 | return __lc_get(lc, enr, LC_GET_MAY_CHANGE); |
497 | } | |
498 | ||
499 | /** | |
500 | * lc_get_cumulative - like lc_get; also finds to-be-changed elements | |
501 | * @lc: the lru cache to operate on | |
502 | * @enr: the label to look up | |
503 | * | |
504 | * Unlike lc_get this also returns the element for @enr, if it is belonging to | |
505 | * a pending transaction, so the return values are like for lc_get(), | |
506 | * plus: | |
507 | * | |
508 | * pointer to an element already on the "to_be_changed" list. | |
509 | * In this case, the cache was already marked %LC_DIRTY. | |
510 | * | |
511 | * Caller needs to make sure that the pending transaction is completed, | |
512 | * before proceeding to actually use this element. | |
513 | */ | |
514 | struct lc_element *lc_get_cumulative(struct lru_cache *lc, unsigned int enr) | |
515 | { | |
516 | return __lc_get(lc, enr, LC_GET_MAY_CHANGE|LC_GET_MAY_USE_UNCOMMITTED); | |
a9efc748 | 517 | } |
b411b363 | 518 | |
a9efc748 LE |
519 | /** |
520 | * lc_try_get - get element by label, if present; do not change the active set | |
521 | * @lc: the lru cache to operate on | |
522 | * @enr: the label to look up | |
523 | * | |
524 | * Finds an element in the cache, increases its usage count, | |
525 | * "touches" and returns it. | |
526 | * | |
527 | * Return values: | |
528 | * NULL | |
529 | * The cache was marked %LC_STARVING, | |
530 | * or the requested label was not in the active set | |
531 | * | |
532 | * pointer to the element with the REQUESTED element number. | |
533 | * In this case, it can be used right away | |
534 | */ | |
535 | struct lc_element *lc_try_get(struct lru_cache *lc, unsigned int enr) | |
536 | { | |
537 | return __lc_get(lc, enr, 0); | |
b411b363 PR |
538 | } |
539 | ||
540 | /** | |
46a15bc3 | 541 | * lc_committed - tell @lc that pending changes have been recorded |
b411b363 | 542 | * @lc: the lru cache to operate on |
46a15bc3 LE |
543 | * |
544 | * User is expected to serialize on explicit lc_try_lock_for_transaction() | |
545 | * before the transaction is started, and later needs to lc_unlock() explicitly | |
546 | * as well. | |
b411b363 | 547 | */ |
46a15bc3 | 548 | void lc_committed(struct lru_cache *lc) |
b411b363 | 549 | { |
46a15bc3 LE |
550 | struct lc_element *e, *tmp; |
551 | ||
b411b363 | 552 | PARANOIA_ENTRY(); |
46a15bc3 LE |
553 | list_for_each_entry_safe(e, tmp, &lc->to_be_changed, list) { |
554 | /* count number of changes, not number of transactions */ | |
555 | ++lc->changed; | |
556 | e->lc_number = e->lc_new_number; | |
557 | list_move(&e->list, &lc->in_use); | |
558 | } | |
559 | lc->pending_changes = 0; | |
b411b363 PR |
560 | RETURN(); |
561 | } | |
562 | ||
563 | ||
564 | /** | |
565 | * lc_put - give up refcnt of @e | |
566 | * @lc: the lru cache to operate on | |
567 | * @e: the element to put | |
568 | * | |
569 | * If refcnt reaches zero, the element is moved to the lru list, | |
570 | * and a %LC_STARVING (if set) is cleared. | |
571 | * Returns the new (post-decrement) refcnt. | |
572 | */ | |
573 | unsigned int lc_put(struct lru_cache *lc, struct lc_element *e) | |
574 | { | |
575 | PARANOIA_ENTRY(); | |
576 | PARANOIA_LC_ELEMENT(lc, e); | |
577 | BUG_ON(e->refcnt == 0); | |
46a15bc3 | 578 | BUG_ON(e->lc_number != e->lc_new_number); |
b411b363 PR |
579 | if (--e->refcnt == 0) { |
580 | /* move it to the front of LRU. */ | |
581 | list_move(&e->list, &lc->lru); | |
582 | lc->used--; | |
4738fa16 | 583 | clear_bit_unlock(__LC_STARVING, &lc->flags); |
b411b363 PR |
584 | } |
585 | RETURN(e->refcnt); | |
586 | } | |
587 | ||
588 | /** | |
589 | * lc_element_by_index | |
590 | * @lc: the lru cache to operate on | |
591 | * @i: the index of the element to return | |
592 | */ | |
593 | struct lc_element *lc_element_by_index(struct lru_cache *lc, unsigned i) | |
594 | { | |
595 | BUG_ON(i >= lc->nr_elements); | |
596 | BUG_ON(lc->lc_element[i] == NULL); | |
597 | BUG_ON(lc->lc_element[i]->lc_index != i); | |
598 | return lc->lc_element[i]; | |
599 | } | |
600 | ||
601 | /** | |
602 | * lc_index_of | |
603 | * @lc: the lru cache to operate on | |
604 | * @e: the element to query for its index position in lc->element | |
605 | */ | |
606 | unsigned int lc_index_of(struct lru_cache *lc, struct lc_element *e) | |
607 | { | |
608 | PARANOIA_LC_ELEMENT(lc, e); | |
609 | return e->lc_index; | |
610 | } | |
611 | ||
612 | /** | |
613 | * lc_set - associate index with label | |
614 | * @lc: the lru cache to operate on | |
615 | * @enr: the label to set | |
616 | * @index: the element index to associate label with. | |
617 | * | |
618 | * Used to initialize the active set to some previously recorded state. | |
619 | */ | |
620 | void lc_set(struct lru_cache *lc, unsigned int enr, int index) | |
621 | { | |
622 | struct lc_element *e; | |
46a15bc3 | 623 | struct list_head *lh; |
b411b363 PR |
624 | |
625 | if (index < 0 || index >= lc->nr_elements) | |
626 | return; | |
627 | ||
628 | e = lc_element_by_index(lc, index); | |
46a15bc3 LE |
629 | BUG_ON(e->lc_number != e->lc_new_number); |
630 | BUG_ON(e->refcnt != 0); | |
b411b363 | 631 | |
46a15bc3 | 632 | e->lc_number = e->lc_new_number = enr; |
b411b363 | 633 | hlist_del_init(&e->colision); |
46a15bc3 LE |
634 | if (enr == LC_FREE) |
635 | lh = &lc->free; | |
636 | else { | |
637 | hlist_add_head(&e->colision, lc_hash_slot(lc, enr)); | |
638 | lh = &lc->lru; | |
639 | } | |
640 | list_move(&e->list, lh); | |
b411b363 PR |
641 | } |
642 | ||
643 | /** | |
644 | * lc_dump - Dump a complete LRU cache to seq in textual form. | |
645 | * @lc: the lru cache to operate on | |
646 | * @seq: the &struct seq_file pointer to seq_printf into | |
54e6fc38 | 647 | * @utext: user supplied additional "heading" or other info |
b411b363 | 648 | * @detail: function pointer the user may provide to dump further details |
54e6fc38 LE |
649 | * of the object the lc_element is embedded in. May be NULL. |
650 | * Note: a leading space ' ' and trailing newline '\n' is implied. | |
b411b363 PR |
651 | */ |
652 | void lc_seq_dump_details(struct seq_file *seq, struct lru_cache *lc, char *utext, | |
653 | void (*detail) (struct seq_file *, struct lc_element *)) | |
654 | { | |
655 | unsigned int nr_elements = lc->nr_elements; | |
656 | struct lc_element *e; | |
657 | int i; | |
658 | ||
54e6fc38 | 659 | seq_printf(seq, "\tnn: lc_number (new nr) refcnt %s\n ", utext); |
b411b363 PR |
660 | for (i = 0; i < nr_elements; i++) { |
661 | e = lc_element_by_index(lc, i); | |
54e6fc38 LE |
662 | if (e->lc_number != e->lc_new_number) |
663 | seq_printf(seq, "\t%5d: %6d %8d %6d ", | |
664 | i, e->lc_number, e->lc_new_number, e->refcnt); | |
665 | else | |
666 | seq_printf(seq, "\t%5d: %6d %-8s %6d ", | |
667 | i, e->lc_number, "-\"-", e->refcnt); | |
668 | if (detail) | |
b411b363 | 669 | detail(seq, e); |
54e6fc38 | 670 | seq_putc(seq, '\n'); |
b411b363 PR |
671 | } |
672 | } | |
673 | ||
674 | EXPORT_SYMBOL(lc_create); | |
675 | EXPORT_SYMBOL(lc_reset); | |
676 | EXPORT_SYMBOL(lc_destroy); | |
677 | EXPORT_SYMBOL(lc_set); | |
678 | EXPORT_SYMBOL(lc_del); | |
679 | EXPORT_SYMBOL(lc_try_get); | |
680 | EXPORT_SYMBOL(lc_find); | |
681 | EXPORT_SYMBOL(lc_get); | |
682 | EXPORT_SYMBOL(lc_put); | |
46a15bc3 | 683 | EXPORT_SYMBOL(lc_committed); |
b411b363 PR |
684 | EXPORT_SYMBOL(lc_element_by_index); |
685 | EXPORT_SYMBOL(lc_index_of); | |
686 | EXPORT_SYMBOL(lc_seq_printf_stats); | |
687 | EXPORT_SYMBOL(lc_seq_dump_details); | |
46a15bc3 LE |
688 | EXPORT_SYMBOL(lc_try_lock); |
689 | EXPORT_SYMBOL(lc_is_used); | |
cbe5e610 | 690 | EXPORT_SYMBOL(lc_get_cumulative); |