blob: 90ccc1326356a58e8589bed396e058ab5d201390 [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 Hiroyukid52aa412008-02-07 00:14:24 -080024#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080025#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080026#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080027#include <linux/bit_spinlock.h>
28#include <linux/rcupdate.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070029#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080030#include <linux/swap.h>
31#include <linux/spinlock.h>
32#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080033#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070034#include <linux/vmalloc.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080035
Balbir Singh8697d332008-02-07 00:13:59 -080036#include <asm/uaccess.h>
37
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070038struct cgroup_subsys mem_cgroup_subsys __read_mostly;
39static struct kmem_cache *page_cgroup_cache __read_mostly;
40#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh8cdea7c2008-02-07 00:13:50 -080041
42/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080043 * Statistics for memory cgroup.
44 */
45enum mem_cgroup_stat_index {
46 /*
47 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
48 */
49 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
50 MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070051 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
52 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080053
54 MEM_CGROUP_STAT_NSTATS,
55};
56
57struct mem_cgroup_stat_cpu {
58 s64 count[MEM_CGROUP_STAT_NSTATS];
59} ____cacheline_aligned_in_smp;
60
61struct mem_cgroup_stat {
62 struct mem_cgroup_stat_cpu cpustat[NR_CPUS];
63};
64
65/*
66 * For accounting under irq disable, no need for increment preempt count.
67 */
68static void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat *stat,
69 enum mem_cgroup_stat_index idx, int val)
70{
71 int cpu = smp_processor_id();
72 stat->cpustat[cpu].count[idx] += val;
73}
74
75static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
76 enum mem_cgroup_stat_index idx)
77{
78 int cpu;
79 s64 ret = 0;
80 for_each_possible_cpu(cpu)
81 ret += stat->cpustat[cpu].count[idx];
82 return ret;
83}
84
85/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -080086 * per-zone information in memory controller.
87 */
88
89enum mem_cgroup_zstat_index {
90 MEM_CGROUP_ZSTAT_ACTIVE,
91 MEM_CGROUP_ZSTAT_INACTIVE,
92
93 NR_MEM_CGROUP_ZSTAT,
94};
95
96struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -080097 /*
98 * spin_lock to protect the per cgroup LRU
99 */
100 spinlock_t lru_lock;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800101 struct list_head active_list;
102 struct list_head inactive_list;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800103 unsigned long count[NR_MEM_CGROUP_ZSTAT];
104};
105/* Macro for accessing counter */
106#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
107
108struct mem_cgroup_per_node {
109 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
110};
111
112struct mem_cgroup_lru_info {
113 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
114};
115
116/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800117 * The memory controller data structure. The memory controller controls both
118 * page cache and RSS per cgroup. We would eventually like to provide
119 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
120 * to help the administrator determine what knobs to tune.
121 *
122 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800123 * we hit the water mark. May be even add a low water mark, such that
124 * no reclaim occurs from a cgroup at it's low water mark, this is
125 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800126 */
127struct mem_cgroup {
128 struct cgroup_subsys_state css;
129 /*
130 * the counter to account for memory usage
131 */
132 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800133 /*
134 * Per cgroup active and inactive list, similar to the
135 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800136 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800137 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800138
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800139 int prev_priority; /* for recording reclaim priority */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800140 /*
141 * statistics.
142 */
143 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800144};
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800145static struct mem_cgroup init_mem_cgroup;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800146
147/*
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800148 * We use the lower bit of the page->page_cgroup pointer as a bit spin
Hugh Dickins9442ec92008-03-04 14:29:07 -0800149 * lock. We need to ensure that page->page_cgroup is at least two
150 * byte aligned (based on comments from Nick Piggin). But since
151 * bit_spin_lock doesn't actually set that lock bit in a non-debug
152 * uniprocessor kernel, we should avoid setting it here too.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800153 */
154#define PAGE_CGROUP_LOCK_BIT 0x0
Hugh Dickins9442ec92008-03-04 14:29:07 -0800155#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
156#define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT)
157#else
158#define PAGE_CGROUP_LOCK 0x0
159#endif
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800160
161/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800162 * A page_cgroup page is associated with every page descriptor. The
163 * page_cgroup helps us identify information about the cgroup
164 */
165struct page_cgroup {
166 struct list_head lru; /* per cgroup LRU list */
167 struct page *page;
168 struct mem_cgroup *mem_cgroup;
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800169 int ref_cnt; /* cached, mapped, migrating */
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800170 int flags;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800171};
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800172#define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800173#define PAGE_CGROUP_FLAG_ACTIVE (0x2) /* page is active in this cgroup */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800174
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800175static int page_cgroup_nid(struct page_cgroup *pc)
KAMEZAWA Hiroyukic01495302008-02-07 00:14:30 -0800176{
177 return page_to_nid(pc->page);
178}
179
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800180static enum zone_type page_cgroup_zid(struct page_cgroup *pc)
KAMEZAWA Hiroyukic01495302008-02-07 00:14:30 -0800181{
182 return page_zonenum(pc->page);
183}
184
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800185enum charge_type {
186 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
187 MEM_CGROUP_CHARGE_TYPE_MAPPED,
188};
189
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800190/*
191 * Always modified under lru lock. Then, not necessary to preempt_disable()
192 */
193static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, int flags,
194 bool charge)
195{
196 int val = (charge)? 1 : -1;
197 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800198
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800199 VM_BUG_ON(!irqs_disabled());
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800200 if (flags & PAGE_CGROUP_FLAG_CACHE)
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800201 __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800202 else
203 __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700204
205 if (charge)
206 __mem_cgroup_stat_add_safe(stat,
207 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
208 else
209 __mem_cgroup_stat_add_safe(stat,
210 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800211}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800212
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800213static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800214mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
215{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800216 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
217}
218
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800219static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800220page_cgroup_zoneinfo(struct page_cgroup *pc)
221{
222 struct mem_cgroup *mem = pc->mem_cgroup;
223 int nid = page_cgroup_nid(pc);
224 int zid = page_cgroup_zid(pc);
225
226 return mem_cgroup_zoneinfo(mem, nid, zid);
227}
228
229static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
230 enum mem_cgroup_zstat_index idx)
231{
232 int nid, zid;
233 struct mem_cgroup_per_zone *mz;
234 u64 total = 0;
235
236 for_each_online_node(nid)
237 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
238 mz = mem_cgroup_zoneinfo(mem, nid, zid);
239 total += MEM_CGROUP_ZSTAT(mz, idx);
240 }
241 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800242}
243
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800244static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800245{
246 return container_of(cgroup_subsys_state(cont,
247 mem_cgroup_subsys_id), struct mem_cgroup,
248 css);
249}
250
Balbir Singhcf475ad2008-04-29 01:00:16 -0700251struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800252{
253 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
254 struct mem_cgroup, css);
255}
256
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800257static inline int page_cgroup_locked(struct page *page)
258{
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800259 return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800260}
261
Hugh Dickins9442ec92008-03-04 14:29:07 -0800262static void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800263{
Hugh Dickins9442ec92008-03-04 14:29:07 -0800264 VM_BUG_ON(!page_cgroup_locked(page));
265 page->page_cgroup = ((unsigned long)pc | PAGE_CGROUP_LOCK);
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800266}
267
268struct page_cgroup *page_get_page_cgroup(struct page *page)
269{
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800270 return (struct page_cgroup *) (page->page_cgroup & ~PAGE_CGROUP_LOCK);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800271}
272
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800273static void lock_page_cgroup(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800274{
275 bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800276}
277
Hugh Dickins2680eed2008-03-04 14:29:13 -0800278static int try_lock_page_cgroup(struct page *page)
279{
280 return bit_spin_trylock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
281}
282
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800283static void unlock_page_cgroup(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800284{
285 bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
286}
287
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700288static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
289 struct page_cgroup *pc)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800290{
291 int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800292
293 if (from)
294 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1;
295 else
296 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1;
297
298 mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, false);
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700299 list_del(&pc->lru);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800300}
301
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700302static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
303 struct page_cgroup *pc)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800304{
305 int to = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800306
307 if (!to) {
308 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800309 list_add(&pc->lru, &mz->inactive_list);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800310 } else {
311 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800312 list_add(&pc->lru, &mz->active_list);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800313 }
314 mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, true);
315}
316
Balbir Singh8697d332008-02-07 00:13:59 -0800317static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
Balbir Singh66e17072008-02-07 00:13:56 -0800318{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800319 int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
320 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
321
322 if (from)
323 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1;
324 else
325 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1;
326
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800327 if (active) {
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800328 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1;
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800329 pc->flags |= PAGE_CGROUP_FLAG_ACTIVE;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800330 list_move(&pc->lru, &mz->active_list);
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800331 } else {
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800332 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1;
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800333 pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800334 list_move(&pc->lru, &mz->inactive_list);
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800335 }
Balbir Singh66e17072008-02-07 00:13:56 -0800336}
337
David Rientjes4c4a2212008-02-07 00:14:06 -0800338int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
339{
340 int ret;
341
342 task_lock(task);
Hugh Dickinsbd845e32008-03-04 14:29:01 -0800343 ret = task->mm && mm_match_cgroup(task->mm, mem);
David Rientjes4c4a2212008-02-07 00:14:06 -0800344 task_unlock(task);
345 return ret;
346}
347
Balbir Singh66e17072008-02-07 00:13:56 -0800348/*
349 * This routine assumes that the appropriate zone's lru lock is already held
350 */
Hugh Dickins427d5412008-03-04 14:29:03 -0800351void mem_cgroup_move_lists(struct page *page, bool active)
Balbir Singh66e17072008-02-07 00:13:56 -0800352{
Hugh Dickins427d5412008-03-04 14:29:03 -0800353 struct page_cgroup *pc;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800354 struct mem_cgroup_per_zone *mz;
355 unsigned long flags;
356
Hugh Dickins2680eed2008-03-04 14:29:13 -0800357 /*
358 * We cannot lock_page_cgroup while holding zone's lru_lock,
359 * because other holders of lock_page_cgroup can be interrupted
360 * with an attempt to rotate_reclaimable_page. But we cannot
361 * safely get to page_cgroup without it, so just try_lock it:
362 * mem_cgroup_isolate_pages allows for page left on wrong list.
363 */
364 if (!try_lock_page_cgroup(page))
Balbir Singh66e17072008-02-07 00:13:56 -0800365 return;
366
Hugh Dickins2680eed2008-03-04 14:29:13 -0800367 pc = page_get_page_cgroup(page);
368 if (pc) {
Hugh Dickins2680eed2008-03-04 14:29:13 -0800369 mz = page_cgroup_zoneinfo(pc);
Hugh Dickins2680eed2008-03-04 14:29:13 -0800370 spin_lock_irqsave(&mz->lru_lock, flags);
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800371 __mem_cgroup_move_lists(pc, active);
Hugh Dickins2680eed2008-03-04 14:29:13 -0800372 spin_unlock_irqrestore(&mz->lru_lock, flags);
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800373 }
374 unlock_page_cgroup(page);
Balbir Singh66e17072008-02-07 00:13:56 -0800375}
376
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800377/*
378 * Calculate mapped_ratio under memory controller. This will be used in
379 * vmscan.c for deteremining we have to reclaim mapped pages.
380 */
381int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
382{
383 long total, rss;
384
385 /*
386 * usage is recorded in bytes. But, here, we assume the number of
387 * physical pages can be represented by "long" on any arch.
388 */
389 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
390 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
391 return (int)((rss * 100L) / total);
392}
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800393
KAMEZAWA Hiroyuki5932f362008-02-07 00:14:33 -0800394/*
395 * This function is called from vmscan.c. In page reclaiming loop. balance
396 * between active and inactive list is calculated. For memory controller
397 * page reclaiming, we should use using mem_cgroup's imbalance rather than
398 * zone's global lru imbalance.
399 */
400long mem_cgroup_reclaim_imbalance(struct mem_cgroup *mem)
401{
402 unsigned long active, inactive;
403 /* active and inactive are the number of pages. 'long' is ok.*/
404 active = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_ACTIVE);
405 inactive = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_INACTIVE);
406 return (long) (active / (inactive + 1));
407}
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800408
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800409/*
410 * prev_priority control...this will be used in memory reclaim path.
411 */
412int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
413{
414 return mem->prev_priority;
415}
416
417void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
418{
419 if (priority < mem->prev_priority)
420 mem->prev_priority = priority;
421}
422
423void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
424{
425 mem->prev_priority = priority;
426}
427
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800428/*
429 * Calculate # of pages to be scanned in this priority/zone.
430 * See also vmscan.c
431 *
432 * priority starts from "DEF_PRIORITY" and decremented in each loop.
433 * (see include/linux/mmzone.h)
434 */
435
436long mem_cgroup_calc_reclaim_active(struct mem_cgroup *mem,
437 struct zone *zone, int priority)
438{
439 long nr_active;
440 int nid = zone->zone_pgdat->node_id;
441 int zid = zone_idx(zone);
442 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
443
444 nr_active = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE);
445 return (nr_active >> priority);
446}
447
448long mem_cgroup_calc_reclaim_inactive(struct mem_cgroup *mem,
449 struct zone *zone, int priority)
450{
451 long nr_inactive;
452 int nid = zone->zone_pgdat->node_id;
453 int zid = zone_idx(zone);
454 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
455
456 nr_inactive = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800457 return (nr_inactive >> priority);
458}
459
Balbir Singh66e17072008-02-07 00:13:56 -0800460unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
461 struct list_head *dst,
462 unsigned long *scanned, int order,
463 int mode, struct zone *z,
464 struct mem_cgroup *mem_cont,
465 int active)
466{
467 unsigned long nr_taken = 0;
468 struct page *page;
469 unsigned long scan;
470 LIST_HEAD(pc_list);
471 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800472 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800473 int nid = z->zone_pgdat->node_id;
474 int zid = zone_idx(z);
475 struct mem_cgroup_per_zone *mz;
Balbir Singh66e17072008-02-07 00:13:56 -0800476
Balbir Singhcf475ad2008-04-29 01:00:16 -0700477 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800478 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Balbir Singh66e17072008-02-07 00:13:56 -0800479 if (active)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800480 src = &mz->active_list;
Balbir Singh66e17072008-02-07 00:13:56 -0800481 else
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800482 src = &mz->inactive_list;
483
Balbir Singh66e17072008-02-07 00:13:56 -0800484
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800485 spin_lock(&mz->lru_lock);
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800486 scan = 0;
487 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800488 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800489 break;
Balbir Singh66e17072008-02-07 00:13:56 -0800490 page = pc->page;
Balbir Singh66e17072008-02-07 00:13:56 -0800491
Hugh Dickins436c65412008-02-07 00:14:12 -0800492 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800493 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800494
Balbir Singh66e17072008-02-07 00:13:56 -0800495 if (PageActive(page) && !active) {
496 __mem_cgroup_move_lists(pc, true);
Balbir Singh66e17072008-02-07 00:13:56 -0800497 continue;
498 }
499 if (!PageActive(page) && active) {
500 __mem_cgroup_move_lists(pc, false);
Balbir Singh66e17072008-02-07 00:13:56 -0800501 continue;
502 }
503
Hugh Dickins436c65412008-02-07 00:14:12 -0800504 scan++;
505 list_move(&pc->lru, &pc_list);
Balbir Singh66e17072008-02-07 00:13:56 -0800506
507 if (__isolate_lru_page(page, mode) == 0) {
508 list_move(&page->lru, dst);
509 nr_taken++;
510 }
511 }
512
513 list_splice(&pc_list, src);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800514 spin_unlock(&mz->lru_lock);
Balbir Singh66e17072008-02-07 00:13:56 -0800515
516 *scanned = scan;
517 return nr_taken;
518}
519
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800520/*
521 * Charge the memory controller for page usage.
522 * Return
523 * 0 if the charge was successful
524 * < 0 if the cgroup is over its limit
525 */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800526static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
527 gfp_t gfp_mask, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800528{
529 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800530 struct page_cgroup *pc;
Balbir Singh66e17072008-02-07 00:13:56 -0800531 unsigned long flags;
532 unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800533 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800534
Balbir Singh40779602008-04-04 14:29:59 -0700535 if (mem_cgroup_subsys.disabled)
536 return 0;
537
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800538 /*
539 * Should page_cgroup's go to their own slab?
540 * One could optimize the performance of the charging routine
541 * by saving a bit in the page_flags and using it as a lock
542 * to see if the cgroup page already has a page_cgroup associated
543 * with it
544 */
Balbir Singh66e17072008-02-07 00:13:56 -0800545retry:
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800546 lock_page_cgroup(page);
547 pc = page_get_page_cgroup(page);
548 /*
549 * The page_cgroup exists and
550 * the page has already been accounted.
551 */
552 if (pc) {
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800553 VM_BUG_ON(pc->page != page);
554 VM_BUG_ON(pc->ref_cnt <= 0);
555
556 pc->ref_cnt++;
557 unlock_page_cgroup(page);
558 goto done;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800559 }
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800560 unlock_page_cgroup(page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800561
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700562 pc = kmem_cache_alloc(page_cgroup_cache, gfp_mask);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800563 if (pc == NULL)
564 goto err;
565
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800566 /*
Hugh Dickins3be91272008-02-07 00:14:19 -0800567 * We always charge the cgroup the mm_struct belongs to.
568 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800569 * thread group leader migrates. It's possible that mm is not
570 * set, if so charge the init_mm (happens for pagecache usage).
571 */
572 if (!mm)
573 mm = &init_mm;
574
Hugh Dickins3be91272008-02-07 00:14:19 -0800575 rcu_read_lock();
Balbir Singhcf475ad2008-04-29 01:00:16 -0700576 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800577 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800578 * For every charge from the cgroup, increment reference count
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800579 */
580 css_get(&mem->css);
581 rcu_read_unlock();
582
Balbir Singh0eea1032008-02-07 00:13:57 -0800583 while (res_counter_charge(&mem->res, PAGE_SIZE)) {
Hugh Dickins3be91272008-02-07 00:14:19 -0800584 if (!(gfp_mask & __GFP_WAIT))
585 goto out;
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800586
587 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
Balbir Singh66e17072008-02-07 00:13:56 -0800588 continue;
589
590 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800591 * try_to_free_mem_cgroup_pages() might not give us a full
592 * picture of reclaim. Some pages are reclaimed and might be
593 * moved to swap cache or just unmapped from the cgroup.
594 * Check the limit again to see if the reclaim reduced the
595 * current usage of the cgroup before giving up
596 */
Balbir Singh66e17072008-02-07 00:13:56 -0800597 if (res_counter_check_under_limit(&mem->res))
598 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -0800599
600 if (!nr_retries--) {
601 mem_cgroup_out_of_memory(mem, gfp_mask);
602 goto out;
Balbir Singh66e17072008-02-07 00:13:56 -0800603 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800604 }
605
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800606 pc->ref_cnt = 1;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800607 pc->mem_cgroup = mem;
608 pc->page = page;
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700609 /*
610 * If a page is accounted as a page cache, insert to inactive list.
611 * If anon, insert to active list.
612 */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800613 if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE)
Balbir Singh4a56d022008-04-29 01:00:23 -0700614 pc->flags = PAGE_CGROUP_FLAG_CACHE;
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700615 else
616 pc->flags = PAGE_CGROUP_FLAG_ACTIVE;
Hugh Dickins3be91272008-02-07 00:14:19 -0800617
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800618 lock_page_cgroup(page);
619 if (page_get_page_cgroup(page)) {
620 unlock_page_cgroup(page);
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800621 /*
Hugh Dickins3be91272008-02-07 00:14:19 -0800622 * Another charge has been added to this page already.
623 * We take lock_page_cgroup(page) again and read
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800624 * page->cgroup, increment refcnt.... just retry is OK.
625 */
626 res_counter_uncharge(&mem->res, PAGE_SIZE);
627 css_put(&mem->css);
Balbir Singhb6ac57d2008-04-29 01:00:19 -0700628 kmem_cache_free(page_cgroup_cache, pc);
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800629 goto retry;
630 }
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800631 page_assign_page_cgroup(page, pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800632
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800633 mz = page_cgroup_zoneinfo(pc);
634 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700635 __mem_cgroup_add_list(mz, pc);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800636 spin_unlock_irqrestore(&mz->lru_lock, flags);
Balbir Singh66e17072008-02-07 00:13:56 -0800637
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800638 unlock_page_cgroup(page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800639done:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800640 return 0;
Hugh Dickins3be91272008-02-07 00:14:19 -0800641out:
642 css_put(&mem->css);
Balbir Singhb6ac57d2008-04-29 01:00:19 -0700643 kmem_cache_free(page_cgroup_cache, pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800644err:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800645 return -ENOMEM;
646}
647
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800648int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800649{
650 return mem_cgroup_charge_common(page, mm, gfp_mask,
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800651 MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800652}
653
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800654int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
655 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -0800656{
Balbir Singh8697d332008-02-07 00:13:59 -0800657 if (!mm)
658 mm = &init_mm;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800659 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800660 MEM_CGROUP_CHARGE_TYPE_CACHE);
Balbir Singh8697d332008-02-07 00:13:59 -0800661}
662
663/*
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800664 * Uncharging is always a welcome operation, we never complain, simply
Hugh Dickins82895462008-03-04 14:29:08 -0800665 * uncharge.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800666 */
Hugh Dickins82895462008-03-04 14:29:08 -0800667void mem_cgroup_uncharge_page(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800668{
Hugh Dickins82895462008-03-04 14:29:08 -0800669 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800670 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800671 struct mem_cgroup_per_zone *mz;
Balbir Singh66e17072008-02-07 00:13:56 -0800672 unsigned long flags;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800673
Balbir Singh40779602008-04-04 14:29:59 -0700674 if (mem_cgroup_subsys.disabled)
675 return;
676
Balbir Singh8697d332008-02-07 00:13:59 -0800677 /*
Balbir Singh3c541e12008-02-07 00:14:41 -0800678 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -0800679 */
Hugh Dickins82895462008-03-04 14:29:08 -0800680 lock_page_cgroup(page);
681 pc = page_get_page_cgroup(page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800682 if (!pc)
Hugh Dickins82895462008-03-04 14:29:08 -0800683 goto unlock;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800684
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800685 VM_BUG_ON(pc->page != page);
686 VM_BUG_ON(pc->ref_cnt <= 0);
687
688 if (--(pc->ref_cnt) == 0) {
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800689 mz = page_cgroup_zoneinfo(pc);
690 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700691 __mem_cgroup_remove_list(mz, pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800692 spin_unlock_irqrestore(&mz->lru_lock, flags);
693
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800694 page_assign_page_cgroup(page, NULL);
695 unlock_page_cgroup(page);
696
Hugh Dickins6d48ff82008-03-04 14:29:12 -0800697 mem = pc->mem_cgroup;
698 res_counter_uncharge(&mem->res, PAGE_SIZE);
699 css_put(&mem->css);
700
Balbir Singhb6ac57d2008-04-29 01:00:19 -0700701 kmem_cache_free(page_cgroup_cache, pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800702 return;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800703 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800704
Hugh Dickins82895462008-03-04 14:29:08 -0800705unlock:
Balbir Singh3c541e12008-02-07 00:14:41 -0800706 unlock_page_cgroup(page);
707}
708
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800709/*
710 * Returns non-zero if a page (under migration) has valid page_cgroup member.
711 * Refcnt of page_cgroup is incremented.
712 */
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800713int mem_cgroup_prepare_migration(struct page *page)
714{
715 struct page_cgroup *pc;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800716
Balbir Singh40779602008-04-04 14:29:59 -0700717 if (mem_cgroup_subsys.disabled)
718 return 0;
719
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800720 lock_page_cgroup(page);
721 pc = page_get_page_cgroup(page);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800722 if (pc)
723 pc->ref_cnt++;
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800724 unlock_page_cgroup(page);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800725 return pc != NULL;
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800726}
727
728void mem_cgroup_end_migration(struct page *page)
729{
Hugh Dickins82895462008-03-04 14:29:08 -0800730 mem_cgroup_uncharge_page(page);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800731}
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800732
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800733/*
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800734 * We know both *page* and *newpage* are now not-on-LRU and PG_locked.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800735 * And no race with uncharge() routines because page_cgroup for *page*
736 * has extra one reference by mem_cgroup_prepare_migration.
737 */
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800738void mem_cgroup_page_migration(struct page *page, struct page *newpage)
739{
740 struct page_cgroup *pc;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800741 struct mem_cgroup_per_zone *mz;
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800742 unsigned long flags;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800743
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800744 lock_page_cgroup(page);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800745 pc = page_get_page_cgroup(page);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800746 if (!pc) {
747 unlock_page_cgroup(page);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800748 return;
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800749 }
750
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800751 mz = page_cgroup_zoneinfo(pc);
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800752 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700753 __mem_cgroup_remove_list(mz, pc);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800754 spin_unlock_irqrestore(&mz->lru_lock, flags);
755
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800756 page_assign_page_cgroup(page, NULL);
757 unlock_page_cgroup(page);
758
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800759 pc->page = newpage;
760 lock_page_cgroup(newpage);
761 page_assign_page_cgroup(newpage, pc);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800762
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800763 mz = page_cgroup_zoneinfo(pc);
764 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700765 __mem_cgroup_add_list(mz, pc);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800766 spin_unlock_irqrestore(&mz->lru_lock, flags);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800767
768 unlock_page_cgroup(newpage);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800769}
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800770
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800771/*
772 * This routine traverse page_cgroup in given list and drop them all.
773 * This routine ignores page_cgroup->ref_cnt.
774 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
775 */
776#define FORCE_UNCHARGE_BATCH (128)
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800777static void mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800778 struct mem_cgroup_per_zone *mz,
779 int active)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800780{
781 struct page_cgroup *pc;
782 struct page *page;
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800783 int count = FORCE_UNCHARGE_BATCH;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800784 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800785 struct list_head *list;
786
787 if (active)
788 list = &mz->active_list;
789 else
790 list = &mz->inactive_list;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800791
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800792 spin_lock_irqsave(&mz->lru_lock, flags);
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800793 while (!list_empty(list)) {
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800794 pc = list_entry(list->prev, struct page_cgroup, lru);
795 page = pc->page;
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800796 get_page(page);
797 spin_unlock_irqrestore(&mz->lru_lock, flags);
798 mem_cgroup_uncharge_page(page);
799 put_page(page);
800 if (--count <= 0) {
801 count = FORCE_UNCHARGE_BATCH;
802 cond_resched();
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800803 }
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800804 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800805 }
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800806 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800807}
808
809/*
810 * make mem_cgroup's charge to be 0 if there is no task.
811 * This enables deleting this mem_cgroup.
812 */
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800813static int mem_cgroup_force_empty(struct mem_cgroup *mem)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800814{
815 int ret = -EBUSY;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800816 int node, zid;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800817
Balbir Singh40779602008-04-04 14:29:59 -0700818 if (mem_cgroup_subsys.disabled)
819 return 0;
820
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800821 css_get(&mem->css);
822 /*
823 * page reclaim code (kswapd etc..) will move pages between
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800824 * active_list <-> inactive_list while we don't take a lock.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800825 * So, we have to do loop here until all lists are empty.
826 */
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800827 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800828 if (atomic_read(&mem->css.cgroup->count) > 0)
829 goto out;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800830 for_each_node_state(node, N_POSSIBLE)
831 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
832 struct mem_cgroup_per_zone *mz;
833 mz = mem_cgroup_zoneinfo(mem, node, zid);
834 /* drop all page_cgroup in active_list */
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800835 mem_cgroup_force_empty_list(mem, mz, 1);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800836 /* drop all page_cgroup in inactive_list */
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800837 mem_cgroup_force_empty_list(mem, mz, 0);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800838 }
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800839 }
840 ret = 0;
841out:
842 css_put(&mem->css);
843 return ret;
844}
845
Paul Menage2c3daa72008-04-29 00:59:58 -0700846static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800847{
Paul Menage2c3daa72008-04-29 00:59:58 -0700848 return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
849 cft->private);
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800850}
851
Paul Menage856c13a2008-07-25 01:47:04 -0700852static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
853 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800854{
855 return res_counter_write(&mem_cgroup_from_cont(cont)->res,
Paul Menage856c13a2008-07-25 01:47:04 -0700856 cft->private, buffer,
857 res_counter_memparse_write_strategy);
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800858}
859
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -0700860static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -0700861{
862 struct mem_cgroup *mem;
863
864 mem = mem_cgroup_from_cont(cont);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -0700865 switch (event) {
866 case RES_MAX_USAGE:
867 res_counter_reset_max(&mem->res);
868 break;
869 case RES_FAILCNT:
870 res_counter_reset_failcnt(&mem->res);
871 break;
872 }
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -0700873 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -0700874}
875
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -0700876static int mem_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800877{
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -0700878 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800879}
880
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800881static const struct mem_cgroup_stat_desc {
882 const char *msg;
883 u64 unit;
884} mem_cgroup_stat_desc[] = {
885 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
886 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
Balaji Rao55e462b2008-05-01 04:35:12 -0700887 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
888 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800889};
890
Paul Menagec64745c2008-04-29 01:00:02 -0700891static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
892 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800893{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800894 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
895 struct mem_cgroup_stat *stat = &mem_cont->stat;
896 int i;
897
898 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
899 s64 val;
900
901 val = mem_cgroup_read_stat(stat, i);
902 val *= mem_cgroup_stat_desc[i].unit;
Paul Menagec64745c2008-04-29 01:00:02 -0700903 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800904 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800905 /* showing # of active pages */
906 {
907 unsigned long active, inactive;
908
909 inactive = mem_cgroup_get_all_zonestat(mem_cont,
910 MEM_CGROUP_ZSTAT_INACTIVE);
911 active = mem_cgroup_get_all_zonestat(mem_cont,
912 MEM_CGROUP_ZSTAT_ACTIVE);
Paul Menagec64745c2008-04-29 01:00:02 -0700913 cb->fill(cb, "active", (active) * PAGE_SIZE);
914 cb->fill(cb, "inactive", (inactive) * PAGE_SIZE);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800915 }
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800916 return 0;
917}
918
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800919static struct cftype mem_cgroup_files[] = {
920 {
Balbir Singh0eea1032008-02-07 00:13:57 -0800921 .name = "usage_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800922 .private = RES_USAGE,
Paul Menage2c3daa72008-04-29 00:59:58 -0700923 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800924 },
925 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -0700926 .name = "max_usage_in_bytes",
927 .private = RES_MAX_USAGE,
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -0700928 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -0700929 .read_u64 = mem_cgroup_read,
930 },
931 {
Balbir Singh0eea1032008-02-07 00:13:57 -0800932 .name = "limit_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800933 .private = RES_LIMIT,
Paul Menage856c13a2008-07-25 01:47:04 -0700934 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -0700935 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800936 },
937 {
938 .name = "failcnt",
939 .private = RES_FAILCNT,
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -0700940 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -0700941 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800942 },
Balbir Singh8697d332008-02-07 00:13:59 -0800943 {
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800944 .name = "force_empty",
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -0700945 .trigger = mem_force_empty_write,
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800946 },
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800947 {
948 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -0700949 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800950 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800951};
952
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800953static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
954{
955 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800956 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -0700957 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800958 /*
959 * This routine is called against possible nodes.
960 * But it's BUG to call kmalloc() against offline node.
961 *
962 * TODO: this routine can waste much memory for nodes which will
963 * never be onlined. It's better to use memory hotplug callback
964 * function.
965 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -0700966 if (!node_state(node, N_NORMAL_MEMORY))
967 tmp = -1;
968 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800969 if (!pn)
970 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800971
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800972 mem->info.nodeinfo[node] = pn;
973 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800974
975 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
976 mz = &pn->zoneinfo[zone];
977 INIT_LIST_HEAD(&mz->active_list);
978 INIT_LIST_HEAD(&mz->inactive_list);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800979 spin_lock_init(&mz->lru_lock);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800980 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800981 return 0;
982}
983
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800984static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
985{
986 kfree(mem->info.nodeinfo[node]);
987}
988
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -0700989static struct mem_cgroup *mem_cgroup_alloc(void)
990{
991 struct mem_cgroup *mem;
992
993 if (sizeof(*mem) < PAGE_SIZE)
994 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
995 else
996 mem = vmalloc(sizeof(*mem));
997
998 if (mem)
999 memset(mem, 0, sizeof(*mem));
1000 return mem;
1001}
1002
1003static void mem_cgroup_free(struct mem_cgroup *mem)
1004{
1005 if (sizeof(*mem) < PAGE_SIZE)
1006 kfree(mem);
1007 else
1008 vfree(mem);
1009}
1010
1011
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001012static struct cgroup_subsys_state *
1013mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1014{
1015 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001016 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001017
Balbir Singhb6ac57d2008-04-29 01:00:19 -07001018 if (unlikely((cont->parent) == NULL)) {
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001019 mem = &init_mem_cgroup;
Balbir Singhb6ac57d2008-04-29 01:00:19 -07001020 page_cgroup_cache = KMEM_CACHE(page_cgroup, SLAB_PANIC);
1021 } else {
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001022 mem = mem_cgroup_alloc();
1023 if (!mem)
1024 return ERR_PTR(-ENOMEM);
Balbir Singhb6ac57d2008-04-29 01:00:19 -07001025 }
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001026
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001027 res_counter_init(&mem->res);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001028
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001029 for_each_node_state(node, N_POSSIBLE)
1030 if (alloc_mem_cgroup_per_zone_info(mem, node))
1031 goto free_out;
1032
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001033 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001034free_out:
1035 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001036 free_mem_cgroup_per_zone_info(mem, node);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001037 if (cont->parent != NULL)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001038 mem_cgroup_free(mem);
Li Zefan2dda81c2008-02-23 15:24:14 -08001039 return ERR_PTR(-ENOMEM);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001040}
1041
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001042static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1043 struct cgroup *cont)
1044{
1045 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1046 mem_cgroup_force_empty(mem);
1047}
1048
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001049static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1050 struct cgroup *cont)
1051{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001052 int node;
1053 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1054
1055 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001056 free_mem_cgroup_per_zone_info(mem, node);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001057
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001058 mem_cgroup_free(mem_cgroup_from_cont(cont));
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001059}
1060
1061static int mem_cgroup_populate(struct cgroup_subsys *ss,
1062 struct cgroup *cont)
1063{
Balbir Singh40779602008-04-04 14:29:59 -07001064 if (mem_cgroup_subsys.disabled)
1065 return 0;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001066 return cgroup_add_files(cont, ss, mem_cgroup_files,
1067 ARRAY_SIZE(mem_cgroup_files));
1068}
1069
Balbir Singh67e465a2008-02-07 00:13:54 -08001070static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1071 struct cgroup *cont,
1072 struct cgroup *old_cont,
1073 struct task_struct *p)
1074{
1075 struct mm_struct *mm;
1076 struct mem_cgroup *mem, *old_mem;
1077
Balbir Singh40779602008-04-04 14:29:59 -07001078 if (mem_cgroup_subsys.disabled)
1079 return;
1080
Balbir Singh67e465a2008-02-07 00:13:54 -08001081 mm = get_task_mm(p);
1082 if (mm == NULL)
1083 return;
1084
1085 mem = mem_cgroup_from_cont(cont);
1086 old_mem = mem_cgroup_from_cont(old_cont);
1087
1088 if (mem == old_mem)
1089 goto out;
1090
1091 /*
1092 * Only thread group leaders are allowed to migrate, the mm_struct is
1093 * in effect owned by the leader
1094 */
Pavel Emelyanov52ea27e2008-03-19 17:00:45 -07001095 if (!thread_group_leader(p))
Balbir Singh67e465a2008-02-07 00:13:54 -08001096 goto out;
1097
Balbir Singh67e465a2008-02-07 00:13:54 -08001098out:
1099 mmput(mm);
Balbir Singh67e465a2008-02-07 00:13:54 -08001100}
1101
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001102struct cgroup_subsys mem_cgroup_subsys = {
1103 .name = "memory",
1104 .subsys_id = mem_cgroup_subsys_id,
1105 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001106 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001107 .destroy = mem_cgroup_destroy,
1108 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08001109 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001110 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001111};