]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/memcontrol.h
net: rtnetlink: validate IFLA_MTU attribute in rtnl_create_link()
[mirror_ubuntu-bionic-kernel.git] / include / linux / memcontrol.h
CommitLineData
8cdea7c0
BS
1/* memcontrol.h - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
78fb7466
PE
6 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
8cdea7c0
BS
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#ifndef _LINUX_MEMCONTROL_H
21#define _LINUX_MEMCONTROL_H
f8d66542 22#include <linux/cgroup.h>
456f998e 23#include <linux/vm_event_item.h>
7ae1e1d0 24#include <linux/hardirq.h>
a8964b9b 25#include <linux/jump_label.h>
33398cf2
MH
26#include <linux/page_counter.h>
27#include <linux/vmpressure.h>
28#include <linux/eventfd.h>
00f3ca2c
JW
29#include <linux/mm.h>
30#include <linux/vmstat.h>
33398cf2 31#include <linux/writeback.h>
fdf1cdb9 32#include <linux/page-flags.h>
456f998e 33
78fb7466 34struct mem_cgroup;
8697d331
BS
35struct page;
36struct mm_struct;
2633d7a0 37struct kmem_cache;
78fb7466 38
71cd3113
JW
39/* Cgroup-specific page state, on top of universal node page state */
40enum memcg_stat_item {
41 MEMCG_CACHE = NR_VM_NODE_STAT_ITEMS,
42 MEMCG_RSS,
43 MEMCG_RSS_HUGE,
44 MEMCG_SWAP,
45 MEMCG_SOCK,
46 /* XXX: why are these zone and not node counters? */
47 MEMCG_KERNEL_STACK_KB,
b2807f07 48 MEMCG_NR_STAT,
2a7106f2
GT
49};
50
71cd3113
JW
51/* Cgroup-specific events, on top of universal VM events */
52enum memcg_event_item {
53 MEMCG_LOW = NR_VM_EVENT_ITEMS,
54 MEMCG_HIGH,
55 MEMCG_MAX,
56 MEMCG_OOM,
57 MEMCG_NR_EVENTS,
58};
59
5660048c 60struct mem_cgroup_reclaim_cookie {
ef8f2327 61 pg_data_t *pgdat;
5660048c
JW
62 int priority;
63 unsigned int generation;
64};
65
71cd3113
JW
66#ifdef CONFIG_MEMCG
67
68#define MEM_CGROUP_ID_SHIFT 16
69#define MEM_CGROUP_ID_MAX USHRT_MAX
70
71struct mem_cgroup_id {
72 int id;
73 atomic_t ref;
74};
75
33398cf2
MH
76/*
77 * Per memcg event counter is incremented at every pagein/pageout. With THP,
78 * it will be incremated by the number of pages. This counter is used for
79 * for trigger some periodic events. This is straightforward and better
80 * than using jiffies etc. to handle periodic memcg event.
81 */
82enum mem_cgroup_events_target {
83 MEM_CGROUP_TARGET_THRESH,
84 MEM_CGROUP_TARGET_SOFTLIMIT,
85 MEM_CGROUP_TARGET_NUMAINFO,
86 MEM_CGROUP_NTARGETS,
87};
88
33398cf2 89struct mem_cgroup_stat_cpu {
b2807f07 90 long count[MEMCG_NR_STAT];
33398cf2
MH
91 unsigned long events[MEMCG_NR_EVENTS];
92 unsigned long nr_page_events;
93 unsigned long targets[MEM_CGROUP_NTARGETS];
94};
95
96struct mem_cgroup_reclaim_iter {
97 struct mem_cgroup *position;
98 /* scan generation, increased every round-trip */
99 unsigned int generation;
100};
101
00f3ca2c
JW
102struct lruvec_stat {
103 long count[NR_VM_NODE_STAT_ITEMS];
104};
105
33398cf2
MH
106/*
107 * per-zone information in memory controller.
108 */
ef8f2327 109struct mem_cgroup_per_node {
33398cf2 110 struct lruvec lruvec;
00f3ca2c 111 struct lruvec_stat __percpu *lruvec_stat;
b4536f0c 112 unsigned long lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
33398cf2
MH
113
114 struct mem_cgroup_reclaim_iter iter[DEF_PRIORITY + 1];
115
116 struct rb_node tree_node; /* RB tree node */
117 unsigned long usage_in_excess;/* Set to the value by which */
118 /* the soft limit is exceeded*/
119 bool on_tree;
120 struct mem_cgroup *memcg; /* Back pointer, we cannot */
121 /* use container_of */
122};
123
33398cf2
MH
124struct mem_cgroup_threshold {
125 struct eventfd_ctx *eventfd;
126 unsigned long threshold;
127};
128
129/* For threshold */
130struct mem_cgroup_threshold_ary {
131 /* An array index points to threshold just below or equal to usage. */
132 int current_threshold;
133 /* Size of entries[] */
134 unsigned int size;
135 /* Array of thresholds */
136 struct mem_cgroup_threshold entries[0];
137};
138
139struct mem_cgroup_thresholds {
140 /* Primary thresholds array */
141 struct mem_cgroup_threshold_ary *primary;
142 /*
143 * Spare threshold array.
144 * This is needed to make mem_cgroup_unregister_event() "never fail".
145 * It must be able to store at least primary->size - 1 entries.
146 */
147 struct mem_cgroup_threshold_ary *spare;
148};
149
567e9ab2
JW
150enum memcg_kmem_state {
151 KMEM_NONE,
152 KMEM_ALLOCATED,
153 KMEM_ONLINE,
154};
155
33398cf2
MH
156/*
157 * The memory controller data structure. The memory controller controls both
158 * page cache and RSS per cgroup. We would eventually like to provide
159 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
160 * to help the administrator determine what knobs to tune.
161 */
162struct mem_cgroup {
163 struct cgroup_subsys_state css;
164
73f576c0
JW
165 /* Private memcg ID. Used to ID objects that outlive the cgroup */
166 struct mem_cgroup_id id;
167
33398cf2
MH
168 /* Accounted resources */
169 struct page_counter memory;
37e84351 170 struct page_counter swap;
0db15298
JW
171
172 /* Legacy consumer-oriented counters */
33398cf2
MH
173 struct page_counter memsw;
174 struct page_counter kmem;
0db15298 175 struct page_counter tcpmem;
33398cf2
MH
176
177 /* Normal memory consumption range */
178 unsigned long low;
179 unsigned long high;
180
f7e1cb6e
JW
181 /* Range enforcement for interrupt charges */
182 struct work_struct high_work;
183
33398cf2
MH
184 unsigned long soft_limit;
185
186 /* vmpressure notifications */
187 struct vmpressure vmpressure;
188
33398cf2
MH
189 /*
190 * Should the accounting and control be hierarchical, per subtree?
191 */
192 bool use_hierarchy;
193
194 /* protected by memcg_oom_lock */
195 bool oom_lock;
196 int under_oom;
197
198 int swappiness;
199 /* OOM-Killer disable */
200 int oom_kill_disable;
201
472912a2
TH
202 /* handle for "memory.events" */
203 struct cgroup_file events_file;
204
33398cf2
MH
205 /* protect arrays of thresholds */
206 struct mutex thresholds_lock;
207
208 /* thresholds for memory usage. RCU-protected */
209 struct mem_cgroup_thresholds thresholds;
210
211 /* thresholds for mem+swap usage. RCU-protected */
212 struct mem_cgroup_thresholds memsw_thresholds;
213
214 /* For oom notifier event fd */
215 struct list_head oom_notify;
216
217 /*
218 * Should we move charges of a task when a task is moved into this
219 * mem_cgroup ? And what type of charges should we move ?
220 */
221 unsigned long move_charge_at_immigrate;
222 /*
223 * set > 0 if pages under this cgroup are moving to other cgroup.
224 */
225 atomic_t moving_account;
226 /* taken only while moving_account > 0 */
227 spinlock_t move_lock;
228 struct task_struct *move_lock_task;
229 unsigned long move_lock_flags;
230 /*
231 * percpu counter.
232 */
233 struct mem_cgroup_stat_cpu __percpu *stat;
33398cf2 234
d886f4e4
JW
235 unsigned long socket_pressure;
236
237 /* Legacy tcp memory accounting */
0db15298
JW
238 bool tcpmem_active;
239 int tcpmem_pressure;
d886f4e4 240
127424c8 241#ifndef CONFIG_SLOB
33398cf2
MH
242 /* Index in the kmem_cache->memcg_params.memcg_caches array */
243 int kmemcg_id;
567e9ab2 244 enum memcg_kmem_state kmem_state;
bc2791f8 245 struct list_head kmem_caches;
33398cf2
MH
246#endif
247
248 int last_scanned_node;
249#if MAX_NUMNODES > 1
250 nodemask_t scan_nodes;
251 atomic_t numainfo_events;
252 atomic_t numainfo_updating;
253#endif
254
255#ifdef CONFIG_CGROUP_WRITEBACK
256 struct list_head cgwb_list;
257 struct wb_domain cgwb_domain;
258#endif
259
260 /* List of events which userspace want to receive */
261 struct list_head event_list;
262 spinlock_t event_list_lock;
263
264 struct mem_cgroup_per_node *nodeinfo[0];
265 /* WARNING: nodeinfo must be the last member here */
266};
7d828602
JW
267
268extern struct mem_cgroup *root_mem_cgroup;
56161634 269
23047a96
JW
270static inline bool mem_cgroup_disabled(void)
271{
272 return !cgroup_subsys_enabled(memory_cgrp_subsys);
273}
274
31176c78 275static inline void mem_cgroup_event(struct mem_cgroup *memcg,
df0e53d0 276 enum memcg_event_item event)
33398cf2 277{
df0e53d0 278 this_cpu_inc(memcg->stat->events[event]);
472912a2 279 cgroup_file_notify(&memcg->events_file);
33398cf2 280}
241994ed
JW
281
282bool mem_cgroup_low(struct mem_cgroup *root, struct mem_cgroup *memcg);
283
00501b53 284int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
f627c2f5
KS
285 gfp_t gfp_mask, struct mem_cgroup **memcgp,
286 bool compound);
00501b53 287void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
f627c2f5
KS
288 bool lrucare, bool compound);
289void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg,
290 bool compound);
0a31bc97 291void mem_cgroup_uncharge(struct page *page);
747db954 292void mem_cgroup_uncharge_list(struct list_head *page_list);
569b846d 293
6a93ca8f 294void mem_cgroup_migrate(struct page *oldpage, struct page *newpage);
569b846d 295
ef8f2327
MG
296static struct mem_cgroup_per_node *
297mem_cgroup_nodeinfo(struct mem_cgroup *memcg, int nid)
55779ec7 298{
ef8f2327 299 return memcg->nodeinfo[nid];
55779ec7
JW
300}
301
302/**
a9dd0a83
MG
303 * mem_cgroup_lruvec - get the lru list vector for a node or a memcg zone
304 * @node: node of the wanted lruvec
55779ec7
JW
305 * @memcg: memcg of the wanted lruvec
306 *
a9dd0a83
MG
307 * Returns the lru list vector holding pages for a given @node or a given
308 * @memcg and @zone. This can be the node lruvec, if the memory controller
55779ec7
JW
309 * is disabled.
310 */
a9dd0a83 311static inline struct lruvec *mem_cgroup_lruvec(struct pglist_data *pgdat,
ef8f2327 312 struct mem_cgroup *memcg)
55779ec7 313{
ef8f2327 314 struct mem_cgroup_per_node *mz;
55779ec7
JW
315 struct lruvec *lruvec;
316
317 if (mem_cgroup_disabled()) {
a9dd0a83 318 lruvec = node_lruvec(pgdat);
55779ec7
JW
319 goto out;
320 }
321
ef8f2327 322 mz = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
55779ec7
JW
323 lruvec = &mz->lruvec;
324out:
325 /*
326 * Since a node can be onlined after the mem_cgroup was created,
599d0c95 327 * we have to be prepared to initialize lruvec->pgdat here;
55779ec7
JW
328 * and if offlined then reonlined, we need to reinitialize it.
329 */
ef8f2327
MG
330 if (unlikely(lruvec->pgdat != pgdat))
331 lruvec->pgdat = pgdat;
55779ec7
JW
332 return lruvec;
333}
334
599d0c95 335struct lruvec *mem_cgroup_page_lruvec(struct page *, struct pglist_data *);
c9b0ed51 336
2314b42d 337bool task_in_mem_cgroup(struct task_struct *task, struct mem_cgroup *memcg);
64219994 338struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
e993d905 339
33398cf2
MH
340static inline
341struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
342 return css ? container_of(css, struct mem_cgroup, css) : NULL;
343}
344
8e8ae645
JW
345#define mem_cgroup_from_counter(counter, member) \
346 container_of(counter, struct mem_cgroup, member)
347
33398cf2
MH
348struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
349 struct mem_cgroup *,
350 struct mem_cgroup_reclaim_cookie *);
351void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
7c5f64f8
VD
352int mem_cgroup_scan_tasks(struct mem_cgroup *,
353 int (*)(struct task_struct *, void *), void *);
33398cf2 354
23047a96
JW
355static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
356{
357 if (mem_cgroup_disabled())
358 return 0;
359
73f576c0 360 return memcg->id.id;
23047a96 361}
73f576c0 362struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
23047a96 363
2262185c
RG
364static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
365{
366 struct mem_cgroup_per_node *mz;
367
368 if (mem_cgroup_disabled())
369 return NULL;
370
371 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
372 return mz->memcg;
373}
374
8e8ae645
JW
375/**
376 * parent_mem_cgroup - find the accounting parent of a memcg
377 * @memcg: memcg whose parent to find
378 *
379 * Returns the parent memcg, or NULL if this is the root or the memory
380 * controller is in legacy no-hierarchy mode.
381 */
382static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
383{
384 if (!memcg->memory.parent)
385 return NULL;
386 return mem_cgroup_from_counter(memcg->memory.parent, memory);
387}
388
33398cf2
MH
389static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
390 struct mem_cgroup *root)
391{
392 if (root == memcg)
393 return true;
394 if (!root->use_hierarchy)
395 return false;
396 return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
397}
e1aab161 398
2314b42d
JW
399static inline bool mm_match_cgroup(struct mm_struct *mm,
400 struct mem_cgroup *memcg)
2e4d4091 401{
587af308 402 struct mem_cgroup *task_memcg;
413918bb 403 bool match = false;
c3ac9a8a 404
2e4d4091 405 rcu_read_lock();
587af308 406 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
413918bb 407 if (task_memcg)
2314b42d 408 match = mem_cgroup_is_descendant(task_memcg, memcg);
2e4d4091 409 rcu_read_unlock();
c3ac9a8a 410 return match;
2e4d4091 411}
8a9f3ccd 412
64219994 413struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page);
2fc04524 414ino_t page_cgroup_ino(struct page *page);
d324236b 415
eb01aaab
VD
416static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
417{
418 if (mem_cgroup_disabled())
419 return true;
420 return !!(memcg->css.flags & CSS_ONLINE);
421}
422
58ae83db
KH
423/*
424 * For memory reclaim.
425 */
889976db 426int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
33398cf2
MH
427
428void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
b4536f0c 429 int zid, int nr_pages);
33398cf2 430
0a6b76dd
VD
431unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
432 int nid, unsigned int lru_mask);
433
33398cf2
MH
434static inline
435unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
436{
ef8f2327 437 struct mem_cgroup_per_node *mz;
b4536f0c
MH
438 unsigned long nr_pages = 0;
439 int zid;
33398cf2 440
ef8f2327 441 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
b4536f0c
MH
442 for (zid = 0; zid < MAX_NR_ZONES; zid++)
443 nr_pages += mz->lru_zone_size[zid][lru];
444 return nr_pages;
445}
446
447static inline
448unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
449 enum lru_list lru, int zone_idx)
450{
451 struct mem_cgroup_per_node *mz;
452
453 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
454 return mz->lru_zone_size[zone_idx][lru];
33398cf2
MH
455}
456
b23afb93
TH
457void mem_cgroup_handle_over_high(void);
458
7c5f64f8
VD
459unsigned long mem_cgroup_get_limit(struct mem_cgroup *memcg);
460
64219994
MH
461void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
462 struct task_struct *p);
58ae83db 463
49426420 464static inline void mem_cgroup_oom_enable(void)
519e5247 465{
626ebc41
TH
466 WARN_ON(current->memcg_may_oom);
467 current->memcg_may_oom = 1;
519e5247
JW
468}
469
49426420 470static inline void mem_cgroup_oom_disable(void)
519e5247 471{
626ebc41
TH
472 WARN_ON(!current->memcg_may_oom);
473 current->memcg_may_oom = 0;
519e5247
JW
474}
475
3812c8c8
JW
476static inline bool task_in_memcg_oom(struct task_struct *p)
477{
626ebc41 478 return p->memcg_in_oom;
3812c8c8
JW
479}
480
49426420 481bool mem_cgroup_oom_synchronize(bool wait);
3812c8c8 482
c255a458 483#ifdef CONFIG_MEMCG_SWAP
c077719b
KH
484extern int do_swap_account;
485#endif
f8d66542 486
739f79fc
JW
487struct mem_cgroup *lock_page_memcg(struct page *page);
488void __unlock_page_memcg(struct mem_cgroup *memcg);
62cccb8c 489void unlock_page_memcg(struct page *page);
d7365e78 490
04fecbf5 491/* idx can be of type enum memcg_stat_item or node_stat_item */
ccda7f43 492static inline unsigned long memcg_page_state(struct mem_cgroup *memcg,
04fecbf5 493 int idx)
2a2e4885
JW
494{
495 long val = 0;
496 int cpu;
497
498 for_each_possible_cpu(cpu)
499 val += per_cpu(memcg->stat->count[idx], cpu);
500
501 if (val < 0)
502 val = 0;
503
504 return val;
505}
506
04fecbf5 507/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 508static inline void __mod_memcg_state(struct mem_cgroup *memcg,
04fecbf5 509 int idx, int val)
2a2e4885
JW
510{
511 if (!mem_cgroup_disabled())
00f3ca2c 512 __this_cpu_add(memcg->stat->count[idx], val);
2a2e4885
JW
513}
514
04fecbf5 515/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 516static inline void mod_memcg_state(struct mem_cgroup *memcg,
04fecbf5 517 int idx, int val)
2a2e4885 518{
00f3ca2c
JW
519 if (!mem_cgroup_disabled())
520 this_cpu_add(memcg->stat->count[idx], val);
2a2e4885
JW
521}
522
33398cf2 523/**
ccda7f43 524 * mod_memcg_page_state - update page state statistics
62cccb8c 525 * @page: the page
33398cf2
MH
526 * @idx: page state item to account
527 * @val: number of pages (positive or negative)
528 *
fdf1cdb9
JW
529 * The @page must be locked or the caller must use lock_page_memcg()
530 * to prevent double accounting when the page is concurrently being
531 * moved to another memcg:
81f8c3a4 532 *
fdf1cdb9 533 * lock_page(page) or lock_page_memcg(page)
81f8c3a4 534 * if (TestClearPageState(page))
ccda7f43 535 * mod_memcg_page_state(page, state, -1);
fdf1cdb9 536 * unlock_page(page) or unlock_page_memcg(page)
2a2e4885
JW
537 *
538 * Kernel pages are an exception to this, since they'll never move.
33398cf2 539 */
00f3ca2c 540static inline void __mod_memcg_page_state(struct page *page,
04fecbf5 541 int idx, int val)
00f3ca2c
JW
542{
543 if (page->mem_cgroup)
544 __mod_memcg_state(page->mem_cgroup, idx, val);
545}
546
ccda7f43 547static inline void mod_memcg_page_state(struct page *page,
04fecbf5 548 int idx, int val)
33398cf2 549{
62cccb8c 550 if (page->mem_cgroup)
ccda7f43 551 mod_memcg_state(page->mem_cgroup, idx, val);
33398cf2
MH
552}
553
00f3ca2c
JW
554static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
555 enum node_stat_item idx)
2a7106f2 556{
00f3ca2c
JW
557 struct mem_cgroup_per_node *pn;
558 long val = 0;
559 int cpu;
560
561 if (mem_cgroup_disabled())
562 return node_page_state(lruvec_pgdat(lruvec), idx);
563
564 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
565 for_each_possible_cpu(cpu)
566 val += per_cpu(pn->lruvec_stat->count[idx], cpu);
567
568 if (val < 0)
569 val = 0;
570
571 return val;
2a7106f2
GT
572}
573
00f3ca2c
JW
574static inline void __mod_lruvec_state(struct lruvec *lruvec,
575 enum node_stat_item idx, int val)
2a7106f2 576{
00f3ca2c
JW
577 struct mem_cgroup_per_node *pn;
578
579 __mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
580 if (mem_cgroup_disabled())
581 return;
582 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
583 __mod_memcg_state(pn->memcg, idx, val);
584 __this_cpu_add(pn->lruvec_stat->count[idx], val);
585}
586
587static inline void mod_lruvec_state(struct lruvec *lruvec,
588 enum node_stat_item idx, int val)
589{
590 struct mem_cgroup_per_node *pn;
591
592 mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
593 if (mem_cgroup_disabled())
594 return;
595 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
596 mod_memcg_state(pn->memcg, idx, val);
597 this_cpu_add(pn->lruvec_stat->count[idx], val);
598}
599
600static inline void __mod_lruvec_page_state(struct page *page,
601 enum node_stat_item idx, int val)
602{
603 struct mem_cgroup_per_node *pn;
604
605 __mod_node_page_state(page_pgdat(page), idx, val);
606 if (mem_cgroup_disabled() || !page->mem_cgroup)
607 return;
608 __mod_memcg_state(page->mem_cgroup, idx, val);
609 pn = page->mem_cgroup->nodeinfo[page_to_nid(page)];
610 __this_cpu_add(pn->lruvec_stat->count[idx], val);
611}
612
613static inline void mod_lruvec_page_state(struct page *page,
614 enum node_stat_item idx, int val)
615{
616 struct mem_cgroup_per_node *pn;
617
618 mod_node_page_state(page_pgdat(page), idx, val);
619 if (mem_cgroup_disabled() || !page->mem_cgroup)
620 return;
621 mod_memcg_state(page->mem_cgroup, idx, val);
622 pn = page->mem_cgroup->nodeinfo[page_to_nid(page)];
623 this_cpu_add(pn->lruvec_stat->count[idx], val);
2a7106f2
GT
624}
625
ef8f2327 626unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
0608f43d
AM
627 gfp_t gfp_mask,
628 unsigned long *total_scanned);
a63d83f4 629
2262185c
RG
630static inline void count_memcg_events(struct mem_cgroup *memcg,
631 enum vm_event_item idx,
632 unsigned long count)
633{
634 if (!mem_cgroup_disabled())
635 this_cpu_add(memcg->stat->events[idx], count);
636}
637
04fecbf5 638/* idx can be of type enum memcg_stat_item or node_stat_item */
2262185c 639static inline void count_memcg_page_event(struct page *page,
04fecbf5 640 int idx)
2262185c
RG
641{
642 if (page->mem_cgroup)
643 count_memcg_events(page->mem_cgroup, idx, 1);
644}
645
646static inline void count_memcg_event_mm(struct mm_struct *mm,
647 enum vm_event_item idx)
68ae564b 648{
33398cf2
MH
649 struct mem_cgroup *memcg;
650
68ae564b
DR
651 if (mem_cgroup_disabled())
652 return;
33398cf2
MH
653
654 rcu_read_lock();
655 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
8e675f7a 656 if (likely(memcg)) {
df0e53d0 657 this_cpu_inc(memcg->stat->events[idx]);
8e675f7a
KK
658 if (idx == OOM_KILL)
659 cgroup_file_notify(&memcg->events_file);
660 }
33398cf2 661 rcu_read_unlock();
68ae564b 662}
ca3e0214 663#ifdef CONFIG_TRANSPARENT_HUGEPAGE
e94c8a9c 664void mem_cgroup_split_huge_fixup(struct page *head);
ca3e0214
KH
665#endif
666
c255a458 667#else /* CONFIG_MEMCG */
23047a96
JW
668
669#define MEM_CGROUP_ID_SHIFT 0
670#define MEM_CGROUP_ID_MAX 0
671
7a81b88c
KH
672struct mem_cgroup;
673
23047a96
JW
674static inline bool mem_cgroup_disabled(void)
675{
676 return true;
677}
678
31176c78 679static inline void mem_cgroup_event(struct mem_cgroup *memcg,
df0e53d0 680 enum memcg_event_item event)
241994ed
JW
681{
682}
683
684static inline bool mem_cgroup_low(struct mem_cgroup *root,
685 struct mem_cgroup *memcg)
686{
687 return false;
688}
689
00501b53
JW
690static inline int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
691 gfp_t gfp_mask,
f627c2f5
KS
692 struct mem_cgroup **memcgp,
693 bool compound)
7a81b88c 694{
00501b53 695 *memcgp = NULL;
7a81b88c
KH
696 return 0;
697}
698
00501b53
JW
699static inline void mem_cgroup_commit_charge(struct page *page,
700 struct mem_cgroup *memcg,
f627c2f5 701 bool lrucare, bool compound)
7a81b88c
KH
702{
703}
704
00501b53 705static inline void mem_cgroup_cancel_charge(struct page *page,
f627c2f5
KS
706 struct mem_cgroup *memcg,
707 bool compound)
7a81b88c
KH
708{
709}
710
0a31bc97 711static inline void mem_cgroup_uncharge(struct page *page)
569b846d
KH
712{
713}
714
747db954 715static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
8a9f3ccd
BS
716{
717}
718
6a93ca8f 719static inline void mem_cgroup_migrate(struct page *old, struct page *new)
69029cd5
KH
720{
721}
722
a9dd0a83 723static inline struct lruvec *mem_cgroup_lruvec(struct pglist_data *pgdat,
ef8f2327 724 struct mem_cgroup *memcg)
08e552c6 725{
a9dd0a83 726 return node_lruvec(pgdat);
08e552c6
KH
727}
728
fa9add64 729static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
599d0c95 730 struct pglist_data *pgdat)
66e1707b 731{
599d0c95 732 return &pgdat->lruvec;
66e1707b
BS
733}
734
587af308 735static inline bool mm_match_cgroup(struct mm_struct *mm,
c0ff4b85 736 struct mem_cgroup *memcg)
bed7161a 737{
587af308 738 return true;
bed7161a
BS
739}
740
ffbdccf5
DR
741static inline bool task_in_mem_cgroup(struct task_struct *task,
742 const struct mem_cgroup *memcg)
4c4a2214 743{
ffbdccf5 744 return true;
4c4a2214
DR
745}
746
5660048c
JW
747static inline struct mem_cgroup *
748mem_cgroup_iter(struct mem_cgroup *root,
749 struct mem_cgroup *prev,
750 struct mem_cgroup_reclaim_cookie *reclaim)
751{
752 return NULL;
753}
754
755static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
756 struct mem_cgroup *prev)
757{
758}
759
7c5f64f8
VD
760static inline int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
761 int (*fn)(struct task_struct *, void *), void *arg)
762{
763 return 0;
764}
765
23047a96 766static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
f8d66542 767{
23047a96
JW
768 return 0;
769}
770
771static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
772{
773 WARN_ON_ONCE(id);
774 /* XXX: This should always return root_mem_cgroup */
775 return NULL;
f8d66542 776}
a636b327 777
2262185c
RG
778static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
779{
780 return NULL;
781}
782
eb01aaab 783static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
14797e23 784{
13308ca9 785 return true;
14797e23
KM
786}
787
a3d8e054 788static inline unsigned long
4d7dcca2 789mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
a3d8e054
KM
790{
791 return 0;
792}
b4536f0c
MH
793static inline
794unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
795 enum lru_list lru, int zone_idx)
796{
797 return 0;
798}
a3d8e054 799
0a6b76dd
VD
800static inline unsigned long
801mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
802 int nid, unsigned int lru_mask)
803{
804 return 0;
805}
806
7c5f64f8
VD
807static inline unsigned long mem_cgroup_get_limit(struct mem_cgroup *memcg)
808{
809 return 0;
810}
811
e222432b
BS
812static inline void
813mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
814{
815}
816
739f79fc
JW
817static inline struct mem_cgroup *lock_page_memcg(struct page *page)
818{
819 return NULL;
820}
821
822static inline void __unlock_page_memcg(struct mem_cgroup *memcg)
89c06bd5
KH
823{
824}
825
62cccb8c 826static inline void unlock_page_memcg(struct page *page)
89c06bd5
KH
827{
828}
829
b23afb93
TH
830static inline void mem_cgroup_handle_over_high(void)
831{
832}
833
49426420 834static inline void mem_cgroup_oom_enable(void)
519e5247
JW
835{
836}
837
49426420 838static inline void mem_cgroup_oom_disable(void)
519e5247
JW
839{
840}
841
3812c8c8
JW
842static inline bool task_in_memcg_oom(struct task_struct *p)
843{
844 return false;
845}
846
49426420 847static inline bool mem_cgroup_oom_synchronize(bool wait)
3812c8c8
JW
848{
849 return false;
850}
851
ccda7f43 852static inline unsigned long memcg_page_state(struct mem_cgroup *memcg,
04fecbf5 853 int idx)
2a2e4885
JW
854{
855 return 0;
856}
857
00f3ca2c 858static inline void __mod_memcg_state(struct mem_cgroup *memcg,
04fecbf5 859 int idx,
00f3ca2c 860 int nr)
2a2e4885
JW
861{
862}
863
00f3ca2c 864static inline void mod_memcg_state(struct mem_cgroup *memcg,
04fecbf5 865 int idx,
00f3ca2c 866 int nr)
2a2e4885
JW
867{
868}
869
00f3ca2c 870static inline void __mod_memcg_page_state(struct page *page,
04fecbf5 871 int idx,
00f3ca2c 872 int nr)
2a2e4885
JW
873{
874}
875
ccda7f43 876static inline void mod_memcg_page_state(struct page *page,
04fecbf5 877 int idx,
ccda7f43 878 int nr)
553af430
JW
879{
880}
881
00f3ca2c
JW
882static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
883 enum node_stat_item idx)
2a7106f2 884{
00f3ca2c 885 return node_page_state(lruvec_pgdat(lruvec), idx);
2a7106f2
GT
886}
887
00f3ca2c
JW
888static inline void __mod_lruvec_state(struct lruvec *lruvec,
889 enum node_stat_item idx, int val)
d69b042f 890{
00f3ca2c
JW
891 __mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
892}
893
894static inline void mod_lruvec_state(struct lruvec *lruvec,
895 enum node_stat_item idx, int val)
896{
897 mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
898}
899
900static inline void __mod_lruvec_page_state(struct page *page,
901 enum node_stat_item idx, int val)
902{
903 __mod_node_page_state(page_pgdat(page), idx, val);
904}
905
906static inline void mod_lruvec_page_state(struct page *page,
907 enum node_stat_item idx, int val)
908{
909 mod_node_page_state(page_pgdat(page), idx, val);
d69b042f
BS
910}
911
4e416953 912static inline
ef8f2327 913unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
0608f43d
AM
914 gfp_t gfp_mask,
915 unsigned long *total_scanned)
4e416953 916{
0608f43d 917 return 0;
4e416953
BS
918}
919
e94c8a9c 920static inline void mem_cgroup_split_huge_fixup(struct page *head)
ca3e0214
KH
921{
922}
923
2262185c
RG
924static inline void count_memcg_events(struct mem_cgroup *memcg,
925 enum vm_event_item idx,
926 unsigned long count)
927{
928}
929
930static inline void count_memcg_page_event(struct page *page,
04fecbf5 931 int idx)
2262185c
RG
932{
933}
934
456f998e 935static inline
2262185c 936void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
456f998e
YH
937{
938}
c255a458 939#endif /* CONFIG_MEMCG */
78fb7466 940
04fecbf5 941/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 942static inline void __inc_memcg_state(struct mem_cgroup *memcg,
04fecbf5 943 int idx)
00f3ca2c
JW
944{
945 __mod_memcg_state(memcg, idx, 1);
946}
947
04fecbf5 948/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 949static inline void __dec_memcg_state(struct mem_cgroup *memcg,
04fecbf5 950 int idx)
00f3ca2c
JW
951{
952 __mod_memcg_state(memcg, idx, -1);
953}
954
04fecbf5 955/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 956static inline void __inc_memcg_page_state(struct page *page,
04fecbf5 957 int idx)
00f3ca2c
JW
958{
959 __mod_memcg_page_state(page, idx, 1);
960}
961
04fecbf5 962/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 963static inline void __dec_memcg_page_state(struct page *page,
04fecbf5 964 int idx)
00f3ca2c
JW
965{
966 __mod_memcg_page_state(page, idx, -1);
967}
968
969static inline void __inc_lruvec_state(struct lruvec *lruvec,
970 enum node_stat_item idx)
971{
972 __mod_lruvec_state(lruvec, idx, 1);
973}
974
975static inline void __dec_lruvec_state(struct lruvec *lruvec,
976 enum node_stat_item idx)
977{
978 __mod_lruvec_state(lruvec, idx, -1);
979}
980
981static inline void __inc_lruvec_page_state(struct page *page,
982 enum node_stat_item idx)
983{
984 __mod_lruvec_page_state(page, idx, 1);
985}
986
987static inline void __dec_lruvec_page_state(struct page *page,
988 enum node_stat_item idx)
989{
990 __mod_lruvec_page_state(page, idx, -1);
991}
992
04fecbf5 993/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 994static inline void inc_memcg_state(struct mem_cgroup *memcg,
04fecbf5 995 int idx)
00f3ca2c
JW
996{
997 mod_memcg_state(memcg, idx, 1);
998}
999
04fecbf5 1000/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 1001static inline void dec_memcg_state(struct mem_cgroup *memcg,
04fecbf5 1002 int idx)
00f3ca2c
JW
1003{
1004 mod_memcg_state(memcg, idx, -1);
1005}
1006
04fecbf5 1007/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 1008static inline void inc_memcg_page_state(struct page *page,
04fecbf5 1009 int idx)
00f3ca2c
JW
1010{
1011 mod_memcg_page_state(page, idx, 1);
1012}
1013
04fecbf5 1014/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 1015static inline void dec_memcg_page_state(struct page *page,
04fecbf5 1016 int idx)
00f3ca2c
JW
1017{
1018 mod_memcg_page_state(page, idx, -1);
1019}
1020
1021static inline void inc_lruvec_state(struct lruvec *lruvec,
1022 enum node_stat_item idx)
1023{
1024 mod_lruvec_state(lruvec, idx, 1);
1025}
1026
1027static inline void dec_lruvec_state(struct lruvec *lruvec,
1028 enum node_stat_item idx)
1029{
1030 mod_lruvec_state(lruvec, idx, -1);
1031}
1032
1033static inline void inc_lruvec_page_state(struct page *page,
1034 enum node_stat_item idx)
1035{
1036 mod_lruvec_page_state(page, idx, 1);
1037}
1038
1039static inline void dec_lruvec_page_state(struct page *page,
1040 enum node_stat_item idx)
1041{
1042 mod_lruvec_page_state(page, idx, -1);
1043}
1044
52ebea74 1045#ifdef CONFIG_CGROUP_WRITEBACK
841710aa 1046
52ebea74 1047struct list_head *mem_cgroup_cgwb_list(struct mem_cgroup *memcg);
841710aa 1048struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
c5edf9cd
TH
1049void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
1050 unsigned long *pheadroom, unsigned long *pdirty,
1051 unsigned long *pwriteback);
841710aa
TH
1052
1053#else /* CONFIG_CGROUP_WRITEBACK */
1054
1055static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
1056{
1057 return NULL;
1058}
1059
c2aa723a 1060static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
c5edf9cd
TH
1061 unsigned long *pfilepages,
1062 unsigned long *pheadroom,
c2aa723a
TH
1063 unsigned long *pdirty,
1064 unsigned long *pwriteback)
1065{
1066}
1067
841710aa 1068#endif /* CONFIG_CGROUP_WRITEBACK */
52ebea74 1069
e1aab161 1070struct sock;
baac50bb
JW
1071bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1072void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
d886f4e4 1073#ifdef CONFIG_MEMCG
ef12947c
JW
1074extern struct static_key_false memcg_sockets_enabled_key;
1075#define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
2d758073
JW
1076void mem_cgroup_sk_alloc(struct sock *sk);
1077void mem_cgroup_sk_free(struct sock *sk);
baac50bb 1078static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
e805605c 1079{
0db15298 1080 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_pressure)
8e8ae645 1081 return true;
8e8ae645
JW
1082 do {
1083 if (time_before(jiffies, memcg->socket_pressure))
1084 return true;
1085 } while ((memcg = parent_mem_cgroup(memcg)));
1086 return false;
e805605c
JW
1087}
1088#else
80e95fe0 1089#define mem_cgroup_sockets_enabled 0
2d758073
JW
1090static inline void mem_cgroup_sk_alloc(struct sock *sk) { };
1091static inline void mem_cgroup_sk_free(struct sock *sk) { };
baac50bb 1092static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
e805605c
JW
1093{
1094 return false;
1095}
1096#endif
7ae1e1d0 1097
45264778
VD
1098struct kmem_cache *memcg_kmem_get_cache(struct kmem_cache *cachep);
1099void memcg_kmem_put_cache(struct kmem_cache *cachep);
1100int memcg_kmem_charge_memcg(struct page *page, gfp_t gfp, int order,
1101 struct mem_cgroup *memcg);
1102int memcg_kmem_charge(struct page *page, gfp_t gfp, int order);
1103void memcg_kmem_uncharge(struct page *page, int order);
1104
127424c8 1105#if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB)
ef12947c 1106extern struct static_key_false memcg_kmem_enabled_key;
17cc4dfe 1107extern struct workqueue_struct *memcg_kmem_cache_wq;
749c5415 1108
dbcf73e2 1109extern int memcg_nr_cache_ids;
64219994
MH
1110void memcg_get_cache_ids(void);
1111void memcg_put_cache_ids(void);
ebe945c2
GC
1112
1113/*
1114 * Helper macro to loop through all memcg-specific caches. Callers must still
1115 * check if the cache is valid (it is either valid or NULL).
1116 * the slab_mutex must be held when looping through those caches
1117 */
749c5415 1118#define for_each_memcg_cache_index(_idx) \
dbcf73e2 1119 for ((_idx) = 0; (_idx) < memcg_nr_cache_ids; (_idx)++)
749c5415 1120
7ae1e1d0
GC
1121static inline bool memcg_kmem_enabled(void)
1122{
ef12947c 1123 return static_branch_unlikely(&memcg_kmem_enabled_key);
7ae1e1d0
GC
1124}
1125
33398cf2 1126/*
9f706d68 1127 * helper for accessing a memcg's index. It will be used as an index in the
33398cf2
MH
1128 * child cache array in kmem_cache, and also to derive its name. This function
1129 * will return -1 when this is not a kmem-limited memcg.
1130 */
1131static inline int memcg_cache_id(struct mem_cgroup *memcg)
1132{
1133 return memcg ? memcg->kmemcg_id : -1;
1134}
5722d094 1135
7ae1e1d0 1136#else
749c5415
GC
1137#define for_each_memcg_cache_index(_idx) \
1138 for (; NULL; )
1139
b9ce5ef4
GC
1140static inline bool memcg_kmem_enabled(void)
1141{
1142 return false;
1143}
1144
2633d7a0
GC
1145static inline int memcg_cache_id(struct mem_cgroup *memcg)
1146{
1147 return -1;
1148}
1149
05257a1a
VD
1150static inline void memcg_get_cache_ids(void)
1151{
1152}
1153
1154static inline void memcg_put_cache_ids(void)
1155{
1156}
1157
127424c8
JW
1158#endif /* CONFIG_MEMCG && !CONFIG_SLOB */
1159
8cdea7c0 1160#endif /* _LINUX_MEMCONTROL_H */