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