memcg: mem+swap controller Kconfig
[safe/jmp/linux-2.6] / mm / memcontrol.c
index 6f8b5b3..59dd8c1 100644 (file)
@@ -21,6 +21,7 @@
 #include <linux/memcontrol.h>
 #include <linux/cgroup.h>
 #include <linux/mm.h>
+#include <linux/pagemap.h>
 #include <linux/smp.h>
 #include <linux/page-flags.h>
 #include <linux/backing-dev.h>
 #include <linux/seq_file.h>
 #include <linux/vmalloc.h>
 #include <linux/mm_inline.h>
+#include <linux/page_cgroup.h>
 
 #include <asm/uaccess.h>
 
 struct cgroup_subsys mem_cgroup_subsys __read_mostly;
-static struct kmem_cache *page_cgroup_cache __read_mostly;
 #define MEM_CGROUP_RECLAIM_RETRIES     5
 
+#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
+/* Turned on only when memory cgroup is enabled && really_do_swap_account = 0 */
+int do_swap_account __read_mostly;
+static int really_do_swap_account __initdata = 1; /* for remember boot option*/
+#else
+#define do_swap_account                (0)
+#endif
+
+
 /*
  * Statistics for memory cgroup.
  */
@@ -60,17 +70,16 @@ struct mem_cgroup_stat_cpu {
 } ____cacheline_aligned_in_smp;
 
 struct mem_cgroup_stat {
-       struct mem_cgroup_stat_cpu cpustat[NR_CPUS];
+       struct mem_cgroup_stat_cpu cpustat[0];
 };
 
 /*
  * For accounting under irq disable, no need for increment preempt count.
  */
-static void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat *stat,
+static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
                enum mem_cgroup_stat_index idx, int val)
 {
-       int cpu = smp_processor_id();
-       stat->cpustat[cpu].count[idx] += val;
+       stat->count[idx] += val;
 }
 
 static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
@@ -130,78 +139,58 @@ struct mem_cgroup {
 
        int     prev_priority;  /* for recording reclaim priority */
        /*
-        * statistics.
+        * statistics. This must be placed at the end of memcg.
         */
        struct mem_cgroup_stat stat;
 };
-static struct mem_cgroup init_mem_cgroup;
-
-/*
- * We use the lower bit of the page->page_cgroup pointer as a bit spin
- * lock.  We need to ensure that page->page_cgroup is at least two
- * byte aligned (based on comments from Nick Piggin).  But since
- * bit_spin_lock doesn't actually set that lock bit in a non-debug
- * uniprocessor kernel, we should avoid setting it here too.
- */
-#define PAGE_CGROUP_LOCK_BIT   0x0
-#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
-#define PAGE_CGROUP_LOCK       (1 << PAGE_CGROUP_LOCK_BIT)
-#else
-#define PAGE_CGROUP_LOCK       0x0
-#endif
-
-/*
- * A page_cgroup page is associated with every page descriptor. The
- * page_cgroup helps us identify information about the cgroup
- */
-struct page_cgroup {
-       struct list_head lru;           /* per cgroup LRU list */
-       struct page *page;
-       struct mem_cgroup *mem_cgroup;
-       int flags;
-};
-#define PAGE_CGROUP_FLAG_CACHE    (0x1)        /* charged as cache */
-#define PAGE_CGROUP_FLAG_ACTIVE    (0x2)       /* page is active in this cgroup */
-#define PAGE_CGROUP_FLAG_FILE     (0x4)        /* page is file system backed */
-#define PAGE_CGROUP_FLAG_UNEVICTABLE (0x8)     /* page is unevictableable */
-
-static int page_cgroup_nid(struct page_cgroup *pc)
-{
-       return page_to_nid(pc->page);
-}
-
-static enum zone_type page_cgroup_zid(struct page_cgroup *pc)
-{
-       return page_zonenum(pc->page);
-}
 
 enum charge_type {
        MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
        MEM_CGROUP_CHARGE_TYPE_MAPPED,
-       MEM_CGROUP_CHARGE_TYPE_FORCE,   /* used by force_empty */
        MEM_CGROUP_CHARGE_TYPE_SHMEM,   /* used by page migration of shmem */
+       MEM_CGROUP_CHARGE_TYPE_FORCE,   /* used by force_empty */
+       MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
+       NR_CHARGE_TYPE,
+};
+
+/* only for here (for easy reading.) */
+#define PCGF_CACHE     (1UL << PCG_CACHE)
+#define PCGF_USED      (1UL << PCG_USED)
+#define PCGF_ACTIVE    (1UL << PCG_ACTIVE)
+#define PCGF_LOCK      (1UL << PCG_LOCK)
+#define PCGF_FILE      (1UL << PCG_FILE)
+static const unsigned long
+pcg_default_flags[NR_CHARGE_TYPE] = {
+       PCGF_CACHE | PCGF_FILE | PCGF_USED | PCGF_LOCK, /* File Cache */
+       PCGF_ACTIVE | PCGF_USED | PCGF_LOCK, /* Anon */
+       PCGF_ACTIVE | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
+       0, /* FORCE */
 };
 
 /*
  * Always modified under lru lock. Then, not necessary to preempt_disable()
  */
