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 | |
Len Baker | 16f6bf2 | 2021-11-05 13:37:40 -0700 | [diff] [blame^] | 357 | memcg_lrus = kvmalloc(struct_size(memcg_lrus, lru, size), GFP_KERNEL); |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 358 | if (!memcg_lrus) |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 359 | return -ENOMEM; |
| 360 | |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 361 | if (__memcg_init_list_lru_node(memcg_lrus, 0, size)) { |
| 362 | kvfree(memcg_lrus); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 363 | return -ENOMEM; |
| 364 | } |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 365 | RCU_INIT_POINTER(nlru->memcg_lrus, memcg_lrus); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | static void memcg_destroy_list_lru_node(struct list_lru_node *nlru) |
| 371 | { |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 372 | struct list_lru_memcg *memcg_lrus; |
| 373 | /* |
| 374 | * This is called when shrinker has already been unregistered, |
Shakeel Butt | a7b7e1d | 2021-02-24 12:04:12 -0800 | [diff] [blame] | 375 | * 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] | 376 | */ |
| 377 | memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus, true); |
| 378 | __memcg_destroy_list_lru_node(memcg_lrus, 0, memcg_nr_cache_ids); |
| 379 | kvfree(memcg_lrus); |
| 380 | } |
| 381 | |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 382 | static int memcg_update_list_lru_node(struct list_lru_node *nlru, |
| 383 | int old_size, int new_size) |
| 384 | { |
| 385 | struct list_lru_memcg *old, *new; |
| 386 | |
| 387 | BUG_ON(old_size > new_size); |
| 388 | |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 389 | old = rcu_dereference_protected(nlru->memcg_lrus, |
| 390 | lockdep_is_held(&list_lrus_mutex)); |
Len Baker | 16f6bf2 | 2021-11-05 13:37:40 -0700 | [diff] [blame^] | 391 | new = kvmalloc(struct_size(new, lru, new_size), GFP_KERNEL); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 392 | if (!new) |
| 393 | return -ENOMEM; |
| 394 | |
| 395 | if (__memcg_init_list_lru_node(new, old_size, new_size)) { |
Johannes Weiner | f80c7da | 2017-10-03 16:16:10 -0700 | [diff] [blame] | 396 | kvfree(new); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 397 | return -ENOMEM; |
| 398 | } |
| 399 | |
Len Baker | 16f6bf2 | 2021-11-05 13:37:40 -0700 | [diff] [blame^] | 400 | memcpy(&new->lru, &old->lru, flex_array_size(new, lru, old_size)); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 401 | |
| 402 | /* |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 403 | * The locking below allows readers that hold nlru->lock avoid taking |
| 404 | * rcu_read_lock (see list_lru_from_memcg_idx). |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 405 | * |
| 406 | * Since list_lru_{add,del} may be called under an IRQ-safe lock, |
| 407 | * we have to use IRQ-safe primitives here to avoid deadlock. |
| 408 | */ |
| 409 | spin_lock_irq(&nlru->lock); |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 410 | rcu_assign_pointer(nlru->memcg_lrus, new); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 411 | spin_unlock_irq(&nlru->lock); |
| 412 | |
Shakeel Butt | a7b7e1d | 2021-02-24 12:04:12 -0800 | [diff] [blame] | 413 | kvfree_rcu(old, rcu); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | static void memcg_cancel_update_list_lru_node(struct list_lru_node *nlru, |
| 418 | int old_size, int new_size) |
| 419 | { |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 420 | struct list_lru_memcg *memcg_lrus; |
| 421 | |
| 422 | memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus, |
| 423 | lockdep_is_held(&list_lrus_mutex)); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 424 | /* do not bother shrinking the array back to the old size, because we |
| 425 | * cannot handle allocation failures here */ |
Kirill Tkhai | 0c7c1be | 2018-04-05 16:25:08 -0700 | [diff] [blame] | 426 | __memcg_destroy_list_lru_node(memcg_lrus, old_size, new_size); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware) |
| 430 | { |
| 431 | int i; |
| 432 | |
Jiri Slaby | 3e85899 | 2019-05-31 22:30:26 -0700 | [diff] [blame] | 433 | lru->memcg_aware = memcg_aware; |
| 434 | |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 435 | if (!memcg_aware) |
| 436 | return 0; |
| 437 | |
| 438 | for_each_node(i) { |
| 439 | if (memcg_init_list_lru_node(&lru->node[i])) |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 440 | goto fail; |
| 441 | } |
| 442 | return 0; |
| 443 | fail: |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 444 | for (i = i - 1; i >= 0; i--) { |
| 445 | if (!lru->node[i].memcg_lrus) |
| 446 | continue; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 447 | memcg_destroy_list_lru_node(&lru->node[i]); |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 448 | } |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 449 | return -ENOMEM; |
| 450 | } |
| 451 | |
| 452 | static void memcg_destroy_list_lru(struct list_lru *lru) |
| 453 | { |
| 454 | int i; |
| 455 | |
| 456 | if (!list_lru_memcg_aware(lru)) |
| 457 | return; |
| 458 | |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 459 | for_each_node(i) |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 460 | memcg_destroy_list_lru_node(&lru->node[i]); |
| 461 | } |
| 462 | |
| 463 | static int memcg_update_list_lru(struct list_lru *lru, |
| 464 | int old_size, int new_size) |
| 465 | { |
| 466 | int i; |
| 467 | |
| 468 | if (!list_lru_memcg_aware(lru)) |
| 469 | return 0; |
| 470 | |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 471 | for_each_node(i) { |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 472 | if (memcg_update_list_lru_node(&lru->node[i], |
| 473 | old_size, new_size)) |
| 474 | goto fail; |
| 475 | } |
| 476 | return 0; |
| 477 | fail: |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 478 | for (i = i - 1; i >= 0; i--) { |
| 479 | if (!lru->node[i].memcg_lrus) |
| 480 | continue; |
| 481 | |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 482 | memcg_cancel_update_list_lru_node(&lru->node[i], |
| 483 | old_size, new_size); |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 484 | } |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 485 | return -ENOMEM; |
| 486 | } |
| 487 | |
| 488 | static void memcg_cancel_update_list_lru(struct list_lru *lru, |
| 489 | int old_size, int new_size) |
| 490 | { |
| 491 | int i; |
| 492 | |
| 493 | if (!list_lru_memcg_aware(lru)) |
| 494 | return; |
| 495 | |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 496 | for_each_node(i) |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 497 | memcg_cancel_update_list_lru_node(&lru->node[i], |
| 498 | old_size, new_size); |
| 499 | } |
| 500 | |
| 501 | int memcg_update_all_list_lrus(int new_size) |
| 502 | { |
| 503 | int ret = 0; |
| 504 | struct list_lru *lru; |
| 505 | int old_size = memcg_nr_cache_ids; |
| 506 | |
| 507 | mutex_lock(&list_lrus_mutex); |
| 508 | list_for_each_entry(lru, &list_lrus, list) { |
| 509 | ret = memcg_update_list_lru(lru, old_size, new_size); |
| 510 | if (ret) |
| 511 | goto fail; |
| 512 | } |
| 513 | out: |
| 514 | mutex_unlock(&list_lrus_mutex); |
| 515 | return ret; |
| 516 | fail: |
| 517 | list_for_each_entry_continue_reverse(lru, &list_lrus, list) |
| 518 | memcg_cancel_update_list_lru(lru, old_size, new_size); |
| 519 | goto out; |
| 520 | } |
Vladimir Davydov | 2788cf0 | 2015-02-12 14:59:38 -0800 | [diff] [blame] | 521 | |
Kirill Tkhai | 3b82c4d | 2018-08-17 15:48:01 -0700 | [diff] [blame] | 522 | 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] | 523 | int src_idx, struct mem_cgroup *dst_memcg) |
Vladimir Davydov | 2788cf0 | 2015-02-12 14:59:38 -0800 | [diff] [blame] | 524 | { |
Kirill Tkhai | 3b82c4d | 2018-08-17 15:48:01 -0700 | [diff] [blame] | 525 | struct list_lru_node *nlru = &lru->node[nid]; |
Kirill Tkhai | 9bec5c3 | 2018-08-17 15:47:58 -0700 | [diff] [blame] | 526 | int dst_idx = dst_memcg->kmemcg_id; |
Vladimir Davydov | 2788cf0 | 2015-02-12 14:59:38 -0800 | [diff] [blame] | 527 | struct list_lru_one *src, *dst; |
| 528 | |
| 529 | /* |
| 530 | * Since list_lru_{add,del} may be called under an IRQ-safe lock, |
| 531 | * we have to use IRQ-safe primitives here to avoid deadlock. |
| 532 | */ |
| 533 | spin_lock_irq(&nlru->lock); |
| 534 | |
| 535 | src = list_lru_from_memcg_idx(nlru, src_idx); |
| 536 | dst = list_lru_from_memcg_idx(nlru, dst_idx); |
| 537 | |
| 538 | list_splice_init(&src->list, &dst->list); |
Yang Shi | 8199be0 | 2020-12-05 22:14:48 -0800 | [diff] [blame] | 539 | |
| 540 | if (src->nr_items) { |
| 541 | dst->nr_items += src->nr_items; |
Yang Shi | 2bfd363 | 2021-05-04 18:36:11 -0700 | [diff] [blame] | 542 | set_shrinker_bit(dst_memcg, nid, lru_shrinker_id(lru)); |
Yang Shi | 8199be0 | 2020-12-05 22:14:48 -0800 | [diff] [blame] | 543 | src->nr_items = 0; |
| 544 | } |
Vladimir Davydov | 2788cf0 | 2015-02-12 14:59:38 -0800 | [diff] [blame] | 545 | |
| 546 | spin_unlock_irq(&nlru->lock); |
| 547 | } |
| 548 | |
| 549 | static void memcg_drain_list_lru(struct list_lru *lru, |
Kirill Tkhai | 9bec5c3 | 2018-08-17 15:47:58 -0700 | [diff] [blame] | 550 | int src_idx, struct mem_cgroup *dst_memcg) |
Vladimir Davydov | 2788cf0 | 2015-02-12 14:59:38 -0800 | [diff] [blame] | 551 | { |
| 552 | int i; |
| 553 | |
| 554 | if (!list_lru_memcg_aware(lru)) |
| 555 | return; |
| 556 | |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 557 | for_each_node(i) |
Kirill Tkhai | 3b82c4d | 2018-08-17 15:48:01 -0700 | [diff] [blame] | 558 | memcg_drain_list_lru_node(lru, i, src_idx, dst_memcg); |
Vladimir Davydov | 2788cf0 | 2015-02-12 14:59:38 -0800 | [diff] [blame] | 559 | } |
| 560 | |
Kirill Tkhai | 9bec5c3 | 2018-08-17 15:47:58 -0700 | [diff] [blame] | 561 | 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] | 562 | { |
| 563 | struct list_lru *lru; |
| 564 | |
| 565 | mutex_lock(&list_lrus_mutex); |
| 566 | list_for_each_entry(lru, &list_lrus, list) |
Kirill Tkhai | 9bec5c3 | 2018-08-17 15:47:58 -0700 | [diff] [blame] | 567 | memcg_drain_list_lru(lru, src_idx, dst_memcg); |
Vladimir Davydov | 2788cf0 | 2015-02-12 14:59:38 -0800 | [diff] [blame] | 568 | mutex_unlock(&list_lrus_mutex); |
| 569 | } |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 570 | #else |
| 571 | static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware) |
| 572 | { |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | static void memcg_destroy_list_lru(struct list_lru *lru) |
| 577 | { |
| 578 | } |
Kirill Tkhai | 84c07d1 | 2018-08-17 15:47:25 -0700 | [diff] [blame] | 579 | #endif /* CONFIG_MEMCG_KMEM */ |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 580 | |
| 581 | int __list_lru_init(struct list_lru *lru, bool memcg_aware, |
Kirill Tkhai | c92e8e1 | 2018-08-17 15:47:50 -0700 | [diff] [blame] | 582 | struct lock_class_key *key, struct shrinker *shrinker) |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 583 | { |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 584 | int i; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 585 | int err = -ENOMEM; |
| 586 | |
Kirill Tkhai | c92e8e1 | 2018-08-17 15:47:50 -0700 | [diff] [blame] | 587 | #ifdef CONFIG_MEMCG_KMEM |
| 588 | if (shrinker) |
| 589 | lru->shrinker_id = shrinker->id; |
| 590 | else |
| 591 | lru->shrinker_id = -1; |
| 592 | #endif |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 593 | memcg_get_cache_ids(); |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 594 | |
Alexey Dobriyan | b9726c2 | 2019-03-05 15:48:26 -0800 | [diff] [blame] | 595 | lru->node = kcalloc(nr_node_ids, sizeof(*lru->node), GFP_KERNEL); |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 596 | if (!lru->node) |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 597 | goto out; |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 598 | |
Raghavendra K T | 145949a | 2015-11-05 18:46:26 -0800 | [diff] [blame] | 599 | for_each_node(i) { |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 600 | spin_lock_init(&lru->node[i].lock); |
Johannes Weiner | 449dd69 | 2014-04-03 14:47:56 -0700 | [diff] [blame] | 601 | if (key) |
| 602 | lockdep_set_class(&lru->node[i].lock, key); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 603 | init_one_lru(&lru->node[i].lru); |
Dave Chinner | 3b1d58a | 2013-08-28 10:18:00 +1000 | [diff] [blame] | 604 | } |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 605 | |
| 606 | err = memcg_init_list_lru(lru, memcg_aware); |
| 607 | if (err) { |
| 608 | kfree(lru->node); |
Alexander Polakov | 1bc11d7 | 2016-10-27 17:46:27 -0700 | [diff] [blame] | 609 | /* Do this so a list_lru_destroy() doesn't crash: */ |
| 610 | lru->node = NULL; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 611 | goto out; |
| 612 | } |
| 613 | |
Vladimir Davydov | c0a5b56 | 2015-02-12 14:59:07 -0800 | [diff] [blame] | 614 | list_lru_register(lru); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 615 | out: |
| 616 | memcg_put_cache_ids(); |
| 617 | return err; |
Dave Chinner | a38e408 | 2013-08-28 10:17:58 +1000 | [diff] [blame] | 618 | } |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 619 | EXPORT_SYMBOL_GPL(__list_lru_init); |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 620 | |
| 621 | void list_lru_destroy(struct list_lru *lru) |
| 622 | { |
Vladimir Davydov | c0a5b56 | 2015-02-12 14:59:07 -0800 | [diff] [blame] | 623 | /* Already destroyed or not yet initialized? */ |
| 624 | if (!lru->node) |
| 625 | return; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 626 | |
| 627 | memcg_get_cache_ids(); |
| 628 | |
Vladimir Davydov | c0a5b56 | 2015-02-12 14:59:07 -0800 | [diff] [blame] | 629 | list_lru_unregister(lru); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 630 | |
| 631 | memcg_destroy_list_lru(lru); |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 632 | kfree(lru->node); |
Vladimir Davydov | c0a5b56 | 2015-02-12 14:59:07 -0800 | [diff] [blame] | 633 | lru->node = NULL; |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 634 | |
Kirill Tkhai | c92e8e1 | 2018-08-17 15:47:50 -0700 | [diff] [blame] | 635 | #ifdef CONFIG_MEMCG_KMEM |
| 636 | lru->shrinker_id = -1; |
| 637 | #endif |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 638 | memcg_put_cache_ids(); |
Glauber Costa | 5ca302c | 2013-08-28 10:18:18 +1000 | [diff] [blame] | 639 | } |
| 640 | EXPORT_SYMBOL_GPL(list_lru_destroy); |