Commit | Line | Data |
---|---|---|
a38e4082 DC |
1 | /* |
2 | * Copyright (c) 2013 Red Hat, Inc. and Parallels Inc. All rights reserved. | |
3 | * Authors: David Chinner and Glauber Costa | |
4 | * | |
5 | * Generic LRU infrastructure | |
6 | */ | |
7 | #ifndef _LRU_LIST_H | |
8 | #define _LRU_LIST_H | |
9 | ||
10 | #include <linux/list.h> | |
3b1d58a4 | 11 | #include <linux/nodemask.h> |
503c358c | 12 | #include <linux/shrinker.h> |
a38e4082 | 13 | |
60d3fd32 VD |
14 | struct mem_cgroup; |
15 | ||
a38e4082 DC |
16 | /* list_lru_walk_cb has to always return one of those */ |
17 | enum lru_status { | |
18 | LRU_REMOVED, /* item removed from list */ | |
449dd698 JW |
19 | LRU_REMOVED_RETRY, /* item removed, but lock has been |
20 | dropped and reacquired */ | |
a38e4082 DC |
21 | LRU_ROTATE, /* item referenced, give another pass */ |
22 | LRU_SKIP, /* item cannot be locked, skip */ | |
23 | LRU_RETRY, /* item not freeable. May drop the lock | |
24 | internally, but has to return locked. */ | |
25 | }; | |
26 | ||
60d3fd32 | 27 | struct list_lru_one { |
a38e4082 DC |
28 | struct list_head list; |
29 | /* kept as signed so we can catch imbalance bugs */ | |
30 | long nr_items; | |
60d3fd32 VD |
31 | }; |
32 | ||
33 | struct list_lru_memcg { | |
34 | /* array of per cgroup lists, indexed by memcg_cache_id */ | |
35 | struct list_lru_one *lru[0]; | |
36 | }; | |
37 | ||
38 | struct list_lru_node { | |
39 | /* protects all lists on the node, including per cgroup */ | |
40 | spinlock_t lock; | |
41 | /* global list, used for the root cgroup in cgroup aware lrus */ | |
42 | struct list_lru_one lru; | |
43 | #ifdef CONFIG_MEMCG_KMEM | |
44 | /* for cgroup aware lrus points to per cgroup lists, otherwise NULL */ | |
45 | struct list_lru_memcg *memcg_lrus; | |
46 | #endif | |
3b1d58a4 DC |
47 | } ____cacheline_aligned_in_smp; |
48 | ||
49 | struct list_lru { | |
5ca302c8 | 50 | struct list_lru_node *node; |
c0a5b560 VD |
51 | #ifdef CONFIG_MEMCG_KMEM |
52 | struct list_head list; | |
53 | #endif | |
a38e4082 DC |
54 | }; |
55 | ||
5ca302c8 | 56 | void list_lru_destroy(struct list_lru *lru); |
60d3fd32 VD |
57 | int __list_lru_init(struct list_lru *lru, bool memcg_aware, |
58 | struct lock_class_key *key); | |
59 | ||
60 | #define list_lru_init(lru) __list_lru_init((lru), false, NULL) | |
61 | #define list_lru_init_key(lru, key) __list_lru_init((lru), false, (key)) | |
62 | #define list_lru_init_memcg(lru) __list_lru_init((lru), true, NULL) | |
63 | ||
64 | int memcg_update_all_list_lrus(int num_memcgs); | |
a38e4082 DC |
65 | |
66 | /** | |
67 | * list_lru_add: add an element to the lru list's tail | |
68 | * @list_lru: the lru pointer | |
69 | * @item: the item to be added. | |
70 | * | |
71 | * If the element is already part of a list, this function returns doing | |
72 | * nothing. Therefore the caller does not need to keep state about whether or | |
73 | * not the element already belongs in the list and is allowed to lazy update | |
74 | * it. Note however that this is valid for *a* list, not *this* list. If | |
75 | * the caller organize itself in a way that elements can be in more than | |
76 | * one type of list, it is up to the caller to fully remove the item from | |
77 | * the previous list (with list_lru_del() for instance) before moving it | |
78 | * to @list_lru | |
79 | * | |
80 | * Return value: true if the list was updated, false otherwise | |
81 | */ | |
82 | bool list_lru_add(struct list_lru *lru, struct list_head *item); | |
83 | ||
84 | /** | |
85 | * list_lru_del: delete an element to the lru list | |
86 | * @list_lru: the lru pointer | |
87 | * @item: the item to be deleted. | |
88 | * | |
89 | * This function works analogously as list_lru_add in terms of list | |
90 | * manipulation. The comments about an element already pertaining to | |
91 | * a list are also valid for list_lru_del. | |
92 | * | |
93 | * Return value: true if the list was updated, false otherwise | |
94 | */ | |
95 | bool list_lru_del(struct list_lru *lru, struct list_head *item); | |
96 | ||
97 | /** | |
60d3fd32 | 98 | * list_lru_count_one: return the number of objects currently held by @lru |
a38e4082 | 99 | * @lru: the lru pointer. |
6a4f496f | 100 | * @nid: the node id to count from. |
60d3fd32 | 101 | * @memcg: the cgroup to count from. |
a38e4082 DC |
102 | * |
103 | * Always return a non-negative number, 0 for empty lists. There is no | |
104 | * guarantee that the list is not updated while the count is being computed. | |
105 | * Callers that want such a guarantee need to provide an outer lock. | |
106 | */ | |
60d3fd32 VD |
107 | unsigned long list_lru_count_one(struct list_lru *lru, |
108 | int nid, struct mem_cgroup *memcg); | |
6a4f496f | 109 | unsigned long list_lru_count_node(struct list_lru *lru, int nid); |
503c358c VD |
110 | |
111 | static inline unsigned long list_lru_shrink_count(struct list_lru *lru, | |
112 | struct shrink_control *sc) | |
113 | { | |
60d3fd32 | 114 | return list_lru_count_one(lru, sc->nid, sc->memcg); |
503c358c VD |
115 | } |
116 | ||
6a4f496f GC |
117 | static inline unsigned long list_lru_count(struct list_lru *lru) |
118 | { | |
119 | long count = 0; | |
120 | int nid; | |
121 | ||
ff0b67ef | 122 | for_each_node_state(nid, N_NORMAL_MEMORY) |
6a4f496f GC |
123 | count += list_lru_count_node(lru, nid); |
124 | ||
125 | return count; | |
126 | } | |
a38e4082 | 127 | |
3f97b163 VD |
128 | void list_lru_isolate(struct list_lru_one *list, struct list_head *item); |
129 | void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item, | |
130 | struct list_head *head); | |
131 | ||
132 | typedef enum lru_status (*list_lru_walk_cb)(struct list_head *item, | |
133 | struct list_lru_one *list, spinlock_t *lock, void *cb_arg); | |
134 | ||
a38e4082 | 135 | /** |
60d3fd32 | 136 | * list_lru_walk_one: walk a list_lru, isolating and disposing freeable items. |
a38e4082 | 137 | * @lru: the lru pointer. |
6a4f496f | 138 | * @nid: the node id to scan from. |
60d3fd32 | 139 | * @memcg: the cgroup to scan from. |
a38e4082 DC |
140 | * @isolate: callback function that is resposible for deciding what to do with |
141 | * the item currently being scanned | |
142 | * @cb_arg: opaque type that will be passed to @isolate | |
143 | * @nr_to_walk: how many items to scan. | |
144 | * | |
145 | * This function will scan all elements in a particular list_lru, calling the | |
146 | * @isolate callback for each of those items, along with the current list | |
147 | * spinlock and a caller-provided opaque. The @isolate callback can choose to | |
148 | * drop the lock internally, but *must* return with the lock held. The callback | |
149 | * will return an enum lru_status telling the list_lru infrastructure what to | |
150 | * do with the object being scanned. | |
151 | * | |
152 | * Please note that nr_to_walk does not mean how many objects will be freed, | |
153 | * just how many objects will be scanned. | |
154 | * | |
155 | * Return value: the number of objects effectively removed from the LRU. | |
156 | */ | |
60d3fd32 VD |
157 | unsigned long list_lru_walk_one(struct list_lru *lru, |
158 | int nid, struct mem_cgroup *memcg, | |
159 | list_lru_walk_cb isolate, void *cb_arg, | |
160 | unsigned long *nr_to_walk); | |
6a4f496f GC |
161 | unsigned long list_lru_walk_node(struct list_lru *lru, int nid, |
162 | list_lru_walk_cb isolate, void *cb_arg, | |
163 | unsigned long *nr_to_walk); | |
164 | ||
503c358c VD |
165 | static inline unsigned long |
166 | list_lru_shrink_walk(struct list_lru *lru, struct shrink_control *sc, | |
167 | list_lru_walk_cb isolate, void *cb_arg) | |
168 | { | |
60d3fd32 VD |
169 | return list_lru_walk_one(lru, sc->nid, sc->memcg, isolate, cb_arg, |
170 | &sc->nr_to_scan); | |
503c358c VD |
171 | } |
172 | ||
6a4f496f GC |
173 | static inline unsigned long |
174 | list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate, | |
175 | void *cb_arg, unsigned long nr_to_walk) | |
176 | { | |
177 | long isolated = 0; | |
178 | int nid; | |
179 | ||
ff0b67ef | 180 | for_each_node_state(nid, N_NORMAL_MEMORY) { |
6a4f496f GC |
181 | isolated += list_lru_walk_node(lru, nid, isolate, |
182 | cb_arg, &nr_to_walk); | |
183 | if (nr_to_walk <= 0) | |
184 | break; | |
185 | } | |
186 | return isolated; | |
187 | } | |
a38e4082 | 188 | #endif /* _LRU_LIST_H */ |