Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 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> |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 12 | #include <linux/nodemask.h> |
Vladimir Davydov | 503c358 | 2015-02-12 14:58:47 -0800 | [diff] [blame] | 13 | #include <linux/shrinker.h> |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 14 | |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 15 | struct mem_cgroup; |
| 16 | |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 17 | /* list_lru_walk_cb has to always return one of those */ |
| 18 | enum lru_status { |
| 19 | LRU_REMOVED, /* item removed from list */ |
Johannes Weiner | 449dd69 | 2014-04-03 14:47:56 -0700 | [diff] [blame] | 20 | LRU_REMOVED_RETRY, /* item removed, but lock has been |
| 21 | dropped and reacquired */ |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 22 | LRU_ROTATE, /* item referenced, give another pass */ |
| 23 | LRU_SKIP, /* item cannot be locked, skip */ |
| 24 | LRU_RETRY, /* item not freeable. May drop the lock |
| 25 | internally, but has to return locked. */ |
| 26 | }; |
| 27 | |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 28 | struct list_lru_one { |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 29 | struct list_head list; |
Vladimir Davydov | 2788cf0 | 2015-02-12 14:59:38 -0800 | [diff] [blame] | 30 | /* may become negative during memcg reparenting */ |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 31 | long nr_items; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | struct list_lru_memcg { |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 35 | struct rcu_head rcu; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 36 | /* array of per cgroup lists, indexed by memcg_cache_id */ |
| 37 | struct list_lru_one *lru[0]; |
| 38 | }; |
| 39 | |
| 40 | struct list_lru_node { |
| 41 | /* protects all lists on the node, including per cgroup */ |
| 42 | spinlock_t lock; |
| 43 | /* global list, used for the root cgroup in cgroup aware lrus */ |
| 44 | struct list_lru_one lru; |
Kirill Tkhai | 84c07d1 | 2018-08-17 15:47:25 -0700 | [diff] [blame] | 45 | #ifdef CONFIG_MEMCG_KMEM |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 46 | /* for cgroup aware lrus points to per cgroup lists, otherwise NULL */ |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 47 | struct list_lru_memcg __rcu *memcg_lrus; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 48 | #endif |
Sahitya Tummala | 2c80cd5 | 2017-07-10 15:49:57 -0700 | [diff] [blame] | 49 | long nr_items; |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 50 | } ____cacheline_aligned_in_smp; |
| 51 | |
| 52 | struct list_lru { |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 53 | struct list_lru_node *node; |
Kirill Tkhai | 84c07d1 | 2018-08-17 15:47:25 -0700 | [diff] [blame] | 54 | #ifdef CONFIG_MEMCG_KMEM |
Vladimir Davydov | c0a5b56 | 2015-02-12 14:59:07 -0800 | [diff] [blame] | 55 | struct list_head list; |
Kirill Tkhai | c92e8e1 | 2018-08-17 15:47:50 -0700 | [diff] [blame] | 56 | int shrinker_id; |
Jiri Slaby | 3e85899 | 2019-05-31 22:30:26 -0700 | [diff] [blame] | 57 | bool memcg_aware; |
Vladimir Davydov | c0a5b56 | 2015-02-12 14:59:07 -0800 | [diff] [blame] | 58 | #endif |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 59 | }; |
| 60 | |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 61 | void list_lru_destroy(struct list_lru *lru); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 62 | int __list_lru_init(struct list_lru *lru, bool memcg_aware, |
Kirill Tkhai | c92e8e1 | 2018-08-17 15:47:50 -0700 | [diff] [blame] | 63 | struct lock_class_key *key, struct shrinker *shrinker); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 64 | |
Kirill Tkhai | c92e8e1 | 2018-08-17 15:47:50 -0700 | [diff] [blame] | 65 | #define list_lru_init(lru) \ |
| 66 | __list_lru_init((lru), false, NULL, NULL) |
| 67 | #define list_lru_init_key(lru, key) \ |
| 68 | __list_lru_init((lru), false, (key), NULL) |
| 69 | #define list_lru_init_memcg(lru, shrinker) \ |
| 70 | __list_lru_init((lru), true, NULL, shrinker) |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 71 | |
| 72 | int memcg_update_all_list_lrus(int num_memcgs); |
Kirill Tkhai | 9bec5c3 | 2018-08-17 15:47:58 -0700 | [diff] [blame] | 73 | void memcg_drain_all_list_lrus(int src_idx, struct mem_cgroup *dst_memcg); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * list_lru_add: add an element to the lru list's tail |
| 77 | * @list_lru: the lru pointer |
| 78 | * @item: the item to be added. |
| 79 | * |
| 80 | * If the element is already part of a list, this function returns doing |
| 81 | * nothing. Therefore the caller does not need to keep state about whether or |
| 82 | * not the element already belongs in the list and is allowed to lazy update |
| 83 | * it. Note however that this is valid for *a* list, not *this* list. If |
| 84 | * the caller organize itself in a way that elements can be in more than |
| 85 | * one type of list, it is up to the caller to fully remove the item from |
| 86 | * the previous list (with list_lru_del() for instance) before moving it |
| 87 | * to @list_lru |
| 88 | * |
| 89 | * Return value: true if the list was updated, false otherwise |
| 90 | */ |
| 91 | bool list_lru_add(struct list_lru *lru, struct list_head *item); |
| 92 | |
| 93 | /** |
| 94 | * list_lru_del: delete an element to the lru list |
| 95 | * @list_lru: the lru pointer |
| 96 | * @item: the item to be deleted. |
| 97 | * |
| 98 | * This function works analogously as list_lru_add in terms of list |
| 99 | * manipulation. The comments about an element already pertaining to |
| 100 | * a list are also valid for list_lru_del. |
| 101 | * |
| 102 | * Return value: true if the list was updated, false otherwise |
| 103 | */ |
| 104 | bool list_lru_del(struct list_lru *lru, struct list_head *item); |
| 105 | |
| 106 | /** |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 107 | * list_lru_count_one: return the number of objects currently held by @lru |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 108 | * @lru: the lru pointer. |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame] | 109 | * @nid: the node id to count from. |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 110 | * @memcg: the cgroup to count from. |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 111 | * |
| 112 | * Always return a non-negative number, 0 for empty lists. There is no |
| 113 | * guarantee that the list is not updated while the count is being computed. |
| 114 | * Callers that want such a guarantee need to provide an outer lock. |
| 115 | */ |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 116 | unsigned long list_lru_count_one(struct list_lru *lru, |
| 117 | int nid, struct mem_cgroup *memcg); |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame] | 118 | unsigned long list_lru_count_node(struct list_lru *lru, int nid); |
Vladimir Davydov | 503c358 | 2015-02-12 14:58:47 -0800 | [diff] [blame] | 119 | |
| 120 | static inline unsigned long list_lru_shrink_count(struct list_lru *lru, |
| 121 | struct shrink_control *sc) |
| 122 | { |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 123 | return list_lru_count_one(lru, sc->nid, sc->memcg); |
Vladimir Davydov | 503c358 | 2015-02-12 14:58:47 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame] | 126 | static inline unsigned long list_lru_count(struct list_lru *lru) |
| 127 | { |
| 128 | long count = 0; |
| 129 | int nid; |
| 130 | |
Vladimir Davydov | ff0b67e | 2015-02-12 14:59:04 -0800 | [diff] [blame] | 131 | for_each_node_state(nid, N_NORMAL_MEMORY) |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame] | 132 | count += list_lru_count_node(lru, nid); |
| 133 | |
| 134 | return count; |
| 135 | } |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 136 | |
Vladimir Davydov | 3f97b16 | 2015-02-12 14:59:35 -0800 | [diff] [blame] | 137 | void list_lru_isolate(struct list_lru_one *list, struct list_head *item); |
| 138 | void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item, |
| 139 | struct list_head *head); |
| 140 | |
| 141 | typedef enum lru_status (*list_lru_walk_cb)(struct list_head *item, |
| 142 | struct list_lru_one *list, spinlock_t *lock, void *cb_arg); |
| 143 | |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 144 | /** |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 145 | * list_lru_walk_one: walk a list_lru, isolating and disposing freeable items. |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 146 | * @lru: the lru pointer. |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame] | 147 | * @nid: the node id to scan from. |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 148 | * @memcg: the cgroup to scan from. |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 149 | * @isolate: callback function that is resposible for deciding what to do with |
| 150 | * the item currently being scanned |
| 151 | * @cb_arg: opaque type that will be passed to @isolate |
| 152 | * @nr_to_walk: how many items to scan. |
| 153 | * |
| 154 | * This function will scan all elements in a particular list_lru, calling the |
| 155 | * @isolate callback for each of those items, along with the current list |
| 156 | * spinlock and a caller-provided opaque. The @isolate callback can choose to |
| 157 | * drop the lock internally, but *must* return with the lock held. The callback |
| 158 | * will return an enum lru_status telling the list_lru infrastructure what to |
| 159 | * do with the object being scanned. |
| 160 | * |
| 161 | * Please note that nr_to_walk does not mean how many objects will be freed, |
| 162 | * just how many objects will be scanned. |
| 163 | * |
| 164 | * Return value: the number of objects effectively removed from the LRU. |
| 165 | */ |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 166 | unsigned long list_lru_walk_one(struct list_lru *lru, |
| 167 | int nid, struct mem_cgroup *memcg, |
| 168 | list_lru_walk_cb isolate, void *cb_arg, |
| 169 | unsigned long *nr_to_walk); |
Sebastian Andrzej Siewior | 6b51e88 | 2018-08-17 15:49:55 -0700 | [diff] [blame] | 170 | /** |
| 171 | * list_lru_walk_one_irq: walk a list_lru, isolating and disposing freeable items. |
| 172 | * @lru: the lru pointer. |
| 173 | * @nid: the node id to scan from. |
| 174 | * @memcg: the cgroup to scan from. |
| 175 | * @isolate: callback function that is resposible for deciding what to do with |
| 176 | * the item currently being scanned |
| 177 | * @cb_arg: opaque type that will be passed to @isolate |
| 178 | * @nr_to_walk: how many items to scan. |
| 179 | * |
| 180 | * Same as @list_lru_walk_one except that the spinlock is acquired with |
| 181 | * spin_lock_irq(). |
| 182 | */ |
| 183 | unsigned long list_lru_walk_one_irq(struct list_lru *lru, |
| 184 | int nid, struct mem_cgroup *memcg, |
| 185 | list_lru_walk_cb isolate, void *cb_arg, |
| 186 | unsigned long *nr_to_walk); |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame] | 187 | unsigned long list_lru_walk_node(struct list_lru *lru, int nid, |
| 188 | list_lru_walk_cb isolate, void *cb_arg, |
| 189 | unsigned long *nr_to_walk); |
| 190 | |
| 191 | static inline unsigned long |
Vladimir Davydov | 503c358 | 2015-02-12 14:58:47 -0800 | [diff] [blame] | 192 | list_lru_shrink_walk(struct list_lru *lru, struct shrink_control *sc, |
| 193 | list_lru_walk_cb isolate, void *cb_arg) |
| 194 | { |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 195 | return list_lru_walk_one(lru, sc->nid, sc->memcg, isolate, cb_arg, |
| 196 | &sc->nr_to_scan); |
Vladimir Davydov | 503c358 | 2015-02-12 14:58:47 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static inline unsigned long |
Sebastian Andrzej Siewior | 6b51e88 | 2018-08-17 15:49:55 -0700 | [diff] [blame] | 200 | list_lru_shrink_walk_irq(struct list_lru *lru, struct shrink_control *sc, |
| 201 | list_lru_walk_cb isolate, void *cb_arg) |
| 202 | { |
| 203 | return list_lru_walk_one_irq(lru, sc->nid, sc->memcg, isolate, cb_arg, |
| 204 | &sc->nr_to_scan); |
| 205 | } |
| 206 | |
| 207 | static inline unsigned long |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame] | 208 | list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate, |
| 209 | void *cb_arg, unsigned long nr_to_walk) |
| 210 | { |
| 211 | long isolated = 0; |
| 212 | int nid; |
| 213 | |
Vladimir Davydov | ff0b67e | 2015-02-12 14:59:04 -0800 | [diff] [blame] | 214 | for_each_node_state(nid, N_NORMAL_MEMORY) { |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame] | 215 | isolated += list_lru_walk_node(lru, nid, isolate, |
| 216 | cb_arg, &nr_to_walk); |
| 217 | if (nr_to_walk <= 0) |
| 218 | break; |
| 219 | } |
| 220 | return isolated; |
| 221 | } |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 222 | #endif /* _LRU_LIST_H */ |