-static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, int flags,
-                                       bool charge)
+static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
+                                        struct page_cgroup *pc,
+                                        bool charge)
 {
        int val = (charge)? 1 : -1;
        struct mem_cgroup_stat *stat = &mem->stat;
+       struct mem_cgroup_stat_cpu *cpustat;
 
        VM_BUG_ON(!irqs_disabled());
-       if (flags & PAGE_CGROUP_FLAG_CACHE)
-               __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_CACHE, val);
+
+       cpustat = &stat->cpustat[smp_processor_id()];
+       if (PageCgroupCache(pc))
+               __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
        else
-               __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_RSS, val);
+               __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
 
        if (charge)
-               __mem_cgroup_stat_add_safe(stat,
+               __mem_cgroup_stat_add_safe(cpustat,
                                MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
        else
-               __mem_cgroup_stat_add_safe(stat,
+               __mem_cgroup_stat_add_safe(cpustat,
                                MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
 }
 
@@ -257,83 +246,55 @@ struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
                                struct mem_cgroup, css);
 }
 
-static inline int page_cgroup_locked(struct page *page)
-{
-       return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
-}
-
-static void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
-{
-       VM_BUG_ON(!page_cgroup_locked(page));
-       page->page_cgroup = ((unsigned long)pc | PAGE_CGROUP_LOCK);
-}
-
-struct page_cgroup *page_get_page_cgroup(struct page *page)
-{
-       return (struct page_cgroup *) (page->page_cgroup & ~PAGE_CGROUP_LOCK);
-}
-
-static void lock_page_cgroup(struct page *page)
-{
-       bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
-}
-
-static int try_lock_page_cgroup(struct page *page)
-{
-       return bit_spin_trylock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
-}
-
-static void unlock_page_cgroup(struct page *page)
-{
-       bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
-}
-
 static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
                        struct page_cgroup *pc)
 {
        int lru = LRU_BASE;
 
-       if (pc->flags & PAGE_CGROUP_FLAG_UNEVICTABLE)
+       if (PageCgroupUnevictable(pc))
                lru = LRU_UNEVICTABLE;
        else {
-               if (pc->flags & PAGE_CGROUP_FLAG_ACTIVE)
+               if (PageCgroupActive(pc))
                        lru += LRU_ACTIVE;
-               if (pc->flags & PAGE_CGROUP_FLAG_FILE)
+               if (PageCgroupFile(pc))
                        lru += LRU_FILE;
        }
 
        MEM_CGROUP_ZSTAT(mz, lru) -= 1;
 
-       mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, false);
+       mem_cgroup_charge_statistics(pc->mem_cgroup, pc, false);
        list_del(&pc->lru);
 }
 
 static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
