blob: ce4ce5e7937b9bcf614d66a0c9b68968f6eabf66 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002/* memcontrol.c - Memory Controller
3 *
4 * Copyright IBM Corporation, 2007
5 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
6 *
Pavel Emelianov78fb7462008-02-07 00:13:51 -08007 * Copyright 2007 OpenVZ SWsoft Inc
8 * Author: Pavel Emelianov <xemul@openvz.org>
9 *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080010 * Memory thresholds
11 * Copyright (C) 2009 Nokia Corporation
12 * Author: Kirill A. Shutemov
13 *
Glauber Costa7ae1e1d2012-12-18 14:21:56 -080014 * Kernel Memory Controller
15 * Copyright (C) 2012 Parallels Inc. and Google Inc.
16 * Authors: Glauber Costa and Suleiman Souhlal
17 *
Johannes Weiner1575e682015-04-14 15:44:51 -070018 * 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
Balbir Singh8cdea7c2008-02-07 00:13:50 -080023 */
24
Johannes Weiner3e32cb22014-12-10 15:42:31 -080025#include <linux/page_counter.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080026#include <linux/memcontrol.h>
27#include <linux/cgroup.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080028#include <linux/mm.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010029#include <linux/sched/mm.h>
Hugh Dickins3a4f8a02017-02-24 14:59:36 -080030#include <linux/shmem_fs.h>
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -080031#include <linux/hugetlb.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080032#include <linux/pagemap.h>
Chris Down1ff9e6e2019-03-05 15:48:09 -080033#include <linux/vm_event_item.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080034#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080035#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080036#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080037#include <linux/bit_spinlock.h>
38#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070039#include <linux/limits.h>
Paul Gortmakerb9e15ba2011-05-26 16:00:52 -040040#include <linux/export.h>
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080041#include <linux/mutex.h>
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -070042#include <linux/rbtree.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070043#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080044#include <linux/swap.h>
Daisuke Nishimura02491442010-03-10 15:22:17 -080045#include <linux/swapops.h>
Balbir Singh66e17072008-02-07 00:13:56 -080046#include <linux/spinlock.h>
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080047#include <linux/eventfd.h>
Tejun Heo79bd9812013-11-22 18:20:42 -050048#include <linux/poll.h>
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080049#include <linux/sort.h>
Balbir Singh66e17072008-02-07 00:13:56 -080050#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080051#include <linux/seq_file.h>
Anton Vorontsov70ddf632013-04-29 15:08:31 -070052#include <linux/vmpressure.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070053#include <linux/mm_inline.h>
Johannes Weiner5d1ea482014-12-10 15:44:55 -080054#include <linux/swap_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080055#include <linux/cpu.h>
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -070056#include <linux/oom.h>
Johannes Weiner0056f4e2013-10-31 16:34:14 -070057#include <linux/lockdep.h>
Tejun Heo79bd9812013-11-22 18:20:42 -050058#include <linux/file.h>
Tejun Heob23afb92015-11-05 18:46:11 -080059#include <linux/tracehook.h>
Johannes Weinerc8713d02019-07-11 20:55:59 -070060#include <linux/seq_buf.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080061#include "internal.h"
Glauber Costad1a4c0b2011-12-11 21:47:04 +000062#include <net/sock.h>
Michal Hocko4bd2c1e2012-10-08 16:33:10 -070063#include <net/ip.h>
Qiang Huangf35c3a82013-11-12 15:08:22 -080064#include "slab.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080065
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080066#include <linux/uaccess.h>
Balbir Singh8697d332008-02-07 00:13:59 -080067
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -070068#include <trace/events/vmscan.h>
69
Tejun Heo073219e2014-02-08 10:36:58 -050070struct cgroup_subsys memory_cgrp_subsys __read_mostly;
71EXPORT_SYMBOL(memory_cgrp_subsys);
David Rientjes68ae5642012-12-12 13:51:57 -080072
Johannes Weiner7d828602016-01-14 15:20:56 -080073struct mem_cgroup *root_mem_cgroup __read_mostly;
74
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070075#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh8cdea7c2008-02-07 00:13:50 -080076
Johannes Weinerf7e1cb62016-01-14 15:21:29 -080077/* Socket memory accounting disabled? */
78static bool cgroup_memory_nosocket;
79
Vladimir Davydov04823c82016-01-20 15:02:38 -080080/* Kernel memory accounting disabled? */
81static bool cgroup_memory_nokmem;
82
Johannes Weiner21afa382015-02-11 15:26:36 -080083/* Whether the swap controller is active */
Andrew Mortonc255a452012-07-31 16:43:02 -070084#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080085int do_swap_account __read_mostly;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080086#else
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -070087#define do_swap_account 0
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080088#endif
89
Johannes Weiner7941d212016-01-14 15:21:23 -080090/* Whether legacy memory+swap accounting is active */
91static bool do_memsw_account(void)
92{
93 return !cgroup_subsys_on_dfl(memory_cgrp_subsys) && do_swap_account;
94}
95
Johannes Weiner71cd3112017-05-03 14:55:13 -070096static const char *const mem_cgroup_lru_names[] = {
Sha Zhengju58cf1882013-02-22 16:32:05 -080097 "inactive_anon",
98 "active_anon",
99 "inactive_file",
100 "active_file",
101 "unevictable",
102};
103
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700104#define THRESHOLDS_EVENTS_TARGET 128
105#define SOFTLIMIT_EVENTS_TARGET 1024
106#define NUMAINFO_EVENTS_TARGET 1024
Johannes Weinere9f89742011-03-23 16:42:37 -0700107
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700108/*
109 * Cgroups above their limits are maintained in a RB-Tree, independent of
110 * their hierarchy representation
111 */
112
Mel Gormanef8f2322016-07-28 15:46:05 -0700113struct mem_cgroup_tree_per_node {
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700114 struct rb_root rb_root;
Davidlohr Buesofa90b2f2017-09-08 16:15:21 -0700115 struct rb_node *rb_rightmost;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700116 spinlock_t lock;
117};
118
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700119struct mem_cgroup_tree {
120 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
121};
122
123static struct mem_cgroup_tree soft_limit_tree __read_mostly;
124
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700125/* for OOM */
126struct mem_cgroup_eventfd_list {
127 struct list_head list;
128 struct eventfd_ctx *eventfd;
129};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800130
Tejun Heo79bd9812013-11-22 18:20:42 -0500131/*
132 * cgroup_event represents events which userspace want to receive.
133 */
Tejun Heo3bc942f2013-11-22 18:20:44 -0500134struct mem_cgroup_event {
Tejun Heo79bd9812013-11-22 18:20:42 -0500135 /*
Tejun Heo59b6f872013-11-22 18:20:43 -0500136 * memcg which the event belongs to.
Tejun Heo79bd9812013-11-22 18:20:42 -0500137 */
Tejun Heo59b6f872013-11-22 18:20:43 -0500138 struct mem_cgroup *memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -0500139 /*
Tejun Heo79bd9812013-11-22 18:20:42 -0500140 * 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 Heofba94802013-11-22 18:20:43 -0500148 * 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 Heo59b6f872013-11-22 18:20:43 -0500152 int (*register_event)(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -0500153 struct eventfd_ctx *eventfd, const char *args);
Tejun Heofba94802013-11-22 18:20:43 -0500154 /*
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 Heo59b6f872013-11-22 18:20:43 -0500159 void (*unregister_event)(struct mem_cgroup *memcg,
Tejun Heofba94802013-11-22 18:20:43 -0500160 struct eventfd_ctx *eventfd);
161 /*
Tejun Heo79bd9812013-11-22 18:20:42 -0500162 * All fields below needed to unregister event when
163 * userspace closes eventfd.
164 */
165 poll_table pt;
166 wait_queue_head_t *wqh;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200167 wait_queue_entry_t wait;
Tejun Heo79bd9812013-11-22 18:20:42 -0500168 struct work_struct remove;
169};
170
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700171static void mem_cgroup_threshold(struct mem_cgroup *memcg);
172static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800173
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800174/* Stuffs for move charges at task migration. */
175/*
Johannes Weiner1dfab5a2015-02-11 15:26:09 -0800176 * Types of charges to be moved.
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800177 */
Johannes Weiner1dfab5a2015-02-11 15:26:09 -0800178#define MOVE_ANON 0x1U
179#define MOVE_FILE 0x2U
180#define MOVE_MASK (MOVE_ANON | MOVE_FILE)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800181
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800182/* "mc" and its members are protected by cgroup_mutex */
183static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800184 spinlock_t lock; /* for from, to */
Tejun Heo264a0ae2016-04-21 19:09:02 -0400185 struct mm_struct *mm;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800186 struct mem_cgroup *from;
187 struct mem_cgroup *to;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -0800188 unsigned long flags;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800189 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800190 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800191 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800192 struct task_struct *moving_task; /* a task moving charges */
193 wait_queue_head_t waitq; /* a waitq for other context */
194} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700195 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800196 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
197};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800198
Balbir Singh4e416952009-09-23 15:56:39 -0700199/*
200 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
201 * limit reclaim to prevent infinite loops, if they ever occur.
202 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700203#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700204#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
Balbir Singh4e416952009-09-23 15:56:39 -0700205
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800206enum charge_type {
207 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700208 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800209 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700210 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700211 NR_CHARGE_TYPE,
212};
213
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800214/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800215enum res_type {
216 _MEM,
217 _MEMSWAP,
218 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800219 _KMEM,
Vladimir Davydovd55f90b2016-01-20 15:02:44 -0800220 _TCP,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800221};
222
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700223#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
224#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800225#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700226/* Used for OOM nofiier */
227#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800228
Kirill Tkhaib05706f2018-08-17 15:47:33 -0700229/*
230 * Iteration constructs for visiting all cgroups (under a tree). If
231 * loops are exited prematurely (break), mem_cgroup_iter_break() must
232 * be used for reference counting.
233 */
234#define for_each_mem_cgroup_tree(iter, root) \
235 for (iter = mem_cgroup_iter(root, NULL, NULL); \
236 iter != NULL; \
237 iter = mem_cgroup_iter(root, iter, NULL))
238
239#define for_each_mem_cgroup(iter) \
240 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
241 iter != NULL; \
242 iter = mem_cgroup_iter(NULL, iter, NULL))
243
Tetsuo Handa7775fac2019-03-05 15:46:47 -0800244static inline bool should_force_charge(void)
245{
246 return tsk_is_oom_victim(current) || fatal_signal_pending(current) ||
247 (current->flags & PF_EXITING);
248}
249
Anton Vorontsov70ddf632013-04-29 15:08:31 -0700250/* Some nice accessors for the vmpressure. */
251struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg)
252{
253 if (!memcg)
254 memcg = root_mem_cgroup;
255 return &memcg->vmpressure;
256}
257
258struct cgroup_subsys_state *vmpressure_to_css(struct vmpressure *vmpr)
259{
260 return &container_of(vmpr, struct mem_cgroup, vmpressure)->css;
261}
262
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700263#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -0800264/*
Vladimir Davydovf7ce3192015-02-12 14:59:20 -0800265 * This will be the memcg's index in each cache's ->memcg_params.memcg_caches.
Li Zefanb8627832013-09-23 16:56:47 +0800266 * The main reason for not using cgroup id for this:
267 * this works better in sparse environments, where we have a lot of memcgs,
268 * but only a few kmem-limited. Or also, if we have, for instance, 200
269 * memcgs, and none but the 200th is kmem-limited, we'd have to have a
270 * 200 entry array for that.
Glauber Costa55007d82012-12-18 14:22:38 -0800271 *
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -0800272 * The current size of the caches array is stored in memcg_nr_cache_ids. It
273 * will double each time we have to increase it.
Glauber Costa55007d82012-12-18 14:22:38 -0800274 */
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -0800275static DEFINE_IDA(memcg_cache_ida);
276int memcg_nr_cache_ids;
Glauber Costa749c5412012-12-18 14:23:01 -0800277
Vladimir Davydov05257a12015-02-12 14:59:01 -0800278/* Protects memcg_nr_cache_ids */
279static DECLARE_RWSEM(memcg_cache_ids_sem);
280
281void memcg_get_cache_ids(void)
282{
283 down_read(&memcg_cache_ids_sem);
284}
285
286void memcg_put_cache_ids(void)
287{
288 up_read(&memcg_cache_ids_sem);
289}
290
Glauber Costa55007d82012-12-18 14:22:38 -0800291/*
292 * MIN_SIZE is different than 1, because we would like to avoid going through
293 * the alloc/free process all the time. In a small machine, 4 kmem-limited
294 * cgroups is a reasonable guess. In the future, it could be a parameter or
295 * tunable, but that is strictly not necessary.
296 *
Li Zefanb8627832013-09-23 16:56:47 +0800297 * MAX_SIZE should be as large as the number of cgrp_ids. Ideally, we could get
Glauber Costa55007d82012-12-18 14:22:38 -0800298 * this constant directly from cgroup, but it is understandable that this is
299 * better kept as an internal representation in cgroup.c. In any case, the
Li Zefanb8627832013-09-23 16:56:47 +0800300 * cgrp_id space is not getting any smaller, and we don't have to necessarily
Glauber Costa55007d82012-12-18 14:22:38 -0800301 * increase ours as well if it increases.
302 */
303#define MEMCG_CACHES_MIN_SIZE 4
Li Zefanb8627832013-09-23 16:56:47 +0800304#define MEMCG_CACHES_MAX_SIZE MEM_CGROUP_ID_MAX
Glauber Costa55007d82012-12-18 14:22:38 -0800305
Glauber Costad7f25f82012-12-18 14:22:40 -0800306/*
307 * A lot of the calls to the cache allocation functions are expected to be
308 * inlined by the compiler. Since the calls to memcg_kmem_get_cache are
309 * conditional to this static branch, we'll have to allow modules that does
310 * kmem_cache_alloc and the such to see this symbol as well
311 */
Johannes Weineref129472016-01-14 15:21:34 -0800312DEFINE_STATIC_KEY_FALSE(memcg_kmem_enabled_key);
Glauber Costad7f25f82012-12-18 14:22:40 -0800313EXPORT_SYMBOL(memcg_kmem_enabled_key);
Glauber Costaa8964b92012-12-18 14:22:09 -0800314
Tejun Heo17cc4df2017-02-22 15:41:36 -0800315struct workqueue_struct *memcg_kmem_cache_wq;
316
Kirill Tkhai0a4465d2018-08-17 15:47:37 -0700317static int memcg_shrinker_map_size;
318static DEFINE_MUTEX(memcg_shrinker_map_mutex);
319
320static void memcg_free_shrinker_map_rcu(struct rcu_head *head)
321{
322 kvfree(container_of(head, struct memcg_shrinker_map, rcu));
323}
324
325static int memcg_expand_one_shrinker_map(struct mem_cgroup *memcg,
326 int size, int old_size)
327{
328 struct memcg_shrinker_map *new, *old;
329 int nid;
330
331 lockdep_assert_held(&memcg_shrinker_map_mutex);
332
333 for_each_node(nid) {
334 old = rcu_dereference_protected(
335 mem_cgroup_nodeinfo(memcg, nid)->shrinker_map, true);
336 /* Not yet online memcg */
337 if (!old)
338 return 0;
339
340 new = kvmalloc(sizeof(*new) + size, GFP_KERNEL);
341 if (!new)
342 return -ENOMEM;
343
344 /* Set all old bits, clear all new bits */
345 memset(new->map, (int)0xff, old_size);
346 memset((void *)new->map + old_size, 0, size - old_size);
347
348 rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_map, new);
349 call_rcu(&old->rcu, memcg_free_shrinker_map_rcu);
350 }
351
352 return 0;
353}
354
355static void memcg_free_shrinker_maps(struct mem_cgroup *memcg)
356{
357 struct mem_cgroup_per_node *pn;
358 struct memcg_shrinker_map *map;
359 int nid;
360
361 if (mem_cgroup_is_root(memcg))
362 return;
363
364 for_each_node(nid) {
365 pn = mem_cgroup_nodeinfo(memcg, nid);
366 map = rcu_dereference_protected(pn->shrinker_map, true);
367 if (map)
368 kvfree(map);
369 rcu_assign_pointer(pn->shrinker_map, NULL);
370 }
371}
372
373static int memcg_alloc_shrinker_maps(struct mem_cgroup *memcg)
374{
375 struct memcg_shrinker_map *map;
376 int nid, size, ret = 0;
377
378 if (mem_cgroup_is_root(memcg))
379 return 0;
380
381 mutex_lock(&memcg_shrinker_map_mutex);
382 size = memcg_shrinker_map_size;
383 for_each_node(nid) {
384 map = kvzalloc(sizeof(*map) + size, GFP_KERNEL);
385 if (!map) {
386 memcg_free_shrinker_maps(memcg);
387 ret = -ENOMEM;
388 break;
389 }
390 rcu_assign_pointer(memcg->nodeinfo[nid]->shrinker_map, map);
391 }
392 mutex_unlock(&memcg_shrinker_map_mutex);
393
394 return ret;
395}
396
397int memcg_expand_shrinker_maps(int new_id)
398{
399 int size, old_size, ret = 0;
400 struct mem_cgroup *memcg;
401
402 size = DIV_ROUND_UP(new_id + 1, BITS_PER_LONG) * sizeof(unsigned long);
403 old_size = memcg_shrinker_map_size;
404 if (size <= old_size)
405 return 0;
406
407 mutex_lock(&memcg_shrinker_map_mutex);
408 if (!root_mem_cgroup)
409 goto unlock;
410
411 for_each_mem_cgroup(memcg) {
412 if (mem_cgroup_is_root(memcg))
413 continue;
414 ret = memcg_expand_one_shrinker_map(memcg, size, old_size);
415 if (ret)
416 goto unlock;
417 }
418unlock:
419 if (!ret)
420 memcg_shrinker_map_size = size;
421 mutex_unlock(&memcg_shrinker_map_mutex);
422 return ret;
423}
Kirill Tkhaifae91d62018-08-17 15:48:10 -0700424
425void memcg_set_shrinker_bit(struct mem_cgroup *memcg, int nid, int shrinker_id)
426{
427 if (shrinker_id >= 0 && memcg && !mem_cgroup_is_root(memcg)) {
428 struct memcg_shrinker_map *map;
429
430 rcu_read_lock();
431 map = rcu_dereference(memcg->nodeinfo[nid]->shrinker_map);
Kirill Tkhaif90280d2018-08-17 15:48:25 -0700432 /* Pairs with smp mb in shrink_slab() */
433 smp_mb__before_atomic();
Kirill Tkhaifae91d62018-08-17 15:48:10 -0700434 set_bit(shrinker_id, map->map);
435 rcu_read_unlock();
436 }
437}
438
Kirill Tkhai0a4465d2018-08-17 15:47:37 -0700439#else /* CONFIG_MEMCG_KMEM */
440static int memcg_alloc_shrinker_maps(struct mem_cgroup *memcg)
441{
442 return 0;
443}
444static void memcg_free_shrinker_maps(struct mem_cgroup *memcg) { }
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700445#endif /* CONFIG_MEMCG_KMEM */
Glauber Costaa8964b92012-12-18 14:22:09 -0800446
Tejun Heoad7fa852015-05-27 20:00:02 -0400447/**
448 * mem_cgroup_css_from_page - css of the memcg associated with a page
449 * @page: page of interest
450 *
451 * If memcg is bound to the default hierarchy, css of the memcg associated
452 * with @page is returned. The returned css remains associated with @page
453 * until it is released.
454 *
455 * If memcg is bound to a traditional hierarchy, the css of root_mem_cgroup
456 * is returned.
Tejun Heoad7fa852015-05-27 20:00:02 -0400457 */
458struct cgroup_subsys_state *mem_cgroup_css_from_page(struct page *page)
459{
460 struct mem_cgroup *memcg;
461
Tejun Heoad7fa852015-05-27 20:00:02 -0400462 memcg = page->mem_cgroup;
463
Tejun Heo9e10a132015-09-18 11:56:28 -0400464 if (!memcg || !cgroup_subsys_on_dfl(memory_cgrp_subsys))
Tejun Heoad7fa852015-05-27 20:00:02 -0400465 memcg = root_mem_cgroup;
466
Tejun Heoad7fa852015-05-27 20:00:02 -0400467 return &memcg->css;
468}
469
Vladimir Davydov2fc04522015-09-09 15:35:28 -0700470/**
471 * page_cgroup_ino - return inode number of the memcg a page is charged to
472 * @page: the page
473 *
474 * Look up the closest online ancestor of the memory cgroup @page is charged to
475 * and return its inode number or 0 if @page is not charged to any cgroup. It
476 * is safe to call this function without holding a reference to @page.
477 *
478 * Note, this function is inherently racy, because there is nothing to prevent
479 * the cgroup inode from getting torn down and potentially reallocated a moment
480 * after page_cgroup_ino() returns, so it only should be used by callers that
481 * do not care (such as procfs interfaces).
482 */
483ino_t page_cgroup_ino(struct page *page)
484{
485 struct mem_cgroup *memcg;
486 unsigned long ino = 0;
487
488 rcu_read_lock();
489 memcg = READ_ONCE(page->mem_cgroup);
490 while (memcg && !(memcg->css.flags & CSS_ONLINE))
491 memcg = parent_mem_cgroup(memcg);
492 if (memcg)
493 ino = cgroup_ino(memcg->css.cgroup);
494 rcu_read_unlock();
495 return ino;
496}
497
Mel Gormanef8f2322016-07-28 15:46:05 -0700498static struct mem_cgroup_per_node *
499mem_cgroup_page_nodeinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700500{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700501 int nid = page_to_nid(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700502
Mel Gormanef8f2322016-07-28 15:46:05 -0700503 return memcg->nodeinfo[nid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700504}
505
Mel Gormanef8f2322016-07-28 15:46:05 -0700506static struct mem_cgroup_tree_per_node *
507soft_limit_tree_node(int nid)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700508{
Mel Gormanef8f2322016-07-28 15:46:05 -0700509 return soft_limit_tree.rb_tree_per_node[nid];
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700510}
511
Mel Gormanef8f2322016-07-28 15:46:05 -0700512static struct mem_cgroup_tree_per_node *
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700513soft_limit_tree_from_page(struct page *page)
514{
515 int nid = page_to_nid(page);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700516
Mel Gormanef8f2322016-07-28 15:46:05 -0700517 return soft_limit_tree.rb_tree_per_node[nid];
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700518}
519
Mel Gormanef8f2322016-07-28 15:46:05 -0700520static void __mem_cgroup_insert_exceeded(struct mem_cgroup_per_node *mz,
521 struct mem_cgroup_tree_per_node *mctz,
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800522 unsigned long new_usage_in_excess)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700523{
524 struct rb_node **p = &mctz->rb_root.rb_node;
525 struct rb_node *parent = NULL;
Mel Gormanef8f2322016-07-28 15:46:05 -0700526 struct mem_cgroup_per_node *mz_node;
Davidlohr Buesofa90b2f2017-09-08 16:15:21 -0700527 bool rightmost = true;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700528
529 if (mz->on_tree)
530 return;
531
532 mz->usage_in_excess = new_usage_in_excess;
533 if (!mz->usage_in_excess)
534 return;
535 while (*p) {
536 parent = *p;
Mel Gormanef8f2322016-07-28 15:46:05 -0700537 mz_node = rb_entry(parent, struct mem_cgroup_per_node,
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700538 tree_node);
Davidlohr Buesofa90b2f2017-09-08 16:15:21 -0700539 if (mz->usage_in_excess < mz_node->usage_in_excess) {
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700540 p = &(*p)->rb_left;
Davidlohr Buesofa90b2f2017-09-08 16:15:21 -0700541 rightmost = false;
542 }
543
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700544 /*
545 * We can't avoid mem cgroups that are over their soft
546 * limit by the same amount
547 */
548 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
549 p = &(*p)->rb_right;
550 }
Davidlohr Buesofa90b2f2017-09-08 16:15:21 -0700551
552 if (rightmost)
553 mctz->rb_rightmost = &mz->tree_node;
554
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700555 rb_link_node(&mz->tree_node, parent, p);
556 rb_insert_color(&mz->tree_node, &mctz->rb_root);
557 mz->on_tree = true;
558}
559
Mel Gormanef8f2322016-07-28 15:46:05 -0700560static void __mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz,
561 struct mem_cgroup_tree_per_node *mctz)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700562{
563 if (!mz->on_tree)
564 return;
Davidlohr Buesofa90b2f2017-09-08 16:15:21 -0700565
566 if (&mz->tree_node == mctz->rb_rightmost)
567 mctz->rb_rightmost = rb_prev(&mz->tree_node);
568
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700569 rb_erase(&mz->tree_node, &mctz->rb_root);
570 mz->on_tree = false;
571}
572
Mel Gormanef8f2322016-07-28 15:46:05 -0700573static void mem_cgroup_remove_exceeded(struct mem_cgroup_per_node *mz,
574 struct mem_cgroup_tree_per_node *mctz)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700575{
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700576 unsigned long flags;
577
578 spin_lock_irqsave(&mctz->lock, flags);
Johannes Weinercf2c8122014-06-06 14:38:21 -0700579 __mem_cgroup_remove_exceeded(mz, mctz);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700580 spin_unlock_irqrestore(&mctz->lock, flags);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700581}
582
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800583static unsigned long soft_limit_excess(struct mem_cgroup *memcg)
584{
585 unsigned long nr_pages = page_counter_read(&memcg->memory);
Jason Low4db0c3c2015-04-15 16:14:08 -0700586 unsigned long soft_limit = READ_ONCE(memcg->soft_limit);
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800587 unsigned long excess = 0;
588
589 if (nr_pages > soft_limit)
590 excess = nr_pages - soft_limit;
591
592 return excess;
593}
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700594
595static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
596{
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800597 unsigned long excess;
Mel Gormanef8f2322016-07-28 15:46:05 -0700598 struct mem_cgroup_per_node *mz;
599 struct mem_cgroup_tree_per_node *mctz;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700600
Jianyu Zhane2318752014-06-06 14:38:20 -0700601 mctz = soft_limit_tree_from_page(page);
Laurent Dufourbfc72282017-03-09 16:17:06 -0800602 if (!mctz)
603 return;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700604 /*
605 * Necessary to update all ancestors when hierarchy is used.
606 * because their event counter is not touched.
607 */
608 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
Mel Gormanef8f2322016-07-28 15:46:05 -0700609 mz = mem_cgroup_page_nodeinfo(memcg, page);
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800610 excess = soft_limit_excess(memcg);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700611 /*
612 * We have to update the tree if mz is on RB-tree or
613 * mem is over its softlimit.
614 */
615 if (excess || mz->on_tree) {
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700616 unsigned long flags;
617
618 spin_lock_irqsave(&mctz->lock, flags);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700619 /* if on-tree, remove it */
620 if (mz->on_tree)
Johannes Weinercf2c8122014-06-06 14:38:21 -0700621 __mem_cgroup_remove_exceeded(mz, mctz);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700622 /*
623 * Insert again. mz->usage_in_excess will be updated.
624 * If excess is 0, no tree ops.
625 */
Johannes Weinercf2c8122014-06-06 14:38:21 -0700626 __mem_cgroup_insert_exceeded(mz, mctz, excess);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700627 spin_unlock_irqrestore(&mctz->lock, flags);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700628 }
629 }
630}
631
632static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
633{
Mel Gormanef8f2322016-07-28 15:46:05 -0700634 struct mem_cgroup_tree_per_node *mctz;
635 struct mem_cgroup_per_node *mz;
636 int nid;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700637
Jianyu Zhane2318752014-06-06 14:38:20 -0700638 for_each_node(nid) {
Mel Gormanef8f2322016-07-28 15:46:05 -0700639 mz = mem_cgroup_nodeinfo(memcg, nid);
640 mctz = soft_limit_tree_node(nid);
Laurent Dufourbfc72282017-03-09 16:17:06 -0800641 if (mctz)
642 mem_cgroup_remove_exceeded(mz, mctz);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700643 }
644}
645
Mel Gormanef8f2322016-07-28 15:46:05 -0700646static struct mem_cgroup_per_node *
647__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700648{
Mel Gormanef8f2322016-07-28 15:46:05 -0700649 struct mem_cgroup_per_node *mz;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700650
651retry:
652 mz = NULL;
Davidlohr Buesofa90b2f2017-09-08 16:15:21 -0700653 if (!mctz->rb_rightmost)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700654 goto done; /* Nothing to reclaim from */
655
Davidlohr Buesofa90b2f2017-09-08 16:15:21 -0700656 mz = rb_entry(mctz->rb_rightmost,
657 struct mem_cgroup_per_node, tree_node);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700658 /*
659 * Remove the node now but someone else can add it back,
660 * we will to add it back at the end of reclaim to its correct
661 * position in the tree.
662 */
Johannes Weinercf2c8122014-06-06 14:38:21 -0700663 __mem_cgroup_remove_exceeded(mz, mctz);
Johannes Weiner3e32cb22014-12-10 15:42:31 -0800664 if (!soft_limit_excess(mz->memcg) ||
Tejun Heoec903c02014-05-13 12:11:01 -0400665 !css_tryget_online(&mz->memcg->css))
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700666 goto retry;
667done:
668 return mz;
669}
670
Mel Gormanef8f2322016-07-28 15:46:05 -0700671static struct mem_cgroup_per_node *
672mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz)
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700673{
Mel Gormanef8f2322016-07-28 15:46:05 -0700674 struct mem_cgroup_per_node *mz;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700675
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700676 spin_lock_irq(&mctz->lock);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700677 mz = __mem_cgroup_largest_soft_limit_node(mctz);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700678 spin_unlock_irq(&mctz->lock);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700679 return mz;
680}
681
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700682/**
683 * __mod_memcg_state - update cgroup memory statistics
684 * @memcg: the memory cgroup
685 * @idx: the stat item - can be enum memcg_stat_item or enum node_stat_item
686 * @val: delta to add to the counter, can be negative
687 */
688void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val)
689{
690 long x;
691
692 if (mem_cgroup_disabled())
693 return;
694
Johannes Weiner815744d2019-06-13 15:55:46 -0700695 __this_cpu_add(memcg->vmstats_local->stat[idx], val);
696
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700697 x = val + __this_cpu_read(memcg->vmstats_percpu->stat[idx]);
698 if (unlikely(abs(x) > MEMCG_CHARGE_BATCH)) {
Johannes Weiner42a30032019-05-14 15:47:12 -0700699 struct mem_cgroup *mi;
700
Johannes Weiner42a30032019-05-14 15:47:12 -0700701 for (mi = memcg; mi; mi = parent_mem_cgroup(mi))
702 atomic_long_add(x, &mi->vmstats[idx]);
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700703 x = 0;
704 }
705 __this_cpu_write(memcg->vmstats_percpu->stat[idx], x);
706}
707
Johannes Weiner42a30032019-05-14 15:47:12 -0700708static struct mem_cgroup_per_node *
709parent_nodeinfo(struct mem_cgroup_per_node *pn, int nid)
710{
711 struct mem_cgroup *parent;
712
713 parent = parent_mem_cgroup(pn->memcg);
714 if (!parent)
715 return NULL;
716 return mem_cgroup_nodeinfo(parent, nid);
717}
718
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700719/**
720 * __mod_lruvec_state - update lruvec memory statistics
721 * @lruvec: the lruvec
722 * @idx: the stat item
723 * @val: delta to add to the counter, can be negative
724 *
725 * The lruvec is the intersection of the NUMA node and a cgroup. This
726 * function updates the all three counters that are affected by a
727 * change of state at this level: per-node, per-cgroup, per-lruvec.
728 */
729void __mod_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx,
730 int val)
731{
Johannes Weiner42a30032019-05-14 15:47:12 -0700732 pg_data_t *pgdat = lruvec_pgdat(lruvec);
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700733 struct mem_cgroup_per_node *pn;
Johannes Weiner42a30032019-05-14 15:47:12 -0700734 struct mem_cgroup *memcg;
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700735 long x;
736
737 /* Update node */
Johannes Weiner42a30032019-05-14 15:47:12 -0700738 __mod_node_page_state(pgdat, idx, val);
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700739
740 if (mem_cgroup_disabled())
741 return;
742
743 pn = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
Johannes Weiner42a30032019-05-14 15:47:12 -0700744 memcg = pn->memcg;
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700745
746 /* Update memcg */
Johannes Weiner42a30032019-05-14 15:47:12 -0700747 __mod_memcg_state(memcg, idx, val);
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700748
749 /* Update lruvec */
Johannes Weiner815744d2019-06-13 15:55:46 -0700750 __this_cpu_add(pn->lruvec_stat_local->count[idx], val);
751
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700752 x = val + __this_cpu_read(pn->lruvec_stat_cpu->count[idx]);
753 if (unlikely(abs(x) > MEMCG_CHARGE_BATCH)) {
Johannes Weiner42a30032019-05-14 15:47:12 -0700754 struct mem_cgroup_per_node *pi;
755
Johannes Weiner42a30032019-05-14 15:47:12 -0700756 for (pi = pn; pi; pi = parent_nodeinfo(pi, pgdat->node_id))
757 atomic_long_add(x, &pi->lruvec_stat[idx]);
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700758 x = 0;
759 }
760 __this_cpu_write(pn->lruvec_stat_cpu->count[idx], x);
761}
762
763/**
764 * __count_memcg_events - account VM events in a cgroup
765 * @memcg: the memory cgroup
766 * @idx: the event item
767 * @count: the number of events that occured
768 */
769void __count_memcg_events(struct mem_cgroup *memcg, enum vm_event_item idx,
770 unsigned long count)
771{
772 unsigned long x;
773
774 if (mem_cgroup_disabled())
775 return;
776
Johannes Weiner815744d2019-06-13 15:55:46 -0700777 __this_cpu_add(memcg->vmstats_local->events[idx], count);
778
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700779 x = count + __this_cpu_read(memcg->vmstats_percpu->events[idx]);
780 if (unlikely(x > MEMCG_CHARGE_BATCH)) {
Johannes Weiner42a30032019-05-14 15:47:12 -0700781 struct mem_cgroup *mi;
782
Johannes Weiner42a30032019-05-14 15:47:12 -0700783 for (mi = memcg; mi; mi = parent_mem_cgroup(mi))
784 atomic_long_add(x, &mi->vmevents[idx]);
Johannes Weinerdb9adbc2019-05-14 15:47:09 -0700785 x = 0;
786 }
787 __this_cpu_write(memcg->vmstats_percpu->events[idx], x);
788}
789
Johannes Weiner42a30032019-05-14 15:47:12 -0700790static unsigned long memcg_events(struct mem_cgroup *memcg, int event)
Johannes Weinere9f89742011-03-23 16:42:37 -0700791{
Chris Down871789d2019-05-14 15:46:57 -0700792 return atomic_long_read(&memcg->vmevents[event]);
Johannes Weinere9f89742011-03-23 16:42:37 -0700793}
794
Johannes Weiner42a30032019-05-14 15:47:12 -0700795static unsigned long memcg_events_local(struct mem_cgroup *memcg, int event)
796{
Johannes Weiner815744d2019-06-13 15:55:46 -0700797 long x = 0;
798 int cpu;
799
800 for_each_possible_cpu(cpu)
801 x += per_cpu(memcg->vmstats_local->events[event], cpu);
802 return x;
Johannes Weiner42a30032019-05-14 15:47:12 -0700803}
804
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700805static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
David Rientjesb070e652013-05-07 16:18:09 -0700806 struct page *page,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800807 bool compound, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800808{
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700809 /*
810 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
811 * counted as CACHE even if it's on ANON LRU.
812 */
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700813 if (PageAnon(page))
Johannes Weinerc9019e92018-01-31 16:16:37 -0800814 __mod_memcg_state(memcg, MEMCG_RSS, nr_pages);
Johannes Weiner9a4caf12017-05-03 14:52:45 -0700815 else {
Johannes Weinerc9019e92018-01-31 16:16:37 -0800816 __mod_memcg_state(memcg, MEMCG_CACHE, nr_pages);
Johannes Weiner9a4caf12017-05-03 14:52:45 -0700817 if (PageSwapBacked(page))
Johannes Weinerc9019e92018-01-31 16:16:37 -0800818 __mod_memcg_state(memcg, NR_SHMEM, nr_pages);
Johannes Weiner9a4caf12017-05-03 14:52:45 -0700819 }
Balaji Rao55e462b2008-05-01 04:35:12 -0700820
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800821 if (compound) {
822 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
Johannes Weinerc9019e92018-01-31 16:16:37 -0800823 __mod_memcg_state(memcg, MEMCG_RSS_HUGE, nr_pages);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800824 }
David Rientjesb070e652013-05-07 16:18:09 -0700825
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800826 /* pagein of a big page is an event. So, ignore page size */
827 if (nr_pages > 0)
Johannes Weinerc9019e92018-01-31 16:16:37 -0800828 __count_memcg_events(memcg, PGPGIN, 1);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800829 else {
Johannes Weinerc9019e92018-01-31 16:16:37 -0800830 __count_memcg_events(memcg, PGPGOUT, 1);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800831 nr_pages = -nr_pages; /* for event */
832 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800833
Chris Down871789d2019-05-14 15:46:57 -0700834 __this_cpu_add(memcg->vmstats_percpu->nr_page_events, nr_pages);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800835}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800836
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800837static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
838 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800839{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700840 unsigned long val, next;
841
Chris Down871789d2019-05-14 15:46:57 -0700842 val = __this_cpu_read(memcg->vmstats_percpu->nr_page_events);
843 next = __this_cpu_read(memcg->vmstats_percpu->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700844 /* from time_after() in jiffies.h */
Michal Hocko6a1a8b82017-07-10 15:48:53 -0700845 if ((long)(next - val) < 0) {
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800846 switch (target) {
847 case MEM_CGROUP_TARGET_THRESH:
848 next = val + THRESHOLDS_EVENTS_TARGET;
849 break;
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700850 case MEM_CGROUP_TARGET_SOFTLIMIT:
851 next = val + SOFTLIMIT_EVENTS_TARGET;
852 break;
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800853 case MEM_CGROUP_TARGET_NUMAINFO:
854 next = val + NUMAINFO_EVENTS_TARGET;
855 break;
856 default:
857 break;
858 }
Chris Down871789d2019-05-14 15:46:57 -0700859 __this_cpu_write(memcg->vmstats_percpu->targets[target], next);
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800860 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700861 }
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800862 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800863}
864
865/*
866 * Check events in order.
867 *
868 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700869static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800870{
871 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800872 if (unlikely(mem_cgroup_event_ratelimit(memcg,
873 MEM_CGROUP_TARGET_THRESH))) {
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700874 bool do_softlimit;
Andrew Morton82b3f2a2012-02-03 15:37:14 -0800875 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800876
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700877 do_softlimit = mem_cgroup_event_ratelimit(memcg,
878 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700879#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800880 do_numainfo = mem_cgroup_event_ratelimit(memcg,
881 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700882#endif
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800883 mem_cgroup_threshold(memcg);
Andrew Mortonbb4cc1a2013-09-24 15:27:40 -0700884 if (unlikely(do_softlimit))
885 mem_cgroup_update_tree(memcg, page);
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800886#if MAX_NUMNODES > 1
887 if (unlikely(do_numainfo))
888 atomic_inc(&memcg->numainfo_events);
889#endif
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700890 }
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800891}
892
Balbir Singhcf475ad2008-04-29 01:00:16 -0700893struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800894{
Balbir Singh31a78f22008-09-28 23:09:31 +0100895 /*
896 * mm_update_next_owner() may clear mm->owner to NULL
897 * if it races with swapoff, page migration, etc.
898 * So this can be called with p == NULL.
899 */
900 if (unlikely(!p))
901 return NULL;
902
Tejun Heo073219e2014-02-08 10:36:58 -0500903 return mem_cgroup_from_css(task_css(p, memory_cgrp_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800904}
Michal Hocko33398cf2015-09-08 15:01:02 -0700905EXPORT_SYMBOL(mem_cgroup_from_task);
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800906
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700907/**
908 * get_mem_cgroup_from_mm: Obtain a reference on given mm_struct's memcg.
909 * @mm: mm from which memcg should be extracted. It can be NULL.
910 *
911 * Obtain a reference on mm->memcg and returns it if successful. Otherwise
912 * root_mem_cgroup is returned. However if mem_cgroup is disabled, NULL is
913 * returned.
914 */
915struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800916{
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700917 struct mem_cgroup *memcg;
918
919 if (mem_cgroup_disabled())
920 return NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700921
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800922 rcu_read_lock();
923 do {
Michal Hocko6f6acb02014-05-22 11:54:19 -0700924 /*
925 * Page cache insertions can happen withou an
926 * actual mm context, e.g. during disk probing
927 * on boot, loopback IO, acct() writes etc.
928 */
929 if (unlikely(!mm))
Johannes Weinerdf381972014-04-07 15:37:43 -0700930 memcg = root_mem_cgroup;
Michal Hocko6f6acb02014-05-22 11:54:19 -0700931 else {
932 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
933 if (unlikely(!memcg))
934 memcg = root_mem_cgroup;
935 }
Tejun Heoec903c02014-05-13 12:11:01 -0400936 } while (!css_tryget_online(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800937 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700938 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800939}
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700940EXPORT_SYMBOL(get_mem_cgroup_from_mm);
941
942/**
Shakeel Buttf745c6f2018-08-17 15:46:44 -0700943 * get_mem_cgroup_from_page: Obtain a reference on given page's memcg.
944 * @page: page from which memcg should be extracted.
945 *
946 * Obtain a reference on page->memcg and returns it if successful. Otherwise
947 * root_mem_cgroup is returned.
948 */
949struct mem_cgroup *get_mem_cgroup_from_page(struct page *page)
950{
951 struct mem_cgroup *memcg = page->mem_cgroup;
952
953 if (mem_cgroup_disabled())
954 return NULL;
955
956 rcu_read_lock();
957 if (!memcg || !css_tryget_online(&memcg->css))
958 memcg = root_mem_cgroup;
959 rcu_read_unlock();
960 return memcg;
961}
962EXPORT_SYMBOL(get_mem_cgroup_from_page);
963
964/**
Shakeel Buttd46eb14b2018-08-17 15:46:39 -0700965 * If current->active_memcg is non-NULL, do not fallback to current->mm->memcg.
966 */
967static __always_inline struct mem_cgroup *get_mem_cgroup_from_current(void)
968{
969 if (unlikely(current->active_memcg)) {
970 struct mem_cgroup *memcg = root_mem_cgroup;
971
972 rcu_read_lock();
973 if (css_tryget_online(&current->active_memcg->css))
974 memcg = current->active_memcg;
975 rcu_read_unlock();
976 return memcg;
977 }
978 return get_mem_cgroup_from_mm(current->mm);
979}
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800980
Johannes Weiner56600482012-01-12 17:17:59 -0800981/**
982 * mem_cgroup_iter - iterate over memory cgroup hierarchy
983 * @root: hierarchy root
984 * @prev: previously returned memcg, NULL on first invocation
985 * @reclaim: cookie for shared reclaim walks, NULL for full walks
986 *
987 * Returns references to children of the hierarchy below @root, or
988 * @root itself, or %NULL after a full round-trip.
989 *
990 * Caller must pass the return value in @prev on subsequent
991 * invocations for reference counting, or use mem_cgroup_iter_break()
992 * to cancel a hierarchy walk before the round-trip is complete.
993 *
Honglei Wangb213b542018-03-28 16:01:12 -0700994 * Reclaimers can specify a node and a priority level in @reclaim to
Johannes Weiner56600482012-01-12 17:17:59 -0800995 * divide up the memcgs in the hierarchy among all concurrent
Honglei Wangb213b542018-03-28 16:01:12 -0700996 * reclaimers operating on the same node and priority.
Johannes Weiner56600482012-01-12 17:17:59 -0800997 */
Andrew Morton694fbc02013-09-24 15:27:37 -0700998struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
Johannes Weiner56600482012-01-12 17:17:59 -0800999 struct mem_cgroup *prev,
Andrew Morton694fbc02013-09-24 15:27:37 -07001000 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001001{
Michal Hocko33398cf2015-09-08 15:01:02 -07001002 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001003 struct cgroup_subsys_state *css = NULL;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001004 struct mem_cgroup *memcg = NULL;
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001005 struct mem_cgroup *pos = NULL;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001006
Andrew Morton694fbc02013-09-24 15:27:37 -07001007 if (mem_cgroup_disabled())
1008 return NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001009
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001010 if (!root)
1011 root = root_mem_cgroup;
1012
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001013 if (prev && !reclaim)
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001014 pos = prev;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001015
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001016 if (!root->use_hierarchy && root != root_mem_cgroup) {
1017 if (prev)
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001018 goto out;
Andrew Morton694fbc02013-09-24 15:27:37 -07001019 return root;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001020 }
1021
Michal Hocko542f85f2013-04-29 15:07:15 -07001022 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001023
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001024 if (reclaim) {
Mel Gormanef8f2322016-07-28 15:46:05 -07001025 struct mem_cgroup_per_node *mz;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001026
Mel Gormanef8f2322016-07-28 15:46:05 -07001027 mz = mem_cgroup_nodeinfo(root, reclaim->pgdat->node_id);
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001028 iter = &mz->iter[reclaim->priority];
Michal Hocko5f578162013-04-29 15:07:17 -07001029
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001030 if (prev && reclaim->generation != iter->generation)
Michal Hocko542f85f2013-04-29 15:07:15 -07001031 goto out_unlock;
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001032
Vladimir Davydov6df38682015-12-29 14:54:10 -08001033 while (1) {
Jason Low4db0c3c2015-04-15 16:14:08 -07001034 pos = READ_ONCE(iter->position);
Vladimir Davydov6df38682015-12-29 14:54:10 -08001035 if (!pos || css_tryget(&pos->css))
1036 break;
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001037 /*
Vladimir Davydov6df38682015-12-29 14:54:10 -08001038 * css reference reached zero, so iter->position will
1039 * be cleared by ->css_released. However, we should not
1040 * rely on this happening soon, because ->css_released
1041 * is called from a work queue, and by busy-waiting we
1042 * might block it. So we clear iter->position right
1043 * away.
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001044 */
Vladimir Davydov6df38682015-12-29 14:54:10 -08001045 (void)cmpxchg(&iter->position, pos, NULL);
1046 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001047 }
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001048
1049 if (pos)
1050 css = &pos->css;
1051
1052 for (;;) {
1053 css = css_next_descendant_pre(css, &root->css);
1054 if (!css) {
1055 /*
1056 * Reclaimers share the hierarchy walk, and a
1057 * new one might jump in right at the end of
1058 * the hierarchy - make sure they see at least
1059 * one group and restart from the beginning.
1060 */
1061 if (!prev)
1062 continue;
1063 break;
1064 }
1065
1066 /*
1067 * Verify the css and acquire a reference. The root
1068 * is provided by the caller, so we know it's alive
1069 * and kicking, and don't take an extra reference.
1070 */
1071 memcg = mem_cgroup_from_css(css);
1072
1073 if (css == &root->css)
1074 break;
1075
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08001076 if (css_tryget(css))
1077 break;
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001078
1079 memcg = NULL;
1080 }
1081
1082 if (reclaim) {
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001083 /*
Vladimir Davydov6df38682015-12-29 14:54:10 -08001084 * The position could have already been updated by a competing
1085 * thread, so check that the value hasn't changed since we read
1086 * it to avoid reclaiming from the same cgroup twice.
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001087 */
Vladimir Davydov6df38682015-12-29 14:54:10 -08001088 (void)cmpxchg(&iter->position, pos, memcg);
1089
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001090 if (pos)
1091 css_put(&pos->css);
1092
1093 if (!memcg)
1094 iter->generation++;
1095 else if (!prev)
1096 reclaim->generation = iter->generation;
1097 }
1098
Michal Hocko542f85f2013-04-29 15:07:15 -07001099out_unlock:
1100 rcu_read_unlock();
Johannes Weiner5ac8fb32014-12-10 15:42:39 -08001101out:
Michal Hockoc40046f2013-04-29 15:07:14 -07001102 if (prev && prev != root)
1103 css_put(&prev->css);
1104
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001105 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001106}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001107
Johannes Weiner56600482012-01-12 17:17:59 -08001108/**
1109 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1110 * @root: hierarchy root
1111 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1112 */
1113void mem_cgroup_iter_break(struct mem_cgroup *root,
1114 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001115{
1116 if (!root)
1117 root = root_mem_cgroup;
1118 if (prev && prev != root)
1119 css_put(&prev->css);
1120}
1121
Vladimir Davydov6df38682015-12-29 14:54:10 -08001122static void invalidate_reclaim_iterators(struct mem_cgroup *dead_memcg)
1123{
1124 struct mem_cgroup *memcg = dead_memcg;
1125 struct mem_cgroup_reclaim_iter *iter;
Mel Gormanef8f2322016-07-28 15:46:05 -07001126 struct mem_cgroup_per_node *mz;
1127 int nid;
Vladimir Davydov6df38682015-12-29 14:54:10 -08001128 int i;
1129
Jing Xia9f15bde2018-07-20 17:53:48 -07001130 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
Vladimir Davydov6df38682015-12-29 14:54:10 -08001131 for_each_node(nid) {
Mel Gormanef8f2322016-07-28 15:46:05 -07001132 mz = mem_cgroup_nodeinfo(memcg, nid);
1133 for (i = 0; i <= DEF_PRIORITY; i++) {
1134 iter = &mz->iter[i];
1135 cmpxchg(&iter->position,
1136 dead_memcg, NULL);
Vladimir Davydov6df38682015-12-29 14:54:10 -08001137 }
1138 }
1139 }
1140}
1141
Johannes Weiner925b7672012-01-12 17:18:15 -08001142/**
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001143 * mem_cgroup_scan_tasks - iterate over tasks of a memory cgroup hierarchy
1144 * @memcg: hierarchy root
1145 * @fn: function to call for each task
1146 * @arg: argument passed to @fn
1147 *
1148 * This function iterates over tasks attached to @memcg or to any of its
1149 * descendants and calls @fn for each task. If @fn returns a non-zero
1150 * value, the function breaks the iteration loop and returns the value.
1151 * Otherwise, it will iterate over all tasks and return 0.
1152 *
1153 * This function must not be called for the root memory cgroup.
1154 */
1155int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
1156 int (*fn)(struct task_struct *, void *), void *arg)
1157{
1158 struct mem_cgroup *iter;
1159 int ret = 0;
1160
1161 BUG_ON(memcg == root_mem_cgroup);
1162
1163 for_each_mem_cgroup_tree(iter, memcg) {
1164 struct css_task_iter it;
1165 struct task_struct *task;
1166
Tejun Heobc2fb7e2017-05-15 09:34:01 -04001167 css_task_iter_start(&iter->css, 0, &it);
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001168 while (!ret && (task = css_task_iter_next(&it)))
1169 ret = fn(task, arg);
1170 css_task_iter_end(&it);
1171 if (ret) {
1172 mem_cgroup_iter_break(memcg, iter);
1173 break;
1174 }
1175 }
1176 return ret;
1177}
1178
1179/**
Johannes Weinerdfe0e772014-12-10 15:43:43 -08001180 * mem_cgroup_page_lruvec - return lruvec for isolating/putting an LRU page
Johannes Weiner925b7672012-01-12 17:18:15 -08001181 * @page: the page
Mike Rapoportf144c392018-02-06 15:42:16 -08001182 * @pgdat: pgdat of the page
Johannes Weinerdfe0e772014-12-10 15:43:43 -08001183 *
1184 * This function is only safe when following the LRU page isolation
1185 * and putback protocol: the LRU lock must be held, and the page must
1186 * either be PageLRU() or the caller must have isolated/allocated it.
Minchan Kim3f58a822011-03-22 16:32:53 -07001187 */
Mel Gorman599d0c92016-07-28 15:45:31 -07001188struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct pglist_data *pgdat)
Minchan Kim3f58a822011-03-22 16:32:53 -07001189{
Mel Gormanef8f2322016-07-28 15:46:05 -07001190 struct mem_cgroup_per_node *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001191 struct mem_cgroup *memcg;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001192 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001193
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001194 if (mem_cgroup_disabled()) {
Mel Gorman599d0c92016-07-28 15:45:31 -07001195 lruvec = &pgdat->lruvec;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001196 goto out;
1197 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001198
Johannes Weiner1306a852014-12-10 15:44:52 -08001199 memcg = page->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001200 /*
Johannes Weinerdfe0e772014-12-10 15:43:43 -08001201 * Swapcache readahead pages are added to the LRU - and
Johannes Weiner29833312014-12-10 15:44:02 -08001202 * possibly migrated - before they are charged.
Hugh Dickins75121022012-03-05 14:59:18 -08001203 */
Johannes Weiner29833312014-12-10 15:44:02 -08001204 if (!memcg)
1205 memcg = root_mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001206
Mel Gormanef8f2322016-07-28 15:46:05 -07001207 mz = mem_cgroup_page_nodeinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001208 lruvec = &mz->lruvec;
1209out:
1210 /*
1211 * Since a node can be onlined after the mem_cgroup was created,
1212 * we have to be prepared to initialize lruvec->zone here;
1213 * and if offlined then reonlined, we need to reinitialize it.
1214 */
Mel Gorman599d0c92016-07-28 15:45:31 -07001215 if (unlikely(lruvec->pgdat != pgdat))
1216 lruvec->pgdat = pgdat;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001217 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001218}
1219
1220/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001221 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1222 * @lruvec: mem_cgroup per zone lru vector
1223 * @lru: index of lru list the page is sitting on
Michal Hockob4536f0c82017-01-10 16:58:04 -08001224 * @zid: zone id of the accounted pages
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001225 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001226 *
Hugh Dickinsca707232016-05-19 17:12:35 -07001227 * This function must be called under lru_lock, just before a page is added
1228 * to or just after a page is removed from an lru list (that ordering being
1229 * so as to allow it to check that lru_size 0 is consistent with list_empty).
Johannes Weiner925b7672012-01-12 17:18:15 -08001230 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001231void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
Michal Hockob4536f0c82017-01-10 16:58:04 -08001232 int zid, int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001233{
Mel Gormanef8f2322016-07-28 15:46:05 -07001234 struct mem_cgroup_per_node *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001235 unsigned long *lru_size;
Hugh Dickinsca707232016-05-19 17:12:35 -07001236 long size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001237
1238 if (mem_cgroup_disabled())
1239 return;
1240
Mel Gormanef8f2322016-07-28 15:46:05 -07001241 mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec);
Michal Hockob4536f0c82017-01-10 16:58:04 -08001242 lru_size = &mz->lru_zone_size[zid][lru];
Hugh Dickinsca707232016-05-19 17:12:35 -07001243
1244 if (nr_pages < 0)
1245 *lru_size += nr_pages;
1246
1247 size = *lru_size;
Michal Hockob4536f0c82017-01-10 16:58:04 -08001248 if (WARN_ONCE(size < 0,
1249 "%s(%p, %d, %d): lru_size %ld\n",
1250 __func__, lruvec, lru, nr_pages, size)) {
Hugh Dickinsca707232016-05-19 17:12:35 -07001251 VM_BUG_ON(1);
1252 *lru_size = 0;
1253 }
1254
1255 if (nr_pages > 0)
1256 *lru_size += nr_pages;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001257}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001258
Johannes Weiner2314b422014-12-10 15:44:33 -08001259bool task_in_mem_cgroup(struct task_struct *task, struct mem_cgroup *memcg)
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001260{
Johannes Weiner2314b422014-12-10 15:44:33 -08001261 struct mem_cgroup *task_memcg;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001262 struct task_struct *p;
David Rientjesffbdccf2013-07-03 15:01:23 -07001263 bool ret;
David Rientjes4c4a2212008-02-07 00:14:06 -08001264
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001265 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001266 if (p) {
Johannes Weiner2314b422014-12-10 15:44:33 -08001267 task_memcg = get_mem_cgroup_from_mm(p->mm);
David Rientjesde077d22012-01-12 17:18:52 -08001268 task_unlock(p);
1269 } else {
1270 /*
1271 * All threads may have already detached their mm's, but the oom
1272 * killer still needs to detect if they have already been oom
1273 * killed to prevent needlessly killing additional tasks.
1274 */
David Rientjesffbdccf2013-07-03 15:01:23 -07001275 rcu_read_lock();
Johannes Weiner2314b422014-12-10 15:44:33 -08001276 task_memcg = mem_cgroup_from_task(task);
1277 css_get(&task_memcg->css);
David Rientjesffbdccf2013-07-03 15:01:23 -07001278 rcu_read_unlock();
David Rientjesde077d22012-01-12 17:18:52 -08001279 }
Johannes Weiner2314b422014-12-10 15:44:33 -08001280 ret = mem_cgroup_is_descendant(task_memcg, memcg);
1281 css_put(&task_memcg->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001282 return ret;
1283}
1284
Johannes Weiner19942822011-02-01 15:52:43 -08001285/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001286 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001287 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001288 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001289 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001290 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001291 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001292static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001293{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001294 unsigned long margin = 0;
1295 unsigned long count;
1296 unsigned long limit;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001297
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001298 count = page_counter_read(&memcg->memory);
Roman Gushchinbbec2e12018-06-07 17:06:18 -07001299 limit = READ_ONCE(memcg->memory.max);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001300 if (count < limit)
1301 margin = limit - count;
1302
Johannes Weiner7941d212016-01-14 15:21:23 -08001303 if (do_memsw_account()) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001304 count = page_counter_read(&memcg->memsw);
Roman Gushchinbbec2e12018-06-07 17:06:18 -07001305 limit = READ_ONCE(memcg->memsw.max);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001306 if (count <= limit)
1307 margin = min(margin, limit - count);
Li RongQingcbedbac2016-05-27 14:27:43 -07001308 else
1309 margin = 0;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001310 }
1311
1312 return margin;
Johannes Weiner19942822011-02-01 15:52:43 -08001313}
1314
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001315/*
Qiang Huangbdcbb652014-06-04 16:08:21 -07001316 * A routine for checking "mem" is under move_account() or not.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001317 *
Qiang Huangbdcbb652014-06-04 16:08:21 -07001318 * Checking a cgroup is mc.from or mc.to or under hierarchy of
1319 * moving cgroups. This is for waiting at high-memory pressure
1320 * caused by "move".
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001321 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001322static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001323{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001324 struct mem_cgroup *from;
1325 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001326 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001327 /*
1328 * Unlike task_move routines, we access mc.to, mc.from not under
1329 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1330 */
1331 spin_lock(&mc.lock);
1332 from = mc.from;
1333 to = mc.to;
1334 if (!from)
1335 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001336
Johannes Weiner2314b422014-12-10 15:44:33 -08001337 ret = mem_cgroup_is_descendant(from, memcg) ||
1338 mem_cgroup_is_descendant(to, memcg);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001339unlock:
1340 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001341 return ret;
1342}
1343
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001344static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001345{
1346 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001347 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001348 DEFINE_WAIT(wait);
1349 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1350 /* moving charge context might have finished. */
1351 if (mc.moving_task)
1352 schedule();
1353 finish_wait(&mc.waitq, &wait);
1354 return true;
1355 }
1356 }
1357 return false;
1358}
1359
Johannes Weinerc8713d02019-07-11 20:55:59 -07001360static char *memory_stat_format(struct mem_cgroup *memcg)
1361{
1362 struct seq_buf s;
1363 int i;
Johannes Weiner71cd3112017-05-03 14:55:13 -07001364
Johannes Weinerc8713d02019-07-11 20:55:59 -07001365 seq_buf_init(&s, kmalloc(PAGE_SIZE, GFP_KERNEL), PAGE_SIZE);
1366 if (!s.buffer)
1367 return NULL;
1368
1369 /*
1370 * Provide statistics on the state of the memory subsystem as
1371 * well as cumulative event counters that show past behavior.
1372 *
1373 * This list is ordered following a combination of these gradients:
1374 * 1) generic big picture -> specifics and details
1375 * 2) reflecting userspace activity -> reflecting kernel heuristics
1376 *
1377 * Current memory state:
1378 */
1379
1380 seq_buf_printf(&s, "anon %llu\n",
1381 (u64)memcg_page_state(memcg, MEMCG_RSS) *
1382 PAGE_SIZE);
1383 seq_buf_printf(&s, "file %llu\n",
1384 (u64)memcg_page_state(memcg, MEMCG_CACHE) *
1385 PAGE_SIZE);
1386 seq_buf_printf(&s, "kernel_stack %llu\n",
1387 (u64)memcg_page_state(memcg, MEMCG_KERNEL_STACK_KB) *
1388 1024);
1389 seq_buf_printf(&s, "slab %llu\n",
1390 (u64)(memcg_page_state(memcg, NR_SLAB_RECLAIMABLE) +
1391 memcg_page_state(memcg, NR_SLAB_UNRECLAIMABLE)) *
1392 PAGE_SIZE);
1393 seq_buf_printf(&s, "sock %llu\n",
1394 (u64)memcg_page_state(memcg, MEMCG_SOCK) *
1395 PAGE_SIZE);
1396
1397 seq_buf_printf(&s, "shmem %llu\n",
1398 (u64)memcg_page_state(memcg, NR_SHMEM) *
1399 PAGE_SIZE);
1400 seq_buf_printf(&s, "file_mapped %llu\n",
1401 (u64)memcg_page_state(memcg, NR_FILE_MAPPED) *
1402 PAGE_SIZE);
1403 seq_buf_printf(&s, "file_dirty %llu\n",
1404 (u64)memcg_page_state(memcg, NR_FILE_DIRTY) *
1405 PAGE_SIZE);
1406 seq_buf_printf(&s, "file_writeback %llu\n",
1407 (u64)memcg_page_state(memcg, NR_WRITEBACK) *
1408 PAGE_SIZE);
1409
1410 /*
1411 * TODO: We should eventually replace our own MEMCG_RSS_HUGE counter
1412 * with the NR_ANON_THP vm counter, but right now it's a pain in the
1413 * arse because it requires migrating the work out of rmap to a place
1414 * where the page->mem_cgroup is set up and stable.
1415 */
1416 seq_buf_printf(&s, "anon_thp %llu\n",
1417 (u64)memcg_page_state(memcg, MEMCG_RSS_HUGE) *
1418 PAGE_SIZE);
1419
1420 for (i = 0; i < NR_LRU_LISTS; i++)
1421 seq_buf_printf(&s, "%s %llu\n", mem_cgroup_lru_names[i],
1422 (u64)memcg_page_state(memcg, NR_LRU_BASE + i) *
1423 PAGE_SIZE);
1424
1425 seq_buf_printf(&s, "slab_reclaimable %llu\n",
1426 (u64)memcg_page_state(memcg, NR_SLAB_RECLAIMABLE) *
1427 PAGE_SIZE);
1428 seq_buf_printf(&s, "slab_unreclaimable %llu\n",
1429 (u64)memcg_page_state(memcg, NR_SLAB_UNRECLAIMABLE) *
1430 PAGE_SIZE);
1431
1432 /* Accumulated memory events */
1433
1434 seq_buf_printf(&s, "pgfault %lu\n", memcg_events(memcg, PGFAULT));
1435 seq_buf_printf(&s, "pgmajfault %lu\n", memcg_events(memcg, PGMAJFAULT));
1436
1437 seq_buf_printf(&s, "workingset_refault %lu\n",
1438 memcg_page_state(memcg, WORKINGSET_REFAULT));
1439 seq_buf_printf(&s, "workingset_activate %lu\n",
1440 memcg_page_state(memcg, WORKINGSET_ACTIVATE));
1441 seq_buf_printf(&s, "workingset_nodereclaim %lu\n",
1442 memcg_page_state(memcg, WORKINGSET_NODERECLAIM));
1443
1444 seq_buf_printf(&s, "pgrefill %lu\n", memcg_events(memcg, PGREFILL));
1445 seq_buf_printf(&s, "pgscan %lu\n",
1446 memcg_events(memcg, PGSCAN_KSWAPD) +
1447 memcg_events(memcg, PGSCAN_DIRECT));
1448 seq_buf_printf(&s, "pgsteal %lu\n",
1449 memcg_events(memcg, PGSTEAL_KSWAPD) +
1450 memcg_events(memcg, PGSTEAL_DIRECT));
1451 seq_buf_printf(&s, "pgactivate %lu\n", memcg_events(memcg, PGACTIVATE));
1452 seq_buf_printf(&s, "pgdeactivate %lu\n", memcg_events(memcg, PGDEACTIVATE));
1453 seq_buf_printf(&s, "pglazyfree %lu\n", memcg_events(memcg, PGLAZYFREE));
1454 seq_buf_printf(&s, "pglazyfreed %lu\n", memcg_events(memcg, PGLAZYFREED));
1455
1456#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1457 seq_buf_printf(&s, "thp_fault_alloc %lu\n",
1458 memcg_events(memcg, THP_FAULT_ALLOC));
1459 seq_buf_printf(&s, "thp_collapse_alloc %lu\n",
1460 memcg_events(memcg, THP_COLLAPSE_ALLOC));
1461#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1462
1463 /* The above should easily fit into one page */
1464 WARN_ON_ONCE(seq_buf_has_overflowed(&s));
1465
1466 return s.buffer;
1467}
Johannes Weiner71cd3112017-05-03 14:55:13 -07001468
Sha Zhengju58cf1882013-02-22 16:32:05 -08001469#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001470/**
yuzhoujianf0c867d2018-12-28 00:36:10 -08001471 * mem_cgroup_print_oom_context: Print OOM information relevant to
1472 * memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001473 * @memcg: The memory cgroup that went over limit
1474 * @p: Task that is going to be killed
1475 *
1476 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1477 * enabled
1478 */
yuzhoujianf0c867d2018-12-28 00:36:10 -08001479void mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *p)
1480{
1481 rcu_read_lock();
1482
1483 if (memcg) {
1484 pr_cont(",oom_memcg=");
1485 pr_cont_cgroup_path(memcg->css.cgroup);
1486 } else
1487 pr_cont(",global_oom");
1488 if (p) {
1489 pr_cont(",task_memcg=");
1490 pr_cont_cgroup_path(task_cgroup(p, memory_cgrp_id));
1491 }
1492 rcu_read_unlock();
1493}
1494
1495/**
1496 * mem_cgroup_print_oom_meminfo: Print OOM memory information relevant to
1497 * memory controller.
1498 * @memcg: The memory cgroup that went over limit
1499 */
1500void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
Balbir Singhe2224322009-04-02 16:57:39 -07001501{
Johannes Weinerc8713d02019-07-11 20:55:59 -07001502 char *buf;
Balbir Singhe2224322009-04-02 16:57:39 -07001503
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001504 pr_info("memory: usage %llukB, limit %llukB, failcnt %lu\n",
1505 K((u64)page_counter_read(&memcg->memory)),
Roman Gushchinbbec2e12018-06-07 17:06:18 -07001506 K((u64)memcg->memory.max), memcg->memory.failcnt);
Johannes Weinerc8713d02019-07-11 20:55:59 -07001507 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
1508 pr_info("swap: usage %llukB, limit %llukB, failcnt %lu\n",
1509 K((u64)page_counter_read(&memcg->swap)),
1510 K((u64)memcg->swap.max), memcg->swap.failcnt);
1511 else {
1512 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %lu\n",
1513 K((u64)page_counter_read(&memcg->memsw)),
1514 K((u64)memcg->memsw.max), memcg->memsw.failcnt);
1515 pr_info("kmem: usage %llukB, limit %llukB, failcnt %lu\n",
1516 K((u64)page_counter_read(&memcg->kmem)),
1517 K((u64)memcg->kmem.max), memcg->kmem.failcnt);
Sha Zhengju58cf1882013-02-22 16:32:05 -08001518 }
Johannes Weinerc8713d02019-07-11 20:55:59 -07001519
1520 pr_info("Memory cgroup stats for ");
1521 pr_cont_cgroup_path(memcg->css.cgroup);
1522 pr_cont(":");
1523 buf = memory_stat_format(memcg);
1524 if (!buf)
1525 return;
1526 pr_info("%s", buf);
1527 kfree(buf);
Balbir Singhe2224322009-04-02 16:57:39 -07001528}
1529
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001530/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001531 * Return the memory (and swap, if configured) limit for a memcg.
1532 */
Roman Gushchinbbec2e12018-06-07 17:06:18 -07001533unsigned long mem_cgroup_get_max(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001534{
Roman Gushchinbbec2e12018-06-07 17:06:18 -07001535 unsigned long max;
David Rientjesa63d83f2010-08-09 17:19:46 -07001536
Roman Gushchinbbec2e12018-06-07 17:06:18 -07001537 max = memcg->memory.max;
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001538 if (mem_cgroup_swappiness(memcg)) {
Roman Gushchinbbec2e12018-06-07 17:06:18 -07001539 unsigned long memsw_max;
1540 unsigned long swap_max;
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001541
Roman Gushchinbbec2e12018-06-07 17:06:18 -07001542 memsw_max = memcg->memsw.max;
1543 swap_max = memcg->swap.max;
1544 swap_max = min(swap_max, (unsigned long)total_swap_pages);
1545 max = min(max + swap_max, memsw_max);
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001546 }
Roman Gushchinbbec2e12018-06-07 17:06:18 -07001547 return max;
David Rientjesa63d83f2010-08-09 17:19:46 -07001548}
1549
Johannes Weinerb6e6edc2016-03-17 14:20:28 -07001550static bool mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
David Rientjes19965462012-12-11 16:00:26 -08001551 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001552{
David Rientjes6e0fc462015-09-08 15:00:36 -07001553 struct oom_control oc = {
1554 .zonelist = NULL,
1555 .nodemask = NULL,
Vladimir Davydov2a966b72016-07-26 15:22:33 -07001556 .memcg = memcg,
David Rientjes6e0fc462015-09-08 15:00:36 -07001557 .gfp_mask = gfp_mask,
1558 .order = order,
David Rientjes6e0fc462015-09-08 15:00:36 -07001559 };
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001560 bool ret;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001561
Tetsuo Handa7775fac2019-03-05 15:46:47 -08001562 if (mutex_lock_killable(&oom_lock))
1563 return true;
1564 /*
1565 * A few threads which were not waiting at mutex_lock_killable() can
1566 * fail to bail out. Therefore, check again after holding oom_lock.
1567 */
1568 ret = should_force_charge() || out_of_memory(&oc);
Johannes Weinerdc564012015-06-24 16:57:19 -07001569 mutex_unlock(&oom_lock);
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001570 return ret;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001571}
1572
Michele Curtiae6e71d2014-12-12 16:56:35 -08001573#if MAX_NUMNODES > 1
1574
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001575/**
1576 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001577 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001578 * @nid: the node ID to be checked.
1579 * @noswap : specify true here if the user wants flle only information.
1580 *
1581 * This function returns whether the specified memcg contains any
1582 * reclaimable pages on a node. Returns true if there are any reclaimable
1583 * pages in the node.
1584 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001585static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001586 int nid, bool noswap)
1587{
Johannes Weiner2b487e52019-05-13 17:18:05 -07001588 struct lruvec *lruvec = mem_cgroup_lruvec(NODE_DATA(nid), memcg);
1589
Johannes Weinerdef0fda2019-05-14 15:47:15 -07001590 if (lruvec_page_state(lruvec, NR_INACTIVE_FILE) ||
1591 lruvec_page_state(lruvec, NR_ACTIVE_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001592 return true;
1593 if (noswap || !total_swap_pages)
1594 return false;
Johannes Weinerdef0fda2019-05-14 15:47:15 -07001595 if (lruvec_page_state(lruvec, NR_INACTIVE_ANON) ||
1596 lruvec_page_state(lruvec, NR_ACTIVE_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001597 return true;
1598 return false;
1599
1600}
Ying Han889976d2011-05-26 16:25:33 -07001601
1602/*
1603 * Always updating the nodemask is not very good - even if we have an empty
1604 * list or the wrong list here, we can start from some node and traverse all
1605 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1606 *
1607 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001608static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001609{
1610 int nid;
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001611 /*
1612 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1613 * pagein/pageout changes since the last update.
1614 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001615 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001616 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001617 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001618 return;
1619
Ying Han889976d2011-05-26 16:25:33 -07001620 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001621 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001622
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001623 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001624
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001625 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1626 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001627 }
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001628
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001629 atomic_set(&memcg->numainfo_events, 0);
1630 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001631}
1632
1633/*
1634 * Selecting a node where we start reclaim from. Because what we need is just
1635 * reducing usage counter, start from anywhere is O,K. Considering
1636 * memory reclaim from current node, there are pros. and cons.
1637 *
1638 * Freeing memory from current node means freeing memory from a node which
1639 * we'll use or we've used. So, it may make LRU bad. And if several threads
1640 * hit limits, it will see a contention on a node. But freeing from remote
1641 * node means more costs for memory reclaim because of memory latency.
1642 *
1643 * Now, we use round-robin. Better algorithm is welcomed.
1644 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001645int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001646{
1647 int node;
1648
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001649 mem_cgroup_may_update_nodemask(memcg);
1650 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001651
Andrew Morton0edaf862016-05-19 17:10:58 -07001652 node = next_node_in(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001653 /*
Michal Hockofda3d692016-05-19 17:11:34 -07001654 * mem_cgroup_may_update_nodemask might have seen no reclaimmable pages
1655 * last time it really checked all the LRUs due to rate limiting.
1656 * Fallback to the current node in that case for simplicity.
Ying Han889976d2011-05-26 16:25:33 -07001657 */
1658 if (unlikely(node == MAX_NUMNODES))
1659 node = numa_node_id();
1660
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001661 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001662 return node;
1663}
Ying Han889976d2011-05-26 16:25:33 -07001664#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001665int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001666{
1667 return 0;
1668}
1669#endif
1670
Andrew Morton0608f432013-09-24 15:27:41 -07001671static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
Mel Gormanef8f2322016-07-28 15:46:05 -07001672 pg_data_t *pgdat,
Andrew Morton0608f432013-09-24 15:27:41 -07001673 gfp_t gfp_mask,
1674 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001675{
Andrew Morton0608f432013-09-24 15:27:41 -07001676 struct mem_cgroup *victim = NULL;
1677 int total = 0;
1678 int loop = 0;
1679 unsigned long excess;
1680 unsigned long nr_scanned;
1681 struct mem_cgroup_reclaim_cookie reclaim = {
Mel Gormanef8f2322016-07-28 15:46:05 -07001682 .pgdat = pgdat,
Andrew Morton0608f432013-09-24 15:27:41 -07001683 .priority = 0,
1684 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001685
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001686 excess = soft_limit_excess(root_memcg);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001687
Andrew Morton0608f432013-09-24 15:27:41 -07001688 while (1) {
1689 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
1690 if (!victim) {
1691 loop++;
1692 if (loop >= 2) {
1693 /*
1694 * If we have not been able to reclaim
1695 * anything, it might because there are
1696 * no reclaimable pages under this hierarchy
1697 */
1698 if (!total)
1699 break;
1700 /*
1701 * We want to do more targeted reclaim.
1702 * excess >> 2 is not to excessive so as to
1703 * reclaim too much, nor too less that we keep
1704 * coming back to reclaim from this cgroup
1705 */
1706 if (total >= (excess >> 2) ||
1707 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
1708 break;
1709 }
1710 continue;
1711 }
Mel Gormana9dd0a82016-07-28 15:46:02 -07001712 total += mem_cgroup_shrink_node(victim, gfp_mask, false,
Mel Gormanef8f2322016-07-28 15:46:05 -07001713 pgdat, &nr_scanned);
Andrew Morton0608f432013-09-24 15:27:41 -07001714 *total_scanned += nr_scanned;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08001715 if (!soft_limit_excess(root_memcg))
Andrew Morton0608f432013-09-24 15:27:41 -07001716 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001717 }
Andrew Morton0608f432013-09-24 15:27:41 -07001718 mem_cgroup_iter_break(root_memcg, victim);
1719 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001720}
1721
Johannes Weiner0056f4e2013-10-31 16:34:14 -07001722#ifdef CONFIG_LOCKDEP
1723static struct lockdep_map memcg_oom_lock_dep_map = {
1724 .name = "memcg_oom_lock",
1725};
1726#endif
1727
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001728static DEFINE_SPINLOCK(memcg_oom_lock);
1729
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001730/*
1731 * Check OOM-Killer is already running under our hierarchy.
1732 * If someone is running, return false.
1733 */
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001734static bool mem_cgroup_oom_trylock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001735{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001736 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001737
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001738 spin_lock(&memcg_oom_lock);
1739
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001740 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001741 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001742 /*
1743 * this subtree of our hierarchy is already locked
1744 * so we cannot give a lock.
1745 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001746 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001747 mem_cgroup_iter_break(memcg, iter);
1748 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001749 } else
1750 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001751 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001752
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001753 if (failed) {
1754 /*
1755 * OK, we failed to lock the whole subtree so we have
1756 * to clean up what we set up to the failing subtree
1757 */
1758 for_each_mem_cgroup_tree(iter, memcg) {
1759 if (iter == failed) {
1760 mem_cgroup_iter_break(memcg, iter);
1761 break;
1762 }
1763 iter->oom_lock = false;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001764 }
Johannes Weiner0056f4e2013-10-31 16:34:14 -07001765 } else
1766 mutex_acquire(&memcg_oom_lock_dep_map, 0, 1, _RET_IP_);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001767
1768 spin_unlock(&memcg_oom_lock);
1769
1770 return !failed;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001771}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001772
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001773static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001774{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001775 struct mem_cgroup *iter;
1776
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001777 spin_lock(&memcg_oom_lock);
Johannes Weiner0056f4e2013-10-31 16:34:14 -07001778 mutex_release(&memcg_oom_lock_dep_map, 1, _RET_IP_);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001779 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001780 iter->oom_lock = false;
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001781 spin_unlock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001782}
1783
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001784static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001785{
1786 struct mem_cgroup *iter;
1787
Tejun Heoc2b42d32015-06-24 16:58:23 -07001788 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001789 for_each_mem_cgroup_tree(iter, memcg)
Tejun Heoc2b42d32015-06-24 16:58:23 -07001790 iter->under_oom++;
1791 spin_unlock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001792}
1793
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001794static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001795{
1796 struct mem_cgroup *iter;
1797
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001798 /*
1799 * When a new child is created while the hierarchy is under oom,
Tejun Heoc2b42d32015-06-24 16:58:23 -07001800 * mem_cgroup_oom_lock() may not be called. Watch for underflow.
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001801 */
Tejun Heoc2b42d32015-06-24 16:58:23 -07001802 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001803 for_each_mem_cgroup_tree(iter, memcg)
Tejun Heoc2b42d32015-06-24 16:58:23 -07001804 if (iter->under_oom > 0)
1805 iter->under_oom--;
1806 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001807}
1808
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001809static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1810
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001811struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001812 struct mem_cgroup *memcg;
Ingo Molnarac6424b2017-06-20 12:06:13 +02001813 wait_queue_entry_t wait;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001814};
1815
Ingo Molnarac6424b2017-06-20 12:06:13 +02001816static int memcg_oom_wake_function(wait_queue_entry_t *wait,
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001817 unsigned mode, int sync, void *arg)
1818{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001819 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
1820 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001821 struct oom_wait_info *oom_wait_info;
1822
1823 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001824 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001825
Johannes Weiner2314b422014-12-10 15:44:33 -08001826 if (!mem_cgroup_is_descendant(wake_memcg, oom_wait_memcg) &&
1827 !mem_cgroup_is_descendant(oom_wait_memcg, wake_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001828 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001829 return autoremove_wake_function(wait, mode, sync, arg);
1830}
1831
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001832static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001833{
Tejun Heoc2b42d32015-06-24 16:58:23 -07001834 /*
1835 * For the following lockless ->under_oom test, the only required
1836 * guarantee is that it must see the state asserted by an OOM when
1837 * this function is called as a result of userland actions
1838 * triggered by the notification of the OOM. This is trivially
1839 * achieved by invoking mem_cgroup_mark_under_oom() before
1840 * triggering notification.
1841 */
1842 if (memcg && memcg->under_oom)
Tejun Heof4b90b702015-06-24 16:58:21 -07001843 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001844}
1845
Michal Hocko29ef6802018-08-17 15:47:11 -07001846enum oom_status {
1847 OOM_SUCCESS,
1848 OOM_FAILED,
1849 OOM_ASYNC,
1850 OOM_SKIPPED
1851};
1852
1853static enum oom_status mem_cgroup_oom(struct mem_cgroup *memcg, gfp_t mask, int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001854{
Michal Hocko7056d3a2018-12-28 00:39:57 -08001855 enum oom_status ret;
1856 bool locked;
1857
Michal Hocko29ef6802018-08-17 15:47:11 -07001858 if (order > PAGE_ALLOC_COSTLY_ORDER)
1859 return OOM_SKIPPED;
1860
Roman Gushchin7a1adfd2018-10-26 15:09:48 -07001861 memcg_memory_event(memcg, MEMCG_OOM);
1862
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001863 /*
Johannes Weiner49426422013-10-16 13:46:59 -07001864 * We are in the middle of the charge context here, so we
1865 * don't want to block when potentially sitting on a callstack
1866 * that holds all kinds of filesystem and mm locks.
1867 *
Michal Hocko29ef6802018-08-17 15:47:11 -07001868 * cgroup1 allows disabling the OOM killer and waiting for outside
1869 * handling until the charge can succeed; remember the context and put
1870 * the task to sleep at the end of the page fault when all locks are
1871 * released.
Johannes Weiner49426422013-10-16 13:46:59 -07001872 *
Michal Hocko29ef6802018-08-17 15:47:11 -07001873 * On the other hand, in-kernel OOM killer allows for an async victim
1874 * memory reclaim (oom_reaper) and that means that we are not solely
1875 * relying on the oom victim to make a forward progress and we can
1876 * invoke the oom killer here.
1877 *
1878 * Please note that mem_cgroup_out_of_memory might fail to find a
1879 * victim and then we have to bail out from the charge path.
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001880 */
Michal Hocko29ef6802018-08-17 15:47:11 -07001881 if (memcg->oom_kill_disable) {
1882 if (!current->in_user_fault)
1883 return OOM_SKIPPED;
1884 css_get(&memcg->css);
1885 current->memcg_in_oom = memcg;
1886 current->memcg_oom_gfp_mask = mask;
1887 current->memcg_oom_order = order;
1888
1889 return OOM_ASYNC;
1890 }
1891
Michal Hocko7056d3a2018-12-28 00:39:57 -08001892 mem_cgroup_mark_under_oom(memcg);
Michal Hocko29ef6802018-08-17 15:47:11 -07001893
Michal Hocko7056d3a2018-12-28 00:39:57 -08001894 locked = mem_cgroup_oom_trylock(memcg);
1895
1896 if (locked)
1897 mem_cgroup_oom_notify(memcg);
1898
1899 mem_cgroup_unmark_under_oom(memcg);
1900 if (mem_cgroup_out_of_memory(memcg, mask, order))
1901 ret = OOM_SUCCESS;
1902 else
1903 ret = OOM_FAILED;
1904
1905 if (locked)
1906 mem_cgroup_oom_unlock(memcg);
1907
1908 return ret;
Johannes Weiner49426422013-10-16 13:46:59 -07001909}
1910
1911/**
1912 * mem_cgroup_oom_synchronize - complete memcg OOM handling
1913 * @handle: actually kill/wait or just clean up the OOM state
1914 *
1915 * This has to be called at the end of a page fault if the memcg OOM
1916 * handler was enabled.
1917 *
1918 * Memcg supports userspace OOM handling where failed allocations must
1919 * sleep on a waitqueue until the userspace task resolves the
1920 * situation. Sleeping directly in the charge context with all kinds
1921 * of locks held is not a good idea, instead we remember an OOM state
1922 * in the task and mem_cgroup_oom_synchronize() has to be called at
1923 * the end of the page fault to complete the OOM handling.
1924 *
1925 * Returns %true if an ongoing memcg OOM situation was detected and
1926 * completed, %false otherwise.
1927 */
1928bool mem_cgroup_oom_synchronize(bool handle)
1929{
Tejun Heo626ebc42015-11-05 18:46:09 -08001930 struct mem_cgroup *memcg = current->memcg_in_oom;
Johannes Weiner49426422013-10-16 13:46:59 -07001931 struct oom_wait_info owait;
1932 bool locked;
1933
1934 /* OOM is global, do not handle */
1935 if (!memcg)
1936 return false;
1937
Vladimir Davydov7c5f64f2016-10-07 16:57:23 -07001938 if (!handle)
Johannes Weiner49426422013-10-16 13:46:59 -07001939 goto cleanup;
1940
1941 owait.memcg = memcg;
1942 owait.wait.flags = 0;
1943 owait.wait.func = memcg_oom_wake_function;
1944 owait.wait.private = current;
Ingo Molnar2055da92017-06-20 12:06:46 +02001945 INIT_LIST_HEAD(&owait.wait.entry);
Johannes Weiner49426422013-10-16 13:46:59 -07001946
1947 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001948 mem_cgroup_mark_under_oom(memcg);
1949
1950 locked = mem_cgroup_oom_trylock(memcg);
1951
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001952 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001953 mem_cgroup_oom_notify(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001954
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001955 if (locked && !memcg->oom_kill_disable) {
1956 mem_cgroup_unmark_under_oom(memcg);
Johannes Weiner49426422013-10-16 13:46:59 -07001957 finish_wait(&memcg_oom_waitq, &owait.wait);
Tejun Heo626ebc42015-11-05 18:46:09 -08001958 mem_cgroup_out_of_memory(memcg, current->memcg_oom_gfp_mask,
1959 current->memcg_oom_order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001960 } else {
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001961 schedule();
Johannes Weiner49426422013-10-16 13:46:59 -07001962 mem_cgroup_unmark_under_oom(memcg);
1963 finish_wait(&memcg_oom_waitq, &owait.wait);
1964 }
1965
1966 if (locked) {
Johannes Weinerfb2a6fc2013-09-12 15:13:43 -07001967 mem_cgroup_oom_unlock(memcg);
1968 /*
1969 * There is no guarantee that an OOM-lock contender
1970 * sees the wakeups triggered by the OOM kill
1971 * uncharges. Wake any sleepers explicitely.
1972 */
1973 memcg_oom_recover(memcg);
1974 }
Johannes Weiner49426422013-10-16 13:46:59 -07001975cleanup:
Tejun Heo626ebc42015-11-05 18:46:09 -08001976 current->memcg_in_oom = NULL;
Johannes Weiner3812c8c2013-09-12 15:13:44 -07001977 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001978 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001979}
1980
Johannes Weinerd7365e72014-10-29 14:50:48 -07001981/**
Roman Gushchin3d8b38e2018-08-21 21:53:54 -07001982 * mem_cgroup_get_oom_group - get a memory cgroup to clean up after OOM
1983 * @victim: task to be killed by the OOM killer
1984 * @oom_domain: memcg in case of memcg OOM, NULL in case of system-wide OOM
1985 *
1986 * Returns a pointer to a memory cgroup, which has to be cleaned up
1987 * by killing all belonging OOM-killable tasks.
1988 *
1989 * Caller has to call mem_cgroup_put() on the returned non-NULL memcg.
1990 */
1991struct mem_cgroup *mem_cgroup_get_oom_group(struct task_struct *victim,
1992 struct mem_cgroup *oom_domain)
1993{
1994 struct mem_cgroup *oom_group = NULL;
1995 struct mem_cgroup *memcg;
1996
1997 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
1998 return NULL;
1999
2000 if (!oom_domain)
2001 oom_domain = root_mem_cgroup;
2002
2003 rcu_read_lock();
2004
2005 memcg = mem_cgroup_from_task(victim);
2006 if (memcg == root_mem_cgroup)
2007 goto out;
2008
2009 /*
2010 * Traverse the memory cgroup hierarchy from the victim task's
2011 * cgroup up to the OOMing cgroup (or root) to find the
2012 * highest-level memory cgroup with oom.group set.
2013 */
2014 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
2015 if (memcg->oom_group)
2016 oom_group = memcg;
2017
2018 if (memcg == oom_domain)
2019 break;
2020 }
2021
2022 if (oom_group)
2023 css_get(&oom_group->css);
2024out:
2025 rcu_read_unlock();
2026
2027 return oom_group;
2028}
2029
2030void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)
2031{
2032 pr_info("Tasks in ");
2033 pr_cont_cgroup_path(memcg->css.cgroup);
2034 pr_cont(" are going to be killed due to memory.oom.group set\n");
2035}
2036
2037/**
Johannes Weiner81f8c3a2016-03-15 14:57:04 -07002038 * lock_page_memcg - lock a page->mem_cgroup binding
2039 * @page: the page
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002040 *
Johannes Weiner81f8c3a2016-03-15 14:57:04 -07002041 * This function protects unlocked LRU pages from being moved to
Johannes Weiner739f79f2017-08-18 15:15:48 -07002042 * another cgroup.
2043 *
2044 * It ensures lifetime of the returned memcg. Caller is responsible
2045 * for the lifetime of the page; __unlock_page_memcg() is available
2046 * when @page might get freed inside the locked section.
Balbir Singhd69b0422009-06-17 16:26:34 -07002047 */
Johannes Weiner739f79f2017-08-18 15:15:48 -07002048struct mem_cgroup *lock_page_memcg(struct page *page)
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002049{
2050 struct mem_cgroup *memcg;
Johannes Weiner6de22612015-02-11 15:25:01 -08002051 unsigned long flags;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002052
Johannes Weiner6de22612015-02-11 15:25:01 -08002053 /*
2054 * The RCU lock is held throughout the transaction. The fast
2055 * path can get away without acquiring the memcg->move_lock
2056 * because page moving starts with an RCU grace period.
Johannes Weiner739f79f2017-08-18 15:15:48 -07002057 *
2058 * The RCU lock also protects the memcg from being freed when
2059 * the page state that is going to change is the only thing
2060 * preventing the page itself from being freed. E.g. writeback
2061 * doesn't hold a page reference and relies on PG_writeback to
2062 * keep off truncation, migration and so forth.
2063 */
Johannes Weinerd7365e72014-10-29 14:50:48 -07002064 rcu_read_lock();
2065
2066 if (mem_cgroup_disabled())
Johannes Weiner739f79f2017-08-18 15:15:48 -07002067 return NULL;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002068again:
Johannes Weiner1306a852014-12-10 15:44:52 -08002069 memcg = page->mem_cgroup;
Johannes Weiner29833312014-12-10 15:44:02 -08002070 if (unlikely(!memcg))
Johannes Weiner739f79f2017-08-18 15:15:48 -07002071 return NULL;
Johannes Weinerd7365e72014-10-29 14:50:48 -07002072
Qiang Huangbdcbb652014-06-04 16:08:21 -07002073 if (atomic_read(&memcg->moving_account) <= 0)
Johannes Weiner739f79f2017-08-18 15:15:48 -07002074 return memcg;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002075
Johannes Weiner6de22612015-02-11 15:25:01 -08002076 spin_lock_irqsave(&memcg->move_lock, flags);
Johannes Weiner1306a852014-12-10 15:44:52 -08002077 if (memcg != page->mem_cgroup) {
Johannes Weiner6de22612015-02-11 15:25:01 -08002078 spin_unlock_irqrestore(&memcg->move_lock, flags);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002079 goto again;
2080 }
Johannes Weiner6de22612015-02-11 15:25:01 -08002081
2082 /*
2083 * When charge migration first begins, we can have locked and
2084 * unlocked page stat updates happening concurrently. Track
Johannes Weiner81f8c3a2016-03-15 14:57:04 -07002085 * the task who has the lock for unlock_page_memcg().
Johannes Weiner6de22612015-02-11 15:25:01 -08002086 */
2087 memcg->move_lock_task = current;
2088 memcg->move_lock_flags = flags;
Johannes Weinerd7365e72014-10-29 14:50:48 -07002089
Johannes Weiner739f79f2017-08-18 15:15:48 -07002090 return memcg;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002091}
Johannes Weiner81f8c3a2016-03-15 14:57:04 -07002092EXPORT_SYMBOL(lock_page_memcg);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002093
Johannes Weinerd7365e72014-10-29 14:50:48 -07002094/**
Johannes Weiner739f79f2017-08-18 15:15:48 -07002095 * __unlock_page_memcg - unlock and unpin a memcg
2096 * @memcg: the memcg
2097 *
2098 * Unlock and unpin a memcg returned by lock_page_memcg().
Johannes Weinerd7365e72014-10-29 14:50:48 -07002099 */
Johannes Weiner739f79f2017-08-18 15:15:48 -07002100void __unlock_page_memcg(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002101{
Johannes Weiner6de22612015-02-11 15:25:01 -08002102 if (memcg && memcg->move_lock_task == current) {
2103 unsigned long flags = memcg->move_lock_flags;
2104
2105 memcg->move_lock_task = NULL;
2106 memcg->move_lock_flags = 0;
2107
2108 spin_unlock_irqrestore(&memcg->move_lock, flags);
2109 }
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002110
Johannes Weinerd7365e72014-10-29 14:50:48 -07002111 rcu_read_unlock();
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002112}
Johannes Weiner739f79f2017-08-18 15:15:48 -07002113
2114/**
2115 * unlock_page_memcg - unlock a page->mem_cgroup binding
2116 * @page: the page
2117 */
2118void unlock_page_memcg(struct page *page)
2119{
2120 __unlock_page_memcg(page->mem_cgroup);
2121}
Johannes Weiner81f8c3a2016-03-15 14:57:04 -07002122EXPORT_SYMBOL(unlock_page_memcg);
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002123
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002124struct memcg_stock_pcp {
2125 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002126 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002127 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002128 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002129#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002130};
2131static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002132static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002133
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002134/**
2135 * consume_stock: Try to consume stocked charge on this cpu.
2136 * @memcg: memcg to consume from.
2137 * @nr_pages: how many pages to charge.
2138 *
2139 * The charges will only happen if @memcg matches the current cpu's memcg
2140 * stock, and at least @nr_pages are available in that stock. Failure to
2141 * service an allocation will refill the stock.
2142 *
2143 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002144 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002145static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002146{
2147 struct memcg_stock_pcp *stock;
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002148 unsigned long flags;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002149 bool ret = false;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002150
Johannes Weinera983b5e2018-01-31 16:16:45 -08002151 if (nr_pages > MEMCG_CHARGE_BATCH)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002152 return ret;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002153
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002154 local_irq_save(flags);
2155
2156 stock = this_cpu_ptr(&memcg_stock);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002157 if (memcg == stock->cached && stock->nr_pages >= nr_pages) {
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002158 stock->nr_pages -= nr_pages;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002159 ret = true;
2160 }
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002161
2162 local_irq_restore(flags);
2163
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002164 return ret;
2165}
2166
2167/*
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002168 * Returns stocks cached in percpu and reset cached information.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002169 */
2170static void drain_stock(struct memcg_stock_pcp *stock)
2171{
2172 struct mem_cgroup *old = stock->cached;
2173
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002174 if (stock->nr_pages) {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002175 page_counter_uncharge(&old->memory, stock->nr_pages);
Johannes Weiner7941d212016-01-14 15:21:23 -08002176 if (do_memsw_account())
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002177 page_counter_uncharge(&old->memsw, stock->nr_pages);
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002178 css_put_many(&old->css, stock->nr_pages);
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002179 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002180 }
2181 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002182}
2183
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002184static void drain_local_stock(struct work_struct *dummy)
2185{
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002186 struct memcg_stock_pcp *stock;
2187 unsigned long flags;
2188
Michal Hocko72f01842017-10-03 16:14:53 -07002189 /*
2190 * The only protection from memory hotplug vs. drain_stock races is
2191 * that we always operate on local CPU stock here with IRQ disabled
2192 */
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002193 local_irq_save(flags);
2194
2195 stock = this_cpu_ptr(&memcg_stock);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002196 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002197 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002198
2199 local_irq_restore(flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002200}
2201
2202/*
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002203 * Cache charges(val) to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002204 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002205 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002206static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002207{
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002208 struct memcg_stock_pcp *stock;
2209 unsigned long flags;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002210
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002211 local_irq_save(flags);
2212
2213 stock = this_cpu_ptr(&memcg_stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002214 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002215 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002216 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002217 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002218 stock->nr_pages += nr_pages;
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002219
Johannes Weinera983b5e2018-01-31 16:16:45 -08002220 if (stock->nr_pages > MEMCG_CHARGE_BATCH)
Roman Gushchin475d0482017-09-08 16:13:09 -07002221 drain_stock(stock);
2222
Johannes Weinerdb2ba40c2016-09-19 14:44:36 -07002223 local_irq_restore(flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002224}
2225
2226/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002227 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002228 * of the hierarchy under it.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002229 */
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002230static void drain_all_stock(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002231{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002232 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002233
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002234 /* If someone's already draining, avoid adding running more workers. */
2235 if (!mutex_trylock(&percpu_charge_mutex))
2236 return;
Michal Hocko72f01842017-10-03 16:14:53 -07002237 /*
2238 * Notify other cpus that system-wide "drain" is running
2239 * We do not care about races with the cpu hotplug because cpu down
2240 * as well as workers from this path always operate on the local
2241 * per-cpu data. CPU up doesn't touch memcg_stock at all.
2242 */
Johannes Weiner5af12d02011-08-25 15:59:07 -07002243 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002244 for_each_online_cpu(cpu) {
2245 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002246 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002247
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002248 memcg = stock->cached;
Michal Hocko72f01842017-10-03 16:14:53 -07002249 if (!memcg || !stock->nr_pages || !css_tryget(&memcg->css))
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002250 continue;
Michal Hocko72f01842017-10-03 16:14:53 -07002251 if (!mem_cgroup_is_descendant(memcg, root_memcg)) {
2252 css_put(&memcg->css);
Michal Hocko3e920412011-07-26 16:08:29 -07002253 continue;
Michal Hocko72f01842017-10-03 16:14:53 -07002254 }
Michal Hockod1a05b62011-07-26 16:08:27 -07002255 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2256 if (cpu == curcpu)
2257 drain_local_stock(&stock->work);
2258 else
2259 schedule_work_on(cpu, &stock->work);
2260 }
Michal Hocko72f01842017-10-03 16:14:53 -07002261 css_put(&memcg->css);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002262 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002263 put_cpu();
Michal Hocko9f50fad2011-08-09 11:56:26 +02002264 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002265}
2266
Sebastian Andrzej Siewior308167f2016-11-03 15:49:59 +01002267static int memcg_hotplug_cpu_dead(unsigned int cpu)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002268{
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002269 struct memcg_stock_pcp *stock;
Johannes Weiner42a30032019-05-14 15:47:12 -07002270 struct mem_cgroup *memcg, *mi;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002271
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002272 stock = &per_cpu(memcg_stock, cpu);
2273 drain_stock(stock);
Johannes Weinera983b5e2018-01-31 16:16:45 -08002274
2275 for_each_mem_cgroup(memcg) {
2276 int i;
2277
2278 for (i = 0; i < MEMCG_NR_STAT; i++) {
2279 int nid;
2280 long x;
2281
Chris Down871789d2019-05-14 15:46:57 -07002282 x = this_cpu_xchg(memcg->vmstats_percpu->stat[i], 0);
Johannes Weiner815744d2019-06-13 15:55:46 -07002283 if (x)
Johannes Weiner42a30032019-05-14 15:47:12 -07002284 for (mi = memcg; mi; mi = parent_mem_cgroup(mi))
2285 atomic_long_add(x, &memcg->vmstats[i]);
Johannes Weinera983b5e2018-01-31 16:16:45 -08002286
2287 if (i >= NR_VM_NODE_STAT_ITEMS)
2288 continue;
2289
2290 for_each_node(nid) {
2291 struct mem_cgroup_per_node *pn;
2292
2293 pn = mem_cgroup_nodeinfo(memcg, nid);
2294 x = this_cpu_xchg(pn->lruvec_stat_cpu->count[i], 0);
Johannes Weiner815744d2019-06-13 15:55:46 -07002295 if (x)
Johannes Weiner42a30032019-05-14 15:47:12 -07002296 do {
2297 atomic_long_add(x, &pn->lruvec_stat[i]);
2298 } while ((pn = parent_nodeinfo(pn, nid)));
Johannes Weinera983b5e2018-01-31 16:16:45 -08002299 }
2300 }
2301
Johannes Weinere27be242018-04-10 16:29:45 -07002302 for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
Johannes Weinera983b5e2018-01-31 16:16:45 -08002303 long x;
2304
Chris Down871789d2019-05-14 15:46:57 -07002305 x = this_cpu_xchg(memcg->vmstats_percpu->events[i], 0);
Johannes Weiner815744d2019-06-13 15:55:46 -07002306 if (x)
Johannes Weiner42a30032019-05-14 15:47:12 -07002307 for (mi = memcg; mi; mi = parent_mem_cgroup(mi))
2308 atomic_long_add(x, &memcg->vmevents[i]);
Johannes Weinera983b5e2018-01-31 16:16:45 -08002309 }
2310 }
2311
Sebastian Andrzej Siewior308167f2016-11-03 15:49:59 +01002312 return 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002313}
2314
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08002315static void reclaim_high(struct mem_cgroup *memcg,
2316 unsigned int nr_pages,
2317 gfp_t gfp_mask)
2318{
2319 do {
2320 if (page_counter_read(&memcg->memory) <= memcg->high)
2321 continue;
Johannes Weinere27be242018-04-10 16:29:45 -07002322 memcg_memory_event(memcg, MEMCG_HIGH);
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08002323 try_to_free_mem_cgroup_pages(memcg, nr_pages, gfp_mask, true);
2324 } while ((memcg = parent_mem_cgroup(memcg)));
2325}
2326
2327static void high_work_func(struct work_struct *work)
2328{
2329 struct mem_cgroup *memcg;
2330
2331 memcg = container_of(work, struct mem_cgroup, high_work);
Johannes Weinera983b5e2018-01-31 16:16:45 -08002332 reclaim_high(memcg, MEMCG_CHARGE_BATCH, GFP_KERNEL);
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08002333}
2334
Tejun Heob23afb92015-11-05 18:46:11 -08002335/*
2336 * Scheduled by try_charge() to be executed from the userland return path
2337 * and reclaims memory over the high limit.
2338 */
2339void mem_cgroup_handle_over_high(void)
2340{
2341 unsigned int nr_pages = current->memcg_nr_pages_over_high;
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08002342 struct mem_cgroup *memcg;
Tejun Heob23afb92015-11-05 18:46:11 -08002343
2344 if (likely(!nr_pages))
2345 return;
2346
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08002347 memcg = get_mem_cgroup_from_mm(current->mm);
2348 reclaim_high(memcg, nr_pages, GFP_KERNEL);
Tejun Heob23afb92015-11-05 18:46:11 -08002349 css_put(&memcg->css);
2350 current->memcg_nr_pages_over_high = 0;
2351}
2352
Johannes Weiner00501b52014-08-08 14:19:20 -07002353static int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
2354 unsigned int nr_pages)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002355{
Johannes Weinera983b5e2018-01-31 16:16:45 -08002356 unsigned int batch = max(MEMCG_CHARGE_BATCH, nr_pages);
Johannes Weiner9b130612014-08-06 16:05:51 -07002357 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002358 struct mem_cgroup *mem_over_limit;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002359 struct page_counter *counter;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002360 unsigned long nr_reclaimed;
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002361 bool may_swap = true;
2362 bool drained = false;
Michal Hocko29ef6802018-08-17 15:47:11 -07002363 enum oom_status oom_status;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002364
Johannes Weinerce00a962014-09-05 08:43:57 -04002365 if (mem_cgroup_is_root(memcg))
Tejun Heo10d53c72015-11-05 18:46:17 -08002366 return 0;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002367retry:
Michal Hockob6b6cc72014-04-07 15:37:44 -07002368 if (consume_stock(memcg, nr_pages))
Tejun Heo10d53c72015-11-05 18:46:17 -08002369 return 0;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002370
Johannes Weiner7941d212016-01-14 15:21:23 -08002371 if (!do_memsw_account() ||
Johannes Weiner6071ca52015-11-05 18:50:26 -08002372 page_counter_try_charge(&memcg->memsw, batch, &counter)) {
2373 if (page_counter_try_charge(&memcg->memory, batch, &counter))
Johannes Weiner6539cc02014-08-06 16:05:42 -07002374 goto done_restock;
Johannes Weiner7941d212016-01-14 15:21:23 -08002375 if (do_memsw_account())
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002376 page_counter_uncharge(&memcg->memsw, batch);
2377 mem_over_limit = mem_cgroup_from_counter(counter, memory);
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002378 } else {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002379 mem_over_limit = mem_cgroup_from_counter(counter, memsw);
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002380 may_swap = false;
Johannes Weiner3fbe7242014-10-09 15:28:54 -07002381 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002382
Johannes Weiner6539cc02014-08-06 16:05:42 -07002383 if (batch > nr_pages) {
2384 batch = nr_pages;
2385 goto retry;
2386 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002387
Johannes Weiner06b078f2014-08-06 16:05:44 -07002388 /*
2389 * Unlike in global OOM situations, memcg is not in a physical
2390 * memory shortage. Allow dying and OOM-killed tasks to
2391 * bypass the last charges so that they can exit quickly and
2392 * free their memory.
2393 */
Tetsuo Handa7775fac2019-03-05 15:46:47 -08002394 if (unlikely(should_force_charge()))
Tejun Heo10d53c72015-11-05 18:46:17 -08002395 goto force;
Johannes Weiner06b078f2014-08-06 16:05:44 -07002396
Johannes Weiner89a28482016-10-27 17:46:56 -07002397 /*
2398 * Prevent unbounded recursion when reclaim operations need to
2399 * allocate memory. This might exceed the limits temporarily,
2400 * but we prefer facilitating memory reclaim and getting back
2401 * under the limit over triggering OOM kills in these cases.
2402 */
2403 if (unlikely(current->flags & PF_MEMALLOC))
2404 goto force;
2405
Johannes Weiner06b078f2014-08-06 16:05:44 -07002406 if (unlikely(task_in_memcg_oom(current)))
2407 goto nomem;
2408
Mel Gormand0164ad2015-11-06 16:28:21 -08002409 if (!gfpflags_allow_blocking(gfp_mask))
Johannes Weiner6539cc02014-08-06 16:05:42 -07002410 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002411
Johannes Weinere27be242018-04-10 16:29:45 -07002412 memcg_memory_event(mem_over_limit, MEMCG_MAX);
Johannes Weiner241994ed2015-02-11 15:26:06 -08002413
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002414 nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages,
2415 gfp_mask, may_swap);
Johannes Weiner6539cc02014-08-06 16:05:42 -07002416
Johannes Weiner61e02c72014-08-06 16:08:16 -07002417 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner6539cc02014-08-06 16:05:42 -07002418 goto retry;
Johannes Weiner28c34c22014-08-06 16:05:47 -07002419
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002420 if (!drained) {
Johannes Weiner6d3d6aa2014-12-10 15:42:50 -08002421 drain_all_stock(mem_over_limit);
Johannes Weinerb70a2a22014-10-09 15:28:56 -07002422 drained = true;
2423 goto retry;
2424 }
2425
Johannes Weiner28c34c22014-08-06 16:05:47 -07002426 if (gfp_mask & __GFP_NORETRY)
2427 goto nomem;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002428 /*
2429 * Even though the limit is exceeded at this point, reclaim
2430 * may have been able to free some pages. Retry the charge
2431 * before killing the task.
2432 *
2433 * Only for regular pages, though: huge pages are rather
2434 * unlikely to succeed so close to the limit, and we fall back
2435 * to regular pages anyway in case of failure.
2436 */
Johannes Weiner61e02c72014-08-06 16:08:16 -07002437 if (nr_reclaimed && nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER))
Johannes Weiner6539cc02014-08-06 16:05:42 -07002438 goto retry;
2439 /*
2440 * At task move, charge accounts can be doubly counted. So, it's
2441 * better to wait until the end of task_move if something is going on.
2442 */
2443 if (mem_cgroup_wait_acct_move(mem_over_limit))
2444 goto retry;
2445
Johannes Weiner9b130612014-08-06 16:05:51 -07002446 if (nr_retries--)
2447 goto retry;
2448
Shakeel Butt38d38492019-07-11 20:55:48 -07002449 if (gfp_mask & __GFP_RETRY_MAYFAIL)
Michal Hocko29ef6802018-08-17 15:47:11 -07002450 goto nomem;
2451
Johannes Weiner06b078f2014-08-06 16:05:44 -07002452 if (gfp_mask & __GFP_NOFAIL)
Tejun Heo10d53c72015-11-05 18:46:17 -08002453 goto force;
Johannes Weiner06b078f2014-08-06 16:05:44 -07002454
Johannes Weiner6539cc02014-08-06 16:05:42 -07002455 if (fatal_signal_pending(current))
Tejun Heo10d53c72015-11-05 18:46:17 -08002456 goto force;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002457
Michal Hocko29ef6802018-08-17 15:47:11 -07002458 /*
2459 * keep retrying as long as the memcg oom killer is able to make
2460 * a forward progress or bypass the charge if the oom killer
2461 * couldn't make any progress.
2462 */
2463 oom_status = mem_cgroup_oom(mem_over_limit, gfp_mask,
Jerome Marchand3608de02015-11-05 18:47:29 -08002464 get_order(nr_pages * PAGE_SIZE));
Michal Hocko29ef6802018-08-17 15:47:11 -07002465 switch (oom_status) {
2466 case OOM_SUCCESS:
2467 nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Michal Hocko29ef6802018-08-17 15:47:11 -07002468 goto retry;
2469 case OOM_FAILED:
2470 goto force;
2471 default:
2472 goto nomem;
2473 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002474nomem:
Johannes Weiner6d1fdc42014-04-07 15:37:45 -07002475 if (!(gfp_mask & __GFP_NOFAIL))
Johannes Weiner3168ecb2013-10-31 16:34:13 -07002476 return -ENOMEM;
Tejun Heo10d53c72015-11-05 18:46:17 -08002477force:
2478 /*
2479 * The allocation either can't fail or will lead to more memory
2480 * being freed very soon. Allow memory usage go over the limit
2481 * temporarily by force charging it.
2482 */
2483 page_counter_charge(&memcg->memory, nr_pages);
Johannes Weiner7941d212016-01-14 15:21:23 -08002484 if (do_memsw_account())
Tejun Heo10d53c72015-11-05 18:46:17 -08002485 page_counter_charge(&memcg->memsw, nr_pages);
2486 css_get_many(&memcg->css, nr_pages);
2487
2488 return 0;
Johannes Weiner6539cc02014-08-06 16:05:42 -07002489
2490done_restock:
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002491 css_get_many(&memcg->css, batch);
Johannes Weiner6539cc02014-08-06 16:05:42 -07002492 if (batch > nr_pages)
2493 refill_stock(memcg, batch - nr_pages);
Tejun Heob23afb92015-11-05 18:46:11 -08002494
Johannes Weiner241994ed2015-02-11 15:26:06 -08002495 /*
Tejun Heob23afb92015-11-05 18:46:11 -08002496 * If the hierarchy is above the normal consumption range, schedule
2497 * reclaim on returning to userland. We can perform reclaim here
Mel Gorman71baba42015-11-06 16:28:28 -08002498 * if __GFP_RECLAIM but let's always punt for simplicity and so that
Tejun Heob23afb92015-11-05 18:46:11 -08002499 * GFP_KERNEL can consistently be used during reclaim. @memcg is
2500 * not recorded as it most likely matches current's and won't
2501 * change in the meantime. As high limit is checked again before
2502 * reclaim, the cost of mismatch is negligible.
Johannes Weiner241994ed2015-02-11 15:26:06 -08002503 */
2504 do {
Tejun Heob23afb92015-11-05 18:46:11 -08002505 if (page_counter_read(&memcg->memory) > memcg->high) {
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08002506 /* Don't bother a random interrupted task */
2507 if (in_interrupt()) {
2508 schedule_work(&memcg->high_work);
2509 break;
2510 }
Vladimir Davydov9516a182015-12-11 13:40:24 -08002511 current->memcg_nr_pages_over_high += batch;
Tejun Heob23afb92015-11-05 18:46:11 -08002512 set_notify_resume(current);
2513 break;
2514 }
Johannes Weiner241994ed2015-02-11 15:26:06 -08002515 } while ((memcg = parent_mem_cgroup(memcg)));
Tejun Heo10d53c72015-11-05 18:46:17 -08002516
2517 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002518}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002519
Johannes Weiner00501b52014-08-08 14:19:20 -07002520static void cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002521{
Johannes Weinerce00a962014-09-05 08:43:57 -04002522 if (mem_cgroup_is_root(memcg))
2523 return;
2524
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002525 page_counter_uncharge(&memcg->memory, nr_pages);
Johannes Weiner7941d212016-01-14 15:21:23 -08002526 if (do_memsw_account())
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002527 page_counter_uncharge(&memcg->memsw, nr_pages);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002528
Johannes Weinere8ea14c2014-12-10 15:42:42 -08002529 css_put_many(&memcg->css, nr_pages);
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002530}
2531
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002532static void lock_page_lru(struct page *page, int *isolated)
2533{
Andrey Ryabininf4b7e272019-03-05 15:49:39 -08002534 pg_data_t *pgdat = page_pgdat(page);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002535
Andrey Ryabininf4b7e272019-03-05 15:49:39 -08002536 spin_lock_irq(&pgdat->lru_lock);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002537 if (PageLRU(page)) {
2538 struct lruvec *lruvec;
2539
Andrey Ryabininf4b7e272019-03-05 15:49:39 -08002540 lruvec = mem_cgroup_page_lruvec(page, pgdat);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002541 ClearPageLRU(page);
2542 del_page_from_lru_list(page, lruvec, page_lru(page));
2543 *isolated = 1;
2544 } else
2545 *isolated = 0;
2546}
2547
2548static void unlock_page_lru(struct page *page, int isolated)
2549{
Andrey Ryabininf4b7e272019-03-05 15:49:39 -08002550 pg_data_t *pgdat = page_pgdat(page);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002551
2552 if (isolated) {
2553 struct lruvec *lruvec;
2554
Andrey Ryabininf4b7e272019-03-05 15:49:39 -08002555 lruvec = mem_cgroup_page_lruvec(page, pgdat);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002556 VM_BUG_ON_PAGE(PageLRU(page), page);
2557 SetPageLRU(page);
2558 add_page_to_lru_list(page, lruvec, page_lru(page));
2559 }
Andrey Ryabininf4b7e272019-03-05 15:49:39 -08002560 spin_unlock_irq(&pgdat->lru_lock);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002561}
2562
Johannes Weiner00501b52014-08-08 14:19:20 -07002563static void commit_charge(struct page *page, struct mem_cgroup *memcg,
Johannes Weiner6abb5a82014-08-08 14:19:33 -07002564 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002565{
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002566 int isolated;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002567
Johannes Weiner1306a852014-12-10 15:44:52 -08002568 VM_BUG_ON_PAGE(page->mem_cgroup, page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002569
2570 /*
2571 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2572 * may already be on some other mem_cgroup's LRU. Take care of it.
2573 */
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002574 if (lrucare)
2575 lock_page_lru(page, &isolated);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002576
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002577 /*
2578 * Nobody should be changing or seriously looking at
Johannes Weiner1306a852014-12-10 15:44:52 -08002579 * page->mem_cgroup at this point:
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002580 *
2581 * - the page is uncharged
2582 *
2583 * - the page is off-LRU
2584 *
2585 * - an anonymous fault has exclusive page access, except for
2586 * a locked page table
2587 *
2588 * - a page cache insertion, a swapin fault, or a migration
2589 * have the page locked
2590 */
Johannes Weiner1306a852014-12-10 15:44:52 -08002591 page->mem_cgroup = memcg;
Hugh Dickins3be912772008-02-07 00:14:19 -08002592
Johannes Weiner0a31bc92014-08-08 14:19:22 -07002593 if (lrucare)
2594 unlock_page_lru(page, isolated);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002595}
2596
Kirill Tkhai84c07d12018-08-17 15:47:25 -07002597#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002598static int memcg_alloc_cache_id(void)
Glauber Costa55007d82012-12-18 14:22:38 -08002599{
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002600 int id, size;
2601 int err;
Glauber Costa55007d82012-12-18 14:22:38 -08002602
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002603 id = ida_simple_get(&memcg_cache_ida,
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002604 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
2605 if (id < 0)
2606 return id;
2607
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002608 if (id < memcg_nr_cache_ids)
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002609 return id;
2610
2611 /*
2612 * There's no space for the new id in memcg_caches arrays,
2613 * so we have to grow them.
2614 */
Vladimir Davydov05257a12015-02-12 14:59:01 -08002615 down_write(&memcg_cache_ids_sem);
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002616
2617 size = 2 * (id + 1);
Glauber Costa55007d82012-12-18 14:22:38 -08002618 if (size < MEMCG_CACHES_MIN_SIZE)
2619 size = MEMCG_CACHES_MIN_SIZE;
2620 else if (size > MEMCG_CACHES_MAX_SIZE)
2621 size = MEMCG_CACHES_MAX_SIZE;
2622
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002623 err = memcg_update_all_caches(size);
Vladimir Davydov05257a12015-02-12 14:59:01 -08002624 if (!err)
Vladimir Davydov60d3fd32015-02-12 14:59:10 -08002625 err = memcg_update_all_list_lrus(size);
2626 if (!err)
Vladimir Davydov05257a12015-02-12 14:59:01 -08002627 memcg_nr_cache_ids = size;
2628
2629 up_write(&memcg_cache_ids_sem);
2630
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002631 if (err) {
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002632 ida_simple_remove(&memcg_cache_ida, id);
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07002633 return err;
2634 }
2635 return id;
2636}
2637
2638static void memcg_free_cache_id(int id)
2639{
Vladimir Davydovdbcf73e2015-02-12 14:58:57 -08002640 ida_simple_remove(&memcg_cache_ida, id);
Glauber Costa55007d82012-12-18 14:22:38 -08002641}
2642
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002643struct memcg_kmem_cache_create_work {
Vladimir Davydov5722d092014-04-07 15:39:24 -07002644 struct mem_cgroup *memcg;
2645 struct kmem_cache *cachep;
2646 struct work_struct work;
2647};
2648
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002649static void memcg_kmem_cache_create_func(struct work_struct *w)
Glauber Costad7f25f82012-12-18 14:22:40 -08002650{
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002651 struct memcg_kmem_cache_create_work *cw =
2652 container_of(w, struct memcg_kmem_cache_create_work, work);
Vladimir Davydov5722d092014-04-07 15:39:24 -07002653 struct mem_cgroup *memcg = cw->memcg;
2654 struct kmem_cache *cachep = cw->cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08002655
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002656 memcg_create_kmem_cache(memcg, cachep);
Vladimir Davydovbd673142014-06-04 16:07:40 -07002657
Vladimir Davydov5722d092014-04-07 15:39:24 -07002658 css_put(&memcg->css);
Glauber Costad7f25f82012-12-18 14:22:40 -08002659 kfree(cw);
2660}
2661
2662/*
2663 * Enqueue the creation of a per-memcg kmem_cache.
Glauber Costad7f25f82012-12-18 14:22:40 -08002664 */
Shakeel Butt85cfb242018-10-26 15:07:41 -07002665static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002666 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08002667{
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002668 struct memcg_kmem_cache_create_work *cw;
Glauber Costad7f25f82012-12-18 14:22:40 -08002669
Roman Gushchinf0a3a242019-07-11 20:56:27 -07002670 if (!css_tryget_online(&memcg->css))
2671 return;
2672
Minchan Kimc892fd82018-04-20 14:56:17 -07002673 cw = kmalloc(sizeof(*cw), GFP_NOWAIT | __GFP_NOWARN);
Vladimir Davydov8135be52014-12-12 16:56:38 -08002674 if (!cw)
Glauber Costad7f25f82012-12-18 14:22:40 -08002675 return;
Vladimir Davydov8135be52014-12-12 16:56:38 -08002676
Glauber Costad7f25f82012-12-18 14:22:40 -08002677 cw->memcg = memcg;
2678 cw->cachep = cachep;
Vladimir Davydovd5b3cf72015-02-10 14:11:47 -08002679 INIT_WORK(&cw->work, memcg_kmem_cache_create_func);
Glauber Costad7f25f82012-12-18 14:22:40 -08002680
Tejun Heo17cc4df2017-02-22 15:41:36 -08002681 queue_work(memcg_kmem_cache_wq, &cw->work);
Glauber Costad7f25f82012-12-18 14:22:40 -08002682}
2683
Vladimir Davydov45264772016-07-26 15:24:21 -07002684static inline bool memcg_kmem_bypass(void)
2685{
2686 if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
2687 return true;
2688 return false;
2689}
2690
2691/**
2692 * memcg_kmem_get_cache: select the correct per-memcg cache for allocation
2693 * @cachep: the original global kmem cache
2694 *
Glauber Costad7f25f82012-12-18 14:22:40 -08002695 * Return the kmem_cache we're supposed to use for a slab allocation.
2696 * We try to use the current memcg's version of the cache.
2697 *
Vladimir Davydov45264772016-07-26 15:24:21 -07002698 * If the cache does not exist yet, if we are the first user of it, we
2699 * create it asynchronously in a workqueue and let the current allocation
2700 * go through with the original cache.
Glauber Costad7f25f82012-12-18 14:22:40 -08002701 *
Vladimir Davydov45264772016-07-26 15:24:21 -07002702 * This function takes a reference to the cache it returns to assure it
2703 * won't get destroyed while we are working with it. Once the caller is
2704 * done with it, memcg_kmem_put_cache() must be called to release the
2705 * reference.
Glauber Costad7f25f82012-12-18 14:22:40 -08002706 */
Vladimir Davydov45264772016-07-26 15:24:21 -07002707struct kmem_cache *memcg_kmem_get_cache(struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08002708{
2709 struct mem_cgroup *memcg;
Vladimir Davydov959c8962014-01-23 15:52:59 -08002710 struct kmem_cache *memcg_cachep;
Roman Gushchinf0a3a242019-07-11 20:56:27 -07002711 struct memcg_cache_array *arr;
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08002712 int kmemcg_id;
Glauber Costad7f25f82012-12-18 14:22:40 -08002713
Vladimir Davydovf7ce3192015-02-12 14:59:20 -08002714 VM_BUG_ON(!is_root_cache(cachep));
Glauber Costad7f25f82012-12-18 14:22:40 -08002715
Vladimir Davydov45264772016-07-26 15:24:21 -07002716 if (memcg_kmem_bypass())
Vladimir Davydov230e9fc2016-01-14 15:18:15 -08002717 return cachep;
2718
Roman Gushchinf0a3a242019-07-11 20:56:27 -07002719 rcu_read_lock();
2720
2721 if (unlikely(current->active_memcg))
2722 memcg = current->active_memcg;
2723 else
2724 memcg = mem_cgroup_from_task(current);
2725
2726 if (!memcg || memcg == root_mem_cgroup)
2727 goto out_unlock;
2728
Jason Low4db0c3c2015-04-15 16:14:08 -07002729 kmemcg_id = READ_ONCE(memcg->kmemcg_id);
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08002730 if (kmemcg_id < 0)
Roman Gushchinf0a3a242019-07-11 20:56:27 -07002731 goto out_unlock;
Glauber Costad7f25f82012-12-18 14:22:40 -08002732
Roman Gushchinf0a3a242019-07-11 20:56:27 -07002733 arr = rcu_dereference(cachep->memcg_params.memcg_caches);
2734
2735 /*
2736 * Make sure we will access the up-to-date value. The code updating
2737 * memcg_caches issues a write barrier to match the data dependency
2738 * barrier inside READ_ONCE() (see memcg_create_kmem_cache()).
2739 */
2740 memcg_cachep = READ_ONCE(arr->entries[kmemcg_id]);
Li Zefanca0dde92013-04-29 15:08:57 -07002741
2742 /*
2743 * If we are in a safe context (can wait, and not in interrupt
2744 * context), we could be be predictable and return right away.
2745 * This would guarantee that the allocation being performed
2746 * already belongs in the new cache.
2747 *
2748 * However, there are some clashes that can arrive from locking.
2749 * For instance, because we acquire the slab_mutex while doing
Vladimir Davydov776ed0f2014-06-04 16:10:02 -07002750 * memcg_create_kmem_cache, this means no further allocation
2751 * could happen with the slab_mutex held. So it's better to
2752 * defer everything.
Roman Gushchinf0a3a242019-07-11 20:56:27 -07002753 *
2754 * If the memcg is dying or memcg_cache is about to be released,
2755 * don't bother creating new kmem_caches. Because memcg_cachep
2756 * is ZEROed as the fist step of kmem offlining, we don't need
2757 * percpu_ref_tryget_live() here. css_tryget_online() check in
2758 * memcg_schedule_kmem_cache_create() will prevent us from
2759 * creation of a new kmem_cache.
Li Zefanca0dde92013-04-29 15:08:57 -07002760 */
Roman Gushchinf0a3a242019-07-11 20:56:27 -07002761 if (unlikely(!memcg_cachep))
2762 memcg_schedule_kmem_cache_create(memcg, cachep);
2763 else if (percpu_ref_tryget(&memcg_cachep->memcg_params.refcnt))
2764 cachep = memcg_cachep;
2765out_unlock:
2766 rcu_read_unlock();
Li Zefanca0dde92013-04-29 15:08:57 -07002767 return cachep;
Glauber Costad7f25f82012-12-18 14:22:40 -08002768}
Glauber Costad7f25f82012-12-18 14:22:40 -08002769
Vladimir Davydov45264772016-07-26 15:24:21 -07002770/**
2771 * memcg_kmem_put_cache: drop reference taken by memcg_kmem_get_cache
2772 * @cachep: the cache returned by memcg_kmem_get_cache
2773 */
2774void memcg_kmem_put_cache(struct kmem_cache *cachep)
Vladimir Davydov8135be52014-12-12 16:56:38 -08002775{
2776 if (!is_root_cache(cachep))
Roman Gushchinf0a3a242019-07-11 20:56:27 -07002777 percpu_ref_put(&cachep->memcg_params.refcnt);
Vladimir Davydov8135be52014-12-12 16:56:38 -08002778}
2779
Vladimir Davydov45264772016-07-26 15:24:21 -07002780/**
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08002781 * __memcg_kmem_charge_memcg: charge a kmem page
Vladimir Davydov45264772016-07-26 15:24:21 -07002782 * @page: page to charge
2783 * @gfp: reclaim mode
2784 * @order: allocation order
2785 * @memcg: memory cgroup to charge
2786 *
2787 * Returns 0 on success, an error code on failure.
2788 */
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08002789int __memcg_kmem_charge_memcg(struct page *page, gfp_t gfp, int order,
Vladimir Davydov45264772016-07-26 15:24:21 -07002790 struct mem_cgroup *memcg)
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002791{
2792 unsigned int nr_pages = 1 << order;
2793 struct page_counter *counter;
Johannes Weiner6071ca52015-11-05 18:50:26 -08002794 int ret;
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002795
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002796 ret = try_charge(memcg, gfp, nr_pages);
Johannes Weiner52c29b02016-01-20 15:02:35 -08002797 if (ret)
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002798 return ret;
Johannes Weiner52c29b02016-01-20 15:02:35 -08002799
2800 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) &&
2801 !page_counter_try_charge(&memcg->kmem, nr_pages, &counter)) {
2802 cancel_charge(memcg, nr_pages);
2803 return -ENOMEM;
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002804 }
2805
2806 page->mem_cgroup = memcg;
2807
2808 return 0;
2809}
2810
Vladimir Davydov45264772016-07-26 15:24:21 -07002811/**
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08002812 * __memcg_kmem_charge: charge a kmem page to the current memory cgroup
Vladimir Davydov45264772016-07-26 15:24:21 -07002813 * @page: page to charge
2814 * @gfp: reclaim mode
2815 * @order: allocation order
2816 *
2817 * Returns 0 on success, an error code on failure.
2818 */
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08002819int __memcg_kmem_charge(struct page *page, gfp_t gfp, int order)
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002820{
2821 struct mem_cgroup *memcg;
Vladimir Davydovfcff7d72016-03-17 14:17:29 -07002822 int ret = 0;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002823
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08002824 if (memcg_kmem_bypass())
Vladimir Davydov45264772016-07-26 15:24:21 -07002825 return 0;
2826
Shakeel Buttd46eb14b2018-08-17 15:46:39 -07002827 memcg = get_mem_cgroup_from_current();
Vladimir Davydovc4159a72016-08-08 23:03:12 +03002828 if (!mem_cgroup_is_root(memcg)) {
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08002829 ret = __memcg_kmem_charge_memcg(page, gfp, order, memcg);
Vladimir Davydovc4159a72016-08-08 23:03:12 +03002830 if (!ret)
2831 __SetPageKmemcg(page);
2832 }
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002833 css_put(&memcg->css);
Vladimir Davydovd05e83a2015-11-05 18:48:59 -08002834 return ret;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002835}
Roman Gushchin49a18ea2019-07-11 20:56:13 -07002836
2837/**
2838 * __memcg_kmem_uncharge_memcg: uncharge a kmem page
2839 * @memcg: memcg to uncharge
2840 * @nr_pages: number of pages to uncharge
2841 */
2842void __memcg_kmem_uncharge_memcg(struct mem_cgroup *memcg,
2843 unsigned int nr_pages)
2844{
2845 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
2846 page_counter_uncharge(&memcg->kmem, nr_pages);
2847
2848 page_counter_uncharge(&memcg->memory, nr_pages);
2849 if (do_memsw_account())
2850 page_counter_uncharge(&memcg->memsw, nr_pages);
2851}
Vladimir Davydov45264772016-07-26 15:24:21 -07002852/**
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08002853 * __memcg_kmem_uncharge: uncharge a kmem page
Vladimir Davydov45264772016-07-26 15:24:21 -07002854 * @page: page to uncharge
2855 * @order: allocation order
2856 */
Shakeel Butt60cd4bc2019-03-05 15:43:13 -08002857void __memcg_kmem_uncharge(struct page *page, int order)
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002858{
Johannes Weiner1306a852014-12-10 15:44:52 -08002859 struct mem_cgroup *memcg = page->mem_cgroup;
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002860 unsigned int nr_pages = 1 << order;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002861
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002862 if (!memcg)
2863 return;
2864
Sasha Levin309381fea2014-01-23 15:52:54 -08002865 VM_BUG_ON_PAGE(mem_cgroup_is_root(memcg), page);
Roman Gushchin49a18ea2019-07-11 20:56:13 -07002866 __memcg_kmem_uncharge_memcg(memcg, nr_pages);
Johannes Weiner1306a852014-12-10 15:44:52 -08002867 page->mem_cgroup = NULL;
Vladimir Davydovc4159a72016-08-08 23:03:12 +03002868
2869 /* slab pages do not have PageKmemcg flag set */
2870 if (PageKmemcg(page))
2871 __ClearPageKmemcg(page);
2872
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08002873 css_put_many(&memcg->css, nr_pages);
Vladimir Davydov60d3fd32015-02-12 14:59:10 -08002874}
Kirill Tkhai84c07d12018-08-17 15:47:25 -07002875#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002876
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002877#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2878
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002879/*
2880 * Because tail pages are not marked as "used", set it. We're under
Andrey Ryabininf4b7e272019-03-05 15:49:39 -08002881 * pgdat->lru_lock and migration entries setup in all page mappings.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002882 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002883void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002884{
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002885 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002886
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002887 if (mem_cgroup_disabled())
2888 return;
David Rientjesb070e652013-05-07 16:18:09 -07002889
Johannes Weiner29833312014-12-10 15:44:02 -08002890 for (i = 1; i < HPAGE_PMD_NR; i++)
Johannes Weiner1306a852014-12-10 15:44:52 -08002891 head[i].mem_cgroup = head->mem_cgroup;
Michal Hockob9982f82014-12-10 15:43:51 -08002892
Johannes Weinerc9019e92018-01-31 16:16:37 -08002893 __mod_memcg_state(head->mem_cgroup, MEMCG_RSS_HUGE, -HPAGE_PMD_NR);
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002894}
Hugh Dickins12d27102012-01-12 17:19:52 -08002895#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002896
Andrew Mortonc255a452012-07-31 16:43:02 -07002897#ifdef CONFIG_MEMCG_SWAP
Daisuke Nishimura02491442010-03-10 15:22:17 -08002898/**
2899 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
2900 * @entry: swap entry to be moved
2901 * @from: mem_cgroup which the entry is moved from
2902 * @to: mem_cgroup which the entry is moved to
2903 *
2904 * It succeeds only when the swap_cgroup's record for this entry is the same
2905 * as the mem_cgroup's id of @from.
2906 *
2907 * Returns 0 on success, -EINVAL on failure.
2908 *
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002909 * The caller must have charged to @to, IOW, called page_counter_charge() about
Daisuke Nishimura02491442010-03-10 15:22:17 -08002910 * both res and memsw, and called css_get().
2911 */
2912static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07002913 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002914{
2915 unsigned short old_id, new_id;
2916
Li Zefan34c00c32013-09-23 16:56:01 +08002917 old_id = mem_cgroup_id(from);
2918 new_id = mem_cgroup_id(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002919
2920 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Johannes Weinerc9019e92018-01-31 16:16:37 -08002921 mod_memcg_state(from, MEMCG_SWAP, -1);
2922 mod_memcg_state(to, MEMCG_SWAP, 1);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002923 return 0;
2924 }
2925 return -EINVAL;
2926}
2927#else
2928static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07002929 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002930{
2931 return -EINVAL;
2932}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002933#endif
2934
Roman Gushchinbbec2e12018-06-07 17:06:18 -07002935static DEFINE_MUTEX(memcg_max_mutex);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07002936
Roman Gushchinbbec2e12018-06-07 17:06:18 -07002937static int mem_cgroup_resize_max(struct mem_cgroup *memcg,
2938 unsigned long max, bool memsw)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002939{
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002940 bool enlarge = false;
Shakeel Buttbb4a7ea2018-06-07 17:07:27 -07002941 bool drained = false;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002942 int ret;
Yu Zhaoc054a782018-01-31 16:20:02 -08002943 bool limits_invariant;
2944 struct page_counter *counter = memsw ? &memcg->memsw : &memcg->memory;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002945
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002946 do {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002947 if (signal_pending(current)) {
2948 ret = -EINTR;
2949 break;
2950 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002951
Roman Gushchinbbec2e12018-06-07 17:06:18 -07002952 mutex_lock(&memcg_max_mutex);
Yu Zhaoc054a782018-01-31 16:20:02 -08002953 /*
2954 * Make sure that the new limit (memsw or memory limit) doesn't
Roman Gushchinbbec2e12018-06-07 17:06:18 -07002955 * break our basic invariant rule memory.max <= memsw.max.
Yu Zhaoc054a782018-01-31 16:20:02 -08002956 */
Roman Gushchinbbec2e12018-06-07 17:06:18 -07002957 limits_invariant = memsw ? max >= memcg->memory.max :
2958 max <= memcg->memsw.max;
Yu Zhaoc054a782018-01-31 16:20:02 -08002959 if (!limits_invariant) {
Roman Gushchinbbec2e12018-06-07 17:06:18 -07002960 mutex_unlock(&memcg_max_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002961 ret = -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002962 break;
2963 }
Roman Gushchinbbec2e12018-06-07 17:06:18 -07002964 if (max > counter->max)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002965 enlarge = true;
Roman Gushchinbbec2e12018-06-07 17:06:18 -07002966 ret = page_counter_set_max(counter, max);
2967 mutex_unlock(&memcg_max_mutex);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002968
2969 if (!ret)
2970 break;
2971
Shakeel Buttbb4a7ea2018-06-07 17:07:27 -07002972 if (!drained) {
2973 drain_all_stock(memcg);
2974 drained = true;
2975 continue;
2976 }
2977
Andrey Ryabinin1ab5c052018-01-31 16:20:37 -08002978 if (!try_to_free_mem_cgroup_pages(memcg, 1,
2979 GFP_KERNEL, !memsw)) {
2980 ret = -EBUSY;
2981 break;
2982 }
2983 } while (true);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002984
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002985 if (!ret && enlarge)
2986 memcg_oom_recover(memcg);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08002987
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002988 return ret;
2989}
2990
Mel Gormanef8f2322016-07-28 15:46:05 -07002991unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
Andrew Morton0608f432013-09-24 15:27:41 -07002992 gfp_t gfp_mask,
2993 unsigned long *total_scanned)
2994{
2995 unsigned long nr_reclaimed = 0;
Mel Gormanef8f2322016-07-28 15:46:05 -07002996 struct mem_cgroup_per_node *mz, *next_mz = NULL;
Andrew Morton0608f432013-09-24 15:27:41 -07002997 unsigned long reclaimed;
2998 int loop = 0;
Mel Gormanef8f2322016-07-28 15:46:05 -07002999 struct mem_cgroup_tree_per_node *mctz;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003000 unsigned long excess;
Andrew Morton0608f432013-09-24 15:27:41 -07003001 unsigned long nr_scanned;
3002
3003 if (order > 0)
3004 return 0;
3005
Mel Gormanef8f2322016-07-28 15:46:05 -07003006 mctz = soft_limit_tree_node(pgdat->node_id);
Michal Hockod6507ff2016-08-02 14:02:37 -07003007
3008 /*
3009 * Do not even bother to check the largest node if the root
3010 * is empty. Do it lockless to prevent lock bouncing. Races
3011 * are acceptable as soft limit is best effort anyway.
3012 */
Laurent Dufourbfc72282017-03-09 16:17:06 -08003013 if (!mctz || RB_EMPTY_ROOT(&mctz->rb_root))
Michal Hockod6507ff2016-08-02 14:02:37 -07003014 return 0;
3015
Andrew Morton0608f432013-09-24 15:27:41 -07003016 /*
3017 * This loop can run a while, specially if mem_cgroup's continuously
3018 * keep exceeding their soft limit and putting the system under
3019 * pressure
3020 */
3021 do {
3022 if (next_mz)
3023 mz = next_mz;
3024 else
3025 mz = mem_cgroup_largest_soft_limit_node(mctz);
3026 if (!mz)
3027 break;
3028
3029 nr_scanned = 0;
Mel Gormanef8f2322016-07-28 15:46:05 -07003030 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, pgdat,
Andrew Morton0608f432013-09-24 15:27:41 -07003031 gfp_mask, &nr_scanned);
3032 nr_reclaimed += reclaimed;
3033 *total_scanned += nr_scanned;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003034 spin_lock_irq(&mctz->lock);
Vladimir Davydovbc2f2e72014-12-10 15:43:40 -08003035 __mem_cgroup_remove_exceeded(mz, mctz);
Andrew Morton0608f432013-09-24 15:27:41 -07003036
3037 /*
3038 * If we failed to reclaim anything from this memory cgroup
3039 * it is time to move on to the next cgroup
3040 */
3041 next_mz = NULL;
Vladimir Davydovbc2f2e72014-12-10 15:43:40 -08003042 if (!reclaimed)
3043 next_mz = __mem_cgroup_largest_soft_limit_node(mctz);
3044
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003045 excess = soft_limit_excess(mz->memcg);
Andrew Morton0608f432013-09-24 15:27:41 -07003046 /*
3047 * One school of thought says that we should not add
3048 * back the node to the tree if reclaim returns 0.
3049 * But our reclaim could return 0, simply because due
3050 * to priority we are exposing a smaller subset of
3051 * memory to reclaim from. Consider this as a longer
3052 * term TODO.
3053 */
3054 /* If excess == 0, no tree ops */
Johannes Weinercf2c8122014-06-06 14:38:21 -07003055 __mem_cgroup_insert_exceeded(mz, mctz, excess);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07003056 spin_unlock_irq(&mctz->lock);
Andrew Morton0608f432013-09-24 15:27:41 -07003057 css_put(&mz->memcg->css);
3058 loop++;
3059 /*
3060 * Could not reclaim anything and there are no more
3061 * mem cgroups to try or we seem to be looping without
3062 * reclaiming anything.
3063 */
3064 if (!nr_reclaimed &&
3065 (next_mz == NULL ||
3066 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3067 break;
3068 } while (!nr_reclaimed);
3069 if (next_mz)
3070 css_put(&next_mz->memcg->css);
3071 return nr_reclaimed;
3072}
3073
Tejun Heoea280e72014-05-16 13:22:48 -04003074/*
3075 * Test whether @memcg has children, dead or alive. Note that this
3076 * function doesn't care whether @memcg has use_hierarchy enabled and
3077 * returns %true if there are child csses according to the cgroup
3078 * hierarchy. Testing use_hierarchy is the caller's responsiblity.
3079 */
Glauber Costab5f99b52013-02-22 16:34:53 -08003080static inline bool memcg_has_children(struct mem_cgroup *memcg)
3081{
Tejun Heoea280e72014-05-16 13:22:48 -04003082 bool ret;
3083
Tejun Heoea280e72014-05-16 13:22:48 -04003084 rcu_read_lock();
3085 ret = css_next_child(NULL, &memcg->css);
3086 rcu_read_unlock();
3087 return ret;
Glauber Costab5f99b52013-02-22 16:34:53 -08003088}
3089
3090/*
Greg Thelen51038172016-05-20 16:58:18 -07003091 * Reclaims as many pages from the given memcg as possible.
Michal Hockoc26251f2012-10-26 13:37:28 +02003092 *
3093 * Caller is responsible for holding css reference for memcg.
3094 */
3095static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
3096{
3097 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Michal Hockoc26251f2012-10-26 13:37:28 +02003098
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003099 /* we call try-to-free pages for make this cgroup empty */
3100 lru_add_drain_all();
Junaid Shahidd12c60f2018-06-07 17:07:31 -07003101
3102 drain_all_stock(memcg);
3103
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003104 /* try to free all pages in this cgroup */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003105 while (nr_retries && page_counter_read(&memcg->memory)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003106 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003107
Michal Hockoc26251f2012-10-26 13:37:28 +02003108 if (signal_pending(current))
3109 return -EINTR;
3110
Johannes Weinerb70a2a22014-10-09 15:28:56 -07003111 progress = try_to_free_mem_cgroup_pages(memcg, 1,
3112 GFP_KERNEL, true);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003113 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003114 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003115 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003116 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003117 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003118
3119 }
Michal Hockoab5196c2012-10-26 13:37:32 +02003120
3121 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003122}
3123
Tejun Heo6770c642014-05-13 12:16:21 -04003124static ssize_t mem_cgroup_force_empty_write(struct kernfs_open_file *of,
3125 char *buf, size_t nbytes,
3126 loff_t off)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003127{
Tejun Heo6770c642014-05-13 12:16:21 -04003128 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Michal Hockoc26251f2012-10-26 13:37:28 +02003129
Michal Hockod8423012012-10-26 13:37:29 +02003130 if (mem_cgroup_is_root(memcg))
3131 return -EINVAL;
Tejun Heo6770c642014-05-13 12:16:21 -04003132 return mem_cgroup_force_empty(memcg) ?: nbytes;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003133}
3134
Tejun Heo182446d2013-08-08 20:11:24 -04003135static u64 mem_cgroup_hierarchy_read(struct cgroup_subsys_state *css,
3136 struct cftype *cft)
Balbir Singh18f59ea2009-01-07 18:08:07 -08003137{
Tejun Heo182446d2013-08-08 20:11:24 -04003138 return mem_cgroup_from_css(css)->use_hierarchy;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003139}
3140
Tejun Heo182446d2013-08-08 20:11:24 -04003141static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,
3142 struct cftype *cft, u64 val)
Balbir Singh18f59ea2009-01-07 18:08:07 -08003143{
3144 int retval = 0;
Tejun Heo182446d2013-08-08 20:11:24 -04003145 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo5c9d5352014-05-16 13:22:48 -04003146 struct mem_cgroup *parent_memcg = mem_cgroup_from_css(memcg->css.parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003147
Glauber Costa567fb432012-07-31 16:43:07 -07003148 if (memcg->use_hierarchy == val)
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08003149 return 0;
Glauber Costa567fb432012-07-31 16:43:07 -07003150
Balbir Singh18f59ea2009-01-07 18:08:07 -08003151 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003152 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003153 * in the child subtrees. If it is unset, then the change can
3154 * occur, provided the current cgroup has no children.
3155 *
3156 * For the root cgroup, parent_mem is NULL, we allow value to be
3157 * set if there are no children.
3158 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003159 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08003160 (val == 1 || val == 0)) {
Tejun Heoea280e72014-05-16 13:22:48 -04003161 if (!memcg_has_children(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003162 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003163 else
3164 retval = -EBUSY;
3165 } else
3166 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07003167
Balbir Singh18f59ea2009-01-07 18:08:07 -08003168 return retval;
3169}
3170
Andrew Morton6f646152015-11-06 16:28:58 -08003171static unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Johannes Weinerce00a962014-09-05 08:43:57 -04003172{
Johannes Weiner42a30032019-05-14 15:47:12 -07003173 unsigned long val;
Johannes Weinerce00a962014-09-05 08:43:57 -04003174
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003175 if (mem_cgroup_is_root(memcg)) {
Johannes Weiner42a30032019-05-14 15:47:12 -07003176 val = memcg_page_state(memcg, MEMCG_CACHE) +
3177 memcg_page_state(memcg, MEMCG_RSS);
3178 if (swap)
3179 val += memcg_page_state(memcg, MEMCG_SWAP);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003180 } else {
Johannes Weinerce00a962014-09-05 08:43:57 -04003181 if (!swap)
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003182 val = page_counter_read(&memcg->memory);
Johannes Weinerce00a962014-09-05 08:43:57 -04003183 else
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003184 val = page_counter_read(&memcg->memsw);
Johannes Weinerce00a962014-09-05 08:43:57 -04003185 }
Michal Hockoc12176d2015-11-05 18:50:29 -08003186 return val;
Johannes Weinerce00a962014-09-05 08:43:57 -04003187}
3188
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003189enum {
3190 RES_USAGE,
3191 RES_LIMIT,
3192 RES_MAX_USAGE,
3193 RES_FAILCNT,
3194 RES_SOFT_LIMIT,
3195};
Johannes Weinerce00a962014-09-05 08:43:57 -04003196
Tejun Heo791badb2013-12-05 12:28:02 -05003197static u64 mem_cgroup_read_u64(struct cgroup_subsys_state *css,
Johannes Weiner05b84302014-08-06 16:05:59 -07003198 struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003199{
Tejun Heo182446d2013-08-08 20:11:24 -04003200 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003201 struct page_counter *counter;
Tejun Heoaf36f902012-04-01 12:09:55 -07003202
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003203 switch (MEMFILE_TYPE(cft->private)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003204 case _MEM:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003205 counter = &memcg->memory;
Glauber Costa510fc4e2012-12-18 14:21:47 -08003206 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003207 case _MEMSWAP:
3208 counter = &memcg->memsw;
3209 break;
3210 case _KMEM:
3211 counter = &memcg->kmem;
3212 break;
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003213 case _TCP:
Johannes Weiner0db15292016-01-20 15:02:50 -08003214 counter = &memcg->tcpmem;
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003215 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003216 default:
3217 BUG();
3218 }
3219
3220 switch (MEMFILE_ATTR(cft->private)) {
3221 case RES_USAGE:
3222 if (counter == &memcg->memory)
Michal Hockoc12176d2015-11-05 18:50:29 -08003223 return (u64)mem_cgroup_usage(memcg, false) * PAGE_SIZE;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003224 if (counter == &memcg->memsw)
Michal Hockoc12176d2015-11-05 18:50:29 -08003225 return (u64)mem_cgroup_usage(memcg, true) * PAGE_SIZE;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003226 return (u64)page_counter_read(counter) * PAGE_SIZE;
3227 case RES_LIMIT:
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003228 return (u64)counter->max * PAGE_SIZE;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003229 case RES_MAX_USAGE:
3230 return (u64)counter->watermark * PAGE_SIZE;
3231 case RES_FAILCNT:
3232 return counter->failcnt;
3233 case RES_SOFT_LIMIT:
3234 return (u64)memcg->soft_limit * PAGE_SIZE;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003235 default:
3236 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003237 }
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003238}
Glauber Costa510fc4e2012-12-18 14:21:47 -08003239
Kirill Tkhai84c07d12018-08-17 15:47:25 -07003240#ifdef CONFIG_MEMCG_KMEM
Johannes Weiner567e9ab2016-01-20 15:02:24 -08003241static int memcg_online_kmem(struct mem_cgroup *memcg)
Vladimir Davydovd6441632014-01-23 15:53:09 -08003242{
Vladimir Davydovd6441632014-01-23 15:53:09 -08003243 int memcg_id;
3244
Vladimir Davydovb313aee2016-03-17 14:18:27 -07003245 if (cgroup_memory_nokmem)
3246 return 0;
3247
Vladimir Davydov2a4db7e2015-02-12 14:59:32 -08003248 BUG_ON(memcg->kmemcg_id >= 0);
Johannes Weiner567e9ab2016-01-20 15:02:24 -08003249 BUG_ON(memcg->kmem_state);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003250
Vladimir Davydovf3bb3042014-10-09 15:28:45 -07003251 memcg_id = memcg_alloc_cache_id();
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08003252 if (memcg_id < 0)
3253 return memcg_id;
Vladimir Davydovd6441632014-01-23 15:53:09 -08003254
Johannes Weineref129472016-01-14 15:21:34 -08003255 static_branch_inc(&memcg_kmem_enabled_key);
Vladimir Davydovd6441632014-01-23 15:53:09 -08003256 /*
Johannes Weiner567e9ab2016-01-20 15:02:24 -08003257 * A memory cgroup is considered kmem-online as soon as it gets
Vladimir Davydov900a38f2014-12-12 16:55:10 -08003258 * kmemcg_id. Setting the id after enabling static branching will
Vladimir Davydovd6441632014-01-23 15:53:09 -08003259 * guarantee no one starts accounting before all call sites are
3260 * patched.
3261 */
Vladimir Davydov900a38f2014-12-12 16:55:10 -08003262 memcg->kmemcg_id = memcg_id;
Johannes Weiner567e9ab2016-01-20 15:02:24 -08003263 memcg->kmem_state = KMEM_ONLINE;
Tejun Heobc2791f2017-02-22 15:41:21 -08003264 INIT_LIST_HEAD(&memcg->kmem_caches);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08003265
3266 return 0;
Vladimir Davydovd6441632014-01-23 15:53:09 -08003267}
3268
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003269static void memcg_offline_kmem(struct mem_cgroup *memcg)
3270{
3271 struct cgroup_subsys_state *css;
3272 struct mem_cgroup *parent, *child;
3273 int kmemcg_id;
3274
3275 if (memcg->kmem_state != KMEM_ONLINE)
3276 return;
3277 /*
3278 * Clear the online state before clearing memcg_caches array
3279 * entries. The slab_mutex in memcg_deactivate_kmem_caches()
3280 * guarantees that no cache will be created for this cgroup
3281 * after we are done (see memcg_create_kmem_cache()).
3282 */
3283 memcg->kmem_state = KMEM_ALLOCATED;
3284
3285 memcg_deactivate_kmem_caches(memcg);
3286
3287 kmemcg_id = memcg->kmemcg_id;
3288 BUG_ON(kmemcg_id < 0);
3289
3290 parent = parent_mem_cgroup(memcg);
3291 if (!parent)
3292 parent = root_mem_cgroup;
3293
3294 /*
3295 * Change kmemcg_id of this cgroup and all its descendants to the
3296 * parent's id, and then move all entries from this cgroup's list_lrus
3297 * to ones of the parent. After we have finished, all list_lrus
3298 * corresponding to this cgroup are guaranteed to remain empty. The
3299 * ordering is imposed by list_lru_node->lock taken by
3300 * memcg_drain_all_list_lrus().
3301 */
Tejun Heo3a06bb72016-06-03 14:55:44 -07003302 rcu_read_lock(); /* can be called from css_free w/o cgroup_mutex */
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003303 css_for_each_descendant_pre(css, &memcg->css) {
3304 child = mem_cgroup_from_css(css);
3305 BUG_ON(child->kmemcg_id != kmemcg_id);
3306 child->kmemcg_id = parent->kmemcg_id;
3307 if (!memcg->use_hierarchy)
3308 break;
3309 }
Tejun Heo3a06bb72016-06-03 14:55:44 -07003310 rcu_read_unlock();
3311
Kirill Tkhai9bec5c32018-08-17 15:47:58 -07003312 memcg_drain_all_list_lrus(kmemcg_id, parent);
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003313
3314 memcg_free_cache_id(kmemcg_id);
3315}
3316
3317static void memcg_free_kmem(struct mem_cgroup *memcg)
3318{
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08003319 /* css_alloc() failed, offlining didn't happen */
3320 if (unlikely(memcg->kmem_state == KMEM_ONLINE))
3321 memcg_offline_kmem(memcg);
3322
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003323 if (memcg->kmem_state == KMEM_ALLOCATED) {
Roman Gushchinf0a3a242019-07-11 20:56:27 -07003324 WARN_ON(!list_empty(&memcg->kmem_caches));
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003325 static_branch_dec(&memcg_kmem_enabled_key);
3326 WARN_ON(page_counter_read(&memcg->kmem));
3327 }
Johannes Weiner8e0a8912016-01-20 15:02:26 -08003328}
Vladimir Davydovd6441632014-01-23 15:53:09 -08003329#else
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08003330static int memcg_online_kmem(struct mem_cgroup *memcg)
Johannes Weiner127424c2016-01-20 15:02:32 -08003331{
3332 return 0;
3333}
3334static void memcg_offline_kmem(struct mem_cgroup *memcg)
3335{
3336}
3337static void memcg_free_kmem(struct mem_cgroup *memcg)
3338{
3339}
Kirill Tkhai84c07d12018-08-17 15:47:25 -07003340#endif /* CONFIG_MEMCG_KMEM */
Johannes Weiner127424c2016-01-20 15:02:32 -08003341
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003342static int memcg_update_kmem_max(struct mem_cgroup *memcg,
3343 unsigned long max)
Johannes Weiner127424c2016-01-20 15:02:32 -08003344{
Vladimir Davydovb313aee2016-03-17 14:18:27 -07003345 int ret;
Johannes Weiner127424c2016-01-20 15:02:32 -08003346
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003347 mutex_lock(&memcg_max_mutex);
3348 ret = page_counter_set_max(&memcg->kmem, max);
3349 mutex_unlock(&memcg_max_mutex);
Johannes Weiner127424c2016-01-20 15:02:32 -08003350 return ret;
3351}
Glauber Costa510fc4e2012-12-18 14:21:47 -08003352
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003353static int memcg_update_tcp_max(struct mem_cgroup *memcg, unsigned long max)
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003354{
3355 int ret;
3356
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003357 mutex_lock(&memcg_max_mutex);
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003358
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003359 ret = page_counter_set_max(&memcg->tcpmem, max);
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003360 if (ret)
3361 goto out;
3362
Johannes Weiner0db15292016-01-20 15:02:50 -08003363 if (!memcg->tcpmem_active) {
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003364 /*
3365 * The active flag needs to be written after the static_key
3366 * update. This is what guarantees that the socket activation
Johannes Weiner2d758072016-10-07 17:00:58 -07003367 * function is the last one to run. See mem_cgroup_sk_alloc()
3368 * for details, and note that we don't mark any socket as
3369 * belonging to this memcg until that flag is up.
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003370 *
3371 * We need to do this, because static_keys will span multiple
3372 * sites, but we can't control their order. If we mark a socket
3373 * as accounted, but the accounting functions are not patched in
3374 * yet, we'll lose accounting.
3375 *
Johannes Weiner2d758072016-10-07 17:00:58 -07003376 * We never race with the readers in mem_cgroup_sk_alloc(),
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003377 * because when this value change, the code to process it is not
3378 * patched in yet.
3379 */
3380 static_branch_inc(&memcg_sockets_enabled_key);
Johannes Weiner0db15292016-01-20 15:02:50 -08003381 memcg->tcpmem_active = true;
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003382 }
3383out:
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003384 mutex_unlock(&memcg_max_mutex);
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003385 return ret;
3386}
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003387
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003388/*
3389 * The user of this function is...
3390 * RES_LIMIT.
3391 */
Tejun Heo451af502014-05-13 12:16:21 -04003392static ssize_t mem_cgroup_write(struct kernfs_open_file *of,
3393 char *buf, size_t nbytes, loff_t off)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003394{
Tejun Heo451af502014-05-13 12:16:21 -04003395 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003396 unsigned long nr_pages;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003397 int ret;
3398
Tejun Heo451af502014-05-13 12:16:21 -04003399 buf = strstrip(buf);
Johannes Weiner650c5e52015-02-11 15:26:03 -08003400 ret = page_counter_memparse(buf, "-1", &nr_pages);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003401 if (ret)
3402 return ret;
Tejun Heoaf36f902012-04-01 12:09:55 -07003403
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003404 switch (MEMFILE_ATTR(of_cft(of)->private)) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003405 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003406 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3407 ret = -EINVAL;
3408 break;
3409 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003410 switch (MEMFILE_TYPE(of_cft(of)->private)) {
3411 case _MEM:
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003412 ret = mem_cgroup_resize_max(memcg, nr_pages, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003413 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003414 case _MEMSWAP:
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003415 ret = mem_cgroup_resize_max(memcg, nr_pages, true);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003416 break;
3417 case _KMEM:
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003418 ret = memcg_update_kmem_max(memcg, nr_pages);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003419 break;
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003420 case _TCP:
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003421 ret = memcg_update_tcp_max(memcg, nr_pages);
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003422 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003423 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003424 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003425 case RES_SOFT_LIMIT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003426 memcg->soft_limit = nr_pages;
3427 ret = 0;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003428 break;
3429 }
Tejun Heo451af502014-05-13 12:16:21 -04003430 return ret ?: nbytes;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003431}
3432
Tejun Heo6770c642014-05-13 12:16:21 -04003433static ssize_t mem_cgroup_reset(struct kernfs_open_file *of, char *buf,
3434 size_t nbytes, loff_t off)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003435{
Tejun Heo6770c642014-05-13 12:16:21 -04003436 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003437 struct page_counter *counter;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003438
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003439 switch (MEMFILE_TYPE(of_cft(of)->private)) {
3440 case _MEM:
3441 counter = &memcg->memory;
3442 break;
3443 case _MEMSWAP:
3444 counter = &memcg->memsw;
3445 break;
3446 case _KMEM:
3447 counter = &memcg->kmem;
3448 break;
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003449 case _TCP:
Johannes Weiner0db15292016-01-20 15:02:50 -08003450 counter = &memcg->tcpmem;
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08003451 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003452 default:
3453 BUG();
3454 }
Tejun Heoaf36f902012-04-01 12:09:55 -07003455
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003456 switch (MEMFILE_ATTR(of_cft(of)->private)) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003457 case RES_MAX_USAGE:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003458 page_counter_reset_watermark(counter);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003459 break;
3460 case RES_FAILCNT:
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003461 counter->failcnt = 0;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003462 break;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003463 default:
3464 BUG();
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003465 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003466
Tejun Heo6770c642014-05-13 12:16:21 -04003467 return nbytes;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003468}
3469
Tejun Heo182446d2013-08-08 20:11:24 -04003470static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003471 struct cftype *cft)
3472{
Tejun Heo182446d2013-08-08 20:11:24 -04003473 return mem_cgroup_from_css(css)->move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003474}
3475
Daisuke Nishimura02491442010-03-10 15:22:17 -08003476#ifdef CONFIG_MMU
Tejun Heo182446d2013-08-08 20:11:24 -04003477static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003478 struct cftype *cft, u64 val)
3479{
Tejun Heo182446d2013-08-08 20:11:24 -04003480 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003481
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08003482 if (val & ~MOVE_MASK)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003483 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003484
Glauber Costaee5e8472013-02-22 16:34:50 -08003485 /*
3486 * No kind of locking is needed in here, because ->can_attach() will
3487 * check this value once in the beginning of the process, and then carry
3488 * on with stale data. This means that changes to this value will only
3489 * affect task migrations starting after the change.
3490 */
3491 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003492 return 0;
3493}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003494#else
Tejun Heo182446d2013-08-08 20:11:24 -04003495static int mem_cgroup_move_charge_write(struct cgroup_subsys_state *css,
Daisuke Nishimura02491442010-03-10 15:22:17 -08003496 struct cftype *cft, u64 val)
3497{
3498 return -ENOSYS;
3499}
3500#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003501
Ying Han406eb0c2011-05-26 16:25:37 -07003502#ifdef CONFIG_NUMA
Johannes Weiner113b7df2019-05-13 17:18:11 -07003503
3504#define LRU_ALL_FILE (BIT(LRU_INACTIVE_FILE) | BIT(LRU_ACTIVE_FILE))
3505#define LRU_ALL_ANON (BIT(LRU_INACTIVE_ANON) | BIT(LRU_ACTIVE_ANON))
3506#define LRU_ALL ((1 << NR_LRU_LISTS) - 1)
3507
3508static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
3509 int nid, unsigned int lru_mask)
3510{
3511 struct lruvec *lruvec = mem_cgroup_lruvec(NODE_DATA(nid), memcg);
3512 unsigned long nr = 0;
3513 enum lru_list lru;
3514
3515 VM_BUG_ON((unsigned)nid >= nr_node_ids);
3516
3517 for_each_lru(lru) {
3518 if (!(BIT(lru) & lru_mask))
3519 continue;
Johannes Weiner205b20c2019-05-14 15:47:06 -07003520 nr += lruvec_page_state_local(lruvec, NR_LRU_BASE + lru);
Johannes Weiner113b7df2019-05-13 17:18:11 -07003521 }
3522 return nr;
3523}
3524
3525static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
3526 unsigned int lru_mask)
3527{
3528 unsigned long nr = 0;
3529 enum lru_list lru;
3530
3531 for_each_lru(lru) {
3532 if (!(BIT(lru) & lru_mask))
3533 continue;
Johannes Weiner205b20c2019-05-14 15:47:06 -07003534 nr += memcg_page_state_local(memcg, NR_LRU_BASE + lru);
Johannes Weiner113b7df2019-05-13 17:18:11 -07003535 }
3536 return nr;
3537}
3538
Tejun Heo2da8ca82013-12-05 12:28:04 -05003539static int memcg_numa_stat_show(struct seq_file *m, void *v)
Ying Han406eb0c2011-05-26 16:25:37 -07003540{
Greg Thelen25485de2013-11-12 15:07:40 -08003541 struct numa_stat {
3542 const char *name;
3543 unsigned int lru_mask;
3544 };
3545
3546 static const struct numa_stat stats[] = {
3547 { "total", LRU_ALL },
3548 { "file", LRU_ALL_FILE },
3549 { "anon", LRU_ALL_ANON },
3550 { "unevictable", BIT(LRU_UNEVICTABLE) },
3551 };
3552 const struct numa_stat *stat;
Ying Han406eb0c2011-05-26 16:25:37 -07003553 int nid;
Greg Thelen25485de2013-11-12 15:07:40 -08003554 unsigned long nr;
Chris Downaa9694b2019-03-05 15:45:52 -08003555 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
Ying Han406eb0c2011-05-26 16:25:37 -07003556
Greg Thelen25485de2013-11-12 15:07:40 -08003557 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
3558 nr = mem_cgroup_nr_lru_pages(memcg, stat->lru_mask);
3559 seq_printf(m, "%s=%lu", stat->name, nr);
3560 for_each_node_state(nid, N_MEMORY) {
3561 nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
3562 stat->lru_mask);
3563 seq_printf(m, " N%d=%lu", nid, nr);
3564 }
3565 seq_putc(m, '\n');
Ying Han406eb0c2011-05-26 16:25:37 -07003566 }
Ying Han406eb0c2011-05-26 16:25:37 -07003567
Ying Han071aee12013-11-12 15:07:41 -08003568 for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) {
3569 struct mem_cgroup *iter;
Ying Han406eb0c2011-05-26 16:25:37 -07003570
Ying Han071aee12013-11-12 15:07:41 -08003571 nr = 0;
3572 for_each_mem_cgroup_tree(iter, memcg)
3573 nr += mem_cgroup_nr_lru_pages(iter, stat->lru_mask);
3574 seq_printf(m, "hierarchical_%s=%lu", stat->name, nr);
3575 for_each_node_state(nid, N_MEMORY) {
3576 nr = 0;
3577 for_each_mem_cgroup_tree(iter, memcg)
3578 nr += mem_cgroup_node_nr_lru_pages(
3579 iter, nid, stat->lru_mask);
3580 seq_printf(m, " N%d=%lu", nid, nr);
3581 }
3582 seq_putc(m, '\n');
Ying Han406eb0c2011-05-26 16:25:37 -07003583 }
Ying Han406eb0c2011-05-26 16:25:37 -07003584
Ying Han406eb0c2011-05-26 16:25:37 -07003585 return 0;
3586}
3587#endif /* CONFIG_NUMA */
3588
Johannes Weinerc8713d02019-07-11 20:55:59 -07003589static const unsigned int memcg1_stats[] = {
3590 MEMCG_CACHE,
3591 MEMCG_RSS,
3592 MEMCG_RSS_HUGE,
3593 NR_SHMEM,
3594 NR_FILE_MAPPED,
3595 NR_FILE_DIRTY,
3596 NR_WRITEBACK,
3597 MEMCG_SWAP,
3598};
3599
3600static const char *const memcg1_stat_names[] = {
3601 "cache",
3602 "rss",
3603 "rss_huge",
3604 "shmem",
3605 "mapped_file",
3606 "dirty",
3607 "writeback",
3608 "swap",
3609};
3610
Johannes Weinerdf0e53d2017-05-03 14:55:10 -07003611/* Universal VM events cgroup1 shows, original sort order */
Greg Thelen8dd53fd2018-06-07 17:07:23 -07003612static const unsigned int memcg1_events[] = {
Johannes Weinerdf0e53d2017-05-03 14:55:10 -07003613 PGPGIN,
3614 PGPGOUT,
3615 PGFAULT,
3616 PGMAJFAULT,
3617};
3618
3619static const char *const memcg1_event_names[] = {
3620 "pgpgin",
3621 "pgpgout",
3622 "pgfault",
3623 "pgmajfault",
3624};
3625
Tejun Heo2da8ca82013-12-05 12:28:04 -05003626static int memcg_stat_show(struct seq_file *m, void *v)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003627{
Chris Downaa9694b2019-03-05 15:45:52 -08003628 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003629 unsigned long memory, memsw;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003630 struct mem_cgroup *mi;
3631 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003632
Johannes Weiner71cd3112017-05-03 14:55:13 -07003633 BUILD_BUG_ON(ARRAY_SIZE(memcg1_stat_names) != ARRAY_SIZE(memcg1_stats));
Rickard Strandqvist70bc0682014-12-12 16:56:41 -08003634 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
3635
Johannes Weiner71cd3112017-05-03 14:55:13 -07003636 for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) {
3637 if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account())
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003638 continue;
Johannes Weiner71cd3112017-05-03 14:55:13 -07003639 seq_printf(m, "%s %lu\n", memcg1_stat_names[i],
Johannes Weiner205b20c2019-05-14 15:47:06 -07003640 memcg_page_state_local(memcg, memcg1_stats[i]) *
Johannes Weiner71cd3112017-05-03 14:55:13 -07003641 PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003642 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003643
Johannes Weinerdf0e53d2017-05-03 14:55:10 -07003644 for (i = 0; i < ARRAY_SIZE(memcg1_events); i++)
3645 seq_printf(m, "%s %lu\n", memcg1_event_names[i],
Johannes Weiner205b20c2019-05-14 15:47:06 -07003646 memcg_events_local(memcg, memcg1_events[i]));
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003647
3648 for (i = 0; i < NR_LRU_LISTS; i++)
3649 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
Johannes Weiner205b20c2019-05-14 15:47:06 -07003650 memcg_page_state_local(memcg, NR_LRU_BASE + i) *
Johannes Weiner21d89d12019-05-13 17:18:08 -07003651 PAGE_SIZE);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003652
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003653 /* Hierarchical information */
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003654 memory = memsw = PAGE_COUNTER_MAX;
3655 for (mi = memcg; mi; mi = parent_mem_cgroup(mi)) {
Roman Gushchinbbec2e12018-06-07 17:06:18 -07003656 memory = min(memory, mi->memory.max);
3657 memsw = min(memsw, mi->memsw.max);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003658 }
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003659 seq_printf(m, "hierarchical_memory_limit %llu\n",
3660 (u64)memory * PAGE_SIZE);
Johannes Weiner7941d212016-01-14 15:21:23 -08003661 if (do_memsw_account())
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003662 seq_printf(m, "hierarchical_memsw_limit %llu\n",
3663 (u64)memsw * PAGE_SIZE);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003664
Shakeel Butt8de7ecc62018-08-21 21:53:17 -07003665 for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) {
Johannes Weiner71cd3112017-05-03 14:55:13 -07003666 if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account())
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003667 continue;
Shakeel Butt8de7ecc62018-08-21 21:53:17 -07003668 seq_printf(m, "total_%s %llu\n", memcg1_stat_names[i],
Yafang Shaodd923992019-07-11 20:52:11 -07003669 (u64)memcg_page_state(memcg, memcg1_stats[i]) *
3670 PAGE_SIZE);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003671 }
3672
Shakeel Butt8de7ecc62018-08-21 21:53:17 -07003673 for (i = 0; i < ARRAY_SIZE(memcg1_events); i++)
3674 seq_printf(m, "total_%s %llu\n", memcg1_event_names[i],
Yafang Shaodd923992019-07-11 20:52:11 -07003675 (u64)memcg_events(memcg, memcg1_events[i]));
Johannes Weineraf7c4b02012-05-29 15:07:08 -07003676
Shakeel Butt8de7ecc62018-08-21 21:53:17 -07003677 for (i = 0; i < NR_LRU_LISTS; i++)
3678 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i],
Johannes Weiner42a30032019-05-14 15:47:12 -07003679 (u64)memcg_page_state(memcg, NR_LRU_BASE + i) *
3680 PAGE_SIZE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003681
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003682#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003683 {
Mel Gormanef8f2322016-07-28 15:46:05 -07003684 pg_data_t *pgdat;
3685 struct mem_cgroup_per_node *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07003686 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003687 unsigned long recent_rotated[2] = {0, 0};
3688 unsigned long recent_scanned[2] = {0, 0};
3689
Mel Gormanef8f2322016-07-28 15:46:05 -07003690 for_each_online_pgdat(pgdat) {
3691 mz = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
3692 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003693
Mel Gormanef8f2322016-07-28 15:46:05 -07003694 recent_rotated[0] += rstat->recent_rotated[0];
3695 recent_rotated[1] += rstat->recent_rotated[1];
3696 recent_scanned[0] += rstat->recent_scanned[0];
3697 recent_scanned[1] += rstat->recent_scanned[1];
3698 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07003699 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
3700 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
3701 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
3702 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003703 }
3704#endif
3705
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003706 return 0;
3707}
3708
Tejun Heo182446d2013-08-08 20:11:24 -04003709static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css,
3710 struct cftype *cft)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003711{
Tejun Heo182446d2013-08-08 20:11:24 -04003712 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003713
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07003714 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003715}
3716
Tejun Heo182446d2013-08-08 20:11:24 -04003717static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
3718 struct cftype *cft, u64 val)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003719{
Tejun Heo182446d2013-08-08 20:11:24 -04003720 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Li Zefan068b38c2009-01-15 13:51:26 -08003721
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07003722 if (val > 100)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003723 return -EINVAL;
3724
Linus Torvalds14208b02014-06-09 15:03:33 -07003725 if (css->parent)
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07003726 memcg->swappiness = val;
3727 else
3728 vm_swappiness = val;
Li Zefan068b38c2009-01-15 13:51:26 -08003729
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003730 return 0;
3731}
3732
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003733static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
3734{
3735 struct mem_cgroup_threshold_ary *t;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003736 unsigned long usage;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003737 int i;
3738
3739 rcu_read_lock();
3740 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003741 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003742 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003743 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003744
3745 if (!t)
3746 goto unlock;
3747
Johannes Weinerce00a962014-09-05 08:43:57 -04003748 usage = mem_cgroup_usage(memcg, swap);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003749
3750 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07003751 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003752 * If it's not true, a threshold was crossed after last
3753 * call of __mem_cgroup_threshold().
3754 */
Phil Carmody5407a562010-05-26 14:42:42 -07003755 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003756
3757 /*
3758 * Iterate backward over array of thresholds starting from
3759 * current_threshold and check if a threshold is crossed.
3760 * If none of thresholds below usage is crossed, we read
3761 * only one element of the array here.
3762 */
3763 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
3764 eventfd_signal(t->entries[i].eventfd, 1);
3765
3766 /* i = current_threshold + 1 */
3767 i++;
3768
3769 /*
3770 * Iterate forward over array of thresholds starting from
3771 * current_threshold+1 and check if a threshold is crossed.
3772 * If none of thresholds above usage is crossed, we read
3773 * only one element of the array here.
3774 */
3775 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
3776 eventfd_signal(t->entries[i].eventfd, 1);
3777
3778 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07003779 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003780unlock:
3781 rcu_read_unlock();
3782}
3783
3784static void mem_cgroup_threshold(struct mem_cgroup *memcg)
3785{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07003786 while (memcg) {
3787 __mem_cgroup_threshold(memcg, false);
Johannes Weiner7941d212016-01-14 15:21:23 -08003788 if (do_memsw_account())
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07003789 __mem_cgroup_threshold(memcg, true);
3790
3791 memcg = parent_mem_cgroup(memcg);
3792 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003793}
3794
3795static int compare_thresholds(const void *a, const void *b)
3796{
3797 const struct mem_cgroup_threshold *_a = a;
3798 const struct mem_cgroup_threshold *_b = b;
3799
Greg Thelen2bff24a2013-09-11 14:23:08 -07003800 if (_a->threshold > _b->threshold)
3801 return 1;
3802
3803 if (_a->threshold < _b->threshold)
3804 return -1;
3805
3806 return 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003807}
3808
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003809static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003810{
3811 struct mem_cgroup_eventfd_list *ev;
3812
Michal Hocko2bcf2e92014-07-30 16:08:33 -07003813 spin_lock(&memcg_oom_lock);
3814
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003815 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003816 eventfd_signal(ev->eventfd, 1);
Michal Hocko2bcf2e92014-07-30 16:08:33 -07003817
3818 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003819 return 0;
3820}
3821
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003822static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003823{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003824 struct mem_cgroup *iter;
3825
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003826 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003827 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003828}
3829
Tejun Heo59b6f872013-11-22 18:20:43 -05003830static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003831 struct eventfd_ctx *eventfd, const char *args, enum res_type type)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003832{
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003833 struct mem_cgroup_thresholds *thresholds;
3834 struct mem_cgroup_threshold_ary *new;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003835 unsigned long threshold;
3836 unsigned long usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003837 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003838
Johannes Weiner650c5e52015-02-11 15:26:03 -08003839 ret = page_counter_memparse(args, "-1", &threshold);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003840 if (ret)
3841 return ret;
3842
3843 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003844
Johannes Weiner05b84302014-08-06 16:05:59 -07003845 if (type == _MEM) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003846 thresholds = &memcg->thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04003847 usage = mem_cgroup_usage(memcg, false);
Johannes Weiner05b84302014-08-06 16:05:59 -07003848 } else if (type == _MEMSWAP) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003849 thresholds = &memcg->memsw_thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04003850 usage = mem_cgroup_usage(memcg, true);
Johannes Weiner05b84302014-08-06 16:05:59 -07003851 } else
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003852 BUG();
3853
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003854 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003855 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003856 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3857
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003858 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003859
3860 /* Allocate memory for new array of thresholds */
Gustavo A. R. Silva67b80462019-03-05 15:44:05 -08003861 new = kmalloc(struct_size(new, entries, size), GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003862 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003863 ret = -ENOMEM;
3864 goto unlock;
3865 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003866 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003867
3868 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003869 if (thresholds->primary) {
3870 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003871 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003872 }
3873
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003874 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003875 new->entries[size - 1].eventfd = eventfd;
3876 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003877
3878 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003879 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003880 compare_thresholds, NULL);
3881
3882 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003883 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003884 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07003885 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003886 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003887 * new->current_threshold will not be used until
3888 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003889 * it here.
3890 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003891 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07003892 } else
3893 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003894 }
3895
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003896 /* Free old spare buffer and save old primary buffer as spare */
3897 kfree(thresholds->spare);
3898 thresholds->spare = thresholds->primary;
3899
3900 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003901
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003902 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003903 synchronize_rcu();
3904
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003905unlock:
3906 mutex_unlock(&memcg->thresholds_lock);
3907
3908 return ret;
3909}
3910
Tejun Heo59b6f872013-11-22 18:20:43 -05003911static int mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003912 struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003913{
Tejun Heo59b6f872013-11-22 18:20:43 -05003914 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEM);
Tejun Heo347c4a82013-11-22 18:20:43 -05003915}
3916
Tejun Heo59b6f872013-11-22 18:20:43 -05003917static int memsw_cgroup_usage_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003918 struct eventfd_ctx *eventfd, const char *args)
3919{
Tejun Heo59b6f872013-11-22 18:20:43 -05003920 return __mem_cgroup_usage_register_event(memcg, eventfd, args, _MEMSWAP);
Tejun Heo347c4a82013-11-22 18:20:43 -05003921}
3922
Tejun Heo59b6f872013-11-22 18:20:43 -05003923static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05003924 struct eventfd_ctx *eventfd, enum res_type type)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003925{
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003926 struct mem_cgroup_thresholds *thresholds;
3927 struct mem_cgroup_threshold_ary *new;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08003928 unsigned long usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003929 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003930
3931 mutex_lock(&memcg->thresholds_lock);
Johannes Weiner05b84302014-08-06 16:05:59 -07003932
3933 if (type == _MEM) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003934 thresholds = &memcg->thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04003935 usage = mem_cgroup_usage(memcg, false);
Johannes Weiner05b84302014-08-06 16:05:59 -07003936 } else if (type == _MEMSWAP) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003937 thresholds = &memcg->memsw_thresholds;
Johannes Weinerce00a962014-09-05 08:43:57 -04003938 usage = mem_cgroup_usage(memcg, true);
Johannes Weiner05b84302014-08-06 16:05:59 -07003939 } else
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003940 BUG();
3941
Anton Vorontsov371528c2012-02-24 05:14:46 +04003942 if (!thresholds->primary)
3943 goto unlock;
3944
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003945 /* Check if a threshold crossed before removing */
3946 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3947
3948 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003949 size = 0;
3950 for (i = 0; i < thresholds->primary->size; i++) {
3951 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003952 size++;
3953 }
3954
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003955 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003956
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003957 /* Set thresholds array to NULL if we don't have thresholds */
3958 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003959 kfree(new);
3960 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003961 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003962 }
3963
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003964 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003965
3966 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003967 new->current_threshold = -1;
3968 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
3969 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003970 continue;
3971
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003972 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07003973 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003974 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003975 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003976 * until rcu_assign_pointer(), so it's safe to increment
3977 * it here.
3978 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003979 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003980 }
3981 j++;
3982 }
3983
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003984swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003985 /* Swap primary and spare array */
3986 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07003987
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003988 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003989
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003990 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003991 synchronize_rcu();
Martijn Coenen6611d8d2016-01-15 16:57:49 -08003992
3993 /* If all events are unregistered, free the spare array */
3994 if (!new) {
3995 kfree(thresholds->spare);
3996 thresholds->spare = NULL;
3997 }
Anton Vorontsov371528c2012-02-24 05:14:46 +04003998unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003999 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004000}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004001
Tejun Heo59b6f872013-11-22 18:20:43 -05004002static void mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004003 struct eventfd_ctx *eventfd)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004004{
Tejun Heo59b6f872013-11-22 18:20:43 -05004005 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEM);
Tejun Heo347c4a82013-11-22 18:20:43 -05004006}
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004007
Tejun Heo59b6f872013-11-22 18:20:43 -05004008static void memsw_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004009 struct eventfd_ctx *eventfd)
4010{
Tejun Heo59b6f872013-11-22 18:20:43 -05004011 return __mem_cgroup_usage_unregister_event(memcg, eventfd, _MEMSWAP);
Tejun Heo347c4a82013-11-22 18:20:43 -05004012}
4013
Tejun Heo59b6f872013-11-22 18:20:43 -05004014static int mem_cgroup_oom_register_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004015 struct eventfd_ctx *eventfd, const char *args)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004016{
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004017 struct mem_cgroup_eventfd_list *event;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004018
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004019 event = kmalloc(sizeof(*event), GFP_KERNEL);
4020 if (!event)
4021 return -ENOMEM;
4022
Michal Hocko1af8efe2011-07-26 16:08:24 -07004023 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004024
4025 event->eventfd = eventfd;
4026 list_add(&event->list, &memcg->oom_notify);
4027
4028 /* already in OOM ? */
Tejun Heoc2b42d32015-06-24 16:58:23 -07004029 if (memcg->under_oom)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004030 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004031 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004032
4033 return 0;
4034}
4035
Tejun Heo59b6f872013-11-22 18:20:43 -05004036static void mem_cgroup_oom_unregister_event(struct mem_cgroup *memcg,
Tejun Heo347c4a82013-11-22 18:20:43 -05004037 struct eventfd_ctx *eventfd)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004038{
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004039 struct mem_cgroup_eventfd_list *ev, *tmp;
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004040
Michal Hocko1af8efe2011-07-26 16:08:24 -07004041 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004042
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004043 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004044 if (ev->eventfd == eventfd) {
4045 list_del(&ev->list);
4046 kfree(ev);
4047 }
4048 }
4049
Michal Hocko1af8efe2011-07-26 16:08:24 -07004050 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004051}
4052
Tejun Heo2da8ca82013-12-05 12:28:04 -05004053static int mem_cgroup_oom_control_read(struct seq_file *sf, void *v)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004054{
Chris Downaa9694b2019-03-05 15:45:52 -08004055 struct mem_cgroup *memcg = mem_cgroup_from_seq(sf);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004056
Tejun Heo791badb2013-12-05 12:28:02 -05004057 seq_printf(sf, "oom_kill_disable %d\n", memcg->oom_kill_disable);
Tejun Heoc2b42d32015-06-24 16:58:23 -07004058 seq_printf(sf, "under_oom %d\n", (bool)memcg->under_oom);
Roman Gushchinfe6bdfc2018-06-14 15:28:05 -07004059 seq_printf(sf, "oom_kill %lu\n",
4060 atomic_long_read(&memcg->memory_events[MEMCG_OOM_KILL]));
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004061 return 0;
4062}
4063
Tejun Heo182446d2013-08-08 20:11:24 -04004064static int mem_cgroup_oom_control_write(struct cgroup_subsys_state *css,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004065 struct cftype *cft, u64 val)
4066{
Tejun Heo182446d2013-08-08 20:11:24 -04004067 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004068
4069 /* cannot set to root cgroup and only 0 and 1 are allowed */
Linus Torvalds14208b02014-06-09 15:03:33 -07004070 if (!css->parent || !((val == 0) || (val == 1)))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004071 return -EINVAL;
4072
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004073 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004074 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004075 memcg_oom_recover(memcg);
Johannes Weiner3dae7fe2014-06-04 16:07:01 -07004076
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004077 return 0;
4078}
4079
Tejun Heo52ebea72015-05-22 17:13:37 -04004080#ifdef CONFIG_CGROUP_WRITEBACK
4081
Tejun Heo841710a2015-05-22 18:23:33 -04004082static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp)
4083{
4084 return wb_domain_init(&memcg->cgwb_domain, gfp);
4085}
4086
4087static void memcg_wb_domain_exit(struct mem_cgroup *memcg)
4088{
4089 wb_domain_exit(&memcg->cgwb_domain);
4090}
4091
Tejun Heo2529bb32015-05-22 18:23:34 -04004092static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)
4093{
4094 wb_domain_size_changed(&memcg->cgwb_domain);
4095}
4096
Tejun Heo841710a2015-05-22 18:23:33 -04004097struct wb_domain *mem_cgroup_wb_domain(struct bdi_writeback *wb)
4098{
4099 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
4100
4101 if (!memcg->css.parent)
4102 return NULL;
4103
4104 return &memcg->cgwb_domain;
4105}
4106
Greg Thelen0b3d6e62019-04-05 18:39:18 -07004107/*
4108 * idx can be of type enum memcg_stat_item or node_stat_item.
4109 * Keep in sync with memcg_exact_page().
4110 */
4111static unsigned long memcg_exact_page_state(struct mem_cgroup *memcg, int idx)
4112{
Chris Down871789d2019-05-14 15:46:57 -07004113 long x = atomic_long_read(&memcg->vmstats[idx]);
Greg Thelen0b3d6e62019-04-05 18:39:18 -07004114 int cpu;
4115
4116 for_each_online_cpu(cpu)
Chris Down871789d2019-05-14 15:46:57 -07004117 x += per_cpu_ptr(memcg->vmstats_percpu, cpu)->stat[idx];
Greg Thelen0b3d6e62019-04-05 18:39:18 -07004118 if (x < 0)
4119 x = 0;
4120 return x;
4121}
4122
Tejun Heoc2aa7232015-05-22 18:23:35 -04004123/**
4124 * mem_cgroup_wb_stats - retrieve writeback related stats from its memcg
4125 * @wb: bdi_writeback in question
Tejun Heoc5edf9c2015-09-29 13:04:26 -04004126 * @pfilepages: out parameter for number of file pages
4127 * @pheadroom: out parameter for number of allocatable pages according to memcg
Tejun Heoc2aa7232015-05-22 18:23:35 -04004128 * @pdirty: out parameter for number of dirty pages
4129 * @pwriteback: out parameter for number of pages under writeback
4130 *
Tejun Heoc5edf9c2015-09-29 13:04:26 -04004131 * Determine the numbers of file, headroom, dirty, and writeback pages in
4132 * @wb's memcg. File, dirty and writeback are self-explanatory. Headroom
4133 * is a bit more involved.
Tejun Heoc2aa7232015-05-22 18:23:35 -04004134 *
Tejun Heoc5edf9c2015-09-29 13:04:26 -04004135 * A memcg's headroom is "min(max, high) - used". In the hierarchy, the
4136 * headroom is calculated as the lowest headroom of itself and the
4137 * ancestors. Note that this doesn't consider the actual amount of
4138 * available memory in the system. The caller should further cap
4139 * *@pheadroom accordingly.
Tejun Heoc2aa7232015-05-22 18:23:35 -04004140 */
Tejun Heoc5edf9c2015-09-29 13:04:26 -04004141void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages,
4142 unsigned long *pheadroom, unsigned long *pdirty,
4143 unsigned long *pwriteback)
Tejun Heoc2aa7232015-05-22 18:23:35 -04004144{
4145 struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css);
4146 struct mem_cgroup *parent;
Tejun Heoc2aa7232015-05-22 18:23:35 -04004147
Greg Thelen0b3d6e62019-04-05 18:39:18 -07004148 *pdirty = memcg_exact_page_state(memcg, NR_FILE_DIRTY);
Tejun Heoc2aa7232015-05-22 18:23:35 -04004149
4150 /* this should eventually include NR_UNSTABLE_NFS */
Greg Thelen0b3d6e62019-04-05 18:39:18 -07004151 *pwriteback = memcg_exact_page_state(memcg, NR_WRITEBACK);
Johannes Weiner21d89d12019-05-13 17:18:08 -07004152 *pfilepages = memcg_exact_page_state(memcg, NR_INACTIVE_FILE) +
4153 memcg_exact_page_state(memcg, NR_ACTIVE_FILE);
Tejun Heoc5edf9c2015-09-29 13:04:26 -04004154 *pheadroom = PAGE_COUNTER_MAX;
Tejun Heoc2aa7232015-05-22 18:23:35 -04004155
Tejun Heoc2aa7232015-05-22 18:23:35 -04004156 while ((parent = parent_mem_cgroup(memcg))) {
Roman Gushchinbbec2e12018-06-07 17:06:18 -07004157 unsigned long ceiling = min(memcg->memory.max, memcg->high);
Tejun Heoc2aa7232015-05-22 18:23:35 -04004158 unsigned long used = page_counter_read(&memcg->memory);
4159
Tejun Heoc5edf9c2015-09-29 13:04:26 -04004160 *pheadroom = min(*pheadroom, ceiling - min(ceiling, used));
Tejun Heoc2aa7232015-05-22 18:23:35 -04004161 memcg = parent;
4162 }
Tejun Heoc2aa7232015-05-22 18:23:35 -04004163}
4164
Tejun Heo841710a2015-05-22 18:23:33 -04004165#else /* CONFIG_CGROUP_WRITEBACK */
4166
4167static int memcg_wb_domain_init(struct mem_cgroup *memcg, gfp_t gfp)
4168{
4169 return 0;
4170}
4171
4172static void memcg_wb_domain_exit(struct mem_cgroup *memcg)
4173{
4174}
4175
Tejun Heo2529bb32015-05-22 18:23:34 -04004176static void memcg_wb_domain_size_changed(struct mem_cgroup *memcg)
4177{
4178}
4179
Tejun Heo52ebea72015-05-22 17:13:37 -04004180#endif /* CONFIG_CGROUP_WRITEBACK */
4181
Tejun Heo79bd9812013-11-22 18:20:42 -05004182/*
Tejun Heo3bc942f2013-11-22 18:20:44 -05004183 * DO NOT USE IN NEW FILES.
4184 *
4185 * "cgroup.event_control" implementation.
4186 *
4187 * This is way over-engineered. It tries to support fully configurable
4188 * events for each user. Such level of flexibility is completely
4189 * unnecessary especially in the light of the planned unified hierarchy.
4190 *
4191 * Please deprecate this and replace with something simpler if at all
4192 * possible.
4193 */
4194
4195/*
Tejun Heo79bd9812013-11-22 18:20:42 -05004196 * Unregister event and free resources.
4197 *
4198 * Gets called from workqueue.
4199 */
Tejun Heo3bc942f2013-11-22 18:20:44 -05004200static void memcg_event_remove(struct work_struct *work)
Tejun Heo79bd9812013-11-22 18:20:42 -05004201{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004202 struct mem_cgroup_event *event =
4203 container_of(work, struct mem_cgroup_event, remove);
Tejun Heo59b6f872013-11-22 18:20:43 -05004204 struct mem_cgroup *memcg = event->memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004205
4206 remove_wait_queue(event->wqh, &event->wait);
4207
Tejun Heo59b6f872013-11-22 18:20:43 -05004208 event->unregister_event(memcg, event->eventfd);
Tejun Heo79bd9812013-11-22 18:20:42 -05004209
4210 /* Notify userspace the event is going away. */
4211 eventfd_signal(event->eventfd, 1);
4212
4213 eventfd_ctx_put(event->eventfd);
4214 kfree(event);
Tejun Heo59b6f872013-11-22 18:20:43 -05004215 css_put(&memcg->css);
Tejun Heo79bd9812013-11-22 18:20:42 -05004216}
4217
4218/*
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004219 * Gets called on EPOLLHUP on eventfd when user closes it.
Tejun Heo79bd9812013-11-22 18:20:42 -05004220 *
4221 * Called with wqh->lock held and interrupts disabled.
4222 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02004223static int memcg_event_wake(wait_queue_entry_t *wait, unsigned mode,
Tejun Heo3bc942f2013-11-22 18:20:44 -05004224 int sync, void *key)
Tejun Heo79bd9812013-11-22 18:20:42 -05004225{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004226 struct mem_cgroup_event *event =
4227 container_of(wait, struct mem_cgroup_event, wait);
Tejun Heo59b6f872013-11-22 18:20:43 -05004228 struct mem_cgroup *memcg = event->memcg;
Al Viro3ad6f932017-07-03 20:14:56 -04004229 __poll_t flags = key_to_poll(key);
Tejun Heo79bd9812013-11-22 18:20:42 -05004230
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004231 if (flags & EPOLLHUP) {
Tejun Heo79bd9812013-11-22 18:20:42 -05004232 /*
4233 * If the event has been detached at cgroup removal, we
4234 * can simply return knowing the other side will cleanup
4235 * for us.
4236 *
4237 * We can't race against event freeing since the other
4238 * side will require wqh->lock via remove_wait_queue(),
4239 * which we hold.
4240 */
Tejun Heofba94802013-11-22 18:20:43 -05004241 spin_lock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004242 if (!list_empty(&event->list)) {
4243 list_del_init(&event->list);
4244 /*
4245 * We are in atomic context, but cgroup_event_remove()
4246 * may sleep, so we have to call it in workqueue.
4247 */
4248 schedule_work(&event->remove);
4249 }
Tejun Heofba94802013-11-22 18:20:43 -05004250 spin_unlock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004251 }
4252
4253 return 0;
4254}
4255
Tejun Heo3bc942f2013-11-22 18:20:44 -05004256static void memcg_event_ptable_queue_proc(struct file *file,
Tejun Heo79bd9812013-11-22 18:20:42 -05004257 wait_queue_head_t *wqh, poll_table *pt)
4258{
Tejun Heo3bc942f2013-11-22 18:20:44 -05004259 struct mem_cgroup_event *event =
4260 container_of(pt, struct mem_cgroup_event, pt);
Tejun Heo79bd9812013-11-22 18:20:42 -05004261
4262 event->wqh = wqh;
4263 add_wait_queue(wqh, &event->wait);
4264}
4265
4266/*
Tejun Heo3bc942f2013-11-22 18:20:44 -05004267 * DO NOT USE IN NEW FILES.
4268 *
Tejun Heo79bd9812013-11-22 18:20:42 -05004269 * Parse input and register new cgroup event handler.
4270 *
4271 * Input must be in format '<event_fd> <control_fd> <args>'.
4272 * Interpretation of args is defined by control file implementation.
4273 */
Tejun Heo451af502014-05-13 12:16:21 -04004274static ssize_t memcg_write_event_control(struct kernfs_open_file *of,
4275 char *buf, size_t nbytes, loff_t off)
Tejun Heo79bd9812013-11-22 18:20:42 -05004276{
Tejun Heo451af502014-05-13 12:16:21 -04004277 struct cgroup_subsys_state *css = of_css(of);
Tejun Heofba94802013-11-22 18:20:43 -05004278 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo3bc942f2013-11-22 18:20:44 -05004279 struct mem_cgroup_event *event;
Tejun Heo79bd9812013-11-22 18:20:42 -05004280 struct cgroup_subsys_state *cfile_css;
4281 unsigned int efd, cfd;
4282 struct fd efile;
4283 struct fd cfile;
Tejun Heofba94802013-11-22 18:20:43 -05004284 const char *name;
Tejun Heo79bd9812013-11-22 18:20:42 -05004285 char *endp;
4286 int ret;
4287
Tejun Heo451af502014-05-13 12:16:21 -04004288 buf = strstrip(buf);
4289
4290 efd = simple_strtoul(buf, &endp, 10);
Tejun Heo79bd9812013-11-22 18:20:42 -05004291 if (*endp != ' ')
4292 return -EINVAL;
Tejun Heo451af502014-05-13 12:16:21 -04004293 buf = endp + 1;
Tejun Heo79bd9812013-11-22 18:20:42 -05004294
Tejun Heo451af502014-05-13 12:16:21 -04004295 cfd = simple_strtoul(buf, &endp, 10);
Tejun Heo79bd9812013-11-22 18:20:42 -05004296 if ((*endp != ' ') && (*endp != '\0'))
4297 return -EINVAL;
Tejun Heo451af502014-05-13 12:16:21 -04004298 buf = endp + 1;
Tejun Heo79bd9812013-11-22 18:20:42 -05004299
4300 event = kzalloc(sizeof(*event), GFP_KERNEL);
4301 if (!event)
4302 return -ENOMEM;
4303
Tejun Heo59b6f872013-11-22 18:20:43 -05004304 event->memcg = memcg;
Tejun Heo79bd9812013-11-22 18:20:42 -05004305 INIT_LIST_HEAD(&event->list);
Tejun Heo3bc942f2013-11-22 18:20:44 -05004306 init_poll_funcptr(&event->pt, memcg_event_ptable_queue_proc);
4307 init_waitqueue_func_entry(&event->wait, memcg_event_wake);
4308 INIT_WORK(&event->remove, memcg_event_remove);
Tejun Heo79bd9812013-11-22 18:20:42 -05004309
4310 efile = fdget(efd);
4311 if (!efile.file) {
4312 ret = -EBADF;
4313 goto out_kfree;
4314 }
4315
4316 event->eventfd = eventfd_ctx_fileget(efile.file);
4317 if (IS_ERR(event->eventfd)) {
4318 ret = PTR_ERR(event->eventfd);
4319 goto out_put_efile;
4320 }
4321
4322 cfile = fdget(cfd);
4323 if (!cfile.file) {
4324 ret = -EBADF;
4325 goto out_put_eventfd;
4326 }
4327
4328 /* the process need read permission on control file */
4329 /* AV: shouldn't we check that it's been opened for read instead? */
4330 ret = inode_permission(file_inode(cfile.file), MAY_READ);
4331 if (ret < 0)
4332 goto out_put_cfile;
4333
Tejun Heo79bd9812013-11-22 18:20:42 -05004334 /*
Tejun Heofba94802013-11-22 18:20:43 -05004335 * Determine the event callbacks and set them in @event. This used
4336 * to be done via struct cftype but cgroup core no longer knows
4337 * about these events. The following is crude but the whole thing
4338 * is for compatibility anyway.
Tejun Heo3bc942f2013-11-22 18:20:44 -05004339 *
4340 * DO NOT ADD NEW FILES.
Tejun Heofba94802013-11-22 18:20:43 -05004341 */
Al Virob5830432014-10-31 01:22:04 -04004342 name = cfile.file->f_path.dentry->d_name.name;
Tejun Heofba94802013-11-22 18:20:43 -05004343
4344 if (!strcmp(name, "memory.usage_in_bytes")) {
4345 event->register_event = mem_cgroup_usage_register_event;
4346 event->unregister_event = mem_cgroup_usage_unregister_event;
4347 } else if (!strcmp(name, "memory.oom_control")) {
4348 event->register_event = mem_cgroup_oom_register_event;
4349 event->unregister_event = mem_cgroup_oom_unregister_event;
4350 } else if (!strcmp(name, "memory.pressure_level")) {
4351 event->register_event = vmpressure_register_event;
4352 event->unregister_event = vmpressure_unregister_event;
4353 } else if (!strcmp(name, "memory.memsw.usage_in_bytes")) {
Tejun Heo347c4a82013-11-22 18:20:43 -05004354 event->register_event = memsw_cgroup_usage_register_event;
4355 event->unregister_event = memsw_cgroup_usage_unregister_event;
Tejun Heofba94802013-11-22 18:20:43 -05004356 } else {
4357 ret = -EINVAL;
4358 goto out_put_cfile;
4359 }
4360
4361 /*
Tejun Heob5557c42013-11-22 18:20:42 -05004362 * Verify @cfile should belong to @css. Also, remaining events are
4363 * automatically removed on cgroup destruction but the removal is
4364 * asynchronous, so take an extra ref on @css.
Tejun Heo79bd9812013-11-22 18:20:42 -05004365 */
Al Virob5830432014-10-31 01:22:04 -04004366 cfile_css = css_tryget_online_from_dir(cfile.file->f_path.dentry->d_parent,
Tejun Heoec903c02014-05-13 12:11:01 -04004367 &memory_cgrp_subsys);
Tejun Heo79bd9812013-11-22 18:20:42 -05004368 ret = -EINVAL;
Tejun Heo5a17f542014-02-11 11:52:47 -05004369 if (IS_ERR(cfile_css))
Tejun Heo79bd9812013-11-22 18:20:42 -05004370 goto out_put_cfile;
Tejun Heo5a17f542014-02-11 11:52:47 -05004371 if (cfile_css != css) {
4372 css_put(cfile_css);
4373 goto out_put_cfile;
4374 }
Tejun Heo79bd9812013-11-22 18:20:42 -05004375
Tejun Heo451af502014-05-13 12:16:21 -04004376 ret = event->register_event(memcg, event->eventfd, buf);
Tejun Heo79bd9812013-11-22 18:20:42 -05004377 if (ret)
4378 goto out_put_css;
4379
Christoph Hellwig9965ed172018-03-05 07:26:05 -08004380 vfs_poll(efile.file, &event->pt);
Tejun Heo79bd9812013-11-22 18:20:42 -05004381
Tejun Heofba94802013-11-22 18:20:43 -05004382 spin_lock(&memcg->event_list_lock);
4383 list_add(&event->list, &memcg->event_list);
4384 spin_unlock(&memcg->event_list_lock);
Tejun Heo79bd9812013-11-22 18:20:42 -05004385
4386 fdput(cfile);
4387 fdput(efile);
4388
Tejun Heo451af502014-05-13 12:16:21 -04004389 return nbytes;
Tejun Heo79bd9812013-11-22 18:20:42 -05004390
4391out_put_css:
Tejun Heob5557c42013-11-22 18:20:42 -05004392 css_put(css);
Tejun Heo79bd9812013-11-22 18:20:42 -05004393out_put_cfile:
4394 fdput(cfile);
4395out_put_eventfd:
4396 eventfd_ctx_put(event->eventfd);
4397out_put_efile:
4398 fdput(efile);
4399out_kfree:
4400 kfree(event);
4401
4402 return ret;
4403}
4404
Johannes Weiner241994ed2015-02-11 15:26:06 -08004405static struct cftype mem_cgroup_legacy_files[] = {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004406 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004407 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004408 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heo791badb2013-12-05 12:28:02 -05004409 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004410 },
4411 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004412 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004413 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Tejun Heo6770c642014-05-13 12:16:21 -04004414 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004415 .read_u64 = mem_cgroup_read_u64,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004416 },
4417 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004418 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004419 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004420 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004421 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004422 },
4423 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004424 .name = "soft_limit_in_bytes",
4425 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004426 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004427 .read_u64 = mem_cgroup_read_u64,
Balbir Singh296c81d2009-09-23 15:56:36 -07004428 },
4429 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004430 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004431 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Tejun Heo6770c642014-05-13 12:16:21 -04004432 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004433 .read_u64 = mem_cgroup_read_u64,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004434 },
Balbir Singh8697d332008-02-07 00:13:59 -08004435 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004436 .name = "stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004437 .seq_show = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004438 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004439 {
4440 .name = "force_empty",
Tejun Heo6770c642014-05-13 12:16:21 -04004441 .write = mem_cgroup_force_empty_write,
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004442 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004443 {
4444 .name = "use_hierarchy",
4445 .write_u64 = mem_cgroup_hierarchy_write,
4446 .read_u64 = mem_cgroup_hierarchy_read,
4447 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004448 {
Tejun Heo3bc942f2013-11-22 18:20:44 -05004449 .name = "cgroup.event_control", /* XXX: for compat */
Tejun Heo451af502014-05-13 12:16:21 -04004450 .write = memcg_write_event_control,
Tejun Heo7dbdb192015-09-18 17:54:23 -04004451 .flags = CFTYPE_NO_PREFIX | CFTYPE_WORLD_WRITABLE,
Tejun Heo79bd9812013-11-22 18:20:42 -05004452 },
4453 {
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004454 .name = "swappiness",
4455 .read_u64 = mem_cgroup_swappiness_read,
4456 .write_u64 = mem_cgroup_swappiness_write,
4457 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004458 {
4459 .name = "move_charge_at_immigrate",
4460 .read_u64 = mem_cgroup_move_charge_read,
4461 .write_u64 = mem_cgroup_move_charge_write,
4462 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004463 {
4464 .name = "oom_control",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004465 .seq_show = mem_cgroup_oom_control_read,
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004466 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004467 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4468 },
Anton Vorontsov70ddf632013-04-29 15:08:31 -07004469 {
4470 .name = "pressure_level",
Anton Vorontsov70ddf632013-04-29 15:08:31 -07004471 },
Ying Han406eb0c2011-05-26 16:25:37 -07004472#ifdef CONFIG_NUMA
4473 {
4474 .name = "numa_stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05004475 .seq_show = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07004476 },
4477#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08004478 {
4479 .name = "kmem.limit_in_bytes",
4480 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
Tejun Heo451af502014-05-13 12:16:21 -04004481 .write = mem_cgroup_write,
Tejun Heo791badb2013-12-05 12:28:02 -05004482 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004483 },
4484 {
4485 .name = "kmem.usage_in_bytes",
4486 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
Tejun Heo791badb2013-12-05 12:28:02 -05004487 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004488 },
4489 {
4490 .name = "kmem.failcnt",
4491 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
Tejun Heo6770c642014-05-13 12:16:21 -04004492 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004493 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004494 },
4495 {
4496 .name = "kmem.max_usage_in_bytes",
4497 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
Tejun Heo6770c642014-05-13 12:16:21 -04004498 .write = mem_cgroup_reset,
Tejun Heo791badb2013-12-05 12:28:02 -05004499 .read_u64 = mem_cgroup_read_u64,
Glauber Costa510fc4e2012-12-18 14:21:47 -08004500 },
Yang Shi5b365772017-11-15 17:32:03 -08004501#if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)
Glauber Costa749c5412012-12-18 14:23:01 -08004502 {
4503 .name = "kmem.slabinfo",
Tejun Heobc2791f2017-02-22 15:41:21 -08004504 .seq_start = memcg_slab_start,
4505 .seq_next = memcg_slab_next,
4506 .seq_stop = memcg_slab_stop,
Vladimir Davydovb0475012014-12-10 15:44:19 -08004507 .seq_show = memcg_slab_show,
Glauber Costa749c5412012-12-18 14:23:01 -08004508 },
4509#endif
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08004510 {
4511 .name = "kmem.tcp.limit_in_bytes",
4512 .private = MEMFILE_PRIVATE(_TCP, RES_LIMIT),
4513 .write = mem_cgroup_write,
4514 .read_u64 = mem_cgroup_read_u64,
4515 },
4516 {
4517 .name = "kmem.tcp.usage_in_bytes",
4518 .private = MEMFILE_PRIVATE(_TCP, RES_USAGE),
4519 .read_u64 = mem_cgroup_read_u64,
4520 },
4521 {
4522 .name = "kmem.tcp.failcnt",
4523 .private = MEMFILE_PRIVATE(_TCP, RES_FAILCNT),
4524 .write = mem_cgroup_reset,
4525 .read_u64 = mem_cgroup_read_u64,
4526 },
4527 {
4528 .name = "kmem.tcp.max_usage_in_bytes",
4529 .private = MEMFILE_PRIVATE(_TCP, RES_MAX_USAGE),
4530 .write = mem_cgroup_reset,
4531 .read_u64 = mem_cgroup_read_u64,
4532 },
Tejun Heo6bc10342012-04-01 12:09:55 -07004533 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07004534};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004535
Johannes Weiner73f576c2016-07-20 15:44:57 -07004536/*
4537 * Private memory cgroup IDR
4538 *
4539 * Swap-out records and page cache shadow entries need to store memcg
4540 * references in constrained space, so we maintain an ID space that is
4541 * limited to 16 bit (MEM_CGROUP_ID_MAX), limiting the total number of
4542 * memory-controlled cgroups to 64k.
4543 *
4544 * However, there usually are many references to the oflline CSS after
4545 * the cgroup has been destroyed, such as page cache or reclaimable
4546 * slab objects, that don't need to hang on to the ID. We want to keep
4547 * those dead CSS from occupying IDs, or we might quickly exhaust the
4548 * relatively small ID space and prevent the creation of new cgroups
4549 * even when there are much fewer than 64k cgroups - possibly none.
4550 *
4551 * Maintain a private 16-bit ID space for memcg, and allow the ID to
4552 * be freed and recycled when it's no longer needed, which is usually
4553 * when the CSS is offlined.
4554 *
4555 * The only exception to that are records of swapped out tmpfs/shmem
4556 * pages that need to be attributed to live ancestors on swapin. But
4557 * those references are manageable from userspace.
4558 */
4559
4560static DEFINE_IDR(mem_cgroup_idr);
4561
Kirill Tkhai7e97de02018-08-02 15:36:01 -07004562static void mem_cgroup_id_remove(struct mem_cgroup *memcg)
4563{
4564 if (memcg->id.id > 0) {
4565 idr_remove(&mem_cgroup_idr, memcg->id.id);
4566 memcg->id.id = 0;
4567 }
4568}
4569
Vladimir Davydov615d66c2016-08-11 15:33:03 -07004570static void mem_cgroup_id_get_many(struct mem_cgroup *memcg, unsigned int n)
Johannes Weiner73f576c2016-07-20 15:44:57 -07004571{
Kirill Tkhai1c2d4792018-10-26 15:09:28 -07004572 refcount_add(n, &memcg->id.ref);
Johannes Weiner73f576c2016-07-20 15:44:57 -07004573}
4574
Vladimir Davydov615d66c2016-08-11 15:33:03 -07004575static void mem_cgroup_id_put_many(struct mem_cgroup *memcg, unsigned int n)
Johannes Weiner73f576c2016-07-20 15:44:57 -07004576{
Kirill Tkhai1c2d4792018-10-26 15:09:28 -07004577 if (refcount_sub_and_test(n, &memcg->id.ref)) {
Kirill Tkhai7e97de02018-08-02 15:36:01 -07004578 mem_cgroup_id_remove(memcg);
Johannes Weiner73f576c2016-07-20 15:44:57 -07004579
4580 /* Memcg ID pins CSS */
4581 css_put(&memcg->css);
4582 }
4583}
4584
Vladimir Davydov615d66c2016-08-11 15:33:03 -07004585static inline void mem_cgroup_id_get(struct mem_cgroup *memcg)
4586{
4587 mem_cgroup_id_get_many(memcg, 1);
4588}
4589
4590static inline void mem_cgroup_id_put(struct mem_cgroup *memcg)
4591{
4592 mem_cgroup_id_put_many(memcg, 1);
4593}
4594
Johannes Weiner73f576c2016-07-20 15:44:57 -07004595/**
4596 * mem_cgroup_from_id - look up a memcg from a memcg id
4597 * @id: the memcg id to look up
4598 *
4599 * Caller must hold rcu_read_lock().
4600 */
4601struct mem_cgroup *mem_cgroup_from_id(unsigned short id)
4602{
4603 WARN_ON_ONCE(!rcu_read_lock_held());
4604 return idr_find(&mem_cgroup_idr, id);
4605}
4606
Mel Gormanef8f2322016-07-28 15:46:05 -07004607static int alloc_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004608{
4609 struct mem_cgroup_per_node *pn;
Mel Gormanef8f2322016-07-28 15:46:05 -07004610 int tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004611 /*
4612 * This routine is called against possible nodes.
4613 * But it's BUG to call kmalloc() against offline node.
4614 *
4615 * TODO: this routine can waste much memory for nodes which will
4616 * never be onlined. It's better to use memory hotplug callback
4617 * function.
4618 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004619 if (!node_state(node, N_NORMAL_MEMORY))
4620 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08004621 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004622 if (!pn)
4623 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004624
Johannes Weiner815744d2019-06-13 15:55:46 -07004625 pn->lruvec_stat_local = alloc_percpu(struct lruvec_stat);
4626 if (!pn->lruvec_stat_local) {
4627 kfree(pn);
4628 return 1;
4629 }
4630
Johannes Weinera983b5e2018-01-31 16:16:45 -08004631 pn->lruvec_stat_cpu = alloc_percpu(struct lruvec_stat);
4632 if (!pn->lruvec_stat_cpu) {
Johannes Weiner815744d2019-06-13 15:55:46 -07004633 free_percpu(pn->lruvec_stat_local);
Johannes Weiner00f3ca22017-07-06 15:40:52 -07004634 kfree(pn);
4635 return 1;
4636 }
4637
Mel Gormanef8f2322016-07-28 15:46:05 -07004638 lruvec_init(&pn->lruvec);
4639 pn->usage_in_excess = 0;
4640 pn->on_tree = false;
4641 pn->memcg = memcg;
4642
Johannes Weiner54f72fe2013-07-08 15:59:49 -07004643 memcg->nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004644 return 0;
4645}
4646
Mel Gormanef8f2322016-07-28 15:46:05 -07004647static void free_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004648{
Johannes Weiner00f3ca22017-07-06 15:40:52 -07004649 struct mem_cgroup_per_node *pn = memcg->nodeinfo[node];
4650
Michal Hocko4eaf4312018-04-10 16:29:52 -07004651 if (!pn)
4652 return;
4653
Johannes Weinera983b5e2018-01-31 16:16:45 -08004654 free_percpu(pn->lruvec_stat_cpu);
Johannes Weiner815744d2019-06-13 15:55:46 -07004655 free_percpu(pn->lruvec_stat_local);
Johannes Weiner00f3ca22017-07-06 15:40:52 -07004656 kfree(pn);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004657}
4658
Tahsin Erdogan40e952f2017-03-09 16:17:26 -08004659static void __mem_cgroup_free(struct mem_cgroup *memcg)
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004660{
4661 int node;
4662
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004663 for_each_node(node)
Mel Gormanef8f2322016-07-28 15:46:05 -07004664 free_mem_cgroup_per_node_info(memcg, node);
Chris Down871789d2019-05-14 15:46:57 -07004665 free_percpu(memcg->vmstats_percpu);
Johannes Weiner815744d2019-06-13 15:55:46 -07004666 free_percpu(memcg->vmstats_local);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004667 kfree(memcg);
4668}
4669
Tahsin Erdogan40e952f2017-03-09 16:17:26 -08004670static void mem_cgroup_free(struct mem_cgroup *memcg)
4671{
4672 memcg_wb_domain_exit(memcg);
4673 __mem_cgroup_free(memcg);
4674}
4675
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004676static struct mem_cgroup *mem_cgroup_alloc(void)
4677{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004678 struct mem_cgroup *memcg;
Alexey Dobriyanb9726c22019-03-05 15:48:26 -08004679 unsigned int size;
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004680 int node;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004681
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004682 size = sizeof(struct mem_cgroup);
4683 size += nr_node_ids * sizeof(struct mem_cgroup_per_node *);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004684
Vladimir Davydov8ff69e22014-01-23 15:52:52 -08004685 memcg = kzalloc(size, GFP_KERNEL);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004686 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004687 return NULL;
4688
Johannes Weiner73f576c2016-07-20 15:44:57 -07004689 memcg->id.id = idr_alloc(&mem_cgroup_idr, NULL,
4690 1, MEM_CGROUP_ID_MAX,
4691 GFP_KERNEL);
4692 if (memcg->id.id < 0)
4693 goto fail;
4694
Johannes Weiner815744d2019-06-13 15:55:46 -07004695 memcg->vmstats_local = alloc_percpu(struct memcg_vmstats_percpu);
4696 if (!memcg->vmstats_local)
4697 goto fail;
4698
Chris Down871789d2019-05-14 15:46:57 -07004699 memcg->vmstats_percpu = alloc_percpu(struct memcg_vmstats_percpu);
4700 if (!memcg->vmstats_percpu)
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004701 goto fail;
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004702
Bob Liu3ed28fa2012-01-12 17:19:04 -08004703 for_each_node(node)
Mel Gormanef8f2322016-07-28 15:46:05 -07004704 if (alloc_mem_cgroup_per_node_info(memcg, node))
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004705 goto fail;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004706
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004707 if (memcg_wb_domain_init(memcg, GFP_KERNEL))
4708 goto fail;
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004709
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08004710 INIT_WORK(&memcg->high_work, high_work_func);
Glauber Costad142e3e2013-02-22 16:34:52 -08004711 memcg->last_scanned_node = MAX_NUMNODES;
4712 INIT_LIST_HEAD(&memcg->oom_notify);
Glauber Costad142e3e2013-02-22 16:34:52 -08004713 mutex_init(&memcg->thresholds_lock);
4714 spin_lock_init(&memcg->move_lock);
Anton Vorontsov70ddf632013-04-29 15:08:31 -07004715 vmpressure_init(&memcg->vmpressure);
Tejun Heofba94802013-11-22 18:20:43 -05004716 INIT_LIST_HEAD(&memcg->event_list);
4717 spin_lock_init(&memcg->event_list_lock);
Johannes Weinerd886f4e2016-01-20 15:02:47 -08004718 memcg->socket_pressure = jiffies;
Kirill Tkhai84c07d12018-08-17 15:47:25 -07004719#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydov900a38f2014-12-12 16:55:10 -08004720 memcg->kmemcg_id = -1;
Vladimir Davydov900a38f2014-12-12 16:55:10 -08004721#endif
Tejun Heo52ebea72015-05-22 17:13:37 -04004722#ifdef CONFIG_CGROUP_WRITEBACK
4723 INIT_LIST_HEAD(&memcg->cgwb_list);
4724#endif
Johannes Weiner73f576c2016-07-20 15:44:57 -07004725 idr_replace(&mem_cgroup_idr, memcg, memcg->id.id);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004726 return memcg;
4727fail:
Kirill Tkhai7e97de02018-08-02 15:36:01 -07004728 mem_cgroup_id_remove(memcg);
Tahsin Erdogan40e952f2017-03-09 16:17:26 -08004729 __mem_cgroup_free(memcg);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004730 return NULL;
Glauber Costad142e3e2013-02-22 16:34:52 -08004731}
4732
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004733static struct cgroup_subsys_state * __ref
4734mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Glauber Costad142e3e2013-02-22 16:34:52 -08004735{
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004736 struct mem_cgroup *parent = mem_cgroup_from_css(parent_css);
4737 struct mem_cgroup *memcg;
4738 long error = -ENOMEM;
Glauber Costad142e3e2013-02-22 16:34:52 -08004739
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004740 memcg = mem_cgroup_alloc();
4741 if (!memcg)
4742 return ERR_PTR(error);
Li Zefan4219b2d2013-09-23 16:56:29 +08004743
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004744 memcg->high = PAGE_COUNTER_MAX;
4745 memcg->soft_limit = PAGE_COUNTER_MAX;
4746 if (parent) {
4747 memcg->swappiness = mem_cgroup_swappiness(parent);
4748 memcg->oom_kill_disable = parent->oom_kill_disable;
4749 }
4750 if (parent && parent->use_hierarchy) {
4751 memcg->use_hierarchy = true;
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004752 page_counter_init(&memcg->memory, &parent->memory);
Vladimir Davydov37e84352016-01-20 15:02:56 -08004753 page_counter_init(&memcg->swap, &parent->swap);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004754 page_counter_init(&memcg->memsw, &parent->memsw);
4755 page_counter_init(&memcg->kmem, &parent->kmem);
Johannes Weiner0db15292016-01-20 15:02:50 -08004756 page_counter_init(&memcg->tcpmem, &parent->tcpmem);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004757 } else {
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004758 page_counter_init(&memcg->memory, NULL);
Vladimir Davydov37e84352016-01-20 15:02:56 -08004759 page_counter_init(&memcg->swap, NULL);
Johannes Weiner3e32cb22014-12-10 15:42:31 -08004760 page_counter_init(&memcg->memsw, NULL);
4761 page_counter_init(&memcg->kmem, NULL);
Johannes Weiner0db15292016-01-20 15:02:50 -08004762 page_counter_init(&memcg->tcpmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004763 /*
4764 * Deeper hierachy with use_hierarchy == false doesn't make
4765 * much sense so let cgroup subsystem know about this
4766 * unfortunate state in our controller.
4767 */
Glauber Costad142e3e2013-02-22 16:34:52 -08004768 if (parent != root_mem_cgroup)
Tejun Heo073219e2014-02-08 10:36:58 -05004769 memory_cgrp_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004770 }
Vladimir Davydovd6441632014-01-23 15:53:09 -08004771
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004772 /* The following stuff does not apply to the root */
4773 if (!parent) {
4774 root_mem_cgroup = memcg;
4775 return &memcg->css;
4776 }
4777
Vladimir Davydovb313aee2016-03-17 14:18:27 -07004778 error = memcg_online_kmem(memcg);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004779 if (error)
4780 goto fail;
Johannes Weiner127424c2016-01-20 15:02:32 -08004781
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08004782 if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket)
Johannes Weineref129472016-01-14 15:21:34 -08004783 static_branch_inc(&memcg_sockets_enabled_key);
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08004784
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004785 return &memcg->css;
4786fail:
Kirill Tkhai7e97de02018-08-02 15:36:01 -07004787 mem_cgroup_id_remove(memcg);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004788 mem_cgroup_free(memcg);
Tejun Heoea3a9642016-06-24 14:49:58 -07004789 return ERR_PTR(-ENOMEM);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004790}
4791
Johannes Weiner73f576c2016-07-20 15:44:57 -07004792static int mem_cgroup_css_online(struct cgroup_subsys_state *css)
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004793{
Vladimir Davydov58fa2a52016-10-07 16:57:29 -07004794 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
4795
Kirill Tkhai0a4465d2018-08-17 15:47:37 -07004796 /*
4797 * A memcg must be visible for memcg_expand_shrinker_maps()
4798 * by the time the maps are allocated. So, we allocate maps
4799 * here, when for_each_mem_cgroup() can't skip it.
4800 */
4801 if (memcg_alloc_shrinker_maps(memcg)) {
4802 mem_cgroup_id_remove(memcg);
4803 return -ENOMEM;
4804 }
4805
Johannes Weiner73f576c2016-07-20 15:44:57 -07004806 /* Online state pins memcg ID, memcg ID pins CSS */
Kirill Tkhai1c2d4792018-10-26 15:09:28 -07004807 refcount_set(&memcg->id.ref, 1);
Johannes Weiner73f576c2016-07-20 15:44:57 -07004808 css_get(css);
Johannes Weiner2f7dd7a2014-10-02 16:16:57 -07004809 return 0;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004810}
4811
Tejun Heoeb954192013-08-08 20:11:23 -04004812static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004813{
Tejun Heoeb954192013-08-08 20:11:23 -04004814 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Tejun Heo3bc942f2013-11-22 18:20:44 -05004815 struct mem_cgroup_event *event, *tmp;
Tejun Heo79bd9812013-11-22 18:20:42 -05004816
4817 /*
4818 * Unregister events and notify userspace.
4819 * Notify userspace about cgroup removing only after rmdir of cgroup
4820 * directory to avoid race between userspace and kernelspace.
4821 */
Tejun Heofba94802013-11-22 18:20:43 -05004822 spin_lock(&memcg->event_list_lock);
4823 list_for_each_entry_safe(event, tmp, &memcg->event_list, list) {
Tejun Heo79bd9812013-11-22 18:20:42 -05004824 list_del_init(&event->list);
4825 schedule_work(&event->remove);
4826 }
Tejun Heofba94802013-11-22 18:20:43 -05004827 spin_unlock(&memcg->event_list_lock);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004828
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07004829 page_counter_set_min(&memcg->memory, 0);
Roman Gushchin23067152018-06-07 17:06:22 -07004830 page_counter_set_low(&memcg->memory, 0);
Roman Gushchin63677c742017-09-06 16:21:47 -07004831
Johannes Weiner567e9ab2016-01-20 15:02:24 -08004832 memcg_offline_kmem(memcg);
Tejun Heo52ebea72015-05-22 17:13:37 -04004833 wb_memcg_offline(memcg);
Johannes Weiner73f576c2016-07-20 15:44:57 -07004834
Roman Gushchin591edfb2018-10-26 15:03:23 -07004835 drain_all_stock(memcg);
4836
Johannes Weiner73f576c2016-07-20 15:44:57 -07004837 mem_cgroup_id_put(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004838}
4839
Vladimir Davydov6df38682015-12-29 14:54:10 -08004840static void mem_cgroup_css_released(struct cgroup_subsys_state *css)
4841{
4842 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
4843
4844 invalidate_reclaim_iterators(memcg);
4845}
4846
Tejun Heoeb954192013-08-08 20:11:23 -04004847static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004848{
Tejun Heoeb954192013-08-08 20:11:23 -04004849 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004850
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08004851 if (cgroup_subsys_on_dfl(memory_cgrp_subsys) && !cgroup_memory_nosocket)
Johannes Weineref129472016-01-14 15:21:34 -08004852 static_branch_dec(&memcg_sockets_enabled_key);
Johannes Weiner3893e302016-01-20 15:02:29 -08004853
Johannes Weiner0db15292016-01-20 15:02:50 -08004854 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_active)
Vladimir Davydovd55f90b2016-01-20 15:02:44 -08004855 static_branch_dec(&memcg_sockets_enabled_key);
Johannes Weiner3893e302016-01-20 15:02:29 -08004856
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004857 vmpressure_cleanup(&memcg->vmpressure);
4858 cancel_work_sync(&memcg->high_work);
4859 mem_cgroup_remove_from_trees(memcg);
Kirill Tkhai0a4465d2018-08-17 15:47:37 -07004860 memcg_free_shrinker_maps(memcg);
Johannes Weinerd886f4e2016-01-20 15:02:47 -08004861 memcg_free_kmem(memcg);
Johannes Weiner0b8f73e2016-01-20 15:02:53 -08004862 mem_cgroup_free(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004863}
4864
Tejun Heo1ced9532014-07-08 18:02:57 -04004865/**
4866 * mem_cgroup_css_reset - reset the states of a mem_cgroup
4867 * @css: the target css
4868 *
4869 * Reset the states of the mem_cgroup associated with @css. This is
4870 * invoked when the userland requests disabling on the default hierarchy
4871 * but the memcg is pinned through dependency. The memcg should stop
4872 * applying policies and should revert to the vanilla state as it may be
4873 * made visible again.
4874 *
4875 * The current implementation only resets the essential configurations.
4876 * This needs to be expanded to cover all the visible parts.
4877 */
4878static void mem_cgroup_css_reset(struct cgroup_subsys_state *css)
4879{
4880 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
4881
Roman Gushchinbbec2e12018-06-07 17:06:18 -07004882 page_counter_set_max(&memcg->memory, PAGE_COUNTER_MAX);
4883 page_counter_set_max(&memcg->swap, PAGE_COUNTER_MAX);
4884 page_counter_set_max(&memcg->memsw, PAGE_COUNTER_MAX);
4885 page_counter_set_max(&memcg->kmem, PAGE_COUNTER_MAX);
4886 page_counter_set_max(&memcg->tcpmem, PAGE_COUNTER_MAX);
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07004887 page_counter_set_min(&memcg->memory, 0);
Roman Gushchin23067152018-06-07 17:06:22 -07004888 page_counter_set_low(&memcg->memory, 0);
Johannes Weiner241994ed2015-02-11 15:26:06 -08004889 memcg->high = PAGE_COUNTER_MAX;
Johannes Weiner24d404d2015-01-08 14:32:35 -08004890 memcg->soft_limit = PAGE_COUNTER_MAX;
Tejun Heo2529bb32015-05-22 18:23:34 -04004891 memcg_wb_domain_size_changed(memcg);
Tejun Heo1ced9532014-07-08 18:02:57 -04004892}
4893
Daisuke Nishimura02491442010-03-10 15:22:17 -08004894#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004895/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004896static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004897{
Johannes Weiner05b84302014-08-06 16:05:59 -07004898 int ret;
Johannes Weiner9476db92014-08-06 16:05:55 -07004899
Mel Gormand0164ad2015-11-06 16:28:21 -08004900 /* Try a single bulk charge without reclaim first, kswapd may wake */
4901 ret = try_charge(mc.to, GFP_KERNEL & ~__GFP_DIRECT_RECLAIM, count);
Johannes Weiner9476db92014-08-06 16:05:55 -07004902 if (!ret) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004903 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004904 return ret;
4905 }
Johannes Weiner9476db92014-08-06 16:05:55 -07004906
David Rientjes36745342017-01-24 15:18:10 -08004907 /* Try charges one by one with reclaim, but do not retry */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004908 while (count--) {
David Rientjes36745342017-01-24 15:18:10 -08004909 ret = try_charge(mc.to, GFP_KERNEL | __GFP_NORETRY, 1);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08004910 if (ret)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08004911 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004912 mc.precharge++;
Johannes Weiner9476db92014-08-06 16:05:55 -07004913 cond_resched();
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004914 }
Johannes Weiner9476db92014-08-06 16:05:55 -07004915 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004916}
4917
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004918union mc_target {
4919 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004920 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004921};
4922
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004923enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07004924 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004925 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08004926 MC_TARGET_SWAP,
Jérôme Glissec733a822017-09-08 16:11:54 -07004927 MC_TARGET_DEVICE,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004928};
4929
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004930static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
4931 unsigned long addr, pte_t ptent)
4932{
Jérôme Glissec733a822017-09-08 16:11:54 -07004933 struct page *page = _vm_normal_page(vma, addr, ptent, true);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004934
4935 if (!page || !page_mapped(page))
4936 return NULL;
4937 if (PageAnon(page)) {
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08004938 if (!(mc.flags & MOVE_ANON))
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004939 return NULL;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08004940 } else {
4941 if (!(mc.flags & MOVE_FILE))
4942 return NULL;
4943 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004944 if (!get_page_unless_zero(page))
4945 return NULL;
4946
4947 return page;
4948}
4949
Jérôme Glissec733a822017-09-08 16:11:54 -07004950#if defined(CONFIG_SWAP) || defined(CONFIG_DEVICE_PRIVATE)
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004951static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
Li RongQing48406ef2016-07-26 15:22:14 -07004952 pte_t ptent, swp_entry_t *entry)
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004953{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004954 struct page *page = NULL;
4955 swp_entry_t ent = pte_to_swp_entry(ptent);
4956
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08004957 if (!(mc.flags & MOVE_ANON) || non_swap_entry(ent))
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004958 return NULL;
Jérôme Glissec733a822017-09-08 16:11:54 -07004959
4960 /*
4961 * Handle MEMORY_DEVICE_PRIVATE which are ZONE_DEVICE page belonging to
4962 * a device and because they are not accessible by CPU they are store
4963 * as special swap entry in the CPU page table.
4964 */
4965 if (is_device_private_entry(ent)) {
4966 page = device_private_entry_to_page(ent);
4967 /*
4968 * MEMORY_DEVICE_PRIVATE means ZONE_DEVICE page and which have
4969 * a refcount of 1 when free (unlike normal page)
4970 */
4971 if (!page_ref_add_unless(page, 1, 1))
4972 return NULL;
4973 return page;
4974 }
4975
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07004976 /*
4977 * Because lookup_swap_cache() updates some statistics counter,
4978 * we call find_get_page() with swapper_space directly.
4979 */
Huang Yingf6ab1f72016-10-07 17:00:21 -07004980 page = find_get_page(swap_address_space(ent), swp_offset(ent));
Johannes Weiner7941d212016-01-14 15:21:23 -08004981 if (do_memsw_account())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004982 entry->val = ent.val;
4983
4984 return page;
4985}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07004986#else
4987static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
Li RongQing48406ef2016-07-26 15:22:14 -07004988 pte_t ptent, swp_entry_t *entry)
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07004989{
4990 return NULL;
4991}
4992#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004993
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004994static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
4995 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4996{
4997 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004998 struct address_space *mapping;
4999 pgoff_t pgoff;
5000
5001 if (!vma->vm_file) /* anonymous vma */
5002 return NULL;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005003 if (!(mc.flags & MOVE_FILE))
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005004 return NULL;
5005
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005006 mapping = vma->vm_file->f_mapping;
Kirill A. Shutemov0661a332015-02-10 14:10:04 -08005007 pgoff = linear_page_index(vma, addr);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005008
5009 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005010#ifdef CONFIG_SWAP
5011 /* shmem/tmpfs may report page out on swap: account for that too. */
Johannes Weiner139b6a62014-05-06 12:50:05 -07005012 if (shmem_mapping(mapping)) {
5013 page = find_get_entry(mapping, pgoff);
Matthew Wilcox3159f942017-11-03 13:30:42 -04005014 if (xa_is_value(page)) {
Johannes Weiner139b6a62014-05-06 12:50:05 -07005015 swp_entry_t swp = radix_to_swp_entry(page);
Johannes Weiner7941d212016-01-14 15:21:23 -08005016 if (do_memsw_account())
Johannes Weiner139b6a62014-05-06 12:50:05 -07005017 *entry = swp;
Huang Yingf6ab1f72016-10-07 17:00:21 -07005018 page = find_get_page(swap_address_space(swp),
5019 swp_offset(swp));
Johannes Weiner139b6a62014-05-06 12:50:05 -07005020 }
5021 } else
5022 page = find_get_page(mapping, pgoff);
5023#else
5024 page = find_get_page(mapping, pgoff);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005025#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005026 return page;
5027}
5028
Chen Gangb1b0dea2015-04-14 15:47:35 -07005029/**
5030 * mem_cgroup_move_account - move account of the page
5031 * @page: the page
Li RongQing25843c22016-07-26 15:26:56 -07005032 * @compound: charge the page as compound or small page
Chen Gangb1b0dea2015-04-14 15:47:35 -07005033 * @from: mem_cgroup which the page is moved from.
5034 * @to: mem_cgroup which the page is moved to. @from != @to.
5035 *
Kirill A. Shutemov3ac808f2016-01-15 16:53:07 -08005036 * The caller must make sure the page is not on LRU (isolate_page() is useful.)
Chen Gangb1b0dea2015-04-14 15:47:35 -07005037 *
5038 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
5039 * from old cgroup.
5040 */
5041static int mem_cgroup_move_account(struct page *page,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08005042 bool compound,
Chen Gangb1b0dea2015-04-14 15:47:35 -07005043 struct mem_cgroup *from,
5044 struct mem_cgroup *to)
5045{
5046 unsigned long flags;
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08005047 unsigned int nr_pages = compound ? hpage_nr_pages(page) : 1;
Chen Gangb1b0dea2015-04-14 15:47:35 -07005048 int ret;
Greg Thelenc4843a72015-05-22 17:13:16 -04005049 bool anon;
Chen Gangb1b0dea2015-04-14 15:47:35 -07005050
5051 VM_BUG_ON(from == to);
5052 VM_BUG_ON_PAGE(PageLRU(page), page);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08005053 VM_BUG_ON(compound && !PageTransHuge(page));
Chen Gangb1b0dea2015-04-14 15:47:35 -07005054
5055 /*
Johannes Weiner6a93ca82016-03-15 14:57:19 -07005056 * Prevent mem_cgroup_migrate() from looking at
Hugh Dickins45637ba2015-11-05 18:49:40 -08005057 * page->mem_cgroup of its source page while we change it.
Chen Gangb1b0dea2015-04-14 15:47:35 -07005058 */
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08005059 ret = -EBUSY;
Chen Gangb1b0dea2015-04-14 15:47:35 -07005060 if (!trylock_page(page))
5061 goto out;
5062
5063 ret = -EINVAL;
5064 if (page->mem_cgroup != from)
5065 goto out_unlock;
5066
Greg Thelenc4843a72015-05-22 17:13:16 -04005067 anon = PageAnon(page);
5068
Chen Gangb1b0dea2015-04-14 15:47:35 -07005069 spin_lock_irqsave(&from->move_lock, flags);
5070
Greg Thelenc4843a72015-05-22 17:13:16 -04005071 if (!anon && page_mapped(page)) {
Johannes Weinerc9019e92018-01-31 16:16:37 -08005072 __mod_memcg_state(from, NR_FILE_MAPPED, -nr_pages);
5073 __mod_memcg_state(to, NR_FILE_MAPPED, nr_pages);
Chen Gangb1b0dea2015-04-14 15:47:35 -07005074 }
5075
Greg Thelenc4843a72015-05-22 17:13:16 -04005076 /*
5077 * move_lock grabbed above and caller set from->moving_account, so
Johannes Weinerccda7f42017-05-03 14:55:16 -07005078 * mod_memcg_page_state will serialize updates to PageDirty.
Greg Thelenc4843a72015-05-22 17:13:16 -04005079 * So mapping should be stable for dirty pages.
5080 */
5081 if (!anon && PageDirty(page)) {
5082 struct address_space *mapping = page_mapping(page);
5083
5084 if (mapping_cap_account_dirty(mapping)) {
Johannes Weinerc9019e92018-01-31 16:16:37 -08005085 __mod_memcg_state(from, NR_FILE_DIRTY, -nr_pages);
5086 __mod_memcg_state(to, NR_FILE_DIRTY, nr_pages);
Greg Thelenc4843a72015-05-22 17:13:16 -04005087 }
5088 }
5089
Chen Gangb1b0dea2015-04-14 15:47:35 -07005090 if (PageWriteback(page)) {
Johannes Weinerc9019e92018-01-31 16:16:37 -08005091 __mod_memcg_state(from, NR_WRITEBACK, -nr_pages);
5092 __mod_memcg_state(to, NR_WRITEBACK, nr_pages);
Chen Gangb1b0dea2015-04-14 15:47:35 -07005093 }
5094
5095 /*
5096 * It is safe to change page->mem_cgroup here because the page
5097 * is referenced, charged, and isolated - we can't race with
5098 * uncharging, charging, migration, or LRU putback.
5099 */
5100
5101 /* caller should have done css_get */
5102 page->mem_cgroup = to;
5103 spin_unlock_irqrestore(&from->move_lock, flags);
5104
5105 ret = 0;
5106
5107 local_irq_disable();
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08005108 mem_cgroup_charge_statistics(to, page, compound, nr_pages);
Chen Gangb1b0dea2015-04-14 15:47:35 -07005109 memcg_check_events(to, page);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08005110 mem_cgroup_charge_statistics(from, page, compound, -nr_pages);
Chen Gangb1b0dea2015-04-14 15:47:35 -07005111 memcg_check_events(from, page);
5112 local_irq_enable();
5113out_unlock:
5114 unlock_page(page);
5115out:
5116 return ret;
5117}
5118
Li RongQing7cf78062016-05-27 14:27:46 -07005119/**
5120 * get_mctgt_type - get target type of moving charge
5121 * @vma: the vma the pte to be checked belongs
5122 * @addr: the address corresponding to the pte to be checked
5123 * @ptent: the pte to be checked
5124 * @target: the pointer the target page or swap ent will be stored(can be NULL)
5125 *
5126 * Returns
5127 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5128 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5129 * move charge. if @target is not NULL, the page is stored in target->page
5130 * with extra refcnt got(Callers should handle it).
5131 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5132 * target for charge migration. if @target is not NULL, the entry is stored
5133 * in target->ent.
Jérôme Glissedf6ad692017-09-08 16:12:24 -07005134 * 3(MC_TARGET_DEVICE): like MC_TARGET_PAGE but page is MEMORY_DEVICE_PUBLIC
5135 * or MEMORY_DEVICE_PRIVATE (so ZONE_DEVICE page and thus not on the lru).
5136 * For now we such page is charge like a regular page would be as for all
5137 * intent and purposes it is just special memory taking the place of a
5138 * regular page.
Jérôme Glissec733a822017-09-08 16:11:54 -07005139 *
5140 * See Documentations/vm/hmm.txt and include/linux/hmm.h
Li RongQing7cf78062016-05-27 14:27:46 -07005141 *
5142 * Called with pte lock held.
5143 */
5144
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005145static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005146 unsigned long addr, pte_t ptent, union mc_target *target)
5147{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005148 struct page *page = NULL;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005149 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005150 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005151
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005152 if (pte_present(ptent))
5153 page = mc_handle_present_pte(vma, addr, ptent);
5154 else if (is_swap_pte(ptent))
Li RongQing48406ef2016-07-26 15:22:14 -07005155 page = mc_handle_swap_pte(vma, ptent, &ent);
Kirill A. Shutemov0661a332015-02-10 14:10:04 -08005156 else if (pte_none(ptent))
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005157 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005158
5159 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005160 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005161 if (page) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005162 /*
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005163 * Do only loose check w/o serialization.
Johannes Weiner1306a852014-12-10 15:44:52 -08005164 * mem_cgroup_move_account() checks the page is valid or
Johannes Weiner0a31bc92014-08-08 14:19:22 -07005165 * not under LRU exclusion.
Daisuke Nishimura02491442010-03-10 15:22:17 -08005166 */
Johannes Weiner1306a852014-12-10 15:44:52 -08005167 if (page->mem_cgroup == mc.from) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005168 ret = MC_TARGET_PAGE;
Jérôme Glissedf6ad692017-09-08 16:12:24 -07005169 if (is_device_private_page(page) ||
5170 is_device_public_page(page))
Jérôme Glissec733a822017-09-08 16:11:54 -07005171 ret = MC_TARGET_DEVICE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005172 if (target)
5173 target->page = page;
5174 }
5175 if (!ret || !target)
5176 put_page(page);
5177 }
Huang Ying3e14a572017-09-06 16:22:37 -07005178 /*
5179 * There is a swap entry and a page doesn't exist or isn't charged.
5180 * But we cannot move a tail-page in a THP.
5181 */
5182 if (ent.val && !ret && (!page || !PageTransCompound(page)) &&
Li Zefan34c00c32013-09-23 16:56:01 +08005183 mem_cgroup_id(mc.from) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005184 ret = MC_TARGET_SWAP;
5185 if (target)
5186 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005187 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005188 return ret;
5189}
5190
Naoya Horiguchi12724852012-03-21 16:34:28 -07005191#ifdef CONFIG_TRANSPARENT_HUGEPAGE
5192/*
Huang Yingd6810d72017-09-06 16:22:45 -07005193 * We don't consider PMD mapped swapping or file mapped pages because THP does
5194 * not support them for now.
Naoya Horiguchi12724852012-03-21 16:34:28 -07005195 * Caller should make sure that pmd_trans_huge(pmd) is true.
5196 */
5197static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5198 unsigned long addr, pmd_t pmd, union mc_target *target)
5199{
5200 struct page *page = NULL;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005201 enum mc_target_type ret = MC_TARGET_NONE;
5202
Zi Yan84c3fc42017-09-08 16:11:01 -07005203 if (unlikely(is_swap_pmd(pmd))) {
5204 VM_BUG_ON(thp_migration_supported() &&
5205 !is_pmd_migration_entry(pmd));
5206 return ret;
5207 }
Naoya Horiguchi12724852012-03-21 16:34:28 -07005208 page = pmd_page(pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08005209 VM_BUG_ON_PAGE(!page || !PageHead(page), page);
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005210 if (!(mc.flags & MOVE_ANON))
Naoya Horiguchi12724852012-03-21 16:34:28 -07005211 return ret;
Johannes Weiner1306a852014-12-10 15:44:52 -08005212 if (page->mem_cgroup == mc.from) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005213 ret = MC_TARGET_PAGE;
5214 if (target) {
5215 get_page(page);
5216 target->page = page;
5217 }
5218 }
5219 return ret;
5220}
5221#else
5222static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5223 unsigned long addr, pmd_t pmd, union mc_target *target)
5224{
5225 return MC_TARGET_NONE;
5226}
5227#endif
5228
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005229static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5230 unsigned long addr, unsigned long end,
5231 struct mm_walk *walk)
5232{
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005233 struct vm_area_struct *vma = walk->vma;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005234 pte_t *pte;
5235 spinlock_t *ptl;
5236
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08005237 ptl = pmd_trans_huge_lock(pmd, vma);
5238 if (ptl) {
Jérôme Glissec733a822017-09-08 16:11:54 -07005239 /*
5240 * Note their can not be MC_TARGET_DEVICE for now as we do not
5241 * support transparent huge page with MEMORY_DEVICE_PUBLIC or
5242 * MEMORY_DEVICE_PRIVATE but this might change.
5243 */
Naoya Horiguchi12724852012-03-21 16:34:28 -07005244 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
5245 mc.precharge += HPAGE_PMD_NR;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005246 spin_unlock(ptl);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005247 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005248 }
Dave Hansen03319322011-03-22 16:32:56 -07005249
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005250 if (pmd_trans_unstable(pmd))
5251 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005252 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5253 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005254 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005255 mc.precharge++; /* increment precharge temporarily */
5256 pte_unmap_unlock(pte - 1, ptl);
5257 cond_resched();
5258
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005259 return 0;
5260}
5261
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005262static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5263{
5264 unsigned long precharge;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005265
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005266 struct mm_walk mem_cgroup_count_precharge_walk = {
5267 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5268 .mm = mm,
5269 };
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005270 down_read(&mm->mmap_sem);
James Morse0247f3f2016-10-07 17:00:12 -07005271 walk_page_range(0, mm->highest_vm_end,
5272 &mem_cgroup_count_precharge_walk);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005273 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005274
5275 precharge = mc.precharge;
5276 mc.precharge = 0;
5277
5278 return precharge;
5279}
5280
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005281static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5282{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005283 unsigned long precharge = mem_cgroup_count_precharge(mm);
5284
5285 VM_BUG_ON(mc.moving_task);
5286 mc.moving_task = current;
5287 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005288}
5289
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005290/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5291static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005292{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005293 struct mem_cgroup *from = mc.from;
5294 struct mem_cgroup *to = mc.to;
5295
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005296 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005297 if (mc.precharge) {
Johannes Weiner00501b52014-08-08 14:19:20 -07005298 cancel_charge(mc.to, mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005299 mc.precharge = 0;
5300 }
5301 /*
5302 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5303 * we must uncharge here.
5304 */
5305 if (mc.moved_charge) {
Johannes Weiner00501b52014-08-08 14:19:20 -07005306 cancel_charge(mc.from, mc.moved_charge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005307 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005308 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005309 /* we must fixup refcnts and charges */
5310 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005311 /* uncharge swap account from the old cgroup */
Johannes Weinerce00a962014-09-05 08:43:57 -04005312 if (!mem_cgroup_is_root(mc.from))
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005313 page_counter_uncharge(&mc.from->memsw, mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005314
Vladimir Davydov615d66c2016-08-11 15:33:03 -07005315 mem_cgroup_id_put_many(mc.from, mc.moved_swap);
5316
Johannes Weiner05b84302014-08-06 16:05:59 -07005317 /*
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005318 * we charged both to->memory and to->memsw, so we
5319 * should uncharge to->memory.
Johannes Weiner05b84302014-08-06 16:05:59 -07005320 */
Johannes Weinerce00a962014-09-05 08:43:57 -04005321 if (!mem_cgroup_is_root(mc.to))
Johannes Weiner3e32cb22014-12-10 15:42:31 -08005322 page_counter_uncharge(&mc.to->memory, mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005323
Vladimir Davydov615d66c2016-08-11 15:33:03 -07005324 mem_cgroup_id_get_many(mc.to, mc.moved_swap);
5325 css_put_many(&mc.to->css, mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005326
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005327 mc.moved_swap = 0;
5328 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005329 memcg_oom_recover(from);
5330 memcg_oom_recover(to);
5331 wake_up_all(&mc.waitq);
5332}
5333
5334static void mem_cgroup_clear_mc(void)
5335{
Tejun Heo264a0ae2016-04-21 19:09:02 -04005336 struct mm_struct *mm = mc.mm;
5337
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005338 /*
5339 * we must clear moving_task before waking up waiters at the end of
5340 * task migration.
5341 */
5342 mc.moving_task = NULL;
5343 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005344 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005345 mc.from = NULL;
5346 mc.to = NULL;
Tejun Heo264a0ae2016-04-21 19:09:02 -04005347 mc.mm = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005348 spin_unlock(&mc.lock);
Tejun Heo264a0ae2016-04-21 19:09:02 -04005349
5350 mmput(mm);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005351}
5352
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005353static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005354{
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005355 struct cgroup_subsys_state *css;
Ross Zwislereed67d72015-12-23 14:53:27 -07005356 struct mem_cgroup *memcg = NULL; /* unneeded init to make gcc happy */
Tejun Heo9f2115f2015-09-08 15:01:10 -07005357 struct mem_cgroup *from;
Tejun Heo4530edd2015-09-11 15:00:19 -04005358 struct task_struct *leader, *p;
Tejun Heo9f2115f2015-09-08 15:01:10 -07005359 struct mm_struct *mm;
Johannes Weiner1dfab5a2015-02-11 15:26:09 -08005360 unsigned long move_flags;
Tejun Heo9f2115f2015-09-08 15:01:10 -07005361 int ret = 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005362
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005363 /* charge immigration isn't supported on the default hierarchy */
5364 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
Tejun Heo9f2115f2015-09-08 15:01:10 -07005365 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005366
Tejun Heo4530edd2015-09-11 15:00:19 -04005367 /*
5368 * Multi-process migrations only happen on the default hierarchy
5369 * where charge immigration is not used. Perform charge
5370 * immigration if @tset contains a leader and whine if there are
5371 * multiple.
5372 */
5373 p = NULL;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005374 cgroup_taskset_for_each_leader(leader, css, tset) {
Tejun Heo4530edd2015-09-11 15:00:19 -04005375 WARN_ON_ONCE(p);
5376 p = leader;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005377 memcg = mem_cgroup_from_css(css);
Tejun Heo4530edd2015-09-11 15:00:19 -04005378 }
5379 if (!p)
5380 return 0;
5381
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005382 /*
5383 * We are now commited to this value whatever it is. Changes in this
5384 * tunable will only affect upcoming migrations, not the current one.
5385 * So we need to save it, and keep it going.
5386 */
5387 move_flags = READ_ONCE(memcg->move_charge_at_immigrate);
5388 if (!move_flags)
5389 return 0;
5390
Tejun Heo9f2115f2015-09-08 15:01:10 -07005391 from = mem_cgroup_from_task(p);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005392
Tejun Heo9f2115f2015-09-08 15:01:10 -07005393 VM_BUG_ON(from == memcg);
Johannes Weiner247b1442014-12-10 15:44:11 -08005394
Tejun Heo9f2115f2015-09-08 15:01:10 -07005395 mm = get_task_mm(p);
5396 if (!mm)
5397 return 0;
5398 /* We move charges only when we move a owner of the mm */
5399 if (mm->owner == p) {
5400 VM_BUG_ON(mc.from);
5401 VM_BUG_ON(mc.to);
5402 VM_BUG_ON(mc.precharge);
5403 VM_BUG_ON(mc.moved_charge);
5404 VM_BUG_ON(mc.moved_swap);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005405
Tejun Heo9f2115f2015-09-08 15:01:10 -07005406 spin_lock(&mc.lock);
Tejun Heo264a0ae2016-04-21 19:09:02 -04005407 mc.mm = mm;
Tejun Heo9f2115f2015-09-08 15:01:10 -07005408 mc.from = from;
5409 mc.to = memcg;
5410 mc.flags = move_flags;
5411 spin_unlock(&mc.lock);
5412 /* We set mc.moving_task later */
5413
5414 ret = mem_cgroup_precharge_mc(mm);
5415 if (ret)
5416 mem_cgroup_clear_mc();
Tejun Heo264a0ae2016-04-21 19:09:02 -04005417 } else {
5418 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005419 }
5420 return ret;
5421}
5422
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005423static void mem_cgroup_cancel_attach(struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005424{
Johannes Weiner4e2f2452014-12-10 15:44:08 -08005425 if (mc.to)
5426 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005427}
5428
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005429static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5430 unsigned long addr, unsigned long end,
5431 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005432{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005433 int ret = 0;
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005434 struct vm_area_struct *vma = walk->vma;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005435 pte_t *pte;
5436 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005437 enum mc_target_type target_type;
5438 union mc_target target;
5439 struct page *page;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005440
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08005441 ptl = pmd_trans_huge_lock(pmd, vma);
5442 if (ptl) {
Hugh Dickins62ade862012-05-18 11:28:34 -07005443 if (mc.precharge < HPAGE_PMD_NR) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005444 spin_unlock(ptl);
Naoya Horiguchi12724852012-03-21 16:34:28 -07005445 return 0;
5446 }
5447 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
5448 if (target_type == MC_TARGET_PAGE) {
5449 page = target.page;
5450 if (!isolate_lru_page(page)) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08005451 if (!mem_cgroup_move_account(page, true,
Johannes Weiner1306a852014-12-10 15:44:52 -08005452 mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005453 mc.precharge -= HPAGE_PMD_NR;
5454 mc.moved_charge += HPAGE_PMD_NR;
5455 }
5456 putback_lru_page(page);
5457 }
5458 put_page(page);
Jérôme Glissec733a822017-09-08 16:11:54 -07005459 } else if (target_type == MC_TARGET_DEVICE) {
5460 page = target.page;
5461 if (!mem_cgroup_move_account(page, true,
5462 mc.from, mc.to)) {
5463 mc.precharge -= HPAGE_PMD_NR;
5464 mc.moved_charge += HPAGE_PMD_NR;
5465 }
5466 put_page(page);
Naoya Horiguchi12724852012-03-21 16:34:28 -07005467 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08005468 spin_unlock(ptl);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005469 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005470 }
5471
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005472 if (pmd_trans_unstable(pmd))
5473 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005474retry:
5475 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5476 for (; addr != end; addr += PAGE_SIZE) {
5477 pte_t ptent = *(pte++);
Jérôme Glissec733a822017-09-08 16:11:54 -07005478 bool device = false;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005479 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005480
5481 if (!mc.precharge)
5482 break;
5483
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005484 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Jérôme Glissec733a822017-09-08 16:11:54 -07005485 case MC_TARGET_DEVICE:
5486 device = true;
5487 /* fall through */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005488 case MC_TARGET_PAGE:
5489 page = target.page;
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08005490 /*
5491 * We can have a part of the split pmd here. Moving it
5492 * can be done but it would be too convoluted so simply
5493 * ignore such a partial THP and keep it in original
5494 * memcg. There should be somebody mapping the head.
5495 */
5496 if (PageTransCompound(page))
5497 goto put;
Jérôme Glissec733a822017-09-08 16:11:54 -07005498 if (!device && isolate_lru_page(page))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005499 goto put;
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08005500 if (!mem_cgroup_move_account(page, false,
5501 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005502 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005503 /* we uncharge from mc.from later. */
5504 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005505 }
Jérôme Glissec733a822017-09-08 16:11:54 -07005506 if (!device)
5507 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005508put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005509 put_page(page);
5510 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005511 case MC_TARGET_SWAP:
5512 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07005513 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005514 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005515 /* we fixup refcnts and charges later. */
5516 mc.moved_swap++;
5517 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005518 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005519 default:
5520 break;
5521 }
5522 }
5523 pte_unmap_unlock(pte - 1, ptl);
5524 cond_resched();
5525
5526 if (addr != end) {
5527 /*
5528 * We have consumed all precharges we got in can_attach().
5529 * We try charge one by one, but don't do any additional
5530 * charges to mc.to if we have failed in charge once in attach()
5531 * phase.
5532 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005533 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005534 if (!ret)
5535 goto retry;
5536 }
5537
5538 return ret;
5539}
5540
Tejun Heo264a0ae2016-04-21 19:09:02 -04005541static void mem_cgroup_move_charge(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005542{
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005543 struct mm_walk mem_cgroup_move_charge_walk = {
5544 .pmd_entry = mem_cgroup_move_charge_pte_range,
Tejun Heo264a0ae2016-04-21 19:09:02 -04005545 .mm = mc.mm,
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005546 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005547
5548 lru_add_drain_all();
Johannes Weiner312722c2014-12-10 15:44:25 -08005549 /*
Johannes Weiner81f8c3a2016-03-15 14:57:04 -07005550 * Signal lock_page_memcg() to take the memcg's move_lock
5551 * while we're moving its pages to another memcg. Then wait
5552 * for already started RCU-only updates to finish.
Johannes Weiner312722c2014-12-10 15:44:25 -08005553 */
5554 atomic_inc(&mc.from->moving_account);
5555 synchronize_rcu();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005556retry:
Tejun Heo264a0ae2016-04-21 19:09:02 -04005557 if (unlikely(!down_read_trylock(&mc.mm->mmap_sem))) {
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005558 /*
5559 * Someone who are holding the mmap_sem might be waiting in
5560 * waitq. So we cancel all extra charges, wake up all waiters,
5561 * and retry. Because we cancel precharges, we might not be able
5562 * to move enough charges, but moving charge is a best-effort
5563 * feature anyway, so it wouldn't be a big problem.
5564 */
5565 __mem_cgroup_clear_mc();
5566 cond_resched();
5567 goto retry;
5568 }
Naoya Horiguchi26bcd642015-02-11 15:27:57 -08005569 /*
5570 * When we have consumed all precharges and failed in doing
5571 * additional charge, the page walk just aborts.
5572 */
James Morse0247f3f2016-10-07 17:00:12 -07005573 walk_page_range(0, mc.mm->highest_vm_end, &mem_cgroup_move_charge_walk);
5574
Tejun Heo264a0ae2016-04-21 19:09:02 -04005575 up_read(&mc.mm->mmap_sem);
Johannes Weiner312722c2014-12-10 15:44:25 -08005576 atomic_dec(&mc.from->moving_account);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005577}
5578
Tejun Heo264a0ae2016-04-21 19:09:02 -04005579static void mem_cgroup_move_task(void)
Balbir Singh67e465a2008-02-07 00:13:54 -08005580{
Tejun Heo264a0ae2016-04-21 19:09:02 -04005581 if (mc.to) {
5582 mem_cgroup_move_charge();
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005583 mem_cgroup_clear_mc();
Tejun Heo264a0ae2016-04-21 19:09:02 -04005584 }
Balbir Singh67e465a2008-02-07 00:13:54 -08005585}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005586#else /* !CONFIG_MMU */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005587static int mem_cgroup_can_attach(struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005588{
5589 return 0;
5590}
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05005591static void mem_cgroup_cancel_attach(struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005592{
5593}
Tejun Heo264a0ae2016-04-21 19:09:02 -04005594static void mem_cgroup_move_task(void)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005595{
5596}
5597#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005598
Tejun Heof00baae2013-04-15 13:41:15 -07005599/*
5600 * Cgroup retains root cgroups across [un]mount cycles making it necessary
Tejun Heoaa6ec292014-07-09 10:08:08 -04005601 * to verify whether we're attached to the default hierarchy on each mount
5602 * attempt.
Tejun Heof00baae2013-04-15 13:41:15 -07005603 */
Tejun Heoeb954192013-08-08 20:11:23 -04005604static void mem_cgroup_bind(struct cgroup_subsys_state *root_css)
Tejun Heof00baae2013-04-15 13:41:15 -07005605{
5606 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -04005607 * use_hierarchy is forced on the default hierarchy. cgroup core
Tejun Heof00baae2013-04-15 13:41:15 -07005608 * guarantees that @root doesn't have any children, so turning it
5609 * on for the root memcg is enough.
5610 */
Tejun Heo9e10a132015-09-18 11:56:28 -04005611 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
Vladimir Davydov7feee5902015-03-12 16:26:19 -07005612 root_mem_cgroup->use_hierarchy = true;
5613 else
5614 root_mem_cgroup->use_hierarchy = false;
Tejun Heof00baae2013-04-15 13:41:15 -07005615}
5616
Chris Down677dc972019-03-05 15:45:55 -08005617static int seq_puts_memcg_tunable(struct seq_file *m, unsigned long value)
5618{
5619 if (value == PAGE_COUNTER_MAX)
5620 seq_puts(m, "max\n");
5621 else
5622 seq_printf(m, "%llu\n", (u64)value * PAGE_SIZE);
5623
5624 return 0;
5625}
5626
Johannes Weiner241994ed2015-02-11 15:26:06 -08005627static u64 memory_current_read(struct cgroup_subsys_state *css,
5628 struct cftype *cft)
5629{
Johannes Weinerf5fc3c5d2015-11-05 18:50:23 -08005630 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
5631
5632 return (u64)page_counter_read(&memcg->memory) * PAGE_SIZE;
Johannes Weiner241994ed2015-02-11 15:26:06 -08005633}
5634
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07005635static int memory_min_show(struct seq_file *m, void *v)
5636{
Chris Down677dc972019-03-05 15:45:55 -08005637 return seq_puts_memcg_tunable(m,
5638 READ_ONCE(mem_cgroup_from_seq(m)->memory.min));
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07005639}
5640
5641static ssize_t memory_min_write(struct kernfs_open_file *of,
5642 char *buf, size_t nbytes, loff_t off)
5643{
5644 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
5645 unsigned long min;
5646 int err;
5647
5648 buf = strstrip(buf);
5649 err = page_counter_memparse(buf, "max", &min);
5650 if (err)
5651 return err;
5652
5653 page_counter_set_min(&memcg->memory, min);
5654
5655 return nbytes;
5656}
5657
Johannes Weiner241994ed2015-02-11 15:26:06 -08005658static int memory_low_show(struct seq_file *m, void *v)
5659{
Chris Down677dc972019-03-05 15:45:55 -08005660 return seq_puts_memcg_tunable(m,
5661 READ_ONCE(mem_cgroup_from_seq(m)->memory.low));
Johannes Weiner241994ed2015-02-11 15:26:06 -08005662}
5663
5664static ssize_t memory_low_write(struct kernfs_open_file *of,
5665 char *buf, size_t nbytes, loff_t off)
5666{
5667 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
5668 unsigned long low;
5669 int err;
5670
5671 buf = strstrip(buf);
Johannes Weinerd2973692015-02-27 15:52:04 -08005672 err = page_counter_memparse(buf, "max", &low);
Johannes Weiner241994ed2015-02-11 15:26:06 -08005673 if (err)
5674 return err;
5675
Roman Gushchin23067152018-06-07 17:06:22 -07005676 page_counter_set_low(&memcg->memory, low);
Johannes Weiner241994ed2015-02-11 15:26:06 -08005677
5678 return nbytes;
5679}
5680
5681static int memory_high_show(struct seq_file *m, void *v)
5682{
Chris Down677dc972019-03-05 15:45:55 -08005683 return seq_puts_memcg_tunable(m, READ_ONCE(mem_cgroup_from_seq(m)->high));
Johannes Weiner241994ed2015-02-11 15:26:06 -08005684}
5685
5686static ssize_t memory_high_write(struct kernfs_open_file *of,
5687 char *buf, size_t nbytes, loff_t off)
5688{
5689 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weiner588083b2016-03-17 14:20:25 -07005690 unsigned long nr_pages;
Johannes Weiner241994ed2015-02-11 15:26:06 -08005691 unsigned long high;
5692 int err;
5693
5694 buf = strstrip(buf);
Johannes Weinerd2973692015-02-27 15:52:04 -08005695 err = page_counter_memparse(buf, "max", &high);
Johannes Weiner241994ed2015-02-11 15:26:06 -08005696 if (err)
5697 return err;
5698
5699 memcg->high = high;
5700
Johannes Weiner588083b2016-03-17 14:20:25 -07005701 nr_pages = page_counter_read(&memcg->memory);
5702 if (nr_pages > high)
5703 try_to_free_mem_cgroup_pages(memcg, nr_pages - high,
5704 GFP_KERNEL, true);
5705
Tejun Heo2529bb32015-05-22 18:23:34 -04005706 memcg_wb_domain_size_changed(memcg);
Johannes Weiner241994ed2015-02-11 15:26:06 -08005707 return nbytes;
5708}
5709
5710static int memory_max_show(struct seq_file *m, void *v)
5711{
Chris Down677dc972019-03-05 15:45:55 -08005712 return seq_puts_memcg_tunable(m,
5713 READ_ONCE(mem_cgroup_from_seq(m)->memory.max));
Johannes Weiner241994ed2015-02-11 15:26:06 -08005714}
5715
5716static ssize_t memory_max_write(struct kernfs_open_file *of,
5717 char *buf, size_t nbytes, loff_t off)
5718{
5719 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
Johannes Weinerb6e6edc2016-03-17 14:20:28 -07005720 unsigned int nr_reclaims = MEM_CGROUP_RECLAIM_RETRIES;
5721 bool drained = false;
Johannes Weiner241994ed2015-02-11 15:26:06 -08005722 unsigned long max;
5723 int err;
5724
5725 buf = strstrip(buf);
Johannes Weinerd2973692015-02-27 15:52:04 -08005726 err = page_counter_memparse(buf, "max", &max);
Johannes Weiner241994ed2015-02-11 15:26:06 -08005727 if (err)
5728 return err;
5729
Roman Gushchinbbec2e12018-06-07 17:06:18 -07005730 xchg(&memcg->memory.max, max);
Johannes Weinerb6e6edc2016-03-17 14:20:28 -07005731
5732 for (;;) {
5733 unsigned long nr_pages = page_counter_read(&memcg->memory);
5734
5735 if (nr_pages <= max)
5736 break;
5737
5738 if (signal_pending(current)) {
5739 err = -EINTR;
5740 break;
5741 }
5742
5743 if (!drained) {
5744 drain_all_stock(memcg);
5745 drained = true;
5746 continue;
5747 }
5748
5749 if (nr_reclaims) {
5750 if (!try_to_free_mem_cgroup_pages(memcg, nr_pages - max,
5751 GFP_KERNEL, true))
5752 nr_reclaims--;
5753 continue;
5754 }
5755
Johannes Weinere27be242018-04-10 16:29:45 -07005756 memcg_memory_event(memcg, MEMCG_OOM);
Johannes Weinerb6e6edc2016-03-17 14:20:28 -07005757 if (!mem_cgroup_out_of_memory(memcg, GFP_KERNEL, 0))
5758 break;
5759 }
Johannes Weiner241994ed2015-02-11 15:26:06 -08005760
Tejun Heo2529bb32015-05-22 18:23:34 -04005761 memcg_wb_domain_size_changed(memcg);
Johannes Weiner241994ed2015-02-11 15:26:06 -08005762 return nbytes;
5763}
5764
Shakeel Butt1e577f92019-07-11 20:55:55 -07005765static void __memory_events_show(struct seq_file *m, atomic_long_t *events)
5766{
5767 seq_printf(m, "low %lu\n", atomic_long_read(&events[MEMCG_LOW]));
5768 seq_printf(m, "high %lu\n", atomic_long_read(&events[MEMCG_HIGH]));
5769 seq_printf(m, "max %lu\n", atomic_long_read(&events[MEMCG_MAX]));
5770 seq_printf(m, "oom %lu\n", atomic_long_read(&events[MEMCG_OOM]));
5771 seq_printf(m, "oom_kill %lu\n",
5772 atomic_long_read(&events[MEMCG_OOM_KILL]));
5773}
5774
Johannes Weiner241994ed2015-02-11 15:26:06 -08005775static int memory_events_show(struct seq_file *m, void *v)
5776{
Chris Downaa9694b2019-03-05 15:45:52 -08005777 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
Johannes Weiner241994ed2015-02-11 15:26:06 -08005778
Shakeel Butt1e577f92019-07-11 20:55:55 -07005779 __memory_events_show(m, memcg->memory_events);
5780 return 0;
5781}
Johannes Weiner241994ed2015-02-11 15:26:06 -08005782
Shakeel Butt1e577f92019-07-11 20:55:55 -07005783static int memory_events_local_show(struct seq_file *m, void *v)
5784{
5785 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
5786
5787 __memory_events_show(m, memcg->memory_events_local);
Johannes Weiner241994ed2015-02-11 15:26:06 -08005788 return 0;
5789}
5790
Johannes Weiner587d9f72016-01-20 15:03:19 -08005791static int memory_stat_show(struct seq_file *m, void *v)
5792{
Chris Downaa9694b2019-03-05 15:45:52 -08005793 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
Johannes Weinerc8713d02019-07-11 20:55:59 -07005794 char *buf;
Johannes Weiner587d9f72016-01-20 15:03:19 -08005795
Johannes Weinerc8713d02019-07-11 20:55:59 -07005796 buf = memory_stat_format(memcg);
5797 if (!buf)
5798 return -ENOMEM;
5799 seq_puts(m, buf);
5800 kfree(buf);
Johannes Weiner587d9f72016-01-20 15:03:19 -08005801 return 0;
5802}
5803
Roman Gushchin3d8b38e2018-08-21 21:53:54 -07005804static int memory_oom_group_show(struct seq_file *m, void *v)
5805{
Chris Downaa9694b2019-03-05 15:45:52 -08005806 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
Roman Gushchin3d8b38e2018-08-21 21:53:54 -07005807
5808 seq_printf(m, "%d\n", memcg->oom_group);
5809
5810 return 0;
5811}
5812
5813static ssize_t memory_oom_group_write(struct kernfs_open_file *of,
5814 char *buf, size_t nbytes, loff_t off)
5815{
5816 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
5817 int ret, oom_group;
5818
5819 buf = strstrip(buf);
5820 if (!buf)
5821 return -EINVAL;
5822
5823 ret = kstrtoint(buf, 0, &oom_group);
5824 if (ret)
5825 return ret;
5826
5827 if (oom_group != 0 && oom_group != 1)
5828 return -EINVAL;
5829
5830 memcg->oom_group = oom_group;
5831
5832 return nbytes;
5833}
5834
Johannes Weiner241994ed2015-02-11 15:26:06 -08005835static struct cftype memory_files[] = {
5836 {
5837 .name = "current",
Johannes Weinerf5fc3c5d2015-11-05 18:50:23 -08005838 .flags = CFTYPE_NOT_ON_ROOT,
Johannes Weiner241994ed2015-02-11 15:26:06 -08005839 .read_u64 = memory_current_read,
5840 },
5841 {
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07005842 .name = "min",
5843 .flags = CFTYPE_NOT_ON_ROOT,
5844 .seq_show = memory_min_show,
5845 .write = memory_min_write,
5846 },
5847 {
Johannes Weiner241994ed2015-02-11 15:26:06 -08005848 .name = "low",
5849 .flags = CFTYPE_NOT_ON_ROOT,
5850 .seq_show = memory_low_show,
5851 .write = memory_low_write,
5852 },
5853 {
5854 .name = "high",
5855 .flags = CFTYPE_NOT_ON_ROOT,
5856 .seq_show = memory_high_show,
5857 .write = memory_high_write,
5858 },
5859 {
5860 .name = "max",
5861 .flags = CFTYPE_NOT_ON_ROOT,
5862 .seq_show = memory_max_show,
5863 .write = memory_max_write,
5864 },
5865 {
5866 .name = "events",
5867 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo472912a2015-09-18 18:01:59 -04005868 .file_offset = offsetof(struct mem_cgroup, events_file),
Johannes Weiner241994ed2015-02-11 15:26:06 -08005869 .seq_show = memory_events_show,
5870 },
Johannes Weiner587d9f72016-01-20 15:03:19 -08005871 {
Shakeel Butt1e577f92019-07-11 20:55:55 -07005872 .name = "events.local",
5873 .flags = CFTYPE_NOT_ON_ROOT,
5874 .file_offset = offsetof(struct mem_cgroup, events_local_file),
5875 .seq_show = memory_events_local_show,
5876 },
5877 {
Johannes Weiner587d9f72016-01-20 15:03:19 -08005878 .name = "stat",
5879 .flags = CFTYPE_NOT_ON_ROOT,
5880 .seq_show = memory_stat_show,
5881 },
Roman Gushchin3d8b38e2018-08-21 21:53:54 -07005882 {
5883 .name = "oom.group",
5884 .flags = CFTYPE_NOT_ON_ROOT | CFTYPE_NS_DELEGATABLE,
5885 .seq_show = memory_oom_group_show,
5886 .write = memory_oom_group_write,
5887 },
Johannes Weiner241994ed2015-02-11 15:26:06 -08005888 { } /* terminate */
5889};
5890
Tejun Heo073219e2014-02-08 10:36:58 -05005891struct cgroup_subsys memory_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08005892 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08005893 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08005894 .css_offline = mem_cgroup_css_offline,
Vladimir Davydov6df38682015-12-29 14:54:10 -08005895 .css_released = mem_cgroup_css_released,
Tejun Heo92fb9742012-11-19 08:13:38 -08005896 .css_free = mem_cgroup_css_free,
Tejun Heo1ced9532014-07-08 18:02:57 -04005897 .css_reset = mem_cgroup_css_reset,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005898 .can_attach = mem_cgroup_can_attach,
5899 .cancel_attach = mem_cgroup_cancel_attach,
Tejun Heo264a0ae2016-04-21 19:09:02 -04005900 .post_attach = mem_cgroup_move_task,
Tejun Heof00baae2013-04-15 13:41:15 -07005901 .bind = mem_cgroup_bind,
Johannes Weiner241994ed2015-02-11 15:26:06 -08005902 .dfl_cftypes = memory_files,
5903 .legacy_cftypes = mem_cgroup_legacy_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005904 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005905};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005906
Johannes Weiner241994ed2015-02-11 15:26:06 -08005907/**
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07005908 * mem_cgroup_protected - check if memory consumption is in the normal range
Sean Christopherson34c81052017-07-10 15:48:05 -07005909 * @root: the top ancestor of the sub-tree being checked
Johannes Weiner241994ed2015-02-11 15:26:06 -08005910 * @memcg: the memory cgroup to check
5911 *
Roman Gushchin23067152018-06-07 17:06:22 -07005912 * WARNING: This function is not stateless! It can only be used as part
5913 * of a top-down tree iteration, not for isolated queries.
Sean Christopherson34c81052017-07-10 15:48:05 -07005914 *
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07005915 * Returns one of the following:
5916 * MEMCG_PROT_NONE: cgroup memory is not protected
5917 * MEMCG_PROT_LOW: cgroup memory is protected as long there is
5918 * an unprotected supply of reclaimable memory from other cgroups.
5919 * MEMCG_PROT_MIN: cgroup memory is protected
Sean Christopherson34c81052017-07-10 15:48:05 -07005920 *
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07005921 * @root is exclusive; it is never protected when looked at directly
Sean Christopherson34c81052017-07-10 15:48:05 -07005922 *
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07005923 * To provide a proper hierarchical behavior, effective memory.min/low values
5924 * are used. Below is the description of how effective memory.low is calculated.
5925 * Effective memory.min values is calculated in the same way.
Sean Christopherson34c81052017-07-10 15:48:05 -07005926 *
Roman Gushchin23067152018-06-07 17:06:22 -07005927 * Effective memory.low is always equal or less than the original memory.low.
5928 * If there is no memory.low overcommittment (which is always true for
5929 * top-level memory cgroups), these two values are equal.
5930 * Otherwise, it's a part of parent's effective memory.low,
5931 * calculated as a cgroup's memory.low usage divided by sum of sibling's
5932 * memory.low usages, where memory.low usage is the size of actually
5933 * protected memory.
Sean Christopherson34c81052017-07-10 15:48:05 -07005934 *
Roman Gushchin23067152018-06-07 17:06:22 -07005935 * low_usage
5936 * elow = min( memory.low, parent->elow * ------------------ ),
5937 * siblings_low_usage
Sean Christopherson34c81052017-07-10 15:48:05 -07005938 *
Roman Gushchin23067152018-06-07 17:06:22 -07005939 * | memory.current, if memory.current < memory.low
5940 * low_usage = |
Qian Cai82ede7e2019-03-05 15:49:53 -08005941 * | 0, otherwise.
Sean Christopherson34c81052017-07-10 15:48:05 -07005942 *
Roman Gushchin23067152018-06-07 17:06:22 -07005943 *
5944 * Such definition of the effective memory.low provides the expected
5945 * hierarchical behavior: parent's memory.low value is limiting
5946 * children, unprotected memory is reclaimed first and cgroups,
5947 * which are not using their guarantee do not affect actual memory
5948 * distribution.
5949 *
5950 * For example, if there are memcgs A, A/B, A/C, A/D and A/E:
5951 *
5952 * A A/memory.low = 2G, A/memory.current = 6G
5953 * //\\
5954 * BC DE B/memory.low = 3G B/memory.current = 2G
5955 * C/memory.low = 1G C/memory.current = 2G
5956 * D/memory.low = 0 D/memory.current = 2G
5957 * E/memory.low = 10G E/memory.current = 0
5958 *
5959 * and the memory pressure is applied, the following memory distribution
5960 * is expected (approximately):
5961 *
5962 * A/memory.current = 2G
5963 *
5964 * B/memory.current = 1.3G
5965 * C/memory.current = 0.6G
5966 * D/memory.current = 0
5967 * E/memory.current = 0
5968 *
5969 * These calculations require constant tracking of the actual low usages
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07005970 * (see propagate_protected_usage()), as well as recursive calculation of
5971 * effective memory.low values. But as we do call mem_cgroup_protected()
Roman Gushchin23067152018-06-07 17:06:22 -07005972 * path for each memory cgroup top-down from the reclaim,
5973 * it's possible to optimize this part, and save calculated elow
5974 * for next usage. This part is intentionally racy, but it's ok,
5975 * as memory.low is a best-effort mechanism.
Johannes Weiner241994ed2015-02-11 15:26:06 -08005976 */
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07005977enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root,
5978 struct mem_cgroup *memcg)
Johannes Weiner241994ed2015-02-11 15:26:06 -08005979{
Roman Gushchin23067152018-06-07 17:06:22 -07005980 struct mem_cgroup *parent;
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07005981 unsigned long emin, parent_emin;
5982 unsigned long elow, parent_elow;
5983 unsigned long usage;
Roman Gushchin23067152018-06-07 17:06:22 -07005984
Johannes Weiner241994ed2015-02-11 15:26:06 -08005985 if (mem_cgroup_disabled())
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07005986 return MEMCG_PROT_NONE;
Johannes Weiner241994ed2015-02-11 15:26:06 -08005987
Sean Christopherson34c81052017-07-10 15:48:05 -07005988 if (!root)
5989 root = root_mem_cgroup;
5990 if (memcg == root)
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07005991 return MEMCG_PROT_NONE;
Johannes Weiner241994ed2015-02-11 15:26:06 -08005992
Roman Gushchin23067152018-06-07 17:06:22 -07005993 usage = page_counter_read(&memcg->memory);
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07005994 if (!usage)
5995 return MEMCG_PROT_NONE;
Sean Christopherson34c81052017-07-10 15:48:05 -07005996
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07005997 emin = memcg->memory.min;
5998 elow = memcg->memory.low;
5999
6000 parent = parent_mem_cgroup(memcg);
Roman Gushchindf2a4192018-06-14 15:26:17 -07006001 /* No parent means a non-hierarchical mode on v1 memcg */
6002 if (!parent)
6003 return MEMCG_PROT_NONE;
6004
Roman Gushchin23067152018-06-07 17:06:22 -07006005 if (parent == root)
6006 goto exit;
6007
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006008 parent_emin = READ_ONCE(parent->memory.emin);
6009 emin = min(emin, parent_emin);
6010 if (emin && parent_emin) {
6011 unsigned long min_usage, siblings_min_usage;
6012
6013 min_usage = min(usage, memcg->memory.min);
6014 siblings_min_usage = atomic_long_read(
6015 &parent->memory.children_min_usage);
6016
6017 if (min_usage && siblings_min_usage)
6018 emin = min(emin, parent_emin * min_usage /
6019 siblings_min_usage);
6020 }
6021
Roman Gushchin23067152018-06-07 17:06:22 -07006022 parent_elow = READ_ONCE(parent->memory.elow);
6023 elow = min(elow, parent_elow);
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006024 if (elow && parent_elow) {
6025 unsigned long low_usage, siblings_low_usage;
Roman Gushchin23067152018-06-07 17:06:22 -07006026
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006027 low_usage = min(usage, memcg->memory.low);
6028 siblings_low_usage = atomic_long_read(
6029 &parent->memory.children_low_usage);
Roman Gushchin23067152018-06-07 17:06:22 -07006030
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006031 if (low_usage && siblings_low_usage)
6032 elow = min(elow, parent_elow * low_usage /
6033 siblings_low_usage);
6034 }
Roman Gushchin23067152018-06-07 17:06:22 -07006035
Roman Gushchin23067152018-06-07 17:06:22 -07006036exit:
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006037 memcg->memory.emin = emin;
Roman Gushchin23067152018-06-07 17:06:22 -07006038 memcg->memory.elow = elow;
Roman Gushchinbf8d5d52018-06-07 17:07:46 -07006039
6040 if (usage <= emin)
6041 return MEMCG_PROT_MIN;
6042 else if (usage <= elow)
6043 return MEMCG_PROT_LOW;
6044 else
6045 return MEMCG_PROT_NONE;
Johannes Weiner241994ed2015-02-11 15:26:06 -08006046}
6047
Johannes Weiner00501b52014-08-08 14:19:20 -07006048/**
6049 * mem_cgroup_try_charge - try charging a page
6050 * @page: page to charge
6051 * @mm: mm context of the victim
6052 * @gfp_mask: reclaim mode
6053 * @memcgp: charged memcg return
Li RongQing25843c22016-07-26 15:26:56 -07006054 * @compound: charge the page as compound or small page
Johannes Weiner00501b52014-08-08 14:19:20 -07006055 *
6056 * Try to charge @page to the memcg that @mm belongs to, reclaiming
6057 * pages according to @gfp_mask if necessary.
6058 *
6059 * Returns 0 on success, with *@memcgp pointing to the charged memcg.
6060 * Otherwise, an error code is returned.
6061 *
6062 * After page->mapping has been set up, the caller must finalize the
6063 * charge with mem_cgroup_commit_charge(). Or abort the transaction
6064 * with mem_cgroup_cancel_charge() in case page instantiation fails.
6065 */
6066int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08006067 gfp_t gfp_mask, struct mem_cgroup **memcgp,
6068 bool compound)
Johannes Weiner00501b52014-08-08 14:19:20 -07006069{
6070 struct mem_cgroup *memcg = NULL;
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08006071 unsigned int nr_pages = compound ? hpage_nr_pages(page) : 1;
Johannes Weiner00501b52014-08-08 14:19:20 -07006072 int ret = 0;
6073
6074 if (mem_cgroup_disabled())
6075 goto out;
6076
6077 if (PageSwapCache(page)) {
Johannes Weiner00501b52014-08-08 14:19:20 -07006078 /*
6079 * Every swap fault against a single page tries to charge the
6080 * page, bail as early as possible. shmem_unuse() encounters
6081 * already charged pages, too. The USED bit is protected by
6082 * the page lock, which serializes swap cache removal, which
6083 * in turn serializes uncharging.
6084 */
Vladimir Davydove993d902015-09-09 15:35:35 -07006085 VM_BUG_ON_PAGE(!PageLocked(page), page);
Huang Yingabe28952017-09-06 16:22:41 -07006086 if (compound_head(page)->mem_cgroup)
Johannes Weiner00501b52014-08-08 14:19:20 -07006087 goto out;
Vladimir Davydove993d902015-09-09 15:35:35 -07006088
Vladimir Davydov37e84352016-01-20 15:02:56 -08006089 if (do_swap_account) {
Vladimir Davydove993d902015-09-09 15:35:35 -07006090 swp_entry_t ent = { .val = page_private(page), };
6091 unsigned short id = lookup_swap_cgroup_id(ent);
6092
6093 rcu_read_lock();
6094 memcg = mem_cgroup_from_id(id);
6095 if (memcg && !css_tryget_online(&memcg->css))
6096 memcg = NULL;
6097 rcu_read_unlock();
6098 }
Johannes Weiner00501b52014-08-08 14:19:20 -07006099 }
6100
Johannes Weiner00501b52014-08-08 14:19:20 -07006101 if (!memcg)
6102 memcg = get_mem_cgroup_from_mm(mm);
6103
6104 ret = try_charge(memcg, gfp_mask, nr_pages);
6105
6106 css_put(&memcg->css);
Johannes Weiner00501b52014-08-08 14:19:20 -07006107out:
6108 *memcgp = memcg;
6109 return ret;
6110}
6111
Tejun Heo2cf85582018-07-03 11:14:56 -04006112int mem_cgroup_try_charge_delay(struct page *page, struct mm_struct *mm,
6113 gfp_t gfp_mask, struct mem_cgroup **memcgp,
6114 bool compound)
6115{
6116 struct mem_cgroup *memcg;
6117 int ret;
6118
6119 ret = mem_cgroup_try_charge(page, mm, gfp_mask, memcgp, compound);
6120 memcg = *memcgp;
6121 mem_cgroup_throttle_swaprate(memcg, page_to_nid(page), gfp_mask);
6122 return ret;
6123}
6124
Johannes Weiner00501b52014-08-08 14:19:20 -07006125/**
6126 * mem_cgroup_commit_charge - commit a page charge
6127 * @page: page to charge
6128 * @memcg: memcg to charge the page to
6129 * @lrucare: page might be on LRU already
Li RongQing25843c22016-07-26 15:26:56 -07006130 * @compound: charge the page as compound or small page
Johannes Weiner00501b52014-08-08 14:19:20 -07006131 *
6132 * Finalize a charge transaction started by mem_cgroup_try_charge(),
6133 * after page->mapping has been set up. This must happen atomically
6134 * as part of the page instantiation, i.e. under the page table lock
6135 * for anonymous pages, under the page lock for page and swap cache.
6136 *
6137 * In addition, the page must not be on the LRU during the commit, to
6138 * prevent racing with task migration. If it might be, use @lrucare.
6139 *
6140 * Use mem_cgroup_cancel_charge() to cancel the transaction instead.
6141 */
6142void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08006143 bool lrucare, bool compound)
Johannes Weiner00501b52014-08-08 14:19:20 -07006144{
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08006145 unsigned int nr_pages = compound ? hpage_nr_pages(page) : 1;
Johannes Weiner00501b52014-08-08 14:19:20 -07006146
6147 VM_BUG_ON_PAGE(!page->mapping, page);
6148 VM_BUG_ON_PAGE(PageLRU(page) && !lrucare, page);
6149
6150 if (mem_cgroup_disabled())
6151 return;
6152 /*
6153 * Swap faults will attempt to charge the same page multiple
6154 * times. But reuse_swap_page() might have removed the page
6155 * from swapcache already, so we can't check PageSwapCache().
6156 */
6157 if (!memcg)
6158 return;
6159
Johannes Weiner6abb5a82014-08-08 14:19:33 -07006160 commit_charge(page, memcg, lrucare);
6161
Johannes Weiner6abb5a82014-08-08 14:19:33 -07006162 local_irq_disable();
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08006163 mem_cgroup_charge_statistics(memcg, page, compound, nr_pages);
Johannes Weiner6abb5a82014-08-08 14:19:33 -07006164 memcg_check_events(memcg, page);
6165 local_irq_enable();
Johannes Weiner00501b52014-08-08 14:19:20 -07006166
Johannes Weiner7941d212016-01-14 15:21:23 -08006167 if (do_memsw_account() && PageSwapCache(page)) {
Johannes Weiner00501b52014-08-08 14:19:20 -07006168 swp_entry_t entry = { .val = page_private(page) };
6169 /*
6170 * The swap entry might not get freed for a long time,
6171 * let's not wait for it. The page already received a
6172 * memory+swap charge, drop the swap entry duplicate.
6173 */
Huang Ying38d8b4e2017-07-06 15:37:18 -07006174 mem_cgroup_uncharge_swap(entry, nr_pages);
Johannes Weiner00501b52014-08-08 14:19:20 -07006175 }
6176}
6177
6178/**
6179 * mem_cgroup_cancel_charge - cancel a page charge
6180 * @page: page to charge
6181 * @memcg: memcg to charge the page to
Li RongQing25843c22016-07-26 15:26:56 -07006182 * @compound: charge the page as compound or small page
Johannes Weiner00501b52014-08-08 14:19:20 -07006183 *
6184 * Cancel a charge transaction started by mem_cgroup_try_charge().
6185 */
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08006186void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg,
6187 bool compound)
Johannes Weiner00501b52014-08-08 14:19:20 -07006188{
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08006189 unsigned int nr_pages = compound ? hpage_nr_pages(page) : 1;
Johannes Weiner00501b52014-08-08 14:19:20 -07006190
6191 if (mem_cgroup_disabled())
6192 return;
6193 /*
6194 * Swap faults will attempt to charge the same page multiple
6195 * times. But reuse_swap_page() might have removed the page
6196 * from swapcache already, so we can't check PageSwapCache().
6197 */
6198 if (!memcg)
6199 return;
6200
Johannes Weiner00501b52014-08-08 14:19:20 -07006201 cancel_charge(memcg, nr_pages);
6202}
6203
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006204struct uncharge_gather {
6205 struct mem_cgroup *memcg;
6206 unsigned long pgpgout;
6207 unsigned long nr_anon;
6208 unsigned long nr_file;
6209 unsigned long nr_kmem;
6210 unsigned long nr_huge;
6211 unsigned long nr_shmem;
6212 struct page *dummy_page;
6213};
6214
6215static inline void uncharge_gather_clear(struct uncharge_gather *ug)
Johannes Weiner747db952014-08-08 14:19:24 -07006216{
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006217 memset(ug, 0, sizeof(*ug));
6218}
6219
6220static void uncharge_batch(const struct uncharge_gather *ug)
6221{
6222 unsigned long nr_pages = ug->nr_anon + ug->nr_file + ug->nr_kmem;
Johannes Weiner747db952014-08-08 14:19:24 -07006223 unsigned long flags;
6224
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006225 if (!mem_cgroup_is_root(ug->memcg)) {
6226 page_counter_uncharge(&ug->memcg->memory, nr_pages);
Johannes Weiner7941d212016-01-14 15:21:23 -08006227 if (do_memsw_account())
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006228 page_counter_uncharge(&ug->memcg->memsw, nr_pages);
6229 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && ug->nr_kmem)
6230 page_counter_uncharge(&ug->memcg->kmem, ug->nr_kmem);
6231 memcg_oom_recover(ug->memcg);
Johannes Weinerce00a962014-09-05 08:43:57 -04006232 }
Johannes Weiner747db952014-08-08 14:19:24 -07006233
6234 local_irq_save(flags);
Johannes Weinerc9019e92018-01-31 16:16:37 -08006235 __mod_memcg_state(ug->memcg, MEMCG_RSS, -ug->nr_anon);
6236 __mod_memcg_state(ug->memcg, MEMCG_CACHE, -ug->nr_file);
6237 __mod_memcg_state(ug->memcg, MEMCG_RSS_HUGE, -ug->nr_huge);
6238 __mod_memcg_state(ug->memcg, NR_SHMEM, -ug->nr_shmem);
6239 __count_memcg_events(ug->memcg, PGPGOUT, ug->pgpgout);
Chris Down871789d2019-05-14 15:46:57 -07006240 __this_cpu_add(ug->memcg->vmstats_percpu->nr_page_events, nr_pages);
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006241 memcg_check_events(ug->memcg, ug->dummy_page);
Johannes Weiner747db952014-08-08 14:19:24 -07006242 local_irq_restore(flags);
Johannes Weinere8ea14c2014-12-10 15:42:42 -08006243
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006244 if (!mem_cgroup_is_root(ug->memcg))
6245 css_put_many(&ug->memcg->css, nr_pages);
6246}
6247
6248static void uncharge_page(struct page *page, struct uncharge_gather *ug)
6249{
6250 VM_BUG_ON_PAGE(PageLRU(page), page);
Jérôme Glisse3f2eb022017-10-03 16:14:57 -07006251 VM_BUG_ON_PAGE(page_count(page) && !is_zone_device_page(page) &&
6252 !PageHWPoison(page) , page);
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006253
6254 if (!page->mem_cgroup)
6255 return;
6256
6257 /*
6258 * Nobody should be changing or seriously looking at
6259 * page->mem_cgroup at this point, we have fully
6260 * exclusive access to the page.
6261 */
6262
6263 if (ug->memcg != page->mem_cgroup) {
6264 if (ug->memcg) {
6265 uncharge_batch(ug);
6266 uncharge_gather_clear(ug);
6267 }
6268 ug->memcg = page->mem_cgroup;
6269 }
6270
6271 if (!PageKmemcg(page)) {
6272 unsigned int nr_pages = 1;
6273
6274 if (PageTransHuge(page)) {
6275 nr_pages <<= compound_order(page);
6276 ug->nr_huge += nr_pages;
6277 }
6278 if (PageAnon(page))
6279 ug->nr_anon += nr_pages;
6280 else {
6281 ug->nr_file += nr_pages;
6282 if (PageSwapBacked(page))
6283 ug->nr_shmem += nr_pages;
6284 }
6285 ug->pgpgout++;
6286 } else {
6287 ug->nr_kmem += 1 << compound_order(page);
6288 __ClearPageKmemcg(page);
6289 }
6290
6291 ug->dummy_page = page;
6292 page->mem_cgroup = NULL;
Johannes Weiner747db952014-08-08 14:19:24 -07006293}
6294
6295static void uncharge_list(struct list_head *page_list)
6296{
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006297 struct uncharge_gather ug;
Johannes Weiner747db952014-08-08 14:19:24 -07006298 struct list_head *next;
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006299
6300 uncharge_gather_clear(&ug);
Johannes Weiner747db952014-08-08 14:19:24 -07006301
Johannes Weiner8b592652016-03-17 14:20:31 -07006302 /*
6303 * Note that the list can be a single page->lru; hence the
6304 * do-while loop instead of a simple list_for_each_entry().
6305 */
Johannes Weiner747db952014-08-08 14:19:24 -07006306 next = page_list->next;
6307 do {
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006308 struct page *page;
6309
Johannes Weiner747db952014-08-08 14:19:24 -07006310 page = list_entry(next, struct page, lru);
6311 next = page->lru.next;
6312
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006313 uncharge_page(page, &ug);
Johannes Weiner747db952014-08-08 14:19:24 -07006314 } while (next != page_list);
6315
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006316 if (ug.memcg)
6317 uncharge_batch(&ug);
Johannes Weiner747db952014-08-08 14:19:24 -07006318}
6319
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006320/**
6321 * mem_cgroup_uncharge - uncharge a page
6322 * @page: page to uncharge
6323 *
6324 * Uncharge a page previously charged with mem_cgroup_try_charge() and
6325 * mem_cgroup_commit_charge().
6326 */
6327void mem_cgroup_uncharge(struct page *page)
6328{
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006329 struct uncharge_gather ug;
6330
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006331 if (mem_cgroup_disabled())
6332 return;
6333
Johannes Weiner747db952014-08-08 14:19:24 -07006334 /* Don't touch page->lru of any random page, pre-check: */
Johannes Weiner1306a852014-12-10 15:44:52 -08006335 if (!page->mem_cgroup)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006336 return;
6337
Jérôme Glissea9d5ade2017-09-08 16:11:50 -07006338 uncharge_gather_clear(&ug);
6339 uncharge_page(page, &ug);
6340 uncharge_batch(&ug);
Johannes Weiner747db952014-08-08 14:19:24 -07006341}
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006342
Johannes Weiner747db952014-08-08 14:19:24 -07006343/**
6344 * mem_cgroup_uncharge_list - uncharge a list of page
6345 * @page_list: list of pages to uncharge
6346 *
6347 * Uncharge a list of pages previously charged with
6348 * mem_cgroup_try_charge() and mem_cgroup_commit_charge().
6349 */
6350void mem_cgroup_uncharge_list(struct list_head *page_list)
6351{
6352 if (mem_cgroup_disabled())
6353 return;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006354
Johannes Weiner747db952014-08-08 14:19:24 -07006355 if (!list_empty(page_list))
6356 uncharge_list(page_list);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006357}
6358
6359/**
Johannes Weiner6a93ca82016-03-15 14:57:19 -07006360 * mem_cgroup_migrate - charge a page's replacement
6361 * @oldpage: currently circulating page
6362 * @newpage: replacement page
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006363 *
Johannes Weiner6a93ca82016-03-15 14:57:19 -07006364 * Charge @newpage as a replacement page for @oldpage. @oldpage will
6365 * be uncharged upon free.
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006366 *
6367 * Both pages must be locked, @newpage->mapping must be set up.
6368 */
Johannes Weiner6a93ca82016-03-15 14:57:19 -07006369void mem_cgroup_migrate(struct page *oldpage, struct page *newpage)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006370{
Johannes Weiner29833312014-12-10 15:44:02 -08006371 struct mem_cgroup *memcg;
Johannes Weiner44b7a8d2016-01-20 15:03:16 -08006372 unsigned int nr_pages;
6373 bool compound;
Tejun Heod93c4132016-06-24 14:49:54 -07006374 unsigned long flags;
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006375
6376 VM_BUG_ON_PAGE(!PageLocked(oldpage), oldpage);
6377 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006378 VM_BUG_ON_PAGE(PageAnon(oldpage) != PageAnon(newpage), newpage);
Johannes Weiner6abb5a82014-08-08 14:19:33 -07006379 VM_BUG_ON_PAGE(PageTransHuge(oldpage) != PageTransHuge(newpage),
6380 newpage);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006381
6382 if (mem_cgroup_disabled())
6383 return;
6384
6385 /* Page cache replacement: new page already charged? */
Johannes Weiner1306a852014-12-10 15:44:52 -08006386 if (newpage->mem_cgroup)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006387 return;
6388
Hugh Dickins45637ba2015-11-05 18:49:40 -08006389 /* Swapcache readahead pages can get replaced before being charged */
Johannes Weiner1306a852014-12-10 15:44:52 -08006390 memcg = oldpage->mem_cgroup;
Johannes Weiner29833312014-12-10 15:44:02 -08006391 if (!memcg)
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006392 return;
6393
Johannes Weiner44b7a8d2016-01-20 15:03:16 -08006394 /* Force-charge the new page. The old one will be freed soon */
6395 compound = PageTransHuge(newpage);
6396 nr_pages = compound ? hpage_nr_pages(newpage) : 1;
6397
6398 page_counter_charge(&memcg->memory, nr_pages);
6399 if (do_memsw_account())
6400 page_counter_charge(&memcg->memsw, nr_pages);
6401 css_get_many(&memcg->css, nr_pages);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006402
Johannes Weiner9cf76662016-03-15 14:57:58 -07006403 commit_charge(newpage, memcg, false);
Johannes Weiner44b7a8d2016-01-20 15:03:16 -08006404
Tejun Heod93c4132016-06-24 14:49:54 -07006405 local_irq_save(flags);
Johannes Weiner44b7a8d2016-01-20 15:03:16 -08006406 mem_cgroup_charge_statistics(memcg, newpage, compound, nr_pages);
6407 memcg_check_events(memcg, newpage);
Tejun Heod93c4132016-06-24 14:49:54 -07006408 local_irq_restore(flags);
Johannes Weiner0a31bc92014-08-08 14:19:22 -07006409}
6410
Johannes Weineref129472016-01-14 15:21:34 -08006411DEFINE_STATIC_KEY_FALSE(memcg_sockets_enabled_key);
Johannes Weiner11092082016-01-14 15:21:26 -08006412EXPORT_SYMBOL(memcg_sockets_enabled_key);
6413
Johannes Weiner2d758072016-10-07 17:00:58 -07006414void mem_cgroup_sk_alloc(struct sock *sk)
Johannes Weiner11092082016-01-14 15:21:26 -08006415{
6416 struct mem_cgroup *memcg;
6417
Johannes Weiner2d758072016-10-07 17:00:58 -07006418 if (!mem_cgroup_sockets_enabled)
6419 return;
6420
Roman Gushchinedbe69e2018-02-02 15:26:57 +00006421 /*
6422 * Socket cloning can throw us here with sk_memcg already
6423 * filled. It won't however, necessarily happen from
6424 * process context. So the test for root memcg given
6425 * the current task's memcg won't help us in this case.
6426 *
6427 * Respecting the original socket's memcg is a better
6428 * decision in this case.
6429 */
6430 if (sk->sk_memcg) {
6431 css_get(&sk->sk_memcg->css);
6432 return;
6433 }
6434
Johannes Weiner11092082016-01-14 15:21:26 -08006435 rcu_read_lock();
6436 memcg = mem_cgroup_from_task(current);
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006437 if (memcg == root_mem_cgroup)
6438 goto out;
Johannes Weiner0db15292016-01-20 15:02:50 -08006439 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && !memcg->tcpmem_active)
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006440 goto out;
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006441 if (css_tryget_online(&memcg->css))
Johannes Weiner11092082016-01-14 15:21:26 -08006442 sk->sk_memcg = memcg;
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006443out:
Johannes Weiner11092082016-01-14 15:21:26 -08006444 rcu_read_unlock();
6445}
Johannes Weiner11092082016-01-14 15:21:26 -08006446
Johannes Weiner2d758072016-10-07 17:00:58 -07006447void mem_cgroup_sk_free(struct sock *sk)
Johannes Weiner11092082016-01-14 15:21:26 -08006448{
Johannes Weiner2d758072016-10-07 17:00:58 -07006449 if (sk->sk_memcg)
6450 css_put(&sk->sk_memcg->css);
Johannes Weiner11092082016-01-14 15:21:26 -08006451}
6452
6453/**
6454 * mem_cgroup_charge_skmem - charge socket memory
6455 * @memcg: memcg to charge
6456 * @nr_pages: number of pages to charge
6457 *
6458 * Charges @nr_pages to @memcg. Returns %true if the charge fit within
6459 * @memcg's configured limit, %false if the charge had to be forced.
6460 */
6461bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages)
6462{
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006463 gfp_t gfp_mask = GFP_KERNEL;
Johannes Weiner11092082016-01-14 15:21:26 -08006464
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006465 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
Johannes Weiner0db15292016-01-20 15:02:50 -08006466 struct page_counter *fail;
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006467
Johannes Weiner0db15292016-01-20 15:02:50 -08006468 if (page_counter_try_charge(&memcg->tcpmem, nr_pages, &fail)) {
6469 memcg->tcpmem_pressure = 0;
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006470 return true;
6471 }
Johannes Weiner0db15292016-01-20 15:02:50 -08006472 page_counter_charge(&memcg->tcpmem, nr_pages);
6473 memcg->tcpmem_pressure = 1;
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006474 return false;
Johannes Weiner11092082016-01-14 15:21:26 -08006475 }
Johannes Weinerd886f4e2016-01-20 15:02:47 -08006476
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006477 /* Don't block in the packet receive path */
6478 if (in_softirq())
6479 gfp_mask = GFP_NOWAIT;
6480
Johannes Weinerc9019e92018-01-31 16:16:37 -08006481 mod_memcg_state(memcg, MEMCG_SOCK, nr_pages);
Johannes Weinerb2807f02016-01-20 15:03:22 -08006482
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006483 if (try_charge(memcg, gfp_mask, nr_pages) == 0)
6484 return true;
6485
6486 try_charge(memcg, gfp_mask|__GFP_NOFAIL, nr_pages);
Johannes Weiner11092082016-01-14 15:21:26 -08006487 return false;
6488}
6489
6490/**
6491 * mem_cgroup_uncharge_skmem - uncharge socket memory
Mike Rapoportb7701a52018-02-06 15:42:13 -08006492 * @memcg: memcg to uncharge
6493 * @nr_pages: number of pages to uncharge
Johannes Weiner11092082016-01-14 15:21:26 -08006494 */
6495void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages)
6496{
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006497 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) {
Johannes Weiner0db15292016-01-20 15:02:50 -08006498 page_counter_uncharge(&memcg->tcpmem, nr_pages);
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006499 return;
6500 }
Johannes Weinerd886f4e2016-01-20 15:02:47 -08006501
Johannes Weinerc9019e92018-01-31 16:16:37 -08006502 mod_memcg_state(memcg, MEMCG_SOCK, -nr_pages);
Johannes Weinerb2807f02016-01-20 15:03:22 -08006503
Roman Gushchin475d0482017-09-08 16:13:09 -07006504 refill_stock(memcg, nr_pages);
Johannes Weiner11092082016-01-14 15:21:26 -08006505}
6506
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006507static int __init cgroup_memory(char *s)
6508{
6509 char *token;
6510
6511 while ((token = strsep(&s, ",")) != NULL) {
6512 if (!*token)
6513 continue;
6514 if (!strcmp(token, "nosocket"))
6515 cgroup_memory_nosocket = true;
Vladimir Davydov04823c82016-01-20 15:02:38 -08006516 if (!strcmp(token, "nokmem"))
6517 cgroup_memory_nokmem = true;
Johannes Weinerf7e1cb62016-01-14 15:21:29 -08006518 }
6519 return 0;
6520}
6521__setup("cgroup.memory=", cgroup_memory);
Johannes Weiner11092082016-01-14 15:21:26 -08006522
Michal Hocko2d110852013-02-22 16:34:43 -08006523/*
Michal Hocko10813122013-02-22 16:35:41 -08006524 * subsys_initcall() for memory controller.
6525 *
Sebastian Andrzej Siewior308167f2016-11-03 15:49:59 +01006526 * Some parts like memcg_hotplug_cpu_dead() have to be initialized from this
6527 * context because of lock dependencies (cgroup_lock -> cpu hotplug) but
6528 * basically everything that doesn't depend on a specific mem_cgroup structure
6529 * should be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08006530 */
6531static int __init mem_cgroup_init(void)
6532{
Johannes Weiner95a045f2015-02-11 15:26:33 -08006533 int cpu, node;
6534
Kirill Tkhai84c07d12018-08-17 15:47:25 -07006535#ifdef CONFIG_MEMCG_KMEM
Vladimir Davydov13583c32016-12-12 16:41:29 -08006536 /*
6537 * Kmem cache creation is mostly done with the slab_mutex held,
Tejun Heo17cc4df2017-02-22 15:41:36 -08006538 * so use a workqueue with limited concurrency to avoid stalling
6539 * all worker threads in case lots of cgroups are created and
6540 * destroyed simultaneously.
Vladimir Davydov13583c32016-12-12 16:41:29 -08006541 */
Tejun Heo17cc4df2017-02-22 15:41:36 -08006542 memcg_kmem_cache_wq = alloc_workqueue("memcg_kmem_cache", 0, 1);
6543 BUG_ON(!memcg_kmem_cache_wq);
Vladimir Davydov13583c32016-12-12 16:41:29 -08006544#endif
6545
Sebastian Andrzej Siewior308167f2016-11-03 15:49:59 +01006546 cpuhp_setup_state_nocalls(CPUHP_MM_MEMCQ_DEAD, "mm/memctrl:dead", NULL,
6547 memcg_hotplug_cpu_dead);
Johannes Weiner95a045f2015-02-11 15:26:33 -08006548
6549 for_each_possible_cpu(cpu)
6550 INIT_WORK(&per_cpu_ptr(&memcg_stock, cpu)->work,
6551 drain_local_stock);
6552
6553 for_each_node(node) {
6554 struct mem_cgroup_tree_per_node *rtpn;
Johannes Weiner95a045f2015-02-11 15:26:33 -08006555
6556 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL,
6557 node_online(node) ? node : NUMA_NO_NODE);
6558
Mel Gormanef8f2322016-07-28 15:46:05 -07006559 rtpn->rb_root = RB_ROOT;
Davidlohr Buesofa90b2f2017-09-08 16:15:21 -07006560 rtpn->rb_rightmost = NULL;
Mel Gormanef8f2322016-07-28 15:46:05 -07006561 spin_lock_init(&rtpn->lock);
Johannes Weiner95a045f2015-02-11 15:26:33 -08006562 soft_limit_tree.rb_tree_per_node[node] = rtpn;
6563 }
6564
Michal Hocko2d110852013-02-22 16:34:43 -08006565 return 0;
6566}
6567subsys_initcall(mem_cgroup_init);
Johannes Weiner21afa382015-02-11 15:26:36 -08006568
6569#ifdef CONFIG_MEMCG_SWAP
Arnd Bergmann358c07f2016-08-25 15:17:08 -07006570static struct mem_cgroup *mem_cgroup_id_get_online(struct mem_cgroup *memcg)
6571{
Kirill Tkhai1c2d4792018-10-26 15:09:28 -07006572 while (!refcount_inc_not_zero(&memcg->id.ref)) {
Arnd Bergmann358c07f2016-08-25 15:17:08 -07006573 /*
6574 * The root cgroup cannot be destroyed, so it's refcount must
6575 * always be >= 1.
6576 */
6577 if (WARN_ON_ONCE(memcg == root_mem_cgroup)) {
6578 VM_BUG_ON(1);
6579 break;
6580 }
6581 memcg = parent_mem_cgroup(memcg);
6582 if (!memcg)
6583 memcg = root_mem_cgroup;
6584 }
6585 return memcg;
6586}
6587
Johannes Weiner21afa382015-02-11 15:26:36 -08006588/**
6589 * mem_cgroup_swapout - transfer a memsw charge to swap
6590 * @page: page whose memsw charge to transfer
6591 * @entry: swap entry to move the charge to
6592 *
6593 * Transfer the memsw charge of @page to @entry.
6594 */
6595void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
6596{
Vladimir Davydov1f47b612016-08-11 15:33:00 -07006597 struct mem_cgroup *memcg, *swap_memcg;
Huang Yingd6810d72017-09-06 16:22:45 -07006598 unsigned int nr_entries;
Johannes Weiner21afa382015-02-11 15:26:36 -08006599 unsigned short oldid;
6600
6601 VM_BUG_ON_PAGE(PageLRU(page), page);
6602 VM_BUG_ON_PAGE(page_count(page), page);
6603
Johannes Weiner7941d212016-01-14 15:21:23 -08006604 if (!do_memsw_account())
Johannes Weiner21afa382015-02-11 15:26:36 -08006605 return;
6606
6607 memcg = page->mem_cgroup;
6608
6609 /* Readahead page, never charged */
6610 if (!memcg)
6611 return;
6612
Vladimir Davydov1f47b612016-08-11 15:33:00 -07006613 /*
6614 * In case the memcg owning these pages has been offlined and doesn't
6615 * have an ID allocated to it anymore, charge the closest online
6616 * ancestor for the swap instead and transfer the memory+swap charge.
6617 */
6618 swap_memcg = mem_cgroup_id_get_online(memcg);
Huang Yingd6810d72017-09-06 16:22:45 -07006619 nr_entries = hpage_nr_pages(page);
6620 /* Get references for the tail pages, too */
6621 if (nr_entries > 1)
6622 mem_cgroup_id_get_many(swap_memcg, nr_entries - 1);
6623 oldid = swap_cgroup_record(entry, mem_cgroup_id(swap_memcg),
6624 nr_entries);
Johannes Weiner21afa382015-02-11 15:26:36 -08006625 VM_BUG_ON_PAGE(oldid, page);
Johannes Weinerc9019e92018-01-31 16:16:37 -08006626 mod_memcg_state(swap_memcg, MEMCG_SWAP, nr_entries);
Johannes Weiner21afa382015-02-11 15:26:36 -08006627
6628 page->mem_cgroup = NULL;
6629
6630 if (!mem_cgroup_is_root(memcg))
Huang Yingd6810d72017-09-06 16:22:45 -07006631 page_counter_uncharge(&memcg->memory, nr_entries);
Johannes Weiner21afa382015-02-11 15:26:36 -08006632
Vladimir Davydov1f47b612016-08-11 15:33:00 -07006633 if (memcg != swap_memcg) {
6634 if (!mem_cgroup_is_root(swap_memcg))
Huang Yingd6810d72017-09-06 16:22:45 -07006635 page_counter_charge(&swap_memcg->memsw, nr_entries);
6636 page_counter_uncharge(&memcg->memsw, nr_entries);
Vladimir Davydov1f47b612016-08-11 15:33:00 -07006637 }
6638
Sebastian Andrzej Siewiorce9ce662015-09-04 15:47:50 -07006639 /*
6640 * Interrupts should be disabled here because the caller holds the
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07006641 * i_pages lock which is taken with interrupts-off. It is
Sebastian Andrzej Siewiorce9ce662015-09-04 15:47:50 -07006642 * important here to have the interrupts disabled because it is the
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07006643 * only synchronisation we have for updating the per-CPU variables.
Sebastian Andrzej Siewiorce9ce662015-09-04 15:47:50 -07006644 */
6645 VM_BUG_ON(!irqs_disabled());
Huang Yingd6810d72017-09-06 16:22:45 -07006646 mem_cgroup_charge_statistics(memcg, page, PageTransHuge(page),
6647 -nr_entries);
Johannes Weiner21afa382015-02-11 15:26:36 -08006648 memcg_check_events(memcg, page);
Johannes Weiner73f576c2016-07-20 15:44:57 -07006649
6650 if (!mem_cgroup_is_root(memcg))
Shakeel Buttd08afa12017-11-29 16:11:15 -08006651 css_put_many(&memcg->css, nr_entries);
Johannes Weiner21afa382015-02-11 15:26:36 -08006652}
6653
Huang Ying38d8b4e2017-07-06 15:37:18 -07006654/**
6655 * mem_cgroup_try_charge_swap - try charging swap space for a page
Vladimir Davydov37e84352016-01-20 15:02:56 -08006656 * @page: page being added to swap
6657 * @entry: swap entry to charge
6658 *
Huang Ying38d8b4e2017-07-06 15:37:18 -07006659 * Try to charge @page's memcg for the swap space at @entry.
Vladimir Davydov37e84352016-01-20 15:02:56 -08006660 *
6661 * Returns 0 on success, -ENOMEM on failure.
6662 */
6663int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry)
6664{
Huang Ying38d8b4e2017-07-06 15:37:18 -07006665 unsigned int nr_pages = hpage_nr_pages(page);
Vladimir Davydov37e84352016-01-20 15:02:56 -08006666 struct page_counter *counter;
Huang Ying38d8b4e2017-07-06 15:37:18 -07006667 struct mem_cgroup *memcg;
Vladimir Davydov37e84352016-01-20 15:02:56 -08006668 unsigned short oldid;
6669
6670 if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) || !do_swap_account)
6671 return 0;
6672
6673 memcg = page->mem_cgroup;
6674
6675 /* Readahead page, never charged */
6676 if (!memcg)
6677 return 0;
6678
Tejun Heof3a53a32018-06-07 17:05:35 -07006679 if (!entry.val) {
6680 memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
Tejun Heobb98f2c2018-06-07 17:05:31 -07006681 return 0;
Tejun Heof3a53a32018-06-07 17:05:35 -07006682 }
Tejun Heobb98f2c2018-06-07 17:05:31 -07006683
Vladimir Davydov1f47b612016-08-11 15:33:00 -07006684 memcg = mem_cgroup_id_get_online(memcg);
Vladimir Davydov37e84352016-01-20 15:02:56 -08006685
Vladimir Davydov1f47b612016-08-11 15:33:00 -07006686 if (!mem_cgroup_is_root(memcg) &&
Huang Ying38d8b4e2017-07-06 15:37:18 -07006687 !page_counter_try_charge(&memcg->swap, nr_pages, &counter)) {
Tejun Heof3a53a32018-06-07 17:05:35 -07006688 memcg_memory_event(memcg, MEMCG_SWAP_MAX);
6689 memcg_memory_event(memcg, MEMCG_SWAP_FAIL);
Vladimir Davydov1f47b612016-08-11 15:33:00 -07006690 mem_cgroup_id_put(memcg);
6691 return -ENOMEM;
6692 }
6693
Huang Ying38d8b4e2017-07-06 15:37:18 -07006694 /* Get references for the tail pages, too */
6695 if (nr_pages > 1)
6696 mem_cgroup_id_get_many(memcg, nr_pages - 1);
6697 oldid = swap_cgroup_record(entry, mem_cgroup_id(memcg), nr_pages);
Vladimir Davydov37e84352016-01-20 15:02:56 -08006698 VM_BUG_ON_PAGE(oldid, page);
Johannes Weinerc9019e92018-01-31 16:16:37 -08006699 mod_memcg_state(memcg, MEMCG_SWAP, nr_pages);
Vladimir Davydov37e84352016-01-20 15:02:56 -08006700
Vladimir Davydov37e84352016-01-20 15:02:56 -08006701 return 0;
6702}
6703
Johannes Weiner21afa382015-02-11 15:26:36 -08006704/**
Huang Ying38d8b4e2017-07-06 15:37:18 -07006705 * mem_cgroup_uncharge_swap - uncharge swap space
Johannes Weiner21afa382015-02-11 15:26:36 -08006706 * @entry: swap entry to uncharge
Huang Ying38d8b4e2017-07-06 15:37:18 -07006707 * @nr_pages: the amount of swap space to uncharge
Johannes Weiner21afa382015-02-11 15:26:36 -08006708 */
Huang Ying38d8b4e2017-07-06 15:37:18 -07006709void mem_cgroup_uncharge_swap(swp_entry_t entry, unsigned int nr_pages)
Johannes Weiner21afa382015-02-11 15:26:36 -08006710{
6711 struct mem_cgroup *memcg;
6712 unsigned short id;
6713
Vladimir Davydov37e84352016-01-20 15:02:56 -08006714 if (!do_swap_account)
Johannes Weiner21afa382015-02-11 15:26:36 -08006715 return;
6716
Huang Ying38d8b4e2017-07-06 15:37:18 -07006717 id = swap_cgroup_record(entry, 0, nr_pages);
Johannes Weiner21afa382015-02-11 15:26:36 -08006718 rcu_read_lock();
Vladimir Davydovadbe4272015-04-15 16:13:00 -07006719 memcg = mem_cgroup_from_id(id);
Johannes Weiner21afa382015-02-11 15:26:36 -08006720 if (memcg) {
Vladimir Davydov37e84352016-01-20 15:02:56 -08006721 if (!mem_cgroup_is_root(memcg)) {
6722 if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
Huang Ying38d8b4e2017-07-06 15:37:18 -07006723 page_counter_uncharge(&memcg->swap, nr_pages);
Vladimir Davydov37e84352016-01-20 15:02:56 -08006724 else
Huang Ying38d8b4e2017-07-06 15:37:18 -07006725 page_counter_uncharge(&memcg->memsw, nr_pages);
Vladimir Davydov37e84352016-01-20 15:02:56 -08006726 }
Johannes Weinerc9019e92018-01-31 16:16:37 -08006727 mod_memcg_state(memcg, MEMCG_SWAP, -nr_pages);
Huang Ying38d8b4e2017-07-06 15:37:18 -07006728 mem_cgroup_id_put_many(memcg, nr_pages);
Johannes Weiner21afa382015-02-11 15:26:36 -08006729 }
6730 rcu_read_unlock();
6731}
6732
Vladimir Davydovd8b38432016-01-20 15:03:07 -08006733long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg)
6734{
6735 long nr_swap_pages = get_nr_swap_pages();
6736
6737 if (!do_swap_account || !cgroup_subsys_on_dfl(memory_cgrp_subsys))
6738 return nr_swap_pages;
6739 for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg))
6740 nr_swap_pages = min_t(long, nr_swap_pages,
Roman Gushchinbbec2e12018-06-07 17:06:18 -07006741 READ_ONCE(memcg->swap.max) -
Vladimir Davydovd8b38432016-01-20 15:03:07 -08006742 page_counter_read(&memcg->swap));
6743 return nr_swap_pages;
6744}
6745
Vladimir Davydov5ccc5ab2016-01-20 15:03:10 -08006746bool mem_cgroup_swap_full(struct page *page)
6747{
6748 struct mem_cgroup *memcg;
6749
6750 VM_BUG_ON_PAGE(!PageLocked(page), page);
6751
6752 if (vm_swap_full())
6753 return true;
6754 if (!do_swap_account || !cgroup_subsys_on_dfl(memory_cgrp_subsys))
6755 return false;
6756
6757 memcg = page->mem_cgroup;
6758 if (!memcg)
6759 return false;
6760
6761 for (; memcg != root_mem_cgroup; memcg = parent_mem_cgroup(memcg))
Roman Gushchinbbec2e12018-06-07 17:06:18 -07006762 if (page_counter_read(&memcg->swap) * 2 >= memcg->swap.max)
Vladimir Davydov5ccc5ab2016-01-20 15:03:10 -08006763 return true;
6764
6765 return false;
6766}
6767
Johannes Weiner21afa382015-02-11 15:26:36 -08006768/* for remember boot option*/
6769#ifdef CONFIG_MEMCG_SWAP_ENABLED
6770static int really_do_swap_account __initdata = 1;
6771#else
6772static int really_do_swap_account __initdata;
6773#endif
6774
6775static int __init enable_swap_account(char *s)
6776{
6777 if (!strcmp(s, "1"))
6778 really_do_swap_account = 1;
6779 else if (!strcmp(s, "0"))
6780 really_do_swap_account = 0;
6781 return 1;
6782}
6783__setup("swapaccount=", enable_swap_account);
6784
Vladimir Davydov37e84352016-01-20 15:02:56 -08006785static u64 swap_current_read(struct cgroup_subsys_state *css,
6786 struct cftype *cft)
6787{
6788 struct mem_cgroup *memcg = mem_cgroup_from_css(css);
6789
6790 return (u64)page_counter_read(&memcg->swap) * PAGE_SIZE;
6791}
6792
6793static int swap_max_show(struct seq_file *m, void *v)
6794{
Chris Down677dc972019-03-05 15:45:55 -08006795 return seq_puts_memcg_tunable(m,
6796 READ_ONCE(mem_cgroup_from_seq(m)->swap.max));
Vladimir Davydov37e84352016-01-20 15:02:56 -08006797}
6798
6799static ssize_t swap_max_write(struct kernfs_open_file *of,
6800 char *buf, size_t nbytes, loff_t off)
6801{
6802 struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
6803 unsigned long max;
6804 int err;
6805
6806 buf = strstrip(buf);
6807 err = page_counter_memparse(buf, "max", &max);
6808 if (err)
6809 return err;
6810
Tejun Heobe091022018-06-07 17:09:21 -07006811 xchg(&memcg->swap.max, max);
Vladimir Davydov37e84352016-01-20 15:02:56 -08006812
6813 return nbytes;
6814}
6815
Tejun Heof3a53a32018-06-07 17:05:35 -07006816static int swap_events_show(struct seq_file *m, void *v)
6817{
Chris Downaa9694b2019-03-05 15:45:52 -08006818 struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
Tejun Heof3a53a32018-06-07 17:05:35 -07006819
6820 seq_printf(m, "max %lu\n",
6821 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_MAX]));
6822 seq_printf(m, "fail %lu\n",
6823 atomic_long_read(&memcg->memory_events[MEMCG_SWAP_FAIL]));
6824
6825 return 0;
6826}
6827
Vladimir Davydov37e84352016-01-20 15:02:56 -08006828static struct cftype swap_files[] = {
6829 {
6830 .name = "swap.current",
6831 .flags = CFTYPE_NOT_ON_ROOT,
6832 .read_u64 = swap_current_read,
6833 },
6834 {
6835 .name = "swap.max",
6836 .flags = CFTYPE_NOT_ON_ROOT,
6837 .seq_show = swap_max_show,
6838 .write = swap_max_write,
6839 },
Tejun Heof3a53a32018-06-07 17:05:35 -07006840 {
6841 .name = "swap.events",
6842 .flags = CFTYPE_NOT_ON_ROOT,
6843 .file_offset = offsetof(struct mem_cgroup, swap_events_file),
6844 .seq_show = swap_events_show,
6845 },
Vladimir Davydov37e84352016-01-20 15:02:56 -08006846 { } /* terminate */
6847};
6848
Johannes Weiner21afa382015-02-11 15:26:36 -08006849static struct cftype memsw_cgroup_files[] = {
6850 {
6851 .name = "memsw.usage_in_bytes",
6852 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
6853 .read_u64 = mem_cgroup_read_u64,
6854 },
6855 {
6856 .name = "memsw.max_usage_in_bytes",
6857 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
6858 .write = mem_cgroup_reset,
6859 .read_u64 = mem_cgroup_read_u64,
6860 },
6861 {
6862 .name = "memsw.limit_in_bytes",
6863 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
6864 .write = mem_cgroup_write,
6865 .read_u64 = mem_cgroup_read_u64,
6866 },
6867 {
6868 .name = "memsw.failcnt",
6869 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
6870 .write = mem_cgroup_reset,
6871 .read_u64 = mem_cgroup_read_u64,
6872 },
6873 { }, /* terminate */
6874};
6875
6876static int __init mem_cgroup_swap_init(void)
6877{
6878 if (!mem_cgroup_disabled() && really_do_swap_account) {
6879 do_swap_account = 1;
Vladimir Davydov37e84352016-01-20 15:02:56 -08006880 WARN_ON(cgroup_add_dfl_cftypes(&memory_cgrp_subsys,
6881 swap_files));
Johannes Weiner21afa382015-02-11 15:26:36 -08006882 WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys,
6883 memsw_cgroup_files));
6884 }
6885 return 0;
6886}
6887subsys_initcall(mem_cgroup_swap_init);
6888
6889#endif /* CONFIG_MEMCG_SWAP */