blob: dbf571547c0333f30f6cfdba07bced94f3bf5405 [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>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080024#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080025#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080026#include <linux/bit_spinlock.h>
27#include <linux/rcupdate.h>
Balbir Singh66e17072008-02-07 00:13:56 -080028#include <linux/swap.h>
29#include <linux/spinlock.h>
30#include <linux/fs.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080031
Balbir Singh8697d332008-02-07 00:13:59 -080032#include <asm/uaccess.h>
33
Balbir Singh8cdea7c2008-02-07 00:13:50 -080034struct cgroup_subsys mem_cgroup_subsys;
Balbir Singh66e17072008-02-07 00:13:56 -080035static const int MEM_CGROUP_RECLAIM_RETRIES = 5;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080036
37/*
38 * The memory controller data structure. The memory controller controls both
39 * page cache and RSS per cgroup. We would eventually like to provide
40 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
41 * to help the administrator determine what knobs to tune.
42 *
43 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080044 * we hit the water mark. May be even add a low water mark, such that
45 * no reclaim occurs from a cgroup at it's low water mark, this is
46 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -080047 */
48struct mem_cgroup {
49 struct cgroup_subsys_state css;
50 /*
51 * the counter to account for memory usage
52 */
53 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -080054 /*
55 * Per cgroup active and inactive list, similar to the
56 * per zone LRU lists.
57 * TODO: Consider making these lists per zone
58 */
59 struct list_head active_list;
60 struct list_head inactive_list;
Balbir Singh66e17072008-02-07 00:13:56 -080061 /*
62 * spin_lock to protect the per cgroup LRU
63 */
64 spinlock_t lru_lock;
Balbir Singh8697d332008-02-07 00:13:59 -080065 unsigned long control_type; /* control RSS or RSS+Pagecache */
Balbir Singh8cdea7c2008-02-07 00:13:50 -080066};
67
68/*
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080069 * We use the lower bit of the page->page_cgroup pointer as a bit spin
70 * lock. We need to ensure that page->page_cgroup is atleast two
71 * byte aligned (based on comments from Nick Piggin)
72 */
73#define PAGE_CGROUP_LOCK_BIT 0x0
74#define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT)
75
76/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -080077 * A page_cgroup page is associated with every page descriptor. The
78 * page_cgroup helps us identify information about the cgroup
79 */
80struct page_cgroup {
81 struct list_head lru; /* per cgroup LRU list */
82 struct page *page;
83 struct mem_cgroup *mem_cgroup;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080084 atomic_t ref_cnt; /* Helpful when pages move b/w */
85 /* mapped and cached states */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -080086 int flags;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080087};
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -080088#define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */
Balbir Singh8cdea7c2008-02-07 00:13:50 -080089
Balbir Singh8697d332008-02-07 00:13:59 -080090enum {
91 MEM_CGROUP_TYPE_UNSPEC = 0,
92 MEM_CGROUP_TYPE_MAPPED,
93 MEM_CGROUP_TYPE_CACHED,
94 MEM_CGROUP_TYPE_ALL,
95 MEM_CGROUP_TYPE_MAX,
96};
97
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -080098enum charge_type {
99 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
100 MEM_CGROUP_CHARGE_TYPE_MAPPED,
101};
102
Balbir Singh8697d332008-02-07 00:13:59 -0800103static struct mem_cgroup init_mem_cgroup;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800104
105static inline
106struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
107{
108 return container_of(cgroup_subsys_state(cont,
109 mem_cgroup_subsys_id), struct mem_cgroup,
110 css);
111}
112
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800113static inline
114struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
115{
116 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
117 struct mem_cgroup, css);
118}
119
120void mm_init_cgroup(struct mm_struct *mm, struct task_struct *p)
121{
122 struct mem_cgroup *mem;
123
124 mem = mem_cgroup_from_task(p);
125 css_get(&mem->css);
126 mm->mem_cgroup = mem;
127}
128
129void mm_free_cgroup(struct mm_struct *mm)
130{
131 css_put(&mm->mem_cgroup->css);
132}
133
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800134static inline int page_cgroup_locked(struct page *page)
135{
136 return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT,
137 &page->page_cgroup);
138}
139
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800140void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
141{
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800142 int locked;
143
144 /*
145 * While resetting the page_cgroup we might not hold the
146 * page_cgroup lock. free_hot_cold_page() is an example
147 * of such a scenario
148 */
149 if (pc)
150 VM_BUG_ON(!page_cgroup_locked(page));
151 locked = (page->page_cgroup & PAGE_CGROUP_LOCK);
152 page->page_cgroup = ((unsigned long)pc | locked);
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800153}
154
155struct page_cgroup *page_get_page_cgroup(struct page *page)
156{
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800157 return (struct page_cgroup *)
158 (page->page_cgroup & ~PAGE_CGROUP_LOCK);
159}
160
Balbir Singh8697d332008-02-07 00:13:59 -0800161static void __always_inline lock_page_cgroup(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800162{
163 bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
164 VM_BUG_ON(!page_cgroup_locked(page));
165}
166
Balbir Singh8697d332008-02-07 00:13:59 -0800167static void __always_inline unlock_page_cgroup(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800168{
169 bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
170}
171
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800172/*
173 * Tie new page_cgroup to struct page under lock_page_cgroup()
174 * This can fail if the page has been tied to a page_cgroup.
175 * If success, returns 0.
176 */
177static inline int
178page_cgroup_assign_new_page_cgroup(struct page *page, struct page_cgroup *pc)
179{
180 int ret = 0;
181
182 lock_page_cgroup(page);
183 if (!page_get_page_cgroup(page))
184 page_assign_page_cgroup(page, pc);
185 else /* A page is tied to other pc. */
186 ret = 1;
187 unlock_page_cgroup(page);
188 return ret;
189}
190
191/*
192 * Clear page->page_cgroup member under lock_page_cgroup().
193 * If given "pc" value is different from one page->page_cgroup,
194 * page->cgroup is not cleared.
195 * Returns a value of page->page_cgroup at lock taken.
196 * A can can detect failure of clearing by following
197 * clear_page_cgroup(page, pc) == pc
198 */
199
200static inline struct page_cgroup *
201clear_page_cgroup(struct page *page, struct page_cgroup *pc)
202{
203 struct page_cgroup *ret;
204 /* lock and clear */
205 lock_page_cgroup(page);
206 ret = page_get_page_cgroup(page);
207 if (likely(ret == pc))
208 page_assign_page_cgroup(page, NULL);
209 unlock_page_cgroup(page);
210 return ret;
211}
212
213
Balbir Singh8697d332008-02-07 00:13:59 -0800214static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
Balbir Singh66e17072008-02-07 00:13:56 -0800215{
216 if (active)
217 list_move(&pc->lru, &pc->mem_cgroup->active_list);
218 else
219 list_move(&pc->lru, &pc->mem_cgroup->inactive_list);
220}
221
David Rientjes4c4a2212008-02-07 00:14:06 -0800222int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
223{
224 int ret;
225
226 task_lock(task);
227 ret = task->mm && mm_cgroup(task->mm) == mem;
228 task_unlock(task);
229 return ret;
230}
231
Balbir Singh66e17072008-02-07 00:13:56 -0800232/*
233 * This routine assumes that the appropriate zone's lru lock is already held
234 */
235void mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
236{
237 struct mem_cgroup *mem;
238 if (!pc)
239 return;
240
241 mem = pc->mem_cgroup;
242
243 spin_lock(&mem->lru_lock);
244 __mem_cgroup_move_lists(pc, active);
245 spin_unlock(&mem->lru_lock);
246}
247
248unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
249 struct list_head *dst,
250 unsigned long *scanned, int order,
251 int mode, struct zone *z,
252 struct mem_cgroup *mem_cont,
253 int active)
254{
255 unsigned long nr_taken = 0;
256 struct page *page;
257 unsigned long scan;
258 LIST_HEAD(pc_list);
259 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800260 struct page_cgroup *pc, *tmp;
Balbir Singh66e17072008-02-07 00:13:56 -0800261
262 if (active)
263 src = &mem_cont->active_list;
264 else
265 src = &mem_cont->inactive_list;
266
267 spin_lock(&mem_cont->lru_lock);
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800268 scan = 0;
269 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800270 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800271 break;
Balbir Singh66e17072008-02-07 00:13:56 -0800272 page = pc->page;
273 VM_BUG_ON(!pc);
274
Hugh Dickins436c65412008-02-07 00:14:12 -0800275 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800276 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800277
Balbir Singh66e17072008-02-07 00:13:56 -0800278 if (PageActive(page) && !active) {
279 __mem_cgroup_move_lists(pc, true);
Balbir Singh66e17072008-02-07 00:13:56 -0800280 continue;
281 }
282 if (!PageActive(page) && active) {
283 __mem_cgroup_move_lists(pc, false);
Balbir Singh66e17072008-02-07 00:13:56 -0800284 continue;
285 }
286
287 /*
288 * Reclaim, per zone
289 * TODO: make the active/inactive lists per zone
290 */
291 if (page_zone(page) != z)
292 continue;
293
Hugh Dickins436c65412008-02-07 00:14:12 -0800294 scan++;
295 list_move(&pc->lru, &pc_list);
Balbir Singh66e17072008-02-07 00:13:56 -0800296
297 if (__isolate_lru_page(page, mode) == 0) {
298 list_move(&page->lru, dst);
299 nr_taken++;
300 }
301 }
302
303 list_splice(&pc_list, src);
304 spin_unlock(&mem_cont->lru_lock);
305
306 *scanned = scan;
307 return nr_taken;
308}
309
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800310/*
311 * Charge the memory controller for page usage.
312 * Return
313 * 0 if the charge was successful
314 * < 0 if the cgroup is over its limit
315 */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800316static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
317 gfp_t gfp_mask, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800318{
319 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800320 struct page_cgroup *pc;
Balbir Singh66e17072008-02-07 00:13:56 -0800321 unsigned long flags;
322 unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800323
324 /*
325 * Should page_cgroup's go to their own slab?
326 * One could optimize the performance of the charging routine
327 * by saving a bit in the page_flags and using it as a lock
328 * to see if the cgroup page already has a page_cgroup associated
329 * with it
330 */
Balbir Singh66e17072008-02-07 00:13:56 -0800331retry:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800332 lock_page_cgroup(page);
333 pc = page_get_page_cgroup(page);
334 /*
335 * The page_cgroup exists and the page has already been accounted
336 */
337 if (pc) {
Balbir Singh66e17072008-02-07 00:13:56 -0800338 if (unlikely(!atomic_inc_not_zero(&pc->ref_cnt))) {
339 /* this page is under being uncharged ? */
340 unlock_page_cgroup(page);
341 cpu_relax();
342 goto retry;
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800343 } else {
344 unlock_page_cgroup(page);
Balbir Singh66e17072008-02-07 00:13:56 -0800345 goto done;
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800346 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800347 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800348 unlock_page_cgroup(page);
349
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800350 pc = kzalloc(sizeof(struct page_cgroup), gfp_mask);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800351 if (pc == NULL)
352 goto err;
353
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800354 /*
Hugh Dickins3be91272008-02-07 00:14:19 -0800355 * We always charge the cgroup the mm_struct belongs to.
356 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800357 * thread group leader migrates. It's possible that mm is not
358 * set, if so charge the init_mm (happens for pagecache usage).
359 */
360 if (!mm)
361 mm = &init_mm;
362
Hugh Dickins3be91272008-02-07 00:14:19 -0800363 rcu_read_lock();
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800364 mem = rcu_dereference(mm->mem_cgroup);
365 /*
366 * For every charge from the cgroup, increment reference
367 * count
368 */
369 css_get(&mem->css);
370 rcu_read_unlock();
371
372 /*
373 * If we created the page_cgroup, we should free it on exceeding
374 * the cgroup limit.
375 */
Balbir Singh0eea1032008-02-07 00:13:57 -0800376 while (res_counter_charge(&mem->res, PAGE_SIZE)) {
Hugh Dickins3be91272008-02-07 00:14:19 -0800377 if (!(gfp_mask & __GFP_WAIT))
378 goto out;
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800379
380 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
Balbir Singh66e17072008-02-07 00:13:56 -0800381 continue;
382
383 /*
384 * try_to_free_mem_cgroup_pages() might not give us a full
385 * picture of reclaim. Some pages are reclaimed and might be
386 * moved to swap cache or just unmapped from the cgroup.
387 * Check the limit again to see if the reclaim reduced the
388 * current usage of the cgroup before giving up
389 */
390 if (res_counter_check_under_limit(&mem->res))
391 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -0800392
393 if (!nr_retries--) {
394 mem_cgroup_out_of_memory(mem, gfp_mask);
395 goto out;
Balbir Singh66e17072008-02-07 00:13:56 -0800396 }
Hugh Dickins3be91272008-02-07 00:14:19 -0800397 congestion_wait(WRITE, HZ/10);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800398 }
399
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800400 atomic_set(&pc->ref_cnt, 1);
401 pc->mem_cgroup = mem;
402 pc->page = page;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800403 pc->flags = 0;
404 if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE)
405 pc->flags |= PAGE_CGROUP_FLAG_CACHE;
Hugh Dickins3be91272008-02-07 00:14:19 -0800406
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800407 if (page_cgroup_assign_new_page_cgroup(page, pc)) {
408 /*
Hugh Dickins3be91272008-02-07 00:14:19 -0800409 * Another charge has been added to this page already.
410 * We take lock_page_cgroup(page) again and read
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800411 * page->cgroup, increment refcnt.... just retry is OK.
412 */
413 res_counter_uncharge(&mem->res, PAGE_SIZE);
414 css_put(&mem->css);
415 kfree(pc);
416 goto retry;
417 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800418
Balbir Singh66e17072008-02-07 00:13:56 -0800419 spin_lock_irqsave(&mem->lru_lock, flags);
420 list_add(&pc->lru, &mem->active_list);
421 spin_unlock_irqrestore(&mem->lru_lock, flags);
422
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800423done:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800424 return 0;
Hugh Dickins3be91272008-02-07 00:14:19 -0800425out:
426 css_put(&mem->css);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800427 kfree(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800428err:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800429 return -ENOMEM;
430}
431
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800432int mem_cgroup_charge(struct page *page, struct mm_struct *mm,
433 gfp_t gfp_mask)
434{
435 return mem_cgroup_charge_common(page, mm, gfp_mask,
436 MEM_CGROUP_CHARGE_TYPE_MAPPED);
437}
438
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800439/*
Balbir Singh8697d332008-02-07 00:13:59 -0800440 * See if the cached pages should be charged at all?
441 */
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800442int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
443 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -0800444{
Balbir Singhac44d352008-02-07 00:14:18 -0800445 int ret = 0;
Balbir Singh8697d332008-02-07 00:13:59 -0800446 struct mem_cgroup *mem;
447 if (!mm)
448 mm = &init_mm;
449
Balbir Singhac44d352008-02-07 00:14:18 -0800450 rcu_read_lock();
Balbir Singh8697d332008-02-07 00:13:59 -0800451 mem = rcu_dereference(mm->mem_cgroup);
Balbir Singhac44d352008-02-07 00:14:18 -0800452 css_get(&mem->css);
453 rcu_read_unlock();
Balbir Singh8697d332008-02-07 00:13:59 -0800454 if (mem->control_type == MEM_CGROUP_TYPE_ALL)
Balbir Singhac44d352008-02-07 00:14:18 -0800455 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800456 MEM_CGROUP_CHARGE_TYPE_CACHE);
Balbir Singhac44d352008-02-07 00:14:18 -0800457 css_put(&mem->css);
458 return ret;
Balbir Singh8697d332008-02-07 00:13:59 -0800459}
460
461/*
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800462 * Uncharging is always a welcome operation, we never complain, simply
463 * uncharge.
464 */
465void mem_cgroup_uncharge(struct page_cgroup *pc)
466{
467 struct mem_cgroup *mem;
468 struct page *page;
Balbir Singh66e17072008-02-07 00:13:56 -0800469 unsigned long flags;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800470
Balbir Singh8697d332008-02-07 00:13:59 -0800471 /*
472 * This can handle cases when a page is not charged at all and we
473 * are switching between handling the control_type.
474 */
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800475 if (!pc)
476 return;
477
478 if (atomic_dec_and_test(&pc->ref_cnt)) {
479 page = pc->page;
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800480 /*
481 * get page->cgroup and clear it under lock.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800482 * force_empty can drop page->cgroup without checking refcnt.
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800483 */
484 if (clear_page_cgroup(page, pc) == pc) {
485 mem = pc->mem_cgroup;
486 css_put(&mem->css);
487 res_counter_uncharge(&mem->res, PAGE_SIZE);
488 spin_lock_irqsave(&mem->lru_lock, flags);
489 list_del_init(&pc->lru);
490 spin_unlock_irqrestore(&mem->lru_lock, flags);
491 kfree(pc);
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800492 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800493 }
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800494}
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800495/*
496 * Returns non-zero if a page (under migration) has valid page_cgroup member.
497 * Refcnt of page_cgroup is incremented.
498 */
499
500int mem_cgroup_prepare_migration(struct page *page)
501{
502 struct page_cgroup *pc;
503 int ret = 0;
504 lock_page_cgroup(page);
505 pc = page_get_page_cgroup(page);
506 if (pc && atomic_inc_not_zero(&pc->ref_cnt))
507 ret = 1;
508 unlock_page_cgroup(page);
509 return ret;
510}
511
512void mem_cgroup_end_migration(struct page *page)
513{
514 struct page_cgroup *pc = page_get_page_cgroup(page);
515 mem_cgroup_uncharge(pc);
516}
517/*
518 * We know both *page* and *newpage* are now not-on-LRU and Pg_locked.
519 * And no race with uncharge() routines because page_cgroup for *page*
520 * has extra one reference by mem_cgroup_prepare_migration.
521 */
522
523void mem_cgroup_page_migration(struct page *page, struct page *newpage)
524{
525 struct page_cgroup *pc;
526retry:
527 pc = page_get_page_cgroup(page);
528 if (!pc)
529 return;
530 if (clear_page_cgroup(page, pc) != pc)
531 goto retry;
532 pc->page = newpage;
533 lock_page_cgroup(newpage);
534 page_assign_page_cgroup(newpage, pc);
535 unlock_page_cgroup(newpage);
536 return;
537}
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800538
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800539/*
540 * This routine traverse page_cgroup in given list and drop them all.
541 * This routine ignores page_cgroup->ref_cnt.
542 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
543 */
544#define FORCE_UNCHARGE_BATCH (128)
545static void
546mem_cgroup_force_empty_list(struct mem_cgroup *mem, struct list_head *list)
547{
548 struct page_cgroup *pc;
549 struct page *page;
550 int count;
551 unsigned long flags;
552
553retry:
554 count = FORCE_UNCHARGE_BATCH;
555 spin_lock_irqsave(&mem->lru_lock, flags);
556
557 while (--count && !list_empty(list)) {
558 pc = list_entry(list->prev, struct page_cgroup, lru);
559 page = pc->page;
560 /* Avoid race with charge */
561 atomic_set(&pc->ref_cnt, 0);
562 if (clear_page_cgroup(page, pc) == pc) {
563 css_put(&mem->css);
564 res_counter_uncharge(&mem->res, PAGE_SIZE);
565 list_del_init(&pc->lru);
566 kfree(pc);
567 } else /* being uncharged ? ...do relax */
568 break;
569 }
570 spin_unlock_irqrestore(&mem->lru_lock, flags);
571 if (!list_empty(list)) {
572 cond_resched();
573 goto retry;
574 }
575 return;
576}
577
578/*
579 * make mem_cgroup's charge to be 0 if there is no task.
580 * This enables deleting this mem_cgroup.
581 */
582
583int mem_cgroup_force_empty(struct mem_cgroup *mem)
584{
585 int ret = -EBUSY;
586 css_get(&mem->css);
587 /*
588 * page reclaim code (kswapd etc..) will move pages between
589` * active_list <-> inactive_list while we don't take a lock.
590 * So, we have to do loop here until all lists are empty.
591 */
592 while (!(list_empty(&mem->active_list) &&
593 list_empty(&mem->inactive_list))) {
594 if (atomic_read(&mem->css.cgroup->count) > 0)
595 goto out;
596 /* drop all page_cgroup in active_list */
597 mem_cgroup_force_empty_list(mem, &mem->active_list);
598 /* drop all page_cgroup in inactive_list */
599 mem_cgroup_force_empty_list(mem, &mem->inactive_list);
600 }
601 ret = 0;
602out:
603 css_put(&mem->css);
604 return ret;
605}
606
607
608
Balbir Singh0eea1032008-02-07 00:13:57 -0800609int mem_cgroup_write_strategy(char *buf, unsigned long long *tmp)
610{
611 *tmp = memparse(buf, &buf);
612 if (*buf != '\0')
613 return -EINVAL;
614
615 /*
616 * Round up the value to the closest page size
617 */
618 *tmp = ((*tmp + PAGE_SIZE - 1) >> PAGE_SHIFT) << PAGE_SHIFT;
619 return 0;
620}
621
622static ssize_t mem_cgroup_read(struct cgroup *cont,
623 struct cftype *cft, struct file *file,
624 char __user *userbuf, size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800625{
626 return res_counter_read(&mem_cgroup_from_cont(cont)->res,
Balbir Singh0eea1032008-02-07 00:13:57 -0800627 cft->private, userbuf, nbytes, ppos,
628 NULL);
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800629}
630
631static ssize_t mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
632 struct file *file, const char __user *userbuf,
633 size_t nbytes, loff_t *ppos)
634{
635 return res_counter_write(&mem_cgroup_from_cont(cont)->res,
Balbir Singh0eea1032008-02-07 00:13:57 -0800636 cft->private, userbuf, nbytes, ppos,
637 mem_cgroup_write_strategy);
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800638}
639
Balbir Singh8697d332008-02-07 00:13:59 -0800640static ssize_t mem_control_type_write(struct cgroup *cont,
641 struct cftype *cft, struct file *file,
642 const char __user *userbuf,
643 size_t nbytes, loff_t *pos)
644{
645 int ret;
646 char *buf, *end;
647 unsigned long tmp;
648 struct mem_cgroup *mem;
649
650 mem = mem_cgroup_from_cont(cont);
651 buf = kmalloc(nbytes + 1, GFP_KERNEL);
652 ret = -ENOMEM;
653 if (buf == NULL)
654 goto out;
655
656 buf[nbytes] = 0;
657 ret = -EFAULT;
658 if (copy_from_user(buf, userbuf, nbytes))
659 goto out_free;
660
661 ret = -EINVAL;
662 tmp = simple_strtoul(buf, &end, 10);
663 if (*end != '\0')
664 goto out_free;
665
666 if (tmp <= MEM_CGROUP_TYPE_UNSPEC || tmp >= MEM_CGROUP_TYPE_MAX)
667 goto out_free;
668
669 mem->control_type = tmp;
670 ret = nbytes;
671out_free:
672 kfree(buf);
673out:
674 return ret;
675}
676
677static ssize_t mem_control_type_read(struct cgroup *cont,
678 struct cftype *cft,
679 struct file *file, char __user *userbuf,
680 size_t nbytes, loff_t *ppos)
681{
682 unsigned long val;
683 char buf[64], *s;
684 struct mem_cgroup *mem;
685
686 mem = mem_cgroup_from_cont(cont);
687 s = buf;
688 val = mem->control_type;
689 s += sprintf(s, "%lu\n", val);
690 return simple_read_from_buffer((void __user *)userbuf, nbytes,
691 ppos, buf, s - buf);
692}
693
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800694
695static ssize_t mem_force_empty_write(struct cgroup *cont,
696 struct cftype *cft, struct file *file,
697 const char __user *userbuf,
698 size_t nbytes, loff_t *ppos)
699{
700 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
701 int ret;
702 ret = mem_cgroup_force_empty(mem);
703 if (!ret)
704 ret = nbytes;
705 return ret;
706}
707
708/*
709 * Note: This should be removed if cgroup supports write-only file.
710 */
711
712static ssize_t mem_force_empty_read(struct cgroup *cont,
713 struct cftype *cft,
714 struct file *file, char __user *userbuf,
715 size_t nbytes, loff_t *ppos)
716{
717 return -EINVAL;
718}
719
720
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800721static struct cftype mem_cgroup_files[] = {
722 {
Balbir Singh0eea1032008-02-07 00:13:57 -0800723 .name = "usage_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800724 .private = RES_USAGE,
725 .read = mem_cgroup_read,
726 },
727 {
Balbir Singh0eea1032008-02-07 00:13:57 -0800728 .name = "limit_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800729 .private = RES_LIMIT,
730 .write = mem_cgroup_write,
731 .read = mem_cgroup_read,
732 },
733 {
734 .name = "failcnt",
735 .private = RES_FAILCNT,
736 .read = mem_cgroup_read,
737 },
Balbir Singh8697d332008-02-07 00:13:59 -0800738 {
739 .name = "control_type",
740 .write = mem_control_type_write,
741 .read = mem_control_type_read,
742 },
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800743 {
744 .name = "force_empty",
745 .write = mem_force_empty_write,
746 .read = mem_force_empty_read,
747 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800748};
749
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800750static struct mem_cgroup init_mem_cgroup;
751
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800752static struct cgroup_subsys_state *
753mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
754{
755 struct mem_cgroup *mem;
756
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800757 if (unlikely((cont->parent) == NULL)) {
758 mem = &init_mem_cgroup;
759 init_mm.mem_cgroup = mem;
760 } else
761 mem = kzalloc(sizeof(struct mem_cgroup), GFP_KERNEL);
762
763 if (mem == NULL)
764 return NULL;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800765
766 res_counter_init(&mem->res);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800767 INIT_LIST_HEAD(&mem->active_list);
768 INIT_LIST_HEAD(&mem->inactive_list);
Balbir Singh66e17072008-02-07 00:13:56 -0800769 spin_lock_init(&mem->lru_lock);
Balbir Singh8697d332008-02-07 00:13:59 -0800770 mem->control_type = MEM_CGROUP_TYPE_ALL;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800771 return &mem->css;
772}
773
774static void mem_cgroup_destroy(struct cgroup_subsys *ss,
775 struct cgroup *cont)
776{
777 kfree(mem_cgroup_from_cont(cont));
778}
779
780static int mem_cgroup_populate(struct cgroup_subsys *ss,
781 struct cgroup *cont)
782{
783 return cgroup_add_files(cont, ss, mem_cgroup_files,
784 ARRAY_SIZE(mem_cgroup_files));
785}
786
Balbir Singh67e465a2008-02-07 00:13:54 -0800787static void mem_cgroup_move_task(struct cgroup_subsys *ss,
788 struct cgroup *cont,
789 struct cgroup *old_cont,
790 struct task_struct *p)
791{
792 struct mm_struct *mm;
793 struct mem_cgroup *mem, *old_mem;
794
795 mm = get_task_mm(p);
796 if (mm == NULL)
797 return;
798
799 mem = mem_cgroup_from_cont(cont);
800 old_mem = mem_cgroup_from_cont(old_cont);
801
802 if (mem == old_mem)
803 goto out;
804
805 /*
806 * Only thread group leaders are allowed to migrate, the mm_struct is
807 * in effect owned by the leader
808 */
809 if (p->tgid != p->pid)
810 goto out;
811
812 css_get(&mem->css);
813 rcu_assign_pointer(mm->mem_cgroup, mem);
814 css_put(&old_mem->css);
815
816out:
817 mmput(mm);
818 return;
819}
820
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800821struct cgroup_subsys mem_cgroup_subsys = {
822 .name = "memory",
823 .subsys_id = mem_cgroup_subsys_id,
824 .create = mem_cgroup_create,
825 .destroy = mem_cgroup_destroy,
826 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -0800827 .attach = mem_cgroup_move_task,
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800828 .early_init = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800829};