| 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 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> |
| 12 | #include <linux/nodemask.h> |
| 13 | #include <linux/shrinker.h> |
| 14 | #include <linux/xarray.h> |
| 15 | |
| 16 | struct mem_cgroup; |
| 17 | |
| 18 | /* list_lru_walk_cb has to always return one of those */ |
| 19 | enum lru_status { |
| 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. */ |
| 29 | }; |
| 30 | |
| 31 | struct list_lru_one { |
| 32 | struct list_head list; |
| 33 | /* may become negative during memcg reparenting */ |
| 34 | long nr_items; |
| 35 | /* protects all fields above */ |
| 36 | spinlock_t lock; |
| 37 | }; |
| 38 | |
| 39 | struct list_lru_memcg { |
| 40 | struct rcu_head rcu; |
| 41 | /* array of per cgroup per node lists, indexed by node id */ |
| 42 | struct list_lru_one node[]; |
| 43 | }; |
| 44 | |
| 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; |
| 50 | |
| 51 | struct list_lru { |
| 52 | struct list_lru_node *node; |
| 53 | #ifdef CONFIG_MEMCG |
| 54 | struct list_head list; |
| 55 | int shrinker_id; |
| 56 | bool memcg_aware; |
| 57 | struct xarray xa; |
| 58 | #endif |
| 59 | #ifdef CONFIG_LOCKDEP |
| 60 | struct lock_class_key *key; |
| 61 | #endif |
| 62 | }; |
| 63 | |
| 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); |
| 67 | |
| 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) |
| 72 | |
| 73 | static 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 | } |
| 81 | |
| 82 | int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru, |
| 83 | gfp_t gfp); |
| 84 | void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *parent); |
| 85 | |
| 86 | /** |
| 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. |
| 92 | * |
| 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. |
| 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). |
| 112 | * |
| 113 | * Return: true if the list was updated, false otherwise |
| 114 | */ |
| 115 | bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid, |
| 116 | struct mem_cgroup *memcg); |
| 117 | |
| 118 | /** |
| 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 | * |
| 127 | * Return: true if the list was updated, false otherwise |
| 128 | */ |
| 129 | bool 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 |
| 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. |
| 137 | * |
| 138 | * This function works analogously as list_lru_add() in terms of list |
| 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. |
| 152 | * |
| 153 | * Return: true if the list was updated, false otherwise |
| 154 | */ |
| 155 | bool 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 | * |
| 167 | * Return: true if the list was updated, false otherwise. |
| 168 | */ |
| 169 | bool list_lru_del_obj(struct list_lru *lru, struct list_head *item); |
| 170 | |
| 171 | /** |
| 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. |
| 176 | * |
| 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. |
| 182 | */ |
| 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); |
| 186 | |
| 187 | static inline unsigned long list_lru_shrink_count(struct list_lru *lru, |
| 188 | struct shrink_control *sc) |
| 189 | { |
| 190 | return list_lru_count_one(lru, sc->nid, sc->memcg); |
| 191 | } |
| 192 | |
| 193 | static inline unsigned long list_lru_count(struct list_lru *lru) |
| 194 | { |
| 195 | long count = 0; |
| 196 | int nid; |
| 197 | |
| 198 | for_each_node_state(nid, N_NORMAL_MEMORY) |
| 199 | count += list_lru_count_node(lru, nid); |
| 200 | |
| 201 | return count; |
| 202 | } |
| 203 | |
| 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); |
| 207 | |
| 208 | typedef enum lru_status (*list_lru_walk_cb)(struct list_head *item, |
| 209 | struct list_lru_one *list, void *cb_arg); |
| 210 | |
| 211 | /** |
| 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. |
| 220 | * |
| 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. |
| 227 | * |
| 228 | * Please note that @nr_to_walk does not mean how many objects will be freed, |
| 229 | * just how many objects will be scanned. |
| 230 | * |
| 231 | * Return: the number of objects effectively removed from the LRU. |
| 232 | */ |
| 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); |
| 237 | /** |
| 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. |
| 246 | * |
| 247 | * Same as list_lru_walk_one() except that the spinlock is acquired with |
| 248 | * spin_lock_irq(). |
| 249 | */ |
| 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); |
| 257 | |
| 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) |
| 261 | { |
| 262 | return list_lru_walk_one(lru, sc->nid, sc->memcg, isolate, cb_arg, |
| 263 | &sc->nr_to_scan); |
| 264 | } |
| 265 | |
| 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) |
| 269 | { |
| 270 | return list_lru_walk_one_irq(lru, sc->nid, sc->memcg, isolate, cb_arg, |
| 271 | &sc->nr_to_scan); |
| 272 | } |
| 273 | |
| 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) |
| 277 | { |
| 278 | long isolated = 0; |
| 279 | int nid; |
| 280 | |
| 281 | for_each_node_state(nid, N_NORMAL_MEMORY) { |
| 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 | } |
| 289 | #endif /* _LRU_LIST_H */ |