mm/ksm.c: use list_for_each_entry_safe
[deliverable/linux.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>
33398cf2
MH
26#include <linux/page_counter.h>
27#include <linux/vmpressure.h>
28#include <linux/eventfd.h>
29#include <linux/mmzone.h>
30#include <linux/writeback.h>
456f998e 31
78fb7466 32struct mem_cgroup;
8697d331
BS
33struct page;
34struct mm_struct;
2633d7a0 35struct kmem_cache;
78fb7466 36
68b4876d
SZ
37/*
38 * The corresponding mem_cgroup_stat_names is defined in mm/memcontrol.c,
39 * These two lists should keep in accord with each other.
40 */
41enum mem_cgroup_stat_index {
42 /*
43 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
44 */
45 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
46 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
47 MEM_CGROUP_STAT_RSS_HUGE, /* # of pages charged as anon huge */
48 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
c4843a75 49 MEM_CGROUP_STAT_DIRTY, /* # of dirty pages in page cache */
3ea67d06 50 MEM_CGROUP_STAT_WRITEBACK, /* # of pages under writeback */
68b4876d
SZ
51 MEM_CGROUP_STAT_SWAP, /* # of pages, swapped out */
52 MEM_CGROUP_STAT_NSTATS,
2a7106f2
GT
53};
54
5660048c
JW
55struct mem_cgroup_reclaim_cookie {
56 struct zone *zone;
57 int priority;
58 unsigned int generation;
59};
60
241994ed
JW
61enum mem_cgroup_events_index {
62 MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */
63 MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */
64 MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */
65 MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */
66 MEM_CGROUP_EVENTS_NSTATS,
67 /* default hierarchy events */
68 MEMCG_LOW = MEM_CGROUP_EVENTS_NSTATS,
69 MEMCG_HIGH,
70 MEMCG_MAX,
71 MEMCG_OOM,
72 MEMCG_NR_EVENTS,
73};
74
33398cf2
MH
75/*
76 * Per memcg event counter is incremented at every pagein/pageout. With THP,
77 * it will be incremated by the number of pages. This counter is used for
78 * for trigger some periodic events. This is straightforward and better
79 * than using jiffies etc. to handle periodic memcg event.
80 */
81enum mem_cgroup_events_target {
82 MEM_CGROUP_TARGET_THRESH,
83 MEM_CGROUP_TARGET_SOFTLIMIT,
84 MEM_CGROUP_TARGET_NUMAINFO,
85 MEM_CGROUP_NTARGETS,
86};
87
33398cf2
MH
88struct cg_proto {
89 struct page_counter memory_allocated; /* Current allocated memory. */
90 struct percpu_counter sockets_allocated; /* Current number of sockets. */
91 int memory_pressure;
9ee11ba4 92 bool active;
33398cf2 93 long sysctl_mem[3];
33398cf2
MH
94 /*
95 * memcg field is used to find which memcg we belong directly
96 * Each memcg struct can hold more than one cg_proto, so container_of
97 * won't really cut.
98 *
99 * The elegant solution would be having an inverse function to
100 * proto_cgroup in struct proto, but that means polluting the structure
101 * for everybody, instead of just for memcg users.
102 */
103 struct mem_cgroup *memcg;
104};
105
c255a458 106#ifdef CONFIG_MEMCG
33398cf2
MH
107struct mem_cgroup_stat_cpu {
108 long count[MEM_CGROUP_STAT_NSTATS];
109 unsigned long events[MEMCG_NR_EVENTS];
110 unsigned long nr_page_events;
111 unsigned long targets[MEM_CGROUP_NTARGETS];
112};
113
114struct mem_cgroup_reclaim_iter {
115 struct mem_cgroup *position;
116 /* scan generation, increased every round-trip */
117 unsigned int generation;
118};
119
120/*
121 * per-zone information in memory controller.
122 */
123struct mem_cgroup_per_zone {
124 struct lruvec lruvec;
125 unsigned long lru_size[NR_LRU_LISTS];
126
127 struct mem_cgroup_reclaim_iter iter[DEF_PRIORITY + 1];
128
129 struct rb_node tree_node; /* RB tree node */
130 unsigned long usage_in_excess;/* Set to the value by which */
131 /* the soft limit is exceeded*/
132 bool on_tree;
133 struct mem_cgroup *memcg; /* Back pointer, we cannot */
134 /* use container_of */
135};
136
137struct mem_cgroup_per_node {
138 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
139};
140
141struct mem_cgroup_threshold {
142 struct eventfd_ctx *eventfd;
143 unsigned long threshold;
144};
145
146/* For threshold */
147struct mem_cgroup_threshold_ary {
148 /* An array index points to threshold just below or equal to usage. */
149 int current_threshold;
150 /* Size of entries[] */
151 unsigned int size;
152 /* Array of thresholds */
153 struct mem_cgroup_threshold entries[0];
154};
155
156struct mem_cgroup_thresholds {
157 /* Primary thresholds array */
158 struct mem_cgroup_threshold_ary *primary;
159 /*
160 * Spare threshold array.
161 * This is needed to make mem_cgroup_unregister_event() "never fail".
162 * It must be able to store at least primary->size - 1 entries.
163 */
164 struct mem_cgroup_threshold_ary *spare;
165};
166
167/*
168 * The memory controller data structure. The memory controller controls both
169 * page cache and RSS per cgroup. We would eventually like to provide
170 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
171 * to help the administrator determine what knobs to tune.
172 */
173struct mem_cgroup {
174 struct cgroup_subsys_state css;
175
176 /* Accounted resources */
177 struct page_counter memory;
178 struct page_counter memsw;
179 struct page_counter kmem;
180
181 /* Normal memory consumption range */
182 unsigned long low;
183 unsigned long high;
184
185 unsigned long soft_limit;
186
187 /* vmpressure notifications */
188 struct vmpressure vmpressure;
189
190 /* css_online() has been completed */
191 int initialized;
192
193 /*
194 * Should the accounting and control be hierarchical, per subtree?
195 */
196 bool use_hierarchy;
197
198 /* protected by memcg_oom_lock */
199 bool oom_lock;
200 int under_oom;
201
202 int swappiness;
203 /* OOM-Killer disable */
204 int oom_kill_disable;
205
472912a2
TH
206 /* handle for "memory.events" */
207 struct cgroup_file events_file;
208
33398cf2
MH
209 /* protect arrays of thresholds */
210 struct mutex thresholds_lock;
211
212 /* thresholds for memory usage. RCU-protected */
213 struct mem_cgroup_thresholds thresholds;
214
215 /* thresholds for mem+swap usage. RCU-protected */
216 struct mem_cgroup_thresholds memsw_thresholds;
217
218 /* For oom notifier event fd */
219 struct list_head oom_notify;
220
221 /*
222 * Should we move charges of a task when a task is moved into this
223 * mem_cgroup ? And what type of charges should we move ?
224 */
225 unsigned long move_charge_at_immigrate;
226 /*
227 * set > 0 if pages under this cgroup are moving to other cgroup.
228 */
229 atomic_t moving_account;
230 /* taken only while moving_account > 0 */
231 spinlock_t move_lock;
232 struct task_struct *move_lock_task;
233 unsigned long move_lock_flags;
234 /*
235 * percpu counter.
236 */
237 struct mem_cgroup_stat_cpu __percpu *stat;
33398cf2
MH
238
239#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
240 struct cg_proto tcp_mem;
241#endif
242#if defined(CONFIG_MEMCG_KMEM)
243 /* Index in the kmem_cache->memcg_params.memcg_caches array */
244 int kmemcg_id;
245 bool kmem_acct_activated;
246 bool kmem_acct_active;
247#endif
248
249 int last_scanned_node;
250#if MAX_NUMNODES > 1
251 nodemask_t scan_nodes;
252 atomic_t numainfo_events;
253 atomic_t numainfo_updating;
254#endif
255
256#ifdef CONFIG_CGROUP_WRITEBACK
257 struct list_head cgwb_list;
258 struct wb_domain cgwb_domain;
259#endif
260
261 /* List of events which userspace want to receive */
262 struct list_head event_list;
263 spinlock_t event_list_lock;
264
265 struct mem_cgroup_per_node *nodeinfo[0];
266 /* WARNING: nodeinfo must be the last member here */
267};
56161634
TH
268extern struct cgroup_subsys_state *mem_cgroup_root_css;
269
33398cf2
MH
270/**
271 * mem_cgroup_events - count memory events against a cgroup
272 * @memcg: the memory cgroup
273 * @idx: the event index
274 * @nr: the number of events to account for
275 */
276static inline void mem_cgroup_events(struct mem_cgroup *memcg,
241994ed 277 enum mem_cgroup_events_index idx,
33398cf2
MH
278 unsigned int nr)
279{
280 this_cpu_add(memcg->stat->events[idx], nr);
472912a2 281 cgroup_file_notify(&memcg->events_file);
33398cf2 282}
241994ed
JW
283
284bool mem_cgroup_low(struct mem_cgroup *root, struct mem_cgroup *memcg);
285
00501b53
JW
286int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
287 gfp_t gfp_mask, struct mem_cgroup **memcgp);
288void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
289 bool lrucare);
290void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg);
0a31bc97 291void mem_cgroup_uncharge(struct page *page);
747db954 292void mem_cgroup_uncharge_list(struct list_head *page_list);
569b846d 293
45637bab 294void mem_cgroup_replace_page(struct page *oldpage, struct page *newpage);
569b846d 295
0a31bc97
JW
296struct lruvec *mem_cgroup_zone_lruvec(struct zone *, struct mem_cgroup *);
297struct lruvec *mem_cgroup_page_lruvec(struct page *, struct zone *);
c9b0ed51 298
2314b42d 299bool task_in_mem_cgroup(struct task_struct *task, struct mem_cgroup *memcg);
64219994 300struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
64219994 301struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
e993d905 302
33398cf2
MH
303static inline
304struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
305 return css ? container_of(css, struct mem_cgroup, css) : NULL;
306}
307
308struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
309 struct mem_cgroup *,
310 struct mem_cgroup_reclaim_cookie *);
311void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
312
313static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
314 struct mem_cgroup *root)
315{
316 if (root == memcg)
317 return true;
318 if (!root->use_hierarchy)
319 return false;
320 return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
321}
e1aab161 322
2314b42d
JW
323static inline bool mm_match_cgroup(struct mm_struct *mm,
324 struct mem_cgroup *memcg)
2e4d4091 325{
587af308 326 struct mem_cgroup *task_memcg;
413918bb 327 bool match = false;
c3ac9a8a 328
2e4d4091 329 rcu_read_lock();
587af308 330 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
413918bb 331 if (task_memcg)
2314b42d 332 match = mem_cgroup_is_descendant(task_memcg, memcg);
2e4d4091 333 rcu_read_unlock();
c3ac9a8a 334 return match;
2e4d4091 335}
8a9f3ccd 336
64219994 337struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page);
2fc04524 338ino_t page_cgroup_ino(struct page *page);
d324236b 339
33398cf2
MH
340static inline bool mem_cgroup_disabled(void)
341{
fc5ed1e9 342 return !cgroup_subsys_enabled(memory_cgrp_subsys);
33398cf2 343}
5660048c 344
58ae83db
KH
345/*
346 * For memory reclaim.
347 */
889976db 348int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
33398cf2
MH
349
350void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
351 int nr_pages);
352
353static inline bool mem_cgroup_lruvec_online(struct lruvec *lruvec)
354{
355 struct mem_cgroup_per_zone *mz;
356 struct mem_cgroup *memcg;
357
358 if (mem_cgroup_disabled())
359 return true;
360
361 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
362 memcg = mz->memcg;
363
364 return !!(memcg->css.flags & CSS_ONLINE);
365}
366
367static inline
368unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
369{
370 struct mem_cgroup_per_zone *mz;
371
372 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
373 return mz->lru_size[lru];
374}
375
13308ca9 376static inline bool mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
33398cf2
MH
377{
378 unsigned long inactive_ratio;
379 unsigned long inactive;
380 unsigned long active;
381 unsigned long gb;
382
383 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
384 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
385
386 gb = (inactive + active) >> (30 - PAGE_SHIFT);
387 if (gb)
388 inactive_ratio = int_sqrt(10 * gb);
389 else
390 inactive_ratio = 1;
391
392 return inactive * inactive_ratio < active;
393}
394
b23afb93
TH
395void mem_cgroup_handle_over_high(void);
396
64219994
MH
397void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
398 struct task_struct *p);
58ae83db 399
49426420 400static inline void mem_cgroup_oom_enable(void)
519e5247 401{
626ebc41
TH
402 WARN_ON(current->memcg_may_oom);
403 current->memcg_may_oom = 1;
519e5247
JW
404}
405
49426420 406static inline void mem_cgroup_oom_disable(void)
519e5247 407{
626ebc41
TH
408 WARN_ON(!current->memcg_may_oom);
409 current->memcg_may_oom = 0;
519e5247
JW
410}
411
3812c8c8
JW
412static inline bool task_in_memcg_oom(struct task_struct *p)
413{
626ebc41 414 return p->memcg_in_oom;
3812c8c8
JW
415}
416
49426420 417bool mem_cgroup_oom_synchronize(bool wait);
3812c8c8 418
c255a458 419#ifdef CONFIG_MEMCG_SWAP
c077719b
KH
420extern int do_swap_account;
421#endif
f8d66542 422
6de22619 423struct mem_cgroup *mem_cgroup_begin_page_stat(struct page *page);
6de22619 424void mem_cgroup_end_page_stat(struct mem_cgroup *memcg);
d7365e78 425
33398cf2
MH
426/**
427 * mem_cgroup_update_page_stat - update page state statistics
428 * @memcg: memcg to account against
429 * @idx: page state item to account
430 * @val: number of pages (positive or negative)
431 *
432 * See mem_cgroup_begin_page_stat() for locking requirements.
433 */
434static inline void mem_cgroup_update_page_stat(struct mem_cgroup *memcg,
435 enum mem_cgroup_stat_index idx, int val)
436{
437 VM_BUG_ON(!rcu_read_lock_held());
438
439 if (memcg)
440 this_cpu_add(memcg->stat->count[idx], val);
441}
442
d7365e78 443static inline void mem_cgroup_inc_page_stat(struct mem_cgroup *memcg,
68b4876d 444 enum mem_cgroup_stat_index idx)
2a7106f2 445{
d7365e78 446 mem_cgroup_update_page_stat(memcg, idx, 1);
2a7106f2
GT
447}
448
d7365e78 449static inline void mem_cgroup_dec_page_stat(struct mem_cgroup *memcg,
68b4876d 450 enum mem_cgroup_stat_index idx)
2a7106f2 451{
d7365e78 452 mem_cgroup_update_page_stat(memcg, idx, -1);
2a7106f2
GT
453}
454
0608f43d
AM
455unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
456 gfp_t gfp_mask,
457 unsigned long *total_scanned);
a63d83f4 458
68ae564b
DR
459static inline void mem_cgroup_count_vm_event(struct mm_struct *mm,
460 enum vm_event_item idx)
461{
33398cf2
MH
462 struct mem_cgroup *memcg;
463
68ae564b
DR
464 if (mem_cgroup_disabled())
465 return;
33398cf2
MH
466
467 rcu_read_lock();
468 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
469 if (unlikely(!memcg))
470 goto out;
471
472 switch (idx) {
473 case PGFAULT:
474 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
475 break;
476 case PGMAJFAULT:
477 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
478 break;
479 default:
480 BUG();
481 }
482out:
483 rcu_read_unlock();
68ae564b 484}
ca3e0214 485#ifdef CONFIG_TRANSPARENT_HUGEPAGE
e94c8a9c 486void mem_cgroup_split_huge_fixup(struct page *head);
ca3e0214
KH
487#endif
488
c255a458 489#else /* CONFIG_MEMCG */
7a81b88c
KH
490struct mem_cgroup;
491
241994ed
JW
492static inline void mem_cgroup_events(struct mem_cgroup *memcg,
493 enum mem_cgroup_events_index idx,
494 unsigned int nr)
495{
496}
497
498static inline bool mem_cgroup_low(struct mem_cgroup *root,
499 struct mem_cgroup *memcg)
500{
501 return false;
502}
503
00501b53
JW
504static inline int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
505 gfp_t gfp_mask,
506 struct mem_cgroup **memcgp)
7a81b88c 507{
00501b53 508 *memcgp = NULL;
7a81b88c
KH
509 return 0;
510}
511
00501b53
JW
512static inline void mem_cgroup_commit_charge(struct page *page,
513 struct mem_cgroup *memcg,
514 bool lrucare)
7a81b88c
KH
515{
516}
517
00501b53
JW
518static inline void mem_cgroup_cancel_charge(struct page *page,
519 struct mem_cgroup *memcg)
7a81b88c
KH
520{
521}
522
0a31bc97 523static inline void mem_cgroup_uncharge(struct page *page)
569b846d
KH
524{
525}
526
747db954 527static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
8a9f3ccd
BS
528{
529}
530
45637bab 531static inline void mem_cgroup_replace_page(struct page *old, struct page *new)
69029cd5
KH
532{
533}
534
925b7673
JW
535static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
536 struct mem_cgroup *memcg)
08e552c6 537{
925b7673 538 return &zone->lruvec;
08e552c6
KH
539}
540
fa9add64
HD
541static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
542 struct zone *zone)
66e1707b 543{
925b7673 544 return &zone->lruvec;
66e1707b
BS
545}
546
587af308 547static inline bool mm_match_cgroup(struct mm_struct *mm,
c0ff4b85 548 struct mem_cgroup *memcg)
bed7161a 549{
587af308 550 return true;
bed7161a
BS
551}
552
ffbdccf5
DR
553static inline bool task_in_mem_cgroup(struct task_struct *task,
554 const struct mem_cgroup *memcg)
4c4a2214 555{
ffbdccf5 556 return true;
4c4a2214
DR
557}
558
5660048c
JW
559static inline struct mem_cgroup *
560mem_cgroup_iter(struct mem_cgroup *root,
561 struct mem_cgroup *prev,
562 struct mem_cgroup_reclaim_cookie *reclaim)
563{
564 return NULL;
565}
566
567static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
568 struct mem_cgroup *prev)
569{
570}
571
f8d66542
HT
572static inline bool mem_cgroup_disabled(void)
573{
574 return true;
575}
a636b327 576
13308ca9 577static inline bool
c56d5c7d 578mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
14797e23 579{
13308ca9 580 return true;
14797e23
KM
581}
582
90cbc250
VD
583static inline bool mem_cgroup_lruvec_online(struct lruvec *lruvec)
584{
585 return true;
586}
587
a3d8e054 588static inline unsigned long
4d7dcca2 589mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
a3d8e054
KM
590{
591 return 0;
592}
593
fa9add64
HD
594static inline void
595mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
596 int increment)
3e2f41f1 597{
3e2f41f1
KM
598}
599
e222432b
BS
600static inline void
601mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
602{
603}
604
6de22619 605static inline struct mem_cgroup *mem_cgroup_begin_page_stat(struct page *page)
89c06bd5 606{
d7365e78 607 return NULL;
89c06bd5
KH
608}
609
6de22619 610static inline void mem_cgroup_end_page_stat(struct mem_cgroup *memcg)
89c06bd5
KH
611{
612}
613
b23afb93
TH
614static inline void mem_cgroup_handle_over_high(void)
615{
616}
617
49426420 618static inline void mem_cgroup_oom_enable(void)
519e5247
JW
619{
620}
621
49426420 622static inline void mem_cgroup_oom_disable(void)
519e5247
JW
623{
624}
625
3812c8c8
JW
626static inline bool task_in_memcg_oom(struct task_struct *p)
627{
628 return false;
629}
630
49426420 631static inline bool mem_cgroup_oom_synchronize(bool wait)
3812c8c8
JW
632{
633 return false;
634}
635
d7365e78 636static inline void mem_cgroup_inc_page_stat(struct mem_cgroup *memcg,
68b4876d 637 enum mem_cgroup_stat_index idx)
2a7106f2
GT
638{
639}
640
d7365e78 641static inline void mem_cgroup_dec_page_stat(struct mem_cgroup *memcg,
68b4876d 642 enum mem_cgroup_stat_index idx)
d69b042f
BS
643{
644}
645
4e416953 646static inline
0608f43d
AM
647unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
648 gfp_t gfp_mask,
649 unsigned long *total_scanned)
4e416953 650{
0608f43d 651 return 0;
4e416953
BS
652}
653
e94c8a9c 654static inline void mem_cgroup_split_huge_fixup(struct page *head)
ca3e0214
KH
655{
656}
657
456f998e
YH
658static inline
659void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
660{
661}
c255a458 662#endif /* CONFIG_MEMCG */
78fb7466 663
e1aab161
GC
664enum {
665 UNDER_LIMIT,
666 SOFT_LIMIT,
667 OVER_LIMIT,
668};
669
52ebea74 670#ifdef CONFIG_CGROUP_WRITEBACK
841710aa 671
52ebea74 672struct list_head *mem_cgroup_cgwb_list(struct mem_cgroup *memcg);
841710aa 673struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
c5edf9cd
TH
674void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
675 unsigned long *pheadroom, unsigned long *pdirty,
676 unsigned long *pwriteback);
841710aa
TH
677
678#else /* CONFIG_CGROUP_WRITEBACK */
679
680static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
681{
682 return NULL;
683}
684
c2aa723a 685static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
c5edf9cd
TH
686 unsigned long *pfilepages,
687 unsigned long *pheadroom,
c2aa723a
TH
688 unsigned long *pdirty,
689 unsigned long *pwriteback)
690{
691}
692
841710aa 693#endif /* CONFIG_CGROUP_WRITEBACK */
52ebea74 694
e1aab161 695struct sock;
cd59085a 696#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
e1aab161
GC
697void sock_update_memcg(struct sock *sk);
698void sock_release_memcg(struct sock *sk);
699#else
700static inline void sock_update_memcg(struct sock *sk)
701{
702}
703static inline void sock_release_memcg(struct sock *sk)
704{
705}
cd59085a 706#endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
7ae1e1d0
GC
707
708#ifdef CONFIG_MEMCG_KMEM
a8964b9b 709extern struct static_key memcg_kmem_enabled_key;
749c5415 710
dbcf73e2 711extern int memcg_nr_cache_ids;
64219994
MH
712void memcg_get_cache_ids(void);
713void memcg_put_cache_ids(void);
ebe945c2
GC
714
715/*
716 * Helper macro to loop through all memcg-specific caches. Callers must still
717 * check if the cache is valid (it is either valid or NULL).
718 * the slab_mutex must be held when looping through those caches
719 */
749c5415 720#define for_each_memcg_cache_index(_idx) \
dbcf73e2 721 for ((_idx) = 0; (_idx) < memcg_nr_cache_ids; (_idx)++)
749c5415 722
7ae1e1d0
GC
723static inline bool memcg_kmem_enabled(void)
724{
a8964b9b 725 return static_key_false(&memcg_kmem_enabled_key);
7ae1e1d0
GC
726}
727
33398cf2
MH
728static inline bool memcg_kmem_is_active(struct mem_cgroup *memcg)
729{
730 return memcg->kmem_acct_active;
731}
cb731d6c 732
7ae1e1d0
GC
733/*
734 * In general, we'll do everything in our power to not incur in any overhead
735 * for non-memcg users for the kmem functions. Not even a function call, if we
736 * can avoid it.
737 *
738 * Therefore, we'll inline all those functions so that in the best case, we'll
739 * see that kmemcg is off for everybody and proceed quickly. If it is on,
740 * we'll still do most of the flag checking inline. We check a lot of
741 * conditions, but because they are pretty simple, they are expected to be
742 * fast.
743 */
f3ccb2c4
VD
744int __memcg_kmem_charge_memcg(struct page *page, gfp_t gfp, int order,
745 struct mem_cgroup *memcg);
d05e83a6
VD
746int __memcg_kmem_charge(struct page *page, gfp_t gfp, int order);
747void __memcg_kmem_uncharge(struct page *page, int order);
7ae1e1d0 748
33398cf2
MH
749/*
750 * helper for acessing a memcg's index. It will be used as an index in the
751 * child cache array in kmem_cache, and also to derive its name. This function
752 * will return -1 when this is not a kmem-limited memcg.
753 */
754static inline int memcg_cache_id(struct mem_cgroup *memcg)
755{
756 return memcg ? memcg->kmemcg_id : -1;
757}
5722d094 758
230e9fc2 759struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp);
8135be5a 760void __memcg_kmem_put_cache(struct kmem_cache *cachep);
d7f25f8a 761
230e9fc2 762static inline bool __memcg_kmem_bypass(void)
7ae1e1d0
GC
763{
764 if (!memcg_kmem_enabled())
765 return true;
7ae1e1d0
GC
766 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
767 return true;
cbfb4798 768 return false;
7ae1e1d0
GC
769}
770
771/**
d05e83a6
VD
772 * memcg_kmem_charge: charge a kmem page
773 * @page: page to charge
774 * @gfp: reclaim mode
775 * @order: allocation order
cbfb4798 776 *
d05e83a6 777 * Returns 0 on success, an error code on failure.
7ae1e1d0 778 */
d05e83a6
VD
779static __always_inline int memcg_kmem_charge(struct page *page,
780 gfp_t gfp, int order)
7ae1e1d0 781{
230e9fc2
VD
782 if (__memcg_kmem_bypass())
783 return 0;
784 if (!(gfp & __GFP_ACCOUNT))
d05e83a6
VD
785 return 0;
786 return __memcg_kmem_charge(page, gfp, order);
7ae1e1d0
GC
787}
788
789/**
d05e83a6
VD
790 * memcg_kmem_uncharge: uncharge a kmem page
791 * @page: page to uncharge
792 * @order: allocation order
7ae1e1d0 793 */
d05e83a6 794static __always_inline void memcg_kmem_uncharge(struct page *page, int order)
7ae1e1d0 795{
7ae1e1d0 796 if (memcg_kmem_enabled())
d05e83a6 797 __memcg_kmem_uncharge(page, order);
7ae1e1d0
GC
798}
799
d7f25f8a
GC
800/**
801 * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation
802 * @cachep: the original global kmem cache
d7f25f8a 803 *
5dfb4175 804 * All memory allocated from a per-memcg cache is charged to the owner memcg.
d7f25f8a
GC
805 */
806static __always_inline struct kmem_cache *
807memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
808{
230e9fc2 809 if (__memcg_kmem_bypass())
d7f25f8a 810 return cachep;
230e9fc2 811 return __memcg_kmem_get_cache(cachep, gfp);
d7f25f8a 812}
8135be5a
VD
813
814static __always_inline void memcg_kmem_put_cache(struct kmem_cache *cachep)
815{
816 if (memcg_kmem_enabled())
817 __memcg_kmem_put_cache(cachep);
818}
7ae1e1d0 819#else
749c5415
GC
820#define for_each_memcg_cache_index(_idx) \
821 for (; NULL; )
822
b9ce5ef4
GC
823static inline bool memcg_kmem_enabled(void)
824{
825 return false;
826}
827
cb731d6c
VD
828static inline bool memcg_kmem_is_active(struct mem_cgroup *memcg)
829{
830 return false;
831}
832
d05e83a6 833static inline int memcg_kmem_charge(struct page *page, gfp_t gfp, int order)
7ae1e1d0 834{
d05e83a6 835 return 0;
7ae1e1d0
GC
836}
837
d05e83a6 838static inline void memcg_kmem_uncharge(struct page *page, int order)
7ae1e1d0
GC
839{
840}
2633d7a0
GC
841
842static inline int memcg_cache_id(struct mem_cgroup *memcg)
843{
844 return -1;
845}
846
05257a1a
VD
847static inline void memcg_get_cache_ids(void)
848{
849}
850
851static inline void memcg_put_cache_ids(void)
852{
853}
854
d7f25f8a
GC
855static inline struct kmem_cache *
856memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
857{
858 return cachep;
859}
8135be5a
VD
860
861static inline void memcg_kmem_put_cache(struct kmem_cache *cachep)
862{
863}
7ae1e1d0 864#endif /* CONFIG_MEMCG_KMEM */
8cdea7c0 865#endif /* _LINUX_MEMCONTROL_H */
This page took 0.906096 seconds and 5 git commands to generate.