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