1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 2a38e4082SDave Chinner /* 3a38e4082SDave Chinner * Copyright (c) 2013 Red Hat, Inc. and Parallels Inc. All rights reserved. 4a38e4082SDave Chinner * Authors: David Chinner and Glauber Costa 5a38e4082SDave Chinner * 6a38e4082SDave Chinner * Generic LRU infrastructure 7a38e4082SDave Chinner */ 8a38e4082SDave Chinner #ifndef _LRU_LIST_H 9a38e4082SDave Chinner #define _LRU_LIST_H 10a38e4082SDave Chinner 11a38e4082SDave Chinner #include <linux/list.h> 123b1d58a4SDave Chinner #include <linux/nodemask.h> 13503c358cSVladimir Davydov #include <linux/shrinker.h> 14bbca91ccSMuchun Song #include <linux/xarray.h> 15a38e4082SDave Chinner 1660d3fd32SVladimir Davydov struct mem_cgroup; 1760d3fd32SVladimir Davydov 18a38e4082SDave Chinner /* list_lru_walk_cb has to always return one of those */ 19a38e4082SDave Chinner enum lru_status { 20a38e4082SDave Chinner LRU_REMOVED, /* item removed from list */ 21449dd698SJohannes Weiner LRU_REMOVED_RETRY, /* item removed, but lock has been 22449dd698SJohannes Weiner dropped and reacquired */ 23a38e4082SDave Chinner LRU_ROTATE, /* item referenced, give another pass */ 24a38e4082SDave Chinner LRU_SKIP, /* item cannot be locked, skip */ 25a38e4082SDave Chinner LRU_RETRY, /* item not freeable. May drop the lock 26a38e4082SDave Chinner internally, but has to return locked. */ 27a38e4082SDave Chinner }; 28a38e4082SDave Chinner 2960d3fd32SVladimir Davydov struct list_lru_one { 30a38e4082SDave Chinner struct list_head list; 312788cf0cSVladimir Davydov /* may become negative during memcg reparenting */ 32a38e4082SDave Chinner long nr_items; 3360d3fd32SVladimir Davydov }; 3460d3fd32SVladimir Davydov 35d7011070SMuchun Song struct list_lru_memcg { 365abc1e37SMuchun Song struct rcu_head rcu; 376a6b7b77SMuchun Song /* array of per cgroup per node lists, indexed by node id */ 385abc1e37SMuchun Song struct list_lru_one node[]; 396a6b7b77SMuchun Song }; 406a6b7b77SMuchun Song 4160d3fd32SVladimir Davydov struct list_lru_node { 4260d3fd32SVladimir Davydov /* protects all lists on the node, including per cgroup */ 4360d3fd32SVladimir Davydov spinlock_t lock; 4460d3fd32SVladimir Davydov /* global list, used for the root cgroup in cgroup aware lrus */ 4560d3fd32SVladimir Davydov struct list_lru_one lru; 462c80cd57SSahitya Tummala long nr_items; 473b1d58a4SDave Chinner } ____cacheline_aligned_in_smp; 483b1d58a4SDave Chinner 493b1d58a4SDave Chinner struct list_lru { 505ca302c8SGlauber Costa struct list_lru_node *node; 5184c07d11SKirill Tkhai #ifdef CONFIG_MEMCG_KMEM 52c0a5b560SVladimir Davydov struct list_head list; 53c92e8e10SKirill Tkhai int shrinker_id; 543e858996SJiri Slaby bool memcg_aware; 55bbca91ccSMuchun Song struct xarray xa; 56c0a5b560SVladimir Davydov #endif 57a38e4082SDave Chinner }; 58a38e4082SDave Chinner 595ca302c8SGlauber Costa void list_lru_destroy(struct list_lru *lru); 6060d3fd32SVladimir Davydov int __list_lru_init(struct list_lru *lru, bool memcg_aware, 61c92e8e10SKirill Tkhai struct lock_class_key *key, struct shrinker *shrinker); 6260d3fd32SVladimir Davydov 63c92e8e10SKirill Tkhai #define list_lru_init(lru) \ 64c92e8e10SKirill Tkhai __list_lru_init((lru), false, NULL, NULL) 65c92e8e10SKirill Tkhai #define list_lru_init_key(lru, key) \ 66c92e8e10SKirill Tkhai __list_lru_init((lru), false, (key), NULL) 67c92e8e10SKirill Tkhai #define list_lru_init_memcg(lru, shrinker) \ 68c92e8e10SKirill Tkhai __list_lru_init((lru), true, NULL, shrinker) 6960d3fd32SVladimir Davydov 7088f2ef73SMuchun Song int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru, 7188f2ef73SMuchun Song gfp_t gfp); 721f391eb2SMuchun Song void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *parent); 73a38e4082SDave Chinner 74a38e4082SDave Chinner /** 75a38e4082SDave Chinner * list_lru_add: add an element to the lru list's tail 767679e140SAndy Shevchenko * @lru: the lru pointer 77a38e4082SDave Chinner * @item: the item to be added. 78*0a97c01cSNhat Pham * @nid: the node id of the sublist to add the item to. 79*0a97c01cSNhat Pham * @memcg: the cgroup of the sublist to add the item to. 80a38e4082SDave Chinner * 81a38e4082SDave Chinner * If the element is already part of a list, this function returns doing 82a38e4082SDave Chinner * nothing. Therefore the caller does not need to keep state about whether or 83a38e4082SDave Chinner * not the element already belongs in the list and is allowed to lazy update 84a38e4082SDave Chinner * it. Note however that this is valid for *a* list, not *this* list. If 85a38e4082SDave Chinner * the caller organize itself in a way that elements can be in more than 86a38e4082SDave Chinner * one type of list, it is up to the caller to fully remove the item from 87a38e4082SDave Chinner * the previous list (with list_lru_del() for instance) before moving it 887679e140SAndy Shevchenko * to @lru. 89a38e4082SDave Chinner * 907679e140SAndy Shevchenko * Return: true if the list was updated, false otherwise 91a38e4082SDave Chinner */ 92*0a97c01cSNhat Pham bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid, 93*0a97c01cSNhat Pham struct mem_cgroup *memcg); 94a38e4082SDave Chinner 95a38e4082SDave Chinner /** 96*0a97c01cSNhat Pham * list_lru_add_obj: add an element to the lru list's tail 97*0a97c01cSNhat Pham * @lru: the lru pointer 98*0a97c01cSNhat Pham * @item: the item to be added. 99*0a97c01cSNhat Pham * 100*0a97c01cSNhat Pham * This function is similar to list_lru_add(), but the NUMA node and the 101*0a97c01cSNhat Pham * memcg of the sublist is determined by @item list_head. This assumption is 102*0a97c01cSNhat Pham * valid for slab objects LRU such as dentries, inodes, etc. 103*0a97c01cSNhat Pham * 104*0a97c01cSNhat Pham * Return value: true if the list was updated, false otherwise 105*0a97c01cSNhat Pham */ 106*0a97c01cSNhat Pham bool list_lru_add_obj(struct list_lru *lru, struct list_head *item); 107*0a97c01cSNhat Pham 108*0a97c01cSNhat Pham /** 109*0a97c01cSNhat Pham * list_lru_del: delete an element from the lru list 1107679e140SAndy Shevchenko * @lru: the lru pointer 111a38e4082SDave Chinner * @item: the item to be deleted. 112*0a97c01cSNhat Pham * @nid: the node id of the sublist to delete the item from. 113*0a97c01cSNhat Pham * @memcg: the cgroup of the sublist to delete the item from. 114a38e4082SDave Chinner * 1157679e140SAndy Shevchenko * This function works analogously as list_lru_add() in terms of list 116a38e4082SDave Chinner * manipulation. The comments about an element already pertaining to 1177679e140SAndy Shevchenko * a list are also valid for list_lru_del(). 118a38e4082SDave Chinner * 1197679e140SAndy Shevchenko * Return: true if the list was updated, false otherwise 120a38e4082SDave Chinner */ 121*0a97c01cSNhat Pham bool list_lru_del(struct list_lru *lru, struct list_head *item, int nid, 122*0a97c01cSNhat Pham struct mem_cgroup *memcg); 123*0a97c01cSNhat Pham 124*0a97c01cSNhat Pham /** 125*0a97c01cSNhat Pham * list_lru_del_obj: delete an element from the lru list 126*0a97c01cSNhat Pham * @lru: the lru pointer 127*0a97c01cSNhat Pham * @item: the item to be deleted. 128*0a97c01cSNhat Pham * 129*0a97c01cSNhat Pham * This function is similar to list_lru_del(), but the NUMA node and the 130*0a97c01cSNhat Pham * memcg of the sublist is determined by @item list_head. This assumption is 131*0a97c01cSNhat Pham * valid for slab objects LRU such as dentries, inodes, etc. 132*0a97c01cSNhat Pham * 133*0a97c01cSNhat Pham * Return value: true if the list was updated, false otherwise. 134*0a97c01cSNhat Pham */ 135*0a97c01cSNhat Pham bool list_lru_del_obj(struct list_lru *lru, struct list_head *item); 136a38e4082SDave Chinner 137a38e4082SDave Chinner /** 13860d3fd32SVladimir Davydov * list_lru_count_one: return the number of objects currently held by @lru 139a38e4082SDave Chinner * @lru: the lru pointer. 1406a4f496fSGlauber Costa * @nid: the node id to count from. 14160d3fd32SVladimir Davydov * @memcg: the cgroup to count from. 142a38e4082SDave Chinner * 1437679e140SAndy Shevchenko * There is no guarantee that the list is not updated while the count is being 1447679e140SAndy Shevchenko * computed. Callers that want such a guarantee need to provide an outer lock. 1457679e140SAndy Shevchenko * 1467679e140SAndy Shevchenko * Return: 0 for empty lists, otherwise the number of objects 1477679e140SAndy Shevchenko * currently held by @lru. 148a38e4082SDave Chinner */ 14960d3fd32SVladimir Davydov unsigned long list_lru_count_one(struct list_lru *lru, 15060d3fd32SVladimir Davydov int nid, struct mem_cgroup *memcg); 1516a4f496fSGlauber Costa unsigned long list_lru_count_node(struct list_lru *lru, int nid); 152503c358cSVladimir Davydov 153503c358cSVladimir Davydov static inline unsigned long list_lru_shrink_count(struct list_lru *lru, 154503c358cSVladimir Davydov struct shrink_control *sc) 155503c358cSVladimir Davydov { 15660d3fd32SVladimir Davydov return list_lru_count_one(lru, sc->nid, sc->memcg); 157503c358cSVladimir Davydov } 158503c358cSVladimir Davydov 1596a4f496fSGlauber Costa static inline unsigned long list_lru_count(struct list_lru *lru) 1606a4f496fSGlauber Costa { 1616a4f496fSGlauber Costa long count = 0; 1626a4f496fSGlauber Costa int nid; 1636a4f496fSGlauber Costa 164ff0b67efSVladimir Davydov for_each_node_state(nid, N_NORMAL_MEMORY) 1656a4f496fSGlauber Costa count += list_lru_count_node(lru, nid); 1666a4f496fSGlauber Costa 1676a4f496fSGlauber Costa return count; 1686a4f496fSGlauber Costa } 169a38e4082SDave Chinner 1703f97b163SVladimir Davydov void list_lru_isolate(struct list_lru_one *list, struct list_head *item); 1713f97b163SVladimir Davydov void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item, 1723f97b163SVladimir Davydov struct list_head *head); 173*0a97c01cSNhat Pham /** 174*0a97c01cSNhat Pham * list_lru_putback: undo list_lru_isolate 175*0a97c01cSNhat Pham * @lru: the lru pointer. 176*0a97c01cSNhat Pham * @item: the item to put back. 177*0a97c01cSNhat Pham * @nid: the node id of the sublist to put the item back to. 178*0a97c01cSNhat Pham * @memcg: the cgroup of the sublist to put the item back to. 179*0a97c01cSNhat Pham * 180*0a97c01cSNhat Pham * Put back an isolated item into its original LRU. Note that unlike 181*0a97c01cSNhat Pham * list_lru_add, this does not increment the node LRU count (as 182*0a97c01cSNhat Pham * list_lru_isolate does not originally decrement this count). 183*0a97c01cSNhat Pham * 184*0a97c01cSNhat Pham * Since we might have dropped the LRU lock in between, recompute list_lru_one 185*0a97c01cSNhat Pham * from the node's id and memcg. 186*0a97c01cSNhat Pham */ 187*0a97c01cSNhat Pham void list_lru_putback(struct list_lru *lru, struct list_head *item, int nid, 188*0a97c01cSNhat Pham struct mem_cgroup *memcg); 1893f97b163SVladimir Davydov 1903f97b163SVladimir Davydov typedef enum lru_status (*list_lru_walk_cb)(struct list_head *item, 1913f97b163SVladimir Davydov struct list_lru_one *list, spinlock_t *lock, void *cb_arg); 1923f97b163SVladimir Davydov 193a38e4082SDave Chinner /** 1947679e140SAndy Shevchenko * list_lru_walk_one: walk a @lru, isolating and disposing freeable items. 195a38e4082SDave Chinner * @lru: the lru pointer. 1966a4f496fSGlauber Costa * @nid: the node id to scan from. 19760d3fd32SVladimir Davydov * @memcg: the cgroup to scan from. 19806c88398SZhen Lei * @isolate: callback function that is responsible for deciding what to do with 199a38e4082SDave Chinner * the item currently being scanned 200a38e4082SDave Chinner * @cb_arg: opaque type that will be passed to @isolate 201a38e4082SDave Chinner * @nr_to_walk: how many items to scan. 202a38e4082SDave Chinner * 2037679e140SAndy Shevchenko * This function will scan all elements in a particular @lru, calling the 204a38e4082SDave Chinner * @isolate callback for each of those items, along with the current list 205a38e4082SDave Chinner * spinlock and a caller-provided opaque. The @isolate callback can choose to 206a38e4082SDave Chinner * drop the lock internally, but *must* return with the lock held. The callback 2077679e140SAndy Shevchenko * will return an enum lru_status telling the @lru infrastructure what to 208a38e4082SDave Chinner * do with the object being scanned. 209a38e4082SDave Chinner * 2107679e140SAndy Shevchenko * Please note that @nr_to_walk does not mean how many objects will be freed, 211a38e4082SDave Chinner * just how many objects will be scanned. 212a38e4082SDave Chinner * 2137679e140SAndy Shevchenko * Return: the number of objects effectively removed from the LRU. 214a38e4082SDave Chinner */ 21560d3fd32SVladimir Davydov unsigned long list_lru_walk_one(struct list_lru *lru, 21660d3fd32SVladimir Davydov int nid, struct mem_cgroup *memcg, 21760d3fd32SVladimir Davydov list_lru_walk_cb isolate, void *cb_arg, 21860d3fd32SVladimir Davydov unsigned long *nr_to_walk); 2196b51e881SSebastian Andrzej Siewior /** 2207679e140SAndy Shevchenko * list_lru_walk_one_irq: walk a @lru, isolating and disposing freeable items. 2216b51e881SSebastian Andrzej Siewior * @lru: the lru pointer. 2226b51e881SSebastian Andrzej Siewior * @nid: the node id to scan from. 2236b51e881SSebastian Andrzej Siewior * @memcg: the cgroup to scan from. 22406c88398SZhen Lei * @isolate: callback function that is responsible for deciding what to do with 2256b51e881SSebastian Andrzej Siewior * the item currently being scanned 2266b51e881SSebastian Andrzej Siewior * @cb_arg: opaque type that will be passed to @isolate 2276b51e881SSebastian Andrzej Siewior * @nr_to_walk: how many items to scan. 2286b51e881SSebastian Andrzej Siewior * 2297679e140SAndy Shevchenko * Same as list_lru_walk_one() except that the spinlock is acquired with 2306b51e881SSebastian Andrzej Siewior * spin_lock_irq(). 2316b51e881SSebastian Andrzej Siewior */ 2326b51e881SSebastian Andrzej Siewior unsigned long list_lru_walk_one_irq(struct list_lru *lru, 2336b51e881SSebastian Andrzej Siewior int nid, struct mem_cgroup *memcg, 2346b51e881SSebastian Andrzej Siewior list_lru_walk_cb isolate, void *cb_arg, 2356b51e881SSebastian Andrzej Siewior unsigned long *nr_to_walk); 2366a4f496fSGlauber Costa unsigned long list_lru_walk_node(struct list_lru *lru, int nid, 2376a4f496fSGlauber Costa list_lru_walk_cb isolate, void *cb_arg, 2386a4f496fSGlauber Costa unsigned long *nr_to_walk); 2396a4f496fSGlauber Costa 2406a4f496fSGlauber Costa static inline unsigned long 241503c358cSVladimir Davydov list_lru_shrink_walk(struct list_lru *lru, struct shrink_control *sc, 242503c358cSVladimir Davydov list_lru_walk_cb isolate, void *cb_arg) 243503c358cSVladimir Davydov { 24460d3fd32SVladimir Davydov return list_lru_walk_one(lru, sc->nid, sc->memcg, isolate, cb_arg, 245503c358cSVladimir Davydov &sc->nr_to_scan); 246503c358cSVladimir Davydov } 247503c358cSVladimir Davydov 248503c358cSVladimir Davydov static inline unsigned long 2496b51e881SSebastian Andrzej Siewior list_lru_shrink_walk_irq(struct list_lru *lru, struct shrink_control *sc, 2506b51e881SSebastian Andrzej Siewior list_lru_walk_cb isolate, void *cb_arg) 2516b51e881SSebastian Andrzej Siewior { 2526b51e881SSebastian Andrzej Siewior return list_lru_walk_one_irq(lru, sc->nid, sc->memcg, isolate, cb_arg, 2536b51e881SSebastian Andrzej Siewior &sc->nr_to_scan); 2546b51e881SSebastian Andrzej Siewior } 2556b51e881SSebastian Andrzej Siewior 2566b51e881SSebastian Andrzej Siewior static inline unsigned long 2576a4f496fSGlauber Costa list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate, 2586a4f496fSGlauber Costa void *cb_arg, unsigned long nr_to_walk) 2596a4f496fSGlauber Costa { 2606a4f496fSGlauber Costa long isolated = 0; 2616a4f496fSGlauber Costa int nid; 2626a4f496fSGlauber Costa 263ff0b67efSVladimir Davydov for_each_node_state(nid, N_NORMAL_MEMORY) { 2646a4f496fSGlauber Costa isolated += list_lru_walk_node(lru, nid, isolate, 2656a4f496fSGlauber Costa cb_arg, &nr_to_walk); 2666a4f496fSGlauber Costa if (nr_to_walk <= 0) 2676a4f496fSGlauber Costa break; 2686a4f496fSGlauber Costa } 2696a4f496fSGlauber Costa return isolated; 2706a4f496fSGlauber Costa } 271a38e4082SDave Chinner #endif /* _LRU_LIST_H */ 272