Merge tag '9p-for-5.3' of git://github.com/martinetd/linux
[linux-block.git] / include / linux / memcontrol.h
CommitLineData
c942fddf 1/* SPDX-License-Identifier: GPL-2.0-or-later */
8cdea7c0
BS
2/* memcontrol.h - Memory Controller
3 *
4 * Copyright IBM Corporation, 2007
5 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
6 *
78fb7466
PE
7 * Copyright 2007 OpenVZ SWsoft Inc
8 * Author: Pavel Emelianov <xemul@openvz.org>
8cdea7c0
BS
9 */
10
11#ifndef _LINUX_MEMCONTROL_H
12#define _LINUX_MEMCONTROL_H
f8d66542 13#include <linux/cgroup.h>
456f998e 14#include <linux/vm_event_item.h>
7ae1e1d0 15#include <linux/hardirq.h>
a8964b9b 16#include <linux/jump_label.h>
33398cf2
MH
17#include <linux/page_counter.h>
18#include <linux/vmpressure.h>
19#include <linux/eventfd.h>
00f3ca2c
JW
20#include <linux/mm.h>
21#include <linux/vmstat.h>
33398cf2 22#include <linux/writeback.h>
fdf1cdb9 23#include <linux/page-flags.h>
456f998e 24
78fb7466 25struct mem_cgroup;
8697d331
BS
26struct page;
27struct mm_struct;
2633d7a0 28struct kmem_cache;
78fb7466 29
71cd3113
JW
30/* 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,
b2807f07 39 MEMCG_NR_STAT,
2a7106f2
GT
40};
41
e27be240
JW
42enum memcg_memory_event {
43 MEMCG_LOW,
71cd3113
JW
44 MEMCG_HIGH,
45 MEMCG_MAX,
46 MEMCG_OOM,
fe6bdfc8 47 MEMCG_OOM_KILL,
f3a53a3a
TH
48 MEMCG_SWAP_MAX,
49 MEMCG_SWAP_FAIL,
e27be240 50 MEMCG_NR_MEMORY_EVENTS,
71cd3113
JW
51};
52
bf8d5d52
RG
53enum mem_cgroup_protection {
54 MEMCG_PROT_NONE,
55 MEMCG_PROT_LOW,
56 MEMCG_PROT_MIN,
57};
58
5660048c 59struct mem_cgroup_reclaim_cookie {
ef8f2327 60 pg_data_t *pgdat;
5660048c
JW
61 int priority;
62 unsigned int generation;
63};
64
71cd3113
JW
65#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;
1c2d479a 72 refcount_t ref;
71cd3113
JW
73};
74
33398cf2
MH
75/*
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
871789d4
CD
88struct memcg_vmstats_percpu {
89 long stat[MEMCG_NR_STAT];
e27be240 90 unsigned long events[NR_VM_EVENT_ITEMS];
33398cf2
MH
91 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
00f3ca2c
JW
101struct lruvec_stat {
102 long count[NR_VM_NODE_STAT_ITEMS];
103};
104
0a4465d3
KT
105/*
106 * 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
33398cf2
MH
114/*
115 * per-zone information in memory controller.
116 */
ef8f2327 117struct mem_cgroup_per_node {
33398cf2 118 struct lruvec lruvec;
a983b5eb 119
815744d7
JW
120 /* Legacy local VM stats */
121 struct lruvec_stat __percpu *lruvec_stat_local;
122
123 /* Subtree VM stats (batched updates) */
a983b5eb
JW
124 struct lruvec_stat __percpu *lruvec_stat_cpu;
125 atomic_long_t lruvec_stat[NR_VM_NODE_STAT_ITEMS];
126
b4536f0c 127 unsigned long lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
33398cf2
MH
128
129 struct mem_cgroup_reclaim_iter iter[DEF_PRIORITY + 1];
130
0a4465d3
KT
131#ifdef CONFIG_MEMCG_KMEM
132 struct memcg_shrinker_map __rcu *shrinker_map;
133#endif
33398cf2
MH
134 struct rb_node tree_node; /* RB tree node */
135 unsigned long usage_in_excess;/* Set to the value by which */
136 /* the soft limit is exceeded*/
137 bool on_tree;
e3c1ac58
AR
138 bool congested; /* memcg has many dirty pages */
139 /* backed by a congested BDI */
140
33398cf2
MH
141 struct mem_cgroup *memcg; /* Back pointer, we cannot */
142 /* use container_of */
143};
144
33398cf2
MH
145struct mem_cgroup_threshold {
146 struct eventfd_ctx *eventfd;
147 unsigned long threshold;
148};
149
150/* For threshold */
151struct mem_cgroup_threshold_ary {
152 /* An array index points to threshold just below or equal to usage. */
153 int current_threshold;
154 /* Size of entries[] */
155 unsigned int size;
156 /* Array of thresholds */
157 struct mem_cgroup_threshold entries[0];
158};
159
160struct mem_cgroup_thresholds {
161 /* Primary thresholds array */
162 struct mem_cgroup_threshold_ary *primary;
163 /*
164 * Spare threshold array.
165 * This is needed to make mem_cgroup_unregister_event() "never fail".
166 * It must be able to store at least primary->size - 1 entries.
167 */
168 struct mem_cgroup_threshold_ary *spare;
169};
170
567e9ab2
JW
171enum memcg_kmem_state {
172 KMEM_NONE,
173 KMEM_ALLOCATED,
174 KMEM_ONLINE,
175};
176
e81bf979
AL
177#if defined(CONFIG_SMP)
178struct memcg_padding {
179 char x[0];
180} ____cacheline_internodealigned_in_smp;
181#define MEMCG_PADDING(name) struct memcg_padding name;
182#else
183#define MEMCG_PADDING(name)
184#endif
185
33398cf2
MH
186/*
187 * The memory controller data structure. The memory controller controls both
188 * page cache and RSS per cgroup. We would eventually like to provide
189 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
190 * to help the administrator determine what knobs to tune.
191 */
192struct mem_cgroup {
193 struct cgroup_subsys_state css;
194
73f576c0
JW
195 /* Private memcg ID. Used to ID objects that outlive the cgroup */
196 struct mem_cgroup_id id;
197
33398cf2
MH
198 /* Accounted resources */
199 struct page_counter memory;
37e84351 200 struct page_counter swap;
0db15298
JW
201
202 /* Legacy consumer-oriented counters */
33398cf2
MH
203 struct page_counter memsw;
204 struct page_counter kmem;
0db15298 205 struct page_counter tcpmem;
33398cf2 206
23067153 207 /* Upper bound of normal memory consumption range */
33398cf2
MH
208 unsigned long high;
209
f7e1cb6e
JW
210 /* Range enforcement for interrupt charges */
211 struct work_struct high_work;
212
33398cf2
MH
213 unsigned long soft_limit;
214
215 /* vmpressure notifications */
216 struct vmpressure vmpressure;
217
33398cf2
MH
218 /*
219 * Should the accounting and control be hierarchical, per subtree?
220 */
221 bool use_hierarchy;
222
3d8b38eb
RG
223 /*
224 * Should the OOM killer kill all belonging tasks, had it kill one?
225 */
226 bool oom_group;
227
33398cf2
MH
228 /* protected by memcg_oom_lock */
229 bool oom_lock;
230 int under_oom;
231
232 int swappiness;
233 /* OOM-Killer disable */
234 int oom_kill_disable;
235
1e577f97 236 /* memory.events and memory.events.local */
472912a2 237 struct cgroup_file events_file;
1e577f97 238 struct cgroup_file events_local_file;
472912a2 239
f3a53a3a
TH
240 /* handle for "memory.swap.events" */
241 struct cgroup_file swap_events_file;
242
33398cf2
MH
243 /* protect arrays of thresholds */
244 struct mutex thresholds_lock;
245
246 /* thresholds for memory usage. RCU-protected */
247 struct mem_cgroup_thresholds thresholds;
248
249 /* thresholds for mem+swap usage. RCU-protected */
250 struct mem_cgroup_thresholds memsw_thresholds;
251
252 /* For oom notifier event fd */
253 struct list_head oom_notify;
254
255 /*
256 * Should we move charges of a task when a task is moved into this
257 * mem_cgroup ? And what type of charges should we move ?
258 */
259 unsigned long move_charge_at_immigrate;
e81bf979
AL
260 /* taken only while moving_account > 0 */
261 spinlock_t move_lock;
262 unsigned long move_lock_flags;
263
264 MEMCG_PADDING(_pad1_);
265
33398cf2
MH
266 /*
267 * set > 0 if pages under this cgroup are moving to other cgroup.
268 */
269 atomic_t moving_account;
33398cf2 270 struct task_struct *move_lock_task;
a983b5eb 271
815744d7
JW
272 /* Legacy local VM stats and events */
273 struct memcg_vmstats_percpu __percpu *vmstats_local;
274
275 /* Subtree VM stats and events (batched updates) */
871789d4 276 struct memcg_vmstats_percpu __percpu *vmstats_percpu;
e81bf979
AL
277
278 MEMCG_PADDING(_pad2_);
279
871789d4
CD
280 atomic_long_t vmstats[MEMCG_NR_STAT];
281 atomic_long_t vmevents[NR_VM_EVENT_ITEMS];
42a30035 282
815744d7 283 /* memory.events */
42a30035 284 atomic_long_t memory_events[MEMCG_NR_MEMORY_EVENTS];
1e577f97 285 atomic_long_t memory_events_local[MEMCG_NR_MEMORY_EVENTS];
33398cf2 286
d886f4e4
JW
287 unsigned long socket_pressure;
288
289 /* Legacy tcp memory accounting */
0db15298
JW
290 bool tcpmem_active;
291 int tcpmem_pressure;
d886f4e4 292
84c07d11 293#ifdef CONFIG_MEMCG_KMEM
33398cf2
MH
294 /* Index in the kmem_cache->memcg_params.memcg_caches array */
295 int kmemcg_id;
567e9ab2 296 enum memcg_kmem_state kmem_state;
bc2791f8 297 struct list_head kmem_caches;
33398cf2
MH
298#endif
299
300 int last_scanned_node;
301#if MAX_NUMNODES > 1
302 nodemask_t scan_nodes;
303 atomic_t numainfo_events;
304 atomic_t numainfo_updating;
305#endif
306
307#ifdef CONFIG_CGROUP_WRITEBACK
308 struct list_head cgwb_list;
309 struct wb_domain cgwb_domain;
310#endif
311
312 /* List of events which userspace want to receive */
313 struct list_head event_list;
314 spinlock_t event_list_lock;
315
316 struct mem_cgroup_per_node *nodeinfo[0];
317 /* WARNING: nodeinfo must be the last member here */
318};
7d828602 319
a983b5eb
JW
320/*
321 * size of first charge trial. "32" comes from vmscan.c's magic value.
322 * TODO: maybe necessary to use big numbers in big irons.
323 */
324#define MEMCG_CHARGE_BATCH 32U
325
7d828602 326extern struct mem_cgroup *root_mem_cgroup;
56161634 327
dfd2f10c
KT
328static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
329{
330 return (memcg == root_mem_cgroup);
331}
332
23047a96
JW
333static inline bool mem_cgroup_disabled(void)
334{
335 return !cgroup_subsys_enabled(memory_cgrp_subsys);
336}
337
bf8d5d52
RG
338enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root,
339 struct mem_cgroup *memcg);
241994ed 340
00501b53 341int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
f627c2f5
KS
342 gfp_t gfp_mask, struct mem_cgroup **memcgp,
343 bool compound);
2cf85583
TH
344int mem_cgroup_try_charge_delay(struct page *page, struct mm_struct *mm,
345 gfp_t gfp_mask, struct mem_cgroup **memcgp,
346 bool compound);
00501b53 347void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
f627c2f5
KS
348 bool lrucare, bool compound);
349void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg,
350 bool compound);
0a31bc97 351void mem_cgroup_uncharge(struct page *page);
747db954 352void mem_cgroup_uncharge_list(struct list_head *page_list);
569b846d 353
6a93ca8f 354void mem_cgroup_migrate(struct page *oldpage, struct page *newpage);
569b846d 355
ef8f2327
MG
356static struct mem_cgroup_per_node *
357mem_cgroup_nodeinfo(struct mem_cgroup *memcg, int nid)
55779ec7 358{
ef8f2327 359 return memcg->nodeinfo[nid];
55779ec7
JW
360}
361
362/**
a9dd0a83
MG
363 * mem_cgroup_lruvec - get the lru list vector for a node or a memcg zone
364 * @node: node of the wanted lruvec
55779ec7
JW
365 * @memcg: memcg of the wanted lruvec
366 *
a9dd0a83
MG
367 * Returns the lru list vector holding pages for a given @node or a given
368 * @memcg and @zone. This can be the node lruvec, if the memory controller
55779ec7
JW
369 * is disabled.
370 */
a9dd0a83 371static inline struct lruvec *mem_cgroup_lruvec(struct pglist_data *pgdat,
ef8f2327 372 struct mem_cgroup *memcg)
55779ec7 373{
ef8f2327 374 struct mem_cgroup_per_node *mz;
55779ec7
JW
375 struct lruvec *lruvec;
376
377 if (mem_cgroup_disabled()) {
a9dd0a83 378 lruvec = node_lruvec(pgdat);
55779ec7
JW
379 goto out;
380 }
381
ef8f2327 382 mz = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
55779ec7
JW
383 lruvec = &mz->lruvec;
384out:
385 /*
386 * Since a node can be onlined after the mem_cgroup was created,
599d0c95 387 * we have to be prepared to initialize lruvec->pgdat here;
55779ec7
JW
388 * and if offlined then reonlined, we need to reinitialize it.
389 */
ef8f2327
MG
390 if (unlikely(lruvec->pgdat != pgdat))
391 lruvec->pgdat = pgdat;
55779ec7
JW
392 return lruvec;
393}
394
599d0c95 395struct lruvec *mem_cgroup_page_lruvec(struct page *, struct pglist_data *);
c9b0ed51 396
64219994 397struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
e993d905 398
d46eb14b
SB
399struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm);
400
f745c6f5
SB
401struct mem_cgroup *get_mem_cgroup_from_page(struct page *page);
402
33398cf2
MH
403static inline
404struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css){
405 return css ? container_of(css, struct mem_cgroup, css) : NULL;
406}
407
dc0b5864
RG
408static inline void mem_cgroup_put(struct mem_cgroup *memcg)
409{
d46eb14b
SB
410 if (memcg)
411 css_put(&memcg->css);
dc0b5864
RG
412}
413
8e8ae645
JW
414#define mem_cgroup_from_counter(counter, member) \
415 container_of(counter, struct mem_cgroup, member)
416
33398cf2
MH
417struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
418 struct mem_cgroup *,
419 struct mem_cgroup_reclaim_cookie *);
420void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
7c5f64f8
VD
421int mem_cgroup_scan_tasks(struct mem_cgroup *,
422 int (*)(struct task_struct *, void *), void *);
33398cf2 423
23047a96
JW
424static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
425{
426 if (mem_cgroup_disabled())
427 return 0;
428
73f576c0 429 return memcg->id.id;
23047a96 430}
73f576c0 431struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
23047a96 432
aa9694bb
CD
433static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
434{
435 return mem_cgroup_from_css(seq_css(m));
436}
437
2262185c
RG
438static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
439{
440 struct mem_cgroup_per_node *mz;
441
442 if (mem_cgroup_disabled())
443 return NULL;
444
445 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
446 return mz->memcg;
447}
448
8e8ae645
JW
449/**
450 * parent_mem_cgroup - find the accounting parent of a memcg
451 * @memcg: memcg whose parent to find
452 *
453 * Returns the parent memcg, or NULL if this is the root or the memory
454 * controller is in legacy no-hierarchy mode.
455 */
456static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
457{
458 if (!memcg->memory.parent)
459 return NULL;
460 return mem_cgroup_from_counter(memcg->memory.parent, memory);
461}
462
33398cf2
MH
463static inline bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
464 struct mem_cgroup *root)
465{
466 if (root == memcg)
467 return true;
468 if (!root->use_hierarchy)
469 return false;
470 return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
471}
e1aab161 472
2314b42d
JW
473static inline bool mm_match_cgroup(struct mm_struct *mm,
474 struct mem_cgroup *memcg)
2e4d4091 475{
587af308 476 struct mem_cgroup *task_memcg;
413918bb 477 bool match = false;
c3ac9a8a 478
2e4d4091 479 rcu_read_lock();
587af308 480 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
413918bb 481 if (task_memcg)
2314b42d 482 match = mem_cgroup_is_descendant(task_memcg, memcg);
2e4d4091 483 rcu_read_unlock();
c3ac9a8a 484 return match;
2e4d4091 485}
8a9f3ccd 486
64219994 487struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page);
2fc04524 488ino_t page_cgroup_ino(struct page *page);
d324236b 489
eb01aaab
VD
490static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
491{
492 if (mem_cgroup_disabled())
493 return true;
494 return !!(memcg->css.flags & CSS_ONLINE);
495}
496
58ae83db
KH
497/*
498 * For memory reclaim.
499 */
889976db 500int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
33398cf2
MH
501
502void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
b4536f0c 503 int zid, int nr_pages);
33398cf2 504
b4536f0c
MH
505static inline
506unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
507 enum lru_list lru, int zone_idx)
508{
509 struct mem_cgroup_per_node *mz;
510
511 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
512 return mz->lru_zone_size[zone_idx][lru];
33398cf2
MH
513}
514
b23afb93
TH
515void mem_cgroup_handle_over_high(void);
516
bbec2e15 517unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);
7c5f64f8 518
f0c867d9 519void mem_cgroup_print_oom_context(struct mem_cgroup *memcg,
64219994 520 struct task_struct *p);
58ae83db 521
f0c867d9 522void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg);
523
29ef680a 524static inline void mem_cgroup_enter_user_fault(void)
519e5247 525{
29ef680a
MH
526 WARN_ON(current->in_user_fault);
527 current->in_user_fault = 1;
519e5247
JW
528}
529
29ef680a 530static inline void mem_cgroup_exit_user_fault(void)
519e5247 531{
29ef680a
MH
532 WARN_ON(!current->in_user_fault);
533 current->in_user_fault = 0;
519e5247
JW
534}
535
3812c8c8
JW
536static inline bool task_in_memcg_oom(struct task_struct *p)
537{
626ebc41 538 return p->memcg_in_oom;
3812c8c8
JW
539}
540
49426420 541bool mem_cgroup_oom_synchronize(bool wait);
3d8b38eb
RG
542struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
543 struct mem_cgroup *oom_domain);
544void mem_cgroup_print_oom_group(struct mem_cgroup *memcg);
3812c8c8 545
c255a458 546#ifdef CONFIG_MEMCG_SWAP
c077719b
KH
547extern int do_swap_account;
548#endif
f8d66542 549
739f79fc
JW
550struct mem_cgroup *lock_page_memcg(struct page *page);
551void __unlock_page_memcg(struct mem_cgroup *memcg);
62cccb8c 552void unlock_page_memcg(struct page *page);
d7365e78 553
42a30035
JW
554/*
555 * idx can be of type enum memcg_stat_item or node_stat_item.
556 * Keep in sync with memcg_exact_page_state().
557 */
558static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
559{
560 long x = atomic_long_read(&memcg->vmstats[idx]);
561#ifdef CONFIG_SMP
562 if (x < 0)
563 x = 0;
564#endif
565 return x;
566}
567
0b3d6e6f
GT
568/*
569 * idx can be of type enum memcg_stat_item or node_stat_item.
570 * Keep in sync with memcg_exact_page_state().
571 */
205b20cc
JW
572static inline unsigned long memcg_page_state_local(struct mem_cgroup *memcg,
573 int idx)
2a2e4885 574{
815744d7
JW
575 long x = 0;
576 int cpu;
577
578 for_each_possible_cpu(cpu)
579 x += per_cpu(memcg->vmstats_local->stat[idx], cpu);
a983b5eb
JW
580#ifdef CONFIG_SMP
581 if (x < 0)
582 x = 0;
583#endif
584 return x;
2a2e4885
JW
585}
586
db9adbcb 587void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val);
2a2e4885 588
04fecbf5 589/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 590static inline void mod_memcg_state(struct mem_cgroup *memcg,
04fecbf5 591 int idx, int val)
2a2e4885 592{
c3cc3911
JW
593 unsigned long flags;
594
595 local_irq_save(flags);
a983b5eb 596 __mod_memcg_state(memcg, idx, val);
c3cc3911 597 local_irq_restore(flags);
2a2e4885
JW
598}
599
33398cf2 600/**
ccda7f43 601 * mod_memcg_page_state - update page state statistics
62cccb8c 602 * @page: the page
33398cf2
MH
603 * @idx: page state item to account
604 * @val: number of pages (positive or negative)
605 *
fdf1cdb9
JW
606 * The @page must be locked or the caller must use lock_page_memcg()
607 * to prevent double accounting when the page is concurrently being
608 * moved to another memcg:
81f8c3a4 609 *
fdf1cdb9 610 * lock_page(page) or lock_page_memcg(page)
81f8c3a4 611 * if (TestClearPageState(page))
ccda7f43 612 * mod_memcg_page_state(page, state, -1);
fdf1cdb9 613 * unlock_page(page) or unlock_page_memcg(page)
2a2e4885
JW
614 *
615 * Kernel pages are an exception to this, since they'll never move.
33398cf2 616 */
00f3ca2c 617static inline void __mod_memcg_page_state(struct page *page,
04fecbf5 618 int idx, int val)
00f3ca2c
JW
619{
620 if (page->mem_cgroup)
621 __mod_memcg_state(page->mem_cgroup, idx, val);
622}
623
ccda7f43 624static inline void mod_memcg_page_state(struct page *page,
04fecbf5 625 int idx, int val)
33398cf2 626{
62cccb8c 627 if (page->mem_cgroup)
ccda7f43 628 mod_memcg_state(page->mem_cgroup, idx, val);
33398cf2
MH
629}
630
42a30035
JW
631static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
632 enum node_stat_item idx)
633{
634 struct mem_cgroup_per_node *pn;
635 long x;
636
637 if (mem_cgroup_disabled())
638 return node_page_state(lruvec_pgdat(lruvec), idx);
639
640 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
641 x = atomic_long_read(&pn->lruvec_stat[idx]);
642#ifdef CONFIG_SMP
643 if (x < 0)
644 x = 0;
645#endif
646 return x;
647}
648
205b20cc
JW
649static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
650 enum node_stat_item idx)
2a7106f2 651{
00f3ca2c 652 struct mem_cgroup_per_node *pn;
815744d7
JW
653 long x = 0;
654 int cpu;
00f3ca2c
JW
655
656 if (mem_cgroup_disabled())
657 return node_page_state(lruvec_pgdat(lruvec), idx);
658
659 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
815744d7
JW
660 for_each_possible_cpu(cpu)
661 x += per_cpu(pn->lruvec_stat_local->count[idx], cpu);
a983b5eb
JW
662#ifdef CONFIG_SMP
663 if (x < 0)
664 x = 0;
665#endif
666 return x;
2a7106f2
GT
667}
668
db9adbcb
JW
669void __mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
670 int val);
00f3ca2c
JW
671
672static inline void mod_lruvec_state(struct lruvec *lruvec,
673 enum node_stat_item idx, int val)
674{
c3cc3911
JW
675 unsigned long flags;
676
677 local_irq_save(flags);
28454265 678 __mod_lruvec_state(lruvec, idx, val);
c3cc3911 679 local_irq_restore(flags);
00f3ca2c
JW
680}
681
682static inline void __mod_lruvec_page_state(struct page *page,
683 enum node_stat_item idx, int val)
684{
28454265
JW
685 pg_data_t *pgdat = page_pgdat(page);
686 struct lruvec *lruvec;
00f3ca2c 687
28454265
JW
688 /* Untracked pages have no memcg, no lruvec. Update only the node */
689 if (!page->mem_cgroup) {
690 __mod_node_page_state(pgdat, idx, val);
00f3ca2c 691 return;
28454265
JW
692 }
693
694 lruvec = mem_cgroup_lruvec(pgdat, page->mem_cgroup);
695 __mod_lruvec_state(lruvec, idx, val);
00f3ca2c
JW
696}
697
698static inline void mod_lruvec_page_state(struct page *page,
699 enum node_stat_item idx, int val)
700{
c3cc3911
JW
701 unsigned long flags;
702
703 local_irq_save(flags);
28454265 704 __mod_lruvec_page_state(page, idx, val);
c3cc3911 705 local_irq_restore(flags);
2a7106f2
GT
706}
707
ef8f2327 708unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
0608f43d
AM
709 gfp_t gfp_mask,
710 unsigned long *total_scanned);
a63d83f4 711
db9adbcb
JW
712void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
713 unsigned long count);
c9019e9b 714
2262185c 715static inline void count_memcg_events(struct mem_cgroup *memcg,
e27be240
JW
716 enum vm_event_item idx,
717 unsigned long count)
2262185c 718{
c3cc3911
JW
719 unsigned long flags;
720
721 local_irq_save(flags);
a983b5eb 722 __count_memcg_events(memcg, idx, count);
c3cc3911 723 local_irq_restore(flags);
2262185c
RG
724}
725
726static inline void count_memcg_page_event(struct page *page,
e27be240 727 enum vm_event_item idx)
2262185c
RG
728{
729 if (page->mem_cgroup)
730 count_memcg_events(page->mem_cgroup, idx, 1);
731}
732
733static inline void count_memcg_event_mm(struct mm_struct *mm,
734 enum vm_event_item idx)
68ae564b 735{
33398cf2
MH
736 struct mem_cgroup *memcg;
737
68ae564b
DR
738 if (mem_cgroup_disabled())
739 return;
33398cf2
MH
740
741 rcu_read_lock();
742 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
fe6bdfc8 743 if (likely(memcg))
c9019e9b 744 count_memcg_events(memcg, idx, 1);
33398cf2 745 rcu_read_unlock();
68ae564b 746}
c9019e9b 747
e27be240
JW
748static inline void memcg_memory_event(struct mem_cgroup *memcg,
749 enum memcg_memory_event event)
c9019e9b 750{
1e577f97
SB
751 atomic_long_inc(&memcg->memory_events_local[event]);
752 cgroup_file_notify(&memcg->events_local_file);
753
9852ae3f
CD
754 do {
755 atomic_long_inc(&memcg->memory_events[event]);
756 cgroup_file_notify(&memcg->events_file);
757
758 if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS)
759 break;
760 } while ((memcg = parent_mem_cgroup(memcg)) &&
761 !mem_cgroup_is_root(memcg));
c9019e9b
JW
762}
763
fe6bdfc8
RG
764static inline void memcg_memory_event_mm(struct mm_struct *mm,
765 enum memcg_memory_event event)
766{
767 struct mem_cgroup *memcg;
768
769 if (mem_cgroup_disabled())
770 return;
771
772 rcu_read_lock();
773 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
774 if (likely(memcg))
775 memcg_memory_event(memcg, event);
776 rcu_read_unlock();
777}
778
ca3e0214 779#ifdef CONFIG_TRANSPARENT_HUGEPAGE
e94c8a9c 780void mem_cgroup_split_huge_fixup(struct page *head);
ca3e0214
KH
781#endif
782
c255a458 783#else /* CONFIG_MEMCG */
23047a96
JW
784
785#define MEM_CGROUP_ID_SHIFT 0
786#define MEM_CGROUP_ID_MAX 0
787
7a81b88c
KH
788struct mem_cgroup;
789
dfd2f10c
KT
790static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
791{
792 return true;
793}
794
23047a96
JW
795static inline bool mem_cgroup_disabled(void)
796{
797 return true;
798}
799
e27be240
JW
800static inline void memcg_memory_event(struct mem_cgroup *memcg,
801 enum memcg_memory_event event)
241994ed
JW
802{
803}
804
fe6bdfc8
RG
805static inline void memcg_memory_event_mm(struct mm_struct *mm,
806 enum memcg_memory_event event)
807{
808}
809
bf8d5d52
RG
810static inline enum mem_cgroup_protection mem_cgroup_protected(
811 struct mem_cgroup *root, struct mem_cgroup *memcg)
241994ed 812{
bf8d5d52 813 return MEMCG_PROT_NONE;
241994ed
JW
814}
815
00501b53
JW
816static inline int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
817 gfp_t gfp_mask,
f627c2f5
KS
818 struct mem_cgroup **memcgp,
819 bool compound)
7a81b88c 820{
00501b53 821 *memcgp = NULL;
7a81b88c
KH
822 return 0;
823}
824
2cf85583
TH
825static inline int mem_cgroup_try_charge_delay(struct page *page,
826 struct mm_struct *mm,
827 gfp_t gfp_mask,
828 struct mem_cgroup **memcgp,
829 bool compound)
830{
831 *memcgp = NULL;
832 return 0;
833}
834
00501b53
JW
835static inline void mem_cgroup_commit_charge(struct page *page,
836 struct mem_cgroup *memcg,
f627c2f5 837 bool lrucare, bool compound)
7a81b88c
KH
838{
839}
840
00501b53 841static inline void mem_cgroup_cancel_charge(struct page *page,
f627c2f5
KS
842 struct mem_cgroup *memcg,
843 bool compound)
7a81b88c
KH
844{
845}
846
0a31bc97 847static inline void mem_cgroup_uncharge(struct page *page)
569b846d
KH
848{
849}
850
747db954 851static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
8a9f3ccd
BS
852{
853}
854
6a93ca8f 855static inline void mem_cgroup_migrate(struct page *old, struct page *new)
69029cd5
KH
856{
857}
858
a9dd0a83 859static inline struct lruvec *mem_cgroup_lruvec(struct pglist_data *pgdat,
ef8f2327 860 struct mem_cgroup *memcg)
08e552c6 861{
a9dd0a83 862 return node_lruvec(pgdat);
08e552c6
KH
863}
864
fa9add64 865static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
599d0c95 866 struct pglist_data *pgdat)
66e1707b 867{
599d0c95 868 return &pgdat->lruvec;
66e1707b
BS
869}
870
587af308 871static inline bool mm_match_cgroup(struct mm_struct *mm,
c0ff4b85 872 struct mem_cgroup *memcg)
bed7161a 873{
587af308 874 return true;
bed7161a
BS
875}
876
d46eb14b
SB
877static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
878{
879 return NULL;
880}
881
f745c6f5
SB
882static inline struct mem_cgroup *get_mem_cgroup_from_page(struct page *page)
883{
884 return NULL;
885}
886
dc0b5864
RG
887static inline void mem_cgroup_put(struct mem_cgroup *memcg)
888{
889}
890
5660048c
JW
891static inline struct mem_cgroup *
892mem_cgroup_iter(struct mem_cgroup *root,
893 struct mem_cgroup *prev,
894 struct mem_cgroup_reclaim_cookie *reclaim)
895{
896 return NULL;
897}
898
899static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
900 struct mem_cgroup *prev)
901{
902}
903
7c5f64f8
VD
904static inline int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
905 int (*fn)(struct task_struct *, void *), void *arg)
906{
907 return 0;
908}
909
23047a96 910static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
f8d66542 911{
23047a96
JW
912 return 0;
913}
914
915static inline struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
916{
917 WARN_ON_ONCE(id);
918 /* XXX: This should always return root_mem_cgroup */
919 return NULL;
f8d66542 920}
a636b327 921
aa9694bb
CD
922static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
923{
924 return NULL;
925}
926
2262185c
RG
927static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
928{
929 return NULL;
930}
931
eb01aaab 932static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
14797e23 933{
13308ca9 934 return true;
14797e23
KM
935}
936
b4536f0c
MH
937static inline
938unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec,
939 enum lru_list lru, int zone_idx)
940{
941 return 0;
942}
a3d8e054 943
bbec2e15 944static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
7c5f64f8
VD
945{
946 return 0;
947}
948
e222432b 949static inline void
f0c867d9 950mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
951{
952}
953
954static inline void
955mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
e222432b
BS
956{
957}
958
739f79fc
JW
959static inline struct mem_cgroup *lock_page_memcg(struct page *page)
960{
961 return NULL;
962}
963
964static inline void __unlock_page_memcg(struct mem_cgroup *memcg)
89c06bd5
KH
965{
966}
967
62cccb8c 968static inline void unlock_page_memcg(struct page *page)
89c06bd5
KH
969{
970}
971
b23afb93
TH
972static inline void mem_cgroup_handle_over_high(void)
973{
974}
975
29ef680a 976static inline void mem_cgroup_enter_user_fault(void)
519e5247
JW
977{
978}
979
29ef680a 980static inline void mem_cgroup_exit_user_fault(void)
519e5247
JW
981{
982}
983
3812c8c8
JW
984static inline bool task_in_memcg_oom(struct task_struct *p)
985{
986 return false;
987}
988
49426420 989static inline bool mem_cgroup_oom_synchronize(bool wait)
3812c8c8
JW
990{
991 return false;
992}
993
3d8b38eb
RG
994static inline struct mem_cgroup *mem_cgroup_get_oom_group(
995 struct task_struct *victim, struct mem_cgroup *oom_domain)
996{
997 return NULL;
998}
999
1000static inline void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
1001{
1002}
1003
42a30035
JW
1004static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
1005{
1006 return 0;
1007}
1008
205b20cc
JW
1009static inline unsigned long memcg_page_state_local(struct mem_cgroup *memcg,
1010 int idx)
2a2e4885
JW
1011{
1012 return 0;
1013}
1014
00f3ca2c 1015static inline void __mod_memcg_state(struct mem_cgroup *memcg,
04fecbf5 1016 int idx,
00f3ca2c 1017 int nr)
2a2e4885
JW
1018{
1019}
1020
00f3ca2c 1021static inline void mod_memcg_state(struct mem_cgroup *memcg,
04fecbf5 1022 int idx,
00f3ca2c 1023 int nr)
2a2e4885
JW
1024{
1025}
1026
00f3ca2c 1027static inline void __mod_memcg_page_state(struct page *page,
04fecbf5 1028 int idx,
00f3ca2c 1029 int nr)
2a2e4885
JW
1030{
1031}
1032
ccda7f43 1033static inline void mod_memcg_page_state(struct page *page,
04fecbf5 1034 int idx,
ccda7f43 1035 int nr)
553af430
JW
1036{
1037}
1038
42a30035
JW
1039static inline unsigned long lruvec_page_state(struct lruvec *lruvec,
1040 enum node_stat_item idx)
1041{
1042 return node_page_state(lruvec_pgdat(lruvec), idx);
1043}
1044
205b20cc
JW
1045static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1046 enum node_stat_item idx)
2a7106f2 1047{
00f3ca2c 1048 return node_page_state(lruvec_pgdat(lruvec), idx);
2a7106f2
GT
1049}
1050
00f3ca2c
JW
1051static inline void __mod_lruvec_state(struct lruvec *lruvec,
1052 enum node_stat_item idx, int val)
d69b042f 1053{
00f3ca2c
JW
1054 __mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
1055}
1056
1057static inline void mod_lruvec_state(struct lruvec *lruvec,
1058 enum node_stat_item idx, int val)
1059{
1060 mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
1061}
1062
1063static inline void __mod_lruvec_page_state(struct page *page,
1064 enum node_stat_item idx, int val)
1065{
1066 __mod_node_page_state(page_pgdat(page), idx, val);
1067}
1068
1069static inline void mod_lruvec_page_state(struct page *page,
1070 enum node_stat_item idx, int val)
1071{
1072 mod_node_page_state(page_pgdat(page), idx, val);
d69b042f
BS
1073}
1074
4e416953 1075static inline
ef8f2327 1076unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
0608f43d
AM
1077 gfp_t gfp_mask,
1078 unsigned long *total_scanned)
4e416953 1079{
0608f43d 1080 return 0;
4e416953
BS
1081}
1082
e94c8a9c 1083static inline void mem_cgroup_split_huge_fixup(struct page *head)
ca3e0214
KH
1084{
1085}
1086
2262185c
RG
1087static inline void count_memcg_events(struct mem_cgroup *memcg,
1088 enum vm_event_item idx,
1089 unsigned long count)
1090{
1091}
1092
9851ac13
KT
1093static inline void __count_memcg_events(struct mem_cgroup *memcg,
1094 enum vm_event_item idx,
1095 unsigned long count)
1096{
1097}
1098
2262185c 1099static inline void count_memcg_page_event(struct page *page,
04fecbf5 1100 int idx)
2262185c
RG
1101{
1102}
1103
456f998e 1104static inline
2262185c 1105void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
456f998e
YH
1106{
1107}
c255a458 1108#endif /* CONFIG_MEMCG */
78fb7466 1109
04fecbf5 1110/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 1111static inline void __inc_memcg_state(struct mem_cgroup *memcg,
04fecbf5 1112 int idx)
00f3ca2c
JW
1113{
1114 __mod_memcg_state(memcg, idx, 1);
1115}
1116
04fecbf5 1117/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 1118static inline void __dec_memcg_state(struct mem_cgroup *memcg,
04fecbf5 1119 int idx)
00f3ca2c
JW
1120{
1121 __mod_memcg_state(memcg, idx, -1);
1122}
1123
04fecbf5 1124/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 1125static inline void __inc_memcg_page_state(struct page *page,
04fecbf5 1126 int idx)
00f3ca2c
JW
1127{
1128 __mod_memcg_page_state(page, idx, 1);
1129}
1130
04fecbf5 1131/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 1132static inline void __dec_memcg_page_state(struct page *page,
04fecbf5 1133 int idx)
00f3ca2c
JW
1134{
1135 __mod_memcg_page_state(page, idx, -1);
1136}
1137
1138static inline void __inc_lruvec_state(struct lruvec *lruvec,
1139 enum node_stat_item idx)
1140{
1141 __mod_lruvec_state(lruvec, idx, 1);
1142}
1143
1144static inline void __dec_lruvec_state(struct lruvec *lruvec,
1145 enum node_stat_item idx)
1146{
1147 __mod_lruvec_state(lruvec, idx, -1);
1148}
1149
1150static inline void __inc_lruvec_page_state(struct page *page,
1151 enum node_stat_item idx)
1152{
1153 __mod_lruvec_page_state(page, idx, 1);
1154}
1155
1156static inline void __dec_lruvec_page_state(struct page *page,
1157 enum node_stat_item idx)
1158{
1159 __mod_lruvec_page_state(page, idx, -1);
1160}
1161
04fecbf5 1162/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 1163static inline void inc_memcg_state(struct mem_cgroup *memcg,
04fecbf5 1164 int idx)
00f3ca2c
JW
1165{
1166 mod_memcg_state(memcg, idx, 1);
1167}
1168
04fecbf5 1169/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 1170static inline void dec_memcg_state(struct mem_cgroup *memcg,
04fecbf5 1171 int idx)
00f3ca2c
JW
1172{
1173 mod_memcg_state(memcg, idx, -1);
1174}
1175
04fecbf5 1176/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 1177static inline void inc_memcg_page_state(struct page *page,
04fecbf5 1178 int idx)
00f3ca2c
JW
1179{
1180 mod_memcg_page_state(page, idx, 1);
1181}
1182
04fecbf5 1183/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 1184static inline void dec_memcg_page_state(struct page *page,
04fecbf5 1185 int idx)
00f3ca2c
JW
1186{
1187 mod_memcg_page_state(page, idx, -1);
1188}
1189
1190static inline void inc_lruvec_state(struct lruvec *lruvec,
1191 enum node_stat_item idx)
1192{
1193 mod_lruvec_state(lruvec, idx, 1);
1194}
1195
1196static inline void dec_lruvec_state(struct lruvec *lruvec,
1197 enum node_stat_item idx)
1198{
1199 mod_lruvec_state(lruvec, idx, -1);
1200}
1201
1202static inline void inc_lruvec_page_state(struct page *page,
1203 enum node_stat_item idx)
1204{
1205 mod_lruvec_page_state(page, idx, 1);
1206}
1207
1208static inline void dec_lruvec_page_state(struct page *page,
1209 enum node_stat_item idx)
1210{
1211 mod_lruvec_page_state(page, idx, -1);
1212}
1213
52ebea74 1214#ifdef CONFIG_CGROUP_WRITEBACK
841710aa 1215
841710aa 1216struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
c5edf9cd
TH
1217void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
1218 unsigned long *pheadroom, unsigned long *pdirty,
1219 unsigned long *pwriteback);
841710aa
TH
1220
1221#else /* CONFIG_CGROUP_WRITEBACK */
1222
1223static inline struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
1224{
1225 return NULL;
1226}
1227
c2aa723a 1228static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
c5edf9cd
TH
1229 unsigned long *pfilepages,
1230 unsigned long *pheadroom,
c2aa723a
TH
1231 unsigned long *pdirty,
1232 unsigned long *pwriteback)
1233{
1234}
1235
841710aa 1236#endif /* CONFIG_CGROUP_WRITEBACK */
52ebea74 1237
e1aab161 1238struct sock;
baac50bb
JW
1239bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
1240void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages);
d886f4e4 1241#ifdef CONFIG_MEMCG
ef12947c
JW
1242extern struct static_key_false memcg_sockets_enabled_key;
1243#define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
2d758073
JW
1244void mem_cgroup_sk_alloc(struct sock *sk);
1245void mem_cgroup_sk_free(struct sock *sk);
baac50bb 1246static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
e805605c 1247{
0db15298 1248 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_pressure)
8e8ae645 1249 return true;
8e8ae645
JW
1250 do {
1251 if (time_before(jiffies, memcg->socket_pressure))
1252 return true;
1253 } while ((memcg = parent_mem_cgroup(memcg)));
1254 return false;
e805605c
JW
1255}
1256#else
80e95fe0 1257#define mem_cgroup_sockets_enabled 0
2d758073
JW
1258static inline void mem_cgroup_sk_alloc(struct sock *sk) { };
1259static inline void mem_cgroup_sk_free(struct sock *sk) { };
baac50bb 1260static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
e805605c
JW
1261{
1262 return false;
1263}
1264#endif
7ae1e1d0 1265
45264778
VD
1266struct kmem_cache *memcg_kmem_get_cache(struct kmem_cache *cachep);
1267void memcg_kmem_put_cache(struct kmem_cache *cachep);
9b6f7e16
RG
1268
1269#ifdef CONFIG_MEMCG_KMEM
60cd4bcd
SB
1270int __memcg_kmem_charge(struct page *page, gfp_t gfp, int order);
1271void __memcg_kmem_uncharge(struct page *page, int order);
1272int __memcg_kmem_charge_memcg(struct page *page, gfp_t gfp, int order,
1273 struct mem_cgroup *memcg);
49a18eae
RG
1274void __memcg_kmem_uncharge_memcg(struct mem_cgroup *memcg,
1275 unsigned int nr_pages);
45264778 1276
ef12947c 1277extern struct static_key_false memcg_kmem_enabled_key;
17cc4dfe 1278extern struct workqueue_struct *memcg_kmem_cache_wq;
749c5415 1279
dbcf73e2 1280extern int memcg_nr_cache_ids;
64219994
MH
1281void memcg_get_cache_ids(void);
1282void memcg_put_cache_ids(void);
ebe945c2
GC
1283
1284/*
1285 * Helper macro to loop through all memcg-specific caches. Callers must still
1286 * check if the cache is valid (it is either valid or NULL).
1287 * the slab_mutex must be held when looping through those caches
1288 */
749c5415 1289#define for_each_memcg_cache_index(_idx) \
dbcf73e2 1290 for ((_idx) = 0; (_idx) < memcg_nr_cache_ids; (_idx)++)
749c5415 1291
7ae1e1d0
GC
1292static inline bool memcg_kmem_enabled(void)
1293{
ef12947c 1294 return static_branch_unlikely(&memcg_kmem_enabled_key);
7ae1e1d0
GC
1295}
1296
60cd4bcd
SB
1297static inline int memcg_kmem_charge(struct page *page, gfp_t gfp, int order)
1298{
1299 if (memcg_kmem_enabled())
1300 return __memcg_kmem_charge(page, gfp, order);
1301 return 0;
1302}
1303
1304static inline void memcg_kmem_uncharge(struct page *page, int order)
1305{
1306 if (memcg_kmem_enabled())
1307 __memcg_kmem_uncharge(page, order);
1308}
1309
1310static inline int memcg_kmem_charge_memcg(struct page *page, gfp_t gfp,
1311 int order, struct mem_cgroup *memcg)
1312{
1313 if (memcg_kmem_enabled())
1314 return __memcg_kmem_charge_memcg(page, gfp, order, memcg);
1315 return 0;
1316}
49a18eae
RG
1317
1318static inline void memcg_kmem_uncharge_memcg(struct page *page, int order,
1319 struct mem_cgroup *memcg)
1320{
1321 if (memcg_kmem_enabled())
1322 __memcg_kmem_uncharge_memcg(memcg, 1 << order);
1323}
1324
33398cf2 1325/*
9f706d68 1326 * helper for accessing a memcg's index. It will be used as an index in the
33398cf2
MH
1327 * child cache array in kmem_cache, and also to derive its name. This function
1328 * will return -1 when this is not a kmem-limited memcg.
1329 */
1330static inline int memcg_cache_id(struct mem_cgroup *memcg)
1331{
1332 return memcg ? memcg->kmemcg_id : -1;
1333}
5722d094 1334
0a4465d3
KT
1335extern int memcg_expand_shrinker_maps(int new_id);
1336
fae91d6d
KT
1337extern void memcg_set_shrinker_bit(struct mem_cgroup *memcg,
1338 int nid, int shrinker_id);
7ae1e1d0 1339#else
9b6f7e16
RG
1340
1341static inline int memcg_kmem_charge(struct page *page, gfp_t gfp, int order)
1342{
1343 return 0;
1344}
1345
1346static inline void memcg_kmem_uncharge(struct page *page, int order)
1347{
1348}
1349
60cd4bcd
SB
1350static inline int __memcg_kmem_charge(struct page *page, gfp_t gfp, int order)
1351{
1352 return 0;
1353}
1354
1355static inline void __memcg_kmem_uncharge(struct page *page, int order)
1356{
1357}
1358
749c5415
GC
1359#define for_each_memcg_cache_index(_idx) \
1360 for (; NULL; )
1361
b9ce5ef4
GC
1362static inline bool memcg_kmem_enabled(void)
1363{
1364 return false;
1365}
1366
2633d7a0
GC
1367static inline int memcg_cache_id(struct mem_cgroup *memcg)
1368{
1369 return -1;
1370}
1371
05257a1a
VD
1372static inline void memcg_get_cache_ids(void)
1373{
1374}
1375
1376static inline void memcg_put_cache_ids(void)
1377{
1378}
1379
fae91d6d
KT
1380static inline void memcg_set_shrinker_bit(struct mem_cgroup *memcg,
1381 int nid, int shrinker_id) { }
84c07d11 1382#endif /* CONFIG_MEMCG_KMEM */
127424c8 1383
8cdea7c0 1384#endif /* _LINUX_MEMCONTROL_H */