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