-                               struct page_cgroup *pc)
+                               struct page_cgroup *pc, bool hot)
 {
        int lru = LRU_BASE;
 
-       if (pc->flags & PAGE_CGROUP_FLAG_UNEVICTABLE)
+       if (PageCgroupUnevictable(pc))
                lru = LRU_UNEVICTABLE;
        else {
-               if (pc->flags & PAGE_CGROUP_FLAG_ACTIVE)
+               if (PageCgroupActive(pc))
                        lru += LRU_ACTIVE;
-               if (pc->flags & PAGE_CGROUP_FLAG_FILE)
+               if (PageCgroupFile(pc))
                        lru += LRU_FILE;
        }
 
        MEM_CGROUP_ZSTAT(mz, lru) += 1;
-       list_add(&pc->lru, &mz->lists[lru]);
+       if (hot)
+               list_add(&pc->lru, &mz->lists[lru]);
+       else
+               list_add_tail(&pc->lru, &mz->lists[lru]);
 
-       mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, true);
+       mem_cgroup_charge_statistics(pc->mem_cgroup, pc, true);
 }
 
 static void __mem_cgroup_move_lists(struct page_cgroup *pc, enum lru_list lru)
 {
        struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
-       int active    = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
-       int file      = pc->flags & PAGE_CGROUP_FLAG_FILE;
-       int unevictable = pc->flags & PAGE_CGROUP_FLAG_UNEVICTABLE;
+       int active    = PageCgroupActive(pc);
+       int file      = PageCgroupFile(pc);
+       int unevictable = PageCgroupUnevictable(pc);
        enum lru_list from = unevictable ? LRU_UNEVICTABLE :
                                (LRU_FILE * !!file + !!active);
 
@@ -341,16 +302,20 @@ static void __mem_cgroup_move_lists(struct page_cgroup *pc, enum lru_list lru)
                return;
 
        MEM_CGROUP_ZSTAT(mz, from) -= 1;
-
+       /*
+        * However this is done under mz->lru_lock, another flags, which
+        * are not related to LRU, will be modified from out-of-lock.
+        * We have to use atomic set/clear flags.
+        */
        if (is_unevictable_lru(lru)) {
-               pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE;
-               pc->flags |= PAGE_CGROUP_FLAG_UNEVICTABLE;
+               ClearPageCgroupActive(pc);
+               SetPageCgroupUnevictable(pc);
        } else {
                if (is_active_lru(lru))
-                       pc->flags |= PAGE_CGROUP_FLAG_ACTIVE;
+                       SetPageCgroupActive(pc);
                else
-                       pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE;
-               pc->flags &= ~PAGE_CGROUP_FLAG_UNEVICTABLE;
+                       ClearPageCgroupActive(pc);
+               ClearPageCgroupUnevictable(pc);
        }
 
        MEM_CGROUP_ZSTAT(mz, lru) += 1;
@@ -386,17 +351,16 @@ void mem_cgroup_move_lists(struct page *page, enum lru_list lru)
         * safely get to page_cgroup without it, so just try_lock it:
         * mem_cgroup_isolate_pages allows for page left on wrong list.
         */
-       if (!try_lock_page_cgroup(page))
+       pc = lookup_page_cgroup(page);
+       if (!trylock_page_cgroup(pc))
                return;
-
-       pc = page_get_page_cgroup(page);
-       if (pc) {
+       if (pc && PageCgroupUsed(pc)) {
                mz = page_cgroup_zoneinfo(pc);
                spin_lock_irqsave(&mz->lru_lock, flags);
                __mem_cgroup_move_lists(pc, lru);
                spin_unlock_irqrestore(&mz->lru_lock, flags);
        }
-       unlock_page_cgroup(page);
+       unlock_page_cgroup(pc);
 }
 
 /*
@@ -483,6 +447,8 @@ unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
        list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
                if (scan >= nr_to_scan)
                        break;
+               if (unlikely(!PageCgroupUsed(pc)))
+                       continue;
                page = pc->page;
 
                if (unlikely(!PageLRU(page)))
@@ -515,52 +481,42 @@ unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
 }
 
 /*
- * Charge the memory controller for page usage.
- * Return
- * 0 if the charge was successful
- * < 0 if the cgroup is over its limit
+ * Unlike exported interface, "oom" parameter is added. if oom==true,
+ * oom-killer can be invoked.
  */
-static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
-                               gfp_t gfp_mask, enum charge_type ctype,
-                               struct mem_cgroup *memcg)
+static int __mem_cgroup_try_charge(struct mm_struct *mm,
+                       gfp_t gfp_mask, struct mem_cgroup **memcg, bool oom)
 {
        struct mem_cgroup *mem;
-       struct page_cgroup *pc;
-       unsigned long flags;
-       unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
-       struct mem_cgroup_per_zone *mz;
-
-       pc = kmem_cache_alloc(page_cgroup_cache, gfp_mask);
-       if (unlikely(pc == NULL))
-               goto err;
-
+       int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
        /*
         * We always charge the cgroup the mm_struct belongs to.
         * The mm_struct's mem_cgroup changes on task migration if the
         * thread group leader migrates. It's possible that mm is not
         * set, if so charge the init_mm (happens for pagecache usage).
         */
-       if (likely(!memcg)) {
+       if (likely(!*memcg)) {
                rcu_read_lock();
                mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
                if (unlikely(!mem)) {
                        rcu_read_unlock();
-                       kmem_cache_free(page_cgroup_cache, pc);
                        return 0;
                }
                /*
                 * For every charge from the cgroup, increment reference count
                 */
                css_get(&mem->css);
+               *memcg = mem;
                rcu_read_unlock();
        } else {
-               mem = memcg;
-               css_get(&memcg->css);
+               mem = *memcg;
+               css_get(&mem->css);
        }
 
-       while (res_counter_charge(&mem->res, PAGE_SIZE)) {
+
+       while (unlikely(res_counter_charge(&mem->res, PAGE_SIZE))) {
                if (!(gfp_mask & __GFP_WAIT))
-                       goto out;
+                       goto nomem;
 
                if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
                        continue;
@@ -576,58 +532,208 @@ static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
                        continue;
 
                if (!nr_retries--) {
-                       mem_cgroup_out_of_memory(mem, gfp_mask);
-                       goto out;
+                       if (oom)
+                               mem_cgroup_out_of_memory(mem, gfp_mask);
+                       goto nomem;
                }
        }
+       return 0;
+nomem:
+       css_put(&mem->css);
+       return -ENOMEM;
+}
+
+/**
+ * mem_cgroup_try_charge - get charge of PAGE_SIZE.
+ * @mm: an mm_struct which is charged against. (when *memcg is NULL)
+ * @gfp_mask: gfp_mask for reclaim.
+ * @memcg: a pointer to memory cgroup which is charged against.
+ *
+ * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated
+ * memory cgroup from @mm is got and stored in *memcg.
+ *
+ * Returns 0 if success. -ENOMEM at failure.
+ * This call can invoke OOM-Killer.
+ */
 
+int mem_cgroup_try_charge(struct mm_struct *mm,
+                         gfp_t mask, struct mem_cgroup **memcg)
+{
+       return __mem_cgroup_try_charge(mm, mask, memcg, true);
+}
+
+/*
+ * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be
+ * USED state. If already USED, uncharge and return.
+ */
+
+static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
+                                    struct page_cgroup *pc,
+                                    enum charge_type ctype)
+{
+       struct mem_cgroup_per_zone *mz;
+       unsigned long flags;
+
+       /* try_charge() can return NULL to *memcg, taking care of it. */
+       if (!mem)
+               return;
+
+       lock_page_cgroup(pc);
+       if (unlikely(PageCgroupUsed(pc))) {
+               unlock_page_cgroup(pc);
+               res_counter_uncharge(&mem->res, PAGE_SIZE);
+               css_put(&mem->css);
+               return;
+       }
        pc->mem_cgroup = mem;
-       pc->page = page;
        /*
         * If a page is accounted as a page cache, insert to inactive list.
         * If anon, insert to active list.
         */
-       if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE) {
-               pc->flags = PAGE_CGROUP_FLAG_CACHE;
-               if (page_is_file_cache(page))
-                       pc->flags |= PAGE_CGROUP_FLAG_FILE;
-               else
-                       pc->flags |= PAGE_CGROUP_FLAG_ACTIVE;
-       } else if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
-               pc->flags = PAGE_CGROUP_FLAG_ACTIVE;
-       else /* MEM_CGROUP_CHARGE_TYPE_SHMEM */
-               pc->flags = PAGE_CGROUP_FLAG_CACHE | PAGE_CGROUP_FLAG_ACTIVE;
-
-       lock_page_cgroup(page);
-       if (unlikely(page_get_page_cgroup(page))) {
-               unlock_page_cgroup(page);
-               res_counter_uncharge(&mem->res, PAGE_SIZE);
-               css_put(&mem->css);
-               kmem_cache_free(page_cgroup_cache, pc);
-               goto done;
-       }
-       page_assign_page_cgroup(page, pc);
+       pc->flags = pcg_default_flags[ctype];
 
        mz = page_cgroup_zoneinfo(pc);
+
        spin_lock_irqsave(&mz->lru_lock, flags);
-       __mem_cgroup_add_list(mz, pc);
+       __mem_cgroup_add_list(mz, pc, true);
        spin_unlock_irqrestore(&mz->lru_lock, flags);
+       unlock_page_cgroup(pc);
+}
 
