list_lru.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2013 Red Hat, Inc. and Parallels Inc. All rights reserved.
  3. * Authors: David Chinner and Glauber Costa
  4. *
  5. * Generic LRU infrastructure
  6. */
  7. #ifndef _LRU_LIST_H
  8. #define _LRU_LIST_H
  9. #include <linux/list.h>
  10. #include <linux/nodemask.h>
  11. /* list_lru_walk_cb has to always return one of those */
  12. enum lru_status {
  13. LRU_REMOVED, /* item removed from list */
  14. LRU_ROTATE, /* item referenced, give another pass */
  15. LRU_SKIP, /* item cannot be locked, skip */
  16. LRU_RETRY, /* item not freeable. May drop the lock
  17. internally, but has to return locked. */
  18. };
  19. struct list_lru_node {
  20. spinlock_t lock;
  21. struct list_head list;
  22. /* kept as signed so we can catch imbalance bugs */
  23. long nr_items;
  24. } ____cacheline_aligned_in_smp;
  25. struct list_lru {
  26. /*
  27. * Because we use a fixed-size array, this struct can be very big if
  28. * MAX_NUMNODES is big. If this becomes a problem this is fixable by
  29. * turning this into a pointer and dynamically allocating this to
  30. * nr_node_ids. This quantity is firwmare-provided, and still would
  31. * provide room for all nodes at the cost of a pointer lookup and an
  32. * extra allocation. Because that allocation will most likely come from
  33. * a different slab cache than the main structure holding this
  34. * structure, we may very well fail.
  35. */
  36. struct list_lru_node node[MAX_NUMNODES];
  37. nodemask_t active_nodes;
  38. };
  39. int list_lru_init(struct list_lru *lru);
  40. /**
  41. * list_lru_add: add an element to the lru list's tail
  42. * @list_lru: the lru pointer
  43. * @item: the item to be added.
  44. *
  45. * If the element is already part of a list, this function returns doing
  46. * nothing. Therefore the caller does not need to keep state about whether or
  47. * not the element already belongs in the list and is allowed to lazy update
  48. * it. Note however that this is valid for *a* list, not *this* list. If
  49. * the caller organize itself in a way that elements can be in more than
  50. * one type of list, it is up to the caller to fully remove the item from
  51. * the previous list (with list_lru_del() for instance) before moving it
  52. * to @list_lru
  53. *
  54. * Return value: true if the list was updated, false otherwise
  55. */
  56. bool list_lru_add(struct list_lru *lru, struct list_head *item);
  57. /**
  58. * list_lru_del: delete an element to the lru list
  59. * @list_lru: the lru pointer
  60. * @item: the item to be deleted.
  61. *
  62. * This function works analogously as list_lru_add in terms of list
  63. * manipulation. The comments about an element already pertaining to
  64. * a list are also valid for list_lru_del.
  65. *
  66. * Return value: true if the list was updated, false otherwise
  67. */
  68. bool list_lru_del(struct list_lru *lru, struct list_head *item);
  69. /**
  70. * list_lru_count_node: return the number of objects currently held by @lru
  71. * @lru: the lru pointer.
  72. * @nid: the node id to count from.
  73. *
  74. * Always return a non-negative number, 0 for empty lists. There is no
  75. * guarantee that the list is not updated while the count is being computed.
  76. * Callers that want such a guarantee need to provide an outer lock.
  77. */
  78. unsigned long list_lru_count_node(struct list_lru *lru, int nid);
  79. static inline unsigned long list_lru_count(struct list_lru *lru)
  80. {
  81. long count = 0;
  82. int nid;
  83. for_each_node_mask(nid, lru->active_nodes)
  84. count += list_lru_count_node(lru, nid);
  85. return count;
  86. }
  87. typedef enum lru_status
  88. (*list_lru_walk_cb)(struct list_head *item, spinlock_t *lock, void *cb_arg);
  89. /**
  90. * list_lru_walk_node: walk a list_lru, isolating and disposing freeable items.
  91. * @lru: the lru pointer.
  92. * @nid: the node id to scan from.
  93. * @isolate: callback function that is resposible for deciding what to do with
  94. * the item currently being scanned
  95. * @cb_arg: opaque type that will be passed to @isolate
  96. * @nr_to_walk: how many items to scan.
  97. *
  98. * This function will scan all elements in a particular list_lru, calling the
  99. * @isolate callback for each of those items, along with the current list
  100. * spinlock and a caller-provided opaque. The @isolate callback can choose to
  101. * drop the lock internally, but *must* return with the lock held. The callback
  102. * will return an enum lru_status telling the list_lru infrastructure what to
  103. * do with the object being scanned.
  104. *
  105. * Please note that nr_to_walk does not mean how many objects will be freed,
  106. * just how many objects will be scanned.
  107. *
  108. * Return value: the number of objects effectively removed from the LRU.
  109. */
  110. unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
  111. list_lru_walk_cb isolate, void *cb_arg,
  112. unsigned long *nr_to_walk);
  113. static inline unsigned long
  114. list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate,
  115. void *cb_arg, unsigned long nr_to_walk)
  116. {
  117. long isolated = 0;
  118. int nid;
  119. for_each_node_mask(nid, lru->active_nodes) {
  120. isolated += list_lru_walk_node(lru, nid, isolate,
  121. cb_arg, &nr_to_walk);
  122. if (nr_to_walk <= 0)
  123. break;
  124. }
  125. return isolated;
  126. }
  127. #endif /* _LRU_LIST_H */