]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - include/linux/memcontrol.h
sl[au]b: always get the cache from its page in kmem_cache_free()
[mirror_ubuntu-zesty-kernel.git] / include / linux / memcontrol.h
CommitLineData
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
27struct mem_cgroup;
28struct page_cgroup;
8697d331
BS
29struct page;
30struct mm_struct;
2633d7a0 31struct kmem_cache;
78fb7466 32
2a7106f2
GT
33/* Stats that can be updated by kernel. */
34enum mem_cgroup_page_stat_item {
35 MEMCG_NR_FILE_MAPPED, /* # of pages charged as file rss */
36};
37
5660048c
JW
38struct mem_cgroup_reclaim_cookie {
39 struct zone *zone;
40 int priority;
41 unsigned int generation;
42};
43
c255a458 44#ifdef CONFIG_MEMCG
2c26fdd7
KH
45/*
46 * All "charge" functions with gfp_mask should use GFP_KERNEL or
47 * (gfp_mask & GFP_RECLAIM_MASK). In current implementatin, memcg doesn't
48 * alloc memory but reclaims memory from all available zones. So, "where I want
49 * memory from" bits of gfp_mask has no meaning. So any bits of that field is
50 * available but adding a rule is better. charge functions' gfp_mask should
51 * be set to GFP_KERNEL or gfp_mask & GFP_RECLAIM_MASK for avoiding ambiguous
52 * codes.
53 * (Of course, if memcg does memory allocation in future, GFP_KERNEL is sane.)
54 */
78fb7466 55
7a81b88c 56extern int mem_cgroup_newpage_charge(struct page *page, struct mm_struct *mm,
e1a1cd59 57 gfp_t gfp_mask);
7a81b88c 58/* for swap handling */
8c7c6e34 59extern int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
72835c86 60 struct page *page, gfp_t mask, struct mem_cgroup **memcgp);
7a81b88c 61extern void mem_cgroup_commit_charge_swapin(struct page *page,
72835c86
JW
62 struct mem_cgroup *memcg);
63extern void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg);
7a81b88c 64
8289546e
HD
65extern int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
66 gfp_t gfp_mask);
925b7673
JW
67
68struct lruvec *mem_cgroup_zone_lruvec(struct zone *, struct mem_cgroup *);
fa9add64 69struct lruvec *mem_cgroup_page_lruvec(struct page *, struct zone *);
569b846d
KH
70
71/* For coalescing uncharge for reducing memcg' overhead*/
72extern void mem_cgroup_uncharge_start(void);
73extern void mem_cgroup_uncharge_end(void);
74
3c541e14 75extern void mem_cgroup_uncharge_page(struct page *page);
69029cd5 76extern void mem_cgroup_uncharge_cache_page(struct page *page);
c9b0ed51 77
c3ac9a8a
JW
78bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
79 struct mem_cgroup *memcg);
c0ff4b85 80int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg);
3062fc67 81
e42d9d5d 82extern struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page);
cf475ad2 83extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
a433658c 84extern struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm);
cf475ad2 85
e1aab161 86extern struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
d1a4c0b3 87extern struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont);
e1aab161 88
2e4d4091 89static inline
587af308 90bool mm_match_cgroup(const struct mm_struct *mm, const struct mem_cgroup *memcg)
2e4d4091 91{
587af308
JW
92 struct mem_cgroup *task_memcg;
93 bool match;
c3ac9a8a 94
2e4d4091 95 rcu_read_lock();
587af308
JW
96 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
97 match = __mem_cgroup_same_or_subtree(memcg, task_memcg);
2e4d4091 98 rcu_read_unlock();
c3ac9a8a 99 return match;
2e4d4091 100}
8a9f3ccd 101
c0ff4b85 102extern struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg);
d324236b 103
0030f535
JW
104extern void
105mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
106 struct mem_cgroup **memcgp);
c0ff4b85 107extern void mem_cgroup_end_migration(struct mem_cgroup *memcg,
50de1dd9 108 struct page *oldpage, struct page *newpage, bool migration_ok);
ae41be37 109
5660048c
JW
110struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
111 struct mem_cgroup *,
112 struct mem_cgroup_reclaim_cookie *);
113void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
114
58ae83db
KH
115/*
116 * For memory reclaim.
117 */
c56d5c7d
KK
118int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec);
119int mem_cgroup_inactive_file_is_low(struct lruvec *lruvec);
889976db 120int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
4d7dcca2 121unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list);
fa9add64 122void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int);
e222432b
BS
123extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
124 struct task_struct *p);
ab936cbc
KH
125extern void mem_cgroup_replace_page_cache(struct page *oldpage,
126 struct page *newpage);
58ae83db 127
c255a458 128#ifdef CONFIG_MEMCG_SWAP
c077719b
KH
129extern int do_swap_account;
130#endif
f8d66542
HT
131
132static inline bool mem_cgroup_disabled(void)
133{
134 if (mem_cgroup_subsys.disabled)
135 return true;
136 return false;
137}
138
89c06bd5
KH
139void __mem_cgroup_begin_update_page_stat(struct page *page, bool *locked,
140 unsigned long *flags);
141
4331f7d3
KH
142extern atomic_t memcg_moving;
143
89c06bd5
KH
144static inline void mem_cgroup_begin_update_page_stat(struct page *page,
145 bool *locked, unsigned long *flags)
146{
147 if (mem_cgroup_disabled())
148 return;
149 rcu_read_lock();
150 *locked = false;
4331f7d3
KH
151 if (atomic_read(&memcg_moving))
152 __mem_cgroup_begin_update_page_stat(page, locked, flags);
89c06bd5
KH
153}
154
155void __mem_cgroup_end_update_page_stat(struct page *page,
156 unsigned long *flags);
157static inline void mem_cgroup_end_update_page_stat(struct page *page,
158 bool *locked, unsigned long *flags)
159{
160 if (mem_cgroup_disabled())
161 return;
162 if (*locked)
163 __mem_cgroup_end_update_page_stat(page, flags);
164 rcu_read_unlock();
165}
166
2a7106f2
GT
167void mem_cgroup_update_page_stat(struct page *page,
168 enum mem_cgroup_page_stat_item idx,
169 int val);
170
171static inline void mem_cgroup_inc_page_stat(struct page *page,
172 enum mem_cgroup_page_stat_item idx)
173{
174 mem_cgroup_update_page_stat(page, idx, 1);
175}
176
177static inline void mem_cgroup_dec_page_stat(struct page *page,
178 enum mem_cgroup_page_stat_item idx)
179{
180 mem_cgroup_update_page_stat(page, idx, -1);
181}
182
4e416953 183unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
0ae5e89c
YH
184 gfp_t gfp_mask,
185 unsigned long *total_scanned);
a63d83f4 186
68ae564b
DR
187void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx);
188static 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 196void mem_cgroup_split_huge_fixup(struct page *head);
ca3e0214
KH
197#endif
198
f212ad7c
DN
199#ifdef CONFIG_DEBUG_VM
200bool mem_cgroup_bad_page_check(struct page *page);
201void mem_cgroup_print_bad_page(struct page *page);
202#endif
c255a458 203#else /* CONFIG_MEMCG */
7a81b88c
KH
204struct mem_cgroup;
205
206static inline int mem_cgroup_newpage_charge(struct page *page,
8289546e 207 struct mm_struct *mm, gfp_t gfp_mask)
8a9f3ccd
BS
208{
209 return 0;
210}
211
8289546e
HD
212static inline int mem_cgroup_cache_charge(struct page *page,
213 struct mm_struct *mm, gfp_t gfp_mask)
8a9f3ccd 214{
8289546e 215 return 0;
8a9f3ccd
BS
216}
217
8c7c6e34 218static inline int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
72835c86 219 struct page *page, gfp_t gfp_mask, struct mem_cgroup **memcgp)
7a81b88c
KH
220{
221 return 0;
222}
223
224static inline void mem_cgroup_commit_charge_swapin(struct page *page,
72835c86 225 struct mem_cgroup *memcg)
7a81b88c
KH
226{
227}
228
72835c86 229static inline void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
7a81b88c
KH
230{
231}
232
569b846d
KH
233static inline void mem_cgroup_uncharge_start(void)
234{
235}
236
237static inline void mem_cgroup_uncharge_end(void)
238{
239}
240
8a9f3ccd
BS
241static inline void mem_cgroup_uncharge_page(struct page *page)
242{
243}
244
69029cd5
KH
245static inline void mem_cgroup_uncharge_cache_page(struct page *page)
246{
247}
248
925b7673
JW
249static 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
255static 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
261static inline struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
262{
263 return NULL;
264}
265
a433658c
KM
266static inline struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
267{
268 return NULL;
269}
270
587af308 271static inline bool mm_match_cgroup(struct mm_struct *mm,
c0ff4b85 272 struct mem_cgroup *memcg)
bed7161a 273{
587af308 274 return true;
bed7161a
BS
275}
276
4c4a2214 277static inline int task_in_mem_cgroup(struct task_struct *task,
c0ff4b85 278 const struct mem_cgroup *memcg)
4c4a2214
DR
279{
280 return 1;
281}
282
c0ff4b85
R
283static inline struct cgroup_subsys_state
284 *mem_cgroup_css(struct mem_cgroup *memcg)
d324236b
WF
285{
286 return NULL;
287}
288
0030f535 289static inline void
ac39cf8c 290mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
0030f535 291 struct mem_cgroup **memcgp)
ae41be37 292{
ae41be37
KH
293}
294
c0ff4b85 295static inline void mem_cgroup_end_migration(struct mem_cgroup *memcg,
50de1dd9 296 struct page *oldpage, struct page *newpage, bool migration_ok)
ae41be37
KH
297{
298}
299
5660048c
JW
300static inline struct mem_cgroup *
301mem_cgroup_iter(struct mem_cgroup *root,
302 struct mem_cgroup *prev,
303 struct mem_cgroup_reclaim_cookie *reclaim)
304{
305 return NULL;
306}
307
308static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
309 struct mem_cgroup *prev)
310{
311}
312
f8d66542
HT
313static inline bool mem_cgroup_disabled(void)
314{
315 return true;
316}
a636b327 317
14797e23 318static inline int
c56d5c7d 319mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
14797e23
KM
320{
321 return 1;
322}
323
56e49d21 324static inline int
c56d5c7d 325mem_cgroup_inactive_file_is_low(struct lruvec *lruvec)
56e49d21
RR
326{
327 return 1;
328}
329
a3d8e054 330static inline unsigned long
4d7dcca2 331mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
a3d8e054
KM
332{
333 return 0;
334}
335
fa9add64
HD
336static inline void
337mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
338 int increment)
3e2f41f1 339{
3e2f41f1
KM
340}
341
e222432b
BS
342static inline void
343mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
344{
345}
346
89c06bd5
KH
347static inline void mem_cgroup_begin_update_page_stat(struct page *page,
348 bool *locked, unsigned long *flags)
349{
350}
351
352static inline void mem_cgroup_end_update_page_stat(struct page *page,
353 bool *locked, unsigned long *flags)
354{
355}
356
2a7106f2
GT
357static inline void mem_cgroup_inc_page_stat(struct page *page,
358 enum mem_cgroup_page_stat_item idx)
359{
360}
361
362static inline void mem_cgroup_dec_page_stat(struct page *page,
363 enum mem_cgroup_page_stat_item idx)
d69b042f
BS
364{
365}
366
4e416953
BS
367static inline
368unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
0ae5e89c
YH
369 gfp_t gfp_mask,
370 unsigned long *total_scanned)
4e416953
BS
371{
372 return 0;
373}
374
e94c8a9c 375static inline void mem_cgroup_split_huge_fixup(struct page *head)
ca3e0214
KH
376{
377}
378
456f998e
YH
379static inline
380void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
381{
382}
ab936cbc
KH
383static inline void mem_cgroup_replace_page_cache(struct page *oldpage,
384 struct page *newpage)
385{
386}
c255a458 387#endif /* CONFIG_MEMCG */
78fb7466 388
c255a458 389#if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
f212ad7c
DN
390static inline bool
391mem_cgroup_bad_page_check(struct page *page)
392{
393 return false;
394}
395
396static inline void
397mem_cgroup_print_bad_page(struct page *page)
398{
399}
400#endif
401
e1aab161
GC
402enum {
403 UNDER_LIMIT,
404 SOFT_LIMIT,
405 OVER_LIMIT,
406};
407
408struct sock;
cd59085a 409#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
e1aab161
GC
410void sock_update_memcg(struct sock *sk);
411void sock_release_memcg(struct sock *sk);
412#else
413static inline void sock_update_memcg(struct sock *sk)
414{
415}
416static inline void sock_release_memcg(struct sock *sk)
417{
418}
cd59085a 419#endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
7ae1e1d0
GC
420
421#ifdef CONFIG_MEMCG_KMEM
a8964b9b 422extern struct static_key memcg_kmem_enabled_key;
7ae1e1d0
GC
423static inline bool memcg_kmem_enabled(void)
424{
a8964b9b 425 return static_key_false(&memcg_kmem_enabled_key);
7ae1e1d0
GC
426}
427
428/*
429 * In general, we'll do everything in our power to not incur in any overhead
430 * for non-memcg users for the kmem functions. Not even a function call, if we
431 * can avoid it.
432 *
433 * Therefore, we'll inline all those functions so that in the best case, we'll
434 * see that kmemcg is off for everybody and proceed quickly. If it is on,
435 * we'll still do most of the flag checking inline. We check a lot of
436 * conditions, but because they are pretty simple, they are expected to be
437 * fast.
438 */
439bool __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg,
440 int order);
441void __memcg_kmem_commit_charge(struct page *page,
442 struct mem_cgroup *memcg, int order);
443void __memcg_kmem_uncharge_pages(struct page *page, int order);
444
2633d7a0
GC
445int memcg_cache_id(struct mem_cgroup *memcg);
446int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s);
447void memcg_release_cache(struct kmem_cache *cachep);
448void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep);
449
55007d84
GC
450int memcg_update_cache_size(struct kmem_cache *s, int num_groups);
451void memcg_update_array_size(int num_groups);
d7f25f8a
GC
452
453struct kmem_cache *
454__memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp);
455
7ae1e1d0
GC
456/**
457 * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
458 * @gfp: the gfp allocation flags.
459 * @memcg: a pointer to the memcg this was charged against.
460 * @order: allocation order.
461 *
462 * returns true if the memcg where the current task belongs can hold this
463 * allocation.
464 *
465 * We return true automatically if this allocation is not to be accounted to
466 * any memcg.
467 */
468static inline bool
469memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
470{
471 if (!memcg_kmem_enabled())
472 return true;
473
474 /*
475 * __GFP_NOFAIL allocations will move on even if charging is not
476 * possible. Therefore we don't even try, and have this allocation
477 * unaccounted. We could in theory charge it with
478 * res_counter_charge_nofail, but we hope those allocations are rare,
479 * and won't be worth the trouble.
480 */
481 if (!(gfp & __GFP_KMEMCG) || (gfp & __GFP_NOFAIL))
482 return true;
483 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
484 return true;
485
486 /* If the test is dying, just let it go. */
487 if (unlikely(fatal_signal_pending(current)))
488 return true;
489
490 return __memcg_kmem_newpage_charge(gfp, memcg, order);
491}
492
493/**
494 * memcg_kmem_uncharge_pages: uncharge pages from memcg
495 * @page: pointer to struct page being freed
496 * @order: allocation order.
497 *
498 * there is no need to specify memcg here, since it is embedded in page_cgroup
499 */
500static inline void
501memcg_kmem_uncharge_pages(struct page *page, int order)
502{
503 if (memcg_kmem_enabled())
504 __memcg_kmem_uncharge_pages(page, order);
505}
506
507/**
508 * memcg_kmem_commit_charge: embeds correct memcg in a page
509 * @page: pointer to struct page recently allocated
510 * @memcg: the memcg structure we charged against
511 * @order: allocation order.
512 *
513 * Needs to be called after memcg_kmem_newpage_charge, regardless of success or
514 * failure of the allocation. if @page is NULL, this function will revert the
515 * charges. Otherwise, it will commit the memcg given by @memcg to the
516 * corresponding page_cgroup.
517 */
518static inline void
519memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
520{
521 if (memcg_kmem_enabled() && memcg)
522 __memcg_kmem_commit_charge(page, memcg, order);
523}
524
d7f25f8a
GC
525/**
526 * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation
527 * @cachep: the original global kmem cache
528 * @gfp: allocation flags.
529 *
530 * This function assumes that the task allocating, which determines the memcg
531 * in the page allocator, belongs to the same cgroup throughout the whole
532 * process. Misacounting can happen if the task calls memcg_kmem_get_cache()
533 * while belonging to a cgroup, and later on changes. This is considered
534 * acceptable, and should only happen upon task migration.
535 *
536 * Before the cache is created by the memcg core, there is also a possible
537 * imbalance: the task belongs to a memcg, but the cache being allocated from
538 * is the global cache, since the child cache is not yet guaranteed to be
539 * ready. This case is also fine, since in this case the GFP_KMEMCG will not be
540 * passed and the page allocator will not attempt any cgroup accounting.
541 */
542static __always_inline struct kmem_cache *
543memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
544{
545 if (!memcg_kmem_enabled())
546 return cachep;
547 if (gfp & __GFP_NOFAIL)
548 return cachep;
549 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
550 return cachep;
551 if (unlikely(fatal_signal_pending(current)))
552 return cachep;
553
554 return __memcg_kmem_get_cache(cachep, gfp);
555}
7ae1e1d0 556#else
b9ce5ef4
GC
557static inline bool memcg_kmem_enabled(void)
558{
559 return false;
560}
561
7ae1e1d0
GC
562static inline bool
563memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
564{
565 return true;
566}
567
568static inline void memcg_kmem_uncharge_pages(struct page *page, int order)
569{
570}
571
572static inline void
573memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
574{
575}
2633d7a0
GC
576
577static inline int memcg_cache_id(struct mem_cgroup *memcg)
578{
579 return -1;
580}
581
582static inline int memcg_register_cache(struct mem_cgroup *memcg,
583 struct kmem_cache *s)
584{
585 return 0;
586}
587
588static inline void memcg_release_cache(struct kmem_cache *cachep)
589{
590}
591
592static inline void memcg_cache_list_add(struct mem_cgroup *memcg,
593 struct kmem_cache *s)
594{
595}
d7f25f8a
GC
596
597static inline struct kmem_cache *
598memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
599{
600 return cachep;
601}
7ae1e1d0 602#endif /* CONFIG_MEMCG_KMEM */
8cdea7c0
BS
603#endif /* _LINUX_MEMCONTROL_H */
604