blob: e4709fdaa8e6af8a35114af54f5dfaa3436a5d1c [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Dave Chinnera38e4082013-08-28 10:17:58 +10002/*
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 Chinner3b1d58a2013-08-28 10:18:00 +100010#include <linux/mm.h>
Dave Chinnera38e4082013-08-28 10:17:58 +100011#include <linux/list_lru.h>
Glauber Costa5ca302c2013-08-28 10:18:18 +100012#include <linux/slab.h>
Vladimir Davydovc0a5b562015-02-12 14:59:07 -080013#include <linux/mutex.h>
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080014#include <linux/memcontrol.h>
Vladimir Davydovc0a5b562015-02-12 14:59:07 -080015
Kirill Tkhai84c07d12018-08-17 15:47:25 -070016#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydovc0a5b562015-02-12 14:59:07 -080017static LIST_HEAD(list_lrus);
18static DEFINE_MUTEX(list_lrus_mutex);
19
20static 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
27static 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 Davydovc0a5b562015-02-12 14:59:07 -080033
Kirill Tkhaifae91d62018-08-17 15:48:10 -070034static int lru_shrinker_id(struct list_lru *lru)
35{
36 return lru->shrinker_id;
37}
38
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080039static inline bool list_lru_memcg_aware(struct list_lru *lru)
40{
Jiri Slaby3e858992019-05-31 22:30:26 -070041 return lru->memcg_aware;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080042}
43
44static inline struct list_lru_one *
45list_lru_from_memcg_idx(struct list_lru_node *nlru, int idx)
46{
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -070047 struct list_lru_memcg *memcg_lrus;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080048 /*
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -070049 * Either lock or RCU protects the array of per cgroup lists
50 * from relocation (see memcg_update_list_lru_node).
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080051 */
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -070052 memcg_lrus = rcu_dereference_check(nlru->memcg_lrus,
53 lockdep_is_held(&nlru->lock));
54 if (memcg_lrus && idx >= 0)
55 return memcg_lrus->lru[idx];
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080056 return &nlru->lru;
57}
58
Vladimir Davydovdf406552015-11-05 18:49:04 -080059static __always_inline struct mem_cgroup *mem_cgroup_from_kmem(void *ptr)
60{
61 struct page *page;
62
63 if (!memcg_kmem_enabled())
64 return NULL;
65 page = virt_to_head_page(ptr);
66 return page->mem_cgroup;
67}
68
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080069static inline struct list_lru_one *
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070070list_lru_from_kmem(struct list_lru_node *nlru, void *ptr,
71 struct mem_cgroup **memcg_ptr)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080072{
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070073 struct list_lru_one *l = &nlru->lru;
74 struct mem_cgroup *memcg = NULL;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080075
76 if (!nlru->memcg_lrus)
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070077 goto out;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080078
79 memcg = mem_cgroup_from_kmem(ptr);
80 if (!memcg)
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070081 goto out;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080082
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070083 l = list_lru_from_memcg_idx(nlru, memcg_cache_id(memcg));
84out:
85 if (memcg_ptr)
86 *memcg_ptr = memcg;
87 return l;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080088}
89#else
Kirill Tkhaie0295232018-08-17 15:47:21 -070090static void list_lru_register(struct list_lru *lru)
91{
92}
93
94static void list_lru_unregister(struct list_lru *lru)
95{
96}
97
Kirill Tkhaifae91d62018-08-17 15:48:10 -070098static int lru_shrinker_id(struct list_lru *lru)
99{
100 return -1;
101}
102
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800103static inline bool list_lru_memcg_aware(struct list_lru *lru)
104{
105 return false;
106}
107
108static inline struct list_lru_one *
109list_lru_from_memcg_idx(struct list_lru_node *nlru, int idx)
110{
111 return &nlru->lru;
112}
113
114static inline struct list_lru_one *
Kirill Tkhai44bd4a42018-08-17 15:47:54 -0700115list_lru_from_kmem(struct list_lru_node *nlru, void *ptr,
116 struct mem_cgroup **memcg_ptr)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800117{
Kirill Tkhai44bd4a42018-08-17 15:47:54 -0700118 if (memcg_ptr)
119 *memcg_ptr = NULL;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800120 return &nlru->lru;
121}
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700122#endif /* CONFIG_MEMCG_KMEM */
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800123
Dave Chinnera38e4082013-08-28 10:17:58 +1000124bool list_lru_add(struct list_lru *lru, struct list_head *item)
125{
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000126 int nid = page_to_nid(virt_to_page(item));
127 struct list_lru_node *nlru = &lru->node[nid];
Kirill Tkhaifae91d62018-08-17 15:48:10 -0700128 struct mem_cgroup *memcg;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800129 struct list_lru_one *l;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000130
131 spin_lock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000132 if (list_empty(item)) {
Kirill Tkhaifae91d62018-08-17 15:48:10 -0700133 l = list_lru_from_kmem(nlru, item, &memcg);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800134 list_add_tail(item, &l->list);
Kirill Tkhaifae91d62018-08-17 15:48:10 -0700135 /* Set shrinker bit if the first element was added */
136 if (!l->nr_items++)
137 memcg_set_shrinker_bit(memcg, nid,
138 lru_shrinker_id(lru));
Sahitya Tummala2c80cd52017-07-10 15:49:57 -0700139 nlru->nr_items++;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000140 spin_unlock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000141 return true;
142 }
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000143 spin_unlock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000144 return false;
145}
146EXPORT_SYMBOL_GPL(list_lru_add);
147
148bool list_lru_del(struct list_lru *lru, struct list_head *item)
149{
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000150 int nid = page_to_nid(virt_to_page(item));
151 struct list_lru_node *nlru = &lru->node[nid];
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800152 struct list_lru_one *l;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000153
154 spin_lock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000155 if (!list_empty(item)) {
Kirill Tkhai44bd4a42018-08-17 15:47:54 -0700156 l = list_lru_from_kmem(nlru, item, NULL);
Dave Chinnera38e4082013-08-28 10:17:58 +1000157 list_del_init(item);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800158 l->nr_items--;
Sahitya Tummala2c80cd52017-07-10 15:49:57 -0700159 nlru->nr_items--;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000160 spin_unlock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000161 return true;
162 }
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000163 spin_unlock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000164 return false;
165}
166EXPORT_SYMBOL_GPL(list_lru_del);
167
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800168void list_lru_isolate(struct list_lru_one *list, struct list_head *item)
169{
170 list_del_init(item);
171 list->nr_items--;
172}
173EXPORT_SYMBOL_GPL(list_lru_isolate);
174
175void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item,
176 struct list_head *head)
177{
178 list_move(item, head);
179 list->nr_items--;
180}
181EXPORT_SYMBOL_GPL(list_lru_isolate_move);
182
Andrew Morton930eaac2018-08-17 15:46:11 -0700183unsigned long list_lru_count_one(struct list_lru *lru,
184 int nid, struct mem_cgroup *memcg)
Dave Chinnera38e4082013-08-28 10:17:58 +1000185{
Glauber Costa6a4f4962013-08-28 10:18:02 +1000186 struct list_lru_node *nlru = &lru->node[nid];
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800187 struct list_lru_one *l;
188 unsigned long count;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000189
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700190 rcu_read_lock();
Andrew Morton930eaac2018-08-17 15:46:11 -0700191 l = list_lru_from_memcg_idx(nlru, memcg_cache_id(memcg));
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800192 count = l->nr_items;
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700193 rcu_read_unlock();
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000194
195 return count;
196}
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800197EXPORT_SYMBOL_GPL(list_lru_count_one);
198
199unsigned long list_lru_count_node(struct list_lru *lru, int nid)
200{
Sahitya Tummala2c80cd52017-07-10 15:49:57 -0700201 struct list_lru_node *nlru;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800202
Sahitya Tummala2c80cd52017-07-10 15:49:57 -0700203 nlru = &lru->node[nid];
204 return nlru->nr_items;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800205}
Glauber Costa6a4f4962013-08-28 10:18:02 +1000206EXPORT_SYMBOL_GPL(list_lru_count_node);
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000207
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800208static unsigned long
Sebastian Andrzej Siewior6e018962018-08-17 15:49:51 -0700209__list_lru_walk_one(struct list_lru_node *nlru, int memcg_idx,
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800210 list_lru_walk_cb isolate, void *cb_arg,
211 unsigned long *nr_to_walk)
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000212{
213
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800214 struct list_lru_one *l;
Dave Chinnera38e4082013-08-28 10:17:58 +1000215 struct list_head *item, *n;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000216 unsigned long isolated = 0;
Dave Chinnera38e4082013-08-28 10:17:58 +1000217
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800218 l = list_lru_from_memcg_idx(nlru, memcg_idx);
Dave Chinnera38e4082013-08-28 10:17:58 +1000219restart:
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800220 list_for_each_safe(item, n, &l->list) {
Dave Chinnera38e4082013-08-28 10:17:58 +1000221 enum lru_status ret;
Dave Chinner5cedf7212013-08-28 10:18:01 +1000222
223 /*
224 * decrement nr_to_walk first so that we don't livelock if we
225 * get stuck on large numbesr of LRU_RETRY items
226 */
Russell Kingc56b0972013-10-30 14:16:16 +0000227 if (!*nr_to_walk)
Dave Chinner5cedf7212013-08-28 10:18:01 +1000228 break;
Russell Kingc56b0972013-10-30 14:16:16 +0000229 --*nr_to_walk;
Dave Chinner5cedf7212013-08-28 10:18:01 +1000230
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800231 ret = isolate(item, l, &nlru->lock, cb_arg);
Dave Chinnera38e4082013-08-28 10:17:58 +1000232 switch (ret) {
Johannes Weiner449dd692014-04-03 14:47:56 -0700233 case LRU_REMOVED_RETRY:
234 assert_spin_locked(&nlru->lock);
Gustavo A. R. Silva5b568ac2017-11-15 17:38:49 -0800235 /* fall through */
Dave Chinnera38e4082013-08-28 10:17:58 +1000236 case LRU_REMOVED:
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000237 isolated++;
Sahitya Tummala2c80cd52017-07-10 15:49:57 -0700238 nlru->nr_items--;
Johannes Weiner449dd692014-04-03 14:47:56 -0700239 /*
240 * If the lru lock has been dropped, our list
241 * traversal is now invalid and so we have to
242 * restart from scratch.
243 */
244 if (ret == LRU_REMOVED_RETRY)
245 goto restart;
Dave Chinnera38e4082013-08-28 10:17:58 +1000246 break;
247 case LRU_ROTATE:
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800248 list_move_tail(item, &l->list);
Dave Chinnera38e4082013-08-28 10:17:58 +1000249 break;
250 case LRU_SKIP:
251 break;
252 case LRU_RETRY:
Dave Chinner5cedf7212013-08-28 10:18:01 +1000253 /*
254 * The lru lock has been dropped, our list traversal is
255 * now invalid and so we have to restart from scratch.
256 */
Johannes Weiner449dd692014-04-03 14:47:56 -0700257 assert_spin_locked(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000258 goto restart;
259 default:
260 BUG();
261 }
Dave Chinnera38e4082013-08-28 10:17:58 +1000262 }
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000263 return isolated;
264}
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800265
266unsigned long
267list_lru_walk_one(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
268 list_lru_walk_cb isolate, void *cb_arg,
269 unsigned long *nr_to_walk)
270{
Sebastian Andrzej Siewior6cfe57a2018-08-17 15:49:48 -0700271 struct list_lru_node *nlru = &lru->node[nid];
272 unsigned long ret;
273
274 spin_lock(&nlru->lock);
Sebastian Andrzej Siewior6e018962018-08-17 15:49:51 -0700275 ret = __list_lru_walk_one(nlru, memcg_cache_id(memcg), isolate, cb_arg,
276 nr_to_walk);
Sebastian Andrzej Siewior6cfe57a2018-08-17 15:49:48 -0700277 spin_unlock(&nlru->lock);
278 return ret;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800279}
280EXPORT_SYMBOL_GPL(list_lru_walk_one);
281
Sebastian Andrzej Siewior6b51e882018-08-17 15:49:55 -0700282unsigned long
283list_lru_walk_one_irq(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
284 list_lru_walk_cb isolate, void *cb_arg,
285 unsigned long *nr_to_walk)
286{
287 struct list_lru_node *nlru = &lru->node[nid];
288 unsigned long ret;
289
290 spin_lock_irq(&nlru->lock);
291 ret = __list_lru_walk_one(nlru, memcg_cache_id(memcg), isolate, cb_arg,
292 nr_to_walk);
293 spin_unlock_irq(&nlru->lock);
294 return ret;
295}
296
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800297unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
298 list_lru_walk_cb isolate, void *cb_arg,
299 unsigned long *nr_to_walk)
300{
301 long isolated = 0;
302 int memcg_idx;
303
Sebastian Andrzej Siewior87a5ffc2018-08-17 15:49:45 -0700304 isolated += list_lru_walk_one(lru, nid, NULL, isolate, cb_arg,
305 nr_to_walk);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800306 if (*nr_to_walk > 0 && list_lru_memcg_aware(lru)) {
307 for_each_memcg_cache_index(memcg_idx) {
Sebastian Andrzej Siewior6cfe57a2018-08-17 15:49:48 -0700308 struct list_lru_node *nlru = &lru->node[nid];
309
310 spin_lock(&nlru->lock);
Sebastian Andrzej Siewior6e018962018-08-17 15:49:51 -0700311 isolated += __list_lru_walk_one(nlru, memcg_idx,
312 isolate, cb_arg,
313 nr_to_walk);
Sebastian Andrzej Siewior6cfe57a2018-08-17 15:49:48 -0700314 spin_unlock(&nlru->lock);
315
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800316 if (*nr_to_walk <= 0)
317 break;
318 }
319 }
320 return isolated;
321}
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000322EXPORT_SYMBOL_GPL(list_lru_walk_node);
323
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800324static void init_one_lru(struct list_lru_one *l)
325{
326 INIT_LIST_HEAD(&l->list);
327 l->nr_items = 0;
328}
329
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700330#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800331static void __memcg_destroy_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 kfree(memcg_lrus->lru[i]);
338}
339
340static int __memcg_init_list_lru_node(struct list_lru_memcg *memcg_lrus,
341 int begin, int end)
342{
343 int i;
344
345 for (i = begin; i < end; i++) {
346 struct list_lru_one *l;
347
348 l = kmalloc(sizeof(struct list_lru_one), GFP_KERNEL);
349 if (!l)
350 goto fail;
351
352 init_one_lru(l);
353 memcg_lrus->lru[i] = l;
354 }
355 return 0;
356fail:
357 __memcg_destroy_list_lru_node(memcg_lrus, begin, i - 1);
358 return -ENOMEM;
359}
360
361static int memcg_init_list_lru_node(struct list_lru_node *nlru)
362{
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700363 struct list_lru_memcg *memcg_lrus;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800364 int size = memcg_nr_cache_ids;
365
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700366 memcg_lrus = kvmalloc(sizeof(*memcg_lrus) +
367 size * sizeof(void *), GFP_KERNEL);
368 if (!memcg_lrus)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800369 return -ENOMEM;
370
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700371 if (__memcg_init_list_lru_node(memcg_lrus, 0, size)) {
372 kvfree(memcg_lrus);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800373 return -ENOMEM;
374 }
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700375 RCU_INIT_POINTER(nlru->memcg_lrus, memcg_lrus);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800376
377 return 0;
378}
379
380static void memcg_destroy_list_lru_node(struct list_lru_node *nlru)
381{
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700382 struct list_lru_memcg *memcg_lrus;
383 /*
384 * This is called when shrinker has already been unregistered,
385 * and nobody can use it. So, there is no need to use kvfree_rcu().
386 */
387 memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus, true);
388 __memcg_destroy_list_lru_node(memcg_lrus, 0, memcg_nr_cache_ids);
389 kvfree(memcg_lrus);
390}
391
392static void kvfree_rcu(struct rcu_head *head)
393{
394 struct list_lru_memcg *mlru;
395
396 mlru = container_of(head, struct list_lru_memcg, rcu);
397 kvfree(mlru);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800398}
399
400static int memcg_update_list_lru_node(struct list_lru_node *nlru,
401 int old_size, int new_size)
402{
403 struct list_lru_memcg *old, *new;
404
405 BUG_ON(old_size > new_size);
406
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700407 old = rcu_dereference_protected(nlru->memcg_lrus,
408 lockdep_is_held(&list_lrus_mutex));
409 new = kvmalloc(sizeof(*new) + new_size * sizeof(void *), GFP_KERNEL);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800410 if (!new)
411 return -ENOMEM;
412
413 if (__memcg_init_list_lru_node(new, old_size, new_size)) {
Johannes Weinerf80c7da2017-10-03 16:16:10 -0700414 kvfree(new);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800415 return -ENOMEM;
416 }
417
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700418 memcpy(&new->lru, &old->lru, old_size * sizeof(void *));
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800419
420 /*
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700421 * The locking below allows readers that hold nlru->lock avoid taking
422 * rcu_read_lock (see list_lru_from_memcg_idx).
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800423 *
424 * Since list_lru_{add,del} may be called under an IRQ-safe lock,
425 * we have to use IRQ-safe primitives here to avoid deadlock.
426 */
427 spin_lock_irq(&nlru->lock);
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700428 rcu_assign_pointer(nlru->memcg_lrus, new);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800429 spin_unlock_irq(&nlru->lock);
430
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700431 call_rcu(&old->rcu, kvfree_rcu);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800432 return 0;
433}
434
435static void memcg_cancel_update_list_lru_node(struct list_lru_node *nlru,
436 int old_size, int new_size)
437{
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700438 struct list_lru_memcg *memcg_lrus;
439
440 memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus,
441 lockdep_is_held(&list_lrus_mutex));
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800442 /* do not bother shrinking the array back to the old size, because we
443 * cannot handle allocation failures here */
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700444 __memcg_destroy_list_lru_node(memcg_lrus, old_size, new_size);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800445}
446
447static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)
448{
449 int i;
450
Jiri Slaby3e858992019-05-31 22:30:26 -0700451 lru->memcg_aware = memcg_aware;
452
Raghavendra K T145949a2015-11-05 18:46:26 -0800453 if (!memcg_aware)
454 return 0;
455
456 for_each_node(i) {
457 if (memcg_init_list_lru_node(&lru->node[i]))
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800458 goto fail;
459 }
460 return 0;
461fail:
Raghavendra K T145949a2015-11-05 18:46:26 -0800462 for (i = i - 1; i >= 0; i--) {
463 if (!lru->node[i].memcg_lrus)
464 continue;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800465 memcg_destroy_list_lru_node(&lru->node[i]);
Raghavendra K T145949a2015-11-05 18:46:26 -0800466 }
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800467 return -ENOMEM;
468}
469
470static void memcg_destroy_list_lru(struct list_lru *lru)
471{
472 int i;
473
474 if (!list_lru_memcg_aware(lru))
475 return;
476
Raghavendra K T145949a2015-11-05 18:46:26 -0800477 for_each_node(i)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800478 memcg_destroy_list_lru_node(&lru->node[i]);
479}
480
481static int memcg_update_list_lru(struct list_lru *lru,
482 int old_size, int new_size)
483{
484 int i;
485
486 if (!list_lru_memcg_aware(lru))
487 return 0;
488
Raghavendra K T145949a2015-11-05 18:46:26 -0800489 for_each_node(i) {
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800490 if (memcg_update_list_lru_node(&lru->node[i],
491 old_size, new_size))
492 goto fail;
493 }
494 return 0;
495fail:
Raghavendra K T145949a2015-11-05 18:46:26 -0800496 for (i = i - 1; i >= 0; i--) {
497 if (!lru->node[i].memcg_lrus)
498 continue;
499
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800500 memcg_cancel_update_list_lru_node(&lru->node[i],
501 old_size, new_size);
Raghavendra K T145949a2015-11-05 18:46:26 -0800502 }
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800503 return -ENOMEM;
504}
505
506static void memcg_cancel_update_list_lru(struct list_lru *lru,
507 int old_size, int new_size)
508{
509 int i;
510
511 if (!list_lru_memcg_aware(lru))
512 return;
513
Raghavendra K T145949a2015-11-05 18:46:26 -0800514 for_each_node(i)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800515 memcg_cancel_update_list_lru_node(&lru->node[i],
516 old_size, new_size);
517}
518
519int memcg_update_all_list_lrus(int new_size)
520{
521 int ret = 0;
522 struct list_lru *lru;
523 int old_size = memcg_nr_cache_ids;
524
525 mutex_lock(&list_lrus_mutex);
526 list_for_each_entry(lru, &list_lrus, list) {
527 ret = memcg_update_list_lru(lru, old_size, new_size);
528 if (ret)
529 goto fail;
530 }
531out:
532 mutex_unlock(&list_lrus_mutex);
533 return ret;
534fail:
535 list_for_each_entry_continue_reverse(lru, &list_lrus, list)
536 memcg_cancel_update_list_lru(lru, old_size, new_size);
537 goto out;
538}
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800539
Kirill Tkhai3b82c4d2018-08-17 15:48:01 -0700540static void memcg_drain_list_lru_node(struct list_lru *lru, int nid,
Kirill Tkhai9bec5c32018-08-17 15:47:58 -0700541 int src_idx, struct mem_cgroup *dst_memcg)
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800542{
Kirill Tkhai3b82c4d2018-08-17 15:48:01 -0700543 struct list_lru_node *nlru = &lru->node[nid];
Kirill Tkhai9bec5c32018-08-17 15:47:58 -0700544 int dst_idx = dst_memcg->kmemcg_id;
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800545 struct list_lru_one *src, *dst;
Kirill Tkhaifae91d62018-08-17 15:48:10 -0700546 bool set;
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800547
548 /*
549 * Since list_lru_{add,del} may be called under an IRQ-safe lock,
550 * we have to use IRQ-safe primitives here to avoid deadlock.
551 */
552 spin_lock_irq(&nlru->lock);
553
554 src = list_lru_from_memcg_idx(nlru, src_idx);
555 dst = list_lru_from_memcg_idx(nlru, dst_idx);
556
557 list_splice_init(&src->list, &dst->list);
Kirill Tkhaifae91d62018-08-17 15:48:10 -0700558 set = (!dst->nr_items && src->nr_items);
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800559 dst->nr_items += src->nr_items;
Kirill Tkhaifae91d62018-08-17 15:48:10 -0700560 if (set)
561 memcg_set_shrinker_bit(dst_memcg, nid, lru_shrinker_id(lru));
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800562 src->nr_items = 0;
563
564 spin_unlock_irq(&nlru->lock);
565}
566
567static void memcg_drain_list_lru(struct list_lru *lru,
Kirill Tkhai9bec5c32018-08-17 15:47:58 -0700568 int src_idx, struct mem_cgroup *dst_memcg)
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800569{
570 int i;
571
572 if (!list_lru_memcg_aware(lru))
573 return;
574
Raghavendra K T145949a2015-11-05 18:46:26 -0800575 for_each_node(i)
Kirill Tkhai3b82c4d2018-08-17 15:48:01 -0700576 memcg_drain_list_lru_node(lru, i, src_idx, dst_memcg);
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800577}
578
Kirill Tkhai9bec5c32018-08-17 15:47:58 -0700579void memcg_drain_all_list_lrus(int src_idx, struct mem_cgroup *dst_memcg)
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800580{
581 struct list_lru *lru;
582
583 mutex_lock(&list_lrus_mutex);
584 list_for_each_entry(lru, &list_lrus, list)
Kirill Tkhai9bec5c32018-08-17 15:47:58 -0700585 memcg_drain_list_lru(lru, src_idx, dst_memcg);
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800586 mutex_unlock(&list_lrus_mutex);
587}
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800588#else
589static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)
590{
591 return 0;
592}
593
594static void memcg_destroy_list_lru(struct list_lru *lru)
595{
596}
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700597#endif /* CONFIG_MEMCG_KMEM */
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800598
599int __list_lru_init(struct list_lru *lru, bool memcg_aware,
Kirill Tkhaic92e8e12018-08-17 15:47:50 -0700600 struct lock_class_key *key, struct shrinker *shrinker)
Dave Chinnera38e4082013-08-28 10:17:58 +1000601{
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000602 int i;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800603 int err = -ENOMEM;
604
Kirill Tkhaic92e8e12018-08-17 15:47:50 -0700605#ifdef CONFIG_MEMCG_KMEM
606 if (shrinker)
607 lru->shrinker_id = shrinker->id;
608 else
609 lru->shrinker_id = -1;
610#endif
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800611 memcg_get_cache_ids();
Glauber Costa5ca302c2013-08-28 10:18:18 +1000612
Alexey Dobriyanb9726c22019-03-05 15:48:26 -0800613 lru->node = kcalloc(nr_node_ids, sizeof(*lru->node), GFP_KERNEL);
Glauber Costa5ca302c2013-08-28 10:18:18 +1000614 if (!lru->node)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800615 goto out;
Dave Chinnera38e4082013-08-28 10:17:58 +1000616
Raghavendra K T145949a2015-11-05 18:46:26 -0800617 for_each_node(i) {
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000618 spin_lock_init(&lru->node[i].lock);
Johannes Weiner449dd692014-04-03 14:47:56 -0700619 if (key)
620 lockdep_set_class(&lru->node[i].lock, key);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800621 init_one_lru(&lru->node[i].lru);
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000622 }
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800623
624 err = memcg_init_list_lru(lru, memcg_aware);
625 if (err) {
626 kfree(lru->node);
Alexander Polakov1bc11d72016-10-27 17:46:27 -0700627 /* Do this so a list_lru_destroy() doesn't crash: */
628 lru->node = NULL;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800629 goto out;
630 }
631
Vladimir Davydovc0a5b562015-02-12 14:59:07 -0800632 list_lru_register(lru);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800633out:
634 memcg_put_cache_ids();
635 return err;
Dave Chinnera38e4082013-08-28 10:17:58 +1000636}
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800637EXPORT_SYMBOL_GPL(__list_lru_init);
Glauber Costa5ca302c2013-08-28 10:18:18 +1000638
639void list_lru_destroy(struct list_lru *lru)
640{
Vladimir Davydovc0a5b562015-02-12 14:59:07 -0800641 /* Already destroyed or not yet initialized? */
642 if (!lru->node)
643 return;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800644
645 memcg_get_cache_ids();
646
Vladimir Davydovc0a5b562015-02-12 14:59:07 -0800647 list_lru_unregister(lru);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800648
649 memcg_destroy_list_lru(lru);
Glauber Costa5ca302c2013-08-28 10:18:18 +1000650 kfree(lru->node);
Vladimir Davydovc0a5b562015-02-12 14:59:07 -0800651 lru->node = NULL;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800652
Kirill Tkhaic92e8e12018-08-17 15:47:50 -0700653#ifdef CONFIG_MEMCG_KMEM
654 lru->shrinker_id = -1;
655#endif
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800656 memcg_put_cache_ids();
Glauber Costa5ca302c2013-08-28 10:18:18 +1000657}
658EXPORT_SYMBOL_GPL(list_lru_destroy);