Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2 | /* memcontrol.c - Memory Controller |
| 3 | * |
| 4 | * Copyright IBM Corporation, 2007 |
| 5 | * Author Balbir Singh <balbir@linux.vnet.ibm.com> |
| 6 | * |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 7 | * Copyright 2007 OpenVZ SWsoft Inc |
| 8 | * Author: Pavel Emelianov <xemul@openvz.org> |
| 9 | * |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 10 | * Memory thresholds |
| 11 | * Copyright (C) 2009 Nokia Corporation |
| 12 | * Author: Kirill A. Shutemov |
| 13 | * |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 14 | * Kernel Memory Controller |
| 15 | * Copyright (C) 2012 Parallels Inc. and Google Inc. |
| 16 | * Authors: Glauber Costa and Suleiman Souhlal |
| 17 | * |
Johannes Weiner | 1575e68 | 2015-04-14 15:44:51 -0700 | [diff] [blame] | 18 | * Native page reclaim |
| 19 | * Charge lifetime sanitation |
| 20 | * Lockless page tracking & accounting |
| 21 | * Unified hierarchy configuration model |
| 22 | * Copyright (C) 2015 Red Hat, Inc., Johannes Weiner |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 23 | * |
| 24 | * Per memcg lru locking |
| 25 | * Copyright (C) 2020 Alibaba, Inc, Alex Shi |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 26 | */ |
| 27 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 28 | #include <linux/page_counter.h> |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 29 | #include <linux/memcontrol.h> |
| 30 | #include <linux/cgroup.h> |
Christoph Hellwig | a520110 | 2019-08-28 16:19:53 +0200 | [diff] [blame] | 31 | #include <linux/pagewalk.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 32 | #include <linux/sched/mm.h> |
Hugh Dickins | 3a4f8a0 | 2017-02-24 14:59:36 -0800 | [diff] [blame] | 33 | #include <linux/shmem_fs.h> |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 34 | #include <linux/hugetlb.h> |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 35 | #include <linux/pagemap.h> |
Chris Down | 1ff9e6e | 2019-03-05 15:48:09 -0800 | [diff] [blame] | 36 | #include <linux/vm_event_item.h> |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 37 | #include <linux/smp.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 38 | #include <linux/page-flags.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 39 | #include <linux/backing-dev.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 40 | #include <linux/bit_spinlock.h> |
| 41 | #include <linux/rcupdate.h> |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 42 | #include <linux/limits.h> |
Paul Gortmaker | b9e15ba | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 43 | #include <linux/export.h> |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 44 | #include <linux/mutex.h> |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 45 | #include <linux/rbtree.h> |
Balbir Singh | b6ac57d | 2008-04-29 01:00:19 -0700 | [diff] [blame] | 46 | #include <linux/slab.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 47 | #include <linux/swap.h> |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 48 | #include <linux/swapops.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 49 | #include <linux/spinlock.h> |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 50 | #include <linux/eventfd.h> |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 51 | #include <linux/poll.h> |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 52 | #include <linux/sort.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 53 | #include <linux/fs.h> |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 54 | #include <linux/seq_file.h> |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 55 | #include <linux/vmpressure.h> |
Christoph Hellwig | dc90f08 | 2022-02-16 15:31:36 +1100 | [diff] [blame] | 56 | #include <linux/memremap.h> |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 57 | #include <linux/mm_inline.h> |
Johannes Weiner | 5d1ea48b | 2014-12-10 15:44:55 -0800 | [diff] [blame] | 58 | #include <linux/swap_cgroup.h> |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 59 | #include <linux/cpu.h> |
KAMEZAWA Hiroyuki | 158e0a2 | 2010-08-10 18:03:00 -0700 | [diff] [blame] | 60 | #include <linux/oom.h> |
Johannes Weiner | 0056f4e | 2013-10-31 16:34:14 -0700 | [diff] [blame] | 61 | #include <linux/lockdep.h> |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 62 | #include <linux/file.h> |
Eric W. Biederman | 03248ad | 2022-02-09 12:20:45 -0600 | [diff] [blame] | 63 | #include <linux/resume_user_mode.h> |
Chris Down | 0e4b01d | 2019-09-23 15:34:55 -0700 | [diff] [blame] | 64 | #include <linux/psi.h> |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 65 | #include <linux/seq_buf.h> |
Michal Hocko | 6a79269 | 2023-03-17 14:44:48 +0100 | [diff] [blame] | 66 | #include <linux/sched/isolation.h> |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 67 | #include "internal.h" |
Glauber Costa | d1a4c0b | 2011-12-11 21:47:04 +0000 | [diff] [blame] | 68 | #include <net/sock.h> |
Michal Hocko | 4bd2c1e | 2012-10-08 16:33:10 -0700 | [diff] [blame] | 69 | #include <net/ip.h> |
Qiang Huang | f35c3a8 | 2013-11-12 15:08:22 -0800 | [diff] [blame] | 70 | #include "slab.h" |
NeilBrown | 014bb1d | 2022-05-09 18:20:47 -0700 | [diff] [blame] | 71 | #include "swap.h" |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 72 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 73 | #include <linux/uaccess.h> |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 74 | |
KOSAKI Motohiro | cc8e970 | 2010-08-09 17:19:57 -0700 | [diff] [blame] | 75 | #include <trace/events/vmscan.h> |
| 76 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 77 | struct cgroup_subsys memory_cgrp_subsys __read_mostly; |
| 78 | EXPORT_SYMBOL(memory_cgrp_subsys); |
David Rientjes | 68ae564 | 2012-12-12 13:51:57 -0800 | [diff] [blame] | 79 | |
Johannes Weiner | 7d82860 | 2016-01-14 15:20:56 -0800 | [diff] [blame] | 80 | struct mem_cgroup *root_mem_cgroup __read_mostly; |
| 81 | |
Roman Gushchin | 37d5985 | 2020-10-17 16:13:50 -0700 | [diff] [blame] | 82 | /* Active memory cgroup to use from an interrupt context */ |
| 83 | DEFINE_PER_CPU(struct mem_cgroup *, int_active_memcg); |
Dan Schatzberg | c74d40e | 2021-06-28 19:38:21 -0700 | [diff] [blame] | 84 | EXPORT_PER_CPU_SYMBOL_GPL(int_active_memcg); |
Roman Gushchin | 37d5985 | 2020-10-17 16:13:50 -0700 | [diff] [blame] | 85 | |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 86 | /* Socket memory accounting disabled? */ |
Roman Gushchin | 0f0cace | 2021-06-02 18:09:29 -0700 | [diff] [blame] | 87 | static bool cgroup_memory_nosocket __ro_after_init; |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 88 | |
Vladimir Davydov | 04823c8 | 2016-01-20 15:02:38 -0800 | [diff] [blame] | 89 | /* Kernel memory accounting disabled? */ |
Muchun Song | 17c1736 | 2022-01-14 14:05:29 -0800 | [diff] [blame] | 90 | static bool cgroup_memory_nokmem __ro_after_init; |
Vladimir Davydov | 04823c8 | 2016-01-20 15:02:38 -0800 | [diff] [blame] | 91 | |
Yafang Shao | b6c1a8a | 2023-02-10 15:47:31 +0000 | [diff] [blame] | 92 | /* BPF memory accounting disabled? */ |
| 93 | static bool cgroup_memory_nobpf __ro_after_init; |
| 94 | |
Tejun Heo | 97b2782 | 2019-08-26 09:06:56 -0700 | [diff] [blame] | 95 | #ifdef CONFIG_CGROUP_WRITEBACK |
| 96 | static DECLARE_WAIT_QUEUE_HEAD(memcg_cgwb_frn_waitq); |
| 97 | #endif |
| 98 | |
Johannes Weiner | 7941d21 | 2016-01-14 15:21:23 -0800 | [diff] [blame] | 99 | /* Whether legacy memory+swap accounting is active */ |
| 100 | static bool do_memsw_account(void) |
| 101 | { |
Johannes Weiner | b25806d | 2022-09-26 09:57:02 -0400 | [diff] [blame] | 102 | return !cgroup_subsys_on_dfl(memory_cgrp_subsys); |
Johannes Weiner | 7941d21 | 2016-01-14 15:21:23 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Kirill A. Shutemov | a0db00f | 2012-05-29 15:06:56 -0700 | [diff] [blame] | 105 | #define THRESHOLDS_EVENTS_TARGET 128 |
| 106 | #define SOFTLIMIT_EVENTS_TARGET 1024 |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 107 | |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 108 | /* |
| 109 | * Cgroups above their limits are maintained in a RB-Tree, independent of |
| 110 | * their hierarchy representation |
| 111 | */ |
| 112 | |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 113 | struct mem_cgroup_tree_per_node { |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 114 | struct rb_root rb_root; |
Davidlohr Bueso | fa90b2f | 2017-09-08 16:15:21 -0700 | [diff] [blame] | 115 | struct rb_node *rb_rightmost; |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 116 | spinlock_t lock; |
| 117 | }; |
| 118 | |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 119 | struct mem_cgroup_tree { |
| 120 | struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES]; |
| 121 | }; |
| 122 | |
| 123 | static struct mem_cgroup_tree soft_limit_tree __read_mostly; |
| 124 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 125 | /* for OOM */ |
| 126 | struct mem_cgroup_eventfd_list { |
| 127 | struct list_head list; |
| 128 | struct eventfd_ctx *eventfd; |
| 129 | }; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 130 | |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 131 | /* |
| 132 | * cgroup_event represents events which userspace want to receive. |
| 133 | */ |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 134 | struct mem_cgroup_event { |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 135 | /* |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 136 | * memcg which the event belongs to. |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 137 | */ |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 138 | struct mem_cgroup *memcg; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 139 | /* |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 140 | * eventfd to signal userspace about the event. |
| 141 | */ |
| 142 | struct eventfd_ctx *eventfd; |
| 143 | /* |
| 144 | * Each of these stored in a list by the cgroup. |
| 145 | */ |
| 146 | struct list_head list; |
| 147 | /* |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 148 | * register_event() callback will be used to add new userspace |
| 149 | * waiter for changes related to this event. Use eventfd_signal() |
| 150 | * on eventfd to send notification to userspace. |
| 151 | */ |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 152 | int (*register_event)(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 153 | struct eventfd_ctx *eventfd, const char *args); |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 154 | /* |
| 155 | * unregister_event() callback will be called when userspace closes |
| 156 | * the eventfd or on cgroup removing. This callback must be set, |
| 157 | * if you want provide notification functionality. |
| 158 | */ |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 159 | void (*unregister_event)(struct mem_cgroup *memcg, |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 160 | struct eventfd_ctx *eventfd); |
| 161 | /* |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 162 | * All fields below needed to unregister event when |
| 163 | * userspace closes eventfd. |
| 164 | */ |
| 165 | poll_table pt; |
| 166 | wait_queue_head_t *wqh; |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 167 | wait_queue_entry_t wait; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 168 | struct work_struct remove; |
| 169 | }; |
| 170 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 171 | static void mem_cgroup_threshold(struct mem_cgroup *memcg); |
| 172 | static void mem_cgroup_oom_notify(struct mem_cgroup *memcg); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 173 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 174 | /* Stuffs for move charges at task migration. */ |
| 175 | /* |
Johannes Weiner | 1dfab5a | 2015-02-11 15:26:09 -0800 | [diff] [blame] | 176 | * Types of charges to be moved. |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 177 | */ |
Johannes Weiner | 1dfab5a | 2015-02-11 15:26:09 -0800 | [diff] [blame] | 178 | #define MOVE_ANON 0x1U |
| 179 | #define MOVE_FILE 0x2U |
| 180 | #define MOVE_MASK (MOVE_ANON | MOVE_FILE) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 181 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 182 | /* "mc" and its members are protected by cgroup_mutex */ |
| 183 | static struct move_charge_struct { |
Daisuke Nishimura | b1dd693 | 2010-11-24 12:57:06 -0800 | [diff] [blame] | 184 | spinlock_t lock; /* for from, to */ |
Tejun Heo | 264a0ae | 2016-04-21 19:09:02 -0400 | [diff] [blame] | 185 | struct mm_struct *mm; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 186 | struct mem_cgroup *from; |
| 187 | struct mem_cgroup *to; |
Johannes Weiner | 1dfab5a | 2015-02-11 15:26:09 -0800 | [diff] [blame] | 188 | unsigned long flags; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 189 | unsigned long precharge; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 190 | unsigned long moved_charge; |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 191 | unsigned long moved_swap; |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 192 | struct task_struct *moving_task; /* a task moving charges */ |
| 193 | wait_queue_head_t waitq; /* a waitq for other context */ |
| 194 | } mc = { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 195 | .lock = __SPIN_LOCK_UNLOCKED(mc.lock), |
Daisuke Nishimura | 8033b97 | 2010-03-10 15:22:16 -0800 | [diff] [blame] | 196 | .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq), |
| 197 | }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 198 | |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 199 | /* |
Miaohe Lin | f4d005af | 2023-07-13 20:14:32 +0800 | [diff] [blame] | 200 | * Maximum loops in mem_cgroup_soft_reclaim(), used for soft |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 201 | * limit reclaim to prevent infinite loops, if they ever occur. |
| 202 | */ |
Kirill A. Shutemov | a0db00f | 2012-05-29 15:06:56 -0700 | [diff] [blame] | 203 | #define MEM_CGROUP_MAX_RECLAIM_LOOPS 100 |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 204 | #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2 |
Balbir Singh | 4e41695 | 2009-09-23 15:56:39 -0700 | [diff] [blame] | 205 | |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 206 | /* for encoding cft->private value on file */ |
Glauber Costa | 86ae53e | 2012-12-18 14:21:45 -0800 | [diff] [blame] | 207 | enum res_type { |
| 208 | _MEM, |
| 209 | _MEMSWAP, |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 210 | _KMEM, |
Vladimir Davydov | d55f90b | 2016-01-20 15:02:44 -0800 | [diff] [blame] | 211 | _TCP, |
Glauber Costa | 86ae53e | 2012-12-18 14:21:45 -0800 | [diff] [blame] | 212 | }; |
| 213 | |
Kirill A. Shutemov | a0db00f | 2012-05-29 15:06:56 -0700 | [diff] [blame] | 214 | #define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val)) |
| 215 | #define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff) |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 216 | #define MEMFILE_ATTR(val) ((val) & 0xffff) |
| 217 | |
Kirill Tkhai | b05706f | 2018-08-17 15:47:33 -0700 | [diff] [blame] | 218 | /* |
| 219 | * Iteration constructs for visiting all cgroups (under a tree). If |
| 220 | * loops are exited prematurely (break), mem_cgroup_iter_break() must |
| 221 | * be used for reference counting. |
| 222 | */ |
| 223 | #define for_each_mem_cgroup_tree(iter, root) \ |
| 224 | for (iter = mem_cgroup_iter(root, NULL, NULL); \ |
| 225 | iter != NULL; \ |
| 226 | iter = mem_cgroup_iter(root, iter, NULL)) |
| 227 | |
| 228 | #define for_each_mem_cgroup(iter) \ |
| 229 | for (iter = mem_cgroup_iter(NULL, NULL, NULL); \ |
| 230 | iter != NULL; \ |
| 231 | iter = mem_cgroup_iter(NULL, iter, NULL)) |
| 232 | |
Vasily Averin | a4ebf1b | 2021-11-05 13:38:09 -0700 | [diff] [blame] | 233 | static inline bool task_is_dying(void) |
Tetsuo Handa | 7775fac | 2019-03-05 15:46:47 -0800 | [diff] [blame] | 234 | { |
| 235 | return tsk_is_oom_victim(current) || fatal_signal_pending(current) || |
| 236 | (current->flags & PF_EXITING); |
| 237 | } |
| 238 | |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 239 | /* Some nice accessors for the vmpressure. */ |
| 240 | struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg) |
| 241 | { |
| 242 | if (!memcg) |
| 243 | memcg = root_mem_cgroup; |
| 244 | return &memcg->vmpressure; |
| 245 | } |
| 246 | |
Hui Su | 9647875 | 2021-09-02 14:59:36 -0700 | [diff] [blame] | 247 | struct mem_cgroup *vmpressure_to_memcg(struct vmpressure *vmpr) |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 248 | { |
Hui Su | 9647875 | 2021-09-02 14:59:36 -0700 | [diff] [blame] | 249 | return container_of(vmpr, struct mem_cgroup, vmpressure); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Roman Gushchin | 1aacbd3 | 2023-10-19 15:53:42 -0700 | [diff] [blame] | 252 | #define CURRENT_OBJCG_UPDATE_BIT 0 |
| 253 | #define CURRENT_OBJCG_UPDATE_FLAG (1UL << CURRENT_OBJCG_UPDATE_BIT) |
| 254 | |
Kirill Tkhai | 84c07d1 | 2018-08-17 15:47:25 -0700 | [diff] [blame] | 255 | #ifdef CONFIG_MEMCG_KMEM |
Roman Gushchin | 0764db9 | 2022-02-11 16:32:32 -0800 | [diff] [blame] | 256 | static DEFINE_SPINLOCK(objcg_lock); |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 257 | |
Roman Gushchin | 4d5c8ae | 2021-06-02 18:09:30 -0700 | [diff] [blame] | 258 | bool mem_cgroup_kmem_disabled(void) |
| 259 | { |
| 260 | return cgroup_memory_nokmem; |
| 261 | } |
| 262 | |
Muchun Song | f1286fa | 2021-04-29 22:56:55 -0700 | [diff] [blame] | 263 | static void obj_cgroup_uncharge_pages(struct obj_cgroup *objcg, |
| 264 | unsigned int nr_pages); |
Roman Gushchin | c1a660d | 2021-02-24 12:03:58 -0800 | [diff] [blame] | 265 | |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 266 | static void obj_cgroup_release(struct percpu_ref *ref) |
| 267 | { |
| 268 | struct obj_cgroup *objcg = container_of(ref, struct obj_cgroup, refcnt); |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 269 | unsigned int nr_bytes; |
| 270 | unsigned int nr_pages; |
| 271 | unsigned long flags; |
| 272 | |
| 273 | /* |
| 274 | * At this point all allocated objects are freed, and |
| 275 | * objcg->nr_charged_bytes can't have an arbitrary byte value. |
| 276 | * However, it can be PAGE_SIZE or (x * PAGE_SIZE). |
| 277 | * |
| 278 | * The following sequence can lead to it: |
| 279 | * 1) CPU0: objcg == stock->cached_objcg |
| 280 | * 2) CPU1: we do a small allocation (e.g. 92 bytes), |
| 281 | * PAGE_SIZE bytes are charged |
| 282 | * 3) CPU1: a process from another memcg is allocating something, |
| 283 | * the stock if flushed, |
| 284 | * objcg->nr_charged_bytes = PAGE_SIZE - 92 |
| 285 | * 5) CPU0: we do release this object, |
| 286 | * 92 bytes are added to stock->nr_bytes |
| 287 | * 6) CPU0: stock is flushed, |
| 288 | * 92 bytes are added to objcg->nr_charged_bytes |
| 289 | * |
| 290 | * In the result, nr_charged_bytes == PAGE_SIZE. |
| 291 | * This page will be uncharged in obj_cgroup_release(). |
| 292 | */ |
| 293 | nr_bytes = atomic_read(&objcg->nr_charged_bytes); |
| 294 | WARN_ON_ONCE(nr_bytes & (PAGE_SIZE - 1)); |
| 295 | nr_pages = nr_bytes >> PAGE_SHIFT; |
| 296 | |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 297 | if (nr_pages) |
Muchun Song | f1286fa | 2021-04-29 22:56:55 -0700 | [diff] [blame] | 298 | obj_cgroup_uncharge_pages(objcg, nr_pages); |
Muchun Song | 271dd6b | 2021-06-28 19:38:06 -0700 | [diff] [blame] | 299 | |
Roman Gushchin | 0764db9 | 2022-02-11 16:32:32 -0800 | [diff] [blame] | 300 | spin_lock_irqsave(&objcg_lock, flags); |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 301 | list_del(&objcg->list); |
Roman Gushchin | 0764db9 | 2022-02-11 16:32:32 -0800 | [diff] [blame] | 302 | spin_unlock_irqrestore(&objcg_lock, flags); |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 303 | |
| 304 | percpu_ref_exit(ref); |
| 305 | kfree_rcu(objcg, rcu); |
| 306 | } |
| 307 | |
| 308 | static struct obj_cgroup *obj_cgroup_alloc(void) |
| 309 | { |
| 310 | struct obj_cgroup *objcg; |
| 311 | int ret; |
| 312 | |
| 313 | objcg = kzalloc(sizeof(struct obj_cgroup), GFP_KERNEL); |
| 314 | if (!objcg) |
| 315 | return NULL; |
| 316 | |
| 317 | ret = percpu_ref_init(&objcg->refcnt, obj_cgroup_release, 0, |
| 318 | GFP_KERNEL); |
| 319 | if (ret) { |
| 320 | kfree(objcg); |
| 321 | return NULL; |
| 322 | } |
| 323 | INIT_LIST_HEAD(&objcg->list); |
| 324 | return objcg; |
| 325 | } |
| 326 | |
| 327 | static void memcg_reparent_objcgs(struct mem_cgroup *memcg, |
| 328 | struct mem_cgroup *parent) |
| 329 | { |
| 330 | struct obj_cgroup *objcg, *iter; |
| 331 | |
| 332 | objcg = rcu_replace_pointer(memcg->objcg, NULL, true); |
| 333 | |
Roman Gushchin | 0764db9 | 2022-02-11 16:32:32 -0800 | [diff] [blame] | 334 | spin_lock_irq(&objcg_lock); |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 335 | |
Muchun Song | 9838354 | 2021-06-28 19:38:03 -0700 | [diff] [blame] | 336 | /* 1) Ready to reparent active objcg. */ |
| 337 | list_add(&objcg->list, &memcg->objcg_list); |
| 338 | /* 2) Reparent active objcg and already reparented objcgs to parent. */ |
| 339 | list_for_each_entry(iter, &memcg->objcg_list, list) |
| 340 | WRITE_ONCE(iter->memcg, parent); |
| 341 | /* 3) Move already reparented objcgs to the parent's list */ |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 342 | list_splice(&memcg->objcg_list, &parent->objcg_list); |
| 343 | |
Roman Gushchin | 0764db9 | 2022-02-11 16:32:32 -0800 | [diff] [blame] | 344 | spin_unlock_irq(&objcg_lock); |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 345 | |
| 346 | percpu_ref_kill(&objcg->refcnt); |
| 347 | } |
| 348 | |
Glauber Costa | 55007d8 | 2012-12-18 14:22:38 -0800 | [diff] [blame] | 349 | /* |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 350 | * A lot of the calls to the cache allocation functions are expected to be |
Roman Gushchin | 272911a | 2020-08-06 23:21:17 -0700 | [diff] [blame] | 351 | * inlined by the compiler. Since the calls to memcg_slab_pre_alloc_hook() are |
Glauber Costa | d7f25f8 | 2012-12-18 14:22:40 -0800 | [diff] [blame] | 352 | * conditional to this static branch, we'll have to allow modules that does |
| 353 | * kmem_cache_alloc and the such to see this symbol as well |
| 354 | */ |
Roman Gushchin | f7a449f | 2023-02-13 11:29:22 -0800 | [diff] [blame] | 355 | DEFINE_STATIC_KEY_FALSE(memcg_kmem_online_key); |
| 356 | EXPORT_SYMBOL(memcg_kmem_online_key); |
Yafang Shao | b6c1a8a | 2023-02-10 15:47:31 +0000 | [diff] [blame] | 357 | |
| 358 | DEFINE_STATIC_KEY_FALSE(memcg_bpf_enabled_key); |
| 359 | EXPORT_SYMBOL(memcg_bpf_enabled_key); |
Yang Shi | 0a432dcb | 2019-09-23 15:38:12 -0700 | [diff] [blame] | 360 | #endif |
Tejun Heo | 17cc4df | 2017-02-22 15:41:36 -0800 | [diff] [blame] | 361 | |
Tejun Heo | ad7fa85 | 2015-05-27 20:00:02 -0400 | [diff] [blame] | 362 | /** |
Matthew Wilcox (Oracle) | 75376c6 | 2023-01-16 19:25:07 +0000 | [diff] [blame] | 363 | * mem_cgroup_css_from_folio - css of the memcg associated with a folio |
| 364 | * @folio: folio of interest |
Tejun Heo | ad7fa85 | 2015-05-27 20:00:02 -0400 | [diff] [blame] | 365 | * |
| 366 | * If memcg is bound to the default hierarchy, css of the memcg associated |
Matthew Wilcox (Oracle) | 75376c6 | 2023-01-16 19:25:07 +0000 | [diff] [blame] | 367 | * with @folio is returned. The returned css remains associated with @folio |
Tejun Heo | ad7fa85 | 2015-05-27 20:00:02 -0400 | [diff] [blame] | 368 | * until it is released. |
| 369 | * |
| 370 | * If memcg is bound to a traditional hierarchy, the css of root_mem_cgroup |
| 371 | * is returned. |
Tejun Heo | ad7fa85 | 2015-05-27 20:00:02 -0400 | [diff] [blame] | 372 | */ |
Matthew Wilcox (Oracle) | 75376c6 | 2023-01-16 19:25:07 +0000 | [diff] [blame] | 373 | struct cgroup_subsys_state *mem_cgroup_css_from_folio(struct folio *folio) |
Tejun Heo | ad7fa85 | 2015-05-27 20:00:02 -0400 | [diff] [blame] | 374 | { |
Matthew Wilcox (Oracle) | 75376c6 | 2023-01-16 19:25:07 +0000 | [diff] [blame] | 375 | struct mem_cgroup *memcg = folio_memcg(folio); |
Tejun Heo | ad7fa85 | 2015-05-27 20:00:02 -0400 | [diff] [blame] | 376 | |
Tejun Heo | 9e10a13 | 2015-09-18 11:56:28 -0400 | [diff] [blame] | 377 | if (!memcg || !cgroup_subsys_on_dfl(memory_cgrp_subsys)) |
Tejun Heo | ad7fa85 | 2015-05-27 20:00:02 -0400 | [diff] [blame] | 378 | memcg = root_mem_cgroup; |
| 379 | |
Tejun Heo | ad7fa85 | 2015-05-27 20:00:02 -0400 | [diff] [blame] | 380 | return &memcg->css; |
| 381 | } |
| 382 | |
Vladimir Davydov | 2fc0452 | 2015-09-09 15:35:28 -0700 | [diff] [blame] | 383 | /** |
| 384 | * page_cgroup_ino - return inode number of the memcg a page is charged to |
| 385 | * @page: the page |
| 386 | * |
| 387 | * Look up the closest online ancestor of the memory cgroup @page is charged to |
| 388 | * and return its inode number or 0 if @page is not charged to any cgroup. It |
| 389 | * is safe to call this function without holding a reference to @page. |
| 390 | * |
| 391 | * Note, this function is inherently racy, because there is nothing to prevent |
| 392 | * the cgroup inode from getting torn down and potentially reallocated a moment |
| 393 | * after page_cgroup_ino() returns, so it only should be used by callers that |
| 394 | * do not care (such as procfs interfaces). |
| 395 | */ |
| 396 | ino_t page_cgroup_ino(struct page *page) |
| 397 | { |
| 398 | struct mem_cgroup *memcg; |
| 399 | unsigned long ino = 0; |
| 400 | |
| 401 | rcu_read_lock(); |
Yosry Ahmed | ec34260 | 2023-04-12 00:34:51 +0000 | [diff] [blame] | 402 | /* page_folio() is racy here, but the entire function is racy anyway */ |
| 403 | memcg = folio_memcg_check(page_folio(page)); |
Roman Gushchin | 286e04b | 2020-08-06 23:20:52 -0700 | [diff] [blame] | 404 | |
Vladimir Davydov | 2fc0452 | 2015-09-09 15:35:28 -0700 | [diff] [blame] | 405 | while (memcg && !(memcg->css.flags & CSS_ONLINE)) |
| 406 | memcg = parent_mem_cgroup(memcg); |
| 407 | if (memcg) |
| 408 | ino = cgroup_ino(memcg->css.cgroup); |
| 409 | rcu_read_unlock(); |
| 410 | return ino; |
| 411 | } |
| 412 | |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 413 | static void __mem_cgroup_insert_exceeded(struct mem_cgroup_per_node *mz, |
| 414 | struct mem_cgroup_tree_per_node *mctz, |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 415 | unsigned long new_usage_in_excess) |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 416 | { |
| 417 | struct rb_node **p = &mctz->rb_root.rb_node; |
| 418 | struct rb_node *parent = NULL; |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 419 | struct mem_cgroup_per_node *mz_node; |
Davidlohr Bueso | fa90b2f | 2017-09-08 16:15:21 -0700 | [diff] [blame] | 420 | bool rightmost = true; |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 421 | |
| 422 | if (mz->on_tree) |
| 423 | return; |
| 424 | |
| 425 | mz->usage_in_excess = new_usage_in_excess; |
| 426 | if (!mz->usage_in_excess) |
| 427 | return; |
| 428 | while (*p) { |
| 429 | parent = *p; |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 430 | mz_node = rb_entry(parent, struct mem_cgroup_per_node, |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 431 | tree_node); |
Davidlohr Bueso | fa90b2f | 2017-09-08 16:15:21 -0700 | [diff] [blame] | 432 | if (mz->usage_in_excess < mz_node->usage_in_excess) { |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 433 | p = &(*p)->rb_left; |
Davidlohr Bueso | fa90b2f | 2017-09-08 16:15:21 -0700 | [diff] [blame] | 434 | rightmost = false; |
Miaohe Lin | 378876b0 | 2020-12-14 19:06:28 -0800 | [diff] [blame] | 435 | } else { |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 436 | p = &(*p)->rb_right; |
Miaohe Lin | 378876b0 | 2020-12-14 19:06:28 -0800 | [diff] [blame] | 437 | } |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 438 | } |
Davidlohr Bueso | fa90b2f | 2017-09-08 16:15:21 -0700 | [diff] [blame] | 439 | |
| 440 | if (rightmost) |
| 441 | mctz->rb_rightmost = &mz->tree_node; |
| 442 | |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 443 | rb_link_node(&mz->tree_node, parent, p); |
| 444 | rb_insert_color(&mz->tree_node, &mctz->rb_root); |
| 445 | mz->on_tree = true; |
| 446 | } |
| 447 | |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 448 | static void __mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz, |
| 449 | struct mem_cgroup_tree_per_node *mctz) |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 450 | { |
| 451 | if (!mz->on_tree) |
| 452 | return; |
Davidlohr Bueso | fa90b2f | 2017-09-08 16:15:21 -0700 | [diff] [blame] | 453 | |
| 454 | if (&mz->tree_node == mctz->rb_rightmost) |
| 455 | mctz->rb_rightmost = rb_prev(&mz->tree_node); |
| 456 | |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 457 | rb_erase(&mz->tree_node, &mctz->rb_root); |
| 458 | mz->on_tree = false; |
| 459 | } |
| 460 | |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 461 | static void mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz, |
| 462 | struct mem_cgroup_tree_per_node *mctz) |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 463 | { |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 464 | unsigned long flags; |
| 465 | |
| 466 | spin_lock_irqsave(&mctz->lock, flags); |
Johannes Weiner | cf2c812 | 2014-06-06 14:38:21 -0700 | [diff] [blame] | 467 | __mem_cgroup_remove_exceeded(mz, mctz); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 468 | spin_unlock_irqrestore(&mctz->lock, flags); |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 471 | static unsigned long soft_limit_excess(struct mem_cgroup *memcg) |
| 472 | { |
| 473 | unsigned long nr_pages = page_counter_read(&memcg->memory); |
Jason Low | 4db0c3c | 2015-04-15 16:14:08 -0700 | [diff] [blame] | 474 | unsigned long soft_limit = READ_ONCE(memcg->soft_limit); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 475 | unsigned long excess = 0; |
| 476 | |
| 477 | if (nr_pages > soft_limit) |
| 478 | excess = nr_pages - soft_limit; |
| 479 | |
| 480 | return excess; |
| 481 | } |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 482 | |
Matthew Wilcox (Oracle) | 658b69c | 2021-04-29 14:51:26 -0400 | [diff] [blame] | 483 | static void mem_cgroup_update_tree(struct mem_cgroup *memcg, int nid) |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 484 | { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 485 | unsigned long excess; |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 486 | struct mem_cgroup_per_node *mz; |
| 487 | struct mem_cgroup_tree_per_node *mctz; |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 488 | |
Yu Zhao | e4dde56 | 2022-12-21 21:19:04 -0700 | [diff] [blame] | 489 | if (lru_gen_enabled()) { |
T.J. Alumbaugh | 36c7b4d | 2023-01-18 00:18:24 +0000 | [diff] [blame] | 490 | if (soft_limit_excess(memcg)) |
T.J. Alumbaugh | 5c7e7a0 | 2023-05-22 11:20:56 +0000 | [diff] [blame] | 491 | lru_gen_soft_reclaim(memcg, nid); |
Yu Zhao | e4dde56 | 2022-12-21 21:19:04 -0700 | [diff] [blame] | 492 | return; |
| 493 | } |
| 494 | |
Matthew Wilcox (Oracle) | 2ab082b | 2021-06-25 08:57:02 -0400 | [diff] [blame] | 495 | mctz = soft_limit_tree.rb_tree_per_node[nid]; |
Laurent Dufour | bfc7228 | 2017-03-09 16:17:06 -0800 | [diff] [blame] | 496 | if (!mctz) |
| 497 | return; |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 498 | /* |
| 499 | * Necessary to update all ancestors when hierarchy is used. |
| 500 | * because their event counter is not touched. |
| 501 | */ |
| 502 | for (; memcg; memcg = parent_mem_cgroup(memcg)) { |
Matthew Wilcox (Oracle) | 658b69c | 2021-04-29 14:51:26 -0400 | [diff] [blame] | 503 | mz = memcg->nodeinfo[nid]; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 504 | excess = soft_limit_excess(memcg); |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 505 | /* |
| 506 | * We have to update the tree if mz is on RB-tree or |
| 507 | * mem is over its softlimit. |
| 508 | */ |
| 509 | if (excess || mz->on_tree) { |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 510 | unsigned long flags; |
| 511 | |
| 512 | spin_lock_irqsave(&mctz->lock, flags); |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 513 | /* if on-tree, remove it */ |
| 514 | if (mz->on_tree) |
Johannes Weiner | cf2c812 | 2014-06-06 14:38:21 -0700 | [diff] [blame] | 515 | __mem_cgroup_remove_exceeded(mz, mctz); |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 516 | /* |
| 517 | * Insert again. mz->usage_in_excess will be updated. |
| 518 | * If excess is 0, no tree ops. |
| 519 | */ |
Johannes Weiner | cf2c812 | 2014-06-06 14:38:21 -0700 | [diff] [blame] | 520 | __mem_cgroup_insert_exceeded(mz, mctz, excess); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 521 | spin_unlock_irqrestore(&mctz->lock, flags); |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg) |
| 527 | { |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 528 | struct mem_cgroup_tree_per_node *mctz; |
| 529 | struct mem_cgroup_per_node *mz; |
| 530 | int nid; |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 531 | |
Jianyu Zhan | e231875 | 2014-06-06 14:38:20 -0700 | [diff] [blame] | 532 | for_each_node(nid) { |
Johannes Weiner | a3747b5 | 2021-04-29 22:56:14 -0700 | [diff] [blame] | 533 | mz = memcg->nodeinfo[nid]; |
Matthew Wilcox (Oracle) | 2ab082b | 2021-06-25 08:57:02 -0400 | [diff] [blame] | 534 | mctz = soft_limit_tree.rb_tree_per_node[nid]; |
Laurent Dufour | bfc7228 | 2017-03-09 16:17:06 -0800 | [diff] [blame] | 535 | if (mctz) |
| 536 | mem_cgroup_remove_exceeded(mz, mctz); |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 540 | static struct mem_cgroup_per_node * |
| 541 | __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz) |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 542 | { |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 543 | struct mem_cgroup_per_node *mz; |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 544 | |
| 545 | retry: |
| 546 | mz = NULL; |
Davidlohr Bueso | fa90b2f | 2017-09-08 16:15:21 -0700 | [diff] [blame] | 547 | if (!mctz->rb_rightmost) |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 548 | goto done; /* Nothing to reclaim from */ |
| 549 | |
Davidlohr Bueso | fa90b2f | 2017-09-08 16:15:21 -0700 | [diff] [blame] | 550 | mz = rb_entry(mctz->rb_rightmost, |
| 551 | struct mem_cgroup_per_node, tree_node); |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 552 | /* |
| 553 | * Remove the node now but someone else can add it back, |
| 554 | * we will to add it back at the end of reclaim to its correct |
| 555 | * position in the tree. |
| 556 | */ |
Johannes Weiner | cf2c812 | 2014-06-06 14:38:21 -0700 | [diff] [blame] | 557 | __mem_cgroup_remove_exceeded(mz, mctz); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 558 | if (!soft_limit_excess(mz->memcg) || |
Shakeel Butt | 8965aa2 | 2020-04-01 21:07:10 -0700 | [diff] [blame] | 559 | !css_tryget(&mz->memcg->css)) |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 560 | goto retry; |
| 561 | done: |
| 562 | return mz; |
| 563 | } |
| 564 | |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 565 | static struct mem_cgroup_per_node * |
| 566 | mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz) |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 567 | { |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 568 | struct mem_cgroup_per_node *mz; |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 569 | |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 570 | spin_lock_irq(&mctz->lock); |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 571 | mz = __mem_cgroup_largest_soft_limit_node(mctz); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 572 | spin_unlock_irq(&mctz->lock); |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 573 | return mz; |
| 574 | } |
| 575 | |
Shakeel Butt | 11192d9 | 2021-11-05 13:37:31 -0700 | [diff] [blame] | 576 | /* |
| 577 | * memcg and lruvec stats flushing |
| 578 | * |
| 579 | * Many codepaths leading to stats update or read are performance sensitive and |
| 580 | * adding stats flushing in such codepaths is not desirable. So, to optimize the |
| 581 | * flushing the kernel does: |
| 582 | * |
| 583 | * 1) Periodically and asynchronously flush the stats every 2 seconds to not let |
| 584 | * rstat update tree grow unbounded. |
| 585 | * |
| 586 | * 2) Flush the stats synchronously on reader side only when there are more than |
| 587 | * (MEMCG_CHARGE_BATCH * nr_cpus) update events. Though this optimization |
| 588 | * will let stats be out of sync by atmost (MEMCG_CHARGE_BATCH * nr_cpus) but |
| 589 | * only for 2 seconds due to (1). |
| 590 | */ |
| 591 | static void flush_memcg_stats_dwork(struct work_struct *w); |
| 592 | static DECLARE_DEFERRABLE_WORK(stats_flush_dwork, flush_memcg_stats_dwork); |
Shakeel Butt | 11192d9 | 2021-11-05 13:37:31 -0700 | [diff] [blame] | 593 | static DEFINE_PER_CPU(unsigned int, stats_updates); |
Yosry Ahmed | 3cd9992 | 2023-03-30 19:17:57 +0000 | [diff] [blame] | 594 | static atomic_t stats_flush_ongoing = ATOMIC_INIT(0); |
Shakeel Butt | 11192d9 | 2021-11-05 13:37:31 -0700 | [diff] [blame] | 595 | static atomic_t stats_flush_threshold = ATOMIC_INIT(0); |
Shakeel Butt | 9b30161 | 2022-04-21 16:35:40 -0700 | [diff] [blame] | 596 | static u64 flush_next_time; |
| 597 | |
| 598 | #define FLUSH_TIME (2UL*HZ) |
Shakeel Butt | 11192d9 | 2021-11-05 13:37:31 -0700 | [diff] [blame] | 599 | |
Sebastian Andrzej Siewior | be3e67b | 2022-03-22 14:40:41 -0700 | [diff] [blame] | 600 | /* |
| 601 | * Accessors to ensure that preemption is disabled on PREEMPT_RT because it can |
| 602 | * not rely on this as part of an acquired spinlock_t lock. These functions are |
| 603 | * never used in hardirq context on PREEMPT_RT and therefore disabling preemtion |
| 604 | * is sufficient. |
| 605 | */ |
| 606 | static void memcg_stats_lock(void) |
| 607 | { |
Thomas Gleixner | e575d40 | 2022-08-25 18:41:28 +0200 | [diff] [blame] | 608 | preempt_disable_nested(); |
| 609 | VM_WARN_ON_IRQS_ENABLED(); |
Sebastian Andrzej Siewior | be3e67b | 2022-03-22 14:40:41 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | static void __memcg_stats_lock(void) |
| 613 | { |
Thomas Gleixner | e575d40 | 2022-08-25 18:41:28 +0200 | [diff] [blame] | 614 | preempt_disable_nested(); |
Sebastian Andrzej Siewior | be3e67b | 2022-03-22 14:40:41 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | static void memcg_stats_unlock(void) |
| 618 | { |
Thomas Gleixner | e575d40 | 2022-08-25 18:41:28 +0200 | [diff] [blame] | 619 | preempt_enable_nested(); |
Sebastian Andrzej Siewior | be3e67b | 2022-03-22 14:40:41 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Shakeel Butt | 5b3be69 | 2022-01-14 14:05:39 -0800 | [diff] [blame] | 622 | static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val) |
Shakeel Butt | 11192d9 | 2021-11-05 13:37:31 -0700 | [diff] [blame] | 623 | { |
Shakeel Butt | 5b3be69 | 2022-01-14 14:05:39 -0800 | [diff] [blame] | 624 | unsigned int x; |
| 625 | |
Yosry Ahmed | f9d911c | 2023-03-30 19:18:01 +0000 | [diff] [blame] | 626 | if (!val) |
| 627 | return; |
| 628 | |
Shakeel Butt | 11192d9 | 2021-11-05 13:37:31 -0700 | [diff] [blame] | 629 | cgroup_rstat_updated(memcg->css.cgroup, smp_processor_id()); |
Shakeel Butt | 5b3be69 | 2022-01-14 14:05:39 -0800 | [diff] [blame] | 630 | |
| 631 | x = __this_cpu_add_return(stats_updates, abs(val)); |
| 632 | if (x > MEMCG_CHARGE_BATCH) { |
Jiebin Sun | 873f64b | 2022-07-23 00:49:49 +0800 | [diff] [blame] | 633 | /* |
| 634 | * If stats_flush_threshold exceeds the threshold |
| 635 | * (>num_online_cpus()), cgroup stats update will be triggered |
| 636 | * in __mem_cgroup_flush_stats(). Increasing this var further |
| 637 | * is redundant and simply adds overhead in atomic update. |
| 638 | */ |
| 639 | if (atomic_read(&stats_flush_threshold) <= num_online_cpus()) |
| 640 | atomic_add(x / MEMCG_CHARGE_BATCH, &stats_flush_threshold); |
Shakeel Butt | 5b3be69 | 2022-01-14 14:05:39 -0800 | [diff] [blame] | 641 | __this_cpu_write(stats_updates, 0); |
| 642 | } |
Shakeel Butt | 11192d9 | 2021-11-05 13:37:31 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Yosry Ahmed | 35822fd | 2023-04-21 17:40:19 +0000 | [diff] [blame] | 645 | static void do_flush_stats(void) |
Shakeel Butt | 11192d9 | 2021-11-05 13:37:31 -0700 | [diff] [blame] | 646 | { |
Yosry Ahmed | 3cd9992 | 2023-03-30 19:17:57 +0000 | [diff] [blame] | 647 | /* |
| 648 | * We always flush the entire tree, so concurrent flushers can just |
| 649 | * skip. This avoids a thundering herd problem on the rstat global lock |
| 650 | * from memcg flushers (e.g. reclaim, refault, etc). |
| 651 | */ |
| 652 | if (atomic_read(&stats_flush_ongoing) || |
| 653 | atomic_xchg(&stats_flush_ongoing, 1)) |
Shakeel Butt | 11192d9 | 2021-11-05 13:37:31 -0700 | [diff] [blame] | 654 | return; |
| 655 | |
Yosry Ahmed | 3cd9992 | 2023-03-30 19:17:57 +0000 | [diff] [blame] | 656 | WRITE_ONCE(flush_next_time, jiffies_64 + 2*FLUSH_TIME); |
Yosry Ahmed | 9fad9ae | 2023-03-30 19:17:58 +0000 | [diff] [blame] | 657 | |
Yosry Ahmed | 35822fd | 2023-04-21 17:40:19 +0000 | [diff] [blame] | 658 | cgroup_rstat_flush(root_mem_cgroup->css.cgroup); |
Yosry Ahmed | 9fad9ae | 2023-03-30 19:17:58 +0000 | [diff] [blame] | 659 | |
Shakeel Butt | 11192d9 | 2021-11-05 13:37:31 -0700 | [diff] [blame] | 660 | atomic_set(&stats_flush_threshold, 0); |
Yosry Ahmed | 3cd9992 | 2023-03-30 19:17:57 +0000 | [diff] [blame] | 661 | atomic_set(&stats_flush_ongoing, 0); |
Shakeel Butt | 11192d9 | 2021-11-05 13:37:31 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Yosry Ahmed | 9fad9ae | 2023-03-30 19:17:58 +0000 | [diff] [blame] | 664 | void mem_cgroup_flush_stats(void) |
| 665 | { |
Yosry Ahmed | 35822fd | 2023-04-21 17:40:19 +0000 | [diff] [blame] | 666 | if (atomic_read(&stats_flush_threshold) > num_online_cpus()) |
| 667 | do_flush_stats(); |
Yosry Ahmed | 9fad9ae | 2023-03-30 19:17:58 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Yosry Ahmed | 4009b2f | 2023-03-30 19:17:59 +0000 | [diff] [blame] | 670 | void mem_cgroup_flush_stats_ratelimited(void) |
Shakeel Butt | 9b30161 | 2022-04-21 16:35:40 -0700 | [diff] [blame] | 671 | { |
Yosry Ahmed | 3cd9992 | 2023-03-30 19:17:57 +0000 | [diff] [blame] | 672 | if (time_after64(jiffies_64, READ_ONCE(flush_next_time))) |
Yosry Ahmed | 4009b2f | 2023-03-30 19:17:59 +0000 | [diff] [blame] | 673 | mem_cgroup_flush_stats(); |
Shakeel Butt | 9b30161 | 2022-04-21 16:35:40 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Shakeel Butt | 11192d9 | 2021-11-05 13:37:31 -0700 | [diff] [blame] | 676 | static void flush_memcg_stats_dwork(struct work_struct *w) |
| 677 | { |
Yosry Ahmed | 9fad9ae | 2023-03-30 19:17:58 +0000 | [diff] [blame] | 678 | /* |
| 679 | * Always flush here so that flushing in latency-sensitive paths is |
| 680 | * as cheap as possible. |
| 681 | */ |
Yosry Ahmed | 35822fd | 2023-04-21 17:40:19 +0000 | [diff] [blame] | 682 | do_flush_stats(); |
Shakeel Butt | 9b30161 | 2022-04-21 16:35:40 -0700 | [diff] [blame] | 683 | queue_delayed_work(system_unbound_wq, &stats_flush_dwork, FLUSH_TIME); |
Shakeel Butt | 11192d9 | 2021-11-05 13:37:31 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Shakeel Butt | d396def | 2022-09-07 04:35:36 +0000 | [diff] [blame] | 686 | /* Subset of vm_event_item to report for memcg event stats */ |
| 687 | static const unsigned int memcg_vm_event_stat[] = { |
Shakeel Butt | 8278f1c | 2022-09-07 04:35:37 +0000 | [diff] [blame] | 688 | PGPGIN, |
| 689 | PGPGOUT, |
Shakeel Butt | d396def | 2022-09-07 04:35:36 +0000 | [diff] [blame] | 690 | PGSCAN_KSWAPD, |
| 691 | PGSCAN_DIRECT, |
Johannes Weiner | 57e9cc5 | 2022-10-26 14:01:33 -0400 | [diff] [blame] | 692 | PGSCAN_KHUGEPAGED, |
Shakeel Butt | d396def | 2022-09-07 04:35:36 +0000 | [diff] [blame] | 693 | PGSTEAL_KSWAPD, |
| 694 | PGSTEAL_DIRECT, |
Johannes Weiner | 57e9cc5 | 2022-10-26 14:01:33 -0400 | [diff] [blame] | 695 | PGSTEAL_KHUGEPAGED, |
Shakeel Butt | d396def | 2022-09-07 04:35:36 +0000 | [diff] [blame] | 696 | PGFAULT, |
| 697 | PGMAJFAULT, |
| 698 | PGREFILL, |
| 699 | PGACTIVATE, |
| 700 | PGDEACTIVATE, |
| 701 | PGLAZYFREE, |
| 702 | PGLAZYFREED, |
| 703 | #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) |
| 704 | ZSWPIN, |
| 705 | ZSWPOUT, |
| 706 | #endif |
| 707 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 708 | THP_FAULT_ALLOC, |
| 709 | THP_COLLAPSE_ALLOC, |
Xin Hao | 811244a | 2023-09-14 00:49:37 +0800 | [diff] [blame] | 710 | THP_SWPOUT, |
| 711 | THP_SWPOUT_FALLBACK, |
Shakeel Butt | d396def | 2022-09-07 04:35:36 +0000 | [diff] [blame] | 712 | #endif |
| 713 | }; |
| 714 | |
Shakeel Butt | 8278f1c | 2022-09-07 04:35:37 +0000 | [diff] [blame] | 715 | #define NR_MEMCG_EVENTS ARRAY_SIZE(memcg_vm_event_stat) |
| 716 | static int mem_cgroup_events_index[NR_VM_EVENT_ITEMS] __read_mostly; |
| 717 | |
| 718 | static void init_memcg_events(void) |
| 719 | { |
| 720 | int i; |
| 721 | |
| 722 | for (i = 0; i < NR_MEMCG_EVENTS; ++i) |
| 723 | mem_cgroup_events_index[memcg_vm_event_stat[i]] = i + 1; |
| 724 | } |
| 725 | |
| 726 | static inline int memcg_events_index(enum vm_event_item idx) |
| 727 | { |
| 728 | return mem_cgroup_events_index[idx] - 1; |
| 729 | } |
| 730 | |
Shakeel Butt | 410f8e8 | 2022-09-07 04:35:35 +0000 | [diff] [blame] | 731 | struct memcg_vmstats_percpu { |
| 732 | /* Local (CPU and cgroup) page state & events */ |
| 733 | long state[MEMCG_NR_STAT]; |
Shakeel Butt | 8278f1c | 2022-09-07 04:35:37 +0000 | [diff] [blame] | 734 | unsigned long events[NR_MEMCG_EVENTS]; |
Shakeel Butt | 410f8e8 | 2022-09-07 04:35:35 +0000 | [diff] [blame] | 735 | |
| 736 | /* Delta calculation for lockless upward propagation */ |
| 737 | long state_prev[MEMCG_NR_STAT]; |
Shakeel Butt | 8278f1c | 2022-09-07 04:35:37 +0000 | [diff] [blame] | 738 | unsigned long events_prev[NR_MEMCG_EVENTS]; |
Shakeel Butt | 410f8e8 | 2022-09-07 04:35:35 +0000 | [diff] [blame] | 739 | |
| 740 | /* Cgroup1: threshold notifications & softlimit tree updates */ |
| 741 | unsigned long nr_page_events; |
| 742 | unsigned long targets[MEM_CGROUP_NTARGETS]; |
| 743 | }; |
| 744 | |
| 745 | struct memcg_vmstats { |
| 746 | /* Aggregated (CPU and subtree) page state & events */ |
| 747 | long state[MEMCG_NR_STAT]; |
Shakeel Butt | 8278f1c | 2022-09-07 04:35:37 +0000 | [diff] [blame] | 748 | unsigned long events[NR_MEMCG_EVENTS]; |
Shakeel Butt | 410f8e8 | 2022-09-07 04:35:35 +0000 | [diff] [blame] | 749 | |
Yosry Ahmed | f82e6bf | 2023-07-26 15:32:23 +0000 | [diff] [blame] | 750 | /* Non-hierarchical (CPU aggregated) page state & events */ |
| 751 | long state_local[MEMCG_NR_STAT]; |
| 752 | unsigned long events_local[NR_MEMCG_EVENTS]; |
| 753 | |
Shakeel Butt | 410f8e8 | 2022-09-07 04:35:35 +0000 | [diff] [blame] | 754 | /* Pending child counts during tree propagation */ |
| 755 | long state_pending[MEMCG_NR_STAT]; |
Shakeel Butt | 8278f1c | 2022-09-07 04:35:37 +0000 | [diff] [blame] | 756 | unsigned long events_pending[NR_MEMCG_EVENTS]; |
Shakeel Butt | 410f8e8 | 2022-09-07 04:35:35 +0000 | [diff] [blame] | 757 | }; |
| 758 | |
| 759 | unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) |
| 760 | { |
| 761 | long x = READ_ONCE(memcg->vmstats->state[idx]); |
| 762 | #ifdef CONFIG_SMP |
| 763 | if (x < 0) |
| 764 | x = 0; |
| 765 | #endif |
| 766 | return x; |
| 767 | } |
| 768 | |
Yosry Ahmed | 7bd5bc3 | 2023-09-22 17:57:40 +0000 | [diff] [blame] | 769 | static int memcg_page_state_unit(int item); |
| 770 | |
| 771 | /* |
| 772 | * Normalize the value passed into memcg_rstat_updated() to be in pages. Round |
| 773 | * up non-zero sub-page updates to 1 page as zero page updates are ignored. |
| 774 | */ |
| 775 | static int memcg_state_val_in_pages(int idx, int val) |
| 776 | { |
| 777 | int unit = memcg_page_state_unit(idx); |
| 778 | |
| 779 | if (!val || unit == PAGE_SIZE) |
| 780 | return val; |
| 781 | else |
| 782 | return max(val * unit / PAGE_SIZE, 1UL); |
| 783 | } |
| 784 | |
Johannes Weiner | db9adbc | 2019-05-14 15:47:09 -0700 | [diff] [blame] | 785 | /** |
| 786 | * __mod_memcg_state - update cgroup memory statistics |
| 787 | * @memcg: the memory cgroup |
| 788 | * @idx: the stat item - can be enum memcg_stat_item or enum node_stat_item |
| 789 | * @val: delta to add to the counter, can be negative |
| 790 | */ |
| 791 | void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val) |
| 792 | { |
Johannes Weiner | db9adbc | 2019-05-14 15:47:09 -0700 | [diff] [blame] | 793 | if (mem_cgroup_disabled()) |
| 794 | return; |
| 795 | |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 796 | __this_cpu_add(memcg->vmstats_percpu->state[idx], val); |
Yosry Ahmed | 7bd5bc3 | 2023-09-22 17:57:40 +0000 | [diff] [blame] | 797 | memcg_rstat_updated(memcg, memcg_state_val_in_pages(idx, val)); |
Johannes Weiner | db9adbc | 2019-05-14 15:47:09 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 800 | /* idx can be of type enum memcg_stat_item or node_stat_item. */ |
Johannes Weiner | a18e6e6 | 2021-04-29 22:56:17 -0700 | [diff] [blame] | 801 | static unsigned long memcg_page_state_local(struct mem_cgroup *memcg, int idx) |
| 802 | { |
Yosry Ahmed | f82e6bf | 2023-07-26 15:32:23 +0000 | [diff] [blame] | 803 | long x = READ_ONCE(memcg->vmstats->state_local[idx]); |
Johannes Weiner | a18e6e6 | 2021-04-29 22:56:17 -0700 | [diff] [blame] | 804 | |
Johannes Weiner | a18e6e6 | 2021-04-29 22:56:17 -0700 | [diff] [blame] | 805 | #ifdef CONFIG_SMP |
| 806 | if (x < 0) |
| 807 | x = 0; |
| 808 | #endif |
| 809 | return x; |
| 810 | } |
| 811 | |
Roman Gushchin | eedc4e5 | 2020-08-06 23:20:32 -0700 | [diff] [blame] | 812 | void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, |
| 813 | int val) |
| 814 | { |
| 815 | struct mem_cgroup_per_node *pn; |
| 816 | struct mem_cgroup *memcg; |
Roman Gushchin | eedc4e5 | 2020-08-06 23:20:32 -0700 | [diff] [blame] | 817 | |
| 818 | pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec); |
| 819 | memcg = pn->memcg; |
| 820 | |
Sebastian Andrzej Siewior | be3e67b | 2022-03-22 14:40:41 -0700 | [diff] [blame] | 821 | /* |
Muhammad Muzammil | be16dd76 | 2023-10-23 17:44:05 +0500 | [diff] [blame] | 822 | * The caller from rmap relies on disabled preemption because they never |
Sebastian Andrzej Siewior | be3e67b | 2022-03-22 14:40:41 -0700 | [diff] [blame] | 823 | * update their counter from in-interrupt context. For these two |
| 824 | * counters we check that the update is never performed from an |
| 825 | * interrupt context while other caller need to have disabled interrupt. |
| 826 | */ |
| 827 | __memcg_stats_lock(); |
Thomas Gleixner | e575d40 | 2022-08-25 18:41:28 +0200 | [diff] [blame] | 828 | if (IS_ENABLED(CONFIG_DEBUG_VM)) { |
Sebastian Andrzej Siewior | be3e67b | 2022-03-22 14:40:41 -0700 | [diff] [blame] | 829 | switch (idx) { |
| 830 | case NR_ANON_MAPPED: |
| 831 | case NR_FILE_MAPPED: |
| 832 | case NR_ANON_THPS: |
| 833 | case NR_SHMEM_PMDMAPPED: |
| 834 | case NR_FILE_PMDMAPPED: |
| 835 | WARN_ON_ONCE(!in_task()); |
| 836 | break; |
| 837 | default: |
Thomas Gleixner | e575d40 | 2022-08-25 18:41:28 +0200 | [diff] [blame] | 838 | VM_WARN_ON_IRQS_ENABLED(); |
Sebastian Andrzej Siewior | be3e67b | 2022-03-22 14:40:41 -0700 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | |
Roman Gushchin | eedc4e5 | 2020-08-06 23:20:32 -0700 | [diff] [blame] | 842 | /* Update memcg */ |
Shakeel Butt | 11192d9 | 2021-11-05 13:37:31 -0700 | [diff] [blame] | 843 | __this_cpu_add(memcg->vmstats_percpu->state[idx], val); |
Roman Gushchin | eedc4e5 | 2020-08-06 23:20:32 -0700 | [diff] [blame] | 844 | |
| 845 | /* Update lruvec */ |
Shakeel Butt | 7e1c0d6 | 2021-09-02 14:55:00 -0700 | [diff] [blame] | 846 | __this_cpu_add(pn->lruvec_stats_percpu->state[idx], val); |
Shakeel Butt | 11192d9 | 2021-11-05 13:37:31 -0700 | [diff] [blame] | 847 | |
Yosry Ahmed | 7bd5bc3 | 2023-09-22 17:57:40 +0000 | [diff] [blame] | 848 | memcg_rstat_updated(memcg, memcg_state_val_in_pages(idx, val)); |
Sebastian Andrzej Siewior | be3e67b | 2022-03-22 14:40:41 -0700 | [diff] [blame] | 849 | memcg_stats_unlock(); |
Roman Gushchin | eedc4e5 | 2020-08-06 23:20:32 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Johannes Weiner | db9adbc | 2019-05-14 15:47:09 -0700 | [diff] [blame] | 852 | /** |
| 853 | * __mod_lruvec_state - update lruvec memory statistics |
| 854 | * @lruvec: the lruvec |
| 855 | * @idx: the stat item |
| 856 | * @val: delta to add to the counter, can be negative |
| 857 | * |
| 858 | * The lruvec is the intersection of the NUMA node and a cgroup. This |
| 859 | * function updates the all three counters that are affected by a |
| 860 | * change of state at this level: per-node, per-cgroup, per-lruvec. |
| 861 | */ |
| 862 | void __mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, |
| 863 | int val) |
| 864 | { |
Johannes Weiner | db9adbc | 2019-05-14 15:47:09 -0700 | [diff] [blame] | 865 | /* Update node */ |
Roman Gushchin | eedc4e5 | 2020-08-06 23:20:32 -0700 | [diff] [blame] | 866 | __mod_node_page_state(lruvec_pgdat(lruvec), idx, val); |
Johannes Weiner | db9adbc | 2019-05-14 15:47:09 -0700 | [diff] [blame] | 867 | |
Roman Gushchin | eedc4e5 | 2020-08-06 23:20:32 -0700 | [diff] [blame] | 868 | /* Update memcg and lruvec */ |
| 869 | if (!mem_cgroup_disabled()) |
| 870 | __mod_memcg_lruvec_state(lruvec, idx, val); |
Johannes Weiner | db9adbc | 2019-05-14 15:47:09 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Shakeel Butt | c47d503 | 2020-12-14 19:07:14 -0800 | [diff] [blame] | 873 | void __mod_lruvec_page_state(struct page *page, enum node_stat_item idx, |
| 874 | int val) |
| 875 | { |
| 876 | struct page *head = compound_head(page); /* rmap on tail pages */ |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 877 | struct mem_cgroup *memcg; |
Shakeel Butt | c47d503 | 2020-12-14 19:07:14 -0800 | [diff] [blame] | 878 | pg_data_t *pgdat = page_pgdat(page); |
| 879 | struct lruvec *lruvec; |
| 880 | |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 881 | rcu_read_lock(); |
| 882 | memcg = page_memcg(head); |
Shakeel Butt | c47d503 | 2020-12-14 19:07:14 -0800 | [diff] [blame] | 883 | /* Untracked pages have no memcg, no lruvec. Update only the node */ |
Linus Torvalds | d635a69d | 2020-12-15 13:22:29 -0800 | [diff] [blame] | 884 | if (!memcg) { |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 885 | rcu_read_unlock(); |
Shakeel Butt | c47d503 | 2020-12-14 19:07:14 -0800 | [diff] [blame] | 886 | __mod_node_page_state(pgdat, idx, val); |
| 887 | return; |
| 888 | } |
| 889 | |
Linus Torvalds | d635a69d | 2020-12-15 13:22:29 -0800 | [diff] [blame] | 890 | lruvec = mem_cgroup_lruvec(memcg, pgdat); |
Shakeel Butt | c47d503 | 2020-12-14 19:07:14 -0800 | [diff] [blame] | 891 | __mod_lruvec_state(lruvec, idx, val); |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 892 | rcu_read_unlock(); |
Shakeel Butt | c47d503 | 2020-12-14 19:07:14 -0800 | [diff] [blame] | 893 | } |
Shakeel Butt | f0c0c11 | 2020-12-14 19:07:17 -0800 | [diff] [blame] | 894 | EXPORT_SYMBOL(__mod_lruvec_page_state); |
Shakeel Butt | c47d503 | 2020-12-14 19:07:14 -0800 | [diff] [blame] | 895 | |
Muchun Song | da3ceef | 2020-12-14 19:07:04 -0800 | [diff] [blame] | 896 | void __mod_lruvec_kmem_state(void *p, enum node_stat_item idx, int val) |
Roman Gushchin | ec9f023 | 2019-08-13 15:37:41 -0700 | [diff] [blame] | 897 | { |
Roman Gushchin | 4f103c6 | 2020-04-01 21:06:36 -0700 | [diff] [blame] | 898 | pg_data_t *pgdat = page_pgdat(virt_to_page(p)); |
Roman Gushchin | ec9f023 | 2019-08-13 15:37:41 -0700 | [diff] [blame] | 899 | struct mem_cgroup *memcg; |
| 900 | struct lruvec *lruvec; |
| 901 | |
| 902 | rcu_read_lock(); |
Roman Gushchin | fc4db90 | 2022-06-10 11:03:10 -0700 | [diff] [blame] | 903 | memcg = mem_cgroup_from_slab_obj(p); |
Roman Gushchin | ec9f023 | 2019-08-13 15:37:41 -0700 | [diff] [blame] | 904 | |
Muchun Song | 8faeb1f | 2020-11-21 22:17:12 -0800 | [diff] [blame] | 905 | /* |
| 906 | * Untracked pages have no memcg, no lruvec. Update only the |
| 907 | * node. If we reparent the slab objects to the root memcg, |
| 908 | * when we free the slab object, we need to update the per-memcg |
| 909 | * vmstats to keep it correct for the root memcg. |
| 910 | */ |
| 911 | if (!memcg) { |
Roman Gushchin | ec9f023 | 2019-08-13 15:37:41 -0700 | [diff] [blame] | 912 | __mod_node_page_state(pgdat, idx, val); |
| 913 | } else { |
Johannes Weiner | 867e5e1 | 2019-11-30 17:55:34 -0800 | [diff] [blame] | 914 | lruvec = mem_cgroup_lruvec(memcg, pgdat); |
Roman Gushchin | ec9f023 | 2019-08-13 15:37:41 -0700 | [diff] [blame] | 915 | __mod_lruvec_state(lruvec, idx, val); |
| 916 | } |
| 917 | rcu_read_unlock(); |
| 918 | } |
| 919 | |
Johannes Weiner | db9adbc | 2019-05-14 15:47:09 -0700 | [diff] [blame] | 920 | /** |
| 921 | * __count_memcg_events - account VM events in a cgroup |
| 922 | * @memcg: the memory cgroup |
| 923 | * @idx: the event item |
Ingo Molnar | f0953a1 | 2021-05-06 18:06:47 -0700 | [diff] [blame] | 924 | * @count: the number of events that occurred |
Johannes Weiner | db9adbc | 2019-05-14 15:47:09 -0700 | [diff] [blame] | 925 | */ |
| 926 | void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx, |
| 927 | unsigned long count) |
| 928 | { |
Shakeel Butt | 8278f1c | 2022-09-07 04:35:37 +0000 | [diff] [blame] | 929 | int index = memcg_events_index(idx); |
| 930 | |
| 931 | if (mem_cgroup_disabled() || index < 0) |
Johannes Weiner | db9adbc | 2019-05-14 15:47:09 -0700 | [diff] [blame] | 932 | return; |
| 933 | |
Sebastian Andrzej Siewior | be3e67b | 2022-03-22 14:40:41 -0700 | [diff] [blame] | 934 | memcg_stats_lock(); |
Shakeel Butt | 8278f1c | 2022-09-07 04:35:37 +0000 | [diff] [blame] | 935 | __this_cpu_add(memcg->vmstats_percpu->events[index], count); |
Shakeel Butt | 5b3be69 | 2022-01-14 14:05:39 -0800 | [diff] [blame] | 936 | memcg_rstat_updated(memcg, count); |
Sebastian Andrzej Siewior | be3e67b | 2022-03-22 14:40:41 -0700 | [diff] [blame] | 937 | memcg_stats_unlock(); |
Johannes Weiner | db9adbc | 2019-05-14 15:47:09 -0700 | [diff] [blame] | 938 | } |
| 939 | |
Johannes Weiner | 42a3003 | 2019-05-14 15:47:12 -0700 | [diff] [blame] | 940 | static unsigned long memcg_events(struct mem_cgroup *memcg, int event) |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 941 | { |
Shakeel Butt | 8278f1c | 2022-09-07 04:35:37 +0000 | [diff] [blame] | 942 | int index = memcg_events_index(event); |
| 943 | |
| 944 | if (index < 0) |
| 945 | return 0; |
| 946 | return READ_ONCE(memcg->vmstats->events[index]); |
Johannes Weiner | e9f8974 | 2011-03-23 16:42:37 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Johannes Weiner | 42a3003 | 2019-05-14 15:47:12 -0700 | [diff] [blame] | 949 | static unsigned long memcg_events_local(struct mem_cgroup *memcg, int event) |
| 950 | { |
Shakeel Butt | 8278f1c | 2022-09-07 04:35:37 +0000 | [diff] [blame] | 951 | int index = memcg_events_index(event); |
| 952 | |
| 953 | if (index < 0) |
| 954 | return 0; |
Johannes Weiner | 815744d | 2019-06-13 15:55:46 -0700 | [diff] [blame] | 955 | |
Yosry Ahmed | f82e6bf | 2023-07-26 15:32:23 +0000 | [diff] [blame] | 956 | return READ_ONCE(memcg->vmstats->events_local[index]); |
Johannes Weiner | 42a3003 | 2019-05-14 15:47:12 -0700 | [diff] [blame] | 957 | } |
| 958 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 959 | static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg, |
Johannes Weiner | 3fba69a | 2020-06-03 16:01:31 -0700 | [diff] [blame] | 960 | int nr_pages) |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 961 | { |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 962 | /* pagein of a big page is an event. So, ignore page size */ |
| 963 | if (nr_pages > 0) |
Johannes Weiner | c9019e9 | 2018-01-31 16:16:37 -0800 | [diff] [blame] | 964 | __count_memcg_events(memcg, PGPGIN, 1); |
KAMEZAWA Hiroyuki | 3751d60 | 2011-02-01 15:52:45 -0800 | [diff] [blame] | 965 | else { |
Johannes Weiner | c9019e9 | 2018-01-31 16:16:37 -0800 | [diff] [blame] | 966 | __count_memcg_events(memcg, PGPGOUT, 1); |
KAMEZAWA Hiroyuki | 3751d60 | 2011-02-01 15:52:45 -0800 | [diff] [blame] | 967 | nr_pages = -nr_pages; /* for event */ |
| 968 | } |
KAMEZAWA Hiroyuki | e401f17 | 2011-01-20 14:44:23 -0800 | [diff] [blame] | 969 | |
Chris Down | 871789d | 2019-05-14 15:46:57 -0700 | [diff] [blame] | 970 | __this_cpu_add(memcg->vmstats_percpu->nr_page_events, nr_pages); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 971 | } |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 972 | |
Johannes Weiner | f53d7ce3 | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 973 | static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg, |
| 974 | enum mem_cgroup_events_target target) |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 975 | { |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 976 | unsigned long val, next; |
| 977 | |
Chris Down | 871789d | 2019-05-14 15:46:57 -0700 | [diff] [blame] | 978 | val = __this_cpu_read(memcg->vmstats_percpu->nr_page_events); |
| 979 | next = __this_cpu_read(memcg->vmstats_percpu->targets[target]); |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 980 | /* from time_after() in jiffies.h */ |
Michal Hocko | 6a1a8b8 | 2017-07-10 15:48:53 -0700 | [diff] [blame] | 981 | if ((long)(next - val) < 0) { |
Johannes Weiner | f53d7ce3 | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 982 | switch (target) { |
| 983 | case MEM_CGROUP_TARGET_THRESH: |
| 984 | next = val + THRESHOLDS_EVENTS_TARGET; |
| 985 | break; |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 986 | case MEM_CGROUP_TARGET_SOFTLIMIT: |
| 987 | next = val + SOFTLIMIT_EVENTS_TARGET; |
| 988 | break; |
Johannes Weiner | f53d7ce3 | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 989 | default: |
| 990 | break; |
| 991 | } |
Chris Down | 871789d | 2019-05-14 15:46:57 -0700 | [diff] [blame] | 992 | __this_cpu_write(memcg->vmstats_percpu->targets[target], next); |
Johannes Weiner | f53d7ce3 | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 993 | return true; |
Johannes Weiner | 7a159cc | 2011-03-23 16:42:38 -0700 | [diff] [blame] | 994 | } |
Johannes Weiner | f53d7ce3 | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 995 | return false; |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | /* |
| 999 | * Check events in order. |
| 1000 | * |
| 1001 | */ |
Matthew Wilcox (Oracle) | 8e88bd2 | 2021-06-25 09:05:47 -0400 | [diff] [blame] | 1002 | static void memcg_check_events(struct mem_cgroup *memcg, int nid) |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 1003 | { |
Sebastian Andrzej Siewior | 2343e88 | 2022-03-22 14:40:38 -0700 | [diff] [blame] | 1004 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) |
| 1005 | return; |
| 1006 | |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 1007 | /* threshold event is triggered in finer grain than soft limit */ |
Johannes Weiner | f53d7ce3 | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 1008 | if (unlikely(mem_cgroup_event_ratelimit(memcg, |
| 1009 | MEM_CGROUP_TARGET_THRESH))) { |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 1010 | bool do_softlimit; |
Johannes Weiner | f53d7ce3 | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 1011 | |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 1012 | do_softlimit = mem_cgroup_event_ratelimit(memcg, |
| 1013 | MEM_CGROUP_TARGET_SOFTLIMIT); |
Johannes Weiner | f53d7ce3 | 2012-01-12 17:18:23 -0800 | [diff] [blame] | 1014 | mem_cgroup_threshold(memcg); |
Andrew Morton | bb4cc1a8 | 2013-09-24 15:27:40 -0700 | [diff] [blame] | 1015 | if (unlikely(do_softlimit)) |
Matthew Wilcox (Oracle) | 8e88bd2 | 2021-06-25 09:05:47 -0400 | [diff] [blame] | 1016 | mem_cgroup_update_tree(memcg, nid); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 1017 | } |
KAMEZAWA Hiroyuki | d2265e6 | 2010-03-10 15:22:31 -0800 | [diff] [blame] | 1018 | } |
| 1019 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 1020 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 1021 | { |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 1022 | /* |
| 1023 | * mm_update_next_owner() may clear mm->owner to NULL |
| 1024 | * if it races with swapoff, page migration, etc. |
| 1025 | * So this can be called with p == NULL. |
| 1026 | */ |
| 1027 | if (unlikely(!p)) |
| 1028 | return NULL; |
| 1029 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 1030 | return mem_cgroup_from_css(task_css(p, memory_cgrp_id)); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 1031 | } |
Michal Hocko | 33398cf | 2015-09-08 15:01:02 -0700 | [diff] [blame] | 1032 | EXPORT_SYMBOL(mem_cgroup_from_task); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 1033 | |
Dan Schatzberg | 04f94e3 | 2021-06-28 19:38:18 -0700 | [diff] [blame] | 1034 | static __always_inline struct mem_cgroup *active_memcg(void) |
| 1035 | { |
Vasily Averin | 55a68c8 | 2021-09-02 14:55:49 -0700 | [diff] [blame] | 1036 | if (!in_task()) |
Dan Schatzberg | 04f94e3 | 2021-06-28 19:38:18 -0700 | [diff] [blame] | 1037 | return this_cpu_read(int_active_memcg); |
| 1038 | else |
| 1039 | return current->active_memcg; |
| 1040 | } |
| 1041 | |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 1042 | /** |
| 1043 | * get_mem_cgroup_from_mm: Obtain a reference on given mm_struct's memcg. |
| 1044 | * @mm: mm from which memcg should be extracted. It can be NULL. |
| 1045 | * |
Dan Schatzberg | 04f94e3 | 2021-06-28 19:38:18 -0700 | [diff] [blame] | 1046 | * Obtain a reference on mm->memcg and returns it if successful. If mm |
| 1047 | * is NULL, then the memcg is chosen as follows: |
| 1048 | * 1) The active memcg, if set. |
| 1049 | * 2) current->mm->memcg, if available |
| 1050 | * 3) root memcg |
| 1051 | * If mem_cgroup is disabled, NULL is returned. |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 1052 | */ |
| 1053 | struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm) |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1054 | { |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 1055 | struct mem_cgroup *memcg; |
| 1056 | |
| 1057 | if (mem_cgroup_disabled()) |
| 1058 | return NULL; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1059 | |
Muchun Song | 2884b6b | 2021-06-28 19:37:50 -0700 | [diff] [blame] | 1060 | /* |
| 1061 | * Page cache insertions can happen without an |
| 1062 | * actual mm context, e.g. during disk probing |
| 1063 | * on boot, loopback IO, acct() writes etc. |
| 1064 | * |
| 1065 | * No need to css_get on root memcg as the reference |
| 1066 | * counting is disabled on the root level in the |
| 1067 | * cgroup core. See CSS_NO_REF. |
| 1068 | */ |
Dan Schatzberg | 04f94e3 | 2021-06-28 19:38:18 -0700 | [diff] [blame] | 1069 | if (unlikely(!mm)) { |
| 1070 | memcg = active_memcg(); |
| 1071 | if (unlikely(memcg)) { |
| 1072 | /* remote memcg must hold a ref */ |
| 1073 | css_get(&memcg->css); |
| 1074 | return memcg; |
| 1075 | } |
| 1076 | mm = current->mm; |
| 1077 | if (unlikely(!mm)) |
| 1078 | return root_mem_cgroup; |
| 1079 | } |
Muchun Song | 2884b6b | 2021-06-28 19:37:50 -0700 | [diff] [blame] | 1080 | |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1081 | rcu_read_lock(); |
| 1082 | do { |
Muchun Song | 2884b6b | 2021-06-28 19:37:50 -0700 | [diff] [blame] | 1083 | memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 1084 | if (unlikely(!memcg)) |
Johannes Weiner | df38197 | 2014-04-07 15:37:43 -0700 | [diff] [blame] | 1085 | memcg = root_mem_cgroup; |
Roman Gushchin | 00d484f | 2019-11-15 17:34:43 -0800 | [diff] [blame] | 1086 | } while (!css_tryget(&memcg->css)); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1087 | rcu_read_unlock(); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1088 | return memcg; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1089 | } |
Shakeel Butt | d46eb14b | 2018-08-17 15:46:39 -0700 | [diff] [blame] | 1090 | EXPORT_SYMBOL(get_mem_cgroup_from_mm); |
| 1091 | |
Shakeel Butt | f745c6f | 2018-08-17 15:46:44 -0700 | [diff] [blame] | 1092 | /** |
Nhat Pham | 4b56938 | 2023-10-06 11:46:26 -0700 | [diff] [blame] | 1093 | * get_mem_cgroup_from_current - Obtain a reference on current task's memcg. |
| 1094 | */ |
| 1095 | struct mem_cgroup *get_mem_cgroup_from_current(void) |
| 1096 | { |
| 1097 | struct mem_cgroup *memcg; |
| 1098 | |
| 1099 | if (mem_cgroup_disabled()) |
| 1100 | return NULL; |
| 1101 | |
| 1102 | again: |
| 1103 | rcu_read_lock(); |
| 1104 | memcg = mem_cgroup_from_task(current); |
| 1105 | if (!css_tryget(&memcg->css)) { |
| 1106 | rcu_read_unlock(); |
| 1107 | goto again; |
| 1108 | } |
| 1109 | rcu_read_unlock(); |
| 1110 | return memcg; |
| 1111 | } |
| 1112 | |
| 1113 | /** |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 1114 | * mem_cgroup_iter - iterate over memory cgroup hierarchy |
| 1115 | * @root: hierarchy root |
| 1116 | * @prev: previously returned memcg, NULL on first invocation |
| 1117 | * @reclaim: cookie for shared reclaim walks, NULL for full walks |
| 1118 | * |
| 1119 | * Returns references to children of the hierarchy below @root, or |
| 1120 | * @root itself, or %NULL after a full round-trip. |
| 1121 | * |
| 1122 | * Caller must pass the return value in @prev on subsequent |
| 1123 | * invocations for reference counting, or use mem_cgroup_iter_break() |
| 1124 | * to cancel a hierarchy walk before the round-trip is complete. |
| 1125 | * |
Miaohe Lin | 05bdc52 | 2020-10-13 16:52:45 -0700 | [diff] [blame] | 1126 | * Reclaimers can specify a node in @reclaim to divide up the memcgs |
| 1127 | * in the hierarchy among all concurrent reclaimers operating on the |
| 1128 | * same node. |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 1129 | */ |
Andrew Morton | 694fbc0 | 2013-09-24 15:27:37 -0700 | [diff] [blame] | 1130 | struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root, |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 1131 | struct mem_cgroup *prev, |
Andrew Morton | 694fbc0 | 2013-09-24 15:27:37 -0700 | [diff] [blame] | 1132 | struct mem_cgroup_reclaim_cookie *reclaim) |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 1133 | { |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 1134 | struct mem_cgroup_reclaim_iter *iter; |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1135 | struct cgroup_subsys_state *css = NULL; |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1136 | struct mem_cgroup *memcg = NULL; |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1137 | struct mem_cgroup *pos = NULL; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1138 | |
Andrew Morton | 694fbc0 | 2013-09-24 15:27:37 -0700 | [diff] [blame] | 1139 | if (mem_cgroup_disabled()) |
| 1140 | return NULL; |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 1141 | |
KAMEZAWA Hiroyuki | 711d3d2 | 2010-10-27 15:33:42 -0700 | [diff] [blame] | 1142 | if (!root) |
| 1143 | root = root_mem_cgroup; |
| 1144 | |
Michal Hocko | 542f85f | 2013-04-29 15:07:15 -0700 | [diff] [blame] | 1145 | rcu_read_lock(); |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1146 | |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1147 | if (reclaim) { |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 1148 | struct mem_cgroup_per_node *mz; |
Johannes Weiner | 527a5ec | 2012-01-12 17:17:55 -0800 | [diff] [blame] | 1149 | |
Johannes Weiner | a3747b5 | 2021-04-29 22:56:14 -0700 | [diff] [blame] | 1150 | mz = root->nodeinfo[reclaim->pgdat->node_id]; |
Yafang Shao | 9da83f3 | 2019-11-30 17:50:03 -0800 | [diff] [blame] | 1151 | iter = &mz->iter; |
Michal Hocko | 5f57816 | 2013-04-29 15:07:17 -0700 | [diff] [blame] | 1152 | |
Wei Yang | a9320aa | 2022-04-28 23:15:59 -0700 | [diff] [blame] | 1153 | /* |
| 1154 | * On start, join the current reclaim iteration cycle. |
| 1155 | * Exit when a concurrent walker completes it. |
| 1156 | */ |
| 1157 | if (!prev) |
| 1158 | reclaim->generation = iter->generation; |
| 1159 | else if (reclaim->generation != iter->generation) |
Michal Hocko | 542f85f | 2013-04-29 15:07:15 -0700 | [diff] [blame] | 1160 | goto out_unlock; |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1161 | |
Vladimir Davydov | 6df3868 | 2015-12-29 14:54:10 -0800 | [diff] [blame] | 1162 | while (1) { |
Jason Low | 4db0c3c | 2015-04-15 16:14:08 -0700 | [diff] [blame] | 1163 | pos = READ_ONCE(iter->position); |
Vladimir Davydov | 6df3868 | 2015-12-29 14:54:10 -0800 | [diff] [blame] | 1164 | if (!pos || css_tryget(&pos->css)) |
| 1165 | break; |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1166 | /* |
Vladimir Davydov | 6df3868 | 2015-12-29 14:54:10 -0800 | [diff] [blame] | 1167 | * css reference reached zero, so iter->position will |
| 1168 | * be cleared by ->css_released. However, we should not |
| 1169 | * rely on this happening soon, because ->css_released |
| 1170 | * is called from a work queue, and by busy-waiting we |
| 1171 | * might block it. So we clear iter->position right |
| 1172 | * away. |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1173 | */ |
Vladimir Davydov | 6df3868 | 2015-12-29 14:54:10 -0800 | [diff] [blame] | 1174 | (void)cmpxchg(&iter->position, pos, NULL); |
| 1175 | } |
Wei Yang | 89d8330 | 2022-04-28 23:15:58 -0700 | [diff] [blame] | 1176 | } else if (prev) { |
| 1177 | pos = prev; |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1178 | } |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1179 | |
| 1180 | if (pos) |
| 1181 | css = &pos->css; |
| 1182 | |
| 1183 | for (;;) { |
| 1184 | css = css_next_descendant_pre(css, &root->css); |
| 1185 | if (!css) { |
| 1186 | /* |
| 1187 | * Reclaimers share the hierarchy walk, and a |
| 1188 | * new one might jump in right at the end of |
| 1189 | * the hierarchy - make sure they see at least |
| 1190 | * one group and restart from the beginning. |
| 1191 | */ |
| 1192 | if (!prev) |
| 1193 | continue; |
| 1194 | break; |
| 1195 | } |
| 1196 | |
| 1197 | /* |
| 1198 | * Verify the css and acquire a reference. The root |
| 1199 | * is provided by the caller, so we know it's alive |
| 1200 | * and kicking, and don't take an extra reference. |
| 1201 | */ |
Wei Yang | 41555da | 2022-04-28 23:15:58 -0700 | [diff] [blame] | 1202 | if (css == &root->css || css_tryget(css)) { |
| 1203 | memcg = mem_cgroup_from_css(css); |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1204 | break; |
Wei Yang | 41555da | 2022-04-28 23:15:58 -0700 | [diff] [blame] | 1205 | } |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | if (reclaim) { |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1209 | /* |
Vladimir Davydov | 6df3868 | 2015-12-29 14:54:10 -0800 | [diff] [blame] | 1210 | * The position could have already been updated by a competing |
| 1211 | * thread, so check that the value hasn't changed since we read |
| 1212 | * it to avoid reclaiming from the same cgroup twice. |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1213 | */ |
Vladimir Davydov | 6df3868 | 2015-12-29 14:54:10 -0800 | [diff] [blame] | 1214 | (void)cmpxchg(&iter->position, pos, memcg); |
| 1215 | |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1216 | if (pos) |
| 1217 | css_put(&pos->css); |
| 1218 | |
| 1219 | if (!memcg) |
| 1220 | iter->generation++; |
Johannes Weiner | 5ac8fb3 | 2014-12-10 15:42:39 -0800 | [diff] [blame] | 1221 | } |
| 1222 | |
Michal Hocko | 542f85f | 2013-04-29 15:07:15 -0700 | [diff] [blame] | 1223 | out_unlock: |
| 1224 | rcu_read_unlock(); |
Michal Hocko | c40046f | 2013-04-29 15:07:14 -0700 | [diff] [blame] | 1225 | if (prev && prev != root) |
| 1226 | css_put(&prev->css); |
| 1227 | |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1228 | return memcg; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1229 | } |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1230 | |
Johannes Weiner | 5660048 | 2012-01-12 17:17:59 -0800 | [diff] [blame] | 1231 | /** |
| 1232 | * mem_cgroup_iter_break - abort a hierarchy walk prematurely |
| 1233 | * @root: hierarchy root |
| 1234 | * @prev: last visited hierarchy member as returned by mem_cgroup_iter() |
| 1235 | */ |
| 1236 | void mem_cgroup_iter_break(struct mem_cgroup *root, |
| 1237 | struct mem_cgroup *prev) |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1238 | { |
| 1239 | if (!root) |
| 1240 | root = root_mem_cgroup; |
| 1241 | if (prev && prev != root) |
| 1242 | css_put(&prev->css); |
| 1243 | } |
| 1244 | |
Miles Chen | 54a83d6 | 2019-08-13 15:37:28 -0700 | [diff] [blame] | 1245 | static void __invalidate_reclaim_iterators(struct mem_cgroup *from, |
| 1246 | struct mem_cgroup *dead_memcg) |
Vladimir Davydov | 6df3868 | 2015-12-29 14:54:10 -0800 | [diff] [blame] | 1247 | { |
Vladimir Davydov | 6df3868 | 2015-12-29 14:54:10 -0800 | [diff] [blame] | 1248 | struct mem_cgroup_reclaim_iter *iter; |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 1249 | struct mem_cgroup_per_node *mz; |
| 1250 | int nid; |
Vladimir Davydov | 6df3868 | 2015-12-29 14:54:10 -0800 | [diff] [blame] | 1251 | |
Miles Chen | 54a83d6 | 2019-08-13 15:37:28 -0700 | [diff] [blame] | 1252 | for_each_node(nid) { |
Johannes Weiner | a3747b5 | 2021-04-29 22:56:14 -0700 | [diff] [blame] | 1253 | mz = from->nodeinfo[nid]; |
Yafang Shao | 9da83f3 | 2019-11-30 17:50:03 -0800 | [diff] [blame] | 1254 | iter = &mz->iter; |
| 1255 | cmpxchg(&iter->position, dead_memcg, NULL); |
Vladimir Davydov | 6df3868 | 2015-12-29 14:54:10 -0800 | [diff] [blame] | 1256 | } |
| 1257 | } |
| 1258 | |
Miles Chen | 54a83d6 | 2019-08-13 15:37:28 -0700 | [diff] [blame] | 1259 | static void invalidate_reclaim_iterators(struct mem_cgroup *dead_memcg) |
| 1260 | { |
| 1261 | struct mem_cgroup *memcg = dead_memcg; |
| 1262 | struct mem_cgroup *last; |
| 1263 | |
| 1264 | do { |
| 1265 | __invalidate_reclaim_iterators(memcg, dead_memcg); |
| 1266 | last = memcg; |
| 1267 | } while ((memcg = parent_mem_cgroup(memcg))); |
| 1268 | |
| 1269 | /* |
xupanda | b8dd3ee | 2022-08-15 06:51:04 +0000 | [diff] [blame] | 1270 | * When cgroup1 non-hierarchy mode is used, |
Miles Chen | 54a83d6 | 2019-08-13 15:37:28 -0700 | [diff] [blame] | 1271 | * parent_mem_cgroup() does not walk all the way up to the |
| 1272 | * cgroup root (root_mem_cgroup). So we have to handle |
| 1273 | * dead_memcg from cgroup root separately. |
| 1274 | */ |
Kamalesh Babulal | 7848ed6 | 2022-09-30 19:14:33 +0530 | [diff] [blame] | 1275 | if (!mem_cgroup_is_root(last)) |
Miles Chen | 54a83d6 | 2019-08-13 15:37:28 -0700 | [diff] [blame] | 1276 | __invalidate_reclaim_iterators(root_mem_cgroup, |
| 1277 | dead_memcg); |
| 1278 | } |
| 1279 | |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1280 | /** |
Vladimir Davydov | 7c5f64f | 2016-10-07 16:57:23 -0700 | [diff] [blame] | 1281 | * mem_cgroup_scan_tasks - iterate over tasks of a memory cgroup hierarchy |
| 1282 | * @memcg: hierarchy root |
| 1283 | * @fn: function to call for each task |
| 1284 | * @arg: argument passed to @fn |
| 1285 | * |
| 1286 | * This function iterates over tasks attached to @memcg or to any of its |
| 1287 | * descendants and calls @fn for each task. If @fn returns a non-zero |
ZhangPeng | 025b779 | 2023-06-16 14:30:30 +0800 | [diff] [blame] | 1288 | * value, the function breaks the iteration loop. Otherwise, it will iterate |
| 1289 | * over all tasks and return 0. |
Vladimir Davydov | 7c5f64f | 2016-10-07 16:57:23 -0700 | [diff] [blame] | 1290 | * |
| 1291 | * This function must not be called for the root memory cgroup. |
| 1292 | */ |
ZhangPeng | 025b779 | 2023-06-16 14:30:30 +0800 | [diff] [blame] | 1293 | void mem_cgroup_scan_tasks(struct mem_cgroup *memcg, |
| 1294 | int (*fn)(struct task_struct *, void *), void *arg) |
Vladimir Davydov | 7c5f64f | 2016-10-07 16:57:23 -0700 | [diff] [blame] | 1295 | { |
| 1296 | struct mem_cgroup *iter; |
| 1297 | int ret = 0; |
| 1298 | |
Kamalesh Babulal | 7848ed6 | 2022-09-30 19:14:33 +0530 | [diff] [blame] | 1299 | BUG_ON(mem_cgroup_is_root(memcg)); |
Vladimir Davydov | 7c5f64f | 2016-10-07 16:57:23 -0700 | [diff] [blame] | 1300 | |
| 1301 | for_each_mem_cgroup_tree(iter, memcg) { |
| 1302 | struct css_task_iter it; |
| 1303 | struct task_struct *task; |
| 1304 | |
Tetsuo Handa | f168a9a | 2019-07-11 21:00:20 -0700 | [diff] [blame] | 1305 | css_task_iter_start(&iter->css, CSS_TASK_ITER_PROCS, &it); |
Vladimir Davydov | 7c5f64f | 2016-10-07 16:57:23 -0700 | [diff] [blame] | 1306 | while (!ret && (task = css_task_iter_next(&it))) |
| 1307 | ret = fn(task, arg); |
| 1308 | css_task_iter_end(&it); |
| 1309 | if (ret) { |
| 1310 | mem_cgroup_iter_break(memcg, iter); |
| 1311 | break; |
| 1312 | } |
| 1313 | } |
Vladimir Davydov | 7c5f64f | 2016-10-07 16:57:23 -0700 | [diff] [blame] | 1314 | } |
| 1315 | |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1316 | #ifdef CONFIG_DEBUG_VM |
Matthew Wilcox (Oracle) | e809c3f | 2021-06-28 21:59:47 -0400 | [diff] [blame] | 1317 | void lruvec_memcg_debug(struct lruvec *lruvec, struct folio *folio) |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1318 | { |
| 1319 | struct mem_cgroup *memcg; |
| 1320 | |
| 1321 | if (mem_cgroup_disabled()) |
| 1322 | return; |
| 1323 | |
Matthew Wilcox (Oracle) | e809c3f | 2021-06-28 21:59:47 -0400 | [diff] [blame] | 1324 | memcg = folio_memcg(folio); |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1325 | |
| 1326 | if (!memcg) |
Kamalesh Babulal | 7848ed6 | 2022-09-30 19:14:33 +0530 | [diff] [blame] | 1327 | VM_BUG_ON_FOLIO(!mem_cgroup_is_root(lruvec_memcg(lruvec)), folio); |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1328 | else |
Matthew Wilcox (Oracle) | e809c3f | 2021-06-28 21:59:47 -0400 | [diff] [blame] | 1329 | VM_BUG_ON_FOLIO(lruvec_memcg(lruvec) != memcg, folio); |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1330 | } |
| 1331 | #endif |
| 1332 | |
Vladimir Davydov | 7c5f64f | 2016-10-07 16:57:23 -0700 | [diff] [blame] | 1333 | /** |
Matthew Wilcox (Oracle) | e809c3f | 2021-06-28 21:59:47 -0400 | [diff] [blame] | 1334 | * folio_lruvec_lock - Lock the lruvec for a folio. |
| 1335 | * @folio: Pointer to the folio. |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1336 | * |
Alex Shi | d7e3aba | 2021-02-24 12:03:47 -0800 | [diff] [blame] | 1337 | * These functions are safe to use under any of the following conditions: |
Matthew Wilcox (Oracle) | e809c3f | 2021-06-28 21:59:47 -0400 | [diff] [blame] | 1338 | * - folio locked |
| 1339 | * - folio_test_lru false |
| 1340 | * - folio_memcg_lock() |
| 1341 | * - folio frozen (refcount of 0) |
| 1342 | * |
| 1343 | * Return: The lruvec this folio is on with its lock held. |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1344 | */ |
Matthew Wilcox (Oracle) | e809c3f | 2021-06-28 21:59:47 -0400 | [diff] [blame] | 1345 | struct lruvec *folio_lruvec_lock(struct folio *folio) |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1346 | { |
Matthew Wilcox (Oracle) | e809c3f | 2021-06-28 21:59:47 -0400 | [diff] [blame] | 1347 | struct lruvec *lruvec = folio_lruvec(folio); |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1348 | |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1349 | spin_lock(&lruvec->lru_lock); |
Matthew Wilcox (Oracle) | e809c3f | 2021-06-28 21:59:47 -0400 | [diff] [blame] | 1350 | lruvec_memcg_debug(lruvec, folio); |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1351 | |
| 1352 | return lruvec; |
| 1353 | } |
| 1354 | |
Matthew Wilcox (Oracle) | e809c3f | 2021-06-28 21:59:47 -0400 | [diff] [blame] | 1355 | /** |
| 1356 | * folio_lruvec_lock_irq - Lock the lruvec for a folio. |
| 1357 | * @folio: Pointer to the folio. |
| 1358 | * |
| 1359 | * These functions are safe to use under any of the following conditions: |
| 1360 | * - folio locked |
| 1361 | * - folio_test_lru false |
| 1362 | * - folio_memcg_lock() |
| 1363 | * - folio frozen (refcount of 0) |
| 1364 | * |
| 1365 | * Return: The lruvec this folio is on with its lock held and interrupts |
| 1366 | * disabled. |
| 1367 | */ |
| 1368 | struct lruvec *folio_lruvec_lock_irq(struct folio *folio) |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1369 | { |
Matthew Wilcox (Oracle) | e809c3f | 2021-06-28 21:59:47 -0400 | [diff] [blame] | 1370 | struct lruvec *lruvec = folio_lruvec(folio); |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1371 | |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1372 | spin_lock_irq(&lruvec->lru_lock); |
Matthew Wilcox (Oracle) | e809c3f | 2021-06-28 21:59:47 -0400 | [diff] [blame] | 1373 | lruvec_memcg_debug(lruvec, folio); |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1374 | |
| 1375 | return lruvec; |
| 1376 | } |
| 1377 | |
Matthew Wilcox (Oracle) | e809c3f | 2021-06-28 21:59:47 -0400 | [diff] [blame] | 1378 | /** |
| 1379 | * folio_lruvec_lock_irqsave - Lock the lruvec for a folio. |
| 1380 | * @folio: Pointer to the folio. |
| 1381 | * @flags: Pointer to irqsave flags. |
| 1382 | * |
| 1383 | * These functions are safe to use under any of the following conditions: |
| 1384 | * - folio locked |
| 1385 | * - folio_test_lru false |
| 1386 | * - folio_memcg_lock() |
| 1387 | * - folio frozen (refcount of 0) |
| 1388 | * |
| 1389 | * Return: The lruvec this folio is on with its lock held and interrupts |
| 1390 | * disabled. |
| 1391 | */ |
| 1392 | struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio, |
| 1393 | unsigned long *flags) |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1394 | { |
Matthew Wilcox (Oracle) | e809c3f | 2021-06-28 21:59:47 -0400 | [diff] [blame] | 1395 | struct lruvec *lruvec = folio_lruvec(folio); |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1396 | |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1397 | spin_lock_irqsave(&lruvec->lru_lock, *flags); |
Matthew Wilcox (Oracle) | e809c3f | 2021-06-28 21:59:47 -0400 | [diff] [blame] | 1398 | lruvec_memcg_debug(lruvec, folio); |
Alex Shi | 6168d0d | 2020-12-15 12:34:29 -0800 | [diff] [blame] | 1399 | |
| 1400 | return lruvec; |
| 1401 | } |
| 1402 | |
| 1403 | /** |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1404 | * mem_cgroup_update_lru_size - account for adding or removing an lru page |
| 1405 | * @lruvec: mem_cgroup per zone lru vector |
| 1406 | * @lru: index of lru list the page is sitting on |
Michal Hocko | b4536f0c8 | 2017-01-10 16:58:04 -0800 | [diff] [blame] | 1407 | * @zid: zone id of the accounted pages |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1408 | * @nr_pages: positive when adding or negative when removing |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1409 | * |
Hugh Dickins | ca70723 | 2016-05-19 17:12:35 -0700 | [diff] [blame] | 1410 | * This function must be called under lru_lock, just before a page is added |
Hugh Dickins | 07ca760 | 2022-02-14 18:29:54 -0800 | [diff] [blame] | 1411 | * to or just after a page is removed from an lru list. |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1412 | */ |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1413 | void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru, |
Michal Hocko | b4536f0c8 | 2017-01-10 16:58:04 -0800 | [diff] [blame] | 1414 | int zid, int nr_pages) |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1415 | { |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 1416 | struct mem_cgroup_per_node *mz; |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1417 | unsigned long *lru_size; |
Hugh Dickins | ca70723 | 2016-05-19 17:12:35 -0700 | [diff] [blame] | 1418 | long size; |
Johannes Weiner | 925b767 | 2012-01-12 17:18:15 -0800 | [diff] [blame] | 1419 | |
| 1420 | if (mem_cgroup_disabled()) |
| 1421 | return; |
| 1422 | |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 1423 | mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec); |
Michal Hocko | b4536f0c8 | 2017-01-10 16:58:04 -0800 | [diff] [blame] | 1424 | lru_size = &mz->lru_zone_size[zid][lru]; |
Hugh Dickins | ca70723 | 2016-05-19 17:12:35 -0700 | [diff] [blame] | 1425 | |
| 1426 | if (nr_pages < 0) |
| 1427 | *lru_size += nr_pages; |
| 1428 | |
| 1429 | size = *lru_size; |
Michal Hocko | b4536f0c8 | 2017-01-10 16:58:04 -0800 | [diff] [blame] | 1430 | if (WARN_ONCE(size < 0, |
| 1431 | "%s(%p, %d, %d): lru_size %ld\n", |
| 1432 | __func__, lruvec, lru, nr_pages, size)) { |
Hugh Dickins | ca70723 | 2016-05-19 17:12:35 -0700 | [diff] [blame] | 1433 | VM_BUG_ON(1); |
| 1434 | *lru_size = 0; |
| 1435 | } |
| 1436 | |
| 1437 | if (nr_pages > 0) |
| 1438 | *lru_size += nr_pages; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1439 | } |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1440 | |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1441 | /** |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1442 | * mem_cgroup_margin - calculate chargeable space of a memory cgroup |
Wanpeng Li | dad7557 | 2012-06-20 12:53:01 -0700 | [diff] [blame] | 1443 | * @memcg: the memory cgroup |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1444 | * |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1445 | * Returns the maximum amount of memory @mem can be charged with, in |
Johannes Weiner | 7ec99d6 | 2011-03-23 16:42:36 -0700 | [diff] [blame] | 1446 | * pages. |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1447 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1448 | static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg) |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1449 | { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1450 | unsigned long margin = 0; |
| 1451 | unsigned long count; |
| 1452 | unsigned long limit; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1453 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1454 | count = page_counter_read(&memcg->memory); |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 1455 | limit = READ_ONCE(memcg->memory.max); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1456 | if (count < limit) |
| 1457 | margin = limit - count; |
| 1458 | |
Johannes Weiner | 7941d21 | 2016-01-14 15:21:23 -0800 | [diff] [blame] | 1459 | if (do_memsw_account()) { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1460 | count = page_counter_read(&memcg->memsw); |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 1461 | limit = READ_ONCE(memcg->memsw.max); |
Kaixu Xia | 1c4448e | 2020-06-01 21:49:36 -0700 | [diff] [blame] | 1462 | if (count < limit) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1463 | margin = min(margin, limit - count); |
Li RongQing | cbedbac | 2016-05-27 14:27:43 -0700 | [diff] [blame] | 1464 | else |
| 1465 | margin = 0; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | return margin; |
Johannes Weiner | 1994282 | 2011-02-01 15:52:43 -0800 | [diff] [blame] | 1469 | } |
| 1470 | |
KAMEZAWA Hiroyuki | 619d094 | 2012-03-21 16:34:23 -0700 | [diff] [blame] | 1471 | /* |
Qiang Huang | bdcbb65 | 2014-06-04 16:08:21 -0700 | [diff] [blame] | 1472 | * A routine for checking "mem" is under move_account() or not. |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1473 | * |
Qiang Huang | bdcbb65 | 2014-06-04 16:08:21 -0700 | [diff] [blame] | 1474 | * Checking a cgroup is mc.from or mc.to or under hierarchy of |
| 1475 | * moving cgroups. This is for waiting at high-memory pressure |
| 1476 | * caused by "move". |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 1477 | */ |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1478 | static bool mem_cgroup_under_move(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1479 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1480 | struct mem_cgroup *from; |
| 1481 | struct mem_cgroup *to; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1482 | bool ret = false; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1483 | /* |
| 1484 | * Unlike task_move routines, we access mc.to, mc.from not under |
| 1485 | * mutual exclusion by cgroup_mutex. Here, we take spinlock instead. |
| 1486 | */ |
| 1487 | spin_lock(&mc.lock); |
| 1488 | from = mc.from; |
| 1489 | to = mc.to; |
| 1490 | if (!from) |
| 1491 | goto unlock; |
Michal Hocko | 3e92041 | 2011-07-26 16:08:29 -0700 | [diff] [blame] | 1492 | |
Johannes Weiner | 2314b42 | 2014-12-10 15:44:33 -0800 | [diff] [blame] | 1493 | ret = mem_cgroup_is_descendant(from, memcg) || |
| 1494 | mem_cgroup_is_descendant(to, memcg); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 1495 | unlock: |
| 1496 | spin_unlock(&mc.lock); |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1497 | return ret; |
| 1498 | } |
| 1499 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1500 | static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1501 | { |
| 1502 | if (mc.moving_task && current != mc.moving_task) { |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1503 | if (mem_cgroup_under_move(memcg)) { |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 1504 | DEFINE_WAIT(wait); |
| 1505 | prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE); |
| 1506 | /* moving charge context might have finished. */ |
| 1507 | if (mc.moving_task) |
| 1508 | schedule(); |
| 1509 | finish_wait(&mc.waitq, &wait); |
| 1510 | return true; |
| 1511 | } |
| 1512 | } |
| 1513 | return false; |
| 1514 | } |
| 1515 | |
Muchun Song | 5f9a4f4 | 2020-10-13 16:52:59 -0700 | [diff] [blame] | 1516 | struct memory_stat { |
| 1517 | const char *name; |
Muchun Song | 5f9a4f4 | 2020-10-13 16:52:59 -0700 | [diff] [blame] | 1518 | unsigned int idx; |
| 1519 | }; |
| 1520 | |
Muchun Song | 57b2847 | 2021-02-24 12:03:31 -0800 | [diff] [blame] | 1521 | static const struct memory_stat memory_stats[] = { |
Muchun Song | fff66b7 | 2021-02-24 12:03:43 -0800 | [diff] [blame] | 1522 | { "anon", NR_ANON_MAPPED }, |
| 1523 | { "file", NR_FILE_PAGES }, |
Yosry Ahmed | a8c49af | 2022-03-22 14:40:10 -0700 | [diff] [blame] | 1524 | { "kernel", MEMCG_KMEM }, |
Muchun Song | fff66b7 | 2021-02-24 12:03:43 -0800 | [diff] [blame] | 1525 | { "kernel_stack", NR_KERNEL_STACK_KB }, |
| 1526 | { "pagetables", NR_PAGETABLE }, |
Yosry Ahmed | ebc97a5 | 2022-08-23 00:46:36 +0000 | [diff] [blame] | 1527 | { "sec_pagetables", NR_SECONDARY_PAGETABLE }, |
Muchun Song | fff66b7 | 2021-02-24 12:03:43 -0800 | [diff] [blame] | 1528 | { "percpu", MEMCG_PERCPU_B }, |
| 1529 | { "sock", MEMCG_SOCK }, |
Shakeel Butt | 4e5aa1f | 2022-01-14 14:05:45 -0800 | [diff] [blame] | 1530 | { "vmalloc", MEMCG_VMALLOC }, |
Muchun Song | fff66b7 | 2021-02-24 12:03:43 -0800 | [diff] [blame] | 1531 | { "shmem", NR_SHMEM }, |
Johannes Weiner | f4840cc | 2022-05-19 14:08:53 -0700 | [diff] [blame] | 1532 | #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) |
| 1533 | { "zswap", MEMCG_ZSWAP_B }, |
| 1534 | { "zswapped", MEMCG_ZSWAPPED }, |
| 1535 | #endif |
Muchun Song | fff66b7 | 2021-02-24 12:03:43 -0800 | [diff] [blame] | 1536 | { "file_mapped", NR_FILE_MAPPED }, |
| 1537 | { "file_dirty", NR_FILE_DIRTY }, |
| 1538 | { "file_writeback", NR_WRITEBACK }, |
Shakeel Butt | b603894 | 2021-02-24 12:03:55 -0800 | [diff] [blame] | 1539 | #ifdef CONFIG_SWAP |
| 1540 | { "swapcached", NR_SWAPCACHE }, |
| 1541 | #endif |
Muchun Song | 5f9a4f4 | 2020-10-13 16:52:59 -0700 | [diff] [blame] | 1542 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Muchun Song | fff66b7 | 2021-02-24 12:03:43 -0800 | [diff] [blame] | 1543 | { "anon_thp", NR_ANON_THPS }, |
| 1544 | { "file_thp", NR_FILE_THPS }, |
| 1545 | { "shmem_thp", NR_SHMEM_THPS }, |
Muchun Song | 5f9a4f4 | 2020-10-13 16:52:59 -0700 | [diff] [blame] | 1546 | #endif |
Muchun Song | fff66b7 | 2021-02-24 12:03:43 -0800 | [diff] [blame] | 1547 | { "inactive_anon", NR_INACTIVE_ANON }, |
| 1548 | { "active_anon", NR_ACTIVE_ANON }, |
| 1549 | { "inactive_file", NR_INACTIVE_FILE }, |
| 1550 | { "active_file", NR_ACTIVE_FILE }, |
| 1551 | { "unevictable", NR_UNEVICTABLE }, |
| 1552 | { "slab_reclaimable", NR_SLAB_RECLAIMABLE_B }, |
| 1553 | { "slab_unreclaimable", NR_SLAB_UNRECLAIMABLE_B }, |
Muchun Song | 5f9a4f4 | 2020-10-13 16:52:59 -0700 | [diff] [blame] | 1554 | |
| 1555 | /* The memory events */ |
Muchun Song | fff66b7 | 2021-02-24 12:03:43 -0800 | [diff] [blame] | 1556 | { "workingset_refault_anon", WORKINGSET_REFAULT_ANON }, |
| 1557 | { "workingset_refault_file", WORKINGSET_REFAULT_FILE }, |
| 1558 | { "workingset_activate_anon", WORKINGSET_ACTIVATE_ANON }, |
| 1559 | { "workingset_activate_file", WORKINGSET_ACTIVATE_FILE }, |
| 1560 | { "workingset_restore_anon", WORKINGSET_RESTORE_ANON }, |
| 1561 | { "workingset_restore_file", WORKINGSET_RESTORE_FILE }, |
| 1562 | { "workingset_nodereclaim", WORKINGSET_NODERECLAIM }, |
Muchun Song | 5f9a4f4 | 2020-10-13 16:52:59 -0700 | [diff] [blame] | 1563 | }; |
| 1564 | |
Yosry Ahmed | ff841a0 | 2023-09-22 17:57:39 +0000 | [diff] [blame] | 1565 | /* The actual unit of the state item, not the same as the output unit */ |
Muchun Song | fff66b7 | 2021-02-24 12:03:43 -0800 | [diff] [blame] | 1566 | static int memcg_page_state_unit(int item) |
| 1567 | { |
| 1568 | switch (item) { |
| 1569 | case MEMCG_PERCPU_B: |
Johannes Weiner | f4840cc | 2022-05-19 14:08:53 -0700 | [diff] [blame] | 1570 | case MEMCG_ZSWAP_B: |
Muchun Song | fff66b7 | 2021-02-24 12:03:43 -0800 | [diff] [blame] | 1571 | case NR_SLAB_RECLAIMABLE_B: |
| 1572 | case NR_SLAB_UNRECLAIMABLE_B: |
Muchun Song | fff66b7 | 2021-02-24 12:03:43 -0800 | [diff] [blame] | 1573 | return 1; |
| 1574 | case NR_KERNEL_STACK_KB: |
| 1575 | return SZ_1K; |
| 1576 | default: |
| 1577 | return PAGE_SIZE; |
| 1578 | } |
| 1579 | } |
| 1580 | |
Yosry Ahmed | ff841a0 | 2023-09-22 17:57:39 +0000 | [diff] [blame] | 1581 | /* Translate stat items to the correct unit for memory.stat output */ |
| 1582 | static int memcg_page_state_output_unit(int item) |
| 1583 | { |
| 1584 | /* |
| 1585 | * Workingset state is actually in pages, but we export it to userspace |
| 1586 | * as a scalar count of events, so special case it here. |
| 1587 | */ |
| 1588 | switch (item) { |
| 1589 | case WORKINGSET_REFAULT_ANON: |
| 1590 | case WORKINGSET_REFAULT_FILE: |
| 1591 | case WORKINGSET_ACTIVATE_ANON: |
| 1592 | case WORKINGSET_ACTIVATE_FILE: |
| 1593 | case WORKINGSET_RESTORE_ANON: |
| 1594 | case WORKINGSET_RESTORE_FILE: |
| 1595 | case WORKINGSET_NODERECLAIM: |
| 1596 | return 1; |
| 1597 | default: |
| 1598 | return memcg_page_state_unit(item); |
| 1599 | } |
| 1600 | } |
| 1601 | |
Muchun Song | fff66b7 | 2021-02-24 12:03:43 -0800 | [diff] [blame] | 1602 | static inline unsigned long memcg_page_state_output(struct mem_cgroup *memcg, |
| 1603 | int item) |
| 1604 | { |
Yosry Ahmed | ff841a0 | 2023-09-22 17:57:39 +0000 | [diff] [blame] | 1605 | return memcg_page_state(memcg, item) * |
| 1606 | memcg_page_state_output_unit(item); |
| 1607 | } |
| 1608 | |
| 1609 | static inline unsigned long memcg_page_state_local_output( |
| 1610 | struct mem_cgroup *memcg, int item) |
| 1611 | { |
| 1612 | return memcg_page_state_local(memcg, item) * |
| 1613 | memcg_page_state_output_unit(item); |
Muchun Song | fff66b7 | 2021-02-24 12:03:43 -0800 | [diff] [blame] | 1614 | } |
| 1615 | |
Yosry Ahmed | dddb44f | 2023-04-28 13:24:06 +0000 | [diff] [blame] | 1616 | static void memcg_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 1617 | { |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 1618 | int i; |
Johannes Weiner | 71cd311 | 2017-05-03 14:55:13 -0700 | [diff] [blame] | 1619 | |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 1620 | /* |
| 1621 | * Provide statistics on the state of the memory subsystem as |
| 1622 | * well as cumulative event counters that show past behavior. |
| 1623 | * |
| 1624 | * This list is ordered following a combination of these gradients: |
| 1625 | * 1) generic big picture -> specifics and details |
| 1626 | * 2) reflecting userspace activity -> reflecting kernel heuristics |
| 1627 | * |
| 1628 | * Current memory state: |
| 1629 | */ |
Shakeel Butt | fd25a9e | 2021-11-05 13:37:34 -0700 | [diff] [blame] | 1630 | mem_cgroup_flush_stats(); |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 1631 | |
Muchun Song | 5f9a4f4 | 2020-10-13 16:52:59 -0700 | [diff] [blame] | 1632 | for (i = 0; i < ARRAY_SIZE(memory_stats); i++) { |
| 1633 | u64 size; |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 1634 | |
Muchun Song | fff66b7 | 2021-02-24 12:03:43 -0800 | [diff] [blame] | 1635 | size = memcg_page_state_output(memcg, memory_stats[i].idx); |
Yosry Ahmed | 5b42360 | 2023-04-28 13:24:05 +0000 | [diff] [blame] | 1636 | seq_buf_printf(s, "%s %llu\n", memory_stats[i].name, size); |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 1637 | |
Muchun Song | 5f9a4f4 | 2020-10-13 16:52:59 -0700 | [diff] [blame] | 1638 | if (unlikely(memory_stats[i].idx == NR_SLAB_UNRECLAIMABLE_B)) { |
Muchun Song | fff66b7 | 2021-02-24 12:03:43 -0800 | [diff] [blame] | 1639 | size += memcg_page_state_output(memcg, |
| 1640 | NR_SLAB_RECLAIMABLE_B); |
Yosry Ahmed | 5b42360 | 2023-04-28 13:24:05 +0000 | [diff] [blame] | 1641 | seq_buf_printf(s, "slab %llu\n", size); |
Muchun Song | 5f9a4f4 | 2020-10-13 16:52:59 -0700 | [diff] [blame] | 1642 | } |
| 1643 | } |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 1644 | |
| 1645 | /* Accumulated memory events */ |
Yosry Ahmed | 5b42360 | 2023-04-28 13:24:05 +0000 | [diff] [blame] | 1646 | seq_buf_printf(s, "pgscan %lu\n", |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 1647 | memcg_events(memcg, PGSCAN_KSWAPD) + |
Johannes Weiner | 57e9cc5 | 2022-10-26 14:01:33 -0400 | [diff] [blame] | 1648 | memcg_events(memcg, PGSCAN_DIRECT) + |
| 1649 | memcg_events(memcg, PGSCAN_KHUGEPAGED)); |
Yosry Ahmed | 5b42360 | 2023-04-28 13:24:05 +0000 | [diff] [blame] | 1650 | seq_buf_printf(s, "pgsteal %lu\n", |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 1651 | memcg_events(memcg, PGSTEAL_KSWAPD) + |
Johannes Weiner | 57e9cc5 | 2022-10-26 14:01:33 -0400 | [diff] [blame] | 1652 | memcg_events(memcg, PGSTEAL_DIRECT) + |
| 1653 | memcg_events(memcg, PGSTEAL_KHUGEPAGED)); |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 1654 | |
Shakeel Butt | 8278f1c | 2022-09-07 04:35:37 +0000 | [diff] [blame] | 1655 | for (i = 0; i < ARRAY_SIZE(memcg_vm_event_stat); i++) { |
| 1656 | if (memcg_vm_event_stat[i] == PGPGIN || |
| 1657 | memcg_vm_event_stat[i] == PGPGOUT) |
| 1658 | continue; |
| 1659 | |
Yosry Ahmed | 5b42360 | 2023-04-28 13:24:05 +0000 | [diff] [blame] | 1660 | seq_buf_printf(s, "%s %lu\n", |
Qi Zheng | 673520f | 2022-06-04 16:22:09 +0800 | [diff] [blame] | 1661 | vm_event_name(memcg_vm_event_stat[i]), |
| 1662 | memcg_events(memcg, memcg_vm_event_stat[i])); |
Shakeel Butt | 8278f1c | 2022-09-07 04:35:37 +0000 | [diff] [blame] | 1663 | } |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 1664 | |
| 1665 | /* The above should easily fit into one page */ |
Yosry Ahmed | 5b42360 | 2023-04-28 13:24:05 +0000 | [diff] [blame] | 1666 | WARN_ON_ONCE(seq_buf_has_overflowed(s)); |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 1667 | } |
Johannes Weiner | 71cd311 | 2017-05-03 14:55:13 -0700 | [diff] [blame] | 1668 | |
Yosry Ahmed | dddb44f | 2023-04-28 13:24:06 +0000 | [diff] [blame] | 1669 | static void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s); |
| 1670 | |
| 1671 | static void memory_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) |
| 1672 | { |
| 1673 | if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) |
| 1674 | memcg_stat_format(memcg, s); |
| 1675 | else |
| 1676 | memcg1_stat_format(memcg, s); |
| 1677 | WARN_ON_ONCE(seq_buf_has_overflowed(s)); |
| 1678 | } |
| 1679 | |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1680 | /** |
yuzhoujian | f0c867d | 2018-12-28 00:36:10 -0800 | [diff] [blame] | 1681 | * mem_cgroup_print_oom_context: Print OOM information relevant to |
| 1682 | * memory controller. |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1683 | * @memcg: The memory cgroup that went over limit |
| 1684 | * @p: Task that is going to be killed |
| 1685 | * |
| 1686 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is |
| 1687 | * enabled |
| 1688 | */ |
yuzhoujian | f0c867d | 2018-12-28 00:36:10 -0800 | [diff] [blame] | 1689 | void mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p) |
| 1690 | { |
| 1691 | rcu_read_lock(); |
| 1692 | |
| 1693 | if (memcg) { |
| 1694 | pr_cont(",oom_memcg="); |
| 1695 | pr_cont_cgroup_path(memcg->css.cgroup); |
| 1696 | } else |
| 1697 | pr_cont(",global_oom"); |
| 1698 | if (p) { |
| 1699 | pr_cont(",task_memcg="); |
| 1700 | pr_cont_cgroup_path(task_cgroup(p, memory_cgrp_id)); |
| 1701 | } |
| 1702 | rcu_read_unlock(); |
| 1703 | } |
| 1704 | |
| 1705 | /** |
| 1706 | * mem_cgroup_print_oom_meminfo: Print OOM memory information relevant to |
| 1707 | * memory controller. |
| 1708 | * @memcg: The memory cgroup that went over limit |
| 1709 | */ |
| 1710 | void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg) |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1711 | { |
Tetsuo Handa | 68aaee1 | 2022-07-22 19:45:39 +0900 | [diff] [blame] | 1712 | /* Use static buffer, for the caller is holding oom_lock. */ |
| 1713 | static char buf[PAGE_SIZE]; |
Yosry Ahmed | 5b42360 | 2023-04-28 13:24:05 +0000 | [diff] [blame] | 1714 | struct seq_buf s; |
Tetsuo Handa | 68aaee1 | 2022-07-22 19:45:39 +0900 | [diff] [blame] | 1715 | |
| 1716 | lockdep_assert_held(&oom_lock); |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1717 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1718 | pr_info("memory: usage %llukB, limit %llukB, failcnt %lu\n", |
| 1719 | K((u64)page_counter_read(&memcg->memory)), |
Chris Down | 15b4256 | 2020-04-01 21:07:20 -0700 | [diff] [blame] | 1720 | K((u64)READ_ONCE(memcg->memory.max)), memcg->memory.failcnt); |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 1721 | if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) |
| 1722 | pr_info("swap: usage %llukB, limit %llukB, failcnt %lu\n", |
| 1723 | K((u64)page_counter_read(&memcg->swap)), |
Chris Down | 32d087c | 2020-04-01 21:07:30 -0700 | [diff] [blame] | 1724 | K((u64)READ_ONCE(memcg->swap.max)), memcg->swap.failcnt); |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 1725 | else { |
| 1726 | pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %lu\n", |
| 1727 | K((u64)page_counter_read(&memcg->memsw)), |
| 1728 | K((u64)memcg->memsw.max), memcg->memsw.failcnt); |
| 1729 | pr_info("kmem: usage %llukB, limit %llukB, failcnt %lu\n", |
| 1730 | K((u64)page_counter_read(&memcg->kmem)), |
| 1731 | K((u64)memcg->kmem.max), memcg->kmem.failcnt); |
Sha Zhengju | 58cf188 | 2013-02-22 16:32:05 -0800 | [diff] [blame] | 1732 | } |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 1733 | |
| 1734 | pr_info("Memory cgroup stats for "); |
| 1735 | pr_cont_cgroup_path(memcg->css.cgroup); |
| 1736 | pr_cont(":"); |
Yosry Ahmed | 5b42360 | 2023-04-28 13:24:05 +0000 | [diff] [blame] | 1737 | seq_buf_init(&s, buf, sizeof(buf)); |
| 1738 | memory_stat_format(memcg, &s); |
| 1739 | seq_buf_do_printk(&s, KERN_INFO); |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 1740 | } |
| 1741 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1742 | /* |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1743 | * Return the memory (and swap, if configured) limit for a memcg. |
| 1744 | */ |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 1745 | unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg) |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1746 | { |
Waiman Long | 8d387a5 | 2020-10-13 16:52:52 -0700 | [diff] [blame] | 1747 | unsigned long max = READ_ONCE(memcg->memory.max); |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1748 | |
Johannes Weiner | b94c4e9 | 2022-09-26 09:57:03 -0400 | [diff] [blame] | 1749 | if (do_memsw_account()) { |
Waiman Long | 8d387a5 | 2020-10-13 16:52:52 -0700 | [diff] [blame] | 1750 | if (mem_cgroup_swappiness(memcg)) { |
| 1751 | /* Calculate swap excess capacity from memsw limit */ |
| 1752 | unsigned long swap = READ_ONCE(memcg->memsw.max) - max; |
Michal Hocko | 9a5a8f1 | 2012-11-16 14:14:49 -0800 | [diff] [blame] | 1753 | |
Waiman Long | 8d387a5 | 2020-10-13 16:52:52 -0700 | [diff] [blame] | 1754 | max += min(swap, (unsigned long)total_swap_pages); |
| 1755 | } |
Johannes Weiner | b94c4e9 | 2022-09-26 09:57:03 -0400 | [diff] [blame] | 1756 | } else { |
| 1757 | if (mem_cgroup_swappiness(memcg)) |
| 1758 | max += min(READ_ONCE(memcg->swap.max), |
| 1759 | (unsigned long)total_swap_pages); |
Michal Hocko | 9a5a8f1 | 2012-11-16 14:14:49 -0800 | [diff] [blame] | 1760 | } |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 1761 | return max; |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1762 | } |
| 1763 | |
Chris Down | 9783aa9 | 2019-10-06 17:58:32 -0700 | [diff] [blame] | 1764 | unsigned long mem_cgroup_size(struct mem_cgroup *memcg) |
| 1765 | { |
| 1766 | return page_counter_read(&memcg->memory); |
| 1767 | } |
| 1768 | |
Johannes Weiner | b6e6edc | 2016-03-17 14:20:28 -0700 | [diff] [blame] | 1769 | static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask, |
David Rientjes | 1996546 | 2012-12-11 16:00:26 -0800 | [diff] [blame] | 1770 | int order) |
David Rientjes | 9cbb78b | 2012-07-31 16:43:44 -0700 | [diff] [blame] | 1771 | { |
David Rientjes | 6e0fc46 | 2015-09-08 15:00:36 -0700 | [diff] [blame] | 1772 | struct oom_control oc = { |
| 1773 | .zonelist = NULL, |
| 1774 | .nodemask = NULL, |
Vladimir Davydov | 2a966b7 | 2016-07-26 15:22:33 -0700 | [diff] [blame] | 1775 | .memcg = memcg, |
David Rientjes | 6e0fc46 | 2015-09-08 15:00:36 -0700 | [diff] [blame] | 1776 | .gfp_mask = gfp_mask, |
| 1777 | .order = order, |
David Rientjes | 6e0fc46 | 2015-09-08 15:00:36 -0700 | [diff] [blame] | 1778 | }; |
Yafang Shao | 1378b37 | 2020-08-06 23:22:08 -0700 | [diff] [blame] | 1779 | bool ret = true; |
David Rientjes | 9cbb78b | 2012-07-31 16:43:44 -0700 | [diff] [blame] | 1780 | |
Tetsuo Handa | 7775fac | 2019-03-05 15:46:47 -0800 | [diff] [blame] | 1781 | if (mutex_lock_killable(&oom_lock)) |
| 1782 | return true; |
Yafang Shao | 1378b37 | 2020-08-06 23:22:08 -0700 | [diff] [blame] | 1783 | |
| 1784 | if (mem_cgroup_margin(memcg) >= (1 << order)) |
| 1785 | goto unlock; |
| 1786 | |
Tetsuo Handa | 7775fac | 2019-03-05 15:46:47 -0800 | [diff] [blame] | 1787 | /* |
| 1788 | * A few threads which were not waiting at mutex_lock_killable() can |
| 1789 | * fail to bail out. Therefore, check again after holding oom_lock. |
| 1790 | */ |
Vasily Averin | a4ebf1b | 2021-11-05 13:38:09 -0700 | [diff] [blame] | 1791 | ret = task_is_dying() || out_of_memory(&oc); |
Yafang Shao | 1378b37 | 2020-08-06 23:22:08 -0700 | [diff] [blame] | 1792 | |
| 1793 | unlock: |
Johannes Weiner | dc56401 | 2015-06-24 16:57:19 -0700 | [diff] [blame] | 1794 | mutex_unlock(&oom_lock); |
Vladimir Davydov | 7c5f64f | 2016-10-07 16:57:23 -0700 | [diff] [blame] | 1795 | return ret; |
David Rientjes | 9cbb78b | 2012-07-31 16:43:44 -0700 | [diff] [blame] | 1796 | } |
| 1797 | |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 1798 | static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg, |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 1799 | pg_data_t *pgdat, |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 1800 | gfp_t gfp_mask, |
| 1801 | unsigned long *total_scanned) |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1802 | { |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 1803 | struct mem_cgroup *victim = NULL; |
| 1804 | int total = 0; |
| 1805 | int loop = 0; |
| 1806 | unsigned long excess; |
| 1807 | unsigned long nr_scanned; |
| 1808 | struct mem_cgroup_reclaim_cookie reclaim = { |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 1809 | .pgdat = pgdat, |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 1810 | }; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 1811 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1812 | excess = soft_limit_excess(root_memcg); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1813 | |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 1814 | while (1) { |
| 1815 | victim = mem_cgroup_iter(root_memcg, victim, &reclaim); |
| 1816 | if (!victim) { |
| 1817 | loop++; |
| 1818 | if (loop >= 2) { |
| 1819 | /* |
| 1820 | * If we have not been able to reclaim |
| 1821 | * anything, it might because there are |
| 1822 | * no reclaimable pages under this hierarchy |
| 1823 | */ |
| 1824 | if (!total) |
| 1825 | break; |
| 1826 | /* |
| 1827 | * We want to do more targeted reclaim. |
| 1828 | * excess >> 2 is not to excessive so as to |
| 1829 | * reclaim too much, nor too less that we keep |
| 1830 | * coming back to reclaim from this cgroup |
| 1831 | */ |
| 1832 | if (total >= (excess >> 2) || |
| 1833 | (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) |
| 1834 | break; |
| 1835 | } |
| 1836 | continue; |
| 1837 | } |
Mel Gorman | a9dd0a8 | 2016-07-28 15:46:02 -0700 | [diff] [blame] | 1838 | total += mem_cgroup_shrink_node(victim, gfp_mask, false, |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 1839 | pgdat, &nr_scanned); |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 1840 | *total_scanned += nr_scanned; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 1841 | if (!soft_limit_excess(root_memcg)) |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 1842 | break; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1843 | } |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 1844 | mem_cgroup_iter_break(root_memcg, victim); |
| 1845 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1846 | } |
| 1847 | |
Johannes Weiner | 0056f4e | 2013-10-31 16:34:14 -0700 | [diff] [blame] | 1848 | #ifdef CONFIG_LOCKDEP |
| 1849 | static struct lockdep_map memcg_oom_lock_dep_map = { |
| 1850 | .name = "memcg_oom_lock", |
| 1851 | }; |
| 1852 | #endif |
| 1853 | |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 1854 | static DEFINE_SPINLOCK(memcg_oom_lock); |
| 1855 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1856 | /* |
| 1857 | * Check OOM-Killer is already running under our hierarchy. |
| 1858 | * If someone is running, return false. |
| 1859 | */ |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 1860 | static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1861 | { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1862 | struct mem_cgroup *iter, *failed = NULL; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1863 | |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 1864 | spin_lock(&memcg_oom_lock); |
| 1865 | |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1866 | for_each_mem_cgroup_tree(iter, memcg) { |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame] | 1867 | if (iter->oom_lock) { |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1868 | /* |
| 1869 | * this subtree of our hierarchy is already locked |
| 1870 | * so we cannot give a lock. |
| 1871 | */ |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1872 | failed = iter; |
Johannes Weiner | 9f3a0d0 | 2012-01-12 17:17:48 -0800 | [diff] [blame] | 1873 | mem_cgroup_iter_break(memcg, iter); |
| 1874 | break; |
Johannes Weiner | 23751be | 2011-08-25 15:59:16 -0700 | [diff] [blame] | 1875 | } else |
| 1876 | iter->oom_lock = true; |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1877 | } |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1878 | |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 1879 | if (failed) { |
| 1880 | /* |
| 1881 | * OK, we failed to lock the whole subtree so we have |
| 1882 | * to clean up what we set up to the failing subtree |
| 1883 | */ |
| 1884 | for_each_mem_cgroup_tree(iter, memcg) { |
| 1885 | if (iter == failed) { |
| 1886 | mem_cgroup_iter_break(memcg, iter); |
| 1887 | break; |
| 1888 | } |
| 1889 | iter->oom_lock = false; |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1890 | } |
Johannes Weiner | 0056f4e | 2013-10-31 16:34:14 -0700 | [diff] [blame] | 1891 | } else |
| 1892 | mutex_acquire(&memcg_oom_lock_dep_map, 0, 1, _RET_IP_); |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 1893 | |
| 1894 | spin_unlock(&memcg_oom_lock); |
| 1895 | |
| 1896 | return !failed; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1897 | } |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1898 | |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 1899 | static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1900 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 1901 | struct mem_cgroup *iter; |
| 1902 | |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 1903 | spin_lock(&memcg_oom_lock); |
Qian Cai | 5facae4 | 2019-09-19 12:09:40 -0400 | [diff] [blame] | 1904 | mutex_release(&memcg_oom_lock_dep_map, _RET_IP_); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1905 | for_each_mem_cgroup_tree(iter, memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1906 | iter->oom_lock = false; |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 1907 | spin_unlock(&memcg_oom_lock); |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1908 | } |
| 1909 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1910 | static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1911 | { |
| 1912 | struct mem_cgroup *iter; |
| 1913 | |
Tejun Heo | c2b42d3 | 2015-06-24 16:58:23 -0700 | [diff] [blame] | 1914 | spin_lock(&memcg_oom_lock); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1915 | for_each_mem_cgroup_tree(iter, memcg) |
Tejun Heo | c2b42d3 | 2015-06-24 16:58:23 -0700 | [diff] [blame] | 1916 | iter->under_oom++; |
| 1917 | spin_unlock(&memcg_oom_lock); |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1918 | } |
| 1919 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1920 | static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg) |
Michal Hocko | 79dfdac | 2011-07-26 16:08:23 -0700 | [diff] [blame] | 1921 | { |
| 1922 | struct mem_cgroup *iter; |
| 1923 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1924 | /* |
Ingo Molnar | f0953a1 | 2021-05-06 18:06:47 -0700 | [diff] [blame] | 1925 | * Be careful about under_oom underflows because a child memcg |
Miaohe Lin | 7a52d4d8 | 2020-10-13 16:53:05 -0700 | [diff] [blame] | 1926 | * could have been added after mem_cgroup_mark_under_oom. |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1927 | */ |
Tejun Heo | c2b42d3 | 2015-06-24 16:58:23 -0700 | [diff] [blame] | 1928 | spin_lock(&memcg_oom_lock); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1929 | for_each_mem_cgroup_tree(iter, memcg) |
Tejun Heo | c2b42d3 | 2015-06-24 16:58:23 -0700 | [diff] [blame] | 1930 | if (iter->under_oom > 0) |
| 1931 | iter->under_oom--; |
| 1932 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1933 | } |
| 1934 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1935 | static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq); |
| 1936 | |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1937 | struct oom_wait_info { |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 1938 | struct mem_cgroup *memcg; |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 1939 | wait_queue_entry_t wait; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1940 | }; |
| 1941 | |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 1942 | static int memcg_oom_wake_function(wait_queue_entry_t *wait, |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1943 | unsigned mode, int sync, void *arg) |
| 1944 | { |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 1945 | struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg; |
| 1946 | struct mem_cgroup *oom_wait_memcg; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1947 | struct oom_wait_info *oom_wait_info; |
| 1948 | |
| 1949 | oom_wait_info = container_of(wait, struct oom_wait_info, wait); |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 1950 | oom_wait_memcg = oom_wait_info->memcg; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1951 | |
Johannes Weiner | 2314b42 | 2014-12-10 15:44:33 -0800 | [diff] [blame] | 1952 | if (!mem_cgroup_is_descendant(wake_memcg, oom_wait_memcg) && |
| 1953 | !mem_cgroup_is_descendant(oom_wait_memcg, wake_memcg)) |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1954 | return 0; |
KAMEZAWA Hiroyuki | dc98df5 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 1955 | return autoremove_wake_function(wait, mode, sync, arg); |
| 1956 | } |
| 1957 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 1958 | static void memcg_oom_recover(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1959 | { |
Tejun Heo | c2b42d3 | 2015-06-24 16:58:23 -0700 | [diff] [blame] | 1960 | /* |
| 1961 | * For the following lockless ->under_oom test, the only required |
| 1962 | * guarantee is that it must see the state asserted by an OOM when |
| 1963 | * this function is called as a result of userland actions |
| 1964 | * triggered by the notification of the OOM. This is trivially |
| 1965 | * achieved by invoking mem_cgroup_mark_under_oom() before |
| 1966 | * triggering notification. |
| 1967 | */ |
| 1968 | if (memcg && memcg->under_oom) |
Tejun Heo | f4b90b70 | 2015-06-24 16:58:21 -0700 | [diff] [blame] | 1969 | __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 1970 | } |
| 1971 | |
Shakeel Butt | becdf89 | 2022-03-22 14:40:19 -0700 | [diff] [blame] | 1972 | /* |
| 1973 | * Returns true if successfully killed one or more processes. Though in some |
| 1974 | * corner cases it can return true even without killing any process. |
| 1975 | */ |
| 1976 | static bool mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order) |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1977 | { |
Shakeel Butt | becdf89 | 2022-03-22 14:40:19 -0700 | [diff] [blame] | 1978 | bool locked, ret; |
Michal Hocko | 7056d3a | 2018-12-28 00:39:57 -0800 | [diff] [blame] | 1979 | |
Michal Hocko | 29ef680 | 2018-08-17 15:47:11 -0700 | [diff] [blame] | 1980 | if (order > PAGE_ALLOC_COSTLY_ORDER) |
Shakeel Butt | becdf89 | 2022-03-22 14:40:19 -0700 | [diff] [blame] | 1981 | return false; |
Michal Hocko | 29ef680 | 2018-08-17 15:47:11 -0700 | [diff] [blame] | 1982 | |
Roman Gushchin | 7a1adfd | 2018-10-26 15:09:48 -0700 | [diff] [blame] | 1983 | memcg_memory_event(memcg, MEMCG_OOM); |
| 1984 | |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 1985 | /* |
Johannes Weiner | 49426420 | 2013-10-16 13:46:59 -0700 | [diff] [blame] | 1986 | * We are in the middle of the charge context here, so we |
| 1987 | * don't want to block when potentially sitting on a callstack |
| 1988 | * that holds all kinds of filesystem and mm locks. |
| 1989 | * |
Michal Hocko | 29ef680 | 2018-08-17 15:47:11 -0700 | [diff] [blame] | 1990 | * cgroup1 allows disabling the OOM killer and waiting for outside |
| 1991 | * handling until the charge can succeed; remember the context and put |
| 1992 | * the task to sleep at the end of the page fault when all locks are |
| 1993 | * released. |
Johannes Weiner | 49426420 | 2013-10-16 13:46:59 -0700 | [diff] [blame] | 1994 | * |
Michal Hocko | 29ef680 | 2018-08-17 15:47:11 -0700 | [diff] [blame] | 1995 | * On the other hand, in-kernel OOM killer allows for an async victim |
| 1996 | * memory reclaim (oom_reaper) and that means that we are not solely |
| 1997 | * relying on the oom victim to make a forward progress and we can |
| 1998 | * invoke the oom killer here. |
| 1999 | * |
| 2000 | * Please note that mem_cgroup_out_of_memory might fail to find a |
| 2001 | * victim and then we have to bail out from the charge path. |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2002 | */ |
Yue Zhao | 17c56de | 2023-03-06 23:41:37 +0800 | [diff] [blame] | 2003 | if (READ_ONCE(memcg->oom_kill_disable)) { |
Shakeel Butt | becdf89 | 2022-03-22 14:40:19 -0700 | [diff] [blame] | 2004 | if (current->in_user_fault) { |
| 2005 | css_get(&memcg->css); |
| 2006 | current->memcg_in_oom = memcg; |
| 2007 | current->memcg_oom_gfp_mask = mask; |
| 2008 | current->memcg_oom_order = order; |
| 2009 | } |
| 2010 | return false; |
Michal Hocko | 29ef680 | 2018-08-17 15:47:11 -0700 | [diff] [blame] | 2011 | } |
| 2012 | |
Michal Hocko | 7056d3a | 2018-12-28 00:39:57 -0800 | [diff] [blame] | 2013 | mem_cgroup_mark_under_oom(memcg); |
Michal Hocko | 29ef680 | 2018-08-17 15:47:11 -0700 | [diff] [blame] | 2014 | |
Michal Hocko | 7056d3a | 2018-12-28 00:39:57 -0800 | [diff] [blame] | 2015 | locked = mem_cgroup_oom_trylock(memcg); |
| 2016 | |
| 2017 | if (locked) |
| 2018 | mem_cgroup_oom_notify(memcg); |
| 2019 | |
| 2020 | mem_cgroup_unmark_under_oom(memcg); |
Shakeel Butt | becdf89 | 2022-03-22 14:40:19 -0700 | [diff] [blame] | 2021 | ret = mem_cgroup_out_of_memory(memcg, mask, order); |
Michal Hocko | 7056d3a | 2018-12-28 00:39:57 -0800 | [diff] [blame] | 2022 | |
| 2023 | if (locked) |
| 2024 | mem_cgroup_oom_unlock(memcg); |
| 2025 | |
| 2026 | return ret; |
Johannes Weiner | 49426420 | 2013-10-16 13:46:59 -0700 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | /** |
| 2030 | * mem_cgroup_oom_synchronize - complete memcg OOM handling |
| 2031 | * @handle: actually kill/wait or just clean up the OOM state |
| 2032 | * |
| 2033 | * This has to be called at the end of a page fault if the memcg OOM |
| 2034 | * handler was enabled. |
| 2035 | * |
| 2036 | * Memcg supports userspace OOM handling where failed allocations must |
| 2037 | * sleep on a waitqueue until the userspace task resolves the |
| 2038 | * situation. Sleeping directly in the charge context with all kinds |
| 2039 | * of locks held is not a good idea, instead we remember an OOM state |
| 2040 | * in the task and mem_cgroup_oom_synchronize() has to be called at |
| 2041 | * the end of the page fault to complete the OOM handling. |
| 2042 | * |
| 2043 | * Returns %true if an ongoing memcg OOM situation was detected and |
| 2044 | * completed, %false otherwise. |
| 2045 | */ |
| 2046 | bool mem_cgroup_oom_synchronize(bool handle) |
| 2047 | { |
Tejun Heo | 626ebc4 | 2015-11-05 18:46:09 -0800 | [diff] [blame] | 2048 | struct mem_cgroup *memcg = current->memcg_in_oom; |
Johannes Weiner | 49426420 | 2013-10-16 13:46:59 -0700 | [diff] [blame] | 2049 | struct oom_wait_info owait; |
| 2050 | bool locked; |
| 2051 | |
| 2052 | /* OOM is global, do not handle */ |
| 2053 | if (!memcg) |
| 2054 | return false; |
| 2055 | |
Vladimir Davydov | 7c5f64f | 2016-10-07 16:57:23 -0700 | [diff] [blame] | 2056 | if (!handle) |
Johannes Weiner | 49426420 | 2013-10-16 13:46:59 -0700 | [diff] [blame] | 2057 | goto cleanup; |
| 2058 | |
| 2059 | owait.memcg = memcg; |
| 2060 | owait.wait.flags = 0; |
| 2061 | owait.wait.func = memcg_oom_wake_function; |
| 2062 | owait.wait.private = current; |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 2063 | INIT_LIST_HEAD(&owait.wait.entry); |
Johannes Weiner | 49426420 | 2013-10-16 13:46:59 -0700 | [diff] [blame] | 2064 | |
| 2065 | prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE); |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 2066 | mem_cgroup_mark_under_oom(memcg); |
| 2067 | |
| 2068 | locked = mem_cgroup_oom_trylock(memcg); |
| 2069 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 2070 | if (locked) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2071 | mem_cgroup_oom_notify(memcg); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2072 | |
Haifeng Xu | 857f213 | 2023-04-19 03:07:38 +0000 | [diff] [blame] | 2073 | schedule(); |
| 2074 | mem_cgroup_unmark_under_oom(memcg); |
| 2075 | finish_wait(&memcg_oom_waitq, &owait.wait); |
Johannes Weiner | 49426420 | 2013-10-16 13:46:59 -0700 | [diff] [blame] | 2076 | |
Haifeng Xu | 18b1d18 | 2023-04-19 03:07:39 +0000 | [diff] [blame] | 2077 | if (locked) |
Johannes Weiner | fb2a6fc | 2013-09-12 15:13:43 -0700 | [diff] [blame] | 2078 | mem_cgroup_oom_unlock(memcg); |
Johannes Weiner | 49426420 | 2013-10-16 13:46:59 -0700 | [diff] [blame] | 2079 | cleanup: |
Tejun Heo | 626ebc4 | 2015-11-05 18:46:09 -0800 | [diff] [blame] | 2080 | current->memcg_in_oom = NULL; |
Johannes Weiner | 3812c8c | 2013-09-12 15:13:44 -0700 | [diff] [blame] | 2081 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | 867578c | 2010-03-10 15:22:39 -0800 | [diff] [blame] | 2082 | return true; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 2083 | } |
| 2084 | |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 2085 | /** |
Roman Gushchin | 3d8b38e | 2018-08-21 21:53:54 -0700 | [diff] [blame] | 2086 | * mem_cgroup_get_oom_group - get a memory cgroup to clean up after OOM |
| 2087 | * @victim: task to be killed by the OOM killer |
| 2088 | * @oom_domain: memcg in case of memcg OOM, NULL in case of system-wide OOM |
| 2089 | * |
| 2090 | * Returns a pointer to a memory cgroup, which has to be cleaned up |
| 2091 | * by killing all belonging OOM-killable tasks. |
| 2092 | * |
| 2093 | * Caller has to call mem_cgroup_put() on the returned non-NULL memcg. |
| 2094 | */ |
| 2095 | struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim, |
| 2096 | struct mem_cgroup *oom_domain) |
| 2097 | { |
| 2098 | struct mem_cgroup *oom_group = NULL; |
| 2099 | struct mem_cgroup *memcg; |
| 2100 | |
| 2101 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) |
| 2102 | return NULL; |
| 2103 | |
| 2104 | if (!oom_domain) |
| 2105 | oom_domain = root_mem_cgroup; |
| 2106 | |
| 2107 | rcu_read_lock(); |
| 2108 | |
| 2109 | memcg = mem_cgroup_from_task(victim); |
Kamalesh Babulal | 7848ed6 | 2022-09-30 19:14:33 +0530 | [diff] [blame] | 2110 | if (mem_cgroup_is_root(memcg)) |
Roman Gushchin | 3d8b38e | 2018-08-21 21:53:54 -0700 | [diff] [blame] | 2111 | goto out; |
| 2112 | |
| 2113 | /* |
Roman Gushchin | 48fe267 | 2020-04-01 21:07:39 -0700 | [diff] [blame] | 2114 | * If the victim task has been asynchronously moved to a different |
| 2115 | * memory cgroup, we might end up killing tasks outside oom_domain. |
| 2116 | * In this case it's better to ignore memory.group.oom. |
| 2117 | */ |
| 2118 | if (unlikely(!mem_cgroup_is_descendant(memcg, oom_domain))) |
| 2119 | goto out; |
| 2120 | |
| 2121 | /* |
Roman Gushchin | 3d8b38e | 2018-08-21 21:53:54 -0700 | [diff] [blame] | 2122 | * Traverse the memory cgroup hierarchy from the victim task's |
| 2123 | * cgroup up to the OOMing cgroup (or root) to find the |
| 2124 | * highest-level memory cgroup with oom.group set. |
| 2125 | */ |
| 2126 | for (; memcg; memcg = parent_mem_cgroup(memcg)) { |
Yue Zhao | eaf7b66 | 2023-03-06 23:41:35 +0800 | [diff] [blame] | 2127 | if (READ_ONCE(memcg->oom_group)) |
Roman Gushchin | 3d8b38e | 2018-08-21 21:53:54 -0700 | [diff] [blame] | 2128 | oom_group = memcg; |
| 2129 | |
| 2130 | if (memcg == oom_domain) |
| 2131 | break; |
| 2132 | } |
| 2133 | |
| 2134 | if (oom_group) |
| 2135 | css_get(&oom_group->css); |
| 2136 | out: |
| 2137 | rcu_read_unlock(); |
| 2138 | |
| 2139 | return oom_group; |
| 2140 | } |
| 2141 | |
| 2142 | void mem_cgroup_print_oom_group(struct mem_cgroup *memcg) |
| 2143 | { |
| 2144 | pr_info("Tasks in "); |
| 2145 | pr_cont_cgroup_path(memcg->css.cgroup); |
| 2146 | pr_cont(" are going to be killed due to memory.oom.group set\n"); |
| 2147 | } |
| 2148 | |
| 2149 | /** |
Matthew Wilcox (Oracle) | f70ad44 | 2021-06-28 17:26:00 -0400 | [diff] [blame] | 2150 | * folio_memcg_lock - Bind a folio to its memcg. |
| 2151 | * @folio: The folio. |
KAMEZAWA Hiroyuki | 32047e2 | 2010-10-27 15:33:40 -0700 | [diff] [blame] | 2152 | * |
Matthew Wilcox (Oracle) | f70ad44 | 2021-06-28 17:26:00 -0400 | [diff] [blame] | 2153 | * This function prevents unlocked LRU folios from being moved to |
Johannes Weiner | 739f79f | 2017-08-18 15:15:48 -0700 | [diff] [blame] | 2154 | * another cgroup. |
| 2155 | * |
Matthew Wilcox (Oracle) | f70ad44 | 2021-06-28 17:26:00 -0400 | [diff] [blame] | 2156 | * It ensures lifetime of the bound memcg. The caller is responsible |
| 2157 | * for the lifetime of the folio. |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2158 | */ |
Matthew Wilcox (Oracle) | f70ad44 | 2021-06-28 17:26:00 -0400 | [diff] [blame] | 2159 | void folio_memcg_lock(struct folio *folio) |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2160 | { |
| 2161 | struct mem_cgroup *memcg; |
Johannes Weiner | 6de2261 | 2015-02-11 15:25:01 -0800 | [diff] [blame] | 2162 | unsigned long flags; |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2163 | |
Johannes Weiner | 6de2261 | 2015-02-11 15:25:01 -0800 | [diff] [blame] | 2164 | /* |
| 2165 | * The RCU lock is held throughout the transaction. The fast |
| 2166 | * path can get away without acquiring the memcg->move_lock |
| 2167 | * because page moving starts with an RCU grace period. |
Johannes Weiner | 739f79f | 2017-08-18 15:15:48 -0700 | [diff] [blame] | 2168 | */ |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 2169 | rcu_read_lock(); |
| 2170 | |
| 2171 | if (mem_cgroup_disabled()) |
Johannes Weiner | 1c824a6 | 2021-04-29 22:55:32 -0700 | [diff] [blame] | 2172 | return; |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2173 | again: |
Matthew Wilcox (Oracle) | f70ad44 | 2021-06-28 17:26:00 -0400 | [diff] [blame] | 2174 | memcg = folio_memcg(folio); |
Johannes Weiner | 2983331 | 2014-12-10 15:44:02 -0800 | [diff] [blame] | 2175 | if (unlikely(!memcg)) |
Johannes Weiner | 1c824a6 | 2021-04-29 22:55:32 -0700 | [diff] [blame] | 2176 | return; |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 2177 | |
Alex Shi | 20ad50d | 2020-12-15 12:33:51 -0800 | [diff] [blame] | 2178 | #ifdef CONFIG_PROVE_LOCKING |
| 2179 | local_irq_save(flags); |
| 2180 | might_lock(&memcg->move_lock); |
| 2181 | local_irq_restore(flags); |
| 2182 | #endif |
| 2183 | |
Qiang Huang | bdcbb65 | 2014-06-04 16:08:21 -0700 | [diff] [blame] | 2184 | if (atomic_read(&memcg->moving_account) <= 0) |
Johannes Weiner | 1c824a6 | 2021-04-29 22:55:32 -0700 | [diff] [blame] | 2185 | return; |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2186 | |
Johannes Weiner | 6de2261 | 2015-02-11 15:25:01 -0800 | [diff] [blame] | 2187 | spin_lock_irqsave(&memcg->move_lock, flags); |
Matthew Wilcox (Oracle) | f70ad44 | 2021-06-28 17:26:00 -0400 | [diff] [blame] | 2188 | if (memcg != folio_memcg(folio)) { |
Johannes Weiner | 6de2261 | 2015-02-11 15:25:01 -0800 | [diff] [blame] | 2189 | spin_unlock_irqrestore(&memcg->move_lock, flags); |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2190 | goto again; |
| 2191 | } |
Johannes Weiner | 6de2261 | 2015-02-11 15:25:01 -0800 | [diff] [blame] | 2192 | |
| 2193 | /* |
Johannes Weiner | 1c824a6 | 2021-04-29 22:55:32 -0700 | [diff] [blame] | 2194 | * When charge migration first begins, we can have multiple |
| 2195 | * critical sections holding the fast-path RCU lock and one |
| 2196 | * holding the slowpath move_lock. Track the task who has the |
Kefeng Wang | 6c77b60 | 2023-06-14 22:36:12 +0800 | [diff] [blame] | 2197 | * move_lock for folio_memcg_unlock(). |
Johannes Weiner | 6de2261 | 2015-02-11 15:25:01 -0800 | [diff] [blame] | 2198 | */ |
| 2199 | memcg->move_lock_task = current; |
| 2200 | memcg->move_lock_flags = flags; |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2201 | } |
Matthew Wilcox (Oracle) | f70ad44 | 2021-06-28 17:26:00 -0400 | [diff] [blame] | 2202 | |
Matthew Wilcox (Oracle) | f70ad44 | 2021-06-28 17:26:00 -0400 | [diff] [blame] | 2203 | static void __folio_memcg_unlock(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2204 | { |
Johannes Weiner | 6de2261 | 2015-02-11 15:25:01 -0800 | [diff] [blame] | 2205 | if (memcg && memcg->move_lock_task == current) { |
| 2206 | unsigned long flags = memcg->move_lock_flags; |
| 2207 | |
| 2208 | memcg->move_lock_task = NULL; |
| 2209 | memcg->move_lock_flags = 0; |
| 2210 | |
| 2211 | spin_unlock_irqrestore(&memcg->move_lock, flags); |
| 2212 | } |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2213 | |
Johannes Weiner | d7365e7 | 2014-10-29 14:50:48 -0700 | [diff] [blame] | 2214 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 89c06bd | 2012-03-21 16:34:25 -0700 | [diff] [blame] | 2215 | } |
Johannes Weiner | 739f79f | 2017-08-18 15:15:48 -0700 | [diff] [blame] | 2216 | |
| 2217 | /** |
Matthew Wilcox (Oracle) | f70ad44 | 2021-06-28 17:26:00 -0400 | [diff] [blame] | 2218 | * folio_memcg_unlock - Release the binding between a folio and its memcg. |
| 2219 | * @folio: The folio. |
| 2220 | * |
| 2221 | * This releases the binding created by folio_memcg_lock(). This does |
| 2222 | * not change the accounting of this folio to its memcg, but it does |
| 2223 | * permit others to change it. |
Johannes Weiner | 739f79f | 2017-08-18 15:15:48 -0700 | [diff] [blame] | 2224 | */ |
Matthew Wilcox (Oracle) | f70ad44 | 2021-06-28 17:26:00 -0400 | [diff] [blame] | 2225 | void folio_memcg_unlock(struct folio *folio) |
| 2226 | { |
| 2227 | __folio_memcg_unlock(folio_memcg(folio)); |
| 2228 | } |
Matthew Wilcox (Oracle) | f70ad44 | 2021-06-28 17:26:00 -0400 | [diff] [blame] | 2229 | |
Michal Hocko | fead2b8 | 2022-03-22 14:40:35 -0700 | [diff] [blame] | 2230 | struct memcg_stock_pcp { |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 2231 | local_lock_t stock_lock; |
Michal Hocko | fead2b8 | 2022-03-22 14:40:35 -0700 | [diff] [blame] | 2232 | struct mem_cgroup *cached; /* this never be root cgroup */ |
| 2233 | unsigned int nr_pages; |
| 2234 | |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 2235 | #ifdef CONFIG_MEMCG_KMEM |
| 2236 | struct obj_cgroup *cached_objcg; |
Waiman Long | 68ac5b3c | 2021-06-28 19:37:23 -0700 | [diff] [blame] | 2237 | struct pglist_data *cached_pgdat; |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 2238 | unsigned int nr_bytes; |
Waiman Long | 68ac5b3c | 2021-06-28 19:37:23 -0700 | [diff] [blame] | 2239 | int nr_slab_reclaimable_b; |
| 2240 | int nr_slab_unreclaimable_b; |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 2241 | #endif |
| 2242 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2243 | struct work_struct work; |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2244 | unsigned long flags; |
Kirill A. Shutemov | a0db00f | 2012-05-29 15:06:56 -0700 | [diff] [blame] | 2245 | #define FLUSHING_CACHED_CHARGE 0 |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2246 | }; |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 2247 | static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock) = { |
| 2248 | .stock_lock = INIT_LOCAL_LOCK(stock_lock), |
| 2249 | }; |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2250 | static DEFINE_MUTEX(percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2251 | |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 2252 | #ifdef CONFIG_MEMCG_KMEM |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 2253 | static struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock); |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 2254 | static bool obj_stock_flush_required(struct memcg_stock_pcp *stock, |
| 2255 | struct mem_cgroup *root_memcg); |
Yosry Ahmed | a8c49af | 2022-03-22 14:40:10 -0700 | [diff] [blame] | 2256 | static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages); |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 2257 | |
| 2258 | #else |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 2259 | static inline struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock) |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 2260 | { |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 2261 | return NULL; |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 2262 | } |
| 2263 | static bool obj_stock_flush_required(struct memcg_stock_pcp *stock, |
| 2264 | struct mem_cgroup *root_memcg) |
| 2265 | { |
| 2266 | return false; |
| 2267 | } |
Yosry Ahmed | a8c49af | 2022-03-22 14:40:10 -0700 | [diff] [blame] | 2268 | static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages) |
| 2269 | { |
| 2270 | } |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 2271 | #endif |
| 2272 | |
Suleiman Souhlal | a0956d5 | 2012-12-18 14:21:36 -0800 | [diff] [blame] | 2273 | /** |
| 2274 | * consume_stock: Try to consume stocked charge on this cpu. |
| 2275 | * @memcg: memcg to consume from. |
| 2276 | * @nr_pages: how many pages to charge. |
| 2277 | * |
| 2278 | * The charges will only happen if @memcg matches the current cpu's memcg |
| 2279 | * stock, and at least @nr_pages are available in that stock. Failure to |
| 2280 | * service an allocation will refill the stock. |
| 2281 | * |
| 2282 | * returns true if successful, false otherwise. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2283 | */ |
Suleiman Souhlal | a0956d5 | 2012-12-18 14:21:36 -0800 | [diff] [blame] | 2284 | static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2285 | { |
| 2286 | struct memcg_stock_pcp *stock; |
Johannes Weiner | db2ba40c | 2016-09-19 14:44:36 -0700 | [diff] [blame] | 2287 | unsigned long flags; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2288 | bool ret = false; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2289 | |
Johannes Weiner | a983b5e | 2018-01-31 16:16:45 -0800 | [diff] [blame] | 2290 | if (nr_pages > MEMCG_CHARGE_BATCH) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2291 | return ret; |
Suleiman Souhlal | a0956d5 | 2012-12-18 14:21:36 -0800 | [diff] [blame] | 2292 | |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 2293 | local_lock_irqsave(&memcg_stock.stock_lock, flags); |
Johannes Weiner | db2ba40c | 2016-09-19 14:44:36 -0700 | [diff] [blame] | 2294 | |
| 2295 | stock = this_cpu_ptr(&memcg_stock); |
Roman Gushchin | f785a8f | 2023-05-02 09:08:39 -0700 | [diff] [blame] | 2296 | if (memcg == READ_ONCE(stock->cached) && stock->nr_pages >= nr_pages) { |
Suleiman Souhlal | a0956d5 | 2012-12-18 14:21:36 -0800 | [diff] [blame] | 2297 | stock->nr_pages -= nr_pages; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2298 | ret = true; |
| 2299 | } |
Johannes Weiner | db2ba40c | 2016-09-19 14:44:36 -0700 | [diff] [blame] | 2300 | |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 2301 | local_unlock_irqrestore(&memcg_stock.stock_lock, flags); |
Johannes Weiner | db2ba40c | 2016-09-19 14:44:36 -0700 | [diff] [blame] | 2302 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2303 | return ret; |
| 2304 | } |
| 2305 | |
| 2306 | /* |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2307 | * Returns stocks cached in percpu and reset cached information. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2308 | */ |
| 2309 | static void drain_stock(struct memcg_stock_pcp *stock) |
| 2310 | { |
Roman Gushchin | f785a8f | 2023-05-02 09:08:39 -0700 | [diff] [blame] | 2311 | struct mem_cgroup *old = READ_ONCE(stock->cached); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2312 | |
Johannes Weiner | 1a3e1f4 | 2020-08-06 23:20:45 -0700 | [diff] [blame] | 2313 | if (!old) |
| 2314 | return; |
| 2315 | |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2316 | if (stock->nr_pages) { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2317 | page_counter_uncharge(&old->memory, stock->nr_pages); |
Johannes Weiner | 7941d21 | 2016-01-14 15:21:23 -0800 | [diff] [blame] | 2318 | if (do_memsw_account()) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2319 | page_counter_uncharge(&old->memsw, stock->nr_pages); |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2320 | stock->nr_pages = 0; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2321 | } |
Johannes Weiner | 1a3e1f4 | 2020-08-06 23:20:45 -0700 | [diff] [blame] | 2322 | |
| 2323 | css_put(&old->css); |
Roman Gushchin | f785a8f | 2023-05-02 09:08:39 -0700 | [diff] [blame] | 2324 | WRITE_ONCE(stock->cached, NULL); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2325 | } |
| 2326 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2327 | static void drain_local_stock(struct work_struct *dummy) |
| 2328 | { |
Johannes Weiner | db2ba40c | 2016-09-19 14:44:36 -0700 | [diff] [blame] | 2329 | struct memcg_stock_pcp *stock; |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 2330 | struct obj_cgroup *old = NULL; |
Johannes Weiner | db2ba40c | 2016-09-19 14:44:36 -0700 | [diff] [blame] | 2331 | unsigned long flags; |
| 2332 | |
Michal Hocko | 72f0184 | 2017-10-03 16:14:53 -0700 | [diff] [blame] | 2333 | /* |
Michal Hocko | 5c49cf9 | 2021-09-02 14:56:02 -0700 | [diff] [blame] | 2334 | * The only protection from cpu hotplug (memcg_hotplug_cpu_dead) vs. |
| 2335 | * drain_stock races is that we always operate on local CPU stock |
| 2336 | * here with IRQ disabled |
Michal Hocko | 72f0184 | 2017-10-03 16:14:53 -0700 | [diff] [blame] | 2337 | */ |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 2338 | local_lock_irqsave(&memcg_stock.stock_lock, flags); |
Johannes Weiner | db2ba40c | 2016-09-19 14:44:36 -0700 | [diff] [blame] | 2339 | |
| 2340 | stock = this_cpu_ptr(&memcg_stock); |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 2341 | old = drain_obj_stock(stock); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2342 | drain_stock(stock); |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2343 | clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); |
Johannes Weiner | db2ba40c | 2016-09-19 14:44:36 -0700 | [diff] [blame] | 2344 | |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 2345 | local_unlock_irqrestore(&memcg_stock.stock_lock, flags); |
| 2346 | if (old) |
| 2347 | obj_cgroup_put(old); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | /* |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2351 | * Cache charges(val) to local per_cpu area. |
Greg Thelen | 320cc51 | 2010-03-15 15:27:28 +0100 | [diff] [blame] | 2352 | * This will be consumed by consume_stock() function, later. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2353 | */ |
Johannes Weiner | af9a3b6 | 2022-03-22 14:40:44 -0700 | [diff] [blame] | 2354 | static void __refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2355 | { |
Johannes Weiner | db2ba40c | 2016-09-19 14:44:36 -0700 | [diff] [blame] | 2356 | struct memcg_stock_pcp *stock; |
Johannes Weiner | db2ba40c | 2016-09-19 14:44:36 -0700 | [diff] [blame] | 2357 | |
| 2358 | stock = this_cpu_ptr(&memcg_stock); |
Roman Gushchin | f785a8f | 2023-05-02 09:08:39 -0700 | [diff] [blame] | 2359 | if (READ_ONCE(stock->cached) != memcg) { /* reset if necessary */ |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2360 | drain_stock(stock); |
Johannes Weiner | 1a3e1f4 | 2020-08-06 23:20:45 -0700 | [diff] [blame] | 2361 | css_get(&memcg->css); |
Roman Gushchin | f785a8f | 2023-05-02 09:08:39 -0700 | [diff] [blame] | 2362 | WRITE_ONCE(stock->cached, memcg); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2363 | } |
Johannes Weiner | 11c9ea4 | 2011-03-23 16:42:34 -0700 | [diff] [blame] | 2364 | stock->nr_pages += nr_pages; |
Johannes Weiner | db2ba40c | 2016-09-19 14:44:36 -0700 | [diff] [blame] | 2365 | |
Johannes Weiner | a983b5e | 2018-01-31 16:16:45 -0800 | [diff] [blame] | 2366 | if (stock->nr_pages > MEMCG_CHARGE_BATCH) |
Roman Gushchin | 475d048 | 2017-09-08 16:13:09 -0700 | [diff] [blame] | 2367 | drain_stock(stock); |
Johannes Weiner | af9a3b6 | 2022-03-22 14:40:44 -0700 | [diff] [blame] | 2368 | } |
Roman Gushchin | 475d048 | 2017-09-08 16:13:09 -0700 | [diff] [blame] | 2369 | |
Johannes Weiner | af9a3b6 | 2022-03-22 14:40:44 -0700 | [diff] [blame] | 2370 | static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) |
| 2371 | { |
| 2372 | unsigned long flags; |
| 2373 | |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 2374 | local_lock_irqsave(&memcg_stock.stock_lock, flags); |
Johannes Weiner | af9a3b6 | 2022-03-22 14:40:44 -0700 | [diff] [blame] | 2375 | __refill_stock(memcg, nr_pages); |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 2376 | local_unlock_irqrestore(&memcg_stock.stock_lock, flags); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | /* |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2380 | * Drains all per-CPU charge caches for given root_memcg resp. subtree |
Johannes Weiner | 6d3d6aa | 2014-12-10 15:42:50 -0800 | [diff] [blame] | 2381 | * of the hierarchy under it. |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2382 | */ |
Johannes Weiner | 6d3d6aa | 2014-12-10 15:42:50 -0800 | [diff] [blame] | 2383 | static void drain_all_stock(struct mem_cgroup *root_memcg) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2384 | { |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2385 | int cpu, curcpu; |
Michal Hocko | d38144b | 2011-07-26 16:08:28 -0700 | [diff] [blame] | 2386 | |
Johannes Weiner | 6d3d6aa | 2014-12-10 15:42:50 -0800 | [diff] [blame] | 2387 | /* If someone's already draining, avoid adding running more workers. */ |
| 2388 | if (!mutex_trylock(&percpu_charge_mutex)) |
| 2389 | return; |
Michal Hocko | 72f0184 | 2017-10-03 16:14:53 -0700 | [diff] [blame] | 2390 | /* |
| 2391 | * Notify other cpus that system-wide "drain" is running |
| 2392 | * We do not care about races with the cpu hotplug because cpu down |
| 2393 | * as well as workers from this path always operate on the local |
| 2394 | * per-cpu data. CPU up doesn't touch memcg_stock at all. |
| 2395 | */ |
Sebastian Andrzej Siewior | 0790ed6 | 2022-03-22 14:40:50 -0700 | [diff] [blame] | 2396 | migrate_disable(); |
| 2397 | curcpu = smp_processor_id(); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2398 | for_each_online_cpu(cpu) { |
| 2399 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 2400 | struct mem_cgroup *memcg; |
Roman Gushchin | e1a366b | 2019-09-23 15:34:58 -0700 | [diff] [blame] | 2401 | bool flush = false; |
KAMEZAWA Hiroyuki | 26fe616 | 2011-06-15 15:08:45 -0700 | [diff] [blame] | 2402 | |
Roman Gushchin | e1a366b | 2019-09-23 15:34:58 -0700 | [diff] [blame] | 2403 | rcu_read_lock(); |
Roman Gushchin | f785a8f | 2023-05-02 09:08:39 -0700 | [diff] [blame] | 2404 | memcg = READ_ONCE(stock->cached); |
Roman Gushchin | e1a366b | 2019-09-23 15:34:58 -0700 | [diff] [blame] | 2405 | if (memcg && stock->nr_pages && |
| 2406 | mem_cgroup_is_descendant(memcg, root_memcg)) |
| 2407 | flush = true; |
Miaohe Lin | 27fb095 | 2021-09-02 14:55:59 -0700 | [diff] [blame] | 2408 | else if (obj_stock_flush_required(stock, root_memcg)) |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 2409 | flush = true; |
Roman Gushchin | e1a366b | 2019-09-23 15:34:58 -0700 | [diff] [blame] | 2410 | rcu_read_unlock(); |
| 2411 | |
| 2412 | if (flush && |
| 2413 | !test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) { |
Michal Hocko | d1a05b6 | 2011-07-26 16:08:27 -0700 | [diff] [blame] | 2414 | if (cpu == curcpu) |
| 2415 | drain_local_stock(&stock->work); |
Michal Hocko | 6a79269 | 2023-03-17 14:44:48 +0100 | [diff] [blame] | 2416 | else if (!cpu_is_isolated(cpu)) |
Michal Hocko | d1a05b6 | 2011-07-26 16:08:27 -0700 | [diff] [blame] | 2417 | schedule_work_on(cpu, &stock->work); |
| 2418 | } |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2419 | } |
Sebastian Andrzej Siewior | 0790ed6 | 2022-03-22 14:40:50 -0700 | [diff] [blame] | 2420 | migrate_enable(); |
Michal Hocko | 9f50fad | 2011-08-09 11:56:26 +0200 | [diff] [blame] | 2421 | mutex_unlock(&percpu_charge_mutex); |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2422 | } |
| 2423 | |
Sebastian Andrzej Siewior | 308167f | 2016-11-03 15:49:59 +0100 | [diff] [blame] | 2424 | static int memcg_hotplug_cpu_dead(unsigned int cpu) |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2425 | { |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2426 | struct memcg_stock_pcp *stock; |
| 2427 | |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2428 | stock = &per_cpu(memcg_stock, cpu); |
| 2429 | drain_stock(stock); |
Johannes Weiner | a983b5e | 2018-01-31 16:16:45 -0800 | [diff] [blame] | 2430 | |
Sebastian Andrzej Siewior | 308167f | 2016-11-03 15:49:59 +0100 | [diff] [blame] | 2431 | return 0; |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2432 | } |
| 2433 | |
Chris Down | b3ff929 | 2020-08-06 23:21:54 -0700 | [diff] [blame] | 2434 | static unsigned long reclaim_high(struct mem_cgroup *memcg, |
| 2435 | unsigned int nr_pages, |
| 2436 | gfp_t gfp_mask) |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 2437 | { |
Chris Down | b3ff929 | 2020-08-06 23:21:54 -0700 | [diff] [blame] | 2438 | unsigned long nr_reclaimed = 0; |
| 2439 | |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 2440 | do { |
Johannes Weiner | e22c6ed | 2020-08-06 23:22:15 -0700 | [diff] [blame] | 2441 | unsigned long pflags; |
| 2442 | |
Jakub Kicinski | d1663a9 | 2020-06-01 21:49:49 -0700 | [diff] [blame] | 2443 | if (page_counter_read(&memcg->memory) <= |
| 2444 | READ_ONCE(memcg->memory.high)) |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 2445 | continue; |
Johannes Weiner | e22c6ed | 2020-08-06 23:22:15 -0700 | [diff] [blame] | 2446 | |
Johannes Weiner | e27be24 | 2018-04-10 16:29:45 -0700 | [diff] [blame] | 2447 | memcg_memory_event(memcg, MEMCG_HIGH); |
Johannes Weiner | e22c6ed | 2020-08-06 23:22:15 -0700 | [diff] [blame] | 2448 | |
| 2449 | psi_memstall_enter(&pflags); |
Chris Down | b3ff929 | 2020-08-06 23:21:54 -0700 | [diff] [blame] | 2450 | nr_reclaimed += try_to_free_mem_cgroup_pages(memcg, nr_pages, |
Yosry Ahmed | 73b73ba | 2022-07-14 06:49:18 +0000 | [diff] [blame] | 2451 | gfp_mask, |
Michal Hocko | 55ab834 | 2022-12-16 10:46:33 +0100 | [diff] [blame] | 2452 | MEMCG_RECLAIM_MAY_SWAP); |
Johannes Weiner | e22c6ed | 2020-08-06 23:22:15 -0700 | [diff] [blame] | 2453 | psi_memstall_leave(&pflags); |
Chris Down | 4bf1730 | 2020-04-06 20:03:30 -0700 | [diff] [blame] | 2454 | } while ((memcg = parent_mem_cgroup(memcg)) && |
| 2455 | !mem_cgroup_is_root(memcg)); |
Chris Down | b3ff929 | 2020-08-06 23:21:54 -0700 | [diff] [blame] | 2456 | |
| 2457 | return nr_reclaimed; |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | static void high_work_func(struct work_struct *work) |
| 2461 | { |
| 2462 | struct mem_cgroup *memcg; |
| 2463 | |
| 2464 | memcg = container_of(work, struct mem_cgroup, high_work); |
Johannes Weiner | a983b5e | 2018-01-31 16:16:45 -0800 | [diff] [blame] | 2465 | reclaim_high(memcg, MEMCG_CHARGE_BATCH, GFP_KERNEL); |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 2466 | } |
| 2467 | |
Tejun Heo | b23afb9 | 2015-11-05 18:46:11 -0800 | [diff] [blame] | 2468 | /* |
Chris Down | 0e4b01d | 2019-09-23 15:34:55 -0700 | [diff] [blame] | 2469 | * Clamp the maximum sleep time per allocation batch to 2 seconds. This is |
| 2470 | * enough to still cause a significant slowdown in most cases, while still |
| 2471 | * allowing diagnostics and tracing to proceed without becoming stuck. |
| 2472 | */ |
| 2473 | #define MEMCG_MAX_HIGH_DELAY_JIFFIES (2UL*HZ) |
| 2474 | |
| 2475 | /* |
| 2476 | * When calculating the delay, we use these either side of the exponentiation to |
| 2477 | * maintain precision and scale to a reasonable number of jiffies (see the table |
| 2478 | * below. |
| 2479 | * |
| 2480 | * - MEMCG_DELAY_PRECISION_SHIFT: Extra precision bits while translating the |
| 2481 | * overage ratio to a delay. |
Randy Dunlap | ac5ddd0 | 2020-08-11 18:33:02 -0700 | [diff] [blame] | 2482 | * - MEMCG_DELAY_SCALING_SHIFT: The number of bits to scale down the |
Chris Down | 0e4b01d | 2019-09-23 15:34:55 -0700 | [diff] [blame] | 2483 | * proposed penalty in order to reduce to a reasonable number of jiffies, and |
| 2484 | * to produce a reasonable delay curve. |
| 2485 | * |
| 2486 | * MEMCG_DELAY_SCALING_SHIFT just happens to be a number that produces a |
| 2487 | * reasonable delay curve compared to precision-adjusted overage, not |
| 2488 | * penalising heavily at first, but still making sure that growth beyond the |
| 2489 | * limit penalises misbehaviour cgroups by slowing them down exponentially. For |
| 2490 | * example, with a high of 100 megabytes: |
| 2491 | * |
| 2492 | * +-------+------------------------+ |
| 2493 | * | usage | time to allocate in ms | |
| 2494 | * +-------+------------------------+ |
| 2495 | * | 100M | 0 | |
| 2496 | * | 101M | 6 | |
| 2497 | * | 102M | 25 | |
| 2498 | * | 103M | 57 | |
| 2499 | * | 104M | 102 | |
| 2500 | * | 105M | 159 | |
| 2501 | * | 106M | 230 | |
| 2502 | * | 107M | 313 | |
| 2503 | * | 108M | 409 | |
| 2504 | * | 109M | 518 | |
| 2505 | * | 110M | 639 | |
| 2506 | * | 111M | 774 | |
| 2507 | * | 112M | 921 | |
| 2508 | * | 113M | 1081 | |
| 2509 | * | 114M | 1254 | |
| 2510 | * | 115M | 1439 | |
| 2511 | * | 116M | 1638 | |
| 2512 | * | 117M | 1849 | |
| 2513 | * | 118M | 2000 | |
| 2514 | * | 119M | 2000 | |
| 2515 | * | 120M | 2000 | |
| 2516 | * +-------+------------------------+ |
| 2517 | */ |
| 2518 | #define MEMCG_DELAY_PRECISION_SHIFT 20 |
| 2519 | #define MEMCG_DELAY_SCALING_SHIFT 14 |
| 2520 | |
Jakub Kicinski | 8a5dbc6 | 2020-06-01 21:49:42 -0700 | [diff] [blame] | 2521 | static u64 calculate_overage(unsigned long usage, unsigned long high) |
| 2522 | { |
| 2523 | u64 overage; |
| 2524 | |
| 2525 | if (usage <= high) |
| 2526 | return 0; |
| 2527 | |
| 2528 | /* |
| 2529 | * Prevent division by 0 in overage calculation by acting as if |
| 2530 | * it was a threshold of 1 page |
| 2531 | */ |
| 2532 | high = max(high, 1UL); |
| 2533 | |
| 2534 | overage = usage - high; |
| 2535 | overage <<= MEMCG_DELAY_PRECISION_SHIFT; |
| 2536 | return div64_u64(overage, high); |
| 2537 | } |
| 2538 | |
| 2539 | static u64 mem_find_max_overage(struct mem_cgroup *memcg) |
| 2540 | { |
| 2541 | u64 overage, max_overage = 0; |
| 2542 | |
| 2543 | do { |
| 2544 | overage = calculate_overage(page_counter_read(&memcg->memory), |
Jakub Kicinski | d1663a9 | 2020-06-01 21:49:49 -0700 | [diff] [blame] | 2545 | READ_ONCE(memcg->memory.high)); |
Jakub Kicinski | 8a5dbc6 | 2020-06-01 21:49:42 -0700 | [diff] [blame] | 2546 | max_overage = max(overage, max_overage); |
| 2547 | } while ((memcg = parent_mem_cgroup(memcg)) && |
| 2548 | !mem_cgroup_is_root(memcg)); |
| 2549 | |
| 2550 | return max_overage; |
| 2551 | } |
| 2552 | |
Jakub Kicinski | 4b82ab4 | 2020-06-01 21:49:52 -0700 | [diff] [blame] | 2553 | static u64 swap_find_max_overage(struct mem_cgroup *memcg) |
| 2554 | { |
| 2555 | u64 overage, max_overage = 0; |
| 2556 | |
| 2557 | do { |
| 2558 | overage = calculate_overage(page_counter_read(&memcg->swap), |
| 2559 | READ_ONCE(memcg->swap.high)); |
| 2560 | if (overage) |
| 2561 | memcg_memory_event(memcg, MEMCG_SWAP_HIGH); |
| 2562 | max_overage = max(overage, max_overage); |
| 2563 | } while ((memcg = parent_mem_cgroup(memcg)) && |
| 2564 | !mem_cgroup_is_root(memcg)); |
| 2565 | |
| 2566 | return max_overage; |
| 2567 | } |
| 2568 | |
Chris Down | 0e4b01d | 2019-09-23 15:34:55 -0700 | [diff] [blame] | 2569 | /* |
Chris Down | e26733e | 2020-03-21 18:22:23 -0700 | [diff] [blame] | 2570 | * Get the number of jiffies that we should penalise a mischievous cgroup which |
| 2571 | * is exceeding its memory.high by checking both it and its ancestors. |
Tejun Heo | b23afb9 | 2015-11-05 18:46:11 -0800 | [diff] [blame] | 2572 | */ |
Chris Down | e26733e | 2020-03-21 18:22:23 -0700 | [diff] [blame] | 2573 | static unsigned long calculate_high_delay(struct mem_cgroup *memcg, |
Jakub Kicinski | 8a5dbc6 | 2020-06-01 21:49:42 -0700 | [diff] [blame] | 2574 | unsigned int nr_pages, |
| 2575 | u64 max_overage) |
Tejun Heo | b23afb9 | 2015-11-05 18:46:11 -0800 | [diff] [blame] | 2576 | { |
Chris Down | e26733e | 2020-03-21 18:22:23 -0700 | [diff] [blame] | 2577 | unsigned long penalty_jiffies; |
Chris Down | e26733e | 2020-03-21 18:22:23 -0700 | [diff] [blame] | 2578 | |
| 2579 | if (!max_overage) |
| 2580 | return 0; |
Chris Down | 0e4b01d | 2019-09-23 15:34:55 -0700 | [diff] [blame] | 2581 | |
| 2582 | /* |
Chris Down | 0e4b01d | 2019-09-23 15:34:55 -0700 | [diff] [blame] | 2583 | * We use overage compared to memory.high to calculate the number of |
| 2584 | * jiffies to sleep (penalty_jiffies). Ideally this value should be |
| 2585 | * fairly lenient on small overages, and increasingly harsh when the |
| 2586 | * memcg in question makes it clear that it has no intention of stopping |
| 2587 | * its crazy behaviour, so we exponentially increase the delay based on |
| 2588 | * overage amount. |
| 2589 | */ |
Chris Down | e26733e | 2020-03-21 18:22:23 -0700 | [diff] [blame] | 2590 | penalty_jiffies = max_overage * max_overage * HZ; |
| 2591 | penalty_jiffies >>= MEMCG_DELAY_PRECISION_SHIFT; |
| 2592 | penalty_jiffies >>= MEMCG_DELAY_SCALING_SHIFT; |
Chris Down | 0e4b01d | 2019-09-23 15:34:55 -0700 | [diff] [blame] | 2593 | |
| 2594 | /* |
| 2595 | * Factor in the task's own contribution to the overage, such that four |
| 2596 | * N-sized allocations are throttled approximately the same as one |
| 2597 | * 4N-sized allocation. |
| 2598 | * |
| 2599 | * MEMCG_CHARGE_BATCH pages is nominal, so work out how much smaller or |
| 2600 | * larger the current charge patch is than that. |
| 2601 | */ |
Jakub Kicinski | ff144e6 | 2020-06-01 21:49:45 -0700 | [diff] [blame] | 2602 | return penalty_jiffies * nr_pages / MEMCG_CHARGE_BATCH; |
Chris Down | e26733e | 2020-03-21 18:22:23 -0700 | [diff] [blame] | 2603 | } |
| 2604 | |
| 2605 | /* |
| 2606 | * Scheduled by try_charge() to be executed from the userland return path |
| 2607 | * and reclaims memory over the high limit. |
| 2608 | */ |
Johannes Weiner | 9ea9cb0 | 2023-09-14 11:21:39 -0400 | [diff] [blame] | 2609 | void mem_cgroup_handle_over_high(gfp_t gfp_mask) |
Chris Down | e26733e | 2020-03-21 18:22:23 -0700 | [diff] [blame] | 2610 | { |
| 2611 | unsigned long penalty_jiffies; |
| 2612 | unsigned long pflags; |
Chris Down | b3ff929 | 2020-08-06 23:21:54 -0700 | [diff] [blame] | 2613 | unsigned long nr_reclaimed; |
Chris Down | e26733e | 2020-03-21 18:22:23 -0700 | [diff] [blame] | 2614 | unsigned int nr_pages = current->memcg_nr_pages_over_high; |
Chris Down | d977aa9 | 2020-08-06 23:21:58 -0700 | [diff] [blame] | 2615 | int nr_retries = MAX_RECLAIM_RETRIES; |
Chris Down | e26733e | 2020-03-21 18:22:23 -0700 | [diff] [blame] | 2616 | struct mem_cgroup *memcg; |
Chris Down | b3ff929 | 2020-08-06 23:21:54 -0700 | [diff] [blame] | 2617 | bool in_retry = false; |
Chris Down | e26733e | 2020-03-21 18:22:23 -0700 | [diff] [blame] | 2618 | |
| 2619 | if (likely(!nr_pages)) |
| 2620 | return; |
| 2621 | |
| 2622 | memcg = get_mem_cgroup_from_mm(current->mm); |
Chris Down | e26733e | 2020-03-21 18:22:23 -0700 | [diff] [blame] | 2623 | current->memcg_nr_pages_over_high = 0; |
| 2624 | |
Chris Down | b3ff929 | 2020-08-06 23:21:54 -0700 | [diff] [blame] | 2625 | retry_reclaim: |
| 2626 | /* |
| 2627 | * The allocating task should reclaim at least the batch size, but for |
| 2628 | * subsequent retries we only want to do what's necessary to prevent oom |
| 2629 | * or breaching resource isolation. |
| 2630 | * |
| 2631 | * This is distinct from memory.max or page allocator behaviour because |
| 2632 | * memory.high is currently batched, whereas memory.max and the page |
| 2633 | * allocator run every time an allocation is made. |
| 2634 | */ |
| 2635 | nr_reclaimed = reclaim_high(memcg, |
| 2636 | in_retry ? SWAP_CLUSTER_MAX : nr_pages, |
Johannes Weiner | 9ea9cb0 | 2023-09-14 11:21:39 -0400 | [diff] [blame] | 2637 | gfp_mask); |
Chris Down | b3ff929 | 2020-08-06 23:21:54 -0700 | [diff] [blame] | 2638 | |
Chris Down | e26733e | 2020-03-21 18:22:23 -0700 | [diff] [blame] | 2639 | /* |
| 2640 | * memory.high is breached and reclaim is unable to keep up. Throttle |
| 2641 | * allocators proactively to slow down excessive growth. |
| 2642 | */ |
Jakub Kicinski | 8a5dbc6 | 2020-06-01 21:49:42 -0700 | [diff] [blame] | 2643 | penalty_jiffies = calculate_high_delay(memcg, nr_pages, |
| 2644 | mem_find_max_overage(memcg)); |
Chris Down | 0e4b01d | 2019-09-23 15:34:55 -0700 | [diff] [blame] | 2645 | |
Jakub Kicinski | 4b82ab4 | 2020-06-01 21:49:52 -0700 | [diff] [blame] | 2646 | penalty_jiffies += calculate_high_delay(memcg, nr_pages, |
| 2647 | swap_find_max_overage(memcg)); |
| 2648 | |
Chris Down | 0e4b01d | 2019-09-23 15:34:55 -0700 | [diff] [blame] | 2649 | /* |
Jakub Kicinski | ff144e6 | 2020-06-01 21:49:45 -0700 | [diff] [blame] | 2650 | * Clamp the max delay per usermode return so as to still keep the |
| 2651 | * application moving forwards and also permit diagnostics, albeit |
| 2652 | * extremely slowly. |
| 2653 | */ |
| 2654 | penalty_jiffies = min(penalty_jiffies, MEMCG_MAX_HIGH_DELAY_JIFFIES); |
| 2655 | |
| 2656 | /* |
Chris Down | 0e4b01d | 2019-09-23 15:34:55 -0700 | [diff] [blame] | 2657 | * Don't sleep if the amount of jiffies this memcg owes us is so low |
| 2658 | * that it's not even worth doing, in an attempt to be nice to those who |
| 2659 | * go only a small amount over their memory.high value and maybe haven't |
| 2660 | * been aggressively reclaimed enough yet. |
| 2661 | */ |
| 2662 | if (penalty_jiffies <= HZ / 100) |
| 2663 | goto out; |
| 2664 | |
| 2665 | /* |
Chris Down | b3ff929 | 2020-08-06 23:21:54 -0700 | [diff] [blame] | 2666 | * If reclaim is making forward progress but we're still over |
| 2667 | * memory.high, we want to encourage that rather than doing allocator |
| 2668 | * throttling. |
| 2669 | */ |
| 2670 | if (nr_reclaimed || nr_retries--) { |
| 2671 | in_retry = true; |
| 2672 | goto retry_reclaim; |
| 2673 | } |
| 2674 | |
| 2675 | /* |
Chris Down | 0e4b01d | 2019-09-23 15:34:55 -0700 | [diff] [blame] | 2676 | * If we exit early, we're guaranteed to die (since |
| 2677 | * schedule_timeout_killable sets TASK_KILLABLE). This means we don't |
| 2678 | * need to account for any ill-begotten jiffies to pay them off later. |
| 2679 | */ |
| 2680 | psi_memstall_enter(&pflags); |
| 2681 | schedule_timeout_killable(penalty_jiffies); |
| 2682 | psi_memstall_leave(&pflags); |
| 2683 | |
| 2684 | out: |
| 2685 | css_put(&memcg->css); |
Tejun Heo | b23afb9 | 2015-11-05 18:46:11 -0800 | [diff] [blame] | 2686 | } |
| 2687 | |
Muchun Song | c5c8b16 | 2021-06-28 19:37:44 -0700 | [diff] [blame] | 2688 | static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, |
| 2689 | unsigned int nr_pages) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2690 | { |
Johannes Weiner | a983b5e | 2018-01-31 16:16:45 -0800 | [diff] [blame] | 2691 | unsigned int batch = max(MEMCG_CHARGE_BATCH, nr_pages); |
Chris Down | d977aa9 | 2020-08-06 23:21:58 -0700 | [diff] [blame] | 2692 | int nr_retries = MAX_RECLAIM_RETRIES; |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2693 | struct mem_cgroup *mem_over_limit; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2694 | struct page_counter *counter; |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2695 | unsigned long nr_reclaimed; |
Vasily Averin | a4ebf1b | 2021-11-05 13:38:09 -0700 | [diff] [blame] | 2696 | bool passed_oom = false; |
Yosry Ahmed | 73b73ba | 2022-07-14 06:49:18 +0000 | [diff] [blame] | 2697 | unsigned int reclaim_options = MEMCG_RECLAIM_MAY_SWAP; |
Johannes Weiner | b70a2a2 | 2014-10-09 15:28:56 -0700 | [diff] [blame] | 2698 | bool drained = false; |
Roman Gushchin | d6e103a | 2022-07-01 20:35:21 -0700 | [diff] [blame] | 2699 | bool raised_max_event = false; |
Johannes Weiner | e22c6ed | 2020-08-06 23:22:15 -0700 | [diff] [blame] | 2700 | unsigned long pflags; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 2701 | |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2702 | retry: |
Michal Hocko | b6b6cc7 | 2014-04-07 15:37:44 -0700 | [diff] [blame] | 2703 | if (consume_stock(memcg, nr_pages)) |
Tejun Heo | 10d53c7 | 2015-11-05 18:46:17 -0800 | [diff] [blame] | 2704 | return 0; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2705 | |
Johannes Weiner | 7941d21 | 2016-01-14 15:21:23 -0800 | [diff] [blame] | 2706 | if (!do_memsw_account() || |
Johannes Weiner | 6071ca5 | 2015-11-05 18:50:26 -0800 | [diff] [blame] | 2707 | page_counter_try_charge(&memcg->memsw, batch, &counter)) { |
| 2708 | if (page_counter_try_charge(&memcg->memory, batch, &counter)) |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2709 | goto done_restock; |
Johannes Weiner | 7941d21 | 2016-01-14 15:21:23 -0800 | [diff] [blame] | 2710 | if (do_memsw_account()) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2711 | page_counter_uncharge(&memcg->memsw, batch); |
| 2712 | mem_over_limit = mem_cgroup_from_counter(counter, memory); |
Johannes Weiner | 3fbe724 | 2014-10-09 15:28:54 -0700 | [diff] [blame] | 2713 | } else { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2714 | mem_over_limit = mem_cgroup_from_counter(counter, memsw); |
Yosry Ahmed | 73b73ba | 2022-07-14 06:49:18 +0000 | [diff] [blame] | 2715 | reclaim_options &= ~MEMCG_RECLAIM_MAY_SWAP; |
Johannes Weiner | 3fbe724 | 2014-10-09 15:28:54 -0700 | [diff] [blame] | 2716 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2717 | |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2718 | if (batch > nr_pages) { |
| 2719 | batch = nr_pages; |
| 2720 | goto retry; |
| 2721 | } |
KAMEZAWA Hiroyuki | cdec2e4 | 2009-12-15 16:47:08 -0800 | [diff] [blame] | 2722 | |
Johannes Weiner | 06b078fc0 | 2014-08-06 16:05:44 -0700 | [diff] [blame] | 2723 | /* |
Johannes Weiner | 89a2848 | 2016-10-27 17:46:56 -0700 | [diff] [blame] | 2724 | * Prevent unbounded recursion when reclaim operations need to |
| 2725 | * allocate memory. This might exceed the limits temporarily, |
| 2726 | * but we prefer facilitating memory reclaim and getting back |
| 2727 | * under the limit over triggering OOM kills in these cases. |
| 2728 | */ |
| 2729 | if (unlikely(current->flags & PF_MEMALLOC)) |
| 2730 | goto force; |
| 2731 | |
Johannes Weiner | 06b078fc0 | 2014-08-06 16:05:44 -0700 | [diff] [blame] | 2732 | if (unlikely(task_in_memcg_oom(current))) |
| 2733 | goto nomem; |
| 2734 | |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 2735 | if (!gfpflags_allow_blocking(gfp_mask)) |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2736 | goto nomem; |
KAMEZAWA Hiroyuki | 4b53433 | 2010-08-10 18:02:57 -0700 | [diff] [blame] | 2737 | |
Johannes Weiner | e27be24 | 2018-04-10 16:29:45 -0700 | [diff] [blame] | 2738 | memcg_memory_event(mem_over_limit, MEMCG_MAX); |
Roman Gushchin | d6e103a | 2022-07-01 20:35:21 -0700 | [diff] [blame] | 2739 | raised_max_event = true; |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 2740 | |
Johannes Weiner | e22c6ed | 2020-08-06 23:22:15 -0700 | [diff] [blame] | 2741 | psi_memstall_enter(&pflags); |
Johannes Weiner | b70a2a2 | 2014-10-09 15:28:56 -0700 | [diff] [blame] | 2742 | nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages, |
Michal Hocko | 55ab834 | 2022-12-16 10:46:33 +0100 | [diff] [blame] | 2743 | gfp_mask, reclaim_options); |
Johannes Weiner | e22c6ed | 2020-08-06 23:22:15 -0700 | [diff] [blame] | 2744 | psi_memstall_leave(&pflags); |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2745 | |
Johannes Weiner | 61e02c7 | 2014-08-06 16:08:16 -0700 | [diff] [blame] | 2746 | if (mem_cgroup_margin(mem_over_limit) >= nr_pages) |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2747 | goto retry; |
Johannes Weiner | 28c34c2 | 2014-08-06 16:05:47 -0700 | [diff] [blame] | 2748 | |
Johannes Weiner | b70a2a2 | 2014-10-09 15:28:56 -0700 | [diff] [blame] | 2749 | if (!drained) { |
Johannes Weiner | 6d3d6aa | 2014-12-10 15:42:50 -0800 | [diff] [blame] | 2750 | drain_all_stock(mem_over_limit); |
Johannes Weiner | b70a2a2 | 2014-10-09 15:28:56 -0700 | [diff] [blame] | 2751 | drained = true; |
| 2752 | goto retry; |
| 2753 | } |
| 2754 | |
Johannes Weiner | 28c34c2 | 2014-08-06 16:05:47 -0700 | [diff] [blame] | 2755 | if (gfp_mask & __GFP_NORETRY) |
| 2756 | goto nomem; |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2757 | /* |
| 2758 | * Even though the limit is exceeded at this point, reclaim |
| 2759 | * may have been able to free some pages. Retry the charge |
| 2760 | * before killing the task. |
| 2761 | * |
| 2762 | * Only for regular pages, though: huge pages are rather |
| 2763 | * unlikely to succeed so close to the limit, and we fall back |
| 2764 | * to regular pages anyway in case of failure. |
| 2765 | */ |
Johannes Weiner | 61e02c7 | 2014-08-06 16:08:16 -0700 | [diff] [blame] | 2766 | if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER)) |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2767 | goto retry; |
| 2768 | /* |
| 2769 | * At task move, charge accounts can be doubly counted. So, it's |
| 2770 | * better to wait until the end of task_move if something is going on. |
| 2771 | */ |
| 2772 | if (mem_cgroup_wait_acct_move(mem_over_limit)) |
| 2773 | goto retry; |
| 2774 | |
Johannes Weiner | 9b13061 | 2014-08-06 16:05:51 -0700 | [diff] [blame] | 2775 | if (nr_retries--) |
| 2776 | goto retry; |
| 2777 | |
Shakeel Butt | 38d3849 | 2019-07-11 20:55:48 -0700 | [diff] [blame] | 2778 | if (gfp_mask & __GFP_RETRY_MAYFAIL) |
Michal Hocko | 29ef680 | 2018-08-17 15:47:11 -0700 | [diff] [blame] | 2779 | goto nomem; |
| 2780 | |
Vasily Averin | a4ebf1b | 2021-11-05 13:38:09 -0700 | [diff] [blame] | 2781 | /* Avoid endless loop for tasks bypassed by the oom killer */ |
| 2782 | if (passed_oom && task_is_dying()) |
| 2783 | goto nomem; |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2784 | |
Michal Hocko | 29ef680 | 2018-08-17 15:47:11 -0700 | [diff] [blame] | 2785 | /* |
| 2786 | * keep retrying as long as the memcg oom killer is able to make |
| 2787 | * a forward progress or bypass the charge if the oom killer |
| 2788 | * couldn't make any progress. |
| 2789 | */ |
Shakeel Butt | becdf89 | 2022-03-22 14:40:19 -0700 | [diff] [blame] | 2790 | if (mem_cgroup_oom(mem_over_limit, gfp_mask, |
| 2791 | get_order(nr_pages * PAGE_SIZE))) { |
Vasily Averin | a4ebf1b | 2021-11-05 13:38:09 -0700 | [diff] [blame] | 2792 | passed_oom = true; |
Chris Down | d977aa9 | 2020-08-06 23:21:58 -0700 | [diff] [blame] | 2793 | nr_retries = MAX_RECLAIM_RETRIES; |
Michal Hocko | 29ef680 | 2018-08-17 15:47:11 -0700 | [diff] [blame] | 2794 | goto retry; |
Michal Hocko | 29ef680 | 2018-08-17 15:47:11 -0700 | [diff] [blame] | 2795 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2796 | nomem: |
Shakeel Butt | 1461e8c | 2022-03-22 14:40:22 -0700 | [diff] [blame] | 2797 | /* |
| 2798 | * Memcg doesn't have a dedicated reserve for atomic |
| 2799 | * allocations. But like the global atomic pool, we need to |
| 2800 | * put the burden of reclaim on regular allocation requests |
| 2801 | * and let these go through as privileged allocations. |
| 2802 | */ |
| 2803 | if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH))) |
Johannes Weiner | 3168ecb | 2013-10-31 16:34:13 -0700 | [diff] [blame] | 2804 | return -ENOMEM; |
Tejun Heo | 10d53c7 | 2015-11-05 18:46:17 -0800 | [diff] [blame] | 2805 | force: |
| 2806 | /* |
Roman Gushchin | d6e103a | 2022-07-01 20:35:21 -0700 | [diff] [blame] | 2807 | * If the allocation has to be enforced, don't forget to raise |
| 2808 | * a MEMCG_MAX event. |
| 2809 | */ |
| 2810 | if (!raised_max_event) |
| 2811 | memcg_memory_event(mem_over_limit, MEMCG_MAX); |
| 2812 | |
| 2813 | /* |
Tejun Heo | 10d53c7 | 2015-11-05 18:46:17 -0800 | [diff] [blame] | 2814 | * The allocation either can't fail or will lead to more memory |
| 2815 | * being freed very soon. Allow memory usage go over the limit |
| 2816 | * temporarily by force charging it. |
| 2817 | */ |
| 2818 | page_counter_charge(&memcg->memory, nr_pages); |
Johannes Weiner | 7941d21 | 2016-01-14 15:21:23 -0800 | [diff] [blame] | 2819 | if (do_memsw_account()) |
Tejun Heo | 10d53c7 | 2015-11-05 18:46:17 -0800 | [diff] [blame] | 2820 | page_counter_charge(&memcg->memsw, nr_pages); |
Tejun Heo | 10d53c7 | 2015-11-05 18:46:17 -0800 | [diff] [blame] | 2821 | |
| 2822 | return 0; |
Johannes Weiner | 6539cc0 | 2014-08-06 16:05:42 -0700 | [diff] [blame] | 2823 | |
| 2824 | done_restock: |
| 2825 | if (batch > nr_pages) |
| 2826 | refill_stock(memcg, batch - nr_pages); |
Tejun Heo | b23afb9 | 2015-11-05 18:46:11 -0800 | [diff] [blame] | 2827 | |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 2828 | /* |
Tejun Heo | b23afb9 | 2015-11-05 18:46:11 -0800 | [diff] [blame] | 2829 | * If the hierarchy is above the normal consumption range, schedule |
| 2830 | * reclaim on returning to userland. We can perform reclaim here |
Mel Gorman | 71baba4 | 2015-11-06 16:28:28 -0800 | [diff] [blame] | 2831 | * if __GFP_RECLAIM but let's always punt for simplicity and so that |
Tejun Heo | b23afb9 | 2015-11-05 18:46:11 -0800 | [diff] [blame] | 2832 | * GFP_KERNEL can consistently be used during reclaim. @memcg is |
| 2833 | * not recorded as it most likely matches current's and won't |
| 2834 | * change in the meantime. As high limit is checked again before |
| 2835 | * reclaim, the cost of mismatch is negligible. |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 2836 | */ |
| 2837 | do { |
Jakub Kicinski | 4b82ab4 | 2020-06-01 21:49:52 -0700 | [diff] [blame] | 2838 | bool mem_high, swap_high; |
| 2839 | |
| 2840 | mem_high = page_counter_read(&memcg->memory) > |
| 2841 | READ_ONCE(memcg->memory.high); |
| 2842 | swap_high = page_counter_read(&memcg->swap) > |
| 2843 | READ_ONCE(memcg->swap.high); |
| 2844 | |
| 2845 | /* Don't bother a random interrupted task */ |
Shakeel Butt | 086f694 | 2022-03-22 14:40:07 -0700 | [diff] [blame] | 2846 | if (!in_task()) { |
Jakub Kicinski | 4b82ab4 | 2020-06-01 21:49:52 -0700 | [diff] [blame] | 2847 | if (mem_high) { |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 2848 | schedule_work(&memcg->high_work); |
| 2849 | break; |
| 2850 | } |
Jakub Kicinski | 4b82ab4 | 2020-06-01 21:49:52 -0700 | [diff] [blame] | 2851 | continue; |
| 2852 | } |
| 2853 | |
| 2854 | if (mem_high || swap_high) { |
| 2855 | /* |
| 2856 | * The allocating tasks in this cgroup will need to do |
| 2857 | * reclaim or be throttled to prevent further growth |
| 2858 | * of the memory or swap footprints. |
| 2859 | * |
| 2860 | * Target some best-effort fairness between the tasks, |
| 2861 | * and distribute reclaim work and delay penalties |
| 2862 | * based on how much each task is actually allocating. |
| 2863 | */ |
Vladimir Davydov | 9516a18 | 2015-12-11 13:40:24 -0800 | [diff] [blame] | 2864 | current->memcg_nr_pages_over_high += batch; |
Tejun Heo | b23afb9 | 2015-11-05 18:46:11 -0800 | [diff] [blame] | 2865 | set_notify_resume(current); |
| 2866 | break; |
| 2867 | } |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 2868 | } while ((memcg = parent_mem_cgroup(memcg))); |
Tejun Heo | 10d53c7 | 2015-11-05 18:46:17 -0800 | [diff] [blame] | 2869 | |
Shakeel Butt | c9afe31 | 2022-03-22 14:40:28 -0700 | [diff] [blame] | 2870 | if (current->memcg_nr_pages_over_high > MEMCG_CHARGE_BATCH && |
| 2871 | !(current->flags & PF_MEMALLOC) && |
| 2872 | gfpflags_allow_blocking(gfp_mask)) { |
Johannes Weiner | 9ea9cb0 | 2023-09-14 11:21:39 -0400 | [diff] [blame] | 2873 | mem_cgroup_handle_over_high(gfp_mask); |
Shakeel Butt | c9afe31 | 2022-03-22 14:40:28 -0700 | [diff] [blame] | 2874 | } |
Tejun Heo | 10d53c7 | 2015-11-05 18:46:17 -0800 | [diff] [blame] | 2875 | return 0; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 2876 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2877 | |
Muchun Song | c5c8b16 | 2021-06-28 19:37:44 -0700 | [diff] [blame] | 2878 | static inline int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, |
| 2879 | unsigned int nr_pages) |
| 2880 | { |
| 2881 | if (mem_cgroup_is_root(memcg)) |
| 2882 | return 0; |
| 2883 | |
| 2884 | return try_charge_memcg(memcg, gfp_mask, nr_pages); |
| 2885 | } |
| 2886 | |
Nhat Pham | 4b56938 | 2023-10-06 11:46:26 -0700 | [diff] [blame] | 2887 | /** |
| 2888 | * mem_cgroup_cancel_charge() - cancel an uncommitted try_charge() call. |
| 2889 | * @memcg: memcg previously charged. |
| 2890 | * @nr_pages: number of pages previously charged. |
| 2891 | */ |
| 2892 | void mem_cgroup_cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages) |
Daisuke Nishimura | a3032a2 | 2009-12-15 16:47:10 -0800 | [diff] [blame] | 2893 | { |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 2894 | if (mem_cgroup_is_root(memcg)) |
| 2895 | return; |
| 2896 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2897 | page_counter_uncharge(&memcg->memory, nr_pages); |
Johannes Weiner | 7941d21 | 2016-01-14 15:21:23 -0800 | [diff] [blame] | 2898 | if (do_memsw_account()) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 2899 | page_counter_uncharge(&memcg->memsw, nr_pages); |
KAMEZAWA Hiroyuki | d01dd17 | 2012-05-29 15:07:03 -0700 | [diff] [blame] | 2900 | } |
| 2901 | |
Matthew Wilcox (Oracle) | 118f287 | 2021-04-29 14:07:12 -0400 | [diff] [blame] | 2902 | static void commit_charge(struct folio *folio, struct mem_cgroup *memcg) |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 2903 | { |
Matthew Wilcox (Oracle) | 118f287 | 2021-04-29 14:07:12 -0400 | [diff] [blame] | 2904 | VM_BUG_ON_FOLIO(folio_memcg(folio), folio); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 2905 | /* |
Alex Shi | a5eb011 | 2020-12-14 19:06:42 -0800 | [diff] [blame] | 2906 | * Any of the following ensures page's memcg stability: |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 2907 | * |
Johannes Weiner | a0b5b41 | 2020-06-03 16:02:27 -0700 | [diff] [blame] | 2908 | * - the page lock |
| 2909 | * - LRU isolation |
Kefeng Wang | 6c77b60 | 2023-06-14 22:36:12 +0800 | [diff] [blame] | 2910 | * - folio_memcg_lock() |
Johannes Weiner | a0b5b41 | 2020-06-03 16:02:27 -0700 | [diff] [blame] | 2911 | * - exclusive reference |
Yu Zhao | 018ee47 | 2022-09-18 02:00:04 -0600 | [diff] [blame] | 2912 | * - mem_cgroup_trylock_pages() |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 2913 | */ |
Matthew Wilcox (Oracle) | 118f287 | 2021-04-29 14:07:12 -0400 | [diff] [blame] | 2914 | folio->memcg_data = (unsigned long)memcg; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 2915 | } |
| 2916 | |
Nhat Pham | 4b56938 | 2023-10-06 11:46:26 -0700 | [diff] [blame] | 2917 | /** |
| 2918 | * mem_cgroup_commit_charge - commit a previously successful try_charge(). |
| 2919 | * @folio: folio to commit the charge to. |
| 2920 | * @memcg: memcg previously charged. |
| 2921 | */ |
| 2922 | void mem_cgroup_commit_charge(struct folio *folio, struct mem_cgroup *memcg) |
| 2923 | { |
| 2924 | css_get(&memcg->css); |
| 2925 | commit_charge(folio, memcg); |
| 2926 | |
| 2927 | local_irq_disable(); |
| 2928 | mem_cgroup_charge_statistics(memcg, folio_nr_pages(folio)); |
| 2929 | memcg_check_events(memcg, folio_nid(folio)); |
| 2930 | local_irq_enable(); |
| 2931 | } |
| 2932 | |
Kirill Tkhai | 84c07d1 | 2018-08-17 15:47:25 -0700 | [diff] [blame] | 2933 | #ifdef CONFIG_MEMCG_KMEM |
Waiman Long | 41eb5df | 2021-06-28 19:37:34 -0700 | [diff] [blame] | 2934 | /* |
| 2935 | * The allocated objcg pointers array is not accounted directly. |
| 2936 | * Moreover, it should not come from DMA buffer and is not readily |
| 2937 | * reclaimable. So those GFP bits should be masked off. |
| 2938 | */ |
Roman Gushchin | 24948e3 | 2023-11-07 09:18:02 -0800 | [diff] [blame] | 2939 | #define OBJCGS_CLEAR_MASK (__GFP_DMA | __GFP_RECLAIMABLE | \ |
| 2940 | __GFP_ACCOUNT | __GFP_NOFAIL) |
Waiman Long | 41eb5df | 2021-06-28 19:37:34 -0700 | [diff] [blame] | 2941 | |
Waiman Long | a7ebf56 | 2021-12-10 14:47:05 -0800 | [diff] [blame] | 2942 | /* |
Waiman Long | a7ebf56 | 2021-12-10 14:47:05 -0800 | [diff] [blame] | 2943 | * mod_objcg_mlstate() may be called with irq enabled, so |
| 2944 | * mod_memcg_lruvec_state() should be used. |
| 2945 | */ |
| 2946 | static inline void mod_objcg_mlstate(struct obj_cgroup *objcg, |
| 2947 | struct pglist_data *pgdat, |
| 2948 | enum node_stat_item idx, int nr) |
| 2949 | { |
| 2950 | struct mem_cgroup *memcg; |
| 2951 | struct lruvec *lruvec; |
| 2952 | |
| 2953 | rcu_read_lock(); |
| 2954 | memcg = obj_cgroup_memcg(objcg); |
| 2955 | lruvec = mem_cgroup_lruvec(memcg, pgdat); |
| 2956 | mod_memcg_lruvec_state(lruvec, idx, nr); |
| 2957 | rcu_read_unlock(); |
| 2958 | } |
| 2959 | |
Vlastimil Babka | 4b5f8d9a | 2021-11-02 22:42:04 +0100 | [diff] [blame] | 2960 | int memcg_alloc_slab_cgroups(struct slab *slab, struct kmem_cache *s, |
| 2961 | gfp_t gfp, bool new_slab) |
Roman Gushchin | 10befea | 2020-08-06 23:21:27 -0700 | [diff] [blame] | 2962 | { |
Vlastimil Babka | 4b5f8d9a | 2021-11-02 22:42:04 +0100 | [diff] [blame] | 2963 | unsigned int objects = objs_per_slab(s, slab); |
Roman Gushchin | 2e9bd48 | 2021-02-24 12:03:11 -0800 | [diff] [blame] | 2964 | unsigned long memcg_data; |
Roman Gushchin | 10befea | 2020-08-06 23:21:27 -0700 | [diff] [blame] | 2965 | void *vec; |
| 2966 | |
Waiman Long | 41eb5df | 2021-06-28 19:37:34 -0700 | [diff] [blame] | 2967 | gfp &= ~OBJCGS_CLEAR_MASK; |
Roman Gushchin | 10befea | 2020-08-06 23:21:27 -0700 | [diff] [blame] | 2968 | vec = kcalloc_node(objects, sizeof(struct obj_cgroup *), gfp, |
Vlastimil Babka | 4b5f8d9a | 2021-11-02 22:42:04 +0100 | [diff] [blame] | 2969 | slab_nid(slab)); |
Roman Gushchin | 10befea | 2020-08-06 23:21:27 -0700 | [diff] [blame] | 2970 | if (!vec) |
| 2971 | return -ENOMEM; |
| 2972 | |
Roman Gushchin | 2e9bd48 | 2021-02-24 12:03:11 -0800 | [diff] [blame] | 2973 | memcg_data = (unsigned long) vec | MEMCG_DATA_OBJCGS; |
Vlastimil Babka | 4b5f8d9a | 2021-11-02 22:42:04 +0100 | [diff] [blame] | 2974 | if (new_slab) { |
Roman Gushchin | 2e9bd48 | 2021-02-24 12:03:11 -0800 | [diff] [blame] | 2975 | /* |
Vlastimil Babka | 4b5f8d9a | 2021-11-02 22:42:04 +0100 | [diff] [blame] | 2976 | * If the slab is brand new and nobody can yet access its |
| 2977 | * memcg_data, no synchronization is required and memcg_data can |
| 2978 | * be simply assigned. |
Roman Gushchin | 2e9bd48 | 2021-02-24 12:03:11 -0800 | [diff] [blame] | 2979 | */ |
Vlastimil Babka | 4b5f8d9a | 2021-11-02 22:42:04 +0100 | [diff] [blame] | 2980 | slab->memcg_data = memcg_data; |
| 2981 | } else if (cmpxchg(&slab->memcg_data, 0, memcg_data)) { |
Roman Gushchin | 2e9bd48 | 2021-02-24 12:03:11 -0800 | [diff] [blame] | 2982 | /* |
Vlastimil Babka | 4b5f8d9a | 2021-11-02 22:42:04 +0100 | [diff] [blame] | 2983 | * If the slab is already in use, somebody can allocate and |
| 2984 | * assign obj_cgroups in parallel. In this case the existing |
Roman Gushchin | 2e9bd48 | 2021-02-24 12:03:11 -0800 | [diff] [blame] | 2985 | * objcg vector should be reused. |
| 2986 | */ |
Roman Gushchin | 10befea | 2020-08-06 23:21:27 -0700 | [diff] [blame] | 2987 | kfree(vec); |
Roman Gushchin | 2e9bd48 | 2021-02-24 12:03:11 -0800 | [diff] [blame] | 2988 | return 0; |
| 2989 | } |
Roman Gushchin | 10befea | 2020-08-06 23:21:27 -0700 | [diff] [blame] | 2990 | |
Roman Gushchin | 2e9bd48 | 2021-02-24 12:03:11 -0800 | [diff] [blame] | 2991 | kmemleak_not_leak(vec); |
Roman Gushchin | 10befea | 2020-08-06 23:21:27 -0700 | [diff] [blame] | 2992 | return 0; |
| 2993 | } |
| 2994 | |
Roman Gushchin | fc4db90 | 2022-06-10 11:03:10 -0700 | [diff] [blame] | 2995 | static __always_inline |
| 2996 | struct mem_cgroup *mem_cgroup_from_obj_folio(struct folio *folio, void *p) |
Roman Gushchin | 8380ce4 | 2020-03-28 19:17:25 -0700 | [diff] [blame] | 2997 | { |
Roman Gushchin | 8380ce4 | 2020-03-28 19:17:25 -0700 | [diff] [blame] | 2998 | /* |
Roman Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame] | 2999 | * Slab objects are accounted individually, not per-page. |
| 3000 | * Memcg membership data for each individual object is saved in |
Vlastimil Babka | 4b5f8d9a | 2021-11-02 22:42:04 +0100 | [diff] [blame] | 3001 | * slab->memcg_data. |
Roman Gushchin | 8380ce4 | 2020-03-28 19:17:25 -0700 | [diff] [blame] | 3002 | */ |
Vlastimil Babka | 4b5f8d9a | 2021-11-02 22:42:04 +0100 | [diff] [blame] | 3003 | if (folio_test_slab(folio)) { |
| 3004 | struct obj_cgroup **objcgs; |
| 3005 | struct slab *slab; |
Roman Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame] | 3006 | unsigned int off; |
| 3007 | |
Vlastimil Babka | 4b5f8d9a | 2021-11-02 22:42:04 +0100 | [diff] [blame] | 3008 | slab = folio_slab(folio); |
| 3009 | objcgs = slab_objcgs(slab); |
| 3010 | if (!objcgs) |
| 3011 | return NULL; |
| 3012 | |
| 3013 | off = obj_to_index(slab->slab_cache, slab, p); |
| 3014 | if (objcgs[off]) |
| 3015 | return obj_cgroup_memcg(objcgs[off]); |
Roman Gushchin | 10befea | 2020-08-06 23:21:27 -0700 | [diff] [blame] | 3016 | |
| 3017 | return NULL; |
Roman Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame] | 3018 | } |
Roman Gushchin | 8380ce4 | 2020-03-28 19:17:25 -0700 | [diff] [blame] | 3019 | |
Roman Gushchin | bcfe06b | 2020-12-01 13:58:27 -0800 | [diff] [blame] | 3020 | /* |
Matthew Wilcox | becacb0 | 2022-12-30 15:08:42 +0800 | [diff] [blame] | 3021 | * folio_memcg_check() is used here, because in theory we can encounter |
Vlastimil Babka | 4b5f8d9a | 2021-11-02 22:42:04 +0100 | [diff] [blame] | 3022 | * a folio where the slab flag has been cleared already, but |
| 3023 | * slab->memcg_data has not been freed yet |
Matthew Wilcox | becacb0 | 2022-12-30 15:08:42 +0800 | [diff] [blame] | 3024 | * folio_memcg_check() will guarantee that a proper memory |
Roman Gushchin | bcfe06b | 2020-12-01 13:58:27 -0800 | [diff] [blame] | 3025 | * cgroup pointer or NULL will be returned. |
| 3026 | */ |
Matthew Wilcox | becacb0 | 2022-12-30 15:08:42 +0800 | [diff] [blame] | 3027 | return folio_memcg_check(folio); |
Roman Gushchin | 8380ce4 | 2020-03-28 19:17:25 -0700 | [diff] [blame] | 3028 | } |
| 3029 | |
Roman Gushchin | fc4db90 | 2022-06-10 11:03:10 -0700 | [diff] [blame] | 3030 | /* |
| 3031 | * Returns a pointer to the memory cgroup to which the kernel object is charged. |
| 3032 | * |
| 3033 | * A passed kernel object can be a slab object, vmalloc object or a generic |
| 3034 | * kernel page, so different mechanisms for getting the memory cgroup pointer |
| 3035 | * should be used. |
| 3036 | * |
| 3037 | * In certain cases (e.g. kernel stacks or large kmallocs with SLUB) the caller |
| 3038 | * can not know for sure how the kernel object is implemented. |
| 3039 | * mem_cgroup_from_obj() can be safely used in such cases. |
| 3040 | * |
| 3041 | * The caller must ensure the memcg lifetime, e.g. by taking rcu_read_lock(), |
| 3042 | * cgroup_mutex, etc. |
| 3043 | */ |
| 3044 | struct mem_cgroup *mem_cgroup_from_obj(void *p) |
| 3045 | { |
| 3046 | struct folio *folio; |
| 3047 | |
| 3048 | if (mem_cgroup_disabled()) |
| 3049 | return NULL; |
| 3050 | |
| 3051 | if (unlikely(is_vmalloc_addr(p))) |
| 3052 | folio = page_folio(vmalloc_to_page(p)); |
| 3053 | else |
| 3054 | folio = virt_to_folio(p); |
| 3055 | |
| 3056 | return mem_cgroup_from_obj_folio(folio, p); |
| 3057 | } |
| 3058 | |
| 3059 | /* |
| 3060 | * Returns a pointer to the memory cgroup to which the kernel object is charged. |
| 3061 | * Similar to mem_cgroup_from_obj(), but faster and not suitable for objects, |
| 3062 | * allocated using vmalloc(). |
| 3063 | * |
| 3064 | * A passed kernel object must be a slab object or a generic kernel page. |
| 3065 | * |
| 3066 | * The caller must ensure the memcg lifetime, e.g. by taking rcu_read_lock(), |
| 3067 | * cgroup_mutex, etc. |
| 3068 | */ |
| 3069 | struct mem_cgroup *mem_cgroup_from_slab_obj(void *p) |
| 3070 | { |
| 3071 | if (mem_cgroup_disabled()) |
| 3072 | return NULL; |
| 3073 | |
| 3074 | return mem_cgroup_from_obj_folio(virt_to_folio(p), p); |
| 3075 | } |
| 3076 | |
Johannes Weiner | f4840cc | 2022-05-19 14:08:53 -0700 | [diff] [blame] | 3077 | static struct obj_cgroup *__get_obj_cgroup_from_memcg(struct mem_cgroup *memcg) |
| 3078 | { |
| 3079 | struct obj_cgroup *objcg = NULL; |
| 3080 | |
Kamalesh Babulal | 7848ed6 | 2022-09-30 19:14:33 +0530 | [diff] [blame] | 3081 | for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) { |
Johannes Weiner | f4840cc | 2022-05-19 14:08:53 -0700 | [diff] [blame] | 3082 | objcg = rcu_dereference(memcg->objcg); |
Roman Gushchin | 7d0715d | 2023-10-19 15:53:41 -0700 | [diff] [blame] | 3083 | if (likely(objcg && obj_cgroup_tryget(objcg))) |
Johannes Weiner | f4840cc | 2022-05-19 14:08:53 -0700 | [diff] [blame] | 3084 | break; |
| 3085 | objcg = NULL; |
| 3086 | } |
| 3087 | return objcg; |
| 3088 | } |
| 3089 | |
Roman Gushchin | 1aacbd3 | 2023-10-19 15:53:42 -0700 | [diff] [blame] | 3090 | static struct obj_cgroup *current_objcg_update(void) |
| 3091 | { |
| 3092 | struct mem_cgroup *memcg; |
| 3093 | struct obj_cgroup *old, *objcg = NULL; |
| 3094 | |
| 3095 | do { |
| 3096 | /* Atomically drop the update bit. */ |
| 3097 | old = xchg(¤t->objcg, NULL); |
| 3098 | if (old) { |
| 3099 | old = (struct obj_cgroup *) |
| 3100 | ((unsigned long)old & ~CURRENT_OBJCG_UPDATE_FLAG); |
| 3101 | if (old) |
| 3102 | obj_cgroup_put(old); |
| 3103 | |
| 3104 | old = NULL; |
| 3105 | } |
| 3106 | |
| 3107 | /* If new objcg is NULL, no reason for the second atomic update. */ |
| 3108 | if (!current->mm || (current->flags & PF_KTHREAD)) |
| 3109 | return NULL; |
| 3110 | |
| 3111 | /* |
| 3112 | * Release the objcg pointer from the previous iteration, |
| 3113 | * if try_cmpxcg() below fails. |
| 3114 | */ |
| 3115 | if (unlikely(objcg)) { |
| 3116 | obj_cgroup_put(objcg); |
| 3117 | objcg = NULL; |
| 3118 | } |
| 3119 | |
| 3120 | /* |
| 3121 | * Obtain the new objcg pointer. The current task can be |
| 3122 | * asynchronously moved to another memcg and the previous |
| 3123 | * memcg can be offlined. So let's get the memcg pointer |
| 3124 | * and try get a reference to objcg under a rcu read lock. |
| 3125 | */ |
| 3126 | |
| 3127 | rcu_read_lock(); |
| 3128 | memcg = mem_cgroup_from_task(current); |
| 3129 | objcg = __get_obj_cgroup_from_memcg(memcg); |
| 3130 | rcu_read_unlock(); |
| 3131 | |
| 3132 | /* |
| 3133 | * Try set up a new objcg pointer atomically. If it |
| 3134 | * fails, it means the update flag was set concurrently, so |
| 3135 | * the whole procedure should be repeated. |
| 3136 | */ |
| 3137 | } while (!try_cmpxchg(¤t->objcg, &old, objcg)); |
| 3138 | |
| 3139 | return objcg; |
| 3140 | } |
| 3141 | |
Roman Gushchin | e86828e | 2023-10-19 15:53:44 -0700 | [diff] [blame] | 3142 | __always_inline struct obj_cgroup *current_obj_cgroup(void) |
| 3143 | { |
| 3144 | struct mem_cgroup *memcg; |
| 3145 | struct obj_cgroup *objcg; |
| 3146 | |
| 3147 | if (in_task()) { |
| 3148 | memcg = current->active_memcg; |
| 3149 | if (unlikely(memcg)) |
| 3150 | goto from_memcg; |
| 3151 | |
| 3152 | objcg = READ_ONCE(current->objcg); |
| 3153 | if (unlikely((unsigned long)objcg & CURRENT_OBJCG_UPDATE_FLAG)) |
| 3154 | objcg = current_objcg_update(); |
| 3155 | /* |
| 3156 | * Objcg reference is kept by the task, so it's safe |
| 3157 | * to use the objcg by the current task. |
| 3158 | */ |
| 3159 | return objcg; |
| 3160 | } |
| 3161 | |
| 3162 | memcg = this_cpu_read(int_active_memcg); |
| 3163 | if (unlikely(memcg)) |
| 3164 | goto from_memcg; |
| 3165 | |
| 3166 | return NULL; |
| 3167 | |
| 3168 | from_memcg: |
| 3169 | for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) { |
| 3170 | /* |
| 3171 | * Memcg pointer is protected by scope (see set_active_memcg()) |
| 3172 | * and is pinning the corresponding objcg, so objcg can't go |
| 3173 | * away and can be used within the scope without any additional |
| 3174 | * protection. |
| 3175 | */ |
| 3176 | objcg = rcu_dereference_check(memcg->objcg, 1); |
| 3177 | if (likely(objcg)) |
| 3178 | break; |
| 3179 | objcg = NULL; |
| 3180 | } |
| 3181 | |
| 3182 | return objcg; |
| 3183 | } |
| 3184 | |
Matthew Wilcox (Oracle) | 074e3e2 | 2023-07-15 05:23:41 +0100 | [diff] [blame] | 3185 | struct obj_cgroup *get_obj_cgroup_from_folio(struct folio *folio) |
Johannes Weiner | f4840cc | 2022-05-19 14:08:53 -0700 | [diff] [blame] | 3186 | { |
| 3187 | struct obj_cgroup *objcg; |
| 3188 | |
Roman Gushchin | f7a449f | 2023-02-13 11:29:22 -0800 | [diff] [blame] | 3189 | if (!memcg_kmem_online()) |
Johannes Weiner | f4840cc | 2022-05-19 14:08:53 -0700 | [diff] [blame] | 3190 | return NULL; |
| 3191 | |
Matthew Wilcox (Oracle) | 074e3e2 | 2023-07-15 05:23:41 +0100 | [diff] [blame] | 3192 | if (folio_memcg_kmem(folio)) { |
| 3193 | objcg = __folio_objcg(folio); |
Johannes Weiner | f4840cc | 2022-05-19 14:08:53 -0700 | [diff] [blame] | 3194 | obj_cgroup_get(objcg); |
| 3195 | } else { |
| 3196 | struct mem_cgroup *memcg; |
| 3197 | |
| 3198 | rcu_read_lock(); |
Matthew Wilcox (Oracle) | 074e3e2 | 2023-07-15 05:23:41 +0100 | [diff] [blame] | 3199 | memcg = __folio_memcg(folio); |
Johannes Weiner | f4840cc | 2022-05-19 14:08:53 -0700 | [diff] [blame] | 3200 | if (memcg) |
| 3201 | objcg = __get_obj_cgroup_from_memcg(memcg); |
| 3202 | else |
| 3203 | objcg = NULL; |
| 3204 | rcu_read_unlock(); |
| 3205 | } |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3206 | return objcg; |
| 3207 | } |
| 3208 | |
Yosry Ahmed | a8c49af | 2022-03-22 14:40:10 -0700 | [diff] [blame] | 3209 | static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages) |
| 3210 | { |
| 3211 | mod_memcg_state(memcg, MEMCG_KMEM, nr_pages); |
| 3212 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) { |
| 3213 | if (nr_pages > 0) |
| 3214 | page_counter_charge(&memcg->kmem, nr_pages); |
| 3215 | else |
| 3216 | page_counter_uncharge(&memcg->kmem, -nr_pages); |
| 3217 | } |
| 3218 | } |
| 3219 | |
| 3220 | |
Muchun Song | f1286fa | 2021-04-29 22:56:55 -0700 | [diff] [blame] | 3221 | /* |
| 3222 | * obj_cgroup_uncharge_pages: uncharge a number of kernel pages from a objcg |
| 3223 | * @objcg: object cgroup to uncharge |
| 3224 | * @nr_pages: number of pages to uncharge |
| 3225 | */ |
Muchun Song | e74d225 | 2021-04-29 22:56:42 -0700 | [diff] [blame] | 3226 | static void obj_cgroup_uncharge_pages(struct obj_cgroup *objcg, |
| 3227 | unsigned int nr_pages) |
| 3228 | { |
| 3229 | struct mem_cgroup *memcg; |
| 3230 | |
| 3231 | memcg = get_mem_cgroup_from_objcg(objcg); |
Muchun Song | f1286fa | 2021-04-29 22:56:55 -0700 | [diff] [blame] | 3232 | |
Yosry Ahmed | a8c49af | 2022-03-22 14:40:10 -0700 | [diff] [blame] | 3233 | memcg_account_kmem(memcg, -nr_pages); |
Muchun Song | f1286fa | 2021-04-29 22:56:55 -0700 | [diff] [blame] | 3234 | refill_stock(memcg, nr_pages); |
| 3235 | |
Muchun Song | e74d225 | 2021-04-29 22:56:42 -0700 | [diff] [blame] | 3236 | css_put(&memcg->css); |
| 3237 | } |
| 3238 | |
Muchun Song | f1286fa | 2021-04-29 22:56:55 -0700 | [diff] [blame] | 3239 | /* |
| 3240 | * obj_cgroup_charge_pages: charge a number of kernel pages to a objcg |
| 3241 | * @objcg: object cgroup to charge |
Vladimir Davydov | 4526477 | 2016-07-26 15:24:21 -0700 | [diff] [blame] | 3242 | * @gfp: reclaim mode |
Roman Gushchin | 92d0510c | 2020-04-01 21:06:49 -0700 | [diff] [blame] | 3243 | * @nr_pages: number of pages to charge |
Vladimir Davydov | 4526477 | 2016-07-26 15:24:21 -0700 | [diff] [blame] | 3244 | * |
| 3245 | * Returns 0 on success, an error code on failure. |
| 3246 | */ |
Muchun Song | f1286fa | 2021-04-29 22:56:55 -0700 | [diff] [blame] | 3247 | static int obj_cgroup_charge_pages(struct obj_cgroup *objcg, gfp_t gfp, |
| 3248 | unsigned int nr_pages) |
Vladimir Davydov | f3ccb2c4 | 2015-11-05 18:49:01 -0800 | [diff] [blame] | 3249 | { |
Muchun Song | f1286fa | 2021-04-29 22:56:55 -0700 | [diff] [blame] | 3250 | struct mem_cgroup *memcg; |
Johannes Weiner | 6071ca5 | 2015-11-05 18:50:26 -0800 | [diff] [blame] | 3251 | int ret; |
Vladimir Davydov | f3ccb2c4 | 2015-11-05 18:49:01 -0800 | [diff] [blame] | 3252 | |
Muchun Song | f1286fa | 2021-04-29 22:56:55 -0700 | [diff] [blame] | 3253 | memcg = get_mem_cgroup_from_objcg(objcg); |
| 3254 | |
Muchun Song | c5c8b16 | 2021-06-28 19:37:44 -0700 | [diff] [blame] | 3255 | ret = try_charge_memcg(memcg, gfp, nr_pages); |
Johannes Weiner | 52c29b0 | 2016-01-20 15:02:35 -0800 | [diff] [blame] | 3256 | if (ret) |
Muchun Song | f1286fa | 2021-04-29 22:56:55 -0700 | [diff] [blame] | 3257 | goto out; |
Johannes Weiner | 52c29b0 | 2016-01-20 15:02:35 -0800 | [diff] [blame] | 3258 | |
Yosry Ahmed | a8c49af | 2022-03-22 14:40:10 -0700 | [diff] [blame] | 3259 | memcg_account_kmem(memcg, nr_pages); |
Muchun Song | f1286fa | 2021-04-29 22:56:55 -0700 | [diff] [blame] | 3260 | out: |
| 3261 | css_put(&memcg->css); |
Vladimir Davydov | f3ccb2c4 | 2015-11-05 18:49:01 -0800 | [diff] [blame] | 3262 | |
Muchun Song | f1286fa | 2021-04-29 22:56:55 -0700 | [diff] [blame] | 3263 | return ret; |
Roman Gushchin | 4b13f64 | 2020-04-01 21:06:56 -0700 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | /** |
Roman Gushchin | f4b00ea | 2020-04-01 21:06:46 -0700 | [diff] [blame] | 3267 | * __memcg_kmem_charge_page: charge a kmem page to the current memory cgroup |
Vladimir Davydov | 4526477 | 2016-07-26 15:24:21 -0700 | [diff] [blame] | 3268 | * @page: page to charge |
| 3269 | * @gfp: reclaim mode |
| 3270 | * @order: allocation order |
| 3271 | * |
| 3272 | * Returns 0 on success, an error code on failure. |
| 3273 | */ |
Roman Gushchin | f4b00ea | 2020-04-01 21:06:46 -0700 | [diff] [blame] | 3274 | int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order) |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3275 | { |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 3276 | struct obj_cgroup *objcg; |
Vladimir Davydov | fcff7d7 | 2016-03-17 14:17:29 -0700 | [diff] [blame] | 3277 | int ret = 0; |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3278 | |
Roman Gushchin | e86828e | 2023-10-19 15:53:44 -0700 | [diff] [blame] | 3279 | objcg = current_obj_cgroup(); |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 3280 | if (objcg) { |
| 3281 | ret = obj_cgroup_charge_pages(objcg, gfp, 1 << order); |
Roman Gushchin | 4d96ba3 | 2019-07-11 20:56:31 -0700 | [diff] [blame] | 3282 | if (!ret) { |
Roman Gushchin | e86828e | 2023-10-19 15:53:44 -0700 | [diff] [blame] | 3283 | obj_cgroup_get(objcg); |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 3284 | page->memcg_data = (unsigned long)objcg | |
Roman Gushchin | 18b2db3 | 2020-12-01 13:58:30 -0800 | [diff] [blame] | 3285 | MEMCG_DATA_KMEM; |
Johannes Weiner | 1a3e1f4 | 2020-08-06 23:20:45 -0700 | [diff] [blame] | 3286 | return 0; |
Roman Gushchin | 4d96ba3 | 2019-07-11 20:56:31 -0700 | [diff] [blame] | 3287 | } |
Vladimir Davydov | c4159a7 | 2016-08-08 23:03:12 +0300 | [diff] [blame] | 3288 | } |
Vladimir Davydov | d05e83a | 2015-11-05 18:48:59 -0800 | [diff] [blame] | 3289 | return ret; |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3290 | } |
Roman Gushchin | 49a18ea | 2019-07-11 20:56:13 -0700 | [diff] [blame] | 3291 | |
| 3292 | /** |
Roman Gushchin | f4b00ea | 2020-04-01 21:06:46 -0700 | [diff] [blame] | 3293 | * __memcg_kmem_uncharge_page: uncharge a kmem page |
Vladimir Davydov | 4526477 | 2016-07-26 15:24:21 -0700 | [diff] [blame] | 3294 | * @page: page to uncharge |
| 3295 | * @order: allocation order |
| 3296 | */ |
Roman Gushchin | f4b00ea | 2020-04-01 21:06:46 -0700 | [diff] [blame] | 3297 | void __memcg_kmem_uncharge_page(struct page *page, int order) |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3298 | { |
Matthew Wilcox (Oracle) | 1b7e446 | 2021-06-28 14:59:26 -0400 | [diff] [blame] | 3299 | struct folio *folio = page_folio(page); |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 3300 | struct obj_cgroup *objcg; |
Vladimir Davydov | f3ccb2c4 | 2015-11-05 18:49:01 -0800 | [diff] [blame] | 3301 | unsigned int nr_pages = 1 << order; |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3302 | |
Matthew Wilcox (Oracle) | 1b7e446 | 2021-06-28 14:59:26 -0400 | [diff] [blame] | 3303 | if (!folio_memcg_kmem(folio)) |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3304 | return; |
| 3305 | |
Matthew Wilcox (Oracle) | 1b7e446 | 2021-06-28 14:59:26 -0400 | [diff] [blame] | 3306 | objcg = __folio_objcg(folio); |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 3307 | obj_cgroup_uncharge_pages(objcg, nr_pages); |
Matthew Wilcox (Oracle) | 1b7e446 | 2021-06-28 14:59:26 -0400 | [diff] [blame] | 3308 | folio->memcg_data = 0; |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 3309 | obj_cgroup_put(objcg); |
Vladimir Davydov | 60d3fd3 | 2015-02-12 14:59:10 -0800 | [diff] [blame] | 3310 | } |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3311 | |
Waiman Long | 68ac5b3c | 2021-06-28 19:37:23 -0700 | [diff] [blame] | 3312 | void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat, |
| 3313 | enum node_stat_item idx, int nr) |
| 3314 | { |
Michal Hocko | fead2b8 | 2022-03-22 14:40:35 -0700 | [diff] [blame] | 3315 | struct memcg_stock_pcp *stock; |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 3316 | struct obj_cgroup *old = NULL; |
Waiman Long | 68ac5b3c | 2021-06-28 19:37:23 -0700 | [diff] [blame] | 3317 | unsigned long flags; |
| 3318 | int *bytes; |
| 3319 | |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 3320 | local_lock_irqsave(&memcg_stock.stock_lock, flags); |
Michal Hocko | fead2b8 | 2022-03-22 14:40:35 -0700 | [diff] [blame] | 3321 | stock = this_cpu_ptr(&memcg_stock); |
| 3322 | |
Waiman Long | 68ac5b3c | 2021-06-28 19:37:23 -0700 | [diff] [blame] | 3323 | /* |
| 3324 | * Save vmstat data in stock and skip vmstat array update unless |
| 3325 | * accumulating over a page of vmstat data or when pgdat or idx |
| 3326 | * changes. |
| 3327 | */ |
Roman Gushchin | 3b8abb3 | 2023-05-02 09:08:38 -0700 | [diff] [blame] | 3328 | if (READ_ONCE(stock->cached_objcg) != objcg) { |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 3329 | old = drain_obj_stock(stock); |
Waiman Long | 68ac5b3c | 2021-06-28 19:37:23 -0700 | [diff] [blame] | 3330 | obj_cgroup_get(objcg); |
| 3331 | stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes) |
| 3332 | ? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0; |
Roman Gushchin | 3b8abb3 | 2023-05-02 09:08:38 -0700 | [diff] [blame] | 3333 | WRITE_ONCE(stock->cached_objcg, objcg); |
Waiman Long | 68ac5b3c | 2021-06-28 19:37:23 -0700 | [diff] [blame] | 3334 | stock->cached_pgdat = pgdat; |
| 3335 | } else if (stock->cached_pgdat != pgdat) { |
| 3336 | /* Flush the existing cached vmstat data */ |
Waiman Long | 7fa0dac | 2021-08-13 16:54:41 -0700 | [diff] [blame] | 3337 | struct pglist_data *oldpg = stock->cached_pgdat; |
| 3338 | |
Waiman Long | 68ac5b3c | 2021-06-28 19:37:23 -0700 | [diff] [blame] | 3339 | if (stock->nr_slab_reclaimable_b) { |
Waiman Long | 7fa0dac | 2021-08-13 16:54:41 -0700 | [diff] [blame] | 3340 | mod_objcg_mlstate(objcg, oldpg, NR_SLAB_RECLAIMABLE_B, |
Waiman Long | 68ac5b3c | 2021-06-28 19:37:23 -0700 | [diff] [blame] | 3341 | stock->nr_slab_reclaimable_b); |
| 3342 | stock->nr_slab_reclaimable_b = 0; |
| 3343 | } |
| 3344 | if (stock->nr_slab_unreclaimable_b) { |
Waiman Long | 7fa0dac | 2021-08-13 16:54:41 -0700 | [diff] [blame] | 3345 | mod_objcg_mlstate(objcg, oldpg, NR_SLAB_UNRECLAIMABLE_B, |
Waiman Long | 68ac5b3c | 2021-06-28 19:37:23 -0700 | [diff] [blame] | 3346 | stock->nr_slab_unreclaimable_b); |
| 3347 | stock->nr_slab_unreclaimable_b = 0; |
| 3348 | } |
| 3349 | stock->cached_pgdat = pgdat; |
| 3350 | } |
| 3351 | |
| 3352 | bytes = (idx == NR_SLAB_RECLAIMABLE_B) ? &stock->nr_slab_reclaimable_b |
| 3353 | : &stock->nr_slab_unreclaimable_b; |
| 3354 | /* |
| 3355 | * Even for large object >= PAGE_SIZE, the vmstat data will still be |
| 3356 | * cached locally at least once before pushing it out. |
| 3357 | */ |
| 3358 | if (!*bytes) { |
| 3359 | *bytes = nr; |
| 3360 | nr = 0; |
| 3361 | } else { |
| 3362 | *bytes += nr; |
| 3363 | if (abs(*bytes) > PAGE_SIZE) { |
| 3364 | nr = *bytes; |
| 3365 | *bytes = 0; |
| 3366 | } else { |
| 3367 | nr = 0; |
| 3368 | } |
| 3369 | } |
| 3370 | if (nr) |
| 3371 | mod_objcg_mlstate(objcg, pgdat, idx, nr); |
| 3372 | |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 3373 | local_unlock_irqrestore(&memcg_stock.stock_lock, flags); |
| 3374 | if (old) |
| 3375 | obj_cgroup_put(old); |
Waiman Long | 68ac5b3c | 2021-06-28 19:37:23 -0700 | [diff] [blame] | 3376 | } |
| 3377 | |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3378 | static bool consume_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes) |
| 3379 | { |
Michal Hocko | fead2b8 | 2022-03-22 14:40:35 -0700 | [diff] [blame] | 3380 | struct memcg_stock_pcp *stock; |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3381 | unsigned long flags; |
| 3382 | bool ret = false; |
| 3383 | |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 3384 | local_lock_irqsave(&memcg_stock.stock_lock, flags); |
Michal Hocko | fead2b8 | 2022-03-22 14:40:35 -0700 | [diff] [blame] | 3385 | |
| 3386 | stock = this_cpu_ptr(&memcg_stock); |
Roman Gushchin | 3b8abb3 | 2023-05-02 09:08:38 -0700 | [diff] [blame] | 3387 | if (objcg == READ_ONCE(stock->cached_objcg) && stock->nr_bytes >= nr_bytes) { |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3388 | stock->nr_bytes -= nr_bytes; |
| 3389 | ret = true; |
| 3390 | } |
| 3391 | |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 3392 | local_unlock_irqrestore(&memcg_stock.stock_lock, flags); |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3393 | |
| 3394 | return ret; |
| 3395 | } |
| 3396 | |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 3397 | static struct obj_cgroup *drain_obj_stock(struct memcg_stock_pcp *stock) |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3398 | { |
Roman Gushchin | 3b8abb3 | 2023-05-02 09:08:38 -0700 | [diff] [blame] | 3399 | struct obj_cgroup *old = READ_ONCE(stock->cached_objcg); |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3400 | |
| 3401 | if (!old) |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 3402 | return NULL; |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3403 | |
| 3404 | if (stock->nr_bytes) { |
| 3405 | unsigned int nr_pages = stock->nr_bytes >> PAGE_SHIFT; |
| 3406 | unsigned int nr_bytes = stock->nr_bytes & (PAGE_SIZE - 1); |
| 3407 | |
Johannes Weiner | af9a3b6 | 2022-03-22 14:40:44 -0700 | [diff] [blame] | 3408 | if (nr_pages) { |
| 3409 | struct mem_cgroup *memcg; |
| 3410 | |
| 3411 | memcg = get_mem_cgroup_from_objcg(old); |
| 3412 | |
| 3413 | memcg_account_kmem(memcg, -nr_pages); |
| 3414 | __refill_stock(memcg, nr_pages); |
| 3415 | |
| 3416 | css_put(&memcg->css); |
| 3417 | } |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3418 | |
| 3419 | /* |
| 3420 | * The leftover is flushed to the centralized per-memcg value. |
| 3421 | * On the next attempt to refill obj stock it will be moved |
| 3422 | * to a per-cpu stock (probably, on an other CPU), see |
| 3423 | * refill_obj_stock(). |
| 3424 | * |
| 3425 | * How often it's flushed is a trade-off between the memory |
| 3426 | * limit enforcement accuracy and potential CPU contention, |
| 3427 | * so it might be changed in the future. |
| 3428 | */ |
| 3429 | atomic_add(nr_bytes, &old->nr_charged_bytes); |
| 3430 | stock->nr_bytes = 0; |
| 3431 | } |
| 3432 | |
Waiman Long | 68ac5b3c | 2021-06-28 19:37:23 -0700 | [diff] [blame] | 3433 | /* |
| 3434 | * Flush the vmstat data in current stock |
| 3435 | */ |
| 3436 | if (stock->nr_slab_reclaimable_b || stock->nr_slab_unreclaimable_b) { |
| 3437 | if (stock->nr_slab_reclaimable_b) { |
| 3438 | mod_objcg_mlstate(old, stock->cached_pgdat, |
| 3439 | NR_SLAB_RECLAIMABLE_B, |
| 3440 | stock->nr_slab_reclaimable_b); |
| 3441 | stock->nr_slab_reclaimable_b = 0; |
| 3442 | } |
| 3443 | if (stock->nr_slab_unreclaimable_b) { |
| 3444 | mod_objcg_mlstate(old, stock->cached_pgdat, |
| 3445 | NR_SLAB_UNRECLAIMABLE_B, |
| 3446 | stock->nr_slab_unreclaimable_b); |
| 3447 | stock->nr_slab_unreclaimable_b = 0; |
| 3448 | } |
| 3449 | stock->cached_pgdat = NULL; |
| 3450 | } |
| 3451 | |
Roman Gushchin | 3b8abb3 | 2023-05-02 09:08:38 -0700 | [diff] [blame] | 3452 | WRITE_ONCE(stock->cached_objcg, NULL); |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 3453 | /* |
| 3454 | * The `old' objects needs to be released by the caller via |
| 3455 | * obj_cgroup_put() outside of memcg_stock_pcp::stock_lock. |
| 3456 | */ |
| 3457 | return old; |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3458 | } |
| 3459 | |
| 3460 | static bool obj_stock_flush_required(struct memcg_stock_pcp *stock, |
| 3461 | struct mem_cgroup *root_memcg) |
| 3462 | { |
Roman Gushchin | 3b8abb3 | 2023-05-02 09:08:38 -0700 | [diff] [blame] | 3463 | struct obj_cgroup *objcg = READ_ONCE(stock->cached_objcg); |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3464 | struct mem_cgroup *memcg; |
| 3465 | |
Roman Gushchin | 3b8abb3 | 2023-05-02 09:08:38 -0700 | [diff] [blame] | 3466 | if (objcg) { |
| 3467 | memcg = obj_cgroup_memcg(objcg); |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3468 | if (memcg && mem_cgroup_is_descendant(memcg, root_memcg)) |
| 3469 | return true; |
| 3470 | } |
| 3471 | |
| 3472 | return false; |
| 3473 | } |
| 3474 | |
Waiman Long | 5387c904 | 2021-06-28 19:37:27 -0700 | [diff] [blame] | 3475 | static void refill_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes, |
| 3476 | bool allow_uncharge) |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3477 | { |
Michal Hocko | fead2b8 | 2022-03-22 14:40:35 -0700 | [diff] [blame] | 3478 | struct memcg_stock_pcp *stock; |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 3479 | struct obj_cgroup *old = NULL; |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3480 | unsigned long flags; |
Waiman Long | 5387c904 | 2021-06-28 19:37:27 -0700 | [diff] [blame] | 3481 | unsigned int nr_pages = 0; |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3482 | |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 3483 | local_lock_irqsave(&memcg_stock.stock_lock, flags); |
Michal Hocko | fead2b8 | 2022-03-22 14:40:35 -0700 | [diff] [blame] | 3484 | |
| 3485 | stock = this_cpu_ptr(&memcg_stock); |
Roman Gushchin | 3b8abb3 | 2023-05-02 09:08:38 -0700 | [diff] [blame] | 3486 | if (READ_ONCE(stock->cached_objcg) != objcg) { /* reset if necessary */ |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 3487 | old = drain_obj_stock(stock); |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3488 | obj_cgroup_get(objcg); |
Roman Gushchin | 3b8abb3 | 2023-05-02 09:08:38 -0700 | [diff] [blame] | 3489 | WRITE_ONCE(stock->cached_objcg, objcg); |
Waiman Long | 5387c904 | 2021-06-28 19:37:27 -0700 | [diff] [blame] | 3490 | stock->nr_bytes = atomic_read(&objcg->nr_charged_bytes) |
| 3491 | ? atomic_xchg(&objcg->nr_charged_bytes, 0) : 0; |
| 3492 | allow_uncharge = true; /* Allow uncharge when objcg changes */ |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3493 | } |
| 3494 | stock->nr_bytes += nr_bytes; |
| 3495 | |
Waiman Long | 5387c904 | 2021-06-28 19:37:27 -0700 | [diff] [blame] | 3496 | if (allow_uncharge && (stock->nr_bytes > PAGE_SIZE)) { |
| 3497 | nr_pages = stock->nr_bytes >> PAGE_SHIFT; |
| 3498 | stock->nr_bytes &= (PAGE_SIZE - 1); |
| 3499 | } |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3500 | |
Sebastian Andrzej Siewior | 5675114 | 2022-03-22 14:40:47 -0700 | [diff] [blame] | 3501 | local_unlock_irqrestore(&memcg_stock.stock_lock, flags); |
| 3502 | if (old) |
| 3503 | obj_cgroup_put(old); |
Waiman Long | 5387c904 | 2021-06-28 19:37:27 -0700 | [diff] [blame] | 3504 | |
| 3505 | if (nr_pages) |
| 3506 | obj_cgroup_uncharge_pages(objcg, nr_pages); |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3507 | } |
| 3508 | |
| 3509 | int obj_cgroup_charge(struct obj_cgroup *objcg, gfp_t gfp, size_t size) |
| 3510 | { |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3511 | unsigned int nr_pages, nr_bytes; |
| 3512 | int ret; |
| 3513 | |
| 3514 | if (consume_obj_stock(objcg, size)) |
| 3515 | return 0; |
| 3516 | |
| 3517 | /* |
Waiman Long | 5387c904 | 2021-06-28 19:37:27 -0700 | [diff] [blame] | 3518 | * In theory, objcg->nr_charged_bytes can have enough |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3519 | * pre-charged bytes to satisfy the allocation. However, |
Waiman Long | 5387c904 | 2021-06-28 19:37:27 -0700 | [diff] [blame] | 3520 | * flushing objcg->nr_charged_bytes requires two atomic |
| 3521 | * operations, and objcg->nr_charged_bytes can't be big. |
| 3522 | * The shared objcg->nr_charged_bytes can also become a |
| 3523 | * performance bottleneck if all tasks of the same memcg are |
| 3524 | * trying to update it. So it's better to ignore it and try |
| 3525 | * grab some new pages. The stock's nr_bytes will be flushed to |
| 3526 | * objcg->nr_charged_bytes later on when objcg changes. |
| 3527 | * |
| 3528 | * The stock's nr_bytes may contain enough pre-charged bytes |
| 3529 | * to allow one less page from being charged, but we can't rely |
| 3530 | * on the pre-charged bytes not being changed outside of |
| 3531 | * consume_obj_stock() or refill_obj_stock(). So ignore those |
| 3532 | * pre-charged bytes as well when charging pages. To avoid a |
| 3533 | * page uncharge right after a page charge, we set the |
| 3534 | * allow_uncharge flag to false when calling refill_obj_stock() |
| 3535 | * to temporarily allow the pre-charged bytes to exceed the page |
| 3536 | * size limit. The maximum reachable value of the pre-charged |
| 3537 | * bytes is (sizeof(object) + PAGE_SIZE - 2) if there is no data |
| 3538 | * race. |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3539 | */ |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3540 | nr_pages = size >> PAGE_SHIFT; |
| 3541 | nr_bytes = size & (PAGE_SIZE - 1); |
| 3542 | |
| 3543 | if (nr_bytes) |
| 3544 | nr_pages += 1; |
| 3545 | |
Muchun Song | e74d225 | 2021-04-29 22:56:42 -0700 | [diff] [blame] | 3546 | ret = obj_cgroup_charge_pages(objcg, gfp, nr_pages); |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3547 | if (!ret && nr_bytes) |
Waiman Long | 5387c904 | 2021-06-28 19:37:27 -0700 | [diff] [blame] | 3548 | refill_obj_stock(objcg, PAGE_SIZE - nr_bytes, false); |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3549 | |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3550 | return ret; |
| 3551 | } |
| 3552 | |
| 3553 | void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size) |
| 3554 | { |
Waiman Long | 5387c904 | 2021-06-28 19:37:27 -0700 | [diff] [blame] | 3555 | refill_obj_stock(objcg, size, true); |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3556 | } |
| 3557 | |
Kirill Tkhai | 84c07d1 | 2018-08-17 15:47:25 -0700 | [diff] [blame] | 3558 | #endif /* CONFIG_MEMCG_KMEM */ |
Glauber Costa | 7ae1e1d | 2012-12-18 14:21:56 -0800 | [diff] [blame] | 3559 | |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3560 | /* |
Zhou Guanghui | be6c898 | 2021-03-12 21:08:30 -0800 | [diff] [blame] | 3561 | * Because page_memcg(head) is not set on tails, set it now. |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3562 | */ |
Zhou Guanghui | be6c898 | 2021-03-12 21:08:30 -0800 | [diff] [blame] | 3563 | void split_page_memcg(struct page *head, unsigned int nr) |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3564 | { |
Matthew Wilcox (Oracle) | 1b7e446 | 2021-06-28 14:59:26 -0400 | [diff] [blame] | 3565 | struct folio *folio = page_folio(head); |
| 3566 | struct mem_cgroup *memcg = folio_memcg(folio); |
KAMEZAWA Hiroyuki | e94c8a9 | 2012-01-12 17:18:20 -0800 | [diff] [blame] | 3567 | int i; |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3568 | |
Zhou Guanghui | be6c898 | 2021-03-12 21:08:30 -0800 | [diff] [blame] | 3569 | if (mem_cgroup_disabled() || !memcg) |
KAMEZAWA Hiroyuki | 3d37c4a | 2011-01-25 15:07:28 -0800 | [diff] [blame] | 3570 | return; |
David Rientjes | b070e65 | 2013-05-07 16:18:09 -0700 | [diff] [blame] | 3571 | |
Zhou Guanghui | be6c898 | 2021-03-12 21:08:30 -0800 | [diff] [blame] | 3572 | for (i = 1; i < nr; i++) |
Matthew Wilcox (Oracle) | 1b7e446 | 2021-06-28 14:59:26 -0400 | [diff] [blame] | 3573 | folio_page(folio, i)->memcg_data = folio->memcg_data; |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 3574 | |
Matthew Wilcox (Oracle) | 1b7e446 | 2021-06-28 14:59:26 -0400 | [diff] [blame] | 3575 | if (folio_memcg_kmem(folio)) |
| 3576 | obj_cgroup_get_many(__folio_objcg(folio), nr - 1); |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 3577 | else |
| 3578 | css_get_many(&memcg->css, nr - 1); |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3579 | } |
KAMEZAWA Hiroyuki | ca3e021 | 2011-01-20 14:44:24 -0800 | [diff] [blame] | 3580 | |
Johannes Weiner | e55b9f9 | 2022-09-26 09:57:04 -0400 | [diff] [blame] | 3581 | #ifdef CONFIG_SWAP |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3582 | /** |
| 3583 | * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record. |
| 3584 | * @entry: swap entry to be moved |
| 3585 | * @from: mem_cgroup which the entry is moved from |
| 3586 | * @to: mem_cgroup which the entry is moved to |
| 3587 | * |
| 3588 | * It succeeds only when the swap_cgroup's record for this entry is the same |
| 3589 | * as the mem_cgroup's id of @from. |
| 3590 | * |
| 3591 | * Returns 0 on success, -EINVAL on failure. |
| 3592 | * |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3593 | * The caller must have charged to @to, IOW, called page_counter_charge() about |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3594 | * both res and memsw, and called css_get(). |
| 3595 | */ |
| 3596 | static int mem_cgroup_move_swap_account(swp_entry_t entry, |
Hugh Dickins | e91cbb4 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 3597 | struct mem_cgroup *from, struct mem_cgroup *to) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3598 | { |
| 3599 | unsigned short old_id, new_id; |
| 3600 | |
Li Zefan | 34c00c3 | 2013-09-23 16:56:01 +0800 | [diff] [blame] | 3601 | old_id = mem_cgroup_id(from); |
| 3602 | new_id = mem_cgroup_id(to); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3603 | |
| 3604 | if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) { |
Johannes Weiner | c9019e9 | 2018-01-31 16:16:37 -0800 | [diff] [blame] | 3605 | mod_memcg_state(from, MEMCG_SWAP, -1); |
| 3606 | mod_memcg_state(to, MEMCG_SWAP, 1); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3607 | return 0; |
| 3608 | } |
| 3609 | return -EINVAL; |
| 3610 | } |
| 3611 | #else |
| 3612 | static inline int mem_cgroup_move_swap_account(swp_entry_t entry, |
Hugh Dickins | e91cbb4 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 3613 | struct mem_cgroup *from, struct mem_cgroup *to) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 3614 | { |
| 3615 | return -EINVAL; |
| 3616 | } |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3617 | #endif |
| 3618 | |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 3619 | static DEFINE_MUTEX(memcg_max_mutex); |
Daisuke Nishimura | f212ad7 | 2011-03-23 16:42:25 -0700 | [diff] [blame] | 3620 | |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 3621 | static int mem_cgroup_resize_max(struct mem_cgroup *memcg, |
| 3622 | unsigned long max, bool memsw) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3623 | { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3624 | bool enlarge = false; |
Shakeel Butt | bb4a7ea | 2018-06-07 17:07:27 -0700 | [diff] [blame] | 3625 | bool drained = false; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3626 | int ret; |
Yu Zhao | c054a78 | 2018-01-31 16:20:02 -0800 | [diff] [blame] | 3627 | bool limits_invariant; |
| 3628 | struct page_counter *counter = memsw ? &memcg->memsw : &memcg->memory; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 3629 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3630 | do { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3631 | if (signal_pending(current)) { |
| 3632 | ret = -EINTR; |
| 3633 | break; |
| 3634 | } |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3635 | |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 3636 | mutex_lock(&memcg_max_mutex); |
Yu Zhao | c054a78 | 2018-01-31 16:20:02 -0800 | [diff] [blame] | 3637 | /* |
| 3638 | * Make sure that the new limit (memsw or memory limit) doesn't |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 3639 | * break our basic invariant rule memory.max <= memsw.max. |
Yu Zhao | c054a78 | 2018-01-31 16:20:02 -0800 | [diff] [blame] | 3640 | */ |
Chris Down | 15b4256 | 2020-04-01 21:07:20 -0700 | [diff] [blame] | 3641 | limits_invariant = memsw ? max >= READ_ONCE(memcg->memory.max) : |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 3642 | max <= memcg->memsw.max; |
Yu Zhao | c054a78 | 2018-01-31 16:20:02 -0800 | [diff] [blame] | 3643 | if (!limits_invariant) { |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 3644 | mutex_unlock(&memcg_max_mutex); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3645 | ret = -EINVAL; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3646 | break; |
| 3647 | } |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 3648 | if (max > counter->max) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3649 | enlarge = true; |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 3650 | ret = page_counter_set_max(counter, max); |
| 3651 | mutex_unlock(&memcg_max_mutex); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3652 | |
| 3653 | if (!ret) |
| 3654 | break; |
| 3655 | |
Shakeel Butt | bb4a7ea | 2018-06-07 17:07:27 -0700 | [diff] [blame] | 3656 | if (!drained) { |
| 3657 | drain_all_stock(memcg); |
| 3658 | drained = true; |
| 3659 | continue; |
| 3660 | } |
| 3661 | |
Yosry Ahmed | 73b73ba | 2022-07-14 06:49:18 +0000 | [diff] [blame] | 3662 | if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL, |
Michal Hocko | 55ab834 | 2022-12-16 10:46:33 +0100 | [diff] [blame] | 3663 | memsw ? 0 : MEMCG_RECLAIM_MAY_SWAP)) { |
Andrey Ryabinin | 1ab5c05 | 2018-01-31 16:20:37 -0800 | [diff] [blame] | 3664 | ret = -EBUSY; |
| 3665 | break; |
| 3666 | } |
| 3667 | } while (true); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3668 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 3669 | if (!ret && enlarge) |
| 3670 | memcg_oom_recover(memcg); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3671 | |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3672 | return ret; |
| 3673 | } |
| 3674 | |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 3675 | unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order, |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 3676 | gfp_t gfp_mask, |
| 3677 | unsigned long *total_scanned) |
| 3678 | { |
| 3679 | unsigned long nr_reclaimed = 0; |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 3680 | struct mem_cgroup_per_node *mz, *next_mz = NULL; |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 3681 | unsigned long reclaimed; |
| 3682 | int loop = 0; |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 3683 | struct mem_cgroup_tree_per_node *mctz; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3684 | unsigned long excess; |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 3685 | |
Yu Zhao | e4dde56 | 2022-12-21 21:19:04 -0700 | [diff] [blame] | 3686 | if (lru_gen_enabled()) |
| 3687 | return 0; |
| 3688 | |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 3689 | if (order > 0) |
| 3690 | return 0; |
| 3691 | |
Matthew Wilcox (Oracle) | 2ab082b | 2021-06-25 08:57:02 -0400 | [diff] [blame] | 3692 | mctz = soft_limit_tree.rb_tree_per_node[pgdat->node_id]; |
Michal Hocko | d6507ff | 2016-08-02 14:02:37 -0700 | [diff] [blame] | 3693 | |
| 3694 | /* |
| 3695 | * Do not even bother to check the largest node if the root |
| 3696 | * is empty. Do it lockless to prevent lock bouncing. Races |
| 3697 | * are acceptable as soft limit is best effort anyway. |
| 3698 | */ |
Laurent Dufour | bfc7228 | 2017-03-09 16:17:06 -0800 | [diff] [blame] | 3699 | if (!mctz || RB_EMPTY_ROOT(&mctz->rb_root)) |
Michal Hocko | d6507ff | 2016-08-02 14:02:37 -0700 | [diff] [blame] | 3700 | return 0; |
| 3701 | |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 3702 | /* |
| 3703 | * This loop can run a while, specially if mem_cgroup's continuously |
| 3704 | * keep exceeding their soft limit and putting the system under |
| 3705 | * pressure |
| 3706 | */ |
| 3707 | do { |
| 3708 | if (next_mz) |
| 3709 | mz = next_mz; |
| 3710 | else |
| 3711 | mz = mem_cgroup_largest_soft_limit_node(mctz); |
| 3712 | if (!mz) |
| 3713 | break; |
| 3714 | |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 3715 | reclaimed = mem_cgroup_soft_reclaim(mz->memcg, pgdat, |
Miaohe Lin | d8f6533 | 2022-04-28 23:15:58 -0700 | [diff] [blame] | 3716 | gfp_mask, total_scanned); |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 3717 | nr_reclaimed += reclaimed; |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 3718 | spin_lock_irq(&mctz->lock); |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 3719 | |
| 3720 | /* |
| 3721 | * If we failed to reclaim anything from this memory cgroup |
| 3722 | * it is time to move on to the next cgroup |
| 3723 | */ |
| 3724 | next_mz = NULL; |
Vladimir Davydov | bc2f2e7 | 2014-12-10 15:43:40 -0800 | [diff] [blame] | 3725 | if (!reclaimed) |
| 3726 | next_mz = __mem_cgroup_largest_soft_limit_node(mctz); |
| 3727 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3728 | excess = soft_limit_excess(mz->memcg); |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 3729 | /* |
| 3730 | * One school of thought says that we should not add |
| 3731 | * back the node to the tree if reclaim returns 0. |
| 3732 | * But our reclaim could return 0, simply because due |
| 3733 | * to priority we are exposing a smaller subset of |
| 3734 | * memory to reclaim from. Consider this as a longer |
| 3735 | * term TODO. |
| 3736 | */ |
| 3737 | /* If excess == 0, no tree ops */ |
Johannes Weiner | cf2c812 | 2014-06-06 14:38:21 -0700 | [diff] [blame] | 3738 | __mem_cgroup_insert_exceeded(mz, mctz, excess); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 3739 | spin_unlock_irq(&mctz->lock); |
Andrew Morton | 0608f43 | 2013-09-24 15:27:41 -0700 | [diff] [blame] | 3740 | css_put(&mz->memcg->css); |
| 3741 | loop++; |
| 3742 | /* |
| 3743 | * Could not reclaim anything and there are no more |
| 3744 | * mem cgroups to try or we seem to be looping without |
| 3745 | * reclaiming anything. |
| 3746 | */ |
| 3747 | if (!nr_reclaimed && |
| 3748 | (next_mz == NULL || |
| 3749 | loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS)) |
| 3750 | break; |
| 3751 | } while (!nr_reclaimed); |
| 3752 | if (next_mz) |
| 3753 | css_put(&next_mz->memcg->css); |
| 3754 | return nr_reclaimed; |
| 3755 | } |
| 3756 | |
Tejun Heo | ea280e7 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 3757 | /* |
Greg Thelen | 5103817 | 2016-05-20 16:58:18 -0700 | [diff] [blame] | 3758 | * Reclaims as many pages from the given memcg as possible. |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 3759 | * |
| 3760 | * Caller is responsible for holding css reference for memcg. |
| 3761 | */ |
| 3762 | static int mem_cgroup_force_empty(struct mem_cgroup *memcg) |
| 3763 | { |
Chris Down | d977aa9 | 2020-08-06 23:21:58 -0700 | [diff] [blame] | 3764 | int nr_retries = MAX_RECLAIM_RETRIES; |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 3765 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3766 | /* we call try-to-free pages for make this cgroup empty */ |
| 3767 | lru_add_drain_all(); |
Junaid Shahid | d12c60f | 2018-06-07 17:07:31 -0700 | [diff] [blame] | 3768 | |
| 3769 | drain_all_stock(memcg); |
| 3770 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3771 | /* try to free all pages in this cgroup */ |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3772 | while (nr_retries && page_counter_read(&memcg->memory)) { |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 3773 | if (signal_pending(current)) |
| 3774 | return -EINTR; |
| 3775 | |
Yosry Ahmed | 73b73ba | 2022-07-14 06:49:18 +0000 | [diff] [blame] | 3776 | if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL, |
Michal Hocko | 55ab834 | 2022-12-16 10:46:33 +0100 | [diff] [blame] | 3777 | MEMCG_RECLAIM_MAY_SWAP)) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3778 | nr_retries--; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 3779 | } |
Michal Hocko | ab5196c | 2012-10-26 13:37:32 +0200 | [diff] [blame] | 3780 | |
| 3781 | return 0; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 3782 | } |
| 3783 | |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3784 | static ssize_t mem_cgroup_force_empty_write(struct kernfs_open_file *of, |
| 3785 | char *buf, size_t nbytes, |
| 3786 | loff_t off) |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3787 | { |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3788 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
Michal Hocko | c26251f | 2012-10-26 13:37:28 +0200 | [diff] [blame] | 3789 | |
Michal Hocko | d842301 | 2012-10-26 13:37:29 +0200 | [diff] [blame] | 3790 | if (mem_cgroup_is_root(memcg)) |
| 3791 | return -EINVAL; |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3792 | return mem_cgroup_force_empty(memcg) ?: nbytes; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 3793 | } |
| 3794 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3795 | static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css, |
| 3796 | struct cftype *cft) |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3797 | { |
Roman Gushchin | bef8620 | 2020-12-14 19:06:49 -0800 | [diff] [blame] | 3798 | return 1; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3799 | } |
| 3800 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3801 | static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css, |
| 3802 | struct cftype *cft, u64 val) |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3803 | { |
Roman Gushchin | bef8620 | 2020-12-14 19:06:49 -0800 | [diff] [blame] | 3804 | if (val == 1) |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 3805 | return 0; |
Glauber Costa | 567fb43 | 2012-07-31 16:43:07 -0700 | [diff] [blame] | 3806 | |
Roman Gushchin | bef8620 | 2020-12-14 19:06:49 -0800 | [diff] [blame] | 3807 | pr_warn_once("Non-hierarchical mode is deprecated. " |
| 3808 | "Please report your usecase to linux-mm@kvack.org if you " |
| 3809 | "depend on this functionality.\n"); |
Glauber Costa | 567fb43 | 2012-07-31 16:43:07 -0700 | [diff] [blame] | 3810 | |
Roman Gushchin | bef8620 | 2020-12-14 19:06:49 -0800 | [diff] [blame] | 3811 | return -EINVAL; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 3812 | } |
| 3813 | |
Andrew Morton | 6f64615 | 2015-11-06 16:28:58 -0800 | [diff] [blame] | 3814 | static unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap) |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 3815 | { |
Johannes Weiner | 42a3003 | 2019-05-14 15:47:12 -0700 | [diff] [blame] | 3816 | unsigned long val; |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 3817 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3818 | if (mem_cgroup_is_root(memcg)) { |
Yosry Ahmed | a2174e9 | 2023-03-30 19:17:56 +0000 | [diff] [blame] | 3819 | /* |
Yosry Ahmed | f82a7a8 | 2023-04-21 17:40:18 +0000 | [diff] [blame] | 3820 | * Approximate root's usage from global state. This isn't |
| 3821 | * perfect, but the root usage was always an approximation. |
Yosry Ahmed | a2174e9 | 2023-03-30 19:17:56 +0000 | [diff] [blame] | 3822 | */ |
Yosry Ahmed | f82a7a8 | 2023-04-21 17:40:18 +0000 | [diff] [blame] | 3823 | val = global_node_page_state(NR_FILE_PAGES) + |
| 3824 | global_node_page_state(NR_ANON_MAPPED); |
Johannes Weiner | 42a3003 | 2019-05-14 15:47:12 -0700 | [diff] [blame] | 3825 | if (swap) |
Yosry Ahmed | f82a7a8 | 2023-04-21 17:40:18 +0000 | [diff] [blame] | 3826 | val += total_swap_pages - get_nr_swap_pages(); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3827 | } else { |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 3828 | if (!swap) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3829 | val = page_counter_read(&memcg->memory); |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 3830 | else |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3831 | val = page_counter_read(&memcg->memsw); |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 3832 | } |
Michal Hocko | c12176d | 2015-11-05 18:50:29 -0800 | [diff] [blame] | 3833 | return val; |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 3834 | } |
| 3835 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3836 | enum { |
| 3837 | RES_USAGE, |
| 3838 | RES_LIMIT, |
| 3839 | RES_MAX_USAGE, |
| 3840 | RES_FAILCNT, |
| 3841 | RES_SOFT_LIMIT, |
| 3842 | }; |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 3843 | |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 3844 | static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css, |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 3845 | struct cftype *cft) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3846 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 3847 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3848 | struct page_counter *counter; |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 3849 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3850 | switch (MEMFILE_TYPE(cft->private)) { |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3851 | case _MEM: |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3852 | counter = &memcg->memory; |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 3853 | break; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3854 | case _MEMSWAP: |
| 3855 | counter = &memcg->memsw; |
| 3856 | break; |
| 3857 | case _KMEM: |
| 3858 | counter = &memcg->kmem; |
| 3859 | break; |
Vladimir Davydov | d55f90b | 2016-01-20 15:02:44 -0800 | [diff] [blame] | 3860 | case _TCP: |
Johannes Weiner | 0db1529 | 2016-01-20 15:02:50 -0800 | [diff] [blame] | 3861 | counter = &memcg->tcpmem; |
Vladimir Davydov | d55f90b | 2016-01-20 15:02:44 -0800 | [diff] [blame] | 3862 | break; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3863 | default: |
| 3864 | BUG(); |
| 3865 | } |
| 3866 | |
| 3867 | switch (MEMFILE_ATTR(cft->private)) { |
| 3868 | case RES_USAGE: |
| 3869 | if (counter == &memcg->memory) |
Michal Hocko | c12176d | 2015-11-05 18:50:29 -0800 | [diff] [blame] | 3870 | return (u64)mem_cgroup_usage(memcg, false) * PAGE_SIZE; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3871 | if (counter == &memcg->memsw) |
Michal Hocko | c12176d | 2015-11-05 18:50:29 -0800 | [diff] [blame] | 3872 | return (u64)mem_cgroup_usage(memcg, true) * PAGE_SIZE; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3873 | return (u64)page_counter_read(counter) * PAGE_SIZE; |
| 3874 | case RES_LIMIT: |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 3875 | return (u64)counter->max * PAGE_SIZE; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 3876 | case RES_MAX_USAGE: |
| 3877 | return (u64)counter->watermark * PAGE_SIZE; |
| 3878 | case RES_FAILCNT: |
| 3879 | return counter->failcnt; |
| 3880 | case RES_SOFT_LIMIT: |
Yue Zhao | 2178e20 | 2023-03-06 23:41:38 +0800 | [diff] [blame] | 3881 | return (u64)READ_ONCE(memcg->soft_limit) * PAGE_SIZE; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3882 | default: |
| 3883 | BUG(); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 3884 | } |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3885 | } |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 3886 | |
Florian Schmidt | 6b0ba2a | 2023-04-04 10:58:59 +0000 | [diff] [blame] | 3887 | /* |
| 3888 | * This function doesn't do anything useful. Its only job is to provide a read |
| 3889 | * handler for a file so that cgroup_file_mode() will add read permissions. |
| 3890 | */ |
| 3891 | static int mem_cgroup_dummy_seq_show(__always_unused struct seq_file *m, |
| 3892 | __always_unused void *v) |
| 3893 | { |
| 3894 | return -EINVAL; |
| 3895 | } |
| 3896 | |
Kirill Tkhai | 84c07d1 | 2018-08-17 15:47:25 -0700 | [diff] [blame] | 3897 | #ifdef CONFIG_MEMCG_KMEM |
Johannes Weiner | 567e9ab | 2016-01-20 15:02:24 -0800 | [diff] [blame] | 3898 | static int memcg_online_kmem(struct mem_cgroup *memcg) |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 3899 | { |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3900 | struct obj_cgroup *objcg; |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 3901 | |
Xiang Yang | 9c94bef | 2022-06-25 14:18:44 +0800 | [diff] [blame] | 3902 | if (mem_cgroup_kmem_disabled()) |
Vladimir Davydov | b313aee | 2016-03-17 14:18:27 -0700 | [diff] [blame] | 3903 | return 0; |
| 3904 | |
Muchun Song | da0efe3 | 2022-03-22 14:41:15 -0700 | [diff] [blame] | 3905 | if (unlikely(mem_cgroup_is_root(memcg))) |
| 3906 | return 0; |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 3907 | |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3908 | objcg = obj_cgroup_alloc(); |
Muchun Song | f9c69d6 | 2022-03-22 14:41:28 -0700 | [diff] [blame] | 3909 | if (!objcg) |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3910 | return -ENOMEM; |
Muchun Song | f9c69d6 | 2022-03-22 14:41:28 -0700 | [diff] [blame] | 3911 | |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3912 | objcg->memcg = memcg; |
| 3913 | rcu_assign_pointer(memcg->objcg, objcg); |
Roman Gushchin | 675d6c9 | 2023-10-19 15:53:43 -0700 | [diff] [blame] | 3914 | obj_cgroup_get(objcg); |
| 3915 | memcg->orig_objcg = objcg; |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3916 | |
Roman Gushchin | f7a449f | 2023-02-13 11:29:22 -0800 | [diff] [blame] | 3917 | static_branch_enable(&memcg_kmem_online_key); |
Roman Gushchin | d648bcc | 2020-08-06 23:20:28 -0700 | [diff] [blame] | 3918 | |
Muchun Song | f9c69d6 | 2022-03-22 14:41:28 -0700 | [diff] [blame] | 3919 | memcg->kmemcg_id = memcg->id.id; |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 3920 | |
| 3921 | return 0; |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 3922 | } |
| 3923 | |
Johannes Weiner | 8e0a891 | 2016-01-20 15:02:26 -0800 | [diff] [blame] | 3924 | static void memcg_offline_kmem(struct mem_cgroup *memcg) |
| 3925 | { |
Muchun Song | 6426886 | 2021-11-05 13:37:53 -0700 | [diff] [blame] | 3926 | struct mem_cgroup *parent; |
Johannes Weiner | 8e0a891 | 2016-01-20 15:02:26 -0800 | [diff] [blame] | 3927 | |
Xiang Yang | 9c94bef | 2022-06-25 14:18:44 +0800 | [diff] [blame] | 3928 | if (mem_cgroup_kmem_disabled()) |
Muchun Song | da0efe3 | 2022-03-22 14:41:15 -0700 | [diff] [blame] | 3929 | return; |
| 3930 | |
| 3931 | if (unlikely(mem_cgroup_is_root(memcg))) |
Johannes Weiner | 8e0a891 | 2016-01-20 15:02:26 -0800 | [diff] [blame] | 3932 | return; |
Roman Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame] | 3933 | |
Johannes Weiner | 8e0a891 | 2016-01-20 15:02:26 -0800 | [diff] [blame] | 3934 | parent = parent_mem_cgroup(memcg); |
| 3935 | if (!parent) |
| 3936 | parent = root_mem_cgroup; |
| 3937 | |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 3938 | memcg_reparent_objcgs(memcg, parent); |
Roman Gushchin | fb2f2b0 | 2019-07-11 20:56:34 -0700 | [diff] [blame] | 3939 | |
Muchun Song | 5abc1e3 | 2022-03-22 14:41:19 -0700 | [diff] [blame] | 3940 | /* |
Muchun Song | 6426886 | 2021-11-05 13:37:53 -0700 | [diff] [blame] | 3941 | * After we have finished memcg_reparent_objcgs(), all list_lrus |
| 3942 | * corresponding to this cgroup are guaranteed to remain empty. |
| 3943 | * The ordering is imposed by list_lru_node->lock taken by |
Muchun Song | 1f391eb | 2022-03-22 14:41:22 -0700 | [diff] [blame] | 3944 | * memcg_reparent_list_lrus(). |
Johannes Weiner | 8e0a891 | 2016-01-20 15:02:26 -0800 | [diff] [blame] | 3945 | */ |
Muchun Song | 1f391eb | 2022-03-22 14:41:22 -0700 | [diff] [blame] | 3946 | memcg_reparent_list_lrus(memcg, parent); |
Johannes Weiner | 8e0a891 | 2016-01-20 15:02:26 -0800 | [diff] [blame] | 3947 | } |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 3948 | #else |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 3949 | static int memcg_online_kmem(struct mem_cgroup *memcg) |
Johannes Weiner | 127424c | 2016-01-20 15:02:32 -0800 | [diff] [blame] | 3950 | { |
| 3951 | return 0; |
| 3952 | } |
| 3953 | static void memcg_offline_kmem(struct mem_cgroup *memcg) |
| 3954 | { |
| 3955 | } |
Kirill Tkhai | 84c07d1 | 2018-08-17 15:47:25 -0700 | [diff] [blame] | 3956 | #endif /* CONFIG_MEMCG_KMEM */ |
Johannes Weiner | 127424c | 2016-01-20 15:02:32 -0800 | [diff] [blame] | 3957 | |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 3958 | static int memcg_update_tcp_max(struct mem_cgroup *memcg, unsigned long max) |
Vladimir Davydov | d55f90b | 2016-01-20 15:02:44 -0800 | [diff] [blame] | 3959 | { |
| 3960 | int ret; |
| 3961 | |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 3962 | mutex_lock(&memcg_max_mutex); |
Vladimir Davydov | d55f90b | 2016-01-20 15:02:44 -0800 | [diff] [blame] | 3963 | |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 3964 | ret = page_counter_set_max(&memcg->tcpmem, max); |
Vladimir Davydov | d55f90b | 2016-01-20 15:02:44 -0800 | [diff] [blame] | 3965 | if (ret) |
| 3966 | goto out; |
| 3967 | |
Johannes Weiner | 0db1529 | 2016-01-20 15:02:50 -0800 | [diff] [blame] | 3968 | if (!memcg->tcpmem_active) { |
Vladimir Davydov | d55f90b | 2016-01-20 15:02:44 -0800 | [diff] [blame] | 3969 | /* |
| 3970 | * The active flag needs to be written after the static_key |
| 3971 | * update. This is what guarantees that the socket activation |
Johannes Weiner | 2d75807 | 2016-10-07 17:00:58 -0700 | [diff] [blame] | 3972 | * function is the last one to run. See mem_cgroup_sk_alloc() |
| 3973 | * for details, and note that we don't mark any socket as |
| 3974 | * belonging to this memcg until that flag is up. |
Vladimir Davydov | d55f90b | 2016-01-20 15:02:44 -0800 | [diff] [blame] | 3975 | * |
| 3976 | * We need to do this, because static_keys will span multiple |
| 3977 | * sites, but we can't control their order. If we mark a socket |
| 3978 | * as accounted, but the accounting functions are not patched in |
| 3979 | * yet, we'll lose accounting. |
| 3980 | * |
Johannes Weiner | 2d75807 | 2016-10-07 17:00:58 -0700 | [diff] [blame] | 3981 | * We never race with the readers in mem_cgroup_sk_alloc(), |
Vladimir Davydov | d55f90b | 2016-01-20 15:02:44 -0800 | [diff] [blame] | 3982 | * because when this value change, the code to process it is not |
| 3983 | * patched in yet. |
| 3984 | */ |
| 3985 | static_branch_inc(&memcg_sockets_enabled_key); |
Johannes Weiner | 0db1529 | 2016-01-20 15:02:50 -0800 | [diff] [blame] | 3986 | memcg->tcpmem_active = true; |
Vladimir Davydov | d55f90b | 2016-01-20 15:02:44 -0800 | [diff] [blame] | 3987 | } |
| 3988 | out: |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 3989 | mutex_unlock(&memcg_max_mutex); |
Vladimir Davydov | d55f90b | 2016-01-20 15:02:44 -0800 | [diff] [blame] | 3990 | return ret; |
| 3991 | } |
Vladimir Davydov | d55f90b | 2016-01-20 15:02:44 -0800 | [diff] [blame] | 3992 | |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 3993 | /* |
| 3994 | * The user of this function is... |
| 3995 | * RES_LIMIT. |
| 3996 | */ |
Tejun Heo | 451af504 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 3997 | static ssize_t mem_cgroup_write(struct kernfs_open_file *of, |
| 3998 | char *buf, size_t nbytes, loff_t off) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 3999 | { |
Tejun Heo | 451af504 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4000 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4001 | unsigned long nr_pages; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4002 | int ret; |
| 4003 | |
Tejun Heo | 451af504 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4004 | buf = strstrip(buf); |
Johannes Weiner | 650c5e5 | 2015-02-11 15:26:03 -0800 | [diff] [blame] | 4005 | ret = page_counter_memparse(buf, "-1", &nr_pages); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4006 | if (ret) |
| 4007 | return ret; |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4008 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4009 | switch (MEMFILE_ATTR(of_cft(of)->private)) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4010 | case RES_LIMIT: |
Balbir Singh | 4b3bde4 | 2009-09-23 15:56:32 -0700 | [diff] [blame] | 4011 | if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */ |
| 4012 | ret = -EINVAL; |
| 4013 | break; |
| 4014 | } |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4015 | switch (MEMFILE_TYPE(of_cft(of)->private)) { |
| 4016 | case _MEM: |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 4017 | ret = mem_cgroup_resize_max(memcg, nr_pages, false); |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 4018 | break; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4019 | case _MEMSWAP: |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 4020 | ret = mem_cgroup_resize_max(memcg, nr_pages, true); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4021 | break; |
Michal Hocko | 4597648 | 2023-09-21 09:38:29 +0200 | [diff] [blame] | 4022 | case _KMEM: |
| 4023 | pr_warn_once("kmem.limit_in_bytes is deprecated and will be removed. " |
| 4024 | "Writing any value to this file has no effect. " |
| 4025 | "Please report your usecase to linux-mm@kvack.org if you " |
| 4026 | "depend on this functionality.\n"); |
| 4027 | ret = 0; |
| 4028 | break; |
Vladimir Davydov | d55f90b | 2016-01-20 15:02:44 -0800 | [diff] [blame] | 4029 | case _TCP: |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 4030 | ret = memcg_update_tcp_max(memcg, nr_pages); |
Vladimir Davydov | d55f90b | 2016-01-20 15:02:44 -0800 | [diff] [blame] | 4031 | break; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4032 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4033 | break; |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 4034 | case RES_SOFT_LIMIT: |
Sebastian Andrzej Siewior | 2343e88 | 2022-03-22 14:40:38 -0700 | [diff] [blame] | 4035 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) { |
| 4036 | ret = -EOPNOTSUPP; |
| 4037 | } else { |
Yue Zhao | 2178e20 | 2023-03-06 23:41:38 +0800 | [diff] [blame] | 4038 | WRITE_ONCE(memcg->soft_limit, nr_pages); |
Sebastian Andrzej Siewior | 2343e88 | 2022-03-22 14:40:38 -0700 | [diff] [blame] | 4039 | ret = 0; |
| 4040 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 4041 | break; |
| 4042 | } |
Tejun Heo | 451af504 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4043 | return ret ?: nbytes; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 4044 | } |
| 4045 | |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4046 | static ssize_t mem_cgroup_reset(struct kernfs_open_file *of, char *buf, |
| 4047 | size_t nbytes, loff_t off) |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4048 | { |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4049 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4050 | struct page_counter *counter; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4051 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4052 | switch (MEMFILE_TYPE(of_cft(of)->private)) { |
| 4053 | case _MEM: |
| 4054 | counter = &memcg->memory; |
| 4055 | break; |
| 4056 | case _MEMSWAP: |
| 4057 | counter = &memcg->memsw; |
| 4058 | break; |
| 4059 | case _KMEM: |
| 4060 | counter = &memcg->kmem; |
| 4061 | break; |
Vladimir Davydov | d55f90b | 2016-01-20 15:02:44 -0800 | [diff] [blame] | 4062 | case _TCP: |
Johannes Weiner | 0db1529 | 2016-01-20 15:02:50 -0800 | [diff] [blame] | 4063 | counter = &memcg->tcpmem; |
Vladimir Davydov | d55f90b | 2016-01-20 15:02:44 -0800 | [diff] [blame] | 4064 | break; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4065 | default: |
| 4066 | BUG(); |
| 4067 | } |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 4068 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4069 | switch (MEMFILE_ATTR(of_cft(of)->private)) { |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4070 | case RES_MAX_USAGE: |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4071 | page_counter_reset_watermark(counter); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4072 | break; |
| 4073 | case RES_FAILCNT: |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4074 | counter->failcnt = 0; |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4075 | break; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4076 | default: |
| 4077 | BUG(); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 4078 | } |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 4079 | |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 4080 | return nbytes; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 4081 | } |
| 4082 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4083 | static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4084 | struct cftype *cft) |
| 4085 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4086 | return mem_cgroup_from_css(css)->move_charge_at_immigrate; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4087 | } |
| 4088 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4089 | #ifdef CONFIG_MMU |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4090 | static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4091 | struct cftype *cft, u64 val) |
| 4092 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4093 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4094 | |
Johannes Weiner | da34a84 | 2022-12-07 14:00:39 +0100 | [diff] [blame] | 4095 | pr_warn_once("Cgroup memory moving (move_charge_at_immigrate) is deprecated. " |
| 4096 | "Please report your usecase to linux-mm@kvack.org if you " |
| 4097 | "depend on this functionality.\n"); |
| 4098 | |
Johannes Weiner | 1dfab5a | 2015-02-11 15:26:09 -0800 | [diff] [blame] | 4099 | if (val & ~MOVE_MASK) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4100 | return -EINVAL; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4101 | |
Glauber Costa | ee5e847 | 2013-02-22 16:34:50 -0800 | [diff] [blame] | 4102 | /* |
| 4103 | * No kind of locking is needed in here, because ->can_attach() will |
| 4104 | * check this value once in the beginning of the process, and then carry |
| 4105 | * on with stale data. This means that changes to this value will only |
| 4106 | * affect task migrations starting after the change. |
| 4107 | */ |
| 4108 | memcg->move_charge_at_immigrate = val; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4109 | return 0; |
| 4110 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4111 | #else |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4112 | static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css, |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 4113 | struct cftype *cft, u64 val) |
| 4114 | { |
| 4115 | return -ENOSYS; |
| 4116 | } |
| 4117 | #endif |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 4118 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4119 | #ifdef CONFIG_NUMA |
Johannes Weiner | 113b7df | 2019-05-13 17:18:11 -0700 | [diff] [blame] | 4120 | |
| 4121 | #define LRU_ALL_FILE (BIT(LRU_INACTIVE_FILE) | BIT(LRU_ACTIVE_FILE)) |
| 4122 | #define LRU_ALL_ANON (BIT(LRU_INACTIVE_ANON) | BIT(LRU_ACTIVE_ANON)) |
| 4123 | #define LRU_ALL ((1 << NR_LRU_LISTS) - 1) |
| 4124 | |
| 4125 | static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg, |
Shakeel Butt | dd8657b | 2020-06-03 15:56:24 -0700 | [diff] [blame] | 4126 | int nid, unsigned int lru_mask, bool tree) |
Johannes Weiner | 113b7df | 2019-05-13 17:18:11 -0700 | [diff] [blame] | 4127 | { |
Johannes Weiner | 867e5e1 | 2019-11-30 17:55:34 -0800 | [diff] [blame] | 4128 | struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid)); |
Johannes Weiner | 113b7df | 2019-05-13 17:18:11 -0700 | [diff] [blame] | 4129 | unsigned long nr = 0; |
| 4130 | enum lru_list lru; |
| 4131 | |
| 4132 | VM_BUG_ON((unsigned)nid >= nr_node_ids); |
| 4133 | |
| 4134 | for_each_lru(lru) { |
| 4135 | if (!(BIT(lru) & lru_mask)) |
| 4136 | continue; |
Shakeel Butt | dd8657b | 2020-06-03 15:56:24 -0700 | [diff] [blame] | 4137 | if (tree) |
| 4138 | nr += lruvec_page_state(lruvec, NR_LRU_BASE + lru); |
| 4139 | else |
| 4140 | nr += lruvec_page_state_local(lruvec, NR_LRU_BASE + lru); |
Johannes Weiner | 113b7df | 2019-05-13 17:18:11 -0700 | [diff] [blame] | 4141 | } |
| 4142 | return nr; |
| 4143 | } |
| 4144 | |
| 4145 | static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg, |
Shakeel Butt | dd8657b | 2020-06-03 15:56:24 -0700 | [diff] [blame] | 4146 | unsigned int lru_mask, |
| 4147 | bool tree) |
Johannes Weiner | 113b7df | 2019-05-13 17:18:11 -0700 | [diff] [blame] | 4148 | { |
| 4149 | unsigned long nr = 0; |
| 4150 | enum lru_list lru; |
| 4151 | |
| 4152 | for_each_lru(lru) { |
| 4153 | if (!(BIT(lru) & lru_mask)) |
| 4154 | continue; |
Shakeel Butt | dd8657b | 2020-06-03 15:56:24 -0700 | [diff] [blame] | 4155 | if (tree) |
| 4156 | nr += memcg_page_state(memcg, NR_LRU_BASE + lru); |
| 4157 | else |
| 4158 | nr += memcg_page_state_local(memcg, NR_LRU_BASE + lru); |
Johannes Weiner | 113b7df | 2019-05-13 17:18:11 -0700 | [diff] [blame] | 4159 | } |
| 4160 | return nr; |
| 4161 | } |
| 4162 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4163 | static int memcg_numa_stat_show(struct seq_file *m, void *v) |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4164 | { |
Greg Thelen | 25485de | 2013-11-12 15:07:40 -0800 | [diff] [blame] | 4165 | struct numa_stat { |
| 4166 | const char *name; |
| 4167 | unsigned int lru_mask; |
| 4168 | }; |
| 4169 | |
| 4170 | static const struct numa_stat stats[] = { |
| 4171 | { "total", LRU_ALL }, |
| 4172 | { "file", LRU_ALL_FILE }, |
| 4173 | { "anon", LRU_ALL_ANON }, |
| 4174 | { "unevictable", BIT(LRU_UNEVICTABLE) }, |
| 4175 | }; |
| 4176 | const struct numa_stat *stat; |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4177 | int nid; |
Chris Down | aa9694b | 2019-03-05 15:45:52 -0800 | [diff] [blame] | 4178 | struct mem_cgroup *memcg = mem_cgroup_from_seq(m); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4179 | |
Shakeel Butt | fd25a9e | 2021-11-05 13:37:34 -0700 | [diff] [blame] | 4180 | mem_cgroup_flush_stats(); |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 4181 | |
Greg Thelen | 25485de | 2013-11-12 15:07:40 -0800 | [diff] [blame] | 4182 | for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) { |
Shakeel Butt | dd8657b | 2020-06-03 15:56:24 -0700 | [diff] [blame] | 4183 | seq_printf(m, "%s=%lu", stat->name, |
| 4184 | mem_cgroup_nr_lru_pages(memcg, stat->lru_mask, |
| 4185 | false)); |
| 4186 | for_each_node_state(nid, N_MEMORY) |
| 4187 | seq_printf(m, " N%d=%lu", nid, |
| 4188 | mem_cgroup_node_nr_lru_pages(memcg, nid, |
| 4189 | stat->lru_mask, false)); |
Greg Thelen | 25485de | 2013-11-12 15:07:40 -0800 | [diff] [blame] | 4190 | seq_putc(m, '\n'); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4191 | } |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4192 | |
Ying Han | 071aee1 | 2013-11-12 15:07:41 -0800 | [diff] [blame] | 4193 | for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) { |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4194 | |
Shakeel Butt | dd8657b | 2020-06-03 15:56:24 -0700 | [diff] [blame] | 4195 | seq_printf(m, "hierarchical_%s=%lu", stat->name, |
| 4196 | mem_cgroup_nr_lru_pages(memcg, stat->lru_mask, |
| 4197 | true)); |
| 4198 | for_each_node_state(nid, N_MEMORY) |
| 4199 | seq_printf(m, " N%d=%lu", nid, |
| 4200 | mem_cgroup_node_nr_lru_pages(memcg, nid, |
| 4201 | stat->lru_mask, true)); |
Ying Han | 071aee1 | 2013-11-12 15:07:41 -0800 | [diff] [blame] | 4202 | seq_putc(m, '\n'); |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4203 | } |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4204 | |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 4205 | return 0; |
| 4206 | } |
| 4207 | #endif /* CONFIG_NUMA */ |
| 4208 | |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 4209 | static const unsigned int memcg1_stats[] = { |
Johannes Weiner | 0d1c207 | 2020-06-03 16:01:54 -0700 | [diff] [blame] | 4210 | NR_FILE_PAGES, |
Johannes Weiner | be5d0a7 | 2020-06-03 16:01:57 -0700 | [diff] [blame] | 4211 | NR_ANON_MAPPED, |
Johannes Weiner | 468c398 | 2020-06-03 16:02:01 -0700 | [diff] [blame] | 4212 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 4213 | NR_ANON_THPS, |
| 4214 | #endif |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 4215 | NR_SHMEM, |
| 4216 | NR_FILE_MAPPED, |
| 4217 | NR_FILE_DIRTY, |
| 4218 | NR_WRITEBACK, |
Yang Shi | e09b0b6 | 2022-08-16 11:58:01 -0700 | [diff] [blame] | 4219 | WORKINGSET_REFAULT_ANON, |
| 4220 | WORKINGSET_REFAULT_FILE, |
Liu Shixin | 72a14e8 | 2023-09-15 18:58:44 +0800 | [diff] [blame] | 4221 | #ifdef CONFIG_SWAP |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 4222 | MEMCG_SWAP, |
Liu Shixin | 72a14e8 | 2023-09-15 18:58:44 +0800 | [diff] [blame] | 4223 | NR_SWAPCACHE, |
| 4224 | #endif |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 4225 | }; |
| 4226 | |
| 4227 | static const char *const memcg1_stat_names[] = { |
| 4228 | "cache", |
| 4229 | "rss", |
Johannes Weiner | 468c398 | 2020-06-03 16:02:01 -0700 | [diff] [blame] | 4230 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 4231 | "rss_huge", |
Johannes Weiner | 468c398 | 2020-06-03 16:02:01 -0700 | [diff] [blame] | 4232 | #endif |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 4233 | "shmem", |
| 4234 | "mapped_file", |
| 4235 | "dirty", |
| 4236 | "writeback", |
Yang Shi | e09b0b6 | 2022-08-16 11:58:01 -0700 | [diff] [blame] | 4237 | "workingset_refault_anon", |
| 4238 | "workingset_refault_file", |
Liu Shixin | 72a14e8 | 2023-09-15 18:58:44 +0800 | [diff] [blame] | 4239 | #ifdef CONFIG_SWAP |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 4240 | "swap", |
Liu Shixin | 72a14e8 | 2023-09-15 18:58:44 +0800 | [diff] [blame] | 4241 | "swapcached", |
| 4242 | #endif |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 4243 | }; |
| 4244 | |
Johannes Weiner | df0e53d | 2017-05-03 14:55:10 -0700 | [diff] [blame] | 4245 | /* Universal VM events cgroup1 shows, original sort order */ |
Greg Thelen | 8dd53fd | 2018-06-07 17:07:23 -0700 | [diff] [blame] | 4246 | static const unsigned int memcg1_events[] = { |
Johannes Weiner | df0e53d | 2017-05-03 14:55:10 -0700 | [diff] [blame] | 4247 | PGPGIN, |
| 4248 | PGPGOUT, |
| 4249 | PGFAULT, |
| 4250 | PGMAJFAULT, |
| 4251 | }; |
| 4252 | |
Yosry Ahmed | dddb44f | 2023-04-28 13:24:06 +0000 | [diff] [blame] | 4253 | static void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4254 | { |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4255 | unsigned long memory, memsw; |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 4256 | struct mem_cgroup *mi; |
| 4257 | unsigned int i; |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4258 | |
Johannes Weiner | 71cd311 | 2017-05-03 14:55:13 -0700 | [diff] [blame] | 4259 | BUILD_BUG_ON(ARRAY_SIZE(memcg1_stat_names) != ARRAY_SIZE(memcg1_stats)); |
Rickard Strandqvist | 70bc068 | 2014-12-12 16:56:41 -0800 | [diff] [blame] | 4260 | |
Shakeel Butt | fd25a9e | 2021-11-05 13:37:34 -0700 | [diff] [blame] | 4261 | mem_cgroup_flush_stats(); |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 4262 | |
Johannes Weiner | 71cd311 | 2017-05-03 14:55:13 -0700 | [diff] [blame] | 4263 | for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) { |
Johannes Weiner | 468c398 | 2020-06-03 16:02:01 -0700 | [diff] [blame] | 4264 | unsigned long nr; |
| 4265 | |
Yosry Ahmed | ff841a0 | 2023-09-22 17:57:39 +0000 | [diff] [blame] | 4266 | nr = memcg_page_state_local_output(memcg, memcg1_stats[i]); |
| 4267 | seq_buf_printf(s, "%s %lu\n", memcg1_stat_names[i], nr); |
Daisuke Nishimura | 1dd3a27 | 2009-09-23 15:56:43 -0700 | [diff] [blame] | 4268 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 4269 | |
Johannes Weiner | df0e53d | 2017-05-03 14:55:10 -0700 | [diff] [blame] | 4270 | for (i = 0; i < ARRAY_SIZE(memcg1_events); i++) |
Yosry Ahmed | dddb44f | 2023-04-28 13:24:06 +0000 | [diff] [blame] | 4271 | seq_buf_printf(s, "%s %lu\n", vm_event_name(memcg1_events[i]), |
| 4272 | memcg_events_local(memcg, memcg1_events[i])); |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 4273 | |
| 4274 | for (i = 0; i < NR_LRU_LISTS; i++) |
Yosry Ahmed | dddb44f | 2023-04-28 13:24:06 +0000 | [diff] [blame] | 4275 | seq_buf_printf(s, "%s %lu\n", lru_list_name(i), |
| 4276 | memcg_page_state_local(memcg, NR_LRU_BASE + i) * |
| 4277 | PAGE_SIZE); |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 4278 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4279 | /* Hierarchical information */ |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4280 | memory = memsw = PAGE_COUNTER_MAX; |
| 4281 | for (mi = memcg; mi; mi = parent_mem_cgroup(mi)) { |
Chris Down | 15b4256 | 2020-04-01 21:07:20 -0700 | [diff] [blame] | 4282 | memory = min(memory, READ_ONCE(mi->memory.max)); |
| 4283 | memsw = min(memsw, READ_ONCE(mi->memsw.max)); |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 4284 | } |
Yosry Ahmed | dddb44f | 2023-04-28 13:24:06 +0000 | [diff] [blame] | 4285 | seq_buf_printf(s, "hierarchical_memory_limit %llu\n", |
| 4286 | (u64)memory * PAGE_SIZE); |
Liu Shixin | 840ea53 | 2023-09-15 18:58:45 +0800 | [diff] [blame] | 4287 | seq_buf_printf(s, "hierarchical_memsw_limit %llu\n", |
| 4288 | (u64)memsw * PAGE_SIZE); |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4289 | |
Shakeel Butt | 8de7ecc6 | 2018-08-21 21:53:17 -0700 | [diff] [blame] | 4290 | for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) { |
zhongjiang-ali | 7de2e9f | 2020-11-01 17:07:30 -0800 | [diff] [blame] | 4291 | unsigned long nr; |
| 4292 | |
Yosry Ahmed | ff841a0 | 2023-09-22 17:57:39 +0000 | [diff] [blame] | 4293 | nr = memcg_page_state_output(memcg, memcg1_stats[i]); |
Yosry Ahmed | dddb44f | 2023-04-28 13:24:06 +0000 | [diff] [blame] | 4294 | seq_buf_printf(s, "total_%s %llu\n", memcg1_stat_names[i], |
Yosry Ahmed | ff841a0 | 2023-09-22 17:57:39 +0000 | [diff] [blame] | 4295 | (u64)nr); |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 4296 | } |
| 4297 | |
Shakeel Butt | 8de7ecc6 | 2018-08-21 21:53:17 -0700 | [diff] [blame] | 4298 | for (i = 0; i < ARRAY_SIZE(memcg1_events); i++) |
Yosry Ahmed | dddb44f | 2023-04-28 13:24:06 +0000 | [diff] [blame] | 4299 | seq_buf_printf(s, "total_%s %llu\n", |
| 4300 | vm_event_name(memcg1_events[i]), |
| 4301 | (u64)memcg_events(memcg, memcg1_events[i])); |
Johannes Weiner | af7c4b0 | 2012-05-29 15:07:08 -0700 | [diff] [blame] | 4302 | |
Shakeel Butt | 8de7ecc6 | 2018-08-21 21:53:17 -0700 | [diff] [blame] | 4303 | for (i = 0; i < NR_LRU_LISTS; i++) |
Yosry Ahmed | dddb44f | 2023-04-28 13:24:06 +0000 | [diff] [blame] | 4304 | seq_buf_printf(s, "total_%s %llu\n", lru_list_name(i), |
| 4305 | (u64)memcg_page_state(memcg, NR_LRU_BASE + i) * |
| 4306 | PAGE_SIZE); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 4307 | |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4308 | #ifdef CONFIG_DEBUG_VM |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4309 | { |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 4310 | pg_data_t *pgdat; |
| 4311 | struct mem_cgroup_per_node *mz; |
Johannes Weiner | 1431d4d | 2020-06-03 16:02:53 -0700 | [diff] [blame] | 4312 | unsigned long anon_cost = 0; |
| 4313 | unsigned long file_cost = 0; |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4314 | |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 4315 | for_each_online_pgdat(pgdat) { |
Johannes Weiner | a3747b5 | 2021-04-29 22:56:14 -0700 | [diff] [blame] | 4316 | mz = memcg->nodeinfo[pgdat->node_id]; |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4317 | |
Johannes Weiner | 1431d4d | 2020-06-03 16:02:53 -0700 | [diff] [blame] | 4318 | anon_cost += mz->lruvec.anon_cost; |
| 4319 | file_cost += mz->lruvec.file_cost; |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 4320 | } |
Yosry Ahmed | dddb44f | 2023-04-28 13:24:06 +0000 | [diff] [blame] | 4321 | seq_buf_printf(s, "anon_cost %lu\n", anon_cost); |
| 4322 | seq_buf_printf(s, "file_cost %lu\n", file_cost); |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 4323 | } |
| 4324 | #endif |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 4325 | } |
| 4326 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4327 | static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css, |
| 4328 | struct cftype *cft) |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4329 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4330 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4331 | |
KAMEZAWA Hiroyuki | 1f4c025 | 2011-07-26 16:08:21 -0700 | [diff] [blame] | 4332 | return mem_cgroup_swappiness(memcg); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4333 | } |
| 4334 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4335 | static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css, |
| 4336 | struct cftype *cft, u64 val) |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4337 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4338 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4339 | |
Baolin Wang | 37bc3cb | 2021-09-02 14:55:53 -0700 | [diff] [blame] | 4340 | if (val > 200) |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4341 | return -EINVAL; |
| 4342 | |
Shakeel Butt | a479203 | 2021-04-29 22:56:05 -0700 | [diff] [blame] | 4343 | if (!mem_cgroup_is_root(memcg)) |
Yue Zhao | 82b3aa2 | 2023-03-06 23:41:36 +0800 | [diff] [blame] | 4344 | WRITE_ONCE(memcg->swappiness, val); |
Johannes Weiner | 3dae7fe | 2014-06-04 16:07:01 -0700 | [diff] [blame] | 4345 | else |
Yue Zhao | 82b3aa2 | 2023-03-06 23:41:36 +0800 | [diff] [blame] | 4346 | WRITE_ONCE(vm_swappiness, val); |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 4347 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 4348 | return 0; |
| 4349 | } |
| 4350 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4351 | static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap) |
| 4352 | { |
| 4353 | struct mem_cgroup_threshold_ary *t; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4354 | unsigned long usage; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4355 | int i; |
| 4356 | |
| 4357 | rcu_read_lock(); |
| 4358 | if (!swap) |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4359 | t = rcu_dereference(memcg->thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4360 | else |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4361 | t = rcu_dereference(memcg->memsw_thresholds.primary); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4362 | |
| 4363 | if (!t) |
| 4364 | goto unlock; |
| 4365 | |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 4366 | usage = mem_cgroup_usage(memcg, swap); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4367 | |
| 4368 | /* |
Sha Zhengju | 748dad3 | 2012-05-29 15:06:57 -0700 | [diff] [blame] | 4369 | * current_threshold points to threshold just below or equal to usage. |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4370 | * If it's not true, a threshold was crossed after last |
| 4371 | * call of __mem_cgroup_threshold(). |
| 4372 | */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 4373 | i = t->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4374 | |
| 4375 | /* |
| 4376 | * Iterate backward over array of thresholds starting from |
| 4377 | * current_threshold and check if a threshold is crossed. |
| 4378 | * If none of thresholds below usage is crossed, we read |
| 4379 | * only one element of the array here. |
| 4380 | */ |
| 4381 | for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--) |
| 4382 | eventfd_signal(t->entries[i].eventfd, 1); |
| 4383 | |
| 4384 | /* i = current_threshold + 1 */ |
| 4385 | i++; |
| 4386 | |
| 4387 | /* |
| 4388 | * Iterate forward over array of thresholds starting from |
| 4389 | * current_threshold+1 and check if a threshold is crossed. |
| 4390 | * If none of thresholds above usage is crossed, we read |
| 4391 | * only one element of the array here. |
| 4392 | */ |
| 4393 | for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++) |
| 4394 | eventfd_signal(t->entries[i].eventfd, 1); |
| 4395 | |
| 4396 | /* Update current_threshold */ |
Phil Carmody | 5407a56 | 2010-05-26 14:42:42 -0700 | [diff] [blame] | 4397 | t->current_threshold = i - 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4398 | unlock: |
| 4399 | rcu_read_unlock(); |
| 4400 | } |
| 4401 | |
| 4402 | static void mem_cgroup_threshold(struct mem_cgroup *memcg) |
| 4403 | { |
Kirill A. Shutemov | ad4ca5f | 2010-10-07 12:59:27 -0700 | [diff] [blame] | 4404 | while (memcg) { |
| 4405 | __mem_cgroup_threshold(memcg, false); |
Johannes Weiner | 7941d21 | 2016-01-14 15:21:23 -0800 | [diff] [blame] | 4406 | if (do_memsw_account()) |
Kirill A. Shutemov | ad4ca5f | 2010-10-07 12:59:27 -0700 | [diff] [blame] | 4407 | __mem_cgroup_threshold(memcg, true); |
| 4408 | |
| 4409 | memcg = parent_mem_cgroup(memcg); |
| 4410 | } |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4411 | } |
| 4412 | |
| 4413 | static int compare_thresholds(const void *a, const void *b) |
| 4414 | { |
| 4415 | const struct mem_cgroup_threshold *_a = a; |
| 4416 | const struct mem_cgroup_threshold *_b = b; |
| 4417 | |
Greg Thelen | 2bff24a | 2013-09-11 14:23:08 -0700 | [diff] [blame] | 4418 | if (_a->threshold > _b->threshold) |
| 4419 | return 1; |
| 4420 | |
| 4421 | if (_a->threshold < _b->threshold) |
| 4422 | return -1; |
| 4423 | |
| 4424 | return 0; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4425 | } |
| 4426 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4427 | static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4428 | { |
| 4429 | struct mem_cgroup_eventfd_list *ev; |
| 4430 | |
Michal Hocko | 2bcf2e9 | 2014-07-30 16:08:33 -0700 | [diff] [blame] | 4431 | spin_lock(&memcg_oom_lock); |
| 4432 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4433 | list_for_each_entry(ev, &memcg->oom_notify, list) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4434 | eventfd_signal(ev->eventfd, 1); |
Michal Hocko | 2bcf2e9 | 2014-07-30 16:08:33 -0700 | [diff] [blame] | 4435 | |
| 4436 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4437 | return 0; |
| 4438 | } |
| 4439 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4440 | static void mem_cgroup_oom_notify(struct mem_cgroup *memcg) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4441 | { |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4442 | struct mem_cgroup *iter; |
| 4443 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4444 | for_each_mem_cgroup_tree(iter, memcg) |
KAMEZAWA Hiroyuki | 7d74b06 | 2010-10-27 15:33:41 -0700 | [diff] [blame] | 4445 | mem_cgroup_oom_notify_cb(iter); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4446 | } |
| 4447 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4448 | static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4449 | struct eventfd_ctx *eventfd, const char *args, enum res_type type) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4450 | { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4451 | struct mem_cgroup_thresholds *thresholds; |
| 4452 | struct mem_cgroup_threshold_ary *new; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4453 | unsigned long threshold; |
| 4454 | unsigned long usage; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4455 | int i, size, ret; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4456 | |
Johannes Weiner | 650c5e5 | 2015-02-11 15:26:03 -0800 | [diff] [blame] | 4457 | ret = page_counter_memparse(args, "-1", &threshold); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4458 | if (ret) |
| 4459 | return ret; |
| 4460 | |
| 4461 | mutex_lock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4462 | |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 4463 | if (type == _MEM) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4464 | thresholds = &memcg->thresholds; |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 4465 | usage = mem_cgroup_usage(memcg, false); |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 4466 | } else if (type == _MEMSWAP) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4467 | thresholds = &memcg->memsw_thresholds; |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 4468 | usage = mem_cgroup_usage(memcg, true); |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 4469 | } else |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4470 | BUG(); |
| 4471 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4472 | /* Check if a threshold crossed before adding a new one */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4473 | if (thresholds->primary) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4474 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 4475 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4476 | size = thresholds->primary ? thresholds->primary->size + 1 : 1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4477 | |
| 4478 | /* Allocate memory for new array of thresholds */ |
Gustavo A. R. Silva | 67b8046 | 2019-03-05 15:44:05 -0800 | [diff] [blame] | 4479 | new = kmalloc(struct_size(new, entries, size), GFP_KERNEL); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4480 | if (!new) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4481 | ret = -ENOMEM; |
| 4482 | goto unlock; |
| 4483 | } |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4484 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4485 | |
| 4486 | /* Copy thresholds (if any) to new array */ |
Gustavo A. R. Silva | e90342e | 2020-10-13 16:52:36 -0700 | [diff] [blame] | 4487 | if (thresholds->primary) |
| 4488 | memcpy(new->entries, thresholds->primary->entries, |
| 4489 | flex_array_size(new, entries, size - 1)); |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4490 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4491 | /* Add new threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4492 | new->entries[size - 1].eventfd = eventfd; |
| 4493 | new->entries[size - 1].threshold = threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4494 | |
| 4495 | /* Sort thresholds. Registering of new threshold isn't time-critical */ |
Gustavo A. R. Silva | 61e604e6 | 2020-10-13 16:52:39 -0700 | [diff] [blame] | 4496 | sort(new->entries, size, sizeof(*new->entries), |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4497 | compare_thresholds, NULL); |
| 4498 | |
| 4499 | /* Find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4500 | new->current_threshold = -1; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4501 | for (i = 0; i < size; i++) { |
Sha Zhengju | 748dad3 | 2012-05-29 15:06:57 -0700 | [diff] [blame] | 4502 | if (new->entries[i].threshold <= usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4503 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4504 | * new->current_threshold will not be used until |
| 4505 | * rcu_assign_pointer(), so it's safe to increment |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4506 | * it here. |
| 4507 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4508 | ++new->current_threshold; |
Sha Zhengju | 748dad3 | 2012-05-29 15:06:57 -0700 | [diff] [blame] | 4509 | } else |
| 4510 | break; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4511 | } |
| 4512 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4513 | /* Free old spare buffer and save old primary buffer as spare */ |
| 4514 | kfree(thresholds->spare); |
| 4515 | thresholds->spare = thresholds->primary; |
| 4516 | |
| 4517 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4518 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4519 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4520 | synchronize_rcu(); |
| 4521 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4522 | unlock: |
| 4523 | mutex_unlock(&memcg->thresholds_lock); |
| 4524 | |
| 4525 | return ret; |
| 4526 | } |
| 4527 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4528 | static int mem_cgroup_usage_register_event(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4529 | struct eventfd_ctx *eventfd, const char *args) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4530 | { |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4531 | return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEM); |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4532 | } |
| 4533 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4534 | static int memsw_cgroup_usage_register_event(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4535 | struct eventfd_ctx *eventfd, const char *args) |
| 4536 | { |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4537 | return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEMSWAP); |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4538 | } |
| 4539 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4540 | static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4541 | struct eventfd_ctx *eventfd, enum res_type type) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4542 | { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4543 | struct mem_cgroup_thresholds *thresholds; |
| 4544 | struct mem_cgroup_threshold_ary *new; |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 4545 | unsigned long usage; |
Chunguang Xu | 7d36665 | 2020-03-21 18:22:10 -0700 | [diff] [blame] | 4546 | int i, j, size, entries; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4547 | |
| 4548 | mutex_lock(&memcg->thresholds_lock); |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 4549 | |
| 4550 | if (type == _MEM) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4551 | thresholds = &memcg->thresholds; |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 4552 | usage = mem_cgroup_usage(memcg, false); |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 4553 | } else if (type == _MEMSWAP) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4554 | thresholds = &memcg->memsw_thresholds; |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 4555 | usage = mem_cgroup_usage(memcg, true); |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 4556 | } else |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4557 | BUG(); |
| 4558 | |
Anton Vorontsov | 371528c | 2012-02-24 05:14:46 +0400 | [diff] [blame] | 4559 | if (!thresholds->primary) |
| 4560 | goto unlock; |
| 4561 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4562 | /* Check if a threshold crossed before removing */ |
| 4563 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
| 4564 | |
| 4565 | /* Calculate new number of threshold */ |
Chunguang Xu | 7d36665 | 2020-03-21 18:22:10 -0700 | [diff] [blame] | 4566 | size = entries = 0; |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4567 | for (i = 0; i < thresholds->primary->size; i++) { |
| 4568 | if (thresholds->primary->entries[i].eventfd != eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4569 | size++; |
Chunguang Xu | 7d36665 | 2020-03-21 18:22:10 -0700 | [diff] [blame] | 4570 | else |
| 4571 | entries++; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4572 | } |
| 4573 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4574 | new = thresholds->spare; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4575 | |
Chunguang Xu | 7d36665 | 2020-03-21 18:22:10 -0700 | [diff] [blame] | 4576 | /* If no items related to eventfd have been cleared, nothing to do */ |
| 4577 | if (!entries) |
| 4578 | goto unlock; |
| 4579 | |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4580 | /* Set thresholds array to NULL if we don't have thresholds */ |
| 4581 | if (!size) { |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4582 | kfree(new); |
| 4583 | new = NULL; |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4584 | goto swap_buffers; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4585 | } |
| 4586 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4587 | new->size = size; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4588 | |
| 4589 | /* Copy thresholds and find current threshold */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4590 | new->current_threshold = -1; |
| 4591 | for (i = 0, j = 0; i < thresholds->primary->size; i++) { |
| 4592 | if (thresholds->primary->entries[i].eventfd == eventfd) |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4593 | continue; |
| 4594 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4595 | new->entries[j] = thresholds->primary->entries[i]; |
Sha Zhengju | 748dad3 | 2012-05-29 15:06:57 -0700 | [diff] [blame] | 4596 | if (new->entries[j].threshold <= usage) { |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4597 | /* |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4598 | * new->current_threshold will not be used |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4599 | * until rcu_assign_pointer(), so it's safe to increment |
| 4600 | * it here. |
| 4601 | */ |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4602 | ++new->current_threshold; |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4603 | } |
| 4604 | j++; |
| 4605 | } |
| 4606 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4607 | swap_buffers: |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4608 | /* Swap primary and spare array */ |
| 4609 | thresholds->spare = thresholds->primary; |
Sha Zhengju | 8c75776 | 2012-05-10 13:01:45 -0700 | [diff] [blame] | 4610 | |
Kirill A. Shutemov | 2c488db | 2010-05-26 14:42:47 -0700 | [diff] [blame] | 4611 | rcu_assign_pointer(thresholds->primary, new); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4612 | |
Kirill A. Shutemov | 907860e | 2010-05-26 14:42:46 -0700 | [diff] [blame] | 4613 | /* To be sure that nobody uses thresholds */ |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4614 | synchronize_rcu(); |
Martijn Coenen | 6611d8d | 2016-01-15 16:57:49 -0800 | [diff] [blame] | 4615 | |
| 4616 | /* If all events are unregistered, free the spare array */ |
| 4617 | if (!new) { |
| 4618 | kfree(thresholds->spare); |
| 4619 | thresholds->spare = NULL; |
| 4620 | } |
Anton Vorontsov | 371528c | 2012-02-24 05:14:46 +0400 | [diff] [blame] | 4621 | unlock: |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4622 | mutex_unlock(&memcg->thresholds_lock); |
Kirill A. Shutemov | 2e72b63 | 2010-03-10 15:22:24 -0800 | [diff] [blame] | 4623 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 4624 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4625 | static void mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4626 | struct eventfd_ctx *eventfd) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4627 | { |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4628 | return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEM); |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4629 | } |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4630 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4631 | static void memsw_cgroup_usage_unregister_event(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4632 | struct eventfd_ctx *eventfd) |
| 4633 | { |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4634 | return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEMSWAP); |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4635 | } |
| 4636 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4637 | static int mem_cgroup_oom_register_event(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4638 | struct eventfd_ctx *eventfd, const char *args) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4639 | { |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4640 | struct mem_cgroup_eventfd_list *event; |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4641 | |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4642 | event = kmalloc(sizeof(*event), GFP_KERNEL); |
| 4643 | if (!event) |
| 4644 | return -ENOMEM; |
| 4645 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4646 | spin_lock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4647 | |
| 4648 | event->eventfd = eventfd; |
| 4649 | list_add(&event->list, &memcg->oom_notify); |
| 4650 | |
| 4651 | /* already in OOM ? */ |
Tejun Heo | c2b42d3 | 2015-06-24 16:58:23 -0700 | [diff] [blame] | 4652 | if (memcg->under_oom) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4653 | eventfd_signal(eventfd, 1); |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4654 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4655 | |
| 4656 | return 0; |
| 4657 | } |
| 4658 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4659 | static void mem_cgroup_oom_unregister_event(struct mem_cgroup *memcg, |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4660 | struct eventfd_ctx *eventfd) |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4661 | { |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4662 | struct mem_cgroup_eventfd_list *ev, *tmp; |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4663 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4664 | spin_lock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4665 | |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4666 | list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) { |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4667 | if (ev->eventfd == eventfd) { |
| 4668 | list_del(&ev->list); |
| 4669 | kfree(ev); |
| 4670 | } |
| 4671 | } |
| 4672 | |
Michal Hocko | 1af8efe | 2011-07-26 16:08:24 -0700 | [diff] [blame] | 4673 | spin_unlock(&memcg_oom_lock); |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 4674 | } |
| 4675 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 4676 | static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4677 | { |
Chris Down | aa9694b | 2019-03-05 15:45:52 -0800 | [diff] [blame] | 4678 | struct mem_cgroup *memcg = mem_cgroup_from_seq(sf); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4679 | |
Yue Zhao | 17c56de | 2023-03-06 23:41:37 +0800 | [diff] [blame] | 4680 | seq_printf(sf, "oom_kill_disable %d\n", READ_ONCE(memcg->oom_kill_disable)); |
Tejun Heo | c2b42d3 | 2015-06-24 16:58:23 -0700 | [diff] [blame] | 4681 | seq_printf(sf, "under_oom %d\n", (bool)memcg->under_oom); |
Roman Gushchin | fe6bdfc | 2018-06-14 15:28:05 -0700 | [diff] [blame] | 4682 | seq_printf(sf, "oom_kill %lu\n", |
| 4683 | atomic_long_read(&memcg->memory_events[MEMCG_OOM_KILL])); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4684 | return 0; |
| 4685 | } |
| 4686 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4687 | static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css, |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4688 | struct cftype *cft, u64 val) |
| 4689 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 4690 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4691 | |
| 4692 | /* cannot set to root cgroup and only 0 and 1 are allowed */ |
Shakeel Butt | a479203 | 2021-04-29 22:56:05 -0700 | [diff] [blame] | 4693 | if (mem_cgroup_is_root(memcg) || !((val == 0) || (val == 1))) |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4694 | return -EINVAL; |
| 4695 | |
Yue Zhao | 17c56de | 2023-03-06 23:41:37 +0800 | [diff] [blame] | 4696 | WRITE_ONCE(memcg->oom_kill_disable, val); |
KAMEZAWA Hiroyuki | 4d845eb | 2010-06-29 15:05:18 -0700 | [diff] [blame] | 4697 | if (!val) |
Raghavendra K T | c0ff4b8 | 2011-11-02 13:38:15 -0700 | [diff] [blame] | 4698 | memcg_oom_recover(memcg); |
Johannes Weiner | 3dae7fe | 2014-06-04 16:07:01 -0700 | [diff] [blame] | 4699 | |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 4700 | return 0; |
| 4701 | } |
| 4702 | |
Tejun Heo | 52ebea7 | 2015-05-22 17:13:37 -0400 | [diff] [blame] | 4703 | #ifdef CONFIG_CGROUP_WRITEBACK |
| 4704 | |
Tejun Heo | 3a8e9ac | 2019-08-29 15:47:19 -0700 | [diff] [blame] | 4705 | #include <trace/events/writeback.h> |
| 4706 | |
Tejun Heo | 841710a | 2015-05-22 18:23:33 -0400 | [diff] [blame] | 4707 | static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp) |
| 4708 | { |
| 4709 | return wb_domain_init(&memcg->cgwb_domain, gfp); |
| 4710 | } |
| 4711 | |
| 4712 | static void memcg_wb_domain_exit(struct mem_cgroup *memcg) |
| 4713 | { |
| 4714 | wb_domain_exit(&memcg->cgwb_domain); |
| 4715 | } |
| 4716 | |
Tejun Heo | 2529bb3 | 2015-05-22 18:23:34 -0400 | [diff] [blame] | 4717 | static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg) |
| 4718 | { |
| 4719 | wb_domain_size_changed(&memcg->cgwb_domain); |
| 4720 | } |
| 4721 | |
Tejun Heo | 841710a | 2015-05-22 18:23:33 -0400 | [diff] [blame] | 4722 | struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb) |
| 4723 | { |
| 4724 | struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css); |
| 4725 | |
| 4726 | if (!memcg->css.parent) |
| 4727 | return NULL; |
| 4728 | |
| 4729 | return &memcg->cgwb_domain; |
| 4730 | } |
| 4731 | |
Tejun Heo | c2aa723 | 2015-05-22 18:23:35 -0400 | [diff] [blame] | 4732 | /** |
| 4733 | * mem_cgroup_wb_stats - retrieve writeback related stats from its memcg |
| 4734 | * @wb: bdi_writeback in question |
Tejun Heo | c5edf9c | 2015-09-29 13:04:26 -0400 | [diff] [blame] | 4735 | * @pfilepages: out parameter for number of file pages |
| 4736 | * @pheadroom: out parameter for number of allocatable pages according to memcg |
Tejun Heo | c2aa723 | 2015-05-22 18:23:35 -0400 | [diff] [blame] | 4737 | * @pdirty: out parameter for number of dirty pages |
| 4738 | * @pwriteback: out parameter for number of pages under writeback |
| 4739 | * |
Tejun Heo | c5edf9c | 2015-09-29 13:04:26 -0400 | [diff] [blame] | 4740 | * Determine the numbers of file, headroom, dirty, and writeback pages in |
| 4741 | * @wb's memcg. File, dirty and writeback are self-explanatory. Headroom |
| 4742 | * is a bit more involved. |
Tejun Heo | c2aa723 | 2015-05-22 18:23:35 -0400 | [diff] [blame] | 4743 | * |
Tejun Heo | c5edf9c | 2015-09-29 13:04:26 -0400 | [diff] [blame] | 4744 | * A memcg's headroom is "min(max, high) - used". In the hierarchy, the |
| 4745 | * headroom is calculated as the lowest headroom of itself and the |
| 4746 | * ancestors. Note that this doesn't consider the actual amount of |
| 4747 | * available memory in the system. The caller should further cap |
| 4748 | * *@pheadroom accordingly. |
Tejun Heo | c2aa723 | 2015-05-22 18:23:35 -0400 | [diff] [blame] | 4749 | */ |
Tejun Heo | c5edf9c | 2015-09-29 13:04:26 -0400 | [diff] [blame] | 4750 | void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages, |
| 4751 | unsigned long *pheadroom, unsigned long *pdirty, |
| 4752 | unsigned long *pwriteback) |
Tejun Heo | c2aa723 | 2015-05-22 18:23:35 -0400 | [diff] [blame] | 4753 | { |
| 4754 | struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css); |
| 4755 | struct mem_cgroup *parent; |
Tejun Heo | c2aa723 | 2015-05-22 18:23:35 -0400 | [diff] [blame] | 4756 | |
Yosry Ahmed | 190409c | 2023-04-21 17:40:17 +0000 | [diff] [blame] | 4757 | mem_cgroup_flush_stats(); |
Tejun Heo | c2aa723 | 2015-05-22 18:23:35 -0400 | [diff] [blame] | 4758 | |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 4759 | *pdirty = memcg_page_state(memcg, NR_FILE_DIRTY); |
| 4760 | *pwriteback = memcg_page_state(memcg, NR_WRITEBACK); |
| 4761 | *pfilepages = memcg_page_state(memcg, NR_INACTIVE_FILE) + |
| 4762 | memcg_page_state(memcg, NR_ACTIVE_FILE); |
| 4763 | |
Tejun Heo | c5edf9c | 2015-09-29 13:04:26 -0400 | [diff] [blame] | 4764 | *pheadroom = PAGE_COUNTER_MAX; |
Tejun Heo | c2aa723 | 2015-05-22 18:23:35 -0400 | [diff] [blame] | 4765 | while ((parent = parent_mem_cgroup(memcg))) { |
Chris Down | 15b4256 | 2020-04-01 21:07:20 -0700 | [diff] [blame] | 4766 | unsigned long ceiling = min(READ_ONCE(memcg->memory.max), |
Jakub Kicinski | d1663a9 | 2020-06-01 21:49:49 -0700 | [diff] [blame] | 4767 | READ_ONCE(memcg->memory.high)); |
Tejun Heo | c2aa723 | 2015-05-22 18:23:35 -0400 | [diff] [blame] | 4768 | unsigned long used = page_counter_read(&memcg->memory); |
| 4769 | |
Tejun Heo | c5edf9c | 2015-09-29 13:04:26 -0400 | [diff] [blame] | 4770 | *pheadroom = min(*pheadroom, ceiling - min(ceiling, used)); |
Tejun Heo | c2aa723 | 2015-05-22 18:23:35 -0400 | [diff] [blame] | 4771 | memcg = parent; |
| 4772 | } |
Tejun Heo | c2aa723 | 2015-05-22 18:23:35 -0400 | [diff] [blame] | 4773 | } |
| 4774 | |
Tejun Heo | 97b2782 | 2019-08-26 09:06:56 -0700 | [diff] [blame] | 4775 | /* |
| 4776 | * Foreign dirty flushing |
| 4777 | * |
| 4778 | * There's an inherent mismatch between memcg and writeback. The former |
Ingo Molnar | f0953a1 | 2021-05-06 18:06:47 -0700 | [diff] [blame] | 4779 | * tracks ownership per-page while the latter per-inode. This was a |
Tejun Heo | 97b2782 | 2019-08-26 09:06:56 -0700 | [diff] [blame] | 4780 | * deliberate design decision because honoring per-page ownership in the |
| 4781 | * writeback path is complicated, may lead to higher CPU and IO overheads |
| 4782 | * and deemed unnecessary given that write-sharing an inode across |
| 4783 | * different cgroups isn't a common use-case. |
| 4784 | * |
| 4785 | * Combined with inode majority-writer ownership switching, this works well |
| 4786 | * enough in most cases but there are some pathological cases. For |
| 4787 | * example, let's say there are two cgroups A and B which keep writing to |
| 4788 | * different but confined parts of the same inode. B owns the inode and |
| 4789 | * A's memory is limited far below B's. A's dirty ratio can rise enough to |
| 4790 | * trigger balance_dirty_pages() sleeps but B's can be low enough to avoid |
| 4791 | * triggering background writeback. A will be slowed down without a way to |
| 4792 | * make writeback of the dirty pages happen. |
| 4793 | * |
Ingo Molnar | f0953a1 | 2021-05-06 18:06:47 -0700 | [diff] [blame] | 4794 | * Conditions like the above can lead to a cgroup getting repeatedly and |
Tejun Heo | 97b2782 | 2019-08-26 09:06:56 -0700 | [diff] [blame] | 4795 | * severely throttled after making some progress after each |
Ingo Molnar | f0953a1 | 2021-05-06 18:06:47 -0700 | [diff] [blame] | 4796 | * dirty_expire_interval while the underlying IO device is almost |
Tejun Heo | 97b2782 | 2019-08-26 09:06:56 -0700 | [diff] [blame] | 4797 | * completely idle. |
| 4798 | * |
| 4799 | * Solving this problem completely requires matching the ownership tracking |
| 4800 | * granularities between memcg and writeback in either direction. However, |
| 4801 | * the more egregious behaviors can be avoided by simply remembering the |
| 4802 | * most recent foreign dirtying events and initiating remote flushes on |
| 4803 | * them when local writeback isn't enough to keep the memory clean enough. |
| 4804 | * |
| 4805 | * The following two functions implement such mechanism. When a foreign |
| 4806 | * page - a page whose memcg and writeback ownerships don't match - is |
| 4807 | * dirtied, mem_cgroup_track_foreign_dirty() records the inode owning |
| 4808 | * bdi_writeback on the page owning memcg. When balance_dirty_pages() |
| 4809 | * decides that the memcg needs to sleep due to high dirty ratio, it calls |
| 4810 | * mem_cgroup_flush_foreign() which queues writeback on the recorded |
| 4811 | * foreign bdi_writebacks which haven't expired. Both the numbers of |
| 4812 | * recorded bdi_writebacks and concurrent in-flight foreign writebacks are |
| 4813 | * limited to MEMCG_CGWB_FRN_CNT. |
| 4814 | * |
| 4815 | * The mechanism only remembers IDs and doesn't hold any object references. |
| 4816 | * As being wrong occasionally doesn't matter, updates and accesses to the |
| 4817 | * records are lockless and racy. |
| 4818 | */ |
Matthew Wilcox (Oracle) | 9d8053f | 2021-05-04 11:43:01 -0400 | [diff] [blame] | 4819 | void mem_cgroup_track_foreign_dirty_slowpath(struct folio *folio, |
Tejun Heo | 97b2782 | 2019-08-26 09:06:56 -0700 | [diff] [blame] | 4820 | struct bdi_writeback *wb) |
| 4821 | { |
Matthew Wilcox (Oracle) | 9d8053f | 2021-05-04 11:43:01 -0400 | [diff] [blame] | 4822 | struct mem_cgroup *memcg = folio_memcg(folio); |
Tejun Heo | 97b2782 | 2019-08-26 09:06:56 -0700 | [diff] [blame] | 4823 | struct memcg_cgwb_frn *frn; |
| 4824 | u64 now = get_jiffies_64(); |
| 4825 | u64 oldest_at = now; |
| 4826 | int oldest = -1; |
| 4827 | int i; |
| 4828 | |
Matthew Wilcox (Oracle) | 9d8053f | 2021-05-04 11:43:01 -0400 | [diff] [blame] | 4829 | trace_track_foreign_dirty(folio, wb); |
Tejun Heo | 3a8e9ac | 2019-08-29 15:47:19 -0700 | [diff] [blame] | 4830 | |
Tejun Heo | 97b2782 | 2019-08-26 09:06:56 -0700 | [diff] [blame] | 4831 | /* |
| 4832 | * Pick the slot to use. If there is already a slot for @wb, keep |
| 4833 | * using it. If not replace the oldest one which isn't being |
| 4834 | * written out. |
| 4835 | */ |
| 4836 | for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) { |
| 4837 | frn = &memcg->cgwb_frn[i]; |
| 4838 | if (frn->bdi_id == wb->bdi->id && |
| 4839 | frn->memcg_id == wb->memcg_css->id) |
| 4840 | break; |
| 4841 | if (time_before64(frn->at, oldest_at) && |
| 4842 | atomic_read(&frn->done.cnt) == 1) { |
| 4843 | oldest = i; |
| 4844 | oldest_at = frn->at; |
| 4845 | } |
| 4846 | } |
| 4847 | |
| 4848 | if (i < MEMCG_CGWB_FRN_CNT) { |
| 4849 | /* |
| 4850 | * Re-using an existing one. Update timestamp lazily to |
| 4851 | * avoid making the cacheline hot. We want them to be |
| 4852 | * reasonably up-to-date and significantly shorter than |
| 4853 | * dirty_expire_interval as that's what expires the record. |
| 4854 | * Use the shorter of 1s and dirty_expire_interval / 8. |
| 4855 | */ |
| 4856 | unsigned long update_intv = |
| 4857 | min_t(unsigned long, HZ, |
| 4858 | msecs_to_jiffies(dirty_expire_interval * 10) / 8); |
| 4859 | |
| 4860 | if (time_before64(frn->at, now - update_intv)) |
| 4861 | frn->at = now; |
| 4862 | } else if (oldest >= 0) { |
| 4863 | /* replace the oldest free one */ |
| 4864 | frn = &memcg->cgwb_frn[oldest]; |
| 4865 | frn->bdi_id = wb->bdi->id; |
| 4866 | frn->memcg_id = wb->memcg_css->id; |
| 4867 | frn->at = now; |
| 4868 | } |
| 4869 | } |
| 4870 | |
| 4871 | /* issue foreign writeback flushes for recorded foreign dirtying events */ |
| 4872 | void mem_cgroup_flush_foreign(struct bdi_writeback *wb) |
| 4873 | { |
| 4874 | struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css); |
| 4875 | unsigned long intv = msecs_to_jiffies(dirty_expire_interval * 10); |
| 4876 | u64 now = jiffies_64; |
| 4877 | int i; |
| 4878 | |
| 4879 | for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) { |
| 4880 | struct memcg_cgwb_frn *frn = &memcg->cgwb_frn[i]; |
| 4881 | |
| 4882 | /* |
| 4883 | * If the record is older than dirty_expire_interval, |
| 4884 | * writeback on it has already started. No need to kick it |
| 4885 | * off again. Also, don't start a new one if there's |
| 4886 | * already one in flight. |
| 4887 | */ |
| 4888 | if (time_after64(frn->at, now - intv) && |
| 4889 | atomic_read(&frn->done.cnt) == 1) { |
| 4890 | frn->at = 0; |
Tejun Heo | 3a8e9ac | 2019-08-29 15:47:19 -0700 | [diff] [blame] | 4891 | trace_flush_foreign(wb, frn->bdi_id, frn->memcg_id); |
Shakeel Butt | 7490a2d | 2021-09-02 14:53:27 -0700 | [diff] [blame] | 4892 | cgroup_writeback_by_id(frn->bdi_id, frn->memcg_id, |
Tejun Heo | 97b2782 | 2019-08-26 09:06:56 -0700 | [diff] [blame] | 4893 | WB_REASON_FOREIGN_FLUSH, |
| 4894 | &frn->done); |
| 4895 | } |
| 4896 | } |
| 4897 | } |
| 4898 | |
Tejun Heo | 841710a | 2015-05-22 18:23:33 -0400 | [diff] [blame] | 4899 | #else /* CONFIG_CGROUP_WRITEBACK */ |
| 4900 | |
| 4901 | static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp) |
| 4902 | { |
| 4903 | return 0; |
| 4904 | } |
| 4905 | |
| 4906 | static void memcg_wb_domain_exit(struct mem_cgroup *memcg) |
| 4907 | { |
| 4908 | } |
| 4909 | |
Tejun Heo | 2529bb3 | 2015-05-22 18:23:34 -0400 | [diff] [blame] | 4910 | static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg) |
| 4911 | { |
| 4912 | } |
| 4913 | |
Tejun Heo | 52ebea7 | 2015-05-22 17:13:37 -0400 | [diff] [blame] | 4914 | #endif /* CONFIG_CGROUP_WRITEBACK */ |
| 4915 | |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4916 | /* |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 4917 | * DO NOT USE IN NEW FILES. |
| 4918 | * |
| 4919 | * "cgroup.event_control" implementation. |
| 4920 | * |
| 4921 | * This is way over-engineered. It tries to support fully configurable |
| 4922 | * events for each user. Such level of flexibility is completely |
| 4923 | * unnecessary especially in the light of the planned unified hierarchy. |
| 4924 | * |
| 4925 | * Please deprecate this and replace with something simpler if at all |
| 4926 | * possible. |
| 4927 | */ |
| 4928 | |
| 4929 | /* |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4930 | * Unregister event and free resources. |
| 4931 | * |
| 4932 | * Gets called from workqueue. |
| 4933 | */ |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 4934 | static void memcg_event_remove(struct work_struct *work) |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4935 | { |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 4936 | struct mem_cgroup_event *event = |
| 4937 | container_of(work, struct mem_cgroup_event, remove); |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4938 | struct mem_cgroup *memcg = event->memcg; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4939 | |
| 4940 | remove_wait_queue(event->wqh, &event->wait); |
| 4941 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4942 | event->unregister_event(memcg, event->eventfd); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4943 | |
| 4944 | /* Notify userspace the event is going away. */ |
| 4945 | eventfd_signal(event->eventfd, 1); |
| 4946 | |
| 4947 | eventfd_ctx_put(event->eventfd); |
| 4948 | kfree(event); |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4949 | css_put(&memcg->css); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4950 | } |
| 4951 | |
| 4952 | /* |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 4953 | * Gets called on EPOLLHUP on eventfd when user closes it. |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4954 | * |
| 4955 | * Called with wqh->lock held and interrupts disabled. |
| 4956 | */ |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 4957 | static int memcg_event_wake(wait_queue_entry_t *wait, unsigned mode, |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 4958 | int sync, void *key) |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4959 | { |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 4960 | struct mem_cgroup_event *event = |
| 4961 | container_of(wait, struct mem_cgroup_event, wait); |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4962 | struct mem_cgroup *memcg = event->memcg; |
Al Viro | 3ad6f93 | 2017-07-03 20:14:56 -0400 | [diff] [blame] | 4963 | __poll_t flags = key_to_poll(key); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4964 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 4965 | if (flags & EPOLLHUP) { |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4966 | /* |
| 4967 | * If the event has been detached at cgroup removal, we |
| 4968 | * can simply return knowing the other side will cleanup |
| 4969 | * for us. |
| 4970 | * |
| 4971 | * We can't race against event freeing since the other |
| 4972 | * side will require wqh->lock via remove_wait_queue(), |
| 4973 | * which we hold. |
| 4974 | */ |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4975 | spin_lock(&memcg->event_list_lock); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4976 | if (!list_empty(&event->list)) { |
| 4977 | list_del_init(&event->list); |
| 4978 | /* |
| 4979 | * We are in atomic context, but cgroup_event_remove() |
| 4980 | * may sleep, so we have to call it in workqueue. |
| 4981 | */ |
| 4982 | schedule_work(&event->remove); |
| 4983 | } |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 4984 | spin_unlock(&memcg->event_list_lock); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4985 | } |
| 4986 | |
| 4987 | return 0; |
| 4988 | } |
| 4989 | |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 4990 | static void memcg_event_ptable_queue_proc(struct file *file, |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4991 | wait_queue_head_t *wqh, poll_table *pt) |
| 4992 | { |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 4993 | struct mem_cgroup_event *event = |
| 4994 | container_of(pt, struct mem_cgroup_event, pt); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 4995 | |
| 4996 | event->wqh = wqh; |
| 4997 | add_wait_queue(wqh, &event->wait); |
| 4998 | } |
| 4999 | |
| 5000 | /* |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 5001 | * DO NOT USE IN NEW FILES. |
| 5002 | * |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5003 | * Parse input and register new cgroup event handler. |
| 5004 | * |
| 5005 | * Input must be in format '<event_fd> <control_fd> <args>'. |
| 5006 | * Interpretation of args is defined by control file implementation. |
| 5007 | */ |
Tejun Heo | 451af504 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5008 | static ssize_t memcg_write_event_control(struct kernfs_open_file *of, |
| 5009 | char *buf, size_t nbytes, loff_t off) |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5010 | { |
Tejun Heo | 451af504 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5011 | struct cgroup_subsys_state *css = of_css(of); |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 5012 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 5013 | struct mem_cgroup_event *event; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5014 | struct cgroup_subsys_state *cfile_css; |
| 5015 | unsigned int efd, cfd; |
| 5016 | struct fd efile; |
| 5017 | struct fd cfile; |
Tejun Heo | 4a7ba45 | 2022-12-07 16:53:15 -1000 | [diff] [blame] | 5018 | struct dentry *cdentry; |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 5019 | const char *name; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5020 | char *endp; |
| 5021 | int ret; |
| 5022 | |
Sebastian Andrzej Siewior | 2343e88 | 2022-03-22 14:40:38 -0700 | [diff] [blame] | 5023 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) |
| 5024 | return -EOPNOTSUPP; |
| 5025 | |
Tejun Heo | 451af504 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5026 | buf = strstrip(buf); |
| 5027 | |
| 5028 | efd = simple_strtoul(buf, &endp, 10); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5029 | if (*endp != ' ') |
| 5030 | return -EINVAL; |
Tejun Heo | 451af504 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5031 | buf = endp + 1; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5032 | |
Tejun Heo | 451af504 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5033 | cfd = simple_strtoul(buf, &endp, 10); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5034 | if ((*endp != ' ') && (*endp != '\0')) |
| 5035 | return -EINVAL; |
Tejun Heo | 451af504 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5036 | buf = endp + 1; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5037 | |
| 5038 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 5039 | if (!event) |
| 5040 | return -ENOMEM; |
| 5041 | |
Tejun Heo | 59b6f87 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 5042 | event->memcg = memcg; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5043 | INIT_LIST_HEAD(&event->list); |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 5044 | init_poll_funcptr(&event->pt, memcg_event_ptable_queue_proc); |
| 5045 | init_waitqueue_func_entry(&event->wait, memcg_event_wake); |
| 5046 | INIT_WORK(&event->remove, memcg_event_remove); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5047 | |
| 5048 | efile = fdget(efd); |
| 5049 | if (!efile.file) { |
| 5050 | ret = -EBADF; |
| 5051 | goto out_kfree; |
| 5052 | } |
| 5053 | |
| 5054 | event->eventfd = eventfd_ctx_fileget(efile.file); |
| 5055 | if (IS_ERR(event->eventfd)) { |
| 5056 | ret = PTR_ERR(event->eventfd); |
| 5057 | goto out_put_efile; |
| 5058 | } |
| 5059 | |
| 5060 | cfile = fdget(cfd); |
| 5061 | if (!cfile.file) { |
| 5062 | ret = -EBADF; |
| 5063 | goto out_put_eventfd; |
| 5064 | } |
| 5065 | |
| 5066 | /* the process need read permission on control file */ |
| 5067 | /* AV: shouldn't we check that it's been opened for read instead? */ |
Christian Brauner | 02f92b3 | 2021-01-21 14:19:22 +0100 | [diff] [blame] | 5068 | ret = file_permission(cfile.file, MAY_READ); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5069 | if (ret < 0) |
| 5070 | goto out_put_cfile; |
| 5071 | |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5072 | /* |
Tejun Heo | 4a7ba45 | 2022-12-07 16:53:15 -1000 | [diff] [blame] | 5073 | * The control file must be a regular cgroup1 file. As a regular cgroup |
| 5074 | * file can't be renamed, it's safe to access its name afterwards. |
| 5075 | */ |
| 5076 | cdentry = cfile.file->f_path.dentry; |
| 5077 | if (cdentry->d_sb->s_type != &cgroup_fs_type || !d_is_reg(cdentry)) { |
| 5078 | ret = -EINVAL; |
| 5079 | goto out_put_cfile; |
| 5080 | } |
| 5081 | |
| 5082 | /* |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 5083 | * Determine the event callbacks and set them in @event. This used |
| 5084 | * to be done via struct cftype but cgroup core no longer knows |
| 5085 | * about these events. The following is crude but the whole thing |
| 5086 | * is for compatibility anyway. |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 5087 | * |
| 5088 | * DO NOT ADD NEW FILES. |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 5089 | */ |
Tejun Heo | 4a7ba45 | 2022-12-07 16:53:15 -1000 | [diff] [blame] | 5090 | name = cdentry->d_name.name; |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 5091 | |
| 5092 | if (!strcmp(name, "memory.usage_in_bytes")) { |
| 5093 | event->register_event = mem_cgroup_usage_register_event; |
| 5094 | event->unregister_event = mem_cgroup_usage_unregister_event; |
| 5095 | } else if (!strcmp(name, "memory.oom_control")) { |
| 5096 | event->register_event = mem_cgroup_oom_register_event; |
| 5097 | event->unregister_event = mem_cgroup_oom_unregister_event; |
| 5098 | } else if (!strcmp(name, "memory.pressure_level")) { |
| 5099 | event->register_event = vmpressure_register_event; |
| 5100 | event->unregister_event = vmpressure_unregister_event; |
| 5101 | } else if (!strcmp(name, "memory.memsw.usage_in_bytes")) { |
Tejun Heo | 347c4a8 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 5102 | event->register_event = memsw_cgroup_usage_register_event; |
| 5103 | event->unregister_event = memsw_cgroup_usage_unregister_event; |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 5104 | } else { |
| 5105 | ret = -EINVAL; |
| 5106 | goto out_put_cfile; |
| 5107 | } |
| 5108 | |
| 5109 | /* |
Tejun Heo | b5557c4 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5110 | * Verify @cfile should belong to @css. Also, remaining events are |
| 5111 | * automatically removed on cgroup destruction but the removal is |
| 5112 | * asynchronous, so take an extra ref on @css. |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5113 | */ |
Tejun Heo | 4a7ba45 | 2022-12-07 16:53:15 -1000 | [diff] [blame] | 5114 | cfile_css = css_tryget_online_from_dir(cdentry->d_parent, |
Tejun Heo | ec903c0 | 2014-05-13 12:11:01 -0400 | [diff] [blame] | 5115 | &memory_cgrp_subsys); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5116 | ret = -EINVAL; |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5117 | if (IS_ERR(cfile_css)) |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5118 | goto out_put_cfile; |
Tejun Heo | 5a17f54 | 2014-02-11 11:52:47 -0500 | [diff] [blame] | 5119 | if (cfile_css != css) { |
| 5120 | css_put(cfile_css); |
| 5121 | goto out_put_cfile; |
| 5122 | } |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5123 | |
Tejun Heo | 451af504 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5124 | ret = event->register_event(memcg, event->eventfd, buf); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5125 | if (ret) |
| 5126 | goto out_put_css; |
| 5127 | |
Christoph Hellwig | 9965ed17 | 2018-03-05 07:26:05 -0800 | [diff] [blame] | 5128 | vfs_poll(efile.file, &event->pt); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5129 | |
Shakeel Butt | 4ba9515 | 2021-09-02 14:56:05 -0700 | [diff] [blame] | 5130 | spin_lock_irq(&memcg->event_list_lock); |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 5131 | list_add(&event->list, &memcg->event_list); |
Shakeel Butt | 4ba9515 | 2021-09-02 14:56:05 -0700 | [diff] [blame] | 5132 | spin_unlock_irq(&memcg->event_list_lock); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5133 | |
| 5134 | fdput(cfile); |
| 5135 | fdput(efile); |
| 5136 | |
Tejun Heo | 451af504 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5137 | return nbytes; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5138 | |
| 5139 | out_put_css: |
Tejun Heo | b5557c4 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5140 | css_put(css); |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5141 | out_put_cfile: |
| 5142 | fdput(cfile); |
| 5143 | out_put_eventfd: |
| 5144 | eventfd_ctx_put(event->eventfd); |
| 5145 | out_put_efile: |
| 5146 | fdput(efile); |
| 5147 | out_kfree: |
| 5148 | kfree(event); |
| 5149 | |
| 5150 | return ret; |
| 5151 | } |
| 5152 | |
Muchun Song | c29b5b3 | 2022-01-14 14:04:01 -0800 | [diff] [blame] | 5153 | #if defined(CONFIG_MEMCG_KMEM) && (defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)) |
| 5154 | static int mem_cgroup_slab_show(struct seq_file *m, void *p) |
| 5155 | { |
| 5156 | /* |
| 5157 | * Deprecated. |
Yang Yang | df4ae28 | 2022-06-10 02:44:52 +0000 | [diff] [blame] | 5158 | * Please, take a look at tools/cgroup/memcg_slabinfo.py . |
Muchun Song | c29b5b3 | 2022-01-14 14:04:01 -0800 | [diff] [blame] | 5159 | */ |
| 5160 | return 0; |
| 5161 | } |
| 5162 | #endif |
| 5163 | |
Yosry Ahmed | dddb44f | 2023-04-28 13:24:06 +0000 | [diff] [blame] | 5164 | static int memory_stat_show(struct seq_file *m, void *v); |
| 5165 | |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 5166 | static struct cftype mem_cgroup_legacy_files[] = { |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5167 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 5168 | .name = "usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5169 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 5170 | .read_u64 = mem_cgroup_read_u64, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5171 | }, |
| 5172 | { |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 5173 | .name = "max_usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5174 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5175 | .write = mem_cgroup_reset, |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 5176 | .read_u64 = mem_cgroup_read_u64, |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 5177 | }, |
| 5178 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 5179 | .name = "limit_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5180 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
Tejun Heo | 451af504 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5181 | .write = mem_cgroup_write, |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 5182 | .read_u64 = mem_cgroup_read_u64, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5183 | }, |
| 5184 | { |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 5185 | .name = "soft_limit_in_bytes", |
| 5186 | .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT), |
Tejun Heo | 451af504 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5187 | .write = mem_cgroup_write, |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 5188 | .read_u64 = mem_cgroup_read_u64, |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 5189 | }, |
| 5190 | { |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5191 | .name = "failcnt", |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5192 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5193 | .write = mem_cgroup_reset, |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 5194 | .read_u64 = mem_cgroup_read_u64, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5195 | }, |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 5196 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 5197 | .name = "stat", |
Yosry Ahmed | dddb44f | 2023-04-28 13:24:06 +0000 | [diff] [blame] | 5198 | .seq_show = memory_stat_show, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 5199 | }, |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 5200 | { |
| 5201 | .name = "force_empty", |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5202 | .write = mem_cgroup_force_empty_write, |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 5203 | }, |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5204 | { |
| 5205 | .name = "use_hierarchy", |
| 5206 | .write_u64 = mem_cgroup_hierarchy_write, |
| 5207 | .read_u64 = mem_cgroup_hierarchy_read, |
| 5208 | }, |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 5209 | { |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 5210 | .name = "cgroup.event_control", /* XXX: for compat */ |
Tejun Heo | 451af504 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5211 | .write = memcg_write_event_control, |
Tejun Heo | 7dbdb19 | 2015-09-18 17:54:23 -0400 | [diff] [blame] | 5212 | .flags = CFTYPE_NO_PREFIX | CFTYPE_WORLD_WRITABLE, |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5213 | }, |
| 5214 | { |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 5215 | .name = "swappiness", |
| 5216 | .read_u64 = mem_cgroup_swappiness_read, |
| 5217 | .write_u64 = mem_cgroup_swappiness_write, |
| 5218 | }, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5219 | { |
| 5220 | .name = "move_charge_at_immigrate", |
| 5221 | .read_u64 = mem_cgroup_move_charge_read, |
| 5222 | .write_u64 = mem_cgroup_move_charge_write, |
| 5223 | }, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5224 | { |
| 5225 | .name = "oom_control", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5226 | .seq_show = mem_cgroup_oom_control_read, |
KAMEZAWA Hiroyuki | 3c11ecf | 2010-05-26 14:42:37 -0700 | [diff] [blame] | 5227 | .write_u64 = mem_cgroup_oom_control_write, |
KAMEZAWA Hiroyuki | 9490ff2 | 2010-05-26 14:42:36 -0700 | [diff] [blame] | 5228 | }, |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 5229 | { |
| 5230 | .name = "pressure_level", |
Florian Schmidt | 6b0ba2a | 2023-04-04 10:58:59 +0000 | [diff] [blame] | 5231 | .seq_show = mem_cgroup_dummy_seq_show, |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 5232 | }, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 5233 | #ifdef CONFIG_NUMA |
| 5234 | { |
| 5235 | .name = "numa_stat", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 5236 | .seq_show = memcg_numa_stat_show, |
Ying Han | 406eb0c | 2011-05-26 16:25:37 -0700 | [diff] [blame] | 5237 | }, |
| 5238 | #endif |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5239 | { |
Michal Hocko | 4597648 | 2023-09-21 09:38:29 +0200 | [diff] [blame] | 5240 | .name = "kmem.limit_in_bytes", |
| 5241 | .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT), |
| 5242 | .write = mem_cgroup_write, |
| 5243 | .read_u64 = mem_cgroup_read_u64, |
| 5244 | }, |
| 5245 | { |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5246 | .name = "kmem.usage_in_bytes", |
| 5247 | .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE), |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 5248 | .read_u64 = mem_cgroup_read_u64, |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5249 | }, |
| 5250 | { |
| 5251 | .name = "kmem.failcnt", |
| 5252 | .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT), |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5253 | .write = mem_cgroup_reset, |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 5254 | .read_u64 = mem_cgroup_read_u64, |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5255 | }, |
| 5256 | { |
| 5257 | .name = "kmem.max_usage_in_bytes", |
| 5258 | .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE), |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 5259 | .write = mem_cgroup_reset, |
Tejun Heo | 791badb | 2013-12-05 12:28:02 -0500 | [diff] [blame] | 5260 | .read_u64 = mem_cgroup_read_u64, |
Glauber Costa | 510fc4e | 2012-12-18 14:21:47 -0800 | [diff] [blame] | 5261 | }, |
Yafang Shao | a87425a | 2020-04-01 21:06:30 -0700 | [diff] [blame] | 5262 | #if defined(CONFIG_MEMCG_KMEM) && \ |
| 5263 | (defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)) |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 5264 | { |
| 5265 | .name = "kmem.slabinfo", |
Muchun Song | c29b5b3 | 2022-01-14 14:04:01 -0800 | [diff] [blame] | 5266 | .seq_show = mem_cgroup_slab_show, |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 5267 | }, |
| 5268 | #endif |
Vladimir Davydov | d55f90b | 2016-01-20 15:02:44 -0800 | [diff] [blame] | 5269 | { |
| 5270 | .name = "kmem.tcp.limit_in_bytes", |
| 5271 | .private = MEMFILE_PRIVATE(_TCP, RES_LIMIT), |
| 5272 | .write = mem_cgroup_write, |
| 5273 | .read_u64 = mem_cgroup_read_u64, |
| 5274 | }, |
| 5275 | { |
| 5276 | .name = "kmem.tcp.usage_in_bytes", |
| 5277 | .private = MEMFILE_PRIVATE(_TCP, RES_USAGE), |
| 5278 | .read_u64 = mem_cgroup_read_u64, |
| 5279 | }, |
| 5280 | { |
| 5281 | .name = "kmem.tcp.failcnt", |
| 5282 | .private = MEMFILE_PRIVATE(_TCP, RES_FAILCNT), |
| 5283 | .write = mem_cgroup_reset, |
| 5284 | .read_u64 = mem_cgroup_read_u64, |
| 5285 | }, |
| 5286 | { |
| 5287 | .name = "kmem.tcp.max_usage_in_bytes", |
| 5288 | .private = MEMFILE_PRIVATE(_TCP, RES_MAX_USAGE), |
| 5289 | .write = mem_cgroup_reset, |
| 5290 | .read_u64 = mem_cgroup_read_u64, |
| 5291 | }, |
Tejun Heo | 6bc1034 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5292 | { }, /* terminate */ |
Tejun Heo | af36f90 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 5293 | }; |
KAMEZAWA Hiroyuki | 8c7c6e34 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 5294 | |
Johannes Weiner | 73f576c | 2016-07-20 15:44:57 -0700 | [diff] [blame] | 5295 | /* |
| 5296 | * Private memory cgroup IDR |
| 5297 | * |
| 5298 | * Swap-out records and page cache shadow entries need to store memcg |
| 5299 | * references in constrained space, so we maintain an ID space that is |
| 5300 | * limited to 16 bit (MEM_CGROUP_ID_MAX), limiting the total number of |
| 5301 | * memory-controlled cgroups to 64k. |
| 5302 | * |
Ethon Paul | b8f2935 | 2020-06-04 16:49:28 -0700 | [diff] [blame] | 5303 | * However, there usually are many references to the offline CSS after |
Johannes Weiner | 73f576c | 2016-07-20 15:44:57 -0700 | [diff] [blame] | 5304 | * the cgroup has been destroyed, such as page cache or reclaimable |
| 5305 | * slab objects, that don't need to hang on to the ID. We want to keep |
| 5306 | * those dead CSS from occupying IDs, or we might quickly exhaust the |
| 5307 | * relatively small ID space and prevent the creation of new cgroups |
| 5308 | * even when there are much fewer than 64k cgroups - possibly none. |
| 5309 | * |
| 5310 | * Maintain a private 16-bit ID space for memcg, and allow the ID to |
| 5311 | * be freed and recycled when it's no longer needed, which is usually |
| 5312 | * when the CSS is offlined. |
| 5313 | * |
| 5314 | * The only exception to that are records of swapped out tmpfs/shmem |
| 5315 | * pages that need to be attributed to live ancestors on swapin. But |
| 5316 | * those references are manageable from userspace. |
| 5317 | */ |
| 5318 | |
Miaohe Lin | 60b1e24 | 2023-07-08 10:33:04 +0800 | [diff] [blame] | 5319 | #define MEM_CGROUP_ID_MAX ((1UL << MEM_CGROUP_ID_SHIFT) - 1) |
Johannes Weiner | 73f576c | 2016-07-20 15:44:57 -0700 | [diff] [blame] | 5320 | static DEFINE_IDR(mem_cgroup_idr); |
| 5321 | |
Kirill Tkhai | 7e97de0 | 2018-08-02 15:36:01 -0700 | [diff] [blame] | 5322 | static void mem_cgroup_id_remove(struct mem_cgroup *memcg) |
| 5323 | { |
| 5324 | if (memcg->id.id > 0) { |
| 5325 | idr_remove(&mem_cgroup_idr, memcg->id.id); |
| 5326 | memcg->id.id = 0; |
| 5327 | } |
| 5328 | } |
| 5329 | |
Vincenzo Frascino | c1514c0 | 2020-04-01 21:07:13 -0700 | [diff] [blame] | 5330 | static void __maybe_unused mem_cgroup_id_get_many(struct mem_cgroup *memcg, |
| 5331 | unsigned int n) |
Johannes Weiner | 73f576c | 2016-07-20 15:44:57 -0700 | [diff] [blame] | 5332 | { |
Kirill Tkhai | 1c2d479 | 2018-10-26 15:09:28 -0700 | [diff] [blame] | 5333 | refcount_add(n, &memcg->id.ref); |
Johannes Weiner | 73f576c | 2016-07-20 15:44:57 -0700 | [diff] [blame] | 5334 | } |
| 5335 | |
Vladimir Davydov | 615d66c | 2016-08-11 15:33:03 -0700 | [diff] [blame] | 5336 | static void mem_cgroup_id_put_many(struct mem_cgroup *memcg, unsigned int n) |
Johannes Weiner | 73f576c | 2016-07-20 15:44:57 -0700 | [diff] [blame] | 5337 | { |
Kirill Tkhai | 1c2d479 | 2018-10-26 15:09:28 -0700 | [diff] [blame] | 5338 | if (refcount_sub_and_test(n, &memcg->id.ref)) { |
Kirill Tkhai | 7e97de0 | 2018-08-02 15:36:01 -0700 | [diff] [blame] | 5339 | mem_cgroup_id_remove(memcg); |
Johannes Weiner | 73f576c | 2016-07-20 15:44:57 -0700 | [diff] [blame] | 5340 | |
| 5341 | /* Memcg ID pins CSS */ |
| 5342 | css_put(&memcg->css); |
| 5343 | } |
| 5344 | } |
| 5345 | |
Vladimir Davydov | 615d66c | 2016-08-11 15:33:03 -0700 | [diff] [blame] | 5346 | static inline void mem_cgroup_id_put(struct mem_cgroup *memcg) |
| 5347 | { |
| 5348 | mem_cgroup_id_put_many(memcg, 1); |
| 5349 | } |
| 5350 | |
Johannes Weiner | 73f576c | 2016-07-20 15:44:57 -0700 | [diff] [blame] | 5351 | /** |
| 5352 | * mem_cgroup_from_id - look up a memcg from a memcg id |
| 5353 | * @id: the memcg id to look up |
| 5354 | * |
| 5355 | * Caller must hold rcu_read_lock(). |
| 5356 | */ |
| 5357 | struct mem_cgroup *mem_cgroup_from_id(unsigned short id) |
| 5358 | { |
| 5359 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 5360 | return idr_find(&mem_cgroup_idr, id); |
| 5361 | } |
| 5362 | |
Roman Gushchin | c15187a | 2022-05-31 20:22:22 -0700 | [diff] [blame] | 5363 | #ifdef CONFIG_SHRINKER_DEBUG |
| 5364 | struct mem_cgroup *mem_cgroup_get_from_ino(unsigned long ino) |
| 5365 | { |
| 5366 | struct cgroup *cgrp; |
| 5367 | struct cgroup_subsys_state *css; |
| 5368 | struct mem_cgroup *memcg; |
| 5369 | |
| 5370 | cgrp = cgroup_get_from_id(ino); |
Michal Koutný | fa7e439 | 2022-08-26 18:52:37 +0200 | [diff] [blame] | 5371 | if (IS_ERR(cgrp)) |
Tejun Heo | c0f2df4 | 2022-08-28 17:54:15 -1000 | [diff] [blame] | 5372 | return ERR_CAST(cgrp); |
Roman Gushchin | c15187a | 2022-05-31 20:22:22 -0700 | [diff] [blame] | 5373 | |
| 5374 | css = cgroup_get_e_css(cgrp, &memory_cgrp_subsys); |
| 5375 | if (css) |
| 5376 | memcg = container_of(css, struct mem_cgroup, css); |
| 5377 | else |
| 5378 | memcg = ERR_PTR(-ENOENT); |
| 5379 | |
| 5380 | cgroup_put(cgrp); |
| 5381 | |
| 5382 | return memcg; |
| 5383 | } |
| 5384 | #endif |
| 5385 | |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 5386 | static int alloc_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5387 | { |
| 5388 | struct mem_cgroup_per_node *pn; |
Wei Yang | 8c9bb39 | 2022-03-22 14:47:06 -0700 | [diff] [blame] | 5389 | |
| 5390 | pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, node); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5391 | if (!pn) |
| 5392 | return 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 5393 | |
Shakeel Butt | 7e1c0d6 | 2021-09-02 14:55:00 -0700 | [diff] [blame] | 5394 | pn->lruvec_stats_percpu = alloc_percpu_gfp(struct lruvec_stats_percpu, |
| 5395 | GFP_KERNEL_ACCOUNT); |
| 5396 | if (!pn->lruvec_stats_percpu) { |
Johannes Weiner | 00f3ca2 | 2017-07-06 15:40:52 -0700 | [diff] [blame] | 5397 | kfree(pn); |
| 5398 | return 1; |
| 5399 | } |
| 5400 | |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 5401 | lruvec_init(&pn->lruvec); |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 5402 | pn->memcg = memcg; |
| 5403 | |
Johannes Weiner | 54f72fe | 2013-07-08 15:59:49 -0700 | [diff] [blame] | 5404 | memcg->nodeinfo[node] = pn; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 5405 | return 0; |
| 5406 | } |
| 5407 | |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 5408 | static void free_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node) |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 5409 | { |
Johannes Weiner | 00f3ca2 | 2017-07-06 15:40:52 -0700 | [diff] [blame] | 5410 | struct mem_cgroup_per_node *pn = memcg->nodeinfo[node]; |
| 5411 | |
Michal Hocko | 4eaf431 | 2018-04-10 16:29:52 -0700 | [diff] [blame] | 5412 | if (!pn) |
| 5413 | return; |
| 5414 | |
Shakeel Butt | 7e1c0d6 | 2021-09-02 14:55:00 -0700 | [diff] [blame] | 5415 | free_percpu(pn->lruvec_stats_percpu); |
Johannes Weiner | 00f3ca2 | 2017-07-06 15:40:52 -0700 | [diff] [blame] | 5416 | kfree(pn); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 5417 | } |
| 5418 | |
Tahsin Erdogan | 40e952f | 2017-03-09 16:17:26 -0800 | [diff] [blame] | 5419 | static void __mem_cgroup_free(struct mem_cgroup *memcg) |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 5420 | { |
| 5421 | int node; |
| 5422 | |
Roman Gushchin | 675d6c9 | 2023-10-19 15:53:43 -0700 | [diff] [blame] | 5423 | if (memcg->orig_objcg) |
| 5424 | obj_cgroup_put(memcg->orig_objcg); |
| 5425 | |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 5426 | for_each_node(node) |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 5427 | free_mem_cgroup_per_node_info(memcg, node); |
Shakeel Butt | 410f8e8 | 2022-09-07 04:35:35 +0000 | [diff] [blame] | 5428 | kfree(memcg->vmstats); |
Chris Down | 871789d | 2019-05-14 15:46:57 -0700 | [diff] [blame] | 5429 | free_percpu(memcg->vmstats_percpu); |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 5430 | kfree(memcg); |
| 5431 | } |
| 5432 | |
Tahsin Erdogan | 40e952f | 2017-03-09 16:17:26 -0800 | [diff] [blame] | 5433 | static void mem_cgroup_free(struct mem_cgroup *memcg) |
| 5434 | { |
Yu Zhao | ec1c86b | 2022-09-18 02:00:02 -0600 | [diff] [blame] | 5435 | lru_gen_exit_memcg(memcg); |
Tahsin Erdogan | 40e952f | 2017-03-09 16:17:26 -0800 | [diff] [blame] | 5436 | memcg_wb_domain_exit(memcg); |
| 5437 | __mem_cgroup_free(memcg); |
| 5438 | } |
| 5439 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 5440 | static struct mem_cgroup *mem_cgroup_alloc(void) |
| 5441 | { |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5442 | struct mem_cgroup *memcg; |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 5443 | int node; |
Tejun Heo | 97b2782 | 2019-08-26 09:06:56 -0700 | [diff] [blame] | 5444 | int __maybe_unused i; |
Yafang Shao | 11d6761 | 2020-05-07 18:35:43 -0700 | [diff] [blame] | 5445 | long error = -ENOMEM; |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 5446 | |
Wang Weiyang | 06b2c3b | 2022-01-14 14:05:42 -0800 | [diff] [blame] | 5447 | memcg = kzalloc(struct_size(memcg, nodeinfo, nr_node_ids), GFP_KERNEL); |
Hugh Dickins | d79154b | 2012-03-21 16:34:18 -0700 | [diff] [blame] | 5448 | if (!memcg) |
Yafang Shao | 11d6761 | 2020-05-07 18:35:43 -0700 | [diff] [blame] | 5449 | return ERR_PTR(error); |
Dan Carpenter | e7bbcdf | 2010-03-23 13:35:12 -0700 | [diff] [blame] | 5450 | |
Johannes Weiner | 73f576c | 2016-07-20 15:44:57 -0700 | [diff] [blame] | 5451 | memcg->id.id = idr_alloc(&mem_cgroup_idr, NULL, |
Muchun Song | be74050 | 2022-03-22 14:41:31 -0700 | [diff] [blame] | 5452 | 1, MEM_CGROUP_ID_MAX + 1, GFP_KERNEL); |
Yafang Shao | 11d6761 | 2020-05-07 18:35:43 -0700 | [diff] [blame] | 5453 | if (memcg->id.id < 0) { |
| 5454 | error = memcg->id.id; |
Johannes Weiner | 73f576c | 2016-07-20 15:44:57 -0700 | [diff] [blame] | 5455 | goto fail; |
Yafang Shao | 11d6761 | 2020-05-07 18:35:43 -0700 | [diff] [blame] | 5456 | } |
Johannes Weiner | 73f576c | 2016-07-20 15:44:57 -0700 | [diff] [blame] | 5457 | |
Shakeel Butt | 410f8e8 | 2022-09-07 04:35:35 +0000 | [diff] [blame] | 5458 | memcg->vmstats = kzalloc(sizeof(struct memcg_vmstats), GFP_KERNEL); |
| 5459 | if (!memcg->vmstats) |
| 5460 | goto fail; |
| 5461 | |
Roman Gushchin | 3e38e0a | 2020-08-11 18:30:25 -0700 | [diff] [blame] | 5462 | memcg->vmstats_percpu = alloc_percpu_gfp(struct memcg_vmstats_percpu, |
| 5463 | GFP_KERNEL_ACCOUNT); |
Chris Down | 871789d | 2019-05-14 15:46:57 -0700 | [diff] [blame] | 5464 | if (!memcg->vmstats_percpu) |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 5465 | goto fail; |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 5466 | |
Bob Liu | 3ed28fa | 2012-01-12 17:19:04 -0800 | [diff] [blame] | 5467 | for_each_node(node) |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 5468 | if (alloc_mem_cgroup_per_node_info(memcg, node)) |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 5469 | goto fail; |
Balbir Singh | f64c3f5 | 2009-09-23 15:56:37 -0700 | [diff] [blame] | 5470 | |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 5471 | if (memcg_wb_domain_init(memcg, GFP_KERNEL)) |
| 5472 | goto fail; |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 5473 | |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 5474 | INIT_WORK(&memcg->high_work, high_work_func); |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 5475 | INIT_LIST_HEAD(&memcg->oom_notify); |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 5476 | mutex_init(&memcg->thresholds_lock); |
| 5477 | spin_lock_init(&memcg->move_lock); |
Anton Vorontsov | 70ddf63 | 2013-04-29 15:08:31 -0700 | [diff] [blame] | 5478 | vmpressure_init(&memcg->vmpressure); |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 5479 | INIT_LIST_HEAD(&memcg->event_list); |
| 5480 | spin_lock_init(&memcg->event_list_lock); |
Johannes Weiner | d886f4e | 2016-01-20 15:02:47 -0800 | [diff] [blame] | 5481 | memcg->socket_pressure = jiffies; |
Kirill Tkhai | 84c07d1 | 2018-08-17 15:47:25 -0700 | [diff] [blame] | 5482 | #ifdef CONFIG_MEMCG_KMEM |
Vladimir Davydov | 900a38f | 2014-12-12 16:55:10 -0800 | [diff] [blame] | 5483 | memcg->kmemcg_id = -1; |
Roman Gushchin | bf4f059 | 2020-08-06 23:20:49 -0700 | [diff] [blame] | 5484 | INIT_LIST_HEAD(&memcg->objcg_list); |
Vladimir Davydov | 900a38f | 2014-12-12 16:55:10 -0800 | [diff] [blame] | 5485 | #endif |
Tejun Heo | 52ebea7 | 2015-05-22 17:13:37 -0400 | [diff] [blame] | 5486 | #ifdef CONFIG_CGROUP_WRITEBACK |
| 5487 | INIT_LIST_HEAD(&memcg->cgwb_list); |
Tejun Heo | 97b2782 | 2019-08-26 09:06:56 -0700 | [diff] [blame] | 5488 | for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) |
| 5489 | memcg->cgwb_frn[i].done = |
| 5490 | __WB_COMPLETION_INIT(&memcg_cgwb_frn_waitq); |
Tejun Heo | 52ebea7 | 2015-05-22 17:13:37 -0400 | [diff] [blame] | 5491 | #endif |
Yang Shi | 87eaceb | 2019-09-23 15:38:15 -0700 | [diff] [blame] | 5492 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 5493 | spin_lock_init(&memcg->deferred_split_queue.split_queue_lock); |
| 5494 | INIT_LIST_HEAD(&memcg->deferred_split_queue.split_queue); |
| 5495 | memcg->deferred_split_queue.split_queue_len = 0; |
| 5496 | #endif |
Yu Zhao | ec1c86b | 2022-09-18 02:00:02 -0600 | [diff] [blame] | 5497 | lru_gen_init_memcg(memcg); |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 5498 | return memcg; |
| 5499 | fail: |
Kirill Tkhai | 7e97de0 | 2018-08-02 15:36:01 -0700 | [diff] [blame] | 5500 | mem_cgroup_id_remove(memcg); |
Tahsin Erdogan | 40e952f | 2017-03-09 16:17:26 -0800 | [diff] [blame] | 5501 | __mem_cgroup_free(memcg); |
Yafang Shao | 11d6761 | 2020-05-07 18:35:43 -0700 | [diff] [blame] | 5502 | return ERR_PTR(error); |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 5503 | } |
| 5504 | |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 5505 | static struct cgroup_subsys_state * __ref |
| 5506 | mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 5507 | { |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 5508 | struct mem_cgroup *parent = mem_cgroup_from_css(parent_css); |
Roman Gushchin | b87d8ce | 2020-10-17 16:13:40 -0700 | [diff] [blame] | 5509 | struct mem_cgroup *memcg, *old_memcg; |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 5510 | |
Roman Gushchin | b87d8ce | 2020-10-17 16:13:40 -0700 | [diff] [blame] | 5511 | old_memcg = set_active_memcg(parent); |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 5512 | memcg = mem_cgroup_alloc(); |
Roman Gushchin | b87d8ce | 2020-10-17 16:13:40 -0700 | [diff] [blame] | 5513 | set_active_memcg(old_memcg); |
Yafang Shao | 11d6761 | 2020-05-07 18:35:43 -0700 | [diff] [blame] | 5514 | if (IS_ERR(memcg)) |
| 5515 | return ERR_CAST(memcg); |
Li Zefan | 4219b2d | 2013-09-23 16:56:29 +0800 | [diff] [blame] | 5516 | |
Jakub Kicinski | d1663a9 | 2020-06-01 21:49:49 -0700 | [diff] [blame] | 5517 | page_counter_set_high(&memcg->memory, PAGE_COUNTER_MAX); |
Yue Zhao | 2178e20 | 2023-03-06 23:41:38 +0800 | [diff] [blame] | 5518 | WRITE_ONCE(memcg->soft_limit, PAGE_COUNTER_MAX); |
Johannes Weiner | f4840cc | 2022-05-19 14:08:53 -0700 | [diff] [blame] | 5519 | #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) |
| 5520 | memcg->zswap_max = PAGE_COUNTER_MAX; |
| 5521 | #endif |
Jakub Kicinski | 4b82ab4 | 2020-06-01 21:49:52 -0700 | [diff] [blame] | 5522 | page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX); |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 5523 | if (parent) { |
Yue Zhao | 82b3aa2 | 2023-03-06 23:41:36 +0800 | [diff] [blame] | 5524 | WRITE_ONCE(memcg->swappiness, mem_cgroup_swappiness(parent)); |
Yue Zhao | 17c56de | 2023-03-06 23:41:37 +0800 | [diff] [blame] | 5525 | WRITE_ONCE(memcg->oom_kill_disable, READ_ONCE(parent->oom_kill_disable)); |
Roman Gushchin | bef8620 | 2020-12-14 19:06:49 -0800 | [diff] [blame] | 5526 | |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 5527 | page_counter_init(&memcg->memory, &parent->memory); |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 5528 | page_counter_init(&memcg->swap, &parent->swap); |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 5529 | page_counter_init(&memcg->kmem, &parent->kmem); |
Johannes Weiner | 0db1529 | 2016-01-20 15:02:50 -0800 | [diff] [blame] | 5530 | page_counter_init(&memcg->tcpmem, &parent->tcpmem); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 5531 | } else { |
Shakeel Butt | 8278f1c | 2022-09-07 04:35:37 +0000 | [diff] [blame] | 5532 | init_memcg_events(); |
Roman Gushchin | bef8620 | 2020-12-14 19:06:49 -0800 | [diff] [blame] | 5533 | page_counter_init(&memcg->memory, NULL); |
| 5534 | page_counter_init(&memcg->swap, NULL); |
| 5535 | page_counter_init(&memcg->kmem, NULL); |
| 5536 | page_counter_init(&memcg->tcpmem, NULL); |
Vladimir Davydov | d644163 | 2014-01-23 15:53:09 -0800 | [diff] [blame] | 5537 | |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 5538 | root_mem_cgroup = memcg; |
| 5539 | return &memcg->css; |
| 5540 | } |
| 5541 | |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 5542 | if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket) |
Johannes Weiner | ef12947 | 2016-01-14 15:21:34 -0800 | [diff] [blame] | 5543 | static_branch_inc(&memcg_sockets_enabled_key); |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 5544 | |
Yafang Shao | b6c1a8a | 2023-02-10 15:47:31 +0000 | [diff] [blame] | 5545 | #if defined(CONFIG_MEMCG_KMEM) |
| 5546 | if (!cgroup_memory_nobpf) |
| 5547 | static_branch_inc(&memcg_bpf_enabled_key); |
| 5548 | #endif |
| 5549 | |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 5550 | return &memcg->css; |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 5551 | } |
| 5552 | |
Johannes Weiner | 73f576c | 2016-07-20 15:44:57 -0700 | [diff] [blame] | 5553 | static int mem_cgroup_css_online(struct cgroup_subsys_state *css) |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 5554 | { |
Vladimir Davydov | 58fa2a5 | 2016-10-07 16:57:29 -0700 | [diff] [blame] | 5555 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
| 5556 | |
Muchun Song | da0efe3 | 2022-03-22 14:41:15 -0700 | [diff] [blame] | 5557 | if (memcg_online_kmem(memcg)) |
| 5558 | goto remove_id; |
| 5559 | |
Kirill Tkhai | 0a4465d | 2018-08-17 15:47:37 -0700 | [diff] [blame] | 5560 | /* |
Yang Shi | e4262c4 | 2021-05-04 18:36:23 -0700 | [diff] [blame] | 5561 | * A memcg must be visible for expand_shrinker_info() |
Kirill Tkhai | 0a4465d | 2018-08-17 15:47:37 -0700 | [diff] [blame] | 5562 | * by the time the maps are allocated. So, we allocate maps |
| 5563 | * here, when for_each_mem_cgroup() can't skip it. |
| 5564 | */ |
Muchun Song | da0efe3 | 2022-03-22 14:41:15 -0700 | [diff] [blame] | 5565 | if (alloc_shrinker_info(memcg)) |
| 5566 | goto offline_kmem; |
Kirill Tkhai | 0a4465d | 2018-08-17 15:47:37 -0700 | [diff] [blame] | 5567 | |
Shakeel Butt | aa48e47 | 2021-09-02 14:55:04 -0700 | [diff] [blame] | 5568 | if (unlikely(mem_cgroup_is_root(memcg))) |
| 5569 | queue_delayed_work(system_unbound_wq, &stats_flush_dwork, |
Miaohe Lin | 396faf8 | 2023-06-03 15:21:16 +0800 | [diff] [blame] | 5570 | FLUSH_TIME); |
Yu Zhao | e4dde56 | 2022-12-21 21:19:04 -0700 | [diff] [blame] | 5571 | lru_gen_online_memcg(memcg); |
Johannes Weiner | 6f0df8e | 2023-08-23 15:54:30 -0700 | [diff] [blame] | 5572 | |
| 5573 | /* Online state pins memcg ID, memcg ID pins CSS */ |
| 5574 | refcount_set(&memcg->id.ref, 1); |
| 5575 | css_get(css); |
| 5576 | |
| 5577 | /* |
| 5578 | * Ensure mem_cgroup_from_id() works once we're fully online. |
| 5579 | * |
| 5580 | * We could do this earlier and require callers to filter with |
| 5581 | * css_tryget_online(). But right now there are no users that |
| 5582 | * need earlier access, and the workingset code relies on the |
| 5583 | * cgroup tree linkage (mem_cgroup_get_nr_swap_pages()). So |
| 5584 | * publish it here at the end of onlining. This matches the |
| 5585 | * regular ID destruction during offlining. |
| 5586 | */ |
| 5587 | idr_replace(&mem_cgroup_idr, memcg, memcg->id.id); |
| 5588 | |
Johannes Weiner | 2f7dd7a | 2014-10-02 16:16:57 -0700 | [diff] [blame] | 5589 | return 0; |
Muchun Song | da0efe3 | 2022-03-22 14:41:15 -0700 | [diff] [blame] | 5590 | offline_kmem: |
| 5591 | memcg_offline_kmem(memcg); |
| 5592 | remove_id: |
| 5593 | mem_cgroup_id_remove(memcg); |
| 5594 | return -ENOMEM; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5595 | } |
| 5596 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5597 | static void mem_cgroup_css_offline(struct cgroup_subsys_state *css) |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 5598 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5599 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Tejun Heo | 3bc942f | 2013-11-22 18:20:44 -0500 | [diff] [blame] | 5600 | struct mem_cgroup_event *event, *tmp; |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5601 | |
| 5602 | /* |
| 5603 | * Unregister events and notify userspace. |
| 5604 | * Notify userspace about cgroup removing only after rmdir of cgroup |
| 5605 | * directory to avoid race between userspace and kernelspace. |
| 5606 | */ |
Shakeel Butt | 4ba9515 | 2021-09-02 14:56:05 -0700 | [diff] [blame] | 5607 | spin_lock_irq(&memcg->event_list_lock); |
Tejun Heo | fba9480 | 2013-11-22 18:20:43 -0500 | [diff] [blame] | 5608 | list_for_each_entry_safe(event, tmp, &memcg->event_list, list) { |
Tejun Heo | 79bd981 | 2013-11-22 18:20:42 -0500 | [diff] [blame] | 5609 | list_del_init(&event->list); |
| 5610 | schedule_work(&event->remove); |
| 5611 | } |
Shakeel Butt | 4ba9515 | 2021-09-02 14:56:05 -0700 | [diff] [blame] | 5612 | spin_unlock_irq(&memcg->event_list_lock); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 5613 | |
Roman Gushchin | bf8d5d5 | 2018-06-07 17:07:46 -0700 | [diff] [blame] | 5614 | page_counter_set_min(&memcg->memory, 0); |
Roman Gushchin | 2306715 | 2018-06-07 17:06:22 -0700 | [diff] [blame] | 5615 | page_counter_set_low(&memcg->memory, 0); |
Roman Gushchin | 63677c74 | 2017-09-06 16:21:47 -0700 | [diff] [blame] | 5616 | |
Johannes Weiner | 567e9ab | 2016-01-20 15:02:24 -0800 | [diff] [blame] | 5617 | memcg_offline_kmem(memcg); |
Yang Shi | a178015 | 2021-05-04 18:36:42 -0700 | [diff] [blame] | 5618 | reparent_shrinker_deferred(memcg); |
Tejun Heo | 52ebea7 | 2015-05-22 17:13:37 -0400 | [diff] [blame] | 5619 | wb_memcg_offline(memcg); |
Yu Zhao | e4dde56 | 2022-12-21 21:19:04 -0700 | [diff] [blame] | 5620 | lru_gen_offline_memcg(memcg); |
Johannes Weiner | 73f576c | 2016-07-20 15:44:57 -0700 | [diff] [blame] | 5621 | |
Roman Gushchin | 591edfb | 2018-10-26 15:03:23 -0700 | [diff] [blame] | 5622 | drain_all_stock(memcg); |
| 5623 | |
Johannes Weiner | 73f576c | 2016-07-20 15:44:57 -0700 | [diff] [blame] | 5624 | mem_cgroup_id_put(memcg); |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 5625 | } |
| 5626 | |
Vladimir Davydov | 6df3868 | 2015-12-29 14:54:10 -0800 | [diff] [blame] | 5627 | static void mem_cgroup_css_released(struct cgroup_subsys_state *css) |
| 5628 | { |
| 5629 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
| 5630 | |
| 5631 | invalidate_reclaim_iterators(memcg); |
Yu Zhao | e4dde56 | 2022-12-21 21:19:04 -0700 | [diff] [blame] | 5632 | lru_gen_release_memcg(memcg); |
Vladimir Davydov | 6df3868 | 2015-12-29 14:54:10 -0800 | [diff] [blame] | 5633 | } |
| 5634 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5635 | static void mem_cgroup_css_free(struct cgroup_subsys_state *css) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5636 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 5637 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
Tejun Heo | 97b2782 | 2019-08-26 09:06:56 -0700 | [diff] [blame] | 5638 | int __maybe_unused i; |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 5639 | |
Tejun Heo | 97b2782 | 2019-08-26 09:06:56 -0700 | [diff] [blame] | 5640 | #ifdef CONFIG_CGROUP_WRITEBACK |
| 5641 | for (i = 0; i < MEMCG_CGWB_FRN_CNT; i++) |
| 5642 | wb_wait_for_completion(&memcg->cgwb_frn[i].done); |
| 5643 | #endif |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 5644 | if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket) |
Johannes Weiner | ef12947 | 2016-01-14 15:21:34 -0800 | [diff] [blame] | 5645 | static_branch_dec(&memcg_sockets_enabled_key); |
Johannes Weiner | 3893e30 | 2016-01-20 15:02:29 -0800 | [diff] [blame] | 5646 | |
Johannes Weiner | 0db1529 | 2016-01-20 15:02:50 -0800 | [diff] [blame] | 5647 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_active) |
Vladimir Davydov | d55f90b | 2016-01-20 15:02:44 -0800 | [diff] [blame] | 5648 | static_branch_dec(&memcg_sockets_enabled_key); |
Johannes Weiner | 3893e30 | 2016-01-20 15:02:29 -0800 | [diff] [blame] | 5649 | |
Yafang Shao | b6c1a8a | 2023-02-10 15:47:31 +0000 | [diff] [blame] | 5650 | #if defined(CONFIG_MEMCG_KMEM) |
| 5651 | if (!cgroup_memory_nobpf) |
| 5652 | static_branch_dec(&memcg_bpf_enabled_key); |
| 5653 | #endif |
| 5654 | |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 5655 | vmpressure_cleanup(&memcg->vmpressure); |
| 5656 | cancel_work_sync(&memcg->high_work); |
| 5657 | mem_cgroup_remove_from_trees(memcg); |
Yang Shi | e4262c4 | 2021-05-04 18:36:23 -0700 | [diff] [blame] | 5658 | free_shrinker_info(memcg); |
Johannes Weiner | 0b8f73e | 2016-01-20 15:02:53 -0800 | [diff] [blame] | 5659 | mem_cgroup_free(memcg); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 5660 | } |
| 5661 | |
Tejun Heo | 1ced953b | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 5662 | /** |
| 5663 | * mem_cgroup_css_reset - reset the states of a mem_cgroup |
| 5664 | * @css: the target css |
| 5665 | * |
| 5666 | * Reset the states of the mem_cgroup associated with @css. This is |
| 5667 | * invoked when the userland requests disabling on the default hierarchy |
| 5668 | * but the memcg is pinned through dependency. The memcg should stop |
| 5669 | * applying policies and should revert to the vanilla state as it may be |
| 5670 | * made visible again. |
| 5671 | * |
| 5672 | * The current implementation only resets the essential configurations. |
| 5673 | * This needs to be expanded to cover all the visible parts. |
| 5674 | */ |
| 5675 | static void mem_cgroup_css_reset(struct cgroup_subsys_state *css) |
| 5676 | { |
| 5677 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
| 5678 | |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 5679 | page_counter_set_max(&memcg->memory, PAGE_COUNTER_MAX); |
| 5680 | page_counter_set_max(&memcg->swap, PAGE_COUNTER_MAX); |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 5681 | page_counter_set_max(&memcg->kmem, PAGE_COUNTER_MAX); |
| 5682 | page_counter_set_max(&memcg->tcpmem, PAGE_COUNTER_MAX); |
Roman Gushchin | bf8d5d5 | 2018-06-07 17:07:46 -0700 | [diff] [blame] | 5683 | page_counter_set_min(&memcg->memory, 0); |
Roman Gushchin | 2306715 | 2018-06-07 17:06:22 -0700 | [diff] [blame] | 5684 | page_counter_set_low(&memcg->memory, 0); |
Jakub Kicinski | d1663a9 | 2020-06-01 21:49:49 -0700 | [diff] [blame] | 5685 | page_counter_set_high(&memcg->memory, PAGE_COUNTER_MAX); |
Yue Zhao | 2178e20 | 2023-03-06 23:41:38 +0800 | [diff] [blame] | 5686 | WRITE_ONCE(memcg->soft_limit, PAGE_COUNTER_MAX); |
Jakub Kicinski | 4b82ab4 | 2020-06-01 21:49:52 -0700 | [diff] [blame] | 5687 | page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX); |
Tejun Heo | 2529bb3 | 2015-05-22 18:23:34 -0400 | [diff] [blame] | 5688 | memcg_wb_domain_size_changed(memcg); |
Tejun Heo | 1ced953b | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 5689 | } |
| 5690 | |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 5691 | static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) |
| 5692 | { |
| 5693 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
| 5694 | struct mem_cgroup *parent = parent_mem_cgroup(memcg); |
| 5695 | struct memcg_vmstats_percpu *statc; |
Yosry Ahmed | f82e6bf | 2023-07-26 15:32:23 +0000 | [diff] [blame] | 5696 | long delta, delta_cpu, v; |
Shakeel Butt | 7e1c0d6 | 2021-09-02 14:55:00 -0700 | [diff] [blame] | 5697 | int i, nid; |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 5698 | |
| 5699 | statc = per_cpu_ptr(memcg->vmstats_percpu, cpu); |
| 5700 | |
| 5701 | for (i = 0; i < MEMCG_NR_STAT; i++) { |
| 5702 | /* |
| 5703 | * Collect the aggregated propagation counts of groups |
| 5704 | * below us. We're in a per-cpu loop here and this is |
| 5705 | * a global counter, so the first cycle will get them. |
| 5706 | */ |
Shakeel Butt | 410f8e8 | 2022-09-07 04:35:35 +0000 | [diff] [blame] | 5707 | delta = memcg->vmstats->state_pending[i]; |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 5708 | if (delta) |
Shakeel Butt | 410f8e8 | 2022-09-07 04:35:35 +0000 | [diff] [blame] | 5709 | memcg->vmstats->state_pending[i] = 0; |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 5710 | |
| 5711 | /* Add CPU changes on this level since the last flush */ |
Yosry Ahmed | f82e6bf | 2023-07-26 15:32:23 +0000 | [diff] [blame] | 5712 | delta_cpu = 0; |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 5713 | v = READ_ONCE(statc->state[i]); |
| 5714 | if (v != statc->state_prev[i]) { |
Yosry Ahmed | f82e6bf | 2023-07-26 15:32:23 +0000 | [diff] [blame] | 5715 | delta_cpu = v - statc->state_prev[i]; |
| 5716 | delta += delta_cpu; |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 5717 | statc->state_prev[i] = v; |
| 5718 | } |
| 5719 | |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 5720 | /* Aggregate counts on this level and propagate upwards */ |
Yosry Ahmed | f82e6bf | 2023-07-26 15:32:23 +0000 | [diff] [blame] | 5721 | if (delta_cpu) |
| 5722 | memcg->vmstats->state_local[i] += delta_cpu; |
| 5723 | |
| 5724 | if (delta) { |
| 5725 | memcg->vmstats->state[i] += delta; |
| 5726 | if (parent) |
| 5727 | parent->vmstats->state_pending[i] += delta; |
| 5728 | } |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 5729 | } |
| 5730 | |
Shakeel Butt | 8278f1c | 2022-09-07 04:35:37 +0000 | [diff] [blame] | 5731 | for (i = 0; i < NR_MEMCG_EVENTS; i++) { |
Shakeel Butt | 410f8e8 | 2022-09-07 04:35:35 +0000 | [diff] [blame] | 5732 | delta = memcg->vmstats->events_pending[i]; |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 5733 | if (delta) |
Shakeel Butt | 410f8e8 | 2022-09-07 04:35:35 +0000 | [diff] [blame] | 5734 | memcg->vmstats->events_pending[i] = 0; |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 5735 | |
Yosry Ahmed | f82e6bf | 2023-07-26 15:32:23 +0000 | [diff] [blame] | 5736 | delta_cpu = 0; |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 5737 | v = READ_ONCE(statc->events[i]); |
| 5738 | if (v != statc->events_prev[i]) { |
Yosry Ahmed | f82e6bf | 2023-07-26 15:32:23 +0000 | [diff] [blame] | 5739 | delta_cpu = v - statc->events_prev[i]; |
| 5740 | delta += delta_cpu; |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 5741 | statc->events_prev[i] = v; |
| 5742 | } |
| 5743 | |
Yosry Ahmed | f82e6bf | 2023-07-26 15:32:23 +0000 | [diff] [blame] | 5744 | if (delta_cpu) |
| 5745 | memcg->vmstats->events_local[i] += delta_cpu; |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 5746 | |
Yosry Ahmed | f82e6bf | 2023-07-26 15:32:23 +0000 | [diff] [blame] | 5747 | if (delta) { |
| 5748 | memcg->vmstats->events[i] += delta; |
| 5749 | if (parent) |
| 5750 | parent->vmstats->events_pending[i] += delta; |
| 5751 | } |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 5752 | } |
Shakeel Butt | 7e1c0d6 | 2021-09-02 14:55:00 -0700 | [diff] [blame] | 5753 | |
| 5754 | for_each_node_state(nid, N_MEMORY) { |
| 5755 | struct mem_cgroup_per_node *pn = memcg->nodeinfo[nid]; |
| 5756 | struct mem_cgroup_per_node *ppn = NULL; |
| 5757 | struct lruvec_stats_percpu *lstatc; |
| 5758 | |
| 5759 | if (parent) |
| 5760 | ppn = parent->nodeinfo[nid]; |
| 5761 | |
| 5762 | lstatc = per_cpu_ptr(pn->lruvec_stats_percpu, cpu); |
| 5763 | |
| 5764 | for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) { |
| 5765 | delta = pn->lruvec_stats.state_pending[i]; |
| 5766 | if (delta) |
| 5767 | pn->lruvec_stats.state_pending[i] = 0; |
| 5768 | |
Yosry Ahmed | f82e6bf | 2023-07-26 15:32:23 +0000 | [diff] [blame] | 5769 | delta_cpu = 0; |
Shakeel Butt | 7e1c0d6 | 2021-09-02 14:55:00 -0700 | [diff] [blame] | 5770 | v = READ_ONCE(lstatc->state[i]); |
| 5771 | if (v != lstatc->state_prev[i]) { |
Yosry Ahmed | f82e6bf | 2023-07-26 15:32:23 +0000 | [diff] [blame] | 5772 | delta_cpu = v - lstatc->state_prev[i]; |
| 5773 | delta += delta_cpu; |
Shakeel Butt | 7e1c0d6 | 2021-09-02 14:55:00 -0700 | [diff] [blame] | 5774 | lstatc->state_prev[i] = v; |
| 5775 | } |
| 5776 | |
Yosry Ahmed | f82e6bf | 2023-07-26 15:32:23 +0000 | [diff] [blame] | 5777 | if (delta_cpu) |
| 5778 | pn->lruvec_stats.state_local[i] += delta_cpu; |
Shakeel Butt | 7e1c0d6 | 2021-09-02 14:55:00 -0700 | [diff] [blame] | 5779 | |
Yosry Ahmed | f82e6bf | 2023-07-26 15:32:23 +0000 | [diff] [blame] | 5780 | if (delta) { |
| 5781 | pn->lruvec_stats.state[i] += delta; |
| 5782 | if (ppn) |
| 5783 | ppn->lruvec_stats.state_pending[i] += delta; |
| 5784 | } |
Shakeel Butt | 7e1c0d6 | 2021-09-02 14:55:00 -0700 | [diff] [blame] | 5785 | } |
| 5786 | } |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 5787 | } |
| 5788 | |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5789 | #ifdef CONFIG_MMU |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5790 | /* Handlers for move charge at task migration. */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5791 | static int mem_cgroup_do_precharge(unsigned long count) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 5792 | { |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 5793 | int ret; |
Johannes Weiner | 9476db9 | 2014-08-06 16:05:55 -0700 | [diff] [blame] | 5794 | |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 5795 | /* Try a single bulk charge without reclaim first, kswapd may wake */ |
| 5796 | ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_DIRECT_RECLAIM, count); |
Johannes Weiner | 9476db9 | 2014-08-06 16:05:55 -0700 | [diff] [blame] | 5797 | if (!ret) { |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5798 | mc.precharge += count; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5799 | return ret; |
| 5800 | } |
Johannes Weiner | 9476db9 | 2014-08-06 16:05:55 -0700 | [diff] [blame] | 5801 | |
David Rientjes | 3674534 | 2017-01-24 15:18:10 -0800 | [diff] [blame] | 5802 | /* Try charges one by one with reclaim, but do not retry */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5803 | while (count--) { |
David Rientjes | 3674534 | 2017-01-24 15:18:10 -0800 | [diff] [blame] | 5804 | ret = try_charge(mc.to, GFP_KERNEL | __GFP_NORETRY, 1); |
KAMEZAWA Hiroyuki | 38c5d72 | 2012-01-12 17:19:01 -0800 | [diff] [blame] | 5805 | if (ret) |
KAMEZAWA Hiroyuki | 38c5d72 | 2012-01-12 17:19:01 -0800 | [diff] [blame] | 5806 | return ret; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5807 | mc.precharge++; |
Johannes Weiner | 9476db9 | 2014-08-06 16:05:55 -0700 | [diff] [blame] | 5808 | cond_resched(); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 5809 | } |
Johannes Weiner | 9476db9 | 2014-08-06 16:05:55 -0700 | [diff] [blame] | 5810 | return 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5811 | } |
| 5812 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5813 | union mc_target { |
| 5814 | struct page *page; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5815 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5816 | }; |
| 5817 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5818 | enum mc_target_type { |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 5819 | MC_TARGET_NONE = 0, |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5820 | MC_TARGET_PAGE, |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 5821 | MC_TARGET_SWAP, |
Jérôme Glisse | c733a82 | 2017-09-08 16:11:54 -0700 | [diff] [blame] | 5822 | MC_TARGET_DEVICE, |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 5823 | }; |
| 5824 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5825 | static struct page *mc_handle_present_pte(struct vm_area_struct *vma, |
| 5826 | unsigned long addr, pte_t ptent) |
| 5827 | { |
Christoph Hellwig | 25b2995 | 2019-06-13 22:50:49 +0200 | [diff] [blame] | 5828 | struct page *page = vm_normal_page(vma, addr, ptent); |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5829 | |
Miaohe Lin | 58f341f | 2023-07-17 19:36:44 +0800 | [diff] [blame] | 5830 | if (!page) |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5831 | return NULL; |
| 5832 | if (PageAnon(page)) { |
Johannes Weiner | 1dfab5a | 2015-02-11 15:26:09 -0800 | [diff] [blame] | 5833 | if (!(mc.flags & MOVE_ANON)) |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5834 | return NULL; |
Johannes Weiner | 1dfab5a | 2015-02-11 15:26:09 -0800 | [diff] [blame] | 5835 | } else { |
| 5836 | if (!(mc.flags & MOVE_FILE)) |
| 5837 | return NULL; |
| 5838 | } |
Miaohe Lin | 58f341f | 2023-07-17 19:36:44 +0800 | [diff] [blame] | 5839 | get_page(page); |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5840 | |
| 5841 | return page; |
| 5842 | } |
| 5843 | |
Jérôme Glisse | c733a82 | 2017-09-08 16:11:54 -0700 | [diff] [blame] | 5844 | #if defined(CONFIG_SWAP) || defined(CONFIG_DEVICE_PRIVATE) |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5845 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, |
Li RongQing | 48406ef | 2016-07-26 15:22:14 -0700 | [diff] [blame] | 5846 | pte_t ptent, swp_entry_t *entry) |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5847 | { |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5848 | struct page *page = NULL; |
| 5849 | swp_entry_t ent = pte_to_swp_entry(ptent); |
| 5850 | |
Ralph Campbell | 9a13715 | 2020-10-13 16:53:13 -0700 | [diff] [blame] | 5851 | if (!(mc.flags & MOVE_ANON)) |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5852 | return NULL; |
Jérôme Glisse | c733a82 | 2017-09-08 16:11:54 -0700 | [diff] [blame] | 5853 | |
| 5854 | /* |
Christoph Hellwig | 27674ef | 2022-02-16 15:31:36 +1100 | [diff] [blame] | 5855 | * Handle device private pages that are not accessible by the CPU, but |
| 5856 | * stored as special swap entries in the page table. |
Jérôme Glisse | c733a82 | 2017-09-08 16:11:54 -0700 | [diff] [blame] | 5857 | */ |
| 5858 | if (is_device_private_entry(ent)) { |
Alistair Popple | af5cdaf | 2021-06-30 18:54:06 -0700 | [diff] [blame] | 5859 | page = pfn_swap_entry_to_page(ent); |
Christoph Hellwig | 27674ef | 2022-02-16 15:31:36 +1100 | [diff] [blame] | 5860 | if (!get_page_unless_zero(page)) |
Jérôme Glisse | c733a82 | 2017-09-08 16:11:54 -0700 | [diff] [blame] | 5861 | return NULL; |
| 5862 | return page; |
| 5863 | } |
| 5864 | |
Ralph Campbell | 9a13715 | 2020-10-13 16:53:13 -0700 | [diff] [blame] | 5865 | if (non_swap_entry(ent)) |
| 5866 | return NULL; |
| 5867 | |
KAMEZAWA Hiroyuki | 4b91355 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 5868 | /* |
Matthew Wilcox (Oracle) | cb691e2 | 2022-09-02 20:46:34 +0100 | [diff] [blame] | 5869 | * Because swap_cache_get_folio() updates some statistics counter, |
KAMEZAWA Hiroyuki | 4b91355 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 5870 | * we call find_get_page() with swapper_space directly. |
| 5871 | */ |
Huang Ying | f6ab1f7 | 2016-10-07 17:00:21 -0700 | [diff] [blame] | 5872 | page = find_get_page(swap_address_space(ent), swp_offset(ent)); |
Johannes Weiner | 2d1c498 | 2020-06-03 16:02:14 -0700 | [diff] [blame] | 5873 | entry->val = ent.val; |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5874 | |
| 5875 | return page; |
| 5876 | } |
KAMEZAWA Hiroyuki | 4b91355 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 5877 | #else |
| 5878 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, |
Li RongQing | 48406ef | 2016-07-26 15:22:14 -0700 | [diff] [blame] | 5879 | pte_t ptent, swp_entry_t *entry) |
KAMEZAWA Hiroyuki | 4b91355 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 5880 | { |
| 5881 | return NULL; |
| 5882 | } |
| 5883 | #endif |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 5884 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5885 | static struct page *mc_handle_file_pte(struct vm_area_struct *vma, |
Peter Xu | 48384b0 | 2021-11-05 13:37:28 -0700 | [diff] [blame] | 5886 | unsigned long addr, pte_t ptent) |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5887 | { |
Matthew Wilcox (Oracle) | 524984f | 2022-10-19 19:33:31 +0100 | [diff] [blame] | 5888 | unsigned long index; |
| 5889 | struct folio *folio; |
| 5890 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5891 | if (!vma->vm_file) /* anonymous vma */ |
| 5892 | return NULL; |
Johannes Weiner | 1dfab5a | 2015-02-11 15:26:09 -0800 | [diff] [blame] | 5893 | if (!(mc.flags & MOVE_FILE)) |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5894 | return NULL; |
| 5895 | |
Matthew Wilcox (Oracle) | 524984f | 2022-10-19 19:33:31 +0100 | [diff] [blame] | 5896 | /* folio is moved even if it's not RSS of this task(page-faulted). */ |
Hugh Dickins | aa3b189 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 5897 | /* shmem/tmpfs may report page out on swap: account for that too. */ |
Matthew Wilcox (Oracle) | 524984f | 2022-10-19 19:33:31 +0100 | [diff] [blame] | 5898 | index = linear_page_index(vma, addr); |
| 5899 | folio = filemap_get_incore_folio(vma->vm_file->f_mapping, index); |
Christoph Hellwig | 66dabbb | 2023-03-07 15:34:10 +0100 | [diff] [blame] | 5900 | if (IS_ERR(folio)) |
Matthew Wilcox (Oracle) | 524984f | 2022-10-19 19:33:31 +0100 | [diff] [blame] | 5901 | return NULL; |
| 5902 | return folio_file_page(folio, index); |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 5903 | } |
| 5904 | |
Chen Gang | b1b0dea | 2015-04-14 15:47:35 -0700 | [diff] [blame] | 5905 | /** |
| 5906 | * mem_cgroup_move_account - move account of the page |
| 5907 | * @page: the page |
Li RongQing | 25843c2 | 2016-07-26 15:26:56 -0700 | [diff] [blame] | 5908 | * @compound: charge the page as compound or small page |
Chen Gang | b1b0dea | 2015-04-14 15:47:35 -0700 | [diff] [blame] | 5909 | * @from: mem_cgroup which the page is moved from. |
| 5910 | * @to: mem_cgroup which the page is moved to. @from != @to. |
| 5911 | * |
Johannes Weiner | 4e0cf05 | 2022-12-06 18:13:39 +0100 | [diff] [blame] | 5912 | * The page must be locked and not on the LRU. |
Chen Gang | b1b0dea | 2015-04-14 15:47:35 -0700 | [diff] [blame] | 5913 | * |
| 5914 | * This function doesn't do "charge" to new cgroup and doesn't do "uncharge" |
| 5915 | * from old cgroup. |
| 5916 | */ |
| 5917 | static int mem_cgroup_move_account(struct page *page, |
Kirill A. Shutemov | f627c2f | 2016-01-15 16:52:20 -0800 | [diff] [blame] | 5918 | bool compound, |
Chen Gang | b1b0dea | 2015-04-14 15:47:35 -0700 | [diff] [blame] | 5919 | struct mem_cgroup *from, |
| 5920 | struct mem_cgroup *to) |
| 5921 | { |
Matthew Wilcox (Oracle) | fcce467 | 2021-03-01 16:34:06 -0500 | [diff] [blame] | 5922 | struct folio *folio = page_folio(page); |
Konstantin Khlebnikov | ae8af43 | 2019-10-18 20:20:11 -0700 | [diff] [blame] | 5923 | struct lruvec *from_vec, *to_vec; |
| 5924 | struct pglist_data *pgdat; |
Matthew Wilcox (Oracle) | fcce467 | 2021-03-01 16:34:06 -0500 | [diff] [blame] | 5925 | unsigned int nr_pages = compound ? folio_nr_pages(folio) : 1; |
Matthew Wilcox (Oracle) | 8e88bd2 | 2021-06-25 09:05:47 -0400 | [diff] [blame] | 5926 | int nid, ret; |
Chen Gang | b1b0dea | 2015-04-14 15:47:35 -0700 | [diff] [blame] | 5927 | |
| 5928 | VM_BUG_ON(from == to); |
Johannes Weiner | 4e0cf05 | 2022-12-06 18:13:39 +0100 | [diff] [blame] | 5929 | VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); |
Matthew Wilcox (Oracle) | fcce467 | 2021-03-01 16:34:06 -0500 | [diff] [blame] | 5930 | VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); |
Matthew Wilcox (Oracle) | 9c32521 | 2021-11-16 21:17:14 -0500 | [diff] [blame] | 5931 | VM_BUG_ON(compound && !folio_test_large(folio)); |
Chen Gang | b1b0dea | 2015-04-14 15:47:35 -0700 | [diff] [blame] | 5932 | |
Chen Gang | b1b0dea | 2015-04-14 15:47:35 -0700 | [diff] [blame] | 5933 | ret = -EINVAL; |
Matthew Wilcox (Oracle) | fcce467 | 2021-03-01 16:34:06 -0500 | [diff] [blame] | 5934 | if (folio_memcg(folio) != from) |
Johannes Weiner | 4e0cf05 | 2022-12-06 18:13:39 +0100 | [diff] [blame] | 5935 | goto out; |
Chen Gang | b1b0dea | 2015-04-14 15:47:35 -0700 | [diff] [blame] | 5936 | |
Matthew Wilcox (Oracle) | fcce467 | 2021-03-01 16:34:06 -0500 | [diff] [blame] | 5937 | pgdat = folio_pgdat(folio); |
Johannes Weiner | 867e5e1 | 2019-11-30 17:55:34 -0800 | [diff] [blame] | 5938 | from_vec = mem_cgroup_lruvec(from, pgdat); |
| 5939 | to_vec = mem_cgroup_lruvec(to, pgdat); |
Konstantin Khlebnikov | ae8af43 | 2019-10-18 20:20:11 -0700 | [diff] [blame] | 5940 | |
Matthew Wilcox (Oracle) | fcce467 | 2021-03-01 16:34:06 -0500 | [diff] [blame] | 5941 | folio_memcg_lock(folio); |
Chen Gang | b1b0dea | 2015-04-14 15:47:35 -0700 | [diff] [blame] | 5942 | |
Matthew Wilcox (Oracle) | fcce467 | 2021-03-01 16:34:06 -0500 | [diff] [blame] | 5943 | if (folio_test_anon(folio)) { |
| 5944 | if (folio_mapped(folio)) { |
Johannes Weiner | be5d0a7 | 2020-06-03 16:01:57 -0700 | [diff] [blame] | 5945 | __mod_lruvec_state(from_vec, NR_ANON_MAPPED, -nr_pages); |
| 5946 | __mod_lruvec_state(to_vec, NR_ANON_MAPPED, nr_pages); |
Matthew Wilcox (Oracle) | 6199277 | 2023-08-16 16:11:59 +0100 | [diff] [blame] | 5947 | if (folio_test_pmd_mappable(folio)) { |
Muchun Song | 69473e5 | 2021-02-24 12:03:23 -0800 | [diff] [blame] | 5948 | __mod_lruvec_state(from_vec, NR_ANON_THPS, |
| 5949 | -nr_pages); |
| 5950 | __mod_lruvec_state(to_vec, NR_ANON_THPS, |
| 5951 | nr_pages); |
Johannes Weiner | 468c398 | 2020-06-03 16:02:01 -0700 | [diff] [blame] | 5952 | } |
Johannes Weiner | be5d0a7 | 2020-06-03 16:01:57 -0700 | [diff] [blame] | 5953 | } |
| 5954 | } else { |
Johannes Weiner | 0d1c207 | 2020-06-03 16:01:54 -0700 | [diff] [blame] | 5955 | __mod_lruvec_state(from_vec, NR_FILE_PAGES, -nr_pages); |
| 5956 | __mod_lruvec_state(to_vec, NR_FILE_PAGES, nr_pages); |
| 5957 | |
Matthew Wilcox (Oracle) | fcce467 | 2021-03-01 16:34:06 -0500 | [diff] [blame] | 5958 | if (folio_test_swapbacked(folio)) { |
Johannes Weiner | 0d1c207 | 2020-06-03 16:01:54 -0700 | [diff] [blame] | 5959 | __mod_lruvec_state(from_vec, NR_SHMEM, -nr_pages); |
| 5960 | __mod_lruvec_state(to_vec, NR_SHMEM, nr_pages); |
| 5961 | } |
| 5962 | |
Matthew Wilcox (Oracle) | fcce467 | 2021-03-01 16:34:06 -0500 | [diff] [blame] | 5963 | if (folio_mapped(folio)) { |
Johannes Weiner | 49e50d2 | 2020-06-03 16:01:47 -0700 | [diff] [blame] | 5964 | __mod_lruvec_state(from_vec, NR_FILE_MAPPED, -nr_pages); |
| 5965 | __mod_lruvec_state(to_vec, NR_FILE_MAPPED, nr_pages); |
| 5966 | } |
Chen Gang | b1b0dea | 2015-04-14 15:47:35 -0700 | [diff] [blame] | 5967 | |
Matthew Wilcox (Oracle) | fcce467 | 2021-03-01 16:34:06 -0500 | [diff] [blame] | 5968 | if (folio_test_dirty(folio)) { |
| 5969 | struct address_space *mapping = folio_mapping(folio); |
Greg Thelen | c4843a7 | 2015-05-22 17:13:16 -0400 | [diff] [blame] | 5970 | |
Christoph Hellwig | f56753a | 2020-09-24 08:51:40 +0200 | [diff] [blame] | 5971 | if (mapping_can_writeback(mapping)) { |
Johannes Weiner | 49e50d2 | 2020-06-03 16:01:47 -0700 | [diff] [blame] | 5972 | __mod_lruvec_state(from_vec, NR_FILE_DIRTY, |
| 5973 | -nr_pages); |
| 5974 | __mod_lruvec_state(to_vec, NR_FILE_DIRTY, |
| 5975 | nr_pages); |
| 5976 | } |
Greg Thelen | c4843a7 | 2015-05-22 17:13:16 -0400 | [diff] [blame] | 5977 | } |
| 5978 | } |
| 5979 | |
Hugh Dickins | c449deb | 2022-12-04 17:01:03 -0800 | [diff] [blame] | 5980 | #ifdef CONFIG_SWAP |
| 5981 | if (folio_test_swapcache(folio)) { |
| 5982 | __mod_lruvec_state(from_vec, NR_SWAPCACHE, -nr_pages); |
| 5983 | __mod_lruvec_state(to_vec, NR_SWAPCACHE, nr_pages); |
| 5984 | } |
| 5985 | #endif |
Matthew Wilcox (Oracle) | fcce467 | 2021-03-01 16:34:06 -0500 | [diff] [blame] | 5986 | if (folio_test_writeback(folio)) { |
Konstantin Khlebnikov | ae8af43 | 2019-10-18 20:20:11 -0700 | [diff] [blame] | 5987 | __mod_lruvec_state(from_vec, NR_WRITEBACK, -nr_pages); |
| 5988 | __mod_lruvec_state(to_vec, NR_WRITEBACK, nr_pages); |
Chen Gang | b1b0dea | 2015-04-14 15:47:35 -0700 | [diff] [blame] | 5989 | } |
| 5990 | |
| 5991 | /* |
Johannes Weiner | abb242f | 2020-06-03 16:01:28 -0700 | [diff] [blame] | 5992 | * All state has been migrated, let's switch to the new memcg. |
| 5993 | * |
Roman Gushchin | bcfe06b | 2020-12-01 13:58:27 -0800 | [diff] [blame] | 5994 | * It is safe to change page's memcg here because the page |
Johannes Weiner | abb242f | 2020-06-03 16:01:28 -0700 | [diff] [blame] | 5995 | * is referenced, charged, isolated, and locked: we can't race |
| 5996 | * with (un)charging, migration, LRU putback, or anything else |
Roman Gushchin | bcfe06b | 2020-12-01 13:58:27 -0800 | [diff] [blame] | 5997 | * that would rely on a stable page's memory cgroup. |
Johannes Weiner | abb242f | 2020-06-03 16:01:28 -0700 | [diff] [blame] | 5998 | * |
Kefeng Wang | 6c77b60 | 2023-06-14 22:36:12 +0800 | [diff] [blame] | 5999 | * Note that folio_memcg_lock is a memcg lock, not a page lock, |
Roman Gushchin | bcfe06b | 2020-12-01 13:58:27 -0800 | [diff] [blame] | 6000 | * to save space. As soon as we switch page's memory cgroup to a |
Johannes Weiner | abb242f | 2020-06-03 16:01:28 -0700 | [diff] [blame] | 6001 | * new memcg that isn't locked, the above state can change |
| 6002 | * concurrently again. Make sure we're truly done with it. |
Chen Gang | b1b0dea | 2015-04-14 15:47:35 -0700 | [diff] [blame] | 6003 | */ |
Johannes Weiner | abb242f | 2020-06-03 16:01:28 -0700 | [diff] [blame] | 6004 | smp_mb(); |
Chen Gang | b1b0dea | 2015-04-14 15:47:35 -0700 | [diff] [blame] | 6005 | |
Johannes Weiner | 1a3e1f4 | 2020-08-06 23:20:45 -0700 | [diff] [blame] | 6006 | css_get(&to->css); |
| 6007 | css_put(&from->css); |
| 6008 | |
Matthew Wilcox (Oracle) | fcce467 | 2021-03-01 16:34:06 -0500 | [diff] [blame] | 6009 | folio->memcg_data = (unsigned long)to; |
Yang Shi | 87eaceb | 2019-09-23 15:38:15 -0700 | [diff] [blame] | 6010 | |
Matthew Wilcox (Oracle) | f70ad44 | 2021-06-28 17:26:00 -0400 | [diff] [blame] | 6011 | __folio_memcg_unlock(from); |
Chen Gang | b1b0dea | 2015-04-14 15:47:35 -0700 | [diff] [blame] | 6012 | |
| 6013 | ret = 0; |
Matthew Wilcox (Oracle) | fcce467 | 2021-03-01 16:34:06 -0500 | [diff] [blame] | 6014 | nid = folio_nid(folio); |
Chen Gang | b1b0dea | 2015-04-14 15:47:35 -0700 | [diff] [blame] | 6015 | |
| 6016 | local_irq_disable(); |
Matthew Wilcox (Oracle) | 6e0110c | 2021-04-29 13:40:11 -0400 | [diff] [blame] | 6017 | mem_cgroup_charge_statistics(to, nr_pages); |
Matthew Wilcox (Oracle) | 8e88bd2 | 2021-06-25 09:05:47 -0400 | [diff] [blame] | 6018 | memcg_check_events(to, nid); |
Matthew Wilcox (Oracle) | 6e0110c | 2021-04-29 13:40:11 -0400 | [diff] [blame] | 6019 | mem_cgroup_charge_statistics(from, -nr_pages); |
Matthew Wilcox (Oracle) | 8e88bd2 | 2021-06-25 09:05:47 -0400 | [diff] [blame] | 6020 | memcg_check_events(from, nid); |
Chen Gang | b1b0dea | 2015-04-14 15:47:35 -0700 | [diff] [blame] | 6021 | local_irq_enable(); |
Chen Gang | b1b0dea | 2015-04-14 15:47:35 -0700 | [diff] [blame] | 6022 | out: |
| 6023 | return ret; |
| 6024 | } |
| 6025 | |
Li RongQing | 7cf7806 | 2016-05-27 14:27:46 -0700 | [diff] [blame] | 6026 | /** |
| 6027 | * get_mctgt_type - get target type of moving charge |
| 6028 | * @vma: the vma the pte to be checked belongs |
| 6029 | * @addr: the address corresponding to the pte to be checked |
| 6030 | * @ptent: the pte to be checked |
| 6031 | * @target: the pointer the target page or swap ent will be stored(can be NULL) |
| 6032 | * |
Matthew Wilcox (Oracle) | 853f62a | 2023-08-18 21:06:28 +0100 | [diff] [blame] | 6033 | * Context: Called with pte lock held. |
| 6034 | * Return: |
| 6035 | * * MC_TARGET_NONE - If the pte is not a target for move charge. |
| 6036 | * * MC_TARGET_PAGE - If the page corresponding to this pte is a target for |
| 6037 | * move charge. If @target is not NULL, the page is stored in target->page |
| 6038 | * with extra refcnt taken (Caller should release it). |
| 6039 | * * MC_TARGET_SWAP - If the swap entry corresponding to this pte is a |
| 6040 | * target for charge migration. If @target is not NULL, the entry is |
| 6041 | * stored in target->ent. |
| 6042 | * * MC_TARGET_DEVICE - Like MC_TARGET_PAGE but page is device memory and |
| 6043 | * thus not on the lru. For now such page is charged like a regular page |
| 6044 | * would be as it is just special memory taking the place of a regular page. |
| 6045 | * See Documentations/vm/hmm.txt and include/linux/hmm.h |
Li RongQing | 7cf7806 | 2016-05-27 14:27:46 -0700 | [diff] [blame] | 6046 | */ |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 6047 | static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma, |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6048 | unsigned long addr, pte_t ptent, union mc_target *target) |
| 6049 | { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6050 | struct page *page = NULL; |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 6051 | enum mc_target_type ret = MC_TARGET_NONE; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6052 | swp_entry_t ent = { .val = 0 }; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6053 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 6054 | if (pte_present(ptent)) |
| 6055 | page = mc_handle_present_pte(vma, addr, ptent); |
Peter Xu | 5c041f5 | 2022-05-12 20:22:52 -0700 | [diff] [blame] | 6056 | else if (pte_none_mostly(ptent)) |
| 6057 | /* |
| 6058 | * PTE markers should be treated as a none pte here, separated |
| 6059 | * from other swap handling below. |
| 6060 | */ |
| 6061 | page = mc_handle_file_pte(vma, addr, ptent); |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 6062 | else if (is_swap_pte(ptent)) |
Li RongQing | 48406ef | 2016-07-26 15:22:14 -0700 | [diff] [blame] | 6063 | page = mc_handle_swap_pte(vma, ptent, &ent); |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 6064 | |
Johannes Weiner | 4e0cf05 | 2022-12-06 18:13:39 +0100 | [diff] [blame] | 6065 | if (target && page) { |
| 6066 | if (!trylock_page(page)) { |
| 6067 | put_page(page); |
| 6068 | return ret; |
| 6069 | } |
| 6070 | /* |
| 6071 | * page_mapped() must be stable during the move. This |
| 6072 | * pte is locked, so if it's present, the page cannot |
| 6073 | * become unmapped. If it isn't, we have only partial |
| 6074 | * control over the mapped state: the page lock will |
| 6075 | * prevent new faults against pagecache and swapcache, |
| 6076 | * so an unmapped page cannot become mapped. However, |
| 6077 | * if the page is already mapped elsewhere, it can |
| 6078 | * unmap, and there is nothing we can do about it. |
| 6079 | * Alas, skip moving the page in this case. |
| 6080 | */ |
| 6081 | if (!pte_present(ptent) && page_mapped(page)) { |
| 6082 | unlock_page(page); |
| 6083 | put_page(page); |
| 6084 | return ret; |
| 6085 | } |
| 6086 | } |
| 6087 | |
Daisuke Nishimura | 90254a6 | 2010-05-26 14:42:38 -0700 | [diff] [blame] | 6088 | if (!page && !ent.val) |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 6089 | return ret; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6090 | if (page) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6091 | /* |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 6092 | * Do only loose check w/o serialization. |
Johannes Weiner | 1306a85 | 2014-12-10 15:44:52 -0800 | [diff] [blame] | 6093 | * mem_cgroup_move_account() checks the page is valid or |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 6094 | * not under LRU exclusion. |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6095 | */ |
Roman Gushchin | bcfe06b | 2020-12-01 13:58:27 -0800 | [diff] [blame] | 6096 | if (page_memcg(page) == mc.from) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6097 | ret = MC_TARGET_PAGE; |
Alex Sierra | f25cbb7 | 2022-07-15 10:05:10 -0500 | [diff] [blame] | 6098 | if (is_device_private_page(page) || |
| 6099 | is_device_coherent_page(page)) |
Jérôme Glisse | c733a82 | 2017-09-08 16:11:54 -0700 | [diff] [blame] | 6100 | ret = MC_TARGET_DEVICE; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6101 | if (target) |
| 6102 | target->page = page; |
| 6103 | } |
Johannes Weiner | 4e0cf05 | 2022-12-06 18:13:39 +0100 | [diff] [blame] | 6104 | if (!ret || !target) { |
| 6105 | if (target) |
| 6106 | unlock_page(page); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6107 | put_page(page); |
Johannes Weiner | 4e0cf05 | 2022-12-06 18:13:39 +0100 | [diff] [blame] | 6108 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6109 | } |
Huang Ying | 3e14a57 | 2017-09-06 16:22:37 -0700 | [diff] [blame] | 6110 | /* |
| 6111 | * There is a swap entry and a page doesn't exist or isn't charged. |
| 6112 | * But we cannot move a tail-page in a THP. |
| 6113 | */ |
| 6114 | if (ent.val && !ret && (!page || !PageTransCompound(page)) && |
Li Zefan | 34c00c3 | 2013-09-23 16:56:01 +0800 | [diff] [blame] | 6115 | mem_cgroup_id(mc.from) == lookup_swap_cgroup_id(ent)) { |
KAMEZAWA Hiroyuki | 7f0f154 | 2010-05-11 14:06:58 -0700 | [diff] [blame] | 6116 | ret = MC_TARGET_SWAP; |
| 6117 | if (target) |
| 6118 | target->ent = ent; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6119 | } |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6120 | return ret; |
| 6121 | } |
| 6122 | |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6123 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 6124 | /* |
Huang Ying | d6810d73 | 2017-09-06 16:22:45 -0700 | [diff] [blame] | 6125 | * We don't consider PMD mapped swapping or file mapped pages because THP does |
| 6126 | * not support them for now. |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6127 | * Caller should make sure that pmd_trans_huge(pmd) is true. |
| 6128 | */ |
| 6129 | static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma, |
| 6130 | unsigned long addr, pmd_t pmd, union mc_target *target) |
| 6131 | { |
| 6132 | struct page *page = NULL; |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6133 | enum mc_target_type ret = MC_TARGET_NONE; |
| 6134 | |
Zi Yan | 84c3fc4 | 2017-09-08 16:11:01 -0700 | [diff] [blame] | 6135 | if (unlikely(is_swap_pmd(pmd))) { |
| 6136 | VM_BUG_ON(thp_migration_supported() && |
| 6137 | !is_pmd_migration_entry(pmd)); |
| 6138 | return ret; |
| 6139 | } |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6140 | page = pmd_page(pmd); |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 6141 | VM_BUG_ON_PAGE(!page || !PageHead(page), page); |
Johannes Weiner | 1dfab5a | 2015-02-11 15:26:09 -0800 | [diff] [blame] | 6142 | if (!(mc.flags & MOVE_ANON)) |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6143 | return ret; |
Roman Gushchin | bcfe06b | 2020-12-01 13:58:27 -0800 | [diff] [blame] | 6144 | if (page_memcg(page) == mc.from) { |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6145 | ret = MC_TARGET_PAGE; |
| 6146 | if (target) { |
| 6147 | get_page(page); |
Johannes Weiner | 4e0cf05 | 2022-12-06 18:13:39 +0100 | [diff] [blame] | 6148 | if (!trylock_page(page)) { |
| 6149 | put_page(page); |
| 6150 | return MC_TARGET_NONE; |
| 6151 | } |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6152 | target->page = page; |
| 6153 | } |
| 6154 | } |
| 6155 | return ret; |
| 6156 | } |
| 6157 | #else |
| 6158 | static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma, |
| 6159 | unsigned long addr, pmd_t pmd, union mc_target *target) |
| 6160 | { |
| 6161 | return MC_TARGET_NONE; |
| 6162 | } |
| 6163 | #endif |
| 6164 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6165 | static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd, |
| 6166 | unsigned long addr, unsigned long end, |
| 6167 | struct mm_walk *walk) |
| 6168 | { |
Naoya Horiguchi | 26bcd64 | 2015-02-11 15:27:57 -0800 | [diff] [blame] | 6169 | struct vm_area_struct *vma = walk->vma; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6170 | pte_t *pte; |
| 6171 | spinlock_t *ptl; |
| 6172 | |
Kirill A. Shutemov | b6ec57f | 2016-01-21 16:40:25 -0800 | [diff] [blame] | 6173 | ptl = pmd_trans_huge_lock(pmd, vma); |
| 6174 | if (ptl) { |
Jérôme Glisse | c733a82 | 2017-09-08 16:11:54 -0700 | [diff] [blame] | 6175 | /* |
| 6176 | * Note their can not be MC_TARGET_DEVICE for now as we do not |
Christoph Hellwig | 25b2995 | 2019-06-13 22:50:49 +0200 | [diff] [blame] | 6177 | * support transparent huge page with MEMORY_DEVICE_PRIVATE but |
| 6178 | * this might change. |
Jérôme Glisse | c733a82 | 2017-09-08 16:11:54 -0700 | [diff] [blame] | 6179 | */ |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6180 | if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE) |
| 6181 | mc.precharge += HPAGE_PMD_NR; |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 6182 | spin_unlock(ptl); |
Andrea Arcangeli | 1a5a990 | 2012-03-21 16:33:42 -0700 | [diff] [blame] | 6183 | return 0; |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6184 | } |
Dave Hansen | 0331932 | 2011-03-22 16:32:56 -0700 | [diff] [blame] | 6185 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6186 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
Hugh Dickins | 04dee9e | 2023-06-08 18:29:22 -0700 | [diff] [blame] | 6187 | if (!pte) |
| 6188 | return 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6189 | for (; addr != end; pte++, addr += PAGE_SIZE) |
Ryan Roberts | c33c794 | 2023-06-12 16:15:45 +0100 | [diff] [blame] | 6190 | if (get_mctgt_type(vma, addr, ptep_get(pte), NULL)) |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6191 | mc.precharge++; /* increment precharge temporarily */ |
| 6192 | pte_unmap_unlock(pte - 1, ptl); |
| 6193 | cond_resched(); |
| 6194 | |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6195 | return 0; |
| 6196 | } |
| 6197 | |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 6198 | static const struct mm_walk_ops precharge_walk_ops = { |
| 6199 | .pmd_entry = mem_cgroup_count_precharge_pte_range, |
Suren Baghdasaryan | 49b0638 | 2023-08-04 08:27:19 -0700 | [diff] [blame] | 6200 | .walk_lock = PGWALK_RDLOCK, |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 6201 | }; |
| 6202 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6203 | static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm) |
| 6204 | { |
| 6205 | unsigned long precharge; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6206 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 6207 | mmap_read_lock(mm); |
Liam R. Howlett | ba0aff8 | 2022-09-06 19:49:01 +0000 | [diff] [blame] | 6208 | walk_page_range(mm, 0, ULONG_MAX, &precharge_walk_ops, NULL); |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 6209 | mmap_read_unlock(mm); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6210 | |
| 6211 | precharge = mc.precharge; |
| 6212 | mc.precharge = 0; |
| 6213 | |
| 6214 | return precharge; |
| 6215 | } |
| 6216 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6217 | static int mem_cgroup_precharge_mc(struct mm_struct *mm) |
| 6218 | { |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 6219 | unsigned long precharge = mem_cgroup_count_precharge(mm); |
| 6220 | |
| 6221 | VM_BUG_ON(mc.moving_task); |
| 6222 | mc.moving_task = current; |
| 6223 | return mem_cgroup_do_precharge(precharge); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6224 | } |
| 6225 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 6226 | /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */ |
| 6227 | static void __mem_cgroup_clear_mc(void) |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6228 | { |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 6229 | struct mem_cgroup *from = mc.from; |
| 6230 | struct mem_cgroup *to = mc.to; |
| 6231 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6232 | /* we must uncharge all the leftover precharges from mc.to */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 6233 | if (mc.precharge) { |
Nhat Pham | 4b56938 | 2023-10-06 11:46:26 -0700 | [diff] [blame] | 6234 | mem_cgroup_cancel_charge(mc.to, mc.precharge); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 6235 | mc.precharge = 0; |
| 6236 | } |
| 6237 | /* |
| 6238 | * we didn't uncharge from mc.from at mem_cgroup_move_account(), so |
| 6239 | * we must uncharge here. |
| 6240 | */ |
| 6241 | if (mc.moved_charge) { |
Nhat Pham | 4b56938 | 2023-10-06 11:46:26 -0700 | [diff] [blame] | 6242 | mem_cgroup_cancel_charge(mc.from, mc.moved_charge); |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 6243 | mc.moved_charge = 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6244 | } |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 6245 | /* we must fixup refcnts and charges */ |
| 6246 | if (mc.moved_swap) { |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 6247 | /* uncharge swap account from the old cgroup */ |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 6248 | if (!mem_cgroup_is_root(mc.from)) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 6249 | page_counter_uncharge(&mc.from->memsw, mc.moved_swap); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 6250 | |
Vladimir Davydov | 615d66c | 2016-08-11 15:33:03 -0700 | [diff] [blame] | 6251 | mem_cgroup_id_put_many(mc.from, mc.moved_swap); |
| 6252 | |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 6253 | /* |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 6254 | * we charged both to->memory and to->memsw, so we |
| 6255 | * should uncharge to->memory. |
Johannes Weiner | 05b8430 | 2014-08-06 16:05:59 -0700 | [diff] [blame] | 6256 | */ |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 6257 | if (!mem_cgroup_is_root(mc.to)) |
Johannes Weiner | 3e32cb2 | 2014-12-10 15:42:31 -0800 | [diff] [blame] | 6258 | page_counter_uncharge(&mc.to->memory, mc.moved_swap); |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 6259 | |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 6260 | mc.moved_swap = 0; |
| 6261 | } |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 6262 | memcg_oom_recover(from); |
| 6263 | memcg_oom_recover(to); |
| 6264 | wake_up_all(&mc.waitq); |
| 6265 | } |
| 6266 | |
| 6267 | static void mem_cgroup_clear_mc(void) |
| 6268 | { |
Tejun Heo | 264a0ae | 2016-04-21 19:09:02 -0400 | [diff] [blame] | 6269 | struct mm_struct *mm = mc.mm; |
| 6270 | |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 6271 | /* |
| 6272 | * we must clear moving_task before waking up waiters at the end of |
| 6273 | * task migration. |
| 6274 | */ |
| 6275 | mc.moving_task = NULL; |
| 6276 | __mem_cgroup_clear_mc(); |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 6277 | spin_lock(&mc.lock); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6278 | mc.from = NULL; |
| 6279 | mc.to = NULL; |
Tejun Heo | 264a0ae | 2016-04-21 19:09:02 -0400 | [diff] [blame] | 6280 | mc.mm = NULL; |
KAMEZAWA Hiroyuki | 2bd9bb2 | 2010-08-10 18:02:58 -0700 | [diff] [blame] | 6281 | spin_unlock(&mc.lock); |
Tejun Heo | 264a0ae | 2016-04-21 19:09:02 -0400 | [diff] [blame] | 6282 | |
| 6283 | mmput(mm); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6284 | } |
| 6285 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 6286 | static int mem_cgroup_can_attach(struct cgroup_taskset *tset) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6287 | { |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 6288 | struct cgroup_subsys_state *css; |
Ross Zwisler | eed67d7 | 2015-12-23 14:53:27 -0700 | [diff] [blame] | 6289 | struct mem_cgroup *memcg = NULL; /* unneeded init to make gcc happy */ |
Tejun Heo | 9f2115f | 2015-09-08 15:01:10 -0700 | [diff] [blame] | 6290 | struct mem_cgroup *from; |
Tejun Heo | 4530edd | 2015-09-11 15:00:19 -0400 | [diff] [blame] | 6291 | struct task_struct *leader, *p; |
Tejun Heo | 9f2115f | 2015-09-08 15:01:10 -0700 | [diff] [blame] | 6292 | struct mm_struct *mm; |
Johannes Weiner | 1dfab5a | 2015-02-11 15:26:09 -0800 | [diff] [blame] | 6293 | unsigned long move_flags; |
Tejun Heo | 9f2115f | 2015-09-08 15:01:10 -0700 | [diff] [blame] | 6294 | int ret = 0; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6295 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 6296 | /* charge immigration isn't supported on the default hierarchy */ |
| 6297 | if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) |
Tejun Heo | 9f2115f | 2015-09-08 15:01:10 -0700 | [diff] [blame] | 6298 | return 0; |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6299 | |
Tejun Heo | 4530edd | 2015-09-11 15:00:19 -0400 | [diff] [blame] | 6300 | /* |
| 6301 | * Multi-process migrations only happen on the default hierarchy |
| 6302 | * where charge immigration is not used. Perform charge |
| 6303 | * immigration if @tset contains a leader and whine if there are |
| 6304 | * multiple. |
| 6305 | */ |
| 6306 | p = NULL; |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 6307 | cgroup_taskset_for_each_leader(leader, css, tset) { |
Tejun Heo | 4530edd | 2015-09-11 15:00:19 -0400 | [diff] [blame] | 6308 | WARN_ON_ONCE(p); |
| 6309 | p = leader; |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 6310 | memcg = mem_cgroup_from_css(css); |
Tejun Heo | 4530edd | 2015-09-11 15:00:19 -0400 | [diff] [blame] | 6311 | } |
| 6312 | if (!p) |
| 6313 | return 0; |
| 6314 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 6315 | /* |
Ingo Molnar | f0953a1 | 2021-05-06 18:06:47 -0700 | [diff] [blame] | 6316 | * We are now committed to this value whatever it is. Changes in this |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 6317 | * tunable will only affect upcoming migrations, not the current one. |
| 6318 | * So we need to save it, and keep it going. |
| 6319 | */ |
| 6320 | move_flags = READ_ONCE(memcg->move_charge_at_immigrate); |
| 6321 | if (!move_flags) |
| 6322 | return 0; |
| 6323 | |
Tejun Heo | 9f2115f | 2015-09-08 15:01:10 -0700 | [diff] [blame] | 6324 | from = mem_cgroup_from_task(p); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6325 | |
Tejun Heo | 9f2115f | 2015-09-08 15:01:10 -0700 | [diff] [blame] | 6326 | VM_BUG_ON(from == memcg); |
Johannes Weiner | 247b144 | 2014-12-10 15:44:11 -0800 | [diff] [blame] | 6327 | |
Tejun Heo | 9f2115f | 2015-09-08 15:01:10 -0700 | [diff] [blame] | 6328 | mm = get_task_mm(p); |
| 6329 | if (!mm) |
| 6330 | return 0; |
| 6331 | /* We move charges only when we move a owner of the mm */ |
| 6332 | if (mm->owner == p) { |
| 6333 | VM_BUG_ON(mc.from); |
| 6334 | VM_BUG_ON(mc.to); |
| 6335 | VM_BUG_ON(mc.precharge); |
| 6336 | VM_BUG_ON(mc.moved_charge); |
| 6337 | VM_BUG_ON(mc.moved_swap); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6338 | |
Tejun Heo | 9f2115f | 2015-09-08 15:01:10 -0700 | [diff] [blame] | 6339 | spin_lock(&mc.lock); |
Tejun Heo | 264a0ae | 2016-04-21 19:09:02 -0400 | [diff] [blame] | 6340 | mc.mm = mm; |
Tejun Heo | 9f2115f | 2015-09-08 15:01:10 -0700 | [diff] [blame] | 6341 | mc.from = from; |
| 6342 | mc.to = memcg; |
| 6343 | mc.flags = move_flags; |
| 6344 | spin_unlock(&mc.lock); |
| 6345 | /* We set mc.moving_task later */ |
| 6346 | |
| 6347 | ret = mem_cgroup_precharge_mc(mm); |
| 6348 | if (ret) |
| 6349 | mem_cgroup_clear_mc(); |
Tejun Heo | 264a0ae | 2016-04-21 19:09:02 -0400 | [diff] [blame] | 6350 | } else { |
| 6351 | mmput(mm); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6352 | } |
| 6353 | return ret; |
| 6354 | } |
| 6355 | |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 6356 | static void mem_cgroup_cancel_attach(struct cgroup_taskset *tset) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6357 | { |
Johannes Weiner | 4e2f245 | 2014-12-10 15:44:08 -0800 | [diff] [blame] | 6358 | if (mc.to) |
| 6359 | mem_cgroup_clear_mc(); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6360 | } |
| 6361 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6362 | static int mem_cgroup_move_charge_pte_range(pmd_t *pmd, |
| 6363 | unsigned long addr, unsigned long end, |
| 6364 | struct mm_walk *walk) |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6365 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6366 | int ret = 0; |
Naoya Horiguchi | 26bcd64 | 2015-02-11 15:27:57 -0800 | [diff] [blame] | 6367 | struct vm_area_struct *vma = walk->vma; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6368 | pte_t *pte; |
| 6369 | spinlock_t *ptl; |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6370 | enum mc_target_type target_type; |
| 6371 | union mc_target target; |
| 6372 | struct page *page; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6373 | |
Kirill A. Shutemov | b6ec57f | 2016-01-21 16:40:25 -0800 | [diff] [blame] | 6374 | ptl = pmd_trans_huge_lock(pmd, vma); |
| 6375 | if (ptl) { |
Hugh Dickins | 62ade86 | 2012-05-18 11:28:34 -0700 | [diff] [blame] | 6376 | if (mc.precharge < HPAGE_PMD_NR) { |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 6377 | spin_unlock(ptl); |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6378 | return 0; |
| 6379 | } |
| 6380 | target_type = get_mctgt_type_thp(vma, addr, *pmd, &target); |
| 6381 | if (target_type == MC_TARGET_PAGE) { |
| 6382 | page = target.page; |
Baolin Wang | f7f9c00 | 2023-02-15 18:39:35 +0800 | [diff] [blame] | 6383 | if (isolate_lru_page(page)) { |
Kirill A. Shutemov | f627c2f | 2016-01-15 16:52:20 -0800 | [diff] [blame] | 6384 | if (!mem_cgroup_move_account(page, true, |
Johannes Weiner | 1306a85 | 2014-12-10 15:44:52 -0800 | [diff] [blame] | 6385 | mc.from, mc.to)) { |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6386 | mc.precharge -= HPAGE_PMD_NR; |
| 6387 | mc.moved_charge += HPAGE_PMD_NR; |
| 6388 | } |
| 6389 | putback_lru_page(page); |
| 6390 | } |
Johannes Weiner | 4e0cf05 | 2022-12-06 18:13:39 +0100 | [diff] [blame] | 6391 | unlock_page(page); |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6392 | put_page(page); |
Jérôme Glisse | c733a82 | 2017-09-08 16:11:54 -0700 | [diff] [blame] | 6393 | } else if (target_type == MC_TARGET_DEVICE) { |
| 6394 | page = target.page; |
| 6395 | if (!mem_cgroup_move_account(page, true, |
| 6396 | mc.from, mc.to)) { |
| 6397 | mc.precharge -= HPAGE_PMD_NR; |
| 6398 | mc.moved_charge += HPAGE_PMD_NR; |
| 6399 | } |
Johannes Weiner | 4e0cf05 | 2022-12-06 18:13:39 +0100 | [diff] [blame] | 6400 | unlock_page(page); |
Jérôme Glisse | c733a82 | 2017-09-08 16:11:54 -0700 | [diff] [blame] | 6401 | put_page(page); |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6402 | } |
Kirill A. Shutemov | bf92915 | 2013-11-14 14:30:54 -0800 | [diff] [blame] | 6403 | spin_unlock(ptl); |
Andrea Arcangeli | 1a5a990 | 2012-03-21 16:33:42 -0700 | [diff] [blame] | 6404 | return 0; |
Naoya Horiguchi | 1272485 | 2012-03-21 16:34:28 -0700 | [diff] [blame] | 6405 | } |
| 6406 | |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6407 | retry: |
| 6408 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
Hugh Dickins | 04dee9e | 2023-06-08 18:29:22 -0700 | [diff] [blame] | 6409 | if (!pte) |
| 6410 | return 0; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6411 | for (; addr != end; addr += PAGE_SIZE) { |
Ryan Roberts | c33c794 | 2023-06-12 16:15:45 +0100 | [diff] [blame] | 6412 | pte_t ptent = ptep_get(pte++); |
Jérôme Glisse | c733a82 | 2017-09-08 16:11:54 -0700 | [diff] [blame] | 6413 | bool device = false; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6414 | swp_entry_t ent; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6415 | |
| 6416 | if (!mc.precharge) |
| 6417 | break; |
| 6418 | |
Naoya Horiguchi | 8d32ff8 | 2012-03-21 16:34:27 -0700 | [diff] [blame] | 6419 | switch (get_mctgt_type(vma, addr, ptent, &target)) { |
Jérôme Glisse | c733a82 | 2017-09-08 16:11:54 -0700 | [diff] [blame] | 6420 | case MC_TARGET_DEVICE: |
| 6421 | device = true; |
Joe Perches | e4a9bc5 | 2020-04-06 20:08:39 -0700 | [diff] [blame] | 6422 | fallthrough; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6423 | case MC_TARGET_PAGE: |
| 6424 | page = target.page; |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 6425 | /* |
| 6426 | * We can have a part of the split pmd here. Moving it |
| 6427 | * can be done but it would be too convoluted so simply |
| 6428 | * ignore such a partial THP and keep it in original |
| 6429 | * memcg. There should be somebody mapping the head. |
| 6430 | */ |
| 6431 | if (PageTransCompound(page)) |
| 6432 | goto put; |
Baolin Wang | f7f9c00 | 2023-02-15 18:39:35 +0800 | [diff] [blame] | 6433 | if (!device && !isolate_lru_page(page)) |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6434 | goto put; |
Kirill A. Shutemov | f627c2f | 2016-01-15 16:52:20 -0800 | [diff] [blame] | 6435 | if (!mem_cgroup_move_account(page, false, |
| 6436 | mc.from, mc.to)) { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6437 | mc.precharge--; |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 6438 | /* we uncharge from mc.from later. */ |
| 6439 | mc.moved_charge++; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6440 | } |
Jérôme Glisse | c733a82 | 2017-09-08 16:11:54 -0700 | [diff] [blame] | 6441 | if (!device) |
| 6442 | putback_lru_page(page); |
Johannes Weiner | 4e0cf05 | 2022-12-06 18:13:39 +0100 | [diff] [blame] | 6443 | put: /* get_mctgt_type() gets & locks the page */ |
| 6444 | unlock_page(page); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6445 | put_page(page); |
| 6446 | break; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6447 | case MC_TARGET_SWAP: |
| 6448 | ent = target.ent; |
Hugh Dickins | e91cbb4 | 2012-05-29 15:06:51 -0700 | [diff] [blame] | 6449 | if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6450 | mc.precharge--; |
Hugh Dickins | 8d22a93 | 2020-07-23 21:15:24 -0700 | [diff] [blame] | 6451 | mem_cgroup_id_get_many(mc.to, 1); |
| 6452 | /* we fixup other refcnts and charges later. */ |
Daisuke Nishimura | 483c30b | 2010-03-10 15:22:18 -0800 | [diff] [blame] | 6453 | mc.moved_swap++; |
| 6454 | } |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 6455 | break; |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6456 | default: |
| 6457 | break; |
| 6458 | } |
| 6459 | } |
| 6460 | pte_unmap_unlock(pte - 1, ptl); |
| 6461 | cond_resched(); |
| 6462 | |
| 6463 | if (addr != end) { |
| 6464 | /* |
| 6465 | * We have consumed all precharges we got in can_attach(). |
| 6466 | * We try charge one by one, but don't do any additional |
| 6467 | * charges to mc.to if we have failed in charge once in attach() |
| 6468 | * phase. |
| 6469 | */ |
Daisuke Nishimura | 854ffa8 | 2010-03-10 15:22:15 -0800 | [diff] [blame] | 6470 | ret = mem_cgroup_do_precharge(1); |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6471 | if (!ret) |
| 6472 | goto retry; |
| 6473 | } |
| 6474 | |
| 6475 | return ret; |
| 6476 | } |
| 6477 | |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 6478 | static const struct mm_walk_ops charge_walk_ops = { |
| 6479 | .pmd_entry = mem_cgroup_move_charge_pte_range, |
Suren Baghdasaryan | 49b0638 | 2023-08-04 08:27:19 -0700 | [diff] [blame] | 6480 | .walk_lock = PGWALK_RDLOCK, |
Christoph Hellwig | 7b86ac3 | 2019-08-28 16:19:54 +0200 | [diff] [blame] | 6481 | }; |
| 6482 | |
Tejun Heo | 264a0ae | 2016-04-21 19:09:02 -0400 | [diff] [blame] | 6483 | static void mem_cgroup_move_charge(void) |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6484 | { |
Daisuke Nishimura | 4ffef5f | 2010-03-10 15:22:14 -0800 | [diff] [blame] | 6485 | lru_add_drain_all(); |
Johannes Weiner | 312722c | 2014-12-10 15:44:25 -0800 | [diff] [blame] | 6486 | /* |
Kefeng Wang | 6c77b60 | 2023-06-14 22:36:12 +0800 | [diff] [blame] | 6487 | * Signal folio_memcg_lock() to take the memcg's move_lock |
Johannes Weiner | 81f8c3a | 2016-03-15 14:57:04 -0700 | [diff] [blame] | 6488 | * while we're moving its pages to another memcg. Then wait |
| 6489 | * for already started RCU-only updates to finish. |
Johannes Weiner | 312722c | 2014-12-10 15:44:25 -0800 | [diff] [blame] | 6490 | */ |
| 6491 | atomic_inc(&mc.from->moving_account); |
| 6492 | synchronize_rcu(); |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 6493 | retry: |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 6494 | if (unlikely(!mmap_read_trylock(mc.mm))) { |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 6495 | /* |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 6496 | * Someone who are holding the mmap_lock might be waiting in |
Daisuke Nishimura | dfe076b | 2011-01-13 15:47:41 -0800 | [diff] [blame] | 6497 | * waitq. So we cancel all extra charges, wake up all waiters, |
| 6498 | * and retry. Because we cancel precharges, we might not be able |
| 6499 | * to move enough charges, but moving charge is a best-effort |
| 6500 | * feature anyway, so it wouldn't be a big problem. |
| 6501 | */ |
| 6502 | __mem_cgroup_clear_mc(); |
| 6503 | cond_resched(); |
| 6504 | goto retry; |
| 6505 | } |
Naoya Horiguchi | 26bcd64 | 2015-02-11 15:27:57 -0800 | [diff] [blame] | 6506 | /* |
| 6507 | * When we have consumed all precharges and failed in doing |
| 6508 | * additional charge, the page walk just aborts. |
| 6509 | */ |
Liam R. Howlett | ba0aff8 | 2022-09-06 19:49:01 +0000 | [diff] [blame] | 6510 | walk_page_range(mc.mm, 0, ULONG_MAX, &charge_walk_ops, NULL); |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 6511 | mmap_read_unlock(mc.mm); |
Johannes Weiner | 312722c | 2014-12-10 15:44:25 -0800 | [diff] [blame] | 6512 | atomic_dec(&mc.from->moving_account); |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 6513 | } |
| 6514 | |
Tejun Heo | 264a0ae | 2016-04-21 19:09:02 -0400 | [diff] [blame] | 6515 | static void mem_cgroup_move_task(void) |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 6516 | { |
Tejun Heo | 264a0ae | 2016-04-21 19:09:02 -0400 | [diff] [blame] | 6517 | if (mc.to) { |
| 6518 | mem_cgroup_move_charge(); |
KOSAKI Motohiro | a433658 | 2011-06-15 15:08:13 -0700 | [diff] [blame] | 6519 | mem_cgroup_clear_mc(); |
Tejun Heo | 264a0ae | 2016-04-21 19:09:02 -0400 | [diff] [blame] | 6520 | } |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 6521 | } |
Roman Gushchin | 1aacbd3 | 2023-10-19 15:53:42 -0700 | [diff] [blame] | 6522 | |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 6523 | #else /* !CONFIG_MMU */ |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 6524 | static int mem_cgroup_can_attach(struct cgroup_taskset *tset) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 6525 | { |
| 6526 | return 0; |
| 6527 | } |
Tejun Heo | 1f7dd3e5 | 2015-12-03 10:18:21 -0500 | [diff] [blame] | 6528 | static void mem_cgroup_cancel_attach(struct cgroup_taskset *tset) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 6529 | { |
| 6530 | } |
Tejun Heo | 264a0ae | 2016-04-21 19:09:02 -0400 | [diff] [blame] | 6531 | static void mem_cgroup_move_task(void) |
Daisuke Nishimura | 5cfb80a | 2010-03-23 13:35:11 -0700 | [diff] [blame] | 6532 | { |
| 6533 | } |
| 6534 | #endif |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 6535 | |
Roman Gushchin | 1aacbd3 | 2023-10-19 15:53:42 -0700 | [diff] [blame] | 6536 | #ifdef CONFIG_MEMCG_KMEM |
| 6537 | static void mem_cgroup_fork(struct task_struct *task) |
| 6538 | { |
| 6539 | /* |
| 6540 | * Set the update flag to cause task->objcg to be initialized lazily |
| 6541 | * on the first allocation. It can be done without any synchronization |
| 6542 | * because it's always performed on the current task, so does |
| 6543 | * current_objcg_update(). |
| 6544 | */ |
| 6545 | task->objcg = (struct obj_cgroup *)CURRENT_OBJCG_UPDATE_FLAG; |
| 6546 | } |
| 6547 | |
| 6548 | static void mem_cgroup_exit(struct task_struct *task) |
| 6549 | { |
| 6550 | struct obj_cgroup *objcg = task->objcg; |
| 6551 | |
| 6552 | objcg = (struct obj_cgroup *) |
| 6553 | ((unsigned long)objcg & ~CURRENT_OBJCG_UPDATE_FLAG); |
| 6554 | if (objcg) |
| 6555 | obj_cgroup_put(objcg); |
| 6556 | |
| 6557 | /* |
| 6558 | * Some kernel allocations can happen after this point, |
| 6559 | * but let's ignore them. It can be done without any synchronization |
| 6560 | * because it's always performed on the current task, so does |
| 6561 | * current_objcg_update(). |
| 6562 | */ |
| 6563 | task->objcg = NULL; |
| 6564 | } |
| 6565 | #endif |
| 6566 | |
Yu Zhao | bd74fda | 2022-09-18 02:00:05 -0600 | [diff] [blame] | 6567 | #ifdef CONFIG_LRU_GEN |
Roman Gushchin | 1aacbd3 | 2023-10-19 15:53:42 -0700 | [diff] [blame] | 6568 | static void mem_cgroup_lru_gen_attach(struct cgroup_taskset *tset) |
Yu Zhao | bd74fda | 2022-09-18 02:00:05 -0600 | [diff] [blame] | 6569 | { |
| 6570 | struct task_struct *task; |
| 6571 | struct cgroup_subsys_state *css; |
| 6572 | |
| 6573 | /* find the first leader if there is any */ |
| 6574 | cgroup_taskset_for_each_leader(task, css, tset) |
| 6575 | break; |
| 6576 | |
| 6577 | if (!task) |
| 6578 | return; |
| 6579 | |
| 6580 | task_lock(task); |
| 6581 | if (task->mm && READ_ONCE(task->mm->owner) == task) |
| 6582 | lru_gen_migrate_mm(task->mm); |
| 6583 | task_unlock(task); |
| 6584 | } |
| 6585 | #else |
Roman Gushchin | 1aacbd3 | 2023-10-19 15:53:42 -0700 | [diff] [blame] | 6586 | static void mem_cgroup_lru_gen_attach(struct cgroup_taskset *tset) {} |
| 6587 | #endif /* CONFIG_LRU_GEN */ |
| 6588 | |
| 6589 | #ifdef CONFIG_MEMCG_KMEM |
| 6590 | static void mem_cgroup_kmem_attach(struct cgroup_taskset *tset) |
| 6591 | { |
| 6592 | struct task_struct *task; |
| 6593 | struct cgroup_subsys_state *css; |
| 6594 | |
| 6595 | cgroup_taskset_for_each(task, css, tset) { |
| 6596 | /* atomically set the update bit */ |
| 6597 | set_bit(CURRENT_OBJCG_UPDATE_BIT, (unsigned long *)&task->objcg); |
| 6598 | } |
| 6599 | } |
| 6600 | #else |
| 6601 | static void mem_cgroup_kmem_attach(struct cgroup_taskset *tset) {} |
| 6602 | #endif /* CONFIG_MEMCG_KMEM */ |
| 6603 | |
| 6604 | #if defined(CONFIG_LRU_GEN) || defined(CONFIG_MEMCG_KMEM) |
Yu Zhao | bd74fda | 2022-09-18 02:00:05 -0600 | [diff] [blame] | 6605 | static void mem_cgroup_attach(struct cgroup_taskset *tset) |
| 6606 | { |
Roman Gushchin | 1aacbd3 | 2023-10-19 15:53:42 -0700 | [diff] [blame] | 6607 | mem_cgroup_lru_gen_attach(tset); |
| 6608 | mem_cgroup_kmem_attach(tset); |
Yu Zhao | bd74fda | 2022-09-18 02:00:05 -0600 | [diff] [blame] | 6609 | } |
Roman Gushchin | 1aacbd3 | 2023-10-19 15:53:42 -0700 | [diff] [blame] | 6610 | #endif |
Yu Zhao | bd74fda | 2022-09-18 02:00:05 -0600 | [diff] [blame] | 6611 | |
Chris Down | 677dc97 | 2019-03-05 15:45:55 -0800 | [diff] [blame] | 6612 | static int seq_puts_memcg_tunable(struct seq_file *m, unsigned long value) |
| 6613 | { |
| 6614 | if (value == PAGE_COUNTER_MAX) |
| 6615 | seq_puts(m, "max\n"); |
| 6616 | else |
| 6617 | seq_printf(m, "%llu\n", (u64)value * PAGE_SIZE); |
| 6618 | |
| 6619 | return 0; |
| 6620 | } |
| 6621 | |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6622 | static u64 memory_current_read(struct cgroup_subsys_state *css, |
| 6623 | struct cftype *cft) |
| 6624 | { |
Johannes Weiner | f5fc3c5d | 2015-11-05 18:50:23 -0800 | [diff] [blame] | 6625 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
| 6626 | |
| 6627 | return (u64)page_counter_read(&memcg->memory) * PAGE_SIZE; |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6628 | } |
| 6629 | |
Ganesan Rajagopal | 8e20d4b | 2022-05-13 16:48:57 -0700 | [diff] [blame] | 6630 | static u64 memory_peak_read(struct cgroup_subsys_state *css, |
| 6631 | struct cftype *cft) |
| 6632 | { |
| 6633 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
| 6634 | |
| 6635 | return (u64)memcg->memory.watermark * PAGE_SIZE; |
| 6636 | } |
| 6637 | |
Roman Gushchin | bf8d5d5 | 2018-06-07 17:07:46 -0700 | [diff] [blame] | 6638 | static int memory_min_show(struct seq_file *m, void *v) |
| 6639 | { |
Chris Down | 677dc97 | 2019-03-05 15:45:55 -0800 | [diff] [blame] | 6640 | return seq_puts_memcg_tunable(m, |
| 6641 | READ_ONCE(mem_cgroup_from_seq(m)->memory.min)); |
Roman Gushchin | bf8d5d5 | 2018-06-07 17:07:46 -0700 | [diff] [blame] | 6642 | } |
| 6643 | |
| 6644 | static ssize_t memory_min_write(struct kernfs_open_file *of, |
| 6645 | char *buf, size_t nbytes, loff_t off) |
| 6646 | { |
| 6647 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
| 6648 | unsigned long min; |
| 6649 | int err; |
| 6650 | |
| 6651 | buf = strstrip(buf); |
| 6652 | err = page_counter_memparse(buf, "max", &min); |
| 6653 | if (err) |
| 6654 | return err; |
| 6655 | |
| 6656 | page_counter_set_min(&memcg->memory, min); |
| 6657 | |
| 6658 | return nbytes; |
| 6659 | } |
| 6660 | |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6661 | static int memory_low_show(struct seq_file *m, void *v) |
| 6662 | { |
Chris Down | 677dc97 | 2019-03-05 15:45:55 -0800 | [diff] [blame] | 6663 | return seq_puts_memcg_tunable(m, |
| 6664 | READ_ONCE(mem_cgroup_from_seq(m)->memory.low)); |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6665 | } |
| 6666 | |
| 6667 | static ssize_t memory_low_write(struct kernfs_open_file *of, |
| 6668 | char *buf, size_t nbytes, loff_t off) |
| 6669 | { |
| 6670 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
| 6671 | unsigned long low; |
| 6672 | int err; |
| 6673 | |
| 6674 | buf = strstrip(buf); |
Johannes Weiner | d297369 | 2015-02-27 15:52:04 -0800 | [diff] [blame] | 6675 | err = page_counter_memparse(buf, "max", &low); |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6676 | if (err) |
| 6677 | return err; |
| 6678 | |
Roman Gushchin | 2306715 | 2018-06-07 17:06:22 -0700 | [diff] [blame] | 6679 | page_counter_set_low(&memcg->memory, low); |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6680 | |
| 6681 | return nbytes; |
| 6682 | } |
| 6683 | |
| 6684 | static int memory_high_show(struct seq_file *m, void *v) |
| 6685 | { |
Jakub Kicinski | d1663a9 | 2020-06-01 21:49:49 -0700 | [diff] [blame] | 6686 | return seq_puts_memcg_tunable(m, |
| 6687 | READ_ONCE(mem_cgroup_from_seq(m)->memory.high)); |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6688 | } |
| 6689 | |
| 6690 | static ssize_t memory_high_write(struct kernfs_open_file *of, |
| 6691 | char *buf, size_t nbytes, loff_t off) |
| 6692 | { |
| 6693 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
Chris Down | d977aa9 | 2020-08-06 23:21:58 -0700 | [diff] [blame] | 6694 | unsigned int nr_retries = MAX_RECLAIM_RETRIES; |
Johannes Weiner | 8c8c383 | 2019-11-30 17:50:09 -0800 | [diff] [blame] | 6695 | bool drained = false; |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6696 | unsigned long high; |
| 6697 | int err; |
| 6698 | |
| 6699 | buf = strstrip(buf); |
Johannes Weiner | d297369 | 2015-02-27 15:52:04 -0800 | [diff] [blame] | 6700 | err = page_counter_memparse(buf, "max", &high); |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6701 | if (err) |
| 6702 | return err; |
| 6703 | |
Johannes Weiner | e82553c | 2021-02-09 13:42:28 -0800 | [diff] [blame] | 6704 | page_counter_set_high(&memcg->memory, high); |
| 6705 | |
Johannes Weiner | 8c8c383 | 2019-11-30 17:50:09 -0800 | [diff] [blame] | 6706 | for (;;) { |
| 6707 | unsigned long nr_pages = page_counter_read(&memcg->memory); |
| 6708 | unsigned long reclaimed; |
Johannes Weiner | 588083b | 2016-03-17 14:20:25 -0700 | [diff] [blame] | 6709 | |
Johannes Weiner | 8c8c383 | 2019-11-30 17:50:09 -0800 | [diff] [blame] | 6710 | if (nr_pages <= high) |
| 6711 | break; |
| 6712 | |
| 6713 | if (signal_pending(current)) |
| 6714 | break; |
| 6715 | |
| 6716 | if (!drained) { |
| 6717 | drain_all_stock(memcg); |
| 6718 | drained = true; |
| 6719 | continue; |
| 6720 | } |
| 6721 | |
| 6722 | reclaimed = try_to_free_mem_cgroup_pages(memcg, nr_pages - high, |
Michal Hocko | 55ab834 | 2022-12-16 10:46:33 +0100 | [diff] [blame] | 6723 | GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP); |
Johannes Weiner | 8c8c383 | 2019-11-30 17:50:09 -0800 | [diff] [blame] | 6724 | |
| 6725 | if (!reclaimed && !nr_retries--) |
| 6726 | break; |
| 6727 | } |
| 6728 | |
Johannes Weiner | 19ce33a | 2020-08-06 23:22:12 -0700 | [diff] [blame] | 6729 | memcg_wb_domain_size_changed(memcg); |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6730 | return nbytes; |
| 6731 | } |
| 6732 | |
| 6733 | static int memory_max_show(struct seq_file *m, void *v) |
| 6734 | { |
Chris Down | 677dc97 | 2019-03-05 15:45:55 -0800 | [diff] [blame] | 6735 | return seq_puts_memcg_tunable(m, |
| 6736 | READ_ONCE(mem_cgroup_from_seq(m)->memory.max)); |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6737 | } |
| 6738 | |
| 6739 | static ssize_t memory_max_write(struct kernfs_open_file *of, |
| 6740 | char *buf, size_t nbytes, loff_t off) |
| 6741 | { |
| 6742 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
Chris Down | d977aa9 | 2020-08-06 23:21:58 -0700 | [diff] [blame] | 6743 | unsigned int nr_reclaims = MAX_RECLAIM_RETRIES; |
Johannes Weiner | b6e6edc | 2016-03-17 14:20:28 -0700 | [diff] [blame] | 6744 | bool drained = false; |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6745 | unsigned long max; |
| 6746 | int err; |
| 6747 | |
| 6748 | buf = strstrip(buf); |
Johannes Weiner | d297369 | 2015-02-27 15:52:04 -0800 | [diff] [blame] | 6749 | err = page_counter_memparse(buf, "max", &max); |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6750 | if (err) |
| 6751 | return err; |
| 6752 | |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 6753 | xchg(&memcg->memory.max, max); |
Johannes Weiner | b6e6edc | 2016-03-17 14:20:28 -0700 | [diff] [blame] | 6754 | |
| 6755 | for (;;) { |
| 6756 | unsigned long nr_pages = page_counter_read(&memcg->memory); |
| 6757 | |
| 6758 | if (nr_pages <= max) |
| 6759 | break; |
| 6760 | |
Johannes Weiner | 7249c9f | 2019-11-30 17:50:06 -0800 | [diff] [blame] | 6761 | if (signal_pending(current)) |
Johannes Weiner | b6e6edc | 2016-03-17 14:20:28 -0700 | [diff] [blame] | 6762 | break; |
Johannes Weiner | b6e6edc | 2016-03-17 14:20:28 -0700 | [diff] [blame] | 6763 | |
| 6764 | if (!drained) { |
| 6765 | drain_all_stock(memcg); |
| 6766 | drained = true; |
| 6767 | continue; |
| 6768 | } |
| 6769 | |
| 6770 | if (nr_reclaims) { |
| 6771 | if (!try_to_free_mem_cgroup_pages(memcg, nr_pages - max, |
Michal Hocko | 55ab834 | 2022-12-16 10:46:33 +0100 | [diff] [blame] | 6772 | GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP)) |
Johannes Weiner | b6e6edc | 2016-03-17 14:20:28 -0700 | [diff] [blame] | 6773 | nr_reclaims--; |
| 6774 | continue; |
| 6775 | } |
| 6776 | |
Johannes Weiner | e27be24 | 2018-04-10 16:29:45 -0700 | [diff] [blame] | 6777 | memcg_memory_event(memcg, MEMCG_OOM); |
Johannes Weiner | b6e6edc | 2016-03-17 14:20:28 -0700 | [diff] [blame] | 6778 | if (!mem_cgroup_out_of_memory(memcg, GFP_KERNEL, 0)) |
| 6779 | break; |
| 6780 | } |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6781 | |
Tejun Heo | 2529bb3 | 2015-05-22 18:23:34 -0400 | [diff] [blame] | 6782 | memcg_wb_domain_size_changed(memcg); |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6783 | return nbytes; |
| 6784 | } |
| 6785 | |
Shakeel Butt | 1e577f9 | 2019-07-11 20:55:55 -0700 | [diff] [blame] | 6786 | static void __memory_events_show(struct seq_file *m, atomic_long_t *events) |
| 6787 | { |
| 6788 | seq_printf(m, "low %lu\n", atomic_long_read(&events[MEMCG_LOW])); |
| 6789 | seq_printf(m, "high %lu\n", atomic_long_read(&events[MEMCG_HIGH])); |
| 6790 | seq_printf(m, "max %lu\n", atomic_long_read(&events[MEMCG_MAX])); |
| 6791 | seq_printf(m, "oom %lu\n", atomic_long_read(&events[MEMCG_OOM])); |
| 6792 | seq_printf(m, "oom_kill %lu\n", |
| 6793 | atomic_long_read(&events[MEMCG_OOM_KILL])); |
Dan Schatzberg | b6bf9ab | 2022-01-14 14:05:35 -0800 | [diff] [blame] | 6794 | seq_printf(m, "oom_group_kill %lu\n", |
| 6795 | atomic_long_read(&events[MEMCG_OOM_GROUP_KILL])); |
Shakeel Butt | 1e577f9 | 2019-07-11 20:55:55 -0700 | [diff] [blame] | 6796 | } |
| 6797 | |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6798 | static int memory_events_show(struct seq_file *m, void *v) |
| 6799 | { |
Chris Down | aa9694b | 2019-03-05 15:45:52 -0800 | [diff] [blame] | 6800 | struct mem_cgroup *memcg = mem_cgroup_from_seq(m); |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6801 | |
Shakeel Butt | 1e577f9 | 2019-07-11 20:55:55 -0700 | [diff] [blame] | 6802 | __memory_events_show(m, memcg->memory_events); |
| 6803 | return 0; |
| 6804 | } |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6805 | |
Shakeel Butt | 1e577f9 | 2019-07-11 20:55:55 -0700 | [diff] [blame] | 6806 | static int memory_events_local_show(struct seq_file *m, void *v) |
| 6807 | { |
| 6808 | struct mem_cgroup *memcg = mem_cgroup_from_seq(m); |
| 6809 | |
| 6810 | __memory_events_show(m, memcg->memory_events_local); |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6811 | return 0; |
| 6812 | } |
| 6813 | |
Johannes Weiner | 587d9f7 | 2016-01-20 15:03:19 -0800 | [diff] [blame] | 6814 | static int memory_stat_show(struct seq_file *m, void *v) |
| 6815 | { |
Chris Down | aa9694b | 2019-03-05 15:45:52 -0800 | [diff] [blame] | 6816 | struct mem_cgroup *memcg = mem_cgroup_from_seq(m); |
Tetsuo Handa | 68aaee1 | 2022-07-22 19:45:39 +0900 | [diff] [blame] | 6817 | char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Yosry Ahmed | 5b42360 | 2023-04-28 13:24:05 +0000 | [diff] [blame] | 6818 | struct seq_buf s; |
Johannes Weiner | 587d9f7 | 2016-01-20 15:03:19 -0800 | [diff] [blame] | 6819 | |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 6820 | if (!buf) |
| 6821 | return -ENOMEM; |
Yosry Ahmed | 5b42360 | 2023-04-28 13:24:05 +0000 | [diff] [blame] | 6822 | seq_buf_init(&s, buf, PAGE_SIZE); |
| 6823 | memory_stat_format(memcg, &s); |
Johannes Weiner | c8713d0 | 2019-07-11 20:55:59 -0700 | [diff] [blame] | 6824 | seq_puts(m, buf); |
| 6825 | kfree(buf); |
Johannes Weiner | 587d9f7 | 2016-01-20 15:03:19 -0800 | [diff] [blame] | 6826 | return 0; |
| 6827 | } |
| 6828 | |
Muchun Song | 5f9a4f4 | 2020-10-13 16:52:59 -0700 | [diff] [blame] | 6829 | #ifdef CONFIG_NUMA |
Muchun Song | fff66b7 | 2021-02-24 12:03:43 -0800 | [diff] [blame] | 6830 | static inline unsigned long lruvec_page_state_output(struct lruvec *lruvec, |
| 6831 | int item) |
| 6832 | { |
Yosry Ahmed | ff841a0 | 2023-09-22 17:57:39 +0000 | [diff] [blame] | 6833 | return lruvec_page_state(lruvec, item) * |
| 6834 | memcg_page_state_output_unit(item); |
Muchun Song | fff66b7 | 2021-02-24 12:03:43 -0800 | [diff] [blame] | 6835 | } |
| 6836 | |
Muchun Song | 5f9a4f4 | 2020-10-13 16:52:59 -0700 | [diff] [blame] | 6837 | static int memory_numa_stat_show(struct seq_file *m, void *v) |
| 6838 | { |
| 6839 | int i; |
| 6840 | struct mem_cgroup *memcg = mem_cgroup_from_seq(m); |
| 6841 | |
Shakeel Butt | fd25a9e | 2021-11-05 13:37:34 -0700 | [diff] [blame] | 6842 | mem_cgroup_flush_stats(); |
Shakeel Butt | 7e1c0d6 | 2021-09-02 14:55:00 -0700 | [diff] [blame] | 6843 | |
Muchun Song | 5f9a4f4 | 2020-10-13 16:52:59 -0700 | [diff] [blame] | 6844 | for (i = 0; i < ARRAY_SIZE(memory_stats); i++) { |
| 6845 | int nid; |
| 6846 | |
| 6847 | if (memory_stats[i].idx >= NR_VM_NODE_STAT_ITEMS) |
| 6848 | continue; |
| 6849 | |
| 6850 | seq_printf(m, "%s", memory_stats[i].name); |
| 6851 | for_each_node_state(nid, N_MEMORY) { |
| 6852 | u64 size; |
| 6853 | struct lruvec *lruvec; |
| 6854 | |
| 6855 | lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid)); |
Muchun Song | fff66b7 | 2021-02-24 12:03:43 -0800 | [diff] [blame] | 6856 | size = lruvec_page_state_output(lruvec, |
| 6857 | memory_stats[i].idx); |
Muchun Song | 5f9a4f4 | 2020-10-13 16:52:59 -0700 | [diff] [blame] | 6858 | seq_printf(m, " N%d=%llu", nid, size); |
| 6859 | } |
| 6860 | seq_putc(m, '\n'); |
| 6861 | } |
| 6862 | |
| 6863 | return 0; |
| 6864 | } |
| 6865 | #endif |
| 6866 | |
Roman Gushchin | 3d8b38e | 2018-08-21 21:53:54 -0700 | [diff] [blame] | 6867 | static int memory_oom_group_show(struct seq_file *m, void *v) |
| 6868 | { |
Chris Down | aa9694b | 2019-03-05 15:45:52 -0800 | [diff] [blame] | 6869 | struct mem_cgroup *memcg = mem_cgroup_from_seq(m); |
Roman Gushchin | 3d8b38e | 2018-08-21 21:53:54 -0700 | [diff] [blame] | 6870 | |
Yue Zhao | eaf7b66 | 2023-03-06 23:41:35 +0800 | [diff] [blame] | 6871 | seq_printf(m, "%d\n", READ_ONCE(memcg->oom_group)); |
Roman Gushchin | 3d8b38e | 2018-08-21 21:53:54 -0700 | [diff] [blame] | 6872 | |
| 6873 | return 0; |
| 6874 | } |
| 6875 | |
| 6876 | static ssize_t memory_oom_group_write(struct kernfs_open_file *of, |
| 6877 | char *buf, size_t nbytes, loff_t off) |
| 6878 | { |
| 6879 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
| 6880 | int ret, oom_group; |
| 6881 | |
| 6882 | buf = strstrip(buf); |
| 6883 | if (!buf) |
| 6884 | return -EINVAL; |
| 6885 | |
| 6886 | ret = kstrtoint(buf, 0, &oom_group); |
| 6887 | if (ret) |
| 6888 | return ret; |
| 6889 | |
| 6890 | if (oom_group != 0 && oom_group != 1) |
| 6891 | return -EINVAL; |
| 6892 | |
Yue Zhao | eaf7b66 | 2023-03-06 23:41:35 +0800 | [diff] [blame] | 6893 | WRITE_ONCE(memcg->oom_group, oom_group); |
Roman Gushchin | 3d8b38e | 2018-08-21 21:53:54 -0700 | [diff] [blame] | 6894 | |
| 6895 | return nbytes; |
| 6896 | } |
| 6897 | |
Shakeel Butt | 9496838 | 2022-04-29 14:36:59 -0700 | [diff] [blame] | 6898 | static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf, |
| 6899 | size_t nbytes, loff_t off) |
| 6900 | { |
| 6901 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
| 6902 | unsigned int nr_retries = MAX_RECLAIM_RETRIES; |
| 6903 | unsigned long nr_to_reclaim, nr_reclaimed = 0; |
Michal Hocko | 55ab834 | 2022-12-16 10:46:33 +0100 | [diff] [blame] | 6904 | unsigned int reclaim_options; |
| 6905 | int err; |
Shakeel Butt | 9496838 | 2022-04-29 14:36:59 -0700 | [diff] [blame] | 6906 | |
| 6907 | buf = strstrip(buf); |
Michal Hocko | 55ab834 | 2022-12-16 10:46:33 +0100 | [diff] [blame] | 6908 | err = page_counter_memparse(buf, "", &nr_to_reclaim); |
| 6909 | if (err) |
| 6910 | return err; |
Shakeel Butt | 9496838 | 2022-04-29 14:36:59 -0700 | [diff] [blame] | 6911 | |
Michal Hocko | 55ab834 | 2022-12-16 10:46:33 +0100 | [diff] [blame] | 6912 | reclaim_options = MEMCG_RECLAIM_MAY_SWAP | MEMCG_RECLAIM_PROACTIVE; |
Shakeel Butt | 9496838 | 2022-04-29 14:36:59 -0700 | [diff] [blame] | 6913 | while (nr_reclaimed < nr_to_reclaim) { |
| 6914 | unsigned long reclaimed; |
| 6915 | |
| 6916 | if (signal_pending(current)) |
| 6917 | return -EINTR; |
| 6918 | |
| 6919 | /* |
| 6920 | * This is the final attempt, drain percpu lru caches in the |
| 6921 | * hope of introducing more evictable pages for |
| 6922 | * try_to_free_mem_cgroup_pages(). |
| 6923 | */ |
| 6924 | if (!nr_retries) |
| 6925 | lru_add_drain_all(); |
| 6926 | |
| 6927 | reclaimed = try_to_free_mem_cgroup_pages(memcg, |
Efly Young | 0388536 | 2023-07-21 09:41:16 +0800 | [diff] [blame] | 6928 | min(nr_to_reclaim - nr_reclaimed, SWAP_CLUSTER_MAX), |
| 6929 | GFP_KERNEL, reclaim_options); |
Shakeel Butt | 9496838 | 2022-04-29 14:36:59 -0700 | [diff] [blame] | 6930 | |
| 6931 | if (!reclaimed && !nr_retries--) |
| 6932 | return -EAGAIN; |
| 6933 | |
| 6934 | nr_reclaimed += reclaimed; |
| 6935 | } |
| 6936 | |
| 6937 | return nbytes; |
| 6938 | } |
| 6939 | |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6940 | static struct cftype memory_files[] = { |
| 6941 | { |
| 6942 | .name = "current", |
Johannes Weiner | f5fc3c5d | 2015-11-05 18:50:23 -0800 | [diff] [blame] | 6943 | .flags = CFTYPE_NOT_ON_ROOT, |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6944 | .read_u64 = memory_current_read, |
| 6945 | }, |
| 6946 | { |
Ganesan Rajagopal | 8e20d4b | 2022-05-13 16:48:57 -0700 | [diff] [blame] | 6947 | .name = "peak", |
| 6948 | .flags = CFTYPE_NOT_ON_ROOT, |
| 6949 | .read_u64 = memory_peak_read, |
| 6950 | }, |
| 6951 | { |
Roman Gushchin | bf8d5d5 | 2018-06-07 17:07:46 -0700 | [diff] [blame] | 6952 | .name = "min", |
| 6953 | .flags = CFTYPE_NOT_ON_ROOT, |
| 6954 | .seq_show = memory_min_show, |
| 6955 | .write = memory_min_write, |
| 6956 | }, |
| 6957 | { |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6958 | .name = "low", |
| 6959 | .flags = CFTYPE_NOT_ON_ROOT, |
| 6960 | .seq_show = memory_low_show, |
| 6961 | .write = memory_low_write, |
| 6962 | }, |
| 6963 | { |
| 6964 | .name = "high", |
| 6965 | .flags = CFTYPE_NOT_ON_ROOT, |
| 6966 | .seq_show = memory_high_show, |
| 6967 | .write = memory_high_write, |
| 6968 | }, |
| 6969 | { |
| 6970 | .name = "max", |
| 6971 | .flags = CFTYPE_NOT_ON_ROOT, |
| 6972 | .seq_show = memory_max_show, |
| 6973 | .write = memory_max_write, |
| 6974 | }, |
| 6975 | { |
| 6976 | .name = "events", |
| 6977 | .flags = CFTYPE_NOT_ON_ROOT, |
Tejun Heo | 472912a | 2015-09-18 18:01:59 -0400 | [diff] [blame] | 6978 | .file_offset = offsetof(struct mem_cgroup, events_file), |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 6979 | .seq_show = memory_events_show, |
| 6980 | }, |
Johannes Weiner | 587d9f7 | 2016-01-20 15:03:19 -0800 | [diff] [blame] | 6981 | { |
Shakeel Butt | 1e577f9 | 2019-07-11 20:55:55 -0700 | [diff] [blame] | 6982 | .name = "events.local", |
| 6983 | .flags = CFTYPE_NOT_ON_ROOT, |
| 6984 | .file_offset = offsetof(struct mem_cgroup, events_local_file), |
| 6985 | .seq_show = memory_events_local_show, |
| 6986 | }, |
| 6987 | { |
Johannes Weiner | 587d9f7 | 2016-01-20 15:03:19 -0800 | [diff] [blame] | 6988 | .name = "stat", |
Johannes Weiner | 587d9f7 | 2016-01-20 15:03:19 -0800 | [diff] [blame] | 6989 | .seq_show = memory_stat_show, |
| 6990 | }, |
Muchun Song | 5f9a4f4 | 2020-10-13 16:52:59 -0700 | [diff] [blame] | 6991 | #ifdef CONFIG_NUMA |
| 6992 | { |
| 6993 | .name = "numa_stat", |
| 6994 | .seq_show = memory_numa_stat_show, |
| 6995 | }, |
| 6996 | #endif |
Roman Gushchin | 3d8b38e | 2018-08-21 21:53:54 -0700 | [diff] [blame] | 6997 | { |
| 6998 | .name = "oom.group", |
| 6999 | .flags = CFTYPE_NOT_ON_ROOT | CFTYPE_NS_DELEGATABLE, |
| 7000 | .seq_show = memory_oom_group_show, |
| 7001 | .write = memory_oom_group_write, |
| 7002 | }, |
Shakeel Butt | 9496838 | 2022-04-29 14:36:59 -0700 | [diff] [blame] | 7003 | { |
| 7004 | .name = "reclaim", |
| 7005 | .flags = CFTYPE_NS_DELEGATABLE, |
| 7006 | .write = memory_reclaim, |
| 7007 | }, |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 7008 | { } /* terminate */ |
| 7009 | }; |
| 7010 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 7011 | struct cgroup_subsys memory_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 7012 | .css_alloc = mem_cgroup_css_alloc, |
Glauber Costa | d142e3e | 2013-02-22 16:34:52 -0800 | [diff] [blame] | 7013 | .css_online = mem_cgroup_css_online, |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 7014 | .css_offline = mem_cgroup_css_offline, |
Vladimir Davydov | 6df3868 | 2015-12-29 14:54:10 -0800 | [diff] [blame] | 7015 | .css_released = mem_cgroup_css_released, |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 7016 | .css_free = mem_cgroup_css_free, |
Tejun Heo | 1ced953b | 2014-07-08 18:02:57 -0400 | [diff] [blame] | 7017 | .css_reset = mem_cgroup_css_reset, |
Johannes Weiner | 2d146aa | 2021-04-29 22:56:26 -0700 | [diff] [blame] | 7018 | .css_rstat_flush = mem_cgroup_css_rstat_flush, |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 7019 | .can_attach = mem_cgroup_can_attach, |
Roman Gushchin | 1aacbd3 | 2023-10-19 15:53:42 -0700 | [diff] [blame] | 7020 | #if defined(CONFIG_LRU_GEN) || defined(CONFIG_MEMCG_KMEM) |
Yu Zhao | bd74fda | 2022-09-18 02:00:05 -0600 | [diff] [blame] | 7021 | .attach = mem_cgroup_attach, |
Roman Gushchin | 1aacbd3 | 2023-10-19 15:53:42 -0700 | [diff] [blame] | 7022 | #endif |
Daisuke Nishimura | 7dc74be | 2010-03-10 15:22:13 -0800 | [diff] [blame] | 7023 | .cancel_attach = mem_cgroup_cancel_attach, |
Tejun Heo | 264a0ae | 2016-04-21 19:09:02 -0400 | [diff] [blame] | 7024 | .post_attach = mem_cgroup_move_task, |
Roman Gushchin | 1aacbd3 | 2023-10-19 15:53:42 -0700 | [diff] [blame] | 7025 | #ifdef CONFIG_MEMCG_KMEM |
| 7026 | .fork = mem_cgroup_fork, |
| 7027 | .exit = mem_cgroup_exit, |
| 7028 | #endif |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 7029 | .dfl_cftypes = memory_files, |
| 7030 | .legacy_cftypes = mem_cgroup_legacy_files, |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 7031 | .early_init = 0, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 7032 | }; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 7033 | |
Johannes Weiner | bc50bcc | 2020-04-01 21:07:03 -0700 | [diff] [blame] | 7034 | /* |
| 7035 | * This function calculates an individual cgroup's effective |
| 7036 | * protection which is derived from its own memory.min/low, its |
| 7037 | * parent's and siblings' settings, as well as the actual memory |
| 7038 | * distribution in the tree. |
| 7039 | * |
| 7040 | * The following rules apply to the effective protection values: |
| 7041 | * |
| 7042 | * 1. At the first level of reclaim, effective protection is equal to |
| 7043 | * the declared protection in memory.min and memory.low. |
| 7044 | * |
| 7045 | * 2. To enable safe delegation of the protection configuration, at |
| 7046 | * subsequent levels the effective protection is capped to the |
| 7047 | * parent's effective protection. |
| 7048 | * |
| 7049 | * 3. To make complex and dynamic subtrees easier to configure, the |
| 7050 | * user is allowed to overcommit the declared protection at a given |
| 7051 | * level. If that is the case, the parent's effective protection is |
| 7052 | * distributed to the children in proportion to how much protection |
| 7053 | * they have declared and how much of it they are utilizing. |
| 7054 | * |
| 7055 | * This makes distribution proportional, but also work-conserving: |
| 7056 | * if one cgroup claims much more protection than it uses memory, |
| 7057 | * the unused remainder is available to its siblings. |
| 7058 | * |
| 7059 | * 4. Conversely, when the declared protection is undercommitted at a |
| 7060 | * given level, the distribution of the larger parental protection |
| 7061 | * budget is NOT proportional. A cgroup's protection from a sibling |
| 7062 | * is capped to its own memory.min/low setting. |
| 7063 | * |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 7064 | * 5. However, to allow protecting recursive subtrees from each other |
| 7065 | * without having to declare each individual cgroup's fixed share |
| 7066 | * of the ancestor's claim to protection, any unutilized - |
| 7067 | * "floating" - protection from up the tree is distributed in |
| 7068 | * proportion to each cgroup's *usage*. This makes the protection |
| 7069 | * neutral wrt sibling cgroups and lets them compete freely over |
| 7070 | * the shared parental protection budget, but it protects the |
| 7071 | * subtree as a whole from neighboring subtrees. |
| 7072 | * |
| 7073 | * Note that 4. and 5. are not in conflict: 4. is about protecting |
| 7074 | * against immediate siblings whereas 5. is about protecting against |
| 7075 | * neighboring subtrees. |
Johannes Weiner | bc50bcc | 2020-04-01 21:07:03 -0700 | [diff] [blame] | 7076 | */ |
| 7077 | static unsigned long effective_protection(unsigned long usage, |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 7078 | unsigned long parent_usage, |
Johannes Weiner | bc50bcc | 2020-04-01 21:07:03 -0700 | [diff] [blame] | 7079 | unsigned long setting, |
| 7080 | unsigned long parent_effective, |
| 7081 | unsigned long siblings_protected) |
| 7082 | { |
| 7083 | unsigned long protected; |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 7084 | unsigned long ep; |
Johannes Weiner | bc50bcc | 2020-04-01 21:07:03 -0700 | [diff] [blame] | 7085 | |
| 7086 | protected = min(usage, setting); |
| 7087 | /* |
| 7088 | * If all cgroups at this level combined claim and use more |
Haifeng Xu | 08e0f49 | 2023-05-22 09:52:32 +0000 | [diff] [blame] | 7089 | * protection than what the parent affords them, distribute |
Johannes Weiner | bc50bcc | 2020-04-01 21:07:03 -0700 | [diff] [blame] | 7090 | * shares in proportion to utilization. |
| 7091 | * |
| 7092 | * We are using actual utilization rather than the statically |
| 7093 | * claimed protection in order to be work-conserving: claimed |
| 7094 | * but unused protection is available to siblings that would |
| 7095 | * otherwise get a smaller chunk than what they claimed. |
| 7096 | */ |
| 7097 | if (siblings_protected > parent_effective) |
| 7098 | return protected * parent_effective / siblings_protected; |
| 7099 | |
| 7100 | /* |
| 7101 | * Ok, utilized protection of all children is within what the |
| 7102 | * parent affords them, so we know whatever this child claims |
| 7103 | * and utilizes is effectively protected. |
| 7104 | * |
| 7105 | * If there is unprotected usage beyond this value, reclaim |
| 7106 | * will apply pressure in proportion to that amount. |
| 7107 | * |
| 7108 | * If there is unutilized protection, the cgroup will be fully |
| 7109 | * shielded from reclaim, but we do return a smaller value for |
| 7110 | * protection than what the group could enjoy in theory. This |
| 7111 | * is okay. With the overcommit distribution above, effective |
| 7112 | * protection is always dependent on how memory is actually |
| 7113 | * consumed among the siblings anyway. |
| 7114 | */ |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 7115 | ep = protected; |
| 7116 | |
| 7117 | /* |
| 7118 | * If the children aren't claiming (all of) the protection |
| 7119 | * afforded to them by the parent, distribute the remainder in |
| 7120 | * proportion to the (unprotected) memory of each cgroup. That |
| 7121 | * way, cgroups that aren't explicitly prioritized wrt each |
| 7122 | * other compete freely over the allowance, but they are |
| 7123 | * collectively protected from neighboring trees. |
| 7124 | * |
| 7125 | * We're using unprotected memory for the weight so that if |
| 7126 | * some cgroups DO claim explicit protection, we don't protect |
| 7127 | * the same bytes twice. |
Johannes Weiner | cd324ed | 2020-06-25 20:30:16 -0700 | [diff] [blame] | 7128 | * |
| 7129 | * Check both usage and parent_usage against the respective |
| 7130 | * protected values. One should imply the other, but they |
| 7131 | * aren't read atomically - make sure the division is sane. |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 7132 | */ |
| 7133 | if (!(cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT)) |
| 7134 | return ep; |
Johannes Weiner | cd324ed | 2020-06-25 20:30:16 -0700 | [diff] [blame] | 7135 | if (parent_effective > siblings_protected && |
| 7136 | parent_usage > siblings_protected && |
| 7137 | usage > protected) { |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 7138 | unsigned long unclaimed; |
| 7139 | |
| 7140 | unclaimed = parent_effective - siblings_protected; |
| 7141 | unclaimed *= usage - protected; |
| 7142 | unclaimed /= parent_usage - siblings_protected; |
| 7143 | |
| 7144 | ep += unclaimed; |
| 7145 | } |
| 7146 | |
| 7147 | return ep; |
Johannes Weiner | bc50bcc | 2020-04-01 21:07:03 -0700 | [diff] [blame] | 7148 | } |
| 7149 | |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 7150 | /** |
Mel Gorman | 05395718 | 2021-06-30 18:53:32 -0700 | [diff] [blame] | 7151 | * mem_cgroup_calculate_protection - check if memory consumption is in the normal range |
Sean Christopherson | 34c8105 | 2017-07-10 15:48:05 -0700 | [diff] [blame] | 7152 | * @root: the top ancestor of the sub-tree being checked |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 7153 | * @memcg: the memory cgroup to check |
| 7154 | * |
Roman Gushchin | 2306715 | 2018-06-07 17:06:22 -0700 | [diff] [blame] | 7155 | * WARNING: This function is not stateless! It can only be used as part |
| 7156 | * of a top-down tree iteration, not for isolated queries. |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 7157 | */ |
Chris Down | 45c7f7e | 2020-08-06 23:22:05 -0700 | [diff] [blame] | 7158 | void mem_cgroup_calculate_protection(struct mem_cgroup *root, |
| 7159 | struct mem_cgroup *memcg) |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 7160 | { |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 7161 | unsigned long usage, parent_usage; |
Roman Gushchin | 2306715 | 2018-06-07 17:06:22 -0700 | [diff] [blame] | 7162 | struct mem_cgroup *parent; |
| 7163 | |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 7164 | if (mem_cgroup_disabled()) |
Chris Down | 45c7f7e | 2020-08-06 23:22:05 -0700 | [diff] [blame] | 7165 | return; |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 7166 | |
Sean Christopherson | 34c8105 | 2017-07-10 15:48:05 -0700 | [diff] [blame] | 7167 | if (!root) |
| 7168 | root = root_mem_cgroup; |
Yafang Shao | 22f7496 | 2020-08-06 23:22:01 -0700 | [diff] [blame] | 7169 | |
| 7170 | /* |
| 7171 | * Effective values of the reclaim targets are ignored so they |
| 7172 | * can be stale. Have a look at mem_cgroup_protection for more |
| 7173 | * details. |
| 7174 | * TODO: calculation should be more robust so that we do not need |
| 7175 | * that special casing. |
| 7176 | */ |
Sean Christopherson | 34c8105 | 2017-07-10 15:48:05 -0700 | [diff] [blame] | 7177 | if (memcg == root) |
Chris Down | 45c7f7e | 2020-08-06 23:22:05 -0700 | [diff] [blame] | 7178 | return; |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 7179 | |
Roman Gushchin | 2306715 | 2018-06-07 17:06:22 -0700 | [diff] [blame] | 7180 | usage = page_counter_read(&memcg->memory); |
Roman Gushchin | bf8d5d5 | 2018-06-07 17:07:46 -0700 | [diff] [blame] | 7181 | if (!usage) |
Chris Down | 45c7f7e | 2020-08-06 23:22:05 -0700 | [diff] [blame] | 7182 | return; |
Sean Christopherson | 34c8105 | 2017-07-10 15:48:05 -0700 | [diff] [blame] | 7183 | |
Roman Gushchin | bf8d5d5 | 2018-06-07 17:07:46 -0700 | [diff] [blame] | 7184 | parent = parent_mem_cgroup(memcg); |
Roman Gushchin | df2a419 | 2018-06-14 15:26:17 -0700 | [diff] [blame] | 7185 | |
Johannes Weiner | bc50bcc | 2020-04-01 21:07:03 -0700 | [diff] [blame] | 7186 | if (parent == root) { |
Chris Down | c3d5320 | 2020-04-01 21:07:27 -0700 | [diff] [blame] | 7187 | memcg->memory.emin = READ_ONCE(memcg->memory.min); |
Chris Down | 03960e3 | 2020-06-25 20:30:22 -0700 | [diff] [blame] | 7188 | memcg->memory.elow = READ_ONCE(memcg->memory.low); |
Chris Down | 45c7f7e | 2020-08-06 23:22:05 -0700 | [diff] [blame] | 7189 | return; |
Roman Gushchin | bf8d5d5 | 2018-06-07 17:07:46 -0700 | [diff] [blame] | 7190 | } |
| 7191 | |
Johannes Weiner | 8a931f8 | 2020-04-01 21:07:07 -0700 | [diff] [blame] | 7192 | parent_usage = page_counter_read(&parent->memory); |
| 7193 | |
Chris Down | b3a7822 | 2020-04-01 21:07:33 -0700 | [diff] [blame] | 7194 | WRITE_ONCE(memcg->memory.emin, effective_protection(usage, parent_usage, |
Chris Down | c3d5320 | 2020-04-01 21:07:27 -0700 | [diff] [blame] | 7195 | READ_ONCE(memcg->memory.min), |
| 7196 | READ_ONCE(parent->memory.emin), |
Chris Down | b3a7822 | 2020-04-01 21:07:33 -0700 | [diff] [blame] | 7197 | atomic_long_read(&parent->memory.children_min_usage))); |
Roman Gushchin | 2306715 | 2018-06-07 17:06:22 -0700 | [diff] [blame] | 7198 | |
Chris Down | b3a7822 | 2020-04-01 21:07:33 -0700 | [diff] [blame] | 7199 | WRITE_ONCE(memcg->memory.elow, effective_protection(usage, parent_usage, |
Chris Down | 03960e3 | 2020-06-25 20:30:22 -0700 | [diff] [blame] | 7200 | READ_ONCE(memcg->memory.low), |
| 7201 | READ_ONCE(parent->memory.elow), |
Chris Down | b3a7822 | 2020-04-01 21:07:33 -0700 | [diff] [blame] | 7202 | atomic_long_read(&parent->memory.children_low_usage))); |
Johannes Weiner | 241994ed | 2015-02-11 15:26:06 -0800 | [diff] [blame] | 7203 | } |
| 7204 | |
Matthew Wilcox (Oracle) | 8f425e4 | 2021-06-25 09:27:04 -0400 | [diff] [blame] | 7205 | static int charge_memcg(struct folio *folio, struct mem_cgroup *memcg, |
| 7206 | gfp_t gfp) |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 7207 | { |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 7208 | int ret; |
| 7209 | |
Nhat Pham | 4b56938 | 2023-10-06 11:46:26 -0700 | [diff] [blame] | 7210 | ret = try_charge(memcg, gfp, folio_nr_pages(folio)); |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 7211 | if (ret) |
| 7212 | goto out; |
| 7213 | |
Nhat Pham | 4b56938 | 2023-10-06 11:46:26 -0700 | [diff] [blame] | 7214 | mem_cgroup_commit_charge(folio, memcg); |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 7215 | out: |
| 7216 | return ret; |
| 7217 | } |
| 7218 | |
Matthew Wilcox (Oracle) | 8f425e4 | 2021-06-25 09:27:04 -0400 | [diff] [blame] | 7219 | int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp) |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 7220 | { |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 7221 | struct mem_cgroup *memcg; |
| 7222 | int ret; |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 7223 | |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 7224 | memcg = get_mem_cgroup_from_mm(mm); |
Matthew Wilcox (Oracle) | 8f425e4 | 2021-06-25 09:27:04 -0400 | [diff] [blame] | 7225 | ret = charge_memcg(folio, memcg, gfp); |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 7226 | css_put(&memcg->css); |
Johannes Weiner | 2d1c498 | 2020-06-03 16:02:14 -0700 | [diff] [blame] | 7227 | |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 7228 | return ret; |
| 7229 | } |
Vladimir Davydov | e993d90 | 2015-09-09 15:35:35 -0700 | [diff] [blame] | 7230 | |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 7231 | /** |
Nhat Pham | 8cba957 | 2023-10-06 11:46:28 -0700 | [diff] [blame] | 7232 | * mem_cgroup_hugetlb_try_charge - try to charge the memcg for a hugetlb folio |
| 7233 | * @memcg: memcg to charge. |
| 7234 | * @gfp: reclaim mode. |
| 7235 | * @nr_pages: number of pages to charge. |
| 7236 | * |
| 7237 | * This function is called when allocating a huge page folio to determine if |
| 7238 | * the memcg has the capacity for it. It does not commit the charge yet, |
| 7239 | * as the hugetlb folio itself has not been obtained from the hugetlb pool. |
| 7240 | * |
| 7241 | * Once we have obtained the hugetlb folio, we can call |
| 7242 | * mem_cgroup_commit_charge() to commit the charge. If we fail to obtain the |
| 7243 | * folio, we should instead call mem_cgroup_cancel_charge() to undo the effect |
| 7244 | * of try_charge(). |
| 7245 | * |
| 7246 | * Returns 0 on success. Otherwise, an error code is returned. |
| 7247 | */ |
| 7248 | int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg, gfp_t gfp, |
| 7249 | long nr_pages) |
| 7250 | { |
| 7251 | /* |
| 7252 | * If hugetlb memcg charging is not enabled, do not fail hugetlb allocation, |
| 7253 | * but do not attempt to commit charge later (or cancel on error) either. |
| 7254 | */ |
| 7255 | if (mem_cgroup_disabled() || !memcg || |
| 7256 | !cgroup_subsys_on_dfl(memory_cgrp_subsys) || |
| 7257 | !(cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING)) |
| 7258 | return -EOPNOTSUPP; |
| 7259 | |
| 7260 | if (try_charge(memcg, gfp, nr_pages)) |
| 7261 | return -ENOMEM; |
| 7262 | |
| 7263 | return 0; |
| 7264 | } |
| 7265 | |
| 7266 | /** |
Matthew Wilcox (Oracle) | 6599591 | 2022-09-02 20:46:12 +0100 | [diff] [blame] | 7267 | * mem_cgroup_swapin_charge_folio - Charge a newly allocated folio for swapin. |
| 7268 | * @folio: folio to charge. |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 7269 | * @mm: mm context of the victim |
| 7270 | * @gfp: reclaim mode |
Matthew Wilcox (Oracle) | 6599591 | 2022-09-02 20:46:12 +0100 | [diff] [blame] | 7271 | * @entry: swap entry for which the folio is allocated |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 7272 | * |
Matthew Wilcox (Oracle) | 6599591 | 2022-09-02 20:46:12 +0100 | [diff] [blame] | 7273 | * This function charges a folio allocated for swapin. Please call this before |
| 7274 | * adding the folio to the swapcache. |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 7275 | * |
| 7276 | * Returns 0 on success. Otherwise, an error code is returned. |
| 7277 | */ |
Matthew Wilcox (Oracle) | 6599591 | 2022-09-02 20:46:12 +0100 | [diff] [blame] | 7278 | int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm, |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 7279 | gfp_t gfp, swp_entry_t entry) |
| 7280 | { |
| 7281 | struct mem_cgroup *memcg; |
| 7282 | unsigned short id; |
| 7283 | int ret; |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 7284 | |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 7285 | if (mem_cgroup_disabled()) |
| 7286 | return 0; |
| 7287 | |
| 7288 | id = lookup_swap_cgroup_id(entry); |
| 7289 | rcu_read_lock(); |
| 7290 | memcg = mem_cgroup_from_id(id); |
| 7291 | if (!memcg || !css_tryget_online(&memcg->css)) |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 7292 | memcg = get_mem_cgroup_from_mm(mm); |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 7293 | rcu_read_unlock(); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 7294 | |
Matthew Wilcox (Oracle) | 8f425e4 | 2021-06-25 09:27:04 -0400 | [diff] [blame] | 7295 | ret = charge_memcg(folio, memcg, gfp); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 7296 | |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 7297 | css_put(&memcg->css); |
| 7298 | return ret; |
| 7299 | } |
Johannes Weiner | 6abb5a8 | 2014-08-08 14:19:33 -0700 | [diff] [blame] | 7300 | |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 7301 | /* |
| 7302 | * mem_cgroup_swapin_uncharge_swap - uncharge swap slot |
| 7303 | * @entry: swap entry for which the page is charged |
| 7304 | * |
| 7305 | * Call this function after successfully adding the charged page to swapcache. |
| 7306 | * |
| 7307 | * Note: This function assumes the page for which swap slot is being uncharged |
| 7308 | * is order 0 page. |
| 7309 | */ |
| 7310 | void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry) |
| 7311 | { |
Muchun Song | cae3af6 | 2021-02-24 12:04:19 -0800 | [diff] [blame] | 7312 | /* |
| 7313 | * Cgroup1's unified memory+swap counter has been charged with the |
| 7314 | * new swapcache page, finish the transfer by uncharging the swap |
| 7315 | * slot. The swap slot would also get uncharged when it dies, but |
| 7316 | * it can stick around indefinitely and we'd count the page twice |
| 7317 | * the entire time. |
| 7318 | * |
| 7319 | * Cgroup2 has separate resource counters for memory and swap, |
| 7320 | * so this is a non-issue here. Memory and swap charge lifetimes |
| 7321 | * correspond 1:1 to page and swap slot lifetimes: we charge the |
| 7322 | * page to memory here, and uncharge swap when the slot is freed. |
| 7323 | */ |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 7324 | if (!mem_cgroup_disabled() && do_memsw_account()) { |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 7325 | /* |
| 7326 | * The swap entry might not get freed for a long time, |
| 7327 | * let's not wait for it. The page already received a |
| 7328 | * memory+swap charge, drop the swap entry duplicate. |
| 7329 | */ |
Shakeel Butt | 0add0c7 | 2021-04-29 22:56:36 -0700 | [diff] [blame] | 7330 | mem_cgroup_uncharge_swap(entry, 1); |
Johannes Weiner | 00501b5 | 2014-08-08 14:19:20 -0700 | [diff] [blame] | 7331 | } |
Johannes Weiner | 3fea5a4 | 2020-06-03 16:01:41 -0700 | [diff] [blame] | 7332 | } |
| 7333 | |
Jérôme Glisse | a9d5ade | 2017-09-08 16:11:50 -0700 | [diff] [blame] | 7334 | struct uncharge_gather { |
| 7335 | struct mem_cgroup *memcg; |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 7336 | unsigned long nr_memory; |
Jérôme Glisse | a9d5ade | 2017-09-08 16:11:50 -0700 | [diff] [blame] | 7337 | unsigned long pgpgout; |
Jérôme Glisse | a9d5ade | 2017-09-08 16:11:50 -0700 | [diff] [blame] | 7338 | unsigned long nr_kmem; |
Matthew Wilcox (Oracle) | 8e88bd2 | 2021-06-25 09:05:47 -0400 | [diff] [blame] | 7339 | int nid; |
Jérôme Glisse | a9d5ade | 2017-09-08 16:11:50 -0700 | [diff] [blame] | 7340 | }; |
| 7341 | |
| 7342 | static inline void uncharge_gather_clear(struct uncharge_gather *ug) |
Johannes Weiner | 747db95 | 2014-08-08 14:19:24 -0700 | [diff] [blame] | 7343 | { |
Jérôme Glisse | a9d5ade | 2017-09-08 16:11:50 -0700 | [diff] [blame] | 7344 | memset(ug, 0, sizeof(*ug)); |
| 7345 | } |
| 7346 | |
| 7347 | static void uncharge_batch(const struct uncharge_gather *ug) |
| 7348 | { |
Johannes Weiner | 747db95 | 2014-08-08 14:19:24 -0700 | [diff] [blame] | 7349 | unsigned long flags; |
| 7350 | |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 7351 | if (ug->nr_memory) { |
| 7352 | page_counter_uncharge(&ug->memcg->memory, ug->nr_memory); |
Johannes Weiner | 7941d21 | 2016-01-14 15:21:23 -0800 | [diff] [blame] | 7353 | if (do_memsw_account()) |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 7354 | page_counter_uncharge(&ug->memcg->memsw, ug->nr_memory); |
Yosry Ahmed | a8c49af | 2022-03-22 14:40:10 -0700 | [diff] [blame] | 7355 | if (ug->nr_kmem) |
| 7356 | memcg_account_kmem(ug->memcg, -ug->nr_kmem); |
Jérôme Glisse | a9d5ade | 2017-09-08 16:11:50 -0700 | [diff] [blame] | 7357 | memcg_oom_recover(ug->memcg); |
Johannes Weiner | ce00a96 | 2014-09-05 08:43:57 -0400 | [diff] [blame] | 7358 | } |
Johannes Weiner | 747db95 | 2014-08-08 14:19:24 -0700 | [diff] [blame] | 7359 | |
| 7360 | local_irq_save(flags); |
Johannes Weiner | c9019e9 | 2018-01-31 16:16:37 -0800 | [diff] [blame] | 7361 | __count_memcg_events(ug->memcg, PGPGOUT, ug->pgpgout); |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 7362 | __this_cpu_add(ug->memcg->vmstats_percpu->nr_page_events, ug->nr_memory); |
Matthew Wilcox (Oracle) | 8e88bd2 | 2021-06-25 09:05:47 -0400 | [diff] [blame] | 7363 | memcg_check_events(ug->memcg, ug->nid); |
Johannes Weiner | 747db95 | 2014-08-08 14:19:24 -0700 | [diff] [blame] | 7364 | local_irq_restore(flags); |
Michal Hocko | f179654 | 2020-09-04 16:35:24 -0700 | [diff] [blame] | 7365 | |
Matthew Wilcox (Oracle) | c4ed6eb | 2021-06-29 21:47:12 -0400 | [diff] [blame] | 7366 | /* drop reference from uncharge_folio */ |
Michal Hocko | f179654 | 2020-09-04 16:35:24 -0700 | [diff] [blame] | 7367 | css_put(&ug->memcg->css); |
Jérôme Glisse | a9d5ade | 2017-09-08 16:11:50 -0700 | [diff] [blame] | 7368 | } |
| 7369 | |
Matthew Wilcox (Oracle) | c4ed6eb | 2021-06-29 21:47:12 -0400 | [diff] [blame] | 7370 | static void uncharge_folio(struct folio *folio, struct uncharge_gather *ug) |
Jérôme Glisse | a9d5ade | 2017-09-08 16:11:50 -0700 | [diff] [blame] | 7371 | { |
Matthew Wilcox (Oracle) | c4ed6eb | 2021-06-29 21:47:12 -0400 | [diff] [blame] | 7372 | long nr_pages; |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 7373 | struct mem_cgroup *memcg; |
| 7374 | struct obj_cgroup *objcg; |
Johannes Weiner | 9f762db | 2020-06-03 16:01:44 -0700 | [diff] [blame] | 7375 | |
Matthew Wilcox (Oracle) | c4ed6eb | 2021-06-29 21:47:12 -0400 | [diff] [blame] | 7376 | VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); |
Jérôme Glisse | a9d5ade | 2017-09-08 16:11:50 -0700 | [diff] [blame] | 7377 | |
Jérôme Glisse | a9d5ade | 2017-09-08 16:11:50 -0700 | [diff] [blame] | 7378 | /* |
| 7379 | * Nobody should be changing or seriously looking at |
Matthew Wilcox (Oracle) | c4ed6eb | 2021-06-29 21:47:12 -0400 | [diff] [blame] | 7380 | * folio memcg or objcg at this point, we have fully |
| 7381 | * exclusive access to the folio. |
Jérôme Glisse | a9d5ade | 2017-09-08 16:11:50 -0700 | [diff] [blame] | 7382 | */ |
Michal Hocko | fead2b8 | 2022-03-22 14:40:35 -0700 | [diff] [blame] | 7383 | if (folio_memcg_kmem(folio)) { |
Matthew Wilcox (Oracle) | 1b7e446 | 2021-06-28 14:59:26 -0400 | [diff] [blame] | 7384 | objcg = __folio_objcg(folio); |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 7385 | /* |
| 7386 | * This get matches the put at the end of the function and |
| 7387 | * kmem pages do not hold memcg references anymore. |
| 7388 | */ |
| 7389 | memcg = get_mem_cgroup_from_objcg(objcg); |
| 7390 | } else { |
Matthew Wilcox (Oracle) | 1b7e446 | 2021-06-28 14:59:26 -0400 | [diff] [blame] | 7391 | memcg = __folio_memcg(folio); |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 7392 | } |
Jérôme Glisse | a9d5ade | 2017-09-08 16:11:50 -0700 | [diff] [blame] | 7393 | |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 7394 | if (!memcg) |
| 7395 | return; |
| 7396 | |
| 7397 | if (ug->memcg != memcg) { |
Jérôme Glisse | a9d5ade | 2017-09-08 16:11:50 -0700 | [diff] [blame] | 7398 | if (ug->memcg) { |
| 7399 | uncharge_batch(ug); |
| 7400 | uncharge_gather_clear(ug); |
| 7401 | } |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 7402 | ug->memcg = memcg; |
Matthew Wilcox (Oracle) | c4ed6eb | 2021-06-29 21:47:12 -0400 | [diff] [blame] | 7403 | ug->nid = folio_nid(folio); |
Michal Hocko | f179654 | 2020-09-04 16:35:24 -0700 | [diff] [blame] | 7404 | |
| 7405 | /* pairs with css_put in uncharge_batch */ |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 7406 | css_get(&memcg->css); |
Jérôme Glisse | a9d5ade | 2017-09-08 16:11:50 -0700 | [diff] [blame] | 7407 | } |
| 7408 | |
Matthew Wilcox (Oracle) | c4ed6eb | 2021-06-29 21:47:12 -0400 | [diff] [blame] | 7409 | nr_pages = folio_nr_pages(folio); |
Jérôme Glisse | a9d5ade | 2017-09-08 16:11:50 -0700 | [diff] [blame] | 7410 | |
Michal Hocko | fead2b8 | 2022-03-22 14:40:35 -0700 | [diff] [blame] | 7411 | if (folio_memcg_kmem(folio)) { |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 7412 | ug->nr_memory += nr_pages; |
Johannes Weiner | 9f762db | 2020-06-03 16:01:44 -0700 | [diff] [blame] | 7413 | ug->nr_kmem += nr_pages; |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 7414 | |
Matthew Wilcox (Oracle) | c4ed6eb | 2021-06-29 21:47:12 -0400 | [diff] [blame] | 7415 | folio->memcg_data = 0; |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 7416 | obj_cgroup_put(objcg); |
| 7417 | } else { |
| 7418 | /* LRU pages aren't accounted at the root level */ |
| 7419 | if (!mem_cgroup_is_root(memcg)) |
| 7420 | ug->nr_memory += nr_pages; |
Roman Gushchin | 18b2db3 | 2020-12-01 13:58:30 -0800 | [diff] [blame] | 7421 | ug->pgpgout++; |
Jérôme Glisse | a9d5ade | 2017-09-08 16:11:50 -0700 | [diff] [blame] | 7422 | |
Matthew Wilcox (Oracle) | c4ed6eb | 2021-06-29 21:47:12 -0400 | [diff] [blame] | 7423 | folio->memcg_data = 0; |
Muchun Song | b4e0b68 | 2021-04-29 22:56:52 -0700 | [diff] [blame] | 7424 | } |
| 7425 | |
| 7426 | css_put(&memcg->css); |
Johannes Weiner | 747db95 | 2014-08-08 14:19:24 -0700 | [diff] [blame] | 7427 | } |
| 7428 | |
Matthew Wilcox (Oracle) | bbc6b70 | 2021-05-01 20:42:23 -0400 | [diff] [blame] | 7429 | void __mem_cgroup_uncharge(struct folio *folio) |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 7430 | { |
Jérôme Glisse | a9d5ade | 2017-09-08 16:11:50 -0700 | [diff] [blame] | 7431 | struct uncharge_gather ug; |
| 7432 | |
Matthew Wilcox (Oracle) | bbc6b70 | 2021-05-01 20:42:23 -0400 | [diff] [blame] | 7433 | /* Don't touch folio->lru of any random page, pre-check: */ |
| 7434 | if (!folio_memcg(folio)) |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 7435 | return; |
| 7436 | |
Jérôme Glisse | a9d5ade | 2017-09-08 16:11:50 -0700 | [diff] [blame] | 7437 | uncharge_gather_clear(&ug); |
Matthew Wilcox (Oracle) | bbc6b70 | 2021-05-01 20:42:23 -0400 | [diff] [blame] | 7438 | uncharge_folio(folio, &ug); |
Jérôme Glisse | a9d5ade | 2017-09-08 16:11:50 -0700 | [diff] [blame] | 7439 | uncharge_batch(&ug); |
Johannes Weiner | 747db95 | 2014-08-08 14:19:24 -0700 | [diff] [blame] | 7440 | } |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 7441 | |
Johannes Weiner | 747db95 | 2014-08-08 14:19:24 -0700 | [diff] [blame] | 7442 | /** |
Suren Baghdasaryan | 2c8d8f9 | 2021-09-02 14:54:50 -0700 | [diff] [blame] | 7443 | * __mem_cgroup_uncharge_list - uncharge a list of page |
Johannes Weiner | 747db95 | 2014-08-08 14:19:24 -0700 | [diff] [blame] | 7444 | * @page_list: list of pages to uncharge |
| 7445 | * |
| 7446 | * Uncharge a list of pages previously charged with |
Suren Baghdasaryan | 2c8d8f9 | 2021-09-02 14:54:50 -0700 | [diff] [blame] | 7447 | * __mem_cgroup_charge(). |
Johannes Weiner | 747db95 | 2014-08-08 14:19:24 -0700 | [diff] [blame] | 7448 | */ |
Suren Baghdasaryan | 2c8d8f9 | 2021-09-02 14:54:50 -0700 | [diff] [blame] | 7449 | void __mem_cgroup_uncharge_list(struct list_head *page_list) |
Johannes Weiner | 747db95 | 2014-08-08 14:19:24 -0700 | [diff] [blame] | 7450 | { |
Muchun Song | c41a40b | 2021-02-24 12:04:08 -0800 | [diff] [blame] | 7451 | struct uncharge_gather ug; |
Matthew Wilcox (Oracle) | c4ed6eb | 2021-06-29 21:47:12 -0400 | [diff] [blame] | 7452 | struct folio *folio; |
Muchun Song | c41a40b | 2021-02-24 12:04:08 -0800 | [diff] [blame] | 7453 | |
Muchun Song | c41a40b | 2021-02-24 12:04:08 -0800 | [diff] [blame] | 7454 | uncharge_gather_clear(&ug); |
Matthew Wilcox (Oracle) | c4ed6eb | 2021-06-29 21:47:12 -0400 | [diff] [blame] | 7455 | list_for_each_entry(folio, page_list, lru) |
| 7456 | uncharge_folio(folio, &ug); |
Muchun Song | c41a40b | 2021-02-24 12:04:08 -0800 | [diff] [blame] | 7457 | if (ug.memcg) |
| 7458 | uncharge_batch(&ug); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 7459 | } |
| 7460 | |
| 7461 | /** |
Nhat Pham | 85ce2c5 | 2023-10-06 11:46:27 -0700 | [diff] [blame] | 7462 | * mem_cgroup_replace_folio - Charge a folio's replacement. |
Matthew Wilcox (Oracle) | d21bba2 | 2021-05-06 18:14:59 -0400 | [diff] [blame] | 7463 | * @old: Currently circulating folio. |
| 7464 | * @new: Replacement folio. |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 7465 | * |
Matthew Wilcox (Oracle) | d21bba2 | 2021-05-06 18:14:59 -0400 | [diff] [blame] | 7466 | * Charge @new as a replacement folio for @old. @old will |
Nhat Pham | 85ce2c5 | 2023-10-06 11:46:27 -0700 | [diff] [blame] | 7467 | * be uncharged upon free. This is only used by the page cache |
| 7468 | * (in replace_page_cache_folio()). |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 7469 | * |
Matthew Wilcox (Oracle) | d21bba2 | 2021-05-06 18:14:59 -0400 | [diff] [blame] | 7470 | * Both folios must be locked, @new->mapping must be set up. |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 7471 | */ |
Nhat Pham | 85ce2c5 | 2023-10-06 11:46:27 -0700 | [diff] [blame] | 7472 | void mem_cgroup_replace_folio(struct folio *old, struct folio *new) |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 7473 | { |
Johannes Weiner | 2983331 | 2014-12-10 15:44:02 -0800 | [diff] [blame] | 7474 | struct mem_cgroup *memcg; |
Matthew Wilcox (Oracle) | d21bba2 | 2021-05-06 18:14:59 -0400 | [diff] [blame] | 7475 | long nr_pages = folio_nr_pages(new); |
Tejun Heo | d93c413 | 2016-06-24 14:49:54 -0700 | [diff] [blame] | 7476 | unsigned long flags; |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 7477 | |
Matthew Wilcox (Oracle) | d21bba2 | 2021-05-06 18:14:59 -0400 | [diff] [blame] | 7478 | VM_BUG_ON_FOLIO(!folio_test_locked(old), old); |
| 7479 | VM_BUG_ON_FOLIO(!folio_test_locked(new), new); |
| 7480 | VM_BUG_ON_FOLIO(folio_test_anon(old) != folio_test_anon(new), new); |
| 7481 | VM_BUG_ON_FOLIO(folio_nr_pages(old) != nr_pages, new); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 7482 | |
| 7483 | if (mem_cgroup_disabled()) |
| 7484 | return; |
| 7485 | |
Matthew Wilcox (Oracle) | d21bba2 | 2021-05-06 18:14:59 -0400 | [diff] [blame] | 7486 | /* Page cache replacement: new folio already charged? */ |
| 7487 | if (folio_memcg(new)) |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 7488 | return; |
| 7489 | |
Matthew Wilcox (Oracle) | d21bba2 | 2021-05-06 18:14:59 -0400 | [diff] [blame] | 7490 | memcg = folio_memcg(old); |
| 7491 | VM_WARN_ON_ONCE_FOLIO(!memcg, old); |
Johannes Weiner | 2983331 | 2014-12-10 15:44:02 -0800 | [diff] [blame] | 7492 | if (!memcg) |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 7493 | return; |
| 7494 | |
Johannes Weiner | 44b7a8d | 2016-01-20 15:03:16 -0800 | [diff] [blame] | 7495 | /* Force-charge the new page. The old one will be freed soon */ |
Muchun Song | 8dc87c7 | 2021-06-28 19:37:47 -0700 | [diff] [blame] | 7496 | if (!mem_cgroup_is_root(memcg)) { |
| 7497 | page_counter_charge(&memcg->memory, nr_pages); |
| 7498 | if (do_memsw_account()) |
| 7499 | page_counter_charge(&memcg->memsw, nr_pages); |
| 7500 | } |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 7501 | |
Johannes Weiner | 1a3e1f4 | 2020-08-06 23:20:45 -0700 | [diff] [blame] | 7502 | css_get(&memcg->css); |
Matthew Wilcox (Oracle) | d21bba2 | 2021-05-06 18:14:59 -0400 | [diff] [blame] | 7503 | commit_charge(new, memcg); |
Johannes Weiner | 44b7a8d | 2016-01-20 15:03:16 -0800 | [diff] [blame] | 7504 | |
Tejun Heo | d93c413 | 2016-06-24 14:49:54 -0700 | [diff] [blame] | 7505 | local_irq_save(flags); |
Matthew Wilcox (Oracle) | 6e0110c | 2021-04-29 13:40:11 -0400 | [diff] [blame] | 7506 | mem_cgroup_charge_statistics(memcg, nr_pages); |
Matthew Wilcox (Oracle) | d21bba2 | 2021-05-06 18:14:59 -0400 | [diff] [blame] | 7507 | memcg_check_events(memcg, folio_nid(new)); |
Tejun Heo | d93c413 | 2016-06-24 14:49:54 -0700 | [diff] [blame] | 7508 | local_irq_restore(flags); |
Johannes Weiner | 0a31bc9 | 2014-08-08 14:19:22 -0700 | [diff] [blame] | 7509 | } |
| 7510 | |
Nhat Pham | 85ce2c5 | 2023-10-06 11:46:27 -0700 | [diff] [blame] | 7511 | /** |
| 7512 | * mem_cgroup_migrate - Transfer the memcg data from the old to the new folio. |
| 7513 | * @old: Currently circulating folio. |
| 7514 | * @new: Replacement folio. |
| 7515 | * |
| 7516 | * Transfer the memcg data from the old folio to the new folio for migration. |
| 7517 | * The old folio's data info will be cleared. Note that the memory counters |
| 7518 | * will remain unchanged throughout the process. |
| 7519 | * |
| 7520 | * Both folios must be locked, @new->mapping must be set up. |
| 7521 | */ |
| 7522 | void mem_cgroup_migrate(struct folio *old, struct folio *new) |
| 7523 | { |
| 7524 | struct mem_cgroup *memcg; |
| 7525 | |
| 7526 | VM_BUG_ON_FOLIO(!folio_test_locked(old), old); |
| 7527 | VM_BUG_ON_FOLIO(!folio_test_locked(new), new); |
| 7528 | VM_BUG_ON_FOLIO(folio_test_anon(old) != folio_test_anon(new), new); |
| 7529 | VM_BUG_ON_FOLIO(folio_nr_pages(old) != folio_nr_pages(new), new); |
| 7530 | |
| 7531 | if (mem_cgroup_disabled()) |
| 7532 | return; |
| 7533 | |
| 7534 | memcg = folio_memcg(old); |
Nhat Pham | 8cba957 | 2023-10-06 11:46:28 -0700 | [diff] [blame] | 7535 | /* |
| 7536 | * Note that it is normal to see !memcg for a hugetlb folio. |
| 7537 | * For e.g, itt could have been allocated when memory_hugetlb_accounting |
| 7538 | * was not selected. |
| 7539 | */ |
| 7540 | VM_WARN_ON_ONCE_FOLIO(!folio_test_hugetlb(old) && !memcg, old); |
Nhat Pham | 85ce2c5 | 2023-10-06 11:46:27 -0700 | [diff] [blame] | 7541 | if (!memcg) |
| 7542 | return; |
| 7543 | |
| 7544 | /* Transfer the charge and the css ref */ |
| 7545 | commit_charge(new, memcg); |
| 7546 | old->memcg_data = 0; |
| 7547 | } |
| 7548 | |
Johannes Weiner | ef12947 | 2016-01-14 15:21:34 -0800 | [diff] [blame] | 7549 | DEFINE_STATIC_KEY_FALSE(memcg_sockets_enabled_key); |
Johannes Weiner | 1109208 | 2016-01-14 15:21:26 -0800 | [diff] [blame] | 7550 | EXPORT_SYMBOL(memcg_sockets_enabled_key); |
| 7551 | |
Johannes Weiner | 2d75807 | 2016-10-07 17:00:58 -0700 | [diff] [blame] | 7552 | void mem_cgroup_sk_alloc(struct sock *sk) |
Johannes Weiner | 1109208 | 2016-01-14 15:21:26 -0800 | [diff] [blame] | 7553 | { |
| 7554 | struct mem_cgroup *memcg; |
| 7555 | |
Johannes Weiner | 2d75807 | 2016-10-07 17:00:58 -0700 | [diff] [blame] | 7556 | if (!mem_cgroup_sockets_enabled) |
| 7557 | return; |
| 7558 | |
Shakeel Butt | e876ecc | 2020-03-09 22:16:05 -0700 | [diff] [blame] | 7559 | /* Do not associate the sock with unrelated interrupted task's memcg. */ |
Shakeel Butt | 086f694 | 2022-03-22 14:40:07 -0700 | [diff] [blame] | 7560 | if (!in_task()) |
Shakeel Butt | e876ecc | 2020-03-09 22:16:05 -0700 | [diff] [blame] | 7561 | return; |
| 7562 | |
Johannes Weiner | 1109208 | 2016-01-14 15:21:26 -0800 | [diff] [blame] | 7563 | rcu_read_lock(); |
| 7564 | memcg = mem_cgroup_from_task(current); |
Kamalesh Babulal | 7848ed6 | 2022-09-30 19:14:33 +0530 | [diff] [blame] | 7565 | if (mem_cgroup_is_root(memcg)) |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 7566 | goto out; |
Johannes Weiner | 0db1529 | 2016-01-20 15:02:50 -0800 | [diff] [blame] | 7567 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && !memcg->tcpmem_active) |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 7568 | goto out; |
Shakeel Butt | 8965aa2 | 2020-04-01 21:07:10 -0700 | [diff] [blame] | 7569 | if (css_tryget(&memcg->css)) |
Johannes Weiner | 1109208 | 2016-01-14 15:21:26 -0800 | [diff] [blame] | 7570 | sk->sk_memcg = memcg; |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 7571 | out: |
Johannes Weiner | 1109208 | 2016-01-14 15:21:26 -0800 | [diff] [blame] | 7572 | rcu_read_unlock(); |
| 7573 | } |
Johannes Weiner | 1109208 | 2016-01-14 15:21:26 -0800 | [diff] [blame] | 7574 | |
Johannes Weiner | 2d75807 | 2016-10-07 17:00:58 -0700 | [diff] [blame] | 7575 | void mem_cgroup_sk_free(struct sock *sk) |
Johannes Weiner | 1109208 | 2016-01-14 15:21:26 -0800 | [diff] [blame] | 7576 | { |
Johannes Weiner | 2d75807 | 2016-10-07 17:00:58 -0700 | [diff] [blame] | 7577 | if (sk->sk_memcg) |
| 7578 | css_put(&sk->sk_memcg->css); |
Johannes Weiner | 1109208 | 2016-01-14 15:21:26 -0800 | [diff] [blame] | 7579 | } |
| 7580 | |
| 7581 | /** |
| 7582 | * mem_cgroup_charge_skmem - charge socket memory |
| 7583 | * @memcg: memcg to charge |
| 7584 | * @nr_pages: number of pages to charge |
Wei Wang | 4b1327b | 2021-08-17 12:40:03 -0700 | [diff] [blame] | 7585 | * @gfp_mask: reclaim mode |
Johannes Weiner | 1109208 | 2016-01-14 15:21:26 -0800 | [diff] [blame] | 7586 | * |
| 7587 | * Charges @nr_pages to @memcg. Returns %true if the charge fit within |
Wei Wang | 4b1327b | 2021-08-17 12:40:03 -0700 | [diff] [blame] | 7588 | * @memcg's configured limit, %false if it doesn't. |
Johannes Weiner | 1109208 | 2016-01-14 15:21:26 -0800 | [diff] [blame] | 7589 | */ |
Wei Wang | 4b1327b | 2021-08-17 12:40:03 -0700 | [diff] [blame] | 7590 | bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages, |
| 7591 | gfp_t gfp_mask) |
Johannes Weiner | 1109208 | 2016-01-14 15:21:26 -0800 | [diff] [blame] | 7592 | { |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 7593 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) { |
Johannes Weiner | 0db1529 | 2016-01-20 15:02:50 -0800 | [diff] [blame] | 7594 | struct page_counter *fail; |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 7595 | |
Johannes Weiner | 0db1529 | 2016-01-20 15:02:50 -0800 | [diff] [blame] | 7596 | if (page_counter_try_charge(&memcg->tcpmem, nr_pages, &fail)) { |
| 7597 | memcg->tcpmem_pressure = 0; |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 7598 | return true; |
| 7599 | } |
Johannes Weiner | 0db1529 | 2016-01-20 15:02:50 -0800 | [diff] [blame] | 7600 | memcg->tcpmem_pressure = 1; |
Wei Wang | 4b1327b | 2021-08-17 12:40:03 -0700 | [diff] [blame] | 7601 | if (gfp_mask & __GFP_NOFAIL) { |
| 7602 | page_counter_charge(&memcg->tcpmem, nr_pages); |
| 7603 | return true; |
| 7604 | } |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 7605 | return false; |
Johannes Weiner | 1109208 | 2016-01-14 15:21:26 -0800 | [diff] [blame] | 7606 | } |
Johannes Weiner | d886f4e | 2016-01-20 15:02:47 -0800 | [diff] [blame] | 7607 | |
Wei Wang | 4b1327b | 2021-08-17 12:40:03 -0700 | [diff] [blame] | 7608 | if (try_charge(memcg, gfp_mask, nr_pages) == 0) { |
| 7609 | mod_memcg_state(memcg, MEMCG_SOCK, nr_pages); |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 7610 | return true; |
Wei Wang | 4b1327b | 2021-08-17 12:40:03 -0700 | [diff] [blame] | 7611 | } |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 7612 | |
Johannes Weiner | 1109208 | 2016-01-14 15:21:26 -0800 | [diff] [blame] | 7613 | return false; |
| 7614 | } |
| 7615 | |
| 7616 | /** |
| 7617 | * mem_cgroup_uncharge_skmem - uncharge socket memory |
Mike Rapoport | b7701a5 | 2018-02-06 15:42:13 -0800 | [diff] [blame] | 7618 | * @memcg: memcg to uncharge |
| 7619 | * @nr_pages: number of pages to uncharge |
Johannes Weiner | 1109208 | 2016-01-14 15:21:26 -0800 | [diff] [blame] | 7620 | */ |
| 7621 | void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages) |
| 7622 | { |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 7623 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) { |
Johannes Weiner | 0db1529 | 2016-01-20 15:02:50 -0800 | [diff] [blame] | 7624 | page_counter_uncharge(&memcg->tcpmem, nr_pages); |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 7625 | return; |
| 7626 | } |
Johannes Weiner | d886f4e | 2016-01-20 15:02:47 -0800 | [diff] [blame] | 7627 | |
Johannes Weiner | c9019e9 | 2018-01-31 16:16:37 -0800 | [diff] [blame] | 7628 | mod_memcg_state(memcg, MEMCG_SOCK, -nr_pages); |
Johannes Weiner | b2807f0 | 2016-01-20 15:03:22 -0800 | [diff] [blame] | 7629 | |
Roman Gushchin | 475d048 | 2017-09-08 16:13:09 -0700 | [diff] [blame] | 7630 | refill_stock(memcg, nr_pages); |
Johannes Weiner | 1109208 | 2016-01-14 15:21:26 -0800 | [diff] [blame] | 7631 | } |
| 7632 | |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 7633 | static int __init cgroup_memory(char *s) |
| 7634 | { |
| 7635 | char *token; |
| 7636 | |
| 7637 | while ((token = strsep(&s, ",")) != NULL) { |
| 7638 | if (!*token) |
| 7639 | continue; |
| 7640 | if (!strcmp(token, "nosocket")) |
| 7641 | cgroup_memory_nosocket = true; |
Vladimir Davydov | 04823c8 | 2016-01-20 15:02:38 -0800 | [diff] [blame] | 7642 | if (!strcmp(token, "nokmem")) |
| 7643 | cgroup_memory_nokmem = true; |
Yafang Shao | b6c1a8a | 2023-02-10 15:47:31 +0000 | [diff] [blame] | 7644 | if (!strcmp(token, "nobpf")) |
| 7645 | cgroup_memory_nobpf = true; |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 7646 | } |
Randy Dunlap | 460a79e | 2022-03-22 14:40:31 -0700 | [diff] [blame] | 7647 | return 1; |
Johannes Weiner | f7e1cb6 | 2016-01-14 15:21:29 -0800 | [diff] [blame] | 7648 | } |
| 7649 | __setup("cgroup.memory=", cgroup_memory); |
Johannes Weiner | 1109208 | 2016-01-14 15:21:26 -0800 | [diff] [blame] | 7650 | |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 7651 | /* |
Michal Hocko | 1081312 | 2013-02-22 16:35:41 -0800 | [diff] [blame] | 7652 | * subsys_initcall() for memory controller. |
| 7653 | * |
Sebastian Andrzej Siewior | 308167f | 2016-11-03 15:49:59 +0100 | [diff] [blame] | 7654 | * Some parts like memcg_hotplug_cpu_dead() have to be initialized from this |
| 7655 | * context because of lock dependencies (cgroup_lock -> cpu hotplug) but |
| 7656 | * basically everything that doesn't depend on a specific mem_cgroup structure |
| 7657 | * should be initialized from here. |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 7658 | */ |
| 7659 | static int __init mem_cgroup_init(void) |
| 7660 | { |
Johannes Weiner | 95a045f | 2015-02-11 15:26:33 -0800 | [diff] [blame] | 7661 | int cpu, node; |
| 7662 | |
Muchun Song | f3344ad | 2021-02-24 12:03:15 -0800 | [diff] [blame] | 7663 | /* |
| 7664 | * Currently s32 type (can refer to struct batched_lruvec_stat) is |
| 7665 | * used for per-memcg-per-cpu caching of per-node statistics. In order |
| 7666 | * to work fine, we should make sure that the overfill threshold can't |
| 7667 | * exceed S32_MAX / PAGE_SIZE. |
| 7668 | */ |
| 7669 | BUILD_BUG_ON(MEMCG_CHARGE_BATCH > S32_MAX / PAGE_SIZE); |
| 7670 | |
Sebastian Andrzej Siewior | 308167f | 2016-11-03 15:49:59 +0100 | [diff] [blame] | 7671 | cpuhp_setup_state_nocalls(CPUHP_MM_MEMCQ_DEAD, "mm/memctrl:dead", NULL, |
| 7672 | memcg_hotplug_cpu_dead); |
Johannes Weiner | 95a045f | 2015-02-11 15:26:33 -0800 | [diff] [blame] | 7673 | |
| 7674 | for_each_possible_cpu(cpu) |
| 7675 | INIT_WORK(&per_cpu_ptr(&memcg_stock, cpu)->work, |
| 7676 | drain_local_stock); |
| 7677 | |
| 7678 | for_each_node(node) { |
| 7679 | struct mem_cgroup_tree_per_node *rtpn; |
Johannes Weiner | 95a045f | 2015-02-11 15:26:33 -0800 | [diff] [blame] | 7680 | |
Haifeng Xu | 91f0dcc | 2023-06-19 13:04:42 +0000 | [diff] [blame] | 7681 | rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, node); |
Johannes Weiner | 95a045f | 2015-02-11 15:26:33 -0800 | [diff] [blame] | 7682 | |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 7683 | rtpn->rb_root = RB_ROOT; |
Davidlohr Bueso | fa90b2f | 2017-09-08 16:15:21 -0700 | [diff] [blame] | 7684 | rtpn->rb_rightmost = NULL; |
Mel Gorman | ef8f232 | 2016-07-28 15:46:05 -0700 | [diff] [blame] | 7685 | spin_lock_init(&rtpn->lock); |
Johannes Weiner | 95a045f | 2015-02-11 15:26:33 -0800 | [diff] [blame] | 7686 | soft_limit_tree.rb_tree_per_node[node] = rtpn; |
| 7687 | } |
| 7688 | |
Michal Hocko | 2d11085 | 2013-02-22 16:34:43 -0800 | [diff] [blame] | 7689 | return 0; |
| 7690 | } |
| 7691 | subsys_initcall(mem_cgroup_init); |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7692 | |
Johannes Weiner | e55b9f9 | 2022-09-26 09:57:04 -0400 | [diff] [blame] | 7693 | #ifdef CONFIG_SWAP |
Arnd Bergmann | 358c07f | 2016-08-25 15:17:08 -0700 | [diff] [blame] | 7694 | static struct mem_cgroup *mem_cgroup_id_get_online(struct mem_cgroup *memcg) |
| 7695 | { |
Kirill Tkhai | 1c2d479 | 2018-10-26 15:09:28 -0700 | [diff] [blame] | 7696 | while (!refcount_inc_not_zero(&memcg->id.ref)) { |
Arnd Bergmann | 358c07f | 2016-08-25 15:17:08 -0700 | [diff] [blame] | 7697 | /* |
| 7698 | * The root cgroup cannot be destroyed, so it's refcount must |
| 7699 | * always be >= 1. |
| 7700 | */ |
Kamalesh Babulal | 7848ed6 | 2022-09-30 19:14:33 +0530 | [diff] [blame] | 7701 | if (WARN_ON_ONCE(mem_cgroup_is_root(memcg))) { |
Arnd Bergmann | 358c07f | 2016-08-25 15:17:08 -0700 | [diff] [blame] | 7702 | VM_BUG_ON(1); |
| 7703 | break; |
| 7704 | } |
| 7705 | memcg = parent_mem_cgroup(memcg); |
| 7706 | if (!memcg) |
| 7707 | memcg = root_mem_cgroup; |
| 7708 | } |
| 7709 | return memcg; |
| 7710 | } |
| 7711 | |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7712 | /** |
| 7713 | * mem_cgroup_swapout - transfer a memsw charge to swap |
Matthew Wilcox (Oracle) | 3ecb008 | 2021-12-27 21:11:34 -0500 | [diff] [blame] | 7714 | * @folio: folio whose memsw charge to transfer |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7715 | * @entry: swap entry to move the charge to |
| 7716 | * |
Matthew Wilcox (Oracle) | 3ecb008 | 2021-12-27 21:11:34 -0500 | [diff] [blame] | 7717 | * Transfer the memsw charge of @folio to @entry. |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7718 | */ |
Matthew Wilcox (Oracle) | 3ecb008 | 2021-12-27 21:11:34 -0500 | [diff] [blame] | 7719 | void mem_cgroup_swapout(struct folio *folio, swp_entry_t entry) |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7720 | { |
Vladimir Davydov | 1f47b61 | 2016-08-11 15:33:00 -0700 | [diff] [blame] | 7721 | struct mem_cgroup *memcg, *swap_memcg; |
Huang Ying | d6810d73 | 2017-09-06 16:22:45 -0700 | [diff] [blame] | 7722 | unsigned int nr_entries; |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7723 | unsigned short oldid; |
| 7724 | |
Matthew Wilcox (Oracle) | 3ecb008 | 2021-12-27 21:11:34 -0500 | [diff] [blame] | 7725 | VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); |
| 7726 | VM_BUG_ON_FOLIO(folio_ref_count(folio), folio); |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7727 | |
Alex Shi | 76358ab | 2020-12-18 14:01:28 -0800 | [diff] [blame] | 7728 | if (mem_cgroup_disabled()) |
| 7729 | return; |
| 7730 | |
Johannes Weiner | b94c4e9 | 2022-09-26 09:57:03 -0400 | [diff] [blame] | 7731 | if (!do_memsw_account()) |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7732 | return; |
| 7733 | |
Matthew Wilcox (Oracle) | 3ecb008 | 2021-12-27 21:11:34 -0500 | [diff] [blame] | 7734 | memcg = folio_memcg(folio); |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7735 | |
Matthew Wilcox (Oracle) | 3ecb008 | 2021-12-27 21:11:34 -0500 | [diff] [blame] | 7736 | VM_WARN_ON_ONCE_FOLIO(!memcg, folio); |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7737 | if (!memcg) |
| 7738 | return; |
| 7739 | |
Vladimir Davydov | 1f47b61 | 2016-08-11 15:33:00 -0700 | [diff] [blame] | 7740 | /* |
| 7741 | * In case the memcg owning these pages has been offlined and doesn't |
| 7742 | * have an ID allocated to it anymore, charge the closest online |
| 7743 | * ancestor for the swap instead and transfer the memory+swap charge. |
| 7744 | */ |
| 7745 | swap_memcg = mem_cgroup_id_get_online(memcg); |
Matthew Wilcox (Oracle) | 3ecb008 | 2021-12-27 21:11:34 -0500 | [diff] [blame] | 7746 | nr_entries = folio_nr_pages(folio); |
Huang Ying | d6810d73 | 2017-09-06 16:22:45 -0700 | [diff] [blame] | 7747 | /* Get references for the tail pages, too */ |
| 7748 | if (nr_entries > 1) |
| 7749 | mem_cgroup_id_get_many(swap_memcg, nr_entries - 1); |
| 7750 | oldid = swap_cgroup_record(entry, mem_cgroup_id(swap_memcg), |
| 7751 | nr_entries); |
Matthew Wilcox (Oracle) | 3ecb008 | 2021-12-27 21:11:34 -0500 | [diff] [blame] | 7752 | VM_BUG_ON_FOLIO(oldid, folio); |
Johannes Weiner | c9019e9 | 2018-01-31 16:16:37 -0800 | [diff] [blame] | 7753 | mod_memcg_state(swap_memcg, MEMCG_SWAP, nr_entries); |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7754 | |
Matthew Wilcox (Oracle) | 3ecb008 | 2021-12-27 21:11:34 -0500 | [diff] [blame] | 7755 | folio->memcg_data = 0; |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7756 | |
| 7757 | if (!mem_cgroup_is_root(memcg)) |
Huang Ying | d6810d73 | 2017-09-06 16:22:45 -0700 | [diff] [blame] | 7758 | page_counter_uncharge(&memcg->memory, nr_entries); |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7759 | |
Johannes Weiner | b25806d | 2022-09-26 09:57:02 -0400 | [diff] [blame] | 7760 | if (memcg != swap_memcg) { |
Vladimir Davydov | 1f47b61 | 2016-08-11 15:33:00 -0700 | [diff] [blame] | 7761 | if (!mem_cgroup_is_root(swap_memcg)) |
Huang Ying | d6810d73 | 2017-09-06 16:22:45 -0700 | [diff] [blame] | 7762 | page_counter_charge(&swap_memcg->memsw, nr_entries); |
| 7763 | page_counter_uncharge(&memcg->memsw, nr_entries); |
Vladimir Davydov | 1f47b61 | 2016-08-11 15:33:00 -0700 | [diff] [blame] | 7764 | } |
| 7765 | |
Sebastian Andrzej Siewior | ce9ce66 | 2015-09-04 15:47:50 -0700 | [diff] [blame] | 7766 | /* |
| 7767 | * Interrupts should be disabled here because the caller holds the |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 7768 | * i_pages lock which is taken with interrupts-off. It is |
Sebastian Andrzej Siewior | ce9ce66 | 2015-09-04 15:47:50 -0700 | [diff] [blame] | 7769 | * important here to have the interrupts disabled because it is the |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 7770 | * only synchronisation we have for updating the per-CPU variables. |
Sebastian Andrzej Siewior | ce9ce66 | 2015-09-04 15:47:50 -0700 | [diff] [blame] | 7771 | */ |
Sebastian Andrzej Siewior | be3e67b | 2022-03-22 14:40:41 -0700 | [diff] [blame] | 7772 | memcg_stats_lock(); |
Matthew Wilcox (Oracle) | 6e0110c | 2021-04-29 13:40:11 -0400 | [diff] [blame] | 7773 | mem_cgroup_charge_statistics(memcg, -nr_entries); |
Sebastian Andrzej Siewior | be3e67b | 2022-03-22 14:40:41 -0700 | [diff] [blame] | 7774 | memcg_stats_unlock(); |
Matthew Wilcox (Oracle) | 3ecb008 | 2021-12-27 21:11:34 -0500 | [diff] [blame] | 7775 | memcg_check_events(memcg, folio_nid(folio)); |
Johannes Weiner | 73f576c | 2016-07-20 15:44:57 -0700 | [diff] [blame] | 7776 | |
Johannes Weiner | 1a3e1f4 | 2020-08-06 23:20:45 -0700 | [diff] [blame] | 7777 | css_put(&memcg->css); |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7778 | } |
| 7779 | |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 7780 | /** |
Matthew Wilcox (Oracle) | e2e3fdc | 2022-05-12 20:23:02 -0700 | [diff] [blame] | 7781 | * __mem_cgroup_try_charge_swap - try charging swap space for a folio |
| 7782 | * @folio: folio being added to swap |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 7783 | * @entry: swap entry to charge |
| 7784 | * |
Matthew Wilcox (Oracle) | e2e3fdc | 2022-05-12 20:23:02 -0700 | [diff] [blame] | 7785 | * Try to charge @folio's memcg for the swap space at @entry. |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 7786 | * |
| 7787 | * Returns 0 on success, -ENOMEM on failure. |
| 7788 | */ |
Matthew Wilcox (Oracle) | e2e3fdc | 2022-05-12 20:23:02 -0700 | [diff] [blame] | 7789 | int __mem_cgroup_try_charge_swap(struct folio *folio, swp_entry_t entry) |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 7790 | { |
Matthew Wilcox (Oracle) | e2e3fdc | 2022-05-12 20:23:02 -0700 | [diff] [blame] | 7791 | unsigned int nr_pages = folio_nr_pages(folio); |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 7792 | struct page_counter *counter; |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 7793 | struct mem_cgroup *memcg; |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 7794 | unsigned short oldid; |
| 7795 | |
Johannes Weiner | b94c4e9 | 2022-09-26 09:57:03 -0400 | [diff] [blame] | 7796 | if (do_memsw_account()) |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 7797 | return 0; |
| 7798 | |
Matthew Wilcox (Oracle) | e2e3fdc | 2022-05-12 20:23:02 -0700 | [diff] [blame] | 7799 | memcg = folio_memcg(folio); |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 7800 | |
Matthew Wilcox (Oracle) | e2e3fdc | 2022-05-12 20:23:02 -0700 | [diff] [blame] | 7801 | VM_WARN_ON_ONCE_FOLIO(!memcg, folio); |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 7802 | if (!memcg) |
| 7803 | return 0; |
| 7804 | |
Tejun Heo | f3a53a3 | 2018-06-07 17:05:35 -0700 | [diff] [blame] | 7805 | if (!entry.val) { |
| 7806 | memcg_memory_event(memcg, MEMCG_SWAP_FAIL); |
Tejun Heo | bb98f2c | 2018-06-07 17:05:31 -0700 | [diff] [blame] | 7807 | return 0; |
Tejun Heo | f3a53a3 | 2018-06-07 17:05:35 -0700 | [diff] [blame] | 7808 | } |
Tejun Heo | bb98f2c | 2018-06-07 17:05:31 -0700 | [diff] [blame] | 7809 | |
Vladimir Davydov | 1f47b61 | 2016-08-11 15:33:00 -0700 | [diff] [blame] | 7810 | memcg = mem_cgroup_id_get_online(memcg); |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 7811 | |
Johannes Weiner | b25806d | 2022-09-26 09:57:02 -0400 | [diff] [blame] | 7812 | if (!mem_cgroup_is_root(memcg) && |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 7813 | !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) { |
Tejun Heo | f3a53a3 | 2018-06-07 17:05:35 -0700 | [diff] [blame] | 7814 | memcg_memory_event(memcg, MEMCG_SWAP_MAX); |
| 7815 | memcg_memory_event(memcg, MEMCG_SWAP_FAIL); |
Vladimir Davydov | 1f47b61 | 2016-08-11 15:33:00 -0700 | [diff] [blame] | 7816 | mem_cgroup_id_put(memcg); |
| 7817 | return -ENOMEM; |
| 7818 | } |
| 7819 | |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 7820 | /* Get references for the tail pages, too */ |
| 7821 | if (nr_pages > 1) |
| 7822 | mem_cgroup_id_get_many(memcg, nr_pages - 1); |
| 7823 | oldid = swap_cgroup_record(entry, mem_cgroup_id(memcg), nr_pages); |
Matthew Wilcox (Oracle) | e2e3fdc | 2022-05-12 20:23:02 -0700 | [diff] [blame] | 7824 | VM_BUG_ON_FOLIO(oldid, folio); |
Johannes Weiner | c9019e9 | 2018-01-31 16:16:37 -0800 | [diff] [blame] | 7825 | mod_memcg_state(memcg, MEMCG_SWAP, nr_pages); |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 7826 | |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 7827 | return 0; |
| 7828 | } |
| 7829 | |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7830 | /** |
Suren Baghdasaryan | 01c4b28 | 2021-09-02 14:54:54 -0700 | [diff] [blame] | 7831 | * __mem_cgroup_uncharge_swap - uncharge swap space |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7832 | * @entry: swap entry to uncharge |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 7833 | * @nr_pages: the amount of swap space to uncharge |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7834 | */ |
Suren Baghdasaryan | 01c4b28 | 2021-09-02 14:54:54 -0700 | [diff] [blame] | 7835 | void __mem_cgroup_uncharge_swap(swp_entry_t entry, unsigned int nr_pages) |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7836 | { |
| 7837 | struct mem_cgroup *memcg; |
| 7838 | unsigned short id; |
| 7839 | |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 7840 | id = swap_cgroup_record(entry, 0, nr_pages); |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7841 | rcu_read_lock(); |
Vladimir Davydov | adbe427 | 2015-04-15 16:13:00 -0700 | [diff] [blame] | 7842 | memcg = mem_cgroup_from_id(id); |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7843 | if (memcg) { |
Johannes Weiner | b25806d | 2022-09-26 09:57:02 -0400 | [diff] [blame] | 7844 | if (!mem_cgroup_is_root(memcg)) { |
Johannes Weiner | b94c4e9 | 2022-09-26 09:57:03 -0400 | [diff] [blame] | 7845 | if (do_memsw_account()) |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 7846 | page_counter_uncharge(&memcg->memsw, nr_pages); |
Johannes Weiner | b94c4e9 | 2022-09-26 09:57:03 -0400 | [diff] [blame] | 7847 | else |
| 7848 | page_counter_uncharge(&memcg->swap, nr_pages); |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 7849 | } |
Johannes Weiner | c9019e9 | 2018-01-31 16:16:37 -0800 | [diff] [blame] | 7850 | mod_memcg_state(memcg, MEMCG_SWAP, -nr_pages); |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 7851 | mem_cgroup_id_put_many(memcg, nr_pages); |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7852 | } |
| 7853 | rcu_read_unlock(); |
| 7854 | } |
| 7855 | |
Vladimir Davydov | d8b3843 | 2016-01-20 15:03:07 -0800 | [diff] [blame] | 7856 | long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg) |
| 7857 | { |
| 7858 | long nr_swap_pages = get_nr_swap_pages(); |
| 7859 | |
Johannes Weiner | b25806d | 2022-09-26 09:57:02 -0400 | [diff] [blame] | 7860 | if (mem_cgroup_disabled() || do_memsw_account()) |
Vladimir Davydov | d8b3843 | 2016-01-20 15:03:07 -0800 | [diff] [blame] | 7861 | return nr_swap_pages; |
Kamalesh Babulal | 7848ed6 | 2022-09-30 19:14:33 +0530 | [diff] [blame] | 7862 | for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) |
Vladimir Davydov | d8b3843 | 2016-01-20 15:03:07 -0800 | [diff] [blame] | 7863 | nr_swap_pages = min_t(long, nr_swap_pages, |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 7864 | READ_ONCE(memcg->swap.max) - |
Vladimir Davydov | d8b3843 | 2016-01-20 15:03:07 -0800 | [diff] [blame] | 7865 | page_counter_read(&memcg->swap)); |
| 7866 | return nr_swap_pages; |
| 7867 | } |
| 7868 | |
Matthew Wilcox (Oracle) | 9202d527b | 2022-09-02 20:46:43 +0100 | [diff] [blame] | 7869 | bool mem_cgroup_swap_full(struct folio *folio) |
Vladimir Davydov | 5ccc5ab | 2016-01-20 15:03:10 -0800 | [diff] [blame] | 7870 | { |
| 7871 | struct mem_cgroup *memcg; |
| 7872 | |
Matthew Wilcox (Oracle) | 9202d527b | 2022-09-02 20:46:43 +0100 | [diff] [blame] | 7873 | VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); |
Vladimir Davydov | 5ccc5ab | 2016-01-20 15:03:10 -0800 | [diff] [blame] | 7874 | |
| 7875 | if (vm_swap_full()) |
| 7876 | return true; |
Johannes Weiner | b25806d | 2022-09-26 09:57:02 -0400 | [diff] [blame] | 7877 | if (do_memsw_account()) |
Vladimir Davydov | 5ccc5ab | 2016-01-20 15:03:10 -0800 | [diff] [blame] | 7878 | return false; |
| 7879 | |
Matthew Wilcox (Oracle) | 9202d527b | 2022-09-02 20:46:43 +0100 | [diff] [blame] | 7880 | memcg = folio_memcg(folio); |
Vladimir Davydov | 5ccc5ab | 2016-01-20 15:03:10 -0800 | [diff] [blame] | 7881 | if (!memcg) |
| 7882 | return false; |
| 7883 | |
Kamalesh Babulal | 7848ed6 | 2022-09-30 19:14:33 +0530 | [diff] [blame] | 7884 | for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) { |
Jakub Kicinski | 4b82ab4 | 2020-06-01 21:49:52 -0700 | [diff] [blame] | 7885 | unsigned long usage = page_counter_read(&memcg->swap); |
| 7886 | |
| 7887 | if (usage * 2 >= READ_ONCE(memcg->swap.high) || |
| 7888 | usage * 2 >= READ_ONCE(memcg->swap.max)) |
Vladimir Davydov | 5ccc5ab | 2016-01-20 15:03:10 -0800 | [diff] [blame] | 7889 | return true; |
Jakub Kicinski | 4b82ab4 | 2020-06-01 21:49:52 -0700 | [diff] [blame] | 7890 | } |
Vladimir Davydov | 5ccc5ab | 2016-01-20 15:03:10 -0800 | [diff] [blame] | 7891 | |
| 7892 | return false; |
| 7893 | } |
| 7894 | |
Johannes Weiner | eccb52e | 2020-06-03 16:02:11 -0700 | [diff] [blame] | 7895 | static int __init setup_swap_account(char *s) |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7896 | { |
Johannes Weiner | b25806d | 2022-09-26 09:57:02 -0400 | [diff] [blame] | 7897 | pr_warn_once("The swapaccount= commandline option is deprecated. " |
| 7898 | "Please report your usecase to linux-mm@kvack.org if you " |
| 7899 | "depend on this functionality.\n"); |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7900 | return 1; |
| 7901 | } |
Johannes Weiner | eccb52e | 2020-06-03 16:02:11 -0700 | [diff] [blame] | 7902 | __setup("swapaccount=", setup_swap_account); |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 7903 | |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 7904 | static u64 swap_current_read(struct cgroup_subsys_state *css, |
| 7905 | struct cftype *cft) |
| 7906 | { |
| 7907 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
| 7908 | |
| 7909 | return (u64)page_counter_read(&memcg->swap) * PAGE_SIZE; |
| 7910 | } |
| 7911 | |
Lars R. Damerow | e0e0b41 | 2023-05-24 11:17:33 -0700 | [diff] [blame] | 7912 | static u64 swap_peak_read(struct cgroup_subsys_state *css, |
| 7913 | struct cftype *cft) |
| 7914 | { |
| 7915 | struct mem_cgroup *memcg = mem_cgroup_from_css(css); |
| 7916 | |
| 7917 | return (u64)memcg->swap.watermark * PAGE_SIZE; |
| 7918 | } |
| 7919 | |
Jakub Kicinski | 4b82ab4 | 2020-06-01 21:49:52 -0700 | [diff] [blame] | 7920 | static int swap_high_show(struct seq_file *m, void *v) |
| 7921 | { |
| 7922 | return seq_puts_memcg_tunable(m, |
| 7923 | READ_ONCE(mem_cgroup_from_seq(m)->swap.high)); |
| 7924 | } |
| 7925 | |
| 7926 | static ssize_t swap_high_write(struct kernfs_open_file *of, |
| 7927 | char *buf, size_t nbytes, loff_t off) |
| 7928 | { |
| 7929 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
| 7930 | unsigned long high; |
| 7931 | int err; |
| 7932 | |
| 7933 | buf = strstrip(buf); |
| 7934 | err = page_counter_memparse(buf, "max", &high); |
| 7935 | if (err) |
| 7936 | return err; |
| 7937 | |
| 7938 | page_counter_set_high(&memcg->swap, high); |
| 7939 | |
| 7940 | return nbytes; |
| 7941 | } |
| 7942 | |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 7943 | static int swap_max_show(struct seq_file *m, void *v) |
| 7944 | { |
Chris Down | 677dc97 | 2019-03-05 15:45:55 -0800 | [diff] [blame] | 7945 | return seq_puts_memcg_tunable(m, |
| 7946 | READ_ONCE(mem_cgroup_from_seq(m)->swap.max)); |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 7947 | } |
| 7948 | |
| 7949 | static ssize_t swap_max_write(struct kernfs_open_file *of, |
| 7950 | char *buf, size_t nbytes, loff_t off) |
| 7951 | { |
| 7952 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
| 7953 | unsigned long max; |
| 7954 | int err; |
| 7955 | |
| 7956 | buf = strstrip(buf); |
| 7957 | err = page_counter_memparse(buf, "max", &max); |
| 7958 | if (err) |
| 7959 | return err; |
| 7960 | |
Tejun Heo | be09102 | 2018-06-07 17:09:21 -0700 | [diff] [blame] | 7961 | xchg(&memcg->swap.max, max); |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 7962 | |
| 7963 | return nbytes; |
| 7964 | } |
| 7965 | |
Tejun Heo | f3a53a3 | 2018-06-07 17:05:35 -0700 | [diff] [blame] | 7966 | static int swap_events_show(struct seq_file *m, void *v) |
| 7967 | { |
Chris Down | aa9694b | 2019-03-05 15:45:52 -0800 | [diff] [blame] | 7968 | struct mem_cgroup *memcg = mem_cgroup_from_seq(m); |
Tejun Heo | f3a53a3 | 2018-06-07 17:05:35 -0700 | [diff] [blame] | 7969 | |
Jakub Kicinski | 4b82ab4 | 2020-06-01 21:49:52 -0700 | [diff] [blame] | 7970 | seq_printf(m, "high %lu\n", |
| 7971 | atomic_long_read(&memcg->memory_events[MEMCG_SWAP_HIGH])); |
Tejun Heo | f3a53a3 | 2018-06-07 17:05:35 -0700 | [diff] [blame] | 7972 | seq_printf(m, "max %lu\n", |
| 7973 | atomic_long_read(&memcg->memory_events[MEMCG_SWAP_MAX])); |
| 7974 | seq_printf(m, "fail %lu\n", |
| 7975 | atomic_long_read(&memcg->memory_events[MEMCG_SWAP_FAIL])); |
| 7976 | |
| 7977 | return 0; |
| 7978 | } |
| 7979 | |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 7980 | static struct cftype swap_files[] = { |
| 7981 | { |
| 7982 | .name = "swap.current", |
| 7983 | .flags = CFTYPE_NOT_ON_ROOT, |
| 7984 | .read_u64 = swap_current_read, |
| 7985 | }, |
| 7986 | { |
Jakub Kicinski | 4b82ab4 | 2020-06-01 21:49:52 -0700 | [diff] [blame] | 7987 | .name = "swap.high", |
| 7988 | .flags = CFTYPE_NOT_ON_ROOT, |
| 7989 | .seq_show = swap_high_show, |
| 7990 | .write = swap_high_write, |
| 7991 | }, |
| 7992 | { |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 7993 | .name = "swap.max", |
| 7994 | .flags = CFTYPE_NOT_ON_ROOT, |
| 7995 | .seq_show = swap_max_show, |
| 7996 | .write = swap_max_write, |
| 7997 | }, |
Tejun Heo | f3a53a3 | 2018-06-07 17:05:35 -0700 | [diff] [blame] | 7998 | { |
Lars R. Damerow | e0e0b41 | 2023-05-24 11:17:33 -0700 | [diff] [blame] | 7999 | .name = "swap.peak", |
| 8000 | .flags = CFTYPE_NOT_ON_ROOT, |
| 8001 | .read_u64 = swap_peak_read, |
| 8002 | }, |
| 8003 | { |
Tejun Heo | f3a53a3 | 2018-06-07 17:05:35 -0700 | [diff] [blame] | 8004 | .name = "swap.events", |
| 8005 | .flags = CFTYPE_NOT_ON_ROOT, |
| 8006 | .file_offset = offsetof(struct mem_cgroup, swap_events_file), |
| 8007 | .seq_show = swap_events_show, |
| 8008 | }, |
Vladimir Davydov | 37e8435 | 2016-01-20 15:02:56 -0800 | [diff] [blame] | 8009 | { } /* terminate */ |
| 8010 | }; |
| 8011 | |
Johannes Weiner | eccb52e | 2020-06-03 16:02:11 -0700 | [diff] [blame] | 8012 | static struct cftype memsw_files[] = { |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 8013 | { |
| 8014 | .name = "memsw.usage_in_bytes", |
| 8015 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), |
| 8016 | .read_u64 = mem_cgroup_read_u64, |
| 8017 | }, |
| 8018 | { |
| 8019 | .name = "memsw.max_usage_in_bytes", |
| 8020 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), |
| 8021 | .write = mem_cgroup_reset, |
| 8022 | .read_u64 = mem_cgroup_read_u64, |
| 8023 | }, |
| 8024 | { |
| 8025 | .name = "memsw.limit_in_bytes", |
| 8026 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), |
| 8027 | .write = mem_cgroup_write, |
| 8028 | .read_u64 = mem_cgroup_read_u64, |
| 8029 | }, |
| 8030 | { |
| 8031 | .name = "memsw.failcnt", |
| 8032 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), |
| 8033 | .write = mem_cgroup_reset, |
| 8034 | .read_u64 = mem_cgroup_read_u64, |
| 8035 | }, |
| 8036 | { }, /* terminate */ |
| 8037 | }; |
| 8038 | |
Johannes Weiner | f4840cc | 2022-05-19 14:08:53 -0700 | [diff] [blame] | 8039 | #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) |
| 8040 | /** |
| 8041 | * obj_cgroup_may_zswap - check if this cgroup can zswap |
| 8042 | * @objcg: the object cgroup |
| 8043 | * |
| 8044 | * Check if the hierarchical zswap limit has been reached. |
| 8045 | * |
| 8046 | * This doesn't check for specific headroom, and it is not atomic |
| 8047 | * either. But with zswap, the size of the allocation is only known |
Muhammad Muzammil | be16dd76 | 2023-10-23 17:44:05 +0500 | [diff] [blame] | 8048 | * once compression has occurred, and this optimistic pre-check avoids |
Johannes Weiner | f4840cc | 2022-05-19 14:08:53 -0700 | [diff] [blame] | 8049 | * spending cycles on compression when there is already no room left |
| 8050 | * or zswap is disabled altogether somewhere in the hierarchy. |
| 8051 | */ |
| 8052 | bool obj_cgroup_may_zswap(struct obj_cgroup *objcg) |
| 8053 | { |
| 8054 | struct mem_cgroup *memcg, *original_memcg; |
| 8055 | bool ret = true; |
| 8056 | |
| 8057 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) |
| 8058 | return true; |
| 8059 | |
| 8060 | original_memcg = get_mem_cgroup_from_objcg(objcg); |
Kamalesh Babulal | 7848ed6 | 2022-09-30 19:14:33 +0530 | [diff] [blame] | 8061 | for (memcg = original_memcg; !mem_cgroup_is_root(memcg); |
Johannes Weiner | f4840cc | 2022-05-19 14:08:53 -0700 | [diff] [blame] | 8062 | memcg = parent_mem_cgroup(memcg)) { |
| 8063 | unsigned long max = READ_ONCE(memcg->zswap_max); |
| 8064 | unsigned long pages; |
| 8065 | |
| 8066 | if (max == PAGE_COUNTER_MAX) |
| 8067 | continue; |
| 8068 | if (max == 0) { |
| 8069 | ret = false; |
| 8070 | break; |
| 8071 | } |
| 8072 | |
| 8073 | cgroup_rstat_flush(memcg->css.cgroup); |
| 8074 | pages = memcg_page_state(memcg, MEMCG_ZSWAP_B) / PAGE_SIZE; |
| 8075 | if (pages < max) |
| 8076 | continue; |
| 8077 | ret = false; |
| 8078 | break; |
| 8079 | } |
| 8080 | mem_cgroup_put(original_memcg); |
| 8081 | return ret; |
| 8082 | } |
| 8083 | |
| 8084 | /** |
| 8085 | * obj_cgroup_charge_zswap - charge compression backend memory |
| 8086 | * @objcg: the object cgroup |
| 8087 | * @size: size of compressed object |
| 8088 | * |
Miaohe Lin | 3a1060c | 2023-08-03 20:00:21 +0800 | [diff] [blame] | 8089 | * This forces the charge after obj_cgroup_may_zswap() allowed |
Johannes Weiner | f4840cc | 2022-05-19 14:08:53 -0700 | [diff] [blame] | 8090 | * compression and storage in zwap for this cgroup to go ahead. |
| 8091 | */ |
| 8092 | void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size) |
| 8093 | { |
| 8094 | struct mem_cgroup *memcg; |
| 8095 | |
| 8096 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) |
| 8097 | return; |
| 8098 | |
| 8099 | VM_WARN_ON_ONCE(!(current->flags & PF_MEMALLOC)); |
| 8100 | |
| 8101 | /* PF_MEMALLOC context, charging must succeed */ |
| 8102 | if (obj_cgroup_charge(objcg, GFP_KERNEL, size)) |
| 8103 | VM_WARN_ON_ONCE(1); |
| 8104 | |
| 8105 | rcu_read_lock(); |
| 8106 | memcg = obj_cgroup_memcg(objcg); |
| 8107 | mod_memcg_state(memcg, MEMCG_ZSWAP_B, size); |
| 8108 | mod_memcg_state(memcg, MEMCG_ZSWAPPED, 1); |
| 8109 | rcu_read_unlock(); |
| 8110 | } |
| 8111 | |
| 8112 | /** |
| 8113 | * obj_cgroup_uncharge_zswap - uncharge compression backend memory |
| 8114 | * @objcg: the object cgroup |
| 8115 | * @size: size of compressed object |
| 8116 | * |
| 8117 | * Uncharges zswap memory on page in. |
| 8118 | */ |
| 8119 | void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size) |
| 8120 | { |
| 8121 | struct mem_cgroup *memcg; |
| 8122 | |
| 8123 | if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) |
| 8124 | return; |
| 8125 | |
| 8126 | obj_cgroup_uncharge(objcg, size); |
| 8127 | |
| 8128 | rcu_read_lock(); |
| 8129 | memcg = obj_cgroup_memcg(objcg); |
| 8130 | mod_memcg_state(memcg, MEMCG_ZSWAP_B, -size); |
| 8131 | mod_memcg_state(memcg, MEMCG_ZSWAPPED, -1); |
| 8132 | rcu_read_unlock(); |
| 8133 | } |
| 8134 | |
| 8135 | static u64 zswap_current_read(struct cgroup_subsys_state *css, |
| 8136 | struct cftype *cft) |
| 8137 | { |
| 8138 | cgroup_rstat_flush(css->cgroup); |
| 8139 | return memcg_page_state(mem_cgroup_from_css(css), MEMCG_ZSWAP_B); |
| 8140 | } |
| 8141 | |
| 8142 | static int zswap_max_show(struct seq_file *m, void *v) |
| 8143 | { |
| 8144 | return seq_puts_memcg_tunable(m, |
| 8145 | READ_ONCE(mem_cgroup_from_seq(m)->zswap_max)); |
| 8146 | } |
| 8147 | |
| 8148 | static ssize_t zswap_max_write(struct kernfs_open_file *of, |
| 8149 | char *buf, size_t nbytes, loff_t off) |
| 8150 | { |
| 8151 | struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); |
| 8152 | unsigned long max; |
| 8153 | int err; |
| 8154 | |
| 8155 | buf = strstrip(buf); |
| 8156 | err = page_counter_memparse(buf, "max", &max); |
| 8157 | if (err) |
| 8158 | return err; |
| 8159 | |
| 8160 | xchg(&memcg->zswap_max, max); |
| 8161 | |
| 8162 | return nbytes; |
| 8163 | } |
| 8164 | |
| 8165 | static struct cftype zswap_files[] = { |
| 8166 | { |
| 8167 | .name = "zswap.current", |
| 8168 | .flags = CFTYPE_NOT_ON_ROOT, |
| 8169 | .read_u64 = zswap_current_read, |
| 8170 | }, |
| 8171 | { |
| 8172 | .name = "zswap.max", |
| 8173 | .flags = CFTYPE_NOT_ON_ROOT, |
| 8174 | .seq_show = zswap_max_show, |
| 8175 | .write = zswap_max_write, |
| 8176 | }, |
| 8177 | { } /* terminate */ |
| 8178 | }; |
| 8179 | #endif /* CONFIG_MEMCG_KMEM && CONFIG_ZSWAP */ |
| 8180 | |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 8181 | static int __init mem_cgroup_swap_init(void) |
| 8182 | { |
Johannes Weiner | 2d1c498 | 2020-06-03 16:02:14 -0700 | [diff] [blame] | 8183 | if (mem_cgroup_disabled()) |
Johannes Weiner | eccb52e | 2020-06-03 16:02:11 -0700 | [diff] [blame] | 8184 | return 0; |
| 8185 | |
| 8186 | WARN_ON(cgroup_add_dfl_cftypes(&memory_cgrp_subsys, swap_files)); |
| 8187 | WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, memsw_files)); |
Johannes Weiner | f4840cc | 2022-05-19 14:08:53 -0700 | [diff] [blame] | 8188 | #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) |
| 8189 | WARN_ON(cgroup_add_dfl_cftypes(&memory_cgrp_subsys, zswap_files)); |
| 8190 | #endif |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 8191 | return 0; |
| 8192 | } |
Johannes Weiner | b25806d | 2022-09-26 09:57:02 -0400 | [diff] [blame] | 8193 | subsys_initcall(mem_cgroup_swap_init); |
Johannes Weiner | 21afa38 | 2015-02-11 15:26:36 -0800 | [diff] [blame] | 8194 | |
Johannes Weiner | e55b9f9 | 2022-09-26 09:57:04 -0400 | [diff] [blame] | 8195 | #endif /* CONFIG_SWAP */ |