]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - mm/memcontrol.c
memcg: use static branches when code not in use
[mirror_ubuntu-artful-kernel.git] / mm / memcontrol.c
CommitLineData
8cdea7c0
BS
1/* memcontrol.c - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
78fb7466
PE
6 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
2e72b634
KS
9 * Memory thresholds
10 * Copyright (C) 2009 Nokia Corporation
11 * Author: Kirill A. Shutemov
12 *
7ae1e1d0
GC
13 * Kernel Memory Controller
14 * Copyright (C) 2012 Parallels Inc. and Google Inc.
15 * Authors: Glauber Costa and Suleiman Souhlal
16 *
8cdea7c0
BS
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 */
27
28#include <linux/res_counter.h>
29#include <linux/memcontrol.h>
30#include <linux/cgroup.h>
78fb7466 31#include <linux/mm.h>
4ffef5fe 32#include <linux/hugetlb.h>
d13d1443 33#include <linux/pagemap.h>
d52aa412 34#include <linux/smp.h>
8a9f3ccd 35#include <linux/page-flags.h>
66e1707b 36#include <linux/backing-dev.h>
8a9f3ccd
BS
37#include <linux/bit_spinlock.h>
38#include <linux/rcupdate.h>
e222432b 39#include <linux/limits.h>
b9e15baf 40#include <linux/export.h>
8c7c6e34 41#include <linux/mutex.h>
f64c3f54 42#include <linux/rbtree.h>
b6ac57d5 43#include <linux/slab.h>
66e1707b 44#include <linux/swap.h>
02491447 45#include <linux/swapops.h>
66e1707b 46#include <linux/spinlock.h>
2e72b634
KS
47#include <linux/eventfd.h>
48#include <linux/sort.h>
66e1707b 49#include <linux/fs.h>
d2ceb9b7 50#include <linux/seq_file.h>
33327948 51#include <linux/vmalloc.h>
b69408e8 52#include <linux/mm_inline.h>
52d4b9ac 53#include <linux/page_cgroup.h>
cdec2e42 54#include <linux/cpu.h>
158e0a2d 55#include <linux/oom.h>
08e552c6 56#include "internal.h"
d1a4c0b3 57#include <net/sock.h>
4bd2c1ee 58#include <net/ip.h>
d1a4c0b3 59#include <net/tcp_memcontrol.h>
8cdea7c0 60
8697d331
BS
61#include <asm/uaccess.h>
62
cc8e970c
KM
63#include <trace/events/vmscan.h>
64
a181b0e8 65struct cgroup_subsys mem_cgroup_subsys __read_mostly;
68ae564b
DR
66EXPORT_SYMBOL(mem_cgroup_subsys);
67
a181b0e8 68#define MEM_CGROUP_RECLAIM_RETRIES 5
6bbda35c 69static struct mem_cgroup *root_mem_cgroup __read_mostly;
8cdea7c0 70
c255a458 71#ifdef CONFIG_MEMCG_SWAP
338c8431 72/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
c077719b 73int do_swap_account __read_mostly;
a42c390c
MH
74
75/* for remember boot option*/
c255a458 76#ifdef CONFIG_MEMCG_SWAP_ENABLED
a42c390c
MH
77static int really_do_swap_account __initdata = 1;
78#else
79static int really_do_swap_account __initdata = 0;
80#endif
81
c077719b 82#else
a0db00fc 83#define do_swap_account 0
c077719b
KH
84#endif
85
86
d52aa412
KH
87/*
88 * Statistics for memory cgroup.
89 */
90enum mem_cgroup_stat_index {
91 /*
92 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
93 */
94 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
d69b042f 95 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
d8046582 96 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
bff6bb83 97 MEM_CGROUP_STAT_SWAP, /* # of pages, swapped out */
d52aa412
KH
98 MEM_CGROUP_STAT_NSTATS,
99};
100
af7c4b0e
JW
101static const char * const mem_cgroup_stat_names[] = {
102 "cache",
103 "rss",
104 "mapped_file",
105 "swap",
106};
107
e9f8974f
JW
108enum mem_cgroup_events_index {
109 MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */
110 MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */
456f998e
YH
111 MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */
112 MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */
e9f8974f
JW
113 MEM_CGROUP_EVENTS_NSTATS,
114};
af7c4b0e
JW
115
116static const char * const mem_cgroup_events_names[] = {
117 "pgpgin",
118 "pgpgout",
119 "pgfault",
120 "pgmajfault",
121};
122
7a159cc9
JW
123/*
124 * Per memcg event counter is incremented at every pagein/pageout. With THP,
125 * it will be incremated by the number of pages. This counter is used for
126 * for trigger some periodic events. This is straightforward and better
127 * than using jiffies etc. to handle periodic memcg event.
128 */
129enum mem_cgroup_events_target {
130 MEM_CGROUP_TARGET_THRESH,
131 MEM_CGROUP_TARGET_SOFTLIMIT,
453a9bf3 132 MEM_CGROUP_TARGET_NUMAINFO,
7a159cc9
JW
133 MEM_CGROUP_NTARGETS,
134};
a0db00fc
KS
135#define THRESHOLDS_EVENTS_TARGET 128
136#define SOFTLIMIT_EVENTS_TARGET 1024
137#define NUMAINFO_EVENTS_TARGET 1024
e9f8974f 138
d52aa412 139struct mem_cgroup_stat_cpu {
7a159cc9 140 long count[MEM_CGROUP_STAT_NSTATS];
e9f8974f 141 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
13114716 142 unsigned long nr_page_events;
7a159cc9 143 unsigned long targets[MEM_CGROUP_NTARGETS];
d52aa412
KH
144};
145
527a5ec9
JW
146struct mem_cgroup_reclaim_iter {
147 /* css_id of the last scanned hierarchy member */
148 int position;
149 /* scan generation, increased every round-trip */
150 unsigned int generation;
151};
152
6d12e2d8
KH
153/*
154 * per-zone information in memory controller.
155 */
6d12e2d8 156struct mem_cgroup_per_zone {
6290df54 157 struct lruvec lruvec;
1eb49272 158 unsigned long lru_size[NR_LRU_LISTS];
3e2f41f1 159
527a5ec9
JW
160 struct mem_cgroup_reclaim_iter reclaim_iter[DEF_PRIORITY + 1];
161
f64c3f54
BS
162 struct rb_node tree_node; /* RB tree node */
163 unsigned long long usage_in_excess;/* Set to the value by which */
164 /* the soft limit is exceeded*/
165 bool on_tree;
d79154bb 166 struct mem_cgroup *memcg; /* Back pointer, we cannot */
4e416953 167 /* use container_of */
6d12e2d8 168};
6d12e2d8
KH
169
170struct mem_cgroup_per_node {
171 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
172};
173
174struct mem_cgroup_lru_info {
175 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
176};
177
f64c3f54
BS
178/*
179 * Cgroups above their limits are maintained in a RB-Tree, independent of
180 * their hierarchy representation
181 */
182
183struct mem_cgroup_tree_per_zone {
184 struct rb_root rb_root;
185 spinlock_t lock;
186};
187
188struct mem_cgroup_tree_per_node {
189 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
190};
191
192struct mem_cgroup_tree {
193 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
194};
195
196static struct mem_cgroup_tree soft_limit_tree __read_mostly;
197
2e72b634
KS
198struct mem_cgroup_threshold {
199 struct eventfd_ctx *eventfd;
200 u64 threshold;
201};
202
9490ff27 203/* For threshold */
2e72b634 204struct mem_cgroup_threshold_ary {
748dad36 205 /* An array index points to threshold just below or equal to usage. */
5407a562 206 int current_threshold;
2e72b634
KS
207 /* Size of entries[] */
208 unsigned int size;
209 /* Array of thresholds */
210 struct mem_cgroup_threshold entries[0];
211};
2c488db2
KS
212
213struct mem_cgroup_thresholds {
214 /* Primary thresholds array */
215 struct mem_cgroup_threshold_ary *primary;
216 /*
217 * Spare threshold array.
218 * This is needed to make mem_cgroup_unregister_event() "never fail".
219 * It must be able to store at least primary->size - 1 entries.
220 */
221 struct mem_cgroup_threshold_ary *spare;
222};
223
9490ff27
KH
224/* for OOM */
225struct mem_cgroup_eventfd_list {
226 struct list_head list;
227 struct eventfd_ctx *eventfd;
228};
2e72b634 229
c0ff4b85
R
230static void mem_cgroup_threshold(struct mem_cgroup *memcg);
231static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
2e72b634 232
8cdea7c0
BS
233/*
234 * The memory controller data structure. The memory controller controls both
235 * page cache and RSS per cgroup. We would eventually like to provide
236 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
237 * to help the administrator determine what knobs to tune.
238 *
239 * TODO: Add a water mark for the memory controller. Reclaim will begin when
8a9f3ccd
BS
240 * we hit the water mark. May be even add a low water mark, such that
241 * no reclaim occurs from a cgroup at it's low water mark, this is
242 * a feature that will be implemented much later in the future.
8cdea7c0
BS
243 */
244struct mem_cgroup {
245 struct cgroup_subsys_state css;
246 /*
247 * the counter to account for memory usage
248 */
249 struct res_counter res;
59927fb9
HD
250
251 union {
252 /*
253 * the counter to account for mem+swap usage.
254 */
255 struct res_counter memsw;
256
257 /*
258 * rcu_freeing is used only when freeing struct mem_cgroup,
259 * so put it into a union to avoid wasting more memory.
260 * It must be disjoint from the css field. It could be
261 * in a union with the res field, but res plays a much
262 * larger part in mem_cgroup life than memsw, and might
263 * be of interest, even at time of free, when debugging.
264 * So share rcu_head with the less interesting memsw.
265 */
266 struct rcu_head rcu_freeing;
267 /*
3afe36b1
GC
268 * We also need some space for a worker in deferred freeing.
269 * By the time we call it, rcu_freeing is no longer in use.
59927fb9
HD
270 */
271 struct work_struct work_freeing;
272 };
273
510fc4e1
GC
274 /*
275 * the counter to account for kernel memory usage.
276 */
277 struct res_counter kmem;
78fb7466
PE
278 /*
279 * Per cgroup active and inactive list, similar to the
280 * per zone LRU lists.
78fb7466 281 */
6d12e2d8 282 struct mem_cgroup_lru_info info;
889976db
YH
283 int last_scanned_node;
284#if MAX_NUMNODES > 1
285 nodemask_t scan_nodes;
453a9bf3
KH
286 atomic_t numainfo_events;
287 atomic_t numainfo_updating;
889976db 288#endif
18f59ea7
BS
289 /*
290 * Should the accounting and control be hierarchical, per subtree?
291 */
292 bool use_hierarchy;
510fc4e1 293 unsigned long kmem_account_flags; /* See KMEM_ACCOUNTED_*, below */
79dfdacc
MH
294
295 bool oom_lock;
296 atomic_t under_oom;
297
8c7c6e34 298 atomic_t refcnt;
14797e23 299
1f4c025b 300 int swappiness;
3c11ecf4
KH
301 /* OOM-Killer disable */
302 int oom_kill_disable;
a7885eb8 303
22a668d7
KH
304 /* set when res.limit == memsw.limit */
305 bool memsw_is_minimum;
306
2e72b634
KS
307 /* protect arrays of thresholds */
308 struct mutex thresholds_lock;
309
310 /* thresholds for memory usage. RCU-protected */
2c488db2 311 struct mem_cgroup_thresholds thresholds;
907860ed 312
2e72b634 313 /* thresholds for mem+swap usage. RCU-protected */
2c488db2 314 struct mem_cgroup_thresholds memsw_thresholds;
907860ed 315
9490ff27
KH
316 /* For oom notifier event fd */
317 struct list_head oom_notify;
185efc0f 318
7dc74be0
DN
319 /*
320 * Should we move charges of a task when a task is moved into this
321 * mem_cgroup ? And what type of charges should we move ?
322 */
323 unsigned long move_charge_at_immigrate;
619d094b
KH
324 /*
325 * set > 0 if pages under this cgroup are moving to other cgroup.
326 */
327 atomic_t moving_account;
312734c0
KH
328 /* taken only while moving_account > 0 */
329 spinlock_t move_lock;
d52aa412 330 /*
c62b1a3b 331 * percpu counter.
d52aa412 332 */
3a7951b4 333 struct mem_cgroup_stat_cpu __percpu *stat;
711d3d2c
KH
334 /*
335 * used when a cpu is offlined or other synchronizations
336 * See mem_cgroup_read_stat().
337 */
338 struct mem_cgroup_stat_cpu nocpu_base;
339 spinlock_t pcp_counter_lock;
d1a4c0b3 340
4bd2c1ee 341#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
d1a4c0b3
GC
342 struct tcp_memcontrol tcp_mem;
343#endif
8cdea7c0
BS
344};
345
510fc4e1
GC
346/* internal only representation about the status of kmem accounting. */
347enum {
348 KMEM_ACCOUNTED_ACTIVE = 0, /* accounted by this cgroup itself */
a8964b9b 349 KMEM_ACCOUNTED_ACTIVATED, /* static key enabled. */
7de37682 350 KMEM_ACCOUNTED_DEAD, /* dead memcg with pending kmem charges */
510fc4e1
GC
351};
352
a8964b9b
GC
353/* We account when limit is on, but only after call sites are patched */
354#define KMEM_ACCOUNTED_MASK \
355 ((1 << KMEM_ACCOUNTED_ACTIVE) | (1 << KMEM_ACCOUNTED_ACTIVATED))
510fc4e1
GC
356
357#ifdef CONFIG_MEMCG_KMEM
358static inline void memcg_kmem_set_active(struct mem_cgroup *memcg)
359{
360 set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
361}
7de37682
GC
362
363static bool memcg_kmem_is_active(struct mem_cgroup *memcg)
364{
365 return test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
366}
367
a8964b9b
GC
368static void memcg_kmem_set_activated(struct mem_cgroup *memcg)
369{
370 set_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
371}
372
7de37682
GC
373static void memcg_kmem_mark_dead(struct mem_cgroup *memcg)
374{
375 if (test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags))
376 set_bit(KMEM_ACCOUNTED_DEAD, &memcg->kmem_account_flags);
377}
378
379static bool memcg_kmem_test_and_clear_dead(struct mem_cgroup *memcg)
380{
381 return test_and_clear_bit(KMEM_ACCOUNTED_DEAD,
382 &memcg->kmem_account_flags);
383}
510fc4e1
GC
384#endif
385
7dc74be0
DN
386/* Stuffs for move charges at task migration. */
387/*
388 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
389 * left-shifted bitmap of these types.
390 */
391enum move_type {
4ffef5fe 392 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
87946a72 393 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
7dc74be0
DN
394 NR_MOVE_TYPE,
395};
396
4ffef5fe
DN
397/* "mc" and its members are protected by cgroup_mutex */
398static struct move_charge_struct {
b1dd693e 399 spinlock_t lock; /* for from, to */
4ffef5fe
DN
400 struct mem_cgroup *from;
401 struct mem_cgroup *to;
402 unsigned long precharge;
854ffa8d 403 unsigned long moved_charge;
483c30b5 404 unsigned long moved_swap;
8033b97c
DN
405 struct task_struct *moving_task; /* a task moving charges */
406 wait_queue_head_t waitq; /* a waitq for other context */
407} mc = {
2bd9bb20 408 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
8033b97c
DN
409 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
410};
4ffef5fe 411
90254a65
DN
412static bool move_anon(void)
413{
414 return test_bit(MOVE_CHARGE_TYPE_ANON,
415 &mc.to->move_charge_at_immigrate);
416}
417
87946a72
DN
418static bool move_file(void)
419{
420 return test_bit(MOVE_CHARGE_TYPE_FILE,
421 &mc.to->move_charge_at_immigrate);
422}
423
4e416953
BS
424/*
425 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
426 * limit reclaim to prevent infinite loops, if they ever occur.
427 */
a0db00fc
KS
428#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
429#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
4e416953 430
217bc319
KH
431enum charge_type {
432 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
41326c17 433 MEM_CGROUP_CHARGE_TYPE_ANON,
d13d1443 434 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
8a9478ca 435 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
c05555b5
KH
436 NR_CHARGE_TYPE,
437};
438
8c7c6e34 439/* for encoding cft->private value on file */
86ae53e1
GC
440enum res_type {
441 _MEM,
442 _MEMSWAP,
443 _OOM_TYPE,
510fc4e1 444 _KMEM,
86ae53e1
GC
445};
446
a0db00fc
KS
447#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
448#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
8c7c6e34 449#define MEMFILE_ATTR(val) ((val) & 0xffff)
9490ff27
KH
450/* Used for OOM nofiier */
451#define OOM_CONTROL (0)
8c7c6e34 452
75822b44
BS
453/*
454 * Reclaim flags for mem_cgroup_hierarchical_reclaim
455 */
456#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
457#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
458#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
459#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
460
c0ff4b85
R
461static void mem_cgroup_get(struct mem_cgroup *memcg);
462static void mem_cgroup_put(struct mem_cgroup *memcg);
e1aab161 463
b2145145
WL
464static inline
465struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
466{
467 return container_of(s, struct mem_cgroup, css);
468}
469
7ffc0edc
MH
470static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
471{
472 return (memcg == root_mem_cgroup);
473}
474
e1aab161 475/* Writing them here to avoid exposing memcg's inner layout */
4bd2c1ee 476#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
e1aab161 477
e1aab161
GC
478void sock_update_memcg(struct sock *sk)
479{
376be5ff 480 if (mem_cgroup_sockets_enabled) {
e1aab161 481 struct mem_cgroup *memcg;
3f134619 482 struct cg_proto *cg_proto;
e1aab161
GC
483
484 BUG_ON(!sk->sk_prot->proto_cgroup);
485
f3f511e1
GC
486 /* Socket cloning can throw us here with sk_cgrp already
487 * filled. It won't however, necessarily happen from
488 * process context. So the test for root memcg given
489 * the current task's memcg won't help us in this case.
490 *
491 * Respecting the original socket's memcg is a better
492 * decision in this case.
493 */
494 if (sk->sk_cgrp) {
495 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
496 mem_cgroup_get(sk->sk_cgrp->memcg);
497 return;
498 }
499
e1aab161
GC
500 rcu_read_lock();
501 memcg = mem_cgroup_from_task(current);
3f134619
GC
502 cg_proto = sk->sk_prot->proto_cgroup(memcg);
503 if (!mem_cgroup_is_root(memcg) && memcg_proto_active(cg_proto)) {
e1aab161 504 mem_cgroup_get(memcg);
3f134619 505 sk->sk_cgrp = cg_proto;
e1aab161
GC
506 }
507 rcu_read_unlock();
508 }
509}
510EXPORT_SYMBOL(sock_update_memcg);
511
512void sock_release_memcg(struct sock *sk)
513{
376be5ff 514 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
e1aab161
GC
515 struct mem_cgroup *memcg;
516 WARN_ON(!sk->sk_cgrp->memcg);
517 memcg = sk->sk_cgrp->memcg;
518 mem_cgroup_put(memcg);
519 }
520}
d1a4c0b3
GC
521
522struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
523{
524 if (!memcg || mem_cgroup_is_root(memcg))
525 return NULL;
526
527 return &memcg->tcp_mem.cg_proto;
528}
529EXPORT_SYMBOL(tcp_proto_cgroup);
e1aab161 530
3f134619
GC
531static void disarm_sock_keys(struct mem_cgroup *memcg)
532{
533 if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto))
534 return;
535 static_key_slow_dec(&memcg_socket_limit_enabled);
536}
537#else
538static void disarm_sock_keys(struct mem_cgroup *memcg)
539{
540}
541#endif
542
a8964b9b
GC
543#ifdef CONFIG_MEMCG_KMEM
544struct static_key memcg_kmem_enabled_key;
545
546static void disarm_kmem_keys(struct mem_cgroup *memcg)
547{
548 if (memcg_kmem_is_active(memcg))
549 static_key_slow_dec(&memcg_kmem_enabled_key);
550}
551#else
552static void disarm_kmem_keys(struct mem_cgroup *memcg)
553{
554}
555#endif /* CONFIG_MEMCG_KMEM */
556
557static void disarm_static_keys(struct mem_cgroup *memcg)
558{
559 disarm_sock_keys(memcg);
560 disarm_kmem_keys(memcg);
561}
562
c0ff4b85 563static void drain_all_stock_async(struct mem_cgroup *memcg);
8c7c6e34 564
f64c3f54 565static struct mem_cgroup_per_zone *
c0ff4b85 566mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
f64c3f54 567{
c0ff4b85 568 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
f64c3f54
BS
569}
570
c0ff4b85 571struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
d324236b 572{
c0ff4b85 573 return &memcg->css;
d324236b
WF
574}
575
f64c3f54 576static struct mem_cgroup_per_zone *
c0ff4b85 577page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
f64c3f54 578{
97a6c37b
JW
579 int nid = page_to_nid(page);
580 int zid = page_zonenum(page);
f64c3f54 581
c0ff4b85 582 return mem_cgroup_zoneinfo(memcg, nid, zid);
f64c3f54
BS
583}
584
585static struct mem_cgroup_tree_per_zone *
586soft_limit_tree_node_zone(int nid, int zid)
587{
588 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
589}
590
591static struct mem_cgroup_tree_per_zone *
592soft_limit_tree_from_page(struct page *page)
593{
594 int nid = page_to_nid(page);
595 int zid = page_zonenum(page);
596
597 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
598}
599
600static void
c0ff4b85 601__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
f64c3f54 602 struct mem_cgroup_per_zone *mz,
ef8745c1
KH
603 struct mem_cgroup_tree_per_zone *mctz,
604 unsigned long long new_usage_in_excess)
f64c3f54
BS
605{
606 struct rb_node **p = &mctz->rb_root.rb_node;
607 struct rb_node *parent = NULL;
608 struct mem_cgroup_per_zone *mz_node;
609
610 if (mz->on_tree)
611 return;
612
ef8745c1
KH
613 mz->usage_in_excess = new_usage_in_excess;
614 if (!mz->usage_in_excess)
615 return;
f64c3f54
BS
616 while (*p) {
617 parent = *p;
618 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
619 tree_node);
620 if (mz->usage_in_excess < mz_node->usage_in_excess)
621 p = &(*p)->rb_left;
622 /*
623 * We can't avoid mem cgroups that are over their soft
624 * limit by the same amount
625 */
626 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
627 p = &(*p)->rb_right;
628 }
629 rb_link_node(&mz->tree_node, parent, p);
630 rb_insert_color(&mz->tree_node, &mctz->rb_root);
631 mz->on_tree = true;
4e416953
BS
632}
633
634static void
c0ff4b85 635__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
4e416953
BS
636 struct mem_cgroup_per_zone *mz,
637 struct mem_cgroup_tree_per_zone *mctz)
638{
639 if (!mz->on_tree)
640 return;
641 rb_erase(&mz->tree_node, &mctz->rb_root);
642 mz->on_tree = false;
643}
644
f64c3f54 645static void
c0ff4b85 646mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
f64c3f54
BS
647 struct mem_cgroup_per_zone *mz,
648 struct mem_cgroup_tree_per_zone *mctz)
649{
650 spin_lock(&mctz->lock);
c0ff4b85 651 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
f64c3f54
BS
652 spin_unlock(&mctz->lock);
653}
654
f64c3f54 655
c0ff4b85 656static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
f64c3f54 657{
ef8745c1 658 unsigned long long excess;
f64c3f54
BS
659 struct mem_cgroup_per_zone *mz;
660 struct mem_cgroup_tree_per_zone *mctz;
4e649152
KH
661 int nid = page_to_nid(page);
662 int zid = page_zonenum(page);
f64c3f54
BS
663 mctz = soft_limit_tree_from_page(page);
664
665 /*
4e649152
KH
666 * Necessary to update all ancestors when hierarchy is used.
667 * because their event counter is not touched.
f64c3f54 668 */
c0ff4b85
R
669 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
670 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
671 excess = res_counter_soft_limit_excess(&memcg->res);
4e649152
KH
672 /*
673 * We have to update the tree if mz is on RB-tree or
674 * mem is over its softlimit.
675 */
ef8745c1 676 if (excess || mz->on_tree) {
4e649152
KH
677 spin_lock(&mctz->lock);
678 /* if on-tree, remove it */
679 if (mz->on_tree)
c0ff4b85 680 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
4e649152 681 /*
ef8745c1
KH
682 * Insert again. mz->usage_in_excess will be updated.
683 * If excess is 0, no tree ops.
4e649152 684 */
c0ff4b85 685 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
4e649152
KH
686 spin_unlock(&mctz->lock);
687 }
f64c3f54
BS
688 }
689}
690
c0ff4b85 691static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
f64c3f54
BS
692{
693 int node, zone;
694 struct mem_cgroup_per_zone *mz;
695 struct mem_cgroup_tree_per_zone *mctz;
696
3ed28fa1 697 for_each_node(node) {
f64c3f54 698 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
c0ff4b85 699 mz = mem_cgroup_zoneinfo(memcg, node, zone);
f64c3f54 700 mctz = soft_limit_tree_node_zone(node, zone);
c0ff4b85 701 mem_cgroup_remove_exceeded(memcg, mz, mctz);
f64c3f54
BS
702 }
703 }
704}
705
4e416953
BS
706static struct mem_cgroup_per_zone *
707__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
708{
709 struct rb_node *rightmost = NULL;
26251eaf 710 struct mem_cgroup_per_zone *mz;
4e416953
BS
711
712retry:
26251eaf 713 mz = NULL;
4e416953
BS
714 rightmost = rb_last(&mctz->rb_root);
715 if (!rightmost)
716 goto done; /* Nothing to reclaim from */
717
718 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
719 /*
720 * Remove the node now but someone else can add it back,
721 * we will to add it back at the end of reclaim to its correct
722 * position in the tree.
723 */
d79154bb
HD
724 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
725 if (!res_counter_soft_limit_excess(&mz->memcg->res) ||
726 !css_tryget(&mz->memcg->css))
4e416953
BS
727 goto retry;
728done:
729 return mz;
730}
731
732static struct mem_cgroup_per_zone *
733mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
734{
735 struct mem_cgroup_per_zone *mz;
736
737 spin_lock(&mctz->lock);
738 mz = __mem_cgroup_largest_soft_limit_node(mctz);
739 spin_unlock(&mctz->lock);
740 return mz;
741}
742
711d3d2c
KH
743/*
744 * Implementation Note: reading percpu statistics for memcg.
745 *
746 * Both of vmstat[] and percpu_counter has threshold and do periodic
747 * synchronization to implement "quick" read. There are trade-off between
748 * reading cost and precision of value. Then, we may have a chance to implement
749 * a periodic synchronizion of counter in memcg's counter.
750 *
751 * But this _read() function is used for user interface now. The user accounts
752 * memory usage by memory cgroup and he _always_ requires exact value because
753 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
754 * have to visit all online cpus and make sum. So, for now, unnecessary
755 * synchronization is not implemented. (just implemented for cpu hotplug)
756 *
757 * If there are kernel internal actions which can make use of some not-exact
758 * value, and reading all cpu value can be performance bottleneck in some
759 * common workload, threashold and synchonization as vmstat[] should be
760 * implemented.
761 */
c0ff4b85 762static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
7a159cc9 763 enum mem_cgroup_stat_index idx)
c62b1a3b 764{
7a159cc9 765 long val = 0;
c62b1a3b 766 int cpu;
c62b1a3b 767
711d3d2c
KH
768 get_online_cpus();
769 for_each_online_cpu(cpu)
c0ff4b85 770 val += per_cpu(memcg->stat->count[idx], cpu);
711d3d2c 771#ifdef CONFIG_HOTPLUG_CPU
c0ff4b85
R
772 spin_lock(&memcg->pcp_counter_lock);
773 val += memcg->nocpu_base.count[idx];
774 spin_unlock(&memcg->pcp_counter_lock);
711d3d2c
KH
775#endif
776 put_online_cpus();
c62b1a3b
KH
777 return val;
778}
779
c0ff4b85 780static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
0c3e73e8
BS
781 bool charge)
782{
783 int val = (charge) ? 1 : -1;
bff6bb83 784 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
0c3e73e8
BS
785}
786
c0ff4b85 787static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
e9f8974f
JW
788 enum mem_cgroup_events_index idx)
789{
790 unsigned long val = 0;
791 int cpu;
792
793 for_each_online_cpu(cpu)
c0ff4b85 794 val += per_cpu(memcg->stat->events[idx], cpu);
e9f8974f 795#ifdef CONFIG_HOTPLUG_CPU
c0ff4b85
R
796 spin_lock(&memcg->pcp_counter_lock);
797 val += memcg->nocpu_base.events[idx];
798 spin_unlock(&memcg->pcp_counter_lock);
e9f8974f
JW
799#endif
800 return val;
801}
802
c0ff4b85 803static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
b2402857 804 bool anon, int nr_pages)
d52aa412 805{
c62b1a3b
KH
806 preempt_disable();
807
b2402857
KH
808 /*
809 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
810 * counted as CACHE even if it's on ANON LRU.
811 */
812 if (anon)
813 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
c0ff4b85 814 nr_pages);
d52aa412 815 else
b2402857 816 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
c0ff4b85 817 nr_pages);
55e462b0 818
e401f176
KH
819 /* pagein of a big page is an event. So, ignore page size */
820 if (nr_pages > 0)
c0ff4b85 821 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
3751d604 822 else {
c0ff4b85 823 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
3751d604
KH
824 nr_pages = -nr_pages; /* for event */
825 }
e401f176 826
13114716 827 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
2e72b634 828
c62b1a3b 829 preempt_enable();
6d12e2d8
KH
830}
831
bb2a0de9 832unsigned long
4d7dcca2 833mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
074291fe
KK
834{
835 struct mem_cgroup_per_zone *mz;
836
837 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
838 return mz->lru_size[lru];
839}
840
841static unsigned long
c0ff4b85 842mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
bb2a0de9 843 unsigned int lru_mask)
889976db
YH
844{
845 struct mem_cgroup_per_zone *mz;
f156ab93 846 enum lru_list lru;
bb2a0de9
KH
847 unsigned long ret = 0;
848
c0ff4b85 849 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
bb2a0de9 850
f156ab93
HD
851 for_each_lru(lru) {
852 if (BIT(lru) & lru_mask)
853 ret += mz->lru_size[lru];
bb2a0de9
KH
854 }
855 return ret;
856}
857
858static unsigned long
c0ff4b85 859mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
bb2a0de9
KH
860 int nid, unsigned int lru_mask)
861{
889976db
YH
862 u64 total = 0;
863 int zid;
864
bb2a0de9 865 for (zid = 0; zid < MAX_NR_ZONES; zid++)
c0ff4b85
R
866 total += mem_cgroup_zone_nr_lru_pages(memcg,
867 nid, zid, lru_mask);
bb2a0de9 868
889976db
YH
869 return total;
870}
bb2a0de9 871
c0ff4b85 872static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
bb2a0de9 873 unsigned int lru_mask)
6d12e2d8 874{
889976db 875 int nid;
6d12e2d8
KH
876 u64 total = 0;
877
31aaea4a 878 for_each_node_state(nid, N_MEMORY)
c0ff4b85 879 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
6d12e2d8 880 return total;
d52aa412
KH
881}
882
f53d7ce3
JW
883static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
884 enum mem_cgroup_events_target target)
7a159cc9
JW
885{
886 unsigned long val, next;
887
13114716 888 val = __this_cpu_read(memcg->stat->nr_page_events);
4799401f 889 next = __this_cpu_read(memcg->stat->targets[target]);
7a159cc9 890 /* from time_after() in jiffies.h */
f53d7ce3
JW
891 if ((long)next - (long)val < 0) {
892 switch (target) {
893 case MEM_CGROUP_TARGET_THRESH:
894 next = val + THRESHOLDS_EVENTS_TARGET;
895 break;
896 case MEM_CGROUP_TARGET_SOFTLIMIT:
897 next = val + SOFTLIMIT_EVENTS_TARGET;
898 break;
899 case MEM_CGROUP_TARGET_NUMAINFO:
900 next = val + NUMAINFO_EVENTS_TARGET;
901 break;
902 default:
903 break;
904 }
905 __this_cpu_write(memcg->stat->targets[target], next);
906 return true;
7a159cc9 907 }
f53d7ce3 908 return false;
d2265e6f
KH
909}
910
911/*
912 * Check events in order.
913 *
914 */
c0ff4b85 915static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
d2265e6f 916{
4799401f 917 preempt_disable();
d2265e6f 918 /* threshold event is triggered in finer grain than soft limit */
f53d7ce3
JW
919 if (unlikely(mem_cgroup_event_ratelimit(memcg,
920 MEM_CGROUP_TARGET_THRESH))) {
82b3f2a7
AM
921 bool do_softlimit;
922 bool do_numainfo __maybe_unused;
f53d7ce3
JW
923
924 do_softlimit = mem_cgroup_event_ratelimit(memcg,
925 MEM_CGROUP_TARGET_SOFTLIMIT);
926#if MAX_NUMNODES > 1
927 do_numainfo = mem_cgroup_event_ratelimit(memcg,
928 MEM_CGROUP_TARGET_NUMAINFO);
929#endif
930 preempt_enable();
931
c0ff4b85 932 mem_cgroup_threshold(memcg);
f53d7ce3 933 if (unlikely(do_softlimit))
c0ff4b85 934 mem_cgroup_update_tree(memcg, page);
453a9bf3 935#if MAX_NUMNODES > 1
f53d7ce3 936 if (unlikely(do_numainfo))
c0ff4b85 937 atomic_inc(&memcg->numainfo_events);
453a9bf3 938#endif
f53d7ce3
JW
939 } else
940 preempt_enable();
d2265e6f
KH
941}
942
d1a4c0b3 943struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
8cdea7c0 944{
b2145145
WL
945 return mem_cgroup_from_css(
946 cgroup_subsys_state(cont, mem_cgroup_subsys_id));
8cdea7c0
BS
947}
948
cf475ad2 949struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
78fb7466 950{
31a78f23
BS
951 /*
952 * mm_update_next_owner() may clear mm->owner to NULL
953 * if it races with swapoff, page migration, etc.
954 * So this can be called with p == NULL.
955 */
956 if (unlikely(!p))
957 return NULL;
958
b2145145 959 return mem_cgroup_from_css(task_subsys_state(p, mem_cgroup_subsys_id));
78fb7466
PE
960}
961
a433658c 962struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
54595fe2 963{
c0ff4b85 964 struct mem_cgroup *memcg = NULL;
0b7f569e
KH
965
966 if (!mm)
967 return NULL;
54595fe2
KH
968 /*
969 * Because we have no locks, mm->owner's may be being moved to other
970 * cgroup. We use css_tryget() here even if this looks
971 * pessimistic (rather than adding locks here).
972 */
973 rcu_read_lock();
974 do {
c0ff4b85
R
975 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
976 if (unlikely(!memcg))
54595fe2 977 break;
c0ff4b85 978 } while (!css_tryget(&memcg->css));
54595fe2 979 rcu_read_unlock();
c0ff4b85 980 return memcg;
54595fe2
KH
981}
982
5660048c
JW
983/**
984 * mem_cgroup_iter - iterate over memory cgroup hierarchy
985 * @root: hierarchy root
986 * @prev: previously returned memcg, NULL on first invocation
987 * @reclaim: cookie for shared reclaim walks, NULL for full walks
988 *
989 * Returns references to children of the hierarchy below @root, or
990 * @root itself, or %NULL after a full round-trip.
991 *
992 * Caller must pass the return value in @prev on subsequent
993 * invocations for reference counting, or use mem_cgroup_iter_break()
994 * to cancel a hierarchy walk before the round-trip is complete.
995 *
996 * Reclaimers can specify a zone and a priority level in @reclaim to
997 * divide up the memcgs in the hierarchy among all concurrent
998 * reclaimers operating on the same zone and priority.
999 */
1000struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
1001 struct mem_cgroup *prev,
1002 struct mem_cgroup_reclaim_cookie *reclaim)
14067bb3 1003{
9f3a0d09
JW
1004 struct mem_cgroup *memcg = NULL;
1005 int id = 0;
711d3d2c 1006
5660048c
JW
1007 if (mem_cgroup_disabled())
1008 return NULL;
1009
9f3a0d09
JW
1010 if (!root)
1011 root = root_mem_cgroup;
7d74b06f 1012
9f3a0d09
JW
1013 if (prev && !reclaim)
1014 id = css_id(&prev->css);
14067bb3 1015
9f3a0d09
JW
1016 if (prev && prev != root)
1017 css_put(&prev->css);
14067bb3 1018
9f3a0d09
JW
1019 if (!root->use_hierarchy && root != root_mem_cgroup) {
1020 if (prev)
1021 return NULL;
1022 return root;
1023 }
14067bb3 1024
9f3a0d09 1025 while (!memcg) {
527a5ec9 1026 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
9f3a0d09 1027 struct cgroup_subsys_state *css;
711d3d2c 1028
527a5ec9
JW
1029 if (reclaim) {
1030 int nid = zone_to_nid(reclaim->zone);
1031 int zid = zone_idx(reclaim->zone);
1032 struct mem_cgroup_per_zone *mz;
1033
1034 mz = mem_cgroup_zoneinfo(root, nid, zid);
1035 iter = &mz->reclaim_iter[reclaim->priority];
1036 if (prev && reclaim->generation != iter->generation)
1037 return NULL;
1038 id = iter->position;
1039 }
7d74b06f 1040
9f3a0d09
JW
1041 rcu_read_lock();
1042 css = css_get_next(&mem_cgroup_subsys, id + 1, &root->css, &id);
1043 if (css) {
1044 if (css == &root->css || css_tryget(css))
b2145145 1045 memcg = mem_cgroup_from_css(css);
9f3a0d09
JW
1046 } else
1047 id = 0;
14067bb3 1048 rcu_read_unlock();
14067bb3 1049
527a5ec9
JW
1050 if (reclaim) {
1051 iter->position = id;
1052 if (!css)
1053 iter->generation++;
1054 else if (!prev && memcg)
1055 reclaim->generation = iter->generation;
1056 }
9f3a0d09
JW
1057
1058 if (prev && !css)
1059 return NULL;
1060 }
1061 return memcg;
14067bb3 1062}
7d74b06f 1063
5660048c
JW
1064/**
1065 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1066 * @root: hierarchy root
1067 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1068 */
1069void mem_cgroup_iter_break(struct mem_cgroup *root,
1070 struct mem_cgroup *prev)
9f3a0d09
JW
1071{
1072 if (!root)
1073 root = root_mem_cgroup;
1074 if (prev && prev != root)
1075 css_put(&prev->css);
1076}
7d74b06f 1077
9f3a0d09
JW
1078/*
1079 * Iteration constructs for visiting all cgroups (under a tree). If
1080 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1081 * be used for reference counting.
1082 */
1083#define for_each_mem_cgroup_tree(iter, root) \
527a5ec9 1084 for (iter = mem_cgroup_iter(root, NULL, NULL); \
9f3a0d09 1085 iter != NULL; \
527a5ec9 1086 iter = mem_cgroup_iter(root, iter, NULL))
711d3d2c 1087
9f3a0d09 1088#define for_each_mem_cgroup(iter) \
527a5ec9 1089 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
9f3a0d09 1090 iter != NULL; \
527a5ec9 1091 iter = mem_cgroup_iter(NULL, iter, NULL))
14067bb3 1092
68ae564b 1093void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
456f998e 1094{
c0ff4b85 1095 struct mem_cgroup *memcg;
456f998e 1096
456f998e 1097 rcu_read_lock();
c0ff4b85
R
1098 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1099 if (unlikely(!memcg))
456f998e
YH
1100 goto out;
1101
1102 switch (idx) {
456f998e 1103 case PGFAULT:
0e574a93
JW
1104 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1105 break;
1106 case PGMAJFAULT:
1107 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
456f998e
YH
1108 break;
1109 default:
1110 BUG();
1111 }
1112out:
1113 rcu_read_unlock();
1114}
68ae564b 1115EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
456f998e 1116
925b7673
JW
1117/**
1118 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1119 * @zone: zone of the wanted lruvec
fa9add64 1120 * @memcg: memcg of the wanted lruvec
925b7673
JW
1121 *
1122 * Returns the lru list vector holding pages for the given @zone and
1123 * @mem. This can be the global zone lruvec, if the memory controller
1124 * is disabled.
1125 */
1126struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1127 struct mem_cgroup *memcg)
1128{
1129 struct mem_cgroup_per_zone *mz;
bea8c150 1130 struct lruvec *lruvec;
925b7673 1131
bea8c150
HD
1132 if (mem_cgroup_disabled()) {
1133 lruvec = &zone->lruvec;
1134 goto out;
1135 }
925b7673
JW
1136
1137 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
bea8c150
HD
1138 lruvec = &mz->lruvec;
1139out:
1140 /*
1141 * Since a node can be onlined after the mem_cgroup was created,
1142 * we have to be prepared to initialize lruvec->zone here;
1143 * and if offlined then reonlined, we need to reinitialize it.
1144 */
1145 if (unlikely(lruvec->zone != zone))
1146 lruvec->zone = zone;
1147 return lruvec;
925b7673
JW
1148}
1149
08e552c6
KH
1150/*
1151 * Following LRU functions are allowed to be used without PCG_LOCK.
1152 * Operations are called by routine of global LRU independently from memcg.
1153 * What we have to take care of here is validness of pc->mem_cgroup.
1154 *
1155 * Changes to pc->mem_cgroup happens when
1156 * 1. charge
1157 * 2. moving account
1158 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1159 * It is added to LRU before charge.
1160 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1161 * When moving account, the page is not on LRU. It's isolated.
1162 */
4f98a2fe 1163
925b7673 1164/**
fa9add64 1165 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
925b7673 1166 * @page: the page
fa9add64 1167 * @zone: zone of the page
925b7673 1168 */
fa9add64 1169struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
08e552c6 1170{
08e552c6 1171 struct mem_cgroup_per_zone *mz;
925b7673
JW
1172 struct mem_cgroup *memcg;
1173 struct page_cgroup *pc;
bea8c150 1174 struct lruvec *lruvec;
6d12e2d8 1175
bea8c150
HD
1176 if (mem_cgroup_disabled()) {
1177 lruvec = &zone->lruvec;
1178 goto out;
1179 }
925b7673 1180
08e552c6 1181 pc = lookup_page_cgroup(page);
38c5d72f 1182 memcg = pc->mem_cgroup;
7512102c
HD
1183
1184 /*
fa9add64 1185 * Surreptitiously switch any uncharged offlist page to root:
7512102c
HD
1186 * an uncharged page off lru does nothing to secure
1187 * its former mem_cgroup from sudden removal.
1188 *
1189 * Our caller holds lru_lock, and PageCgroupUsed is updated
1190 * under page_cgroup lock: between them, they make all uses
1191 * of pc->mem_cgroup safe.
1192 */
fa9add64 1193 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
7512102c
HD
1194 pc->mem_cgroup = memcg = root_mem_cgroup;
1195
925b7673 1196 mz = page_cgroup_zoneinfo(memcg, page);
bea8c150
HD
1197 lruvec = &mz->lruvec;
1198out:
1199 /*
1200 * Since a node can be onlined after the mem_cgroup was created,
1201 * we have to be prepared to initialize lruvec->zone here;
1202 * and if offlined then reonlined, we need to reinitialize it.
1203 */
1204 if (unlikely(lruvec->zone != zone))
1205 lruvec->zone = zone;
1206 return lruvec;
08e552c6 1207}
b69408e8 1208
925b7673 1209/**
fa9add64
HD
1210 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1211 * @lruvec: mem_cgroup per zone lru vector
1212 * @lru: index of lru list the page is sitting on
1213 * @nr_pages: positive when adding or negative when removing
925b7673 1214 *
fa9add64
HD
1215 * This function must be called when a page is added to or removed from an
1216 * lru list.
3f58a829 1217 */
fa9add64
HD
1218void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1219 int nr_pages)
3f58a829
MK
1220{
1221 struct mem_cgroup_per_zone *mz;
fa9add64 1222 unsigned long *lru_size;
3f58a829
MK
1223
1224 if (mem_cgroup_disabled())
1225 return;
1226
fa9add64
HD
1227 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1228 lru_size = mz->lru_size + lru;
1229 *lru_size += nr_pages;
1230 VM_BUG_ON((long)(*lru_size) < 0);
08e552c6 1231}
544122e5 1232
3e92041d 1233/*
c0ff4b85 1234 * Checks whether given mem is same or in the root_mem_cgroup's
3e92041d
MH
1235 * hierarchy subtree
1236 */
c3ac9a8a
JW
1237bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1238 struct mem_cgroup *memcg)
3e92041d 1239{
91c63734
JW
1240 if (root_memcg == memcg)
1241 return true;
3a981f48 1242 if (!root_memcg->use_hierarchy || !memcg)
91c63734 1243 return false;
c3ac9a8a
JW
1244 return css_is_ancestor(&memcg->css, &root_memcg->css);
1245}
1246
1247static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1248 struct mem_cgroup *memcg)
1249{
1250 bool ret;
1251
91c63734 1252 rcu_read_lock();
c3ac9a8a 1253 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
91c63734
JW
1254 rcu_read_unlock();
1255 return ret;
3e92041d
MH
1256}
1257
c0ff4b85 1258int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
4c4a2214
DR
1259{
1260 int ret;
0b7f569e 1261 struct mem_cgroup *curr = NULL;
158e0a2d 1262 struct task_struct *p;
4c4a2214 1263
158e0a2d 1264 p = find_lock_task_mm(task);
de077d22
DR
1265 if (p) {
1266 curr = try_get_mem_cgroup_from_mm(p->mm);
1267 task_unlock(p);
1268 } else {
1269 /*
1270 * All threads may have already detached their mm's, but the oom
1271 * killer still needs to detect if they have already been oom
1272 * killed to prevent needlessly killing additional tasks.
1273 */
1274 task_lock(task);
1275 curr = mem_cgroup_from_task(task);
1276 if (curr)
1277 css_get(&curr->css);
1278 task_unlock(task);
1279 }
0b7f569e
KH
1280 if (!curr)
1281 return 0;
d31f56db 1282 /*
c0ff4b85 1283 * We should check use_hierarchy of "memcg" not "curr". Because checking
d31f56db 1284 * use_hierarchy of "curr" here make this function true if hierarchy is
c0ff4b85
R
1285 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1286 * hierarchy(even if use_hierarchy is disabled in "memcg").
d31f56db 1287 */
c0ff4b85 1288 ret = mem_cgroup_same_or_subtree(memcg, curr);
0b7f569e 1289 css_put(&curr->css);
4c4a2214
DR
1290 return ret;
1291}
1292
c56d5c7d 1293int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
14797e23 1294{
9b272977 1295 unsigned long inactive_ratio;
14797e23 1296 unsigned long inactive;
9b272977 1297 unsigned long active;
c772be93 1298 unsigned long gb;
14797e23 1299
4d7dcca2
HD
1300 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1301 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
14797e23 1302
c772be93
KM
1303 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1304 if (gb)
1305 inactive_ratio = int_sqrt(10 * gb);
1306 else
1307 inactive_ratio = 1;
1308
9b272977 1309 return inactive * inactive_ratio < active;
14797e23
KM
1310}
1311
c56d5c7d 1312int mem_cgroup_inactive_file_is_low(struct lruvec *lruvec)
56e49d21
RR
1313{
1314 unsigned long active;
1315 unsigned long inactive;
1316
4d7dcca2
HD
1317 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_FILE);
1318 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_FILE);
56e49d21
RR
1319
1320 return (active > inactive);
1321}
1322
6d61ef40
BS
1323#define mem_cgroup_from_res_counter(counter, member) \
1324 container_of(counter, struct mem_cgroup, member)
1325
19942822 1326/**
9d11ea9f 1327 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
dad7557e 1328 * @memcg: the memory cgroup
19942822 1329 *
9d11ea9f 1330 * Returns the maximum amount of memory @mem can be charged with, in
7ec99d62 1331 * pages.
19942822 1332 */
c0ff4b85 1333static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
19942822 1334{
9d11ea9f
JW
1335 unsigned long long margin;
1336
c0ff4b85 1337 margin = res_counter_margin(&memcg->res);
9d11ea9f 1338 if (do_swap_account)
c0ff4b85 1339 margin = min(margin, res_counter_margin(&memcg->memsw));
7ec99d62 1340 return margin >> PAGE_SHIFT;
19942822
JW
1341}
1342
1f4c025b 1343int mem_cgroup_swappiness(struct mem_cgroup *memcg)
a7885eb8
KM
1344{
1345 struct cgroup *cgrp = memcg->css.cgroup;
a7885eb8
KM
1346
1347 /* root ? */
1348 if (cgrp->parent == NULL)
1349 return vm_swappiness;
1350
bf1ff263 1351 return memcg->swappiness;
a7885eb8
KM
1352}
1353
619d094b
KH
1354/*
1355 * memcg->moving_account is used for checking possibility that some thread is
1356 * calling move_account(). When a thread on CPU-A starts moving pages under
1357 * a memcg, other threads should check memcg->moving_account under
1358 * rcu_read_lock(), like this:
1359 *
1360 * CPU-A CPU-B
1361 * rcu_read_lock()
1362 * memcg->moving_account+1 if (memcg->mocing_account)
1363 * take heavy locks.
1364 * synchronize_rcu() update something.
1365 * rcu_read_unlock()
1366 * start move here.
1367 */
4331f7d3
KH
1368
1369/* for quick checking without looking up memcg */
1370atomic_t memcg_moving __read_mostly;
1371
c0ff4b85 1372static void mem_cgroup_start_move(struct mem_cgroup *memcg)
32047e2a 1373{
4331f7d3 1374 atomic_inc(&memcg_moving);
619d094b 1375 atomic_inc(&memcg->moving_account);
32047e2a
KH
1376 synchronize_rcu();
1377}
1378
c0ff4b85 1379static void mem_cgroup_end_move(struct mem_cgroup *memcg)
32047e2a 1380{
619d094b
KH
1381 /*
1382 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1383 * We check NULL in callee rather than caller.
1384 */
4331f7d3
KH
1385 if (memcg) {
1386 atomic_dec(&memcg_moving);
619d094b 1387 atomic_dec(&memcg->moving_account);
4331f7d3 1388 }
32047e2a 1389}
619d094b 1390
32047e2a
KH
1391/*
1392 * 2 routines for checking "mem" is under move_account() or not.
1393 *
13fd1dd9
AM
1394 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1395 * is used for avoiding races in accounting. If true,
32047e2a
KH
1396 * pc->mem_cgroup may be overwritten.
1397 *
1398 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1399 * under hierarchy of moving cgroups. This is for
1400 * waiting at hith-memory prressure caused by "move".
1401 */
1402
13fd1dd9 1403static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
32047e2a
KH
1404{
1405 VM_BUG_ON(!rcu_read_lock_held());
619d094b 1406 return atomic_read(&memcg->moving_account) > 0;
32047e2a 1407}
4b534334 1408
c0ff4b85 1409static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
4b534334 1410{
2bd9bb20
KH
1411 struct mem_cgroup *from;
1412 struct mem_cgroup *to;
4b534334 1413 bool ret = false;
2bd9bb20
KH
1414 /*
1415 * Unlike task_move routines, we access mc.to, mc.from not under
1416 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1417 */
1418 spin_lock(&mc.lock);
1419 from = mc.from;
1420 to = mc.to;
1421 if (!from)
1422 goto unlock;
3e92041d 1423
c0ff4b85
R
1424 ret = mem_cgroup_same_or_subtree(memcg, from)
1425 || mem_cgroup_same_or_subtree(memcg, to);
2bd9bb20
KH
1426unlock:
1427 spin_unlock(&mc.lock);
4b534334
KH
1428 return ret;
1429}
1430
c0ff4b85 1431static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
4b534334
KH
1432{
1433 if (mc.moving_task && current != mc.moving_task) {
c0ff4b85 1434 if (mem_cgroup_under_move(memcg)) {
4b534334
KH
1435 DEFINE_WAIT(wait);
1436 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1437 /* moving charge context might have finished. */
1438 if (mc.moving_task)
1439 schedule();
1440 finish_wait(&mc.waitq, &wait);
1441 return true;
1442 }
1443 }
1444 return false;
1445}
1446
312734c0
KH
1447/*
1448 * Take this lock when
1449 * - a code tries to modify page's memcg while it's USED.
1450 * - a code tries to modify page state accounting in a memcg.
13fd1dd9 1451 * see mem_cgroup_stolen(), too.
312734c0
KH
1452 */
1453static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1454 unsigned long *flags)
1455{
1456 spin_lock_irqsave(&memcg->move_lock, *flags);
1457}
1458
1459static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1460 unsigned long *flags)
1461{
1462 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1463}
1464
e222432b 1465/**
6a6135b6 1466 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
e222432b
BS
1467 * @memcg: The memory cgroup that went over limit
1468 * @p: Task that is going to be killed
1469 *
1470 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1471 * enabled
1472 */
1473void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1474{
1475 struct cgroup *task_cgrp;
1476 struct cgroup *mem_cgrp;
1477 /*
1478 * Need a buffer in BSS, can't rely on allocations. The code relies
1479 * on the assumption that OOM is serialized for memory controller.
1480 * If this assumption is broken, revisit this code.
1481 */
1482 static char memcg_name[PATH_MAX];
1483 int ret;
1484
d31f56db 1485 if (!memcg || !p)
e222432b
BS
1486 return;
1487
e222432b
BS
1488 rcu_read_lock();
1489
1490 mem_cgrp = memcg->css.cgroup;
1491 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1492
1493 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1494 if (ret < 0) {
1495 /*
1496 * Unfortunately, we are unable to convert to a useful name
1497 * But we'll still print out the usage information
1498 */
1499 rcu_read_unlock();
1500 goto done;
1501 }
1502 rcu_read_unlock();
1503
1504 printk(KERN_INFO "Task in %s killed", memcg_name);
1505
1506 rcu_read_lock();
1507 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1508 if (ret < 0) {
1509 rcu_read_unlock();
1510 goto done;
1511 }
1512 rcu_read_unlock();
1513
1514 /*
1515 * Continues from above, so we don't need an KERN_ level
1516 */
1517 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1518done:
1519
1520 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1521 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1522 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1523 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1524 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1525 "failcnt %llu\n",
1526 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1527 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1528 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
510fc4e1
GC
1529 printk(KERN_INFO "kmem: usage %llukB, limit %llukB, failcnt %llu\n",
1530 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1531 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1532 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
e222432b
BS
1533}
1534
81d39c20
KH
1535/*
1536 * This function returns the number of memcg under hierarchy tree. Returns
1537 * 1(self count) if no children.
1538 */
c0ff4b85 1539static int mem_cgroup_count_children(struct mem_cgroup *memcg)
81d39c20
KH
1540{
1541 int num = 0;
7d74b06f
KH
1542 struct mem_cgroup *iter;
1543
c0ff4b85 1544 for_each_mem_cgroup_tree(iter, memcg)
7d74b06f 1545 num++;
81d39c20
KH
1546 return num;
1547}
1548
a63d83f4
DR
1549/*
1550 * Return the memory (and swap, if configured) limit for a memcg.
1551 */
9cbb78bb 1552static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
a63d83f4
DR
1553{
1554 u64 limit;
a63d83f4 1555
f3e8eb70 1556 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
f3e8eb70 1557
a63d83f4 1558 /*
9a5a8f19 1559 * Do not consider swap space if we cannot swap due to swappiness
a63d83f4 1560 */
9a5a8f19
MH
1561 if (mem_cgroup_swappiness(memcg)) {
1562 u64 memsw;
1563
1564 limit += total_swap_pages << PAGE_SHIFT;
1565 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1566
1567 /*
1568 * If memsw is finite and limits the amount of swap space
1569 * available to this memcg, return that limit.
1570 */
1571 limit = min(limit, memsw);
1572 }
1573
1574 return limit;
a63d83f4
DR
1575}
1576
19965460
DR
1577static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1578 int order)
9cbb78bb
DR
1579{
1580 struct mem_cgroup *iter;
1581 unsigned long chosen_points = 0;
1582 unsigned long totalpages;
1583 unsigned int points = 0;
1584 struct task_struct *chosen = NULL;
1585
876aafbf
DR
1586 /*
1587 * If current has a pending SIGKILL, then automatically select it. The
1588 * goal is to allow it to allocate so that it may quickly exit and free
1589 * its memory.
1590 */
1591 if (fatal_signal_pending(current)) {
1592 set_thread_flag(TIF_MEMDIE);
1593 return;
1594 }
1595
1596 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
9cbb78bb
DR
1597 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1598 for_each_mem_cgroup_tree(iter, memcg) {
1599 struct cgroup *cgroup = iter->css.cgroup;
1600 struct cgroup_iter it;
1601 struct task_struct *task;
1602
1603 cgroup_iter_start(cgroup, &it);
1604 while ((task = cgroup_iter_next(cgroup, &it))) {
1605 switch (oom_scan_process_thread(task, totalpages, NULL,
1606 false)) {
1607 case OOM_SCAN_SELECT:
1608 if (chosen)
1609 put_task_struct(chosen);
1610 chosen = task;
1611 chosen_points = ULONG_MAX;
1612 get_task_struct(chosen);
1613 /* fall through */
1614 case OOM_SCAN_CONTINUE:
1615 continue;
1616 case OOM_SCAN_ABORT:
1617 cgroup_iter_end(cgroup, &it);
1618 mem_cgroup_iter_break(memcg, iter);
1619 if (chosen)
1620 put_task_struct(chosen);
1621 return;
1622 case OOM_SCAN_OK:
1623 break;
1624 };
1625 points = oom_badness(task, memcg, NULL, totalpages);
1626 if (points > chosen_points) {
1627 if (chosen)
1628 put_task_struct(chosen);
1629 chosen = task;
1630 chosen_points = points;
1631 get_task_struct(chosen);
1632 }
1633 }
1634 cgroup_iter_end(cgroup, &it);
1635 }
1636
1637 if (!chosen)
1638 return;
1639 points = chosen_points * 1000 / totalpages;
9cbb78bb
DR
1640 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1641 NULL, "Memory cgroup out of memory");
9cbb78bb
DR
1642}
1643
5660048c
JW
1644static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1645 gfp_t gfp_mask,
1646 unsigned long flags)
1647{
1648 unsigned long total = 0;
1649 bool noswap = false;
1650 int loop;
1651
1652 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1653 noswap = true;
1654 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1655 noswap = true;
1656
1657 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1658 if (loop)
1659 drain_all_stock_async(memcg);
1660 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1661 /*
1662 * Allow limit shrinkers, which are triggered directly
1663 * by userspace, to catch signals and stop reclaim
1664 * after minimal progress, regardless of the margin.
1665 */
1666 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1667 break;
1668 if (mem_cgroup_margin(memcg))
1669 break;
1670 /*
1671 * If nothing was reclaimed after two attempts, there
1672 * may be no reclaimable pages in this hierarchy.
1673 */
1674 if (loop && !total)
1675 break;
1676 }
1677 return total;
1678}
1679
4d0c066d
KH
1680/**
1681 * test_mem_cgroup_node_reclaimable
dad7557e 1682 * @memcg: the target memcg
4d0c066d
KH
1683 * @nid: the node ID to be checked.
1684 * @noswap : specify true here if the user wants flle only information.
1685 *
1686 * This function returns whether the specified memcg contains any
1687 * reclaimable pages on a node. Returns true if there are any reclaimable
1688 * pages in the node.
1689 */
c0ff4b85 1690static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
4d0c066d
KH
1691 int nid, bool noswap)
1692{
c0ff4b85 1693 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
4d0c066d
KH
1694 return true;
1695 if (noswap || !total_swap_pages)
1696 return false;
c0ff4b85 1697 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
4d0c066d
KH
1698 return true;
1699 return false;
1700
1701}
889976db
YH
1702#if MAX_NUMNODES > 1
1703
1704/*
1705 * Always updating the nodemask is not very good - even if we have an empty
1706 * list or the wrong list here, we can start from some node and traverse all
1707 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1708 *
1709 */
c0ff4b85 1710static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
889976db
YH
1711{
1712 int nid;
453a9bf3
KH
1713 /*
1714 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1715 * pagein/pageout changes since the last update.
1716 */
c0ff4b85 1717 if (!atomic_read(&memcg->numainfo_events))
453a9bf3 1718 return;
c0ff4b85 1719 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
889976db
YH
1720 return;
1721
889976db 1722 /* make a nodemask where this memcg uses memory from */
31aaea4a 1723 memcg->scan_nodes = node_states[N_MEMORY];
889976db 1724
31aaea4a 1725 for_each_node_mask(nid, node_states[N_MEMORY]) {
889976db 1726
c0ff4b85
R
1727 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1728 node_clear(nid, memcg->scan_nodes);
889976db 1729 }
453a9bf3 1730
c0ff4b85
R
1731 atomic_set(&memcg->numainfo_events, 0);
1732 atomic_set(&memcg->numainfo_updating, 0);
889976db
YH
1733}
1734
1735/*
1736 * Selecting a node where we start reclaim from. Because what we need is just
1737 * reducing usage counter, start from anywhere is O,K. Considering
1738 * memory reclaim from current node, there are pros. and cons.
1739 *
1740 * Freeing memory from current node means freeing memory from a node which
1741 * we'll use or we've used. So, it may make LRU bad. And if several threads
1742 * hit limits, it will see a contention on a node. But freeing from remote
1743 * node means more costs for memory reclaim because of memory latency.
1744 *
1745 * Now, we use round-robin. Better algorithm is welcomed.
1746 */
c0ff4b85 1747int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
889976db
YH
1748{
1749 int node;
1750
c0ff4b85
R
1751 mem_cgroup_may_update_nodemask(memcg);
1752 node = memcg->last_scanned_node;
889976db 1753
c0ff4b85 1754 node = next_node(node, memcg->scan_nodes);
889976db 1755 if (node == MAX_NUMNODES)
c0ff4b85 1756 node = first_node(memcg->scan_nodes);
889976db
YH
1757 /*
1758 * We call this when we hit limit, not when pages are added to LRU.
1759 * No LRU may hold pages because all pages are UNEVICTABLE or
1760 * memcg is too small and all pages are not on LRU. In that case,
1761 * we use curret node.
1762 */
1763 if (unlikely(node == MAX_NUMNODES))
1764 node = numa_node_id();
1765
c0ff4b85 1766 memcg->last_scanned_node = node;
889976db
YH
1767 return node;
1768}
1769
4d0c066d
KH
1770/*
1771 * Check all nodes whether it contains reclaimable pages or not.
1772 * For quick scan, we make use of scan_nodes. This will allow us to skip
1773 * unused nodes. But scan_nodes is lazily updated and may not cotain
1774 * enough new information. We need to do double check.
1775 */
6bbda35c 1776static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
4d0c066d
KH
1777{
1778 int nid;
1779
1780 /*
1781 * quick check...making use of scan_node.
1782 * We can skip unused nodes.
1783 */
c0ff4b85
R
1784 if (!nodes_empty(memcg->scan_nodes)) {
1785 for (nid = first_node(memcg->scan_nodes);
4d0c066d 1786 nid < MAX_NUMNODES;
c0ff4b85 1787 nid = next_node(nid, memcg->scan_nodes)) {
4d0c066d 1788
c0ff4b85 1789 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
4d0c066d
KH
1790 return true;
1791 }
1792 }
1793 /*
1794 * Check rest of nodes.
1795 */
31aaea4a 1796 for_each_node_state(nid, N_MEMORY) {
c0ff4b85 1797 if (node_isset(nid, memcg->scan_nodes))
4d0c066d 1798 continue;
c0ff4b85 1799 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
4d0c066d
KH
1800 return true;
1801 }
1802 return false;
1803}
1804
889976db 1805#else
c0ff4b85 1806int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
889976db
YH
1807{
1808 return 0;
1809}
4d0c066d 1810
6bbda35c 1811static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
4d0c066d 1812{
c0ff4b85 1813 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
4d0c066d 1814}
889976db
YH
1815#endif
1816
5660048c
JW
1817static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1818 struct zone *zone,
1819 gfp_t gfp_mask,
1820 unsigned long *total_scanned)
6d61ef40 1821{
9f3a0d09 1822 struct mem_cgroup *victim = NULL;
5660048c 1823 int total = 0;
04046e1a 1824 int loop = 0;
9d11ea9f 1825 unsigned long excess;
185efc0f 1826 unsigned long nr_scanned;
527a5ec9
JW
1827 struct mem_cgroup_reclaim_cookie reclaim = {
1828 .zone = zone,
1829 .priority = 0,
1830 };
9d11ea9f 1831
c0ff4b85 1832 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
04046e1a 1833
4e416953 1834 while (1) {
527a5ec9 1835 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
9f3a0d09 1836 if (!victim) {
04046e1a 1837 loop++;
4e416953
BS
1838 if (loop >= 2) {
1839 /*
1840 * If we have not been able to reclaim
1841 * anything, it might because there are
1842 * no reclaimable pages under this hierarchy
1843 */
5660048c 1844 if (!total)
4e416953 1845 break;
4e416953 1846 /*
25985edc 1847 * We want to do more targeted reclaim.
4e416953
BS
1848 * excess >> 2 is not to excessive so as to
1849 * reclaim too much, nor too less that we keep
1850 * coming back to reclaim from this cgroup
1851 */
1852 if (total >= (excess >> 2) ||
9f3a0d09 1853 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
4e416953 1854 break;
4e416953 1855 }
9f3a0d09 1856 continue;
4e416953 1857 }
5660048c 1858 if (!mem_cgroup_reclaimable(victim, false))
6d61ef40 1859 continue;
5660048c
JW
1860 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
1861 zone, &nr_scanned);
1862 *total_scanned += nr_scanned;
1863 if (!res_counter_soft_limit_excess(&root_memcg->res))
9f3a0d09 1864 break;
6d61ef40 1865 }
9f3a0d09 1866 mem_cgroup_iter_break(root_memcg, victim);
04046e1a 1867 return total;
6d61ef40
BS
1868}
1869
867578cb
KH
1870/*
1871 * Check OOM-Killer is already running under our hierarchy.
1872 * If someone is running, return false.
1af8efe9 1873 * Has to be called with memcg_oom_lock
867578cb 1874 */
c0ff4b85 1875static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
867578cb 1876{
79dfdacc 1877 struct mem_cgroup *iter, *failed = NULL;
a636b327 1878
9f3a0d09 1879 for_each_mem_cgroup_tree(iter, memcg) {
23751be0 1880 if (iter->oom_lock) {
79dfdacc
MH
1881 /*
1882 * this subtree of our hierarchy is already locked
1883 * so we cannot give a lock.
1884 */
79dfdacc 1885 failed = iter;
9f3a0d09
JW
1886 mem_cgroup_iter_break(memcg, iter);
1887 break;
23751be0
JW
1888 } else
1889 iter->oom_lock = true;
7d74b06f 1890 }
867578cb 1891
79dfdacc 1892 if (!failed)
23751be0 1893 return true;
79dfdacc
MH
1894
1895 /*
1896 * OK, we failed to lock the whole subtree so we have to clean up
1897 * what we set up to the failing subtree
1898 */
9f3a0d09 1899 for_each_mem_cgroup_tree(iter, memcg) {
79dfdacc 1900 if (iter == failed) {
9f3a0d09
JW
1901 mem_cgroup_iter_break(memcg, iter);
1902 break;
79dfdacc
MH
1903 }
1904 iter->oom_lock = false;
1905 }
23751be0 1906 return false;
a636b327 1907}
0b7f569e 1908
79dfdacc 1909/*
1af8efe9 1910 * Has to be called with memcg_oom_lock
79dfdacc 1911 */
c0ff4b85 1912static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
0b7f569e 1913{
7d74b06f
KH
1914 struct mem_cgroup *iter;
1915
c0ff4b85 1916 for_each_mem_cgroup_tree(iter, memcg)
79dfdacc
MH
1917 iter->oom_lock = false;
1918 return 0;
1919}
1920
c0ff4b85 1921static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
79dfdacc
MH
1922{
1923 struct mem_cgroup *iter;
1924
c0ff4b85 1925 for_each_mem_cgroup_tree(iter, memcg)
79dfdacc
MH
1926 atomic_inc(&iter->under_oom);
1927}
1928
c0ff4b85 1929static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
79dfdacc
MH
1930{
1931 struct mem_cgroup *iter;
1932
867578cb
KH
1933 /*
1934 * When a new child is created while the hierarchy is under oom,
1935 * mem_cgroup_oom_lock() may not be called. We have to use
1936 * atomic_add_unless() here.
1937 */
c0ff4b85 1938 for_each_mem_cgroup_tree(iter, memcg)
79dfdacc 1939 atomic_add_unless(&iter->under_oom, -1, 0);
0b7f569e
KH
1940}
1941
1af8efe9 1942static DEFINE_SPINLOCK(memcg_oom_lock);
867578cb
KH
1943static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1944
dc98df5a 1945struct oom_wait_info {
d79154bb 1946 struct mem_cgroup *memcg;
dc98df5a
KH
1947 wait_queue_t wait;
1948};
1949
1950static int memcg_oom_wake_function(wait_queue_t *wait,
1951 unsigned mode, int sync, void *arg)
1952{
d79154bb
HD
1953 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
1954 struct mem_cgroup *oom_wait_memcg;
dc98df5a
KH
1955 struct oom_wait_info *oom_wait_info;
1956
1957 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
d79154bb 1958 oom_wait_memcg = oom_wait_info->memcg;
dc98df5a 1959
dc98df5a 1960 /*
d79154bb 1961 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
dc98df5a
KH
1962 * Then we can use css_is_ancestor without taking care of RCU.
1963 */
c0ff4b85
R
1964 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
1965 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
dc98df5a 1966 return 0;
dc98df5a
KH
1967 return autoremove_wake_function(wait, mode, sync, arg);
1968}
1969
c0ff4b85 1970static void memcg_wakeup_oom(struct mem_cgroup *memcg)
dc98df5a 1971{
c0ff4b85
R
1972 /* for filtering, pass "memcg" as argument. */
1973 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
dc98df5a
KH
1974}
1975
c0ff4b85 1976static void memcg_oom_recover(struct mem_cgroup *memcg)
3c11ecf4 1977{
c0ff4b85
R
1978 if (memcg && atomic_read(&memcg->under_oom))
1979 memcg_wakeup_oom(memcg);
3c11ecf4
KH
1980}
1981
867578cb
KH
1982/*
1983 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1984 */
6bbda35c
KS
1985static bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask,
1986 int order)
0b7f569e 1987{
dc98df5a 1988 struct oom_wait_info owait;
3c11ecf4 1989 bool locked, need_to_kill;
867578cb 1990
d79154bb 1991 owait.memcg = memcg;
dc98df5a
KH
1992 owait.wait.flags = 0;
1993 owait.wait.func = memcg_oom_wake_function;
1994 owait.wait.private = current;
1995 INIT_LIST_HEAD(&owait.wait.task_list);
3c11ecf4 1996 need_to_kill = true;
c0ff4b85 1997 mem_cgroup_mark_under_oom(memcg);
79dfdacc 1998
c0ff4b85 1999 /* At first, try to OOM lock hierarchy under memcg.*/
1af8efe9 2000 spin_lock(&memcg_oom_lock);
c0ff4b85 2001 locked = mem_cgroup_oom_lock(memcg);
867578cb
KH
2002 /*
2003 * Even if signal_pending(), we can't quit charge() loop without
2004 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
2005 * under OOM is always welcomed, use TASK_KILLABLE here.
2006 */
3c11ecf4 2007 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
c0ff4b85 2008 if (!locked || memcg->oom_kill_disable)
3c11ecf4
KH
2009 need_to_kill = false;
2010 if (locked)
c0ff4b85 2011 mem_cgroup_oom_notify(memcg);
1af8efe9 2012 spin_unlock(&memcg_oom_lock);
867578cb 2013
3c11ecf4
KH
2014 if (need_to_kill) {
2015 finish_wait(&memcg_oom_waitq, &owait.wait);
e845e199 2016 mem_cgroup_out_of_memory(memcg, mask, order);
3c11ecf4 2017 } else {
867578cb 2018 schedule();
dc98df5a 2019 finish_wait(&memcg_oom_waitq, &owait.wait);
867578cb 2020 }
1af8efe9 2021 spin_lock(&memcg_oom_lock);
79dfdacc 2022 if (locked)
c0ff4b85
R
2023 mem_cgroup_oom_unlock(memcg);
2024 memcg_wakeup_oom(memcg);
1af8efe9 2025 spin_unlock(&memcg_oom_lock);
867578cb 2026
c0ff4b85 2027 mem_cgroup_unmark_under_oom(memcg);
79dfdacc 2028
867578cb
KH
2029 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
2030 return false;
2031 /* Give chance to dying process */
715a5ee8 2032 schedule_timeout_uninterruptible(1);
867578cb 2033 return true;
0b7f569e
KH
2034}
2035
d69b042f
BS
2036/*
2037 * Currently used to update mapped file statistics, but the routine can be
2038 * generalized to update other statistics as well.
32047e2a
KH
2039 *
2040 * Notes: Race condition
2041 *
2042 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2043 * it tends to be costly. But considering some conditions, we doesn't need
2044 * to do so _always_.
2045 *
2046 * Considering "charge", lock_page_cgroup() is not required because all
2047 * file-stat operations happen after a page is attached to radix-tree. There
2048 * are no race with "charge".
2049 *
2050 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2051 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2052 * if there are race with "uncharge". Statistics itself is properly handled
2053 * by flags.
2054 *
2055 * Considering "move", this is an only case we see a race. To make the race
619d094b
KH
2056 * small, we check mm->moving_account and detect there are possibility of race
2057 * If there is, we take a lock.
d69b042f 2058 */
26174efd 2059
89c06bd5
KH
2060void __mem_cgroup_begin_update_page_stat(struct page *page,
2061 bool *locked, unsigned long *flags)
2062{
2063 struct mem_cgroup *memcg;
2064 struct page_cgroup *pc;
2065
2066 pc = lookup_page_cgroup(page);
2067again:
2068 memcg = pc->mem_cgroup;
2069 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2070 return;
2071 /*
2072 * If this memory cgroup is not under account moving, we don't
da92c47d 2073 * need to take move_lock_mem_cgroup(). Because we already hold
89c06bd5 2074 * rcu_read_lock(), any calls to move_account will be delayed until
13fd1dd9 2075 * rcu_read_unlock() if mem_cgroup_stolen() == true.
89c06bd5 2076 */
13fd1dd9 2077 if (!mem_cgroup_stolen(memcg))
89c06bd5
KH
2078 return;
2079
2080 move_lock_mem_cgroup(memcg, flags);
2081 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2082 move_unlock_mem_cgroup(memcg, flags);
2083 goto again;
2084 }
2085 *locked = true;
2086}
2087
2088void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2089{
2090 struct page_cgroup *pc = lookup_page_cgroup(page);
2091
2092 /*
2093 * It's guaranteed that pc->mem_cgroup never changes while
2094 * lock is held because a routine modifies pc->mem_cgroup
da92c47d 2095 * should take move_lock_mem_cgroup().
89c06bd5
KH
2096 */
2097 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2098}
2099
2a7106f2
GT
2100void mem_cgroup_update_page_stat(struct page *page,
2101 enum mem_cgroup_page_stat_item idx, int val)
d69b042f 2102{
c0ff4b85 2103 struct mem_cgroup *memcg;
32047e2a 2104 struct page_cgroup *pc = lookup_page_cgroup(page);
dbd4ea78 2105 unsigned long uninitialized_var(flags);
d69b042f 2106
cfa44946 2107 if (mem_cgroup_disabled())
d69b042f 2108 return;
89c06bd5 2109
c0ff4b85
R
2110 memcg = pc->mem_cgroup;
2111 if (unlikely(!memcg || !PageCgroupUsed(pc)))
89c06bd5 2112 return;
26174efd 2113
26174efd 2114 switch (idx) {
2a7106f2 2115 case MEMCG_NR_FILE_MAPPED:
2a7106f2 2116 idx = MEM_CGROUP_STAT_FILE_MAPPED;
26174efd
KH
2117 break;
2118 default:
2119 BUG();
8725d541 2120 }
d69b042f 2121
c0ff4b85 2122 this_cpu_add(memcg->stat->count[idx], val);
d69b042f 2123}
26174efd 2124
cdec2e42
KH
2125/*
2126 * size of first charge trial. "32" comes from vmscan.c's magic value.
2127 * TODO: maybe necessary to use big numbers in big irons.
2128 */
7ec99d62 2129#define CHARGE_BATCH 32U
cdec2e42
KH
2130struct memcg_stock_pcp {
2131 struct mem_cgroup *cached; /* this never be root cgroup */
11c9ea4e 2132 unsigned int nr_pages;
cdec2e42 2133 struct work_struct work;
26fe6168 2134 unsigned long flags;
a0db00fc 2135#define FLUSHING_CACHED_CHARGE 0
cdec2e42
KH
2136};
2137static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
9f50fad6 2138static DEFINE_MUTEX(percpu_charge_mutex);
cdec2e42 2139
a0956d54
SS
2140/**
2141 * consume_stock: Try to consume stocked charge on this cpu.
2142 * @memcg: memcg to consume from.
2143 * @nr_pages: how many pages to charge.
2144 *
2145 * The charges will only happen if @memcg matches the current cpu's memcg
2146 * stock, and at least @nr_pages are available in that stock. Failure to
2147 * service an allocation will refill the stock.
2148 *
2149 * returns true if successful, false otherwise.
cdec2e42 2150 */
a0956d54 2151static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
cdec2e42
KH
2152{
2153 struct memcg_stock_pcp *stock;
2154 bool ret = true;
2155
a0956d54
SS
2156 if (nr_pages > CHARGE_BATCH)
2157 return false;
2158
cdec2e42 2159 stock = &get_cpu_var(memcg_stock);
a0956d54
SS
2160 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2161 stock->nr_pages -= nr_pages;
cdec2e42
KH
2162 else /* need to call res_counter_charge */
2163 ret = false;
2164 put_cpu_var(memcg_stock);
2165 return ret;
2166}
2167
2168/*
2169 * Returns stocks cached in percpu to res_counter and reset cached information.
2170 */
2171static void drain_stock(struct memcg_stock_pcp *stock)
2172{
2173 struct mem_cgroup *old = stock->cached;
2174
11c9ea4e
JW
2175 if (stock->nr_pages) {
2176 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2177
2178 res_counter_uncharge(&old->res, bytes);
cdec2e42 2179 if (do_swap_account)
11c9ea4e
JW
2180 res_counter_uncharge(&old->memsw, bytes);
2181 stock->nr_pages = 0;
cdec2e42
KH
2182 }
2183 stock->cached = NULL;
cdec2e42
KH
2184}
2185
2186/*
2187 * This must be called under preempt disabled or must be called by
2188 * a thread which is pinned to local cpu.
2189 */
2190static void drain_local_stock(struct work_struct *dummy)
2191{
2192 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2193 drain_stock(stock);
26fe6168 2194 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
cdec2e42
KH
2195}
2196
2197/*
2198 * Cache charges(val) which is from res_counter, to local per_cpu area.
320cc51d 2199 * This will be consumed by consume_stock() function, later.
cdec2e42 2200 */
c0ff4b85 2201static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
cdec2e42
KH
2202{
2203 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2204
c0ff4b85 2205 if (stock->cached != memcg) { /* reset if necessary */
cdec2e42 2206 drain_stock(stock);
c0ff4b85 2207 stock->cached = memcg;
cdec2e42 2208 }
11c9ea4e 2209 stock->nr_pages += nr_pages;
cdec2e42
KH
2210 put_cpu_var(memcg_stock);
2211}
2212
2213/*
c0ff4b85 2214 * Drains all per-CPU charge caches for given root_memcg resp. subtree
d38144b7
MH
2215 * of the hierarchy under it. sync flag says whether we should block
2216 * until the work is done.
cdec2e42 2217 */
c0ff4b85 2218static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
cdec2e42 2219{
26fe6168 2220 int cpu, curcpu;
d38144b7 2221
cdec2e42 2222 /* Notify other cpus that system-wide "drain" is running */
cdec2e42 2223 get_online_cpus();
5af12d0e 2224 curcpu = get_cpu();
cdec2e42
KH
2225 for_each_online_cpu(cpu) {
2226 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
c0ff4b85 2227 struct mem_cgroup *memcg;
26fe6168 2228
c0ff4b85
R
2229 memcg = stock->cached;
2230 if (!memcg || !stock->nr_pages)
26fe6168 2231 continue;
c0ff4b85 2232 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
3e92041d 2233 continue;
d1a05b69
MH
2234 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2235 if (cpu == curcpu)
2236 drain_local_stock(&stock->work);
2237 else
2238 schedule_work_on(cpu, &stock->work);
2239 }
cdec2e42 2240 }
5af12d0e 2241 put_cpu();
d38144b7
MH
2242
2243 if (!sync)
2244 goto out;
2245
2246 for_each_online_cpu(cpu) {
2247 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
9f50fad6 2248 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
d38144b7
MH
2249 flush_work(&stock->work);
2250 }
2251out:
cdec2e42 2252 put_online_cpus();
d38144b7
MH
2253}
2254
2255/*
2256 * Tries to drain stocked charges in other cpus. This function is asynchronous
2257 * and just put a work per cpu for draining localy on each cpu. Caller can
2258 * expects some charges will be back to res_counter later but cannot wait for
2259 * it.
2260 */
c0ff4b85 2261static void drain_all_stock_async(struct mem_cgroup *root_memcg)
d38144b7 2262{
9f50fad6
MH
2263 /*
2264 * If someone calls draining, avoid adding more kworker runs.
2265 */
2266 if (!mutex_trylock(&percpu_charge_mutex))
2267 return;
c0ff4b85 2268 drain_all_stock(root_memcg, false);
9f50fad6 2269 mutex_unlock(&percpu_charge_mutex);
cdec2e42
KH
2270}
2271
2272/* This is a synchronous drain interface. */
c0ff4b85 2273static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
cdec2e42
KH
2274{
2275 /* called when force_empty is called */
9f50fad6 2276 mutex_lock(&percpu_charge_mutex);
c0ff4b85 2277 drain_all_stock(root_memcg, true);
9f50fad6 2278 mutex_unlock(&percpu_charge_mutex);
cdec2e42
KH
2279}
2280
711d3d2c
KH
2281/*
2282 * This function drains percpu counter value from DEAD cpu and
2283 * move it to local cpu. Note that this function can be preempted.
2284 */
c0ff4b85 2285static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
711d3d2c
KH
2286{
2287 int i;
2288
c0ff4b85 2289 spin_lock(&memcg->pcp_counter_lock);
6104621d 2290 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
c0ff4b85 2291 long x = per_cpu(memcg->stat->count[i], cpu);
711d3d2c 2292
c0ff4b85
R
2293 per_cpu(memcg->stat->count[i], cpu) = 0;
2294 memcg->nocpu_base.count[i] += x;
711d3d2c 2295 }
e9f8974f 2296 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
c0ff4b85 2297 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
e9f8974f 2298
c0ff4b85
R
2299 per_cpu(memcg->stat->events[i], cpu) = 0;
2300 memcg->nocpu_base.events[i] += x;
e9f8974f 2301 }
c0ff4b85 2302 spin_unlock(&memcg->pcp_counter_lock);
711d3d2c
KH
2303}
2304
2305static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
cdec2e42
KH
2306 unsigned long action,
2307 void *hcpu)
2308{
2309 int cpu = (unsigned long)hcpu;
2310 struct memcg_stock_pcp *stock;
711d3d2c 2311 struct mem_cgroup *iter;
cdec2e42 2312
619d094b 2313 if (action == CPU_ONLINE)
1489ebad 2314 return NOTIFY_OK;
1489ebad 2315
d833049b 2316 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
cdec2e42 2317 return NOTIFY_OK;
711d3d2c 2318
9f3a0d09 2319 for_each_mem_cgroup(iter)
711d3d2c
KH
2320 mem_cgroup_drain_pcp_counter(iter, cpu);
2321
cdec2e42
KH
2322 stock = &per_cpu(memcg_stock, cpu);
2323 drain_stock(stock);
2324 return NOTIFY_OK;
2325}
2326
4b534334
KH
2327
2328/* See __mem_cgroup_try_charge() for details */
2329enum {
2330 CHARGE_OK, /* success */
2331 CHARGE_RETRY, /* need to retry but retry is not bad */
2332 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2333 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2334 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2335};
2336
c0ff4b85 2337static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
4c9c5359
SS
2338 unsigned int nr_pages, unsigned int min_pages,
2339 bool oom_check)
4b534334 2340{
7ec99d62 2341 unsigned long csize = nr_pages * PAGE_SIZE;
4b534334
KH
2342 struct mem_cgroup *mem_over_limit;
2343 struct res_counter *fail_res;
2344 unsigned long flags = 0;
2345 int ret;
2346
c0ff4b85 2347 ret = res_counter_charge(&memcg->res, csize, &fail_res);
4b534334
KH
2348
2349 if (likely(!ret)) {
2350 if (!do_swap_account)
2351 return CHARGE_OK;
c0ff4b85 2352 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
4b534334
KH
2353 if (likely(!ret))
2354 return CHARGE_OK;
2355
c0ff4b85 2356 res_counter_uncharge(&memcg->res, csize);
4b534334
KH
2357 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2358 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2359 } else
2360 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
9221edb7 2361 /*
9221edb7
JW
2362 * Never reclaim on behalf of optional batching, retry with a
2363 * single page instead.
2364 */
4c9c5359 2365 if (nr_pages > min_pages)
4b534334
KH
2366 return CHARGE_RETRY;
2367
2368 if (!(gfp_mask & __GFP_WAIT))
2369 return CHARGE_WOULDBLOCK;
2370
4c9c5359
SS
2371 if (gfp_mask & __GFP_NORETRY)
2372 return CHARGE_NOMEM;
2373
5660048c 2374 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
7ec99d62 2375 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
19942822 2376 return CHARGE_RETRY;
4b534334 2377 /*
19942822
JW
2378 * Even though the limit is exceeded at this point, reclaim
2379 * may have been able to free some pages. Retry the charge
2380 * before killing the task.
2381 *
2382 * Only for regular pages, though: huge pages are rather
2383 * unlikely to succeed so close to the limit, and we fall back
2384 * to regular pages anyway in case of failure.
4b534334 2385 */
4c9c5359 2386 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
4b534334
KH
2387 return CHARGE_RETRY;
2388
2389 /*
2390 * At task move, charge accounts can be doubly counted. So, it's
2391 * better to wait until the end of task_move if something is going on.
2392 */
2393 if (mem_cgroup_wait_acct_move(mem_over_limit))
2394 return CHARGE_RETRY;
2395
2396 /* If we don't need to call oom-killer at el, return immediately */
2397 if (!oom_check)
2398 return CHARGE_NOMEM;
2399 /* check OOM */
e845e199 2400 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask, get_order(csize)))
4b534334
KH
2401 return CHARGE_OOM_DIE;
2402
2403 return CHARGE_RETRY;
2404}
2405
f817ed48 2406/*
38c5d72f
KH
2407 * __mem_cgroup_try_charge() does
2408 * 1. detect memcg to be charged against from passed *mm and *ptr,
2409 * 2. update res_counter
2410 * 3. call memory reclaim if necessary.
2411 *
2412 * In some special case, if the task is fatal, fatal_signal_pending() or
2413 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2414 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2415 * as possible without any hazards. 2: all pages should have a valid
2416 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2417 * pointer, that is treated as a charge to root_mem_cgroup.
2418 *
2419 * So __mem_cgroup_try_charge() will return
2420 * 0 ... on success, filling *ptr with a valid memcg pointer.
2421 * -ENOMEM ... charge failure because of resource limits.
2422 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2423 *
2424 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2425 * the oom-killer can be invoked.
8a9f3ccd 2426 */
f817ed48 2427static int __mem_cgroup_try_charge(struct mm_struct *mm,
ec168510 2428 gfp_t gfp_mask,
7ec99d62 2429 unsigned int nr_pages,
c0ff4b85 2430 struct mem_cgroup **ptr,
7ec99d62 2431 bool oom)
8a9f3ccd 2432{
7ec99d62 2433 unsigned int batch = max(CHARGE_BATCH, nr_pages);
4b534334 2434 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
c0ff4b85 2435 struct mem_cgroup *memcg = NULL;
4b534334 2436 int ret;
a636b327 2437
867578cb
KH
2438 /*
2439 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2440 * in system level. So, allow to go ahead dying process in addition to
2441 * MEMDIE process.
2442 */
2443 if (unlikely(test_thread_flag(TIF_MEMDIE)
2444 || fatal_signal_pending(current)))
2445 goto bypass;
a636b327 2446
8a9f3ccd 2447 /*
3be91277
HD
2448 * We always charge the cgroup the mm_struct belongs to.
2449 * The mm_struct's mem_cgroup changes on task migration if the
8a9f3ccd 2450 * thread group leader migrates. It's possible that mm is not
24467cac 2451 * set, if so charge the root memcg (happens for pagecache usage).
8a9f3ccd 2452 */
c0ff4b85 2453 if (!*ptr && !mm)
38c5d72f 2454 *ptr = root_mem_cgroup;
f75ca962 2455again:
c0ff4b85
R
2456 if (*ptr) { /* css should be a valid one */
2457 memcg = *ptr;
c0ff4b85 2458 if (mem_cgroup_is_root(memcg))
f75ca962 2459 goto done;
a0956d54 2460 if (consume_stock(memcg, nr_pages))
f75ca962 2461 goto done;
c0ff4b85 2462 css_get(&memcg->css);
4b534334 2463 } else {
f75ca962 2464 struct task_struct *p;
54595fe2 2465
f75ca962
KH
2466 rcu_read_lock();
2467 p = rcu_dereference(mm->owner);
f75ca962 2468 /*
ebb76ce1 2469 * Because we don't have task_lock(), "p" can exit.
c0ff4b85 2470 * In that case, "memcg" can point to root or p can be NULL with
ebb76ce1
KH
2471 * race with swapoff. Then, we have small risk of mis-accouning.
2472 * But such kind of mis-account by race always happens because
2473 * we don't have cgroup_mutex(). It's overkill and we allo that
2474 * small race, here.
2475 * (*) swapoff at el will charge against mm-struct not against
2476 * task-struct. So, mm->owner can be NULL.
f75ca962 2477 */
c0ff4b85 2478 memcg = mem_cgroup_from_task(p);
38c5d72f
KH
2479 if (!memcg)
2480 memcg = root_mem_cgroup;
2481 if (mem_cgroup_is_root(memcg)) {
f75ca962
KH
2482 rcu_read_unlock();
2483 goto done;
2484 }
a0956d54 2485 if (consume_stock(memcg, nr_pages)) {
f75ca962
KH
2486 /*
2487 * It seems dagerous to access memcg without css_get().
2488 * But considering how consume_stok works, it's not
2489 * necessary. If consume_stock success, some charges
2490 * from this memcg are cached on this cpu. So, we
2491 * don't need to call css_get()/css_tryget() before
2492 * calling consume_stock().
2493 */
2494 rcu_read_unlock();
2495 goto done;
2496 }
2497 /* after here, we may be blocked. we need to get refcnt */
c0ff4b85 2498 if (!css_tryget(&memcg->css)) {
f75ca962
KH
2499 rcu_read_unlock();
2500 goto again;
2501 }
2502 rcu_read_unlock();
2503 }
8a9f3ccd 2504
4b534334
KH
2505 do {
2506 bool oom_check;
7a81b88c 2507
4b534334 2508 /* If killed, bypass charge */
f75ca962 2509 if (fatal_signal_pending(current)) {
c0ff4b85 2510 css_put(&memcg->css);
4b534334 2511 goto bypass;
f75ca962 2512 }
6d61ef40 2513
4b534334
KH
2514 oom_check = false;
2515 if (oom && !nr_oom_retries) {
2516 oom_check = true;
2517 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
cdec2e42 2518 }
66e1707b 2519
4c9c5359
SS
2520 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, nr_pages,
2521 oom_check);
4b534334
KH
2522 switch (ret) {
2523 case CHARGE_OK:
2524 break;
2525 case CHARGE_RETRY: /* not in OOM situation but retry */
7ec99d62 2526 batch = nr_pages;
c0ff4b85
R
2527 css_put(&memcg->css);
2528 memcg = NULL;
f75ca962 2529 goto again;
4b534334 2530 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
c0ff4b85 2531 css_put(&memcg->css);
4b534334
KH
2532 goto nomem;
2533 case CHARGE_NOMEM: /* OOM routine works */
f75ca962 2534 if (!oom) {
c0ff4b85 2535 css_put(&memcg->css);
867578cb 2536 goto nomem;
f75ca962 2537 }
4b534334
KH
2538 /* If oom, we never return -ENOMEM */
2539 nr_oom_retries--;
2540 break;
2541 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
c0ff4b85 2542 css_put(&memcg->css);
867578cb 2543 goto bypass;
66e1707b 2544 }
4b534334
KH
2545 } while (ret != CHARGE_OK);
2546
7ec99d62 2547 if (batch > nr_pages)
c0ff4b85
R
2548 refill_stock(memcg, batch - nr_pages);
2549 css_put(&memcg->css);
0c3e73e8 2550done:
c0ff4b85 2551 *ptr = memcg;
7a81b88c
KH
2552 return 0;
2553nomem:
c0ff4b85 2554 *ptr = NULL;
7a81b88c 2555 return -ENOMEM;
867578cb 2556bypass:
38c5d72f
KH
2557 *ptr = root_mem_cgroup;
2558 return -EINTR;
7a81b88c 2559}
8a9f3ccd 2560
a3032a2c
DN
2561/*
2562 * Somemtimes we have to undo a charge we got by try_charge().
2563 * This function is for that and do uncharge, put css's refcnt.
2564 * gotten by try_charge().
2565 */
c0ff4b85 2566static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
e7018b8d 2567 unsigned int nr_pages)
a3032a2c 2568{
c0ff4b85 2569 if (!mem_cgroup_is_root(memcg)) {
e7018b8d
JW
2570 unsigned long bytes = nr_pages * PAGE_SIZE;
2571
c0ff4b85 2572 res_counter_uncharge(&memcg->res, bytes);
a3032a2c 2573 if (do_swap_account)
c0ff4b85 2574 res_counter_uncharge(&memcg->memsw, bytes);
a3032a2c 2575 }
854ffa8d
DN
2576}
2577
d01dd17f
KH
2578/*
2579 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2580 * This is useful when moving usage to parent cgroup.
2581 */
2582static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2583 unsigned int nr_pages)
2584{
2585 unsigned long bytes = nr_pages * PAGE_SIZE;
2586
2587 if (mem_cgroup_is_root(memcg))
2588 return;
2589
2590 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2591 if (do_swap_account)
2592 res_counter_uncharge_until(&memcg->memsw,
2593 memcg->memsw.parent, bytes);
2594}
2595
a3b2d692
KH
2596/*
2597 * A helper function to get mem_cgroup from ID. must be called under
e9316080
TH
2598 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2599 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2600 * called against removed memcg.)
a3b2d692
KH
2601 */
2602static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2603{
2604 struct cgroup_subsys_state *css;
2605
2606 /* ID 0 is unused ID */
2607 if (!id)
2608 return NULL;
2609 css = css_lookup(&mem_cgroup_subsys, id);
2610 if (!css)
2611 return NULL;
b2145145 2612 return mem_cgroup_from_css(css);
a3b2d692
KH
2613}
2614
e42d9d5d 2615struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
b5a84319 2616{
c0ff4b85 2617 struct mem_cgroup *memcg = NULL;
3c776e64 2618 struct page_cgroup *pc;
a3b2d692 2619 unsigned short id;
b5a84319
KH
2620 swp_entry_t ent;
2621
3c776e64
DN
2622 VM_BUG_ON(!PageLocked(page));
2623
3c776e64 2624 pc = lookup_page_cgroup(page);
c0bd3f63 2625 lock_page_cgroup(pc);
a3b2d692 2626 if (PageCgroupUsed(pc)) {
c0ff4b85
R
2627 memcg = pc->mem_cgroup;
2628 if (memcg && !css_tryget(&memcg->css))
2629 memcg = NULL;
e42d9d5d 2630 } else if (PageSwapCache(page)) {
3c776e64 2631 ent.val = page_private(page);
9fb4b7cc 2632 id = lookup_swap_cgroup_id(ent);
a3b2d692 2633 rcu_read_lock();
c0ff4b85
R
2634 memcg = mem_cgroup_lookup(id);
2635 if (memcg && !css_tryget(&memcg->css))
2636 memcg = NULL;
a3b2d692 2637 rcu_read_unlock();
3c776e64 2638 }
c0bd3f63 2639 unlock_page_cgroup(pc);
c0ff4b85 2640 return memcg;
b5a84319
KH
2641}
2642
c0ff4b85 2643static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
5564e88b 2644 struct page *page,
7ec99d62 2645 unsigned int nr_pages,
9ce70c02
HD
2646 enum charge_type ctype,
2647 bool lrucare)
7a81b88c 2648{
ce587e65 2649 struct page_cgroup *pc = lookup_page_cgroup(page);
9ce70c02 2650 struct zone *uninitialized_var(zone);
fa9add64 2651 struct lruvec *lruvec;
9ce70c02 2652 bool was_on_lru = false;
b2402857 2653 bool anon;
9ce70c02 2654
ca3e0214 2655 lock_page_cgroup(pc);
90deb788 2656 VM_BUG_ON(PageCgroupUsed(pc));
ca3e0214
KH
2657 /*
2658 * we don't need page_cgroup_lock about tail pages, becase they are not
2659 * accessed by any other context at this point.
2660 */
9ce70c02
HD
2661
2662 /*
2663 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2664 * may already be on some other mem_cgroup's LRU. Take care of it.
2665 */
2666 if (lrucare) {
2667 zone = page_zone(page);
2668 spin_lock_irq(&zone->lru_lock);
2669 if (PageLRU(page)) {
fa9add64 2670 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
9ce70c02 2671 ClearPageLRU(page);
fa9add64 2672 del_page_from_lru_list(page, lruvec, page_lru(page));
9ce70c02
HD
2673 was_on_lru = true;
2674 }
2675 }
2676
c0ff4b85 2677 pc->mem_cgroup = memcg;
261fb61a
KH
2678 /*
2679 * We access a page_cgroup asynchronously without lock_page_cgroup().
2680 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2681 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2682 * before USED bit, we need memory barrier here.
2683 * See mem_cgroup_add_lru_list(), etc.
2684 */
08e552c6 2685 smp_wmb();
b2402857 2686 SetPageCgroupUsed(pc);
3be91277 2687
9ce70c02
HD
2688 if (lrucare) {
2689 if (was_on_lru) {
fa9add64 2690 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
9ce70c02
HD
2691 VM_BUG_ON(PageLRU(page));
2692 SetPageLRU(page);
fa9add64 2693 add_page_to_lru_list(page, lruvec, page_lru(page));
9ce70c02
HD
2694 }
2695 spin_unlock_irq(&zone->lru_lock);
2696 }
2697
41326c17 2698 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
b2402857
KH
2699 anon = true;
2700 else
2701 anon = false;
2702
2703 mem_cgroup_charge_statistics(memcg, anon, nr_pages);
52d4b9ac 2704 unlock_page_cgroup(pc);
9ce70c02 2705
430e4863
KH
2706 /*
2707 * "charge_statistics" updated event counter. Then, check it.
2708 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2709 * if they exceeds softlimit.
2710 */
c0ff4b85 2711 memcg_check_events(memcg, page);
7a81b88c 2712}
66e1707b 2713
7ae1e1d0
GC
2714#ifdef CONFIG_MEMCG_KMEM
2715static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2716{
2717 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2718 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2719}
2720
2721static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
2722{
2723 struct res_counter *fail_res;
2724 struct mem_cgroup *_memcg;
2725 int ret = 0;
2726 bool may_oom;
2727
2728 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
2729 if (ret)
2730 return ret;
2731
2732 /*
2733 * Conditions under which we can wait for the oom_killer. Those are
2734 * the same conditions tested by the core page allocator
2735 */
2736 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
2737
2738 _memcg = memcg;
2739 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
2740 &_memcg, may_oom);
2741
2742 if (ret == -EINTR) {
2743 /*
2744 * __mem_cgroup_try_charge() chosed to bypass to root due to
2745 * OOM kill or fatal signal. Since our only options are to
2746 * either fail the allocation or charge it to this cgroup, do
2747 * it as a temporary condition. But we can't fail. From a
2748 * kmem/slab perspective, the cache has already been selected,
2749 * by mem_cgroup_kmem_get_cache(), so it is too late to change
2750 * our minds.
2751 *
2752 * This condition will only trigger if the task entered
2753 * memcg_charge_kmem in a sane state, but was OOM-killed during
2754 * __mem_cgroup_try_charge() above. Tasks that were already
2755 * dying when the allocation triggers should have been already
2756 * directed to the root cgroup in memcontrol.h
2757 */
2758 res_counter_charge_nofail(&memcg->res, size, &fail_res);
2759 if (do_swap_account)
2760 res_counter_charge_nofail(&memcg->memsw, size,
2761 &fail_res);
2762 ret = 0;
2763 } else if (ret)
2764 res_counter_uncharge(&memcg->kmem, size);
2765
2766 return ret;
2767}
2768
2769static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
2770{
7ae1e1d0
GC
2771 res_counter_uncharge(&memcg->res, size);
2772 if (do_swap_account)
2773 res_counter_uncharge(&memcg->memsw, size);
7de37682
GC
2774
2775 /* Not down to 0 */
2776 if (res_counter_uncharge(&memcg->kmem, size))
2777 return;
2778
2779 if (memcg_kmem_test_and_clear_dead(memcg))
2780 mem_cgroup_put(memcg);
7ae1e1d0
GC
2781}
2782
2783/*
2784 * We need to verify if the allocation against current->mm->owner's memcg is
2785 * possible for the given order. But the page is not allocated yet, so we'll
2786 * need a further commit step to do the final arrangements.
2787 *
2788 * It is possible for the task to switch cgroups in this mean time, so at
2789 * commit time, we can't rely on task conversion any longer. We'll then use
2790 * the handle argument to return to the caller which cgroup we should commit
2791 * against. We could also return the memcg directly and avoid the pointer
2792 * passing, but a boolean return value gives better semantics considering
2793 * the compiled-out case as well.
2794 *
2795 * Returning true means the allocation is possible.
2796 */
2797bool
2798__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
2799{
2800 struct mem_cgroup *memcg;
2801 int ret;
2802
2803 *_memcg = NULL;
2804 memcg = try_get_mem_cgroup_from_mm(current->mm);
2805
2806 /*
2807 * very rare case described in mem_cgroup_from_task. Unfortunately there
2808 * isn't much we can do without complicating this too much, and it would
2809 * be gfp-dependent anyway. Just let it go
2810 */
2811 if (unlikely(!memcg))
2812 return true;
2813
2814 if (!memcg_can_account_kmem(memcg)) {
2815 css_put(&memcg->css);
2816 return true;
2817 }
2818
7ae1e1d0
GC
2819 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
2820 if (!ret)
2821 *_memcg = memcg;
7ae1e1d0
GC
2822
2823 css_put(&memcg->css);
2824 return (ret == 0);
2825}
2826
2827void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
2828 int order)
2829{
2830 struct page_cgroup *pc;
2831
2832 VM_BUG_ON(mem_cgroup_is_root(memcg));
2833
2834 /* The page allocation failed. Revert */
2835 if (!page) {
2836 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
7ae1e1d0
GC
2837 return;
2838 }
2839
2840 pc = lookup_page_cgroup(page);
2841 lock_page_cgroup(pc);
2842 pc->mem_cgroup = memcg;
2843 SetPageCgroupUsed(pc);
2844 unlock_page_cgroup(pc);
2845}
2846
2847void __memcg_kmem_uncharge_pages(struct page *page, int order)
2848{
2849 struct mem_cgroup *memcg = NULL;
2850 struct page_cgroup *pc;
2851
2852
2853 pc = lookup_page_cgroup(page);
2854 /*
2855 * Fast unlocked return. Theoretically might have changed, have to
2856 * check again after locking.
2857 */
2858 if (!PageCgroupUsed(pc))
2859 return;
2860
2861 lock_page_cgroup(pc);
2862 if (PageCgroupUsed(pc)) {
2863 memcg = pc->mem_cgroup;
2864 ClearPageCgroupUsed(pc);
2865 }
2866 unlock_page_cgroup(pc);
2867
2868 /*
2869 * We trust that only if there is a memcg associated with the page, it
2870 * is a valid allocation
2871 */
2872 if (!memcg)
2873 return;
2874
2875 VM_BUG_ON(mem_cgroup_is_root(memcg));
2876 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
7ae1e1d0
GC
2877}
2878#endif /* CONFIG_MEMCG_KMEM */
2879
ca3e0214
KH
2880#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2881
a0db00fc 2882#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
ca3e0214
KH
2883/*
2884 * Because tail pages are not marked as "used", set it. We're under
e94c8a9c
KH
2885 * zone->lru_lock, 'splitting on pmd' and compound_lock.
2886 * charge/uncharge will be never happen and move_account() is done under
2887 * compound_lock(), so we don't have to take care of races.
ca3e0214 2888 */
e94c8a9c 2889void mem_cgroup_split_huge_fixup(struct page *head)
ca3e0214
KH
2890{
2891 struct page_cgroup *head_pc = lookup_page_cgroup(head);
e94c8a9c
KH
2892 struct page_cgroup *pc;
2893 int i;
ca3e0214 2894
3d37c4a9
KH
2895 if (mem_cgroup_disabled())
2896 return;
e94c8a9c
KH
2897 for (i = 1; i < HPAGE_PMD_NR; i++) {
2898 pc = head_pc + i;
2899 pc->mem_cgroup = head_pc->mem_cgroup;
2900 smp_wmb();/* see __commit_charge() */
e94c8a9c
KH
2901 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
2902 }
ca3e0214 2903}
12d27107 2904#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
ca3e0214 2905
f817ed48 2906/**
de3638d9 2907 * mem_cgroup_move_account - move account of the page
5564e88b 2908 * @page: the page
7ec99d62 2909 * @nr_pages: number of regular pages (>1 for huge pages)
f817ed48
KH
2910 * @pc: page_cgroup of the page.
2911 * @from: mem_cgroup which the page is moved from.
2912 * @to: mem_cgroup which the page is moved to. @from != @to.
2913 *
2914 * The caller must confirm following.
08e552c6 2915 * - page is not on LRU (isolate_page() is useful.)
7ec99d62 2916 * - compound_lock is held when nr_pages > 1
f817ed48 2917 *
2f3479b1
KH
2918 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
2919 * from old cgroup.
f817ed48 2920 */
7ec99d62
JW
2921static int mem_cgroup_move_account(struct page *page,
2922 unsigned int nr_pages,
2923 struct page_cgroup *pc,
2924 struct mem_cgroup *from,
2f3479b1 2925 struct mem_cgroup *to)
f817ed48 2926{
de3638d9
JW
2927 unsigned long flags;
2928 int ret;
b2402857 2929 bool anon = PageAnon(page);
987eba66 2930
f817ed48 2931 VM_BUG_ON(from == to);
5564e88b 2932 VM_BUG_ON(PageLRU(page));
de3638d9
JW
2933 /*
2934 * The page is isolated from LRU. So, collapse function
2935 * will not handle this page. But page splitting can happen.
2936 * Do this check under compound_page_lock(). The caller should
2937 * hold it.
2938 */
2939 ret = -EBUSY;
7ec99d62 2940 if (nr_pages > 1 && !PageTransHuge(page))
de3638d9
JW
2941 goto out;
2942
2943 lock_page_cgroup(pc);
2944
2945 ret = -EINVAL;
2946 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
2947 goto unlock;
2948
312734c0 2949 move_lock_mem_cgroup(from, &flags);
f817ed48 2950
2ff76f11 2951 if (!anon && page_mapped(page)) {
c62b1a3b
KH
2952 /* Update mapped_file data for mem_cgroup */
2953 preempt_disable();
2954 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2955 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2956 preempt_enable();
d69b042f 2957 }
b2402857 2958 mem_cgroup_charge_statistics(from, anon, -nr_pages);
d69b042f 2959
854ffa8d 2960 /* caller should have done css_get */
08e552c6 2961 pc->mem_cgroup = to;
b2402857 2962 mem_cgroup_charge_statistics(to, anon, nr_pages);
312734c0 2963 move_unlock_mem_cgroup(from, &flags);
de3638d9
JW
2964 ret = 0;
2965unlock:
57f9fd7d 2966 unlock_page_cgroup(pc);
d2265e6f
KH
2967 /*
2968 * check events
2969 */
5564e88b
JW
2970 memcg_check_events(to, page);
2971 memcg_check_events(from, page);
de3638d9 2972out:
f817ed48
KH
2973 return ret;
2974}
2975
2ef37d3f
MH
2976/**
2977 * mem_cgroup_move_parent - moves page to the parent group
2978 * @page: the page to move
2979 * @pc: page_cgroup of the page
2980 * @child: page's cgroup
2981 *
2982 * move charges to its parent or the root cgroup if the group has no
2983 * parent (aka use_hierarchy==0).
2984 * Although this might fail (get_page_unless_zero, isolate_lru_page or
2985 * mem_cgroup_move_account fails) the failure is always temporary and
2986 * it signals a race with a page removal/uncharge or migration. In the
2987 * first case the page is on the way out and it will vanish from the LRU
2988 * on the next attempt and the call should be retried later.
2989 * Isolation from the LRU fails only if page has been isolated from
2990 * the LRU since we looked at it and that usually means either global
2991 * reclaim or migration going on. The page will either get back to the
2992 * LRU or vanish.
2993 * Finaly mem_cgroup_move_account fails only if the page got uncharged
2994 * (!PageCgroupUsed) or moved to a different group. The page will
2995 * disappear in the next attempt.
f817ed48 2996 */
5564e88b
JW
2997static int mem_cgroup_move_parent(struct page *page,
2998 struct page_cgroup *pc,
6068bf01 2999 struct mem_cgroup *child)
f817ed48 3000{
f817ed48 3001 struct mem_cgroup *parent;
7ec99d62 3002 unsigned int nr_pages;
4be4489f 3003 unsigned long uninitialized_var(flags);
f817ed48
KH
3004 int ret;
3005
d8423011 3006 VM_BUG_ON(mem_cgroup_is_root(child));
f817ed48 3007
57f9fd7d
DN
3008 ret = -EBUSY;
3009 if (!get_page_unless_zero(page))
3010 goto out;
3011 if (isolate_lru_page(page))
3012 goto put;
52dbb905 3013
7ec99d62 3014 nr_pages = hpage_nr_pages(page);
08e552c6 3015
cc926f78
KH
3016 parent = parent_mem_cgroup(child);
3017 /*
3018 * If no parent, move charges to root cgroup.
3019 */
3020 if (!parent)
3021 parent = root_mem_cgroup;
f817ed48 3022
2ef37d3f
MH
3023 if (nr_pages > 1) {
3024 VM_BUG_ON(!PageTransHuge(page));
987eba66 3025 flags = compound_lock_irqsave(page);
2ef37d3f 3026 }
987eba66 3027
cc926f78 3028 ret = mem_cgroup_move_account(page, nr_pages,
2f3479b1 3029 pc, child, parent);
cc926f78
KH
3030 if (!ret)
3031 __mem_cgroup_cancel_local_charge(child, nr_pages);
8dba474f 3032
7ec99d62 3033 if (nr_pages > 1)
987eba66 3034 compound_unlock_irqrestore(page, flags);
08e552c6 3035 putback_lru_page(page);
57f9fd7d 3036put:
40d58138 3037 put_page(page);
57f9fd7d 3038out:
f817ed48
KH
3039 return ret;
3040}
3041
7a81b88c
KH
3042/*
3043 * Charge the memory controller for page usage.
3044 * Return
3045 * 0 if the charge was successful
3046 * < 0 if the cgroup is over its limit
3047 */
3048static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
73045c47 3049 gfp_t gfp_mask, enum charge_type ctype)
7a81b88c 3050{
c0ff4b85 3051 struct mem_cgroup *memcg = NULL;
7ec99d62 3052 unsigned int nr_pages = 1;
8493ae43 3053 bool oom = true;
7a81b88c 3054 int ret;
ec168510 3055
37c2ac78 3056 if (PageTransHuge(page)) {
7ec99d62 3057 nr_pages <<= compound_order(page);
37c2ac78 3058 VM_BUG_ON(!PageTransHuge(page));
8493ae43
JW
3059 /*
3060 * Never OOM-kill a process for a huge page. The
3061 * fault handler will fall back to regular pages.
3062 */
3063 oom = false;
37c2ac78 3064 }
7a81b88c 3065
c0ff4b85 3066 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
38c5d72f 3067 if (ret == -ENOMEM)
7a81b88c 3068 return ret;
ce587e65 3069 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
8a9f3ccd 3070 return 0;
8a9f3ccd
BS
3071}
3072
7a81b88c
KH
3073int mem_cgroup_newpage_charge(struct page *page,
3074 struct mm_struct *mm, gfp_t gfp_mask)
217bc319 3075{
f8d66542 3076 if (mem_cgroup_disabled())
cede86ac 3077 return 0;
7a0524cf
JW
3078 VM_BUG_ON(page_mapped(page));
3079 VM_BUG_ON(page->mapping && !PageAnon(page));
3080 VM_BUG_ON(!mm);
217bc319 3081 return mem_cgroup_charge_common(page, mm, gfp_mask,
41326c17 3082 MEM_CGROUP_CHARGE_TYPE_ANON);
217bc319
KH
3083}
3084
54595fe2
KH
3085/*
3086 * While swap-in, try_charge -> commit or cancel, the page is locked.
3087 * And when try_charge() successfully returns, one refcnt to memcg without
21ae2956 3088 * struct page_cgroup is acquired. This refcnt will be consumed by
54595fe2
KH
3089 * "commit()" or removed by "cancel()"
3090 */
0435a2fd
JW
3091static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3092 struct page *page,
3093 gfp_t mask,
3094 struct mem_cgroup **memcgp)
8c7c6e34 3095{
c0ff4b85 3096 struct mem_cgroup *memcg;
90deb788 3097 struct page_cgroup *pc;
54595fe2 3098 int ret;
8c7c6e34 3099
90deb788
JW
3100 pc = lookup_page_cgroup(page);
3101 /*
3102 * Every swap fault against a single page tries to charge the
3103 * page, bail as early as possible. shmem_unuse() encounters
3104 * already charged pages, too. The USED bit is protected by
3105 * the page lock, which serializes swap cache removal, which
3106 * in turn serializes uncharging.
3107 */
3108 if (PageCgroupUsed(pc))
3109 return 0;
8c7c6e34
KH
3110 if (!do_swap_account)
3111 goto charge_cur_mm;
c0ff4b85
R
3112 memcg = try_get_mem_cgroup_from_page(page);
3113 if (!memcg)
54595fe2 3114 goto charge_cur_mm;
72835c86
JW
3115 *memcgp = memcg;
3116 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
c0ff4b85 3117 css_put(&memcg->css);
38c5d72f
KH
3118 if (ret == -EINTR)
3119 ret = 0;
54595fe2 3120 return ret;
8c7c6e34 3121charge_cur_mm:
38c5d72f
KH
3122 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
3123 if (ret == -EINTR)
3124 ret = 0;
3125 return ret;
8c7c6e34
KH
3126}
3127
0435a2fd
JW
3128int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
3129 gfp_t gfp_mask, struct mem_cgroup **memcgp)
3130{
3131 *memcgp = NULL;
3132 if (mem_cgroup_disabled())
3133 return 0;
bdf4f4d2
JW
3134 /*
3135 * A racing thread's fault, or swapoff, may have already
3136 * updated the pte, and even removed page from swap cache: in
3137 * those cases unuse_pte()'s pte_same() test will fail; but
3138 * there's also a KSM case which does need to charge the page.
3139 */
3140 if (!PageSwapCache(page)) {
3141 int ret;
3142
3143 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
3144 if (ret == -EINTR)
3145 ret = 0;
3146 return ret;
3147 }
0435a2fd
JW
3148 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
3149}
3150
827a03d2
JW
3151void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
3152{
3153 if (mem_cgroup_disabled())
3154 return;
3155 if (!memcg)
3156 return;
3157 __mem_cgroup_cancel_charge(memcg, 1);
3158}
3159
83aae4c7 3160static void
72835c86 3161__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
83aae4c7 3162 enum charge_type ctype)
7a81b88c 3163{
f8d66542 3164 if (mem_cgroup_disabled())
7a81b88c 3165 return;
72835c86 3166 if (!memcg)
7a81b88c 3167 return;
5a6475a4 3168
ce587e65 3169 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
8c7c6e34
KH
3170 /*
3171 * Now swap is on-memory. This means this page may be
3172 * counted both as mem and swap....double count.
03f3c433
KH
3173 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
3174 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
3175 * may call delete_from_swap_cache() before reach here.
8c7c6e34 3176 */
03f3c433 3177 if (do_swap_account && PageSwapCache(page)) {
8c7c6e34 3178 swp_entry_t ent = {.val = page_private(page)};
86493009 3179 mem_cgroup_uncharge_swap(ent);
8c7c6e34 3180 }
7a81b88c
KH
3181}
3182
72835c86
JW
3183void mem_cgroup_commit_charge_swapin(struct page *page,
3184 struct mem_cgroup *memcg)
83aae4c7 3185{
72835c86 3186 __mem_cgroup_commit_charge_swapin(page, memcg,
41326c17 3187 MEM_CGROUP_CHARGE_TYPE_ANON);
83aae4c7
DN
3188}
3189
827a03d2
JW
3190int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
3191 gfp_t gfp_mask)
7a81b88c 3192{
827a03d2
JW
3193 struct mem_cgroup *memcg = NULL;
3194 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
3195 int ret;
3196
f8d66542 3197 if (mem_cgroup_disabled())
827a03d2
JW
3198 return 0;
3199 if (PageCompound(page))
3200 return 0;
3201
827a03d2
JW
3202 if (!PageSwapCache(page))
3203 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
3204 else { /* page is swapcache/shmem */
0435a2fd
JW
3205 ret = __mem_cgroup_try_charge_swapin(mm, page,
3206 gfp_mask, &memcg);
827a03d2
JW
3207 if (!ret)
3208 __mem_cgroup_commit_charge_swapin(page, memcg, type);
3209 }
3210 return ret;
7a81b88c
KH
3211}
3212
c0ff4b85 3213static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
7ec99d62
JW
3214 unsigned int nr_pages,
3215 const enum charge_type ctype)
569b846d
KH
3216{
3217 struct memcg_batch_info *batch = NULL;
3218 bool uncharge_memsw = true;
7ec99d62 3219
569b846d
KH
3220 /* If swapout, usage of swap doesn't decrease */
3221 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
3222 uncharge_memsw = false;
569b846d
KH
3223
3224 batch = &current->memcg_batch;
3225 /*
3226 * In usual, we do css_get() when we remember memcg pointer.
3227 * But in this case, we keep res->usage until end of a series of
3228 * uncharges. Then, it's ok to ignore memcg's refcnt.
3229 */
3230 if (!batch->memcg)
c0ff4b85 3231 batch->memcg = memcg;
3c11ecf4
KH
3232 /*
3233 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
25985edc 3234 * In those cases, all pages freed continuously can be expected to be in
3c11ecf4
KH
3235 * the same cgroup and we have chance to coalesce uncharges.
3236 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
3237 * because we want to do uncharge as soon as possible.
3238 */
3239
3240 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
3241 goto direct_uncharge;
3242
7ec99d62 3243 if (nr_pages > 1)
ec168510
AA
3244 goto direct_uncharge;
3245
569b846d
KH
3246 /*
3247 * In typical case, batch->memcg == mem. This means we can
3248 * merge a series of uncharges to an uncharge of res_counter.
3249 * If not, we uncharge res_counter ony by one.
3250 */
c0ff4b85 3251 if (batch->memcg != memcg)
569b846d
KH
3252 goto direct_uncharge;
3253 /* remember freed charge and uncharge it later */
7ffd4ca7 3254 batch->nr_pages++;
569b846d 3255 if (uncharge_memsw)
7ffd4ca7 3256 batch->memsw_nr_pages++;
569b846d
KH
3257 return;
3258direct_uncharge:
c0ff4b85 3259 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
569b846d 3260 if (uncharge_memsw)
c0ff4b85
R
3261 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
3262 if (unlikely(batch->memcg != memcg))
3263 memcg_oom_recover(memcg);
569b846d 3264}
7a81b88c 3265
8a9f3ccd 3266/*
69029cd5 3267 * uncharge if !page_mapped(page)
8a9f3ccd 3268 */
8c7c6e34 3269static struct mem_cgroup *
0030f535
JW
3270__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
3271 bool end_migration)
8a9f3ccd 3272{
c0ff4b85 3273 struct mem_cgroup *memcg = NULL;
7ec99d62
JW
3274 unsigned int nr_pages = 1;
3275 struct page_cgroup *pc;
b2402857 3276 bool anon;
8a9f3ccd 3277
f8d66542 3278 if (mem_cgroup_disabled())
8c7c6e34 3279 return NULL;
4077960e 3280
0c59b89c 3281 VM_BUG_ON(PageSwapCache(page));
d13d1443 3282
37c2ac78 3283 if (PageTransHuge(page)) {
7ec99d62 3284 nr_pages <<= compound_order(page);
37c2ac78
AA
3285 VM_BUG_ON(!PageTransHuge(page));
3286 }
8697d331 3287 /*
3c541e14 3288 * Check if our page_cgroup is valid
8697d331 3289 */
52d4b9ac 3290 pc = lookup_page_cgroup(page);
cfa44946 3291 if (unlikely(!PageCgroupUsed(pc)))
8c7c6e34 3292 return NULL;
b9c565d5 3293
52d4b9ac 3294 lock_page_cgroup(pc);
d13d1443 3295
c0ff4b85 3296 memcg = pc->mem_cgroup;
8c7c6e34 3297
d13d1443
KH
3298 if (!PageCgroupUsed(pc))
3299 goto unlock_out;
3300
b2402857
KH
3301 anon = PageAnon(page);
3302
d13d1443 3303 switch (ctype) {
41326c17 3304 case MEM_CGROUP_CHARGE_TYPE_ANON:
2ff76f11
KH
3305 /*
3306 * Generally PageAnon tells if it's the anon statistics to be
3307 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
3308 * used before page reached the stage of being marked PageAnon.
3309 */
b2402857
KH
3310 anon = true;
3311 /* fallthrough */
8a9478ca 3312 case MEM_CGROUP_CHARGE_TYPE_DROP:
ac39cf8c 3313 /* See mem_cgroup_prepare_migration() */
0030f535
JW
3314 if (page_mapped(page))
3315 goto unlock_out;
3316 /*
3317 * Pages under migration may not be uncharged. But
3318 * end_migration() /must/ be the one uncharging the
3319 * unused post-migration page and so it has to call
3320 * here with the migration bit still set. See the
3321 * res_counter handling below.
3322 */
3323 if (!end_migration && PageCgroupMigration(pc))
d13d1443
KH
3324 goto unlock_out;
3325 break;
3326 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
3327 if (!PageAnon(page)) { /* Shared memory */
3328 if (page->mapping && !page_is_file_cache(page))
3329 goto unlock_out;
3330 } else if (page_mapped(page)) /* Anon */
3331 goto unlock_out;
3332 break;
3333 default:
3334 break;
52d4b9ac 3335 }
d13d1443 3336
b2402857 3337 mem_cgroup_charge_statistics(memcg, anon, -nr_pages);
04046e1a 3338
52d4b9ac 3339 ClearPageCgroupUsed(pc);
544122e5
KH
3340 /*
3341 * pc->mem_cgroup is not cleared here. It will be accessed when it's
3342 * freed from LRU. This is safe because uncharged page is expected not
3343 * to be reused (freed soon). Exception is SwapCache, it's handled by
3344 * special functions.
3345 */
b9c565d5 3346
52d4b9ac 3347 unlock_page_cgroup(pc);
f75ca962 3348 /*
c0ff4b85 3349 * even after unlock, we have memcg->res.usage here and this memcg
f75ca962
KH
3350 * will never be freed.
3351 */
c0ff4b85 3352 memcg_check_events(memcg, page);
f75ca962 3353 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
c0ff4b85
R
3354 mem_cgroup_swap_statistics(memcg, true);
3355 mem_cgroup_get(memcg);
f75ca962 3356 }
0030f535
JW
3357 /*
3358 * Migration does not charge the res_counter for the
3359 * replacement page, so leave it alone when phasing out the
3360 * page that is unused after the migration.
3361 */
3362 if (!end_migration && !mem_cgroup_is_root(memcg))
c0ff4b85 3363 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
6d12e2d8 3364
c0ff4b85 3365 return memcg;
d13d1443
KH
3366
3367unlock_out:
3368 unlock_page_cgroup(pc);
8c7c6e34 3369 return NULL;
3c541e14
BS
3370}
3371
69029cd5
KH
3372void mem_cgroup_uncharge_page(struct page *page)
3373{
52d4b9ac
KH
3374 /* early check. */
3375 if (page_mapped(page))
3376 return;
40f23a21 3377 VM_BUG_ON(page->mapping && !PageAnon(page));
0c59b89c
JW
3378 if (PageSwapCache(page))
3379 return;
0030f535 3380 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
69029cd5
KH
3381}
3382
3383void mem_cgroup_uncharge_cache_page(struct page *page)
3384{
3385 VM_BUG_ON(page_mapped(page));
b7abea96 3386 VM_BUG_ON(page->mapping);
0030f535 3387 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
69029cd5
KH
3388}
3389
569b846d
KH
3390/*
3391 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3392 * In that cases, pages are freed continuously and we can expect pages
3393 * are in the same memcg. All these calls itself limits the number of
3394 * pages freed at once, then uncharge_start/end() is called properly.
3395 * This may be called prural(2) times in a context,
3396 */
3397
3398void mem_cgroup_uncharge_start(void)
3399{
3400 current->memcg_batch.do_batch++;
3401 /* We can do nest. */
3402 if (current->memcg_batch.do_batch == 1) {
3403 current->memcg_batch.memcg = NULL;
7ffd4ca7
JW
3404 current->memcg_batch.nr_pages = 0;
3405 current->memcg_batch.memsw_nr_pages = 0;
569b846d
KH
3406 }
3407}
3408
3409void mem_cgroup_uncharge_end(void)
3410{
3411 struct memcg_batch_info *batch = &current->memcg_batch;
3412
3413 if (!batch->do_batch)
3414 return;
3415
3416 batch->do_batch--;
3417 if (batch->do_batch) /* If stacked, do nothing. */
3418 return;
3419
3420 if (!batch->memcg)
3421 return;
3422 /*
3423 * This "batch->memcg" is valid without any css_get/put etc...
3424 * bacause we hide charges behind us.
3425 */
7ffd4ca7
JW
3426 if (batch->nr_pages)
3427 res_counter_uncharge(&batch->memcg->res,
3428 batch->nr_pages * PAGE_SIZE);
3429 if (batch->memsw_nr_pages)
3430 res_counter_uncharge(&batch->memcg->memsw,
3431 batch->memsw_nr_pages * PAGE_SIZE);
3c11ecf4 3432 memcg_oom_recover(batch->memcg);
569b846d
KH
3433 /* forget this pointer (for sanity check) */
3434 batch->memcg = NULL;
3435}
3436
e767e056 3437#ifdef CONFIG_SWAP
8c7c6e34 3438/*
e767e056 3439 * called after __delete_from_swap_cache() and drop "page" account.
8c7c6e34
KH
3440 * memcg information is recorded to swap_cgroup of "ent"
3441 */
8a9478ca
KH
3442void
3443mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
8c7c6e34
KH
3444{
3445 struct mem_cgroup *memcg;
8a9478ca
KH
3446 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
3447
3448 if (!swapout) /* this was a swap cache but the swap is unused ! */
3449 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
3450
0030f535 3451 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
8c7c6e34 3452
f75ca962
KH
3453 /*
3454 * record memcg information, if swapout && memcg != NULL,
3455 * mem_cgroup_get() was called in uncharge().
3456 */
3457 if (do_swap_account && swapout && memcg)
a3b2d692 3458 swap_cgroup_record(ent, css_id(&memcg->css));
8c7c6e34 3459}
e767e056 3460#endif
8c7c6e34 3461
c255a458 3462#ifdef CONFIG_MEMCG_SWAP
8c7c6e34
KH
3463/*
3464 * called from swap_entry_free(). remove record in swap_cgroup and
3465 * uncharge "memsw" account.
3466 */
3467void mem_cgroup_uncharge_swap(swp_entry_t ent)
d13d1443 3468{
8c7c6e34 3469 struct mem_cgroup *memcg;
a3b2d692 3470 unsigned short id;
8c7c6e34
KH
3471
3472 if (!do_swap_account)
3473 return;
3474
a3b2d692
KH
3475 id = swap_cgroup_record(ent, 0);
3476 rcu_read_lock();
3477 memcg = mem_cgroup_lookup(id);
8c7c6e34 3478 if (memcg) {
a3b2d692
KH
3479 /*
3480 * We uncharge this because swap is freed.
3481 * This memcg can be obsolete one. We avoid calling css_tryget
3482 */
0c3e73e8 3483 if (!mem_cgroup_is_root(memcg))
4e649152 3484 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
0c3e73e8 3485 mem_cgroup_swap_statistics(memcg, false);
8c7c6e34
KH
3486 mem_cgroup_put(memcg);
3487 }
a3b2d692 3488 rcu_read_unlock();
d13d1443 3489}
02491447
DN
3490
3491/**
3492 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3493 * @entry: swap entry to be moved
3494 * @from: mem_cgroup which the entry is moved from
3495 * @to: mem_cgroup which the entry is moved to
3496 *
3497 * It succeeds only when the swap_cgroup's record for this entry is the same
3498 * as the mem_cgroup's id of @from.
3499 *
3500 * Returns 0 on success, -EINVAL on failure.
3501 *
3502 * The caller must have charged to @to, IOW, called res_counter_charge() about
3503 * both res and memsw, and called css_get().
3504 */
3505static int mem_cgroup_move_swap_account(swp_entry_t entry,
e91cbb42 3506 struct mem_cgroup *from, struct mem_cgroup *to)
02491447
DN
3507{
3508 unsigned short old_id, new_id;
3509
3510 old_id = css_id(&from->css);
3511 new_id = css_id(&to->css);
3512
3513 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
02491447 3514 mem_cgroup_swap_statistics(from, false);
483c30b5 3515 mem_cgroup_swap_statistics(to, true);
02491447 3516 /*
483c30b5
DN
3517 * This function is only called from task migration context now.
3518 * It postpones res_counter and refcount handling till the end
3519 * of task migration(mem_cgroup_clear_mc()) for performance
3520 * improvement. But we cannot postpone mem_cgroup_get(to)
3521 * because if the process that has been moved to @to does
3522 * swap-in, the refcount of @to might be decreased to 0.
02491447 3523 */
02491447 3524 mem_cgroup_get(to);
02491447
DN
3525 return 0;
3526 }
3527 return -EINVAL;
3528}
3529#else
3530static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
e91cbb42 3531 struct mem_cgroup *from, struct mem_cgroup *to)
02491447
DN
3532{
3533 return -EINVAL;
3534}
8c7c6e34 3535#endif
d13d1443 3536
ae41be37 3537/*
01b1ae63
KH
3538 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3539 * page belongs to.
ae41be37 3540 */
0030f535
JW
3541void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
3542 struct mem_cgroup **memcgp)
ae41be37 3543{
c0ff4b85 3544 struct mem_cgroup *memcg = NULL;
b32967ff 3545 unsigned int nr_pages = 1;
7ec99d62 3546 struct page_cgroup *pc;
ac39cf8c 3547 enum charge_type ctype;
8869b8f6 3548
72835c86 3549 *memcgp = NULL;
56039efa 3550
f8d66542 3551 if (mem_cgroup_disabled())
0030f535 3552 return;
4077960e 3553
b32967ff
MG
3554 if (PageTransHuge(page))
3555 nr_pages <<= compound_order(page);
3556
52d4b9ac
KH
3557 pc = lookup_page_cgroup(page);
3558 lock_page_cgroup(pc);
3559 if (PageCgroupUsed(pc)) {
c0ff4b85
R
3560 memcg = pc->mem_cgroup;
3561 css_get(&memcg->css);
ac39cf8c
AM
3562 /*
3563 * At migrating an anonymous page, its mapcount goes down
3564 * to 0 and uncharge() will be called. But, even if it's fully
3565 * unmapped, migration may fail and this page has to be
3566 * charged again. We set MIGRATION flag here and delay uncharge
3567 * until end_migration() is called
3568 *
3569 * Corner Case Thinking
3570 * A)
3571 * When the old page was mapped as Anon and it's unmap-and-freed
3572 * while migration was ongoing.
3573 * If unmap finds the old page, uncharge() of it will be delayed
3574 * until end_migration(). If unmap finds a new page, it's
3575 * uncharged when it make mapcount to be 1->0. If unmap code
3576 * finds swap_migration_entry, the new page will not be mapped
3577 * and end_migration() will find it(mapcount==0).
3578 *
3579 * B)
3580 * When the old page was mapped but migraion fails, the kernel
3581 * remaps it. A charge for it is kept by MIGRATION flag even
3582 * if mapcount goes down to 0. We can do remap successfully
3583 * without charging it again.
3584 *
3585 * C)
3586 * The "old" page is under lock_page() until the end of
3587 * migration, so, the old page itself will not be swapped-out.
3588 * If the new page is swapped out before end_migraton, our
3589 * hook to usual swap-out path will catch the event.
3590 */
3591 if (PageAnon(page))
3592 SetPageCgroupMigration(pc);
e8589cc1 3593 }
52d4b9ac 3594 unlock_page_cgroup(pc);
ac39cf8c
AM
3595 /*
3596 * If the page is not charged at this point,
3597 * we return here.
3598 */
c0ff4b85 3599 if (!memcg)
0030f535 3600 return;
01b1ae63 3601
72835c86 3602 *memcgp = memcg;
ac39cf8c
AM
3603 /*
3604 * We charge new page before it's used/mapped. So, even if unlock_page()
3605 * is called before end_migration, we can catch all events on this new
3606 * page. In the case new page is migrated but not remapped, new page's
3607 * mapcount will be finally 0 and we call uncharge in end_migration().
3608 */
ac39cf8c 3609 if (PageAnon(page))
41326c17 3610 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
ac39cf8c 3611 else
62ba7442 3612 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
0030f535
JW
3613 /*
3614 * The page is committed to the memcg, but it's not actually
3615 * charged to the res_counter since we plan on replacing the
3616 * old one and only one page is going to be left afterwards.
3617 */
b32967ff 3618 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
ae41be37 3619}
8869b8f6 3620
69029cd5 3621/* remove redundant charge if migration failed*/
c0ff4b85 3622void mem_cgroup_end_migration(struct mem_cgroup *memcg,
50de1dd9 3623 struct page *oldpage, struct page *newpage, bool migration_ok)
ae41be37 3624{
ac39cf8c 3625 struct page *used, *unused;
01b1ae63 3626 struct page_cgroup *pc;
b2402857 3627 bool anon;
01b1ae63 3628
c0ff4b85 3629 if (!memcg)
01b1ae63 3630 return;
b25ed609 3631
50de1dd9 3632 if (!migration_ok) {
ac39cf8c
AM
3633 used = oldpage;
3634 unused = newpage;
01b1ae63 3635 } else {
ac39cf8c 3636 used = newpage;
01b1ae63
KH
3637 unused = oldpage;
3638 }
0030f535 3639 anon = PageAnon(used);
7d188958
JW
3640 __mem_cgroup_uncharge_common(unused,
3641 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
3642 : MEM_CGROUP_CHARGE_TYPE_CACHE,
3643 true);
0030f535 3644 css_put(&memcg->css);
69029cd5 3645 /*
ac39cf8c
AM
3646 * We disallowed uncharge of pages under migration because mapcount
3647 * of the page goes down to zero, temporarly.
3648 * Clear the flag and check the page should be charged.
01b1ae63 3649 */
ac39cf8c
AM
3650 pc = lookup_page_cgroup(oldpage);
3651 lock_page_cgroup(pc);
3652 ClearPageCgroupMigration(pc);
3653 unlock_page_cgroup(pc);
ac39cf8c 3654
01b1ae63 3655 /*
ac39cf8c
AM
3656 * If a page is a file cache, radix-tree replacement is very atomic
3657 * and we can skip this check. When it was an Anon page, its mapcount
3658 * goes down to 0. But because we added MIGRATION flage, it's not
3659 * uncharged yet. There are several case but page->mapcount check
3660 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3661 * check. (see prepare_charge() also)
69029cd5 3662 */
b2402857 3663 if (anon)
ac39cf8c 3664 mem_cgroup_uncharge_page(used);
ae41be37 3665}
78fb7466 3666
ab936cbc
KH
3667/*
3668 * At replace page cache, newpage is not under any memcg but it's on
3669 * LRU. So, this function doesn't touch res_counter but handles LRU
3670 * in correct way. Both pages are locked so we cannot race with uncharge.
3671 */
3672void mem_cgroup_replace_page_cache(struct page *oldpage,
3673 struct page *newpage)
3674{
bde05d1c 3675 struct mem_cgroup *memcg = NULL;
ab936cbc 3676 struct page_cgroup *pc;
ab936cbc 3677 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
ab936cbc
KH
3678
3679 if (mem_cgroup_disabled())
3680 return;
3681
3682 pc = lookup_page_cgroup(oldpage);
3683 /* fix accounting on old pages */
3684 lock_page_cgroup(pc);
bde05d1c
HD
3685 if (PageCgroupUsed(pc)) {
3686 memcg = pc->mem_cgroup;
3687 mem_cgroup_charge_statistics(memcg, false, -1);
3688 ClearPageCgroupUsed(pc);
3689 }
ab936cbc
KH
3690 unlock_page_cgroup(pc);
3691
bde05d1c
HD
3692 /*
3693 * When called from shmem_replace_page(), in some cases the
3694 * oldpage has already been charged, and in some cases not.
3695 */
3696 if (!memcg)
3697 return;
ab936cbc
KH
3698 /*
3699 * Even if newpage->mapping was NULL before starting replacement,
3700 * the newpage may be on LRU(or pagevec for LRU) already. We lock
3701 * LRU while we overwrite pc->mem_cgroup.
3702 */
ce587e65 3703 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
ab936cbc
KH
3704}
3705
f212ad7c
DN
3706#ifdef CONFIG_DEBUG_VM
3707static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3708{
3709 struct page_cgroup *pc;
3710
3711 pc = lookup_page_cgroup(page);
cfa44946
JW
3712 /*
3713 * Can be NULL while feeding pages into the page allocator for
3714 * the first time, i.e. during boot or memory hotplug;
3715 * or when mem_cgroup_disabled().
3716 */
f212ad7c
DN
3717 if (likely(pc) && PageCgroupUsed(pc))
3718 return pc;
3719 return NULL;
3720}
3721
3722bool mem_cgroup_bad_page_check(struct page *page)
3723{
3724 if (mem_cgroup_disabled())
3725 return false;
3726
3727 return lookup_page_cgroup_used(page) != NULL;
3728}
3729
3730void mem_cgroup_print_bad_page(struct page *page)
3731{
3732 struct page_cgroup *pc;
3733
3734 pc = lookup_page_cgroup_used(page);
3735 if (pc) {
90b3feae 3736 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
f212ad7c 3737 pc, pc->flags, pc->mem_cgroup);
f212ad7c
DN
3738 }
3739}
3740#endif
3741
8c7c6e34
KH
3742static DEFINE_MUTEX(set_limit_mutex);
3743
d38d2a75 3744static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
8c7c6e34 3745 unsigned long long val)
628f4235 3746{
81d39c20 3747 int retry_count;
3c11ecf4 3748 u64 memswlimit, memlimit;
628f4235 3749 int ret = 0;
81d39c20
KH
3750 int children = mem_cgroup_count_children(memcg);
3751 u64 curusage, oldusage;
3c11ecf4 3752 int enlarge;
81d39c20
KH
3753
3754 /*
3755 * For keeping hierarchical_reclaim simple, how long we should retry
3756 * is depends on callers. We set our retry-count to be function
3757 * of # of children which we should visit in this loop.
3758 */
3759 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3760
3761 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
628f4235 3762
3c11ecf4 3763 enlarge = 0;
8c7c6e34 3764 while (retry_count) {
628f4235
KH
3765 if (signal_pending(current)) {
3766 ret = -EINTR;
3767 break;
3768 }
8c7c6e34
KH
3769 /*
3770 * Rather than hide all in some function, I do this in
3771 * open coded manner. You see what this really does.
aaad153e 3772 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
8c7c6e34
KH
3773 */
3774 mutex_lock(&set_limit_mutex);
3775 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3776 if (memswlimit < val) {
3777 ret = -EINVAL;
3778 mutex_unlock(&set_limit_mutex);
628f4235
KH
3779 break;
3780 }
3c11ecf4
KH
3781
3782 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3783 if (memlimit < val)
3784 enlarge = 1;
3785
8c7c6e34 3786 ret = res_counter_set_limit(&memcg->res, val);
22a668d7
KH
3787 if (!ret) {
3788 if (memswlimit == val)
3789 memcg->memsw_is_minimum = true;
3790 else
3791 memcg->memsw_is_minimum = false;
3792 }
8c7c6e34
KH
3793 mutex_unlock(&set_limit_mutex);
3794
3795 if (!ret)
3796 break;
3797
5660048c
JW
3798 mem_cgroup_reclaim(memcg, GFP_KERNEL,
3799 MEM_CGROUP_RECLAIM_SHRINK);
81d39c20
KH
3800 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3801 /* Usage is reduced ? */
3802 if (curusage >= oldusage)
3803 retry_count--;
3804 else
3805 oldusage = curusage;
8c7c6e34 3806 }
3c11ecf4
KH
3807 if (!ret && enlarge)
3808 memcg_oom_recover(memcg);
14797e23 3809
8c7c6e34
KH
3810 return ret;
3811}
3812
338c8431
LZ
3813static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3814 unsigned long long val)
8c7c6e34 3815{
81d39c20 3816 int retry_count;
3c11ecf4 3817 u64 memlimit, memswlimit, oldusage, curusage;
81d39c20
KH
3818 int children = mem_cgroup_count_children(memcg);
3819 int ret = -EBUSY;
3c11ecf4 3820 int enlarge = 0;
8c7c6e34 3821
81d39c20
KH
3822 /* see mem_cgroup_resize_res_limit */
3823 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3824 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
8c7c6e34
KH
3825 while (retry_count) {
3826 if (signal_pending(current)) {
3827 ret = -EINTR;
3828 break;
3829 }
3830 /*
3831 * Rather than hide all in some function, I do this in
3832 * open coded manner. You see what this really does.
aaad153e 3833 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
8c7c6e34
KH
3834 */
3835 mutex_lock(&set_limit_mutex);
3836 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3837 if (memlimit > val) {
3838 ret = -EINVAL;
3839 mutex_unlock(&set_limit_mutex);
3840 break;
3841 }
3c11ecf4
KH
3842 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3843 if (memswlimit < val)
3844 enlarge = 1;
8c7c6e34 3845 ret = res_counter_set_limit(&memcg->memsw, val);
22a668d7
KH
3846 if (!ret) {
3847 if (memlimit == val)
3848 memcg->memsw_is_minimum = true;
3849 else
3850 memcg->memsw_is_minimum = false;
3851 }
8c7c6e34
KH
3852 mutex_unlock(&set_limit_mutex);
3853
3854 if (!ret)
3855 break;
3856
5660048c
JW
3857 mem_cgroup_reclaim(memcg, GFP_KERNEL,
3858 MEM_CGROUP_RECLAIM_NOSWAP |
3859 MEM_CGROUP_RECLAIM_SHRINK);
8c7c6e34 3860 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
81d39c20 3861 /* Usage is reduced ? */
8c7c6e34 3862 if (curusage >= oldusage)
628f4235 3863 retry_count--;
81d39c20
KH
3864 else
3865 oldusage = curusage;
628f4235 3866 }
3c11ecf4
KH
3867 if (!ret && enlarge)
3868 memcg_oom_recover(memcg);
628f4235
KH
3869 return ret;
3870}
3871
4e416953 3872unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
0ae5e89c
YH
3873 gfp_t gfp_mask,
3874 unsigned long *total_scanned)
4e416953
BS
3875{
3876 unsigned long nr_reclaimed = 0;
3877 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3878 unsigned long reclaimed;
3879 int loop = 0;
3880 struct mem_cgroup_tree_per_zone *mctz;
ef8745c1 3881 unsigned long long excess;
0ae5e89c 3882 unsigned long nr_scanned;
4e416953
BS
3883
3884 if (order > 0)
3885 return 0;
3886
00918b6a 3887 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
4e416953
BS
3888 /*
3889 * This loop can run a while, specially if mem_cgroup's continuously
3890 * keep exceeding their soft limit and putting the system under
3891 * pressure
3892 */
3893 do {
3894 if (next_mz)
3895 mz = next_mz;
3896 else
3897 mz = mem_cgroup_largest_soft_limit_node(mctz);
3898 if (!mz)
3899 break;
3900
0ae5e89c 3901 nr_scanned = 0;
d79154bb 3902 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
5660048c 3903 gfp_mask, &nr_scanned);
4e416953 3904 nr_reclaimed += reclaimed;
0ae5e89c 3905 *total_scanned += nr_scanned;
4e416953
BS
3906 spin_lock(&mctz->lock);
3907
3908 /*
3909 * If we failed to reclaim anything from this memory cgroup
3910 * it is time to move on to the next cgroup
3911 */
3912 next_mz = NULL;
3913 if (!reclaimed) {
3914 do {
3915 /*
3916 * Loop until we find yet another one.
3917 *
3918 * By the time we get the soft_limit lock
3919 * again, someone might have aded the
3920 * group back on the RB tree. Iterate to
3921 * make sure we get a different mem.
3922 * mem_cgroup_largest_soft_limit_node returns
3923 * NULL if no other cgroup is present on
3924 * the tree
3925 */
3926 next_mz =
3927 __mem_cgroup_largest_soft_limit_node(mctz);
39cc98f1 3928 if (next_mz == mz)
d79154bb 3929 css_put(&next_mz->memcg->css);
39cc98f1 3930 else /* next_mz == NULL or other memcg */
4e416953
BS
3931 break;
3932 } while (1);
3933 }
d79154bb
HD
3934 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
3935 excess = res_counter_soft_limit_excess(&mz->memcg->res);
4e416953
BS
3936 /*
3937 * One school of thought says that we should not add
3938 * back the node to the tree if reclaim returns 0.
3939 * But our reclaim could return 0, simply because due
3940 * to priority we are exposing a smaller subset of
3941 * memory to reclaim from. Consider this as a longer
3942 * term TODO.
3943 */
ef8745c1 3944 /* If excess == 0, no tree ops */
d79154bb 3945 __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess);
4e416953 3946 spin_unlock(&mctz->lock);
d79154bb 3947 css_put(&mz->memcg->css);
4e416953
BS
3948 loop++;
3949 /*
3950 * Could not reclaim anything and there are no more
3951 * mem cgroups to try or we seem to be looping without
3952 * reclaiming anything.
3953 */
3954 if (!nr_reclaimed &&
3955 (next_mz == NULL ||
3956 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3957 break;
3958 } while (!nr_reclaimed);
3959 if (next_mz)
d79154bb 3960 css_put(&next_mz->memcg->css);
4e416953
BS
3961 return nr_reclaimed;
3962}
3963
2ef37d3f
MH
3964/**
3965 * mem_cgroup_force_empty_list - clears LRU of a group
3966 * @memcg: group to clear
3967 * @node: NUMA node
3968 * @zid: zone id
3969 * @lru: lru to to clear
3970 *
3c935d18 3971 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
2ef37d3f
MH
3972 * reclaim the pages page themselves - pages are moved to the parent (or root)
3973 * group.
cc847582 3974 */
2ef37d3f 3975static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
08e552c6 3976 int node, int zid, enum lru_list lru)
cc847582 3977{
bea8c150 3978 struct lruvec *lruvec;
2ef37d3f 3979 unsigned long flags;
072c56c1 3980 struct list_head *list;
925b7673
JW
3981 struct page *busy;
3982 struct zone *zone;
072c56c1 3983
08e552c6 3984 zone = &NODE_DATA(node)->node_zones[zid];
bea8c150
HD
3985 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
3986 list = &lruvec->lists[lru];
cc847582 3987
f817ed48 3988 busy = NULL;
2ef37d3f 3989 do {
925b7673 3990 struct page_cgroup *pc;
5564e88b
JW
3991 struct page *page;
3992
08e552c6 3993 spin_lock_irqsave(&zone->lru_lock, flags);
f817ed48 3994 if (list_empty(list)) {
08e552c6 3995 spin_unlock_irqrestore(&zone->lru_lock, flags);
52d4b9ac 3996 break;
f817ed48 3997 }
925b7673
JW
3998 page = list_entry(list->prev, struct page, lru);
3999 if (busy == page) {
4000 list_move(&page->lru, list);
648bcc77 4001 busy = NULL;
08e552c6 4002 spin_unlock_irqrestore(&zone->lru_lock, flags);
f817ed48
KH
4003 continue;
4004 }
08e552c6 4005 spin_unlock_irqrestore(&zone->lru_lock, flags);
f817ed48 4006
925b7673 4007 pc = lookup_page_cgroup(page);
5564e88b 4008
3c935d18 4009 if (mem_cgroup_move_parent(page, pc, memcg)) {
f817ed48 4010 /* found lock contention or "pc" is obsolete. */
925b7673 4011 busy = page;
f817ed48
KH
4012 cond_resched();
4013 } else
4014 busy = NULL;
2ef37d3f 4015 } while (!list_empty(list));
cc847582
KH
4016}
4017
4018/*
c26251f9
MH
4019 * make mem_cgroup's charge to be 0 if there is no task by moving
4020 * all the charges and pages to the parent.
cc847582 4021 * This enables deleting this mem_cgroup.
c26251f9
MH
4022 *
4023 * Caller is responsible for holding css reference on the memcg.
cc847582 4024 */
ab5196c2 4025static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
cc847582 4026{
c26251f9 4027 int node, zid;
f817ed48 4028
fce66477 4029 do {
52d4b9ac
KH
4030 /* This is for making all *used* pages to be on LRU. */
4031 lru_add_drain_all();
c0ff4b85 4032 drain_all_stock_sync(memcg);
c0ff4b85 4033 mem_cgroup_start_move(memcg);
31aaea4a 4034 for_each_node_state(node, N_MEMORY) {
2ef37d3f 4035 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
f156ab93
HD
4036 enum lru_list lru;
4037 for_each_lru(lru) {
2ef37d3f 4038 mem_cgroup_force_empty_list(memcg,
f156ab93 4039 node, zid, lru);
f817ed48 4040 }
1ecaab2b 4041 }
f817ed48 4042 }
c0ff4b85
R
4043 mem_cgroup_end_move(memcg);
4044 memcg_oom_recover(memcg);
52d4b9ac 4045 cond_resched();
f817ed48 4046
2ef37d3f
MH
4047 /*
4048 * This is a safety check because mem_cgroup_force_empty_list
4049 * could have raced with mem_cgroup_replace_page_cache callers
4050 * so the lru seemed empty but the page could have been added
4051 * right after the check. RES_USAGE should be safe as we always
4052 * charge before adding to the LRU.
4053 */
4054 } while (res_counter_read_u64(&memcg->res, RES_USAGE) > 0);
c26251f9
MH
4055}
4056
4057/*
4058 * Reclaims as many pages from the given memcg as possible and moves
4059 * the rest to the parent.
4060 *
4061 * Caller is responsible for holding css reference for memcg.
4062 */
4063static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
4064{
4065 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4066 struct cgroup *cgrp = memcg->css.cgroup;
f817ed48 4067
c1e862c1 4068 /* returns EBUSY if there is a task or if we come here twice. */
c26251f9
MH
4069 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
4070 return -EBUSY;
4071
c1e862c1
KH
4072 /* we call try-to-free pages for make this cgroup empty */
4073 lru_add_drain_all();
f817ed48 4074 /* try to free all pages in this cgroup */
569530fb 4075 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
f817ed48 4076 int progress;
c1e862c1 4077
c26251f9
MH
4078 if (signal_pending(current))
4079 return -EINTR;
4080
c0ff4b85 4081 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
185efc0f 4082 false);
c1e862c1 4083 if (!progress) {
f817ed48 4084 nr_retries--;
c1e862c1 4085 /* maybe some writeback is necessary */
8aa7e847 4086 congestion_wait(BLK_RW_ASYNC, HZ/10);
c1e862c1 4087 }
f817ed48
KH
4088
4089 }
08e552c6 4090 lru_add_drain();
ab5196c2
MH
4091 mem_cgroup_reparent_charges(memcg);
4092
4093 return 0;
cc847582
KH
4094}
4095
6bbda35c 4096static int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
c1e862c1 4097{
c26251f9
MH
4098 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4099 int ret;
4100
d8423011
MH
4101 if (mem_cgroup_is_root(memcg))
4102 return -EINVAL;
c26251f9
MH
4103 css_get(&memcg->css);
4104 ret = mem_cgroup_force_empty(memcg);
4105 css_put(&memcg->css);
4106
4107 return ret;
c1e862c1
KH
4108}
4109
4110
18f59ea7
BS
4111static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
4112{
4113 return mem_cgroup_from_cont(cont)->use_hierarchy;
4114}
4115
4116static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
4117 u64 val)
4118{
4119 int retval = 0;
c0ff4b85 4120 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
18f59ea7 4121 struct cgroup *parent = cont->parent;
c0ff4b85 4122 struct mem_cgroup *parent_memcg = NULL;
18f59ea7
BS
4123
4124 if (parent)
c0ff4b85 4125 parent_memcg = mem_cgroup_from_cont(parent);
18f59ea7
BS
4126
4127 cgroup_lock();
567fb435
GC
4128
4129 if (memcg->use_hierarchy == val)
4130 goto out;
4131
18f59ea7 4132 /*
af901ca1 4133 * If parent's use_hierarchy is set, we can't make any modifications
18f59ea7
BS
4134 * in the child subtrees. If it is unset, then the change can
4135 * occur, provided the current cgroup has no children.
4136 *
4137 * For the root cgroup, parent_mem is NULL, we allow value to be
4138 * set if there are no children.
4139 */
c0ff4b85 4140 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
18f59ea7
BS
4141 (val == 1 || val == 0)) {
4142 if (list_empty(&cont->children))
c0ff4b85 4143 memcg->use_hierarchy = val;
18f59ea7
BS
4144 else
4145 retval = -EBUSY;
4146 } else
4147 retval = -EINVAL;
567fb435
GC
4148
4149out:
18f59ea7
BS
4150 cgroup_unlock();
4151
4152 return retval;
4153}
4154
0c3e73e8 4155
c0ff4b85 4156static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
7a159cc9 4157 enum mem_cgroup_stat_index idx)
0c3e73e8 4158{
7d74b06f 4159 struct mem_cgroup *iter;
7a159cc9 4160 long val = 0;
0c3e73e8 4161
7a159cc9 4162 /* Per-cpu values can be negative, use a signed accumulator */
c0ff4b85 4163 for_each_mem_cgroup_tree(iter, memcg)
7d74b06f
KH
4164 val += mem_cgroup_read_stat(iter, idx);
4165
4166 if (val < 0) /* race ? */
4167 val = 0;
4168 return val;
0c3e73e8
BS
4169}
4170
c0ff4b85 4171static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
104f3928 4172{
7d74b06f 4173 u64 val;
104f3928 4174
c0ff4b85 4175 if (!mem_cgroup_is_root(memcg)) {
104f3928 4176 if (!swap)
65c64ce8 4177 return res_counter_read_u64(&memcg->res, RES_USAGE);
104f3928 4178 else
65c64ce8 4179 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
104f3928
KS
4180 }
4181
c0ff4b85
R
4182 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
4183 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
104f3928 4184
7d74b06f 4185 if (swap)
bff6bb83 4186 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
104f3928
KS
4187
4188 return val << PAGE_SHIFT;
4189}
4190
af36f906
TH
4191static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,
4192 struct file *file, char __user *buf,
4193 size_t nbytes, loff_t *ppos)
8cdea7c0 4194{
c0ff4b85 4195 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
af36f906 4196 char str[64];
104f3928 4197 u64 val;
86ae53e1
GC
4198 int name, len;
4199 enum res_type type;
8c7c6e34
KH
4200
4201 type = MEMFILE_TYPE(cft->private);
4202 name = MEMFILE_ATTR(cft->private);
af36f906
TH
4203
4204 if (!do_swap_account && type == _MEMSWAP)
4205 return -EOPNOTSUPP;
4206
8c7c6e34
KH
4207 switch (type) {
4208 case _MEM:
104f3928 4209 if (name == RES_USAGE)
c0ff4b85 4210 val = mem_cgroup_usage(memcg, false);
104f3928 4211 else
c0ff4b85 4212 val = res_counter_read_u64(&memcg->res, name);
8c7c6e34
KH
4213 break;
4214 case _MEMSWAP:
104f3928 4215 if (name == RES_USAGE)
c0ff4b85 4216 val = mem_cgroup_usage(memcg, true);
104f3928 4217 else
c0ff4b85 4218 val = res_counter_read_u64(&memcg->memsw, name);
8c7c6e34 4219 break;
510fc4e1
GC
4220 case _KMEM:
4221 val = res_counter_read_u64(&memcg->kmem, name);
4222 break;
8c7c6e34
KH
4223 default:
4224 BUG();
8c7c6e34 4225 }
af36f906
TH
4226
4227 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
4228 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
8cdea7c0 4229}
510fc4e1
GC
4230
4231static int memcg_update_kmem_limit(struct cgroup *cont, u64 val)
4232{
4233 int ret = -EINVAL;
4234#ifdef CONFIG_MEMCG_KMEM
a8964b9b
GC
4235 bool must_inc_static_branch = false;
4236
510fc4e1
GC
4237 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4238 /*
4239 * For simplicity, we won't allow this to be disabled. It also can't
4240 * be changed if the cgroup has children already, or if tasks had
4241 * already joined.
4242 *
4243 * If tasks join before we set the limit, a person looking at
4244 * kmem.usage_in_bytes will have no way to determine when it took
4245 * place, which makes the value quite meaningless.
4246 *
4247 * After it first became limited, changes in the value of the limit are
4248 * of course permitted.
4249 *
4250 * Taking the cgroup_lock is really offensive, but it is so far the only
4251 * way to guarantee that no children will appear. There are plenty of
4252 * other offenders, and they should all go away. Fine grained locking
4253 * is probably the way to go here. When we are fully hierarchical, we
4254 * can also get rid of the use_hierarchy check.
4255 */
4256 cgroup_lock();
4257 mutex_lock(&set_limit_mutex);
4258 if (!memcg->kmem_account_flags && val != RESOURCE_MAX) {
4259 if (cgroup_task_count(cont) || (memcg->use_hierarchy &&
4260 !list_empty(&cont->children))) {
4261 ret = -EBUSY;
4262 goto out;
4263 }
4264 ret = res_counter_set_limit(&memcg->kmem, val);
4265 VM_BUG_ON(ret);
4266
a8964b9b
GC
4267 /*
4268 * After this point, kmem_accounted (that we test atomically in
4269 * the beginning of this conditional), is no longer 0. This
4270 * guarantees only one process will set the following boolean
4271 * to true. We don't need test_and_set because we're protected
4272 * by the set_limit_mutex anyway.
4273 */
4274 memcg_kmem_set_activated(memcg);
4275 must_inc_static_branch = true;
7de37682
GC
4276 /*
4277 * kmem charges can outlive the cgroup. In the case of slab
4278 * pages, for instance, a page contain objects from various
4279 * processes, so it is unfeasible to migrate them away. We
4280 * need to reference count the memcg because of that.
4281 */
4282 mem_cgroup_get(memcg);
510fc4e1
GC
4283 } else
4284 ret = res_counter_set_limit(&memcg->kmem, val);
4285out:
4286 mutex_unlock(&set_limit_mutex);
4287 cgroup_unlock();
a8964b9b
GC
4288
4289 /*
4290 * We are by now familiar with the fact that we can't inc the static
4291 * branch inside cgroup_lock. See disarm functions for details. A
4292 * worker here is overkill, but also wrong: After the limit is set, we
4293 * must start accounting right away. Since this operation can't fail,
4294 * we can safely defer it to here - no rollback will be needed.
4295 *
4296 * The boolean used to control this is also safe, because
4297 * KMEM_ACCOUNTED_ACTIVATED guarantees that only one process will be
4298 * able to set it to true;
4299 */
4300 if (must_inc_static_branch) {
4301 static_key_slow_inc(&memcg_kmem_enabled_key);
4302 /*
4303 * setting the active bit after the inc will guarantee no one
4304 * starts accounting before all call sites are patched
4305 */
4306 memcg_kmem_set_active(memcg);
4307 }
4308
510fc4e1
GC
4309#endif
4310 return ret;
4311}
4312
4313static void memcg_propagate_kmem(struct mem_cgroup *memcg)
4314{
4315 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4316 if (!parent)
4317 return;
4318 memcg->kmem_account_flags = parent->kmem_account_flags;
7de37682 4319#ifdef CONFIG_MEMCG_KMEM
a8964b9b
GC
4320 /*
4321 * When that happen, we need to disable the static branch only on those
4322 * memcgs that enabled it. To achieve this, we would be forced to
4323 * complicate the code by keeping track of which memcgs were the ones
4324 * that actually enabled limits, and which ones got it from its
4325 * parents.
4326 *
4327 * It is a lot simpler just to do static_key_slow_inc() on every child
4328 * that is accounted.
4329 */
4330 if (memcg_kmem_is_active(memcg)) {
7de37682 4331 mem_cgroup_get(memcg);
a8964b9b
GC
4332 static_key_slow_inc(&memcg_kmem_enabled_key);
4333 }
7de37682 4334#endif
510fc4e1
GC
4335}
4336
628f4235
KH
4337/*
4338 * The user of this function is...
4339 * RES_LIMIT.
4340 */
856c13aa
PM
4341static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
4342 const char *buffer)
8cdea7c0 4343{
628f4235 4344 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
86ae53e1
GC
4345 enum res_type type;
4346 int name;
628f4235
KH
4347 unsigned long long val;
4348 int ret;
4349
8c7c6e34
KH
4350 type = MEMFILE_TYPE(cft->private);
4351 name = MEMFILE_ATTR(cft->private);
af36f906
TH
4352
4353 if (!do_swap_account && type == _MEMSWAP)
4354 return -EOPNOTSUPP;
4355
8c7c6e34 4356 switch (name) {
628f4235 4357 case RES_LIMIT:
4b3bde4c
BS
4358 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
4359 ret = -EINVAL;
4360 break;
4361 }
628f4235
KH
4362 /* This function does all necessary parse...reuse it */
4363 ret = res_counter_memparse_write_strategy(buffer, &val);
8c7c6e34
KH
4364 if (ret)
4365 break;
4366 if (type == _MEM)
628f4235 4367 ret = mem_cgroup_resize_limit(memcg, val);
510fc4e1 4368 else if (type == _MEMSWAP)
8c7c6e34 4369 ret = mem_cgroup_resize_memsw_limit(memcg, val);
510fc4e1
GC
4370 else if (type == _KMEM)
4371 ret = memcg_update_kmem_limit(cont, val);
4372 else
4373 return -EINVAL;
628f4235 4374 break;
296c81d8
BS
4375 case RES_SOFT_LIMIT:
4376 ret = res_counter_memparse_write_strategy(buffer, &val);
4377 if (ret)
4378 break;
4379 /*
4380 * For memsw, soft limits are hard to implement in terms
4381 * of semantics, for now, we support soft limits for
4382 * control without swap
4383 */
4384 if (type == _MEM)
4385 ret = res_counter_set_soft_limit(&memcg->res, val);
4386 else
4387 ret = -EINVAL;
4388 break;
628f4235
KH
4389 default:
4390 ret = -EINVAL; /* should be BUG() ? */
4391 break;
4392 }
4393 return ret;
8cdea7c0
BS
4394}
4395
fee7b548
KH
4396static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
4397 unsigned long long *mem_limit, unsigned long long *memsw_limit)
4398{
4399 struct cgroup *cgroup;
4400 unsigned long long min_limit, min_memsw_limit, tmp;
4401
4402 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4403 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4404 cgroup = memcg->css.cgroup;
4405 if (!memcg->use_hierarchy)
4406 goto out;
4407
4408 while (cgroup->parent) {
4409 cgroup = cgroup->parent;
4410 memcg = mem_cgroup_from_cont(cgroup);
4411 if (!memcg->use_hierarchy)
4412 break;
4413 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
4414 min_limit = min(min_limit, tmp);
4415 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4416 min_memsw_limit = min(min_memsw_limit, tmp);
4417 }
4418out:
4419 *mem_limit = min_limit;
4420 *memsw_limit = min_memsw_limit;
fee7b548
KH
4421}
4422
29f2a4da 4423static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
c84872e1 4424{
af36f906 4425 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
86ae53e1
GC
4426 int name;
4427 enum res_type type;
c84872e1 4428
8c7c6e34
KH
4429 type = MEMFILE_TYPE(event);
4430 name = MEMFILE_ATTR(event);
af36f906
TH
4431
4432 if (!do_swap_account && type == _MEMSWAP)
4433 return -EOPNOTSUPP;
4434
8c7c6e34 4435 switch (name) {
29f2a4da 4436 case RES_MAX_USAGE:
8c7c6e34 4437 if (type == _MEM)
c0ff4b85 4438 res_counter_reset_max(&memcg->res);
510fc4e1 4439 else if (type == _MEMSWAP)
c0ff4b85 4440 res_counter_reset_max(&memcg->memsw);
510fc4e1
GC
4441 else if (type == _KMEM)
4442 res_counter_reset_max(&memcg->kmem);
4443 else
4444 return -EINVAL;
29f2a4da
PE
4445 break;
4446 case RES_FAILCNT:
8c7c6e34 4447 if (type == _MEM)
c0ff4b85 4448 res_counter_reset_failcnt(&memcg->res);
510fc4e1 4449 else if (type == _MEMSWAP)
c0ff4b85 4450 res_counter_reset_failcnt(&memcg->memsw);
510fc4e1
GC
4451 else if (type == _KMEM)
4452 res_counter_reset_failcnt(&memcg->kmem);
4453 else
4454 return -EINVAL;
29f2a4da
PE
4455 break;
4456 }
f64c3f54 4457
85cc59db 4458 return 0;
c84872e1
PE
4459}
4460
7dc74be0
DN
4461static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
4462 struct cftype *cft)
4463{
4464 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
4465}
4466
02491447 4467#ifdef CONFIG_MMU
7dc74be0
DN
4468static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4469 struct cftype *cft, u64 val)
4470{
c0ff4b85 4471 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
7dc74be0
DN
4472
4473 if (val >= (1 << NR_MOVE_TYPE))
4474 return -EINVAL;
4475 /*
4476 * We check this value several times in both in can_attach() and
4477 * attach(), so we need cgroup lock to prevent this value from being
4478 * inconsistent.
4479 */
4480 cgroup_lock();
c0ff4b85 4481 memcg->move_charge_at_immigrate = val;
7dc74be0
DN
4482 cgroup_unlock();
4483
4484 return 0;
4485}
02491447
DN
4486#else
4487static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4488 struct cftype *cft, u64 val)
4489{
4490 return -ENOSYS;
4491}
4492#endif
7dc74be0 4493
406eb0c9 4494#ifdef CONFIG_NUMA
ab215884 4495static int memcg_numa_stat_show(struct cgroup *cont, struct cftype *cft,
fada52ca 4496 struct seq_file *m)
406eb0c9
YH
4497{
4498 int nid;
4499 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
4500 unsigned long node_nr;
d79154bb 4501 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
406eb0c9 4502
d79154bb 4503 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
406eb0c9 4504 seq_printf(m, "total=%lu", total_nr);
31aaea4a 4505 for_each_node_state(nid, N_MEMORY) {
d79154bb 4506 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
406eb0c9
YH
4507 seq_printf(m, " N%d=%lu", nid, node_nr);
4508 }
4509 seq_putc(m, '\n');
4510
d79154bb 4511 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
406eb0c9 4512 seq_printf(m, "file=%lu", file_nr);
31aaea4a 4513 for_each_node_state(nid, N_MEMORY) {
d79154bb 4514 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
bb2a0de9 4515 LRU_ALL_FILE);
406eb0c9
YH
4516 seq_printf(m, " N%d=%lu", nid, node_nr);
4517 }
4518 seq_putc(m, '\n');
4519
d79154bb 4520 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
406eb0c9 4521 seq_printf(m, "anon=%lu", anon_nr);
31aaea4a 4522 for_each_node_state(nid, N_MEMORY) {
d79154bb 4523 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
bb2a0de9 4524 LRU_ALL_ANON);
406eb0c9
YH
4525 seq_printf(m, " N%d=%lu", nid, node_nr);
4526 }
4527 seq_putc(m, '\n');
4528
d79154bb 4529 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
406eb0c9 4530 seq_printf(m, "unevictable=%lu", unevictable_nr);
31aaea4a 4531 for_each_node_state(nid, N_MEMORY) {
d79154bb 4532 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
bb2a0de9 4533 BIT(LRU_UNEVICTABLE));
406eb0c9
YH
4534 seq_printf(m, " N%d=%lu", nid, node_nr);
4535 }
4536 seq_putc(m, '\n');
4537 return 0;
4538}
4539#endif /* CONFIG_NUMA */
4540
af7c4b0e
JW
4541static const char * const mem_cgroup_lru_names[] = {
4542 "inactive_anon",
4543 "active_anon",
4544 "inactive_file",
4545 "active_file",
4546 "unevictable",
4547};
4548
4549static inline void mem_cgroup_lru_names_not_uptodate(void)
4550{
4551 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
4552}
4553
ab215884 4554static int memcg_stat_show(struct cgroup *cont, struct cftype *cft,
78ccf5b5 4555 struct seq_file *m)
d2ceb9b7 4556{
d79154bb 4557 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
af7c4b0e
JW
4558 struct mem_cgroup *mi;
4559 unsigned int i;
406eb0c9 4560
af7c4b0e 4561 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
bff6bb83 4562 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1dd3a273 4563 continue;
af7c4b0e
JW
4564 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
4565 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
1dd3a273 4566 }
7b854121 4567
af7c4b0e
JW
4568 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
4569 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
4570 mem_cgroup_read_events(memcg, i));
4571
4572 for (i = 0; i < NR_LRU_LISTS; i++)
4573 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
4574 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
4575
14067bb3 4576 /* Hierarchical information */
fee7b548
KH
4577 {
4578 unsigned long long limit, memsw_limit;
d79154bb 4579 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
78ccf5b5 4580 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
fee7b548 4581 if (do_swap_account)
78ccf5b5
JW
4582 seq_printf(m, "hierarchical_memsw_limit %llu\n",
4583 memsw_limit);
fee7b548 4584 }
7f016ee8 4585
af7c4b0e
JW
4586 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
4587 long long val = 0;
4588
bff6bb83 4589 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1dd3a273 4590 continue;
af7c4b0e
JW
4591 for_each_mem_cgroup_tree(mi, memcg)
4592 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
4593 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
4594 }
4595
4596 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
4597 unsigned long long val = 0;
4598
4599 for_each_mem_cgroup_tree(mi, memcg)
4600 val += mem_cgroup_read_events(mi, i);
4601 seq_printf(m, "total_%s %llu\n",
4602 mem_cgroup_events_names[i], val);
4603 }
4604
4605 for (i = 0; i < NR_LRU_LISTS; i++) {
4606 unsigned long long val = 0;
4607
4608 for_each_mem_cgroup_tree(mi, memcg)
4609 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
4610 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
1dd3a273 4611 }
14067bb3 4612
7f016ee8 4613#ifdef CONFIG_DEBUG_VM
7f016ee8
KM
4614 {
4615 int nid, zid;
4616 struct mem_cgroup_per_zone *mz;
89abfab1 4617 struct zone_reclaim_stat *rstat;
7f016ee8
KM
4618 unsigned long recent_rotated[2] = {0, 0};
4619 unsigned long recent_scanned[2] = {0, 0};
4620
4621 for_each_online_node(nid)
4622 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
d79154bb 4623 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
89abfab1 4624 rstat = &mz->lruvec.reclaim_stat;
7f016ee8 4625
89abfab1
HD
4626 recent_rotated[0] += rstat->recent_rotated[0];
4627 recent_rotated[1] += rstat->recent_rotated[1];
4628 recent_scanned[0] += rstat->recent_scanned[0];
4629 recent_scanned[1] += rstat->recent_scanned[1];
7f016ee8 4630 }
78ccf5b5
JW
4631 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
4632 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
4633 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
4634 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
7f016ee8
KM
4635 }
4636#endif
4637
d2ceb9b7
KH
4638 return 0;
4639}
4640
a7885eb8
KM
4641static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
4642{
4643 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4644
1f4c025b 4645 return mem_cgroup_swappiness(memcg);
a7885eb8
KM
4646}
4647
4648static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
4649 u64 val)
4650{
4651 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4652 struct mem_cgroup *parent;
068b38c1 4653
a7885eb8
KM
4654 if (val > 100)
4655 return -EINVAL;
4656
4657 if (cgrp->parent == NULL)
4658 return -EINVAL;
4659
4660 parent = mem_cgroup_from_cont(cgrp->parent);
068b38c1
LZ
4661
4662 cgroup_lock();
4663
a7885eb8
KM
4664 /* If under hierarchy, only empty-root can set this value */
4665 if ((parent->use_hierarchy) ||
068b38c1
LZ
4666 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
4667 cgroup_unlock();
a7885eb8 4668 return -EINVAL;
068b38c1 4669 }
a7885eb8 4670
a7885eb8 4671 memcg->swappiness = val;
a7885eb8 4672
068b38c1
LZ
4673 cgroup_unlock();
4674
a7885eb8
KM
4675 return 0;
4676}
4677
2e72b634
KS
4678static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4679{
4680 struct mem_cgroup_threshold_ary *t;
4681 u64 usage;
4682 int i;
4683
4684 rcu_read_lock();
4685 if (!swap)
2c488db2 4686 t = rcu_dereference(memcg->thresholds.primary);
2e72b634 4687 else
2c488db2 4688 t = rcu_dereference(memcg->memsw_thresholds.primary);
2e72b634
KS
4689
4690 if (!t)
4691 goto unlock;
4692
4693 usage = mem_cgroup_usage(memcg, swap);
4694
4695 /*
748dad36 4696 * current_threshold points to threshold just below or equal to usage.
2e72b634
KS
4697 * If it's not true, a threshold was crossed after last
4698 * call of __mem_cgroup_threshold().
4699 */
5407a562 4700 i = t->current_threshold;
2e72b634
KS
4701
4702 /*
4703 * Iterate backward over array of thresholds starting from
4704 * current_threshold and check if a threshold is crossed.
4705 * If none of thresholds below usage is crossed, we read
4706 * only one element of the array here.
4707 */
4708 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4709 eventfd_signal(t->entries[i].eventfd, 1);
4710
4711 /* i = current_threshold + 1 */
4712 i++;
4713
4714 /*
4715 * Iterate forward over array of thresholds starting from
4716 * current_threshold+1 and check if a threshold is crossed.
4717 * If none of thresholds above usage is crossed, we read
4718 * only one element of the array here.
4719 */
4720 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4721 eventfd_signal(t->entries[i].eventfd, 1);
4722
4723 /* Update current_threshold */
5407a562 4724 t->current_threshold = i - 1;
2e72b634
KS
4725unlock:
4726 rcu_read_unlock();
4727}
4728
4729static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4730{
ad4ca5f4
KS
4731 while (memcg) {
4732 __mem_cgroup_threshold(memcg, false);
4733 if (do_swap_account)
4734 __mem_cgroup_threshold(memcg, true);
4735
4736 memcg = parent_mem_cgroup(memcg);
4737 }
2e72b634
KS
4738}
4739
4740static int compare_thresholds(const void *a, const void *b)
4741{
4742 const struct mem_cgroup_threshold *_a = a;
4743 const struct mem_cgroup_threshold *_b = b;
4744
4745 return _a->threshold - _b->threshold;
4746}
4747
c0ff4b85 4748static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
9490ff27
KH
4749{
4750 struct mem_cgroup_eventfd_list *ev;
4751
c0ff4b85 4752 list_for_each_entry(ev, &memcg->oom_notify, list)
9490ff27
KH
4753 eventfd_signal(ev->eventfd, 1);
4754 return 0;
4755}
4756
c0ff4b85 4757static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
9490ff27 4758{
7d74b06f
KH
4759 struct mem_cgroup *iter;
4760
c0ff4b85 4761 for_each_mem_cgroup_tree(iter, memcg)
7d74b06f 4762 mem_cgroup_oom_notify_cb(iter);
9490ff27
KH
4763}
4764
4765static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
4766 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
2e72b634
KS
4767{
4768 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2c488db2
KS
4769 struct mem_cgroup_thresholds *thresholds;
4770 struct mem_cgroup_threshold_ary *new;
86ae53e1 4771 enum res_type type = MEMFILE_TYPE(cft->private);
2e72b634 4772 u64 threshold, usage;
2c488db2 4773 int i, size, ret;
2e72b634
KS
4774
4775 ret = res_counter_memparse_write_strategy(args, &threshold);
4776 if (ret)
4777 return ret;
4778
4779 mutex_lock(&memcg->thresholds_lock);
2c488db2 4780
2e72b634 4781 if (type == _MEM)
2c488db2 4782 thresholds = &memcg->thresholds;
2e72b634 4783 else if (type == _MEMSWAP)
2c488db2 4784 thresholds = &memcg->memsw_thresholds;
2e72b634
KS
4785 else
4786 BUG();
4787
4788 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4789
4790 /* Check if a threshold crossed before adding a new one */
2c488db2 4791 if (thresholds->primary)
2e72b634
KS
4792 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4793
2c488db2 4794 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
2e72b634
KS
4795
4796 /* Allocate memory for new array of thresholds */
2c488db2 4797 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
2e72b634 4798 GFP_KERNEL);
2c488db2 4799 if (!new) {
2e72b634
KS
4800 ret = -ENOMEM;
4801 goto unlock;
4802 }
2c488db2 4803 new->size = size;
2e72b634
KS
4804
4805 /* Copy thresholds (if any) to new array */
2c488db2
KS
4806 if (thresholds->primary) {
4807 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
2e72b634 4808 sizeof(struct mem_cgroup_threshold));
2c488db2
KS
4809 }
4810
2e72b634 4811 /* Add new threshold */
2c488db2
KS
4812 new->entries[size - 1].eventfd = eventfd;
4813 new->entries[size - 1].threshold = threshold;
2e72b634
KS
4814
4815 /* Sort thresholds. Registering of new threshold isn't time-critical */
2c488db2 4816 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
2e72b634
KS
4817 compare_thresholds, NULL);
4818
4819 /* Find current threshold */
2c488db2 4820 new->current_threshold = -1;
2e72b634 4821 for (i = 0; i < size; i++) {
748dad36 4822 if (new->entries[i].threshold <= usage) {
2e72b634 4823 /*
2c488db2
KS
4824 * new->current_threshold will not be used until
4825 * rcu_assign_pointer(), so it's safe to increment
2e72b634
KS
4826 * it here.
4827 */
2c488db2 4828 ++new->current_threshold;
748dad36
SZ
4829 } else
4830 break;
2e72b634
KS
4831 }
4832
2c488db2
KS
4833 /* Free old spare buffer and save old primary buffer as spare */
4834 kfree(thresholds->spare);
4835 thresholds->spare = thresholds->primary;
4836
4837 rcu_assign_pointer(thresholds->primary, new);
2e72b634 4838
907860ed 4839 /* To be sure that nobody uses thresholds */
2e72b634
KS
4840 synchronize_rcu();
4841
2e72b634
KS
4842unlock:
4843 mutex_unlock(&memcg->thresholds_lock);
4844
4845 return ret;
4846}
4847
907860ed 4848static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
9490ff27 4849 struct cftype *cft, struct eventfd_ctx *eventfd)
2e72b634
KS
4850{
4851 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2c488db2
KS
4852 struct mem_cgroup_thresholds *thresholds;
4853 struct mem_cgroup_threshold_ary *new;
86ae53e1 4854 enum res_type type = MEMFILE_TYPE(cft->private);
2e72b634 4855 u64 usage;
2c488db2 4856 int i, j, size;
2e72b634
KS
4857
4858 mutex_lock(&memcg->thresholds_lock);
4859 if (type == _MEM)
2c488db2 4860 thresholds = &memcg->thresholds;
2e72b634 4861 else if (type == _MEMSWAP)
2c488db2 4862 thresholds = &memcg->memsw_thresholds;
2e72b634
KS
4863 else
4864 BUG();
4865
371528ca
AV
4866 if (!thresholds->primary)
4867 goto unlock;
4868
2e72b634
KS
4869 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4870
4871 /* Check if a threshold crossed before removing */
4872 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4873
4874 /* Calculate new number of threshold */
2c488db2
KS
4875 size = 0;
4876 for (i = 0; i < thresholds->primary->size; i++) {
4877 if (thresholds->primary->entries[i].eventfd != eventfd)
2e72b634
KS
4878 size++;
4879 }
4880
2c488db2 4881 new = thresholds->spare;
907860ed 4882
2e72b634
KS
4883 /* Set thresholds array to NULL if we don't have thresholds */
4884 if (!size) {
2c488db2
KS
4885 kfree(new);
4886 new = NULL;
907860ed 4887 goto swap_buffers;
2e72b634
KS
4888 }
4889
2c488db2 4890 new->size = size;
2e72b634
KS
4891
4892 /* Copy thresholds and find current threshold */
2c488db2
KS
4893 new->current_threshold = -1;
4894 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4895 if (thresholds->primary->entries[i].eventfd == eventfd)
2e72b634
KS
4896 continue;
4897
2c488db2 4898 new->entries[j] = thresholds->primary->entries[i];
748dad36 4899 if (new->entries[j].threshold <= usage) {
2e72b634 4900 /*
2c488db2 4901 * new->current_threshold will not be used
2e72b634
KS
4902 * until rcu_assign_pointer(), so it's safe to increment
4903 * it here.
4904 */
2c488db2 4905 ++new->current_threshold;
2e72b634
KS
4906 }
4907 j++;
4908 }
4909
907860ed 4910swap_buffers:
2c488db2
KS
4911 /* Swap primary and spare array */
4912 thresholds->spare = thresholds->primary;
8c757763
SZ
4913 /* If all events are unregistered, free the spare array */
4914 if (!new) {
4915 kfree(thresholds->spare);
4916 thresholds->spare = NULL;
4917 }
4918
2c488db2 4919 rcu_assign_pointer(thresholds->primary, new);
2e72b634 4920
907860ed 4921 /* To be sure that nobody uses thresholds */
2e72b634 4922 synchronize_rcu();
371528ca 4923unlock:
2e72b634 4924 mutex_unlock(&memcg->thresholds_lock);
2e72b634 4925}
c1e862c1 4926
9490ff27
KH
4927static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
4928 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
4929{
4930 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4931 struct mem_cgroup_eventfd_list *event;
86ae53e1 4932 enum res_type type = MEMFILE_TYPE(cft->private);
9490ff27
KH
4933
4934 BUG_ON(type != _OOM_TYPE);
4935 event = kmalloc(sizeof(*event), GFP_KERNEL);
4936 if (!event)
4937 return -ENOMEM;
4938
1af8efe9 4939 spin_lock(&memcg_oom_lock);
9490ff27
KH
4940
4941 event->eventfd = eventfd;
4942 list_add(&event->list, &memcg->oom_notify);
4943
4944 /* already in OOM ? */
79dfdacc 4945 if (atomic_read(&memcg->under_oom))
9490ff27 4946 eventfd_signal(eventfd, 1);
1af8efe9 4947 spin_unlock(&memcg_oom_lock);
9490ff27
KH
4948
4949 return 0;
4950}
4951
907860ed 4952static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
9490ff27
KH
4953 struct cftype *cft, struct eventfd_ctx *eventfd)
4954{
c0ff4b85 4955 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
9490ff27 4956 struct mem_cgroup_eventfd_list *ev, *tmp;
86ae53e1 4957 enum res_type type = MEMFILE_TYPE(cft->private);
9490ff27
KH
4958
4959 BUG_ON(type != _OOM_TYPE);
4960
1af8efe9 4961 spin_lock(&memcg_oom_lock);
9490ff27 4962
c0ff4b85 4963 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
9490ff27
KH
4964 if (ev->eventfd == eventfd) {
4965 list_del(&ev->list);
4966 kfree(ev);
4967 }
4968 }
4969
1af8efe9 4970 spin_unlock(&memcg_oom_lock);
9490ff27
KH
4971}
4972
3c11ecf4
KH
4973static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4974 struct cftype *cft, struct cgroup_map_cb *cb)
4975{
c0ff4b85 4976 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3c11ecf4 4977
c0ff4b85 4978 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
3c11ecf4 4979
c0ff4b85 4980 if (atomic_read(&memcg->under_oom))
3c11ecf4
KH
4981 cb->fill(cb, "under_oom", 1);
4982 else
4983 cb->fill(cb, "under_oom", 0);
4984 return 0;
4985}
4986
3c11ecf4
KH
4987static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4988 struct cftype *cft, u64 val)
4989{
c0ff4b85 4990 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3c11ecf4
KH
4991 struct mem_cgroup *parent;
4992
4993 /* cannot set to root cgroup and only 0 and 1 are allowed */
4994 if (!cgrp->parent || !((val == 0) || (val == 1)))
4995 return -EINVAL;
4996
4997 parent = mem_cgroup_from_cont(cgrp->parent);
4998
4999 cgroup_lock();
5000 /* oom-kill-disable is a flag for subhierarchy. */
5001 if ((parent->use_hierarchy) ||
c0ff4b85 5002 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
3c11ecf4
KH
5003 cgroup_unlock();
5004 return -EINVAL;
5005 }
c0ff4b85 5006 memcg->oom_kill_disable = val;
4d845ebf 5007 if (!val)
c0ff4b85 5008 memcg_oom_recover(memcg);
3c11ecf4
KH
5009 cgroup_unlock();
5010 return 0;
5011}
5012
c255a458 5013#ifdef CONFIG_MEMCG_KMEM
cbe128e3 5014static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
e5671dfa 5015{
510fc4e1 5016 memcg_propagate_kmem(memcg);
1d62e436 5017 return mem_cgroup_sockets_init(memcg, ss);
e5671dfa
GC
5018};
5019
1d62e436 5020static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
d1a4c0b3 5021{
1d62e436 5022 mem_cgroup_sockets_destroy(memcg);
7de37682
GC
5023
5024 memcg_kmem_mark_dead(memcg);
5025
5026 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
5027 return;
5028
5029 /*
5030 * Charges already down to 0, undo mem_cgroup_get() done in the charge
5031 * path here, being careful not to race with memcg_uncharge_kmem: it is
5032 * possible that the charges went down to 0 between mark_dead and the
5033 * res_counter read, so in that case, we don't need the put
5034 */
5035 if (memcg_kmem_test_and_clear_dead(memcg))
5036 mem_cgroup_put(memcg);
d1a4c0b3 5037}
e5671dfa 5038#else
cbe128e3 5039static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
e5671dfa
GC
5040{
5041 return 0;
5042}
d1a4c0b3 5043
1d62e436 5044static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
d1a4c0b3
GC
5045{
5046}
e5671dfa
GC
5047#endif
5048
8cdea7c0
BS
5049static struct cftype mem_cgroup_files[] = {
5050 {
0eea1030 5051 .name = "usage_in_bytes",
8c7c6e34 5052 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
af36f906 5053 .read = mem_cgroup_read,
9490ff27
KH
5054 .register_event = mem_cgroup_usage_register_event,
5055 .unregister_event = mem_cgroup_usage_unregister_event,
8cdea7c0 5056 },
c84872e1
PE
5057 {
5058 .name = "max_usage_in_bytes",
8c7c6e34 5059 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
29f2a4da 5060 .trigger = mem_cgroup_reset,
af36f906 5061 .read = mem_cgroup_read,
c84872e1 5062 },
8cdea7c0 5063 {
0eea1030 5064 .name = "limit_in_bytes",
8c7c6e34 5065 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
856c13aa 5066 .write_string = mem_cgroup_write,
af36f906 5067 .read = mem_cgroup_read,
8cdea7c0 5068 },
296c81d8
BS
5069 {
5070 .name = "soft_limit_in_bytes",
5071 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5072 .write_string = mem_cgroup_write,
af36f906 5073 .read = mem_cgroup_read,
296c81d8 5074 },
8cdea7c0
BS
5075 {
5076 .name = "failcnt",
8c7c6e34 5077 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
29f2a4da 5078 .trigger = mem_cgroup_reset,
af36f906 5079 .read = mem_cgroup_read,
8cdea7c0 5080 },
d2ceb9b7
KH
5081 {
5082 .name = "stat",
ab215884 5083 .read_seq_string = memcg_stat_show,
d2ceb9b7 5084 },
c1e862c1
KH
5085 {
5086 .name = "force_empty",
5087 .trigger = mem_cgroup_force_empty_write,
5088 },
18f59ea7
BS
5089 {
5090 .name = "use_hierarchy",
5091 .write_u64 = mem_cgroup_hierarchy_write,
5092 .read_u64 = mem_cgroup_hierarchy_read,
5093 },
a7885eb8
KM
5094 {
5095 .name = "swappiness",
5096 .read_u64 = mem_cgroup_swappiness_read,
5097 .write_u64 = mem_cgroup_swappiness_write,
5098 },
7dc74be0
DN
5099 {
5100 .name = "move_charge_at_immigrate",
5101 .read_u64 = mem_cgroup_move_charge_read,
5102 .write_u64 = mem_cgroup_move_charge_write,
5103 },
9490ff27
KH
5104 {
5105 .name = "oom_control",
3c11ecf4
KH
5106 .read_map = mem_cgroup_oom_control_read,
5107 .write_u64 = mem_cgroup_oom_control_write,
9490ff27
KH
5108 .register_event = mem_cgroup_oom_register_event,
5109 .unregister_event = mem_cgroup_oom_unregister_event,
5110 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
5111 },
406eb0c9
YH
5112#ifdef CONFIG_NUMA
5113 {
5114 .name = "numa_stat",
ab215884 5115 .read_seq_string = memcg_numa_stat_show,
406eb0c9
YH
5116 },
5117#endif
c255a458 5118#ifdef CONFIG_MEMCG_SWAP
8c7c6e34
KH
5119 {
5120 .name = "memsw.usage_in_bytes",
5121 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
af36f906 5122 .read = mem_cgroup_read,
9490ff27
KH
5123 .register_event = mem_cgroup_usage_register_event,
5124 .unregister_event = mem_cgroup_usage_unregister_event,
8c7c6e34
KH
5125 },
5126 {
5127 .name = "memsw.max_usage_in_bytes",
5128 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
5129 .trigger = mem_cgroup_reset,
af36f906 5130 .read = mem_cgroup_read,
8c7c6e34
KH
5131 },
5132 {
5133 .name = "memsw.limit_in_bytes",
5134 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
5135 .write_string = mem_cgroup_write,
af36f906 5136 .read = mem_cgroup_read,
8c7c6e34
KH
5137 },
5138 {
5139 .name = "memsw.failcnt",
5140 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
5141 .trigger = mem_cgroup_reset,
af36f906 5142 .read = mem_cgroup_read,
8c7c6e34 5143 },
510fc4e1
GC
5144#endif
5145#ifdef CONFIG_MEMCG_KMEM
5146 {
5147 .name = "kmem.limit_in_bytes",
5148 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5149 .write_string = mem_cgroup_write,
5150 .read = mem_cgroup_read,
5151 },
5152 {
5153 .name = "kmem.usage_in_bytes",
5154 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
5155 .read = mem_cgroup_read,
5156 },
5157 {
5158 .name = "kmem.failcnt",
5159 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
5160 .trigger = mem_cgroup_reset,
5161 .read = mem_cgroup_read,
5162 },
5163 {
5164 .name = "kmem.max_usage_in_bytes",
5165 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
5166 .trigger = mem_cgroup_reset,
5167 .read = mem_cgroup_read,
5168 },
8c7c6e34 5169#endif
6bc10349 5170 { }, /* terminate */
af36f906 5171};
8c7c6e34 5172
c0ff4b85 5173static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
6d12e2d8
KH
5174{
5175 struct mem_cgroup_per_node *pn;
1ecaab2b 5176 struct mem_cgroup_per_zone *mz;
41e3355d 5177 int zone, tmp = node;
1ecaab2b
KH
5178 /*
5179 * This routine is called against possible nodes.
5180 * But it's BUG to call kmalloc() against offline node.
5181 *
5182 * TODO: this routine can waste much memory for nodes which will
5183 * never be onlined. It's better to use memory hotplug callback
5184 * function.
5185 */
41e3355d
KH
5186 if (!node_state(node, N_NORMAL_MEMORY))
5187 tmp = -1;
17295c88 5188 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
6d12e2d8
KH
5189 if (!pn)
5190 return 1;
1ecaab2b 5191
1ecaab2b
KH
5192 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5193 mz = &pn->zoneinfo[zone];
bea8c150 5194 lruvec_init(&mz->lruvec);
f64c3f54 5195 mz->usage_in_excess = 0;
4e416953 5196 mz->on_tree = false;
d79154bb 5197 mz->memcg = memcg;
1ecaab2b 5198 }
0a619e58 5199 memcg->info.nodeinfo[node] = pn;
6d12e2d8
KH
5200 return 0;
5201}
5202
c0ff4b85 5203static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
1ecaab2b 5204{
c0ff4b85 5205 kfree(memcg->info.nodeinfo[node]);
1ecaab2b
KH
5206}
5207
33327948
KH
5208static struct mem_cgroup *mem_cgroup_alloc(void)
5209{
d79154bb 5210 struct mem_cgroup *memcg;
c62b1a3b 5211 int size = sizeof(struct mem_cgroup);
33327948 5212
c62b1a3b 5213 /* Can be very big if MAX_NUMNODES is very big */
c8dad2bb 5214 if (size < PAGE_SIZE)
d79154bb 5215 memcg = kzalloc(size, GFP_KERNEL);
33327948 5216 else
d79154bb 5217 memcg = vzalloc(size);
33327948 5218
d79154bb 5219 if (!memcg)
e7bbcdf3
DC
5220 return NULL;
5221
d79154bb
HD
5222 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
5223 if (!memcg->stat)
d2e61b8d 5224 goto out_free;
d79154bb
HD
5225 spin_lock_init(&memcg->pcp_counter_lock);
5226 return memcg;
d2e61b8d
DC
5227
5228out_free:
5229 if (size < PAGE_SIZE)
d79154bb 5230 kfree(memcg);
d2e61b8d 5231 else
d79154bb 5232 vfree(memcg);
d2e61b8d 5233 return NULL;
33327948
KH
5234}
5235
59927fb9 5236/*
3afe36b1 5237 * Helpers for freeing a kmalloc()ed/vzalloc()ed mem_cgroup by RCU,
59927fb9
HD
5238 * but in process context. The work_freeing structure is overlaid
5239 * on the rcu_freeing structure, which itself is overlaid on memsw.
5240 */
3afe36b1 5241static void free_work(struct work_struct *work)
59927fb9
HD
5242{
5243 struct mem_cgroup *memcg;
3afe36b1 5244 int size = sizeof(struct mem_cgroup);
59927fb9
HD
5245
5246 memcg = container_of(work, struct mem_cgroup, work_freeing);
3f134619
GC
5247 /*
5248 * We need to make sure that (at least for now), the jump label
5249 * destruction code runs outside of the cgroup lock. This is because
5250 * get_online_cpus(), which is called from the static_branch update,
5251 * can't be called inside the cgroup_lock. cpusets are the ones
5252 * enforcing this dependency, so if they ever change, we might as well.
5253 *
5254 * schedule_work() will guarantee this happens. Be careful if you need
5255 * to move this code around, and make sure it is outside
5256 * the cgroup_lock.
5257 */
a8964b9b 5258 disarm_static_keys(memcg);
3afe36b1
GC
5259 if (size < PAGE_SIZE)
5260 kfree(memcg);
5261 else
5262 vfree(memcg);
59927fb9 5263}
3afe36b1
GC
5264
5265static void free_rcu(struct rcu_head *rcu_head)
59927fb9
HD
5266{
5267 struct mem_cgroup *memcg;
5268
5269 memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing);
3afe36b1 5270 INIT_WORK(&memcg->work_freeing, free_work);
59927fb9
HD
5271 schedule_work(&memcg->work_freeing);
5272}
5273
8c7c6e34
KH
5274/*
5275 * At destroying mem_cgroup, references from swap_cgroup can remain.
5276 * (scanning all at force_empty is too costly...)
5277 *
5278 * Instead of clearing all references at force_empty, we remember
5279 * the number of reference from swap_cgroup and free mem_cgroup when
5280 * it goes down to 0.
5281 *
8c7c6e34
KH
5282 * Removal of cgroup itself succeeds regardless of refs from swap.
5283 */
5284
c0ff4b85 5285static void __mem_cgroup_free(struct mem_cgroup *memcg)
33327948 5286{
08e552c6
KH
5287 int node;
5288
c0ff4b85
R
5289 mem_cgroup_remove_from_trees(memcg);
5290 free_css_id(&mem_cgroup_subsys, &memcg->css);
04046e1a 5291
3ed28fa1 5292 for_each_node(node)
c0ff4b85 5293 free_mem_cgroup_per_zone_info(memcg, node);
08e552c6 5294
c0ff4b85 5295 free_percpu(memcg->stat);
3afe36b1 5296 call_rcu(&memcg->rcu_freeing, free_rcu);
33327948
KH
5297}
5298
c0ff4b85 5299static void mem_cgroup_get(struct mem_cgroup *memcg)
8c7c6e34 5300{
c0ff4b85 5301 atomic_inc(&memcg->refcnt);
8c7c6e34
KH
5302}
5303
c0ff4b85 5304static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
8c7c6e34 5305{
c0ff4b85
R
5306 if (atomic_sub_and_test(count, &memcg->refcnt)) {
5307 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5308 __mem_cgroup_free(memcg);
7bcc1bb1
DN
5309 if (parent)
5310 mem_cgroup_put(parent);
5311 }
8c7c6e34
KH
5312}
5313
c0ff4b85 5314static void mem_cgroup_put(struct mem_cgroup *memcg)
483c30b5 5315{
c0ff4b85 5316 __mem_cgroup_put(memcg, 1);
483c30b5
DN
5317}
5318
7bcc1bb1
DN
5319/*
5320 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
5321 */
e1aab161 5322struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
7bcc1bb1 5323{
c0ff4b85 5324 if (!memcg->res.parent)
7bcc1bb1 5325 return NULL;
c0ff4b85 5326 return mem_cgroup_from_res_counter(memcg->res.parent, res);
7bcc1bb1 5327}
e1aab161 5328EXPORT_SYMBOL(parent_mem_cgroup);
33327948 5329
c255a458 5330#ifdef CONFIG_MEMCG_SWAP
c077719b
KH
5331static void __init enable_swap_cgroup(void)
5332{
f8d66542 5333 if (!mem_cgroup_disabled() && really_do_swap_account)
c077719b
KH
5334 do_swap_account = 1;
5335}
5336#else
5337static void __init enable_swap_cgroup(void)
5338{
5339}
5340#endif
5341
f64c3f54
BS
5342static int mem_cgroup_soft_limit_tree_init(void)
5343{
5344 struct mem_cgroup_tree_per_node *rtpn;
5345 struct mem_cgroup_tree_per_zone *rtpz;
5346 int tmp, node, zone;
5347
3ed28fa1 5348 for_each_node(node) {
f64c3f54
BS
5349 tmp = node;
5350 if (!node_state(node, N_NORMAL_MEMORY))
5351 tmp = -1;
5352 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
5353 if (!rtpn)
c3cecc68 5354 goto err_cleanup;
f64c3f54
BS
5355
5356 soft_limit_tree.rb_tree_per_node[node] = rtpn;
5357
5358 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5359 rtpz = &rtpn->rb_tree_per_zone[zone];
5360 rtpz->rb_root = RB_ROOT;
5361 spin_lock_init(&rtpz->lock);
5362 }
5363 }
5364 return 0;
c3cecc68
MH
5365
5366err_cleanup:
3ed28fa1 5367 for_each_node(node) {
c3cecc68
MH
5368 if (!soft_limit_tree.rb_tree_per_node[node])
5369 break;
5370 kfree(soft_limit_tree.rb_tree_per_node[node]);
5371 soft_limit_tree.rb_tree_per_node[node] = NULL;
5372 }
5373 return 1;
5374
f64c3f54
BS
5375}
5376
0eb253e2 5377static struct cgroup_subsys_state * __ref
92fb9748 5378mem_cgroup_css_alloc(struct cgroup *cont)
8cdea7c0 5379{
c0ff4b85 5380 struct mem_cgroup *memcg, *parent;
04046e1a 5381 long error = -ENOMEM;
6d12e2d8 5382 int node;
8cdea7c0 5383
c0ff4b85
R
5384 memcg = mem_cgroup_alloc();
5385 if (!memcg)
04046e1a 5386 return ERR_PTR(error);
78fb7466 5387
3ed28fa1 5388 for_each_node(node)
c0ff4b85 5389 if (alloc_mem_cgroup_per_zone_info(memcg, node))
6d12e2d8 5390 goto free_out;
f64c3f54 5391
c077719b 5392 /* root ? */
28dbc4b6 5393 if (cont->parent == NULL) {
cdec2e42 5394 int cpu;
c077719b 5395 enable_swap_cgroup();
28dbc4b6 5396 parent = NULL;
f64c3f54
BS
5397 if (mem_cgroup_soft_limit_tree_init())
5398 goto free_out;
a41c58a6 5399 root_mem_cgroup = memcg;
cdec2e42
KH
5400 for_each_possible_cpu(cpu) {
5401 struct memcg_stock_pcp *stock =
5402 &per_cpu(memcg_stock, cpu);
5403 INIT_WORK(&stock->work, drain_local_stock);
5404 }
711d3d2c 5405 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
18f59ea7 5406 } else {
28dbc4b6 5407 parent = mem_cgroup_from_cont(cont->parent);
c0ff4b85
R
5408 memcg->use_hierarchy = parent->use_hierarchy;
5409 memcg->oom_kill_disable = parent->oom_kill_disable;
18f59ea7 5410 }
28dbc4b6 5411
18f59ea7 5412 if (parent && parent->use_hierarchy) {
c0ff4b85
R
5413 res_counter_init(&memcg->res, &parent->res);
5414 res_counter_init(&memcg->memsw, &parent->memsw);
510fc4e1 5415 res_counter_init(&memcg->kmem, &parent->kmem);
7bcc1bb1
DN
5416 /*
5417 * We increment refcnt of the parent to ensure that we can
5418 * safely access it on res_counter_charge/uncharge.
5419 * This refcnt will be decremented when freeing this
5420 * mem_cgroup(see mem_cgroup_put).
5421 */
5422 mem_cgroup_get(parent);
18f59ea7 5423 } else {
c0ff4b85
R
5424 res_counter_init(&memcg->res, NULL);
5425 res_counter_init(&memcg->memsw, NULL);
510fc4e1 5426 res_counter_init(&memcg->kmem, NULL);
8c7f6edb
TH
5427 /*
5428 * Deeper hierachy with use_hierarchy == false doesn't make
5429 * much sense so let cgroup subsystem know about this
5430 * unfortunate state in our controller.
5431 */
5432 if (parent && parent != root_mem_cgroup)
5433 mem_cgroup_subsys.broken_hierarchy = true;
18f59ea7 5434 }
c0ff4b85
R
5435 memcg->last_scanned_node = MAX_NUMNODES;
5436 INIT_LIST_HEAD(&memcg->oom_notify);
6d61ef40 5437
a7885eb8 5438 if (parent)
c0ff4b85
R
5439 memcg->swappiness = mem_cgroup_swappiness(parent);
5440 atomic_set(&memcg->refcnt, 1);
5441 memcg->move_charge_at_immigrate = 0;
5442 mutex_init(&memcg->thresholds_lock);
312734c0 5443 spin_lock_init(&memcg->move_lock);
cbe128e3
GC
5444
5445 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
5446 if (error) {
5447 /*
5448 * We call put now because our (and parent's) refcnts
5449 * are already in place. mem_cgroup_put() will internally
5450 * call __mem_cgroup_free, so return directly
5451 */
5452 mem_cgroup_put(memcg);
5453 return ERR_PTR(error);
5454 }
c0ff4b85 5455 return &memcg->css;
6d12e2d8 5456free_out:
c0ff4b85 5457 __mem_cgroup_free(memcg);
04046e1a 5458 return ERR_PTR(error);
8cdea7c0
BS
5459}
5460
92fb9748 5461static void mem_cgroup_css_offline(struct cgroup *cont)
df878fb0 5462{
c0ff4b85 5463 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
ec64f515 5464
ab5196c2 5465 mem_cgroup_reparent_charges(memcg);
df878fb0
KH
5466}
5467
92fb9748 5468static void mem_cgroup_css_free(struct cgroup *cont)
8cdea7c0 5469{
c0ff4b85 5470 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
c268e994 5471
1d62e436 5472 kmem_cgroup_destroy(memcg);
d1a4c0b3 5473
c0ff4b85 5474 mem_cgroup_put(memcg);
8cdea7c0
BS
5475}
5476
02491447 5477#ifdef CONFIG_MMU
7dc74be0 5478/* Handlers for move charge at task migration. */
854ffa8d
DN
5479#define PRECHARGE_COUNT_AT_ONCE 256
5480static int mem_cgroup_do_precharge(unsigned long count)
7dc74be0 5481{
854ffa8d
DN
5482 int ret = 0;
5483 int batch_count = PRECHARGE_COUNT_AT_ONCE;
c0ff4b85 5484 struct mem_cgroup *memcg = mc.to;
4ffef5fe 5485
c0ff4b85 5486 if (mem_cgroup_is_root(memcg)) {
854ffa8d
DN
5487 mc.precharge += count;
5488 /* we don't need css_get for root */
5489 return ret;
5490 }
5491 /* try to charge at once */
5492 if (count > 1) {
5493 struct res_counter *dummy;
5494 /*
c0ff4b85 5495 * "memcg" cannot be under rmdir() because we've already checked
854ffa8d
DN
5496 * by cgroup_lock_live_cgroup() that it is not removed and we
5497 * are still under the same cgroup_mutex. So we can postpone
5498 * css_get().
5499 */
c0ff4b85 5500 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
854ffa8d 5501 goto one_by_one;
c0ff4b85 5502 if (do_swap_account && res_counter_charge(&memcg->memsw,
854ffa8d 5503 PAGE_SIZE * count, &dummy)) {
c0ff4b85 5504 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
854ffa8d
DN
5505 goto one_by_one;
5506 }
5507 mc.precharge += count;
854ffa8d
DN
5508 return ret;
5509 }
5510one_by_one:
5511 /* fall back to one by one charge */
5512 while (count--) {
5513 if (signal_pending(current)) {
5514 ret = -EINTR;
5515 break;
5516 }
5517 if (!batch_count--) {
5518 batch_count = PRECHARGE_COUNT_AT_ONCE;
5519 cond_resched();
5520 }
c0ff4b85
R
5521 ret = __mem_cgroup_try_charge(NULL,
5522 GFP_KERNEL, 1, &memcg, false);
38c5d72f 5523 if (ret)
854ffa8d 5524 /* mem_cgroup_clear_mc() will do uncharge later */
38c5d72f 5525 return ret;
854ffa8d
DN
5526 mc.precharge++;
5527 }
4ffef5fe
DN
5528 return ret;
5529}
5530
5531/**
8d32ff84 5532 * get_mctgt_type - get target type of moving charge
4ffef5fe
DN
5533 * @vma: the vma the pte to be checked belongs
5534 * @addr: the address corresponding to the pte to be checked
5535 * @ptent: the pte to be checked
02491447 5536 * @target: the pointer the target page or swap ent will be stored(can be NULL)
4ffef5fe
DN
5537 *
5538 * Returns
5539 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5540 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5541 * move charge. if @target is not NULL, the page is stored in target->page
5542 * with extra refcnt got(Callers should handle it).
02491447
DN
5543 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5544 * target for charge migration. if @target is not NULL, the entry is stored
5545 * in target->ent.
4ffef5fe
DN
5546 *
5547 * Called with pte lock held.
5548 */
4ffef5fe
DN
5549union mc_target {
5550 struct page *page;
02491447 5551 swp_entry_t ent;
4ffef5fe
DN
5552};
5553
4ffef5fe 5554enum mc_target_type {
8d32ff84 5555 MC_TARGET_NONE = 0,
4ffef5fe 5556 MC_TARGET_PAGE,
02491447 5557 MC_TARGET_SWAP,
4ffef5fe
DN
5558};
5559
90254a65
DN
5560static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5561 unsigned long addr, pte_t ptent)
4ffef5fe 5562{
90254a65 5563 struct page *page = vm_normal_page(vma, addr, ptent);
4ffef5fe 5564
90254a65
DN
5565 if (!page || !page_mapped(page))
5566 return NULL;
5567 if (PageAnon(page)) {
5568 /* we don't move shared anon */
4b91355e 5569 if (!move_anon())
90254a65 5570 return NULL;
87946a72
DN
5571 } else if (!move_file())
5572 /* we ignore mapcount for file pages */
90254a65
DN
5573 return NULL;
5574 if (!get_page_unless_zero(page))
5575 return NULL;
5576
5577 return page;
5578}
5579
4b91355e 5580#ifdef CONFIG_SWAP
90254a65
DN
5581static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5582 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5583{
90254a65
DN
5584 struct page *page = NULL;
5585 swp_entry_t ent = pte_to_swp_entry(ptent);
5586
5587 if (!move_anon() || non_swap_entry(ent))
5588 return NULL;
4b91355e
KH
5589 /*
5590 * Because lookup_swap_cache() updates some statistics counter,
5591 * we call find_get_page() with swapper_space directly.
5592 */
5593 page = find_get_page(&swapper_space, ent.val);
90254a65
DN
5594 if (do_swap_account)
5595 entry->val = ent.val;
5596
5597 return page;
5598}
4b91355e
KH
5599#else
5600static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5601 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5602{
5603 return NULL;
5604}
5605#endif
90254a65 5606
87946a72
DN
5607static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5608 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5609{
5610 struct page *page = NULL;
87946a72
DN
5611 struct address_space *mapping;
5612 pgoff_t pgoff;
5613
5614 if (!vma->vm_file) /* anonymous vma */
5615 return NULL;
5616 if (!move_file())
5617 return NULL;
5618
87946a72
DN
5619 mapping = vma->vm_file->f_mapping;
5620 if (pte_none(ptent))
5621 pgoff = linear_page_index(vma, addr);
5622 else /* pte_file(ptent) is true */
5623 pgoff = pte_to_pgoff(ptent);
5624
5625 /* page is moved even if it's not RSS of this task(page-faulted). */
aa3b1895
HD
5626 page = find_get_page(mapping, pgoff);
5627
5628#ifdef CONFIG_SWAP
5629 /* shmem/tmpfs may report page out on swap: account for that too. */
5630 if (radix_tree_exceptional_entry(page)) {
5631 swp_entry_t swap = radix_to_swp_entry(page);
87946a72 5632 if (do_swap_account)
aa3b1895
HD
5633 *entry = swap;
5634 page = find_get_page(&swapper_space, swap.val);
87946a72 5635 }
aa3b1895 5636#endif
87946a72
DN
5637 return page;
5638}
5639
8d32ff84 5640static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
90254a65
DN
5641 unsigned long addr, pte_t ptent, union mc_target *target)
5642{
5643 struct page *page = NULL;
5644 struct page_cgroup *pc;
8d32ff84 5645 enum mc_target_type ret = MC_TARGET_NONE;
90254a65
DN
5646 swp_entry_t ent = { .val = 0 };
5647
5648 if (pte_present(ptent))
5649 page = mc_handle_present_pte(vma, addr, ptent);
5650 else if (is_swap_pte(ptent))
5651 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
87946a72
DN
5652 else if (pte_none(ptent) || pte_file(ptent))
5653 page = mc_handle_file_pte(vma, addr, ptent, &ent);
90254a65
DN
5654
5655 if (!page && !ent.val)
8d32ff84 5656 return ret;
02491447
DN
5657 if (page) {
5658 pc = lookup_page_cgroup(page);
5659 /*
5660 * Do only loose check w/o page_cgroup lock.
5661 * mem_cgroup_move_account() checks the pc is valid or not under
5662 * the lock.
5663 */
5664 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5665 ret = MC_TARGET_PAGE;
5666 if (target)
5667 target->page = page;
5668 }
5669 if (!ret || !target)
5670 put_page(page);
5671 }
90254a65
DN
5672 /* There is a swap entry and a page doesn't exist or isn't charged */
5673 if (ent.val && !ret &&
9fb4b7cc 5674 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
7f0f1546
KH
5675 ret = MC_TARGET_SWAP;
5676 if (target)
5677 target->ent = ent;
4ffef5fe 5678 }
4ffef5fe
DN
5679 return ret;
5680}
5681
12724850
NH
5682#ifdef CONFIG_TRANSPARENT_HUGEPAGE
5683/*
5684 * We don't consider swapping or file mapped pages because THP does not
5685 * support them for now.
5686 * Caller should make sure that pmd_trans_huge(pmd) is true.
5687 */
5688static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5689 unsigned long addr, pmd_t pmd, union mc_target *target)
5690{
5691 struct page *page = NULL;
5692 struct page_cgroup *pc;
5693 enum mc_target_type ret = MC_TARGET_NONE;
5694
5695 page = pmd_page(pmd);
5696 VM_BUG_ON(!page || !PageHead(page));
5697 if (!move_anon())
5698 return ret;
5699 pc = lookup_page_cgroup(page);
5700 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5701 ret = MC_TARGET_PAGE;
5702 if (target) {
5703 get_page(page);
5704 target->page = page;
5705 }
5706 }
5707 return ret;
5708}
5709#else
5710static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5711 unsigned long addr, pmd_t pmd, union mc_target *target)
5712{
5713 return MC_TARGET_NONE;
5714}
5715#endif
5716
4ffef5fe
DN
5717static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5718 unsigned long addr, unsigned long end,
5719 struct mm_walk *walk)
5720{
5721 struct vm_area_struct *vma = walk->private;
5722 pte_t *pte;
5723 spinlock_t *ptl;
5724
12724850
NH
5725 if (pmd_trans_huge_lock(pmd, vma) == 1) {
5726 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
5727 mc.precharge += HPAGE_PMD_NR;
5728 spin_unlock(&vma->vm_mm->page_table_lock);
1a5a9906 5729 return 0;
12724850 5730 }
03319327 5731
45f83cef
AA
5732 if (pmd_trans_unstable(pmd))
5733 return 0;
4ffef5fe
DN
5734 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5735 for (; addr != end; pte++, addr += PAGE_SIZE)
8d32ff84 5736 if (get_mctgt_type(vma, addr, *pte, NULL))
4ffef5fe
DN
5737 mc.precharge++; /* increment precharge temporarily */
5738 pte_unmap_unlock(pte - 1, ptl);
5739 cond_resched();
5740
7dc74be0
DN
5741 return 0;
5742}
5743
4ffef5fe
DN
5744static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5745{
5746 unsigned long precharge;
5747 struct vm_area_struct *vma;
5748
dfe076b0 5749 down_read(&mm->mmap_sem);
4ffef5fe
DN
5750 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5751 struct mm_walk mem_cgroup_count_precharge_walk = {
5752 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5753 .mm = mm,
5754 .private = vma,
5755 };
5756 if (is_vm_hugetlb_page(vma))
5757 continue;
4ffef5fe
DN
5758 walk_page_range(vma->vm_start, vma->vm_end,
5759 &mem_cgroup_count_precharge_walk);
5760 }
dfe076b0 5761 up_read(&mm->mmap_sem);
4ffef5fe
DN
5762
5763 precharge = mc.precharge;
5764 mc.precharge = 0;
5765
5766 return precharge;
5767}
5768
4ffef5fe
DN
5769static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5770{
dfe076b0
DN
5771 unsigned long precharge = mem_cgroup_count_precharge(mm);
5772
5773 VM_BUG_ON(mc.moving_task);
5774 mc.moving_task = current;
5775 return mem_cgroup_do_precharge(precharge);
4ffef5fe
DN
5776}
5777
dfe076b0
DN
5778/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5779static void __mem_cgroup_clear_mc(void)
4ffef5fe 5780{
2bd9bb20
KH
5781 struct mem_cgroup *from = mc.from;
5782 struct mem_cgroup *to = mc.to;
5783
4ffef5fe 5784 /* we must uncharge all the leftover precharges from mc.to */
854ffa8d
DN
5785 if (mc.precharge) {
5786 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
5787 mc.precharge = 0;
5788 }
5789 /*
5790 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5791 * we must uncharge here.
5792 */
5793 if (mc.moved_charge) {
5794 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
5795 mc.moved_charge = 0;
4ffef5fe 5796 }
483c30b5
DN
5797 /* we must fixup refcnts and charges */
5798 if (mc.moved_swap) {
483c30b5
DN
5799 /* uncharge swap account from the old cgroup */
5800 if (!mem_cgroup_is_root(mc.from))
5801 res_counter_uncharge(&mc.from->memsw,
5802 PAGE_SIZE * mc.moved_swap);
5803 __mem_cgroup_put(mc.from, mc.moved_swap);
5804
5805 if (!mem_cgroup_is_root(mc.to)) {
5806 /*
5807 * we charged both to->res and to->memsw, so we should
5808 * uncharge to->res.
5809 */
5810 res_counter_uncharge(&mc.to->res,
5811 PAGE_SIZE * mc.moved_swap);
483c30b5
DN
5812 }
5813 /* we've already done mem_cgroup_get(mc.to) */
483c30b5
DN
5814 mc.moved_swap = 0;
5815 }
dfe076b0
DN
5816 memcg_oom_recover(from);
5817 memcg_oom_recover(to);
5818 wake_up_all(&mc.waitq);
5819}
5820
5821static void mem_cgroup_clear_mc(void)
5822{
5823 struct mem_cgroup *from = mc.from;
5824
5825 /*
5826 * we must clear moving_task before waking up waiters at the end of
5827 * task migration.
5828 */
5829 mc.moving_task = NULL;
5830 __mem_cgroup_clear_mc();
2bd9bb20 5831 spin_lock(&mc.lock);
4ffef5fe
DN
5832 mc.from = NULL;
5833 mc.to = NULL;
2bd9bb20 5834 spin_unlock(&mc.lock);
32047e2a 5835 mem_cgroup_end_move(from);
4ffef5fe
DN
5836}
5837
761b3ef5
LZ
5838static int mem_cgroup_can_attach(struct cgroup *cgroup,
5839 struct cgroup_taskset *tset)
7dc74be0 5840{
2f7ee569 5841 struct task_struct *p = cgroup_taskset_first(tset);
7dc74be0 5842 int ret = 0;
c0ff4b85 5843 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
7dc74be0 5844
c0ff4b85 5845 if (memcg->move_charge_at_immigrate) {
7dc74be0
DN
5846 struct mm_struct *mm;
5847 struct mem_cgroup *from = mem_cgroup_from_task(p);
5848
c0ff4b85 5849 VM_BUG_ON(from == memcg);
7dc74be0
DN
5850
5851 mm = get_task_mm(p);
5852 if (!mm)
5853 return 0;
7dc74be0 5854 /* We move charges only when we move a owner of the mm */
4ffef5fe
DN
5855 if (mm->owner == p) {
5856 VM_BUG_ON(mc.from);
5857 VM_BUG_ON(mc.to);
5858 VM_BUG_ON(mc.precharge);
854ffa8d 5859 VM_BUG_ON(mc.moved_charge);
483c30b5 5860 VM_BUG_ON(mc.moved_swap);
32047e2a 5861 mem_cgroup_start_move(from);
2bd9bb20 5862 spin_lock(&mc.lock);
4ffef5fe 5863 mc.from = from;
c0ff4b85 5864 mc.to = memcg;
2bd9bb20 5865 spin_unlock(&mc.lock);
dfe076b0 5866 /* We set mc.moving_task later */
4ffef5fe
DN
5867
5868 ret = mem_cgroup_precharge_mc(mm);
5869 if (ret)
5870 mem_cgroup_clear_mc();
dfe076b0
DN
5871 }
5872 mmput(mm);
7dc74be0
DN
5873 }
5874 return ret;
5875}
5876
761b3ef5
LZ
5877static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
5878 struct cgroup_taskset *tset)
7dc74be0 5879{
4ffef5fe 5880 mem_cgroup_clear_mc();
7dc74be0
DN
5881}
5882
4ffef5fe
DN
5883static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5884 unsigned long addr, unsigned long end,
5885 struct mm_walk *walk)
7dc74be0 5886{
4ffef5fe
DN
5887 int ret = 0;
5888 struct vm_area_struct *vma = walk->private;
5889 pte_t *pte;
5890 spinlock_t *ptl;
12724850
NH
5891 enum mc_target_type target_type;
5892 union mc_target target;
5893 struct page *page;
5894 struct page_cgroup *pc;
4ffef5fe 5895
12724850
NH
5896 /*
5897 * We don't take compound_lock() here but no race with splitting thp
5898 * happens because:
5899 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
5900 * under splitting, which means there's no concurrent thp split,
5901 * - if another thread runs into split_huge_page() just after we
5902 * entered this if-block, the thread must wait for page table lock
5903 * to be unlocked in __split_huge_page_splitting(), where the main
5904 * part of thp split is not executed yet.
5905 */
5906 if (pmd_trans_huge_lock(pmd, vma) == 1) {
62ade86a 5907 if (mc.precharge < HPAGE_PMD_NR) {
12724850
NH
5908 spin_unlock(&vma->vm_mm->page_table_lock);
5909 return 0;
5910 }
5911 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
5912 if (target_type == MC_TARGET_PAGE) {
5913 page = target.page;
5914 if (!isolate_lru_page(page)) {
5915 pc = lookup_page_cgroup(page);
5916 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
2f3479b1 5917 pc, mc.from, mc.to)) {
12724850
NH
5918 mc.precharge -= HPAGE_PMD_NR;
5919 mc.moved_charge += HPAGE_PMD_NR;
5920 }
5921 putback_lru_page(page);
5922 }
5923 put_page(page);
5924 }
5925 spin_unlock(&vma->vm_mm->page_table_lock);
1a5a9906 5926 return 0;
12724850
NH
5927 }
5928
45f83cef
AA
5929 if (pmd_trans_unstable(pmd))
5930 return 0;
4ffef5fe
DN
5931retry:
5932 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5933 for (; addr != end; addr += PAGE_SIZE) {
5934 pte_t ptent = *(pte++);
02491447 5935 swp_entry_t ent;
4ffef5fe
DN
5936
5937 if (!mc.precharge)
5938 break;
5939
8d32ff84 5940 switch (get_mctgt_type(vma, addr, ptent, &target)) {
4ffef5fe
DN
5941 case MC_TARGET_PAGE:
5942 page = target.page;
5943 if (isolate_lru_page(page))
5944 goto put;
5945 pc = lookup_page_cgroup(page);
7ec99d62 5946 if (!mem_cgroup_move_account(page, 1, pc,
2f3479b1 5947 mc.from, mc.to)) {
4ffef5fe 5948 mc.precharge--;
854ffa8d
DN
5949 /* we uncharge from mc.from later. */
5950 mc.moved_charge++;
4ffef5fe
DN
5951 }
5952 putback_lru_page(page);
8d32ff84 5953put: /* get_mctgt_type() gets the page */
4ffef5fe
DN
5954 put_page(page);
5955 break;
02491447
DN
5956 case MC_TARGET_SWAP:
5957 ent = target.ent;
e91cbb42 5958 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
02491447 5959 mc.precharge--;
483c30b5
DN
5960 /* we fixup refcnts and charges later. */
5961 mc.moved_swap++;
5962 }
02491447 5963 break;
4ffef5fe
DN
5964 default:
5965 break;
5966 }
5967 }
5968 pte_unmap_unlock(pte - 1, ptl);
5969 cond_resched();
5970
5971 if (addr != end) {
5972 /*
5973 * We have consumed all precharges we got in can_attach().
5974 * We try charge one by one, but don't do any additional
5975 * charges to mc.to if we have failed in charge once in attach()
5976 * phase.
5977 */
854ffa8d 5978 ret = mem_cgroup_do_precharge(1);
4ffef5fe
DN
5979 if (!ret)
5980 goto retry;
5981 }
5982
5983 return ret;
5984}
5985
5986static void mem_cgroup_move_charge(struct mm_struct *mm)
5987{
5988 struct vm_area_struct *vma;
5989
5990 lru_add_drain_all();
dfe076b0
DN
5991retry:
5992 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5993 /*
5994 * Someone who are holding the mmap_sem might be waiting in
5995 * waitq. So we cancel all extra charges, wake up all waiters,
5996 * and retry. Because we cancel precharges, we might not be able
5997 * to move enough charges, but moving charge is a best-effort
5998 * feature anyway, so it wouldn't be a big problem.
5999 */
6000 __mem_cgroup_clear_mc();
6001 cond_resched();
6002 goto retry;
6003 }
4ffef5fe
DN
6004 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6005 int ret;
6006 struct mm_walk mem_cgroup_move_charge_walk = {
6007 .pmd_entry = mem_cgroup_move_charge_pte_range,
6008 .mm = mm,
6009 .private = vma,
6010 };
6011 if (is_vm_hugetlb_page(vma))
6012 continue;
4ffef5fe
DN
6013 ret = walk_page_range(vma->vm_start, vma->vm_end,
6014 &mem_cgroup_move_charge_walk);
6015 if (ret)
6016 /*
6017 * means we have consumed all precharges and failed in
6018 * doing additional charge. Just abandon here.
6019 */
6020 break;
6021 }
dfe076b0 6022 up_read(&mm->mmap_sem);
7dc74be0
DN
6023}
6024
761b3ef5
LZ
6025static void mem_cgroup_move_task(struct cgroup *cont,
6026 struct cgroup_taskset *tset)
67e465a7 6027{
2f7ee569 6028 struct task_struct *p = cgroup_taskset_first(tset);
a433658c 6029 struct mm_struct *mm = get_task_mm(p);
dfe076b0 6030
dfe076b0 6031 if (mm) {
a433658c
KM
6032 if (mc.to)
6033 mem_cgroup_move_charge(mm);
dfe076b0
DN
6034 mmput(mm);
6035 }
a433658c
KM
6036 if (mc.to)
6037 mem_cgroup_clear_mc();
67e465a7 6038}
5cfb80a7 6039#else /* !CONFIG_MMU */
761b3ef5
LZ
6040static int mem_cgroup_can_attach(struct cgroup *cgroup,
6041 struct cgroup_taskset *tset)
5cfb80a7
DN
6042{
6043 return 0;
6044}
761b3ef5
LZ
6045static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6046 struct cgroup_taskset *tset)
5cfb80a7
DN
6047{
6048}
761b3ef5
LZ
6049static void mem_cgroup_move_task(struct cgroup *cont,
6050 struct cgroup_taskset *tset)
5cfb80a7
DN
6051{
6052}
6053#endif
67e465a7 6054
8cdea7c0
BS
6055struct cgroup_subsys mem_cgroup_subsys = {
6056 .name = "memory",
6057 .subsys_id = mem_cgroup_subsys_id,
92fb9748
TH
6058 .css_alloc = mem_cgroup_css_alloc,
6059 .css_offline = mem_cgroup_css_offline,
6060 .css_free = mem_cgroup_css_free,
7dc74be0
DN
6061 .can_attach = mem_cgroup_can_attach,
6062 .cancel_attach = mem_cgroup_cancel_attach,
67e465a7 6063 .attach = mem_cgroup_move_task,
6bc10349 6064 .base_cftypes = mem_cgroup_files,
6d12e2d8 6065 .early_init = 0,
04046e1a 6066 .use_id = 1,
8cdea7c0 6067};
c077719b 6068
c255a458 6069#ifdef CONFIG_MEMCG_SWAP
a42c390c
MH
6070static int __init enable_swap_account(char *s)
6071{
6072 /* consider enabled if no parameter or 1 is given */
a2c8990a 6073 if (!strcmp(s, "1"))
a42c390c 6074 really_do_swap_account = 1;
a2c8990a 6075 else if (!strcmp(s, "0"))
a42c390c
MH
6076 really_do_swap_account = 0;
6077 return 1;
6078}
a2c8990a 6079__setup("swapaccount=", enable_swap_account);
c077719b 6080
c077719b 6081#endif