cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

memcontrol.h (45620B)


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