// SPDX-License-Identifier: GPL-2.0-only /* * Copyright (c) 2013 Red Hat, Inc. and Parallels Inc. All rights reserved. * Authors: David Chinner and Glauber Costa * * Generic LRU infrastructure
*/ #include <linux/kernel.h> #include <linux/module.h> #include <linux/mm.h> #include <linux/list_lru.h> #include <linux/slab.h> #include <linux/mutex.h> #include <linux/memcontrol.h> #include"slab.h" #include"internal.h"
rcu_read_lock();
again:
l = list_lru_from_memcg_idx(lru, nid, memcg_kmem_id(memcg)); if (likely(l) && lock_list_lru(l, irq)) {
rcu_read_unlock(); return l;
} /* * Caller may simply bail out if raced with reparenting or * may iterate through the list_lru and expect empty slots.
*/ if (skip_empty) {
rcu_read_unlock(); return NULL;
}
VM_WARN_ON(!css_is_dying(&memcg->css));
memcg = parent_mem_cgroup(memcg); goto again;
}
/* The caller must ensure the memcg lifetime. */ bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid, struct mem_cgroup *memcg)
{ struct list_lru_node *nlru = &lru->node[nid]; struct list_lru_one *l;
l = lock_list_lru_of_memcg(lru, nid, memcg, false, false); if (!l) returnfalse; if (list_empty(item)) {
list_add_tail(item, &l->list); /* Set shrinker bit if the first element was added */ if (!l->nr_items++)
set_shrinker_bit(memcg, nid, lru_shrinker_id(lru));
unlock_list_lru(l, false);
atomic_long_inc(&nlru->nr_items); returntrue;
}
unlock_list_lru(l, false); returnfalse;
}
bool list_lru_add_obj(struct list_lru *lru, struct list_head *item)
{ bool ret; int nid = page_to_nid(virt_to_page(item));
if (list_lru_memcg_aware(lru)) {
rcu_read_lock();
ret = list_lru_add(lru, item, nid, mem_cgroup_from_slab_obj(item));
rcu_read_unlock();
} else {
ret = list_lru_add(lru, item, nid, NULL);
}
restart:
l = lock_list_lru_of_memcg(lru, nid, memcg, irq_off, true); if (!l) return isolated;
list_for_each_safe(item, n, &l->list) { enum lru_status ret;
/* * decrement nr_to_walk first so that we don't livelock if we * get stuck on large numbers of LRU_RETRY items
*/ if (!*nr_to_walk) break;
--*nr_to_walk;
ret = isolate(item, l, cb_arg); switch (ret) { /* * LRU_RETRY, LRU_REMOVED_RETRY and LRU_STOP will drop the lru * lock. List traversal will have to restart from scratch.
*/ case LRU_RETRY: goto restart; case LRU_REMOVED_RETRY:
fallthrough; case LRU_REMOVED:
isolated++;
atomic_long_dec(&nlru->nr_items); if (ret == LRU_REMOVED_RETRY) goto restart; break; case LRU_ROTATE:
list_move_tail(item, &l->list); break; case LRU_SKIP: break; case LRU_STOP: goto out; default:
BUG();
}
}
unlock_list_lru(l, irq_off);
out: return isolated;
}
/* * Lock the Xarray to ensure no on going list_lru_memcg * allocation and further allocation will see css_is_dying().
*/
xas_lock_irq(&xas);
mlru = xas_store(&xas, NULL);
xas_unlock_irq(&xas); if (!mlru) continue;
/* * With Xarray value set to NULL, holding the lru lock below * prevents list_lru_{add,del,isolate} from touching the lru, * safe to reparent.
*/
for_each_node(i)
memcg_reparent_list_lru_one(lru, i, &mlru->node[i], parent);
/* * Here all list_lrus corresponding to the cgroup are guaranteed * to remain empty, we can safely free this lru, any further * memcg_list_lru_alloc() call will simply bail out.
*/
kvfree_rcu(mlru, rcu);
}
mutex_unlock(&list_lrus_mutex);
}
if (!list_lru_memcg_aware(lru) || memcg_list_lru_allocated(memcg, lru)) return 0;
gfp &= GFP_RECLAIM_MASK; /* * Because the list_lru can be reparented to the parent cgroup's * list_lru, we should make sure that this cgroup and all its * ancestors have allocated list_lru_memcg.
*/ do { /* * Keep finding the farest parent that wasn't populated * until found memcg itself.
*/
pos = memcg;
parent = parent_mem_cgroup(pos); while (!memcg_list_lru_allocated(parent, lru)) {
pos = parent;
parent = parent_mem_cgroup(pos);
}
if (!mlru) {
mlru = memcg_init_list_lru_one(lru, gfp); if (!mlru) return -ENOMEM;
}
xas_set(&xas, pos->kmemcg_id); do {
xas_lock_irqsave(&xas, flags); if (!xas_load(&xas) && !css_is_dying(&pos->css)) {
xas_store(&xas, mlru); if (!xas_error(&xas))
mlru = NULL;
}
xas_unlock_irqrestore(&xas, flags);
} while (xas_nomem(&xas, gfp));
} while (pos != memcg && !css_is_dying(&pos->css));
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.