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