GNU Linux-libre 5.15.54-gnu
[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 obj_cgroup;
27 struct page;
28 struct mm_struct;
29 struct kmem_cache;
30
31 /* Cgroup-specific page state, on top of universal node page state */
32 enum memcg_stat_item {
33         MEMCG_SWAP = NR_VM_NODE_STAT_ITEMS,
34         MEMCG_SOCK,
35         MEMCG_PERCPU_B,
36         MEMCG_NR_STAT,
37 };
38
39 enum memcg_memory_event {
40         MEMCG_LOW,
41         MEMCG_HIGH,
42         MEMCG_MAX,
43         MEMCG_OOM,
44         MEMCG_OOM_KILL,
45         MEMCG_SWAP_HIGH,
46         MEMCG_SWAP_MAX,
47         MEMCG_SWAP_FAIL,
48         MEMCG_NR_MEMORY_EVENTS,
49 };
50
51 struct mem_cgroup_reclaim_cookie {
52         pg_data_t *pgdat;
53         unsigned int generation;
54 };
55
56 #ifdef CONFIG_MEMCG
57
58 #define MEM_CGROUP_ID_SHIFT     16
59 #define MEM_CGROUP_ID_MAX       USHRT_MAX
60
61 struct mem_cgroup_id {
62         int id;
63         refcount_t ref;
64 };
65
66 /*
67  * Per memcg event counter is incremented at every pagein/pageout. With THP,
68  * it will be incremented by the number of pages. This counter is used
69  * to trigger some periodic events. This is straightforward and better
70  * than using jiffies etc. to handle periodic memcg event.
71  */
72 enum mem_cgroup_events_target {
73         MEM_CGROUP_TARGET_THRESH,
74         MEM_CGROUP_TARGET_SOFTLIMIT,
75         MEM_CGROUP_NTARGETS,
76 };
77
78 struct memcg_vmstats_percpu {
79         /* Local (CPU and cgroup) page state & events */
80         long                    state[MEMCG_NR_STAT];
81         unsigned long           events[NR_VM_EVENT_ITEMS];
82
83         /* Delta calculation for lockless upward propagation */
84         long                    state_prev[MEMCG_NR_STAT];
85         unsigned long           events_prev[NR_VM_EVENT_ITEMS];
86
87         /* Cgroup1: threshold notifications & softlimit tree updates */
88         unsigned long           nr_page_events;
89         unsigned long           targets[MEM_CGROUP_NTARGETS];
90 };
91
92 struct memcg_vmstats {
93         /* Aggregated (CPU and subtree) page state & events */
94         long                    state[MEMCG_NR_STAT];
95         unsigned long           events[NR_VM_EVENT_ITEMS];
96
97         /* Pending child counts during tree propagation */
98         long                    state_pending[MEMCG_NR_STAT];
99         unsigned long           events_pending[NR_VM_EVENT_ITEMS];
100 };
101
102 struct mem_cgroup_reclaim_iter {
103         struct mem_cgroup *position;
104         /* scan generation, increased every round-trip */
105         unsigned int generation;
106 };
107
108 /*
109  * Bitmap and deferred work of shrinker::id corresponding to memcg-aware
110  * shrinkers, which have elements charged to this memcg.
111  */
112 struct shrinker_info {
113         struct rcu_head rcu;
114         atomic_long_t *nr_deferred;
115         unsigned long *map;
116 };
117
118 struct lruvec_stats_percpu {
119         /* Local (CPU and cgroup) state */
120         long state[NR_VM_NODE_STAT_ITEMS];
121
122         /* Delta calculation for lockless upward propagation */
123         long state_prev[NR_VM_NODE_STAT_ITEMS];
124 };
125
126 struct lruvec_stats {
127         /* Aggregated (CPU and subtree) state */
128         long state[NR_VM_NODE_STAT_ITEMS];
129
130         /* Pending child counts during tree propagation */
131         long state_pending[NR_VM_NODE_STAT_ITEMS];
132 };
133
134 /*
135  * per-node information in memory controller.
136  */
137 struct mem_cgroup_per_node {
138         struct lruvec           lruvec;
139
140         struct lruvec_stats_percpu __percpu     *lruvec_stats_percpu;
141         struct lruvec_stats                     lruvec_stats;
142
143         unsigned long           lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
144
145         struct mem_cgroup_reclaim_iter  iter;
146
147         struct shrinker_info __rcu      *shrinker_info;
148
149         struct rb_node          tree_node;      /* RB tree node */
150         unsigned long           usage_in_excess;/* Set to the value by which */
151                                                 /* the soft limit is exceeded*/
152         bool                    on_tree;
153         struct mem_cgroup       *memcg;         /* Back pointer, we cannot */
154                                                 /* use container_of        */
155 };
156
157 struct mem_cgroup_threshold {
158         struct eventfd_ctx *eventfd;
159         unsigned long threshold;
160 };
161
162 /* For threshold */
163 struct mem_cgroup_threshold_ary {
164         /* An array index points to threshold just below or equal to usage. */
165         int current_threshold;
166         /* Size of entries[] */
167         unsigned int size;
168         /* Array of thresholds */
169         struct mem_cgroup_threshold entries[];
170 };
171
172 struct mem_cgroup_thresholds {
173         /* Primary thresholds array */
174         struct mem_cgroup_threshold_ary *primary;
175         /*
176          * Spare threshold array.
177          * This is needed to make mem_cgroup_unregister_event() "never fail".
178          * It must be able to store at least primary->size - 1 entries.
179          */
180         struct mem_cgroup_threshold_ary *spare;
181 };
182
183 enum memcg_kmem_state {
184         KMEM_NONE,
185         KMEM_ALLOCATED,
186         KMEM_ONLINE,
187 };
188
189 #if defined(CONFIG_SMP)
190 struct memcg_padding {
191         char x[0];
192 } ____cacheline_internodealigned_in_smp;
193 #define MEMCG_PADDING(name)      struct memcg_padding name
194 #else
195 #define MEMCG_PADDING(name)
196 #endif
197
198 /*
199  * Remember four most recent foreign writebacks with dirty pages in this
200  * cgroup.  Inode sharing is expected to be uncommon and, even if we miss
201  * one in a given round, we're likely to catch it later if it keeps
202  * foreign-dirtying, so a fairly low count should be enough.
203  *
204  * See mem_cgroup_track_foreign_dirty_slowpath() for details.
205  */
206 #define MEMCG_CGWB_FRN_CNT      4
207
208 struct memcg_cgwb_frn {
209         u64 bdi_id;                     /* bdi->id of the foreign inode */
210         int memcg_id;                   /* memcg->css.id of foreign inode */
211         u64 at;                         /* jiffies_64 at the time of dirtying */
212         struct wb_completion done;      /* tracks in-flight foreign writebacks */
213 };
214
215 /*
216  * Bucket for arbitrarily byte-sized objects charged to a memory
217  * cgroup. The bucket can be reparented in one piece when the cgroup
218  * is destroyed, without having to round up the individual references
219  * of all live memory objects in the wild.
220  */
221 struct obj_cgroup {
222         struct percpu_ref refcnt;
223         struct mem_cgroup *memcg;
224         atomic_t nr_charged_bytes;
225         union {
226                 struct list_head list; /* protected by objcg_lock */
227                 struct rcu_head rcu;
228         };
229 };
230
231 /*
232  * The memory controller data structure. The memory controller controls both
233  * page cache and RSS per cgroup. We would eventually like to provide
234  * statistics based on the statistics developed by Rik Van Riel for clock-pro,
235  * to help the administrator determine what knobs to tune.
236  */
237 struct mem_cgroup {
238         struct cgroup_subsys_state css;
239
240         /* Private memcg ID. Used to ID objects that outlive the cgroup */
241         struct mem_cgroup_id id;
242
243         /* Accounted resources */
244         struct page_counter memory;             /* Both v1 & v2 */
245
246         union {
247                 struct page_counter swap;       /* v2 only */
248                 struct page_counter memsw;      /* v1 only */
249         };
250
251         /* Legacy consumer-oriented counters */
252         struct page_counter kmem;               /* v1 only */
253         struct page_counter tcpmem;             /* v1 only */
254
255         /* Range enforcement for interrupt charges */
256         struct work_struct high_work;
257
258         unsigned long soft_limit;
259
260         /* vmpressure notifications */
261         struct vmpressure vmpressure;
262
263         /*
264          * Should the OOM killer kill all belonging tasks, had it kill one?
265          */
266         bool oom_group;
267
268         /* protected by memcg_oom_lock */
269         bool            oom_lock;
270         int             under_oom;
271
272         int     swappiness;
273         /* OOM-Killer disable */
274         int             oom_kill_disable;
275
276         /* memory.events and memory.events.local */
277         struct cgroup_file events_file;
278         struct cgroup_file events_local_file;
279
280         /* handle for "memory.swap.events" */
281         struct cgroup_file swap_events_file;
282
283         /* protect arrays of thresholds */
284         struct mutex thresholds_lock;
285
286         /* thresholds for memory usage. RCU-protected */
287         struct mem_cgroup_thresholds thresholds;
288
289         /* thresholds for mem+swap usage. RCU-protected */
290         struct mem_cgroup_thresholds memsw_thresholds;
291
292         /* For oom notifier event fd */
293         struct list_head oom_notify;
294
295         /*
296          * Should we move charges of a task when a task is moved into this
297          * mem_cgroup ? And what type of charges should we move ?
298          */
299         unsigned long move_charge_at_immigrate;
300         /* taken only while moving_account > 0 */
301         spinlock_t              move_lock;
302         unsigned long           move_lock_flags;
303
304         MEMCG_PADDING(_pad1_);
305
306         /* memory.stat */
307         struct memcg_vmstats    vmstats;
308
309         /* memory.events */
310         atomic_long_t           memory_events[MEMCG_NR_MEMORY_EVENTS];
311         atomic_long_t           memory_events_local[MEMCG_NR_MEMORY_EVENTS];
312
313         unsigned long           socket_pressure;
314
315         /* Legacy tcp memory accounting */
316         bool                    tcpmem_active;
317         int                     tcpmem_pressure;
318
319 #ifdef CONFIG_MEMCG_KMEM
320         int kmemcg_id;
321         enum memcg_kmem_state kmem_state;
322         struct obj_cgroup __rcu *objcg;
323         /* list of inherited objcgs, protected by objcg_lock */
324         struct list_head objcg_list;
325 #endif
326
327         MEMCG_PADDING(_pad2_);
328
329         /*
330          * set > 0 if pages under this cgroup are moving to other cgroup.
331          */
332         atomic_t                moving_account;
333         struct task_struct      *move_lock_task;
334
335         struct memcg_vmstats_percpu __percpu *vmstats_percpu;
336
337 #ifdef CONFIG_CGROUP_WRITEBACK
338         struct list_head cgwb_list;
339         struct wb_domain cgwb_domain;
340         struct memcg_cgwb_frn cgwb_frn[MEMCG_CGWB_FRN_CNT];
341 #endif
342
343         /* List of events which userspace want to receive */
344         struct list_head event_list;
345         spinlock_t event_list_lock;
346
347 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
348         struct deferred_split deferred_split_queue;
349 #endif
350
351         struct mem_cgroup_per_node *nodeinfo[];
352 };
353
354 /*
355  * size of first charge trial. "32" comes from vmscan.c's magic value.
356  * TODO: maybe necessary to use big numbers in big irons.
357  */
358 #define MEMCG_CHARGE_BATCH 32U
359
360 extern struct mem_cgroup *root_mem_cgroup;
361
362 enum page_memcg_data_flags {
363         /* page->memcg_data is a pointer to an objcgs vector */
364         MEMCG_DATA_OBJCGS = (1UL << 0),
365         /* page has been accounted as a non-slab kernel page */
366         MEMCG_DATA_KMEM = (1UL << 1),
367         /* the next bit after the last actual flag */
368         __NR_MEMCG_DATA_FLAGS  = (1UL << 2),
369 };
370
371 #define MEMCG_DATA_FLAGS_MASK (__NR_MEMCG_DATA_FLAGS - 1)
372
373 static inline bool PageMemcgKmem(struct page *page);
374
375 /*
376  * After the initialization objcg->memcg is always pointing at
377  * a valid memcg, but can be atomically swapped to the parent memcg.
378  *
379  * The caller must ensure that the returned memcg won't be released:
380  * e.g. acquire the rcu_read_lock or css_set_lock.
381  */
382 static inline struct mem_cgroup *obj_cgroup_memcg(struct obj_cgroup *objcg)
383 {
384         return READ_ONCE(objcg->memcg);
385 }
386
387 /*
388  * __page_memcg - get the memory cgroup associated with a non-kmem page
389  * @page: a pointer to the page struct
390  *
391  * Returns a pointer to the memory cgroup associated with the page,
392  * or NULL. This function assumes that the page is known to have a
393  * proper memory cgroup pointer. It's not safe to call this function
394  * against some type of pages, e.g. slab pages or ex-slab pages or
395  * kmem pages.
396  */
397 static inline struct mem_cgroup *__page_memcg(struct page *page)
398 {
399         unsigned long memcg_data = page->memcg_data;
400
401         VM_BUG_ON_PAGE(PageSlab(page), page);
402         VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_OBJCGS, page);
403         VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, page);
404
405         return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
406 }
407
408 /*
409  * __page_objcg - get the object cgroup associated with a kmem page
410  * @page: a pointer to the page struct
411  *
412  * Returns a pointer to the object cgroup associated with the page,
413  * or NULL. This function assumes that the page is known to have a
414  * proper object cgroup pointer. It's not safe to call this function
415  * against some type of pages, e.g. slab pages or ex-slab pages or
416  * LRU pages.
417  */
418 static inline struct obj_cgroup *__page_objcg(struct page *page)
419 {
420         unsigned long memcg_data = page->memcg_data;
421
422         VM_BUG_ON_PAGE(PageSlab(page), page);
423         VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_OBJCGS, page);
424         VM_BUG_ON_PAGE(!(memcg_data & MEMCG_DATA_KMEM), page);
425
426         return (struct obj_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
427 }
428
429 /*
430  * page_memcg - get the memory cgroup associated with a page
431  * @page: a pointer to the page struct
432  *
433  * Returns a pointer to the memory cgroup associated with the page,
434  * or NULL. This function assumes that the page is known to have a
435  * proper memory cgroup pointer. It's not safe to call this function
436  * against some type of pages, e.g. slab pages or ex-slab pages.
437  *
438  * For a non-kmem page any of the following ensures page and memcg binding
439  * stability:
440  *
441  * - the page lock
442  * - LRU isolation
443  * - lock_page_memcg()
444  * - exclusive reference
445  *
446  * For a kmem page a caller should hold an rcu read lock to protect memcg
447  * associated with a kmem page from being released.
448  */
449 static inline struct mem_cgroup *page_memcg(struct page *page)
450 {
451         if (PageMemcgKmem(page))
452                 return obj_cgroup_memcg(__page_objcg(page));
453         else
454                 return __page_memcg(page);
455 }
456
457 /*
458  * page_memcg_rcu - locklessly get the memory cgroup associated with a page
459  * @page: a pointer to the page struct
460  *
461  * Returns a pointer to the memory cgroup associated with the page,
462  * or NULL. This function assumes that the page is known to have a
463  * proper memory cgroup pointer. It's not safe to call this function
464  * against some type of pages, e.g. slab pages or ex-slab pages.
465  */
466 static inline struct mem_cgroup *page_memcg_rcu(struct page *page)
467 {
468         unsigned long memcg_data = READ_ONCE(page->memcg_data);
469
470         VM_BUG_ON_PAGE(PageSlab(page), page);
471         WARN_ON_ONCE(!rcu_read_lock_held());
472
473         if (memcg_data & MEMCG_DATA_KMEM) {
474                 struct obj_cgroup *objcg;
475
476                 objcg = (void *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
477                 return obj_cgroup_memcg(objcg);
478         }
479
480         return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
481 }
482
483 /*
484  * page_memcg_check - get the memory cgroup associated with a page
485  * @page: a pointer to the page struct
486  *
487  * Returns a pointer to the memory cgroup associated with the page,
488  * or NULL. This function unlike page_memcg() can take any page
489  * as an argument. It has to be used in cases when it's not known if a page
490  * has an associated memory cgroup pointer or an object cgroups vector or
491  * an object cgroup.
492  *
493  * For a non-kmem page any of the following ensures page and memcg binding
494  * stability:
495  *
496  * - the page lock
497  * - LRU isolation
498  * - lock_page_memcg()
499  * - exclusive reference
500  *
501  * For a kmem page a caller should hold an rcu read lock to protect memcg
502  * associated with a kmem page from being released.
503  */
504 static inline struct mem_cgroup *page_memcg_check(struct page *page)
505 {
506         /*
507          * Because page->memcg_data might be changed asynchronously
508          * for slab pages, READ_ONCE() should be used here.
509          */
510         unsigned long memcg_data = READ_ONCE(page->memcg_data);
511
512         if (memcg_data & MEMCG_DATA_OBJCGS)
513                 return NULL;
514
515         if (memcg_data & MEMCG_DATA_KMEM) {
516                 struct obj_cgroup *objcg;
517
518                 objcg = (void *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
519                 return obj_cgroup_memcg(objcg);
520         }
521
522         return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
523 }
524
525 #ifdef CONFIG_MEMCG_KMEM
526 /*
527  * PageMemcgKmem - check if the page has MemcgKmem flag set
528  * @page: a pointer to the page struct
529  *
530  * Checks if the page has MemcgKmem flag set. The caller must ensure that
531  * the page has an associated memory cgroup. It's not safe to call this function
532  * against some types of pages, e.g. slab pages.
533  */
534 static inline bool PageMemcgKmem(struct page *page)
535 {
536         VM_BUG_ON_PAGE(page->memcg_data & MEMCG_DATA_OBJCGS, page);
537         return page->memcg_data & MEMCG_DATA_KMEM;
538 }
539
540 /*
541  * page_objcgs - get the object cgroups vector associated with a page
542  * @page: a pointer to the page struct
543  *
544  * Returns a pointer to the object cgroups vector associated with the page,
545  * or NULL. This function assumes that the page is known to have an
546  * associated object cgroups vector. It's not safe to call this function
547  * against pages, which might have an associated memory cgroup: e.g.
548  * kernel stack pages.
549  */
550 static inline struct obj_cgroup **page_objcgs(struct page *page)
551 {
552         unsigned long memcg_data = READ_ONCE(page->memcg_data);
553
554         VM_BUG_ON_PAGE(memcg_data && !(memcg_data & MEMCG_DATA_OBJCGS), page);
555         VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, page);
556
557         return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
558 }
559
560 /*
561  * page_objcgs_check - get the object cgroups vector associated with a page
562  * @page: a pointer to the page struct
563  *
564  * Returns a pointer to the object cgroups vector associated with the page,
565  * or NULL. This function is safe to use if the page can be directly associated
566  * with a memory cgroup.
567  */
568 static inline struct obj_cgroup **page_objcgs_check(struct page *page)
569 {
570         unsigned long memcg_data = READ_ONCE(page->memcg_data);
571
572         if (!memcg_data || !(memcg_data & MEMCG_DATA_OBJCGS))
573                 return NULL;
574
575         VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, page);
576
577         return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
578 }
579
580 #else
581 static inline bool PageMemcgKmem(struct page *page)
582 {
583         return false;
584 }
585
586 static inline struct obj_cgroup **page_objcgs(struct page *page)
587 {
588         return NULL;
589 }
590
591 static inline struct obj_cgroup **page_objcgs_check(struct page *page)
592 {
593         return NULL;
594 }
595 #endif
596
597 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
598 {
599         return (memcg == root_mem_cgroup);
600 }
601
602 static inline bool mem_cgroup_disabled(void)
603 {
604         return !cgroup_subsys_enabled(memory_cgrp_subsys);
605 }
606
607 static inline void mem_cgroup_protection(struct mem_cgroup *root,
608                                          struct mem_cgroup *memcg,
609                                          unsigned long *min,
610                                          unsigned long *low)
611 {
612         *min = *low = 0;
613
614         if (mem_cgroup_disabled())
615                 return;
616
617         /*
618          * There is no reclaim protection applied to a targeted reclaim.
619          * We are special casing this specific case here because
620          * mem_cgroup_protected calculation is not robust enough to keep
621          * the protection invariant for calculated effective values for
622          * parallel reclaimers with different reclaim target. This is
623          * especially a problem for tail memcgs (as they have pages on LRU)
624          * which would want to have effective values 0 for targeted reclaim
625          * but a different value for external reclaim.
626          *
627          * Example
628          * Let's have global and A's reclaim in parallel:
629          *  |
630          *  A (low=2G, usage = 3G, max = 3G, children_low_usage = 1.5G)
631          *  |\
632          *  | C (low = 1G, usage = 2.5G)
633          *  B (low = 1G, usage = 0.5G)
634          *
635          * For the global reclaim
636          * A.elow = A.low
637          * B.elow = min(B.usage, B.low) because children_low_usage <= A.elow
638          * C.elow = min(C.usage, C.low)
639          *
640          * With the effective values resetting we have A reclaim
641          * A.elow = 0
642          * B.elow = B.low
643          * C.elow = C.low
644          *
645          * If the global reclaim races with A's reclaim then
646          * B.elow = C.elow = 0 because children_low_usage > A.elow)
647          * is possible and reclaiming B would be violating the protection.
648          *
649          */
650         if (root == memcg)
651                 return;
652
653         *min = READ_ONCE(memcg->memory.emin);
654         *low = READ_ONCE(memcg->memory.elow);
655 }
656
657 void mem_cgroup_calculate_protection(struct mem_cgroup *root,
658                                      struct mem_cgroup *memcg);
659
660 static inline bool mem_cgroup_supports_protection(struct mem_cgroup *memcg)
661 {
662         /*
663          * The root memcg doesn't account charges, and doesn't support
664          * protection.
665          */
666         return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg);
667
668 }
669
670 static inline bool mem_cgroup_below_low(struct mem_cgroup *memcg)
671 {
672         if (!mem_cgroup_supports_protection(memcg))
673                 return false;
674
675         return READ_ONCE(memcg->memory.elow) >=
676                 page_counter_read(&memcg->memory);
677 }
678
679 static inline bool mem_cgroup_below_min(struct mem_cgroup *memcg)
680 {
681         if (!mem_cgroup_supports_protection(memcg))
682                 return false;
683
684         return READ_ONCE(memcg->memory.emin) >=
685                 page_counter_read(&memcg->memory);
686 }
687
688 int __mem_cgroup_charge(struct page *page, struct mm_struct *mm,
689                         gfp_t gfp_mask);
690 static inline int mem_cgroup_charge(struct page *page, struct mm_struct *mm,
691                                     gfp_t gfp_mask)
692 {
693         if (mem_cgroup_disabled())
694                 return 0;
695         return __mem_cgroup_charge(page, mm, gfp_mask);
696 }
697
698 int mem_cgroup_swapin_charge_page(struct page *page, struct mm_struct *mm,
699                                   gfp_t gfp, swp_entry_t entry);
700 void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry);
701
702 void __mem_cgroup_uncharge(struct page *page);
703 static inline void mem_cgroup_uncharge(struct page *page)
704 {
705         if (mem_cgroup_disabled())
706                 return;
707         __mem_cgroup_uncharge(page);
708 }
709
710 void __mem_cgroup_uncharge_list(struct list_head *page_list);
711 static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
712 {
713         if (mem_cgroup_disabled())
714                 return;
715         __mem_cgroup_uncharge_list(page_list);
716 }
717
718 void mem_cgroup_migrate(struct page *oldpage, struct page *newpage);
719
720 /**
721  * mem_cgroup_lruvec - get the lru list vector for a memcg & node
722  * @memcg: memcg of the wanted lruvec
723  * @pgdat: pglist_data
724  *
725  * Returns the lru list vector holding pages for a given @memcg &
726  * @pgdat combination. This can be the node lruvec, if the memory
727  * controller is disabled.
728  */
729 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
730                                                struct pglist_data *pgdat)
731 {
732         struct mem_cgroup_per_node *mz;
733         struct lruvec *lruvec;
734
735         if (mem_cgroup_disabled()) {
736                 lruvec = &pgdat->__lruvec;
737                 goto out;
738         }
739
740         if (!memcg)
741                 memcg = root_mem_cgroup;
742
743         mz = memcg->nodeinfo[pgdat->node_id];
744         lruvec = &mz->lruvec;
745 out:
746         /*
747          * Since a node can be onlined after the mem_cgroup was created,
748          * we have to be prepared to initialize lruvec->pgdat here;
749          * and if offlined then reonlined, we need to reinitialize it.
750          */
751         if (unlikely(lruvec->pgdat != pgdat))
752                 lruvec->pgdat = pgdat;
753         return lruvec;
754 }
755
756 /**
757  * mem_cgroup_page_lruvec - return lruvec for isolating/putting an LRU page
758  * @page: the page
759  *
760  * This function relies on page->mem_cgroup being stable.
761  */
762 static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page)
763 {
764         pg_data_t *pgdat = page_pgdat(page);
765         struct mem_cgroup *memcg = page_memcg(page);
766
767         VM_WARN_ON_ONCE_PAGE(!memcg && !mem_cgroup_disabled(), page);
768         return mem_cgroup_lruvec(memcg, pgdat);
769 }
770
771 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
772
773 struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm);
774
775 struct lruvec *lock_page_lruvec(struct page *page);
776 struct lruvec *lock_page_lruvec_irq(struct page *page);
777 struct lruvec *lock_page_lruvec_irqsave(struct page *page,
778                                                 unsigned long *flags);
779
780 #ifdef CONFIG_DEBUG_VM
781 void lruvec_memcg_debug(struct lruvec *lruvec, struct page *page);
782 #else
783 static inline void lruvec_memcg_debug(struct lruvec *lruvec, struct page *page)
784 {
785 }
786 #endif
787
788 static inline
789 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
790         return css ? container_of(css, struct mem_cgroup, css) : NULL;
791 }
792
793 static inline bool obj_cgroup_tryget(struct obj_cgroup *objcg)
794 {
795         return percpu_ref_tryget(&objcg->refcnt);
796 }
797
798 static inline void obj_cgroup_get(struct obj_cgroup *objcg)
799 {
800         percpu_ref_get(&objcg->refcnt);
801 }
802
803 static inline void obj_cgroup_get_many(struct obj_cgroup *objcg,
804                                        unsigned long nr)
805 {
806         percpu_ref_get_many(&objcg->refcnt, nr);
807 }
808
809 static inline void obj_cgroup_put(struct obj_cgroup *objcg)
810 {
811         percpu_ref_put(&objcg->refcnt);
812 }
813
814 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
815 {
816         if (memcg)
817                 css_put(&memcg->css);
818 }
819
820 #define mem_cgroup_from_counter(counter, member)        \
821         container_of(counter, struct mem_cgroup, member)
822
823 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
824                                    struct mem_cgroup *,
825                                    struct mem_cgroup_reclaim_cookie *);
826 void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
827 int mem_cgroup_scan_tasks(struct mem_cgroup *,
828                           int (*)(struct task_struct *, void *), void *);
829
830 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
831 {
832         if (mem_cgroup_disabled())
833                 return 0;
834
835         return memcg->id.id;
836 }
837 struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
838
839 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
840 {
841         return mem_cgroup_from_css(seq_css(m));
842 }
843
844 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
845 {
846         struct mem_cgroup_per_node *mz;
847
848         if (mem_cgroup_disabled())
849                 return NULL;
850
851         mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
852         return mz->memcg;
853 }
854
855 /**
856  * parent_mem_cgroup - find the accounting parent of a memcg
857  * @memcg: memcg whose parent to find
858  *
859  * Returns the parent memcg, or NULL if this is the root or the memory
860  * controller is in legacy no-hierarchy mode.
861  */
862 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
863 {
864         if (!memcg->memory.parent)
865                 return NULL;
866         return mem_cgroup_from_counter(memcg->memory.parent, memory);
867 }
868
869 static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
870                               struct mem_cgroup *root)
871 {
872         if (root == memcg)
873                 return true;
874         return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
875 }
876
877 static inline bool mm_match_cgroup(struct mm_struct *mm,
878                                    struct mem_cgroup *memcg)
879 {
880         struct mem_cgroup *task_memcg;
881         bool match = false;
882
883         rcu_read_lock();
884         task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
885         if (task_memcg)
886                 match = mem_cgroup_is_descendant(task_memcg, memcg);
887         rcu_read_unlock();
888         return match;
889 }
890
891 struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page);
892 ino_t page_cgroup_ino(struct page *page);
893
894 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
895 {
896         if (mem_cgroup_disabled())
897                 return true;
898         return !!(memcg->css.flags & CSS_ONLINE);
899 }
900
901 void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
902                 int zid, int nr_pages);
903
904 static inline
905 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
906                 enum lru_list lru, int zone_idx)
907 {
908         struct mem_cgroup_per_node *mz;
909
910         mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
911         return READ_ONCE(mz->lru_zone_size[zone_idx][lru]);
912 }
913
914 void mem_cgroup_handle_over_high(void);
915
916 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);
917
918 unsigned long mem_cgroup_size(struct mem_cgroup *memcg);
919
920 void mem_cgroup_print_oom_context(struct mem_cgroup *memcg,
921                                 struct task_struct *p);
922
923 void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg);
924
925 static inline void mem_cgroup_enter_user_fault(void)
926 {
927         WARN_ON(current->in_user_fault);
928         current->in_user_fault = 1;
929 }
930
931 static inline void mem_cgroup_exit_user_fault(void)
932 {
933         WARN_ON(!current->in_user_fault);
934         current->in_user_fault = 0;
935 }
936
937 static inline bool task_in_memcg_oom(struct task_struct *p)
938 {
939         return p->memcg_in_oom;
940 }
941
942 bool mem_cgroup_oom_synchronize(bool wait);
943 struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
944                                             struct mem_cgroup *oom_domain);
945 void mem_cgroup_print_oom_group(struct mem_cgroup *memcg);
946
947 #ifdef CONFIG_MEMCG_SWAP
948 extern bool cgroup_memory_noswap;
949 #endif
950
951 void lock_page_memcg(struct page *page);
952 void unlock_page_memcg(struct page *page);
953
954 void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val);
955
956 /* idx can be of type enum memcg_stat_item or node_stat_item */
957 static inline void mod_memcg_state(struct mem_cgroup *memcg,
958                                    int idx, int val)
959 {
960         unsigned long flags;
961
962         local_irq_save(flags);
963         __mod_memcg_state(memcg, idx, val);
964         local_irq_restore(flags);
965 }
966
967 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
968 {
969         return READ_ONCE(memcg->vmstats.state[idx]);
970 }
971
972 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
973                                               enum node_stat_item idx)
974 {
975         struct mem_cgroup_per_node *pn;
976
977         if (mem_cgroup_disabled())
978                 return node_page_state(lruvec_pgdat(lruvec), idx);
979
980         pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
981         return READ_ONCE(pn->lruvec_stats.state[idx]);
982 }
983
984 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
985                                                     enum node_stat_item idx)
986 {
987         struct mem_cgroup_per_node *pn;
988         long x = 0;
989         int cpu;
990
991         if (mem_cgroup_disabled())
992                 return node_page_state(lruvec_pgdat(lruvec), idx);
993
994         pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
995         for_each_possible_cpu(cpu)
996                 x += per_cpu(pn->lruvec_stats_percpu->state[idx], cpu);
997 #ifdef CONFIG_SMP
998         if (x < 0)
999                 x = 0;
1000 #endif
1001         return x;
1002 }
1003
1004 void mem_cgroup_flush_stats(void);
1005 void mem_cgroup_flush_stats_delayed(void);
1006
1007 void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
1008                               int val);
1009 void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val);
1010
1011 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1012                                          int val)
1013 {
1014         unsigned long flags;
1015
1016         local_irq_save(flags);
1017         __mod_lruvec_kmem_state(p, idx, val);
1018         local_irq_restore(flags);
1019 }
1020
1021 static inline void mod_memcg_lruvec_state(struct lruvec *lruvec,
1022                                           enum node_stat_item idx, int val)
1023 {
1024         unsigned long flags;
1025
1026         local_irq_save(flags);
1027         __mod_memcg_lruvec_state(lruvec, idx, val);
1028         local_irq_restore(flags);
1029 }
1030
1031 void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
1032                           unsigned long count);
1033
1034 static inline void count_memcg_events(struct mem_cgroup *memcg,
1035                                       enum vm_event_item idx,
1036                                       unsigned long count)
1037 {
1038         unsigned long flags;
1039
1040         local_irq_save(flags);
1041         __count_memcg_events(memcg, idx, count);
1042         local_irq_restore(flags);
1043 }
1044
1045 static inline void count_memcg_page_event(struct page *page,
1046                                           enum vm_event_item idx)
1047 {
1048         struct mem_cgroup *memcg = page_memcg(page);
1049
1050         if (memcg)
1051                 count_memcg_events(memcg, idx, 1);
1052 }
1053
1054 static inline void count_memcg_event_mm(struct mm_struct *mm,
1055                                         enum vm_event_item idx)
1056 {
1057         struct mem_cgroup *memcg;
1058
1059         if (mem_cgroup_disabled())
1060                 return;
1061
1062         rcu_read_lock();
1063         memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1064         if (likely(memcg))
1065                 count_memcg_events(memcg, idx, 1);
1066         rcu_read_unlock();
1067 }
1068
1069 static inline void memcg_memory_event(struct mem_cgroup *memcg,
1070                                       enum memcg_memory_event event)
1071 {
1072         bool swap_event = event == MEMCG_SWAP_HIGH || event == MEMCG_SWAP_MAX ||
1073                           event == MEMCG_SWAP_FAIL;
1074
1075         atomic_long_inc(&memcg->memory_events_local[event]);
1076         if (!swap_event)
1077                 cgroup_file_notify(&memcg->events_local_file);
1078
1079         do {
1080                 atomic_long_inc(&memcg->memory_events[event]);
1081                 if (swap_event)
1082                         cgroup_file_notify(&memcg->swap_events_file);
1083                 else
1084                         cgroup_file_notify(&memcg->events_file);
1085
1086                 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
1087                         break;
1088                 if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
1089                         break;
1090         } while ((memcg = parent_mem_cgroup(memcg)) &&
1091                  !mem_cgroup_is_root(memcg));
1092 }
1093
1094 static inline void memcg_memory_event_mm(struct mm_struct *mm,
1095                                          enum memcg_memory_event event)
1096 {
1097         struct mem_cgroup *memcg;
1098
1099         if (mem_cgroup_disabled())
1100                 return;
1101
1102         rcu_read_lock();
1103         memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1104         if (likely(memcg))
1105                 memcg_memory_event(memcg, event);
1106         rcu_read_unlock();
1107 }
1108
1109 void split_page_memcg(struct page *head, unsigned int nr);
1110
1111 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
1112                                                 gfp_t gfp_mask,
1113                                                 unsigned long *total_scanned);
1114
1115 #else /* CONFIG_MEMCG */
1116
1117 #define MEM_CGROUP_ID_SHIFT     0
1118 #define MEM_CGROUP_ID_MAX       0
1119
1120 static inline struct mem_cgroup *page_memcg(struct page *page)
1121 {
1122         return NULL;
1123 }
1124
1125 static inline struct mem_cgroup *page_memcg_rcu(struct page *page)
1126 {
1127         WARN_ON_ONCE(!rcu_read_lock_held());
1128         return NULL;
1129 }
1130
1131 static inline struct mem_cgroup *page_memcg_check(struct page *page)
1132 {
1133         return NULL;
1134 }
1135
1136 static inline bool PageMemcgKmem(struct page *page)
1137 {
1138         return false;
1139 }
1140
1141 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
1142 {
1143         return true;
1144 }
1145
1146 static inline bool mem_cgroup_disabled(void)
1147 {
1148         return true;
1149 }
1150
1151 static inline void memcg_memory_event(struct mem_cgroup *memcg,
1152                                       enum memcg_memory_event event)
1153 {
1154 }
1155
1156 static inline void memcg_memory_event_mm(struct mm_struct *mm,
1157                                          enum memcg_memory_event event)
1158 {
1159 }
1160
1161 static inline void mem_cgroup_protection(struct mem_cgroup *root,
1162                                          struct mem_cgroup *memcg,
1163                                          unsigned long *min,
1164                                          unsigned long *low)
1165 {
1166         *min = *low = 0;
1167 }
1168
1169 static inline void mem_cgroup_calculate_protection(struct mem_cgroup *root,
1170                                                    struct mem_cgroup *memcg)
1171 {
1172 }
1173
1174 static inline bool mem_cgroup_below_low(struct mem_cgroup *memcg)
1175 {
1176         return false;
1177 }
1178
1179 static inline bool mem_cgroup_below_min(struct mem_cgroup *memcg)
1180 {
1181         return false;
1182 }
1183
1184 static inline int mem_cgroup_charge(struct page *page, struct mm_struct *mm,
1185                                     gfp_t gfp_mask)
1186 {
1187         return 0;
1188 }
1189
1190 static inline int mem_cgroup_swapin_charge_page(struct page *page,
1191                         struct mm_struct *mm, gfp_t gfp, swp_entry_t entry)
1192 {
1193         return 0;
1194 }
1195
1196 static inline void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry)
1197 {
1198 }
1199
1200 static inline void mem_cgroup_uncharge(struct page *page)
1201 {
1202 }
1203
1204 static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
1205 {
1206 }
1207
1208 static inline void mem_cgroup_migrate(struct page *old, struct page *new)
1209 {
1210 }
1211
1212 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
1213                                                struct pglist_data *pgdat)
1214 {
1215         return &pgdat->__lruvec;
1216 }
1217
1218 static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page)
1219 {
1220         pg_data_t *pgdat = page_pgdat(page);
1221
1222         return &pgdat->__lruvec;
1223 }
1224
1225 static inline void lruvec_memcg_debug(struct lruvec *lruvec, struct page *page)
1226 {
1227 }
1228
1229 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
1230 {
1231         return NULL;
1232 }
1233
1234 static inline bool mm_match_cgroup(struct mm_struct *mm,
1235                 struct mem_cgroup *memcg)
1236 {
1237         return true;
1238 }
1239
1240 static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
1241 {
1242         return NULL;
1243 }
1244
1245 static inline
1246 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css)
1247 {
1248         return NULL;
1249 }
1250
1251 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
1252 {
1253 }
1254
1255 static inline struct lruvec *lock_page_lruvec(struct page *page)
1256 {
1257         struct pglist_data *pgdat = page_pgdat(page);
1258
1259         spin_lock(&pgdat->__lruvec.lru_lock);
1260         return &pgdat->__lruvec;
1261 }
1262
1263 static inline struct lruvec *lock_page_lruvec_irq(struct page *page)
1264 {
1265         struct pglist_data *pgdat = page_pgdat(page);
1266
1267         spin_lock_irq(&pgdat->__lruvec.lru_lock);
1268         return &pgdat->__lruvec;
1269 }
1270
1271 static inline struct lruvec *lock_page_lruvec_irqsave(struct page *page,
1272                 unsigned long *flagsp)
1273 {
1274         struct pglist_data *pgdat = page_pgdat(page);
1275
1276         spin_lock_irqsave(&pgdat->__lruvec.lru_lock, *flagsp);
1277         return &pgdat->__lruvec;
1278 }
1279
1280 static inline struct mem_cgroup *
1281 mem_cgroup_iter(struct mem_cgroup *root,
1282                 struct mem_cgroup *prev,
1283                 struct mem_cgroup_reclaim_cookie *reclaim)
1284 {
1285         return NULL;
1286 }
1287
1288 static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
1289                                          struct mem_cgroup *prev)
1290 {
1291 }
1292
1293 static inline int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
1294                 int (*fn)(struct task_struct *, void *), void *arg)
1295 {
1296         return 0;
1297 }
1298
1299 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
1300 {
1301         return 0;
1302 }
1303
1304 static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
1305 {
1306         WARN_ON_ONCE(id);
1307         /* XXX: This should always return root_mem_cgroup */
1308         return NULL;
1309 }
1310
1311 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
1312 {
1313         return NULL;
1314 }
1315
1316 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
1317 {
1318         return NULL;
1319 }
1320
1321 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
1322 {
1323         return true;
1324 }
1325
1326 static inline
1327 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
1328                 enum lru_list lru, int zone_idx)
1329 {
1330         return 0;
1331 }
1332
1333 static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
1334 {
1335         return 0;
1336 }
1337
1338 static inline unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
1339 {
1340         return 0;
1341 }
1342
1343 static inline void
1344 mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
1345 {
1346 }
1347
1348 static inline void
1349 mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
1350 {
1351 }
1352
1353 static inline void lock_page_memcg(struct page *page)
1354 {
1355 }
1356
1357 static inline void unlock_page_memcg(struct page *page)
1358 {
1359 }
1360
1361 static inline void mem_cgroup_handle_over_high(void)
1362 {
1363 }
1364
1365 static inline void mem_cgroup_enter_user_fault(void)
1366 {
1367 }
1368
1369 static inline void mem_cgroup_exit_user_fault(void)
1370 {
1371 }
1372
1373 static inline bool task_in_memcg_oom(struct task_struct *p)
1374 {
1375         return false;
1376 }
1377
1378 static inline bool mem_cgroup_oom_synchronize(bool wait)
1379 {
1380         return false;
1381 }
1382
1383 static inline struct mem_cgroup *mem_cgroup_get_oom_group(
1384         struct task_struct *victim, struct mem_cgroup *oom_domain)
1385 {
1386         return NULL;
1387 }
1388
1389 static inline void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
1390 {
1391 }
1392
1393 static inline void __mod_memcg_state(struct mem_cgroup *memcg,
1394                                      int idx,
1395                                      int nr)
1396 {
1397 }
1398
1399 static inline void mod_memcg_state(struct mem_cgroup *memcg,
1400                                    int idx,
1401                                    int nr)
1402 {
1403 }
1404
1405 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
1406 {
1407         return 0;
1408 }
1409
1410 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
1411                                               enum node_stat_item idx)
1412 {
1413         return node_page_state(lruvec_pgdat(lruvec), idx);
1414 }
1415
1416 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1417                                                     enum node_stat_item idx)
1418 {
1419         return node_page_state(lruvec_pgdat(lruvec), idx);
1420 }
1421
1422 static inline void mem_cgroup_flush_stats(void)
1423 {
1424 }
1425
1426 static inline void mem_cgroup_flush_stats_delayed(void)
1427 {
1428 }
1429
1430 static inline void __mod_memcg_lruvec_state(struct lruvec *lruvec,
1431                                             enum node_stat_item idx, int val)
1432 {
1433 }
1434
1435 static inline void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1436                                            int val)
1437 {
1438         struct page *page = virt_to_head_page(p);
1439
1440         __mod_node_page_state(page_pgdat(page), idx, val);
1441 }
1442
1443 static inline void mod_lruvec_kmem_state(void *p, enum node_stat_item idx,
1444                                          int val)
1445 {
1446         struct page *page = virt_to_head_page(p);
1447
1448         mod_node_page_state(page_pgdat(page), idx, val);
1449 }
1450
1451 static inline void count_memcg_events(struct mem_cgroup *memcg,
1452                                       enum vm_event_item idx,
1453                                       unsigned long count)
1454 {
1455 }
1456
1457 static inline void __count_memcg_events(struct mem_cgroup *memcg,
1458                                         enum vm_event_item idx,
1459                                         unsigned long count)
1460 {
1461 }
1462
1463 static inline void count_memcg_page_event(struct page *page,
1464                                           int idx)
1465 {
1466 }
1467
1468 static inline
1469 void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
1470 {
1471 }
1472
1473 static inline void split_page_memcg(struct page *head, unsigned int nr)
1474 {
1475 }
1476
1477 static inline
1478 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
1479                                             gfp_t gfp_mask,
1480                                             unsigned long *total_scanned)
1481 {
1482         return 0;
1483 }
1484 #endif /* CONFIG_MEMCG */
1485
1486 static inline void __inc_lruvec_kmem_state(void *p, enum node_stat_item idx)
1487 {
1488         __mod_lruvec_kmem_state(p, idx, 1);
1489 }
1490
1491 static inline void __dec_lruvec_kmem_state(void *p, enum node_stat_item idx)
1492 {
1493         __mod_lruvec_kmem_state(p, idx, -1);
1494 }
1495
1496 static inline struct lruvec *parent_lruvec(struct lruvec *lruvec)
1497 {
1498         struct mem_cgroup *memcg;
1499
1500         memcg = lruvec_memcg(lruvec);
1501         if (!memcg)
1502                 return NULL;
1503         memcg = parent_mem_cgroup(memcg);
1504         if (!memcg)
1505                 return NULL;
1506         return mem_cgroup_lruvec(memcg, lruvec_pgdat(lruvec));
1507 }
1508
1509 static inline void unlock_page_lruvec(struct lruvec *lruvec)
1510 {
1511         spin_unlock(&lruvec->lru_lock);
1512 }
1513
1514 static inline void unlock_page_lruvec_irq(struct lruvec *lruvec)
1515 {
1516         spin_unlock_irq(&lruvec->lru_lock);
1517 }
1518
1519 static inline void unlock_page_lruvec_irqrestore(struct lruvec *lruvec,
1520                 unsigned long flags)
1521 {
1522         spin_unlock_irqrestore(&lruvec->lru_lock, flags);
1523 }
1524
1525 /* Test requires a stable page->memcg binding, see page_memcg() */
1526 static inline bool page_matches_lruvec(struct page *page, struct lruvec *lruvec)
1527 {
1528         return lruvec_pgdat(lruvec) == page_pgdat(page) &&
1529                lruvec_memcg(lruvec) == page_memcg(page);
1530 }
1531
1532 /* Don't lock again iff page's lruvec locked */
1533 static inline struct lruvec *relock_page_lruvec_irq(struct page *page,
1534                 struct lruvec *locked_lruvec)
1535 {
1536         if (locked_lruvec) {
1537                 if (page_matches_lruvec(page, locked_lruvec))
1538                         return locked_lruvec;
1539
1540                 unlock_page_lruvec_irq(locked_lruvec);
1541         }
1542
1543         return lock_page_lruvec_irq(page);
1544 }
1545
1546 /* Don't lock again iff page's lruvec locked */
1547 static inline struct lruvec *relock_page_lruvec_irqsave(struct page *page,
1548                 struct lruvec *locked_lruvec, unsigned long *flags)
1549 {
1550         if (locked_lruvec) {
1551                 if (page_matches_lruvec(page, locked_lruvec))
1552                         return locked_lruvec;
1553
1554                 unlock_page_lruvec_irqrestore(locked_lruvec, *flags);
1555         }
1556
1557         return lock_page_lruvec_irqsave(page, flags);
1558 }
1559
1560 #ifdef CONFIG_CGROUP_WRITEBACK
1561
1562 struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
1563 void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
1564                          unsigned long *pheadroom, unsigned long *pdirty,
1565                          unsigned long *pwriteback);
1566
1567 void mem_cgroup_track_foreign_dirty_slowpath(struct page *page,
1568                                              struct bdi_writeback *wb);
1569
1570 static inline void mem_cgroup_track_foreign_dirty(struct page *page,
1571                                                   struct bdi_writeback *wb)
1572 {
1573         if (mem_cgroup_disabled())
1574                 return;
1575
1576         if (unlikely(&page_memcg(page)->css != wb->memcg_css))
1577                 mem_cgroup_track_foreign_dirty_slowpath(page, wb);
1578 }
1579
1580 void mem_cgroup_flush_foreign(struct bdi_writeback *wb);
1581
1582 #else   /* CONFIG_CGROUP_WRITEBACK */
1583
1584 static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
1585 {
1586         return NULL;
1587 }
1588
1589 static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
1590                                        unsigned long *pfilepages,
1591                                        unsigned long *pheadroom,
1592                                        unsigned long *pdirty,
1593                                        unsigned long *pwriteback)
1594 {
1595 }
1596
1597 static inline void mem_cgroup_track_foreign_dirty(struct page *page,
1598                                                   struct bdi_writeback *wb)
1599 {
1600 }
1601
1602 static inline void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
1603 {
1604 }
1605
1606 #endif  /* CONFIG_CGROUP_WRITEBACK */
1607
1608 struct sock;
1609 bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
1610                              gfp_t gfp_mask);
1611 void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1612 #ifdef CONFIG_MEMCG
1613 extern struct static_key_false memcg_sockets_enabled_key;
1614 #define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
1615 void mem_cgroup_sk_alloc(struct sock *sk);
1616 void mem_cgroup_sk_free(struct sock *sk);
1617 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1618 {
1619         if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_pressure)
1620                 return true;
1621         do {
1622                 if (time_before(jiffies, memcg->socket_pressure))
1623                         return true;
1624         } while ((memcg = parent_mem_cgroup(memcg)));
1625         return false;
1626 }
1627
1628 int alloc_shrinker_info(struct mem_cgroup *memcg);
1629 void free_shrinker_info(struct mem_cgroup *memcg);
1630 void set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id);
1631 void reparent_shrinker_deferred(struct mem_cgroup *memcg);
1632 #else
1633 #define mem_cgroup_sockets_enabled 0
1634 static inline void mem_cgroup_sk_alloc(struct sock *sk) { };
1635 static inline void mem_cgroup_sk_free(struct sock *sk) { };
1636 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1637 {
1638         return false;
1639 }
1640
1641 static inline void set_shrinker_bit(struct mem_cgroup *memcg,
1642                                     int nid, int shrinker_id)
1643 {
1644 }
1645 #endif
1646
1647 #ifdef CONFIG_MEMCG_KMEM
1648 bool mem_cgroup_kmem_disabled(void);
1649 int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order);
1650 void __memcg_kmem_uncharge_page(struct page *page, int order);
1651
1652 struct obj_cgroup *get_obj_cgroup_from_current(void);
1653
1654 int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size);
1655 void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size);
1656
1657 extern struct static_key_false memcg_kmem_enabled_key;
1658
1659 extern int memcg_nr_cache_ids;
1660 void memcg_get_cache_ids(void);
1661 void memcg_put_cache_ids(void);
1662
1663 /*
1664  * Helper macro to loop through all memcg-specific caches. Callers must still
1665  * check if the cache is valid (it is either valid or NULL).
1666  * the slab_mutex must be held when looping through those caches
1667  */
1668 #define for_each_memcg_cache_index(_idx)        \
1669         for ((_idx) = 0; (_idx) < memcg_nr_cache_ids; (_idx)++)
1670
1671 static inline bool memcg_kmem_enabled(void)
1672 {
1673         return static_branch_likely(&memcg_kmem_enabled_key);
1674 }
1675
1676 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1677                                          int order)
1678 {
1679         if (memcg_kmem_enabled())
1680                 return __memcg_kmem_charge_page(page, gfp, order);
1681         return 0;
1682 }
1683
1684 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1685 {
1686         if (memcg_kmem_enabled())
1687                 __memcg_kmem_uncharge_page(page, order);
1688 }
1689
1690 /*
1691  * A helper for accessing memcg's kmem_id, used for getting
1692  * corresponding LRU lists.
1693  */
1694 static inline int memcg_cache_id(struct mem_cgroup *memcg)
1695 {
1696         return memcg ? memcg->kmemcg_id : -1;
1697 }
1698
1699 struct mem_cgroup *mem_cgroup_from_obj(void *p);
1700
1701 #else
1702 static inline bool mem_cgroup_kmem_disabled(void)
1703 {
1704         return true;
1705 }
1706
1707 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1708                                          int order)
1709 {
1710         return 0;
1711 }
1712
1713 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1714 {
1715 }
1716
1717 static inline int __memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1718                                            int order)
1719 {
1720         return 0;
1721 }
1722
1723 static inline void __memcg_kmem_uncharge_page(struct page *page, int order)
1724 {
1725 }
1726
1727 #define for_each_memcg_cache_index(_idx)        \
1728         for (; NULL; )
1729
1730 static inline bool memcg_kmem_enabled(void)
1731 {
1732         return false;
1733 }
1734
1735 static inline int memcg_cache_id(struct mem_cgroup *memcg)
1736 {
1737         return -1;
1738 }
1739
1740 static inline void memcg_get_cache_ids(void)
1741 {
1742 }
1743
1744 static inline void memcg_put_cache_ids(void)
1745 {
1746 }
1747
1748 static inline struct mem_cgroup *mem_cgroup_from_obj(void *p)
1749 {
1750        return NULL;
1751 }
1752
1753 #endif /* CONFIG_MEMCG_KMEM */
1754
1755 #endif /* _LINUX_MEMCONTROL_H */