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