blob: 73fe0a700911a4627eca3a06de21d090215a1268 [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 int priority;
62 unsigned int generation;
63};
64
Johannes Weiner71cd3112017-05-03 14:55:13 -070065#ifdef CONFIG_MEMCG
66
67#define MEM_CGROUP_ID_SHIFT 16
68#define MEM_CGROUP_ID_MAX USHRT_MAX
69
70struct mem_cgroup_id {
71 int id;
Kirill Tkhai1c2d4792018-10-26 15:09:28 -070072 refcount_t ref;
Johannes Weiner71cd3112017-05-03 14:55:13 -070073};
74
Michal Hocko33398cf2015-09-08 15:01:02 -070075/*
76 * Per memcg event counter is incremented at every pagein/pageout. With THP,
77 * it will be incremated by the number of pages. This counter is used for
78 * for trigger some periodic events. This is straightforward and better
79 * than using jiffies etc. to handle periodic memcg event.
80 */
81enum mem_cgroup_events_target {
82 MEM_CGROUP_TARGET_THRESH,
83 MEM_CGROUP_TARGET_SOFTLIMIT,
84 MEM_CGROUP_TARGET_NUMAINFO,
85 MEM_CGROUP_NTARGETS,
86};
87
Chris Down871789d2019-05-14 15:46:57 -070088struct memcg_vmstats_percpu {
89 long stat[MEMCG_NR_STAT];
Johannes Weinere27be242018-04-10 16:29:45 -070090 unsigned long events[NR_VM_EVENT_ITEMS];
Michal Hocko33398cf2015-09-08 15:01:02 -070091 unsigned long nr_page_events;
92 unsigned long targets[MEM_CGROUP_NTARGETS];
93};
94
95struct mem_cgroup_reclaim_iter {
96 struct mem_cgroup *position;
97 /* scan generation, increased every round-trip */
98 unsigned int generation;
99};
100
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700101struct lruvec_stat {
102 long count[NR_VM_NODE_STAT_ITEMS];
103};
104
Michal Hocko33398cf2015-09-08 15:01:02 -0700105/*
Kirill Tkhai0a4465d2018-08-17 15:47:37 -0700106 * Bitmap of shrinker::id corresponding to memcg-aware shrinkers,
107 * which have elements charged to this memcg.
108 */
109struct memcg_shrinker_map {
110 struct rcu_head rcu;
111 unsigned long map[0];
112};
113
114/*
Michal Hocko33398cf2015-09-08 15:01:02 -0700115 * per-zone information in memory controller.
116 */
Mel Gormanef8f2322016-07-28 15:46:05 -0700117struct mem_cgroup_per_node {
Michal Hocko33398cf2015-09-08 15:01:02 -0700118 struct lruvec lruvec;
Johannes Weinera983b5e2018-01-31 16:16:45 -0800119
120 struct lruvec_stat __percpu *lruvec_stat_cpu;
121 atomic_long_t lruvec_stat[NR_VM_NODE_STAT_ITEMS];
Johannes Weiner42a30032019-05-14 15:47:12 -0700122 atomic_long_t lruvec_stat_local[NR_VM_NODE_STAT_ITEMS];
Johannes Weinera983b5e2018-01-31 16:16:45 -0800123
Michal Hockob4536f0c82017-01-10 16:58:04 -0800124 unsigned long lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
Michal Hocko33398cf2015-09-08 15:01:02 -0700125
126 struct mem_cgroup_reclaim_iter iter[DEF_PRIORITY + 1];
127
Kirill Tkhai0a4465d2018-08-17 15:47:37 -0700128#ifdef CONFIG_MEMCG_KMEM
129 struct memcg_shrinker_map __rcu *shrinker_map;
130#endif
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;
Andrey Ryabinine3c1ac52018-04-10 16:28:03 -0700135 bool congested; /* memcg has many dirty pages */
136 /* backed by a congested BDI */
137
Michal Hocko33398cf2015-09-08 15:01:02 -0700138 struct mem_cgroup *memcg; /* Back pointer, we cannot */
139 /* use container_of */
140};
141
Michal Hocko33398cf2015-09-08 15:01:02 -0700142struct mem_cgroup_threshold {
143 struct eventfd_ctx *eventfd;
144 unsigned long threshold;
145};
146
147/* For threshold */
148struct mem_cgroup_threshold_ary {
149 /* An array index points to threshold just below or equal to usage. */
150 int current_threshold;
151 /* Size of entries[] */
152 unsigned int size;
153 /* Array of thresholds */
154 struct mem_cgroup_threshold entries[0];
155};
156
157struct mem_cgroup_thresholds {
158 /* Primary thresholds array */
159 struct mem_cgroup_threshold_ary *primary;
160 /*
161 * Spare threshold array.
162 * This is needed to make mem_cgroup_unregister_event() "never fail".
163 * It must be able to store at least primary->size - 1 entries.
164 */
165 struct mem_cgroup_threshold_ary *spare;
166};
167
Johannes Weiner567e9ab2016-01-20 15:02:24 -0800168enum memcg_kmem_state {
169 KMEM_NONE,
170 KMEM_ALLOCATED,
171 KMEM_ONLINE,
172};
173
Aaron Lue81bf972018-06-07 17:09:44 -0700174#if defined(CONFIG_SMP)
175struct memcg_padding {
176 char x[0];
177} ____cacheline_internodealigned_in_smp;
178#define MEMCG_PADDING(name) struct memcg_padding name;
179#else
180#define MEMCG_PADDING(name)
181#endif
182
Michal Hocko33398cf2015-09-08 15:01:02 -0700183/*
184 * The memory controller data structure. The memory controller controls both
185 * page cache and RSS per cgroup. We would eventually like to provide
186 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
187 * to help the administrator determine what knobs to tune.
188 */
189struct mem_cgroup {
190 struct cgroup_subsys_state css;
191
Johannes Weiner73f576c2016-07-20 15:44:57 -0700192 /* Private memcg ID. Used to ID objects that outlive the cgroup */
193 struct mem_cgroup_id id;
194
Michal Hocko33398cf2015-09-08 15:01:02 -0700195 /* Accounted resources */
196 struct page_counter memory;
Vladimir Davydov37e84352016-01-20 15:02:56 -0800197 struct page_counter swap;
Johannes Weiner0db15292016-01-20 15:02:50 -0800198
199 /* Legacy consumer-oriented counters */
Michal Hocko33398cf2015-09-08 15:01:02 -0700200 struct page_counter memsw;
201 struct page_counter kmem;
Johannes Weiner0db15292016-01-20 15:02:50 -0800202 struct page_counter tcpmem;
Michal Hocko33398cf2015-09-08 15:01:02 -0700203
Roman Gushchin23067152018-06-07 17:06:22 -0700204 /* Upper bound of normal memory consumption range */
Michal Hocko33398cf2015-09-08 15:01:02 -0700205 unsigned long high;
206
Johannes Weinerf7e1cb62016-01-14 15:21:29 -0800207 /* Range enforcement for interrupt charges */
208 struct work_struct high_work;
209
Michal Hocko33398cf2015-09-08 15:01:02 -0700210 unsigned long soft_limit;
211
212 /* vmpressure notifications */
213 struct vmpressure vmpressure;
214
Michal Hocko33398cf2015-09-08 15:01:02 -0700215 /*
216 * Should the accounting and control be hierarchical, per subtree?
217 */
218 bool use_hierarchy;
219
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700220 /*
221 * Should the OOM killer kill all belonging tasks, had it kill one?
222 */
223 bool oom_group;
224
Michal Hocko33398cf2015-09-08 15:01:02 -0700225 /* protected by memcg_oom_lock */
226 bool oom_lock;
227 int under_oom;
228
229 int swappiness;
230 /* OOM-Killer disable */
231 int oom_kill_disable;
232
Johannes Weinere27be242018-04-10 16:29:45 -0700233 /* memory.events */
Tejun Heo472912a2015-09-18 18:01:59 -0400234 struct cgroup_file events_file;
235
Tejun Heof3a53a32018-06-07 17:05:35 -0700236 /* handle for "memory.swap.events" */
237 struct cgroup_file swap_events_file;
238
Michal Hocko33398cf2015-09-08 15:01:02 -0700239 /* protect arrays of thresholds */
240 struct mutex thresholds_lock;
241
242 /* thresholds for memory usage. RCU-protected */
243 struct mem_cgroup_thresholds thresholds;
244
245 /* thresholds for mem+swap usage. RCU-protected */
246 struct mem_cgroup_thresholds memsw_thresholds;
247
248 /* For oom notifier event fd */
249 struct list_head oom_notify;
250
251 /*
252 * Should we move charges of a task when a task is moved into this
253 * mem_cgroup ? And what type of charges should we move ?
254 */
255 unsigned long move_charge_at_immigrate;
Aaron Lue81bf972018-06-07 17:09:44 -0700256 /* taken only while moving_account > 0 */
257 spinlock_t move_lock;
258 unsigned long move_lock_flags;
259
260 MEMCG_PADDING(_pad1_);
261
Michal Hocko33398cf2015-09-08 15:01:02 -0700262 /*
263 * set > 0 if pages under this cgroup are moving to other cgroup.
264 */
265 atomic_t moving_account;
Michal Hocko33398cf2015-09-08 15:01:02 -0700266 struct task_struct *move_lock_task;
Johannes Weinera983b5e2018-01-31 16:16:45 -0800267
Johannes Weinere27be242018-04-10 16:29:45 -0700268 /* memory.stat */
Chris Down871789d2019-05-14 15:46:57 -0700269 struct memcg_vmstats_percpu __percpu *vmstats_percpu;
Aaron Lue81bf972018-06-07 17:09:44 -0700270
271 MEMCG_PADDING(_pad2_);
272
Chris Down871789d2019-05-14 15:46:57 -0700273 atomic_long_t vmstats[MEMCG_NR_STAT];
Johannes Weiner42a30032019-05-14 15:47:12 -0700274 atomic_long_t vmstats_local[MEMCG_NR_STAT];
275
Chris Down871789d2019-05-14 15:46:57 -0700276 atomic_long_t vmevents[NR_VM_EVENT_ITEMS];
Johannes Weiner42a30032019-05-14 15:47:12 -0700277 atomic_long_t vmevents_local[NR_VM_EVENT_ITEMS];
278
279 atomic_long_t memory_events[MEMCG_NR_MEMORY_EVENTS];
Michal Hocko33398cf2015-09-08 15:01:02 -0700280
Johannes Weinerd886f4e2016-01-20 15:02:47 -0800281 unsigned long socket_pressure;
282
283 /* Legacy tcp memory accounting */
Johannes Weiner0db15292016-01-20 15:02:50 -0800284 bool tcpmem_active;
285 int tcpmem_pressure;
Johannes Weinerd886f4e2016-01-20 15:02:47 -0800286
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700287#ifdef CONFIG_MEMCG_KMEM
Michal Hocko33398cf2015-09-08 15:01:02 -0700288 /* Index in the kmem_cache->memcg_params.memcg_caches array */
289 int kmemcg_id;
Johannes Weiner567e9ab2016-01-20 15:02:24 -0800290 enum memcg_kmem_state kmem_state;
Tejun Heobc2791f2017-02-22 15:41:21 -0800291 struct list_head kmem_caches;
Michal Hocko33398cf2015-09-08 15:01:02 -0700292#endif
293
294 int last_scanned_node;
295#if MAX_NUMNODES > 1
296 nodemask_t scan_nodes;
297 atomic_t numainfo_events;
298 atomic_t numainfo_updating;
299#endif
300
301#ifdef CONFIG_CGROUP_WRITEBACK
302 struct list_head cgwb_list;
303 struct wb_domain cgwb_domain;
304#endif
305
306 /* List of events which userspace want to receive */
307 struct list_head event_list;
308 spinlock_t event_list_lock;
309
310 struct mem_cgroup_per_node *nodeinfo[0];
311 /* WARNING: nodeinfo must be the last member here */
312};
Johannes Weiner7d828602016-01-14 15:20:56 -0800313
Johannes Weinera983b5e2018-01-31 16:16:45 -0800314/*
315 * size of first charge trial. "32" comes from vmscan.c's magic value.
316 * TODO: maybe necessary to use big numbers in big irons.
317 */
318#define MEMCG_CHARGE_BATCH 32U
319
Johannes Weiner7d828602016-01-14 15:20:56 -0800320extern struct mem_cgroup *root_mem_cgroup;
Tejun Heo56161632015-05-22 17:13:20 -0400321
Kirill Tkhaidfd2f102018-08-17 15:48:06 -0700322static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
323{
324 return (memcg == root_mem_cgroup);
325}
326
Johannes Weiner23047a92016-03-15 14:57:16 -0700327static inline bool mem_cgroup_disabled(void)
328{
329 return !cgroup_subsys_enabled(memory_cgrp_subsys);
330}
331
Roman Gushchinbf8d5d52018-06-07 17:07:46 -0700332enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root,
333 struct mem_cgroup *memcg);
Johannes Weiner241994ed2015-02-11 15:26:06 -0800334
Johannes Weiner00501b52014-08-08 14:19:20 -0700335int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800336 gfp_t gfp_mask, struct mem_cgroup **memcgp,
337 bool compound);
Tejun Heo2cf85582018-07-03 11:14:56 -0400338int mem_cgroup_try_charge_delay(struct page *page, struct mm_struct *mm,
339 gfp_t gfp_mask, struct mem_cgroup **memcgp,
340 bool compound);
Johannes Weiner00501b52014-08-08 14:19:20 -0700341void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800342 bool lrucare, bool compound);
343void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg,
344 bool compound);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700345void mem_cgroup_uncharge(struct page *page);
Johannes Weiner747db952014-08-08 14:19:24 -0700346void mem_cgroup_uncharge_list(struct list_head *page_list);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700347
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700348void mem_cgroup_migrate(struct page *oldpage, struct page *newpage);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700349
Mel Gormanef8f2322016-07-28 15:46:05 -0700350static struct mem_cgroup_per_node *
351mem_cgroup_nodeinfo(struct mem_cgroup *memcg, int nid)
Johannes Weiner55779ec2016-07-28 15:45:10 -0700352{
Mel Gormanef8f2322016-07-28 15:46:05 -0700353 return memcg->nodeinfo[nid];
Johannes Weiner55779ec2016-07-28 15:45:10 -0700354}
355
356/**
Mel Gormana9dd0a82016-07-28 15:46:02 -0700357 * mem_cgroup_lruvec - get the lru list vector for a node or a memcg zone
358 * @node: node of the wanted lruvec
Johannes Weiner55779ec2016-07-28 15:45:10 -0700359 * @memcg: memcg of the wanted lruvec
360 *
Mel Gormana9dd0a82016-07-28 15:46:02 -0700361 * Returns the lru list vector holding pages for a given @node or a given
362 * @memcg and @zone. This can be the node lruvec, if the memory controller
Johannes Weiner55779ec2016-07-28 15:45:10 -0700363 * is disabled.
364 */
Mel Gormana9dd0a82016-07-28 15:46:02 -0700365static inline struct lruvec *mem_cgroup_lruvec(struct pglist_data *pgdat,
Mel Gormanef8f2322016-07-28 15:46:05 -0700366 struct mem_cgroup *memcg)
Johannes Weiner55779ec2016-07-28 15:45:10 -0700367{
Mel Gormanef8f2322016-07-28 15:46:05 -0700368 struct mem_cgroup_per_node *mz;
Johannes Weiner55779ec2016-07-28 15:45:10 -0700369 struct lruvec *lruvec;
370
371 if (mem_cgroup_disabled()) {
Mel Gormana9dd0a82016-07-28 15:46:02 -0700372 lruvec = node_lruvec(pgdat);
Johannes Weiner55779ec2016-07-28 15:45:10 -0700373 goto out;
374 }
375
Mel Gormanef8f2322016-07-28 15:46:05 -0700376 mz = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
Johannes Weiner55779ec2016-07-28 15:45:10 -0700377 lruvec = &mz->lruvec;
378out:
379 /*
380 * Since a node can be onlined after the mem_cgroup was created,
Mel Gorman599d0c92016-07-28 15:45:31 -0700381 * we have to be prepared to initialize lruvec->pgdat here;
Johannes Weiner55779ec2016-07-28 15:45:10 -0700382 * and if offlined then reonlined, we need to reinitialize it.
383 */
Mel Gormanef8f2322016-07-28 15:46:05 -0700384 if (unlikely(lruvec->pgdat != pgdat))
385 lruvec->pgdat = pgdat;
Johannes Weiner55779ec2016-07-28 15:45:10 -0700386 return lruvec;
387}
388
Mel Gorman599d0c92016-07-28 15:45:31 -0700389struct lruvec *mem_cgroup_page_lruvec(struct page *, struct pglist_data *);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -0800390
Johannes Weiner2314b422014-12-10 15:44:33 -0800391bool task_in_mem_cgroup(struct task_struct *task, struct mem_cgroup *memcg);
Michal Hocko64219992015-09-08 15:01:07 -0700392struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
Vladimir Davydove993d902015-09-09 15:35:35 -0700393
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700394struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm);
395
Shakeel Buttf745c6f2018-08-17 15:46:44 -0700396struct mem_cgroup *get_mem_cgroup_from_page(struct page *page);
397
Michal Hocko33398cf2015-09-08 15:01:02 -0700398static inline
399struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
400 return css ? container_of(css, struct mem_cgroup, css) : NULL;
401}
402
Roman Gushchindc0b5862018-08-17 15:46:36 -0700403static inline void mem_cgroup_put(struct mem_cgroup *memcg)
404{
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700405 if (memcg)
406 css_put(&memcg->css);
Roman Gushchindc0b5862018-08-17 15:46:36 -0700407}
408
Johannes Weiner8e8ae642016-01-14 15:21:32 -0800409#define mem_cgroup_from_counter(counter, member) \
410 container_of(counter, struct mem_cgroup, member)
411
Michal Hocko33398cf2015-09-08 15:01:02 -0700412struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
413 struct mem_cgroup *,
414 struct mem_cgroup_reclaim_cookie *);
415void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700416int mem_cgroup_scan_tasks(struct mem_cgroup *,
417 int (*)(struct task_struct *, void *), void *);
Michal Hocko33398cf2015-09-08 15:01:02 -0700418
Johannes Weiner23047a92016-03-15 14:57:16 -0700419static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
420{
421 if (mem_cgroup_disabled())
422 return 0;
423
Johannes Weiner73f576c2016-07-20 15:44:57 -0700424 return memcg->id.id;
Johannes Weiner23047a92016-03-15 14:57:16 -0700425}
Johannes Weiner73f576c2016-07-20 15:44:57 -0700426struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
Johannes Weiner23047a92016-03-15 14:57:16 -0700427
Chris Downaa9694b2019-03-05 15:45:52 -0800428static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
429{
430 return mem_cgroup_from_css(seq_css(m));
431}
432
Roman Gushchin22621852017-07-06 15:40:25 -0700433static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
434{
435 struct mem_cgroup_per_node *mz;
436
437 if (mem_cgroup_disabled())
438 return NULL;
439
440 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
441 return mz->memcg;
442}
443
Johannes Weiner8e8ae642016-01-14 15:21:32 -0800444/**
445 * parent_mem_cgroup - find the accounting parent of a memcg
446 * @memcg: memcg whose parent to find
447 *
448 * Returns the parent memcg, or NULL if this is the root or the memory
449 * controller is in legacy no-hierarchy mode.
450 */
451static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
452{
453 if (!memcg->memory.parent)
454 return NULL;
455 return mem_cgroup_from_counter(memcg->memory.parent, memory);
456}
457
Michal Hocko33398cf2015-09-08 15:01:02 -0700458static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
459 struct mem_cgroup *root)
460{
461 if (root == memcg)
462 return true;
463 if (!root->use_hierarchy)
464 return false;
465 return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
466}
Glauber Costae1aab162011-12-11 21:47:03 +0000467
Johannes Weiner2314b422014-12-10 15:44:33 -0800468static inline bool mm_match_cgroup(struct mm_struct *mm,
469 struct mem_cgroup *memcg)
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800470{
Johannes Weiner587af302012-10-08 16:34:12 -0700471 struct mem_cgroup *task_memcg;
Johannes Weiner413918b2014-12-10 15:44:30 -0800472 bool match = false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -0700473
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800474 rcu_read_lock();
Johannes Weiner587af302012-10-08 16:34:12 -0700475 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
Johannes Weiner413918b2014-12-10 15:44:30 -0800476 if (task_memcg)
Johannes Weiner2314b422014-12-10 15:44:33 -0800477 match = mem_cgroup_is_descendant(task_memcg, memcg);
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800478 rcu_read_unlock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -0700479 return match;
Lai Jiangshan2e4d4092009-01-07 18:08:07 -0800480}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800481
Michal Hocko64219992015-09-08 15:01:07 -0700482struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page);
Vladimir Davydov2fc04522015-09-09 15:35:28 -0700483ino_t page_cgroup_ino(struct page *page);
Wu Fengguangd3242362009-12-16 12:19:59 +0100484
Vladimir Davydoveb01aaa2016-01-20 15:03:02 -0800485static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
486{
487 if (mem_cgroup_disabled())
488 return true;
489 return !!(memcg->css.flags & CSS_ONLINE);
490}
491
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800492/*
493 * For memory reclaim.
494 */
Ying Han889976d2011-05-26 16:25:33 -0700495int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
Michal Hocko33398cf2015-09-08 15:01:02 -0700496
497void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
Michal Hockob4536f0c82017-01-10 16:58:04 -0800498 int zid, int nr_pages);
Michal Hocko33398cf2015-09-08 15:01:02 -0700499
Michal Hocko33398cf2015-09-08 15:01:02 -0700500static inline
Michal Hockob4536f0c82017-01-10 16:58:04 -0800501unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
502 enum lru_list lru, int zone_idx)
503{
504 struct mem_cgroup_per_node *mz;
505
506 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
507 return mz->lru_zone_size[zone_idx][lru];
Michal Hocko33398cf2015-09-08 15:01:02 -0700508}
509
Tejun Heob23afb92015-11-05 18:46:11 -0800510void mem_cgroup_handle_over_high(void);
511
Roman Gushchinbbec2e12018-06-07 17:06:18 -0700512unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700513
yuzhoujianf0c867d2018-12-28 00:36:10 -0800514void mem_cgroup_print_oom_context(struct mem_cgroup *memcg,
Michal Hocko64219992015-09-08 15:01:07 -0700515 struct task_struct *p);
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800516
yuzhoujianf0c867d2018-12-28 00:36:10 -0800517void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg);
518
Michal Hocko29ef6802018-08-17 15:47:11 -0700519static inline void mem_cgroup_enter_user_fault(void)
Johannes Weiner519e5242013-09-12 15:13:42 -0700520{
Michal Hocko29ef6802018-08-17 15:47:11 -0700521 WARN_ON(current->in_user_fault);
522 current->in_user_fault = 1;
Johannes Weiner519e5242013-09-12 15:13:42 -0700523}
524
Michal Hocko29ef6802018-08-17 15:47:11 -0700525static inline void mem_cgroup_exit_user_fault(void)
Johannes Weiner519e5242013-09-12 15:13:42 -0700526{
Michal Hocko29ef6802018-08-17 15:47:11 -0700527 WARN_ON(!current->in_user_fault);
528 current->in_user_fault = 0;
Johannes Weiner519e5242013-09-12 15:13:42 -0700529}
530
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700531static inline bool task_in_memcg_oom(struct task_struct *p)
532{
Tejun Heo626ebc42015-11-05 18:46:09 -0800533 return p->memcg_in_oom;
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700534}
535
Johannes Weiner49426422013-10-16 13:46:59 -0700536bool mem_cgroup_oom_synchronize(bool wait);
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700537struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
538 struct mem_cgroup *oom_domain);
539void mem_cgroup_print_oom_group(struct mem_cgroup *memcg);
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700540
Andrew Mortonc255a452012-07-31 16:43:02 -0700541#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -0800542extern int do_swap_account;
543#endif
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800544
Johannes Weiner739f79f2017-08-18 15:15:48 -0700545struct mem_cgroup *lock_page_memcg(struct page *page);
546void __unlock_page_memcg(struct mem_cgroup *memcg);
Johannes Weiner62cccb82016-03-15 14:57:22 -0700547void unlock_page_memcg(struct page *page);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700548
Greg Thelen0b3d6e62019-04-05 18:39:18 -0700549/*
550 * idx can be of type enum memcg_stat_item or node_stat_item.
551 * Keep in sync with memcg_exact_page_state().
552 */
Johannes Weiner42a30032019-05-14 15:47:12 -0700553static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
554{
555 long x = atomic_long_read(&memcg->vmstats[idx]);
556#ifdef CONFIG_SMP
557 if (x < 0)
558 x = 0;
559#endif
560 return x;
561}
562
563/*
564 * idx can be of type enum memcg_stat_item or node_stat_item.
565 * Keep in sync with memcg_exact_page_state().
566 */
Johannes Weiner205b20c2019-05-14 15:47:06 -0700567static inline unsigned long memcg_page_state_local(struct mem_cgroup *memcg,
568 int idx)
Johannes Weiner2a2e4882017-05-03 14:55:03 -0700569{
Johannes Weiner42a30032019-05-14 15:47:12 -0700570 long x = atomic_long_read(&memcg->vmstats_local[idx]);
Johannes Weinera983b5e2018-01-31 16:16:45 -0800571#ifdef CONFIG_SMP
572 if (x < 0)
573 x = 0;
574#endif
575 return x;
Johannes Weiner2a2e4882017-05-03 14:55:03 -0700576}
577
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700578void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val);
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700579
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -0700580/* idx can be of type enum memcg_stat_item or node_stat_item */
Johannes Weinerccda7f42017-05-03 14:55:16 -0700581static inline void mod_memcg_state(struct mem_cgroup *memcg,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -0700582 int idx, int val)
Johannes Weiner2a2e4882017-05-03 14:55:03 -0700583{
Johannes Weinerc3cc3912018-02-21 14:45:24 -0800584 unsigned long flags;
585
586 local_irq_save(flags);
Johannes Weinera983b5e2018-01-31 16:16:45 -0800587 __mod_memcg_state(memcg, idx, val);
Johannes Weinerc3cc3912018-02-21 14:45:24 -0800588 local_irq_restore(flags);
Johannes Weiner2a2e4882017-05-03 14:55:03 -0700589}
590
Michal Hocko33398cf2015-09-08 15:01:02 -0700591/**
Johannes Weinerccda7f42017-05-03 14:55:16 -0700592 * mod_memcg_page_state - update page state statistics
Johannes Weiner62cccb82016-03-15 14:57:22 -0700593 * @page: the page
Michal Hocko33398cf2015-09-08 15:01:02 -0700594 * @idx: page state item to account
595 * @val: number of pages (positive or negative)
596 *
Johannes Weinerfdf1cdb2016-03-15 14:57:25 -0700597 * The @page must be locked or the caller must use lock_page_memcg()
598 * to prevent double accounting when the page is concurrently being
599 * moved to another memcg:
Johannes Weiner81f8c3a2016-03-15 14:57:04 -0700600 *
Johannes Weinerfdf1cdb2016-03-15 14:57:25 -0700601 * lock_page(page) or lock_page_memcg(page)
Johannes Weiner81f8c3a2016-03-15 14:57:04 -0700602 * if (TestClearPageState(page))
Johannes Weinerccda7f42017-05-03 14:55:16 -0700603 * mod_memcg_page_state(page, state, -1);
Johannes Weinerfdf1cdb2016-03-15 14:57:25 -0700604 * unlock_page(page) or unlock_page_memcg(page)
Johannes Weiner2a2e4882017-05-03 14:55:03 -0700605 *
606 * Kernel pages are an exception to this, since they'll never move.
Michal Hocko33398cf2015-09-08 15:01:02 -0700607 */
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700608static inline void __mod_memcg_page_state(struct page *page,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -0700609 int idx, int val)
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700610{
611 if (page->mem_cgroup)
612 __mod_memcg_state(page->mem_cgroup, idx, val);
613}
614
Johannes Weinerccda7f42017-05-03 14:55:16 -0700615static inline void mod_memcg_page_state(struct page *page,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -0700616 int idx, int val)
Michal Hocko33398cf2015-09-08 15:01:02 -0700617{
Johannes Weiner62cccb82016-03-15 14:57:22 -0700618 if (page->mem_cgroup)
Johannes Weinerccda7f42017-05-03 14:55:16 -0700619 mod_memcg_state(page->mem_cgroup, idx, val);
Michal Hocko33398cf2015-09-08 15:01:02 -0700620}
621
Johannes Weiner42a30032019-05-14 15:47:12 -0700622static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
623 enum node_stat_item idx)
624{
625 struct mem_cgroup_per_node *pn;
626 long x;
627
628 if (mem_cgroup_disabled())
629 return node_page_state(lruvec_pgdat(lruvec), idx);
630
631 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
632 x = atomic_long_read(&pn->lruvec_stat[idx]);
633#ifdef CONFIG_SMP
634 if (x < 0)
635 x = 0;
636#endif
637 return x;
638}
639
Johannes Weiner205b20c2019-05-14 15:47:06 -0700640static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
641 enum node_stat_item idx)
Greg Thelen2a7106f2011-01-13 15:47:37 -0800642{
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700643 struct mem_cgroup_per_node *pn;
Johannes Weinera983b5e2018-01-31 16:16:45 -0800644 long x;
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700645
646 if (mem_cgroup_disabled())
647 return node_page_state(lruvec_pgdat(lruvec), idx);
648
649 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
Johannes Weiner42a30032019-05-14 15:47:12 -0700650 x = atomic_long_read(&pn->lruvec_stat_local[idx]);
Johannes Weinera983b5e2018-01-31 16:16:45 -0800651#ifdef CONFIG_SMP
652 if (x < 0)
653 x = 0;
654#endif
655 return x;
Greg Thelen2a7106f2011-01-13 15:47:37 -0800656}
657
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700658void __mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
659 int val);
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700660
661static inline void mod_lruvec_state(struct lruvec *lruvec,
662 enum node_stat_item idx, int val)
663{
Johannes Weinerc3cc3912018-02-21 14:45:24 -0800664 unsigned long flags;
665
666 local_irq_save(flags);
Johannes Weiner28454262018-01-31 16:16:41 -0800667 __mod_lruvec_state(lruvec, idx, val);
Johannes Weinerc3cc3912018-02-21 14:45:24 -0800668 local_irq_restore(flags);
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700669}
670
671static inline void __mod_lruvec_page_state(struct page *page,
672 enum node_stat_item idx, int val)
673{
Johannes Weiner28454262018-01-31 16:16:41 -0800674 pg_data_t *pgdat = page_pgdat(page);
675 struct lruvec *lruvec;
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700676
Johannes Weiner28454262018-01-31 16:16:41 -0800677 /* Untracked pages have no memcg, no lruvec. Update only the node */
678 if (!page->mem_cgroup) {
679 __mod_node_page_state(pgdat, idx, val);
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700680 return;
Johannes Weiner28454262018-01-31 16:16:41 -0800681 }
682
683 lruvec = mem_cgroup_lruvec(pgdat, page->mem_cgroup);
684 __mod_lruvec_state(lruvec, idx, val);
Johannes Weiner00f3ca22017-07-06 15:40:52 -0700685}
686
687static inline void mod_lruvec_page_state(struct page *page,
688 enum node_stat_item idx, int val)
689{
Johannes Weinerc3cc3912018-02-21 14:45:24 -0800690 unsigned long flags;
691
692 local_irq_save(flags);
Johannes Weiner28454262018-01-31 16:16:41 -0800693 __mod_lruvec_page_state(page, idx, val);
Johannes Weinerc3cc3912018-02-21 14:45:24 -0800694 local_irq_restore(flags);
Greg Thelen2a7106f2011-01-13 15:47:37 -0800695}
696
Mel Gormanef8f2322016-07-28 15:46:05 -0700697unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
Andrew Morton0608f432013-09-24 15:27:41 -0700698 gfp_t gfp_mask,
699 unsigned long *total_scanned);
David Rientjesa63d83f2010-08-09 17:19:46 -0700700
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700701void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
702 unsigned long count);
Johannes Weinerc9019e92018-01-31 16:16:37 -0800703
Roman Gushchin22621852017-07-06 15:40:25 -0700704static inline void count_memcg_events(struct mem_cgroup *memcg,
Johannes Weinere27be242018-04-10 16:29:45 -0700705 enum vm_event_item idx,
706 unsigned long count)
Roman Gushchin22621852017-07-06 15:40:25 -0700707{
Johannes Weinerc3cc3912018-02-21 14:45:24 -0800708 unsigned long flags;
709
710 local_irq_save(flags);
Johannes Weinera983b5e2018-01-31 16:16:45 -0800711 __count_memcg_events(memcg, idx, count);
Johannes Weinerc3cc3912018-02-21 14:45:24 -0800712 local_irq_restore(flags);
Roman Gushchin22621852017-07-06 15:40:25 -0700713}
714
715static inline void count_memcg_page_event(struct page *page,
Johannes Weinere27be242018-04-10 16:29:45 -0700716 enum vm_event_item idx)
Roman Gushchin22621852017-07-06 15:40:25 -0700717{
718 if (page->mem_cgroup)
719 count_memcg_events(page->mem_cgroup, idx, 1);
720}
721
722static inline void count_memcg_event_mm(struct mm_struct *mm,
723 enum vm_event_item idx)
David Rientjes68ae5642012-12-12 13:51:57 -0800724{
Michal Hocko33398cf2015-09-08 15:01:02 -0700725 struct mem_cgroup *memcg;
726
David Rientjes68ae5642012-12-12 13:51:57 -0800727 if (mem_cgroup_disabled())
728 return;
Michal Hocko33398cf2015-09-08 15:01:02 -0700729
730 rcu_read_lock();
731 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
Roman Gushchinfe6bdfc2018-06-14 15:28:05 -0700732 if (likely(memcg))
Johannes Weinerc9019e92018-01-31 16:16:37 -0800733 count_memcg_events(memcg, idx, 1);
Michal Hocko33398cf2015-09-08 15:01:02 -0700734 rcu_read_unlock();
David Rientjes68ae5642012-12-12 13:51:57 -0800735}
Johannes Weinerc9019e92018-01-31 16:16:37 -0800736
Johannes Weinere27be242018-04-10 16:29:45 -0700737static inline void memcg_memory_event(struct mem_cgroup *memcg,
738 enum memcg_memory_event event)
Johannes Weinerc9019e92018-01-31 16:16:37 -0800739{
Johannes Weinere27be242018-04-10 16:29:45 -0700740 atomic_long_inc(&memcg->memory_events[event]);
Johannes Weinerc9019e92018-01-31 16:16:37 -0800741 cgroup_file_notify(&memcg->events_file);
742}
743
Roman Gushchinfe6bdfc2018-06-14 15:28:05 -0700744static inline void memcg_memory_event_mm(struct mm_struct *mm,
745 enum memcg_memory_event event)
746{
747 struct mem_cgroup *memcg;
748
749 if (mem_cgroup_disabled())
750 return;
751
752 rcu_read_lock();
753 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
754 if (likely(memcg))
755 memcg_memory_event(memcg, event);
756 rcu_read_unlock();
757}
758
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800759#ifdef CONFIG_TRANSPARENT_HUGEPAGE
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -0800760void mem_cgroup_split_huge_fixup(struct page *head);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -0800761#endif
762
Andrew Mortonc255a452012-07-31 16:43:02 -0700763#else /* CONFIG_MEMCG */
Johannes Weiner23047a92016-03-15 14:57:16 -0700764
765#define MEM_CGROUP_ID_SHIFT 0
766#define MEM_CGROUP_ID_MAX 0
767
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800768struct mem_cgroup;
769
Kirill Tkhaidfd2f102018-08-17 15:48:06 -0700770static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
771{
772 return true;
773}
774
Johannes Weiner23047a92016-03-15 14:57:16 -0700775static inline bool mem_cgroup_disabled(void)
776{
777 return true;
778}
779
Johannes Weinere27be242018-04-10 16:29:45 -0700780static inline void memcg_memory_event(struct mem_cgroup *memcg,
781 enum memcg_memory_event event)
Johannes Weiner241994ed2015-02-11 15:26:06 -0800782{
783}
784
Roman Gushchinfe6bdfc2018-06-14 15:28:05 -0700785static inline void memcg_memory_event_mm(struct mm_struct *mm,
786 enum memcg_memory_event event)
787{
788}
789
Roman Gushchinbf8d5d52018-06-07 17:07:46 -0700790static inline enum mem_cgroup_protection mem_cgroup_protected(
791 struct mem_cgroup *root, struct mem_cgroup *memcg)
Johannes Weiner241994ed2015-02-11 15:26:06 -0800792{
Roman Gushchinbf8d5d52018-06-07 17:07:46 -0700793 return MEMCG_PROT_NONE;
Johannes Weiner241994ed2015-02-11 15:26:06 -0800794}
795
Johannes Weiner00501b52014-08-08 14:19:20 -0700796static inline int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
797 gfp_t gfp_mask,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800798 struct mem_cgroup **memcgp,
799 bool compound)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800800{
Johannes Weiner00501b52014-08-08 14:19:20 -0700801 *memcgp = NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800802 return 0;
803}
804
Tejun Heo2cf85582018-07-03 11:14:56 -0400805static inline int mem_cgroup_try_charge_delay(struct page *page,
806 struct mm_struct *mm,
807 gfp_t gfp_mask,
808 struct mem_cgroup **memcgp,
809 bool compound)
810{
811 *memcgp = NULL;
812 return 0;
813}
814
Johannes Weiner00501b52014-08-08 14:19:20 -0700815static inline void mem_cgroup_commit_charge(struct page *page,
816 struct mem_cgroup *memcg,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800817 bool lrucare, bool compound)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800818{
819}
820
Johannes Weiner00501b52014-08-08 14:19:20 -0700821static inline void mem_cgroup_cancel_charge(struct page *page,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800822 struct mem_cgroup *memcg,
823 bool compound)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800824{
825}
826
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700827static inline void mem_cgroup_uncharge(struct page *page)
828{
829}
830
Johannes Weiner747db952014-08-08 14:19:24 -0700831static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -0800832{
833}
834
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700835static inline void mem_cgroup_migrate(struct page *old, struct page *new)
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700836{
837}
838
Mel Gormana9dd0a82016-07-28 15:46:02 -0700839static inline struct lruvec *mem_cgroup_lruvec(struct pglist_data *pgdat,
Mel Gormanef8f2322016-07-28 15:46:05 -0700840 struct mem_cgroup *memcg)
Johannes Weiner925b7672012-01-12 17:18:15 -0800841{
Mel Gormana9dd0a82016-07-28 15:46:02 -0700842 return node_lruvec(pgdat);
Johannes Weiner925b7672012-01-12 17:18:15 -0800843}
844
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700845static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
Mel Gorman599d0c92016-07-28 15:45:31 -0700846 struct pglist_data *pgdat)
Minchan Kim3f58a822011-03-22 16:32:53 -0700847{
Mel Gorman599d0c92016-07-28 15:45:31 -0700848 return &pgdat->lruvec;
Balbir Singh66e17072008-02-07 00:13:56 -0800849}
850
Johannes Weiner587af302012-10-08 16:34:12 -0700851static inline bool mm_match_cgroup(struct mm_struct *mm,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700852 struct mem_cgroup *memcg)
Balbir Singhbed71612008-02-07 00:14:01 -0800853{
Johannes Weiner587af302012-10-08 16:34:12 -0700854 return true;
Balbir Singhbed71612008-02-07 00:14:01 -0800855}
856
David Rientjesffbdccf2013-07-03 15:01:23 -0700857static inline bool task_in_mem_cgroup(struct task_struct *task,
858 const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -0800859{
David Rientjesffbdccf2013-07-03 15:01:23 -0700860 return true;
David Rientjes4c4a2212008-02-07 00:14:06 -0800861}
862
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700863static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
864{
865 return NULL;
866}
867
Shakeel Buttf745c6f2018-08-17 15:46:44 -0700868static inline struct mem_cgroup *get_mem_cgroup_from_page(struct page *page)
869{
870 return NULL;
871}
872
Roman Gushchindc0b5862018-08-17 15:46:36 -0700873static inline void mem_cgroup_put(struct mem_cgroup *memcg)
874{
875}
876
Johannes Weiner56600482012-01-12 17:17:59 -0800877static inline struct mem_cgroup *
878mem_cgroup_iter(struct mem_cgroup *root,
879 struct mem_cgroup *prev,
880 struct mem_cgroup_reclaim_cookie *reclaim)
881{
882 return NULL;
883}
884
885static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
886 struct mem_cgroup *prev)
887{
888}
889
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700890static inline int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
891 int (*fn)(struct task_struct *, void *), void *arg)
892{
893 return 0;
894}
895
Johannes Weiner23047a92016-03-15 14:57:16 -0700896static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800897{
Johannes Weiner23047a92016-03-15 14:57:16 -0700898 return 0;
899}
900
901static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
902{
903 WARN_ON_ONCE(id);
904 /* XXX: This should always return root_mem_cgroup */
905 return NULL;
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800906}
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800907
Chris Downaa9694b2019-03-05 15:45:52 -0800908static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
909{
910 return NULL;
911}
912
Roman Gushchin22621852017-07-06 15:40:25 -0700913static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
914{
915 return NULL;
916}
917
Vladimir Davydoveb01aaa2016-01-20 15:03:02 -0800918static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800919{
Yaowei Bai13308ca2015-11-05 18:47:40 -0800920 return true;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800921}
922
Michal Hockob4536f0c82017-01-10 16:58:04 -0800923static inline
924unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
925 enum lru_list lru, int zone_idx)
926{
927 return 0;
928}
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800929
Roman Gushchinbbec2e12018-06-07 17:06:18 -0700930static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -0700931{
932 return 0;
933}
934
Balbir Singhe2224322009-04-02 16:57:39 -0700935static inline void
yuzhoujianf0c867d2018-12-28 00:36:10 -0800936mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
937{
938}
939
940static inline void
941mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
Balbir Singhe2224322009-04-02 16:57:39 -0700942{
943}
944
Johannes Weiner739f79f2017-08-18 15:15:48 -0700945static inline struct mem_cgroup *lock_page_memcg(struct page *page)
946{
947 return NULL;
948}
949
950static inline void __unlock_page_memcg(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700951{
952}
953
Johannes Weiner62cccb82016-03-15 14:57:22 -0700954static inline void unlock_page_memcg(struct page *page)
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -0700955{
956}
957
Tejun Heob23afb92015-11-05 18:46:11 -0800958static inline void mem_cgroup_handle_over_high(void)
959{
960}
961
Michal Hocko29ef6802018-08-17 15:47:11 -0700962static inline void mem_cgroup_enter_user_fault(void)
Johannes Weiner519e5242013-09-12 15:13:42 -0700963{
964}
965
Michal Hocko29ef6802018-08-17 15:47:11 -0700966static inline void mem_cgroup_exit_user_fault(void)
Johannes Weiner519e5242013-09-12 15:13:42 -0700967{
968}
969
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700970static inline bool task_in_memcg_oom(struct task_struct *p)
971{
972 return false;
973}
974
Johannes Weiner49426422013-10-16 13:46:59 -0700975static inline bool mem_cgroup_oom_synchronize(bool wait)
Johannes Weiner3812c8c2013-09-12 15:13:44 -0700976{
977 return false;
978}
979
Roman Gushchin3d8b38e2018-08-21 21:53:54 -0700980static inline struct mem_cgroup *mem_cgroup_get_oom_group(
981 struct task_struct *victim, struct mem_cgroup *oom_domain)
982{
983 return NULL;
984}
985
986static inline void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
987{
988}
989
Johannes Weiner42a30032019-05-14 15:47:12 -0700990static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
991{
992 return 0;
993}
994
Johannes Weiner205b20c2019-05-14 15:47:06 -0700995static inline unsigned long memcg_page_state_local(struct mem_cgroup *memcg,
996 int idx)
Johannes Weiner2a2e4882017-05-03 14:55:03 -0700997{
998 return 0;
999}
1000
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001001static inline void __mod_memcg_state(struct mem_cgroup *memcg,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001002 int idx,
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001003 int nr)
1004{
1005}
1006
Johannes Weinerccda7f42017-05-03 14:55:16 -07001007static inline void mod_memcg_state(struct mem_cgroup *memcg,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001008 int idx,
Johannes Weinerccda7f42017-05-03 14:55:16 -07001009 int nr)
Johannes Weiner2a2e4882017-05-03 14:55:03 -07001010{
1011}
1012
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001013static inline void __mod_memcg_page_state(struct page *page,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001014 int idx,
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001015 int nr)
Johannes Weiner2a2e4882017-05-03 14:55:03 -07001016{
1017}
1018
Johannes Weinerccda7f42017-05-03 14:55:16 -07001019static inline void mod_memcg_page_state(struct page *page,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001020 int idx,
Johannes Weinerccda7f42017-05-03 14:55:16 -07001021 int nr)
Johannes Weiner553af432017-03-31 15:11:50 -07001022{
1023}
1024
Johannes Weiner42a30032019-05-14 15:47:12 -07001025static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
1026 enum node_stat_item idx)
1027{
1028 return node_page_state(lruvec_pgdat(lruvec), idx);
1029}
1030
Johannes Weiner205b20c2019-05-14 15:47:06 -07001031static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1032 enum node_stat_item idx)
Greg Thelen2a7106f2011-01-13 15:47:37 -08001033{
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001034 return node_page_state(lruvec_pgdat(lruvec), idx);
Greg Thelen2a7106f2011-01-13 15:47:37 -08001035}
1036
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001037static inline void __mod_lruvec_state(struct lruvec *lruvec,
1038 enum node_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001039{
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001040 __mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
1041}
1042
1043static inline void mod_lruvec_state(struct lruvec *lruvec,
1044 enum node_stat_item idx, int val)
1045{
1046 mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
1047}
1048
1049static inline void __mod_lruvec_page_state(struct page *page,
1050 enum node_stat_item idx, int val)
1051{
1052 __mod_node_page_state(page_pgdat(page), idx, val);
1053}
1054
1055static inline void mod_lruvec_page_state(struct page *page,
1056 enum node_stat_item idx, int val)
1057{
1058 mod_node_page_state(page_pgdat(page), idx, val);
Balbir Singhd69b0422009-06-17 16:26:34 -07001059}
1060
Balbir Singh4e416952009-09-23 15:56:39 -07001061static inline
Mel Gormanef8f2322016-07-28 15:46:05 -07001062unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
Andrew Morton0608f432013-09-24 15:27:41 -07001063 gfp_t gfp_mask,
1064 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07001065{
Andrew Morton0608f432013-09-24 15:27:41 -07001066 return 0;
Balbir Singh4e416952009-09-23 15:56:39 -07001067}
1068
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08001069static inline void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08001070{
1071}
1072
Roman Gushchin22621852017-07-06 15:40:25 -07001073static inline void count_memcg_events(struct mem_cgroup *memcg,
1074 enum vm_event_item idx,
1075 unsigned long count)
1076{
1077}
1078
Kirill Tkhai9851ac12019-05-13 17:16:54 -07001079static inline void __count_memcg_events(struct mem_cgroup *memcg,
1080 enum vm_event_item idx,
1081 unsigned long count)
1082{
1083}
1084
Roman Gushchin22621852017-07-06 15:40:25 -07001085static inline void count_memcg_page_event(struct page *page,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001086 int idx)
Roman Gushchin22621852017-07-06 15:40:25 -07001087{
1088}
1089
Ying Han456f9982011-05-26 16:25:38 -07001090static inline
Roman Gushchin22621852017-07-06 15:40:25 -07001091void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001092{
1093}
Andrew Mortonc255a452012-07-31 16:43:02 -07001094#endif /* CONFIG_MEMCG */
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001095
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001096/* idx can be of type enum memcg_stat_item or node_stat_item */
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001097static inline void __inc_memcg_state(struct mem_cgroup *memcg,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001098 int idx)
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001099{
1100 __mod_memcg_state(memcg, idx, 1);
1101}
1102
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001103/* idx can be of type enum memcg_stat_item or node_stat_item */
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001104static inline void __dec_memcg_state(struct mem_cgroup *memcg,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001105 int idx)
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001106{
1107 __mod_memcg_state(memcg, idx, -1);
1108}
1109
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001110/* idx can be of type enum memcg_stat_item or node_stat_item */
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001111static inline void __inc_memcg_page_state(struct page *page,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001112 int idx)
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001113{
1114 __mod_memcg_page_state(page, idx, 1);
1115}
1116
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001117/* idx can be of type enum memcg_stat_item or node_stat_item */
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001118static inline void __dec_memcg_page_state(struct page *page,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001119 int idx)
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001120{
1121 __mod_memcg_page_state(page, idx, -1);
1122}
1123
1124static inline void __inc_lruvec_state(struct lruvec *lruvec,
1125 enum node_stat_item idx)
1126{
1127 __mod_lruvec_state(lruvec, idx, 1);
1128}
1129
1130static inline void __dec_lruvec_state(struct lruvec *lruvec,
1131 enum node_stat_item idx)
1132{
1133 __mod_lruvec_state(lruvec, idx, -1);
1134}
1135
1136static inline void __inc_lruvec_page_state(struct page *page,
1137 enum node_stat_item idx)
1138{
1139 __mod_lruvec_page_state(page, idx, 1);
1140}
1141
1142static inline void __dec_lruvec_page_state(struct page *page,
1143 enum node_stat_item idx)
1144{
1145 __mod_lruvec_page_state(page, idx, -1);
1146}
1147
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001148/* idx can be of type enum memcg_stat_item or node_stat_item */
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001149static inline void inc_memcg_state(struct mem_cgroup *memcg,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001150 int idx)
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001151{
1152 mod_memcg_state(memcg, idx, 1);
1153}
1154
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001155/* idx can be of type enum memcg_stat_item or node_stat_item */
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001156static inline void dec_memcg_state(struct mem_cgroup *memcg,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001157 int idx)
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001158{
1159 mod_memcg_state(memcg, idx, -1);
1160}
1161
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001162/* idx can be of type enum memcg_stat_item or node_stat_item */
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001163static inline void inc_memcg_page_state(struct page *page,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001164 int idx)
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001165{
1166 mod_memcg_page_state(page, idx, 1);
1167}
1168
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001169/* idx can be of type enum memcg_stat_item or node_stat_item */
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001170static inline void dec_memcg_page_state(struct page *page,
Matthias Kaehlcke04fecbf2017-09-06 16:22:09 -07001171 int idx)
Johannes Weiner00f3ca22017-07-06 15:40:52 -07001172{
1173 mod_memcg_page_state(page, idx, -1);
1174}
1175
1176static inline void inc_lruvec_state(struct lruvec *lruvec,
1177 enum node_stat_item idx)
1178{
1179 mod_lruvec_state(lruvec, idx, 1);
1180}
1181
1182static inline void dec_lruvec_state(struct lruvec *lruvec,
1183 enum node_stat_item idx)
1184{
1185 mod_lruvec_state(lruvec, idx, -1);
1186}
1187
1188static inline void inc_lruvec_page_state(struct page *page,
1189 enum node_stat_item idx)
1190{
1191 mod_lruvec_page_state(page, idx, 1);
1192}
1193
1194static inline void dec_lruvec_page_state(struct page *page,
1195 enum node_stat_item idx)
1196{
1197 mod_lruvec_page_state(page, idx, -1);
1198}
1199
Tejun Heo52ebea72015-05-22 17:13:37 -04001200#ifdef CONFIG_CGROUP_WRITEBACK
Tejun Heo841710a2015-05-22 18:23:33 -04001201
Tejun Heo841710a2015-05-22 18:23:33 -04001202struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
Tejun Heoc5edf9c2015-09-29 13:04:26 -04001203void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
1204 unsigned long *pheadroom, unsigned long *pdirty,
1205 unsigned long *pwriteback);
Tejun Heo841710a2015-05-22 18:23:33 -04001206
1207#else /* CONFIG_CGROUP_WRITEBACK */
1208
1209static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
1210{
1211 return NULL;
1212}
1213
Tejun Heoc2aa7232015-05-22 18:23:35 -04001214static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
Tejun Heoc5edf9c2015-09-29 13:04:26 -04001215 unsigned long *pfilepages,
1216 unsigned long *pheadroom,
Tejun Heoc2aa7232015-05-22 18:23:35 -04001217 unsigned long *pdirty,
1218 unsigned long *pwriteback)
1219{
1220}
1221
Tejun Heo841710a2015-05-22 18:23:33 -04001222#endif /* CONFIG_CGROUP_WRITEBACK */
Tejun Heo52ebea72015-05-22 17:13:37 -04001223
Glauber Costae1aab162011-12-11 21:47:03 +00001224struct sock;
Johannes Weinerbaac50b2016-01-14 15:21:17 -08001225bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1226void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
Johannes Weinerd886f4e2016-01-20 15:02:47 -08001227#ifdef CONFIG_MEMCG
Johannes Weineref129472016-01-14 15:21:34 -08001228extern struct static_key_false memcg_sockets_enabled_key;
1229#define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
Johannes Weiner2d758072016-10-07 17:00:58 -07001230void mem_cgroup_sk_alloc(struct sock *sk);
1231void mem_cgroup_sk_free(struct sock *sk);
Johannes Weinerbaac50b2016-01-14 15:21:17 -08001232static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
Johannes Weinere8056052016-01-14 15:21:14 -08001233{
Johannes Weiner0db15292016-01-20 15:02:50 -08001234 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_pressure)
Johannes Weiner8e8ae642016-01-14 15:21:32 -08001235 return true;
Johannes Weiner8e8ae642016-01-14 15:21:32 -08001236 do {
1237 if (time_before(jiffies, memcg->socket_pressure))
1238 return true;
1239 } while ((memcg = parent_mem_cgroup(memcg)));
1240 return false;
Johannes Weinere8056052016-01-14 15:21:14 -08001241}
1242#else
Johannes Weiner80e95fe2016-01-14 15:21:20 -08001243#define mem_cgroup_sockets_enabled 0
Johannes Weiner2d758072016-10-07 17:00:58 -07001244static inline void mem_cgroup_sk_alloc(struct sock *sk) { };
1245static inline void mem_cgroup_sk_free(struct sock *sk) { };
Johannes Weinerbaac50b2016-01-14 15:21:17 -08001246static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
Johannes Weinere8056052016-01-14 15:21:14 -08001247{
1248 return false;
1249}
1250#endif
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08001251
Vladimir Davydov45264772016-07-26 15:24:21 -07001252struct kmem_cache *memcg_kmem_get_cache(struct kmem_cache *cachep);
1253void memcg_kmem_put_cache(struct kmem_cache *cachep);
Roman Gushchin9b6f7e12018-10-26 15:03:19 -07001254
1255#ifdef CONFIG_MEMCG_KMEM
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08001256int __memcg_kmem_charge(struct page *page, gfp_t gfp, int order);
1257void __memcg_kmem_uncharge(struct page *page, int order);
1258int __memcg_kmem_charge_memcg(struct page *page, gfp_t gfp, int order,
1259 struct mem_cgroup *memcg);
Vladimir Davydov45264772016-07-26 15:24:21 -07001260
Johannes Weineref129472016-01-14 15:21:34 -08001261extern struct static_key_false memcg_kmem_enabled_key;
Tejun Heo17cc4df2017-02-22 15:41:36 -08001262extern struct workqueue_struct *memcg_kmem_cache_wq;
Glauber Costa749c5412012-12-18 14:23:01 -08001263
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08001264extern int memcg_nr_cache_ids;
Michal Hocko64219992015-09-08 15:01:07 -07001265void memcg_get_cache_ids(void);
1266void memcg_put_cache_ids(void);
Glauber Costaebe945c2012-12-18 14:23:10 -08001267
1268/*
1269 * Helper macro to loop through all memcg-specific caches. Callers must still
1270 * check if the cache is valid (it is either valid or NULL).
1271 * the slab_mutex must be held when looping through those caches
1272 */
Glauber Costa749c5412012-12-18 14:23:01 -08001273#define for_each_memcg_cache_index(_idx) \
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08001274 for ((_idx) = 0; (_idx) < memcg_nr_cache_ids; (_idx)++)
Glauber Costa749c5412012-12-18 14:23:01 -08001275
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08001276static inline bool memcg_kmem_enabled(void)
1277{
Johannes Weineref129472016-01-14 15:21:34 -08001278 return static_branch_unlikely(&memcg_kmem_enabled_key);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08001279}
1280
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08001281static inline int memcg_kmem_charge(struct page *page, gfp_t gfp, int order)
1282{
1283 if (memcg_kmem_enabled())
1284 return __memcg_kmem_charge(page, gfp, order);
1285 return 0;
1286}
1287
1288static inline void memcg_kmem_uncharge(struct page *page, int order)
1289{
1290 if (memcg_kmem_enabled())
1291 __memcg_kmem_uncharge(page, order);
1292}
1293
1294static inline int memcg_kmem_charge_memcg(struct page *page, gfp_t gfp,
1295 int order, struct mem_cgroup *memcg)
1296{
1297 if (memcg_kmem_enabled())
1298 return __memcg_kmem_charge_memcg(page, gfp, order, memcg);
1299 return 0;
1300}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08001301/*
Jesper Dangaard Brouer9f706d62016-03-15 14:54:03 -07001302 * helper for accessing a memcg's index. It will be used as an index in the
Michal Hocko33398cf2015-09-08 15:01:02 -07001303 * child cache array in kmem_cache, and also to derive its name. This function
1304 * will return -1 when this is not a kmem-limited memcg.
1305 */
1306static inline int memcg_cache_id(struct mem_cgroup *memcg)
1307{
1308 return memcg ? memcg->kmemcg_id : -1;
1309}
Vladimir Davydov5722d092014-04-07 15:39:24 -07001310
Kirill Tkhai0a4465d2018-08-17 15:47:37 -07001311extern int memcg_expand_shrinker_maps(int new_id);
1312
Kirill Tkhaifae91d62018-08-17 15:48:10 -07001313extern void memcg_set_shrinker_bit(struct mem_cgroup *memcg,
1314 int nid, int shrinker_id);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08001315#else
Roman Gushchin9b6f7e12018-10-26 15:03:19 -07001316
1317static inline int memcg_kmem_charge(struct page *page, gfp_t gfp, int order)
1318{
1319 return 0;
1320}
1321
1322static inline void memcg_kmem_uncharge(struct page *page, int order)
1323{
1324}
1325
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08001326static inline int __memcg_kmem_charge(struct page *page, gfp_t gfp, int order)
1327{
1328 return 0;
1329}
1330
1331static inline void __memcg_kmem_uncharge(struct page *page, int order)
1332{
1333}
1334
Glauber Costa749c5412012-12-18 14:23:01 -08001335#define for_each_memcg_cache_index(_idx) \
1336 for (; NULL; )
1337
Glauber Costab9ce5ef2012-12-18 14:22:46 -08001338static inline bool memcg_kmem_enabled(void)
1339{
1340 return false;
1341}
1342
Glauber Costa2633d7a2012-12-18 14:22:34 -08001343static inline int memcg_cache_id(struct mem_cgroup *memcg)
1344{
1345 return -1;
1346}
1347
Vladimir Davydov05257a12015-02-12 14:59:01 -08001348static inline void memcg_get_cache_ids(void)
1349{
1350}
1351
1352static inline void memcg_put_cache_ids(void)
1353{
1354}
1355
Kirill Tkhaifae91d62018-08-17 15:48:10 -07001356static inline void memcg_set_shrinker_bit(struct mem_cgroup *memcg,
1357 int nid, int shrinker_id) { }
Kirill Tkhai84c07d12018-08-17 15:47:25 -07001358#endif /* CONFIG_MEMCG_KMEM */
Johannes Weiner127424c2016-01-20 15:02:32 -08001359
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001360#endif /* _LINUX_MEMCONTROL_H */