blob: 59dd8c1163724c464575bd2acc02dabb10d74c74 [file] [log] [blame]
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001/* memcontrol.c - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -08009 * 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 Emelianov78fb7462008-02-07 00:13:51 -080023#include <linux/mm.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080024#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080025#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080026#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080027#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080028#include <linux/bit_spinlock.h>
29#include <linux/rcupdate.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070030#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080031#include <linux/swap.h>
32#include <linux/spinlock.h>
33#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080034#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070035#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070036#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070037#include <linux/page_cgroup.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080038
Balbir Singh8697d332008-02-07 00:13:59 -080039#include <asm/uaccess.h>
40
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070041struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070042#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh8cdea7c2008-02-07 00:13:50 -080043
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080044#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
45/* Turned on only when memory cgroup is enabled && really_do_swap_account = 0 */
46int do_swap_account __read_mostly;
47static int really_do_swap_account __initdata = 1; /* for remember boot option*/
48#else
49#define do_swap_account (0)
50#endif
51
52
Balbir Singh8cdea7c2008-02-07 00:13:50 -080053/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080054 * Statistics for memory cgroup.
55 */
56enum mem_cgroup_stat_index {
57 /*
58 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
59 */
60 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
61 MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070062 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
63 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080064
65 MEM_CGROUP_STAT_NSTATS,
66};
67
68struct mem_cgroup_stat_cpu {
69 s64 count[MEM_CGROUP_STAT_NSTATS];
70} ____cacheline_aligned_in_smp;
71
72struct mem_cgroup_stat {
Jan Blunckc8dad2b2009-01-07 18:07:53 -080073 struct mem_cgroup_stat_cpu cpustat[0];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080074};
75
76/*
77 * For accounting under irq disable, no need for increment preempt count.
78 */
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -070079static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080080 enum mem_cgroup_stat_index idx, int val)
81{
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -070082 stat->count[idx] += val;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080083}
84
85static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
86 enum mem_cgroup_stat_index idx)
87{
88 int cpu;
89 s64 ret = 0;
90 for_each_possible_cpu(cpu)
91 ret += stat->cpustat[cpu].count[idx];
92 return ret;
93}
94
95/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -080096 * per-zone information in memory controller.
97 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -080098struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -080099 /*
100 * spin_lock to protect the per cgroup LRU
101 */
102 spinlock_t lru_lock;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700103 struct list_head lists[NR_LRU_LISTS];
104 unsigned long count[NR_LRU_LISTS];
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800105};
106/* Macro for accessing counter */
107#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
108
109struct mem_cgroup_per_node {
110 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
111};
112
113struct mem_cgroup_lru_info {
114 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
115};
116
117/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800118 * The memory controller data structure. The memory controller controls both
119 * page cache and RSS per cgroup. We would eventually like to provide
120 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
121 * to help the administrator determine what knobs to tune.
122 *
123 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800124 * we hit the water mark. May be even add a low water mark, such that
125 * no reclaim occurs from a cgroup at it's low water mark, this is
126 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800127 */
128struct mem_cgroup {
129 struct cgroup_subsys_state css;
130 /*
131 * the counter to account for memory usage
132 */
133 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800134 /*
135 * Per cgroup active and inactive list, similar to the
136 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800137 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800138 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800139
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800140 int prev_priority; /* for recording reclaim priority */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800141 /*
Jan Blunckc8dad2b2009-01-07 18:07:53 -0800142 * statistics. This must be placed at the end of memcg.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800143 */
144 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800145};
146
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800147enum charge_type {
148 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
149 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700150 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700151 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800152 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700153 NR_CHARGE_TYPE,
154};
155
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700156/* only for here (for easy reading.) */
157#define PCGF_CACHE (1UL << PCG_CACHE)
158#define PCGF_USED (1UL << PCG_USED)
159#define PCGF_ACTIVE (1UL << PCG_ACTIVE)
160#define PCGF_LOCK (1UL << PCG_LOCK)
161#define PCGF_FILE (1UL << PCG_FILE)
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700162static const unsigned long
163pcg_default_flags[NR_CHARGE_TYPE] = {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700164 PCGF_CACHE | PCGF_FILE | PCGF_USED | PCGF_LOCK, /* File Cache */
165 PCGF_ACTIVE | PCGF_USED | PCGF_LOCK, /* Anon */
166 PCGF_ACTIVE | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
167 0, /* FORCE */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800168};
169
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800170/*
171 * Always modified under lru lock. Then, not necessary to preempt_disable()
172 */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700173static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
174 struct page_cgroup *pc,
175 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800176{
177 int val = (charge)? 1 : -1;
178 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700179 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800180
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800181 VM_BUG_ON(!irqs_disabled());
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700182
183 cpustat = &stat->cpustat[smp_processor_id()];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700184 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700185 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800186 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700187 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700188
189 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700190 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700191 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
192 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700193 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700194 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800195}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800196
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800197static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800198mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
199{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800200 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
201}
202
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800203static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800204page_cgroup_zoneinfo(struct page_cgroup *pc)
205{
206 struct mem_cgroup *mem = pc->mem_cgroup;
207 int nid = page_cgroup_nid(pc);
208 int zid = page_cgroup_zid(pc);
209
210 return mem_cgroup_zoneinfo(mem, nid, zid);
211}
212
213static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700214 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800215{
216 int nid, zid;
217 struct mem_cgroup_per_zone *mz;
218 u64 total = 0;
219
220 for_each_online_node(nid)
221 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
222 mz = mem_cgroup_zoneinfo(mem, nid, zid);
223 total += MEM_CGROUP_ZSTAT(mz, idx);
224 }
225 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800226}
227
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800228static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800229{
230 return container_of(cgroup_subsys_state(cont,
231 mem_cgroup_subsys_id), struct mem_cgroup,
232 css);
233}
234
Balbir Singhcf475ad2008-04-29 01:00:16 -0700235struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800236{
Balbir Singh31a78f22008-09-28 23:09:31 +0100237 /*
238 * mm_update_next_owner() may clear mm->owner to NULL
239 * if it races with swapoff, page migration, etc.
240 * So this can be called with p == NULL.
241 */
242 if (unlikely(!p))
243 return NULL;
244
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800245 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
246 struct mem_cgroup, css);
247}
248
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700249static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
250 struct page_cgroup *pc)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800251{
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700252 int lru = LRU_BASE;
253
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700254 if (PageCgroupUnevictable(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700255 lru = LRU_UNEVICTABLE;
256 else {
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700257 if (PageCgroupActive(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700258 lru += LRU_ACTIVE;
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700259 if (PageCgroupFile(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700260 lru += LRU_FILE;
261 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800262
Christoph Lameterb69408e2008-10-18 20:26:14 -0700263 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800264
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700265 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, false);
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700266 list_del(&pc->lru);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800267}
268
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700269static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800270 struct page_cgroup *pc, bool hot)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800271{
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700272 int lru = LRU_BASE;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800273
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700274 if (PageCgroupUnevictable(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700275 lru = LRU_UNEVICTABLE;
276 else {
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700277 if (PageCgroupActive(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700278 lru += LRU_ACTIVE;
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700279 if (PageCgroupFile(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700280 lru += LRU_FILE;
281 }
Christoph Lameterb69408e2008-10-18 20:26:14 -0700282
283 MEM_CGROUP_ZSTAT(mz, lru) += 1;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800284 if (hot)
285 list_add(&pc->lru, &mz->lists[lru]);
286 else
287 list_add_tail(&pc->lru, &mz->lists[lru]);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700288
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700289 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, true);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800290}
291
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700292static void __mem_cgroup_move_lists(struct page_cgroup *pc, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800293{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800294 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700295 int active = PageCgroupActive(pc);
296 int file = PageCgroupFile(pc);
297 int unevictable = PageCgroupUnevictable(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700298 enum lru_list from = unevictable ? LRU_UNEVICTABLE :
299 (LRU_FILE * !!file + !!active);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800300
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700301 if (lru == from)
302 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700303
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700304 MEM_CGROUP_ZSTAT(mz, from) -= 1;
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700305 /*
306 * However this is done under mz->lru_lock, another flags, which
307 * are not related to LRU, will be modified from out-of-lock.
308 * We have to use atomic set/clear flags.
309 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700310 if (is_unevictable_lru(lru)) {
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700311 ClearPageCgroupActive(pc);
312 SetPageCgroupUnevictable(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700313 } else {
314 if (is_active_lru(lru))
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700315 SetPageCgroupActive(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700316 else
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700317 ClearPageCgroupActive(pc);
318 ClearPageCgroupUnevictable(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700319 }
Christoph Lameterb69408e2008-10-18 20:26:14 -0700320
Christoph Lameterb69408e2008-10-18 20:26:14 -0700321 MEM_CGROUP_ZSTAT(mz, lru) += 1;
322 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800323}
324
David Rientjes4c4a2212008-02-07 00:14:06 -0800325int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
326{
327 int ret;
328
329 task_lock(task);
Hugh Dickinsbd845e32008-03-04 14:29:01 -0800330 ret = task->mm && mm_match_cgroup(task->mm, mem);
David Rientjes4c4a2212008-02-07 00:14:06 -0800331 task_unlock(task);
332 return ret;
333}
334
Balbir Singh66e17072008-02-07 00:13:56 -0800335/*
336 * This routine assumes that the appropriate zone's lru lock is already held
337 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700338void mem_cgroup_move_lists(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800339{
Hugh Dickins427d5412008-03-04 14:29:03 -0800340 struct page_cgroup *pc;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800341 struct mem_cgroup_per_zone *mz;
342 unsigned long flags;
343
Li Zefancede86a2008-07-25 01:47:18 -0700344 if (mem_cgroup_subsys.disabled)
345 return;
346
Hugh Dickins2680eed2008-03-04 14:29:13 -0800347 /*
348 * We cannot lock_page_cgroup while holding zone's lru_lock,
349 * because other holders of lock_page_cgroup can be interrupted
350 * with an attempt to rotate_reclaimable_page. But we cannot
351 * safely get to page_cgroup without it, so just try_lock it:
352 * mem_cgroup_isolate_pages allows for page left on wrong list.
353 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700354 pc = lookup_page_cgroup(page);
355 if (!trylock_page_cgroup(pc))
Balbir Singh66e17072008-02-07 00:13:56 -0800356 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700357 if (pc && PageCgroupUsed(pc)) {
Hugh Dickins2680eed2008-03-04 14:29:13 -0800358 mz = page_cgroup_zoneinfo(pc);
Hugh Dickins2680eed2008-03-04 14:29:13 -0800359 spin_lock_irqsave(&mz->lru_lock, flags);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700360 __mem_cgroup_move_lists(pc, lru);
Hugh Dickins2680eed2008-03-04 14:29:13 -0800361 spin_unlock_irqrestore(&mz->lru_lock, flags);
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800362 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700363 unlock_page_cgroup(pc);
Balbir Singh66e17072008-02-07 00:13:56 -0800364}
365
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800366/*
367 * Calculate mapped_ratio under memory controller. This will be used in
368 * vmscan.c for deteremining we have to reclaim mapped pages.
369 */
370int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
371{
372 long total, rss;
373
374 /*
375 * usage is recorded in bytes. But, here, we assume the number of
376 * physical pages can be represented by "long" on any arch.
377 */
378 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
379 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
380 return (int)((rss * 100L) / total);
381}
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800382
KAMEZAWA Hiroyuki5932f362008-02-07 00:14:33 -0800383/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800384 * prev_priority control...this will be used in memory reclaim path.
385 */
386int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
387{
388 return mem->prev_priority;
389}
390
391void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
392{
393 if (priority < mem->prev_priority)
394 mem->prev_priority = priority;
395}
396
397void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
398{
399 mem->prev_priority = priority;
400}
401
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800402/*
403 * Calculate # of pages to be scanned in this priority/zone.
404 * See also vmscan.c
405 *
406 * priority starts from "DEF_PRIORITY" and decremented in each loop.
407 * (see include/linux/mmzone.h)
408 */
409
Christoph Lameterb69408e2008-10-18 20:26:14 -0700410long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
411 int priority, enum lru_list lru)
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800412{
Christoph Lameterb69408e2008-10-18 20:26:14 -0700413 long nr_pages;
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800414 int nid = zone->zone_pgdat->node_id;
415 int zid = zone_idx(zone);
416 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
417
Christoph Lameterb69408e2008-10-18 20:26:14 -0700418 nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800419
Christoph Lameterb69408e2008-10-18 20:26:14 -0700420 return (nr_pages >> priority);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800421}
422
Balbir Singh66e17072008-02-07 00:13:56 -0800423unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
424 struct list_head *dst,
425 unsigned long *scanned, int order,
426 int mode, struct zone *z,
427 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700428 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800429{
430 unsigned long nr_taken = 0;
431 struct page *page;
432 unsigned long scan;
433 LIST_HEAD(pc_list);
434 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800435 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800436 int nid = z->zone_pgdat->node_id;
437 int zid = zone_idx(z);
438 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700439 int lru = LRU_FILE * !!file + !!active;
Balbir Singh66e17072008-02-07 00:13:56 -0800440
Balbir Singhcf475ad2008-04-29 01:00:16 -0700441 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800442 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700443 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800444
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800445 spin_lock(&mz->lru_lock);
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800446 scan = 0;
447 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800448 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800449 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700450 if (unlikely(!PageCgroupUsed(pc)))
451 continue;
Balbir Singh66e17072008-02-07 00:13:56 -0800452 page = pc->page;
Balbir Singh66e17072008-02-07 00:13:56 -0800453
Hugh Dickins436c65412008-02-07 00:14:12 -0800454 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800455 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800456
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700457 /*
458 * TODO: play better with lumpy reclaim, grabbing anything.
459 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700460 if (PageUnevictable(page) ||
461 (PageActive(page) && !active) ||
462 (!PageActive(page) && active)) {
463 __mem_cgroup_move_lists(pc, page_lru(page));
Balbir Singh66e17072008-02-07 00:13:56 -0800464 continue;
465 }
466
Hugh Dickins436c65412008-02-07 00:14:12 -0800467 scan++;
468 list_move(&pc->lru, &pc_list);
Balbir Singh66e17072008-02-07 00:13:56 -0800469
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700470 if (__isolate_lru_page(page, mode, file) == 0) {
Balbir Singh66e17072008-02-07 00:13:56 -0800471 list_move(&page->lru, dst);
472 nr_taken++;
473 }
474 }
475
476 list_splice(&pc_list, src);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800477 spin_unlock(&mz->lru_lock);
Balbir Singh66e17072008-02-07 00:13:56 -0800478
479 *scanned = scan;
480 return nr_taken;
481}
482
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800483/*
484 * Unlike exported interface, "oom" parameter is added. if oom==true,
485 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800486 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800487static int __mem_cgroup_try_charge(struct mm_struct *mm,
488 gfp_t gfp_mask, struct mem_cgroup **memcg, bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800489{
490 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800491 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800492 /*
Hugh Dickins3be91272008-02-07 00:14:19 -0800493 * We always charge the cgroup the mm_struct belongs to.
494 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800495 * thread group leader migrates. It's possible that mm is not
496 * set, if so charge the init_mm (happens for pagecache usage).
497 */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800498 if (likely(!*memcg)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700499 rcu_read_lock();
500 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
Balbir Singh31a78f22008-09-28 23:09:31 +0100501 if (unlikely(!mem)) {
502 rcu_read_unlock();
Balbir Singh31a78f22008-09-28 23:09:31 +0100503 return 0;
504 }
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700505 /*
506 * For every charge from the cgroup, increment reference count
507 */
508 css_get(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800509 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700510 rcu_read_unlock();
511 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800512 mem = *memcg;
513 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700514 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800515
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800516
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700517 while (unlikely(res_counter_charge(&mem->res, PAGE_SIZE))) {
Hugh Dickins3be91272008-02-07 00:14:19 -0800518 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800519 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800520
521 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
Balbir Singh66e17072008-02-07 00:13:56 -0800522 continue;
523
524 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800525 * try_to_free_mem_cgroup_pages() might not give us a full
526 * picture of reclaim. Some pages are reclaimed and might be
527 * moved to swap cache or just unmapped from the cgroup.
528 * Check the limit again to see if the reclaim reduced the
529 * current usage of the cgroup before giving up
530 */
Balbir Singh66e17072008-02-07 00:13:56 -0800531 if (res_counter_check_under_limit(&mem->res))
532 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -0800533
534 if (!nr_retries--) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800535 if (oom)
536 mem_cgroup_out_of_memory(mem, gfp_mask);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800537 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -0800538 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800539 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800540 return 0;
541nomem:
542 css_put(&mem->css);
543 return -ENOMEM;
544}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800545
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800546/**
547 * mem_cgroup_try_charge - get charge of PAGE_SIZE.
548 * @mm: an mm_struct which is charged against. (when *memcg is NULL)
549 * @gfp_mask: gfp_mask for reclaim.
550 * @memcg: a pointer to memory cgroup which is charged against.
551 *
552 * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated
553 * memory cgroup from @mm is got and stored in *memcg.
554 *
555 * Returns 0 if success. -ENOMEM at failure.
556 * This call can invoke OOM-Killer.
557 */
558
559int mem_cgroup_try_charge(struct mm_struct *mm,
560 gfp_t mask, struct mem_cgroup **memcg)
561{
562 return __mem_cgroup_try_charge(mm, mask, memcg, true);
563}
564
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800565/*
566 * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be
567 * USED state. If already USED, uncharge and return.
568 */
569
570static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
571 struct page_cgroup *pc,
572 enum charge_type ctype)
573{
574 struct mem_cgroup_per_zone *mz;
575 unsigned long flags;
576
577 /* try_charge() can return NULL to *memcg, taking care of it. */
578 if (!mem)
579 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700580
581 lock_page_cgroup(pc);
582 if (unlikely(PageCgroupUsed(pc))) {
583 unlock_page_cgroup(pc);
584 res_counter_uncharge(&mem->res, PAGE_SIZE);
585 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800586 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700587 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800588 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700589 /*
590 * If a page is accounted as a page cache, insert to inactive list.
591 * If anon, insert to active list.
592 */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700593 pc->flags = pcg_default_flags[ctype];
Hugh Dickins3be91272008-02-07 00:14:19 -0800594
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800595 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700596
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800597 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800598 __mem_cgroup_add_list(mz, pc, true);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800599 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700600 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800601}
602
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800603/**
604 * mem_cgroup_move_account - move account of the page
605 * @pc: page_cgroup of the page.
606 * @from: mem_cgroup which the page is moved from.
607 * @to: mem_cgroup which the page is moved to. @from != @to.
608 *
609 * The caller must confirm following.
610 * 1. disable irq.
611 * 2. lru_lock of old mem_cgroup(@from) should be held.
612 *
613 * returns 0 at success,
614 * returns -EBUSY when lock is busy or "pc" is unstable.
615 *
616 * This function does "uncharge" from old cgroup but doesn't do "charge" to
617 * new cgroup. It should be done by a caller.
618 */
619
620static int mem_cgroup_move_account(struct page_cgroup *pc,
621 struct mem_cgroup *from, struct mem_cgroup *to)
622{
623 struct mem_cgroup_per_zone *from_mz, *to_mz;
624 int nid, zid;
625 int ret = -EBUSY;
626
627 VM_BUG_ON(!irqs_disabled());
628 VM_BUG_ON(from == to);
629
630 nid = page_cgroup_nid(pc);
631 zid = page_cgroup_zid(pc);
632 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
633 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
634
635
636 if (!trylock_page_cgroup(pc))
637 return ret;
638
639 if (!PageCgroupUsed(pc))
640 goto out;
641
642 if (pc->mem_cgroup != from)
643 goto out;
644
645 if (spin_trylock(&to_mz->lru_lock)) {
646 __mem_cgroup_remove_list(from_mz, pc);
647 css_put(&from->css);
648 res_counter_uncharge(&from->res, PAGE_SIZE);
649 pc->mem_cgroup = to;
650 css_get(&to->css);
651 __mem_cgroup_add_list(to_mz, pc, false);
652 ret = 0;
653 spin_unlock(&to_mz->lru_lock);
654 }
655out:
656 unlock_page_cgroup(pc);
657 return ret;
658}
659
660/*
661 * move charges to its parent.
662 */
663
664static int mem_cgroup_move_parent(struct page_cgroup *pc,
665 struct mem_cgroup *child,
666 gfp_t gfp_mask)
667{
668 struct cgroup *cg = child->css.cgroup;
669 struct cgroup *pcg = cg->parent;
670 struct mem_cgroup *parent;
671 struct mem_cgroup_per_zone *mz;
672 unsigned long flags;
673 int ret;
674
675 /* Is ROOT ? */
676 if (!pcg)
677 return -EINVAL;
678
679 parent = mem_cgroup_from_cont(pcg);
680
681 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
682 if (ret)
683 return ret;
684
685 mz = mem_cgroup_zoneinfo(child,
686 page_cgroup_nid(pc), page_cgroup_zid(pc));
687
688 spin_lock_irqsave(&mz->lru_lock, flags);
689 ret = mem_cgroup_move_account(pc, child, parent);
690 spin_unlock_irqrestore(&mz->lru_lock, flags);
691
692 /* drop extra refcnt */
693 css_put(&parent->css);
694 /* uncharge if move fails */
695 if (ret)
696 res_counter_uncharge(&parent->res, PAGE_SIZE);
697
698 return ret;
699}
700
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800701/*
702 * Charge the memory controller for page usage.
703 * Return
704 * 0 if the charge was successful
705 * < 0 if the cgroup is over its limit
706 */
707static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
708 gfp_t gfp_mask, enum charge_type ctype,
709 struct mem_cgroup *memcg)
710{
711 struct mem_cgroup *mem;
712 struct page_cgroup *pc;
713 int ret;
714
715 pc = lookup_page_cgroup(page);
716 /* can happen at boot */
717 if (unlikely(!pc))
718 return 0;
719 prefetchw(pc);
720
721 mem = memcg;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800722 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800723 if (ret)
724 return ret;
725
726 __mem_cgroup_commit_charge(mem, pc, ctype);
727 return 0;
728}
729
730int mem_cgroup_newpage_charge(struct page *page,
731 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800732{
Li Zefancede86a2008-07-25 01:47:18 -0700733 if (mem_cgroup_subsys.disabled)
734 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700735 if (PageCompound(page))
736 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700737 /*
738 * If already mapped, we don't have to account.
739 * If page cache, page->mapping has address_space.
740 * But page->mapping may have out-of-use anon_vma pointer,
741 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
742 * is NULL.
743 */
744 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
745 return 0;
746 if (unlikely(!mm))
747 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800748 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700749 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800750}
751
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800752int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
753 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -0800754{
Li Zefancede86a2008-07-25 01:47:18 -0700755 if (mem_cgroup_subsys.disabled)
756 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700757 if (PageCompound(page))
758 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700759 /*
760 * Corner case handling. This is called from add_to_page_cache()
761 * in usual. But some FS (shmem) precharges this page before calling it
762 * and call add_to_page_cache() with GFP_NOWAIT.
763 *
764 * For GFP_NOWAIT case, the page may be pre-charged before calling
765 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
766 * charge twice. (It works but has to pay a bit larger cost.)
767 */
768 if (!(gfp_mask & __GFP_WAIT)) {
769 struct page_cgroup *pc;
770
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700771
772 pc = lookup_page_cgroup(page);
773 if (!pc)
774 return 0;
775 lock_page_cgroup(pc);
776 if (PageCgroupUsed(pc)) {
777 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700778 return 0;
779 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700780 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700781 }
782
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700783 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -0800784 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700785
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700786 if (page_is_file_cache(page))
787 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700788 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700789 else
790 return mem_cgroup_charge_common(page, mm, gfp_mask,
791 MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700792}
793
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800794#ifdef CONFIG_SWAP
795int mem_cgroup_cache_charge_swapin(struct page *page,
796 struct mm_struct *mm, gfp_t mask, bool locked)
797{
798 int ret = 0;
799
800 if (mem_cgroup_subsys.disabled)
801 return 0;
802 if (unlikely(!mm))
803 mm = &init_mm;
804 if (!locked)
805 lock_page(page);
806 /*
807 * If not locked, the page can be dropped from SwapCache until
808 * we reach here.
809 */
810 if (PageSwapCache(page)) {
811 ret = mem_cgroup_charge_common(page, mm, mask,
812 MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
813 }
814 if (!locked)
815 unlock_page(page);
816
817 return ret;
818}
819#endif
820
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800821void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
822{
823 struct page_cgroup *pc;
824
825 if (mem_cgroup_subsys.disabled)
826 return;
827 if (!ptr)
828 return;
829 pc = lookup_page_cgroup(page);
830 __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED);
831}
832
833void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
834{
835 if (mem_cgroup_subsys.disabled)
836 return;
837 if (!mem)
838 return;
839 res_counter_uncharge(&mem->res, PAGE_SIZE);
840 css_put(&mem->css);
841}
842
843
Balbir Singh8697d332008-02-07 00:13:59 -0800844/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700845 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800846 */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700847static void
848__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800849{
Hugh Dickins82895462008-03-04 14:29:08 -0800850 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800851 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800852 struct mem_cgroup_per_zone *mz;
Balbir Singh66e17072008-02-07 00:13:56 -0800853 unsigned long flags;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800854
Balbir Singh40779602008-04-04 14:29:59 -0700855 if (mem_cgroup_subsys.disabled)
856 return;
857
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800858 if (PageSwapCache(page))
859 return;
860
Balbir Singh8697d332008-02-07 00:13:59 -0800861 /*
Balbir Singh3c541e12008-02-07 00:14:41 -0800862 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -0800863 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700864 pc = lookup_page_cgroup(page);
865 if (unlikely(!pc || !PageCgroupUsed(pc)))
866 return;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800867
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700868 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800869
870 if (!PageCgroupUsed(pc))
871 goto unlock_out;
872
873 switch (ctype) {
874 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
875 if (page_mapped(page))
876 goto unlock_out;
877 break;
878 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
879 if (!PageAnon(page)) { /* Shared memory */
880 if (page->mapping && !page_is_file_cache(page))
881 goto unlock_out;
882 } else if (page_mapped(page)) /* Anon */
883 goto unlock_out;
884 break;
885 default:
886 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700887 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800888
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700889 ClearPageCgroupUsed(pc);
890 mem = pc->mem_cgroup;
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800891
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700892 mz = page_cgroup_zoneinfo(pc);
893 spin_lock_irqsave(&mz->lru_lock, flags);
894 __mem_cgroup_remove_list(mz, pc);
895 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700896 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800897
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700898 res_counter_uncharge(&mem->res, PAGE_SIZE);
899 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800900
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700901 return;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800902
903unlock_out:
904 unlock_page_cgroup(pc);
905 return;
Balbir Singh3c541e12008-02-07 00:14:41 -0800906}
907
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700908void mem_cgroup_uncharge_page(struct page *page)
909{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700910 /* early check. */
911 if (page_mapped(page))
912 return;
913 if (page->mapping && !PageAnon(page))
914 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700915 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
916}
917
918void mem_cgroup_uncharge_cache_page(struct page *page)
919{
920 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -0700921 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700922 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
923}
924
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800925void mem_cgroup_uncharge_swapcache(struct page *page)
926{
927 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_SWAPOUT);
928}
929
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800930/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800931 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
932 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800933 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800934int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800935{
936 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700937 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700938 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800939
Balbir Singh40779602008-04-04 14:29:59 -0700940 if (mem_cgroup_subsys.disabled)
941 return 0;
942
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700943 pc = lookup_page_cgroup(page);
944 lock_page_cgroup(pc);
945 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700946 mem = pc->mem_cgroup;
947 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800948 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700949 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800950
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700951 if (mem) {
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800952 ret = mem_cgroup_try_charge(NULL, GFP_HIGHUSER_MOVABLE, &mem);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700953 css_put(&mem->css);
954 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800955 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700956 return ret;
957}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800958
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700959/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800960void mem_cgroup_end_migration(struct mem_cgroup *mem,
961 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700962{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800963 struct page *target, *unused;
964 struct page_cgroup *pc;
965 enum charge_type ctype;
966
967 if (!mem)
968 return;
969
970 /* at migration success, oldpage->mapping is NULL. */
971 if (oldpage->mapping) {
972 target = oldpage;
973 unused = NULL;
974 } else {
975 target = newpage;
976 unused = oldpage;
977 }
978
979 if (PageAnon(target))
980 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
981 else if (page_is_file_cache(target))
982 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
983 else
984 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
985
986 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800987 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800988 __mem_cgroup_uncharge_common(unused, ctype);
989
990 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700991 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800992 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
993 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700994 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800995 __mem_cgroup_commit_charge(mem, pc, ctype);
996
997 /*
998 * Both of oldpage and newpage are still under lock_page().
999 * Then, we don't have to care about race in radix-tree.
1000 * But we have to be careful that this page is unmapped or not.
1001 *
1002 * There is a case for !page_mapped(). At the start of
1003 * migration, oldpage was mapped. But now, it's zapped.
1004 * But we know *target* page is not freed/reused under us.
1005 * mem_cgroup_uncharge_page() does all necessary checks.
1006 */
1007 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1008 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001009}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001010
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001011/*
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001012 * A call to try to shrink memory usage under specified resource controller.
1013 * This is typically used for page reclaiming for shmem for reducing side
1014 * effect of page allocation from shmem, which is used by some mem_cgroup.
1015 */
1016int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
1017{
1018 struct mem_cgroup *mem;
1019 int progress = 0;
1020 int retry = MEM_CGROUP_RECLAIM_RETRIES;
1021
Li Zefancede86a2008-07-25 01:47:18 -07001022 if (mem_cgroup_subsys.disabled)
1023 return 0;
Hugh Dickins9623e072008-08-12 15:08:41 -07001024 if (!mm)
1025 return 0;
Li Zefancede86a2008-07-25 01:47:18 -07001026
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001027 rcu_read_lock();
1028 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
Balbir Singh31a78f22008-09-28 23:09:31 +01001029 if (unlikely(!mem)) {
1030 rcu_read_unlock();
1031 return 0;
1032 }
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001033 css_get(&mem->css);
1034 rcu_read_unlock();
1035
1036 do {
1037 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
Daisuke Nishimuraa10cebf2008-09-22 13:57:52 -07001038 progress += res_counter_check_under_limit(&mem->res);
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001039 } while (!progress && --retry);
1040
1041 css_put(&mem->css);
1042 if (!retry)
1043 return -ENOMEM;
1044 return 0;
1045}
1046
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08001047static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
1048 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001049{
1050
1051 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
1052 int progress;
1053 int ret = 0;
1054
1055 while (res_counter_set_limit(&memcg->res, val)) {
1056 if (signal_pending(current)) {
1057 ret = -EINTR;
1058 break;
1059 }
1060 if (!retry_count) {
1061 ret = -EBUSY;
1062 break;
1063 }
KAMEZAWA Hiroyukibced0522009-01-07 18:07:49 -08001064 progress = try_to_free_mem_cgroup_pages(memcg,
1065 GFP_HIGHUSER_MOVABLE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001066 if (!progress)
1067 retry_count--;
1068 }
1069 return ret;
1070}
1071
1072
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001073/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001074 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001075 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1076 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001077static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001078 struct mem_cgroup_per_zone *mz,
Christoph Lameterb69408e2008-10-18 20:26:14 -07001079 enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001080{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001081 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001082 unsigned long flags;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001083 unsigned long loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001084 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001085 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001086
Christoph Lameterb69408e2008-10-18 20:26:14 -07001087 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001088
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001089 loop = MEM_CGROUP_ZSTAT(mz, lru);
1090 /* give some margin against EBUSY etc...*/
1091 loop += 256;
1092 busy = NULL;
1093 while (loop--) {
1094 ret = 0;
1095 spin_lock_irqsave(&mz->lru_lock, flags);
1096 if (list_empty(list)) {
1097 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001098 break;
1099 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001100 pc = list_entry(list->prev, struct page_cgroup, lru);
1101 if (busy == pc) {
1102 list_move(&pc->lru, list);
1103 busy = 0;
1104 spin_unlock_irqrestore(&mz->lru_lock, flags);
1105 continue;
1106 }
1107 spin_unlock_irqrestore(&mz->lru_lock, flags);
1108
1109 ret = mem_cgroup_move_parent(pc, mem, GFP_HIGHUSER_MOVABLE);
1110 if (ret == -ENOMEM)
1111 break;
1112
1113 if (ret == -EBUSY || ret == -EINVAL) {
1114 /* found lock contention or "pc" is obsolete. */
1115 busy = pc;
1116 cond_resched();
1117 } else
1118 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001119 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001120 if (!ret && !list_empty(list))
1121 return -EBUSY;
1122 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001123}
1124
1125/*
1126 * make mem_cgroup's charge to be 0 if there is no task.
1127 * This enables deleting this mem_cgroup.
1128 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001129static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001130{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001131 int ret;
1132 int node, zid, shrink;
1133 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001134 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001135
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001136 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001137
1138 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001139 /* should free all ? */
1140 if (free_all)
1141 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001142move_account:
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001143 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001144 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001145 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001146 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001147 ret = -EINTR;
1148 if (signal_pending(current))
1149 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001150 /* This is for making all *used* pages to be on LRU. */
1151 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001152 ret = 0;
1153 for_each_node_state(node, N_POSSIBLE) {
1154 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001155 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07001156 enum lru_list l;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001157 mz = mem_cgroup_zoneinfo(mem, node, zid);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001158 for_each_lru(l) {
1159 ret = mem_cgroup_force_empty_list(mem,
1160 mz, l);
1161 if (ret)
1162 break;
1163 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001164 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001165 if (ret)
1166 break;
1167 }
1168 /* it seems parent cgroup doesn't have enough mem */
1169 if (ret == -ENOMEM)
1170 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001171 cond_resched();
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001172 }
1173 ret = 0;
1174out:
1175 css_put(&mem->css);
1176 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001177
1178try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001179 /* returns EBUSY if there is a task or if we come here twice. */
1180 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001181 ret = -EBUSY;
1182 goto out;
1183 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001184 /* we call try-to-free pages for make this cgroup empty */
1185 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001186 /* try to free all pages in this cgroup */
1187 shrink = 1;
1188 while (nr_retries && mem->res.usage > 0) {
1189 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001190
1191 if (signal_pending(current)) {
1192 ret = -EINTR;
1193 goto out;
1194 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001195 progress = try_to_free_mem_cgroup_pages(mem,
1196 GFP_HIGHUSER_MOVABLE);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001197 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001198 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001199 /* maybe some writeback is necessary */
1200 congestion_wait(WRITE, HZ/10);
1201 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001202
1203 }
1204 /* try move_account...there may be some *locked* pages. */
1205 if (mem->res.usage)
1206 goto move_account;
1207 ret = 0;
1208 goto out;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001209}
1210
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001211int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
1212{
1213 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
1214}
1215
1216
Paul Menage2c3daa72008-04-29 00:59:58 -07001217static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001218{
Paul Menage2c3daa72008-04-29 00:59:58 -07001219 return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
1220 cft->private);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001221}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001222/*
1223 * The user of this function is...
1224 * RES_LIMIT.
1225 */
Paul Menage856c13a2008-07-25 01:47:04 -07001226static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
1227 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001228{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001229 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
1230 unsigned long long val;
1231 int ret;
1232
1233 switch (cft->private) {
1234 case RES_LIMIT:
1235 /* This function does all necessary parse...reuse it */
1236 ret = res_counter_memparse_write_strategy(buffer, &val);
1237 if (!ret)
1238 ret = mem_cgroup_resize_limit(memcg, val);
1239 break;
1240 default:
1241 ret = -EINVAL; /* should be BUG() ? */
1242 break;
1243 }
1244 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001245}
1246
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001247static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001248{
1249 struct mem_cgroup *mem;
1250
1251 mem = mem_cgroup_from_cont(cont);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001252 switch (event) {
1253 case RES_MAX_USAGE:
1254 res_counter_reset_max(&mem->res);
1255 break;
1256 case RES_FAILCNT:
1257 res_counter_reset_failcnt(&mem->res);
1258 break;
1259 }
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07001260 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001261}
1262
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001263static const struct mem_cgroup_stat_desc {
1264 const char *msg;
1265 u64 unit;
1266} mem_cgroup_stat_desc[] = {
1267 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
1268 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
Balaji Rao55e462b2008-05-01 04:35:12 -07001269 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
1270 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001271};
1272
Paul Menagec64745c2008-04-29 01:00:02 -07001273static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
1274 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001275{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001276 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
1277 struct mem_cgroup_stat *stat = &mem_cont->stat;
1278 int i;
1279
1280 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
1281 s64 val;
1282
1283 val = mem_cgroup_read_stat(stat, i);
1284 val *= mem_cgroup_stat_desc[i].unit;
Paul Menagec64745c2008-04-29 01:00:02 -07001285 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001286 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001287 /* showing # of active pages */
1288 {
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001289 unsigned long active_anon, inactive_anon;
1290 unsigned long active_file, inactive_file;
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001291 unsigned long unevictable;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001292
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001293 inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
1294 LRU_INACTIVE_ANON);
1295 active_anon = mem_cgroup_get_all_zonestat(mem_cont,
1296 LRU_ACTIVE_ANON);
1297 inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
1298 LRU_INACTIVE_FILE);
1299 active_file = mem_cgroup_get_all_zonestat(mem_cont,
1300 LRU_ACTIVE_FILE);
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001301 unevictable = mem_cgroup_get_all_zonestat(mem_cont,
1302 LRU_UNEVICTABLE);
1303
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001304 cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
1305 cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
1306 cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
1307 cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001308 cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
1309
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001310 }
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001311 return 0;
1312}
1313
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001314
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001315static struct cftype mem_cgroup_files[] = {
1316 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001317 .name = "usage_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001318 .private = RES_USAGE,
Paul Menage2c3daa72008-04-29 00:59:58 -07001319 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001320 },
1321 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001322 .name = "max_usage_in_bytes",
1323 .private = RES_MAX_USAGE,
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001324 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001325 .read_u64 = mem_cgroup_read,
1326 },
1327 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001328 .name = "limit_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001329 .private = RES_LIMIT,
Paul Menage856c13a2008-07-25 01:47:04 -07001330 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07001331 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001332 },
1333 {
1334 .name = "failcnt",
1335 .private = RES_FAILCNT,
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001336 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07001337 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001338 },
Balbir Singh8697d332008-02-07 00:13:59 -08001339 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001340 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07001341 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001342 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001343 {
1344 .name = "force_empty",
1345 .trigger = mem_cgroup_force_empty_write,
1346 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001347};
1348
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001349static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1350{
1351 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001352 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07001353 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001354 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001355 /*
1356 * This routine is called against possible nodes.
1357 * But it's BUG to call kmalloc() against offline node.
1358 *
1359 * TODO: this routine can waste much memory for nodes which will
1360 * never be onlined. It's better to use memory hotplug callback
1361 * function.
1362 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001363 if (!node_state(node, N_NORMAL_MEMORY))
1364 tmp = -1;
1365 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001366 if (!pn)
1367 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001368
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001369 mem->info.nodeinfo[node] = pn;
1370 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001371
1372 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1373 mz = &pn->zoneinfo[zone];
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001374 spin_lock_init(&mz->lru_lock);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001375 for_each_lru(l)
1376 INIT_LIST_HEAD(&mz->lists[l]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001377 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001378 return 0;
1379}
1380
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001381static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1382{
1383 kfree(mem->info.nodeinfo[node]);
1384}
1385
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001386static int mem_cgroup_size(void)
1387{
1388 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
1389 return sizeof(struct mem_cgroup) + cpustat_size;
1390}
1391
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001392static struct mem_cgroup *mem_cgroup_alloc(void)
1393{
1394 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001395 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001396
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001397 if (size < PAGE_SIZE)
1398 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001399 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001400 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001401
1402 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001403 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001404 return mem;
1405}
1406
1407static void mem_cgroup_free(struct mem_cgroup *mem)
1408{
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001409 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001410 kfree(mem);
1411 else
1412 vfree(mem);
1413}
1414
1415
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08001416#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1417static void __init enable_swap_cgroup(void)
1418{
1419 if (!mem_cgroup_subsys.disabled && really_do_swap_account)
1420 do_swap_account = 1;
1421}
1422#else
1423static void __init enable_swap_cgroup(void)
1424{
1425}
1426#endif
1427
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001428static struct cgroup_subsys_state *
1429mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1430{
1431 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001432 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001433
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001434 mem = mem_cgroup_alloc();
1435 if (!mem)
1436 return ERR_PTR(-ENOMEM);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001437
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001438 res_counter_init(&mem->res);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001439
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001440 for_each_node_state(node, N_POSSIBLE)
1441 if (alloc_mem_cgroup_per_zone_info(mem, node))
1442 goto free_out;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08001443 /* root ? */
1444 if (cont->parent == NULL)
1445 enable_swap_cgroup();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001446
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001447 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001448free_out:
1449 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001450 free_mem_cgroup_per_zone_info(mem, node);
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001451 mem_cgroup_free(mem);
Li Zefan2dda81c2008-02-23 15:24:14 -08001452 return ERR_PTR(-ENOMEM);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001453}
1454
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001455static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1456 struct cgroup *cont)
1457{
1458 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001459 mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001460}
1461
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001462static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1463 struct cgroup *cont)
1464{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001465 int node;
1466 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1467
1468 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001469 free_mem_cgroup_per_zone_info(mem, node);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001470
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001471 mem_cgroup_free(mem_cgroup_from_cont(cont));
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001472}
1473
1474static int mem_cgroup_populate(struct cgroup_subsys *ss,
1475 struct cgroup *cont)
1476{
1477 return cgroup_add_files(cont, ss, mem_cgroup_files,
1478 ARRAY_SIZE(mem_cgroup_files));
1479}
1480
Balbir Singh67e465a2008-02-07 00:13:54 -08001481static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1482 struct cgroup *cont,
1483 struct cgroup *old_cont,
1484 struct task_struct *p)
1485{
1486 struct mm_struct *mm;
1487 struct mem_cgroup *mem, *old_mem;
1488
1489 mm = get_task_mm(p);
1490 if (mm == NULL)
1491 return;
1492
1493 mem = mem_cgroup_from_cont(cont);
1494 old_mem = mem_cgroup_from_cont(old_cont);
1495
Balbir Singh67e465a2008-02-07 00:13:54 -08001496 /*
1497 * Only thread group leaders are allowed to migrate, the mm_struct is
1498 * in effect owned by the leader
1499 */
Pavel Emelyanov52ea27e2008-03-19 17:00:45 -07001500 if (!thread_group_leader(p))
Balbir Singh67e465a2008-02-07 00:13:54 -08001501 goto out;
1502
Balbir Singh67e465a2008-02-07 00:13:54 -08001503out:
1504 mmput(mm);
Balbir Singh67e465a2008-02-07 00:13:54 -08001505}
1506
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001507struct cgroup_subsys mem_cgroup_subsys = {
1508 .name = "memory",
1509 .subsys_id = mem_cgroup_subsys_id,
1510 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001511 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001512 .destroy = mem_cgroup_destroy,
1513 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08001514 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001515 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001516};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08001517
1518#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1519
1520static int __init disable_swap_account(char *s)
1521{
1522 really_do_swap_account = 0;
1523 return 1;
1524}
1525__setup("noswapaccount", disable_swap_account);
1526#endif