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