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. Therefore the caller does not need to keep state about whether or
95 * not the element already belongs in the list and is allowed to lazy update
96 * it. Note however that this is valid for *a* list, not *this* list. If
97 * the caller organize itself in a way that elements can be in more than
98 * one type of list, it is up to the caller to fully remove the item from
99 * the previous list (with list_lru_del() for instance) before moving it
102 * Return: true if the list was updated, false otherwise
104 bool list_lru_add(struct list_lru
*lru
, struct list_head
*item
, int nid
,
105 struct mem_cgroup
*memcg
);
108 * list_lru_add_obj: add an element to the lru list's tail
109 * @lru: the lru pointer
110 * @item: the item to be added.
112 * This function is similar to list_lru_add(), but the NUMA node and the
113 * memcg of the sublist is determined by @item list_head. This assumption is
114 * valid for slab objects LRU such as dentries, inodes, etc.
116 * Return value: true if the list was updated, false otherwise
118 bool list_lru_add_obj(struct list_lru
*lru
, struct list_head
*item
);
121 * list_lru_del: delete an element from the lru list
122 * @lru: the lru pointer
123 * @item: the item to be deleted.
124 * @nid: the node id of the sublist to delete the item from.
125 * @memcg: the cgroup of the sublist to delete the item from.
127 * This function works analogously as list_lru_add() in terms of list
128 * manipulation. The comments about an element already pertaining to
129 * a list are also valid for list_lru_del().
131 * Return: true if the list was updated, false otherwise
133 bool list_lru_del(struct list_lru
*lru
, struct list_head
*item
, int nid
,
134 struct mem_cgroup
*memcg
);
137 * list_lru_del_obj: delete an element from the lru list
138 * @lru: the lru pointer
139 * @item: the item to be deleted.
141 * This function is similar to list_lru_del(), but the NUMA node and the
142 * memcg of the sublist is determined by @item list_head. This assumption is
143 * valid for slab objects LRU such as dentries, inodes, etc.
145 * Return value: true if the list was updated, false otherwise.
147 bool list_lru_del_obj(struct list_lru
*lru
, struct list_head
*item
);
150 * list_lru_count_one: return the number of objects currently held by @lru
151 * @lru: the lru pointer.
152 * @nid: the node id to count from.
153 * @memcg: the cgroup to count from.
155 * There is no guarantee that the list is not updated while the count is being
156 * computed. Callers that want such a guarantee need to provide an outer lock.
158 * Return: 0 for empty lists, otherwise the number of objects
159 * currently held by @lru.
161 unsigned long list_lru_count_one(struct list_lru
*lru
,
162 int nid
, struct mem_cgroup
*memcg
);
163 unsigned long list_lru_count_node(struct list_lru
*lru
, int nid
);
165 static inline unsigned long list_lru_shrink_count(struct list_lru
*lru
,
166 struct shrink_control
*sc
)
168 return list_lru_count_one(lru
, sc
->nid
, sc
->memcg
);
171 static inline unsigned long list_lru_count(struct list_lru
*lru
)
176 for_each_node_state(nid
, N_NORMAL_MEMORY
)
177 count
+= list_lru_count_node(lru
, nid
);
182 void list_lru_isolate(struct list_lru_one
*list
, struct list_head
*item
);
183 void list_lru_isolate_move(struct list_lru_one
*list
, struct list_head
*item
,
184 struct list_head
*head
);
186 typedef enum lru_status (*list_lru_walk_cb
)(struct list_head
*item
,
187 struct list_lru_one
*list
, void *cb_arg
);
190 * list_lru_walk_one: walk a @lru, isolating and disposing freeable items.
191 * @lru: the lru pointer.
192 * @nid: the node id to scan from.
193 * @memcg: the cgroup to scan from.
194 * @isolate: callback function that is responsible for deciding what to do with
195 * the item currently being scanned
196 * @cb_arg: opaque type that will be passed to @isolate
197 * @nr_to_walk: how many items to scan.
199 * This function will scan all elements in a particular @lru, calling the
200 * @isolate callback for each of those items, along with the current list
201 * spinlock and a caller-provided opaque. The @isolate callback can choose to
202 * drop the lock internally, but *must* return with the lock held. The callback
203 * will return an enum lru_status telling the @lru infrastructure what to
204 * do with the object being scanned.
206 * Please note that @nr_to_walk does not mean how many objects will be freed,
207 * just how many objects will be scanned.
209 * Return: the number of objects effectively removed from the LRU.
211 unsigned long list_lru_walk_one(struct list_lru
*lru
,
212 int nid
, struct mem_cgroup
*memcg
,
213 list_lru_walk_cb isolate
, void *cb_arg
,
214 unsigned long *nr_to_walk
);
216 * list_lru_walk_one_irq: walk a @lru, isolating and disposing freeable items.
217 * @lru: the lru pointer.
218 * @nid: the node id to scan from.
219 * @memcg: the cgroup to scan from.
220 * @isolate: callback function that is responsible for deciding what to do with
221 * the item currently being scanned
222 * @cb_arg: opaque type that will be passed to @isolate
223 * @nr_to_walk: how many items to scan.
225 * Same as list_lru_walk_one() except that the spinlock is acquired with
228 unsigned long list_lru_walk_one_irq(struct list_lru
*lru
,
229 int nid
, struct mem_cgroup
*memcg
,
230 list_lru_walk_cb isolate
, void *cb_arg
,
231 unsigned long *nr_to_walk
);
232 unsigned long list_lru_walk_node(struct list_lru
*lru
, int nid
,
233 list_lru_walk_cb isolate
, void *cb_arg
,
234 unsigned long *nr_to_walk
);
236 static inline unsigned long
237 list_lru_shrink_walk(struct list_lru
*lru
, struct shrink_control
*sc
,
238 list_lru_walk_cb isolate
, void *cb_arg
)
240 return list_lru_walk_one(lru
, sc
->nid
, sc
->memcg
, isolate
, cb_arg
,
244 static inline unsigned long
245 list_lru_shrink_walk_irq(struct list_lru
*lru
, struct shrink_control
*sc
,
246 list_lru_walk_cb isolate
, void *cb_arg
)
248 return list_lru_walk_one_irq(lru
, sc
->nid
, sc
->memcg
, isolate
, cb_arg
,
252 static inline unsigned long
253 list_lru_walk(struct list_lru
*lru
, list_lru_walk_cb isolate
,
254 void *cb_arg
, unsigned long nr_to_walk
)
259 for_each_node_state(nid
, N_NORMAL_MEMORY
) {
260 isolated
+= list_lru_walk_node(lru
, nid
, isolate
,
261 cb_arg
, &nr_to_walk
);
267 #endif /* _LRU_LIST_H */