-       unlock_page_cgroup(page);
-done:
-       return 0;
+/**
+ * mem_cgroup_move_account - move account of the page
+ * @pc:        page_cgroup of the page.
+ * @from: mem_cgroup which the page is moved from.
+ * @to:        mem_cgroup which the page is moved to. @from != @to.
+ *
+ * The caller must confirm following.
+ * 1. disable irq.
+ * 2. lru_lock of old mem_cgroup(@from) should be held.
+ *
+ * returns 0 at success,
+ * returns -EBUSY when lock is busy or "pc" is unstable.
+ *
+ * This function does "uncharge" from old cgroup but doesn't do "charge" to
+ * new cgroup. It should be done by a caller.
+ */
+
+static int mem_cgroup_move_account(struct page_cgroup *pc,
+       struct mem_cgroup *from, struct mem_cgroup *to)
+{
+       struct mem_cgroup_per_zone *from_mz, *to_mz;
+       int nid, zid;
+       int ret = -EBUSY;
+
+       VM_BUG_ON(!irqs_disabled());
+       VM_BUG_ON(from == to);
+
+       nid = page_cgroup_nid(pc);
+       zid = page_cgroup_zid(pc);
+       from_mz =  mem_cgroup_zoneinfo(from, nid, zid);
+       to_mz =  mem_cgroup_zoneinfo(to, nid, zid);
+
+
+       if (!trylock_page_cgroup(pc))
+               return ret;
+
+       if (!PageCgroupUsed(pc))
+               goto out;
+
+       if (pc->mem_cgroup != from)
+               goto out;
+
+       if (spin_trylock(&to_mz->lru_lock)) {
+               __mem_cgroup_remove_list(from_mz, pc);
+               css_put(&from->css);
+               res_counter_uncharge(&from->res, PAGE_SIZE);
+               pc->mem_cgroup = to;
+               css_get(&to->css);
+               __mem_cgroup_add_list(to_mz, pc, false);
+               ret = 0;
+               spin_unlock(&to_mz->lru_lock);
+       }
 out:
-       css_put(&mem->css);
-       kmem_cache_free(page_cgroup_cache, pc);
-err:
-       return -ENOMEM;
+       unlock_page_cgroup(pc);
+       return ret;
 }
 
