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