Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-2.6-block.git] / include / linux / list_lru.h
CommitLineData
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
16struct mem_cgroup;
17
a38e4082
DC
18/* list_lru_walk_cb has to always return one of those */
19enum 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 31struct list_lru_one {
a38e4082 32 struct list_head list;
2788cf0c 33 /* may become negative during memcg reparenting */
a38e4082 34 long nr_items;
fb56fdf8
KS
35 /* protects all fields above */
36 spinlock_t lock;
60d3fd32
VD
37};
38
d7011070 39struct list_lru_memcg {
5abc1e37 40 struct rcu_head rcu;
6a6b7b77 41 /* array of per cgroup per node lists, indexed by node id */
5abc1e37 42 struct list_lru_one node[];
6a6b7b77
MS
43};
44
60d3fd32 45struct list_lru_node {
60d3fd32
VD
46 /* global list, used for the root cgroup in cgroup aware lrus */
47 struct list_lru_one lru;
fb56fdf8 48 atomic_long_t nr_items;
3b1d58a4
DC
49} ____cacheline_aligned_in_smp;
50
51struct 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
3f28bbe5
KS
59#ifdef CONFIG_LOCKDEP
60 struct lock_class_key *key;
61#endif
a38e4082
DC
62};
63
5ca302c8 64void list_lru_destroy(struct list_lru *lru);
60d3fd32 65int __list_lru_init(struct list_lru *lru, bool memcg_aware,
3f28bbe5 66 struct shrinker *shrinker);
60d3fd32 67
c92e8e10 68#define list_lru_init(lru) \
3f28bbe5 69 __list_lru_init((lru), false, NULL)
c92e8e10 70#define list_lru_init_memcg(lru, shrinker) \
3f28bbe5
KS
71 __list_lru_init((lru), true, shrinker)
72
73static inline int list_lru_init_memcg_key(struct list_lru *lru, struct shrinker *shrinker,
74 struct lock_class_key *key)
75{
76#ifdef CONFIG_LOCKDEP
77 lru->key = key;
78#endif
79 return list_lru_init_memcg(lru, shrinker);
80}
60d3fd32 81
88f2ef73
MS
82int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru,
83 gfp_t gfp);
1f391eb2 84void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *parent);
a38e4082
DC
85
86/**
87 * list_lru_add: add an element to the lru list's tail
7679e140 88 * @lru: the lru pointer
a38e4082 89 * @item: the item to be added.
0a97c01c
NP
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.
a38e4082
DC
92 *
93 * If the element is already part of a list, this function returns doing
da80f4ff
AR
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.
99 *
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.
104 *
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
108 * used.
109 *
110 * You must ensure that the memcg is not freed during this call (e.g., with
111 * rcu or by taking a css refcnt).
a38e4082 112 *
7679e140 113 * Return: true if the list was updated, false otherwise
a38e4082 114 */
0a97c01c
NP
115bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid,
116 struct mem_cgroup *memcg);
a38e4082
DC
117
118/**
0a97c01c
NP
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.
122 *
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.
126 *
da80f4ff 127 * Return: true if the list was updated, false otherwise
0a97c01c
NP
128 */
129bool list_lru_add_obj(struct list_lru *lru, struct list_head *item);
130
131/**
132 * list_lru_del: delete an element from the lru list
7679e140 133 * @lru: the lru pointer
a38e4082 134 * @item: the item to be deleted.
0a97c01c
NP
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.
a38e4082 137 *
7679e140 138 * This function works analogously as list_lru_add() in terms of list
da80f4ff
AR
139 * manipulation.
140 *
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.
146 *
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.
a38e4082 152 *
7679e140 153 * Return: true if the list was updated, false otherwise
a38e4082 154 */
0a97c01c
NP
155bool list_lru_del(struct list_lru *lru, struct list_head *item, int nid,
156 struct mem_cgroup *memcg);
157
158/**
159 * list_lru_del_obj: delete an element from the lru list
160 * @lru: the lru pointer
161 * @item: the item to be deleted.
162 *
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.
166 *
da80f4ff 167 * Return: true if the list was updated, false otherwise.
0a97c01c
NP
168 */
169bool list_lru_del_obj(struct list_lru *lru, struct list_head *item);
a38e4082
DC
170
171/**
60d3fd32 172 * list_lru_count_one: return the number of objects currently held by @lru
a38e4082 173 * @lru: the lru pointer.
6a4f496f 174 * @nid: the node id to count from.
60d3fd32 175 * @memcg: the cgroup to count from.
a38e4082 176 *
7679e140
AS
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.
179 *
180 * Return: 0 for empty lists, otherwise the number of objects
181 * currently held by @lru.
a38e4082 182 */
60d3fd32
VD
183unsigned long list_lru_count_one(struct list_lru *lru,
184 int nid, struct mem_cgroup *memcg);
6a4f496f 185unsigned long list_lru_count_node(struct list_lru *lru, int nid);
503c358c
VD
186
187static inline unsigned long list_lru_shrink_count(struct list_lru *lru,
188 struct shrink_control *sc)
189{
60d3fd32 190 return list_lru_count_one(lru, sc->nid, sc->memcg);
503c358c
VD
191}
192
6a4f496f
GC
193static inline unsigned long list_lru_count(struct list_lru *lru)
194{
195 long count = 0;
196 int nid;
197
ff0b67ef 198 for_each_node_state(nid, N_NORMAL_MEMORY)
6a4f496f
GC
199 count += list_lru_count_node(lru, nid);
200
201 return count;
202}
a38e4082 203
3f97b163
VD
204void list_lru_isolate(struct list_lru_one *list, struct list_head *item);
205void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item,
206 struct list_head *head);
207
208typedef enum lru_status (*list_lru_walk_cb)(struct list_head *item,
da0c0251 209 struct list_lru_one *list, void *cb_arg);
3f97b163 210
a38e4082 211/**
7679e140 212 * list_lru_walk_one: walk a @lru, isolating and disposing freeable items.
a38e4082 213 * @lru: the lru pointer.
6a4f496f 214 * @nid: the node id to scan from.
60d3fd32 215 * @memcg: the cgroup to scan from.
06c88398 216 * @isolate: callback function that is responsible for deciding what to do with
a38e4082
DC
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.
220 *
7679e140 221 * This function will scan all elements in a particular @lru, calling the
a38e4082
DC
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
7679e140 225 * will return an enum lru_status telling the @lru infrastructure what to
a38e4082
DC
226 * do with the object being scanned.
227 *
7679e140 228 * Please note that @nr_to_walk does not mean how many objects will be freed,
a38e4082
DC
229 * just how many objects will be scanned.
230 *
7679e140 231 * Return: the number of objects effectively removed from the LRU.
a38e4082 232 */
60d3fd32
VD
233unsigned 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);
6b51e881 237/**
7679e140 238 * list_lru_walk_one_irq: walk a @lru, isolating and disposing freeable items.
6b51e881
SAS
239 * @lru: the lru pointer.
240 * @nid: the node id to scan from.
241 * @memcg: the cgroup to scan from.
06c88398 242 * @isolate: callback function that is responsible for deciding what to do with
6b51e881
SAS
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.
246 *
7679e140 247 * Same as list_lru_walk_one() except that the spinlock is acquired with
6b51e881
SAS
248 * spin_lock_irq().
249 */
250unsigned 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);
6a4f496f
GC
254unsigned 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);
257
503c358c
VD
258static inline unsigned long
259list_lru_shrink_walk(struct list_lru *lru, struct shrink_control *sc,
260 list_lru_walk_cb isolate, void *cb_arg)
261{
60d3fd32
VD
262 return list_lru_walk_one(lru, sc->nid, sc->memcg, isolate, cb_arg,
263 &sc->nr_to_scan);
503c358c
VD
264}
265
6b51e881
SAS
266static inline unsigned long
267list_lru_shrink_walk_irq(struct list_lru *lru, struct shrink_control *sc,
268 list_lru_walk_cb isolate, void *cb_arg)
269{
270 return list_lru_walk_one_irq(lru, sc->nid, sc->memcg, isolate, cb_arg,
271 &sc->nr_to_scan);
272}
273
6a4f496f
GC
274static inline unsigned long
275list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate,
276 void *cb_arg, unsigned long nr_to_walk)
277{
278 long isolated = 0;
279 int nid;
280
ff0b67ef 281 for_each_node_state(nid, N_NORMAL_MEMORY) {
6a4f496f
GC
282 isolated += list_lru_walk_node(lru, nid, isolate,
283 cb_arg, &nr_to_walk);
284 if (nr_to_walk <= 0)
285 break;
286 }
287 return isolated;
288}
a38e4082 289#endif /* _LRU_LIST_H */