Commit | Line | Data |
---|---|---|
8cdea7c0 BS |
1 | /* memcontrol.h - Memory Controller |
2 | * | |
3 | * Copyright IBM Corporation, 2007 | |
4 | * Author Balbir Singh <balbir@linux.vnet.ibm.com> | |
5 | * | |
78fb7466 PE |
6 | * Copyright 2007 OpenVZ SWsoft Inc |
7 | * Author: Pavel Emelianov <xemul@openvz.org> | |
8 | * | |
8cdea7c0 BS |
9 | * This program is free software; you can redistribute it and/or modify |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | */ | |
19 | ||
20 | #ifndef _LINUX_MEMCONTROL_H | |
21 | #define _LINUX_MEMCONTROL_H | |
f8d66542 | 22 | #include <linux/cgroup.h> |
456f998e | 23 | #include <linux/vm_event_item.h> |
7ae1e1d0 | 24 | #include <linux/hardirq.h> |
a8964b9b | 25 | #include <linux/jump_label.h> |
456f998e | 26 | |
78fb7466 PE |
27 | struct mem_cgroup; |
28 | struct page_cgroup; | |
8697d331 BS |
29 | struct page; |
30 | struct mm_struct; | |
2633d7a0 | 31 | struct kmem_cache; |
78fb7466 | 32 | |
2a7106f2 GT |
33 | /* Stats that can be updated by kernel. */ |
34 | enum mem_cgroup_page_stat_item { | |
35 | MEMCG_NR_FILE_MAPPED, /* # of pages charged as file rss */ | |
36 | }; | |
37 | ||
5660048c JW |
38 | struct mem_cgroup_reclaim_cookie { |
39 | struct zone *zone; | |
40 | int priority; | |
41 | unsigned int generation; | |
42 | }; | |
43 | ||
c255a458 | 44 | #ifdef CONFIG_MEMCG |
2c26fdd7 KH |
45 | /* |
46 | * All "charge" functions with gfp_mask should use GFP_KERNEL or | |
47 | * (gfp_mask & GFP_RECLAIM_MASK). In current implementatin, memcg doesn't | |
48 | * alloc memory but reclaims memory from all available zones. So, "where I want | |
49 | * memory from" bits of gfp_mask has no meaning. So any bits of that field is | |
50 | * available but adding a rule is better. charge functions' gfp_mask should | |
51 | * be set to GFP_KERNEL or gfp_mask & GFP_RECLAIM_MASK for avoiding ambiguous | |
52 | * codes. | |
53 | * (Of course, if memcg does memory allocation in future, GFP_KERNEL is sane.) | |
54 | */ | |
78fb7466 | 55 | |
7a81b88c | 56 | extern int mem_cgroup_newpage_charge(struct page *page, struct mm_struct *mm, |
e1a1cd59 | 57 | gfp_t gfp_mask); |
7a81b88c | 58 | /* for swap handling */ |
8c7c6e34 | 59 | extern int mem_cgroup_try_charge_swapin(struct mm_struct *mm, |
72835c86 | 60 | struct page *page, gfp_t mask, struct mem_cgroup **memcgp); |
7a81b88c | 61 | extern void mem_cgroup_commit_charge_swapin(struct page *page, |
72835c86 JW |
62 | struct mem_cgroup *memcg); |
63 | extern void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg); | |
7a81b88c | 64 | |
8289546e HD |
65 | extern int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
66 | gfp_t gfp_mask); | |
925b7673 JW |
67 | |
68 | struct lruvec *mem_cgroup_zone_lruvec(struct zone *, struct mem_cgroup *); | |
fa9add64 | 69 | struct lruvec *mem_cgroup_page_lruvec(struct page *, struct zone *); |
569b846d KH |
70 | |
71 | /* For coalescing uncharge for reducing memcg' overhead*/ | |
72 | extern void mem_cgroup_uncharge_start(void); | |
73 | extern void mem_cgroup_uncharge_end(void); | |
74 | ||
3c541e14 | 75 | extern void mem_cgroup_uncharge_page(struct page *page); |
69029cd5 | 76 | extern void mem_cgroup_uncharge_cache_page(struct page *page); |
c9b0ed51 | 77 | |
c3ac9a8a JW |
78 | bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg, |
79 | struct mem_cgroup *memcg); | |
c0ff4b85 | 80 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg); |
3062fc67 | 81 | |
e42d9d5d | 82 | extern struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page); |
cf475ad2 | 83 | extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p); |
a433658c | 84 | extern struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm); |
cf475ad2 | 85 | |
e1aab161 | 86 | extern struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg); |
d1a4c0b3 | 87 | extern struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont); |
e1aab161 | 88 | |
2e4d4091 | 89 | static inline |
587af308 | 90 | bool mm_match_cgroup(const struct mm_struct *mm, const struct mem_cgroup *memcg) |
2e4d4091 | 91 | { |
587af308 JW |
92 | struct mem_cgroup *task_memcg; |
93 | bool match; | |
c3ac9a8a | 94 | |
2e4d4091 | 95 | rcu_read_lock(); |
587af308 JW |
96 | task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
97 | match = __mem_cgroup_same_or_subtree(memcg, task_memcg); | |
2e4d4091 | 98 | rcu_read_unlock(); |
c3ac9a8a | 99 | return match; |
2e4d4091 | 100 | } |
8a9f3ccd | 101 | |
c0ff4b85 | 102 | extern struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg); |
d324236b | 103 | |
0030f535 JW |
104 | extern void |
105 | mem_cgroup_prepare_migration(struct page *page, struct page *newpage, | |
106 | struct mem_cgroup **memcgp); | |
c0ff4b85 | 107 | extern void mem_cgroup_end_migration(struct mem_cgroup *memcg, |
50de1dd9 | 108 | struct page *oldpage, struct page *newpage, bool migration_ok); |
ae41be37 | 109 | |
5660048c JW |
110 | struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *, |
111 | struct mem_cgroup *, | |
112 | struct mem_cgroup_reclaim_cookie *); | |
113 | void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *); | |
114 | ||
58ae83db KH |
115 | /* |
116 | * For memory reclaim. | |
117 | */ | |
c56d5c7d | 118 | int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec); |
889976db | 119 | int mem_cgroup_select_victim_node(struct mem_cgroup *memcg); |
4d7dcca2 | 120 | unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list); |
fa9add64 | 121 | void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int); |
e222432b BS |
122 | extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, |
123 | struct task_struct *p); | |
ab936cbc KH |
124 | extern void mem_cgroup_replace_page_cache(struct page *oldpage, |
125 | struct page *newpage); | |
58ae83db | 126 | |
f8a51179 | 127 | static inline void mem_cgroup_oom_enable(void) |
11f34787 | 128 | { |
f8a51179 JW |
129 | WARN_ON(current->memcg_oom.may_oom); |
130 | current->memcg_oom.may_oom = 1; | |
11f34787 JW |
131 | } |
132 | ||
f8a51179 | 133 | static inline void mem_cgroup_oom_disable(void) |
11f34787 | 134 | { |
f8a51179 JW |
135 | WARN_ON(!current->memcg_oom.may_oom); |
136 | current->memcg_oom.may_oom = 0; | |
11f34787 JW |
137 | } |
138 | ||
f79d6a46 JW |
139 | static inline bool task_in_memcg_oom(struct task_struct *p) |
140 | { | |
f8a51179 | 141 | return p->memcg_oom.memcg; |
f79d6a46 JW |
142 | } |
143 | ||
f8a51179 | 144 | bool mem_cgroup_oom_synchronize(bool wait); |
f79d6a46 | 145 | |
c255a458 | 146 | #ifdef CONFIG_MEMCG_SWAP |
c077719b KH |
147 | extern int do_swap_account; |
148 | #endif | |
f8d66542 HT |
149 | |
150 | static inline bool mem_cgroup_disabled(void) | |
151 | { | |
152 | if (mem_cgroup_subsys.disabled) | |
153 | return true; | |
154 | return false; | |
155 | } | |
156 | ||
89c06bd5 KH |
157 | void __mem_cgroup_begin_update_page_stat(struct page *page, bool *locked, |
158 | unsigned long *flags); | |
159 | ||
4331f7d3 KH |
160 | extern atomic_t memcg_moving; |
161 | ||
89c06bd5 KH |
162 | static inline void mem_cgroup_begin_update_page_stat(struct page *page, |
163 | bool *locked, unsigned long *flags) | |
164 | { | |
165 | if (mem_cgroup_disabled()) | |
166 | return; | |
167 | rcu_read_lock(); | |
168 | *locked = false; | |
4331f7d3 KH |
169 | if (atomic_read(&memcg_moving)) |
170 | __mem_cgroup_begin_update_page_stat(page, locked, flags); | |
89c06bd5 KH |
171 | } |
172 | ||
173 | void __mem_cgroup_end_update_page_stat(struct page *page, | |
174 | unsigned long *flags); | |
175 | static inline void mem_cgroup_end_update_page_stat(struct page *page, | |
176 | bool *locked, unsigned long *flags) | |
177 | { | |
178 | if (mem_cgroup_disabled()) | |
179 | return; | |
180 | if (*locked) | |
181 | __mem_cgroup_end_update_page_stat(page, flags); | |
182 | rcu_read_unlock(); | |
183 | } | |
184 | ||
2a7106f2 GT |
185 | void mem_cgroup_update_page_stat(struct page *page, |
186 | enum mem_cgroup_page_stat_item idx, | |
187 | int val); | |
188 | ||
189 | static inline void mem_cgroup_inc_page_stat(struct page *page, | |
190 | enum mem_cgroup_page_stat_item idx) | |
191 | { | |
192 | mem_cgroup_update_page_stat(page, idx, 1); | |
193 | } | |
194 | ||
195 | static inline void mem_cgroup_dec_page_stat(struct page *page, | |
196 | enum mem_cgroup_page_stat_item idx) | |
197 | { | |
198 | mem_cgroup_update_page_stat(page, idx, -1); | |
199 | } | |
200 | ||
4e416953 | 201 | unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order, |
0ae5e89c YH |
202 | gfp_t gfp_mask, |
203 | unsigned long *total_scanned); | |
a63d83f4 | 204 | |
68ae564b DR |
205 | void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx); |
206 | static inline void mem_cgroup_count_vm_event(struct mm_struct *mm, | |
207 | enum vm_event_item idx) | |
208 | { | |
209 | if (mem_cgroup_disabled()) | |
210 | return; | |
211 | __mem_cgroup_count_vm_event(mm, idx); | |
212 | } | |
ca3e0214 | 213 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
e94c8a9c | 214 | void mem_cgroup_split_huge_fixup(struct page *head); |
ca3e0214 KH |
215 | #endif |
216 | ||
f212ad7c DN |
217 | #ifdef CONFIG_DEBUG_VM |
218 | bool mem_cgroup_bad_page_check(struct page *page); | |
219 | void mem_cgroup_print_bad_page(struct page *page); | |
220 | #endif | |
c255a458 | 221 | #else /* CONFIG_MEMCG */ |
7a81b88c KH |
222 | struct mem_cgroup; |
223 | ||
224 | static inline int mem_cgroup_newpage_charge(struct page *page, | |
8289546e | 225 | struct mm_struct *mm, gfp_t gfp_mask) |
8a9f3ccd BS |
226 | { |
227 | return 0; | |
228 | } | |
229 | ||
8289546e HD |
230 | static inline int mem_cgroup_cache_charge(struct page *page, |
231 | struct mm_struct *mm, gfp_t gfp_mask) | |
8a9f3ccd | 232 | { |
8289546e | 233 | return 0; |
8a9f3ccd BS |
234 | } |
235 | ||
8c7c6e34 | 236 | static inline int mem_cgroup_try_charge_swapin(struct mm_struct *mm, |
72835c86 | 237 | struct page *page, gfp_t gfp_mask, struct mem_cgroup **memcgp) |
7a81b88c KH |
238 | { |
239 | return 0; | |
240 | } | |
241 | ||
242 | static inline void mem_cgroup_commit_charge_swapin(struct page *page, | |
72835c86 | 243 | struct mem_cgroup *memcg) |
7a81b88c KH |
244 | { |
245 | } | |
246 | ||
72835c86 | 247 | static inline void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg) |
7a81b88c KH |
248 | { |
249 | } | |
250 | ||
569b846d KH |
251 | static inline void mem_cgroup_uncharge_start(void) |
252 | { | |
253 | } | |
254 | ||
255 | static inline void mem_cgroup_uncharge_end(void) | |
256 | { | |
257 | } | |
258 | ||
8a9f3ccd BS |
259 | static inline void mem_cgroup_uncharge_page(struct page *page) |
260 | { | |
261 | } | |
262 | ||
69029cd5 KH |
263 | static inline void mem_cgroup_uncharge_cache_page(struct page *page) |
264 | { | |
265 | } | |
266 | ||
925b7673 JW |
267 | static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone, |
268 | struct mem_cgroup *memcg) | |
08e552c6 | 269 | { |
925b7673 | 270 | return &zone->lruvec; |
08e552c6 KH |
271 | } |
272 | ||
fa9add64 HD |
273 | static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page, |
274 | struct zone *zone) | |
66e1707b | 275 | { |
925b7673 | 276 | return &zone->lruvec; |
66e1707b BS |
277 | } |
278 | ||
e42d9d5d WF |
279 | static inline struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page) |
280 | { | |
281 | return NULL; | |
282 | } | |
283 | ||
a433658c KM |
284 | static inline struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) |
285 | { | |
286 | return NULL; | |
287 | } | |
288 | ||
587af308 | 289 | static inline bool mm_match_cgroup(struct mm_struct *mm, |
c0ff4b85 | 290 | struct mem_cgroup *memcg) |
bed7161a | 291 | { |
587af308 | 292 | return true; |
bed7161a BS |
293 | } |
294 | ||
4c4a2214 | 295 | static inline int task_in_mem_cgroup(struct task_struct *task, |
c0ff4b85 | 296 | const struct mem_cgroup *memcg) |
4c4a2214 DR |
297 | { |
298 | return 1; | |
299 | } | |
300 | ||
c0ff4b85 R |
301 | static inline struct cgroup_subsys_state |
302 | *mem_cgroup_css(struct mem_cgroup *memcg) | |
d324236b WF |
303 | { |
304 | return NULL; | |
305 | } | |
306 | ||
0030f535 | 307 | static inline void |
ac39cf8c | 308 | mem_cgroup_prepare_migration(struct page *page, struct page *newpage, |
0030f535 | 309 | struct mem_cgroup **memcgp) |
ae41be37 | 310 | { |
ae41be37 KH |
311 | } |
312 | ||
c0ff4b85 | 313 | static inline void mem_cgroup_end_migration(struct mem_cgroup *memcg, |
50de1dd9 | 314 | struct page *oldpage, struct page *newpage, bool migration_ok) |
ae41be37 KH |
315 | { |
316 | } | |
317 | ||
5660048c JW |
318 | static inline struct mem_cgroup * |
319 | mem_cgroup_iter(struct mem_cgroup *root, | |
320 | struct mem_cgroup *prev, | |
321 | struct mem_cgroup_reclaim_cookie *reclaim) | |
322 | { | |
323 | return NULL; | |
324 | } | |
325 | ||
326 | static inline void mem_cgroup_iter_break(struct mem_cgroup *root, | |
327 | struct mem_cgroup *prev) | |
328 | { | |
329 | } | |
330 | ||
f8d66542 HT |
331 | static inline bool mem_cgroup_disabled(void) |
332 | { | |
333 | return true; | |
334 | } | |
a636b327 | 335 | |
14797e23 | 336 | static inline int |
c56d5c7d | 337 | mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec) |
14797e23 KM |
338 | { |
339 | return 1; | |
340 | } | |
341 | ||
a3d8e054 | 342 | static inline unsigned long |
4d7dcca2 | 343 | mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru) |
a3d8e054 KM |
344 | { |
345 | return 0; | |
346 | } | |
347 | ||
fa9add64 HD |
348 | static inline void |
349 | mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru, | |
350 | int increment) | |
3e2f41f1 | 351 | { |
3e2f41f1 KM |
352 | } |
353 | ||
e222432b BS |
354 | static inline void |
355 | mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) | |
356 | { | |
357 | } | |
358 | ||
89c06bd5 KH |
359 | static inline void mem_cgroup_begin_update_page_stat(struct page *page, |
360 | bool *locked, unsigned long *flags) | |
361 | { | |
362 | } | |
363 | ||
364 | static inline void mem_cgroup_end_update_page_stat(struct page *page, | |
365 | bool *locked, unsigned long *flags) | |
366 | { | |
367 | } | |
368 | ||
f8a51179 | 369 | static inline void mem_cgroup_oom_enable(void) |
11f34787 JW |
370 | { |
371 | } | |
372 | ||
f8a51179 | 373 | static inline void mem_cgroup_oom_disable(void) |
11f34787 JW |
374 | { |
375 | } | |
376 | ||
f79d6a46 JW |
377 | static inline bool task_in_memcg_oom(struct task_struct *p) |
378 | { | |
379 | return false; | |
380 | } | |
381 | ||
f8a51179 | 382 | static inline bool mem_cgroup_oom_synchronize(bool wait) |
f79d6a46 JW |
383 | { |
384 | return false; | |
385 | } | |
386 | ||
2a7106f2 GT |
387 | static inline void mem_cgroup_inc_page_stat(struct page *page, |
388 | enum mem_cgroup_page_stat_item idx) | |
389 | { | |
390 | } | |
391 | ||
392 | static inline void mem_cgroup_dec_page_stat(struct page *page, | |
393 | enum mem_cgroup_page_stat_item idx) | |
d69b042f BS |
394 | { |
395 | } | |
396 | ||
4e416953 BS |
397 | static inline |
398 | unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order, | |
0ae5e89c YH |
399 | gfp_t gfp_mask, |
400 | unsigned long *total_scanned) | |
4e416953 BS |
401 | { |
402 | return 0; | |
403 | } | |
404 | ||
e94c8a9c | 405 | static inline void mem_cgroup_split_huge_fixup(struct page *head) |
ca3e0214 KH |
406 | { |
407 | } | |
408 | ||
456f998e YH |
409 | static inline |
410 | void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx) | |
411 | { | |
412 | } | |
ab936cbc KH |
413 | static inline void mem_cgroup_replace_page_cache(struct page *oldpage, |
414 | struct page *newpage) | |
415 | { | |
416 | } | |
c255a458 | 417 | #endif /* CONFIG_MEMCG */ |
78fb7466 | 418 | |
c255a458 | 419 | #if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM) |
f212ad7c DN |
420 | static inline bool |
421 | mem_cgroup_bad_page_check(struct page *page) | |
422 | { | |
423 | return false; | |
424 | } | |
425 | ||
426 | static inline void | |
427 | mem_cgroup_print_bad_page(struct page *page) | |
428 | { | |
429 | } | |
430 | #endif | |
431 | ||
e1aab161 GC |
432 | enum { |
433 | UNDER_LIMIT, | |
434 | SOFT_LIMIT, | |
435 | OVER_LIMIT, | |
436 | }; | |
437 | ||
438 | struct sock; | |
cd59085a | 439 | #if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM) |
e1aab161 GC |
440 | void sock_update_memcg(struct sock *sk); |
441 | void sock_release_memcg(struct sock *sk); | |
442 | #else | |
443 | static inline void sock_update_memcg(struct sock *sk) | |
444 | { | |
445 | } | |
446 | static inline void sock_release_memcg(struct sock *sk) | |
447 | { | |
448 | } | |
cd59085a | 449 | #endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */ |
7ae1e1d0 GC |
450 | |
451 | #ifdef CONFIG_MEMCG_KMEM | |
a8964b9b | 452 | extern struct static_key memcg_kmem_enabled_key; |
749c5415 GC |
453 | |
454 | extern int memcg_limited_groups_array_size; | |
ebe945c2 GC |
455 | |
456 | /* | |
457 | * Helper macro to loop through all memcg-specific caches. Callers must still | |
458 | * check if the cache is valid (it is either valid or NULL). | |
459 | * the slab_mutex must be held when looping through those caches | |
460 | */ | |
749c5415 | 461 | #define for_each_memcg_cache_index(_idx) \ |
91c777d8 | 462 | for ((_idx) = 0; (_idx) < memcg_limited_groups_array_size; (_idx)++) |
749c5415 | 463 | |
7ae1e1d0 GC |
464 | static inline bool memcg_kmem_enabled(void) |
465 | { | |
a8964b9b | 466 | return static_key_false(&memcg_kmem_enabled_key); |
7ae1e1d0 GC |
467 | } |
468 | ||
469 | /* | |
470 | * In general, we'll do everything in our power to not incur in any overhead | |
471 | * for non-memcg users for the kmem functions. Not even a function call, if we | |
472 | * can avoid it. | |
473 | * | |
474 | * Therefore, we'll inline all those functions so that in the best case, we'll | |
475 | * see that kmemcg is off for everybody and proceed quickly. If it is on, | |
476 | * we'll still do most of the flag checking inline. We check a lot of | |
477 | * conditions, but because they are pretty simple, they are expected to be | |
478 | * fast. | |
479 | */ | |
480 | bool __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, | |
481 | int order); | |
482 | void __memcg_kmem_commit_charge(struct page *page, | |
483 | struct mem_cgroup *memcg, int order); | |
484 | void __memcg_kmem_uncharge_pages(struct page *page, int order); | |
485 | ||
2633d7a0 | 486 | int memcg_cache_id(struct mem_cgroup *memcg); |
943a451a GC |
487 | int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s, |
488 | struct kmem_cache *root_cache); | |
2633d7a0 GC |
489 | void memcg_release_cache(struct kmem_cache *cachep); |
490 | void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep); | |
491 | ||
55007d84 GC |
492 | int memcg_update_cache_size(struct kmem_cache *s, int num_groups); |
493 | void memcg_update_array_size(int num_groups); | |
d7f25f8a GC |
494 | |
495 | struct kmem_cache * | |
496 | __memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp); | |
497 | ||
1f458cbf | 498 | void mem_cgroup_destroy_cache(struct kmem_cache *cachep); |
7cf27982 | 499 | void kmem_cache_destroy_memcg_children(struct kmem_cache *s); |
1f458cbf | 500 | |
7ae1e1d0 GC |
501 | /** |
502 | * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed. | |
503 | * @gfp: the gfp allocation flags. | |
504 | * @memcg: a pointer to the memcg this was charged against. | |
505 | * @order: allocation order. | |
506 | * | |
507 | * returns true if the memcg where the current task belongs can hold this | |
508 | * allocation. | |
509 | * | |
510 | * We return true automatically if this allocation is not to be accounted to | |
511 | * any memcg. | |
512 | */ | |
513 | static inline bool | |
514 | memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order) | |
515 | { | |
516 | if (!memcg_kmem_enabled()) | |
517 | return true; | |
518 | ||
519 | /* | |
520 | * __GFP_NOFAIL allocations will move on even if charging is not | |
521 | * possible. Therefore we don't even try, and have this allocation | |
522 | * unaccounted. We could in theory charge it with | |
523 | * res_counter_charge_nofail, but we hope those allocations are rare, | |
524 | * and won't be worth the trouble. | |
525 | */ | |
526 | if (!(gfp & __GFP_KMEMCG) || (gfp & __GFP_NOFAIL)) | |
527 | return true; | |
528 | if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD)) | |
529 | return true; | |
530 | ||
531 | /* If the test is dying, just let it go. */ | |
532 | if (unlikely(fatal_signal_pending(current))) | |
533 | return true; | |
534 | ||
535 | return __memcg_kmem_newpage_charge(gfp, memcg, order); | |
536 | } | |
537 | ||
538 | /** | |
539 | * memcg_kmem_uncharge_pages: uncharge pages from memcg | |
540 | * @page: pointer to struct page being freed | |
541 | * @order: allocation order. | |
542 | * | |
543 | * there is no need to specify memcg here, since it is embedded in page_cgroup | |
544 | */ | |
545 | static inline void | |
546 | memcg_kmem_uncharge_pages(struct page *page, int order) | |
547 | { | |
548 | if (memcg_kmem_enabled()) | |
549 | __memcg_kmem_uncharge_pages(page, order); | |
550 | } | |
551 | ||
552 | /** | |
553 | * memcg_kmem_commit_charge: embeds correct memcg in a page | |
554 | * @page: pointer to struct page recently allocated | |
555 | * @memcg: the memcg structure we charged against | |
556 | * @order: allocation order. | |
557 | * | |
558 | * Needs to be called after memcg_kmem_newpage_charge, regardless of success or | |
559 | * failure of the allocation. if @page is NULL, this function will revert the | |
560 | * charges. Otherwise, it will commit the memcg given by @memcg to the | |
561 | * corresponding page_cgroup. | |
562 | */ | |
563 | static inline void | |
564 | memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order) | |
565 | { | |
566 | if (memcg_kmem_enabled() && memcg) | |
567 | __memcg_kmem_commit_charge(page, memcg, order); | |
568 | } | |
569 | ||
d7f25f8a GC |
570 | /** |
571 | * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation | |
572 | * @cachep: the original global kmem cache | |
573 | * @gfp: allocation flags. | |
574 | * | |
575 | * This function assumes that the task allocating, which determines the memcg | |
576 | * in the page allocator, belongs to the same cgroup throughout the whole | |
577 | * process. Misacounting can happen if the task calls memcg_kmem_get_cache() | |
578 | * while belonging to a cgroup, and later on changes. This is considered | |
579 | * acceptable, and should only happen upon task migration. | |
580 | * | |
581 | * Before the cache is created by the memcg core, there is also a possible | |
582 | * imbalance: the task belongs to a memcg, but the cache being allocated from | |
583 | * is the global cache, since the child cache is not yet guaranteed to be | |
584 | * ready. This case is also fine, since in this case the GFP_KMEMCG will not be | |
585 | * passed and the page allocator will not attempt any cgroup accounting. | |
586 | */ | |
587 | static __always_inline struct kmem_cache * | |
588 | memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp) | |
589 | { | |
590 | if (!memcg_kmem_enabled()) | |
591 | return cachep; | |
592 | if (gfp & __GFP_NOFAIL) | |
593 | return cachep; | |
594 | if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD)) | |
595 | return cachep; | |
596 | if (unlikely(fatal_signal_pending(current))) | |
597 | return cachep; | |
598 | ||
599 | return __memcg_kmem_get_cache(cachep, gfp); | |
600 | } | |
7ae1e1d0 | 601 | #else |
749c5415 GC |
602 | #define for_each_memcg_cache_index(_idx) \ |
603 | for (; NULL; ) | |
604 | ||
b9ce5ef4 GC |
605 | static inline bool memcg_kmem_enabled(void) |
606 | { | |
607 | return false; | |
608 | } | |
609 | ||
7ae1e1d0 GC |
610 | static inline bool |
611 | memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order) | |
612 | { | |
613 | return true; | |
614 | } | |
615 | ||
616 | static inline void memcg_kmem_uncharge_pages(struct page *page, int order) | |
617 | { | |
618 | } | |
619 | ||
620 | static inline void | |
621 | memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order) | |
622 | { | |
623 | } | |
2633d7a0 GC |
624 | |
625 | static inline int memcg_cache_id(struct mem_cgroup *memcg) | |
626 | { | |
627 | return -1; | |
628 | } | |
629 | ||
943a451a GC |
630 | static inline int |
631 | memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s, | |
632 | struct kmem_cache *root_cache) | |
2633d7a0 GC |
633 | { |
634 | return 0; | |
635 | } | |
636 | ||
637 | static inline void memcg_release_cache(struct kmem_cache *cachep) | |
638 | { | |
639 | } | |
640 | ||
641 | static inline void memcg_cache_list_add(struct mem_cgroup *memcg, | |
642 | struct kmem_cache *s) | |
643 | { | |
644 | } | |
d7f25f8a GC |
645 | |
646 | static inline struct kmem_cache * | |
647 | memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp) | |
648 | { | |
649 | return cachep; | |
650 | } | |
7cf27982 GC |
651 | |
652 | static inline void kmem_cache_destroy_memcg_children(struct kmem_cache *s) | |
653 | { | |
654 | } | |
7ae1e1d0 | 655 | #endif /* CONFIG_MEMCG_KMEM */ |
8cdea7c0 BS |
656 | #endif /* _LINUX_MEMCONTROL_H */ |
657 |