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