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