-int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask)
+/*
+ * move charges to its parent.
+ */
+
+static int mem_cgroup_move_parent(struct page_cgroup *pc,
+                                 struct mem_cgroup *child,
+                                 gfp_t gfp_mask)
 {
-       if (mem_cgroup_subsys.disabled)
+       struct cgroup *cg = child->css.cgroup;
+       struct cgroup *pcg = cg->parent;
+       struct mem_cgroup *parent;
+       struct mem_cgroup_per_zone *mz;
+       unsigned long flags;
+       int ret;
+
+       /* Is ROOT ? */
+       if (!pcg)
+               return -EINVAL;
+
+       parent = mem_cgroup_from_cont(pcg);
+
+       ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
+       if (ret)
+               return ret;
+
+       mz = mem_cgroup_zoneinfo(child,
+                       page_cgroup_nid(pc), page_cgroup_zid(pc));
+
+       spin_lock_irqsave(&mz->lru_lock, flags);
+       ret = mem_cgroup_move_account(pc, child, parent);
+       spin_unlock_irqrestore(&mz->lru_lock, flags);
+
+       /* drop extra refcnt */
+       css_put(&parent->css);
+       /* uncharge if move fails */
+       if (ret)
+               res_counter_uncharge(&parent->res, PAGE_SIZE);
+
+       return ret;
+}
+
+/*
+ * Charge the memory controller for page usage.
+ * Return
+ * 0 if the charge was successful
+ * < 0 if the cgroup is over its limit
+ */
+static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
+                               gfp_t gfp_mask, enum charge_type ctype,
+                               struct mem_cgroup *memcg)
+{
+       struct mem_cgroup *mem;
+       struct page_cgroup *pc;
+       int ret;
+
+       pc = lookup_page_cgroup(page);
+       /* can happen at boot */
+       if (unlikely(!pc))
                return 0;
+       prefetchw(pc);
+
+       mem = memcg;
+       ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
+       if (ret)
+               return ret;
 
+       __mem_cgroup_commit_charge(mem, pc, ctype);
+       return 0;
+}
+
+int mem_cgroup_newpage_charge(struct page *page,
+                             struct mm_struct *mm, gfp_t gfp_mask)
+{
+       if (mem_cgroup_subsys.disabled)
+               return 0;
+       if (PageCompound(page))
+               return 0;
        /*
         * If already mapped, we don't have to account.
         * If page cache, page->mapping has address_space.
@@ -648,7 +754,8 @@ int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
 {
        if (mem_cgroup_subsys.disabled)
                return 0;
-
+       if (PageCompound(page))
+               return 0;
        /*
         * Corner case handling. This is called from add_to_page_cache()
         * in usual. But some FS (shmem) precharges this page before calling it
@@ -661,24 +768,79 @@ int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
        if (!(gfp_mask & __GFP_WAIT)) {
                struct page_cgroup *pc;
 
-               lock_page_cgroup(page);
-               pc = page_get_page_cgroup(page);
-               if (pc) {
-                       VM_BUG_ON(pc->page != page);
-                       VM_BUG_ON(!pc->mem_cgroup);
-                       unlock_page_cgroup(page);
+
+               pc = lookup_page_cgroup(page);
+               if (!pc)
+                       return 0;
+               lock_page_cgroup(pc);
+               if (PageCgroupUsed(pc)) {
+                       unlock_page_cgroup(pc);
                        return 0;
                }
-               unlock_page_cgroup(page);
+               unlock_page_cgroup(pc);
        }
 
        if (unlikely(!mm))
                mm = &init_mm;
 
-       return mem_cgroup_charge_common(page, mm, gfp_mask,
+       if (page_is_file_cache(page))
+               return mem_cgroup_charge_common(page, mm, gfp_mask,
                                MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
+       else
+               return mem_cgroup_charge_common(page, mm, gfp_mask,
+                               MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
 }
 
+#ifdef CONFIG_SWAP
+int mem_cgroup_cache_charge_swapin(struct page *page,
+                       struct mm_struct *mm, gfp_t mask, bool locked)
+{
+       int ret = 0;
+
+       if (mem_cgroup_subsys.disabled)
+               return 0;
+       if (unlikely(!mm))
+               mm = &init_mm;
+       if (!locked)
+               lock_page(page);
+       /*
+        * If not locked, the page can be dropped from SwapCache until
+        * we reach here.
+        */
+       if (PageSwapCache(page)) {
+               ret = mem_cgroup_charge_common(page, mm, mask,
+                               MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
+       }
+       if (!locked)
+               unlock_page(page);
+
+       return ret;
+}
+#endif
+
+void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
+{
+       struct page_cgroup *pc;
+
+       if (mem_cgroup_subsys.disabled)
+               return;
+       if (!ptr)
+               return;
+       pc = lookup_page_cgroup(page);
+       __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED);
+}
+
+void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
+{
+       if (mem_cgroup_subsys.disabled)
+               return;
+       if (!mem)
+               return;
+       res_counter_uncharge(&mem->res, PAGE_SIZE);
+       css_put(&mem->css);
+}
+
+
 /*
  * uncharge if !page_mapped(page)
  */
@@ -693,41 +855,63 @@ __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
        if (mem_cgroup_subsys.disabled)
                return;
 
+       if (PageSwapCache(page))
+               return;
+
        /*
         * Check if our page_cgroup is valid
         */
-       lock_page_cgroup(page);
-       pc = page_get_page_cgroup(page);
-       if (unlikely(!pc))
-               goto unlock;
+       pc = lookup_page_cgroup(page);
+       if (unlikely(!pc || !PageCgroupUsed(pc)))
+               return;
 
-       VM_BUG_ON(pc->page != page);
+       lock_page_cgroup(pc);
 
