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