]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - include/linux/memcontrol.h
prctl_set_mm: downgrade mmap_sem to read lock
[thirdparty/kernel/stable.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
JW
119
120 struct lruvec_stat __percpu *lruvec_stat_cpu;
121 atomic_long_t lruvec_stat[NR_VM_NODE_STAT_ITEMS];
42a30035 122 atomic_long_t lruvec_stat_local[NR_VM_NODE_STAT_ITEMS];
a983b5eb 123
b4536f0c 124 unsigned long lru_zone_size[MAX_NR_ZONES][NR_LRU_LISTS];
33398cf2
MH
125
126 struct mem_cgroup_reclaim_iter iter[DEF_PRIORITY + 1];
127
0a4465d3
KT
128#ifdef CONFIG_MEMCG_KMEM
129 struct memcg_shrinker_map __rcu *shrinker_map;
130#endif
33398cf2
MH
131 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;
e3c1ac58
AR
135 bool congested; /* memcg has many dirty pages */
136 /* backed by a congested BDI */
137
33398cf2
MH
138 struct mem_cgroup *memcg; /* Back pointer, we cannot */
139 /* use container_of */
140};
141
33398cf2
MH
142struct 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
567e9ab2
JW
168enum memcg_kmem_state {
169 KMEM_NONE,
170 KMEM_ALLOCATED,
171 KMEM_ONLINE,
172};
173
e81bf979
AL
174#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
33398cf2
MH
183/*
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
73f576c0
JW
192 /* Private memcg ID. Used to ID objects that outlive the cgroup */
193 struct mem_cgroup_id id;
194
33398cf2
MH
195 /* Accounted resources */
196 struct page_counter memory;
37e84351 197 struct page_counter swap;
0db15298
JW
198
199 /* Legacy consumer-oriented counters */
33398cf2
MH
200 struct page_counter memsw;
201 struct page_counter kmem;
0db15298 202 struct page_counter tcpmem;
33398cf2 203
23067153 204 /* Upper bound of normal memory consumption range */
33398cf2
MH
205 unsigned long high;
206
f7e1cb6e
JW
207 /* Range enforcement for interrupt charges */
208 struct work_struct high_work;
209
33398cf2
MH
210 unsigned long soft_limit;
211
212 /* vmpressure notifications */
213 struct vmpressure vmpressure;
214
33398cf2
MH
215 /*
216 * Should the accounting and control be hierarchical, per subtree?
217 */
218 bool use_hierarchy;
219
3d8b38eb
RG
220 /*
221 * Should the OOM killer kill all belonging tasks, had it kill one?
222 */
223 bool oom_group;
224
33398cf2
MH
225 /* 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
e27be240 233 /* memory.events */
472912a2
TH
234 struct cgroup_file events_file;
235
f3a53a3a
TH
236 /* handle for "memory.swap.events" */
237 struct cgroup_file swap_events_file;
238
33398cf2
MH
239 /* 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;
e81bf979
AL
256 /* taken only while moving_account > 0 */
257 spinlock_t move_lock;
258 unsigned long move_lock_flags;
259
260 MEMCG_PADDING(_pad1_);
261
33398cf2
MH
262 /*
263 * set > 0 if pages under this cgroup are moving to other cgroup.
264 */
265 atomic_t moving_account;
33398cf2 266 struct task_struct *move_lock_task;
a983b5eb 267
e27be240 268 /* memory.stat */
871789d4 269 struct memcg_vmstats_percpu __percpu *vmstats_percpu;
e81bf979
AL
270
271 MEMCG_PADDING(_pad2_);
272
871789d4 273 atomic_long_t vmstats[MEMCG_NR_STAT];
42a30035
JW
274 atomic_long_t vmstats_local[MEMCG_NR_STAT];
275
871789d4 276 atomic_long_t vmevents[NR_VM_EVENT_ITEMS];
42a30035
JW
277 atomic_long_t vmevents_local[NR_VM_EVENT_ITEMS];
278
279 atomic_long_t memory_events[MEMCG_NR_MEMORY_EVENTS];
33398cf2 280
d886f4e4
JW
281 unsigned long socket_pressure;
282
283 /* Legacy tcp memory accounting */
0db15298
JW
284 bool tcpmem_active;
285 int tcpmem_pressure;
d886f4e4 286
84c07d11 287#ifdef CONFIG_MEMCG_KMEM
33398cf2
MH
288 /* Index in the kmem_cache->memcg_params.memcg_caches array */
289 int kmemcg_id;
567e9ab2 290 enum memcg_kmem_state kmem_state;
bc2791f8 291 struct list_head kmem_caches;
33398cf2
MH
292#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};
7d828602 313
a983b5eb
JW
314/*
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
7d828602 320extern struct mem_cgroup *root_mem_cgroup;
56161634 321
dfd2f10c
KT
322static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
323{
324 return (memcg == root_mem_cgroup);
325}
326
23047a96
JW
327static inline bool mem_cgroup_disabled(void)
328{
329 return !cgroup_subsys_enabled(memory_cgrp_subsys);
330}
331
bf8d5d52
RG
332enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root,
333 struct mem_cgroup *memcg);
241994ed 334
00501b53 335int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
f627c2f5
KS
336 gfp_t gfp_mask, struct mem_cgroup **memcgp,
337 bool compound);
2cf85583
TH
338int mem_cgroup_try_charge_delay(struct page *page, struct mm_struct *mm,
339 gfp_t gfp_mask, struct mem_cgroup **memcgp,
340 bool compound);
00501b53 341void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
f627c2f5
KS
342 bool lrucare, bool compound);
343void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg,
344 bool compound);
0a31bc97 345void mem_cgroup_uncharge(struct page *page);
747db954 346void mem_cgroup_uncharge_list(struct list_head *page_list);
569b846d 347
6a93ca8f 348void mem_cgroup_migrate(struct page *oldpage, struct page *newpage);
569b846d 349
ef8f2327
MG
350static struct mem_cgroup_per_node *
351mem_cgroup_nodeinfo(struct mem_cgroup *memcg, int nid)
55779ec7 352{
ef8f2327 353 return memcg->nodeinfo[nid];
55779ec7
JW
354}
355
356/**
a9dd0a83
MG
357 * mem_cgroup_lruvec - get the lru list vector for a node or a memcg zone
358 * @node: node of the wanted lruvec
55779ec7
JW
359 * @memcg: memcg of the wanted lruvec
360 *
a9dd0a83
MG
361 * 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
55779ec7
JW
363 * is disabled.
364 */
a9dd0a83 365static inline struct lruvec *mem_cgroup_lruvec(struct pglist_data *pgdat,
ef8f2327 366 struct mem_cgroup *memcg)
55779ec7 367{
ef8f2327 368 struct mem_cgroup_per_node *mz;
55779ec7
JW
369 struct lruvec *lruvec;
370
371 if (mem_cgroup_disabled()) {
a9dd0a83 372 lruvec = node_lruvec(pgdat);
55779ec7
JW
373 goto out;
374 }
375
ef8f2327 376 mz = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
55779ec7
JW
377 lruvec = &mz->lruvec;
378out:
379 /*
380 * Since a node can be onlined after the mem_cgroup was created,
599d0c95 381 * we have to be prepared to initialize lruvec->pgdat here;
55779ec7
JW
382 * and if offlined then reonlined, we need to reinitialize it.
383 */
ef8f2327
MG
384 if (unlikely(lruvec->pgdat != pgdat))
385 lruvec->pgdat = pgdat;
55779ec7
JW
386 return lruvec;
387}
388
599d0c95 389struct lruvec *mem_cgroup_page_lruvec(struct page *, struct pglist_data *);
c9b0ed51 390
2314b42d 391bool task_in_mem_cgroup(struct task_struct *task, struct mem_cgroup *memcg);
64219994 392struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
e993d905 393
d46eb14b
SB
394struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm);
395
f745c6f5
SB
396struct mem_cgroup *get_mem_cgroup_from_page(struct page *page);
397
33398cf2
MH
398static 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
dc0b5864
RG
403static inline void mem_cgroup_put(struct mem_cgroup *memcg)
404{
d46eb14b
SB
405 if (memcg)
406 css_put(&memcg->css);
dc0b5864
RG
407}
408
8e8ae645
JW
409#define mem_cgroup_from_counter(counter, member) \
410 container_of(counter, struct mem_cgroup, member)
411
33398cf2
MH
412struct 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 *);
7c5f64f8
VD
416int mem_cgroup_scan_tasks(struct mem_cgroup *,
417 int (*)(struct task_struct *, void *), void *);
33398cf2 418
23047a96
JW
419static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
420{
421 if (mem_cgroup_disabled())
422 return 0;
423
73f576c0 424 return memcg->id.id;
23047a96 425}
73f576c0 426struct mem_cgroup *mem_cgroup_from_id(unsigned short id);
23047a96 427
aa9694bb
CD
428static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
429{
430 return mem_cgroup_from_css(seq_css(m));
431}
432
2262185c
RG
433static 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
8e8ae645
JW
444/**
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
33398cf2
MH
458static 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}
e1aab161 467
2314b42d
JW
468static inline bool mm_match_cgroup(struct mm_struct *mm,
469 struct mem_cgroup *memcg)
2e4d4091 470{
587af308 471 struct mem_cgroup *task_memcg;
413918bb 472 bool match = false;
c3ac9a8a 473
2e4d4091 474 rcu_read_lock();
587af308 475 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
413918bb 476 if (task_memcg)
2314b42d 477 match = mem_cgroup_is_descendant(task_memcg, memcg);
2e4d4091 478 rcu_read_unlock();
c3ac9a8a 479 return match;
2e4d4091 480}
8a9f3ccd 481
64219994 482struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page);
2fc04524 483ino_t page_cgroup_ino(struct page *page);
d324236b 484
eb01aaab
VD
485static 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
58ae83db
KH
492/*
493 * For memory reclaim.
494 */
889976db 495int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
33398cf2
MH
496
497void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
b4536f0c 498 int zid, int nr_pages);
33398cf2 499
b4536f0c
MH
500static inline
501unsigned 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];
33398cf2
MH
508}
509
b23afb93
TH
510void mem_cgroup_handle_over_high(void);
511
bbec2e15 512unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg);
7c5f64f8 513
f0c867d9 514void mem_cgroup_print_oom_context(struct mem_cgroup *memcg,
64219994 515 struct task_struct *p);
58ae83db 516
f0c867d9 517void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg);
518
29ef680a 519static inline void mem_cgroup_enter_user_fault(void)
519e5247 520{
29ef680a
MH
521 WARN_ON(current->in_user_fault);
522 current->in_user_fault = 1;
519e5247
JW
523}
524
29ef680a 525static inline void mem_cgroup_exit_user_fault(void)
519e5247 526{
29ef680a
MH
527 WARN_ON(!current->in_user_fault);
528 current->in_user_fault = 0;
519e5247
JW
529}
530
3812c8c8
JW
531static inline bool task_in_memcg_oom(struct task_struct *p)
532{
626ebc41 533 return p->memcg_in_oom;
3812c8c8
JW
534}
535
49426420 536bool mem_cgroup_oom_synchronize(bool wait);
3d8b38eb
RG
537struct 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);
3812c8c8 540
c255a458 541#ifdef CONFIG_MEMCG_SWAP
c077719b
KH
542extern int do_swap_account;
543#endif
f8d66542 544
739f79fc
JW
545struct mem_cgroup *lock_page_memcg(struct page *page);
546void __unlock_page_memcg(struct mem_cgroup *memcg);
62cccb8c 547void unlock_page_memcg(struct page *page);
d7365e78 548
42a30035
JW
549/*
550 * idx can be of type enum memcg_stat_item or node_stat_item.
551 * Keep in sync with memcg_exact_page_state().
552 */
553static 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
0b3d6e6f
GT
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 */
205b20cc
JW
567static inline unsigned long memcg_page_state_local(struct mem_cgroup *memcg,
568 int idx)
2a2e4885 569{
42a30035 570 long x = atomic_long_read(&memcg->vmstats_local[idx]);
a983b5eb
JW
571#ifdef CONFIG_SMP
572 if (x < 0)
573 x = 0;
574#endif
575 return x;
2a2e4885
JW
576}
577
db9adbcb 578void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val);
2a2e4885 579
04fecbf5 580/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 581static inline void mod_memcg_state(struct mem_cgroup *memcg,
04fecbf5 582 int idx, int val)
2a2e4885 583{
c3cc3911
JW
584 unsigned long flags;
585
586 local_irq_save(flags);
a983b5eb 587 __mod_memcg_state(memcg, idx, val);
c3cc3911 588 local_irq_restore(flags);
2a2e4885
JW
589}
590
33398cf2 591/**
ccda7f43 592 * mod_memcg_page_state - update page state statistics
62cccb8c 593 * @page: the page
33398cf2
MH
594 * @idx: page state item to account
595 * @val: number of pages (positive or negative)
596 *
fdf1cdb9
JW
597 * 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:
81f8c3a4 600 *
fdf1cdb9 601 * lock_page(page) or lock_page_memcg(page)
81f8c3a4 602 * if (TestClearPageState(page))
ccda7f43 603 * mod_memcg_page_state(page, state, -1);
fdf1cdb9 604 * unlock_page(page) or unlock_page_memcg(page)
2a2e4885
JW
605 *
606 * Kernel pages are an exception to this, since they'll never move.
33398cf2 607 */
00f3ca2c 608static inline void __mod_memcg_page_state(struct page *page,
04fecbf5 609 int idx, int val)
00f3ca2c
JW
610{
611 if (page->mem_cgroup)
612 __mod_memcg_state(page->mem_cgroup, idx, val);
613}
614
ccda7f43 615static inline void mod_memcg_page_state(struct page *page,
04fecbf5 616 int idx, int val)
33398cf2 617{
62cccb8c 618 if (page->mem_cgroup)
ccda7f43 619 mod_memcg_state(page->mem_cgroup, idx, val);
33398cf2
MH
620}
621
42a30035
JW
622static 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
205b20cc
JW
640static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
641 enum node_stat_item idx)
2a7106f2 642{
00f3ca2c 643 struct mem_cgroup_per_node *pn;
a983b5eb 644 long x;
00f3ca2c
JW
645
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);
42a30035 650 x = atomic_long_read(&pn->lruvec_stat_local[idx]);
a983b5eb
JW
651#ifdef CONFIG_SMP
652 if (x < 0)
653 x = 0;
654#endif
655 return x;
2a7106f2
GT
656}
657
db9adbcb
JW
658void __mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
659 int val);
00f3ca2c
JW
660
661static inline void mod_lruvec_state(struct lruvec *lruvec,
662 enum node_stat_item idx, int val)
663{
c3cc3911
JW
664 unsigned long flags;
665
666 local_irq_save(flags);
28454265 667 __mod_lruvec_state(lruvec, idx, val);
c3cc3911 668 local_irq_restore(flags);
00f3ca2c
JW
669}
670
671static inline void __mod_lruvec_page_state(struct page *page,
672 enum node_stat_item idx, int val)
673{
28454265
JW
674 pg_data_t *pgdat = page_pgdat(page);
675 struct lruvec *lruvec;
00f3ca2c 676
28454265
JW
677 /* Untracked pages have no memcg, no lruvec. Update only the node */
678 if (!page->mem_cgroup) {
679 __mod_node_page_state(pgdat, idx, val);
00f3ca2c 680 return;
28454265
JW
681 }
682
683 lruvec = mem_cgroup_lruvec(pgdat, page->mem_cgroup);
684 __mod_lruvec_state(lruvec, idx, val);
00f3ca2c
JW
685}
686
687static inline void mod_lruvec_page_state(struct page *page,
688 enum node_stat_item idx, int val)
689{
c3cc3911
JW
690 unsigned long flags;
691
692 local_irq_save(flags);
28454265 693 __mod_lruvec_page_state(page, idx, val);
c3cc3911 694 local_irq_restore(flags);
2a7106f2
GT
695}
696
ef8f2327 697unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
0608f43d
AM
698 gfp_t gfp_mask,
699 unsigned long *total_scanned);
a63d83f4 700
db9adbcb
JW
701void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
702 unsigned long count);
c9019e9b 703
2262185c 704static inline void count_memcg_events(struct mem_cgroup *memcg,
e27be240
JW
705 enum vm_event_item idx,
706 unsigned long count)
2262185c 707{
c3cc3911
JW
708 unsigned long flags;
709
710 local_irq_save(flags);
a983b5eb 711 __count_memcg_events(memcg, idx, count);
c3cc3911 712 local_irq_restore(flags);
2262185c
RG
713}
714
715static inline void count_memcg_page_event(struct page *page,
e27be240 716 enum vm_event_item idx)
2262185c
RG
717{
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)
68ae564b 724{
33398cf2
MH
725 struct mem_cgroup *memcg;
726
68ae564b
DR
727 if (mem_cgroup_disabled())
728 return;
33398cf2
MH
729
730 rcu_read_lock();
731 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
fe6bdfc8 732 if (likely(memcg))
c9019e9b 733 count_memcg_events(memcg, idx, 1);
33398cf2 734 rcu_read_unlock();
68ae564b 735}
c9019e9b 736
e27be240
JW
737static inline void memcg_memory_event(struct mem_cgroup *memcg,
738 enum memcg_memory_event event)
c9019e9b 739{
e27be240 740 atomic_long_inc(&memcg->memory_events[event]);
c9019e9b
JW
741 cgroup_file_notify(&memcg->events_file);
742}
743
fe6bdfc8
RG
744static 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
ca3e0214 759#ifdef CONFIG_TRANSPARENT_HUGEPAGE
e94c8a9c 760void mem_cgroup_split_huge_fixup(struct page *head);
ca3e0214
KH
761#endif
762
c255a458 763#else /* CONFIG_MEMCG */
23047a96
JW
764
765#define MEM_CGROUP_ID_SHIFT 0
766#define MEM_CGROUP_ID_MAX 0
767
7a81b88c
KH
768struct mem_cgroup;
769
dfd2f10c
KT
770static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
771{
772 return true;
773}
774
23047a96
JW
775static inline bool mem_cgroup_disabled(void)
776{
777 return true;
778}
779
e27be240
JW
780static inline void memcg_memory_event(struct mem_cgroup *memcg,
781 enum memcg_memory_event event)
241994ed
JW
782{
783}
784
fe6bdfc8
RG
785static inline void memcg_memory_event_mm(struct mm_struct *mm,
786 enum memcg_memory_event event)
787{
788}
789
bf8d5d52
RG
790static inline enum mem_cgroup_protection mem_cgroup_protected(
791 struct mem_cgroup *root, struct mem_cgroup *memcg)
241994ed 792{
bf8d5d52 793 return MEMCG_PROT_NONE;
241994ed
JW
794}
795
00501b53
JW
796static inline int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
797 gfp_t gfp_mask,
f627c2f5
KS
798 struct mem_cgroup **memcgp,
799 bool compound)
7a81b88c 800{
00501b53 801 *memcgp = NULL;
7a81b88c
KH
802 return 0;
803}
804
2cf85583
TH
805static 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
00501b53
JW
815static inline void mem_cgroup_commit_charge(struct page *page,
816 struct mem_cgroup *memcg,
f627c2f5 817 bool lrucare, bool compound)
7a81b88c
KH
818{
819}
820
00501b53 821static inline void mem_cgroup_cancel_charge(struct page *page,
f627c2f5
KS
822 struct mem_cgroup *memcg,
823 bool compound)
7a81b88c
KH
824{
825}
826
0a31bc97 827static inline void mem_cgroup_uncharge(struct page *page)
569b846d
KH
828{
829}
830
747db954 831static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
8a9f3ccd
BS
832{
833}
834
6a93ca8f 835static inline void mem_cgroup_migrate(struct page *old, struct page *new)
69029cd5
KH
836{
837}
838
a9dd0a83 839static inline struct lruvec *mem_cgroup_lruvec(struct pglist_data *pgdat,
ef8f2327 840 struct mem_cgroup *memcg)
08e552c6 841{
a9dd0a83 842 return node_lruvec(pgdat);
08e552c6
KH
843}
844
fa9add64 845static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
599d0c95 846 struct pglist_data *pgdat)
66e1707b 847{
599d0c95 848 return &pgdat->lruvec;
66e1707b
BS
849}
850
587af308 851static inline bool mm_match_cgroup(struct mm_struct *mm,
c0ff4b85 852 struct mem_cgroup *memcg)
bed7161a 853{
587af308 854 return true;
bed7161a
BS
855}
856
ffbdccf5
DR
857static inline bool task_in_mem_cgroup(struct task_struct *task,
858 const struct mem_cgroup *memcg)
4c4a2214 859{
ffbdccf5 860 return true;
4c4a2214
DR
861}
862
d46eb14b
SB
863static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
864{
865 return NULL;
866}
867
f745c6f5
SB
868static inline struct mem_cgroup *get_mem_cgroup_from_page(struct page *page)
869{
870 return NULL;
871}
872
dc0b5864
RG
873static inline void mem_cgroup_put(struct mem_cgroup *memcg)
874{
875}
876
5660048c
JW
877static 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
7c5f64f8
VD
890static 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
23047a96 896static inline unsigned short mem_cgroup_id(struct mem_cgroup *memcg)
f8d66542 897{
23047a96
JW
898 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;
f8d66542 906}
a636b327 907
aa9694bb
CD
908static inline struct mem_cgroup *mem_cgroup_from_seq(struct seq_file *m)
909{
910 return NULL;
911}
912
2262185c
RG
913static inline struct mem_cgroup *lruvec_memcg(struct lruvec *lruvec)
914{
915 return NULL;
916}
917
eb01aaab 918static inline bool mem_cgroup_online(struct mem_cgroup *memcg)
14797e23 919{
13308ca9 920 return true;
14797e23
KM
921}
922
b4536f0c
MH
923static 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}
a3d8e054 929
bbec2e15 930static inline unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
7c5f64f8
VD
931{
932 return 0;
933}
934
e222432b 935static inline void
f0c867d9 936mem_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)
e222432b
BS
942{
943}
944
739f79fc
JW
945static 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)
89c06bd5
KH
951{
952}
953
62cccb8c 954static inline void unlock_page_memcg(struct page *page)
89c06bd5
KH
955{
956}
957
b23afb93
TH
958static inline void mem_cgroup_handle_over_high(void)
959{
960}
961
29ef680a 962static inline void mem_cgroup_enter_user_fault(void)
519e5247
JW
963{
964}
965
29ef680a 966static inline void mem_cgroup_exit_user_fault(void)
519e5247
JW
967{
968}
969
3812c8c8
JW
970static inline bool task_in_memcg_oom(struct task_struct *p)
971{
972 return false;
973}
974
49426420 975static inline bool mem_cgroup_oom_synchronize(bool wait)
3812c8c8
JW
976{
977 return false;
978}
979
3d8b38eb
RG
980static 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
42a30035
JW
990static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
991{
992 return 0;
993}
994
205b20cc
JW
995static inline unsigned long memcg_page_state_local(struct mem_cgroup *memcg,
996 int idx)
2a2e4885
JW
997{
998 return 0;
999}
1000
00f3ca2c 1001static inline void __mod_memcg_state(struct mem_cgroup *memcg,
04fecbf5 1002 int idx,
00f3ca2c 1003 int nr)
2a2e4885
JW
1004{
1005}
1006
00f3ca2c 1007static inline void mod_memcg_state(struct mem_cgroup *memcg,
04fecbf5 1008 int idx,
00f3ca2c 1009 int nr)
2a2e4885
JW
1010{
1011}
1012
00f3ca2c 1013static inline void __mod_memcg_page_state(struct page *page,
04fecbf5 1014 int idx,
00f3ca2c 1015 int nr)
2a2e4885
JW
1016{
1017}
1018
ccda7f43 1019static inline void mod_memcg_page_state(struct page *page,
04fecbf5 1020 int idx,
ccda7f43 1021 int nr)
553af430
JW
1022{
1023}
1024
42a30035
JW
1025static 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
205b20cc
JW
1031static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec,
1032 enum node_stat_item idx)
2a7106f2 1033{
00f3ca2c 1034 return node_page_state(lruvec_pgdat(lruvec), idx);
2a7106f2
GT
1035}
1036
00f3ca2c
JW
1037static inline void __mod_lruvec_state(struct lruvec *lruvec,
1038 enum node_stat_item idx, int val)
d69b042f 1039{
00f3ca2c
JW
1040 __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);
d69b042f
BS
1059}
1060
4e416953 1061static inline
ef8f2327 1062unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
0608f43d
AM
1063 gfp_t gfp_mask,
1064 unsigned long *total_scanned)
4e416953 1065{
0608f43d 1066 return 0;
4e416953
BS
1067}
1068
e94c8a9c 1069static inline void mem_cgroup_split_huge_fixup(struct page *head)
ca3e0214
KH
1070{
1071}
1072
2262185c
RG
1073static inline void count_memcg_events(struct mem_cgroup *memcg,
1074 enum vm_event_item idx,
1075 unsigned long count)
1076{
1077}
1078
9851ac13
KT
1079static inline void __count_memcg_events(struct mem_cgroup *memcg,
1080 enum vm_event_item idx,
1081 unsigned long count)
1082{
1083}
1084
2262185c 1085static inline void count_memcg_page_event(struct page *page,
04fecbf5 1086 int idx)
2262185c
RG
1087{
1088}
1089
456f998e 1090static inline
2262185c 1091void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx)
456f998e
YH
1092{
1093}
c255a458 1094#endif /* CONFIG_MEMCG */
78fb7466 1095
04fecbf5 1096/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 1097static inline void __inc_memcg_state(struct mem_cgroup *memcg,
04fecbf5 1098 int idx)
00f3ca2c
JW
1099{
1100 __mod_memcg_state(memcg, idx, 1);
1101}
1102
04fecbf5 1103/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 1104static inline void __dec_memcg_state(struct mem_cgroup *memcg,
04fecbf5 1105 int idx)
00f3ca2c
JW
1106{
1107 __mod_memcg_state(memcg, idx, -1);
1108}
1109
04fecbf5 1110/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 1111static inline void __inc_memcg_page_state(struct page *page,
04fecbf5 1112 int idx)
00f3ca2c
JW
1113{
1114 __mod_memcg_page_state(page, 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_page_state(struct page *page,
04fecbf5 1119 int idx)
00f3ca2c
JW
1120{
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
04fecbf5 1148/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 1149static inline void inc_memcg_state(struct mem_cgroup *memcg,
04fecbf5 1150 int idx)
00f3ca2c
JW
1151{
1152 mod_memcg_state(memcg, idx, 1);
1153}
1154
04fecbf5 1155/* idx can be of type enum memcg_stat_item or node_stat_item */
00f3ca2c 1156static inline void dec_memcg_state(struct mem_cgroup *memcg,
04fecbf5 1157 int idx)
00f3ca2c
JW
1158{
1159 mod_memcg_state(memcg, 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_page_state(struct page *page,
04fecbf5 1164 int idx)
00f3ca2c
JW
1165{
1166 mod_memcg_page_state(page, 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_page_state(struct page *page,
04fecbf5 1171 int idx)
00f3ca2c
JW
1172{
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
52ebea74 1200#ifdef CONFIG_CGROUP_WRITEBACK
841710aa 1201
841710aa 1202struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb);
c5edf9cd
TH
1203void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
1204 unsigned long *pheadroom, unsigned long *pdirty,
1205 unsigned long *pwriteback);
841710aa
TH
1206
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
c2aa723a 1214static inline void mem_cgroup_wb_stats(struct bdi_writeback *wb,
c5edf9cd
TH
1215 unsigned long *pfilepages,
1216 unsigned long *pheadroom,
c2aa723a
TH
1217 unsigned long *pdirty,
1218 unsigned long *pwriteback)
1219{
1220}
1221
841710aa 1222#endif /* CONFIG_CGROUP_WRITEBACK */
52ebea74 1223
e1aab161 1224struct sock;
baac50bb
JW
1225bool 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);
d886f4e4 1227#ifdef CONFIG_MEMCG
ef12947c
JW
1228extern struct static_key_false memcg_sockets_enabled_key;
1229#define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key)
2d758073
JW
1230void mem_cgroup_sk_alloc(struct sock *sk);
1231void mem_cgroup_sk_free(struct sock *sk);
baac50bb 1232static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
e805605c 1233{
0db15298 1234 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_pressure)
8e8ae645 1235 return true;
8e8ae645
JW
1236 do {
1237 if (time_before(jiffies, memcg->socket_pressure))
1238 return true;
1239 } while ((memcg = parent_mem_cgroup(memcg)));
1240 return false;
e805605c
JW
1241}
1242#else
80e95fe0 1243#define mem_cgroup_sockets_enabled 0
2d758073
JW
1244static inline void mem_cgroup_sk_alloc(struct sock *sk) { };
1245static inline void mem_cgroup_sk_free(struct sock *sk) { };
baac50bb 1246static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
e805605c
JW
1247{
1248 return false;
1249}
1250#endif
7ae1e1d0 1251
45264778
VD
1252struct kmem_cache *memcg_kmem_get_cache(struct kmem_cache *cachep);
1253void memcg_kmem_put_cache(struct kmem_cache *cachep);
9b6f7e16
RG
1254
1255#ifdef CONFIG_MEMCG_KMEM
60cd4bcd
SB
1256int __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);
45264778 1260
ef12947c 1261extern struct static_key_false memcg_kmem_enabled_key;
17cc4dfe 1262extern struct workqueue_struct *memcg_kmem_cache_wq;
749c5415 1263
dbcf73e2 1264extern int memcg_nr_cache_ids;
64219994
MH
1265void memcg_get_cache_ids(void);
1266void memcg_put_cache_ids(void);
ebe945c2
GC
1267
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 */
749c5415 1273#define for_each_memcg_cache_index(_idx) \
dbcf73e2 1274 for ((_idx) = 0; (_idx) < memcg_nr_cache_ids; (_idx)++)
749c5415 1275
7ae1e1d0
GC
1276static inline bool memcg_kmem_enabled(void)
1277{
ef12947c 1278 return static_branch_unlikely(&memcg_kmem_enabled_key);
7ae1e1d0
GC
1279}
1280
60cd4bcd
SB
1281static 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}
33398cf2 1301/*
9f706d68 1302 * helper for accessing a memcg's index. It will be used as an index in the
33398cf2
MH
1303 * 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}
5722d094 1310
0a4465d3
KT
1311extern int memcg_expand_shrinker_maps(int new_id);
1312
fae91d6d
KT
1313extern void memcg_set_shrinker_bit(struct mem_cgroup *memcg,
1314 int nid, int shrinker_id);
7ae1e1d0 1315#else
9b6f7e16
RG
1316
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
60cd4bcd
SB
1326static 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
749c5415
GC
1335#define for_each_memcg_cache_index(_idx) \
1336 for (; NULL; )
1337
b9ce5ef4
GC
1338static inline bool memcg_kmem_enabled(void)
1339{
1340 return false;
1341}
1342
2633d7a0
GC
1343static inline int memcg_cache_id(struct mem_cgroup *memcg)
1344{
1345 return -1;
1346}
1347
05257a1a
VD
1348static inline void memcg_get_cache_ids(void)
1349{
1350}
1351
1352static inline void memcg_put_cache_ids(void)
1353{
1354}
1355
fae91d6d
KT
1356static inline void memcg_set_shrinker_bit(struct mem_cgroup *memcg,
1357 int nid, int shrinker_id) { }
84c07d11 1358#endif /* CONFIG_MEMCG_KMEM */
127424c8 1359
8cdea7c0 1360#endif /* _LINUX_MEMCONTROL_H */