lru_cache.c (a9efc748d679efb39fe7a8a536dde94cee691604) lru_cache.c (46a15bc3ec425b546d140581c28192ab7877ddc4)
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 <philipp.reisner@linbit.com>.
8 Copyright (C) 2003-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.

--- 41 unchanged lines hidden (view full) ---

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
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 <philipp.reisner@linbit.com>.
8 Copyright (C) 2003-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.

--- 41 unchanged lines hidden (view full) ---

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
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 */
67int 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
58/**
59 * lc_create - prepares to track objects in an active set
60 * @name: descriptive name only used in lc_seq_printf_stats and lc_seq_dump_details
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
91 * @max_pending_changes: maximum changes to accumulate until a transaction is required
61 * @e_count: number of elements allowed to be active simultaneously
62 * @e_size: size of the tracked objects
63 * @e_off: offset to the &struct lc_element member in a tracked object
64 *
65 * Returns a pointer to a newly initialized struct lru_cache on success,
66 * or NULL on (allocation) failure.
67 */
68struct lru_cache *lc_create(const char *name, struct kmem_cache *cache,
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 */
99struct lru_cache *lc_create(const char *name, struct kmem_cache *cache,
100 unsigned max_pending_changes,
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;

--- 16 unchanged lines hidden (view full) ---

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 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;

--- 16 unchanged lines hidden (view full) ---

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);
133 INIT_LIST_HEAD(&lc->to_be_changed);
101
102 lc->name = name;
103 lc->element_size = e_size;
104 lc->element_off = e_off;
105 lc->nr_elements = e_count;
134
135 lc->name = name;
136 lc->element_size = e_size;
137 lc->element_off = e_off;
138 lc->nr_elements = e_count;
106 lc->new_number = LC_FREE;
139 lc->max_pending_changes = max_pending_changes;
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;
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;
153 e->lc_new_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--) {

--- 42 unchanged lines hidden (view full) ---

170 */
171void 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);
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--) {

--- 42 unchanged lines hidden (view full) ---

204 */
205void 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);
212 INIT_LIST_HEAD(&lc->to_be_changed);
178 lc->used = 0;
179 lc->hits = 0;
180 lc->misses = 0;
181 lc->starving = 0;
213 lc->used = 0;
214 lc->hits = 0;
215 lc->misses = 0;
216 lc->starving = 0;
182 lc->dirty = 0;
217 lc->locked = 0;
183 lc->changed = 0;
218 lc->changed = 0;
219 lc->pending_changes = 0;
184 lc->flags = 0;
220 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;
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;
231 e->lc_new_number = LC_FREE;
197 list_add(&e->list, &lc->free);
198 }
199}
200
201/**
202 * lc_seq_printf_stats - print stats about @lc into @seq
203 * @seq: the seq_file to print into
204 * @lc: the lru cache to print statistics of
205 */
206size_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)
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 */
241size_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)
211 * misses include "dirty" count (update from an other thread in
246 * misses include "locked" 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 "
247 * progress) and "changed", when this in fact lead to an successful
248 * update of the cache.
249 */
250 return seq_printf(seq, "\t%s: used:%u/%u "
216 "hits:%lu misses:%lu starving:%lu dirty:%lu changed:%lu\n",
251 "hits:%lu misses:%lu starving:%lu locked:%lu changed:%lu\n",
217 lc->name, lc->used, lc->nr_elements,
252 lc->name, lc->used, lc->nr_elements,
218 lc->hits, lc->misses, lc->starving, lc->dirty, lc->changed);
253 lc->hits, lc->misses, lc->starving, lc->locked, lc->changed);
219}
220
221static 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
254}
255
256static struct hlist_head *lc_hash_slot(struct lru_cache *lc, unsigned int enr)
257{
258 return lc->lc_slot + (enr % lc->nr_elements);
259}
260
261
227/**
228 * lc_find - find element by label, if present in the hash table
229 * @lc: The lru_cache object
230 * @enr: element number
231 *
232 * Returns the pointer to an element, if the element with the requested
233 * "label" or element number is present in the hash table,
234 * or NULL if not found. Does not change the refcnt.
235 */
236struct lc_element *lc_find(struct lru_cache *lc, unsigned int enr)
262static struct lc_element *__lc_find(struct lru_cache *lc, unsigned int enr,
263 bool include_changing)
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) {
264{
265 struct hlist_node *n;
266 struct lc_element *e;
267
268 BUG_ON(!lc);
269 BUG_ON(!lc->nr_elements);
270 hlist_for_each_entry(e, n, lc_hash_slot(lc, enr), colision) {
244 if (e->lc_number == enr)
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)
245 return e;
277 return e;
278 break;
246 }
247 return NULL;
248}
249
279 }
280 return NULL;
281}
282
250/* returned element will be "recycled" immediately */
251static struct lc_element *lc_evict(struct lru_cache *lc)
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 */
294struct lc_element *lc_find(struct lru_cache *lc, unsigned int enr)
252{
295{
253 struct list_head *n;
254 struct lc_element *e;
296 return __lc_find(lc, enr, 0);
297}
255
298
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;
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 */
309bool 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;
267}
268
269/**
270 * lc_del - removes an element from the cache
271 * @lc: The lru_cache object
272 * @e: The element to remove
273 *
274 * @e must be unused (refcnt == 0). Moves @e from "lru" to "free" list,
275 * sets @e->enr to %LC_FREE.
276 */
277void 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
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 */
323void 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
283 e->lc_number = LC_FREE;
329 e->lc_number = e->lc_new_number = LC_FREE;
284 hlist_del_init(&e->colision);
285 list_move(&e->list, &lc->free);
286 RETURN();
287}
288
330 hlist_del_init(&e->colision);
331 list_move(&e->list, &lc->free);
332 RETURN();
333}
334
289static struct lc_element *lc_get_unused_element(struct lru_cache *lc)
335static struct lc_element *lc_prepare_for_change(struct lru_cache *lc, unsigned new_number)
290{
291 struct list_head *n;
336{
337 struct list_head *n;
338 struct lc_element *e;
292
339
293 if (list_empty(&lc->free))
294 return lc_evict(lc);
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;
295
346
296 n = lc->free.next;
297 list_del(n);
298 return list_entry(n, struct lc_element, list);
347 e = list_entry(n, struct lc_element, list);
348 PARANOIA_LC_ELEMENT(lc, e);
349
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);
355
356 return e;
299}
300
301static 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 */

