GNU Linux-libre 5.4.241-gnu1
[releases.git] / include / linux / memcontrol.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* memcontrol.h - Memory Controller
3  *
4  * Copyright IBM Corporation, 2007
5  * Author Balbir Singh <balbir@linux.vnet.ibm.com>
6  *
7  * Copyright 2007 OpenVZ SWsoft Inc
8  * Author: Pavel Emelianov <xemul@openvz.org>
9  */
10
11 #ifndef _LINUX_MEMCONTROL_H
12 #define _LINUX_MEMCONTROL_H
13 #include <linux/cgroup.h>
14 #include <linux/vm_event_item.h>
15 #include <linux/hardirq.h>
16 #include <linux/jump_label.h>
17 #include <linux/page_counter.h>
18 #include <linux/vmpressure.h>
19 #include <linux/eventfd.h>
20 #include <linux/mm.h>
21 #include <linux/vmstat.h>
22 #include <linux/writeback.h>
23 #include <linux/page-flags.h>
24
25 struct mem_cgroup;
26 struct page;
27 struct mm_struct;
28 struct kmem_cache;
29
30 /* Cgroup-specific page state, on top of universal node page state */
31 enum memcg_stat_item {
32         MEMCG_CACHE = NR_VM_NODE_STAT_ITEMS,
33         MEMCG_RSS,
34         MEMCG_RSS_HUGE,
35         MEMCG_SWAP,
36         MEMCG_SOCK,
37         /* XXX: why are these zone and not node counters? */
38         MEMCG_KERNEL_STACK_KB,
39         MEMCG_NR_STAT,
40 };
41
42 enum memcg_memory_event {
43         MEMCG_LOW,
44         MEMCG_HIGH,
45         MEMCG_MAX,
46         MEMCG_OOM,
47         MEMCG_OOM_KILL,
48         MEMCG_SWAP_MAX,
49         MEMCG_SWAP_FAIL,
50         MEMCG_NR_MEMORY_EVENTS,
51 };
52
53 enum mem_cgroup_protection {
54         MEMCG_PROT_NONE,
55         MEMCG_PROT_LOW,
56         MEMCG_PROT_MIN,
57 };
58
59 struct mem_cgroup_reclaim_cookie {
60         pg_data_t *pgdat;
61         int priority;
62         unsigned int generation;
63 };
64
65 #ifdef CONFIG_MEMCG
66
67 #define MEM_CGROUP_ID_SHIFT     16
68 #define MEM_CGROUP_ID_MAX       USHRT_MAX
69
70 struct mem_cgroup_id {
71         int id;
72         refcount_t ref;
73 };
74
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  */
81 enum 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
88 struct memcg_vmstats_percpu {
89         long stat[MEMCG_NR_STAT];
90         unsigned long events[NR_VM_EVENT_ITEMS];
91         unsigned long nr_page_events;
92         unsigned long targets[MEM_CGROUP_NTARGETS];
93 };
94
95 struct mem_cgroup_reclaim_iter {
96         struct mem_cgroup *position;
97         /* scan generation, increased every round-trip */
98         unsigned int generation;
99 };
100
101 struct lruvec_stat {
102         long count[NR_VM_NODE_STAT_ITEMS];
103 };
104
105 /*
106  * Bitmap of shrinker::id corresponding to memcg-aware shrinkers,
107  * which have elements charged to this memcg.
108  */
109 struct memcg_shrinker_map {
110         struct rcu_head rcu;
111         unsigned long map[0];
112 };
113
114 /*
115  * per-zone information in memory controller.
116  */
117 struct mem_cgroup_per_node {
118         struct lruvec           lruvec;
119
120         /* Legacy local VM stats */
121         struct lruvec_stat __percpu *lruvec_stat_local;
122
123         /* Subtree VM stats (batched updates) */
124         struct lruvec_stat __percpu *lruvec_stat_cpu;
125         atomic_long_t           lruvec_stat[NR_VM_NODE_STAT_ITEMS];
126
127         unsigned long           lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
128
129         struct mem_cgroup_reclaim_iter  iter[DEF_PRIORITY + 1];
130
131         struct memcg_shrinker_map __rcu *shrinker_map;
132
133         struct rb_node          tree_node;      /* RB tree node */
134         unsigned long           usage_in_excess;/* Set to the value by which */
135                                                 /* the soft limit is exceeded*/
136         bool                    on_tree;
137         bool                    congested;      /* memcg has many dirty pages */
138                                                 /* backed by a congested BDI */
139
140         struct mem_cgroup       *memcg;         /* Back pointer, we cannot */
141                                                 /* use container_of        */
142 };
143
144 struct mem_cgroup_threshold {
145         struct eventfd_ctx *eventfd;
146         unsigned long threshold;
147 };
148
149 /* For threshold */
150 struct mem_cgroup_threshold_ary {
151         /* An array index points to threshold just below or equal to usage. */
152         int current_threshold;
153         /* Size of entries[] */
154         unsigned int size;
155         /* Array of thresholds */
156         struct mem_cgroup_threshold entries[0];
157 };
158
159 struct mem_cgroup_thresholds {
160         /* Primary thresholds array */
161         struct mem_cgroup_threshold_ary *primary;
162         /*
163          * Spare threshold array.
164          * This is needed to make mem_cgroup_unregister_event() "never fail".
165          * It must be able to store at least primary->size - 1 entries.
166          */
167         struct mem_cgroup_threshold_ary *spare;
168 };
169
170 enum memcg_kmem_state {
171         KMEM_NONE,
172         KMEM_ALLOCATED,
173         KMEM_ONLINE,
174 };
175
176 #if defined(CONFIG_SMP)
177 struct memcg_padding {
178         char x[0];
179 } ____cacheline_internodealigned_in_smp;
180 #define MEMCG_PADDING(name)      struct memcg_padding name;
181 #else
182 #define MEMCG_PADDING(name)
183 #endif
184
185 /*
186  * Remember four most recent foreign writebacks with dirty pages in this
187  * cgroup.  Inode sharing is expected to be uncommon and, even if we miss
188  * one in a given round, we're likely to catch it later if it keeps
189  * foreign-dirtying, so a fairly low count should be enough.
190  *
191  * See mem_cgroup_track_foreign_dirty_slowpath() for details.
192  */
193 #define MEMCG_CGWB_FRN_CNT      4
194
195 struct memcg_cgwb_frn {
196         u64 bdi_id;                     /* bdi->id of the foreign inode */
197         int memcg_id;                   /* memcg->css.id of foreign inode */
198         u64 at;                         /* jiffies_64 at the time of dirtying */
199         struct wb_completion done;      /* tracks in-flight foreign writebacks */
200 };
201
202 /*
203  * The memory controller data structure. The memory controller controls both
204  * page cache and RSS per cgroup. We would eventually like to provide
205  * statistics based on the statistics developed by Rik Van Riel for clock-pro,
206  * to help the administrator determine what knobs to tune.
207  */
208 struct mem_cgroup {
209         struct cgroup_subsys_state css;
210
211         /* Private memcg ID. Used to ID objects that outlive the cgroup */
212         struct mem_cgroup_id id;
213
214         /* Accounted resources */
215         struct page_counter memory;
216         struct page_counter swap;
217
218         /* Legacy consumer-oriented counters */
219         struct page_counter memsw;
220         struct page_counter kmem;
221         struct page_counter tcpmem;
222
223         /* Upper bound of normal memory consumption range */
224         unsigned long high;
225
226         /* Range enforcement for interrupt charges */
227         struct work_struct high_work;
228
229         unsigned long soft_limit;
230
231         /* vmpressure notifications */
232         struct vmpressure vmpressure;
233
234         /*
235          * Should the accounting and control be hierarchical, per subtree?
236          */
237         bool use_hierarchy;
238
239         /*
240          * Should the OOM killer kill all belonging tasks, had it kill one?
241          */
242         bool oom_group;
243
244         /* protected by memcg_oom_lock */
245         bool            oom_lock;
246         int             under_oom;
247
248         int     swappiness;
249         /* OOM-Killer disable */
250         int             oom_kill_disable;
251
252         /* memory.events and memory.events.local */
253         struct cgroup_file events_file;
254         struct cgroup_file events_local_file;
255
256         /* handle for "memory.swap.events" */
257         struct cgroup_file swap_events_file;
258
259         /* protect arrays of thresholds */
260         struct mutex thresholds_lock;
261
262         /* thresholds for memory usage. RCU-protected */
263         struct mem_cgroup_thresholds thresholds;
264
265         /* thresholds for mem+swap usage. RCU-protected */
266         struct mem_cgroup_thresholds memsw_thresholds;
267
268         /* For oom notifier event fd */
269         struct list_head oom_notify;
270
271         /*
272          * Should we move charges of a task when a task is moved into this
273          * mem_cgroup ? And what type of charges should we move ?
274          */
275         unsigned long move_charge_at_immigrate;
276         /* taken only while moving_account > 0 */
277         spinlock_t              move_lock;
278         unsigned long           move_lock_flags;
279
280         MEMCG_PADDING(_pad1_);
281
282         /*
283          * set > 0 if pages under this cgroup are moving to other cgroup.
284          */
285         atomic_t                moving_account;
286         struct task_struct      *move_lock_task;
287
288         /* Legacy local VM stats and events */
289         struct memcg_vmstats_percpu __percpu *vmstats_local;
290
291         /* Subtree VM stats and events (batched updates) */
292         struct memcg_vmstats_percpu __percpu *vmstats_percpu;
293
294         MEMCG_PADDING(_pad2_);
295
296         atomic_long_t           vmstats[MEMCG_NR_STAT];
297         atomic_long_t           vmevents[NR_VM_EVENT_ITEMS];
298
299         /* memory.events */
300         atomic_long_t           memory_events[MEMCG_NR_MEMORY_EVENTS];
301         atomic_long_t           memory_events_local[MEMCG_NR_MEMORY_EVENTS];
302
303         unsigned long           socket_pressure;
304
305         /* Legacy tcp memory accounting */
306         bool                    tcpmem_active;
307         int                     tcpmem_pressure;
308
309 #ifdef CONFIG_MEMCG_KMEM
310         /* Index in the kmem_cache->memcg_params.memcg_caches array */
311         int kmemcg_id;
312         enum memcg_kmem_state kmem_state;
313         struct list_head kmem_caches;
314 #endif
315
316         int last_scanned_node;
317 #if MAX_NUMNODES > 1
318         nodemask_t      scan_nodes;
319         atomic_t        numainfo_events;
320         atomic_t        numainfo_updating;
321 #endif
322
323 #ifdef CONFIG_CGROUP_WRITEBACK
324         struct list_head cgwb_list;
325         struct wb_domain cgwb_domain;
326         struct memcg_cgwb_frn cgwb_frn[MEMCG_CGWB_FRN_CNT];
327 #endif
328
329         /* List of events which userspace want to receive */
330         struct list_head event_list;
331         spinlock_t event_list_lock;
332
333 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
334         struct deferred_split deferred_split_queue;
335 #endif
336
337         struct mem_cgroup_per_node *nodeinfo[0];
338         /* WARNING: nodeinfo must be the last member here */
339 };
340
341 /*
342  * size of first charge trial. "32" comes from vmscan.c's magic value.
343  * TODO: maybe necessary to use big numbers in big irons.
344  */
345 #define MEMCG_CHARGE_BATCH 32U
346
347 extern struct mem_cgroup *root_mem_cgroup;
348
349 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
350 {
351         return (memcg == root_mem_cgroup);
352 }
353
354 static inline bool mem_cgroup_disabled(void)
355 {
356         return !cgroup_subsys_enabled(memory_cgrp_subsys);
357 }
358
359 static inline void mem_cgroup_protection(struct mem_cgroup *root,
360                                          struct mem_cgroup *memcg,
361                                          unsigned long *min,
362                                          unsigned long *low)
363 {
364         *min = *low = 0;
365
366         if (mem_cgroup_disabled())
367                 return;
368
369         /*
370          * There is no reclaim protection applied to a targeted reclaim.
371          * We are special casing this specific case here because
372          * mem_cgroup_protected calculation is not robust enough to keep
373          * the protection invariant for calculated effective values for
374          * parallel reclaimers with different reclaim target. This is
375          * especially a problem for tail memcgs (as they have pages on LRU)
376          * which would want to have effective values 0 for targeted reclaim
377          * but a different value for external reclaim.
378          *
379          * Example
380          * Let's have global and A's reclaim in parallel:
381          *  |
382          *  A (low=2G, usage = 3G, max = 3G, children_low_usage = 1.5G)
383          *  |\
384          *  | C (low = 1G, usage = 2.5G)
385          *  B (low = 1G, usage = 0.5G)
386          *
387          * For the global reclaim
388          * A.elow = A.low
389          * B.elow = min(B.usage, B.low) because children_low_usage <= A.elow
390          * C.elow = min(C.usage, C.low)
391          *
392          * With the effective values resetting we have A reclaim
393          * A.elow = 0
394          * B.elow = B.low
395          * C.elow = C.low
396          *
397          * If the global reclaim races with A's reclaim then
398          * B.elow = C.elow = 0 because children_low_usage > A.elow)
399          * is possible and reclaiming B would be violating the protection.
400          *
401          */
402         if (root == memcg)
403                 return;
404
405         *min = READ_ONCE(memcg->memory.emin);
406         *low = READ_ONCE(memcg->memory.elow);
407 }
408
409 enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root,
410                                                 struct mem_cgroup *memcg);
411
412 int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
413                           gfp_t gfp_mask, struct mem_cgroup **memcgp,
414                           bool compound);
415 int mem_cgroup_try_charge_delay(struct page *page, struct mm_struct *mm,
416                           gfp_t gfp_mask, struct mem_cgroup **memcgp,
417                           bool compound);
418 void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
419                               bool lrucare, bool compound);
420 void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg,
421                 bool compound);
422 void mem_cgroup_uncharge(struct page *page);
423 void mem_cgroup_uncharge_list(struct list_head *page_list);
424
425 void mem_cgroup_migrate(struct page *oldpage, struct page *newpage);
426
427 static struct mem_cgroup_per_node *
428 mem_cgroup_nodeinfo(struct mem_cgroup *memcg, int nid)
429 {
430         return memcg->nodeinfo[nid];
431 }
432
433 /**
434  * mem_cgroup_lruvec - get the lru list vector for a node or a memcg zone
435  * @node: node of the wanted lruvec
436  * @memcg: memcg of the wanted lruvec
437  *
438  * Returns the lru list vector holding pages for a given @node or a given
439  * @memcg and @zone. This can be the node lruvec, if the memory controller
440  * is disabled.
441  */
442 static inline struct lruvec *mem_cgroup_lruvec(struct pglist_data *pgdat,
443                                 struct mem_cgroup *memcg)
444 {
445         struct mem_cgroup_per_node *mz;
446         struct lruvec *lruvec;
447
448         if (mem_cgroup_disabled()) {
449                 lruvec = node_lruvec(pgdat);
450                 goto out;
451         }
452
453         mz = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
454         lruvec = &mz->lruvec;
455 out:
456         /*
457          * Since a node can be onlined after the mem_cgroup was created,
458          * we have to be prepared to initialize lruvec->pgdat here;
459          * and if offlined then reonlined, we need to reinitialize it.
460          */
461         if (unlikely(lruvec->pgdat != pgdat))
462                 lruvec->pgdat = pgdat;
463         return lruvec;
464 }
465
466 struct lruvec *mem_cgroup_page_lruvec(struct page *, struct pglist_data *);
467
468 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
469
470 struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm);
471
472 struct mem_cgroup *get_mem_cgroup_from_page(struct page *page);
473
474 static inline
475 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
476         return css ? container_of(css, struct mem_cgroup, css) : NULL;
477 }
478
479 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
480 {
481         if (memcg)
482                 css_put(&memcg->css);
483 }
484
485 #define mem_cgroup_from_counter(counter, member)        \
486         container_of(counter, struct mem_cgroup, member)
487
488 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
489                                    struct mem_cgroup *,
490                                    struct mem_cgroup_reclaim_cookie *);
491 void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
492 int mem_cgroup_scan_tasks(struct mem_cgroup *,
493                           int (*)(struct task_struct *, void *), void *);
494
495 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
496 {
497         if (mem_cgroup_disabled())
498                 return 0;
499
500         return memcg->id.id;
501 }
502 struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
503
504 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
505 {
506         return mem_cgroup_from_css(seq_css(m));
507 }
508
509 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
510 {
511         struct mem_cgroup_per_node *mz;
512
513         if (mem_cgroup_disabled())
514                 return NULL;
515
516         mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
517         return mz->memcg;
518 }
519
520 /**
521  * parent_mem_cgroup - find the accounting parent of a memcg
522  * @memcg: memcg whose parent to find
523  *
524  * Returns the parent memcg, or NULL if this is the root or the memory
525  * controller is in legacy no-hierarchy mode.
526  */
527 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
528 {
529         if (!memcg->memory.parent)
530                 return NULL;
531         return mem_cgroup_from_counter(memcg->memory.parent, memory);
532 }
533
534 static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
535                               struct mem_cgroup *root)
536 {
537         if (root == memcg)
538                 return true;
539         if (!root->use_hierarchy)
540                 return false;
541         return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
542 }
543
544 static inline bool mm_match_cgroup(struct mm_struct *mm,
545                                    struct mem_cgroup *memcg)
546 {
547         struct mem_cgroup *task_memcg;
548         bool match = false;
549
550         rcu_read_lock();
551         task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
552         if (task_memcg)
553                 match = mem_cgroup_is_descendant(task_memcg, memcg);
554         rcu_read_unlock();
555         return match;
556 }
557
558 struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page);
559 ino_t page_cgroup_ino(struct page *page);
560
561 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
562 {
563         if (mem_cgroup_disabled())
564                 return true;
565         return !!(memcg->css.flags & CSS_ONLINE);
566 }
567
568 /*
569  * For memory reclaim.
570  */
571 int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
572
573 void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
574                 int zid, int nr_pages);
575
576 static inline
577 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
578                 enum lru_list lru, int zone_idx)
579 {
580         struct mem_cgroup_per_node *mz;
581
582         mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
583         return mz->lru_zone_size[zone_idx][lru];
584 }
585
586 void mem_cgroup_handle_over_high(void);
587
588 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);
589
590 unsigned long mem_cgroup_size(struct mem_cgroup *memcg);
591
592 void mem_cgroup_print_oom_context(struct mem_cgroup *memcg,
593                                 struct task_struct *p);
594
595 void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg);
596
597 static inline void mem_cgroup_enter_user_fault(void)
598 {
599         WARN_ON(current->in_user_fault);
600         current->in_user_fault = 1;
601 }
602
603 static inline void mem_cgroup_exit_user_fault(void)
604 {
605         WARN_ON(!current->in_user_fault);
606         current->in_user_fault = 0;
607 }
608
609 static inline bool task_in_memcg_oom(struct task_struct *p)
610 {
611         return p->memcg_in_oom;
612 }
613
614 bool mem_cgroup_oom_synchronize(bool wait);
615 struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
616                                             struct mem_cgroup *oom_domain);
617 void mem_cgroup_print_oom_group(struct mem_cgroup *memcg);
618
619 #ifdef CONFIG_MEMCG_SWAP
620 extern int do_swap_account;
621 #endif
622
623 struct mem_cgroup *lock_page_memcg(struct page *page);
624 void __unlock_page_memcg(struct mem_cgroup *memcg);
625 void unlock_page_memcg(struct page *page);
626
627 /*
628  * idx can be of type enum memcg_stat_item or node_stat_item.
629  * Keep in sync with memcg_exact_page_state().
630  */
631 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
632 {
633         long x = atomic_long_read(&memcg->vmstats[idx]);
634 #ifdef CONFIG_SMP
635         if (x < 0)
636                 x = 0;
637 #endif
638         return x;
639 }
640
641 /*
642  * idx can be of type enum memcg_stat_item or node_stat_item.
643  * Keep in sync with memcg_exact_page_state().
644  */
645 static inline unsigned long memcg_page_state_local(struct mem_cgroup *memcg,
646                                                    int idx)
647 {
648         long x = 0;
649         int cpu;
650
651         for_each_possible_cpu(cpu)
652                 x += per_cpu(memcg->vmstats_local->stat[idx], cpu);
653 #ifdef CONFIG_SMP
654         if (x < 0)
655                 x = 0;
656 #endif
657         return x;
658 }
659
660 void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val);
661
662 /* idx can be of type enum memcg_stat_item or node_stat_item */
663 static inline void mod_memcg_state(struct mem_cgroup *memcg,
664                                    int idx, int val)
665 {
666         unsigned long flags;
667
668         local_irq_save(flags);
669         __mod_memcg_state(memcg, idx, val);
670         local_irq_restore(flags);
671 }
672
673 /**
674  * mod_memcg_page_state - update page state statistics
675  * @page: the page
676  * @idx: page state item to account
677  * @val: number of pages (positive or negative)
678  *
679  * The @page must be locked or the caller must use lock_page_memcg()
680  * to prevent double accounting when the page is concurrently being
681  * moved to another memcg:
682  *
683  *   lock_page(page) or lock_page_memcg(page)
684  *   if (TestClearPageState(page))
685  *     mod_memcg_page_state(page, state, -1);
686  *   unlock_page(page) or unlock_page_memcg(page)
687  *
688  * Kernel pages are an exception to this, since they'll never move.
689  */
690 static inline void __mod_memcg_page_state(struct page *page,
691                                           int idx, int val)
692 {
693         if (page->mem_cgroup)
694                 __mod_memcg_state(page->mem_cgroup, idx, val);
695 }
696
697 static inline void mod_memcg_page_state(struct page *page,
698                                         int idx, int val)
699 {
700         if (page->mem_cgroup)
701                 mod_memcg_state(page->mem_cgroup, idx, val);
702 }
703
704 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
705                                               enum node_stat_item idx)
706 {
707         struct mem_cgroup_per_node *pn;
708         long x;
709
710         if (mem_cgroup_disabled())
711                 return node_page_state(lruvec_pgdat(lruvec), idx);
712
713         pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
714         x = atomic_long_read(&pn->lruvec_stat[idx]);
715 #ifdef CONFIG_SMP
716         if (x < 0)
717                 x = 0;
718 #endif
719         return x;
720 }
721
722 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
723                                                     enum node_stat_item idx)
724 {
725         struct mem_cgroup_per_node *pn;
726         long x = 0;
727         int cpu;
728
729         if (mem_cgroup_disabled())
730                 return node_page_state(lruvec_pgdat(lruvec), idx);
731
732         pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
733         for_each_possible_cpu(cpu)
734                 x += per_cpu(pn->lruvec_stat_local->count[idx], cpu);
735 #ifdef CONFIG_SMP
736         if (x < 0)
737                 x = 0;
738 #endif
739         return x;
740 }
741
742 void __mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
743                         int val);
744 void __mod_lruvec_slab_state(void *p, enum node_stat_item idx, int val);
745 void mod_memcg_obj_state(void *p, int idx, int val);
746
747 static inline void mod_lruvec_state(struct lruvec *lruvec,
748                                     enum node_stat_item idx, int val)
749 {
750         unsigned long flags;
751
752         local_irq_save(flags);
753         __mod_lruvec_state(lruvec, idx, val);
754         local_irq_restore(flags);
755 }
756
757 static inline void __mod_lruvec_page_state(struct page *page,
758                                            enum node_stat_item idx, int val)
759 {
760         pg_data_t *pgdat = page_pgdat(page);
761         struct lruvec *lruvec;
762
763         /* Untracked pages have no memcg, no lruvec. Update only the node */
764         if (!page->mem_cgroup) {
765                 __mod_node_page_state(pgdat, idx, val);
766                 return;
767         }
768
769         lruvec = mem_cgroup_lruvec(pgdat, page->mem_cgroup);
770         __mod_lruvec_state(lruvec, idx, val);
771 }
772
773 static inline void mod_lruvec_page_state(struct page *page,
774                                          enum node_stat_item idx, int val)
775 {
776         unsigned long flags;
777
778         local_irq_save(flags);
779         __mod_lruvec_page_state(page, idx, val);
780         local_irq_restore(flags);
781 }
782
783 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
784                                                 gfp_t gfp_mask,
785                                                 unsigned long *total_scanned);
786
787 void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
788                           unsigned long count);
789
790 static inline void count_memcg_events(struct mem_cgroup *memcg,
791                                       enum vm_event_item idx,
792                                       unsigned long count)
793 {
794         unsigned long flags;
795
796         local_irq_save(flags);
797         __count_memcg_events(memcg, idx, count);
798         local_irq_restore(flags);
799 }
800
801 static inline void count_memcg_page_event(struct page *page,
802                                           enum vm_event_item idx)
803 {
804         if (page->mem_cgroup)
805                 count_memcg_events(page->mem_cgroup, idx, 1);
806 }
807
808 static inline void count_memcg_event_mm(struct mm_struct *mm,
809                                         enum vm_event_item idx)
810 {
811         struct mem_cgroup *memcg;
812
813         if (mem_cgroup_disabled())
814                 return;
815
816         rcu_read_lock();
817         memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
818         if (likely(memcg))
819                 count_memcg_events(memcg, idx, 1);
820         rcu_read_unlock();
821 }
822
823 static inline void memcg_memory_event(struct mem_cgroup *memcg,
824                                       enum memcg_memory_event event)
825 {
826         atomic_long_inc(&memcg->memory_events_local[event]);
827         cgroup_file_notify(&memcg->events_local_file);
828
829         do {
830                 atomic_long_inc(&memcg->memory_events[event]);
831                 cgroup_file_notify(&memcg->events_file);
832
833                 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
834                         break;
835                 if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
836                         break;
837         } while ((memcg = parent_mem_cgroup(memcg)) &&
838                  !mem_cgroup_is_root(memcg));
839 }
840
841 static inline void memcg_memory_event_mm(struct mm_struct *mm,
842                                          enum memcg_memory_event event)
843 {
844         struct mem_cgroup *memcg;
845
846         if (mem_cgroup_disabled())
847                 return;
848
849         rcu_read_lock();
850         memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
851         if (likely(memcg))
852                 memcg_memory_event(memcg, event);
853         rcu_read_unlock();
854 }
855
856 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
857 void mem_cgroup_split_huge_fixup(struct page *head);
858 #endif
859
860 #else /* CONFIG_MEMCG */
861
862 #define MEM_CGROUP_ID_SHIFT     0
863 #define MEM_CGROUP_ID_MAX       0
864
865 struct mem_cgroup;
866
867 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
868 {
869         return true;
870 }
871
872 static inline bool mem_cgroup_disabled(void)
873 {
874         return true;
875 }
876
877 static inline void memcg_memory_event(struct mem_cgroup *memcg,
878                                       enum memcg_memory_event event)
879 {
880 }
881
882 static inline void memcg_memory_event_mm(struct mm_struct *mm,
883                                          enum memcg_memory_event event)
884 {
885 }
886
887 static inline void mem_cgroup_protection(struct mem_cgroup *root,
888                                          struct mem_cgroup *memcg,
889                                          unsigned long *min,
890                                          unsigned long *low)
891 {
892         *min = *low = 0;
893 }
894
895 static inline enum mem_cgroup_protection mem_cgroup_protected(
896         struct mem_cgroup *root, struct mem_cgroup *memcg)
897 {
898         return MEMCG_PROT_NONE;
899 }
900
901 static inline int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
902                                         gfp_t gfp_mask,
903                                         struct mem_cgroup **memcgp,
904                                         bool compound)
905 {
906         *memcgp = NULL;
907         return 0;
908 }
909
910 static inline int mem_cgroup_try_charge_delay(struct page *page,
911                                               struct mm_struct *mm,
912                                               gfp_t gfp_mask,
913                                               struct mem_cgroup **memcgp,
914                                               bool compound)
915 {
916         *memcgp = NULL;
917         return 0;
918 }
919
920 static inline void mem_cgroup_commit_charge(struct page *page,
921                                             struct mem_cgroup *memcg,
922                                             bool lrucare, bool compound)
923 {
924 }
925
926 static inline void mem_cgroup_cancel_charge(struct page *page,
927                                             struct mem_cgroup *memcg,
928                                             bool compound)
929 {
930 }
931
932 static inline void mem_cgroup_uncharge(struct page *page)
933 {
934 }
935
936 static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
937 {
938 }
939
940 static inline void mem_cgroup_migrate(struct page *old, struct page *new)
941 {
942 }
943
944 static inline struct lruvec *mem_cgroup_lruvec(struct pglist_data *pgdat,
945                                 struct mem_cgroup *memcg)
946 {
947         return node_lruvec(pgdat);
948 }
949
950 static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
951                                                     struct pglist_data *pgdat)
952 {
953         return &pgdat->lruvec;
954 }
955
956 static inline bool mm_match_cgroup(struct mm_struct *mm,
957                 struct mem_cgroup *memcg)
958 {
959         return true;
960 }
961
962 static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
963 {
964         return NULL;
965 }
966
967 static inline struct mem_cgroup *get_mem_cgroup_from_page(struct page *page)
968 {
969         return NULL;
970 }
971
972 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
973 {
974 }
975
976 static inline struct mem_cgroup *
977 mem_cgroup_iter(struct mem_cgroup *root,
978                 struct mem_cgroup *prev,
979                 struct mem_cgroup_reclaim_cookie *reclaim)
980 {
981         return NULL;
982 }
983
984 static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
985                                          struct mem_cgroup *prev)
986 {
987 }
988
989 static inline int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
990                 int (*fn)(struct task_struct *, void *), void *arg)
991 {
992         return 0;
993 }
994
995 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
996 {
997         return 0;
998 }
999
1000 static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
1001 {
1002         WARN_ON_ONCE(id);
1003         /* XXX: This should always return root_mem_cgroup */
1004         return NULL;
1005 }
1006
1007 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
1008 {
1009         return NULL;
1010 }
1011
1012 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
1013 {
1014         return NULL;
1015 }
1016
1017 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
1018 {
1019         return true;
1020 }
1021
1022 static inline
1023 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
1024                 enum lru_list lru, int zone_idx)
1025 {
1026         return 0;
1027 }
1028
1029 static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
1030 {
1031         return 0;
1032 }
1033
1034 static inline unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
1035 {
1036         return 0;
1037 }
1038
1039 static inline void
1040 mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
1041 {
1042 }
1043
1044 static inline void
1045 mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
1046 {
1047 }
1048
1049 static inline struct mem_cgroup *lock_page_memcg(struct page *page)
1050 {
1051         return NULL;
1052 }
1053
1054 static inline void __unlock_page_memcg(struct mem_cgroup *memcg)
1055 {
1056 }
1057
1058 static inline void unlock_page_memcg(struct page *page)
1059 {
1060 }
1061
1062 static inline void mem_cgroup_handle_over_high(void)
1063 {
1064 }
1065
1066 static inline void mem_cgroup_enter_user_fault(void)
1067 {
1068 }
1069
1070 static inline void mem_cgroup_exit_user_fault(void)
1071 {
1072 }
1073
1074 static inline bool task_in_memcg_oom(struct task_struct *p)
1075 {
1076         return false;
1077 }
1078
1079 static inline bool mem_cgroup_oom_synchronize(bool wait)
1080 {
1081         return false;
1082 }
1083
1084 static inline struct mem_cgroup *mem_cgroup_get_oom_group(
1085         struct task_struct *victim, struct mem_cgroup *oom_domain)
1086 {
1087         return NULL;
1088 }
1089
1090 static inline void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
1091 {
1092 }
1093
1094 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
1095 {
1096         return 0;
1097 }
1098
1099 static inline unsigned long memcg_page_state_local(struct mem_cgroup *memcg,
1100                                                    int idx)
1101 {
1102         return 0;
1103 }
1104
1105 static inline void __mod_memcg_state(struct mem_cgroup *memcg,
1106                                      int idx,
1107                                      int nr)
1108 {
1109 }
1110
1111 static inline void mod_memcg_state(struct mem_cgroup *memcg,
1112                                    int idx,
1113                                    int nr)
1114 {
1115 }
1116
1117 static inline void __mod_memcg_page_state(struct page *page,
1118                                           int idx,
1119                                           int nr)
1120 {
1121 }
1122
1123 static inline void mod_memcg_page_state(struct page *page,
1124                                         int idx,
1125                                         int nr)
1126 {
1127 }
1128
1129 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
1130                                               enum node_stat_item idx)
1131 {
1132         return node_page_state(lruvec_pgdat(lruvec), idx);
1133 }
1134
1135 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1136                                                     enum node_stat_item idx)
1137 {
1138         return node_page_state(lruvec_pgdat(lruvec), idx);
1139 }
1140
1141 static inline void __mod_lruvec_state(struct lruvec *lruvec,
1142                                       enum node_stat_item idx, int val)
1143 {
1144         __mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
1145 }
1146
1147 static inline void mod_lruvec_state(struct lruvec *lruvec,
1148                                     enum node_stat_item idx, int val)
1149 {
1150         mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
1151 }
1152
1153 static inline void __mod_lruvec_page_state(struct page *page,
1154                                            enum node_stat_item idx, int val)
1155 {
1156         __mod_node_page_state(page_pgdat(page), idx, val);
1157 }
1158
1159 static inline void mod_lruvec_page_state(struct page *page,
1160                                          enum node_stat_item idx, int val)
1161 {
1162         mod_node_page_state(page_pgdat(page), idx, val);
1163 }
1164
1165 static inline void __mod_lruvec_slab_state(void *p, enum node_stat_item idx,
1166                                            int val)
1167 {
1168         struct page *page = virt_to_head_page(p);
1169
1170         __mod_node_page_state(page_pgdat(page), idx, val);
1171 }
1172
1173 static inline void mod_memcg_obj_state(void *p, int idx, int val)
1174 {
1175 }
1176
1177 static inline
1178 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
1179                                             gfp_t gfp_mask,
1180                                             unsigned long *total_scanned)
1181 {
1182         return 0;
1183 }
1184
1185 static inline void mem_cgroup_split_huge_fixup(struct page *head)
1186 {
1187 }
1188
1189 static inline void count_memcg_events(struct mem_cgroup *memcg,
1190                                       enum vm_event_item idx,
1191                                       unsigned long count)
1192 {
1193 }
1194
1195 static inline void __count_memcg_events(struct mem_cgroup *memcg,
1196                                         enum vm_event_item idx,
1197                                         unsigned long count)
1198 {
1199 }
1200
1201 static inline void count_memcg_page_event(struct page *page,
1202                                           int idx)
1203 {
1204 }
1205
1206 static inline
1207 void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
1208 {
1209 }
1210 #endif /* CONFIG_MEMCG */
1211
1212 /* idx can be of type enum memcg_stat_item or node_stat_item */
1213 static inline void __inc_memcg_state(struct mem_cgroup *memcg,
1214                                      int idx)
1215 {
1216         __mod_memcg_state(memcg, idx, 1);
1217 }
1218
1219 /* idx can be of type enum memcg_stat_item or node_stat_item */
1220 static inline void __dec_memcg_state(struct mem_cgroup *memcg,
1221                                      int idx)
1222 {
1223         __mod_memcg_state(memcg, idx, -1);
1224 }
1225
1226 /* idx can be of type enum memcg_stat_item or node_stat_item */
1227 static inline void __inc_memcg_page_state(struct page *page,
1228                                           int idx)
1229 {
1230         __mod_memcg_page_state(page, idx, 1);
1231 }
1232
1233 /* idx can be of type enum memcg_stat_item or node_stat_item */
1234 static inline void __dec_memcg_page_state(struct page *page,
1235                                           int idx)
1236 {
1237         __mod_memcg_page_state(page, idx, -1);
1238 }
1239
1240 static inline void __inc_lruvec_state(struct lruvec *lruvec,
1241                                       enum node_stat_item idx)
1242 {
1243         __mod_lruvec_state(lruvec, idx, 1);
1244 }
1245
1246 static inline void __dec_lruvec_state(struct lruvec *lruvec,
1247                                       enum node_stat_item idx)
1248 {
1249         __mod_lruvec_state(lruvec, idx, -1);
1250 }
1251
1252 static inline void __inc_lruvec_page_state(struct page *page,
1253                                            enum node_stat_item idx)
1254 {
1255         __mod_lruvec_page_state(page, idx, 1);
1256 }
1257
1258 static inline void __dec_lruvec_page_state(struct page *page,
1259                                            enum node_stat_item idx)
1260 {
1261         __mod_lruvec_page_state(page, idx, -1);
1262 }
1263
1264 static inline void __inc_lruvec_slab_state(void *p, enum node_stat_item idx)
1265 {
1266         __mod_lruvec_slab_state(p, idx, 1);
1267 }
1268
1269 static inline void __dec_lruvec_slab_state(void *p, enum node_stat_item idx)
1270 {
1271         __mod_lruvec_slab_state(p, idx, -1);
1272 }
1273
1274 /* idx can be of type enum memcg_stat_item or node_stat_item */
1275 static inline void inc_memcg_state(struct mem_cgroup *memcg,
1276                                    int idx)
1277 {
1278         mod_memcg_state(memcg, idx, 1);
1279 }
1280
1281 /* idx can be of type enum memcg_stat_item or node_stat_item */
1282 static inline void dec_memcg_state(struct mem_cgroup *memcg,
1283                                    int idx)
1284 {
1285         mod_memcg_state(memcg, idx, -1);
1286 }
1287
1288 /* idx can be of type enum memcg_stat_item or node_stat_item */
1289 static inline void inc_memcg_page_state(struct page *page,
1290                                         int idx)
1291 {
1292         mod_memcg_page_state(page, idx, 1);
1293 }
1294
1295 /* idx can be of type enum memcg_stat_item or node_stat_item */
1296 static inline void dec_memcg_page_state(struct page *page,
1297                                         int idx)
1298 {
1299         mod_memcg_page_state(page, idx, -1);
1300 }
1301
1302 static inline void inc_lruvec_state(struct lruvec *lruvec,
1303                                     enum node_stat_item idx)
1304 {
1305         mod_lruvec_state(lruvec, idx, 1);
1306 }
1307
1308 static inline void dec_lruvec_state(struct lruvec *lruvec,
1309                                     enum node_stat_item idx)
1310 {
1311         mod_lruvec_state(lruvec, idx, -1);
1312 }
1313
1314 static inline void inc_lruvec_page_state(struct page *page,
1315                                          enum node_stat_item idx)
1316 {
1317         mod_lruvec_page_state(page, idx, 1);
1318 }
1319
1320 static inline void dec_lruvec_page_state(struct page *page,
1321                                          enum node_stat_item idx)
1322 {
1323         mod_lruvec_page_state(page, idx, -1);
1324 }
1325
1326 #ifdef CONFIG_CGROUP_WRITEBACK
1327
1328 struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
1329 void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
1330                          unsigned long *pheadroom, unsigned long *pdirty,
1331                          unsigned long *pwriteback);
1332
1333 void mem_cgroup_track_foreign_dirty_slowpath(struct page *page,
1334                                              struct bdi_writeback *wb);
1335
1336 static inline void mem_cgroup_track_foreign_dirty(struct page *page,
1337                                                   struct bdi_writeback *wb)
1338 {
1339         if (mem_cgroup_disabled())
1340                 return;
1341
1342         if (unlikely(&page->mem_cgroup->css != wb->memcg_css))
1343                 mem_cgroup_track_foreign_dirty_slowpath(page, wb);
1344 }
1345
1346 void mem_cgroup_flush_foreign(struct bdi_writeback *wb);
1347
1348 #else   /* CONFIG_CGROUP_WRITEBACK */
1349
1350 static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
1351 {
1352         return NULL;
1353 }
1354
1355 static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
1356                                        unsigned long *pfilepages,
1357                                        unsigned long *pheadroom,
1358                                        unsigned long *pdirty,
1359                                        unsigned long *pwriteback)
1360 {
1361 }
1362
1363 static inline void mem_cgroup_track_foreign_dirty(struct page *page,
1364                                                   struct bdi_writeback *wb)
1365 {
1366 }
1367
1368 static inline void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
1369 {
1370 }
1371
1372 #endif  /* CONFIG_CGROUP_WRITEBACK */
1373
1374 struct sock;
1375 bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1376 void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1377 #ifdef CONFIG_MEMCG
1378 extern struct static_key_false memcg_sockets_enabled_key;
1379 #define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
1380 void mem_cgroup_sk_alloc(struct sock *sk);
1381 void mem_cgroup_sk_free(struct sock *sk);
1382 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1383 {
1384         if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_pressure)
1385                 return true;
1386         do {
1387                 if (time_before(jiffies, memcg->socket_pressure))
1388                         return true;
1389         } while ((memcg = parent_mem_cgroup(memcg)));
1390         return false;
1391 }
1392
1393 extern int memcg_expand_shrinker_maps(int new_id);
1394
1395 extern void memcg_set_shrinker_bit(struct mem_cgroup *memcg,
1396                                    int nid, int shrinker_id);
1397 #else
1398 #define mem_cgroup_sockets_enabled 0
1399 static inline void mem_cgroup_sk_alloc(struct sock *sk) { };
1400 static inline void mem_cgroup_sk_free(struct sock *sk) { };
1401 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1402 {
1403         return false;
1404 }
1405
1406 static inline void memcg_set_shrinker_bit(struct mem_cgroup *memcg,
1407                                           int nid, int shrinker_id)
1408 {
1409 }
1410 #endif
1411
1412 struct kmem_cache *memcg_kmem_get_cache(struct kmem_cache *cachep);
1413 void memcg_kmem_put_cache(struct kmem_cache *cachep);
1414
1415 #ifdef CONFIG_MEMCG_KMEM
1416 int __memcg_kmem_charge(struct page *page, gfp_t gfp, int order);
1417 void __memcg_kmem_uncharge(struct page *page, int order);
1418 int __memcg_kmem_charge_memcg(struct page *page, gfp_t gfp, int order,
1419                               struct mem_cgroup *memcg);
1420 void __memcg_kmem_uncharge_memcg(struct mem_cgroup *memcg,
1421                                  unsigned int nr_pages);
1422
1423 extern struct static_key_false memcg_kmem_enabled_key;
1424 extern struct workqueue_struct *memcg_kmem_cache_wq;
1425
1426 extern int memcg_nr_cache_ids;
1427 void memcg_get_cache_ids(void);
1428 void memcg_put_cache_ids(void);
1429
1430 /*
1431  * Helper macro to loop through all memcg-specific caches. Callers must still
1432  * check if the cache is valid (it is either valid or NULL).
1433  * the slab_mutex must be held when looping through those caches
1434  */
1435 #define for_each_memcg_cache_index(_idx)        \
1436         for ((_idx) = 0; (_idx) < memcg_nr_cache_ids; (_idx)++)
1437
1438 static inline bool memcg_kmem_enabled(void)
1439 {
1440         return static_branch_unlikely(&memcg_kmem_enabled_key);
1441 }
1442
1443 static inline int memcg_kmem_charge(struct page *page, gfp_t gfp, int order)
1444 {
1445         if (memcg_kmem_enabled())
1446                 return __memcg_kmem_charge(page, gfp, order);
1447         return 0;
1448 }
1449
1450 static inline void memcg_kmem_uncharge(struct page *page, int order)
1451 {
1452         if (memcg_kmem_enabled())
1453                 __memcg_kmem_uncharge(page, order);
1454 }
1455
1456 static inline int memcg_kmem_charge_memcg(struct page *page, gfp_t gfp,
1457                                           int order, struct mem_cgroup *memcg)
1458 {
1459         if (memcg_kmem_enabled())
1460                 return __memcg_kmem_charge_memcg(page, gfp, order, memcg);
1461         return 0;
1462 }
1463
1464 static inline void memcg_kmem_uncharge_memcg(struct page *page, int order,
1465                                              struct mem_cgroup *memcg)
1466 {
1467         if (memcg_kmem_enabled())
1468                 __memcg_kmem_uncharge_memcg(memcg, 1 << order);
1469 }
1470
1471 /*
1472  * helper for accessing a memcg's index. It will be used as an index in the
1473  * child cache array in kmem_cache, and also to derive its name. This function
1474  * will return -1 when this is not a kmem-limited memcg.
1475  */
1476 static inline int memcg_cache_id(struct mem_cgroup *memcg)
1477 {
1478         return memcg ? memcg->kmemcg_id : -1;
1479 }
1480
1481 struct mem_cgroup *mem_cgroup_from_obj(void *p);
1482
1483 #else
1484
1485 static inline int memcg_kmem_charge(struct page *page, gfp_t gfp, int order)
1486 {
1487         return 0;
1488 }
1489
1490 static inline void memcg_kmem_uncharge(struct page *page, int order)
1491 {
1492 }
1493
1494 static inline int __memcg_kmem_charge(struct page *page, gfp_t gfp, int order)
1495 {
1496         return 0;
1497 }
1498
1499 static inline void __memcg_kmem_uncharge(struct page *page, int order)
1500 {
1501 }
1502
1503 #define for_each_memcg_cache_index(_idx)        \
1504         for (; NULL; )
1505
1506 static inline bool memcg_kmem_enabled(void)
1507 {
1508         return false;
1509 }
1510
1511 static inline int memcg_cache_id(struct mem_cgroup *memcg)
1512 {
1513         return -1;
1514 }
1515
1516 static inline void memcg_get_cache_ids(void)
1517 {
1518 }
1519
1520 static inline void memcg_put_cache_ids(void)
1521 {
1522 }
1523
1524 static inline struct mem_cgroup *mem_cgroup_from_obj(void *p)
1525 {
1526        return NULL;
1527 }
1528
1529 #endif /* CONFIG_MEMCG_KMEM */
1530
1531 #endif /* _LINUX_MEMCONTROL_H */