1 /* memcontrol.c - Memory Controller
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
6 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
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.
20 #include <linux/res_counter.h>
21 #include <linux/memcontrol.h>
22 #include <linux/cgroup.h>
24 #include <linux/page-flags.h>
25 #include <linux/backing-dev.h>
26 #include <linux/bit_spinlock.h>
27 #include <linux/rcupdate.h>
28 #include <linux/swap.h>
29 #include <linux/spinlock.h>
32 #include <asm/uaccess.h>
34 struct cgroup_subsys mem_cgroup_subsys
;
35 static const int MEM_CGROUP_RECLAIM_RETRIES
= 5;
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.
43 * TODO: Add a water mark for the memory controller. Reclaim will begin when
44 * 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.
49 struct cgroup_subsys_state css
;
51 * the counter to account for memory usage
53 struct res_counter res
;
55 * Per cgroup active and inactive list, similar to the
57 * TODO: Consider making these lists per zone
59 struct list_head active_list
;
60 struct list_head inactive_list
;
62 * spin_lock to protect the per cgroup LRU
65 unsigned long control_type
; /* control RSS or RSS+Pagecache */
69 * 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)
73 #define PAGE_CGROUP_LOCK_BIT 0x0
74 #define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT)
77 * A page_cgroup page is associated with every page descriptor. The
78 * page_cgroup helps us identify information about the cgroup
81 struct list_head lru
; /* per cgroup LRU list */
83 struct mem_cgroup
*mem_cgroup
;
84 atomic_t ref_cnt
; /* Helpful when pages move b/w */
85 /* mapped and cached states */
89 MEM_CGROUP_TYPE_UNSPEC
= 0,
90 MEM_CGROUP_TYPE_MAPPED
,
91 MEM_CGROUP_TYPE_CACHED
,
96 static struct mem_cgroup init_mem_cgroup
;
99 struct mem_cgroup
*mem_cgroup_from_cont(struct cgroup
*cont
)
101 return container_of(cgroup_subsys_state(cont
,
102 mem_cgroup_subsys_id
), struct mem_cgroup
,
107 struct mem_cgroup
*mem_cgroup_from_task(struct task_struct
*p
)
109 return container_of(task_subsys_state(p
, mem_cgroup_subsys_id
),
110 struct mem_cgroup
, css
);
113 inline struct mem_cgroup
*mm_cgroup(struct mm_struct
*mm
)
115 return rcu_dereference(mm
->mem_cgroup
);
118 void mm_init_cgroup(struct mm_struct
*mm
, struct task_struct
*p
)
120 struct mem_cgroup
*mem
;
122 mem
= mem_cgroup_from_task(p
);
124 mm
->mem_cgroup
= mem
;
127 void mm_free_cgroup(struct mm_struct
*mm
)
129 css_put(&mm
->mem_cgroup
->css
);
132 static inline int page_cgroup_locked(struct page
*page
)
134 return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT
,
138 void page_assign_page_cgroup(struct page
*page
, struct page_cgroup
*pc
)
143 * While resetting the page_cgroup we might not hold the
144 * page_cgroup lock. free_hot_cold_page() is an example
148 VM_BUG_ON(!page_cgroup_locked(page
));
149 locked
= (page
->page_cgroup
& PAGE_CGROUP_LOCK
);
150 page
->page_cgroup
= ((unsigned long)pc
| locked
);
153 struct page_cgroup
*page_get_page_cgroup(struct page
*page
)
155 return (struct page_cgroup
*)
156 (page
->page_cgroup
& ~PAGE_CGROUP_LOCK
);
159 static void __always_inline
lock_page_cgroup(struct page
*page
)
161 bit_spin_lock(PAGE_CGROUP_LOCK_BIT
, &page
->page_cgroup
);
162 VM_BUG_ON(!page_cgroup_locked(page
));
165 static void __always_inline
unlock_page_cgroup(struct page
*page
)
167 bit_spin_unlock(PAGE_CGROUP_LOCK_BIT
, &page
->page_cgroup
);
170 static void __mem_cgroup_move_lists(struct page_cgroup
*pc
, bool active
)
173 list_move(&pc
->lru
, &pc
->mem_cgroup
->active_list
);
175 list_move(&pc
->lru
, &pc
->mem_cgroup
->inactive_list
);
179 * This routine assumes that the appropriate zone's lru lock is already held
181 void mem_cgroup_move_lists(struct page_cgroup
*pc
, bool active
)
183 struct mem_cgroup
*mem
;
187 mem
= pc
->mem_cgroup
;
189 spin_lock(&mem
->lru_lock
);
190 __mem_cgroup_move_lists(pc
, active
);
191 spin_unlock(&mem
->lru_lock
);
194 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan
,
195 struct list_head
*dst
,
196 unsigned long *scanned
, int order
,
197 int mode
, struct zone
*z
,
198 struct mem_cgroup
*mem_cont
,
201 unsigned long nr_taken
= 0;
205 struct list_head
*src
;
206 struct page_cgroup
*pc
;
209 src
= &mem_cont
->active_list
;
211 src
= &mem_cont
->inactive_list
;
213 spin_lock(&mem_cont
->lru_lock
);
214 for (scan
= 0; scan
< nr_to_scan
&& !list_empty(src
); scan
++) {
215 pc
= list_entry(src
->prev
, struct page_cgroup
, lru
);
219 if (PageActive(page
) && !active
) {
220 __mem_cgroup_move_lists(pc
, true);
224 if (!PageActive(page
) && active
) {
225 __mem_cgroup_move_lists(pc
, false);
232 * TODO: make the active/inactive lists per zone
234 if (page_zone(page
) != z
)
238 * Check if the meta page went away from under us
240 if (!list_empty(&pc
->lru
))
241 list_move(&pc
->lru
, &pc_list
);
245 if (__isolate_lru_page(page
, mode
) == 0) {
246 list_move(&page
->lru
, dst
);
251 list_splice(&pc_list
, src
);
252 spin_unlock(&mem_cont
->lru_lock
);
259 * Charge the memory controller for page usage.
261 * 0 if the charge was successful
262 * < 0 if the cgroup is over its limit
264 int mem_cgroup_charge(struct page
*page
, struct mm_struct
*mm
)
266 struct mem_cgroup
*mem
;
267 struct page_cgroup
*pc
, *race_pc
;
269 unsigned long nr_retries
= MEM_CGROUP_RECLAIM_RETRIES
;
272 * Should page_cgroup's go to their own slab?
273 * One could optimize the performance of the charging routine
274 * by saving a bit in the page_flags and using it as a lock
275 * to see if the cgroup page already has a page_cgroup associated
279 lock_page_cgroup(page
);
280 pc
= page_get_page_cgroup(page
);
282 * The page_cgroup exists and the page has already been accounted
285 if (unlikely(!atomic_inc_not_zero(&pc
->ref_cnt
))) {
286 /* this page is under being uncharged ? */
287 unlock_page_cgroup(page
);
294 unlock_page_cgroup(page
);
296 pc
= kzalloc(sizeof(struct page_cgroup
), GFP_KERNEL
);
302 * We always charge the cgroup the mm_struct belongs to
303 * the mm_struct's mem_cgroup changes on task migration if the
304 * thread group leader migrates. It's possible that mm is not
305 * set, if so charge the init_mm (happens for pagecache usage).
310 mem
= rcu_dereference(mm
->mem_cgroup
);
312 * For every charge from the cgroup, increment reference
319 * If we created the page_cgroup, we should free it on exceeding
322 while (res_counter_charge(&mem
->res
, PAGE_SIZE
)) {
323 if (try_to_free_mem_cgroup_pages(mem
))
327 * try_to_free_mem_cgroup_pages() might not give us a full
328 * picture of reclaim. Some pages are reclaimed and might be
329 * moved to swap cache or just unmapped from the cgroup.
330 * Check the limit again to see if the reclaim reduced the
331 * current usage of the cgroup before giving up
333 if (res_counter_check_under_limit(&mem
->res
))
336 * Since we control both RSS and cache, we end up with a
337 * very interesting scenario where we end up reclaiming
338 * memory (essentially RSS), since the memory is pushed
339 * to swap cache, we eventually end up adding those
340 * pages back to our list. Hence we give ourselves a
341 * few chances before we fail
343 else if (nr_retries
--) {
344 congestion_wait(WRITE
, HZ
/10);
349 mem_cgroup_out_of_memory(mem
, GFP_KERNEL
);
353 lock_page_cgroup(page
);
355 * Check if somebody else beat us to allocating the page_cgroup
357 race_pc
= page_get_page_cgroup(page
);
361 atomic_inc(&pc
->ref_cnt
);
362 res_counter_uncharge(&mem
->res
, PAGE_SIZE
);
367 atomic_set(&pc
->ref_cnt
, 1);
368 pc
->mem_cgroup
= mem
;
370 page_assign_page_cgroup(page
, pc
);
372 spin_lock_irqsave(&mem
->lru_lock
, flags
);
373 list_add(&pc
->lru
, &mem
->active_list
);
374 spin_unlock_irqrestore(&mem
->lru_lock
, flags
);
377 unlock_page_cgroup(page
);
386 * See if the cached pages should be charged at all?
388 int mem_cgroup_cache_charge(struct page
*page
, struct mm_struct
*mm
)
390 struct mem_cgroup
*mem
;
394 mem
= rcu_dereference(mm
->mem_cgroup
);
395 if (mem
->control_type
== MEM_CGROUP_TYPE_ALL
)
396 return mem_cgroup_charge(page
, mm
);
402 * Uncharging is always a welcome operation, we never complain, simply
405 void mem_cgroup_uncharge(struct page_cgroup
*pc
)
407 struct mem_cgroup
*mem
;
412 * This can handle cases when a page is not charged at all and we
413 * are switching between handling the control_type.
418 if (atomic_dec_and_test(&pc
->ref_cnt
)) {
420 lock_page_cgroup(page
);
421 mem
= pc
->mem_cgroup
;
423 page_assign_page_cgroup(page
, NULL
);
424 unlock_page_cgroup(page
);
425 res_counter_uncharge(&mem
->res
, PAGE_SIZE
);
427 spin_lock_irqsave(&mem
->lru_lock
, flags
);
428 list_del_init(&pc
->lru
);
429 spin_unlock_irqrestore(&mem
->lru_lock
, flags
);
434 int mem_cgroup_write_strategy(char *buf
, unsigned long long *tmp
)
436 *tmp
= memparse(buf
, &buf
);
441 * Round up the value to the closest page size
443 *tmp
= ((*tmp
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
) << PAGE_SHIFT
;
447 static ssize_t
mem_cgroup_read(struct cgroup
*cont
,
448 struct cftype
*cft
, struct file
*file
,
449 char __user
*userbuf
, size_t nbytes
, loff_t
*ppos
)
451 return res_counter_read(&mem_cgroup_from_cont(cont
)->res
,
452 cft
->private, userbuf
, nbytes
, ppos
,
456 static ssize_t
mem_cgroup_write(struct cgroup
*cont
, struct cftype
*cft
,
457 struct file
*file
, const char __user
*userbuf
,
458 size_t nbytes
, loff_t
*ppos
)
460 return res_counter_write(&mem_cgroup_from_cont(cont
)->res
,
461 cft
->private, userbuf
, nbytes
, ppos
,
462 mem_cgroup_write_strategy
);
465 static ssize_t
mem_control_type_write(struct cgroup
*cont
,
466 struct cftype
*cft
, struct file
*file
,
467 const char __user
*userbuf
,
468 size_t nbytes
, loff_t
*pos
)
473 struct mem_cgroup
*mem
;
475 mem
= mem_cgroup_from_cont(cont
);
476 buf
= kmalloc(nbytes
+ 1, GFP_KERNEL
);
483 if (copy_from_user(buf
, userbuf
, nbytes
))
487 tmp
= simple_strtoul(buf
, &end
, 10);
491 if (tmp
<= MEM_CGROUP_TYPE_UNSPEC
|| tmp
>= MEM_CGROUP_TYPE_MAX
)
494 mem
->control_type
= tmp
;
502 static ssize_t
mem_control_type_read(struct cgroup
*cont
,
504 struct file
*file
, char __user
*userbuf
,
505 size_t nbytes
, loff_t
*ppos
)
509 struct mem_cgroup
*mem
;
511 mem
= mem_cgroup_from_cont(cont
);
513 val
= mem
->control_type
;
514 s
+= sprintf(s
, "%lu\n", val
);
515 return simple_read_from_buffer((void __user
*)userbuf
, nbytes
,
519 static struct cftype mem_cgroup_files
[] = {
521 .name
= "usage_in_bytes",
522 .private = RES_USAGE
,
523 .read
= mem_cgroup_read
,
526 .name
= "limit_in_bytes",
527 .private = RES_LIMIT
,
528 .write
= mem_cgroup_write
,
529 .read
= mem_cgroup_read
,
533 .private = RES_FAILCNT
,
534 .read
= mem_cgroup_read
,
537 .name
= "control_type",
538 .write
= mem_control_type_write
,
539 .read
= mem_control_type_read
,
543 static struct mem_cgroup init_mem_cgroup
;
545 static struct cgroup_subsys_state
*
546 mem_cgroup_create(struct cgroup_subsys
*ss
, struct cgroup
*cont
)
548 struct mem_cgroup
*mem
;
550 if (unlikely((cont
->parent
) == NULL
)) {
551 mem
= &init_mem_cgroup
;
552 init_mm
.mem_cgroup
= mem
;
554 mem
= kzalloc(sizeof(struct mem_cgroup
), GFP_KERNEL
);
559 res_counter_init(&mem
->res
);
560 INIT_LIST_HEAD(&mem
->active_list
);
561 INIT_LIST_HEAD(&mem
->inactive_list
);
562 spin_lock_init(&mem
->lru_lock
);
563 mem
->control_type
= MEM_CGROUP_TYPE_ALL
;
567 static void mem_cgroup_destroy(struct cgroup_subsys
*ss
,
570 kfree(mem_cgroup_from_cont(cont
));
573 static int mem_cgroup_populate(struct cgroup_subsys
*ss
,
576 return cgroup_add_files(cont
, ss
, mem_cgroup_files
,
577 ARRAY_SIZE(mem_cgroup_files
));
580 static void mem_cgroup_move_task(struct cgroup_subsys
*ss
,
582 struct cgroup
*old_cont
,
583 struct task_struct
*p
)
585 struct mm_struct
*mm
;
586 struct mem_cgroup
*mem
, *old_mem
;
592 mem
= mem_cgroup_from_cont(cont
);
593 old_mem
= mem_cgroup_from_cont(old_cont
);
599 * Only thread group leaders are allowed to migrate, the mm_struct is
600 * in effect owned by the leader
602 if (p
->tgid
!= p
->pid
)
606 rcu_assign_pointer(mm
->mem_cgroup
, mem
);
607 css_put(&old_mem
->css
);
614 struct cgroup_subsys mem_cgroup_subsys
= {
616 .subsys_id
= mem_cgroup_subsys_id
,
617 .create
= mem_cgroup_create
,
618 .destroy
= mem_cgroup_destroy
,
619 .populate
= mem_cgroup_populate
,
620 .attach
= mem_cgroup_move_task
,