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