]> asedeno.scripts.mit.edu Git - linux.git/blob - include/linux/memcontrol.h
10d741e8fe51c4347aa8eda279a57aa823c1ff82
[linux.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 #include <linux/page_counter.h>
27 #include <linux/vmpressure.h>
28 #include <linux/eventfd.h>
29 #include <linux/mm.h>
30 #include <linux/vmstat.h>
31 #include <linux/writeback.h>
32 #include <linux/page-flags.h>
33
34 struct mem_cgroup;
35 struct page;
36 struct mm_struct;
37 struct kmem_cache;
38
39 /* Cgroup-specific page state, on top of universal node page state */
40 enum memcg_stat_item {
41         MEMCG_CACHE = NR_VM_NODE_STAT_ITEMS,
42         MEMCG_RSS,
43         MEMCG_RSS_HUGE,
44         MEMCG_SWAP,
45         MEMCG_SOCK,
46         /* XXX: why are these zone and not node counters? */
47         MEMCG_KERNEL_STACK_KB,
48         MEMCG_NR_STAT,
49 };
50
51 enum memcg_memory_event {
52         MEMCG_LOW,
53         MEMCG_HIGH,
54         MEMCG_MAX,
55         MEMCG_OOM,
56         MEMCG_SWAP_MAX,
57         MEMCG_SWAP_FAIL,
58         MEMCG_NR_MEMORY_EVENTS,
59 };
60
61 struct mem_cgroup_reclaim_cookie {
62         pg_data_t *pgdat;
63         int priority;
64         unsigned int generation;
65 };
66
67 #ifdef CONFIG_MEMCG
68
69 #define MEM_CGROUP_ID_SHIFT     16
70 #define MEM_CGROUP_ID_MAX       USHRT_MAX
71
72 struct mem_cgroup_id {
73         int id;
74         atomic_t ref;
75 };
76
77 /*
78  * Per memcg event counter is incremented at every pagein/pageout. With THP,
79  * it will be incremated by the number of pages. This counter is used for
80  * for trigger some periodic events. This is straightforward and better
81  * than using jiffies etc. to handle periodic memcg event.
82  */
83 enum mem_cgroup_events_target {
84         MEM_CGROUP_TARGET_THRESH,
85         MEM_CGROUP_TARGET_SOFTLIMIT,
86         MEM_CGROUP_TARGET_NUMAINFO,
87         MEM_CGROUP_NTARGETS,
88 };
89
90 struct mem_cgroup_stat_cpu {
91         long count[MEMCG_NR_STAT];
92         unsigned long events[NR_VM_EVENT_ITEMS];
93         unsigned long nr_page_events;
94         unsigned long targets[MEM_CGROUP_NTARGETS];
95 };
96
97 struct mem_cgroup_reclaim_iter {
98         struct mem_cgroup *position;
99         /* scan generation, increased every round-trip */
100         unsigned int generation;
101 };
102
103 struct lruvec_stat {
104         long count[NR_VM_NODE_STAT_ITEMS];
105 };
106
107 /*
108  * per-zone information in memory controller.
109  */
110 struct mem_cgroup_per_node {
111         struct lruvec           lruvec;
112
113         struct lruvec_stat __percpu *lruvec_stat_cpu;
114         atomic_long_t           lruvec_stat[NR_VM_NODE_STAT_ITEMS];
115
116         unsigned long           lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
117
118         struct mem_cgroup_reclaim_iter  iter[DEF_PRIORITY + 1];
119
120         struct rb_node          tree_node;      /* RB tree node */
121         unsigned long           usage_in_excess;/* Set to the value by which */
122                                                 /* the soft limit is exceeded*/
123         bool                    on_tree;
124         bool                    congested;      /* memcg has many dirty pages */
125                                                 /* backed by a congested BDI */
126
127         struct mem_cgroup       *memcg;         /* Back pointer, we cannot */
128                                                 /* use container_of        */
129 };
130
131 struct mem_cgroup_threshold {
132         struct eventfd_ctx *eventfd;
133         unsigned long threshold;
134 };
135
136 /* For threshold */
137 struct mem_cgroup_threshold_ary {
138         /* An array index points to threshold just below or equal to usage. */
139         int current_threshold;
140         /* Size of entries[] */
141         unsigned int size;
142         /* Array of thresholds */
143         struct mem_cgroup_threshold entries[0];
144 };
145
146 struct mem_cgroup_thresholds {
147         /* Primary thresholds array */
148         struct mem_cgroup_threshold_ary *primary;
149         /*
150          * Spare threshold array.
151          * This is needed to make mem_cgroup_unregister_event() "never fail".
152          * It must be able to store at least primary->size - 1 entries.
153          */
154         struct mem_cgroup_threshold_ary *spare;
155 };
156
157 enum memcg_kmem_state {
158         KMEM_NONE,
159         KMEM_ALLOCATED,
160         KMEM_ONLINE,
161 };
162
163 /*
164  * The memory controller data structure. The memory controller controls both
165  * page cache and RSS per cgroup. We would eventually like to provide
166  * statistics based on the statistics developed by Rik Van Riel for clock-pro,
167  * to help the administrator determine what knobs to tune.
168  */
169 struct mem_cgroup {
170         struct cgroup_subsys_state css;
171
172         /* Private memcg ID. Used to ID objects that outlive the cgroup */
173         struct mem_cgroup_id id;
174
175         /* Accounted resources */
176         struct page_counter memory;
177         struct page_counter swap;
178
179         /* Legacy consumer-oriented counters */
180         struct page_counter memsw;
181         struct page_counter kmem;
182         struct page_counter tcpmem;
183
184         /* Upper bound of normal memory consumption range */
185         unsigned long high;
186
187         /* Range enforcement for interrupt charges */
188         struct work_struct high_work;
189
190         unsigned long soft_limit;
191
192         /* vmpressure notifications */
193         struct vmpressure vmpressure;
194
195         /*
196          * Should the accounting and control be hierarchical, per subtree?
197          */
198         bool use_hierarchy;
199
200         /* protected by memcg_oom_lock */
201         bool            oom_lock;
202         int             under_oom;
203
204         int     swappiness;
205         /* OOM-Killer disable */
206         int             oom_kill_disable;
207
208         /* memory.events */
209         atomic_long_t memory_events[MEMCG_NR_MEMORY_EVENTS];
210         struct cgroup_file events_file;
211
212         /* handle for "memory.swap.events" */
213         struct cgroup_file swap_events_file;
214
215         /* protect arrays of thresholds */
216         struct mutex thresholds_lock;
217
218         /* thresholds for memory usage. RCU-protected */
219         struct mem_cgroup_thresholds thresholds;
220
221         /* thresholds for mem+swap usage. RCU-protected */
222         struct mem_cgroup_thresholds memsw_thresholds;
223
224         /* For oom notifier event fd */
225         struct list_head oom_notify;
226
227         /*
228          * Should we move charges of a task when a task is moved into this
229          * mem_cgroup ? And what type of charges should we move ?
230          */
231         unsigned long move_charge_at_immigrate;
232         /*
233          * set > 0 if pages under this cgroup are moving to other cgroup.
234          */
235         atomic_t                moving_account;
236         /* taken only while moving_account > 0 */
237         spinlock_t              move_lock;
238         struct task_struct      *move_lock_task;
239         unsigned long           move_lock_flags;
240
241         /* memory.stat */
242         struct mem_cgroup_stat_cpu __percpu *stat_cpu;
243         atomic_long_t           stat[MEMCG_NR_STAT];
244         atomic_long_t           events[NR_VM_EVENT_ITEMS];
245
246         unsigned long           socket_pressure;
247
248         /* Legacy tcp memory accounting */
249         bool                    tcpmem_active;
250         int                     tcpmem_pressure;
251
252 #ifndef CONFIG_SLOB
253         /* Index in the kmem_cache->memcg_params.memcg_caches array */
254         int kmemcg_id;
255         enum memcg_kmem_state kmem_state;
256         struct list_head kmem_caches;
257 #endif
258
259         int last_scanned_node;
260 #if MAX_NUMNODES > 1
261         nodemask_t      scan_nodes;
262         atomic_t        numainfo_events;
263         atomic_t        numainfo_updating;
264 #endif
265
266 #ifdef CONFIG_CGROUP_WRITEBACK
267         struct list_head cgwb_list;
268         struct wb_domain cgwb_domain;
269 #endif
270
271         /* List of events which userspace want to receive */
272         struct list_head event_list;
273         spinlock_t event_list_lock;
274
275         struct mem_cgroup_per_node *nodeinfo[0];
276         /* WARNING: nodeinfo must be the last member here */
277 };
278
279 /*
280  * size of first charge trial. "32" comes from vmscan.c's magic value.
281  * TODO: maybe necessary to use big numbers in big irons.
282  */
283 #define MEMCG_CHARGE_BATCH 32U
284
285 extern struct mem_cgroup *root_mem_cgroup;
286
287 static inline bool mem_cgroup_disabled(void)
288 {
289         return !cgroup_subsys_enabled(memory_cgrp_subsys);
290 }
291
292 bool mem_cgroup_low(struct mem_cgroup *root, struct mem_cgroup *memcg);
293
294 int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
295                           gfp_t gfp_mask, struct mem_cgroup **memcgp,
296                           bool compound);
297 void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
298                               bool lrucare, bool compound);
299 void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg,
300                 bool compound);
301 void mem_cgroup_uncharge(struct page *page);
302 void mem_cgroup_uncharge_list(struct list_head *page_list);
303
304 void mem_cgroup_migrate(struct page *oldpage, struct page *newpage);
305
306 static struct mem_cgroup_per_node *
307 mem_cgroup_nodeinfo(struct mem_cgroup *memcg, int nid)
308 {
309         return memcg->nodeinfo[nid];
310 }
311
312 /**
313  * mem_cgroup_lruvec - get the lru list vector for a node or a memcg zone
314  * @node: node of the wanted lruvec
315  * @memcg: memcg of the wanted lruvec
316  *
317  * Returns the lru list vector holding pages for a given @node or a given
318  * @memcg and @zone. This can be the node lruvec, if the memory controller
319  * is disabled.
320  */
321 static inline struct lruvec *mem_cgroup_lruvec(struct pglist_data *pgdat,
322                                 struct mem_cgroup *memcg)
323 {
324         struct mem_cgroup_per_node *mz;
325         struct lruvec *lruvec;
326
327         if (mem_cgroup_disabled()) {
328                 lruvec = node_lruvec(pgdat);
329                 goto out;
330         }
331
332         mz = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
333         lruvec = &mz->lruvec;
334 out:
335         /*
336          * Since a node can be onlined after the mem_cgroup was created,
337          * we have to be prepared to initialize lruvec->pgdat here;
338          * and if offlined then reonlined, we need to reinitialize it.
339          */
340         if (unlikely(lruvec->pgdat != pgdat))
341                 lruvec->pgdat = pgdat;
342         return lruvec;
343 }
344
345 struct lruvec *mem_cgroup_page_lruvec(struct page *, struct pglist_data *);
346
347 bool task_in_mem_cgroup(struct task_struct *task, struct mem_cgroup *memcg);
348 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
349
350 static inline
351 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
352         return css ? container_of(css, struct mem_cgroup, css) : NULL;
353 }
354
355 #define mem_cgroup_from_counter(counter, member)        \
356         container_of(counter, struct mem_cgroup, member)
357
358 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
359                                    struct mem_cgroup *,
360                                    struct mem_cgroup_reclaim_cookie *);
361 void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
362 int mem_cgroup_scan_tasks(struct mem_cgroup *,
363                           int (*)(struct task_struct *, void *), void *);
364
365 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
366 {
367         if (mem_cgroup_disabled())
368                 return 0;
369
370         return memcg->id.id;
371 }
372 struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
373
374 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
375 {
376         struct mem_cgroup_per_node *mz;
377
378         if (mem_cgroup_disabled())
379                 return NULL;
380
381         mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
382         return mz->memcg;
383 }
384
385 /**
386  * parent_mem_cgroup - find the accounting parent of a memcg
387  * @memcg: memcg whose parent to find
388  *
389  * Returns the parent memcg, or NULL if this is the root or the memory
390  * controller is in legacy no-hierarchy mode.
391  */
392 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
393 {
394         if (!memcg->memory.parent)
395                 return NULL;
396         return mem_cgroup_from_counter(memcg->memory.parent, memory);
397 }
398
399 static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
400                               struct mem_cgroup *root)
401 {
402         if (root == memcg)
403                 return true;
404         if (!root->use_hierarchy)
405                 return false;
406         return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
407 }
408
409 static inline bool mm_match_cgroup(struct mm_struct *mm,
410                                    struct mem_cgroup *memcg)
411 {
412         struct mem_cgroup *task_memcg;
413         bool match = false;
414
415         rcu_read_lock();
416         task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
417         if (task_memcg)
418                 match = mem_cgroup_is_descendant(task_memcg, memcg);
419         rcu_read_unlock();
420         return match;
421 }
422
423 struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page);
424 ino_t page_cgroup_ino(struct page *page);
425
426 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
427 {
428         if (mem_cgroup_disabled())
429                 return true;
430         return !!(memcg->css.flags & CSS_ONLINE);
431 }
432
433 /*
434  * For memory reclaim.
435  */
436 int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
437
438 void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
439                 int zid, int nr_pages);
440
441 unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
442                                            int nid, unsigned int lru_mask);
443
444 static inline
445 unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
446 {
447         struct mem_cgroup_per_node *mz;
448         unsigned long nr_pages = 0;
449         int zid;
450
451         mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
452         for (zid = 0; zid < MAX_NR_ZONES; zid++)
453                 nr_pages += mz->lru_zone_size[zid][lru];
454         return nr_pages;
455 }
456
457 static inline
458 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
459                 enum lru_list lru, int zone_idx)
460 {
461         struct mem_cgroup_per_node *mz;
462
463         mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
464         return mz->lru_zone_size[zone_idx][lru];
465 }
466
467 void mem_cgroup_handle_over_high(void);
468
469 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);
470
471 void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
472                                 struct task_struct *p);
473
474 static inline void mem_cgroup_oom_enable(void)
475 {
476         WARN_ON(current->memcg_may_oom);
477         current->memcg_may_oom = 1;
478 }
479
480 static inline void mem_cgroup_oom_disable(void)
481 {
482         WARN_ON(!current->memcg_may_oom);
483         current->memcg_may_oom = 0;
484 }
485
486 static inline bool task_in_memcg_oom(struct task_struct *p)
487 {
488         return p->memcg_in_oom;
489 }
490
491 bool mem_cgroup_oom_synchronize(bool wait);
492
493 #ifdef CONFIG_MEMCG_SWAP
494 extern int do_swap_account;
495 #endif
496
497 struct mem_cgroup *lock_page_memcg(struct page *page);
498 void __unlock_page_memcg(struct mem_cgroup *memcg);
499 void unlock_page_memcg(struct page *page);
500
501 /* idx can be of type enum memcg_stat_item or node_stat_item */
502 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg,
503                                              int idx)
504 {
505         long x = atomic_long_read(&memcg->stat[idx]);
506 #ifdef CONFIG_SMP
507         if (x < 0)
508                 x = 0;
509 #endif
510         return x;
511 }
512
513 /* idx can be of type enum memcg_stat_item or node_stat_item */
514 static inline void __mod_memcg_state(struct mem_cgroup *memcg,
515                                      int idx, int val)
516 {
517         long x;
518
519         if (mem_cgroup_disabled())
520                 return;
521
522         x = val + __this_cpu_read(memcg->stat_cpu->count[idx]);
523         if (unlikely(abs(x) > MEMCG_CHARGE_BATCH)) {
524                 atomic_long_add(x, &memcg->stat[idx]);
525                 x = 0;
526         }
527         __this_cpu_write(memcg->stat_cpu->count[idx], x);
528 }
529
530 /* idx can be of type enum memcg_stat_item or node_stat_item */
531 static inline void mod_memcg_state(struct mem_cgroup *memcg,
532                                    int idx, int val)
533 {
534         unsigned long flags;
535
536         local_irq_save(flags);
537         __mod_memcg_state(memcg, idx, val);
538         local_irq_restore(flags);
539 }
540
541 /**
542  * mod_memcg_page_state - update page state statistics
543  * @page: the page
544  * @idx: page state item to account
545  * @val: number of pages (positive or negative)
546  *
547  * The @page must be locked or the caller must use lock_page_memcg()
548  * to prevent double accounting when the page is concurrently being
549  * moved to another memcg:
550  *
551  *   lock_page(page) or lock_page_memcg(page)
552  *   if (TestClearPageState(page))
553  *     mod_memcg_page_state(page, state, -1);
554  *   unlock_page(page) or unlock_page_memcg(page)
555  *
556  * Kernel pages are an exception to this, since they'll never move.
557  */
558 static inline void __mod_memcg_page_state(struct page *page,
559                                           int idx, int val)
560 {
561         if (page->mem_cgroup)
562                 __mod_memcg_state(page->mem_cgroup, idx, val);
563 }
564
565 static inline void mod_memcg_page_state(struct page *page,
566                                         int idx, int val)
567 {
568         if (page->mem_cgroup)
569                 mod_memcg_state(page->mem_cgroup, idx, val);
570 }
571
572 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
573                                               enum node_stat_item idx)
574 {
575         struct mem_cgroup_per_node *pn;
576         long x;
577
578         if (mem_cgroup_disabled())
579                 return node_page_state(lruvec_pgdat(lruvec), idx);
580
581         pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
582         x = atomic_long_read(&pn->lruvec_stat[idx]);
583 #ifdef CONFIG_SMP
584         if (x < 0)
585                 x = 0;
586 #endif
587         return x;
588 }
589
590 static inline void __mod_lruvec_state(struct lruvec *lruvec,
591                                       enum node_stat_item idx, int val)
592 {
593         struct mem_cgroup_per_node *pn;
594         long x;
595
596         /* Update node */
597         __mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
598
599         if (mem_cgroup_disabled())
600                 return;
601
602         pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
603
604         /* Update memcg */
605         __mod_memcg_state(pn->memcg, idx, val);
606
607         /* Update lruvec */
608         x = val + __this_cpu_read(pn->lruvec_stat_cpu->count[idx]);
609         if (unlikely(abs(x) > MEMCG_CHARGE_BATCH)) {
610                 atomic_long_add(x, &pn->lruvec_stat[idx]);
611                 x = 0;
612         }
613         __this_cpu_write(pn->lruvec_stat_cpu->count[idx], x);
614 }
615
616 static inline void mod_lruvec_state(struct lruvec *lruvec,
617                                     enum node_stat_item idx, int val)
618 {
619         unsigned long flags;
620
621         local_irq_save(flags);
622         __mod_lruvec_state(lruvec, idx, val);
623         local_irq_restore(flags);
624 }
625
626 static inline void __mod_lruvec_page_state(struct page *page,
627                                            enum node_stat_item idx, int val)
628 {
629         pg_data_t *pgdat = page_pgdat(page);
630         struct lruvec *lruvec;
631
632         /* Untracked pages have no memcg, no lruvec. Update only the node */
633         if (!page->mem_cgroup) {
634                 __mod_node_page_state(pgdat, idx, val);
635                 return;
636         }
637
638         lruvec = mem_cgroup_lruvec(pgdat, page->mem_cgroup);
639         __mod_lruvec_state(lruvec, idx, val);
640 }
641
642 static inline void mod_lruvec_page_state(struct page *page,
643                                          enum node_stat_item idx, int val)
644 {
645         unsigned long flags;
646
647         local_irq_save(flags);
648         __mod_lruvec_page_state(page, idx, val);
649         local_irq_restore(flags);
650 }
651
652 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
653                                                 gfp_t gfp_mask,
654                                                 unsigned long *total_scanned);
655
656 static inline void __count_memcg_events(struct mem_cgroup *memcg,
657                                         enum vm_event_item idx,
658                                         unsigned long count)
659 {
660         unsigned long x;
661
662         if (mem_cgroup_disabled())
663                 return;
664
665         x = count + __this_cpu_read(memcg->stat_cpu->events[idx]);
666         if (unlikely(x > MEMCG_CHARGE_BATCH)) {
667                 atomic_long_add(x, &memcg->events[idx]);
668                 x = 0;
669         }
670         __this_cpu_write(memcg->stat_cpu->events[idx], x);
671 }
672
673 static inline void count_memcg_events(struct mem_cgroup *memcg,
674                                       enum vm_event_item idx,
675                                       unsigned long count)
676 {
677         unsigned long flags;
678
679         local_irq_save(flags);
680         __count_memcg_events(memcg, idx, count);
681         local_irq_restore(flags);
682 }
683
684 static inline void count_memcg_page_event(struct page *page,
685                                           enum vm_event_item idx)
686 {
687         if (page->mem_cgroup)
688                 count_memcg_events(page->mem_cgroup, idx, 1);
689 }
690
691 static inline void count_memcg_event_mm(struct mm_struct *mm,
692                                         enum vm_event_item idx)
693 {
694         struct mem_cgroup *memcg;
695
696         if (mem_cgroup_disabled())
697                 return;
698
699         rcu_read_lock();
700         memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
701         if (likely(memcg)) {
702                 count_memcg_events(memcg, idx, 1);
703                 if (idx == OOM_KILL)
704                         cgroup_file_notify(&memcg->events_file);
705         }
706         rcu_read_unlock();
707 }
708
709 static inline void memcg_memory_event(struct mem_cgroup *memcg,
710                                       enum memcg_memory_event event)
711 {
712         atomic_long_inc(&memcg->memory_events[event]);
713         cgroup_file_notify(&memcg->events_file);
714 }
715
716 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
717 void mem_cgroup_split_huge_fixup(struct page *head);
718 #endif
719
720 #else /* CONFIG_MEMCG */
721
722 #define MEM_CGROUP_ID_SHIFT     0
723 #define MEM_CGROUP_ID_MAX       0
724
725 struct mem_cgroup;
726
727 static inline bool mem_cgroup_disabled(void)
728 {
729         return true;
730 }
731
732 static inline void memcg_memory_event(struct mem_cgroup *memcg,
733                                       enum memcg_memory_event event)
734 {
735 }
736
737 static inline bool mem_cgroup_low(struct mem_cgroup *root,
738                                   struct mem_cgroup *memcg)
739 {
740         return false;
741 }
742
743 static inline int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
744                                         gfp_t gfp_mask,
745                                         struct mem_cgroup **memcgp,
746                                         bool compound)
747 {
748         *memcgp = NULL;
749         return 0;
750 }
751
752 static inline void mem_cgroup_commit_charge(struct page *page,
753                                             struct mem_cgroup *memcg,
754                                             bool lrucare, bool compound)
755 {
756 }
757
758 static inline void mem_cgroup_cancel_charge(struct page *page,
759                                             struct mem_cgroup *memcg,
760                                             bool compound)
761 {
762 }
763
764 static inline void mem_cgroup_uncharge(struct page *page)
765 {
766 }
767
768 static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
769 {
770 }
771
772 static inline void mem_cgroup_migrate(struct page *old, struct page *new)
773 {
774 }
775
776 static inline struct lruvec *mem_cgroup_lruvec(struct pglist_data *pgdat,
777                                 struct mem_cgroup *memcg)
778 {
779         return node_lruvec(pgdat);
780 }
781
782 static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
783                                                     struct pglist_data *pgdat)
784 {
785         return &pgdat->lruvec;
786 }
787
788 static inline bool mm_match_cgroup(struct mm_struct *mm,
789                 struct mem_cgroup *memcg)
790 {
791         return true;
792 }
793
794 static inline bool task_in_mem_cgroup(struct task_struct *task,
795                                       const struct mem_cgroup *memcg)
796 {
797         return true;
798 }
799
800 static inline struct mem_cgroup *
801 mem_cgroup_iter(struct mem_cgroup *root,
802                 struct mem_cgroup *prev,
803                 struct mem_cgroup_reclaim_cookie *reclaim)
804 {
805         return NULL;
806 }
807
808 static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
809                                          struct mem_cgroup *prev)
810 {
811 }
812
813 static inline int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
814                 int (*fn)(struct task_struct *, void *), void *arg)
815 {
816         return 0;
817 }
818
819 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
820 {
821         return 0;
822 }
823
824 static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
825 {
826         WARN_ON_ONCE(id);
827         /* XXX: This should always return root_mem_cgroup */
828         return NULL;
829 }
830
831 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
832 {
833         return NULL;
834 }
835
836 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
837 {
838         return true;
839 }
840
841 static inline unsigned long
842 mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
843 {
844         return 0;
845 }
846 static inline
847 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
848                 enum lru_list lru, int zone_idx)
849 {
850         return 0;
851 }
852
853 static inline unsigned long
854 mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
855                              int nid, unsigned int lru_mask)
856 {
857         return 0;
858 }
859
860 static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
861 {
862         return 0;
863 }
864
865 static inline void
866 mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
867 {
868 }
869
870 static inline struct mem_cgroup *lock_page_memcg(struct page *page)
871 {
872         return NULL;
873 }
874
875 static inline void __unlock_page_memcg(struct mem_cgroup *memcg)
876 {
877 }
878
879 static inline void unlock_page_memcg(struct page *page)
880 {
881 }
882
883 static inline void mem_cgroup_handle_over_high(void)
884 {
885 }
886
887 static inline void mem_cgroup_oom_enable(void)
888 {
889 }
890
891 static inline void mem_cgroup_oom_disable(void)
892 {
893 }
894
895 static inline bool task_in_memcg_oom(struct task_struct *p)
896 {
897         return false;
898 }
899
900 static inline bool mem_cgroup_oom_synchronize(bool wait)
901 {
902         return false;
903 }
904
905 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg,
906                                              int idx)
907 {
908         return 0;
909 }
910
911 static inline void __mod_memcg_state(struct mem_cgroup *memcg,
912                                      int idx,
913                                      int nr)
914 {
915 }
916
917 static inline void mod_memcg_state(struct mem_cgroup *memcg,
918                                    int idx,
919                                    int nr)
920 {
921 }
922
923 static inline void __mod_memcg_page_state(struct page *page,
924                                           int idx,
925                                           int nr)
926 {
927 }
928
929 static inline void mod_memcg_page_state(struct page *page,
930                                         int idx,
931                                         int nr)
932 {
933 }
934
935 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
936                                               enum node_stat_item idx)
937 {
938         return node_page_state(lruvec_pgdat(lruvec), idx);
939 }
940
941 static inline void __mod_lruvec_state(struct lruvec *lruvec,
942                                       enum node_stat_item idx, int val)
943 {
944         __mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
945 }
946
947 static inline void mod_lruvec_state(struct lruvec *lruvec,
948                                     enum node_stat_item idx, int val)
949 {
950         mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
951 }
952
953 static inline void __mod_lruvec_page_state(struct page *page,
954                                            enum node_stat_item idx, int val)
955 {
956         __mod_node_page_state(page_pgdat(page), idx, val);
957 }
958
959 static inline void mod_lruvec_page_state(struct page *page,
960                                          enum node_stat_item idx, int val)
961 {
962         mod_node_page_state(page_pgdat(page), idx, val);
963 }
964
965 static inline
966 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
967                                             gfp_t gfp_mask,
968                                             unsigned long *total_scanned)
969 {
970         return 0;
971 }
972
973 static inline void mem_cgroup_split_huge_fixup(struct page *head)
974 {
975 }
976
977 static inline void count_memcg_events(struct mem_cgroup *memcg,
978                                       enum vm_event_item idx,
979                                       unsigned long count)
980 {
981 }
982
983 static inline void count_memcg_page_event(struct page *page,
984                                           int idx)
985 {
986 }
987
988 static inline
989 void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
990 {
991 }
992 #endif /* CONFIG_MEMCG */
993
994 /* idx can be of type enum memcg_stat_item or node_stat_item */
995 static inline void __inc_memcg_state(struct mem_cgroup *memcg,
996                                      int idx)
997 {
998         __mod_memcg_state(memcg, idx, 1);
999 }
1000
1001 /* idx can be of type enum memcg_stat_item or node_stat_item */
1002 static inline void __dec_memcg_state(struct mem_cgroup *memcg,
1003                                      int idx)
1004 {
1005         __mod_memcg_state(memcg, idx, -1);
1006 }
1007
1008 /* idx can be of type enum memcg_stat_item or node_stat_item */
1009 static inline void __inc_memcg_page_state(struct page *page,
1010                                           int idx)
1011 {
1012         __mod_memcg_page_state(page, idx, 1);
1013 }
1014
1015 /* idx can be of type enum memcg_stat_item or node_stat_item */
1016 static inline void __dec_memcg_page_state(struct page *page,
1017                                           int idx)
1018 {
1019         __mod_memcg_page_state(page, idx, -1);
1020 }
1021
1022 static inline void __inc_lruvec_state(struct lruvec *lruvec,
1023                                       enum node_stat_item idx)
1024 {
1025         __mod_lruvec_state(lruvec, idx, 1);
1026 }
1027
1028 static inline void __dec_lruvec_state(struct lruvec *lruvec,
1029                                       enum node_stat_item idx)
1030 {
1031         __mod_lruvec_state(lruvec, idx, -1);
1032 }
1033
1034 static inline void __inc_lruvec_page_state(struct page *page,
1035                                            enum node_stat_item idx)
1036 {
1037         __mod_lruvec_page_state(page, idx, 1);
1038 }
1039
1040 static inline void __dec_lruvec_page_state(struct page *page,
1041                                            enum node_stat_item idx)
1042 {
1043         __mod_lruvec_page_state(page, idx, -1);
1044 }
1045
1046 /* idx can be of type enum memcg_stat_item or node_stat_item */
1047 static inline void inc_memcg_state(struct mem_cgroup *memcg,
1048                                    int idx)
1049 {
1050         mod_memcg_state(memcg, idx, 1);
1051 }
1052
1053 /* idx can be of type enum memcg_stat_item or node_stat_item */
1054 static inline void dec_memcg_state(struct mem_cgroup *memcg,
1055                                    int idx)
1056 {
1057         mod_memcg_state(memcg, idx, -1);
1058 }
1059
1060 /* idx can be of type enum memcg_stat_item or node_stat_item */
1061 static inline void inc_memcg_page_state(struct page *page,
1062                                         int idx)
1063 {
1064         mod_memcg_page_state(page, idx, 1);
1065 }
1066
1067 /* idx can be of type enum memcg_stat_item or node_stat_item */
1068 static inline void dec_memcg_page_state(struct page *page,
1069                                         int idx)
1070 {
1071         mod_memcg_page_state(page, idx, -1);
1072 }
1073
1074 static inline void inc_lruvec_state(struct lruvec *lruvec,
1075                                     enum node_stat_item idx)
1076 {
1077         mod_lruvec_state(lruvec, idx, 1);
1078 }
1079
1080 static inline void dec_lruvec_state(struct lruvec *lruvec,
1081                                     enum node_stat_item idx)
1082 {
1083         mod_lruvec_state(lruvec, idx, -1);
1084 }
1085
1086 static inline void inc_lruvec_page_state(struct page *page,
1087                                          enum node_stat_item idx)
1088 {
1089         mod_lruvec_page_state(page, idx, 1);
1090 }
1091
1092 static inline void dec_lruvec_page_state(struct page *page,
1093                                          enum node_stat_item idx)
1094 {
1095         mod_lruvec_page_state(page, idx, -1);
1096 }
1097
1098 #ifdef CONFIG_CGROUP_WRITEBACK
1099
1100 struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
1101 void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
1102                          unsigned long *pheadroom, unsigned long *pdirty,
1103                          unsigned long *pwriteback);
1104
1105 #else   /* CONFIG_CGROUP_WRITEBACK */
1106
1107 static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
1108 {
1109         return NULL;
1110 }
1111
1112 static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
1113                                        unsigned long *pfilepages,
1114                                        unsigned long *pheadroom,
1115                                        unsigned long *pdirty,
1116                                        unsigned long *pwriteback)
1117 {
1118 }
1119
1120 #endif  /* CONFIG_CGROUP_WRITEBACK */
1121
1122 struct sock;
1123 bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1124 void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1125 #ifdef CONFIG_MEMCG
1126 extern struct static_key_false memcg_sockets_enabled_key;
1127 #define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
1128 void mem_cgroup_sk_alloc(struct sock *sk);
1129 void mem_cgroup_sk_free(struct sock *sk);
1130 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1131 {
1132         if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_pressure)
1133                 return true;
1134         do {
1135                 if (time_before(jiffies, memcg->socket_pressure))
1136                         return true;
1137         } while ((memcg = parent_mem_cgroup(memcg)));
1138         return false;
1139 }
1140 #else
1141 #define mem_cgroup_sockets_enabled 0
1142 static inline void mem_cgroup_sk_alloc(struct sock *sk) { };
1143 static inline void mem_cgroup_sk_free(struct sock *sk) { };
1144 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1145 {
1146         return false;
1147 }
1148 #endif
1149
1150 struct kmem_cache *memcg_kmem_get_cache(struct kmem_cache *cachep);
1151 void memcg_kmem_put_cache(struct kmem_cache *cachep);
1152 int memcg_kmem_charge_memcg(struct page *page, gfp_t gfp, int order,
1153                             struct mem_cgroup *memcg);
1154 int memcg_kmem_charge(struct page *page, gfp_t gfp, int order);
1155 void memcg_kmem_uncharge(struct page *page, int order);
1156
1157 #if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB)
1158 extern struct static_key_false memcg_kmem_enabled_key;
1159 extern struct workqueue_struct *memcg_kmem_cache_wq;
1160
1161 extern int memcg_nr_cache_ids;
1162 void memcg_get_cache_ids(void);
1163 void memcg_put_cache_ids(void);
1164
1165 /*
1166  * Helper macro to loop through all memcg-specific caches. Callers must still
1167  * check if the cache is valid (it is either valid or NULL).
1168  * the slab_mutex must be held when looping through those caches
1169  */
1170 #define for_each_memcg_cache_index(_idx)        \
1171         for ((_idx) = 0; (_idx) < memcg_nr_cache_ids; (_idx)++)
1172
1173 static inline bool memcg_kmem_enabled(void)
1174 {
1175         return static_branch_unlikely(&memcg_kmem_enabled_key);
1176 }
1177
1178 /*
1179  * helper for accessing a memcg's index. It will be used as an index in the
1180  * child cache array in kmem_cache, and also to derive its name. This function
1181  * will return -1 when this is not a kmem-limited memcg.
1182  */
1183 static inline int memcg_cache_id(struct mem_cgroup *memcg)
1184 {
1185         return memcg ? memcg->kmemcg_id : -1;
1186 }
1187
1188 #else
1189 #define for_each_memcg_cache_index(_idx)        \
1190         for (; NULL; )
1191
1192 static inline bool memcg_kmem_enabled(void)
1193 {
1194         return false;
1195 }
1196
1197 static inline int memcg_cache_id(struct mem_cgroup *memcg)
1198 {
1199         return -1;
1200 }
1201
1202 static inline void memcg_get_cache_ids(void)
1203 {
1204 }
1205
1206 static inline void memcg_put_cache_ids(void)
1207 {
1208 }
1209
1210 #endif /* CONFIG_MEMCG && !CONFIG_SLOB */
1211
1212 #endif /* _LINUX_MEMCONTROL_H */