xfs_mru_cache.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2006-2007 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_mru_cache.h"
  8. /*
  9. * The MRU Cache data structure consists of a data store, an array of lists and
  10. * a lock to protect its internal state. At initialisation time, the client
  11. * supplies an element lifetime in milliseconds and a group count, as well as a
  12. * function pointer to call when deleting elements. A data structure for
  13. * queueing up work in the form of timed callbacks is also included.
  14. *
  15. * The group count controls how many lists are created, and thereby how finely
  16. * the elements are grouped in time. When reaping occurs, all the elements in
  17. * all the lists whose time has expired are deleted.
  18. *
  19. * To give an example of how this works in practice, consider a client that
  20. * initialises an MRU Cache with a lifetime of ten seconds and a group count of
  21. * five. Five internal lists will be created, each representing a two second
  22. * period in time. When the first element is added, time zero for the data
  23. * structure is initialised to the current time.
  24. *
  25. * All the elements added in the first two seconds are appended to the first
  26. * list. Elements added in the third second go into the second list, and so on.
  27. * If an element is accessed at any point, it is removed from its list and
  28. * inserted at the head of the current most-recently-used list.
  29. *
  30. * The reaper function will have nothing to do until at least twelve seconds
  31. * have elapsed since the first element was added. The reason for this is that
  32. * if it were called at t=11s, there could be elements in the first list that
  33. * have only been inactive for nine seconds, so it still does nothing. If it is
  34. * called anywhere between t=12 and t=14 seconds, it will delete all the
  35. * elements that remain in the first list. It's therefore possible for elements
  36. * to remain in the data store even after they've been inactive for up to
  37. * (t + t/g) seconds, where t is the inactive element lifetime and g is the
  38. * number of groups.
  39. *
  40. * The above example assumes that the reaper function gets called at least once
  41. * every (t/g) seconds. If it is called less frequently, unused elements will
  42. * accumulate in the reap list until the reaper function is eventually called.
  43. * The current implementation uses work queue callbacks to carefully time the
  44. * reaper function calls, so this should happen rarely, if at all.
  45. *
  46. * From a design perspective, the primary reason for the choice of a list array
  47. * representing discrete time intervals is that it's only practical to reap
  48. * expired elements in groups of some appreciable size. This automatically
  49. * introduces a granularity to element lifetimes, so there's no point storing an
  50. * individual timeout with each element that specifies a more precise reap time.
  51. * The bonus is a saving of sizeof(long) bytes of memory per element stored.
  52. *
  53. * The elements could have been stored in just one list, but an array of
  54. * counters or pointers would need to be maintained to allow them to be divided
  55. * up into discrete time groups. More critically, the process of touching or
  56. * removing an element would involve walking large portions of the entire list,
  57. * which would have a detrimental effect on performance. The additional memory
  58. * requirement for the array of list heads is minimal.
  59. *
  60. * When an element is touched or deleted, it needs to be removed from its
  61. * current list. Doubly linked lists are used to make the list maintenance
  62. * portion of these operations O(1). Since reaper timing can be imprecise,
  63. * inserts and lookups can occur when there are no free lists available. When
  64. * this happens, all the elements on the LRU list need to be migrated to the end
  65. * of the reap list. To keep the list maintenance portion of these operations
  66. * O(1) also, list tails need to be accessible without walking the entire list.
  67. * This is the reason why doubly linked list heads are used.
  68. */
  69. /*
  70. * An MRU Cache is a dynamic data structure that stores its elements in a way
  71. * that allows efficient lookups, but also groups them into discrete time
  72. * intervals based on insertion time. This allows elements to be efficiently
  73. * and automatically reaped after a fixed period of inactivity.
  74. *
  75. * When a client data pointer is stored in the MRU Cache it needs to be added to
  76. * both the data store and to one of the lists. It must also be possible to
  77. * access each of these entries via the other, i.e. to:
  78. *
  79. * a) Walk a list, removing the corresponding data store entry for each item.
  80. * b) Look up a data store entry, then access its list entry directly.
  81. *
  82. * To achieve both of these goals, each entry must contain both a list entry and
  83. * a key, in addition to the user's data pointer. Note that it's not a good
  84. * idea to have the client embed one of these structures at the top of their own
  85. * data structure, because inserting the same item more than once would most
  86. * likely result in a loop in one of the lists. That's a sure-fire recipe for
  87. * an infinite loop in the code.
  88. */
  89. struct xfs_mru_cache {
  90. struct radix_tree_root store; /* Core storage data structure. */
  91. struct list_head *lists; /* Array of lists, one per grp. */
  92. struct list_head reap_list; /* Elements overdue for reaping. */
  93. spinlock_t lock; /* Lock to protect this struct. */
  94. unsigned int grp_count; /* Number of discrete groups. */
  95. unsigned int grp_time; /* Time period spanned by grps. */
  96. unsigned int lru_grp; /* Group containing time zero. */
  97. unsigned long time_zero; /* Time first element was added. */
  98. xfs_mru_cache_free_func_t free_func; /* Function pointer for freeing. */
  99. struct delayed_work work; /* Workqueue data for reaping. */
  100. unsigned int queued; /* work has been queued */
  101. void *data;
  102. };
  103. static struct workqueue_struct *xfs_mru_reap_wq;
  104. /*
  105. * When inserting, destroying or reaping, it's first necessary to update the
  106. * lists relative to a particular time. In the case of destroying, that time
  107. * will be well in the future to ensure that all items are moved to the reap
  108. * list. In all other cases though, the time will be the current time.
  109. *
  110. * This function enters a loop, moving the contents of the LRU list to the reap
  111. * list again and again until either a) the lists are all empty, or b) time zero
  112. * has been advanced sufficiently to be within the immediate element lifetime.
  113. *
  114. * Case a) above is detected by counting how many groups are migrated and
  115. * stopping when they've all been moved. Case b) is detected by monitoring the
  116. * time_zero field, which is updated as each group is migrated.
  117. *
  118. * The return value is the earliest time that more migration could be needed, or
  119. * zero if there's no need to schedule more work because the lists are empty.
  120. */
  121. STATIC unsigned long
  122. _xfs_mru_cache_migrate(
  123. struct xfs_mru_cache *mru,
  124. unsigned long now)
  125. {
  126. unsigned int grp;
  127. unsigned int migrated = 0;
  128. struct list_head *lru_list;
  129. /* Nothing to do if the data store is empty. */
  130. if (!mru->time_zero)
  131. return 0;
  132. /* While time zero is older than the time spanned by all the lists. */
  133. while (mru->time_zero <= now - mru->grp_count * mru->grp_time) {
  134. /*
  135. * If the LRU list isn't empty, migrate its elements to the tail
  136. * of the reap list.
  137. */
  138. lru_list = mru->lists + mru->lru_grp;
  139. if (!list_empty(lru_list))
  140. list_splice_init(lru_list, mru->reap_list.prev);
  141. /*
  142. * Advance the LRU group number, freeing the old LRU list to
  143. * become the new MRU list; advance time zero accordingly.
  144. */
  145. mru->lru_grp = (mru->lru_grp + 1) % mru->grp_count;
  146. mru->time_zero += mru->grp_time;
  147. /*
  148. * If reaping is so far behind that all the elements on all the
  149. * lists have been migrated to the reap list, it's now empty.
  150. */
  151. if (++migrated == mru->grp_count) {
  152. mru->lru_grp = 0;
  153. mru->time_zero = 0;
  154. return 0;
  155. }
  156. }
  157. /* Find the first non-empty list from the LRU end. */
  158. for (grp = 0; grp < mru->grp_count; grp++) {
  159. /* Check the grp'th list from the LRU end. */
  160. lru_list = mru->lists + ((mru->lru_grp + grp) % mru->grp_count);
  161. if (!list_empty(lru_list))
  162. return mru->time_zero +
  163. (mru->grp_count + grp) * mru->grp_time;
  164. }
  165. /* All the lists must be empty. */
  166. mru->lru_grp = 0;
  167. mru->time_zero = 0;
  168. return 0;
  169. }
  170. /*
  171. * When inserting or doing a lookup, an element needs to be inserted into the
  172. * MRU list. The lists must be migrated first to ensure that they're
  173. * up-to-date, otherwise the new element could be given a shorter lifetime in
  174. * the cache than it should.
  175. */
  176. STATIC void
  177. _xfs_mru_cache_list_insert(
  178. struct xfs_mru_cache *mru,
  179. struct xfs_mru_cache_elem *elem)
  180. {
  181. unsigned int grp = 0;
  182. unsigned long now = jiffies;
  183. /*
  184. * If the data store is empty, initialise time zero, leave grp set to
  185. * zero and start the work queue timer if necessary. Otherwise, set grp
  186. * to the number of group times that have elapsed since time zero.
  187. */
  188. if (!_xfs_mru_cache_migrate(mru, now)) {
  189. mru->time_zero = now;
  190. if (!mru->queued) {
  191. mru->queued = 1;
  192. queue_delayed_work(xfs_mru_reap_wq, &mru->work,
  193. mru->grp_count * mru->grp_time);
  194. }
  195. } else {
  196. grp = (now - mru->time_zero) / mru->grp_time;
  197. grp = (mru->lru_grp + grp) % mru->grp_count;
  198. }
  199. /* Insert the element at the tail of the corresponding list. */
  200. list_add_tail(&elem->list_node, mru->lists + grp);
  201. }
  202. /*
  203. * When destroying or reaping, all the elements that were migrated to the reap
  204. * list need to be deleted. For each element this involves removing it from the
  205. * data store, removing it from the reap list, calling the client's free
  206. * function and deleting the element from the element cache.
  207. *
  208. * We get called holding the mru->lock, which we drop and then reacquire.
  209. * Sparse need special help with this to tell it we know what we are doing.
  210. */
  211. STATIC void
  212. _xfs_mru_cache_clear_reap_list(
  213. struct xfs_mru_cache *mru)
  214. __releases(mru->lock) __acquires(mru->lock)
  215. {
  216. struct xfs_mru_cache_elem *elem, *next;
  217. LIST_HEAD(tmp);
  218. list_for_each_entry_safe(elem, next, &mru->reap_list, list_node) {
  219. /* Remove the element from the data store. */
  220. radix_tree_delete(&mru->store, elem->key);
  221. /*
  222. * remove to temp list so it can be freed without
  223. * needing to hold the lock
  224. */
  225. list_move(&elem->list_node, &tmp);
  226. }
  227. spin_unlock(&mru->lock);
  228. list_for_each_entry_safe(elem, next, &tmp, list_node) {
  229. list_del_init(&elem->list_node);
  230. mru->free_func(mru->data, elem);
  231. }
  232. spin_lock(&mru->lock);
  233. }
  234. /*
  235. * We fire the reap timer every group expiry interval so
  236. * we always have a reaper ready to run. This makes shutdown
  237. * and flushing of the reaper easy to do. Hence we need to
  238. * keep when the next reap must occur so we can determine
  239. * at each interval whether there is anything we need to do.
  240. */
  241. STATIC void
  242. _xfs_mru_cache_reap(
  243. struct work_struct *work)
  244. {
  245. struct xfs_mru_cache *mru =
  246. container_of(work, struct xfs_mru_cache, work.work);
  247. unsigned long now, next;
  248. ASSERT(mru && mru->lists);
  249. if (!mru || !mru->lists)
  250. return;
  251. spin_lock(&mru->lock);
  252. next = _xfs_mru_cache_migrate(mru, jiffies);
  253. _xfs_mru_cache_clear_reap_list(mru);
  254. mru->queued = next;
  255. if ((mru->queued > 0)) {
  256. now = jiffies;
  257. if (next <= now)
  258. next = 0;
  259. else
  260. next -= now;
  261. queue_delayed_work(xfs_mru_reap_wq, &mru->work, next);
  262. }
  263. spin_unlock(&mru->lock);
  264. }
  265. int
  266. xfs_mru_cache_init(void)
  267. {
  268. xfs_mru_reap_wq = alloc_workqueue("xfs_mru_cache",
  269. XFS_WQFLAGS(WQ_MEM_RECLAIM | WQ_FREEZABLE), 1);
  270. if (!xfs_mru_reap_wq)
  271. return -ENOMEM;
  272. return 0;
  273. }
  274. void
  275. xfs_mru_cache_uninit(void)
  276. {
  277. destroy_workqueue(xfs_mru_reap_wq);
  278. }
  279. /*
  280. * To initialise a struct xfs_mru_cache pointer, call xfs_mru_cache_create()
  281. * with the address of the pointer, a lifetime value in milliseconds, a group
  282. * count and a free function to use when deleting elements. This function
  283. * returns 0 if the initialisation was successful.
  284. */
  285. int
  286. xfs_mru_cache_create(
  287. struct xfs_mru_cache **mrup,
  288. void *data,
  289. unsigned int lifetime_ms,
  290. unsigned int grp_count,
  291. xfs_mru_cache_free_func_t free_func)
  292. {
  293. struct xfs_mru_cache *mru = NULL;
  294. int err = 0, grp;
  295. unsigned int grp_time;
  296. if (mrup)
  297. *mrup = NULL;
  298. if (!mrup || !grp_count || !lifetime_ms || !free_func)
  299. return -EINVAL;
  300. if (!(grp_time = msecs_to_jiffies(lifetime_ms) / grp_count))
  301. return -EINVAL;
  302. mru = kzalloc(sizeof(*mru), GFP_KERNEL | __GFP_NOFAIL);
  303. if (!mru)
  304. return -ENOMEM;
  305. /* An extra list is needed to avoid reaping up to a grp_time early. */
  306. mru->grp_count = grp_count + 1;
  307. mru->lists = kzalloc(mru->grp_count * sizeof(*mru->lists),
  308. GFP_KERNEL | __GFP_NOFAIL);
  309. if (!mru->lists) {
  310. err = -ENOMEM;
  311. goto exit;
  312. }
  313. for (grp = 0; grp < mru->grp_count; grp++)
  314. INIT_LIST_HEAD(mru->lists + grp);
  315. /*
  316. * We use GFP_KERNEL radix tree preload and do inserts under a
  317. * spinlock so GFP_ATOMIC is appropriate for the radix tree itself.
  318. */
  319. INIT_RADIX_TREE(&mru->store, GFP_ATOMIC);
  320. INIT_LIST_HEAD(&mru->reap_list);
  321. spin_lock_init(&mru->lock);
  322. INIT_DELAYED_WORK(&mru->work, _xfs_mru_cache_reap);
  323. mru->grp_time = grp_time;
  324. mru->free_func = free_func;
  325. mru->data = data;
  326. *mrup = mru;
  327. exit:
  328. if (err && mru && mru->lists)
  329. kfree(mru->lists);
  330. if (err && mru)
  331. kfree(mru);
  332. return err;
  333. }
  334. /*
  335. * Call xfs_mru_cache_flush() to flush out all cached entries, calling their
  336. * free functions as they're deleted. When this function returns, the caller is
  337. * guaranteed that all the free functions for all the elements have finished
  338. * executing and the reaper is not running.
  339. */
  340. static void
  341. xfs_mru_cache_flush(
  342. struct xfs_mru_cache *mru)
  343. {
  344. if (!mru || !mru->lists)
  345. return;
  346. spin_lock(&mru->lock);
  347. if (mru->queued) {
  348. spin_unlock(&mru->lock);
  349. cancel_delayed_work_sync(&mru->work);
  350. spin_lock(&mru->lock);
  351. }
  352. _xfs_mru_cache_migrate(mru, jiffies + mru->grp_count * mru->grp_time);
  353. _xfs_mru_cache_clear_reap_list(mru);
  354. spin_unlock(&mru->lock);
  355. }
  356. void
  357. xfs_mru_cache_destroy(
  358. struct xfs_mru_cache *mru)
  359. {
  360. if (!mru || !mru->lists)
  361. return;
  362. xfs_mru_cache_flush(mru);
  363. kfree(mru->lists);
  364. kfree(mru);
  365. }
  366. /*
  367. * To insert an element, call xfs_mru_cache_insert() with the data store, the
  368. * element's key and the client data pointer. This function returns 0 on
  369. * success or ENOMEM if memory for the data element couldn't be allocated.
  370. */
  371. int
  372. xfs_mru_cache_insert(
  373. struct xfs_mru_cache *mru,
  374. unsigned long key,
  375. struct xfs_mru_cache_elem *elem)
  376. {
  377. int error;
  378. ASSERT(mru && mru->lists);
  379. if (!mru || !mru->lists)
  380. return -EINVAL;
  381. if (radix_tree_preload(GFP_KERNEL))
  382. return -ENOMEM;
  383. INIT_LIST_HEAD(&elem->list_node);
  384. elem->key = key;
  385. spin_lock(&mru->lock);
  386. error = radix_tree_insert(&mru->store, key, elem);
  387. radix_tree_preload_end();
  388. if (!error)
  389. _xfs_mru_cache_list_insert(mru, elem);
  390. spin_unlock(&mru->lock);
  391. return error;
  392. }
  393. /*
  394. * To remove an element without calling the free function, call
  395. * xfs_mru_cache_remove() with the data store and the element's key. On success
  396. * the client data pointer for the removed element is returned, otherwise this
  397. * function will return a NULL pointer.
  398. */
  399. struct xfs_mru_cache_elem *
  400. xfs_mru_cache_remove(
  401. struct xfs_mru_cache *mru,
  402. unsigned long key)
  403. {
  404. struct xfs_mru_cache_elem *elem;
  405. ASSERT(mru && mru->lists);
  406. if (!mru || !mru->lists)
  407. return NULL;
  408. spin_lock(&mru->lock);
  409. elem = radix_tree_delete(&mru->store, key);
  410. if (elem)
  411. list_del(&elem->list_node);
  412. spin_unlock(&mru->lock);
  413. return elem;
  414. }
  415. /*
  416. * To remove and element and call the free function, call xfs_mru_cache_delete()
  417. * with the data store and the element's key.
  418. */
  419. void
  420. xfs_mru_cache_delete(
  421. struct xfs_mru_cache *mru,
  422. unsigned long key)
  423. {
  424. struct xfs_mru_cache_elem *elem;
  425. elem = xfs_mru_cache_remove(mru, key);
  426. if (elem)
  427. mru->free_func(mru->data, elem);
  428. }
  429. /*
  430. * To look up an element using its key, call xfs_mru_cache_lookup() with the
  431. * data store and the element's key. If found, the element will be moved to the
  432. * head of the MRU list to indicate that it's been touched.
  433. *
  434. * The internal data structures are protected by a spinlock that is STILL HELD
  435. * when this function returns. Call xfs_mru_cache_done() to release it. Note
  436. * that it is not safe to call any function that might sleep in the interim.
  437. *
  438. * The implementation could have used reference counting to avoid this
  439. * restriction, but since most clients simply want to get, set or test a member
  440. * of the returned data structure, the extra per-element memory isn't warranted.
  441. *
  442. * If the element isn't found, this function returns NULL and the spinlock is
  443. * released. xfs_mru_cache_done() should NOT be called when this occurs.
  444. *
  445. * Because sparse isn't smart enough to know about conditional lock return
  446. * status, we need to help it get it right by annotating the path that does
  447. * not release the lock.
  448. */
  449. struct xfs_mru_cache_elem *
  450. xfs_mru_cache_lookup(
  451. struct xfs_mru_cache *mru,
  452. unsigned long key)
  453. {
  454. struct xfs_mru_cache_elem *elem;
  455. ASSERT(mru && mru->lists);
  456. if (!mru || !mru->lists)
  457. return NULL;
  458. spin_lock(&mru->lock);
  459. elem = radix_tree_lookup(&mru->store, key);
  460. if (elem) {
  461. list_del(&elem->list_node);
  462. _xfs_mru_cache_list_insert(mru, elem);
  463. __release(mru_lock); /* help sparse not be stupid */
  464. } else
  465. spin_unlock(&mru->lock);
  466. return elem;
  467. }
  468. /*
  469. * To release the internal data structure spinlock after having performed an
  470. * xfs_mru_cache_lookup() or an xfs_mru_cache_peek(), call xfs_mru_cache_done()
  471. * with the data store pointer.
  472. */
  473. void
  474. xfs_mru_cache_done(
  475. struct xfs_mru_cache *mru)
  476. __releases(mru->lock)
  477. {
  478. spin_unlock(&mru->lock);
  479. }