list_lru.h 6.2 KB

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