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