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