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