lru_cache.c 19 KB

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