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>
19 static LIST_HEAD(memcg_list_lrus);
20 static DEFINE_MUTEX(list_lrus_mutex);
22 static inline bool list_lru_memcg_aware(struct list_lru *lru)
24 return lru->memcg_aware;
27 static void list_lru_register(struct list_lru *lru)
29 if (!list_lru_memcg_aware(lru))
32 mutex_lock(&list_lrus_mutex);
33 list_add(&lru->list, &memcg_list_lrus);
34 mutex_unlock(&list_lrus_mutex);
37 static void list_lru_unregister(struct list_lru *lru)
39 if (!list_lru_memcg_aware(lru))
42 mutex_lock(&list_lrus_mutex);
44 mutex_unlock(&list_lrus_mutex);
47 static int lru_shrinker_id(struct list_lru *lru)
49 return lru->shrinker_id;
52 static inline struct list_lru_one *
53 list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)
55 if (list_lru_memcg_aware(lru) && idx >= 0) {
56 struct list_lru_memcg *mlru = xa_load(&lru->xa, idx);
58 return mlru ? &mlru->node[nid] : NULL;
60 return &lru->node[nid].lru;
63 static void list_lru_register(struct list_lru *lru)
67 static void list_lru_unregister(struct list_lru *lru)
71 static int lru_shrinker_id(struct list_lru *lru)
76 static inline bool list_lru_memcg_aware(struct list_lru *lru)
81 static inline struct list_lru_one *
82 list_lru_from_memcg_idx(struct list_lru *lru, int nid, int idx)
84 return &lru->node[nid].lru;
86 #endif /* CONFIG_MEMCG */
88 /* The caller must ensure the memcg lifetime. */
89 bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid,
90 struct mem_cgroup *memcg)
92 struct list_lru_node *nlru = &lru->node[nid];
93 struct list_lru_one *l;
95 spin_lock(&nlru->lock);
96 if (list_empty(item)) {
97 l = list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(memcg));
98 list_add_tail(item, &l->list);
99 /* Set shrinker bit if the first element was added */
101 set_shrinker_bit(memcg, nid, lru_shrinker_id(lru));
103 spin_unlock(&nlru->lock);
106 spin_unlock(&nlru->lock);
109 EXPORT_SYMBOL_GPL(list_lru_add);
111 bool list_lru_add_obj(struct list_lru *lru, struct list_head *item)
114 int nid = page_to_nid(virt_to_page(item));
116 if (list_lru_memcg_aware(lru)) {
118 ret = list_lru_add(lru, item, nid, mem_cgroup_from_slab_obj(item));
121 ret = list_lru_add(lru, item, nid, NULL);
126 EXPORT_SYMBOL_GPL(list_lru_add_obj);
128 /* The caller must ensure the memcg lifetime. */
129 bool list_lru_del(struct list_lru *lru, struct list_head *item, int nid,
130 struct mem_cgroup *memcg)
132 struct list_lru_node *nlru = &lru->node[nid];
133 struct list_lru_one *l;
135 spin_lock(&nlru->lock);
136 if (!list_empty(item)) {
137 l = list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(memcg));
141 spin_unlock(&nlru->lock);
144 spin_unlock(&nlru->lock);
147 EXPORT_SYMBOL_GPL(list_lru_del);
149 bool list_lru_del_obj(struct list_lru *lru, struct list_head *item)
152 int nid = page_to_nid(virt_to_page(item));
154 if (list_lru_memcg_aware(lru)) {
156 ret = list_lru_del(lru, item, nid, mem_cgroup_from_slab_obj(item));
159 ret = list_lru_del(lru, item, nid, NULL);
164 EXPORT_SYMBOL_GPL(list_lru_del_obj);
166 void list_lru_isolate(struct list_lru_one *list, struct list_head *item)
171 EXPORT_SYMBOL_GPL(list_lru_isolate);
173 void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item,
174 struct list_head *head)
176 list_move(item, head);
179 EXPORT_SYMBOL_GPL(list_lru_isolate_move);
181 unsigned long list_lru_count_one(struct list_lru *lru,
182 int nid, struct mem_cgroup *memcg)
184 struct list_lru_one *l;
188 l = list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(memcg));
189 count = l ? READ_ONCE(l->nr_items) : 0;
192 if (unlikely(count < 0))
197 EXPORT_SYMBOL_GPL(list_lru_count_one);
199 unsigned long list_lru_count_node(struct list_lru *lru, int nid)
201 struct list_lru_node *nlru;
203 nlru = &lru->node[nid];
204 return nlru->nr_items;
206 EXPORT_SYMBOL_GPL(list_lru_count_node);
209 __list_lru_walk_one(struct list_lru *lru, int nid, int memcg_idx,
210 list_lru_walk_cb isolate, void *cb_arg,
211 unsigned long *nr_to_walk)
213 struct list_lru_node *nlru = &lru->node[nid];
214 struct list_lru_one *l;
215 struct list_head *item, *n;
216 unsigned long isolated = 0;
219 l = list_lru_from_memcg_idx(lru, nid, memcg_idx);
223 list_for_each_safe(item, n, &l->list) {
227 * decrement nr_to_walk first so that we don't livelock if we
228 * get stuck on large numbers of LRU_RETRY items
234 ret = isolate(item, l, &nlru->lock, cb_arg);
236 case LRU_REMOVED_RETRY:
237 assert_spin_locked(&nlru->lock);
243 * If the lru lock has been dropped, our list
244 * traversal is now invalid and so we have to
245 * restart from scratch.
247 if (ret == LRU_REMOVED_RETRY)
251 list_move_tail(item, &l->list);
257 * The lru lock has been dropped, our list traversal is
258 * now invalid and so we have to restart from scratch.
260 assert_spin_locked(&nlru->lock);
263 assert_spin_locked(&nlru->lock);
274 list_lru_walk_one(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(&nlru->lock);
282 ret = __list_lru_walk_one(lru, nid, memcg_kmem_id(memcg), isolate,
284 spin_unlock(&nlru->lock);
287 EXPORT_SYMBOL_GPL(list_lru_walk_one);
290 list_lru_walk_one_irq(struct list_lru *lru, int nid, struct mem_cgroup *memcg,
291 list_lru_walk_cb isolate, void *cb_arg,
292 unsigned long *nr_to_walk)
294 struct list_lru_node *nlru = &lru->node[nid];
297 spin_lock_irq(&nlru->lock);
298 ret = __list_lru_walk_one(lru, nid, memcg_kmem_id(memcg), isolate,
300 spin_unlock_irq(&nlru->lock);
304 unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
305 list_lru_walk_cb isolate, void *cb_arg,
306 unsigned long *nr_to_walk)
310 isolated += list_lru_walk_one(lru, nid, NULL, isolate, cb_arg,
314 if (*nr_to_walk > 0 && list_lru_memcg_aware(lru)) {
315 struct list_lru_memcg *mlru;
318 xa_for_each(&lru->xa, index, mlru) {
319 struct list_lru_node *nlru = &lru->node[nid];
321 spin_lock(&nlru->lock);
322 isolated += __list_lru_walk_one(lru, nid, index,
325 spin_unlock(&nlru->lock);
327 if (*nr_to_walk <= 0)
335 EXPORT_SYMBOL_GPL(list_lru_walk_node);
337 static void init_one_lru(struct list_lru_one *l)
339 INIT_LIST_HEAD(&l->list);
344 static struct list_lru_memcg *memcg_init_list_lru_one(gfp_t gfp)
347 struct list_lru_memcg *mlru;
349 mlru = kmalloc(struct_size(mlru, node, nr_node_ids), gfp);
354 init_one_lru(&mlru->node[nid]);
359 static void memcg_list_lru_free(struct list_lru *lru, int src_idx)
361 struct list_lru_memcg *mlru = xa_erase_irq(&lru->xa, src_idx);
364 * The __list_lru_walk_one() can walk the list of this node.
365 * We need kvfree_rcu() here. And the walking of the list
366 * is under lru->node[nid]->lock, which can serve as a RCU
367 * read-side critical section.
370 kvfree_rcu(mlru, rcu);
373 static inline void memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)
376 xa_init_flags(&lru->xa, XA_FLAGS_LOCK_IRQ);
377 lru->memcg_aware = memcg_aware;
380 static void memcg_destroy_list_lru(struct list_lru *lru)
382 XA_STATE(xas, &lru->xa, 0);
383 struct list_lru_memcg *mlru;
385 if (!list_lru_memcg_aware(lru))
389 xas_for_each(&xas, mlru, ULONG_MAX) {
391 xas_store(&xas, NULL);
393 xas_unlock_irq(&xas);
396 static void memcg_reparent_list_lru_node(struct list_lru *lru, int nid,
397 int src_idx, struct mem_cgroup *dst_memcg)
399 struct list_lru_node *nlru = &lru->node[nid];
400 int dst_idx = dst_memcg->kmemcg_id;
401 struct list_lru_one *src, *dst;
404 * Since list_lru_{add,del} may be called under an IRQ-safe lock,
405 * we have to use IRQ-safe primitives here to avoid deadlock.
407 spin_lock_irq(&nlru->lock);
409 src = list_lru_from_memcg_idx(lru, nid, src_idx);
412 dst = list_lru_from_memcg_idx(lru, nid, dst_idx);
414 list_splice_init(&src->list, &dst->list);
417 dst->nr_items += src->nr_items;
418 set_shrinker_bit(dst_memcg, nid, lru_shrinker_id(lru));
422 spin_unlock_irq(&nlru->lock);
425 static void memcg_reparent_list_lru(struct list_lru *lru,
426 int src_idx, struct mem_cgroup *dst_memcg)
431 memcg_reparent_list_lru_node(lru, i, src_idx, dst_memcg);
433 memcg_list_lru_free(lru, src_idx);
436 void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *parent)
438 struct cgroup_subsys_state *css;
439 struct list_lru *lru;
440 int src_idx = memcg->kmemcg_id;
443 * Change kmemcg_id of this cgroup and all its descendants to the
444 * parent's id, and then move all entries from this cgroup's list_lrus
445 * to ones of the parent.
447 * After we have finished, all list_lrus corresponding to this cgroup
448 * are guaranteed to remain empty. So we can safely free this cgroup's
449 * list lrus in memcg_list_lru_free().
451 * Changing ->kmemcg_id to the parent can prevent memcg_list_lru_alloc()
452 * from allocating list lrus for this cgroup after memcg_list_lru_free()
456 css_for_each_descendant_pre(css, &memcg->css) {
457 struct mem_cgroup *child;
459 child = mem_cgroup_from_css(css);
460 WRITE_ONCE(child->kmemcg_id, parent->kmemcg_id);
464 mutex_lock(&list_lrus_mutex);
465 list_for_each_entry(lru, &memcg_list_lrus, list)
466 memcg_reparent_list_lru(lru, src_idx, parent);
467 mutex_unlock(&list_lrus_mutex);
470 static inline bool memcg_list_lru_allocated(struct mem_cgroup *memcg,
471 struct list_lru *lru)
473 int idx = memcg->kmemcg_id;
475 return idx < 0 || xa_load(&lru->xa, idx);
478 int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru,
483 struct list_lru_memcg_table {
484 struct list_lru_memcg *mlru;
485 struct mem_cgroup *memcg;
487 XA_STATE(xas, &lru->xa, 0);
489 if (!list_lru_memcg_aware(lru) || memcg_list_lru_allocated(memcg, lru))
492 gfp &= GFP_RECLAIM_MASK;
493 table = kmalloc_array(memcg->css.cgroup->level, sizeof(*table), gfp);
498 * Because the list_lru can be reparented to the parent cgroup's
499 * list_lru, we should make sure that this cgroup and all its
500 * ancestors have allocated list_lru_memcg.
502 for (i = 0; memcg; memcg = parent_mem_cgroup(memcg), i++) {
503 if (memcg_list_lru_allocated(memcg, lru))
506 table[i].memcg = memcg;
507 table[i].mlru = memcg_init_list_lru_one(gfp);
508 if (!table[i].mlru) {
510 kfree(table[i].mlru);
516 xas_lock_irqsave(&xas, flags);
518 int index = READ_ONCE(table[i].memcg->kmemcg_id);
519 struct list_lru_memcg *mlru = table[i].mlru;
521 xas_set(&xas, index);
523 if (unlikely(index < 0 || xas_error(&xas) || xas_load(&xas))) {
526 xas_store(&xas, mlru);
527 if (xas_error(&xas) == -ENOMEM) {
528 xas_unlock_irqrestore(&xas, flags);
529 if (xas_nomem(&xas, gfp))
530 xas_set_err(&xas, 0);
531 xas_lock_irqsave(&xas, flags);
533 * The xas lock has been released, this memcg
534 * can be reparented before us. So reload
535 * memcg id. More details see the comments
536 * in memcg_reparent_list_lrus().
538 index = READ_ONCE(table[i].memcg->kmemcg_id);
540 xas_set_err(&xas, 0);
541 else if (!xas_error(&xas) && index != xas.xa_index)
542 xas_set(&xas, index);
547 /* xas_nomem() is used to free memory instead of memory allocation. */
549 xas_nomem(&xas, gfp);
550 xas_unlock_irqrestore(&xas, flags);
553 return xas_error(&xas);
556 static inline void memcg_init_list_lru(struct list_lru *lru, bool memcg_aware)
560 static void memcg_destroy_list_lru(struct list_lru *lru)
563 #endif /* CONFIG_MEMCG */
565 int __list_lru_init(struct list_lru *lru, bool memcg_aware,
566 struct lock_class_key *key, struct shrinker *shrinker)
572 lru->shrinker_id = shrinker->id;
574 lru->shrinker_id = -1;
576 if (mem_cgroup_kmem_disabled())
580 lru->node = kcalloc(nr_node_ids, sizeof(*lru->node), GFP_KERNEL);
585 spin_lock_init(&lru->node[i].lock);
587 lockdep_set_class(&lru->node[i].lock, key);
588 init_one_lru(&lru->node[i].lru);
591 memcg_init_list_lru(lru, memcg_aware);
592 list_lru_register(lru);
596 EXPORT_SYMBOL_GPL(__list_lru_init);
598 void list_lru_destroy(struct list_lru *lru)
600 /* Already destroyed or not yet initialized? */
604 list_lru_unregister(lru);
606 memcg_destroy_list_lru(lru);
611 lru->shrinker_id = -1;
614 EXPORT_SYMBOL_GPL(list_lru_destroy);