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