]>
Commit | Line | Data |
---|---|---|
c942fddf | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
8cdea7c0 BS |
2 | /* memcontrol.c - 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> | |
9 | * | |
2e72b634 KS |
10 | * Memory thresholds |
11 | * Copyright (C) 2009 Nokia Corporation | |
12 | * Author: Kirill A. Shutemov | |
13 | * | |
7ae1e1d0 GC |
14 | * Kernel Memory Controller |
15 | * Copyright (C) 2012 Parallels Inc. and Google Inc. | |
16 | * Authors: Glauber Costa and Suleiman Souhlal | |
17 | * | |
1575e68b JW |
18 | * Native page reclaim |
19 | * Charge lifetime sanitation | |
20 | * Lockless page tracking & accounting | |
21 | * Unified hierarchy configuration model | |
22 | * Copyright (C) 2015 Red Hat, Inc., Johannes Weiner | |
6168d0da AS |
23 | * |
24 | * Per memcg lru locking | |
25 | * Copyright (C) 2020 Alibaba, Inc, Alex Shi | |
8cdea7c0 BS |
26 | */ |
27 | ||
3e32cb2e | 28 | #include <linux/page_counter.h> |
8cdea7c0 BS |
29 | #include <linux/memcontrol.h> |
30 | #include <linux/cgroup.h> | |
a520110e | 31 | #include <linux/pagewalk.h> |
6e84f315 | 32 | #include <linux/sched/mm.h> |
3a4f8a0b | 33 | #include <linux/shmem_fs.h> |
4ffef5fe | 34 | #include <linux/hugetlb.h> |
d13d1443 | 35 | #include <linux/pagemap.h> |
1ff9e6e1 | 36 | #include <linux/vm_event_item.h> |
d52aa412 | 37 | #include <linux/smp.h> |
8a9f3ccd | 38 | #include <linux/page-flags.h> |
66e1707b | 39 | #include <linux/backing-dev.h> |
8a9f3ccd BS |
40 | #include <linux/bit_spinlock.h> |
41 | #include <linux/rcupdate.h> | |
e222432b | 42 | #include <linux/limits.h> |
b9e15baf | 43 | #include <linux/export.h> |
8c7c6e34 | 44 | #include <linux/mutex.h> |
bb4cc1a8 | 45 | #include <linux/rbtree.h> |
b6ac57d5 | 46 | #include <linux/slab.h> |
66e1707b | 47 | #include <linux/swap.h> |
02491447 | 48 | #include <linux/swapops.h> |
66e1707b | 49 | #include <linux/spinlock.h> |
2e72b634 | 50 | #include <linux/eventfd.h> |
79bd9814 | 51 | #include <linux/poll.h> |
2e72b634 | 52 | #include <linux/sort.h> |
66e1707b | 53 | #include <linux/fs.h> |
d2ceb9b7 | 54 | #include <linux/seq_file.h> |
70ddf637 | 55 | #include <linux/vmpressure.h> |
dc90f084 | 56 | #include <linux/memremap.h> |
b69408e8 | 57 | #include <linux/mm_inline.h> |
5d1ea48b | 58 | #include <linux/swap_cgroup.h> |
cdec2e42 | 59 | #include <linux/cpu.h> |
158e0a2d | 60 | #include <linux/oom.h> |
0056f4e6 | 61 | #include <linux/lockdep.h> |
79bd9814 | 62 | #include <linux/file.h> |
03248add | 63 | #include <linux/resume_user_mode.h> |
0e4b01df | 64 | #include <linux/psi.h> |
c8713d0b | 65 | #include <linux/seq_buf.h> |
6a792697 | 66 | #include <linux/sched/isolation.h> |
08e552c6 | 67 | #include "internal.h" |
d1a4c0b3 | 68 | #include <net/sock.h> |
4bd2c1ee | 69 | #include <net/ip.h> |
f35c3a8e | 70 | #include "slab.h" |
014bb1de | 71 | #include "swap.h" |
8cdea7c0 | 72 | |
7c0f6ba6 | 73 | #include <linux/uaccess.h> |
8697d331 | 74 | |
cc8e970c KM |
75 | #include <trace/events/vmscan.h> |
76 | ||
073219e9 TH |
77 | struct cgroup_subsys memory_cgrp_subsys __read_mostly; |
78 | EXPORT_SYMBOL(memory_cgrp_subsys); | |
68ae564b | 79 | |
7d828602 JW |
80 | struct mem_cgroup *root_mem_cgroup __read_mostly; |
81 | ||
37d5985c RG |
82 | /* Active memory cgroup to use from an interrupt context */ |
83 | DEFINE_PER_CPU(struct mem_cgroup *, int_active_memcg); | |
c74d40e8 | 84 | EXPORT_PER_CPU_SYMBOL_GPL(int_active_memcg); |
37d5985c | 85 | |
f7e1cb6e | 86 | /* Socket memory accounting disabled? */ |
0f0cace3 | 87 | static bool cgroup_memory_nosocket __ro_after_init; |
f7e1cb6e | 88 | |
04823c83 | 89 | /* Kernel memory accounting disabled? */ |
17c17367 | 90 | static bool cgroup_memory_nokmem __ro_after_init; |
04823c83 | 91 | |
b6c1a8af YS |
92 | /* BPF memory accounting disabled? */ |
93 | static bool cgroup_memory_nobpf __ro_after_init; | |
94 | ||
97b27821 TH |
95 | #ifdef CONFIG_CGROUP_WRITEBACK |
96 | static DECLARE_WAIT_QUEUE_HEAD(memcg_cgwb_frn_waitq); | |
97 | #endif | |
98 | ||
7941d214 JW |
99 | /* Whether legacy memory+swap accounting is active */ |
100 | static bool do_memsw_account(void) | |
101 | { | |
b25806dc | 102 | return !cgroup_subsys_on_dfl(memory_cgrp_subsys); |
7941d214 JW |
103 | } |
104 | ||
a0db00fc KS |
105 | #define THRESHOLDS_EVENTS_TARGET 128 |
106 | #define SOFTLIMIT_EVENTS_TARGET 1024 | |
e9f8974f | 107 | |
bb4cc1a8 AM |
108 | /* |
109 | * Cgroups above their limits are maintained in a RB-Tree, independent of | |
110 | * their hierarchy representation | |
111 | */ | |
112 | ||
ef8f2327 | 113 | struct mem_cgroup_tree_per_node { |
bb4cc1a8 | 114 | struct rb_root rb_root; |
fa90b2fd | 115 | struct rb_node *rb_rightmost; |
bb4cc1a8 AM |
116 | spinlock_t lock; |
117 | }; | |
118 | ||
bb4cc1a8 AM |
119 | struct mem_cgroup_tree { |
120 | struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES]; | |
121 | }; | |
122 | ||
123 | static struct mem_cgroup_tree soft_limit_tree __read_mostly; | |
124 | ||
9490ff27 KH |
125 | /* for OOM */ |
126 | struct mem_cgroup_eventfd_list { | |
127 | struct list_head list; | |
128 | struct eventfd_ctx *eventfd; | |
129 | }; | |
2e72b634 | 130 | |
79bd9814 TH |
131 | /* |
132 | * cgroup_event represents events which userspace want to receive. | |
133 | */ | |
3bc942f3 | 134 | struct mem_cgroup_event { |
79bd9814 | 135 | /* |
59b6f873 | 136 | * memcg which the event belongs to. |
79bd9814 | 137 | */ |
59b6f873 | 138 | struct mem_cgroup *memcg; |
79bd9814 TH |
139 | /* |
140 | * eventfd to signal userspace about the event. | |
141 | */ | |
142 | struct eventfd_ctx *eventfd; | |
143 | /* | |
144 | * Each of these stored in a list by the cgroup. | |
145 | */ | |
146 | struct list_head list; | |
fba94807 TH |
147 | /* |
148 | * register_event() callback will be used to add new userspace | |
149 | * waiter for changes related to this event. Use eventfd_signal() | |
150 | * on eventfd to send notification to userspace. | |
151 | */ | |
59b6f873 | 152 | int (*register_event)(struct mem_cgroup *memcg, |
347c4a87 | 153 | struct eventfd_ctx *eventfd, const char *args); |
fba94807 TH |
154 | /* |
155 | * unregister_event() callback will be called when userspace closes | |
156 | * the eventfd or on cgroup removing. This callback must be set, | |
157 | * if you want provide notification functionality. | |
158 | */ | |
59b6f873 | 159 | void (*unregister_event)(struct mem_cgroup *memcg, |
fba94807 | 160 | struct eventfd_ctx *eventfd); |
79bd9814 TH |
161 | /* |
162 | * All fields below needed to unregister event when | |
163 | * userspace closes eventfd. | |
164 | */ | |
165 | poll_table pt; | |
166 | wait_queue_head_t *wqh; | |
ac6424b9 | 167 | wait_queue_entry_t wait; |
79bd9814 TH |
168 | struct work_struct remove; |
169 | }; | |
170 | ||
c0ff4b85 R |
171 | static void mem_cgroup_threshold(struct mem_cgroup *memcg); |
172 | static void mem_cgroup_oom_notify(struct mem_cgroup *memcg); | |
2e72b634 | 173 | |
7dc74be0 DN |
174 | /* Stuffs for move charges at task migration. */ |
175 | /* | |
1dfab5ab | 176 | * Types of charges to be moved. |
7dc74be0 | 177 | */ |
1dfab5ab JW |
178 | #define MOVE_ANON 0x1U |
179 | #define MOVE_FILE 0x2U | |
180 | #define MOVE_MASK (MOVE_ANON | MOVE_FILE) | |
7dc74be0 | 181 | |
4ffef5fe DN |
182 | /* "mc" and its members are protected by cgroup_mutex */ |
183 | static struct move_charge_struct { | |
b1dd693e | 184 | spinlock_t lock; /* for from, to */ |
264a0ae1 | 185 | struct mm_struct *mm; |
4ffef5fe DN |
186 | struct mem_cgroup *from; |
187 | struct mem_cgroup *to; | |
1dfab5ab | 188 | unsigned long flags; |
4ffef5fe | 189 | unsigned long precharge; |
854ffa8d | 190 | unsigned long moved_charge; |
483c30b5 | 191 | unsigned long moved_swap; |
8033b97c DN |
192 | struct task_struct *moving_task; /* a task moving charges */ |
193 | wait_queue_head_t waitq; /* a waitq for other context */ | |
194 | } mc = { | |
2bd9bb20 | 195 | .lock = __SPIN_LOCK_UNLOCKED(mc.lock), |
8033b97c DN |
196 | .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq), |
197 | }; | |
4ffef5fe | 198 | |
4e416953 BS |
199 | /* |
200 | * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft | |
201 | * limit reclaim to prevent infinite loops, if they ever occur. | |
202 | */ | |
a0db00fc | 203 | #define MEM_CGROUP_MAX_RECLAIM_LOOPS 100 |
bb4cc1a8 | 204 | #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2 |
4e416953 | 205 | |
8c7c6e34 | 206 | /* for encoding cft->private value on file */ |
86ae53e1 GC |
207 | enum res_type { |
208 | _MEM, | |
209 | _MEMSWAP, | |
510fc4e1 | 210 | _KMEM, |
d55f90bf | 211 | _TCP, |
86ae53e1 GC |
212 | }; |
213 | ||
a0db00fc KS |
214 | #define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val)) |
215 | #define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff) | |
8c7c6e34 KH |
216 | #define MEMFILE_ATTR(val) ((val) & 0xffff) |
217 | ||
b05706f1 KT |
218 | /* |
219 | * Iteration constructs for visiting all cgroups (under a tree). If | |
220 | * loops are exited prematurely (break), mem_cgroup_iter_break() must | |
221 | * be used for reference counting. | |
222 | */ | |
223 | #define for_each_mem_cgroup_tree(iter, root) \ | |
224 | for (iter = mem_cgroup_iter(root, NULL, NULL); \ | |
225 | iter != NULL; \ | |
226 | iter = mem_cgroup_iter(root, iter, NULL)) | |
227 | ||
228 | #define for_each_mem_cgroup(iter) \ | |
229 | for (iter = mem_cgroup_iter(NULL, NULL, NULL); \ | |
230 | iter != NULL; \ | |
231 | iter = mem_cgroup_iter(NULL, iter, NULL)) | |
232 | ||
a4ebf1b6 | 233 | static inline bool task_is_dying(void) |
7775face TH |
234 | { |
235 | return tsk_is_oom_victim(current) || fatal_signal_pending(current) || | |
236 | (current->flags & PF_EXITING); | |
237 | } | |
238 | ||
70ddf637 AV |
239 | /* Some nice accessors for the vmpressure. */ |
240 | struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg) | |
241 | { | |
242 | if (!memcg) | |
243 | memcg = root_mem_cgroup; | |
244 | return &memcg->vmpressure; | |
245 | } | |
246 | ||
9647875b | 247 | struct mem_cgroup *vmpressure_to_memcg(struct vmpressure *vmpr) |
70ddf637 | 248 | { |
9647875b | 249 | return container_of(vmpr, struct mem_cgroup, vmpressure); |
70ddf637 AV |
250 | } |
251 | ||
84c07d11 | 252 | #ifdef CONFIG_MEMCG_KMEM |
0764db9b | 253 | static DEFINE_SPINLOCK(objcg_lock); |
bf4f0599 | 254 | |
4d5c8aed RG |
255 | bool mem_cgroup_kmem_disabled(void) |
256 | { | |
257 | return cgroup_memory_nokmem; | |
258 | } | |
259 | ||
f1286fae MS |
260 | static void obj_cgroup_uncharge_pages(struct obj_cgroup *objcg, |
261 | unsigned int nr_pages); | |
c1a660de | 262 | |
bf4f0599 RG |
263 | static void obj_cgroup_release(struct percpu_ref *ref) |
264 | { | |
265 | struct obj_cgroup *objcg = container_of(ref, struct obj_cgroup, refcnt); | |
bf4f0599 RG |
266 | unsigned int nr_bytes; |
267 | unsigned int nr_pages; | |
268 | unsigned long flags; | |
269 | ||
270 | /* | |
271 | * At this point all allocated objects are freed, and | |
272 | * objcg->nr_charged_bytes can't have an arbitrary byte value. | |
273 | * However, it can be PAGE_SIZE or (x * PAGE_SIZE). | |
274 | * | |
275 | * The following sequence can lead to it: | |
276 | * 1) CPU0: objcg == stock->cached_objcg | |
277 | * 2) CPU1: we do a small allocation (e.g. 92 bytes), | |
278 | * PAGE_SIZE bytes are charged | |
279 | * 3) CPU1: a process from another memcg is allocating something, | |
280 | * the stock if flushed, | |
281 | * objcg->nr_charged_bytes = PAGE_SIZE - 92 | |
282 | * 5) CPU0: we do release this object, | |
283 | * 92 bytes are added to stock->nr_bytes | |
284 | * 6) CPU0: stock is flushed, | |
285 | * 92 bytes are added to objcg->nr_charged_bytes | |
286 | * | |
287 | * In the result, nr_charged_bytes == PAGE_SIZE. | |
288 | * This page will be uncharged in obj_cgroup_release(). | |
289 | */ | |
290 | nr_bytes = atomic_read(&objcg->nr_charged_bytes); | |
291 | WARN_ON_ONCE(nr_bytes & (PAGE_SIZE - 1)); | |
292 | nr_pages = nr_bytes >> PAGE_SHIFT; | |
293 | ||
bf4f0599 | 294 | if (nr_pages) |
f1286fae | 295 | obj_cgroup_uncharge_pages(objcg, nr_pages); |
271dd6b1 | 296 | |
0764db9b | 297 | spin_lock_irqsave(&objcg_lock, flags); |
bf4f0599 | 298 | list_del(&objcg->list); |
0764db9b | 299 | spin_unlock_irqrestore(&objcg_lock, flags); |
bf4f0599 RG |
300 | |
301 | percpu_ref_exit(ref); | |
302 | kfree_rcu(objcg, rcu); | |
303 | } | |
304 | ||
305 | static struct obj_cgroup *obj_cgroup_alloc(void) | |
306 | { | |
307 | struct obj_cgroup *objcg; | |
308 | int ret; | |
309 | ||
310 | objcg = kzalloc(sizeof(struct obj_cgroup), GFP_KERNEL); | |
311 | if (!objcg) | |
312 | return NULL; | |
313 | ||
314 | ret = percpu_ref_init(&objcg->refcnt, obj_cgroup_release, 0, | |
315 | GFP_KERNEL); | |
316 | if (ret) { | |
317 | kfree(objcg); | |
318 | return NULL; | |
319 | } | |
320 | INIT_LIST_HEAD(&objcg->list); | |
321 | return objcg; | |
322 | } | |
323 | ||
324 | static void memcg_reparent_objcgs(struct mem_cgroup *memcg, | |
325 | struct mem_cgroup *parent) | |
326 | { | |
327 | struct obj_cgroup *objcg, *iter; | |
328 | ||
329 | objcg = rcu_replace_pointer(memcg->objcg, NULL, true); | |
330 | ||
0764db9b | 331 | spin_lock_irq(&objcg_lock); |
bf4f0599 | 332 | |
9838354e MS |
333 | /* 1) Ready to reparent active objcg. */ |
334 | list_add(&objcg->list, &memcg->objcg_list); | |
335 | /* 2) Reparent active objcg and already reparented objcgs to parent. */ | |
336 | list_for_each_entry(iter, &memcg->objcg_list, list) | |
337 | WRITE_ONCE(iter->memcg, parent); | |
338 | /* 3) Move already reparented objcgs to the parent's list */ | |
bf4f0599 RG |
339 | list_splice(&memcg->objcg_list, &parent->objcg_list); |
340 | ||
0764db9b | 341 | spin_unlock_irq(&objcg_lock); |
bf4f0599 RG |
342 | |
343 | percpu_ref_kill(&objcg->refcnt); | |
344 | } | |
345 | ||
d7f25f8a GC |
346 | /* |
347 | * A lot of the calls to the cache allocation functions are expected to be | |
272911a4 | 348 | * inlined by the compiler. Since the calls to memcg_slab_pre_alloc_hook() are |
d7f25f8a GC |
349 | * conditional to this static branch, we'll have to allow modules that does |
350 | * kmem_cache_alloc and the such to see this symbol as well | |
351 | */ | |
f7a449f7 RG |
352 | DEFINE_STATIC_KEY_FALSE(memcg_kmem_online_key); |
353 | EXPORT_SYMBOL(memcg_kmem_online_key); | |
b6c1a8af YS |
354 | |
355 | DEFINE_STATIC_KEY_FALSE(memcg_bpf_enabled_key); | |
356 | EXPORT_SYMBOL(memcg_bpf_enabled_key); | |
0a432dcb | 357 | #endif |
17cc4dfe | 358 | |
ad7fa852 | 359 | /** |
75376c6f MWO |
360 | * mem_cgroup_css_from_folio - css of the memcg associated with a folio |
361 | * @folio: folio of interest | |
ad7fa852 TH |
362 | * |
363 | * If memcg is bound to the default hierarchy, css of the memcg associated | |
75376c6f | 364 | * with @folio is returned. The returned css remains associated with @folio |
ad7fa852 TH |
365 | * until it is released. |
366 | * | |
367 | * If memcg is bound to a traditional hierarchy, the css of root_mem_cgroup | |
368 | * is returned. | |
ad7fa852 | 369 | */ |
75376c6f | 370 | struct cgroup_subsys_state *mem_cgroup_css_from_folio(struct folio *folio) |
ad7fa852 | 371 | { |
75376c6f | 372 | struct mem_cgroup *memcg = folio_memcg(folio); |
ad7fa852 | 373 | |
9e10a130 | 374 | if (!memcg || !cgroup_subsys_on_dfl(memory_cgrp_subsys)) |
ad7fa852 TH |
375 | memcg = root_mem_cgroup; |
376 | ||
ad7fa852 TH |
377 | return &memcg->css; |
378 | } | |
379 | ||
2fc04524 VD |
380 | /** |
381 | * page_cgroup_ino - return inode number of the memcg a page is charged to | |
382 | * @page: the page | |
383 | * | |
384 | * Look up the closest online ancestor of the memory cgroup @page is charged to | |
385 | * and return its inode number or 0 if @page is not charged to any cgroup. It | |
386 | * is safe to call this function without holding a reference to @page. | |
387 | * | |
388 | * Note, this function is inherently racy, because there is nothing to prevent | |
389 | * the cgroup inode from getting torn down and potentially reallocated a moment | |
390 | * after page_cgroup_ino() returns, so it only should be used by callers that | |
391 | * do not care (such as procfs interfaces). | |
392 | */ | |
393 | ino_t page_cgroup_ino(struct page *page) | |
394 | { | |
395 | struct mem_cgroup *memcg; | |
396 | unsigned long ino = 0; | |
397 | ||
398 | rcu_read_lock(); | |
ec342603 YA |
399 | /* page_folio() is racy here, but the entire function is racy anyway */ |
400 | memcg = folio_memcg_check(page_folio(page)); | |
286e04b8 | 401 | |
2fc04524 VD |
402 | while (memcg && !(memcg->css.flags & CSS_ONLINE)) |
403 | memcg = parent_mem_cgroup(memcg); | |
404 | if (memcg) | |
405 | ino = cgroup_ino(memcg->css.cgroup); | |
406 | rcu_read_unlock(); | |
407 | return ino; | |
408 | } | |
409 | ||
ef8f2327 MG |
410 | static void __mem_cgroup_insert_exceeded(struct mem_cgroup_per_node *mz, |
411 | struct mem_cgroup_tree_per_node *mctz, | |
3e32cb2e | 412 | unsigned long new_usage_in_excess) |
bb4cc1a8 AM |
413 | { |
414 | struct rb_node **p = &mctz->rb_root.rb_node; | |
415 | struct rb_node *parent = NULL; | |
ef8f2327 | 416 | struct mem_cgroup_per_node *mz_node; |
fa90b2fd | 417 | bool rightmost = true; |
bb4cc1a8 AM |
418 | |
419 | if (mz->on_tree) | |
420 | return; | |
421 | ||
422 | mz->usage_in_excess = new_usage_in_excess; | |
423 | if (!mz->usage_in_excess) | |
424 | return; | |
425 | while (*p) { | |
426 | parent = *p; | |
ef8f2327 | 427 | mz_node = rb_entry(parent, struct mem_cgroup_per_node, |
bb4cc1a8 | 428 | tree_node); |
fa90b2fd | 429 | if (mz->usage_in_excess < mz_node->usage_in_excess) { |
bb4cc1a8 | 430 | p = &(*p)->rb_left; |
fa90b2fd | 431 | rightmost = false; |
378876b0 | 432 | } else { |
bb4cc1a8 | 433 | p = &(*p)->rb_right; |
378876b0 | 434 | } |
bb4cc1a8 | 435 | } |
fa90b2fd DB |
436 | |
437 | if (rightmost) | |
438 | mctz->rb_rightmost = &mz->tree_node; | |
439 | ||
bb4cc1a8 AM |
440 | rb_link_node(&mz->tree_node, parent, p); |
441 | rb_insert_color(&mz->tree_node, &mctz->rb_root); | |
442 | mz->on_tree = true; | |
443 | } | |
444 | ||
ef8f2327 MG |
445 | static void __mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz, |
446 | struct mem_cgroup_tree_per_node *mctz) | |
bb4cc1a8 AM |
447 | { |
448 | if (!mz->on_tree) | |
449 | return; | |
fa90b2fd DB |
450 | |
451 | if (&mz->tree_node == mctz->rb_rightmost) | |
452 | mctz->rb_rightmost = rb_prev(&mz->tree_node); | |
453 | ||
bb4cc1a8 AM |
454 | rb_erase(&mz->tree_node, &mctz->rb_root); |
455 | mz->on_tree = false; | |
456 | } | |
457 | ||
ef8f2327 MG |
458 | static void mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz, |
459 | struct mem_cgroup_tree_per_node *mctz) | |
bb4cc1a8 | 460 | { |
0a31bc97 JW |
461 | unsigned long flags; |
462 | ||
463 | spin_lock_irqsave(&mctz->lock, flags); | |
cf2c8127 | 464 | __mem_cgroup_remove_exceeded(mz, mctz); |
0a31bc97 | 465 | spin_unlock_irqrestore(&mctz->lock, flags); |
bb4cc1a8 AM |
466 | } |
467 | ||
3e32cb2e JW |
468 | static unsigned long soft_limit_excess(struct mem_cgroup *memcg) |
469 | { | |
470 | unsigned long nr_pages = page_counter_read(&memcg->memory); | |
4db0c3c2 | 471 | unsigned long soft_limit = READ_ONCE(memcg->soft_limit); |
3e32cb2e JW |
472 | unsigned long excess = 0; |
473 | ||
474 | if (nr_pages > soft_limit) | |
475 | excess = nr_pages - soft_limit; | |
476 | ||
477 | return excess; | |
478 | } | |
bb4cc1a8 | 479 | |
658b69c9 | 480 | static void mem_cgroup_update_tree(struct mem_cgroup *memcg, int nid) |
bb4cc1a8 | 481 | { |
3e32cb2e | 482 | unsigned long excess; |
ef8f2327 MG |
483 | struct mem_cgroup_per_node *mz; |
484 | struct mem_cgroup_tree_per_node *mctz; | |
bb4cc1a8 | 485 | |
e4dde56c | 486 | if (lru_gen_enabled()) { |
36c7b4db | 487 | if (soft_limit_excess(memcg)) |
5c7e7a0d | 488 | lru_gen_soft_reclaim(memcg, nid); |
e4dde56c YZ |
489 | return; |
490 | } | |
491 | ||
2ab082ba | 492 | mctz = soft_limit_tree.rb_tree_per_node[nid]; |
bfc7228b LD |
493 | if (!mctz) |
494 | return; | |
bb4cc1a8 AM |
495 | /* |
496 | * Necessary to update all ancestors when hierarchy is used. | |
497 | * because their event counter is not touched. | |
498 | */ | |
499 | for (; memcg; memcg = parent_mem_cgroup(memcg)) { | |
658b69c9 | 500 | mz = memcg->nodeinfo[nid]; |
3e32cb2e | 501 | excess = soft_limit_excess(memcg); |
bb4cc1a8 AM |
502 | /* |
503 | * We have to update the tree if mz is on RB-tree or | |
504 | * mem is over its softlimit. | |
505 | */ | |
506 | if (excess || mz->on_tree) { | |
0a31bc97 JW |
507 | unsigned long flags; |
508 | ||
509 | spin_lock_irqsave(&mctz->lock, flags); | |
bb4cc1a8 AM |
510 | /* if on-tree, remove it */ |
511 | if (mz->on_tree) | |
cf2c8127 | 512 | __mem_cgroup_remove_exceeded(mz, mctz); |
bb4cc1a8 AM |
513 | /* |
514 | * Insert again. mz->usage_in_excess will be updated. | |
515 | * If excess is 0, no tree ops. | |
516 | */ | |
cf2c8127 | 517 | __mem_cgroup_insert_exceeded(mz, mctz, excess); |
0a31bc97 | 518 | spin_unlock_irqrestore(&mctz->lock, flags); |
bb4cc1a8 AM |
519 | } |
520 | } | |
521 | } | |
522 | ||
523 | static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg) | |
524 | { | |
ef8f2327 MG |
525 | struct mem_cgroup_tree_per_node *mctz; |
526 | struct mem_cgroup_per_node *mz; | |
527 | int nid; | |
bb4cc1a8 | 528 | |
e231875b | 529 | for_each_node(nid) { |
a3747b53 | 530 | mz = memcg->nodeinfo[nid]; |
2ab082ba | 531 | mctz = soft_limit_tree.rb_tree_per_node[nid]; |
bfc7228b LD |
532 | if (mctz) |
533 | mem_cgroup_remove_exceeded(mz, mctz); | |
bb4cc1a8 AM |
534 | } |
535 | } | |
536 | ||
ef8f2327 MG |
537 | static struct mem_cgroup_per_node * |
538 | __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz) | |
bb4cc1a8 | 539 | { |
ef8f2327 | 540 | struct mem_cgroup_per_node *mz; |
bb4cc1a8 AM |
541 | |
542 | retry: | |
543 | mz = NULL; | |
fa90b2fd | 544 | if (!mctz->rb_rightmost) |
bb4cc1a8 AM |
545 | goto done; /* Nothing to reclaim from */ |
546 | ||
fa90b2fd DB |
547 | mz = rb_entry(mctz->rb_rightmost, |
548 | struct mem_cgroup_per_node, tree_node); | |
bb4cc1a8 AM |
549 | /* |
550 | * Remove the node now but someone else can add it back, | |
551 | * we will to add it back at the end of reclaim to its correct | |
552 | * position in the tree. | |
553 | */ | |
cf2c8127 | 554 | __mem_cgroup_remove_exceeded(mz, mctz); |
3e32cb2e | 555 | if (!soft_limit_excess(mz->memcg) || |
8965aa28 | 556 | !css_tryget(&mz->memcg->css)) |
bb4cc1a8 AM |
557 | goto retry; |
558 | done: | |
559 | return mz; | |
560 | } | |
561 | ||
ef8f2327 MG |
562 | static struct mem_cgroup_per_node * |
563 | mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz) | |
bb4cc1a8 | 564 | { |
ef8f2327 | 565 | struct mem_cgroup_per_node *mz; |
bb4cc1a8 | 566 | |
0a31bc97 | 567 | spin_lock_irq(&mctz->lock); |
bb4cc1a8 | 568 | mz = __mem_cgroup_largest_soft_limit_node(mctz); |
0a31bc97 | 569 | spin_unlock_irq(&mctz->lock); |
bb4cc1a8 AM |
570 | return mz; |
571 | } | |
572 | ||
11192d9c SB |
573 | /* |
574 | * memcg and lruvec stats flushing | |
575 | * | |
576 | * Many codepaths leading to stats update or read are performance sensitive and | |
577 | * adding stats flushing in such codepaths is not desirable. So, to optimize the | |
578 | * flushing the kernel does: | |
579 | * | |
580 | * 1) Periodically and asynchronously flush the stats every 2 seconds to not let | |
581 | * rstat update tree grow unbounded. | |
582 | * | |
583 | * 2) Flush the stats synchronously on reader side only when there are more than | |
584 | * (MEMCG_CHARGE_BATCH * nr_cpus) update events. Though this optimization | |
585 | * will let stats be out of sync by atmost (MEMCG_CHARGE_BATCH * nr_cpus) but | |
586 | * only for 2 seconds due to (1). | |
587 | */ | |
588 | static void flush_memcg_stats_dwork(struct work_struct *w); | |
589 | static DECLARE_DEFERRABLE_WORK(stats_flush_dwork, flush_memcg_stats_dwork); | |
11192d9c | 590 | static DEFINE_PER_CPU(unsigned int, stats_updates); |
3cd9992b | 591 | static atomic_t stats_flush_ongoing = ATOMIC_INIT(0); |
11192d9c | 592 | static atomic_t stats_flush_threshold = ATOMIC_INIT(0); |
9b301615 SB |
593 | static u64 flush_next_time; |
594 | ||
595 | #define FLUSH_TIME (2UL*HZ) | |
11192d9c | 596 | |
be3e67b5 SAS |
597 | /* |
598 | * Accessors to ensure that preemption is disabled on PREEMPT_RT because it can | |
599 | * not rely on this as part of an acquired spinlock_t lock. These functions are | |
600 | * never used in hardirq context on PREEMPT_RT and therefore disabling preemtion | |
601 | * is sufficient. | |
602 | */ | |
603 | static void memcg_stats_lock(void) | |
604 | { | |
e575d401 TG |
605 | preempt_disable_nested(); |
606 | VM_WARN_ON_IRQS_ENABLED(); | |
be3e67b5 SAS |
607 | } |
608 | ||
609 | static void __memcg_stats_lock(void) | |
610 | { | |
e575d401 | 611 | preempt_disable_nested(); |
be3e67b5 SAS |
612 | } |
613 | ||
614 | static void memcg_stats_unlock(void) | |
615 | { | |
e575d401 | 616 | preempt_enable_nested(); |
be3e67b5 SAS |
617 | } |
618 | ||
5b3be698 | 619 | static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val) |
11192d9c | 620 | { |
5b3be698 SB |
621 | unsigned int x; |
622 | ||
f9d911ca YA |
623 | if (!val) |
624 | return; | |
625 | ||
11192d9c | 626 | cgroup_rstat_updated(memcg->css.cgroup, smp_processor_id()); |
5b3be698 SB |
627 | |
628 | x = __this_cpu_add_return(stats_updates, abs(val)); | |
629 | if (x > MEMCG_CHARGE_BATCH) { | |
873f64b7 JS |
630 | /* |
631 | * If stats_flush_threshold exceeds the threshold | |
632 | * (>num_online_cpus()), cgroup stats update will be triggered | |
633 | * in __mem_cgroup_flush_stats(). Increasing this var further | |
634 | * is redundant and simply adds overhead in atomic update. | |
635 | */ | |
636 | if (atomic_read(&stats_flush_threshold) <= num_online_cpus()) | |
637 | atomic_add(x / MEMCG_CHARGE_BATCH, &stats_flush_threshold); | |
5b3be698 SB |
638 | __this_cpu_write(stats_updates, 0); |
639 | } | |
11192d9c SB |
640 | } |
641 | ||
35822fda | 642 | static void do_flush_stats(void) |
11192d9c | 643 | { |
3cd9992b YA |
644 | /* |
645 | * We always flush the entire tree, so concurrent flushers can just | |
646 | * skip. This avoids a thundering herd problem on the rstat global lock | |
647 | * from memcg flushers (e.g. reclaim, refault, etc). | |
648 | */ | |
649 | if (atomic_read(&stats_flush_ongoing) || | |
650 | atomic_xchg(&stats_flush_ongoing, 1)) | |
11192d9c SB |
651 | return; |
652 | ||
3cd9992b | 653 | WRITE_ONCE(flush_next_time, jiffies_64 + 2*FLUSH_TIME); |
9fad9aee | 654 | |
35822fda | 655 | cgroup_rstat_flush(root_mem_cgroup->css.cgroup); |
9fad9aee | 656 | |
11192d9c | 657 | atomic_set(&stats_flush_threshold, 0); |
3cd9992b | 658 | atomic_set(&stats_flush_ongoing, 0); |
11192d9c SB |
659 | } |
660 | ||
661 | void mem_cgroup_flush_stats(void) | |
662 | { | |
35822fda YA |
663 | if (atomic_read(&stats_flush_threshold) > num_online_cpus()) |
664 | do_flush_stats(); | |
9fad9aee YA |
665 | } |
666 | ||
4009b2f1 | 667 | void mem_cgroup_flush_stats_ratelimited(void) |
9b301615 | 668 | { |
3cd9992b | 669 | if (time_after64(jiffies_64, READ_ONCE(flush_next_time))) |
4009b2f1 | 670 | mem_cgroup_flush_stats(); |
9b301615 SB |
671 | } |
672 | ||
11192d9c SB |
673 | static void flush_memcg_stats_dwork(struct work_struct *w) |
674 | { | |
9fad9aee YA |
675 | /* |
676 | * Always flush here so that flushing in latency-sensitive paths is | |
677 | * as cheap as possible. | |
678 | */ | |
35822fda | 679 | do_flush_stats(); |
9b301615 | 680 | queue_delayed_work(system_unbound_wq, &stats_flush_dwork, FLUSH_TIME); |
11192d9c SB |
681 | } |
682 | ||
d396def5 SB |
683 | /* Subset of vm_event_item to report for memcg event stats */ |
684 | static const unsigned int memcg_vm_event_stat[] = { | |
8278f1c7 SB |
685 | PGPGIN, |
686 | PGPGOUT, | |
d396def5 SB |
687 | PGSCAN_KSWAPD, |
688 | PGSCAN_DIRECT, | |
57e9cc50 | 689 | PGSCAN_KHUGEPAGED, |
d396def5 SB |
690 | PGSTEAL_KSWAPD, |
691 | PGSTEAL_DIRECT, | |
57e9cc50 | 692 | PGSTEAL_KHUGEPAGED, |
d396def5 SB |
693 | PGFAULT, |
694 | PGMAJFAULT, | |
695 | PGREFILL, | |
696 | PGACTIVATE, | |
697 | PGDEACTIVATE, | |
698 | PGLAZYFREE, | |
699 | PGLAZYFREED, | |
700 | #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) | |
701 | ZSWPIN, | |
702 | ZSWPOUT, | |
703 | #endif | |
704 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE | |
705 | THP_FAULT_ALLOC, | |
706 | THP_COLLAPSE_ALLOC, | |
707 | #endif | |
708 | }; | |
709 | ||
8278f1c7 SB |
710 | #define NR_MEMCG_EVENTS ARRAY_SIZE(memcg_vm_event_stat) |
711 | static int mem_cgroup_events_index[NR_VM_EVENT_ITEMS] __read_mostly; | |
712 | ||
713 | static void init_memcg_events(void) | |
714 | { | |
715 | int i; | |
716 | ||
717 | for (i = 0; i < NR_MEMCG_EVENTS; ++i) | |
718 | mem_cgroup_events_index[memcg_vm_event_stat[i]] = i + 1; | |
719 | } | |
720 | ||
721 | static inline int memcg_events_index(enum vm_event_item idx) | |
722 | { | |
723 | return mem_cgroup_events_index[idx] - 1; | |
724 | } | |
725 | ||
410f8e82 SB |
726 | struct memcg_vmstats_percpu { |
727 | /* Local (CPU and cgroup) page state & events */ | |
728 | long state[MEMCG_NR_STAT]; | |
8278f1c7 | 729 | unsigned long events[NR_MEMCG_EVENTS]; |
410f8e82 SB |
730 | |
731 | /* Delta calculation for lockless upward propagation */ | |
732 | long state_prev[MEMCG_NR_STAT]; | |
8278f1c7 | 733 | unsigned long events_prev[NR_MEMCG_EVENTS]; |
410f8e82 SB |
734 | |
735 | /* Cgroup1: threshold notifications & softlimit tree updates */ | |
736 | unsigned long nr_page_events; | |
737 | unsigned long targets[MEM_CGROUP_NTARGETS]; | |
738 | }; | |
739 | ||
740 | struct memcg_vmstats { | |
741 | /* Aggregated (CPU and subtree) page state & events */ | |
742 | long state[MEMCG_NR_STAT]; | |
8278f1c7 | 743 | unsigned long events[NR_MEMCG_EVENTS]; |
410f8e82 SB |
744 | |
745 | /* Pending child counts during tree propagation */ | |
746 | long state_pending[MEMCG_NR_STAT]; | |
8278f1c7 | 747 | unsigned long events_pending[NR_MEMCG_EVENTS]; |
410f8e82 SB |
748 | }; |
749 | ||
750 | unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) | |
751 | { | |
752 | long x = READ_ONCE(memcg->vmstats->state[idx]); | |
753 | #ifdef CONFIG_SMP | |
754 | if (x < 0) | |
755 | x = 0; | |
756 | #endif | |
757 | return x; | |
758 | } | |
759 | ||
db9adbcb JW |
760 | /** |
761 | * __mod_memcg_state - update cgroup memory statistics | |
762 | * @memcg: the memory cgroup | |
763 | * @idx: the stat item - can be enum memcg_stat_item or enum node_stat_item | |
764 | * @val: delta to add to the counter, can be negative | |
765 | */ | |
766 | void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val) | |
767 | { | |
db9adbcb JW |
768 | if (mem_cgroup_disabled()) |
769 | return; | |
770 | ||
2d146aa3 | 771 | __this_cpu_add(memcg->vmstats_percpu->state[idx], val); |
5b3be698 | 772 | memcg_rstat_updated(memcg, val); |
db9adbcb JW |
773 | } |
774 | ||
2d146aa3 | 775 | /* idx can be of type enum memcg_stat_item or node_stat_item. */ |
a18e6e6e JW |
776 | static unsigned long memcg_page_state_local(struct mem_cgroup *memcg, int idx) |
777 | { | |
778 | long x = 0; | |
779 | int cpu; | |
780 | ||
781 | for_each_possible_cpu(cpu) | |
2d146aa3 | 782 | x += per_cpu(memcg->vmstats_percpu->state[idx], cpu); |
a18e6e6e JW |
783 | #ifdef CONFIG_SMP |
784 | if (x < 0) | |
785 | x = 0; | |
786 | #endif | |
787 | return x; | |
788 | } | |
789 | ||
eedc4e5a RG |
790 | void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, |
791 | int val) | |
db9adbcb JW |
792 | { |
793 | struct mem_cgroup_per_node *pn; | |
42a30035 | 794 | struct mem_cgroup *memcg; |
db9adbcb | 795 | |
db9adbcb | 796 | pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); |
42a30035 | 797 | memcg = pn->memcg; |
db9adbcb | 798 | |
be3e67b5 SAS |
799 | /* |
800 | * The caller from rmap relay on disabled preemption becase they never | |
801 | * update their counter from in-interrupt context. For these two | |
802 | * counters we check that the update is never performed from an | |
803 | * interrupt context while other caller need to have disabled interrupt. | |
804 | */ | |
805 | __memcg_stats_lock(); | |
e575d401 | 806 | if (IS_ENABLED(CONFIG_DEBUG_VM)) { |
be3e67b5 SAS |
807 | switch (idx) { |
808 | case NR_ANON_MAPPED: | |
809 | case NR_FILE_MAPPED: | |
810 | case NR_ANON_THPS: | |
811 | case NR_SHMEM_PMDMAPPED: | |
812 | case NR_FILE_PMDMAPPED: | |
813 | WARN_ON_ONCE(!in_task()); | |
814 | break; | |
815 | default: | |
e575d401 | 816 | VM_WARN_ON_IRQS_ENABLED(); |
be3e67b5 SAS |
817 | } |
818 | } | |
819 | ||
db9adbcb | 820 | /* Update memcg */ |
11192d9c | 821 | __this_cpu_add(memcg->vmstats_percpu->state[idx], val); |
db9adbcb | 822 | |
b4c46484 | 823 | /* Update lruvec */ |
7e1c0d6f | 824 | __this_cpu_add(pn->lruvec_stats_percpu->state[idx], val); |
11192d9c | 825 | |
5b3be698 | 826 | memcg_rstat_updated(memcg, val); |
be3e67b5 | 827 | memcg_stats_unlock(); |
db9adbcb JW |
828 | } |
829 | ||
eedc4e5a RG |
830 | /** |
831 | * __mod_lruvec_state - update lruvec memory statistics | |
832 | * @lruvec: the lruvec | |
833 | * @idx: the stat item | |
834 | * @val: delta to add to the counter, can be negative | |
835 | * | |
836 | * The lruvec is the intersection of the NUMA node and a cgroup. This | |
837 | * function updates the all three counters that are affected by a | |
838 | * change of state at this level: per-node, per-cgroup, per-lruvec. | |
839 | */ | |
840 | void __mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, | |
841 | int val) | |
842 | { | |
843 | /* Update node */ | |
844 | __mod_node_page_state(lruvec_pgdat(lruvec), idx, val); | |
845 | ||
846 | /* Update memcg and lruvec */ | |
847 | if (!mem_cgroup_disabled()) | |
848 | __mod_memcg_lruvec_state(lruvec, idx, val); | |
849 | } | |
850 | ||
c47d5032 SB |
851 | void __mod_lruvec_page_state(struct page *page, enum node_stat_item idx, |
852 | int val) | |
853 | { | |
854 | struct page *head = compound_head(page); /* rmap on tail pages */ | |
b4e0b68f | 855 | struct mem_cgroup *memcg; |
c47d5032 SB |
856 | pg_data_t *pgdat = page_pgdat(page); |
857 | struct lruvec *lruvec; | |
858 | ||
b4e0b68f MS |
859 | rcu_read_lock(); |
860 | memcg = page_memcg(head); | |
c47d5032 | 861 | /* Untracked pages have no memcg, no lruvec. Update only the node */ |
d635a69d | 862 | if (!memcg) { |
b4e0b68f | 863 | rcu_read_unlock(); |
c47d5032 SB |
864 | __mod_node_page_state(pgdat, idx, val); |
865 | return; | |
866 | } | |
867 | ||
d635a69d | 868 | lruvec = mem_cgroup_lruvec(memcg, pgdat); |
c47d5032 | 869 | __mod_lruvec_state(lruvec, idx, val); |
b4e0b68f | 870 | rcu_read_unlock(); |
c47d5032 | 871 | } |
f0c0c115 | 872 | EXPORT_SYMBOL(__mod_lruvec_page_state); |
c47d5032 | 873 | |
da3ceeff | 874 | void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val) |
ec9f0238 | 875 | { |
4f103c63 | 876 | pg_data_t *pgdat = page_pgdat(virt_to_page(p)); |
ec9f0238 RG |
877 | struct mem_cgroup *memcg; |
878 | struct lruvec *lruvec; | |
879 | ||
880 | rcu_read_lock(); | |
fc4db90f | 881 | memcg = mem_cgroup_from_slab_obj(p); |
ec9f0238 | 882 | |
8faeb1ff MS |
883 | /* |
884 | * Untracked pages have no memcg, no lruvec. Update only the | |
885 | * node. If we reparent the slab objects to the root memcg, | |
886 | * when we free the slab object, we need to update the per-memcg | |
887 | * vmstats to keep it correct for the root memcg. | |
888 | */ | |
889 | if (!memcg) { | |
ec9f0238 RG |
890 | __mod_node_page_state(pgdat, idx, val); |
891 | } else { | |
867e5e1d | 892 | lruvec = mem_cgroup_lruvec(memcg, pgdat); |
ec9f0238 RG |
893 | __mod_lruvec_state(lruvec, idx, val); |
894 | } | |
895 | rcu_read_unlock(); | |
896 | } | |
897 | ||
db9adbcb JW |
898 | /** |
899 | * __count_memcg_events - account VM events in a cgroup | |
900 | * @memcg: the memory cgroup | |
901 | * @idx: the event item | |
f0953a1b | 902 | * @count: the number of events that occurred |
db9adbcb JW |
903 | */ |
904 | void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx, | |
905 | unsigned long count) | |
906 | { | |
8278f1c7 SB |
907 | int index = memcg_events_index(idx); |
908 | ||
909 | if (mem_cgroup_disabled() || index < 0) | |
db9adbcb JW |
910 | return; |
911 | ||
be3e67b5 | 912 | memcg_stats_lock(); |
8278f1c7 | 913 | __this_cpu_add(memcg->vmstats_percpu->events[index], count); |
5b3be698 | 914 | memcg_rstat_updated(memcg, count); |
be3e67b5 | 915 | memcg_stats_unlock(); |
db9adbcb JW |
916 | } |
917 | ||
42a30035 | 918 | static unsigned long memcg_events(struct mem_cgroup *memcg, int event) |
e9f8974f | 919 | { |
8278f1c7 SB |
920 | int index = memcg_events_index(event); |
921 | ||
922 | if (index < 0) | |
923 | return 0; | |
924 | return READ_ONCE(memcg->vmstats->events[index]); | |
e9f8974f JW |
925 | } |
926 | ||
42a30035 JW |
927 | static unsigned long memcg_events_local(struct mem_cgroup *memcg, int event) |
928 | { | |
815744d7 JW |
929 | long x = 0; |
930 | int cpu; | |
8278f1c7 SB |
931 | int index = memcg_events_index(event); |
932 | ||
933 | if (index < 0) | |
934 | return 0; | |
815744d7 JW |
935 | |
936 | for_each_possible_cpu(cpu) | |
8278f1c7 | 937 | x += per_cpu(memcg->vmstats_percpu->events[index], cpu); |
815744d7 | 938 | return x; |
42a30035 JW |
939 | } |
940 | ||
c0ff4b85 | 941 | static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg, |
3fba69a5 | 942 | int nr_pages) |
d52aa412 | 943 | { |
e401f176 KH |
944 | /* pagein of a big page is an event. So, ignore page size */ |
945 | if (nr_pages > 0) | |
c9019e9b | 946 | __count_memcg_events(memcg, PGPGIN, 1); |
3751d604 | 947 | else { |
c9019e9b | 948 | __count_memcg_events(memcg, PGPGOUT, 1); |
3751d604 KH |
949 | nr_pages = -nr_pages; /* for event */ |
950 | } | |
e401f176 | 951 | |
871789d4 | 952 | __this_cpu_add(memcg->vmstats_percpu->nr_page_events, nr_pages); |
6d12e2d8 KH |
953 | } |
954 | ||
f53d7ce3 JW |
955 | static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg, |
956 | enum mem_cgroup_events_target target) | |
7a159cc9 JW |
957 | { |
958 | unsigned long val, next; | |
959 | ||
871789d4 CD |
960 | val = __this_cpu_read(memcg->vmstats_percpu->nr_page_events); |
961 | next = __this_cpu_read(memcg->vmstats_percpu->targets[target]); | |
7a159cc9 | 962 | /* from time_after() in jiffies.h */ |
6a1a8b80 | 963 | if ((long)(next - val) < 0) { |
f53d7ce3 JW |
964 | switch (target) { |
965 | case MEM_CGROUP_TARGET_THRESH: | |
966 | next = val + THRESHOLDS_EVENTS_TARGET; | |
967 | break; | |
bb4cc1a8 AM |
968 | case MEM_CGROUP_TARGET_SOFTLIMIT: |
969 | next = val + SOFTLIMIT_EVENTS_TARGET; | |
970 | break; | |
f53d7ce3 JW |
971 | default: |
972 | break; | |
973 | } | |
871789d4 | 974 | __this_cpu_write(memcg->vmstats_percpu->targets[target], next); |
f53d7ce3 | 975 | return true; |
7a159cc9 | 976 | } |
f53d7ce3 | 977 | return false; |
d2265e6f KH |
978 | } |
979 | ||
980 | /* | |
981 | * Check events in order. | |
982 | * | |
983 | */ | |
8e88bd2d | 984 | static void memcg_check_events(struct mem_cgroup *memcg, int nid) |
d2265e6f | 985 | { |
2343e88d SAS |
986 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) |
987 | return; | |
988 | ||
d2265e6f | 989 | /* threshold event is triggered in finer grain than soft limit */ |
f53d7ce3 JW |
990 | if (unlikely(mem_cgroup_event_ratelimit(memcg, |
991 | MEM_CGROUP_TARGET_THRESH))) { | |
bb4cc1a8 | 992 | bool do_softlimit; |
f53d7ce3 | 993 | |
bb4cc1a8 AM |
994 | do_softlimit = mem_cgroup_event_ratelimit(memcg, |
995 | MEM_CGROUP_TARGET_SOFTLIMIT); | |
c0ff4b85 | 996 | mem_cgroup_threshold(memcg); |
bb4cc1a8 | 997 | if (unlikely(do_softlimit)) |
8e88bd2d | 998 | mem_cgroup_update_tree(memcg, nid); |
0a31bc97 | 999 | } |
d2265e6f KH |
1000 | } |
1001 | ||
cf475ad2 | 1002 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
78fb7466 | 1003 | { |
31a78f23 BS |
1004 | /* |
1005 | * mm_update_next_owner() may clear mm->owner to NULL | |
1006 | * if it races with swapoff, page migration, etc. | |
1007 | * So this can be called with p == NULL. | |
1008 | */ | |
1009 | if (unlikely(!p)) | |
1010 | return NULL; | |
1011 | ||
073219e9 | 1012 | return mem_cgroup_from_css(task_css(p, memory_cgrp_id)); |
78fb7466 | 1013 | } |
33398cf2 | 1014 | EXPORT_SYMBOL(mem_cgroup_from_task); |
78fb7466 | 1015 | |
04f94e3f DS |
1016 | static __always_inline struct mem_cgroup *active_memcg(void) |
1017 | { | |
55a68c82 | 1018 | if (!in_task()) |
04f94e3f DS |
1019 | return this_cpu_read(int_active_memcg); |
1020 | else | |
1021 | return current->active_memcg; | |
1022 | } | |
1023 | ||
d46eb14b SB |
1024 | /** |
1025 | * get_mem_cgroup_from_mm: Obtain a reference on given mm_struct's memcg. | |
1026 | * @mm: mm from which memcg should be extracted. It can be NULL. | |
1027 | * | |
04f94e3f DS |
1028 | * Obtain a reference on mm->memcg and returns it if successful. If mm |
1029 | * is NULL, then the memcg is chosen as follows: | |
1030 | * 1) The active memcg, if set. | |
1031 | * 2) current->mm->memcg, if available | |
1032 | * 3) root memcg | |
1033 | * If mem_cgroup is disabled, NULL is returned. | |
d46eb14b SB |
1034 | */ |
1035 | struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm) | |
54595fe2 | 1036 | { |
d46eb14b SB |
1037 | struct mem_cgroup *memcg; |
1038 | ||
1039 | if (mem_cgroup_disabled()) | |
1040 | return NULL; | |
0b7f569e | 1041 | |
2884b6b7 MS |
1042 | /* |
1043 | * Page cache insertions can happen without an | |
1044 | * actual mm context, e.g. during disk probing | |
1045 | * on boot, loopback IO, acct() writes etc. | |
1046 | * | |
1047 | * No need to css_get on root memcg as the reference | |
1048 | * counting is disabled on the root level in the | |
1049 | * cgroup core. See CSS_NO_REF. | |
1050 | */ | |
04f94e3f DS |
1051 | if (unlikely(!mm)) { |
1052 | memcg = active_memcg(); | |
1053 | if (unlikely(memcg)) { | |
1054 | /* remote memcg must hold a ref */ | |
1055 | css_get(&memcg->css); | |
1056 | return memcg; | |
1057 | } | |
1058 | mm = current->mm; | |
1059 | if (unlikely(!mm)) | |
1060 | return root_mem_cgroup; | |
1061 | } | |
2884b6b7 | 1062 | |
54595fe2 KH |
1063 | rcu_read_lock(); |
1064 | do { | |
2884b6b7 MS |
1065 | memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
1066 | if (unlikely(!memcg)) | |
df381975 | 1067 | memcg = root_mem_cgroup; |
00d484f3 | 1068 | } while (!css_tryget(&memcg->css)); |
54595fe2 | 1069 | rcu_read_unlock(); |
c0ff4b85 | 1070 | return memcg; |
54595fe2 | 1071 | } |
d46eb14b SB |
1072 | EXPORT_SYMBOL(get_mem_cgroup_from_mm); |
1073 | ||
4127c650 RG |
1074 | static __always_inline bool memcg_kmem_bypass(void) |
1075 | { | |
1076 | /* Allow remote memcg charging from any context. */ | |
1077 | if (unlikely(active_memcg())) | |
1078 | return false; | |
1079 | ||
1080 | /* Memcg to charge can't be determined. */ | |
6126891c | 1081 | if (!in_task() || !current->mm || (current->flags & PF_KTHREAD)) |
4127c650 RG |
1082 | return true; |
1083 | ||
1084 | return false; | |
1085 | } | |
1086 | ||
5660048c JW |
1087 | /** |
1088 | * mem_cgroup_iter - iterate over memory cgroup hierarchy | |
1089 | * @root: hierarchy root | |
1090 | * @prev: previously returned memcg, NULL on first invocation | |
1091 | * @reclaim: cookie for shared reclaim walks, NULL for full walks | |
1092 | * | |
1093 | * Returns references to children of the hierarchy below @root, or | |
1094 | * @root itself, or %NULL after a full round-trip. | |
1095 | * | |
1096 | * Caller must pass the return value in @prev on subsequent | |
1097 | * invocations for reference counting, or use mem_cgroup_iter_break() | |
1098 | * to cancel a hierarchy walk before the round-trip is complete. | |
1099 | * | |
05bdc520 ML |
1100 | * Reclaimers can specify a node in @reclaim to divide up the memcgs |
1101 | * in the hierarchy among all concurrent reclaimers operating on the | |
1102 | * same node. | |
5660048c | 1103 | */ |
694fbc0f | 1104 | struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, |
5660048c | 1105 | struct mem_cgroup *prev, |
694fbc0f | 1106 | struct mem_cgroup_reclaim_cookie *reclaim) |
14067bb3 | 1107 | { |
3f649ab7 | 1108 | struct mem_cgroup_reclaim_iter *iter; |
5ac8fb31 | 1109 | struct cgroup_subsys_state *css = NULL; |
9f3a0d09 | 1110 | struct mem_cgroup *memcg = NULL; |
5ac8fb31 | 1111 | struct mem_cgroup *pos = NULL; |
711d3d2c | 1112 | |
694fbc0f AM |
1113 | if (mem_cgroup_disabled()) |
1114 | return NULL; | |
5660048c | 1115 | |
9f3a0d09 JW |
1116 | if (!root) |
1117 | root = root_mem_cgroup; | |
7d74b06f | 1118 | |
542f85f9 | 1119 | rcu_read_lock(); |
5f578161 | 1120 | |
5ac8fb31 | 1121 | if (reclaim) { |
ef8f2327 | 1122 | struct mem_cgroup_per_node *mz; |
5ac8fb31 | 1123 | |
a3747b53 | 1124 | mz = root->nodeinfo[reclaim->pgdat->node_id]; |
9da83f3f | 1125 | iter = &mz->iter; |
5ac8fb31 | 1126 | |
a9320aae WY |
1127 | /* |
1128 | * On start, join the current reclaim iteration cycle. | |
1129 | * Exit when a concurrent walker completes it. | |
1130 | */ | |
1131 | if (!prev) | |
1132 | reclaim->generation = iter->generation; | |
1133 | else if (reclaim->generation != iter->generation) | |
5ac8fb31 JW |
1134 | goto out_unlock; |
1135 | ||
6df38689 | 1136 | while (1) { |
4db0c3c2 | 1137 | pos = READ_ONCE(iter->position); |
6df38689 VD |
1138 | if (!pos || css_tryget(&pos->css)) |
1139 | break; | |
5ac8fb31 | 1140 | /* |
6df38689 VD |
1141 | * css reference reached zero, so iter->position will |
1142 | * be cleared by ->css_released. However, we should not | |
1143 | * rely on this happening soon, because ->css_released | |
1144 | * is called from a work queue, and by busy-waiting we | |
1145 | * might block it. So we clear iter->position right | |
1146 | * away. | |
5ac8fb31 | 1147 | */ |
6df38689 VD |
1148 | (void)cmpxchg(&iter->position, pos, NULL); |
1149 | } | |
89d8330c WY |
1150 | } else if (prev) { |
1151 | pos = prev; | |
5ac8fb31 JW |
1152 | } |
1153 | ||
1154 | if (pos) | |
1155 | css = &pos->css; | |
1156 | ||
1157 | for (;;) { | |
1158 | css = css_next_descendant_pre(css, &root->css); | |
1159 | if (!css) { | |
1160 | /* | |
1161 | * Reclaimers share the hierarchy walk, and a | |
1162 | * new one might jump in right at the end of | |
1163 | * the hierarchy - make sure they see at least | |
1164 | * one group and restart from the beginning. | |
1165 | */ | |
1166 | if (!prev) | |
1167 | continue; | |
1168 | break; | |
527a5ec9 | 1169 | } |
7d74b06f | 1170 | |
5ac8fb31 JW |
1171 | /* |
1172 | * Verify the css and acquire a reference. The root | |
1173 | * is provided by the caller, so we know it's alive | |
1174 | * and kicking, and don't take an extra reference. | |
1175 | */ | |
41555dad WY |
1176 | if (css == &root->css || css_tryget(css)) { |
1177 | memcg = mem_cgroup_from_css(css); | |
0b8f73e1 | 1178 | break; |
41555dad | 1179 | } |
9f3a0d09 | 1180 | } |
5ac8fb31 JW |
1181 | |
1182 | if (reclaim) { | |
5ac8fb31 | 1183 | /* |
6df38689 VD |
1184 | * The position could have already been updated by a competing |
1185 | * thread, so check that the value hasn't changed since we read | |
1186 | * it to avoid reclaiming from the same cgroup twice. | |
5ac8fb31 | 1187 | */ |
6df38689 VD |
1188 | (void)cmpxchg(&iter->position, pos, memcg); |
1189 | ||
5ac8fb31 JW |
1190 | if (pos) |
1191 | css_put(&pos->css); | |
1192 | ||
1193 | if (!memcg) | |
1194 | iter->generation++; | |
9f3a0d09 | 1195 | } |
5ac8fb31 | 1196 | |
542f85f9 MH |
1197 | out_unlock: |
1198 | rcu_read_unlock(); | |
c40046f3 MH |
1199 | if (prev && prev != root) |
1200 | css_put(&prev->css); | |
1201 | ||
9f3a0d09 | 1202 | return memcg; |
14067bb3 | 1203 | } |
7d74b06f | 1204 | |
5660048c JW |
1205 | /** |
1206 | * mem_cgroup_iter_break - abort a hierarchy walk prematurely | |
1207 | * @root: hierarchy root | |
1208 | * @prev: last visited hierarchy member as returned by mem_cgroup_iter() | |
1209 | */ | |
1210 | void mem_cgroup_iter_break(struct mem_cgroup *root, | |
1211 | struct mem_cgroup *prev) | |
9f3a0d09 JW |
1212 | { |
1213 | if (!root) | |
1214 | root = root_mem_cgroup; | |
1215 | if (prev && prev != root) | |
1216 | css_put(&prev->css); | |
1217 | } | |
7d74b06f | 1218 | |
54a83d6b MC |
1219 | static void __invalidate_reclaim_iterators(struct mem_cgroup *from, |
1220 | struct mem_cgroup *dead_memcg) | |
6df38689 | 1221 | { |
6df38689 | 1222 | struct mem_cgroup_reclaim_iter *iter; |
ef8f2327 MG |
1223 | struct mem_cgroup_per_node *mz; |
1224 | int nid; | |
6df38689 | 1225 | |
54a83d6b | 1226 | for_each_node(nid) { |
a3747b53 | 1227 | mz = from->nodeinfo[nid]; |
9da83f3f YS |
1228 | iter = &mz->iter; |
1229 | cmpxchg(&iter->position, dead_memcg, NULL); | |
6df38689 VD |
1230 | } |
1231 | } | |
1232 | ||
54a83d6b MC |
1233 | static void invalidate_reclaim_iterators(struct mem_cgroup *dead_memcg) |
1234 | { | |
1235 | struct mem_cgroup *memcg = dead_memcg; | |
1236 | struct mem_cgroup *last; | |
1237 | ||
1238 | do { | |
1239 | __invalidate_reclaim_iterators(memcg, dead_memcg); | |
1240 | last = memcg; | |
1241 | } while ((memcg = parent_mem_cgroup(memcg))); | |
1242 | ||
1243 | /* | |
b8dd3ee9 | 1244 | * When cgroup1 non-hierarchy mode is used, |
54a83d6b MC |
1245 | * parent_mem_cgroup() does not walk all the way up to the |
1246 | * cgroup root (root_mem_cgroup). So we have to handle | |
1247 | * dead_memcg from cgroup root separately. | |
1248 | */ | |
7848ed62 | 1249 | if (!mem_cgroup_is_root(last)) |
54a83d6b MC |
1250 | __invalidate_reclaim_iterators(root_mem_cgroup, |
1251 | dead_memcg); | |
1252 | } | |
1253 | ||
7c5f64f8 VD |
1254 | /** |
1255 | * mem_cgroup_scan_tasks - iterate over tasks of a memory cgroup hierarchy | |
1256 | * @memcg: hierarchy root | |
1257 | * @fn: function to call for each task | |
1258 | * @arg: argument passed to @fn | |
1259 | * | |
1260 | * This function iterates over tasks attached to @memcg or to any of its | |
1261 | * descendants and calls @fn for each task. If @fn returns a non-zero | |
025b7799 Z |
1262 | * value, the function breaks the iteration loop. Otherwise, it will iterate |
1263 | * over all tasks and return 0. | |
7c5f64f8 VD |
1264 | * |
1265 | * This function must not be called for the root memory cgroup. | |
1266 | */ | |
025b7799 Z |
1267 | void mem_cgroup_scan_tasks(struct mem_cgroup *memcg, |
1268 | int (*fn)(struct task_struct *, void *), void *arg) | |
7c5f64f8 VD |
1269 | { |
1270 | struct mem_cgroup *iter; | |
1271 | int ret = 0; | |
1272 | ||
7848ed62 | 1273 | BUG_ON(mem_cgroup_is_root(memcg)); |
7c5f64f8 VD |
1274 | |
1275 | for_each_mem_cgroup_tree(iter, memcg) { | |
1276 | struct css_task_iter it; | |
1277 | struct task_struct *task; | |
1278 | ||
f168a9a5 | 1279 | css_task_iter_start(&iter->css, CSS_TASK_ITER_PROCS, &it); |
7c5f64f8 VD |
1280 | while (!ret && (task = css_task_iter_next(&it))) |
1281 | ret = fn(task, arg); | |
1282 | css_task_iter_end(&it); | |
1283 | if (ret) { | |
1284 | mem_cgroup_iter_break(memcg, iter); | |
1285 | break; | |
1286 | } | |
1287 | } | |
7c5f64f8 VD |
1288 | } |
1289 | ||
6168d0da | 1290 | #ifdef CONFIG_DEBUG_VM |
e809c3fe | 1291 | void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio) |
6168d0da AS |
1292 | { |
1293 | struct mem_cgroup *memcg; | |
1294 | ||
1295 | if (mem_cgroup_disabled()) | |
1296 | return; | |
1297 | ||
e809c3fe | 1298 | memcg = folio_memcg(folio); |
6168d0da AS |
1299 | |
1300 | if (!memcg) | |
7848ed62 | 1301 | VM_BUG_ON_FOLIO(!mem_cgroup_is_root(lruvec_memcg(lruvec)), folio); |
6168d0da | 1302 | else |
e809c3fe | 1303 | VM_BUG_ON_FOLIO(lruvec_memcg(lruvec) != memcg, folio); |
6168d0da AS |
1304 | } |
1305 | #endif | |
1306 | ||
6168d0da | 1307 | /** |
e809c3fe MWO |
1308 | * folio_lruvec_lock - Lock the lruvec for a folio. |
1309 | * @folio: Pointer to the folio. | |
6168d0da | 1310 | * |
d7e3aba5 | 1311 | * These functions are safe to use under any of the following conditions: |
e809c3fe MWO |
1312 | * - folio locked |
1313 | * - folio_test_lru false | |
1314 | * - folio_memcg_lock() | |
1315 | * - folio frozen (refcount of 0) | |
1316 | * | |
1317 | * Return: The lruvec this folio is on with its lock held. | |
6168d0da | 1318 | */ |
e809c3fe | 1319 | struct lruvec *folio_lruvec_lock(struct folio *folio) |
6168d0da | 1320 | { |
e809c3fe | 1321 | struct lruvec *lruvec = folio_lruvec(folio); |
6168d0da | 1322 | |
6168d0da | 1323 | spin_lock(&lruvec->lru_lock); |
e809c3fe | 1324 | lruvec_memcg_debug(lruvec, folio); |
6168d0da AS |
1325 | |
1326 | return lruvec; | |
1327 | } | |
1328 | ||
e809c3fe MWO |
1329 | /** |
1330 | * folio_lruvec_lock_irq - Lock the lruvec for a folio. | |
1331 | * @folio: Pointer to the folio. | |
1332 | * | |
1333 | * These functions are safe to use under any of the following conditions: | |
1334 | * - folio locked | |
1335 | * - folio_test_lru false | |
1336 | * - folio_memcg_lock() | |
1337 | * - folio frozen (refcount of 0) | |
1338 | * | |
1339 | * Return: The lruvec this folio is on with its lock held and interrupts | |
1340 | * disabled. | |
1341 | */ | |
1342 | struct lruvec *folio_lruvec_lock_irq(struct folio *folio) | |
6168d0da | 1343 | { |
e809c3fe | 1344 | struct lruvec *lruvec = folio_lruvec(folio); |
6168d0da | 1345 | |
6168d0da | 1346 | spin_lock_irq(&lruvec->lru_lock); |
e809c3fe | 1347 | lruvec_memcg_debug(lruvec, folio); |
6168d0da AS |
1348 | |
1349 | return lruvec; | |
1350 | } | |
1351 | ||
e809c3fe MWO |
1352 | /** |
1353 | * folio_lruvec_lock_irqsave - Lock the lruvec for a folio. | |
1354 | * @folio: Pointer to the folio. | |
1355 | * @flags: Pointer to irqsave flags. | |
1356 | * | |
1357 | * These functions are safe to use under any of the following conditions: | |
1358 | * - folio locked | |
1359 | * - folio_test_lru false | |
1360 | * - folio_memcg_lock() | |
1361 | * - folio frozen (refcount of 0) | |
1362 | * | |
1363 | * Return: The lruvec this folio is on with its lock held and interrupts | |
1364 | * disabled. | |
1365 | */ | |
1366 | struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio, | |
1367 | unsigned long *flags) | |
6168d0da | 1368 | { |
e809c3fe | 1369 | struct lruvec *lruvec = folio_lruvec(folio); |
6168d0da | 1370 | |
6168d0da | 1371 | spin_lock_irqsave(&lruvec->lru_lock, *flags); |
e809c3fe | 1372 | lruvec_memcg_debug(lruvec, folio); |
6168d0da AS |
1373 | |
1374 | return lruvec; | |
1375 | } | |
1376 | ||
925b7673 | 1377 | /** |
fa9add64 HD |
1378 | * mem_cgroup_update_lru_size - account for adding or removing an lru page |
1379 | * @lruvec: mem_cgroup per zone lru vector | |
1380 | * @lru: index of lru list the page is sitting on | |
b4536f0c | 1381 | * @zid: zone id of the accounted pages |
fa9add64 | 1382 | * @nr_pages: positive when adding or negative when removing |
925b7673 | 1383 | * |
ca707239 | 1384 | * This function must be called under lru_lock, just before a page is added |
07ca7606 | 1385 | * to or just after a page is removed from an lru list. |
3f58a829 | 1386 | */ |
fa9add64 | 1387 | void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru, |
b4536f0c | 1388 | int zid, int nr_pages) |
3f58a829 | 1389 | { |
ef8f2327 | 1390 | struct mem_cgroup_per_node *mz; |
fa9add64 | 1391 | unsigned long *lru_size; |
ca707239 | 1392 | long size; |
3f58a829 MK |
1393 | |
1394 | if (mem_cgroup_disabled()) | |
1395 | return; | |
1396 | ||
ef8f2327 | 1397 | mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec); |
b4536f0c | 1398 | lru_size = &mz->lru_zone_size[zid][lru]; |
ca707239 HD |
1399 | |
1400 | if (nr_pages < 0) | |
1401 | *lru_size += nr_pages; | |
1402 | ||
1403 | size = *lru_size; | |
b4536f0c MH |
1404 | if (WARN_ONCE(size < 0, |
1405 | "%s(%p, %d, %d): lru_size %ld\n", | |
1406 | __func__, lruvec, lru, nr_pages, size)) { | |
ca707239 HD |
1407 | VM_BUG_ON(1); |
1408 | *lru_size = 0; | |
1409 | } | |
1410 | ||
1411 | if (nr_pages > 0) | |
1412 | *lru_size += nr_pages; | |
08e552c6 | 1413 | } |
544122e5 | 1414 | |
19942822 | 1415 | /** |
9d11ea9f | 1416 | * mem_cgroup_margin - calculate chargeable space of a memory cgroup |
dad7557e | 1417 | * @memcg: the memory cgroup |
19942822 | 1418 | * |
9d11ea9f | 1419 | * Returns the maximum amount of memory @mem can be charged with, in |
7ec99d62 | 1420 | * pages. |
19942822 | 1421 | */ |
c0ff4b85 | 1422 | static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg) |
19942822 | 1423 | { |
3e32cb2e JW |
1424 | unsigned long margin = 0; |
1425 | unsigned long count; | |
1426 | unsigned long limit; | |
9d11ea9f | 1427 | |
3e32cb2e | 1428 | count = page_counter_read(&memcg->memory); |
bbec2e15 | 1429 | limit = READ_ONCE(memcg->memory.max); |
3e32cb2e JW |
1430 | if (count < limit) |
1431 | margin = limit - count; | |
1432 | ||
7941d214 | 1433 | if (do_memsw_account()) { |
3e32cb2e | 1434 | count = page_counter_read(&memcg->memsw); |
bbec2e15 | 1435 | limit = READ_ONCE(memcg->memsw.max); |
1c4448ed | 1436 | if (count < limit) |
3e32cb2e | 1437 | margin = min(margin, limit - count); |
cbedbac3 LR |
1438 | else |
1439 | margin = 0; | |
3e32cb2e JW |
1440 | } |
1441 | ||
1442 | return margin; | |
19942822 JW |
1443 | } |
1444 | ||
32047e2a | 1445 | /* |
bdcbb659 | 1446 | * A routine for checking "mem" is under move_account() or not. |
32047e2a | 1447 | * |
bdcbb659 QH |
1448 | * Checking a cgroup is mc.from or mc.to or under hierarchy of |
1449 | * moving cgroups. This is for waiting at high-memory pressure | |
1450 | * caused by "move". | |
32047e2a | 1451 | */ |
c0ff4b85 | 1452 | static bool mem_cgroup_under_move(struct mem_cgroup *memcg) |
4b534334 | 1453 | { |
2bd9bb20 KH |
1454 | struct mem_cgroup *from; |
1455 | struct mem_cgroup *to; | |
4b534334 | 1456 | bool ret = false; |
2bd9bb20 KH |
1457 | /* |
1458 | * Unlike task_move routines, we access mc.to, mc.from not under | |
1459 | * mutual exclusion by cgroup_mutex. Here, we take spinlock instead. | |
1460 | */ | |
1461 | spin_lock(&mc.lock); | |
1462 | from = mc.from; | |
1463 | to = mc.to; | |
1464 | if (!from) | |
1465 | goto unlock; | |
3e92041d | 1466 | |
2314b42d JW |
1467 | ret = mem_cgroup_is_descendant(from, memcg) || |
1468 | mem_cgroup_is_descendant(to, memcg); | |
2bd9bb20 KH |
1469 | unlock: |
1470 | spin_unlock(&mc.lock); | |
4b534334 KH |
1471 | return ret; |
1472 | } | |
1473 | ||
c0ff4b85 | 1474 | static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg) |
4b534334 KH |
1475 | { |
1476 | if (mc.moving_task && current != mc.moving_task) { | |
c0ff4b85 | 1477 | if (mem_cgroup_under_move(memcg)) { |
4b534334 KH |
1478 | DEFINE_WAIT(wait); |
1479 | prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE); | |
1480 | /* moving charge context might have finished. */ | |
1481 | if (mc.moving_task) | |
1482 | schedule(); | |
1483 | finish_wait(&mc.waitq, &wait); | |
1484 | return true; | |
1485 | } | |
1486 | } | |
1487 | return false; | |
1488 | } | |
1489 | ||
5f9a4f4a MS |
1490 | struct memory_stat { |
1491 | const char *name; | |
5f9a4f4a MS |
1492 | unsigned int idx; |
1493 | }; | |
1494 | ||
57b2847d | 1495 | static const struct memory_stat memory_stats[] = { |
fff66b79 MS |
1496 | { "anon", NR_ANON_MAPPED }, |
1497 | { "file", NR_FILE_PAGES }, | |
a8c49af3 | 1498 | { "kernel", MEMCG_KMEM }, |
fff66b79 MS |
1499 | { "kernel_stack", NR_KERNEL_STACK_KB }, |
1500 | { "pagetables", NR_PAGETABLE }, | |
ebc97a52 | 1501 | { "sec_pagetables", NR_SECONDARY_PAGETABLE }, |
fff66b79 MS |
1502 | { "percpu", MEMCG_PERCPU_B }, |
1503 | { "sock", MEMCG_SOCK }, | |
4e5aa1f4 | 1504 | { "vmalloc", MEMCG_VMALLOC }, |
fff66b79 | 1505 | { "shmem", NR_SHMEM }, |
f4840ccf JW |
1506 | #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) |
1507 | { "zswap", MEMCG_ZSWAP_B }, | |
1508 | { "zswapped", MEMCG_ZSWAPPED }, | |
1509 | #endif | |
fff66b79 MS |
1510 | { "file_mapped", NR_FILE_MAPPED }, |
1511 | { "file_dirty", NR_FILE_DIRTY }, | |
1512 | { "file_writeback", NR_WRITEBACK }, | |
b6038942 SB |
1513 | #ifdef CONFIG_SWAP |
1514 | { "swapcached", NR_SWAPCACHE }, | |
1515 | #endif | |
5f9a4f4a | 1516 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
fff66b79 MS |
1517 | { "anon_thp", NR_ANON_THPS }, |
1518 | { "file_thp", NR_FILE_THPS }, | |
1519 | { "shmem_thp", NR_SHMEM_THPS }, | |
5f9a4f4a | 1520 | #endif |
fff66b79 MS |
1521 | { "inactive_anon", NR_INACTIVE_ANON }, |
1522 | { "active_anon", NR_ACTIVE_ANON }, | |
1523 | { "inactive_file", NR_INACTIVE_FILE }, | |
1524 | { "active_file", NR_ACTIVE_FILE }, | |
1525 | { "unevictable", NR_UNEVICTABLE }, | |
1526 | { "slab_reclaimable", NR_SLAB_RECLAIMABLE_B }, | |
1527 | { "slab_unreclaimable", NR_SLAB_UNRECLAIMABLE_B }, | |
5f9a4f4a MS |
1528 | |
1529 | /* The memory events */ | |
fff66b79 MS |
1530 | { "workingset_refault_anon", WORKINGSET_REFAULT_ANON }, |
1531 | { "workingset_refault_file", WORKINGSET_REFAULT_FILE }, | |
1532 | { "workingset_activate_anon", WORKINGSET_ACTIVATE_ANON }, | |
1533 | { "workingset_activate_file", WORKINGSET_ACTIVATE_FILE }, | |
1534 | { "workingset_restore_anon", WORKINGSET_RESTORE_ANON }, | |
1535 | { "workingset_restore_file", WORKINGSET_RESTORE_FILE }, | |
1536 | { "workingset_nodereclaim", WORKINGSET_NODERECLAIM }, | |
5f9a4f4a MS |
1537 | }; |
1538 | ||
fff66b79 MS |
1539 | /* Translate stat items to the correct unit for memory.stat output */ |
1540 | static int memcg_page_state_unit(int item) | |
1541 | { | |
1542 | switch (item) { | |
1543 | case MEMCG_PERCPU_B: | |
f4840ccf | 1544 | case MEMCG_ZSWAP_B: |
fff66b79 MS |
1545 | case NR_SLAB_RECLAIMABLE_B: |
1546 | case NR_SLAB_UNRECLAIMABLE_B: | |
1547 | case WORKINGSET_REFAULT_ANON: | |
1548 | case WORKINGSET_REFAULT_FILE: | |
1549 | case WORKINGSET_ACTIVATE_ANON: | |
1550 | case WORKINGSET_ACTIVATE_FILE: | |
1551 | case WORKINGSET_RESTORE_ANON: | |
1552 | case WORKINGSET_RESTORE_FILE: | |
1553 | case WORKINGSET_NODERECLAIM: | |
1554 | return 1; | |
1555 | case NR_KERNEL_STACK_KB: | |
1556 | return SZ_1K; | |
1557 | default: | |
1558 | return PAGE_SIZE; | |
1559 | } | |
1560 | } | |
1561 | ||
1562 | static inline unsigned long memcg_page_state_output(struct mem_cgroup *memcg, | |
1563 | int item) | |
1564 | { | |
1565 | return memcg_page_state(memcg, item) * memcg_page_state_unit(item); | |
1566 | } | |
1567 | ||
dddb44ff | 1568 | static void memcg_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) |
c8713d0b | 1569 | { |
c8713d0b | 1570 | int i; |
71cd3113 | 1571 | |
c8713d0b JW |
1572 | /* |
1573 | * Provide statistics on the state of the memory subsystem as | |
1574 | * well as cumulative event counters that show past behavior. | |
1575 | * | |
1576 | * This list is ordered following a combination of these gradients: | |
1577 | * 1) generic big picture -> specifics and details | |
1578 | * 2) reflecting userspace activity -> reflecting kernel heuristics | |
1579 | * | |
1580 | * Current memory state: | |
1581 | */ | |
fd25a9e0 | 1582 | mem_cgroup_flush_stats(); |
c8713d0b | 1583 | |
5f9a4f4a MS |
1584 | for (i = 0; i < ARRAY_SIZE(memory_stats); i++) { |
1585 | u64 size; | |
c8713d0b | 1586 | |
fff66b79 | 1587 | size = memcg_page_state_output(memcg, memory_stats[i].idx); |
5b42360c | 1588 | seq_buf_printf(s, "%s %llu\n", memory_stats[i].name, size); |
c8713d0b | 1589 | |
5f9a4f4a | 1590 | if (unlikely(memory_stats[i].idx == NR_SLAB_UNRECLAIMABLE_B)) { |
fff66b79 MS |
1591 | size += memcg_page_state_output(memcg, |
1592 | NR_SLAB_RECLAIMABLE_B); | |
5b42360c | 1593 | seq_buf_printf(s, "slab %llu\n", size); |
5f9a4f4a MS |
1594 | } |
1595 | } | |
c8713d0b JW |
1596 | |
1597 | /* Accumulated memory events */ | |
5b42360c | 1598 | seq_buf_printf(s, "pgscan %lu\n", |
c8713d0b | 1599 | memcg_events(memcg, PGSCAN_KSWAPD) + |
57e9cc50 JW |
1600 | memcg_events(memcg, PGSCAN_DIRECT) + |
1601 | memcg_events(memcg, PGSCAN_KHUGEPAGED)); | |
5b42360c | 1602 | seq_buf_printf(s, "pgsteal %lu\n", |
c8713d0b | 1603 | memcg_events(memcg, PGSTEAL_KSWAPD) + |
57e9cc50 JW |
1604 | memcg_events(memcg, PGSTEAL_DIRECT) + |
1605 | memcg_events(memcg, PGSTEAL_KHUGEPAGED)); | |
c8713d0b | 1606 | |
8278f1c7 SB |
1607 | for (i = 0; i < ARRAY_SIZE(memcg_vm_event_stat); i++) { |
1608 | if (memcg_vm_event_stat[i] == PGPGIN || | |
1609 | memcg_vm_event_stat[i] == PGPGOUT) | |
1610 | continue; | |
1611 | ||
5b42360c | 1612 | seq_buf_printf(s, "%s %lu\n", |
673520f8 QZ |
1613 | vm_event_name(memcg_vm_event_stat[i]), |
1614 | memcg_events(memcg, memcg_vm_event_stat[i])); | |
8278f1c7 | 1615 | } |
c8713d0b JW |
1616 | |
1617 | /* The above should easily fit into one page */ | |
5b42360c | 1618 | WARN_ON_ONCE(seq_buf_has_overflowed(s)); |
c8713d0b | 1619 | } |
71cd3113 | 1620 | |
dddb44ff YA |
1621 | static void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s); |
1622 | ||
1623 | static void memory_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) | |
1624 | { | |
1625 | if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) | |
1626 | memcg_stat_format(memcg, s); | |
1627 | else | |
1628 | memcg1_stat_format(memcg, s); | |
1629 | WARN_ON_ONCE(seq_buf_has_overflowed(s)); | |
1630 | } | |
1631 | ||
58cf188e | 1632 | #define K(x) ((x) << (PAGE_SHIFT-10)) |
e222432b | 1633 | /** |
f0c867d9 | 1634 | * mem_cgroup_print_oom_context: Print OOM information relevant to |
1635 | * memory controller. | |
e222432b BS |
1636 | * @memcg: The memory cgroup that went over limit |
1637 | * @p: Task that is going to be killed | |
1638 | * | |
1639 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is | |
1640 | * enabled | |
1641 | */ | |
f0c867d9 | 1642 | void mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p) |
e222432b | 1643 | { |
e222432b BS |
1644 | rcu_read_lock(); |
1645 | ||
f0c867d9 | 1646 | if (memcg) { |
1647 | pr_cont(",oom_memcg="); | |
1648 | pr_cont_cgroup_path(memcg->css.cgroup); | |
1649 | } else | |
1650 | pr_cont(",global_oom"); | |
2415b9f5 | 1651 | if (p) { |
f0c867d9 | 1652 | pr_cont(",task_memcg="); |
2415b9f5 | 1653 | pr_cont_cgroup_path(task_cgroup(p, memory_cgrp_id)); |
2415b9f5 | 1654 | } |
e222432b | 1655 | rcu_read_unlock(); |
f0c867d9 | 1656 | } |
1657 | ||
1658 | /** | |
1659 | * mem_cgroup_print_oom_meminfo: Print OOM memory information relevant to | |
1660 | * memory controller. | |
1661 | * @memcg: The memory cgroup that went over limit | |
1662 | */ | |
1663 | void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg) | |
1664 | { | |
68aaee14 TH |
1665 | /* Use static buffer, for the caller is holding oom_lock. */ |
1666 | static char buf[PAGE_SIZE]; | |
5b42360c | 1667 | struct seq_buf s; |
68aaee14 TH |
1668 | |
1669 | lockdep_assert_held(&oom_lock); | |
e222432b | 1670 | |
3e32cb2e JW |
1671 | pr_info("memory: usage %llukB, limit %llukB, failcnt %lu\n", |
1672 | K((u64)page_counter_read(&memcg->memory)), | |
15b42562 | 1673 | K((u64)READ_ONCE(memcg->memory.max)), memcg->memory.failcnt); |
c8713d0b JW |
1674 | if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) |
1675 | pr_info("swap: usage %llukB, limit %llukB, failcnt %lu\n", | |
1676 | K((u64)page_counter_read(&memcg->swap)), | |
32d087cd | 1677 | K((u64)READ_ONCE(memcg->swap.max)), memcg->swap.failcnt); |
c8713d0b JW |
1678 | else { |
1679 | pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %lu\n", | |
1680 | K((u64)page_counter_read(&memcg->memsw)), | |
1681 | K((u64)memcg->memsw.max), memcg->memsw.failcnt); | |
1682 | pr_info("kmem: usage %llukB, limit %llukB, failcnt %lu\n", | |
1683 | K((u64)page_counter_read(&memcg->kmem)), | |
1684 | K((u64)memcg->kmem.max), memcg->kmem.failcnt); | |
58cf188e | 1685 | } |
c8713d0b JW |
1686 | |
1687 | pr_info("Memory cgroup stats for "); | |
1688 | pr_cont_cgroup_path(memcg->css.cgroup); | |
1689 | pr_cont(":"); | |
5b42360c YA |
1690 | seq_buf_init(&s, buf, sizeof(buf)); |
1691 | memory_stat_format(memcg, &s); | |
1692 | seq_buf_do_printk(&s, KERN_INFO); | |
e222432b BS |
1693 | } |
1694 | ||
a63d83f4 DR |
1695 | /* |
1696 | * Return the memory (and swap, if configured) limit for a memcg. | |
1697 | */ | |
bbec2e15 | 1698 | unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg) |
a63d83f4 | 1699 | { |
8d387a5f WL |
1700 | unsigned long max = READ_ONCE(memcg->memory.max); |
1701 | ||
b94c4e94 | 1702 | if (do_memsw_account()) { |
8d387a5f WL |
1703 | if (mem_cgroup_swappiness(memcg)) { |
1704 | /* Calculate swap excess capacity from memsw limit */ | |
1705 | unsigned long swap = READ_ONCE(memcg->memsw.max) - max; | |
1706 | ||
1707 | max += min(swap, (unsigned long)total_swap_pages); | |
1708 | } | |
b94c4e94 JW |
1709 | } else { |
1710 | if (mem_cgroup_swappiness(memcg)) | |
1711 | max += min(READ_ONCE(memcg->swap.max), | |
1712 | (unsigned long)total_swap_pages); | |
9a5a8f19 | 1713 | } |
bbec2e15 | 1714 | return max; |
a63d83f4 DR |
1715 | } |
1716 | ||
9783aa99 CD |
1717 | unsigned long mem_cgroup_size(struct mem_cgroup *memcg) |
1718 | { | |
1719 | return page_counter_read(&memcg->memory); | |
1720 | } | |
1721 | ||
b6e6edcf | 1722 | static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, |
19965460 | 1723 | int order) |
9cbb78bb | 1724 | { |
6e0fc46d DR |
1725 | struct oom_control oc = { |
1726 | .zonelist = NULL, | |
1727 | .nodemask = NULL, | |
2a966b77 | 1728 | .memcg = memcg, |
6e0fc46d DR |
1729 | .gfp_mask = gfp_mask, |
1730 | .order = order, | |
6e0fc46d | 1731 | }; |
1378b37d | 1732 | bool ret = true; |
9cbb78bb | 1733 | |
7775face TH |
1734 | if (mutex_lock_killable(&oom_lock)) |
1735 | return true; | |
1378b37d YS |
1736 | |
1737 | if (mem_cgroup_margin(memcg) >= (1 << order)) | |
1738 | goto unlock; | |
1739 | ||
7775face TH |
1740 | /* |
1741 | * A few threads which were not waiting at mutex_lock_killable() can | |
1742 | * fail to bail out. Therefore, check again after holding oom_lock. | |
1743 | */ | |
a4ebf1b6 | 1744 | ret = task_is_dying() || out_of_memory(&oc); |
1378b37d YS |
1745 | |
1746 | unlock: | |
dc56401f | 1747 | mutex_unlock(&oom_lock); |
7c5f64f8 | 1748 | return ret; |
9cbb78bb DR |
1749 | } |
1750 | ||
0608f43d | 1751 | static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg, |
ef8f2327 | 1752 | pg_data_t *pgdat, |
0608f43d AM |
1753 | gfp_t gfp_mask, |
1754 | unsigned long *total_scanned) | |
1755 | { | |
1756 | struct mem_cgroup *victim = NULL; | |
1757 | int total = 0; | |
1758 | int loop = 0; | |
1759 | unsigned long excess; | |
1760 | unsigned long nr_scanned; | |
1761 | struct mem_cgroup_reclaim_cookie reclaim = { | |
ef8f2327 | 1762 | .pgdat = pgdat, |
0608f43d AM |
1763 | }; |
1764 | ||
3e32cb2e | 1765 | excess = soft_limit_excess(root_memcg); |
0608f43d AM |
1766 | |
1767 | while (1) { | |
1768 | victim = mem_cgroup_iter(root_memcg, victim, &reclaim); | |
1769 | if (!victim) { | |
1770 | loop++; | |
1771 | if (loop >= 2) { | |
1772 | /* | |
1773 | * If we have not been able to reclaim | |
1774 | * anything, it might because there are | |
1775 | * no reclaimable pages under this hierarchy | |
1776 | */ | |
1777 | if (!total) | |
1778 | break; | |
1779 | /* | |
1780 | * We want to do more targeted reclaim. | |
1781 | * excess >> 2 is not to excessive so as to | |
1782 | * reclaim too much, nor too less that we keep | |
1783 | * coming back to reclaim from this cgroup | |
1784 | */ | |
1785 | if (total >= (excess >> 2) || | |
1786 | (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) | |
1787 | break; | |
1788 | } | |
1789 | continue; | |
1790 | } | |
a9dd0a83 | 1791 | total += mem_cgroup_shrink_node(victim, gfp_mask, false, |
ef8f2327 | 1792 | pgdat, &nr_scanned); |
0608f43d | 1793 | *total_scanned += nr_scanned; |
3e32cb2e | 1794 | if (!soft_limit_excess(root_memcg)) |
0608f43d | 1795 | break; |
6d61ef40 | 1796 | } |
0608f43d AM |
1797 | mem_cgroup_iter_break(root_memcg, victim); |
1798 | return total; | |
6d61ef40 BS |
1799 | } |
1800 | ||
0056f4e6 JW |
1801 | #ifdef CONFIG_LOCKDEP |
1802 | static struct lockdep_map memcg_oom_lock_dep_map = { | |
1803 | .name = "memcg_oom_lock", | |
1804 | }; | |
1805 | #endif | |
1806 | ||
fb2a6fc5 JW |
1807 | static DEFINE_SPINLOCK(memcg_oom_lock); |
1808 | ||
867578cb KH |
1809 | /* |
1810 | * Check OOM-Killer is already running under our hierarchy. | |
1811 | * If someone is running, return false. | |
1812 | */ | |
fb2a6fc5 | 1813 | static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg) |
867578cb | 1814 | { |
79dfdacc | 1815 | struct mem_cgroup *iter, *failed = NULL; |
a636b327 | 1816 | |
fb2a6fc5 JW |
1817 | spin_lock(&memcg_oom_lock); |
1818 | ||
9f3a0d09 | 1819 | for_each_mem_cgroup_tree(iter, memcg) { |
23751be0 | 1820 | if (iter->oom_lock) { |
79dfdacc MH |
1821 | /* |
1822 | * this subtree of our hierarchy is already locked | |
1823 | * so we cannot give a lock. | |
1824 | */ | |
79dfdacc | 1825 | failed = iter; |
9f3a0d09 JW |
1826 | mem_cgroup_iter_break(memcg, iter); |
1827 | break; | |
23751be0 JW |
1828 | } else |
1829 | iter->oom_lock = true; | |
7d74b06f | 1830 | } |
867578cb | 1831 | |
fb2a6fc5 JW |
1832 | if (failed) { |
1833 | /* | |
1834 | * OK, we failed to lock the whole subtree so we have | |
1835 | * to clean up what we set up to the failing subtree | |
1836 | */ | |
1837 | for_each_mem_cgroup_tree(iter, memcg) { | |
1838 | if (iter == failed) { | |
1839 | mem_cgroup_iter_break(memcg, iter); | |
1840 | break; | |
1841 | } | |
1842 | iter->oom_lock = false; | |
79dfdacc | 1843 | } |
0056f4e6 JW |
1844 | } else |
1845 | mutex_acquire(&memcg_oom_lock_dep_map, 0, 1, _RET_IP_); | |
fb2a6fc5 JW |
1846 | |
1847 | spin_unlock(&memcg_oom_lock); | |
1848 | ||
1849 | return !failed; | |
a636b327 | 1850 | } |
0b7f569e | 1851 | |
fb2a6fc5 | 1852 | static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg) |
0b7f569e | 1853 | { |
7d74b06f KH |
1854 | struct mem_cgroup *iter; |
1855 | ||
fb2a6fc5 | 1856 | spin_lock(&memcg_oom_lock); |
5facae4f | 1857 | mutex_release(&memcg_oom_lock_dep_map, _RET_IP_); |
c0ff4b85 | 1858 | for_each_mem_cgroup_tree(iter, memcg) |
79dfdacc | 1859 | iter->oom_lock = false; |
fb2a6fc5 | 1860 | spin_unlock(&memcg_oom_lock); |
79dfdacc MH |
1861 | } |
1862 | ||
c0ff4b85 | 1863 | static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg) |
79dfdacc MH |
1864 | { |
1865 | struct mem_cgroup *iter; | |
1866 | ||
c2b42d3c | 1867 | spin_lock(&memcg_oom_lock); |
c0ff4b85 | 1868 | for_each_mem_cgroup_tree(iter, memcg) |
c2b42d3c TH |
1869 | iter->under_oom++; |
1870 | spin_unlock(&memcg_oom_lock); | |
79dfdacc MH |
1871 | } |
1872 | ||
c0ff4b85 | 1873 | static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg) |
79dfdacc MH |
1874 | { |
1875 | struct mem_cgroup *iter; | |
1876 | ||
867578cb | 1877 | /* |
f0953a1b | 1878 | * Be careful about under_oom underflows because a child memcg |
7a52d4d8 | 1879 | * could have been added after mem_cgroup_mark_under_oom. |
867578cb | 1880 | */ |
c2b42d3c | 1881 | spin_lock(&memcg_oom_lock); |
c0ff4b85 | 1882 | for_each_mem_cgroup_tree(iter, memcg) |
c2b42d3c TH |
1883 | if (iter->under_oom > 0) |
1884 | iter->under_oom--; | |
1885 | spin_unlock(&memcg_oom_lock); | |
0b7f569e KH |
1886 | } |
1887 | ||
867578cb KH |
1888 | static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq); |
1889 | ||
dc98df5a | 1890 | struct oom_wait_info { |
d79154bb | 1891 | struct mem_cgroup *memcg; |
ac6424b9 | 1892 | wait_queue_entry_t wait; |
dc98df5a KH |
1893 | }; |
1894 | ||
ac6424b9 | 1895 | static int memcg_oom_wake_function(wait_queue_entry_t *wait, |
dc98df5a KH |
1896 | unsigned mode, int sync, void *arg) |
1897 | { | |
d79154bb HD |
1898 | struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg; |
1899 | struct mem_cgroup *oom_wait_memcg; | |
dc98df5a KH |
1900 | struct oom_wait_info *oom_wait_info; |
1901 | ||
1902 | oom_wait_info = container_of(wait, struct oom_wait_info, wait); | |
d79154bb | 1903 | oom_wait_memcg = oom_wait_info->memcg; |
dc98df5a | 1904 | |
2314b42d JW |
1905 | if (!mem_cgroup_is_descendant(wake_memcg, oom_wait_memcg) && |
1906 | !mem_cgroup_is_descendant(oom_wait_memcg, wake_memcg)) | |
dc98df5a | 1907 | return 0; |
dc98df5a KH |
1908 | return autoremove_wake_function(wait, mode, sync, arg); |
1909 | } | |
1910 | ||
c0ff4b85 | 1911 | static void memcg_oom_recover(struct mem_cgroup *memcg) |
3c11ecf4 | 1912 | { |
c2b42d3c TH |
1913 | /* |
1914 | * For the following lockless ->under_oom test, the only required | |
1915 | * guarantee is that it must see the state asserted by an OOM when | |
1916 | * this function is called as a result of userland actions | |
1917 | * triggered by the notification of the OOM. This is trivially | |
1918 | * achieved by invoking mem_cgroup_mark_under_oom() before | |
1919 | * triggering notification. | |
1920 | */ | |
1921 | if (memcg && memcg->under_oom) | |
f4b90b70 | 1922 | __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg); |
3c11ecf4 KH |
1923 | } |
1924 | ||
becdf89d SB |
1925 | /* |
1926 | * Returns true if successfully killed one or more processes. Though in some | |
1927 | * corner cases it can return true even without killing any process. | |
1928 | */ | |
1929 | static bool mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order) | |
0b7f569e | 1930 | { |
becdf89d | 1931 | bool locked, ret; |
7056d3a3 | 1932 | |
29ef680a | 1933 | if (order > PAGE_ALLOC_COSTLY_ORDER) |
becdf89d | 1934 | return false; |
29ef680a | 1935 | |
7a1adfdd RG |
1936 | memcg_memory_event(memcg, MEMCG_OOM); |
1937 | ||
867578cb | 1938 | /* |
49426420 JW |
1939 | * We are in the middle of the charge context here, so we |
1940 | * don't want to block when potentially sitting on a callstack | |
1941 | * that holds all kinds of filesystem and mm locks. | |
1942 | * | |
29ef680a MH |
1943 | * cgroup1 allows disabling the OOM killer and waiting for outside |
1944 | * handling until the charge can succeed; remember the context and put | |
1945 | * the task to sleep at the end of the page fault when all locks are | |
1946 | * released. | |
49426420 | 1947 | * |
29ef680a MH |
1948 | * On the other hand, in-kernel OOM killer allows for an async victim |
1949 | * memory reclaim (oom_reaper) and that means that we are not solely | |
1950 | * relying on the oom victim to make a forward progress and we can | |
1951 | * invoke the oom killer here. | |
1952 | * | |
1953 | * Please note that mem_cgroup_out_of_memory might fail to find a | |
1954 | * victim and then we have to bail out from the charge path. | |
867578cb | 1955 | */ |
17c56de6 | 1956 | if (READ_ONCE(memcg->oom_kill_disable)) { |
becdf89d SB |
1957 | if (current->in_user_fault) { |
1958 | css_get(&memcg->css); | |
1959 | current->memcg_in_oom = memcg; | |
1960 | current->memcg_oom_gfp_mask = mask; | |
1961 | current->memcg_oom_order = order; | |
1962 | } | |
1963 | return false; | |
29ef680a MH |
1964 | } |
1965 | ||
7056d3a3 MH |
1966 | mem_cgroup_mark_under_oom(memcg); |
1967 | ||
1968 | locked = mem_cgroup_oom_trylock(memcg); | |
1969 | ||
1970 | if (locked) | |
1971 | mem_cgroup_oom_notify(memcg); | |
1972 | ||
1973 | mem_cgroup_unmark_under_oom(memcg); | |
becdf89d | 1974 | ret = mem_cgroup_out_of_memory(memcg, mask, order); |
7056d3a3 MH |
1975 | |
1976 | if (locked) | |
1977 | mem_cgroup_oom_unlock(memcg); | |
29ef680a | 1978 | |
7056d3a3 | 1979 | return ret; |
3812c8c8 JW |
1980 | } |
1981 | ||
1982 | /** | |
1983 | * mem_cgroup_oom_synchronize - complete memcg OOM handling | |
49426420 | 1984 | * @handle: actually kill/wait or just clean up the OOM state |
3812c8c8 | 1985 | * |
49426420 JW |
1986 | * This has to be called at the end of a page fault if the memcg OOM |
1987 | * handler was enabled. | |
3812c8c8 | 1988 | * |
49426420 | 1989 | * Memcg supports userspace OOM handling where failed allocations must |
3812c8c8 JW |
1990 | * sleep on a waitqueue until the userspace task resolves the |
1991 | * situation. Sleeping directly in the charge context with all kinds | |
1992 | * of locks held is not a good idea, instead we remember an OOM state | |
1993 | * in the task and mem_cgroup_oom_synchronize() has to be called at | |
49426420 | 1994 | * the end of the page fault to complete the OOM handling. |
3812c8c8 JW |
1995 | * |
1996 | * Returns %true if an ongoing memcg OOM situation was detected and | |
49426420 | 1997 | * completed, %false otherwise. |
3812c8c8 | 1998 | */ |
49426420 | 1999 | bool mem_cgroup_oom_synchronize(bool handle) |
3812c8c8 | 2000 | { |
626ebc41 | 2001 | struct mem_cgroup *memcg = current->memcg_in_oom; |
3812c8c8 | 2002 | struct oom_wait_info owait; |
49426420 | 2003 | bool locked; |
3812c8c8 JW |
2004 | |
2005 | /* OOM is global, do not handle */ | |
3812c8c8 | 2006 | if (!memcg) |
49426420 | 2007 | return false; |
3812c8c8 | 2008 | |
7c5f64f8 | 2009 | if (!handle) |
49426420 | 2010 | goto cleanup; |
3812c8c8 JW |
2011 | |
2012 | owait.memcg = memcg; | |
2013 | owait.wait.flags = 0; | |
2014 | owait.wait.func = memcg_oom_wake_function; | |
2015 | owait.wait.private = current; | |
2055da97 | 2016 | INIT_LIST_HEAD(&owait.wait.entry); |
867578cb | 2017 | |
3812c8c8 | 2018 | prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE); |
49426420 JW |
2019 | mem_cgroup_mark_under_oom(memcg); |
2020 | ||
2021 | locked = mem_cgroup_oom_trylock(memcg); | |
2022 | ||
2023 | if (locked) | |
2024 | mem_cgroup_oom_notify(memcg); | |
2025 | ||
857f2139 HX |
2026 | schedule(); |
2027 | mem_cgroup_unmark_under_oom(memcg); | |
2028 | finish_wait(&memcg_oom_waitq, &owait.wait); | |
49426420 | 2029 | |
18b1d18b | 2030 | if (locked) |
fb2a6fc5 | 2031 | mem_cgroup_oom_unlock(memcg); |
49426420 | 2032 | cleanup: |
626ebc41 | 2033 | current->memcg_in_oom = NULL; |
3812c8c8 | 2034 | css_put(&memcg->css); |
867578cb | 2035 | return true; |
0b7f569e KH |
2036 | } |
2037 | ||
3d8b38eb RG |
2038 | /** |
2039 | * mem_cgroup_get_oom_group - get a memory cgroup to clean up after OOM | |
2040 | * @victim: task to be killed by the OOM killer | |
2041 | * @oom_domain: memcg in case of memcg OOM, NULL in case of system-wide OOM | |
2042 | * | |
2043 | * Returns a pointer to a memory cgroup, which has to be cleaned up | |
2044 | * by killing all belonging OOM-killable tasks. | |
2045 | * | |
2046 | * Caller has to call mem_cgroup_put() on the returned non-NULL memcg. | |
2047 | */ | |
2048 | struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim, | |
2049 | struct mem_cgroup *oom_domain) | |
2050 | { | |
2051 | struct mem_cgroup *oom_group = NULL; | |
2052 | struct mem_cgroup *memcg; | |
2053 | ||
2054 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) | |
2055 | return NULL; | |
2056 | ||
2057 | if (!oom_domain) | |
2058 | oom_domain = root_mem_cgroup; | |
2059 | ||
2060 | rcu_read_lock(); | |
2061 | ||
2062 | memcg = mem_cgroup_from_task(victim); | |
7848ed62 | 2063 | if (mem_cgroup_is_root(memcg)) |
3d8b38eb RG |
2064 | goto out; |
2065 | ||
48fe267c RG |
2066 | /* |
2067 | * If the victim task has been asynchronously moved to a different | |
2068 | * memory cgroup, we might end up killing tasks outside oom_domain. | |
2069 | * In this case it's better to ignore memory.group.oom. | |
2070 | */ | |
2071 | if (unlikely(!mem_cgroup_is_descendant(memcg, oom_domain))) | |
2072 | goto out; | |
2073 | ||
3d8b38eb RG |
2074 | /* |
2075 | * Traverse the memory cgroup hierarchy from the victim task's | |
2076 | * cgroup up to the OOMing cgroup (or root) to find the | |
2077 | * highest-level memory cgroup with oom.group set. | |
2078 | */ | |
2079 | for (; memcg; memcg = parent_mem_cgroup(memcg)) { | |
eaf7b66b | 2080 | if (READ_ONCE(memcg->oom_group)) |
3d8b38eb RG |
2081 | oom_group = memcg; |
2082 | ||
2083 | if (memcg == oom_domain) | |
2084 | break; | |
2085 | } | |
2086 | ||
2087 | if (oom_group) | |
2088 | css_get(&oom_group->css); | |
2089 | out: | |
2090 | rcu_read_unlock(); | |
2091 | ||
2092 | return oom_group; | |
2093 | } | |
2094 | ||
2095 | void mem_cgroup_print_oom_group(struct mem_cgroup *memcg) | |
2096 | { | |
2097 | pr_info("Tasks in "); | |
2098 | pr_cont_cgroup_path(memcg->css.cgroup); | |
2099 | pr_cont(" are going to be killed due to memory.oom.group set\n"); | |
2100 | } | |
2101 | ||
d7365e78 | 2102 | /** |
f70ad448 MWO |
2103 | * folio_memcg_lock - Bind a folio to its memcg. |
2104 | * @folio: The folio. | |
32047e2a | 2105 | * |
f70ad448 | 2106 | * This function prevents unlocked LRU folios from being moved to |
739f79fc JW |
2107 | * another cgroup. |
2108 | * | |
f70ad448 MWO |
2109 | * It ensures lifetime of the bound memcg. The caller is responsible |
2110 | * for the lifetime of the folio. | |
d69b042f | 2111 | */ |
f70ad448 | 2112 | void folio_memcg_lock(struct folio *folio) |
89c06bd5 KH |
2113 | { |
2114 | struct mem_cgroup *memcg; | |
6de22619 | 2115 | unsigned long flags; |
89c06bd5 | 2116 | |
6de22619 JW |
2117 | /* |
2118 | * The RCU lock is held throughout the transaction. The fast | |
2119 | * path can get away without acquiring the memcg->move_lock | |
2120 | * because page moving starts with an RCU grace period. | |
739f79fc | 2121 | */ |
d7365e78 JW |
2122 | rcu_read_lock(); |
2123 | ||
2124 | if (mem_cgroup_disabled()) | |
1c824a68 | 2125 | return; |
89c06bd5 | 2126 | again: |
f70ad448 | 2127 | memcg = folio_memcg(folio); |
29833315 | 2128 | if (unlikely(!memcg)) |
1c824a68 | 2129 | return; |
d7365e78 | 2130 | |
20ad50d6 AS |
2131 | #ifdef CONFIG_PROVE_LOCKING |
2132 | local_irq_save(flags); | |
2133 | might_lock(&memcg->move_lock); | |
2134 | local_irq_restore(flags); | |
2135 | #endif | |
2136 | ||
bdcbb659 | 2137 | if (atomic_read(&memcg->moving_account) <= 0) |
1c824a68 | 2138 | return; |
89c06bd5 | 2139 | |
6de22619 | 2140 | spin_lock_irqsave(&memcg->move_lock, flags); |
f70ad448 | 2141 | if (memcg != folio_memcg(folio)) { |
6de22619 | 2142 | spin_unlock_irqrestore(&memcg->move_lock, flags); |
89c06bd5 KH |
2143 | goto again; |
2144 | } | |
6de22619 JW |
2145 | |
2146 | /* | |
1c824a68 JW |
2147 | * When charge migration first begins, we can have multiple |
2148 | * critical sections holding the fast-path RCU lock and one | |
2149 | * holding the slowpath move_lock. Track the task who has the | |
6c77b607 | 2150 | * move_lock for folio_memcg_unlock(). |
6de22619 JW |
2151 | */ |
2152 | memcg->move_lock_task = current; | |
2153 | memcg->move_lock_flags = flags; | |
89c06bd5 | 2154 | } |
f70ad448 | 2155 | |
f70ad448 | 2156 | static void __folio_memcg_unlock(struct mem_cgroup *memcg) |
89c06bd5 | 2157 | { |
6de22619 JW |
2158 | if (memcg && memcg->move_lock_task == current) { |
2159 | unsigned long flags = memcg->move_lock_flags; | |
2160 | ||
2161 | memcg->move_lock_task = NULL; | |
2162 | memcg->move_lock_flags = 0; | |
2163 | ||
2164 | spin_unlock_irqrestore(&memcg->move_lock, flags); | |
2165 | } | |
89c06bd5 | 2166 | |
d7365e78 | 2167 | rcu_read_unlock(); |
89c06bd5 | 2168 | } |
739f79fc JW |
2169 | |
2170 | /** | |
f70ad448 MWO |
2171 | * folio_memcg_unlock - Release the binding between a folio and its memcg. |
2172 | * @folio: The folio. | |
2173 | * | |
2174 | * This releases the binding created by folio_memcg_lock(). This does | |
2175 | * not change the accounting of this folio to its memcg, but it does | |
2176 | * permit others to change it. | |
739f79fc | 2177 | */ |
f70ad448 | 2178 | void folio_memcg_unlock(struct folio *folio) |
739f79fc | 2179 | { |
f70ad448 MWO |
2180 | __folio_memcg_unlock(folio_memcg(folio)); |
2181 | } | |
9da7b521 | 2182 | |
fead2b86 | 2183 | struct memcg_stock_pcp { |
56751146 | 2184 | local_lock_t stock_lock; |
fead2b86 MH |
2185 | struct mem_cgroup *cached; /* this never be root cgroup */ |
2186 | unsigned int nr_pages; | |
2187 | ||
bf4f0599 RG |
2188 | #ifdef CONFIG_MEMCG_KMEM |
2189 | struct obj_cgroup *cached_objcg; | |
68ac5b3c | 2190 | struct pglist_data *cached_pgdat; |
bf4f0599 | 2191 | unsigned int nr_bytes; |
68ac5b3c WL |
2192 | int nr_slab_reclaimable_b; |
2193 | int nr_slab_unreclaimable_b; | |
bf4f0599 RG |
2194 | #endif |
2195 | ||
cdec2e42 | 2196 | struct work_struct work; |
26fe6168 | 2197 | unsigned long flags; |
a0db00fc | 2198 | #define FLUSHING_CACHED_CHARGE 0 |
cdec2e42 | 2199 | }; |
56751146 SAS |
2200 | static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock) = { |
2201 | .stock_lock = INIT_LOCAL_LOCK(stock_lock), | |
2202 | }; | |
9f50fad6 | 2203 | static DEFINE_MUTEX(percpu_charge_mutex); |
cdec2e42 | 2204 | |
bf4f0599 | 2205 | #ifdef CONFIG_MEMCG_KMEM |
56751146 | 2206 | static struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock); |
bf4f0599 RG |
2207 | static bool obj_stock_flush_required(struct memcg_stock_pcp *stock, |
2208 | struct mem_cgroup *root_memcg); | |
a8c49af3 | 2209 | static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages); |
bf4f0599 RG |
2210 | |
2211 | #else | |
56751146 | 2212 | static inline struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock) |
bf4f0599 | 2213 | { |
56751146 | 2214 | return NULL; |
bf4f0599 RG |
2215 | } |
2216 | static bool obj_stock_flush_required(struct memcg_stock_pcp *stock, | |
2217 | struct mem_cgroup *root_memcg) | |
2218 | { | |
2219 | return false; | |
2220 | } | |
a8c49af3 YA |
2221 | static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages) |
2222 | { | |
2223 | } | |
bf4f0599 RG |
2224 | #endif |
2225 | ||
a0956d54 SS |
2226 | /** |
2227 | * consume_stock: Try to consume stocked charge on this cpu. | |
2228 | * @memcg: memcg to consume from. | |
2229 | * @nr_pages: how many pages to charge. | |
2230 | * | |
2231 | * The charges will only happen if @memcg matches the current cpu's memcg | |
2232 | * stock, and at least @nr_pages are available in that stock. Failure to | |
2233 | * service an allocation will refill the stock. | |
2234 | * | |
2235 | * returns true if successful, false otherwise. | |
cdec2e42 | 2236 | */ |
a0956d54 | 2237 | static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages) |
cdec2e42 KH |
2238 | { |
2239 | struct memcg_stock_pcp *stock; | |
db2ba40c | 2240 | unsigned long flags; |
3e32cb2e | 2241 | bool ret = false; |
cdec2e42 | 2242 | |
a983b5eb | 2243 | if (nr_pages > MEMCG_CHARGE_BATCH) |
3e32cb2e | 2244 | return ret; |
a0956d54 | 2245 | |
56751146 | 2246 | local_lock_irqsave(&memcg_stock.stock_lock, flags); |
db2ba40c JW |
2247 | |
2248 | stock = this_cpu_ptr(&memcg_stock); | |
f785a8f2 | 2249 | if (memcg == READ_ONCE(stock->cached) && stock->nr_pages >= nr_pages) { |
a0956d54 | 2250 | stock->nr_pages -= nr_pages; |
3e32cb2e JW |
2251 | ret = true; |
2252 | } | |
db2ba40c | 2253 | |
56751146 | 2254 | local_unlock_irqrestore(&memcg_stock.stock_lock, flags); |
db2ba40c | 2255 | |
cdec2e42 KH |
2256 | return ret; |
2257 | } | |
2258 | ||
2259 | /* | |
3e32cb2e | 2260 | * Returns stocks cached in percpu and reset cached information. |
cdec2e42 KH |
2261 | */ |
2262 | static void drain_stock(struct memcg_stock_pcp *stock) | |
2263 | { | |
f785a8f2 | 2264 | struct mem_cgroup *old = READ_ONCE(stock->cached); |
cdec2e42 | 2265 | |
1a3e1f40 JW |
2266 | if (!old) |
2267 | return; | |
2268 | ||
11c9ea4e | 2269 | if (stock->nr_pages) { |
3e32cb2e | 2270 | page_counter_uncharge(&old->memory, stock->nr_pages); |
7941d214 | 2271 | if (do_memsw_account()) |
3e32cb2e | 2272 | page_counter_uncharge(&old->memsw, stock->nr_pages); |
11c9ea4e | 2273 | stock->nr_pages = 0; |
cdec2e42 | 2274 | } |
1a3e1f40 JW |
2275 | |
2276 | css_put(&old->css); | |
f785a8f2 | 2277 | WRITE_ONCE(stock->cached, NULL); |
cdec2e42 KH |
2278 | } |
2279 | ||
cdec2e42 KH |
2280 | static void drain_local_stock(struct work_struct *dummy) |
2281 | { | |
db2ba40c | 2282 | struct memcg_stock_pcp *stock; |
56751146 | 2283 | struct obj_cgroup *old = NULL; |
db2ba40c JW |
2284 | unsigned long flags; |
2285 | ||
72f0184c | 2286 | /* |
5c49cf9a MH |
2287 | * The only protection from cpu hotplug (memcg_hotplug_cpu_dead) vs. |
2288 | * drain_stock races is that we always operate on local CPU stock | |
2289 | * here with IRQ disabled | |
72f0184c | 2290 | */ |
56751146 | 2291 | local_lock_irqsave(&memcg_stock.stock_lock, flags); |
db2ba40c JW |
2292 | |
2293 | stock = this_cpu_ptr(&memcg_stock); | |
56751146 | 2294 | old = drain_obj_stock(stock); |
cdec2e42 | 2295 | drain_stock(stock); |
26fe6168 | 2296 | clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); |
db2ba40c | 2297 | |
56751146 SAS |
2298 | local_unlock_irqrestore(&memcg_stock.stock_lock, flags); |
2299 | if (old) | |
2300 | obj_cgroup_put(old); | |
cdec2e42 KH |
2301 | } |
2302 | ||
2303 | /* | |
3e32cb2e | 2304 | * Cache charges(val) to local per_cpu area. |
320cc51d | 2305 | * This will be consumed by consume_stock() function, later. |
cdec2e42 | 2306 | */ |
af9a3b69 | 2307 | static void __refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) |
cdec2e42 | 2308 | { |
db2ba40c | 2309 | struct memcg_stock_pcp *stock; |
cdec2e42 | 2310 | |
db2ba40c | 2311 | stock = this_cpu_ptr(&memcg_stock); |
f785a8f2 | 2312 | if (READ_ONCE(stock->cached) != memcg) { /* reset if necessary */ |
cdec2e42 | 2313 | drain_stock(stock); |
1a3e1f40 | 2314 | css_get(&memcg->css); |
f785a8f2 | 2315 | WRITE_ONCE(stock->cached, memcg); |
cdec2e42 | 2316 | } |
11c9ea4e | 2317 | stock->nr_pages += nr_pages; |
db2ba40c | 2318 | |
a983b5eb | 2319 | if (stock->nr_pages > MEMCG_CHARGE_BATCH) |
475d0487 | 2320 | drain_stock(stock); |
af9a3b69 JW |
2321 | } |
2322 | ||
2323 | static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) | |
2324 | { | |
2325 | unsigned long flags; | |
475d0487 | 2326 | |
56751146 | 2327 | local_lock_irqsave(&memcg_stock.stock_lock, flags); |
af9a3b69 | 2328 | __refill_stock(memcg, nr_pages); |
56751146 | 2329 | local_unlock_irqrestore(&memcg_stock.stock_lock, flags); |
cdec2e42 KH |
2330 | } |
2331 | ||
2332 | /* | |
c0ff4b85 | 2333 | * Drains all per-CPU charge caches for given root_memcg resp. subtree |
6d3d6aa2 | 2334 | * of the hierarchy under it. |
cdec2e42 | 2335 | */ |
6d3d6aa2 | 2336 | static void drain_all_stock(struct mem_cgroup *root_memcg) |
cdec2e42 | 2337 | { |
26fe6168 | 2338 | int cpu, curcpu; |
d38144b7 | 2339 | |
6d3d6aa2 JW |
2340 | /* If someone's already draining, avoid adding running more workers. */ |
2341 | if (!mutex_trylock(&percpu_charge_mutex)) | |
2342 | return; | |
72f0184c MH |
2343 | /* |
2344 | * Notify other cpus that system-wide "drain" is running | |
2345 | * We do not care about races with the cpu hotplug because cpu down | |
2346 | * as well as workers from this path always operate on the local | |
2347 | * per-cpu data. CPU up doesn't touch memcg_stock at all. | |
2348 | */ | |
0790ed62 SAS |
2349 | migrate_disable(); |
2350 | curcpu = smp_processor_id(); | |
cdec2e42 KH |
2351 | for_each_online_cpu(cpu) { |
2352 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); | |
c0ff4b85 | 2353 | struct mem_cgroup *memcg; |
e1a366be | 2354 | bool flush = false; |
26fe6168 | 2355 | |
e1a366be | 2356 | rcu_read_lock(); |
f785a8f2 | 2357 | memcg = READ_ONCE(stock->cached); |
e1a366be RG |
2358 | if (memcg && stock->nr_pages && |
2359 | mem_cgroup_is_descendant(memcg, root_memcg)) | |
2360 | flush = true; | |
27fb0956 | 2361 | else if (obj_stock_flush_required(stock, root_memcg)) |
bf4f0599 | 2362 | flush = true; |
e1a366be RG |
2363 | rcu_read_unlock(); |
2364 | ||
2365 | if (flush && | |
2366 | !test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) { | |
d1a05b69 MH |
2367 | if (cpu == curcpu) |
2368 | drain_local_stock(&stock->work); | |
6a792697 | 2369 | else if (!cpu_is_isolated(cpu)) |
d1a05b69 MH |
2370 | schedule_work_on(cpu, &stock->work); |
2371 | } | |
cdec2e42 | 2372 | } |
0790ed62 | 2373 | migrate_enable(); |
9f50fad6 | 2374 | mutex_unlock(&percpu_charge_mutex); |
cdec2e42 KH |
2375 | } |
2376 | ||
2cd21c89 JW |
2377 | static int memcg_hotplug_cpu_dead(unsigned int cpu) |
2378 | { | |
2379 | struct memcg_stock_pcp *stock; | |
a3d4c05a | 2380 | |
2cd21c89 JW |
2381 | stock = &per_cpu(memcg_stock, cpu); |
2382 | drain_stock(stock); | |
a3d4c05a | 2383 | |
308167fc | 2384 | return 0; |
cdec2e42 KH |
2385 | } |
2386 | ||
b3ff9291 CD |
2387 | static unsigned long reclaim_high(struct mem_cgroup *memcg, |
2388 | unsigned int nr_pages, | |
2389 | gfp_t gfp_mask) | |
f7e1cb6e | 2390 | { |
b3ff9291 CD |
2391 | unsigned long nr_reclaimed = 0; |
2392 | ||
f7e1cb6e | 2393 | do { |
e22c6ed9 JW |
2394 | unsigned long pflags; |
2395 | ||
d1663a90 JK |
2396 | if (page_counter_read(&memcg->memory) <= |
2397 | READ_ONCE(memcg->memory.high)) | |
f7e1cb6e | 2398 | continue; |
e22c6ed9 | 2399 | |
e27be240 | 2400 | memcg_memory_event(memcg, MEMCG_HIGH); |
e22c6ed9 JW |
2401 | |
2402 | psi_memstall_enter(&pflags); | |
b3ff9291 | 2403 | nr_reclaimed += try_to_free_mem_cgroup_pages(memcg, nr_pages, |
73b73bac | 2404 | gfp_mask, |
55ab834a | 2405 | MEMCG_RECLAIM_MAY_SWAP); |
e22c6ed9 | 2406 | psi_memstall_leave(&pflags); |
4bf17307 CD |
2407 | } while ((memcg = parent_mem_cgroup(memcg)) && |
2408 | !mem_cgroup_is_root(memcg)); | |
b3ff9291 CD |
2409 | |
2410 | return nr_reclaimed; | |
f7e1cb6e JW |
2411 | } |
2412 | ||
2413 | static void high_work_func(struct work_struct *work) | |
2414 | { | |
2415 | struct mem_cgroup *memcg; | |
2416 | ||
2417 | memcg = container_of(work, struct mem_cgroup, high_work); | |
a983b5eb | 2418 | reclaim_high(memcg, MEMCG_CHARGE_BATCH, GFP_KERNEL); |
f7e1cb6e JW |
2419 | } |
2420 | ||
0e4b01df CD |
2421 | /* |
2422 | * Clamp the maximum sleep time per allocation batch to 2 seconds. This is | |
2423 | * enough to still cause a significant slowdown in most cases, while still | |
2424 | * allowing diagnostics and tracing to proceed without becoming stuck. | |
2425 | */ | |
2426 | #define MEMCG_MAX_HIGH_DELAY_JIFFIES (2UL*HZ) | |
2427 | ||
2428 | /* | |
2429 | * When calculating the delay, we use these either side of the exponentiation to | |
2430 | * maintain precision and scale to a reasonable number of jiffies (see the table | |
2431 | * below. | |
2432 | * | |
2433 | * - MEMCG_DELAY_PRECISION_SHIFT: Extra precision bits while translating the | |
2434 | * overage ratio to a delay. | |
ac5ddd0f | 2435 | * - MEMCG_DELAY_SCALING_SHIFT: The number of bits to scale down the |
0e4b01df CD |
2436 | * proposed penalty in order to reduce to a reasonable number of jiffies, and |
2437 | * to produce a reasonable delay curve. | |
2438 | * | |
2439 | * MEMCG_DELAY_SCALING_SHIFT just happens to be a number that produces a | |
2440 | * reasonable delay curve compared to precision-adjusted overage, not | |
2441 | * penalising heavily at first, but still making sure that growth beyond the | |
2442 | * limit penalises misbehaviour cgroups by slowing them down exponentially. For | |
2443 | * example, with a high of 100 megabytes: | |
2444 | * | |
2445 | * +-------+------------------------+ | |
2446 | * | usage | time to allocate in ms | | |
2447 | * +-------+------------------------+ | |
2448 | * | 100M | 0 | | |
2449 | * | 101M | 6 | | |
2450 | * | 102M | 25 | | |
2451 | * | 103M | 57 | | |
2452 | * | 104M | 102 | | |
2453 | * | 105M | 159 | | |
2454 | * | 106M | 230 | | |
2455 | * | 107M | 313 | | |
2456 | * | 108M | 409 | | |
2457 | * | 109M | 518 | | |
2458 | * | 110M | 639 | | |
2459 | * | 111M | 774 | | |
2460 | * | 112M | 921 | | |
2461 | * | 113M | 1081 | | |
2462 | * | 114M | 1254 | | |
2463 | * | 115M | 1439 | | |
2464 | * | 116M | 1638 | | |
2465 | * | 117M | 1849 | | |
2466 | * | 118M | 2000 | | |
2467 | * | 119M | 2000 | | |
2468 | * | 120M | 2000 | | |
2469 | * +-------+------------------------+ | |
2470 | */ | |
2471 | #define MEMCG_DELAY_PRECISION_SHIFT 20 | |
2472 | #define MEMCG_DELAY_SCALING_SHIFT 14 | |
2473 | ||
8a5dbc65 | 2474 | static u64 calculate_overage(unsigned long usage, unsigned long high) |
b23afb93 | 2475 | { |
8a5dbc65 | 2476 | u64 overage; |
b23afb93 | 2477 | |
8a5dbc65 JK |
2478 | if (usage <= high) |
2479 | return 0; | |
e26733e0 | 2480 | |
8a5dbc65 JK |
2481 | /* |
2482 | * Prevent division by 0 in overage calculation by acting as if | |
2483 | * it was a threshold of 1 page | |
2484 | */ | |
2485 | high = max(high, 1UL); | |
9b8b1754 | 2486 | |
8a5dbc65 JK |
2487 | overage = usage - high; |
2488 | overage <<= MEMCG_DELAY_PRECISION_SHIFT; | |
2489 | return div64_u64(overage, high); | |
2490 | } | |
e26733e0 | 2491 | |
8a5dbc65 JK |
2492 | static u64 mem_find_max_overage(struct mem_cgroup *memcg) |
2493 | { | |
2494 | u64 overage, max_overage = 0; | |
e26733e0 | 2495 | |
8a5dbc65 JK |
2496 | do { |
2497 | overage = calculate_overage(page_counter_read(&memcg->memory), | |
d1663a90 | 2498 | READ_ONCE(memcg->memory.high)); |
8a5dbc65 | 2499 | max_overage = max(overage, max_overage); |
e26733e0 CD |
2500 | } while ((memcg = parent_mem_cgroup(memcg)) && |
2501 | !mem_cgroup_is_root(memcg)); | |
2502 | ||
8a5dbc65 JK |
2503 | return max_overage; |
2504 | } | |
2505 | ||
4b82ab4f JK |
2506 | static u64 swap_find_max_overage(struct mem_cgroup *memcg) |
2507 | { | |
2508 | u64 overage, max_overage = 0; | |
2509 | ||
2510 | do { | |
2511 | overage = calculate_overage(page_counter_read(&memcg->swap), | |
2512 | READ_ONCE(memcg->swap.high)); | |
2513 | if (overage) | |
2514 | memcg_memory_event(memcg, MEMCG_SWAP_HIGH); | |
2515 | max_overage = max(overage, max_overage); | |
2516 | } while ((memcg = parent_mem_cgroup(memcg)) && | |
2517 | !mem_cgroup_is_root(memcg)); | |
2518 | ||
2519 | return max_overage; | |
2520 | } | |
2521 | ||
8a5dbc65 JK |
2522 | /* |
2523 | * Get the number of jiffies that we should penalise a mischievous cgroup which | |
2524 | * is exceeding its memory.high by checking both it and its ancestors. | |
2525 | */ | |
2526 | static unsigned long calculate_high_delay(struct mem_cgroup *memcg, | |
2527 | unsigned int nr_pages, | |
2528 | u64 max_overage) | |
2529 | { | |
2530 | unsigned long penalty_jiffies; | |
2531 | ||
e26733e0 CD |
2532 | if (!max_overage) |
2533 | return 0; | |
0e4b01df CD |
2534 | |
2535 | /* | |
0e4b01df CD |
2536 | * We use overage compared to memory.high to calculate the number of |
2537 | * jiffies to sleep (penalty_jiffies). Ideally this value should be | |
2538 | * fairly lenient on small overages, and increasingly harsh when the | |
2539 | * memcg in question makes it clear that it has no intention of stopping | |
2540 | * its crazy behaviour, so we exponentially increase the delay based on | |
2541 | * overage amount. | |
2542 | */ | |
e26733e0 CD |
2543 | penalty_jiffies = max_overage * max_overage * HZ; |
2544 | penalty_jiffies >>= MEMCG_DELAY_PRECISION_SHIFT; | |
2545 | penalty_jiffies >>= MEMCG_DELAY_SCALING_SHIFT; | |
0e4b01df CD |
2546 | |
2547 | /* | |
2548 | * Factor in the task's own contribution to the overage, such that four | |
2549 | * N-sized allocations are throttled approximately the same as one | |
2550 | * 4N-sized allocation. | |
2551 | * | |
2552 | * MEMCG_CHARGE_BATCH pages is nominal, so work out how much smaller or | |
2553 | * larger the current charge patch is than that. | |
2554 | */ | |
ff144e69 | 2555 | return penalty_jiffies * nr_pages / MEMCG_CHARGE_BATCH; |
e26733e0 CD |
2556 | } |
2557 | ||
2558 | /* | |
2559 | * Scheduled by try_charge() to be executed from the userland return path | |
2560 | * and reclaims memory over the high limit. | |
2561 | */ | |
2562 | void mem_cgroup_handle_over_high(void) | |
2563 | { | |
2564 | unsigned long penalty_jiffies; | |
2565 | unsigned long pflags; | |
b3ff9291 | 2566 | unsigned long nr_reclaimed; |
e26733e0 | 2567 | unsigned int nr_pages = current->memcg_nr_pages_over_high; |
d977aa93 | 2568 | int nr_retries = MAX_RECLAIM_RETRIES; |
e26733e0 | 2569 | struct mem_cgroup *memcg; |
b3ff9291 | 2570 | bool in_retry = false; |
e26733e0 CD |
2571 | |
2572 | if (likely(!nr_pages)) | |
2573 | return; | |
2574 | ||
2575 | memcg = get_mem_cgroup_from_mm(current->mm); | |
e26733e0 CD |
2576 | current->memcg_nr_pages_over_high = 0; |
2577 | ||
b3ff9291 CD |
2578 | retry_reclaim: |
2579 | /* | |
2580 | * The allocating task should reclaim at least the batch size, but for | |
2581 | * subsequent retries we only want to do what's necessary to prevent oom | |
2582 | * or breaching resource isolation. | |
2583 | * | |
2584 | * This is distinct from memory.max or page allocator behaviour because | |
2585 | * memory.high is currently batched, whereas memory.max and the page | |
2586 | * allocator run every time an allocation is made. | |
2587 | */ | |
2588 | nr_reclaimed = reclaim_high(memcg, | |
2589 | in_retry ? SWAP_CLUSTER_MAX : nr_pages, | |
2590 | GFP_KERNEL); | |
2591 | ||
e26733e0 CD |
2592 | /* |
2593 | * memory.high is breached and reclaim is unable to keep up. Throttle | |
2594 | * allocators proactively to slow down excessive growth. | |
2595 | */ | |
8a5dbc65 JK |
2596 | penalty_jiffies = calculate_high_delay(memcg, nr_pages, |
2597 | mem_find_max_overage(memcg)); | |
0e4b01df | 2598 | |
4b82ab4f JK |
2599 | penalty_jiffies += calculate_high_delay(memcg, nr_pages, |
2600 | swap_find_max_overage(memcg)); | |
2601 | ||
ff144e69 JK |
2602 | /* |
2603 | * Clamp the max delay per usermode return so as to still keep the | |
2604 | * application moving forwards and also permit diagnostics, albeit | |
2605 | * extremely slowly. | |
2606 | */ | |
2607 | penalty_jiffies = min(penalty_jiffies, MEMCG_MAX_HIGH_DELAY_JIFFIES); | |
2608 | ||
0e4b01df CD |
2609 | /* |
2610 | * Don't sleep if the amount of jiffies this memcg owes us is so low | |
2611 | * that it's not even worth doing, in an attempt to be nice to those who | |
2612 | * go only a small amount over their memory.high value and maybe haven't | |
2613 | * been aggressively reclaimed enough yet. | |
2614 | */ | |
2615 | if (penalty_jiffies <= HZ / 100) | |
2616 | goto out; | |
2617 | ||
b3ff9291 CD |
2618 | /* |
2619 | * If reclaim is making forward progress but we're still over | |
2620 | * memory.high, we want to encourage that rather than doing allocator | |
2621 | * throttling. | |
2622 | */ | |
2623 | if (nr_reclaimed || nr_retries--) { | |
2624 | in_retry = true; | |
2625 | goto retry_reclaim; | |
2626 | } | |
2627 | ||
0e4b01df CD |
2628 | /* |
2629 | * If we exit early, we're guaranteed to die (since | |
2630 | * schedule_timeout_killable sets TASK_KILLABLE). This means we don't | |
2631 | * need to account for any ill-begotten jiffies to pay them off later. | |
2632 | */ | |
2633 | psi_memstall_enter(&pflags); | |
2634 | schedule_timeout_killable(penalty_jiffies); | |
2635 | psi_memstall_leave(&pflags); | |
2636 | ||
2637 | out: | |
2638 | css_put(&memcg->css); | |
b23afb93 TH |
2639 | } |
2640 | ||
c5c8b16b MS |
2641 | static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, |
2642 | unsigned int nr_pages) | |
8a9f3ccd | 2643 | { |
a983b5eb | 2644 | unsigned int batch = max(MEMCG_CHARGE_BATCH, nr_pages); |
d977aa93 | 2645 | int nr_retries = MAX_RECLAIM_RETRIES; |
6539cc05 | 2646 | struct mem_cgroup *mem_over_limit; |
3e32cb2e | 2647 | struct page_counter *counter; |
6539cc05 | 2648 | unsigned long nr_reclaimed; |
a4ebf1b6 | 2649 | bool passed_oom = false; |
73b73bac | 2650 | unsigned int reclaim_options = MEMCG_RECLAIM_MAY_SWAP; |
b70a2a21 | 2651 | bool drained = false; |
d6e103a7 | 2652 | bool raised_max_event = false; |
e22c6ed9 | 2653 | unsigned long pflags; |
a636b327 | 2654 | |
6539cc05 | 2655 | retry: |
b6b6cc72 | 2656 | if (consume_stock(memcg, nr_pages)) |
10d53c74 | 2657 | return 0; |
8a9f3ccd | 2658 | |
7941d214 | 2659 | if (!do_memsw_account() || |
6071ca52 JW |
2660 | page_counter_try_charge(&memcg->memsw, batch, &counter)) { |
2661 | if (page_counter_try_charge(&memcg->memory, batch, &counter)) | |
6539cc05 | 2662 | goto done_restock; |
7941d214 | 2663 | if (do_memsw_account()) |
3e32cb2e JW |
2664 | page_counter_uncharge(&memcg->memsw, batch); |
2665 | mem_over_limit = mem_cgroup_from_counter(counter, memory); | |
3fbe7244 | 2666 | } else { |
3e32cb2e | 2667 | mem_over_limit = mem_cgroup_from_counter(counter, memsw); |
73b73bac | 2668 | reclaim_options &= ~MEMCG_RECLAIM_MAY_SWAP; |
3fbe7244 | 2669 | } |
7a81b88c | 2670 | |
6539cc05 JW |
2671 | if (batch > nr_pages) { |
2672 | batch = nr_pages; | |
2673 | goto retry; | |
2674 | } | |
6d61ef40 | 2675 | |
89a28483 JW |
2676 | /* |
2677 | * Prevent unbounded recursion when reclaim operations need to | |
2678 | * allocate memory. This might exceed the limits temporarily, | |
2679 | * but we prefer facilitating memory reclaim and getting back | |
2680 | * under the limit over triggering OOM kills in these cases. | |
2681 | */ | |
2682 | if (unlikely(current->flags & PF_MEMALLOC)) | |
2683 | goto force; | |
2684 | ||
06b078fc JW |
2685 | if (unlikely(task_in_memcg_oom(current))) |
2686 | goto nomem; | |
2687 | ||
d0164adc | 2688 | if (!gfpflags_allow_blocking(gfp_mask)) |
6539cc05 | 2689 | goto nomem; |
4b534334 | 2690 | |
e27be240 | 2691 | memcg_memory_event(mem_over_limit, MEMCG_MAX); |
d6e103a7 | 2692 | raised_max_event = true; |
241994ed | 2693 | |
e22c6ed9 | 2694 | psi_memstall_enter(&pflags); |
b70a2a21 | 2695 | nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages, |
55ab834a | 2696 | gfp_mask, reclaim_options); |
e22c6ed9 | 2697 | psi_memstall_leave(&pflags); |
6539cc05 | 2698 | |
61e02c74 | 2699 | if (mem_cgroup_margin(mem_over_limit) >= nr_pages) |
6539cc05 | 2700 | goto retry; |
28c34c29 | 2701 | |
b70a2a21 | 2702 | if (!drained) { |
6d3d6aa2 | 2703 | drain_all_stock(mem_over_limit); |
b70a2a21 JW |
2704 | drained = true; |
2705 | goto retry; | |
2706 | } | |
2707 | ||
28c34c29 JW |
2708 | if (gfp_mask & __GFP_NORETRY) |
2709 | goto nomem; | |
6539cc05 JW |
2710 | /* |
2711 | * Even though the limit is exceeded at this point, reclaim | |
2712 | * may have been able to free some pages. Retry the charge | |
2713 | * before killing the task. | |
2714 | * | |
2715 | * Only for regular pages, though: huge pages are rather | |
2716 | * unlikely to succeed so close to the limit, and we fall back | |
2717 | * to regular pages anyway in case of failure. | |
2718 | */ | |
61e02c74 | 2719 | if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER)) |
6539cc05 JW |
2720 | goto retry; |
2721 | /* | |
2722 | * At task move, charge accounts can be doubly counted. So, it's | |
2723 | * better to wait until the end of task_move if something is going on. | |
2724 | */ | |
2725 | if (mem_cgroup_wait_acct_move(mem_over_limit)) | |
2726 | goto retry; | |
2727 | ||
9b130619 JW |
2728 | if (nr_retries--) |
2729 | goto retry; | |
2730 | ||
38d38493 | 2731 | if (gfp_mask & __GFP_RETRY_MAYFAIL) |
29ef680a MH |
2732 | goto nomem; |
2733 | ||
a4ebf1b6 VA |
2734 | /* Avoid endless loop for tasks bypassed by the oom killer */ |
2735 | if (passed_oom && task_is_dying()) | |
2736 | goto nomem; | |
6539cc05 | 2737 | |
29ef680a MH |
2738 | /* |
2739 | * keep retrying as long as the memcg oom killer is able to make | |
2740 | * a forward progress or bypass the charge if the oom killer | |
2741 | * couldn't make any progress. | |
2742 | */ | |
becdf89d SB |
2743 | if (mem_cgroup_oom(mem_over_limit, gfp_mask, |
2744 | get_order(nr_pages * PAGE_SIZE))) { | |
a4ebf1b6 | 2745 | passed_oom = true; |
d977aa93 | 2746 | nr_retries = MAX_RECLAIM_RETRIES; |
29ef680a | 2747 | goto retry; |
29ef680a | 2748 | } |
7a81b88c | 2749 | nomem: |
1461e8c2 SB |
2750 | /* |
2751 | * Memcg doesn't have a dedicated reserve for atomic | |
2752 | * allocations. But like the global atomic pool, we need to | |
2753 | * put the burden of reclaim on regular allocation requests | |
2754 | * and let these go through as privileged allocations. | |
2755 | */ | |
2756 | if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH))) | |
3168ecbe | 2757 | return -ENOMEM; |
10d53c74 | 2758 | force: |
d6e103a7 RG |
2759 | /* |
2760 | * If the allocation has to be enforced, don't forget to raise | |
2761 | * a MEMCG_MAX event. | |
2762 | */ | |
2763 | if (!raised_max_event) | |
2764 | memcg_memory_event(mem_over_limit, MEMCG_MAX); | |
2765 | ||
10d53c74 TH |
2766 | /* |
2767 | * The allocation either can't fail or will lead to more memory | |
2768 | * being freed very soon. Allow memory usage go over the limit | |
2769 | * temporarily by force charging it. | |
2770 | */ | |
2771 | page_counter_charge(&memcg->memory, nr_pages); | |
7941d214 | 2772 | if (do_memsw_account()) |
10d53c74 | 2773 | page_counter_charge(&memcg->memsw, nr_pages); |
10d53c74 TH |
2774 | |
2775 | return 0; | |
6539cc05 JW |
2776 | |
2777 | done_restock: | |
2778 | if (batch > nr_pages) | |
2779 | refill_stock(memcg, batch - nr_pages); | |
b23afb93 | 2780 | |
241994ed | 2781 | /* |
b23afb93 TH |
2782 | * If the hierarchy is above the normal consumption range, schedule |
2783 | * reclaim on returning to userland. We can perform reclaim here | |
71baba4b | 2784 | * if __GFP_RECLAIM but let's always punt for simplicity and so that |
b23afb93 TH |
2785 | * GFP_KERNEL can consistently be used during reclaim. @memcg is |
2786 | * not recorded as it most likely matches current's and won't | |
2787 | * change in the meantime. As high limit is checked again before | |
2788 | * reclaim, the cost of mismatch is negligible. | |
241994ed JW |
2789 | */ |
2790 | do { | |
4b82ab4f JK |
2791 | bool mem_high, swap_high; |
2792 | ||
2793 | mem_high = page_counter_read(&memcg->memory) > | |
2794 | READ_ONCE(memcg->memory.high); | |
2795 | swap_high = page_counter_read(&memcg->swap) > | |
2796 | READ_ONCE(memcg->swap.high); | |
2797 | ||
2798 | /* Don't bother a random interrupted task */ | |
086f694a | 2799 | if (!in_task()) { |
4b82ab4f | 2800 | if (mem_high) { |
f7e1cb6e JW |
2801 | schedule_work(&memcg->high_work); |
2802 | break; | |
2803 | } | |
4b82ab4f JK |
2804 | continue; |
2805 | } | |
2806 | ||
2807 | if (mem_high || swap_high) { | |
2808 | /* | |
2809 | * The allocating tasks in this cgroup will need to do | |
2810 | * reclaim or be throttled to prevent further growth | |
2811 | * of the memory or swap footprints. | |
2812 | * | |
2813 | * Target some best-effort fairness between the tasks, | |
2814 | * and distribute reclaim work and delay penalties | |
2815 | * based on how much each task is actually allocating. | |
2816 | */ | |
9516a18a | 2817 | current->memcg_nr_pages_over_high += batch; |
b23afb93 TH |
2818 | set_notify_resume(current); |
2819 | break; | |
2820 | } | |
241994ed | 2821 | } while ((memcg = parent_mem_cgroup(memcg))); |
10d53c74 | 2822 | |
c9afe31e SB |
2823 | if (current->memcg_nr_pages_over_high > MEMCG_CHARGE_BATCH && |
2824 | !(current->flags & PF_MEMALLOC) && | |
2825 | gfpflags_allow_blocking(gfp_mask)) { | |
2826 | mem_cgroup_handle_over_high(); | |
2827 | } | |
10d53c74 | 2828 | return 0; |
7a81b88c | 2829 | } |
8a9f3ccd | 2830 | |
c5c8b16b MS |
2831 | static inline int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, |
2832 | unsigned int nr_pages) | |
2833 | { | |
2834 | if (mem_cgroup_is_root(memcg)) | |
2835 | return 0; | |
2836 | ||
2837 | return try_charge_memcg(memcg, gfp_mask, nr_pages); | |
2838 | } | |
2839 | ||
58056f77 | 2840 | static inline void cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages) |
a3032a2c | 2841 | { |
ce00a967 JW |
2842 | if (mem_cgroup_is_root(memcg)) |
2843 | return; | |
2844 | ||
3e32cb2e | 2845 | page_counter_uncharge(&memcg->memory, nr_pages); |
7941d214 | 2846 | if (do_memsw_account()) |
3e32cb2e | 2847 | page_counter_uncharge(&memcg->memsw, nr_pages); |
d01dd17f KH |
2848 | } |
2849 | ||
118f2875 | 2850 | static void commit_charge(struct folio *folio, struct mem_cgroup *memcg) |
0a31bc97 | 2851 | { |
118f2875 | 2852 | VM_BUG_ON_FOLIO(folio_memcg(folio), folio); |
0a31bc97 | 2853 | /* |
a5eb011a | 2854 | * Any of the following ensures page's memcg stability: |
0a31bc97 | 2855 | * |
a0b5b414 JW |
2856 | * - the page lock |
2857 | * - LRU isolation | |
6c77b607 | 2858 | * - folio_memcg_lock() |
a0b5b414 | 2859 | * - exclusive reference |
018ee47f | 2860 | * - mem_cgroup_trylock_pages() |
0a31bc97 | 2861 | */ |
118f2875 | 2862 | folio->memcg_data = (unsigned long)memcg; |
7a81b88c | 2863 | } |
66e1707b | 2864 | |
84c07d11 | 2865 | #ifdef CONFIG_MEMCG_KMEM |
41eb5df1 WL |
2866 | /* |
2867 | * The allocated objcg pointers array is not accounted directly. | |
2868 | * Moreover, it should not come from DMA buffer and is not readily | |
2869 | * reclaimable. So those GFP bits should be masked off. | |
2870 | */ | |
2871 | #define OBJCGS_CLEAR_MASK (__GFP_DMA | __GFP_RECLAIMABLE | __GFP_ACCOUNT) | |
2872 | ||
a7ebf564 WL |
2873 | /* |
2874 | * mod_objcg_mlstate() may be called with irq enabled, so | |
2875 | * mod_memcg_lruvec_state() should be used. | |
2876 | */ | |
2877 | static inline void mod_objcg_mlstate(struct obj_cgroup *objcg, | |
2878 | struct pglist_data *pgdat, | |
2879 | enum node_stat_item idx, int nr) | |
2880 | { | |
2881 | struct mem_cgroup *memcg; | |
2882 | struct lruvec *lruvec; | |
2883 | ||
2884 | rcu_read_lock(); | |
2885 | memcg = obj_cgroup_memcg(objcg); | |
2886 | lruvec = mem_cgroup_lruvec(memcg, pgdat); | |
2887 | mod_memcg_lruvec_state(lruvec, idx, nr); | |
2888 | rcu_read_unlock(); | |
2889 | } | |
2890 | ||
4b5f8d9a VB |
2891 | int memcg_alloc_slab_cgroups(struct slab *slab, struct kmem_cache *s, |
2892 | gfp_t gfp, bool new_slab) | |
10befea9 | 2893 | { |
4b5f8d9a | 2894 | unsigned int objects = objs_per_slab(s, slab); |
2e9bd483 | 2895 | unsigned long memcg_data; |
10befea9 RG |
2896 | void *vec; |
2897 | ||
41eb5df1 | 2898 | gfp &= ~OBJCGS_CLEAR_MASK; |
10befea9 | 2899 | vec = kcalloc_node(objects, sizeof(struct obj_cgroup *), gfp, |
4b5f8d9a | 2900 | slab_nid(slab)); |
10befea9 RG |
2901 | if (!vec) |
2902 | return -ENOMEM; | |
2903 | ||
2e9bd483 | 2904 | memcg_data = (unsigned long) vec | MEMCG_DATA_OBJCGS; |
4b5f8d9a | 2905 | if (new_slab) { |
2e9bd483 | 2906 | /* |
4b5f8d9a VB |
2907 | * If the slab is brand new and nobody can yet access its |
2908 | * memcg_data, no synchronization is required and memcg_data can | |
2909 | * be simply assigned. | |
2e9bd483 | 2910 | */ |
4b5f8d9a VB |
2911 | slab->memcg_data = memcg_data; |
2912 | } else if (cmpxchg(&slab->memcg_data, 0, memcg_data)) { | |
2e9bd483 | 2913 | /* |
4b5f8d9a VB |
2914 | * If the slab is already in use, somebody can allocate and |
2915 | * assign obj_cgroups in parallel. In this case the existing | |
2e9bd483 RG |
2916 | * objcg vector should be reused. |
2917 | */ | |
10befea9 | 2918 | kfree(vec); |
2e9bd483 RG |
2919 | return 0; |
2920 | } | |
10befea9 | 2921 | |
2e9bd483 | 2922 | kmemleak_not_leak(vec); |
10befea9 RG |
2923 | return 0; |
2924 | } | |
2925 | ||
fc4db90f RG |
2926 | static __always_inline |
2927 | struct mem_cgroup *mem_cgroup_from_obj_folio(struct folio *folio, void *p) | |
8380ce47 | 2928 | { |
8380ce47 | 2929 | /* |
9855609b RG |
2930 | * Slab objects are accounted individually, not per-page. |
2931 | * Memcg membership data for each individual object is saved in | |
4b5f8d9a | 2932 | * slab->memcg_data. |
8380ce47 | 2933 | */ |
4b5f8d9a VB |
2934 | if (folio_test_slab(folio)) { |
2935 | struct obj_cgroup **objcgs; | |
2936 | struct slab *slab; | |
9855609b RG |
2937 | unsigned int off; |
2938 | ||
4b5f8d9a VB |
2939 | slab = folio_slab(folio); |
2940 | objcgs = slab_objcgs(slab); | |
2941 | if (!objcgs) | |
2942 | return NULL; | |
2943 | ||
2944 | off = obj_to_index(slab->slab_cache, slab, p); | |
2945 | if (objcgs[off]) | |
2946 | return obj_cgroup_memcg(objcgs[off]); | |
10befea9 RG |
2947 | |
2948 | return NULL; | |
9855609b | 2949 | } |
8380ce47 | 2950 | |
bcfe06bf | 2951 | /* |
becacb04 | 2952 | * folio_memcg_check() is used here, because in theory we can encounter |
4b5f8d9a VB |
2953 | * a folio where the slab flag has been cleared already, but |
2954 | * slab->memcg_data has not been freed yet | |
becacb04 | 2955 | * folio_memcg_check() will guarantee that a proper memory |
bcfe06bf RG |
2956 | * cgroup pointer or NULL will be returned. |
2957 | */ | |
becacb04 | 2958 | return folio_memcg_check(folio); |
8380ce47 RG |
2959 | } |
2960 | ||
fc4db90f RG |
2961 | /* |
2962 | * Returns a pointer to the memory cgroup to which the kernel object is charged. | |
2963 | * | |
2964 | * A passed kernel object can be a slab object, vmalloc object or a generic | |
2965 | * kernel page, so different mechanisms for getting the memory cgroup pointer | |
2966 | * should be used. | |
2967 | * | |
2968 | * In certain cases (e.g. kernel stacks or large kmallocs with SLUB) the caller | |
2969 | * can not know for sure how the kernel object is implemented. | |
2970 | * mem_cgroup_from_obj() can be safely used in such cases. | |
2971 | * | |
2972 | * The caller must ensure the memcg lifetime, e.g. by taking rcu_read_lock(), | |
2973 | * cgroup_mutex, etc. | |
2974 | */ | |
2975 | struct mem_cgroup *mem_cgroup_from_obj(void *p) | |
2976 | { | |
2977 | struct folio *folio; | |
2978 | ||
2979 | if (mem_cgroup_disabled()) | |
2980 | return NULL; | |
2981 | ||
2982 | if (unlikely(is_vmalloc_addr(p))) | |
2983 | folio = page_folio(vmalloc_to_page(p)); | |
2984 | else | |
2985 | folio = virt_to_folio(p); | |
2986 | ||
2987 | return mem_cgroup_from_obj_folio(folio, p); | |
2988 | } | |
2989 | ||
2990 | /* | |
2991 | * Returns a pointer to the memory cgroup to which the kernel object is charged. | |
2992 | * Similar to mem_cgroup_from_obj(), but faster and not suitable for objects, | |
2993 | * allocated using vmalloc(). | |
2994 | * | |
2995 | * A passed kernel object must be a slab object or a generic kernel page. | |
2996 | * | |
2997 | * The caller must ensure the memcg lifetime, e.g. by taking rcu_read_lock(), | |
2998 | * cgroup_mutex, etc. | |
2999 | */ | |
3000 | struct mem_cgroup *mem_cgroup_from_slab_obj(void *p) | |
3001 | { | |
3002 | if (mem_cgroup_disabled()) | |
3003 | return NULL; | |
3004 | ||
3005 | return mem_cgroup_from_obj_folio(virt_to_folio(p), p); | |
3006 | } | |
3007 | ||
f4840ccf JW |
3008 | static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) |
3009 | { | |
3010 | struct obj_cgroup *objcg = NULL; | |
3011 | ||
7848ed62 | 3012 | for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) { |
f4840ccf JW |
3013 | objcg = rcu_dereference(memcg->objcg); |
3014 | if (objcg && obj_cgroup_tryget(objcg)) | |
3015 | break; | |
3016 | objcg = NULL; | |
3017 | } | |
3018 | return objcg; | |
3019 | } | |
3020 | ||
bf4f0599 RG |
3021 | __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void) |
3022 | { | |
3023 | struct obj_cgroup *objcg = NULL; | |
3024 | struct mem_cgroup *memcg; | |
3025 | ||
279c3393 RG |
3026 | if (memcg_kmem_bypass()) |
3027 | return NULL; | |
3028 | ||
bf4f0599 | 3029 | rcu_read_lock(); |
37d5985c RG |
3030 | if (unlikely(active_memcg())) |
3031 | memcg = active_memcg(); | |
bf4f0599 RG |
3032 | else |
3033 | memcg = mem_cgroup_from_task(current); | |
f4840ccf | 3034 | objcg = __get_obj_cgroup_from_memcg(memcg); |
bf4f0599 | 3035 | rcu_read_unlock(); |
f4840ccf JW |
3036 | return objcg; |
3037 | } | |
3038 | ||
3039 | struct obj_cgroup *get_obj_cgroup_from_page(struct page *page) | |
3040 | { | |
3041 | struct obj_cgroup *objcg; | |
3042 | ||
f7a449f7 | 3043 | if (!memcg_kmem_online()) |
f4840ccf JW |
3044 | return NULL; |
3045 | ||
3046 | if (PageMemcgKmem(page)) { | |
3047 | objcg = __folio_objcg(page_folio(page)); | |
3048 | obj_cgroup_get(objcg); | |
3049 | } else { | |
3050 | struct mem_cgroup *memcg; | |
bf4f0599 | 3051 | |
f4840ccf JW |
3052 | rcu_read_lock(); |
3053 | memcg = __folio_memcg(page_folio(page)); | |
3054 | if (memcg) | |
3055 | objcg = __get_obj_cgroup_from_memcg(memcg); | |
3056 | else | |
3057 | objcg = NULL; | |
3058 | rcu_read_unlock(); | |
3059 | } | |
bf4f0599 RG |
3060 | return objcg; |
3061 | } | |
3062 | ||
a8c49af3 YA |
3063 | static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages) |
3064 | { | |
3065 | mod_memcg_state(memcg, MEMCG_KMEM, nr_pages); | |
3066 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) { | |
3067 | if (nr_pages > 0) | |
3068 | page_counter_charge(&memcg->kmem, nr_pages); | |
3069 | else | |
3070 | page_counter_uncharge(&memcg->kmem, -nr_pages); | |
3071 | } | |
3072 | } | |
3073 | ||
3074 | ||
f1286fae MS |
3075 | /* |
3076 | * obj_cgroup_uncharge_pages: uncharge a number of kernel pages from a objcg | |
3077 | * @objcg: object cgroup to uncharge | |
3078 | * @nr_pages: number of pages to uncharge | |
3079 | */ | |
e74d2259 MS |
3080 | static void obj_cgroup_uncharge_pages(struct obj_cgroup *objcg, |
3081 | unsigned int nr_pages) | |
3082 | { | |
3083 | struct mem_cgroup *memcg; | |
3084 | ||
3085 | memcg = get_mem_cgroup_from_objcg(objcg); | |
e74d2259 | 3086 | |
a8c49af3 | 3087 | memcg_account_kmem(memcg, -nr_pages); |
f1286fae | 3088 | refill_stock(memcg, nr_pages); |
e74d2259 | 3089 | |
e74d2259 | 3090 | css_put(&memcg->css); |
e74d2259 MS |
3091 | } |
3092 | ||
f1286fae MS |
3093 | /* |
3094 | * obj_cgroup_charge_pages: charge a number of kernel pages to a objcg | |
3095 | * @objcg: object cgroup to charge | |
45264778 | 3096 | * @gfp: reclaim mode |
92d0510c | 3097 | * @nr_pages: number of pages to charge |
45264778 VD |
3098 | * |
3099 | * Returns 0 on success, an error code on failure. | |
3100 | */ | |
f1286fae MS |
3101 | static int obj_cgroup_charge_pages(struct obj_cgroup *objcg, gfp_t gfp, |
3102 | unsigned int nr_pages) | |
7ae1e1d0 | 3103 | { |
f1286fae | 3104 | struct mem_cgroup *memcg; |
7ae1e1d0 GC |
3105 | int ret; |
3106 | ||
f1286fae MS |
3107 | memcg = get_mem_cgroup_from_objcg(objcg); |
3108 | ||
c5c8b16b | 3109 | ret = try_charge_memcg(memcg, gfp, nr_pages); |
52c29b04 | 3110 | if (ret) |
f1286fae | 3111 | goto out; |
52c29b04 | 3112 | |
a8c49af3 | 3113 | memcg_account_kmem(memcg, nr_pages); |
f1286fae MS |
3114 | out: |
3115 | css_put(&memcg->css); | |
4b13f64d | 3116 | |
f1286fae | 3117 | return ret; |
4b13f64d RG |
3118 | } |
3119 | ||
45264778 | 3120 | /** |
f4b00eab | 3121 | * __memcg_kmem_charge_page: charge a kmem page to the current memory cgroup |
45264778 VD |
3122 | * @page: page to charge |
3123 | * @gfp: reclaim mode | |
3124 | * @order: allocation order | |
3125 | * | |
3126 | * Returns 0 on success, an error code on failure. | |
3127 | */ | |
f4b00eab | 3128 | int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order) |
7ae1e1d0 | 3129 | { |
b4e0b68f | 3130 | struct obj_cgroup *objcg; |
fcff7d7e | 3131 | int ret = 0; |
7ae1e1d0 | 3132 | |
b4e0b68f MS |
3133 | objcg = get_obj_cgroup_from_current(); |
3134 | if (objcg) { | |
3135 | ret = obj_cgroup_charge_pages(objcg, gfp, 1 << order); | |
4d96ba35 | 3136 | if (!ret) { |
b4e0b68f | 3137 | page->memcg_data = (unsigned long)objcg | |
18b2db3b | 3138 | MEMCG_DATA_KMEM; |
1a3e1f40 | 3139 | return 0; |
4d96ba35 | 3140 | } |
b4e0b68f | 3141 | obj_cgroup_put(objcg); |
c4159a75 | 3142 | } |
d05e83a6 | 3143 | return ret; |
7ae1e1d0 | 3144 | } |
49a18eae | 3145 | |
45264778 | 3146 | /** |
f4b00eab | 3147 | * __memcg_kmem_uncharge_page: uncharge a kmem page |
45264778 VD |
3148 | * @page: page to uncharge |
3149 | * @order: allocation order | |
3150 | */ | |
f4b00eab | 3151 | void __memcg_kmem_uncharge_page(struct page *page, int order) |
7ae1e1d0 | 3152 | { |
1b7e4464 | 3153 | struct folio *folio = page_folio(page); |
b4e0b68f | 3154 | struct obj_cgroup *objcg; |
f3ccb2c4 | 3155 | unsigned int nr_pages = 1 << order; |
7ae1e1d0 | 3156 | |
1b7e4464 | 3157 | if (!folio_memcg_kmem(folio)) |
7ae1e1d0 GC |
3158 | return; |
3159 | ||
1b7e4464 | 3160 | objcg = __folio_objcg(folio); |
b4e0b68f | 3161 | obj_cgroup_uncharge_pages(objcg, nr_pages); |
1b7e4464 | 3162 | folio->memcg_data = 0; |
b4e0b68f | 3163 | obj_cgroup_put(objcg); |
60d3fd32 | 3164 | } |
bf4f0599 | 3165 | |
68ac5b3c WL |
3166 | void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat, |
3167 | enum node_stat_item idx, int nr) | |
3168 | { | |
fead2b86 | 3169 | struct memcg_stock_pcp *stock; |
56751146 | 3170 | struct obj_cgroup *old = NULL; |
68ac5b3c WL |
3171 | unsigned long flags; |
3172 | int *bytes; | |
3173 | ||
56751146 | 3174 | local_lock_irqsave(&memcg_stock.stock_lock, flags); |
fead2b86 MH |
3175 | stock = this_cpu_ptr(&memcg_stock); |
3176 | ||
68ac5b3c WL |
3177 | /* |
3178 | * Save vmstat data in stock and skip vmstat array update unless | |
3179 | * accumulating over a page of vmstat data or when pgdat or idx | |
3180 | * changes. | |
3181 | */ | |
3b8abb32 | 3182 | if (READ_ONCE(stock->cached_objcg) != objcg) { |
56751146 | 3183 | old = drain_obj_stock(stock); |
68ac5b3c WL |
3184 | obj_cgroup_get(objcg); |
3185 | stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes) | |
3186 | ? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0; | |
3b8abb32 | 3187 | WRITE_ONCE(stock->cached_objcg, objcg); |
68ac5b3c WL |
3188 | stock->cached_pgdat = pgdat; |
3189 | } else if (stock->cached_pgdat != pgdat) { | |
3190 | /* Flush the existing cached vmstat data */ | |
7fa0dacb WL |
3191 | struct pglist_data *oldpg = stock->cached_pgdat; |
3192 | ||
68ac5b3c | 3193 | if (stock->nr_slab_reclaimable_b) { |
7fa0dacb | 3194 | mod_objcg_mlstate(objcg, oldpg, NR_SLAB_RECLAIMABLE_B, |
68ac5b3c WL |
3195 | stock->nr_slab_reclaimable_b); |
3196 | stock->nr_slab_reclaimable_b = 0; | |
3197 | } | |
3198 | if (stock->nr_slab_unreclaimable_b) { | |
7fa0dacb | 3199 | mod_objcg_mlstate(objcg, oldpg, NR_SLAB_UNRECLAIMABLE_B, |
68ac5b3c WL |
3200 | stock->nr_slab_unreclaimable_b); |
3201 | stock->nr_slab_unreclaimable_b = 0; | |
3202 | } | |
3203 | stock->cached_pgdat = pgdat; | |
3204 | } | |
3205 | ||
3206 | bytes = (idx == NR_SLAB_RECLAIMABLE_B) ? &stock->nr_slab_reclaimable_b | |
3207 | : &stock->nr_slab_unreclaimable_b; | |
3208 | /* | |
3209 | * Even for large object >= PAGE_SIZE, the vmstat data will still be | |
3210 | * cached locally at least once before pushing it out. | |
3211 | */ | |
3212 | if (!*bytes) { | |
3213 | *bytes = nr; | |
3214 | nr = 0; | |
3215 | } else { | |
3216 | *bytes += nr; | |
3217 | if (abs(*bytes) > PAGE_SIZE) { | |
3218 | nr = *bytes; | |
3219 | *bytes = 0; | |
3220 | } else { | |
3221 | nr = 0; | |
3222 | } | |
3223 | } | |
3224 | if (nr) | |
3225 | mod_objcg_mlstate(objcg, pgdat, idx, nr); | |
3226 | ||
56751146 SAS |
3227 | local_unlock_irqrestore(&memcg_stock.stock_lock, flags); |
3228 | if (old) | |
3229 | obj_cgroup_put(old); | |
68ac5b3c WL |
3230 | } |
3231 | ||
bf4f0599 RG |
3232 | static bool consume_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes) |
3233 | { | |
fead2b86 | 3234 | struct memcg_stock_pcp *stock; |
bf4f0599 RG |
3235 | unsigned long flags; |
3236 | bool ret = false; | |
3237 | ||
56751146 | 3238 | local_lock_irqsave(&memcg_stock.stock_lock, flags); |
fead2b86 MH |
3239 | |
3240 | stock = this_cpu_ptr(&memcg_stock); | |
3b8abb32 | 3241 | if (objcg == READ_ONCE(stock->cached_objcg) && stock->nr_bytes >= nr_bytes) { |
bf4f0599 RG |
3242 | stock->nr_bytes -= nr_bytes; |
3243 | ret = true; | |
3244 | } | |
3245 | ||
56751146 | 3246 | local_unlock_irqrestore(&memcg_stock.stock_lock, flags); |
bf4f0599 RG |
3247 | |
3248 | return ret; | |
3249 | } | |
3250 | ||
56751146 | 3251 | static struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock) |
bf4f0599 | 3252 | { |
3b8abb32 | 3253 | struct obj_cgroup *old = READ_ONCE(stock->cached_objcg); |
bf4f0599 RG |
3254 | |
3255 | if (!old) | |
56751146 | 3256 | return NULL; |
bf4f0599 RG |
3257 | |
3258 | if (stock->nr_bytes) { | |
3259 | unsigned int nr_pages = stock->nr_bytes >> PAGE_SHIFT; | |
3260 | unsigned int nr_bytes = stock->nr_bytes & (PAGE_SIZE - 1); | |
3261 | ||
af9a3b69 JW |
3262 | if (nr_pages) { |
3263 | struct mem_cgroup *memcg; | |
3264 | ||
3265 | memcg = get_mem_cgroup_from_objcg(old); | |
3266 | ||
3267 | memcg_account_kmem(memcg, -nr_pages); | |
3268 | __refill_stock(memcg, nr_pages); | |
3269 | ||
3270 | css_put(&memcg->css); | |
3271 | } | |
bf4f0599 RG |
3272 | |
3273 | /* | |
3274 | * The leftover is flushed to the centralized per-memcg value. | |
3275 | * On the next attempt to refill obj stock it will be moved | |
3276 | * to a per-cpu stock (probably, on an other CPU), see | |
3277 | * refill_obj_stock(). | |
3278 | * | |
3279 | * How often it's flushed is a trade-off between the memory | |
3280 | * limit enforcement accuracy and potential CPU contention, | |
3281 | * so it might be changed in the future. | |
3282 | */ | |
3283 | atomic_add(nr_bytes, &old->nr_charged_bytes); | |
3284 | stock->nr_bytes = 0; | |
3285 | } | |
3286 | ||
68ac5b3c WL |
3287 | /* |
3288 | * Flush the vmstat data in current stock | |
3289 | */ | |
3290 | if (stock->nr_slab_reclaimable_b || stock->nr_slab_unreclaimable_b) { | |
3291 | if (stock->nr_slab_reclaimable_b) { | |
3292 | mod_objcg_mlstate(old, stock->cached_pgdat, | |
3293 | NR_SLAB_RECLAIMABLE_B, | |
3294 | stock->nr_slab_reclaimable_b); | |
3295 | stock->nr_slab_reclaimable_b = 0; | |
3296 | } | |
3297 | if (stock->nr_slab_unreclaimable_b) { | |
3298 | mod_objcg_mlstate(old, stock->cached_pgdat, | |
3299 | NR_SLAB_UNRECLAIMABLE_B, | |
3300 | stock->nr_slab_unreclaimable_b); | |
3301 | stock->nr_slab_unreclaimable_b = 0; | |
3302 | } | |
3303 | stock->cached_pgdat = NULL; | |
3304 | } | |
3305 | ||
3b8abb32 | 3306 | WRITE_ONCE(stock->cached_objcg, NULL); |
56751146 SAS |
3307 | /* |
3308 | * The `old' objects needs to be released by the caller via | |
3309 | * obj_cgroup_put() outside of memcg_stock_pcp::stock_lock. | |
3310 | */ | |
3311 | return old; | |
bf4f0599 RG |
3312 | } |
3313 | ||
3314 | static bool obj_stock_flush_required(struct memcg_stock_pcp *stock, | |
3315 | struct mem_cgroup *root_memcg) | |
3316 | { | |
3b8abb32 | 3317 | struct obj_cgroup *objcg = READ_ONCE(stock->cached_objcg); |
bf4f0599 RG |
3318 | struct mem_cgroup *memcg; |
3319 | ||
3b8abb32 RG |
3320 | if (objcg) { |
3321 | memcg = obj_cgroup_memcg(objcg); | |
bf4f0599 RG |
3322 | if (memcg && mem_cgroup_is_descendant(memcg, root_memcg)) |
3323 | return true; | |
3324 | } | |
3325 | ||
3326 | return false; | |
3327 | } | |
3328 | ||
5387c904 WL |
3329 | static void refill_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes, |
3330 | bool allow_uncharge) | |
bf4f0599 | 3331 | { |
fead2b86 | 3332 | struct memcg_stock_pcp *stock; |
56751146 | 3333 | struct obj_cgroup *old = NULL; |
bf4f0599 | 3334 | unsigned long flags; |
5387c904 | 3335 | unsigned int nr_pages = 0; |
bf4f0599 | 3336 | |
56751146 | 3337 | local_lock_irqsave(&memcg_stock.stock_lock, flags); |
fead2b86 MH |
3338 | |
3339 | stock = this_cpu_ptr(&memcg_stock); | |
3b8abb32 | 3340 | if (READ_ONCE(stock->cached_objcg) != objcg) { /* reset if necessary */ |
56751146 | 3341 | old = drain_obj_stock(stock); |
bf4f0599 | 3342 | obj_cgroup_get(objcg); |
3b8abb32 | 3343 | WRITE_ONCE(stock->cached_objcg, objcg); |
5387c904 WL |
3344 | stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes) |
3345 | ? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0; | |
3346 | allow_uncharge = true; /* Allow uncharge when objcg changes */ | |
bf4f0599 RG |
3347 | } |
3348 | stock->nr_bytes += nr_bytes; | |
3349 | ||
5387c904 WL |
3350 | if (allow_uncharge && (stock->nr_bytes > PAGE_SIZE)) { |
3351 | nr_pages = stock->nr_bytes >> PAGE_SHIFT; | |
3352 | stock->nr_bytes &= (PAGE_SIZE - 1); | |
3353 | } | |
bf4f0599 | 3354 | |
56751146 SAS |
3355 | local_unlock_irqrestore(&memcg_stock.stock_lock, flags); |
3356 | if (old) | |
3357 | obj_cgroup_put(old); | |
5387c904 WL |
3358 | |
3359 | if (nr_pages) | |
3360 | obj_cgroup_uncharge_pages(objcg, nr_pages); | |
bf4f0599 RG |
3361 | } |
3362 | ||
3363 | int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size) | |
3364 | { | |
bf4f0599 RG |
3365 | unsigned int nr_pages, nr_bytes; |
3366 | int ret; | |
3367 | ||
3368 | if (consume_obj_stock(objcg, size)) | |
3369 | return 0; | |
3370 | ||
3371 | /* | |
5387c904 | 3372 | * In theory, objcg->nr_charged_bytes can have enough |
bf4f0599 | 3373 | * pre-charged bytes to satisfy the allocation. However, |
5387c904 WL |
3374 | * flushing objcg->nr_charged_bytes requires two atomic |
3375 | * operations, and objcg->nr_charged_bytes can't be big. | |
3376 | * The shared objcg->nr_charged_bytes can also become a | |
3377 | * performance bottleneck if all tasks of the same memcg are | |
3378 | * trying to update it. So it's better to ignore it and try | |
3379 | * grab some new pages. The stock's nr_bytes will be flushed to | |
3380 | * objcg->nr_charged_bytes later on when objcg changes. | |
3381 | * | |
3382 | * The stock's nr_bytes may contain enough pre-charged bytes | |
3383 | * to allow one less page from being charged, but we can't rely | |
3384 | * on the pre-charged bytes not being changed outside of | |
3385 | * consume_obj_stock() or refill_obj_stock(). So ignore those | |
3386 | * pre-charged bytes as well when charging pages. To avoid a | |
3387 | * page uncharge right after a page charge, we set the | |
3388 | * allow_uncharge flag to false when calling refill_obj_stock() | |
3389 | * to temporarily allow the pre-charged bytes to exceed the page | |
3390 | * size limit. The maximum reachable value of the pre-charged | |
3391 | * bytes is (sizeof(object) + PAGE_SIZE - 2) if there is no data | |
3392 | * race. | |
bf4f0599 | 3393 | */ |
bf4f0599 RG |
3394 | nr_pages = size >> PAGE_SHIFT; |
3395 | nr_bytes = size & (PAGE_SIZE - 1); | |
3396 | ||
3397 | if (nr_bytes) | |
3398 | nr_pages += 1; | |
3399 | ||
e74d2259 | 3400 | ret = obj_cgroup_charge_pages(objcg, gfp, nr_pages); |
bf4f0599 | 3401 | if (!ret && nr_bytes) |
5387c904 | 3402 | refill_obj_stock(objcg, PAGE_SIZE - nr_bytes, false); |
bf4f0599 | 3403 | |
bf4f0599 RG |
3404 | return ret; |
3405 | } | |
3406 | ||
3407 | void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size) | |
3408 | { | |
5387c904 | 3409 | refill_obj_stock(objcg, size, true); |
bf4f0599 RG |
3410 | } |
3411 | ||
84c07d11 | 3412 | #endif /* CONFIG_MEMCG_KMEM */ |
7ae1e1d0 | 3413 | |
ca3e0214 | 3414 | /* |
be6c8982 | 3415 | * Because page_memcg(head) is not set on tails, set it now. |
ca3e0214 | 3416 | */ |
be6c8982 | 3417 | void split_page_memcg(struct page *head, unsigned int nr) |
ca3e0214 | 3418 | { |
1b7e4464 MWO |
3419 | struct folio *folio = page_folio(head); |
3420 | struct mem_cgroup *memcg = folio_memcg(folio); | |
e94c8a9c | 3421 | int i; |
ca3e0214 | 3422 | |
be6c8982 | 3423 | if (mem_cgroup_disabled() || !memcg) |
3d37c4a9 | 3424 | return; |
b070e65c | 3425 | |
be6c8982 | 3426 | for (i = 1; i < nr; i++) |
1b7e4464 | 3427 | folio_page(folio, i)->memcg_data = folio->memcg_data; |
b4e0b68f | 3428 | |
1b7e4464 MWO |
3429 | if (folio_memcg_kmem(folio)) |
3430 | obj_cgroup_get_many(__folio_objcg(folio), nr - 1); | |
b4e0b68f MS |
3431 | else |
3432 | css_get_many(&memcg->css, nr - 1); | |
ca3e0214 | 3433 | } |
ca3e0214 | 3434 | |
e55b9f96 | 3435 | #ifdef CONFIG_SWAP |
02491447 DN |
3436 | /** |
3437 | * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record. | |
3438 | * @entry: swap entry to be moved | |
3439 | * @from: mem_cgroup which the entry is moved from | |
3440 | * @to: mem_cgroup which the entry is moved to | |
3441 | * | |
3442 | * It succeeds only when the swap_cgroup's record for this entry is the same | |
3443 | * as the mem_cgroup's id of @from. | |
3444 | * | |
3445 | * Returns 0 on success, -EINVAL on failure. | |
3446 | * | |
3e32cb2e | 3447 | * The caller must have charged to @to, IOW, called page_counter_charge() about |
02491447 DN |
3448 | * both res and memsw, and called css_get(). |
3449 | */ | |
3450 | static int mem_cgroup_move_swap_account(swp_entry_t entry, | |
e91cbb42 | 3451 | struct mem_cgroup *from, struct mem_cgroup *to) |
02491447 DN |
3452 | { |
3453 | unsigned short old_id, new_id; | |
3454 | ||
34c00c31 LZ |
3455 | old_id = mem_cgroup_id(from); |
3456 | new_id = mem_cgroup_id(to); | |
02491447 DN |
3457 | |
3458 | if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) { | |
c9019e9b JW |
3459 | mod_memcg_state(from, MEMCG_SWAP, -1); |
3460 | mod_memcg_state(to, MEMCG_SWAP, 1); | |
02491447 DN |
3461 | return 0; |
3462 | } | |
3463 | return -EINVAL; | |
3464 | } | |
3465 | #else | |
3466 | static inline int mem_cgroup_move_swap_account(swp_entry_t entry, | |
e91cbb42 | 3467 | struct mem_cgroup *from, struct mem_cgroup *to) |
02491447 DN |
3468 | { |
3469 | return -EINVAL; | |
3470 | } | |
8c7c6e34 | 3471 | #endif |
d13d1443 | 3472 | |
bbec2e15 | 3473 | static DEFINE_MUTEX(memcg_max_mutex); |
f212ad7c | 3474 | |
bbec2e15 RG |
3475 | static int mem_cgroup_resize_max(struct mem_cgroup *memcg, |
3476 | unsigned long max, bool memsw) | |
628f4235 | 3477 | { |
3e32cb2e | 3478 | bool enlarge = false; |
bb4a7ea2 | 3479 | bool drained = false; |
3e32cb2e | 3480 | int ret; |
c054a78c YZ |
3481 | bool limits_invariant; |
3482 | struct page_counter *counter = memsw ? &memcg->memsw : &memcg->memory; | |
81d39c20 | 3483 | |
3e32cb2e | 3484 | do { |
628f4235 KH |
3485 | if (signal_pending(current)) { |
3486 | ret = -EINTR; | |
3487 | break; | |
3488 | } | |
3e32cb2e | 3489 | |
bbec2e15 | 3490 | mutex_lock(&memcg_max_mutex); |
c054a78c YZ |
3491 | /* |
3492 | * Make sure that the new limit (memsw or memory limit) doesn't | |
bbec2e15 | 3493 | * break our basic invariant rule memory.max <= memsw.max. |
c054a78c | 3494 | */ |
15b42562 | 3495 | limits_invariant = memsw ? max >= READ_ONCE(memcg->memory.max) : |
bbec2e15 | 3496 | max <= memcg->memsw.max; |
c054a78c | 3497 | if (!limits_invariant) { |
bbec2e15 | 3498 | mutex_unlock(&memcg_max_mutex); |
8c7c6e34 | 3499 | ret = -EINVAL; |
8c7c6e34 KH |
3500 | break; |
3501 | } | |
bbec2e15 | 3502 | if (max > counter->max) |
3e32cb2e | 3503 | enlarge = true; |
bbec2e15 RG |
3504 | ret = page_counter_set_max(counter, max); |
3505 | mutex_unlock(&memcg_max_mutex); | |
8c7c6e34 KH |
3506 | |
3507 | if (!ret) | |
3508 | break; | |
3509 | ||
bb4a7ea2 SB |
3510 | if (!drained) { |
3511 | drain_all_stock(memcg); | |
3512 | drained = true; | |
3513 | continue; | |
3514 | } | |
3515 | ||
73b73bac | 3516 | if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL, |
55ab834a | 3517 | memsw ? 0 : MEMCG_RECLAIM_MAY_SWAP)) { |
1ab5c056 AR |
3518 | ret = -EBUSY; |
3519 | break; | |
3520 | } | |
3521 | } while (true); | |
3e32cb2e | 3522 | |
3c11ecf4 KH |
3523 | if (!ret && enlarge) |
3524 | memcg_oom_recover(memcg); | |
3e32cb2e | 3525 | |
628f4235 KH |
3526 | return ret; |
3527 | } | |
3528 | ||
ef8f2327 | 3529 | unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order, |
0608f43d AM |
3530 | gfp_t gfp_mask, |
3531 | unsigned long *total_scanned) | |
3532 | { | |
3533 | unsigned long nr_reclaimed = 0; | |
ef8f2327 | 3534 | struct mem_cgroup_per_node *mz, *next_mz = NULL; |
0608f43d AM |
3535 | unsigned long reclaimed; |
3536 | int loop = 0; | |
ef8f2327 | 3537 | struct mem_cgroup_tree_per_node *mctz; |
3e32cb2e | 3538 | unsigned long excess; |
0608f43d | 3539 | |
e4dde56c YZ |
3540 | if (lru_gen_enabled()) |
3541 | return 0; | |
3542 | ||
0608f43d AM |
3543 | if (order > 0) |
3544 | return 0; | |
3545 | ||
2ab082ba | 3546 | mctz = soft_limit_tree.rb_tree_per_node[pgdat->node_id]; |
d6507ff5 MH |
3547 | |
3548 | /* | |
3549 | * Do not even bother to check the largest node if the root | |
3550 | * is empty. Do it lockless to prevent lock bouncing. Races | |
3551 | * are acceptable as soft limit is best effort anyway. | |
3552 | */ | |
bfc7228b | 3553 | if (!mctz || RB_EMPTY_ROOT(&mctz->rb_root)) |
d6507ff5 MH |
3554 | return 0; |
3555 | ||
0608f43d AM |
3556 | /* |
3557 | * This loop can run a while, specially if mem_cgroup's continuously | |
3558 | * keep exceeding their soft limit and putting the system under | |
3559 | * pressure | |
3560 | */ | |
3561 | do { | |
3562 | if (next_mz) | |
3563 | mz = next_mz; | |
3564 | else | |
3565 | mz = mem_cgroup_largest_soft_limit_node(mctz); | |
3566 | if (!mz) | |
3567 | break; | |
3568 | ||
ef8f2327 | 3569 | reclaimed = mem_cgroup_soft_reclaim(mz->memcg, pgdat, |
d8f65338 | 3570 | gfp_mask, total_scanned); |
0608f43d | 3571 | nr_reclaimed += reclaimed; |
0a31bc97 | 3572 | spin_lock_irq(&mctz->lock); |
0608f43d AM |
3573 | |
3574 | /* | |
3575 | * If we failed to reclaim anything from this memory cgroup | |
3576 | * it is time to move on to the next cgroup | |
3577 | */ | |
3578 | next_mz = NULL; | |
bc2f2e7f VD |
3579 | if (!reclaimed) |
3580 | next_mz = __mem_cgroup_largest_soft_limit_node(mctz); | |
3581 | ||
3e32cb2e | 3582 | excess = soft_limit_excess(mz->memcg); |
0608f43d AM |
3583 | /* |
3584 | * One school of thought says that we should not add | |
3585 | * back the node to the tree if reclaim returns 0. | |
3586 | * But our reclaim could return 0, simply because due | |
3587 | * to priority we are exposing a smaller subset of | |
3588 | * memory to reclaim from. Consider this as a longer | |
3589 | * term TODO. | |
3590 | */ | |
3591 | /* If excess == 0, no tree ops */ | |
cf2c8127 | 3592 | __mem_cgroup_insert_exceeded(mz, mctz, excess); |
0a31bc97 | 3593 | spin_unlock_irq(&mctz->lock); |
0608f43d AM |
3594 | css_put(&mz->memcg->css); |
3595 | loop++; | |
3596 | /* | |
3597 | * Could not reclaim anything and there are no more | |
3598 | * mem cgroups to try or we seem to be looping without | |
3599 | * reclaiming anything. | |
3600 | */ | |
3601 | if (!nr_reclaimed && | |
3602 | (next_mz == NULL || | |
3603 | loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS)) | |
3604 | break; | |
3605 | } while (!nr_reclaimed); | |
3606 | if (next_mz) | |
3607 | css_put(&next_mz->memcg->css); | |
3608 | return nr_reclaimed; | |
3609 | } | |
3610 | ||
c26251f9 | 3611 | /* |
51038171 | 3612 | * Reclaims as many pages from the given memcg as possible. |
c26251f9 MH |
3613 | * |
3614 | * Caller is responsible for holding css reference for memcg. | |
3615 | */ | |
3616 | static int mem_cgroup_force_empty(struct mem_cgroup *memcg) | |
3617 | { | |
d977aa93 | 3618 | int nr_retries = MAX_RECLAIM_RETRIES; |
c26251f9 | 3619 | |
c1e862c1 KH |
3620 | /* we call try-to-free pages for make this cgroup empty */ |
3621 | lru_add_drain_all(); | |
d12c60f6 JS |
3622 | |
3623 | drain_all_stock(memcg); | |
3624 | ||
f817ed48 | 3625 | /* try to free all pages in this cgroup */ |
3e32cb2e | 3626 | while (nr_retries && page_counter_read(&memcg->memory)) { |
c26251f9 MH |
3627 | if (signal_pending(current)) |
3628 | return -EINTR; | |
3629 | ||
73b73bac | 3630 | if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL, |
55ab834a | 3631 | MEMCG_RECLAIM_MAY_SWAP)) |
f817ed48 | 3632 | nr_retries--; |
f817ed48 | 3633 | } |
ab5196c2 MH |
3634 | |
3635 | return 0; | |
cc847582 KH |
3636 | } |
3637 | ||
6770c64e TH |
3638 | static ssize_t mem_cgroup_force_empty_write(struct kernfs_open_file *of, |
3639 | char *buf, size_t nbytes, | |
3640 | loff_t off) | |
c1e862c1 | 3641 | { |
6770c64e | 3642 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
c26251f9 | 3643 | |
d8423011 MH |
3644 | if (mem_cgroup_is_root(memcg)) |
3645 | return -EINVAL; | |
6770c64e | 3646 | return mem_cgroup_force_empty(memcg) ?: nbytes; |
c1e862c1 KH |
3647 | } |
3648 | ||
182446d0 TH |
3649 | static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css, |
3650 | struct cftype *cft) | |
18f59ea7 | 3651 | { |
bef8620c | 3652 | return 1; |
18f59ea7 BS |
3653 | } |
3654 | ||
182446d0 TH |
3655 | static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css, |
3656 | struct cftype *cft, u64 val) | |
18f59ea7 | 3657 | { |
bef8620c | 3658 | if (val == 1) |
0b8f73e1 | 3659 | return 0; |
567fb435 | 3660 | |
bef8620c RG |
3661 | pr_warn_once("Non-hierarchical mode is deprecated. " |
3662 | "Please report your usecase to linux-mm@kvack.org if you " | |
3663 | "depend on this functionality.\n"); | |
567fb435 | 3664 | |
bef8620c | 3665 | return -EINVAL; |
18f59ea7 BS |
3666 | } |
3667 | ||
6f646156 | 3668 | static unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap) |
ce00a967 | 3669 | { |
42a30035 | 3670 | unsigned long val; |
ce00a967 | 3671 | |
3e32cb2e | 3672 | if (mem_cgroup_is_root(memcg)) { |
a2174e95 | 3673 | /* |
f82a7a86 YA |
3674 | * Approximate root's usage from global state. This isn't |
3675 | * perfect, but the root usage was always an approximation. | |
a2174e95 | 3676 | */ |
f82a7a86 YA |
3677 | val = global_node_page_state(NR_FILE_PAGES) + |
3678 | global_node_page_state(NR_ANON_MAPPED); | |
42a30035 | 3679 | if (swap) |
f82a7a86 | 3680 | val += total_swap_pages - get_nr_swap_pages(); |
3e32cb2e | 3681 | } else { |
ce00a967 | 3682 | if (!swap) |
3e32cb2e | 3683 | val = page_counter_read(&memcg->memory); |
ce00a967 | 3684 | else |
3e32cb2e | 3685 | val = page_counter_read(&memcg->memsw); |
ce00a967 | 3686 | } |
c12176d3 | 3687 | return val; |
ce00a967 JW |
3688 | } |
3689 | ||
3e32cb2e JW |
3690 | enum { |
3691 | RES_USAGE, | |
3692 | RES_LIMIT, | |
3693 | RES_MAX_USAGE, | |
3694 | RES_FAILCNT, | |
3695 | RES_SOFT_LIMIT, | |
3696 | }; | |
ce00a967 | 3697 | |
791badbd | 3698 | static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css, |
05b84301 | 3699 | struct cftype *cft) |
8cdea7c0 | 3700 | { |
182446d0 | 3701 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
3e32cb2e | 3702 | struct page_counter *counter; |
af36f906 | 3703 | |
3e32cb2e | 3704 | switch (MEMFILE_TYPE(cft->private)) { |
8c7c6e34 | 3705 | case _MEM: |
3e32cb2e JW |
3706 | counter = &memcg->memory; |
3707 | break; | |
8c7c6e34 | 3708 | case _MEMSWAP: |
3e32cb2e JW |
3709 | counter = &memcg->memsw; |
3710 | break; | |
510fc4e1 | 3711 | case _KMEM: |
3e32cb2e | 3712 | counter = &memcg->kmem; |
510fc4e1 | 3713 | break; |
d55f90bf | 3714 | case _TCP: |
0db15298 | 3715 | counter = &memcg->tcpmem; |
d55f90bf | 3716 | break; |
8c7c6e34 KH |
3717 | default: |
3718 | BUG(); | |
8c7c6e34 | 3719 | } |
3e32cb2e JW |
3720 | |
3721 | switch (MEMFILE_ATTR(cft->private)) { | |
3722 | case RES_USAGE: | |
3723 | if (counter == &memcg->memory) | |
c12176d3 | 3724 | return (u64)mem_cgroup_usage(memcg, false) * PAGE_SIZE; |
3e32cb2e | 3725 | if (counter == &memcg->memsw) |
c12176d3 | 3726 | return (u64)mem_cgroup_usage(memcg, true) * PAGE_SIZE; |
3e32cb2e JW |
3727 | return (u64)page_counter_read(counter) * PAGE_SIZE; |
3728 | case RES_LIMIT: | |
bbec2e15 | 3729 | return (u64)counter->max * PAGE_SIZE; |
3e32cb2e JW |
3730 | case RES_MAX_USAGE: |
3731 | return (u64)counter->watermark * PAGE_SIZE; | |
3732 | case RES_FAILCNT: | |
3733 | return counter->failcnt; | |
3734 | case RES_SOFT_LIMIT: | |
2178e20c | 3735 | return (u64)READ_ONCE(memcg->soft_limit) * PAGE_SIZE; |
3e32cb2e JW |
3736 | default: |
3737 | BUG(); | |
3738 | } | |
8cdea7c0 | 3739 | } |
510fc4e1 | 3740 | |
6b0ba2ab FS |
3741 | /* |
3742 | * This function doesn't do anything useful. Its only job is to provide a read | |
3743 | * handler for a file so that cgroup_file_mode() will add read permissions. | |
3744 | */ | |
3745 | static int mem_cgroup_dummy_seq_show(__always_unused struct seq_file *m, | |
3746 | __always_unused void *v) | |
3747 | { | |
3748 | return -EINVAL; | |
3749 | } | |
3750 | ||
84c07d11 | 3751 | #ifdef CONFIG_MEMCG_KMEM |
567e9ab2 | 3752 | static int memcg_online_kmem(struct mem_cgroup *memcg) |
d6441637 | 3753 | { |
bf4f0599 | 3754 | struct obj_cgroup *objcg; |
d6441637 | 3755 | |
9c94bef9 | 3756 | if (mem_cgroup_kmem_disabled()) |
b313aeee VD |
3757 | return 0; |
3758 | ||
da0efe30 MS |
3759 | if (unlikely(mem_cgroup_is_root(memcg))) |
3760 | return 0; | |
d6441637 | 3761 | |
bf4f0599 | 3762 | objcg = obj_cgroup_alloc(); |
f9c69d63 | 3763 | if (!objcg) |
bf4f0599 | 3764 | return -ENOMEM; |
f9c69d63 | 3765 | |
bf4f0599 RG |
3766 | objcg->memcg = memcg; |
3767 | rcu_assign_pointer(memcg->objcg, objcg); | |
3768 | ||
f7a449f7 | 3769 | static_branch_enable(&memcg_kmem_online_key); |
d648bcc7 | 3770 | |
f9c69d63 | 3771 | memcg->kmemcg_id = memcg->id.id; |
0b8f73e1 JW |
3772 | |
3773 | return 0; | |
d6441637 VD |
3774 | } |
3775 | ||
8e0a8912 JW |
3776 | static void memcg_offline_kmem(struct mem_cgroup *memcg) |
3777 | { | |
64268868 | 3778 | struct mem_cgroup *parent; |
8e0a8912 | 3779 | |
9c94bef9 | 3780 | if (mem_cgroup_kmem_disabled()) |
da0efe30 MS |
3781 | return; |
3782 | ||
3783 | if (unlikely(mem_cgroup_is_root(memcg))) | |
8e0a8912 | 3784 | return; |
9855609b | 3785 | |
8e0a8912 JW |
3786 | parent = parent_mem_cgroup(memcg); |
3787 | if (!parent) | |
3788 | parent = root_mem_cgroup; | |
3789 | ||
bf4f0599 | 3790 | memcg_reparent_objcgs(memcg, parent); |
fb2f2b0a | 3791 | |
8e0a8912 | 3792 | /* |
64268868 MS |
3793 | * After we have finished memcg_reparent_objcgs(), all list_lrus |
3794 | * corresponding to this cgroup are guaranteed to remain empty. | |
3795 | * The ordering is imposed by list_lru_node->lock taken by | |
1f391eb2 | 3796 | * memcg_reparent_list_lrus(). |
8e0a8912 | 3797 | */ |
1f391eb2 | 3798 | memcg_reparent_list_lrus(memcg, parent); |
8e0a8912 | 3799 | } |
d6441637 | 3800 | #else |
0b8f73e1 | 3801 | static int memcg_online_kmem(struct mem_cgroup *memcg) |
127424c8 JW |
3802 | { |
3803 | return 0; | |
3804 | } | |
3805 | static void memcg_offline_kmem(struct mem_cgroup *memcg) | |
3806 | { | |
3807 | } | |
84c07d11 | 3808 | #endif /* CONFIG_MEMCG_KMEM */ |
127424c8 | 3809 | |
bbec2e15 | 3810 | static int memcg_update_tcp_max(struct mem_cgroup *memcg, unsigned long max) |
d55f90bf VD |
3811 | { |
3812 | int ret; | |
3813 | ||
bbec2e15 | 3814 | mutex_lock(&memcg_max_mutex); |
d55f90bf | 3815 | |
bbec2e15 | 3816 | ret = page_counter_set_max(&memcg->tcpmem, max); |
d55f90bf VD |
3817 | if (ret) |
3818 | goto out; | |
3819 | ||
0db15298 | 3820 | if (!memcg->tcpmem_active) { |
d55f90bf VD |
3821 | /* |
3822 | * The active flag needs to be written after the static_key | |
3823 | * update. This is what guarantees that the socket activation | |
2d758073 JW |
3824 | * function is the last one to run. See mem_cgroup_sk_alloc() |
3825 | * for details, and note that we don't mark any socket as | |
3826 | * belonging to this memcg until that flag is up. | |
d55f90bf VD |
3827 | * |
3828 | * We need to do this, because static_keys will span multiple | |
3829 | * sites, but we can't control their order. If we mark a socket | |
3830 | * as accounted, but the accounting functions are not patched in | |
3831 | * yet, we'll lose accounting. | |
3832 | * | |
2d758073 | 3833 | * We never race with the readers in mem_cgroup_sk_alloc(), |
d55f90bf VD |
3834 | * because when this value change, the code to process it is not |
3835 | * patched in yet. | |
3836 | */ | |
3837 | static_branch_inc(&memcg_sockets_enabled_key); | |
0db15298 | 3838 | memcg->tcpmem_active = true; |
d55f90bf VD |
3839 | } |
3840 | out: | |
bbec2e15 | 3841 | mutex_unlock(&memcg_max_mutex); |
d55f90bf VD |
3842 | return ret; |
3843 | } | |
d55f90bf | 3844 | |
628f4235 KH |
3845 | /* |
3846 | * The user of this function is... | |
3847 | * RES_LIMIT. | |
3848 | */ | |
451af504 TH |
3849 | static ssize_t mem_cgroup_write(struct kernfs_open_file *of, |
3850 | char *buf, size_t nbytes, loff_t off) | |
8cdea7c0 | 3851 | { |
451af504 | 3852 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
3e32cb2e | 3853 | unsigned long nr_pages; |
628f4235 KH |
3854 | int ret; |
3855 | ||
451af504 | 3856 | buf = strstrip(buf); |
650c5e56 | 3857 | ret = page_counter_memparse(buf, "-1", &nr_pages); |
3e32cb2e JW |
3858 | if (ret) |
3859 | return ret; | |
af36f906 | 3860 | |
3e32cb2e | 3861 | switch (MEMFILE_ATTR(of_cft(of)->private)) { |
628f4235 | 3862 | case RES_LIMIT: |
4b3bde4c BS |
3863 | if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */ |
3864 | ret = -EINVAL; | |
3865 | break; | |
3866 | } | |
3e32cb2e JW |
3867 | switch (MEMFILE_TYPE(of_cft(of)->private)) { |
3868 | case _MEM: | |
bbec2e15 | 3869 | ret = mem_cgroup_resize_max(memcg, nr_pages, false); |
8c7c6e34 | 3870 | break; |
3e32cb2e | 3871 | case _MEMSWAP: |
bbec2e15 | 3872 | ret = mem_cgroup_resize_max(memcg, nr_pages, true); |
296c81d8 | 3873 | break; |
d55f90bf | 3874 | case _TCP: |
bbec2e15 | 3875 | ret = memcg_update_tcp_max(memcg, nr_pages); |
d55f90bf | 3876 | break; |
3e32cb2e | 3877 | } |
296c81d8 | 3878 | break; |
3e32cb2e | 3879 | case RES_SOFT_LIMIT: |
2343e88d SAS |
3880 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) { |
3881 | ret = -EOPNOTSUPP; | |
3882 | } else { | |
2178e20c | 3883 | WRITE_ONCE(memcg->soft_limit, nr_pages); |
2343e88d SAS |
3884 | ret = 0; |
3885 | } | |
628f4235 KH |
3886 | break; |
3887 | } | |
451af504 | 3888 | return ret ?: nbytes; |
8cdea7c0 BS |
3889 | } |
3890 | ||
6770c64e TH |
3891 | static ssize_t mem_cgroup_reset(struct kernfs_open_file *of, char *buf, |
3892 | size_t nbytes, loff_t off) | |
c84872e1 | 3893 | { |
6770c64e | 3894 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
3e32cb2e | 3895 | struct page_counter *counter; |
c84872e1 | 3896 | |
3e32cb2e JW |
3897 | switch (MEMFILE_TYPE(of_cft(of)->private)) { |
3898 | case _MEM: | |
3899 | counter = &memcg->memory; | |
3900 | break; | |
3901 | case _MEMSWAP: | |
3902 | counter = &memcg->memsw; | |
3903 | break; | |
3904 | case _KMEM: | |
3905 | counter = &memcg->kmem; | |
3906 | break; | |
d55f90bf | 3907 | case _TCP: |
0db15298 | 3908 | counter = &memcg->tcpmem; |
d55f90bf | 3909 | break; |
3e32cb2e JW |
3910 | default: |
3911 | BUG(); | |
3912 | } | |
af36f906 | 3913 | |
3e32cb2e | 3914 | switch (MEMFILE_ATTR(of_cft(of)->private)) { |
29f2a4da | 3915 | case RES_MAX_USAGE: |
3e32cb2e | 3916 | page_counter_reset_watermark(counter); |
29f2a4da PE |
3917 | break; |
3918 | case RES_FAILCNT: | |
3e32cb2e | 3919 | counter->failcnt = 0; |
29f2a4da | 3920 | break; |
3e32cb2e JW |
3921 | default: |
3922 | BUG(); | |
29f2a4da | 3923 | } |
f64c3f54 | 3924 | |
6770c64e | 3925 | return nbytes; |
c84872e1 PE |
3926 | } |
3927 | ||
182446d0 | 3928 | static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css, |
7dc74be0 DN |
3929 | struct cftype *cft) |
3930 | { | |
182446d0 | 3931 | return mem_cgroup_from_css(css)->move_charge_at_immigrate; |
7dc74be0 DN |
3932 | } |
3933 | ||
02491447 | 3934 | #ifdef CONFIG_MMU |
182446d0 | 3935 | static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css, |
7dc74be0 DN |
3936 | struct cftype *cft, u64 val) |
3937 | { | |
182446d0 | 3938 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
7dc74be0 | 3939 | |
da34a848 JW |
3940 | pr_warn_once("Cgroup memory moving (move_charge_at_immigrate) is deprecated. " |
3941 | "Please report your usecase to linux-mm@kvack.org if you " | |
3942 | "depend on this functionality.\n"); | |
3943 | ||
1dfab5ab | 3944 | if (val & ~MOVE_MASK) |
7dc74be0 | 3945 | return -EINVAL; |
ee5e8472 | 3946 | |
7dc74be0 | 3947 | /* |
ee5e8472 GC |
3948 | * No kind of locking is needed in here, because ->can_attach() will |
3949 | * check this value once in the beginning of the process, and then carry | |
3950 | * on with stale data. This means that changes to this value will only | |
3951 | * affect task migrations starting after the change. | |
7dc74be0 | 3952 | */ |
c0ff4b85 | 3953 | memcg->move_charge_at_immigrate = val; |
7dc74be0 DN |
3954 | return 0; |
3955 | } | |
02491447 | 3956 | #else |
182446d0 | 3957 | static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css, |
02491447 DN |
3958 | struct cftype *cft, u64 val) |
3959 | { | |
3960 | return -ENOSYS; | |
3961 | } | |
3962 | #endif | |
7dc74be0 | 3963 | |
406eb0c9 | 3964 | #ifdef CONFIG_NUMA |
113b7dfd JW |
3965 | |
3966 | #define LRU_ALL_FILE (BIT(LRU_INACTIVE_FILE) | BIT(LRU_ACTIVE_FILE)) | |
3967 | #define LRU_ALL_ANON (BIT(LRU_INACTIVE_ANON) | BIT(LRU_ACTIVE_ANON)) | |
3968 | #define LRU_ALL ((1 << NR_LRU_LISTS) - 1) | |
3969 | ||
3970 | static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg, | |
dd8657b6 | 3971 | int nid, unsigned int lru_mask, bool tree) |
113b7dfd | 3972 | { |
867e5e1d | 3973 | struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid)); |
113b7dfd JW |
3974 | unsigned long nr = 0; |
3975 | enum lru_list lru; | |
3976 | ||
3977 | VM_BUG_ON((unsigned)nid >= nr_node_ids); | |
3978 | ||
3979 | for_each_lru(lru) { | |
3980 | if (!(BIT(lru) & lru_mask)) | |
3981 | continue; | |
dd8657b6 SB |
3982 | if (tree) |
3983 | nr += lruvec_page_state(lruvec, NR_LRU_BASE + lru); | |
3984 | else | |
3985 | nr += lruvec_page_state_local(lruvec, NR_LRU_BASE + lru); | |
113b7dfd JW |
3986 | } |
3987 | return nr; | |
3988 | } | |
3989 | ||
3990 | static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg, | |
dd8657b6 SB |
3991 | unsigned int lru_mask, |
3992 | bool tree) | |
113b7dfd JW |
3993 | { |
3994 | unsigned long nr = 0; | |
3995 | enum lru_list lru; | |
3996 | ||
3997 | for_each_lru(lru) { | |
3998 | if (!(BIT(lru) & lru_mask)) | |
3999 | continue; | |
dd8657b6 SB |
4000 | if (tree) |
4001 | nr += memcg_page_state(memcg, NR_LRU_BASE + lru); | |
4002 | else | |
4003 | nr += memcg_page_state_local(memcg, NR_LRU_BASE + lru); | |
113b7dfd JW |
4004 | } |
4005 | return nr; | |
4006 | } | |
4007 | ||
2da8ca82 | 4008 | static int memcg_numa_stat_show(struct seq_file *m, void *v) |
406eb0c9 | 4009 | { |
25485de6 GT |
4010 | struct numa_stat { |
4011 | const char *name; | |
4012 | unsigned int lru_mask; | |
4013 | }; | |
4014 | ||
4015 | static const struct numa_stat stats[] = { | |
4016 | { "total", LRU_ALL }, | |
4017 | { "file", LRU_ALL_FILE }, | |
4018 | { "anon", LRU_ALL_ANON }, | |
4019 | { "unevictable", BIT(LRU_UNEVICTABLE) }, | |
4020 | }; | |
4021 | const struct numa_stat *stat; | |
406eb0c9 | 4022 | int nid; |
aa9694bb | 4023 | struct mem_cgroup *memcg = mem_cgroup_from_seq(m); |
406eb0c9 | 4024 | |
fd25a9e0 | 4025 | mem_cgroup_flush_stats(); |
2d146aa3 | 4026 | |
25485de6 | 4027 | for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) { |
dd8657b6 SB |
4028 | seq_printf(m, "%s=%lu", stat->name, |
4029 | mem_cgroup_nr_lru_pages(memcg, stat->lru_mask, | |
4030 | false)); | |
4031 | for_each_node_state(nid, N_MEMORY) | |
4032 | seq_printf(m, " N%d=%lu", nid, | |
4033 | mem_cgroup_node_nr_lru_pages(memcg, nid, | |
4034 | stat->lru_mask, false)); | |
25485de6 | 4035 | seq_putc(m, '\n'); |
406eb0c9 | 4036 | } |
406eb0c9 | 4037 | |
071aee13 | 4038 | for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) { |
dd8657b6 SB |
4039 | |
4040 | seq_printf(m, "hierarchical_%s=%lu", stat->name, | |
4041 | mem_cgroup_nr_lru_pages(memcg, stat->lru_mask, | |
4042 | true)); | |
4043 | for_each_node_state(nid, N_MEMORY) | |
4044 | seq_printf(m, " N%d=%lu", nid, | |
4045 | mem_cgroup_node_nr_lru_pages(memcg, nid, | |
4046 | stat->lru_mask, true)); | |
071aee13 | 4047 | seq_putc(m, '\n'); |
406eb0c9 | 4048 | } |
406eb0c9 | 4049 | |
406eb0c9 YH |
4050 | return 0; |
4051 | } | |
4052 | #endif /* CONFIG_NUMA */ | |
4053 | ||
c8713d0b | 4054 | static const unsigned int memcg1_stats[] = { |
0d1c2072 | 4055 | NR_FILE_PAGES, |
be5d0a74 | 4056 | NR_ANON_MAPPED, |
468c3982 JW |
4057 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
4058 | NR_ANON_THPS, | |
4059 | #endif | |
c8713d0b JW |
4060 | NR_SHMEM, |
4061 | NR_FILE_MAPPED, | |
4062 | NR_FILE_DIRTY, | |
4063 | NR_WRITEBACK, | |
e09b0b61 YS |
4064 | WORKINGSET_REFAULT_ANON, |
4065 | WORKINGSET_REFAULT_FILE, | |
c8713d0b JW |
4066 | MEMCG_SWAP, |
4067 | }; | |
4068 | ||
4069 | static const char *const memcg1_stat_names[] = { | |
4070 | "cache", | |
4071 | "rss", | |
468c3982 | 4072 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
c8713d0b | 4073 | "rss_huge", |
468c3982 | 4074 | #endif |
c8713d0b JW |
4075 | "shmem", |
4076 | "mapped_file", | |
4077 | "dirty", | |
4078 | "writeback", | |
e09b0b61 YS |
4079 | "workingset_refault_anon", |
4080 | "workingset_refault_file", | |
c8713d0b JW |
4081 | "swap", |
4082 | }; | |
4083 | ||
df0e53d0 | 4084 | /* Universal VM events cgroup1 shows, original sort order */ |
8dd53fd3 | 4085 | static const unsigned int memcg1_events[] = { |
df0e53d0 JW |
4086 | PGPGIN, |
4087 | PGPGOUT, | |
4088 | PGFAULT, | |
4089 | PGMAJFAULT, | |
4090 | }; | |
4091 | ||
dddb44ff | 4092 | static void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) |
d2ceb9b7 | 4093 | { |
3e32cb2e | 4094 | unsigned long memory, memsw; |
af7c4b0e JW |
4095 | struct mem_cgroup *mi; |
4096 | unsigned int i; | |
406eb0c9 | 4097 | |
71cd3113 | 4098 | BUILD_BUG_ON(ARRAY_SIZE(memcg1_stat_names) != ARRAY_SIZE(memcg1_stats)); |
70bc068c | 4099 | |
fd25a9e0 | 4100 | mem_cgroup_flush_stats(); |
2d146aa3 | 4101 | |
71cd3113 | 4102 | for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) { |
468c3982 JW |
4103 | unsigned long nr; |
4104 | ||
71cd3113 | 4105 | if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account()) |
1dd3a273 | 4106 | continue; |
468c3982 | 4107 | nr = memcg_page_state_local(memcg, memcg1_stats[i]); |
dddb44ff | 4108 | seq_buf_printf(s, "%s %lu\n", memcg1_stat_names[i], |
e09b0b61 | 4109 | nr * memcg_page_state_unit(memcg1_stats[i])); |
1dd3a273 | 4110 | } |
7b854121 | 4111 | |
df0e53d0 | 4112 | for (i = 0; i < ARRAY_SIZE(memcg1_events); i++) |
dddb44ff YA |
4113 | seq_buf_printf(s, "%s %lu\n", vm_event_name(memcg1_events[i]), |
4114 | memcg_events_local(memcg, memcg1_events[i])); | |
af7c4b0e JW |
4115 | |
4116 | for (i = 0; i < NR_LRU_LISTS; i++) | |
dddb44ff YA |
4117 | seq_buf_printf(s, "%s %lu\n", lru_list_name(i), |
4118 | memcg_page_state_local(memcg, NR_LRU_BASE + i) * | |
4119 | PAGE_SIZE); | |
af7c4b0e | 4120 | |
14067bb3 | 4121 | /* Hierarchical information */ |
3e32cb2e JW |
4122 | memory = memsw = PAGE_COUNTER_MAX; |
4123 | for (mi = memcg; mi; mi = parent_mem_cgroup(mi)) { | |
15b42562 CD |
4124 | memory = min(memory, READ_ONCE(mi->memory.max)); |
4125 | memsw = min(memsw, READ_ONCE(mi->memsw.max)); | |
fee7b548 | 4126 | } |
dddb44ff YA |
4127 | seq_buf_printf(s, "hierarchical_memory_limit %llu\n", |
4128 | (u64)memory * PAGE_SIZE); | |
7941d214 | 4129 | if (do_memsw_account()) |
dddb44ff YA |
4130 | seq_buf_printf(s, "hierarchical_memsw_limit %llu\n", |
4131 | (u64)memsw * PAGE_SIZE); | |
7f016ee8 | 4132 | |
8de7ecc6 | 4133 | for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) { |
7de2e9f1 | 4134 | unsigned long nr; |
4135 | ||
71cd3113 | 4136 | if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account()) |
1dd3a273 | 4137 | continue; |
7de2e9f1 | 4138 | nr = memcg_page_state(memcg, memcg1_stats[i]); |
dddb44ff | 4139 | seq_buf_printf(s, "total_%s %llu\n", memcg1_stat_names[i], |
e09b0b61 | 4140 | (u64)nr * memcg_page_state_unit(memcg1_stats[i])); |
af7c4b0e JW |
4141 | } |
4142 | ||
8de7ecc6 | 4143 | for (i = 0; i < ARRAY_SIZE(memcg1_events); i++) |
dddb44ff YA |
4144 | seq_buf_printf(s, "total_%s %llu\n", |
4145 | vm_event_name(memcg1_events[i]), | |
4146 | (u64)memcg_events(memcg, memcg1_events[i])); | |
af7c4b0e | 4147 | |
8de7ecc6 | 4148 | for (i = 0; i < NR_LRU_LISTS; i++) |
dddb44ff YA |
4149 | seq_buf_printf(s, "total_%s %llu\n", lru_list_name(i), |
4150 | (u64)memcg_page_state(memcg, NR_LRU_BASE + i) * | |
4151 | PAGE_SIZE); | |
14067bb3 | 4152 | |
7f016ee8 | 4153 | #ifdef CONFIG_DEBUG_VM |
7f016ee8 | 4154 | { |
ef8f2327 MG |
4155 | pg_data_t *pgdat; |
4156 | struct mem_cgroup_per_node *mz; | |
1431d4d1 JW |
4157 | unsigned long anon_cost = 0; |
4158 | unsigned long file_cost = 0; | |
7f016ee8 | 4159 | |
ef8f2327 | 4160 | for_each_online_pgdat(pgdat) { |
a3747b53 | 4161 | mz = memcg->nodeinfo[pgdat->node_id]; |
7f016ee8 | 4162 | |
1431d4d1 JW |
4163 | anon_cost += mz->lruvec.anon_cost; |
4164 | file_cost += mz->lruvec.file_cost; | |
ef8f2327 | 4165 | } |
dddb44ff YA |
4166 | seq_buf_printf(s, "anon_cost %lu\n", anon_cost); |
4167 | seq_buf_printf(s, "file_cost %lu\n", file_cost); | |
7f016ee8 KM |
4168 | } |
4169 | #endif | |
d2ceb9b7 KH |
4170 | } |
4171 | ||
182446d0 TH |
4172 | static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css, |
4173 | struct cftype *cft) | |
a7885eb8 | 4174 | { |
182446d0 | 4175 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
a7885eb8 | 4176 | |
1f4c025b | 4177 | return mem_cgroup_swappiness(memcg); |
a7885eb8 KM |
4178 | } |
4179 | ||
182446d0 TH |
4180 | static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css, |
4181 | struct cftype *cft, u64 val) | |
a7885eb8 | 4182 | { |
182446d0 | 4183 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
a7885eb8 | 4184 | |
37bc3cb9 | 4185 | if (val > 200) |
a7885eb8 KM |
4186 | return -EINVAL; |
4187 | ||
a4792030 | 4188 | if (!mem_cgroup_is_root(memcg)) |
82b3aa26 | 4189 | WRITE_ONCE(memcg->swappiness, val); |
3dae7fec | 4190 | else |
82b3aa26 | 4191 | WRITE_ONCE(vm_swappiness, val); |
068b38c1 | 4192 | |
a7885eb8 KM |
4193 | return 0; |
4194 | } | |
4195 | ||
2e72b634 KS |
4196 | static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap) |
4197 | { | |
4198 | struct mem_cgroup_threshold_ary *t; | |
3e32cb2e | 4199 | unsigned long usage; |
2e72b634 KS |
4200 | int i; |
4201 | ||
4202 | rcu_read_lock(); | |
4203 | if (!swap) | |
2c488db2 | 4204 | t = rcu_dereference(memcg->thresholds.primary); |
2e72b634 | 4205 | else |
2c488db2 | 4206 | t = rcu_dereference(memcg->memsw_thresholds.primary); |
2e72b634 KS |
4207 | |
4208 | if (!t) | |
4209 | goto unlock; | |
4210 | ||
ce00a967 | 4211 | usage = mem_cgroup_usage(memcg, swap); |
2e72b634 KS |
4212 | |
4213 | /* | |
748dad36 | 4214 | * current_threshold points to threshold just below or equal to usage. |
2e72b634 KS |
4215 | * If it's not true, a threshold was crossed after last |
4216 | * call of __mem_cgroup_threshold(). | |
4217 | */ | |
5407a562 | 4218 | i = t->current_threshold; |
2e72b634 KS |
4219 | |
4220 | /* | |
4221 | * Iterate backward over array of thresholds starting from | |
4222 | * current_threshold and check if a threshold is crossed. | |
4223 | * If none of thresholds below usage is crossed, we read | |
4224 | * only one element of the array here. | |
4225 | */ | |
4226 | for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--) | |
4227 | eventfd_signal(t->entries[i].eventfd, 1); | |
4228 | ||
4229 | /* i = current_threshold + 1 */ | |
4230 | i++; | |
4231 | ||
4232 | /* | |
4233 | * Iterate forward over array of thresholds starting from | |
4234 | * current_threshold+1 and check if a threshold is crossed. | |
4235 | * If none of thresholds above usage is crossed, we read | |
4236 | * only one element of the array here. | |
4237 | */ | |
4238 | for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++) | |
4239 | eventfd_signal(t->entries[i].eventfd, 1); | |
4240 | ||
4241 | /* Update current_threshold */ | |
5407a562 | 4242 | t->current_threshold = i - 1; |
2e72b634 KS |
4243 | unlock: |
4244 | rcu_read_unlock(); | |
4245 | } | |
4246 | ||
4247 | static void mem_cgroup_threshold(struct mem_cgroup *memcg) | |
4248 | { | |
ad4ca5f4 KS |
4249 | while (memcg) { |
4250 | __mem_cgroup_threshold(memcg, false); | |
7941d214 | 4251 | if (do_memsw_account()) |
ad4ca5f4 KS |
4252 | __mem_cgroup_threshold(memcg, true); |
4253 | ||
4254 | memcg = parent_mem_cgroup(memcg); | |
4255 | } | |
2e72b634 KS |
4256 | } |
4257 | ||
4258 | static int compare_thresholds(const void *a, const void *b) | |
4259 | { | |
4260 | const struct mem_cgroup_threshold *_a = a; | |
4261 | const struct mem_cgroup_threshold *_b = b; | |
4262 | ||
2bff24a3 GT |
4263 | if (_a->threshold > _b->threshold) |
4264 | return 1; | |
4265 | ||
4266 | if (_a->threshold < _b->threshold) | |
4267 | return -1; | |
4268 | ||
4269 | return 0; | |
2e72b634 KS |
4270 | } |
4271 | ||
c0ff4b85 | 4272 | static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg) |
9490ff27 KH |
4273 | { |
4274 | struct mem_cgroup_eventfd_list *ev; | |
4275 | ||
2bcf2e92 MH |
4276 | spin_lock(&memcg_oom_lock); |
4277 | ||
c0ff4b85 | 4278 | list_for_each_entry(ev, &memcg->oom_notify, list) |
9490ff27 | 4279 | eventfd_signal(ev->eventfd, 1); |
2bcf2e92 MH |
4280 | |
4281 | spin_unlock(&memcg_oom_lock); | |
9490ff27 KH |
4282 | return 0; |
4283 | } | |
4284 | ||
c0ff4b85 | 4285 | static void mem_cgroup_oom_notify(struct mem_cgroup *memcg) |
9490ff27 | 4286 | { |
7d74b06f KH |
4287 | struct mem_cgroup *iter; |
4288 | ||
c0ff4b85 | 4289 | for_each_mem_cgroup_tree(iter, memcg) |
7d74b06f | 4290 | mem_cgroup_oom_notify_cb(iter); |
9490ff27 KH |
4291 | } |
4292 | ||
59b6f873 | 4293 | static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg, |
347c4a87 | 4294 | struct eventfd_ctx *eventfd, const char *args, enum res_type type) |
2e72b634 | 4295 | { |
2c488db2 KS |
4296 | struct mem_cgroup_thresholds *thresholds; |
4297 | struct mem_cgroup_threshold_ary *new; | |
3e32cb2e JW |
4298 | unsigned long threshold; |
4299 | unsigned long usage; | |
2c488db2 | 4300 | int i, size, ret; |
2e72b634 | 4301 | |
650c5e56 | 4302 | ret = page_counter_memparse(args, "-1", &threshold); |
2e72b634 KS |
4303 | if (ret) |
4304 | return ret; | |
4305 | ||
4306 | mutex_lock(&memcg->thresholds_lock); | |
2c488db2 | 4307 | |
05b84301 | 4308 | if (type == _MEM) { |
2c488db2 | 4309 | thresholds = &memcg->thresholds; |
ce00a967 | 4310 | usage = mem_cgroup_usage(memcg, false); |
05b84301 | 4311 | } else if (type == _MEMSWAP) { |
2c488db2 | 4312 | thresholds = &memcg->memsw_thresholds; |
ce00a967 | 4313 | usage = mem_cgroup_usage(memcg, true); |
05b84301 | 4314 | } else |
2e72b634 KS |
4315 | BUG(); |
4316 | ||
2e72b634 | 4317 | /* Check if a threshold crossed before adding a new one */ |
2c488db2 | 4318 | if (thresholds->primary) |
2e72b634 KS |
4319 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
4320 | ||
2c488db2 | 4321 | size = thresholds->primary ? thresholds->primary->size + 1 : 1; |
2e72b634 KS |
4322 | |
4323 | /* Allocate memory for new array of thresholds */ | |
67b8046f | 4324 | new = kmalloc(struct_size(new, entries, size), GFP_KERNEL); |
2c488db2 | 4325 | if (!new) { |
2e72b634 KS |
4326 | ret = -ENOMEM; |
4327 | goto unlock; | |
4328 | } | |
2c488db2 | 4329 | new->size = size; |
2e72b634 KS |
4330 | |
4331 | /* Copy thresholds (if any) to new array */ | |
e90342e6 GS |
4332 | if (thresholds->primary) |
4333 | memcpy(new->entries, thresholds->primary->entries, | |
4334 | flex_array_size(new, entries, size - 1)); | |
2c488db2 | 4335 | |
2e72b634 | 4336 | /* Add new threshold */ |
2c488db2 KS |
4337 | new->entries[size - 1].eventfd = eventfd; |
4338 | new->entries[size - 1].threshold = threshold; | |
2e72b634 KS |
4339 | |
4340 | /* Sort thresholds. Registering of new threshold isn't time-critical */ | |
61e604e6 | 4341 | sort(new->entries, size, sizeof(*new->entries), |
2e72b634 KS |
4342 | compare_thresholds, NULL); |
4343 | ||
4344 | /* Find current threshold */ | |
2c488db2 | 4345 | new->current_threshold = -1; |
2e72b634 | 4346 | for (i = 0; i < size; i++) { |
748dad36 | 4347 | if (new->entries[i].threshold <= usage) { |
2e72b634 | 4348 | /* |
2c488db2 KS |
4349 | * new->current_threshold will not be used until |
4350 | * rcu_assign_pointer(), so it's safe to increment | |
2e72b634 KS |
4351 | * it here. |
4352 | */ | |
2c488db2 | 4353 | ++new->current_threshold; |
748dad36 SZ |
4354 | } else |
4355 | break; | |
2e72b634 KS |
4356 | } |
4357 | ||
2c488db2 KS |
4358 | /* Free old spare buffer and save old primary buffer as spare */ |
4359 | kfree(thresholds->spare); | |
4360 | thresholds->spare = thresholds->primary; | |
4361 | ||
4362 | rcu_assign_pointer(thresholds->primary, new); | |
2e72b634 | 4363 | |
907860ed | 4364 | /* To be sure that nobody uses thresholds */ |
2e72b634 KS |
4365 | synchronize_rcu(); |
4366 | ||
2e72b634 KS |
4367 | unlock: |
4368 | mutex_unlock(&memcg->thresholds_lock); | |
4369 | ||
4370 | return ret; | |
4371 | } | |
4372 | ||
59b6f873 | 4373 | static int mem_cgroup_usage_register_event(struct mem_cgroup *memcg, |
347c4a87 TH |
4374 | struct eventfd_ctx *eventfd, const char *args) |
4375 | { | |
59b6f873 | 4376 | return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEM); |
347c4a87 TH |
4377 | } |
4378 | ||
59b6f873 | 4379 | static int memsw_cgroup_usage_register_event(struct mem_cgroup *memcg, |
347c4a87 TH |
4380 | struct eventfd_ctx *eventfd, const char *args) |
4381 | { | |
59b6f873 | 4382 | return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEMSWAP); |
347c4a87 TH |
4383 | } |
4384 | ||
59b6f873 | 4385 | static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg, |
347c4a87 | 4386 | struct eventfd_ctx *eventfd, enum res_type type) |
2e72b634 | 4387 | { |
2c488db2 KS |
4388 | struct mem_cgroup_thresholds *thresholds; |
4389 | struct mem_cgroup_threshold_ary *new; | |
3e32cb2e | 4390 | unsigned long usage; |
7d36665a | 4391 | int i, j, size, entries; |
2e72b634 KS |
4392 | |
4393 | mutex_lock(&memcg->thresholds_lock); | |
05b84301 JW |
4394 | |
4395 | if (type == _MEM) { | |
2c488db2 | 4396 | thresholds = &memcg->thresholds; |
ce00a967 | 4397 | usage = mem_cgroup_usage(memcg, false); |
05b84301 | 4398 | } else if (type == _MEMSWAP) { |
2c488db2 | 4399 | thresholds = &memcg->memsw_thresholds; |
ce00a967 | 4400 | usage = mem_cgroup_usage(memcg, true); |
05b84301 | 4401 | } else |
2e72b634 KS |
4402 | BUG(); |
4403 | ||
371528ca AV |
4404 | if (!thresholds->primary) |
4405 | goto unlock; | |
4406 | ||
2e72b634 KS |
4407 | /* Check if a threshold crossed before removing */ |
4408 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); | |
4409 | ||
4410 | /* Calculate new number of threshold */ | |
7d36665a | 4411 | size = entries = 0; |
2c488db2 KS |
4412 | for (i = 0; i < thresholds->primary->size; i++) { |
4413 | if (thresholds->primary->entries[i].eventfd != eventfd) | |
2e72b634 | 4414 | size++; |
7d36665a CX |
4415 | else |
4416 | entries++; | |
2e72b634 KS |
4417 | } |
4418 | ||
2c488db2 | 4419 | new = thresholds->spare; |
907860ed | 4420 | |
7d36665a CX |
4421 | /* If no items related to eventfd have been cleared, nothing to do */ |
4422 | if (!entries) | |
4423 | goto unlock; | |
4424 | ||
2e72b634 KS |
4425 | /* Set thresholds array to NULL if we don't have thresholds */ |
4426 | if (!size) { | |
2c488db2 KS |
4427 | kfree(new); |
4428 | new = NULL; | |
907860ed | 4429 | goto swap_buffers; |
2e72b634 KS |
4430 | } |
4431 | ||
2c488db2 | 4432 | new->size = size; |
2e72b634 KS |
4433 | |
4434 | /* Copy thresholds and find current threshold */ | |
2c488db2 KS |
4435 | new->current_threshold = -1; |
4436 | for (i = 0, j = 0; i < thresholds->primary->size; i++) { | |
4437 | if (thresholds->primary->entries[i].eventfd == eventfd) | |
2e72b634 KS |
4438 | continue; |
4439 | ||
2c488db2 | 4440 | new->entries[j] = thresholds->primary->entries[i]; |
748dad36 | 4441 | if (new->entries[j].threshold <= usage) { |
2e72b634 | 4442 | /* |
2c488db2 | 4443 | * new->current_threshold will not be used |
2e72b634 KS |
4444 | * until rcu_assign_pointer(), so it's safe to increment |
4445 | * it here. | |
4446 | */ | |
2c488db2 | 4447 | ++new->current_threshold; |
2e72b634 KS |
4448 | } |
4449 | j++; | |
4450 | } | |
4451 | ||
907860ed | 4452 | swap_buffers: |
2c488db2 KS |
4453 | /* Swap primary and spare array */ |
4454 | thresholds->spare = thresholds->primary; | |
8c757763 | 4455 | |
2c488db2 | 4456 | rcu_assign_pointer(thresholds->primary, new); |
2e72b634 | 4457 | |
907860ed | 4458 | /* To be sure that nobody uses thresholds */ |
2e72b634 | 4459 | synchronize_rcu(); |
6611d8d7 MC |
4460 | |
4461 | /* If all events are unregistered, free the spare array */ | |
4462 | if (!new) { | |
4463 | kfree(thresholds->spare); | |
4464 | thresholds->spare = NULL; | |
4465 | } | |
371528ca | 4466 | unlock: |
2e72b634 | 4467 | mutex_unlock(&memcg->thresholds_lock); |
2e72b634 | 4468 | } |
c1e862c1 | 4469 | |
59b6f873 | 4470 | static void mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg, |
347c4a87 TH |
4471 | struct eventfd_ctx *eventfd) |
4472 | { | |
59b6f873 | 4473 | return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEM); |
347c4a87 TH |
4474 | } |
4475 | ||
59b6f873 | 4476 | static void memsw_cgroup_usage_unregister_event(struct mem_cgroup *memcg, |
347c4a87 TH |
4477 | struct eventfd_ctx *eventfd) |
4478 | { | |
59b6f873 | 4479 | return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEMSWAP); |
347c4a87 TH |
4480 | } |
4481 | ||
59b6f873 | 4482 | static int mem_cgroup_oom_register_event(struct mem_cgroup *memcg, |
347c4a87 | 4483 | struct eventfd_ctx *eventfd, const char *args) |
9490ff27 | 4484 | { |
9490ff27 | 4485 | struct mem_cgroup_eventfd_list *event; |
9490ff27 | 4486 | |
9490ff27 KH |
4487 | event = kmalloc(sizeof(*event), GFP_KERNEL); |
4488 | if (!event) | |
4489 | return -ENOMEM; | |
4490 | ||
1af8efe9 | 4491 | spin_lock(&memcg_oom_lock); |
9490ff27 KH |
4492 | |
4493 | event->eventfd = eventfd; | |
4494 | list_add(&event->list, &memcg->oom_notify); | |
4495 | ||
4496 | /* already in OOM ? */ | |
c2b42d3c | 4497 | if (memcg->under_oom) |
9490ff27 | 4498 | eventfd_signal(eventfd, 1); |
1af8efe9 | 4499 | spin_unlock(&memcg_oom_lock); |
9490ff27 KH |
4500 | |
4501 | return 0; | |
4502 | } | |
4503 | ||
59b6f873 | 4504 | static void mem_cgroup_oom_unregister_event(struct mem_cgroup *memcg, |
347c4a87 | 4505 | struct eventfd_ctx *eventfd) |
9490ff27 | 4506 | { |
9490ff27 | 4507 | struct mem_cgroup_eventfd_list *ev, *tmp; |
9490ff27 | 4508 | |
1af8efe9 | 4509 | spin_lock(&memcg_oom_lock); |
9490ff27 | 4510 | |
c0ff4b85 | 4511 | list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) { |
9490ff27 KH |
4512 | if (ev->eventfd == eventfd) { |
4513 | list_del(&ev->list); | |
4514 | kfree(ev); | |
4515 | } | |
4516 | } | |
4517 | ||
1af8efe9 | 4518 | spin_unlock(&memcg_oom_lock); |
9490ff27 KH |
4519 | } |
4520 | ||
2da8ca82 | 4521 | static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v) |
3c11ecf4 | 4522 | { |
aa9694bb | 4523 | struct mem_cgroup *memcg = mem_cgroup_from_seq(sf); |
3c11ecf4 | 4524 | |
17c56de6 | 4525 | seq_printf(sf, "oom_kill_disable %d\n", READ_ONCE(memcg->oom_kill_disable)); |
c2b42d3c | 4526 | seq_printf(sf, "under_oom %d\n", (bool)memcg->under_oom); |
fe6bdfc8 RG |
4527 | seq_printf(sf, "oom_kill %lu\n", |
4528 | atomic_long_read(&memcg->memory_events[MEMCG_OOM_KILL])); | |
3c11ecf4 KH |
4529 | return 0; |
4530 | } | |
4531 | ||
182446d0 | 4532 | static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css, |
3c11ecf4 KH |
4533 | struct cftype *cft, u64 val) |
4534 | { | |
182446d0 | 4535 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
3c11ecf4 KH |
4536 | |
4537 | /* cannot set to root cgroup and only 0 and 1 are allowed */ | |
a4792030 | 4538 | if (mem_cgroup_is_root(memcg) || !((val == 0) || (val == 1))) |
3c11ecf4 KH |
4539 | return -EINVAL; |
4540 | ||
17c56de6 | 4541 | WRITE_ONCE(memcg->oom_kill_disable, val); |
4d845ebf | 4542 | if (!val) |
c0ff4b85 | 4543 | memcg_oom_recover(memcg); |
3dae7fec | 4544 | |
3c11ecf4 KH |
4545 | return 0; |
4546 | } | |
4547 | ||
52ebea74 TH |
4548 | #ifdef CONFIG_CGROUP_WRITEBACK |
4549 | ||
3a8e9ac8 TH |
4550 | #include <trace/events/writeback.h> |
4551 | ||
841710aa TH |
4552 | static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp) |
4553 | { | |
4554 | return wb_domain_init(&memcg->cgwb_domain, gfp); | |
4555 | } | |
4556 | ||
4557 | static void memcg_wb_domain_exit(struct mem_cgroup *memcg) | |
4558 | { | |
4559 | wb_domain_exit(&memcg->cgwb_domain); | |
4560 | } | |
4561 | ||
2529bb3a TH |
4562 | static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg) |
4563 | { | |
4564 | wb_domain_size_changed(&memcg->cgwb_domain); | |
4565 | } | |
4566 | ||
841710aa TH |
4567 | struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb) |
4568 | { | |
4569 | struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css); | |
4570 | ||
4571 | if (!memcg->css.parent) | |
4572 | return NULL; | |
4573 | ||
4574 | return &memcg->cgwb_domain; | |
4575 | } | |
4576 | ||
c2aa723a TH |
4577 | /** |
4578 | * mem_cgroup_wb_stats - retrieve writeback related stats from its memcg | |
4579 | * @wb: bdi_writeback in question | |
c5edf9cd TH |
4580 | * @pfilepages: out parameter for number of file pages |
4581 | * @pheadroom: out parameter for number of allocatable pages according to memcg | |
c2aa723a TH |
4582 | * @pdirty: out parameter for number of dirty pages |
4583 | * @pwriteback: out parameter for number of pages under writeback | |
4584 | * | |
c5edf9cd TH |
4585 | * Determine the numbers of file, headroom, dirty, and writeback pages in |
4586 | * @wb's memcg. File, dirty and writeback are self-explanatory. Headroom | |
4587 | * is a bit more involved. | |
c2aa723a | 4588 | * |
c5edf9cd TH |
4589 | * A memcg's headroom is "min(max, high) - used". In the hierarchy, the |
4590 | * headroom is calculated as the lowest headroom of itself and the | |
4591 | * ancestors. Note that this doesn't consider the actual amount of | |
4592 | * available memory in the system. The caller should further cap | |
4593 | * *@pheadroom accordingly. | |
c2aa723a | 4594 | */ |
c5edf9cd TH |
4595 | void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages, |
4596 | unsigned long *pheadroom, unsigned long *pdirty, | |
4597 | unsigned long *pwriteback) | |
c2aa723a TH |
4598 | { |
4599 | struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css); | |
4600 | struct mem_cgroup *parent; | |
c2aa723a | 4601 | |
190409ca | 4602 | mem_cgroup_flush_stats(); |
c2aa723a | 4603 | |
2d146aa3 JW |
4604 | *pdirty = memcg_page_state(memcg, NR_FILE_DIRTY); |
4605 | *pwriteback = memcg_page_state(memcg, NR_WRITEBACK); | |
4606 | *pfilepages = memcg_page_state(memcg, NR_INACTIVE_FILE) + | |
4607 | memcg_page_state(memcg, NR_ACTIVE_FILE); | |
c2aa723a | 4608 | |
2d146aa3 | 4609 | *pheadroom = PAGE_COUNTER_MAX; |
c2aa723a | 4610 | while ((parent = parent_mem_cgroup(memcg))) { |
15b42562 | 4611 | unsigned long ceiling = min(READ_ONCE(memcg->memory.max), |
d1663a90 | 4612 | READ_ONCE(memcg->memory.high)); |
c2aa723a TH |
4613 | unsigned long used = page_counter_read(&memcg->memory); |
4614 | ||
c5edf9cd | 4615 | *pheadroom = min(*pheadroom, ceiling - min(ceiling, used)); |
c2aa723a TH |
4616 | memcg = parent; |
4617 | } | |
c2aa723a TH |
4618 | } |
4619 | ||
97b27821 TH |
4620 | /* |
4621 | * Foreign dirty flushing | |
4622 | * | |
4623 | * There's an inherent mismatch between memcg and writeback. The former | |
f0953a1b | 4624 | * tracks ownership per-page while the latter per-inode. This was a |
97b27821 TH |
4625 | * deliberate design decision because honoring per-page ownership in the |
4626 | * writeback path is complicated, may lead to higher CPU and IO overheads | |
4627 | * and deemed unnecessary given that write-sharing an inode across | |
4628 | * different cgroups isn't a common use-case. | |
4629 | * | |
4630 | * Combined with inode majority-writer ownership switching, this works well | |
4631 | * enough in most cases but there are some pathological cases. For | |
4632 | * example, let's say there are two cgroups A and B which keep writing to | |
4633 | * different but confined parts of the same inode. B owns the inode and | |
4634 | * A's memory is limited far below B's. A's dirty ratio can rise enough to | |
4635 | * trigger balance_dirty_pages() sleeps but B's can be low enough to avoid | |
4636 | * triggering background writeback. A will be slowed down without a way to | |
4637 | * make writeback of the dirty pages happen. | |
4638 | * | |
f0953a1b | 4639 | * Conditions like the above can lead to a cgroup getting repeatedly and |
97b27821 | 4640 | * severely throttled after making some progress after each |
f0953a1b | 4641 | * dirty_expire_interval while the underlying IO device is almost |
97b27821 TH |
4642 | * completely idle. |
4643 | * | |
4644 | * Solving this problem completely requires matching the ownership tracking | |
4645 | * granularities between memcg and writeback in either direction. However, | |
4646 | * the more egregious behaviors can be avoided by simply remembering the | |
4647 | * most recent foreign dirtying events and initiating remote flushes on | |
4648 | * them when local writeback isn't enough to keep the memory clean enough. | |
4649 | * | |
4650 | * The following two functions implement such mechanism. When a foreign | |
4651 | * page - a page whose memcg and writeback ownerships don't match - is | |
4652 | * dirtied, mem_cgroup_track_foreign_dirty() records the inode owning | |
4653 | * bdi_writeback on the page owning memcg. When balance_dirty_pages() | |
4654 | * decides that the memcg needs to sleep due to high dirty ratio, it calls | |
4655 | * mem_cgroup_flush_foreign() which queues writeback on the recorded | |
4656 | * foreign bdi_writebacks which haven't expired. Both the numbers of | |
4657 | * recorded bdi_writebacks and concurrent in-flight foreign writebacks are | |
4658 | * limited to MEMCG_CGWB_FRN_CNT. | |
4659 | * | |
4660 | * The mechanism only remembers IDs and doesn't hold any object references. | |
4661 | * As being wrong occasionally doesn't matter, updates and accesses to the | |
4662 | * records are lockless and racy. | |
4663 | */ | |
9d8053fc | 4664 | void mem_cgroup_track_foreign_dirty_slowpath(struct folio *folio, |
97b27821 TH |
4665 | struct bdi_writeback *wb) |
4666 | { | |
9d8053fc | 4667 | struct mem_cgroup *memcg = folio_memcg(folio); |
97b27821 TH |
4668 | struct memcg_cgwb_frn *frn; |
4669 | u64 now = get_jiffies_64(); | |
4670 | u64 oldest_at = now; | |
4671 | int oldest = -1; | |
4672 | int i; | |
4673 | ||
9d8053fc | 4674 | trace_track_foreign_dirty(folio, wb); |
3a8e9ac8 | 4675 | |
97b27821 TH |
4676 | /* |
4677 | * Pick the slot to use. If there is already a slot for @wb, keep | |
4678 | * using it. If not replace the oldest one which isn't being | |
4679 | * written out. | |
4680 | */ | |
4681 | for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) { | |
4682 | frn = &memcg->cgwb_frn[i]; | |
4683 | if (frn->bdi_id == wb->bdi->id && | |
4684 | frn->memcg_id == wb->memcg_css->id) | |
4685 | break; | |
4686 | if (time_before64(frn->at, oldest_at) && | |
4687 | atomic_read(&frn->done.cnt) == 1) { | |
4688 | oldest = i; | |
4689 | oldest_at = frn->at; | |
4690 | } | |
4691 | } | |
4692 | ||
4693 | if (i < MEMCG_CGWB_FRN_CNT) { | |
4694 | /* | |
4695 | * Re-using an existing one. Update timestamp lazily to | |
4696 | * avoid making the cacheline hot. We want them to be | |
4697 | * reasonably up-to-date and significantly shorter than | |
4698 | * dirty_expire_interval as that's what expires the record. | |
4699 | * Use the shorter of 1s and dirty_expire_interval / 8. | |
4700 | */ | |
4701 | unsigned long update_intv = | |
4702 | min_t(unsigned long, HZ, | |
4703 | msecs_to_jiffies(dirty_expire_interval * 10) / 8); | |
4704 | ||
4705 | if (time_before64(frn->at, now - update_intv)) | |
4706 | frn->at = now; | |
4707 | } else if (oldest >= 0) { | |
4708 | /* replace the oldest free one */ | |
4709 | frn = &memcg->cgwb_frn[oldest]; | |
4710 | frn->bdi_id = wb->bdi->id; | |
4711 | frn->memcg_id = wb->memcg_css->id; | |
4712 | frn->at = now; | |
4713 | } | |
4714 | } | |
4715 | ||
4716 | /* issue foreign writeback flushes for recorded foreign dirtying events */ | |
4717 | void mem_cgroup_flush_foreign(struct bdi_writeback *wb) | |
4718 | { | |
4719 | struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css); | |
4720 | unsigned long intv = msecs_to_jiffies(dirty_expire_interval * 10); | |
4721 | u64 now = jiffies_64; | |
4722 | int i; | |
4723 | ||
4724 | for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) { | |
4725 | struct memcg_cgwb_frn *frn = &memcg->cgwb_frn[i]; | |
4726 | ||
4727 | /* | |
4728 | * If the record is older than dirty_expire_interval, | |
4729 | * writeback on it has already started. No need to kick it | |
4730 | * off again. Also, don't start a new one if there's | |
4731 | * already one in flight. | |
4732 | */ | |
4733 | if (time_after64(frn->at, now - intv) && | |
4734 | atomic_read(&frn->done.cnt) == 1) { | |
4735 | frn->at = 0; | |
3a8e9ac8 | 4736 | trace_flush_foreign(wb, frn->bdi_id, frn->memcg_id); |
7490a2d2 | 4737 | cgroup_writeback_by_id(frn->bdi_id, frn->memcg_id, |
97b27821 TH |
4738 | WB_REASON_FOREIGN_FLUSH, |
4739 | &frn->done); | |
4740 | } | |
4741 | } | |
4742 | } | |
4743 | ||
841710aa TH |
4744 | #else /* CONFIG_CGROUP_WRITEBACK */ |
4745 | ||
4746 | static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp) | |
4747 | { | |
4748 | return 0; | |
4749 | } | |
4750 | ||
4751 | static void memcg_wb_domain_exit(struct mem_cgroup *memcg) | |
4752 | { | |
4753 | } | |
4754 | ||
2529bb3a TH |
4755 | static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg) |
4756 | { | |
4757 | } | |
4758 | ||
52ebea74 TH |
4759 | #endif /* CONFIG_CGROUP_WRITEBACK */ |
4760 | ||
3bc942f3 TH |
4761 | /* |
4762 | * DO NOT USE IN NEW FILES. | |
4763 | * | |
4764 | * "cgroup.event_control" implementation. | |
4765 | * | |
4766 | * This is way over-engineered. It tries to support fully configurable | |
4767 | * events for each user. Such level of flexibility is completely | |
4768 | * unnecessary especially in the light of the planned unified hierarchy. | |
4769 | * | |
4770 | * Please deprecate this and replace with something simpler if at all | |
4771 | * possible. | |
4772 | */ | |
4773 | ||
79bd9814 TH |
4774 | /* |
4775 | * Unregister event and free resources. | |
4776 | * | |
4777 | * Gets called from workqueue. | |
4778 | */ | |
3bc942f3 | 4779 | static void memcg_event_remove(struct work_struct *work) |
79bd9814 | 4780 | { |
3bc942f3 TH |
4781 | struct mem_cgroup_event *event = |
4782 | container_of(work, struct mem_cgroup_event, remove); | |
59b6f873 | 4783 | struct mem_cgroup *memcg = event->memcg; |
79bd9814 TH |
4784 | |
4785 | remove_wait_queue(event->wqh, &event->wait); | |
4786 | ||
59b6f873 | 4787 | event->unregister_event(memcg, event->eventfd); |
79bd9814 TH |
4788 | |
4789 | /* Notify userspace the event is going away. */ | |
4790 | eventfd_signal(event->eventfd, 1); | |
4791 | ||
4792 | eventfd_ctx_put(event->eventfd); | |
4793 | kfree(event); | |
59b6f873 | 4794 | css_put(&memcg->css); |
79bd9814 TH |
4795 | } |
4796 | ||
4797 | /* | |
a9a08845 | 4798 | * Gets called on EPOLLHUP on eventfd when user closes it. |
79bd9814 TH |
4799 | * |
4800 | * Called with wqh->lock held and interrupts disabled. | |
4801 | */ | |
ac6424b9 | 4802 | static int memcg_event_wake(wait_queue_entry_t *wait, unsigned mode, |
3bc942f3 | 4803 | int sync, void *key) |
79bd9814 | 4804 | { |
3bc942f3 TH |
4805 | struct mem_cgroup_event *event = |
4806 | container_of(wait, struct mem_cgroup_event, wait); | |
59b6f873 | 4807 | struct mem_cgroup *memcg = event->memcg; |
3ad6f93e | 4808 | __poll_t flags = key_to_poll(key); |
79bd9814 | 4809 | |
a9a08845 | 4810 | if (flags & EPOLLHUP) { |
79bd9814 TH |
4811 | /* |
4812 | * If the event has been detached at cgroup removal, we | |
4813 | * can simply return knowing the other side will cleanup | |
4814 | * for us. | |
4815 | * | |
4816 | * We can't race against event freeing since the other | |
4817 | * side will require wqh->lock via remove_wait_queue(), | |
4818 | * which we hold. | |
4819 | */ | |
fba94807 | 4820 | spin_lock(&memcg->event_list_lock); |
79bd9814 TH |
4821 | if (!list_empty(&event->list)) { |
4822 | list_del_init(&event->list); | |
4823 | /* | |
4824 | * We are in atomic context, but cgroup_event_remove() | |
4825 | * may sleep, so we have to call it in workqueue. | |
4826 | */ | |
4827 | schedule_work(&event->remove); | |
4828 | } | |
fba94807 | 4829 | spin_unlock(&memcg->event_list_lock); |
79bd9814 TH |
4830 | } |
4831 | ||
4832 | return 0; | |
4833 | } | |
4834 | ||
3bc942f3 | 4835 | static void memcg_event_ptable_queue_proc(struct file *file, |
79bd9814 TH |
4836 | wait_queue_head_t *wqh, poll_table *pt) |
4837 | { | |
3bc942f3 TH |
4838 | struct mem_cgroup_event *event = |
4839 | container_of(pt, struct mem_cgroup_event, pt); | |
79bd9814 TH |
4840 | |
4841 | event->wqh = wqh; | |
4842 | add_wait_queue(wqh, &event->wait); | |
4843 | } | |
4844 | ||
4845 | /* | |
3bc942f3 TH |
4846 | * DO NOT USE IN NEW FILES. |
4847 | * | |
79bd9814 TH |
4848 | * Parse input and register new cgroup event handler. |
4849 | * | |
4850 | * Input must be in format '<event_fd> <control_fd> <args>'. | |
4851 | * Interpretation of args is defined by control file implementation. | |
4852 | */ | |
451af504 TH |
4853 | static ssize_t memcg_write_event_control(struct kernfs_open_file *of, |
4854 | char *buf, size_t nbytes, loff_t off) | |
79bd9814 | 4855 | { |
451af504 | 4856 | struct cgroup_subsys_state *css = of_css(of); |
fba94807 | 4857 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
3bc942f3 | 4858 | struct mem_cgroup_event *event; |
79bd9814 TH |
4859 | struct cgroup_subsys_state *cfile_css; |
4860 | unsigned int efd, cfd; | |
4861 | struct fd efile; | |
4862 | struct fd cfile; | |
4a7ba45b | 4863 | struct dentry *cdentry; |
fba94807 | 4864 | const char *name; |
79bd9814 TH |
4865 | char *endp; |
4866 | int ret; | |
4867 | ||
2343e88d SAS |
4868 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) |
4869 | return -EOPNOTSUPP; | |
4870 | ||
451af504 TH |
4871 | buf = strstrip(buf); |
4872 | ||
4873 | efd = simple_strtoul(buf, &endp, 10); | |
79bd9814 TH |
4874 | if (*endp != ' ') |
4875 | return -EINVAL; | |
451af504 | 4876 | buf = endp + 1; |
79bd9814 | 4877 | |
451af504 | 4878 | cfd = simple_strtoul(buf, &endp, 10); |
79bd9814 TH |
4879 | if ((*endp != ' ') && (*endp != '\0')) |
4880 | return -EINVAL; | |
451af504 | 4881 | buf = endp + 1; |
79bd9814 TH |
4882 | |
4883 | event = kzalloc(sizeof(*event), GFP_KERNEL); | |
4884 | if (!event) | |
4885 | return -ENOMEM; | |
4886 | ||
59b6f873 | 4887 | event->memcg = memcg; |
79bd9814 | 4888 | INIT_LIST_HEAD(&event->list); |
3bc942f3 TH |
4889 | init_poll_funcptr(&event->pt, memcg_event_ptable_queue_proc); |
4890 | init_waitqueue_func_entry(&event->wait, memcg_event_wake); | |
4891 | INIT_WORK(&event->remove, memcg_event_remove); | |
79bd9814 TH |
4892 | |
4893 | efile = fdget(efd); | |
4894 | if (!efile.file) { | |
4895 | ret = -EBADF; | |
4896 | goto out_kfree; | |
4897 | } | |
4898 | ||
4899 | event->eventfd = eventfd_ctx_fileget(efile.file); | |
4900 | if (IS_ERR(event->eventfd)) { | |
4901 | ret = PTR_ERR(event->eventfd); | |
4902 | goto out_put_efile; | |
4903 | } | |
4904 | ||
4905 | cfile = fdget(cfd); | |
4906 | if (!cfile.file) { | |
4907 | ret = -EBADF; | |
4908 | goto out_put_eventfd; | |
4909 | } | |
4910 | ||
4911 | /* the process need read permission on control file */ | |
4912 | /* AV: shouldn't we check that it's been opened for read instead? */ | |
02f92b38 | 4913 | ret = file_permission(cfile.file, MAY_READ); |
79bd9814 TH |
4914 | if (ret < 0) |
4915 | goto out_put_cfile; | |
4916 | ||
4a7ba45b TH |
4917 | /* |
4918 | * The control file must be a regular cgroup1 file. As a regular cgroup | |
4919 | * file can't be renamed, it's safe to access its name afterwards. | |
4920 | */ | |
4921 | cdentry = cfile.file->f_path.dentry; | |
4922 | if (cdentry->d_sb->s_type != &cgroup_fs_type || !d_is_reg(cdentry)) { | |
4923 | ret = -EINVAL; | |
4924 | goto out_put_cfile; | |
4925 | } | |
4926 | ||
fba94807 TH |
4927 | /* |
4928 | * Determine the event callbacks and set them in @event. This used | |
4929 | * to be done via struct cftype but cgroup core no longer knows | |
4930 | * about these events. The following is crude but the whole thing | |
4931 | * is for compatibility anyway. | |
3bc942f3 TH |
4932 | * |
4933 | * DO NOT ADD NEW FILES. | |
fba94807 | 4934 | */ |
4a7ba45b | 4935 | name = cdentry->d_name.name; |
fba94807 TH |
4936 | |
4937 | if (!strcmp(name, "memory.usage_in_bytes")) { | |
4938 | event->register_event = mem_cgroup_usage_register_event; | |
4939 | event->unregister_event = mem_cgroup_usage_unregister_event; | |
4940 | } else if (!strcmp(name, "memory.oom_control")) { | |
4941 | event->register_event = mem_cgroup_oom_register_event; | |
4942 | event->unregister_event = mem_cgroup_oom_unregister_event; | |
4943 | } else if (!strcmp(name, "memory.pressure_level")) { | |
4944 | event->register_event = vmpressure_register_event; | |
4945 | event->unregister_event = vmpressure_unregister_event; | |
4946 | } else if (!strcmp(name, "memory.memsw.usage_in_bytes")) { | |
347c4a87 TH |
4947 | event->register_event = memsw_cgroup_usage_register_event; |
4948 | event->unregister_event = memsw_cgroup_usage_unregister_event; | |
fba94807 TH |
4949 | } else { |
4950 | ret = -EINVAL; | |
4951 | goto out_put_cfile; | |
4952 | } | |
4953 | ||
79bd9814 | 4954 | /* |
b5557c4c TH |
4955 | * Verify @cfile should belong to @css. Also, remaining events are |
4956 | * automatically removed on cgroup destruction but the removal is | |
4957 | * asynchronous, so take an extra ref on @css. | |
79bd9814 | 4958 | */ |
4a7ba45b | 4959 | cfile_css = css_tryget_online_from_dir(cdentry->d_parent, |
ec903c0c | 4960 | &memory_cgrp_subsys); |
79bd9814 | 4961 | ret = -EINVAL; |
5a17f543 | 4962 | if (IS_ERR(cfile_css)) |
79bd9814 | 4963 | goto out_put_cfile; |
5a17f543 TH |
4964 | if (cfile_css != css) { |
4965 | css_put(cfile_css); | |
79bd9814 | 4966 | goto out_put_cfile; |
5a17f543 | 4967 | } |
79bd9814 | 4968 | |
451af504 | 4969 | ret = event->register_event(memcg, event->eventfd, buf); |
79bd9814 TH |
4970 | if (ret) |
4971 | goto out_put_css; | |
4972 | ||
9965ed17 | 4973 | vfs_poll(efile.file, &event->pt); |
79bd9814 | 4974 | |
4ba9515d | 4975 | spin_lock_irq(&memcg->event_list_lock); |
fba94807 | 4976 | list_add(&event->list, &memcg->event_list); |
4ba9515d | 4977 | spin_unlock_irq(&memcg->event_list_lock); |
79bd9814 TH |
4978 | |
4979 | fdput(cfile); | |
4980 | fdput(efile); | |
4981 | ||
451af504 | 4982 | return nbytes; |
79bd9814 TH |
4983 | |
4984 | out_put_css: | |
b5557c4c | 4985 | css_put(css); |
79bd9814 TH |
4986 | out_put_cfile: |
4987 | fdput(cfile); | |
4988 | out_put_eventfd: | |
4989 | eventfd_ctx_put(event->eventfd); | |
4990 | out_put_efile: | |
4991 | fdput(efile); | |
4992 | out_kfree: | |
4993 | kfree(event); | |
4994 | ||
4995 | return ret; | |
4996 | } | |
4997 | ||
c29b5b3d MS |
4998 | #if defined(CONFIG_MEMCG_KMEM) && (defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)) |
4999 | static int mem_cgroup_slab_show(struct seq_file *m, void *p) | |
5000 | { | |
5001 | /* | |
5002 | * Deprecated. | |
df4ae285 | 5003 | * Please, take a look at tools/cgroup/memcg_slabinfo.py . |
c29b5b3d MS |
5004 | */ |
5005 | return 0; | |
5006 | } | |
5007 | #endif | |
5008 | ||
dddb44ff YA |
5009 | static int memory_stat_show(struct seq_file *m, void *v); |
5010 | ||
241994ed | 5011 | static struct cftype mem_cgroup_legacy_files[] = { |
8cdea7c0 | 5012 | { |
0eea1030 | 5013 | .name = "usage_in_bytes", |
8c7c6e34 | 5014 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
791badbd | 5015 | .read_u64 = mem_cgroup_read_u64, |
8cdea7c0 | 5016 | }, |
c84872e1 PE |
5017 | { |
5018 | .name = "max_usage_in_bytes", | |
8c7c6e34 | 5019 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
6770c64e | 5020 | .write = mem_cgroup_reset, |
791badbd | 5021 | .read_u64 = mem_cgroup_read_u64, |
c84872e1 | 5022 | }, |
8cdea7c0 | 5023 | { |
0eea1030 | 5024 | .name = "limit_in_bytes", |
8c7c6e34 | 5025 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
451af504 | 5026 | .write = mem_cgroup_write, |
791badbd | 5027 | .read_u64 = mem_cgroup_read_u64, |
8cdea7c0 | 5028 | }, |
296c81d8 BS |
5029 | { |
5030 | .name = "soft_limit_in_bytes", | |
5031 | .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT), | |
451af504 | 5032 | .write = mem_cgroup_write, |
791badbd | 5033 | .read_u64 = mem_cgroup_read_u64, |
296c81d8 | 5034 | }, |
8cdea7c0 BS |
5035 | { |
5036 | .name = "failcnt", | |
8c7c6e34 | 5037 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
6770c64e | 5038 | .write = mem_cgroup_reset, |
791badbd | 5039 | .read_u64 = mem_cgroup_read_u64, |
8cdea7c0 | 5040 | }, |
d2ceb9b7 KH |
5041 | { |
5042 | .name = "stat", | |
dddb44ff | 5043 | .seq_show = memory_stat_show, |
d2ceb9b7 | 5044 | }, |
c1e862c1 KH |
5045 | { |
5046 | .name = "force_empty", | |
6770c64e | 5047 | .write = mem_cgroup_force_empty_write, |
c1e862c1 | 5048 | }, |
18f59ea7 BS |
5049 | { |
5050 | .name = "use_hierarchy", | |
5051 | .write_u64 = mem_cgroup_hierarchy_write, | |
5052 | .read_u64 = mem_cgroup_hierarchy_read, | |
5053 | }, | |
79bd9814 | 5054 | { |
3bc942f3 | 5055 | .name = "cgroup.event_control", /* XXX: for compat */ |
451af504 | 5056 | .write = memcg_write_event_control, |
7dbdb199 | 5057 | .flags = CFTYPE_NO_PREFIX | CFTYPE_WORLD_WRITABLE, |
79bd9814 | 5058 | }, |
a7885eb8 KM |
5059 | { |
5060 | .name = "swappiness", | |
5061 | .read_u64 = mem_cgroup_swappiness_read, | |
5062 | .write_u64 = mem_cgroup_swappiness_write, | |
5063 | }, | |
7dc74be0 DN |
5064 | { |
5065 | .name = "move_charge_at_immigrate", | |
5066 | .read_u64 = mem_cgroup_move_charge_read, | |
5067 | .write_u64 = mem_cgroup_move_charge_write, | |
5068 | }, | |
9490ff27 KH |
5069 | { |
5070 | .name = "oom_control", | |
2da8ca82 | 5071 | .seq_show = mem_cgroup_oom_control_read, |
3c11ecf4 | 5072 | .write_u64 = mem_cgroup_oom_control_write, |
9490ff27 | 5073 | }, |
70ddf637 AV |
5074 | { |
5075 | .name = "pressure_level", | |
6b0ba2ab | 5076 | .seq_show = mem_cgroup_dummy_seq_show, |
70ddf637 | 5077 | }, |
406eb0c9 YH |
5078 | #ifdef CONFIG_NUMA |
5079 | { | |
5080 | .name = "numa_stat", | |
2da8ca82 | 5081 | .seq_show = memcg_numa_stat_show, |
406eb0c9 YH |
5082 | }, |
5083 | #endif | |
510fc4e1 GC |
5084 | { |
5085 | .name = "kmem.usage_in_bytes", | |
5086 | .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE), | |
791badbd | 5087 | .read_u64 = mem_cgroup_read_u64, |
510fc4e1 GC |
5088 | }, |
5089 | { | |
5090 | .name = "kmem.failcnt", | |
5091 | .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT), | |
6770c64e | 5092 | .write = mem_cgroup_reset, |
791badbd | 5093 | .read_u64 = mem_cgroup_read_u64, |
510fc4e1 GC |
5094 | }, |
5095 | { | |
5096 | .name = "kmem.max_usage_in_bytes", | |
5097 | .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE), | |
6770c64e | 5098 | .write = mem_cgroup_reset, |
791badbd | 5099 | .read_u64 = mem_cgroup_read_u64, |
510fc4e1 | 5100 | }, |
a87425a3 YS |
5101 | #if defined(CONFIG_MEMCG_KMEM) && \ |
5102 | (defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)) | |
749c5415 GC |
5103 | { |
5104 | .name = "kmem.slabinfo", | |
c29b5b3d | 5105 | .seq_show = mem_cgroup_slab_show, |
749c5415 GC |
5106 | }, |
5107 | #endif | |
d55f90bf VD |
5108 | { |
5109 | .name = "kmem.tcp.limit_in_bytes", | |
5110 | .private = MEMFILE_PRIVATE(_TCP, RES_LIMIT), | |
5111 | .write = mem_cgroup_write, | |
5112 | .read_u64 = mem_cgroup_read_u64, | |
5113 | }, | |
5114 | { | |
5115 | .name = "kmem.tcp.usage_in_bytes", | |
5116 | .private = MEMFILE_PRIVATE(_TCP, RES_USAGE), | |
5117 | .read_u64 = mem_cgroup_read_u64, | |
5118 | }, | |
5119 | { | |
5120 | .name = "kmem.tcp.failcnt", | |
5121 | .private = MEMFILE_PRIVATE(_TCP, RES_FAILCNT), | |
5122 | .write = mem_cgroup_reset, | |
5123 | .read_u64 = mem_cgroup_read_u64, | |
5124 | }, | |
5125 | { | |
5126 | .name = "kmem.tcp.max_usage_in_bytes", | |
5127 | .private = MEMFILE_PRIVATE(_TCP, RES_MAX_USAGE), | |
5128 | .write = mem_cgroup_reset, | |
5129 | .read_u64 = mem_cgroup_read_u64, | |
5130 | }, | |
6bc10349 | 5131 | { }, /* terminate */ |
af36f906 | 5132 | }; |
8c7c6e34 | 5133 | |
73f576c0 JW |
5134 | /* |
5135 | * Private memory cgroup IDR | |
5136 | * | |
5137 | * Swap-out records and page cache shadow entries need to store memcg | |
5138 | * references in constrained space, so we maintain an ID space that is | |
5139 | * limited to 16 bit (MEM_CGROUP_ID_MAX), limiting the total number of | |
5140 | * memory-controlled cgroups to 64k. | |
5141 | * | |
b8f2935f | 5142 | * However, there usually are many references to the offline CSS after |
73f576c0 JW |
5143 | * the cgroup has been destroyed, such as page cache or reclaimable |
5144 | * slab objects, that don't need to hang on to the ID. We want to keep | |
5145 | * those dead CSS from occupying IDs, or we might quickly exhaust the | |
5146 | * relatively small ID space and prevent the creation of new cgroups | |
5147 | * even when there are much fewer than 64k cgroups - possibly none. | |
5148 | * | |
5149 | * Maintain a private 16-bit ID space for memcg, and allow the ID to | |
5150 | * be freed and recycled when it's no longer needed, which is usually | |
5151 | * when the CSS is offlined. | |
5152 | * | |
5153 | * The only exception to that are records of swapped out tmpfs/shmem | |
5154 | * pages that need to be attributed to live ancestors on swapin. But | |
5155 | * those references are manageable from userspace. | |
5156 | */ | |
5157 | ||
5158 | static DEFINE_IDR(mem_cgroup_idr); | |
5159 | ||
7e97de0b KT |
5160 | static void mem_cgroup_id_remove(struct mem_cgroup *memcg) |
5161 | { | |
5162 | if (memcg->id.id > 0) { | |
5163 | idr_remove(&mem_cgroup_idr, memcg->id.id); | |
5164 | memcg->id.id = 0; | |
5165 | } | |
5166 | } | |
5167 | ||
c1514c0a VF |
5168 | static void __maybe_unused mem_cgroup_id_get_many(struct mem_cgroup *memcg, |
5169 | unsigned int n) | |
73f576c0 | 5170 | { |
1c2d479a | 5171 | refcount_add(n, &memcg->id.ref); |
73f576c0 JW |
5172 | } |
5173 | ||
615d66c3 | 5174 | static void mem_cgroup_id_put_many(struct mem_cgroup *memcg, unsigned int n) |
73f576c0 | 5175 | { |
1c2d479a | 5176 | if (refcount_sub_and_test(n, &memcg->id.ref)) { |
7e97de0b | 5177 | mem_cgroup_id_remove(memcg); |
73f576c0 JW |
5178 | |
5179 | /* Memcg ID pins CSS */ | |
5180 | css_put(&memcg->css); | |
5181 | } | |
5182 | } | |
5183 | ||
615d66c3 VD |
5184 | static inline void mem_cgroup_id_put(struct mem_cgroup *memcg) |
5185 | { | |
5186 | mem_cgroup_id_put_many(memcg, 1); | |
5187 | } | |
5188 | ||
73f576c0 JW |
5189 | /** |
5190 | * mem_cgroup_from_id - look up a memcg from a memcg id | |
5191 | * @id: the memcg id to look up | |
5192 | * | |
5193 | * Caller must hold rcu_read_lock(). | |
5194 | */ | |
5195 | struct mem_cgroup *mem_cgroup_from_id(unsigned short id) | |
5196 | { | |
5197 | WARN_ON_ONCE(!rcu_read_lock_held()); | |
5198 | return idr_find(&mem_cgroup_idr, id); | |
5199 | } | |
5200 | ||
c15187a4 RG |
5201 | #ifdef CONFIG_SHRINKER_DEBUG |
5202 | struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino) | |
5203 | { | |
5204 | struct cgroup *cgrp; | |
5205 | struct cgroup_subsys_state *css; | |
5206 | struct mem_cgroup *memcg; | |
5207 | ||
5208 | cgrp = cgroup_get_from_id(ino); | |
fa7e439c | 5209 | if (IS_ERR(cgrp)) |
c0f2df49 | 5210 | return ERR_CAST(cgrp); |
c15187a4 RG |
5211 | |
5212 | css = cgroup_get_e_css(cgrp, &memory_cgrp_subsys); | |
5213 | if (css) | |
5214 | memcg = container_of(css, struct mem_cgroup, css); | |
5215 | else | |
5216 | memcg = ERR_PTR(-ENOENT); | |
5217 | ||
5218 | cgroup_put(cgrp); | |
5219 | ||
5220 | return memcg; | |
5221 | } | |
5222 | #endif | |
5223 | ||
ef8f2327 | 5224 | static int alloc_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node) |
6d12e2d8 KH |
5225 | { |
5226 | struct mem_cgroup_per_node *pn; | |
8c9bb398 WY |
5227 | |
5228 | pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, node); | |
6d12e2d8 KH |
5229 | if (!pn) |
5230 | return 1; | |
1ecaab2b | 5231 | |
7e1c0d6f SB |
5232 | pn->lruvec_stats_percpu = alloc_percpu_gfp(struct lruvec_stats_percpu, |
5233 | GFP_KERNEL_ACCOUNT); | |
5234 | if (!pn->lruvec_stats_percpu) { | |
00f3ca2c JW |
5235 | kfree(pn); |
5236 | return 1; | |
5237 | } | |
5238 | ||
ef8f2327 | 5239 | lruvec_init(&pn->lruvec); |
ef8f2327 MG |
5240 | pn->memcg = memcg; |
5241 | ||
54f72fe0 | 5242 | memcg->nodeinfo[node] = pn; |
6d12e2d8 KH |
5243 | return 0; |
5244 | } | |
5245 | ||
ef8f2327 | 5246 | static void free_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node) |
1ecaab2b | 5247 | { |
00f3ca2c JW |
5248 | struct mem_cgroup_per_node *pn = memcg->nodeinfo[node]; |
5249 | ||
4eaf431f MH |
5250 | if (!pn) |
5251 | return; | |
5252 | ||
7e1c0d6f | 5253 | free_percpu(pn->lruvec_stats_percpu); |
00f3ca2c | 5254 | kfree(pn); |
1ecaab2b KH |
5255 | } |
5256 | ||
40e952f9 | 5257 | static void __mem_cgroup_free(struct mem_cgroup *memcg) |
59927fb9 | 5258 | { |
c8b2a36f | 5259 | int node; |
59927fb9 | 5260 | |
c8b2a36f | 5261 | for_each_node(node) |
ef8f2327 | 5262 | free_mem_cgroup_per_node_info(memcg, node); |
410f8e82 | 5263 | kfree(memcg->vmstats); |
871789d4 | 5264 | free_percpu(memcg->vmstats_percpu); |
8ff69e2c | 5265 | kfree(memcg); |
59927fb9 | 5266 | } |
3afe36b1 | 5267 | |
40e952f9 TE |
5268 | static void mem_cgroup_free(struct mem_cgroup *memcg) |
5269 | { | |
ec1c86b2 | 5270 | lru_gen_exit_memcg(memcg); |
40e952f9 TE |
5271 | memcg_wb_domain_exit(memcg); |
5272 | __mem_cgroup_free(memcg); | |
5273 | } | |
5274 | ||
0b8f73e1 | 5275 | static struct mem_cgroup *mem_cgroup_alloc(void) |
8cdea7c0 | 5276 | { |
d142e3e6 | 5277 | struct mem_cgroup *memcg; |
6d12e2d8 | 5278 | int node; |
97b27821 | 5279 | int __maybe_unused i; |
11d67612 | 5280 | long error = -ENOMEM; |
8cdea7c0 | 5281 | |
06b2c3b0 | 5282 | memcg = kzalloc(struct_size(memcg, nodeinfo, nr_node_ids), GFP_KERNEL); |
c0ff4b85 | 5283 | if (!memcg) |
11d67612 | 5284 | return ERR_PTR(error); |
0b8f73e1 | 5285 | |
73f576c0 | 5286 | memcg->id.id = idr_alloc(&mem_cgroup_idr, NULL, |
be740503 | 5287 | 1, MEM_CGROUP_ID_MAX + 1, GFP_KERNEL); |
11d67612 YS |
5288 | if (memcg->id.id < 0) { |
5289 | error = memcg->id.id; | |
73f576c0 | 5290 | goto fail; |
11d67612 | 5291 | } |
73f576c0 | 5292 | |
410f8e82 SB |
5293 | memcg->vmstats = kzalloc(sizeof(struct memcg_vmstats), GFP_KERNEL); |
5294 | if (!memcg->vmstats) | |
5295 | goto fail; | |
5296 | ||
3e38e0aa RG |
5297 | memcg->vmstats_percpu = alloc_percpu_gfp(struct memcg_vmstats_percpu, |
5298 | GFP_KERNEL_ACCOUNT); | |
871789d4 | 5299 | if (!memcg->vmstats_percpu) |
0b8f73e1 | 5300 | goto fail; |
78fb7466 | 5301 | |
3ed28fa1 | 5302 | for_each_node(node) |
ef8f2327 | 5303 | if (alloc_mem_cgroup_per_node_info(memcg, node)) |
0b8f73e1 | 5304 | goto fail; |
f64c3f54 | 5305 | |
0b8f73e1 JW |
5306 | if (memcg_wb_domain_init(memcg, GFP_KERNEL)) |
5307 | goto fail; | |
28dbc4b6 | 5308 | |
f7e1cb6e | 5309 | INIT_WORK(&memcg->high_work, high_work_func); |
d142e3e6 | 5310 | INIT_LIST_HEAD(&memcg->oom_notify); |
d142e3e6 GC |
5311 | mutex_init(&memcg->thresholds_lock); |
5312 | spin_lock_init(&memcg->move_lock); | |
70ddf637 | 5313 | vmpressure_init(&memcg->vmpressure); |
fba94807 TH |
5314 | INIT_LIST_HEAD(&memcg->event_list); |
5315 | spin_lock_init(&memcg->event_list_lock); | |
d886f4e4 | 5316 | memcg->socket_pressure = jiffies; |
84c07d11 | 5317 | #ifdef CONFIG_MEMCG_KMEM |
900a38f0 | 5318 | memcg->kmemcg_id = -1; |
bf4f0599 | 5319 | INIT_LIST_HEAD(&memcg->objcg_list); |
900a38f0 | 5320 | #endif |
52ebea74 TH |
5321 | #ifdef CONFIG_CGROUP_WRITEBACK |
5322 | INIT_LIST_HEAD(&memcg->cgwb_list); | |
97b27821 TH |
5323 | for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) |
5324 | memcg->cgwb_frn[i].done = | |
5325 | __WB_COMPLETION_INIT(&memcg_cgwb_frn_waitq); | |
87eaceb3 YS |
5326 | #endif |
5327 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE | |
5328 | spin_lock_init(&memcg->deferred_split_queue.split_queue_lock); | |
5329 | INIT_LIST_HEAD(&memcg->deferred_split_queue.split_queue); | |
5330 | memcg->deferred_split_queue.split_queue_len = 0; | |
52ebea74 | 5331 | #endif |
73f576c0 | 5332 | idr_replace(&mem_cgroup_idr, memcg, memcg->id.id); |
ec1c86b2 | 5333 | lru_gen_init_memcg(memcg); |
0b8f73e1 JW |
5334 | return memcg; |
5335 | fail: | |
7e97de0b | 5336 | mem_cgroup_id_remove(memcg); |
40e952f9 | 5337 | __mem_cgroup_free(memcg); |
11d67612 | 5338 | return ERR_PTR(error); |
d142e3e6 GC |
5339 | } |
5340 | ||
0b8f73e1 JW |
5341 | static struct cgroup_subsys_state * __ref |
5342 | mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) | |
d142e3e6 | 5343 | { |
0b8f73e1 | 5344 | struct mem_cgroup *parent = mem_cgroup_from_css(parent_css); |
b87d8cef | 5345 | struct mem_cgroup *memcg, *old_memcg; |
d142e3e6 | 5346 | |
b87d8cef | 5347 | old_memcg = set_active_memcg(parent); |
0b8f73e1 | 5348 | memcg = mem_cgroup_alloc(); |
b87d8cef | 5349 | set_active_memcg(old_memcg); |
11d67612 YS |
5350 | if (IS_ERR(memcg)) |
5351 | return ERR_CAST(memcg); | |
d142e3e6 | 5352 | |
d1663a90 | 5353 | page_counter_set_high(&memcg->memory, PAGE_COUNTER_MAX); |
2178e20c | 5354 | WRITE_ONCE(memcg->soft_limit, PAGE_COUNTER_MAX); |
f4840ccf JW |
5355 | #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) |
5356 | memcg->zswap_max = PAGE_COUNTER_MAX; | |
5357 | #endif | |
4b82ab4f | 5358 | page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX); |
0b8f73e1 | 5359 | if (parent) { |
82b3aa26 | 5360 | WRITE_ONCE(memcg->swappiness, mem_cgroup_swappiness(parent)); |
17c56de6 | 5361 | WRITE_ONCE(memcg->oom_kill_disable, READ_ONCE(parent->oom_kill_disable)); |
bef8620c | 5362 | |
3e32cb2e | 5363 | page_counter_init(&memcg->memory, &parent->memory); |
37e84351 | 5364 | page_counter_init(&memcg->swap, &parent->swap); |
3e32cb2e | 5365 | page_counter_init(&memcg->kmem, &parent->kmem); |
0db15298 | 5366 | page_counter_init(&memcg->tcpmem, &parent->tcpmem); |
18f59ea7 | 5367 | } else { |
8278f1c7 | 5368 | init_memcg_events(); |
bef8620c RG |
5369 | page_counter_init(&memcg->memory, NULL); |
5370 | page_counter_init(&memcg->swap, NULL); | |
5371 | page_counter_init(&memcg->kmem, NULL); | |
5372 | page_counter_init(&memcg->tcpmem, NULL); | |
d6441637 | 5373 | |
0b8f73e1 JW |
5374 | root_mem_cgroup = memcg; |
5375 | return &memcg->css; | |
5376 | } | |
5377 | ||
f7e1cb6e | 5378 | if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket) |
ef12947c | 5379 | static_branch_inc(&memcg_sockets_enabled_key); |
f7e1cb6e | 5380 | |
b6c1a8af YS |
5381 | #if defined(CONFIG_MEMCG_KMEM) |
5382 | if (!cgroup_memory_nobpf) | |
5383 | static_branch_inc(&memcg_bpf_enabled_key); | |
5384 | #endif | |
5385 | ||
0b8f73e1 | 5386 | return &memcg->css; |
0b8f73e1 JW |
5387 | } |
5388 | ||
73f576c0 | 5389 | static int mem_cgroup_css_online(struct cgroup_subsys_state *css) |
0b8f73e1 | 5390 | { |
58fa2a55 VD |
5391 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
5392 | ||
da0efe30 MS |
5393 | if (memcg_online_kmem(memcg)) |
5394 | goto remove_id; | |
5395 | ||
0a4465d3 | 5396 | /* |
e4262c4f | 5397 | * A memcg must be visible for expand_shrinker_info() |
0a4465d3 KT |
5398 | * by the time the maps are allocated. So, we allocate maps |
5399 | * here, when for_each_mem_cgroup() can't skip it. | |
5400 | */ | |
da0efe30 MS |
5401 | if (alloc_shrinker_info(memcg)) |
5402 | goto offline_kmem; | |
0a4465d3 | 5403 | |
73f576c0 | 5404 | /* Online state pins memcg ID, memcg ID pins CSS */ |
1c2d479a | 5405 | refcount_set(&memcg->id.ref, 1); |
73f576c0 | 5406 | css_get(css); |
aa48e47e SB |
5407 | |
5408 | if (unlikely(mem_cgroup_is_root(memcg))) | |
5409 | queue_delayed_work(system_unbound_wq, &stats_flush_dwork, | |
396faf88 | 5410 | FLUSH_TIME); |
e4dde56c | 5411 | lru_gen_online_memcg(memcg); |
2f7dd7a4 | 5412 | return 0; |
da0efe30 MS |
5413 | offline_kmem: |
5414 | memcg_offline_kmem(memcg); | |
5415 | remove_id: | |
5416 | mem_cgroup_id_remove(memcg); | |
5417 | return -ENOMEM; | |
8cdea7c0 BS |
5418 | } |
5419 | ||
eb95419b | 5420 | static void mem_cgroup_css_offline(struct cgroup_subsys_state *css) |
df878fb0 | 5421 | { |
eb95419b | 5422 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
3bc942f3 | 5423 | struct mem_cgroup_event *event, *tmp; |
79bd9814 TH |
5424 | |
5425 | /* | |
5426 | * Unregister events and notify userspace. | |
5427 | * Notify userspace about cgroup removing only after rmdir of cgroup | |
5428 | * directory to avoid race between userspace and kernelspace. | |
5429 | */ | |
4ba9515d | 5430 | spin_lock_irq(&memcg->event_list_lock); |
fba94807 | 5431 | list_for_each_entry_safe(event, tmp, &memcg->event_list, list) { |
79bd9814 TH |
5432 | list_del_init(&event->list); |
5433 | schedule_work(&event->remove); | |
5434 | } | |
4ba9515d | 5435 | spin_unlock_irq(&memcg->event_list_lock); |
ec64f515 | 5436 | |
bf8d5d52 | 5437 | page_counter_set_min(&memcg->memory, 0); |
23067153 | 5438 | page_counter_set_low(&memcg->memory, 0); |
63677c74 | 5439 | |
567e9ab2 | 5440 | memcg_offline_kmem(memcg); |
a178015c | 5441 | reparent_shrinker_deferred(memcg); |
52ebea74 | 5442 | wb_memcg_offline(memcg); |
e4dde56c | 5443 | lru_gen_offline_memcg(memcg); |
73f576c0 | 5444 | |
591edfb1 RG |
5445 | drain_all_stock(memcg); |
5446 | ||
73f576c0 | 5447 | mem_cgroup_id_put(memcg); |
df878fb0 KH |
5448 | } |
5449 | ||
6df38689 VD |
5450 | static void mem_cgroup_css_released(struct cgroup_subsys_state *css) |
5451 | { | |
5452 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); | |
5453 | ||
5454 | invalidate_reclaim_iterators(memcg); | |
e4dde56c | 5455 | lru_gen_release_memcg(memcg); |
6df38689 VD |
5456 | } |
5457 | ||
eb95419b | 5458 | static void mem_cgroup_css_free(struct cgroup_subsys_state *css) |
8cdea7c0 | 5459 | { |
eb95419b | 5460 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
97b27821 | 5461 | int __maybe_unused i; |
c268e994 | 5462 | |
97b27821 TH |
5463 | #ifdef CONFIG_CGROUP_WRITEBACK |
5464 | for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) | |
5465 | wb_wait_for_completion(&memcg->cgwb_frn[i].done); | |
5466 | #endif | |
f7e1cb6e | 5467 | if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket) |
ef12947c | 5468 | static_branch_dec(&memcg_sockets_enabled_key); |
127424c8 | 5469 | |
0db15298 | 5470 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_active) |
d55f90bf | 5471 | static_branch_dec(&memcg_sockets_enabled_key); |
3893e302 | 5472 | |
b6c1a8af YS |
5473 | #if defined(CONFIG_MEMCG_KMEM) |
5474 | if (!cgroup_memory_nobpf) | |
5475 | static_branch_dec(&memcg_bpf_enabled_key); | |
5476 | #endif | |
5477 | ||
0b8f73e1 JW |
5478 | vmpressure_cleanup(&memcg->vmpressure); |
5479 | cancel_work_sync(&memcg->high_work); | |
5480 | mem_cgroup_remove_from_trees(memcg); | |
e4262c4f | 5481 | free_shrinker_info(memcg); |
0b8f73e1 | 5482 | mem_cgroup_free(memcg); |
8cdea7c0 BS |
5483 | } |
5484 | ||
1ced953b TH |
5485 | /** |
5486 | * mem_cgroup_css_reset - reset the states of a mem_cgroup | |
5487 | * @css: the target css | |
5488 | * | |
5489 | * Reset the states of the mem_cgroup associated with @css. This is | |
5490 | * invoked when the userland requests disabling on the default hierarchy | |
5491 | * but the memcg is pinned through dependency. The memcg should stop | |
5492 | * applying policies and should revert to the vanilla state as it may be | |
5493 | * made visible again. | |
5494 | * | |
5495 | * The current implementation only resets the essential configurations. | |
5496 | * This needs to be expanded to cover all the visible parts. | |
5497 | */ | |
5498 | static void mem_cgroup_css_reset(struct cgroup_subsys_state *css) | |
5499 | { | |
5500 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); | |
5501 | ||
bbec2e15 RG |
5502 | page_counter_set_max(&memcg->memory, PAGE_COUNTER_MAX); |
5503 | page_counter_set_max(&memcg->swap, PAGE_COUNTER_MAX); | |
bbec2e15 RG |
5504 | page_counter_set_max(&memcg->kmem, PAGE_COUNTER_MAX); |
5505 | page_counter_set_max(&memcg->tcpmem, PAGE_COUNTER_MAX); | |
bf8d5d52 | 5506 | page_counter_set_min(&memcg->memory, 0); |
23067153 | 5507 | page_counter_set_low(&memcg->memory, 0); |
d1663a90 | 5508 | page_counter_set_high(&memcg->memory, PAGE_COUNTER_MAX); |
2178e20c | 5509 | WRITE_ONCE(memcg->soft_limit, PAGE_COUNTER_MAX); |
4b82ab4f | 5510 | page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX); |
2529bb3a | 5511 | memcg_wb_domain_size_changed(memcg); |
1ced953b TH |
5512 | } |
5513 | ||
2d146aa3 JW |
5514 | static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) |
5515 | { | |
5516 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); | |
5517 | struct mem_cgroup *parent = parent_mem_cgroup(memcg); | |
5518 | struct memcg_vmstats_percpu *statc; | |
5519 | long delta, v; | |
7e1c0d6f | 5520 | int i, nid; |
2d146aa3 JW |
5521 | |
5522 | statc = per_cpu_ptr(memcg->vmstats_percpu, cpu); | |
5523 | ||
5524 | for (i = 0; i < MEMCG_NR_STAT; i++) { | |
5525 | /* | |
5526 | * Collect the aggregated propagation counts of groups | |
5527 | * below us. We're in a per-cpu loop here and this is | |
5528 | * a global counter, so the first cycle will get them. | |
5529 | */ | |
410f8e82 | 5530 | delta = memcg->vmstats->state_pending[i]; |
2d146aa3 | 5531 | if (delta) |
410f8e82 | 5532 | memcg->vmstats->state_pending[i] = 0; |
2d146aa3 JW |
5533 | |
5534 | /* Add CPU changes on this level since the last flush */ | |
5535 | v = READ_ONCE(statc->state[i]); | |
5536 | if (v != statc->state_prev[i]) { | |
5537 | delta += v - statc->state_prev[i]; | |
5538 | statc->state_prev[i] = v; | |
5539 | } | |
5540 | ||
5541 | if (!delta) | |
5542 | continue; | |
5543 | ||
5544 | /* Aggregate counts on this level and propagate upwards */ | |
410f8e82 | 5545 | memcg->vmstats->state[i] += delta; |
2d146aa3 | 5546 | if (parent) |
410f8e82 | 5547 | parent->vmstats->state_pending[i] += delta; |
2d146aa3 JW |
5548 | } |
5549 | ||
8278f1c7 | 5550 | for (i = 0; i < NR_MEMCG_EVENTS; i++) { |
410f8e82 | 5551 | delta = memcg->vmstats->events_pending[i]; |
2d146aa3 | 5552 | if (delta) |
410f8e82 | 5553 | memcg->vmstats->events_pending[i] = 0; |
2d146aa3 JW |
5554 | |
5555 | v = READ_ONCE(statc->events[i]); | |
5556 | if (v != statc->events_prev[i]) { | |
5557 | delta += v - statc->events_prev[i]; | |
5558 | statc->events_prev[i] = v; | |
5559 | } | |
5560 | ||
5561 | if (!delta) | |
5562 | continue; | |
5563 | ||
410f8e82 | 5564 | memcg->vmstats->events[i] += delta; |
2d146aa3 | 5565 | if (parent) |
410f8e82 | 5566 | parent->vmstats->events_pending[i] += delta; |
2d146aa3 | 5567 | } |
7e1c0d6f SB |
5568 | |
5569 | for_each_node_state(nid, N_MEMORY) { | |
5570 | struct mem_cgroup_per_node *pn = memcg->nodeinfo[nid]; | |
5571 | struct mem_cgroup_per_node *ppn = NULL; | |
5572 | struct lruvec_stats_percpu *lstatc; | |
5573 | ||
5574 | if (parent) | |
5575 | ppn = parent->nodeinfo[nid]; | |
5576 | ||
5577 | lstatc = per_cpu_ptr(pn->lruvec_stats_percpu, cpu); | |
5578 | ||
5579 | for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) { | |
5580 | delta = pn->lruvec_stats.state_pending[i]; | |
5581 | if (delta) | |
5582 | pn->lruvec_stats.state_pending[i] = 0; | |
5583 | ||
5584 | v = READ_ONCE(lstatc->state[i]); | |
5585 | if (v != lstatc->state_prev[i]) { | |
5586 | delta += v - lstatc->state_prev[i]; | |
5587 | lstatc->state_prev[i] = v; | |
5588 | } | |
5589 | ||
5590 | if (!delta) | |
5591 | continue; | |
5592 | ||
5593 | pn->lruvec_stats.state[i] += delta; | |
5594 | if (ppn) | |
5595 | ppn->lruvec_stats.state_pending[i] += delta; | |
5596 | } | |
5597 | } | |
2d146aa3 JW |
5598 | } |
5599 | ||
02491447 | 5600 | #ifdef CONFIG_MMU |
7dc74be0 | 5601 | /* Handlers for move charge at task migration. */ |
854ffa8d | 5602 | static int mem_cgroup_do_precharge(unsigned long count) |
7dc74be0 | 5603 | { |
05b84301 | 5604 | int ret; |
9476db97 | 5605 | |
d0164adc MG |
5606 | /* Try a single bulk charge without reclaim first, kswapd may wake */ |
5607 | ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_DIRECT_RECLAIM, count); | |
9476db97 | 5608 | if (!ret) { |
854ffa8d | 5609 | mc.precharge += count; |
854ffa8d DN |
5610 | return ret; |
5611 | } | |
9476db97 | 5612 | |
3674534b | 5613 | /* Try charges one by one with reclaim, but do not retry */ |
854ffa8d | 5614 | while (count--) { |
3674534b | 5615 | ret = try_charge(mc.to, GFP_KERNEL | __GFP_NORETRY, 1); |
38c5d72f | 5616 | if (ret) |
38c5d72f | 5617 | return ret; |
854ffa8d | 5618 | mc.precharge++; |
9476db97 | 5619 | cond_resched(); |
854ffa8d | 5620 | } |
9476db97 | 5621 | return 0; |
4ffef5fe DN |
5622 | } |
5623 | ||
4ffef5fe DN |
5624 | union mc_target { |
5625 | struct page *page; | |
02491447 | 5626 | swp_entry_t ent; |
4ffef5fe DN |
5627 | }; |
5628 | ||
4ffef5fe | 5629 | enum mc_target_type { |
8d32ff84 | 5630 | MC_TARGET_NONE = 0, |
4ffef5fe | 5631 | MC_TARGET_PAGE, |
02491447 | 5632 | MC_TARGET_SWAP, |
c733a828 | 5633 | MC_TARGET_DEVICE, |
4ffef5fe DN |
5634 | }; |
5635 | ||
90254a65 DN |
5636 | static struct page *mc_handle_present_pte(struct vm_area_struct *vma, |
5637 | unsigned long addr, pte_t ptent) | |
4ffef5fe | 5638 | { |
25b2995a | 5639 | struct page *page = vm_normal_page(vma, addr, ptent); |
4ffef5fe | 5640 | |
90254a65 DN |
5641 | if (!page || !page_mapped(page)) |
5642 | return NULL; | |
5643 | if (PageAnon(page)) { | |
1dfab5ab | 5644 | if (!(mc.flags & MOVE_ANON)) |
90254a65 | 5645 | return NULL; |
1dfab5ab JW |
5646 | } else { |
5647 | if (!(mc.flags & MOVE_FILE)) | |
5648 | return NULL; | |
5649 | } | |
90254a65 DN |
5650 | if (!get_page_unless_zero(page)) |
5651 | return NULL; | |
5652 | ||
5653 | return page; | |
5654 | } | |
5655 | ||
c733a828 | 5656 | #if defined(CONFIG_SWAP) || defined(CONFIG_DEVICE_PRIVATE) |
90254a65 | 5657 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, |
48406ef8 | 5658 | pte_t ptent, swp_entry_t *entry) |
90254a65 | 5659 | { |
90254a65 DN |
5660 | struct page *page = NULL; |
5661 | swp_entry_t ent = pte_to_swp_entry(ptent); | |
5662 | ||
9a137153 | 5663 | if (!(mc.flags & MOVE_ANON)) |
90254a65 | 5664 | return NULL; |
c733a828 JG |
5665 | |
5666 | /* | |
27674ef6 CH |
5667 | * Handle device private pages that are not accessible by the CPU, but |
5668 | * stored as special swap entries in the page table. | |
c733a828 JG |
5669 | */ |
5670 | if (is_device_private_entry(ent)) { | |
af5cdaf8 | 5671 | page = pfn_swap_entry_to_page(ent); |
27674ef6 | 5672 | if (!get_page_unless_zero(page)) |
c733a828 JG |
5673 | return NULL; |
5674 | return page; | |
5675 | } | |
5676 | ||
9a137153 RC |
5677 | if (non_swap_entry(ent)) |
5678 | return NULL; | |
5679 | ||
4b91355e | 5680 | /* |
cb691e2f | 5681 | * Because swap_cache_get_folio() updates some statistics counter, |
4b91355e KH |
5682 | * we call find_get_page() with swapper_space directly. |
5683 | */ | |
f6ab1f7f | 5684 | page = find_get_page(swap_address_space(ent), swp_offset(ent)); |
2d1c4980 | 5685 | entry->val = ent.val; |
90254a65 DN |
5686 | |
5687 | return page; | |
5688 | } | |
4b91355e KH |
5689 | #else |
5690 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, | |
48406ef8 | 5691 | pte_t ptent, swp_entry_t *entry) |
4b91355e KH |
5692 | { |
5693 | return NULL; | |
5694 | } | |
5695 | #endif | |
90254a65 | 5696 | |
87946a72 | 5697 | static struct page *mc_handle_file_pte(struct vm_area_struct *vma, |
48384b0b | 5698 | unsigned long addr, pte_t ptent) |
87946a72 | 5699 | { |
524984ff MWO |
5700 | unsigned long index; |
5701 | struct folio *folio; | |
5702 | ||
87946a72 DN |
5703 | if (!vma->vm_file) /* anonymous vma */ |
5704 | return NULL; | |
1dfab5ab | 5705 | if (!(mc.flags & MOVE_FILE)) |
87946a72 DN |
5706 | return NULL; |
5707 | ||
524984ff | 5708 | /* folio is moved even if it's not RSS of this task(page-faulted). */ |
aa3b1895 | 5709 | /* shmem/tmpfs may report page out on swap: account for that too. */ |
524984ff MWO |
5710 | index = linear_page_index(vma, addr); |
5711 | folio = filemap_get_incore_folio(vma->vm_file->f_mapping, index); | |
66dabbb6 | 5712 | if (IS_ERR(folio)) |
524984ff MWO |
5713 | return NULL; |
5714 | return folio_file_page(folio, index); | |
87946a72 DN |
5715 | } |
5716 | ||
b1b0deab CG |
5717 | /** |
5718 | * mem_cgroup_move_account - move account of the page | |
5719 | * @page: the page | |
25843c2b | 5720 | * @compound: charge the page as compound or small page |
b1b0deab CG |
5721 | * @from: mem_cgroup which the page is moved from. |
5722 | * @to: mem_cgroup which the page is moved to. @from != @to. | |
5723 | * | |
4e0cf05f | 5724 | * The page must be locked and not on the LRU. |
b1b0deab CG |
5725 | * |
5726 | * This function doesn't do "charge" to new cgroup and doesn't do "uncharge" | |
5727 | * from old cgroup. | |
5728 | */ | |
5729 | static int mem_cgroup_move_account(struct page *page, | |
f627c2f5 | 5730 | bool compound, |
b1b0deab CG |
5731 | struct mem_cgroup *from, |
5732 | struct mem_cgroup *to) | |
5733 | { | |
fcce4672 | 5734 | struct folio *folio = page_folio(page); |
ae8af438 KK |
5735 | struct lruvec *from_vec, *to_vec; |
5736 | struct pglist_data *pgdat; | |
fcce4672 | 5737 | unsigned int nr_pages = compound ? folio_nr_pages(folio) : 1; |
8e88bd2d | 5738 | int nid, ret; |
b1b0deab CG |
5739 | |
5740 | VM_BUG_ON(from == to); | |
4e0cf05f | 5741 | VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); |
fcce4672 | 5742 | VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); |
9c325215 | 5743 | VM_BUG_ON(compound && !folio_test_large(folio)); |
b1b0deab | 5744 | |
b1b0deab | 5745 | ret = -EINVAL; |
fcce4672 | 5746 | if (folio_memcg(folio) != from) |
4e0cf05f | 5747 | goto out; |
b1b0deab | 5748 | |
fcce4672 | 5749 | pgdat = folio_pgdat(folio); |
867e5e1d JW |
5750 | from_vec = mem_cgroup_lruvec(from, pgdat); |
5751 | to_vec = mem_cgroup_lruvec(to, pgdat); | |
ae8af438 | 5752 | |
fcce4672 | 5753 | folio_memcg_lock(folio); |
b1b0deab | 5754 | |
fcce4672 MWO |
5755 | if (folio_test_anon(folio)) { |
5756 | if (folio_mapped(folio)) { | |
be5d0a74 JW |
5757 | __mod_lruvec_state(from_vec, NR_ANON_MAPPED, -nr_pages); |
5758 | __mod_lruvec_state(to_vec, NR_ANON_MAPPED, nr_pages); | |
fcce4672 | 5759 | if (folio_test_transhuge(folio)) { |
69473e5d MS |
5760 | __mod_lruvec_state(from_vec, NR_ANON_THPS, |
5761 | -nr_pages); | |
5762 | __mod_lruvec_state(to_vec, NR_ANON_THPS, | |
5763 | nr_pages); | |
468c3982 | 5764 | } |
be5d0a74 JW |
5765 | } |
5766 | } else { | |
0d1c2072 JW |
5767 | __mod_lruvec_state(from_vec, NR_FILE_PAGES, -nr_pages); |
5768 | __mod_lruvec_state(to_vec, NR_FILE_PAGES, nr_pages); | |
5769 | ||
fcce4672 | 5770 | if (folio_test_swapbacked(folio)) { |
0d1c2072 JW |
5771 | __mod_lruvec_state(from_vec, NR_SHMEM, -nr_pages); |
5772 | __mod_lruvec_state(to_vec, NR_SHMEM, nr_pages); | |
5773 | } | |
5774 | ||
fcce4672 | 5775 | if (folio_mapped(folio)) { |
49e50d27 JW |
5776 | __mod_lruvec_state(from_vec, NR_FILE_MAPPED, -nr_pages); |
5777 | __mod_lruvec_state(to_vec, NR_FILE_MAPPED, nr_pages); | |
5778 | } | |
b1b0deab | 5779 | |
fcce4672 MWO |
5780 | if (folio_test_dirty(folio)) { |
5781 | struct address_space *mapping = folio_mapping(folio); | |
c4843a75 | 5782 | |
f56753ac | 5783 | if (mapping_can_writeback(mapping)) { |
49e50d27 JW |
5784 | __mod_lruvec_state(from_vec, NR_FILE_DIRTY, |
5785 | -nr_pages); | |
5786 | __mod_lruvec_state(to_vec, NR_FILE_DIRTY, | |
5787 | nr_pages); | |
5788 | } | |
c4843a75 GT |
5789 | } |
5790 | } | |
5791 | ||
c449deb2 HD |
5792 | #ifdef CONFIG_SWAP |
5793 | if (folio_test_swapcache(folio)) { | |
5794 | __mod_lruvec_state(from_vec, NR_SWAPCACHE, -nr_pages); | |
5795 | __mod_lruvec_state(to_vec, NR_SWAPCACHE, nr_pages); | |
5796 | } | |
5797 | #endif | |
fcce4672 | 5798 | if (folio_test_writeback(folio)) { |
ae8af438 KK |
5799 | __mod_lruvec_state(from_vec, NR_WRITEBACK, -nr_pages); |
5800 | __mod_lruvec_state(to_vec, NR_WRITEBACK, nr_pages); | |
b1b0deab CG |
5801 | } |
5802 | ||
5803 | /* | |
abb242f5 JW |
5804 | * All state has been migrated, let's switch to the new memcg. |
5805 | * | |
bcfe06bf | 5806 | * It is safe to change page's memcg here because the page |
abb242f5 JW |
5807 | * is referenced, charged, isolated, and locked: we can't race |
5808 | * with (un)charging, migration, LRU putback, or anything else | |
bcfe06bf | 5809 | * that would rely on a stable page's memory cgroup. |
abb242f5 | 5810 | * |
6c77b607 | 5811 | * Note that folio_memcg_lock is a memcg lock, not a page lock, |
bcfe06bf | 5812 | * to save space. As soon as we switch page's memory cgroup to a |
abb242f5 JW |
5813 | * new memcg that isn't locked, the above state can change |
5814 | * concurrently again. Make sure we're truly done with it. | |
b1b0deab | 5815 | */ |
abb242f5 | 5816 | smp_mb(); |
b1b0deab | 5817 | |
1a3e1f40 JW |
5818 | css_get(&to->css); |
5819 | css_put(&from->css); | |
5820 | ||
fcce4672 | 5821 | folio->memcg_data = (unsigned long)to; |
87eaceb3 | 5822 | |
f70ad448 | 5823 | __folio_memcg_unlock(from); |
b1b0deab CG |
5824 | |
5825 | ret = 0; | |
fcce4672 | 5826 | nid = folio_nid(folio); |
b1b0deab CG |
5827 | |
5828 | local_irq_disable(); | |
6e0110c2 | 5829 | mem_cgroup_charge_statistics(to, nr_pages); |
8e88bd2d | 5830 | memcg_check_events(to, nid); |
6e0110c2 | 5831 | mem_cgroup_charge_statistics(from, -nr_pages); |
8e88bd2d | 5832 | memcg_check_events(from, nid); |
b1b0deab | 5833 | local_irq_enable(); |
b1b0deab CG |
5834 | out: |
5835 | return ret; | |
5836 | } | |
5837 | ||
7cf7806c LR |
5838 | /** |
5839 | * get_mctgt_type - get target type of moving charge | |
5840 | * @vma: the vma the pte to be checked belongs | |
5841 | * @addr: the address corresponding to the pte to be checked | |
5842 | * @ptent: the pte to be checked | |
5843 | * @target: the pointer the target page or swap ent will be stored(can be NULL) | |
5844 | * | |
5845 | * Returns | |
5846 | * 0(MC_TARGET_NONE): if the pte is not a target for move charge. | |
5847 | * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for | |
5848 | * move charge. if @target is not NULL, the page is stored in target->page | |
5849 | * with extra refcnt got(Callers should handle it). | |
5850 | * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a | |
5851 | * target for charge migration. if @target is not NULL, the entry is stored | |
5852 | * in target->ent. | |
f25cbb7a AS |
5853 | * 3(MC_TARGET_DEVICE): like MC_TARGET_PAGE but page is device memory and |
5854 | * thus not on the lru. | |
df6ad698 JG |
5855 | * For now we such page is charge like a regular page would be as for all |
5856 | * intent and purposes it is just special memory taking the place of a | |
5857 | * regular page. | |
c733a828 JG |
5858 | * |
5859 | * See Documentations/vm/hmm.txt and include/linux/hmm.h | |
7cf7806c LR |
5860 | * |
5861 | * Called with pte lock held. | |
5862 | */ | |
5863 | ||
8d32ff84 | 5864 | static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma, |
90254a65 DN |
5865 | unsigned long addr, pte_t ptent, union mc_target *target) |
5866 | { | |
5867 | struct page *page = NULL; | |
8d32ff84 | 5868 | enum mc_target_type ret = MC_TARGET_NONE; |
90254a65 DN |
5869 | swp_entry_t ent = { .val = 0 }; |
5870 | ||
5871 | if (pte_present(ptent)) | |
5872 | page = mc_handle_present_pte(vma, addr, ptent); | |
5c041f5d PX |
5873 | else if (pte_none_mostly(ptent)) |
5874 | /* | |
5875 | * PTE markers should be treated as a none pte here, separated | |
5876 | * from other swap handling below. | |
5877 | */ | |
5878 | page = mc_handle_file_pte(vma, addr, ptent); | |
90254a65 | 5879 | else if (is_swap_pte(ptent)) |
48406ef8 | 5880 | page = mc_handle_swap_pte(vma, ptent, &ent); |
90254a65 | 5881 | |
4e0cf05f JW |
5882 | if (target && page) { |
5883 | if (!trylock_page(page)) { | |
5884 | put_page(page); | |
5885 | return ret; | |
5886 | } | |
5887 | /* | |
5888 | * page_mapped() must be stable during the move. This | |
5889 | * pte is locked, so if it's present, the page cannot | |
5890 | * become unmapped. If it isn't, we have only partial | |
5891 | * control over the mapped state: the page lock will | |
5892 | * prevent new faults against pagecache and swapcache, | |
5893 | * so an unmapped page cannot become mapped. However, | |
5894 | * if the page is already mapped elsewhere, it can | |
5895 | * unmap, and there is nothing we can do about it. | |
5896 | * Alas, skip moving the page in this case. | |
5897 | */ | |
5898 | if (!pte_present(ptent) && page_mapped(page)) { | |
5899 | unlock_page(page); | |
5900 | put_page(page); | |
5901 | return ret; | |
5902 | } | |
5903 | } | |
5904 | ||
90254a65 | 5905 | if (!page && !ent.val) |
8d32ff84 | 5906 | return ret; |
02491447 | 5907 | if (page) { |
02491447 | 5908 | /* |
0a31bc97 | 5909 | * Do only loose check w/o serialization. |
1306a85a | 5910 | * mem_cgroup_move_account() checks the page is valid or |
0a31bc97 | 5911 | * not under LRU exclusion. |
02491447 | 5912 | */ |
bcfe06bf | 5913 | if (page_memcg(page) == mc.from) { |
02491447 | 5914 | ret = MC_TARGET_PAGE; |
f25cbb7a AS |
5915 | if (is_device_private_page(page) || |
5916 | is_device_coherent_page(page)) | |
c733a828 | 5917 | ret = MC_TARGET_DEVICE; |
02491447 DN |
5918 | if (target) |
5919 | target->page = page; | |
5920 | } | |
4e0cf05f JW |
5921 | if (!ret || !target) { |
5922 | if (target) | |
5923 | unlock_page(page); | |
02491447 | 5924 | put_page(page); |
4e0cf05f | 5925 | } |
02491447 | 5926 | } |
3e14a57b HY |
5927 | /* |
5928 | * There is a swap entry and a page doesn't exist or isn't charged. | |
5929 | * But we cannot move a tail-page in a THP. | |
5930 | */ | |
5931 | if (ent.val && !ret && (!page || !PageTransCompound(page)) && | |
34c00c31 | 5932 | mem_cgroup_id(mc.from) == lookup_swap_cgroup_id(ent)) { |
7f0f1546 KH |
5933 | ret = MC_TARGET_SWAP; |
5934 | if (target) | |
5935 | target->ent = ent; | |
4ffef5fe | 5936 | } |
4ffef5fe DN |
5937 | return ret; |
5938 | } | |
5939 | ||
12724850 NH |
5940 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
5941 | /* | |
d6810d73 HY |
5942 | * We don't consider PMD mapped swapping or file mapped pages because THP does |
5943 | * not support them for now. | |
12724850 NH |
5944 | * Caller should make sure that pmd_trans_huge(pmd) is true. |
5945 | */ | |
5946 | static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma, | |
5947 | unsigned long addr, pmd_t pmd, union mc_target *target) | |
5948 | { | |
5949 | struct page *page = NULL; | |
12724850 NH |
5950 | enum mc_target_type ret = MC_TARGET_NONE; |
5951 | ||
84c3fc4e ZY |
5952 | if (unlikely(is_swap_pmd(pmd))) { |
5953 | VM_BUG_ON(thp_migration_supported() && | |
5954 | !is_pmd_migration_entry(pmd)); | |
5955 | return ret; | |
5956 | } | |
12724850 | 5957 | page = pmd_page(pmd); |
309381fe | 5958 | VM_BUG_ON_PAGE(!page || !PageHead(page), page); |
1dfab5ab | 5959 | if (!(mc.flags & MOVE_ANON)) |
12724850 | 5960 | return ret; |
bcfe06bf | 5961 | if (page_memcg(page) == mc.from) { |
12724850 NH |
5962 | ret = MC_TARGET_PAGE; |
5963 | if (target) { | |
5964 | get_page(page); | |
4e0cf05f JW |
5965 | if (!trylock_page(page)) { |
5966 | put_page(page); | |
5967 | return MC_TARGET_NONE; | |
5968 | } | |
12724850 NH |
5969 | target->page = page; |
5970 | } | |
5971 | } | |
5972 | return ret; | |
5973 | } | |
5974 | #else | |
5975 | static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma, | |
5976 | unsigned long addr, pmd_t pmd, union mc_target *target) | |
5977 | { | |
5978 | return MC_TARGET_NONE; | |
5979 | } | |
5980 | #endif | |
5981 | ||
4ffef5fe DN |
5982 | static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd, |
5983 | unsigned long addr, unsigned long end, | |
5984 | struct mm_walk *walk) | |
5985 | { | |
26bcd64a | 5986 | struct vm_area_struct *vma = walk->vma; |
4ffef5fe DN |
5987 | pte_t *pte; |
5988 | spinlock_t *ptl; | |
5989 | ||
b6ec57f4 KS |
5990 | ptl = pmd_trans_huge_lock(pmd, vma); |
5991 | if (ptl) { | |
c733a828 JG |
5992 | /* |
5993 | * Note their can not be MC_TARGET_DEVICE for now as we do not | |
25b2995a CH |
5994 | * support transparent huge page with MEMORY_DEVICE_PRIVATE but |
5995 | * this might change. | |
c733a828 | 5996 | */ |
12724850 NH |
5997 | if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE) |
5998 | mc.precharge += HPAGE_PMD_NR; | |
bf929152 | 5999 | spin_unlock(ptl); |
1a5a9906 | 6000 | return 0; |
12724850 | 6001 | } |
03319327 | 6002 | |
4ffef5fe | 6003 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
04dee9e8 HD |
6004 | if (!pte) |
6005 | return 0; | |
4ffef5fe | 6006 | for (; addr != end; pte++, addr += PAGE_SIZE) |
c33c7948 | 6007 | if (get_mctgt_type(vma, addr, ptep_get(pte), NULL)) |
4ffef5fe DN |
6008 | mc.precharge++; /* increment precharge temporarily */ |
6009 | pte_unmap_unlock(pte - 1, ptl); | |
6010 | cond_resched(); | |
6011 | ||
7dc74be0 DN |
6012 | return 0; |
6013 | } | |
6014 | ||
7b86ac33 CH |
6015 | static const struct mm_walk_ops precharge_walk_ops = { |
6016 | .pmd_entry = mem_cgroup_count_precharge_pte_range, | |
6017 | }; | |
6018 | ||
4ffef5fe DN |
6019 | static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm) |
6020 | { | |
6021 | unsigned long precharge; | |
4ffef5fe | 6022 | |
d8ed45c5 | 6023 | mmap_read_lock(mm); |
ba0aff8e | 6024 | walk_page_range(mm, 0, ULONG_MAX, &precharge_walk_ops, NULL); |
d8ed45c5 | 6025 | mmap_read_unlock(mm); |
4ffef5fe DN |
6026 | |
6027 | precharge = mc.precharge; | |
6028 | mc.precharge = 0; | |
6029 | ||
6030 | return precharge; | |
6031 | } | |
6032 | ||
4ffef5fe DN |
6033 | static int mem_cgroup_precharge_mc(struct mm_struct *mm) |
6034 | { | |
dfe076b0 DN |
6035 | unsigned long precharge = mem_cgroup_count_precharge(mm); |
6036 | ||
6037 | VM_BUG_ON(mc.moving_task); | |
6038 | mc.moving_task = current; | |
6039 | return mem_cgroup_do_precharge(precharge); | |
4ffef5fe DN |
6040 | } |
6041 | ||
dfe076b0 DN |
6042 | /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */ |
6043 | static void __mem_cgroup_clear_mc(void) | |
4ffef5fe | 6044 | { |
2bd9bb20 KH |
6045 | struct mem_cgroup *from = mc.from; |
6046 | struct mem_cgroup *to = mc.to; | |
6047 | ||
4ffef5fe | 6048 | /* we must uncharge all the leftover precharges from mc.to */ |
854ffa8d | 6049 | if (mc.precharge) { |
00501b53 | 6050 | cancel_charge(mc.to, mc.precharge); |
854ffa8d DN |
6051 | mc.precharge = 0; |
6052 | } | |
6053 | /* | |
6054 | * we didn't uncharge from mc.from at mem_cgroup_move_account(), so | |
6055 | * we must uncharge here. | |
6056 | */ | |
6057 | if (mc.moved_charge) { | |
00501b53 | 6058 | cancel_charge(mc.from, mc.moved_charge); |
854ffa8d | 6059 | mc.moved_charge = 0; |
4ffef5fe | 6060 | } |
483c30b5 DN |
6061 | /* we must fixup refcnts and charges */ |
6062 | if (mc.moved_swap) { | |
483c30b5 | 6063 | /* uncharge swap account from the old cgroup */ |
ce00a967 | 6064 | if (!mem_cgroup_is_root(mc.from)) |
3e32cb2e | 6065 | page_counter_uncharge(&mc.from->memsw, mc.moved_swap); |
483c30b5 | 6066 | |
615d66c3 VD |
6067 | mem_cgroup_id_put_many(mc.from, mc.moved_swap); |
6068 | ||
05b84301 | 6069 | /* |
3e32cb2e JW |
6070 | * we charged both to->memory and to->memsw, so we |
6071 | * should uncharge to->memory. | |
05b84301 | 6072 | */ |
ce00a967 | 6073 | if (!mem_cgroup_is_root(mc.to)) |
3e32cb2e JW |
6074 | page_counter_uncharge(&mc.to->memory, mc.moved_swap); |
6075 | ||
483c30b5 DN |
6076 | mc.moved_swap = 0; |
6077 | } | |
dfe076b0 DN |
6078 | memcg_oom_recover(from); |
6079 | memcg_oom_recover(to); | |
6080 | wake_up_all(&mc.waitq); | |
6081 | } | |
6082 | ||
6083 | static void mem_cgroup_clear_mc(void) | |
6084 | { | |
264a0ae1 TH |
6085 | struct mm_struct *mm = mc.mm; |
6086 | ||
dfe076b0 DN |
6087 | /* |
6088 | * we must clear moving_task before waking up waiters at the end of | |
6089 | * task migration. | |
6090 | */ | |
6091 | mc.moving_task = NULL; | |
6092 | __mem_cgroup_clear_mc(); | |
2bd9bb20 | 6093 | spin_lock(&mc.lock); |
4ffef5fe DN |
6094 | mc.from = NULL; |
6095 | mc.to = NULL; | |
264a0ae1 | 6096 | mc.mm = NULL; |
2bd9bb20 | 6097 | spin_unlock(&mc.lock); |
264a0ae1 TH |
6098 | |
6099 | mmput(mm); | |
4ffef5fe DN |
6100 | } |
6101 | ||
1f7dd3e5 | 6102 | static int mem_cgroup_can_attach(struct cgroup_taskset *tset) |
7dc74be0 | 6103 | { |
1f7dd3e5 | 6104 | struct cgroup_subsys_state *css; |
eed67d75 | 6105 | struct mem_cgroup *memcg = NULL; /* unneeded init to make gcc happy */ |
9f2115f9 | 6106 | struct mem_cgroup *from; |
4530eddb | 6107 | struct task_struct *leader, *p; |
9f2115f9 | 6108 | struct mm_struct *mm; |
1dfab5ab | 6109 | unsigned long move_flags; |
9f2115f9 | 6110 | int ret = 0; |
7dc74be0 | 6111 | |
1f7dd3e5 TH |
6112 | /* charge immigration isn't supported on the default hierarchy */ |
6113 | if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) | |
9f2115f9 TH |
6114 | return 0; |
6115 | ||
4530eddb TH |
6116 | /* |
6117 | * Multi-process migrations only happen on the default hierarchy | |
6118 | * where charge immigration is not used. Perform charge | |
6119 | * immigration if @tset contains a leader and whine if there are | |
6120 | * multiple. | |
6121 | */ | |
6122 | p = NULL; | |
1f7dd3e5 | 6123 | cgroup_taskset_for_each_leader(leader, css, tset) { |
4530eddb TH |
6124 | WARN_ON_ONCE(p); |
6125 | p = leader; | |
1f7dd3e5 | 6126 | memcg = mem_cgroup_from_css(css); |
4530eddb TH |
6127 | } |
6128 | if (!p) | |
6129 | return 0; | |
6130 | ||
1f7dd3e5 | 6131 | /* |
f0953a1b | 6132 | * We are now committed to this value whatever it is. Changes in this |
1f7dd3e5 TH |
6133 | * tunable will only affect upcoming migrations, not the current one. |
6134 | * So we need to save it, and keep it going. | |
6135 | */ | |
6136 | move_flags = READ_ONCE(memcg->move_charge_at_immigrate); | |
6137 | if (!move_flags) | |
6138 | return 0; | |
6139 | ||
9f2115f9 TH |
6140 | from = mem_cgroup_from_task(p); |
6141 | ||
6142 | VM_BUG_ON(from == memcg); | |
6143 | ||
6144 | mm = get_task_mm(p); | |
6145 | if (!mm) | |
6146 | return 0; | |
6147 | /* We move charges only when we move a owner of the mm */ | |
6148 | if (mm->owner == p) { | |
6149 | VM_BUG_ON(mc.from); | |
6150 | VM_BUG_ON(mc.to); | |
6151 | VM_BUG_ON(mc.precharge); | |
6152 | VM_BUG_ON(mc.moved_charge); | |
6153 | VM_BUG_ON(mc.moved_swap); | |
6154 | ||
6155 | spin_lock(&mc.lock); | |
264a0ae1 | 6156 | mc.mm = mm; |
9f2115f9 TH |
6157 | mc.from = from; |
6158 | mc.to = memcg; | |
6159 | mc.flags = move_flags; | |
6160 | spin_unlock(&mc.lock); | |
6161 | /* We set mc.moving_task later */ | |
6162 | ||
6163 | ret = mem_cgroup_precharge_mc(mm); | |
6164 | if (ret) | |
6165 | mem_cgroup_clear_mc(); | |
264a0ae1 TH |
6166 | } else { |
6167 | mmput(mm); | |
7dc74be0 DN |
6168 | } |
6169 | return ret; | |
6170 | } | |
6171 | ||
1f7dd3e5 | 6172 | static void mem_cgroup_cancel_attach(struct cgroup_taskset *tset) |
7dc74be0 | 6173 | { |
4e2f245d JW |
6174 | if (mc.to) |
6175 | mem_cgroup_clear_mc(); | |
7dc74be0 DN |
6176 | } |
6177 | ||
4ffef5fe DN |
6178 | static int mem_cgroup_move_charge_pte_range(pmd_t *pmd, |
6179 | unsigned long addr, unsigned long end, | |
6180 | struct mm_walk *walk) | |
7dc74be0 | 6181 | { |
4ffef5fe | 6182 | int ret = 0; |
26bcd64a | 6183 | struct vm_area_struct *vma = walk->vma; |
4ffef5fe DN |
6184 | pte_t *pte; |
6185 | spinlock_t *ptl; | |
12724850 NH |
6186 | enum mc_target_type target_type; |
6187 | union mc_target target; | |
6188 | struct page *page; | |
4ffef5fe | 6189 | |
b6ec57f4 KS |
6190 | ptl = pmd_trans_huge_lock(pmd, vma); |
6191 | if (ptl) { | |
62ade86a | 6192 | if (mc.precharge < HPAGE_PMD_NR) { |
bf929152 | 6193 | spin_unlock(ptl); |
12724850 NH |
6194 | return 0; |
6195 | } | |
6196 | target_type = get_mctgt_type_thp(vma, addr, *pmd, &target); | |
6197 | if (target_type == MC_TARGET_PAGE) { | |
6198 | page = target.page; | |
f7f9c00d | 6199 | if (isolate_lru_page(page)) { |
f627c2f5 | 6200 | if (!mem_cgroup_move_account(page, true, |
1306a85a | 6201 | mc.from, mc.to)) { |
12724850 NH |
6202 | mc.precharge -= HPAGE_PMD_NR; |
6203 | mc.moved_charge += HPAGE_PMD_NR; | |
6204 | } | |
6205 | putback_lru_page(page); | |
6206 | } | |
4e0cf05f | 6207 | unlock_page(page); |
12724850 | 6208 | put_page(page); |
c733a828 JG |
6209 | } else if (target_type == MC_TARGET_DEVICE) { |
6210 | page = target.page; | |
6211 | if (!mem_cgroup_move_account(page, true, | |
6212 | mc.from, mc.to)) { | |
6213 | mc.precharge -= HPAGE_PMD_NR; | |
6214 | mc.moved_charge += HPAGE_PMD_NR; | |
6215 | } | |
4e0cf05f | 6216 | unlock_page(page); |
c733a828 | 6217 | put_page(page); |
12724850 | 6218 | } |
bf929152 | 6219 | spin_unlock(ptl); |
1a5a9906 | 6220 | return 0; |
12724850 NH |
6221 | } |
6222 | ||
4ffef5fe DN |
6223 | retry: |
6224 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); | |
04dee9e8 HD |
6225 | if (!pte) |
6226 | return 0; | |
4ffef5fe | 6227 | for (; addr != end; addr += PAGE_SIZE) { |
c33c7948 | 6228 | pte_t ptent = ptep_get(pte++); |
c733a828 | 6229 | bool device = false; |
02491447 | 6230 | swp_entry_t ent; |
4ffef5fe DN |
6231 | |
6232 | if (!mc.precharge) | |
6233 | break; | |
6234 | ||
8d32ff84 | 6235 | switch (get_mctgt_type(vma, addr, ptent, &target)) { |
c733a828 JG |
6236 | case MC_TARGET_DEVICE: |
6237 | device = true; | |
e4a9bc58 | 6238 | fallthrough; |
4ffef5fe DN |
6239 | case MC_TARGET_PAGE: |
6240 | page = target.page; | |
53f9263b KS |
6241 | /* |
6242 | * We can have a part of the split pmd here. Moving it | |
6243 | * can be done but it would be too convoluted so simply | |
6244 | * ignore such a partial THP and keep it in original | |
6245 | * memcg. There should be somebody mapping the head. | |
6246 | */ | |
6247 | if (PageTransCompound(page)) | |
6248 | goto put; | |
f7f9c00d | 6249 | if (!device && !isolate_lru_page(page)) |
4ffef5fe | 6250 | goto put; |
f627c2f5 KS |
6251 | if (!mem_cgroup_move_account(page, false, |
6252 | mc.from, mc.to)) { | |
4ffef5fe | 6253 | mc.precharge--; |
854ffa8d DN |
6254 | /* we uncharge from mc.from later. */ |
6255 | mc.moved_charge++; | |
4ffef5fe | 6256 | } |
c733a828 JG |
6257 | if (!device) |
6258 | putback_lru_page(page); | |
4e0cf05f JW |
6259 | put: /* get_mctgt_type() gets & locks the page */ |
6260 | unlock_page(page); | |
4ffef5fe DN |
6261 | put_page(page); |
6262 | break; | |
02491447 DN |
6263 | case MC_TARGET_SWAP: |
6264 | ent = target.ent; | |
e91cbb42 | 6265 | if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) { |
02491447 | 6266 | mc.precharge--; |
8d22a935 HD |
6267 | mem_cgroup_id_get_many(mc.to, 1); |
6268 | /* we fixup other refcnts and charges later. */ | |
483c30b5 DN |
6269 | mc.moved_swap++; |
6270 | } | |
02491447 | 6271 | break; |
4ffef5fe DN |
6272 | default: |
6273 | break; | |
6274 | } | |
6275 | } | |
6276 | pte_unmap_unlock(pte - 1, ptl); | |
6277 | cond_resched(); | |
6278 | ||
6279 | if (addr != end) { | |
6280 | /* | |
6281 | * We have consumed all precharges we got in can_attach(). | |
6282 | * We try charge one by one, but don't do any additional | |
6283 | * charges to mc.to if we have failed in charge once in attach() | |
6284 | * phase. | |
6285 | */ | |
854ffa8d | 6286 | ret = mem_cgroup_do_precharge(1); |
4ffef5fe DN |
6287 | if (!ret) |
6288 | goto retry; | |
6289 | } | |
6290 | ||
6291 | return ret; | |
6292 | } | |
6293 | ||
7b86ac33 CH |
6294 | static const struct mm_walk_ops charge_walk_ops = { |
6295 | .pmd_entry = mem_cgroup_move_charge_pte_range, | |
6296 | }; | |
6297 | ||
264a0ae1 | 6298 | static void mem_cgroup_move_charge(void) |
4ffef5fe | 6299 | { |
4ffef5fe | 6300 | lru_add_drain_all(); |
312722cb | 6301 | /* |
6c77b607 | 6302 | * Signal folio_memcg_lock() to take the memcg's move_lock |
81f8c3a4 JW |
6303 | * while we're moving its pages to another memcg. Then wait |
6304 | * for already started RCU-only updates to finish. | |
312722cb JW |
6305 | */ |
6306 | atomic_inc(&mc.from->moving_account); | |
6307 | synchronize_rcu(); | |
dfe076b0 | 6308 | retry: |
d8ed45c5 | 6309 | if (unlikely(!mmap_read_trylock(mc.mm))) { |
dfe076b0 | 6310 | /* |
c1e8d7c6 | 6311 | * Someone who are holding the mmap_lock might be waiting in |
dfe076b0 DN |
6312 | * waitq. So we cancel all extra charges, wake up all waiters, |
6313 | * and retry. Because we cancel precharges, we might not be able | |
6314 | * to move enough charges, but moving charge is a best-effort | |
6315 | * feature anyway, so it wouldn't be a big problem. | |
6316 | */ | |
6317 | __mem_cgroup_clear_mc(); | |
6318 | cond_resched(); | |
6319 | goto retry; | |
6320 | } | |
26bcd64a NH |
6321 | /* |
6322 | * When we have consumed all precharges and failed in doing | |
6323 | * additional charge, the page walk just aborts. | |
6324 | */ | |
ba0aff8e | 6325 | walk_page_range(mc.mm, 0, ULONG_MAX, &charge_walk_ops, NULL); |
d8ed45c5 | 6326 | mmap_read_unlock(mc.mm); |
312722cb | 6327 | atomic_dec(&mc.from->moving_account); |
7dc74be0 DN |
6328 | } |
6329 | ||
264a0ae1 | 6330 | static void mem_cgroup_move_task(void) |
67e465a7 | 6331 | { |
264a0ae1 TH |
6332 | if (mc.to) { |
6333 | mem_cgroup_move_charge(); | |
a433658c | 6334 | mem_cgroup_clear_mc(); |
264a0ae1 | 6335 | } |
67e465a7 | 6336 | } |
5cfb80a7 | 6337 | #else /* !CONFIG_MMU */ |
1f7dd3e5 | 6338 | static int mem_cgroup_can_attach(struct cgroup_taskset *tset) |
5cfb80a7 DN |
6339 | { |
6340 | return 0; | |
6341 | } | |
1f7dd3e5 | 6342 | static void mem_cgroup_cancel_attach(struct cgroup_taskset *tset) |
5cfb80a7 DN |
6343 | { |
6344 | } | |
264a0ae1 | 6345 | static void mem_cgroup_move_task(void) |
5cfb80a7 DN |
6346 | { |
6347 | } | |
6348 | #endif | |
67e465a7 | 6349 | |
bd74fdae YZ |
6350 | #ifdef CONFIG_LRU_GEN |
6351 | static void mem_cgroup_attach(struct cgroup_taskset *tset) | |
6352 | { | |
6353 | struct task_struct *task; | |
6354 | struct cgroup_subsys_state *css; | |
6355 | ||
6356 | /* find the first leader if there is any */ | |
6357 | cgroup_taskset_for_each_leader(task, css, tset) | |
6358 | break; | |
6359 | ||
6360 | if (!task) | |
6361 | return; | |
6362 | ||
6363 | task_lock(task); | |
6364 | if (task->mm && READ_ONCE(task->mm->owner) == task) | |
6365 | lru_gen_migrate_mm(task->mm); | |
6366 | task_unlock(task); | |
6367 | } | |
6368 | #else | |
6369 | static void mem_cgroup_attach(struct cgroup_taskset *tset) | |
6370 | { | |
6371 | } | |
6372 | #endif /* CONFIG_LRU_GEN */ | |
6373 | ||
677dc973 CD |
6374 | static int seq_puts_memcg_tunable(struct seq_file *m, unsigned long value) |
6375 | { | |
6376 | if (value == PAGE_COUNTER_MAX) | |
6377 | seq_puts(m, "max\n"); | |
6378 | else | |
6379 | seq_printf(m, "%llu\n", (u64)value * PAGE_SIZE); | |
6380 | ||
6381 | return 0; | |
6382 | } | |
6383 | ||
241994ed JW |
6384 | static u64 memory_current_read(struct cgroup_subsys_state *css, |
6385 | struct cftype *cft) | |
6386 | { | |
f5fc3c5d JW |
6387 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
6388 | ||
6389 | return (u64)page_counter_read(&memcg->memory) * PAGE_SIZE; | |
241994ed JW |
6390 | } |
6391 | ||
8e20d4b3 GR |
6392 | static u64 memory_peak_read(struct cgroup_subsys_state *css, |
6393 | struct cftype *cft) | |
6394 | { | |
6395 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); | |
6396 | ||
6397 | return (u64)memcg->memory.watermark * PAGE_SIZE; | |
6398 | } | |
6399 | ||
bf8d5d52 RG |
6400 | static int memory_min_show(struct seq_file *m, void *v) |
6401 | { | |
677dc973 CD |
6402 | return seq_puts_memcg_tunable(m, |
6403 | READ_ONCE(mem_cgroup_from_seq(m)->memory.min)); | |
bf8d5d52 RG |
6404 | } |
6405 | ||
6406 | static ssize_t memory_min_write(struct kernfs_open_file *of, | |
6407 | char *buf, size_t nbytes, loff_t off) | |
6408 | { | |
6409 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); | |
6410 | unsigned long min; | |
6411 | int err; | |
6412 | ||
6413 | buf = strstrip(buf); | |
6414 | err = page_counter_memparse(buf, "max", &min); | |
6415 | if (err) | |
6416 | return err; | |
6417 | ||
6418 | page_counter_set_min(&memcg->memory, min); | |
6419 | ||
6420 | return nbytes; | |
6421 | } | |
6422 | ||
241994ed JW |
6423 | static int memory_low_show(struct seq_file *m, void *v) |
6424 | { | |
677dc973 CD |
6425 | return seq_puts_memcg_tunable(m, |
6426 | READ_ONCE(mem_cgroup_from_seq(m)->memory.low)); | |
241994ed JW |
6427 | } |
6428 | ||
6429 | static ssize_t memory_low_write(struct kernfs_open_file *of, | |
6430 | char *buf, size_t nbytes, loff_t off) | |
6431 | { | |
6432 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); | |
6433 | unsigned long low; | |
6434 | int err; | |
6435 | ||
6436 | buf = strstrip(buf); | |
d2973697 | 6437 | err = page_counter_memparse(buf, "max", &low); |
241994ed JW |
6438 | if (err) |
6439 | return err; | |
6440 | ||
23067153 | 6441 | page_counter_set_low(&memcg->memory, low); |
241994ed JW |
6442 | |
6443 | return nbytes; | |
6444 | } | |
6445 | ||
6446 | static int memory_high_show(struct seq_file *m, void *v) | |
6447 | { | |
d1663a90 JK |
6448 | return seq_puts_memcg_tunable(m, |
6449 | READ_ONCE(mem_cgroup_from_seq(m)->memory.high)); | |
241994ed JW |
6450 | } |
6451 | ||
6452 | static ssize_t memory_high_write(struct kernfs_open_file *of, | |
6453 | char *buf, size_t nbytes, loff_t off) | |
6454 | { | |
6455 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); | |
d977aa93 | 6456 | unsigned int nr_retries = MAX_RECLAIM_RETRIES; |
8c8c383c | 6457 | bool drained = false; |
241994ed JW |
6458 | unsigned long high; |
6459 | int err; | |
6460 | ||
6461 | buf = strstrip(buf); | |
d2973697 | 6462 | err = page_counter_memparse(buf, "max", &high); |
241994ed JW |
6463 | if (err) |
6464 | return err; | |
6465 | ||
e82553c1 JW |
6466 | page_counter_set_high(&memcg->memory, high); |
6467 | ||
8c8c383c JW |
6468 | for (;;) { |
6469 | unsigned long nr_pages = page_counter_read(&memcg->memory); | |
6470 | unsigned long reclaimed; | |
6471 | ||
6472 | if (nr_pages <= high) | |
6473 | break; | |
6474 | ||
6475 | if (signal_pending(current)) | |
6476 | break; | |
6477 | ||
6478 | if (!drained) { | |
6479 | drain_all_stock(memcg); | |
6480 | drained = true; | |
6481 | continue; | |
6482 | } | |
6483 | ||
6484 | reclaimed = try_to_free_mem_cgroup_pages(memcg, nr_pages - high, | |
55ab834a | 6485 | GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP); |
8c8c383c JW |
6486 | |
6487 | if (!reclaimed && !nr_retries--) | |
6488 | break; | |
6489 | } | |
588083bb | 6490 | |
19ce33ac | 6491 | memcg_wb_domain_size_changed(memcg); |
241994ed JW |
6492 | return nbytes; |
6493 | } | |
6494 | ||
6495 | static int memory_max_show(struct seq_file *m, void *v) | |
6496 | { | |
677dc973 CD |
6497 | return seq_puts_memcg_tunable(m, |
6498 | READ_ONCE(mem_cgroup_from_seq(m)->memory.max)); | |
241994ed JW |
6499 | } |
6500 | ||
6501 | static ssize_t memory_max_write(struct kernfs_open_file *of, | |
6502 | char *buf, size_t nbytes, loff_t off) | |
6503 | { | |
6504 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); | |
d977aa93 | 6505 | unsigned int nr_reclaims = MAX_RECLAIM_RETRIES; |
b6e6edcf | 6506 | bool drained = false; |
241994ed JW |
6507 | unsigned long max; |
6508 | int err; | |
6509 | ||
6510 | buf = strstrip(buf); | |
d2973697 | 6511 | err = page_counter_memparse(buf, "max", &max); |
241994ed JW |
6512 | if (err) |
6513 | return err; | |
6514 | ||
bbec2e15 | 6515 | xchg(&memcg->memory.max, max); |
b6e6edcf JW |
6516 | |
6517 | for (;;) { | |
6518 | unsigned long nr_pages = page_counter_read(&memcg->memory); | |
6519 | ||
6520 | if (nr_pages <= max) | |
6521 | break; | |
6522 | ||
7249c9f0 | 6523 | if (signal_pending(current)) |
b6e6edcf | 6524 | break; |
b6e6edcf JW |
6525 | |
6526 | if (!drained) { | |
6527 | drain_all_stock(memcg); | |
6528 | drained = true; | |
6529 | continue; | |
6530 | } | |
6531 | ||
6532 | if (nr_reclaims) { | |
6533 | if (!try_to_free_mem_cgroup_pages(memcg, nr_pages - max, | |
55ab834a | 6534 | GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP)) |
b6e6edcf JW |
6535 | nr_reclaims--; |
6536 | continue; | |
6537 | } | |
6538 | ||
e27be240 | 6539 | memcg_memory_event(memcg, MEMCG_OOM); |
b6e6edcf JW |
6540 | if (!mem_cgroup_out_of_memory(memcg, GFP_KERNEL, 0)) |
6541 | break; | |
6542 | } | |
241994ed | 6543 | |
2529bb3a | 6544 | memcg_wb_domain_size_changed(memcg); |
241994ed JW |
6545 | return nbytes; |
6546 | } | |
6547 | ||
1e577f97 SB |
6548 | static void __memory_events_show(struct seq_file *m, atomic_long_t *events) |
6549 | { | |
6550 | seq_printf(m, "low %lu\n", atomic_long_read(&events[MEMCG_LOW])); | |
6551 | seq_printf(m, "high %lu\n", atomic_long_read(&events[MEMCG_HIGH])); | |
6552 | seq_printf(m, "max %lu\n", atomic_long_read(&events[MEMCG_MAX])); | |
6553 | seq_printf(m, "oom %lu\n", atomic_long_read(&events[MEMCG_OOM])); | |
6554 | seq_printf(m, "oom_kill %lu\n", | |
6555 | atomic_long_read(&events[MEMCG_OOM_KILL])); | |
b6bf9abb DS |
6556 | seq_printf(m, "oom_group_kill %lu\n", |
6557 | atomic_long_read(&events[MEMCG_OOM_GROUP_KILL])); | |
1e577f97 SB |
6558 | } |
6559 | ||
241994ed JW |
6560 | static int memory_events_show(struct seq_file *m, void *v) |
6561 | { | |
aa9694bb | 6562 | struct mem_cgroup *memcg = mem_cgroup_from_seq(m); |
241994ed | 6563 | |
1e577f97 SB |
6564 | __memory_events_show(m, memcg->memory_events); |
6565 | return 0; | |
6566 | } | |
6567 | ||
6568 | static int memory_events_local_show(struct seq_file *m, void *v) | |
6569 | { | |
6570 | struct mem_cgroup *memcg = mem_cgroup_from_seq(m); | |
241994ed | 6571 | |
1e577f97 | 6572 | __memory_events_show(m, memcg->memory_events_local); |
241994ed JW |
6573 | return 0; |
6574 | } | |
6575 | ||
587d9f72 JW |
6576 | static int memory_stat_show(struct seq_file *m, void *v) |
6577 | { | |
aa9694bb | 6578 | struct mem_cgroup *memcg = mem_cgroup_from_seq(m); |
68aaee14 | 6579 | char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
5b42360c | 6580 | struct seq_buf s; |
1ff9e6e1 | 6581 | |
c8713d0b JW |
6582 | if (!buf) |
6583 | return -ENOMEM; | |
5b42360c YA |
6584 | seq_buf_init(&s, buf, PAGE_SIZE); |
6585 | memory_stat_format(memcg, &s); | |
c8713d0b JW |
6586 | seq_puts(m, buf); |
6587 | kfree(buf); | |
587d9f72 JW |
6588 | return 0; |
6589 | } | |
6590 | ||
5f9a4f4a | 6591 | #ifdef CONFIG_NUMA |
fff66b79 MS |
6592 | static inline unsigned long lruvec_page_state_output(struct lruvec *lruvec, |
6593 | int item) | |
6594 | { | |
6595 | return lruvec_page_state(lruvec, item) * memcg_page_state_unit(item); | |
6596 | } | |
6597 | ||
5f9a4f4a MS |
6598 | static int memory_numa_stat_show(struct seq_file *m, void *v) |
6599 | { | |
6600 | int i; | |
6601 | struct mem_cgroup *memcg = mem_cgroup_from_seq(m); | |
6602 | ||
fd25a9e0 | 6603 | mem_cgroup_flush_stats(); |
7e1c0d6f | 6604 | |
5f9a4f4a MS |
6605 | for (i = 0; i < ARRAY_SIZE(memory_stats); i++) { |
6606 | int nid; | |
6607 | ||
6608 | if (memory_stats[i].idx >= NR_VM_NODE_STAT_ITEMS) | |
6609 | continue; | |
6610 | ||
6611 | seq_printf(m, "%s", memory_stats[i].name); | |
6612 | for_each_node_state(nid, N_MEMORY) { | |
6613 | u64 size; | |
6614 | struct lruvec *lruvec; | |
6615 | ||
6616 | lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid)); | |
fff66b79 MS |
6617 | size = lruvec_page_state_output(lruvec, |
6618 | memory_stats[i].idx); | |
5f9a4f4a MS |
6619 | seq_printf(m, " N%d=%llu", nid, size); |
6620 | } | |
6621 | seq_putc(m, '\n'); | |
6622 | } | |
6623 | ||
6624 | return 0; | |
6625 | } | |
6626 | #endif | |
6627 | ||
3d8b38eb RG |
6628 | static int memory_oom_group_show(struct seq_file *m, void *v) |
6629 | { | |
aa9694bb | 6630 | struct mem_cgroup *memcg = mem_cgroup_from_seq(m); |
3d8b38eb | 6631 | |
eaf7b66b | 6632 | seq_printf(m, "%d\n", READ_ONCE(memcg->oom_group)); |
3d8b38eb RG |
6633 | |
6634 | return 0; | |
6635 | } | |
6636 | ||
6637 | static ssize_t memory_oom_group_write(struct kernfs_open_file *of, | |
6638 | char *buf, size_t nbytes, loff_t off) | |
6639 | { | |
6640 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); | |
6641 | int ret, oom_group; | |
6642 | ||
6643 | buf = strstrip(buf); | |
6644 | if (!buf) | |
6645 | return -EINVAL; | |
6646 | ||
6647 | ret = kstrtoint(buf, 0, &oom_group); | |
6648 | if (ret) | |
6649 | return ret; | |
6650 | ||
6651 | if (oom_group != 0 && oom_group != 1) | |
6652 | return -EINVAL; | |
6653 | ||
eaf7b66b | 6654 | WRITE_ONCE(memcg->oom_group, oom_group); |
3d8b38eb RG |
6655 | |
6656 | return nbytes; | |
6657 | } | |
6658 | ||
94968384 SB |
6659 | static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf, |
6660 | size_t nbytes, loff_t off) | |
6661 | { | |
6662 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); | |
6663 | unsigned int nr_retries = MAX_RECLAIM_RETRIES; | |
6664 | unsigned long nr_to_reclaim, nr_reclaimed = 0; | |
55ab834a MH |
6665 | unsigned int reclaim_options; |
6666 | int err; | |
12a5d395 MA |
6667 | |
6668 | buf = strstrip(buf); | |
55ab834a MH |
6669 | err = page_counter_memparse(buf, "", &nr_to_reclaim); |
6670 | if (err) | |
6671 | return err; | |
12a5d395 | 6672 | |
55ab834a | 6673 | reclaim_options = MEMCG_RECLAIM_MAY_SWAP | MEMCG_RECLAIM_PROACTIVE; |
94968384 SB |
6674 | while (nr_reclaimed < nr_to_reclaim) { |
6675 | unsigned long reclaimed; | |
6676 | ||
6677 | if (signal_pending(current)) | |
6678 | return -EINTR; | |
6679 | ||
6680 | /* | |
6681 | * This is the final attempt, drain percpu lru caches in the | |
6682 | * hope of introducing more evictable pages for | |
6683 | * try_to_free_mem_cgroup_pages(). | |
6684 | */ | |
6685 | if (!nr_retries) | |
6686 | lru_add_drain_all(); | |
6687 | ||
6688 | reclaimed = try_to_free_mem_cgroup_pages(memcg, | |
6689 | nr_to_reclaim - nr_reclaimed, | |
55ab834a | 6690 | GFP_KERNEL, reclaim_options); |
94968384 SB |
6691 | |
6692 | if (!reclaimed && !nr_retries--) | |
6693 | return -EAGAIN; | |
6694 | ||
6695 | nr_reclaimed += reclaimed; | |
6696 | } | |
6697 | ||
6698 | return nbytes; | |
6699 | } | |
6700 | ||
241994ed JW |
6701 | static struct cftype memory_files[] = { |
6702 | { | |
6703 | .name = "current", | |
f5fc3c5d | 6704 | .flags = CFTYPE_NOT_ON_ROOT, |
241994ed JW |
6705 | .read_u64 = memory_current_read, |
6706 | }, | |
8e20d4b3 GR |
6707 | { |
6708 | .name = "peak", | |
6709 | .flags = CFTYPE_NOT_ON_ROOT, | |
6710 | .read_u64 = memory_peak_read, | |
6711 | }, | |
bf8d5d52 RG |
6712 | { |
6713 | .name = "min", | |
6714 | .flags = CFTYPE_NOT_ON_ROOT, | |
6715 | .seq_show = memory_min_show, | |
6716 | .write = memory_min_write, | |
6717 | }, | |
241994ed JW |
6718 | { |
6719 | .name = "low", | |
6720 | .flags = CFTYPE_NOT_ON_ROOT, | |
6721 | .seq_show = memory_low_show, | |
6722 | .write = memory_low_write, | |
6723 | }, | |
6724 | { | |
6725 | .name = "high", | |
6726 | .flags = CFTYPE_NOT_ON_ROOT, | |
6727 | .seq_show = memory_high_show, | |
6728 | .write = memory_high_write, | |
6729 | }, | |
6730 | { | |
6731 | .name = "max", | |
6732 | .flags = CFTYPE_NOT_ON_ROOT, | |
6733 | .seq_show = memory_max_show, | |
6734 | .write = memory_max_write, | |
6735 | }, | |
6736 | { | |
6737 | .name = "events", | |
6738 | .flags = CFTYPE_NOT_ON_ROOT, | |
472912a2 | 6739 | .file_offset = offsetof(struct mem_cgroup, events_file), |
241994ed JW |
6740 | .seq_show = memory_events_show, |
6741 | }, | |
1e577f97 SB |
6742 | { |
6743 | .name = "events.local", | |
6744 | .flags = CFTYPE_NOT_ON_ROOT, | |
6745 | .file_offset = offsetof(struct mem_cgroup, events_local_file), | |
6746 | .seq_show = memory_events_local_show, | |
6747 | }, | |
587d9f72 JW |
6748 | { |
6749 | .name = "stat", | |
587d9f72 JW |
6750 | .seq_show = memory_stat_show, |
6751 | }, | |
5f9a4f4a MS |
6752 | #ifdef CONFIG_NUMA |
6753 | { | |
6754 | .name = "numa_stat", | |
6755 | .seq_show = memory_numa_stat_show, | |
6756 | }, | |
6757 | #endif | |
3d8b38eb RG |
6758 | { |
6759 | .name = "oom.group", | |
6760 | .flags = CFTYPE_NOT_ON_ROOT | CFTYPE_NS_DELEGATABLE, | |
6761 | .seq_show = memory_oom_group_show, | |
6762 | .write = memory_oom_group_write, | |
6763 | }, | |
94968384 SB |
6764 | { |
6765 | .name = "reclaim", | |
6766 | .flags = CFTYPE_NS_DELEGATABLE, | |
6767 | .write = memory_reclaim, | |
6768 | }, | |
241994ed JW |
6769 | { } /* terminate */ |
6770 | }; | |
6771 | ||
073219e9 | 6772 | struct cgroup_subsys memory_cgrp_subsys = { |
92fb9748 | 6773 | .css_alloc = mem_cgroup_css_alloc, |
d142e3e6 | 6774 | .css_online = mem_cgroup_css_online, |
92fb9748 | 6775 | .css_offline = mem_cgroup_css_offline, |
6df38689 | 6776 | .css_released = mem_cgroup_css_released, |
92fb9748 | 6777 | .css_free = mem_cgroup_css_free, |
1ced953b | 6778 | .css_reset = mem_cgroup_css_reset, |
2d146aa3 | 6779 | .css_rstat_flush = mem_cgroup_css_rstat_flush, |
7dc74be0 | 6780 | .can_attach = mem_cgroup_can_attach, |
bd74fdae | 6781 | .attach = mem_cgroup_attach, |
7dc74be0 | 6782 | .cancel_attach = mem_cgroup_cancel_attach, |
264a0ae1 | 6783 | .post_attach = mem_cgroup_move_task, |
241994ed JW |
6784 | .dfl_cftypes = memory_files, |
6785 | .legacy_cftypes = mem_cgroup_legacy_files, | |
6d12e2d8 | 6786 | .early_init = 0, |
8cdea7c0 | 6787 | }; |
c077719b | 6788 | |
bc50bcc6 JW |
6789 | /* |
6790 | * This function calculates an individual cgroup's effective | |
6791 | * protection which is derived from its own memory.min/low, its | |
6792 | * parent's and siblings' settings, as well as the actual memory | |
6793 | * distribution in the tree. | |
6794 | * | |
6795 | * The following rules apply to the effective protection values: | |
6796 | * | |
6797 | * 1. At the first level of reclaim, effective protection is equal to | |
6798 | * the declared protection in memory.min and memory.low. | |
6799 | * | |
6800 | * 2. To enable safe delegation of the protection configuration, at | |
6801 | * subsequent levels the effective protection is capped to the | |
6802 | * parent's effective protection. | |
6803 | * | |
6804 | * 3. To make complex and dynamic subtrees easier to configure, the | |
6805 | * user is allowed to overcommit the declared protection at a given | |
6806 | * level. If that is the case, the parent's effective protection is | |
6807 | * distributed to the children in proportion to how much protection | |
6808 | * they have declared and how much of it they are utilizing. | |
6809 | * | |
6810 | * This makes distribution proportional, but also work-conserving: | |
6811 | * if one cgroup claims much more protection than it uses memory, | |
6812 | * the unused remainder is available to its siblings. | |
6813 | * | |
6814 | * 4. Conversely, when the declared protection is undercommitted at a | |
6815 | * given level, the distribution of the larger parental protection | |
6816 | * budget is NOT proportional. A cgroup's protection from a sibling | |
6817 | * is capped to its own memory.min/low setting. | |
6818 | * | |
8a931f80 JW |
6819 | * 5. However, to allow protecting recursive subtrees from each other |
6820 | * without having to declare each individual cgroup's fixed share | |
6821 | * of the ancestor's claim to protection, any unutilized - | |
6822 | * "floating" - protection from up the tree is distributed in | |
6823 | * proportion to each cgroup's *usage*. This makes the protection | |
6824 | * neutral wrt sibling cgroups and lets them compete freely over | |
6825 | * the shared parental protection budget, but it protects the | |
6826 | * subtree as a whole from neighboring subtrees. | |
6827 | * | |
6828 | * Note that 4. and 5. are not in conflict: 4. is about protecting | |
6829 | * against immediate siblings whereas 5. is about protecting against | |
6830 | * neighboring subtrees. | |
bc50bcc6 JW |
6831 | */ |
6832 | static unsigned long effective_protection(unsigned long usage, | |
8a931f80 | 6833 | unsigned long parent_usage, |
bc50bcc6 JW |
6834 | unsigned long setting, |
6835 | unsigned long parent_effective, | |
6836 | unsigned long siblings_protected) | |
6837 | { | |
6838 | unsigned long protected; | |
8a931f80 | 6839 | unsigned long ep; |
bc50bcc6 JW |
6840 | |
6841 | protected = min(usage, setting); | |
6842 | /* | |
6843 | * If all cgroups at this level combined claim and use more | |
08e0f49e | 6844 | * protection than what the parent affords them, distribute |
bc50bcc6 JW |
6845 | * shares in proportion to utilization. |
6846 | * | |
6847 | * We are using actual utilization rather than the statically | |
6848 | * claimed protection in order to be work-conserving: claimed | |
6849 | * but unused protection is available to siblings that would | |
6850 | * otherwise get a smaller chunk than what they claimed. | |
6851 | */ | |
6852 | if (siblings_protected > parent_effective) | |
6853 | return protected * parent_effective / siblings_protected; | |
6854 | ||
6855 | /* | |
6856 | * Ok, utilized protection of all children is within what the | |
6857 | * parent affords them, so we know whatever this child claims | |
6858 | * and utilizes is effectively protected. | |
6859 | * | |
6860 | * If there is unprotected usage beyond this value, reclaim | |
6861 | * will apply pressure in proportion to that amount. | |
6862 | * | |
6863 | * If there is unutilized protection, the cgroup will be fully | |
6864 | * shielded from reclaim, but we do return a smaller value for | |
6865 | * protection than what the group could enjoy in theory. This | |
6866 | * is okay. With the overcommit distribution above, effective | |
6867 | * protection is always dependent on how memory is actually | |
6868 | * consumed among the siblings anyway. | |
6869 | */ | |
8a931f80 JW |
6870 | ep = protected; |
6871 | ||
6872 | /* | |
6873 | * If the children aren't claiming (all of) the protection | |
6874 | * afforded to them by the parent, distribute the remainder in | |
6875 | * proportion to the (unprotected) memory of each cgroup. That | |
6876 | * way, cgroups that aren't explicitly prioritized wrt each | |
6877 | * other compete freely over the allowance, but they are | |
6878 | * collectively protected from neighboring trees. | |
6879 | * | |
6880 | * We're using unprotected memory for the weight so that if | |
6881 | * some cgroups DO claim explicit protection, we don't protect | |
6882 | * the same bytes twice. | |
cd324edc JW |
6883 | * |
6884 | * Check both usage and parent_usage against the respective | |
6885 | * protected values. One should imply the other, but they | |
6886 | * aren't read atomically - make sure the division is sane. | |
8a931f80 JW |
6887 | */ |
6888 | if (!(cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT)) | |
6889 | return ep; | |
cd324edc JW |
6890 | if (parent_effective > siblings_protected && |
6891 | parent_usage > siblings_protected && | |
6892 | usage > protected) { | |
8a931f80 JW |
6893 | unsigned long unclaimed; |
6894 | ||
6895 | unclaimed = parent_effective - siblings_protected; | |
6896 | unclaimed *= usage - protected; | |
6897 | unclaimed /= parent_usage - siblings_protected; | |
6898 | ||
6899 | ep += unclaimed; | |
6900 | } | |
6901 | ||
6902 | return ep; | |
bc50bcc6 JW |
6903 | } |
6904 | ||
241994ed | 6905 | /** |
05395718 | 6906 | * mem_cgroup_calculate_protection - check if memory consumption is in the normal range |
34c81057 | 6907 | * @root: the top ancestor of the sub-tree being checked |
241994ed JW |
6908 | * @memcg: the memory cgroup to check |
6909 | * | |
23067153 RG |
6910 | * WARNING: This function is not stateless! It can only be used as part |
6911 | * of a top-down tree iteration, not for isolated queries. | |
241994ed | 6912 | */ |
45c7f7e1 CD |
6913 | void mem_cgroup_calculate_protection(struct mem_cgroup *root, |
6914 | struct mem_cgroup *memcg) | |
241994ed | 6915 | { |
8a931f80 | 6916 | unsigned long usage, parent_usage; |
23067153 RG |
6917 | struct mem_cgroup *parent; |
6918 | ||
241994ed | 6919 | if (mem_cgroup_disabled()) |
45c7f7e1 | 6920 | return; |
241994ed | 6921 | |
34c81057 SC |
6922 | if (!root) |
6923 | root = root_mem_cgroup; | |
22f7496f YS |
6924 | |
6925 | /* | |
6926 | * Effective values of the reclaim targets are ignored so they | |
6927 | * can be stale. Have a look at mem_cgroup_protection for more | |
6928 | * details. | |
6929 | * TODO: calculation should be more robust so that we do not need | |
6930 | * that special casing. | |
6931 | */ | |
34c81057 | 6932 | if (memcg == root) |
45c7f7e1 | 6933 | return; |
241994ed | 6934 | |
23067153 | 6935 | usage = page_counter_read(&memcg->memory); |
bf8d5d52 | 6936 | if (!usage) |
45c7f7e1 | 6937 | return; |
bf8d5d52 | 6938 | |
bf8d5d52 | 6939 | parent = parent_mem_cgroup(memcg); |
df2a4196 | 6940 | |
bc50bcc6 | 6941 | if (parent == root) { |
c3d53200 | 6942 | memcg->memory.emin = READ_ONCE(memcg->memory.min); |
03960e33 | 6943 | memcg->memory.elow = READ_ONCE(memcg->memory.low); |
45c7f7e1 | 6944 | return; |
bf8d5d52 RG |
6945 | } |
6946 | ||
8a931f80 JW |
6947 | parent_usage = page_counter_read(&parent->memory); |
6948 | ||
b3a7822e | 6949 | WRITE_ONCE(memcg->memory.emin, effective_protection(usage, parent_usage, |
c3d53200 CD |
6950 | READ_ONCE(memcg->memory.min), |
6951 | READ_ONCE(parent->memory.emin), | |
b3a7822e | 6952 | atomic_long_read(&parent->memory.children_min_usage))); |
23067153 | 6953 | |
b3a7822e | 6954 | WRITE_ONCE(memcg->memory.elow, effective_protection(usage, parent_usage, |
03960e33 CD |
6955 | READ_ONCE(memcg->memory.low), |
6956 | READ_ONCE(parent->memory.elow), | |
b3a7822e | 6957 | atomic_long_read(&parent->memory.children_low_usage))); |
241994ed JW |
6958 | } |
6959 | ||
8f425e4e MWO |
6960 | static int charge_memcg(struct folio *folio, struct mem_cgroup *memcg, |
6961 | gfp_t gfp) | |
0add0c77 | 6962 | { |
118f2875 | 6963 | long nr_pages = folio_nr_pages(folio); |
0add0c77 SB |
6964 | int ret; |
6965 | ||
6966 | ret = try_charge(memcg, gfp, nr_pages); | |
6967 | if (ret) | |
6968 | goto out; | |
6969 | ||
6970 | css_get(&memcg->css); | |
118f2875 | 6971 | commit_charge(folio, memcg); |
0add0c77 SB |
6972 | |
6973 | local_irq_disable(); | |
6e0110c2 | 6974 | mem_cgroup_charge_statistics(memcg, nr_pages); |
8f425e4e | 6975 | memcg_check_events(memcg, folio_nid(folio)); |
0add0c77 SB |
6976 | local_irq_enable(); |
6977 | out: | |
6978 | return ret; | |
6979 | } | |
6980 | ||
8f425e4e | 6981 | int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp) |
00501b53 | 6982 | { |
0add0c77 SB |
6983 | struct mem_cgroup *memcg; |
6984 | int ret; | |
00501b53 | 6985 | |
0add0c77 | 6986 | memcg = get_mem_cgroup_from_mm(mm); |
8f425e4e | 6987 | ret = charge_memcg(folio, memcg, gfp); |
0add0c77 | 6988 | css_put(&memcg->css); |
2d1c4980 | 6989 | |
0add0c77 SB |
6990 | return ret; |
6991 | } | |
e993d905 | 6992 | |
0add0c77 | 6993 | /** |
65995918 MWO |
6994 | * mem_cgroup_swapin_charge_folio - Charge a newly allocated folio for swapin. |
6995 | * @folio: folio to charge. | |
0add0c77 SB |
6996 | * @mm: mm context of the victim |
6997 | * @gfp: reclaim mode | |
65995918 | 6998 | * @entry: swap entry for which the folio is allocated |
0add0c77 | 6999 | * |
65995918 MWO |
7000 | * This function charges a folio allocated for swapin. Please call this before |
7001 | * adding the folio to the swapcache. | |
0add0c77 SB |
7002 | * |
7003 | * Returns 0 on success. Otherwise, an error code is returned. | |
7004 | */ | |
65995918 | 7005 | int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm, |
0add0c77 SB |
7006 | gfp_t gfp, swp_entry_t entry) |
7007 | { | |
7008 | struct mem_cgroup *memcg; | |
7009 | unsigned short id; | |
7010 | int ret; | |
00501b53 | 7011 | |
0add0c77 SB |
7012 | if (mem_cgroup_disabled()) |
7013 | return 0; | |
00501b53 | 7014 | |
0add0c77 SB |
7015 | id = lookup_swap_cgroup_id(entry); |
7016 | rcu_read_lock(); | |
7017 | memcg = mem_cgroup_from_id(id); | |
7018 | if (!memcg || !css_tryget_online(&memcg->css)) | |
7019 | memcg = get_mem_cgroup_from_mm(mm); | |
7020 | rcu_read_unlock(); | |
00501b53 | 7021 | |
8f425e4e | 7022 | ret = charge_memcg(folio, memcg, gfp); |
6abb5a86 | 7023 | |
0add0c77 SB |
7024 | css_put(&memcg->css); |
7025 | return ret; | |
7026 | } | |
00501b53 | 7027 | |
0add0c77 SB |
7028 | /* |
7029 | * mem_cgroup_swapin_uncharge_swap - uncharge swap slot | |
7030 | * @entry: swap entry for which the page is charged | |
7031 | * | |
7032 | * Call this function after successfully adding the charged page to swapcache. | |
7033 | * | |
7034 | * Note: This function assumes the page for which swap slot is being uncharged | |
7035 | * is order 0 page. | |
7036 | */ | |
7037 | void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry) | |
7038 | { | |
cae3af62 MS |
7039 | /* |
7040 | * Cgroup1's unified memory+swap counter has been charged with the | |
7041 | * new swapcache page, finish the transfer by uncharging the swap | |
7042 | * slot. The swap slot would also get uncharged when it dies, but | |
7043 | * it can stick around indefinitely and we'd count the page twice | |
7044 | * the entire time. | |
7045 | * | |
7046 | * Cgroup2 has separate resource counters for memory and swap, | |
7047 | * so this is a non-issue here. Memory and swap charge lifetimes | |
7048 | * correspond 1:1 to page and swap slot lifetimes: we charge the | |
7049 | * page to memory here, and uncharge swap when the slot is freed. | |
7050 | */ | |
0add0c77 | 7051 | if (!mem_cgroup_disabled() && do_memsw_account()) { |
00501b53 JW |
7052 | /* |
7053 | * The swap entry might not get freed for a long time, | |
7054 | * let's not wait for it. The page already received a | |
7055 | * memory+swap charge, drop the swap entry duplicate. | |
7056 | */ | |
0add0c77 | 7057 | mem_cgroup_uncharge_swap(entry, 1); |
00501b53 | 7058 | } |
3fea5a49 JW |
7059 | } |
7060 | ||
a9d5adee JG |
7061 | struct uncharge_gather { |
7062 | struct mem_cgroup *memcg; | |
b4e0b68f | 7063 | unsigned long nr_memory; |
a9d5adee | 7064 | unsigned long pgpgout; |
a9d5adee | 7065 | unsigned long nr_kmem; |
8e88bd2d | 7066 | int nid; |
a9d5adee JG |
7067 | }; |
7068 | ||
7069 | static inline void uncharge_gather_clear(struct uncharge_gather *ug) | |
747db954 | 7070 | { |
a9d5adee JG |
7071 | memset(ug, 0, sizeof(*ug)); |
7072 | } | |
7073 | ||
7074 | static void uncharge_batch(const struct uncharge_gather *ug) | |
7075 | { | |
747db954 JW |
7076 | unsigned long flags; |
7077 | ||
b4e0b68f MS |
7078 | if (ug->nr_memory) { |
7079 | page_counter_uncharge(&ug->memcg->memory, ug->nr_memory); | |
7941d214 | 7080 | if (do_memsw_account()) |
b4e0b68f | 7081 | page_counter_uncharge(&ug->memcg->memsw, ug->nr_memory); |
a8c49af3 YA |
7082 | if (ug->nr_kmem) |
7083 | memcg_account_kmem(ug->memcg, -ug->nr_kmem); | |
a9d5adee | 7084 | memcg_oom_recover(ug->memcg); |
ce00a967 | 7085 | } |
747db954 JW |
7086 | |
7087 | local_irq_save(flags); | |
c9019e9b | 7088 | __count_memcg_events(ug->memcg, PGPGOUT, ug->pgpgout); |
b4e0b68f | 7089 | __this_cpu_add(ug->memcg->vmstats_percpu->nr_page_events, ug->nr_memory); |
8e88bd2d | 7090 | memcg_check_events(ug->memcg, ug->nid); |
747db954 | 7091 | local_irq_restore(flags); |
f1796544 | 7092 | |
c4ed6ebf | 7093 | /* drop reference from uncharge_folio */ |
f1796544 | 7094 | css_put(&ug->memcg->css); |
a9d5adee JG |
7095 | } |
7096 | ||
c4ed6ebf | 7097 | static void uncharge_folio(struct folio *folio, struct uncharge_gather *ug) |
a9d5adee | 7098 | { |
c4ed6ebf | 7099 | long nr_pages; |
b4e0b68f MS |
7100 | struct mem_cgroup *memcg; |
7101 | struct obj_cgroup *objcg; | |
9f762dbe | 7102 | |
c4ed6ebf | 7103 | VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); |
a9d5adee | 7104 | |
a9d5adee JG |
7105 | /* |
7106 | * Nobody should be changing or seriously looking at | |
c4ed6ebf MWO |
7107 | * folio memcg or objcg at this point, we have fully |
7108 | * exclusive access to the folio. | |
a9d5adee | 7109 | */ |
fead2b86 | 7110 | if (folio_memcg_kmem(folio)) { |
1b7e4464 | 7111 | objcg = __folio_objcg(folio); |
b4e0b68f MS |
7112 | /* |
7113 | * This get matches the put at the end of the function and | |
7114 | * kmem pages do not hold memcg references anymore. | |
7115 | */ | |
7116 | memcg = get_mem_cgroup_from_objcg(objcg); | |
7117 | } else { | |
1b7e4464 | 7118 | memcg = __folio_memcg(folio); |
b4e0b68f | 7119 | } |
a9d5adee | 7120 | |
b4e0b68f MS |
7121 | if (!memcg) |
7122 | return; | |
7123 | ||
7124 | if (ug->memcg != memcg) { | |
a9d5adee JG |
7125 | if (ug->memcg) { |
7126 | uncharge_batch(ug); | |
7127 | uncharge_gather_clear(ug); | |
7128 | } | |
b4e0b68f | 7129 | ug->memcg = memcg; |
c4ed6ebf | 7130 | ug->nid = folio_nid(folio); |
f1796544 MH |
7131 | |
7132 | /* pairs with css_put in uncharge_batch */ | |
b4e0b68f | 7133 | css_get(&memcg->css); |
a9d5adee JG |
7134 | } |
7135 | ||
c4ed6ebf | 7136 | nr_pages = folio_nr_pages(folio); |
a9d5adee | 7137 | |
fead2b86 | 7138 | if (folio_memcg_kmem(folio)) { |
b4e0b68f | 7139 | ug->nr_memory += nr_pages; |
9f762dbe | 7140 | ug->nr_kmem += nr_pages; |
b4e0b68f | 7141 | |
c4ed6ebf | 7142 | folio->memcg_data = 0; |
b4e0b68f MS |
7143 | obj_cgroup_put(objcg); |
7144 | } else { | |
7145 | /* LRU pages aren't accounted at the root level */ | |
7146 | if (!mem_cgroup_is_root(memcg)) | |
7147 | ug->nr_memory += nr_pages; | |
18b2db3b | 7148 | ug->pgpgout++; |
a9d5adee | 7149 | |
c4ed6ebf | 7150 | folio->memcg_data = 0; |
b4e0b68f MS |
7151 | } |
7152 | ||
7153 | css_put(&memcg->css); | |
747db954 JW |
7154 | } |
7155 | ||
bbc6b703 | 7156 | void __mem_cgroup_uncharge(struct folio *folio) |
0a31bc97 | 7157 | { |
a9d5adee JG |
7158 | struct uncharge_gather ug; |
7159 | ||
bbc6b703 MWO |
7160 | /* Don't touch folio->lru of any random page, pre-check: */ |
7161 | if (!folio_memcg(folio)) | |
0a31bc97 JW |
7162 | return; |
7163 | ||
a9d5adee | 7164 | uncharge_gather_clear(&ug); |
bbc6b703 | 7165 | uncharge_folio(folio, &ug); |
a9d5adee | 7166 | uncharge_batch(&ug); |
747db954 | 7167 | } |
0a31bc97 | 7168 | |
747db954 | 7169 | /** |
2c8d8f97 | 7170 | * __mem_cgroup_uncharge_list - uncharge a list of page |
747db954 JW |
7171 | * @page_list: list of pages to uncharge |
7172 | * | |
7173 | * Uncharge a list of pages previously charged with | |
2c8d8f97 | 7174 | * __mem_cgroup_charge(). |
747db954 | 7175 | */ |
2c8d8f97 | 7176 | void __mem_cgroup_uncharge_list(struct list_head *page_list) |
747db954 | 7177 | { |
c41a40b6 | 7178 | struct uncharge_gather ug; |
c4ed6ebf | 7179 | struct folio *folio; |
c41a40b6 | 7180 | |
c41a40b6 | 7181 | uncharge_gather_clear(&ug); |
c4ed6ebf MWO |
7182 | list_for_each_entry(folio, page_list, lru) |
7183 | uncharge_folio(folio, &ug); | |
c41a40b6 MS |
7184 | if (ug.memcg) |
7185 | uncharge_batch(&ug); | |
0a31bc97 JW |
7186 | } |
7187 | ||
7188 | /** | |
d21bba2b MWO |
7189 | * mem_cgroup_migrate - Charge a folio's replacement. |
7190 | * @old: Currently circulating folio. | |
7191 | * @new: Replacement folio. | |
0a31bc97 | 7192 | * |
d21bba2b | 7193 | * Charge @new as a replacement folio for @old. @old will |
6a93ca8f | 7194 | * be uncharged upon free. |
0a31bc97 | 7195 | * |
d21bba2b | 7196 | * Both folios must be locked, @new->mapping must be set up. |
0a31bc97 | 7197 | */ |
d21bba2b | 7198 | void mem_cgroup_migrate(struct folio *old, struct folio *new) |
0a31bc97 | 7199 | { |
29833315 | 7200 | struct mem_cgroup *memcg; |
d21bba2b | 7201 | long nr_pages = folio_nr_pages(new); |
d93c4130 | 7202 | unsigned long flags; |
0a31bc97 | 7203 | |
d21bba2b MWO |
7204 | VM_BUG_ON_FOLIO(!folio_test_locked(old), old); |
7205 | VM_BUG_ON_FOLIO(!folio_test_locked(new), new); | |
7206 | VM_BUG_ON_FOLIO(folio_test_anon(old) != folio_test_anon(new), new); | |
7207 | VM_BUG_ON_FOLIO(folio_nr_pages(old) != nr_pages, new); | |
0a31bc97 JW |
7208 | |
7209 | if (mem_cgroup_disabled()) | |
7210 | return; | |
7211 | ||
d21bba2b MWO |
7212 | /* Page cache replacement: new folio already charged? */ |
7213 | if (folio_memcg(new)) | |
0a31bc97 JW |
7214 | return; |
7215 | ||
d21bba2b MWO |
7216 | memcg = folio_memcg(old); |
7217 | VM_WARN_ON_ONCE_FOLIO(!memcg, old); | |
29833315 | 7218 | if (!memcg) |
0a31bc97 JW |
7219 | return; |
7220 | ||
44b7a8d3 | 7221 | /* Force-charge the new page. The old one will be freed soon */ |
8dc87c7d MS |
7222 | if (!mem_cgroup_is_root(memcg)) { |
7223 | page_counter_charge(&memcg->memory, nr_pages); | |
7224 | if (do_memsw_account()) | |
7225 | page_counter_charge(&memcg->memsw, nr_pages); | |
7226 | } | |
0a31bc97 | 7227 | |
1a3e1f40 | 7228 | css_get(&memcg->css); |
d21bba2b | 7229 | commit_charge(new, memcg); |
44b7a8d3 | 7230 | |
d93c4130 | 7231 | local_irq_save(flags); |
6e0110c2 | 7232 | mem_cgroup_charge_statistics(memcg, nr_pages); |
d21bba2b | 7233 | memcg_check_events(memcg, folio_nid(new)); |
d93c4130 | 7234 | local_irq_restore(flags); |
0a31bc97 JW |
7235 | } |
7236 | ||
ef12947c | 7237 | DEFINE_STATIC_KEY_FALSE(memcg_sockets_enabled_key); |
11092087 JW |
7238 | EXPORT_SYMBOL(memcg_sockets_enabled_key); |
7239 | ||
2d758073 | 7240 | void mem_cgroup_sk_alloc(struct sock *sk) |
11092087 JW |
7241 | { |
7242 | struct mem_cgroup *memcg; | |
7243 | ||
2d758073 JW |
7244 | if (!mem_cgroup_sockets_enabled) |
7245 | return; | |
7246 | ||
e876ecc6 | 7247 | /* Do not associate the sock with unrelated interrupted task's memcg. */ |
086f694a | 7248 | if (!in_task()) |
e876ecc6 SB |
7249 | return; |
7250 | ||
11092087 JW |
7251 | rcu_read_lock(); |
7252 | memcg = mem_cgroup_from_task(current); | |
7848ed62 | 7253 | if (mem_cgroup_is_root(memcg)) |
f7e1cb6e | 7254 | goto out; |
0db15298 | 7255 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && !memcg->tcpmem_active) |
f7e1cb6e | 7256 | goto out; |
8965aa28 | 7257 | if (css_tryget(&memcg->css)) |
11092087 | 7258 | sk->sk_memcg = memcg; |
f7e1cb6e | 7259 | out: |
11092087 JW |
7260 | rcu_read_unlock(); |
7261 | } | |
11092087 | 7262 | |
2d758073 | 7263 | void mem_cgroup_sk_free(struct sock *sk) |
11092087 | 7264 | { |
2d758073 JW |
7265 | if (sk->sk_memcg) |
7266 | css_put(&sk->sk_memcg->css); | |
11092087 JW |
7267 | } |
7268 | ||
7269 | /** | |
7270 | * mem_cgroup_charge_skmem - charge socket memory | |
7271 | * @memcg: memcg to charge | |
7272 | * @nr_pages: number of pages to charge | |
4b1327be | 7273 | * @gfp_mask: reclaim mode |
11092087 JW |
7274 | * |
7275 | * Charges @nr_pages to @memcg. Returns %true if the charge fit within | |
4b1327be | 7276 | * @memcg's configured limit, %false if it doesn't. |
11092087 | 7277 | */ |
4b1327be WW |
7278 | bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages, |
7279 | gfp_t gfp_mask) | |
11092087 | 7280 | { |
f7e1cb6e | 7281 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) { |
0db15298 | 7282 | struct page_counter *fail; |
f7e1cb6e | 7283 | |
0db15298 JW |
7284 | if (page_counter_try_charge(&memcg->tcpmem, nr_pages, &fail)) { |
7285 | memcg->tcpmem_pressure = 0; | |
f7e1cb6e JW |
7286 | return true; |
7287 | } | |
0db15298 | 7288 | memcg->tcpmem_pressure = 1; |
4b1327be WW |
7289 | if (gfp_mask & __GFP_NOFAIL) { |
7290 | page_counter_charge(&memcg->tcpmem, nr_pages); | |
7291 | return true; | |
7292 | } | |
f7e1cb6e | 7293 | return false; |
11092087 | 7294 | } |
d886f4e4 | 7295 | |
4b1327be WW |
7296 | if (try_charge(memcg, gfp_mask, nr_pages) == 0) { |
7297 | mod_memcg_state(memcg, MEMCG_SOCK, nr_pages); | |
f7e1cb6e | 7298 | return true; |
4b1327be | 7299 | } |
f7e1cb6e | 7300 | |
11092087 JW |
7301 | return false; |
7302 | } | |
7303 | ||
7304 | /** | |
7305 | * mem_cgroup_uncharge_skmem - uncharge socket memory | |
b7701a5f MR |
7306 | * @memcg: memcg to uncharge |
7307 | * @nr_pages: number of pages to uncharge | |
11092087 JW |
7308 | */ |
7309 | void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages) | |
7310 | { | |
f7e1cb6e | 7311 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) { |
0db15298 | 7312 | page_counter_uncharge(&memcg->tcpmem, nr_pages); |
f7e1cb6e JW |
7313 | return; |
7314 | } | |
d886f4e4 | 7315 | |
c9019e9b | 7316 | mod_memcg_state(memcg, MEMCG_SOCK, -nr_pages); |
b2807f07 | 7317 | |
475d0487 | 7318 | refill_stock(memcg, nr_pages); |
11092087 JW |
7319 | } |
7320 | ||
f7e1cb6e JW |
7321 | static int __init cgroup_memory(char *s) |
7322 | { | |
7323 | char *token; | |
7324 | ||
7325 | while ((token = strsep(&s, ",")) != NULL) { | |
7326 | if (!*token) | |
7327 | continue; | |
7328 | if (!strcmp(token, "nosocket")) | |
7329 | cgroup_memory_nosocket = true; | |
04823c83 VD |
7330 | if (!strcmp(token, "nokmem")) |
7331 | cgroup_memory_nokmem = true; | |
b6c1a8af YS |
7332 | if (!strcmp(token, "nobpf")) |
7333 | cgroup_memory_nobpf = true; | |
f7e1cb6e | 7334 | } |
460a79e1 | 7335 | return 1; |
f7e1cb6e JW |
7336 | } |
7337 | __setup("cgroup.memory=", cgroup_memory); | |
11092087 | 7338 | |
2d11085e | 7339 | /* |
1081312f MH |
7340 | * subsys_initcall() for memory controller. |
7341 | * | |
308167fc SAS |
7342 | * Some parts like memcg_hotplug_cpu_dead() have to be initialized from this |
7343 | * context because of lock dependencies (cgroup_lock -> cpu hotplug) but | |
7344 | * basically everything that doesn't depend on a specific mem_cgroup structure | |
7345 | * should be initialized from here. | |
2d11085e MH |
7346 | */ |
7347 | static int __init mem_cgroup_init(void) | |
7348 | { | |
95a045f6 JW |
7349 | int cpu, node; |
7350 | ||
f3344adf MS |
7351 | /* |
7352 | * Currently s32 type (can refer to struct batched_lruvec_stat) is | |
7353 | * used for per-memcg-per-cpu caching of per-node statistics. In order | |
7354 | * to work fine, we should make sure that the overfill threshold can't | |
7355 | * exceed S32_MAX / PAGE_SIZE. | |
7356 | */ | |
7357 | BUILD_BUG_ON(MEMCG_CHARGE_BATCH > S32_MAX / PAGE_SIZE); | |
7358 | ||
308167fc SAS |
7359 | cpuhp_setup_state_nocalls(CPUHP_MM_MEMCQ_DEAD, "mm/memctrl:dead", NULL, |
7360 | memcg_hotplug_cpu_dead); | |
95a045f6 JW |
7361 | |
7362 | for_each_possible_cpu(cpu) | |
7363 | INIT_WORK(&per_cpu_ptr(&memcg_stock, cpu)->work, | |
7364 | drain_local_stock); | |
7365 | ||
7366 | for_each_node(node) { | |
7367 | struct mem_cgroup_tree_per_node *rtpn; | |
95a045f6 | 7368 | |
91f0dcce | 7369 | rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, node); |
95a045f6 | 7370 | |
ef8f2327 | 7371 | rtpn->rb_root = RB_ROOT; |
fa90b2fd | 7372 | rtpn->rb_rightmost = NULL; |
ef8f2327 | 7373 | spin_lock_init(&rtpn->lock); |
95a045f6 JW |
7374 | soft_limit_tree.rb_tree_per_node[node] = rtpn; |
7375 | } | |
7376 | ||
2d11085e MH |
7377 | return 0; |
7378 | } | |
7379 | subsys_initcall(mem_cgroup_init); | |
21afa38e | 7380 | |
e55b9f96 | 7381 | #ifdef CONFIG_SWAP |
358c07fc AB |
7382 | static struct mem_cgroup *mem_cgroup_id_get_online(struct mem_cgroup *memcg) |
7383 | { | |
1c2d479a | 7384 | while (!refcount_inc_not_zero(&memcg->id.ref)) { |
358c07fc AB |
7385 | /* |
7386 | * The root cgroup cannot be destroyed, so it's refcount must | |
7387 | * always be >= 1. | |
7388 | */ | |
7848ed62 | 7389 | if (WARN_ON_ONCE(mem_cgroup_is_root(memcg))) { |
358c07fc AB |
7390 | VM_BUG_ON(1); |
7391 | break; | |
7392 | } | |
7393 | memcg = parent_mem_cgroup(memcg); | |
7394 | if (!memcg) | |
7395 | memcg = root_mem_cgroup; | |
7396 | } | |
7397 | return memcg; | |
7398 | } | |
7399 | ||
21afa38e JW |
7400 | /** |
7401 | * mem_cgroup_swapout - transfer a memsw charge to swap | |
3ecb0087 | 7402 | * @folio: folio whose memsw charge to transfer |
21afa38e JW |
7403 | * @entry: swap entry to move the charge to |
7404 | * | |
3ecb0087 | 7405 | * Transfer the memsw charge of @folio to @entry. |
21afa38e | 7406 | */ |
3ecb0087 | 7407 | void mem_cgroup_swapout(struct folio *folio, swp_entry_t entry) |
21afa38e | 7408 | { |
1f47b61f | 7409 | struct mem_cgroup *memcg, *swap_memcg; |
d6810d73 | 7410 | unsigned int nr_entries; |
21afa38e JW |
7411 | unsigned short oldid; |
7412 | ||
3ecb0087 MWO |
7413 | VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); |
7414 | VM_BUG_ON_FOLIO(folio_ref_count(folio), folio); | |
21afa38e | 7415 | |
76358ab5 AS |
7416 | if (mem_cgroup_disabled()) |
7417 | return; | |
7418 | ||
b94c4e94 | 7419 | if (!do_memsw_account()) |
21afa38e JW |
7420 | return; |
7421 | ||
3ecb0087 | 7422 | memcg = folio_memcg(folio); |
21afa38e | 7423 | |
3ecb0087 | 7424 | VM_WARN_ON_ONCE_FOLIO(!memcg, folio); |
21afa38e JW |
7425 | if (!memcg) |
7426 | return; | |
7427 | ||
1f47b61f VD |
7428 | /* |
7429 | * In case the memcg owning these pages has been offlined and doesn't | |
7430 | * have an ID allocated to it anymore, charge the closest online | |
7431 | * ancestor for the swap instead and transfer the memory+swap charge. | |
7432 | */ | |
7433 | swap_memcg = mem_cgroup_id_get_online(memcg); | |
3ecb0087 | 7434 | nr_entries = folio_nr_pages(folio); |
d6810d73 HY |
7435 | /* Get references for the tail pages, too */ |
7436 | if (nr_entries > 1) | |
7437 | mem_cgroup_id_get_many(swap_memcg, nr_entries - 1); | |
7438 | oldid = swap_cgroup_record(entry, mem_cgroup_id(swap_memcg), | |
7439 | nr_entries); | |
3ecb0087 | 7440 | VM_BUG_ON_FOLIO(oldid, folio); |
c9019e9b | 7441 | mod_memcg_state(swap_memcg, MEMCG_SWAP, nr_entries); |
21afa38e | 7442 | |
3ecb0087 | 7443 | folio->memcg_data = 0; |
21afa38e JW |
7444 | |
7445 | if (!mem_cgroup_is_root(memcg)) | |
d6810d73 | 7446 | page_counter_uncharge(&memcg->memory, nr_entries); |
21afa38e | 7447 | |
b25806dc | 7448 | if (memcg != swap_memcg) { |
1f47b61f | 7449 | if (!mem_cgroup_is_root(swap_memcg)) |
d6810d73 HY |
7450 | page_counter_charge(&swap_memcg->memsw, nr_entries); |
7451 | page_counter_uncharge(&memcg->memsw, nr_entries); | |
1f47b61f VD |
7452 | } |
7453 | ||
ce9ce665 SAS |
7454 | /* |
7455 | * Interrupts should be disabled here because the caller holds the | |
b93b0163 | 7456 | * i_pages lock which is taken with interrupts-off. It is |
ce9ce665 | 7457 | * important here to have the interrupts disabled because it is the |
b93b0163 | 7458 | * only synchronisation we have for updating the per-CPU variables. |
ce9ce665 | 7459 | */ |
be3e67b5 | 7460 | memcg_stats_lock(); |
6e0110c2 | 7461 | mem_cgroup_charge_statistics(memcg, -nr_entries); |
be3e67b5 | 7462 | memcg_stats_unlock(); |
3ecb0087 | 7463 | memcg_check_events(memcg, folio_nid(folio)); |
73f576c0 | 7464 | |
1a3e1f40 | 7465 | css_put(&memcg->css); |
21afa38e JW |
7466 | } |
7467 | ||
38d8b4e6 | 7468 | /** |
e2e3fdc7 MWO |
7469 | * __mem_cgroup_try_charge_swap - try charging swap space for a folio |
7470 | * @folio: folio being added to swap | |
37e84351 VD |
7471 | * @entry: swap entry to charge |
7472 | * | |
e2e3fdc7 | 7473 | * Try to charge @folio's memcg for the swap space at @entry. |
37e84351 VD |
7474 | * |
7475 | * Returns 0 on success, -ENOMEM on failure. | |
7476 | */ | |
e2e3fdc7 | 7477 | int __mem_cgroup_try_charge_swap(struct folio *folio, swp_entry_t entry) |
37e84351 | 7478 | { |
e2e3fdc7 | 7479 | unsigned int nr_pages = folio_nr_pages(folio); |
37e84351 | 7480 | struct page_counter *counter; |
38d8b4e6 | 7481 | struct mem_cgroup *memcg; |
37e84351 VD |
7482 | unsigned short oldid; |
7483 | ||
b94c4e94 | 7484 | if (do_memsw_account()) |
37e84351 VD |
7485 | return 0; |
7486 | ||
e2e3fdc7 | 7487 | memcg = folio_memcg(folio); |
37e84351 | 7488 | |
e2e3fdc7 | 7489 | VM_WARN_ON_ONCE_FOLIO(!memcg, folio); |
37e84351 VD |
7490 | if (!memcg) |
7491 | return 0; | |
7492 | ||
f3a53a3a TH |
7493 | if (!entry.val) { |
7494 | memcg_memory_event(memcg, MEMCG_SWAP_FAIL); | |
bb98f2c5 | 7495 | return 0; |
f3a53a3a | 7496 | } |
bb98f2c5 | 7497 | |
1f47b61f VD |
7498 | memcg = mem_cgroup_id_get_online(memcg); |
7499 | ||
b25806dc | 7500 | if (!mem_cgroup_is_root(memcg) && |
38d8b4e6 | 7501 | !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) { |
f3a53a3a TH |
7502 | memcg_memory_event(memcg, MEMCG_SWAP_MAX); |
7503 | memcg_memory_event(memcg, MEMCG_SWAP_FAIL); | |
1f47b61f | 7504 | mem_cgroup_id_put(memcg); |
37e84351 | 7505 | return -ENOMEM; |
1f47b61f | 7506 | } |
37e84351 | 7507 | |
38d8b4e6 HY |
7508 | /* Get references for the tail pages, too */ |
7509 | if (nr_pages > 1) | |
7510 | mem_cgroup_id_get_many(memcg, nr_pages - 1); | |
7511 | oldid = swap_cgroup_record(entry, mem_cgroup_id(memcg), nr_pages); | |
e2e3fdc7 | 7512 | VM_BUG_ON_FOLIO(oldid, folio); |
c9019e9b | 7513 | mod_memcg_state(memcg, MEMCG_SWAP, nr_pages); |
37e84351 | 7514 | |
37e84351 VD |
7515 | return 0; |
7516 | } | |
7517 | ||
21afa38e | 7518 | /** |
01c4b28c | 7519 | * __mem_cgroup_uncharge_swap - uncharge swap space |
21afa38e | 7520 | * @entry: swap entry to uncharge |
38d8b4e6 | 7521 | * @nr_pages: the amount of swap space to uncharge |
21afa38e | 7522 | */ |
01c4b28c | 7523 | void __mem_cgroup_uncharge_swap(swp_entry_t entry, unsigned int nr_pages) |
21afa38e JW |
7524 | { |
7525 | struct mem_cgroup *memcg; | |
7526 | unsigned short id; | |
7527 | ||
c91bdc93 JW |
7528 | if (mem_cgroup_disabled()) |
7529 | return; | |
7530 | ||
38d8b4e6 | 7531 | id = swap_cgroup_record(entry, 0, nr_pages); |
21afa38e | 7532 | rcu_read_lock(); |
adbe427b | 7533 | memcg = mem_cgroup_from_id(id); |
21afa38e | 7534 | if (memcg) { |
b25806dc | 7535 | if (!mem_cgroup_is_root(memcg)) { |
b94c4e94 | 7536 | if (do_memsw_account()) |
38d8b4e6 | 7537 | page_counter_uncharge(&memcg->memsw, nr_pages); |
b94c4e94 JW |
7538 | else |
7539 | page_counter_uncharge(&memcg->swap, nr_pages); | |
37e84351 | 7540 | } |
c9019e9b | 7541 | mod_memcg_state(memcg, MEMCG_SWAP, -nr_pages); |
38d8b4e6 | 7542 | mem_cgroup_id_put_many(memcg, nr_pages); |
21afa38e JW |
7543 | } |
7544 | rcu_read_unlock(); | |
7545 | } | |
7546 | ||
d8b38438 VD |
7547 | long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg) |
7548 | { | |
7549 | long nr_swap_pages = get_nr_swap_pages(); | |
7550 | ||
b25806dc | 7551 | if (mem_cgroup_disabled() || do_memsw_account()) |
d8b38438 | 7552 | return nr_swap_pages; |
7848ed62 | 7553 | for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) |
d8b38438 | 7554 | nr_swap_pages = min_t(long, nr_swap_pages, |
bbec2e15 | 7555 | READ_ONCE(memcg->swap.max) - |
d8b38438 VD |
7556 | page_counter_read(&memcg->swap)); |
7557 | return nr_swap_pages; | |
7558 | } | |
7559 | ||
9202d527 | 7560 | bool mem_cgroup_swap_full(struct folio *folio) |
5ccc5aba VD |
7561 | { |
7562 | struct mem_cgroup *memcg; | |
7563 | ||
9202d527 | 7564 | VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); |
5ccc5aba VD |
7565 | |
7566 | if (vm_swap_full()) | |
7567 | return true; | |
b25806dc | 7568 | if (do_memsw_account()) |
5ccc5aba VD |
7569 | return false; |
7570 | ||
9202d527 | 7571 | memcg = folio_memcg(folio); |
5ccc5aba VD |
7572 | if (!memcg) |
7573 | return false; | |
7574 | ||
7848ed62 | 7575 | for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) { |
4b82ab4f JK |
7576 | unsigned long usage = page_counter_read(&memcg->swap); |
7577 | ||
7578 | if (usage * 2 >= READ_ONCE(memcg->swap.high) || | |
7579 | usage * 2 >= READ_ONCE(memcg->swap.max)) | |
5ccc5aba | 7580 | return true; |
4b82ab4f | 7581 | } |
5ccc5aba VD |
7582 | |
7583 | return false; | |
7584 | } | |
7585 | ||
eccb52e7 | 7586 | static int __init setup_swap_account(char *s) |
21afa38e | 7587 | { |
b25806dc JW |
7588 | pr_warn_once("The swapaccount= commandline option is deprecated. " |
7589 | "Please report your usecase to linux-mm@kvack.org if you " | |
7590 | "depend on this functionality.\n"); | |
21afa38e JW |
7591 | return 1; |
7592 | } | |
eccb52e7 | 7593 | __setup("swapaccount=", setup_swap_account); |
21afa38e | 7594 | |
37e84351 VD |
7595 | static u64 swap_current_read(struct cgroup_subsys_state *css, |
7596 | struct cftype *cft) | |
7597 | { | |
7598 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); | |
7599 | ||
7600 | return (u64)page_counter_read(&memcg->swap) * PAGE_SIZE; | |
7601 | } | |
7602 | ||
e0e0b412 LD |
7603 | static u64 swap_peak_read(struct cgroup_subsys_state *css, |
7604 | struct cftype *cft) | |
7605 | { | |
7606 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); | |
7607 | ||
7608 | return (u64)memcg->swap.watermark * PAGE_SIZE; | |
7609 | } | |
7610 | ||
4b82ab4f JK |
7611 | static int swap_high_show(struct seq_file *m, void *v) |
7612 | { | |
7613 | return seq_puts_memcg_tunable(m, | |
7614 | READ_ONCE(mem_cgroup_from_seq(m)->swap.high)); | |
7615 | } | |
7616 | ||
7617 | static ssize_t swap_high_write(struct kernfs_open_file *of, | |
7618 | char *buf, size_t nbytes, loff_t off) | |
7619 | { | |
7620 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); | |
7621 | unsigned long high; | |
7622 | int err; | |
7623 | ||
7624 | buf = strstrip(buf); | |
7625 | err = page_counter_memparse(buf, "max", &high); | |
7626 | if (err) | |
7627 | return err; | |
7628 | ||
7629 | page_counter_set_high(&memcg->swap, high); | |
7630 | ||
7631 | return nbytes; | |
7632 | } | |
7633 | ||
37e84351 VD |
7634 | static int swap_max_show(struct seq_file *m, void *v) |
7635 | { | |
677dc973 CD |
7636 | return seq_puts_memcg_tunable(m, |
7637 | READ_ONCE(mem_cgroup_from_seq(m)->swap.max)); | |
37e84351 VD |
7638 | } |
7639 | ||
7640 | static ssize_t swap_max_write(struct kernfs_open_file *of, | |
7641 | char *buf, size_t nbytes, loff_t off) | |
7642 | { | |
7643 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); | |
7644 | unsigned long max; | |
7645 | int err; | |
7646 | ||
7647 | buf = strstrip(buf); | |
7648 | err = page_counter_memparse(buf, "max", &max); | |
7649 | if (err) | |
7650 | return err; | |
7651 | ||
be09102b | 7652 | xchg(&memcg->swap.max, max); |
37e84351 VD |
7653 | |
7654 | return nbytes; | |
7655 | } | |
7656 | ||
f3a53a3a TH |
7657 | static int swap_events_show(struct seq_file *m, void *v) |
7658 | { | |
aa9694bb | 7659 | struct mem_cgroup *memcg = mem_cgroup_from_seq(m); |
f3a53a3a | 7660 | |
4b82ab4f JK |
7661 | seq_printf(m, "high %lu\n", |
7662 | atomic_long_read(&memcg->memory_events[MEMCG_SWAP_HIGH])); | |
f3a53a3a TH |
7663 | seq_printf(m, "max %lu\n", |
7664 | atomic_long_read(&memcg->memory_events[MEMCG_SWAP_MAX])); | |
7665 | seq_printf(m, "fail %lu\n", | |
7666 | atomic_long_read(&memcg->memory_events[MEMCG_SWAP_FAIL])); | |
7667 | ||
7668 | return 0; | |
7669 | } | |
7670 | ||
37e84351 VD |
7671 | static struct cftype swap_files[] = { |
7672 | { | |
7673 | .name = "swap.current", | |
7674 | .flags = CFTYPE_NOT_ON_ROOT, | |
7675 | .read_u64 = swap_current_read, | |
7676 | }, | |
4b82ab4f JK |
7677 | { |
7678 | .name = "swap.high", | |
7679 | .flags = CFTYPE_NOT_ON_ROOT, | |
7680 | .seq_show = swap_high_show, | |
7681 | .write = swap_high_write, | |
7682 | }, | |
37e84351 VD |
7683 | { |
7684 | .name = "swap.max", | |
7685 | .flags = CFTYPE_NOT_ON_ROOT, | |
7686 | .seq_show = swap_max_show, | |
7687 | .write = swap_max_write, | |
7688 | }, | |
e0e0b412 LD |
7689 | { |
7690 | .name = "swap.peak", | |
7691 | .flags = CFTYPE_NOT_ON_ROOT, | |
7692 | .read_u64 = swap_peak_read, | |
7693 | }, | |
f3a53a3a TH |
7694 | { |
7695 | .name = "swap.events", | |
7696 | .flags = CFTYPE_NOT_ON_ROOT, | |
7697 | .file_offset = offsetof(struct mem_cgroup, swap_events_file), | |
7698 | .seq_show = swap_events_show, | |
7699 | }, | |
37e84351 VD |
7700 | { } /* terminate */ |
7701 | }; | |
7702 | ||
eccb52e7 | 7703 | static struct cftype memsw_files[] = { |
21afa38e JW |
7704 | { |
7705 | .name = "memsw.usage_in_bytes", | |
7706 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), | |
7707 | .read_u64 = mem_cgroup_read_u64, | |
7708 | }, | |
7709 | { | |
7710 | .name = "memsw.max_usage_in_bytes", | |
7711 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), | |
7712 | .write = mem_cgroup_reset, | |
7713 | .read_u64 = mem_cgroup_read_u64, | |
7714 | }, | |
7715 | { | |
7716 | .name = "memsw.limit_in_bytes", | |
7717 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), | |
7718 | .write = mem_cgroup_write, | |
7719 | .read_u64 = mem_cgroup_read_u64, | |
7720 | }, | |
7721 | { | |
7722 | .name = "memsw.failcnt", | |
7723 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), | |
7724 | .write = mem_cgroup_reset, | |
7725 | .read_u64 = mem_cgroup_read_u64, | |
7726 | }, | |
7727 | { }, /* terminate */ | |
7728 | }; | |
7729 | ||
f4840ccf JW |
7730 | #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) |
7731 | /** | |
7732 | * obj_cgroup_may_zswap - check if this cgroup can zswap | |
7733 | * @objcg: the object cgroup | |
7734 | * | |
7735 | * Check if the hierarchical zswap limit has been reached. | |
7736 | * | |
7737 | * This doesn't check for specific headroom, and it is not atomic | |
7738 | * either. But with zswap, the size of the allocation is only known | |
7739 | * once compression has occured, and this optimistic pre-check avoids | |
7740 | * spending cycles on compression when there is already no room left | |
7741 | * or zswap is disabled altogether somewhere in the hierarchy. | |
7742 | */ | |
7743 | bool obj_cgroup_may_zswap(struct obj_cgroup *objcg) | |
7744 | { | |
7745 | struct mem_cgroup *memcg, *original_memcg; | |
7746 | bool ret = true; | |
7747 | ||
7748 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) | |
7749 | return true; | |
7750 | ||
7751 | original_memcg = get_mem_cgroup_from_objcg(objcg); | |
7848ed62 | 7752 | for (memcg = original_memcg; !mem_cgroup_is_root(memcg); |
f4840ccf JW |
7753 | memcg = parent_mem_cgroup(memcg)) { |
7754 | unsigned long max = READ_ONCE(memcg->zswap_max); | |
7755 | unsigned long pages; | |
7756 | ||
7757 | if (max == PAGE_COUNTER_MAX) | |
7758 | continue; | |
7759 | if (max == 0) { | |
7760 | ret = false; | |
7761 | break; | |
7762 | } | |
7763 | ||
7764 | cgroup_rstat_flush(memcg->css.cgroup); | |
7765 | pages = memcg_page_state(memcg, MEMCG_ZSWAP_B) / PAGE_SIZE; | |
7766 | if (pages < max) | |
7767 | continue; | |
7768 | ret = false; | |
7769 | break; | |
7770 | } | |
7771 | mem_cgroup_put(original_memcg); | |
7772 | return ret; | |
7773 | } | |
7774 | ||
7775 | /** | |
7776 | * obj_cgroup_charge_zswap - charge compression backend memory | |
7777 | * @objcg: the object cgroup | |
7778 | * @size: size of compressed object | |
7779 | * | |
7780 | * This forces the charge after obj_cgroup_may_swap() allowed | |
7781 | * compression and storage in zwap for this cgroup to go ahead. | |
7782 | */ | |
7783 | void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size) | |
7784 | { | |
7785 | struct mem_cgroup *memcg; | |
7786 | ||
7787 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) | |
7788 | return; | |
7789 | ||
7790 | VM_WARN_ON_ONCE(!(current->flags & PF_MEMALLOC)); | |
7791 | ||
7792 | /* PF_MEMALLOC context, charging must succeed */ | |
7793 | if (obj_cgroup_charge(objcg, GFP_KERNEL, size)) | |
7794 | VM_WARN_ON_ONCE(1); | |
7795 | ||
7796 | rcu_read_lock(); | |
7797 | memcg = obj_cgroup_memcg(objcg); | |
7798 | mod_memcg_state(memcg, MEMCG_ZSWAP_B, size); | |
7799 | mod_memcg_state(memcg, MEMCG_ZSWAPPED, 1); | |
7800 | rcu_read_unlock(); | |
7801 | } | |
7802 | ||
7803 | /** | |
7804 | * obj_cgroup_uncharge_zswap - uncharge compression backend memory | |
7805 | * @objcg: the object cgroup | |
7806 | * @size: size of compressed object | |
7807 | * | |
7808 | * Uncharges zswap memory on page in. | |
7809 | */ | |
7810 | void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size) | |
7811 | { | |
7812 | struct mem_cgroup *memcg; | |
7813 | ||
7814 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) | |
7815 | return; | |
7816 | ||
7817 | obj_cgroup_uncharge(objcg, size); | |
7818 | ||
7819 | rcu_read_lock(); | |
7820 | memcg = obj_cgroup_memcg(objcg); | |
7821 | mod_memcg_state(memcg, MEMCG_ZSWAP_B, -size); | |
7822 | mod_memcg_state(memcg, MEMCG_ZSWAPPED, -1); | |
7823 | rcu_read_unlock(); | |
7824 | } | |
7825 | ||
7826 | static u64 zswap_current_read(struct cgroup_subsys_state *css, | |
7827 | struct cftype *cft) | |
7828 | { | |
7829 | cgroup_rstat_flush(css->cgroup); | |
7830 | return memcg_page_state(mem_cgroup_from_css(css), MEMCG_ZSWAP_B); | |
7831 | } | |
7832 | ||
7833 | static int zswap_max_show(struct seq_file *m, void *v) | |
7834 | { | |
7835 | return seq_puts_memcg_tunable(m, | |
7836 | READ_ONCE(mem_cgroup_from_seq(m)->zswap_max)); | |
7837 | } | |
7838 | ||
7839 | static ssize_t zswap_max_write(struct kernfs_open_file *of, | |
7840 | char *buf, size_t nbytes, loff_t off) | |
7841 | { | |
7842 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); | |
7843 | unsigned long max; | |
7844 | int err; | |
7845 | ||
7846 | buf = strstrip(buf); | |
7847 | err = page_counter_memparse(buf, "max", &max); | |
7848 | if (err) | |
7849 | return err; | |
7850 | ||
7851 | xchg(&memcg->zswap_max, max); | |
7852 | ||
7853 | return nbytes; | |
7854 | } | |
7855 | ||
7856 | static struct cftype zswap_files[] = { | |
7857 | { | |
7858 | .name = "zswap.current", | |
7859 | .flags = CFTYPE_NOT_ON_ROOT, | |
7860 | .read_u64 = zswap_current_read, | |
7861 | }, | |
7862 | { | |
7863 | .name = "zswap.max", | |
7864 | .flags = CFTYPE_NOT_ON_ROOT, | |
7865 | .seq_show = zswap_max_show, | |
7866 | .write = zswap_max_write, | |
7867 | }, | |
7868 | { } /* terminate */ | |
7869 | }; | |
7870 | #endif /* CONFIG_MEMCG_KMEM && CONFIG_ZSWAP */ | |
7871 | ||
21afa38e JW |
7872 | static int __init mem_cgroup_swap_init(void) |
7873 | { | |
2d1c4980 | 7874 | if (mem_cgroup_disabled()) |
eccb52e7 JW |
7875 | return 0; |
7876 | ||
7877 | WARN_ON(cgroup_add_dfl_cftypes(&memory_cgrp_subsys, swap_files)); | |
7878 | WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, memsw_files)); | |
f4840ccf JW |
7879 | #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) |
7880 | WARN_ON(cgroup_add_dfl_cftypes(&memory_cgrp_subsys, zswap_files)); | |
7881 | #endif | |
21afa38e JW |
7882 | return 0; |
7883 | } | |
b25806dc | 7884 | subsys_initcall(mem_cgroup_swap_init); |
21afa38e | 7885 | |
e55b9f96 | 7886 | #endif /* CONFIG_SWAP */ |