blob: 5384cda08984603fb93fb6dc37cf787009e944ea [file] [log] [blame]
Dave Chinnera38e4082013-08-28 10:17:58 +10001/*
2 * Copyright (c) 2013 Red Hat, Inc. and Parallels Inc. All rights reserved.
3 * Authors: David Chinner and Glauber Costa
4 *
5 * Generic LRU infrastructure
6 */
7#include <linux/kernel.h>
8#include <linux/module.h>
Dave Chinner3b1d58a2013-08-28 10:18:00 +10009#include <linux/mm.h>
Dave Chinnera38e4082013-08-28 10:17:58 +100010#include <linux/list_lru.h>
Glauber Costa5ca302c2013-08-28 10:18:18 +100011#include <linux/slab.h>
Vladimir Davydovc0a5b562015-02-12 14:59:07 -080012#include <linux/mutex.h>
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080013#include <linux/memcontrol.h>
Vladimir Davydovc0a5b562015-02-12 14:59:07 -080014
Kirill Tkhai84c07d12018-08-17 15:47:25 -070015#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydovc0a5b562015-02-12 14:59:07 -080016static LIST_HEAD(list_lrus);
17static DEFINE_MUTEX(list_lrus_mutex);
18
19static void list_lru_register(struct list_lru *lru)
20{
21 mutex_lock(&list_lrus_mutex);
22 list_add(&lru->list, &list_lrus);
23 mutex_unlock(&list_lrus_mutex);
24}
25
26static void list_lru_unregister(struct list_lru *lru)
27{
28 mutex_lock(&list_lrus_mutex);
29 list_del(&lru->list);
30 mutex_unlock(&list_lrus_mutex);
31}
Vladimir Davydovc0a5b562015-02-12 14:59:07 -080032
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080033static inline bool list_lru_memcg_aware(struct list_lru *lru)
34{
Raghavendra K T145949a2015-11-05 18:46:26 -080035 /*
36 * This needs node 0 to be always present, even
37 * in the systems supporting sparse numa ids.
38 */
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080039 return !!lru->node[0].memcg_lrus;
40}
41
42static inline struct list_lru_one *
43list_lru_from_memcg_idx(struct list_lru_node *nlru, int idx)
44{
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -070045 struct list_lru_memcg *memcg_lrus;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080046 /*
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -070047 * Either lock or RCU protects the array of per cgroup lists
48 * from relocation (see memcg_update_list_lru_node).
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080049 */
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -070050 memcg_lrus = rcu_dereference_check(nlru->memcg_lrus,
51 lockdep_is_held(&nlru->lock));
52 if (memcg_lrus && idx >= 0)
53 return memcg_lrus->lru[idx];
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080054 return &nlru->lru;
55}
56
Vladimir Davydovdf406552015-11-05 18:49:04 -080057static __always_inline struct mem_cgroup *mem_cgroup_from_kmem(void *ptr)
58{
59 struct page *page;
60
61 if (!memcg_kmem_enabled())
62 return NULL;
63 page = virt_to_head_page(ptr);
64 return page->mem_cgroup;
65}
66
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080067static inline struct list_lru_one *
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070068list_lru_from_kmem(struct list_lru_node *nlru, void *ptr,
69 struct mem_cgroup **memcg_ptr)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080070{
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070071 struct list_lru_one *l = &nlru->lru;
72 struct mem_cgroup *memcg = NULL;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080073
74 if (!nlru->memcg_lrus)
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070075 goto out;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080076
77 memcg = mem_cgroup_from_kmem(ptr);
78 if (!memcg)
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070079 goto out;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080080
Kirill Tkhai44bd4a42018-08-17 15:47:54 -070081 l = list_lru_from_memcg_idx(nlru, memcg_cache_id(memcg));
82out:
83 if (memcg_ptr)
84 *memcg_ptr = memcg;
85 return l;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080086}
87#else
Kirill Tkhaie0295232018-08-17 15:47:21 -070088static void list_lru_register(struct list_lru *lru)
89{
90}
91
92static void list_lru_unregister(struct list_lru *lru)
93{
94}
95
Vladimir Davydov60d3fd32015-02-12 14:59:10 -080096static inline bool list_lru_memcg_aware(struct list_lru *lru)
97{
98 return false;
99}
100
101static inline struct list_lru_one *
102list_lru_from_memcg_idx(struct list_lru_node *nlru, int idx)
103{
104 return &nlru->lru;
105}
106
107static inline struct list_lru_one *
Kirill Tkhai44bd4a42018-08-17 15:47:54 -0700108list_lru_from_kmem(struct list_lru_node *nlru, void *ptr,
109 struct mem_cgroup **memcg_ptr)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800110{
Kirill Tkhai44bd4a42018-08-17 15:47:54 -0700111 if (memcg_ptr)
112 *memcg_ptr = NULL;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800113 return &nlru->lru;
114}
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700115#endif /* CONFIG_MEMCG_KMEM */
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800116
Dave Chinnera38e4082013-08-28 10:17:58 +1000117bool list_lru_add(struct list_lru *lru, struct list_head *item)
118{
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000119 int nid = page_to_nid(virt_to_page(item));
120 struct list_lru_node *nlru = &lru->node[nid];
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800121 struct list_lru_one *l;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000122
123 spin_lock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000124 if (list_empty(item)) {
Kirill Tkhai44bd4a42018-08-17 15:47:54 -0700125 l = list_lru_from_kmem(nlru, item, NULL);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800126 list_add_tail(item, &l->list);
127 l->nr_items++;
Sahitya Tummala2c80cd52017-07-10 15:49:57 -0700128 nlru->nr_items++;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000129 spin_unlock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000130 return true;
131 }
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000132 spin_unlock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000133 return false;
134}
135EXPORT_SYMBOL_GPL(list_lru_add);
136
137bool list_lru_del(struct list_lru *lru, struct list_head *item)
138{
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000139 int nid = page_to_nid(virt_to_page(item));
140 struct list_lru_node *nlru = &lru->node[nid];
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800141 struct list_lru_one *l;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000142
143 spin_lock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000144 if (!list_empty(item)) {
Kirill Tkhai44bd4a42018-08-17 15:47:54 -0700145 l = list_lru_from_kmem(nlru, item, NULL);
Dave Chinnera38e4082013-08-28 10:17:58 +1000146 list_del_init(item);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800147 l->nr_items--;
Sahitya Tummala2c80cd52017-07-10 15:49:57 -0700148 nlru->nr_items--;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000149 spin_unlock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000150 return true;
151 }
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000152 spin_unlock(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000153 return false;
154}
155EXPORT_SYMBOL_GPL(list_lru_del);
156
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800157void list_lru_isolate(struct list_lru_one *list, struct list_head *item)
158{
159 list_del_init(item);
160 list->nr_items--;
161}
162EXPORT_SYMBOL_GPL(list_lru_isolate);
163
164void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item,
165 struct list_head *head)
166{
167 list_move(item, head);
168 list->nr_items--;
169}
170EXPORT_SYMBOL_GPL(list_lru_isolate_move);
171
Andrew Morton930eaac2018-08-17 15:46:11 -0700172unsigned long list_lru_count_one(struct list_lru *lru,
173 int nid, struct mem_cgroup *memcg)
Dave Chinnera38e4082013-08-28 10:17:58 +1000174{
Glauber Costa6a4f4962013-08-28 10:18:02 +1000175 struct list_lru_node *nlru = &lru->node[nid];
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800176 struct list_lru_one *l;
177 unsigned long count;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000178
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700179 rcu_read_lock();
Andrew Morton930eaac2018-08-17 15:46:11 -0700180 l = list_lru_from_memcg_idx(nlru, memcg_cache_id(memcg));
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800181 count = l->nr_items;
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700182 rcu_read_unlock();
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000183
184 return count;
185}
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800186EXPORT_SYMBOL_GPL(list_lru_count_one);
187
188unsigned long list_lru_count_node(struct list_lru *lru, int nid)
189{
Sahitya Tummala2c80cd52017-07-10 15:49:57 -0700190 struct list_lru_node *nlru;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800191
Sahitya Tummala2c80cd52017-07-10 15:49:57 -0700192 nlru = &lru->node[nid];
193 return nlru->nr_items;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800194}
Glauber Costa6a4f4962013-08-28 10:18:02 +1000195EXPORT_SYMBOL_GPL(list_lru_count_node);
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000196
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800197static unsigned long
198__list_lru_walk_one(struct list_lru *lru, int nid, int memcg_idx,
199 list_lru_walk_cb isolate, void *cb_arg,
200 unsigned long *nr_to_walk)
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000201{
202
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800203 struct list_lru_node *nlru = &lru->node[nid];
204 struct list_lru_one *l;
Dave Chinnera38e4082013-08-28 10:17:58 +1000205 struct list_head *item, *n;
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000206 unsigned long isolated = 0;
Dave Chinnera38e4082013-08-28 10:17:58 +1000207
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000208 spin_lock(&nlru->lock);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800209 l = list_lru_from_memcg_idx(nlru, memcg_idx);
Dave Chinnera38e4082013-08-28 10:17:58 +1000210restart:
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800211 list_for_each_safe(item, n, &l->list) {
Dave Chinnera38e4082013-08-28 10:17:58 +1000212 enum lru_status ret;
Dave Chinner5cedf7212013-08-28 10:18:01 +1000213
214 /*
215 * decrement nr_to_walk first so that we don't livelock if we
216 * get stuck on large numbesr of LRU_RETRY items
217 */
Russell Kingc56b0972013-10-30 14:16:16 +0000218 if (!*nr_to_walk)
Dave Chinner5cedf7212013-08-28 10:18:01 +1000219 break;
Russell Kingc56b0972013-10-30 14:16:16 +0000220 --*nr_to_walk;
Dave Chinner5cedf7212013-08-28 10:18:01 +1000221
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800222 ret = isolate(item, l, &nlru->lock, cb_arg);
Dave Chinnera38e4082013-08-28 10:17:58 +1000223 switch (ret) {
Johannes Weiner449dd692014-04-03 14:47:56 -0700224 case LRU_REMOVED_RETRY:
225 assert_spin_locked(&nlru->lock);
Gustavo A. R. Silva5b568ac2017-11-15 17:38:49 -0800226 /* fall through */
Dave Chinnera38e4082013-08-28 10:17:58 +1000227 case LRU_REMOVED:
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000228 isolated++;
Sahitya Tummala2c80cd52017-07-10 15:49:57 -0700229 nlru->nr_items--;
Johannes Weiner449dd692014-04-03 14:47:56 -0700230 /*
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 Chinnera38e4082013-08-28 10:17:58 +1000237 break;
238 case LRU_ROTATE:
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800239 list_move_tail(item, &l->list);
Dave Chinnera38e4082013-08-28 10:17:58 +1000240 break;
241 case LRU_SKIP:
242 break;
243 case LRU_RETRY:
Dave Chinner5cedf7212013-08-28 10:18:01 +1000244 /*
245 * The lru lock has been dropped, our list traversal is
246 * now invalid and so we have to restart from scratch.
247 */
Johannes Weiner449dd692014-04-03 14:47:56 -0700248 assert_spin_locked(&nlru->lock);
Dave Chinnera38e4082013-08-28 10:17:58 +1000249 goto restart;
250 default:
251 BUG();
252 }
Dave Chinnera38e4082013-08-28 10:17:58 +1000253 }
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000254
255 spin_unlock(&nlru->lock);
256 return isolated;
257}
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800258
259unsigned long
260list_lru_walk_one(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
261 list_lru_walk_cb isolate, void *cb_arg,
262 unsigned long *nr_to_walk)
263{
264 return __list_lru_walk_one(lru, nid, memcg_cache_id(memcg),
265 isolate, cb_arg, nr_to_walk);
266}
267EXPORT_SYMBOL_GPL(list_lru_walk_one);
268
269unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
270 list_lru_walk_cb isolate, void *cb_arg,
271 unsigned long *nr_to_walk)
272{
273 long isolated = 0;
274 int memcg_idx;
275
276 isolated += __list_lru_walk_one(lru, nid, -1, isolate, cb_arg,
277 nr_to_walk);
278 if (*nr_to_walk > 0 && list_lru_memcg_aware(lru)) {
279 for_each_memcg_cache_index(memcg_idx) {
280 isolated += __list_lru_walk_one(lru, nid, memcg_idx,
281 isolate, cb_arg, nr_to_walk);
282 if (*nr_to_walk <= 0)
283 break;
284 }
285 }
286 return isolated;
287}
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000288EXPORT_SYMBOL_GPL(list_lru_walk_node);
289
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800290static void init_one_lru(struct list_lru_one *l)
291{
292 INIT_LIST_HEAD(&l->list);
293 l->nr_items = 0;
294}
295
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700296#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800297static void __memcg_destroy_list_lru_node(struct list_lru_memcg *memcg_lrus,
298 int begin, int end)
299{
300 int i;
301
302 for (i = begin; i < end; i++)
303 kfree(memcg_lrus->lru[i]);
304}
305
306static int __memcg_init_list_lru_node(struct list_lru_memcg *memcg_lrus,
307 int begin, int end)
308{
309 int i;
310
311 for (i = begin; i < end; i++) {
312 struct list_lru_one *l;
313
314 l = kmalloc(sizeof(struct list_lru_one), GFP_KERNEL);
315 if (!l)
316 goto fail;
317
318 init_one_lru(l);
319 memcg_lrus->lru[i] = l;
320 }
321 return 0;
322fail:
323 __memcg_destroy_list_lru_node(memcg_lrus, begin, i - 1);
324 return -ENOMEM;
325}
326
327static int memcg_init_list_lru_node(struct list_lru_node *nlru)
328{
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700329 struct list_lru_memcg *memcg_lrus;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800330 int size = memcg_nr_cache_ids;
331
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700332 memcg_lrus = kvmalloc(sizeof(*memcg_lrus) +
333 size * sizeof(void *), GFP_KERNEL);
334 if (!memcg_lrus)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800335 return -ENOMEM;
336
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700337 if (__memcg_init_list_lru_node(memcg_lrus, 0, size)) {
338 kvfree(memcg_lrus);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800339 return -ENOMEM;
340 }
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700341 RCU_INIT_POINTER(nlru->memcg_lrus, memcg_lrus);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800342
343 return 0;
344}
345
346static void memcg_destroy_list_lru_node(struct list_lru_node *nlru)
347{
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700348 struct list_lru_memcg *memcg_lrus;
349 /*
350 * This is called when shrinker has already been unregistered,
351 * and nobody can use it. So, there is no need to use kvfree_rcu().
352 */
353 memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus, true);
354 __memcg_destroy_list_lru_node(memcg_lrus, 0, memcg_nr_cache_ids);
355 kvfree(memcg_lrus);
356}
357
358static void kvfree_rcu(struct rcu_head *head)
359{
360 struct list_lru_memcg *mlru;
361
362 mlru = container_of(head, struct list_lru_memcg, rcu);
363 kvfree(mlru);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800364}
365
366static int memcg_update_list_lru_node(struct list_lru_node *nlru,
367 int old_size, int new_size)
368{
369 struct list_lru_memcg *old, *new;
370
371 BUG_ON(old_size > new_size);
372
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700373 old = rcu_dereference_protected(nlru->memcg_lrus,
374 lockdep_is_held(&list_lrus_mutex));
375 new = kvmalloc(sizeof(*new) + new_size * sizeof(void *), GFP_KERNEL);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800376 if (!new)
377 return -ENOMEM;
378
379 if (__memcg_init_list_lru_node(new, old_size, new_size)) {
Johannes Weinerf80c7da2017-10-03 16:16:10 -0700380 kvfree(new);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800381 return -ENOMEM;
382 }
383
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700384 memcpy(&new->lru, &old->lru, old_size * sizeof(void *));
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800385
386 /*
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700387 * The locking below allows readers that hold nlru->lock avoid taking
388 * rcu_read_lock (see list_lru_from_memcg_idx).
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800389 *
390 * Since list_lru_{add,del} may be called under an IRQ-safe lock,
391 * we have to use IRQ-safe primitives here to avoid deadlock.
392 */
393 spin_lock_irq(&nlru->lock);
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700394 rcu_assign_pointer(nlru->memcg_lrus, new);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800395 spin_unlock_irq(&nlru->lock);
396
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700397 call_rcu(&old->rcu, kvfree_rcu);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800398 return 0;
399}
400
401static void memcg_cancel_update_list_lru_node(struct list_lru_node *nlru,
402 int old_size, int new_size)
403{
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700404 struct list_lru_memcg *memcg_lrus;
405
406 memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus,
407 lockdep_is_held(&list_lrus_mutex));
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800408 /* do not bother shrinking the array back to the old size, because we
409 * cannot handle allocation failures here */
Kirill Tkhai0c7c1be2018-04-05 16:25:08 -0700410 __memcg_destroy_list_lru_node(memcg_lrus, old_size, new_size);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800411}
412
413static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)
414{
415 int i;
416
Raghavendra K T145949a2015-11-05 18:46:26 -0800417 if (!memcg_aware)
418 return 0;
419
420 for_each_node(i) {
421 if (memcg_init_list_lru_node(&lru->node[i]))
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800422 goto fail;
423 }
424 return 0;
425fail:
Raghavendra K T145949a2015-11-05 18:46:26 -0800426 for (i = i - 1; i >= 0; i--) {
427 if (!lru->node[i].memcg_lrus)
428 continue;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800429 memcg_destroy_list_lru_node(&lru->node[i]);
Raghavendra K T145949a2015-11-05 18:46:26 -0800430 }
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800431 return -ENOMEM;
432}
433
434static void memcg_destroy_list_lru(struct list_lru *lru)
435{
436 int i;
437
438 if (!list_lru_memcg_aware(lru))
439 return;
440
Raghavendra K T145949a2015-11-05 18:46:26 -0800441 for_each_node(i)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800442 memcg_destroy_list_lru_node(&lru->node[i]);
443}
444
445static int memcg_update_list_lru(struct list_lru *lru,
446 int old_size, int new_size)
447{
448 int i;
449
450 if (!list_lru_memcg_aware(lru))
451 return 0;
452
Raghavendra K T145949a2015-11-05 18:46:26 -0800453 for_each_node(i) {
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800454 if (memcg_update_list_lru_node(&lru->node[i],
455 old_size, new_size))
456 goto fail;
457 }
458 return 0;
459fail:
Raghavendra K T145949a2015-11-05 18:46:26 -0800460 for (i = i - 1; i >= 0; i--) {
461 if (!lru->node[i].memcg_lrus)
462 continue;
463
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800464 memcg_cancel_update_list_lru_node(&lru->node[i],
465 old_size, new_size);
Raghavendra K T145949a2015-11-05 18:46:26 -0800466 }
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800467 return -ENOMEM;
468}
469
470static void memcg_cancel_update_list_lru(struct list_lru *lru,
471 int old_size, int new_size)
472{
473 int i;
474
475 if (!list_lru_memcg_aware(lru))
476 return;
477
Raghavendra K T145949a2015-11-05 18:46:26 -0800478 for_each_node(i)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800479 memcg_cancel_update_list_lru_node(&lru->node[i],
480 old_size, new_size);
481}
482
483int memcg_update_all_list_lrus(int new_size)
484{
485 int ret = 0;
486 struct list_lru *lru;
487 int old_size = memcg_nr_cache_ids;
488
489 mutex_lock(&list_lrus_mutex);
490 list_for_each_entry(lru, &list_lrus, list) {
491 ret = memcg_update_list_lru(lru, old_size, new_size);
492 if (ret)
493 goto fail;
494 }
495out:
496 mutex_unlock(&list_lrus_mutex);
497 return ret;
498fail:
499 list_for_each_entry_continue_reverse(lru, &list_lrus, list)
500 memcg_cancel_update_list_lru(lru, old_size, new_size);
501 goto out;
502}
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800503
504static void memcg_drain_list_lru_node(struct list_lru_node *nlru,
Kirill Tkhai9bec5c32018-08-17 15:47:58 -0700505 int src_idx, struct mem_cgroup *dst_memcg)
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800506{
Kirill Tkhai9bec5c32018-08-17 15:47:58 -0700507 int dst_idx = dst_memcg->kmemcg_id;
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800508 struct list_lru_one *src, *dst;
509
510 /*
511 * Since list_lru_{add,del} may be called under an IRQ-safe lock,
512 * we have to use IRQ-safe primitives here to avoid deadlock.
513 */
514 spin_lock_irq(&nlru->lock);
515
516 src = list_lru_from_memcg_idx(nlru, src_idx);
517 dst = list_lru_from_memcg_idx(nlru, dst_idx);
518
519 list_splice_init(&src->list, &dst->list);
520 dst->nr_items += src->nr_items;
521 src->nr_items = 0;
522
523 spin_unlock_irq(&nlru->lock);
524}
525
526static void memcg_drain_list_lru(struct list_lru *lru,
Kirill Tkhai9bec5c32018-08-17 15:47:58 -0700527 int src_idx, struct mem_cgroup *dst_memcg)
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800528{
529 int i;
530
531 if (!list_lru_memcg_aware(lru))
532 return;
533
Raghavendra K T145949a2015-11-05 18:46:26 -0800534 for_each_node(i)
Kirill Tkhai9bec5c32018-08-17 15:47:58 -0700535 memcg_drain_list_lru_node(&lru->node[i], src_idx, dst_memcg);
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800536}
537
Kirill Tkhai9bec5c32018-08-17 15:47:58 -0700538void memcg_drain_all_list_lrus(int src_idx, struct mem_cgroup *dst_memcg)
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800539{
540 struct list_lru *lru;
541
542 mutex_lock(&list_lrus_mutex);
543 list_for_each_entry(lru, &list_lrus, list)
Kirill Tkhai9bec5c32018-08-17 15:47:58 -0700544 memcg_drain_list_lru(lru, src_idx, dst_memcg);
Vladimir Davydov2788cf02015-02-12 14:59:38 -0800545 mutex_unlock(&list_lrus_mutex);
546}
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800547#else
548static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)
549{
550 return 0;
551}
552
553static void memcg_destroy_list_lru(struct list_lru *lru)
554{
555}
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700556#endif /* CONFIG_MEMCG_KMEM */
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800557
558int __list_lru_init(struct list_lru *lru, bool memcg_aware,
Kirill Tkhaic92e8e12018-08-17 15:47:50 -0700559 struct lock_class_key *key, struct shrinker *shrinker)
Dave Chinnera38e4082013-08-28 10:17:58 +1000560{
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000561 int i;
Glauber Costa5ca302c2013-08-28 10:18:18 +1000562 size_t size = sizeof(*lru->node) * nr_node_ids;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800563 int err = -ENOMEM;
564
Kirill Tkhaic92e8e12018-08-17 15:47:50 -0700565#ifdef CONFIG_MEMCG_KMEM
566 if (shrinker)
567 lru->shrinker_id = shrinker->id;
568 else
569 lru->shrinker_id = -1;
570#endif
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800571 memcg_get_cache_ids();
Glauber Costa5ca302c2013-08-28 10:18:18 +1000572
573 lru->node = kzalloc(size, GFP_KERNEL);
574 if (!lru->node)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800575 goto out;
Dave Chinnera38e4082013-08-28 10:17:58 +1000576
Raghavendra K T145949a2015-11-05 18:46:26 -0800577 for_each_node(i) {
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000578 spin_lock_init(&lru->node[i].lock);
Johannes Weiner449dd692014-04-03 14:47:56 -0700579 if (key)
580 lockdep_set_class(&lru->node[i].lock, key);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800581 init_one_lru(&lru->node[i].lru);
Dave Chinner3b1d58a2013-08-28 10:18:00 +1000582 }
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800583
584 err = memcg_init_list_lru(lru, memcg_aware);
585 if (err) {
586 kfree(lru->node);
Alexander Polakov1bc11d72016-10-27 17:46:27 -0700587 /* Do this so a list_lru_destroy() doesn't crash: */
588 lru->node = NULL;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800589 goto out;
590 }
591
Vladimir Davydovc0a5b562015-02-12 14:59:07 -0800592 list_lru_register(lru);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800593out:
594 memcg_put_cache_ids();
595 return err;
Dave Chinnera38e4082013-08-28 10:17:58 +1000596}
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800597EXPORT_SYMBOL_GPL(__list_lru_init);
Glauber Costa5ca302c2013-08-28 10:18:18 +1000598
599void list_lru_destroy(struct list_lru *lru)
600{
Vladimir Davydovc0a5b562015-02-12 14:59:07 -0800601 /* Already destroyed or not yet initialized? */
602 if (!lru->node)
603 return;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800604
605 memcg_get_cache_ids();
606
Vladimir Davydovc0a5b562015-02-12 14:59:07 -0800607 list_lru_unregister(lru);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800608
609 memcg_destroy_list_lru(lru);
Glauber Costa5ca302c2013-08-28 10:18:18 +1000610 kfree(lru->node);
Vladimir Davydovc0a5b562015-02-12 14:59:07 -0800611 lru->node = NULL;
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800612
Kirill Tkhaic92e8e12018-08-17 15:47:50 -0700613#ifdef CONFIG_MEMCG_KMEM
614 lru->shrinker_id = -1;
615#endif
Vladimir Davydov60d3fd32015-02-12 14:59:10 -0800616 memcg_put_cache_ids();
Glauber Costa5ca302c2013-08-28 10:18:18 +1000617}
618EXPORT_SYMBOL_GPL(list_lru_destroy);