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>
66 #include <linux/parser.h>
73 #include <linux/uaccess.h>
75 #include <trace/events/vmscan.h>
77 struct cgroup_subsys memory_cgrp_subsys __read_mostly;
78 EXPORT_SYMBOL(memory_cgrp_subsys);
80 struct mem_cgroup *root_mem_cgroup __read_mostly;
82 /* Active memory cgroup to use from an interrupt context */
83 DEFINE_PER_CPU(struct mem_cgroup *, int_active_memcg);
84 EXPORT_PER_CPU_SYMBOL_GPL(int_active_memcg);
86 /* Socket memory accounting disabled? */
87 static bool cgroup_memory_nosocket __ro_after_init;
89 /* Kernel memory accounting disabled? */
90 static bool cgroup_memory_nokmem __ro_after_init;
92 #ifdef CONFIG_CGROUP_WRITEBACK
93 static DECLARE_WAIT_QUEUE_HEAD(memcg_cgwb_frn_waitq);
96 /* Whether legacy memory+swap accounting is active */
97 static bool do_memsw_account(void)
99 return !cgroup_subsys_on_dfl(memory_cgrp_subsys);
102 #define THRESHOLDS_EVENTS_TARGET 128
103 #define SOFTLIMIT_EVENTS_TARGET 1024
106 * Cgroups above their limits are maintained in a RB-Tree, independent of
107 * their hierarchy representation
110 struct mem_cgroup_tree_per_node {
111 struct rb_root rb_root;
112 struct rb_node *rb_rightmost;
116 struct mem_cgroup_tree {
117 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
120 static struct mem_cgroup_tree soft_limit_tree __read_mostly;
123 struct mem_cgroup_eventfd_list {
124 struct list_head list;
125 struct eventfd_ctx *eventfd;
129 * cgroup_event represents events which userspace want to receive.
131 struct mem_cgroup_event {
133 * memcg which the event belongs to.
135 struct mem_cgroup *memcg;
137 * eventfd to signal userspace about the event.
139 struct eventfd_ctx *eventfd;
141 * Each of these stored in a list by the cgroup.
143 struct list_head list;
145 * register_event() callback will be used to add new userspace
146 * waiter for changes related to this event. Use eventfd_signal()
147 * on eventfd to send notification to userspace.
149 int (*register_event)(struct mem_cgroup *memcg,
150 struct eventfd_ctx *eventfd, const char *args);
152 * unregister_event() callback will be called when userspace closes
153 * the eventfd or on cgroup removing. This callback must be set,
154 * if you want provide notification functionality.
156 void (*unregister_event)(struct mem_cgroup *memcg,
157 struct eventfd_ctx *eventfd);
159 * All fields below needed to unregister event when
160 * userspace closes eventfd.
163 wait_queue_head_t *wqh;
164 wait_queue_entry_t wait;
165 struct work_struct remove;
168 static void mem_cgroup_threshold(struct mem_cgroup *memcg);
169 static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
171 /* Stuffs for move charges at task migration. */
173 * Types of charges to be moved.
175 #define MOVE_ANON 0x1U
176 #define MOVE_FILE 0x2U
177 #define MOVE_MASK (MOVE_ANON | MOVE_FILE)
179 /* "mc" and its members are protected by cgroup_mutex */
180 static struct move_charge_struct {
181 spinlock_t lock; /* for from, to */
182 struct mm_struct *mm;
183 struct mem_cgroup *from;
184 struct mem_cgroup *to;
186 unsigned long precharge;
187 unsigned long moved_charge;
188 unsigned long moved_swap;
189 struct task_struct *moving_task; /* a task moving charges */
190 wait_queue_head_t waitq; /* a waitq for other context */
192 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
193 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
197 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
198 * limit reclaim to prevent infinite loops, if they ever occur.
200 #define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
201 #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
203 /* for encoding cft->private value on file */
211 #define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
212 #define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
213 #define MEMFILE_ATTR(val) ((val) & 0xffff)
216 * Iteration constructs for visiting all cgroups (under a tree). If
217 * loops are exited prematurely (break), mem_cgroup_iter_break() must
218 * be used for reference counting.
220 #define for_each_mem_cgroup_tree(iter, root) \
221 for (iter = mem_cgroup_iter(root, NULL, NULL); \
223 iter = mem_cgroup_iter(root, iter, NULL))
225 #define for_each_mem_cgroup(iter) \
226 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
228 iter = mem_cgroup_iter(NULL, iter, NULL))
230 static inline bool task_is_dying(void)
232 return tsk_is_oom_victim(current) || fatal_signal_pending(current) ||
233 (current->flags & PF_EXITING);
236 /* Some nice accessors for the vmpressure. */
237 struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg)
240 memcg = root_mem_cgroup;
241 return &memcg->vmpressure;
244 struct mem_cgroup *vmpressure_to_memcg(struct vmpressure *vmpr)
246 return container_of(vmpr, struct mem_cgroup, vmpressure);
249 #ifdef CONFIG_MEMCG_KMEM
250 static DEFINE_SPINLOCK(objcg_lock);
252 bool mem_cgroup_kmem_disabled(void)
254 return cgroup_memory_nokmem;
257 static void obj_cgroup_uncharge_pages(struct obj_cgroup *objcg,
258 unsigned int nr_pages);
260 static void obj_cgroup_release(struct percpu_ref *ref)
262 struct obj_cgroup *objcg = container_of(ref, struct obj_cgroup, refcnt);
263 unsigned int nr_bytes;
264 unsigned int nr_pages;
268 * At this point all allocated objects are freed, and
269 * objcg->nr_charged_bytes can't have an arbitrary byte value.
270 * However, it can be PAGE_SIZE or (x * PAGE_SIZE).
272 * The following sequence can lead to it:
273 * 1) CPU0: objcg == stock->cached_objcg
274 * 2) CPU1: we do a small allocation (e.g. 92 bytes),
275 * PAGE_SIZE bytes are charged
276 * 3) CPU1: a process from another memcg is allocating something,
277 * the stock if flushed,
278 * objcg->nr_charged_bytes = PAGE_SIZE - 92
279 * 5) CPU0: we do release this object,
280 * 92 bytes are added to stock->nr_bytes
281 * 6) CPU0: stock is flushed,
282 * 92 bytes are added to objcg->nr_charged_bytes
284 * In the result, nr_charged_bytes == PAGE_SIZE.
285 * This page will be uncharged in obj_cgroup_release().
287 nr_bytes = atomic_read(&objcg->nr_charged_bytes);
288 WARN_ON_ONCE(nr_bytes & (PAGE_SIZE - 1));
289 nr_pages = nr_bytes >> PAGE_SHIFT;
292 obj_cgroup_uncharge_pages(objcg, nr_pages);
294 spin_lock_irqsave(&objcg_lock, flags);
295 list_del(&objcg->list);
296 spin_unlock_irqrestore(&objcg_lock, flags);
298 percpu_ref_exit(ref);
299 kfree_rcu(objcg, rcu);
302 static struct obj_cgroup *obj_cgroup_alloc(void)
304 struct obj_cgroup *objcg;
307 objcg = kzalloc(sizeof(struct obj_cgroup), GFP_KERNEL);
311 ret = percpu_ref_init(&objcg->refcnt, obj_cgroup_release, 0,
317 INIT_LIST_HEAD(&objcg->list);
321 static void memcg_reparent_objcgs(struct mem_cgroup *memcg,
322 struct mem_cgroup *parent)
324 struct obj_cgroup *objcg, *iter;
326 objcg = rcu_replace_pointer(memcg->objcg, NULL, true);
328 spin_lock_irq(&objcg_lock);
330 /* 1) Ready to reparent active objcg. */
331 list_add(&objcg->list, &memcg->objcg_list);
332 /* 2) Reparent active objcg and already reparented objcgs to parent. */
333 list_for_each_entry(iter, &memcg->objcg_list, list)
334 WRITE_ONCE(iter->memcg, parent);
335 /* 3) Move already reparented objcgs to the parent's list */
336 list_splice(&memcg->objcg_list, &parent->objcg_list);
338 spin_unlock_irq(&objcg_lock);
340 percpu_ref_kill(&objcg->refcnt);
344 * A lot of the calls to the cache allocation functions are expected to be
345 * inlined by the compiler. Since the calls to memcg_slab_pre_alloc_hook() are
346 * conditional to this static branch, we'll have to allow modules that does
347 * kmem_cache_alloc and the such to see this symbol as well
349 DEFINE_STATIC_KEY_FALSE(memcg_kmem_enabled_key);
350 EXPORT_SYMBOL(memcg_kmem_enabled_key);
354 * mem_cgroup_css_from_page - css of the memcg associated with a page
355 * @page: page of interest
357 * If memcg is bound to the default hierarchy, css of the memcg associated
358 * with @page is returned. The returned css remains associated with @page
359 * until it is released.
361 * If memcg is bound to a traditional hierarchy, the css of root_mem_cgroup
364 struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page)
366 struct mem_cgroup *memcg;
368 memcg = page_memcg(page);
370 if (!memcg || !cgroup_subsys_on_dfl(memory_cgrp_subsys))
371 memcg = root_mem_cgroup;
377 * page_cgroup_ino - return inode number of the memcg a page is charged to
380 * Look up the closest online ancestor of the memory cgroup @page is charged to
381 * and return its inode number or 0 if @page is not charged to any cgroup. It
382 * is safe to call this function without holding a reference to @page.
384 * Note, this function is inherently racy, because there is nothing to prevent
385 * the cgroup inode from getting torn down and potentially reallocated a moment
386 * after page_cgroup_ino() returns, so it only should be used by callers that
387 * do not care (such as procfs interfaces).
389 ino_t page_cgroup_ino(struct page *page)
391 struct mem_cgroup *memcg;
392 unsigned long ino = 0;
395 memcg = page_memcg_check(page);
397 while (memcg && !(memcg->css.flags & CSS_ONLINE))
398 memcg = parent_mem_cgroup(memcg);
400 ino = cgroup_ino(memcg->css.cgroup);
405 static void __mem_cgroup_insert_exceeded(struct mem_cgroup_per_node *mz,
406 struct mem_cgroup_tree_per_node *mctz,
407 unsigned long new_usage_in_excess)
409 struct rb_node **p = &mctz->rb_root.rb_node;
410 struct rb_node *parent = NULL;
411 struct mem_cgroup_per_node *mz_node;
412 bool rightmost = true;
417 mz->usage_in_excess = new_usage_in_excess;
418 if (!mz->usage_in_excess)
422 mz_node = rb_entry(parent, struct mem_cgroup_per_node,
424 if (mz->usage_in_excess < mz_node->usage_in_excess) {
433 mctz->rb_rightmost = &mz->tree_node;
435 rb_link_node(&mz->tree_node, parent, p);
436 rb_insert_color(&mz->tree_node, &mctz->rb_root);
440 static void __mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz,
441 struct mem_cgroup_tree_per_node *mctz)
446 if (&mz->tree_node == mctz->rb_rightmost)
447 mctz->rb_rightmost = rb_prev(&mz->tree_node);
449 rb_erase(&mz->tree_node, &mctz->rb_root);
453 static void mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz,
454 struct mem_cgroup_tree_per_node *mctz)
458 spin_lock_irqsave(&mctz->lock, flags);
459 __mem_cgroup_remove_exceeded(mz, mctz);
460 spin_unlock_irqrestore(&mctz->lock, flags);
463 static unsigned long soft_limit_excess(struct mem_cgroup *memcg)
465 unsigned long nr_pages = page_counter_read(&memcg->memory);
466 unsigned long soft_limit = READ_ONCE(memcg->soft_limit);
467 unsigned long excess = 0;
469 if (nr_pages > soft_limit)
470 excess = nr_pages - soft_limit;
475 static void mem_cgroup_update_tree(struct mem_cgroup *memcg, int nid)
477 unsigned long excess;
478 struct mem_cgroup_per_node *mz;
479 struct mem_cgroup_tree_per_node *mctz;
481 mctz = soft_limit_tree.rb_tree_per_node[nid];
485 * Necessary to update all ancestors when hierarchy is used.
486 * because their event counter is not touched.
488 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
489 mz = memcg->nodeinfo[nid];
490 excess = soft_limit_excess(memcg);
492 * We have to update the tree if mz is on RB-tree or
493 * mem is over its softlimit.
495 if (excess || mz->on_tree) {
498 spin_lock_irqsave(&mctz->lock, flags);
499 /* if on-tree, remove it */
501 __mem_cgroup_remove_exceeded(mz, mctz);
503 * Insert again. mz->usage_in_excess will be updated.
504 * If excess is 0, no tree ops.
506 __mem_cgroup_insert_exceeded(mz, mctz, excess);
507 spin_unlock_irqrestore(&mctz->lock, flags);
512 static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
514 struct mem_cgroup_tree_per_node *mctz;
515 struct mem_cgroup_per_node *mz;
519 mz = memcg->nodeinfo[nid];
520 mctz = soft_limit_tree.rb_tree_per_node[nid];
522 mem_cgroup_remove_exceeded(mz, mctz);
526 static struct mem_cgroup_per_node *
527 __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz)
529 struct mem_cgroup_per_node *mz;
533 if (!mctz->rb_rightmost)
534 goto done; /* Nothing to reclaim from */
536 mz = rb_entry(mctz->rb_rightmost,
537 struct mem_cgroup_per_node, tree_node);
539 * Remove the node now but someone else can add it back,
540 * we will to add it back at the end of reclaim to its correct
541 * position in the tree.
543 __mem_cgroup_remove_exceeded(mz, mctz);
544 if (!soft_limit_excess(mz->memcg) ||
545 !css_tryget(&mz->memcg->css))
551 static struct mem_cgroup_per_node *
552 mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz)
554 struct mem_cgroup_per_node *mz;
556 spin_lock_irq(&mctz->lock);
557 mz = __mem_cgroup_largest_soft_limit_node(mctz);
558 spin_unlock_irq(&mctz->lock);
563 * memcg and lruvec stats flushing
565 * Many codepaths leading to stats update or read are performance sensitive and
566 * adding stats flushing in such codepaths is not desirable. So, to optimize the
567 * flushing the kernel does:
569 * 1) Periodically and asynchronously flush the stats every 2 seconds to not let
570 * rstat update tree grow unbounded.
572 * 2) Flush the stats synchronously on reader side only when there are more than
573 * (MEMCG_CHARGE_BATCH * nr_cpus) update events. Though this optimization
574 * will let stats be out of sync by atmost (MEMCG_CHARGE_BATCH * nr_cpus) but
575 * only for 2 seconds due to (1).
577 static void flush_memcg_stats_dwork(struct work_struct *w);
578 static DECLARE_DEFERRABLE_WORK(stats_flush_dwork, flush_memcg_stats_dwork);
579 static DEFINE_SPINLOCK(stats_flush_lock);
580 static DEFINE_PER_CPU(unsigned int, stats_updates);
581 static atomic_t stats_flush_threshold = ATOMIC_INIT(0);
582 static u64 flush_next_time;
584 #define FLUSH_TIME (2UL*HZ)
587 * Accessors to ensure that preemption is disabled on PREEMPT_RT because it can
588 * not rely on this as part of an acquired spinlock_t lock. These functions are
589 * never used in hardirq context on PREEMPT_RT and therefore disabling preemtion
592 static void memcg_stats_lock(void)
594 preempt_disable_nested();
595 VM_WARN_ON_IRQS_ENABLED();
598 static void __memcg_stats_lock(void)
600 preempt_disable_nested();
603 static void memcg_stats_unlock(void)
605 preempt_enable_nested();
608 static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val)
612 cgroup_rstat_updated(memcg->css.cgroup, smp_processor_id());
614 x = __this_cpu_add_return(stats_updates, abs(val));
615 if (x > MEMCG_CHARGE_BATCH) {
617 * If stats_flush_threshold exceeds the threshold
618 * (>num_online_cpus()), cgroup stats update will be triggered
619 * in __mem_cgroup_flush_stats(). Increasing this var further
620 * is redundant and simply adds overhead in atomic update.
622 if (atomic_read(&stats_flush_threshold) <= num_online_cpus())
623 atomic_add(x / MEMCG_CHARGE_BATCH, &stats_flush_threshold);
624 __this_cpu_write(stats_updates, 0);
628 static void __mem_cgroup_flush_stats(void)
632 if (!spin_trylock_irqsave(&stats_flush_lock, flag))
635 flush_next_time = jiffies_64 + 2*FLUSH_TIME;
636 cgroup_rstat_flush_irqsafe(root_mem_cgroup->css.cgroup);
637 atomic_set(&stats_flush_threshold, 0);
638 spin_unlock_irqrestore(&stats_flush_lock, flag);
641 void mem_cgroup_flush_stats(void)
643 if (atomic_read(&stats_flush_threshold) > num_online_cpus())
644 __mem_cgroup_flush_stats();
647 void mem_cgroup_flush_stats_delayed(void)
649 if (time_after64(jiffies_64, flush_next_time))
650 mem_cgroup_flush_stats();
653 static void flush_memcg_stats_dwork(struct work_struct *w)
655 __mem_cgroup_flush_stats();
656 queue_delayed_work(system_unbound_wq, &stats_flush_dwork, FLUSH_TIME);
659 /* Subset of vm_event_item to report for memcg event stats */
660 static const unsigned int memcg_vm_event_stat[] = {
676 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
680 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
686 #define NR_MEMCG_EVENTS ARRAY_SIZE(memcg_vm_event_stat)
687 static int mem_cgroup_events_index[NR_VM_EVENT_ITEMS] __read_mostly;
689 static void init_memcg_events(void)
693 for (i = 0; i < NR_MEMCG_EVENTS; ++i)
694 mem_cgroup_events_index[memcg_vm_event_stat[i]] = i + 1;
697 static inline int memcg_events_index(enum vm_event_item idx)
699 return mem_cgroup_events_index[idx] - 1;
702 struct memcg_vmstats_percpu {
703 /* Local (CPU and cgroup) page state & events */
704 long state[MEMCG_NR_STAT];
705 unsigned long events[NR_MEMCG_EVENTS];
707 /* Delta calculation for lockless upward propagation */
708 long state_prev[MEMCG_NR_STAT];
709 unsigned long events_prev[NR_MEMCG_EVENTS];
711 /* Cgroup1: threshold notifications & softlimit tree updates */
712 unsigned long nr_page_events;
713 unsigned long targets[MEM_CGROUP_NTARGETS];
716 struct memcg_vmstats {
717 /* Aggregated (CPU and subtree) page state & events */
718 long state[MEMCG_NR_STAT];
719 unsigned long events[NR_MEMCG_EVENTS];
721 /* Pending child counts during tree propagation */
722 long state_pending[MEMCG_NR_STAT];
723 unsigned long events_pending[NR_MEMCG_EVENTS];
726 unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx)
728 long x = READ_ONCE(memcg->vmstats->state[idx]);
737 * __mod_memcg_state - update cgroup memory statistics
738 * @memcg: the memory cgroup
739 * @idx: the stat item - can be enum memcg_stat_item or enum node_stat_item
740 * @val: delta to add to the counter, can be negative
742 void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val)
744 if (mem_cgroup_disabled())
747 __this_cpu_add(memcg->vmstats_percpu->state[idx], val);
748 memcg_rstat_updated(memcg, val);
751 /* idx can be of type enum memcg_stat_item or node_stat_item. */
752 static unsigned long memcg_page_state_local(struct mem_cgroup *memcg, int idx)
757 for_each_possible_cpu(cpu)
758 x += per_cpu(memcg->vmstats_percpu->state[idx], cpu);
766 void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
769 struct mem_cgroup_per_node *pn;
770 struct mem_cgroup *memcg;
772 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
776 * The caller from rmap relay on disabled preemption becase they never
777 * update their counter from in-interrupt context. For these two
778 * counters we check that the update is never performed from an
779 * interrupt context while other caller need to have disabled interrupt.
781 __memcg_stats_lock();
782 if (IS_ENABLED(CONFIG_DEBUG_VM)) {
787 case NR_SHMEM_PMDMAPPED:
788 case NR_FILE_PMDMAPPED:
789 WARN_ON_ONCE(!in_task());
792 VM_WARN_ON_IRQS_ENABLED();
797 __this_cpu_add(memcg->vmstats_percpu->state[idx], val);
800 __this_cpu_add(pn->lruvec_stats_percpu->state[idx], val);
802 memcg_rstat_updated(memcg, val);
803 memcg_stats_unlock();
807 * __mod_lruvec_state - update lruvec memory statistics
808 * @lruvec: the lruvec
809 * @idx: the stat item
810 * @val: delta to add to the counter, can be negative
812 * The lruvec is the intersection of the NUMA node and a cgroup. This
813 * function updates the all three counters that are affected by a
814 * change of state at this level: per-node, per-cgroup, per-lruvec.
816 void __mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
820 __mod_node_page_state(lruvec_pgdat(lruvec), idx, val);
822 /* Update memcg and lruvec */
823 if (!mem_cgroup_disabled())
824 __mod_memcg_lruvec_state(lruvec, idx, val);
827 void __mod_lruvec_page_state(struct page *page, enum node_stat_item idx,
830 struct page *head = compound_head(page); /* rmap on tail pages */
831 struct mem_cgroup *memcg;
832 pg_data_t *pgdat = page_pgdat(page);
833 struct lruvec *lruvec;
836 memcg = page_memcg(head);
837 /* Untracked pages have no memcg, no lruvec. Update only the node */
840 __mod_node_page_state(pgdat, idx, val);
844 lruvec = mem_cgroup_lruvec(memcg, pgdat);
845 __mod_lruvec_state(lruvec, idx, val);
848 EXPORT_SYMBOL(__mod_lruvec_page_state);
850 void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val)
852 pg_data_t *pgdat = page_pgdat(virt_to_page(p));
853 struct mem_cgroup *memcg;
854 struct lruvec *lruvec;
857 memcg = mem_cgroup_from_slab_obj(p);
860 * Untracked pages have no memcg, no lruvec. Update only the
861 * node. If we reparent the slab objects to the root memcg,
862 * when we free the slab object, we need to update the per-memcg
863 * vmstats to keep it correct for the root memcg.
866 __mod_node_page_state(pgdat, idx, val);
868 lruvec = mem_cgroup_lruvec(memcg, pgdat);
869 __mod_lruvec_state(lruvec, idx, val);
875 * __count_memcg_events - account VM events in a cgroup
876 * @memcg: the memory cgroup
877 * @idx: the event item
878 * @count: the number of events that occurred
880 void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
883 int index = memcg_events_index(idx);
885 if (mem_cgroup_disabled() || index < 0)
889 __this_cpu_add(memcg->vmstats_percpu->events[index], count);
890 memcg_rstat_updated(memcg, count);
891 memcg_stats_unlock();
894 static unsigned long memcg_events(struct mem_cgroup *memcg, int event)
896 int index = memcg_events_index(event);
900 return READ_ONCE(memcg->vmstats->events[index]);
903 static unsigned long memcg_events_local(struct mem_cgroup *memcg, int event)
907 int index = memcg_events_index(event);
912 for_each_possible_cpu(cpu)
913 x += per_cpu(memcg->vmstats_percpu->events[index], cpu);
917 static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
920 /* pagein of a big page is an event. So, ignore page size */
922 __count_memcg_events(memcg, PGPGIN, 1);
924 __count_memcg_events(memcg, PGPGOUT, 1);
925 nr_pages = -nr_pages; /* for event */
928 __this_cpu_add(memcg->vmstats_percpu->nr_page_events, nr_pages);
931 static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
932 enum mem_cgroup_events_target target)
934 unsigned long val, next;
936 val = __this_cpu_read(memcg->vmstats_percpu->nr_page_events);
937 next = __this_cpu_read(memcg->vmstats_percpu->targets[target]);
938 /* from time_after() in jiffies.h */
939 if ((long)(next - val) < 0) {
941 case MEM_CGROUP_TARGET_THRESH:
942 next = val + THRESHOLDS_EVENTS_TARGET;
944 case MEM_CGROUP_TARGET_SOFTLIMIT:
945 next = val + SOFTLIMIT_EVENTS_TARGET;
950 __this_cpu_write(memcg->vmstats_percpu->targets[target], next);
957 * Check events in order.
960 static void memcg_check_events(struct mem_cgroup *memcg, int nid)
962 if (IS_ENABLED(CONFIG_PREEMPT_RT))
965 /* threshold event is triggered in finer grain than soft limit */
966 if (unlikely(mem_cgroup_event_ratelimit(memcg,
967 MEM_CGROUP_TARGET_THRESH))) {
970 do_softlimit = mem_cgroup_event_ratelimit(memcg,
971 MEM_CGROUP_TARGET_SOFTLIMIT);
972 mem_cgroup_threshold(memcg);
973 if (unlikely(do_softlimit))
974 mem_cgroup_update_tree(memcg, nid);
978 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
981 * mm_update_next_owner() may clear mm->owner to NULL
982 * if it races with swapoff, page migration, etc.
983 * So this can be called with p == NULL.
988 return mem_cgroup_from_css(task_css(p, memory_cgrp_id));
990 EXPORT_SYMBOL(mem_cgroup_from_task);
992 static __always_inline struct mem_cgroup *active_memcg(void)
995 return this_cpu_read(int_active_memcg);
997 return current->active_memcg;
1001 * get_mem_cgroup_from_mm: Obtain a reference on given mm_struct's memcg.
1002 * @mm: mm from which memcg should be extracted. It can be NULL.
1004 * Obtain a reference on mm->memcg and returns it if successful. If mm
1005 * is NULL, then the memcg is chosen as follows:
1006 * 1) The active memcg, if set.
1007 * 2) current->mm->memcg, if available
1009 * If mem_cgroup is disabled, NULL is returned.
1011 struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
1013 struct mem_cgroup *memcg;
1015 if (mem_cgroup_disabled())
1019 * Page cache insertions can happen without an
1020 * actual mm context, e.g. during disk probing
1021 * on boot, loopback IO, acct() writes etc.
1023 * No need to css_get on root memcg as the reference
1024 * counting is disabled on the root level in the
1025 * cgroup core. See CSS_NO_REF.
1027 if (unlikely(!mm)) {
1028 memcg = active_memcg();
1029 if (unlikely(memcg)) {
1030 /* remote memcg must hold a ref */
1031 css_get(&memcg->css);
1036 return root_mem_cgroup;
1041 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1042 if (unlikely(!memcg))
1043 memcg = root_mem_cgroup;
1044 } while (!css_tryget(&memcg->css));
1048 EXPORT_SYMBOL(get_mem_cgroup_from_mm);
1050 static __always_inline bool memcg_kmem_bypass(void)
1052 /* Allow remote memcg charging from any context. */
1053 if (unlikely(active_memcg()))
1056 /* Memcg to charge can't be determined. */
1057 if (!in_task() || !current->mm || (current->flags & PF_KTHREAD))
1064 * mem_cgroup_iter - iterate over memory cgroup hierarchy
1065 * @root: hierarchy root
1066 * @prev: previously returned memcg, NULL on first invocation
1067 * @reclaim: cookie for shared reclaim walks, NULL for full walks
1069 * Returns references to children of the hierarchy below @root, or
1070 * @root itself, or %NULL after a full round-trip.
1072 * Caller must pass the return value in @prev on subsequent
1073 * invocations for reference counting, or use mem_cgroup_iter_break()
1074 * to cancel a hierarchy walk before the round-trip is complete.
1076 * Reclaimers can specify a node in @reclaim to divide up the memcgs
1077 * in the hierarchy among all concurrent reclaimers operating on the
1080 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
1081 struct mem_cgroup *prev,
1082 struct mem_cgroup_reclaim_cookie *reclaim)
1084 struct mem_cgroup_reclaim_iter *iter;
1085 struct cgroup_subsys_state *css = NULL;
1086 struct mem_cgroup *memcg = NULL;
1087 struct mem_cgroup *pos = NULL;
1089 if (mem_cgroup_disabled())
1093 root = root_mem_cgroup;
1098 struct mem_cgroup_per_node *mz;
1100 mz = root->nodeinfo[reclaim->pgdat->node_id];
1104 * On start, join the current reclaim iteration cycle.
1105 * Exit when a concurrent walker completes it.
1108 reclaim->generation = iter->generation;
1109 else if (reclaim->generation != iter->generation)
1113 pos = READ_ONCE(iter->position);
1114 if (!pos || css_tryget(&pos->css))
1117 * css reference reached zero, so iter->position will
1118 * be cleared by ->css_released. However, we should not
1119 * rely on this happening soon, because ->css_released
1120 * is called from a work queue, and by busy-waiting we
1121 * might block it. So we clear iter->position right
1124 (void)cmpxchg(&iter->position, pos, NULL);
1134 css = css_next_descendant_pre(css, &root->css);
1137 * Reclaimers share the hierarchy walk, and a
1138 * new one might jump in right at the end of
1139 * the hierarchy - make sure they see at least
1140 * one group and restart from the beginning.
1148 * Verify the css and acquire a reference. The root
1149 * is provided by the caller, so we know it's alive
1150 * and kicking, and don't take an extra reference.
1152 if (css == &root->css || css_tryget(css)) {
1153 memcg = mem_cgroup_from_css(css);
1160 * The position could have already been updated by a competing
1161 * thread, so check that the value hasn't changed since we read
1162 * it to avoid reclaiming from the same cgroup twice.
1164 (void)cmpxchg(&iter->position, pos, memcg);
1175 if (prev && prev != root)
1176 css_put(&prev->css);
1182 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1183 * @root: hierarchy root
1184 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1186 void mem_cgroup_iter_break(struct mem_cgroup *root,
1187 struct mem_cgroup *prev)
1190 root = root_mem_cgroup;
1191 if (prev && prev != root)
1192 css_put(&prev->css);
1195 static void __invalidate_reclaim_iterators(struct mem_cgroup *from,
1196 struct mem_cgroup *dead_memcg)
1198 struct mem_cgroup_reclaim_iter *iter;
1199 struct mem_cgroup_per_node *mz;
1202 for_each_node(nid) {
1203 mz = from->nodeinfo[nid];
1205 cmpxchg(&iter->position, dead_memcg, NULL);
1209 static void invalidate_reclaim_iterators(struct mem_cgroup *dead_memcg)
1211 struct mem_cgroup *memcg = dead_memcg;
1212 struct mem_cgroup *last;
1215 __invalidate_reclaim_iterators(memcg, dead_memcg);
1217 } while ((memcg = parent_mem_cgroup(memcg)));
1220 * When cgroup1 non-hierarchy mode is used,
1221 * parent_mem_cgroup() does not walk all the way up to the
1222 * cgroup root (root_mem_cgroup). So we have to handle
1223 * dead_memcg from cgroup root separately.
1225 if (!mem_cgroup_is_root(last))
1226 __invalidate_reclaim_iterators(root_mem_cgroup,
1231 * mem_cgroup_scan_tasks - iterate over tasks of a memory cgroup hierarchy
1232 * @memcg: hierarchy root
1233 * @fn: function to call for each task
1234 * @arg: argument passed to @fn
1236 * This function iterates over tasks attached to @memcg or to any of its
1237 * descendants and calls @fn for each task. If @fn returns a non-zero
1238 * value, the function breaks the iteration loop and returns the value.
1239 * Otherwise, it will iterate over all tasks and return 0.
1241 * This function must not be called for the root memory cgroup.
1243 int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
1244 int (*fn)(struct task_struct *, void *), void *arg)
1246 struct mem_cgroup *iter;
1249 BUG_ON(mem_cgroup_is_root(memcg));
1251 for_each_mem_cgroup_tree(iter, memcg) {
1252 struct css_task_iter it;
1253 struct task_struct *task;
1255 css_task_iter_start(&iter->css, CSS_TASK_ITER_PROCS, &it);
1256 while (!ret && (task = css_task_iter_next(&it)))
1257 ret = fn(task, arg);
1258 css_task_iter_end(&it);
1260 mem_cgroup_iter_break(memcg, iter);
1267 #ifdef CONFIG_DEBUG_VM
1268 void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio)
1270 struct mem_cgroup *memcg;
1272 if (mem_cgroup_disabled())
1275 memcg = folio_memcg(folio);
1278 VM_BUG_ON_FOLIO(!mem_cgroup_is_root(lruvec_memcg(lruvec)), folio);
1280 VM_BUG_ON_FOLIO(lruvec_memcg(lruvec) != memcg, folio);
1285 * folio_lruvec_lock - Lock the lruvec for a folio.
1286 * @folio: Pointer to the folio.
1288 * These functions are safe to use under any of the following conditions:
1290 * - folio_test_lru false
1291 * - folio_memcg_lock()
1292 * - folio frozen (refcount of 0)
1294 * Return: The lruvec this folio is on with its lock held.
1296 struct lruvec *folio_lruvec_lock(struct folio *folio)
1298 struct lruvec *lruvec = folio_lruvec(folio);
1300 spin_lock(&lruvec->lru_lock);
1301 lruvec_memcg_debug(lruvec, folio);
1307 * folio_lruvec_lock_irq - Lock the lruvec for a folio.
1308 * @folio: Pointer to the folio.
1310 * These functions are safe to use under any of the following conditions:
1312 * - folio_test_lru false
1313 * - folio_memcg_lock()
1314 * - folio frozen (refcount of 0)
1316 * Return: The lruvec this folio is on with its lock held and interrupts
1319 struct lruvec *folio_lruvec_lock_irq(struct folio *folio)
1321 struct lruvec *lruvec = folio_lruvec(folio);
1323 spin_lock_irq(&lruvec->lru_lock);
1324 lruvec_memcg_debug(lruvec, folio);
1330 * folio_lruvec_lock_irqsave - Lock the lruvec for a folio.
1331 * @folio: Pointer to the folio.
1332 * @flags: Pointer to irqsave flags.
1334 * These functions are safe to use under any of the following conditions:
1336 * - folio_test_lru false
1337 * - folio_memcg_lock()
1338 * - folio frozen (refcount of 0)
1340 * Return: The lruvec this folio is on with its lock held and interrupts
1343 struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio,
1344 unsigned long *flags)
1346 struct lruvec *lruvec = folio_lruvec(folio);
1348 spin_lock_irqsave(&lruvec->lru_lock, *flags);
1349 lruvec_memcg_debug(lruvec, folio);
1355 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1356 * @lruvec: mem_cgroup per zone lru vector
1357 * @lru: index of lru list the page is sitting on
1358 * @zid: zone id of the accounted pages
1359 * @nr_pages: positive when adding or negative when removing
1361 * This function must be called under lru_lock, just before a page is added
1362 * to or just after a page is removed from an lru list.
1364 void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1365 int zid, int nr_pages)
1367 struct mem_cgroup_per_node *mz;
1368 unsigned long *lru_size;
1371 if (mem_cgroup_disabled())
1374 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
1375 lru_size = &mz->lru_zone_size[zid][lru];
1378 *lru_size += nr_pages;
1381 if (WARN_ONCE(size < 0,
1382 "%s(%p, %d, %d): lru_size %ld\n",
1383 __func__, lruvec, lru, nr_pages, size)) {
1389 *lru_size += nr_pages;
1393 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
1394 * @memcg: the memory cgroup
1396 * Returns the maximum amount of memory @mem can be charged with, in
1399 static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
1401 unsigned long margin = 0;
1402 unsigned long count;
1403 unsigned long limit;
1405 count = page_counter_read(&memcg->memory);
1406 limit = READ_ONCE(memcg->memory.max);
1408 margin = limit - count;
1410 if (do_memsw_account()) {
1411 count = page_counter_read(&memcg->memsw);
1412 limit = READ_ONCE(memcg->memsw.max);
1414 margin = min(margin, limit - count);
1423 * A routine for checking "mem" is under move_account() or not.
1425 * Checking a cgroup is mc.from or mc.to or under hierarchy of
1426 * moving cgroups. This is for waiting at high-memory pressure
1429 static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
1431 struct mem_cgroup *from;
1432 struct mem_cgroup *to;
1435 * Unlike task_move routines, we access mc.to, mc.from not under
1436 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1438 spin_lock(&mc.lock);
1444 ret = mem_cgroup_is_descendant(from, memcg) ||
1445 mem_cgroup_is_descendant(to, memcg);
1447 spin_unlock(&mc.lock);
1451 static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
1453 if (mc.moving_task && current != mc.moving_task) {
1454 if (mem_cgroup_under_move(memcg)) {
1456 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1457 /* moving charge context might have finished. */
1460 finish_wait(&mc.waitq, &wait);
1467 struct memory_stat {
1472 static const struct memory_stat memory_stats[] = {
1473 { "anon", NR_ANON_MAPPED },
1474 { "file", NR_FILE_PAGES },
1475 { "kernel", MEMCG_KMEM },
1476 { "kernel_stack", NR_KERNEL_STACK_KB },
1477 { "pagetables", NR_PAGETABLE },
1478 { "sec_pagetables", NR_SECONDARY_PAGETABLE },
1479 { "percpu", MEMCG_PERCPU_B },
1480 { "sock", MEMCG_SOCK },
1481 { "vmalloc", MEMCG_VMALLOC },
1482 { "shmem", NR_SHMEM },
1483 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
1484 { "zswap", MEMCG_ZSWAP_B },
1485 { "zswapped", MEMCG_ZSWAPPED },
1487 { "file_mapped", NR_FILE_MAPPED },
1488 { "file_dirty", NR_FILE_DIRTY },
1489 { "file_writeback", NR_WRITEBACK },
1491 { "swapcached", NR_SWAPCACHE },
1493 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1494 { "anon_thp", NR_ANON_THPS },
1495 { "file_thp", NR_FILE_THPS },
1496 { "shmem_thp", NR_SHMEM_THPS },
1498 { "inactive_anon", NR_INACTIVE_ANON },
1499 { "active_anon", NR_ACTIVE_ANON },
1500 { "inactive_file", NR_INACTIVE_FILE },
1501 { "active_file", NR_ACTIVE_FILE },
1502 { "unevictable", NR_UNEVICTABLE },
1503 { "slab_reclaimable", NR_SLAB_RECLAIMABLE_B },
1504 { "slab_unreclaimable", NR_SLAB_UNRECLAIMABLE_B },
1506 /* The memory events */
1507 { "workingset_refault_anon", WORKINGSET_REFAULT_ANON },
1508 { "workingset_refault_file", WORKINGSET_REFAULT_FILE },
1509 { "workingset_activate_anon", WORKINGSET_ACTIVATE_ANON },
1510 { "workingset_activate_file", WORKINGSET_ACTIVATE_FILE },
1511 { "workingset_restore_anon", WORKINGSET_RESTORE_ANON },
1512 { "workingset_restore_file", WORKINGSET_RESTORE_FILE },
1513 { "workingset_nodereclaim", WORKINGSET_NODERECLAIM },
1516 /* Translate stat items to the correct unit for memory.stat output */
1517 static int memcg_page_state_unit(int item)
1520 case MEMCG_PERCPU_B:
1522 case NR_SLAB_RECLAIMABLE_B:
1523 case NR_SLAB_UNRECLAIMABLE_B:
1524 case WORKINGSET_REFAULT_ANON:
1525 case WORKINGSET_REFAULT_FILE:
1526 case WORKINGSET_ACTIVATE_ANON:
1527 case WORKINGSET_ACTIVATE_FILE:
1528 case WORKINGSET_RESTORE_ANON:
1529 case WORKINGSET_RESTORE_FILE:
1530 case WORKINGSET_NODERECLAIM:
1532 case NR_KERNEL_STACK_KB:
1539 static inline unsigned long memcg_page_state_output(struct mem_cgroup *memcg,
1542 return memcg_page_state(memcg, item) * memcg_page_state_unit(item);
1545 static void memory_stat_format(struct mem_cgroup *memcg, char *buf, int bufsize)
1550 seq_buf_init(&s, buf, bufsize);
1553 * Provide statistics on the state of the memory subsystem as
1554 * well as cumulative event counters that show past behavior.
1556 * This list is ordered following a combination of these gradients:
1557 * 1) generic big picture -> specifics and details
1558 * 2) reflecting userspace activity -> reflecting kernel heuristics
1560 * Current memory state:
1562 mem_cgroup_flush_stats();
1564 for (i = 0; i < ARRAY_SIZE(memory_stats); i++) {
1567 size = memcg_page_state_output(memcg, memory_stats[i].idx);
1568 seq_buf_printf(&s, "%s %llu\n", memory_stats[i].name, size);
1570 if (unlikely(memory_stats[i].idx == NR_SLAB_UNRECLAIMABLE_B)) {
1571 size += memcg_page_state_output(memcg,
1572 NR_SLAB_RECLAIMABLE_B);
1573 seq_buf_printf(&s, "slab %llu\n", size);
1577 /* Accumulated memory events */
1578 seq_buf_printf(&s, "pgscan %lu\n",
1579 memcg_events(memcg, PGSCAN_KSWAPD) +
1580 memcg_events(memcg, PGSCAN_DIRECT) +
1581 memcg_events(memcg, PGSCAN_KHUGEPAGED));
1582 seq_buf_printf(&s, "pgsteal %lu\n",
1583 memcg_events(memcg, PGSTEAL_KSWAPD) +
1584 memcg_events(memcg, PGSTEAL_DIRECT) +
1585 memcg_events(memcg, PGSTEAL_KHUGEPAGED));
1587 for (i = 0; i < ARRAY_SIZE(memcg_vm_event_stat); i++) {
1588 if (memcg_vm_event_stat[i] == PGPGIN ||
1589 memcg_vm_event_stat[i] == PGPGOUT)
1592 seq_buf_printf(&s, "%s %lu\n",
1593 vm_event_name(memcg_vm_event_stat[i]),
1594 memcg_events(memcg, memcg_vm_event_stat[i]));
1597 /* The above should easily fit into one page */
1598 WARN_ON_ONCE(seq_buf_has_overflowed(&s));
1601 #define K(x) ((x) << (PAGE_SHIFT-10))
1603 * mem_cgroup_print_oom_context: Print OOM information relevant to
1604 * memory controller.
1605 * @memcg: The memory cgroup that went over limit
1606 * @p: Task that is going to be killed
1608 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1611 void mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
1616 pr_cont(",oom_memcg=");
1617 pr_cont_cgroup_path(memcg->css.cgroup);
1619 pr_cont(",global_oom");
1621 pr_cont(",task_memcg=");
1622 pr_cont_cgroup_path(task_cgroup(p, memory_cgrp_id));
1628 * mem_cgroup_print_oom_meminfo: Print OOM memory information relevant to
1629 * memory controller.
1630 * @memcg: The memory cgroup that went over limit
1632 void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
1634 /* Use static buffer, for the caller is holding oom_lock. */
1635 static char buf[PAGE_SIZE];
1637 lockdep_assert_held(&oom_lock);
1639 pr_info("memory: usage %llukB, limit %llukB, failcnt %lu\n",
1640 K((u64)page_counter_read(&memcg->memory)),
1641 K((u64)READ_ONCE(memcg->memory.max)), memcg->memory.failcnt);
1642 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
1643 pr_info("swap: usage %llukB, limit %llukB, failcnt %lu\n",
1644 K((u64)page_counter_read(&memcg->swap)),
1645 K((u64)READ_ONCE(memcg->swap.max)), memcg->swap.failcnt);
1647 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %lu\n",
1648 K((u64)page_counter_read(&memcg->memsw)),
1649 K((u64)memcg->memsw.max), memcg->memsw.failcnt);
1650 pr_info("kmem: usage %llukB, limit %llukB, failcnt %lu\n",
1651 K((u64)page_counter_read(&memcg->kmem)),
1652 K((u64)memcg->kmem.max), memcg->kmem.failcnt);
1655 pr_info("Memory cgroup stats for ");
1656 pr_cont_cgroup_path(memcg->css.cgroup);
1658 memory_stat_format(memcg, buf, sizeof(buf));
1663 * Return the memory (and swap, if configured) limit for a memcg.
1665 unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
1667 unsigned long max = READ_ONCE(memcg->memory.max);
1669 if (do_memsw_account()) {
1670 if (mem_cgroup_swappiness(memcg)) {
1671 /* Calculate swap excess capacity from memsw limit */
1672 unsigned long swap = READ_ONCE(memcg->memsw.max) - max;
1674 max += min(swap, (unsigned long)total_swap_pages);
1677 if (mem_cgroup_swappiness(memcg))
1678 max += min(READ_ONCE(memcg->swap.max),
1679 (unsigned long)total_swap_pages);
1684 unsigned long mem_cgroup_size(struct mem_cgroup *memcg)
1686 return page_counter_read(&memcg->memory);
1689 static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1692 struct oom_control oc = {
1696 .gfp_mask = gfp_mask,
1701 if (mutex_lock_killable(&oom_lock))
1704 if (mem_cgroup_margin(memcg) >= (1 << order))
1708 * A few threads which were not waiting at mutex_lock_killable() can
1709 * fail to bail out. Therefore, check again after holding oom_lock.
1711 ret = task_is_dying() || out_of_memory(&oc);
1714 mutex_unlock(&oom_lock);
1718 static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1721 unsigned long *total_scanned)
1723 struct mem_cgroup *victim = NULL;
1726 unsigned long excess;
1727 unsigned long nr_scanned;
1728 struct mem_cgroup_reclaim_cookie reclaim = {
1732 excess = soft_limit_excess(root_memcg);
1735 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
1740 * If we have not been able to reclaim
1741 * anything, it might because there are
1742 * no reclaimable pages under this hierarchy
1747 * We want to do more targeted reclaim.
1748 * excess >> 2 is not to excessive so as to
1749 * reclaim too much, nor too less that we keep
1750 * coming back to reclaim from this cgroup
1752 if (total >= (excess >> 2) ||
1753 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
1758 total += mem_cgroup_shrink_node(victim, gfp_mask, false,
1759 pgdat, &nr_scanned);
1760 *total_scanned += nr_scanned;
1761 if (!soft_limit_excess(root_memcg))
1764 mem_cgroup_iter_break(root_memcg, victim);
1768 #ifdef CONFIG_LOCKDEP
1769 static struct lockdep_map memcg_oom_lock_dep_map = {
1770 .name = "memcg_oom_lock",
1774 static DEFINE_SPINLOCK(memcg_oom_lock);
1777 * Check OOM-Killer is already running under our hierarchy.
1778 * If someone is running, return false.
1780 static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
1782 struct mem_cgroup *iter, *failed = NULL;
1784 spin_lock(&memcg_oom_lock);
1786 for_each_mem_cgroup_tree(iter, memcg) {
1787 if (iter->oom_lock) {
1789 * this subtree of our hierarchy is already locked
1790 * so we cannot give a lock.
1793 mem_cgroup_iter_break(memcg, iter);
1796 iter->oom_lock = true;
1801 * OK, we failed to lock the whole subtree so we have
1802 * to clean up what we set up to the failing subtree
1804 for_each_mem_cgroup_tree(iter, memcg) {
1805 if (iter == failed) {
1806 mem_cgroup_iter_break(memcg, iter);
1809 iter->oom_lock = false;
1812 mutex_acquire(&memcg_oom_lock_dep_map, 0, 1, _RET_IP_);
1814 spin_unlock(&memcg_oom_lock);
1819 static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
1821 struct mem_cgroup *iter;
1823 spin_lock(&memcg_oom_lock);
1824 mutex_release(&memcg_oom_lock_dep_map, _RET_IP_);
1825 for_each_mem_cgroup_tree(iter, memcg)
1826 iter->oom_lock = false;
1827 spin_unlock(&memcg_oom_lock);
1830 static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
1832 struct mem_cgroup *iter;
1834 spin_lock(&memcg_oom_lock);
1835 for_each_mem_cgroup_tree(iter, memcg)
1837 spin_unlock(&memcg_oom_lock);
1840 static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
1842 struct mem_cgroup *iter;
1845 * Be careful about under_oom underflows because a child memcg
1846 * could have been added after mem_cgroup_mark_under_oom.
1848 spin_lock(&memcg_oom_lock);
1849 for_each_mem_cgroup_tree(iter, memcg)
1850 if (iter->under_oom > 0)
1852 spin_unlock(&memcg_oom_lock);
1855 static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1857 struct oom_wait_info {
1858 struct mem_cgroup *memcg;
1859 wait_queue_entry_t wait;
1862 static int memcg_oom_wake_function(wait_queue_entry_t *wait,
1863 unsigned mode, int sync, void *arg)
1865 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
1866 struct mem_cgroup *oom_wait_memcg;
1867 struct oom_wait_info *oom_wait_info;
1869 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
1870 oom_wait_memcg = oom_wait_info->memcg;
1872 if (!mem_cgroup_is_descendant(wake_memcg, oom_wait_memcg) &&
1873 !mem_cgroup_is_descendant(oom_wait_memcg, wake_memcg))
1875 return autoremove_wake_function(wait, mode, sync, arg);
1878 static void memcg_oom_recover(struct mem_cgroup *memcg)
1881 * For the following lockless ->under_oom test, the only required
1882 * guarantee is that it must see the state asserted by an OOM when
1883 * this function is called as a result of userland actions
1884 * triggered by the notification of the OOM. This is trivially
1885 * achieved by invoking mem_cgroup_mark_under_oom() before
1886 * triggering notification.
1888 if (memcg && memcg->under_oom)
1889 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
1893 * Returns true if successfully killed one or more processes. Though in some
1894 * corner cases it can return true even without killing any process.
1896 static bool mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
1900 if (order > PAGE_ALLOC_COSTLY_ORDER)
1903 memcg_memory_event(memcg, MEMCG_OOM);
1906 * We are in the middle of the charge context here, so we
1907 * don't want to block when potentially sitting on a callstack
1908 * that holds all kinds of filesystem and mm locks.
1910 * cgroup1 allows disabling the OOM killer and waiting for outside
1911 * handling until the charge can succeed; remember the context and put
1912 * the task to sleep at the end of the page fault when all locks are
1915 * On the other hand, in-kernel OOM killer allows for an async victim
1916 * memory reclaim (oom_reaper) and that means that we are not solely
1917 * relying on the oom victim to make a forward progress and we can
1918 * invoke the oom killer here.
1920 * Please note that mem_cgroup_out_of_memory might fail to find a
1921 * victim and then we have to bail out from the charge path.
1923 if (memcg->oom_kill_disable) {
1924 if (current->in_user_fault) {
1925 css_get(&memcg->css);
1926 current->memcg_in_oom = memcg;
1927 current->memcg_oom_gfp_mask = mask;
1928 current->memcg_oom_order = order;
1933 mem_cgroup_mark_under_oom(memcg);
1935 locked = mem_cgroup_oom_trylock(memcg);
1938 mem_cgroup_oom_notify(memcg);
1940 mem_cgroup_unmark_under_oom(memcg);
1941 ret = mem_cgroup_out_of_memory(memcg, mask, order);
1944 mem_cgroup_oom_unlock(memcg);
1950 * mem_cgroup_oom_synchronize - complete memcg OOM handling
1951 * @handle: actually kill/wait or just clean up the OOM state
1953 * This has to be called at the end of a page fault if the memcg OOM
1954 * handler was enabled.
1956 * Memcg supports userspace OOM handling where failed allocations must
1957 * sleep on a waitqueue until the userspace task resolves the
1958 * situation. Sleeping directly in the charge context with all kinds
1959 * of locks held is not a good idea, instead we remember an OOM state
1960 * in the task and mem_cgroup_oom_synchronize() has to be called at
1961 * the end of the page fault to complete the OOM handling.
1963 * Returns %true if an ongoing memcg OOM situation was detected and
1964 * completed, %false otherwise.
1966 bool mem_cgroup_oom_synchronize(bool handle)
1968 struct mem_cgroup *memcg = current->memcg_in_oom;
1969 struct oom_wait_info owait;
1972 /* OOM is global, do not handle */
1979 owait.memcg = memcg;
1980 owait.wait.flags = 0;
1981 owait.wait.func = memcg_oom_wake_function;
1982 owait.wait.private = current;
1983 INIT_LIST_HEAD(&owait.wait.entry);
1985 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
1986 mem_cgroup_mark_under_oom(memcg);
1988 locked = mem_cgroup_oom_trylock(memcg);
1991 mem_cgroup_oom_notify(memcg);
1993 if (locked && !memcg->oom_kill_disable) {
1994 mem_cgroup_unmark_under_oom(memcg);
1995 finish_wait(&memcg_oom_waitq, &owait.wait);
1996 mem_cgroup_out_of_memory(memcg, current->memcg_oom_gfp_mask,
1997 current->memcg_oom_order);
2000 mem_cgroup_unmark_under_oom(memcg);
2001 finish_wait(&memcg_oom_waitq, &owait.wait);
2005 mem_cgroup_oom_unlock(memcg);
2007 * There is no guarantee that an OOM-lock contender
2008 * sees the wakeups triggered by the OOM kill
2009 * uncharges. Wake any sleepers explicitly.
2011 memcg_oom_recover(memcg);
2014 current->memcg_in_oom = NULL;
2015 css_put(&memcg->css);
2020 * mem_cgroup_get_oom_group - get a memory cgroup to clean up after OOM
2021 * @victim: task to be killed by the OOM killer
2022 * @oom_domain: memcg in case of memcg OOM, NULL in case of system-wide OOM
2024 * Returns a pointer to a memory cgroup, which has to be cleaned up
2025 * by killing all belonging OOM-killable tasks.
2027 * Caller has to call mem_cgroup_put() on the returned non-NULL memcg.
2029 struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
2030 struct mem_cgroup *oom_domain)
2032 struct mem_cgroup *oom_group = NULL;
2033 struct mem_cgroup *memcg;
2035 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
2039 oom_domain = root_mem_cgroup;
2043 memcg = mem_cgroup_from_task(victim);
2044 if (mem_cgroup_is_root(memcg))
2048 * If the victim task has been asynchronously moved to a different
2049 * memory cgroup, we might end up killing tasks outside oom_domain.
2050 * In this case it's better to ignore memory.group.oom.
2052 if (unlikely(!mem_cgroup_is_descendant(memcg, oom_domain)))
2056 * Traverse the memory cgroup hierarchy from the victim task's
2057 * cgroup up to the OOMing cgroup (or root) to find the
2058 * highest-level memory cgroup with oom.group set.
2060 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
2061 if (memcg->oom_group)
2064 if (memcg == oom_domain)
2069 css_get(&oom_group->css);
2076 void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
2078 pr_info("Tasks in ");
2079 pr_cont_cgroup_path(memcg->css.cgroup);
2080 pr_cont(" are going to be killed due to memory.oom.group set\n");
2084 * folio_memcg_lock - Bind a folio to its memcg.
2085 * @folio: The folio.
2087 * This function prevents unlocked LRU folios from being moved to
2090 * It ensures lifetime of the bound memcg. The caller is responsible
2091 * for the lifetime of the folio.
2093 void folio_memcg_lock(struct folio *folio)
2095 struct mem_cgroup *memcg;
2096 unsigned long flags;
2099 * The RCU lock is held throughout the transaction. The fast
2100 * path can get away without acquiring the memcg->move_lock
2101 * because page moving starts with an RCU grace period.
2105 if (mem_cgroup_disabled())
2108 memcg = folio_memcg(folio);
2109 if (unlikely(!memcg))
2112 #ifdef CONFIG_PROVE_LOCKING
2113 local_irq_save(flags);
2114 might_lock(&memcg->move_lock);
2115 local_irq_restore(flags);
2118 if (atomic_read(&memcg->moving_account) <= 0)
2121 spin_lock_irqsave(&memcg->move_lock, flags);
2122 if (memcg != folio_memcg(folio)) {
2123 spin_unlock_irqrestore(&memcg->move_lock, flags);
2128 * When charge migration first begins, we can have multiple
2129 * critical sections holding the fast-path RCU lock and one
2130 * holding the slowpath move_lock. Track the task who has the
2131 * move_lock for unlock_page_memcg().
2133 memcg->move_lock_task = current;
2134 memcg->move_lock_flags = flags;
2137 void lock_page_memcg(struct page *page)
2139 folio_memcg_lock(page_folio(page));
2142 static void __folio_memcg_unlock(struct mem_cgroup *memcg)
2144 if (memcg && memcg->move_lock_task == current) {
2145 unsigned long flags = memcg->move_lock_flags;
2147 memcg->move_lock_task = NULL;
2148 memcg->move_lock_flags = 0;
2150 spin_unlock_irqrestore(&memcg->move_lock, flags);
2157 * folio_memcg_unlock - Release the binding between a folio and its memcg.
2158 * @folio: The folio.
2160 * This releases the binding created by folio_memcg_lock(). This does
2161 * not change the accounting of this folio to its memcg, but it does
2162 * permit others to change it.
2164 void folio_memcg_unlock(struct folio *folio)
2166 __folio_memcg_unlock(folio_memcg(folio));
2169 void unlock_page_memcg(struct page *page)
2171 folio_memcg_unlock(page_folio(page));
2174 struct memcg_stock_pcp {
2175 local_lock_t stock_lock;
2176 struct mem_cgroup *cached; /* this never be root cgroup */
2177 unsigned int nr_pages;
2179 #ifdef CONFIG_MEMCG_KMEM
2180 struct obj_cgroup *cached_objcg;
2181 struct pglist_data *cached_pgdat;
2182 unsigned int nr_bytes;
2183 int nr_slab_reclaimable_b;
2184 int nr_slab_unreclaimable_b;
2187 struct work_struct work;
2188 unsigned long flags;
2189 #define FLUSHING_CACHED_CHARGE 0
2191 static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock) = {
2192 .stock_lock = INIT_LOCAL_LOCK(stock_lock),
2194 static DEFINE_MUTEX(percpu_charge_mutex);
2196 #ifdef CONFIG_MEMCG_KMEM
2197 static struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock);
2198 static bool obj_stock_flush_required(struct memcg_stock_pcp *stock,
2199 struct mem_cgroup *root_memcg);
2200 static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages);
2203 static inline struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock)
2207 static bool obj_stock_flush_required(struct memcg_stock_pcp *stock,
2208 struct mem_cgroup *root_memcg)
2212 static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages)
2218 * consume_stock: Try to consume stocked charge on this cpu.
2219 * @memcg: memcg to consume from.
2220 * @nr_pages: how many pages to charge.
2222 * The charges will only happen if @memcg matches the current cpu's memcg
2223 * stock, and at least @nr_pages are available in that stock. Failure to
2224 * service an allocation will refill the stock.
2226 * returns true if successful, false otherwise.
2228 static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
2230 struct memcg_stock_pcp *stock;
2231 unsigned long flags;
2234 if (nr_pages > MEMCG_CHARGE_BATCH)
2237 local_lock_irqsave(&memcg_stock.stock_lock, flags);
2239 stock = this_cpu_ptr(&memcg_stock);
2240 if (memcg == stock->cached && stock->nr_pages >= nr_pages) {
2241 stock->nr_pages -= nr_pages;
2245 local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
2251 * Returns stocks cached in percpu and reset cached information.
2253 static void drain_stock(struct memcg_stock_pcp *stock)
2255 struct mem_cgroup *old = stock->cached;
2260 if (stock->nr_pages) {
2261 page_counter_uncharge(&old->memory, stock->nr_pages);
2262 if (do_memsw_account())
2263 page_counter_uncharge(&old->memsw, stock->nr_pages);
2264 stock->nr_pages = 0;
2268 stock->cached = NULL;
2271 static void drain_local_stock(struct work_struct *dummy)
2273 struct memcg_stock_pcp *stock;
2274 struct obj_cgroup *old = NULL;
2275 unsigned long flags;
2278 * The only protection from cpu hotplug (memcg_hotplug_cpu_dead) vs.
2279 * drain_stock races is that we always operate on local CPU stock
2280 * here with IRQ disabled
2282 local_lock_irqsave(&memcg_stock.stock_lock, flags);
2284 stock = this_cpu_ptr(&memcg_stock);
2285 old = drain_obj_stock(stock);
2287 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
2289 local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
2291 obj_cgroup_put(old);
2295 * Cache charges(val) to local per_cpu area.
2296 * This will be consumed by consume_stock() function, later.
2298 static void __refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
2300 struct memcg_stock_pcp *stock;
2302 stock = this_cpu_ptr(&memcg_stock);
2303 if (stock->cached != memcg) { /* reset if necessary */
2305 css_get(&memcg->css);
2306 stock->cached = memcg;
2308 stock->nr_pages += nr_pages;
2310 if (stock->nr_pages > MEMCG_CHARGE_BATCH)
2314 static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
2316 unsigned long flags;
2318 local_lock_irqsave(&memcg_stock.stock_lock, flags);
2319 __refill_stock(memcg, nr_pages);
2320 local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
2324 * Drains all per-CPU charge caches for given root_memcg resp. subtree
2325 * of the hierarchy under it.
2327 static void drain_all_stock(struct mem_cgroup *root_memcg)
2331 /* If someone's already draining, avoid adding running more workers. */
2332 if (!mutex_trylock(&percpu_charge_mutex))
2335 * Notify other cpus that system-wide "drain" is running
2336 * We do not care about races with the cpu hotplug because cpu down
2337 * as well as workers from this path always operate on the local
2338 * per-cpu data. CPU up doesn't touch memcg_stock at all.
2341 curcpu = smp_processor_id();
2342 for_each_online_cpu(cpu) {
2343 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
2344 struct mem_cgroup *memcg;
2348 memcg = stock->cached;
2349 if (memcg && stock->nr_pages &&
2350 mem_cgroup_is_descendant(memcg, root_memcg))
2352 else if (obj_stock_flush_required(stock, root_memcg))
2357 !test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2359 drain_local_stock(&stock->work);
2361 schedule_work_on(cpu, &stock->work);
2365 mutex_unlock(&percpu_charge_mutex);
2368 static int memcg_hotplug_cpu_dead(unsigned int cpu)
2370 struct memcg_stock_pcp *stock;
2372 stock = &per_cpu(memcg_stock, cpu);
2378 static unsigned long reclaim_high(struct mem_cgroup *memcg,
2379 unsigned int nr_pages,
2382 unsigned long nr_reclaimed = 0;
2385 unsigned long pflags;
2387 if (page_counter_read(&memcg->memory) <=
2388 READ_ONCE(memcg->memory.high))
2391 memcg_memory_event(memcg, MEMCG_HIGH);
2393 psi_memstall_enter(&pflags);
2394 nr_reclaimed += try_to_free_mem_cgroup_pages(memcg, nr_pages,
2396 MEMCG_RECLAIM_MAY_SWAP,
2398 psi_memstall_leave(&pflags);
2399 } while ((memcg = parent_mem_cgroup(memcg)) &&
2400 !mem_cgroup_is_root(memcg));
2402 return nr_reclaimed;
2405 static void high_work_func(struct work_struct *work)
2407 struct mem_cgroup *memcg;
2409 memcg = container_of(work, struct mem_cgroup, high_work);
2410 reclaim_high(memcg, MEMCG_CHARGE_BATCH, GFP_KERNEL);
2414 * Clamp the maximum sleep time per allocation batch to 2 seconds. This is
2415 * enough to still cause a significant slowdown in most cases, while still
2416 * allowing diagnostics and tracing to proceed without becoming stuck.
2418 #define MEMCG_MAX_HIGH_DELAY_JIFFIES (2UL*HZ)
2421 * When calculating the delay, we use these either side of the exponentiation to
2422 * maintain precision and scale to a reasonable number of jiffies (see the table
2425 * - MEMCG_DELAY_PRECISION_SHIFT: Extra precision bits while translating the
2426 * overage ratio to a delay.
2427 * - MEMCG_DELAY_SCALING_SHIFT: The number of bits to scale down the
2428 * proposed penalty in order to reduce to a reasonable number of jiffies, and
2429 * to produce a reasonable delay curve.
2431 * MEMCG_DELAY_SCALING_SHIFT just happens to be a number that produces a
2432 * reasonable delay curve compared to precision-adjusted overage, not
2433 * penalising heavily at first, but still making sure that growth beyond the
2434 * limit penalises misbehaviour cgroups by slowing them down exponentially. For
2435 * example, with a high of 100 megabytes:
2437 * +-------+------------------------+
2438 * | usage | time to allocate in ms |
2439 * +-------+------------------------+
2461 * +-------+------------------------+
2463 #define MEMCG_DELAY_PRECISION_SHIFT 20
2464 #define MEMCG_DELAY_SCALING_SHIFT 14
2466 static u64 calculate_overage(unsigned long usage, unsigned long high)
2474 * Prevent division by 0 in overage calculation by acting as if
2475 * it was a threshold of 1 page
2477 high = max(high, 1UL);
2479 overage = usage - high;
2480 overage <<= MEMCG_DELAY_PRECISION_SHIFT;
2481 return div64_u64(overage, high);
2484 static u64 mem_find_max_overage(struct mem_cgroup *memcg)
2486 u64 overage, max_overage = 0;
2489 overage = calculate_overage(page_counter_read(&memcg->memory),
2490 READ_ONCE(memcg->memory.high));
2491 max_overage = max(overage, max_overage);
2492 } while ((memcg = parent_mem_cgroup(memcg)) &&
2493 !mem_cgroup_is_root(memcg));
2498 static u64 swap_find_max_overage(struct mem_cgroup *memcg)
2500 u64 overage, max_overage = 0;
2503 overage = calculate_overage(page_counter_read(&memcg->swap),
2504 READ_ONCE(memcg->swap.high));
2506 memcg_memory_event(memcg, MEMCG_SWAP_HIGH);
2507 max_overage = max(overage, max_overage);
2508 } while ((memcg = parent_mem_cgroup(memcg)) &&
2509 !mem_cgroup_is_root(memcg));
2515 * Get the number of jiffies that we should penalise a mischievous cgroup which
2516 * is exceeding its memory.high by checking both it and its ancestors.
2518 static unsigned long calculate_high_delay(struct mem_cgroup *memcg,
2519 unsigned int nr_pages,
2522 unsigned long penalty_jiffies;
2528 * We use overage compared to memory.high to calculate the number of
2529 * jiffies to sleep (penalty_jiffies). Ideally this value should be
2530 * fairly lenient on small overages, and increasingly harsh when the
2531 * memcg in question makes it clear that it has no intention of stopping
2532 * its crazy behaviour, so we exponentially increase the delay based on
2535 penalty_jiffies = max_overage * max_overage * HZ;
2536 penalty_jiffies >>= MEMCG_DELAY_PRECISION_SHIFT;
2537 penalty_jiffies >>= MEMCG_DELAY_SCALING_SHIFT;
2540 * Factor in the task's own contribution to the overage, such that four
2541 * N-sized allocations are throttled approximately the same as one
2542 * 4N-sized allocation.
2544 * MEMCG_CHARGE_BATCH pages is nominal, so work out how much smaller or
2545 * larger the current charge patch is than that.
2547 return penalty_jiffies * nr_pages / MEMCG_CHARGE_BATCH;
2551 * Scheduled by try_charge() to be executed from the userland return path
2552 * and reclaims memory over the high limit.
2554 void mem_cgroup_handle_over_high(void)
2556 unsigned long penalty_jiffies;
2557 unsigned long pflags;
2558 unsigned long nr_reclaimed;
2559 unsigned int nr_pages = current->memcg_nr_pages_over_high;
2560 int nr_retries = MAX_RECLAIM_RETRIES;
2561 struct mem_cgroup *memcg;
2562 bool in_retry = false;
2564 if (likely(!nr_pages))
2567 memcg = get_mem_cgroup_from_mm(current->mm);
2568 current->memcg_nr_pages_over_high = 0;
2572 * The allocating task should reclaim at least the batch size, but for
2573 * subsequent retries we only want to do what's necessary to prevent oom
2574 * or breaching resource isolation.
2576 * This is distinct from memory.max or page allocator behaviour because
2577 * memory.high is currently batched, whereas memory.max and the page
2578 * allocator run every time an allocation is made.
2580 nr_reclaimed = reclaim_high(memcg,
2581 in_retry ? SWAP_CLUSTER_MAX : nr_pages,
2585 * memory.high is breached and reclaim is unable to keep up. Throttle
2586 * allocators proactively to slow down excessive growth.
2588 penalty_jiffies = calculate_high_delay(memcg, nr_pages,
2589 mem_find_max_overage(memcg));
2591 penalty_jiffies += calculate_high_delay(memcg, nr_pages,
2592 swap_find_max_overage(memcg));
2595 * Clamp the max delay per usermode return so as to still keep the
2596 * application moving forwards and also permit diagnostics, albeit
2599 penalty_jiffies = min(penalty_jiffies, MEMCG_MAX_HIGH_DELAY_JIFFIES);
2602 * Don't sleep if the amount of jiffies this memcg owes us is so low
2603 * that it's not even worth doing, in an attempt to be nice to those who
2604 * go only a small amount over their memory.high value and maybe haven't
2605 * been aggressively reclaimed enough yet.
2607 if (penalty_jiffies <= HZ / 100)
2611 * If reclaim is making forward progress but we're still over
2612 * memory.high, we want to encourage that rather than doing allocator
2615 if (nr_reclaimed || nr_retries--) {
2621 * If we exit early, we're guaranteed to die (since
2622 * schedule_timeout_killable sets TASK_KILLABLE). This means we don't
2623 * need to account for any ill-begotten jiffies to pay them off later.
2625 psi_memstall_enter(&pflags);
2626 schedule_timeout_killable(penalty_jiffies);
2627 psi_memstall_leave(&pflags);
2630 css_put(&memcg->css);
2633 static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
2634 unsigned int nr_pages)
2636 unsigned int batch = max(MEMCG_CHARGE_BATCH, nr_pages);
2637 int nr_retries = MAX_RECLAIM_RETRIES;
2638 struct mem_cgroup *mem_over_limit;
2639 struct page_counter *counter;
2640 unsigned long nr_reclaimed;
2641 bool passed_oom = false;
2642 unsigned int reclaim_options = MEMCG_RECLAIM_MAY_SWAP;
2643 bool drained = false;
2644 bool raised_max_event = false;
2645 unsigned long pflags;
2648 if (consume_stock(memcg, nr_pages))
2651 if (!do_memsw_account() ||
2652 page_counter_try_charge(&memcg->memsw, batch, &counter)) {
2653 if (page_counter_try_charge(&memcg->memory, batch, &counter))
2655 if (do_memsw_account())
2656 page_counter_uncharge(&memcg->memsw, batch);
2657 mem_over_limit = mem_cgroup_from_counter(counter, memory);
2659 mem_over_limit = mem_cgroup_from_counter(counter, memsw);
2660 reclaim_options &= ~MEMCG_RECLAIM_MAY_SWAP;
2663 if (batch > nr_pages) {
2669 * Prevent unbounded recursion when reclaim operations need to
2670 * allocate memory. This might exceed the limits temporarily,
2671 * but we prefer facilitating memory reclaim and getting back
2672 * under the limit over triggering OOM kills in these cases.
2674 if (unlikely(current->flags & PF_MEMALLOC))
2677 if (unlikely(task_in_memcg_oom(current)))
2680 if (!gfpflags_allow_blocking(gfp_mask))
2683 memcg_memory_event(mem_over_limit, MEMCG_MAX);
2684 raised_max_event = true;
2686 psi_memstall_enter(&pflags);
2687 nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages,
2688 gfp_mask, reclaim_options,
2690 psi_memstall_leave(&pflags);
2692 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
2696 drain_all_stock(mem_over_limit);
2701 if (gfp_mask & __GFP_NORETRY)
2704 * Even though the limit is exceeded at this point, reclaim
2705 * may have been able to free some pages. Retry the charge
2706 * before killing the task.
2708 * Only for regular pages, though: huge pages are rather
2709 * unlikely to succeed so close to the limit, and we fall back
2710 * to regular pages anyway in case of failure.
2712 if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER))
2715 * At task move, charge accounts can be doubly counted. So, it's
2716 * better to wait until the end of task_move if something is going on.
2718 if (mem_cgroup_wait_acct_move(mem_over_limit))
2724 if (gfp_mask & __GFP_RETRY_MAYFAIL)
2727 /* Avoid endless loop for tasks bypassed by the oom killer */
2728 if (passed_oom && task_is_dying())
2732 * keep retrying as long as the memcg oom killer is able to make
2733 * a forward progress or bypass the charge if the oom killer
2734 * couldn't make any progress.
2736 if (mem_cgroup_oom(mem_over_limit, gfp_mask,
2737 get_order(nr_pages * PAGE_SIZE))) {
2739 nr_retries = MAX_RECLAIM_RETRIES;
2744 * Memcg doesn't have a dedicated reserve for atomic
2745 * allocations. But like the global atomic pool, we need to
2746 * put the burden of reclaim on regular allocation requests
2747 * and let these go through as privileged allocations.
2749 if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH)))
2753 * If the allocation has to be enforced, don't forget to raise
2754 * a MEMCG_MAX event.
2756 if (!raised_max_event)
2757 memcg_memory_event(mem_over_limit, MEMCG_MAX);
2760 * The allocation either can't fail or will lead to more memory
2761 * being freed very soon. Allow memory usage go over the limit
2762 * temporarily by force charging it.
2764 page_counter_charge(&memcg->memory, nr_pages);
2765 if (do_memsw_account())
2766 page_counter_charge(&memcg->memsw, nr_pages);
2771 if (batch > nr_pages)
2772 refill_stock(memcg, batch - nr_pages);
2775 * If the hierarchy is above the normal consumption range, schedule
2776 * reclaim on returning to userland. We can perform reclaim here
2777 * if __GFP_RECLAIM but let's always punt for simplicity and so that
2778 * GFP_KERNEL can consistently be used during reclaim. @memcg is
2779 * not recorded as it most likely matches current's and won't
2780 * change in the meantime. As high limit is checked again before
2781 * reclaim, the cost of mismatch is negligible.
2784 bool mem_high, swap_high;
2786 mem_high = page_counter_read(&memcg->memory) >
2787 READ_ONCE(memcg->memory.high);
2788 swap_high = page_counter_read(&memcg->swap) >
2789 READ_ONCE(memcg->swap.high);
2791 /* Don't bother a random interrupted task */
2794 schedule_work(&memcg->high_work);
2800 if (mem_high || swap_high) {
2802 * The allocating tasks in this cgroup will need to do
2803 * reclaim or be throttled to prevent further growth
2804 * of the memory or swap footprints.
2806 * Target some best-effort fairness between the tasks,
2807 * and distribute reclaim work and delay penalties
2808 * based on how much each task is actually allocating.
2810 current->memcg_nr_pages_over_high += batch;
2811 set_notify_resume(current);
2814 } while ((memcg = parent_mem_cgroup(memcg)));
2816 if (current->memcg_nr_pages_over_high > MEMCG_CHARGE_BATCH &&
2817 !(current->flags & PF_MEMALLOC) &&
2818 gfpflags_allow_blocking(gfp_mask)) {
2819 mem_cgroup_handle_over_high();
2824 static inline int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
2825 unsigned int nr_pages)
2827 if (mem_cgroup_is_root(memcg))
2830 return try_charge_memcg(memcg, gfp_mask, nr_pages);
2833 static inline void cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages)
2835 if (mem_cgroup_is_root(memcg))
2838 page_counter_uncharge(&memcg->memory, nr_pages);
2839 if (do_memsw_account())
2840 page_counter_uncharge(&memcg->memsw, nr_pages);
2843 static void commit_charge(struct folio *folio, struct mem_cgroup *memcg)
2845 VM_BUG_ON_FOLIO(folio_memcg(folio), folio);
2847 * Any of the following ensures page's memcg stability:
2851 * - lock_page_memcg()
2852 * - exclusive reference
2853 * - mem_cgroup_trylock_pages()
2855 folio->memcg_data = (unsigned long)memcg;
2858 #ifdef CONFIG_MEMCG_KMEM
2860 * The allocated objcg pointers array is not accounted directly.
2861 * Moreover, it should not come from DMA buffer and is not readily
2862 * reclaimable. So those GFP bits should be masked off.
2864 #define OBJCGS_CLEAR_MASK (__GFP_DMA | __GFP_RECLAIMABLE | __GFP_ACCOUNT)
2867 * mod_objcg_mlstate() may be called with irq enabled, so
2868 * mod_memcg_lruvec_state() should be used.
2870 static inline void mod_objcg_mlstate(struct obj_cgroup *objcg,
2871 struct pglist_data *pgdat,
2872 enum node_stat_item idx, int nr)
2874 struct mem_cgroup *memcg;
2875 struct lruvec *lruvec;
2878 memcg = obj_cgroup_memcg(objcg);
2879 lruvec = mem_cgroup_lruvec(memcg, pgdat);
2880 mod_memcg_lruvec_state(lruvec, idx, nr);
2884 int memcg_alloc_slab_cgroups(struct slab *slab, struct kmem_cache *s,
2885 gfp_t gfp, bool new_slab)
2887 unsigned int objects = objs_per_slab(s, slab);
2888 unsigned long memcg_data;
2891 gfp &= ~OBJCGS_CLEAR_MASK;
2892 vec = kcalloc_node(objects, sizeof(struct obj_cgroup *), gfp,
2897 memcg_data = (unsigned long) vec | MEMCG_DATA_OBJCGS;
2900 * If the slab is brand new and nobody can yet access its
2901 * memcg_data, no synchronization is required and memcg_data can
2902 * be simply assigned.
2904 slab->memcg_data = memcg_data;
2905 } else if (cmpxchg(&slab->memcg_data, 0, memcg_data)) {
2907 * If the slab is already in use, somebody can allocate and
2908 * assign obj_cgroups in parallel. In this case the existing
2909 * objcg vector should be reused.
2915 kmemleak_not_leak(vec);
2919 static __always_inline
2920 struct mem_cgroup *mem_cgroup_from_obj_folio(struct folio *folio, void *p)
2923 * Slab objects are accounted individually, not per-page.
2924 * Memcg membership data for each individual object is saved in
2927 if (folio_test_slab(folio)) {
2928 struct obj_cgroup **objcgs;
2932 slab = folio_slab(folio);
2933 objcgs = slab_objcgs(slab);
2937 off = obj_to_index(slab->slab_cache, slab, p);
2939 return obj_cgroup_memcg(objcgs[off]);
2945 * page_memcg_check() is used here, because in theory we can encounter
2946 * a folio where the slab flag has been cleared already, but
2947 * slab->memcg_data has not been freed yet
2948 * page_memcg_check(page) will guarantee that a proper memory
2949 * cgroup pointer or NULL will be returned.
2951 return page_memcg_check(folio_page(folio, 0));
2955 * Returns a pointer to the memory cgroup to which the kernel object is charged.
2957 * A passed kernel object can be a slab object, vmalloc object or a generic
2958 * kernel page, so different mechanisms for getting the memory cgroup pointer
2961 * In certain cases (e.g. kernel stacks or large kmallocs with SLUB) the caller
2962 * can not know for sure how the kernel object is implemented.
2963 * mem_cgroup_from_obj() can be safely used in such cases.
2965 * The caller must ensure the memcg lifetime, e.g. by taking rcu_read_lock(),
2966 * cgroup_mutex, etc.
2968 struct mem_cgroup *mem_cgroup_from_obj(void *p)
2970 struct folio *folio;
2972 if (mem_cgroup_disabled())
2975 if (unlikely(is_vmalloc_addr(p)))
2976 folio = page_folio(vmalloc_to_page(p));
2978 folio = virt_to_folio(p);
2980 return mem_cgroup_from_obj_folio(folio, p);
2984 * Returns a pointer to the memory cgroup to which the kernel object is charged.
2985 * Similar to mem_cgroup_from_obj(), but faster and not suitable for objects,
2986 * allocated using vmalloc().
2988 * A passed kernel object must be a slab object or a generic kernel page.
2990 * The caller must ensure the memcg lifetime, e.g. by taking rcu_read_lock(),
2991 * cgroup_mutex, etc.
2993 struct mem_cgroup *mem_cgroup_from_slab_obj(void *p)
2995 if (mem_cgroup_disabled())
2998 return mem_cgroup_from_obj_folio(virt_to_folio(p), p);
3001 static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg)
3003 struct obj_cgroup *objcg = NULL;
3005 for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) {
3006 objcg = rcu_dereference(memcg->objcg);
3007 if (objcg && obj_cgroup_tryget(objcg))
3014 __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void)
3016 struct obj_cgroup *objcg = NULL;
3017 struct mem_cgroup *memcg;
3019 if (memcg_kmem_bypass())
3023 if (unlikely(active_memcg()))
3024 memcg = active_memcg();
3026 memcg = mem_cgroup_from_task(current);
3027 objcg = __get_obj_cgroup_from_memcg(memcg);
3032 struct obj_cgroup *get_obj_cgroup_from_page(struct page *page)
3034 struct obj_cgroup *objcg;
3036 if (!memcg_kmem_enabled())
3039 if (PageMemcgKmem(page)) {
3040 objcg = __folio_objcg(page_folio(page));
3041 obj_cgroup_get(objcg);
3043 struct mem_cgroup *memcg;
3046 memcg = __folio_memcg(page_folio(page));
3048 objcg = __get_obj_cgroup_from_memcg(memcg);
3056 static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages)
3058 mod_memcg_state(memcg, MEMCG_KMEM, nr_pages);
3059 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
3061 page_counter_charge(&memcg->kmem, nr_pages);
3063 page_counter_uncharge(&memcg->kmem, -nr_pages);
3069 * obj_cgroup_uncharge_pages: uncharge a number of kernel pages from a objcg
3070 * @objcg: object cgroup to uncharge
3071 * @nr_pages: number of pages to uncharge
3073 static void obj_cgroup_uncharge_pages(struct obj_cgroup *objcg,
3074 unsigned int nr_pages)
3076 struct mem_cgroup *memcg;
3078 memcg = get_mem_cgroup_from_objcg(objcg);
3080 memcg_account_kmem(memcg, -nr_pages);
3081 refill_stock(memcg, nr_pages);
3083 css_put(&memcg->css);
3087 * obj_cgroup_charge_pages: charge a number of kernel pages to a objcg
3088 * @objcg: object cgroup to charge
3089 * @gfp: reclaim mode
3090 * @nr_pages: number of pages to charge
3092 * Returns 0 on success, an error code on failure.
3094 static int obj_cgroup_charge_pages(struct obj_cgroup *objcg, gfp_t gfp,
3095 unsigned int nr_pages)
3097 struct mem_cgroup *memcg;
3100 memcg = get_mem_cgroup_from_objcg(objcg);
3102 ret = try_charge_memcg(memcg, gfp, nr_pages);
3106 memcg_account_kmem(memcg, nr_pages);
3108 css_put(&memcg->css);
3114 * __memcg_kmem_charge_page: charge a kmem page to the current memory cgroup
3115 * @page: page to charge
3116 * @gfp: reclaim mode
3117 * @order: allocation order
3119 * Returns 0 on success, an error code on failure.
3121 int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order)
3123 struct obj_cgroup *objcg;
3126 objcg = get_obj_cgroup_from_current();
3128 ret = obj_cgroup_charge_pages(objcg, gfp, 1 << order);
3130 page->memcg_data = (unsigned long)objcg |
3134 obj_cgroup_put(objcg);
3140 * __memcg_kmem_uncharge_page: uncharge a kmem page
3141 * @page: page to uncharge
3142 * @order: allocation order
3144 void __memcg_kmem_uncharge_page(struct page *page, int order)
3146 struct folio *folio = page_folio(page);
3147 struct obj_cgroup *objcg;
3148 unsigned int nr_pages = 1 << order;
3150 if (!folio_memcg_kmem(folio))
3153 objcg = __folio_objcg(folio);
3154 obj_cgroup_uncharge_pages(objcg, nr_pages);
3155 folio->memcg_data = 0;
3156 obj_cgroup_put(objcg);
3159 void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat,
3160 enum node_stat_item idx, int nr)
3162 struct memcg_stock_pcp *stock;
3163 struct obj_cgroup *old = NULL;
3164 unsigned long flags;
3167 local_lock_irqsave(&memcg_stock.stock_lock, flags);
3168 stock = this_cpu_ptr(&memcg_stock);
3171 * Save vmstat data in stock and skip vmstat array update unless
3172 * accumulating over a page of vmstat data or when pgdat or idx
3175 if (stock->cached_objcg != objcg) {
3176 old = drain_obj_stock(stock);
3177 obj_cgroup_get(objcg);
3178 stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes)
3179 ? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0;
3180 stock->cached_objcg = objcg;
3181 stock->cached_pgdat = pgdat;
3182 } else if (stock->cached_pgdat != pgdat) {
3183 /* Flush the existing cached vmstat data */
3184 struct pglist_data *oldpg = stock->cached_pgdat;
3186 if (stock->nr_slab_reclaimable_b) {
3187 mod_objcg_mlstate(objcg, oldpg, NR_SLAB_RECLAIMABLE_B,
3188 stock->nr_slab_reclaimable_b);
3189 stock->nr_slab_reclaimable_b = 0;
3191 if (stock->nr_slab_unreclaimable_b) {
3192 mod_objcg_mlstate(objcg, oldpg, NR_SLAB_UNRECLAIMABLE_B,
3193 stock->nr_slab_unreclaimable_b);
3194 stock->nr_slab_unreclaimable_b = 0;
3196 stock->cached_pgdat = pgdat;
3199 bytes = (idx == NR_SLAB_RECLAIMABLE_B) ? &stock->nr_slab_reclaimable_b
3200 : &stock->nr_slab_unreclaimable_b;
3202 * Even for large object >= PAGE_SIZE, the vmstat data will still be
3203 * cached locally at least once before pushing it out.
3210 if (abs(*bytes) > PAGE_SIZE) {
3218 mod_objcg_mlstate(objcg, pgdat, idx, nr);
3220 local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
3222 obj_cgroup_put(old);
3225 static bool consume_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes)
3227 struct memcg_stock_pcp *stock;
3228 unsigned long flags;
3231 local_lock_irqsave(&memcg_stock.stock_lock, flags);
3233 stock = this_cpu_ptr(&memcg_stock);
3234 if (objcg == stock->cached_objcg && stock->nr_bytes >= nr_bytes) {
3235 stock->nr_bytes -= nr_bytes;
3239 local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
3244 static struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock)
3246 struct obj_cgroup *old = stock->cached_objcg;
3251 if (stock->nr_bytes) {
3252 unsigned int nr_pages = stock->nr_bytes >> PAGE_SHIFT;
3253 unsigned int nr_bytes = stock->nr_bytes & (PAGE_SIZE - 1);
3256 struct mem_cgroup *memcg;
3258 memcg = get_mem_cgroup_from_objcg(old);
3260 memcg_account_kmem(memcg, -nr_pages);
3261 __refill_stock(memcg, nr_pages);
3263 css_put(&memcg->css);
3267 * The leftover is flushed to the centralized per-memcg value.
3268 * On the next attempt to refill obj stock it will be moved
3269 * to a per-cpu stock (probably, on an other CPU), see
3270 * refill_obj_stock().
3272 * How often it's flushed is a trade-off between the memory
3273 * limit enforcement accuracy and potential CPU contention,
3274 * so it might be changed in the future.
3276 atomic_add(nr_bytes, &old->nr_charged_bytes);
3277 stock->nr_bytes = 0;
3281 * Flush the vmstat data in current stock
3283 if (stock->nr_slab_reclaimable_b || stock->nr_slab_unreclaimable_b) {
3284 if (stock->nr_slab_reclaimable_b) {
3285 mod_objcg_mlstate(old, stock->cached_pgdat,
3286 NR_SLAB_RECLAIMABLE_B,
3287 stock->nr_slab_reclaimable_b);
3288 stock->nr_slab_reclaimable_b = 0;
3290 if (stock->nr_slab_unreclaimable_b) {
3291 mod_objcg_mlstate(old, stock->cached_pgdat,
3292 NR_SLAB_UNRECLAIMABLE_B,
3293 stock->nr_slab_unreclaimable_b);
3294 stock->nr_slab_unreclaimable_b = 0;
3296 stock->cached_pgdat = NULL;
3299 stock->cached_objcg = NULL;
3301 * The `old' objects needs to be released by the caller via
3302 * obj_cgroup_put() outside of memcg_stock_pcp::stock_lock.
3307 static bool obj_stock_flush_required(struct memcg_stock_pcp *stock,
3308 struct mem_cgroup *root_memcg)
3310 struct mem_cgroup *memcg;
3312 if (stock->cached_objcg) {
3313 memcg = obj_cgroup_memcg(stock->cached_objcg);
3314 if (memcg && mem_cgroup_is_descendant(memcg, root_memcg))
3321 static void refill_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes,
3322 bool allow_uncharge)
3324 struct memcg_stock_pcp *stock;
3325 struct obj_cgroup *old = NULL;
3326 unsigned long flags;
3327 unsigned int nr_pages = 0;
3329 local_lock_irqsave(&memcg_stock.stock_lock, flags);
3331 stock = this_cpu_ptr(&memcg_stock);
3332 if (stock->cached_objcg != objcg) { /* reset if necessary */
3333 old = drain_obj_stock(stock);
3334 obj_cgroup_get(objcg);
3335 stock->cached_objcg = objcg;
3336 stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes)
3337 ? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0;
3338 allow_uncharge = true; /* Allow uncharge when objcg changes */
3340 stock->nr_bytes += nr_bytes;
3342 if (allow_uncharge && (stock->nr_bytes > PAGE_SIZE)) {
3343 nr_pages = stock->nr_bytes >> PAGE_SHIFT;
3344 stock->nr_bytes &= (PAGE_SIZE - 1);
3347 local_unlock_irqrestore(&memcg_stock.stock_lock, flags);
3349 obj_cgroup_put(old);
3352 obj_cgroup_uncharge_pages(objcg, nr_pages);
3355 int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size)
3357 unsigned int nr_pages, nr_bytes;
3360 if (consume_obj_stock(objcg, size))
3364 * In theory, objcg->nr_charged_bytes can have enough
3365 * pre-charged bytes to satisfy the allocation. However,
3366 * flushing objcg->nr_charged_bytes requires two atomic
3367 * operations, and objcg->nr_charged_bytes can't be big.
3368 * The shared objcg->nr_charged_bytes can also become a
3369 * performance bottleneck if all tasks of the same memcg are
3370 * trying to update it. So it's better to ignore it and try
3371 * grab some new pages. The stock's nr_bytes will be flushed to
3372 * objcg->nr_charged_bytes later on when objcg changes.
3374 * The stock's nr_bytes may contain enough pre-charged bytes
3375 * to allow one less page from being charged, but we can't rely
3376 * on the pre-charged bytes not being changed outside of
3377 * consume_obj_stock() or refill_obj_stock(). So ignore those
3378 * pre-charged bytes as well when charging pages. To avoid a
3379 * page uncharge right after a page charge, we set the
3380 * allow_uncharge flag to false when calling refill_obj_stock()
3381 * to temporarily allow the pre-charged bytes to exceed the page
3382 * size limit. The maximum reachable value of the pre-charged
3383 * bytes is (sizeof(object) + PAGE_SIZE - 2) if there is no data
3386 nr_pages = size >> PAGE_SHIFT;
3387 nr_bytes = size & (PAGE_SIZE - 1);
3392 ret = obj_cgroup_charge_pages(objcg, gfp, nr_pages);
3393 if (!ret && nr_bytes)
3394 refill_obj_stock(objcg, PAGE_SIZE - nr_bytes, false);
3399 void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size)
3401 refill_obj_stock(objcg, size, true);
3404 #endif /* CONFIG_MEMCG_KMEM */
3407 * Because page_memcg(head) is not set on tails, set it now.
3409 void split_page_memcg(struct page *head, unsigned int nr)
3411 struct folio *folio = page_folio(head);
3412 struct mem_cgroup *memcg = folio_memcg(folio);
3415 if (mem_cgroup_disabled() || !memcg)
3418 for (i = 1; i < nr; i++)
3419 folio_page(folio, i)->memcg_data = folio->memcg_data;
3421 if (folio_memcg_kmem(folio))
3422 obj_cgroup_get_many(__folio_objcg(folio), nr - 1);
3424 css_get_many(&memcg->css, nr - 1);
3429 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3430 * @entry: swap entry to be moved
3431 * @from: mem_cgroup which the entry is moved from
3432 * @to: mem_cgroup which the entry is moved to
3434 * It succeeds only when the swap_cgroup's record for this entry is the same
3435 * as the mem_cgroup's id of @from.
3437 * Returns 0 on success, -EINVAL on failure.
3439 * The caller must have charged to @to, IOW, called page_counter_charge() about
3440 * both res and memsw, and called css_get().
3442 static int mem_cgroup_move_swap_account(swp_entry_t entry,
3443 struct mem_cgroup *from, struct mem_cgroup *to)
3445 unsigned short old_id, new_id;
3447 old_id = mem_cgroup_id(from);
3448 new_id = mem_cgroup_id(to);
3450 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
3451 mod_memcg_state(from, MEMCG_SWAP, -1);
3452 mod_memcg_state(to, MEMCG_SWAP, 1);
3458 static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
3459 struct mem_cgroup *from, struct mem_cgroup *to)
3465 static DEFINE_MUTEX(memcg_max_mutex);
3467 static int mem_cgroup_resize_max(struct mem_cgroup *memcg,
3468 unsigned long max, bool memsw)
3470 bool enlarge = false;
3471 bool drained = false;
3473 bool limits_invariant;
3474 struct page_counter *counter = memsw ? &memcg->memsw : &memcg->memory;
3477 if (signal_pending(current)) {
3482 mutex_lock(&memcg_max_mutex);
3484 * Make sure that the new limit (memsw or memory limit) doesn't
3485 * break our basic invariant rule memory.max <= memsw.max.
3487 limits_invariant = memsw ? max >= READ_ONCE(memcg->memory.max) :
3488 max <= memcg->memsw.max;
3489 if (!limits_invariant) {
3490 mutex_unlock(&memcg_max_mutex);
3494 if (max > counter->max)
3496 ret = page_counter_set_max(counter, max);
3497 mutex_unlock(&memcg_max_mutex);
3503 drain_all_stock(memcg);
3508 if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL,
3509 memsw ? 0 : MEMCG_RECLAIM_MAY_SWAP,
3516 if (!ret && enlarge)
3517 memcg_oom_recover(memcg);
3522 unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
3524 unsigned long *total_scanned)
3526 unsigned long nr_reclaimed = 0;
3527 struct mem_cgroup_per_node *mz, *next_mz = NULL;
3528 unsigned long reclaimed;
3530 struct mem_cgroup_tree_per_node *mctz;
3531 unsigned long excess;
3536 mctz = soft_limit_tree.rb_tree_per_node[pgdat->node_id];
3539 * Do not even bother to check the largest node if the root
3540 * is empty. Do it lockless to prevent lock bouncing. Races
3541 * are acceptable as soft limit is best effort anyway.
3543 if (!mctz || RB_EMPTY_ROOT(&mctz->rb_root))
3547 * This loop can run a while, specially if mem_cgroup's continuously
3548 * keep exceeding their soft limit and putting the system under
3555 mz = mem_cgroup_largest_soft_limit_node(mctz);
3559 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, pgdat,
3560 gfp_mask, total_scanned);
3561 nr_reclaimed += reclaimed;
3562 spin_lock_irq(&mctz->lock);
3565 * If we failed to reclaim anything from this memory cgroup
3566 * it is time to move on to the next cgroup
3570 next_mz = __mem_cgroup_largest_soft_limit_node(mctz);
3572 excess = soft_limit_excess(mz->memcg);
3574 * One school of thought says that we should not add
3575 * back the node to the tree if reclaim returns 0.
3576 * But our reclaim could return 0, simply because due
3577 * to priority we are exposing a smaller subset of
3578 * memory to reclaim from. Consider this as a longer
3581 /* If excess == 0, no tree ops */
3582 __mem_cgroup_insert_exceeded(mz, mctz, excess);
3583 spin_unlock_irq(&mctz->lock);
3584 css_put(&mz->memcg->css);
3587 * Could not reclaim anything and there are no more
3588 * mem cgroups to try or we seem to be looping without
3589 * reclaiming anything.
3591 if (!nr_reclaimed &&
3593 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3595 } while (!nr_reclaimed);
3597 css_put(&next_mz->memcg->css);
3598 return nr_reclaimed;
3602 * Reclaims as many pages from the given memcg as possible.
3604 * Caller is responsible for holding css reference for memcg.
3606 static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
3608 int nr_retries = MAX_RECLAIM_RETRIES;
3610 /* we call try-to-free pages for make this cgroup empty */
3611 lru_add_drain_all();
3613 drain_all_stock(memcg);
3615 /* try to free all pages in this cgroup */
3616 while (nr_retries && page_counter_read(&memcg->memory)) {
3617 if (signal_pending(current))
3620 if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL,
3621 MEMCG_RECLAIM_MAY_SWAP,
3629 static ssize_t mem_cgroup_force_empty_write(struct kernfs_open_file *of,
3630 char *buf, size_t nbytes,
3633 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
3635 if (mem_cgroup_is_root(memcg))
3637 return mem_cgroup_force_empty(memcg) ?: nbytes;
3640 static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
3646 static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
3647 struct cftype *cft, u64 val)
3652 pr_warn_once("Non-hierarchical mode is deprecated. "
3653 "Please report your usecase to linux-mm@kvack.org if you "
3654 "depend on this functionality.\n");
3659 static unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
3663 if (mem_cgroup_is_root(memcg)) {
3664 mem_cgroup_flush_stats();
3665 val = memcg_page_state(memcg, NR_FILE_PAGES) +
3666 memcg_page_state(memcg, NR_ANON_MAPPED);
3668 val += memcg_page_state(memcg, MEMCG_SWAP);
3671 val = page_counter_read(&memcg->memory);
3673 val = page_counter_read(&memcg->memsw);
3686 static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css,
3689 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
3690 struct page_counter *counter;
3692 switch (MEMFILE_TYPE(cft->private)) {
3694 counter = &memcg->memory;
3697 counter = &memcg->memsw;
3700 counter = &memcg->kmem;
3703 counter = &memcg->tcpmem;
3709 switch (MEMFILE_ATTR(cft->private)) {
3711 if (counter == &memcg->memory)
3712 return (u64)mem_cgroup_usage(memcg, false) * PAGE_SIZE;
3713 if (counter == &memcg->memsw)
3714 return (u64)mem_cgroup_usage(memcg, true) * PAGE_SIZE;
3715 return (u64)page_counter_read(counter) * PAGE_SIZE;
3717 return (u64)counter->max * PAGE_SIZE;
3719 return (u64)counter->watermark * PAGE_SIZE;
3721 return counter->failcnt;
3722 case RES_SOFT_LIMIT:
3723 return (u64)memcg->soft_limit * PAGE_SIZE;
3729 #ifdef CONFIG_MEMCG_KMEM
3730 static int memcg_online_kmem(struct mem_cgroup *memcg)
3732 struct obj_cgroup *objcg;
3734 if (mem_cgroup_kmem_disabled())
3737 if (unlikely(mem_cgroup_is_root(memcg)))
3740 objcg = obj_cgroup_alloc();
3744 objcg->memcg = memcg;
3745 rcu_assign_pointer(memcg->objcg, objcg);
3747 static_branch_enable(&memcg_kmem_enabled_key);
3749 memcg->kmemcg_id = memcg->id.id;
3754 static void memcg_offline_kmem(struct mem_cgroup *memcg)
3756 struct mem_cgroup *parent;
3758 if (mem_cgroup_kmem_disabled())
3761 if (unlikely(mem_cgroup_is_root(memcg)))
3764 parent = parent_mem_cgroup(memcg);
3766 parent = root_mem_cgroup;
3768 memcg_reparent_objcgs(memcg, parent);
3771 * After we have finished memcg_reparent_objcgs(), all list_lrus
3772 * corresponding to this cgroup are guaranteed to remain empty.
3773 * The ordering is imposed by list_lru_node->lock taken by
3774 * memcg_reparent_list_lrus().
3776 memcg_reparent_list_lrus(memcg, parent);
3779 static int memcg_online_kmem(struct mem_cgroup *memcg)
3783 static void memcg_offline_kmem(struct mem_cgroup *memcg)
3786 #endif /* CONFIG_MEMCG_KMEM */
3788 static int memcg_update_tcp_max(struct mem_cgroup *memcg, unsigned long max)
3792 mutex_lock(&memcg_max_mutex);
3794 ret = page_counter_set_max(&memcg->tcpmem, max);
3798 if (!memcg->tcpmem_active) {
3800 * The active flag needs to be written after the static_key
3801 * update. This is what guarantees that the socket activation
3802 * function is the last one to run. See mem_cgroup_sk_alloc()
3803 * for details, and note that we don't mark any socket as
3804 * belonging to this memcg until that flag is up.
3806 * We need to do this, because static_keys will span multiple
3807 * sites, but we can't control their order. If we mark a socket
3808 * as accounted, but the accounting functions are not patched in
3809 * yet, we'll lose accounting.
3811 * We never race with the readers in mem_cgroup_sk_alloc(),
3812 * because when this value change, the code to process it is not
3815 static_branch_inc(&memcg_sockets_enabled_key);
3816 memcg->tcpmem_active = true;
3819 mutex_unlock(&memcg_max_mutex);
3824 * The user of this function is...
3827 static ssize_t mem_cgroup_write(struct kernfs_open_file *of,
3828 char *buf, size_t nbytes, loff_t off)
3830 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
3831 unsigned long nr_pages;
3834 buf = strstrip(buf);
3835 ret = page_counter_memparse(buf, "-1", &nr_pages);
3839 switch (MEMFILE_ATTR(of_cft(of)->private)) {
3841 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3845 switch (MEMFILE_TYPE(of_cft(of)->private)) {
3847 ret = mem_cgroup_resize_max(memcg, nr_pages, false);
3850 ret = mem_cgroup_resize_max(memcg, nr_pages, true);
3853 /* kmem.limit_in_bytes is deprecated. */
3857 ret = memcg_update_tcp_max(memcg, nr_pages);
3861 case RES_SOFT_LIMIT:
3862 if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
3865 memcg->soft_limit = nr_pages;
3870 return ret ?: nbytes;
3873 static ssize_t mem_cgroup_reset(struct kernfs_open_file *of, char *buf,
3874 size_t nbytes, loff_t off)
3876 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
3877 struct page_counter *counter;
3879 switch (MEMFILE_TYPE(of_cft(of)->private)) {
3881 counter = &memcg->memory;
3884 counter = &memcg->memsw;
3887 counter = &memcg->kmem;
3890 counter = &memcg->tcpmem;
3896 switch (MEMFILE_ATTR(of_cft(of)->private)) {
3898 page_counter_reset_watermark(counter);
3901 counter->failcnt = 0;
3910 static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
3913 return mem_cgroup_from_css(css)->move_charge_at_immigrate;
3917 static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
3918 struct cftype *cft, u64 val)
3920 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
3922 if (val & ~MOVE_MASK)
3926 * No kind of locking is needed in here, because ->can_attach() will
3927 * check this value once in the beginning of the process, and then carry
3928 * on with stale data. This means that changes to this value will only
3929 * affect task migrations starting after the change.
3931 memcg->move_charge_at_immigrate = val;
3935 static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
3936 struct cftype *cft, u64 val)
3944 #define LRU_ALL_FILE (BIT(LRU_INACTIVE_FILE) | BIT(LRU_ACTIVE_FILE))
3945 #define LRU_ALL_ANON (BIT(LRU_INACTIVE_ANON) | BIT(LRU_ACTIVE_ANON))
3946 #define LRU_ALL ((1 << NR_LRU_LISTS) - 1)
3948 static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
3949 int nid, unsigned int lru_mask, bool tree)
3951 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid));
3952 unsigned long nr = 0;
3955 VM_BUG_ON((unsigned)nid >= nr_node_ids);
3958 if (!(BIT(lru) & lru_mask))
3961 nr += lruvec_page_state(lruvec, NR_LRU_BASE + lru);
3963 nr += lruvec_page_state_local(lruvec, NR_LRU_BASE + lru);
3968 static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
3969 unsigned int lru_mask,
3972 unsigned long nr = 0;
3976 if (!(BIT(lru) & lru_mask))
3979 nr += memcg_page_state(memcg, NR_LRU_BASE + lru);
3981 nr += memcg_page_state_local(memcg, NR_LRU_BASE + lru);
3986 static int memcg_numa_stat_show(struct seq_file *m, void *v)
3990 unsigned int lru_mask;
3993 static const struct numa_stat stats[] = {
3994 { "total", LRU_ALL },
3995 { "file", LRU_ALL_FILE },
3996 { "anon", LRU_ALL_ANON },
3997 { "unevictable", BIT(LRU_UNEVICTABLE) },
3999 const struct numa_stat *stat;
4001 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
4003 mem_cgroup_flush_stats();
4005 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
4006 seq_printf(m, "%s=%lu", stat->name,
4007 mem_cgroup_nr_lru_pages(memcg, stat->lru_mask,
4009 for_each_node_state(nid, N_MEMORY)
4010 seq_printf(m, " N%d=%lu", nid,
4011 mem_cgroup_node_nr_lru_pages(memcg, nid,
4012 stat->lru_mask, false));
4016 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
4018 seq_printf(m, "hierarchical_%s=%lu", stat->name,
4019 mem_cgroup_nr_lru_pages(memcg, stat->lru_mask,
4021 for_each_node_state(nid, N_MEMORY)
4022 seq_printf(m, " N%d=%lu", nid,
4023 mem_cgroup_node_nr_lru_pages(memcg, nid,
4024 stat->lru_mask, true));
4030 #endif /* CONFIG_NUMA */
4032 static const unsigned int memcg1_stats[] = {
4035 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
4042 WORKINGSET_REFAULT_ANON,
4043 WORKINGSET_REFAULT_FILE,
4047 static const char *const memcg1_stat_names[] = {
4050 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
4057 "workingset_refault_anon",
4058 "workingset_refault_file",
4062 /* Universal VM events cgroup1 shows, original sort order */
4063 static const unsigned int memcg1_events[] = {
4070 static int memcg_stat_show(struct seq_file *m, void *v)
4072 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
4073 unsigned long memory, memsw;
4074 struct mem_cgroup *mi;
4077 BUILD_BUG_ON(ARRAY_SIZE(memcg1_stat_names) != ARRAY_SIZE(memcg1_stats));
4079 mem_cgroup_flush_stats();
4081 for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) {
4084 if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account())
4086 nr = memcg_page_state_local(memcg, memcg1_stats[i]);
4087 seq_printf(m, "%s %lu\n", memcg1_stat_names[i],
4088 nr * memcg_page_state_unit(memcg1_stats[i]));
4091 for (i = 0; i < ARRAY_SIZE(memcg1_events); i++)
4092 seq_printf(m, "%s %lu\n", vm_event_name(memcg1_events[i]),
4093 memcg_events_local(memcg, memcg1_events[i]));
4095 for (i = 0; i < NR_LRU_LISTS; i++)
4096 seq_printf(m, "%s %lu\n", lru_list_name(i),
4097 memcg_page_state_local(memcg, NR_LRU_BASE + i) *
4100 /* Hierarchical information */
4101 memory = memsw = PAGE_COUNTER_MAX;
4102 for (mi = memcg; mi; mi = parent_mem_cgroup(mi)) {
4103 memory = min(memory, READ_ONCE(mi->memory.max));
4104 memsw = min(memsw, READ_ONCE(mi->memsw.max));
4106 seq_printf(m, "hierarchical_memory_limit %llu\n",
4107 (u64)memory * PAGE_SIZE);
4108 if (do_memsw_account())
4109 seq_printf(m, "hierarchical_memsw_limit %llu\n",
4110 (u64)memsw * PAGE_SIZE);
4112 for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) {
4115 if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account())
4117 nr = memcg_page_state(memcg, memcg1_stats[i]);
4118 seq_printf(m, "total_%s %llu\n", memcg1_stat_names[i],
4119 (u64)nr * memcg_page_state_unit(memcg1_stats[i]));
4122 for (i = 0; i < ARRAY_SIZE(memcg1_events); i++)
4123 seq_printf(m, "total_%s %llu\n",
4124 vm_event_name(memcg1_events[i]),
4125 (u64)memcg_events(memcg, memcg1_events[i]));
4127 for (i = 0; i < NR_LRU_LISTS; i++)
4128 seq_printf(m, "total_%s %llu\n", lru_list_name(i),
4129 (u64)memcg_page_state(memcg, NR_LRU_BASE + i) *
4132 #ifdef CONFIG_DEBUG_VM
4135 struct mem_cgroup_per_node *mz;
4136 unsigned long anon_cost = 0;
4137 unsigned long file_cost = 0;
4139 for_each_online_pgdat(pgdat) {
4140 mz = memcg->nodeinfo[pgdat->node_id];
4142 anon_cost += mz->lruvec.anon_cost;
4143 file_cost += mz->lruvec.file_cost;
4145 seq_printf(m, "anon_cost %lu\n", anon_cost);
4146 seq_printf(m, "file_cost %lu\n", file_cost);
4153 static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
4156 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
4158 return mem_cgroup_swappiness(memcg);
4161 static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
4162 struct cftype *cft, u64 val)
4164 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
4169 if (!mem_cgroup_is_root(memcg))
4170 memcg->swappiness = val;
4172 vm_swappiness = val;
4177 static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4179 struct mem_cgroup_threshold_ary *t;
4180 unsigned long usage;
4185 t = rcu_dereference(memcg->thresholds.primary);
4187 t = rcu_dereference(memcg->memsw_thresholds.primary);
4192 usage = mem_cgroup_usage(memcg, swap);
4195 * current_threshold points to threshold just below or equal to usage.
4196 * If it's not true, a threshold was crossed after last
4197 * call of __mem_cgroup_threshold().
4199 i = t->current_threshold;
4202 * Iterate backward over array of thresholds starting from
4203 * current_threshold and check if a threshold is crossed.
4204 * If none of thresholds below usage is crossed, we read
4205 * only one element of the array here.
4207 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4208 eventfd_signal(t->entries[i].eventfd, 1);
4210 /* i = current_threshold + 1 */
4214 * Iterate forward over array of thresholds starting from
4215 * current_threshold+1 and check if a threshold is crossed.
4216 * If none of thresholds above usage is crossed, we read
4217 * only one element of the array here.
4219 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4220 eventfd_signal(t->entries[i].eventfd, 1);
4222 /* Update current_threshold */
4223 t->current_threshold = i - 1;
4228 static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4231 __mem_cgroup_threshold(memcg, false);
4232 if (do_memsw_account())
4233 __mem_cgroup_threshold(memcg, true);
4235 memcg = parent_mem_cgroup(memcg);
4239 static int compare_thresholds(const void *a, const void *b)
4241 const struct mem_cgroup_threshold *_a = a;
4242 const struct mem_cgroup_threshold *_b = b;
4244 if (_a->threshold > _b->threshold)
4247 if (_a->threshold < _b->threshold)
4253 static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
4255 struct mem_cgroup_eventfd_list *ev;
4257 spin_lock(&memcg_oom_lock);
4259 list_for_each_entry(ev, &memcg->oom_notify, list)
4260 eventfd_signal(ev->eventfd, 1);
4262 spin_unlock(&memcg_oom_lock);
4266 static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
4268 struct mem_cgroup *iter;
4270 for_each_mem_cgroup_tree(iter, memcg)
4271 mem_cgroup_oom_notify_cb(iter);
4274 static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
4275 struct eventfd_ctx *eventfd, const char *args, enum res_type type)
4277 struct mem_cgroup_thresholds *thresholds;
4278 struct mem_cgroup_threshold_ary *new;
4279 unsigned long threshold;
4280 unsigned long usage;
4283 ret = page_counter_memparse(args, "-1", &threshold);
4287 mutex_lock(&memcg->thresholds_lock);
4290 thresholds = &memcg->thresholds;
4291 usage = mem_cgroup_usage(memcg, false);
4292 } else if (type == _MEMSWAP) {
4293 thresholds = &memcg->memsw_thresholds;
4294 usage = mem_cgroup_usage(memcg, true);
4298 /* Check if a threshold crossed before adding a new one */
4299 if (thresholds->primary)
4300 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4302 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
4304 /* Allocate memory for new array of thresholds */
4305 new = kmalloc(struct_size(new, entries, size), GFP_KERNEL);
4312 /* Copy thresholds (if any) to new array */
4313 if (thresholds->primary)
4314 memcpy(new->entries, thresholds->primary->entries,
4315 flex_array_size(new, entries, size - 1));
4317 /* Add new threshold */
4318 new->entries[size - 1].eventfd = eventfd;
4319 new->entries[size - 1].threshold = threshold;
4321 /* Sort thresholds. Registering of new threshold isn't time-critical */
4322 sort(new->entries, size, sizeof(*new->entries),
4323 compare_thresholds, NULL);
4325 /* Find current threshold */
4326 new->current_threshold = -1;
4327 for (i = 0; i < size; i++) {
4328 if (new->entries[i].threshold <= usage) {
4330 * new->current_threshold will not be used until
4331 * rcu_assign_pointer(), so it's safe to increment
4334 ++new->current_threshold;
4339 /* Free old spare buffer and save old primary buffer as spare */
4340 kfree(thresholds->spare);
4341 thresholds->spare = thresholds->primary;
4343 rcu_assign_pointer(thresholds->primary, new);
4345 /* To be sure that nobody uses thresholds */
4349 mutex_unlock(&memcg->thresholds_lock);
4354 static int mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
4355 struct eventfd_ctx *eventfd, const char *args)
4357 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEM);
4360 static int memsw_cgroup_usage_register_event(struct mem_cgroup *memcg,
4361 struct eventfd_ctx *eventfd, const char *args)
4363 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEMSWAP);
4366 static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
4367 struct eventfd_ctx *eventfd, enum res_type type)
4369 struct mem_cgroup_thresholds *thresholds;
4370 struct mem_cgroup_threshold_ary *new;
4371 unsigned long usage;
4372 int i, j, size, entries;
4374 mutex_lock(&memcg->thresholds_lock);
4377 thresholds = &memcg->thresholds;
4378 usage = mem_cgroup_usage(memcg, false);
4379 } else if (type == _MEMSWAP) {
4380 thresholds = &memcg->memsw_thresholds;
4381 usage = mem_cgroup_usage(memcg, true);
4385 if (!thresholds->primary)
4388 /* Check if a threshold crossed before removing */
4389 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4391 /* Calculate new number of threshold */
4393 for (i = 0; i < thresholds->primary->size; i++) {
4394 if (thresholds->primary->entries[i].eventfd != eventfd)
4400 new = thresholds->spare;
4402 /* If no items related to eventfd have been cleared, nothing to do */
4406 /* Set thresholds array to NULL if we don't have thresholds */
4415 /* Copy thresholds and find current threshold */
4416 new->current_threshold = -1;
4417 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4418 if (thresholds->primary->entries[i].eventfd == eventfd)
4421 new->entries[j] = thresholds->primary->entries[i];
4422 if (new->entries[j].threshold <= usage) {
4424 * new->current_threshold will not be used
4425 * until rcu_assign_pointer(), so it's safe to increment
4428 ++new->current_threshold;
4434 /* Swap primary and spare array */
4435 thresholds->spare = thresholds->primary;
4437 rcu_assign_pointer(thresholds->primary, new);
4439 /* To be sure that nobody uses thresholds */
4442 /* If all events are unregistered, free the spare array */
4444 kfree(thresholds->spare);
4445 thresholds->spare = NULL;
4448 mutex_unlock(&memcg->thresholds_lock);
4451 static void mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
4452 struct eventfd_ctx *eventfd)
4454 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEM);
4457 static void memsw_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
4458 struct eventfd_ctx *eventfd)
4460 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEMSWAP);
4463 static int mem_cgroup_oom_register_event(struct mem_cgroup *memcg,
4464 struct eventfd_ctx *eventfd, const char *args)
4466 struct mem_cgroup_eventfd_list *event;
4468 event = kmalloc(sizeof(*event), GFP_KERNEL);
4472 spin_lock(&memcg_oom_lock);
4474 event->eventfd = eventfd;
4475 list_add(&event->list, &memcg->oom_notify);
4477 /* already in OOM ? */
4478 if (memcg->under_oom)
4479 eventfd_signal(eventfd, 1);
4480 spin_unlock(&memcg_oom_lock);
4485 static void mem_cgroup_oom_unregister_event(struct mem_cgroup *memcg,
4486 struct eventfd_ctx *eventfd)
4488 struct mem_cgroup_eventfd_list *ev, *tmp;
4490 spin_lock(&memcg_oom_lock);
4492 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
4493 if (ev->eventfd == eventfd) {
4494 list_del(&ev->list);
4499 spin_unlock(&memcg_oom_lock);
4502 static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v)
4504 struct mem_cgroup *memcg = mem_cgroup_from_seq(sf);
4506 seq_printf(sf, "oom_kill_disable %d\n", memcg->oom_kill_disable);
4507 seq_printf(sf, "under_oom %d\n", (bool)memcg->under_oom);
4508 seq_printf(sf, "oom_kill %lu\n",
4509 atomic_long_read(&memcg->memory_events[MEMCG_OOM_KILL]));
4513 static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
4514 struct cftype *cft, u64 val)
4516 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
4518 /* cannot set to root cgroup and only 0 and 1 are allowed */
4519 if (mem_cgroup_is_root(memcg) || !((val == 0) || (val == 1)))
4522 memcg->oom_kill_disable = val;
4524 memcg_oom_recover(memcg);
4529 #ifdef CONFIG_CGROUP_WRITEBACK
4531 #include <trace/events/writeback.h>
4533 static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp)
4535 return wb_domain_init(&memcg->cgwb_domain, gfp);
4538 static void memcg_wb_domain_exit(struct mem_cgroup *memcg)
4540 wb_domain_exit(&memcg->cgwb_domain);
4543 static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)
4545 wb_domain_size_changed(&memcg->cgwb_domain);
4548 struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
4550 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
4552 if (!memcg->css.parent)
4555 return &memcg->cgwb_domain;
4559 * mem_cgroup_wb_stats - retrieve writeback related stats from its memcg
4560 * @wb: bdi_writeback in question
4561 * @pfilepages: out parameter for number of file pages
4562 * @pheadroom: out parameter for number of allocatable pages according to memcg
4563 * @pdirty: out parameter for number of dirty pages
4564 * @pwriteback: out parameter for number of pages under writeback
4566 * Determine the numbers of file, headroom, dirty, and writeback pages in
4567 * @wb's memcg. File, dirty and writeback are self-explanatory. Headroom
4568 * is a bit more involved.
4570 * A memcg's headroom is "min(max, high) - used". In the hierarchy, the
4571 * headroom is calculated as the lowest headroom of itself and the
4572 * ancestors. Note that this doesn't consider the actual amount of
4573 * available memory in the system. The caller should further cap
4574 * *@pheadroom accordingly.
4576 void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
4577 unsigned long *pheadroom, unsigned long *pdirty,
4578 unsigned long *pwriteback)
4580 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
4581 struct mem_cgroup *parent;
4583 mem_cgroup_flush_stats();
4585 *pdirty = memcg_page_state(memcg, NR_FILE_DIRTY);
4586 *pwriteback = memcg_page_state(memcg, NR_WRITEBACK);
4587 *pfilepages = memcg_page_state(memcg, NR_INACTIVE_FILE) +
4588 memcg_page_state(memcg, NR_ACTIVE_FILE);
4590 *pheadroom = PAGE_COUNTER_MAX;
4591 while ((parent = parent_mem_cgroup(memcg))) {
4592 unsigned long ceiling = min(READ_ONCE(memcg->memory.max),
4593 READ_ONCE(memcg->memory.high));
4594 unsigned long used = page_counter_read(&memcg->memory);
4596 *pheadroom = min(*pheadroom, ceiling - min(ceiling, used));
4602 * Foreign dirty flushing
4604 * There's an inherent mismatch between memcg and writeback. The former
4605 * tracks ownership per-page while the latter per-inode. This was a
4606 * deliberate design decision because honoring per-page ownership in the
4607 * writeback path is complicated, may lead to higher CPU and IO overheads
4608 * and deemed unnecessary given that write-sharing an inode across
4609 * different cgroups isn't a common use-case.
4611 * Combined with inode majority-writer ownership switching, this works well
4612 * enough in most cases but there are some pathological cases. For
4613 * example, let's say there are two cgroups A and B which keep writing to
4614 * different but confined parts of the same inode. B owns the inode and
4615 * A's memory is limited far below B's. A's dirty ratio can rise enough to
4616 * trigger balance_dirty_pages() sleeps but B's can be low enough to avoid
4617 * triggering background writeback. A will be slowed down without a way to
4618 * make writeback of the dirty pages happen.
4620 * Conditions like the above can lead to a cgroup getting repeatedly and
4621 * severely throttled after making some progress after each
4622 * dirty_expire_interval while the underlying IO device is almost
4625 * Solving this problem completely requires matching the ownership tracking
4626 * granularities between memcg and writeback in either direction. However,
4627 * the more egregious behaviors can be avoided by simply remembering the
4628 * most recent foreign dirtying events and initiating remote flushes on
4629 * them when local writeback isn't enough to keep the memory clean enough.
4631 * The following two functions implement such mechanism. When a foreign
4632 * page - a page whose memcg and writeback ownerships don't match - is
4633 * dirtied, mem_cgroup_track_foreign_dirty() records the inode owning
4634 * bdi_writeback on the page owning memcg. When balance_dirty_pages()
4635 * decides that the memcg needs to sleep due to high dirty ratio, it calls
4636 * mem_cgroup_flush_foreign() which queues writeback on the recorded
4637 * foreign bdi_writebacks which haven't expired. Both the numbers of
4638 * recorded bdi_writebacks and concurrent in-flight foreign writebacks are
4639 * limited to MEMCG_CGWB_FRN_CNT.
4641 * The mechanism only remembers IDs and doesn't hold any object references.
4642 * As being wrong occasionally doesn't matter, updates and accesses to the
4643 * records are lockless and racy.
4645 void mem_cgroup_track_foreign_dirty_slowpath(struct folio *folio,
4646 struct bdi_writeback *wb)
4648 struct mem_cgroup *memcg = folio_memcg(folio);
4649 struct memcg_cgwb_frn *frn;
4650 u64 now = get_jiffies_64();
4651 u64 oldest_at = now;
4655 trace_track_foreign_dirty(folio, wb);
4658 * Pick the slot to use. If there is already a slot for @wb, keep
4659 * using it. If not replace the oldest one which isn't being
4662 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) {
4663 frn = &memcg->cgwb_frn[i];
4664 if (frn->bdi_id == wb->bdi->id &&
4665 frn->memcg_id == wb->memcg_css->id)
4667 if (time_before64(frn->at, oldest_at) &&
4668 atomic_read(&frn->done.cnt) == 1) {
4670 oldest_at = frn->at;
4674 if (i < MEMCG_CGWB_FRN_CNT) {
4676 * Re-using an existing one. Update timestamp lazily to
4677 * avoid making the cacheline hot. We want them to be
4678 * reasonably up-to-date and significantly shorter than
4679 * dirty_expire_interval as that's what expires the record.
4680 * Use the shorter of 1s and dirty_expire_interval / 8.
4682 unsigned long update_intv =
4683 min_t(unsigned long, HZ,
4684 msecs_to_jiffies(dirty_expire_interval * 10) / 8);
4686 if (time_before64(frn->at, now - update_intv))
4688 } else if (oldest >= 0) {
4689 /* replace the oldest free one */
4690 frn = &memcg->cgwb_frn[oldest];
4691 frn->bdi_id = wb->bdi->id;
4692 frn->memcg_id = wb->memcg_css->id;
4697 /* issue foreign writeback flushes for recorded foreign dirtying events */
4698 void mem_cgroup_flush_foreign(struct bdi_writeback *wb)
4700 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
4701 unsigned long intv = msecs_to_jiffies(dirty_expire_interval * 10);
4702 u64 now = jiffies_64;
4705 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) {
4706 struct memcg_cgwb_frn *frn = &memcg->cgwb_frn[i];
4709 * If the record is older than dirty_expire_interval,
4710 * writeback on it has already started. No need to kick it
4711 * off again. Also, don't start a new one if there's
4712 * already one in flight.
4714 if (time_after64(frn->at, now - intv) &&
4715 atomic_read(&frn->done.cnt) == 1) {
4717 trace_flush_foreign(wb, frn->bdi_id, frn->memcg_id);
4718 cgroup_writeback_by_id(frn->bdi_id, frn->memcg_id,
4719 WB_REASON_FOREIGN_FLUSH,
4725 #else /* CONFIG_CGROUP_WRITEBACK */
4727 static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp)
4732 static void memcg_wb_domain_exit(struct mem_cgroup *memcg)
4736 static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)
4740 #endif /* CONFIG_CGROUP_WRITEBACK */
4743 * DO NOT USE IN NEW FILES.
4745 * "cgroup.event_control" implementation.
4747 * This is way over-engineered. It tries to support fully configurable
4748 * events for each user. Such level of flexibility is completely
4749 * unnecessary especially in the light of the planned unified hierarchy.
4751 * Please deprecate this and replace with something simpler if at all
4756 * Unregister event and free resources.
4758 * Gets called from workqueue.
4760 static void memcg_event_remove(struct work_struct *work)
4762 struct mem_cgroup_event *event =
4763 container_of(work, struct mem_cgroup_event, remove);
4764 struct mem_cgroup *memcg = event->memcg;
4766 remove_wait_queue(event->wqh, &event->wait);
4768 event->unregister_event(memcg, event->eventfd);
4770 /* Notify userspace the event is going away. */
4771 eventfd_signal(event->eventfd, 1);
4773 eventfd_ctx_put(event->eventfd);
4775 css_put(&memcg->css);
4779 * Gets called on EPOLLHUP on eventfd when user closes it.
4781 * Called with wqh->lock held and interrupts disabled.
4783 static int memcg_event_wake(wait_queue_entry_t *wait, unsigned mode,
4784 int sync, void *key)
4786 struct mem_cgroup_event *event =
4787 container_of(wait, struct mem_cgroup_event, wait);
4788 struct mem_cgroup *memcg = event->memcg;
4789 __poll_t flags = key_to_poll(key);
4791 if (flags & EPOLLHUP) {
4793 * If the event has been detached at cgroup removal, we
4794 * can simply return knowing the other side will cleanup
4797 * We can't race against event freeing since the other
4798 * side will require wqh->lock via remove_wait_queue(),
4801 spin_lock(&memcg->event_list_lock);
4802 if (!list_empty(&event->list)) {
4803 list_del_init(&event->list);
4805 * We are in atomic context, but cgroup_event_remove()
4806 * may sleep, so we have to call it in workqueue.
4808 schedule_work(&event->remove);
4810 spin_unlock(&memcg->event_list_lock);
4816 static void memcg_event_ptable_queue_proc(struct file *file,
4817 wait_queue_head_t *wqh, poll_table *pt)
4819 struct mem_cgroup_event *event =
4820 container_of(pt, struct mem_cgroup_event, pt);
4823 add_wait_queue(wqh, &event->wait);
4827 * DO NOT USE IN NEW FILES.
4829 * Parse input and register new cgroup event handler.
4831 * Input must be in format '<event_fd> <control_fd> <args>'.
4832 * Interpretation of args is defined by control file implementation.
4834 static ssize_t memcg_write_event_control(struct kernfs_open_file *of,
4835 char *buf, size_t nbytes, loff_t off)
4837 struct cgroup_subsys_state *css = of_css(of);
4838 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
4839 struct mem_cgroup_event *event;
4840 struct cgroup_subsys_state *cfile_css;
4841 unsigned int efd, cfd;
4844 struct dentry *cdentry;
4849 if (IS_ENABLED(CONFIG_PREEMPT_RT))
4852 buf = strstrip(buf);
4854 efd = simple_strtoul(buf, &endp, 10);
4859 cfd = simple_strtoul(buf, &endp, 10);
4860 if ((*endp != ' ') && (*endp != '\0'))
4864 event = kzalloc(sizeof(*event), GFP_KERNEL);
4868 event->memcg = memcg;
4869 INIT_LIST_HEAD(&event->list);
4870 init_poll_funcptr(&event->pt, memcg_event_ptable_queue_proc);
4871 init_waitqueue_func_entry(&event->wait, memcg_event_wake);
4872 INIT_WORK(&event->remove, memcg_event_remove);
4880 event->eventfd = eventfd_ctx_fileget(efile.file);
4881 if (IS_ERR(event->eventfd)) {
4882 ret = PTR_ERR(event->eventfd);
4889 goto out_put_eventfd;
4892 /* the process need read permission on control file */
4893 /* AV: shouldn't we check that it's been opened for read instead? */
4894 ret = file_permission(cfile.file, MAY_READ);
4899 * The control file must be a regular cgroup1 file. As a regular cgroup
4900 * file can't be renamed, it's safe to access its name afterwards.
4902 cdentry = cfile.file->f_path.dentry;
4903 if (cdentry->d_sb->s_type != &cgroup_fs_type || !d_is_reg(cdentry)) {
4909 * Determine the event callbacks and set them in @event. This used
4910 * to be done via struct cftype but cgroup core no longer knows
4911 * about these events. The following is crude but the whole thing
4912 * is for compatibility anyway.
4914 * DO NOT ADD NEW FILES.
4916 name = cdentry->d_name.name;
4918 if (!strcmp(name, "memory.usage_in_bytes")) {
4919 event->register_event = mem_cgroup_usage_register_event;
4920 event->unregister_event = mem_cgroup_usage_unregister_event;
4921 } else if (!strcmp(name, "memory.oom_control")) {
4922 event->register_event = mem_cgroup_oom_register_event;
4923 event->unregister_event = mem_cgroup_oom_unregister_event;
4924 } else if (!strcmp(name, "memory.pressure_level")) {
4925 event->register_event = vmpressure_register_event;
4926 event->unregister_event = vmpressure_unregister_event;
4927 } else if (!strcmp(name, "memory.memsw.usage_in_bytes")) {
4928 event->register_event = memsw_cgroup_usage_register_event;
4929 event->unregister_event = memsw_cgroup_usage_unregister_event;
4936 * Verify @cfile should belong to @css. Also, remaining events are
4937 * automatically removed on cgroup destruction but the removal is
4938 * asynchronous, so take an extra ref on @css.
4940 cfile_css = css_tryget_online_from_dir(cdentry->d_parent,
4941 &memory_cgrp_subsys);
4943 if (IS_ERR(cfile_css))
4945 if (cfile_css != css) {
4950 ret = event->register_event(memcg, event->eventfd, buf);
4954 vfs_poll(efile.file, &event->pt);
4956 spin_lock_irq(&memcg->event_list_lock);
4957 list_add(&event->list, &memcg->event_list);
4958 spin_unlock_irq(&memcg->event_list_lock);
4970 eventfd_ctx_put(event->eventfd);
4979 #if defined(CONFIG_MEMCG_KMEM) && (defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG))
4980 static int mem_cgroup_slab_show(struct seq_file *m, void *p)
4984 * Please, take a look at tools/cgroup/memcg_slabinfo.py .
4990 static struct cftype mem_cgroup_legacy_files[] = {
4992 .name = "usage_in_bytes",
4993 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
4994 .read_u64 = mem_cgroup_read_u64,
4997 .name = "max_usage_in_bytes",
4998 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
4999 .write = mem_cgroup_reset,
5000 .read_u64 = mem_cgroup_read_u64,
5003 .name = "limit_in_bytes",
5004 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
5005 .write = mem_cgroup_write,
5006 .read_u64 = mem_cgroup_read_u64,
5009 .name = "soft_limit_in_bytes",
5010 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5011 .write = mem_cgroup_write,
5012 .read_u64 = mem_cgroup_read_u64,
5016 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
5017 .write = mem_cgroup_reset,
5018 .read_u64 = mem_cgroup_read_u64,
5022 .seq_show = memcg_stat_show,
5025 .name = "force_empty",
5026 .write = mem_cgroup_force_empty_write,
5029 .name = "use_hierarchy",
5030 .write_u64 = mem_cgroup_hierarchy_write,
5031 .read_u64 = mem_cgroup_hierarchy_read,
5034 .name = "cgroup.event_control", /* XXX: for compat */
5035 .write = memcg_write_event_control,
5036 .flags = CFTYPE_NO_PREFIX | CFTYPE_WORLD_WRITABLE,
5039 .name = "swappiness",
5040 .read_u64 = mem_cgroup_swappiness_read,
5041 .write_u64 = mem_cgroup_swappiness_write,
5044 .name = "move_charge_at_immigrate",
5045 .read_u64 = mem_cgroup_move_charge_read,
5046 .write_u64 = mem_cgroup_move_charge_write,
5049 .name = "oom_control",
5050 .seq_show = mem_cgroup_oom_control_read,
5051 .write_u64 = mem_cgroup_oom_control_write,
5054 .name = "pressure_level",
5058 .name = "numa_stat",
5059 .seq_show = memcg_numa_stat_show,
5063 .name = "kmem.limit_in_bytes",
5064 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5065 .write = mem_cgroup_write,
5066 .read_u64 = mem_cgroup_read_u64,
5069 .name = "kmem.usage_in_bytes",
5070 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
5071 .read_u64 = mem_cgroup_read_u64,
5074 .name = "kmem.failcnt",
5075 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
5076 .write = mem_cgroup_reset,
5077 .read_u64 = mem_cgroup_read_u64,
5080 .name = "kmem.max_usage_in_bytes",
5081 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
5082 .write = mem_cgroup_reset,
5083 .read_u64 = mem_cgroup_read_u64,
5085 #if defined(CONFIG_MEMCG_KMEM) && \
5086 (defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG))
5088 .name = "kmem.slabinfo",
5089 .seq_show = mem_cgroup_slab_show,
5093 .name = "kmem.tcp.limit_in_bytes",
5094 .private = MEMFILE_PRIVATE(_TCP, RES_LIMIT),
5095 .write = mem_cgroup_write,
5096 .read_u64 = mem_cgroup_read_u64,
5099 .name = "kmem.tcp.usage_in_bytes",
5100 .private = MEMFILE_PRIVATE(_TCP, RES_USAGE),
5101 .read_u64 = mem_cgroup_read_u64,
5104 .name = "kmem.tcp.failcnt",
5105 .private = MEMFILE_PRIVATE(_TCP, RES_FAILCNT),
5106 .write = mem_cgroup_reset,
5107 .read_u64 = mem_cgroup_read_u64,
5110 .name = "kmem.tcp.max_usage_in_bytes",
5111 .private = MEMFILE_PRIVATE(_TCP, RES_MAX_USAGE),
5112 .write = mem_cgroup_reset,
5113 .read_u64 = mem_cgroup_read_u64,
5115 { }, /* terminate */
5119 * Private memory cgroup IDR
5121 * Swap-out records and page cache shadow entries need to store memcg
5122 * references in constrained space, so we maintain an ID space that is
5123 * limited to 16 bit (MEM_CGROUP_ID_MAX), limiting the total number of
5124 * memory-controlled cgroups to 64k.
5126 * However, there usually are many references to the offline CSS after
5127 * the cgroup has been destroyed, such as page cache or reclaimable
5128 * slab objects, that don't need to hang on to the ID. We want to keep
5129 * those dead CSS from occupying IDs, or we might quickly exhaust the
5130 * relatively small ID space and prevent the creation of new cgroups
5131 * even when there are much fewer than 64k cgroups - possibly none.
5133 * Maintain a private 16-bit ID space for memcg, and allow the ID to
5134 * be freed and recycled when it's no longer needed, which is usually
5135 * when the CSS is offlined.
5137 * The only exception to that are records of swapped out tmpfs/shmem
5138 * pages that need to be attributed to live ancestors on swapin. But
5139 * those references are manageable from userspace.
5142 static DEFINE_IDR(mem_cgroup_idr);
5144 static void mem_cgroup_id_remove(struct mem_cgroup *memcg)
5146 if (memcg->id.id > 0) {
5147 idr_remove(&mem_cgroup_idr, memcg->id.id);
5152 static void __maybe_unused mem_cgroup_id_get_many(struct mem_cgroup *memcg,
5155 refcount_add(n, &memcg->id.ref);
5158 static void mem_cgroup_id_put_many(struct mem_cgroup *memcg, unsigned int n)
5160 if (refcount_sub_and_test(n, &memcg->id.ref)) {
5161 mem_cgroup_id_remove(memcg);
5163 /* Memcg ID pins CSS */
5164 css_put(&memcg->css);
5168 static inline void mem_cgroup_id_put(struct mem_cgroup *memcg)
5170 mem_cgroup_id_put_many(memcg, 1);
5174 * mem_cgroup_from_id - look up a memcg from a memcg id
5175 * @id: the memcg id to look up
5177 * Caller must hold rcu_read_lock().
5179 struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
5181 WARN_ON_ONCE(!rcu_read_lock_held());
5182 return idr_find(&mem_cgroup_idr, id);
5185 #ifdef CONFIG_SHRINKER_DEBUG
5186 struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino)
5188 struct cgroup *cgrp;
5189 struct cgroup_subsys_state *css;
5190 struct mem_cgroup *memcg;
5192 cgrp = cgroup_get_from_id(ino);
5194 return ERR_CAST(cgrp);
5196 css = cgroup_get_e_css(cgrp, &memory_cgrp_subsys);
5198 memcg = container_of(css, struct mem_cgroup, css);
5200 memcg = ERR_PTR(-ENOENT);
5208 static int alloc_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node)
5210 struct mem_cgroup_per_node *pn;
5212 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, node);
5216 pn->lruvec_stats_percpu = alloc_percpu_gfp(struct lruvec_stats_percpu,
5217 GFP_KERNEL_ACCOUNT);
5218 if (!pn->lruvec_stats_percpu) {
5223 lruvec_init(&pn->lruvec);
5226 memcg->nodeinfo[node] = pn;
5230 static void free_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node)
5232 struct mem_cgroup_per_node *pn = memcg->nodeinfo[node];
5237 free_percpu(pn->lruvec_stats_percpu);
5241 static void __mem_cgroup_free(struct mem_cgroup *memcg)
5246 free_mem_cgroup_per_node_info(memcg, node);
5247 kfree(memcg->vmstats);
5248 free_percpu(memcg->vmstats_percpu);
5252 static void mem_cgroup_free(struct mem_cgroup *memcg)
5254 lru_gen_exit_memcg(memcg);
5255 memcg_wb_domain_exit(memcg);
5256 __mem_cgroup_free(memcg);
5259 static struct mem_cgroup *mem_cgroup_alloc(void)
5261 struct mem_cgroup *memcg;
5263 int __maybe_unused i;
5264 long error = -ENOMEM;
5266 memcg = kzalloc(struct_size(memcg, nodeinfo, nr_node_ids), GFP_KERNEL);
5268 return ERR_PTR(error);
5270 memcg->id.id = idr_alloc(&mem_cgroup_idr, NULL,
5271 1, MEM_CGROUP_ID_MAX + 1, GFP_KERNEL);
5272 if (memcg->id.id < 0) {
5273 error = memcg->id.id;
5277 memcg->vmstats = kzalloc(sizeof(struct memcg_vmstats), GFP_KERNEL);
5278 if (!memcg->vmstats)
5281 memcg->vmstats_percpu = alloc_percpu_gfp(struct memcg_vmstats_percpu,
5282 GFP_KERNEL_ACCOUNT);
5283 if (!memcg->vmstats_percpu)
5287 if (alloc_mem_cgroup_per_node_info(memcg, node))
5290 if (memcg_wb_domain_init(memcg, GFP_KERNEL))
5293 INIT_WORK(&memcg->high_work, high_work_func);
5294 INIT_LIST_HEAD(&memcg->oom_notify);
5295 mutex_init(&memcg->thresholds_lock);
5296 spin_lock_init(&memcg->move_lock);
5297 vmpressure_init(&memcg->vmpressure);
5298 INIT_LIST_HEAD(&memcg->event_list);
5299 spin_lock_init(&memcg->event_list_lock);
5300 memcg->socket_pressure = jiffies;
5301 #ifdef CONFIG_MEMCG_KMEM
5302 memcg->kmemcg_id = -1;
5303 INIT_LIST_HEAD(&memcg->objcg_list);
5305 #ifdef CONFIG_CGROUP_WRITEBACK
5306 INIT_LIST_HEAD(&memcg->cgwb_list);
5307 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++)
5308 memcg->cgwb_frn[i].done =
5309 __WB_COMPLETION_INIT(&memcg_cgwb_frn_waitq);
5311 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
5312 spin_lock_init(&memcg->deferred_split_queue.split_queue_lock);
5313 INIT_LIST_HEAD(&memcg->deferred_split_queue.split_queue);
5314 memcg->deferred_split_queue.split_queue_len = 0;
5316 idr_replace(&mem_cgroup_idr, memcg, memcg->id.id);
5317 lru_gen_init_memcg(memcg);
5320 mem_cgroup_id_remove(memcg);
5321 __mem_cgroup_free(memcg);
5322 return ERR_PTR(error);
5325 static struct cgroup_subsys_state * __ref
5326 mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
5328 struct mem_cgroup *parent = mem_cgroup_from_css(parent_css);
5329 struct mem_cgroup *memcg, *old_memcg;
5331 old_memcg = set_active_memcg(parent);
5332 memcg = mem_cgroup_alloc();
5333 set_active_memcg(old_memcg);
5335 return ERR_CAST(memcg);
5337 page_counter_set_high(&memcg->memory, PAGE_COUNTER_MAX);
5338 memcg->soft_limit = PAGE_COUNTER_MAX;
5339 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
5340 memcg->zswap_max = PAGE_COUNTER_MAX;
5342 page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX);
5344 memcg->swappiness = mem_cgroup_swappiness(parent);
5345 memcg->oom_kill_disable = parent->oom_kill_disable;
5347 page_counter_init(&memcg->memory, &parent->memory);
5348 page_counter_init(&memcg->swap, &parent->swap);
5349 page_counter_init(&memcg->kmem, &parent->kmem);
5350 page_counter_init(&memcg->tcpmem, &parent->tcpmem);
5352 init_memcg_events();
5353 page_counter_init(&memcg->memory, NULL);
5354 page_counter_init(&memcg->swap, NULL);
5355 page_counter_init(&memcg->kmem, NULL);
5356 page_counter_init(&memcg->tcpmem, NULL);
5358 root_mem_cgroup = memcg;
5362 if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket)
5363 static_branch_inc(&memcg_sockets_enabled_key);
5368 static int mem_cgroup_css_online(struct cgroup_subsys_state *css)
5370 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5372 if (memcg_online_kmem(memcg))
5376 * A memcg must be visible for expand_shrinker_info()
5377 * by the time the maps are allocated. So, we allocate maps
5378 * here, when for_each_mem_cgroup() can't skip it.
5380 if (alloc_shrinker_info(memcg))
5383 /* Online state pins memcg ID, memcg ID pins CSS */
5384 refcount_set(&memcg->id.ref, 1);
5387 if (unlikely(mem_cgroup_is_root(memcg)))
5388 queue_delayed_work(system_unbound_wq, &stats_flush_dwork,
5392 memcg_offline_kmem(memcg);
5394 mem_cgroup_id_remove(memcg);
5398 static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
5400 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5401 struct mem_cgroup_event *event, *tmp;
5404 * Unregister events and notify userspace.
5405 * Notify userspace about cgroup removing only after rmdir of cgroup
5406 * directory to avoid race between userspace and kernelspace.
5408 spin_lock_irq(&memcg->event_list_lock);
5409 list_for_each_entry_safe(event, tmp, &memcg->event_list, list) {
5410 list_del_init(&event->list);
5411 schedule_work(&event->remove);
5413 spin_unlock_irq(&memcg->event_list_lock);
5415 page_counter_set_min(&memcg->memory, 0);
5416 page_counter_set_low(&memcg->memory, 0);
5418 memcg_offline_kmem(memcg);
5419 reparent_shrinker_deferred(memcg);
5420 wb_memcg_offline(memcg);
5422 drain_all_stock(memcg);
5424 mem_cgroup_id_put(memcg);
5427 static void mem_cgroup_css_released(struct cgroup_subsys_state *css)
5429 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5431 invalidate_reclaim_iterators(memcg);
5434 static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
5436 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5437 int __maybe_unused i;
5439 #ifdef CONFIG_CGROUP_WRITEBACK
5440 for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++)
5441 wb_wait_for_completion(&memcg->cgwb_frn[i].done);
5443 if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket)
5444 static_branch_dec(&memcg_sockets_enabled_key);
5446 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_active)
5447 static_branch_dec(&memcg_sockets_enabled_key);
5449 vmpressure_cleanup(&memcg->vmpressure);
5450 cancel_work_sync(&memcg->high_work);
5451 mem_cgroup_remove_from_trees(memcg);
5452 free_shrinker_info(memcg);
5453 mem_cgroup_free(memcg);
5457 * mem_cgroup_css_reset - reset the states of a mem_cgroup
5458 * @css: the target css
5460 * Reset the states of the mem_cgroup associated with @css. This is
5461 * invoked when the userland requests disabling on the default hierarchy
5462 * but the memcg is pinned through dependency. The memcg should stop
5463 * applying policies and should revert to the vanilla state as it may be
5464 * made visible again.
5466 * The current implementation only resets the essential configurations.
5467 * This needs to be expanded to cover all the visible parts.
5469 static void mem_cgroup_css_reset(struct cgroup_subsys_state *css)
5471 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5473 page_counter_set_max(&memcg->memory, PAGE_COUNTER_MAX);
5474 page_counter_set_max(&memcg->swap, PAGE_COUNTER_MAX);
5475 page_counter_set_max(&memcg->kmem, PAGE_COUNTER_MAX);
5476 page_counter_set_max(&memcg->tcpmem, PAGE_COUNTER_MAX);
5477 page_counter_set_min(&memcg->memory, 0);
5478 page_counter_set_low(&memcg->memory, 0);
5479 page_counter_set_high(&memcg->memory, PAGE_COUNTER_MAX);
5480 memcg->soft_limit = PAGE_COUNTER_MAX;
5481 page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX);
5482 memcg_wb_domain_size_changed(memcg);
5485 static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu)
5487 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5488 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5489 struct memcg_vmstats_percpu *statc;
5493 statc = per_cpu_ptr(memcg->vmstats_percpu, cpu);
5495 for (i = 0; i < MEMCG_NR_STAT; i++) {
5497 * Collect the aggregated propagation counts of groups
5498 * below us. We're in a per-cpu loop here and this is
5499 * a global counter, so the first cycle will get them.
5501 delta = memcg->vmstats->state_pending[i];
5503 memcg->vmstats->state_pending[i] = 0;
5505 /* Add CPU changes on this level since the last flush */
5506 v = READ_ONCE(statc->state[i]);
5507 if (v != statc->state_prev[i]) {
5508 delta += v - statc->state_prev[i];
5509 statc->state_prev[i] = v;
5515 /* Aggregate counts on this level and propagate upwards */
5516 memcg->vmstats->state[i] += delta;
5518 parent->vmstats->state_pending[i] += delta;
5521 for (i = 0; i < NR_MEMCG_EVENTS; i++) {
5522 delta = memcg->vmstats->events_pending[i];
5524 memcg->vmstats->events_pending[i] = 0;
5526 v = READ_ONCE(statc->events[i]);
5527 if (v != statc->events_prev[i]) {
5528 delta += v - statc->events_prev[i];
5529 statc->events_prev[i] = v;
5535 memcg->vmstats->events[i] += delta;
5537 parent->vmstats->events_pending[i] += delta;
5540 for_each_node_state(nid, N_MEMORY) {
5541 struct mem_cgroup_per_node *pn = memcg->nodeinfo[nid];
5542 struct mem_cgroup_per_node *ppn = NULL;
5543 struct lruvec_stats_percpu *lstatc;
5546 ppn = parent->nodeinfo[nid];
5548 lstatc = per_cpu_ptr(pn->lruvec_stats_percpu, cpu);
5550 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
5551 delta = pn->lruvec_stats.state_pending[i];
5553 pn->lruvec_stats.state_pending[i] = 0;
5555 v = READ_ONCE(lstatc->state[i]);
5556 if (v != lstatc->state_prev[i]) {
5557 delta += v - lstatc->state_prev[i];
5558 lstatc->state_prev[i] = v;
5564 pn->lruvec_stats.state[i] += delta;
5566 ppn->lruvec_stats.state_pending[i] += delta;
5572 /* Handlers for move charge at task migration. */
5573 static int mem_cgroup_do_precharge(unsigned long count)
5577 /* Try a single bulk charge without reclaim first, kswapd may wake */
5578 ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_DIRECT_RECLAIM, count);
5580 mc.precharge += count;
5584 /* Try charges one by one with reclaim, but do not retry */
5586 ret = try_charge(mc.to, GFP_KERNEL | __GFP_NORETRY, 1);
5600 enum mc_target_type {
5607 static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5608 unsigned long addr, pte_t ptent)
5610 struct page *page = vm_normal_page(vma, addr, ptent);
5612 if (!page || !page_mapped(page))
5614 if (PageAnon(page)) {
5615 if (!(mc.flags & MOVE_ANON))
5618 if (!(mc.flags & MOVE_FILE))
5621 if (!get_page_unless_zero(page))
5627 #if defined(CONFIG_SWAP) || defined(CONFIG_DEVICE_PRIVATE)
5628 static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5629 pte_t ptent, swp_entry_t *entry)
5631 struct page *page = NULL;
5632 swp_entry_t ent = pte_to_swp_entry(ptent);
5634 if (!(mc.flags & MOVE_ANON))
5638 * Handle device private pages that are not accessible by the CPU, but
5639 * stored as special swap entries in the page table.
5641 if (is_device_private_entry(ent)) {
5642 page = pfn_swap_entry_to_page(ent);
5643 if (!get_page_unless_zero(page))
5648 if (non_swap_entry(ent))
5652 * Because swap_cache_get_folio() updates some statistics counter,
5653 * we call find_get_page() with swapper_space directly.
5655 page = find_get_page(swap_address_space(ent), swp_offset(ent));
5656 entry->val = ent.val;
5661 static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5662 pte_t ptent, swp_entry_t *entry)
5668 static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5669 unsigned long addr, pte_t ptent)
5671 unsigned long index;
5672 struct folio *folio;
5674 if (!vma->vm_file) /* anonymous vma */
5676 if (!(mc.flags & MOVE_FILE))
5679 /* folio is moved even if it's not RSS of this task(page-faulted). */
5680 /* shmem/tmpfs may report page out on swap: account for that too. */
5681 index = linear_page_index(vma, addr);
5682 folio = filemap_get_incore_folio(vma->vm_file->f_mapping, index);
5685 return folio_file_page(folio, index);
5689 * mem_cgroup_move_account - move account of the page
5691 * @compound: charge the page as compound or small page
5692 * @from: mem_cgroup which the page is moved from.
5693 * @to: mem_cgroup which the page is moved to. @from != @to.
5695 * The caller must make sure the page is not on LRU (isolate_page() is useful.)
5697 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
5700 static int mem_cgroup_move_account(struct page *page,
5702 struct mem_cgroup *from,
5703 struct mem_cgroup *to)
5705 struct folio *folio = page_folio(page);
5706 struct lruvec *from_vec, *to_vec;
5707 struct pglist_data *pgdat;
5708 unsigned int nr_pages = compound ? folio_nr_pages(folio) : 1;
5711 VM_BUG_ON(from == to);
5712 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
5713 VM_BUG_ON(compound && !folio_test_large(folio));
5716 * Prevent mem_cgroup_migrate() from looking at
5717 * page's memory cgroup of its source page while we change it.
5720 if (!folio_trylock(folio))
5724 if (folio_memcg(folio) != from)
5727 pgdat = folio_pgdat(folio);
5728 from_vec = mem_cgroup_lruvec(from, pgdat);
5729 to_vec = mem_cgroup_lruvec(to, pgdat);
5731 folio_memcg_lock(folio);
5733 if (folio_test_anon(folio)) {
5734 if (folio_mapped(folio)) {
5735 __mod_lruvec_state(from_vec, NR_ANON_MAPPED, -nr_pages);
5736 __mod_lruvec_state(to_vec, NR_ANON_MAPPED, nr_pages);
5737 if (folio_test_transhuge(folio)) {
5738 __mod_lruvec_state(from_vec, NR_ANON_THPS,
5740 __mod_lruvec_state(to_vec, NR_ANON_THPS,
5745 __mod_lruvec_state(from_vec, NR_FILE_PAGES, -nr_pages);
5746 __mod_lruvec_state(to_vec, NR_FILE_PAGES, nr_pages);
5748 if (folio_test_swapbacked(folio)) {
5749 __mod_lruvec_state(from_vec, NR_SHMEM, -nr_pages);
5750 __mod_lruvec_state(to_vec, NR_SHMEM, nr_pages);
5753 if (folio_mapped(folio)) {
5754 __mod_lruvec_state(from_vec, NR_FILE_MAPPED, -nr_pages);
5755 __mod_lruvec_state(to_vec, NR_FILE_MAPPED, nr_pages);
5758 if (folio_test_dirty(folio)) {
5759 struct address_space *mapping = folio_mapping(folio);
5761 if (mapping_can_writeback(mapping)) {
5762 __mod_lruvec_state(from_vec, NR_FILE_DIRTY,
5764 __mod_lruvec_state(to_vec, NR_FILE_DIRTY,
5771 if (folio_test_swapcache(folio)) {
5772 __mod_lruvec_state(from_vec, NR_SWAPCACHE, -nr_pages);
5773 __mod_lruvec_state(to_vec, NR_SWAPCACHE, nr_pages);
5776 if (folio_test_writeback(folio)) {
5777 __mod_lruvec_state(from_vec, NR_WRITEBACK, -nr_pages);
5778 __mod_lruvec_state(to_vec, NR_WRITEBACK, nr_pages);
5782 * All state has been migrated, let's switch to the new memcg.
5784 * It is safe to change page's memcg here because the page
5785 * is referenced, charged, isolated, and locked: we can't race
5786 * with (un)charging, migration, LRU putback, or anything else
5787 * that would rely on a stable page's memory cgroup.
5789 * Note that lock_page_memcg is a memcg lock, not a page lock,
5790 * to save space. As soon as we switch page's memory cgroup to a
5791 * new memcg that isn't locked, the above state can change
5792 * concurrently again. Make sure we're truly done with it.
5797 css_put(&from->css);
5799 folio->memcg_data = (unsigned long)to;
5801 __folio_memcg_unlock(from);
5804 nid = folio_nid(folio);
5806 local_irq_disable();
5807 mem_cgroup_charge_statistics(to, nr_pages);
5808 memcg_check_events(to, nid);
5809 mem_cgroup_charge_statistics(from, -nr_pages);
5810 memcg_check_events(from, nid);
5813 folio_unlock(folio);
5819 * get_mctgt_type - get target type of moving charge
5820 * @vma: the vma the pte to be checked belongs
5821 * @addr: the address corresponding to the pte to be checked
5822 * @ptent: the pte to be checked
5823 * @target: the pointer the target page or swap ent will be stored(can be NULL)
5826 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5827 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5828 * move charge. if @target is not NULL, the page is stored in target->page
5829 * with extra refcnt got(Callers should handle it).
5830 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5831 * target for charge migration. if @target is not NULL, the entry is stored
5833 * 3(MC_TARGET_DEVICE): like MC_TARGET_PAGE but page is device memory and
5834 * thus not on the lru.
5835 * For now we such page is charge like a regular page would be as for all
5836 * intent and purposes it is just special memory taking the place of a
5839 * See Documentations/vm/hmm.txt and include/linux/hmm.h
5841 * Called with pte lock held.
5844 static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
5845 unsigned long addr, pte_t ptent, union mc_target *target)
5847 struct page *page = NULL;
5848 enum mc_target_type ret = MC_TARGET_NONE;
5849 swp_entry_t ent = { .val = 0 };
5851 if (pte_present(ptent))
5852 page = mc_handle_present_pte(vma, addr, ptent);
5853 else if (pte_none_mostly(ptent))
5855 * PTE markers should be treated as a none pte here, separated
5856 * from other swap handling below.
5858 page = mc_handle_file_pte(vma, addr, ptent);
5859 else if (is_swap_pte(ptent))
5860 page = mc_handle_swap_pte(vma, ptent, &ent);
5862 if (!page && !ent.val)
5866 * Do only loose check w/o serialization.
5867 * mem_cgroup_move_account() checks the page is valid or
5868 * not under LRU exclusion.
5870 if (page_memcg(page) == mc.from) {
5871 ret = MC_TARGET_PAGE;
5872 if (is_device_private_page(page) ||
5873 is_device_coherent_page(page))
5874 ret = MC_TARGET_DEVICE;
5876 target->page = page;
5878 if (!ret || !target)
5882 * There is a swap entry and a page doesn't exist or isn't charged.
5883 * But we cannot move a tail-page in a THP.
5885 if (ent.val && !ret && (!page || !PageTransCompound(page)) &&
5886 mem_cgroup_id(mc.from) == lookup_swap_cgroup_id(ent)) {
5887 ret = MC_TARGET_SWAP;
5894 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
5896 * We don't consider PMD mapped swapping or file mapped pages because THP does
5897 * not support them for now.
5898 * Caller should make sure that pmd_trans_huge(pmd) is true.
5900 static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5901 unsigned long addr, pmd_t pmd, union mc_target *target)
5903 struct page *page = NULL;
5904 enum mc_target_type ret = MC_TARGET_NONE;
5906 if (unlikely(is_swap_pmd(pmd))) {
5907 VM_BUG_ON(thp_migration_supported() &&
5908 !is_pmd_migration_entry(pmd));
5911 page = pmd_page(pmd);
5912 VM_BUG_ON_PAGE(!page || !PageHead(page), page);
5913 if (!(mc.flags & MOVE_ANON))
5915 if (page_memcg(page) == mc.from) {
5916 ret = MC_TARGET_PAGE;
5919 target->page = page;
5925 static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5926 unsigned long addr, pmd_t pmd, union mc_target *target)
5928 return MC_TARGET_NONE;
5932 static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5933 unsigned long addr, unsigned long end,
5934 struct mm_walk *walk)
5936 struct vm_area_struct *vma = walk->vma;
5940 ptl = pmd_trans_huge_lock(pmd, vma);
5943 * Note their can not be MC_TARGET_DEVICE for now as we do not
5944 * support transparent huge page with MEMORY_DEVICE_PRIVATE but
5945 * this might change.
5947 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
5948 mc.precharge += HPAGE_PMD_NR;
5953 if (pmd_trans_unstable(pmd))
5955 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5956 for (; addr != end; pte++, addr += PAGE_SIZE)
5957 if (get_mctgt_type(vma, addr, *pte, NULL))
5958 mc.precharge++; /* increment precharge temporarily */
5959 pte_unmap_unlock(pte - 1, ptl);
5965 static const struct mm_walk_ops precharge_walk_ops = {
5966 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5969 static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5971 unsigned long precharge;
5974 walk_page_range(mm, 0, ULONG_MAX, &precharge_walk_ops, NULL);
5975 mmap_read_unlock(mm);
5977 precharge = mc.precharge;
5983 static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5985 unsigned long precharge = mem_cgroup_count_precharge(mm);
5987 VM_BUG_ON(mc.moving_task);
5988 mc.moving_task = current;
5989 return mem_cgroup_do_precharge(precharge);
5992 /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5993 static void __mem_cgroup_clear_mc(void)
5995 struct mem_cgroup *from = mc.from;
5996 struct mem_cgroup *to = mc.to;
5998 /* we must uncharge all the leftover precharges from mc.to */
6000 cancel_charge(mc.to, mc.precharge);
6004 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
6005 * we must uncharge here.
6007 if (mc.moved_charge) {
6008 cancel_charge(mc.from, mc.moved_charge);
6009 mc.moved_charge = 0;
6011 /* we must fixup refcnts and charges */
6012 if (mc.moved_swap) {
6013 /* uncharge swap account from the old cgroup */
6014 if (!mem_cgroup_is_root(mc.from))
6015 page_counter_uncharge(&mc.from->memsw, mc.moved_swap);
6017 mem_cgroup_id_put_many(mc.from, mc.moved_swap);
6020 * we charged both to->memory and to->memsw, so we
6021 * should uncharge to->memory.
6023 if (!mem_cgroup_is_root(mc.to))
6024 page_counter_uncharge(&mc.to->memory, mc.moved_swap);
6028 memcg_oom_recover(from);
6029 memcg_oom_recover(to);
6030 wake_up_all(&mc.waitq);
6033 static void mem_cgroup_clear_mc(void)
6035 struct mm_struct *mm = mc.mm;
6038 * we must clear moving_task before waking up waiters at the end of
6041 mc.moving_task = NULL;
6042 __mem_cgroup_clear_mc();
6043 spin_lock(&mc.lock);
6047 spin_unlock(&mc.lock);
6052 static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
6054 struct cgroup_subsys_state *css;
6055 struct mem_cgroup *memcg = NULL; /* unneeded init to make gcc happy */
6056 struct mem_cgroup *from;
6057 struct task_struct *leader, *p;
6058 struct mm_struct *mm;
6059 unsigned long move_flags;
6062 /* charge immigration isn't supported on the default hierarchy */
6063 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
6067 * Multi-process migrations only happen on the default hierarchy
6068 * where charge immigration is not used. Perform charge
6069 * immigration if @tset contains a leader and whine if there are
6073 cgroup_taskset_for_each_leader(leader, css, tset) {
6076 memcg = mem_cgroup_from_css(css);
6082 * We are now committed to this value whatever it is. Changes in this
6083 * tunable will only affect upcoming migrations, not the current one.
6084 * So we need to save it, and keep it going.
6086 move_flags = READ_ONCE(memcg->move_charge_at_immigrate);
6090 from = mem_cgroup_from_task(p);
6092 VM_BUG_ON(from == memcg);
6094 mm = get_task_mm(p);
6097 /* We move charges only when we move a owner of the mm */
6098 if (mm->owner == p) {
6101 VM_BUG_ON(mc.precharge);
6102 VM_BUG_ON(mc.moved_charge);
6103 VM_BUG_ON(mc.moved_swap);
6105 spin_lock(&mc.lock);
6109 mc.flags = move_flags;
6110 spin_unlock(&mc.lock);
6111 /* We set mc.moving_task later */
6113 ret = mem_cgroup_precharge_mc(mm);
6115 mem_cgroup_clear_mc();
6122 static void mem_cgroup_cancel_attach(struct cgroup_taskset *tset)
6125 mem_cgroup_clear_mc();
6128 static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
6129 unsigned long addr, unsigned long end,
6130 struct mm_walk *walk)
6133 struct vm_area_struct *vma = walk->vma;
6136 enum mc_target_type target_type;
6137 union mc_target target;
6140 ptl = pmd_trans_huge_lock(pmd, vma);
6142 if (mc.precharge < HPAGE_PMD_NR) {
6146 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
6147 if (target_type == MC_TARGET_PAGE) {
6149 if (!isolate_lru_page(page)) {
6150 if (!mem_cgroup_move_account(page, true,
6152 mc.precharge -= HPAGE_PMD_NR;
6153 mc.moved_charge += HPAGE_PMD_NR;
6155 putback_lru_page(page);
6158 } else if (target_type == MC_TARGET_DEVICE) {
6160 if (!mem_cgroup_move_account(page, true,
6162 mc.precharge -= HPAGE_PMD_NR;
6163 mc.moved_charge += HPAGE_PMD_NR;
6171 if (pmd_trans_unstable(pmd))
6174 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6175 for (; addr != end; addr += PAGE_SIZE) {
6176 pte_t ptent = *(pte++);
6177 bool device = false;
6183 switch (get_mctgt_type(vma, addr, ptent, &target)) {
6184 case MC_TARGET_DEVICE:
6187 case MC_TARGET_PAGE:
6190 * We can have a part of the split pmd here. Moving it
6191 * can be done but it would be too convoluted so simply
6192 * ignore such a partial THP and keep it in original
6193 * memcg. There should be somebody mapping the head.
6195 if (PageTransCompound(page))
6197 if (!device && isolate_lru_page(page))
6199 if (!mem_cgroup_move_account(page, false,
6202 /* we uncharge from mc.from later. */
6206 putback_lru_page(page);
6207 put: /* get_mctgt_type() gets the page */
6210 case MC_TARGET_SWAP:
6212 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
6214 mem_cgroup_id_get_many(mc.to, 1);
6215 /* we fixup other refcnts and charges later. */
6223 pte_unmap_unlock(pte - 1, ptl);
6228 * We have consumed all precharges we got in can_attach().
6229 * We try charge one by one, but don't do any additional
6230 * charges to mc.to if we have failed in charge once in attach()
6233 ret = mem_cgroup_do_precharge(1);
6241 static const struct mm_walk_ops charge_walk_ops = {
6242 .pmd_entry = mem_cgroup_move_charge_pte_range,
6245 static void mem_cgroup_move_charge(void)
6247 lru_add_drain_all();
6249 * Signal lock_page_memcg() to take the memcg's move_lock
6250 * while we're moving its pages to another memcg. Then wait
6251 * for already started RCU-only updates to finish.
6253 atomic_inc(&mc.from->moving_account);
6256 if (unlikely(!mmap_read_trylock(mc.mm))) {
6258 * Someone who are holding the mmap_lock might be waiting in
6259 * waitq. So we cancel all extra charges, wake up all waiters,
6260 * and retry. Because we cancel precharges, we might not be able
6261 * to move enough charges, but moving charge is a best-effort
6262 * feature anyway, so it wouldn't be a big problem.
6264 __mem_cgroup_clear_mc();
6269 * When we have consumed all precharges and failed in doing
6270 * additional charge, the page walk just aborts.
6272 walk_page_range(mc.mm, 0, ULONG_MAX, &charge_walk_ops, NULL);
6273 mmap_read_unlock(mc.mm);
6274 atomic_dec(&mc.from->moving_account);
6277 static void mem_cgroup_move_task(void)
6280 mem_cgroup_move_charge();
6281 mem_cgroup_clear_mc();
6284 #else /* !CONFIG_MMU */
6285 static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
6289 static void mem_cgroup_cancel_attach(struct cgroup_taskset *tset)
6292 static void mem_cgroup_move_task(void)
6297 #ifdef CONFIG_LRU_GEN
6298 static void mem_cgroup_attach(struct cgroup_taskset *tset)
6300 struct task_struct *task;
6301 struct cgroup_subsys_state *css;
6303 /* find the first leader if there is any */
6304 cgroup_taskset_for_each_leader(task, css, tset)
6311 if (task->mm && READ_ONCE(task->mm->owner) == task)
6312 lru_gen_migrate_mm(task->mm);
6316 static void mem_cgroup_attach(struct cgroup_taskset *tset)
6319 #endif /* CONFIG_LRU_GEN */
6321 static int seq_puts_memcg_tunable(struct seq_file *m, unsigned long value)
6323 if (value == PAGE_COUNTER_MAX)
6324 seq_puts(m, "max\n");
6326 seq_printf(m, "%llu\n", (u64)value * PAGE_SIZE);
6331 static u64 memory_current_read(struct cgroup_subsys_state *css,
6334 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
6336 return (u64)page_counter_read(&memcg->memory) * PAGE_SIZE;
6339 static u64 memory_peak_read(struct cgroup_subsys_state *css,
6342 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
6344 return (u64)memcg->memory.watermark * PAGE_SIZE;
6347 static int memory_min_show(struct seq_file *m, void *v)
6349 return seq_puts_memcg_tunable(m,
6350 READ_ONCE(mem_cgroup_from_seq(m)->memory.min));
6353 static ssize_t memory_min_write(struct kernfs_open_file *of,
6354 char *buf, size_t nbytes, loff_t off)
6356 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6360 buf = strstrip(buf);
6361 err = page_counter_memparse(buf, "max", &min);
6365 page_counter_set_min(&memcg->memory, min);
6370 static int memory_low_show(struct seq_file *m, void *v)
6372 return seq_puts_memcg_tunable(m,
6373 READ_ONCE(mem_cgroup_from_seq(m)->memory.low));
6376 static ssize_t memory_low_write(struct kernfs_open_file *of,
6377 char *buf, size_t nbytes, loff_t off)
6379 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6383 buf = strstrip(buf);
6384 err = page_counter_memparse(buf, "max", &low);
6388 page_counter_set_low(&memcg->memory, low);
6393 static int memory_high_show(struct seq_file *m, void *v)
6395 return seq_puts_memcg_tunable(m,
6396 READ_ONCE(mem_cgroup_from_seq(m)->memory.high));
6399 static ssize_t memory_high_write(struct kernfs_open_file *of,
6400 char *buf, size_t nbytes, loff_t off)
6402 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6403 unsigned int nr_retries = MAX_RECLAIM_RETRIES;
6404 bool drained = false;
6408 buf = strstrip(buf);
6409 err = page_counter_memparse(buf, "max", &high);
6413 page_counter_set_high(&memcg->memory, high);
6416 unsigned long nr_pages = page_counter_read(&memcg->memory);
6417 unsigned long reclaimed;
6419 if (nr_pages <= high)
6422 if (signal_pending(current))
6426 drain_all_stock(memcg);
6431 reclaimed = try_to_free_mem_cgroup_pages(memcg, nr_pages - high,
6432 GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP,
6435 if (!reclaimed && !nr_retries--)
6439 memcg_wb_domain_size_changed(memcg);
6443 static int memory_max_show(struct seq_file *m, void *v)
6445 return seq_puts_memcg_tunable(m,
6446 READ_ONCE(mem_cgroup_from_seq(m)->memory.max));
6449 static ssize_t memory_max_write(struct kernfs_open_file *of,
6450 char *buf, size_t nbytes, loff_t off)
6452 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6453 unsigned int nr_reclaims = MAX_RECLAIM_RETRIES;
6454 bool drained = false;
6458 buf = strstrip(buf);
6459 err = page_counter_memparse(buf, "max", &max);
6463 xchg(&memcg->memory.max, max);
6466 unsigned long nr_pages = page_counter_read(&memcg->memory);
6468 if (nr_pages <= max)
6471 if (signal_pending(current))
6475 drain_all_stock(memcg);
6481 if (!try_to_free_mem_cgroup_pages(memcg, nr_pages - max,
6482 GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP,
6488 memcg_memory_event(memcg, MEMCG_OOM);
6489 if (!mem_cgroup_out_of_memory(memcg, GFP_KERNEL, 0))
6493 memcg_wb_domain_size_changed(memcg);
6497 static void __memory_events_show(struct seq_file *m, atomic_long_t *events)
6499 seq_printf(m, "low %lu\n", atomic_long_read(&events[MEMCG_LOW]));
6500 seq_printf(m, "high %lu\n", atomic_long_read(&events[MEMCG_HIGH]));
6501 seq_printf(m, "max %lu\n", atomic_long_read(&events[MEMCG_MAX]));
6502 seq_printf(m, "oom %lu\n", atomic_long_read(&events[MEMCG_OOM]));
6503 seq_printf(m, "oom_kill %lu\n",
6504 atomic_long_read(&events[MEMCG_OOM_KILL]));
6505 seq_printf(m, "oom_group_kill %lu\n",
6506 atomic_long_read(&events[MEMCG_OOM_GROUP_KILL]));
6509 static int memory_events_show(struct seq_file *m, void *v)
6511 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
6513 __memory_events_show(m, memcg->memory_events);
6517 static int memory_events_local_show(struct seq_file *m, void *v)
6519 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
6521 __memory_events_show(m, memcg->memory_events_local);
6525 static int memory_stat_show(struct seq_file *m, void *v)
6527 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
6528 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
6532 memory_stat_format(memcg, buf, PAGE_SIZE);
6539 static inline unsigned long lruvec_page_state_output(struct lruvec *lruvec,
6542 return lruvec_page_state(lruvec, item) * memcg_page_state_unit(item);
6545 static int memory_numa_stat_show(struct seq_file *m, void *v)
6548 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
6550 mem_cgroup_flush_stats();
6552 for (i = 0; i < ARRAY_SIZE(memory_stats); i++) {
6555 if (memory_stats[i].idx >= NR_VM_NODE_STAT_ITEMS)
6558 seq_printf(m, "%s", memory_stats[i].name);
6559 for_each_node_state(nid, N_MEMORY) {
6561 struct lruvec *lruvec;
6563 lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid));
6564 size = lruvec_page_state_output(lruvec,
6565 memory_stats[i].idx);
6566 seq_printf(m, " N%d=%llu", nid, size);
6575 static int memory_oom_group_show(struct seq_file *m, void *v)
6577 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
6579 seq_printf(m, "%d\n", memcg->oom_group);
6584 static ssize_t memory_oom_group_write(struct kernfs_open_file *of,
6585 char *buf, size_t nbytes, loff_t off)
6587 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6590 buf = strstrip(buf);
6594 ret = kstrtoint(buf, 0, &oom_group);
6598 if (oom_group != 0 && oom_group != 1)
6601 memcg->oom_group = oom_group;
6607 MEMORY_RECLAIM_NODES = 0,
6608 MEMORY_RECLAIM_NULL,
6611 static const match_table_t if_tokens = {
6612 { MEMORY_RECLAIM_NODES, "nodes=%s" },
6613 { MEMORY_RECLAIM_NULL, NULL },
6616 static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
6617 size_t nbytes, loff_t off)
6619 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6620 unsigned int nr_retries = MAX_RECLAIM_RETRIES;
6621 unsigned long nr_to_reclaim, nr_reclaimed = 0;
6622 unsigned int reclaim_options = MEMCG_RECLAIM_MAY_SWAP |
6623 MEMCG_RECLAIM_PROACTIVE;
6624 char *old_buf, *start;
6625 substring_t args[MAX_OPT_ARGS];
6628 nodemask_t nodemask = NODE_MASK_ALL;
6630 buf = strstrip(buf);
6633 nr_to_reclaim = memparse(buf, &buf) / PAGE_SIZE;
6637 buf = strstrip(buf);
6639 while ((start = strsep(&buf, " ")) != NULL) {
6642 token = match_token(start, if_tokens, args);
6643 match_strlcpy(value, args, sizeof(value));
6645 case MEMORY_RECLAIM_NODES:
6646 if (nodelist_parse(value, nodemask) < 0)
6654 while (nr_reclaimed < nr_to_reclaim) {
6655 unsigned long reclaimed;
6657 if (signal_pending(current))
6661 * This is the final attempt, drain percpu lru caches in the
6662 * hope of introducing more evictable pages for
6663 * try_to_free_mem_cgroup_pages().
6666 lru_add_drain_all();
6668 reclaimed = try_to_free_mem_cgroup_pages(memcg,
6669 nr_to_reclaim - nr_reclaimed,
6670 GFP_KERNEL, reclaim_options,
6673 if (!reclaimed && !nr_retries--)
6676 nr_reclaimed += reclaimed;
6682 static struct cftype memory_files[] = {
6685 .flags = CFTYPE_NOT_ON_ROOT,
6686 .read_u64 = memory_current_read,
6690 .flags = CFTYPE_NOT_ON_ROOT,
6691 .read_u64 = memory_peak_read,
6695 .flags = CFTYPE_NOT_ON_ROOT,
6696 .seq_show = memory_min_show,
6697 .write = memory_min_write,
6701 .flags = CFTYPE_NOT_ON_ROOT,
6702 .seq_show = memory_low_show,
6703 .write = memory_low_write,
6707 .flags = CFTYPE_NOT_ON_ROOT,
6708 .seq_show = memory_high_show,
6709 .write = memory_high_write,
6713 .flags = CFTYPE_NOT_ON_ROOT,
6714 .seq_show = memory_max_show,
6715 .write = memory_max_write,
6719 .flags = CFTYPE_NOT_ON_ROOT,
6720 .file_offset = offsetof(struct mem_cgroup, events_file),
6721 .seq_show = memory_events_show,
6724 .name = "events.local",
6725 .flags = CFTYPE_NOT_ON_ROOT,
6726 .file_offset = offsetof(struct mem_cgroup, events_local_file),
6727 .seq_show = memory_events_local_show,
6731 .seq_show = memory_stat_show,
6735 .name = "numa_stat",
6736 .seq_show = memory_numa_stat_show,
6740 .name = "oom.group",
6741 .flags = CFTYPE_NOT_ON_ROOT | CFTYPE_NS_DELEGATABLE,
6742 .seq_show = memory_oom_group_show,
6743 .write = memory_oom_group_write,
6747 .flags = CFTYPE_NS_DELEGATABLE,
6748 .write = memory_reclaim,
6753 struct cgroup_subsys memory_cgrp_subsys = {
6754 .css_alloc = mem_cgroup_css_alloc,
6755 .css_online = mem_cgroup_css_online,
6756 .css_offline = mem_cgroup_css_offline,
6757 .css_released = mem_cgroup_css_released,
6758 .css_free = mem_cgroup_css_free,
6759 .css_reset = mem_cgroup_css_reset,
6760 .css_rstat_flush = mem_cgroup_css_rstat_flush,
6761 .can_attach = mem_cgroup_can_attach,
6762 .attach = mem_cgroup_attach,
6763 .cancel_attach = mem_cgroup_cancel_attach,
6764 .post_attach = mem_cgroup_move_task,
6765 .dfl_cftypes = memory_files,
6766 .legacy_cftypes = mem_cgroup_legacy_files,
6771 * This function calculates an individual cgroup's effective
6772 * protection which is derived from its own memory.min/low, its
6773 * parent's and siblings' settings, as well as the actual memory
6774 * distribution in the tree.
6776 * The following rules apply to the effective protection values:
6778 * 1. At the first level of reclaim, effective protection is equal to
6779 * the declared protection in memory.min and memory.low.
6781 * 2. To enable safe delegation of the protection configuration, at
6782 * subsequent levels the effective protection is capped to the
6783 * parent's effective protection.
6785 * 3. To make complex and dynamic subtrees easier to configure, the
6786 * user is allowed to overcommit the declared protection at a given
6787 * level. If that is the case, the parent's effective protection is
6788 * distributed to the children in proportion to how much protection
6789 * they have declared and how much of it they are utilizing.
6791 * This makes distribution proportional, but also work-conserving:
6792 * if one cgroup claims much more protection than it uses memory,
6793 * the unused remainder is available to its siblings.
6795 * 4. Conversely, when the declared protection is undercommitted at a
6796 * given level, the distribution of the larger parental protection
6797 * budget is NOT proportional. A cgroup's protection from a sibling
6798 * is capped to its own memory.min/low setting.
6800 * 5. However, to allow protecting recursive subtrees from each other
6801 * without having to declare each individual cgroup's fixed share
6802 * of the ancestor's claim to protection, any unutilized -
6803 * "floating" - protection from up the tree is distributed in
6804 * proportion to each cgroup's *usage*. This makes the protection
6805 * neutral wrt sibling cgroups and lets them compete freely over
6806 * the shared parental protection budget, but it protects the
6807 * subtree as a whole from neighboring subtrees.
6809 * Note that 4. and 5. are not in conflict: 4. is about protecting
6810 * against immediate siblings whereas 5. is about protecting against
6811 * neighboring subtrees.
6813 static unsigned long effective_protection(unsigned long usage,
6814 unsigned long parent_usage,
6815 unsigned long setting,
6816 unsigned long parent_effective,
6817 unsigned long siblings_protected)
6819 unsigned long protected;
6822 protected = min(usage, setting);
6824 * If all cgroups at this level combined claim and use more
6825 * protection then what the parent affords them, distribute
6826 * shares in proportion to utilization.
6828 * We are using actual utilization rather than the statically
6829 * claimed protection in order to be work-conserving: claimed
6830 * but unused protection is available to siblings that would
6831 * otherwise get a smaller chunk than what they claimed.
6833 if (siblings_protected > parent_effective)
6834 return protected * parent_effective / siblings_protected;
6837 * Ok, utilized protection of all children is within what the
6838 * parent affords them, so we know whatever this child claims
6839 * and utilizes is effectively protected.
6841 * If there is unprotected usage beyond this value, reclaim
6842 * will apply pressure in proportion to that amount.
6844 * If there is unutilized protection, the cgroup will be fully
6845 * shielded from reclaim, but we do return a smaller value for
6846 * protection than what the group could enjoy in theory. This
6847 * is okay. With the overcommit distribution above, effective
6848 * protection is always dependent on how memory is actually
6849 * consumed among the siblings anyway.
6854 * If the children aren't claiming (all of) the protection
6855 * afforded to them by the parent, distribute the remainder in
6856 * proportion to the (unprotected) memory of each cgroup. That
6857 * way, cgroups that aren't explicitly prioritized wrt each
6858 * other compete freely over the allowance, but they are
6859 * collectively protected from neighboring trees.
6861 * We're using unprotected memory for the weight so that if
6862 * some cgroups DO claim explicit protection, we don't protect
6863 * the same bytes twice.
6865 * Check both usage and parent_usage against the respective
6866 * protected values. One should imply the other, but they
6867 * aren't read atomically - make sure the division is sane.
6869 if (!(cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT))
6871 if (parent_effective > siblings_protected &&
6872 parent_usage > siblings_protected &&
6873 usage > protected) {
6874 unsigned long unclaimed;
6876 unclaimed = parent_effective - siblings_protected;
6877 unclaimed *= usage - protected;
6878 unclaimed /= parent_usage - siblings_protected;
6887 * mem_cgroup_calculate_protection - check if memory consumption is in the normal range
6888 * @root: the top ancestor of the sub-tree being checked
6889 * @memcg: the memory cgroup to check
6891 * WARNING: This function is not stateless! It can only be used as part
6892 * of a top-down tree iteration, not for isolated queries.
6894 void mem_cgroup_calculate_protection(struct mem_cgroup *root,
6895 struct mem_cgroup *memcg)
6897 unsigned long usage, parent_usage;
6898 struct mem_cgroup *parent;
6900 if (mem_cgroup_disabled())
6904 root = root_mem_cgroup;
6907 * Effective values of the reclaim targets are ignored so they
6908 * can be stale. Have a look at mem_cgroup_protection for more
6910 * TODO: calculation should be more robust so that we do not need
6911 * that special casing.
6916 usage = page_counter_read(&memcg->memory);
6920 parent = parent_mem_cgroup(memcg);
6922 if (parent == root) {
6923 memcg->memory.emin = READ_ONCE(memcg->memory.min);
6924 memcg->memory.elow = READ_ONCE(memcg->memory.low);
6928 parent_usage = page_counter_read(&parent->memory);
6930 WRITE_ONCE(memcg->memory.emin, effective_protection(usage, parent_usage,
6931 READ_ONCE(memcg->memory.min),
6932 READ_ONCE(parent->memory.emin),
6933 atomic_long_read(&parent->memory.children_min_usage)));
6935 WRITE_ONCE(memcg->memory.elow, effective_protection(usage, parent_usage,
6936 READ_ONCE(memcg->memory.low),
6937 READ_ONCE(parent->memory.elow),
6938 atomic_long_read(&parent->memory.children_low_usage)));
6941 static int charge_memcg(struct folio *folio, struct mem_cgroup *memcg,
6944 long nr_pages = folio_nr_pages(folio);
6947 ret = try_charge(memcg, gfp, nr_pages);
6951 css_get(&memcg->css);
6952 commit_charge(folio, memcg);
6954 local_irq_disable();
6955 mem_cgroup_charge_statistics(memcg, nr_pages);
6956 memcg_check_events(memcg, folio_nid(folio));
6962 int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp)
6964 struct mem_cgroup *memcg;
6967 memcg = get_mem_cgroup_from_mm(mm);
6968 ret = charge_memcg(folio, memcg, gfp);
6969 css_put(&memcg->css);
6975 * mem_cgroup_swapin_charge_folio - Charge a newly allocated folio for swapin.
6976 * @folio: folio to charge.
6977 * @mm: mm context of the victim
6978 * @gfp: reclaim mode
6979 * @entry: swap entry for which the folio is allocated
6981 * This function charges a folio allocated for swapin. Please call this before
6982 * adding the folio to the swapcache.
6984 * Returns 0 on success. Otherwise, an error code is returned.
6986 int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm,
6987 gfp_t gfp, swp_entry_t entry)
6989 struct mem_cgroup *memcg;
6993 if (mem_cgroup_disabled())
6996 id = lookup_swap_cgroup_id(entry);
6998 memcg = mem_cgroup_from_id(id);
6999 if (!memcg || !css_tryget_online(&memcg->css))
7000 memcg = get_mem_cgroup_from_mm(mm);
7003 ret = charge_memcg(folio, memcg, gfp);
7005 css_put(&memcg->css);
7010 * mem_cgroup_swapin_uncharge_swap - uncharge swap slot
7011 * @entry: swap entry for which the page is charged
7013 * Call this function after successfully adding the charged page to swapcache.
7015 * Note: This function assumes the page for which swap slot is being uncharged
7018 void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry)
7021 * Cgroup1's unified memory+swap counter has been charged with the
7022 * new swapcache page, finish the transfer by uncharging the swap
7023 * slot. The swap slot would also get uncharged when it dies, but
7024 * it can stick around indefinitely and we'd count the page twice
7027 * Cgroup2 has separate resource counters for memory and swap,
7028 * so this is a non-issue here. Memory and swap charge lifetimes
7029 * correspond 1:1 to page and swap slot lifetimes: we charge the
7030 * page to memory here, and uncharge swap when the slot is freed.
7032 if (!mem_cgroup_disabled() && do_memsw_account()) {
7034 * The swap entry might not get freed for a long time,
7035 * let's not wait for it. The page already received a
7036 * memory+swap charge, drop the swap entry duplicate.
7038 mem_cgroup_uncharge_swap(entry, 1);
7042 struct uncharge_gather {
7043 struct mem_cgroup *memcg;
7044 unsigned long nr_memory;
7045 unsigned long pgpgout;
7046 unsigned long nr_kmem;
7050 static inline void uncharge_gather_clear(struct uncharge_gather *ug)
7052 memset(ug, 0, sizeof(*ug));
7055 static void uncharge_batch(const struct uncharge_gather *ug)
7057 unsigned long flags;
7059 if (ug->nr_memory) {
7060 page_counter_uncharge(&ug->memcg->memory, ug->nr_memory);
7061 if (do_memsw_account())
7062 page_counter_uncharge(&ug->memcg->memsw, ug->nr_memory);
7064 memcg_account_kmem(ug->memcg, -ug->nr_kmem);
7065 memcg_oom_recover(ug->memcg);
7068 local_irq_save(flags);
7069 __count_memcg_events(ug->memcg, PGPGOUT, ug->pgpgout);
7070 __this_cpu_add(ug->memcg->vmstats_percpu->nr_page_events, ug->nr_memory);
7071 memcg_check_events(ug->memcg, ug->nid);
7072 local_irq_restore(flags);
7074 /* drop reference from uncharge_folio */
7075 css_put(&ug->memcg->css);
7078 static void uncharge_folio(struct folio *folio, struct uncharge_gather *ug)
7081 struct mem_cgroup *memcg;
7082 struct obj_cgroup *objcg;
7084 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
7087 * Nobody should be changing or seriously looking at
7088 * folio memcg or objcg at this point, we have fully
7089 * exclusive access to the folio.
7091 if (folio_memcg_kmem(folio)) {
7092 objcg = __folio_objcg(folio);
7094 * This get matches the put at the end of the function and
7095 * kmem pages do not hold memcg references anymore.
7097 memcg = get_mem_cgroup_from_objcg(objcg);
7099 memcg = __folio_memcg(folio);
7105 if (ug->memcg != memcg) {
7108 uncharge_gather_clear(ug);
7111 ug->nid = folio_nid(folio);
7113 /* pairs with css_put in uncharge_batch */
7114 css_get(&memcg->css);
7117 nr_pages = folio_nr_pages(folio);
7119 if (folio_memcg_kmem(folio)) {
7120 ug->nr_memory += nr_pages;
7121 ug->nr_kmem += nr_pages;
7123 folio->memcg_data = 0;
7124 obj_cgroup_put(objcg);
7126 /* LRU pages aren't accounted at the root level */
7127 if (!mem_cgroup_is_root(memcg))
7128 ug->nr_memory += nr_pages;
7131 folio->memcg_data = 0;
7134 css_put(&memcg->css);
7137 void __mem_cgroup_uncharge(struct folio *folio)
7139 struct uncharge_gather ug;
7141 /* Don't touch folio->lru of any random page, pre-check: */
7142 if (!folio_memcg(folio))
7145 uncharge_gather_clear(&ug);
7146 uncharge_folio(folio, &ug);
7147 uncharge_batch(&ug);
7151 * __mem_cgroup_uncharge_list - uncharge a list of page
7152 * @page_list: list of pages to uncharge
7154 * Uncharge a list of pages previously charged with
7155 * __mem_cgroup_charge().
7157 void __mem_cgroup_uncharge_list(struct list_head *page_list)
7159 struct uncharge_gather ug;
7160 struct folio *folio;
7162 uncharge_gather_clear(&ug);
7163 list_for_each_entry(folio, page_list, lru)
7164 uncharge_folio(folio, &ug);
7166 uncharge_batch(&ug);
7170 * mem_cgroup_migrate - Charge a folio's replacement.
7171 * @old: Currently circulating folio.
7172 * @new: Replacement folio.
7174 * Charge @new as a replacement folio for @old. @old will
7175 * be uncharged upon free.
7177 * Both folios must be locked, @new->mapping must be set up.
7179 void mem_cgroup_migrate(struct folio *old, struct folio *new)
7181 struct mem_cgroup *memcg;
7182 long nr_pages = folio_nr_pages(new);
7183 unsigned long flags;
7185 VM_BUG_ON_FOLIO(!folio_test_locked(old), old);
7186 VM_BUG_ON_FOLIO(!folio_test_locked(new), new);
7187 VM_BUG_ON_FOLIO(folio_test_anon(old) != folio_test_anon(new), new);
7188 VM_BUG_ON_FOLIO(folio_nr_pages(old) != nr_pages, new);
7190 if (mem_cgroup_disabled())
7193 /* Page cache replacement: new folio already charged? */
7194 if (folio_memcg(new))
7197 memcg = folio_memcg(old);
7198 VM_WARN_ON_ONCE_FOLIO(!memcg, old);
7202 /* Force-charge the new page. The old one will be freed soon */
7203 if (!mem_cgroup_is_root(memcg)) {
7204 page_counter_charge(&memcg->memory, nr_pages);
7205 if (do_memsw_account())
7206 page_counter_charge(&memcg->memsw, nr_pages);
7209 css_get(&memcg->css);
7210 commit_charge(new, memcg);
7212 local_irq_save(flags);
7213 mem_cgroup_charge_statistics(memcg, nr_pages);
7214 memcg_check_events(memcg, folio_nid(new));
7215 local_irq_restore(flags);
7218 DEFINE_STATIC_KEY_FALSE(memcg_sockets_enabled_key);
7219 EXPORT_SYMBOL(memcg_sockets_enabled_key);
7221 void mem_cgroup_sk_alloc(struct sock *sk)
7223 struct mem_cgroup *memcg;
7225 if (!mem_cgroup_sockets_enabled)
7228 /* Do not associate the sock with unrelated interrupted task's memcg. */
7233 memcg = mem_cgroup_from_task(current);
7234 if (mem_cgroup_is_root(memcg))
7236 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && !memcg->tcpmem_active)
7238 if (css_tryget(&memcg->css))
7239 sk->sk_memcg = memcg;
7244 void mem_cgroup_sk_free(struct sock *sk)
7247 css_put(&sk->sk_memcg->css);
7251 * mem_cgroup_charge_skmem - charge socket memory
7252 * @memcg: memcg to charge
7253 * @nr_pages: number of pages to charge
7254 * @gfp_mask: reclaim mode
7256 * Charges @nr_pages to @memcg. Returns %true if the charge fit within
7257 * @memcg's configured limit, %false if it doesn't.
7259 bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages,
7262 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
7263 struct page_counter *fail;
7265 if (page_counter_try_charge(&memcg->tcpmem, nr_pages, &fail)) {
7266 memcg->tcpmem_pressure = 0;
7269 memcg->tcpmem_pressure = 1;
7270 if (gfp_mask & __GFP_NOFAIL) {
7271 page_counter_charge(&memcg->tcpmem, nr_pages);
7277 if (try_charge(memcg, gfp_mask, nr_pages) == 0) {
7278 mod_memcg_state(memcg, MEMCG_SOCK, nr_pages);
7286 * mem_cgroup_uncharge_skmem - uncharge socket memory
7287 * @memcg: memcg to uncharge
7288 * @nr_pages: number of pages to uncharge
7290 void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages)
7292 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
7293 page_counter_uncharge(&memcg->tcpmem, nr_pages);
7297 mod_memcg_state(memcg, MEMCG_SOCK, -nr_pages);
7299 refill_stock(memcg, nr_pages);
7302 static int __init cgroup_memory(char *s)
7306 while ((token = strsep(&s, ",")) != NULL) {
7309 if (!strcmp(token, "nosocket"))
7310 cgroup_memory_nosocket = true;
7311 if (!strcmp(token, "nokmem"))
7312 cgroup_memory_nokmem = true;
7316 __setup("cgroup.memory=", cgroup_memory);
7319 * subsys_initcall() for memory controller.
7321 * Some parts like memcg_hotplug_cpu_dead() have to be initialized from this
7322 * context because of lock dependencies (cgroup_lock -> cpu hotplug) but
7323 * basically everything that doesn't depend on a specific mem_cgroup structure
7324 * should be initialized from here.
7326 static int __init mem_cgroup_init(void)
7331 * Currently s32 type (can refer to struct batched_lruvec_stat) is
7332 * used for per-memcg-per-cpu caching of per-node statistics. In order
7333 * to work fine, we should make sure that the overfill threshold can't
7334 * exceed S32_MAX / PAGE_SIZE.
7336 BUILD_BUG_ON(MEMCG_CHARGE_BATCH > S32_MAX / PAGE_SIZE);
7338 cpuhp_setup_state_nocalls(CPUHP_MM_MEMCQ_DEAD, "mm/memctrl:dead", NULL,
7339 memcg_hotplug_cpu_dead);
7341 for_each_possible_cpu(cpu)
7342 INIT_WORK(&per_cpu_ptr(&memcg_stock, cpu)->work,
7345 for_each_node(node) {
7346 struct mem_cgroup_tree_per_node *rtpn;
7348 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL,
7349 node_online(node) ? node : NUMA_NO_NODE);
7351 rtpn->rb_root = RB_ROOT;
7352 rtpn->rb_rightmost = NULL;
7353 spin_lock_init(&rtpn->lock);
7354 soft_limit_tree.rb_tree_per_node[node] = rtpn;
7359 subsys_initcall(mem_cgroup_init);
7362 static struct mem_cgroup *mem_cgroup_id_get_online(struct mem_cgroup *memcg)
7364 while (!refcount_inc_not_zero(&memcg->id.ref)) {
7366 * The root cgroup cannot be destroyed, so it's refcount must
7369 if (WARN_ON_ONCE(mem_cgroup_is_root(memcg))) {
7373 memcg = parent_mem_cgroup(memcg);
7375 memcg = root_mem_cgroup;
7381 * mem_cgroup_swapout - transfer a memsw charge to swap
7382 * @folio: folio whose memsw charge to transfer
7383 * @entry: swap entry to move the charge to
7385 * Transfer the memsw charge of @folio to @entry.
7387 void mem_cgroup_swapout(struct folio *folio, swp_entry_t entry)
7389 struct mem_cgroup *memcg, *swap_memcg;
7390 unsigned int nr_entries;
7391 unsigned short oldid;
7393 VM_BUG_ON_FOLIO(folio_test_lru(folio), folio);
7394 VM_BUG_ON_FOLIO(folio_ref_count(folio), folio);
7396 if (mem_cgroup_disabled())
7399 if (!do_memsw_account())
7402 memcg = folio_memcg(folio);
7404 VM_WARN_ON_ONCE_FOLIO(!memcg, folio);
7409 * In case the memcg owning these pages has been offlined and doesn't
7410 * have an ID allocated to it anymore, charge the closest online
7411 * ancestor for the swap instead and transfer the memory+swap charge.
7413 swap_memcg = mem_cgroup_id_get_online(memcg);
7414 nr_entries = folio_nr_pages(folio);
7415 /* Get references for the tail pages, too */
7417 mem_cgroup_id_get_many(swap_memcg, nr_entries - 1);
7418 oldid = swap_cgroup_record(entry, mem_cgroup_id(swap_memcg),
7420 VM_BUG_ON_FOLIO(oldid, folio);
7421 mod_memcg_state(swap_memcg, MEMCG_SWAP, nr_entries);
7423 folio->memcg_data = 0;
7425 if (!mem_cgroup_is_root(memcg))
7426 page_counter_uncharge(&memcg->memory, nr_entries);
7428 if (memcg != swap_memcg) {
7429 if (!mem_cgroup_is_root(swap_memcg))
7430 page_counter_charge(&swap_memcg->memsw, nr_entries);
7431 page_counter_uncharge(&memcg->memsw, nr_entries);
7435 * Interrupts should be disabled here because the caller holds the
7436 * i_pages lock which is taken with interrupts-off. It is
7437 * important here to have the interrupts disabled because it is the
7438 * only synchronisation we have for updating the per-CPU variables.
7441 mem_cgroup_charge_statistics(memcg, -nr_entries);
7442 memcg_stats_unlock();
7443 memcg_check_events(memcg, folio_nid(folio));
7445 css_put(&memcg->css);
7449 * __mem_cgroup_try_charge_swap - try charging swap space for a folio
7450 * @folio: folio being added to swap
7451 * @entry: swap entry to charge
7453 * Try to charge @folio's memcg for the swap space at @entry.
7455 * Returns 0 on success, -ENOMEM on failure.
7457 int __mem_cgroup_try_charge_swap(struct folio *folio, swp_entry_t entry)
7459 unsigned int nr_pages = folio_nr_pages(folio);
7460 struct page_counter *counter;
7461 struct mem_cgroup *memcg;
7462 unsigned short oldid;
7464 if (do_memsw_account())
7467 memcg = folio_memcg(folio);
7469 VM_WARN_ON_ONCE_FOLIO(!memcg, folio);
7474 memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
7478 memcg = mem_cgroup_id_get_online(memcg);
7480 if (!mem_cgroup_is_root(memcg) &&
7481 !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) {
7482 memcg_memory_event(memcg, MEMCG_SWAP_MAX);
7483 memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
7484 mem_cgroup_id_put(memcg);
7488 /* Get references for the tail pages, too */
7490 mem_cgroup_id_get_many(memcg, nr_pages - 1);
7491 oldid = swap_cgroup_record(entry, mem_cgroup_id(memcg), nr_pages);
7492 VM_BUG_ON_FOLIO(oldid, folio);
7493 mod_memcg_state(memcg, MEMCG_SWAP, nr_pages);
7499 * __mem_cgroup_uncharge_swap - uncharge swap space
7500 * @entry: swap entry to uncharge
7501 * @nr_pages: the amount of swap space to uncharge
7503 void __mem_cgroup_uncharge_swap(swp_entry_t entry, unsigned int nr_pages)
7505 struct mem_cgroup *memcg;
7508 if (mem_cgroup_disabled())
7511 id = swap_cgroup_record(entry, 0, nr_pages);
7513 memcg = mem_cgroup_from_id(id);
7515 if (!mem_cgroup_is_root(memcg)) {
7516 if (do_memsw_account())
7517 page_counter_uncharge(&memcg->memsw, nr_pages);
7519 page_counter_uncharge(&memcg->swap, nr_pages);
7521 mod_memcg_state(memcg, MEMCG_SWAP, -nr_pages);
7522 mem_cgroup_id_put_many(memcg, nr_pages);
7527 long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg)
7529 long nr_swap_pages = get_nr_swap_pages();
7531 if (mem_cgroup_disabled() || do_memsw_account())
7532 return nr_swap_pages;
7533 for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg))
7534 nr_swap_pages = min_t(long, nr_swap_pages,
7535 READ_ONCE(memcg->swap.max) -
7536 page_counter_read(&memcg->swap));
7537 return nr_swap_pages;
7540 bool mem_cgroup_swap_full(struct folio *folio)
7542 struct mem_cgroup *memcg;
7544 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
7548 if (do_memsw_account())
7551 memcg = folio_memcg(folio);
7555 for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) {
7556 unsigned long usage = page_counter_read(&memcg->swap);
7558 if (usage * 2 >= READ_ONCE(memcg->swap.high) ||
7559 usage * 2 >= READ_ONCE(memcg->swap.max))
7566 static int __init setup_swap_account(char *s)
7568 pr_warn_once("The swapaccount= commandline option is deprecated. "
7569 "Please report your usecase to linux-mm@kvack.org if you "
7570 "depend on this functionality.\n");
7573 __setup("swapaccount=", setup_swap_account);
7575 static u64 swap_current_read(struct cgroup_subsys_state *css,
7578 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
7580 return (u64)page_counter_read(&memcg->swap) * PAGE_SIZE;
7583 static int swap_high_show(struct seq_file *m, void *v)
7585 return seq_puts_memcg_tunable(m,
7586 READ_ONCE(mem_cgroup_from_seq(m)->swap.high));
7589 static ssize_t swap_high_write(struct kernfs_open_file *of,
7590 char *buf, size_t nbytes, loff_t off)
7592 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
7596 buf = strstrip(buf);
7597 err = page_counter_memparse(buf, "max", &high);
7601 page_counter_set_high(&memcg->swap, high);
7606 static int swap_max_show(struct seq_file *m, void *v)
7608 return seq_puts_memcg_tunable(m,
7609 READ_ONCE(mem_cgroup_from_seq(m)->swap.max));
7612 static ssize_t swap_max_write(struct kernfs_open_file *of,
7613 char *buf, size_t nbytes, loff_t off)
7615 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
7619 buf = strstrip(buf);
7620 err = page_counter_memparse(buf, "max", &max);
7624 xchg(&memcg->swap.max, max);
7629 static int swap_events_show(struct seq_file *m, void *v)
7631 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
7633 seq_printf(m, "high %lu\n",
7634 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_HIGH]));
7635 seq_printf(m, "max %lu\n",
7636 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_MAX]));
7637 seq_printf(m, "fail %lu\n",
7638 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_FAIL]));
7643 static struct cftype swap_files[] = {
7645 .name = "swap.current",
7646 .flags = CFTYPE_NOT_ON_ROOT,
7647 .read_u64 = swap_current_read,
7650 .name = "swap.high",
7651 .flags = CFTYPE_NOT_ON_ROOT,
7652 .seq_show = swap_high_show,
7653 .write = swap_high_write,
7657 .flags = CFTYPE_NOT_ON_ROOT,
7658 .seq_show = swap_max_show,
7659 .write = swap_max_write,
7662 .name = "swap.events",
7663 .flags = CFTYPE_NOT_ON_ROOT,
7664 .file_offset = offsetof(struct mem_cgroup, swap_events_file),
7665 .seq_show = swap_events_show,
7670 static struct cftype memsw_files[] = {
7672 .name = "memsw.usage_in_bytes",
7673 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
7674 .read_u64 = mem_cgroup_read_u64,
7677 .name = "memsw.max_usage_in_bytes",
7678 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
7679 .write = mem_cgroup_reset,
7680 .read_u64 = mem_cgroup_read_u64,
7683 .name = "memsw.limit_in_bytes",
7684 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
7685 .write = mem_cgroup_write,
7686 .read_u64 = mem_cgroup_read_u64,
7689 .name = "memsw.failcnt",
7690 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
7691 .write = mem_cgroup_reset,
7692 .read_u64 = mem_cgroup_read_u64,
7694 { }, /* terminate */
7697 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
7699 * obj_cgroup_may_zswap - check if this cgroup can zswap
7700 * @objcg: the object cgroup
7702 * Check if the hierarchical zswap limit has been reached.
7704 * This doesn't check for specific headroom, and it is not atomic
7705 * either. But with zswap, the size of the allocation is only known
7706 * once compression has occured, and this optimistic pre-check avoids
7707 * spending cycles on compression when there is already no room left
7708 * or zswap is disabled altogether somewhere in the hierarchy.
7710 bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
7712 struct mem_cgroup *memcg, *original_memcg;
7715 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
7718 original_memcg = get_mem_cgroup_from_objcg(objcg);
7719 for (memcg = original_memcg; !mem_cgroup_is_root(memcg);
7720 memcg = parent_mem_cgroup(memcg)) {
7721 unsigned long max = READ_ONCE(memcg->zswap_max);
7722 unsigned long pages;
7724 if (max == PAGE_COUNTER_MAX)
7731 cgroup_rstat_flush(memcg->css.cgroup);
7732 pages = memcg_page_state(memcg, MEMCG_ZSWAP_B) / PAGE_SIZE;
7738 mem_cgroup_put(original_memcg);
7743 * obj_cgroup_charge_zswap - charge compression backend memory
7744 * @objcg: the object cgroup
7745 * @size: size of compressed object
7747 * This forces the charge after obj_cgroup_may_swap() allowed
7748 * compression and storage in zwap for this cgroup to go ahead.
7750 void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size)
7752 struct mem_cgroup *memcg;
7754 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
7757 VM_WARN_ON_ONCE(!(current->flags & PF_MEMALLOC));
7759 /* PF_MEMALLOC context, charging must succeed */
7760 if (obj_cgroup_charge(objcg, GFP_KERNEL, size))
7764 memcg = obj_cgroup_memcg(objcg);
7765 mod_memcg_state(memcg, MEMCG_ZSWAP_B, size);
7766 mod_memcg_state(memcg, MEMCG_ZSWAPPED, 1);
7771 * obj_cgroup_uncharge_zswap - uncharge compression backend memory
7772 * @objcg: the object cgroup
7773 * @size: size of compressed object
7775 * Uncharges zswap memory on page in.
7777 void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size)
7779 struct mem_cgroup *memcg;
7781 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
7784 obj_cgroup_uncharge(objcg, size);
7787 memcg = obj_cgroup_memcg(objcg);
7788 mod_memcg_state(memcg, MEMCG_ZSWAP_B, -size);
7789 mod_memcg_state(memcg, MEMCG_ZSWAPPED, -1);
7793 static u64 zswap_current_read(struct cgroup_subsys_state *css,
7796 cgroup_rstat_flush(css->cgroup);
7797 return memcg_page_state(mem_cgroup_from_css(css), MEMCG_ZSWAP_B);
7800 static int zswap_max_show(struct seq_file *m, void *v)
7802 return seq_puts_memcg_tunable(m,
7803 READ_ONCE(mem_cgroup_from_seq(m)->zswap_max));
7806 static ssize_t zswap_max_write(struct kernfs_open_file *of,
7807 char *buf, size_t nbytes, loff_t off)
7809 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
7813 buf = strstrip(buf);
7814 err = page_counter_memparse(buf, "max", &max);
7818 xchg(&memcg->zswap_max, max);
7823 static struct cftype zswap_files[] = {
7825 .name = "zswap.current",
7826 .flags = CFTYPE_NOT_ON_ROOT,
7827 .read_u64 = zswap_current_read,
7830 .name = "zswap.max",
7831 .flags = CFTYPE_NOT_ON_ROOT,
7832 .seq_show = zswap_max_show,
7833 .write = zswap_max_write,
7837 #endif /* CONFIG_MEMCG_KMEM && CONFIG_ZSWAP */
7839 static int __init mem_cgroup_swap_init(void)
7841 if (mem_cgroup_disabled())
7844 WARN_ON(cgroup_add_dfl_cftypes(&memory_cgrp_subsys, swap_files));
7845 WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, memsw_files));
7846 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP)
7847 WARN_ON(cgroup_add_dfl_cftypes(&memory_cgrp_subsys, zswap_files));
7851 subsys_initcall(mem_cgroup_swap_init);
7853 #endif /* CONFIG_SWAP */