-       if ((ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
-           && ((pc->flags & PAGE_CGROUP_FLAG_CACHE)
-               || page_mapped(page)))
-               goto unlock;
+       if (!PageCgroupUsed(pc))
+               goto unlock_out;
+
+       switch (ctype) {
+       case MEM_CGROUP_CHARGE_TYPE_MAPPED:
+               if (page_mapped(page))
+                       goto unlock_out;
+               break;
+       case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
+               if (!PageAnon(page)) {  /* Shared memory */
+                       if (page->mapping && !page_is_file_cache(page))
+                               goto unlock_out;
+               } else if (page_mapped(page)) /* Anon */
+                               goto unlock_out;
+               break;
+       default:
+               break;
+       }
+
+       ClearPageCgroupUsed(pc);
+       mem = pc->mem_cgroup;
 
        mz = page_cgroup_zoneinfo(pc);
        spin_lock_irqsave(&mz->lru_lock, flags);
        __mem_cgroup_remove_list(mz, pc);
        spin_unlock_irqrestore(&mz->lru_lock, flags);
+       unlock_page_cgroup(pc);
 
-       page_assign_page_cgroup(page, NULL);
-       unlock_page_cgroup(page);
-
-       mem = pc->mem_cgroup;
        res_counter_uncharge(&mem->res, PAGE_SIZE);
        css_put(&mem->css);
 
-       kmem_cache_free(page_cgroup_cache, pc);
        return;
-unlock:
-       unlock_page_cgroup(page);
+
+unlock_out:
+       unlock_page_cgroup(pc);
+       return;
 }
 
 void mem_cgroup_uncharge_page(struct page *page)
 {
+       /* early check. */
+       if (page_mapped(page))
+               return;
+       if (page->mapping && !PageAnon(page))
+               return;
        __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
 }
 
@@ -738,59 +922,90 @@ void mem_cgroup_uncharge_cache_page(struct page *page)
        __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
 }
 
+void mem_cgroup_uncharge_swapcache(struct page *page)
+{
+       __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_SWAPOUT);
+}
+
 /*
- * Before starting migration, account against new page.
+ * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
+ * page belongs to.
  */
