Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1 | /* memcontrol.c - Memory Controller |
| 2 | * |
| 3 | * Copyright IBM Corporation, 2007 |
| 4 | * Author Balbir Singh <balbir@linux.vnet.ibm.com> |
| 5 | * |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 6 | * Copyright 2007 OpenVZ SWsoft Inc |
| 7 | * Author: Pavel Emelianov <xemul@openvz.org> |
| 8 | * |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 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> |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 23 | #include <linux/mm.h> |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 24 | #include <linux/pagemap.h> |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 25 | #include <linux/smp.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 26 | #include <linux/page-flags.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 27 | #include <linux/backing-dev.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 28 | #include <linux/bit_spinlock.h> |
| 29 | #include <linux/rcupdate.h> |
Balbir Singh | b6ac57d | 2008-04-29 01:00:19 -0700 | [diff] [blame] | 30 | #include <linux/slab.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 31 | #include <linux/swap.h> |
| 32 | #include <linux/spinlock.h> |
| 33 | #include <linux/fs.h> |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 34 | #include <linux/seq_file.h> |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 35 | #include <linux/vmalloc.h> |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 36 | #include <linux/mm_inline.h> |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 37 | #include <linux/page_cgroup.h> |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 38 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 39 | #include <asm/uaccess.h> |
| 40 | |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 41 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 42 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 43 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame^] | 44 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 45 | /* Turned on only when memory cgroup is enabled && really_do_swap_account = 0 */ |
| 46 | int do_swap_account __read_mostly; |
| 47 | static int really_do_swap_account __initdata = 1; /* for remember boot option*/ |
| 48 | #else |
| 49 | #define do_swap_account (0) |
| 50 | #endif |
| 51 | |
| 52 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 53 | /* |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 54 | * Statistics for memory cgroup. |
| 55 | */ |
| 56 | enum mem_cgroup_stat_index { |
| 57 | /* |
| 58 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. |
| 59 | */ |
| 60 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ |
| 61 | MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */ |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 62 | MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */ |
| 63 | MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */ |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 64 | |
| 65 | MEM_CGROUP_STAT_NSTATS, |
| 66 | }; |
| 67 | |
| 68 | struct mem_cgroup_stat_cpu { |
| 69 | s64 count[MEM_CGROUP_STAT_NSTATS]; |
| 70 | } ____cacheline_aligned_in_smp; |
| 71 | |
| 72 | struct mem_cgroup_stat { |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 73 | struct mem_cgroup_stat_cpu cpustat[0]; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /* |
| 77 | * For accounting under irq disable, no need for increment preempt count. |
| 78 | */ |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 79 | static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat, |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 80 | enum mem_cgroup_stat_index idx, int val) |
| 81 | { |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 82 | stat->count[idx] += val; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat, |
| 86 | enum mem_cgroup_stat_index idx) |
| 87 | { |
| 88 | int cpu; |
| 89 | s64 ret = 0; |
| 90 | for_each_possible_cpu(cpu) |
| 91 | ret += stat->cpustat[cpu].count[idx]; |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | /* |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 96 | * per-zone information in memory controller. |
| 97 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 98 | struct mem_cgroup_per_zone { |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 99 | /* |
| 100 | * spin_lock to protect the per cgroup LRU |
| 101 | */ |
| 102 | spinlock_t lru_lock; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 103 | struct list_head lists[NR_LRU_LISTS]; |
| 104 | unsigned long count[NR_LRU_LISTS]; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 105 | }; |
| 106 | /* Macro for accessing counter */ |
| 107 | #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)]) |
| 108 | |
| 109 | struct mem_cgroup_per_node { |
| 110 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; |
| 111 | }; |
| 112 | |
| 113 | struct mem_cgroup_lru_info { |
| 114 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; |
| 115 | }; |
| 116 | |
| 117 | /* |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 118 | * The memory controller data structure. The memory controller controls both |
| 119 | * page cache and RSS per cgroup. We would eventually like to provide |
| 120 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, |
| 121 | * to help the administrator determine what knobs to tune. |
| 122 | * |
| 123 | * TODO: Add a water mark for the memory controller. Reclaim will begin when |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 124 | * we hit the water mark. May be even add a low water mark, such that |
| 125 | * no reclaim occurs from a cgroup at it's low water mark, this is |
| 126 | * a feature that will be implemented much later in the future. |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 127 | */ |
| 128 | struct mem_cgroup { |
| 129 | struct cgroup_subsys_state css; |
| 130 | /* |
| 131 | * the counter to account for memory usage |
| 132 | */ |
| 133 | struct res_counter res; |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 134 | /* |
| 135 | * Per cgroup active and inactive list, similar to the |
| 136 | * per zone LRU lists. |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 137 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 138 | struct mem_cgroup_lru_info info; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 139 | |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 140 | int prev_priority; /* for recording reclaim priority */ |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 141 | /* |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 142 | * statistics. This must be placed at the end of memcg. |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 143 | */ |
| 144 | struct mem_cgroup_stat stat; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 145 | }; |
| 146 | |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 147 | enum charge_type { |
| 148 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, |
| 149 | MEM_CGROUP_CHARGE_TYPE_MAPPED, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 150 | MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 151 | MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */ |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 152 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 153 | NR_CHARGE_TYPE, |
| 154 | }; |
| 155 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 156 | /* only for here (for easy reading.) */ |
| 157 | #define PCGF_CACHE (1UL << PCG_CACHE) |
| 158 | #define PCGF_USED (1UL << PCG_USED) |
| 159 | #define PCGF_ACTIVE (1UL << PCG_ACTIVE) |
| 160 | #define PCGF_LOCK (1UL << PCG_LOCK) |
| 161 | #define PCGF_FILE (1UL << PCG_FILE) |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 162 | static const unsigned long |
| 163 | pcg_default_flags[NR_CHARGE_TYPE] = { |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 164 | PCGF_CACHE | PCGF_FILE | PCGF_USED | PCGF_LOCK, /* File Cache */ |
| 165 | PCGF_ACTIVE | PCGF_USED | PCGF_LOCK, /* Anon */ |
| 166 | PCGF_ACTIVE | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */ |
| 167 | 0, /* FORCE */ |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 168 | }; |
| 169 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 170 | /* |
| 171 | * Always modified under lru lock. Then, not necessary to preempt_disable() |
| 172 | */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 173 | static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, |
| 174 | struct page_cgroup *pc, |
| 175 | bool charge) |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 176 | { |
| 177 | int val = (charge)? 1 : -1; |
| 178 | struct mem_cgroup_stat *stat = &mem->stat; |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 179 | struct mem_cgroup_stat_cpu *cpustat; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 180 | |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 181 | VM_BUG_ON(!irqs_disabled()); |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 182 | |
| 183 | cpustat = &stat->cpustat[smp_processor_id()]; |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 184 | if (PageCgroupCache(pc)) |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 185 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val); |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 186 | else |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 187 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val); |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 188 | |
| 189 | if (charge) |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 190 | __mem_cgroup_stat_add_safe(cpustat, |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 191 | MEM_CGROUP_STAT_PGPGIN_COUNT, 1); |
| 192 | else |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 193 | __mem_cgroup_stat_add_safe(cpustat, |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 194 | MEM_CGROUP_STAT_PGPGOUT_COUNT, 1); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 195 | } |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 196 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 197 | static struct mem_cgroup_per_zone * |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 198 | mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid) |
| 199 | { |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 200 | return &mem->info.nodeinfo[nid]->zoneinfo[zid]; |
| 201 | } |
| 202 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 203 | static struct mem_cgroup_per_zone * |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 204 | page_cgroup_zoneinfo(struct page_cgroup *pc) |
| 205 | { |
| 206 | struct mem_cgroup *mem = pc->mem_cgroup; |
| 207 | int nid = page_cgroup_nid(pc); |
| 208 | int zid = page_cgroup_zid(pc); |
| 209 | |
| 210 | return mem_cgroup_zoneinfo(mem, nid, zid); |
| 211 | } |
| 212 | |
| 213 | static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem, |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 214 | enum lru_list idx) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 215 | { |
| 216 | int nid, zid; |
| 217 | struct mem_cgroup_per_zone *mz; |
| 218 | u64 total = 0; |
| 219 | |
| 220 | for_each_online_node(nid) |
| 221 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 222 | mz = mem_cgroup_zoneinfo(mem, nid, zid); |
| 223 | total += MEM_CGROUP_ZSTAT(mz, idx); |
| 224 | } |
| 225 | return total; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 228 | static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 229 | { |
| 230 | return container_of(cgroup_subsys_state(cont, |
| 231 | mem_cgroup_subsys_id), struct mem_cgroup, |
| 232 | css); |
| 233 | } |
| 234 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 235 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 236 | { |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 237 | /* |
| 238 | * mm_update_next_owner() may clear mm->owner to NULL |
| 239 | * if it races with swapoff, page migration, etc. |
| 240 | * So this can be called with p == NULL. |
| 241 | */ |
| 242 | if (unlikely(!p)) |
| 243 | return NULL; |
| 244 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 245 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
| 246 | struct mem_cgroup, css); |
| 247 | } |
| 248 | |
KAMEZAWA Hiroyuki | 3eae90c | 2008-04-29 01:00:22 -0700 | [diff] [blame] | 249 | static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz, |
| 250 | struct page_cgroup *pc) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 251 | { |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 252 | int lru = LRU_BASE; |
| 253 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 254 | if (PageCgroupUnevictable(pc)) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 255 | lru = LRU_UNEVICTABLE; |
| 256 | else { |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 257 | if (PageCgroupActive(pc)) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 258 | lru += LRU_ACTIVE; |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 259 | if (PageCgroupFile(pc)) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 260 | lru += LRU_FILE; |
| 261 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 262 | |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 263 | MEM_CGROUP_ZSTAT(mz, lru) -= 1; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 264 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 265 | mem_cgroup_charge_statistics(pc->mem_cgroup, pc, false); |
KAMEZAWA Hiroyuki | 508b7be | 2008-07-25 01:47:09 -0700 | [diff] [blame] | 266 | list_del(&pc->lru); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 267 | } |
| 268 | |
KAMEZAWA Hiroyuki | 3eae90c | 2008-04-29 01:00:22 -0700 | [diff] [blame] | 269 | static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz, |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 270 | struct page_cgroup *pc, bool hot) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 271 | { |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 272 | int lru = LRU_BASE; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 273 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 274 | if (PageCgroupUnevictable(pc)) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 275 | lru = LRU_UNEVICTABLE; |
| 276 | else { |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 277 | if (PageCgroupActive(pc)) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 278 | lru += LRU_ACTIVE; |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 279 | if (PageCgroupFile(pc)) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 280 | lru += LRU_FILE; |
| 281 | } |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 282 | |
| 283 | MEM_CGROUP_ZSTAT(mz, lru) += 1; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 284 | if (hot) |
| 285 | list_add(&pc->lru, &mz->lists[lru]); |
| 286 | else |
| 287 | list_add_tail(&pc->lru, &mz->lists[lru]); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 288 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 289 | mem_cgroup_charge_statistics(pc->mem_cgroup, pc, true); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 292 | static void __mem_cgroup_move_lists(struct page_cgroup *pc, enum lru_list lru) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 293 | { |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 294 | struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc); |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 295 | int active = PageCgroupActive(pc); |
| 296 | int file = PageCgroupFile(pc); |
| 297 | int unevictable = PageCgroupUnevictable(pc); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 298 | enum lru_list from = unevictable ? LRU_UNEVICTABLE : |
| 299 | (LRU_FILE * !!file + !!active); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 300 | |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 301 | if (lru == from) |
| 302 | return; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 303 | |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 304 | MEM_CGROUP_ZSTAT(mz, from) -= 1; |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 305 | /* |
| 306 | * However this is done under mz->lru_lock, another flags, which |
| 307 | * are not related to LRU, will be modified from out-of-lock. |
| 308 | * We have to use atomic set/clear flags. |
| 309 | */ |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 310 | if (is_unevictable_lru(lru)) { |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 311 | ClearPageCgroupActive(pc); |
| 312 | SetPageCgroupUnevictable(pc); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 313 | } else { |
| 314 | if (is_active_lru(lru)) |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 315 | SetPageCgroupActive(pc); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 316 | else |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 317 | ClearPageCgroupActive(pc); |
| 318 | ClearPageCgroupUnevictable(pc); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 319 | } |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 320 | |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 321 | MEM_CGROUP_ZSTAT(mz, lru) += 1; |
| 322 | list_move(&pc->lru, &mz->lists[lru]); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 323 | } |
| 324 | |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 325 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
| 326 | { |
| 327 | int ret; |
| 328 | |
| 329 | task_lock(task); |
Hugh Dickins | bd845e3 | 2008-03-04 14:29:01 -0800 | [diff] [blame] | 330 | ret = task->mm && mm_match_cgroup(task->mm, mem); |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 331 | task_unlock(task); |
| 332 | return ret; |
| 333 | } |
| 334 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 335 | /* |
| 336 | * This routine assumes that the appropriate zone's lru lock is already held |
| 337 | */ |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 338 | void mem_cgroup_move_lists(struct page *page, enum lru_list lru) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 339 | { |
Hugh Dickins | 427d541 | 2008-03-04 14:29:03 -0800 | [diff] [blame] | 340 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 341 | struct mem_cgroup_per_zone *mz; |
| 342 | unsigned long flags; |
| 343 | |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 344 | if (mem_cgroup_subsys.disabled) |
| 345 | return; |
| 346 | |
Hugh Dickins | 2680eed | 2008-03-04 14:29:13 -0800 | [diff] [blame] | 347 | /* |
| 348 | * We cannot lock_page_cgroup while holding zone's lru_lock, |
| 349 | * because other holders of lock_page_cgroup can be interrupted |
| 350 | * with an attempt to rotate_reclaimable_page. But we cannot |
| 351 | * safely get to page_cgroup without it, so just try_lock it: |
| 352 | * mem_cgroup_isolate_pages allows for page left on wrong list. |
| 353 | */ |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 354 | pc = lookup_page_cgroup(page); |
| 355 | if (!trylock_page_cgroup(pc)) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 356 | return; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 357 | if (pc && PageCgroupUsed(pc)) { |
Hugh Dickins | 2680eed | 2008-03-04 14:29:13 -0800 | [diff] [blame] | 358 | mz = page_cgroup_zoneinfo(pc); |
Hugh Dickins | 2680eed | 2008-03-04 14:29:13 -0800 | [diff] [blame] | 359 | spin_lock_irqsave(&mz->lru_lock, flags); |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 360 | __mem_cgroup_move_lists(pc, lru); |
Hugh Dickins | 2680eed | 2008-03-04 14:29:13 -0800 | [diff] [blame] | 361 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
Hirokazu Takahashi | 9b3c0a0 | 2008-03-04 14:29:15 -0800 | [diff] [blame] | 362 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 363 | unlock_page_cgroup(pc); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 364 | } |
| 365 | |
KAMEZAWA Hiroyuki | 58ae83d | 2008-02-07 00:14:32 -0800 | [diff] [blame] | 366 | /* |
| 367 | * Calculate mapped_ratio under memory controller. This will be used in |
| 368 | * vmscan.c for deteremining we have to reclaim mapped pages. |
| 369 | */ |
| 370 | int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem) |
| 371 | { |
| 372 | long total, rss; |
| 373 | |
| 374 | /* |
| 375 | * usage is recorded in bytes. But, here, we assume the number of |
| 376 | * physical pages can be represented by "long" on any arch. |
| 377 | */ |
| 378 | total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L; |
| 379 | rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS); |
| 380 | return (int)((rss * 100L) / total); |
| 381 | } |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 382 | |
KAMEZAWA Hiroyuki | 5932f36 | 2008-02-07 00:14:33 -0800 | [diff] [blame] | 383 | /* |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 384 | * prev_priority control...this will be used in memory reclaim path. |
| 385 | */ |
| 386 | int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem) |
| 387 | { |
| 388 | return mem->prev_priority; |
| 389 | } |
| 390 | |
| 391 | void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority) |
| 392 | { |
| 393 | if (priority < mem->prev_priority) |
| 394 | mem->prev_priority = priority; |
| 395 | } |
| 396 | |
| 397 | void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority) |
| 398 | { |
| 399 | mem->prev_priority = priority; |
| 400 | } |
| 401 | |
KAMEZAWA Hiroyuki | cc38108 | 2008-02-07 00:14:35 -0800 | [diff] [blame] | 402 | /* |
| 403 | * Calculate # of pages to be scanned in this priority/zone. |
| 404 | * See also vmscan.c |
| 405 | * |
| 406 | * priority starts from "DEF_PRIORITY" and decremented in each loop. |
| 407 | * (see include/linux/mmzone.h) |
| 408 | */ |
| 409 | |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 410 | long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone, |
| 411 | int priority, enum lru_list lru) |
KAMEZAWA Hiroyuki | cc38108 | 2008-02-07 00:14:35 -0800 | [diff] [blame] | 412 | { |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 413 | long nr_pages; |
KAMEZAWA Hiroyuki | cc38108 | 2008-02-07 00:14:35 -0800 | [diff] [blame] | 414 | int nid = zone->zone_pgdat->node_id; |
| 415 | int zid = zone_idx(zone); |
| 416 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid); |
| 417 | |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 418 | nr_pages = MEM_CGROUP_ZSTAT(mz, lru); |
KAMEZAWA Hiroyuki | cc38108 | 2008-02-07 00:14:35 -0800 | [diff] [blame] | 419 | |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 420 | return (nr_pages >> priority); |
KAMEZAWA Hiroyuki | cc38108 | 2008-02-07 00:14:35 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 423 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
| 424 | struct list_head *dst, |
| 425 | unsigned long *scanned, int order, |
| 426 | int mode, struct zone *z, |
| 427 | struct mem_cgroup *mem_cont, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 428 | int active, int file) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 429 | { |
| 430 | unsigned long nr_taken = 0; |
| 431 | struct page *page; |
| 432 | unsigned long scan; |
| 433 | LIST_HEAD(pc_list); |
| 434 | struct list_head *src; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 435 | struct page_cgroup *pc, *tmp; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 436 | int nid = z->zone_pgdat->node_id; |
| 437 | int zid = zone_idx(z); |
| 438 | struct mem_cgroup_per_zone *mz; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 439 | int lru = LRU_FILE * !!file + !!active; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 440 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 441 | BUG_ON(!mem_cont); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 442 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 443 | src = &mz->lists[lru]; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 444 | |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 445 | spin_lock(&mz->lru_lock); |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 446 | scan = 0; |
| 447 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 448 | if (scan >= nr_to_scan) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 449 | break; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 450 | if (unlikely(!PageCgroupUsed(pc))) |
| 451 | continue; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 452 | page = pc->page; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 453 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 454 | if (unlikely(!PageLRU(page))) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 455 | continue; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 456 | |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 457 | /* |
| 458 | * TODO: play better with lumpy reclaim, grabbing anything. |
| 459 | */ |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 460 | if (PageUnevictable(page) || |
| 461 | (PageActive(page) && !active) || |
| 462 | (!PageActive(page) && active)) { |
| 463 | __mem_cgroup_move_lists(pc, page_lru(page)); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 464 | continue; |
| 465 | } |
| 466 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 467 | scan++; |
| 468 | list_move(&pc->lru, &pc_list); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 469 | |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 470 | if (__isolate_lru_page(page, mode, file) == 0) { |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 471 | list_move(&page->lru, dst); |
| 472 | nr_taken++; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | list_splice(&pc_list, src); |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 477 | spin_unlock(&mz->lru_lock); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 478 | |
| 479 | *scanned = scan; |
| 480 | return nr_taken; |
| 481 | } |
| 482 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 483 | /* |
| 484 | * Unlike exported interface, "oom" parameter is added. if oom==true, |
| 485 | * oom-killer can be invoked. |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 486 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 487 | static int __mem_cgroup_try_charge(struct mm_struct *mm, |
| 488 | gfp_t gfp_mask, struct mem_cgroup **memcg, bool oom) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 489 | { |
| 490 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 491 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 492 | /* |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 493 | * We always charge the cgroup the mm_struct belongs to. |
| 494 | * The mm_struct's mem_cgroup changes on task migration if the |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 495 | * thread group leader migrates. It's possible that mm is not |
| 496 | * set, if so charge the init_mm (happens for pagecache usage). |
| 497 | */ |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 498 | if (likely(!*memcg)) { |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 499 | rcu_read_lock(); |
| 500 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 501 | if (unlikely(!mem)) { |
| 502 | rcu_read_unlock(); |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 503 | return 0; |
| 504 | } |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 505 | /* |
| 506 | * For every charge from the cgroup, increment reference count |
| 507 | */ |
| 508 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 509 | *memcg = mem; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 510 | rcu_read_unlock(); |
| 511 | } else { |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 512 | mem = *memcg; |
| 513 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 514 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 515 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 516 | |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 517 | while (unlikely(res_counter_charge(&mem->res, PAGE_SIZE))) { |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 518 | if (!(gfp_mask & __GFP_WAIT)) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 519 | goto nomem; |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 520 | |
| 521 | if (try_to_free_mem_cgroup_pages(mem, gfp_mask)) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 522 | continue; |
| 523 | |
| 524 | /* |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 525 | * try_to_free_mem_cgroup_pages() might not give us a full |
| 526 | * picture of reclaim. Some pages are reclaimed and might be |
| 527 | * moved to swap cache or just unmapped from the cgroup. |
| 528 | * Check the limit again to see if the reclaim reduced the |
| 529 | * current usage of the cgroup before giving up |
| 530 | */ |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 531 | if (res_counter_check_under_limit(&mem->res)) |
| 532 | continue; |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 533 | |
| 534 | if (!nr_retries--) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 535 | if (oom) |
| 536 | mem_cgroup_out_of_memory(mem, gfp_mask); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 537 | goto nomem; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 538 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 539 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 540 | return 0; |
| 541 | nomem: |
| 542 | css_put(&mem->css); |
| 543 | return -ENOMEM; |
| 544 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 545 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 546 | /** |
| 547 | * mem_cgroup_try_charge - get charge of PAGE_SIZE. |
| 548 | * @mm: an mm_struct which is charged against. (when *memcg is NULL) |
| 549 | * @gfp_mask: gfp_mask for reclaim. |
| 550 | * @memcg: a pointer to memory cgroup which is charged against. |
| 551 | * |
| 552 | * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated |
| 553 | * memory cgroup from @mm is got and stored in *memcg. |
| 554 | * |
| 555 | * Returns 0 if success. -ENOMEM at failure. |
| 556 | * This call can invoke OOM-Killer. |
| 557 | */ |
| 558 | |
| 559 | int mem_cgroup_try_charge(struct mm_struct *mm, |
| 560 | gfp_t mask, struct mem_cgroup **memcg) |
| 561 | { |
| 562 | return __mem_cgroup_try_charge(mm, mask, memcg, true); |
| 563 | } |
| 564 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 565 | /* |
| 566 | * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be |
| 567 | * USED state. If already USED, uncharge and return. |
| 568 | */ |
| 569 | |
| 570 | static void __mem_cgroup_commit_charge(struct mem_cgroup *mem, |
| 571 | struct page_cgroup *pc, |
| 572 | enum charge_type ctype) |
| 573 | { |
| 574 | struct mem_cgroup_per_zone *mz; |
| 575 | unsigned long flags; |
| 576 | |
| 577 | /* try_charge() can return NULL to *memcg, taking care of it. */ |
| 578 | if (!mem) |
| 579 | return; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 580 | |
| 581 | lock_page_cgroup(pc); |
| 582 | if (unlikely(PageCgroupUsed(pc))) { |
| 583 | unlock_page_cgroup(pc); |
| 584 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
| 585 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 586 | return; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 587 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 588 | pc->mem_cgroup = mem; |
KAMEZAWA Hiroyuki | 508b7be | 2008-07-25 01:47:09 -0700 | [diff] [blame] | 589 | /* |
| 590 | * If a page is accounted as a page cache, insert to inactive list. |
| 591 | * If anon, insert to active list. |
| 592 | */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 593 | pc->flags = pcg_default_flags[ctype]; |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 594 | |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 595 | mz = page_cgroup_zoneinfo(pc); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 596 | |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 597 | spin_lock_irqsave(&mz->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 598 | __mem_cgroup_add_list(mz, pc, true); |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 599 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 600 | unlock_page_cgroup(pc); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 601 | } |
| 602 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 603 | /** |
| 604 | * mem_cgroup_move_account - move account of the page |
| 605 | * @pc: page_cgroup of the page. |
| 606 | * @from: mem_cgroup which the page is moved from. |
| 607 | * @to: mem_cgroup which the page is moved to. @from != @to. |
| 608 | * |
| 609 | * The caller must confirm following. |
| 610 | * 1. disable irq. |
| 611 | * 2. lru_lock of old mem_cgroup(@from) should be held. |
| 612 | * |
| 613 | * returns 0 at success, |
| 614 | * returns -EBUSY when lock is busy or "pc" is unstable. |
| 615 | * |
| 616 | * This function does "uncharge" from old cgroup but doesn't do "charge" to |
| 617 | * new cgroup. It should be done by a caller. |
| 618 | */ |
| 619 | |
| 620 | static int mem_cgroup_move_account(struct page_cgroup *pc, |
| 621 | struct mem_cgroup *from, struct mem_cgroup *to) |
| 622 | { |
| 623 | struct mem_cgroup_per_zone *from_mz, *to_mz; |
| 624 | int nid, zid; |
| 625 | int ret = -EBUSY; |
| 626 | |
| 627 | VM_BUG_ON(!irqs_disabled()); |
| 628 | VM_BUG_ON(from == to); |
| 629 | |
| 630 | nid = page_cgroup_nid(pc); |
| 631 | zid = page_cgroup_zid(pc); |
| 632 | from_mz = mem_cgroup_zoneinfo(from, nid, zid); |
| 633 | to_mz = mem_cgroup_zoneinfo(to, nid, zid); |
| 634 | |
| 635 | |
| 636 | if (!trylock_page_cgroup(pc)) |
| 637 | return ret; |
| 638 | |
| 639 | if (!PageCgroupUsed(pc)) |
| 640 | goto out; |
| 641 | |
| 642 | if (pc->mem_cgroup != from) |
| 643 | goto out; |
| 644 | |
| 645 | if (spin_trylock(&to_mz->lru_lock)) { |
| 646 | __mem_cgroup_remove_list(from_mz, pc); |
| 647 | css_put(&from->css); |
| 648 | res_counter_uncharge(&from->res, PAGE_SIZE); |
| 649 | pc->mem_cgroup = to; |
| 650 | css_get(&to->css); |
| 651 | __mem_cgroup_add_list(to_mz, pc, false); |
| 652 | ret = 0; |
| 653 | spin_unlock(&to_mz->lru_lock); |
| 654 | } |
| 655 | out: |
| 656 | unlock_page_cgroup(pc); |
| 657 | return ret; |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | * move charges to its parent. |
| 662 | */ |
| 663 | |
| 664 | static int mem_cgroup_move_parent(struct page_cgroup *pc, |
| 665 | struct mem_cgroup *child, |
| 666 | gfp_t gfp_mask) |
| 667 | { |
| 668 | struct cgroup *cg = child->css.cgroup; |
| 669 | struct cgroup *pcg = cg->parent; |
| 670 | struct mem_cgroup *parent; |
| 671 | struct mem_cgroup_per_zone *mz; |
| 672 | unsigned long flags; |
| 673 | int ret; |
| 674 | |
| 675 | /* Is ROOT ? */ |
| 676 | if (!pcg) |
| 677 | return -EINVAL; |
| 678 | |
| 679 | parent = mem_cgroup_from_cont(pcg); |
| 680 | |
| 681 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false); |
| 682 | if (ret) |
| 683 | return ret; |
| 684 | |
| 685 | mz = mem_cgroup_zoneinfo(child, |
| 686 | page_cgroup_nid(pc), page_cgroup_zid(pc)); |
| 687 | |
| 688 | spin_lock_irqsave(&mz->lru_lock, flags); |
| 689 | ret = mem_cgroup_move_account(pc, child, parent); |
| 690 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
| 691 | |
| 692 | /* drop extra refcnt */ |
| 693 | css_put(&parent->css); |
| 694 | /* uncharge if move fails */ |
| 695 | if (ret) |
| 696 | res_counter_uncharge(&parent->res, PAGE_SIZE); |
| 697 | |
| 698 | return ret; |
| 699 | } |
| 700 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 701 | /* |
| 702 | * Charge the memory controller for page usage. |
| 703 | * Return |
| 704 | * 0 if the charge was successful |
| 705 | * < 0 if the cgroup is over its limit |
| 706 | */ |
| 707 | static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm, |
| 708 | gfp_t gfp_mask, enum charge_type ctype, |
| 709 | struct mem_cgroup *memcg) |
| 710 | { |
| 711 | struct mem_cgroup *mem; |
| 712 | struct page_cgroup *pc; |
| 713 | int ret; |
| 714 | |
| 715 | pc = lookup_page_cgroup(page); |
| 716 | /* can happen at boot */ |
| 717 | if (unlikely(!pc)) |
| 718 | return 0; |
| 719 | prefetchw(pc); |
| 720 | |
| 721 | mem = memcg; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 722 | ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 723 | if (ret) |
| 724 | return ret; |
| 725 | |
| 726 | __mem_cgroup_commit_charge(mem, pc, ctype); |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | int mem_cgroup_newpage_charge(struct page *page, |
| 731 | struct mm_struct *mm, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 732 | { |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 733 | if (mem_cgroup_subsys.disabled) |
| 734 | return 0; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 735 | if (PageCompound(page)) |
| 736 | return 0; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 737 | /* |
| 738 | * If already mapped, we don't have to account. |
| 739 | * If page cache, page->mapping has address_space. |
| 740 | * But page->mapping may have out-of-use anon_vma pointer, |
| 741 | * detecit it by PageAnon() check. newly-mapped-anon's page->mapping |
| 742 | * is NULL. |
| 743 | */ |
| 744 | if (page_mapped(page) || (page->mapping && !PageAnon(page))) |
| 745 | return 0; |
| 746 | if (unlikely(!mm)) |
| 747 | mm = &init_mm; |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 748 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 749 | MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL); |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 750 | } |
| 751 | |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 752 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
| 753 | gfp_t gfp_mask) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 754 | { |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 755 | if (mem_cgroup_subsys.disabled) |
| 756 | return 0; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 757 | if (PageCompound(page)) |
| 758 | return 0; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 759 | /* |
| 760 | * Corner case handling. This is called from add_to_page_cache() |
| 761 | * in usual. But some FS (shmem) precharges this page before calling it |
| 762 | * and call add_to_page_cache() with GFP_NOWAIT. |
| 763 | * |
| 764 | * For GFP_NOWAIT case, the page may be pre-charged before calling |
| 765 | * add_to_page_cache(). (See shmem.c) check it here and avoid to call |
| 766 | * charge twice. (It works but has to pay a bit larger cost.) |
| 767 | */ |
| 768 | if (!(gfp_mask & __GFP_WAIT)) { |
| 769 | struct page_cgroup *pc; |
| 770 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 771 | |
| 772 | pc = lookup_page_cgroup(page); |
| 773 | if (!pc) |
| 774 | return 0; |
| 775 | lock_page_cgroup(pc); |
| 776 | if (PageCgroupUsed(pc)) { |
| 777 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 778 | return 0; |
| 779 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 780 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 781 | } |
| 782 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 783 | if (unlikely(!mm)) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 784 | mm = &init_mm; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 785 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 786 | if (page_is_file_cache(page)) |
| 787 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 788 | MEM_CGROUP_CHARGE_TYPE_CACHE, NULL); |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 789 | else |
| 790 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
| 791 | MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 792 | } |
| 793 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 794 | #ifdef CONFIG_SWAP |
| 795 | int mem_cgroup_cache_charge_swapin(struct page *page, |
| 796 | struct mm_struct *mm, gfp_t mask, bool locked) |
| 797 | { |
| 798 | int ret = 0; |
| 799 | |
| 800 | if (mem_cgroup_subsys.disabled) |
| 801 | return 0; |
| 802 | if (unlikely(!mm)) |
| 803 | mm = &init_mm; |
| 804 | if (!locked) |
| 805 | lock_page(page); |
| 806 | /* |
| 807 | * If not locked, the page can be dropped from SwapCache until |
| 808 | * we reach here. |
| 809 | */ |
| 810 | if (PageSwapCache(page)) { |
| 811 | ret = mem_cgroup_charge_common(page, mm, mask, |
| 812 | MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL); |
| 813 | } |
| 814 | if (!locked) |
| 815 | unlock_page(page); |
| 816 | |
| 817 | return ret; |
| 818 | } |
| 819 | #endif |
| 820 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 821 | void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr) |
| 822 | { |
| 823 | struct page_cgroup *pc; |
| 824 | |
| 825 | if (mem_cgroup_subsys.disabled) |
| 826 | return; |
| 827 | if (!ptr) |
| 828 | return; |
| 829 | pc = lookup_page_cgroup(page); |
| 830 | __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 831 | } |
| 832 | |
| 833 | void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem) |
| 834 | { |
| 835 | if (mem_cgroup_subsys.disabled) |
| 836 | return; |
| 837 | if (!mem) |
| 838 | return; |
| 839 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
| 840 | css_put(&mem->css); |
| 841 | } |
| 842 | |
| 843 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 844 | /* |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 845 | * uncharge if !page_mapped(page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 846 | */ |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 847 | static void |
| 848 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 849 | { |
Hugh Dickins | 8289546 | 2008-03-04 14:29:08 -0800 | [diff] [blame] | 850 | struct page_cgroup *pc; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 851 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 852 | struct mem_cgroup_per_zone *mz; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 853 | unsigned long flags; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 854 | |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 855 | if (mem_cgroup_subsys.disabled) |
| 856 | return; |
| 857 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 858 | if (PageSwapCache(page)) |
| 859 | return; |
| 860 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 861 | /* |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 862 | * Check if our page_cgroup is valid |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 863 | */ |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 864 | pc = lookup_page_cgroup(page); |
| 865 | if (unlikely(!pc || !PageCgroupUsed(pc))) |
| 866 | return; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 867 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 868 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 869 | |
| 870 | if (!PageCgroupUsed(pc)) |
| 871 | goto unlock_out; |
| 872 | |
| 873 | switch (ctype) { |
| 874 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
| 875 | if (page_mapped(page)) |
| 876 | goto unlock_out; |
| 877 | break; |
| 878 | case MEM_CGROUP_CHARGE_TYPE_SWAPOUT: |
| 879 | if (!PageAnon(page)) { /* Shared memory */ |
| 880 | if (page->mapping && !page_is_file_cache(page)) |
| 881 | goto unlock_out; |
| 882 | } else if (page_mapped(page)) /* Anon */ |
| 883 | goto unlock_out; |
| 884 | break; |
| 885 | default: |
| 886 | break; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 887 | } |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 888 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 889 | ClearPageCgroupUsed(pc); |
| 890 | mem = pc->mem_cgroup; |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 891 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 892 | mz = page_cgroup_zoneinfo(pc); |
| 893 | spin_lock_irqsave(&mz->lru_lock, flags); |
| 894 | __mem_cgroup_remove_list(mz, pc); |
| 895 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 896 | unlock_page_cgroup(pc); |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 897 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 898 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
| 899 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 900 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 901 | return; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 902 | |
| 903 | unlock_out: |
| 904 | unlock_page_cgroup(pc); |
| 905 | return; |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 906 | } |
| 907 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 908 | void mem_cgroup_uncharge_page(struct page *page) |
| 909 | { |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 910 | /* early check. */ |
| 911 | if (page_mapped(page)) |
| 912 | return; |
| 913 | if (page->mapping && !PageAnon(page)) |
| 914 | return; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 915 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 916 | } |
| 917 | |
| 918 | void mem_cgroup_uncharge_cache_page(struct page *page) |
| 919 | { |
| 920 | VM_BUG_ON(page_mapped(page)); |
KAMEZAWA Hiroyuki | b7abea9 | 2008-10-18 20:28:09 -0700 | [diff] [blame] | 921 | VM_BUG_ON(page->mapping); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 922 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 923 | } |
| 924 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 925 | void mem_cgroup_uncharge_swapcache(struct page *page) |
| 926 | { |
| 927 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_SWAPOUT); |
| 928 | } |
| 929 | |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 930 | /* |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 931 | * Before starting migration, account PAGE_SIZE to mem_cgroup that the old |
| 932 | * page belongs to. |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 933 | */ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 934 | int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr) |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 935 | { |
| 936 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 937 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 938 | int ret = 0; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 939 | |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 940 | if (mem_cgroup_subsys.disabled) |
| 941 | return 0; |
| 942 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 943 | pc = lookup_page_cgroup(page); |
| 944 | lock_page_cgroup(pc); |
| 945 | if (PageCgroupUsed(pc)) { |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 946 | mem = pc->mem_cgroup; |
| 947 | css_get(&mem->css); |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 948 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 949 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 950 | |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 951 | if (mem) { |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 952 | ret = mem_cgroup_try_charge(NULL, GFP_HIGHUSER_MOVABLE, &mem); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 953 | css_put(&mem->css); |
| 954 | } |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 955 | *ptr = mem; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 956 | return ret; |
| 957 | } |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 958 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 959 | /* remove redundant charge if migration failed*/ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 960 | void mem_cgroup_end_migration(struct mem_cgroup *mem, |
| 961 | struct page *oldpage, struct page *newpage) |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 962 | { |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 963 | struct page *target, *unused; |
| 964 | struct page_cgroup *pc; |
| 965 | enum charge_type ctype; |
| 966 | |
| 967 | if (!mem) |
| 968 | return; |
| 969 | |
| 970 | /* at migration success, oldpage->mapping is NULL. */ |
| 971 | if (oldpage->mapping) { |
| 972 | target = oldpage; |
| 973 | unused = NULL; |
| 974 | } else { |
| 975 | target = newpage; |
| 976 | unused = oldpage; |
| 977 | } |
| 978 | |
| 979 | if (PageAnon(target)) |
| 980 | ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED; |
| 981 | else if (page_is_file_cache(target)) |
| 982 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; |
| 983 | else |
| 984 | ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM; |
| 985 | |
| 986 | /* unused page is not on radix-tree now. */ |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 987 | if (unused) |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 988 | __mem_cgroup_uncharge_common(unused, ctype); |
| 989 | |
| 990 | pc = lookup_page_cgroup(target); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 991 | /* |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 992 | * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup. |
| 993 | * So, double-counting is effectively avoided. |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 994 | */ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 995 | __mem_cgroup_commit_charge(mem, pc, ctype); |
| 996 | |
| 997 | /* |
| 998 | * Both of oldpage and newpage are still under lock_page(). |
| 999 | * Then, we don't have to care about race in radix-tree. |
| 1000 | * But we have to be careful that this page is unmapped or not. |
| 1001 | * |
| 1002 | * There is a case for !page_mapped(). At the start of |
| 1003 | * migration, oldpage was mapped. But now, it's zapped. |
| 1004 | * But we know *target* page is not freed/reused under us. |
| 1005 | * mem_cgroup_uncharge_page() does all necessary checks. |
| 1006 | */ |
| 1007 | if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED) |
| 1008 | mem_cgroup_uncharge_page(target); |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 1009 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 1010 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1011 | /* |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1012 | * A call to try to shrink memory usage under specified resource controller. |
| 1013 | * This is typically used for page reclaiming for shmem for reducing side |
| 1014 | * effect of page allocation from shmem, which is used by some mem_cgroup. |
| 1015 | */ |
| 1016 | int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask) |
| 1017 | { |
| 1018 | struct mem_cgroup *mem; |
| 1019 | int progress = 0; |
| 1020 | int retry = MEM_CGROUP_RECLAIM_RETRIES; |
| 1021 | |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 1022 | if (mem_cgroup_subsys.disabled) |
| 1023 | return 0; |
Hugh Dickins | 9623e07 | 2008-08-12 15:08:41 -0700 | [diff] [blame] | 1024 | if (!mm) |
| 1025 | return 0; |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 1026 | |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1027 | rcu_read_lock(); |
| 1028 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 1029 | if (unlikely(!mem)) { |
| 1030 | rcu_read_unlock(); |
| 1031 | return 0; |
| 1032 | } |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1033 | css_get(&mem->css); |
| 1034 | rcu_read_unlock(); |
| 1035 | |
| 1036 | do { |
| 1037 | progress = try_to_free_mem_cgroup_pages(mem, gfp_mask); |
Daisuke Nishimura | a10cebf | 2008-09-22 13:57:52 -0700 | [diff] [blame] | 1038 | progress += res_counter_check_under_limit(&mem->res); |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1039 | } while (!progress && --retry); |
| 1040 | |
| 1041 | css_put(&mem->css); |
| 1042 | if (!retry) |
| 1043 | return -ENOMEM; |
| 1044 | return 0; |
| 1045 | } |
| 1046 | |
KOSAKI Motohiro | d38d2a7 | 2009-01-06 14:39:44 -0800 | [diff] [blame] | 1047 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
| 1048 | unsigned long long val) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1049 | { |
| 1050 | |
| 1051 | int retry_count = MEM_CGROUP_RECLAIM_RETRIES; |
| 1052 | int progress; |
| 1053 | int ret = 0; |
| 1054 | |
| 1055 | while (res_counter_set_limit(&memcg->res, val)) { |
| 1056 | if (signal_pending(current)) { |
| 1057 | ret = -EINTR; |
| 1058 | break; |
| 1059 | } |
| 1060 | if (!retry_count) { |
| 1061 | ret = -EBUSY; |
| 1062 | break; |
| 1063 | } |
KAMEZAWA Hiroyuki | bced052 | 2009-01-07 18:07:49 -0800 | [diff] [blame] | 1064 | progress = try_to_free_mem_cgroup_pages(memcg, |
| 1065 | GFP_HIGHUSER_MOVABLE); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1066 | if (!progress) |
| 1067 | retry_count--; |
| 1068 | } |
| 1069 | return ret; |
| 1070 | } |
| 1071 | |
| 1072 | |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1073 | /* |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1074 | * This routine traverse page_cgroup in given list and drop them all. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1075 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
| 1076 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1077 | static int mem_cgroup_force_empty_list(struct mem_cgroup *mem, |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 1078 | struct mem_cgroup_per_zone *mz, |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1079 | enum lru_list lru) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1080 | { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1081 | struct page_cgroup *pc, *busy; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1082 | unsigned long flags; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1083 | unsigned long loop; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 1084 | struct list_head *list; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1085 | int ret = 0; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 1086 | |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1087 | list = &mz->lists[lru]; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1088 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1089 | loop = MEM_CGROUP_ZSTAT(mz, lru); |
| 1090 | /* give some margin against EBUSY etc...*/ |
| 1091 | loop += 256; |
| 1092 | busy = NULL; |
| 1093 | while (loop--) { |
| 1094 | ret = 0; |
| 1095 | spin_lock_irqsave(&mz->lru_lock, flags); |
| 1096 | if (list_empty(list)) { |
| 1097 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1098 | break; |
| 1099 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1100 | pc = list_entry(list->prev, struct page_cgroup, lru); |
| 1101 | if (busy == pc) { |
| 1102 | list_move(&pc->lru, list); |
| 1103 | busy = 0; |
| 1104 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
| 1105 | continue; |
| 1106 | } |
| 1107 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
| 1108 | |
| 1109 | ret = mem_cgroup_move_parent(pc, mem, GFP_HIGHUSER_MOVABLE); |
| 1110 | if (ret == -ENOMEM) |
| 1111 | break; |
| 1112 | |
| 1113 | if (ret == -EBUSY || ret == -EINVAL) { |
| 1114 | /* found lock contention or "pc" is obsolete. */ |
| 1115 | busy = pc; |
| 1116 | cond_resched(); |
| 1117 | } else |
| 1118 | busy = NULL; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1119 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1120 | if (!ret && !list_empty(list)) |
| 1121 | return -EBUSY; |
| 1122 | return ret; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | /* |
| 1126 | * make mem_cgroup's charge to be 0 if there is no task. |
| 1127 | * This enables deleting this mem_cgroup. |
| 1128 | */ |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1129 | static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1130 | { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1131 | int ret; |
| 1132 | int node, zid, shrink; |
| 1133 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1134 | struct cgroup *cgrp = mem->css.cgroup; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 1135 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1136 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1137 | |
| 1138 | shrink = 0; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1139 | /* should free all ? */ |
| 1140 | if (free_all) |
| 1141 | goto try_to_free; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1142 | move_account: |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1143 | while (mem->res.usage > 0) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1144 | ret = -EBUSY; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1145 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children)) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1146 | goto out; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1147 | ret = -EINTR; |
| 1148 | if (signal_pending(current)) |
| 1149 | goto out; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1150 | /* This is for making all *used* pages to be on LRU. */ |
| 1151 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1152 | ret = 0; |
| 1153 | for_each_node_state(node, N_POSSIBLE) { |
| 1154 | for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) { |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1155 | struct mem_cgroup_per_zone *mz; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1156 | enum lru_list l; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1157 | mz = mem_cgroup_zoneinfo(mem, node, zid); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1158 | for_each_lru(l) { |
| 1159 | ret = mem_cgroup_force_empty_list(mem, |
| 1160 | mz, l); |
| 1161 | if (ret) |
| 1162 | break; |
| 1163 | } |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1164 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1165 | if (ret) |
| 1166 | break; |
| 1167 | } |
| 1168 | /* it seems parent cgroup doesn't have enough mem */ |
| 1169 | if (ret == -ENOMEM) |
| 1170 | goto try_to_free; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1171 | cond_resched(); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1172 | } |
| 1173 | ret = 0; |
| 1174 | out: |
| 1175 | css_put(&mem->css); |
| 1176 | return ret; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1177 | |
| 1178 | try_to_free: |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1179 | /* returns EBUSY if there is a task or if we come here twice. */ |
| 1180 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1181 | ret = -EBUSY; |
| 1182 | goto out; |
| 1183 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1184 | /* we call try-to-free pages for make this cgroup empty */ |
| 1185 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1186 | /* try to free all pages in this cgroup */ |
| 1187 | shrink = 1; |
| 1188 | while (nr_retries && mem->res.usage > 0) { |
| 1189 | int progress; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1190 | |
| 1191 | if (signal_pending(current)) { |
| 1192 | ret = -EINTR; |
| 1193 | goto out; |
| 1194 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1195 | progress = try_to_free_mem_cgroup_pages(mem, |
| 1196 | GFP_HIGHUSER_MOVABLE); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1197 | if (!progress) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1198 | nr_retries--; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1199 | /* maybe some writeback is necessary */ |
| 1200 | congestion_wait(WRITE, HZ/10); |
| 1201 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1202 | |
| 1203 | } |
| 1204 | /* try move_account...there may be some *locked* pages. */ |
| 1205 | if (mem->res.usage) |
| 1206 | goto move_account; |
| 1207 | ret = 0; |
| 1208 | goto out; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1209 | } |
| 1210 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1211 | int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event) |
| 1212 | { |
| 1213 | return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true); |
| 1214 | } |
| 1215 | |
| 1216 | |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 1217 | static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1218 | { |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 1219 | return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res, |
| 1220 | cft->private); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1221 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1222 | /* |
| 1223 | * The user of this function is... |
| 1224 | * RES_LIMIT. |
| 1225 | */ |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 1226 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
| 1227 | const char *buffer) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1228 | { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1229 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
| 1230 | unsigned long long val; |
| 1231 | int ret; |
| 1232 | |
| 1233 | switch (cft->private) { |
| 1234 | case RES_LIMIT: |
| 1235 | /* This function does all necessary parse...reuse it */ |
| 1236 | ret = res_counter_memparse_write_strategy(buffer, &val); |
| 1237 | if (!ret) |
| 1238 | ret = mem_cgroup_resize_limit(memcg, val); |
| 1239 | break; |
| 1240 | default: |
| 1241 | ret = -EINVAL; /* should be BUG() ? */ |
| 1242 | break; |
| 1243 | } |
| 1244 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1245 | } |
| 1246 | |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 1247 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 1248 | { |
| 1249 | struct mem_cgroup *mem; |
| 1250 | |
| 1251 | mem = mem_cgroup_from_cont(cont); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 1252 | switch (event) { |
| 1253 | case RES_MAX_USAGE: |
| 1254 | res_counter_reset_max(&mem->res); |
| 1255 | break; |
| 1256 | case RES_FAILCNT: |
| 1257 | res_counter_reset_failcnt(&mem->res); |
| 1258 | break; |
| 1259 | } |
Pavel Emelyanov | 85cc59d | 2008-04-29 01:00:20 -0700 | [diff] [blame] | 1260 | return 0; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 1263 | static const struct mem_cgroup_stat_desc { |
| 1264 | const char *msg; |
| 1265 | u64 unit; |
| 1266 | } mem_cgroup_stat_desc[] = { |
| 1267 | [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, }, |
| 1268 | [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, }, |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 1269 | [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, }, |
| 1270 | [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, }, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 1271 | }; |
| 1272 | |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 1273 | static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft, |
| 1274 | struct cgroup_map_cb *cb) |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 1275 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 1276 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
| 1277 | struct mem_cgroup_stat *stat = &mem_cont->stat; |
| 1278 | int i; |
| 1279 | |
| 1280 | for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) { |
| 1281 | s64 val; |
| 1282 | |
| 1283 | val = mem_cgroup_read_stat(stat, i); |
| 1284 | val *= mem_cgroup_stat_desc[i].unit; |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 1285 | cb->fill(cb, mem_cgroup_stat_desc[i].msg, val); |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 1286 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1287 | /* showing # of active pages */ |
| 1288 | { |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1289 | unsigned long active_anon, inactive_anon; |
| 1290 | unsigned long active_file, inactive_file; |
Lee Schermerhorn | 7b85412 | 2008-10-18 20:26:40 -0700 | [diff] [blame] | 1291 | unsigned long unevictable; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1292 | |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1293 | inactive_anon = mem_cgroup_get_all_zonestat(mem_cont, |
| 1294 | LRU_INACTIVE_ANON); |
| 1295 | active_anon = mem_cgroup_get_all_zonestat(mem_cont, |
| 1296 | LRU_ACTIVE_ANON); |
| 1297 | inactive_file = mem_cgroup_get_all_zonestat(mem_cont, |
| 1298 | LRU_INACTIVE_FILE); |
| 1299 | active_file = mem_cgroup_get_all_zonestat(mem_cont, |
| 1300 | LRU_ACTIVE_FILE); |
Lee Schermerhorn | 7b85412 | 2008-10-18 20:26:40 -0700 | [diff] [blame] | 1301 | unevictable = mem_cgroup_get_all_zonestat(mem_cont, |
| 1302 | LRU_UNEVICTABLE); |
| 1303 | |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 1304 | cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE); |
| 1305 | cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE); |
| 1306 | cb->fill(cb, "active_file", (active_file) * PAGE_SIZE); |
| 1307 | cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE); |
Lee Schermerhorn | 7b85412 | 2008-10-18 20:26:40 -0700 | [diff] [blame] | 1308 | cb->fill(cb, "unevictable", unevictable * PAGE_SIZE); |
| 1309 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1310 | } |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 1311 | return 0; |
| 1312 | } |
| 1313 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1314 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1315 | static struct cftype mem_cgroup_files[] = { |
| 1316 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 1317 | .name = "usage_in_bytes", |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1318 | .private = RES_USAGE, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 1319 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1320 | }, |
| 1321 | { |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 1322 | .name = "max_usage_in_bytes", |
| 1323 | .private = RES_MAX_USAGE, |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 1324 | .trigger = mem_cgroup_reset, |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 1325 | .read_u64 = mem_cgroup_read, |
| 1326 | }, |
| 1327 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 1328 | .name = "limit_in_bytes", |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1329 | .private = RES_LIMIT, |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 1330 | .write_string = mem_cgroup_write, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 1331 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1332 | }, |
| 1333 | { |
| 1334 | .name = "failcnt", |
| 1335 | .private = RES_FAILCNT, |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 1336 | .trigger = mem_cgroup_reset, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 1337 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1338 | }, |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 1339 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 1340 | .name = "stat", |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 1341 | .read_map = mem_control_stat_show, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 1342 | }, |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1343 | { |
| 1344 | .name = "force_empty", |
| 1345 | .trigger = mem_cgroup_force_empty_write, |
| 1346 | }, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1347 | }; |
| 1348 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1349 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 1350 | { |
| 1351 | struct mem_cgroup_per_node *pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1352 | struct mem_cgroup_per_zone *mz; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1353 | enum lru_list l; |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 1354 | int zone, tmp = node; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1355 | /* |
| 1356 | * This routine is called against possible nodes. |
| 1357 | * But it's BUG to call kmalloc() against offline node. |
| 1358 | * |
| 1359 | * TODO: this routine can waste much memory for nodes which will |
| 1360 | * never be onlined. It's better to use memory hotplug callback |
| 1361 | * function. |
| 1362 | */ |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 1363 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 1364 | tmp = -1; |
| 1365 | pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1366 | if (!pn) |
| 1367 | return 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1368 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1369 | mem->info.nodeinfo[node] = pn; |
| 1370 | memset(pn, 0, sizeof(*pn)); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1371 | |
| 1372 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 1373 | mz = &pn->zoneinfo[zone]; |
KAMEZAWA Hiroyuki | 072c56c1 | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 1374 | spin_lock_init(&mz->lru_lock); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1375 | for_each_lru(l) |
| 1376 | INIT_LIST_HEAD(&mz->lists[l]); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1377 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1378 | return 0; |
| 1379 | } |
| 1380 | |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1381 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 1382 | { |
| 1383 | kfree(mem->info.nodeinfo[node]); |
| 1384 | } |
| 1385 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1386 | static int mem_cgroup_size(void) |
| 1387 | { |
| 1388 | int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu); |
| 1389 | return sizeof(struct mem_cgroup) + cpustat_size; |
| 1390 | } |
| 1391 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1392 | static struct mem_cgroup *mem_cgroup_alloc(void) |
| 1393 | { |
| 1394 | struct mem_cgroup *mem; |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1395 | int size = mem_cgroup_size(); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1396 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1397 | if (size < PAGE_SIZE) |
| 1398 | mem = kmalloc(size, GFP_KERNEL); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1399 | else |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1400 | mem = vmalloc(size); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1401 | |
| 1402 | if (mem) |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1403 | memset(mem, 0, size); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1404 | return mem; |
| 1405 | } |
| 1406 | |
| 1407 | static void mem_cgroup_free(struct mem_cgroup *mem) |
| 1408 | { |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1409 | if (mem_cgroup_size() < PAGE_SIZE) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1410 | kfree(mem); |
| 1411 | else |
| 1412 | vfree(mem); |
| 1413 | } |
| 1414 | |
| 1415 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame^] | 1416 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 1417 | static void __init enable_swap_cgroup(void) |
| 1418 | { |
| 1419 | if (!mem_cgroup_subsys.disabled && really_do_swap_account) |
| 1420 | do_swap_account = 1; |
| 1421 | } |
| 1422 | #else |
| 1423 | static void __init enable_swap_cgroup(void) |
| 1424 | { |
| 1425 | } |
| 1426 | #endif |
| 1427 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1428 | static struct cgroup_subsys_state * |
| 1429 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
| 1430 | { |
| 1431 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1432 | int node; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1433 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1434 | mem = mem_cgroup_alloc(); |
| 1435 | if (!mem) |
| 1436 | return ERR_PTR(-ENOMEM); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 1437 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1438 | res_counter_init(&mem->res); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1439 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1440 | for_each_node_state(node, N_POSSIBLE) |
| 1441 | if (alloc_mem_cgroup_per_zone_info(mem, node)) |
| 1442 | goto free_out; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame^] | 1443 | /* root ? */ |
| 1444 | if (cont->parent == NULL) |
| 1445 | enable_swap_cgroup(); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1446 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1447 | return &mem->css; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1448 | free_out: |
| 1449 | for_each_node_state(node, N_POSSIBLE) |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1450 | free_mem_cgroup_per_zone_info(mem, node); |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1451 | mem_cgroup_free(mem); |
Li Zefan | 2dda81c | 2008-02-23 15:24:14 -0800 | [diff] [blame] | 1452 | return ERR_PTR(-ENOMEM); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1453 | } |
| 1454 | |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 1455 | static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
| 1456 | struct cgroup *cont) |
| 1457 | { |
| 1458 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1459 | mem_cgroup_force_empty(mem, false); |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 1460 | } |
| 1461 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1462 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
| 1463 | struct cgroup *cont) |
| 1464 | { |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1465 | int node; |
| 1466 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
| 1467 | |
| 1468 | for_each_node_state(node, N_POSSIBLE) |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1469 | free_mem_cgroup_per_zone_info(mem, node); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1470 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 1471 | mem_cgroup_free(mem_cgroup_from_cont(cont)); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | static int mem_cgroup_populate(struct cgroup_subsys *ss, |
| 1475 | struct cgroup *cont) |
| 1476 | { |
| 1477 | return cgroup_add_files(cont, ss, mem_cgroup_files, |
| 1478 | ARRAY_SIZE(mem_cgroup_files)); |
| 1479 | } |
| 1480 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1481 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 1482 | struct cgroup *cont, |
| 1483 | struct cgroup *old_cont, |
| 1484 | struct task_struct *p) |
| 1485 | { |
| 1486 | struct mm_struct *mm; |
| 1487 | struct mem_cgroup *mem, *old_mem; |
| 1488 | |
| 1489 | mm = get_task_mm(p); |
| 1490 | if (mm == NULL) |
| 1491 | return; |
| 1492 | |
| 1493 | mem = mem_cgroup_from_cont(cont); |
| 1494 | old_mem = mem_cgroup_from_cont(old_cont); |
| 1495 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1496 | /* |
| 1497 | * Only thread group leaders are allowed to migrate, the mm_struct is |
| 1498 | * in effect owned by the leader |
| 1499 | */ |
Pavel Emelyanov | 52ea27e | 2008-03-19 17:00:45 -0700 | [diff] [blame] | 1500 | if (!thread_group_leader(p)) |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1501 | goto out; |
| 1502 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1503 | out: |
| 1504 | mmput(mm); |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1505 | } |
| 1506 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1507 | struct cgroup_subsys mem_cgroup_subsys = { |
| 1508 | .name = "memory", |
| 1509 | .subsys_id = mem_cgroup_subsys_id, |
| 1510 | .create = mem_cgroup_create, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 1511 | .pre_destroy = mem_cgroup_pre_destroy, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1512 | .destroy = mem_cgroup_destroy, |
| 1513 | .populate = mem_cgroup_populate, |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 1514 | .attach = mem_cgroup_move_task, |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1515 | .early_init = 0, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 1516 | }; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame^] | 1517 | |
| 1518 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 1519 | |
| 1520 | static int __init disable_swap_account(char *s) |
| 1521 | { |
| 1522 | really_do_swap_account = 0; |
| 1523 | return 1; |
| 1524 | } |
| 1525 | __setup("noswapaccount", disable_swap_account); |
| 1526 | #endif |