xref: /linux-6.15/include/linux/list_lru.h (revision 2788cf0c)
1a38e4082SDave Chinner /*
2a38e4082SDave Chinner  * Copyright (c) 2013 Red Hat, Inc. and Parallels Inc. All rights reserved.
3a38e4082SDave Chinner  * Authors: David Chinner and Glauber Costa
4a38e4082SDave Chinner  *
5a38e4082SDave Chinner  * Generic LRU infrastructure
6a38e4082SDave Chinner  */
7a38e4082SDave Chinner #ifndef _LRU_LIST_H
8a38e4082SDave Chinner #define _LRU_LIST_H
9a38e4082SDave Chinner 
10a38e4082SDave Chinner #include <linux/list.h>
113b1d58a4SDave Chinner #include <linux/nodemask.h>
12503c358cSVladimir Davydov #include <linux/shrinker.h>
13a38e4082SDave Chinner 
1460d3fd32SVladimir Davydov struct mem_cgroup;
1560d3fd32SVladimir Davydov 
16a38e4082SDave Chinner /* list_lru_walk_cb has to always return one of those */
17a38e4082SDave Chinner enum lru_status {
18a38e4082SDave Chinner 	LRU_REMOVED,		/* item removed from list */
19449dd698SJohannes Weiner 	LRU_REMOVED_RETRY,	/* item removed, but lock has been
20449dd698SJohannes Weiner 				   dropped and reacquired */
21a38e4082SDave Chinner 	LRU_ROTATE,		/* item referenced, give another pass */
22a38e4082SDave Chinner 	LRU_SKIP,		/* item cannot be locked, skip */
23a38e4082SDave Chinner 	LRU_RETRY,		/* item not freeable. May drop the lock
24a38e4082SDave Chinner 				   internally, but has to return locked. */
25a38e4082SDave Chinner };
26a38e4082SDave Chinner 
2760d3fd32SVladimir Davydov struct list_lru_one {
28a38e4082SDave Chinner 	struct list_head	list;
29*2788cf0cSVladimir Davydov 	/* may become negative during memcg reparenting */
30a38e4082SDave Chinner 	long			nr_items;
3160d3fd32SVladimir Davydov };
3260d3fd32SVladimir Davydov 
3360d3fd32SVladimir Davydov struct list_lru_memcg {
3460d3fd32SVladimir Davydov 	/* array of per cgroup lists, indexed by memcg_cache_id */
3560d3fd32SVladimir Davydov 	struct list_lru_one	*lru[0];
3660d3fd32SVladimir Davydov };
3760d3fd32SVladimir Davydov 
3860d3fd32SVladimir Davydov struct list_lru_node {
3960d3fd32SVladimir Davydov 	/* protects all lists on the node, including per cgroup */
4060d3fd32SVladimir Davydov 	spinlock_t		lock;
4160d3fd32SVladimir Davydov 	/* global list, used for the root cgroup in cgroup aware lrus */
4260d3fd32SVladimir Davydov 	struct list_lru_one	lru;
4360d3fd32SVladimir Davydov #ifdef CONFIG_MEMCG_KMEM
4460d3fd32SVladimir Davydov 	/* for cgroup aware lrus points to per cgroup lists, otherwise NULL */
4560d3fd32SVladimir Davydov 	struct list_lru_memcg	*memcg_lrus;
4660d3fd32SVladimir Davydov #endif
473b1d58a4SDave Chinner } ____cacheline_aligned_in_smp;
483b1d58a4SDave Chinner 
493b1d58a4SDave Chinner struct list_lru {
505ca302c8SGlauber Costa 	struct list_lru_node	*node;
51c0a5b560SVladimir Davydov #ifdef CONFIG_MEMCG_KMEM
52c0a5b560SVladimir Davydov 	struct list_head	list;
53c0a5b560SVladimir Davydov #endif
54a38e4082SDave Chinner };
55a38e4082SDave Chinner 
565ca302c8SGlauber Costa void list_lru_destroy(struct list_lru *lru);
5760d3fd32SVladimir Davydov int __list_lru_init(struct list_lru *lru, bool memcg_aware,
5860d3fd32SVladimir Davydov 		    struct lock_class_key *key);
5960d3fd32SVladimir Davydov 
6060d3fd32SVladimir Davydov #define list_lru_init(lru)		__list_lru_init((lru), false, NULL)
6160d3fd32SVladimir Davydov #define list_lru_init_key(lru, key)	__list_lru_init((lru), false, (key))
6260d3fd32SVladimir Davydov #define list_lru_init_memcg(lru)	__list_lru_init((lru), true, NULL)
6360d3fd32SVladimir Davydov 
6460d3fd32SVladimir Davydov int memcg_update_all_list_lrus(int num_memcgs);
65*2788cf0cSVladimir Davydov void memcg_drain_all_list_lrus(int src_idx, int dst_idx);
66a38e4082SDave Chinner 
67a38e4082SDave Chinner /**
68a38e4082SDave Chinner  * list_lru_add: add an element to the lru list's tail
69a38e4082SDave Chinner  * @list_lru: the lru pointer
70a38e4082SDave Chinner  * @item: the item to be added.
71a38e4082SDave Chinner  *
72a38e4082SDave Chinner  * If the element is already part of a list, this function returns doing
73a38e4082SDave Chinner  * nothing. Therefore the caller does not need to keep state about whether or
74a38e4082SDave Chinner  * not the element already belongs in the list and is allowed to lazy update
75a38e4082SDave Chinner  * it. Note however that this is valid for *a* list, not *this* list. If
76a38e4082SDave Chinner  * the caller organize itself in a way that elements can be in more than
77a38e4082SDave Chinner  * one type of list, it is up to the caller to fully remove the item from
78a38e4082SDave Chinner  * the previous list (with list_lru_del() for instance) before moving it
79a38e4082SDave Chinner  * to @list_lru
80a38e4082SDave Chinner  *
81a38e4082SDave Chinner  * Return value: true if the list was updated, false otherwise
82a38e4082SDave Chinner  */
83a38e4082SDave Chinner bool list_lru_add(struct list_lru *lru, struct list_head *item);
84a38e4082SDave Chinner 
85a38e4082SDave Chinner /**
86a38e4082SDave Chinner  * list_lru_del: delete an element to the lru list
87a38e4082SDave Chinner  * @list_lru: the lru pointer
88a38e4082SDave Chinner  * @item: the item to be deleted.
89a38e4082SDave Chinner  *
90a38e4082SDave Chinner  * This function works analogously as list_lru_add in terms of list
91a38e4082SDave Chinner  * manipulation. The comments about an element already pertaining to
92a38e4082SDave Chinner  * a list are also valid for list_lru_del.
93a38e4082SDave Chinner  *
94a38e4082SDave Chinner  * Return value: true if the list was updated, false otherwise
95a38e4082SDave Chinner  */
96a38e4082SDave Chinner bool list_lru_del(struct list_lru *lru, struct list_head *item);
97a38e4082SDave Chinner 
98a38e4082SDave Chinner /**
9960d3fd32SVladimir Davydov  * list_lru_count_one: return the number of objects currently held by @lru
100a38e4082SDave Chinner  * @lru: the lru pointer.
1016a4f496fSGlauber Costa  * @nid: the node id to count from.
10260d3fd32SVladimir Davydov  * @memcg: the cgroup to count from.
103a38e4082SDave Chinner  *
104a38e4082SDave Chinner  * Always return a non-negative number, 0 for empty lists. There is no
105a38e4082SDave Chinner  * guarantee that the list is not updated while the count is being computed.
106a38e4082SDave Chinner  * Callers that want such a guarantee need to provide an outer lock.
107a38e4082SDave Chinner  */
10860d3fd32SVladimir Davydov unsigned long list_lru_count_one(struct list_lru *lru,
10960d3fd32SVladimir Davydov 				 int nid, struct mem_cgroup *memcg);
1106a4f496fSGlauber Costa unsigned long list_lru_count_node(struct list_lru *lru, int nid);
111503c358cSVladimir Davydov 
112503c358cSVladimir Davydov static inline unsigned long list_lru_shrink_count(struct list_lru *lru,
113503c358cSVladimir Davydov 						  struct shrink_control *sc)
114503c358cSVladimir Davydov {
11560d3fd32SVladimir Davydov 	return list_lru_count_one(lru, sc->nid, sc->memcg);
116503c358cSVladimir Davydov }
117503c358cSVladimir Davydov 
1186a4f496fSGlauber Costa static inline unsigned long list_lru_count(struct list_lru *lru)
1196a4f496fSGlauber Costa {
1206a4f496fSGlauber Costa 	long count = 0;
1216a4f496fSGlauber Costa 	int nid;
1226a4f496fSGlauber Costa 
123ff0b67efSVladimir Davydov 	for_each_node_state(nid, N_NORMAL_MEMORY)
1246a4f496fSGlauber Costa 		count += list_lru_count_node(lru, nid);
1256a4f496fSGlauber Costa 
1266a4f496fSGlauber Costa 	return count;
1276a4f496fSGlauber Costa }
128a38e4082SDave Chinner 
1293f97b163SVladimir Davydov void list_lru_isolate(struct list_lru_one *list, struct list_head *item);
1303f97b163SVladimir Davydov void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item,
1313f97b163SVladimir Davydov 			   struct list_head *head);
1323f97b163SVladimir Davydov 
1333f97b163SVladimir Davydov typedef enum lru_status (*list_lru_walk_cb)(struct list_head *item,
1343f97b163SVladimir Davydov 		struct list_lru_one *list, spinlock_t *lock, void *cb_arg);
1353f97b163SVladimir Davydov 
136a38e4082SDave Chinner /**
13760d3fd32SVladimir Davydov  * list_lru_walk_one: walk a list_lru, isolating and disposing freeable items.
138a38e4082SDave Chinner  * @lru: the lru pointer.
1396a4f496fSGlauber Costa  * @nid: the node id to scan from.
14060d3fd32SVladimir Davydov  * @memcg: the cgroup to scan from.
141a38e4082SDave Chinner  * @isolate: callback function that is resposible for deciding what to do with
142a38e4082SDave Chinner  *  the item currently being scanned
143a38e4082SDave Chinner  * @cb_arg: opaque type that will be passed to @isolate
144a38e4082SDave Chinner  * @nr_to_walk: how many items to scan.
145a38e4082SDave Chinner  *
146a38e4082SDave Chinner  * This function will scan all elements in a particular list_lru, calling the
147a38e4082SDave Chinner  * @isolate callback for each of those items, along with the current list
148a38e4082SDave Chinner  * spinlock and a caller-provided opaque. The @isolate callback can choose to
149a38e4082SDave Chinner  * drop the lock internally, but *must* return with the lock held. The callback
150a38e4082SDave Chinner  * will return an enum lru_status telling the list_lru infrastructure what to
151a38e4082SDave Chinner  * do with the object being scanned.
152a38e4082SDave Chinner  *
153a38e4082SDave Chinner  * Please note that nr_to_walk does not mean how many objects will be freed,
154a38e4082SDave Chinner  * just how many objects will be scanned.
155a38e4082SDave Chinner  *
156a38e4082SDave Chinner  * Return value: the number of objects effectively removed from the LRU.
157a38e4082SDave Chinner  */
15860d3fd32SVladimir Davydov unsigned long list_lru_walk_one(struct list_lru *lru,
15960d3fd32SVladimir Davydov 				int nid, struct mem_cgroup *memcg,
16060d3fd32SVladimir Davydov 				list_lru_walk_cb isolate, void *cb_arg,
16160d3fd32SVladimir Davydov 				unsigned long *nr_to_walk);
1626a4f496fSGlauber Costa unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
1636a4f496fSGlauber Costa 				 list_lru_walk_cb isolate, void *cb_arg,
1646a4f496fSGlauber Costa 				 unsigned long *nr_to_walk);
1656a4f496fSGlauber Costa 
1666a4f496fSGlauber Costa static inline unsigned long
167503c358cSVladimir Davydov list_lru_shrink_walk(struct list_lru *lru, struct shrink_control *sc,
168503c358cSVladimir Davydov 		     list_lru_walk_cb isolate, void *cb_arg)
169503c358cSVladimir Davydov {
17060d3fd32SVladimir Davydov 	return list_lru_walk_one(lru, sc->nid, sc->memcg, isolate, cb_arg,
171503c358cSVladimir Davydov 				 &sc->nr_to_scan);
172503c358cSVladimir Davydov }
173503c358cSVladimir Davydov 
174503c358cSVladimir Davydov static inline unsigned long
1756a4f496fSGlauber Costa list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate,
1766a4f496fSGlauber Costa 	      void *cb_arg, unsigned long nr_to_walk)
1776a4f496fSGlauber Costa {
1786a4f496fSGlauber Costa 	long isolated = 0;
1796a4f496fSGlauber Costa 	int nid;
1806a4f496fSGlauber Costa 
181ff0b67efSVladimir Davydov 	for_each_node_state(nid, N_NORMAL_MEMORY) {
1826a4f496fSGlauber Costa 		isolated += list_lru_walk_node(lru, nid, isolate,
1836a4f496fSGlauber Costa 					       cb_arg, &nr_to_walk);
1846a4f496fSGlauber Costa 		if (nr_to_walk <= 0)
1856a4f496fSGlauber Costa 			break;
1866a4f496fSGlauber Costa 	}
1876a4f496fSGlauber Costa 	return isolated;
1886a4f496fSGlauber Costa }
189a38e4082SDave Chinner #endif /* _LRU_LIST_H */
190