-int mem_cgroup_prepare_migration(struct page *page, struct page *newpage)
+int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
 {
        struct page_cgroup *pc;
        struct mem_cgroup *mem = NULL;
-       enum charge_type ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
        int ret = 0;
 
        if (mem_cgroup_subsys.disabled)
                return 0;
 
-       lock_page_cgroup(page);
-       pc = page_get_page_cgroup(page);
-       if (pc) {
+       pc = lookup_page_cgroup(page);
+       lock_page_cgroup(pc);
+       if (PageCgroupUsed(pc)) {
                mem = pc->mem_cgroup;
                css_get(&mem->css);
-               if (pc->flags & PAGE_CGROUP_FLAG_CACHE) {
-                       if (page_is_file_cache(page))
-                               ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
-                       else
-                               ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
-               }
        }
-       unlock_page_cgroup(page);
+       unlock_page_cgroup(pc);
+
        if (mem) {
-               ret = mem_cgroup_charge_common(newpage, NULL, GFP_KERNEL,
-                       ctype, mem);
+               ret = mem_cgroup_try_charge(NULL, GFP_HIGHUSER_MOVABLE, &mem);
                css_put(&mem->css);
        }
+       *ptr = mem;
        return ret;
 }
 
 /* remove redundant charge if migration failed*/
-void mem_cgroup_end_migration(struct page *newpage)
+void mem_cgroup_end_migration(struct mem_cgroup *mem,
+               struct page *oldpage, struct page *newpage)
 {
+       struct page *target, *unused;
+       struct page_cgroup *pc;
+       enum charge_type ctype;
+
+       if (!mem)
+               return;
+
+       /* at migration success, oldpage->mapping is NULL. */
+       if (oldpage->mapping) {
+               target = oldpage;
+               unused = NULL;
+       } else {
+               target = newpage;
+               unused = oldpage;
+       }
+
+       if (PageAnon(target))
+               ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
+       else if (page_is_file_cache(target))
+               ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
+       else
+               ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
+
+       /* unused page is not on radix-tree now. */
+       if (unused)
+               __mem_cgroup_uncharge_common(unused, ctype);
+
+       pc = lookup_page_cgroup(target);
        /*
-        * At success, page->mapping is not NULL.
-        * special rollback care is necessary when
-        * 1. at migration failure. (newpage->mapping is cleared in this case)
-        * 2. the newpage was moved but not remapped again because the task
-        *    exits and the newpage is obsolete. In this case, the new page
-        *    may be a swapcache. So, we just call mem_cgroup_uncharge_page()
-        *    always for avoiding mess. The  page_cgroup will be removed if
-        *    unnecessary. File cache pages is still on radix-tree. Don't
-        *    care it.
+        * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
+        * So, double-counting is effectively avoided.
+        */
+       __mem_cgroup_commit_charge(mem, pc, ctype);
+
+       /*
+        * Both of oldpage and newpage are still under lock_page().
+        * Then, we don't have to care about race in radix-tree.
+        * But we have to be careful that this page is unmapped or not.
+        *
+        * There is a case for !page_mapped(). At the start of
+        * migration, oldpage was mapped. But now, it's zapped.
+        * But we know *target* page is not freed/reused under us.
+        * mem_cgroup_uncharge_page() does all necessary checks.
         */
-       if (!newpage->mapping)
-               __mem_cgroup_uncharge_common(newpage,
-                                        MEM_CGROUP_CHARGE_TYPE_FORCE);
-       else if (PageAnon(newpage))
-               mem_cgroup_uncharge_page(newpage);
+       if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
+               mem_cgroup_uncharge_page(target);
 }
 
 /*
@@ -829,7 +1044,8 @@ int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
        return 0;
 }
 
-int mem_cgroup_resize_limit(struct mem_cgroup *memcg, unsigned long long val)
+static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
+                                  unsigned long long val)
 {
 
        int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
@@ -845,7 +1061,8 @@ int mem_cgroup_resize_limit(struct mem_cgroup *memcg, unsigned long long val)
                        ret = -EBUSY;
                        break;
                }
-               progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL);
+               progress = try_to_free_mem_cgroup_pages(memcg,
+                               GFP_HIGHUSER_MOVABLE);
                if (!progress)
                        retry_count--;
        }
@@ -857,77 +1074,146 @@ int mem_cgroup_resize_limit(struct mem_cgroup *memcg, unsigned long long val)
  * This routine traverse page_cgroup in given list and drop them all.
  * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
  */
-#define FORCE_UNCHARGE_BATCH   (128)
-static void mem_cgroup_force_empty_list(struct mem_cgroup *mem,
+static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
                            struct mem_cgroup_per_zone *mz,
                            enum lru_list lru)
 {
-       struct page_cgroup *pc;
-       struct page *page;
-       int count = FORCE_UNCHARGE_BATCH;
+       struct page_cgroup *pc, *busy;
        unsigned long flags;
+       unsigned long loop;
        struct list_head *list;
+       int ret = 0;
 
        list = &mz->lists[lru];
 
-       spin_lock_irqsave(&mz->lru_lock, flags);
-       while (!list_empty(list)) {
+       loop = MEM_CGROUP_ZSTAT(mz, lru);
+       /* give some margin against EBUSY etc...*/
+       loop += 256;
+       busy = NULL;
+       while (loop--) {
+               ret = 0;
+               spin_lock_irqsave(&mz->lru_lock, flags);
+               if (list_empty(list)) {
+                       spin_unlock_irqrestore(&mz->lru_lock, flags);
+                       break;
+               }
                pc = list_entry(list->prev, struct page_cgroup, lru);
-               page = pc->page;
-               get_page(page);
+               if (busy == pc) {
+                       list_move(&pc->lru, list);
+                       busy = 0;
+                       spin_unlock_irqrestore(&mz->lru_lock, flags);
+                       continue;
+               }
                spin_unlock_irqrestore(&mz->lru_lock, flags);
-               /*
-                * Check if this page is on LRU. !LRU page can be found
-                * if it's under page migration.
-                */
-               if (PageLRU(page)) {
-                       __mem_cgroup_uncharge_common(page,
-                                       MEM_CGROUP_CHARGE_TYPE_FORCE);
-                       put_page(page);
-                       if (--count <= 0) {
-                               count = FORCE_UNCHARGE_BATCH;
-                               cond_resched();
-                       }
-               } else
+
+               ret = mem_cgroup_move_parent(pc, mem, GFP_HIGHUSER_MOVABLE);
+               if (ret == -ENOMEM)
+                       break;
+
+               if (ret == -EBUSY || ret == -EINVAL) {
+                       /* found lock contention or "pc" is obsolete. */
+                       busy = pc;
                        cond_resched();
-               spin_lock_irqsave(&mz->lru_lock, flags);
+               } else
+                       busy = NULL;
        }
-       spin_unlock_irqrestore(&mz->lru_lock, flags);
+       if (!ret && !list_empty(list))
+               return -EBUSY;
+       return ret;
 }
 
 /*
  * make mem_cgroup's charge to be 0 if there is no task.
  * This enables deleting this mem_cgroup.
  */
-static int mem_cgroup_force_empty(struct mem_cgroup *mem)
+static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
 {
-       int ret = -EBUSY;
-       int node, zid;
+       int ret;
+       int node, zid, shrink;
+       int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
+       struct cgroup *cgrp = mem->css.cgroup;
 
        css_get(&mem->css);
-       /*
-        * page reclaim code (kswapd etc..) will move pages between
-        * active_list <-> inactive_list while we don't take a lock.
-        * So, we have to do loop here until all lists are empty.
-        */
+
+       shrink = 0;
+       /* should free all ? */
+       if (free_all)
+               goto try_to_free;
+move_account:
        while (mem->res.usage > 0) {
-               if (atomic_read(&mem->css.cgroup->count) > 0)
+               ret = -EBUSY;
+               if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
+                       goto out;
+               ret = -EINTR;
+               if (signal_pending(current))
                        goto out;
-               for_each_node_state(node, N_POSSIBLE)
-                       for (zid = 0; zid < MAX_NR_ZONES; zid++) {
+               /* This is for making all *used* pages to be on LRU. */
+               lru_add_drain_all();
+               ret = 0;
+               for_each_node_state(node, N_POSSIBLE) {
+                       for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
                                struct mem_cgroup_per_zone *mz;
                                enum lru_list l;
                                mz = mem_cgroup_zoneinfo(mem, node, zid);
-                               for_each_lru(l)
-                                       mem_cgroup_force_empty_list(mem, mz, l);
+                               for_each_lru(l) {
+                                       ret = mem_cgroup_force_empty_list(mem,
+                                                                 mz, l);
+                                       if (ret)
+                                               break;
+                               }
                        }
+                       if (ret)
+                               break;
+               }
+               /* it seems parent cgroup doesn't have enough mem */
+               if (ret == -ENOMEM)
+                       goto try_to_free;
+               cond_resched();
        }
        ret = 0;
 out:
        css_put(&mem->css);
        return ret;
+
+try_to_free:
+       /* returns EBUSY if there is a task or if we come here twice. */
+       if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
+               ret = -EBUSY;
+               goto out;
+       }
+       /* we call try-to-free pages for make this cgroup empty */
+       lru_add_drain_all();
+       /* try to free all pages in this cgroup */
+       shrink = 1;
+       while (nr_retries && mem->res.usage > 0) {
+               int progress;
+
+               if (signal_pending(current)) {
+                       ret = -EINTR;
+                       goto out;
+               }
+               progress = try_to_free_mem_cgroup_pages(mem,
+                                                 GFP_HIGHUSER_MOVABLE);
+               if (!progress) {
+                       nr_retries--;
+                       /* maybe some writeback is necessary */
+                       congestion_wait(WRITE, HZ/10);
+               }
+
+       }
+       /* try move_account...there may be some *locked* pages. */
+       if (mem->res.usage)
+               goto move_account;
+       ret = 0;
+       goto out;
+}
+
+int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
+{
+       return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
 }
 
+
 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
 {
        return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
@@ -974,11 +1260,6 @@ static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
        return 0;
 }
 
-static int mem_force_empty_write(struct cgroup *cont, unsigned int event)
-{
-       return mem_cgroup_force_empty(mem_cgroup_from_cont(cont));
-}
-
 static const struct mem_cgroup_stat_desc {
        const char *msg;
        u64 unit;
@@ -1030,6 +1311,7 @@ static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
        return 0;
 }
 
+
 static struct cftype mem_cgroup_files[] = {
        {
                .name = "usage_in_bytes",
@@ -1055,13 +1337,13 @@ static struct cftype mem_cgroup_files[] = {
                .read_u64 = mem_cgroup_read,
        },
        {
-               .name = "force_empty",
-               .trigger = mem_force_empty_write,
-       },
-       {
                .name = "stat",
                .read_map = mem_control_stat_show,
        },
+       {
+               .name = "force_empty",
+               .trigger = mem_cgroup_force_empty_write,
+       },
 };
 
 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
@@ -1101,56 +1383,72 @@ static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
        kfree(mem->info.nodeinfo[node]);
 }
 
+static int mem_cgroup_size(void)
+{
+       int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
+       return sizeof(struct mem_cgroup) + cpustat_size;
+}
+
 static struct mem_cgroup *mem_cgroup_alloc(void)
 {
        struct mem_cgroup *mem;
+       int size = mem_cgroup_size();
 
-       if (sizeof(*mem) < PAGE_SIZE)
-               mem = kmalloc(sizeof(*mem), GFP_KERNEL);
+       if (size < PAGE_SIZE)
+               mem = kmalloc(size, GFP_KERNEL);
        else
-               mem = vmalloc(sizeof(*mem));
+               mem = vmalloc(size);
 
        if (mem)
-               memset(mem, 0, sizeof(*mem));
+               memset(mem, 0, size);
        return mem;
 }
 
 static void mem_cgroup_free(struct mem_cgroup *mem)
 {
-       if (sizeof(*mem) < PAGE_SIZE)
+       if (mem_cgroup_size() < PAGE_SIZE)
                kfree(mem);
        else
                vfree(mem);
 }
 
 
+#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
+static void __init enable_swap_cgroup(void)
+{
+       if (!mem_cgroup_subsys.disabled && really_do_swap_account)
+               do_swap_account = 1;
+}
+#else
+static void __init enable_swap_cgroup(void)
+{
+}
+#endif
+
 static struct cgroup_subsys_state *
 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
 {
        struct mem_cgroup *mem;
        int node;
 
-       if (unlikely((cont->parent) == NULL)) {
-               mem = &init_mem_cgroup;
-               page_cgroup_cache = KMEM_CACHE(page_cgroup, SLAB_PANIC);
-       } else {
-               mem = mem_cgroup_alloc();
-               if (!mem)
-                       return ERR_PTR(-ENOMEM);
-       }
+       mem = mem_cgroup_alloc();
+       if (!mem)
+               return ERR_PTR(-ENOMEM);
 
        res_counter_init(&mem->res);
 
        for_each_node_state(node, N_POSSIBLE)
                if (alloc_mem_cgroup_per_zone_info(mem, node))
                        goto free_out;
+       /* root ? */
+       if (cont->parent == NULL)
+               enable_swap_cgroup();
 
        return &mem->css;
 free_out:
        for_each_node_state(node, N_POSSIBLE)
                free_mem_cgroup_per_zone_info(mem, node);
-       if (cont->parent != NULL)
-               mem_cgroup_free(mem);
+       mem_cgroup_free(mem);
        return ERR_PTR(-ENOMEM);
 }
 
@@ -1158,7 +1456,7 @@ static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
                                        struct cgroup *cont)
 {
        struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
-       mem_cgroup_force_empty(mem);
+       mem_cgroup_force_empty(mem, false);
 }
 
 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
@@ -1216,3 +1514,13 @@ struct cgroup_subsys mem_cgroup_subsys = {
        .attach = mem_cgroup_move_task,
        .early_init = 0,
 };
+
+#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
+
+static int __init disable_swap_account(char *s)
+{
+       really_do_swap_account = 0;
+       return 1;
+}
+__setup("noswapaccount", disable_swap_account);
+#endif