]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - mm/memcontrol.c
memcg: memory cgroup resource counters for hierarchy
[linux-2.6-omap-h63xx.git] / mm / memcontrol.c
1 /* memcontrol.c - Memory Controller
2  *
3  * Copyright IBM Corporation, 2007
4  * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5  *
6  * Copyright 2007 OpenVZ SWsoft Inc
7  * Author: Pavel Emelianov <xemul@openvz.org>
8  *
9  * 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
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  */
19
20 #include <linux/res_counter.h>
21 #include <linux/memcontrol.h>
22 #include <linux/cgroup.h>
23 #include <linux/mm.h>
24 #include <linux/pagemap.h>
25 #include <linux/smp.h>
26 #include <linux/page-flags.h>
27 #include <linux/backing-dev.h>
28 #include <linux/bit_spinlock.h>
29 #include <linux/rcupdate.h>
30 #include <linux/mutex.h>
31 #include <linux/slab.h>
32 #include <linux/swap.h>
33 #include <linux/spinlock.h>
34 #include <linux/fs.h>
35 #include <linux/seq_file.h>
36 #include <linux/vmalloc.h>
37 #include <linux/mm_inline.h>
38 #include <linux/page_cgroup.h>
39 #include "internal.h"
40
41 #include <asm/uaccess.h>
42
43 struct cgroup_subsys mem_cgroup_subsys __read_mostly;
44 #define MEM_CGROUP_RECLAIM_RETRIES      5
45
46 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
47 /* Turned on only when memory cgroup is enabled && really_do_swap_account = 0 */
48 int do_swap_account __read_mostly;
49 static int really_do_swap_account __initdata = 1; /* for remember boot option*/
50 #else
51 #define do_swap_account         (0)
52 #endif
53
54
55 /*
56  * Statistics for memory cgroup.
57  */
58 enum mem_cgroup_stat_index {
59         /*
60          * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
61          */
62         MEM_CGROUP_STAT_CACHE,     /* # of pages charged as cache */
63         MEM_CGROUP_STAT_RSS,       /* # of pages charged as rss */
64         MEM_CGROUP_STAT_PGPGIN_COUNT,   /* # of pages paged in */
65         MEM_CGROUP_STAT_PGPGOUT_COUNT,  /* # of pages paged out */
66
67         MEM_CGROUP_STAT_NSTATS,
68 };
69
70 struct mem_cgroup_stat_cpu {
71         s64 count[MEM_CGROUP_STAT_NSTATS];
72 } ____cacheline_aligned_in_smp;
73
74 struct mem_cgroup_stat {
75         struct mem_cgroup_stat_cpu cpustat[0];
76 };
77
78 /*
79  * For accounting under irq disable, no need for increment preempt count.
80  */
81 static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
82                 enum mem_cgroup_stat_index idx, int val)
83 {
84         stat->count[idx] += val;
85 }
86
87 static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
88                 enum mem_cgroup_stat_index idx)
89 {
90         int cpu;
91         s64 ret = 0;
92         for_each_possible_cpu(cpu)
93                 ret += stat->cpustat[cpu].count[idx];
94         return ret;
95 }
96
97 /*
98  * per-zone information in memory controller.
99  */
100 struct mem_cgroup_per_zone {
101         /*
102          * spin_lock to protect the per cgroup LRU
103          */
104         struct list_head        lists[NR_LRU_LISTS];
105         unsigned long           count[NR_LRU_LISTS];
106 };
107 /* Macro for accessing counter */
108 #define MEM_CGROUP_ZSTAT(mz, idx)       ((mz)->count[(idx)])
109
110 struct mem_cgroup_per_node {
111         struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
112 };
113
114 struct mem_cgroup_lru_info {
115         struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
116 };
117
118 /*
119  * The memory controller data structure. The memory controller controls both
120  * page cache and RSS per cgroup. We would eventually like to provide
121  * statistics based on the statistics developed by Rik Van Riel for clock-pro,
122  * to help the administrator determine what knobs to tune.
123  *
124  * TODO: Add a water mark for the memory controller. Reclaim will begin when
125  * we hit the water mark. May be even add a low water mark, such that
126  * no reclaim occurs from a cgroup at it's low water mark, this is
127  * a feature that will be implemented much later in the future.
128  */
129 struct mem_cgroup {
130         struct cgroup_subsys_state css;
131         /*
132          * the counter to account for memory usage
133          */
134         struct res_counter res;
135         /*
136          * the counter to account for mem+swap usage.
137          */
138         struct res_counter memsw;
139         /*
140          * Per cgroup active and inactive list, similar to the
141          * per zone LRU lists.
142          */
143         struct mem_cgroup_lru_info info;
144
145         int     prev_priority;  /* for recording reclaim priority */
146         int             obsolete;
147         atomic_t        refcnt;
148         /*
149          * statistics. This must be placed at the end of memcg.
150          */
151         struct mem_cgroup_stat stat;
152 };
153
154 enum charge_type {
155         MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
156         MEM_CGROUP_CHARGE_TYPE_MAPPED,
157         MEM_CGROUP_CHARGE_TYPE_SHMEM,   /* used by page migration of shmem */
158         MEM_CGROUP_CHARGE_TYPE_FORCE,   /* used by force_empty */
159         MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
160         NR_CHARGE_TYPE,
161 };
162
163 /* only for here (for easy reading.) */
164 #define PCGF_CACHE      (1UL << PCG_CACHE)
165 #define PCGF_USED       (1UL << PCG_USED)
166 #define PCGF_LOCK       (1UL << PCG_LOCK)
167 static const unsigned long
168 pcg_default_flags[NR_CHARGE_TYPE] = {
169         PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* File Cache */
170         PCGF_USED | PCGF_LOCK, /* Anon */
171         PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
172         0, /* FORCE */
173 };
174
175
176 /* for encoding cft->private value on file */
177 #define _MEM                    (0)
178 #define _MEMSWAP                (1)
179 #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
180 #define MEMFILE_TYPE(val)       (((val) >> 16) & 0xffff)
181 #define MEMFILE_ATTR(val)       ((val) & 0xffff)
182
183 static void mem_cgroup_get(struct mem_cgroup *mem);
184 static void mem_cgroup_put(struct mem_cgroup *mem);
185
186 static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
187                                          struct page_cgroup *pc,
188                                          bool charge)
189 {
190         int val = (charge)? 1 : -1;
191         struct mem_cgroup_stat *stat = &mem->stat;
192         struct mem_cgroup_stat_cpu *cpustat;
193         int cpu = get_cpu();
194
195         cpustat = &stat->cpustat[cpu];
196         if (PageCgroupCache(pc))
197                 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
198         else
199                 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
200
201         if (charge)
202                 __mem_cgroup_stat_add_safe(cpustat,
203                                 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
204         else
205                 __mem_cgroup_stat_add_safe(cpustat,
206                                 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
207         put_cpu();
208 }
209
210 static struct mem_cgroup_per_zone *
211 mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
212 {
213         return &mem->info.nodeinfo[nid]->zoneinfo[zid];
214 }
215
216 static struct mem_cgroup_per_zone *
217 page_cgroup_zoneinfo(struct page_cgroup *pc)
218 {
219         struct mem_cgroup *mem = pc->mem_cgroup;
220         int nid = page_cgroup_nid(pc);
221         int zid = page_cgroup_zid(pc);
222
223         return mem_cgroup_zoneinfo(mem, nid, zid);
224 }
225
226 static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
227                                         enum lru_list idx)
228 {
229         int nid, zid;
230         struct mem_cgroup_per_zone *mz;
231         u64 total = 0;
232
233         for_each_online_node(nid)
234                 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
235                         mz = mem_cgroup_zoneinfo(mem, nid, zid);
236                         total += MEM_CGROUP_ZSTAT(mz, idx);
237                 }
238         return total;
239 }
240
241 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
242 {
243         return container_of(cgroup_subsys_state(cont,
244                                 mem_cgroup_subsys_id), struct mem_cgroup,
245                                 css);
246 }
247
248 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
249 {
250         /*
251          * mm_update_next_owner() may clear mm->owner to NULL
252          * if it races with swapoff, page migration, etc.
253          * So this can be called with p == NULL.
254          */
255         if (unlikely(!p))
256                 return NULL;
257
258         return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
259                                 struct mem_cgroup, css);
260 }
261
262 /*
263  * Following LRU functions are allowed to be used without PCG_LOCK.
264  * Operations are called by routine of global LRU independently from memcg.
265  * What we have to take care of here is validness of pc->mem_cgroup.
266  *
267  * Changes to pc->mem_cgroup happens when
268  * 1. charge
269  * 2. moving account
270  * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
271  * It is added to LRU before charge.
272  * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
273  * When moving account, the page is not on LRU. It's isolated.
274  */
275
276 void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
277 {
278         struct page_cgroup *pc;
279         struct mem_cgroup *mem;
280         struct mem_cgroup_per_zone *mz;
281
282         if (mem_cgroup_disabled())
283                 return;
284         pc = lookup_page_cgroup(page);
285         /* can happen while we handle swapcache. */
286         if (list_empty(&pc->lru))
287                 return;
288         mz = page_cgroup_zoneinfo(pc);
289         mem = pc->mem_cgroup;
290         MEM_CGROUP_ZSTAT(mz, lru) -= 1;
291         list_del_init(&pc->lru);
292         return;
293 }
294
295 void mem_cgroup_del_lru(struct page *page)
296 {
297         mem_cgroup_del_lru_list(page, page_lru(page));
298 }
299
300 void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
301 {
302         struct mem_cgroup_per_zone *mz;
303         struct page_cgroup *pc;
304
305         if (mem_cgroup_disabled())
306                 return;
307
308         pc = lookup_page_cgroup(page);
309         smp_rmb();
310         /* unused page is not rotated. */
311         if (!PageCgroupUsed(pc))
312                 return;
313         mz = page_cgroup_zoneinfo(pc);
314         list_move(&pc->lru, &mz->lists[lru]);
315 }
316
317 void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
318 {
319         struct page_cgroup *pc;
320         struct mem_cgroup_per_zone *mz;
321
322         if (mem_cgroup_disabled())
323                 return;
324         pc = lookup_page_cgroup(page);
325         /* barrier to sync with "charge" */
326         smp_rmb();
327         if (!PageCgroupUsed(pc))
328                 return;
329
330         mz = page_cgroup_zoneinfo(pc);
331         MEM_CGROUP_ZSTAT(mz, lru) += 1;
332         list_add(&pc->lru, &mz->lists[lru]);
333 }
334 /*
335  * To add swapcache into LRU. Be careful to all this function.
336  * zone->lru_lock shouldn't be held and irq must not be disabled.
337  */
338 static void mem_cgroup_lru_fixup(struct page *page)
339 {
340         if (!isolate_lru_page(page))
341                 putback_lru_page(page);
342 }
343
344 void mem_cgroup_move_lists(struct page *page,
345                            enum lru_list from, enum lru_list to)
346 {
347         if (mem_cgroup_disabled())
348                 return;
349         mem_cgroup_del_lru_list(page, from);
350         mem_cgroup_add_lru_list(page, to);
351 }
352
353 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
354 {
355         int ret;
356
357         task_lock(task);
358         ret = task->mm && mm_match_cgroup(task->mm, mem);
359         task_unlock(task);
360         return ret;
361 }
362
363 /*
364  * Calculate mapped_ratio under memory controller. This will be used in
365  * vmscan.c for deteremining we have to reclaim mapped pages.
366  */
367 int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
368 {
369         long total, rss;
370
371         /*
372          * usage is recorded in bytes. But, here, we assume the number of
373          * physical pages can be represented by "long" on any arch.
374          */
375         total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
376         rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
377         return (int)((rss * 100L) / total);
378 }
379
380 /*
381  * prev_priority control...this will be used in memory reclaim path.
382  */
383 int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
384 {
385         return mem->prev_priority;
386 }
387
388 void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
389 {
390         if (priority < mem->prev_priority)
391                 mem->prev_priority = priority;
392 }
393
394 void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
395 {
396         mem->prev_priority = priority;
397 }
398
399 /*
400  * Calculate # of pages to be scanned in this priority/zone.
401  * See also vmscan.c
402  *
403  * priority starts from "DEF_PRIORITY" and decremented in each loop.
404  * (see include/linux/mmzone.h)
405  */
406
407 long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
408                                         int priority, enum lru_list lru)
409 {
410         long nr_pages;
411         int nid = zone->zone_pgdat->node_id;
412         int zid = zone_idx(zone);
413         struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
414
415         nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
416
417         return (nr_pages >> priority);
418 }
419
420 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
421                                         struct list_head *dst,
422                                         unsigned long *scanned, int order,
423                                         int mode, struct zone *z,
424                                         struct mem_cgroup *mem_cont,
425                                         int active, int file)
426 {
427         unsigned long nr_taken = 0;
428         struct page *page;
429         unsigned long scan;
430         LIST_HEAD(pc_list);
431         struct list_head *src;
432         struct page_cgroup *pc, *tmp;
433         int nid = z->zone_pgdat->node_id;
434         int zid = zone_idx(z);
435         struct mem_cgroup_per_zone *mz;
436         int lru = LRU_FILE * !!file + !!active;
437
438         BUG_ON(!mem_cont);
439         mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
440         src = &mz->lists[lru];
441
442         scan = 0;
443         list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
444                 if (scan >= nr_to_scan)
445                         break;
446
447                 page = pc->page;
448                 if (unlikely(!PageCgroupUsed(pc)))
449                         continue;
450                 if (unlikely(!PageLRU(page)))
451                         continue;
452
453                 scan++;
454                 if (__isolate_lru_page(page, mode, file) == 0) {
455                         list_move(&page->lru, dst);
456                         nr_taken++;
457                 }
458         }
459
460         *scanned = scan;
461         return nr_taken;
462 }
463
464 /*
465  * Unlike exported interface, "oom" parameter is added. if oom==true,
466  * oom-killer can be invoked.
467  */
468 static int __mem_cgroup_try_charge(struct mm_struct *mm,
469                         gfp_t gfp_mask, struct mem_cgroup **memcg,
470                         bool oom)
471 {
472         struct mem_cgroup *mem;
473         int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
474         struct res_counter *fail_res;
475         /*
476          * We always charge the cgroup the mm_struct belongs to.
477          * The mm_struct's mem_cgroup changes on task migration if the
478          * thread group leader migrates. It's possible that mm is not
479          * set, if so charge the init_mm (happens for pagecache usage).
480          */
481         if (likely(!*memcg)) {
482                 rcu_read_lock();
483                 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
484                 if (unlikely(!mem)) {
485                         rcu_read_unlock();
486                         return 0;
487                 }
488                 /*
489                  * For every charge from the cgroup, increment reference count
490                  */
491                 css_get(&mem->css);
492                 *memcg = mem;
493                 rcu_read_unlock();
494         } else {
495                 mem = *memcg;
496                 css_get(&mem->css);
497         }
498
499         while (1) {
500                 int ret;
501                 bool noswap = false;
502
503                 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
504                 if (likely(!ret)) {
505                         if (!do_swap_account)
506                                 break;
507                         ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
508                                                         &fail_res);
509                         if (likely(!ret))
510                                 break;
511                         /* mem+swap counter fails */
512                         res_counter_uncharge(&mem->res, PAGE_SIZE);
513                         noswap = true;
514                 }
515                 if (!(gfp_mask & __GFP_WAIT))
516                         goto nomem;
517
518                 if (try_to_free_mem_cgroup_pages(mem, gfp_mask, noswap))
519                         continue;
520
521                 /*
522                  * try_to_free_mem_cgroup_pages() might not give us a full
523                  * picture of reclaim. Some pages are reclaimed and might be
524                  * moved to swap cache or just unmapped from the cgroup.
525                  * Check the limit again to see if the reclaim reduced the
526                  * current usage of the cgroup before giving up
527                  *
528                  */
529                 if (!do_swap_account &&
530                         res_counter_check_under_limit(&mem->res))
531                         continue;
532                 if (do_swap_account &&
533                         res_counter_check_under_limit(&mem->memsw))
534                         continue;
535
536                 if (!nr_retries--) {
537                         if (oom)
538                                 mem_cgroup_out_of_memory(mem, gfp_mask);
539                         goto nomem;
540                 }
541         }
542         return 0;
543 nomem:
544         css_put(&mem->css);
545         return -ENOMEM;
546 }
547
548 /**
549  * mem_cgroup_try_charge - get charge of PAGE_SIZE.
550  * @mm: an mm_struct which is charged against. (when *memcg is NULL)
551  * @gfp_mask: gfp_mask for reclaim.
552  * @memcg: a pointer to memory cgroup which is charged against.
553  *
554  * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated
555  * memory cgroup from @mm is got and stored in *memcg.
556  *
557  * Returns 0 if success. -ENOMEM at failure.
558  * This call can invoke OOM-Killer.
559  */
560
561 int mem_cgroup_try_charge(struct mm_struct *mm,
562                           gfp_t mask, struct mem_cgroup **memcg)
563 {
564         return __mem_cgroup_try_charge(mm, mask, memcg, true);
565 }
566
567 /*
568  * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be
569  * USED state. If already USED, uncharge and return.
570  */
571
572 static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
573                                      struct page_cgroup *pc,
574                                      enum charge_type ctype)
575 {
576         /* try_charge() can return NULL to *memcg, taking care of it. */
577         if (!mem)
578                 return;
579
580         lock_page_cgroup(pc);
581         if (unlikely(PageCgroupUsed(pc))) {
582                 unlock_page_cgroup(pc);
583                 res_counter_uncharge(&mem->res, PAGE_SIZE);
584                 if (do_swap_account)
585                         res_counter_uncharge(&mem->memsw, PAGE_SIZE);
586                 css_put(&mem->css);
587                 return;
588         }
589         pc->mem_cgroup = mem;
590         smp_wmb();
591         pc->flags = pcg_default_flags[ctype];
592
593         mem_cgroup_charge_statistics(mem, pc, true);
594
595         unlock_page_cgroup(pc);
596 }
597
598 /**
599  * mem_cgroup_move_account - move account of the page
600  * @pc: page_cgroup of the page.
601  * @from: mem_cgroup which the page is moved from.
602  * @to: mem_cgroup which the page is moved to. @from != @to.
603  *
604  * The caller must confirm following.
605  * - page is not on LRU (isolate_page() is useful.)
606  *
607  * returns 0 at success,
608  * returns -EBUSY when lock is busy or "pc" is unstable.
609  *
610  * This function does "uncharge" from old cgroup but doesn't do "charge" to
611  * new cgroup. It should be done by a caller.
612  */
613
614 static int mem_cgroup_move_account(struct page_cgroup *pc,
615         struct mem_cgroup *from, struct mem_cgroup *to)
616 {
617         struct mem_cgroup_per_zone *from_mz, *to_mz;
618         int nid, zid;
619         int ret = -EBUSY;
620
621         VM_BUG_ON(from == to);
622         VM_BUG_ON(PageLRU(pc->page));
623
624         nid = page_cgroup_nid(pc);
625         zid = page_cgroup_zid(pc);
626         from_mz =  mem_cgroup_zoneinfo(from, nid, zid);
627         to_mz =  mem_cgroup_zoneinfo(to, nid, zid);
628
629         if (!trylock_page_cgroup(pc))
630                 return ret;
631
632         if (!PageCgroupUsed(pc))
633                 goto out;
634
635         if (pc->mem_cgroup != from)
636                 goto out;
637
638         css_put(&from->css);
639         res_counter_uncharge(&from->res, PAGE_SIZE);
640         mem_cgroup_charge_statistics(from, pc, false);
641         if (do_swap_account)
642                 res_counter_uncharge(&from->memsw, PAGE_SIZE);
643         pc->mem_cgroup = to;
644         mem_cgroup_charge_statistics(to, pc, true);
645         css_get(&to->css);
646         ret = 0;
647 out:
648         unlock_page_cgroup(pc);
649         return ret;
650 }
651
652 /*
653  * move charges to its parent.
654  */
655
656 static int mem_cgroup_move_parent(struct page_cgroup *pc,
657                                   struct mem_cgroup *child,
658                                   gfp_t gfp_mask)
659 {
660         struct page *page = pc->page;
661         struct cgroup *cg = child->css.cgroup;
662         struct cgroup *pcg = cg->parent;
663         struct mem_cgroup *parent;
664         int ret;
665
666         /* Is ROOT ? */
667         if (!pcg)
668                 return -EINVAL;
669
670
671         parent = mem_cgroup_from_cont(pcg);
672
673
674         ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
675         if (ret)
676                 return ret;
677
678         if (!get_page_unless_zero(page))
679                 return -EBUSY;
680
681         ret = isolate_lru_page(page);
682
683         if (ret)
684                 goto cancel;
685
686         ret = mem_cgroup_move_account(pc, child, parent);
687
688         /* drop extra refcnt by try_charge() (move_account increment one) */
689         css_put(&parent->css);
690         putback_lru_page(page);
691         if (!ret) {
692                 put_page(page);
693                 return 0;
694         }
695         /* uncharge if move fails */
696 cancel:
697         res_counter_uncharge(&parent->res, PAGE_SIZE);
698         if (do_swap_account)
699                 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
700         put_page(page);
701         return ret;
702 }
703
704 /*
705  * Charge the memory controller for page usage.
706  * Return
707  * 0 if the charge was successful
708  * < 0 if the cgroup is over its limit
709  */
710 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
711                                 gfp_t gfp_mask, enum charge_type ctype,
712                                 struct mem_cgroup *memcg)
713 {
714         struct mem_cgroup *mem;
715         struct page_cgroup *pc;
716         int ret;
717
718         pc = lookup_page_cgroup(page);
719         /* can happen at boot */
720         if (unlikely(!pc))
721                 return 0;
722         prefetchw(pc);
723
724         mem = memcg;
725         ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
726         if (ret)
727                 return ret;
728
729         __mem_cgroup_commit_charge(mem, pc, ctype);
730         return 0;
731 }
732
733 int mem_cgroup_newpage_charge(struct page *page,
734                               struct mm_struct *mm, gfp_t gfp_mask)
735 {
736         if (mem_cgroup_disabled())
737                 return 0;
738         if (PageCompound(page))
739                 return 0;
740         /*
741          * If already mapped, we don't have to account.
742          * If page cache, page->mapping has address_space.
743          * But page->mapping may have out-of-use anon_vma pointer,
744          * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
745          * is NULL.
746          */
747         if (page_mapped(page) || (page->mapping && !PageAnon(page)))
748                 return 0;
749         if (unlikely(!mm))
750                 mm = &init_mm;
751         return mem_cgroup_charge_common(page, mm, gfp_mask,
752                                 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
753 }
754
755 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
756                                 gfp_t gfp_mask)
757 {
758         if (mem_cgroup_disabled())
759                 return 0;
760         if (PageCompound(page))
761                 return 0;
762         /*
763          * Corner case handling. This is called from add_to_page_cache()
764          * in usual. But some FS (shmem) precharges this page before calling it
765          * and call add_to_page_cache() with GFP_NOWAIT.
766          *
767          * For GFP_NOWAIT case, the page may be pre-charged before calling
768          * add_to_page_cache(). (See shmem.c) check it here and avoid to call
769          * charge twice. (It works but has to pay a bit larger cost.)
770          */
771         if (!(gfp_mask & __GFP_WAIT)) {
772                 struct page_cgroup *pc;
773
774
775                 pc = lookup_page_cgroup(page);
776                 if (!pc)
777                         return 0;
778                 lock_page_cgroup(pc);
779                 if (PageCgroupUsed(pc)) {
780                         unlock_page_cgroup(pc);
781                         return 0;
782                 }
783                 unlock_page_cgroup(pc);
784         }
785
786         if (unlikely(!mm))
787                 mm = &init_mm;
788
789         if (page_is_file_cache(page))
790                 return mem_cgroup_charge_common(page, mm, gfp_mask,
791                                 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
792         else
793                 return mem_cgroup_charge_common(page, mm, gfp_mask,
794                                 MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
795 }
796
797 int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
798                                  struct page *page,
799                                  gfp_t mask, struct mem_cgroup **ptr)
800 {
801         struct mem_cgroup *mem;
802         swp_entry_t     ent;
803
804         if (mem_cgroup_disabled())
805                 return 0;
806
807         if (!do_swap_account)
808                 goto charge_cur_mm;
809
810         /*
811          * A racing thread's fault, or swapoff, may have already updated
812          * the pte, and even removed page from swap cache: return success
813          * to go on to do_swap_page()'s pte_same() test, which should fail.
814          */
815         if (!PageSwapCache(page))
816                 return 0;
817
818         ent.val = page_private(page);
819
820         mem = lookup_swap_cgroup(ent);
821         if (!mem || mem->obsolete)
822                 goto charge_cur_mm;
823         *ptr = mem;
824         return __mem_cgroup_try_charge(NULL, mask, ptr, true);
825 charge_cur_mm:
826         if (unlikely(!mm))
827                 mm = &init_mm;
828         return __mem_cgroup_try_charge(mm, mask, ptr, true);
829 }
830
831 #ifdef CONFIG_SWAP
832
833 int mem_cgroup_cache_charge_swapin(struct page *page,
834                         struct mm_struct *mm, gfp_t mask, bool locked)
835 {
836         int ret = 0;
837
838         if (mem_cgroup_disabled())
839                 return 0;
840         if (unlikely(!mm))
841                 mm = &init_mm;
842         if (!locked)
843                 lock_page(page);
844         /*
845          * If not locked, the page can be dropped from SwapCache until
846          * we reach here.
847          */
848         if (PageSwapCache(page)) {
849                 struct mem_cgroup *mem = NULL;
850                 swp_entry_t ent;
851
852                 ent.val = page_private(page);
853                 if (do_swap_account) {
854                         mem = lookup_swap_cgroup(ent);
855                         if (mem && mem->obsolete)
856                                 mem = NULL;
857                         if (mem)
858                                 mm = NULL;
859                 }
860                 ret = mem_cgroup_charge_common(page, mm, mask,
861                                 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
862
863                 if (!ret && do_swap_account) {
864                         /* avoid double counting */
865                         mem = swap_cgroup_record(ent, NULL);
866                         if (mem) {
867                                 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
868                                 mem_cgroup_put(mem);
869                         }
870                 }
871         }
872         if (!locked)
873                 unlock_page(page);
874         /* add this page(page_cgroup) to the LRU we want. */
875         mem_cgroup_lru_fixup(page);
876
877         return ret;
878 }
879 #endif
880
881 void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
882 {
883         struct page_cgroup *pc;
884
885         if (mem_cgroup_disabled())
886                 return;
887         if (!ptr)
888                 return;
889         pc = lookup_page_cgroup(page);
890         __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED);
891         /*
892          * Now swap is on-memory. This means this page may be
893          * counted both as mem and swap....double count.
894          * Fix it by uncharging from memsw. This SwapCache is stable
895          * because we're still under lock_page().
896          */
897         if (do_swap_account) {
898                 swp_entry_t ent = {.val = page_private(page)};
899                 struct mem_cgroup *memcg;
900                 memcg = swap_cgroup_record(ent, NULL);
901                 if (memcg) {
902                         /* If memcg is obsolete, memcg can be != ptr */
903                         res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
904                         mem_cgroup_put(memcg);
905                 }
906
907         }
908         /* add this page(page_cgroup) to the LRU we want. */
909         mem_cgroup_lru_fixup(page);
910 }
911
912 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
913 {
914         if (mem_cgroup_disabled())
915                 return;
916         if (!mem)
917                 return;
918         res_counter_uncharge(&mem->res, PAGE_SIZE);
919         if (do_swap_account)
920                 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
921         css_put(&mem->css);
922 }
923
924
925 /*
926  * uncharge if !page_mapped(page)
927  */
928 static struct mem_cgroup *
929 __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
930 {
931         struct page_cgroup *pc;
932         struct mem_cgroup *mem = NULL;
933         struct mem_cgroup_per_zone *mz;
934
935         if (mem_cgroup_disabled())
936                 return NULL;
937
938         if (PageSwapCache(page))
939                 return NULL;
940
941         /*
942          * Check if our page_cgroup is valid
943          */
944         pc = lookup_page_cgroup(page);
945         if (unlikely(!pc || !PageCgroupUsed(pc)))
946                 return NULL;
947
948         lock_page_cgroup(pc);
949
950         mem = pc->mem_cgroup;
951
952         if (!PageCgroupUsed(pc))
953                 goto unlock_out;
954
955         switch (ctype) {
956         case MEM_CGROUP_CHARGE_TYPE_MAPPED:
957                 if (page_mapped(page))
958                         goto unlock_out;
959                 break;
960         case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
961                 if (!PageAnon(page)) {  /* Shared memory */
962                         if (page->mapping && !page_is_file_cache(page))
963                                 goto unlock_out;
964                 } else if (page_mapped(page)) /* Anon */
965                                 goto unlock_out;
966                 break;
967         default:
968                 break;
969         }
970
971         res_counter_uncharge(&mem->res, PAGE_SIZE);
972         if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
973                 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
974
975         mem_cgroup_charge_statistics(mem, pc, false);
976         ClearPageCgroupUsed(pc);
977
978         mz = page_cgroup_zoneinfo(pc);
979         unlock_page_cgroup(pc);
980
981         css_put(&mem->css);
982
983         return mem;
984
985 unlock_out:
986         unlock_page_cgroup(pc);
987         return NULL;
988 }
989
990 void mem_cgroup_uncharge_page(struct page *page)
991 {
992         /* early check. */
993         if (page_mapped(page))
994                 return;
995         if (page->mapping && !PageAnon(page))
996                 return;
997         __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
998 }
999
1000 void mem_cgroup_uncharge_cache_page(struct page *page)
1001 {
1002         VM_BUG_ON(page_mapped(page));
1003         VM_BUG_ON(page->mapping);
1004         __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1005 }
1006
1007 /*
1008  * called from __delete_from_swap_cache() and drop "page" account.
1009  * memcg information is recorded to swap_cgroup of "ent"
1010  */
1011 void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent)
1012 {
1013         struct mem_cgroup *memcg;
1014
1015         memcg = __mem_cgroup_uncharge_common(page,
1016                                         MEM_CGROUP_CHARGE_TYPE_SWAPOUT);
1017         /* record memcg information */
1018         if (do_swap_account && memcg) {
1019                 swap_cgroup_record(ent, memcg);
1020                 mem_cgroup_get(memcg);
1021         }
1022 }
1023
1024 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1025 /*
1026  * called from swap_entry_free(). remove record in swap_cgroup and
1027  * uncharge "memsw" account.
1028  */
1029 void mem_cgroup_uncharge_swap(swp_entry_t ent)
1030 {
1031         struct mem_cgroup *memcg;
1032
1033         if (!do_swap_account)
1034                 return;
1035
1036         memcg = swap_cgroup_record(ent, NULL);
1037         if (memcg) {
1038                 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1039                 mem_cgroup_put(memcg);
1040         }
1041 }
1042 #endif
1043
1044 /*
1045  * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1046  * page belongs to.
1047  */
1048 int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
1049 {
1050         struct page_cgroup *pc;
1051         struct mem_cgroup *mem = NULL;
1052         int ret = 0;
1053
1054         if (mem_cgroup_disabled())
1055                 return 0;
1056
1057         pc = lookup_page_cgroup(page);
1058         lock_page_cgroup(pc);
1059         if (PageCgroupUsed(pc)) {
1060                 mem = pc->mem_cgroup;
1061                 css_get(&mem->css);
1062         }
1063         unlock_page_cgroup(pc);
1064
1065         if (mem) {
1066                 ret = mem_cgroup_try_charge(NULL, GFP_HIGHUSER_MOVABLE, &mem);
1067                 css_put(&mem->css);
1068         }
1069         *ptr = mem;
1070         return ret;
1071 }
1072
1073 /* remove redundant charge if migration failed*/
1074 void mem_cgroup_end_migration(struct mem_cgroup *mem,
1075                 struct page *oldpage, struct page *newpage)
1076 {
1077         struct page *target, *unused;
1078         struct page_cgroup *pc;
1079         enum charge_type ctype;
1080
1081         if (!mem)
1082                 return;
1083
1084         /* at migration success, oldpage->mapping is NULL. */
1085         if (oldpage->mapping) {
1086                 target = oldpage;
1087                 unused = NULL;
1088         } else {
1089                 target = newpage;
1090                 unused = oldpage;
1091         }
1092
1093         if (PageAnon(target))
1094                 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1095         else if (page_is_file_cache(target))
1096                 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1097         else
1098                 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1099
1100         /* unused page is not on radix-tree now. */
1101         if (unused)
1102                 __mem_cgroup_uncharge_common(unused, ctype);
1103
1104         pc = lookup_page_cgroup(target);
1105         /*
1106          * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1107          * So, double-counting is effectively avoided.
1108          */
1109         __mem_cgroup_commit_charge(mem, pc, ctype);
1110
1111         /*
1112          * Both of oldpage and newpage are still under lock_page().
1113          * Then, we don't have to care about race in radix-tree.
1114          * But we have to be careful that this page is unmapped or not.
1115          *
1116          * There is a case for !page_mapped(). At the start of
1117          * migration, oldpage was mapped. But now, it's zapped.
1118          * But we know *target* page is not freed/reused under us.
1119          * mem_cgroup_uncharge_page() does all necessary checks.
1120          */
1121         if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1122                 mem_cgroup_uncharge_page(target);
1123 }
1124
1125 /*
1126  * A call to try to shrink memory usage under specified resource controller.
1127  * This is typically used for page reclaiming for shmem for reducing side
1128  * effect of page allocation from shmem, which is used by some mem_cgroup.
1129  */
1130 int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
1131 {
1132         struct mem_cgroup *mem;
1133         int progress = 0;
1134         int retry = MEM_CGROUP_RECLAIM_RETRIES;
1135
1136         if (mem_cgroup_disabled())
1137                 return 0;
1138         if (!mm)
1139                 return 0;
1140
1141         rcu_read_lock();
1142         mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1143         if (unlikely(!mem)) {
1144                 rcu_read_unlock();
1145                 return 0;
1146         }
1147         css_get(&mem->css);
1148         rcu_read_unlock();
1149
1150         do {
1151                 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask, true);
1152                 progress += res_counter_check_under_limit(&mem->res);
1153         } while (!progress && --retry);
1154
1155         css_put(&mem->css);
1156         if (!retry)
1157                 return -ENOMEM;
1158         return 0;
1159 }
1160
1161 static DEFINE_MUTEX(set_limit_mutex);
1162
1163 static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
1164                                 unsigned long long val)
1165 {
1166
1167         int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
1168         int progress;
1169         u64 memswlimit;
1170         int ret = 0;
1171
1172         while (retry_count) {
1173                 if (signal_pending(current)) {
1174                         ret = -EINTR;
1175                         break;
1176                 }
1177                 /*
1178                  * Rather than hide all in some function, I do this in
1179                  * open coded manner. You see what this really does.
1180                  * We have to guarantee mem->res.limit < mem->memsw.limit.
1181                  */
1182                 mutex_lock(&set_limit_mutex);
1183                 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1184                 if (memswlimit < val) {
1185                         ret = -EINVAL;
1186                         mutex_unlock(&set_limit_mutex);
1187                         break;
1188                 }
1189                 ret = res_counter_set_limit(&memcg->res, val);
1190                 mutex_unlock(&set_limit_mutex);
1191
1192                 if (!ret)
1193                         break;
1194
1195                 progress = try_to_free_mem_cgroup_pages(memcg,
1196                                 GFP_HIGHUSER_MOVABLE, false);
1197                 if (!progress)                  retry_count--;
1198         }
1199         return ret;
1200 }
1201
1202 int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
1203                                 unsigned long long val)
1204 {
1205         int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
1206         u64 memlimit, oldusage, curusage;
1207         int ret;
1208
1209         if (!do_swap_account)
1210                 return -EINVAL;
1211
1212         while (retry_count) {
1213                 if (signal_pending(current)) {
1214                         ret = -EINTR;
1215                         break;
1216                 }
1217                 /*
1218                  * Rather than hide all in some function, I do this in
1219                  * open coded manner. You see what this really does.
1220                  * We have to guarantee mem->res.limit < mem->memsw.limit.
1221                  */
1222                 mutex_lock(&set_limit_mutex);
1223                 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1224                 if (memlimit > val) {
1225                         ret = -EINVAL;
1226                         mutex_unlock(&set_limit_mutex);
1227                         break;
1228                 }
1229                 ret = res_counter_set_limit(&memcg->memsw, val);
1230                 mutex_unlock(&set_limit_mutex);
1231
1232                 if (!ret)
1233                         break;
1234
1235                 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1236                 try_to_free_mem_cgroup_pages(memcg, GFP_HIGHUSER_MOVABLE, true);
1237                 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1238                 if (curusage >= oldusage)
1239                         retry_count--;
1240         }
1241         return ret;
1242 }
1243
1244 /*
1245  * This routine traverse page_cgroup in given list and drop them all.
1246  * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1247  */
1248 static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
1249                                 int node, int zid, enum lru_list lru)
1250 {
1251         struct zone *zone;
1252         struct mem_cgroup_per_zone *mz;
1253         struct page_cgroup *pc, *busy;
1254         unsigned long flags, loop;
1255         struct list_head *list;
1256         int ret = 0;
1257
1258         zone = &NODE_DATA(node)->node_zones[zid];
1259         mz = mem_cgroup_zoneinfo(mem, node, zid);
1260         list = &mz->lists[lru];
1261
1262         loop = MEM_CGROUP_ZSTAT(mz, lru);
1263         /* give some margin against EBUSY etc...*/
1264         loop += 256;
1265         busy = NULL;
1266         while (loop--) {
1267                 ret = 0;
1268                 spin_lock_irqsave(&zone->lru_lock, flags);
1269                 if (list_empty(list)) {
1270                         spin_unlock_irqrestore(&zone->lru_lock, flags);
1271                         break;
1272                 }
1273                 pc = list_entry(list->prev, struct page_cgroup, lru);
1274                 if (busy == pc) {
1275                         list_move(&pc->lru, list);
1276                         busy = 0;
1277                         spin_unlock_irqrestore(&zone->lru_lock, flags);
1278                         continue;
1279                 }
1280                 spin_unlock_irqrestore(&zone->lru_lock, flags);
1281
1282                 ret = mem_cgroup_move_parent(pc, mem, GFP_HIGHUSER_MOVABLE);
1283                 if (ret == -ENOMEM)
1284                         break;
1285
1286                 if (ret == -EBUSY || ret == -EINVAL) {
1287                         /* found lock contention or "pc" is obsolete. */
1288                         busy = pc;
1289                         cond_resched();
1290                 } else
1291                         busy = NULL;
1292         }
1293
1294         if (!ret && !list_empty(list))
1295                 return -EBUSY;
1296         return ret;
1297 }
1298
1299 /*
1300  * make mem_cgroup's charge to be 0 if there is no task.
1301  * This enables deleting this mem_cgroup.
1302  */
1303 static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
1304 {
1305         int ret;
1306         int node, zid, shrink;
1307         int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
1308         struct cgroup *cgrp = mem->css.cgroup;
1309
1310         css_get(&mem->css);
1311
1312         shrink = 0;
1313         /* should free all ? */
1314         if (free_all)
1315                 goto try_to_free;
1316 move_account:
1317         while (mem->res.usage > 0) {
1318                 ret = -EBUSY;
1319                 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
1320                         goto out;
1321                 ret = -EINTR;
1322                 if (signal_pending(current))
1323                         goto out;
1324                 /* This is for making all *used* pages to be on LRU. */
1325                 lru_add_drain_all();
1326                 ret = 0;
1327                 for_each_node_state(node, N_POSSIBLE) {
1328                         for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
1329                                 enum lru_list l;
1330                                 for_each_lru(l) {
1331                                         ret = mem_cgroup_force_empty_list(mem,
1332                                                         node, zid, l);
1333                                         if (ret)
1334                                                 break;
1335                                 }
1336                         }
1337                         if (ret)
1338                                 break;
1339                 }
1340                 /* it seems parent cgroup doesn't have enough mem */
1341                 if (ret == -ENOMEM)
1342                         goto try_to_free;
1343                 cond_resched();
1344         }
1345         ret = 0;
1346 out:
1347         css_put(&mem->css);
1348         return ret;
1349
1350 try_to_free:
1351         /* returns EBUSY if there is a task or if we come here twice. */
1352         if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
1353                 ret = -EBUSY;
1354                 goto out;
1355         }
1356         /* we call try-to-free pages for make this cgroup empty */
1357         lru_add_drain_all();
1358         /* try to free all pages in this cgroup */
1359         shrink = 1;
1360         while (nr_retries && mem->res.usage > 0) {
1361                 int progress;
1362
1363                 if (signal_pending(current)) {
1364                         ret = -EINTR;
1365                         goto out;
1366                 }
1367                 progress = try_to_free_mem_cgroup_pages(mem,
1368                                                   GFP_HIGHUSER_MOVABLE, false);
1369                 if (!progress) {
1370                         nr_retries--;
1371                         /* maybe some writeback is necessary */
1372                         congestion_wait(WRITE, HZ/10);
1373                 }
1374
1375         }
1376         lru_add_drain();
1377         /* try move_account...there may be some *locked* pages. */
1378         if (mem->res.usage)
1379                 goto move_account;
1380         ret = 0;
1381         goto out;
1382 }
1383
1384 int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
1385 {
1386         return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
1387 }
1388
1389
1390 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
1391 {
1392         struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1393         u64 val = 0;
1394         int type, name;
1395
1396         type = MEMFILE_TYPE(cft->private);
1397         name = MEMFILE_ATTR(cft->private);
1398         switch (type) {
1399         case _MEM:
1400                 val = res_counter_read_u64(&mem->res, name);
1401                 break;
1402         case _MEMSWAP:
1403                 if (do_swap_account)
1404                         val = res_counter_read_u64(&mem->memsw, name);
1405                 break;
1406         default:
1407                 BUG();
1408                 break;
1409         }
1410         return val;
1411 }
1412 /*
1413  * The user of this function is...
1414  * RES_LIMIT.
1415  */
1416 static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
1417                             const char *buffer)
1418 {
1419         struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
1420         int type, name;
1421         unsigned long long val;
1422         int ret;
1423
1424         type = MEMFILE_TYPE(cft->private);
1425         name = MEMFILE_ATTR(cft->private);
1426         switch (name) {
1427         case RES_LIMIT:
1428                 /* This function does all necessary parse...reuse it */
1429                 ret = res_counter_memparse_write_strategy(buffer, &val);
1430                 if (ret)
1431                         break;
1432                 if (type == _MEM)
1433                         ret = mem_cgroup_resize_limit(memcg, val);
1434                 else
1435                         ret = mem_cgroup_resize_memsw_limit(memcg, val);
1436                 break;
1437         default:
1438                 ret = -EINVAL; /* should be BUG() ? */
1439                 break;
1440         }
1441         return ret;
1442 }
1443
1444 static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
1445 {
1446         struct mem_cgroup *mem;
1447         int type, name;
1448
1449         mem = mem_cgroup_from_cont(cont);
1450         type = MEMFILE_TYPE(event);
1451         name = MEMFILE_ATTR(event);
1452         switch (name) {
1453         case RES_MAX_USAGE:
1454                 if (type == _MEM)
1455                         res_counter_reset_max(&mem->res);
1456                 else
1457                         res_counter_reset_max(&mem->memsw);
1458                 break;
1459         case RES_FAILCNT:
1460                 if (type == _MEM)
1461                         res_counter_reset_failcnt(&mem->res);
1462                 else
1463                         res_counter_reset_failcnt(&mem->memsw);
1464                 break;
1465         }
1466         return 0;
1467 }
1468
1469 static const struct mem_cgroup_stat_desc {
1470         const char *msg;
1471         u64 unit;
1472 } mem_cgroup_stat_desc[] = {
1473         [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
1474         [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
1475         [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
1476         [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
1477 };
1478
1479 static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
1480                                  struct cgroup_map_cb *cb)
1481 {
1482         struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
1483         struct mem_cgroup_stat *stat = &mem_cont->stat;
1484         int i;
1485
1486         for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
1487                 s64 val;
1488
1489                 val = mem_cgroup_read_stat(stat, i);
1490                 val *= mem_cgroup_stat_desc[i].unit;
1491                 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
1492         }
1493         /* showing # of active pages */
1494         {
1495                 unsigned long active_anon, inactive_anon;
1496                 unsigned long active_file, inactive_file;
1497                 unsigned long unevictable;
1498
1499                 inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
1500                                                 LRU_INACTIVE_ANON);
1501                 active_anon = mem_cgroup_get_all_zonestat(mem_cont,
1502                                                 LRU_ACTIVE_ANON);
1503                 inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
1504                                                 LRU_INACTIVE_FILE);
1505                 active_file = mem_cgroup_get_all_zonestat(mem_cont,
1506                                                 LRU_ACTIVE_FILE);
1507                 unevictable = mem_cgroup_get_all_zonestat(mem_cont,
1508                                                         LRU_UNEVICTABLE);
1509
1510                 cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
1511                 cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
1512                 cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
1513                 cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
1514                 cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
1515
1516         }
1517         return 0;
1518 }
1519
1520
1521 static struct cftype mem_cgroup_files[] = {
1522         {
1523                 .name = "usage_in_bytes",
1524                 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
1525                 .read_u64 = mem_cgroup_read,
1526         },
1527         {
1528                 .name = "max_usage_in_bytes",
1529                 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
1530                 .trigger = mem_cgroup_reset,
1531                 .read_u64 = mem_cgroup_read,
1532         },
1533         {
1534                 .name = "limit_in_bytes",
1535                 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
1536                 .write_string = mem_cgroup_write,
1537                 .read_u64 = mem_cgroup_read,
1538         },
1539         {
1540                 .name = "failcnt",
1541                 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
1542                 .trigger = mem_cgroup_reset,
1543                 .read_u64 = mem_cgroup_read,
1544         },
1545         {
1546                 .name = "stat",
1547                 .read_map = mem_control_stat_show,
1548         },
1549         {
1550                 .name = "force_empty",
1551                 .trigger = mem_cgroup_force_empty_write,
1552         },
1553 };
1554
1555 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1556 static struct cftype memsw_cgroup_files[] = {
1557         {
1558                 .name = "memsw.usage_in_bytes",
1559                 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
1560                 .read_u64 = mem_cgroup_read,
1561         },
1562         {
1563                 .name = "memsw.max_usage_in_bytes",
1564                 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
1565                 .trigger = mem_cgroup_reset,
1566                 .read_u64 = mem_cgroup_read,
1567         },
1568         {
1569                 .name = "memsw.limit_in_bytes",
1570                 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
1571                 .write_string = mem_cgroup_write,
1572                 .read_u64 = mem_cgroup_read,
1573         },
1574         {
1575                 .name = "memsw.failcnt",
1576                 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
1577                 .trigger = mem_cgroup_reset,
1578                 .read_u64 = mem_cgroup_read,
1579         },
1580 };
1581
1582 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
1583 {
1584         if (!do_swap_account)
1585                 return 0;
1586         return cgroup_add_files(cont, ss, memsw_cgroup_files,
1587                                 ARRAY_SIZE(memsw_cgroup_files));
1588 };
1589 #else
1590 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
1591 {
1592         return 0;
1593 }
1594 #endif
1595
1596 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1597 {
1598         struct mem_cgroup_per_node *pn;
1599         struct mem_cgroup_per_zone *mz;
1600         enum lru_list l;
1601         int zone, tmp = node;
1602         /*
1603          * This routine is called against possible nodes.
1604          * But it's BUG to call kmalloc() against offline node.
1605          *
1606          * TODO: this routine can waste much memory for nodes which will
1607          *       never be onlined. It's better to use memory hotplug callback
1608          *       function.
1609          */
1610         if (!node_state(node, N_NORMAL_MEMORY))
1611                 tmp = -1;
1612         pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
1613         if (!pn)
1614                 return 1;
1615
1616         mem->info.nodeinfo[node] = pn;
1617         memset(pn, 0, sizeof(*pn));
1618
1619         for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1620                 mz = &pn->zoneinfo[zone];
1621                 for_each_lru(l)
1622                         INIT_LIST_HEAD(&mz->lists[l]);
1623         }
1624         return 0;
1625 }
1626
1627 static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1628 {
1629         kfree(mem->info.nodeinfo[node]);
1630 }
1631
1632 static int mem_cgroup_size(void)
1633 {
1634         int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
1635         return sizeof(struct mem_cgroup) + cpustat_size;
1636 }
1637
1638 static struct mem_cgroup *mem_cgroup_alloc(void)
1639 {
1640         struct mem_cgroup *mem;
1641         int size = mem_cgroup_size();
1642
1643         if (size < PAGE_SIZE)
1644                 mem = kmalloc(size, GFP_KERNEL);
1645         else
1646                 mem = vmalloc(size);
1647
1648         if (mem)
1649                 memset(mem, 0, size);
1650         return mem;
1651 }
1652
1653 /*
1654  * At destroying mem_cgroup, references from swap_cgroup can remain.
1655  * (scanning all at force_empty is too costly...)
1656  *
1657  * Instead of clearing all references at force_empty, we remember
1658  * the number of reference from swap_cgroup and free mem_cgroup when
1659  * it goes down to 0.
1660  *
1661  * When mem_cgroup is destroyed, mem->obsolete will be set to 0 and
1662  * entry which points to this memcg will be ignore at swapin.
1663  *
1664  * Removal of cgroup itself succeeds regardless of refs from swap.
1665  */
1666
1667 static void mem_cgroup_free(struct mem_cgroup *mem)
1668 {
1669         int node;
1670
1671         if (atomic_read(&mem->refcnt) > 0)
1672                 return;
1673
1674
1675         for_each_node_state(node, N_POSSIBLE)
1676                 free_mem_cgroup_per_zone_info(mem, node);
1677
1678         if (mem_cgroup_size() < PAGE_SIZE)
1679                 kfree(mem);
1680         else
1681                 vfree(mem);
1682 }
1683
1684 static void mem_cgroup_get(struct mem_cgroup *mem)
1685 {
1686         atomic_inc(&mem->refcnt);
1687 }
1688
1689 static void mem_cgroup_put(struct mem_cgroup *mem)
1690 {
1691         if (atomic_dec_and_test(&mem->refcnt)) {
1692                 if (!mem->obsolete)
1693                         return;
1694                 mem_cgroup_free(mem);
1695         }
1696 }
1697
1698
1699 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1700 static void __init enable_swap_cgroup(void)
1701 {
1702         if (!mem_cgroup_disabled() && really_do_swap_account)
1703                 do_swap_account = 1;
1704 }
1705 #else
1706 static void __init enable_swap_cgroup(void)
1707 {
1708 }
1709 #endif
1710
1711 static struct cgroup_subsys_state *
1712 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1713 {
1714         struct mem_cgroup *mem, *parent;
1715         int node;
1716
1717         mem = mem_cgroup_alloc();
1718         if (!mem)
1719                 return ERR_PTR(-ENOMEM);
1720
1721         for_each_node_state(node, N_POSSIBLE)
1722                 if (alloc_mem_cgroup_per_zone_info(mem, node))
1723                         goto free_out;
1724         /* root ? */
1725         if (cont->parent == NULL) {
1726                 enable_swap_cgroup();
1727                 parent = NULL;
1728         } else
1729                 parent = mem_cgroup_from_cont(cont->parent);
1730
1731         res_counter_init(&mem->res, parent ? &parent->res : NULL);
1732         res_counter_init(&mem->memsw, parent ? &parent->memsw : NULL);
1733
1734
1735         return &mem->css;
1736 free_out:
1737         for_each_node_state(node, N_POSSIBLE)
1738                 free_mem_cgroup_per_zone_info(mem, node);
1739         mem_cgroup_free(mem);
1740         return ERR_PTR(-ENOMEM);
1741 }
1742
1743 static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1744                                         struct cgroup *cont)
1745 {
1746         struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1747         mem->obsolete = 1;
1748         mem_cgroup_force_empty(mem, false);
1749 }
1750
1751 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1752                                 struct cgroup *cont)
1753 {
1754         mem_cgroup_free(mem_cgroup_from_cont(cont));
1755 }
1756
1757 static int mem_cgroup_populate(struct cgroup_subsys *ss,
1758                                 struct cgroup *cont)
1759 {
1760         int ret;
1761
1762         ret = cgroup_add_files(cont, ss, mem_cgroup_files,
1763                                 ARRAY_SIZE(mem_cgroup_files));
1764
1765         if (!ret)
1766                 ret = register_memsw_files(cont, ss);
1767         return ret;
1768 }
1769
1770 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1771                                 struct cgroup *cont,
1772                                 struct cgroup *old_cont,
1773                                 struct task_struct *p)
1774 {
1775         struct mm_struct *mm;
1776         struct mem_cgroup *mem, *old_mem;
1777
1778         mm = get_task_mm(p);
1779         if (mm == NULL)
1780                 return;
1781
1782         mem = mem_cgroup_from_cont(cont);
1783         old_mem = mem_cgroup_from_cont(old_cont);
1784
1785         /*
1786          * Only thread group leaders are allowed to migrate, the mm_struct is
1787          * in effect owned by the leader
1788          */
1789         if (!thread_group_leader(p))
1790                 goto out;
1791
1792 out:
1793         mmput(mm);
1794 }
1795
1796 struct cgroup_subsys mem_cgroup_subsys = {
1797         .name = "memory",
1798         .subsys_id = mem_cgroup_subsys_id,
1799         .create = mem_cgroup_create,
1800         .pre_destroy = mem_cgroup_pre_destroy,
1801         .destroy = mem_cgroup_destroy,
1802         .populate = mem_cgroup_populate,
1803         .attach = mem_cgroup_move_task,
1804         .early_init = 0,
1805 };
1806
1807 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1808
1809 static int __init disable_swap_account(char *s)
1810 {
1811         really_do_swap_account = 0;
1812         return 1;
1813 }
1814 __setup("noswapaccount", disable_swap_account);
1815 #endif