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