1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* memcontrol.c - Memory Controller
4 * Copyright IBM Corporation, 2007
5 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
7 * Copyright 2007 OpenVZ SWsoft Inc
8 * Author: Pavel Emelianov <xemul@openvz.org>
11 * Copyright (C) 2009 Nokia Corporation
12 * Author: Kirill A. Shutemov
14 * Kernel Memory Controller
15 * Copyright (C) 2012 Parallels Inc. and Google Inc.
16 * Authors: Glauber Costa and Suleiman Souhlal
19 * Charge lifetime sanitation
20 * Lockless page tracking & accounting
21 * Unified hierarchy configuration model
22 * Copyright (C) 2015 Red Hat, Inc., Johannes Weiner
24 * Per memcg lru locking
25 * Copyright (C) 2020 Alibaba, Inc, Alex Shi
28 #include <linux/page_counter.h>
29 #include <linux/memcontrol.h>
30 #include <linux/cgroup.h>
31 #include <linux/pagewalk.h>
32 #include <linux/sched/mm.h>
33 #include <linux/shmem_fs.h>
34 #include <linux/hugetlb.h>
35 #include <linux/pagemap.h>
36 #include <linux/vm_event_item.h>
37 #include <linux/smp.h>
38 #include <linux/page-flags.h>
39 #include <linux/backing-dev.h>
40 #include <linux/bit_spinlock.h>
41 #include <linux/rcupdate.h>
42 #include <linux/limits.h>
43 #include <linux/export.h>
44 #include <linux/mutex.h>
45 #include <linux/rbtree.h>
46 #include <linux/slab.h>
47 #include <linux/swap.h>
48 #include <linux/swapops.h>
49 #include <linux/spinlock.h>
50 #include <linux/eventfd.h>
51 #include <linux/poll.h>
52 #include <linux/sort.h>
54 #include <linux/seq_file.h>
55 #include <linux/vmpressure.h>
56 #include <linux/memremap.h>
57 #include <linux/mm_inline.h>
58 #include <linux/swap_cgroup.h>
59 #include <linux/cpu.h>
60 #include <linux/oom.h>
61 #include <linux/lockdep.h>
62 #include <linux/file.h>
63 #include <linux/resume_user_mode.h>
64 #include <linux/psi.h>
65 #include <linux/seq_buf.h>
72 #include <linux/uaccess.h>
74 #include <trace/events/vmscan.h>
76 struct cgroup_subsys memory_cgrp_subsys __read_mostly;
77 EXPORT_SYMBOL(memory_cgrp_subsys);
79 struct mem_cgroup *root_mem_cgroup __read_mostly;
81 /* Active memory cgroup to use from an interrupt context */
82 DEFINE_PER_CPU(struct mem_cgroup *, int_active_memcg);
83 EXPORT_PER_CPU_SYMBOL_GPL(int_active_memcg);
85 /* Socket memory accounting disabled? */
86 static bool cgroup_memory_nosocket __ro_after_init;
88 /* Kernel memory accounting disabled? */
89 static bool cgroup_memory_nokmem __ro_after_init;
91 /* Whether the swap controller is active */
92 #ifdef CONFIG_MEMCG_SWAP
93 static bool cgroup_memory_noswap __ro_after_init;
95 #define cgroup_memory_noswap 1
98 #ifdef CONFIG_CGROUP_WRITEBACK
99 static DECLARE_WAIT_QUEUE_HEAD(memcg_cgwb_frn_waitq);
102 /* Whether legacy memory+swap accounting is active */
103 static bool do_memsw_account(void)
105 return !cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_noswap;
108 #define THRESHOLDS_EVENTS_TARGET 128
109 #define SOFTLIMIT_EVENTS_TARGET 1024
112 * Cgroups above their limits are maintained in a RB-Tree, independent of
113 * their hierarchy representation
116 struct mem_cgroup_tree_per_node {
117 struct rb_root rb_root;
118 struct rb_node *rb_rightmost;
122 struct mem_cgroup_tree {
123 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
126 static struct mem_cgroup_tree soft_limit_tree __read_mostly;
129 struct mem_cgroup_eventfd_list {
130 struct list_head list;
131 struct eventfd_ctx *eventfd;
135 * cgroup_event represents events which userspace want to receive.
137 struct mem_cgroup_event {
139 * memcg which the event belongs to.
141 struct mem_cgroup *memcg;
143 * eventfd to signal userspace about the event.
145 struct eventfd_ctx *eventfd;
147 * Each of these stored in a list by the cgroup.
149 struct list_head list;
151 * register_event() callback will be used to add new userspace
152 * waiter for changes related to this event. Use eventfd_signal()
153 * on eventfd to send notification to userspace.
155 int (*register_event)(struct mem_cgroup *memcg,
156 struct eventfd_ctx *eventfd, const char *args);
158 * unregister_event() callback will be called when userspace closes
159 * the eventfd or on cgroup removing. This callback must be set,
160 * if you want provide notification functionality.
162 void (*unregister_event)(struct mem_cgroup *memcg,
163 struct eventfd_ctx *eventfd);
165 * All fields below needed to unregister event when
166 * userspace closes eventfd.
169 wait_queue_head_t *wqh;
170 wait_queue_entry_t wait;
171 struct work_struct remove;
174 static void mem_cgroup_threshold(struct mem_cgroup *memcg);
175 static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
177 /* Stuffs for move charges at task migration. */
179 * Types of charges to be moved.
181 #define MOVE_ANON 0x1U
182 #define MOVE_FILE 0x2U
183 #define MOVE_MASK (MOVE_ANON | MOVE_FILE)
185 /* "mc" and its members are protected by cgroup_mutex */
186 static struct move_charge_struct {
187 spinlock_t lock; /* for from, to */
188 struct mm_struct *mm;
189 struct mem_cgroup *from;
190 struct mem_cgroup *to;
192 unsigned long precharge;
193 unsigned long moved_charge;
194 unsigned long moved_swap;
195 struct task_struct *moving_task; /* a task moving charges */
196 wait_queue_head_t waitq; /* a waitq for other context */
198 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
199 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
203 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
204 * limit reclaim to prevent infinite loops, if they ever occur.
206 #define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
207 #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
209 /* for encoding cft->private value on file */
217 #define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
218 #define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
219 #define MEMFILE_ATTR(val) ((val) & 0xffff)
222 * Iteration constructs for visiting all cgroups (under a tree). If
223 * loops are exited prematurely (break), mem_cgroup_iter_break() must
224 * be used for reference counting.
226 #define for_each_mem_cgroup_tree(iter, root) \
227 for (iter = mem_cgroup_iter(root, NULL, NULL); \
229 iter = mem_cgroup_iter(root, iter, NULL))
231 #define for_each_mem_cgroup(iter) \
232 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
234 iter = mem_cgroup_iter(NULL, iter, NULL))
236 static inline bool task_is_dying(void)
238 return tsk_is_oom_victim(current) || fatal_signal_pending(current) ||
239 (current->flags & PF_EXITING);
242 /* Some nice accessors for the vmpressure. */
243 struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg)
246 memcg = root_mem_cgroup;
247 return &memcg->vmpressure;
250 struct mem_cgroup *vmpressure_to_memcg(struct vmpressure *vmpr)
252 return container_of(vmpr, struct mem_cgroup, vmpressure);
255 #ifdef CONFIG_MEMCG_KMEM
256 static DEFINE_SPINLOCK(objcg_lock);
258 bool mem_cgroup_kmem_disabled(void)
260 return cgroup_memory_nokmem;
263 static void obj_cgroup_uncharge_pages(struct obj_cgroup *objcg,
264 unsigned int nr_pages);
266 static void obj_cgroup_release(struct percpu_ref *ref)
268 struct obj_cgroup *objcg = container_of(ref, struct obj_cgroup, refcnt);
269 unsigned int nr_bytes;
270 unsigned int nr_pages;
274 * At this point all allocated objects are freed, and
275 * objcg->nr_charged_bytes can't have an arbitrary byte value.
276 * However, it can be PAGE_SIZE or (x * PAGE_SIZE).
278 * The following sequence can lead to it:
279 * 1) CPU0: objcg == stock->cached_objcg
280 * 2) CPU1: we do a small allocation (e.g. 92 bytes),
281 * PAGE_SIZE bytes are charged
282 * 3) CPU1: a process from another memcg is allocating something,
283 * the stock if flushed,
284 * objcg->nr_charged_bytes = PAGE_SIZE - 92
285 * 5) CPU0: we do release this object,
286 * 92 bytes are added to stock->nr_bytes
287 * 6) CPU0: stock is flushed,
288 * 92 bytes are added to objcg->nr_charged_bytes
290 * In the result, nr_charged_bytes == PAGE_SIZE.
291 * This page will be uncharged in obj_cgroup_release().
293 nr_bytes = atomic_read(&objcg->nr_charged_bytes);
294 WARN_ON_ONCE(nr_bytes & (PAGE_SIZE - 1));
295 nr_pages = nr_bytes >> PAGE_SHIFT;
298 obj_cgroup_uncharge_pages(objcg, nr_pages);
300 spin_lock_irqsave(&objcg_lock, flags);
301 list_del(&objcg->list);
302 spin_unlock_irqrestore(&objcg_lock, flags);
304 percpu_ref_exit(ref);
305 kfree_rcu(objcg, rcu);
308 static struct obj_cgroup *obj_cgroup_alloc(void)
310 struct obj_cgroup *objcg;
313 objcg = kzalloc(sizeof(struct obj_cgroup), GFP_KERNEL);
317 ret = percpu_ref_init(&objcg->refcnt, obj_cgroup_release, 0,
323 INIT_LIST_HEAD(&objcg->list);
327 static void memcg_reparent_objcgs(struct mem_cgroup *memcg,
328 struct mem_cgroup *parent)
330 struct obj_cgroup *objcg, *iter;
332 objcg = rcu_replace_pointer(memcg->objcg, NULL, true);
334 spin_lock_irq(&objcg_lock);
336 /* 1) Ready to reparent active objcg. */
337 list_add(&objcg->list, &memcg->objcg_list);
338 /* 2) Reparent active objcg and already reparented objcgs to parent. */
339 list_for_each_entry(iter, &memcg->objcg_list, list)
340 WRITE_ONCE(iter->memcg, parent);
341 /* 3) Move already reparented objcgs to the parent's list */
342 list_splice(&memcg->objcg_list, &parent->objcg_list);
344 spin_unlock_irq(&objcg_lock);
346 percpu_ref_kill(&objcg->refcnt);
350 * A lot of the calls to the cache allocation functions are expected to be
351 * inlined by the compiler. Since the calls to memcg_slab_pre_alloc_hook() are
352 * conditional to this static branch, we'll have to allow modules that does
353 * kmem_cache_alloc and the such to see this symbol as well
355 DEFINE_STATIC_KEY_FALSE(memcg_kmem_enabled_key);
356 EXPORT_SYMBOL(memcg_kmem_enabled_key);
360 * mem_cgroup_css_from_page - css of the memcg associated with a page
361 * @page: page of interest
363 * If memcg is bound to the default hierarchy, css of the memcg associated
364 * with @page is returned. The returned css remains associated with @page
365 * until it is released.
367 * If memcg is bound to a traditional hierarchy, the css of root_mem_cgroup
370 struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page)
372 struct mem_cgroup *memcg;
374 memcg = page_memcg(page);
376 if (!memcg || !cgroup_subsys_on_dfl(memory_cgrp_subsys))
377 memcg = root_mem_cgroup;
383 * page_cgroup_ino - return inode number of the memcg a page is charged to
386 * Look up the closest online ancestor of the memory cgroup @page is charged to
387 * and return its inode number or 0 if @page is not charged to any cgroup. It
388 * is safe to call this function without holding a reference to @page.
390 * Note, this function is inherently racy, because there is nothing to prevent
391 * the cgroup inode from getting torn down and potentially reallocated a moment
392 * after page_cgroup_ino() returns, so it only should be used by callers that
393 * do not care (such as procfs interfaces).
395 ino_t page_cgroup_ino(struct page *page)
397 struct mem_cgroup *memcg;
398 unsigned long ino = 0;
401 memcg = page_memcg_check(page);
403 while (memcg && !(memcg->css.flags & CSS_ONLINE))
404 memcg = parent_mem_cgroup(memcg);
406 ino = cgroup_ino(memcg->css.cgroup);
411 static void __mem_cgroup_insert_exceeded(struct mem_cgroup_per_node *mz,
412 struct mem_cgroup_tree_per_node *mctz,
413 unsigned long new_usage_in_excess)
415 struct rb_node **p = &mctz->rb_root.rb_node;
416 struct rb_node *parent = NULL;
417 struct mem_cgroup_per_node *mz_node;
418 bool rightmost = true;
423 mz->usage_in_excess = new_usage_in_excess;
424 if (!mz->usage_in_excess)
428 mz_node = rb_entry(parent, struct mem_cgroup_per_node,
430 if (mz->usage_in_excess < mz_node->usage_in_excess) {
439 mctz->rb_rightmost = &mz->tree_node;
441 rb_link_node(&mz->tree_node, parent, p);
442 rb_insert_color(&mz->tree_node, &mctz->rb_root);
446 static void __mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz,
447 struct mem_cgroup_tree_per_node *mctz)
452 if (&mz->tree_node == mctz->rb_rightmost)
453 mctz->rb_rightmost = rb_prev(&mz->tree_node);
455 rb_erase(&mz->tree_node, &mctz->rb_root);
459 static void mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz,
460 struct mem_cgroup_tree_per_node *mctz)
464 spin_lock_irqsave(&mctz->lock, flags);
465 __mem_cgroup_remove_exceeded(mz, mctz);
466 spin_unlock_irqrestore(&mctz->lock, flags);
469 static unsigned long soft_limit_excess(struct mem_cgroup *memcg)
471 unsigned long nr_pages = page_counter_read(&memcg->memory);
472 unsigned long soft_limit = READ_ONCE(memcg->soft_limit);
473 unsigned long excess = 0;
475 if (nr_pages > soft_limit)
476 excess = nr_pages - soft_limit;
481 static void mem_cgroup_update_tree(struct mem_cgroup *memcg, int nid)
483 unsigned long excess;
484 struct mem_cgroup_per_node *mz;
485 struct mem_cgroup_tree_per_node *mctz;
487 mctz = soft_limit_tree.rb_tree_per_node[nid];
491 * Necessary to update all ancestors when hierarchy is used.
492 * because their event counter is not touched.
494 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
495 mz = memcg->nodeinfo[nid];
496 excess = soft_limit_excess(memcg);
498 * We have to update the tree if mz is on RB-tree or
499 * mem is over its softlimit.
501 if (excess || mz->on_tree) {
504 spin_lock_irqsave(&mctz->lock, flags);
505 /* if on-tree, remove it */
507 __mem_cgroup_remove_exceeded(mz, mctz);
509 * Insert again. mz->usage_in_excess will be updated.
510 * If excess is 0, no tree ops.
512 __mem_cgroup_insert_exceeded(mz, mctz, excess);
513 spin_unlock_irqrestore(&mctz->lock, flags);
518 static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
520 struct mem_cgroup_tree_per_node *mctz;
521 struct mem_cgroup_per_node *mz;
525 mz = memcg->nodeinfo[nid];
526 mctz = soft_limit_tree.rb_tree_per_node[nid];
528 mem_cgroup_remove_exceeded(mz, mctz);
532 static struct mem_cgroup_per_node *
533 __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz)
535 struct mem_cgroup_per_node *mz;
539 if (!mctz->rb_rightmost)
540 goto done; /* Nothing to reclaim from */
542 mz = rb_entry(mctz->rb_rightmost,
543 struct mem_cgroup_per_node, tree_node);
545 * Remove the node now but someone else can add it back,
546 * we will to add it back at the end of reclaim to its correct
547 * position in the tree.
549 __mem_cgroup_remove_exceeded(mz, mctz);
550 if (!soft_limit_excess(mz->memcg) ||
551 !css_tryget(&mz->memcg->css))
557 static struct mem_cgroup_per_node *
558 mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz)
560 struct mem_cgroup_per_node *mz;
562 spin_lock_irq(&mctz->lock);
563 mz = __mem_cgroup_largest_soft_limit_node(mctz);
564 spin_unlock_irq(&mctz->lock);
569 * memcg and lruvec stats flushing
571 * Many codepaths leading to stats update or read are performance sensitive and
572 * adding stats flushing in such codepaths is not desirable. So, to optimize the
573 * flushing the kernel does:
575 * 1) Periodically and asynchronously flush the stats every 2 seconds to not let
576 * rstat update tree grow unbounded.
578 * 2) Flush the stats synchronously on reader side only when there are more than
579 * (MEMCG_CHARGE_BATCH * nr_cpus) update events. Though this optimization
580 * will let stats be out of sync by atmost (MEMCG_CHARGE_BATCH * nr_cpus) but
581 * only for 2 seconds due to (1).
583 static void flush_memcg_stats_dwork(struct work_struct *w);
584 static DECLARE_DEFERRABLE_WORK(stats_flush_dwork, flush_memcg_stats_dwork);
585 static DEFINE_SPINLOCK(stats_flush_lock);
586 static DEFINE_PER_CPU(unsigned int, stats_updates);
587 static atomic_t stats_flush_threshold = ATOMIC_INIT(0);
588 static u64 flush_next_time;
590 #define FLUSH_TIME (2UL*HZ)
593 * Accessors to ensure that preemption is disabled on PREEMPT_RT because it can
594 * not rely on this as part of an acquired spinlock_t lock. These functions are
595 * never used in hardirq context on PREEMPT_RT and therefore disabling preemtion
598 static void memcg_stats_lock(void)
600 #ifdef CONFIG_PREEMPT_RT
603 VM_BUG_ON(!irqs_disabled());
607 static void __memcg_stats_lock(void)
609 #ifdef CONFIG_PREEMPT_RT
614 static void memcg_stats_unlock(void)
616 #ifdef CONFIG_PREEMPT_RT
621 static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val)
625 cgroup_rstat_updated(memcg->css.cgroup, smp_processor_id());
627 x = __this_cpu_add_return(stats_updates, abs(val));
628 if (x > MEMCG_CHARGE_BATCH) {
630 * If stats_flush_threshold exceeds the threshold
631 * (>num_online_cpus()), cgroup stats update will be triggered
632 * in __mem_cgroup_flush_stats(). Increasing this var further
633 * is redundant and simply adds overhead in atomic update.
635 if (atomic_read(&stats_flush_threshold) <= num_online_cpus())
636 atomic_add(x / MEMCG_CHARGE_BATCH, &stats_flush_threshold);
637 __this_cpu_write(stats_updates, 0);
641 static void __mem_cgroup_flush_stats(void)
645 if (!spin_trylock_irqsave(&stats_flush_lock, flag))
648 flush_next_time = jiffies_64 + 2*FLUSH_TIME;
649 cgroup_rstat_flush_irqsafe(root_mem_cgroup->css.cgroup);
650 atomic_set(&stats_flush_threshold, 0);
651 spin_unlock_irqrestore(&stats_flush_lock, flag);
654 void mem_cgroup_flush_stats(void)
656 if (atomic_read(&stats_flush_threshold) > num_online_cpus())
657 __mem_cgroup_flush_stats();
660 void mem_cgroup_flush_stats_delayed(void)
662 if (time_after64(jiffies_64, flush_next_time))
663 mem_cgroup_flush_stats();
666 static void flush_memcg_stats_dwork(struct work_struct *w)
668 __mem_cgroup_flush_stats();
669 queue_delayed_work(system_unbound_wq, &stats_flush_dwork, FLUSH_TIME);
673 * __mod_memcg_state - update cgroup memory statistics
674 * @memcg: the memory cgroup
675 * @idx: the stat item - can be enum memcg_stat_item or enum node_stat_item
676 * @val: delta to add to the counter, can be negative
678 void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val)
680 if (mem_cgroup_disabled())
683 __this_cpu_add(memcg->vmstats_percpu->state[idx], val);
684 memcg_rstat_updated(memcg, val);
687 /* idx can be of type enum memcg_stat_item or node_stat_item. */
688 static unsigned long memcg_page_state_local(struct mem_cgroup *memcg, int idx)
693 for_each_possible_cpu(cpu)
694 x += per_cpu(memcg->vmstats_percpu->state[idx], cpu);
702 void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
705 struct mem_cgroup_per_node *pn;
706 struct mem_cgroup *memcg;
708 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
712 * The caller from rmap relay on disabled preemption becase they never
713 * update their counter from in-interrupt context. For these two
714 * counters we check that the update is never performed from an
715 * interrupt context while other caller need to have disabled interrupt.
717 __memcg_stats_lock();
718 if (IS_ENABLED(CONFIG_DEBUG_VM) && !IS_ENABLED(CONFIG_PREEMPT_RT)) {
723 case NR_SHMEM_PMDMAPPED:
724 case NR_FILE_PMDMAPPED:
725 WARN_ON_ONCE(!in_task());
728 WARN_ON_ONCE(!irqs_disabled());
733 __this_cpu_add(memcg->vmstats_percpu->state[idx], val);
736 __this_cpu_add(pn->lruvec_stats_percpu->state[idx], val);
738 memcg_rstat_updated(memcg, val);
739 memcg_stats_unlock();
743 * __mod_lruvec_state - update lruvec memory statistics
744 * @lruvec: the lruvec
745 * @idx: the stat item
746 * @val: delta to add to the counter, can be negative
748 * The lruvec is the intersection of the NUMA node and a cgroup. This
749 * function updates the all three counters that are affected by a
750 * change of state at this level: per-node, per-cgroup, per-lruvec.
752 void __mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
756 __mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
758 /* Update memcg and lruvec */
759 if (!mem_cgroup_disabled())
760 __mod_memcg_lruvec_state(lruvec, idx, val);
763 void __mod_lruvec_page_state(struct page *page, enum node_stat_item idx,
766 struct page *head = compound_head(page); /* rmap on tail pages */
767 struct mem_cgroup *memcg;
768 pg_data_t *pgdat = page_pgdat(page);
769 struct lruvec *lruvec;
772 memcg = page_memcg(head);
773 /* Untracked pages have no memcg, no lruvec. Update only the node */
776 __mod_node_page_state(pgdat, idx, val);
780 lruvec = mem_cgroup_lruvec(memcg, pgdat);
781 __mod_lruvec_state(lruvec, idx, val);
784 EXPORT_SYMBOL(__mod_lruvec_page_state);
786 void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val)
788 pg_data_t *pgdat = page_pgdat(virt_to_page(p));
789 struct mem_cgroup *memcg;
790 struct lruvec *lruvec;
793 memcg = mem_cgroup_from_slab_obj(p);
796 * Untracked pages have no memcg, no lruvec. Update only the
797 * node. If we reparent the slab objects to the root memcg,
798 * when we free the slab object, we need to update the per-memcg
799 * vmstats to keep it correct for the root memcg.
802 __mod_node_page_state(pgdat, idx, val);
804 lruvec = mem_cgroup_lruvec(memcg, pgdat);
805 __mod_lruvec_state(lruvec, idx, val);
811 * __count_memcg_events - account VM events in a cgroup
812 * @memcg: the memory cgroup
813 * @idx: the event item
814 * @count: the number of events that occurred
816 void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
819 if (mem_cgroup_disabled())
823 __this_cpu_add(memcg->vmstats_percpu->events[idx], count);
824 memcg_rstat_updated(memcg, count);
825 memcg_stats_unlock();
828 static unsigned long memcg_events(struct mem_cgroup *memcg, int event)
830 return READ_ONCE(memcg->vmstats.events[event]);
833 static unsigned long memcg_events_local(struct mem_cgroup *memcg, int event)
838 for_each_possible_cpu(cpu)
839 x += per_cpu(memcg->vmstats_percpu->events[event], cpu);
843 static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
846 /* pagein of a big page is an event. So, ignore page size */
848 __count_memcg_events(memcg, PGPGIN, 1);
850 __count_memcg_events(memcg, PGPGOUT, 1);
851 nr_pages = -nr_pages; /* for event */
854 __this_cpu_add(memcg->vmstats_percpu->nr_page_events, nr_pages);
857 static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
858 enum mem_cgroup_events_target target)
860 unsigned long val, next;
862 val = __this_cpu_read(memcg->vmstats_percpu->nr_page_events);
863 next = __this_cpu_read(memcg->vmstats_percpu->targets[target]);
864 /* from time_after() in jiffies.h */
865 if ((long)(next - val) < 0) {
867 case MEM_CGROUP_TARGET_THRESH:
868 next = val + THRESHOLDS_EVENTS_TARGET;
870 case MEM_CGROUP_TARGET_SOFTLIMIT:
871 next = val + SOFTLIMIT_EVENTS_TARGET;
876 __this_cpu_write(memcg->vmstats_percpu->targets[target], next);
883 * Check events in order.
886 static void memcg_check_events(struct mem_cgroup *memcg, int nid)
888 if (IS_ENABLED(CONFIG_PREEMPT_RT))
891 /* threshold event is triggered in finer grain than soft limit */
892 if (unlikely(mem_cgroup_event_ratelimit(memcg,
893 MEM_CGROUP_TARGET_THRESH))) {
896 do_softlimit = mem_cgroup_event_ratelimit(memcg,
897 MEM_CGROUP_TARGET_SOFTLIMIT);
898 mem_cgroup_threshold(memcg);
899 if (unlikely(do_softlimit))
900 mem_cgroup_update_tree(memcg, nid);
904 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
907 * mm_update_next_owner() may clear mm->owner to NULL
908 * if it races with swapoff, page migration, etc.
909 * So this can be called with p == NULL.
914 return mem_cgroup_from_css(task_css(p, memory_cgrp_id));
916 EXPORT_SYMBOL(mem_cgroup_from_task);
918 static __always_inline struct mem_cgroup *active_memcg(void)
921 return this_cpu_read(int_active_memcg);
923 return current->active_memcg;
927 * get_mem_cgroup_from_mm: Obtain a reference on given mm_struct's memcg.
928 * @mm: mm from which memcg should be extracted. It can be NULL.
930 * Obtain a reference on mm->memcg and returns it if successful. If mm
931 * is NULL, then the memcg is chosen as follows:
932 * 1) The active memcg, if set.
933 * 2) current->mm->memcg, if available
935 * If mem_cgroup is disabled, NULL is returned.
937 struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
939 struct mem_cgroup *memcg;
941 if (mem_cgroup_disabled())
945 * Page cache insertions can happen without an
946 * actual mm context, e.g. during disk probing
947 * on boot, loopback IO, acct() writes etc.
949 * No need to css_get on root memcg as the reference
950 * counting is disabled on the root level in the
951 * cgroup core. See CSS_NO_REF.
954 memcg = active_memcg();
955 if (unlikely(memcg)) {
956 /* remote memcg must hold a ref */
957 css_get(&memcg->css);
962 return root_mem_cgroup;
967 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
968 if (unlikely(!memcg))
969 memcg = root_mem_cgroup;
970 } while (!css_tryget(&memcg->css));
974 EXPORT_SYMBOL(get_mem_cgroup_from_mm);
976 static __always_inline bool memcg_kmem_bypass(void)
978 /* Allow remote memcg charging from any context. */
979 if (unlikely(active_memcg()))
982 /* Memcg to charge can't be determined. */
983 if (!in_task() || !current->mm || (current->flags & PF_KTHREAD))
990 * mem_cgroup_iter - iterate over memory cgroup hierarchy
991 * @root: hierarchy root
992 * @prev: previously returned memcg, NULL on first invocation
993 * @reclaim: cookie for shared reclaim walks, NULL for full walks
995 * Returns references to children of the hierarchy below @root, or
996 * @root itself, or %NULL after a full round-trip.
998 * Caller must pass the return value in @prev on subsequent
999 * invocations for reference counting, or use mem_cgroup_iter_break()
1000 * to cancel a hierarchy walk before the round-trip is complete.
1002 * Reclaimers can specify a node in @reclaim to divide up the memcgs
1003 * in the hierarchy among all concurrent reclaimers operating on the
1006 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
1007 struct mem_cgroup *prev,
1008 struct mem_cgroup_reclaim_cookie *reclaim)
1010 struct mem_cgroup_reclaim_iter *iter;
1011 struct cgroup_subsys_state *css = NULL;
1012 struct mem_cgroup *memcg = NULL;
1013 struct mem_cgroup *pos = NULL;
1015 if (mem_cgroup_disabled())
1019 root = root_mem_cgroup;
1024 struct mem_cgroup_per_node *mz;
1026 mz = root->nodeinfo[reclaim->pgdat->node_id];
1030 * On start, join the current reclaim iteration cycle.
1031 * Exit when a concurrent walker completes it.
1034 reclaim->generation = iter->generation;
1035 else if (reclaim->generation != iter->generation)
1039 pos = READ_ONCE(iter->position);
1040 if (!pos || css_tryget(&pos->css))
1043 * css reference reached zero, so iter->position will
1044 * be cleared by ->css_released. However, we should not
1045 * rely on this happening soon, because ->css_released
1046 * is called from a work queue, and by busy-waiting we
1047 * might block it. So we clear iter->position right
1050 (void)cmpxchg(&iter->position, pos, NULL);
1060 css = css_next_descendant_pre(css, &root->css);
1063 * Reclaimers share the hierarchy walk, and a
1064 * new one might jump in right at the end of
1065 * the hierarchy - make sure they see at least
1066 * one group and restart from the beginning.
1074 * Verify the css and acquire a reference. The root
1075 * is provided by the caller, so we know it's alive
1076 * and kicking, and don't take an extra reference.
1078 if (css == &root->css || css_tryget(css)) {
1079 memcg = mem_cgroup_from_css(css);
1086 * The position could have already been updated by a competing
1087 * thread, so check that the value hasn't changed since we read
1088 * it to avoid reclaiming from the same cgroup twice.
1090 (void)cmpxchg(&iter->position, pos, memcg);
1101 if (prev && prev != root)
1102 css_put(&prev->css);
1108 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1109 * @root: hierarchy root
1110 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1112 void mem_cgroup_iter_break(struct mem_cgroup *root,
1113 struct mem_cgroup *prev)
1116 root = root_mem_cgroup;
1117 if (prev && prev != root)
1118 css_put(&prev->css);
1121 static void __invalidate_reclaim_iterators(struct mem_cgroup *from,
1122 struct mem_cgroup *dead_memcg)
1124 struct mem_cgroup_reclaim_iter *iter;
1125 struct mem_cgroup_per_node *mz;
1128 for_each_node(nid) {
1129 mz = from->nodeinfo[nid];
1131 cmpxchg(&iter->position, dead_memcg, NULL);
1135 static void invalidate_reclaim_iterators(struct mem_cgroup *dead_memcg)
1137 struct mem_cgroup *memcg = dead_memcg;
1138 struct mem_cgroup *last;
1141 __invalidate_reclaim_iterators(memcg, dead_memcg);
1143 } while ((memcg = parent_mem_cgroup(memcg)));
1146 * When cgruop1 non-hierarchy mode is used,
1147 * parent_mem_cgroup() does not walk all the way up to the
1148 * cgroup root (root_mem_cgroup). So we have to handle
1149 * dead_memcg from cgroup root separately.
1151 if (last != root_mem_cgroup)
1152 __invalidate_reclaim_iterators(root_mem_cgroup,
1157 * mem_cgroup_scan_tasks - iterate over tasks of a memory cgroup hierarchy
1158 * @memcg: hierarchy root
1159 * @fn: function to call for each task
1160 * @arg: argument passed to @fn
1162 * This function iterates over tasks attached to @memcg or to any of its
1163 * descendants and calls @fn for each task. If @fn returns a non-zero
1164 * value, the function breaks the iteration loop and returns the value.
1165 * Otherwise, it will iterate over all tasks and return 0.
1167 * This function must not be called for the root memory cgroup.
1169 int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
1170 int (*fn)(struct task_struct *, void *), void *arg)
1172 struct mem_cgroup *iter;
1175 BUG_ON(memcg == root_mem_cgroup);
1177 for_each_mem_cgroup_tree(iter, memcg) {
1178 struct css_task_iter it;
1179 struct task_struct *task;
1181 css_task_iter_start(&iter->css, CSS_TASK_ITER_PROCS, &it);
1182 while (!ret && (task = css_task_iter_next(&it)))
1183 ret = fn(task, arg);
1184 css_task_iter_end(&it);
1186 mem_cgroup_iter_break(memcg, iter);
1193 #ifdef CONFIG_DEBUG_VM
1194 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio)
1196 struct mem_cgroup *memcg;
1198 if (mem_cgroup_disabled())
1201 memcg = folio_memcg(folio);
1204 VM_BUG_ON_FOLIO(lruvec_memcg(lruvec) != root_mem_cgroup, folio);
1206 VM_BUG_ON_FOLIO(lruvec_memcg(lruvec) != memcg, folio);
1211 * folio_lruvec_lock - Lock the lruvec for a folio.
1212 * @folio: Pointer to the folio.
1214 * These functions are safe to use under any of the following conditions:
1216 * - folio_test_lru false
1217 * - folio_memcg_lock()
1218 * - folio frozen (refcount of 0)
1220 * Return: The lruvec this folio is on with its lock held.
1222 struct lruvec *folio_lruvec_lock(struct folio *folio)
1224 struct lruvec *lruvec = folio_lruvec(folio);
1226 spin_lock(&lruvec->lru_lock);
1227 lruvec_memcg_debug(lruvec, folio);
1233 * folio_lruvec_lock_irq - Lock the lruvec for a folio.
1234 * @folio: Pointer to the folio.
1236 * These functions are safe to use under any of the following conditions:
1238 * - folio_test_lru false
1239 * - folio_memcg_lock()
1240 * - folio frozen (refcount of 0)
1242 * Return: The lruvec this folio is on with its lock held and interrupts
1245 struct lruvec *folio_lruvec_lock_irq(struct folio *folio)
1247 struct lruvec *lruvec = folio_lruvec(folio);
1249 spin_lock_irq(&lruvec->lru_lock);
1250 lruvec_memcg_debug(lruvec, folio);
1256 * folio_lruvec_lock_irqsave - Lock the lruvec for a folio.
1257 * @folio: Pointer to the folio.
1258 * @flags: Pointer to irqsave flags.
1260 * These functions are safe to use under any of the following conditions:
1262 * - folio_test_lru false
1263 * - folio_memcg_lock()
1264 * - folio frozen (refcount of 0)
1266 * Return: The lruvec this folio is on with its lock held and interrupts
1269 struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio,
1270 unsigned long *flags)
1272 struct lruvec *lruvec = folio_lruvec(folio);
1274 spin_lock_irqsave(&lruvec->lru_lock, *flags);
1275 lruvec_memcg_debug(lruvec, folio);
1281 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1282 * @lruvec: mem_cgroup per zone lru vector
1283 * @lru: index of lru list the page is sitting on
1284 * @zid: zone id of the accounted pages
1285 * @nr_pages: positive when adding or negative when removing
1287 * This function must be called under lru_lock, just before a page is added
1288 * to or just after a page is removed from an lru list.
1290 void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1291 int zid, int nr_pages)
1293 struct mem_cgroup_per_node *mz;
1294 unsigned long *lru_size;
1297 if (mem_cgroup_disabled())
1300 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
1301 lru_size = &mz->lru_zone_size[zid][lru];
1304 *lru_size += nr_pages;
1307 if (WARN_ONCE(size < 0,
1308 "%s(%p, %d, %d): lru_size %ld\n",
1309 __func__, lruvec, lru, nr_pages, size)) {
1315 *lru_size += nr_pages;
1319 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
1320 * @memcg: the memory cgroup
1322 * Returns the maximum amount of memory @mem can be charged with, in
1325 static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
1327 unsigned long margin = 0;
1328 unsigned long count;
1329 unsigned long limit;
1331 count = page_counter_read(&memcg->memory);
1332 limit = READ_ONCE(memcg->memory.max);
1334 margin = limit - count;
1336 if (do_memsw_account()) {
1337 count = page_counter_read(&memcg->memsw);
1338 limit = READ_ONCE(memcg->memsw.max);
1340 margin = min(margin, limit - count);
1349 * A routine for checking "mem" is under move_account() or not.
1351 * Checking a cgroup is mc.from or mc.to or under hierarchy of
1352 * moving cgroups. This is for waiting at high-memory pressure
1355 static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
1357 struct mem_cgroup *from;
1358 struct mem_cgroup *to;
1361 * Unlike task_move routines, we access mc.to, mc.from not under
1362 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1364 spin_lock(&mc.lock);
1370 ret = mem_cgroup_is_descendant(from, memcg) ||
1371 mem_cgroup_is_descendant(to, memcg);
1373 spin_unlock(&mc.lock);
1377 static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
1379 if (mc.moving_task && current != mc.moving_task) {
1380 if (mem_cgroup_under_move(memcg)) {
1382 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1383 /* moving charge context might have finished. */
1386 finish_wait(&mc.waitq, &wait);
1393 struct memory_stat {
1398 static const struct memory_stat memory_stats[] = {
1399 { "anon", NR_ANON_MAPPED },
1400 { "file", NR_FILE_PAGES },
1401 { "kernel", MEMCG_KMEM },
1402 { "kernel_stack", NR_KERNEL_STACK_KB },
1403 { "pagetables", NR_PAGETABLE },
1404 { "percpu", MEMCG_PERCPU_B },
1405 { "sock", MEMCG_SOCK },
1406 { "vmalloc", MEMCG_VMALLOC },
1407 { "shmem", NR_SHMEM },
1408 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
1409 { "zswap", MEMCG_ZSWAP_B },
1410 { "zswapped", MEMCG_ZSWAPPED },
1412 { "file_mapped", NR_FILE_MAPPED },
1413 { "file_dirty", NR_FILE_DIRTY },
1414 { "file_writeback", NR_WRITEBACK },
1416 { "swapcached", NR_SWAPCACHE },
1418 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1419 { "anon_thp", NR_ANON_THPS },
1420 { "file_thp", NR_FILE_THPS },
1421 { "shmem_thp", NR_SHMEM_THPS },
1423 { "inactive_anon", NR_INACTIVE_ANON },
1424 { "active_anon", NR_ACTIVE_ANON },
1425 { "inactive_file", NR_INACTIVE_FILE },
1426 { "active_file", NR_ACTIVE_FILE },
1427 { "unevictable", NR_UNEVICTABLE },
1428 { "slab_reclaimable", NR_SLAB_RECLAIMABLE_B },
1429 { "slab_unreclaimable", NR_SLAB_UNRECLAIMABLE_B },
1431 /* The memory events */
1432 { "workingset_refault_anon", WORKINGSET_REFAULT_ANON },
1433 { "workingset_refault_file", WORKINGSET_REFAULT_FILE },
1434 { "workingset_activate_anon", WORKINGSET_ACTIVATE_ANON },
1435 { "workingset_activate_file", WORKINGSET_ACTIVATE_FILE },
1436 { "workingset_restore_anon", WORKINGSET_RESTORE_ANON },
1437 { "workingset_restore_file", WORKINGSET_RESTORE_FILE },
1438 { "workingset_nodereclaim", WORKINGSET_NODERECLAIM },
1441 /* Translate stat items to the correct unit for memory.stat output */
1442 static int memcg_page_state_unit(int item)
1445 case MEMCG_PERCPU_B:
1447 case NR_SLAB_RECLAIMABLE_B:
1448 case NR_SLAB_UNRECLAIMABLE_B:
1449 case WORKINGSET_REFAULT_ANON:
1450 case WORKINGSET_REFAULT_FILE:
1451 case WORKINGSET_ACTIVATE_ANON:
1452 case WORKINGSET_ACTIVATE_FILE:
1453 case WORKINGSET_RESTORE_ANON:
1454 case WORKINGSET_RESTORE_FILE:
1455 case WORKINGSET_NODERECLAIM:
1457 case NR_KERNEL_STACK_KB:
1464 static inline unsigned long memcg_page_state_output(struct mem_cgroup *memcg,
1467 return memcg_page_state(memcg, item) * memcg_page_state_unit(item);
1470 /* Subset of vm_event_item to report for memcg event stats */
1471 static const unsigned int memcg_vm_event_stat[] = {
1483 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
1487 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1493 static void memory_stat_format(struct mem_cgroup *memcg, char *buf, int bufsize)
1498 seq_buf_init(&s, buf, bufsize);
1501 * Provide statistics on the state of the memory subsystem as
1502 * well as cumulative event counters that show past behavior.
1504 * This list is ordered following a combination of these gradients:
1505 * 1) generic big picture -> specifics and details
1506 * 2) reflecting userspace activity -> reflecting kernel heuristics
1508 * Current memory state:
1510 mem_cgroup_flush_stats();
1512 for (i = 0; i < ARRAY_SIZE(memory_stats); i++) {
1515 size = memcg_page_state_output(memcg, memory_stats[i].idx);
1516 seq_buf_printf(&s, "%s %llu\n", memory_stats[i].name, size);
1518 if (unlikely(memory_stats[i].idx == NR_SLAB_UNRECLAIMABLE_B)) {
1519 size += memcg_page_state_output(memcg,
1520 NR_SLAB_RECLAIMABLE_B);
1521 seq_buf_printf(&s, "slab %llu\n", size);
1525 /* Accumulated memory events */
1526 seq_buf_printf(&s, "pgscan %lu\n",
1527 memcg_events(memcg, PGSCAN_KSWAPD) +
1528 memcg_events(memcg, PGSCAN_DIRECT));
1529 seq_buf_printf(&s, "pgsteal %lu\n",
1530 memcg_events(memcg, PGSTEAL_KSWAPD) +
1531 memcg_events(memcg, PGSTEAL_DIRECT));
1533 for (i = 0; i < ARRAY_SIZE(memcg_vm_event_stat); i++)
1534 seq_buf_printf(&s, "%s %lu\n",
1535 vm_event_name(memcg_vm_event_stat[i]),
1536 memcg_events(memcg, memcg_vm_event_stat[i]));
1538 /* The above should easily fit into one page */
1539 WARN_ON_ONCE(seq_buf_has_overflowed(&s));
1542 #define K(x) ((x) << (PAGE_SHIFT-10))
1544 * mem_cgroup_print_oom_context: Print OOM information relevant to
1545 * memory controller.
1546 * @memcg: The memory cgroup that went over limit
1547 * @p: Task that is going to be killed
1549 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1552 void mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
1557 pr_cont(",oom_memcg=");
1558 pr_cont_cgroup_path(memcg->css.cgroup);
1560 pr_cont(",global_oom");
1562 pr_cont(",task_memcg=");
1563 pr_cont_cgroup_path(task_cgroup(p, memory_cgrp_id));
1569 * mem_cgroup_print_oom_meminfo: Print OOM memory information relevant to
1570 * memory controller.
1571 * @memcg: The memory cgroup that went over limit
1573 void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
1575 /* Use static buffer, for the caller is holding oom_lock. */
1576 static char buf[PAGE_SIZE];
1578 lockdep_assert_held(&oom_lock);
1580 pr_info("memory: usage %llukB, limit %llukB, failcnt %lu\n",
1581 K((u64)page_counter_read(&memcg->memory)),
1582 K((u64)READ_ONCE(memcg->memory.max)), memcg->memory.failcnt);
1583 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
1584 pr_info("swap: usage %llukB, limit %llukB, failcnt %lu\n",
1585 K((u64)page_counter_read(&memcg->swap)),
1586 K((u64)READ_ONCE(memcg->swap.max)), memcg->swap.failcnt);
1588 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %lu\n",
1589 K((u64)page_counter_read(&memcg->memsw)),
1590 K((u64)memcg->memsw.max), memcg->memsw.failcnt);
1591 pr_info("kmem: usage %llukB, limit %llukB, failcnt %lu\n",
1592 K((u64)page_counter_read(&memcg->kmem)),
1593 K((u64)memcg->kmem.max), memcg->kmem.failcnt);
1596 pr_info("Memory cgroup stats for ");
1597 pr_cont_cgroup_path(memcg->css.cgroup);
1599 memory_stat_format(memcg, buf, sizeof(buf));
1604 * Return the memory (and swap, if configured) limit for a memcg.
1606 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
1608 unsigned long max = READ_ONCE(memcg->memory.max);
1610 if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
1611 if (mem_cgroup_swappiness(memcg))
1612 max += min(READ_ONCE(memcg->swap.max),
1613 (unsigned long)total_swap_pages);
1615 if (mem_cgroup_swappiness(memcg)) {
1616 /* Calculate swap excess capacity from memsw limit */
1617 unsigned long swap = READ_ONCE(memcg->memsw.max) - max;
1619 max += min(swap, (unsigned long)total_swap_pages);
1625 unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
1627 return page_counter_read(&memcg->memory);
1630 static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1633 struct oom_control oc = {
1637 .gfp_mask = gfp_mask,
1642 if (mutex_lock_killable(&oom_lock))
1645 if (mem_cgroup_margin(memcg) >= (1 << order))
1649 * A few threads which were not waiting at mutex_lock_killable() can
1650 * fail to bail out. Therefore, check again after holding oom_lock.
1652 ret = task_is_dying() || out_of_memory(&oc);
1655 mutex_unlock(&oom_lock);
1659 static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1662 unsigned long *total_scanned)
1664 struct mem_cgroup *victim = NULL;
1667 unsigned long excess;
1668 unsigned long nr_scanned;
1669 struct mem_cgroup_reclaim_cookie reclaim = {
1673 excess = soft_limit_excess(root_memcg);
1676 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
1681 * If we have not been able to reclaim
1682 * anything, it might because there are
1683 * no reclaimable pages under this hierarchy
1688 * We want to do more targeted reclaim.
1689 * excess >> 2 is not to excessive so as to
1690 * reclaim too much, nor too less that we keep
1691 * coming back to reclaim from this cgroup
1693 if (total >= (excess >> 2) ||
1694 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
1699 total += mem_cgroup_shrink_node(victim, gfp_mask, false,
1700 pgdat, &nr_scanned);
1701 *total_scanned += nr_scanned;
1702 if (!soft_limit_excess(root_memcg))
1705 mem_cgroup_iter_break(root_memcg, victim);
1709 #ifdef CONFIG_LOCKDEP
1710 static struct lockdep_map memcg_oom_lock_dep_map = {
1711 .name = "memcg_oom_lock",
1715 static DEFINE_SPINLOCK(memcg_oom_lock);
1718 * Check OOM-Killer is already running under our hierarchy.
1719 * If someone is running, return false.
1721 static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
1723 struct mem_cgroup *iter, *failed = NULL;
1725 spin_lock(&memcg_oom_lock);
1727 for_each_mem_cgroup_tree(iter, memcg) {
1728 if (iter->oom_lock) {
1730 * this subtree of our hierarchy is already locked
1731 * so we cannot give a lock.
1734 mem_cgroup_iter_break(memcg, iter);
1737 iter->oom_lock = true;
1742 * OK, we failed to lock the whole subtree so we have
1743 * to clean up what we set up to the failing subtree
1745 for_each_mem_cgroup_tree(iter, memcg) {
1746 if (iter == failed) {
1747 mem_cgroup_iter_break(memcg, iter);
1750 iter->oom_lock = false;
1753 mutex_acquire(&memcg_oom_lock_dep_map, 0, 1, _RET_IP_);
1755 spin_unlock(&memcg_oom_lock);
1760 static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
1762 struct mem_cgroup *iter;
1764 spin_lock(&memcg_oom_lock);
1765 mutex_release(&memcg_oom_lock_dep_map, _RET_IP_);
1766 for_each_mem_cgroup_tree(iter, memcg)
1767 iter->oom_lock = false;
1768 spin_unlock(&memcg_oom_lock);
1771 static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
1773 struct mem_cgroup *iter;
1775 spin_lock(&memcg_oom_lock);
1776 for_each_mem_cgroup_tree(iter, memcg)
1778 spin_unlock(&memcg_oom_lock);
1781 static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
1783 struct mem_cgroup *iter;
1786 * Be careful about under_oom underflows because a child memcg
1787 * could have been added after mem_cgroup_mark_under_oom.
1789 spin_lock(&memcg_oom_lock);
1790 for_each_mem_cgroup_tree(iter, memcg)
1791 if (iter->under_oom > 0)
1793 spin_unlock(&memcg_oom_lock);
1796 static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1798 struct oom_wait_info {
1799 struct mem_cgroup *memcg;
1800 wait_queue_entry_t wait;
1803 static int memcg_oom_wake_function(wait_queue_entry_t *wait,
1804 unsigned mode, int sync, void *arg)
1806 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
1807 struct mem_cgroup *oom_wait_memcg;
1808 struct oom_wait_info *oom_wait_info;
1810 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
1811 oom_wait_memcg = oom_wait_info->memcg;
1813 if (!mem_cgroup_is_descendant(wake_memcg, oom_wait_memcg) &&
1814 !mem_cgroup_is_descendant(oom_wait_memcg, wake_memcg))
1816 return autoremove_wake_function(wait, mode, sync, arg);
1819 static void memcg_oom_recover(struct mem_cgroup *memcg)
1822 * For the following lockless ->under_oom test, the only required
1823 * guarantee is that it must see the state asserted by an OOM when
1824 * this function is called as a result of userland actions
1825 * triggered by the notification of the OOM. This is trivially
1826 * achieved by invoking mem_cgroup_mark_under_oom() before
1827 * triggering notification.
1829 if (memcg && memcg->under_oom)
1830 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
1834 * Returns true if successfully killed one or more processes. Though in some
1835 * corner cases it can return true even without killing any process.
1837 static bool mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
1841 if (order > PAGE_ALLOC_COSTLY_ORDER)
1844 memcg_memory_event(memcg, MEMCG_OOM);
1847 * We are in the middle of the charge context here, so we
1848 * don't want to block when potentially sitting on a callstack
1849 * that holds all kinds of filesystem and mm locks.
1851 * cgroup1 allows disabling the OOM killer and waiting for outside
1852 * handling until the charge can succeed; remember the context and put
1853 * the task to sleep at the end of the page fault when all locks are
1856 * On the other hand, in-kernel OOM killer allows for an async victim
1857 * memory reclaim (oom_reaper) and that means that we are not solely
1858 * relying on the oom victim to make a forward progress and we can
1859 * invoke the oom killer here.
1861 * Please note that mem_cgroup_out_of_memory might fail to find a
1862 * victim and then we have to bail out from the charge path.
1864 if (memcg->oom_kill_disable) {
1865 if (current->in_user_fault) {
1866 css_get(&memcg->css);
1867 current->memcg_in_oom = memcg;
1868 current->memcg_oom_gfp_mask = mask;
1869 current->memcg_oom_order = order;
1874 mem_cgroup_mark_under_oom(memcg);
1876 locked = mem_cgroup_oom_trylock(memcg);
1879 mem_cgroup_oom_notify(memcg);
1881 mem_cgroup_unmark_under_oom(memcg);
1882 ret = mem_cgroup_out_of_memory(memcg, mask, order);
1885 mem_cgroup_oom_unlock(memcg);
1891 * mem_cgroup_oom_synchronize - complete memcg OOM handling
1892 * @handle: actually kill/wait or just clean up the OOM state
1894 * This has to be called at the end of a page fault if the memcg OOM
1895 * handler was enabled.
1897 * Memcg supports userspace OOM handling where failed allocations must
1898 * sleep on a waitqueue until the userspace task resolves the
1899 * situation. Sleeping directly in the charge context with all kinds
1900 * of locks held is not a good idea, instead we remember an OOM state
1901 * in the task and mem_cgroup_oom_synchronize() has to be called at
1902 * the end of the page fault to complete the OOM handling.
1904 * Returns %true if an ongoing memcg OOM situation was detected and
1905 * completed, %false otherwise.
1907 bool mem_cgroup_oom_synchronize(bool handle)
1909 struct mem_cgroup *memcg = current->memcg_in_oom;
1910 struct oom_wait_info owait;
1913 /* OOM is global, do not handle */
1920 owait.memcg = memcg;
1921 owait.wait.flags = 0;
1922 owait.wait.func = memcg_oom_wake_function;
1923 owait.wait.private = current;
1924 INIT_LIST_HEAD(&owait.wait.entry);
1926 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
1927 mem_cgroup_mark_under_oom(memcg);
1929 locked = mem_cgroup_oom_trylock(memcg);
1932 mem_cgroup_oom_notify(memcg);
1934 if (locked && !memcg->oom_kill_disable) {
1935 mem_cgroup_unmark_under_oom(memcg);
1936 finish_wait(&memcg_oom_waitq, &owait.wait);
1937 mem_cgroup_out_of_memory(memcg, current->memcg_oom_gfp_mask,
1938 current->memcg_oom_order);
1941 mem_cgroup_unmark_under_oom(memcg);
1942 finish_wait(&memcg_oom_waitq, &owait.wait);
1946 mem_cgroup_oom_unlock(memcg);
1948 * There is no guarantee that an OOM-lock contender
1949 * sees the wakeups triggered by the OOM kill
1950 * uncharges. Wake any sleepers explicitly.
1952 memcg_oom_recover(memcg);
1955 current->memcg_in_oom = NULL;
1956 css_put(&memcg->css);
1961 * mem_cgroup_get_oom_group - get a memory cgroup to clean up after OOM
1962 * @victim: task to be killed by the OOM killer
1963 * @oom_domain: memcg in case of memcg OOM, NULL in case of system-wide OOM
1965 * Returns a pointer to a memory cgroup, which has to be cleaned up
1966 * by killing all belonging OOM-killable tasks.
1968 * Caller has to call mem_cgroup_put() on the returned non-NULL memcg.
1970 struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
1971 struct mem_cgroup *oom_domain)
1973 struct mem_cgroup *oom_group = NULL;
1974 struct mem_cgroup *memcg;
1976 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
1980 oom_domain = root_mem_cgroup;
1984 memcg = mem_cgroup_from_task(victim);
1985 if (memcg == root_mem_cgroup)
1989 * If the victim task has been asynchronously moved to a different
1990 * memory cgroup, we might end up killing tasks outside oom_domain.
1991 * In this case it's better to ignore memory.group.oom.
1993 if (unlikely(!mem_cgroup_is_descendant(memcg, oom_domain)))
1997 * Traverse the memory cgroup hierarchy from the victim task's
1998 * cgroup up to the OOMing cgroup (or root) to find the
1999 * highest-level memory cgroup with oom.group set.
2001 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
2002 if (memcg->oom_group)
2005 if (memcg == oom_domain)
2010 css_get(&oom_group->css);
2017 void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
2019 pr_info("Tasks in ");
2020 pr_cont_cgroup_path(memcg->css.cgroup);
2021 pr_cont(" are going to be killed due to memory.oom.group set\n");
2025 * folio_memcg_lock - Bind a folio to its memcg.
2026 * @folio: The folio.
2028 * This function prevents unlocked LRU folios from being moved to
2031 * It ensures lifetime of the bound memcg. The caller is responsible
2032 * for the lifetime of the folio.
2034 void folio_memcg_lock(struct folio *folio)
2036 struct mem_cgroup *memcg;
2037 unsigned long flags;
2040 * The RCU lock is held throughout the transaction. The fast
2041 * path can get away without acquiring the memcg->move_lock
2042 * because page moving starts with an RCU grace period.
2046 if (mem_cgroup_disabled())
2049 memcg = folio_memcg(folio);
2050 if (unlikely(!memcg))
2053 #ifdef CONFIG_PROVE_LOCKING
2054 local_irq_save(flags);
2055 might_lock(&memcg->move_lock);
2056 local_irq_restore(flags);
2059 if (atomic_read(&memcg->moving_account) <= 0)
2062 spin_lock_irqsave(&memcg->move_lock, flags);
2063 if (memcg != folio_memcg(folio)) {
2064 spin_unlock_irqrestore(&memcg->move_lock, flags);
2069 * When charge migration first begins, we can have multiple
2070 * critical sections holding the fast-path RCU lock and one
2071 * holding the slowpath move_lock. Track the task who has the
2072 * move_lock for unlock_page_memcg().
2074 memcg->move_lock_task = current;
2075 memcg->move_lock_flags = flags;
2078 void lock_page_memcg(struct page *page)
2080 folio_memcg_lock(page_folio(page));
2083 static void __folio_memcg_unlock(struct mem_cgroup *memcg)
2085 if (memcg && memcg->move_lock_task == current) {
2086 unsigned long flags = memcg->move_lock_flags;
2088 memcg->move_lock_task = NULL;
2089 memcg->move_lock_flags = 0;
2091 spin_unlock_irqrestore(&memcg->move_lock, flags);
2098 * folio_memcg_unlock - Release the binding between a folio and its memcg.
2099 * @folio: The folio.
2101 * This releases the binding created by folio_memcg_lock(). This does
2102 * not change the accounting of this folio to its memcg, but it does
2103 * permit others to change it.
2105 void folio_memcg_unlock(struct folio *folio)
2107 __folio_memcg_unlock(folio_memcg(folio));
2110 void unlock_page_memcg(struct page *page)
2112 folio_memcg_unlock(page_folio(page));
2115 struct memcg_stock_pcp {
2116 local_lock_t stock_lock;
2117 struct mem_cgroup *cached; /* this never be root cgroup */
2118 unsigned int nr_pages;
2120 #ifdef CONFIG_MEMCG_KMEM
2121 struct obj_cgroup *cached_objcg;
2122 struct pglist_data *cached_pgdat;
2123 unsigned int nr_bytes;
2124 int nr_slab_reclaimable_b;
2125 int nr_slab_unreclaimable_b;
2128 struct work_struct work;
2129 unsigned long flags;
2130 #define FLUSHING_CACHED_CHARGE 0
2132 static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock) = {
2133 .stock_lock = INIT_LOCAL_LOCK(stock_lock),
2135 static DEFINE_MUTEX(percpu_charge_mutex);
2137 #ifdef CONFIG_MEMCG_KMEM
2138 static struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock);
2139 static bool obj_stock_flush_required(struct memcg_stock_pcp *stock,
2140 struct mem_cgroup *root_memcg);
2141 static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages);
2144 static inline struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock)
2148 static bool obj_stock_flush_required(struct memcg_stock_pcp *stock,
2149 struct mem_cgroup *root_memcg)
2153 static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages)
2159 * consume_stock: Try to consume stocked charge on this cpu.
2160 * @memcg: memcg to consume from.
2161 * @nr_pages: how many pages to charge.
2163 * The charges will only happen if @memcg matches the current cpu's memcg
2164 * stock, and at least @nr_pages are available in that stock. Failure to
2165 * service an allocation will refill the stock.
2167 * returns true if successful, false otherwise.
2169 static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
2171 struct memcg_stock_pcp *stock;
2172 unsigned long flags;
2175 if (nr_pages > MEMCG_CHARGE_BATCH)
2178 local_lock_irqsave(&memcg_stock.stock_lock, flags);
2180 stock = this_cpu_ptr(&memcg_stock);
2181 if (memcg == stock->cached && stock->nr_pages >= nr_pages) {
2182 stock->nr_pages -= nr_pages;
2186 local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
2192 * Returns stocks cached in percpu and reset cached information.
2194 static void drain_stock(struct memcg_stock_pcp *stock)
2196 struct mem_cgroup *old = stock->cached;
2201 if (stock->nr_pages) {
2202 page_counter_uncharge(&old->memory, stock->nr_pages);
2203 if (do_memsw_account())
2204 page_counter_uncharge(&old->memsw, stock->nr_pages);
2205 stock->nr_pages = 0;
2209 stock->cached = NULL;
2212 static void drain_local_stock(struct work_struct *dummy)
2214 struct memcg_stock_pcp *stock;
2215 struct obj_cgroup *old = NULL;
2216 unsigned long flags;
2219 * The only protection from cpu hotplug (memcg_hotplug_cpu_dead) vs.
2220 * drain_stock races is that we always operate on local CPU stock
2221 * here with IRQ disabled
2223 local_lock_irqsave(&memcg_stock.stock_lock, flags);
2225 stock = this_cpu_ptr(&memcg_stock);
2226 old = drain_obj_stock(stock);
2228 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
2230 local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
2232 obj_cgroup_put(old);
2236 * Cache charges(val) to local per_cpu area.
2237 * This will be consumed by consume_stock() function, later.
2239 static void __refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
2241 struct memcg_stock_pcp *stock;
2243 stock = this_cpu_ptr(&memcg_stock);
2244 if (stock->cached != memcg) { /* reset if necessary */
2246 css_get(&memcg->css);
2247 stock->cached = memcg;
2249 stock->nr_pages += nr_pages;
2251 if (stock->nr_pages > MEMCG_CHARGE_BATCH)
2255 static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
2257 unsigned long flags;
2259 local_lock_irqsave(&memcg_stock.stock_lock, flags);
2260 __refill_stock(memcg, nr_pages);
2261 local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
2265 * Drains all per-CPU charge caches for given root_memcg resp. subtree
2266 * of the hierarchy under it.
2268 static void drain_all_stock(struct mem_cgroup *root_memcg)
2272 /* If someone's already draining, avoid adding running more workers. */
2273 if (!mutex_trylock(&percpu_charge_mutex))
2276 * Notify other cpus that system-wide "drain" is running
2277 * We do not care about races with the cpu hotplug because cpu down
2278 * as well as workers from this path always operate on the local
2279 * per-cpu data. CPU up doesn't touch memcg_stock at all.
2282 curcpu = smp_processor_id();
2283 for_each_online_cpu(cpu) {
2284 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
2285 struct mem_cgroup *memcg;
2289 memcg = stock->cached;
2290 if (memcg && stock->nr_pages &&
2291 mem_cgroup_is_descendant(memcg, root_memcg))
2293 else if (obj_stock_flush_required(stock, root_memcg))
2298 !test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2300 drain_local_stock(&stock->work);
2302 schedule_work_on(cpu, &stock->work);
2306 mutex_unlock(&percpu_charge_mutex);
2309 static int memcg_hotplug_cpu_dead(unsigned int cpu)
2311 struct memcg_stock_pcp *stock;
2313 stock = &per_cpu(memcg_stock, cpu);
2319 static unsigned long reclaim_high(struct mem_cgroup *memcg,
2320 unsigned int nr_pages,
2323 unsigned long nr_reclaimed = 0;
2326 unsigned long pflags;
2328 if (page_counter_read(&memcg->memory) <=
2329 READ_ONCE(memcg->memory.high))
2332 memcg_memory_event(memcg, MEMCG_HIGH);
2334 psi_memstall_enter(&pflags);
2335 nr_reclaimed += try_to_free_mem_cgroup_pages(memcg, nr_pages,
2337 MEMCG_RECLAIM_MAY_SWAP);
2338 psi_memstall_leave(&pflags);
2339 } while ((memcg = parent_mem_cgroup(memcg)) &&
2340 !mem_cgroup_is_root(memcg));
2342 return nr_reclaimed;
2345 static void high_work_func(struct work_struct *work)
2347 struct mem_cgroup *memcg;
2349 memcg = container_of(work, struct mem_cgroup, high_work);
2350 reclaim_high(memcg, MEMCG_CHARGE_BATCH, GFP_KERNEL);
2354 * Clamp the maximum sleep time per allocation batch to 2 seconds. This is
2355 * enough to still cause a significant slowdown in most cases, while still
2356 * allowing diagnostics and tracing to proceed without becoming stuck.
2358 #define MEMCG_MAX_HIGH_DELAY_JIFFIES (2UL*HZ)
2361 * When calculating the delay, we use these either side of the exponentiation to
2362 * maintain precision and scale to a reasonable number of jiffies (see the table
2365 * - MEMCG_DELAY_PRECISION_SHIFT: Extra precision bits while translating the
2366 * overage ratio to a delay.
2367 * - MEMCG_DELAY_SCALING_SHIFT: The number of bits to scale down the
2368 * proposed penalty in order to reduce to a reasonable number of jiffies, and
2369 * to produce a reasonable delay curve.
2371 * MEMCG_DELAY_SCALING_SHIFT just happens to be a number that produces a
2372 * reasonable delay curve compared to precision-adjusted overage, not
2373 * penalising heavily at first, but still making sure that growth beyond the
2374 * limit penalises misbehaviour cgroups by slowing them down exponentially. For
2375 * example, with a high of 100 megabytes:
2377 * +-------+------------------------+
2378 * | usage | time to allocate in ms |
2379 * +-------+------------------------+
2401 * +-------+------------------------+
2403 #define MEMCG_DELAY_PRECISION_SHIFT 20
2404 #define MEMCG_DELAY_SCALING_SHIFT 14
2406 static u64 calculate_overage(unsigned long usage, unsigned long high)
2414 * Prevent division by 0 in overage calculation by acting as if
2415 * it was a threshold of 1 page
2417 high = max(high, 1UL);
2419 overage = usage - high;
2420 overage <<= MEMCG_DELAY_PRECISION_SHIFT;
2421 return div64_u64(overage, high);
2424 static u64 mem_find_max_overage(struct mem_cgroup *memcg)
2426 u64 overage, max_overage = 0;
2429 overage = calculate_overage(page_counter_read(&memcg->memory),
2430 READ_ONCE(memcg->memory.high));
2431 max_overage = max(overage, max_overage);
2432 } while ((memcg = parent_mem_cgroup(memcg)) &&
2433 !mem_cgroup_is_root(memcg));
2438 static u64 swap_find_max_overage(struct mem_cgroup *memcg)
2440 u64 overage, max_overage = 0;
2443 overage = calculate_overage(page_counter_read(&memcg->swap),
2444 READ_ONCE(memcg->swap.high));
2446 memcg_memory_event(memcg, MEMCG_SWAP_HIGH);
2447 max_overage = max(overage, max_overage);
2448 } while ((memcg = parent_mem_cgroup(memcg)) &&
2449 !mem_cgroup_is_root(memcg));
2455 * Get the number of jiffies that we should penalise a mischievous cgroup which
2456 * is exceeding its memory.high by checking both it and its ancestors.
2458 static unsigned long calculate_high_delay(struct mem_cgroup *memcg,
2459 unsigned int nr_pages,
2462 unsigned long penalty_jiffies;
2468 * We use overage compared to memory.high to calculate the number of
2469 * jiffies to sleep (penalty_jiffies). Ideally this value should be
2470 * fairly lenient on small overages, and increasingly harsh when the
2471 * memcg in question makes it clear that it has no intention of stopping
2472 * its crazy behaviour, so we exponentially increase the delay based on
2475 penalty_jiffies = max_overage * max_overage * HZ;
2476 penalty_jiffies >>= MEMCG_DELAY_PRECISION_SHIFT;
2477 penalty_jiffies >>= MEMCG_DELAY_SCALING_SHIFT;
2480 * Factor in the task's own contribution to the overage, such that four
2481 * N-sized allocations are throttled approximately the same as one
2482 * 4N-sized allocation.
2484 * MEMCG_CHARGE_BATCH pages is nominal, so work out how much smaller or
2485 * larger the current charge patch is than that.
2487 return penalty_jiffies * nr_pages / MEMCG_CHARGE_BATCH;
2491 * Scheduled by try_charge() to be executed from the userland return path
2492 * and reclaims memory over the high limit.
2494 void mem_cgroup_handle_over_high(void)
2496 unsigned long penalty_jiffies;
2497 unsigned long pflags;
2498 unsigned long nr_reclaimed;
2499 unsigned int nr_pages = current->memcg_nr_pages_over_high;
2500 int nr_retries = MAX_RECLAIM_RETRIES;
2501 struct mem_cgroup *memcg;
2502 bool in_retry = false;
2504 if (likely(!nr_pages))
2507 memcg = get_mem_cgroup_from_mm(current->mm);
2508 current->memcg_nr_pages_over_high = 0;
2512 * The allocating task should reclaim at least the batch size, but for
2513 * subsequent retries we only want to do what's necessary to prevent oom
2514 * or breaching resource isolation.
2516 * This is distinct from memory.max or page allocator behaviour because
2517 * memory.high is currently batched, whereas memory.max and the page
2518 * allocator run every time an allocation is made.
2520 nr_reclaimed = reclaim_high(memcg,
2521 in_retry ? SWAP_CLUSTER_MAX : nr_pages,
2525 * memory.high is breached and reclaim is unable to keep up. Throttle
2526 * allocators proactively to slow down excessive growth.
2528 penalty_jiffies = calculate_high_delay(memcg, nr_pages,
2529 mem_find_max_overage(memcg));
2531 penalty_jiffies += calculate_high_delay(memcg, nr_pages,
2532 swap_find_max_overage(memcg));
2535 * Clamp the max delay per usermode return so as to still keep the
2536 * application moving forwards and also permit diagnostics, albeit
2539 penalty_jiffies = min(penalty_jiffies, MEMCG_MAX_HIGH_DELAY_JIFFIES);
2542 * Don't sleep if the amount of jiffies this memcg owes us is so low
2543 * that it's not even worth doing, in an attempt to be nice to those who
2544 * go only a small amount over their memory.high value and maybe haven't
2545 * been aggressively reclaimed enough yet.
2547 if (penalty_jiffies <= HZ / 100)
2551 * If reclaim is making forward progress but we're still over
2552 * memory.high, we want to encourage that rather than doing allocator
2555 if (nr_reclaimed || nr_retries--) {
2561 * If we exit early, we're guaranteed to die (since
2562 * schedule_timeout_killable sets TASK_KILLABLE). This means we don't
2563 * need to account for any ill-begotten jiffies to pay them off later.
2565 psi_memstall_enter(&pflags);
2566 schedule_timeout_killable(penalty_jiffies);
2567 psi_memstall_leave(&pflags);
2570 css_put(&memcg->css);
2573 static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
2574 unsigned int nr_pages)
2576 unsigned int batch = max(MEMCG_CHARGE_BATCH, nr_pages);
2577 int nr_retries = MAX_RECLAIM_RETRIES;
2578 struct mem_cgroup *mem_over_limit;
2579 struct page_counter *counter;
2580 unsigned long nr_reclaimed;
2581 bool passed_oom = false;
2582 unsigned int reclaim_options = MEMCG_RECLAIM_MAY_SWAP;
2583 bool drained = false;
2584 bool raised_max_event = false;
2585 unsigned long pflags;
2588 if (consume_stock(memcg, nr_pages))
2591 if (!do_memsw_account() ||
2592 page_counter_try_charge(&memcg->memsw, batch, &counter)) {
2593 if (page_counter_try_charge(&memcg->memory, batch, &counter))
2595 if (do_memsw_account())
2596 page_counter_uncharge(&memcg->memsw, batch);
2597 mem_over_limit = mem_cgroup_from_counter(counter, memory);
2599 mem_over_limit = mem_cgroup_from_counter(counter, memsw);
2600 reclaim_options &= ~MEMCG_RECLAIM_MAY_SWAP;
2603 if (batch > nr_pages) {
2609 * Prevent unbounded recursion when reclaim operations need to
2610 * allocate memory. This might exceed the limits temporarily,
2611 * but we prefer facilitating memory reclaim and getting back
2612 * under the limit over triggering OOM kills in these cases.
2614 if (unlikely(current->flags & PF_MEMALLOC))
2617 if (unlikely(task_in_memcg_oom(current)))
2620 if (!gfpflags_allow_blocking(gfp_mask))
2623 memcg_memory_event(mem_over_limit, MEMCG_MAX);
2624 raised_max_event = true;
2626 psi_memstall_enter(&pflags);
2627 nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages,
2628 gfp_mask, reclaim_options);
2629 psi_memstall_leave(&pflags);
2631 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
2635 drain_all_stock(mem_over_limit);
2640 if (gfp_mask & __GFP_NORETRY)
2643 * Even though the limit is exceeded at this point, reclaim
2644 * may have been able to free some pages. Retry the charge
2645 * before killing the task.
2647 * Only for regular pages, though: huge pages are rather
2648 * unlikely to succeed so close to the limit, and we fall back
2649 * to regular pages anyway in case of failure.
2651 if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER))
2654 * At task move, charge accounts can be doubly counted. So, it's
2655 * better to wait until the end of task_move if something is going on.
2657 if (mem_cgroup_wait_acct_move(mem_over_limit))
2663 if (gfp_mask & __GFP_RETRY_MAYFAIL)
2666 /* Avoid endless loop for tasks bypassed by the oom killer */
2667 if (passed_oom && task_is_dying())
2671 * keep retrying as long as the memcg oom killer is able to make
2672 * a forward progress or bypass the charge if the oom killer
2673 * couldn't make any progress.
2675 if (mem_cgroup_oom(mem_over_limit, gfp_mask,
2676 get_order(nr_pages * PAGE_SIZE))) {
2678 nr_retries = MAX_RECLAIM_RETRIES;
2683 * Memcg doesn't have a dedicated reserve for atomic
2684 * allocations. But like the global atomic pool, we need to
2685 * put the burden of reclaim on regular allocation requests
2686 * and let these go through as privileged allocations.
2688 if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH)))
2692 * If the allocation has to be enforced, don't forget to raise
2693 * a MEMCG_MAX event.
2695 if (!raised_max_event)
2696 memcg_memory_event(mem_over_limit, MEMCG_MAX);
2699 * The allocation either can't fail or will lead to more memory
2700 * being freed very soon. Allow memory usage go over the limit
2701 * temporarily by force charging it.
2703 page_counter_charge(&memcg->memory, nr_pages);
2704 if (do_memsw_account())
2705 page_counter_charge(&memcg->memsw, nr_pages);
2710 if (batch > nr_pages)
2711 refill_stock(memcg, batch - nr_pages);
2714 * If the hierarchy is above the normal consumption range, schedule
2715 * reclaim on returning to userland. We can perform reclaim here
2716 * if __GFP_RECLAIM but let's always punt for simplicity and so that
2717 * GFP_KERNEL can consistently be used during reclaim. @memcg is
2718 * not recorded as it most likely matches current's and won't
2719 * change in the meantime. As high limit is checked again before
2720 * reclaim, the cost of mismatch is negligible.
2723 bool mem_high, swap_high;
2725 mem_high = page_counter_read(&memcg->memory) >
2726 READ_ONCE(memcg->memory.high);
2727 swap_high = page_counter_read(&memcg->swap) >
2728 READ_ONCE(memcg->swap.high);
2730 /* Don't bother a random interrupted task */
2733 schedule_work(&memcg->high_work);
2739 if (mem_high || swap_high) {
2741 * The allocating tasks in this cgroup will need to do
2742 * reclaim or be throttled to prevent further growth
2743 * of the memory or swap footprints.
2745 * Target some best-effort fairness between the tasks,
2746 * and distribute reclaim work and delay penalties
2747 * based on how much each task is actually allocating.
2749 current->memcg_nr_pages_over_high += batch;
2750 set_notify_resume(current);
2753 } while ((memcg = parent_mem_cgroup(memcg)));
2755 if (current->memcg_nr_pages_over_high > MEMCG_CHARGE_BATCH &&
2756 !(current->flags & PF_MEMALLOC) &&
2757 gfpflags_allow_blocking(gfp_mask)) {
2758 mem_cgroup_handle_over_high();
2763 static inline int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
2764 unsigned int nr_pages)
2766 if (mem_cgroup_is_root(memcg))
2769 return try_charge_memcg(memcg, gfp_mask, nr_pages);
2772 static inline void cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages)
2774 if (mem_cgroup_is_root(memcg))
2777 page_counter_uncharge(&memcg->memory, nr_pages);
2778 if (do_memsw_account())
2779 page_counter_uncharge(&memcg->memsw, nr_pages);
2782 static void commit_charge(struct folio *folio, struct mem_cgroup *memcg)
2784 VM_BUG_ON_FOLIO(folio_memcg(folio), folio);
2786 * Any of the following ensures page's memcg stability:
2790 * - lock_page_memcg()
2791 * - exclusive reference
2793 folio->memcg_data = (unsigned long)memcg;
2796 #ifdef CONFIG_MEMCG_KMEM
2798 * The allocated objcg pointers array is not accounted directly.
2799 * Moreover, it should not come from DMA buffer and is not readily
2800 * reclaimable. So those GFP bits should be masked off.
2802 #define OBJCGS_CLEAR_MASK (__GFP_DMA | __GFP_RECLAIMABLE | __GFP_ACCOUNT)
2805 * mod_objcg_mlstate() may be called with irq enabled, so
2806 * mod_memcg_lruvec_state() should be used.
2808 static inline void mod_objcg_mlstate(struct obj_cgroup *objcg,
2809 struct pglist_data *pgdat,
2810 enum node_stat_item idx, int nr)
2812 struct mem_cgroup *memcg;
2813 struct lruvec *lruvec;
2816 memcg = obj_cgroup_memcg(objcg);
2817 lruvec = mem_cgroup_lruvec(memcg, pgdat);
2818 mod_memcg_lruvec_state(lruvec, idx, nr);
2822 int memcg_alloc_slab_cgroups(struct slab *slab, struct kmem_cache *s,
2823 gfp_t gfp, bool new_slab)
2825 unsigned int objects = objs_per_slab(s, slab);
2826 unsigned long memcg_data;
2829 gfp &= ~OBJCGS_CLEAR_MASK;
2830 vec = kcalloc_node(objects, sizeof(struct obj_cgroup *), gfp,
2835 memcg_data = (unsigned long) vec | MEMCG_DATA_OBJCGS;
2838 * If the slab is brand new and nobody can yet access its
2839 * memcg_data, no synchronization is required and memcg_data can
2840 * be simply assigned.
2842 slab->memcg_data = memcg_data;
2843 } else if (cmpxchg(&slab->memcg_data, 0, memcg_data)) {
2845 * If the slab is already in use, somebody can allocate and
2846 * assign obj_cgroups in parallel. In this case the existing
2847 * objcg vector should be reused.
2853 kmemleak_not_leak(vec);
2857 static __always_inline
2858 struct mem_cgroup *mem_cgroup_from_obj_folio(struct folio *folio, void *p)
2861 * Slab objects are accounted individually, not per-page.
2862 * Memcg membership data for each individual object is saved in
2865 if (folio_test_slab(folio)) {
2866 struct obj_cgroup **objcgs;
2870 slab = folio_slab(folio);
2871 objcgs = slab_objcgs(slab);
2875 off = obj_to_index(slab->slab_cache, slab, p);
2877 return obj_cgroup_memcg(objcgs[off]);
2883 * page_memcg_check() is used here, because in theory we can encounter
2884 * a folio where the slab flag has been cleared already, but
2885 * slab->memcg_data has not been freed yet
2886 * page_memcg_check(page) will guarantee that a proper memory
2887 * cgroup pointer or NULL will be returned.
2889 return page_memcg_check(folio_page(folio, 0));
2893 * Returns a pointer to the memory cgroup to which the kernel object is charged.
2895 * A passed kernel object can be a slab object, vmalloc object or a generic
2896 * kernel page, so different mechanisms for getting the memory cgroup pointer
2899 * In certain cases (e.g. kernel stacks or large kmallocs with SLUB) the caller
2900 * can not know for sure how the kernel object is implemented.
2901 * mem_cgroup_from_obj() can be safely used in such cases.
2903 * The caller must ensure the memcg lifetime, e.g. by taking rcu_read_lock(),
2904 * cgroup_mutex, etc.
2906 struct mem_cgroup *mem_cgroup_from_obj(void *p)
2908 struct folio *folio;
2910 if (mem_cgroup_disabled())
2913 if (unlikely(is_vmalloc_addr(p)))
2914 folio = page_folio(vmalloc_to_page(p));
2916 folio = virt_to_folio(p);
2918 return mem_cgroup_from_obj_folio(folio, p);
2922 * Returns a pointer to the memory cgroup to which the kernel object is charged.
2923 * Similar to mem_cgroup_from_obj(), but faster and not suitable for objects,
2924 * allocated using vmalloc().
2926 * A passed kernel object must be a slab object or a generic kernel page.
2928 * The caller must ensure the memcg lifetime, e.g. by taking rcu_read_lock(),
2929 * cgroup_mutex, etc.
2931 struct mem_cgroup *mem_cgroup_from_slab_obj(void *p)
2933 if (mem_cgroup_disabled())
2936 return mem_cgroup_from_obj_folio(virt_to_folio(p), p);
2939 static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg)
2941 struct obj_cgroup *objcg = NULL;
2943 for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg)) {
2944 objcg = rcu_dereference(memcg->objcg);
2945 if (objcg && obj_cgroup_tryget(objcg))
2952 __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void)
2954 struct obj_cgroup *objcg = NULL;
2955 struct mem_cgroup *memcg;
2957 if (memcg_kmem_bypass())
2961 if (unlikely(active_memcg()))
2962 memcg = active_memcg();
2964 memcg = mem_cgroup_from_task(current);
2965 objcg = __get_obj_cgroup_from_memcg(memcg);
2970 struct obj_cgroup *get_obj_cgroup_from_page(struct page *page)
2972 struct obj_cgroup *objcg;
2974 if (!memcg_kmem_enabled() || memcg_kmem_bypass())
2977 if (PageMemcgKmem(page)) {
2978 objcg = __folio_objcg(page_folio(page));
2979 obj_cgroup_get(objcg);
2981 struct mem_cgroup *memcg;
2984 memcg = __folio_memcg(page_folio(page));
2986 objcg = __get_obj_cgroup_from_memcg(memcg);
2994 static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages)
2996 mod_memcg_state(memcg, MEMCG_KMEM, nr_pages);
2997 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
2999 page_counter_charge(&memcg->kmem, nr_pages);
3001 page_counter_uncharge(&memcg->kmem, -nr_pages);
3007 * obj_cgroup_uncharge_pages: uncharge a number of kernel pages from a objcg
3008 * @objcg: object cgroup to uncharge
3009 * @nr_pages: number of pages to uncharge
3011 static void obj_cgroup_uncharge_pages(struct obj_cgroup *objcg,
3012 unsigned int nr_pages)
3014 struct mem_cgroup *memcg;
3016 memcg = get_mem_cgroup_from_objcg(objcg);
3018 memcg_account_kmem(memcg, -nr_pages);
3019 refill_stock(memcg, nr_pages);
3021 css_put(&memcg->css);
3025 * obj_cgroup_charge_pages: charge a number of kernel pages to a objcg
3026 * @objcg: object cgroup to charge
3027 * @gfp: reclaim mode
3028 * @nr_pages: number of pages to charge
3030 * Returns 0 on success, an error code on failure.
3032 static int obj_cgroup_charge_pages(struct obj_cgroup *objcg, gfp_t gfp,
3033 unsigned int nr_pages)
3035 struct mem_cgroup *memcg;
3038 memcg = get_mem_cgroup_from_objcg(objcg);
3040 ret = try_charge_memcg(memcg, gfp, nr_pages);
3044 memcg_account_kmem(memcg, nr_pages);
3046 css_put(&memcg->css);
3052 * __memcg_kmem_charge_page: charge a kmem page to the current memory cgroup
3053 * @page: page to charge
3054 * @gfp: reclaim mode
3055 * @order: allocation order
3057 * Returns 0 on success, an error code on failure.
3059 int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order)
3061 struct obj_cgroup *objcg;
3064 objcg = get_obj_cgroup_from_current();
3066 ret = obj_cgroup_charge_pages(objcg, gfp, 1 << order);
3068 page->memcg_data = (unsigned long)objcg |
3072 obj_cgroup_put(objcg);
3078 * __memcg_kmem_uncharge_page: uncharge a kmem page
3079 * @page: page to uncharge
3080 * @order: allocation order
3082 void __memcg_kmem_uncharge_page(struct page *page, int order)
3084 struct folio *folio = page_folio(page);
3085 struct obj_cgroup *objcg;
3086 unsigned int nr_pages = 1 << order;
3088 if (!folio_memcg_kmem(folio))
3091 objcg = __folio_objcg(folio);
3092 obj_cgroup_uncharge_pages(objcg, nr_pages);
3093 folio->memcg_data = 0;
3094 obj_cgroup_put(objcg);
3097 void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat,
3098 enum node_stat_item idx, int nr)
3100 struct memcg_stock_pcp *stock;
3101 struct obj_cgroup *old = NULL;
3102 unsigned long flags;
3105 local_lock_irqsave(&memcg_stock.stock_lock, flags);
3106 stock = this_cpu_ptr(&memcg_stock);
3109 * Save vmstat data in stock and skip vmstat array update unless
3110 * accumulating over a page of vmstat data or when pgdat or idx
3113 if (stock->cached_objcg != objcg) {
3114 old = drain_obj_stock(stock);
3115 obj_cgroup_get(objcg);
3116 stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes)
3117 ? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0;
3118 stock->cached_objcg = objcg;
3119 stock->cached_pgdat = pgdat;
3120 } else if (stock->cached_pgdat != pgdat) {
3121 /* Flush the existing cached vmstat data */
3122 struct pglist_data *oldpg = stock->cached_pgdat;
3124 if (stock->nr_slab_reclaimable_b) {
3125 mod_objcg_mlstate(objcg, oldpg, NR_SLAB_RECLAIMABLE_B,
3126 stock->nr_slab_reclaimable_b);
3127 stock->nr_slab_reclaimable_b = 0;
3129 if (stock->nr_slab_unreclaimable_b) {
3130 mod_objcg_mlstate(objcg, oldpg, NR_SLAB_UNRECLAIMABLE_B,
3131 stock->nr_slab_unreclaimable_b);
3132 stock->nr_slab_unreclaimable_b = 0;
3134 stock->cached_pgdat = pgdat;
3137 bytes = (idx == NR_SLAB_RECLAIMABLE_B) ? &stock->nr_slab_reclaimable_b
3138 : &stock->nr_slab_unreclaimable_b;
3140 * Even for large object >= PAGE_SIZE, the vmstat data will still be
3141 * cached locally at least once before pushing it out.
3148 if (abs(*bytes) > PAGE_SIZE) {
3156 mod_objcg_mlstate(objcg, pgdat, idx, nr);
3158 local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
3160 obj_cgroup_put(old);
3163 static bool consume_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes)
3165 struct memcg_stock_pcp *stock;
3166 unsigned long flags;
3169 local_lock_irqsave(&memcg_stock.stock_lock, flags);
3171 stock = this_cpu_ptr(&memcg_stock);
3172 if (objcg == stock->cached_objcg && stock->nr_bytes >= nr_bytes) {
3173 stock->nr_bytes -= nr_bytes;
3177 local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
3182 static struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock)
3184 struct obj_cgroup *old = stock->cached_objcg;
3189 if (stock->nr_bytes) {
3190 unsigned int nr_pages = stock->nr_bytes >> PAGE_SHIFT;
3191 unsigned int nr_bytes = stock->nr_bytes & (PAGE_SIZE - 1);
3194 struct mem_cgroup *memcg;
3196 memcg = get_mem_cgroup_from_objcg(old);
3198 memcg_account_kmem(memcg, -nr_pages);
3199 __refill_stock(memcg, nr_pages);
3201 css_put(&memcg->css);
3205 * The leftover is flushed to the centralized per-memcg value.
3206 * On the next attempt to refill obj stock it will be moved
3207 * to a per-cpu stock (probably, on an other CPU), see
3208 * refill_obj_stock().
3210 * How often it's flushed is a trade-off between the memory
3211 * limit enforcement accuracy and potential CPU contention,
3212 * so it might be changed in the future.
3214 atomic_add(nr_bytes, &old->nr_charged_bytes);
3215 stock->nr_bytes = 0;
3219 * Flush the vmstat data in current stock
3221 if (stock->nr_slab_reclaimable_b || stock->nr_slab_unreclaimable_b) {
3222 if (stock->nr_slab_reclaimable_b) {
3223 mod_objcg_mlstate(old, stock->cached_pgdat,
3224 NR_SLAB_RECLAIMABLE_B,
3225 stock->nr_slab_reclaimable_b);
3226 stock->nr_slab_reclaimable_b = 0;
3228 if (stock->nr_slab_unreclaimable_b) {
3229 mod_objcg_mlstate(old, stock->cached_pgdat,
3230 NR_SLAB_UNRECLAIMABLE_B,
3231 stock->nr_slab_unreclaimable_b);
3232 stock->nr_slab_unreclaimable_b = 0;
3234 stock->cached_pgdat = NULL;
3237 stock->cached_objcg = NULL;
3239 * The `old' objects needs to be released by the caller via
3240 * obj_cgroup_put() outside of memcg_stock_pcp::stock_lock.
3245 static bool obj_stock_flush_required(struct memcg_stock_pcp *stock,
3246 struct mem_cgroup *root_memcg)
3248 struct mem_cgroup *memcg;
3250 if (stock->cached_objcg) {
3251 memcg = obj_cgroup_memcg(stock->cached_objcg);
3252 if (memcg && mem_cgroup_is_descendant(memcg, root_memcg))
3259 static void refill_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes,
3260 bool allow_uncharge)
3262 struct memcg_stock_pcp *stock;
3263 struct obj_cgroup *old = NULL;
3264 unsigned long flags;
3265 unsigned int nr_pages = 0;
3267 local_lock_irqsave(&memcg_stock.stock_lock, flags);
3269 stock = this_cpu_ptr(&memcg_stock);
3270 if (stock->cached_objcg != objcg) { /* reset if necessary */
3271 old = drain_obj_stock(stock);
3272 obj_cgroup_get(objcg);
3273 stock->cached_objcg = objcg;
3274 stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes)
3275 ? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0;
3276 allow_uncharge = true; /* Allow uncharge when objcg changes */
3278 stock->nr_bytes += nr_bytes;
3280 if (allow_uncharge && (stock->nr_bytes > PAGE_SIZE)) {
3281 nr_pages = stock->nr_bytes >> PAGE_SHIFT;
3282 stock->nr_bytes &= (PAGE_SIZE - 1);
3285 local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
3287 obj_cgroup_put(old);
3290 obj_cgroup_uncharge_pages(objcg, nr_pages);
3293 int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size)
3295 unsigned int nr_pages, nr_bytes;
3298 if (consume_obj_stock(objcg, size))
3302 * In theory, objcg->nr_charged_bytes can have enough
3303 * pre-charged bytes to satisfy the allocation. However,
3304 * flushing objcg->nr_charged_bytes requires two atomic
3305 * operations, and objcg->nr_charged_bytes can't be big.
3306 * The shared objcg->nr_charged_bytes can also become a
3307 * performance bottleneck if all tasks of the same memcg are
3308 * trying to update it. So it's better to ignore it and try
3309 * grab some new pages. The stock's nr_bytes will be flushed to
3310 * objcg->nr_charged_bytes later on when objcg changes.
3312 * The stock's nr_bytes may contain enough pre-charged bytes
3313 * to allow one less page from being charged, but we can't rely
3314 * on the pre-charged bytes not being changed outside of
3315 * consume_obj_stock() or refill_obj_stock(). So ignore those
3316 * pre-charged bytes as well when charging pages. To avoid a
3317 * page uncharge right after a page charge, we set the
3318 * allow_uncharge flag to false when calling refill_obj_stock()
3319 * to temporarily allow the pre-charged bytes to exceed the page
3320 * size limit. The maximum reachable value of the pre-charged
3321 * bytes is (sizeof(object) + PAGE_SIZE - 2) if there is no data
3324 nr_pages = size >> PAGE_SHIFT;
3325 nr_bytes = size & (PAGE_SIZE - 1);
3330 ret = obj_cgroup_charge_pages(objcg, gfp, nr_pages);
3331 if (!ret && nr_bytes)
3332 refill_obj_stock(objcg, PAGE_SIZE - nr_bytes, false);
3337 void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size)
3339 refill_obj_stock(objcg, size, true);
3342 #endif /* CONFIG_MEMCG_KMEM */
3345 * Because page_memcg(head) is not set on tails, set it now.
3347 void split_page_memcg(struct page *head, unsigned int nr)
3349 struct folio *folio = page_folio(head);
3350 struct mem_cgroup *memcg = folio_memcg(folio);
3353 if (mem_cgroup_disabled() || !memcg)
3356 for (i = 1; i < nr; i++)
3357 folio_page(folio, i)->memcg_data = folio->memcg_data;
3359 if (folio_memcg_kmem(folio))
3360 obj_cgroup_get_many(__folio_objcg(folio), nr - 1);
3362 css_get_many(&memcg->css, nr - 1);
3365 #ifdef CONFIG_MEMCG_SWAP
3367 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3368 * @entry: swap entry to be moved
3369 * @from: mem_cgroup which the entry is moved from
3370 * @to: mem_cgroup which the entry is moved to
3372 * It succeeds only when the swap_cgroup's record for this entry is the same
3373 * as the mem_cgroup's id of @from.
3375 * Returns 0 on success, -EINVAL on failure.
3377 * The caller must have charged to @to, IOW, called page_counter_charge() about
3378 * both res and memsw, and called css_get().
3380 static int mem_cgroup_move_swap_account(swp_entry_t entry,
3381 struct mem_cgroup *from, struct mem_cgroup *to)
3383 unsigned short old_id, new_id;
3385 old_id = mem_cgroup_id(from);
3386 new_id = mem_cgroup_id(to);
3388 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
3389 mod_memcg_state(from, MEMCG_SWAP, -1);
3390 mod_memcg_state(to, MEMCG_SWAP, 1);
3396 static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
3397 struct mem_cgroup *from, struct mem_cgroup *to)
3403 static DEFINE_MUTEX(memcg_max_mutex);
3405 static int mem_cgroup_resize_max(struct mem_cgroup *memcg,
3406 unsigned long max, bool memsw)
3408 bool enlarge = false;
3409 bool drained = false;
3411 bool limits_invariant;
3412 struct page_counter *counter = memsw ? &memcg->memsw : &memcg->memory;
3415 if (signal_pending(current)) {
3420 mutex_lock(&memcg_max_mutex);
3422 * Make sure that the new limit (memsw or memory limit) doesn't
3423 * break our basic invariant rule memory.max <= memsw.max.
3425 limits_invariant = memsw ? max >= READ_ONCE(memcg->memory.max) :
3426 max <= memcg->memsw.max;
3427 if (!limits_invariant) {
3428 mutex_unlock(&memcg_max_mutex);
3432 if (max > counter->max)
3434 ret = page_counter_set_max(counter, max);
3435 mutex_unlock(&memcg_max_mutex);
3441 drain_all_stock(memcg);
3446 if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL,
3447 memsw ? 0 : MEMCG_RECLAIM_MAY_SWAP)) {
3453 if (!ret && enlarge)
3454 memcg_oom_recover(memcg);
3459 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
3461 unsigned long *total_scanned)
3463 unsigned long nr_reclaimed = 0;
3464 struct mem_cgroup_per_node *mz, *next_mz = NULL;
3465 unsigned long reclaimed;
3467 struct mem_cgroup_tree_per_node *mctz;
3468 unsigned long excess;
3473 mctz = soft_limit_tree.rb_tree_per_node[pgdat->node_id];
3476 * Do not even bother to check the largest node if the root
3477 * is empty. Do it lockless to prevent lock bouncing. Races
3478 * are acceptable as soft limit is best effort anyway.
3480 if (!mctz || RB_EMPTY_ROOT(&mctz->rb_root))
3484 * This loop can run a while, specially if mem_cgroup's continuously
3485 * keep exceeding their soft limit and putting the system under
3492 mz = mem_cgroup_largest_soft_limit_node(mctz);
3496 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, pgdat,
3497 gfp_mask, total_scanned);
3498 nr_reclaimed += reclaimed;
3499 spin_lock_irq(&mctz->lock);
3502 * If we failed to reclaim anything from this memory cgroup
3503 * it is time to move on to the next cgroup
3507 next_mz = __mem_cgroup_largest_soft_limit_node(mctz);
3509 excess = soft_limit_excess(mz->memcg);
3511 * One school of thought says that we should not add
3512 * back the node to the tree if reclaim returns 0.
3513 * But our reclaim could return 0, simply because due
3514 * to priority we are exposing a smaller subset of
3515 * memory to reclaim from. Consider this as a longer
3518 /* If excess == 0, no tree ops */
3519 __mem_cgroup_insert_exceeded(mz, mctz, excess);
3520 spin_unlock_irq(&mctz->lock);
3521 css_put(&mz->memcg->css);
3524 * Could not reclaim anything and there are no more
3525 * mem cgroups to try or we seem to be looping without
3526 * reclaiming anything.
3528 if (!nr_reclaimed &&
3530 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3532 } while (!nr_reclaimed);
3534 css_put(&next_mz->memcg->css);
3535 return nr_reclaimed;
3539 * Reclaims as many pages from the given memcg as possible.
3541 * Caller is responsible for holding css reference for memcg.
3543 static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
3545 int nr_retries = MAX_RECLAIM_RETRIES;
3547 /* we call try-to-free pages for make this cgroup empty */
3548 lru_add_drain_all();
3550 drain_all_stock(memcg);
3552 /* try to free all pages in this cgroup */
3553 while (nr_retries && page_counter_read(&memcg->memory)) {
3554 if (signal_pending(current))
3557 if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL,
3558 MEMCG_RECLAIM_MAY_SWAP))
3565 static ssize_t mem_cgroup_force_empty_write(struct kernfs_open_file *of,
3566 char *buf, size_t nbytes,
3569 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
3571 if (mem_cgroup_is_root(memcg))
3573 return mem_cgroup_force_empty(memcg) ?: nbytes;
3576 static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
3582 static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
3583 struct cftype *cft, u64 val)
3588 pr_warn_once("Non-hierarchical mode is deprecated. "
3589 "Please report your usecase to linux-mm@kvack.org if you "
3590 "depend on this functionality.\n");
3595 static unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
3599 if (mem_cgroup_is_root(memcg)) {
3600 mem_cgroup_flush_stats();
3601 val = memcg_page_state(memcg, NR_FILE_PAGES) +
3602 memcg_page_state(memcg, NR_ANON_MAPPED);
3604 val += memcg_page_state(memcg, MEMCG_SWAP);
3607 val = page_counter_read(&memcg->memory);
3609 val = page_counter_read(&memcg->memsw);
3622 static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css,
3625 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
3626 struct page_counter *counter;
3628 switch (MEMFILE_TYPE(cft->private)) {
3630 counter = &memcg->memory;
3633 counter = &memcg->memsw;
3636 counter = &memcg->kmem;
3639 counter = &memcg->tcpmem;
3645 switch (MEMFILE_ATTR(cft->private)) {
3647 if (counter == &memcg->memory)
3648 return (u64)mem_cgroup_usage(memcg, false) * PAGE_SIZE;
3649 if (counter == &memcg->memsw)
3650 return (u64)mem_cgroup_usage(memcg, true) * PAGE_SIZE;
3651 return (u64)page_counter_read(counter) * PAGE_SIZE;
3653 return (u64)counter->max * PAGE_SIZE;
3655 return (u64)counter->watermark * PAGE_SIZE;
3657 return counter->failcnt;
3658 case RES_SOFT_LIMIT:
3659 return (u64)memcg->soft_limit * PAGE_SIZE;
3665 #ifdef CONFIG_MEMCG_KMEM
3666 static int memcg_online_kmem(struct mem_cgroup *memcg)
3668 struct obj_cgroup *objcg;
3670 if (mem_cgroup_kmem_disabled())
3673 if (unlikely(mem_cgroup_is_root(memcg)))
3676 objcg = obj_cgroup_alloc();
3680 objcg->memcg = memcg;
3681 rcu_assign_pointer(memcg->objcg, objcg);
3683 static_branch_enable(&memcg_kmem_enabled_key);
3685 memcg->kmemcg_id = memcg->id.id;
3690 static void memcg_offline_kmem(struct mem_cgroup *memcg)
3692 struct mem_cgroup *parent;
3694 if (mem_cgroup_kmem_disabled())
3697 if (unlikely(mem_cgroup_is_root(memcg)))
3700 parent = parent_mem_cgroup(memcg);
3702 parent = root_mem_cgroup;
3704 memcg_reparent_objcgs(memcg, parent);
3707 * After we have finished memcg_reparent_objcgs(), all list_lrus
3708 * corresponding to this cgroup are guaranteed to remain empty.
3709 * The ordering is imposed by list_lru_node->lock taken by
3710 * memcg_reparent_list_lrus().
3712 memcg_reparent_list_lrus(memcg, parent);
3715 static int memcg_online_kmem(struct mem_cgroup *memcg)
3719 static void memcg_offline_kmem(struct mem_cgroup *memcg)
3722 #endif /* CONFIG_MEMCG_KMEM */
3724 static int memcg_update_tcp_max(struct mem_cgroup *memcg, unsigned long max)
3728 mutex_lock(&memcg_max_mutex);
3730 ret = page_counter_set_max(&memcg->tcpmem, max);
3734 if (!memcg->tcpmem_active) {
3736 * The active flag needs to be written after the static_key
3737 * update. This is what guarantees that the socket activation
3738 * function is the last one to run. See mem_cgroup_sk_alloc()
3739 * for details, and note that we don't mark any socket as
3740 * belonging to this memcg until that flag is up.
3742 * We need to do this, because static_keys will span multiple
3743 * sites, but we can't control their order. If we mark a socket
3744 * as accounted, but the accounting functions are not patched in
3745 * yet, we'll lose accounting.
3747 * We never race with the readers in mem_cgroup_sk_alloc(),
3748 * because when this value change, the code to process it is not
3751 static_branch_inc(&memcg_sockets_enabled_key);
3752 memcg->tcpmem_active = true;
3755 mutex_unlock(&memcg_max_mutex);
3760 * The user of this function is...
3763 static ssize_t mem_cgroup_write(struct kernfs_open_file *of,
3764 char *buf, size_t nbytes, loff_t off)
3766 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
3767 unsigned long nr_pages;
3770 buf = strstrip(buf);
3771 ret = page_counter_memparse(buf, "-1", &nr_pages);
3775 switch (MEMFILE_ATTR(of_cft(of)->private)) {
3777 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3781 switch (MEMFILE_TYPE(of_cft(of)->private)) {
3783 ret = mem_cgroup_resize_max(memcg, nr_pages, false);
3786 ret = mem_cgroup_resize_max(memcg, nr_pages, true);
3789 /* kmem.limit_in_bytes is deprecated. */
3793 ret = memcg_update_tcp_max(memcg, nr_pages);
3797 case RES_SOFT_LIMIT:
3798 if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
3801 memcg->soft_limit = nr_pages;
3806 return ret ?: nbytes;
3809 static ssize_t mem_cgroup_reset(struct kernfs_open_file *of, char *buf,
3810 size_t nbytes, loff_t off)
3812 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
3813 struct page_counter *counter;
3815 switch (MEMFILE_TYPE(of_cft(of)->private)) {
3817 counter = &memcg->memory;
3820 counter = &memcg->memsw;
3823 counter = &memcg->kmem;
3826 counter = &memcg->tcpmem;
3832 switch (MEMFILE_ATTR(of_cft(of)->private)) {
3834 page_counter_reset_watermark(counter);
3837 counter->failcnt = 0;
3846 static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
3849 return mem_cgroup_from_css(css)->move_charge_at_immigrate;
3853 static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
3854 struct cftype *cft, u64 val)
3856 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
3858 if (val & ~MOVE_MASK)
3862 * No kind of locking is needed in here, because ->can_attach() will
3863 * check this value once in the beginning of the process, and then carry
3864 * on with stale data. This means that changes to this value will only
3865 * affect task migrations starting after the change.
3867 memcg->move_charge_at_immigrate = val;
3871 static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
3872 struct cftype *cft, u64 val)
3880 #define LRU_ALL_FILE (BIT(LRU_INACTIVE_FILE) | BIT(LRU_ACTIVE_FILE))
3881 #define LRU_ALL_ANON (BIT(LRU_INACTIVE_ANON) | BIT(LRU_ACTIVE_ANON))
3882 #define LRU_ALL ((1 << NR_LRU_LISTS) - 1)
3884 static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
3885 int nid, unsigned int lru_mask, bool tree)
3887 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid));
3888 unsigned long nr = 0;
3891 VM_BUG_ON((unsigned)nid >= nr_node_ids);
3894 if (!(BIT(lru) & lru_mask))
3897 nr += lruvec_page_state(lruvec, NR_LRU_BASE + lru);
3899 nr += lruvec_page_state_local(lruvec, NR_LRU_BASE + lru);
3904 static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
3905 unsigned int lru_mask,
3908 unsigned long nr = 0;
3912 if (!(BIT(lru) & lru_mask))
3915 nr += memcg_page_state(memcg, NR_LRU_BASE + lru);
3917 nr += memcg_page_state_local(memcg, NR_LRU_BASE + lru);
3922 static int memcg_numa_stat_show(struct seq_file *m, void *v)
3926 unsigned int lru_mask;
3929 static const struct numa_stat stats[] = {
3930 { "total", LRU_ALL },
3931 { "file", LRU_ALL_FILE },
3932 { "anon", LRU_ALL_ANON },
3933 { "unevictable", BIT(LRU_UNEVICTABLE) },
3935 const struct numa_stat *stat;
3937 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
3939 mem_cgroup_flush_stats();
3941 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
3942 seq_printf(m, "%s=%lu", stat->name,
3943 mem_cgroup_nr_lru_pages(memcg, stat->lru_mask,
3945 for_each_node_state(nid, N_MEMORY)
3946 seq_printf(m, " N%d=%lu", nid,
3947 mem_cgroup_node_nr_lru_pages(memcg, nid,
3948 stat->lru_mask, false));
3952 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
3954 seq_printf(m, "hierarchical_%s=%lu", stat->name,
3955 mem_cgroup_nr_lru_pages(memcg, stat->lru_mask,
3957 for_each_node_state(nid, N_MEMORY)
3958 seq_printf(m, " N%d=%lu", nid,
3959 mem_cgroup_node_nr_lru_pages(memcg, nid,
3960 stat->lru_mask, true));
3966 #endif /* CONFIG_NUMA */
3968 static const unsigned int memcg1_stats[] = {
3971 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
3981 static const char *const memcg1_stat_names[] = {
3984 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
3994 /* Universal VM events cgroup1 shows, original sort order */
3995 static const unsigned int memcg1_events[] = {
4002 static int memcg_stat_show(struct seq_file *m, void *v)
4004 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
4005 unsigned long memory, memsw;
4006 struct mem_cgroup *mi;
4009 BUILD_BUG_ON(ARRAY_SIZE(memcg1_stat_names) != ARRAY_SIZE(memcg1_stats));
4011 mem_cgroup_flush_stats();
4013 for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) {
4016 if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account())
4018 nr = memcg_page_state_local(memcg, memcg1_stats[i]);
4019 seq_printf(m, "%s %lu\n", memcg1_stat_names[i], nr * PAGE_SIZE);
4022 for (i = 0; i < ARRAY_SIZE(memcg1_events); i++)
4023 seq_printf(m, "%s %lu\n", vm_event_name(memcg1_events[i]),
4024 memcg_events_local(memcg, memcg1_events[i]));
4026 for (i = 0; i < NR_LRU_LISTS; i++)
4027 seq_printf(m, "%s %lu\n", lru_list_name(i),
4028 memcg_page_state_local(memcg, NR_LRU_BASE + i) *
4031 /* Hierarchical information */
4032 memory = memsw = PAGE_COUNTER_MAX;
4033 for (mi = memcg; mi; mi = parent_mem_cgroup(mi)) {
4034 memory = min(memory, READ_ONCE(mi->memory.max));
4035 memsw = min(memsw, READ_ONCE(mi->memsw.max));
4037 seq_printf(m, "hierarchical_memory_limit %llu\n",
4038 (u64)memory * PAGE_SIZE);
4039 if (do_memsw_account())
4040 seq_printf(m, "hierarchical_memsw_limit %llu\n",
4041 (u64)memsw * PAGE_SIZE);
4043 for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) {
4046 if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account())
4048 nr = memcg_page_state(memcg, memcg1_stats[i]);
4049 seq_printf(m, "total_%s %llu\n", memcg1_stat_names[i],
4050 (u64)nr * PAGE_SIZE);
4053 for (i = 0; i < ARRAY_SIZE(memcg1_events); i++)
4054 seq_printf(m, "total_%s %llu\n",
4055 vm_event_name(memcg1_events[i]),
4056 (u64)memcg_events(memcg, memcg1_events[i]));
4058 for (i = 0; i < NR_LRU_LISTS; i++)
4059 seq_printf(m, "total_%s %llu\n", lru_list_name(i),
4060 (u64)memcg_page_state(memcg, NR_LRU_BASE + i) *
4063 #ifdef CONFIG_DEBUG_VM
4066 struct mem_cgroup_per_node *mz;
4067 unsigned long anon_cost = 0;
4068 unsigned long file_cost = 0;
4070 for_each_online_pgdat(pgdat) {
4071 mz = memcg->nodeinfo[pgdat->node_id];
4073 anon_cost += mz->lruvec.anon_cost;
4074 file_cost += mz->lruvec.file_cost;
4076 seq_printf(m, "anon_cost %lu\n", anon_cost);
4077 seq_printf(m, "file_cost %lu\n", file_cost);
4084 static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
4087 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
4089 return mem_cgroup_swappiness(memcg);
4092 static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
4093 struct cftype *cft, u64 val)
4095 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
4100 if (!mem_cgroup_is_root(memcg))
4101 memcg->swappiness = val;
4103 vm_swappiness = val;
4108 static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4110 struct mem_cgroup_threshold_ary *t;
4111 unsigned long usage;
4116 t = rcu_dereference(memcg->thresholds.primary);
4118 t = rcu_dereference(memcg->memsw_thresholds.primary);
4123 usage = mem_cgroup_usage(memcg, swap);
4126 * current_threshold points to threshold just below or equal to usage.
4127 * If it's not true, a threshold was crossed after last
4128 * call of __mem_cgroup_threshold().
4130 i = t->current_threshold;
4133 * Iterate backward over array of thresholds starting from
4134 * current_threshold and check if a threshold is crossed.
4135 * If none of thresholds below usage is crossed, we read
4136 * only one element of the array here.
4138 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4139 eventfd_signal(t->entries[i].eventfd, 1);
4141 /* i = current_threshold + 1 */
4145 * Iterate forward over array of thresholds starting from
4146 * current_threshold+1 and check if a threshold is crossed.
4147 * If none of thresholds above usage is crossed, we read
4148 * only one element of the array here.
4150 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4151 eventfd_signal(t->entries[i].eventfd, 1);
4153 /* Update current_threshold */
4154 t->current_threshold = i - 1;
4159 static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4162 __mem_cgroup_threshold(memcg, false);
4163 if (do_memsw_account())
4164 __mem_cgroup_threshold(memcg, true);
4166 memcg = parent_mem_cgroup(memcg);
4170 static int compare_thresholds(const void *a, const void *b)
4172 const struct mem_cgroup_threshold *_a = a;
4173 const struct mem_cgroup_threshold *_b = b;
4175 if (_a->threshold > _b->threshold)
4178 if (_a->threshold < _b->threshold)
4184 static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
4186 struct mem_cgroup_eventfd_list *ev;
4188 spin_lock(&memcg_oom_lock);
4190 list_for_each_entry(ev, &memcg->oom_notify, list)
4191 eventfd_signal(ev->eventfd, 1);
4193 spin_unlock(&memcg_oom_lock);
4197 static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
4199 struct mem_cgroup *iter;
4201 for_each_mem_cgroup_tree(iter, memcg)
4202 mem_cgroup_oom_notify_cb(iter);
4205 static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
4206 struct eventfd_ctx *eventfd, const char *args, enum res_type type)
4208 struct mem_cgroup_thresholds *thresholds;
4209 struct mem_cgroup_threshold_ary *new;
4210 unsigned long threshold;
4211 unsigned long usage;
4214 ret = page_counter_memparse(args, "-1", &threshold);
4218 mutex_lock(&memcg->thresholds_lock);
4221 thresholds = &memcg->thresholds;
4222 usage = mem_cgroup_usage(memcg, false);
4223 } else if (type == _MEMSWAP) {
4224 thresholds = &memcg->memsw_thresholds;
4225 usage = mem_cgroup_usage(memcg, true);
4229 /* Check if a threshold crossed before adding a new one */
4230 if (thresholds->primary)
4231 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4233 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
4235 /* Allocate memory for new array of thresholds */
4236 new = kmalloc(struct_size(new, entries, size), GFP_KERNEL);
4243 /* Copy thresholds (if any) to new array */
4244 if (thresholds->primary)
4245 memcpy(new->entries, thresholds->primary->entries,
4246 flex_array_size(new, entries, size - 1));
4248 /* Add new threshold */
4249 new->entries[size - 1].eventfd = eventfd;
4250 new->entries[size - 1].threshold = threshold;
4252 /* Sort thresholds. Registering of new threshold isn't time-critical */
4253 sort(new->entries, size, sizeof(*new->entries),
4254 compare_thresholds, NULL);
4256 /* Find current threshold */
4257 new->current_threshold = -1;
4258 for (i = 0; i < size; i++) {
4259 if (new->entries[i].threshold <= usage) {
4261 * new->current_threshold will not be used until
4262 * rcu_assign_pointer(), so it's safe to increment
4265 ++new->current_threshold;
4270 /* Free old spare buffer and save old primary buffer as spare */
4271 kfree(thresholds->spare);
4272 thresholds->spare = thresholds->primary;
4274 rcu_assign_pointer(thresholds->primary, new);
4276 /* To be sure that nobody uses thresholds */
4280 mutex_unlock(&memcg->thresholds_lock);
4285 static int mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
4286 struct eventfd_ctx *eventfd, const char *args)
4288 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEM);
4291 static int memsw_cgroup_usage_register_event(struct mem_cgroup *memcg,
4292 struct eventfd_ctx *eventfd, const char *args)
4294 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEMSWAP);
4297 static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
4298 struct eventfd_ctx *eventfd, enum res_type type)
4300 struct mem_cgroup_thresholds *thresholds;
4301 struct mem_cgroup_threshold_ary *new;
4302 unsigned long usage;
4303 int i, j, size, entries;
4305 mutex_lock(&memcg->thresholds_lock);
4308 thresholds = &memcg->thresholds;
4309 usage = mem_cgroup_usage(memcg, false);
4310 } else if (type == _MEMSWAP) {
4311 thresholds = &memcg->memsw_thresholds;
4312 usage = mem_cgroup_usage(memcg, true);
4316 if (!thresholds->primary)
4319 /* Check if a threshold crossed before removing */
4320 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4322 /* Calculate new number of threshold */
4324 for (i = 0; i < thresholds->primary->size; i++) {
4325 if (thresholds->primary->entries[i].eventfd != eventfd)
4331 new = thresholds->spare;
4333 /* If no items related to eventfd have been cleared, nothing to do */
4337 /* Set thresholds array to NULL if we don't have thresholds */
4346 /* Copy thresholds and find current threshold */
4347 new->current_threshold = -1;
4348 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4349 if (thresholds->primary->entries[i].eventfd == eventfd)
4352 new->entries[j] = thresholds->primary->entries[i];
4353 if (new->entries[j].threshold <= usage) {
4355 * new->current_threshold will not be used
4356 * until rcu_assign_pointer(), so it's safe to increment
4359 ++new->current_threshold;
4365 /* Swap primary and spare array */
4366 thresholds->spare = thresholds->primary;
4368 rcu_assign_pointer(thresholds->primary, new);
4370 /* To be sure that nobody uses thresholds */
4373 /* If all events are unregistered, free the spare array */
4375 kfree(thresholds->spare);
4376 thresholds->spare = NULL;
4379 mutex_unlock(&memcg->thresholds_lock);
4382 static void mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
4383 struct eventfd_ctx *eventfd)
4385 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEM);
4388 static void memsw_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
4389 struct eventfd_ctx *eventfd)
4391 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEMSWAP);
4394 static int mem_cgroup_oom_register_event(struct mem_cgroup *memcg,
4395 struct eventfd_ctx *eventfd, const char *args)
4397 struct mem_cgroup_eventfd_list *event;
4399 event = kmalloc(sizeof(*event), GFP_KERNEL);
4403 spin_lock(&memcg_oom_lock);
4405 event->eventfd = eventfd;
4406 list_add(&event->list, &memcg->oom_notify);
4408 /* already in OOM ? */
4409 if (memcg->under_oom)
4410 eventfd_signal(eventfd, 1);
4411 spin_unlock(&memcg_oom_lock);
4416 static void mem_cgroup_oom_unregister_event(struct mem_cgroup *memcg,
4417 struct eventfd_ctx *eventfd)
4419 struct mem_cgroup_eventfd_list *ev, *tmp;
4421 spin_lock(&memcg_oom_lock);
4423 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
4424 if (ev->eventfd == eventfd) {
4425 list_del(&ev->list);
4430 spin_unlock(&memcg_oom_lock);
4433 static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v)
4435 struct mem_cgroup *memcg = mem_cgroup_from_seq(sf);
4437 seq_printf(sf, "oom_kill_disable %d\n", memcg->oom_kill_disable);
4438 seq_printf(sf, "under_oom %d\n", (bool)memcg->under_oom);
4439 seq_printf(sf, "oom_kill %lu\n",
4440 atomic_long_read(&memcg->memory_events[MEMCG_OOM_KILL]));
4444 static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
4445 struct cftype *cft, u64 val)
4447 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
4449 /* cannot set to root cgroup and only 0 and 1 are allowed */
4450 if (mem_cgroup_is_root(memcg) || !((val == 0) || (val == 1)))
4453 memcg->oom_kill_disable = val;
4455 memcg_oom_recover(memcg);
4460 #ifdef CONFIG_CGROUP_WRITEBACK
4462 #include <trace/events/writeback.h>
4464 static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp)
4466 return wb_domain_init(&memcg->cgwb_domain, gfp);
4469 static void memcg_wb_domain_exit(struct mem_cgroup *memcg)
4471 wb_domain_exit(&memcg->cgwb_domain);
4474 static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)
4476 wb_domain_size_changed(&memcg->cgwb_domain);
4479 struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
4481 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
4483 if (!memcg->css.parent)
4486 return &memcg->cgwb_domain;
4490 * mem_cgroup_wb_stats - retrieve writeback related stats from its memcg
4491 * @wb: bdi_writeback in question
4492 * @pfilepages: out parameter for number of file pages
4493 * @pheadroom: out parameter for number of allocatable pages according to memcg
4494 * @pdirty: out parameter for number of dirty pages
4495 * @pwriteback: out parameter for number of pages under writeback
4497 * Determine the numbers of file, headroom, dirty, and writeback pages in
4498 * @wb's memcg. File, dirty and writeback are self-explanatory. Headroom
4499 * is a bit more involved.
4501 * A memcg's headroom is "min(max, high) - used". In the hierarchy, the
4502 * headroom is calculated as the lowest headroom of itself and the
4503 * ancestors. Note that this doesn't consider the actual amount of
4504 * available memory in the system. The caller should further cap
4505 * *@pheadroom accordingly.
4507 void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
4508 unsigned long *pheadroom, unsigned long *pdirty,
4509 unsigned long *pwriteback)
4511 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
4512 struct mem_cgroup *parent;
4514 mem_cgroup_flush_stats();
4516 *pdirty = memcg_page_state(memcg, NR_FILE_DIRTY);
4517 *pwriteback = memcg_page_state(memcg, NR_WRITEBACK);
4518 *pfilepages = memcg_page_state(memcg, NR_INACTIVE_FILE) +
4519 memcg_page_state(memcg, NR_ACTIVE_FILE);
4521 *pheadroom = PAGE_COUNTER_MAX;
4522 while ((parent = parent_mem_cgroup(memcg))) {
4523 unsigned long ceiling = min(READ_ONCE(memcg->memory.max),
4524 READ_ONCE(memcg->memory.high));
4525 unsigned long used = page_counter_read(&memcg->memory);
4527 *pheadroom = min(*pheadroom, ceiling - min(ceiling, used));
4533 * Foreign dirty flushing
4535 * There's an inherent mismatch between memcg and writeback. The former
4536 * tracks ownership per-page while the latter per-inode. This was a
4537 * deliberate design decision because honoring per-page ownership in the
4538 * writeback path is complicated, may lead to higher CPU and IO overheads
4539 * and deemed unnecessary given that write-sharing an inode across
4540 * different cgroups isn't a common use-case.
4542 * Combined with inode majority-writer ownership switching, this works well
4543 * enough in most cases but there are some pathological cases. For
4544 * example, let's say there are two cgroups A and B which keep writing to
4545 * different but confined parts of the same inode. B owns the inode and
4546 * A's memory is limited far below B's. A's dirty ratio can rise enough to
4547 * trigger balance_dirty_pages() sleeps but B's can be low enough to avoid
4548 * triggering background writeback. A will be slowed down without a way to
4549 * make writeback of the dirty pages happen.
4551 * Conditions like the above can lead to a cgroup getting repeatedly and
4552 * severely throttled after making some progress after each
4553 * dirty_expire_interval while the underlying IO device is almost
4556 * Solving this problem completely requires matching the ownership tracking
4557 * granularities between memcg and writeback in either direction. However,
4558 * the more egregious behaviors can be avoided by simply remembering the
4559 * most recent foreign dirtying events and initiating remote flushes on
4560 * them when local writeback isn't enough to keep the memory clean enough.
4562 * The following two functions implement such mechanism. When a foreign
4563 * page - a page whose memcg and writeback ownerships don't match - is
4564 * dirtied, mem_cgroup_track_foreign_dirty() records the inode owning
4565 * bdi_writeback on the page owning memcg. When balance_dirty_pages()
4566 * decides that the memcg needs to sleep due to high dirty ratio, it calls
4567 * mem_cgroup_flush_foreign() which queues writeback on the recorded
4568 * foreign bdi_writebacks which haven't expired. Both the numbers of
4569 * recorded bdi_writebacks and concurrent in-flight foreign writebacks are
4570 * limited to MEMCG_CGWB_FRN_CNT.
4572 * The mechanism only remembers IDs and doesn't hold any object references.
4573 * As being wrong occasionally doesn't matter, updates and accesses to the
4574 * records are lockless and racy.
4576 void mem_cgroup_track_foreign_dirty_slowpath(struct folio *folio,
4577 struct bdi_writeback *wb)
4579 struct mem_cgroup *memcg = folio_memcg(folio);
4580 struct memcg_cgwb_frn *frn;
4581 u64 now = get_jiffies_64();
4582 u64 oldest_at = now;
4586 trace_track_foreign_dirty(folio, wb);
4589 * Pick the slot to use. If there is already a slot for @wb, keep
4590 * using it. If not replace the oldest one which isn't being
4593 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) {
4594 frn = &memcg->cgwb_frn[i];
4595 if (frn->bdi_id == wb->bdi->id &&
4596 frn->memcg_id == wb->memcg_css->id)
4598 if (time_before64(frn->at, oldest_at) &&
4599 atomic_read(&frn->done.cnt) == 1) {
4601 oldest_at = frn->at;
4605 if (i < MEMCG_CGWB_FRN_CNT) {
4607 * Re-using an existing one. Update timestamp lazily to
4608 * avoid making the cacheline hot. We want them to be
4609 * reasonably up-to-date and significantly shorter than
4610 * dirty_expire_interval as that's what expires the record.
4611 * Use the shorter of 1s and dirty_expire_interval / 8.
4613 unsigned long update_intv =
4614 min_t(unsigned long, HZ,
4615 msecs_to_jiffies(dirty_expire_interval * 10) / 8);
4617 if (time_before64(frn->at, now - update_intv))
4619 } else if (oldest >= 0) {
4620 /* replace the oldest free one */
4621 frn = &memcg->cgwb_frn[oldest];
4622 frn->bdi_id = wb->bdi->id;
4623 frn->memcg_id = wb->memcg_css->id;
4628 /* issue foreign writeback flushes for recorded foreign dirtying events */
4629 void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
4631 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
4632 unsigned long intv = msecs_to_jiffies(dirty_expire_interval * 10);
4633 u64 now = jiffies_64;
4636 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) {
4637 struct memcg_cgwb_frn *frn = &memcg->cgwb_frn[i];
4640 * If the record is older than dirty_expire_interval,
4641 * writeback on it has already started. No need to kick it
4642 * off again. Also, don't start a new one if there's
4643 * already one in flight.
4645 if (time_after64(frn->at, now - intv) &&
4646 atomic_read(&frn->done.cnt) == 1) {
4648 trace_flush_foreign(wb, frn->bdi_id, frn->memcg_id);
4649 cgroup_writeback_by_id(frn->bdi_id, frn->memcg_id,
4650 WB_REASON_FOREIGN_FLUSH,
4656 #else /* CONFIG_CGROUP_WRITEBACK */
4658 static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp)
4663 static void memcg_wb_domain_exit(struct mem_cgroup *memcg)
4667 static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)
4671 #endif /* CONFIG_CGROUP_WRITEBACK */
4674 * DO NOT USE IN NEW FILES.
4676 * "cgroup.event_control" implementation.
4678 * This is way over-engineered. It tries to support fully configurable
4679 * events for each user. Such level of flexibility is completely
4680 * unnecessary especially in the light of the planned unified hierarchy.
4682 * Please deprecate this and replace with something simpler if at all
4687 * Unregister event and free resources.
4689 * Gets called from workqueue.
4691 static void memcg_event_remove(struct work_struct *work)
4693 struct mem_cgroup_event *event =
4694 container_of(work, struct mem_cgroup_event, remove);
4695 struct mem_cgroup *memcg = event->memcg;
4697 remove_wait_queue(event->wqh, &event->wait);
4699 event->unregister_event(memcg, event->eventfd);
4701 /* Notify userspace the event is going away. */
4702 eventfd_signal(event->eventfd, 1);
4704 eventfd_ctx_put(event->eventfd);
4706 css_put(&memcg->css);
4710 * Gets called on EPOLLHUP on eventfd when user closes it.
4712 * Called with wqh->lock held and interrupts disabled.
4714 static int memcg_event_wake(wait_queue_entry_t *wait, unsigned mode,
4715 int sync, void *key)
4717 struct mem_cgroup_event *event =
4718 container_of(wait, struct mem_cgroup_event, wait);
4719 struct mem_cgroup *memcg = event->memcg;
4720 __poll_t flags = key_to_poll(key);
4722 if (flags & EPOLLHUP) {
4724 * If the event has been detached at cgroup removal, we
4725 * can simply return knowing the other side will cleanup
4728 * We can't race against event freeing since the other
4729 * side will require wqh->lock via remove_wait_queue(),
4732 spin_lock(&memcg->event_list_lock);
4733 if (!list_empty(&event->list)) {
4734 list_del_init(&event->list);
4736 * We are in atomic context, but cgroup_event_remove()
4737 * may sleep, so we have to call it in workqueue.
4739 schedule_work(&event->remove);
4741 spin_unlock(&memcg->event_list_lock);
4747 static void memcg_event_ptable_queue_proc(struct file *file,
4748 wait_queue_head_t *wqh, poll_table *pt)
4750 struct mem_cgroup_event *event =
4751 container_of(pt, struct mem_cgroup_event, pt);
4754 add_wait_queue(wqh, &event->wait);
4758 * DO NOT USE IN NEW FILES.
4760 * Parse input and register new cgroup event handler.
4762 * Input must be in format '<event_fd> <control_fd> <args>'.
4763 * Interpretation of args is defined by control file implementation.
4765 static ssize_t memcg_write_event_control(struct kernfs_open_file *of,
4766 char *buf, size_t nbytes, loff_t off)
4768 struct cgroup_subsys_state *css = of_css(of);
4769 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
4770 struct mem_cgroup_event *event;
4771 struct cgroup_subsys_state *cfile_css;
4772 unsigned int efd, cfd;
4779 if (IS_ENABLED(CONFIG_PREEMPT_RT))
4782 buf = strstrip(buf);
4784 efd = simple_strtoul(buf, &endp, 10);
4789 cfd = simple_strtoul(buf, &endp, 10);
4790 if ((*endp != ' ') && (*endp != '\0'))
4794 event = kzalloc(sizeof(*event), GFP_KERNEL);
4798 event->memcg = memcg;
4799 INIT_LIST_HEAD(&event->list);
4800 init_poll_funcptr(&event->pt, memcg_event_ptable_queue_proc);
4801 init_waitqueue_func_entry(&event->wait, memcg_event_wake);
4802 INIT_WORK(&event->remove, memcg_event_remove);
4810 event->eventfd = eventfd_ctx_fileget(efile.file);
4811 if (IS_ERR(event->eventfd)) {
4812 ret = PTR_ERR(event->eventfd);
4819 goto out_put_eventfd;
4822 /* the process need read permission on control file */
4823 /* AV: shouldn't we check that it's been opened for read instead? */
4824 ret = file_permission(cfile.file, MAY_READ);
4829 * Determine the event callbacks and set them in @event. This used
4830 * to be done via struct cftype but cgroup core no longer knows
4831 * about these events. The following is crude but the whole thing
4832 * is for compatibility anyway.
4834 * DO NOT ADD NEW FILES.
4836 name = cfile.file->f_path.dentry->d_name.name;
4838 if (!strcmp(name, "memory.usage_in_bytes")) {
4839 event->register_event = mem_cgroup_usage_register_event;
4840 event->unregister_event = mem_cgroup_usage_unregister_event;
4841 } else if (!strcmp(name, "memory.oom_control")) {
4842 event->register_event = mem_cgroup_oom_register_event;
4843 event->unregister_event = mem_cgroup_oom_unregister_event;
4844 } else if (!strcmp(name, "memory.pressure_level")) {
4845 event->register_event = vmpressure_register_event;
4846 event->unregister_event = vmpressure_unregister_event;
4847 } else if (!strcmp(name, "memory.memsw.usage_in_bytes")) {
4848 event->register_event = memsw_cgroup_usage_register_event;
4849 event->unregister_event = memsw_cgroup_usage_unregister_event;
4856 * Verify @cfile should belong to @css. Also, remaining events are
4857 * automatically removed on cgroup destruction but the removal is
4858 * asynchronous, so take an extra ref on @css.
4860 cfile_css = css_tryget_online_from_dir(cfile.file->f_path.dentry->d_parent,
4861 &memory_cgrp_subsys);
4863 if (IS_ERR(cfile_css))
4865 if (cfile_css != css) {
4870 ret = event->register_event(memcg, event->eventfd, buf);
4874 vfs_poll(efile.file, &event->pt);
4876 spin_lock_irq(&memcg->event_list_lock);
4877 list_add(&event->list, &memcg->event_list);
4878 spin_unlock_irq(&memcg->event_list_lock);
4890 eventfd_ctx_put(event->eventfd);
4899 #if defined(CONFIG_MEMCG_KMEM) && (defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG))
4900 static int mem_cgroup_slab_show(struct seq_file *m, void *p)
4904 * Please, take a look at tools/cgroup/memcg_slabinfo.py .
4910 static struct cftype mem_cgroup_legacy_files[] = {
4912 .name = "usage_in_bytes",
4913 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
4914 .read_u64 = mem_cgroup_read_u64,
4917 .name = "max_usage_in_bytes",
4918 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
4919 .write = mem_cgroup_reset,
4920 .read_u64 = mem_cgroup_read_u64,
4923 .name = "limit_in_bytes",
4924 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
4925 .write = mem_cgroup_write,
4926 .read_u64 = mem_cgroup_read_u64,
4929 .name = "soft_limit_in_bytes",
4930 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
4931 .write = mem_cgroup_write,
4932 .read_u64 = mem_cgroup_read_u64,
4936 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
4937 .write = mem_cgroup_reset,
4938 .read_u64 = mem_cgroup_read_u64,
4942 .seq_show = memcg_stat_show,
4945 .name = "force_empty",
4946 .write = mem_cgroup_force_empty_write,
4949 .name = "use_hierarchy",
4950 .write_u64 = mem_cgroup_hierarchy_write,
4951 .read_u64 = mem_cgroup_hierarchy_read,
4954 .name = "cgroup.event_control", /* XXX: for compat */
4955 .write = memcg_write_event_control,
4956 .flags = CFTYPE_NO_PREFIX | CFTYPE_WORLD_WRITABLE,
4959 .name = "swappiness",
4960 .read_u64 = mem_cgroup_swappiness_read,
4961 .write_u64 = mem_cgroup_swappiness_write,
4964 .name = "move_charge_at_immigrate",
4965 .read_u64 = mem_cgroup_move_charge_read,
4966 .write_u64 = mem_cgroup_move_charge_write,
4969 .name = "oom_control",
4970 .seq_show = mem_cgroup_oom_control_read,
4971 .write_u64 = mem_cgroup_oom_control_write,
4974 .name = "pressure_level",
4978 .name = "numa_stat",
4979 .seq_show = memcg_numa_stat_show,
4983 .name = "kmem.limit_in_bytes",
4984 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
4985 .write = mem_cgroup_write,
4986 .read_u64 = mem_cgroup_read_u64,
4989 .name = "kmem.usage_in_bytes",
4990 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
4991 .read_u64 = mem_cgroup_read_u64,
4994 .name = "kmem.failcnt",
4995 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
4996 .write = mem_cgroup_reset,
4997 .read_u64 = mem_cgroup_read_u64,
5000 .name = "kmem.max_usage_in_bytes",
5001 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
5002 .write = mem_cgroup_reset,
5003 .read_u64 = mem_cgroup_read_u64,
5005 #if defined(CONFIG_MEMCG_KMEM) && \
5006 (defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG))
5008 .name = "kmem.slabinfo",
5009 .seq_show = mem_cgroup_slab_show,
5013 .name = "kmem.tcp.limit_in_bytes",
5014 .private = MEMFILE_PRIVATE(_TCP, RES_LIMIT),
5015 .write = mem_cgroup_write,
5016 .read_u64 = mem_cgroup_read_u64,
5019 .name = "kmem.tcp.usage_in_bytes",
5020 .private = MEMFILE_PRIVATE(_TCP, RES_USAGE),
5021 .read_u64 = mem_cgroup_read_u64,
5024 .name = "kmem.tcp.failcnt",
5025 .private = MEMFILE_PRIVATE(_TCP, RES_FAILCNT),
5026 .write = mem_cgroup_reset,
5027 .read_u64 = mem_cgroup_read_u64,
5030 .name = "kmem.tcp.max_usage_in_bytes",
5031 .private = MEMFILE_PRIVATE(_TCP, RES_MAX_USAGE),
5032 .write = mem_cgroup_reset,
5033 .read_u64 = mem_cgroup_read_u64,
5035 { }, /* terminate */
5039 * Private memory cgroup IDR
5041 * Swap-out records and page cache shadow entries need to store memcg
5042 * references in constrained space, so we maintain an ID space that is
5043 * limited to 16 bit (MEM_CGROUP_ID_MAX), limiting the total number of
5044 * memory-controlled cgroups to 64k.
5046 * However, there usually are many references to the offline CSS after
5047 * the cgroup has been destroyed, such as page cache or reclaimable
5048 * slab objects, that don't need to hang on to the ID. We want to keep
5049 * those dead CSS from occupying IDs, or we might quickly exhaust the
5050 * relatively small ID space and prevent the creation of new cgroups
5051 * even when there are much fewer than 64k cgroups - possibly none.
5053 * Maintain a private 16-bit ID space for memcg, and allow the ID to
5054 * be freed and recycled when it's no longer needed, which is usually
5055 * when the CSS is offlined.
5057 * The only exception to that are records of swapped out tmpfs/shmem
5058 * pages that need to be attributed to live ancestors on swapin. But
5059 * those references are manageable from userspace.
5062 static DEFINE_IDR(mem_cgroup_idr);
5064 static void mem_cgroup_id_remove(struct mem_cgroup *memcg)
5066 if (memcg->id.id > 0) {
5067 idr_remove(&mem_cgroup_idr, memcg->id.id);
5072 static void __maybe_unused mem_cgroup_id_get_many(struct mem_cgroup *memcg,
5075 refcount_add(n, &memcg->id.ref);
5078 static void mem_cgroup_id_put_many(struct mem_cgroup *memcg, unsigned int n)
5080 if (refcount_sub_and_test(n, &memcg->id.ref)) {
5081 mem_cgroup_id_remove(memcg);
5083 /* Memcg ID pins CSS */
5084 css_put(&memcg->css);
5088 static inline void mem_cgroup_id_put(struct mem_cgroup *memcg)
5090 mem_cgroup_id_put_many(memcg, 1);
5094 * mem_cgroup_from_id - look up a memcg from a memcg id
5095 * @id: the memcg id to look up
5097 * Caller must hold rcu_read_lock().
5099 struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
5101 WARN_ON_ONCE(!rcu_read_lock_held());
5102 return idr_find(&mem_cgroup_idr, id);
5105 #ifdef CONFIG_SHRINKER_DEBUG
5106 struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino)
5108 struct cgroup *cgrp;
5109 struct cgroup_subsys_state *css;
5110 struct mem_cgroup *memcg;
5112 cgrp = cgroup_get_from_id(ino);
5114 return ERR_PTR(-ENOENT);
5116 css = cgroup_get_e_css(cgrp, &memory_cgrp_subsys);
5118 memcg = container_of(css, struct mem_cgroup, css);
5120 memcg = ERR_PTR(-ENOENT);
5128 static int alloc_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node)
5130 struct mem_cgroup_per_node *pn;
5132 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, node);
5136 pn->lruvec_stats_percpu = alloc_percpu_gfp(struct lruvec_stats_percpu,
5137 GFP_KERNEL_ACCOUNT);
5138 if (!pn->lruvec_stats_percpu) {
5143 lruvec_init(&pn->lruvec);
5146 memcg->nodeinfo[node] = pn;
5150 static void free_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node)
5152 struct mem_cgroup_per_node *pn = memcg->nodeinfo[node];
5157 free_percpu(pn->lruvec_stats_percpu);
5161 static void __mem_cgroup_free(struct mem_cgroup *memcg)
5166 free_mem_cgroup_per_node_info(memcg, node);
5167 free_percpu(memcg->vmstats_percpu);
5171 static void mem_cgroup_free(struct mem_cgroup *memcg)
5173 memcg_wb_domain_exit(memcg);
5174 __mem_cgroup_free(memcg);
5177 static struct mem_cgroup *mem_cgroup_alloc(void)
5179 struct mem_cgroup *memcg;
5181 int __maybe_unused i;
5182 long error = -ENOMEM;
5184 memcg = kzalloc(struct_size(memcg, nodeinfo, nr_node_ids), GFP_KERNEL);
5186 return ERR_PTR(error);
5188 memcg->id.id = idr_alloc(&mem_cgroup_idr, NULL,
5189 1, MEM_CGROUP_ID_MAX + 1, GFP_KERNEL);
5190 if (memcg->id.id < 0) {
5191 error = memcg->id.id;
5195 memcg->vmstats_percpu = alloc_percpu_gfp(struct memcg_vmstats_percpu,
5196 GFP_KERNEL_ACCOUNT);
5197 if (!memcg->vmstats_percpu)
5201 if (alloc_mem_cgroup_per_node_info(memcg, node))
5204 if (memcg_wb_domain_init(memcg, GFP_KERNEL))
5207 INIT_WORK(&memcg->high_work, high_work_func);
5208 INIT_LIST_HEAD(&memcg->oom_notify);
5209 mutex_init(&memcg->thresholds_lock);
5210 spin_lock_init(&memcg->move_lock);
5211 vmpressure_init(&memcg->vmpressure);
5212 INIT_LIST_HEAD(&memcg->event_list);
5213 spin_lock_init(&memcg->event_list_lock);
5214 memcg->socket_pressure = jiffies;
5215 #ifdef CONFIG_MEMCG_KMEM
5216 memcg->kmemcg_id = -1;
5217 INIT_LIST_HEAD(&memcg->objcg_list);
5219 #ifdef CONFIG_CGROUP_WRITEBACK
5220 INIT_LIST_HEAD(&memcg->cgwb_list);
5221 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++)
5222 memcg->cgwb_frn[i].done =
5223 __WB_COMPLETION_INIT(&memcg_cgwb_frn_waitq);
5225 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
5226 spin_lock_init(&memcg->deferred_split_queue.split_queue_lock);
5227 INIT_LIST_HEAD(&memcg->deferred_split_queue.split_queue);
5228 memcg->deferred_split_queue.split_queue_len = 0;
5230 idr_replace(&mem_cgroup_idr, memcg, memcg->id.id);
5233 mem_cgroup_id_remove(memcg);
5234 __mem_cgroup_free(memcg);
5235 return ERR_PTR(error);
5238 static struct cgroup_subsys_state * __ref
5239 mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
5241 struct mem_cgroup *parent = mem_cgroup_from_css(parent_css);
5242 struct mem_cgroup *memcg, *old_memcg;
5244 old_memcg = set_active_memcg(parent);
5245 memcg = mem_cgroup_alloc();
5246 set_active_memcg(old_memcg);
5248 return ERR_CAST(memcg);
5250 page_counter_set_high(&memcg->memory, PAGE_COUNTER_MAX);
5251 memcg->soft_limit = PAGE_COUNTER_MAX;
5252 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
5253 memcg->zswap_max = PAGE_COUNTER_MAX;
5255 page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX);
5257 memcg->swappiness = mem_cgroup_swappiness(parent);
5258 memcg->oom_kill_disable = parent->oom_kill_disable;
5260 page_counter_init(&memcg->memory, &parent->memory);
5261 page_counter_init(&memcg->swap, &parent->swap);
5262 page_counter_init(&memcg->kmem, &parent->kmem);
5263 page_counter_init(&memcg->tcpmem, &parent->tcpmem);
5265 page_counter_init(&memcg->memory, NULL);
5266 page_counter_init(&memcg->swap, NULL);
5267 page_counter_init(&memcg->kmem, NULL);
5268 page_counter_init(&memcg->tcpmem, NULL);
5270 root_mem_cgroup = memcg;
5274 if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket)
5275 static_branch_inc(&memcg_sockets_enabled_key);
5280 static int mem_cgroup_css_online(struct cgroup_subsys_state *css)
5282 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5284 if (memcg_online_kmem(memcg))
5288 * A memcg must be visible for expand_shrinker_info()
5289 * by the time the maps are allocated. So, we allocate maps
5290 * here, when for_each_mem_cgroup() can't skip it.
5292 if (alloc_shrinker_info(memcg))
5295 /* Online state pins memcg ID, memcg ID pins CSS */
5296 refcount_set(&memcg->id.ref, 1);
5299 if (unlikely(mem_cgroup_is_root(memcg)))
5300 queue_delayed_work(system_unbound_wq, &stats_flush_dwork,
5304 memcg_offline_kmem(memcg);
5306 mem_cgroup_id_remove(memcg);
5310 static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
5312 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5313 struct mem_cgroup_event *event, *tmp;
5316 * Unregister events and notify userspace.
5317 * Notify userspace about cgroup removing only after rmdir of cgroup
5318 * directory to avoid race between userspace and kernelspace.
5320 spin_lock_irq(&memcg->event_list_lock);
5321 list_for_each_entry_safe(event, tmp, &memcg->event_list, list) {
5322 list_del_init(&event->list);
5323 schedule_work(&event->remove);
5325 spin_unlock_irq(&memcg->event_list_lock);
5327 page_counter_set_min(&memcg->memory, 0);
5328 page_counter_set_low(&memcg->memory, 0);
5330 memcg_offline_kmem(memcg);
5331 reparent_shrinker_deferred(memcg);
5332 wb_memcg_offline(memcg);
5334 drain_all_stock(memcg);
5336 mem_cgroup_id_put(memcg);
5339 static void mem_cgroup_css_released(struct cgroup_subsys_state *css)
5341 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5343 invalidate_reclaim_iterators(memcg);
5346 static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
5348 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5349 int __maybe_unused i;
5351 #ifdef CONFIG_CGROUP_WRITEBACK
5352 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++)
5353 wb_wait_for_completion(&memcg->cgwb_frn[i].done);
5355 if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket)
5356 static_branch_dec(&memcg_sockets_enabled_key);
5358 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_active)
5359 static_branch_dec(&memcg_sockets_enabled_key);
5361 vmpressure_cleanup(&memcg->vmpressure);
5362 cancel_work_sync(&memcg->high_work);
5363 mem_cgroup_remove_from_trees(memcg);
5364 free_shrinker_info(memcg);
5365 mem_cgroup_free(memcg);
5369 * mem_cgroup_css_reset - reset the states of a mem_cgroup
5370 * @css: the target css
5372 * Reset the states of the mem_cgroup associated with @css. This is
5373 * invoked when the userland requests disabling on the default hierarchy
5374 * but the memcg is pinned through dependency. The memcg should stop
5375 * applying policies and should revert to the vanilla state as it may be
5376 * made visible again.
5378 * The current implementation only resets the essential configurations.
5379 * This needs to be expanded to cover all the visible parts.
5381 static void mem_cgroup_css_reset(struct cgroup_subsys_state *css)
5383 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5385 page_counter_set_max(&memcg->memory, PAGE_COUNTER_MAX);
5386 page_counter_set_max(&memcg->swap, PAGE_COUNTER_MAX);
5387 page_counter_set_max(&memcg->kmem, PAGE_COUNTER_MAX);
5388 page_counter_set_max(&memcg->tcpmem, PAGE_COUNTER_MAX);
5389 page_counter_set_min(&memcg->memory, 0);
5390 page_counter_set_low(&memcg->memory, 0);
5391 page_counter_set_high(&memcg->memory, PAGE_COUNTER_MAX);
5392 memcg->soft_limit = PAGE_COUNTER_MAX;
5393 page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX);
5394 memcg_wb_domain_size_changed(memcg);
5397 static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu)
5399 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5400 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5401 struct memcg_vmstats_percpu *statc;
5405 statc = per_cpu_ptr(memcg->vmstats_percpu, cpu);
5407 for (i = 0; i < MEMCG_NR_STAT; i++) {
5409 * Collect the aggregated propagation counts of groups
5410 * below us. We're in a per-cpu loop here and this is
5411 * a global counter, so the first cycle will get them.
5413 delta = memcg->vmstats.state_pending[i];
5415 memcg->vmstats.state_pending[i] = 0;
5417 /* Add CPU changes on this level since the last flush */
5418 v = READ_ONCE(statc->state[i]);
5419 if (v != statc->state_prev[i]) {
5420 delta += v - statc->state_prev[i];
5421 statc->state_prev[i] = v;
5427 /* Aggregate counts on this level and propagate upwards */
5428 memcg->vmstats.state[i] += delta;
5430 parent->vmstats.state_pending[i] += delta;
5433 for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
5434 delta = memcg->vmstats.events_pending[i];
5436 memcg->vmstats.events_pending[i] = 0;
5438 v = READ_ONCE(statc->events[i]);
5439 if (v != statc->events_prev[i]) {
5440 delta += v - statc->events_prev[i];
5441 statc->events_prev[i] = v;
5447 memcg->vmstats.events[i] += delta;
5449 parent->vmstats.events_pending[i] += delta;
5452 for_each_node_state(nid, N_MEMORY) {
5453 struct mem_cgroup_per_node *pn = memcg->nodeinfo[nid];
5454 struct mem_cgroup_per_node *ppn = NULL;
5455 struct lruvec_stats_percpu *lstatc;
5458 ppn = parent->nodeinfo[nid];
5460 lstatc = per_cpu_ptr(pn->lruvec_stats_percpu, cpu);
5462 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
5463 delta = pn->lruvec_stats.state_pending[i];
5465 pn->lruvec_stats.state_pending[i] = 0;
5467 v = READ_ONCE(lstatc->state[i]);
5468 if (v != lstatc->state_prev[i]) {
5469 delta += v - lstatc->state_prev[i];
5470 lstatc->state_prev[i] = v;
5476 pn->lruvec_stats.state[i] += delta;
5478 ppn->lruvec_stats.state_pending[i] += delta;
5484 /* Handlers for move charge at task migration. */
5485 static int mem_cgroup_do_precharge(unsigned long count)
5489 /* Try a single bulk charge without reclaim first, kswapd may wake */
5490 ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_DIRECT_RECLAIM, count);
5492 mc.precharge += count;
5496 /* Try charges one by one with reclaim, but do not retry */
5498 ret = try_charge(mc.to, GFP_KERNEL | __GFP_NORETRY, 1);
5512 enum mc_target_type {
5519 static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5520 unsigned long addr, pte_t ptent)
5522 struct page *page = vm_normal_page(vma, addr, ptent);
5524 if (!page || !page_mapped(page))
5526 if (PageAnon(page)) {
5527 if (!(mc.flags & MOVE_ANON))
5530 if (!(mc.flags & MOVE_FILE))
5533 if (!get_page_unless_zero(page))
5539 #if defined(CONFIG_SWAP) || defined(CONFIG_DEVICE_PRIVATE)
5540 static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5541 pte_t ptent, swp_entry_t *entry)
5543 struct page *page = NULL;
5544 swp_entry_t ent = pte_to_swp_entry(ptent);
5546 if (!(mc.flags & MOVE_ANON))
5550 * Handle device private pages that are not accessible by the CPU, but
5551 * stored as special swap entries in the page table.
5553 if (is_device_private_entry(ent)) {
5554 page = pfn_swap_entry_to_page(ent);
5555 if (!get_page_unless_zero(page))
5560 if (non_swap_entry(ent))
5564 * Because lookup_swap_cache() updates some statistics counter,
5565 * we call find_get_page() with swapper_space directly.
5567 page = find_get_page(swap_address_space(ent), swp_offset(ent));
5568 entry->val = ent.val;
5573 static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5574 pte_t ptent, swp_entry_t *entry)
5580 static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5581 unsigned long addr, pte_t ptent)
5583 if (!vma->vm_file) /* anonymous vma */
5585 if (!(mc.flags & MOVE_FILE))
5588 /* page is moved even if it's not RSS of this task(page-faulted). */
5589 /* shmem/tmpfs may report page out on swap: account for that too. */
5590 return find_get_incore_page(vma->vm_file->f_mapping,
5591 linear_page_index(vma, addr));
5595 * mem_cgroup_move_account - move account of the page
5597 * @compound: charge the page as compound or small page
5598 * @from: mem_cgroup which the page is moved from.
5599 * @to: mem_cgroup which the page is moved to. @from != @to.
5601 * The caller must make sure the page is not on LRU (isolate_page() is useful.)
5603 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
5606 static int mem_cgroup_move_account(struct page *page,
5608 struct mem_cgroup *from,
5609 struct mem_cgroup *to)
5611 struct folio *folio = page_folio(page);
5612 struct lruvec *from_vec, *to_vec;
5613 struct pglist_data *pgdat;
5614 unsigned int nr_pages = compound ? folio_nr_pages(folio) : 1;
5617 VM_BUG_ON(from == to);
5618 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
5619 VM_BUG_ON(compound && !folio_test_large(folio));
5622 * Prevent mem_cgroup_migrate() from looking at
5623 * page's memory cgroup of its source page while we change it.
5626 if (!folio_trylock(folio))
5630 if (folio_memcg(folio) != from)
5633 pgdat = folio_pgdat(folio);
5634 from_vec = mem_cgroup_lruvec(from, pgdat);
5635 to_vec = mem_cgroup_lruvec(to, pgdat);
5637 folio_memcg_lock(folio);
5639 if (folio_test_anon(folio)) {
5640 if (folio_mapped(folio)) {
5641 __mod_lruvec_state(from_vec, NR_ANON_MAPPED, -nr_pages);
5642 __mod_lruvec_state(to_vec, NR_ANON_MAPPED, nr_pages);
5643 if (folio_test_transhuge(folio)) {
5644 __mod_lruvec_state(from_vec, NR_ANON_THPS,
5646 __mod_lruvec_state(to_vec, NR_ANON_THPS,
5651 __mod_lruvec_state(from_vec, NR_FILE_PAGES, -nr_pages);
5652 __mod_lruvec_state(to_vec, NR_FILE_PAGES, nr_pages);
5654 if (folio_test_swapbacked(folio)) {
5655 __mod_lruvec_state(from_vec, NR_SHMEM, -nr_pages);
5656 __mod_lruvec_state(to_vec, NR_SHMEM, nr_pages);
5659 if (folio_mapped(folio)) {
5660 __mod_lruvec_state(from_vec, NR_FILE_MAPPED, -nr_pages);
5661 __mod_lruvec_state(to_vec, NR_FILE_MAPPED, nr_pages);
5664 if (folio_test_dirty(folio)) {
5665 struct address_space *mapping = folio_mapping(folio);
5667 if (mapping_can_writeback(mapping)) {
5668 __mod_lruvec_state(from_vec, NR_FILE_DIRTY,
5670 __mod_lruvec_state(to_vec, NR_FILE_DIRTY,
5676 if (folio_test_writeback(folio)) {
5677 __mod_lruvec_state(from_vec, NR_WRITEBACK, -nr_pages);
5678 __mod_lruvec_state(to_vec, NR_WRITEBACK, nr_pages);
5682 * All state has been migrated, let's switch to the new memcg.
5684 * It is safe to change page's memcg here because the page
5685 * is referenced, charged, isolated, and locked: we can't race
5686 * with (un)charging, migration, LRU putback, or anything else
5687 * that would rely on a stable page's memory cgroup.
5689 * Note that lock_page_memcg is a memcg lock, not a page lock,
5690 * to save space. As soon as we switch page's memory cgroup to a
5691 * new memcg that isn't locked, the above state can change
5692 * concurrently again. Make sure we're truly done with it.
5697 css_put(&from->css);
5699 folio->memcg_data = (unsigned long)to;
5701 __folio_memcg_unlock(from);
5704 nid = folio_nid(folio);
5706 local_irq_disable();
5707 mem_cgroup_charge_statistics(to, nr_pages);
5708 memcg_check_events(to, nid);
5709 mem_cgroup_charge_statistics(from, -nr_pages);
5710 memcg_check_events(from, nid);
5713 folio_unlock(folio);
5719 * get_mctgt_type - get target type of moving charge
5720 * @vma: the vma the pte to be checked belongs
5721 * @addr: the address corresponding to the pte to be checked
5722 * @ptent: the pte to be checked
5723 * @target: the pointer the target page or swap ent will be stored(can be NULL)
5726 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5727 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5728 * move charge. if @target is not NULL, the page is stored in target->page
5729 * with extra refcnt got(Callers should handle it).
5730 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5731 * target for charge migration. if @target is not NULL, the entry is stored
5733 * 3(MC_TARGET_DEVICE): like MC_TARGET_PAGE but page is device memory and
5734 * thus not on the lru.
5735 * For now we such page is charge like a regular page would be as for all
5736 * intent and purposes it is just special memory taking the place of a
5739 * See Documentations/vm/hmm.txt and include/linux/hmm.h
5741 * Called with pte lock held.
5744 static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
5745 unsigned long addr, pte_t ptent, union mc_target *target)
5747 struct page *page = NULL;
5748 enum mc_target_type ret = MC_TARGET_NONE;
5749 swp_entry_t ent = { .val = 0 };
5751 if (pte_present(ptent))
5752 page = mc_handle_present_pte(vma, addr, ptent);
5753 else if (pte_none_mostly(ptent))
5755 * PTE markers should be treated as a none pte here, separated
5756 * from other swap handling below.
5758 page = mc_handle_file_pte(vma, addr, ptent);
5759 else if (is_swap_pte(ptent))
5760 page = mc_handle_swap_pte(vma, ptent, &ent);
5762 if (!page && !ent.val)
5766 * Do only loose check w/o serialization.
5767 * mem_cgroup_move_account() checks the page is valid or
5768 * not under LRU exclusion.
5770 if (page_memcg(page) == mc.from) {
5771 ret = MC_TARGET_PAGE;
5772 if (is_device_private_page(page) ||
5773 is_device_coherent_page(page))
5774 ret = MC_TARGET_DEVICE;
5776 target->page = page;
5778 if (!ret || !target)
5782 * There is a swap entry and a page doesn't exist or isn't charged.
5783 * But we cannot move a tail-page in a THP.
5785 if (ent.val && !ret && (!page || !PageTransCompound(page)) &&
5786 mem_cgroup_id(mc.from) == lookup_swap_cgroup_id(ent)) {
5787 ret = MC_TARGET_SWAP;
5794 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
5796 * We don't consider PMD mapped swapping or file mapped pages because THP does
5797 * not support them for now.
5798 * Caller should make sure that pmd_trans_huge(pmd) is true.
5800 static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5801 unsigned long addr, pmd_t pmd, union mc_target *target)
5803 struct page *page = NULL;
5804 enum mc_target_type ret = MC_TARGET_NONE;
5806 if (unlikely(is_swap_pmd(pmd))) {
5807 VM_BUG_ON(thp_migration_supported() &&
5808 !is_pmd_migration_entry(pmd));
5811 page = pmd_page(pmd);
5812 VM_BUG_ON_PAGE(!page || !PageHead(page), page);
5813 if (!(mc.flags & MOVE_ANON))
5815 if (page_memcg(page) == mc.from) {
5816 ret = MC_TARGET_PAGE;
5819 target->page = page;
5825 static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5826 unsigned long addr, pmd_t pmd, union mc_target *target)
5828 return MC_TARGET_NONE;
5832 static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5833 unsigned long addr, unsigned long end,
5834 struct mm_walk *walk)
5836 struct vm_area_struct *vma = walk->vma;
5840 ptl = pmd_trans_huge_lock(pmd, vma);
5843 * Note their can not be MC_TARGET_DEVICE for now as we do not
5844 * support transparent huge page with MEMORY_DEVICE_PRIVATE but
5845 * this might change.
5847 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
5848 mc.precharge += HPAGE_PMD_NR;
5853 if (pmd_trans_unstable(pmd))
5855 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5856 for (; addr != end; pte++, addr += PAGE_SIZE)
5857 if (get_mctgt_type(vma, addr, *pte, NULL))
5858 mc.precharge++; /* increment precharge temporarily */
5859 pte_unmap_unlock(pte - 1, ptl);
5865 static const struct mm_walk_ops precharge_walk_ops = {
5866 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5869 static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5871 unsigned long precharge;
5874 walk_page_range(mm, 0, mm->highest_vm_end, &precharge_walk_ops, NULL);
5875 mmap_read_unlock(mm);
5877 precharge = mc.precharge;
5883 static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5885 unsigned long precharge = mem_cgroup_count_precharge(mm);
5887 VM_BUG_ON(mc.moving_task);
5888 mc.moving_task = current;
5889 return mem_cgroup_do_precharge(precharge);
5892 /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5893 static void __mem_cgroup_clear_mc(void)
5895 struct mem_cgroup *from = mc.from;
5896 struct mem_cgroup *to = mc.to;
5898 /* we must uncharge all the leftover precharges from mc.to */
5900 cancel_charge(mc.to, mc.precharge);
5904 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5905 * we must uncharge here.
5907 if (mc.moved_charge) {
5908 cancel_charge(mc.from, mc.moved_charge);
5909 mc.moved_charge = 0;
5911 /* we must fixup refcnts and charges */
5912 if (mc.moved_swap) {
5913 /* uncharge swap account from the old cgroup */
5914 if (!mem_cgroup_is_root(mc.from))
5915 page_counter_uncharge(&mc.from->memsw, mc.moved_swap);
5917 mem_cgroup_id_put_many(mc.from, mc.moved_swap);
5920 * we charged both to->memory and to->memsw, so we
5921 * should uncharge to->memory.
5923 if (!mem_cgroup_is_root(mc.to))
5924 page_counter_uncharge(&mc.to->memory, mc.moved_swap);
5928 memcg_oom_recover(from);
5929 memcg_oom_recover(to);
5930 wake_up_all(&mc.waitq);
5933 static void mem_cgroup_clear_mc(void)
5935 struct mm_struct *mm = mc.mm;
5938 * we must clear moving_task before waking up waiters at the end of
5941 mc.moving_task = NULL;
5942 __mem_cgroup_clear_mc();
5943 spin_lock(&mc.lock);
5947 spin_unlock(&mc.lock);
5952 static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
5954 struct cgroup_subsys_state *css;
5955 struct mem_cgroup *memcg = NULL; /* unneeded init to make gcc happy */
5956 struct mem_cgroup *from;
5957 struct task_struct *leader, *p;
5958 struct mm_struct *mm;
5959 unsigned long move_flags;
5962 /* charge immigration isn't supported on the default hierarchy */
5963 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
5967 * Multi-process migrations only happen on the default hierarchy
5968 * where charge immigration is not used. Perform charge
5969 * immigration if @tset contains a leader and whine if there are
5973 cgroup_taskset_for_each_leader(leader, css, tset) {
5976 memcg = mem_cgroup_from_css(css);
5982 * We are now committed to this value whatever it is. Changes in this
5983 * tunable will only affect upcoming migrations, not the current one.
5984 * So we need to save it, and keep it going.
5986 move_flags = READ_ONCE(memcg->move_charge_at_immigrate);
5990 from = mem_cgroup_from_task(p);
5992 VM_BUG_ON(from == memcg);
5994 mm = get_task_mm(p);
5997 /* We move charges only when we move a owner of the mm */
5998 if (mm->owner == p) {
6001 VM_BUG_ON(mc.precharge);
6002 VM_BUG_ON(mc.moved_charge);
6003 VM_BUG_ON(mc.moved_swap);
6005 spin_lock(&mc.lock);
6009 mc.flags = move_flags;
6010 spin_unlock(&mc.lock);
6011 /* We set mc.moving_task later */
6013 ret = mem_cgroup_precharge_mc(mm);
6015 mem_cgroup_clear_mc();
6022 static void mem_cgroup_cancel_attach(struct cgroup_taskset *tset)
6025 mem_cgroup_clear_mc();
6028 static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
6029 unsigned long addr, unsigned long end,
6030 struct mm_walk *walk)
6033 struct vm_area_struct *vma = walk->vma;
6036 enum mc_target_type target_type;
6037 union mc_target target;
6040 ptl = pmd_trans_huge_lock(pmd, vma);
6042 if (mc.precharge < HPAGE_PMD_NR) {
6046 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
6047 if (target_type == MC_TARGET_PAGE) {
6049 if (!isolate_lru_page(page)) {
6050 if (!mem_cgroup_move_account(page, true,
6052 mc.precharge -= HPAGE_PMD_NR;
6053 mc.moved_charge += HPAGE_PMD_NR;
6055 putback_lru_page(page);
6058 } else if (target_type == MC_TARGET_DEVICE) {
6060 if (!mem_cgroup_move_account(page, true,
6062 mc.precharge -= HPAGE_PMD_NR;
6063 mc.moved_charge += HPAGE_PMD_NR;
6071 if (pmd_trans_unstable(pmd))
6074 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6075 for (; addr != end; addr += PAGE_SIZE) {
6076 pte_t ptent = *(pte++);
6077 bool device = false;
6083 switch (get_mctgt_type(vma, addr, ptent, &target)) {
6084 case MC_TARGET_DEVICE:
6087 case MC_TARGET_PAGE:
6090 * We can have a part of the split pmd here. Moving it
6091 * can be done but it would be too convoluted so simply
6092 * ignore such a partial THP and keep it in original
6093 * memcg. There should be somebody mapping the head.
6095 if (PageTransCompound(page))
6097 if (!device && isolate_lru_page(page))
6099 if (!mem_cgroup_move_account(page, false,
6102 /* we uncharge from mc.from later. */
6106 putback_lru_page(page);
6107 put: /* get_mctgt_type() gets the page */
6110 case MC_TARGET_SWAP:
6112 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
6114 mem_cgroup_id_get_many(mc.to, 1);
6115 /* we fixup other refcnts and charges later. */
6123 pte_unmap_unlock(pte - 1, ptl);
6128 * We have consumed all precharges we got in can_attach().
6129 * We try charge one by one, but don't do any additional
6130 * charges to mc.to if we have failed in charge once in attach()
6133 ret = mem_cgroup_do_precharge(1);
6141 static const struct mm_walk_ops charge_walk_ops = {
6142 .pmd_entry = mem_cgroup_move_charge_pte_range,
6145 static void mem_cgroup_move_charge(void)
6147 lru_add_drain_all();
6149 * Signal lock_page_memcg() to take the memcg's move_lock
6150 * while we're moving its pages to another memcg. Then wait
6151 * for already started RCU-only updates to finish.
6153 atomic_inc(&mc.from->moving_account);
6156 if (unlikely(!mmap_read_trylock(mc.mm))) {
6158 * Someone who are holding the mmap_lock might be waiting in
6159 * waitq. So we cancel all extra charges, wake up all waiters,
6160 * and retry. Because we cancel precharges, we might not be able
6161 * to move enough charges, but moving charge is a best-effort
6162 * feature anyway, so it wouldn't be a big problem.
6164 __mem_cgroup_clear_mc();
6169 * When we have consumed all precharges and failed in doing
6170 * additional charge, the page walk just aborts.
6172 walk_page_range(mc.mm, 0, mc.mm->highest_vm_end, &charge_walk_ops,
6175 mmap_read_unlock(mc.mm);
6176 atomic_dec(&mc.from->moving_account);
6179 static void mem_cgroup_move_task(void)
6182 mem_cgroup_move_charge();
6183 mem_cgroup_clear_mc();
6186 #else /* !CONFIG_MMU */
6187 static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
6191 static void mem_cgroup_cancel_attach(struct cgroup_taskset *tset)
6194 static void mem_cgroup_move_task(void)
6199 static int seq_puts_memcg_tunable(struct seq_file *m, unsigned long value)
6201 if (value == PAGE_COUNTER_MAX)
6202 seq_puts(m, "max\n");
6204 seq_printf(m, "%llu\n", (u64)value * PAGE_SIZE);
6209 static u64 memory_current_read(struct cgroup_subsys_state *css,
6212 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
6214 return (u64)page_counter_read(&memcg->memory) * PAGE_SIZE;
6217 static u64 memory_peak_read(struct cgroup_subsys_state *css,
6220 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
6222 return (u64)memcg->memory.watermark * PAGE_SIZE;
6225 static int memory_min_show(struct seq_file *m, void *v)
6227 return seq_puts_memcg_tunable(m,
6228 READ_ONCE(mem_cgroup_from_seq(m)->memory.min));
6231 static ssize_t memory_min_write(struct kernfs_open_file *of,
6232 char *buf, size_t nbytes, loff_t off)
6234 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6238 buf = strstrip(buf);
6239 err = page_counter_memparse(buf, "max", &min);
6243 page_counter_set_min(&memcg->memory, min);
6248 static int memory_low_show(struct seq_file *m, void *v)
6250 return seq_puts_memcg_tunable(m,
6251 READ_ONCE(mem_cgroup_from_seq(m)->memory.low));
6254 static ssize_t memory_low_write(struct kernfs_open_file *of,
6255 char *buf, size_t nbytes, loff_t off)
6257 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6261 buf = strstrip(buf);
6262 err = page_counter_memparse(buf, "max", &low);
6266 page_counter_set_low(&memcg->memory, low);
6271 static int memory_high_show(struct seq_file *m, void *v)
6273 return seq_puts_memcg_tunable(m,
6274 READ_ONCE(mem_cgroup_from_seq(m)->memory.high));
6277 static ssize_t memory_high_write(struct kernfs_open_file *of,
6278 char *buf, size_t nbytes, loff_t off)
6280 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6281 unsigned int nr_retries = MAX_RECLAIM_RETRIES;
6282 bool drained = false;
6286 buf = strstrip(buf);
6287 err = page_counter_memparse(buf, "max", &high);
6291 page_counter_set_high(&memcg->memory, high);
6294 unsigned long nr_pages = page_counter_read(&memcg->memory);
6295 unsigned long reclaimed;
6297 if (nr_pages <= high)
6300 if (signal_pending(current))
6304 drain_all_stock(memcg);
6309 reclaimed = try_to_free_mem_cgroup_pages(memcg, nr_pages - high,
6310 GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP);
6312 if (!reclaimed && !nr_retries--)
6316 memcg_wb_domain_size_changed(memcg);
6320 static int memory_max_show(struct seq_file *m, void *v)
6322 return seq_puts_memcg_tunable(m,
6323 READ_ONCE(mem_cgroup_from_seq(m)->memory.max));
6326 static ssize_t memory_max_write(struct kernfs_open_file *of,
6327 char *buf, size_t nbytes, loff_t off)
6329 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6330 unsigned int nr_reclaims = MAX_RECLAIM_RETRIES;
6331 bool drained = false;
6335 buf = strstrip(buf);
6336 err = page_counter_memparse(buf, "max", &max);
6340 xchg(&memcg->memory.max, max);
6343 unsigned long nr_pages = page_counter_read(&memcg->memory);
6345 if (nr_pages <= max)
6348 if (signal_pending(current))
6352 drain_all_stock(memcg);
6358 if (!try_to_free_mem_cgroup_pages(memcg, nr_pages - max,
6359 GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP))
6364 memcg_memory_event(memcg, MEMCG_OOM);
6365 if (!mem_cgroup_out_of_memory(memcg, GFP_KERNEL, 0))
6369 memcg_wb_domain_size_changed(memcg);
6373 static void __memory_events_show(struct seq_file *m, atomic_long_t *events)
6375 seq_printf(m, "low %lu\n", atomic_long_read(&events[MEMCG_LOW]));
6376 seq_printf(m, "high %lu\n", atomic_long_read(&events[MEMCG_HIGH]));
6377 seq_printf(m, "max %lu\n", atomic_long_read(&events[MEMCG_MAX]));
6378 seq_printf(m, "oom %lu\n", atomic_long_read(&events[MEMCG_OOM]));
6379 seq_printf(m, "oom_kill %lu\n",
6380 atomic_long_read(&events[MEMCG_OOM_KILL]));
6381 seq_printf(m, "oom_group_kill %lu\n",
6382 atomic_long_read(&events[MEMCG_OOM_GROUP_KILL]));
6385 static int memory_events_show(struct seq_file *m, void *v)
6387 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
6389 __memory_events_show(m, memcg->memory_events);
6393 static int memory_events_local_show(struct seq_file *m, void *v)
6395 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
6397 __memory_events_show(m, memcg->memory_events_local);
6401 static int memory_stat_show(struct seq_file *m, void *v)
6403 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
6404 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
6408 memory_stat_format(memcg, buf, PAGE_SIZE);
6415 static inline unsigned long lruvec_page_state_output(struct lruvec *lruvec,
6418 return lruvec_page_state(lruvec, item) * memcg_page_state_unit(item);
6421 static int memory_numa_stat_show(struct seq_file *m, void *v)
6424 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
6426 mem_cgroup_flush_stats();
6428 for (i = 0; i < ARRAY_SIZE(memory_stats); i++) {
6431 if (memory_stats[i].idx >= NR_VM_NODE_STAT_ITEMS)
6434 seq_printf(m, "%s", memory_stats[i].name);
6435 for_each_node_state(nid, N_MEMORY) {
6437 struct lruvec *lruvec;
6439 lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid));
6440 size = lruvec_page_state_output(lruvec,
6441 memory_stats[i].idx);
6442 seq_printf(m, " N%d=%llu", nid, size);
6451 static int memory_oom_group_show(struct seq_file *m, void *v)
6453 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
6455 seq_printf(m, "%d\n", memcg->oom_group);
6460 static ssize_t memory_oom_group_write(struct kernfs_open_file *of,
6461 char *buf, size_t nbytes, loff_t off)
6463 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6466 buf = strstrip(buf);
6470 ret = kstrtoint(buf, 0, &oom_group);
6474 if (oom_group != 0 && oom_group != 1)
6477 memcg->oom_group = oom_group;
6482 static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
6483 size_t nbytes, loff_t off)
6485 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6486 unsigned int nr_retries = MAX_RECLAIM_RETRIES;
6487 unsigned long nr_to_reclaim, nr_reclaimed = 0;
6488 unsigned int reclaim_options;
6491 buf = strstrip(buf);
6492 err = page_counter_memparse(buf, "", &nr_to_reclaim);
6496 reclaim_options = MEMCG_RECLAIM_MAY_SWAP | MEMCG_RECLAIM_PROACTIVE;
6497 while (nr_reclaimed < nr_to_reclaim) {
6498 unsigned long reclaimed;
6500 if (signal_pending(current))
6504 * This is the final attempt, drain percpu lru caches in the
6505 * hope of introducing more evictable pages for
6506 * try_to_free_mem_cgroup_pages().
6509 lru_add_drain_all();
6511 reclaimed = try_to_free_mem_cgroup_pages(memcg,
6512 nr_to_reclaim - nr_reclaimed,
6513 GFP_KERNEL, reclaim_options);
6515 if (!reclaimed && !nr_retries--)
6518 nr_reclaimed += reclaimed;
6524 static struct cftype memory_files[] = {
6527 .flags = CFTYPE_NOT_ON_ROOT,
6528 .read_u64 = memory_current_read,
6532 .flags = CFTYPE_NOT_ON_ROOT,
6533 .read_u64 = memory_peak_read,
6537 .flags = CFTYPE_NOT_ON_ROOT,
6538 .seq_show = memory_min_show,
6539 .write = memory_min_write,
6543 .flags = CFTYPE_NOT_ON_ROOT,
6544 .seq_show = memory_low_show,
6545 .write = memory_low_write,
6549 .flags = CFTYPE_NOT_ON_ROOT,
6550 .seq_show = memory_high_show,
6551 .write = memory_high_write,
6555 .flags = CFTYPE_NOT_ON_ROOT,
6556 .seq_show = memory_max_show,
6557 .write = memory_max_write,
6561 .flags = CFTYPE_NOT_ON_ROOT,
6562 .file_offset = offsetof(struct mem_cgroup, events_file),
6563 .seq_show = memory_events_show,
6566 .name = "events.local",
6567 .flags = CFTYPE_NOT_ON_ROOT,
6568 .file_offset = offsetof(struct mem_cgroup, events_local_file),
6569 .seq_show = memory_events_local_show,
6573 .seq_show = memory_stat_show,
6577 .name = "numa_stat",
6578 .seq_show = memory_numa_stat_show,
6582 .name = "oom.group",
6583 .flags = CFTYPE_NOT_ON_ROOT | CFTYPE_NS_DELEGATABLE,
6584 .seq_show = memory_oom_group_show,
6585 .write = memory_oom_group_write,
6589 .flags = CFTYPE_NS_DELEGATABLE,
6590 .write = memory_reclaim,
6595 struct cgroup_subsys memory_cgrp_subsys = {
6596 .css_alloc = mem_cgroup_css_alloc,
6597 .css_online = mem_cgroup_css_online,
6598 .css_offline = mem_cgroup_css_offline,
6599 .css_released = mem_cgroup_css_released,
6600 .css_free = mem_cgroup_css_free,
6601 .css_reset = mem_cgroup_css_reset,
6602 .css_rstat_flush = mem_cgroup_css_rstat_flush,
6603 .can_attach = mem_cgroup_can_attach,
6604 .cancel_attach = mem_cgroup_cancel_attach,
6605 .post_attach = mem_cgroup_move_task,
6606 .dfl_cftypes = memory_files,
6607 .legacy_cftypes = mem_cgroup_legacy_files,
6612 * This function calculates an individual cgroup's effective
6613 * protection which is derived from its own memory.min/low, its
6614 * parent's and siblings' settings, as well as the actual memory
6615 * distribution in the tree.
6617 * The following rules apply to the effective protection values:
6619 * 1. At the first level of reclaim, effective protection is equal to
6620 * the declared protection in memory.min and memory.low.
6622 * 2. To enable safe delegation of the protection configuration, at
6623 * subsequent levels the effective protection is capped to the
6624 * parent's effective protection.
6626 * 3. To make complex and dynamic subtrees easier to configure, the
6627 * user is allowed to overcommit the declared protection at a given
6628 * level. If that is the case, the parent's effective protection is
6629 * distributed to the children in proportion to how much protection
6630 * they have declared and how much of it they are utilizing.
6632 * This makes distribution proportional, but also work-conserving:
6633 * if one cgroup claims much more protection than it uses memory,
6634 * the unused remainder is available to its siblings.
6636 * 4. Conversely, when the declared protection is undercommitted at a
6637 * given level, the distribution of the larger parental protection
6638 * budget is NOT proportional. A cgroup's protection from a sibling
6639 * is capped to its own memory.min/low setting.
6641 * 5. However, to allow protecting recursive subtrees from each other
6642 * without having to declare each individual cgroup's fixed share
6643 * of the ancestor's claim to protection, any unutilized -
6644 * "floating" - protection from up the tree is distributed in
6645 * proportion to each cgroup's *usage*. This makes the protection
6646 * neutral wrt sibling cgroups and lets them compete freely over
6647 * the shared parental protection budget, but it protects the
6648 * subtree as a whole from neighboring subtrees.
6650 * Note that 4. and 5. are not in conflict: 4. is about protecting
6651 * against immediate siblings whereas 5. is about protecting against
6652 * neighboring subtrees.
6654 static unsigned long effective_protection(unsigned long usage,
6655 unsigned long parent_usage,
6656 unsigned long setting,
6657 unsigned long parent_effective,
6658 unsigned long siblings_protected)
6660 unsigned long protected;
6663 protected = min(usage, setting);
6665 * If all cgroups at this level combined claim and use more
6666 * protection then what the parent affords them, distribute
6667 * shares in proportion to utilization.
6669 * We are using actual utilization rather than the statically
6670 * claimed protection in order to be work-conserving: claimed
6671 * but unused protection is available to siblings that would
6672 * otherwise get a smaller chunk than what they claimed.
6674 if (siblings_protected > parent_effective)
6675 return protected * parent_effective / siblings_protected;
6678 * Ok, utilized protection of all children is within what the
6679 * parent affords them, so we know whatever this child claims
6680 * and utilizes is effectively protected.
6682 * If there is unprotected usage beyond this value, reclaim
6683 * will apply pressure in proportion to that amount.
6685 * If there is unutilized protection, the cgroup will be fully
6686 * shielded from reclaim, but we do return a smaller value for
6687 * protection than what the group could enjoy in theory. This
6688 * is okay. With the overcommit distribution above, effective
6689 * protection is always dependent on how memory is actually
6690 * consumed among the siblings anyway.
6695 * If the children aren't claiming (all of) the protection
6696 * afforded to them by the parent, distribute the remainder in
6697 * proportion to the (unprotected) memory of each cgroup. That
6698 * way, cgroups that aren't explicitly prioritized wrt each
6699 * other compete freely over the allowance, but they are
6700 * collectively protected from neighboring trees.
6702 * We're using unprotected memory for the weight so that if
6703 * some cgroups DO claim explicit protection, we don't protect
6704 * the same bytes twice.
6706 * Check both usage and parent_usage against the respective
6707 * protected values. One should imply the other, but they
6708 * aren't read atomically - make sure the division is sane.
6710 if (!(cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT))
6712 if (parent_effective > siblings_protected &&
6713 parent_usage > siblings_protected &&
6714 usage > protected) {
6715 unsigned long unclaimed;
6717 unclaimed = parent_effective - siblings_protected;
6718 unclaimed *= usage - protected;
6719 unclaimed /= parent_usage - siblings_protected;
6728 * mem_cgroup_calculate_protection - check if memory consumption is in the normal range
6729 * @root: the top ancestor of the sub-tree being checked
6730 * @memcg: the memory cgroup to check
6732 * WARNING: This function is not stateless! It can only be used as part
6733 * of a top-down tree iteration, not for isolated queries.
6735 void mem_cgroup_calculate_protection(struct mem_cgroup *root,
6736 struct mem_cgroup *memcg)
6738 unsigned long usage, parent_usage;
6739 struct mem_cgroup *parent;
6741 if (mem_cgroup_disabled())
6745 root = root_mem_cgroup;
6748 * Effective values of the reclaim targets are ignored so they
6749 * can be stale. Have a look at mem_cgroup_protection for more
6751 * TODO: calculation should be more robust so that we do not need
6752 * that special casing.
6757 usage = page_counter_read(&memcg->memory);
6761 parent = parent_mem_cgroup(memcg);
6763 if (parent == root) {
6764 memcg->memory.emin = READ_ONCE(memcg->memory.min);
6765 memcg->memory.elow = READ_ONCE(memcg->memory.low);
6769 parent_usage = page_counter_read(&parent->memory);
6771 WRITE_ONCE(memcg->memory.emin, effective_protection(usage, parent_usage,
6772 READ_ONCE(memcg->memory.min),
6773 READ_ONCE(parent->memory.emin),
6774 atomic_long_read(&parent->memory.children_min_usage)));
6776 WRITE_ONCE(memcg->memory.elow, effective_protection(usage, parent_usage,
6777 READ_ONCE(memcg->memory.low),
6778 READ_ONCE(parent->memory.elow),
6779 atomic_long_read(&parent->memory.children_low_usage)));
6782 static int charge_memcg(struct folio *folio, struct mem_cgroup *memcg,
6785 long nr_pages = folio_nr_pages(folio);
6788 ret = try_charge(memcg, gfp, nr_pages);
6792 css_get(&memcg->css);
6793 commit_charge(folio, memcg);
6795 local_irq_disable();
6796 mem_cgroup_charge_statistics(memcg, nr_pages);
6797 memcg_check_events(memcg, folio_nid(folio));
6803 int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp)
6805 struct mem_cgroup *memcg;
6808 memcg = get_mem_cgroup_from_mm(mm);
6809 ret = charge_memcg(folio, memcg, gfp);
6810 css_put(&memcg->css);
6816 * mem_cgroup_swapin_charge_page - charge a newly allocated page for swapin
6817 * @page: page to charge
6818 * @mm: mm context of the victim
6819 * @gfp: reclaim mode
6820 * @entry: swap entry for which the page is allocated
6822 * This function charges a page allocated for swapin. Please call this before
6823 * adding the page to the swapcache.
6825 * Returns 0 on success. Otherwise, an error code is returned.
6827 int mem_cgroup_swapin_charge_page(struct page *page, struct mm_struct *mm,
6828 gfp_t gfp, swp_entry_t entry)
6830 struct folio *folio = page_folio(page);
6831 struct mem_cgroup *memcg;
6835 if (mem_cgroup_disabled())
6838 id = lookup_swap_cgroup_id(entry);
6840 memcg = mem_cgroup_from_id(id);
6841 if (!memcg || !css_tryget_online(&memcg->css))
6842 memcg = get_mem_cgroup_from_mm(mm);
6845 ret = charge_memcg(folio, memcg, gfp);
6847 css_put(&memcg->css);
6852 * mem_cgroup_swapin_uncharge_swap - uncharge swap slot
6853 * @entry: swap entry for which the page is charged
6855 * Call this function after successfully adding the charged page to swapcache.
6857 * Note: This function assumes the page for which swap slot is being uncharged
6860 void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry)
6863 * Cgroup1's unified memory+swap counter has been charged with the
6864 * new swapcache page, finish the transfer by uncharging the swap
6865 * slot. The swap slot would also get uncharged when it dies, but
6866 * it can stick around indefinitely and we'd count the page twice
6869 * Cgroup2 has separate resource counters for memory and swap,
6870 * so this is a non-issue here. Memory and swap charge lifetimes
6871 * correspond 1:1 to page and swap slot lifetimes: we charge the
6872 * page to memory here, and uncharge swap when the slot is freed.
6874 if (!mem_cgroup_disabled() && do_memsw_account()) {
6876 * The swap entry might not get freed for a long time,
6877 * let's not wait for it. The page already received a
6878 * memory+swap charge, drop the swap entry duplicate.
6880 mem_cgroup_uncharge_swap(entry, 1);
6884 struct uncharge_gather {
6885 struct mem_cgroup *memcg;
6886 unsigned long nr_memory;
6887 unsigned long pgpgout;
6888 unsigned long nr_kmem;
6892 static inline void uncharge_gather_clear(struct uncharge_gather *ug)
6894 memset(ug, 0, sizeof(*ug));
6897 static void uncharge_batch(const struct uncharge_gather *ug)
6899 unsigned long flags;
6901 if (ug->nr_memory) {
6902 page_counter_uncharge(&ug->memcg->memory, ug->nr_memory);
6903 if (do_memsw_account())
6904 page_counter_uncharge(&ug->memcg->memsw, ug->nr_memory);
6906 memcg_account_kmem(ug->memcg, -ug->nr_kmem);
6907 memcg_oom_recover(ug->memcg);
6910 local_irq_save(flags);
6911 __count_memcg_events(ug->memcg, PGPGOUT, ug->pgpgout);
6912 __this_cpu_add(ug->memcg->vmstats_percpu->nr_page_events, ug->nr_memory);
6913 memcg_check_events(ug->memcg, ug->nid);
6914 local_irq_restore(flags);
6916 /* drop reference from uncharge_folio */
6917 css_put(&ug->memcg->css);
6920 static void uncharge_folio(struct folio *folio, struct uncharge_gather *ug)
6923 struct mem_cgroup *memcg;
6924 struct obj_cgroup *objcg;
6926 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
6929 * Nobody should be changing or seriously looking at
6930 * folio memcg or objcg at this point, we have fully
6931 * exclusive access to the folio.
6933 if (folio_memcg_kmem(folio)) {
6934 objcg = __folio_objcg(folio);
6936 * This get matches the put at the end of the function and
6937 * kmem pages do not hold memcg references anymore.
6939 memcg = get_mem_cgroup_from_objcg(objcg);
6941 memcg = __folio_memcg(folio);
6947 if (ug->memcg != memcg) {
6950 uncharge_gather_clear(ug);
6953 ug->nid = folio_nid(folio);
6955 /* pairs with css_put in uncharge_batch */
6956 css_get(&memcg->css);
6959 nr_pages = folio_nr_pages(folio);
6961 if (folio_memcg_kmem(folio)) {
6962 ug->nr_memory += nr_pages;
6963 ug->nr_kmem += nr_pages;
6965 folio->memcg_data = 0;
6966 obj_cgroup_put(objcg);
6968 /* LRU pages aren't accounted at the root level */
6969 if (!mem_cgroup_is_root(memcg))
6970 ug->nr_memory += nr_pages;
6973 folio->memcg_data = 0;
6976 css_put(&memcg->css);
6979 void __mem_cgroup_uncharge(struct folio *folio)
6981 struct uncharge_gather ug;
6983 /* Don't touch folio->lru of any random page, pre-check: */
6984 if (!folio_memcg(folio))
6987 uncharge_gather_clear(&ug);
6988 uncharge_folio(folio, &ug);
6989 uncharge_batch(&ug);
6993 * __mem_cgroup_uncharge_list - uncharge a list of page
6994 * @page_list: list of pages to uncharge
6996 * Uncharge a list of pages previously charged with
6997 * __mem_cgroup_charge().
6999 void __mem_cgroup_uncharge_list(struct list_head *page_list)
7001 struct uncharge_gather ug;
7002 struct folio *folio;
7004 uncharge_gather_clear(&ug);
7005 list_for_each_entry(folio, page_list, lru)
7006 uncharge_folio(folio, &ug);
7008 uncharge_batch(&ug);
7012 * mem_cgroup_migrate - Charge a folio's replacement.
7013 * @old: Currently circulating folio.
7014 * @new: Replacement folio.
7016 * Charge @new as a replacement folio for @old. @old will
7017 * be uncharged upon free.
7019 * Both folios must be locked, @new->mapping must be set up.
7021 void mem_cgroup_migrate(struct folio *old, struct folio *new)
7023 struct mem_cgroup *memcg;
7024 long nr_pages = folio_nr_pages(new);
7025 unsigned long flags;
7027 VM_BUG_ON_FOLIO(!folio_test_locked(old), old);
7028 VM_BUG_ON_FOLIO(!folio_test_locked(new), new);
7029 VM_BUG_ON_FOLIO(folio_test_anon(old) != folio_test_anon(new), new);
7030 VM_BUG_ON_FOLIO(folio_nr_pages(old) != nr_pages, new);
7032 if (mem_cgroup_disabled())
7035 /* Page cache replacement: new folio already charged? */
7036 if (folio_memcg(new))
7039 memcg = folio_memcg(old);
7040 VM_WARN_ON_ONCE_FOLIO(!memcg, old);
7044 /* Force-charge the new page. The old one will be freed soon */
7045 if (!mem_cgroup_is_root(memcg)) {
7046 page_counter_charge(&memcg->memory, nr_pages);
7047 if (do_memsw_account())
7048 page_counter_charge(&memcg->memsw, nr_pages);
7051 css_get(&memcg->css);
7052 commit_charge(new, memcg);
7054 local_irq_save(flags);
7055 mem_cgroup_charge_statistics(memcg, nr_pages);
7056 memcg_check_events(memcg, folio_nid(new));
7057 local_irq_restore(flags);
7060 DEFINE_STATIC_KEY_FALSE(memcg_sockets_enabled_key);
7061 EXPORT_SYMBOL(memcg_sockets_enabled_key);
7063 void mem_cgroup_sk_alloc(struct sock *sk)
7065 struct mem_cgroup *memcg;
7067 if (!mem_cgroup_sockets_enabled)
7070 /* Do not associate the sock with unrelated interrupted task's memcg. */
7075 memcg = mem_cgroup_from_task(current);
7076 if (memcg == root_mem_cgroup)
7078 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && !memcg->tcpmem_active)
7080 if (css_tryget(&memcg->css))
7081 sk->sk_memcg = memcg;
7086 void mem_cgroup_sk_free(struct sock *sk)
7089 css_put(&sk->sk_memcg->css);
7093 * mem_cgroup_charge_skmem - charge socket memory
7094 * @memcg: memcg to charge
7095 * @nr_pages: number of pages to charge
7096 * @gfp_mask: reclaim mode
7098 * Charges @nr_pages to @memcg. Returns %true if the charge fit within
7099 * @memcg's configured limit, %false if it doesn't.
7101 bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
7104 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
7105 struct page_counter *fail;
7107 if (page_counter_try_charge(&memcg->tcpmem, nr_pages, &fail)) {
7108 memcg->tcpmem_pressure = 0;
7111 memcg->tcpmem_pressure = 1;
7112 if (gfp_mask & __GFP_NOFAIL) {
7113 page_counter_charge(&memcg->tcpmem, nr_pages);
7119 if (try_charge(memcg, gfp_mask, nr_pages) == 0) {
7120 mod_memcg_state(memcg, MEMCG_SOCK, nr_pages);
7128 * mem_cgroup_uncharge_skmem - uncharge socket memory
7129 * @memcg: memcg to uncharge
7130 * @nr_pages: number of pages to uncharge
7132 void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages)
7134 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
7135 page_counter_uncharge(&memcg->tcpmem, nr_pages);
7139 mod_memcg_state(memcg, MEMCG_SOCK, -nr_pages);
7141 refill_stock(memcg, nr_pages);
7144 static int __init cgroup_memory(char *s)
7148 while ((token = strsep(&s, ",")) != NULL) {
7151 if (!strcmp(token, "nosocket"))
7152 cgroup_memory_nosocket = true;
7153 if (!strcmp(token, "nokmem"))
7154 cgroup_memory_nokmem = true;
7158 __setup("cgroup.memory=", cgroup_memory);
7161 * subsys_initcall() for memory controller.
7163 * Some parts like memcg_hotplug_cpu_dead() have to be initialized from this
7164 * context because of lock dependencies (cgroup_lock -> cpu hotplug) but
7165 * basically everything that doesn't depend on a specific mem_cgroup structure
7166 * should be initialized from here.
7168 static int __init mem_cgroup_init(void)
7173 * Currently s32 type (can refer to struct batched_lruvec_stat) is
7174 * used for per-memcg-per-cpu caching of per-node statistics. In order
7175 * to work fine, we should make sure that the overfill threshold can't
7176 * exceed S32_MAX / PAGE_SIZE.
7178 BUILD_BUG_ON(MEMCG_CHARGE_BATCH > S32_MAX / PAGE_SIZE);
7180 cpuhp_setup_state_nocalls(CPUHP_MM_MEMCQ_DEAD, "mm/memctrl:dead", NULL,
7181 memcg_hotplug_cpu_dead);
7183 for_each_possible_cpu(cpu)
7184 INIT_WORK(&per_cpu_ptr(&memcg_stock, cpu)->work,
7187 for_each_node(node) {
7188 struct mem_cgroup_tree_per_node *rtpn;
7190 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL,
7191 node_online(node) ? node : NUMA_NO_NODE);
7193 rtpn->rb_root = RB_ROOT;
7194 rtpn->rb_rightmost = NULL;
7195 spin_lock_init(&rtpn->lock);
7196 soft_limit_tree.rb_tree_per_node[node] = rtpn;
7201 subsys_initcall(mem_cgroup_init);
7203 #ifdef CONFIG_MEMCG_SWAP
7204 static struct mem_cgroup *mem_cgroup_id_get_online(struct mem_cgroup *memcg)
7206 while (!refcount_inc_not_zero(&memcg->id.ref)) {
7208 * The root cgroup cannot be destroyed, so it's refcount must
7211 if (WARN_ON_ONCE(memcg == root_mem_cgroup)) {
7215 memcg = parent_mem_cgroup(memcg);
7217 memcg = root_mem_cgroup;
7223 * mem_cgroup_swapout - transfer a memsw charge to swap
7224 * @folio: folio whose memsw charge to transfer
7225 * @entry: swap entry to move the charge to
7227 * Transfer the memsw charge of @folio to @entry.
7229 void mem_cgroup_swapout(struct folio *folio, swp_entry_t entry)
7231 struct mem_cgroup *memcg, *swap_memcg;
7232 unsigned int nr_entries;
7233 unsigned short oldid;
7235 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
7236 VM_BUG_ON_FOLIO(folio_ref_count(folio), folio);
7238 if (mem_cgroup_disabled())
7241 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
7244 memcg = folio_memcg(folio);
7246 VM_WARN_ON_ONCE_FOLIO(!memcg, folio);
7251 * In case the memcg owning these pages has been offlined and doesn't
7252 * have an ID allocated to it anymore, charge the closest online
7253 * ancestor for the swap instead and transfer the memory+swap charge.
7255 swap_memcg = mem_cgroup_id_get_online(memcg);
7256 nr_entries = folio_nr_pages(folio);
7257 /* Get references for the tail pages, too */
7259 mem_cgroup_id_get_many(swap_memcg, nr_entries - 1);
7260 oldid = swap_cgroup_record(entry, mem_cgroup_id(swap_memcg),
7262 VM_BUG_ON_FOLIO(oldid, folio);
7263 mod_memcg_state(swap_memcg, MEMCG_SWAP, nr_entries);
7265 folio->memcg_data = 0;
7267 if (!mem_cgroup_is_root(memcg))
7268 page_counter_uncharge(&memcg->memory, nr_entries);
7270 if (!cgroup_memory_noswap && memcg != swap_memcg) {
7271 if (!mem_cgroup_is_root(swap_memcg))
7272 page_counter_charge(&swap_memcg->memsw, nr_entries);
7273 page_counter_uncharge(&memcg->memsw, nr_entries);
7277 * Interrupts should be disabled here because the caller holds the
7278 * i_pages lock which is taken with interrupts-off. It is
7279 * important here to have the interrupts disabled because it is the
7280 * only synchronisation we have for updating the per-CPU variables.
7283 mem_cgroup_charge_statistics(memcg, -nr_entries);
7284 memcg_stats_unlock();
7285 memcg_check_events(memcg, folio_nid(folio));
7287 css_put(&memcg->css);
7291 * __mem_cgroup_try_charge_swap - try charging swap space for a folio
7292 * @folio: folio being added to swap
7293 * @entry: swap entry to charge
7295 * Try to charge @folio's memcg for the swap space at @entry.
7297 * Returns 0 on success, -ENOMEM on failure.
7299 int __mem_cgroup_try_charge_swap(struct folio *folio, swp_entry_t entry)
7301 unsigned int nr_pages = folio_nr_pages(folio);
7302 struct page_counter *counter;
7303 struct mem_cgroup *memcg;
7304 unsigned short oldid;
7306 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
7309 memcg = folio_memcg(folio);
7311 VM_WARN_ON_ONCE_FOLIO(!memcg, folio);
7316 memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
7320 memcg = mem_cgroup_id_get_online(memcg);
7322 if (!cgroup_memory_noswap && !mem_cgroup_is_root(memcg) &&
7323 !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) {
7324 memcg_memory_event(memcg, MEMCG_SWAP_MAX);
7325 memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
7326 mem_cgroup_id_put(memcg);
7330 /* Get references for the tail pages, too */
7332 mem_cgroup_id_get_many(memcg, nr_pages - 1);
7333 oldid = swap_cgroup_record(entry, mem_cgroup_id(memcg), nr_pages);
7334 VM_BUG_ON_FOLIO(oldid, folio);
7335 mod_memcg_state(memcg, MEMCG_SWAP, nr_pages);
7341 * __mem_cgroup_uncharge_swap - uncharge swap space
7342 * @entry: swap entry to uncharge
7343 * @nr_pages: the amount of swap space to uncharge
7345 void __mem_cgroup_uncharge_swap(swp_entry_t entry, unsigned int nr_pages)
7347 struct mem_cgroup *memcg;
7350 id = swap_cgroup_record(entry, 0, nr_pages);
7352 memcg = mem_cgroup_from_id(id);
7354 if (!cgroup_memory_noswap && !mem_cgroup_is_root(memcg)) {
7355 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
7356 page_counter_uncharge(&memcg->swap, nr_pages);
7358 page_counter_uncharge(&memcg->memsw, nr_pages);
7360 mod_memcg_state(memcg, MEMCG_SWAP, -nr_pages);
7361 mem_cgroup_id_put_many(memcg, nr_pages);
7366 long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg)
7368 long nr_swap_pages = get_nr_swap_pages();
7370 if (cgroup_memory_noswap || !cgroup_subsys_on_dfl(memory_cgrp_subsys))
7371 return nr_swap_pages;
7372 for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg))
7373 nr_swap_pages = min_t(long, nr_swap_pages,
7374 READ_ONCE(memcg->swap.max) -
7375 page_counter_read(&memcg->swap));
7376 return nr_swap_pages;
7379 bool mem_cgroup_swap_full(struct page *page)
7381 struct mem_cgroup *memcg;
7383 VM_BUG_ON_PAGE(!PageLocked(page), page);
7387 if (cgroup_memory_noswap || !cgroup_subsys_on_dfl(memory_cgrp_subsys))
7390 memcg = page_memcg(page);
7394 for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg)) {
7395 unsigned long usage = page_counter_read(&memcg->swap);
7397 if (usage * 2 >= READ_ONCE(memcg->swap.high) ||
7398 usage * 2 >= READ_ONCE(memcg->swap.max))
7405 static int __init setup_swap_account(char *s)
7407 if (!strcmp(s, "1"))
7408 cgroup_memory_noswap = false;
7409 else if (!strcmp(s, "0"))
7410 cgroup_memory_noswap = true;
7413 __setup("swapaccount=", setup_swap_account);
7415 static u64 swap_current_read(struct cgroup_subsys_state *css,
7418 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
7420 return (u64)page_counter_read(&memcg->swap) * PAGE_SIZE;
7423 static int swap_high_show(struct seq_file *m, void *v)
7425 return seq_puts_memcg_tunable(m,
7426 READ_ONCE(mem_cgroup_from_seq(m)->swap.high));
7429 static ssize_t swap_high_write(struct kernfs_open_file *of,
7430 char *buf, size_t nbytes, loff_t off)
7432 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
7436 buf = strstrip(buf);
7437 err = page_counter_memparse(buf, "max", &high);
7441 page_counter_set_high(&memcg->swap, high);
7446 static int swap_max_show(struct seq_file *m, void *v)
7448 return seq_puts_memcg_tunable(m,
7449 READ_ONCE(mem_cgroup_from_seq(m)->swap.max));
7452 static ssize_t swap_max_write(struct kernfs_open_file *of,
7453 char *buf, size_t nbytes, loff_t off)
7455 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
7459 buf = strstrip(buf);
7460 err = page_counter_memparse(buf, "max", &max);
7464 xchg(&memcg->swap.max, max);
7469 static int swap_events_show(struct seq_file *m, void *v)
7471 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
7473 seq_printf(m, "high %lu\n",
7474 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_HIGH]));
7475 seq_printf(m, "max %lu\n",
7476 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_MAX]));
7477 seq_printf(m, "fail %lu\n",
7478 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_FAIL]));
7483 static struct cftype swap_files[] = {
7485 .name = "swap.current",
7486 .flags = CFTYPE_NOT_ON_ROOT,
7487 .read_u64 = swap_current_read,
7490 .name = "swap.high",
7491 .flags = CFTYPE_NOT_ON_ROOT,
7492 .seq_show = swap_high_show,
7493 .write = swap_high_write,
7497 .flags = CFTYPE_NOT_ON_ROOT,
7498 .seq_show = swap_max_show,
7499 .write = swap_max_write,
7502 .name = "swap.events",
7503 .flags = CFTYPE_NOT_ON_ROOT,
7504 .file_offset = offsetof(struct mem_cgroup, swap_events_file),
7505 .seq_show = swap_events_show,
7510 static struct cftype memsw_files[] = {
7512 .name = "memsw.usage_in_bytes",
7513 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
7514 .read_u64 = mem_cgroup_read_u64,
7517 .name = "memsw.max_usage_in_bytes",
7518 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
7519 .write = mem_cgroup_reset,
7520 .read_u64 = mem_cgroup_read_u64,
7523 .name = "memsw.limit_in_bytes",
7524 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
7525 .write = mem_cgroup_write,
7526 .read_u64 = mem_cgroup_read_u64,
7529 .name = "memsw.failcnt",
7530 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
7531 .write = mem_cgroup_reset,
7532 .read_u64 = mem_cgroup_read_u64,
7534 { }, /* terminate */
7537 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
7539 * obj_cgroup_may_zswap - check if this cgroup can zswap
7540 * @objcg: the object cgroup
7542 * Check if the hierarchical zswap limit has been reached.
7544 * This doesn't check for specific headroom, and it is not atomic
7545 * either. But with zswap, the size of the allocation is only known
7546 * once compression has occured, and this optimistic pre-check avoids
7547 * spending cycles on compression when there is already no room left
7548 * or zswap is disabled altogether somewhere in the hierarchy.
7550 bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
7552 struct mem_cgroup *memcg, *original_memcg;
7555 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
7558 original_memcg = get_mem_cgroup_from_objcg(objcg);
7559 for (memcg = original_memcg; memcg != root_mem_cgroup;
7560 memcg = parent_mem_cgroup(memcg)) {
7561 unsigned long max = READ_ONCE(memcg->zswap_max);
7562 unsigned long pages;
7564 if (max == PAGE_COUNTER_MAX)
7571 cgroup_rstat_flush(memcg->css.cgroup);
7572 pages = memcg_page_state(memcg, MEMCG_ZSWAP_B) / PAGE_SIZE;
7578 mem_cgroup_put(original_memcg);
7583 * obj_cgroup_charge_zswap - charge compression backend memory
7584 * @objcg: the object cgroup
7585 * @size: size of compressed object
7587 * This forces the charge after obj_cgroup_may_swap() allowed
7588 * compression and storage in zwap for this cgroup to go ahead.
7590 void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size)
7592 struct mem_cgroup *memcg;
7594 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
7597 VM_WARN_ON_ONCE(!(current->flags & PF_MEMALLOC));
7599 /* PF_MEMALLOC context, charging must succeed */
7600 if (obj_cgroup_charge(objcg, GFP_KERNEL, size))
7604 memcg = obj_cgroup_memcg(objcg);
7605 mod_memcg_state(memcg, MEMCG_ZSWAP_B, size);
7606 mod_memcg_state(memcg, MEMCG_ZSWAPPED, 1);
7611 * obj_cgroup_uncharge_zswap - uncharge compression backend memory
7612 * @objcg: the object cgroup
7613 * @size: size of compressed object
7615 * Uncharges zswap memory on page in.
7617 void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size)
7619 struct mem_cgroup *memcg;
7621 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
7624 obj_cgroup_uncharge(objcg, size);
7627 memcg = obj_cgroup_memcg(objcg);
7628 mod_memcg_state(memcg, MEMCG_ZSWAP_B, -size);
7629 mod_memcg_state(memcg, MEMCG_ZSWAPPED, -1);
7633 static u64 zswap_current_read(struct cgroup_subsys_state *css,
7636 cgroup_rstat_flush(css->cgroup);
7637 return memcg_page_state(mem_cgroup_from_css(css), MEMCG_ZSWAP_B);
7640 static int zswap_max_show(struct seq_file *m, void *v)
7642 return seq_puts_memcg_tunable(m,
7643 READ_ONCE(mem_cgroup_from_seq(m)->zswap_max));
7646 static ssize_t zswap_max_write(struct kernfs_open_file *of,
7647 char *buf, size_t nbytes, loff_t off)
7649 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
7653 buf = strstrip(buf);
7654 err = page_counter_memparse(buf, "max", &max);
7658 xchg(&memcg->zswap_max, max);
7663 static struct cftype zswap_files[] = {
7665 .name = "zswap.current",
7666 .flags = CFTYPE_NOT_ON_ROOT,
7667 .read_u64 = zswap_current_read,
7670 .name = "zswap.max",
7671 .flags = CFTYPE_NOT_ON_ROOT,
7672 .seq_show = zswap_max_show,
7673 .write = zswap_max_write,
7677 #endif /* CONFIG_MEMCG_KMEM && CONFIG_ZSWAP */
7680 * If mem_cgroup_swap_init() is implemented as a subsys_initcall()
7681 * instead of a core_initcall(), this could mean cgroup_memory_noswap still
7682 * remains set to false even when memcg is disabled via "cgroup_disable=memory"
7683 * boot parameter. This may result in premature OOPS inside
7684 * mem_cgroup_get_nr_swap_pages() function in corner cases.
7686 static int __init mem_cgroup_swap_init(void)
7688 /* No memory control -> no swap control */
7689 if (mem_cgroup_disabled())
7690 cgroup_memory_noswap = true;
7692 if (cgroup_memory_noswap)
7695 WARN_ON(cgroup_add_dfl_cftypes(&memory_cgrp_subsys, swap_files));
7696 WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, memsw_files));
7697 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
7698 WARN_ON(cgroup_add_dfl_cftypes(&memory_cgrp_subsys, zswap_files));
7702 core_initcall(mem_cgroup_swap_init);
7704 #endif /* CONFIG_MEMCG_SWAP */