list_lru.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2013 Red Hat, Inc. and Parallels Inc. All rights reserved.
  4. * Authors: David Chinner and Glauber Costa
  5. *
  6. * Generic LRU infrastructure
  7. */
  8. #ifndef _LRU_LIST_H
  9. #define _LRU_LIST_H
  10. #include <linux/list.h>
  11. #include <linux/nodemask.h>
  12. #include <linux/shrinker.h>
  13. struct mem_cgroup;
  14. /* list_lru_walk_cb has to always return one of those */
  15. enum lru_status {
  16. LRU_REMOVED, /* item removed from list */
  17. LRU_REMOVED_RETRY, /* item removed, but lock has been
  18. dropped and reacquired */
  19. LRU_ROTATE, /* item referenced, give another pass */
  20. LRU_SKIP, /* item cannot be locked, skip */
  21. LRU_RETRY, /* item not freeable. May drop the lock
  22. internally, but has to return locked. */
  23. };
  24. struct list_lru_one {
  25. struct list_head list;
  26. /* may become negative during memcg reparenting */
  27. long nr_items;
  28. };
  29. struct list_lru_memcg {
  30. struct rcu_head rcu;
  31. /* array of per cgroup lists, indexed by memcg_cache_id */
  32. struct list_lru_one *lru[];
  33. };
  34. struct list_lru_node {
  35. /* protects all lists on the node, including per cgroup */
  36. spinlock_t lock;
  37. /* global list, used for the root cgroup in cgroup aware lrus */
  38. struct list_lru_one lru;
  39. #ifdef CONFIG_MEMCG_KMEM
  40. /* for cgroup aware lrus points to per cgroup lists, otherwise NULL */
  41. struct list_lru_memcg __rcu *memcg_lrus;
  42. #endif
  43. long nr_items;
  44. } ____cacheline_aligned_in_smp;
  45. struct list_lru {
  46. struct list_lru_node *node;
  47. #ifdef CONFIG_MEMCG_KMEM
  48. struct list_head list;
  49. int shrinker_id;
  50. bool memcg_aware;
  51. #endif
  52. };
  53. void list_lru_destroy(struct list_lru *lru);
  54. int __list_lru_init(struct list_lru *lru, bool memcg_aware,
  55. struct lock_class_key *key, struct shrinker *shrinker);
  56. #define list_lru_init(lru) \
  57. __list_lru_init((lru), false, NULL, NULL)
  58. #define list_lru_init_key(lru, key) \
  59. __list_lru_init((lru), false, (key), NULL)
  60. #define list_lru_init_memcg(lru, shrinker) \
  61. __list_lru_init((lru), true, NULL, shrinker)
  62. int memcg_update_all_list_lrus(int num_memcgs);
  63. void memcg_drain_all_list_lrus(int src_idx, struct mem_cgroup *dst_memcg);
  64. /**
  65. * list_lru_add: add an element to the lru list's tail
  66. * @list_lru: the lru pointer
  67. * @item: the item to be added.
  68. *
  69. * If the element is already part of a list, this function returns doing
  70. * nothing. Therefore the caller does not need to keep state about whether or
  71. * not the element already belongs in the list and is allowed to lazy update
  72. * it. Note however that this is valid for *a* list, not *this* list. If
  73. * the caller organize itself in a way that elements can be in more than
  74. * one type of list, it is up to the caller to fully remove the item from
  75. * the previous list (with list_lru_del() for instance) before moving it
  76. * to @list_lru
  77. *
  78. * Return value: true if the list was updated, false otherwise
  79. */
  80. bool list_lru_add(struct list_lru *lru, struct list_head *item);
  81. /**
  82. * list_lru_del: delete an element to the lru list
  83. * @list_lru: the lru pointer
  84. * @item: the item to be deleted.
  85. *
  86. * This function works analogously as list_lru_add in terms of list
  87. * manipulation. The comments about an element already pertaining to
  88. * a list are also valid for list_lru_del.
  89. *
  90. * Return value: true if the list was updated, false otherwise
  91. */
  92. bool list_lru_del(struct list_lru *lru, struct list_head *item);
  93. /**
  94. * list_lru_count_one: return the number of objects currently held by @lru
  95. * @lru: the lru pointer.
  96. * @nid: the node id to count from.
  97. * @memcg: the cgroup to count from.
  98. *
  99. * Always return a non-negative number, 0 for empty lists. There is no
  100. * guarantee that the list is not updated while the count is being computed.
  101. * Callers that want such a guarantee need to provide an outer lock.
  102. */
  103. unsigned long list_lru_count_one(struct list_lru *lru,
  104. int nid, struct mem_cgroup *memcg);
  105. unsigned long list_lru_count_node(struct list_lru *lru, int nid);
  106. static inline unsigned long list_lru_shrink_count(struct list_lru *lru,
  107. struct shrink_control *sc)
  108. {
  109. return list_lru_count_one(lru, sc->nid, sc->memcg);
  110. }
  111. static inline unsigned long list_lru_count(struct list_lru *lru)
  112. {
  113. long count = 0;
  114. int nid;
  115. for_each_node_state(nid, N_NORMAL_MEMORY)
  116. count += list_lru_count_node(lru, nid);
  117. return count;
  118. }
  119. void list_lru_isolate(struct list_lru_one *list, struct list_head *item);
  120. void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item,
  121. struct list_head *head);
  122. typedef enum lru_status (*list_lru_walk_cb)(struct list_head *item,
  123. struct list_lru_one *list, spinlock_t *lock, void *cb_arg);
  124. /**
  125. * list_lru_walk_one: walk a list_lru, isolating and disposing freeable items.
  126. * @lru: the lru pointer.
  127. * @nid: the node id to scan from.
  128. * @memcg: the cgroup to scan from.
  129. * @isolate: callback function that is resposible for deciding what to do with
  130. * the item currently being scanned
  131. * @cb_arg: opaque type that will be passed to @isolate
  132. * @nr_to_walk: how many items to scan.
  133. *
  134. * This function will scan all elements in a particular list_lru, calling the
  135. * @isolate callback for each of those items, along with the current list
  136. * spinlock and a caller-provided opaque. The @isolate callback can choose to
  137. * drop the lock internally, but *must* return with the lock held. The callback
  138. * will return an enum lru_status telling the list_lru infrastructure what to
  139. * do with the object being scanned.
  140. *
  141. * Please note that nr_to_walk does not mean how many objects will be freed,
  142. * just how many objects will be scanned.
  143. *
  144. * Return value: the number of objects effectively removed from the LRU.
  145. */
  146. unsigned long list_lru_walk_one(struct list_lru *lru,
  147. int nid, struct mem_cgroup *memcg,
  148. list_lru_walk_cb isolate, void *cb_arg,
  149. unsigned long *nr_to_walk);
  150. /**
  151. * list_lru_walk_one_irq: walk a list_lru, isolating and disposing freeable items.
  152. * @lru: the lru pointer.
  153. * @nid: the node id to scan from.
  154. * @memcg: the cgroup to scan from.
  155. * @isolate: callback function that is resposible for deciding what to do with
  156. * the item currently being scanned
  157. * @cb_arg: opaque type that will be passed to @isolate
  158. * @nr_to_walk: how many items to scan.
  159. *
  160. * Same as @list_lru_walk_one except that the spinlock is acquired with
  161. * spin_lock_irq().
  162. */
  163. unsigned long list_lru_walk_one_irq(struct list_lru *lru,
  164. int nid, struct mem_cgroup *memcg,
  165. list_lru_walk_cb isolate, void *cb_arg,
  166. unsigned long *nr_to_walk);
  167. unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
  168. list_lru_walk_cb isolate, void *cb_arg,
  169. unsigned long *nr_to_walk);
  170. static inline unsigned long
  171. list_lru_shrink_walk(struct list_lru *lru, struct shrink_control *sc,
  172. list_lru_walk_cb isolate, void *cb_arg)
  173. {
  174. return list_lru_walk_one(lru, sc->nid, sc->memcg, isolate, cb_arg,
  175. &sc->nr_to_scan);
  176. }
  177. static inline unsigned long
  178. list_lru_shrink_walk_irq(struct list_lru *lru, struct shrink_control *sc,
  179. list_lru_walk_cb isolate, void *cb_arg)
  180. {
  181. return list_lru_walk_one_irq(lru, sc->nid, sc->memcg, isolate, cb_arg,
  182. &sc->nr_to_scan);
  183. }
  184. static inline unsigned long
  185. list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate,
  186. void *cb_arg, unsigned long nr_to_walk)
  187. {
  188. long isolated = 0;
  189. int nid;
  190. for_each_node_state(nid, N_NORMAL_MEMORY) {
  191. isolated += list_lru_walk_node(lru, nid, isolate,
  192. cb_arg, &nr_to_walk);
  193. if (nr_to_walk <= 0)
  194. break;
  195. }
  196. return isolated;
  197. }
  198. #endif /* _LRU_LIST_H */