blob: 977edd3b7bd8934b9408b81ff5591a9a684b09f9 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002/* memcontrol.h - Memory Controller
3 *
4 * Copyright IBM Corporation, 2007
5 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
6 *
Pavel Emelianov78fb7462008-02-07 00:13:51 -08007 * Copyright 2007 OpenVZ SWsoft Inc
8 * Author: Pavel Emelianov <xemul@openvz.org>
Balbir Singh8cdea7c2008-02-07 00:13:50 -08009 */
10
11#ifndef _LINUX_MEMCONTROL_H
12#define _LINUX_MEMCONTROL_H
Hirokazu Takahashif8d665422009-01-07 18:08:02 -080013#include <linux/cgroup.h>
Ying Han456f9982011-05-26 16:25:38 -070014#include <linux/vm_event_item.h>
Glauber Costa7ae1e1d2012-12-18 14:21:56 -080015#include <linux/hardirq.h>
Glauber Costaa8964b92012-12-18 14:22:09 -080016#include <linux/jump_label.h>
Michal Hocko33398cf2015-09-08 15:01:02 -070017#include <linux/page_counter.h>
18#include <linux/vmpressure.h>
19#include <linux/eventfd.h>
Johannes Weiner00f3ca22017-07-06 15:40:52 -070020#include <linux/mm.h>
21#include <linux/vmstat.h>
Michal Hocko33398cf2015-09-08 15:01:02 -070022#include <linux/writeback.h>
Johannes Weinerfdf1cdb2016-03-15 14:57:25 -070023#include <linux/page-flags.h>
Ying Han456f9982011-05-26 16:25:38 -070024
Pavel Emelianov78fb7462008-02-07 00:13:51 -080025struct mem_cgroup;
Balbir Singh8697d332008-02-07 00:13:59 -080026struct page;
27struct mm_struct;
Glauber Costa2633d7a2012-12-18 14:22:34 -080028struct kmem_cache;
Pavel Emelianov78fb7462008-02-07 00:13:51 -080029
Johannes Weiner71cd3112017-05-03 14:55:13 -070030/* Cgroup-specific page state, on top of universal node page state */
31enum memcg_stat_item {
32 MEMCG_CACHE = NR_VM_NODE_STAT_ITEMS,
33 MEMCG_RSS,
34 MEMCG_RSS_HUGE,
35 MEMCG_SWAP,
36 MEMCG_SOCK,
37 /* XXX: why are these zone and not node counters? */
38 MEMCG_KERNEL_STACK_KB,
Johannes Weinerb2807f02016-01-20 15:03:22 -080039 MEMCG_NR_STAT,
Greg Thelen2a7106f2011-01-13 15:47:37 -080040};
41
Johannes Weinere27be242018-04-10 16:29:45 -070042enum memcg_memory_event {
43 MEMCG_LOW,
Johannes Weiner71cd3112017-05-03 14:55:13 -070044 MEMCG_HIGH,
45 MEMCG_MAX,
46 MEMCG_OOM,
Roman Gushchinfe6bdfc2018-06-14 15:28:05 -070047 MEMCG_OOM_KILL,
Tejun Heof3a53a32018-06-07 17:05:35 -070048 MEMCG_SWAP_MAX,
49 MEMCG_SWAP_FAIL,
Johannes Weinere27be242018-04-10 16:29:45 -070050 MEMCG_NR_MEMORY_EVENTS,
Johannes Weiner71cd3112017-05-03 14:55:13 -070051};
52
Roman Gushchinbf8d5d52018-06-07 17:07:46 -070053enum mem_cgroup_protection {
54 MEMCG_PROT_NONE,
55 MEMCG_PROT_LOW,
56 MEMCG_PROT_MIN,
57};
58
Johannes Weiner56600482012-01-12 17:17:59 -080059struct mem_cgroup_reclaim_cookie {
Mel Gormanef8f2322016-07-28 15:46:05 -070060 pg_data_t *pgdat;
Johannes Weiner56600482012-01-12 17:17:59 -080061 unsigned int generation;
62};
63
Johannes Weiner71cd3112017-05-03 14:55:13 -070064#ifdef CONFIG_MEMCG
65
66#define MEM_CGROUP_ID_SHIFT 16
67#define MEM_CGROUP_ID_MAX USHRT_MAX
68
69struct mem_cgroup_id {
70 int id;
Kirill Tkhai1c2d4792018-10-26 15:09:28 -070071 refcount_t ref;
Johannes Weiner71cd3112017-05-03 14:55:13 -070072};
73
Michal Hocko33398cf2015-09-08 15:01:02 -070074/*
75 * Per memcg event counter is incremented at every pagein/pageout. With THP,
76 * it will be incremated by the number of pages. This counter is used for
77 * for trigger some periodic events. This is straightforward and better
78 * than using jiffies etc. to handle periodic memcg event.
79 */
80enum mem_cgroup_events_target {
81 MEM_CGROUP_TARGET_THRESH,
82 MEM_CGROUP_TARGET_SOFTLIMIT,
Michal Hocko33398cf2015-09-08 15:01:02 -070083 MEM_CGROUP_NTARGETS,
84};
85
Chris Down871789d2019-05-14 15:46:57 -070086struct memcg_vmstats_percpu {
87 long stat[MEMCG_NR_STAT];
Johannes Weinere27be242018-04-10 16:29:45 -070088 unsigned long events[NR_VM_EVENT_ITEMS];
Michal Hocko33398cf2015-09-08 15:01:02 -070089 unsigned long nr_page_events;
90 unsigned long targets[MEM_CGROUP_NTARGETS];
91};
92
93struct mem_cgroup_reclaim_iter {
94 struct mem_cgroup *position;
95 /* scan generation, increased every round-trip */
96 unsigned int generation;
97};
98
Johannes Weiner00f3ca22017-07-06 15:40:52 -070099struct lruvec_stat {
100 long count[NR_VM_NODE_STAT_ITEMS];
101};
102
Michal Hocko33398cf2015-09-08 15:01:02 -0700103/*
Kirill Tkhai0a4465d2018-08-17 15:47:37 -0700104 * Bitmap of shrinker::id corresponding to memcg-aware shrinkers,
105 * which have elements charged to this memcg.
106 */
107struct memcg_shrinker_map {
108 struct rcu_head rcu;
Gustavo A. R. Silva307ed942020-03-23 18:36:10 -0500109 unsigned long map[];
Kirill Tkhai0a4465d2018-08-17 15:47:37 -0700110};
111
112/*
Hao Lee242c37b2019-11-30 17:50:12 -0800113 * per-node information in memory controller.
Michal Hocko33398cf2015-09-08 15:01:02 -0700114 */
Mel Gormanef8f2322016-07-28 15:46:05 -0700115struct mem_cgroup_per_node {
Michal Hocko33398cf2015-09-08 15:01:02 -0700116 struct lruvec lruvec;
Johannes Weinera983b5e2018-01-31 16:16:45 -0800117
Johannes Weiner815744d2019-06-13 15:55:46 -0700118 /* Legacy local VM stats */
119 struct lruvec_stat __percpu *lruvec_stat_local;
120
121 /* Subtree VM stats (batched updates) */
Johannes Weinera983b5e2018-01-31 16:16:45 -0800122 struct lruvec_stat __percpu *lruvec_stat_cpu;
123 atomic_long_t lruvec_stat[NR_VM_NODE_STAT_ITEMS];
124
Michal Hockob4536f0c82017-01-10 16:58:04 -0800125 unsigned long lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
Michal Hocko33398cf2015-09-08 15:01:02 -0700126
Yafang Shao9da83f32019-11-30 17:50:03 -0800127 struct mem_cgroup_reclaim_iter iter;
Michal Hocko33398cf2015-09-08 15:01:02 -0700128
Kirill Tkhai0a4465d2018-08-17 15:47:37 -0700129 struct memcg_shrinker_map __rcu *shrinker_map;
Yang Shi0a432dc2019-09-23 15:38:12 -0700130
Michal Hocko33398cf2015-09-08 15:01:02 -0700131 struct rb_node tree_node; /* RB tree node */
132 unsigned long usage_in_excess;/* Set to the value by which */
133 /* the soft limit is exceeded*/
134 bool on_tree;
135 struct mem_cgroup *memcg; /* Back pointer, we cannot */
136 /* use container_of */
137};
138
Michal Hocko33398cf2015-09-08 15:01:02 -0700139struct mem_cgroup_threshold {
140 struct eventfd_ctx *eventfd;
141 unsigned long threshold;
142};
143
144/* For threshold */
145struct mem_cgroup_threshold_ary {
146 /* An array index points to threshold just below or equal to usage. */
147 int current_threshold;
148 /* Size of entries[] */
149 unsigned int size;
150 /* Array of thresholds */
Gustavo A. R. Silva307ed942020-03-23 18:36:10 -0500151 struct mem_cgroup_threshold entries[];
Michal Hocko33398cf2015-09-08 15:01:02 -0700152};
153
154struct mem_cgroup_thresholds {
155 /* Primary thresholds array */
156 struct mem_cgroup_threshold_ary *primary;
157 /*
158 * Spare threshold array.
159 * This is needed to make mem_cgroup_unregister_event() "never fail".
160 * It must be able to store at least primary->size - 1 entries.
161 */
162 struct mem_cgroup_threshold_ary *spare;
163};
164
Johannes Weiner567e9ab2016-01-20 15:02:24 -0800165enum memcg_kmem_state {
166 KMEM_NONE,
167 KMEM_ALLOCATED,
168 KMEM_ONLINE,
169};
170
Aaron Lue81bf972018-06-07 17:09:44 -0700171#if defined(CONFIG_SMP)
172struct memcg_padding {
173 char x[0];
174} ____cacheline_internodealigned_in_smp;
175#define MEMCG_PADDING(name) struct memcg_padding name;
176#else
177#define MEMCG_PADDING(name)
178#endif
179
Michal Hocko33398cf2015-09-08 15:01:02 -0700180/*
Tejun Heo97b27822019-08-26 09:06:56 -0700181 * Remember four most recent foreign writebacks with dirty pages in this
182 * cgroup. Inode sharing is expected to be uncommon and, even if we miss
183 * one in a given round, we're likely to catch it later if it keeps
184 * foreign-dirtying, so a fairly low count should be enough.
185 *
186 * See mem_cgroup_track_foreign_dirty_slowpath() for details.
187 */
188#define MEMCG_CGWB_FRN_CNT 4
189
190struct memcg_cgwb_frn {
191 u64 bdi_id; /* bdi->id of the foreign inode */
192 int memcg_id; /* memcg->css.id of foreign inode */
193 u64 at; /* jiffies_64 at the time of dirtying */
194 struct wb_completion done; /* tracks in-flight foreign writebacks */
195};
196
197/*
Michal Hocko33398cf2015-09-08 15:01:02 -0700198 * The memory controller data structure. The memory controller controls both
199 * page cache and RSS per cgroup. We would eventually like to provide
200 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
201 * to help the administrator determine what knobs to tune.
202 */
203struct mem_cgroup {
204 struct cgroup_subsys_state css;
205
Johannes Weiner73f576c2016-07-20 15:44:57 -0700206 /* Private memcg ID. Used to ID objects that outlive the cgroup */
207 struct mem_cgroup_id id;
208
Michal Hocko33398cf2015-09-08 15:01:02 -0700209 /* Accounted resources */
210 struct page_counter memory;
Vladimir Davydov37e84352016-01-20 15:02:56 -0800211 struct page_counter swap;
Johannes Weiner0db15292016-01-20 15:02:50 -0800212
213 /* Legacy consumer-oriented counters */
Michal Hocko33398cf2015-09-08 15:01:02 -0700214 struct page_counter memsw;
215 struct page_counter kmem;
Johannes Weiner0db15292016-01-20 15:02:50 -0800216 struct page_counter tcpmem;
Michal Hocko33398cf2015-09-08 15:01:02 -0700217
Roman Gushchin23067152018-06-07 17:06:22 -0700218 /* Upper bound of normal memory consumption range */
Michal Hocko33398cf2015-09-08 15:01:02 -0700219 unsigned long high;
220
Johannes Weinerf7e1cb62016-01-14 15:21:29 -0800221 /* Range enforcement for interrupt charges */
222 struct work_struct high_work;
223
Michal Hocko33398cf2015-09-08 15:01:02 -0700224 unsigned long soft_limit;
225
226 /* vmpressure notifications */
227 struct vmpressure vmpressure;
228
Michal Hocko33398cf2015-09-08 15:01:02 -0700229 /*
230 * Should the accounting and control be hierarchical, per subtree?
231 */
232 bool use_hierarchy;
233
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700234 /*
235 * Should the OOM killer kill all belonging tasks, had it kill one?
236 */
237 bool oom_group;
238
Michal Hocko33398cf2015-09-08 15:01:02 -0700239 /* protected by memcg_oom_lock */
240 bool oom_lock;
241 int under_oom;
242
243 int swappiness;
244 /* OOM-Killer disable */
245 int oom_kill_disable;
246
Shakeel Butt1e577f92019-07-11 20:55:55 -0700247 /* memory.events and memory.events.local */
Tejun Heo472912a2015-09-18 18:01:59 -0400248 struct cgroup_file events_file;
Shakeel Butt1e577f92019-07-11 20:55:55 -0700249 struct cgroup_file events_local_file;
Tejun Heo472912a2015-09-18 18:01:59 -0400250
Tejun Heof3a53a32018-06-07 17:05:35 -0700251 /* handle for "memory.swap.events" */
252 struct cgroup_file swap_events_file;
253
Michal Hocko33398cf2015-09-08 15:01:02 -0700254 /* protect arrays of thresholds */
255 struct mutex thresholds_lock;
256
257 /* thresholds for memory usage. RCU-protected */
258 struct mem_cgroup_thresholds thresholds;
259
260 /* thresholds for mem+swap usage. RCU-protected */
261 struct mem_cgroup_thresholds memsw_thresholds;
262
263 /* For oom notifier event fd */
264 struct list_head oom_notify;
265
266 /*
267 * Should we move charges of a task when a task is moved into this
268 * mem_cgroup ? And what type of charges should we move ?
269 */
270 unsigned long move_charge_at_immigrate;
Aaron Lue81bf972018-06-07 17:09:44 -0700271 /* taken only while moving_account > 0 */
272 spinlock_t move_lock;
273 unsigned long move_lock_flags;
274
275 MEMCG_PADDING(_pad1_);
276
Michal Hocko33398cf2015-09-08 15:01:02 -0700277 /*
278 * set > 0 if pages under this cgroup are moving to other cgroup.
279 */
280 atomic_t moving_account;
Michal Hocko33398cf2015-09-08 15:01:02 -0700281 struct task_struct *move_lock_task;
Johannes Weinera983b5e2018-01-31 16:16:45 -0800282
Johannes Weiner815744d2019-06-13 15:55:46 -0700283 /* Legacy local VM stats and events */
284 struct memcg_vmstats_percpu __percpu *vmstats_local;
285
286 /* Subtree VM stats and events (batched updates) */
Chris Down871789d2019-05-14 15:46:57 -0700287 struct memcg_vmstats_percpu __percpu *vmstats_percpu;
Aaron Lue81bf972018-06-07 17:09:44 -0700288
289 MEMCG_PADDING(_pad2_);
290
Chris Down871789d2019-05-14 15:46:57 -0700291 atomic_long_t vmstats[MEMCG_NR_STAT];
292 atomic_long_t vmevents[NR_VM_EVENT_ITEMS];
Johannes Weiner42a30032019-05-14 15:47:12 -0700293
Johannes Weiner815744d2019-06-13 15:55:46 -0700294 /* memory.events */
Johannes Weiner42a30032019-05-14 15:47:12 -0700295 atomic_long_t memory_events[MEMCG_NR_MEMORY_EVENTS];
Shakeel Butt1e577f92019-07-11 20:55:55 -0700296 atomic_long_t memory_events_local[MEMCG_NR_MEMORY_EVENTS];
Michal Hocko33398cf2015-09-08 15:01:02 -0700297
Johannes Weinerd886f4e2016-01-20 15:02:47 -0800298 unsigned long socket_pressure;
299
300 /* Legacy tcp memory accounting */
Johannes Weiner0db15292016-01-20 15:02:50 -0800301 bool tcpmem_active;
302 int tcpmem_pressure;
Johannes Weinerd886f4e2016-01-20 15:02:47 -0800303
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700304#ifdef CONFIG_MEMCG_KMEM
Michal Hocko33398cf2015-09-08 15:01:02 -0700305 /* Index in the kmem_cache->memcg_params.memcg_caches array */
306 int kmemcg_id;
Johannes Weiner567e9ab2016-01-20 15:02:24 -0800307 enum memcg_kmem_state kmem_state;
Tejun Heobc2791f2017-02-22 15:41:21 -0800308 struct list_head kmem_caches;
Michal Hocko33398cf2015-09-08 15:01:02 -0700309#endif
310
Michal Hocko33398cf2015-09-08 15:01:02 -0700311#ifdef CONFIG_CGROUP_WRITEBACK
312 struct list_head cgwb_list;
313 struct wb_domain cgwb_domain;
Tejun Heo97b27822019-08-26 09:06:56 -0700314 struct memcg_cgwb_frn cgwb_frn[MEMCG_CGWB_FRN_CNT];
Michal Hocko33398cf2015-09-08 15:01:02 -0700315#endif
316
317 /* List of events which userspace want to receive */
318 struct list_head event_list;
319 spinlock_t event_list_lock;
320
Yang Shi87eaceb2019-09-23 15:38:15 -0700321#ifdef CONFIG_TRANSPARENT_HUGEPAGE
322 struct deferred_split deferred_split_queue;
323#endif
324
Michal Hocko33398cf2015-09-08 15:01:02 -0700325 struct mem_cgroup_per_node *nodeinfo[0];
326 /* WARNING: nodeinfo must be the last member here */
327};
Johannes Weiner7d828602016-01-14 15:20:56 -0800328
Johannes Weinera983b5e2018-01-31 16:16:45 -0800329/*
330 * size of first charge trial. "32" comes from vmscan.c's magic value.
331 * TODO: maybe necessary to use big numbers in big irons.
332 */
333#define MEMCG_CHARGE_BATCH 32U
334
Johannes Weiner7d828602016-01-14 15:20:56 -0800335extern struct mem_cgroup *root_mem_cgroup;
Tejun Heo56161632015-05-22 17:13:20 -0400336
Kirill Tkhaidfd2f102018-08-17 15:48:06 -0700337static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
338{
339 return (memcg == root_mem_cgroup);
340}
341
Johannes Weiner23047a92016-03-15 14:57:16 -0700342static inline bool mem_cgroup_disabled(void)
343{
344 return !cgroup_subsys_enabled(memory_cgrp_subsys);
345}
346
Chris Down1bc63fb2019-10-06 17:58:38 -0700347static inline unsigned long mem_cgroup_protection(struct mem_cgroup *memcg,
348 bool in_low_reclaim)
Chris Down9783aa92019-10-06 17:58:32 -0700349{
Chris Down1bc63fb2019-10-06 17:58:38 -0700350 if (mem_cgroup_disabled())
351 return 0;
Chris Down9783aa92019-10-06 17:58:32 -0700352
Chris Down1bc63fb2019-10-06 17:58:38 -0700353 if (in_low_reclaim)
354 return READ_ONCE(memcg->memory.emin);
355
356 return max(READ_ONCE(memcg->memory.emin),
357 READ_ONCE(memcg->memory.elow));
Chris Down9783aa92019-10-06 17:58:32 -0700358}
359
Roman Gushchinbf8d5d52018-06-07 17:07:46 -0700360enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root,
361 struct mem_cgroup *memcg);
Johannes Weiner241994ed2015-02-11 15:26:06 -0800362
Johannes Weiner00501b52014-08-08 14:19:20 -0700363int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800364 gfp_t gfp_mask, struct mem_cgroup **memcgp,
365 bool compound);
Tejun Heo2cf85582018-07-03 11:14:56 -0400366int mem_cgroup_try_charge_delay(struct page *page, struct mm_struct *mm,
367 gfp_t gfp_mask, struct mem_cgroup **memcgp,
368 bool compound);
Johannes Weiner00501b52014-08-08 14:19:20 -0700369void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800370 bool lrucare, bool compound);
371void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg,
372 bool compound);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700373void mem_cgroup_uncharge(struct page *page);
Johannes Weiner747db952014-08-08 14:19:24 -0700374void mem_cgroup_uncharge_list(struct list_head *page_list);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700375
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700376void mem_cgroup_migrate(struct page *oldpage, struct page *newpage);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700377
Mel Gormanef8f2322016-07-28 15:46:05 -0700378static struct mem_cgroup_per_node *
379mem_cgroup_nodeinfo(struct mem_cgroup *memcg, int nid)
Johannes Weiner55779ec2016-07-28 15:45:10 -0700380{
Mel Gormanef8f2322016-07-28 15:46:05 -0700381 return memcg->nodeinfo[nid];
Johannes Weiner55779ec2016-07-28 15:45:10 -0700382}
383
384/**
Johannes Weiner867e5e12019-11-30 17:55:34 -0800385 * mem_cgroup_lruvec - get the lru list vector for a memcg & node
Johannes Weiner55779ec2016-07-28 15:45:10 -0700386 * @memcg: memcg of the wanted lruvec
387 *
Johannes Weiner867e5e12019-11-30 17:55:34 -0800388 * Returns the lru list vector holding pages for a given @memcg &
389 * @node combination. This can be the node lruvec, if the memory
390 * controller is disabled.
Johannes Weiner55779ec2016-07-28 15:45:10 -0700391 */
Johannes Weiner867e5e12019-11-30 17:55:34 -0800392static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
393 struct pglist_data *pgdat)
Johannes Weiner55779ec2016-07-28 15:45:10 -0700394{
Mel Gormanef8f2322016-07-28 15:46:05 -0700395 struct mem_cgroup_per_node *mz;
Johannes Weiner55779ec2016-07-28 15:45:10 -0700396 struct lruvec *lruvec;
397
398 if (mem_cgroup_disabled()) {
Johannes Weiner867e5e12019-11-30 17:55:34 -0800399 lruvec = &pgdat->__lruvec;
Johannes Weiner55779ec2016-07-28 15:45:10 -0700400 goto out;
401 }
402
Johannes Weiner1b051172019-11-30 17:55:52 -0800403 if (!memcg)
404 memcg = root_mem_cgroup;
405
Mel Gormanef8f2322016-07-28 15:46:05 -0700406 mz = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
Johannes Weiner55779ec2016-07-28 15:45:10 -0700407 lruvec = &mz->lruvec;
408out:
409 /*
410 * Since a node can be onlined after the mem_cgroup was created,
Mel Gorman599d0c92016-07-28 15:45:31 -0700411 * we have to be prepared to initialize lruvec->pgdat here;
Johannes Weiner55779ec2016-07-28 15:45:10 -0700412 * and if offlined then reonlined, we need to reinitialize it.
413 */
Mel Gormanef8f2322016-07-28 15:46:05 -0700414 if (unlikely(lruvec->pgdat != pgdat))
415 lruvec->pgdat = pgdat;
Johannes Weiner55779ec2016-07-28 15:45:10 -0700416 return lruvec;
417}
418
Mel Gorman599d0c92016-07-28 15:45:31 -0700419struct lruvec *mem_cgroup_page_lruvec(struct page *, struct pglist_data *);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -0800420
Michal Hocko64219992015-09-08 15:01:07 -0700421struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
Vladimir Davydove993d902015-09-09 15:35:35 -0700422
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700423struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm);
424
Shakeel Buttf745c6f2018-08-17 15:46:44 -0700425struct mem_cgroup *get_mem_cgroup_from_page(struct page *page);
426
Michal Hocko33398cf2015-09-08 15:01:02 -0700427static inline
428struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
429 return css ? container_of(css, struct mem_cgroup, css) : NULL;
430}
431
Roman Gushchindc0b5862018-08-17 15:46:36 -0700432static inline void mem_cgroup_put(struct mem_cgroup *memcg)
433{
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700434 if (memcg)
435 css_put(&memcg->css);
Roman Gushchindc0b5862018-08-17 15:46:36 -0700436}
437
Johannes Weiner8e8ae642016-01-14 15:21:32 -0800438#define mem_cgroup_from_counter(counter, member) \
439 container_of(counter, struct mem_cgroup, member)
440
Michal Hocko33398cf2015-09-08 15:01:02 -0700441struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
442 struct mem_cgroup *,
443 struct mem_cgroup_reclaim_cookie *);
444void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700445int mem_cgroup_scan_tasks(struct mem_cgroup *,
446 int (*)(struct task_struct *, void *), void *);
Michal Hocko33398cf2015-09-08 15:01:02 -0700447
Johannes Weiner23047a92016-03-15 14:57:16 -0700448static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
449{
450 if (mem_cgroup_disabled())
451 return 0;
452
Johannes Weiner73f576c2016-07-20 15:44:57 -0700453 return memcg->id.id;
Johannes Weiner23047a92016-03-15 14:57:16 -0700454}
Johannes Weiner73f576c2016-07-20 15:44:57 -0700455struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
Johannes Weiner23047a92016-03-15 14:57:16 -0700456
Chris Downaa9694b2019-03-05 15:45:52 -0800457static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
458{
459 return mem_cgroup_from_css(seq_css(m));
460}
461
Roman Gushchin22621852017-07-06 15:40:25 -0700462static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
463{
464 struct mem_cgroup_per_node *mz;
465
466 if (mem_cgroup_disabled())
467 return NULL;
468
469 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
470 return mz->memcg;
471}
472
Johannes Weiner8e8ae642016-01-14 15:21:32 -0800473/**
474 * parent_mem_cgroup - find the accounting parent of a memcg
475 * @memcg: memcg whose parent to find
476 *
477 * Returns the parent memcg, or NULL if this is the root or the memory
478 * controller is in legacy no-hierarchy mode.
479 */
480static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
481{
482 if (!memcg->memory.parent)
483 return NULL;
484 return mem_cgroup_from_counter(memcg->memory.parent, memory);
485}
486
Michal Hocko33398cf2015-09-08 15:01:02 -0700487static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
488 struct mem_cgroup *root)
489{
490 if (root == memcg)
491 return true;
492 if (!root->use_hierarchy)
493 return false;
494 return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
495}
Glauber Costae1aab162011-12-11 21:47:03 +0000496
Johannes Weiner2314b422014-12-10 15:44:33 -0800497static inline bool mm_match_cgroup(struct mm_struct *mm,
498 struct mem_cgroup *memcg)
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800499{
Johannes Weiner587af302012-10-08 16:34:12 -0700500 struct mem_cgroup *task_memcg;
Johannes Weiner413918b2014-12-10 15:44:30 -0800501 bool match = false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -0700502
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800503 rcu_read_lock();
Johannes Weiner587af302012-10-08 16:34:12 -0700504 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
Johannes Weiner413918b2014-12-10 15:44:30 -0800505 if (task_memcg)
Johannes Weiner2314b422014-12-10 15:44:33 -0800506 match = mem_cgroup_is_descendant(task_memcg, memcg);
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800507 rcu_read_unlock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -0700508 return match;
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800509}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800510
Michal Hocko64219992015-09-08 15:01:07 -0700511struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page);
Vladimir Davydov2fc04522015-09-09 15:35:28 -0700512ino_t page_cgroup_ino(struct page *page);
Wu Fengguangd3242362009-12-16 12:19:59 +0100513
Vladimir Davydoveb01aaa2016-01-20 15:03:02 -0800514static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
515{
516 if (mem_cgroup_disabled())
517 return true;
518 return !!(memcg->css.flags & CSS_ONLINE);
519}
520
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800521/*
522 * For memory reclaim.
523 */
Ying Han889976d2011-05-26 16:25:33 -0700524int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
Michal Hocko33398cf2015-09-08 15:01:02 -0700525
526void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
Michal Hockob4536f0c82017-01-10 16:58:04 -0800527 int zid, int nr_pages);
Michal Hocko33398cf2015-09-08 15:01:02 -0700528
Michal Hocko33398cf2015-09-08 15:01:02 -0700529static inline
Michal Hockob4536f0c82017-01-10 16:58:04 -0800530unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
531 enum lru_list lru, int zone_idx)
532{
533 struct mem_cgroup_per_node *mz;
534
535 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
536 return mz->lru_zone_size[zone_idx][lru];
Michal Hocko33398cf2015-09-08 15:01:02 -0700537}
538
Tejun Heob23afb92015-11-05 18:46:11 -0800539void mem_cgroup_handle_over_high(void);
540
Roman Gushchinbbec2e12018-06-07 17:06:18 -0700541unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700542
Chris Down9783aa92019-10-06 17:58:32 -0700543unsigned long mem_cgroup_size(struct mem_cgroup *memcg);
544
yuzhoujianf0c867d2018-12-28 00:36:10 -0800545void mem_cgroup_print_oom_context(struct mem_cgroup *memcg,
Michal Hocko64219992015-09-08 15:01:07 -0700546 struct task_struct *p);
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800547
yuzhoujianf0c867d2018-12-28 00:36:10 -0800548void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg);
549
Michal Hocko29ef6802018-08-17 15:47:11 -0700550static inline void mem_cgroup_enter_user_fault(void)
Johannes Weiner519e5242013-09-12 15:13:42 -0700551{
Michal Hocko29ef6802018-08-17 15:47:11 -0700552 WARN_ON(current->in_user_fault);
553 current->in_user_fault = 1;
Johannes Weiner519e5242013-09-12 15:13:42 -0700554}
555
Michal Hocko29ef6802018-08-17 15:47:11 -0700556static inline void mem_cgroup_exit_user_fault(void)
Johannes Weiner519e5242013-09-12 15:13:42 -0700557{
Michal Hocko29ef6802018-08-17 15:47:11 -0700558 WARN_ON(!current->in_user_fault);
559 current->in_user_fault = 0;
Johannes Weiner519e5242013-09-12 15:13:42 -0700560}
561
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700562static inline bool task_in_memcg_oom(struct task_struct *p)
563{
Tejun Heo626ebc42015-11-05 18:46:09 -0800564 return p->memcg_in_oom;
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700565}
566
Johannes Weiner49426422013-10-16 13:46:59 -0700567bool mem_cgroup_oom_synchronize(bool wait);
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700568struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
569 struct mem_cgroup *oom_domain);
570void mem_cgroup_print_oom_group(struct mem_cgroup *memcg);
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700571
Andrew Mortonc255a452012-07-31 16:43:02 -0700572#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -0800573extern int do_swap_account;
574#endif
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800575
Johannes Weiner739f79f2017-08-18 15:15:48 -0700576struct mem_cgroup *lock_page_memcg(struct page *page);
577void __unlock_page_memcg(struct mem_cgroup *memcg);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700578void unlock_page_memcg(struct page *page);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700579
Greg Thelen0b3d6e62019-04-05 18:39:18 -0700580/*
581 * idx can be of type enum memcg_stat_item or node_stat_item.
582 * Keep in sync with memcg_exact_page_state().
583 */
Johannes Weiner42a30032019-05-14 15:47:12 -0700584static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
585{
586 long x = atomic_long_read(&memcg->vmstats[idx]);
587#ifdef CONFIG_SMP
588 if (x < 0)
589 x = 0;
590#endif
591 return x;
592}
593
594/*
595 * idx can be of type enum memcg_stat_item or node_stat_item.
596 * Keep in sync with memcg_exact_page_state().
597 */
Johannes Weiner205b20c2019-05-14 15:47:06 -0700598static inline unsigned long memcg_page_state_local(struct mem_cgroup *memcg,
599 int idx)
Johannes Weiner2a2e4882017-05-03 14:55:03 -0700600{
Johannes Weiner815744d2019-06-13 15:55:46 -0700601 long x = 0;
602 int cpu;
603
604 for_each_possible_cpu(cpu)
605 x += per_cpu(memcg->vmstats_local->stat[idx], cpu);
Johannes Weinera983b5e2018-01-31 16:16:45 -0800606#ifdef CONFIG_SMP
607 if (x < 0)
608 x = 0;
609#endif
610 return x;
Johannes Weiner2a2e4882017-05-03 14:55:03 -0700611}
612
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700613void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val);
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700614
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -0700615/* idx can be of type enum memcg_stat_item or node_stat_item */
Johannes Weinerccda7f42017-05-03 14:55:16 -0700616static inline void mod_memcg_state(struct mem_cgroup *memcg,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -0700617 int idx, int val)
Johannes Weiner2a2e4882017-05-03 14:55:03 -0700618{
Johannes Weinerc3cc3912018-02-21 14:45:24 -0800619 unsigned long flags;
620
621 local_irq_save(flags);
Johannes Weinera983b5e2018-01-31 16:16:45 -0800622 __mod_memcg_state(memcg, idx, val);
Johannes Weinerc3cc3912018-02-21 14:45:24 -0800623 local_irq_restore(flags);
Johannes Weiner2a2e4882017-05-03 14:55:03 -0700624}
625
Michal Hocko33398cf2015-09-08 15:01:02 -0700626/**
Johannes Weinerccda7f42017-05-03 14:55:16 -0700627 * mod_memcg_page_state - update page state statistics
Johannes Weiner62cccb82016-03-15 14:57:22 -0700628 * @page: the page
Michal Hocko33398cf2015-09-08 15:01:02 -0700629 * @idx: page state item to account
630 * @val: number of pages (positive or negative)
631 *
Johannes Weinerfdf1cdb2016-03-15 14:57:25 -0700632 * The @page must be locked or the caller must use lock_page_memcg()
633 * to prevent double accounting when the page is concurrently being
634 * moved to another memcg:
Johannes Weiner81f8c3a2016-03-15 14:57:04 -0700635 *
Johannes Weinerfdf1cdb2016-03-15 14:57:25 -0700636 * lock_page(page) or lock_page_memcg(page)
Johannes Weiner81f8c3a2016-03-15 14:57:04 -0700637 * if (TestClearPageState(page))
Johannes Weinerccda7f42017-05-03 14:55:16 -0700638 * mod_memcg_page_state(page, state, -1);
Johannes Weinerfdf1cdb2016-03-15 14:57:25 -0700639 * unlock_page(page) or unlock_page_memcg(page)
Johannes Weiner2a2e4882017-05-03 14:55:03 -0700640 *
641 * Kernel pages are an exception to this, since they'll never move.
Michal Hocko33398cf2015-09-08 15:01:02 -0700642 */
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700643static inline void __mod_memcg_page_state(struct page *page,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -0700644 int idx, int val)
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700645{
646 if (page->mem_cgroup)
647 __mod_memcg_state(page->mem_cgroup, idx, val);
648}
649
Johannes Weinerccda7f42017-05-03 14:55:16 -0700650static inline void mod_memcg_page_state(struct page *page,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -0700651 int idx, int val)
Michal Hocko33398cf2015-09-08 15:01:02 -0700652{
Johannes Weiner62cccb82016-03-15 14:57:22 -0700653 if (page->mem_cgroup)
Johannes Weinerccda7f42017-05-03 14:55:16 -0700654 mod_memcg_state(page->mem_cgroup, idx, val);
Michal Hocko33398cf2015-09-08 15:01:02 -0700655}
656
Johannes Weiner42a30032019-05-14 15:47:12 -0700657static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
658 enum node_stat_item idx)
659{
660 struct mem_cgroup_per_node *pn;
661 long x;
662
663 if (mem_cgroup_disabled())
664 return node_page_state(lruvec_pgdat(lruvec), idx);
665
666 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
667 x = atomic_long_read(&pn->lruvec_stat[idx]);
668#ifdef CONFIG_SMP
669 if (x < 0)
670 x = 0;
671#endif
672 return x;
673}
674
Johannes Weiner205b20c2019-05-14 15:47:06 -0700675static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
676 enum node_stat_item idx)
Greg Thelen2a7106f2011-01-13 15:47:37 -0800677{
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700678 struct mem_cgroup_per_node *pn;
Johannes Weiner815744d2019-06-13 15:55:46 -0700679 long x = 0;
680 int cpu;
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700681
682 if (mem_cgroup_disabled())
683 return node_page_state(lruvec_pgdat(lruvec), idx);
684
685 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
Johannes Weiner815744d2019-06-13 15:55:46 -0700686 for_each_possible_cpu(cpu)
687 x += per_cpu(pn->lruvec_stat_local->count[idx], cpu);
Johannes Weinera983b5e2018-01-31 16:16:45 -0800688#ifdef CONFIG_SMP
689 if (x < 0)
690 x = 0;
691#endif
692 return x;
Greg Thelen2a7106f2011-01-13 15:47:37 -0800693}
694
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700695void __mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
696 int val);
Roman Gushchinec9f0232019-08-13 15:37:41 -0700697void __mod_lruvec_slab_state(void *p, enum node_stat_item idx, int val);
Roman Gushchin8380ce42020-03-28 19:17:25 -0700698void mod_memcg_obj_state(void *p, int idx, int val);
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700699
700static inline void mod_lruvec_state(struct lruvec *lruvec,
701 enum node_stat_item idx, int val)
702{
Johannes Weinerc3cc3912018-02-21 14:45:24 -0800703 unsigned long flags;
704
705 local_irq_save(flags);
Johannes Weiner28454262018-01-31 16:16:41 -0800706 __mod_lruvec_state(lruvec, idx, val);
Johannes Weinerc3cc3912018-02-21 14:45:24 -0800707 local_irq_restore(flags);
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700708}
709
710static inline void __mod_lruvec_page_state(struct page *page,
711 enum node_stat_item idx, int val)
712{
Johannes Weiner28454262018-01-31 16:16:41 -0800713 pg_data_t *pgdat = page_pgdat(page);
714 struct lruvec *lruvec;
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700715
Johannes Weiner28454262018-01-31 16:16:41 -0800716 /* Untracked pages have no memcg, no lruvec. Update only the node */
717 if (!page->mem_cgroup) {
718 __mod_node_page_state(pgdat, idx, val);
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700719 return;
Johannes Weiner28454262018-01-31 16:16:41 -0800720 }
721
Johannes Weiner867e5e12019-11-30 17:55:34 -0800722 lruvec = mem_cgroup_lruvec(page->mem_cgroup, pgdat);
Johannes Weiner28454262018-01-31 16:16:41 -0800723 __mod_lruvec_state(lruvec, idx, val);
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700724}
725
726static inline void mod_lruvec_page_state(struct page *page,
727 enum node_stat_item idx, int val)
728{
Johannes Weinerc3cc3912018-02-21 14:45:24 -0800729 unsigned long flags;
730
731 local_irq_save(flags);
Johannes Weiner28454262018-01-31 16:16:41 -0800732 __mod_lruvec_page_state(page, idx, val);
Johannes Weinerc3cc3912018-02-21 14:45:24 -0800733 local_irq_restore(flags);
Greg Thelen2a7106f2011-01-13 15:47:37 -0800734}
735
Mel Gormanef8f2322016-07-28 15:46:05 -0700736unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
Andrew Morton0608f432013-09-24 15:27:41 -0700737 gfp_t gfp_mask,
738 unsigned long *total_scanned);
David Rientjesa63d83f2010-08-09 17:19:46 -0700739
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700740void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
741 unsigned long count);
Johannes Weinerc9019e92018-01-31 16:16:37 -0800742
Roman Gushchin22621852017-07-06 15:40:25 -0700743static inline void count_memcg_events(struct mem_cgroup *memcg,
Johannes Weinere27be242018-04-10 16:29:45 -0700744 enum vm_event_item idx,
745 unsigned long count)
Roman Gushchin22621852017-07-06 15:40:25 -0700746{
Johannes Weinerc3cc3912018-02-21 14:45:24 -0800747 unsigned long flags;
748
749 local_irq_save(flags);
Johannes Weinera983b5e2018-01-31 16:16:45 -0800750 __count_memcg_events(memcg, idx, count);
Johannes Weinerc3cc3912018-02-21 14:45:24 -0800751 local_irq_restore(flags);
Roman Gushchin22621852017-07-06 15:40:25 -0700752}
753
754static inline void count_memcg_page_event(struct page *page,
Johannes Weinere27be242018-04-10 16:29:45 -0700755 enum vm_event_item idx)
Roman Gushchin22621852017-07-06 15:40:25 -0700756{
757 if (page->mem_cgroup)
758 count_memcg_events(page->mem_cgroup, idx, 1);
759}
760
761static inline void count_memcg_event_mm(struct mm_struct *mm,
762 enum vm_event_item idx)
David Rientjes68ae5642012-12-12 13:51:57 -0800763{
Michal Hocko33398cf2015-09-08 15:01:02 -0700764 struct mem_cgroup *memcg;
765
David Rientjes68ae5642012-12-12 13:51:57 -0800766 if (mem_cgroup_disabled())
767 return;
Michal Hocko33398cf2015-09-08 15:01:02 -0700768
769 rcu_read_lock();
770 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
Roman Gushchinfe6bdfc2018-06-14 15:28:05 -0700771 if (likely(memcg))
Johannes Weinerc9019e92018-01-31 16:16:37 -0800772 count_memcg_events(memcg, idx, 1);
Michal Hocko33398cf2015-09-08 15:01:02 -0700773 rcu_read_unlock();
David Rientjes68ae5642012-12-12 13:51:57 -0800774}
Johannes Weinerc9019e92018-01-31 16:16:37 -0800775
Johannes Weinere27be242018-04-10 16:29:45 -0700776static inline void memcg_memory_event(struct mem_cgroup *memcg,
777 enum memcg_memory_event event)
Johannes Weinerc9019e92018-01-31 16:16:37 -0800778{
Shakeel Butt1e577f92019-07-11 20:55:55 -0700779 atomic_long_inc(&memcg->memory_events_local[event]);
780 cgroup_file_notify(&memcg->events_local_file);
781
Chris Down9852ae32019-05-31 22:30:22 -0700782 do {
783 atomic_long_inc(&memcg->memory_events[event]);
784 cgroup_file_notify(&memcg->events_file);
785
Yafang Shao04fd61a42020-05-13 17:50:34 -0700786 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
787 break;
Chris Down9852ae32019-05-31 22:30:22 -0700788 if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
789 break;
790 } while ((memcg = parent_mem_cgroup(memcg)) &&
791 !mem_cgroup_is_root(memcg));
Johannes Weinerc9019e92018-01-31 16:16:37 -0800792}
793
Roman Gushchinfe6bdfc2018-06-14 15:28:05 -0700794static inline void memcg_memory_event_mm(struct mm_struct *mm,
795 enum memcg_memory_event event)
796{
797 struct mem_cgroup *memcg;
798
799 if (mem_cgroup_disabled())
800 return;
801
802 rcu_read_lock();
803 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
804 if (likely(memcg))
805 memcg_memory_event(memcg, event);
806 rcu_read_unlock();
807}
808
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800809#ifdef CONFIG_TRANSPARENT_HUGEPAGE
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -0800810void mem_cgroup_split_huge_fixup(struct page *head);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800811#endif
812
Andrew Mortonc255a452012-07-31 16:43:02 -0700813#else /* CONFIG_MEMCG */
Johannes Weiner23047a92016-03-15 14:57:16 -0700814
815#define MEM_CGROUP_ID_SHIFT 0
816#define MEM_CGROUP_ID_MAX 0
817
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800818struct mem_cgroup;
819
Kirill Tkhaidfd2f102018-08-17 15:48:06 -0700820static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
821{
822 return true;
823}
824
Johannes Weiner23047a92016-03-15 14:57:16 -0700825static inline bool mem_cgroup_disabled(void)
826{
827 return true;
828}
829
Johannes Weinere27be242018-04-10 16:29:45 -0700830static inline void memcg_memory_event(struct mem_cgroup *memcg,
831 enum memcg_memory_event event)
Johannes Weiner241994ed2015-02-11 15:26:06 -0800832{
833}
834
Roman Gushchinfe6bdfc2018-06-14 15:28:05 -0700835static inline void memcg_memory_event_mm(struct mm_struct *mm,
836 enum memcg_memory_event event)
837{
838}
839
Chris Down1bc63fb2019-10-06 17:58:38 -0700840static inline unsigned long mem_cgroup_protection(struct mem_cgroup *memcg,
841 bool in_low_reclaim)
Chris Down9783aa92019-10-06 17:58:32 -0700842{
Chris Down1bc63fb2019-10-06 17:58:38 -0700843 return 0;
Chris Down9783aa92019-10-06 17:58:32 -0700844}
845
Roman Gushchinbf8d5d52018-06-07 17:07:46 -0700846static inline enum mem_cgroup_protection mem_cgroup_protected(
847 struct mem_cgroup *root, struct mem_cgroup *memcg)
Johannes Weiner241994ed2015-02-11 15:26:06 -0800848{
Roman Gushchinbf8d5d52018-06-07 17:07:46 -0700849 return MEMCG_PROT_NONE;
Johannes Weiner241994ed2015-02-11 15:26:06 -0800850}
851
Johannes Weiner00501b52014-08-08 14:19:20 -0700852static inline int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
853 gfp_t gfp_mask,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800854 struct mem_cgroup **memcgp,
855 bool compound)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800856{
Johannes Weiner00501b52014-08-08 14:19:20 -0700857 *memcgp = NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800858 return 0;
859}
860
Tejun Heo2cf85582018-07-03 11:14:56 -0400861static inline int mem_cgroup_try_charge_delay(struct page *page,
862 struct mm_struct *mm,
863 gfp_t gfp_mask,
864 struct mem_cgroup **memcgp,
865 bool compound)
866{
867 *memcgp = NULL;
868 return 0;
869}
870
Johannes Weiner00501b52014-08-08 14:19:20 -0700871static inline void mem_cgroup_commit_charge(struct page *page,
872 struct mem_cgroup *memcg,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800873 bool lrucare, bool compound)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800874{
875}
876
Johannes Weiner00501b52014-08-08 14:19:20 -0700877static inline void mem_cgroup_cancel_charge(struct page *page,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800878 struct mem_cgroup *memcg,
879 bool compound)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800880{
881}
882
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700883static inline void mem_cgroup_uncharge(struct page *page)
884{
885}
886
Johannes Weiner747db952014-08-08 14:19:24 -0700887static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -0800888{
889}
890
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700891static inline void mem_cgroup_migrate(struct page *old, struct page *new)
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700892{
893}
894
Johannes Weiner867e5e12019-11-30 17:55:34 -0800895static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
896 struct pglist_data *pgdat)
Johannes Weiner925b7672012-01-12 17:18:15 -0800897{
Johannes Weiner867e5e12019-11-30 17:55:34 -0800898 return &pgdat->__lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -0800899}
900
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700901static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
Mel Gorman599d0c92016-07-28 15:45:31 -0700902 struct pglist_data *pgdat)
Minchan Kim3f58a822011-03-22 16:32:53 -0700903{
Johannes Weiner867e5e12019-11-30 17:55:34 -0800904 return &pgdat->__lruvec;
Balbir Singh66e17072008-02-07 00:13:56 -0800905}
906
Johannes Weinerb9107182019-11-30 17:55:59 -0800907static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
908{
909 return NULL;
910}
911
Johannes Weiner587af302012-10-08 16:34:12 -0700912static inline bool mm_match_cgroup(struct mm_struct *mm,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700913 struct mem_cgroup *memcg)
Balbir Singhbed71612008-02-07 00:14:01 -0800914{
Johannes Weiner587af302012-10-08 16:34:12 -0700915 return true;
Balbir Singhbed71612008-02-07 00:14:01 -0800916}
917
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700918static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
919{
920 return NULL;
921}
922
Shakeel Buttf745c6f2018-08-17 15:46:44 -0700923static inline struct mem_cgroup *get_mem_cgroup_from_page(struct page *page)
924{
925 return NULL;
926}
927
Roman Gushchindc0b5862018-08-17 15:46:36 -0700928static inline void mem_cgroup_put(struct mem_cgroup *memcg)
929{
930}
931
Johannes Weiner56600482012-01-12 17:17:59 -0800932static inline struct mem_cgroup *
933mem_cgroup_iter(struct mem_cgroup *root,
934 struct mem_cgroup *prev,
935 struct mem_cgroup_reclaim_cookie *reclaim)
936{
937 return NULL;
938}
939
940static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
941 struct mem_cgroup *prev)
942{
943}
944
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700945static inline int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
946 int (*fn)(struct task_struct *, void *), void *arg)
947{
948 return 0;
949}
950
Johannes Weiner23047a92016-03-15 14:57:16 -0700951static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800952{
Johannes Weiner23047a92016-03-15 14:57:16 -0700953 return 0;
954}
955
956static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
957{
958 WARN_ON_ONCE(id);
959 /* XXX: This should always return root_mem_cgroup */
960 return NULL;
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800961}
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800962
Chris Downaa9694b2019-03-05 15:45:52 -0800963static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
964{
965 return NULL;
966}
967
Roman Gushchin22621852017-07-06 15:40:25 -0700968static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
969{
970 return NULL;
971}
972
Vladimir Davydoveb01aaa2016-01-20 15:03:02 -0800973static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800974{
Yaowei Bai13308ca2015-11-05 18:47:40 -0800975 return true;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800976}
977
Michal Hockob4536f0c82017-01-10 16:58:04 -0800978static inline
979unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
980 enum lru_list lru, int zone_idx)
981{
982 return 0;
983}
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800984
Roman Gushchinbbec2e12018-06-07 17:06:18 -0700985static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700986{
987 return 0;
988}
989
Chris Down9783aa92019-10-06 17:58:32 -0700990static inline unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
991{
992 return 0;
993}
994
Balbir Singhe2224322009-04-02 16:57:39 -0700995static inline void
yuzhoujianf0c867d2018-12-28 00:36:10 -0800996mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
997{
998}
999
1000static inline void
1001mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
Balbir Singhe2224322009-04-02 16:57:39 -07001002{
1003}
1004
Johannes Weiner739f79f2017-08-18 15:15:48 -07001005static inline struct mem_cgroup *lock_page_memcg(struct page *page)
1006{
1007 return NULL;
1008}
1009
1010static inline void __unlock_page_memcg(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07001011{
1012}
1013
Johannes Weiner62cccb82016-03-15 14:57:22 -07001014static inline void unlock_page_memcg(struct page *page)
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07001015{
1016}
1017
Tejun Heob23afb92015-11-05 18:46:11 -08001018static inline void mem_cgroup_handle_over_high(void)
1019{
1020}
1021
Michal Hocko29ef6802018-08-17 15:47:11 -07001022static inline void mem_cgroup_enter_user_fault(void)
Johannes Weiner519e5242013-09-12 15:13:42 -07001023{
1024}
1025
Michal Hocko29ef6802018-08-17 15:47:11 -07001026static inline void mem_cgroup_exit_user_fault(void)
Johannes Weiner519e5242013-09-12 15:13:42 -07001027{
1028}
1029
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001030static inline bool task_in_memcg_oom(struct task_struct *p)
1031{
1032 return false;
1033}
1034
Johannes Weiner49426422013-10-16 13:46:59 -07001035static inline bool mem_cgroup_oom_synchronize(bool wait)
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001036{
1037 return false;
1038}
1039
Roman Gushchin3d8b38e2018-08-21 21:53:54 -07001040static inline struct mem_cgroup *mem_cgroup_get_oom_group(
1041 struct task_struct *victim, struct mem_cgroup *oom_domain)
1042{
1043 return NULL;
1044}
1045
1046static inline void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
1047{
1048}
1049
Johannes Weiner42a30032019-05-14 15:47:12 -07001050static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
1051{
1052 return 0;
1053}
1054
Johannes Weiner205b20c2019-05-14 15:47:06 -07001055static inline unsigned long memcg_page_state_local(struct mem_cgroup *memcg,
1056 int idx)
Johannes Weiner2a2e4882017-05-03 14:55:03 -07001057{
1058 return 0;
1059}
1060
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001061static inline void __mod_memcg_state(struct mem_cgroup *memcg,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001062 int idx,
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001063 int nr)
1064{
1065}
1066
Johannes Weinerccda7f42017-05-03 14:55:16 -07001067static inline void mod_memcg_state(struct mem_cgroup *memcg,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001068 int idx,
Johannes Weinerccda7f42017-05-03 14:55:16 -07001069 int nr)
Johannes Weiner2a2e4882017-05-03 14:55:03 -07001070{
1071}
1072
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001073static inline void __mod_memcg_page_state(struct page *page,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001074 int idx,
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001075 int nr)
Johannes Weiner2a2e4882017-05-03 14:55:03 -07001076{
1077}
1078
Johannes Weinerccda7f42017-05-03 14:55:16 -07001079static inline void mod_memcg_page_state(struct page *page,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001080 int idx,
Johannes Weinerccda7f42017-05-03 14:55:16 -07001081 int nr)
Johannes Weiner553af432017-03-31 15:11:50 -07001082{
1083}
1084
Johannes Weiner42a30032019-05-14 15:47:12 -07001085static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
1086 enum node_stat_item idx)
1087{
1088 return node_page_state(lruvec_pgdat(lruvec), idx);
1089}
1090
Johannes Weiner205b20c2019-05-14 15:47:06 -07001091static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1092 enum node_stat_item idx)
Greg Thelen2a7106f2011-01-13 15:47:37 -08001093{
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001094 return node_page_state(lruvec_pgdat(lruvec), idx);
Greg Thelen2a7106f2011-01-13 15:47:37 -08001095}
1096
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001097static inline void __mod_lruvec_state(struct lruvec *lruvec,
1098 enum node_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001099{
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001100 __mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
1101}
1102
1103static inline void mod_lruvec_state(struct lruvec *lruvec,
1104 enum node_stat_item idx, int val)
1105{
1106 mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
1107}
1108
1109static inline void __mod_lruvec_page_state(struct page *page,
1110 enum node_stat_item idx, int val)
1111{
1112 __mod_node_page_state(page_pgdat(page), idx, val);
1113}
1114
1115static inline void mod_lruvec_page_state(struct page *page,
1116 enum node_stat_item idx, int val)
1117{
1118 mod_node_page_state(page_pgdat(page), idx, val);
Balbir Singhd69b0422009-06-17 16:26:34 -07001119}
1120
Roman Gushchinec9f0232019-08-13 15:37:41 -07001121static inline void __mod_lruvec_slab_state(void *p, enum node_stat_item idx,
1122 int val)
1123{
1124 struct page *page = virt_to_head_page(p);
1125
1126 __mod_node_page_state(page_pgdat(page), idx, val);
1127}
1128
Roman Gushchin8380ce42020-03-28 19:17:25 -07001129static inline void mod_memcg_obj_state(void *p, int idx, int val)
1130{
1131}
1132
Balbir Singh4e416952009-09-23 15:56:39 -07001133static inline
Mel Gormanef8f2322016-07-28 15:46:05 -07001134unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
Andrew Morton0608f432013-09-24 15:27:41 -07001135 gfp_t gfp_mask,
1136 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07001137{
Andrew Morton0608f432013-09-24 15:27:41 -07001138 return 0;
Balbir Singh4e416952009-09-23 15:56:39 -07001139}
1140
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08001141static inline void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08001142{
1143}
1144
Roman Gushchin22621852017-07-06 15:40:25 -07001145static inline void count_memcg_events(struct mem_cgroup *memcg,
1146 enum vm_event_item idx,
1147 unsigned long count)
1148{
1149}
1150
Kirill Tkhai9851ac12019-05-13 17:16:54 -07001151static inline void __count_memcg_events(struct mem_cgroup *memcg,
1152 enum vm_event_item idx,
1153 unsigned long count)
1154{
1155}
1156
Roman Gushchin22621852017-07-06 15:40:25 -07001157static inline void count_memcg_page_event(struct page *page,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001158 int idx)
Roman Gushchin22621852017-07-06 15:40:25 -07001159{
1160}
1161
Ying Han456f9982011-05-26 16:25:38 -07001162static inline
Roman Gushchin22621852017-07-06 15:40:25 -07001163void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001164{
1165}
Andrew Mortonc255a452012-07-31 16:43:02 -07001166#endif /* CONFIG_MEMCG */
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001167
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001168/* idx can be of type enum memcg_stat_item or node_stat_item */
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001169static inline void __inc_memcg_state(struct mem_cgroup *memcg,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001170 int idx)
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001171{
1172 __mod_memcg_state(memcg, idx, 1);
1173}
1174
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001175/* idx can be of type enum memcg_stat_item or node_stat_item */
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001176static inline void __dec_memcg_state(struct mem_cgroup *memcg,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001177 int idx)
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001178{
1179 __mod_memcg_state(memcg, idx, -1);
1180}
1181
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001182/* idx can be of type enum memcg_stat_item or node_stat_item */
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001183static inline void __inc_memcg_page_state(struct page *page,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001184 int idx)
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001185{
1186 __mod_memcg_page_state(page, idx, 1);
1187}
1188
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001189/* idx can be of type enum memcg_stat_item or node_stat_item */
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001190static inline void __dec_memcg_page_state(struct page *page,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001191 int idx)
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001192{
1193 __mod_memcg_page_state(page, idx, -1);
1194}
1195
1196static inline void __inc_lruvec_state(struct lruvec *lruvec,
1197 enum node_stat_item idx)
1198{
1199 __mod_lruvec_state(lruvec, idx, 1);
1200}
1201
1202static inline void __dec_lruvec_state(struct lruvec *lruvec,
1203 enum node_stat_item idx)
1204{
1205 __mod_lruvec_state(lruvec, idx, -1);
1206}
1207
1208static inline void __inc_lruvec_page_state(struct page *page,
1209 enum node_stat_item idx)
1210{
1211 __mod_lruvec_page_state(page, idx, 1);
1212}
1213
1214static inline void __dec_lruvec_page_state(struct page *page,
1215 enum node_stat_item idx)
1216{
1217 __mod_lruvec_page_state(page, idx, -1);
1218}
1219
Roman Gushchinec9f0232019-08-13 15:37:41 -07001220static inline void __inc_lruvec_slab_state(void *p, enum node_stat_item idx)
1221{
1222 __mod_lruvec_slab_state(p, idx, 1);
1223}
1224
1225static inline void __dec_lruvec_slab_state(void *p, enum node_stat_item idx)
1226{
1227 __mod_lruvec_slab_state(p, idx, -1);
1228}
1229
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001230/* idx can be of type enum memcg_stat_item or node_stat_item */
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001231static inline void inc_memcg_state(struct mem_cgroup *memcg,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001232 int idx)
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001233{
1234 mod_memcg_state(memcg, idx, 1);
1235}
1236
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001237/* idx can be of type enum memcg_stat_item or node_stat_item */
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001238static inline void dec_memcg_state(struct mem_cgroup *memcg,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001239 int idx)
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001240{
1241 mod_memcg_state(memcg, idx, -1);
1242}
1243
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001244/* idx can be of type enum memcg_stat_item or node_stat_item */
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001245static inline void inc_memcg_page_state(struct page *page,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001246 int idx)
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001247{
1248 mod_memcg_page_state(page, idx, 1);
1249}
1250
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001251/* idx can be of type enum memcg_stat_item or node_stat_item */
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001252static inline void dec_memcg_page_state(struct page *page,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001253 int idx)
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001254{
1255 mod_memcg_page_state(page, idx, -1);
1256}
1257
1258static inline void inc_lruvec_state(struct lruvec *lruvec,
1259 enum node_stat_item idx)
1260{
1261 mod_lruvec_state(lruvec, idx, 1);
1262}
1263
1264static inline void dec_lruvec_state(struct lruvec *lruvec,
1265 enum node_stat_item idx)
1266{
1267 mod_lruvec_state(lruvec, idx, -1);
1268}
1269
1270static inline void inc_lruvec_page_state(struct page *page,
1271 enum node_stat_item idx)
1272{
1273 mod_lruvec_page_state(page, idx, 1);
1274}
1275
1276static inline void dec_lruvec_page_state(struct page *page,
1277 enum node_stat_item idx)
1278{
1279 mod_lruvec_page_state(page, idx, -1);
1280}
1281
Tejun Heo52ebea72015-05-22 17:13:37 -04001282#ifdef CONFIG_CGROUP_WRITEBACK
Tejun Heo841710a2015-05-22 18:23:33 -04001283
Tejun Heo841710a2015-05-22 18:23:33 -04001284struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
Tejun Heoc5edf9c2015-09-29 13:04:26 -04001285void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
1286 unsigned long *pheadroom, unsigned long *pdirty,
1287 unsigned long *pwriteback);
Tejun Heo841710a2015-05-22 18:23:33 -04001288
Tejun Heo97b27822019-08-26 09:06:56 -07001289void mem_cgroup_track_foreign_dirty_slowpath(struct page *page,
1290 struct bdi_writeback *wb);
1291
1292static inline void mem_cgroup_track_foreign_dirty(struct page *page,
1293 struct bdi_writeback *wb)
1294{
Baoquan He08d1d0e2019-10-06 17:58:15 -07001295 if (mem_cgroup_disabled())
1296 return;
1297
Tejun Heo97b27822019-08-26 09:06:56 -07001298 if (unlikely(&page->mem_cgroup->css != wb->memcg_css))
1299 mem_cgroup_track_foreign_dirty_slowpath(page, wb);
1300}
1301
1302void mem_cgroup_flush_foreign(struct bdi_writeback *wb);
1303
Tejun Heo841710a2015-05-22 18:23:33 -04001304#else /* CONFIG_CGROUP_WRITEBACK */
1305
1306static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
1307{
1308 return NULL;
1309}
1310
Tejun Heoc2aa7232015-05-22 18:23:35 -04001311static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
Tejun Heoc5edf9c2015-09-29 13:04:26 -04001312 unsigned long *pfilepages,
1313 unsigned long *pheadroom,
Tejun Heoc2aa7232015-05-22 18:23:35 -04001314 unsigned long *pdirty,
1315 unsigned long *pwriteback)
1316{
1317}
1318
Tejun Heo97b27822019-08-26 09:06:56 -07001319static inline void mem_cgroup_track_foreign_dirty(struct page *page,
1320 struct bdi_writeback *wb)
1321{
1322}
1323
1324static inline void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
1325{
1326}
1327
Tejun Heo841710a2015-05-22 18:23:33 -04001328#endif /* CONFIG_CGROUP_WRITEBACK */
Tejun Heo52ebea72015-05-22 17:13:37 -04001329
Glauber Costae1aab162011-12-11 21:47:03 +00001330struct sock;
Johannes Weinerbaac50b2016-01-14 15:21:17 -08001331bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1332void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
Johannes Weinerd886f4e2016-01-20 15:02:47 -08001333#ifdef CONFIG_MEMCG
Johannes Weineref129472016-01-14 15:21:34 -08001334extern struct static_key_false memcg_sockets_enabled_key;
1335#define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
Johannes Weiner2d758072016-10-07 17:00:58 -07001336void mem_cgroup_sk_alloc(struct sock *sk);
1337void mem_cgroup_sk_free(struct sock *sk);
Johannes Weinerbaac50b2016-01-14 15:21:17 -08001338static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
Johannes Weinere8056052016-01-14 15:21:14 -08001339{
Johannes Weiner0db15292016-01-20 15:02:50 -08001340 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_pressure)
Johannes Weiner8e8ae642016-01-14 15:21:32 -08001341 return true;
Johannes Weiner8e8ae642016-01-14 15:21:32 -08001342 do {
1343 if (time_before(jiffies, memcg->socket_pressure))
1344 return true;
1345 } while ((memcg = parent_mem_cgroup(memcg)));
1346 return false;
Johannes Weinere8056052016-01-14 15:21:14 -08001347}
Yang Shi0a432dc2019-09-23 15:38:12 -07001348
1349extern int memcg_expand_shrinker_maps(int new_id);
1350
1351extern void memcg_set_shrinker_bit(struct mem_cgroup *memcg,
1352 int nid, int shrinker_id);
Johannes Weinere8056052016-01-14 15:21:14 -08001353#else
Johannes Weiner80e95fe2016-01-14 15:21:20 -08001354#define mem_cgroup_sockets_enabled 0
Johannes Weiner2d758072016-10-07 17:00:58 -07001355static inline void mem_cgroup_sk_alloc(struct sock *sk) { };
1356static inline void mem_cgroup_sk_free(struct sock *sk) { };
Johannes Weinerbaac50b2016-01-14 15:21:17 -08001357static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
Johannes Weinere8056052016-01-14 15:21:14 -08001358{
1359 return false;
1360}
Yang Shi0a432dc2019-09-23 15:38:12 -07001361
1362static inline void memcg_set_shrinker_bit(struct mem_cgroup *memcg,
1363 int nid, int shrinker_id)
1364{
1365}
Johannes Weinere8056052016-01-14 15:21:14 -08001366#endif
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08001367
Vladimir Davydov45264772016-07-26 15:24:21 -07001368struct kmem_cache *memcg_kmem_get_cache(struct kmem_cache *cachep);
1369void memcg_kmem_put_cache(struct kmem_cache *cachep);
Roman Gushchin9b6f7e12018-10-26 15:03:19 -07001370
1371#ifdef CONFIG_MEMCG_KMEM
Roman Gushchin4b13f642020-04-01 21:06:56 -07001372int __memcg_kmem_charge(struct mem_cgroup *memcg, gfp_t gfp,
1373 unsigned int nr_pages);
1374void __memcg_kmem_uncharge(struct mem_cgroup *memcg, unsigned int nr_pages);
Roman Gushchinf4b00ea2020-04-01 21:06:46 -07001375int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order);
1376void __memcg_kmem_uncharge_page(struct page *page, int order);
Vladimir Davydov45264772016-07-26 15:24:21 -07001377
Johannes Weineref129472016-01-14 15:21:34 -08001378extern struct static_key_false memcg_kmem_enabled_key;
Tejun Heo17cc4df2017-02-22 15:41:36 -08001379extern struct workqueue_struct *memcg_kmem_cache_wq;
Glauber Costa749c5412012-12-18 14:23:01 -08001380
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08001381extern int memcg_nr_cache_ids;
Michal Hocko64219992015-09-08 15:01:07 -07001382void memcg_get_cache_ids(void);
1383void memcg_put_cache_ids(void);
Glauber Costaebe945c2012-12-18 14:23:10 -08001384
1385/*
1386 * Helper macro to loop through all memcg-specific caches. Callers must still
1387 * check if the cache is valid (it is either valid or NULL).
1388 * the slab_mutex must be held when looping through those caches
1389 */
Glauber Costa749c5412012-12-18 14:23:01 -08001390#define for_each_memcg_cache_index(_idx) \
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08001391 for ((_idx) = 0; (_idx) < memcg_nr_cache_ids; (_idx)++)
Glauber Costa749c5412012-12-18 14:23:01 -08001392
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08001393static inline bool memcg_kmem_enabled(void)
1394{
Johannes Weineref129472016-01-14 15:21:34 -08001395 return static_branch_unlikely(&memcg_kmem_enabled_key);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08001396}
1397
Roman Gushchinf4b00ea2020-04-01 21:06:46 -07001398static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1399 int order)
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08001400{
1401 if (memcg_kmem_enabled())
Roman Gushchinf4b00ea2020-04-01 21:06:46 -07001402 return __memcg_kmem_charge_page(page, gfp, order);
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08001403 return 0;
1404}
1405
Roman Gushchinf4b00ea2020-04-01 21:06:46 -07001406static inline void memcg_kmem_uncharge_page(struct page *page, int order)
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08001407{
1408 if (memcg_kmem_enabled())
Roman Gushchinf4b00ea2020-04-01 21:06:46 -07001409 __memcg_kmem_uncharge_page(page, order);
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08001410}
1411
Roman Gushchin4b13f642020-04-01 21:06:56 -07001412static inline int memcg_kmem_charge(struct mem_cgroup *memcg, gfp_t gfp,
1413 unsigned int nr_pages)
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08001414{
1415 if (memcg_kmem_enabled())
Roman Gushchin4b13f642020-04-01 21:06:56 -07001416 return __memcg_kmem_charge(memcg, gfp, nr_pages);
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08001417 return 0;
1418}
Roman Gushchin49a18ea2019-07-11 20:56:13 -07001419
Roman Gushchin4b13f642020-04-01 21:06:56 -07001420static inline void memcg_kmem_uncharge(struct mem_cgroup *memcg,
1421 unsigned int nr_pages)
Roman Gushchin49a18ea2019-07-11 20:56:13 -07001422{
1423 if (memcg_kmem_enabled())
Roman Gushchin4b13f642020-04-01 21:06:56 -07001424 __memcg_kmem_uncharge(memcg, nr_pages);
Roman Gushchin49a18ea2019-07-11 20:56:13 -07001425}
1426
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08001427/*
Jesper Dangaard Brouer9f706d62016-03-15 14:54:03 -07001428 * helper for accessing a memcg's index. It will be used as an index in the
Michal Hocko33398cf2015-09-08 15:01:02 -07001429 * child cache array in kmem_cache, and also to derive its name. This function
1430 * will return -1 when this is not a kmem-limited memcg.
1431 */
1432static inline int memcg_cache_id(struct mem_cgroup *memcg)
1433{
1434 return memcg ? memcg->kmemcg_id : -1;
1435}
Vladimir Davydov5722d092014-04-07 15:39:24 -07001436
Roman Gushchin8380ce42020-03-28 19:17:25 -07001437struct mem_cgroup *mem_cgroup_from_obj(void *p);
1438
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08001439#else
Roman Gushchin9b6f7e12018-10-26 15:03:19 -07001440
Roman Gushchinf4b00ea2020-04-01 21:06:46 -07001441static inline int memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1442 int order)
Roman Gushchin9b6f7e12018-10-26 15:03:19 -07001443{
1444 return 0;
1445}
1446
Roman Gushchinf4b00ea2020-04-01 21:06:46 -07001447static inline void memcg_kmem_uncharge_page(struct page *page, int order)
Roman Gushchin9b6f7e12018-10-26 15:03:19 -07001448{
1449}
1450
Roman Gushchinf4b00ea2020-04-01 21:06:46 -07001451static inline int __memcg_kmem_charge_page(struct page *page, gfp_t gfp,
1452 int order)
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08001453{
1454 return 0;
1455}
1456
Roman Gushchinf4b00ea2020-04-01 21:06:46 -07001457static inline void __memcg_kmem_uncharge_page(struct page *page, int order)
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08001458{
1459}
1460
Glauber Costa749c5412012-12-18 14:23:01 -08001461#define for_each_memcg_cache_index(_idx) \
1462 for (; NULL; )
1463
Glauber Costab9ce5ef2012-12-18 14:22:46 -08001464static inline bool memcg_kmem_enabled(void)
1465{
1466 return false;
1467}
1468
Glauber Costa2633d7a2012-12-18 14:22:34 -08001469static inline int memcg_cache_id(struct mem_cgroup *memcg)
1470{
1471 return -1;
1472}
1473
Vladimir Davydov05257a12015-02-12 14:59:01 -08001474static inline void memcg_get_cache_ids(void)
1475{
1476}
1477
1478static inline void memcg_put_cache_ids(void)
1479{
1480}
1481
Roman Gushchin8380ce42020-03-28 19:17:25 -07001482static inline struct mem_cgroup *mem_cgroup_from_obj(void *p)
1483{
1484 return NULL;
1485}
1486
Kirill Tkhai84c07d12018-08-17 15:47:25 -07001487#endif /* CONFIG_MEMCG_KMEM */
Johannes Weiner127424c2016-01-20 15:02:32 -08001488
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001489#endif /* _LINUX_MEMCONTROL_H */