• 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;
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 	struct list_head objcg_list; /* list of inherited objcgs */
317 #endif
318 
319 	MEMCG_PADDING(_pad2_);
320 
321 	/*
322 	 * set > 0 if pages under this cgroup are moving to other cgroup.
323 	 */
324 	atomic_t		moving_account;
325 	struct task_struct	*move_lock_task;
326 
327 	/* Legacy local VM stats and events */
328 	struct memcg_vmstats_percpu __percpu *vmstats_local;
329 
330 	/* Subtree VM stats and events (batched updates) */
331 	struct memcg_vmstats_percpu __percpu *vmstats_percpu;
332 
333 #ifdef CONFIG_CGROUP_WRITEBACK
334 	struct list_head cgwb_list;
335 	struct wb_domain cgwb_domain;
336 	struct memcg_cgwb_frn cgwb_frn[MEMCG_CGWB_FRN_CNT];
337 #endif
338 
339 	/* List of events which userspace want to receive */
340 	struct list_head event_list;
341 	spinlock_t event_list_lock;
342 
343 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
344 	struct deferred_split deferred_split_queue;
345 #endif
346 
347 	struct mem_cgroup_per_node *nodeinfo[0];
348 	/* WARNING: nodeinfo must be the last member here */
349 };
350 
351 /*
352  * size of first charge trial. "32" comes from vmscan.c's magic value.
353  * TODO: maybe necessary to use big numbers in big irons.
354  */
355 #define MEMCG_CHARGE_BATCH 32U
356 
357 extern struct mem_cgroup *root_mem_cgroup;
358 
memcg_stat_item_in_bytes(int idx)359 static __always_inline bool memcg_stat_item_in_bytes(int idx)
360 {
361 	if (idx == MEMCG_PERCPU_B)
362 		return true;
363 	return vmstat_item_in_bytes(idx);
364 }
365 
mem_cgroup_is_root(struct mem_cgroup * memcg)366 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
367 {
368 	return (memcg == root_mem_cgroup);
369 }
370 
mem_cgroup_disabled(void)371 static inline bool mem_cgroup_disabled(void)
372 {
373 	return !cgroup_subsys_enabled(memory_cgrp_subsys);
374 }
375 
mem_cgroup_protection(struct mem_cgroup * root,struct mem_cgroup * memcg,unsigned long * min,unsigned long * low)376 static inline void mem_cgroup_protection(struct mem_cgroup *root,
377 					 struct mem_cgroup *memcg,
378 					 unsigned long *min,
379 					 unsigned long *low)
380 {
381 	*min = *low = 0;
382 
383 	if (mem_cgroup_disabled())
384 		return;
385 
386 	/*
387 	 * There is no reclaim protection applied to a targeted reclaim.
388 	 * We are special casing this specific case here because
389 	 * mem_cgroup_protected calculation is not robust enough to keep
390 	 * the protection invariant for calculated effective values for
391 	 * parallel reclaimers with different reclaim target. This is
392 	 * especially a problem for tail memcgs (as they have pages on LRU)
393 	 * which would want to have effective values 0 for targeted reclaim
394 	 * but a different value for external reclaim.
395 	 *
396 	 * Example
397 	 * Let's have global and A's reclaim in parallel:
398 	 *  |
399 	 *  A (low=2G, usage = 3G, max = 3G, children_low_usage = 1.5G)
400 	 *  |\
401 	 *  | C (low = 1G, usage = 2.5G)
402 	 *  B (low = 1G, usage = 0.5G)
403 	 *
404 	 * For the global reclaim
405 	 * A.elow = A.low
406 	 * B.elow = min(B.usage, B.low) because children_low_usage <= A.elow
407 	 * C.elow = min(C.usage, C.low)
408 	 *
409 	 * With the effective values resetting we have A reclaim
410 	 * A.elow = 0
411 	 * B.elow = B.low
412 	 * C.elow = C.low
413 	 *
414 	 * If the global reclaim races with A's reclaim then
415 	 * B.elow = C.elow = 0 because children_low_usage > A.elow)
416 	 * is possible and reclaiming B would be violating the protection.
417 	 *
418 	 */
419 	if (root == memcg)
420 		return;
421 
422 	*min = READ_ONCE(memcg->memory.emin);
423 	*low = READ_ONCE(memcg->memory.elow);
424 }
425 
426 void mem_cgroup_calculate_protection(struct mem_cgroup *root,
427 				     struct mem_cgroup *memcg);
428 
mem_cgroup_supports_protection(struct mem_cgroup * memcg)429 static inline bool mem_cgroup_supports_protection(struct mem_cgroup *memcg)
430 {
431 	/*
432 	 * The root memcg doesn't account charges, and doesn't support
433 	 * protection.
434 	 */
435 	return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg);
436 
437 }
438 
mem_cgroup_below_low(struct mem_cgroup * memcg)439 static inline bool mem_cgroup_below_low(struct mem_cgroup *memcg)
440 {
441 	if (!mem_cgroup_supports_protection(memcg))
442 		return false;
443 
444 	return READ_ONCE(memcg->memory.elow) >=
445 		page_counter_read(&memcg->memory);
446 }
447 
mem_cgroup_below_min(struct mem_cgroup * memcg)448 static inline bool mem_cgroup_below_min(struct mem_cgroup *memcg)
449 {
450 	if (!mem_cgroup_supports_protection(memcg))
451 		return false;
452 
453 	return READ_ONCE(memcg->memory.emin) >=
454 		page_counter_read(&memcg->memory);
455 }
456 
457 int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask);
458 
459 void mem_cgroup_uncharge(struct page *page);
460 void mem_cgroup_uncharge_list(struct list_head *page_list);
461 
462 void mem_cgroup_migrate(struct page *oldpage, struct page *newpage);
463 
464 static struct mem_cgroup_per_node *
mem_cgroup_nodeinfo(struct mem_cgroup * memcg,int nid)465 mem_cgroup_nodeinfo(struct mem_cgroup *memcg, int nid)
466 {
467 	return memcg->nodeinfo[nid];
468 }
469 
470 /**
471  * mem_cgroup_lruvec - get the lru list vector for a memcg & node
472  * @memcg: memcg of the wanted lruvec
473  *
474  * Returns the lru list vector holding pages for a given @memcg &
475  * @node combination. This can be the node lruvec, if the memory
476  * controller is disabled.
477  */
mem_cgroup_lruvec(struct mem_cgroup * memcg,struct pglist_data * pgdat)478 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
479 					       struct pglist_data *pgdat)
480 {
481 	struct mem_cgroup_per_node *mz;
482 	struct lruvec *lruvec;
483 
484 	if (mem_cgroup_disabled()) {
485 		lruvec = &pgdat->__lruvec;
486 		goto out;
487 	}
488 
489 	if (!memcg)
490 		memcg = root_mem_cgroup;
491 
492 	mz = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
493 	lruvec = &mz->lruvec;
494 out:
495 	/*
496 	 * Since a node can be onlined after the mem_cgroup was created,
497 	 * we have to be prepared to initialize lruvec->pgdat here;
498 	 * and if offlined then reonlined, we need to reinitialize it.
499 	 */
500 	if (unlikely(lruvec->pgdat != pgdat))
501 		lruvec->pgdat = pgdat;
502 	return lruvec;
503 }
504 
505 struct lruvec *mem_cgroup_page_lruvec(struct page *, struct pglist_data *);
506 
507 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
508 
509 struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm);
510 
511 struct mem_cgroup *get_mem_cgroup_from_page(struct page *page);
512 
513 static inline
mem_cgroup_from_css(struct cgroup_subsys_state * css)514 struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
515 	return css ? container_of(css, struct mem_cgroup, css) : NULL;
516 }
517 
obj_cgroup_tryget(struct obj_cgroup * objcg)518 static inline bool obj_cgroup_tryget(struct obj_cgroup *objcg)
519 {
520 	return percpu_ref_tryget(&objcg->refcnt);
521 }
522 
obj_cgroup_get(struct obj_cgroup * objcg)523 static inline void obj_cgroup_get(struct obj_cgroup *objcg)
524 {
525 	percpu_ref_get(&objcg->refcnt);
526 }
527 
obj_cgroup_put(struct obj_cgroup * objcg)528 static inline void obj_cgroup_put(struct obj_cgroup *objcg)
529 {
530 	percpu_ref_put(&objcg->refcnt);
531 }
532 
533 /*
534  * After the initialization objcg->memcg is always pointing at
535  * a valid memcg, but can be atomically swapped to the parent memcg.
536  *
537  * The caller must ensure that the returned memcg won't be released:
538  * e.g. acquire the rcu_read_lock or css_set_lock.
539  */
obj_cgroup_memcg(struct obj_cgroup * objcg)540 static inline struct mem_cgroup *obj_cgroup_memcg(struct obj_cgroup *objcg)
541 {
542 	return READ_ONCE(objcg->memcg);
543 }
544 
mem_cgroup_put(struct mem_cgroup * memcg)545 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
546 {
547 	if (memcg)
548 		css_put(&memcg->css);
549 }
550 
551 #define mem_cgroup_from_counter(counter, member)	\
552 	container_of(counter, struct mem_cgroup, member)
553 
554 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
555 				   struct mem_cgroup *,
556 				   struct mem_cgroup_reclaim_cookie *);
557 void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
558 int mem_cgroup_scan_tasks(struct mem_cgroup *,
559 			  int (*)(struct task_struct *, void *), void *);
560 
mem_cgroup_id(struct mem_cgroup * memcg)561 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
562 {
563 	if (mem_cgroup_disabled())
564 		return 0;
565 #ifdef CONFIG_HYPERHOLD_FILE_LRU
566 	if (!memcg)
567 		return -1;
568 #endif
569 
570 	return memcg->id.id;
571 }
572 struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
573 
mem_cgroup_from_seq(struct seq_file * m)574 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
575 {
576 	return mem_cgroup_from_css(seq_css(m));
577 }
578 
lruvec_memcg(struct lruvec * lruvec)579 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
580 {
581 	struct mem_cgroup_per_node *mz;
582 
583 	if (mem_cgroup_disabled())
584 		return NULL;
585 
586 #ifdef CONFIG_HYPERHOLD_FILE_LRU
587 	if (is_node_lruvec(lruvec))
588 		return NULL;
589 #endif
590 
591 	mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
592 	return mz->memcg;
593 }
594 
595 /**
596  * parent_mem_cgroup - find the accounting parent of a memcg
597  * @memcg: memcg whose parent to find
598  *
599  * Returns the parent memcg, or NULL if this is the root or the memory
600  * controller is in legacy no-hierarchy mode.
601  */
parent_mem_cgroup(struct mem_cgroup * memcg)602 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
603 {
604 	if (!memcg->memory.parent)
605 		return NULL;
606 	return mem_cgroup_from_counter(memcg->memory.parent, memory);
607 }
608 
mem_cgroup_is_descendant(struct mem_cgroup * memcg,struct mem_cgroup * root)609 static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
610 			      struct mem_cgroup *root)
611 {
612 	if (root == memcg)
613 		return true;
614 	if (!root->use_hierarchy)
615 		return false;
616 	return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
617 }
618 
mm_match_cgroup(struct mm_struct * mm,struct mem_cgroup * memcg)619 static inline bool mm_match_cgroup(struct mm_struct *mm,
620 				   struct mem_cgroup *memcg)
621 {
622 	struct mem_cgroup *task_memcg;
623 	bool match = false;
624 
625 	rcu_read_lock();
626 	task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
627 	if (task_memcg)
628 		match = mem_cgroup_is_descendant(task_memcg, memcg);
629 	rcu_read_unlock();
630 	return match;
631 }
632 
633 struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page);
634 ino_t page_cgroup_ino(struct page *page);
635 
mem_cgroup_online(struct mem_cgroup * memcg)636 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
637 {
638 	if (mem_cgroup_disabled())
639 		return true;
640 	return !!(memcg->css.flags & CSS_ONLINE);
641 }
642 
643 /*
644  * For memory reclaim.
645  */
646 int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
647 
648 void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
649 		int zid, int nr_pages);
650 
651 static inline
mem_cgroup_get_zone_lru_size(struct lruvec * lruvec,enum lru_list lru,int zone_idx)652 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
653 		enum lru_list lru, int zone_idx)
654 {
655 	struct mem_cgroup_per_node *mz;
656 
657 	mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
658 	return READ_ONCE(mz->lru_zone_size[zone_idx][lru]);
659 }
660 
661 void mem_cgroup_handle_over_high(void);
662 
663 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);
664 
665 unsigned long mem_cgroup_size(struct mem_cgroup *memcg);
666 
667 void mem_cgroup_print_oom_context(struct mem_cgroup *memcg,
668 				struct task_struct *p);
669 
670 void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg);
671 
mem_cgroup_enter_user_fault(void)672 static inline void mem_cgroup_enter_user_fault(void)
673 {
674 	WARN_ON(current->in_user_fault);
675 	current->in_user_fault = 1;
676 }
677 
mem_cgroup_exit_user_fault(void)678 static inline void mem_cgroup_exit_user_fault(void)
679 {
680 	WARN_ON(!current->in_user_fault);
681 	current->in_user_fault = 0;
682 }
683 
task_in_memcg_oom(struct task_struct * p)684 static inline bool task_in_memcg_oom(struct task_struct *p)
685 {
686 	return p->memcg_in_oom;
687 }
688 
689 bool mem_cgroup_oom_synchronize(bool wait);
690 struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
691 					    struct mem_cgroup *oom_domain);
692 void mem_cgroup_print_oom_group(struct mem_cgroup *memcg);
693 
694 #ifdef CONFIG_MEMCG_SWAP
695 extern bool cgroup_memory_noswap;
696 #endif
697 
698 struct mem_cgroup *lock_page_memcg(struct page *page);
699 void __unlock_page_memcg(struct mem_cgroup *memcg);
700 void unlock_page_memcg(struct page *page);
701 
702 /*
703  * idx can be of type enum memcg_stat_item or node_stat_item.
704  * Keep in sync with memcg_exact_page_state().
705  */
memcg_page_state(struct mem_cgroup * memcg,int idx)706 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
707 {
708 	long x = atomic_long_read(&memcg->vmstats[idx]);
709 #ifdef CONFIG_SMP
710 	if (x < 0)
711 		x = 0;
712 #endif
713 	return x;
714 }
715 
716 /*
717  * idx can be of type enum memcg_stat_item or node_stat_item.
718  * Keep in sync with memcg_exact_page_state().
719  */
memcg_page_state_local(struct mem_cgroup * memcg,int idx)720 static inline unsigned long memcg_page_state_local(struct mem_cgroup *memcg,
721 						   int idx)
722 {
723 	long x = 0;
724 	int cpu;
725 
726 	for_each_possible_cpu(cpu)
727 		x += per_cpu(memcg->vmstats_local->stat[idx], cpu);
728 #ifdef CONFIG_SMP
729 	if (x < 0)
730 		x = 0;
731 #endif
732 	return x;
733 }
734 
735 void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val);
736 
737 /* idx can be of type enum memcg_stat_item or node_stat_item */
mod_memcg_state(struct mem_cgroup * memcg,int idx,int val)738 static inline void mod_memcg_state(struct mem_cgroup *memcg,
739 				   int idx, int val)
740 {
741 	unsigned long flags;
742 
743 	local_irq_save(flags);
744 	__mod_memcg_state(memcg, idx, val);
745 	local_irq_restore(flags);
746 }
747 
748 /**
749  * mod_memcg_page_state - update page state statistics
750  * @page: the page
751  * @idx: page state item to account
752  * @val: number of pages (positive or negative)
753  *
754  * The @page must be locked or the caller must use lock_page_memcg()
755  * to prevent double accounting when the page is concurrently being
756  * moved to another memcg:
757  *
758  *   lock_page(page) or lock_page_memcg(page)
759  *   if (TestClearPageState(page))
760  *     mod_memcg_page_state(page, state, -1);
761  *   unlock_page(page) or unlock_page_memcg(page)
762  *
763  * Kernel pages are an exception to this, since they'll never move.
764  */
__mod_memcg_page_state(struct page * page,int idx,int val)765 static inline void __mod_memcg_page_state(struct page *page,
766 					  int idx, int val)
767 {
768 	if (page->mem_cgroup)
769 		__mod_memcg_state(page->mem_cgroup, idx, val);
770 }
771 
mod_memcg_page_state(struct page * page,int idx,int val)772 static inline void mod_memcg_page_state(struct page *page,
773 					int idx, int val)
774 {
775 	if (page->mem_cgroup)
776 		mod_memcg_state(page->mem_cgroup, idx, val);
777 }
778 
lruvec_page_state(struct lruvec * lruvec,enum node_stat_item idx)779 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
780 					      enum node_stat_item idx)
781 {
782 	struct mem_cgroup_per_node *pn;
783 	long x;
784 
785 	if (mem_cgroup_disabled())
786 		return node_page_state(lruvec_pgdat(lruvec), idx);
787 
788 #ifdef CONFIG_HYPERHOLD_FILE_LRU
789 	if (is_node_lruvec(lruvec))
790 		return node_page_state(lruvec_pgdat(lruvec), idx);
791 #endif
792 	pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
793 	x = atomic_long_read(&pn->lruvec_stat[idx]);
794 #ifdef CONFIG_SMP
795 	if (x < 0)
796 		x = 0;
797 #endif
798 	return x;
799 }
800 
lruvec_page_state_local(struct lruvec * lruvec,enum node_stat_item idx)801 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
802 						    enum node_stat_item idx)
803 {
804 	struct mem_cgroup_per_node *pn;
805 	long x = 0;
806 	int cpu;
807 
808 	if (mem_cgroup_disabled())
809 		return node_page_state(lruvec_pgdat(lruvec), idx);
810 
811 #ifdef CONFIG_HYPERHOLD_FILE_LRU
812 	if (is_node_lruvec(lruvec))
813 		return node_page_state(lruvec_pgdat(lruvec), idx);
814 #endif
815 
816 	pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
817 	for_each_possible_cpu(cpu)
818 		x += per_cpu(pn->lruvec_stat_local->count[idx], cpu);
819 #ifdef CONFIG_SMP
820 	if (x < 0)
821 		x = 0;
822 #endif
823 	return x;
824 }
825 
826 void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
827 			      int val);
828 void __mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
829 			int val);
830 void __mod_lruvec_slab_state(void *p, enum node_stat_item idx, int val);
831 
832 void mod_memcg_obj_state(void *p, int idx, int val);
833 
mod_lruvec_slab_state(void * p,enum node_stat_item idx,int val)834 static inline void mod_lruvec_slab_state(void *p, enum node_stat_item idx,
835 					 int val)
836 {
837 	unsigned long flags;
838 
839 	local_irq_save(flags);
840 	__mod_lruvec_slab_state(p, idx, val);
841 	local_irq_restore(flags);
842 }
843 
mod_memcg_lruvec_state(struct lruvec * lruvec,enum node_stat_item idx,int val)844 static inline void mod_memcg_lruvec_state(struct lruvec *lruvec,
845 					  enum node_stat_item idx, int val)
846 {
847 	unsigned long flags;
848 
849 	local_irq_save(flags);
850 	__mod_memcg_lruvec_state(lruvec, idx, val);
851 	local_irq_restore(flags);
852 }
853 
mod_lruvec_state(struct lruvec * lruvec,enum node_stat_item idx,int val)854 static inline void mod_lruvec_state(struct lruvec *lruvec,
855 				    enum node_stat_item idx, int val)
856 {
857 	unsigned long flags;
858 
859 	local_irq_save(flags);
860 	__mod_lruvec_state(lruvec, idx, val);
861 	local_irq_restore(flags);
862 }
863 
864 #ifdef CONFIG_HYPERHOLD_FILE_LRU
is_file_page(struct page * page)865 static __always_inline bool is_file_page(struct page *page)
866 {
867 	if (!PageUnevictable(page) && !PageSwapBacked(page) && page_mapping(page))
868 		return true;
869 
870 	return false;
871 
872 }
873 #endif
874 
__mod_lruvec_page_state(struct page * page,enum node_stat_item idx,int val)875 static inline void __mod_lruvec_page_state(struct page *page,
876 					   enum node_stat_item idx, int val)
877 {
878 	struct page *head = compound_head(page); /* rmap on tail pages */
879 	pg_data_t *pgdat = page_pgdat(page);
880 	struct lruvec *lruvec;
881 
882 #ifdef CONFIG_HYPERHOLD_FILE_LRU
883 	if (is_file_page(page) && !is_prot_page(page)) {
884 		__mod_node_page_state(pgdat, idx, val);
885 		return;
886 
887 	}
888 #endif
889 
890 	/* Untracked pages have no memcg, no lruvec. Update only the node */
891 	if (!head->mem_cgroup) {
892 		__mod_node_page_state(pgdat, idx, val);
893 		return;
894 	}
895 
896 	lruvec = mem_cgroup_lruvec(head->mem_cgroup, pgdat);
897 	__mod_lruvec_state(lruvec, idx, val);
898 }
899 
mod_lruvec_page_state(struct page * page,enum node_stat_item idx,int val)900 static inline void mod_lruvec_page_state(struct page *page,
901 					 enum node_stat_item idx, int val)
902 {
903 	unsigned long flags;
904 
905 	local_irq_save(flags);
906 	__mod_lruvec_page_state(page, idx, val);
907 	local_irq_restore(flags);
908 }
909 
910 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
911 						gfp_t gfp_mask,
912 						unsigned long *total_scanned);
913 
914 void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
915 			  unsigned long count);
916 
count_memcg_events(struct mem_cgroup * memcg,enum vm_event_item idx,unsigned long count)917 static inline void count_memcg_events(struct mem_cgroup *memcg,
918 				      enum vm_event_item idx,
919 				      unsigned long count)
920 {
921 	unsigned long flags;
922 
923 	local_irq_save(flags);
924 	__count_memcg_events(memcg, idx, count);
925 	local_irq_restore(flags);
926 }
927 
count_memcg_page_event(struct page * page,enum vm_event_item idx)928 static inline void count_memcg_page_event(struct page *page,
929 					  enum vm_event_item idx)
930 {
931 	if (page->mem_cgroup)
932 		count_memcg_events(page->mem_cgroup, idx, 1);
933 }
934 
count_memcg_event_mm(struct mm_struct * mm,enum vm_event_item idx)935 static inline void count_memcg_event_mm(struct mm_struct *mm,
936 					enum vm_event_item idx)
937 {
938 	struct mem_cgroup *memcg;
939 
940 	if (mem_cgroup_disabled())
941 		return;
942 
943 	rcu_read_lock();
944 	memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
945 	if (likely(memcg))
946 		count_memcg_events(memcg, idx, 1);
947 	rcu_read_unlock();
948 }
949 
memcg_memory_event(struct mem_cgroup * memcg,enum memcg_memory_event event)950 static inline void memcg_memory_event(struct mem_cgroup *memcg,
951 				      enum memcg_memory_event event)
952 {
953 	bool swap_event = event == MEMCG_SWAP_HIGH || event == MEMCG_SWAP_MAX ||
954 			  event == MEMCG_SWAP_FAIL;
955 
956 	atomic_long_inc(&memcg->memory_events_local[event]);
957 	if (!swap_event)
958 		cgroup_file_notify(&memcg->events_local_file);
959 
960 	do {
961 		atomic_long_inc(&memcg->memory_events[event]);
962 		if (swap_event)
963 			cgroup_file_notify(&memcg->swap_events_file);
964 		else
965 			cgroup_file_notify(&memcg->events_file);
966 
967 		if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
968 			break;
969 		if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
970 			break;
971 	} while ((memcg = parent_mem_cgroup(memcg)) &&
972 		 !mem_cgroup_is_root(memcg));
973 }
974 
memcg_memory_event_mm(struct mm_struct * mm,enum memcg_memory_event event)975 static inline void memcg_memory_event_mm(struct mm_struct *mm,
976 					 enum memcg_memory_event event)
977 {
978 	struct mem_cgroup *memcg;
979 
980 	if (mem_cgroup_disabled())
981 		return;
982 
983 	rcu_read_lock();
984 	memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
985 	if (likely(memcg))
986 		memcg_memory_event(memcg, event);
987 	rcu_read_unlock();
988 }
989 
990 void split_page_memcg(struct page *head, unsigned int nr);
991 
992 #else /* CONFIG_MEMCG */
993 
994 #define MEM_CGROUP_ID_SHIFT	0
995 #define MEM_CGROUP_ID_MAX	0
996 
997 struct mem_cgroup;
998 
mem_cgroup_is_root(struct mem_cgroup * memcg)999 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
1000 {
1001 	return true;
1002 }
1003 
mem_cgroup_disabled(void)1004 static inline bool mem_cgroup_disabled(void)
1005 {
1006 	return true;
1007 }
1008 
memcg_memory_event(struct mem_cgroup * memcg,enum memcg_memory_event event)1009 static inline void memcg_memory_event(struct mem_cgroup *memcg,
1010 				      enum memcg_memory_event event)
1011 {
1012 }
1013 
memcg_memory_event_mm(struct mm_struct * mm,enum memcg_memory_event event)1014 static inline void memcg_memory_event_mm(struct mm_struct *mm,
1015 					 enum memcg_memory_event event)
1016 {
1017 }
1018 
mem_cgroup_protection(struct mem_cgroup * root,struct mem_cgroup * memcg,unsigned long * min,unsigned long * low)1019 static inline void mem_cgroup_protection(struct mem_cgroup *root,
1020 					 struct mem_cgroup *memcg,
1021 					 unsigned long *min,
1022 					 unsigned long *low)
1023 {
1024 	*min = *low = 0;
1025 }
1026 
mem_cgroup_calculate_protection(struct mem_cgroup * root,struct mem_cgroup * memcg)1027 static inline void mem_cgroup_calculate_protection(struct mem_cgroup *root,
1028 						   struct mem_cgroup *memcg)
1029 {
1030 }
1031 
mem_cgroup_below_low(struct mem_cgroup * memcg)1032 static inline bool mem_cgroup_below_low(struct mem_cgroup *memcg)
1033 {
1034 	return false;
1035 }
1036 
mem_cgroup_below_min(struct mem_cgroup * memcg)1037 static inline bool mem_cgroup_below_min(struct mem_cgroup *memcg)
1038 {
1039 	return false;
1040 }
1041 
mem_cgroup_charge(struct page * page,struct mm_struct * mm,gfp_t gfp_mask)1042 static inline int mem_cgroup_charge(struct page *page, struct mm_struct *mm,
1043 				    gfp_t gfp_mask)
1044 {
1045 	return 0;
1046 }
1047 
mem_cgroup_uncharge(struct page * page)1048 static inline void mem_cgroup_uncharge(struct page *page)
1049 {
1050 }
1051 
mem_cgroup_uncharge_list(struct list_head * page_list)1052 static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
1053 {
1054 }
1055 
mem_cgroup_migrate(struct page * old,struct page * new)1056 static inline void mem_cgroup_migrate(struct page *old, struct page *new)
1057 {
1058 }
1059 
mem_cgroup_lruvec(struct mem_cgroup * memcg,struct pglist_data * pgdat)1060 static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
1061 					       struct pglist_data *pgdat)
1062 {
1063 	return &pgdat->__lruvec;
1064 }
1065 
mem_cgroup_page_lruvec(struct page * page,struct pglist_data * pgdat)1066 static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
1067 						    struct pglist_data *pgdat)
1068 {
1069 	return &pgdat->__lruvec;
1070 }
1071 
parent_mem_cgroup(struct mem_cgroup * memcg)1072 static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
1073 {
1074 	return NULL;
1075 }
1076 
mm_match_cgroup(struct mm_struct * mm,struct mem_cgroup * memcg)1077 static inline bool mm_match_cgroup(struct mm_struct *mm,
1078 		struct mem_cgroup *memcg)
1079 {
1080 	return true;
1081 }
1082 
get_mem_cgroup_from_mm(struct mm_struct * mm)1083 static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
1084 {
1085 	return NULL;
1086 }
1087 
get_mem_cgroup_from_page(struct page * page)1088 static inline struct mem_cgroup *get_mem_cgroup_from_page(struct page *page)
1089 {
1090 	return NULL;
1091 }
1092 
mem_cgroup_put(struct mem_cgroup * memcg)1093 static inline void mem_cgroup_put(struct mem_cgroup *memcg)
1094 {
1095 }
1096 
1097 static inline struct mem_cgroup *
mem_cgroup_iter(struct mem_cgroup * root,struct mem_cgroup * prev,struct mem_cgroup_reclaim_cookie * reclaim)1098 mem_cgroup_iter(struct mem_cgroup *root,
1099 		struct mem_cgroup *prev,
1100 		struct mem_cgroup_reclaim_cookie *reclaim)
1101 {
1102 	return NULL;
1103 }
1104 
mem_cgroup_iter_break(struct mem_cgroup * root,struct mem_cgroup * prev)1105 static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
1106 					 struct mem_cgroup *prev)
1107 {
1108 }
1109 
mem_cgroup_scan_tasks(struct mem_cgroup * memcg,int (* fn)(struct task_struct *,void *),void * arg)1110 static inline int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
1111 		int (*fn)(struct task_struct *, void *), void *arg)
1112 {
1113 	return 0;
1114 }
1115 
mem_cgroup_id(struct mem_cgroup * memcg)1116 static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
1117 {
1118 	return 0;
1119 }
1120 
mem_cgroup_from_id(unsigned short id)1121 static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
1122 {
1123 	WARN_ON_ONCE(id);
1124 	/* XXX: This should always return root_mem_cgroup */
1125 	return NULL;
1126 }
1127 
mem_cgroup_from_seq(struct seq_file * m)1128 static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
1129 {
1130 	return NULL;
1131 }
1132 
lruvec_memcg(struct lruvec * lruvec)1133 static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
1134 {
1135 	return NULL;
1136 }
1137 
mem_cgroup_online(struct mem_cgroup * memcg)1138 static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
1139 {
1140 	return true;
1141 }
1142 
1143 static inline
mem_cgroup_get_zone_lru_size(struct lruvec * lruvec,enum lru_list lru,int zone_idx)1144 unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
1145 		enum lru_list lru, int zone_idx)
1146 {
1147 	return 0;
1148 }
1149 
mem_cgroup_get_max(struct mem_cgroup * memcg)1150 static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
1151 {
1152 	return 0;
1153 }
1154 
mem_cgroup_size(struct mem_cgroup * memcg)1155 static inline unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
1156 {
1157 	return 0;
1158 }
1159 
1160 static inline void
mem_cgroup_print_oom_context(struct mem_cgroup * memcg,struct task_struct * p)1161 mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
1162 {
1163 }
1164 
1165 static inline void
mem_cgroup_print_oom_meminfo(struct mem_cgroup * memcg)1166 mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
1167 {
1168 }
1169 
lock_page_memcg(struct page * page)1170 static inline struct mem_cgroup *lock_page_memcg(struct page *page)
1171 {
1172 	return NULL;
1173 }
1174 
__unlock_page_memcg(struct mem_cgroup * memcg)1175 static inline void __unlock_page_memcg(struct mem_cgroup *memcg)
1176 {
1177 }
1178 
unlock_page_memcg(struct page * page)1179 static inline void unlock_page_memcg(struct page *page)
1180 {
1181 }
1182 
mem_cgroup_handle_over_high(void)1183 static inline void mem_cgroup_handle_over_high(void)
1184 {
1185 }
1186 
mem_cgroup_enter_user_fault(void)1187 static inline void mem_cgroup_enter_user_fault(void)
1188 {
1189 }
1190 
mem_cgroup_exit_user_fault(void)1191 static inline void mem_cgroup_exit_user_fault(void)
1192 {
1193 }
1194 
task_in_memcg_oom(struct task_struct * p)1195 static inline bool task_in_memcg_oom(struct task_struct *p)
1196 {
1197 	return false;
1198 }
1199 
mem_cgroup_oom_synchronize(bool wait)1200 static inline bool mem_cgroup_oom_synchronize(bool wait)
1201 {
1202 	return false;
1203 }
1204 
mem_cgroup_get_oom_group(struct task_struct * victim,struct mem_cgroup * oom_domain)1205 static inline struct mem_cgroup *mem_cgroup_get_oom_group(
1206 	struct task_struct *victim, struct mem_cgroup *oom_domain)
1207 {
1208 	return NULL;
1209 }
1210 
mem_cgroup_print_oom_group(struct mem_cgroup * memcg)1211 static inline void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
1212 {
1213 }
1214 
memcg_page_state(struct mem_cgroup * memcg,int idx)1215 static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
1216 {
1217 	return 0;
1218 }
1219 
memcg_page_state_local(struct mem_cgroup * memcg,int idx)1220 static inline unsigned long memcg_page_state_local(struct mem_cgroup *memcg,
1221 						   int idx)
1222 {
1223 	return 0;
1224 }
1225 
__mod_memcg_state(struct mem_cgroup * memcg,int idx,int nr)1226 static inline void __mod_memcg_state(struct mem_cgroup *memcg,
1227 				     int idx,
1228 				     int nr)
1229 {
1230 }
1231 
mod_memcg_state(struct mem_cgroup * memcg,int idx,int nr)1232 static inline void mod_memcg_state(struct mem_cgroup *memcg,
1233 				   int idx,
1234 				   int nr)
1235 {
1236 }
1237 
__mod_memcg_page_state(struct page * page,int idx,int nr)1238 static inline void __mod_memcg_page_state(struct page *page,
1239 					  int idx,
1240 					  int nr)
1241 {
1242 }
1243 
mod_memcg_page_state(struct page * page,int idx,int nr)1244 static inline void mod_memcg_page_state(struct page *page,
1245 					int idx,
1246 					int nr)
1247 {
1248 }
1249 
lruvec_page_state(struct lruvec * lruvec,enum node_stat_item idx)1250 static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
1251 					      enum node_stat_item idx)
1252 {
1253 	return node_page_state(lruvec_pgdat(lruvec), idx);
1254 }
1255 
lruvec_page_state_local(struct lruvec * lruvec,enum node_stat_item idx)1256 static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1257 						    enum node_stat_item idx)
1258 {
1259 	return node_page_state(lruvec_pgdat(lruvec), idx);
1260 }
1261 
__mod_memcg_lruvec_state(struct lruvec * lruvec,enum node_stat_item idx,int val)1262 static inline void __mod_memcg_lruvec_state(struct lruvec *lruvec,
1263 					    enum node_stat_item idx, int val)
1264 {
1265 }
1266 
__mod_lruvec_state(struct lruvec * lruvec,enum node_stat_item idx,int val)1267 static inline void __mod_lruvec_state(struct lruvec *lruvec,
1268 				      enum node_stat_item idx, int val)
1269 {
1270 	__mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
1271 }
1272 
mod_lruvec_state(struct lruvec * lruvec,enum node_stat_item idx,int val)1273 static inline void mod_lruvec_state(struct lruvec *lruvec,
1274 				    enum node_stat_item idx, int val)
1275 {
1276 	mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
1277 }
1278 
__mod_lruvec_page_state(struct page * page,enum node_stat_item idx,int val)1279 static inline void __mod_lruvec_page_state(struct page *page,
1280 					   enum node_stat_item idx, int val)
1281 {
1282 	__mod_node_page_state(page_pgdat(page), idx, val);
1283 }
1284 
mod_lruvec_page_state(struct page * page,enum node_stat_item idx,int val)1285 static inline void mod_lruvec_page_state(struct page *page,
1286 					 enum node_stat_item idx, int val)
1287 {
1288 	mod_node_page_state(page_pgdat(page), idx, val);
1289 }
1290 
__mod_lruvec_slab_state(void * p,enum node_stat_item idx,int val)1291 static inline void __mod_lruvec_slab_state(void *p, enum node_stat_item idx,
1292 					   int val)
1293 {
1294 	struct page *page = virt_to_head_page(p);
1295 
1296 	__mod_node_page_state(page_pgdat(page), idx, val);
1297 }
1298 
mod_lruvec_slab_state(void * p,enum node_stat_item idx,int val)1299 static inline void mod_lruvec_slab_state(void *p, enum node_stat_item idx,
1300 					 int val)
1301 {
1302 	struct page *page = virt_to_head_page(p);
1303 
1304 	mod_node_page_state(page_pgdat(page), idx, val);
1305 }
1306 
mod_memcg_obj_state(void * p,int idx,int val)1307 static inline void mod_memcg_obj_state(void *p, int idx, int val)
1308 {
1309 }
1310 
1311 static inline
mem_cgroup_soft_limit_reclaim(pg_data_t * pgdat,int order,gfp_t gfp_mask,unsigned long * total_scanned)1312 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
1313 					    gfp_t gfp_mask,
1314 					    unsigned long *total_scanned)
1315 {
1316 	return 0;
1317 }
1318 
split_page_memcg(struct page * head,unsigned int nr)1319 static inline void split_page_memcg(struct page *head, unsigned int nr)
1320 {
1321 }
1322 
count_memcg_events(struct mem_cgroup * memcg,enum vm_event_item idx,unsigned long count)1323 static inline void count_memcg_events(struct mem_cgroup *memcg,
1324 				      enum vm_event_item idx,
1325 				      unsigned long count)
1326 {
1327 }
1328 
__count_memcg_events(struct mem_cgroup * memcg,enum vm_event_item idx,unsigned long count)1329 static inline void __count_memcg_events(struct mem_cgroup *memcg,
1330 					enum vm_event_item idx,
1331 					unsigned long count)
1332 {
1333 }
1334 
count_memcg_page_event(struct page * page,int idx)1335 static inline void count_memcg_page_event(struct page *page,
1336 					  int idx)
1337 {
1338 }
1339 
1340 static inline
count_memcg_event_mm(struct mm_struct * mm,enum vm_event_item idx)1341 void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
1342 {
1343 }
1344 #endif /* CONFIG_MEMCG */
1345 
1346 /* idx can be of type enum memcg_stat_item or node_stat_item */
__inc_memcg_state(struct mem_cgroup * memcg,int idx)1347 static inline void __inc_memcg_state(struct mem_cgroup *memcg,
1348 				     int idx)
1349 {
1350 	__mod_memcg_state(memcg, idx, 1);
1351 }
1352 
1353 /* idx can be of type enum memcg_stat_item or node_stat_item */
__dec_memcg_state(struct mem_cgroup * memcg,int idx)1354 static inline void __dec_memcg_state(struct mem_cgroup *memcg,
1355 				     int idx)
1356 {
1357 	__mod_memcg_state(memcg, idx, -1);
1358 }
1359 
1360 /* idx can be of type enum memcg_stat_item or node_stat_item */
__inc_memcg_page_state(struct page * page,int idx)1361 static inline void __inc_memcg_page_state(struct page *page,
1362 					  int idx)
1363 {
1364 	__mod_memcg_page_state(page, idx, 1);
1365 }
1366 
1367 /* idx can be of type enum memcg_stat_item or node_stat_item */
__dec_memcg_page_state(struct page * page,int idx)1368 static inline void __dec_memcg_page_state(struct page *page,
1369 					  int idx)
1370 {
1371 	__mod_memcg_page_state(page, idx, -1);
1372 }
1373 
__inc_lruvec_state(struct lruvec * lruvec,enum node_stat_item idx)1374 static inline void __inc_lruvec_state(struct lruvec *lruvec,
1375 				      enum node_stat_item idx)
1376 {
1377 	__mod_lruvec_state(lruvec, idx, 1);
1378 }
1379 
__dec_lruvec_state(struct lruvec * lruvec,enum node_stat_item idx)1380 static inline void __dec_lruvec_state(struct lruvec *lruvec,
1381 				      enum node_stat_item idx)
1382 {
1383 	__mod_lruvec_state(lruvec, idx, -1);
1384 }
1385 
__inc_lruvec_page_state(struct page * page,enum node_stat_item idx)1386 static inline void __inc_lruvec_page_state(struct page *page,
1387 					   enum node_stat_item idx)
1388 {
1389 	__mod_lruvec_page_state(page, idx, 1);
1390 }
1391 
__dec_lruvec_page_state(struct page * page,enum node_stat_item idx)1392 static inline void __dec_lruvec_page_state(struct page *page,
1393 					   enum node_stat_item idx)
1394 {
1395 	__mod_lruvec_page_state(page, idx, -1);
1396 }
1397 
__inc_lruvec_slab_state(void * p,enum node_stat_item idx)1398 static inline void __inc_lruvec_slab_state(void *p, enum node_stat_item idx)
1399 {
1400 	__mod_lruvec_slab_state(p, idx, 1);
1401 }
1402 
__dec_lruvec_slab_state(void * p,enum node_stat_item idx)1403 static inline void __dec_lruvec_slab_state(void *p, enum node_stat_item idx)
1404 {
1405 	__mod_lruvec_slab_state(p, idx, -1);
1406 }
1407 
1408 /* idx can be of type enum memcg_stat_item or node_stat_item */
inc_memcg_state(struct mem_cgroup * memcg,int idx)1409 static inline void inc_memcg_state(struct mem_cgroup *memcg,
1410 				   int idx)
1411 {
1412 	mod_memcg_state(memcg, idx, 1);
1413 }
1414 
1415 /* idx can be of type enum memcg_stat_item or node_stat_item */
dec_memcg_state(struct mem_cgroup * memcg,int idx)1416 static inline void dec_memcg_state(struct mem_cgroup *memcg,
1417 				   int idx)
1418 {
1419 	mod_memcg_state(memcg, idx, -1);
1420 }
1421 
1422 /* idx can be of type enum memcg_stat_item or node_stat_item */
inc_memcg_page_state(struct page * page,int idx)1423 static inline void inc_memcg_page_state(struct page *page,
1424 					int idx)
1425 {
1426 	mod_memcg_page_state(page, idx, 1);
1427 }
1428 
1429 /* idx can be of type enum memcg_stat_item or node_stat_item */
dec_memcg_page_state(struct page * page,int idx)1430 static inline void dec_memcg_page_state(struct page *page,
1431 					int idx)
1432 {
1433 	mod_memcg_page_state(page, idx, -1);
1434 }
1435 
inc_lruvec_state(struct lruvec * lruvec,enum node_stat_item idx)1436 static inline void inc_lruvec_state(struct lruvec *lruvec,
1437 				    enum node_stat_item idx)
1438 {
1439 	mod_lruvec_state(lruvec, idx, 1);
1440 }
1441 
dec_lruvec_state(struct lruvec * lruvec,enum node_stat_item idx)1442 static inline void dec_lruvec_state(struct lruvec *lruvec,
1443 				    enum node_stat_item idx)
1444 {
1445 	mod_lruvec_state(lruvec, idx, -1);
1446 }
1447 
inc_lruvec_page_state(struct page * page,enum node_stat_item idx)1448 static inline void inc_lruvec_page_state(struct page *page,
1449 					 enum node_stat_item idx)
1450 {
1451 	mod_lruvec_page_state(page, idx, 1);
1452 }
1453 
dec_lruvec_page_state(struct page * page,enum node_stat_item idx)1454 static inline void dec_lruvec_page_state(struct page *page,
1455 					 enum node_stat_item idx)
1456 {
1457 	mod_lruvec_page_state(page, idx, -1);
1458 }
1459 
parent_lruvec(struct lruvec * lruvec)1460 static inline struct lruvec *parent_lruvec(struct lruvec *lruvec)
1461 {
1462 	struct mem_cgroup *memcg;
1463 
1464 	memcg = lruvec_memcg(lruvec);
1465 	if (!memcg)
1466 		return NULL;
1467 	memcg = parent_mem_cgroup(memcg);
1468 	if (!memcg)
1469 		return NULL;
1470 	return mem_cgroup_lruvec(memcg, lruvec_pgdat(lruvec));
1471 }
1472 
1473 #ifdef CONFIG_CGROUP_WRITEBACK
1474 
1475 struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
1476 void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
1477 			 unsigned long *pheadroom, unsigned long *pdirty,
1478 			 unsigned long *pwriteback);
1479 
1480 void mem_cgroup_track_foreign_dirty_slowpath(struct page *page,
1481 					     struct bdi_writeback *wb);
1482 
mem_cgroup_track_foreign_dirty(struct page * page,struct bdi_writeback * wb)1483 static inline void mem_cgroup_track_foreign_dirty(struct page *page,
1484 						  struct bdi_writeback *wb)
1485 {
1486 	if (mem_cgroup_disabled())
1487 		return;
1488 
1489 	if (unlikely(&page->mem_cgroup->css != wb->memcg_css))
1490 		mem_cgroup_track_foreign_dirty_slowpath(page, wb);
1491 }
1492 
1493 void mem_cgroup_flush_foreign(struct bdi_writeback *wb);
1494 
1495 #else	/* CONFIG_CGROUP_WRITEBACK */
1496 
mem_cgroup_wb_domain(struct bdi_writeback * wb)1497 static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
1498 {
1499 	return NULL;
1500 }
1501 
mem_cgroup_wb_stats(struct bdi_writeback * wb,unsigned long * pfilepages,unsigned long * pheadroom,unsigned long * pdirty,unsigned long * pwriteback)1502 static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
1503 				       unsigned long *pfilepages,
1504 				       unsigned long *pheadroom,
1505 				       unsigned long *pdirty,
1506 				       unsigned long *pwriteback)
1507 {
1508 }
1509 
mem_cgroup_track_foreign_dirty(struct page * page,struct bdi_writeback * wb)1510 static inline void mem_cgroup_track_foreign_dirty(struct page *page,
1511 						  struct bdi_writeback *wb)
1512 {
1513 }
1514 
mem_cgroup_flush_foreign(struct bdi_writeback * wb)1515 static inline void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
1516 {
1517 }
1518 
1519 #endif	/* CONFIG_CGROUP_WRITEBACK */
1520 
1521 struct sock;
1522 bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1523 void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1524 #ifdef CONFIG_MEMCG
1525 extern struct static_key_false memcg_sockets_enabled_key;
1526 #define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
1527 void mem_cgroup_sk_alloc(struct sock *sk);
1528 void mem_cgroup_sk_free(struct sock *sk);
mem_cgroup_under_socket_pressure(struct mem_cgroup * memcg)1529 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1530 {
1531 	if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_pressure)
1532 		return true;
1533 	do {
1534 		if (time_before(jiffies, memcg->socket_pressure))
1535 			return true;
1536 	} while ((memcg = parent_mem_cgroup(memcg)));
1537 	return false;
1538 }
1539 
1540 extern int memcg_expand_shrinker_maps(int new_id);
1541 
1542 extern void memcg_set_shrinker_bit(struct mem_cgroup *memcg,
1543 				   int nid, int shrinker_id);
1544 #else
1545 #define mem_cgroup_sockets_enabled 0
mem_cgroup_sk_alloc(struct sock * sk)1546 static inline void mem_cgroup_sk_alloc(struct sock *sk) { };
mem_cgroup_sk_free(struct sock * sk)1547 static inline void mem_cgroup_sk_free(struct sock *sk) { };
mem_cgroup_under_socket_pressure(struct mem_cgroup * memcg)1548 static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
1549 {
1550 	return false;
1551 }
1552 
memcg_set_shrinker_bit(struct mem_cgroup * memcg,int nid,int shrinker_id)1553 static inline void memcg_set_shrinker_bit(struct mem_cgroup *memcg,
1554 					  int nid, int shrinker_id)
1555 {
1556 }
1557 #endif
1558 
1559 #ifdef CONFIG_MEMCG_KMEM
1560 int __memcg_kmem_charge(struct mem_cgroup *memcg, gfp_t gfp,
1561 			unsigned int nr_pages);
1562 void __memcg_kmem_uncharge(struct mem_cgroup *memcg, unsigned int nr_pages);
1563 int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order);
1564 void __memcg_kmem_uncharge_page(struct page *page, int order);
1565 
1566 struct obj_cgroup *get_obj_cgroup_from_current(void);
1567 
1568 int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size);
1569 void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size);
1570 
1571 extern struct static_key_false memcg_kmem_enabled_key;
1572 
1573 extern int memcg_nr_cache_ids;
1574 void memcg_get_cache_ids(void);
1575 void memcg_put_cache_ids(void);
1576 
1577 /*
1578  * Helper macro to loop through all memcg-specific caches. Callers must still
1579  * check if the cache is valid (it is either valid or NULL).
1580  * the slab_mutex must be held when looping through those caches
1581  */
1582 #define for_each_memcg_cache_index(_idx)	\
1583 	for ((_idx) = 0; (_idx) < memcg_nr_cache_ids; (_idx)++)
1584 
memcg_kmem_enabled(void)1585 static inline bool memcg_kmem_enabled(void)
1586 {
1587 	return static_branch_likely(&memcg_kmem_enabled_key);
1588 }
1589 
memcg_kmem_charge_page(struct page * page,gfp_t gfp,int order)1590 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1591 					 int order)
1592 {
1593 	if (memcg_kmem_enabled())
1594 		return __memcg_kmem_charge_page(page, gfp, order);
1595 	return 0;
1596 }
1597 
memcg_kmem_uncharge_page(struct page * page,int order)1598 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1599 {
1600 	if (memcg_kmem_enabled())
1601 		__memcg_kmem_uncharge_page(page, order);
1602 }
1603 
memcg_kmem_charge(struct mem_cgroup * memcg,gfp_t gfp,unsigned int nr_pages)1604 static inline int memcg_kmem_charge(struct mem_cgroup *memcg, gfp_t gfp,
1605 				    unsigned int nr_pages)
1606 {
1607 	if (memcg_kmem_enabled())
1608 		return __memcg_kmem_charge(memcg, gfp, nr_pages);
1609 	return 0;
1610 }
1611 
memcg_kmem_uncharge(struct mem_cgroup * memcg,unsigned int nr_pages)1612 static inline void memcg_kmem_uncharge(struct mem_cgroup *memcg,
1613 				       unsigned int nr_pages)
1614 {
1615 	if (memcg_kmem_enabled())
1616 		__memcg_kmem_uncharge(memcg, nr_pages);
1617 }
1618 
1619 /*
1620  * helper for accessing a memcg's index. It will be used as an index in the
1621  * child cache array in kmem_cache, and also to derive its name. This function
1622  * will return -1 when this is not a kmem-limited memcg.
1623  */
memcg_cache_id(struct mem_cgroup * memcg)1624 static inline int memcg_cache_id(struct mem_cgroup *memcg)
1625 {
1626 	return memcg ? memcg->kmemcg_id : -1;
1627 }
1628 
1629 struct mem_cgroup *mem_cgroup_from_obj(void *p);
1630 
1631 #else
1632 
memcg_kmem_charge_page(struct page * page,gfp_t gfp,int order)1633 static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1634 					 int order)
1635 {
1636 	return 0;
1637 }
1638 
memcg_kmem_uncharge_page(struct page * page,int order)1639 static inline void memcg_kmem_uncharge_page(struct page *page, int order)
1640 {
1641 }
1642 
__memcg_kmem_charge_page(struct page * page,gfp_t gfp,int order)1643 static inline int __memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1644 					   int order)
1645 {
1646 	return 0;
1647 }
1648 
__memcg_kmem_uncharge_page(struct page * page,int order)1649 static inline void __memcg_kmem_uncharge_page(struct page *page, int order)
1650 {
1651 }
1652 
1653 #define for_each_memcg_cache_index(_idx)	\
1654 	for (; NULL; )
1655 
memcg_kmem_enabled(void)1656 static inline bool memcg_kmem_enabled(void)
1657 {
1658 	return false;
1659 }
1660 
memcg_cache_id(struct mem_cgroup * memcg)1661 static inline int memcg_cache_id(struct mem_cgroup *memcg)
1662 {
1663 	return -1;
1664 }
1665 
memcg_get_cache_ids(void)1666 static inline void memcg_get_cache_ids(void)
1667 {
1668 }
1669 
memcg_put_cache_ids(void)1670 static inline void memcg_put_cache_ids(void)
1671 {
1672 }
1673 
mem_cgroup_from_obj(void * p)1674 static inline struct mem_cgroup *mem_cgroup_from_obj(void *p)
1675 {
1676        return NULL;
1677 }
1678 
1679 #endif /* CONFIG_MEMCG_KMEM */
1680 
1681 #endif /* _LINUX_MEMCONTROL_H */
1682