blob: 7572f8e70b86e6dd240b82cda71c781cdede86bd [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>
Roman Gushchin4d96ba32019-07-11 20:56:31 -070015#include "slab.h"
Vladimir Davydovc0a5b562015-02-12 14:59:07 -080016
Kirill Tkhai84c07d12018-08-17 15:47:25 -070017#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydovc0a5b562015-02-12 14:59:07 -080018static LIST_HEAD(list_lrus);
19static DEFINE_MUTEX(list_lrus_mutex);
20
21static 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
28static 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 Davydovc0a5b562015-02-12 14:59:07 -080034
Kirill Tkhaifae91d62018-08-17 15:48:10 -070035static int lru_shrinker_id(struct list_lru *lru)
36{
37 return lru->shrinker_id;
38}
39
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080040static inline bool list_lru_memcg_aware(struct list_lru *lru)
41{
Jiri Slaby3e858992019-05-31 22:30:26 -070042 return lru->memcg_aware;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080043}
44
45static inline struct list_lru_one *
46list_lru_from_memcg_idx(struct list_lru_node *nlru, int idx)
47{
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -070048 struct list_lru_memcg *memcg_lrus;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080049 /*
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -070050 * Either lock or RCU protects the array of per cgroup lists
51 * from relocation (see memcg_update_list_lru_node).
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080052 */
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -070053 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 Davydov60d3fd32015-02-12 14:59:10 -080057 return &nlru->lru;
58}
59
60static inline struct list_lru_one *
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070061list_lru_from_kmem(struct list_lru_node *nlru, void *ptr,
62 struct mem_cgroup **memcg_ptr)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080063{
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070064 struct list_lru_one *l = &nlru->lru;
65 struct mem_cgroup *memcg = NULL;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080066
67 if (!nlru->memcg_lrus)
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070068 goto out;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080069
Roman Gushchin4f103c62020-04-01 21:06:36 -070070 memcg = mem_cgroup_from_obj(ptr);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080071 if (!memcg)
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070072 goto out;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080073
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070074 l = list_lru_from_memcg_idx(nlru, memcg_cache_id(memcg));
75out:
76 if (memcg_ptr)
77 *memcg_ptr = memcg;
78 return l;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080079}
80#else
Kirill Tkhaie0295232018-08-17 15:47:21 -070081static void list_lru_register(struct list_lru *lru)
82{
83}
84
85static void list_lru_unregister(struct list_lru *lru)
86{
87}
88
Kirill Tkhaifae91d62018-08-17 15:48:10 -070089static int lru_shrinker_id(struct list_lru *lru)
90{
91 return -1;
92}
93
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080094static inline bool list_lru_memcg_aware(struct list_lru *lru)
95{
96 return false;
97}
98
99static inline struct list_lru_one *
100list_lru_from_memcg_idx(struct list_lru_node *nlru, int idx)
101{
102 return &nlru->lru;
103}
104
105static inline struct list_lru_one *
Kirill Tkhai44bd4a42018-08-17 15:47:54 -0700106list_lru_from_kmem(struct list_lru_node *nlru, void *ptr,
107 struct mem_cgroup **memcg_ptr)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800108{
Kirill Tkhai44bd4a42018-08-17 15:47:54 -0700109 if (memcg_ptr)
110 *memcg_ptr = NULL;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800111 return &nlru->lru;
112}
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700113#endif /* CONFIG_MEMCG_KMEM */
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800114
Dave Chinnera38e4082013-08-28 10:17:58 +1000115bool list_lru_add(struct list_lru *lru, struct list_head *item)
116{
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000117 int nid = page_to_nid(virt_to_page(item));
118 struct list_lru_node *nlru = &lru->node[nid];
Kirill Tkhaifae91d62018-08-17 15:48:10 -0700119 struct mem_cgroup *memcg;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800120 struct list_lru_one *l;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000121
122 spin_lock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000123 if (list_empty(item)) {
Kirill Tkhaifae91d62018-08-17 15:48:10 -0700124 l = list_lru_from_kmem(nlru, item, &memcg);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800125 list_add_tail(item, &l->list);
Kirill Tkhaifae91d62018-08-17 15:48:10 -0700126 /* Set shrinker bit if the first element was added */
127 if (!l->nr_items++)
Yang Shi2bfd3632021-05-04 18:36:11 -0700128 set_shrinker_bit(memcg, nid,
129 lru_shrinker_id(lru));
Sahitya Tummala2c80cd52017-07-10 15:49:57 -0700130 nlru->nr_items++;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000131 spin_unlock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000132 return true;
133 }
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000134 spin_unlock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000135 return false;
136}
137EXPORT_SYMBOL_GPL(list_lru_add);
138
139bool list_lru_del(struct list_lru *lru, struct list_head *item)
140{
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000141 int nid = page_to_nid(virt_to_page(item));
142 struct list_lru_node *nlru = &lru->node[nid];
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800143 struct list_lru_one *l;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000144
145 spin_lock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000146 if (!list_empty(item)) {
Kirill Tkhai44bd4a42018-08-17 15:47:54 -0700147 l = list_lru_from_kmem(nlru, item, NULL);
Dave Chinnera38e4082013-08-28 10:17:58 +1000148 list_del_init(item);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800149 l->nr_items--;
Sahitya Tummala2c80cd52017-07-10 15:49:57 -0700150 nlru->nr_items--;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000151 spin_unlock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000152 return true;
153 }
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000154 spin_unlock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000155 return false;
156}
157EXPORT_SYMBOL_GPL(list_lru_del);
158
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800159void list_lru_isolate(struct list_lru_one *list, struct list_head *item)
160{
161 list_del_init(item);
162 list->nr_items--;
163}
164EXPORT_SYMBOL_GPL(list_lru_isolate);
165
166void 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}
172EXPORT_SYMBOL_GPL(list_lru_isolate_move);
173
Andrew Morton930eaac2018-08-17 15:46:11 -0700174unsigned long list_lru_count_one(struct list_lru *lru,
175 int nid, struct mem_cgroup *memcg)
Dave Chinnera38e4082013-08-28 10:17:58 +1000176{
Glauber Costa6a4f4962013-08-28 10:18:02 +1000177 struct list_lru_node *nlru = &lru->node[nid];
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800178 struct list_lru_one *l;
Muchun Song41d17432021-11-05 13:37:50 -0700179 long count;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000180
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700181 rcu_read_lock();
Andrew Morton930eaac2018-08-17 15:46:11 -0700182 l = list_lru_from_memcg_idx(nlru, memcg_cache_id(memcg));
Qian Caia1f45932020-08-14 17:31:41 -0700183 count = READ_ONCE(l->nr_items);
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700184 rcu_read_unlock();
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000185
Muchun Song41d17432021-11-05 13:37:50 -0700186 if (unlikely(count < 0))
187 count = 0;
188
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000189 return count;
190}
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800191EXPORT_SYMBOL_GPL(list_lru_count_one);
192
193unsigned long list_lru_count_node(struct list_lru *lru, int nid)
194{
Sahitya Tummala2c80cd52017-07-10 15:49:57 -0700195 struct list_lru_node *nlru;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800196
Sahitya Tummala2c80cd52017-07-10 15:49:57 -0700197 nlru = &lru->node[nid];
198 return nlru->nr_items;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800199}
Glauber Costa6a4f4962013-08-28 10:18:02 +1000200EXPORT_SYMBOL_GPL(list_lru_count_node);
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000201
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800202static unsigned long
Sebastian Andrzej Siewior6e018962018-08-17 15:49:51 -0700203__list_lru_walk_one(struct list_lru_node *nlru, int memcg_idx,
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800204 list_lru_walk_cb isolate, void *cb_arg,
205 unsigned long *nr_to_walk)
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000206{
207
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800208 struct list_lru_one *l;
Dave Chinnera38e4082013-08-28 10:17:58 +1000209 struct list_head *item, *n;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000210 unsigned long isolated = 0;
Dave Chinnera38e4082013-08-28 10:17:58 +1000211
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800212 l = list_lru_from_memcg_idx(nlru, memcg_idx);
Dave Chinnera38e4082013-08-28 10:17:58 +1000213restart:
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800214 list_for_each_safe(item, n, &l->list) {
Dave Chinnera38e4082013-08-28 10:17:58 +1000215 enum lru_status ret;
Dave Chinner5cedf7212013-08-28 10:18:01 +1000216
217 /*
218 * decrement nr_to_walk first so that we don't livelock if we
Ethon Paul3dc5f032020-06-04 16:49:19 -0700219 * get stuck on large numbers of LRU_RETRY items
Dave Chinner5cedf7212013-08-28 10:18:01 +1000220 */
Russell Kingc56b0972013-10-30 14:16:16 +0000221 if (!*nr_to_walk)
Dave Chinner5cedf7212013-08-28 10:18:01 +1000222 break;
Russell Kingc56b0972013-10-30 14:16:16 +0000223 --*nr_to_walk;
Dave Chinner5cedf7212013-08-28 10:18:01 +1000224
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800225 ret = isolate(item, l, &nlru->lock, cb_arg);
Dave Chinnera38e4082013-08-28 10:17:58 +1000226 switch (ret) {
Johannes Weiner449dd692014-04-03 14:47:56 -0700227 case LRU_REMOVED_RETRY:
228 assert_spin_locked(&nlru->lock);
Joe Perchese4a9bc52020-04-06 20:08:39 -0700229 fallthrough;
Dave Chinnera38e4082013-08-28 10:17:58 +1000230 case LRU_REMOVED:
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000231 isolated++;
Sahitya Tummala2c80cd52017-07-10 15:49:57 -0700232 nlru->nr_items--;
Johannes Weiner449dd692014-04-03 14:47:56 -0700233 /*
234 * If the lru lock has been dropped, our list
235 * traversal is now invalid and so we have to
236 * restart from scratch.
237 */
238 if (ret == LRU_REMOVED_RETRY)
239 goto restart;
Dave Chinnera38e4082013-08-28 10:17:58 +1000240 break;
241 case LRU_ROTATE:
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800242 list_move_tail(item, &l->list);
Dave Chinnera38e4082013-08-28 10:17:58 +1000243 break;
244 case LRU_SKIP:
245 break;
246 case LRU_RETRY:
Dave Chinner5cedf7212013-08-28 10:18:01 +1000247 /*
248 * The lru lock has been dropped, our list traversal is
249 * now invalid and so we have to restart from scratch.
250 */
Johannes Weiner449dd692014-04-03 14:47:56 -0700251 assert_spin_locked(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000252 goto restart;
253 default:
254 BUG();
255 }
Dave Chinnera38e4082013-08-28 10:17:58 +1000256 }
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000257 return isolated;
258}
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800259
260unsigned long
261list_lru_walk_one(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
262 list_lru_walk_cb isolate, void *cb_arg,
263 unsigned long *nr_to_walk)
264{
Sebastian Andrzej Siewior6cfe57a2018-08-17 15:49:48 -0700265 struct list_lru_node *nlru = &lru->node[nid];
266 unsigned long ret;
267
268 spin_lock(&nlru->lock);
Sebastian Andrzej Siewior6e018962018-08-17 15:49:51 -0700269 ret = __list_lru_walk_one(nlru, memcg_cache_id(memcg), isolate, cb_arg,
270 nr_to_walk);
Sebastian Andrzej Siewior6cfe57a2018-08-17 15:49:48 -0700271 spin_unlock(&nlru->lock);
272 return ret;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800273}
274EXPORT_SYMBOL_GPL(list_lru_walk_one);
275
Sebastian Andrzej Siewior6b51e882018-08-17 15:49:55 -0700276unsigned long
277list_lru_walk_one_irq(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
278 list_lru_walk_cb isolate, void *cb_arg,
279 unsigned long *nr_to_walk)
280{
281 struct list_lru_node *nlru = &lru->node[nid];
282 unsigned long ret;
283
284 spin_lock_irq(&nlru->lock);
285 ret = __list_lru_walk_one(nlru, memcg_cache_id(memcg), isolate, cb_arg,
286 nr_to_walk);
287 spin_unlock_irq(&nlru->lock);
288 return ret;
289}
290
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800291unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
292 list_lru_walk_cb isolate, void *cb_arg,
293 unsigned long *nr_to_walk)
294{
295 long isolated = 0;
296 int memcg_idx;
297
Sebastian Andrzej Siewior87a5ffc2018-08-17 15:49:45 -0700298 isolated += list_lru_walk_one(lru, nid, NULL, isolate, cb_arg,
299 nr_to_walk);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800300 if (*nr_to_walk > 0 && list_lru_memcg_aware(lru)) {
301 for_each_memcg_cache_index(memcg_idx) {
Sebastian Andrzej Siewior6cfe57a2018-08-17 15:49:48 -0700302 struct list_lru_node *nlru = &lru->node[nid];
303
304 spin_lock(&nlru->lock);
Sebastian Andrzej Siewior6e018962018-08-17 15:49:51 -0700305 isolated += __list_lru_walk_one(nlru, memcg_idx,
306 isolate, cb_arg,
307 nr_to_walk);
Sebastian Andrzej Siewior6cfe57a2018-08-17 15:49:48 -0700308 spin_unlock(&nlru->lock);
309
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800310 if (*nr_to_walk <= 0)
311 break;
312 }
313 }
314 return isolated;
315}
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000316EXPORT_SYMBOL_GPL(list_lru_walk_node);
317
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800318static void init_one_lru(struct list_lru_one *l)
319{
320 INIT_LIST_HEAD(&l->list);
321 l->nr_items = 0;
322}
323
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700324#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800325static void __memcg_destroy_list_lru_node(struct list_lru_memcg *memcg_lrus,
326 int begin, int end)
327{
328 int i;
329
330 for (i = begin; i < end; i++)
331 kfree(memcg_lrus->lru[i]);
332}
333
334static int __memcg_init_list_lru_node(struct list_lru_memcg *memcg_lrus,
335 int begin, int end)
336{
337 int i;
338
339 for (i = begin; i < end; i++) {
340 struct list_lru_one *l;
341
342 l = kmalloc(sizeof(struct list_lru_one), GFP_KERNEL);
343 if (!l)
344 goto fail;
345
346 init_one_lru(l);
347 memcg_lrus->lru[i] = l;
348 }
349 return 0;
350fail:
Shakeel Butt35109552019-06-13 15:55:49 -0700351 __memcg_destroy_list_lru_node(memcg_lrus, begin, i);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800352 return -ENOMEM;
353}
354
355static int memcg_init_list_lru_node(struct list_lru_node *nlru)
356{
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700357 struct list_lru_memcg *memcg_lrus;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800358 int size = memcg_nr_cache_ids;
359
Len Baker16f6bf22021-11-05 13:37:40 -0700360 memcg_lrus = kvmalloc(struct_size(memcg_lrus, lru, size), GFP_KERNEL);
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700361 if (!memcg_lrus)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800362 return -ENOMEM;
363
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700364 if (__memcg_init_list_lru_node(memcg_lrus, 0, size)) {
365 kvfree(memcg_lrus);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800366 return -ENOMEM;
367 }
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700368 RCU_INIT_POINTER(nlru->memcg_lrus, memcg_lrus);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800369
370 return 0;
371}
372
373static void memcg_destroy_list_lru_node(struct list_lru_node *nlru)
374{
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700375 struct list_lru_memcg *memcg_lrus;
376 /*
377 * This is called when shrinker has already been unregistered,
Shakeel Butta7b7e1d2021-02-24 12:04:12 -0800378 * and nobody can use it. So, there is no need to use kvfree_rcu().
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700379 */
380 memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus, true);
381 __memcg_destroy_list_lru_node(memcg_lrus, 0, memcg_nr_cache_ids);
382 kvfree(memcg_lrus);
383}
384
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800385static int memcg_update_list_lru_node(struct list_lru_node *nlru,
386 int old_size, int new_size)
387{
388 struct list_lru_memcg *old, *new;
389
390 BUG_ON(old_size > new_size);
391
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700392 old = rcu_dereference_protected(nlru->memcg_lrus,
393 lockdep_is_held(&list_lrus_mutex));
Len Baker16f6bf22021-11-05 13:37:40 -0700394 new = kvmalloc(struct_size(new, lru, new_size), GFP_KERNEL);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800395 if (!new)
396 return -ENOMEM;
397
398 if (__memcg_init_list_lru_node(new, old_size, new_size)) {
Johannes Weinerf80c7da2017-10-03 16:16:10 -0700399 kvfree(new);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800400 return -ENOMEM;
401 }
402
Len Baker16f6bf22021-11-05 13:37:40 -0700403 memcpy(&new->lru, &old->lru, flex_array_size(new, lru, old_size));
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700404 rcu_assign_pointer(nlru->memcg_lrus, new);
Shakeel Butta7b7e1d2021-02-24 12:04:12 -0800405 kvfree_rcu(old, rcu);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800406 return 0;
407}
408
409static void memcg_cancel_update_list_lru_node(struct list_lru_node *nlru,
410 int old_size, int new_size)
411{
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700412 struct list_lru_memcg *memcg_lrus;
413
414 memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus,
415 lockdep_is_held(&list_lrus_mutex));
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800416 /* do not bother shrinking the array back to the old size, because we
417 * cannot handle allocation failures here */
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700418 __memcg_destroy_list_lru_node(memcg_lrus, old_size, new_size);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800419}
420
421static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)
422{
423 int i;
424
Jiri Slaby3e858992019-05-31 22:30:26 -0700425 lru->memcg_aware = memcg_aware;
426
Raghavendra K T145949a2015-11-05 18:46:26 -0800427 if (!memcg_aware)
428 return 0;
429
430 for_each_node(i) {
431 if (memcg_init_list_lru_node(&lru->node[i]))
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800432 goto fail;
433 }
434 return 0;
435fail:
Raghavendra K T145949a2015-11-05 18:46:26 -0800436 for (i = i - 1; i >= 0; i--) {
437 if (!lru->node[i].memcg_lrus)
438 continue;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800439 memcg_destroy_list_lru_node(&lru->node[i]);
Raghavendra K T145949a2015-11-05 18:46:26 -0800440 }
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800441 return -ENOMEM;
442}
443
444static void memcg_destroy_list_lru(struct list_lru *lru)
445{
446 int i;
447
448 if (!list_lru_memcg_aware(lru))
449 return;
450
Raghavendra K T145949a2015-11-05 18:46:26 -0800451 for_each_node(i)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800452 memcg_destroy_list_lru_node(&lru->node[i]);
453}
454
455static int memcg_update_list_lru(struct list_lru *lru,
456 int old_size, int new_size)
457{
458 int i;
459
460 if (!list_lru_memcg_aware(lru))
461 return 0;
462
Raghavendra K T145949a2015-11-05 18:46:26 -0800463 for_each_node(i) {
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800464 if (memcg_update_list_lru_node(&lru->node[i],
465 old_size, new_size))
466 goto fail;
467 }
468 return 0;
469fail:
Raghavendra K T145949a2015-11-05 18:46:26 -0800470 for (i = i - 1; i >= 0; i--) {
471 if (!lru->node[i].memcg_lrus)
472 continue;
473
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800474 memcg_cancel_update_list_lru_node(&lru->node[i],
475 old_size, new_size);
Raghavendra K T145949a2015-11-05 18:46:26 -0800476 }
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800477 return -ENOMEM;
478}
479
480static void memcg_cancel_update_list_lru(struct list_lru *lru,
481 int old_size, int new_size)
482{
483 int i;
484
485 if (!list_lru_memcg_aware(lru))
486 return;
487
Raghavendra K T145949a2015-11-05 18:46:26 -0800488 for_each_node(i)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800489 memcg_cancel_update_list_lru_node(&lru->node[i],
490 old_size, new_size);
491}
492
493int memcg_update_all_list_lrus(int new_size)
494{
495 int ret = 0;
496 struct list_lru *lru;
497 int old_size = memcg_nr_cache_ids;
498
499 mutex_lock(&list_lrus_mutex);
500 list_for_each_entry(lru, &list_lrus, list) {
501 ret = memcg_update_list_lru(lru, old_size, new_size);
502 if (ret)
503 goto fail;
504 }
505out:
506 mutex_unlock(&list_lrus_mutex);
507 return ret;
508fail:
509 list_for_each_entry_continue_reverse(lru, &list_lrus, list)
510 memcg_cancel_update_list_lru(lru, old_size, new_size);
511 goto out;
512}
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800513
Kirill Tkhai3b82c4d2018-08-17 15:48:01 -0700514static void memcg_drain_list_lru_node(struct list_lru *lru, int nid,
Kirill Tkhai9bec5c32018-08-17 15:47:58 -0700515 int src_idx, struct mem_cgroup *dst_memcg)
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800516{
Kirill Tkhai3b82c4d2018-08-17 15:48:01 -0700517 struct list_lru_node *nlru = &lru->node[nid];
Kirill Tkhai9bec5c32018-08-17 15:47:58 -0700518 int dst_idx = dst_memcg->kmemcg_id;
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800519 struct list_lru_one *src, *dst;
520
521 /*
522 * Since list_lru_{add,del} may be called under an IRQ-safe lock,
523 * we have to use IRQ-safe primitives here to avoid deadlock.
524 */
525 spin_lock_irq(&nlru->lock);
526
527 src = list_lru_from_memcg_idx(nlru, src_idx);
528 dst = list_lru_from_memcg_idx(nlru, dst_idx);
529
530 list_splice_init(&src->list, &dst->list);
Yang Shi8199be02020-12-05 22:14:48 -0800531
532 if (src->nr_items) {
533 dst->nr_items += src->nr_items;
Yang Shi2bfd3632021-05-04 18:36:11 -0700534 set_shrinker_bit(dst_memcg, nid, lru_shrinker_id(lru));
Yang Shi8199be02020-12-05 22:14:48 -0800535 src->nr_items = 0;
536 }
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800537
538 spin_unlock_irq(&nlru->lock);
539}
540
541static void memcg_drain_list_lru(struct list_lru *lru,
Kirill Tkhai9bec5c32018-08-17 15:47:58 -0700542 int src_idx, struct mem_cgroup *dst_memcg)
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800543{
544 int i;
545
546 if (!list_lru_memcg_aware(lru))
547 return;
548
Raghavendra K T145949a2015-11-05 18:46:26 -0800549 for_each_node(i)
Kirill Tkhai3b82c4d2018-08-17 15:48:01 -0700550 memcg_drain_list_lru_node(lru, i, src_idx, dst_memcg);
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800551}
552
Kirill Tkhai9bec5c32018-08-17 15:47:58 -0700553void memcg_drain_all_list_lrus(int src_idx, struct mem_cgroup *dst_memcg)
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800554{
555 struct list_lru *lru;
556
557 mutex_lock(&list_lrus_mutex);
558 list_for_each_entry(lru, &list_lrus, list)
Kirill Tkhai9bec5c32018-08-17 15:47:58 -0700559 memcg_drain_list_lru(lru, src_idx, dst_memcg);
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800560 mutex_unlock(&list_lrus_mutex);
561}
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800562#else
563static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)
564{
565 return 0;
566}
567
568static void memcg_destroy_list_lru(struct list_lru *lru)
569{
570}
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700571#endif /* CONFIG_MEMCG_KMEM */
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800572
573int __list_lru_init(struct list_lru *lru, bool memcg_aware,
Kirill Tkhaic92e8e12018-08-17 15:47:50 -0700574 struct lock_class_key *key, struct shrinker *shrinker)
Dave Chinnera38e4082013-08-28 10:17:58 +1000575{
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000576 int i;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800577 int err = -ENOMEM;
578
Kirill Tkhaic92e8e12018-08-17 15:47:50 -0700579#ifdef CONFIG_MEMCG_KMEM
580 if (shrinker)
581 lru->shrinker_id = shrinker->id;
582 else
583 lru->shrinker_id = -1;
584#endif
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800585 memcg_get_cache_ids();
Glauber Costa5ca302c2013-08-28 10:18:18 +1000586
Alexey Dobriyanb9726c22019-03-05 15:48:26 -0800587 lru->node = kcalloc(nr_node_ids, sizeof(*lru->node), GFP_KERNEL);
Glauber Costa5ca302c2013-08-28 10:18:18 +1000588 if (!lru->node)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800589 goto out;
Dave Chinnera38e4082013-08-28 10:17:58 +1000590
Raghavendra K T145949a2015-11-05 18:46:26 -0800591 for_each_node(i) {
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000592 spin_lock_init(&lru->node[i].lock);
Johannes Weiner449dd692014-04-03 14:47:56 -0700593 if (key)
594 lockdep_set_class(&lru->node[i].lock, key);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800595 init_one_lru(&lru->node[i].lru);
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000596 }
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800597
598 err = memcg_init_list_lru(lru, memcg_aware);
599 if (err) {
600 kfree(lru->node);
Alexander Polakov1bc11d72016-10-27 17:46:27 -0700601 /* Do this so a list_lru_destroy() doesn't crash: */
602 lru->node = NULL;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800603 goto out;
604 }
605
Vladimir Davydovc0a5b562015-02-12 14:59:07 -0800606 list_lru_register(lru);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800607out:
608 memcg_put_cache_ids();
609 return err;
Dave Chinnera38e4082013-08-28 10:17:58 +1000610}
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800611EXPORT_SYMBOL_GPL(__list_lru_init);
Glauber Costa5ca302c2013-08-28 10:18:18 +1000612
613void list_lru_destroy(struct list_lru *lru)
614{
Vladimir Davydovc0a5b562015-02-12 14:59:07 -0800615 /* Already destroyed or not yet initialized? */
616 if (!lru->node)
617 return;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800618
619 memcg_get_cache_ids();
620
Vladimir Davydovc0a5b562015-02-12 14:59:07 -0800621 list_lru_unregister(lru);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800622
623 memcg_destroy_list_lru(lru);
Glauber Costa5ca302c2013-08-28 10:18:18 +1000624 kfree(lru->node);
Vladimir Davydovc0a5b562015-02-12 14:59:07 -0800625 lru->node = NULL;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800626
Kirill Tkhaic92e8e12018-08-17 15:47:50 -0700627#ifdef CONFIG_MEMCG_KMEM
628 lru->shrinker_id = -1;
629#endif
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800630 memcg_put_cache_ids();
Glauber Costa5ca302c2013-08-28 10:18:18 +1000631}
632EXPORT_SYMBOL_GPL(list_lru_destroy);