1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2013 Red Hat, Inc. and Parallels Inc. All rights reserved.
4 * Authors: David Chinner and Glauber Costa
6 * Generic LRU infrastructure
8 #include <linux/kernel.h>
9 #include <linux/module.h>
11 #include <linux/list_lru.h>
12 #include <linux/slab.h>
13 #include <linux/mutex.h>
14 #include <linux/memcontrol.h>
17 #ifdef CONFIG_MEMCG_KMEM
18 static LIST_HEAD(list_lrus);
19 static DEFINE_MUTEX(list_lrus_mutex);
21 static void list_lru_register(struct list_lru *lru)
23 mutex_lock(&list_lrus_mutex);
24 list_add(&lru->list, &list_lrus);
25 mutex_unlock(&list_lrus_mutex);
28 static void list_lru_unregister(struct list_lru *lru)
30 mutex_lock(&list_lrus_mutex);
32 mutex_unlock(&list_lrus_mutex);
35 static int lru_shrinker_id(struct list_lru *lru)
37 return lru->shrinker_id;
40 static inline bool list_lru_memcg_aware(struct list_lru *lru)
42 return lru->memcg_aware;
45 static inline struct list_lru_one *
46 list_lru_from_memcg_idx(struct list_lru_node *nlru, int idx)
48 struct list_lru_memcg *memcg_lrus;
50 * Either lock or RCU protects the array of per cgroup lists
51 * from relocation (see memcg_update_list_lru_node).
53 memcg_lrus = rcu_dereference_check(nlru->memcg_lrus,
54 lockdep_is_held(&nlru->lock));
55 if (memcg_lrus && idx >= 0)
56 return memcg_lrus->lru[idx];
60 static inline struct list_lru_one *
61 list_lru_from_kmem(struct list_lru_node *nlru, void *ptr,
62 struct mem_cgroup **memcg_ptr)
64 struct list_lru_one *l = &nlru->lru;
65 struct mem_cgroup *memcg = NULL;
67 if (!nlru->memcg_lrus)
70 memcg = mem_cgroup_from_obj(ptr);
74 l = list_lru_from_memcg_idx(nlru, memcg_cache_id(memcg));
81 static void list_lru_register(struct list_lru *lru)
85 static void list_lru_unregister(struct list_lru *lru)
89 static int lru_shrinker_id(struct list_lru *lru)
94 static inline bool list_lru_memcg_aware(struct list_lru *lru)
99 static inline struct list_lru_one *
100 list_lru_from_memcg_idx(struct list_lru_node *nlru, int idx)
105 static inline struct list_lru_one *
106 list_lru_from_kmem(struct list_lru_node *nlru, void *ptr,
107 struct mem_cgroup **memcg_ptr)
113 #endif /* CONFIG_MEMCG_KMEM */
115 bool list_lru_add(struct list_lru *lru, struct list_head *item)
117 int nid = page_to_nid(virt_to_page(item));
118 struct list_lru_node *nlru = &lru->node[nid];
119 struct mem_cgroup *memcg;
120 struct list_lru_one *l;
122 spin_lock(&nlru->lock);
123 if (list_empty(item)) {
124 l = list_lru_from_kmem(nlru, item, &memcg);
125 list_add_tail(item, &l->list);
126 /* Set shrinker bit if the first element was added */
128 memcg_set_shrinker_bit(memcg, nid,
129 lru_shrinker_id(lru));
131 spin_unlock(&nlru->lock);
134 spin_unlock(&nlru->lock);
137 EXPORT_SYMBOL_GPL(list_lru_add);
139 bool list_lru_del(struct list_lru *lru, struct list_head *item)
141 int nid = page_to_nid(virt_to_page(item));
142 struct list_lru_node *nlru = &lru->node[nid];
143 struct list_lru_one *l;
145 spin_lock(&nlru->lock);
146 if (!list_empty(item)) {
147 l = list_lru_from_kmem(nlru, item, NULL);
151 spin_unlock(&nlru->lock);
154 spin_unlock(&nlru->lock);
157 EXPORT_SYMBOL_GPL(list_lru_del);
159 void list_lru_isolate(struct list_lru_one *list, struct list_head *item)
164 EXPORT_SYMBOL_GPL(list_lru_isolate);
166 void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item,
167 struct list_head *head)
169 list_move(item, head);
172 EXPORT_SYMBOL_GPL(list_lru_isolate_move);
174 unsigned long list_lru_count_one(struct list_lru *lru,
175 int nid, struct mem_cgroup *memcg)
177 struct list_lru_node *nlru = &lru->node[nid];
178 struct list_lru_one *l;
182 l = list_lru_from_memcg_idx(nlru, memcg_cache_id(memcg));
183 count = READ_ONCE(l->nr_items);
188 EXPORT_SYMBOL_GPL(list_lru_count_one);
190 unsigned long list_lru_count_node(struct list_lru *lru, int nid)
192 struct list_lru_node *nlru;
194 nlru = &lru->node[nid];
195 return nlru->nr_items;
197 EXPORT_SYMBOL_GPL(list_lru_count_node);
200 __list_lru_walk_one(struct list_lru_node *nlru, int memcg_idx,
201 list_lru_walk_cb isolate, void *cb_arg,
202 unsigned long *nr_to_walk)
205 struct list_lru_one *l;
206 struct list_head *item, *n;
207 unsigned long isolated = 0;
209 l = list_lru_from_memcg_idx(nlru, memcg_idx);
211 list_for_each_safe(item, n, &l->list) {
215 * decrement nr_to_walk first so that we don't livelock if we
216 * get stuck on large numbers of LRU_RETRY items
222 ret = isolate(item, l, &nlru->lock, cb_arg);
224 case LRU_REMOVED_RETRY:
225 assert_spin_locked(&nlru->lock);
231 * If the lru lock has been dropped, our list
232 * traversal is now invalid and so we have to
233 * restart from scratch.
235 if (ret == LRU_REMOVED_RETRY)
239 list_move_tail(item, &l->list);
245 * The lru lock has been dropped, our list traversal is
246 * now invalid and so we have to restart from scratch.
248 assert_spin_locked(&nlru->lock);
258 list_lru_walk_one(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
259 list_lru_walk_cb isolate, void *cb_arg,
260 unsigned long *nr_to_walk)
262 struct list_lru_node *nlru = &lru->node[nid];
265 spin_lock(&nlru->lock);
266 ret = __list_lru_walk_one(nlru, memcg_cache_id(memcg), isolate, cb_arg,
268 spin_unlock(&nlru->lock);
271 EXPORT_SYMBOL_GPL(list_lru_walk_one);
274 list_lru_walk_one_irq(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
275 list_lru_walk_cb isolate, void *cb_arg,
276 unsigned long *nr_to_walk)
278 struct list_lru_node *nlru = &lru->node[nid];
281 spin_lock_irq(&nlru->lock);
282 ret = __list_lru_walk_one(nlru, memcg_cache_id(memcg), isolate, cb_arg,
284 spin_unlock_irq(&nlru->lock);
288 unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
289 list_lru_walk_cb isolate, void *cb_arg,
290 unsigned long *nr_to_walk)
295 isolated += list_lru_walk_one(lru, nid, NULL, isolate, cb_arg,
297 if (*nr_to_walk > 0 && list_lru_memcg_aware(lru)) {
298 for_each_memcg_cache_index(memcg_idx) {
299 struct list_lru_node *nlru = &lru->node[nid];
301 spin_lock(&nlru->lock);
302 isolated += __list_lru_walk_one(nlru, memcg_idx,
305 spin_unlock(&nlru->lock);
307 if (*nr_to_walk <= 0)
313 EXPORT_SYMBOL_GPL(list_lru_walk_node);
315 static void init_one_lru(struct list_lru_one *l)
317 INIT_LIST_HEAD(&l->list);
321 #ifdef CONFIG_MEMCG_KMEM
322 static void __memcg_destroy_list_lru_node(struct list_lru_memcg *memcg_lrus,
327 for (i = begin; i < end; i++)
328 kfree(memcg_lrus->lru[i]);
331 static int __memcg_init_list_lru_node(struct list_lru_memcg *memcg_lrus,
336 for (i = begin; i < end; i++) {
337 struct list_lru_one *l;
339 l = kmalloc(sizeof(struct list_lru_one), GFP_KERNEL);
344 memcg_lrus->lru[i] = l;
348 __memcg_destroy_list_lru_node(memcg_lrus, begin, i);
352 static int memcg_init_list_lru_node(struct list_lru_node *nlru)
354 struct list_lru_memcg *memcg_lrus;
355 int size = memcg_nr_cache_ids;
357 memcg_lrus = kvmalloc(sizeof(*memcg_lrus) +
358 size * sizeof(void *), GFP_KERNEL);
362 if (__memcg_init_list_lru_node(memcg_lrus, 0, size)) {
366 RCU_INIT_POINTER(nlru->memcg_lrus, memcg_lrus);
371 static void memcg_destroy_list_lru_node(struct list_lru_node *nlru)
373 struct list_lru_memcg *memcg_lrus;
375 * This is called when shrinker has already been unregistered,
376 * and nobody can use it. So, there is no need to use kvfree_rcu_local().
378 memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus, true);
379 __memcg_destroy_list_lru_node(memcg_lrus, 0, memcg_nr_cache_ids);
383 static void kvfree_rcu_local(struct rcu_head *head)
385 struct list_lru_memcg *mlru;
387 mlru = container_of(head, struct list_lru_memcg, rcu);
391 static int memcg_update_list_lru_node(struct list_lru_node *nlru,
392 int old_size, int new_size)
394 struct list_lru_memcg *old, *new;
396 BUG_ON(old_size > new_size);
398 old = rcu_dereference_protected(nlru->memcg_lrus,
399 lockdep_is_held(&list_lrus_mutex));
400 new = kvmalloc(sizeof(*new) + new_size * sizeof(void *), GFP_KERNEL);
404 if (__memcg_init_list_lru_node(new, old_size, new_size)) {
409 memcpy(&new->lru, &old->lru, old_size * sizeof(void *));
412 * The locking below allows readers that hold nlru->lock avoid taking
413 * rcu_read_lock (see list_lru_from_memcg_idx).
415 * Since list_lru_{add,del} may be called under an IRQ-safe lock,
416 * we have to use IRQ-safe primitives here to avoid deadlock.
418 spin_lock_irq(&nlru->lock);
419 rcu_assign_pointer(nlru->memcg_lrus, new);
420 spin_unlock_irq(&nlru->lock);
422 call_rcu(&old->rcu, kvfree_rcu_local);
426 static void memcg_cancel_update_list_lru_node(struct list_lru_node *nlru,
427 int old_size, int new_size)
429 struct list_lru_memcg *memcg_lrus;
431 memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus,
432 lockdep_is_held(&list_lrus_mutex));
433 /* do not bother shrinking the array back to the old size, because we
434 * cannot handle allocation failures here */
435 __memcg_destroy_list_lru_node(memcg_lrus, old_size, new_size);
438 static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)
442 lru->memcg_aware = memcg_aware;
448 if (memcg_init_list_lru_node(&lru->node[i]))
453 for (i = i - 1; i >= 0; i--) {
454 if (!lru->node[i].memcg_lrus)
456 memcg_destroy_list_lru_node(&lru->node[i]);
461 static void memcg_destroy_list_lru(struct list_lru *lru)
465 if (!list_lru_memcg_aware(lru))
469 memcg_destroy_list_lru_node(&lru->node[i]);
472 static int memcg_update_list_lru(struct list_lru *lru,
473 int old_size, int new_size)
477 if (!list_lru_memcg_aware(lru))
481 if (memcg_update_list_lru_node(&lru->node[i],
487 for (i = i - 1; i >= 0; i--) {
488 if (!lru->node[i].memcg_lrus)
491 memcg_cancel_update_list_lru_node(&lru->node[i],
497 static void memcg_cancel_update_list_lru(struct list_lru *lru,
498 int old_size, int new_size)
502 if (!list_lru_memcg_aware(lru))
506 memcg_cancel_update_list_lru_node(&lru->node[i],
510 int memcg_update_all_list_lrus(int new_size)
513 struct list_lru *lru;
514 int old_size = memcg_nr_cache_ids;
516 mutex_lock(&list_lrus_mutex);
517 list_for_each_entry(lru, &list_lrus, list) {
518 ret = memcg_update_list_lru(lru, old_size, new_size);
523 mutex_unlock(&list_lrus_mutex);
526 list_for_each_entry_continue_reverse(lru, &list_lrus, list)
527 memcg_cancel_update_list_lru(lru, old_size, new_size);
531 static void memcg_drain_list_lru_node(struct list_lru *lru, int nid,
532 int src_idx, struct mem_cgroup *dst_memcg)
534 struct list_lru_node *nlru = &lru->node[nid];
535 int dst_idx = dst_memcg->kmemcg_id;
536 struct list_lru_one *src, *dst;
539 * Since list_lru_{add,del} may be called under an IRQ-safe lock,
540 * we have to use IRQ-safe primitives here to avoid deadlock.
542 spin_lock_irq(&nlru->lock);
544 src = list_lru_from_memcg_idx(nlru, src_idx);
545 dst = list_lru_from_memcg_idx(nlru, dst_idx);
547 list_splice_init(&src->list, &dst->list);
550 dst->nr_items += src->nr_items;
551 memcg_set_shrinker_bit(dst_memcg, nid, lru_shrinker_id(lru));
555 spin_unlock_irq(&nlru->lock);
558 static void memcg_drain_list_lru(struct list_lru *lru,
559 int src_idx, struct mem_cgroup *dst_memcg)
563 if (!list_lru_memcg_aware(lru))
567 memcg_drain_list_lru_node(lru, i, src_idx, dst_memcg);
570 void memcg_drain_all_list_lrus(int src_idx, struct mem_cgroup *dst_memcg)
572 struct list_lru *lru;
574 mutex_lock(&list_lrus_mutex);
575 list_for_each_entry(lru, &list_lrus, list)
576 memcg_drain_list_lru(lru, src_idx, dst_memcg);
577 mutex_unlock(&list_lrus_mutex);
580 static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)
585 static void memcg_destroy_list_lru(struct list_lru *lru)
588 #endif /* CONFIG_MEMCG_KMEM */
590 int __list_lru_init(struct list_lru *lru, bool memcg_aware,
591 struct lock_class_key *key, struct shrinker *shrinker)
596 #ifdef CONFIG_MEMCG_KMEM
598 lru->shrinker_id = shrinker->id;
600 lru->shrinker_id = -1;
602 memcg_get_cache_ids();
604 lru->node = kcalloc(nr_node_ids, sizeof(*lru->node), GFP_KERNEL);
609 spin_lock_init(&lru->node[i].lock);
611 lockdep_set_class(&lru->node[i].lock, key);
612 init_one_lru(&lru->node[i].lru);
615 err = memcg_init_list_lru(lru, memcg_aware);
618 /* Do this so a list_lru_destroy() doesn't crash: */
623 list_lru_register(lru);
625 memcg_put_cache_ids();
628 EXPORT_SYMBOL_GPL(__list_lru_init);
630 void list_lru_destroy(struct list_lru *lru)
632 /* Already destroyed or not yet initialized? */
636 memcg_get_cache_ids();
638 list_lru_unregister(lru);
640 memcg_destroy_list_lru(lru);
644 #ifdef CONFIG_MEMCG_KMEM
645 lru->shrinker_id = -1;
647 memcg_put_cache_ids();
649 EXPORT_SYMBOL_GPL(list_lru_destroy);