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