Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 Red Hat, Inc. and Parallels Inc. All rights reserved. |
| 3 | * Authors: David Chinner and Glauber Costa |
| 4 | * |
| 5 | * Generic LRU infrastructure |
| 6 | */ |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/module.h> |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 9 | #include <linux/mm.h> |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 10 | #include <linux/list_lru.h> |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 11 | #include <linux/slab.h> |
Vladimir Davydov | c0a5b56 | 2015-02-12 14:59:07 -0800 | [diff] [blame] | 12 | #include <linux/mutex.h> |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 13 | #include <linux/memcontrol.h> |
Vladimir Davydov | c0a5b56 | 2015-02-12 14:59:07 -0800 | [diff] [blame] | 14 | |
Johannes Weiner | 127424c | 2016-01-20 15:02:32 -0800 | [diff] [blame] | 15 | #if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB) |
Vladimir Davydov | c0a5b56 | 2015-02-12 14:59:07 -0800 | [diff] [blame] | 16 | static LIST_HEAD(list_lrus); |
| 17 | static DEFINE_MUTEX(list_lrus_mutex); |
| 18 | |
| 19 | static void list_lru_register(struct list_lru *lru) |
| 20 | { |
| 21 | mutex_lock(&list_lrus_mutex); |
| 22 | list_add(&lru->list, &list_lrus); |
| 23 | mutex_unlock(&list_lrus_mutex); |
| 24 | } |
| 25 | |
| 26 | static void list_lru_unregister(struct list_lru *lru) |
| 27 | { |
| 28 | mutex_lock(&list_lrus_mutex); |
| 29 | list_del(&lru->list); |
| 30 | mutex_unlock(&list_lrus_mutex); |
| 31 | } |
Vladimir Davydov | c0a5b56 | 2015-02-12 14:59:07 -0800 | [diff] [blame] | 32 | |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 33 | static inline bool list_lru_memcg_aware(struct list_lru *lru) |
| 34 | { |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 35 | /* |
| 36 | * This needs node 0 to be always present, even |
| 37 | * in the systems supporting sparse numa ids. |
| 38 | */ |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 39 | return !!lru->node[0].memcg_lrus; |
| 40 | } |
| 41 | |
| 42 | static inline struct list_lru_one * |
| 43 | list_lru_from_memcg_idx(struct list_lru_node *nlru, int idx) |
| 44 | { |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 45 | struct list_lru_memcg *memcg_lrus; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 46 | /* |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 47 | * Either lock or RCU protects the array of per cgroup lists |
| 48 | * from relocation (see memcg_update_list_lru_node). |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 49 | */ |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 50 | memcg_lrus = rcu_dereference_check(nlru->memcg_lrus, |
| 51 | lockdep_is_held(&nlru->lock)); |
| 52 | if (memcg_lrus && idx >= 0) |
| 53 | return memcg_lrus->lru[idx]; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 54 | return &nlru->lru; |
| 55 | } |
| 56 | |
Vladimir Davydov | df40655 | 2015-11-05 18:49:04 -0800 | [diff] [blame] | 57 | static __always_inline struct mem_cgroup *mem_cgroup_from_kmem(void *ptr) |
| 58 | { |
| 59 | struct page *page; |
| 60 | |
| 61 | if (!memcg_kmem_enabled()) |
| 62 | return NULL; |
| 63 | page = virt_to_head_page(ptr); |
| 64 | return page->mem_cgroup; |
| 65 | } |
| 66 | |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 67 | static inline struct list_lru_one * |
| 68 | list_lru_from_kmem(struct list_lru_node *nlru, void *ptr) |
| 69 | { |
| 70 | struct mem_cgroup *memcg; |
| 71 | |
| 72 | if (!nlru->memcg_lrus) |
| 73 | return &nlru->lru; |
| 74 | |
| 75 | memcg = mem_cgroup_from_kmem(ptr); |
| 76 | if (!memcg) |
| 77 | return &nlru->lru; |
| 78 | |
| 79 | return list_lru_from_memcg_idx(nlru, memcg_cache_id(memcg)); |
| 80 | } |
| 81 | #else |
Kirill Tkhai | e029523 | 2018-08-17 15:47:21 -0700 | [diff] [blame^] | 82 | static void list_lru_register(struct list_lru *lru) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | static void list_lru_unregister(struct list_lru *lru) |
| 87 | { |
| 88 | } |
| 89 | |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 90 | static inline bool list_lru_memcg_aware(struct list_lru *lru) |
| 91 | { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | static inline struct list_lru_one * |
| 96 | list_lru_from_memcg_idx(struct list_lru_node *nlru, int idx) |
| 97 | { |
| 98 | return &nlru->lru; |
| 99 | } |
| 100 | |
| 101 | static inline struct list_lru_one * |
| 102 | list_lru_from_kmem(struct list_lru_node *nlru, void *ptr) |
| 103 | { |
| 104 | return &nlru->lru; |
| 105 | } |
Johannes Weiner | 127424c | 2016-01-20 15:02:32 -0800 | [diff] [blame] | 106 | #endif /* CONFIG_MEMCG && !CONFIG_SLOB */ |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 107 | |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 108 | bool list_lru_add(struct list_lru *lru, struct list_head *item) |
| 109 | { |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 110 | int nid = page_to_nid(virt_to_page(item)); |
| 111 | struct list_lru_node *nlru = &lru->node[nid]; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 112 | struct list_lru_one *l; |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 113 | |
| 114 | spin_lock(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 115 | if (list_empty(item)) { |
Jeff Layton | 26f5d76 | 2015-09-08 15:03:44 -0700 | [diff] [blame] | 116 | l = list_lru_from_kmem(nlru, item); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 117 | list_add_tail(item, &l->list); |
| 118 | l->nr_items++; |
Sahitya Tummala | 2c80cd5 | 2017-07-10 15:49:57 -0700 | [diff] [blame] | 119 | nlru->nr_items++; |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 120 | spin_unlock(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 121 | return true; |
| 122 | } |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 123 | spin_unlock(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | EXPORT_SYMBOL_GPL(list_lru_add); |
| 127 | |
| 128 | bool list_lru_del(struct list_lru *lru, struct list_head *item) |
| 129 | { |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 130 | int nid = page_to_nid(virt_to_page(item)); |
| 131 | struct list_lru_node *nlru = &lru->node[nid]; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 132 | struct list_lru_one *l; |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 133 | |
| 134 | spin_lock(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 135 | if (!list_empty(item)) { |
Jeff Layton | 26f5d76 | 2015-09-08 15:03:44 -0700 | [diff] [blame] | 136 | l = list_lru_from_kmem(nlru, item); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 137 | list_del_init(item); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 138 | l->nr_items--; |
Sahitya Tummala | 2c80cd5 | 2017-07-10 15:49:57 -0700 | [diff] [blame] | 139 | nlru->nr_items--; |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 140 | spin_unlock(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 141 | return true; |
| 142 | } |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 143 | spin_unlock(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 144 | return false; |
| 145 | } |
| 146 | EXPORT_SYMBOL_GPL(list_lru_del); |
| 147 | |
Vladimir Davydov | 3f97b16 | 2015-02-12 14:59:35 -0800 | [diff] [blame] | 148 | void list_lru_isolate(struct list_lru_one *list, struct list_head *item) |
| 149 | { |
| 150 | list_del_init(item); |
| 151 | list->nr_items--; |
| 152 | } |
| 153 | EXPORT_SYMBOL_GPL(list_lru_isolate); |
| 154 | |
| 155 | void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item, |
| 156 | struct list_head *head) |
| 157 | { |
| 158 | list_move(item, head); |
| 159 | list->nr_items--; |
| 160 | } |
| 161 | EXPORT_SYMBOL_GPL(list_lru_isolate_move); |
| 162 | |
Andrew Morton | 930eaac | 2018-08-17 15:46:11 -0700 | [diff] [blame] | 163 | unsigned long list_lru_count_one(struct list_lru *lru, |
| 164 | int nid, struct mem_cgroup *memcg) |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 165 | { |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame] | 166 | struct list_lru_node *nlru = &lru->node[nid]; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 167 | struct list_lru_one *l; |
| 168 | unsigned long count; |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 169 | |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 170 | rcu_read_lock(); |
Andrew Morton | 930eaac | 2018-08-17 15:46:11 -0700 | [diff] [blame] | 171 | l = list_lru_from_memcg_idx(nlru, memcg_cache_id(memcg)); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 172 | count = l->nr_items; |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 173 | rcu_read_unlock(); |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 174 | |
| 175 | return count; |
| 176 | } |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 177 | EXPORT_SYMBOL_GPL(list_lru_count_one); |
| 178 | |
| 179 | unsigned long list_lru_count_node(struct list_lru *lru, int nid) |
| 180 | { |
Sahitya Tummala | 2c80cd5 | 2017-07-10 15:49:57 -0700 | [diff] [blame] | 181 | struct list_lru_node *nlru; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 182 | |
Sahitya Tummala | 2c80cd5 | 2017-07-10 15:49:57 -0700 | [diff] [blame] | 183 | nlru = &lru->node[nid]; |
| 184 | return nlru->nr_items; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 185 | } |
Glauber Costa | 6a4f496 | 2013-08-28 10:18:02 +1000 | [diff] [blame] | 186 | EXPORT_SYMBOL_GPL(list_lru_count_node); |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 187 | |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 188 | static unsigned long |
| 189 | __list_lru_walk_one(struct list_lru *lru, int nid, int memcg_idx, |
| 190 | list_lru_walk_cb isolate, void *cb_arg, |
| 191 | unsigned long *nr_to_walk) |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 192 | { |
| 193 | |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 194 | struct list_lru_node *nlru = &lru->node[nid]; |
| 195 | struct list_lru_one *l; |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 196 | struct list_head *item, *n; |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 197 | unsigned long isolated = 0; |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 198 | |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 199 | spin_lock(&nlru->lock); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 200 | l = list_lru_from_memcg_idx(nlru, memcg_idx); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 201 | restart: |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 202 | list_for_each_safe(item, n, &l->list) { |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 203 | enum lru_status ret; |
Dave Chinner | 5cedf721 | 2013-08-28 10:18:01 +1000 | [diff] [blame] | 204 | |
| 205 | /* |
| 206 | * decrement nr_to_walk first so that we don't livelock if we |
| 207 | * get stuck on large numbesr of LRU_RETRY items |
| 208 | */ |
Russell King | c56b097 | 2013-10-30 14:16:16 +0000 | [diff] [blame] | 209 | if (!*nr_to_walk) |
Dave Chinner | 5cedf721 | 2013-08-28 10:18:01 +1000 | [diff] [blame] | 210 | break; |
Russell King | c56b097 | 2013-10-30 14:16:16 +0000 | [diff] [blame] | 211 | --*nr_to_walk; |
Dave Chinner | 5cedf721 | 2013-08-28 10:18:01 +1000 | [diff] [blame] | 212 | |
Vladimir Davydov | 3f97b16 | 2015-02-12 14:59:35 -0800 | [diff] [blame] | 213 | ret = isolate(item, l, &nlru->lock, cb_arg); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 214 | switch (ret) { |
Johannes Weiner | 449dd69 | 2014-04-03 14:47:56 -0700 | [diff] [blame] | 215 | case LRU_REMOVED_RETRY: |
| 216 | assert_spin_locked(&nlru->lock); |
Gustavo A. R. Silva | 5b568ac | 2017-11-15 17:38:49 -0800 | [diff] [blame] | 217 | /* fall through */ |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 218 | case LRU_REMOVED: |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 219 | isolated++; |
Sahitya Tummala | 2c80cd5 | 2017-07-10 15:49:57 -0700 | [diff] [blame] | 220 | nlru->nr_items--; |
Johannes Weiner | 449dd69 | 2014-04-03 14:47:56 -0700 | [diff] [blame] | 221 | /* |
| 222 | * If the lru lock has been dropped, our list |
| 223 | * traversal is now invalid and so we have to |
| 224 | * restart from scratch. |
| 225 | */ |
| 226 | if (ret == LRU_REMOVED_RETRY) |
| 227 | goto restart; |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 228 | break; |
| 229 | case LRU_ROTATE: |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 230 | list_move_tail(item, &l->list); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 231 | break; |
| 232 | case LRU_SKIP: |
| 233 | break; |
| 234 | case LRU_RETRY: |
Dave Chinner | 5cedf721 | 2013-08-28 10:18:01 +1000 | [diff] [blame] | 235 | /* |
| 236 | * The lru lock has been dropped, our list traversal is |
| 237 | * now invalid and so we have to restart from scratch. |
| 238 | */ |
Johannes Weiner | 449dd69 | 2014-04-03 14:47:56 -0700 | [diff] [blame] | 239 | assert_spin_locked(&nlru->lock); |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 240 | goto restart; |
| 241 | default: |
| 242 | BUG(); |
| 243 | } |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 244 | } |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 245 | |
| 246 | spin_unlock(&nlru->lock); |
| 247 | return isolated; |
| 248 | } |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 249 | |
| 250 | unsigned long |
| 251 | list_lru_walk_one(struct list_lru *lru, int nid, struct mem_cgroup *memcg, |
| 252 | list_lru_walk_cb isolate, void *cb_arg, |
| 253 | unsigned long *nr_to_walk) |
| 254 | { |
| 255 | return __list_lru_walk_one(lru, nid, memcg_cache_id(memcg), |
| 256 | isolate, cb_arg, nr_to_walk); |
| 257 | } |
| 258 | EXPORT_SYMBOL_GPL(list_lru_walk_one); |
| 259 | |
| 260 | unsigned long list_lru_walk_node(struct list_lru *lru, int nid, |
| 261 | list_lru_walk_cb isolate, void *cb_arg, |
| 262 | unsigned long *nr_to_walk) |
| 263 | { |
| 264 | long isolated = 0; |
| 265 | int memcg_idx; |
| 266 | |
| 267 | isolated += __list_lru_walk_one(lru, nid, -1, isolate, cb_arg, |
| 268 | nr_to_walk); |
| 269 | if (*nr_to_walk > 0 && list_lru_memcg_aware(lru)) { |
| 270 | for_each_memcg_cache_index(memcg_idx) { |
| 271 | isolated += __list_lru_walk_one(lru, nid, memcg_idx, |
| 272 | isolate, cb_arg, nr_to_walk); |
| 273 | if (*nr_to_walk <= 0) |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | return isolated; |
| 278 | } |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 279 | EXPORT_SYMBOL_GPL(list_lru_walk_node); |
| 280 | |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 281 | static void init_one_lru(struct list_lru_one *l) |
| 282 | { |
| 283 | INIT_LIST_HEAD(&l->list); |
| 284 | l->nr_items = 0; |
| 285 | } |
| 286 | |
Johannes Weiner | 127424c | 2016-01-20 15:02:32 -0800 | [diff] [blame] | 287 | #if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB) |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 288 | static void __memcg_destroy_list_lru_node(struct list_lru_memcg *memcg_lrus, |
| 289 | int begin, int end) |
| 290 | { |
| 291 | int i; |
| 292 | |
| 293 | for (i = begin; i < end; i++) |
| 294 | kfree(memcg_lrus->lru[i]); |
| 295 | } |
| 296 | |
| 297 | static int __memcg_init_list_lru_node(struct list_lru_memcg *memcg_lrus, |
| 298 | int begin, int end) |
| 299 | { |
| 300 | int i; |
| 301 | |
| 302 | for (i = begin; i < end; i++) { |
| 303 | struct list_lru_one *l; |
| 304 | |
| 305 | l = kmalloc(sizeof(struct list_lru_one), GFP_KERNEL); |
| 306 | if (!l) |
| 307 | goto fail; |
| 308 | |
| 309 | init_one_lru(l); |
| 310 | memcg_lrus->lru[i] = l; |
| 311 | } |
| 312 | return 0; |
| 313 | fail: |
| 314 | __memcg_destroy_list_lru_node(memcg_lrus, begin, i - 1); |
| 315 | return -ENOMEM; |
| 316 | } |
| 317 | |
| 318 | static int memcg_init_list_lru_node(struct list_lru_node *nlru) |
| 319 | { |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 320 | struct list_lru_memcg *memcg_lrus; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 321 | int size = memcg_nr_cache_ids; |
| 322 | |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 323 | memcg_lrus = kvmalloc(sizeof(*memcg_lrus) + |
| 324 | size * sizeof(void *), GFP_KERNEL); |
| 325 | if (!memcg_lrus) |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 326 | return -ENOMEM; |
| 327 | |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 328 | if (__memcg_init_list_lru_node(memcg_lrus, 0, size)) { |
| 329 | kvfree(memcg_lrus); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 330 | return -ENOMEM; |
| 331 | } |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 332 | RCU_INIT_POINTER(nlru->memcg_lrus, memcg_lrus); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | static void memcg_destroy_list_lru_node(struct list_lru_node *nlru) |
| 338 | { |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 339 | struct list_lru_memcg *memcg_lrus; |
| 340 | /* |
| 341 | * This is called when shrinker has already been unregistered, |
| 342 | * and nobody can use it. So, there is no need to use kvfree_rcu(). |
| 343 | */ |
| 344 | memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus, true); |
| 345 | __memcg_destroy_list_lru_node(memcg_lrus, 0, memcg_nr_cache_ids); |
| 346 | kvfree(memcg_lrus); |
| 347 | } |
| 348 | |
| 349 | static void kvfree_rcu(struct rcu_head *head) |
| 350 | { |
| 351 | struct list_lru_memcg *mlru; |
| 352 | |
| 353 | mlru = container_of(head, struct list_lru_memcg, rcu); |
| 354 | kvfree(mlru); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | static int memcg_update_list_lru_node(struct list_lru_node *nlru, |
| 358 | int old_size, int new_size) |
| 359 | { |
| 360 | struct list_lru_memcg *old, *new; |
| 361 | |
| 362 | BUG_ON(old_size > new_size); |
| 363 | |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 364 | old = rcu_dereference_protected(nlru->memcg_lrus, |
| 365 | lockdep_is_held(&list_lrus_mutex)); |
| 366 | new = kvmalloc(sizeof(*new) + new_size * sizeof(void *), GFP_KERNEL); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 367 | if (!new) |
| 368 | return -ENOMEM; |
| 369 | |
| 370 | if (__memcg_init_list_lru_node(new, old_size, new_size)) { |
Johannes Weiner | f80c7da | 2017-10-03 16:16:10 -0700 | [diff] [blame] | 371 | kvfree(new); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 372 | return -ENOMEM; |
| 373 | } |
| 374 | |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 375 | memcpy(&new->lru, &old->lru, old_size * sizeof(void *)); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 376 | |
| 377 | /* |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 378 | * The locking below allows readers that hold nlru->lock avoid taking |
| 379 | * rcu_read_lock (see list_lru_from_memcg_idx). |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 380 | * |
| 381 | * Since list_lru_{add,del} may be called under an IRQ-safe lock, |
| 382 | * we have to use IRQ-safe primitives here to avoid deadlock. |
| 383 | */ |
| 384 | spin_lock_irq(&nlru->lock); |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 385 | rcu_assign_pointer(nlru->memcg_lrus, new); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 386 | spin_unlock_irq(&nlru->lock); |
| 387 | |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 388 | call_rcu(&old->rcu, kvfree_rcu); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | static void memcg_cancel_update_list_lru_node(struct list_lru_node *nlru, |
| 393 | int old_size, int new_size) |
| 394 | { |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 395 | struct list_lru_memcg *memcg_lrus; |
| 396 | |
| 397 | memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus, |
| 398 | lockdep_is_held(&list_lrus_mutex)); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 399 | /* do not bother shrinking the array back to the old size, because we |
| 400 | * cannot handle allocation failures here */ |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 401 | __memcg_destroy_list_lru_node(memcg_lrus, old_size, new_size); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware) |
| 405 | { |
| 406 | int i; |
| 407 | |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 408 | if (!memcg_aware) |
| 409 | return 0; |
| 410 | |
| 411 | for_each_node(i) { |
| 412 | if (memcg_init_list_lru_node(&lru->node[i])) |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 413 | goto fail; |
| 414 | } |
| 415 | return 0; |
| 416 | fail: |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 417 | for (i = i - 1; i >= 0; i--) { |
| 418 | if (!lru->node[i].memcg_lrus) |
| 419 | continue; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 420 | memcg_destroy_list_lru_node(&lru->node[i]); |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 421 | } |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 422 | return -ENOMEM; |
| 423 | } |
| 424 | |
| 425 | static void memcg_destroy_list_lru(struct list_lru *lru) |
| 426 | { |
| 427 | int i; |
| 428 | |
| 429 | if (!list_lru_memcg_aware(lru)) |
| 430 | return; |
| 431 | |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 432 | for_each_node(i) |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 433 | memcg_destroy_list_lru_node(&lru->node[i]); |
| 434 | } |
| 435 | |
| 436 | static int memcg_update_list_lru(struct list_lru *lru, |
| 437 | int old_size, int new_size) |
| 438 | { |
| 439 | int i; |
| 440 | |
| 441 | if (!list_lru_memcg_aware(lru)) |
| 442 | return 0; |
| 443 | |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 444 | for_each_node(i) { |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 445 | if (memcg_update_list_lru_node(&lru->node[i], |
| 446 | old_size, new_size)) |
| 447 | goto fail; |
| 448 | } |
| 449 | return 0; |
| 450 | fail: |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 451 | for (i = i - 1; i >= 0; i--) { |
| 452 | if (!lru->node[i].memcg_lrus) |
| 453 | continue; |
| 454 | |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 455 | memcg_cancel_update_list_lru_node(&lru->node[i], |
| 456 | old_size, new_size); |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 457 | } |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 458 | return -ENOMEM; |
| 459 | } |
| 460 | |
| 461 | static void memcg_cancel_update_list_lru(struct list_lru *lru, |
| 462 | int old_size, int new_size) |
| 463 | { |
| 464 | int i; |
| 465 | |
| 466 | if (!list_lru_memcg_aware(lru)) |
| 467 | return; |
| 468 | |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 469 | for_each_node(i) |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 470 | memcg_cancel_update_list_lru_node(&lru->node[i], |
| 471 | old_size, new_size); |
| 472 | } |
| 473 | |
| 474 | int memcg_update_all_list_lrus(int new_size) |
| 475 | { |
| 476 | int ret = 0; |
| 477 | struct list_lru *lru; |
| 478 | int old_size = memcg_nr_cache_ids; |
| 479 | |
| 480 | mutex_lock(&list_lrus_mutex); |
| 481 | list_for_each_entry(lru, &list_lrus, list) { |
| 482 | ret = memcg_update_list_lru(lru, old_size, new_size); |
| 483 | if (ret) |
| 484 | goto fail; |
| 485 | } |
| 486 | out: |
| 487 | mutex_unlock(&list_lrus_mutex); |
| 488 | return ret; |
| 489 | fail: |
| 490 | list_for_each_entry_continue_reverse(lru, &list_lrus, list) |
| 491 | memcg_cancel_update_list_lru(lru, old_size, new_size); |
| 492 | goto out; |
| 493 | } |
Vladimir Davydov | 2788cf0 | 2015-02-12 14:59:38 -0800 | [diff] [blame] | 494 | |
| 495 | static void memcg_drain_list_lru_node(struct list_lru_node *nlru, |
| 496 | int src_idx, int dst_idx) |
| 497 | { |
| 498 | struct list_lru_one *src, *dst; |
| 499 | |
| 500 | /* |
| 501 | * Since list_lru_{add,del} may be called under an IRQ-safe lock, |
| 502 | * we have to use IRQ-safe primitives here to avoid deadlock. |
| 503 | */ |
| 504 | spin_lock_irq(&nlru->lock); |
| 505 | |
| 506 | src = list_lru_from_memcg_idx(nlru, src_idx); |
| 507 | dst = list_lru_from_memcg_idx(nlru, dst_idx); |
| 508 | |
| 509 | list_splice_init(&src->list, &dst->list); |
| 510 | dst->nr_items += src->nr_items; |
| 511 | src->nr_items = 0; |
| 512 | |
| 513 | spin_unlock_irq(&nlru->lock); |
| 514 | } |
| 515 | |
| 516 | static void memcg_drain_list_lru(struct list_lru *lru, |
| 517 | int src_idx, int dst_idx) |
| 518 | { |
| 519 | int i; |
| 520 | |
| 521 | if (!list_lru_memcg_aware(lru)) |
| 522 | return; |
| 523 | |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 524 | for_each_node(i) |
Vladimir Davydov | 2788cf0 | 2015-02-12 14:59:38 -0800 | [diff] [blame] | 525 | memcg_drain_list_lru_node(&lru->node[i], src_idx, dst_idx); |
| 526 | } |
| 527 | |
| 528 | void memcg_drain_all_list_lrus(int src_idx, int dst_idx) |
| 529 | { |
| 530 | struct list_lru *lru; |
| 531 | |
| 532 | mutex_lock(&list_lrus_mutex); |
| 533 | list_for_each_entry(lru, &list_lrus, list) |
| 534 | memcg_drain_list_lru(lru, src_idx, dst_idx); |
| 535 | mutex_unlock(&list_lrus_mutex); |
| 536 | } |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 537 | #else |
| 538 | static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware) |
| 539 | { |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | static void memcg_destroy_list_lru(struct list_lru *lru) |
| 544 | { |
| 545 | } |
Johannes Weiner | 127424c | 2016-01-20 15:02:32 -0800 | [diff] [blame] | 546 | #endif /* CONFIG_MEMCG && !CONFIG_SLOB */ |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 547 | |
| 548 | int __list_lru_init(struct list_lru *lru, bool memcg_aware, |
| 549 | struct lock_class_key *key) |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 550 | { |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 551 | int i; |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 552 | size_t size = sizeof(*lru->node) * nr_node_ids; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 553 | int err = -ENOMEM; |
| 554 | |
| 555 | memcg_get_cache_ids(); |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 556 | |
| 557 | lru->node = kzalloc(size, GFP_KERNEL); |
| 558 | if (!lru->node) |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 559 | goto out; |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 560 | |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 561 | for_each_node(i) { |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 562 | spin_lock_init(&lru->node[i].lock); |
Johannes Weiner | 449dd69 | 2014-04-03 14:47:56 -0700 | [diff] [blame] | 563 | if (key) |
| 564 | lockdep_set_class(&lru->node[i].lock, key); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 565 | init_one_lru(&lru->node[i].lru); |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 566 | } |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 567 | |
| 568 | err = memcg_init_list_lru(lru, memcg_aware); |
| 569 | if (err) { |
| 570 | kfree(lru->node); |
Alexander Polakov | 1bc11d7 | 2016-10-27 17:46:27 -0700 | [diff] [blame] | 571 | /* Do this so a list_lru_destroy() doesn't crash: */ |
| 572 | lru->node = NULL; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 573 | goto out; |
| 574 | } |
| 575 | |
Vladimir Davydov | c0a5b56 | 2015-02-12 14:59:07 -0800 | [diff] [blame] | 576 | list_lru_register(lru); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 577 | out: |
| 578 | memcg_put_cache_ids(); |
| 579 | return err; |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 580 | } |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 581 | EXPORT_SYMBOL_GPL(__list_lru_init); |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 582 | |
| 583 | void list_lru_destroy(struct list_lru *lru) |
| 584 | { |
Vladimir Davydov | c0a5b56 | 2015-02-12 14:59:07 -0800 | [diff] [blame] | 585 | /* Already destroyed or not yet initialized? */ |
| 586 | if (!lru->node) |
| 587 | return; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 588 | |
| 589 | memcg_get_cache_ids(); |
| 590 | |
Vladimir Davydov | c0a5b56 | 2015-02-12 14:59:07 -0800 | [diff] [blame] | 591 | list_lru_unregister(lru); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 592 | |
| 593 | memcg_destroy_list_lru(lru); |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 594 | kfree(lru->node); |
Vladimir Davydov | c0a5b56 | 2015-02-12 14:59:07 -0800 | [diff] [blame] | 595 | lru->node = NULL; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 596 | |
| 597 | memcg_put_cache_ids(); |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 598 | } |
| 599 | EXPORT_SYMBOL_GPL(list_lru_destroy); |