xref: /linux-6.15/include/linux/list_lru.h (revision 1f391eb2)
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>
14a38e4082SDave Chinner 
1560d3fd32SVladimir Davydov struct mem_cgroup;
1660d3fd32SVladimir Davydov 
17a38e4082SDave Chinner /* list_lru_walk_cb has to always return one of those */
18a38e4082SDave Chinner enum lru_status {
19a38e4082SDave Chinner 	LRU_REMOVED,		/* item removed from list */
20449dd698SJohannes Weiner 	LRU_REMOVED_RETRY,	/* item removed, but lock has been
21449dd698SJohannes Weiner 				   dropped and reacquired */
22a38e4082SDave Chinner 	LRU_ROTATE,		/* item referenced, give another pass */
23a38e4082SDave Chinner 	LRU_SKIP,		/* item cannot be locked, skip */
24a38e4082SDave Chinner 	LRU_RETRY,		/* item not freeable. May drop the lock
25a38e4082SDave Chinner 				   internally, but has to return locked. */
26a38e4082SDave Chinner };
27a38e4082SDave Chinner 
2860d3fd32SVladimir Davydov struct list_lru_one {
29a38e4082SDave Chinner 	struct list_head	list;
302788cf0cSVladimir Davydov 	/* may become negative during memcg reparenting */
31a38e4082SDave Chinner 	long			nr_items;
3260d3fd32SVladimir Davydov };
3360d3fd32SVladimir Davydov 
346a6b7b77SMuchun Song struct list_lru_per_memcg {
355abc1e37SMuchun Song 	struct rcu_head		rcu;
366a6b7b77SMuchun Song 	/* array of per cgroup per node lists, indexed by node id */
375abc1e37SMuchun Song 	struct list_lru_one	node[];
386a6b7b77SMuchun Song };
396a6b7b77SMuchun Song 
4060d3fd32SVladimir Davydov struct list_lru_memcg {
410c7c1bedSKirill Tkhai 	struct rcu_head			rcu;
4260d3fd32SVladimir Davydov 	/* array of per cgroup lists, indexed by memcg_cache_id */
435abc1e37SMuchun Song 	struct list_lru_per_memcg __rcu	*mlru[];
4460d3fd32SVladimir Davydov };
4560d3fd32SVladimir Davydov 
4660d3fd32SVladimir Davydov struct list_lru_node {
4760d3fd32SVladimir Davydov 	/* protects all lists on the node, including per cgroup */
4860d3fd32SVladimir Davydov 	spinlock_t		lock;
4960d3fd32SVladimir Davydov 	/* global list, used for the root cgroup in cgroup aware lrus */
5060d3fd32SVladimir Davydov 	struct list_lru_one	lru;
512c80cd57SSahitya Tummala 	long			nr_items;
523b1d58a4SDave Chinner } ____cacheline_aligned_in_smp;
533b1d58a4SDave Chinner 
543b1d58a4SDave Chinner struct list_lru {
555ca302c8SGlauber Costa 	struct list_lru_node	*node;
5684c07d11SKirill Tkhai #ifdef CONFIG_MEMCG_KMEM
57c0a5b560SVladimir Davydov 	struct list_head	list;
58c92e8e10SKirill Tkhai 	int			shrinker_id;
593e858996SJiri Slaby 	bool			memcg_aware;
6088f2ef73SMuchun Song 	/* protects ->mlrus->mlru[i] */
6188f2ef73SMuchun Song 	spinlock_t		lock;
626a6b7b77SMuchun Song 	/* for cgroup aware lrus points to per cgroup lists, otherwise NULL */
636a6b7b77SMuchun Song 	struct list_lru_memcg	__rcu *mlrus;
64c0a5b560SVladimir Davydov #endif
65a38e4082SDave Chinner };
66a38e4082SDave Chinner 
675ca302c8SGlauber Costa void list_lru_destroy(struct list_lru *lru);
6860d3fd32SVladimir Davydov int __list_lru_init(struct list_lru *lru, bool memcg_aware,
69c92e8e10SKirill Tkhai 		    struct lock_class_key *key, struct shrinker *shrinker);
7060d3fd32SVladimir Davydov 
71c92e8e10SKirill Tkhai #define list_lru_init(lru)				\
72c92e8e10SKirill Tkhai 	__list_lru_init((lru), false, NULL, NULL)
73c92e8e10SKirill Tkhai #define list_lru_init_key(lru, key)			\
74c92e8e10SKirill Tkhai 	__list_lru_init((lru), false, (key), NULL)
75c92e8e10SKirill Tkhai #define list_lru_init_memcg(lru, shrinker)		\
76c92e8e10SKirill Tkhai 	__list_lru_init((lru), true, NULL, shrinker)
7760d3fd32SVladimir Davydov 
7888f2ef73SMuchun Song int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru,
7988f2ef73SMuchun Song 			 gfp_t gfp);
8060d3fd32SVladimir Davydov int memcg_update_all_list_lrus(int num_memcgs);
81*1f391eb2SMuchun Song void memcg_reparent_list_lrus(struct mem_cgroup *memcg, struct mem_cgroup *parent);
82a38e4082SDave Chinner 
83a38e4082SDave Chinner /**
84a38e4082SDave Chinner  * list_lru_add: add an element to the lru list's tail
85a38e4082SDave Chinner  * @list_lru: the lru pointer
86a38e4082SDave Chinner  * @item: the item to be added.
87a38e4082SDave Chinner  *
88a38e4082SDave Chinner  * If the element is already part of a list, this function returns doing
89a38e4082SDave Chinner  * nothing. Therefore the caller does not need to keep state about whether or
90a38e4082SDave Chinner  * not the element already belongs in the list and is allowed to lazy update
91a38e4082SDave Chinner  * it. Note however that this is valid for *a* list, not *this* list. If
92a38e4082SDave Chinner  * the caller organize itself in a way that elements can be in more than
93a38e4082SDave Chinner  * one type of list, it is up to the caller to fully remove the item from
94a38e4082SDave Chinner  * the previous list (with list_lru_del() for instance) before moving it
95a38e4082SDave Chinner  * to @list_lru
96a38e4082SDave Chinner  *
97a38e4082SDave Chinner  * Return value: true if the list was updated, false otherwise
98a38e4082SDave Chinner  */
99a38e4082SDave Chinner bool list_lru_add(struct list_lru *lru, struct list_head *item);
100a38e4082SDave Chinner 
101a38e4082SDave Chinner /**
102a38e4082SDave Chinner  * list_lru_del: delete an element to the lru list
103a38e4082SDave Chinner  * @list_lru: the lru pointer
104a38e4082SDave Chinner  * @item: the item to be deleted.
105a38e4082SDave Chinner  *
106a38e4082SDave Chinner  * This function works analogously as list_lru_add in terms of list
107a38e4082SDave Chinner  * manipulation. The comments about an element already pertaining to
108a38e4082SDave Chinner  * a list are also valid for list_lru_del.
109a38e4082SDave Chinner  *
110a38e4082SDave Chinner  * Return value: true if the list was updated, false otherwise
111a38e4082SDave Chinner  */
112a38e4082SDave Chinner bool list_lru_del(struct list_lru *lru, struct list_head *item);
113a38e4082SDave Chinner 
114a38e4082SDave Chinner /**
11560d3fd32SVladimir Davydov  * list_lru_count_one: return the number of objects currently held by @lru
116a38e4082SDave Chinner  * @lru: the lru pointer.
1176a4f496fSGlauber Costa  * @nid: the node id to count from.
11860d3fd32SVladimir Davydov  * @memcg: the cgroup to count from.
119a38e4082SDave Chinner  *
120a38e4082SDave Chinner  * Always return a non-negative number, 0 for empty lists. There is no
121a38e4082SDave Chinner  * guarantee that the list is not updated while the count is being computed.
122a38e4082SDave Chinner  * Callers that want such a guarantee need to provide an outer lock.
123a38e4082SDave Chinner  */
12460d3fd32SVladimir Davydov unsigned long list_lru_count_one(struct list_lru *lru,
12560d3fd32SVladimir Davydov 				 int nid, struct mem_cgroup *memcg);
1266a4f496fSGlauber Costa unsigned long list_lru_count_node(struct list_lru *lru, int nid);
127503c358cSVladimir Davydov 
128503c358cSVladimir Davydov static inline unsigned long list_lru_shrink_count(struct list_lru *lru,
129503c358cSVladimir Davydov 						  struct shrink_control *sc)
130503c358cSVladimir Davydov {
13160d3fd32SVladimir Davydov 	return list_lru_count_one(lru, sc->nid, sc->memcg);
132503c358cSVladimir Davydov }
133503c358cSVladimir Davydov 
1346a4f496fSGlauber Costa static inline unsigned long list_lru_count(struct list_lru *lru)
1356a4f496fSGlauber Costa {
1366a4f496fSGlauber Costa 	long count = 0;
1376a4f496fSGlauber Costa 	int nid;
1386a4f496fSGlauber Costa 
139ff0b67efSVladimir Davydov 	for_each_node_state(nid, N_NORMAL_MEMORY)
1406a4f496fSGlauber Costa 		count += list_lru_count_node(lru, nid);
1416a4f496fSGlauber Costa 
1426a4f496fSGlauber Costa 	return count;
1436a4f496fSGlauber Costa }
144a38e4082SDave Chinner 
1453f97b163SVladimir Davydov void list_lru_isolate(struct list_lru_one *list, struct list_head *item);
1463f97b163SVladimir Davydov void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item,
1473f97b163SVladimir Davydov 			   struct list_head *head);
1483f97b163SVladimir Davydov 
1493f97b163SVladimir Davydov typedef enum lru_status (*list_lru_walk_cb)(struct list_head *item,
1503f97b163SVladimir Davydov 		struct list_lru_one *list, spinlock_t *lock, void *cb_arg);
1513f97b163SVladimir Davydov 
152a38e4082SDave Chinner /**
15360d3fd32SVladimir Davydov  * list_lru_walk_one: walk a list_lru, isolating and disposing freeable items.
154a38e4082SDave Chinner  * @lru: the lru pointer.
1556a4f496fSGlauber Costa  * @nid: the node id to scan from.
15660d3fd32SVladimir Davydov  * @memcg: the cgroup to scan from.
15706c88398SZhen Lei  * @isolate: callback function that is responsible for deciding what to do with
158a38e4082SDave Chinner  *  the item currently being scanned
159a38e4082SDave Chinner  * @cb_arg: opaque type that will be passed to @isolate
160a38e4082SDave Chinner  * @nr_to_walk: how many items to scan.
161a38e4082SDave Chinner  *
162a38e4082SDave Chinner  * This function will scan all elements in a particular list_lru, calling the
163a38e4082SDave Chinner  * @isolate callback for each of those items, along with the current list
164a38e4082SDave Chinner  * spinlock and a caller-provided opaque. The @isolate callback can choose to
165a38e4082SDave Chinner  * drop the lock internally, but *must* return with the lock held. The callback
166a38e4082SDave Chinner  * will return an enum lru_status telling the list_lru infrastructure what to
167a38e4082SDave Chinner  * do with the object being scanned.
168a38e4082SDave Chinner  *
169a38e4082SDave Chinner  * Please note that nr_to_walk does not mean how many objects will be freed,
170a38e4082SDave Chinner  * just how many objects will be scanned.
171a38e4082SDave Chinner  *
172a38e4082SDave Chinner  * Return value: the number of objects effectively removed from the LRU.
173a38e4082SDave Chinner  */
17460d3fd32SVladimir Davydov unsigned long list_lru_walk_one(struct list_lru *lru,
17560d3fd32SVladimir Davydov 				int nid, struct mem_cgroup *memcg,
17660d3fd32SVladimir Davydov 				list_lru_walk_cb isolate, void *cb_arg,
17760d3fd32SVladimir Davydov 				unsigned long *nr_to_walk);
1786b51e881SSebastian Andrzej Siewior /**
1796b51e881SSebastian Andrzej Siewior  * list_lru_walk_one_irq: walk a list_lru, isolating and disposing freeable items.
1806b51e881SSebastian Andrzej Siewior  * @lru: the lru pointer.
1816b51e881SSebastian Andrzej Siewior  * @nid: the node id to scan from.
1826b51e881SSebastian Andrzej Siewior  * @memcg: the cgroup to scan from.
18306c88398SZhen Lei  * @isolate: callback function that is responsible for deciding what to do with
1846b51e881SSebastian Andrzej Siewior  *  the item currently being scanned
1856b51e881SSebastian Andrzej Siewior  * @cb_arg: opaque type that will be passed to @isolate
1866b51e881SSebastian Andrzej Siewior  * @nr_to_walk: how many items to scan.
1876b51e881SSebastian Andrzej Siewior  *
1886b51e881SSebastian Andrzej Siewior  * Same as @list_lru_walk_one except that the spinlock is acquired with
1896b51e881SSebastian Andrzej Siewior  * spin_lock_irq().
1906b51e881SSebastian Andrzej Siewior  */
1916b51e881SSebastian Andrzej Siewior unsigned long list_lru_walk_one_irq(struct list_lru *lru,
1926b51e881SSebastian Andrzej Siewior 				    int nid, struct mem_cgroup *memcg,
1936b51e881SSebastian Andrzej Siewior 				    list_lru_walk_cb isolate, void *cb_arg,
1946b51e881SSebastian Andrzej Siewior 				    unsigned long *nr_to_walk);
1956a4f496fSGlauber Costa unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
1966a4f496fSGlauber Costa 				 list_lru_walk_cb isolate, void *cb_arg,
1976a4f496fSGlauber Costa 				 unsigned long *nr_to_walk);
1986a4f496fSGlauber Costa 
1996a4f496fSGlauber Costa static inline unsigned long
200503c358cSVladimir Davydov list_lru_shrink_walk(struct list_lru *lru, struct shrink_control *sc,
201503c358cSVladimir Davydov 		     list_lru_walk_cb isolate, void *cb_arg)
202503c358cSVladimir Davydov {
20360d3fd32SVladimir Davydov 	return list_lru_walk_one(lru, sc->nid, sc->memcg, isolate, cb_arg,
204503c358cSVladimir Davydov 				 &sc->nr_to_scan);
205503c358cSVladimir Davydov }
206503c358cSVladimir Davydov 
207503c358cSVladimir Davydov static inline unsigned long
2086b51e881SSebastian Andrzej Siewior list_lru_shrink_walk_irq(struct list_lru *lru, struct shrink_control *sc,
2096b51e881SSebastian Andrzej Siewior 			 list_lru_walk_cb isolate, void *cb_arg)
2106b51e881SSebastian Andrzej Siewior {
2116b51e881SSebastian Andrzej Siewior 	return list_lru_walk_one_irq(lru, sc->nid, sc->memcg, isolate, cb_arg,
2126b51e881SSebastian Andrzej Siewior 				     &sc->nr_to_scan);
2136b51e881SSebastian Andrzej Siewior }
2146b51e881SSebastian Andrzej Siewior 
2156b51e881SSebastian Andrzej Siewior static inline unsigned long
2166a4f496fSGlauber Costa list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate,
2176a4f496fSGlauber Costa 	      void *cb_arg, unsigned long nr_to_walk)
2186a4f496fSGlauber Costa {
2196a4f496fSGlauber Costa 	long isolated = 0;
2206a4f496fSGlauber Costa 	int nid;
2216a4f496fSGlauber Costa 
222ff0b67efSVladimir Davydov 	for_each_node_state(nid, N_NORMAL_MEMORY) {
2236a4f496fSGlauber Costa 		isolated += list_lru_walk_node(lru, nid, isolate,
2246a4f496fSGlauber Costa 					       cb_arg, &nr_to_walk);
2256a4f496fSGlauber Costa 		if (nr_to_walk <= 0)
2266a4f496fSGlauber Costa 			break;
2276a4f496fSGlauber Costa 	}
2286a4f496fSGlauber Costa 	return isolated;
2296a4f496fSGlauber Costa }
230a38e4082SDave Chinner #endif /* _LRU_LIST_H */
231