Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
a38e4082 DC |
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 | ||
11 | #include <linux/list.h> | |
3b1d58a4 | 12 | #include <linux/nodemask.h> |
503c358c | 13 | #include <linux/shrinker.h> |
bbca91cc | 14 | #include <linux/xarray.h> |
a38e4082 | 15 | |
60d3fd32 VD |
16 | struct mem_cgroup; |
17 | ||
a38e4082 DC |
18 | /* list_lru_walk_cb has to always return one of those */ |
19 | enum lru_status { | |
20 | LRU_REMOVED, /* item removed from list */ | |
449dd698 JW |
21 | LRU_REMOVED_RETRY, /* item removed, but lock has been |
22 | dropped and reacquired */ | |
a38e4082 DC |
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. */ | |
b49547ad CZ |
27 | LRU_STOP, /* stop lru list walking. May drop the lock |
28 | internally, but has to return locked. */ | |
a38e4082 DC |
29 | }; |
30 | ||
60d3fd32 | 31 | struct list_lru_one { |
a38e4082 | 32 | struct list_head list; |
2788cf0c | 33 | /* may become negative during memcg reparenting */ |
a38e4082 | 34 | long nr_items; |
60d3fd32 VD |
35 | }; |
36 | ||
d7011070 | 37 | struct list_lru_memcg { |
5abc1e37 | 38 | struct rcu_head rcu; |
6a6b7b77 | 39 | /* array of per cgroup per node lists, indexed by node id */ |
5abc1e37 | 40 | struct list_lru_one node[]; |
6a6b7b77 MS |
41 | }; |
42 | ||
60d3fd32 VD |
43 | struct list_lru_node { |
44 | /* protects all lists on the node, including per cgroup */ | |
45 | spinlock_t lock; | |
46 | /* global list, used for the root cgroup in cgroup aware lrus */ | |
47 | struct list_lru_one lru; | |
6a6b7b77 | 48 | long nr_items; |
3b1d58a4 DC |
49 | } ____cacheline_aligned_in_smp; |
50 | ||
51 | struct list_lru { | |
5ca302c8 | 52 | struct list_lru_node *node; |
3a3b7fec | 53 | #ifdef CONFIG_MEMCG |
c0a5b560 | 54 | struct list_head list; |
c92e8e10 | 55 | int shrinker_id; |
3e858996 | 56 | bool memcg_aware; |
bbca91cc | 57 | struct xarray xa; |
c0a5b560 | 58 | #endif |
a38e4082 DC |
59 | }; |
60 | ||
5ca302c8 | 61 | void list_lru_destroy(struct list_lru *lru); |
60d3fd32 | 62 | int __list_lru_init(struct list_lru *lru, bool memcg_aware, |
c92e8e10 | 63 | struct lock_class_key *key, struct shrinker *shrinker); |
60d3fd32 | 64 | |
c92e8e10 KT |
65 | #define list_lru_init(lru) \ |
66 | __list_lru_init((lru), false, NULL, NULL) | |
c92e8e10 KT |
67 | #define list_lru_init_memcg(lru, shrinker) \ |
68 | __list_lru_init((lru), true, NULL, shrinker) | |
60d3fd32 | 69 | |
88f2ef73 MS |
70 | int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru, |
71 | gfp_t gfp); | |
1f391eb2 | 72 | void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *parent); |
a38e4082 DC |
73 | |
74 | /** | |
75 | * list_lru_add: add an element to the lru list's tail | |
7679e140 | 76 | * @lru: the lru pointer |
a38e4082 | 77 | * @item: the item to be added. |
0a97c01c NP |
78 | * @nid: the node id of the sublist to add the item to. |
79 | * @memcg: the cgroup of the sublist to add the item to. | |
a38e4082 DC |
80 | * |
81 | * If the element is already part of a list, this function returns doing | |
82 | * nothing. Therefore the caller does not need to keep state about whether or | |
83 | * not the element already belongs in the list and is allowed to lazy update | |
84 | * it. Note however that this is valid for *a* list, not *this* list. If | |
85 | * the caller organize itself in a way that elements can be in more than | |
86 | * one type of list, it is up to the caller to fully remove the item from | |
87 | * the previous list (with list_lru_del() for instance) before moving it | |
7679e140 | 88 | * to @lru. |
a38e4082 | 89 | * |
7679e140 | 90 | * Return: true if the list was updated, false otherwise |
a38e4082 | 91 | */ |
0a97c01c NP |
92 | bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid, |
93 | struct mem_cgroup *memcg); | |
a38e4082 DC |
94 | |
95 | /** | |
0a97c01c NP |
96 | * list_lru_add_obj: add an element to the lru list's tail |
97 | * @lru: the lru pointer | |
98 | * @item: the item to be added. | |
99 | * | |
100 | * This function is similar to list_lru_add(), but the NUMA node and the | |
101 | * memcg of the sublist is determined by @item list_head. This assumption is | |
102 | * valid for slab objects LRU such as dentries, inodes, etc. | |
103 | * | |
104 | * Return value: true if the list was updated, false otherwise | |
105 | */ | |
106 | bool list_lru_add_obj(struct list_lru *lru, struct list_head *item); | |
107 | ||
108 | /** | |
109 | * list_lru_del: delete an element from the lru list | |
7679e140 | 110 | * @lru: the lru pointer |
a38e4082 | 111 | * @item: the item to be deleted. |
0a97c01c NP |
112 | * @nid: the node id of the sublist to delete the item from. |
113 | * @memcg: the cgroup of the sublist to delete the item from. | |
a38e4082 | 114 | * |
7679e140 | 115 | * This function works analogously as list_lru_add() in terms of list |
a38e4082 | 116 | * manipulation. The comments about an element already pertaining to |
7679e140 | 117 | * a list are also valid for list_lru_del(). |
a38e4082 | 118 | * |
7679e140 | 119 | * Return: true if the list was updated, false otherwise |
a38e4082 | 120 | */ |
0a97c01c NP |
121 | bool list_lru_del(struct list_lru *lru, struct list_head *item, int nid, |
122 | struct mem_cgroup *memcg); | |
123 | ||
124 | /** | |
125 | * list_lru_del_obj: delete an element from the lru list | |
126 | * @lru: the lru pointer | |
127 | * @item: the item to be deleted. | |
128 | * | |
129 | * This function is similar to list_lru_del(), but the NUMA node and the | |
130 | * memcg of the sublist is determined by @item list_head. This assumption is | |
131 | * valid for slab objects LRU such as dentries, inodes, etc. | |
132 | * | |
133 | * Return value: true if the list was updated, false otherwise. | |
134 | */ | |
135 | bool list_lru_del_obj(struct list_lru *lru, struct list_head *item); | |
a38e4082 DC |
136 | |
137 | /** | |
60d3fd32 | 138 | * list_lru_count_one: return the number of objects currently held by @lru |
a38e4082 | 139 | * @lru: the lru pointer. |
6a4f496f | 140 | * @nid: the node id to count from. |
60d3fd32 | 141 | * @memcg: the cgroup to count from. |
a38e4082 | 142 | * |
7679e140 AS |
143 | * There is no guarantee that the list is not updated while the count is being |
144 | * computed. Callers that want such a guarantee need to provide an outer lock. | |
145 | * | |
146 | * Return: 0 for empty lists, otherwise the number of objects | |
147 | * currently held by @lru. | |
a38e4082 | 148 | */ |
60d3fd32 VD |
149 | unsigned long list_lru_count_one(struct list_lru *lru, |
150 | int nid, struct mem_cgroup *memcg); | |
6a4f496f | 151 | unsigned long list_lru_count_node(struct list_lru *lru, int nid); |
503c358c VD |
152 | |
153 | static inline unsigned long list_lru_shrink_count(struct list_lru *lru, | |
154 | struct shrink_control *sc) | |
155 | { | |
60d3fd32 | 156 | return list_lru_count_one(lru, sc->nid, sc->memcg); |
503c358c VD |
157 | } |
158 | ||
6a4f496f GC |
159 | static inline unsigned long list_lru_count(struct list_lru *lru) |
160 | { | |
161 | long count = 0; | |
162 | int nid; | |
163 | ||
ff0b67ef | 164 | for_each_node_state(nid, N_NORMAL_MEMORY) |
6a4f496f GC |
165 | count += list_lru_count_node(lru, nid); |
166 | ||
167 | return count; | |
168 | } | |
a38e4082 | 169 | |
3f97b163 VD |
170 | void list_lru_isolate(struct list_lru_one *list, struct list_head *item); |
171 | void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item, | |
172 | struct list_head *head); | |
173 | ||
174 | typedef enum lru_status (*list_lru_walk_cb)(struct list_head *item, | |
175 | struct list_lru_one *list, spinlock_t *lock, void *cb_arg); | |
176 | ||
a38e4082 | 177 | /** |
7679e140 | 178 | * list_lru_walk_one: walk a @lru, isolating and disposing freeable items. |
a38e4082 | 179 | * @lru: the lru pointer. |
6a4f496f | 180 | * @nid: the node id to scan from. |
60d3fd32 | 181 | * @memcg: the cgroup to scan from. |
06c88398 | 182 | * @isolate: callback function that is responsible for deciding what to do with |
a38e4082 DC |
183 | * the item currently being scanned |
184 | * @cb_arg: opaque type that will be passed to @isolate | |
185 | * @nr_to_walk: how many items to scan. | |
186 | * | |
7679e140 | 187 | * This function will scan all elements in a particular @lru, calling the |
a38e4082 DC |
188 | * @isolate callback for each of those items, along with the current list |
189 | * spinlock and a caller-provided opaque. The @isolate callback can choose to | |
190 | * drop the lock internally, but *must* return with the lock held. The callback | |
7679e140 | 191 | * will return an enum lru_status telling the @lru infrastructure what to |
a38e4082 DC |
192 | * do with the object being scanned. |
193 | * | |
7679e140 | 194 | * Please note that @nr_to_walk does not mean how many objects will be freed, |
a38e4082 DC |
195 | * just how many objects will be scanned. |
196 | * | |
7679e140 | 197 | * Return: the number of objects effectively removed from the LRU. |
a38e4082 | 198 | */ |
60d3fd32 VD |
199 | unsigned long list_lru_walk_one(struct list_lru *lru, |
200 | int nid, struct mem_cgroup *memcg, | |
201 | list_lru_walk_cb isolate, void *cb_arg, | |
202 | unsigned long *nr_to_walk); | |
6b51e881 | 203 | /** |
7679e140 | 204 | * list_lru_walk_one_irq: walk a @lru, isolating and disposing freeable items. |
6b51e881 SAS |
205 | * @lru: the lru pointer. |
206 | * @nid: the node id to scan from. | |
207 | * @memcg: the cgroup to scan from. | |
06c88398 | 208 | * @isolate: callback function that is responsible for deciding what to do with |
6b51e881 SAS |
209 | * the item currently being scanned |
210 | * @cb_arg: opaque type that will be passed to @isolate | |
211 | * @nr_to_walk: how many items to scan. | |
212 | * | |
7679e140 | 213 | * Same as list_lru_walk_one() except that the spinlock is acquired with |
6b51e881 SAS |
214 | * spin_lock_irq(). |
215 | */ | |
216 | unsigned long list_lru_walk_one_irq(struct list_lru *lru, | |
217 | int nid, struct mem_cgroup *memcg, | |
218 | list_lru_walk_cb isolate, void *cb_arg, | |
219 | unsigned long *nr_to_walk); | |
6a4f496f GC |
220 | unsigned long list_lru_walk_node(struct list_lru *lru, int nid, |
221 | list_lru_walk_cb isolate, void *cb_arg, | |
222 | unsigned long *nr_to_walk); | |
223 | ||
503c358c VD |
224 | static inline unsigned long |
225 | list_lru_shrink_walk(struct list_lru *lru, struct shrink_control *sc, | |
226 | list_lru_walk_cb isolate, void *cb_arg) | |
227 | { | |
60d3fd32 VD |
228 | return list_lru_walk_one(lru, sc->nid, sc->memcg, isolate, cb_arg, |
229 | &sc->nr_to_scan); | |
503c358c VD |
230 | } |
231 | ||
6b51e881 SAS |
232 | static inline unsigned long |
233 | list_lru_shrink_walk_irq(struct list_lru *lru, struct shrink_control *sc, | |
234 | list_lru_walk_cb isolate, void *cb_arg) | |
235 | { | |
236 | return list_lru_walk_one_irq(lru, sc->nid, sc->memcg, isolate, cb_arg, | |
237 | &sc->nr_to_scan); | |
238 | } | |
239 | ||
6a4f496f GC |
240 | static inline unsigned long |
241 | list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate, | |
242 | void *cb_arg, unsigned long nr_to_walk) | |
243 | { | |
244 | long isolated = 0; | |
245 | int nid; | |
246 | ||
ff0b67ef | 247 | for_each_node_state(nid, N_NORMAL_MEMORY) { |
6a4f496f GC |
248 | isolated += list_lru_walk_node(lru, nid, isolate, |
249 | cb_arg, &nr_to_walk); | |
250 | if (nr_to_walk <= 0) | |
251 | break; | |
252 | } | |
253 | return isolated; | |
254 | } | |
a38e4082 | 255 | #endif /* _LRU_LIST_H */ |