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