Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
a38e4082 DC |
2 | /* |
3 | * Copyright (c) 2013 Red Hat, Inc. and Parallels Inc. All rights reserved. | |
4 | * Authors: David Chinner and Glauber Costa | |
5 | * | |
6 | * Generic LRU infrastructure | |
7 | */ | |
8 | #include <linux/kernel.h> | |
9 | #include <linux/module.h> | |
3b1d58a4 | 10 | #include <linux/mm.h> |
a38e4082 | 11 | #include <linux/list_lru.h> |
5ca302c8 | 12 | #include <linux/slab.h> |
c0a5b560 | 13 | #include <linux/mutex.h> |
60d3fd32 | 14 | #include <linux/memcontrol.h> |
4d96ba35 | 15 | #include "slab.h" |
c0a5b560 | 16 | |
84c07d11 | 17 | #ifdef CONFIG_MEMCG_KMEM |
c0a5b560 VD |
18 | static LIST_HEAD(list_lrus); |
19 | static DEFINE_MUTEX(list_lrus_mutex); | |
20 | ||
21 | static void list_lru_register(struct list_lru *lru) | |
22 | { | |
23 | mutex_lock(&list_lrus_mutex); | |
24 | list_add(&lru->list, &list_lrus); | |
25 | mutex_unlock(&list_lrus_mutex); | |
26 | } | |
27 | ||
28 | static void list_lru_unregister(struct list_lru *lru) | |
29 | { | |
30 | mutex_lock(&list_lrus_mutex); | |
31 | list_del(&lru->list); | |
32 | mutex_unlock(&list_lrus_mutex); | |
33 | } | |
c0a5b560 | 34 | |
fae91d6d KT |
35 | static int lru_shrinker_id(struct list_lru *lru) |
36 | { | |
37 | return lru->shrinker_id; | |
38 | } | |
39 | ||
60d3fd32 VD |
40 | static inline bool list_lru_memcg_aware(struct list_lru *lru) |
41 | { | |
3e858996 | 42 | return lru->memcg_aware; |
60d3fd32 VD |
43 | } |
44 | ||
45 | static inline struct list_lru_one * | |
46 | list_lru_from_memcg_idx(struct list_lru_node *nlru, int idx) | |
47 | { | |
0c7c1bed | 48 | struct list_lru_memcg *memcg_lrus; |
60d3fd32 | 49 | /* |
0c7c1bed KT |
50 | * Either lock or RCU protects the array of per cgroup lists |
51 | * from relocation (see memcg_update_list_lru_node). | |
60d3fd32 | 52 | */ |
0c7c1bed KT |
53 | memcg_lrus = rcu_dereference_check(nlru->memcg_lrus, |
54 | lockdep_is_held(&nlru->lock)); | |
55 | if (memcg_lrus && idx >= 0) | |
56 | return memcg_lrus->lru[idx]; | |
60d3fd32 VD |
57 | return &nlru->lru; |
58 | } | |
59 | ||
60 | static inline struct list_lru_one * | |
44bd4a47 KT |
61 | list_lru_from_kmem(struct list_lru_node *nlru, void *ptr, |
62 | struct mem_cgroup **memcg_ptr) | |
60d3fd32 | 63 | { |
44bd4a47 KT |
64 | struct list_lru_one *l = &nlru->lru; |
65 | struct mem_cgroup *memcg = NULL; | |
60d3fd32 VD |
66 | |
67 | if (!nlru->memcg_lrus) | |
44bd4a47 | 68 | goto out; |
60d3fd32 | 69 | |
4f103c63 | 70 | memcg = mem_cgroup_from_obj(ptr); |
60d3fd32 | 71 | if (!memcg) |
44bd4a47 | 72 | goto out; |
60d3fd32 | 73 | |
44bd4a47 KT |
74 | l = list_lru_from_memcg_idx(nlru, memcg_cache_id(memcg)); |
75 | out: | |
76 | if (memcg_ptr) | |
77 | *memcg_ptr = memcg; | |
78 | return l; | |
60d3fd32 VD |
79 | } |
80 | #else | |
e0295238 KT |
81 | static void list_lru_register(struct list_lru *lru) |
82 | { | |
83 | } | |
84 | ||
85 | static void list_lru_unregister(struct list_lru *lru) | |
86 | { | |
87 | } | |
88 | ||
fae91d6d KT |
89 | static int lru_shrinker_id(struct list_lru *lru) |
90 | { | |
91 | return -1; | |
92 | } | |
93 | ||
60d3fd32 VD |
94 | static inline bool list_lru_memcg_aware(struct list_lru *lru) |
95 | { | |
96 | return false; | |
97 | } | |
98 | ||
99 | static inline struct list_lru_one * | |
100 | list_lru_from_memcg_idx(struct list_lru_node *nlru, int idx) | |
101 | { | |
102 | return &nlru->lru; | |
103 | } | |
104 | ||
105 | static inline struct list_lru_one * | |
44bd4a47 KT |
106 | list_lru_from_kmem(struct list_lru_node *nlru, void *ptr, |
107 | struct mem_cgroup **memcg_ptr) | |
60d3fd32 | 108 | { |
44bd4a47 KT |
109 | if (memcg_ptr) |
110 | *memcg_ptr = NULL; | |
60d3fd32 VD |
111 | return &nlru->lru; |
112 | } | |
84c07d11 | 113 | #endif /* CONFIG_MEMCG_KMEM */ |
60d3fd32 | 114 | |
a38e4082 DC |
115 | bool list_lru_add(struct list_lru *lru, struct list_head *item) |
116 | { | |
3b1d58a4 DC |
117 | int nid = page_to_nid(virt_to_page(item)); |
118 | struct list_lru_node *nlru = &lru->node[nid]; | |
fae91d6d | 119 | struct mem_cgroup *memcg; |
60d3fd32 | 120 | struct list_lru_one *l; |
3b1d58a4 DC |
121 | |
122 | spin_lock(&nlru->lock); | |
a38e4082 | 123 | if (list_empty(item)) { |
fae91d6d | 124 | l = list_lru_from_kmem(nlru, item, &memcg); |
60d3fd32 | 125 | list_add_tail(item, &l->list); |
fae91d6d KT |
126 | /* Set shrinker bit if the first element was added */ |
127 | if (!l->nr_items++) | |
128 | memcg_set_shrinker_bit(memcg, nid, | |
129 | lru_shrinker_id(lru)); | |
2c80cd57 | 130 | nlru->nr_items++; |
3b1d58a4 | 131 | spin_unlock(&nlru->lock); |
a38e4082 DC |
132 | return true; |
133 | } | |
3b1d58a4 | 134 | spin_unlock(&nlru->lock); |
a38e4082 DC |
135 | return false; |
136 | } | |
137 | EXPORT_SYMBOL_GPL(list_lru_add); | |
138 | ||
139 | bool list_lru_del(struct list_lru *lru, struct list_head *item) | |
140 | { | |
3b1d58a4 DC |
141 | int nid = page_to_nid(virt_to_page(item)); |
142 | struct list_lru_node *nlru = &lru->node[nid]; | |
60d3fd32 | 143 | struct list_lru_one *l; |
3b1d58a4 DC |
144 | |
145 | spin_lock(&nlru->lock); | |
a38e4082 | 146 | if (!list_empty(item)) { |
44bd4a47 | 147 | l = list_lru_from_kmem(nlru, item, NULL); |
a38e4082 | 148 | list_del_init(item); |
60d3fd32 | 149 | l->nr_items--; |
2c80cd57 | 150 | nlru->nr_items--; |
3b1d58a4 | 151 | spin_unlock(&nlru->lock); |
a38e4082 DC |
152 | return true; |
153 | } | |
3b1d58a4 | 154 | spin_unlock(&nlru->lock); |
a38e4082 DC |
155 | return false; |
156 | } | |
157 | EXPORT_SYMBOL_GPL(list_lru_del); | |
158 | ||
3f97b163 VD |
159 | void list_lru_isolate(struct list_lru_one *list, struct list_head *item) |
160 | { | |
161 | list_del_init(item); | |
162 | list->nr_items--; | |
163 | } | |
164 | EXPORT_SYMBOL_GPL(list_lru_isolate); | |
165 | ||
166 | void list_lru_isolate_move(struct list_lru_one *list, struct list_head *item, | |
167 | struct list_head *head) | |
168 | { | |
169 | list_move(item, head); | |
170 | list->nr_items--; | |
171 | } | |
172 | EXPORT_SYMBOL_GPL(list_lru_isolate_move); | |
173 | ||
930eaac5 AM |
174 | unsigned long list_lru_count_one(struct list_lru *lru, |
175 | int nid, struct mem_cgroup *memcg) | |
a38e4082 | 176 | { |
6a4f496f | 177 | struct list_lru_node *nlru = &lru->node[nid]; |
60d3fd32 VD |
178 | struct list_lru_one *l; |
179 | unsigned long count; | |
3b1d58a4 | 180 | |
0c7c1bed | 181 | rcu_read_lock(); |
930eaac5 | 182 | l = list_lru_from_memcg_idx(nlru, memcg_cache_id(memcg)); |
a1f45935 | 183 | count = READ_ONCE(l->nr_items); |
0c7c1bed | 184 | rcu_read_unlock(); |
3b1d58a4 DC |
185 | |
186 | return count; | |
187 | } | |
60d3fd32 VD |
188 | EXPORT_SYMBOL_GPL(list_lru_count_one); |
189 | ||
190 | unsigned long list_lru_count_node(struct list_lru *lru, int nid) | |
191 | { | |
2c80cd57 | 192 | struct list_lru_node *nlru; |
60d3fd32 | 193 | |
2c80cd57 ST |
194 | nlru = &lru->node[nid]; |
195 | return nlru->nr_items; | |
60d3fd32 | 196 | } |
6a4f496f | 197 | EXPORT_SYMBOL_GPL(list_lru_count_node); |
3b1d58a4 | 198 | |
60d3fd32 | 199 | static unsigned long |
6e018968 | 200 | __list_lru_walk_one(struct list_lru_node *nlru, int memcg_idx, |
60d3fd32 VD |
201 | list_lru_walk_cb isolate, void *cb_arg, |
202 | unsigned long *nr_to_walk) | |
3b1d58a4 DC |
203 | { |
204 | ||
60d3fd32 | 205 | struct list_lru_one *l; |
a38e4082 | 206 | struct list_head *item, *n; |
3b1d58a4 | 207 | unsigned long isolated = 0; |
a38e4082 | 208 | |
60d3fd32 | 209 | l = list_lru_from_memcg_idx(nlru, memcg_idx); |
a38e4082 | 210 | restart: |
60d3fd32 | 211 | list_for_each_safe(item, n, &l->list) { |
a38e4082 | 212 | enum lru_status ret; |
5cedf721 DC |
213 | |
214 | /* | |
215 | * decrement nr_to_walk first so that we don't livelock if we | |
3dc5f032 | 216 | * get stuck on large numbers of LRU_RETRY items |
5cedf721 | 217 | */ |
c56b097a | 218 | if (!*nr_to_walk) |
5cedf721 | 219 | break; |
c56b097a | 220 | --*nr_to_walk; |
5cedf721 | 221 | |
3f97b163 | 222 | ret = isolate(item, l, &nlru->lock, cb_arg); |
a38e4082 | 223 | switch (ret) { |
449dd698 JW |
224 | case LRU_REMOVED_RETRY: |
225 | assert_spin_locked(&nlru->lock); | |
e4a9bc58 | 226 | fallthrough; |
a38e4082 | 227 | case LRU_REMOVED: |
3b1d58a4 | 228 | isolated++; |
2c80cd57 | 229 | nlru->nr_items--; |
449dd698 JW |
230 | /* |
231 | * If the lru lock has been dropped, our list | |
232 | * traversal is now invalid and so we have to | |
233 | * restart from scratch. | |
234 | */ | |
235 | if (ret == LRU_REMOVED_RETRY) | |
236 | goto restart; | |
a38e4082 DC |
237 | break; |
238 | case LRU_ROTATE: | |
60d3fd32 | 239 | list_move_tail(item, &l->list); |
a38e4082 DC |
240 | break; |
241 | case LRU_SKIP: | |
242 | break; | |
243 | case LRU_RETRY: | |
5cedf721 DC |
244 | /* |
245 | * The lru lock has been dropped, our list traversal is | |
246 | * now invalid and so we have to restart from scratch. | |
247 | */ | |
449dd698 | 248 | assert_spin_locked(&nlru->lock); |
a38e4082 DC |
249 | goto restart; |
250 | default: | |
251 | BUG(); | |
252 | } | |
a38e4082 | 253 | } |
3b1d58a4 DC |
254 | return isolated; |
255 | } | |
60d3fd32 VD |
256 | |
257 | unsigned long | |
258 | list_lru_walk_one(struct list_lru *lru, int nid, struct mem_cgroup *memcg, | |
259 | list_lru_walk_cb isolate, void *cb_arg, | |
260 | unsigned long *nr_to_walk) | |
261 | { | |
6cfe57a9 SAS |
262 | struct list_lru_node *nlru = &lru->node[nid]; |
263 | unsigned long ret; | |
264 | ||
265 | spin_lock(&nlru->lock); | |
6e018968 SAS |
266 | ret = __list_lru_walk_one(nlru, memcg_cache_id(memcg), isolate, cb_arg, |
267 | nr_to_walk); | |
6cfe57a9 SAS |
268 | spin_unlock(&nlru->lock); |
269 | return ret; | |
60d3fd32 VD |
270 | } |
271 | EXPORT_SYMBOL_GPL(list_lru_walk_one); | |
272 | ||
6b51e881 SAS |
273 | unsigned long |
274 | list_lru_walk_one_irq(struct list_lru *lru, int nid, struct mem_cgroup *memcg, | |
275 | list_lru_walk_cb isolate, void *cb_arg, | |
276 | unsigned long *nr_to_walk) | |
277 | { | |
278 | struct list_lru_node *nlru = &lru->node[nid]; | |
279 | unsigned long ret; | |
280 | ||
281 | spin_lock_irq(&nlru->lock); | |
282 | ret = __list_lru_walk_one(nlru, memcg_cache_id(memcg), isolate, cb_arg, | |
283 | nr_to_walk); | |
284 | spin_unlock_irq(&nlru->lock); | |
285 | return ret; | |
286 | } | |
287 | ||
60d3fd32 VD |
288 | unsigned long list_lru_walk_node(struct list_lru *lru, int nid, |
289 | list_lru_walk_cb isolate, void *cb_arg, | |
290 | unsigned long *nr_to_walk) | |
291 | { | |
292 | long isolated = 0; | |
293 | int memcg_idx; | |
294 | ||
87a5ffc1 SAS |
295 | isolated += list_lru_walk_one(lru, nid, NULL, isolate, cb_arg, |
296 | nr_to_walk); | |
60d3fd32 VD |
297 | if (*nr_to_walk > 0 && list_lru_memcg_aware(lru)) { |
298 | for_each_memcg_cache_index(memcg_idx) { | |
6cfe57a9 SAS |
299 | struct list_lru_node *nlru = &lru->node[nid]; |
300 | ||
301 | spin_lock(&nlru->lock); | |
6e018968 SAS |
302 | isolated += __list_lru_walk_one(nlru, memcg_idx, |
303 | isolate, cb_arg, | |
304 | nr_to_walk); | |
6cfe57a9 SAS |
305 | spin_unlock(&nlru->lock); |
306 | ||
60d3fd32 VD |
307 | if (*nr_to_walk <= 0) |
308 | break; | |
309 | } | |
310 | } | |
311 | return isolated; | |
312 | } | |
3b1d58a4 DC |
313 | EXPORT_SYMBOL_GPL(list_lru_walk_node); |
314 | ||
60d3fd32 VD |
315 | static void init_one_lru(struct list_lru_one *l) |
316 | { | |
317 | INIT_LIST_HEAD(&l->list); | |
318 | l->nr_items = 0; | |
319 | } | |
320 | ||
84c07d11 | 321 | #ifdef CONFIG_MEMCG_KMEM |
60d3fd32 VD |
322 | static void __memcg_destroy_list_lru_node(struct list_lru_memcg *memcg_lrus, |
323 | int begin, int end) | |
324 | { | |
325 | int i; | |
326 | ||
327 | for (i = begin; i < end; i++) | |
328 | kfree(memcg_lrus->lru[i]); | |
329 | } | |
330 | ||
331 | static int __memcg_init_list_lru_node(struct list_lru_memcg *memcg_lrus, | |
332 | int begin, int end) | |
333 | { | |
334 | int i; | |
335 | ||
336 | for (i = begin; i < end; i++) { | |
337 | struct list_lru_one *l; | |
338 | ||
339 | l = kmalloc(sizeof(struct list_lru_one), GFP_KERNEL); | |
340 | if (!l) | |
341 | goto fail; | |
342 | ||
343 | init_one_lru(l); | |
344 | memcg_lrus->lru[i] = l; | |
345 | } | |
346 | return 0; | |
347 | fail: | |
3510955b | 348 | __memcg_destroy_list_lru_node(memcg_lrus, begin, i); |
60d3fd32 VD |
349 | return -ENOMEM; |
350 | } | |
351 | ||
352 | static int memcg_init_list_lru_node(struct list_lru_node *nlru) | |
353 | { | |
0c7c1bed | 354 | struct list_lru_memcg *memcg_lrus; |
60d3fd32 VD |
355 | int size = memcg_nr_cache_ids; |
356 | ||
0c7c1bed KT |
357 | memcg_lrus = kvmalloc(sizeof(*memcg_lrus) + |
358 | size * sizeof(void *), GFP_KERNEL); | |
359 | if (!memcg_lrus) | |
60d3fd32 VD |
360 | return -ENOMEM; |
361 | ||
0c7c1bed KT |
362 | if (__memcg_init_list_lru_node(memcg_lrus, 0, size)) { |
363 | kvfree(memcg_lrus); | |
60d3fd32 VD |
364 | return -ENOMEM; |
365 | } | |
0c7c1bed | 366 | RCU_INIT_POINTER(nlru->memcg_lrus, memcg_lrus); |
60d3fd32 VD |
367 | |
368 | return 0; | |
369 | } | |
370 | ||
371 | static void memcg_destroy_list_lru_node(struct list_lru_node *nlru) | |
372 | { | |
0c7c1bed KT |
373 | struct list_lru_memcg *memcg_lrus; |
374 | /* | |
375 | * This is called when shrinker has already been unregistered, | |
e0feed08 | 376 | * and nobody can use it. So, there is no need to use kvfree_rcu_local(). |
0c7c1bed KT |
377 | */ |
378 | memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus, true); | |
379 | __memcg_destroy_list_lru_node(memcg_lrus, 0, memcg_nr_cache_ids); | |
380 | kvfree(memcg_lrus); | |
381 | } | |
382 | ||
e0feed08 | 383 | static void kvfree_rcu_local(struct rcu_head *head) |
0c7c1bed KT |
384 | { |
385 | struct list_lru_memcg *mlru; | |
386 | ||
387 | mlru = container_of(head, struct list_lru_memcg, rcu); | |
388 | kvfree(mlru); | |
60d3fd32 VD |
389 | } |
390 | ||
391 | static int memcg_update_list_lru_node(struct list_lru_node *nlru, | |
392 | int old_size, int new_size) | |
393 | { | |
394 | struct list_lru_memcg *old, *new; | |
395 | ||
396 | BUG_ON(old_size > new_size); | |
397 | ||
0c7c1bed KT |
398 | old = rcu_dereference_protected(nlru->memcg_lrus, |
399 | lockdep_is_held(&list_lrus_mutex)); | |
400 | new = kvmalloc(sizeof(*new) + new_size * sizeof(void *), GFP_KERNEL); | |
60d3fd32 VD |
401 | if (!new) |
402 | return -ENOMEM; | |
403 | ||
404 | if (__memcg_init_list_lru_node(new, old_size, new_size)) { | |
f80c7dab | 405 | kvfree(new); |
60d3fd32 VD |
406 | return -ENOMEM; |
407 | } | |
408 | ||
0c7c1bed | 409 | memcpy(&new->lru, &old->lru, old_size * sizeof(void *)); |
60d3fd32 VD |
410 | |
411 | /* | |
0c7c1bed KT |
412 | * The locking below allows readers that hold nlru->lock avoid taking |
413 | * rcu_read_lock (see list_lru_from_memcg_idx). | |
60d3fd32 VD |
414 | * |
415 | * Since list_lru_{add,del} may be called under an IRQ-safe lock, | |
416 | * we have to use IRQ-safe primitives here to avoid deadlock. | |
417 | */ | |
418 | spin_lock_irq(&nlru->lock); | |
0c7c1bed | 419 | rcu_assign_pointer(nlru->memcg_lrus, new); |
60d3fd32 VD |
420 | spin_unlock_irq(&nlru->lock); |
421 | ||
e0feed08 | 422 | call_rcu(&old->rcu, kvfree_rcu_local); |
60d3fd32 VD |
423 | return 0; |
424 | } | |
425 | ||
426 | static void memcg_cancel_update_list_lru_node(struct list_lru_node *nlru, | |
427 | int old_size, int new_size) | |
428 | { | |
0c7c1bed KT |
429 | struct list_lru_memcg *memcg_lrus; |
430 | ||
431 | memcg_lrus = rcu_dereference_protected(nlru->memcg_lrus, | |
432 | lockdep_is_held(&list_lrus_mutex)); | |
60d3fd32 VD |
433 | /* do not bother shrinking the array back to the old size, because we |
434 | * cannot handle allocation failures here */ | |
0c7c1bed | 435 | __memcg_destroy_list_lru_node(memcg_lrus, old_size, new_size); |
60d3fd32 VD |
436 | } |
437 | ||
438 | static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware) | |
439 | { | |
440 | int i; | |
441 | ||
3e858996 JS |
442 | lru->memcg_aware = memcg_aware; |
443 | ||
145949a1 R |
444 | if (!memcg_aware) |
445 | return 0; | |
446 | ||
447 | for_each_node(i) { | |
448 | if (memcg_init_list_lru_node(&lru->node[i])) | |
60d3fd32 VD |
449 | goto fail; |
450 | } | |
451 | return 0; | |
452 | fail: | |
145949a1 R |
453 | for (i = i - 1; i >= 0; i--) { |
454 | if (!lru->node[i].memcg_lrus) | |
455 | continue; | |
60d3fd32 | 456 | memcg_destroy_list_lru_node(&lru->node[i]); |
145949a1 | 457 | } |
60d3fd32 VD |
458 | return -ENOMEM; |
459 | } | |
460 | ||
461 | static void memcg_destroy_list_lru(struct list_lru *lru) | |
462 | { | |
463 | int i; | |
464 | ||
465 | if (!list_lru_memcg_aware(lru)) | |
466 | return; | |
467 | ||
145949a1 | 468 | for_each_node(i) |
60d3fd32 VD |
469 | memcg_destroy_list_lru_node(&lru->node[i]); |
470 | } | |
471 | ||
472 | static int memcg_update_list_lru(struct list_lru *lru, | |
473 | int old_size, int new_size) | |
474 | { | |
475 | int i; | |
476 | ||
477 | if (!list_lru_memcg_aware(lru)) | |
478 | return 0; | |
479 | ||
145949a1 | 480 | for_each_node(i) { |
60d3fd32 VD |
481 | if (memcg_update_list_lru_node(&lru->node[i], |
482 | old_size, new_size)) | |
483 | goto fail; | |
484 | } | |
485 | return 0; | |
486 | fail: | |
145949a1 R |
487 | for (i = i - 1; i >= 0; i--) { |
488 | if (!lru->node[i].memcg_lrus) | |
489 | continue; | |
490 | ||
60d3fd32 VD |
491 | memcg_cancel_update_list_lru_node(&lru->node[i], |
492 | old_size, new_size); | |
145949a1 | 493 | } |
60d3fd32 VD |
494 | return -ENOMEM; |
495 | } | |
496 | ||
497 | static void memcg_cancel_update_list_lru(struct list_lru *lru, | |
498 | int old_size, int new_size) | |
499 | { | |
500 | int i; | |
501 | ||
502 | if (!list_lru_memcg_aware(lru)) | |
503 | return; | |
504 | ||
145949a1 | 505 | for_each_node(i) |
60d3fd32 VD |
506 | memcg_cancel_update_list_lru_node(&lru->node[i], |
507 | old_size, new_size); | |
508 | } | |
509 | ||
510 | int memcg_update_all_list_lrus(int new_size) | |
511 | { | |
512 | int ret = 0; | |
513 | struct list_lru *lru; | |
514 | int old_size = memcg_nr_cache_ids; | |
515 | ||
516 | mutex_lock(&list_lrus_mutex); | |
517 | list_for_each_entry(lru, &list_lrus, list) { | |
518 | ret = memcg_update_list_lru(lru, old_size, new_size); | |
519 | if (ret) | |
520 | goto fail; | |
521 | } | |
522 | out: | |
523 | mutex_unlock(&list_lrus_mutex); | |
524 | return ret; | |
525 | fail: | |
526 | list_for_each_entry_continue_reverse(lru, &list_lrus, list) | |
527 | memcg_cancel_update_list_lru(lru, old_size, new_size); | |
528 | goto out; | |
529 | } | |
2788cf0c | 530 | |
3b82c4dc | 531 | static void memcg_drain_list_lru_node(struct list_lru *lru, int nid, |
9bec5c35 | 532 | int src_idx, struct mem_cgroup *dst_memcg) |
2788cf0c | 533 | { |
3b82c4dc | 534 | struct list_lru_node *nlru = &lru->node[nid]; |
9bec5c35 | 535 | int dst_idx = dst_memcg->kmemcg_id; |
2788cf0c VD |
536 | struct list_lru_one *src, *dst; |
537 | ||
538 | /* | |
539 | * Since list_lru_{add,del} may be called under an IRQ-safe lock, | |
540 | * we have to use IRQ-safe primitives here to avoid deadlock. | |
541 | */ | |
542 | spin_lock_irq(&nlru->lock); | |
543 | ||
544 | src = list_lru_from_memcg_idx(nlru, src_idx); | |
545 | dst = list_lru_from_memcg_idx(nlru, dst_idx); | |
546 | ||
547 | list_splice_init(&src->list, &dst->list); | |
8199be00 YS |
548 | |
549 | if (src->nr_items) { | |
550 | dst->nr_items += src->nr_items; | |
fae91d6d | 551 | memcg_set_shrinker_bit(dst_memcg, nid, lru_shrinker_id(lru)); |
8199be00 YS |
552 | src->nr_items = 0; |
553 | } | |
2788cf0c VD |
554 | |
555 | spin_unlock_irq(&nlru->lock); | |
556 | } | |
557 | ||
558 | static void memcg_drain_list_lru(struct list_lru *lru, | |
9bec5c35 | 559 | int src_idx, struct mem_cgroup *dst_memcg) |
2788cf0c VD |
560 | { |
561 | int i; | |
562 | ||
563 | if (!list_lru_memcg_aware(lru)) | |
564 | return; | |
565 | ||
145949a1 | 566 | for_each_node(i) |
3b82c4dc | 567 | memcg_drain_list_lru_node(lru, i, src_idx, dst_memcg); |
2788cf0c VD |
568 | } |
569 | ||
9bec5c35 | 570 | void memcg_drain_all_list_lrus(int src_idx, struct mem_cgroup *dst_memcg) |
2788cf0c VD |
571 | { |
572 | struct list_lru *lru; | |
573 | ||
574 | mutex_lock(&list_lrus_mutex); | |
575 | list_for_each_entry(lru, &list_lrus, list) | |
9bec5c35 | 576 | memcg_drain_list_lru(lru, src_idx, dst_memcg); |
2788cf0c VD |
577 | mutex_unlock(&list_lrus_mutex); |
578 | } | |
60d3fd32 VD |
579 | #else |
580 | static int memcg_init_list_lru(struct list_lru *lru, bool memcg_aware) | |
581 | { | |
582 | return 0; | |
583 | } | |
584 | ||
585 | static void memcg_destroy_list_lru(struct list_lru *lru) | |
586 | { | |
587 | } | |
84c07d11 | 588 | #endif /* CONFIG_MEMCG_KMEM */ |
60d3fd32 VD |
589 | |
590 | int __list_lru_init(struct list_lru *lru, bool memcg_aware, | |
c92e8e10 | 591 | struct lock_class_key *key, struct shrinker *shrinker) |
a38e4082 | 592 | { |
3b1d58a4 | 593 | int i; |
60d3fd32 VD |
594 | int err = -ENOMEM; |
595 | ||
c92e8e10 KT |
596 | #ifdef CONFIG_MEMCG_KMEM |
597 | if (shrinker) | |
598 | lru->shrinker_id = shrinker->id; | |
599 | else | |
600 | lru->shrinker_id = -1; | |
601 | #endif | |
60d3fd32 | 602 | memcg_get_cache_ids(); |
5ca302c8 | 603 | |
b9726c26 | 604 | lru->node = kcalloc(nr_node_ids, sizeof(*lru->node), GFP_KERNEL); |
5ca302c8 | 605 | if (!lru->node) |
60d3fd32 | 606 | goto out; |
a38e4082 | 607 | |
145949a1 | 608 | for_each_node(i) { |
3b1d58a4 | 609 | spin_lock_init(&lru->node[i].lock); |
449dd698 JW |
610 | if (key) |
611 | lockdep_set_class(&lru->node[i].lock, key); | |
60d3fd32 VD |
612 | init_one_lru(&lru->node[i].lru); |
613 | } | |
614 | ||
615 | err = memcg_init_list_lru(lru, memcg_aware); | |
616 | if (err) { | |
617 | kfree(lru->node); | |
1bc11d70 AP |
618 | /* Do this so a list_lru_destroy() doesn't crash: */ |
619 | lru->node = NULL; | |
60d3fd32 | 620 | goto out; |
3b1d58a4 | 621 | } |
60d3fd32 | 622 | |
c0a5b560 | 623 | list_lru_register(lru); |
60d3fd32 VD |
624 | out: |
625 | memcg_put_cache_ids(); | |
626 | return err; | |
a38e4082 | 627 | } |
60d3fd32 | 628 | EXPORT_SYMBOL_GPL(__list_lru_init); |
5ca302c8 GC |
629 | |
630 | void list_lru_destroy(struct list_lru *lru) | |
631 | { | |
c0a5b560 VD |
632 | /* Already destroyed or not yet initialized? */ |
633 | if (!lru->node) | |
634 | return; | |
60d3fd32 VD |
635 | |
636 | memcg_get_cache_ids(); | |
637 | ||
c0a5b560 | 638 | list_lru_unregister(lru); |
60d3fd32 VD |
639 | |
640 | memcg_destroy_list_lru(lru); | |
5ca302c8 | 641 | kfree(lru->node); |
c0a5b560 | 642 | lru->node = NULL; |
60d3fd32 | 643 | |
c92e8e10 KT |
644 | #ifdef CONFIG_MEMCG_KMEM |
645 | lru->shrinker_id = -1; | |
646 | #endif | |
60d3fd32 | 647 | memcg_put_cache_ids(); |
5ca302c8 GC |
648 | } |
649 | EXPORT_SYMBOL_GPL(list_lru_destroy); |