xref: /linux-6.15/include/linux/list_lru.h (revision da80f4ff)
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. */
27b49547adSChengming Zhou 	LRU_STOP,		/* stop lru list walking. May drop the lock
28b49547adSChengming Zhou 				   internally, but has to return locked. */
29a38e4082SDave Chinner };
30a38e4082SDave Chinner 
3160d3fd32SVladimir Davydov struct list_lru_one {
32a38e4082SDave Chinner 	struct list_head	list;
332788cf0cSVladimir Davydov 	/* may become negative during memcg reparenting */
34a38e4082SDave Chinner 	long			nr_items;
35fb56fdf8SKairui Song 	/* protects all fields above */
36fb56fdf8SKairui Song 	spinlock_t		lock;
3760d3fd32SVladimir Davydov };
3860d3fd32SVladimir Davydov 
39d7011070SMuchun Song struct list_lru_memcg {
405abc1e37SMuchun Song 	struct rcu_head		rcu;
416a6b7b77SMuchun Song 	/* array of per cgroup per node lists, indexed by node id */
425abc1e37SMuchun Song 	struct list_lru_one	node[];
436a6b7b77SMuchun Song };
446a6b7b77SMuchun Song 
4560d3fd32SVladimir Davydov struct list_lru_node {
4660d3fd32SVladimir Davydov 	/* global list, used for the root cgroup in cgroup aware lrus */
4760d3fd32SVladimir Davydov 	struct list_lru_one	lru;
48fb56fdf8SKairui Song 	atomic_long_t		nr_items;
493b1d58a4SDave Chinner } ____cacheline_aligned_in_smp;
503b1d58a4SDave Chinner 
513b1d58a4SDave Chinner struct list_lru {
525ca302c8SGlauber Costa 	struct list_lru_node	*node;
533a3b7fecSJohannes Weiner #ifdef CONFIG_MEMCG
54c0a5b560SVladimir Davydov 	struct list_head	list;
55c92e8e10SKirill Tkhai 	int			shrinker_id;
563e858996SJiri Slaby 	bool			memcg_aware;
57bbca91ccSMuchun Song 	struct xarray		xa;
58c0a5b560SVladimir Davydov #endif
593f28bbe5SKairui Song #ifdef CONFIG_LOCKDEP
603f28bbe5SKairui Song 	struct lock_class_key	*key;
613f28bbe5SKairui Song #endif
62a38e4082SDave Chinner };
63a38e4082SDave Chinner 
645ca302c8SGlauber Costa void list_lru_destroy(struct list_lru *lru);
6560d3fd32SVladimir Davydov int __list_lru_init(struct list_lru *lru, bool memcg_aware,
663f28bbe5SKairui Song 		    struct shrinker *shrinker);
6760d3fd32SVladimir Davydov 
68c92e8e10SKirill Tkhai #define list_lru_init(lru)				\
693f28bbe5SKairui Song 	__list_lru_init((lru), false, NULL)
70c92e8e10SKirill Tkhai #define list_lru_init_memcg(lru, shrinker)		\
713f28bbe5SKairui Song 	__list_lru_init((lru), true, shrinker)
723f28bbe5SKairui Song 
list_lru_init_memcg_key(struct list_lru * lru,struct shrinker * shrinker,struct lock_class_key * key)733f28bbe5SKairui Song static inline int list_lru_init_memcg_key(struct list_lru *lru, struct shrinker *shrinker,
743f28bbe5SKairui Song 					  struct lock_class_key *key)
753f28bbe5SKairui Song {
763f28bbe5SKairui Song #ifdef CONFIG_LOCKDEP
773f28bbe5SKairui Song 	lru->key = key;
783f28bbe5SKairui Song #endif
793f28bbe5SKairui Song 	return list_lru_init_memcg(lru, shrinker);
803f28bbe5SKairui Song }
8160d3fd32SVladimir Davydov 
8288f2ef73SMuchun Song int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru,
8388f2ef73SMuchun Song 			 gfp_t gfp);
841f391eb2SMuchun Song void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *parent);
85a38e4082SDave Chinner 
86a38e4082SDave Chinner /**
87a38e4082SDave Chinner  * list_lru_add: add an element to the lru list's tail
887679e140SAndy Shevchenko  * @lru: the lru pointer
89a38e4082SDave Chinner  * @item: the item to be added.
900a97c01cSNhat Pham  * @nid: the node id of the sublist to add the item to.
910a97c01cSNhat Pham  * @memcg: the cgroup of the sublist to add the item to.
92a38e4082SDave Chinner  *
93a38e4082SDave Chinner  * If the element is already part of a list, this function returns doing
94*da80f4ffSAlice Ryhl  * nothing. This means that it is not necessary to keep state about whether or
95*da80f4ffSAlice Ryhl  * not the element already belongs in the list. That said, this logic only
96*da80f4ffSAlice Ryhl  * works if the item is in *this* list. If the item might be in some other
97*da80f4ffSAlice Ryhl  * list, then you cannot rely on this check and you must remove it from the
98*da80f4ffSAlice Ryhl  * other list before trying to insert it.
99*da80f4ffSAlice Ryhl  *
100*da80f4ffSAlice Ryhl  * The lru list consists of many sublists internally; the @nid and @memcg
101*da80f4ffSAlice Ryhl  * parameters are used to determine which sublist to insert the item into.
102*da80f4ffSAlice Ryhl  * It's important to use the right value of @nid and @memcg when deleting the
103*da80f4ffSAlice Ryhl  * item, since it might otherwise get deleted from the wrong sublist.
104*da80f4ffSAlice Ryhl  *
105*da80f4ffSAlice Ryhl  * This also applies when attempting to insert the item multiple times - if
106*da80f4ffSAlice Ryhl  * the item is currently in one sublist and you call list_lru_add() again, you
107*da80f4ffSAlice Ryhl  * must pass the right @nid and @memcg parameters so that the same sublist is
108*da80f4ffSAlice Ryhl  * used.
109*da80f4ffSAlice Ryhl  *
110*da80f4ffSAlice Ryhl  * You must ensure that the memcg is not freed during this call (e.g., with
111*da80f4ffSAlice Ryhl  * rcu or by taking a css refcnt).
112a38e4082SDave Chinner  *
1137679e140SAndy Shevchenko  * Return: true if the list was updated, false otherwise
114a38e4082SDave Chinner  */
1150a97c01cSNhat Pham bool list_lru_add(struct list_lru *lru, struct list_head *item, int nid,
1160a97c01cSNhat Pham 		    struct mem_cgroup *memcg);
117a38e4082SDave Chinner 
118a38e4082SDave Chinner /**
1190a97c01cSNhat Pham  * list_lru_add_obj: add an element to the lru list's tail
1200a97c01cSNhat Pham  * @lru: the lru pointer
1210a97c01cSNhat Pham  * @item: the item to be added.
1220a97c01cSNhat Pham  *
1230a97c01cSNhat Pham  * This function is similar to list_lru_add(), but the NUMA node and the
1240a97c01cSNhat Pham  * memcg of the sublist is determined by @item list_head. This assumption is
1250a97c01cSNhat Pham  * valid for slab objects LRU such as dentries, inodes, etc.
1260a97c01cSNhat Pham  *
127*da80f4ffSAlice Ryhl  * Return: true if the list was updated, false otherwise
1280a97c01cSNhat Pham  */
1290a97c01cSNhat Pham bool list_lru_add_obj(struct list_lru *lru, struct list_head *item);
1300a97c01cSNhat Pham 
1310a97c01cSNhat Pham /**
1320a97c01cSNhat Pham  * list_lru_del: delete an element from the lru list
1337679e140SAndy Shevchenko  * @lru: the lru pointer
134a38e4082SDave Chinner  * @item: the item to be deleted.
1350a97c01cSNhat Pham  * @nid: the node id of the sublist to delete the item from.
1360a97c01cSNhat Pham  * @memcg: the cgroup of the sublist to delete the item from.
137a38e4082SDave Chinner  *
1387679e140SAndy Shevchenko  * This function works analogously as list_lru_add() in terms of list
139*da80f4ffSAlice Ryhl  * manipulation.
140*da80f4ffSAlice Ryhl  *
141*da80f4ffSAlice Ryhl  * The comments in list_lru_add() about an element already being in a list are
142*da80f4ffSAlice Ryhl  * also valid for list_lru_del(), that is, you can delete an item that has
143*da80f4ffSAlice Ryhl  * already been removed or never been added. However, if the item is in a
144*da80f4ffSAlice Ryhl  * list, it must be in *this* list, and you must pass the right value of @nid
145*da80f4ffSAlice Ryhl  * and @memcg so that the right sublist is used.
146*da80f4ffSAlice Ryhl  *
147*da80f4ffSAlice Ryhl  * You must ensure that the memcg is not freed during this call (e.g., with
148*da80f4ffSAlice Ryhl  * rcu or by taking a css refcnt). When a memcg is deleted, list_lru entries
149*da80f4ffSAlice Ryhl  * are automatically moved to the parent memcg. This is done in a race-free
150*da80f4ffSAlice Ryhl  * way, so during deletion of an memcg both the old and new memcg will resolve
151*da80f4ffSAlice Ryhl  * to the same sublist internally.
152a38e4082SDave Chinner  *
1537679e140SAndy Shevchenko  * Return: true if the list was updated, false otherwise
154a38e4082SDave Chinner  */
1550a97c01cSNhat Pham bool list_lru_del(struct list_lru *lru, struct list_head *item, int nid,
1560a97c01cSNhat Pham 		    struct mem_cgroup *memcg);
1570a97c01cSNhat Pham 
1580a97c01cSNhat Pham /**
1590a97c01cSNhat Pham  * list_lru_del_obj: delete an element from the lru list
1600a97c01cSNhat Pham  * @lru: the lru pointer
1610a97c01cSNhat Pham  * @item: the item to be deleted.
1620a97c01cSNhat Pham  *
1630a97c01cSNhat Pham  * This function is similar to list_lru_del(), but the NUMA node and the
1640a97c01cSNhat Pham  * memcg of the sublist is determined by @item list_head. This assumption is
1650a97c01cSNhat Pham  * valid for slab objects LRU such as dentries, inodes, etc.
1660a97c01cSNhat Pham  *
167*da80f4ffSAlice Ryhl  * Return: true if the list was updated, false otherwise.
1680a97c01cSNhat Pham  */
1690a97c01cSNhat Pham bool list_lru_del_obj(struct list_lru *lru, struct list_head *item);
170a38e4082SDave Chinner 
171a38e4082SDave Chinner /**
17260d3fd32SVladimir Davydov  * list_lru_count_one: return the number of objects currently held by @lru
173a38e4082SDave Chinner  * @lru: the lru pointer.
1746a4f496fSGlauber Costa  * @nid: the node id to count from.
17560d3fd32SVladimir Davydov  * @memcg: the cgroup to count from.
176a38e4082SDave Chinner  *
1777679e140SAndy Shevchenko  * There is no guarantee that the list is not updated while the count is being
1787679e140SAndy Shevchenko  * computed. Callers that want such a guarantee need to provide an outer lock.
1797679e140SAndy Shevchenko  *
1807679e140SAndy Shevchenko  * Return: 0 for empty lists, otherwise the number of objects
1817679e140SAndy Shevchenko  * currently held by @lru.
182a38e4082SDave Chinner  */
18360d3fd32SVladimir Davydov unsigned long list_lru_count_one(struct list_lru *lru,
18460d3fd32SVladimir Davydov 				 int nid, struct mem_cgroup *memcg);
1856a4f496fSGlauber Costa unsigned long list_lru_count_node(struct list_lru *lru, int nid);
186503c358cSVladimir Davydov 
list_lru_shrink_count(struct list_lru * lru,struct shrink_control * sc)187503c358cSVladimir Davydov static inline unsigned long list_lru_shrink_count(struct list_lru *lru,
188503c358cSVladimir Davydov 						  struct shrink_control *sc)
189503c358cSVladimir Davydov {
19060d3fd32SVladimir Davydov 	return list_lru_count_one(lru, sc->nid, sc->memcg);
191503c358cSVladimir Davydov }
192503c358cSVladimir Davydov 
list_lru_count(struct list_lru * lru)1936a4f496fSGlauber Costa static inline unsigned long list_lru_count(struct list_lru *lru)
1946a4f496fSGlauber Costa {
1956a4f496fSGlauber Costa 	long count = 0;
1966a4f496fSGlauber Costa 	int nid;
1976a4f496fSGlauber Costa 
198ff0b67efSVladimir Davydov 	for_each_node_state(nid, N_NORMAL_MEMORY)
1996a4f496fSGlauber Costa 		count += list_lru_count_node(lru, nid);
2006a4f496fSGlauber Costa 
2016a4f496fSGlauber Costa 	return count;
2026a4f496fSGlauber Costa }
203a38e4082SDave Chinner 
2043f97b163SVladimir Davydov void list_lru_isolate(struct list_lru_one *list, struct list_head *item);
2053f97b163SVladimir Davydov void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item,
2063f97b163SVladimir Davydov 			   struct list_head *head);
2073f97b163SVladimir Davydov 
2083f97b163SVladimir Davydov typedef enum lru_status (*list_lru_walk_cb)(struct list_head *item,
209da0c0251SKairui Song 		struct list_lru_one *list, void *cb_arg);
2103f97b163SVladimir Davydov 
211a38e4082SDave Chinner /**
2127679e140SAndy Shevchenko  * list_lru_walk_one: walk a @lru, isolating and disposing freeable items.
213a38e4082SDave Chinner  * @lru: the lru pointer.
2146a4f496fSGlauber Costa  * @nid: the node id to scan from.
21560d3fd32SVladimir Davydov  * @memcg: the cgroup to scan from.
21606c88398SZhen Lei  * @isolate: callback function that is responsible for deciding what to do with
217a38e4082SDave Chinner  *  the item currently being scanned
218a38e4082SDave Chinner  * @cb_arg: opaque type that will be passed to @isolate
219a38e4082SDave Chinner  * @nr_to_walk: how many items to scan.
220a38e4082SDave Chinner  *
2217679e140SAndy Shevchenko  * This function will scan all elements in a particular @lru, calling the
222a38e4082SDave Chinner  * @isolate callback for each of those items, along with the current list
223a38e4082SDave Chinner  * spinlock and a caller-provided opaque. The @isolate callback can choose to
224a38e4082SDave Chinner  * drop the lock internally, but *must* return with the lock held. The callback
2257679e140SAndy Shevchenko  * will return an enum lru_status telling the @lru infrastructure what to
226a38e4082SDave Chinner  * do with the object being scanned.
227a38e4082SDave Chinner  *
2287679e140SAndy Shevchenko  * Please note that @nr_to_walk does not mean how many objects will be freed,
229a38e4082SDave Chinner  * just how many objects will be scanned.
230a38e4082SDave Chinner  *
2317679e140SAndy Shevchenko  * Return: the number of objects effectively removed from the LRU.
232a38e4082SDave Chinner  */
23360d3fd32SVladimir Davydov unsigned long list_lru_walk_one(struct list_lru *lru,
23460d3fd32SVladimir Davydov 				int nid, struct mem_cgroup *memcg,
23560d3fd32SVladimir Davydov 				list_lru_walk_cb isolate, void *cb_arg,
23660d3fd32SVladimir Davydov 				unsigned long *nr_to_walk);
2376b51e881SSebastian Andrzej Siewior /**
2387679e140SAndy Shevchenko  * list_lru_walk_one_irq: walk a @lru, isolating and disposing freeable items.
2396b51e881SSebastian Andrzej Siewior  * @lru: the lru pointer.
2406b51e881SSebastian Andrzej Siewior  * @nid: the node id to scan from.
2416b51e881SSebastian Andrzej Siewior  * @memcg: the cgroup to scan from.
24206c88398SZhen Lei  * @isolate: callback function that is responsible for deciding what to do with
2436b51e881SSebastian Andrzej Siewior  *  the item currently being scanned
2446b51e881SSebastian Andrzej Siewior  * @cb_arg: opaque type that will be passed to @isolate
2456b51e881SSebastian Andrzej Siewior  * @nr_to_walk: how many items to scan.
2466b51e881SSebastian Andrzej Siewior  *
2477679e140SAndy Shevchenko  * Same as list_lru_walk_one() except that the spinlock is acquired with
2486b51e881SSebastian Andrzej Siewior  * spin_lock_irq().
2496b51e881SSebastian Andrzej Siewior  */
2506b51e881SSebastian Andrzej Siewior unsigned long list_lru_walk_one_irq(struct list_lru *lru,
2516b51e881SSebastian Andrzej Siewior 				    int nid, struct mem_cgroup *memcg,
2526b51e881SSebastian Andrzej Siewior 				    list_lru_walk_cb isolate, void *cb_arg,
2536b51e881SSebastian Andrzej Siewior 				    unsigned long *nr_to_walk);
2546a4f496fSGlauber Costa unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
2556a4f496fSGlauber Costa 				 list_lru_walk_cb isolate, void *cb_arg,
2566a4f496fSGlauber Costa 				 unsigned long *nr_to_walk);
2576a4f496fSGlauber Costa 
2586a4f496fSGlauber Costa static inline unsigned long
list_lru_shrink_walk(struct list_lru * lru,struct shrink_control * sc,list_lru_walk_cb isolate,void * cb_arg)259503c358cSVladimir Davydov list_lru_shrink_walk(struct list_lru *lru, struct shrink_control *sc,
260503c358cSVladimir Davydov 		     list_lru_walk_cb isolate, void *cb_arg)
261503c358cSVladimir Davydov {
26260d3fd32SVladimir Davydov 	return list_lru_walk_one(lru, sc->nid, sc->memcg, isolate, cb_arg,
263503c358cSVladimir Davydov 				 &sc->nr_to_scan);
264503c358cSVladimir Davydov }
265503c358cSVladimir Davydov 
266503c358cSVladimir Davydov static inline unsigned long
list_lru_shrink_walk_irq(struct list_lru * lru,struct shrink_control * sc,list_lru_walk_cb isolate,void * cb_arg)2676b51e881SSebastian Andrzej Siewior list_lru_shrink_walk_irq(struct list_lru *lru, struct shrink_control *sc,
2686b51e881SSebastian Andrzej Siewior 			 list_lru_walk_cb isolate, void *cb_arg)
2696b51e881SSebastian Andrzej Siewior {
2706b51e881SSebastian Andrzej Siewior 	return list_lru_walk_one_irq(lru, sc->nid, sc->memcg, isolate, cb_arg,
2716b51e881SSebastian Andrzej Siewior 				     &sc->nr_to_scan);
2726b51e881SSebastian Andrzej Siewior }
2736b51e881SSebastian Andrzej Siewior 
2746b51e881SSebastian Andrzej Siewior static inline unsigned long
list_lru_walk(struct list_lru * lru,list_lru_walk_cb isolate,void * cb_arg,unsigned long nr_to_walk)2756a4f496fSGlauber Costa list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate,
2766a4f496fSGlauber Costa 	      void *cb_arg, unsigned long nr_to_walk)
2776a4f496fSGlauber Costa {
2786a4f496fSGlauber Costa 	long isolated = 0;
2796a4f496fSGlauber Costa 	int nid;
2806a4f496fSGlauber Costa 
281ff0b67efSVladimir Davydov 	for_each_node_state(nid, N_NORMAL_MEMORY) {
2826a4f496fSGlauber Costa 		isolated += list_lru_walk_node(lru, nid, isolate,
2836a4f496fSGlauber Costa 					       cb_arg, &nr_to_walk);
2846a4f496fSGlauber Costa 		if (nr_to_walk <= 0)
2856a4f496fSGlauber Costa 			break;
2866a4f496fSGlauber Costa 	}
2876a4f496fSGlauber Costa 	return isolated;
2886a4f496fSGlauber Costa }
289a38e4082SDave Chinner #endif /* _LRU_LIST_H */
290