blob: 0cd5e89ca0631fa0ed16ec522dad263f0346ea77 [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
Muchun Song3eef1122021-11-05 13:37:59 -070018static LIST_HEAD(memcg_list_lrus);
Vladimir Davydovc0a5b562015-02-12 14:59:07 -080019static DEFINE_MUTEX(list_lrus_mutex);
20
Muchun Song3eef1122021-11-05 13:37:59 -070021static inline bool list_lru_memcg_aware(struct list_lru *lru)
22{
23 return lru->memcg_aware;
24}
25
Vladimir Davydovc0a5b562015-02-12 14:59:07 -080026static void list_lru_register(struct list_lru *lru)
27{
Muchun Song3eef1122021-11-05 13:37:59 -070028 if (!list_lru_memcg_aware(lru))
29 return;
30
Vladimir Davydovc0a5b562015-02-12 14:59:07 -080031 mutex_lock(&list_lrus_mutex);
Muchun Song3eef1122021-11-05 13:37:59 -070032 list_add(&lru->list, &memcg_list_lrus);
Vladimir Davydovc0a5b562015-02-12 14:59:07 -080033 mutex_unlock(&list_lrus_mutex);
34}
35
36static void list_lru_unregister(struct list_lru *lru)
37{
Muchun Song3eef1122021-11-05 13:37:59 -070038 if (!list_lru_memcg_aware(lru))
39 return;
40
Vladimir Davydovc0a5b562015-02-12 14:59:07 -080041 mutex_lock(&list_lrus_mutex);
42 list_del(&lru->list);
43 mutex_unlock(&list_lrus_mutex);
44}
Vladimir Davydovc0a5b562015-02-12 14:59:07 -080045
Kirill Tkhaifae91d62018-08-17 15:48:10 -070046static int lru_shrinker_id(struct list_lru *lru)
47{
48 return lru->shrinker_id;
49}
50
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080051static inline struct list_lru_one *
52list_lru_from_memcg_idx(struct list_lru_node *nlru, int idx)
53{
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -070054 struct list_lru_memcg *memcg_lrus;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080055 /*
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -070056 * Either lock or RCU protects the array of per cgroup lists
57 * from relocation (see memcg_update_list_lru_node).
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080058 */
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -070059 memcg_lrus = rcu_dereference_check(nlru->memcg_lrus,
60 lockdep_is_held(&nlru->lock));
61 if (memcg_lrus && idx >= 0)
62 return memcg_lrus->lru[idx];
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080063 return &nlru->lru;
64}
65
66static inline struct list_lru_one *
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070067list_lru_from_kmem(struct list_lru_node *nlru, void *ptr,
68 struct mem_cgroup **memcg_ptr)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080069{
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070070 struct list_lru_one *l = &nlru->lru;
71 struct mem_cgroup *memcg = NULL;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080072
73 if (!nlru->memcg_lrus)
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070074 goto out;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080075
Roman Gushchin4f103c62020-04-01 21:06:36 -070076 memcg = mem_cgroup_from_obj(ptr);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080077 if (!memcg)
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070078 goto out;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080079
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070080 l = list_lru_from_memcg_idx(nlru, memcg_cache_id(memcg));
81out:
82 if (memcg_ptr)
83 *memcg_ptr = memcg;
84 return l;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080085}
86#else
Kirill Tkhaie0295232018-08-17 15:47:21 -070087static void list_lru_register(struct list_lru *lru)
88{
89}
90
91static void list_lru_unregister(struct list_lru *lru)
92{
93}
94
Kirill Tkhaifae91d62018-08-17 15:48:10 -070095static int lru_shrinker_id(struct list_lru *lru)
96{
97 return -1;
98}
99
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800100static inline bool list_lru_memcg_aware(struct list_lru *lru)
101{
102 return false;
103}
104
105static inline struct list_lru_one *
106list_lru_from_memcg_idx(struct list_lru_node *nlru, int idx)
107{
108 return &nlru->lru;
109}
110
111static inline struct list_lru_one *
Kirill Tkhai44bd4a42018-08-17 15:47:54 -0700112list_lru_from_kmem(struct list_lru_node *nlru, void *ptr,
113 struct mem_cgroup **memcg_ptr)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800114{
Kirill Tkhai44bd4a42018-08-17 15:47:54 -0700115 if (memcg_ptr)
116 *memcg_ptr = NULL;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800117 return &nlru->lru;
118}
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700119#endif /* CONFIG_MEMCG_KMEM */
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800120
Dave Chinnera38e4082013-08-28 10:17:58 +1000121bool list_lru_add(struct list_lru *lru, struct list_head *item)
122{
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000123 int nid = page_to_nid(virt_to_page(item));
124 struct list_lru_node *nlru = &lru->node[nid];
Kirill Tkhaifae91d62018-08-17 15:48:10 -0700125 struct mem_cgroup *memcg;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800126 struct list_lru_one *l;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000127
128 spin_lock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000129 if (list_empty(item)) {
Kirill Tkhaifae91d62018-08-17 15:48:10 -0700130 l = list_lru_from_kmem(nlru, item, &memcg);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800131 list_add_tail(item, &l->list);
Kirill Tkhaifae91d62018-08-17 15:48:10 -0700132 /* Set shrinker bit if the first element was added */
133 if (!l->nr_items++)
Yang Shi2bfd3632021-05-04 18:36:11 -0700134 set_shrinker_bit(memcg, nid,
135 lru_shrinker_id(lru));
Sahitya Tummala2c80cd52017-07-10 15:49:57 -0700136 nlru->nr_items++;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000137 spin_unlock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000138 return true;
139 }
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000140 spin_unlock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000141 return false;
142}
143EXPORT_SYMBOL_GPL(list_lru_add);
144
145bool list_lru_del(struct list_lru *lru, struct list_head *item)
146{
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000147 int nid = page_to_nid(virt_to_page(item));
148 struct list_lru_node *nlru = &lru->node[nid];
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800149 struct list_lru_one *l;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000150
151 spin_lock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000152 if (!list_empty(item)) {
Kirill Tkhai44bd4a42018-08-17 15:47:54 -0700153 l = list_lru_from_kmem(nlru, item, NULL);
Dave Chinnera38e4082013-08-28 10:17:58 +1000154 list_del_init(item);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800155 l->nr_items--;
Sahitya Tummala2c80cd52017-07-10 15:49:57 -0700156 nlru->nr_items--;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000157 spin_unlock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000158 return true;
159 }
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000160 spin_unlock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000161 return false;
162}
163EXPORT_SYMBOL_GPL(list_lru_del);
164
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800165void list_lru_isolate(struct list_lru_one *list, struct list_head *item)
166{
167 list_del_init(item);
168 list->nr_items--;
169}
170EXPORT_SYMBOL_GPL(list_lru_isolate);
171
172void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item,
173 struct list_head *head)
174{
175 list_move(item, head);
176 list->nr_items--;
177}
178EXPORT_SYMBOL_GPL(list_lru_isolate_move);
179
Andrew Morton930eaac2018-08-17 15:46:11 -0700180unsigned long list_lru_count_one(struct list_lru *lru,
181 int nid, struct mem_cgroup *memcg)
Dave Chinnera38e4082013-08-28 10:17:58 +1000182{
Glauber Costa6a4f4962013-08-28 10:18:02 +1000183 struct list_lru_node *nlru = &lru->node[nid];
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800184 struct list_lru_one *l;
Muchun Song41d17432021-11-05 13:37:50 -0700185 long count;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000186
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700187 rcu_read_lock();
Andrew Morton930eaac2018-08-17 15:46:11 -0700188 l = list_lru_from_memcg_idx(nlru, memcg_cache_id(memcg));
Qian Caia1f45932020-08-14 17:31:41 -0700189 count = READ_ONCE(l->nr_items);
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700190 rcu_read_unlock();
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000191
Muchun Song41d17432021-11-05 13:37:50 -0700192 if (unlikely(count < 0))
193 count = 0;
194
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000195 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
Ethon Paul3dc5f032020-06-04 16:49:19 -0700225 * get stuck on large numbers of LRU_RETRY items
Dave Chinner5cedf7212013-08-28 10:18:01 +1000226 */
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);
Joe Perchese4a9bc52020-04-06 20:08:39 -0700235 fallthrough;
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:
Shakeel Butt35109552019-06-13 15:55:49 -0700357 __memcg_destroy_list_lru_node(memcg_lrus, begin, i);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800358 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
Len Baker16f6bf22021-11-05 13:37:40 -0700366 memcg_lrus = kvmalloc(struct_size(memcg_lrus, lru, size), GFP_KERNEL);
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700367 if (!memcg_lrus)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800368 return -ENOMEM;
369
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700370 if (__memcg_init_list_lru_node(memcg_lrus, 0, size)) {
371 kvfree(memcg_lrus);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800372 return -ENOMEM;
373 }
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700374 RCU_INIT_POINTER(nlru->memcg_lrus, memcg_lrus);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800375
376 return 0;
377}
378
379static void memcg_destroy_list_lru_node(struct list_lru_node *nlru)
380{
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700381 struct list_lru_memcg *memcg_lrus;
382 /*
383 * This is called when shrinker has already been unregistered,
Shakeel Butta7b7e1d2021-02-24 12:04:12 -0800384 * and nobody can use it. So, there is no need to use kvfree_rcu().
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700385 */
386 memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus, true);
387 __memcg_destroy_list_lru_node(memcg_lrus, 0, memcg_nr_cache_ids);
388 kvfree(memcg_lrus);
389}
390
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800391static int memcg_update_list_lru_node(struct list_lru_node *nlru,
392 int old_size, int new_size)
393{
394 struct list_lru_memcg *old, *new;
395
396 BUG_ON(old_size > new_size);
397
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700398 old = rcu_dereference_protected(nlru->memcg_lrus,
399 lockdep_is_held(&list_lrus_mutex));
Len Baker16f6bf22021-11-05 13:37:40 -0700400 new = kvmalloc(struct_size(new, lru, new_size), GFP_KERNEL);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800401 if (!new)
402 return -ENOMEM;
403
404 if (__memcg_init_list_lru_node(new, old_size, new_size)) {
Johannes Weinerf80c7da2017-10-03 16:16:10 -0700405 kvfree(new);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800406 return -ENOMEM;
407 }
408
Len Baker16f6bf22021-11-05 13:37:40 -0700409 memcpy(&new->lru, &old->lru, flex_array_size(new, lru, old_size));
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700410 rcu_assign_pointer(nlru->memcg_lrus, new);
Shakeel Butta7b7e1d2021-02-24 12:04:12 -0800411 kvfree_rcu(old, rcu);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800412 return 0;
413}
414
415static void memcg_cancel_update_list_lru_node(struct list_lru_node *nlru,
416 int old_size, int new_size)
417{
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700418 struct list_lru_memcg *memcg_lrus;
419
420 memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus,
421 lockdep_is_held(&list_lrus_mutex));
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800422 /* do not bother shrinking the array back to the old size, because we
423 * cannot handle allocation failures here */
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700424 __memcg_destroy_list_lru_node(memcg_lrus, old_size, new_size);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800425}
426
427static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)
428{
429 int i;
430
Jiri Slaby3e858992019-05-31 22:30:26 -0700431 lru->memcg_aware = memcg_aware;
432
Raghavendra K T145949a2015-11-05 18:46:26 -0800433 if (!memcg_aware)
434 return 0;
435
436 for_each_node(i) {
437 if (memcg_init_list_lru_node(&lru->node[i]))
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800438 goto fail;
439 }
440 return 0;
441fail:
Raghavendra K T145949a2015-11-05 18:46:26 -0800442 for (i = i - 1; i >= 0; i--) {
443 if (!lru->node[i].memcg_lrus)
444 continue;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800445 memcg_destroy_list_lru_node(&lru->node[i]);
Raghavendra K T145949a2015-11-05 18:46:26 -0800446 }
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800447 return -ENOMEM;
448}
449
450static void memcg_destroy_list_lru(struct list_lru *lru)
451{
452 int i;
453
454 if (!list_lru_memcg_aware(lru))
455 return;
456
Raghavendra K T145949a2015-11-05 18:46:26 -0800457 for_each_node(i)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800458 memcg_destroy_list_lru_node(&lru->node[i]);
459}
460
461static int memcg_update_list_lru(struct list_lru *lru,
462 int old_size, int new_size)
463{
464 int i;
465
Raghavendra K T145949a2015-11-05 18:46:26 -0800466 for_each_node(i) {
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800467 if (memcg_update_list_lru_node(&lru->node[i],
468 old_size, new_size))
469 goto fail;
470 }
471 return 0;
472fail:
Raghavendra K T145949a2015-11-05 18:46:26 -0800473 for (i = i - 1; i >= 0; i--) {
474 if (!lru->node[i].memcg_lrus)
475 continue;
476
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800477 memcg_cancel_update_list_lru_node(&lru->node[i],
478 old_size, new_size);
Raghavendra K T145949a2015-11-05 18:46:26 -0800479 }
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800480 return -ENOMEM;
481}
482
483static void memcg_cancel_update_list_lru(struct list_lru *lru,
484 int old_size, int new_size)
485{
486 int i;
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);
Muchun Song3eef1122021-11-05 13:37:59 -0700500 list_for_each_entry(lru, &memcg_list_lrus, list) {
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800501 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:
Muchun Song3eef1122021-11-05 13:37:59 -0700509 list_for_each_entry_continue_reverse(lru, &memcg_list_lrus, list)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800510 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
Raghavendra K T145949a2015-11-05 18:46:26 -0800546 for_each_node(i)
Kirill Tkhai3b82c4d2018-08-17 15:48:01 -0700547 memcg_drain_list_lru_node(lru, i, src_idx, dst_memcg);
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800548}
549
Kirill Tkhai9bec5c32018-08-17 15:47:58 -0700550void memcg_drain_all_list_lrus(int src_idx, struct mem_cgroup *dst_memcg)
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800551{
552 struct list_lru *lru;
553
554 mutex_lock(&list_lrus_mutex);
Muchun Song3eef1122021-11-05 13:37:59 -0700555 list_for_each_entry(lru, &memcg_list_lrus, list)
Kirill Tkhai9bec5c32018-08-17 15:47:58 -0700556 memcg_drain_list_lru(lru, src_idx, dst_memcg);
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800557 mutex_unlock(&list_lrus_mutex);
558}
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800559#else
560static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)
561{
562 return 0;
563}
564
565static void memcg_destroy_list_lru(struct list_lru *lru)
566{
567}
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700568#endif /* CONFIG_MEMCG_KMEM */
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800569
570int __list_lru_init(struct list_lru *lru, bool memcg_aware,
Kirill Tkhaic92e8e12018-08-17 15:47:50 -0700571 struct lock_class_key *key, struct shrinker *shrinker)
Dave Chinnera38e4082013-08-28 10:17:58 +1000572{
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000573 int i;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800574 int err = -ENOMEM;
575
Kirill Tkhaic92e8e12018-08-17 15:47:50 -0700576#ifdef CONFIG_MEMCG_KMEM
577 if (shrinker)
578 lru->shrinker_id = shrinker->id;
579 else
580 lru->shrinker_id = -1;
581#endif
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800582 memcg_get_cache_ids();
Glauber Costa5ca302c2013-08-28 10:18:18 +1000583
Alexey Dobriyanb9726c22019-03-05 15:48:26 -0800584 lru->node = kcalloc(nr_node_ids, sizeof(*lru->node), GFP_KERNEL);
Glauber Costa5ca302c2013-08-28 10:18:18 +1000585 if (!lru->node)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800586 goto out;
Dave Chinnera38e4082013-08-28 10:17:58 +1000587
Raghavendra K T145949a2015-11-05 18:46:26 -0800588 for_each_node(i) {
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000589 spin_lock_init(&lru->node[i].lock);
Johannes Weiner449dd692014-04-03 14:47:56 -0700590 if (key)
591 lockdep_set_class(&lru->node[i].lock, key);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800592 init_one_lru(&lru->node[i].lru);
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000593 }
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800594
595 err = memcg_init_list_lru(lru, memcg_aware);
596 if (err) {
597 kfree(lru->node);
Alexander Polakov1bc11d72016-10-27 17:46:27 -0700598 /* Do this so a list_lru_destroy() doesn't crash: */
599 lru->node = NULL;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800600 goto out;
601 }
602
Vladimir Davydovc0a5b562015-02-12 14:59:07 -0800603 list_lru_register(lru);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800604out:
605 memcg_put_cache_ids();
606 return err;
Dave Chinnera38e4082013-08-28 10:17:58 +1000607}
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800608EXPORT_SYMBOL_GPL(__list_lru_init);
Glauber Costa5ca302c2013-08-28 10:18:18 +1000609
610void list_lru_destroy(struct list_lru *lru)
611{
Vladimir Davydovc0a5b562015-02-12 14:59:07 -0800612 /* Already destroyed or not yet initialized? */
613 if (!lru->node)
614 return;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800615
616 memcg_get_cache_ids();
617
Vladimir Davydovc0a5b562015-02-12 14:59:07 -0800618 list_lru_unregister(lru);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800619
620 memcg_destroy_list_lru(lru);
Glauber Costa5ca302c2013-08-28 10:18:18 +1000621 kfree(lru->node);
Vladimir Davydovc0a5b562015-02-12 14:59:07 -0800622 lru->node = NULL;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800623
Kirill Tkhaic92e8e12018-08-17 15:47:50 -0700624#ifdef CONFIG_MEMCG_KMEM
625 lru->shrinker_id = -1;
626#endif
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800627 memcg_put_cache_ids();
Glauber Costa5ca302c2013-08-28 10:18:18 +1000628}
629EXPORT_SYMBOL_GPL(list_lru_destroy);