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