1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (c) 2013 Red Hat, Inc. and Parallels Inc. All rights reserved.
4 * Authors: David Chinner and Glauber Costa
6 * Generic LRU infrastructure
11 #include <linux/list.h>
12 #include <linux/nodemask.h>
13 #include <linux/shrinker.h>
14 #include <linux/xarray.h>
18 /* list_lru_walk_cb has to always return one of those */
20 LRU_REMOVED, /* item removed from list */
21 LRU_REMOVED_RETRY, /* item removed, but lock has been
22 dropped and reacquired */
23 LRU_ROTATE, /* item referenced, give another pass */
24 LRU_SKIP, /* item cannot be locked, skip */
25 LRU_RETRY, /* item not freeable. May drop the lock
26 internally, but has to return locked. */
27 LRU_STOP, /* stop lru list walking. May drop the lock
28 internally, but has to return locked. */
32 struct list_head list;
33 /* may become negative during memcg reparenting */
35 /* protects all fields above */
39 struct list_lru_memcg {
41 /* array of per cgroup per node lists, indexed by node id */
42 struct list_lru_one node[];
45 struct list_lru_node {
46 /* global list, used for the root cgroup in cgroup aware lrus */
47 struct list_lru_one lru;
48 atomic_long_t nr_items;
49 } ____cacheline_aligned_in_smp;
52 struct list_lru_node *node;
54 struct list_head list;
60 struct lock_class_key *key;
64 void list_lru_destroy(struct list_lru *lru);
65 int __list_lru_init(struct list_lru *lru, bool memcg_aware,
66 struct shrinker *shrinker);
68 #define list_lru_init(lru) \
69 __list_lru_init((lru), false, NULL)
70 #define list_lru_init_memcg(lru, shrinker) \
71 __list_lru_init((lru), true, shrinker)
73 static inline int list_lru_init_memcg_key(struct list_lru *lru, struct shrinker *shrinker,
74 struct lock_class_key *key)
79 return list_lru_init_memcg(lru, shrinker);
82 int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru,
84 void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *parent);
87 * list_lru_add: add an element to the lru list's tail
88 * @lru: the lru pointer
89 * @item: the item to be added.
90 * @nid: the node id of the sublist to add the item to.
91 * @memcg: the cgroup of the sublist to add the item to.
93 * If the element is already part of a list, this function returns doing
94 * nothing. This means that it is not necessary to keep state about whether or
95 * not the element already belongs in the list. That said, this logic only
96 * works if the item is in *this* list. If the item might be in some other
97 * list, then you cannot rely on this check and you must remove it from the
98 * other list before trying to insert it.
100 * The lru list consists of many sublists internally; the @nid and @memcg
101 * parameters are used to determine which sublist to insert the item into.
102 * It's important to use the right value of @nid and @memcg when deleting the
103 * item, since it might otherwise get deleted from the wrong sublist.
105 * This also applies when attempting to insert the item multiple times - if
106 * the item is currently in one sublist and you call list_lru_add() again, you
107 * must pass the right @nid and @memcg parameters so that the same sublist is
110 * You must ensure that the memcg is not freed during this call (e.g., with
111 * rcu or by taking a css refcnt).
113 * Return: true if the list was updated, false otherwise
115 bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid,
116 struct mem_cgroup *memcg);
119 * list_lru_add_obj: add an element to the lru list's tail
120 * @lru: the lru pointer
121 * @item: the item to be added.
123 * This function is similar to list_lru_add(), but the NUMA node and the
124 * memcg of the sublist is determined by @item list_head. This assumption is
125 * valid for slab objects LRU such as dentries, inodes, etc.
127 * Return: true if the list was updated, false otherwise
129 bool list_lru_add_obj(struct list_lru *lru, struct list_head *item);
132 * list_lru_del: delete an element from the lru list
133 * @lru: the lru pointer
134 * @item: the item to be deleted.
135 * @nid: the node id of the sublist to delete the item from.
136 * @memcg: the cgroup of the sublist to delete the item from.
138 * This function works analogously as list_lru_add() in terms of list
141 * The comments in list_lru_add() about an element already being in a list are
142 * also valid for list_lru_del(), that is, you can delete an item that has
143 * already been removed or never been added. However, if the item is in a
144 * list, it must be in *this* list, and you must pass the right value of @nid
145 * and @memcg so that the right sublist is used.
147 * You must ensure that the memcg is not freed during this call (e.g., with
148 * rcu or by taking a css refcnt). When a memcg is deleted, list_lru entries
149 * are automatically moved to the parent memcg. This is done in a race-free
150 * way, so during deletion of an memcg both the old and new memcg will resolve
151 * to the same sublist internally.
153 * Return: true if the list was updated, false otherwise
155 bool list_lru_del(struct list_lru *lru, struct list_head *item, int nid,
156 struct mem_cgroup *memcg);
159 * list_lru_del_obj: delete an element from the lru list
160 * @lru: the lru pointer
161 * @item: the item to be deleted.
163 * This function is similar to list_lru_del(), but the NUMA node and the
164 * memcg of the sublist is determined by @item list_head. This assumption is
165 * valid for slab objects LRU such as dentries, inodes, etc.
167 * Return: true if the list was updated, false otherwise.
169 bool list_lru_del_obj(struct list_lru *lru, struct list_head *item);
172 * list_lru_count_one: return the number of objects currently held by @lru
173 * @lru: the lru pointer.
174 * @nid: the node id to count from.
175 * @memcg: the cgroup to count from.
177 * There is no guarantee that the list is not updated while the count is being
178 * computed. Callers that want such a guarantee need to provide an outer lock.
180 * Return: 0 for empty lists, otherwise the number of objects
181 * currently held by @lru.
183 unsigned long list_lru_count_one(struct list_lru *lru,
184 int nid, struct mem_cgroup *memcg);
185 unsigned long list_lru_count_node(struct list_lru *lru, int nid);
187 static inline unsigned long list_lru_shrink_count(struct list_lru *lru,
188 struct shrink_control *sc)
190 return list_lru_count_one(lru, sc->nid, sc->memcg);
193 static inline unsigned long list_lru_count(struct list_lru *lru)
198 for_each_node_state(nid, N_NORMAL_MEMORY)
199 count += list_lru_count_node(lru, nid);
204 void list_lru_isolate(struct list_lru_one *list, struct list_head *item);
205 void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item,
206 struct list_head *head);
208 typedef enum lru_status (*list_lru_walk_cb)(struct list_head *item,
209 struct list_lru_one *list, void *cb_arg);
212 * list_lru_walk_one: walk a @lru, isolating and disposing freeable items.
213 * @lru: the lru pointer.
214 * @nid: the node id to scan from.
215 * @memcg: the cgroup to scan from.
216 * @isolate: callback function that is responsible for deciding what to do with
217 * the item currently being scanned
218 * @cb_arg: opaque type that will be passed to @isolate
219 * @nr_to_walk: how many items to scan.
221 * This function will scan all elements in a particular @lru, calling the
222 * @isolate callback for each of those items, along with the current list
223 * spinlock and a caller-provided opaque. The @isolate callback can choose to
224 * drop the lock internally, but *must* return with the lock held. The callback
225 * will return an enum lru_status telling the @lru infrastructure what to
226 * do with the object being scanned.
228 * Please note that @nr_to_walk does not mean how many objects will be freed,
229 * just how many objects will be scanned.
231 * Return: the number of objects effectively removed from the LRU.
233 unsigned long list_lru_walk_one(struct list_lru *lru,
234 int nid, struct mem_cgroup *memcg,
235 list_lru_walk_cb isolate, void *cb_arg,
236 unsigned long *nr_to_walk);
238 * list_lru_walk_one_irq: walk a @lru, isolating and disposing freeable items.
239 * @lru: the lru pointer.
240 * @nid: the node id to scan from.
241 * @memcg: the cgroup to scan from.
242 * @isolate: callback function that is responsible for deciding what to do with
243 * the item currently being scanned
244 * @cb_arg: opaque type that will be passed to @isolate
245 * @nr_to_walk: how many items to scan.
247 * Same as list_lru_walk_one() except that the spinlock is acquired with
250 unsigned long list_lru_walk_one_irq(struct list_lru *lru,
251 int nid, struct mem_cgroup *memcg,
252 list_lru_walk_cb isolate, void *cb_arg,
253 unsigned long *nr_to_walk);
254 unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
255 list_lru_walk_cb isolate, void *cb_arg,
256 unsigned long *nr_to_walk);
258 static inline unsigned long
259 list_lru_shrink_walk(struct list_lru *lru, struct shrink_control *sc,
260 list_lru_walk_cb isolate, void *cb_arg)
262 return list_lru_walk_one(lru, sc->nid, sc->memcg, isolate, cb_arg,
266 static inline unsigned long
267 list_lru_shrink_walk_irq(struct list_lru *lru, struct shrink_control *sc,
268 list_lru_walk_cb isolate, void *cb_arg)
270 return list_lru_walk_one_irq(lru, sc->nid, sc->memcg, isolate, cb_arg,
274 static inline unsigned long
275 list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate,
276 void *cb_arg, unsigned long nr_to_walk)
281 for_each_node_state(nid, N_NORMAL_MEMORY) {
282 isolated += list_lru_walk_node(lru, nid, isolate,
283 cb_arg, &nr_to_walk);
289 #endif /* _LRU_LIST_H */