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