aboutsummaryrefslogtreecommitdiffstats
path: root/mm/memcontrol.c
diff options
context:
space:
mode:
Diffstat (limited to 'mm/memcontrol.c')
-rw-r--r--mm/memcontrol.c1427
1 files changed, 1190 insertions, 237 deletions
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 954032b80bed..0f711c213d2e 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -6,6 +6,10 @@
6 * Copyright 2007 OpenVZ SWsoft Inc 6 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org> 7 * Author: Pavel Emelianov <xemul@openvz.org>
8 * 8 *
9 * Memory thresholds
10 * Copyright (C) 2009 Nokia Corporation
11 * Author: Kirill A. Shutemov
12 *
9 * This program is free software; you can redistribute it and/or modify 13 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by 14 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or 15 * the Free Software Foundation; either version 2 of the License, or
@@ -21,6 +25,7 @@
21#include <linux/memcontrol.h> 25#include <linux/memcontrol.h>
22#include <linux/cgroup.h> 26#include <linux/cgroup.h>
23#include <linux/mm.h> 27#include <linux/mm.h>
28#include <linux/hugetlb.h>
24#include <linux/pagemap.h> 29#include <linux/pagemap.h>
25#include <linux/smp.h> 30#include <linux/smp.h>
26#include <linux/page-flags.h> 31#include <linux/page-flags.h>
@@ -32,7 +37,10 @@
32#include <linux/rbtree.h> 37#include <linux/rbtree.h>
33#include <linux/slab.h> 38#include <linux/slab.h>
34#include <linux/swap.h> 39#include <linux/swap.h>
40#include <linux/swapops.h>
35#include <linux/spinlock.h> 41#include <linux/spinlock.h>
42#include <linux/eventfd.h>
43#include <linux/sort.h>
36#include <linux/fs.h> 44#include <linux/fs.h>
37#include <linux/seq_file.h> 45#include <linux/seq_file.h>
38#include <linux/vmalloc.h> 46#include <linux/vmalloc.h>
@@ -55,7 +63,15 @@ static int really_do_swap_account __initdata = 1; /* for remember boot option*/
55#define do_swap_account (0) 63#define do_swap_account (0)
56#endif 64#endif
57 65
58#define SOFTLIMIT_EVENTS_THRESH (1000) 66/*
67 * Per memcg event counter is incremented at every pagein/pageout. This counter
68 * is used for trigger some periodic events. This is straightforward and better
69 * than using jiffies etc. to handle periodic memcg event.
70 *
71 * These values will be used as !((event) & ((1 <<(thresh)) - 1))
72 */
73#define THRESHOLDS_EVENTS_THRESH (7) /* once in 128 */
74#define SOFTLIMIT_EVENTS_THRESH (10) /* once in 1024 */
59 75
60/* 76/*
61 * Statistics for memory cgroup. 77 * Statistics for memory cgroup.
@@ -69,62 +85,16 @@ enum mem_cgroup_stat_index {
69 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */ 85 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
70 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */ 86 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
71 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */ 87 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
72 MEM_CGROUP_STAT_EVENTS, /* sum of pagein + pageout for internal use */
73 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */ 88 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
89 MEM_CGROUP_EVENTS, /* incremented at every pagein/pageout */
74 90
75 MEM_CGROUP_STAT_NSTATS, 91 MEM_CGROUP_STAT_NSTATS,
76}; 92};
77 93
78struct mem_cgroup_stat_cpu { 94struct mem_cgroup_stat_cpu {
79 s64 count[MEM_CGROUP_STAT_NSTATS]; 95 s64 count[MEM_CGROUP_STAT_NSTATS];
80} ____cacheline_aligned_in_smp;
81
82struct mem_cgroup_stat {
83 struct mem_cgroup_stat_cpu cpustat[0];
84}; 96};
85 97
86static inline void
87__mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat,
88 enum mem_cgroup_stat_index idx)
89{
90 stat->count[idx] = 0;
91}
92
93static inline s64
94__mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat,
95 enum mem_cgroup_stat_index idx)
96{
97 return stat->count[idx];
98}
99
100/*
101 * For accounting under irq disable, no need for increment preempt count.
102 */
103static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
104 enum mem_cgroup_stat_index idx, int val)
105{
106 stat->count[idx] += val;
107}
108
109static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
110 enum mem_cgroup_stat_index idx)
111{
112 int cpu;
113 s64 ret = 0;
114 for_each_possible_cpu(cpu)
115 ret += stat->cpustat[cpu].count[idx];
116 return ret;
117}
118
119static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
120{
121 s64 ret;
122
123 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
124 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
125 return ret;
126}
127
128/* 98/*
129 * per-zone information in memory controller. 99 * per-zone information in memory controller.
130 */ 100 */
@@ -174,6 +144,22 @@ struct mem_cgroup_tree {
174 144
175static struct mem_cgroup_tree soft_limit_tree __read_mostly; 145static struct mem_cgroup_tree soft_limit_tree __read_mostly;
176 146
147struct mem_cgroup_threshold {
148 struct eventfd_ctx *eventfd;
149 u64 threshold;
150};
151
152struct mem_cgroup_threshold_ary {
153 /* An array index points to threshold just below usage. */
154 atomic_t current_threshold;
155 /* Size of entries[] */
156 unsigned int size;
157 /* Array of thresholds */
158 struct mem_cgroup_threshold entries[0];
159};
160
161static void mem_cgroup_threshold(struct mem_cgroup *mem);
162
177/* 163/*
178 * The memory controller data structure. The memory controller controls both 164 * The memory controller data structure. The memory controller controls both
179 * page cache and RSS per cgroup. We would eventually like to provide 165 * page cache and RSS per cgroup. We would eventually like to provide
@@ -217,7 +203,7 @@ struct mem_cgroup {
217 * Should the accounting and control be hierarchical, per subtree? 203 * Should the accounting and control be hierarchical, per subtree?
218 */ 204 */
219 bool use_hierarchy; 205 bool use_hierarchy;
220 unsigned long last_oom_jiffies; 206 atomic_t oom_lock;
221 atomic_t refcnt; 207 atomic_t refcnt;
222 208
223 unsigned int swappiness; 209 unsigned int swappiness;
@@ -225,10 +211,48 @@ struct mem_cgroup {
225 /* set when res.limit == memsw.limit */ 211 /* set when res.limit == memsw.limit */
226 bool memsw_is_minimum; 212 bool memsw_is_minimum;
227 213
214 /* protect arrays of thresholds */
215 struct mutex thresholds_lock;
216
217 /* thresholds for memory usage. RCU-protected */
218 struct mem_cgroup_threshold_ary *thresholds;
219
220 /* thresholds for mem+swap usage. RCU-protected */
221 struct mem_cgroup_threshold_ary *memsw_thresholds;
222
223 /*
224 * Should we move charges of a task when a task is moved into this
225 * mem_cgroup ? And what type of charges should we move ?
226 */
227 unsigned long move_charge_at_immigrate;
228
228 /* 229 /*
229 * statistics. This must be placed at the end of memcg. 230 * percpu counter.
230 */ 231 */
231 struct mem_cgroup_stat stat; 232 struct mem_cgroup_stat_cpu *stat;
233};
234
235/* Stuffs for move charges at task migration. */
236/*
237 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
238 * left-shifted bitmap of these types.
239 */
240enum move_type {
241 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
242 NR_MOVE_TYPE,
243};
244
245/* "mc" and its members are protected by cgroup_mutex */
246static struct move_charge_struct {
247 struct mem_cgroup *from;
248 struct mem_cgroup *to;
249 unsigned long precharge;
250 unsigned long moved_charge;
251 unsigned long moved_swap;
252 struct task_struct *moving_task; /* a task moving charges */
253 wait_queue_head_t waitq; /* a waitq for other context */
254} mc = {
255 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
232}; 256};
233 257
234/* 258/*
@@ -371,23 +395,6 @@ mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
371 spin_unlock(&mctz->lock); 395 spin_unlock(&mctz->lock);
372} 396}
373 397
374static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
375{
376 bool ret = false;
377 int cpu;
378 s64 val;
379 struct mem_cgroup_stat_cpu *cpustat;
380
381 cpu = get_cpu();
382 cpustat = &mem->stat.cpustat[cpu];
383 val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
384 if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
385 __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
386 ret = true;
387 }
388 put_cpu();
389 return ret;
390}
391 398
392static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page) 399static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
393{ 400{
@@ -481,17 +488,31 @@ mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
481 return mz; 488 return mz;
482} 489}
483 490
491static s64 mem_cgroup_read_stat(struct mem_cgroup *mem,
492 enum mem_cgroup_stat_index idx)
493{
494 int cpu;
495 s64 val = 0;
496
497 for_each_possible_cpu(cpu)
498 val += per_cpu(mem->stat->count[idx], cpu);
499 return val;
500}
501
502static s64 mem_cgroup_local_usage(struct mem_cgroup *mem)
503{
504 s64 ret;
505
506 ret = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
507 ret += mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
508 return ret;
509}
510
484static void mem_cgroup_swap_statistics(struct mem_cgroup *mem, 511static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
485 bool charge) 512 bool charge)
486{ 513{
487 int val = (charge) ? 1 : -1; 514 int val = (charge) ? 1 : -1;
488 struct mem_cgroup_stat *stat = &mem->stat; 515 this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
489 struct mem_cgroup_stat_cpu *cpustat;
490 int cpu = get_cpu();
491
492 cpustat = &stat->cpustat[cpu];
493 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val);
494 put_cpu();
495} 516}
496 517
497static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, 518static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
@@ -499,24 +520,21 @@ static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
499 bool charge) 520 bool charge)
500{ 521{
501 int val = (charge) ? 1 : -1; 522 int val = (charge) ? 1 : -1;
502 struct mem_cgroup_stat *stat = &mem->stat;
503 struct mem_cgroup_stat_cpu *cpustat;
504 int cpu = get_cpu();
505 523
506 cpustat = &stat->cpustat[cpu]; 524 preempt_disable();
525
507 if (PageCgroupCache(pc)) 526 if (PageCgroupCache(pc))
508 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val); 527 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], val);
509 else 528 else
510 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val); 529 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], val);
511 530
512 if (charge) 531 if (charge)
513 __mem_cgroup_stat_add_safe(cpustat, 532 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGIN_COUNT]);
514 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
515 else 533 else
516 __mem_cgroup_stat_add_safe(cpustat, 534 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGOUT_COUNT]);
517 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1); 535 __this_cpu_inc(mem->stat->count[MEM_CGROUP_EVENTS]);
518 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1); 536
519 put_cpu(); 537 preempt_enable();
520} 538}
521 539
522static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem, 540static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
@@ -534,6 +552,29 @@ static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
534 return total; 552 return total;
535} 553}
536 554
555static bool __memcg_event_check(struct mem_cgroup *mem, int event_mask_shift)
556{
557 s64 val;
558
559 val = this_cpu_read(mem->stat->count[MEM_CGROUP_EVENTS]);
560
561 return !(val & ((1 << event_mask_shift) - 1));
562}
563
564/*
565 * Check events in order.
566 *
567 */
568static void memcg_check_events(struct mem_cgroup *mem, struct page *page)
569{
570 /* threshold event is triggered in finer grain than soft limit */
571 if (unlikely(__memcg_event_check(mem, THRESHOLDS_EVENTS_THRESH))) {
572 mem_cgroup_threshold(mem);
573 if (unlikely(__memcg_event_check(mem, SOFTLIMIT_EVENTS_THRESH)))
574 mem_cgroup_update_tree(mem, page);
575 }
576}
577
537static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) 578static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
538{ 579{
539 return container_of(cgroup_subsys_state(cont, 580 return container_of(cgroup_subsys_state(cont,
@@ -770,10 +811,12 @@ int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
770 * enabled in "curr" and "curr" is a child of "mem" in *cgroup* 811 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
771 * hierarchy(even if use_hierarchy is disabled in "mem"). 812 * hierarchy(even if use_hierarchy is disabled in "mem").
772 */ 813 */
814 rcu_read_lock();
773 if (mem->use_hierarchy) 815 if (mem->use_hierarchy)
774 ret = css_is_ancestor(&curr->css, &mem->css); 816 ret = css_is_ancestor(&curr->css, &mem->css);
775 else 817 else
776 ret = (curr == mem); 818 ret = (curr == mem);
819 rcu_read_unlock();
777 css_put(&curr->css); 820 css_put(&curr->css);
778 return ret; 821 return ret;
779} 822}
@@ -1000,7 +1043,7 @@ static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
1000} 1043}
1001 1044
1002/** 1045/**
1003 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode. 1046 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
1004 * @memcg: The memory cgroup that went over limit 1047 * @memcg: The memory cgroup that went over limit
1005 * @p: Task that is going to be killed 1048 * @p: Task that is going to be killed
1006 * 1049 *
@@ -1174,7 +1217,7 @@ static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
1174 } 1217 }
1175 } 1218 }
1176 } 1219 }
1177 if (!mem_cgroup_local_usage(&victim->stat)) { 1220 if (!mem_cgroup_local_usage(victim)) {
1178 /* this cgroup's local usage == 0 */ 1221 /* this cgroup's local usage == 0 */
1179 css_put(&victim->css); 1222 css_put(&victim->css);
1180 continue; 1223 continue;
@@ -1205,32 +1248,102 @@ static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
1205 return total; 1248 return total;
1206} 1249}
1207 1250
1208bool mem_cgroup_oom_called(struct task_struct *task) 1251static int mem_cgroup_oom_lock_cb(struct mem_cgroup *mem, void *data)
1209{ 1252{
1210 bool ret = false; 1253 int *val = (int *)data;
1211 struct mem_cgroup *mem; 1254 int x;
1212 struct mm_struct *mm; 1255 /*
1256 * Logically, we can stop scanning immediately when we find
1257 * a memcg is already locked. But condidering unlock ops and
1258 * creation/removal of memcg, scan-all is simple operation.
1259 */
1260 x = atomic_inc_return(&mem->oom_lock);
1261 *val = max(x, *val);
1262 return 0;
1263}
1264/*
1265 * Check OOM-Killer is already running under our hierarchy.
1266 * If someone is running, return false.
1267 */
1268static bool mem_cgroup_oom_lock(struct mem_cgroup *mem)
1269{
1270 int lock_count = 0;
1213 1271
1214 rcu_read_lock(); 1272 mem_cgroup_walk_tree(mem, &lock_count, mem_cgroup_oom_lock_cb);
1215 mm = task->mm; 1273
1216 if (!mm) 1274 if (lock_count == 1)
1217 mm = &init_mm; 1275 return true;
1218 mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); 1276 return false;
1219 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
1220 ret = true;
1221 rcu_read_unlock();
1222 return ret;
1223} 1277}
1224 1278
1225static int record_last_oom_cb(struct mem_cgroup *mem, void *data) 1279static int mem_cgroup_oom_unlock_cb(struct mem_cgroup *mem, void *data)
1226{ 1280{
1227 mem->last_oom_jiffies = jiffies; 1281 /*
1282 * When a new child is created while the hierarchy is under oom,
1283 * mem_cgroup_oom_lock() may not be called. We have to use
1284 * atomic_add_unless() here.
1285 */
1286 atomic_add_unless(&mem->oom_lock, -1, 0);
1228 return 0; 1287 return 0;
1229} 1288}
1230 1289
1231static void record_last_oom(struct mem_cgroup *mem) 1290static void mem_cgroup_oom_unlock(struct mem_cgroup *mem)
1291{
1292 mem_cgroup_walk_tree(mem, NULL, mem_cgroup_oom_unlock_cb);
1293}
1294
1295static DEFINE_MUTEX(memcg_oom_mutex);
1296static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1297
1298/*
1299 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1300 */
1301bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask)
1232{ 1302{
1233 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb); 1303 DEFINE_WAIT(wait);
1304 bool locked;
1305
1306 /* At first, try to OOM lock hierarchy under mem.*/
1307 mutex_lock(&memcg_oom_mutex);
1308 locked = mem_cgroup_oom_lock(mem);
1309 /*
1310 * Even if signal_pending(), we can't quit charge() loop without
1311 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1312 * under OOM is always welcomed, use TASK_KILLABLE here.
1313 */
1314 if (!locked)
1315 prepare_to_wait(&memcg_oom_waitq, &wait, TASK_KILLABLE);
1316 mutex_unlock(&memcg_oom_mutex);
1317
1318 if (locked)
1319 mem_cgroup_out_of_memory(mem, mask);
1320 else {
1321 schedule();
1322 finish_wait(&memcg_oom_waitq, &wait);
1323 }
1324 mutex_lock(&memcg_oom_mutex);
1325 mem_cgroup_oom_unlock(mem);
1326 /*
1327 * Here, we use global waitq .....more fine grained waitq ?
1328 * Assume following hierarchy.
1329 * A/
1330 * 01
1331 * 02
1332 * assume OOM happens both in A and 01 at the same time. Tthey are
1333 * mutually exclusive by lock. (kill in 01 helps A.)
1334 * When we use per memcg waitq, we have to wake up waiters on A and 02
1335 * in addtion to waiters on 01. We use global waitq for avoiding mess.
1336 * It will not be a big problem.
1337 * (And a task may be moved to other groups while it's waiting for OOM.)
1338 */
1339 wake_up_all(&memcg_oom_waitq);
1340 mutex_unlock(&memcg_oom_mutex);
1341
1342 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1343 return false;
1344 /* Give chance to dying process */
1345 schedule_timeout(1);
1346 return true;
1234} 1347}
1235 1348
1236/* 1349/*
@@ -1240,9 +1353,6 @@ static void record_last_oom(struct mem_cgroup *mem)
1240void mem_cgroup_update_file_mapped(struct page *page, int val) 1353void mem_cgroup_update_file_mapped(struct page *page, int val)
1241{ 1354{
1242 struct mem_cgroup *mem; 1355 struct mem_cgroup *mem;
1243 struct mem_cgroup_stat *stat;
1244 struct mem_cgroup_stat_cpu *cpustat;
1245 int cpu;
1246 struct page_cgroup *pc; 1356 struct page_cgroup *pc;
1247 1357
1248 pc = lookup_page_cgroup(page); 1358 pc = lookup_page_cgroup(page);
@@ -1251,20 +1361,20 @@ void mem_cgroup_update_file_mapped(struct page *page, int val)
1251 1361
1252 lock_page_cgroup(pc); 1362 lock_page_cgroup(pc);
1253 mem = pc->mem_cgroup; 1363 mem = pc->mem_cgroup;
1254 if (!mem) 1364 if (!mem || !PageCgroupUsed(pc))
1255 goto done;
1256
1257 if (!PageCgroupUsed(pc))
1258 goto done; 1365 goto done;
1259 1366
1260 /* 1367 /*
1261 * Preemption is already disabled, we don't need get_cpu() 1368 * Preemption is already disabled. We can use __this_cpu_xxx
1262 */ 1369 */
1263 cpu = smp_processor_id(); 1370 if (val > 0) {
1264 stat = &mem->stat; 1371 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1265 cpustat = &stat->cpustat[cpu]; 1372 SetPageCgroupFileMapped(pc);
1373 } else {
1374 __this_cpu_dec(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1375 ClearPageCgroupFileMapped(pc);
1376 }
1266 1377
1267 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED, val);
1268done: 1378done:
1269 unlock_page_cgroup(pc); 1379 unlock_page_cgroup(pc);
1270} 1380}
@@ -1401,19 +1511,21 @@ static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb,
1401 * oom-killer can be invoked. 1511 * oom-killer can be invoked.
1402 */ 1512 */
1403static int __mem_cgroup_try_charge(struct mm_struct *mm, 1513static int __mem_cgroup_try_charge(struct mm_struct *mm,
1404 gfp_t gfp_mask, struct mem_cgroup **memcg, 1514 gfp_t gfp_mask, struct mem_cgroup **memcg, bool oom)
1405 bool oom, struct page *page)
1406{ 1515{
1407 struct mem_cgroup *mem, *mem_over_limit; 1516 struct mem_cgroup *mem, *mem_over_limit;
1408 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; 1517 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
1409 struct res_counter *fail_res; 1518 struct res_counter *fail_res;
1410 int csize = CHARGE_SIZE; 1519 int csize = CHARGE_SIZE;
1411 1520
1412 if (unlikely(test_thread_flag(TIF_MEMDIE))) { 1521 /*
1413 /* Don't account this! */ 1522 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
1414 *memcg = NULL; 1523 * in system level. So, allow to go ahead dying process in addition to
1415 return 0; 1524 * MEMDIE process.
1416 } 1525 */
1526 if (unlikely(test_thread_flag(TIF_MEMDIE)
1527 || fatal_signal_pending(current)))
1528 goto bypass;
1417 1529
1418 /* 1530 /*
1419 * We always charge the cgroup the mm_struct belongs to. 1531 * We always charge the cgroup the mm_struct belongs to.
@@ -1440,7 +1552,7 @@ static int __mem_cgroup_try_charge(struct mm_struct *mm,
1440 unsigned long flags = 0; 1552 unsigned long flags = 0;
1441 1553
1442 if (consume_stock(mem)) 1554 if (consume_stock(mem))
1443 goto charged; 1555 goto done;
1444 1556
1445 ret = res_counter_charge(&mem->res, csize, &fail_res); 1557 ret = res_counter_charge(&mem->res, csize, &fail_res);
1446 if (likely(!ret)) { 1558 if (likely(!ret)) {
@@ -1483,28 +1595,70 @@ static int __mem_cgroup_try_charge(struct mm_struct *mm,
1483 if (mem_cgroup_check_under_limit(mem_over_limit)) 1595 if (mem_cgroup_check_under_limit(mem_over_limit))
1484 continue; 1596 continue;
1485 1597
1598 /* try to avoid oom while someone is moving charge */
1599 if (mc.moving_task && current != mc.moving_task) {
1600 struct mem_cgroup *from, *to;
1601 bool do_continue = false;
1602 /*
1603 * There is a small race that "from" or "to" can be
1604 * freed by rmdir, so we use css_tryget().
1605 */
1606 rcu_read_lock();
1607 from = mc.from;
1608 to = mc.to;
1609 if (from && css_tryget(&from->css)) {
1610 if (mem_over_limit->use_hierarchy)
1611 do_continue = css_is_ancestor(
1612 &from->css,
1613 &mem_over_limit->css);
1614 else
1615 do_continue = (from == mem_over_limit);
1616 css_put(&from->css);
1617 }
1618 if (!do_continue && to && css_tryget(&to->css)) {
1619 if (mem_over_limit->use_hierarchy)
1620 do_continue = css_is_ancestor(
1621 &to->css,
1622 &mem_over_limit->css);
1623 else
1624 do_continue = (to == mem_over_limit);
1625 css_put(&to->css);
1626 }
1627 rcu_read_unlock();
1628 if (do_continue) {
1629 DEFINE_WAIT(wait);
1630 prepare_to_wait(&mc.waitq, &wait,
1631 TASK_INTERRUPTIBLE);
1632 /* moving charge context might have finished. */
1633 if (mc.moving_task)
1634 schedule();
1635 finish_wait(&mc.waitq, &wait);
1636 continue;
1637 }
1638 }
1639
1486 if (!nr_retries--) { 1640 if (!nr_retries--) {
1487 if (oom) { 1641 if (!oom)
1488 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask); 1642 goto nomem;
1489 record_last_oom(mem_over_limit); 1643 if (mem_cgroup_handle_oom(mem_over_limit, gfp_mask)) {
1644 nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
1645 continue;
1490 } 1646 }
1491 goto nomem; 1647 /* When we reach here, current task is dying .*/
1648 css_put(&mem->css);
1649 goto bypass;
1492 } 1650 }
1493 } 1651 }
1494 if (csize > PAGE_SIZE) 1652 if (csize > PAGE_SIZE)
1495 refill_stock(mem, csize - PAGE_SIZE); 1653 refill_stock(mem, csize - PAGE_SIZE);
1496charged:
1497 /*
1498 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1499 * if they exceeds softlimit.
1500 */
1501 if (mem_cgroup_soft_limit_check(mem))
1502 mem_cgroup_update_tree(mem, page);
1503done: 1654done:
1504 return 0; 1655 return 0;
1505nomem: 1656nomem:
1506 css_put(&mem->css); 1657 css_put(&mem->css);
1507 return -ENOMEM; 1658 return -ENOMEM;
1659bypass:
1660 *memcg = NULL;
1661 return 0;
1508} 1662}
1509 1663
1510/* 1664/*
@@ -1512,14 +1666,23 @@ nomem:
1512 * This function is for that and do uncharge, put css's refcnt. 1666 * This function is for that and do uncharge, put css's refcnt.
1513 * gotten by try_charge(). 1667 * gotten by try_charge().
1514 */ 1668 */
1515static void mem_cgroup_cancel_charge(struct mem_cgroup *mem) 1669static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem,
1670 unsigned long count)
1516{ 1671{
1517 if (!mem_cgroup_is_root(mem)) { 1672 if (!mem_cgroup_is_root(mem)) {
1518 res_counter_uncharge(&mem->res, PAGE_SIZE); 1673 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
1519 if (do_swap_account) 1674 if (do_swap_account)
1520 res_counter_uncharge(&mem->memsw, PAGE_SIZE); 1675 res_counter_uncharge(&mem->memsw, PAGE_SIZE * count);
1676 VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags));
1677 WARN_ON_ONCE(count > INT_MAX);
1678 __css_put(&mem->css, (int)count);
1521 } 1679 }
1522 css_put(&mem->css); 1680 /* we don't need css_put for root */
1681}
1682
1683static void mem_cgroup_cancel_charge(struct mem_cgroup *mem)
1684{
1685 __mem_cgroup_cancel_charge(mem, 1);
1523} 1686}
1524 1687
1525/* 1688/*
@@ -1615,6 +1778,12 @@ static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1615 mem_cgroup_charge_statistics(mem, pc, true); 1778 mem_cgroup_charge_statistics(mem, pc, true);
1616 1779
1617 unlock_page_cgroup(pc); 1780 unlock_page_cgroup(pc);
1781 /*
1782 * "charge_statistics" updated event counter. Then, check it.
1783 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1784 * if they exceeds softlimit.
1785 */
1786 memcg_check_events(mem, pc->page);
1618} 1787}
1619 1788
1620/** 1789/**
@@ -1622,61 +1791,48 @@ static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1622 * @pc: page_cgroup of the page. 1791 * @pc: page_cgroup of the page.
1623 * @from: mem_cgroup which the page is moved from. 1792 * @from: mem_cgroup which the page is moved from.
1624 * @to: mem_cgroup which the page is moved to. @from != @to. 1793 * @to: mem_cgroup which the page is moved to. @from != @to.
1794 * @uncharge: whether we should call uncharge and css_put against @from.
1625 * 1795 *
1626 * The caller must confirm following. 1796 * The caller must confirm following.
1627 * - page is not on LRU (isolate_page() is useful.) 1797 * - page is not on LRU (isolate_page() is useful.)
1628 * - the pc is locked, used, and ->mem_cgroup points to @from. 1798 * - the pc is locked, used, and ->mem_cgroup points to @from.
1629 * 1799 *
1630 * This function does "uncharge" from old cgroup but doesn't do "charge" to 1800 * This function doesn't do "charge" nor css_get to new cgroup. It should be
1631 * new cgroup. It should be done by a caller. 1801 * done by a caller(__mem_cgroup_try_charge would be usefull). If @uncharge is
1802 * true, this function does "uncharge" from old cgroup, but it doesn't if
1803 * @uncharge is false, so a caller should do "uncharge".
1632 */ 1804 */
1633 1805
1634static void __mem_cgroup_move_account(struct page_cgroup *pc, 1806static void __mem_cgroup_move_account(struct page_cgroup *pc,
1635 struct mem_cgroup *from, struct mem_cgroup *to) 1807 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
1636{ 1808{
1637 struct page *page;
1638 int cpu;
1639 struct mem_cgroup_stat *stat;
1640 struct mem_cgroup_stat_cpu *cpustat;
1641
1642 VM_BUG_ON(from == to); 1809 VM_BUG_ON(from == to);
1643 VM_BUG_ON(PageLRU(pc->page)); 1810 VM_BUG_ON(PageLRU(pc->page));
1644 VM_BUG_ON(!PageCgroupLocked(pc)); 1811 VM_BUG_ON(!PageCgroupLocked(pc));
1645 VM_BUG_ON(!PageCgroupUsed(pc)); 1812 VM_BUG_ON(!PageCgroupUsed(pc));
1646 VM_BUG_ON(pc->mem_cgroup != from); 1813 VM_BUG_ON(pc->mem_cgroup != from);
1647 1814
1648 if (!mem_cgroup_is_root(from)) 1815 if (PageCgroupFileMapped(pc)) {
1649 res_counter_uncharge(&from->res, PAGE_SIZE); 1816 /* Update mapped_file data for mem_cgroup */
1650 mem_cgroup_charge_statistics(from, pc, false); 1817 preempt_disable();
1651 1818 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1652 page = pc->page; 1819 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1653 if (page_mapped(page) && !PageAnon(page)) { 1820 preempt_enable();
1654 cpu = smp_processor_id();
1655 /* Update mapped_file data for mem_cgroup "from" */
1656 stat = &from->stat;
1657 cpustat = &stat->cpustat[cpu];
1658 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
1659 -1);
1660
1661 /* Update mapped_file data for mem_cgroup "to" */
1662 stat = &to->stat;
1663 cpustat = &stat->cpustat[cpu];
1664 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
1665 1);
1666 } 1821 }
1822 mem_cgroup_charge_statistics(from, pc, false);
1823 if (uncharge)
1824 /* This is not "cancel", but cancel_charge does all we need. */
1825 mem_cgroup_cancel_charge(from);
1667 1826
1668 if (do_swap_account && !mem_cgroup_is_root(from)) 1827 /* caller should have done css_get */
1669 res_counter_uncharge(&from->memsw, PAGE_SIZE);
1670 css_put(&from->css);
1671
1672 css_get(&to->css);
1673 pc->mem_cgroup = to; 1828 pc->mem_cgroup = to;
1674 mem_cgroup_charge_statistics(to, pc, true); 1829 mem_cgroup_charge_statistics(to, pc, true);
1675 /* 1830 /*
1676 * We charges against "to" which may not have any tasks. Then, "to" 1831 * We charges against "to" which may not have any tasks. Then, "to"
1677 * can be under rmdir(). But in current implementation, caller of 1832 * can be under rmdir(). But in current implementation, caller of
1678 * this function is just force_empty() and it's garanteed that 1833 * this function is just force_empty() and move charge, so it's
1679 * "to" is never removed. So, we don't check rmdir status here. 1834 * garanteed that "to" is never removed. So, we don't check rmdir
1835 * status here.
1680 */ 1836 */
1681} 1837}
1682 1838
@@ -1685,15 +1841,20 @@ static void __mem_cgroup_move_account(struct page_cgroup *pc,
1685 * __mem_cgroup_move_account() 1841 * __mem_cgroup_move_account()
1686 */ 1842 */
1687static int mem_cgroup_move_account(struct page_cgroup *pc, 1843static int mem_cgroup_move_account(struct page_cgroup *pc,
1688 struct mem_cgroup *from, struct mem_cgroup *to) 1844 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
1689{ 1845{
1690 int ret = -EINVAL; 1846 int ret = -EINVAL;
1691 lock_page_cgroup(pc); 1847 lock_page_cgroup(pc);
1692 if (PageCgroupUsed(pc) && pc->mem_cgroup == from) { 1848 if (PageCgroupUsed(pc) && pc->mem_cgroup == from) {
1693 __mem_cgroup_move_account(pc, from, to); 1849 __mem_cgroup_move_account(pc, from, to, uncharge);
1694 ret = 0; 1850 ret = 0;
1695 } 1851 }
1696 unlock_page_cgroup(pc); 1852 unlock_page_cgroup(pc);
1853 /*
1854 * check events
1855 */
1856 memcg_check_events(to, pc->page);
1857 memcg_check_events(from, pc->page);
1697 return ret; 1858 return ret;
1698} 1859}
1699 1860
@@ -1722,15 +1883,13 @@ static int mem_cgroup_move_parent(struct page_cgroup *pc,
1722 goto put; 1883 goto put;
1723 1884
1724 parent = mem_cgroup_from_cont(pcg); 1885 parent = mem_cgroup_from_cont(pcg);
1725 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page); 1886 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
1726 if (ret || !parent) 1887 if (ret || !parent)
1727 goto put_back; 1888 goto put_back;
1728 1889
1729 ret = mem_cgroup_move_account(pc, child, parent); 1890 ret = mem_cgroup_move_account(pc, child, parent, true);
1730 if (!ret) 1891 if (ret)
1731 css_put(&parent->css); /* drop extra refcnt by try_charge() */ 1892 mem_cgroup_cancel_charge(parent);
1732 else
1733 mem_cgroup_cancel_charge(parent); /* does css_put */
1734put_back: 1893put_back:
1735 putback_lru_page(page); 1894 putback_lru_page(page);
1736put: 1895put:
@@ -1760,7 +1919,7 @@ static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1760 prefetchw(pc); 1919 prefetchw(pc);
1761 1920
1762 mem = memcg; 1921 mem = memcg;
1763 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page); 1922 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
1764 if (ret || !mem) 1923 if (ret || !mem)
1765 return ret; 1924 return ret;
1766 1925
@@ -1880,14 +2039,14 @@ int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1880 if (!mem) 2039 if (!mem)
1881 goto charge_cur_mm; 2040 goto charge_cur_mm;
1882 *ptr = mem; 2041 *ptr = mem;
1883 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page); 2042 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
1884 /* drop extra refcnt from tryget */ 2043 /* drop extra refcnt from tryget */
1885 css_put(&mem->css); 2044 css_put(&mem->css);
1886 return ret; 2045 return ret;
1887charge_cur_mm: 2046charge_cur_mm:
1888 if (unlikely(!mm)) 2047 if (unlikely(!mm))
1889 mm = &init_mm; 2048 mm = &init_mm;
1890 return __mem_cgroup_try_charge(mm, mask, ptr, true, page); 2049 return __mem_cgroup_try_charge(mm, mask, ptr, true);
1891} 2050}
1892 2051
1893static void 2052static void
@@ -2064,8 +2223,7 @@ __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
2064 mz = page_cgroup_zoneinfo(pc); 2223 mz = page_cgroup_zoneinfo(pc);
2065 unlock_page_cgroup(pc); 2224 unlock_page_cgroup(pc);
2066 2225
2067 if (mem_cgroup_soft_limit_check(mem)) 2226 memcg_check_events(mem, page);
2068 mem_cgroup_update_tree(mem, page);
2069 /* at swapout, this memcg will be accessed to record to swap */ 2227 /* at swapout, this memcg will be accessed to record to swap */
2070 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT) 2228 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2071 css_put(&mem->css); 2229 css_put(&mem->css);
@@ -2156,7 +2314,9 @@ mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
2156 2314
2157 /* record memcg information */ 2315 /* record memcg information */
2158 if (do_swap_account && swapout && memcg) { 2316 if (do_swap_account && swapout && memcg) {
2317 rcu_read_lock();
2159 swap_cgroup_record(ent, css_id(&memcg->css)); 2318 swap_cgroup_record(ent, css_id(&memcg->css));
2319 rcu_read_unlock();
2160 mem_cgroup_get(memcg); 2320 mem_cgroup_get(memcg);
2161 } 2321 }
2162 if (swapout && memcg) 2322 if (swapout && memcg)
@@ -2192,6 +2352,66 @@ void mem_cgroup_uncharge_swap(swp_entry_t ent)
2192 } 2352 }
2193 rcu_read_unlock(); 2353 rcu_read_unlock();
2194} 2354}
2355
2356/**
2357 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
2358 * @entry: swap entry to be moved
2359 * @from: mem_cgroup which the entry is moved from
2360 * @to: mem_cgroup which the entry is moved to
2361 * @need_fixup: whether we should fixup res_counters and refcounts.
2362 *
2363 * It succeeds only when the swap_cgroup's record for this entry is the same
2364 * as the mem_cgroup's id of @from.
2365 *
2366 * Returns 0 on success, -EINVAL on failure.
2367 *
2368 * The caller must have charged to @to, IOW, called res_counter_charge() about
2369 * both res and memsw, and called css_get().
2370 */
2371static int mem_cgroup_move_swap_account(swp_entry_t entry,
2372 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
2373{
2374 unsigned short old_id, new_id;
2375
2376 rcu_read_lock();
2377 old_id = css_id(&from->css);
2378 new_id = css_id(&to->css);
2379 rcu_read_unlock();
2380
2381 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
2382 mem_cgroup_swap_statistics(from, false);
2383 mem_cgroup_swap_statistics(to, true);
2384 /*
2385 * This function is only called from task migration context now.
2386 * It postpones res_counter and refcount handling till the end
2387 * of task migration(mem_cgroup_clear_mc()) for performance
2388 * improvement. But we cannot postpone mem_cgroup_get(to)
2389 * because if the process that has been moved to @to does
2390 * swap-in, the refcount of @to might be decreased to 0.
2391 */
2392 mem_cgroup_get(to);
2393 if (need_fixup) {
2394 if (!mem_cgroup_is_root(from))
2395 res_counter_uncharge(&from->memsw, PAGE_SIZE);
2396 mem_cgroup_put(from);
2397 /*
2398 * we charged both to->res and to->memsw, so we should
2399 * uncharge to->res.
2400 */
2401 if (!mem_cgroup_is_root(to))
2402 res_counter_uncharge(&to->res, PAGE_SIZE);
2403 css_put(&to->css);
2404 }
2405 return 0;
2406 }
2407 return -EINVAL;
2408}
2409#else
2410static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
2411 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
2412{
2413 return -EINVAL;
2414}
2195#endif 2415#endif
2196 2416
2197/* 2417/*
@@ -2215,12 +2435,11 @@ int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
2215 } 2435 }
2216 unlock_page_cgroup(pc); 2436 unlock_page_cgroup(pc);
2217 2437
2438 *ptr = mem;
2218 if (mem) { 2439 if (mem) {
2219 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false, 2440 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, ptr, false);
2220 page);
2221 css_put(&mem->css); 2441 css_put(&mem->css);
2222 } 2442 }
2223 *ptr = mem;
2224 return ret; 2443 return ret;
2225} 2444}
2226 2445
@@ -2545,7 +2764,7 @@ static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
2545 pc = list_entry(list->prev, struct page_cgroup, lru); 2764 pc = list_entry(list->prev, struct page_cgroup, lru);
2546 if (busy == pc) { 2765 if (busy == pc) {
2547 list_move(&pc->lru, list); 2766 list_move(&pc->lru, list);
2548 busy = 0; 2767 busy = NULL;
2549 spin_unlock_irqrestore(&zone->lru_lock, flags); 2768 spin_unlock_irqrestore(&zone->lru_lock, flags);
2550 continue; 2769 continue;
2551 } 2770 }
@@ -2704,7 +2923,7 @@ static int
2704mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data) 2923mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
2705{ 2924{
2706 struct mem_cgroup_idx_data *d = data; 2925 struct mem_cgroup_idx_data *d = data;
2707 d->val += mem_cgroup_read_stat(&mem->stat, d->idx); 2926 d->val += mem_cgroup_read_stat(mem, d->idx);
2708 return 0; 2927 return 0;
2709} 2928}
2710 2929
@@ -2719,40 +2938,50 @@ mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
2719 *val = d.val; 2938 *val = d.val;
2720} 2939}
2721 2940
2941static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
2942{
2943 u64 idx_val, val;
2944
2945 if (!mem_cgroup_is_root(mem)) {
2946 if (!swap)
2947 return res_counter_read_u64(&mem->res, RES_USAGE);
2948 else
2949 return res_counter_read_u64(&mem->memsw, RES_USAGE);
2950 }
2951
2952 mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_CACHE, &idx_val);
2953 val = idx_val;
2954 mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_RSS, &idx_val);
2955 val += idx_val;
2956
2957 if (swap) {
2958 mem_cgroup_get_recursive_idx_stat(mem,
2959 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
2960 val += idx_val;
2961 }
2962
2963 return val << PAGE_SHIFT;
2964}
2965
2722static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) 2966static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
2723{ 2967{
2724 struct mem_cgroup *mem = mem_cgroup_from_cont(cont); 2968 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2725 u64 idx_val, val; 2969 u64 val;
2726 int type, name; 2970 int type, name;
2727 2971
2728 type = MEMFILE_TYPE(cft->private); 2972 type = MEMFILE_TYPE(cft->private);
2729 name = MEMFILE_ATTR(cft->private); 2973 name = MEMFILE_ATTR(cft->private);
2730 switch (type) { 2974 switch (type) {
2731 case _MEM: 2975 case _MEM:
2732 if (name == RES_USAGE && mem_cgroup_is_root(mem)) { 2976 if (name == RES_USAGE)
2733 mem_cgroup_get_recursive_idx_stat(mem, 2977 val = mem_cgroup_usage(mem, false);
2734 MEM_CGROUP_STAT_CACHE, &idx_val); 2978 else
2735 val = idx_val;
2736 mem_cgroup_get_recursive_idx_stat(mem,
2737 MEM_CGROUP_STAT_RSS, &idx_val);
2738 val += idx_val;
2739 val <<= PAGE_SHIFT;
2740 } else
2741 val = res_counter_read_u64(&mem->res, name); 2979 val = res_counter_read_u64(&mem->res, name);
2742 break; 2980 break;
2743 case _MEMSWAP: 2981 case _MEMSWAP:
2744 if (name == RES_USAGE && mem_cgroup_is_root(mem)) { 2982 if (name == RES_USAGE)
2745 mem_cgroup_get_recursive_idx_stat(mem, 2983 val = mem_cgroup_usage(mem, true);
2746 MEM_CGROUP_STAT_CACHE, &idx_val); 2984 else
2747 val = idx_val;
2748 mem_cgroup_get_recursive_idx_stat(mem,
2749 MEM_CGROUP_STAT_RSS, &idx_val);
2750 val += idx_val;
2751 mem_cgroup_get_recursive_idx_stat(mem,
2752 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
2753 val += idx_val;
2754 val <<= PAGE_SHIFT;
2755 } else
2756 val = res_counter_read_u64(&mem->memsw, name); 2985 val = res_counter_read_u64(&mem->memsw, name);
2757 break; 2986 break;
2758 default: 2987 default:
@@ -2865,6 +3094,39 @@ static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
2865 return 0; 3094 return 0;
2866} 3095}
2867 3096
3097static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
3098 struct cftype *cft)
3099{
3100 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
3101}
3102
3103#ifdef CONFIG_MMU
3104static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3105 struct cftype *cft, u64 val)
3106{
3107 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3108
3109 if (val >= (1 << NR_MOVE_TYPE))
3110 return -EINVAL;
3111 /*
3112 * We check this value several times in both in can_attach() and
3113 * attach(), so we need cgroup lock to prevent this value from being
3114 * inconsistent.
3115 */
3116 cgroup_lock();
3117 mem->move_charge_at_immigrate = val;
3118 cgroup_unlock();
3119
3120 return 0;
3121}
3122#else
3123static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3124 struct cftype *cft, u64 val)
3125{
3126 return -ENOSYS;
3127}
3128#endif
3129
2868 3130
2869/* For read statistics */ 3131/* For read statistics */
2870enum { 3132enum {
@@ -2910,18 +3172,18 @@ static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2910 s64 val; 3172 s64 val;
2911 3173
2912 /* per cpu stat */ 3174 /* per cpu stat */
2913 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE); 3175 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
2914 s->stat[MCS_CACHE] += val * PAGE_SIZE; 3176 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2915 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS); 3177 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
2916 s->stat[MCS_RSS] += val * PAGE_SIZE; 3178 s->stat[MCS_RSS] += val * PAGE_SIZE;
2917 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_FILE_MAPPED); 3179 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED);
2918 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE; 3180 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
2919 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT); 3181 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGIN_COUNT);
2920 s->stat[MCS_PGPGIN] += val; 3182 s->stat[MCS_PGPGIN] += val;
2921 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT); 3183 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2922 s->stat[MCS_PGPGOUT] += val; 3184 s->stat[MCS_PGPGOUT] += val;
2923 if (do_swap_account) { 3185 if (do_swap_account) {
2924 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT); 3186 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
2925 s->stat[MCS_SWAP] += val * PAGE_SIZE; 3187 s->stat[MCS_SWAP] += val * PAGE_SIZE;
2926 } 3188 }
2927 3189
@@ -3049,12 +3311,249 @@ static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
3049 return 0; 3311 return 0;
3050} 3312}
3051 3313
3314static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
3315{
3316 struct mem_cgroup_threshold_ary *t;
3317 u64 usage;
3318 int i;
3319
3320 rcu_read_lock();
3321 if (!swap)
3322 t = rcu_dereference(memcg->thresholds);
3323 else
3324 t = rcu_dereference(memcg->memsw_thresholds);
3325
3326 if (!t)
3327 goto unlock;
3328
3329 usage = mem_cgroup_usage(memcg, swap);
3330
3331 /*
3332 * current_threshold points to threshold just below usage.
3333 * If it's not true, a threshold was crossed after last
3334 * call of __mem_cgroup_threshold().
3335 */
3336 i = atomic_read(&t->current_threshold);
3337
3338 /*
3339 * Iterate backward over array of thresholds starting from
3340 * current_threshold and check if a threshold is crossed.
3341 * If none of thresholds below usage is crossed, we read
3342 * only one element of the array here.
3343 */
3344 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
3345 eventfd_signal(t->entries[i].eventfd, 1);
3346
3347 /* i = current_threshold + 1 */
3348 i++;
3349
3350 /*
3351 * Iterate forward over array of thresholds starting from
3352 * current_threshold+1 and check if a threshold is crossed.
3353 * If none of thresholds above usage is crossed, we read
3354 * only one element of the array here.
3355 */
3356 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
3357 eventfd_signal(t->entries[i].eventfd, 1);
3358
3359 /* Update current_threshold */
3360 atomic_set(&t->current_threshold, i - 1);
3361unlock:
3362 rcu_read_unlock();
3363}
3364
3365static void mem_cgroup_threshold(struct mem_cgroup *memcg)
3366{
3367 __mem_cgroup_threshold(memcg, false);
3368 if (do_swap_account)
3369 __mem_cgroup_threshold(memcg, true);
3370}
3371
3372static int compare_thresholds(const void *a, const void *b)
3373{
3374 const struct mem_cgroup_threshold *_a = a;
3375 const struct mem_cgroup_threshold *_b = b;
3376
3377 return _a->threshold - _b->threshold;
3378}
3379
3380static int mem_cgroup_register_event(struct cgroup *cgrp, struct cftype *cft,
3381 struct eventfd_ctx *eventfd, const char *args)
3382{
3383 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3384 struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
3385 int type = MEMFILE_TYPE(cft->private);
3386 u64 threshold, usage;
3387 int size;
3388 int i, ret;
3389
3390 ret = res_counter_memparse_write_strategy(args, &threshold);
3391 if (ret)
3392 return ret;
3393
3394 mutex_lock(&memcg->thresholds_lock);
3395 if (type == _MEM)
3396 thresholds = memcg->thresholds;
3397 else if (type == _MEMSWAP)
3398 thresholds = memcg->memsw_thresholds;
3399 else
3400 BUG();
3401
3402 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3403
3404 /* Check if a threshold crossed before adding a new one */
3405 if (thresholds)
3406 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3407
3408 if (thresholds)
3409 size = thresholds->size + 1;
3410 else
3411 size = 1;
3412
3413 /* Allocate memory for new array of thresholds */
3414 thresholds_new = kmalloc(sizeof(*thresholds_new) +
3415 size * sizeof(struct mem_cgroup_threshold),
3416 GFP_KERNEL);
3417 if (!thresholds_new) {
3418 ret = -ENOMEM;
3419 goto unlock;
3420 }
3421 thresholds_new->size = size;
3422
3423 /* Copy thresholds (if any) to new array */
3424 if (thresholds)
3425 memcpy(thresholds_new->entries, thresholds->entries,
3426 thresholds->size *
3427 sizeof(struct mem_cgroup_threshold));
3428 /* Add new threshold */
3429 thresholds_new->entries[size - 1].eventfd = eventfd;
3430 thresholds_new->entries[size - 1].threshold = threshold;
3431
3432 /* Sort thresholds. Registering of new threshold isn't time-critical */
3433 sort(thresholds_new->entries, size,
3434 sizeof(struct mem_cgroup_threshold),
3435 compare_thresholds, NULL);
3436
3437 /* Find current threshold */
3438 atomic_set(&thresholds_new->current_threshold, -1);
3439 for (i = 0; i < size; i++) {
3440 if (thresholds_new->entries[i].threshold < usage) {
3441 /*
3442 * thresholds_new->current_threshold will not be used
3443 * until rcu_assign_pointer(), so it's safe to increment
3444 * it here.
3445 */
3446 atomic_inc(&thresholds_new->current_threshold);
3447 }
3448 }
3449
3450 if (type == _MEM)
3451 rcu_assign_pointer(memcg->thresholds, thresholds_new);
3452 else
3453 rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
3454
3455 /* To be sure that nobody uses thresholds before freeing it */
3456 synchronize_rcu();
3457
3458 kfree(thresholds);
3459unlock:
3460 mutex_unlock(&memcg->thresholds_lock);
3461
3462 return ret;
3463}
3464
3465static int mem_cgroup_unregister_event(struct cgroup *cgrp, struct cftype *cft,
3466 struct eventfd_ctx *eventfd)
3467{
3468 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3469 struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
3470 int type = MEMFILE_TYPE(cft->private);
3471 u64 usage;
3472 int size = 0;
3473 int i, j, ret;
3474
3475 mutex_lock(&memcg->thresholds_lock);
3476 if (type == _MEM)
3477 thresholds = memcg->thresholds;
3478 else if (type == _MEMSWAP)
3479 thresholds = memcg->memsw_thresholds;
3480 else
3481 BUG();
3482
3483 /*
3484 * Something went wrong if we trying to unregister a threshold
3485 * if we don't have thresholds
3486 */
3487 BUG_ON(!thresholds);
3488
3489 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3490
3491 /* Check if a threshold crossed before removing */
3492 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3493
3494 /* Calculate new number of threshold */
3495 for (i = 0; i < thresholds->size; i++) {
3496 if (thresholds->entries[i].eventfd != eventfd)
3497 size++;
3498 }
3499
3500 /* Set thresholds array to NULL if we don't have thresholds */
3501 if (!size) {
3502 thresholds_new = NULL;
3503 goto assign;
3504 }
3505
3506 /* Allocate memory for new array of thresholds */
3507 thresholds_new = kmalloc(sizeof(*thresholds_new) +
3508 size * sizeof(struct mem_cgroup_threshold),
3509 GFP_KERNEL);
3510 if (!thresholds_new) {
3511 ret = -ENOMEM;
3512 goto unlock;
3513 }
3514 thresholds_new->size = size;
3515
3516 /* Copy thresholds and find current threshold */
3517 atomic_set(&thresholds_new->current_threshold, -1);
3518 for (i = 0, j = 0; i < thresholds->size; i++) {
3519 if (thresholds->entries[i].eventfd == eventfd)
3520 continue;
3521
3522 thresholds_new->entries[j] = thresholds->entries[i];
3523 if (thresholds_new->entries[j].threshold < usage) {
3524 /*
3525 * thresholds_new->current_threshold will not be used
3526 * until rcu_assign_pointer(), so it's safe to increment
3527 * it here.
3528 */
3529 atomic_inc(&thresholds_new->current_threshold);
3530 }
3531 j++;
3532 }
3533
3534assign:
3535 if (type == _MEM)
3536 rcu_assign_pointer(memcg->thresholds, thresholds_new);
3537 else
3538 rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
3539
3540 /* To be sure that nobody uses thresholds before freeing it */
3541 synchronize_rcu();
3542
3543 kfree(thresholds);
3544unlock:
3545 mutex_unlock(&memcg->thresholds_lock);
3546
3547 return ret;
3548}
3052 3549
3053static struct cftype mem_cgroup_files[] = { 3550static struct cftype mem_cgroup_files[] = {
3054 { 3551 {
3055 .name = "usage_in_bytes", 3552 .name = "usage_in_bytes",
3056 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), 3553 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
3057 .read_u64 = mem_cgroup_read, 3554 .read_u64 = mem_cgroup_read,
3555 .register_event = mem_cgroup_register_event,
3556 .unregister_event = mem_cgroup_unregister_event,
3058 }, 3557 },
3059 { 3558 {
3060 .name = "max_usage_in_bytes", 3559 .name = "max_usage_in_bytes",
@@ -3098,6 +3597,11 @@ static struct cftype mem_cgroup_files[] = {
3098 .read_u64 = mem_cgroup_swappiness_read, 3597 .read_u64 = mem_cgroup_swappiness_read,
3099 .write_u64 = mem_cgroup_swappiness_write, 3598 .write_u64 = mem_cgroup_swappiness_write,
3100 }, 3599 },
3600 {
3601 .name = "move_charge_at_immigrate",
3602 .read_u64 = mem_cgroup_move_charge_read,
3603 .write_u64 = mem_cgroup_move_charge_write,
3604 },
3101}; 3605};
3102 3606
3103#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP 3607#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
@@ -3106,6 +3610,8 @@ static struct cftype memsw_cgroup_files[] = {
3106 .name = "memsw.usage_in_bytes", 3610 .name = "memsw.usage_in_bytes",
3107 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), 3611 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
3108 .read_u64 = mem_cgroup_read, 3612 .read_u64 = mem_cgroup_read,
3613 .register_event = mem_cgroup_register_event,
3614 .unregister_event = mem_cgroup_unregister_event,
3109 }, 3615 },
3110 { 3616 {
3111 .name = "memsw.max_usage_in_bytes", 3617 .name = "memsw.max_usage_in_bytes",
@@ -3180,24 +3686,29 @@ static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3180 kfree(mem->info.nodeinfo[node]); 3686 kfree(mem->info.nodeinfo[node]);
3181} 3687}
3182 3688
3183static int mem_cgroup_size(void)
3184{
3185 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
3186 return sizeof(struct mem_cgroup) + cpustat_size;
3187}
3188
3189static struct mem_cgroup *mem_cgroup_alloc(void) 3689static struct mem_cgroup *mem_cgroup_alloc(void)
3190{ 3690{
3191 struct mem_cgroup *mem; 3691 struct mem_cgroup *mem;
3192 int size = mem_cgroup_size(); 3692 int size = sizeof(struct mem_cgroup);
3193 3693
3694 /* Can be very big if MAX_NUMNODES is very big */
3194 if (size < PAGE_SIZE) 3695 if (size < PAGE_SIZE)
3195 mem = kmalloc(size, GFP_KERNEL); 3696 mem = kmalloc(size, GFP_KERNEL);
3196 else 3697 else
3197 mem = vmalloc(size); 3698 mem = vmalloc(size);
3198 3699
3199 if (mem) 3700 if (!mem)
3200 memset(mem, 0, size); 3701 return NULL;
3702
3703 memset(mem, 0, size);
3704 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
3705 if (!mem->stat) {
3706 if (size < PAGE_SIZE)
3707 kfree(mem);
3708 else
3709 vfree(mem);
3710 mem = NULL;
3711 }
3201 return mem; 3712 return mem;
3202} 3713}
3203 3714
@@ -3222,7 +3733,8 @@ static void __mem_cgroup_free(struct mem_cgroup *mem)
3222 for_each_node_state(node, N_POSSIBLE) 3733 for_each_node_state(node, N_POSSIBLE)
3223 free_mem_cgroup_per_zone_info(mem, node); 3734 free_mem_cgroup_per_zone_info(mem, node);
3224 3735
3225 if (mem_cgroup_size() < PAGE_SIZE) 3736 free_percpu(mem->stat);
3737 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
3226 kfree(mem); 3738 kfree(mem);
3227 else 3739 else
3228 vfree(mem); 3740 vfree(mem);
@@ -3233,9 +3745,9 @@ static void mem_cgroup_get(struct mem_cgroup *mem)
3233 atomic_inc(&mem->refcnt); 3745 atomic_inc(&mem->refcnt);
3234} 3746}
3235 3747
3236static void mem_cgroup_put(struct mem_cgroup *mem) 3748static void __mem_cgroup_put(struct mem_cgroup *mem, int count)
3237{ 3749{
3238 if (atomic_dec_and_test(&mem->refcnt)) { 3750 if (atomic_sub_and_test(count, &mem->refcnt)) {
3239 struct mem_cgroup *parent = parent_mem_cgroup(mem); 3751 struct mem_cgroup *parent = parent_mem_cgroup(mem);
3240 __mem_cgroup_free(mem); 3752 __mem_cgroup_free(mem);
3241 if (parent) 3753 if (parent)
@@ -3243,6 +3755,11 @@ static void mem_cgroup_put(struct mem_cgroup *mem)
3243 } 3755 }
3244} 3756}
3245 3757
3758static void mem_cgroup_put(struct mem_cgroup *mem)
3759{
3760 __mem_cgroup_put(mem, 1);
3761}
3762
3246/* 3763/*
3247 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled. 3764 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
3248 */ 3765 */
@@ -3319,7 +3836,6 @@ mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
3319 INIT_WORK(&stock->work, drain_local_stock); 3836 INIT_WORK(&stock->work, drain_local_stock);
3320 } 3837 }
3321 hotcpu_notifier(memcg_stock_cpu_callback, 0); 3838 hotcpu_notifier(memcg_stock_cpu_callback, 0);
3322
3323 } else { 3839 } else {
3324 parent = mem_cgroup_from_cont(cont->parent); 3840 parent = mem_cgroup_from_cont(cont->parent);
3325 mem->use_hierarchy = parent->use_hierarchy; 3841 mem->use_hierarchy = parent->use_hierarchy;
@@ -3345,6 +3861,8 @@ mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
3345 if (parent) 3861 if (parent)
3346 mem->swappiness = get_swappiness(parent); 3862 mem->swappiness = get_swappiness(parent);
3347 atomic_set(&mem->refcnt, 1); 3863 atomic_set(&mem->refcnt, 1);
3864 mem->move_charge_at_immigrate = 0;
3865 mutex_init(&mem->thresholds_lock);
3348 return &mem->css; 3866 return &mem->css;
3349free_out: 3867free_out:
3350 __mem_cgroup_free(mem); 3868 __mem_cgroup_free(mem);
@@ -3381,17 +3899,450 @@ static int mem_cgroup_populate(struct cgroup_subsys *ss,
3381 return ret; 3899 return ret;
3382} 3900}
3383 3901
3902#ifdef CONFIG_MMU
3903/* Handlers for move charge at task migration. */
3904#define PRECHARGE_COUNT_AT_ONCE 256
3905static int mem_cgroup_do_precharge(unsigned long count)
3906{
3907 int ret = 0;
3908 int batch_count = PRECHARGE_COUNT_AT_ONCE;
3909 struct mem_cgroup *mem = mc.to;
3910
3911 if (mem_cgroup_is_root(mem)) {
3912 mc.precharge += count;
3913 /* we don't need css_get for root */
3914 return ret;
3915 }
3916 /* try to charge at once */
3917 if (count > 1) {
3918 struct res_counter *dummy;
3919 /*
3920 * "mem" cannot be under rmdir() because we've already checked
3921 * by cgroup_lock_live_cgroup() that it is not removed and we
3922 * are still under the same cgroup_mutex. So we can postpone
3923 * css_get().
3924 */
3925 if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy))
3926 goto one_by_one;
3927 if (do_swap_account && res_counter_charge(&mem->memsw,
3928 PAGE_SIZE * count, &dummy)) {
3929 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
3930 goto one_by_one;
3931 }
3932 mc.precharge += count;
3933 VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags));
3934 WARN_ON_ONCE(count > INT_MAX);
3935 __css_get(&mem->css, (int)count);
3936 return ret;
3937 }
3938one_by_one:
3939 /* fall back to one by one charge */
3940 while (count--) {
3941 if (signal_pending(current)) {
3942 ret = -EINTR;
3943 break;
3944 }
3945 if (!batch_count--) {
3946 batch_count = PRECHARGE_COUNT_AT_ONCE;
3947 cond_resched();
3948 }
3949 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
3950 if (ret || !mem)
3951 /* mem_cgroup_clear_mc() will do uncharge later */
3952 return -ENOMEM;
3953 mc.precharge++;
3954 }
3955 return ret;
3956}
3957
3958/**
3959 * is_target_pte_for_mc - check a pte whether it is valid for move charge
3960 * @vma: the vma the pte to be checked belongs
3961 * @addr: the address corresponding to the pte to be checked
3962 * @ptent: the pte to be checked
3963 * @target: the pointer the target page or swap ent will be stored(can be NULL)
3964 *
3965 * Returns
3966 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
3967 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
3968 * move charge. if @target is not NULL, the page is stored in target->page
3969 * with extra refcnt got(Callers should handle it).
3970 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
3971 * target for charge migration. if @target is not NULL, the entry is stored
3972 * in target->ent.
3973 *
3974 * Called with pte lock held.
3975 */
3976union mc_target {
3977 struct page *page;
3978 swp_entry_t ent;
3979};
3980
3981enum mc_target_type {
3982 MC_TARGET_NONE, /* not used */
3983 MC_TARGET_PAGE,
3984 MC_TARGET_SWAP,
3985};
3986
3987static int is_target_pte_for_mc(struct vm_area_struct *vma,
3988 unsigned long addr, pte_t ptent, union mc_target *target)
3989{
3990 struct page *page = NULL;
3991 struct page_cgroup *pc;
3992 int ret = 0;
3993 swp_entry_t ent = { .val = 0 };
3994 int usage_count = 0;
3995 bool move_anon = test_bit(MOVE_CHARGE_TYPE_ANON,
3996 &mc.to->move_charge_at_immigrate);
3997
3998 if (!pte_present(ptent)) {
3999 /* TODO: handle swap of shmes/tmpfs */
4000 if (pte_none(ptent) || pte_file(ptent))
4001 return 0;
4002 else if (is_swap_pte(ptent)) {
4003 ent = pte_to_swp_entry(ptent);
4004 if (!move_anon || non_swap_entry(ent))
4005 return 0;
4006 usage_count = mem_cgroup_count_swap_user(ent, &page);
4007 }
4008 } else {
4009 page = vm_normal_page(vma, addr, ptent);
4010 if (!page || !page_mapped(page))
4011 return 0;
4012 /*
4013 * TODO: We don't move charges of file(including shmem/tmpfs)
4014 * pages for now.
4015 */
4016 if (!move_anon || !PageAnon(page))
4017 return 0;
4018 if (!get_page_unless_zero(page))
4019 return 0;
4020 usage_count = page_mapcount(page);
4021 }
4022 if (usage_count > 1) {
4023 /*
4024 * TODO: We don't move charges of shared(used by multiple
4025 * processes) pages for now.
4026 */
4027 if (page)
4028 put_page(page);
4029 return 0;
4030 }
4031 if (page) {
4032 pc = lookup_page_cgroup(page);
4033 /*
4034 * Do only loose check w/o page_cgroup lock.
4035 * mem_cgroup_move_account() checks the pc is valid or not under
4036 * the lock.
4037 */
4038 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
4039 ret = MC_TARGET_PAGE;
4040 if (target)
4041 target->page = page;
4042 }
4043 if (!ret || !target)
4044 put_page(page);
4045 }
4046 /* throught */
4047 if (ent.val && do_swap_account && !ret) {
4048 unsigned short id;
4049 rcu_read_lock();
4050 id = css_id(&mc.from->css);
4051 rcu_read_unlock();
4052 if (id == lookup_swap_cgroup(ent)) {
4053 ret = MC_TARGET_SWAP;
4054 if (target)
4055 target->ent = ent;
4056 }
4057 }
4058 return ret;
4059}
4060
4061static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
4062 unsigned long addr, unsigned long end,
4063 struct mm_walk *walk)
4064{
4065 struct vm_area_struct *vma = walk->private;
4066 pte_t *pte;
4067 spinlock_t *ptl;
4068
4069 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4070 for (; addr != end; pte++, addr += PAGE_SIZE)
4071 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
4072 mc.precharge++; /* increment precharge temporarily */
4073 pte_unmap_unlock(pte - 1, ptl);
4074 cond_resched();
4075
4076 return 0;
4077}
4078
4079static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
4080{
4081 unsigned long precharge;
4082 struct vm_area_struct *vma;
4083
4084 down_read(&mm->mmap_sem);
4085 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4086 struct mm_walk mem_cgroup_count_precharge_walk = {
4087 .pmd_entry = mem_cgroup_count_precharge_pte_range,
4088 .mm = mm,
4089 .private = vma,
4090 };
4091 if (is_vm_hugetlb_page(vma))
4092 continue;
4093 /* TODO: We don't move charges of shmem/tmpfs pages for now. */
4094 if (vma->vm_flags & VM_SHARED)
4095 continue;
4096 walk_page_range(vma->vm_start, vma->vm_end,
4097 &mem_cgroup_count_precharge_walk);
4098 }
4099 up_read(&mm->mmap_sem);
4100
4101 precharge = mc.precharge;
4102 mc.precharge = 0;
4103
4104 return precharge;
4105}
4106
4107static int mem_cgroup_precharge_mc(struct mm_struct *mm)
4108{
4109 return mem_cgroup_do_precharge(mem_cgroup_count_precharge(mm));
4110}
4111
4112static void mem_cgroup_clear_mc(void)
4113{
4114 /* we must uncharge all the leftover precharges from mc.to */
4115 if (mc.precharge) {
4116 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
4117 mc.precharge = 0;
4118 }
4119 /*
4120 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
4121 * we must uncharge here.
4122 */
4123 if (mc.moved_charge) {
4124 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
4125 mc.moved_charge = 0;
4126 }
4127 /* we must fixup refcnts and charges */
4128 if (mc.moved_swap) {
4129 WARN_ON_ONCE(mc.moved_swap > INT_MAX);
4130 /* uncharge swap account from the old cgroup */
4131 if (!mem_cgroup_is_root(mc.from))
4132 res_counter_uncharge(&mc.from->memsw,
4133 PAGE_SIZE * mc.moved_swap);
4134 __mem_cgroup_put(mc.from, mc.moved_swap);
4135
4136 if (!mem_cgroup_is_root(mc.to)) {
4137 /*
4138 * we charged both to->res and to->memsw, so we should
4139 * uncharge to->res.
4140 */
4141 res_counter_uncharge(&mc.to->res,
4142 PAGE_SIZE * mc.moved_swap);
4143 VM_BUG_ON(test_bit(CSS_ROOT, &mc.to->css.flags));
4144 __css_put(&mc.to->css, mc.moved_swap);
4145 }
4146 /* we've already done mem_cgroup_get(mc.to) */
4147
4148 mc.moved_swap = 0;
4149 }
4150 mc.from = NULL;
4151 mc.to = NULL;
4152 mc.moving_task = NULL;
4153 wake_up_all(&mc.waitq);
4154}
4155
4156static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
4157 struct cgroup *cgroup,
4158 struct task_struct *p,
4159 bool threadgroup)
4160{
4161 int ret = 0;
4162 struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
4163
4164 if (mem->move_charge_at_immigrate) {
4165 struct mm_struct *mm;
4166 struct mem_cgroup *from = mem_cgroup_from_task(p);
4167
4168 VM_BUG_ON(from == mem);
4169
4170 mm = get_task_mm(p);
4171 if (!mm)
4172 return 0;
4173 /* We move charges only when we move a owner of the mm */
4174 if (mm->owner == p) {
4175 VM_BUG_ON(mc.from);
4176 VM_BUG_ON(mc.to);
4177 VM_BUG_ON(mc.precharge);
4178 VM_BUG_ON(mc.moved_charge);
4179 VM_BUG_ON(mc.moved_swap);
4180 VM_BUG_ON(mc.moving_task);
4181 mc.from = from;
4182 mc.to = mem;
4183 mc.precharge = 0;
4184 mc.moved_charge = 0;
4185 mc.moved_swap = 0;
4186 mc.moving_task = current;
4187
4188 ret = mem_cgroup_precharge_mc(mm);
4189 if (ret)
4190 mem_cgroup_clear_mc();
4191 }
4192 mmput(mm);
4193 }
4194 return ret;
4195}
4196
4197static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
4198 struct cgroup *cgroup,
4199 struct task_struct *p,
4200 bool threadgroup)
4201{
4202 mem_cgroup_clear_mc();
4203}
4204
4205static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
4206 unsigned long addr, unsigned long end,
4207 struct mm_walk *walk)
4208{
4209 int ret = 0;
4210 struct vm_area_struct *vma = walk->private;
4211 pte_t *pte;
4212 spinlock_t *ptl;
4213
4214retry:
4215 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4216 for (; addr != end; addr += PAGE_SIZE) {
4217 pte_t ptent = *(pte++);
4218 union mc_target target;
4219 int type;
4220 struct page *page;
4221 struct page_cgroup *pc;
4222 swp_entry_t ent;
4223
4224 if (!mc.precharge)
4225 break;
4226
4227 type = is_target_pte_for_mc(vma, addr, ptent, &target);
4228 switch (type) {
4229 case MC_TARGET_PAGE:
4230 page = target.page;
4231 if (isolate_lru_page(page))
4232 goto put;
4233 pc = lookup_page_cgroup(page);
4234 if (!mem_cgroup_move_account(pc,
4235 mc.from, mc.to, false)) {
4236 mc.precharge--;
4237 /* we uncharge from mc.from later. */
4238 mc.moved_charge++;
4239 }
4240 putback_lru_page(page);
4241put: /* is_target_pte_for_mc() gets the page */
4242 put_page(page);
4243 break;
4244 case MC_TARGET_SWAP:
4245 ent = target.ent;
4246 if (!mem_cgroup_move_swap_account(ent,
4247 mc.from, mc.to, false)) {
4248 mc.precharge--;
4249 /* we fixup refcnts and charges later. */
4250 mc.moved_swap++;
4251 }
4252 break;
4253 default:
4254 break;
4255 }
4256 }
4257 pte_unmap_unlock(pte - 1, ptl);
4258 cond_resched();
4259
4260 if (addr != end) {
4261 /*
4262 * We have consumed all precharges we got in can_attach().
4263 * We try charge one by one, but don't do any additional
4264 * charges to mc.to if we have failed in charge once in attach()
4265 * phase.
4266 */
4267 ret = mem_cgroup_do_precharge(1);
4268 if (!ret)
4269 goto retry;
4270 }
4271
4272 return ret;
4273}
4274
4275static void mem_cgroup_move_charge(struct mm_struct *mm)
4276{
4277 struct vm_area_struct *vma;
4278
4279 lru_add_drain_all();
4280 down_read(&mm->mmap_sem);
4281 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4282 int ret;
4283 struct mm_walk mem_cgroup_move_charge_walk = {
4284 .pmd_entry = mem_cgroup_move_charge_pte_range,
4285 .mm = mm,
4286 .private = vma,
4287 };
4288 if (is_vm_hugetlb_page(vma))
4289 continue;
4290 /* TODO: We don't move charges of shmem/tmpfs pages for now. */
4291 if (vma->vm_flags & VM_SHARED)
4292 continue;
4293 ret = walk_page_range(vma->vm_start, vma->vm_end,
4294 &mem_cgroup_move_charge_walk);
4295 if (ret)
4296 /*
4297 * means we have consumed all precharges and failed in
4298 * doing additional charge. Just abandon here.
4299 */
4300 break;
4301 }
4302 up_read(&mm->mmap_sem);
4303}
4304
3384static void mem_cgroup_move_task(struct cgroup_subsys *ss, 4305static void mem_cgroup_move_task(struct cgroup_subsys *ss,
3385 struct cgroup *cont, 4306 struct cgroup *cont,
3386 struct cgroup *old_cont, 4307 struct cgroup *old_cont,
3387 struct task_struct *p, 4308 struct task_struct *p,
3388 bool threadgroup) 4309 bool threadgroup)
3389{ 4310{
3390 /* 4311 struct mm_struct *mm;
3391 * FIXME: It's better to move charges of this process from old 4312
3392 * memcg to new memcg. But it's just on TODO-List now. 4313 if (!mc.to)
3393 */ 4314 /* no need to move charge */
4315 return;
4316
4317 mm = get_task_mm(p);
4318 if (mm) {
4319 mem_cgroup_move_charge(mm);
4320 mmput(mm);
4321 }
4322 mem_cgroup_clear_mc();
4323}
4324#else /* !CONFIG_MMU */
4325static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
4326 struct cgroup *cgroup,
4327 struct task_struct *p,
4328 bool threadgroup)
4329{
4330 return 0;
4331}
4332static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
4333 struct cgroup *cgroup,
4334 struct task_struct *p,
4335 bool threadgroup)
4336{
3394} 4337}
4338static void mem_cgroup_move_task(struct cgroup_subsys *ss,
4339 struct cgroup *cont,
4340 struct cgroup *old_cont,
4341 struct task_struct *p,
4342 bool threadgroup)
4343{
4344}
4345#endif
3395 4346
3396struct cgroup_subsys mem_cgroup_subsys = { 4347struct cgroup_subsys mem_cgroup_subsys = {
3397 .name = "memory", 4348 .name = "memory",
@@ -3400,6 +4351,8 @@ struct cgroup_subsys mem_cgroup_subsys = {
3400 .pre_destroy = mem_cgroup_pre_destroy, 4351 .pre_destroy = mem_cgroup_pre_destroy,
3401 .destroy = mem_cgroup_destroy, 4352 .destroy = mem_cgroup_destroy,
3402 .populate = mem_cgroup_populate, 4353 .populate = mem_cgroup_populate,
4354 .can_attach = mem_cgroup_can_attach,
4355 .cancel_attach = mem_cgroup_cancel_attach,
3403 .attach = mem_cgroup_move_task, 4356 .attach = mem_cgroup_move_task,
3404 .early_init = 0, 4357 .early_init = 0,
3405 .use_id = 1, 4358 .use_id = 1,