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 | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 30 | #include <linux/limits.h> |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 31 | #include <linux/mutex.h> |
Balbir Singh | b6ac57d | 2008-04-29 01:00:19 -0700 | [diff] [blame] | 32 | #include <linux/slab.h> |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 33 | #include <linux/swap.h> |
| 34 | #include <linux/spinlock.h> |
| 35 | #include <linux/fs.h> |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 36 | #include <linux/seq_file.h> |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 37 | #include <linux/vmalloc.h> |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 38 | #include <linux/mm_inline.h> |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 39 | #include <linux/page_cgroup.h> |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 40 | #include "internal.h" |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 41 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 42 | #include <asm/uaccess.h> |
| 43 | |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 44 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
KAMEZAWA Hiroyuki | a181b0e | 2008-07-25 01:47:08 -0700 | [diff] [blame] | 45 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 46 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 47 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 48 | /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */ |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 49 | int do_swap_account __read_mostly; |
| 50 | static int really_do_swap_account __initdata = 1; /* for remember boot option*/ |
| 51 | #else |
| 52 | #define do_swap_account (0) |
| 53 | #endif |
| 54 | |
Daisuke Nishimura | 7f4d454 | 2009-01-07 18:08:29 -0800 | [diff] [blame] | 55 | static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */ |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 56 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 57 | /* |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 58 | * Statistics for memory cgroup. |
| 59 | */ |
| 60 | enum mem_cgroup_stat_index { |
| 61 | /* |
| 62 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. |
| 63 | */ |
| 64 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 65 | MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */ |
| 66 | MEM_CGROUP_STAT_MAPPED_FILE, /* # of pages charged as file rss */ |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 67 | MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */ |
| 68 | MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */ |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 69 | |
| 70 | MEM_CGROUP_STAT_NSTATS, |
| 71 | }; |
| 72 | |
| 73 | struct mem_cgroup_stat_cpu { |
| 74 | s64 count[MEM_CGROUP_STAT_NSTATS]; |
| 75 | } ____cacheline_aligned_in_smp; |
| 76 | |
| 77 | struct mem_cgroup_stat { |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 78 | struct mem_cgroup_stat_cpu cpustat[0]; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | /* |
| 82 | * For accounting under irq disable, no need for increment preempt count. |
| 83 | */ |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 84 | 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] | 85 | enum mem_cgroup_stat_index idx, int val) |
| 86 | { |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 87 | stat->count[idx] += val; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat, |
| 91 | enum mem_cgroup_stat_index idx) |
| 92 | { |
| 93 | int cpu; |
| 94 | s64 ret = 0; |
| 95 | for_each_possible_cpu(cpu) |
| 96 | ret += stat->cpustat[cpu].count[idx]; |
| 97 | return ret; |
| 98 | } |
| 99 | |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 100 | static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat) |
| 101 | { |
| 102 | s64 ret; |
| 103 | |
| 104 | ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE); |
| 105 | ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS); |
| 106 | return ret; |
| 107 | } |
| 108 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 109 | /* |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 110 | * per-zone information in memory controller. |
| 111 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 112 | struct mem_cgroup_per_zone { |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 113 | /* |
| 114 | * spin_lock to protect the per cgroup LRU |
| 115 | */ |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 116 | struct list_head lists[NR_LRU_LISTS]; |
| 117 | unsigned long count[NR_LRU_LISTS]; |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 118 | |
| 119 | struct zone_reclaim_stat reclaim_stat; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 120 | }; |
| 121 | /* Macro for accessing counter */ |
| 122 | #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)]) |
| 123 | |
| 124 | struct mem_cgroup_per_node { |
| 125 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; |
| 126 | }; |
| 127 | |
| 128 | struct mem_cgroup_lru_info { |
| 129 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; |
| 130 | }; |
| 131 | |
| 132 | /* |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 133 | * The memory controller data structure. The memory controller controls both |
| 134 | * page cache and RSS per cgroup. We would eventually like to provide |
| 135 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, |
| 136 | * to help the administrator determine what knobs to tune. |
| 137 | * |
| 138 | * 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] | 139 | * we hit the water mark. May be even add a low water mark, such that |
| 140 | * no reclaim occurs from a cgroup at it's low water mark, this is |
| 141 | * a feature that will be implemented much later in the future. |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 142 | */ |
| 143 | struct mem_cgroup { |
| 144 | struct cgroup_subsys_state css; |
| 145 | /* |
| 146 | * the counter to account for memory usage |
| 147 | */ |
| 148 | struct res_counter res; |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 149 | /* |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 150 | * the counter to account for mem+swap usage. |
| 151 | */ |
| 152 | struct res_counter memsw; |
| 153 | /* |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 154 | * Per cgroup active and inactive list, similar to the |
| 155 | * per zone LRU lists. |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 156 | */ |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 157 | struct mem_cgroup_lru_info info; |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 158 | |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 159 | /* |
| 160 | protect against reclaim related member. |
| 161 | */ |
| 162 | spinlock_t reclaim_param_lock; |
| 163 | |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 164 | int prev_priority; /* for recording reclaim priority */ |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | * While reclaiming in a hiearchy, we cache the last child we |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 168 | * reclaimed from. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 169 | */ |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 170 | int last_scanned_child; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 171 | /* |
| 172 | * Should the accounting and control be hierarchical, per subtree? |
| 173 | */ |
| 174 | bool use_hierarchy; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 175 | unsigned long last_oom_jiffies; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 176 | atomic_t refcnt; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 177 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 178 | unsigned int swappiness; |
| 179 | |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 180 | /* set when res.limit == memsw.limit */ |
| 181 | bool memsw_is_minimum; |
| 182 | |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 183 | /* |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 184 | * statistics. This must be placed at the end of memcg. |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 185 | */ |
| 186 | struct mem_cgroup_stat stat; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 187 | }; |
| 188 | |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 189 | enum charge_type { |
| 190 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, |
| 191 | MEM_CGROUP_CHARGE_TYPE_MAPPED, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 192 | MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 193 | MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */ |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 194 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 195 | MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */ |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 196 | NR_CHARGE_TYPE, |
| 197 | }; |
| 198 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 199 | /* only for here (for easy reading.) */ |
| 200 | #define PCGF_CACHE (1UL << PCG_CACHE) |
| 201 | #define PCGF_USED (1UL << PCG_USED) |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 202 | #define PCGF_LOCK (1UL << PCG_LOCK) |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 203 | static const unsigned long |
| 204 | pcg_default_flags[NR_CHARGE_TYPE] = { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 205 | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* File Cache */ |
| 206 | PCGF_USED | PCGF_LOCK, /* Anon */ |
| 207 | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */ |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 208 | 0, /* FORCE */ |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 209 | }; |
| 210 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 211 | /* for encoding cft->private value on file */ |
| 212 | #define _MEM (0) |
| 213 | #define _MEMSWAP (1) |
| 214 | #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val)) |
| 215 | #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff) |
| 216 | #define MEMFILE_ATTR(val) ((val) & 0xffff) |
| 217 | |
| 218 | static void mem_cgroup_get(struct mem_cgroup *mem); |
| 219 | static void mem_cgroup_put(struct mem_cgroup *mem); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 220 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 221 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 222 | static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, |
| 223 | struct page_cgroup *pc, |
| 224 | bool charge) |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 225 | { |
| 226 | int val = (charge)? 1 : -1; |
| 227 | struct mem_cgroup_stat *stat = &mem->stat; |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 228 | struct mem_cgroup_stat_cpu *cpustat; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 229 | int cpu = get_cpu(); |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 230 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 231 | cpustat = &stat->cpustat[cpu]; |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 232 | if (PageCgroupCache(pc)) |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 233 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val); |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 234 | else |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 235 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val); |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 236 | |
| 237 | if (charge) |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 238 | __mem_cgroup_stat_add_safe(cpustat, |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 239 | MEM_CGROUP_STAT_PGPGIN_COUNT, 1); |
| 240 | else |
KAMEZAWA Hiroyuki | addb9ef | 2008-10-18 20:28:10 -0700 | [diff] [blame] | 241 | __mem_cgroup_stat_add_safe(cpustat, |
Balaji Rao | 55e462b | 2008-05-01 04:35:12 -0700 | [diff] [blame] | 242 | MEM_CGROUP_STAT_PGPGOUT_COUNT, 1); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 243 | put_cpu(); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 244 | } |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 245 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 246 | static struct mem_cgroup_per_zone * |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 247 | mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid) |
| 248 | { |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 249 | return &mem->info.nodeinfo[nid]->zoneinfo[zid]; |
| 250 | } |
| 251 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 252 | static struct mem_cgroup_per_zone * |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 253 | page_cgroup_zoneinfo(struct page_cgroup *pc) |
| 254 | { |
| 255 | struct mem_cgroup *mem = pc->mem_cgroup; |
| 256 | int nid = page_cgroup_nid(pc); |
| 257 | int zid = page_cgroup_zid(pc); |
| 258 | |
KOSAKI Motohiro | 5499276 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 259 | if (!mem) |
| 260 | return NULL; |
| 261 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 262 | return mem_cgroup_zoneinfo(mem, nid, zid); |
| 263 | } |
| 264 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 265 | static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem, |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 266 | enum lru_list idx) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 267 | { |
| 268 | int nid, zid; |
| 269 | struct mem_cgroup_per_zone *mz; |
| 270 | u64 total = 0; |
| 271 | |
| 272 | for_each_online_node(nid) |
| 273 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 274 | mz = mem_cgroup_zoneinfo(mem, nid, zid); |
| 275 | total += MEM_CGROUP_ZSTAT(mz, idx); |
| 276 | } |
| 277 | return total; |
KAMEZAWA Hiroyuki | d52aa41 | 2008-02-07 00:14:24 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Hugh Dickins | d5b69e3 | 2008-03-04 14:29:10 -0800 | [diff] [blame] | 280 | static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 281 | { |
| 282 | return container_of(cgroup_subsys_state(cont, |
| 283 | mem_cgroup_subsys_id), struct mem_cgroup, |
| 284 | css); |
| 285 | } |
| 286 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 287 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 288 | { |
Balbir Singh | 31a78f2 | 2008-09-28 23:09:31 +0100 | [diff] [blame] | 289 | /* |
| 290 | * mm_update_next_owner() may clear mm->owner to NULL |
| 291 | * if it races with swapoff, page migration, etc. |
| 292 | * So this can be called with p == NULL. |
| 293 | */ |
| 294 | if (unlikely(!p)) |
| 295 | return NULL; |
| 296 | |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 297 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
| 298 | struct mem_cgroup, css); |
| 299 | } |
| 300 | |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 301 | static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) |
| 302 | { |
| 303 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 304 | |
| 305 | if (!mm) |
| 306 | return NULL; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 307 | /* |
| 308 | * Because we have no locks, mm->owner's may be being moved to other |
| 309 | * cgroup. We use css_tryget() here even if this looks |
| 310 | * pessimistic (rather than adding locks here). |
| 311 | */ |
| 312 | rcu_read_lock(); |
| 313 | do { |
| 314 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 315 | if (unlikely(!mem)) |
| 316 | break; |
| 317 | } while (!css_tryget(&mem->css)); |
| 318 | rcu_read_unlock(); |
| 319 | return mem; |
| 320 | } |
| 321 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 322 | /* |
| 323 | * Call callback function against all cgroup under hierarchy tree. |
| 324 | */ |
| 325 | static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data, |
| 326 | int (*func)(struct mem_cgroup *, void *)) |
| 327 | { |
| 328 | int found, ret, nextid; |
| 329 | struct cgroup_subsys_state *css; |
| 330 | struct mem_cgroup *mem; |
| 331 | |
| 332 | if (!root->use_hierarchy) |
| 333 | return (*func)(root, data); |
| 334 | |
| 335 | nextid = 1; |
| 336 | do { |
| 337 | ret = 0; |
| 338 | mem = NULL; |
| 339 | |
| 340 | rcu_read_lock(); |
| 341 | css = css_get_next(&mem_cgroup_subsys, nextid, &root->css, |
| 342 | &found); |
| 343 | if (css && css_tryget(css)) |
| 344 | mem = container_of(css, struct mem_cgroup, css); |
| 345 | rcu_read_unlock(); |
| 346 | |
| 347 | if (mem) { |
| 348 | ret = (*func)(mem, data); |
| 349 | css_put(&mem->css); |
| 350 | } |
| 351 | nextid = found + 1; |
| 352 | } while (!ret && css); |
| 353 | |
| 354 | return ret; |
| 355 | } |
| 356 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 357 | /* |
| 358 | * Following LRU functions are allowed to be used without PCG_LOCK. |
| 359 | * Operations are called by routine of global LRU independently from memcg. |
| 360 | * What we have to take care of here is validness of pc->mem_cgroup. |
| 361 | * |
| 362 | * Changes to pc->mem_cgroup happens when |
| 363 | * 1. charge |
| 364 | * 2. moving account |
| 365 | * In typical case, "charge" is done before add-to-lru. Exception is SwapCache. |
| 366 | * It is added to LRU before charge. |
| 367 | * If PCG_USED bit is not set, page_cgroup is not added to this private LRU. |
| 368 | * When moving account, the page is not on LRU. It's isolated. |
| 369 | */ |
| 370 | |
| 371 | void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 372 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 373 | struct page_cgroup *pc; |
| 374 | struct mem_cgroup *mem; |
| 375 | struct mem_cgroup_per_zone *mz; |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 376 | |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 377 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 378 | return; |
| 379 | pc = lookup_page_cgroup(page); |
| 380 | /* can happen while we handle swapcache. */ |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 381 | if (list_empty(&pc->lru) || !pc->mem_cgroup) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 382 | return; |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 383 | /* |
| 384 | * We don't check PCG_USED bit. It's cleared when the "page" is finally |
| 385 | * removed from global LRU. |
| 386 | */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 387 | mz = page_cgroup_zoneinfo(pc); |
| 388 | mem = pc->mem_cgroup; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 389 | MEM_CGROUP_ZSTAT(mz, lru) -= 1; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 390 | list_del_init(&pc->lru); |
| 391 | return; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 392 | } |
| 393 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 394 | void mem_cgroup_del_lru(struct page *page) |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 395 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 396 | mem_cgroup_del_lru_list(page, page_lru(page)); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 397 | } |
| 398 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 399 | void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 400 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 401 | struct mem_cgroup_per_zone *mz; |
| 402 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 403 | |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 404 | if (mem_cgroup_disabled()) |
Lee Schermerhorn | 894bc31 | 2008-10-18 20:26:39 -0700 | [diff] [blame] | 405 | return; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 406 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 407 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | bd112db | 2009-01-15 13:51:11 -0800 | [diff] [blame] | 408 | /* |
| 409 | * Used bit is set without atomic ops but after smp_wmb(). |
| 410 | * For making pc->mem_cgroup visible, insert smp_rmb() here. |
| 411 | */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 412 | smp_rmb(); |
| 413 | /* unused page is not rotated. */ |
| 414 | if (!PageCgroupUsed(pc)) |
| 415 | return; |
| 416 | mz = page_cgroup_zoneinfo(pc); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 417 | list_move(&pc->lru, &mz->lists[lru]); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 418 | } |
| 419 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 420 | void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru) |
| 421 | { |
| 422 | struct page_cgroup *pc; |
| 423 | struct mem_cgroup_per_zone *mz; |
| 424 | |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 425 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 426 | return; |
| 427 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | bd112db | 2009-01-15 13:51:11 -0800 | [diff] [blame] | 428 | /* |
| 429 | * Used bit is set without atomic ops but after smp_wmb(). |
| 430 | * For making pc->mem_cgroup visible, insert smp_rmb() here. |
| 431 | */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 432 | smp_rmb(); |
| 433 | if (!PageCgroupUsed(pc)) |
| 434 | return; |
| 435 | |
| 436 | mz = page_cgroup_zoneinfo(pc); |
| 437 | MEM_CGROUP_ZSTAT(mz, lru) += 1; |
| 438 | list_add(&pc->lru, &mz->lists[lru]); |
| 439 | } |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 440 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 441 | /* |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 442 | * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to |
| 443 | * lru because the page may.be reused after it's fully uncharged (because of |
| 444 | * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge |
| 445 | * it again. This function is only used to charge SwapCache. It's done under |
| 446 | * lock_page and expected that zone->lru_lock is never held. |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 447 | */ |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 448 | static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 449 | { |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 450 | unsigned long flags; |
| 451 | struct zone *zone = page_zone(page); |
| 452 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 453 | |
| 454 | spin_lock_irqsave(&zone->lru_lock, flags); |
| 455 | /* |
| 456 | * Forget old LRU when this page_cgroup is *not* used. This Used bit |
| 457 | * is guarded by lock_page() because the page is SwapCache. |
| 458 | */ |
| 459 | if (!PageCgroupUsed(pc)) |
| 460 | mem_cgroup_del_lru_list(page, page_lru(page)); |
| 461 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 462 | } |
| 463 | |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 464 | static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page) |
| 465 | { |
| 466 | unsigned long flags; |
| 467 | struct zone *zone = page_zone(page); |
| 468 | struct page_cgroup *pc = lookup_page_cgroup(page); |
| 469 | |
| 470 | spin_lock_irqsave(&zone->lru_lock, flags); |
| 471 | /* link when the page is linked to LRU but page_cgroup isn't */ |
| 472 | if (PageLRU(page) && list_empty(&pc->lru)) |
| 473 | mem_cgroup_add_lru_list(page, page_lru(page)); |
| 474 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
| 475 | } |
| 476 | |
| 477 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 478 | void mem_cgroup_move_lists(struct page *page, |
| 479 | enum lru_list from, enum lru_list to) |
| 480 | { |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 481 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 482 | return; |
| 483 | mem_cgroup_del_lru_list(page, from); |
| 484 | mem_cgroup_add_lru_list(page, to); |
| 485 | } |
| 486 | |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 487 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
| 488 | { |
| 489 | int ret; |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 490 | struct mem_cgroup *curr = NULL; |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 491 | |
| 492 | task_lock(task); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 493 | rcu_read_lock(); |
| 494 | curr = try_get_mem_cgroup_from_mm(task->mm); |
| 495 | rcu_read_unlock(); |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 496 | task_unlock(task); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 497 | if (!curr) |
| 498 | return 0; |
| 499 | if (curr->use_hierarchy) |
| 500 | ret = css_is_ancestor(&curr->css, &mem->css); |
| 501 | else |
| 502 | ret = (curr == mem); |
| 503 | css_put(&curr->css); |
David Rientjes | 4c4a221 | 2008-02-07 00:14:06 -0800 | [diff] [blame] | 504 | return ret; |
| 505 | } |
| 506 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 507 | /* |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 508 | * prev_priority control...this will be used in memory reclaim path. |
| 509 | */ |
| 510 | int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem) |
| 511 | { |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 512 | int prev_priority; |
| 513 | |
| 514 | spin_lock(&mem->reclaim_param_lock); |
| 515 | prev_priority = mem->prev_priority; |
| 516 | spin_unlock(&mem->reclaim_param_lock); |
| 517 | |
| 518 | return prev_priority; |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority) |
| 522 | { |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 523 | spin_lock(&mem->reclaim_param_lock); |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 524 | if (priority < mem->prev_priority) |
| 525 | mem->prev_priority = priority; |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 526 | spin_unlock(&mem->reclaim_param_lock); |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority) |
| 530 | { |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 531 | spin_lock(&mem->reclaim_param_lock); |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 532 | mem->prev_priority = priority; |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 533 | spin_unlock(&mem->reclaim_param_lock); |
KAMEZAWA Hiroyuki | 6c48a1d | 2008-02-07 00:14:34 -0800 | [diff] [blame] | 534 | } |
| 535 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 536 | static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages) |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 537 | { |
| 538 | unsigned long active; |
| 539 | unsigned long inactive; |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 540 | unsigned long gb; |
| 541 | unsigned long inactive_ratio; |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 542 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 543 | inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON); |
| 544 | active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON); |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 545 | |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 546 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
| 547 | if (gb) |
| 548 | inactive_ratio = int_sqrt(10 * gb); |
| 549 | else |
| 550 | inactive_ratio = 1; |
| 551 | |
| 552 | if (present_pages) { |
| 553 | present_pages[0] = inactive; |
| 554 | present_pages[1] = active; |
| 555 | } |
| 556 | |
| 557 | return inactive_ratio; |
| 558 | } |
| 559 | |
| 560 | int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg) |
| 561 | { |
| 562 | unsigned long active; |
| 563 | unsigned long inactive; |
| 564 | unsigned long present_pages[2]; |
| 565 | unsigned long inactive_ratio; |
| 566 | |
| 567 | inactive_ratio = calc_inactive_ratio(memcg, present_pages); |
| 568 | |
| 569 | inactive = present_pages[0]; |
| 570 | active = present_pages[1]; |
| 571 | |
| 572 | if (inactive * inactive_ratio < active) |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 573 | return 1; |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
Rik van Riel | 56e49d2 | 2009-06-16 15:32:28 -0700 | [diff] [blame] | 578 | int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg) |
| 579 | { |
| 580 | unsigned long active; |
| 581 | unsigned long inactive; |
| 582 | |
| 583 | inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE); |
| 584 | active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE); |
| 585 | |
| 586 | return (active > inactive); |
| 587 | } |
| 588 | |
KOSAKI Motohiro | a3d8e05 | 2009-01-07 18:08:19 -0800 | [diff] [blame] | 589 | unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg, |
| 590 | struct zone *zone, |
| 591 | enum lru_list lru) |
| 592 | { |
| 593 | int nid = zone->zone_pgdat->node_id; |
| 594 | int zid = zone_idx(zone); |
| 595 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
| 596 | |
| 597 | return MEM_CGROUP_ZSTAT(mz, lru); |
| 598 | } |
| 599 | |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 600 | struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg, |
| 601 | struct zone *zone) |
| 602 | { |
| 603 | int nid = zone->zone_pgdat->node_id; |
| 604 | int zid = zone_idx(zone); |
| 605 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
| 606 | |
| 607 | return &mz->reclaim_stat; |
| 608 | } |
| 609 | |
| 610 | struct zone_reclaim_stat * |
| 611 | mem_cgroup_get_reclaim_stat_from_page(struct page *page) |
| 612 | { |
| 613 | struct page_cgroup *pc; |
| 614 | struct mem_cgroup_per_zone *mz; |
| 615 | |
| 616 | if (mem_cgroup_disabled()) |
| 617 | return NULL; |
| 618 | |
| 619 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | bd112db | 2009-01-15 13:51:11 -0800 | [diff] [blame] | 620 | /* |
| 621 | * Used bit is set without atomic ops but after smp_wmb(). |
| 622 | * For making pc->mem_cgroup visible, insert smp_rmb() here. |
| 623 | */ |
| 624 | smp_rmb(); |
| 625 | if (!PageCgroupUsed(pc)) |
| 626 | return NULL; |
| 627 | |
KOSAKI Motohiro | 3e2f41f | 2009-01-07 18:08:20 -0800 | [diff] [blame] | 628 | mz = page_cgroup_zoneinfo(pc); |
| 629 | if (!mz) |
| 630 | return NULL; |
| 631 | |
| 632 | return &mz->reclaim_stat; |
| 633 | } |
| 634 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 635 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
| 636 | struct list_head *dst, |
| 637 | unsigned long *scanned, int order, |
| 638 | int mode, struct zone *z, |
| 639 | struct mem_cgroup *mem_cont, |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 640 | int active, int file) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 641 | { |
| 642 | unsigned long nr_taken = 0; |
| 643 | struct page *page; |
| 644 | unsigned long scan; |
| 645 | LIST_HEAD(pc_list); |
| 646 | struct list_head *src; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 647 | struct page_cgroup *pc, *tmp; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 648 | int nid = z->zone_pgdat->node_id; |
| 649 | int zid = zone_idx(z); |
| 650 | struct mem_cgroup_per_zone *mz; |
Johannes Weiner | b7c46d1 | 2009-09-21 17:02:56 -0700 | [diff] [blame^] | 651 | int lru = LRU_FILE * file + active; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 652 | int ret; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 653 | |
Balbir Singh | cf475ad | 2008-04-29 01:00:16 -0700 | [diff] [blame] | 654 | BUG_ON(!mem_cont); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 655 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 656 | src = &mz->lists[lru]; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 657 | |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 658 | scan = 0; |
| 659 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 660 | if (scan >= nr_to_scan) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 661 | break; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 662 | |
| 663 | page = pc->page; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 664 | if (unlikely(!PageCgroupUsed(pc))) |
| 665 | continue; |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 666 | if (unlikely(!PageLRU(page))) |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 667 | continue; |
KAMEZAWA Hiroyuki | ff7283f | 2008-02-07 00:14:11 -0800 | [diff] [blame] | 668 | |
Hugh Dickins | 436c6541 | 2008-02-07 00:14:12 -0800 | [diff] [blame] | 669 | scan++; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 670 | ret = __isolate_lru_page(page, mode, file); |
| 671 | switch (ret) { |
| 672 | case 0: |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 673 | list_move(&page->lru, dst); |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 674 | mem_cgroup_del_lru(page); |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 675 | nr_taken++; |
KAMEZAWA Hiroyuki | 2ffebca | 2009-06-17 16:27:21 -0700 | [diff] [blame] | 676 | break; |
| 677 | case -EBUSY: |
| 678 | /* we don't affect global LRU but rotate in our LRU */ |
| 679 | mem_cgroup_rotate_lru_list(page, page_lru(page)); |
| 680 | break; |
| 681 | default: |
| 682 | break; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 686 | *scanned = scan; |
| 687 | return nr_taken; |
| 688 | } |
| 689 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 690 | #define mem_cgroup_from_res_counter(counter, member) \ |
| 691 | container_of(counter, struct mem_cgroup, member) |
| 692 | |
Daisuke Nishimura | b85a96c | 2009-01-07 18:08:12 -0800 | [diff] [blame] | 693 | static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem) |
| 694 | { |
| 695 | if (do_swap_account) { |
| 696 | if (res_counter_check_under_limit(&mem->res) && |
| 697 | res_counter_check_under_limit(&mem->memsw)) |
| 698 | return true; |
| 699 | } else |
| 700 | if (res_counter_check_under_limit(&mem->res)) |
| 701 | return true; |
| 702 | return false; |
| 703 | } |
| 704 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 705 | static unsigned int get_swappiness(struct mem_cgroup *memcg) |
| 706 | { |
| 707 | struct cgroup *cgrp = memcg->css.cgroup; |
| 708 | unsigned int swappiness; |
| 709 | |
| 710 | /* root ? */ |
| 711 | if (cgrp->parent == NULL) |
| 712 | return vm_swappiness; |
| 713 | |
| 714 | spin_lock(&memcg->reclaim_param_lock); |
| 715 | swappiness = memcg->swappiness; |
| 716 | spin_unlock(&memcg->reclaim_param_lock); |
| 717 | |
| 718 | return swappiness; |
| 719 | } |
| 720 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 721 | static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data) |
| 722 | { |
| 723 | int *val = data; |
| 724 | (*val)++; |
| 725 | return 0; |
| 726 | } |
Balbir Singh | e222432 | 2009-04-02 16:57:39 -0700 | [diff] [blame] | 727 | |
| 728 | /** |
| 729 | * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode. |
| 730 | * @memcg: The memory cgroup that went over limit |
| 731 | * @p: Task that is going to be killed |
| 732 | * |
| 733 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is |
| 734 | * enabled |
| 735 | */ |
| 736 | void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) |
| 737 | { |
| 738 | struct cgroup *task_cgrp; |
| 739 | struct cgroup *mem_cgrp; |
| 740 | /* |
| 741 | * Need a buffer in BSS, can't rely on allocations. The code relies |
| 742 | * on the assumption that OOM is serialized for memory controller. |
| 743 | * If this assumption is broken, revisit this code. |
| 744 | */ |
| 745 | static char memcg_name[PATH_MAX]; |
| 746 | int ret; |
| 747 | |
| 748 | if (!memcg) |
| 749 | return; |
| 750 | |
| 751 | |
| 752 | rcu_read_lock(); |
| 753 | |
| 754 | mem_cgrp = memcg->css.cgroup; |
| 755 | task_cgrp = task_cgroup(p, mem_cgroup_subsys_id); |
| 756 | |
| 757 | ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX); |
| 758 | if (ret < 0) { |
| 759 | /* |
| 760 | * Unfortunately, we are unable to convert to a useful name |
| 761 | * But we'll still print out the usage information |
| 762 | */ |
| 763 | rcu_read_unlock(); |
| 764 | goto done; |
| 765 | } |
| 766 | rcu_read_unlock(); |
| 767 | |
| 768 | printk(KERN_INFO "Task in %s killed", memcg_name); |
| 769 | |
| 770 | rcu_read_lock(); |
| 771 | ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX); |
| 772 | if (ret < 0) { |
| 773 | rcu_read_unlock(); |
| 774 | goto done; |
| 775 | } |
| 776 | rcu_read_unlock(); |
| 777 | |
| 778 | /* |
| 779 | * Continues from above, so we don't need an KERN_ level |
| 780 | */ |
| 781 | printk(KERN_CONT " as a result of limit of %s\n", memcg_name); |
| 782 | done: |
| 783 | |
| 784 | printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n", |
| 785 | res_counter_read_u64(&memcg->res, RES_USAGE) >> 10, |
| 786 | res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10, |
| 787 | res_counter_read_u64(&memcg->res, RES_FAILCNT)); |
| 788 | printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, " |
| 789 | "failcnt %llu\n", |
| 790 | res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10, |
| 791 | res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10, |
| 792 | res_counter_read_u64(&memcg->memsw, RES_FAILCNT)); |
| 793 | } |
| 794 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 795 | /* |
| 796 | * This function returns the number of memcg under hierarchy tree. Returns |
| 797 | * 1(self count) if no children. |
| 798 | */ |
| 799 | static int mem_cgroup_count_children(struct mem_cgroup *mem) |
| 800 | { |
| 801 | int num = 0; |
| 802 | mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb); |
| 803 | return num; |
| 804 | } |
| 805 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 806 | /* |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 807 | * Visit the first child (need not be the first child as per the ordering |
| 808 | * of the cgroup list, since we track last_scanned_child) of @mem and use |
| 809 | * that to reclaim free pages from. |
| 810 | */ |
| 811 | static struct mem_cgroup * |
| 812 | mem_cgroup_select_victim(struct mem_cgroup *root_mem) |
| 813 | { |
| 814 | struct mem_cgroup *ret = NULL; |
| 815 | struct cgroup_subsys_state *css; |
| 816 | int nextid, found; |
| 817 | |
| 818 | if (!root_mem->use_hierarchy) { |
| 819 | css_get(&root_mem->css); |
| 820 | ret = root_mem; |
| 821 | } |
| 822 | |
| 823 | while (!ret) { |
| 824 | rcu_read_lock(); |
| 825 | nextid = root_mem->last_scanned_child + 1; |
| 826 | css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css, |
| 827 | &found); |
| 828 | if (css && css_tryget(css)) |
| 829 | ret = container_of(css, struct mem_cgroup, css); |
| 830 | |
| 831 | rcu_read_unlock(); |
| 832 | /* Updates scanning parameter */ |
| 833 | spin_lock(&root_mem->reclaim_param_lock); |
| 834 | if (!css) { |
| 835 | /* this means start scan from ID:1 */ |
| 836 | root_mem->last_scanned_child = 0; |
| 837 | } else |
| 838 | root_mem->last_scanned_child = found; |
| 839 | spin_unlock(&root_mem->reclaim_param_lock); |
| 840 | } |
| 841 | |
| 842 | return ret; |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | * Scan the hierarchy if needed to reclaim memory. We remember the last child |
| 847 | * we reclaimed from, so that we don't end up penalizing one child extensively |
| 848 | * based on its position in the children list. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 849 | * |
| 850 | * root_mem is the original ancestor that we've been reclaim from. |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 851 | * |
| 852 | * We give up and return to the caller when we visit root_mem twice. |
| 853 | * (other groups can be removed while we're walking....) |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 854 | * |
| 855 | * If shrink==true, for avoiding to free too much, this returns immedieately. |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 856 | */ |
| 857 | static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem, |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 858 | gfp_t gfp_mask, bool noswap, bool shrink) |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 859 | { |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 860 | struct mem_cgroup *victim; |
| 861 | int ret, total = 0; |
| 862 | int loop = 0; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 863 | |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 864 | /* If memsw_is_minimum==1, swap-out is of-no-use. */ |
| 865 | if (root_mem->memsw_is_minimum) |
| 866 | noswap = true; |
| 867 | |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 868 | while (loop < 2) { |
| 869 | victim = mem_cgroup_select_victim(root_mem); |
| 870 | if (victim == root_mem) |
| 871 | loop++; |
| 872 | if (!mem_cgroup_local_usage(&victim->stat)) { |
| 873 | /* this cgroup's local usage == 0 */ |
| 874 | css_put(&victim->css); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 875 | continue; |
| 876 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 877 | /* we use swappiness of local cgroup */ |
| 878 | ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, noswap, |
| 879 | get_swappiness(victim)); |
| 880 | css_put(&victim->css); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 881 | /* |
| 882 | * At shrinking usage, we can't check we should stop here or |
| 883 | * reclaim more. It's depends on callers. last_scanned_child |
| 884 | * will work enough for keeping fairness under tree. |
| 885 | */ |
| 886 | if (shrink) |
| 887 | return ret; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 888 | total += ret; |
Daisuke Nishimura | b85a96c | 2009-01-07 18:08:12 -0800 | [diff] [blame] | 889 | if (mem_cgroup_check_under_limit(root_mem)) |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 890 | return 1 + total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 891 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 892 | return total; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 893 | } |
| 894 | |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 895 | bool mem_cgroup_oom_called(struct task_struct *task) |
| 896 | { |
| 897 | bool ret = false; |
| 898 | struct mem_cgroup *mem; |
| 899 | struct mm_struct *mm; |
| 900 | |
| 901 | rcu_read_lock(); |
| 902 | mm = task->mm; |
| 903 | if (!mm) |
| 904 | mm = &init_mm; |
| 905 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
| 906 | if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10)) |
| 907 | ret = true; |
| 908 | rcu_read_unlock(); |
| 909 | return ret; |
| 910 | } |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 911 | |
| 912 | static int record_last_oom_cb(struct mem_cgroup *mem, void *data) |
| 913 | { |
| 914 | mem->last_oom_jiffies = jiffies; |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | static void record_last_oom(struct mem_cgroup *mem) |
| 919 | { |
| 920 | mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb); |
| 921 | } |
| 922 | |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 923 | /* |
| 924 | * Currently used to update mapped file statistics, but the routine can be |
| 925 | * generalized to update other statistics as well. |
| 926 | */ |
| 927 | void mem_cgroup_update_mapped_file_stat(struct page *page, int val) |
| 928 | { |
| 929 | struct mem_cgroup *mem; |
| 930 | struct mem_cgroup_stat *stat; |
| 931 | struct mem_cgroup_stat_cpu *cpustat; |
| 932 | int cpu; |
| 933 | struct page_cgroup *pc; |
| 934 | |
| 935 | if (!page_is_file_cache(page)) |
| 936 | return; |
| 937 | |
| 938 | pc = lookup_page_cgroup(page); |
| 939 | if (unlikely(!pc)) |
| 940 | return; |
| 941 | |
| 942 | lock_page_cgroup(pc); |
| 943 | mem = pc->mem_cgroup; |
| 944 | if (!mem) |
| 945 | goto done; |
| 946 | |
| 947 | if (!PageCgroupUsed(pc)) |
| 948 | goto done; |
| 949 | |
| 950 | /* |
| 951 | * Preemption is already disabled, we don't need get_cpu() |
| 952 | */ |
| 953 | cpu = smp_processor_id(); |
| 954 | stat = &mem->stat; |
| 955 | cpustat = &stat->cpustat[cpu]; |
| 956 | |
| 957 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, val); |
| 958 | done: |
| 959 | unlock_page_cgroup(pc); |
| 960 | } |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 961 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 962 | /* |
| 963 | * Unlike exported interface, "oom" parameter is added. if oom==true, |
| 964 | * oom-killer can be invoked. |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 965 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 966 | static int __mem_cgroup_try_charge(struct mm_struct *mm, |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 967 | gfp_t gfp_mask, struct mem_cgroup **memcg, |
| 968 | bool oom) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 969 | { |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 970 | struct mem_cgroup *mem, *mem_over_limit; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 971 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 972 | struct res_counter *fail_res; |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 973 | |
| 974 | if (unlikely(test_thread_flag(TIF_MEMDIE))) { |
| 975 | /* Don't account this! */ |
| 976 | *memcg = NULL; |
| 977 | return 0; |
| 978 | } |
| 979 | |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 980 | /* |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 981 | * We always charge the cgroup the mm_struct belongs to. |
| 982 | * The mm_struct's mem_cgroup changes on task migration if the |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 983 | * thread group leader migrates. It's possible that mm is not |
| 984 | * set, if so charge the init_mm (happens for pagecache usage). |
| 985 | */ |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 986 | mem = *memcg; |
| 987 | if (likely(!mem)) { |
| 988 | mem = try_get_mem_cgroup_from_mm(mm); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 989 | *memcg = mem; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 990 | } else { |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 991 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 992 | } |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 993 | if (unlikely(!mem)) |
| 994 | return 0; |
| 995 | |
Nikanth Karthikesan | 46f7e60 | 2009-05-28 14:34:41 -0700 | [diff] [blame] | 996 | VM_BUG_ON(css_is_removed(&mem->css)); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 997 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 998 | while (1) { |
| 999 | int ret; |
| 1000 | bool noswap = false; |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1001 | |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 1002 | ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1003 | if (likely(!ret)) { |
| 1004 | if (!do_swap_account) |
| 1005 | break; |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 1006 | ret = res_counter_charge(&mem->memsw, PAGE_SIZE, |
| 1007 | &fail_res); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1008 | if (likely(!ret)) |
| 1009 | break; |
| 1010 | /* mem+swap counter fails */ |
| 1011 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
| 1012 | noswap = true; |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1013 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, |
| 1014 | memsw); |
| 1015 | } else |
| 1016 | /* mem counter fails */ |
| 1017 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, |
| 1018 | res); |
| 1019 | |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 1020 | if (!(gfp_mask & __GFP_WAIT)) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1021 | goto nomem; |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 1022 | |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 1023 | ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask, |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1024 | noswap, false); |
Daisuke Nishimura | 4d1c627 | 2009-01-15 13:51:14 -0800 | [diff] [blame] | 1025 | if (ret) |
| 1026 | continue; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1027 | |
| 1028 | /* |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 1029 | * try_to_free_mem_cgroup_pages() might not give us a full |
| 1030 | * picture of reclaim. Some pages are reclaimed and might be |
| 1031 | * moved to swap cache or just unmapped from the cgroup. |
| 1032 | * Check the limit again to see if the reclaim reduced the |
| 1033 | * current usage of the cgroup before giving up |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1034 | * |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 1035 | */ |
Daisuke Nishimura | b85a96c | 2009-01-07 18:08:12 -0800 | [diff] [blame] | 1036 | if (mem_cgroup_check_under_limit(mem_over_limit)) |
| 1037 | continue; |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 1038 | |
| 1039 | if (!nr_retries--) { |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1040 | if (oom) { |
Daisuke Nishimura | 7f4d454 | 2009-01-07 18:08:29 -0800 | [diff] [blame] | 1041 | mutex_lock(&memcg_tasklist); |
KAMEZAWA Hiroyuki | 8870075 | 2009-01-07 18:08:09 -0800 | [diff] [blame] | 1042 | mem_cgroup_out_of_memory(mem_over_limit, gfp_mask); |
Daisuke Nishimura | 7f4d454 | 2009-01-07 18:08:29 -0800 | [diff] [blame] | 1043 | mutex_unlock(&memcg_tasklist); |
KAMEZAWA Hiroyuki | 0b7f569 | 2009-04-02 16:57:38 -0700 | [diff] [blame] | 1044 | record_last_oom(mem_over_limit); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1045 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1046 | goto nomem; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 1047 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1048 | } |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1049 | return 0; |
| 1050 | nomem: |
| 1051 | css_put(&mem->css); |
| 1052 | return -ENOMEM; |
| 1053 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1054 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1055 | |
| 1056 | /* |
| 1057 | * A helper function to get mem_cgroup from ID. must be called under |
| 1058 | * rcu_read_lock(). The caller must check css_is_removed() or some if |
| 1059 | * it's concern. (dropping refcnt from swap can be called against removed |
| 1060 | * memcg.) |
| 1061 | */ |
| 1062 | static struct mem_cgroup *mem_cgroup_lookup(unsigned short id) |
| 1063 | { |
| 1064 | struct cgroup_subsys_state *css; |
| 1065 | |
| 1066 | /* ID 0 is unused ID */ |
| 1067 | if (!id) |
| 1068 | return NULL; |
| 1069 | css = css_lookup(&mem_cgroup_subsys, id); |
| 1070 | if (!css) |
| 1071 | return NULL; |
| 1072 | return container_of(css, struct mem_cgroup, css); |
| 1073 | } |
| 1074 | |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1075 | static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page) |
| 1076 | { |
| 1077 | struct mem_cgroup *mem; |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 1078 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1079 | unsigned short id; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1080 | swp_entry_t ent; |
| 1081 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 1082 | VM_BUG_ON(!PageLocked(page)); |
| 1083 | |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1084 | if (!PageSwapCache(page)) |
| 1085 | return NULL; |
| 1086 | |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 1087 | pc = lookup_page_cgroup(page); |
Daisuke Nishimura | c0bd3f6 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 1088 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1089 | if (PageCgroupUsed(pc)) { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 1090 | mem = pc->mem_cgroup; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1091 | if (mem && !css_tryget(&mem->css)) |
| 1092 | mem = NULL; |
| 1093 | } else { |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 1094 | ent.val = page_private(page); |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1095 | id = lookup_swap_cgroup(ent); |
| 1096 | rcu_read_lock(); |
| 1097 | mem = mem_cgroup_lookup(id); |
| 1098 | if (mem && !css_tryget(&mem->css)) |
| 1099 | mem = NULL; |
| 1100 | rcu_read_unlock(); |
Daisuke Nishimura | 3c776e6 | 2009-04-02 16:57:43 -0700 | [diff] [blame] | 1101 | } |
Daisuke Nishimura | c0bd3f6 | 2009-04-30 15:08:11 -0700 | [diff] [blame] | 1102 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1103 | return mem; |
| 1104 | } |
| 1105 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1106 | /* |
Daisuke Nishimura | a5e924f | 2009-01-07 18:08:28 -0800 | [diff] [blame] | 1107 | * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1108 | * USED state. If already USED, uncharge and return. |
| 1109 | */ |
| 1110 | |
| 1111 | static void __mem_cgroup_commit_charge(struct mem_cgroup *mem, |
| 1112 | struct page_cgroup *pc, |
| 1113 | enum charge_type ctype) |
| 1114 | { |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1115 | /* try_charge() can return NULL to *memcg, taking care of it. */ |
| 1116 | if (!mem) |
| 1117 | return; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1118 | |
| 1119 | lock_page_cgroup(pc); |
| 1120 | if (unlikely(PageCgroupUsed(pc))) { |
| 1121 | unlock_page_cgroup(pc); |
| 1122 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1123 | if (do_swap_account) |
| 1124 | res_counter_uncharge(&mem->memsw, PAGE_SIZE); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1125 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1126 | return; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1127 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1128 | pc->mem_cgroup = mem; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1129 | smp_wmb(); |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 1130 | pc->flags = pcg_default_flags[ctype]; |
Hugh Dickins | 3be9127 | 2008-02-07 00:14:19 -0800 | [diff] [blame] | 1131 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1132 | mem_cgroup_charge_statistics(mem, pc, true); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1133 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1134 | unlock_page_cgroup(pc); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1135 | } |
| 1136 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1137 | /** |
| 1138 | * mem_cgroup_move_account - move account of the page |
| 1139 | * @pc: page_cgroup of the page. |
| 1140 | * @from: mem_cgroup which the page is moved from. |
| 1141 | * @to: mem_cgroup which the page is moved to. @from != @to. |
| 1142 | * |
| 1143 | * The caller must confirm following. |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1144 | * - page is not on LRU (isolate_page() is useful.) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1145 | * |
| 1146 | * returns 0 at success, |
| 1147 | * returns -EBUSY when lock is busy or "pc" is unstable. |
| 1148 | * |
| 1149 | * This function does "uncharge" from old cgroup but doesn't do "charge" to |
| 1150 | * new cgroup. It should be done by a caller. |
| 1151 | */ |
| 1152 | |
| 1153 | static int mem_cgroup_move_account(struct page_cgroup *pc, |
| 1154 | struct mem_cgroup *from, struct mem_cgroup *to) |
| 1155 | { |
| 1156 | struct mem_cgroup_per_zone *from_mz, *to_mz; |
| 1157 | int nid, zid; |
| 1158 | int ret = -EBUSY; |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1159 | struct page *page; |
| 1160 | int cpu; |
| 1161 | struct mem_cgroup_stat *stat; |
| 1162 | struct mem_cgroup_stat_cpu *cpustat; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1163 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1164 | VM_BUG_ON(from == to); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1165 | VM_BUG_ON(PageLRU(pc->page)); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1166 | |
| 1167 | nid = page_cgroup_nid(pc); |
| 1168 | zid = page_cgroup_zid(pc); |
| 1169 | from_mz = mem_cgroup_zoneinfo(from, nid, zid); |
| 1170 | to_mz = mem_cgroup_zoneinfo(to, nid, zid); |
| 1171 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1172 | if (!trylock_page_cgroup(pc)) |
| 1173 | return ret; |
| 1174 | |
| 1175 | if (!PageCgroupUsed(pc)) |
| 1176 | goto out; |
| 1177 | |
| 1178 | if (pc->mem_cgroup != from) |
| 1179 | goto out; |
| 1180 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1181 | res_counter_uncharge(&from->res, PAGE_SIZE); |
| 1182 | mem_cgroup_charge_statistics(from, pc, false); |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 1183 | |
| 1184 | page = pc->page; |
| 1185 | if (page_is_file_cache(page) && page_mapped(page)) { |
| 1186 | cpu = smp_processor_id(); |
| 1187 | /* Update mapped_file data for mem_cgroup "from" */ |
| 1188 | stat = &from->stat; |
| 1189 | cpustat = &stat->cpustat[cpu]; |
| 1190 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, |
| 1191 | -1); |
| 1192 | |
| 1193 | /* Update mapped_file data for mem_cgroup "to" */ |
| 1194 | stat = &to->stat; |
| 1195 | cpustat = &stat->cpustat[cpu]; |
| 1196 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, |
| 1197 | 1); |
| 1198 | } |
| 1199 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1200 | if (do_swap_account) |
| 1201 | res_counter_uncharge(&from->memsw, PAGE_SIZE); |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 1202 | css_put(&from->css); |
| 1203 | |
| 1204 | css_get(&to->css); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1205 | pc->mem_cgroup = to; |
| 1206 | mem_cgroup_charge_statistics(to, pc, true); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1207 | ret = 0; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1208 | out: |
| 1209 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 1210 | /* |
| 1211 | * We charges against "to" which may not have any tasks. Then, "to" |
| 1212 | * can be under rmdir(). But in current implementation, caller of |
| 1213 | * this function is just force_empty() and it's garanteed that |
| 1214 | * "to" is never removed. So, we don't check rmdir status here. |
| 1215 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1216 | return ret; |
| 1217 | } |
| 1218 | |
| 1219 | /* |
| 1220 | * move charges to its parent. |
| 1221 | */ |
| 1222 | |
| 1223 | static int mem_cgroup_move_parent(struct page_cgroup *pc, |
| 1224 | struct mem_cgroup *child, |
| 1225 | gfp_t gfp_mask) |
| 1226 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1227 | struct page *page = pc->page; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1228 | struct cgroup *cg = child->css.cgroup; |
| 1229 | struct cgroup *pcg = cg->parent; |
| 1230 | struct mem_cgroup *parent; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1231 | int ret; |
| 1232 | |
| 1233 | /* Is ROOT ? */ |
| 1234 | if (!pcg) |
| 1235 | return -EINVAL; |
| 1236 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1237 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1238 | parent = mem_cgroup_from_cont(pcg); |
| 1239 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1240 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1241 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1242 | if (ret || !parent) |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1243 | return ret; |
| 1244 | |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 1245 | if (!get_page_unless_zero(page)) { |
| 1246 | ret = -EBUSY; |
| 1247 | goto uncharge; |
| 1248 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1249 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1250 | ret = isolate_lru_page(page); |
| 1251 | |
| 1252 | if (ret) |
| 1253 | goto cancel; |
| 1254 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1255 | ret = mem_cgroup_move_account(pc, child, parent); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1256 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1257 | putback_lru_page(page); |
| 1258 | if (!ret) { |
| 1259 | put_page(page); |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 1260 | /* drop extra refcnt by try_charge() */ |
| 1261 | css_put(&parent->css); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1262 | return 0; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1263 | } |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 1264 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1265 | cancel: |
Daisuke Nishimura | 40d5813 | 2009-01-15 13:51:12 -0800 | [diff] [blame] | 1266 | put_page(page); |
| 1267 | uncharge: |
| 1268 | /* drop extra refcnt by try_charge() */ |
| 1269 | css_put(&parent->css); |
| 1270 | /* uncharge if move fails */ |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1271 | res_counter_uncharge(&parent->res, PAGE_SIZE); |
| 1272 | if (do_swap_account) |
| 1273 | res_counter_uncharge(&parent->memsw, PAGE_SIZE); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1274 | return ret; |
| 1275 | } |
| 1276 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1277 | /* |
| 1278 | * Charge the memory controller for page usage. |
| 1279 | * Return |
| 1280 | * 0 if the charge was successful |
| 1281 | * < 0 if the cgroup is over its limit |
| 1282 | */ |
| 1283 | static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm, |
| 1284 | gfp_t gfp_mask, enum charge_type ctype, |
| 1285 | struct mem_cgroup *memcg) |
| 1286 | { |
| 1287 | struct mem_cgroup *mem; |
| 1288 | struct page_cgroup *pc; |
| 1289 | int ret; |
| 1290 | |
| 1291 | pc = lookup_page_cgroup(page); |
| 1292 | /* can happen at boot */ |
| 1293 | if (unlikely(!pc)) |
| 1294 | return 0; |
| 1295 | prefetchw(pc); |
| 1296 | |
| 1297 | mem = memcg; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1298 | ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true); |
KAMEZAWA Hiroyuki | a636b32 | 2009-01-07 18:08:08 -0800 | [diff] [blame] | 1299 | if (ret || !mem) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1300 | return ret; |
| 1301 | |
| 1302 | __mem_cgroup_commit_charge(mem, pc, ctype); |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
| 1306 | int mem_cgroup_newpage_charge(struct page *page, |
| 1307 | struct mm_struct *mm, gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 1308 | { |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1309 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 1310 | return 0; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1311 | if (PageCompound(page)) |
| 1312 | return 0; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1313 | /* |
| 1314 | * If already mapped, we don't have to account. |
| 1315 | * If page cache, page->mapping has address_space. |
| 1316 | * But page->mapping may have out-of-use anon_vma pointer, |
| 1317 | * detecit it by PageAnon() check. newly-mapped-anon's page->mapping |
| 1318 | * is NULL. |
| 1319 | */ |
| 1320 | if (page_mapped(page) || (page->mapping && !PageAnon(page))) |
| 1321 | return 0; |
| 1322 | if (unlikely(!mm)) |
| 1323 | mm = &init_mm; |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 1324 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1325 | MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL); |
KAMEZAWA Hiroyuki | 217bc31 | 2008-02-07 00:14:17 -0800 | [diff] [blame] | 1326 | } |
| 1327 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 1328 | static void |
| 1329 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 1330 | enum charge_type ctype); |
| 1331 | |
Balbir Singh | e1a1cd5 | 2008-02-07 00:14:02 -0800 | [diff] [blame] | 1332 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
| 1333 | gfp_t gfp_mask) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 1334 | { |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1335 | struct mem_cgroup *mem = NULL; |
| 1336 | int ret; |
| 1337 | |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1338 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 1339 | return 0; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1340 | if (PageCompound(page)) |
| 1341 | return 0; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 1342 | /* |
| 1343 | * Corner case handling. This is called from add_to_page_cache() |
| 1344 | * in usual. But some FS (shmem) precharges this page before calling it |
| 1345 | * and call add_to_page_cache() with GFP_NOWAIT. |
| 1346 | * |
| 1347 | * For GFP_NOWAIT case, the page may be pre-charged before calling |
| 1348 | * add_to_page_cache(). (See shmem.c) check it here and avoid to call |
| 1349 | * charge twice. (It works but has to pay a bit larger cost.) |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1350 | * And when the page is SwapCache, it should take swap information |
| 1351 | * into account. This is under lock_page() now. |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 1352 | */ |
| 1353 | if (!(gfp_mask & __GFP_WAIT)) { |
| 1354 | struct page_cgroup *pc; |
| 1355 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1356 | |
| 1357 | pc = lookup_page_cgroup(page); |
| 1358 | if (!pc) |
| 1359 | return 0; |
| 1360 | lock_page_cgroup(pc); |
| 1361 | if (PageCgroupUsed(pc)) { |
| 1362 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 1363 | return 0; |
| 1364 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1365 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1368 | if (unlikely(!mm && !mem)) |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 1369 | mm = &init_mm; |
KAMEZAWA Hiroyuki | accf163 | 2008-07-25 01:47:17 -0700 | [diff] [blame] | 1370 | |
KAMEZAWA Hiroyuki | c05555b | 2008-10-18 20:28:11 -0700 | [diff] [blame] | 1371 | if (page_is_file_cache(page)) |
| 1372 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1373 | MEM_CGROUP_CHARGE_TYPE_CACHE, NULL); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1374 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 1375 | /* shmem */ |
| 1376 | if (PageSwapCache(page)) { |
| 1377 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); |
| 1378 | if (!ret) |
| 1379 | __mem_cgroup_commit_charge_swapin(page, mem, |
| 1380 | MEM_CGROUP_CHARGE_TYPE_SHMEM); |
| 1381 | } else |
| 1382 | ret = mem_cgroup_charge_common(page, mm, gfp_mask, |
| 1383 | MEM_CGROUP_CHARGE_TYPE_SHMEM, mem); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1384 | |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1385 | return ret; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1386 | } |
| 1387 | |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1388 | /* |
| 1389 | * While swap-in, try_charge -> commit or cancel, the page is locked. |
| 1390 | * And when try_charge() successfully returns, one refcnt to memcg without |
| 1391 | * struct page_cgroup is aquired. This refcnt will be cumsumed by |
| 1392 | * "commit()" or removed by "cancel()" |
| 1393 | */ |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1394 | int mem_cgroup_try_charge_swapin(struct mm_struct *mm, |
| 1395 | struct page *page, |
| 1396 | gfp_t mask, struct mem_cgroup **ptr) |
| 1397 | { |
| 1398 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1399 | int ret; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1400 | |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1401 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1402 | return 0; |
| 1403 | |
| 1404 | if (!do_swap_account) |
| 1405 | goto charge_cur_mm; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1406 | /* |
| 1407 | * A racing thread's fault, or swapoff, may have already updated |
| 1408 | * the pte, and even removed page from swap cache: return success |
| 1409 | * to go on to do_swap_page()'s pte_same() test, which should fail. |
| 1410 | */ |
| 1411 | if (!PageSwapCache(page)) |
| 1412 | return 0; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1413 | mem = try_get_mem_cgroup_from_swapcache(page); |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1414 | if (!mem) |
| 1415 | goto charge_cur_mm; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1416 | *ptr = mem; |
KAMEZAWA Hiroyuki | 54595fe | 2009-01-07 18:08:33 -0800 | [diff] [blame] | 1417 | ret = __mem_cgroup_try_charge(NULL, mask, ptr, true); |
| 1418 | /* drop extra refcnt from tryget */ |
| 1419 | css_put(&mem->css); |
| 1420 | return ret; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1421 | charge_cur_mm: |
| 1422 | if (unlikely(!mm)) |
| 1423 | mm = &init_mm; |
| 1424 | return __mem_cgroup_try_charge(mm, mask, ptr, true); |
| 1425 | } |
| 1426 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 1427 | static void |
| 1428 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, |
| 1429 | enum charge_type ctype) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1430 | { |
| 1431 | struct page_cgroup *pc; |
| 1432 | |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1433 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1434 | return; |
| 1435 | if (!ptr) |
| 1436 | return; |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 1437 | cgroup_exclude_rmdir(&ptr->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1438 | pc = lookup_page_cgroup(page); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1439 | mem_cgroup_lru_del_before_commit_swapcache(page); |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 1440 | __mem_cgroup_commit_charge(ptr, pc, ctype); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1441 | mem_cgroup_lru_add_after_commit_swapcache(page); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1442 | /* |
| 1443 | * Now swap is on-memory. This means this page may be |
| 1444 | * counted both as mem and swap....double count. |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 1445 | * Fix it by uncharging from memsw. Basically, this SwapCache is stable |
| 1446 | * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page() |
| 1447 | * may call delete_from_swap_cache() before reach here. |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1448 | */ |
KAMEZAWA Hiroyuki | 03f3c43 | 2009-01-07 18:08:31 -0800 | [diff] [blame] | 1449 | if (do_swap_account && PageSwapCache(page)) { |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1450 | swp_entry_t ent = {.val = page_private(page)}; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1451 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1452 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1453 | |
| 1454 | id = swap_cgroup_record(ent, 0); |
| 1455 | rcu_read_lock(); |
| 1456 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1457 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1458 | /* |
| 1459 | * This recorded memcg can be obsolete one. So, avoid |
| 1460 | * calling css_tryget |
| 1461 | */ |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1462 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
| 1463 | mem_cgroup_put(memcg); |
| 1464 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1465 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1466 | } |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 1467 | /* |
| 1468 | * At swapin, we may charge account against cgroup which has no tasks. |
| 1469 | * So, rmdir()->pre_destroy() can be called while we do this charge. |
| 1470 | * In that case, we need to call pre_destroy() again. check it here. |
| 1471 | */ |
| 1472 | cgroup_release_and_wakeup_rmdir(&ptr->css); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1473 | } |
| 1474 | |
Daisuke Nishimura | 83aae4c | 2009-04-02 16:57:48 -0700 | [diff] [blame] | 1475 | void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr) |
| 1476 | { |
| 1477 | __mem_cgroup_commit_charge_swapin(page, ptr, |
| 1478 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 1479 | } |
| 1480 | |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1481 | void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem) |
| 1482 | { |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1483 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1484 | return; |
| 1485 | if (!mem) |
| 1486 | return; |
| 1487 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1488 | if (do_swap_account) |
| 1489 | res_counter_uncharge(&mem->memsw, PAGE_SIZE); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 1490 | css_put(&mem->css); |
| 1491 | } |
| 1492 | |
| 1493 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 1494 | /* |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1495 | * uncharge if !page_mapped(page) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1496 | */ |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1497 | static struct mem_cgroup * |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1498 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype) |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1499 | { |
Hugh Dickins | 8289546 | 2008-03-04 14:29:08 -0800 | [diff] [blame] | 1500 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1501 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 1502 | struct mem_cgroup_per_zone *mz; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1503 | |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1504 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1505 | return NULL; |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 1506 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1507 | if (PageSwapCache(page)) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1508 | return NULL; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1509 | |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 1510 | /* |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 1511 | * Check if our page_cgroup is valid |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 1512 | */ |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1513 | pc = lookup_page_cgroup(page); |
| 1514 | if (unlikely(!pc || !PageCgroupUsed(pc))) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1515 | return NULL; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 1516 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1517 | lock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1518 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1519 | mem = pc->mem_cgroup; |
| 1520 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1521 | if (!PageCgroupUsed(pc)) |
| 1522 | goto unlock_out; |
| 1523 | |
| 1524 | switch (ctype) { |
| 1525 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 1526 | case MEM_CGROUP_CHARGE_TYPE_DROP: |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1527 | if (page_mapped(page)) |
| 1528 | goto unlock_out; |
| 1529 | break; |
| 1530 | case MEM_CGROUP_CHARGE_TYPE_SWAPOUT: |
| 1531 | if (!PageAnon(page)) { /* Shared memory */ |
| 1532 | if (page->mapping && !page_is_file_cache(page)) |
| 1533 | goto unlock_out; |
| 1534 | } else if (page_mapped(page)) /* Anon */ |
| 1535 | goto unlock_out; |
| 1536 | break; |
| 1537 | default: |
| 1538 | break; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1539 | } |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1540 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1541 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
| 1542 | if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)) |
| 1543 | res_counter_uncharge(&mem->memsw, PAGE_SIZE); |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1544 | mem_cgroup_charge_statistics(mem, pc, false); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 1545 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1546 | ClearPageCgroupUsed(pc); |
KAMEZAWA Hiroyuki | 544122e | 2009-01-07 18:08:34 -0800 | [diff] [blame] | 1547 | /* |
| 1548 | * pc->mem_cgroup is not cleared here. It will be accessed when it's |
| 1549 | * freed from LRU. This is safe because uncharged page is expected not |
| 1550 | * to be reused (freed soon). Exception is SwapCache, it's handled by |
| 1551 | * special functions. |
| 1552 | */ |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 1553 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1554 | mz = page_cgroup_zoneinfo(pc); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1555 | unlock_page_cgroup(pc); |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 1556 | |
KAMEZAWA Hiroyuki | a7fe942 | 2009-01-07 18:08:13 -0800 | [diff] [blame] | 1557 | /* at swapout, this memcg will be accessed to record to swap */ |
| 1558 | if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT) |
| 1559 | css_put(&mem->css); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 1560 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1561 | return mem; |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1562 | |
| 1563 | unlock_out: |
| 1564 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1565 | return NULL; |
Balbir Singh | 3c541e1 | 2008-02-07 00:14:41 -0800 | [diff] [blame] | 1566 | } |
| 1567 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1568 | void mem_cgroup_uncharge_page(struct page *page) |
| 1569 | { |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1570 | /* early check. */ |
| 1571 | if (page_mapped(page)) |
| 1572 | return; |
| 1573 | if (page->mapping && !PageAnon(page)) |
| 1574 | return; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1575 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
| 1576 | } |
| 1577 | |
| 1578 | void mem_cgroup_uncharge_cache_page(struct page *page) |
| 1579 | { |
| 1580 | VM_BUG_ON(page_mapped(page)); |
KAMEZAWA Hiroyuki | b7abea9 | 2008-10-18 20:28:09 -0700 | [diff] [blame] | 1581 | VM_BUG_ON(page->mapping); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1582 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE); |
| 1583 | } |
| 1584 | |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 1585 | #ifdef CONFIG_SWAP |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1586 | /* |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 1587 | * called after __delete_from_swap_cache() and drop "page" account. |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1588 | * memcg information is recorded to swap_cgroup of "ent" |
| 1589 | */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 1590 | void |
| 1591 | mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout) |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1592 | { |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1593 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 1594 | int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1595 | |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 1596 | if (!swapout) /* this was a swap cache but the swap is unused ! */ |
| 1597 | ctype = MEM_CGROUP_CHARGE_TYPE_DROP; |
| 1598 | |
| 1599 | memcg = __mem_cgroup_uncharge_common(page, ctype); |
| 1600 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1601 | /* record memcg information */ |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 1602 | if (do_swap_account && swapout && memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1603 | swap_cgroup_record(ent, css_id(&memcg->css)); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1604 | mem_cgroup_get(memcg); |
| 1605 | } |
KAMEZAWA Hiroyuki | 8a9478c | 2009-06-17 16:27:17 -0700 | [diff] [blame] | 1606 | if (swapout && memcg) |
KAMEZAWA Hiroyuki | a7fe942 | 2009-01-07 18:08:13 -0800 | [diff] [blame] | 1607 | css_put(&memcg->css); |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1608 | } |
Daisuke Nishimura | e767e05 | 2009-05-28 14:34:28 -0700 | [diff] [blame] | 1609 | #endif |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1610 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1611 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 1612 | /* |
| 1613 | * called from swap_entry_free(). remove record in swap_cgroup and |
| 1614 | * uncharge "memsw" account. |
| 1615 | */ |
| 1616 | void mem_cgroup_uncharge_swap(swp_entry_t ent) |
| 1617 | { |
| 1618 | struct mem_cgroup *memcg; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1619 | unsigned short id; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1620 | |
| 1621 | if (!do_swap_account) |
| 1622 | return; |
| 1623 | |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1624 | id = swap_cgroup_record(ent, 0); |
| 1625 | rcu_read_lock(); |
| 1626 | memcg = mem_cgroup_lookup(id); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1627 | if (memcg) { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1628 | /* |
| 1629 | * We uncharge this because swap is freed. |
| 1630 | * This memcg can be obsolete one. We avoid calling css_tryget |
| 1631 | */ |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1632 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
| 1633 | mem_cgroup_put(memcg); |
| 1634 | } |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 1635 | rcu_read_unlock(); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1636 | } |
| 1637 | #endif |
| 1638 | |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 1639 | /* |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1640 | * Before starting migration, account PAGE_SIZE to mem_cgroup that the old |
| 1641 | * page belongs to. |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 1642 | */ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1643 | int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr) |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 1644 | { |
| 1645 | struct page_cgroup *pc; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1646 | struct mem_cgroup *mem = NULL; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1647 | int ret = 0; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 1648 | |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1649 | if (mem_cgroup_disabled()) |
Balbir Singh | 4077960 | 2008-04-04 14:29:59 -0700 | [diff] [blame] | 1650 | return 0; |
| 1651 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1652 | pc = lookup_page_cgroup(page); |
| 1653 | lock_page_cgroup(pc); |
| 1654 | if (PageCgroupUsed(pc)) { |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1655 | mem = pc->mem_cgroup; |
| 1656 | css_get(&mem->css); |
Hugh Dickins | b9c565d | 2008-03-04 14:29:11 -0800 | [diff] [blame] | 1657 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1658 | unlock_page_cgroup(pc); |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1659 | |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1660 | if (mem) { |
Daisuke Nishimura | 3bb4edf | 2009-01-07 18:08:28 -0800 | [diff] [blame] | 1661 | ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false); |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1662 | css_put(&mem->css); |
| 1663 | } |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1664 | *ptr = mem; |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1665 | return ret; |
| 1666 | } |
Hugh Dickins | fb59e9f | 2008-03-04 14:29:16 -0800 | [diff] [blame] | 1667 | |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1668 | /* remove redundant charge if migration failed*/ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1669 | void mem_cgroup_end_migration(struct mem_cgroup *mem, |
| 1670 | struct page *oldpage, struct page *newpage) |
KAMEZAWA Hiroyuki | e8589cc | 2008-07-25 01:47:10 -0700 | [diff] [blame] | 1671 | { |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1672 | struct page *target, *unused; |
| 1673 | struct page_cgroup *pc; |
| 1674 | enum charge_type ctype; |
| 1675 | |
| 1676 | if (!mem) |
| 1677 | return; |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 1678 | cgroup_exclude_rmdir(&mem->css); |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1679 | /* at migration success, oldpage->mapping is NULL. */ |
| 1680 | if (oldpage->mapping) { |
| 1681 | target = oldpage; |
| 1682 | unused = NULL; |
| 1683 | } else { |
| 1684 | target = newpage; |
| 1685 | unused = oldpage; |
| 1686 | } |
| 1687 | |
| 1688 | if (PageAnon(target)) |
| 1689 | ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED; |
| 1690 | else if (page_is_file_cache(target)) |
| 1691 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; |
| 1692 | else |
| 1693 | ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM; |
| 1694 | |
| 1695 | /* unused page is not on radix-tree now. */ |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 1696 | if (unused) |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1697 | __mem_cgroup_uncharge_common(unused, ctype); |
| 1698 | |
| 1699 | pc = lookup_page_cgroup(target); |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1700 | /* |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1701 | * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup. |
| 1702 | * So, double-counting is effectively avoided. |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 1703 | */ |
KAMEZAWA Hiroyuki | 01b1ae6 | 2009-01-07 18:07:50 -0800 | [diff] [blame] | 1704 | __mem_cgroup_commit_charge(mem, pc, ctype); |
| 1705 | |
| 1706 | /* |
| 1707 | * Both of oldpage and newpage are still under lock_page(). |
| 1708 | * Then, we don't have to care about race in radix-tree. |
| 1709 | * But we have to be careful that this page is unmapped or not. |
| 1710 | * |
| 1711 | * There is a case for !page_mapped(). At the start of |
| 1712 | * migration, oldpage was mapped. But now, it's zapped. |
| 1713 | * But we know *target* page is not freed/reused under us. |
| 1714 | * mem_cgroup_uncharge_page() does all necessary checks. |
| 1715 | */ |
| 1716 | if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED) |
| 1717 | mem_cgroup_uncharge_page(target); |
KAMEZAWA Hiroyuki | 8870326 | 2009-07-29 15:04:06 -0700 | [diff] [blame] | 1718 | /* |
| 1719 | * At migration, we may charge account against cgroup which has no tasks |
| 1720 | * So, rmdir()->pre_destroy() can be called while we do this charge. |
| 1721 | * In that case, we need to call pre_destroy() again. check it here. |
| 1722 | */ |
| 1723 | cgroup_release_and_wakeup_rmdir(&mem->css); |
KAMEZAWA Hiroyuki | ae41be3 | 2008-02-07 00:14:10 -0800 | [diff] [blame] | 1724 | } |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 1725 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1726 | /* |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 1727 | * A call to try to shrink memory usage on charge failure at shmem's swapin. |
| 1728 | * Calling hierarchical_reclaim is not enough because we should update |
| 1729 | * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM. |
| 1730 | * Moreover considering hierarchy, we should reclaim from the mem_over_limit, |
| 1731 | * not from the memcg which this page would be charged to. |
| 1732 | * try_charge_swapin does all of these works properly. |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1733 | */ |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 1734 | int mem_cgroup_shmem_charge_fallback(struct page *page, |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1735 | struct mm_struct *mm, |
| 1736 | gfp_t gfp_mask) |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1737 | { |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 1738 | struct mem_cgroup *mem = NULL; |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 1739 | int ret; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1740 | |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 1741 | if (mem_cgroup_disabled()) |
Li Zefan | cede86a | 2008-07-25 01:47:18 -0700 | [diff] [blame] | 1742 | return 0; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1743 | |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 1744 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); |
| 1745 | if (!ret) |
| 1746 | mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */ |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1747 | |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 1748 | return ret; |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1749 | } |
| 1750 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1751 | static DEFINE_MUTEX(set_limit_mutex); |
| 1752 | |
KOSAKI Motohiro | d38d2a7 | 2009-01-06 14:39:44 -0800 | [diff] [blame] | 1753 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1754 | unsigned long long val) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1755 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1756 | int retry_count; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1757 | int progress; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1758 | u64 memswlimit; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1759 | int ret = 0; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1760 | int children = mem_cgroup_count_children(memcg); |
| 1761 | u64 curusage, oldusage; |
| 1762 | |
| 1763 | /* |
| 1764 | * For keeping hierarchical_reclaim simple, how long we should retry |
| 1765 | * is depends on callers. We set our retry-count to be function |
| 1766 | * of # of children which we should visit in this loop. |
| 1767 | */ |
| 1768 | retry_count = MEM_CGROUP_RECLAIM_RETRIES * children; |
| 1769 | |
| 1770 | oldusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1771 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1772 | while (retry_count) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1773 | if (signal_pending(current)) { |
| 1774 | ret = -EINTR; |
| 1775 | break; |
| 1776 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1777 | /* |
| 1778 | * Rather than hide all in some function, I do this in |
| 1779 | * open coded manner. You see what this really does. |
| 1780 | * We have to guarantee mem->res.limit < mem->memsw.limit. |
| 1781 | */ |
| 1782 | mutex_lock(&set_limit_mutex); |
| 1783 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 1784 | if (memswlimit < val) { |
| 1785 | ret = -EINVAL; |
| 1786 | mutex_unlock(&set_limit_mutex); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1787 | break; |
| 1788 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1789 | ret = res_counter_set_limit(&memcg->res, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 1790 | if (!ret) { |
| 1791 | if (memswlimit == val) |
| 1792 | memcg->memsw_is_minimum = true; |
| 1793 | else |
| 1794 | memcg->memsw_is_minimum = false; |
| 1795 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1796 | mutex_unlock(&set_limit_mutex); |
| 1797 | |
| 1798 | if (!ret) |
| 1799 | break; |
| 1800 | |
Daisuke Nishimura | 42e9abb | 2009-01-07 18:08:30 -0800 | [diff] [blame] | 1801 | progress = mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1802 | false, true); |
| 1803 | curusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
| 1804 | /* Usage is reduced ? */ |
| 1805 | if (curusage >= oldusage) |
| 1806 | retry_count--; |
| 1807 | else |
| 1808 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1809 | } |
KOSAKI Motohiro | 14797e2 | 2009-01-07 18:08:18 -0800 | [diff] [blame] | 1810 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1811 | return ret; |
| 1812 | } |
| 1813 | |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 1814 | static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg, |
| 1815 | unsigned long long val) |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1816 | { |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1817 | int retry_count; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1818 | u64 memlimit, oldusage, curusage; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1819 | int children = mem_cgroup_count_children(memcg); |
| 1820 | int ret = -EBUSY; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1821 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1822 | /* see mem_cgroup_resize_res_limit */ |
| 1823 | retry_count = children * MEM_CGROUP_RECLAIM_RETRIES; |
| 1824 | oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1825 | while (retry_count) { |
| 1826 | if (signal_pending(current)) { |
| 1827 | ret = -EINTR; |
| 1828 | break; |
| 1829 | } |
| 1830 | /* |
| 1831 | * Rather than hide all in some function, I do this in |
| 1832 | * open coded manner. You see what this really does. |
| 1833 | * We have to guarantee mem->res.limit < mem->memsw.limit. |
| 1834 | */ |
| 1835 | mutex_lock(&set_limit_mutex); |
| 1836 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 1837 | if (memlimit > val) { |
| 1838 | ret = -EINVAL; |
| 1839 | mutex_unlock(&set_limit_mutex); |
| 1840 | break; |
| 1841 | } |
| 1842 | ret = res_counter_set_limit(&memcg->memsw, val); |
KAMEZAWA Hiroyuki | 22a668d | 2009-06-17 16:27:19 -0700 | [diff] [blame] | 1843 | if (!ret) { |
| 1844 | if (memlimit == val) |
| 1845 | memcg->memsw_is_minimum = true; |
| 1846 | else |
| 1847 | memcg->memsw_is_minimum = false; |
| 1848 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1849 | mutex_unlock(&set_limit_mutex); |
| 1850 | |
| 1851 | if (!ret) |
| 1852 | break; |
| 1853 | |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1854 | mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, true, true); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1855 | curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1856 | /* Usage is reduced ? */ |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 1857 | if (curusage >= oldusage) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1858 | retry_count--; |
KAMEZAWA Hiroyuki | 81d39c2 | 2009-04-02 16:57:36 -0700 | [diff] [blame] | 1859 | else |
| 1860 | oldusage = curusage; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 1861 | } |
| 1862 | return ret; |
| 1863 | } |
| 1864 | |
KAMEZAWA Hiroyuki | c9b0ed5 | 2008-07-25 01:47:15 -0700 | [diff] [blame] | 1865 | /* |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1866 | * This routine traverse page_cgroup in given list and drop them all. |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1867 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
| 1868 | */ |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1869 | static int mem_cgroup_force_empty_list(struct mem_cgroup *mem, |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1870 | int node, int zid, enum lru_list lru) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1871 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1872 | struct zone *zone; |
| 1873 | struct mem_cgroup_per_zone *mz; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1874 | struct page_cgroup *pc, *busy; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1875 | unsigned long flags, loop; |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 1876 | struct list_head *list; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1877 | int ret = 0; |
KAMEZAWA Hiroyuki | 072c56c | 2008-02-07 00:14:39 -0800 | [diff] [blame] | 1878 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1879 | zone = &NODE_DATA(node)->node_zones[zid]; |
| 1880 | mz = mem_cgroup_zoneinfo(mem, node, zid); |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1881 | list = &mz->lists[lru]; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1882 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1883 | loop = MEM_CGROUP_ZSTAT(mz, lru); |
| 1884 | /* give some margin against EBUSY etc...*/ |
| 1885 | loop += 256; |
| 1886 | busy = NULL; |
| 1887 | while (loop--) { |
| 1888 | ret = 0; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1889 | spin_lock_irqsave(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1890 | if (list_empty(list)) { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1891 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1892 | break; |
| 1893 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1894 | pc = list_entry(list->prev, struct page_cgroup, lru); |
| 1895 | if (busy == pc) { |
| 1896 | list_move(&pc->lru, list); |
| 1897 | busy = 0; |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1898 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1899 | continue; |
| 1900 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1901 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1902 | |
KAMEZAWA Hiroyuki | 2c26fdd | 2009-01-07 18:08:10 -0800 | [diff] [blame] | 1903 | ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1904 | if (ret == -ENOMEM) |
| 1905 | break; |
| 1906 | |
| 1907 | if (ret == -EBUSY || ret == -EINVAL) { |
| 1908 | /* found lock contention or "pc" is obsolete. */ |
| 1909 | busy = pc; |
| 1910 | cond_resched(); |
| 1911 | } else |
| 1912 | busy = NULL; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1913 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1914 | |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1915 | if (!ret && !list_empty(list)) |
| 1916 | return -EBUSY; |
| 1917 | return ret; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | /* |
| 1921 | * make mem_cgroup's charge to be 0 if there is no task. |
| 1922 | * This enables deleting this mem_cgroup. |
| 1923 | */ |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1924 | 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] | 1925 | { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1926 | int ret; |
| 1927 | int node, zid, shrink; |
| 1928 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1929 | struct cgroup *cgrp = mem->css.cgroup; |
Hugh Dickins | 8869b8f | 2008-03-04 14:29:09 -0800 | [diff] [blame] | 1930 | |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1931 | css_get(&mem->css); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1932 | |
| 1933 | shrink = 0; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1934 | /* should free all ? */ |
| 1935 | if (free_all) |
| 1936 | goto try_to_free; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1937 | move_account: |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1938 | while (mem->res.usage > 0) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1939 | ret = -EBUSY; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1940 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children)) |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1941 | goto out; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1942 | ret = -EINTR; |
| 1943 | if (signal_pending(current)) |
| 1944 | goto out; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1945 | /* This is for making all *used* pages to be on LRU. */ |
| 1946 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1947 | ret = 0; |
KAMEZAWA Hiroyuki | 299b4ea | 2009-01-29 14:25:17 -0800 | [diff] [blame] | 1948 | for_each_node_state(node, N_HIGH_MEMORY) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1949 | for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) { |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 1950 | enum lru_list l; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1951 | for_each_lru(l) { |
| 1952 | ret = mem_cgroup_force_empty_list(mem, |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1953 | node, zid, l); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1954 | if (ret) |
| 1955 | break; |
| 1956 | } |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 1957 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1958 | if (ret) |
| 1959 | break; |
| 1960 | } |
| 1961 | /* it seems parent cgroup doesn't have enough mem */ |
| 1962 | if (ret == -ENOMEM) |
| 1963 | goto try_to_free; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 1964 | cond_resched(); |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 1965 | } |
| 1966 | ret = 0; |
| 1967 | out: |
| 1968 | css_put(&mem->css); |
| 1969 | return ret; |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1970 | |
| 1971 | try_to_free: |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1972 | /* returns EBUSY if there is a task or if we come here twice. */ |
| 1973 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1974 | ret = -EBUSY; |
| 1975 | goto out; |
| 1976 | } |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1977 | /* we call try-to-free pages for make this cgroup empty */ |
| 1978 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1979 | /* try to free all pages in this cgroup */ |
| 1980 | shrink = 1; |
| 1981 | while (nr_retries && mem->res.usage > 0) { |
| 1982 | int progress; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1983 | |
| 1984 | if (signal_pending(current)) { |
| 1985 | ret = -EINTR; |
| 1986 | goto out; |
| 1987 | } |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 1988 | progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL, |
| 1989 | false, get_swappiness(mem)); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1990 | if (!progress) { |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1991 | nr_retries--; |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1992 | /* maybe some writeback is necessary */ |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 1993 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 1994 | } |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1995 | |
| 1996 | } |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 1997 | lru_add_drain(); |
KAMEZAWA Hiroyuki | f817ed4 | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 1998 | /* try move_account...there may be some *locked* pages. */ |
| 1999 | if (mem->res.usage) |
| 2000 | goto move_account; |
| 2001 | ret = 0; |
| 2002 | goto out; |
KAMEZAWA Hiroyuki | cc84758 | 2008-02-07 00:14:16 -0800 | [diff] [blame] | 2003 | } |
| 2004 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 2005 | int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event) |
| 2006 | { |
| 2007 | return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true); |
| 2008 | } |
| 2009 | |
| 2010 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 2011 | static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft) |
| 2012 | { |
| 2013 | return mem_cgroup_from_cont(cont)->use_hierarchy; |
| 2014 | } |
| 2015 | |
| 2016 | static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft, |
| 2017 | u64 val) |
| 2018 | { |
| 2019 | int retval = 0; |
| 2020 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
| 2021 | struct cgroup *parent = cont->parent; |
| 2022 | struct mem_cgroup *parent_mem = NULL; |
| 2023 | |
| 2024 | if (parent) |
| 2025 | parent_mem = mem_cgroup_from_cont(parent); |
| 2026 | |
| 2027 | cgroup_lock(); |
| 2028 | /* |
| 2029 | * If parent's use_hiearchy is set, we can't make any modifications |
| 2030 | * in the child subtrees. If it is unset, then the change can |
| 2031 | * occur, provided the current cgroup has no children. |
| 2032 | * |
| 2033 | * For the root cgroup, parent_mem is NULL, we allow value to be |
| 2034 | * set if there are no children. |
| 2035 | */ |
| 2036 | if ((!parent_mem || !parent_mem->use_hierarchy) && |
| 2037 | (val == 1 || val == 0)) { |
| 2038 | if (list_empty(&cont->children)) |
| 2039 | mem->use_hierarchy = val; |
| 2040 | else |
| 2041 | retval = -EBUSY; |
| 2042 | } else |
| 2043 | retval = -EINVAL; |
| 2044 | cgroup_unlock(); |
| 2045 | |
| 2046 | return retval; |
| 2047 | } |
| 2048 | |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 2049 | static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2050 | { |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2051 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
| 2052 | u64 val = 0; |
| 2053 | int type, name; |
| 2054 | |
| 2055 | type = MEMFILE_TYPE(cft->private); |
| 2056 | name = MEMFILE_ATTR(cft->private); |
| 2057 | switch (type) { |
| 2058 | case _MEM: |
| 2059 | val = res_counter_read_u64(&mem->res, name); |
| 2060 | break; |
| 2061 | case _MEMSWAP: |
Li Zefan | 338c843 | 2009-06-17 16:27:15 -0700 | [diff] [blame] | 2062 | val = res_counter_read_u64(&mem->memsw, name); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2063 | break; |
| 2064 | default: |
| 2065 | BUG(); |
| 2066 | break; |
| 2067 | } |
| 2068 | return val; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2069 | } |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 2070 | /* |
| 2071 | * The user of this function is... |
| 2072 | * RES_LIMIT. |
| 2073 | */ |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 2074 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
| 2075 | const char *buffer) |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2076 | { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 2077 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2078 | int type, name; |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 2079 | unsigned long long val; |
| 2080 | int ret; |
| 2081 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2082 | type = MEMFILE_TYPE(cft->private); |
| 2083 | name = MEMFILE_ATTR(cft->private); |
| 2084 | switch (name) { |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 2085 | case RES_LIMIT: |
| 2086 | /* This function does all necessary parse...reuse it */ |
| 2087 | ret = res_counter_memparse_write_strategy(buffer, &val); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2088 | if (ret) |
| 2089 | break; |
| 2090 | if (type == _MEM) |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 2091 | ret = mem_cgroup_resize_limit(memcg, val); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2092 | else |
| 2093 | ret = mem_cgroup_resize_memsw_limit(memcg, val); |
KAMEZAWA Hiroyuki | 628f423 | 2008-07-25 01:47:20 -0700 | [diff] [blame] | 2094 | break; |
| 2095 | default: |
| 2096 | ret = -EINVAL; /* should be BUG() ? */ |
| 2097 | break; |
| 2098 | } |
| 2099 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2100 | } |
| 2101 | |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 2102 | static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg, |
| 2103 | unsigned long long *mem_limit, unsigned long long *memsw_limit) |
| 2104 | { |
| 2105 | struct cgroup *cgroup; |
| 2106 | unsigned long long min_limit, min_memsw_limit, tmp; |
| 2107 | |
| 2108 | min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 2109 | min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 2110 | cgroup = memcg->css.cgroup; |
| 2111 | if (!memcg->use_hierarchy) |
| 2112 | goto out; |
| 2113 | |
| 2114 | while (cgroup->parent) { |
| 2115 | cgroup = cgroup->parent; |
| 2116 | memcg = mem_cgroup_from_cont(cgroup); |
| 2117 | if (!memcg->use_hierarchy) |
| 2118 | break; |
| 2119 | tmp = res_counter_read_u64(&memcg->res, RES_LIMIT); |
| 2120 | min_limit = min(min_limit, tmp); |
| 2121 | tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
| 2122 | min_memsw_limit = min(min_memsw_limit, tmp); |
| 2123 | } |
| 2124 | out: |
| 2125 | *mem_limit = min_limit; |
| 2126 | *memsw_limit = min_memsw_limit; |
| 2127 | return; |
| 2128 | } |
| 2129 | |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 2130 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 2131 | { |
| 2132 | struct mem_cgroup *mem; |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2133 | int type, name; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 2134 | |
| 2135 | mem = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2136 | type = MEMFILE_TYPE(event); |
| 2137 | name = MEMFILE_ATTR(event); |
| 2138 | switch (name) { |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 2139 | case RES_MAX_USAGE: |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2140 | if (type == _MEM) |
| 2141 | res_counter_reset_max(&mem->res); |
| 2142 | else |
| 2143 | res_counter_reset_max(&mem->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 2144 | break; |
| 2145 | case RES_FAILCNT: |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2146 | if (type == _MEM) |
| 2147 | res_counter_reset_failcnt(&mem->res); |
| 2148 | else |
| 2149 | res_counter_reset_failcnt(&mem->memsw); |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 2150 | break; |
| 2151 | } |
Pavel Emelyanov | 85cc59d | 2008-04-29 01:00:20 -0700 | [diff] [blame] | 2152 | return 0; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 2153 | } |
| 2154 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 2155 | |
| 2156 | /* For read statistics */ |
| 2157 | enum { |
| 2158 | MCS_CACHE, |
| 2159 | MCS_RSS, |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2160 | MCS_MAPPED_FILE, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 2161 | MCS_PGPGIN, |
| 2162 | MCS_PGPGOUT, |
| 2163 | MCS_INACTIVE_ANON, |
| 2164 | MCS_ACTIVE_ANON, |
| 2165 | MCS_INACTIVE_FILE, |
| 2166 | MCS_ACTIVE_FILE, |
| 2167 | MCS_UNEVICTABLE, |
| 2168 | NR_MCS_STAT, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 2169 | }; |
| 2170 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 2171 | struct mcs_total_stat { |
| 2172 | s64 stat[NR_MCS_STAT]; |
| 2173 | }; |
| 2174 | |
| 2175 | struct { |
| 2176 | char *local_name; |
| 2177 | char *total_name; |
| 2178 | } memcg_stat_strings[NR_MCS_STAT] = { |
| 2179 | {"cache", "total_cache"}, |
| 2180 | {"rss", "total_rss"}, |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2181 | {"mapped_file", "total_mapped_file"}, |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 2182 | {"pgpgin", "total_pgpgin"}, |
| 2183 | {"pgpgout", "total_pgpgout"}, |
| 2184 | {"inactive_anon", "total_inactive_anon"}, |
| 2185 | {"active_anon", "total_active_anon"}, |
| 2186 | {"inactive_file", "total_inactive_file"}, |
| 2187 | {"active_file", "total_active_file"}, |
| 2188 | {"unevictable", "total_unevictable"} |
| 2189 | }; |
| 2190 | |
| 2191 | |
| 2192 | static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data) |
| 2193 | { |
| 2194 | struct mcs_total_stat *s = data; |
| 2195 | s64 val; |
| 2196 | |
| 2197 | /* per cpu stat */ |
| 2198 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE); |
| 2199 | s->stat[MCS_CACHE] += val * PAGE_SIZE; |
| 2200 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS); |
| 2201 | s->stat[MCS_RSS] += val * PAGE_SIZE; |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 2202 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE); |
| 2203 | s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE; |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 2204 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT); |
| 2205 | s->stat[MCS_PGPGIN] += val; |
| 2206 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT); |
| 2207 | s->stat[MCS_PGPGOUT] += val; |
| 2208 | |
| 2209 | /* per zone stat */ |
| 2210 | val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON); |
| 2211 | s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE; |
| 2212 | val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON); |
| 2213 | s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE; |
| 2214 | val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE); |
| 2215 | s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE; |
| 2216 | val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE); |
| 2217 | s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE; |
| 2218 | val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE); |
| 2219 | s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE; |
| 2220 | return 0; |
| 2221 | } |
| 2222 | |
| 2223 | static void |
| 2224 | mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s) |
| 2225 | { |
| 2226 | mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat); |
| 2227 | } |
| 2228 | |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 2229 | static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft, |
| 2230 | struct cgroup_map_cb *cb) |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 2231 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 2232 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 2233 | struct mcs_total_stat mystat; |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 2234 | int i; |
| 2235 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 2236 | memset(&mystat, 0, sizeof(mystat)); |
| 2237 | mem_cgroup_get_local_stat(mem_cont, &mystat); |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 2238 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 2239 | for (i = 0; i < NR_MCS_STAT; i++) |
| 2240 | cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 2241 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 2242 | /* Hierarchical information */ |
KAMEZAWA Hiroyuki | fee7b54 | 2009-01-07 18:08:26 -0800 | [diff] [blame] | 2243 | { |
| 2244 | unsigned long long limit, memsw_limit; |
| 2245 | memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit); |
| 2246 | cb->fill(cb, "hierarchical_memory_limit", limit); |
| 2247 | if (do_swap_account) |
| 2248 | cb->fill(cb, "hierarchical_memsw_limit", memsw_limit); |
| 2249 | } |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 2250 | |
KAMEZAWA Hiroyuki | 14067bb | 2009-04-02 16:57:35 -0700 | [diff] [blame] | 2251 | memset(&mystat, 0, sizeof(mystat)); |
| 2252 | mem_cgroup_get_total_stat(mem_cont, &mystat); |
| 2253 | for (i = 0; i < NR_MCS_STAT; i++) |
| 2254 | cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]); |
| 2255 | |
| 2256 | |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 2257 | #ifdef CONFIG_DEBUG_VM |
KOSAKI Motohiro | c772be9 | 2009-01-07 18:08:25 -0800 | [diff] [blame] | 2258 | cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL)); |
KOSAKI Motohiro | 7f016ee | 2009-01-07 18:08:22 -0800 | [diff] [blame] | 2259 | |
| 2260 | { |
| 2261 | int nid, zid; |
| 2262 | struct mem_cgroup_per_zone *mz; |
| 2263 | unsigned long recent_rotated[2] = {0, 0}; |
| 2264 | unsigned long recent_scanned[2] = {0, 0}; |
| 2265 | |
| 2266 | for_each_online_node(nid) |
| 2267 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 2268 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
| 2269 | |
| 2270 | recent_rotated[0] += |
| 2271 | mz->reclaim_stat.recent_rotated[0]; |
| 2272 | recent_rotated[1] += |
| 2273 | mz->reclaim_stat.recent_rotated[1]; |
| 2274 | recent_scanned[0] += |
| 2275 | mz->reclaim_stat.recent_scanned[0]; |
| 2276 | recent_scanned[1] += |
| 2277 | mz->reclaim_stat.recent_scanned[1]; |
| 2278 | } |
| 2279 | cb->fill(cb, "recent_rotated_anon", recent_rotated[0]); |
| 2280 | cb->fill(cb, "recent_rotated_file", recent_rotated[1]); |
| 2281 | cb->fill(cb, "recent_scanned_anon", recent_scanned[0]); |
| 2282 | cb->fill(cb, "recent_scanned_file", recent_scanned[1]); |
| 2283 | } |
| 2284 | #endif |
| 2285 | |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 2286 | return 0; |
| 2287 | } |
| 2288 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 2289 | static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft) |
| 2290 | { |
| 2291 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 2292 | |
| 2293 | return get_swappiness(memcg); |
| 2294 | } |
| 2295 | |
| 2296 | static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, |
| 2297 | u64 val) |
| 2298 | { |
| 2299 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
| 2300 | struct mem_cgroup *parent; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 2301 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 2302 | if (val > 100) |
| 2303 | return -EINVAL; |
| 2304 | |
| 2305 | if (cgrp->parent == NULL) |
| 2306 | return -EINVAL; |
| 2307 | |
| 2308 | parent = mem_cgroup_from_cont(cgrp->parent); |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 2309 | |
| 2310 | cgroup_lock(); |
| 2311 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 2312 | /* If under hierarchy, only empty-root can set this value */ |
| 2313 | if ((parent->use_hierarchy) || |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 2314 | (memcg->use_hierarchy && !list_empty(&cgrp->children))) { |
| 2315 | cgroup_unlock(); |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 2316 | return -EINVAL; |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 2317 | } |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 2318 | |
| 2319 | spin_lock(&memcg->reclaim_param_lock); |
| 2320 | memcg->swappiness = val; |
| 2321 | spin_unlock(&memcg->reclaim_param_lock); |
| 2322 | |
Li Zefan | 068b38c | 2009-01-15 13:51:26 -0800 | [diff] [blame] | 2323 | cgroup_unlock(); |
| 2324 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 2325 | return 0; |
| 2326 | } |
| 2327 | |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 2328 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2329 | static struct cftype mem_cgroup_files[] = { |
| 2330 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 2331 | .name = "usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2332 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 2333 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2334 | }, |
| 2335 | { |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 2336 | .name = "max_usage_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2337 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 2338 | .trigger = mem_cgroup_reset, |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 2339 | .read_u64 = mem_cgroup_read, |
| 2340 | }, |
| 2341 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 2342 | .name = "limit_in_bytes", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2343 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 2344 | .write_string = mem_cgroup_write, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 2345 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2346 | }, |
| 2347 | { |
| 2348 | .name = "failcnt", |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2349 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 2350 | .trigger = mem_cgroup_reset, |
Paul Menage | 2c3daa7 | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 2351 | .read_u64 = mem_cgroup_read, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2352 | }, |
Balbir Singh | 8697d33 | 2008-02-07 00:13:59 -0800 | [diff] [blame] | 2353 | { |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 2354 | .name = "stat", |
Paul Menage | c64745c | 2008-04-29 01:00:02 -0700 | [diff] [blame] | 2355 | .read_map = mem_control_stat_show, |
KAMEZAWA Hiroyuki | d2ceb9b | 2008-02-07 00:14:25 -0800 | [diff] [blame] | 2356 | }, |
KAMEZAWA Hiroyuki | c1e862c | 2009-01-07 18:07:55 -0800 | [diff] [blame] | 2357 | { |
| 2358 | .name = "force_empty", |
| 2359 | .trigger = mem_cgroup_force_empty_write, |
| 2360 | }, |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 2361 | { |
| 2362 | .name = "use_hierarchy", |
| 2363 | .write_u64 = mem_cgroup_hierarchy_write, |
| 2364 | .read_u64 = mem_cgroup_hierarchy_read, |
| 2365 | }, |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 2366 | { |
| 2367 | .name = "swappiness", |
| 2368 | .read_u64 = mem_cgroup_swappiness_read, |
| 2369 | .write_u64 = mem_cgroup_swappiness_write, |
| 2370 | }, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2371 | }; |
| 2372 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2373 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 2374 | static struct cftype memsw_cgroup_files[] = { |
| 2375 | { |
| 2376 | .name = "memsw.usage_in_bytes", |
| 2377 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), |
| 2378 | .read_u64 = mem_cgroup_read, |
| 2379 | }, |
| 2380 | { |
| 2381 | .name = "memsw.max_usage_in_bytes", |
| 2382 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), |
| 2383 | .trigger = mem_cgroup_reset, |
| 2384 | .read_u64 = mem_cgroup_read, |
| 2385 | }, |
| 2386 | { |
| 2387 | .name = "memsw.limit_in_bytes", |
| 2388 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), |
| 2389 | .write_string = mem_cgroup_write, |
| 2390 | .read_u64 = mem_cgroup_read, |
| 2391 | }, |
| 2392 | { |
| 2393 | .name = "memsw.failcnt", |
| 2394 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), |
| 2395 | .trigger = mem_cgroup_reset, |
| 2396 | .read_u64 = mem_cgroup_read, |
| 2397 | }, |
| 2398 | }; |
| 2399 | |
| 2400 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 2401 | { |
| 2402 | if (!do_swap_account) |
| 2403 | return 0; |
| 2404 | return cgroup_add_files(cont, ss, memsw_cgroup_files, |
| 2405 | ARRAY_SIZE(memsw_cgroup_files)); |
| 2406 | }; |
| 2407 | #else |
| 2408 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) |
| 2409 | { |
| 2410 | return 0; |
| 2411 | } |
| 2412 | #endif |
| 2413 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 2414 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 2415 | { |
| 2416 | struct mem_cgroup_per_node *pn; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 2417 | struct mem_cgroup_per_zone *mz; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 2418 | enum lru_list l; |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 2419 | int zone, tmp = node; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 2420 | /* |
| 2421 | * This routine is called against possible nodes. |
| 2422 | * But it's BUG to call kmalloc() against offline node. |
| 2423 | * |
| 2424 | * TODO: this routine can waste much memory for nodes which will |
| 2425 | * never be onlined. It's better to use memory hotplug callback |
| 2426 | * function. |
| 2427 | */ |
KAMEZAWA Hiroyuki | 41e3355 | 2008-04-08 17:41:54 -0700 | [diff] [blame] | 2428 | if (!node_state(node, N_NORMAL_MEMORY)) |
| 2429 | tmp = -1; |
| 2430 | pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp); |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 2431 | if (!pn) |
| 2432 | return 1; |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 2433 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 2434 | mem->info.nodeinfo[node] = pn; |
| 2435 | memset(pn, 0, sizeof(*pn)); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 2436 | |
| 2437 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
| 2438 | mz = &pn->zoneinfo[zone]; |
Christoph Lameter | b69408e | 2008-10-18 20:26:14 -0700 | [diff] [blame] | 2439 | for_each_lru(l) |
| 2440 | INIT_LIST_HEAD(&mz->lists[l]); |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 2441 | } |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 2442 | return 0; |
| 2443 | } |
| 2444 | |
KAMEZAWA Hiroyuki | 1ecaab2 | 2008-02-07 00:14:38 -0800 | [diff] [blame] | 2445 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
| 2446 | { |
| 2447 | kfree(mem->info.nodeinfo[node]); |
| 2448 | } |
| 2449 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2450 | static int mem_cgroup_size(void) |
| 2451 | { |
| 2452 | int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu); |
| 2453 | return sizeof(struct mem_cgroup) + cpustat_size; |
| 2454 | } |
| 2455 | |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 2456 | static struct mem_cgroup *mem_cgroup_alloc(void) |
| 2457 | { |
| 2458 | struct mem_cgroup *mem; |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2459 | int size = mem_cgroup_size(); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 2460 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2461 | if (size < PAGE_SIZE) |
| 2462 | mem = kmalloc(size, GFP_KERNEL); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 2463 | else |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2464 | mem = vmalloc(size); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 2465 | |
| 2466 | if (mem) |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2467 | memset(mem, 0, size); |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 2468 | return mem; |
| 2469 | } |
| 2470 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2471 | /* |
| 2472 | * At destroying mem_cgroup, references from swap_cgroup can remain. |
| 2473 | * (scanning all at force_empty is too costly...) |
| 2474 | * |
| 2475 | * Instead of clearing all references at force_empty, we remember |
| 2476 | * the number of reference from swap_cgroup and free mem_cgroup when |
| 2477 | * it goes down to 0. |
| 2478 | * |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2479 | * Removal of cgroup itself succeeds regardless of refs from swap. |
| 2480 | */ |
| 2481 | |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 2482 | static void __mem_cgroup_free(struct mem_cgroup *mem) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 2483 | { |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2484 | int node; |
| 2485 | |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 2486 | free_css_id(&mem_cgroup_subsys, &mem->css); |
| 2487 | |
KAMEZAWA Hiroyuki | 08e552c | 2009-01-07 18:08:01 -0800 | [diff] [blame] | 2488 | for_each_node_state(node, N_POSSIBLE) |
| 2489 | free_mem_cgroup_per_zone_info(mem, node); |
| 2490 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2491 | if (mem_cgroup_size() < PAGE_SIZE) |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 2492 | kfree(mem); |
| 2493 | else |
| 2494 | vfree(mem); |
| 2495 | } |
| 2496 | |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2497 | static void mem_cgroup_get(struct mem_cgroup *mem) |
| 2498 | { |
| 2499 | atomic_inc(&mem->refcnt); |
| 2500 | } |
| 2501 | |
| 2502 | static void mem_cgroup_put(struct mem_cgroup *mem) |
| 2503 | { |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 2504 | if (atomic_dec_and_test(&mem->refcnt)) { |
| 2505 | struct mem_cgroup *parent = parent_mem_cgroup(mem); |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 2506 | __mem_cgroup_free(mem); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 2507 | if (parent) |
| 2508 | mem_cgroup_put(parent); |
| 2509 | } |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2510 | } |
| 2511 | |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 2512 | /* |
| 2513 | * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled. |
| 2514 | */ |
| 2515 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem) |
| 2516 | { |
| 2517 | if (!mem->res.parent) |
| 2518 | return NULL; |
| 2519 | return mem_cgroup_from_res_counter(mem->res.parent, res); |
| 2520 | } |
KAMEZAWA Hiroyuki | 3332794 | 2008-04-29 01:00:24 -0700 | [diff] [blame] | 2521 | |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 2522 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 2523 | static void __init enable_swap_cgroup(void) |
| 2524 | { |
Hirokazu Takahashi | f8d6654 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 2525 | if (!mem_cgroup_disabled() && really_do_swap_account) |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 2526 | do_swap_account = 1; |
| 2527 | } |
| 2528 | #else |
| 2529 | static void __init enable_swap_cgroup(void) |
| 2530 | { |
| 2531 | } |
| 2532 | #endif |
| 2533 | |
Li Zefan | 0eb253e | 2009-01-15 13:51:25 -0800 | [diff] [blame] | 2534 | static struct cgroup_subsys_state * __ref |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2535 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
| 2536 | { |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 2537 | struct mem_cgroup *mem, *parent; |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 2538 | long error = -ENOMEM; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 2539 | int node; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2540 | |
Jan Blunck | c8dad2b | 2009-01-07 18:07:53 -0800 | [diff] [blame] | 2541 | mem = mem_cgroup_alloc(); |
| 2542 | if (!mem) |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 2543 | return ERR_PTR(error); |
Pavel Emelianov | 78fb746 | 2008-02-07 00:13:51 -0800 | [diff] [blame] | 2544 | |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 2545 | for_each_node_state(node, N_POSSIBLE) |
| 2546 | if (alloc_mem_cgroup_per_zone_info(mem, node)) |
| 2547 | goto free_out; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 2548 | /* root ? */ |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 2549 | if (cont->parent == NULL) { |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 2550 | enable_swap_cgroup(); |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 2551 | parent = NULL; |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 2552 | } else { |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 2553 | parent = mem_cgroup_from_cont(cont->parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 2554 | mem->use_hierarchy = parent->use_hierarchy; |
| 2555 | } |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 2556 | |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 2557 | if (parent && parent->use_hierarchy) { |
| 2558 | res_counter_init(&mem->res, &parent->res); |
| 2559 | res_counter_init(&mem->memsw, &parent->memsw); |
Daisuke Nishimura | 7bcc1bb | 2009-01-29 14:25:11 -0800 | [diff] [blame] | 2560 | /* |
| 2561 | * We increment refcnt of the parent to ensure that we can |
| 2562 | * safely access it on res_counter_charge/uncharge. |
| 2563 | * This refcnt will be decremented when freeing this |
| 2564 | * mem_cgroup(see mem_cgroup_put). |
| 2565 | */ |
| 2566 | mem_cgroup_get(parent); |
Balbir Singh | 18f59ea | 2009-01-07 18:08:07 -0800 | [diff] [blame] | 2567 | } else { |
| 2568 | res_counter_init(&mem->res, NULL); |
| 2569 | res_counter_init(&mem->memsw, NULL); |
| 2570 | } |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 2571 | mem->last_scanned_child = 0; |
KOSAKI Motohiro | 2733c06 | 2009-01-07 18:08:23 -0800 | [diff] [blame] | 2572 | spin_lock_init(&mem->reclaim_param_lock); |
Balbir Singh | 6d61ef4 | 2009-01-07 18:08:06 -0800 | [diff] [blame] | 2573 | |
KOSAKI Motohiro | a7885eb | 2009-01-07 18:08:24 -0800 | [diff] [blame] | 2574 | if (parent) |
| 2575 | mem->swappiness = get_swappiness(parent); |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 2576 | atomic_set(&mem->refcnt, 1); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2577 | return &mem->css; |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 2578 | free_out: |
KAMEZAWA Hiroyuki | a7ba0ee | 2009-01-07 18:08:32 -0800 | [diff] [blame] | 2579 | __mem_cgroup_free(mem); |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 2580 | return ERR_PTR(error); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2581 | } |
| 2582 | |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 2583 | static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 2584 | struct cgroup *cont) |
| 2585 | { |
| 2586 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
KAMEZAWA Hiroyuki | ec64f51 | 2009-04-02 16:57:26 -0700 | [diff] [blame] | 2587 | |
| 2588 | return mem_cgroup_force_empty(mem, false); |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 2589 | } |
| 2590 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2591 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
| 2592 | struct cgroup *cont) |
| 2593 | { |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 2594 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 2595 | |
Daisuke Nishimura | c268e99 | 2009-01-15 13:51:13 -0800 | [diff] [blame] | 2596 | mem_cgroup_put(mem); |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | static int mem_cgroup_populate(struct cgroup_subsys *ss, |
| 2600 | struct cgroup *cont) |
| 2601 | { |
KAMEZAWA Hiroyuki | 8c7c6e3 | 2009-01-07 18:08:00 -0800 | [diff] [blame] | 2602 | int ret; |
| 2603 | |
| 2604 | ret = cgroup_add_files(cont, ss, mem_cgroup_files, |
| 2605 | ARRAY_SIZE(mem_cgroup_files)); |
| 2606 | |
| 2607 | if (!ret) |
| 2608 | ret = register_memsw_files(cont, ss); |
| 2609 | return ret; |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2610 | } |
| 2611 | |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 2612 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
| 2613 | struct cgroup *cont, |
| 2614 | struct cgroup *old_cont, |
| 2615 | struct task_struct *p) |
| 2616 | { |
Daisuke Nishimura | 7f4d454 | 2009-01-07 18:08:29 -0800 | [diff] [blame] | 2617 | mutex_lock(&memcg_tasklist); |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 2618 | /* |
Nikanth Karthikesan | f9717d2 | 2009-01-07 18:08:11 -0800 | [diff] [blame] | 2619 | * FIXME: It's better to move charges of this process from old |
| 2620 | * memcg to new memcg. But it's just on TODO-List now. |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 2621 | */ |
Daisuke Nishimura | 7f4d454 | 2009-01-07 18:08:29 -0800 | [diff] [blame] | 2622 | mutex_unlock(&memcg_tasklist); |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 2623 | } |
| 2624 | |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2625 | struct cgroup_subsys mem_cgroup_subsys = { |
| 2626 | .name = "memory", |
| 2627 | .subsys_id = mem_cgroup_subsys_id, |
| 2628 | .create = mem_cgroup_create, |
KAMEZAWA Hiroyuki | df878fb | 2008-02-07 00:14:28 -0800 | [diff] [blame] | 2629 | .pre_destroy = mem_cgroup_pre_destroy, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2630 | .destroy = mem_cgroup_destroy, |
| 2631 | .populate = mem_cgroup_populate, |
Balbir Singh | 67e465a | 2008-02-07 00:13:54 -0800 | [diff] [blame] | 2632 | .attach = mem_cgroup_move_task, |
KAMEZAWA Hiroyuki | 6d12e2d | 2008-02-07 00:14:31 -0800 | [diff] [blame] | 2633 | .early_init = 0, |
KAMEZAWA Hiroyuki | 04046e1 | 2009-04-02 16:57:33 -0700 | [diff] [blame] | 2634 | .use_id = 1, |
Balbir Singh | 8cdea7c | 2008-02-07 00:13:50 -0800 | [diff] [blame] | 2635 | }; |
KAMEZAWA Hiroyuki | c077719 | 2009-01-07 18:07:57 -0800 | [diff] [blame] | 2636 | |
| 2637 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
| 2638 | |
| 2639 | static int __init disable_swap_account(char *s) |
| 2640 | { |
| 2641 | really_do_swap_account = 0; |
| 2642 | return 1; |
| 2643 | } |
| 2644 | __setup("noswapaccount", disable_swap_account); |
| 2645 | #endif |