--- 6 unchanged lines hidden (view full) ---

313 struct lc_element *e;
314
315 PARANOIA_ENTRY();
316 if (lc->flags & LC_STARVING) {
317 ++lc->starving;
318 RETURN(NULL);
319 }
320
357}
358
359static 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 */

--- 6 unchanged lines hidden (view full) ---

371 struct lc_element *e;
372
373 PARANOIA_ENTRY();
374 if (lc->flags & LC_STARVING) {
375 ++lc->starving;
376 RETURN(NULL);
377 }
378
321 e = lc_find(lc, enr);
322 if (e) {
379 e = __lc_find(lc, enr, 1);
380 /* if lc_new_number != lc_number,
381 * this enr is currently being pulled in already,
382 * and will be available once the pending transaction
383 * has been committed. */
384 if (e && e->lc_new_number == e->lc_number) {
323 ++lc->hits;
324 if (e->refcnt++ == 0)
325 lc->used++;
326 list_move(&e->list, &lc->in_use); /* Not evictable... */
327 RETURN(e);
328 }
329
330 ++lc->misses;
331 if (!may_change)
332 RETURN(NULL);
333
385 ++lc->hits;
386 if (e->refcnt++ == 0)
387 lc->used++;
388 list_move(&e->list, &lc->in_use); /* Not evictable... */
389 RETURN(e);
390 }
391
392 ++lc->misses;
393 if (!may_change)
394 RETURN(NULL);
395
396 /* It has been found above, but on the "to_be_changed" list, not yet
397 * committed. Don't pull it in twice, wait for the transaction, then
398 * try again */
399 if (e)
400 RETURN(NULL);
401
402 /* To avoid races with lc_try_lock(), first, mark us dirty
403 * (using test_and_set_bit, as it implies memory barriers), ... */
404 test_and_set_bit(__LC_DIRTY, &lc->flags);
405
406 /* ... only then check if it is locked anyways. If lc_unlock clears
407 * the dirty bit again, that's not a problem, we will come here again.
408 */
409 if (test_bit(__LC_LOCKED, &lc->flags)) {
410 ++lc->locked;
411 RETURN(NULL);
412 }
413
334 /* In case there is nothing available and we can not kick out
335 * the LRU element, we have to wait ...
336 */
337 if (!lc_unused_element_available(lc)) {
338 __set_bit(__LC_STARVING, &lc->flags);
339 RETURN(NULL);
340 }
341
414 /* In case there is nothing available and we can not kick out
415 * the LRU element, we have to wait ...
416 */
417 if (!lc_unused_element_available(lc)) {
418 __set_bit(__LC_STARVING, &lc->flags);
419 RETURN(NULL);
420 }
421
342 /* it was not present in the active set.
343 * we are going to recycle an unused (or even "free") element.
344 * user may need to commit a transaction to record that change.
345 * we serialize on flags & LC_DIRTY */
346 if (test_and_set_bit(__LC_DIRTY, &lc->flags)) {
347 ++lc->dirty;
422 /* It was not present in the active set. We are going to recycle an
423 * unused (or even "free") element, but we won't accumulate more than
424 * max_pending_changes changes. */
425 if (lc->pending_changes >= lc->max_pending_changes)
348 RETURN(NULL);
426 RETURN(NULL);
349 }
350
427
351 e = lc_get_unused_element(lc);
428 e = lc_prepare_for_change(lc, enr);
352 BUG_ON(!e);
353
354 clear_bit(__LC_STARVING, &lc->flags);
355 BUG_ON(++e->refcnt != 1);
356 lc->used++;
429 BUG_ON(!e);
430
431 clear_bit(__LC_STARVING, &lc->flags);
432 BUG_ON(++e->refcnt != 1);
433 lc->used++;
434 lc->pending_changes++;
357
435
358 lc->changing_element = e;
359 lc->new_number = enr;
360
361 RETURN(e);
362}
363
364/**
365 * lc_get - get element by label, maybe change the active set
366 * @lc: the lru cache to operate on
367 * @enr: the label to look up
368 *

--- 14 unchanged lines hidden (view full) ---

383 * %LC_STARVING, blocking further lc_get() operations).
384 *
385 * pointer to the element with the REQUESTED element number.
386 * In this case, it can be used right away
387 *
388 * pointer to an UNUSED element with some different element number,
389 * where that different number may also be %LC_FREE.
390 *
436 RETURN(e);
437}
438
439/**
440 * lc_get - get element by label, maybe change the active set
441 * @lc: the lru cache to operate on
442 * @enr: the label to look up
443 *

--- 14 unchanged lines hidden (view full) ---

458 * %LC_STARVING, blocking further lc_get() operations).
459 *
460 * pointer to the element with the REQUESTED element number.
461 * In this case, it can be used right away
462 *
463 * pointer to an UNUSED element with some different element number,
464 * where that different number may also be %LC_FREE.
465 *
391 * In this case, the cache is marked %LC_DIRTY (blocking further changes),
392 * and the returned element pointer is removed from the lru list and
393 * hash collision chains. The user now should do whatever housekeeping
394 * is necessary.
395 * Then he must call lc_changed(lc,element_pointer), to finish
396 * the change.
466 * In this case, the cache is marked %LC_DIRTY,
467 * so lc_try_lock() will no longer succeed.
468 * The returned element pointer is moved to the "to_be_changed" list,
469 * and registered with the new element number on the hash collision chains,
470 * so it is possible to pick it up from lc_is_used().
471 * Up to "max_pending_changes" (see lc_create()) can be accumulated.
472 * The user now should do whatever housekeeping is necessary,
473 * typically serialize on lc_try_lock_for_transaction(), then call
474 * lc_committed(lc) and lc_unlock(), to finish the change.
397 *
398 * NOTE: The user needs to check the lc_number on EACH use, so he recognizes
399 * any cache set change.
400 */
401struct lc_element *lc_get(struct lru_cache *lc, unsigned int enr)
402{
403 return __lc_get(lc, enr, 1);
404}

