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