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