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