--- 15 unchanged lines hidden (view full) ---

420 * In this case, it can be used right away
421 */
422struct lc_element *lc_try_get(struct lru_cache *lc, unsigned int enr)
423{
424 return __lc_get(lc, enr, 0);
425}
426
427/**
475 *
476 * NOTE: The user needs to check the lc_number on EACH use, so he recognizes
477 * any cache set change.
478 */
479struct lc_element *lc_get(struct lru_cache *lc, unsigned int enr)
480{
481 return __lc_get(lc, enr, 1);
482}

--- 15 unchanged lines hidden (view full) ---

498 * In this case, it can be used right away
499 */
500struct lc_element *lc_try_get(struct lru_cache *lc, unsigned int enr)
501{
502 return __lc_get(lc, enr, 0);
503}
504
505/**
428 * lc_changed - tell @lc that the change has been recorded
506 * lc_committed - tell @lc that pending changes have been recorded
429 * @lc: the lru cache to operate on
507 * @lc: the lru cache to operate on
430 * @e: the element pending label change
508 *
509 * User is expected to serialize on explicit lc_try_lock_for_transaction()
510 * before the transaction is started, and later needs to lc_unlock() explicitly
511 * as well.
431 */
512 */
432void lc_changed(struct lru_cache *lc, struct lc_element *e)
513void lc_committed(struct lru_cache *lc)
433{
514{
515 struct lc_element *e, *tmp;
516
434 PARANOIA_ENTRY();
517 PARANOIA_ENTRY();
435 BUG_ON(e != lc->changing_element);
436 PARANOIA_LC_ELEMENT(lc, e);
437 ++lc->changed;
438 e->lc_number = lc->new_number;
439 list_add(&e->list, &lc->in_use);
440 hlist_add_head(&e->colision, lc_hash_slot(lc, lc->new_number));
441 lc->changing_element = NULL;
442 lc->new_number = LC_FREE;
443 clear_bit_unlock(__LC_DIRTY, &lc->flags);
518 list_for_each_entry_safe(e, tmp, &lc->to_be_changed, list) {
519 /* count number of changes, not number of transactions */
520 ++lc->changed;
521 e->lc_number = e->lc_new_number;
522 list_move(&e->list, &lc->in_use);
523 }
524 lc->pending_changes = 0;
444 RETURN();
445}
446
447
448/**
449 * lc_put - give up refcnt of @e
450 * @lc: the lru cache to operate on
451 * @e: the element to put
452 *
453 * If refcnt reaches zero, the element is moved to the lru list,
454 * and a %LC_STARVING (if set) is cleared.
455 * Returns the new (post-decrement) refcnt.
456 */
457unsigned int lc_put(struct lru_cache *lc, struct lc_element *e)
458{
459 PARANOIA_ENTRY();
460 PARANOIA_LC_ELEMENT(lc, e);
461 BUG_ON(e->refcnt == 0);
525 RETURN();
526}
527
528
529/**
530 * lc_put - give up refcnt of @e
531 * @lc: the lru cache to operate on
532 * @e: the element to put
533 *
534 * If refcnt reaches zero, the element is moved to the lru list,
535 * and a %LC_STARVING (if set) is cleared.
536 * Returns the new (post-decrement) refcnt.
537 */
538unsigned int lc_put(struct lru_cache *lc, struct lc_element *e)
539{
540 PARANOIA_ENTRY();
541 PARANOIA_LC_ELEMENT(lc, e);
542 BUG_ON(e->refcnt == 0);
462 BUG_ON(e == lc->changing_element);
543 BUG_ON(e->lc_number != e->lc_new_number);
463 if (--e->refcnt == 0) {
464 /* move it to the front of LRU. */
465 list_move(&e->list, &lc->lru);
466 lc->used--;
467 clear_bit_unlock(__LC_STARVING, &lc->flags);
468 }
469 RETURN(e->refcnt);
470}

--- 28 unchanged lines hidden (view full) ---

499 * @enr: the label to set
500 * @index: the element index to associate label with.
501 *
502 * Used to initialize the active set to some previously recorded state.
503 */
504void lc_set(struct lru_cache *lc, unsigned int enr, int index)
505{
506 struct lc_element *e;
544 if (--e->refcnt == 0) {
545 /* move it to the front of LRU. */
546 list_move(&e->list, &lc->lru);
547 lc->used--;
548 clear_bit_unlock(__LC_STARVING, &lc->flags);
549 }
550 RETURN(e->refcnt);
551}

--- 28 unchanged lines hidden (view full) ---

580 * @enr: the label to set
581 * @index: the element index to associate label with.
582 *
583 * Used to initialize the active set to some previously recorded state.
584 */
585void lc_set(struct lru_cache *lc, unsigned int enr, int index)
586{
587 struct lc_element *e;
588 struct list_head *lh;
507
508 if (index < 0 || index >= lc->nr_elements)
509 return;
510
511 e = lc_element_by_index(lc, index);
589
590 if (index < 0 || index >= lc->nr_elements)
591 return;
592
593 e = lc_element_by_index(lc, index);
512 e->lc_number = enr;
594 BUG_ON(e->lc_number != e->lc_new_number);
595 BUG_ON(e->refcnt != 0);
513
596
597 e->lc_number = e->lc_new_number = enr;
514 hlist_del_init(&e->colision);
598 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);
599 if (enr == LC_FREE)
600 lh = &lc->free;
601 else {
602 hlist_add_head(&e->colision, lc_hash_slot(lc, enr));
603 lh = &lc->lru;
604 }
605 list_move(&e->list, lh);
517}
518
519/**
520 * lc_dump - Dump a complete LRU cache to seq in textual form.
521 * @lc: the lru cache to operate on
522 * @seq: the &struct seq_file pointer to seq_printf into
523 * @utext: user supplied "heading" or other info
524 * @detail: function pointer the user may provide to dump further details

--- 23 unchanged lines hidden (view full) ---

548EXPORT_SYMBOL(lc_reset);
549EXPORT_SYMBOL(lc_destroy);
550EXPORT_SYMBOL(lc_set);
551EXPORT_SYMBOL(lc_del);
552EXPORT_SYMBOL(lc_try_get);
553EXPORT_SYMBOL(lc_find);
554EXPORT_SYMBOL(lc_get);
555EXPORT_SYMBOL(lc_put);
606}
607
608/**
609 * lc_dump - Dump a complete LRU cache to seq in textual form.
610 * @lc: the lru cache to operate on
611 * @seq: the &struct seq_file pointer to seq_printf into
612 * @utext: user supplied "heading" or other info
613 * @detail: function pointer the user may provide to dump further details

--- 23 unchanged lines hidden (view full) ---

637EXPORT_SYMBOL(lc_reset);
638EXPORT_SYMBOL(lc_destroy);
639EXPORT_SYMBOL(lc_set);
640EXPORT_SYMBOL(lc_del);
641EXPORT_SYMBOL(lc_try_get);
642EXPORT_SYMBOL(lc_find);
643EXPORT_SYMBOL(lc_get);
644EXPORT_SYMBOL(lc_put);
556EXPORT_SYMBOL(lc_changed);
645EXPORT_SYMBOL(lc_committed);
557EXPORT_SYMBOL(lc_element_by_index);
558EXPORT_SYMBOL(lc_index_of);
559EXPORT_SYMBOL(lc_seq_printf_stats);
560EXPORT_SYMBOL(lc_seq_dump_details);
646EXPORT_SYMBOL(lc_element_by_index);
647EXPORT_SYMBOL(lc_index_of);
648EXPORT_SYMBOL(lc_seq_printf_stats);
649EXPORT_SYMBOL(lc_seq_dump_details);
650EXPORT_SYMBOL(lc_try_lock);
651EXPORT_SYMBOL(lc_is_used);