]>
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 | 27 | struct mem_cgroup; |
8697d331 BS |
28 | struct page; |
29 | struct mm_struct; | |
2633d7a0 | 30 | struct kmem_cache; |
78fb7466 | 31 | |
68b4876d SZ |
32 | /* |
33 | * The corresponding mem_cgroup_stat_names is defined in mm/memcontrol.c, | |
34 | * These two lists should keep in accord with each other. | |
35 | */ | |
36 | enum mem_cgroup_stat_index { | |
37 | /* | |
38 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. | |
39 | */ | |
40 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ | |
41 | MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */ | |
42 | MEM_CGROUP_STAT_RSS_HUGE, /* # of pages charged as anon huge */ | |
43 | MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */ | |
c4843a75 | 44 | MEM_CGROUP_STAT_DIRTY, /* # of dirty pages in page cache */ |
3ea67d06 | 45 | MEM_CGROUP_STAT_WRITEBACK, /* # of pages under writeback */ |
68b4876d SZ |
46 | MEM_CGROUP_STAT_SWAP, /* # of pages, swapped out */ |
47 | MEM_CGROUP_STAT_NSTATS, | |
2a7106f2 GT |
48 | }; |
49 | ||
5660048c JW |
50 | struct mem_cgroup_reclaim_cookie { |
51 | struct zone *zone; | |
52 | int priority; | |
53 | unsigned int generation; | |
54 | }; | |
55 | ||
241994ed JW |
56 | enum mem_cgroup_events_index { |
57 | MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */ | |
58 | MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */ | |
59 | MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */ | |
60 | MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */ | |
61 | MEM_CGROUP_EVENTS_NSTATS, | |
62 | /* default hierarchy events */ | |
63 | MEMCG_LOW = MEM_CGROUP_EVENTS_NSTATS, | |
64 | MEMCG_HIGH, | |
65 | MEMCG_MAX, | |
66 | MEMCG_OOM, | |
67 | MEMCG_NR_EVENTS, | |
68 | }; | |
69 | ||
c255a458 | 70 | #ifdef CONFIG_MEMCG |
241994ed JW |
71 | void mem_cgroup_events(struct mem_cgroup *memcg, |
72 | enum mem_cgroup_events_index idx, | |
73 | unsigned int nr); | |
74 | ||
75 | bool mem_cgroup_low(struct mem_cgroup *root, struct mem_cgroup *memcg); | |
76 | ||
00501b53 JW |
77 | int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm, |
78 | gfp_t gfp_mask, struct mem_cgroup **memcgp); | |
79 | void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg, | |
80 | bool lrucare); | |
81 | void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg); | |
0a31bc97 | 82 | void mem_cgroup_uncharge(struct page *page); |
747db954 | 83 | void mem_cgroup_uncharge_list(struct list_head *page_list); |
569b846d | 84 | |
0a31bc97 JW |
85 | void mem_cgroup_migrate(struct page *oldpage, struct page *newpage, |
86 | bool lrucare); | |
569b846d | 87 | |
0a31bc97 JW |
88 | struct lruvec *mem_cgroup_zone_lruvec(struct zone *, struct mem_cgroup *); |
89 | struct lruvec *mem_cgroup_page_lruvec(struct page *, struct zone *); | |
c9b0ed51 | 90 | |
2314b42d JW |
91 | bool mem_cgroup_is_descendant(struct mem_cgroup *memcg, |
92 | struct mem_cgroup *root); | |
93 | bool task_in_mem_cgroup(struct task_struct *task, struct mem_cgroup *memcg); | |
3062fc67 | 94 | |
e42d9d5d | 95 | extern struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page); |
cf475ad2 BS |
96 | extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p); |
97 | ||
e1aab161 | 98 | extern struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg); |
182446d0 | 99 | extern struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css); |
e1aab161 | 100 | |
2314b42d JW |
101 | static inline bool mm_match_cgroup(struct mm_struct *mm, |
102 | struct mem_cgroup *memcg) | |
2e4d4091 | 103 | { |
587af308 | 104 | struct mem_cgroup *task_memcg; |
413918bb | 105 | bool match = false; |
c3ac9a8a | 106 | |
2e4d4091 | 107 | rcu_read_lock(); |
587af308 | 108 | task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
413918bb | 109 | if (task_memcg) |
2314b42d | 110 | match = mem_cgroup_is_descendant(task_memcg, memcg); |
2e4d4091 | 111 | rcu_read_unlock(); |
c3ac9a8a | 112 | return match; |
2e4d4091 | 113 | } |
8a9f3ccd | 114 | |
c0ff4b85 | 115 | extern struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg); |
d324236b | 116 | |
694fbc0f AM |
117 | struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *, |
118 | struct mem_cgroup *, | |
119 | struct mem_cgroup_reclaim_cookie *); | |
5660048c JW |
120 | void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *); |
121 | ||
58ae83db KH |
122 | /* |
123 | * For memory reclaim. | |
124 | */ | |
c56d5c7d | 125 | int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec); |
90cbc250 | 126 | bool mem_cgroup_lruvec_online(struct lruvec *lruvec); |
889976db | 127 | int mem_cgroup_select_victim_node(struct mem_cgroup *memcg); |
4d7dcca2 | 128 | unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list); |
fa9add64 | 129 | void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int); |
e222432b BS |
130 | extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, |
131 | struct task_struct *p); | |
58ae83db | 132 | |
49426420 | 133 | static inline void mem_cgroup_oom_enable(void) |
519e5247 | 134 | { |
49426420 JW |
135 | WARN_ON(current->memcg_oom.may_oom); |
136 | current->memcg_oom.may_oom = 1; | |
519e5247 JW |
137 | } |
138 | ||
49426420 | 139 | static inline void mem_cgroup_oom_disable(void) |
519e5247 | 140 | { |
49426420 JW |
141 | WARN_ON(!current->memcg_oom.may_oom); |
142 | current->memcg_oom.may_oom = 0; | |
519e5247 JW |
143 | } |
144 | ||
3812c8c8 JW |
145 | static inline bool task_in_memcg_oom(struct task_struct *p) |
146 | { | |
49426420 | 147 | return p->memcg_oom.memcg; |
3812c8c8 JW |
148 | } |
149 | ||
49426420 | 150 | bool mem_cgroup_oom_synchronize(bool wait); |
3812c8c8 | 151 | |
c255a458 | 152 | #ifdef CONFIG_MEMCG_SWAP |
c077719b KH |
153 | extern int do_swap_account; |
154 | #endif | |
f8d66542 HT |
155 | |
156 | static inline bool mem_cgroup_disabled(void) | |
157 | { | |
073219e9 | 158 | if (memory_cgrp_subsys.disabled) |
f8d66542 HT |
159 | return true; |
160 | return false; | |
161 | } | |
162 | ||
6de22619 | 163 | struct mem_cgroup *mem_cgroup_begin_page_stat(struct page *page); |
d7365e78 JW |
164 | void mem_cgroup_update_page_stat(struct mem_cgroup *memcg, |
165 | enum mem_cgroup_stat_index idx, int val); | |
6de22619 | 166 | void mem_cgroup_end_page_stat(struct mem_cgroup *memcg); |
d7365e78 JW |
167 | |
168 | static inline void mem_cgroup_inc_page_stat(struct mem_cgroup *memcg, | |
68b4876d | 169 | enum mem_cgroup_stat_index idx) |
2a7106f2 | 170 | { |
d7365e78 | 171 | mem_cgroup_update_page_stat(memcg, idx, 1); |
2a7106f2 GT |
172 | } |
173 | ||
d7365e78 | 174 | static inline void mem_cgroup_dec_page_stat(struct mem_cgroup *memcg, |
68b4876d | 175 | enum mem_cgroup_stat_index idx) |
2a7106f2 | 176 | { |
d7365e78 | 177 | mem_cgroup_update_page_stat(memcg, idx, -1); |
2a7106f2 GT |
178 | } |
179 | ||
0608f43d AM |
180 | unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order, |
181 | gfp_t gfp_mask, | |
182 | unsigned long *total_scanned); | |
a63d83f4 | 183 | |
68ae564b DR |
184 | void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx); |
185 | static inline void mem_cgroup_count_vm_event(struct mm_struct *mm, | |
186 | enum vm_event_item idx) | |
187 | { | |
188 | if (mem_cgroup_disabled()) | |
189 | return; | |
190 | __mem_cgroup_count_vm_event(mm, idx); | |
191 | } | |
ca3e0214 | 192 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
e94c8a9c | 193 | void mem_cgroup_split_huge_fixup(struct page *head); |
ca3e0214 KH |
194 | #endif |
195 | ||
c255a458 | 196 | #else /* CONFIG_MEMCG */ |
7a81b88c KH |
197 | struct mem_cgroup; |
198 | ||
241994ed JW |
199 | static inline void mem_cgroup_events(struct mem_cgroup *memcg, |
200 | enum mem_cgroup_events_index idx, | |
201 | unsigned int nr) | |
202 | { | |
203 | } | |
204 | ||
205 | static inline bool mem_cgroup_low(struct mem_cgroup *root, | |
206 | struct mem_cgroup *memcg) | |
207 | { | |
208 | return false; | |
209 | } | |
210 | ||
00501b53 JW |
211 | static inline int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm, |
212 | gfp_t gfp_mask, | |
213 | struct mem_cgroup **memcgp) | |
7a81b88c | 214 | { |
00501b53 | 215 | *memcgp = NULL; |
7a81b88c KH |
216 | return 0; |
217 | } | |
218 | ||
00501b53 JW |
219 | static inline void mem_cgroup_commit_charge(struct page *page, |
220 | struct mem_cgroup *memcg, | |
221 | bool lrucare) | |
7a81b88c KH |
222 | { |
223 | } | |
224 | ||
00501b53 JW |
225 | static inline void mem_cgroup_cancel_charge(struct page *page, |
226 | struct mem_cgroup *memcg) | |
7a81b88c KH |
227 | { |
228 | } | |
229 | ||
0a31bc97 | 230 | static inline void mem_cgroup_uncharge(struct page *page) |
569b846d KH |
231 | { |
232 | } | |
233 | ||
747db954 | 234 | static inline void mem_cgroup_uncharge_list(struct list_head *page_list) |
8a9f3ccd BS |
235 | { |
236 | } | |
237 | ||
0a31bc97 JW |
238 | static inline void mem_cgroup_migrate(struct page *oldpage, |
239 | struct page *newpage, | |
240 | bool lrucare) | |
69029cd5 KH |
241 | { |
242 | } | |
243 | ||
925b7673 JW |
244 | static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone, |
245 | struct mem_cgroup *memcg) | |
08e552c6 | 246 | { |
925b7673 | 247 | return &zone->lruvec; |
08e552c6 KH |
248 | } |
249 | ||
fa9add64 HD |
250 | static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page, |
251 | struct zone *zone) | |
66e1707b | 252 | { |
925b7673 | 253 | return &zone->lruvec; |
66e1707b BS |
254 | } |
255 | ||
e42d9d5d WF |
256 | static inline struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page) |
257 | { | |
258 | return NULL; | |
259 | } | |
260 | ||
587af308 | 261 | static inline bool mm_match_cgroup(struct mm_struct *mm, |
c0ff4b85 | 262 | struct mem_cgroup *memcg) |
bed7161a | 263 | { |
587af308 | 264 | return true; |
bed7161a BS |
265 | } |
266 | ||
ffbdccf5 DR |
267 | static inline bool task_in_mem_cgroup(struct task_struct *task, |
268 | const struct mem_cgroup *memcg) | |
4c4a2214 | 269 | { |
ffbdccf5 | 270 | return true; |
4c4a2214 DR |
271 | } |
272 | ||
c0ff4b85 R |
273 | static inline struct cgroup_subsys_state |
274 | *mem_cgroup_css(struct mem_cgroup *memcg) | |
d324236b WF |
275 | { |
276 | return NULL; | |
277 | } | |
278 | ||
5660048c JW |
279 | static inline struct mem_cgroup * |
280 | mem_cgroup_iter(struct mem_cgroup *root, | |
281 | struct mem_cgroup *prev, | |
282 | struct mem_cgroup_reclaim_cookie *reclaim) | |
283 | { | |
284 | return NULL; | |
285 | } | |
286 | ||
287 | static inline void mem_cgroup_iter_break(struct mem_cgroup *root, | |
288 | struct mem_cgroup *prev) | |
289 | { | |
290 | } | |
291 | ||
f8d66542 HT |
292 | static inline bool mem_cgroup_disabled(void) |
293 | { | |
294 | return true; | |
295 | } | |
a636b327 | 296 | |
14797e23 | 297 | static inline int |
c56d5c7d | 298 | mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec) |
14797e23 KM |
299 | { |
300 | return 1; | |
301 | } | |
302 | ||
90cbc250 VD |
303 | static inline bool mem_cgroup_lruvec_online(struct lruvec *lruvec) |
304 | { | |
305 | return true; | |
306 | } | |
307 | ||
a3d8e054 | 308 | static inline unsigned long |
4d7dcca2 | 309 | mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru) |
a3d8e054 KM |
310 | { |
311 | return 0; | |
312 | } | |
313 | ||
fa9add64 HD |
314 | static inline void |
315 | mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru, | |
316 | int increment) | |
3e2f41f1 | 317 | { |
3e2f41f1 KM |
318 | } |
319 | ||
e222432b BS |
320 | static inline void |
321 | mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) | |
322 | { | |
323 | } | |
324 | ||
6de22619 | 325 | static inline struct mem_cgroup *mem_cgroup_begin_page_stat(struct page *page) |
89c06bd5 | 326 | { |
d7365e78 | 327 | return NULL; |
89c06bd5 KH |
328 | } |
329 | ||
6de22619 | 330 | static inline void mem_cgroup_end_page_stat(struct mem_cgroup *memcg) |
89c06bd5 KH |
331 | { |
332 | } | |
333 | ||
49426420 | 334 | static inline void mem_cgroup_oom_enable(void) |
519e5247 JW |
335 | { |
336 | } | |
337 | ||
49426420 | 338 | static inline void mem_cgroup_oom_disable(void) |
519e5247 JW |
339 | { |
340 | } | |
341 | ||
3812c8c8 JW |
342 | static inline bool task_in_memcg_oom(struct task_struct *p) |
343 | { | |
344 | return false; | |
345 | } | |
346 | ||
49426420 | 347 | static inline bool mem_cgroup_oom_synchronize(bool wait) |
3812c8c8 JW |
348 | { |
349 | return false; | |
350 | } | |
351 | ||
d7365e78 | 352 | static inline void mem_cgroup_inc_page_stat(struct mem_cgroup *memcg, |
68b4876d | 353 | enum mem_cgroup_stat_index idx) |
2a7106f2 GT |
354 | { |
355 | } | |
356 | ||
d7365e78 | 357 | static inline void mem_cgroup_dec_page_stat(struct mem_cgroup *memcg, |
68b4876d | 358 | enum mem_cgroup_stat_index idx) |
d69b042f BS |
359 | { |
360 | } | |
361 | ||
4e416953 | 362 | static inline |
0608f43d AM |
363 | unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order, |
364 | gfp_t gfp_mask, | |
365 | unsigned long *total_scanned) | |
4e416953 | 366 | { |
0608f43d | 367 | return 0; |
4e416953 BS |
368 | } |
369 | ||
e94c8a9c | 370 | static inline void mem_cgroup_split_huge_fixup(struct page *head) |
ca3e0214 KH |
371 | { |
372 | } | |
373 | ||
456f998e YH |
374 | static inline |
375 | void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx) | |
376 | { | |
377 | } | |
c255a458 | 378 | #endif /* CONFIG_MEMCG */ |
78fb7466 | 379 | |
e1aab161 GC |
380 | enum { |
381 | UNDER_LIMIT, | |
382 | SOFT_LIMIT, | |
383 | OVER_LIMIT, | |
384 | }; | |
385 | ||
386 | struct sock; | |
cd59085a | 387 | #if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM) |
e1aab161 GC |
388 | void sock_update_memcg(struct sock *sk); |
389 | void sock_release_memcg(struct sock *sk); | |
390 | #else | |
391 | static inline void sock_update_memcg(struct sock *sk) | |
392 | { | |
393 | } | |
394 | static inline void sock_release_memcg(struct sock *sk) | |
395 | { | |
396 | } | |
cd59085a | 397 | #endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */ |
7ae1e1d0 GC |
398 | |
399 | #ifdef CONFIG_MEMCG_KMEM | |
a8964b9b | 400 | extern struct static_key memcg_kmem_enabled_key; |
749c5415 | 401 | |
dbcf73e2 | 402 | extern int memcg_nr_cache_ids; |
05257a1a VD |
403 | extern void memcg_get_cache_ids(void); |
404 | extern void memcg_put_cache_ids(void); | |
ebe945c2 GC |
405 | |
406 | /* | |
407 | * Helper macro to loop through all memcg-specific caches. Callers must still | |
408 | * check if the cache is valid (it is either valid or NULL). | |
409 | * the slab_mutex must be held when looping through those caches | |
410 | */ | |
749c5415 | 411 | #define for_each_memcg_cache_index(_idx) \ |
dbcf73e2 | 412 | for ((_idx) = 0; (_idx) < memcg_nr_cache_ids; (_idx)++) |
749c5415 | 413 | |
7ae1e1d0 GC |
414 | static inline bool memcg_kmem_enabled(void) |
415 | { | |
a8964b9b | 416 | return static_key_false(&memcg_kmem_enabled_key); |
7ae1e1d0 GC |
417 | } |
418 | ||
cb731d6c VD |
419 | bool memcg_kmem_is_active(struct mem_cgroup *memcg); |
420 | ||
7ae1e1d0 GC |
421 | /* |
422 | * In general, we'll do everything in our power to not incur in any overhead | |
423 | * for non-memcg users for the kmem functions. Not even a function call, if we | |
424 | * can avoid it. | |
425 | * | |
426 | * Therefore, we'll inline all those functions so that in the best case, we'll | |
427 | * see that kmemcg is off for everybody and proceed quickly. If it is on, | |
428 | * we'll still do most of the flag checking inline. We check a lot of | |
429 | * conditions, but because they are pretty simple, they are expected to be | |
430 | * fast. | |
431 | */ | |
432 | bool __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, | |
433 | int order); | |
434 | void __memcg_kmem_commit_charge(struct page *page, | |
435 | struct mem_cgroup *memcg, int order); | |
436 | void __memcg_kmem_uncharge_pages(struct page *page, int order); | |
437 | ||
2633d7a0 | 438 | int memcg_cache_id(struct mem_cgroup *memcg); |
5722d094 | 439 | |
8135be5a VD |
440 | struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep); |
441 | void __memcg_kmem_put_cache(struct kmem_cache *cachep); | |
d7f25f8a | 442 | |
60d3fd32 VD |
443 | struct mem_cgroup *__mem_cgroup_from_kmem(void *ptr); |
444 | ||
dbf22eb6 VD |
445 | int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, |
446 | unsigned long nr_pages); | |
447 | void memcg_uncharge_kmem(struct mem_cgroup *memcg, unsigned long nr_pages); | |
5dfb4175 | 448 | |
7ae1e1d0 GC |
449 | /** |
450 | * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed. | |
451 | * @gfp: the gfp allocation flags. | |
452 | * @memcg: a pointer to the memcg this was charged against. | |
453 | * @order: allocation order. | |
454 | * | |
455 | * returns true if the memcg where the current task belongs can hold this | |
456 | * allocation. | |
457 | * | |
458 | * We return true automatically if this allocation is not to be accounted to | |
459 | * any memcg. | |
460 | */ | |
461 | static inline bool | |
462 | memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order) | |
463 | { | |
464 | if (!memcg_kmem_enabled()) | |
465 | return true; | |
466 | ||
467 | /* | |
468 | * __GFP_NOFAIL allocations will move on even if charging is not | |
469 | * possible. Therefore we don't even try, and have this allocation | |
3e32cb2e JW |
470 | * unaccounted. We could in theory charge it forcibly, but we hope |
471 | * those allocations are rare, and won't be worth the trouble. | |
7ae1e1d0 | 472 | */ |
52383431 | 473 | if (gfp & __GFP_NOFAIL) |
7ae1e1d0 GC |
474 | return true; |
475 | if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD)) | |
476 | return true; | |
477 | ||
478 | /* If the test is dying, just let it go. */ | |
479 | if (unlikely(fatal_signal_pending(current))) | |
480 | return true; | |
481 | ||
482 | return __memcg_kmem_newpage_charge(gfp, memcg, order); | |
483 | } | |
484 | ||
485 | /** | |
486 | * memcg_kmem_uncharge_pages: uncharge pages from memcg | |
487 | * @page: pointer to struct page being freed | |
488 | * @order: allocation order. | |
7ae1e1d0 GC |
489 | */ |
490 | static inline void | |
491 | memcg_kmem_uncharge_pages(struct page *page, int order) | |
492 | { | |
493 | if (memcg_kmem_enabled()) | |
494 | __memcg_kmem_uncharge_pages(page, order); | |
495 | } | |
496 | ||
497 | /** | |
498 | * memcg_kmem_commit_charge: embeds correct memcg in a page | |
499 | * @page: pointer to struct page recently allocated | |
500 | * @memcg: the memcg structure we charged against | |
501 | * @order: allocation order. | |
502 | * | |
503 | * Needs to be called after memcg_kmem_newpage_charge, regardless of success or | |
504 | * failure of the allocation. if @page is NULL, this function will revert the | |
1306a85a | 505 | * charges. Otherwise, it will commit @page to @memcg. |
7ae1e1d0 GC |
506 | */ |
507 | static inline void | |
508 | memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order) | |
509 | { | |
510 | if (memcg_kmem_enabled() && memcg) | |
511 | __memcg_kmem_commit_charge(page, memcg, order); | |
512 | } | |
513 | ||
d7f25f8a GC |
514 | /** |
515 | * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation | |
516 | * @cachep: the original global kmem cache | |
517 | * @gfp: allocation flags. | |
518 | * | |
5dfb4175 | 519 | * All memory allocated from a per-memcg cache is charged to the owner memcg. |
d7f25f8a GC |
520 | */ |
521 | static __always_inline struct kmem_cache * | |
522 | memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp) | |
523 | { | |
524 | if (!memcg_kmem_enabled()) | |
525 | return cachep; | |
526 | if (gfp & __GFP_NOFAIL) | |
527 | return cachep; | |
528 | if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD)) | |
529 | return cachep; | |
530 | if (unlikely(fatal_signal_pending(current))) | |
531 | return cachep; | |
532 | ||
056b7cce | 533 | return __memcg_kmem_get_cache(cachep); |
d7f25f8a | 534 | } |
8135be5a VD |
535 | |
536 | static __always_inline void memcg_kmem_put_cache(struct kmem_cache *cachep) | |
537 | { | |
538 | if (memcg_kmem_enabled()) | |
539 | __memcg_kmem_put_cache(cachep); | |
540 | } | |
60d3fd32 VD |
541 | |
542 | static __always_inline struct mem_cgroup *mem_cgroup_from_kmem(void *ptr) | |
543 | { | |
544 | if (!memcg_kmem_enabled()) | |
545 | return NULL; | |
546 | return __mem_cgroup_from_kmem(ptr); | |
547 | } | |
7ae1e1d0 | 548 | #else |
749c5415 GC |
549 | #define for_each_memcg_cache_index(_idx) \ |
550 | for (; NULL; ) | |
551 | ||
b9ce5ef4 GC |
552 | static inline bool memcg_kmem_enabled(void) |
553 | { | |
554 | return false; | |
555 | } | |
556 | ||
cb731d6c VD |
557 | static inline bool memcg_kmem_is_active(struct mem_cgroup *memcg) |
558 | { | |
559 | return false; | |
560 | } | |
561 | ||
7ae1e1d0 GC |
562 | static inline bool |
563 | memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order) | |
564 | { | |
565 | return true; | |
566 | } | |
567 | ||
568 | static inline void memcg_kmem_uncharge_pages(struct page *page, int order) | |
569 | { | |
570 | } | |
571 | ||
572 | static inline void | |
573 | memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order) | |
574 | { | |
575 | } | |
2633d7a0 GC |
576 | |
577 | static inline int memcg_cache_id(struct mem_cgroup *memcg) | |
578 | { | |
579 | return -1; | |
580 | } | |
581 | ||
05257a1a VD |
582 | static inline void memcg_get_cache_ids(void) |
583 | { | |
584 | } | |
585 | ||
586 | static inline void memcg_put_cache_ids(void) | |
587 | { | |
588 | } | |
589 | ||
d7f25f8a GC |
590 | static inline struct kmem_cache * |
591 | memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp) | |
592 | { | |
593 | return cachep; | |
594 | } | |
8135be5a VD |
595 | |
596 | static inline void memcg_kmem_put_cache(struct kmem_cache *cachep) | |
597 | { | |
598 | } | |
60d3fd32 VD |
599 | |
600 | static inline struct mem_cgroup *mem_cgroup_from_kmem(void *ptr) | |
601 | { | |
602 | return NULL; | |
603 | } | |
7ae1e1d0 | 604 | #endif /* CONFIG_MEMCG_KMEM */ |
8cdea7c0 BS |
605 | #endif /* _LINUX_MEMCONTROL_H */ |
606 |