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/pagemap.h>
25 #include <linux/smp.h>
26 #include <linux/page-flags.h>
27 #include <linux/backing-dev.h>
28 #include <linux/bit_spinlock.h>
29 #include <linux/rcupdate.h>
30 #include <linux/mutex.h>
31 #include <linux/slab.h>
32 #include <linux/swap.h>
33 #include <linux/spinlock.h>
35 #include <linux/seq_file.h>
36 #include <linux/vmalloc.h>
37 #include <linux/mm_inline.h>
38 #include <linux/page_cgroup.h>
40 #include <asm/uaccess.h>
42 struct cgroup_subsys mem_cgroup_subsys __read_mostly
;
43 #define MEM_CGROUP_RECLAIM_RETRIES 5
45 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
46 /* Turned on only when memory cgroup is enabled && really_do_swap_account = 0 */
47 int do_swap_account __read_mostly
;
48 static int really_do_swap_account __initdata
= 1; /* for remember boot option*/
50 #define do_swap_account (0)
55 * Statistics for memory cgroup.
57 enum mem_cgroup_stat_index
{
59 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
61 MEM_CGROUP_STAT_CACHE
, /* # of pages charged as cache */
62 MEM_CGROUP_STAT_RSS
, /* # of pages charged as rss */
63 MEM_CGROUP_STAT_PGPGIN_COUNT
, /* # of pages paged in */
64 MEM_CGROUP_STAT_PGPGOUT_COUNT
, /* # of pages paged out */
66 MEM_CGROUP_STAT_NSTATS
,
69 struct mem_cgroup_stat_cpu
{
70 s64 count
[MEM_CGROUP_STAT_NSTATS
];
71 } ____cacheline_aligned_in_smp
;
73 struct mem_cgroup_stat
{
74 struct mem_cgroup_stat_cpu cpustat
[0];
78 * For accounting under irq disable, no need for increment preempt count.
80 static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu
*stat
,
81 enum mem_cgroup_stat_index idx
, int val
)
83 stat
->count
[idx
] += val
;
86 static s64
mem_cgroup_read_stat(struct mem_cgroup_stat
*stat
,
87 enum mem_cgroup_stat_index idx
)
91 for_each_possible_cpu(cpu
)
92 ret
+= stat
->cpustat
[cpu
].count
[idx
];
97 * per-zone information in memory controller.
99 struct mem_cgroup_per_zone
{
101 * spin_lock to protect the per cgroup LRU
104 struct list_head lists
[NR_LRU_LISTS
];
105 unsigned long count
[NR_LRU_LISTS
];
107 /* Macro for accessing counter */
108 #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
110 struct mem_cgroup_per_node
{
111 struct mem_cgroup_per_zone zoneinfo
[MAX_NR_ZONES
];
114 struct mem_cgroup_lru_info
{
115 struct mem_cgroup_per_node
*nodeinfo
[MAX_NUMNODES
];
119 * The memory controller data structure. The memory controller controls both
120 * page cache and RSS per cgroup. We would eventually like to provide
121 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
122 * to help the administrator determine what knobs to tune.
124 * TODO: Add a water mark for the memory controller. Reclaim will begin when
125 * we hit the water mark. May be even add a low water mark, such that
126 * no reclaim occurs from a cgroup at it's low water mark, this is
127 * a feature that will be implemented much later in the future.
130 struct cgroup_subsys_state css
;
132 * the counter to account for memory usage
134 struct res_counter res
;
136 * the counter to account for mem+swap usage.
138 struct res_counter memsw
;
140 * Per cgroup active and inactive list, similar to the
141 * per zone LRU lists.
143 struct mem_cgroup_lru_info info
;
145 int prev_priority
; /* for recording reclaim priority */
149 * statistics. This must be placed at the end of memcg.
151 struct mem_cgroup_stat stat
;
155 MEM_CGROUP_CHARGE_TYPE_CACHE
= 0,
156 MEM_CGROUP_CHARGE_TYPE_MAPPED
,
157 MEM_CGROUP_CHARGE_TYPE_SHMEM
, /* used by page migration of shmem */
158 MEM_CGROUP_CHARGE_TYPE_FORCE
, /* used by force_empty */
159 MEM_CGROUP_CHARGE_TYPE_SWAPOUT
, /* for accounting swapcache */
163 /* only for here (for easy reading.) */
164 #define PCGF_CACHE (1UL << PCG_CACHE)
165 #define PCGF_USED (1UL << PCG_USED)
166 #define PCGF_ACTIVE (1UL << PCG_ACTIVE)
167 #define PCGF_LOCK (1UL << PCG_LOCK)
168 #define PCGF_FILE (1UL << PCG_FILE)
169 static const unsigned long
170 pcg_default_flags
[NR_CHARGE_TYPE
] = {
171 PCGF_CACHE
| PCGF_FILE
| PCGF_USED
| PCGF_LOCK
, /* File Cache */
172 PCGF_ACTIVE
| PCGF_USED
| PCGF_LOCK
, /* Anon */
173 PCGF_ACTIVE
| PCGF_CACHE
| PCGF_USED
| PCGF_LOCK
, /* Shmem */
178 /* for encoding cft->private value on file */
181 #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
182 #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
183 #define MEMFILE_ATTR(val) ((val) & 0xffff)
185 static void mem_cgroup_get(struct mem_cgroup
*mem
);
186 static void mem_cgroup_put(struct mem_cgroup
*mem
);
189 * Always modified under lru lock. Then, not necessary to preempt_disable()
191 static void mem_cgroup_charge_statistics(struct mem_cgroup
*mem
,
192 struct page_cgroup
*pc
,
195 int val
= (charge
)? 1 : -1;
196 struct mem_cgroup_stat
*stat
= &mem
->stat
;
197 struct mem_cgroup_stat_cpu
*cpustat
;
199 VM_BUG_ON(!irqs_disabled());
201 cpustat
= &stat
->cpustat
[smp_processor_id()];
202 if (PageCgroupCache(pc
))
203 __mem_cgroup_stat_add_safe(cpustat
, MEM_CGROUP_STAT_CACHE
, val
);
205 __mem_cgroup_stat_add_safe(cpustat
, MEM_CGROUP_STAT_RSS
, val
);
208 __mem_cgroup_stat_add_safe(cpustat
,
209 MEM_CGROUP_STAT_PGPGIN_COUNT
, 1);
211 __mem_cgroup_stat_add_safe(cpustat
,
212 MEM_CGROUP_STAT_PGPGOUT_COUNT
, 1);
215 static struct mem_cgroup_per_zone
*
216 mem_cgroup_zoneinfo(struct mem_cgroup
*mem
, int nid
, int zid
)
218 return &mem
->info
.nodeinfo
[nid
]->zoneinfo
[zid
];
221 static struct mem_cgroup_per_zone
*
222 page_cgroup_zoneinfo(struct page_cgroup
*pc
)
224 struct mem_cgroup
*mem
= pc
->mem_cgroup
;
225 int nid
= page_cgroup_nid(pc
);
226 int zid
= page_cgroup_zid(pc
);
228 return mem_cgroup_zoneinfo(mem
, nid
, zid
);
231 static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup
*mem
,
235 struct mem_cgroup_per_zone
*mz
;
238 for_each_online_node(nid
)
239 for (zid
= 0; zid
< MAX_NR_ZONES
; zid
++) {
240 mz
= mem_cgroup_zoneinfo(mem
, nid
, zid
);
241 total
+= MEM_CGROUP_ZSTAT(mz
, idx
);
246 static struct mem_cgroup
*mem_cgroup_from_cont(struct cgroup
*cont
)
248 return container_of(cgroup_subsys_state(cont
,
249 mem_cgroup_subsys_id
), struct mem_cgroup
,
253 struct mem_cgroup
*mem_cgroup_from_task(struct task_struct
*p
)
256 * mm_update_next_owner() may clear mm->owner to NULL
257 * if it races with swapoff, page migration, etc.
258 * So this can be called with p == NULL.
263 return container_of(task_subsys_state(p
, mem_cgroup_subsys_id
),
264 struct mem_cgroup
, css
);
267 static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone
*mz
,
268 struct page_cgroup
*pc
)
272 if (PageCgroupUnevictable(pc
))
273 lru
= LRU_UNEVICTABLE
;
275 if (PageCgroupActive(pc
))
277 if (PageCgroupFile(pc
))
281 MEM_CGROUP_ZSTAT(mz
, lru
) -= 1;
283 mem_cgroup_charge_statistics(pc
->mem_cgroup
, pc
, false);
287 static void __mem_cgroup_add_list(struct mem_cgroup_per_zone
*mz
,
288 struct page_cgroup
*pc
, bool hot
)
292 if (PageCgroupUnevictable(pc
))
293 lru
= LRU_UNEVICTABLE
;
295 if (PageCgroupActive(pc
))
297 if (PageCgroupFile(pc
))
301 MEM_CGROUP_ZSTAT(mz
, lru
) += 1;
303 list_add(&pc
->lru
, &mz
->lists
[lru
]);
305 list_add_tail(&pc
->lru
, &mz
->lists
[lru
]);
307 mem_cgroup_charge_statistics(pc
->mem_cgroup
, pc
, true);
310 static void __mem_cgroup_move_lists(struct page_cgroup
*pc
, enum lru_list lru
)
312 struct mem_cgroup_per_zone
*mz
= page_cgroup_zoneinfo(pc
);
313 int active
= PageCgroupActive(pc
);
314 int file
= PageCgroupFile(pc
);
315 int unevictable
= PageCgroupUnevictable(pc
);
316 enum lru_list from
= unevictable
? LRU_UNEVICTABLE
:
317 (LRU_FILE
* !!file
+ !!active
);
322 MEM_CGROUP_ZSTAT(mz
, from
) -= 1;
324 * However this is done under mz->lru_lock, another flags, which
325 * are not related to LRU, will be modified from out-of-lock.
326 * We have to use atomic set/clear flags.
328 if (is_unevictable_lru(lru
)) {
329 ClearPageCgroupActive(pc
);
330 SetPageCgroupUnevictable(pc
);
332 if (is_active_lru(lru
))
333 SetPageCgroupActive(pc
);
335 ClearPageCgroupActive(pc
);
336 ClearPageCgroupUnevictable(pc
);
339 MEM_CGROUP_ZSTAT(mz
, lru
) += 1;
340 list_move(&pc
->lru
, &mz
->lists
[lru
]);
343 int task_in_mem_cgroup(struct task_struct
*task
, const struct mem_cgroup
*mem
)
348 ret
= task
->mm
&& mm_match_cgroup(task
->mm
, mem
);
354 * This routine assumes that the appropriate zone's lru lock is already held
356 void mem_cgroup_move_lists(struct page
*page
, enum lru_list lru
)
358 struct page_cgroup
*pc
;
359 struct mem_cgroup_per_zone
*mz
;
362 if (mem_cgroup_subsys
.disabled
)
366 * We cannot lock_page_cgroup while holding zone's lru_lock,
367 * because other holders of lock_page_cgroup can be interrupted
368 * with an attempt to rotate_reclaimable_page. But we cannot
369 * safely get to page_cgroup without it, so just try_lock it:
370 * mem_cgroup_isolate_pages allows for page left on wrong list.
372 pc
= lookup_page_cgroup(page
);
373 if (!trylock_page_cgroup(pc
))
375 if (pc
&& PageCgroupUsed(pc
)) {
376 mz
= page_cgroup_zoneinfo(pc
);
377 spin_lock_irqsave(&mz
->lru_lock
, flags
);
378 __mem_cgroup_move_lists(pc
, lru
);
379 spin_unlock_irqrestore(&mz
->lru_lock
, flags
);
381 unlock_page_cgroup(pc
);
385 * Calculate mapped_ratio under memory controller. This will be used in
386 * vmscan.c for deteremining we have to reclaim mapped pages.
388 int mem_cgroup_calc_mapped_ratio(struct mem_cgroup
*mem
)
393 * usage is recorded in bytes. But, here, we assume the number of
394 * physical pages can be represented by "long" on any arch.
396 total
= (long) (mem
->res
.usage
>> PAGE_SHIFT
) + 1L;
397 rss
= (long)mem_cgroup_read_stat(&mem
->stat
, MEM_CGROUP_STAT_RSS
);
398 return (int)((rss
* 100L) / total
);
402 * prev_priority control...this will be used in memory reclaim path.
404 int mem_cgroup_get_reclaim_priority(struct mem_cgroup
*mem
)
406 return mem
->prev_priority
;
409 void mem_cgroup_note_reclaim_priority(struct mem_cgroup
*mem
, int priority
)
411 if (priority
< mem
->prev_priority
)
412 mem
->prev_priority
= priority
;
415 void mem_cgroup_record_reclaim_priority(struct mem_cgroup
*mem
, int priority
)
417 mem
->prev_priority
= priority
;
421 * Calculate # of pages to be scanned in this priority/zone.
424 * priority starts from "DEF_PRIORITY" and decremented in each loop.
425 * (see include/linux/mmzone.h)
428 long mem_cgroup_calc_reclaim(struct mem_cgroup
*mem
, struct zone
*zone
,
429 int priority
, enum lru_list lru
)
432 int nid
= zone
->zone_pgdat
->node_id
;
433 int zid
= zone_idx(zone
);
434 struct mem_cgroup_per_zone
*mz
= mem_cgroup_zoneinfo(mem
, nid
, zid
);
436 nr_pages
= MEM_CGROUP_ZSTAT(mz
, lru
);
438 return (nr_pages
>> priority
);
441 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan
,
442 struct list_head
*dst
,
443 unsigned long *scanned
, int order
,
444 int mode
, struct zone
*z
,
445 struct mem_cgroup
*mem_cont
,
446 int active
, int file
)
448 unsigned long nr_taken
= 0;
452 struct list_head
*src
;
453 struct page_cgroup
*pc
, *tmp
;
454 int nid
= z
->zone_pgdat
->node_id
;
455 int zid
= zone_idx(z
);
456 struct mem_cgroup_per_zone
*mz
;
457 int lru
= LRU_FILE
* !!file
+ !!active
;
460 mz
= mem_cgroup_zoneinfo(mem_cont
, nid
, zid
);
461 src
= &mz
->lists
[lru
];
463 spin_lock(&mz
->lru_lock
);
465 list_for_each_entry_safe_reverse(pc
, tmp
, src
, lru
) {
466 if (scan
>= nr_to_scan
)
468 if (unlikely(!PageCgroupUsed(pc
)))
472 if (unlikely(!PageLRU(page
)))
476 * TODO: play better with lumpy reclaim, grabbing anything.
478 if (PageUnevictable(page
) ||
479 (PageActive(page
) && !active
) ||
480 (!PageActive(page
) && active
)) {
481 __mem_cgroup_move_lists(pc
, page_lru(page
));
486 list_move(&pc
->lru
, &pc_list
);
488 if (__isolate_lru_page(page
, mode
, file
) == 0) {
489 list_move(&page
->lru
, dst
);
494 list_splice(&pc_list
, src
);
495 spin_unlock(&mz
->lru_lock
);
502 * Unlike exported interface, "oom" parameter is added. if oom==true,
503 * oom-killer can be invoked.
505 static int __mem_cgroup_try_charge(struct mm_struct
*mm
,
506 gfp_t gfp_mask
, struct mem_cgroup
**memcg
,
509 struct mem_cgroup
*mem
;
510 int nr_retries
= MEM_CGROUP_RECLAIM_RETRIES
;
512 * We always charge the cgroup the mm_struct belongs to.
513 * The mm_struct's mem_cgroup changes on task migration if the
514 * thread group leader migrates. It's possible that mm is not
515 * set, if so charge the init_mm (happens for pagecache usage).
517 if (likely(!*memcg
)) {
519 mem
= mem_cgroup_from_task(rcu_dereference(mm
->owner
));
520 if (unlikely(!mem
)) {
525 * For every charge from the cgroup, increment reference count
539 ret
= res_counter_charge(&mem
->res
, PAGE_SIZE
);
541 if (!do_swap_account
)
543 ret
= res_counter_charge(&mem
->memsw
, PAGE_SIZE
);
546 /* mem+swap counter fails */
547 res_counter_uncharge(&mem
->res
, PAGE_SIZE
);
550 if (!(gfp_mask
& __GFP_WAIT
))
553 if (try_to_free_mem_cgroup_pages(mem
, gfp_mask
, noswap
))
557 * try_to_free_mem_cgroup_pages() might not give us a full
558 * picture of reclaim. Some pages are reclaimed and might be
559 * moved to swap cache or just unmapped from the cgroup.
560 * Check the limit again to see if the reclaim reduced the
561 * current usage of the cgroup before giving up
564 if (!do_swap_account
&&
565 res_counter_check_under_limit(&mem
->res
))
567 if (do_swap_account
&&
568 res_counter_check_under_limit(&mem
->memsw
))
573 mem_cgroup_out_of_memory(mem
, gfp_mask
);
584 * mem_cgroup_try_charge - get charge of PAGE_SIZE.
585 * @mm: an mm_struct which is charged against. (when *memcg is NULL)
586 * @gfp_mask: gfp_mask for reclaim.
587 * @memcg: a pointer to memory cgroup which is charged against.
589 * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated
590 * memory cgroup from @mm is got and stored in *memcg.
592 * Returns 0 if success. -ENOMEM at failure.
593 * This call can invoke OOM-Killer.
596 int mem_cgroup_try_charge(struct mm_struct
*mm
,
597 gfp_t mask
, struct mem_cgroup
**memcg
)
599 return __mem_cgroup_try_charge(mm
, mask
, memcg
, true);
603 * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be
604 * USED state. If already USED, uncharge and return.
607 static void __mem_cgroup_commit_charge(struct mem_cgroup
*mem
,
608 struct page_cgroup
*pc
,
609 enum charge_type ctype
)
611 struct mem_cgroup_per_zone
*mz
;
614 /* try_charge() can return NULL to *memcg, taking care of it. */
618 lock_page_cgroup(pc
);
619 if (unlikely(PageCgroupUsed(pc
))) {
620 unlock_page_cgroup(pc
);
621 res_counter_uncharge(&mem
->res
, PAGE_SIZE
);
623 res_counter_uncharge(&mem
->memsw
, PAGE_SIZE
);
627 pc
->mem_cgroup
= mem
;
629 * If a page is accounted as a page cache, insert to inactive list.
630 * If anon, insert to active list.
632 pc
->flags
= pcg_default_flags
[ctype
];
634 mz
= page_cgroup_zoneinfo(pc
);
636 spin_lock_irqsave(&mz
->lru_lock
, flags
);
637 __mem_cgroup_add_list(mz
, pc
, true);
638 spin_unlock_irqrestore(&mz
->lru_lock
, flags
);
639 unlock_page_cgroup(pc
);
643 * mem_cgroup_move_account - move account of the page
644 * @pc: page_cgroup of the page.
645 * @from: mem_cgroup which the page is moved from.
646 * @to: mem_cgroup which the page is moved to. @from != @to.
648 * The caller must confirm following.
650 * 2. lru_lock of old mem_cgroup(@from) should be held.
652 * returns 0 at success,
653 * returns -EBUSY when lock is busy or "pc" is unstable.
655 * This function does "uncharge" from old cgroup but doesn't do "charge" to
656 * new cgroup. It should be done by a caller.
659 static int mem_cgroup_move_account(struct page_cgroup
*pc
,
660 struct mem_cgroup
*from
, struct mem_cgroup
*to
)
662 struct mem_cgroup_per_zone
*from_mz
, *to_mz
;
666 VM_BUG_ON(!irqs_disabled());
667 VM_BUG_ON(from
== to
);
669 nid
= page_cgroup_nid(pc
);
670 zid
= page_cgroup_zid(pc
);
671 from_mz
= mem_cgroup_zoneinfo(from
, nid
, zid
);
672 to_mz
= mem_cgroup_zoneinfo(to
, nid
, zid
);
675 if (!trylock_page_cgroup(pc
))
678 if (!PageCgroupUsed(pc
))
681 if (pc
->mem_cgroup
!= from
)
684 if (spin_trylock(&to_mz
->lru_lock
)) {
685 __mem_cgroup_remove_list(from_mz
, pc
);
687 res_counter_uncharge(&from
->res
, PAGE_SIZE
);
689 res_counter_uncharge(&from
->memsw
, PAGE_SIZE
);
692 __mem_cgroup_add_list(to_mz
, pc
, false);
694 spin_unlock(&to_mz
->lru_lock
);
697 unlock_page_cgroup(pc
);
702 * move charges to its parent.
705 static int mem_cgroup_move_parent(struct page_cgroup
*pc
,
706 struct mem_cgroup
*child
,
709 struct cgroup
*cg
= child
->css
.cgroup
;
710 struct cgroup
*pcg
= cg
->parent
;
711 struct mem_cgroup
*parent
;
712 struct mem_cgroup_per_zone
*mz
;
720 parent
= mem_cgroup_from_cont(pcg
);
722 ret
= __mem_cgroup_try_charge(NULL
, gfp_mask
, &parent
, false);
726 mz
= mem_cgroup_zoneinfo(child
,
727 page_cgroup_nid(pc
), page_cgroup_zid(pc
));
729 spin_lock_irqsave(&mz
->lru_lock
, flags
);
730 ret
= mem_cgroup_move_account(pc
, child
, parent
);
731 spin_unlock_irqrestore(&mz
->lru_lock
, flags
);
733 /* drop extra refcnt */
734 css_put(&parent
->css
);
735 /* uncharge if move fails */
737 res_counter_uncharge(&parent
->res
, PAGE_SIZE
);
739 res_counter_uncharge(&parent
->memsw
, PAGE_SIZE
);
746 * Charge the memory controller for page usage.
748 * 0 if the charge was successful
749 * < 0 if the cgroup is over its limit
751 static int mem_cgroup_charge_common(struct page
*page
, struct mm_struct
*mm
,
752 gfp_t gfp_mask
, enum charge_type ctype
,
753 struct mem_cgroup
*memcg
)
755 struct mem_cgroup
*mem
;
756 struct page_cgroup
*pc
;
759 pc
= lookup_page_cgroup(page
);
760 /* can happen at boot */
766 ret
= __mem_cgroup_try_charge(mm
, gfp_mask
, &mem
, true);
770 __mem_cgroup_commit_charge(mem
, pc
, ctype
);
774 int mem_cgroup_newpage_charge(struct page
*page
,
775 struct mm_struct
*mm
, gfp_t gfp_mask
)
777 if (mem_cgroup_subsys
.disabled
)
779 if (PageCompound(page
))
782 * If already mapped, we don't have to account.
783 * If page cache, page->mapping has address_space.
784 * But page->mapping may have out-of-use anon_vma pointer,
785 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
788 if (page_mapped(page
) || (page
->mapping
&& !PageAnon(page
)))
792 return mem_cgroup_charge_common(page
, mm
, gfp_mask
,
793 MEM_CGROUP_CHARGE_TYPE_MAPPED
, NULL
);
796 int mem_cgroup_cache_charge(struct page
*page
, struct mm_struct
*mm
,
799 if (mem_cgroup_subsys
.disabled
)
801 if (PageCompound(page
))
804 * Corner case handling. This is called from add_to_page_cache()
805 * in usual. But some FS (shmem) precharges this page before calling it
806 * and call add_to_page_cache() with GFP_NOWAIT.
808 * For GFP_NOWAIT case, the page may be pre-charged before calling
809 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
810 * charge twice. (It works but has to pay a bit larger cost.)
812 if (!(gfp_mask
& __GFP_WAIT
)) {
813 struct page_cgroup
*pc
;
816 pc
= lookup_page_cgroup(page
);
819 lock_page_cgroup(pc
);
820 if (PageCgroupUsed(pc
)) {
821 unlock_page_cgroup(pc
);
824 unlock_page_cgroup(pc
);
830 if (page_is_file_cache(page
))
831 return mem_cgroup_charge_common(page
, mm
, gfp_mask
,
832 MEM_CGROUP_CHARGE_TYPE_CACHE
, NULL
);
834 return mem_cgroup_charge_common(page
, mm
, gfp_mask
,
835 MEM_CGROUP_CHARGE_TYPE_SHMEM
, NULL
);
838 int mem_cgroup_try_charge_swapin(struct mm_struct
*mm
,
840 gfp_t mask
, struct mem_cgroup
**ptr
)
842 struct mem_cgroup
*mem
;
845 if (mem_cgroup_subsys
.disabled
)
848 if (!do_swap_account
)
852 * A racing thread's fault, or swapoff, may have already updated
853 * the pte, and even removed page from swap cache: return success
854 * to go on to do_swap_page()'s pte_same() test, which should fail.
856 if (!PageSwapCache(page
))
859 ent
.val
= page_private(page
);
861 mem
= lookup_swap_cgroup(ent
);
862 if (!mem
|| mem
->obsolete
)
865 return __mem_cgroup_try_charge(NULL
, mask
, ptr
, true);
869 return __mem_cgroup_try_charge(mm
, mask
, ptr
, true);
874 int mem_cgroup_cache_charge_swapin(struct page
*page
,
875 struct mm_struct
*mm
, gfp_t mask
, bool locked
)
879 if (mem_cgroup_subsys
.disabled
)
886 * If not locked, the page can be dropped from SwapCache until
889 if (PageSwapCache(page
)) {
890 struct mem_cgroup
*mem
= NULL
;
893 ent
.val
= page_private(page
);
894 if (do_swap_account
) {
895 mem
= lookup_swap_cgroup(ent
);
896 if (mem
&& mem
->obsolete
)
901 ret
= mem_cgroup_charge_common(page
, mm
, mask
,
902 MEM_CGROUP_CHARGE_TYPE_SHMEM
, mem
);
904 if (!ret
&& do_swap_account
) {
905 /* avoid double counting */
906 mem
= swap_cgroup_record(ent
, NULL
);
908 res_counter_uncharge(&mem
->memsw
, PAGE_SIZE
);
920 void mem_cgroup_commit_charge_swapin(struct page
*page
, struct mem_cgroup
*ptr
)
922 struct page_cgroup
*pc
;
924 if (mem_cgroup_subsys
.disabled
)
928 pc
= lookup_page_cgroup(page
);
929 __mem_cgroup_commit_charge(ptr
, pc
, MEM_CGROUP_CHARGE_TYPE_MAPPED
);
931 * Now swap is on-memory. This means this page may be
932 * counted both as mem and swap....double count.
933 * Fix it by uncharging from memsw. This SwapCache is stable
934 * because we're still under lock_page().
936 if (do_swap_account
) {
937 swp_entry_t ent
= {.val
= page_private(page
)};
938 struct mem_cgroup
*memcg
;
939 memcg
= swap_cgroup_record(ent
, NULL
);
941 /* If memcg is obsolete, memcg can be != ptr */
942 res_counter_uncharge(&memcg
->memsw
, PAGE_SIZE
);
943 mem_cgroup_put(memcg
);
949 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup
*mem
)
951 if (mem_cgroup_subsys
.disabled
)
955 res_counter_uncharge(&mem
->res
, PAGE_SIZE
);
957 res_counter_uncharge(&mem
->memsw
, PAGE_SIZE
);
963 * uncharge if !page_mapped(page)
965 static struct mem_cgroup
*
966 __mem_cgroup_uncharge_common(struct page
*page
, enum charge_type ctype
)
968 struct page_cgroup
*pc
;
969 struct mem_cgroup
*mem
= NULL
;
970 struct mem_cgroup_per_zone
*mz
;
973 if (mem_cgroup_subsys
.disabled
)
976 if (PageSwapCache(page
))
980 * Check if our page_cgroup is valid
982 pc
= lookup_page_cgroup(page
);
983 if (unlikely(!pc
|| !PageCgroupUsed(pc
)))
986 lock_page_cgroup(pc
);
988 mem
= pc
->mem_cgroup
;
990 if (!PageCgroupUsed(pc
))
994 case MEM_CGROUP_CHARGE_TYPE_MAPPED
:
995 if (page_mapped(page
))
998 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT
:
999 if (!PageAnon(page
)) { /* Shared memory */
1000 if (page
->mapping
&& !page_is_file_cache(page
))
1002 } else if (page_mapped(page
)) /* Anon */
1009 res_counter_uncharge(&mem
->res
, PAGE_SIZE
);
1010 if (do_swap_account
&& (ctype
!= MEM_CGROUP_CHARGE_TYPE_SWAPOUT
))
1011 res_counter_uncharge(&mem
->memsw
, PAGE_SIZE
);
1013 ClearPageCgroupUsed(pc
);
1015 mz
= page_cgroup_zoneinfo(pc
);
1016 spin_lock_irqsave(&mz
->lru_lock
, flags
);
1017 __mem_cgroup_remove_list(mz
, pc
);
1018 spin_unlock_irqrestore(&mz
->lru_lock
, flags
);
1019 unlock_page_cgroup(pc
);
1026 unlock_page_cgroup(pc
);
1030 void mem_cgroup_uncharge_page(struct page
*page
)
1033 if (page_mapped(page
))
1035 if (page
->mapping
&& !PageAnon(page
))
1037 __mem_cgroup_uncharge_common(page
, MEM_CGROUP_CHARGE_TYPE_MAPPED
);
1040 void mem_cgroup_uncharge_cache_page(struct page
*page
)
1042 VM_BUG_ON(page_mapped(page
));
1043 VM_BUG_ON(page
->mapping
);
1044 __mem_cgroup_uncharge_common(page
, MEM_CGROUP_CHARGE_TYPE_CACHE
);
1048 * called from __delete_from_swap_cache() and drop "page" account.
1049 * memcg information is recorded to swap_cgroup of "ent"
1051 void mem_cgroup_uncharge_swapcache(struct page
*page
, swp_entry_t ent
)
1053 struct mem_cgroup
*memcg
;
1055 memcg
= __mem_cgroup_uncharge_common(page
,
1056 MEM_CGROUP_CHARGE_TYPE_SWAPOUT
);
1057 /* record memcg information */
1058 if (do_swap_account
&& memcg
) {
1059 swap_cgroup_record(ent
, memcg
);
1060 mem_cgroup_get(memcg
);
1064 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1066 * called from swap_entry_free(). remove record in swap_cgroup and
1067 * uncharge "memsw" account.
1069 void mem_cgroup_uncharge_swap(swp_entry_t ent
)
1071 struct mem_cgroup
*memcg
;
1073 if (!do_swap_account
)
1076 memcg
= swap_cgroup_record(ent
, NULL
);
1078 res_counter_uncharge(&memcg
->memsw
, PAGE_SIZE
);
1079 mem_cgroup_put(memcg
);
1085 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1088 int mem_cgroup_prepare_migration(struct page
*page
, struct mem_cgroup
**ptr
)
1090 struct page_cgroup
*pc
;
1091 struct mem_cgroup
*mem
= NULL
;
1094 if (mem_cgroup_subsys
.disabled
)
1097 pc
= lookup_page_cgroup(page
);
1098 lock_page_cgroup(pc
);
1099 if (PageCgroupUsed(pc
)) {
1100 mem
= pc
->mem_cgroup
;
1103 unlock_page_cgroup(pc
);
1106 ret
= mem_cgroup_try_charge(NULL
, GFP_HIGHUSER_MOVABLE
, &mem
);
1113 /* remove redundant charge if migration failed*/
1114 void mem_cgroup_end_migration(struct mem_cgroup
*mem
,
1115 struct page
*oldpage
, struct page
*newpage
)
1117 struct page
*target
, *unused
;
1118 struct page_cgroup
*pc
;
1119 enum charge_type ctype
;
1124 /* at migration success, oldpage->mapping is NULL. */
1125 if (oldpage
->mapping
) {
1133 if (PageAnon(target
))
1134 ctype
= MEM_CGROUP_CHARGE_TYPE_MAPPED
;
1135 else if (page_is_file_cache(target
))
1136 ctype
= MEM_CGROUP_CHARGE_TYPE_CACHE
;
1138 ctype
= MEM_CGROUP_CHARGE_TYPE_SHMEM
;
1140 /* unused page is not on radix-tree now. */
1142 __mem_cgroup_uncharge_common(unused
, ctype
);
1144 pc
= lookup_page_cgroup(target
);
1146 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1147 * So, double-counting is effectively avoided.
1149 __mem_cgroup_commit_charge(mem
, pc
, ctype
);
1152 * Both of oldpage and newpage are still under lock_page().
1153 * Then, we don't have to care about race in radix-tree.
1154 * But we have to be careful that this page is unmapped or not.
1156 * There is a case for !page_mapped(). At the start of
1157 * migration, oldpage was mapped. But now, it's zapped.
1158 * But we know *target* page is not freed/reused under us.
1159 * mem_cgroup_uncharge_page() does all necessary checks.
1161 if (ctype
== MEM_CGROUP_CHARGE_TYPE_MAPPED
)
1162 mem_cgroup_uncharge_page(target
);
1166 * A call to try to shrink memory usage under specified resource controller.
1167 * This is typically used for page reclaiming for shmem for reducing side
1168 * effect of page allocation from shmem, which is used by some mem_cgroup.
1170 int mem_cgroup_shrink_usage(struct mm_struct
*mm
, gfp_t gfp_mask
)
1172 struct mem_cgroup
*mem
;
1174 int retry
= MEM_CGROUP_RECLAIM_RETRIES
;
1176 if (mem_cgroup_subsys
.disabled
)
1182 mem
= mem_cgroup_from_task(rcu_dereference(mm
->owner
));
1183 if (unlikely(!mem
)) {
1191 progress
= try_to_free_mem_cgroup_pages(mem
, gfp_mask
, true);
1192 progress
+= res_counter_check_under_limit(&mem
->res
);
1193 } while (!progress
&& --retry
);
1201 static DEFINE_MUTEX(set_limit_mutex
);
1203 static int mem_cgroup_resize_limit(struct mem_cgroup
*memcg
,
1204 unsigned long long val
)
1207 int retry_count
= MEM_CGROUP_RECLAIM_RETRIES
;
1212 while (retry_count
) {
1213 if (signal_pending(current
)) {
1218 * Rather than hide all in some function, I do this in
1219 * open coded manner. You see what this really does.
1220 * We have to guarantee mem->res.limit < mem->memsw.limit.
1222 mutex_lock(&set_limit_mutex
);
1223 memswlimit
= res_counter_read_u64(&memcg
->memsw
, RES_LIMIT
);
1224 if (memswlimit
< val
) {
1226 mutex_unlock(&set_limit_mutex
);
1229 ret
= res_counter_set_limit(&memcg
->res
, val
);
1230 mutex_unlock(&set_limit_mutex
);
1235 progress
= try_to_free_mem_cgroup_pages(memcg
,
1236 GFP_HIGHUSER_MOVABLE
, false);
1237 if (!progress
) retry_count
--;
1242 int mem_cgroup_resize_memsw_limit(struct mem_cgroup
*memcg
,
1243 unsigned long long val
)
1245 int retry_count
= MEM_CGROUP_RECLAIM_RETRIES
;
1246 u64 memlimit
, oldusage
, curusage
;
1249 if (!do_swap_account
)
1252 while (retry_count
) {
1253 if (signal_pending(current
)) {
1258 * Rather than hide all in some function, I do this in
1259 * open coded manner. You see what this really does.
1260 * We have to guarantee mem->res.limit < mem->memsw.limit.
1262 mutex_lock(&set_limit_mutex
);
1263 memlimit
= res_counter_read_u64(&memcg
->res
, RES_LIMIT
);
1264 if (memlimit
> val
) {
1266 mutex_unlock(&set_limit_mutex
);
1269 ret
= res_counter_set_limit(&memcg
->memsw
, val
);
1270 mutex_unlock(&set_limit_mutex
);
1275 oldusage
= res_counter_read_u64(&memcg
->memsw
, RES_USAGE
);
1276 try_to_free_mem_cgroup_pages(memcg
, GFP_HIGHUSER_MOVABLE
, true);
1277 curusage
= res_counter_read_u64(&memcg
->memsw
, RES_USAGE
);
1278 if (curusage
>= oldusage
)
1286 * This routine traverse page_cgroup in given list and drop them all.
1287 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1289 static int mem_cgroup_force_empty_list(struct mem_cgroup
*mem
,
1290 struct mem_cgroup_per_zone
*mz
,
1293 struct page_cgroup
*pc
, *busy
;
1294 unsigned long flags
;
1296 struct list_head
*list
;
1299 list
= &mz
->lists
[lru
];
1301 loop
= MEM_CGROUP_ZSTAT(mz
, lru
);
1302 /* give some margin against EBUSY etc...*/
1307 spin_lock_irqsave(&mz
->lru_lock
, flags
);
1308 if (list_empty(list
)) {
1309 spin_unlock_irqrestore(&mz
->lru_lock
, flags
);
1312 pc
= list_entry(list
->prev
, struct page_cgroup
, lru
);
1314 list_move(&pc
->lru
, list
);
1316 spin_unlock_irqrestore(&mz
->lru_lock
, flags
);
1319 spin_unlock_irqrestore(&mz
->lru_lock
, flags
);
1321 ret
= mem_cgroup_move_parent(pc
, mem
, GFP_HIGHUSER_MOVABLE
);
1325 if (ret
== -EBUSY
|| ret
== -EINVAL
) {
1326 /* found lock contention or "pc" is obsolete. */
1332 if (!ret
&& !list_empty(list
))
1338 * make mem_cgroup's charge to be 0 if there is no task.
1339 * This enables deleting this mem_cgroup.
1341 static int mem_cgroup_force_empty(struct mem_cgroup
*mem
, bool free_all
)
1344 int node
, zid
, shrink
;
1345 int nr_retries
= MEM_CGROUP_RECLAIM_RETRIES
;
1346 struct cgroup
*cgrp
= mem
->css
.cgroup
;
1351 /* should free all ? */
1355 while (mem
->res
.usage
> 0) {
1357 if (cgroup_task_count(cgrp
) || !list_empty(&cgrp
->children
))
1360 if (signal_pending(current
))
1362 /* This is for making all *used* pages to be on LRU. */
1363 lru_add_drain_all();
1365 for_each_node_state(node
, N_POSSIBLE
) {
1366 for (zid
= 0; !ret
&& zid
< MAX_NR_ZONES
; zid
++) {
1367 struct mem_cgroup_per_zone
*mz
;
1369 mz
= mem_cgroup_zoneinfo(mem
, node
, zid
);
1371 ret
= mem_cgroup_force_empty_list(mem
,
1380 /* it seems parent cgroup doesn't have enough mem */
1391 /* returns EBUSY if there is a task or if we come here twice. */
1392 if (cgroup_task_count(cgrp
) || !list_empty(&cgrp
->children
) || shrink
) {
1396 /* we call try-to-free pages for make this cgroup empty */
1397 lru_add_drain_all();
1398 /* try to free all pages in this cgroup */
1400 while (nr_retries
&& mem
->res
.usage
> 0) {
1403 if (signal_pending(current
)) {
1407 progress
= try_to_free_mem_cgroup_pages(mem
,
1408 GFP_HIGHUSER_MOVABLE
, false);
1411 /* maybe some writeback is necessary */
1412 congestion_wait(WRITE
, HZ
/10);
1416 /* try move_account...there may be some *locked* pages. */
1423 int mem_cgroup_force_empty_write(struct cgroup
*cont
, unsigned int event
)
1425 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont
), true);
1429 static u64
mem_cgroup_read(struct cgroup
*cont
, struct cftype
*cft
)
1431 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cont
);
1435 type
= MEMFILE_TYPE(cft
->private);
1436 name
= MEMFILE_ATTR(cft
->private);
1439 val
= res_counter_read_u64(&mem
->res
, name
);
1442 if (do_swap_account
)
1443 val
= res_counter_read_u64(&mem
->memsw
, name
);
1452 * The user of this function is...
1455 static int mem_cgroup_write(struct cgroup
*cont
, struct cftype
*cft
,
1458 struct mem_cgroup
*memcg
= mem_cgroup_from_cont(cont
);
1460 unsigned long long val
;
1463 type
= MEMFILE_TYPE(cft
->private);
1464 name
= MEMFILE_ATTR(cft
->private);
1467 /* This function does all necessary parse...reuse it */
1468 ret
= res_counter_memparse_write_strategy(buffer
, &val
);
1472 ret
= mem_cgroup_resize_limit(memcg
, val
);
1474 ret
= mem_cgroup_resize_memsw_limit(memcg
, val
);
1477 ret
= -EINVAL
; /* should be BUG() ? */
1483 static int mem_cgroup_reset(struct cgroup
*cont
, unsigned int event
)
1485 struct mem_cgroup
*mem
;
1488 mem
= mem_cgroup_from_cont(cont
);
1489 type
= MEMFILE_TYPE(event
);
1490 name
= MEMFILE_ATTR(event
);
1494 res_counter_reset_max(&mem
->res
);
1496 res_counter_reset_max(&mem
->memsw
);
1500 res_counter_reset_failcnt(&mem
->res
);
1502 res_counter_reset_failcnt(&mem
->memsw
);
1508 static const struct mem_cgroup_stat_desc
{
1511 } mem_cgroup_stat_desc
[] = {
1512 [MEM_CGROUP_STAT_CACHE
] = { "cache", PAGE_SIZE
, },
1513 [MEM_CGROUP_STAT_RSS
] = { "rss", PAGE_SIZE
, },
1514 [MEM_CGROUP_STAT_PGPGIN_COUNT
] = {"pgpgin", 1, },
1515 [MEM_CGROUP_STAT_PGPGOUT_COUNT
] = {"pgpgout", 1, },
1518 static int mem_control_stat_show(struct cgroup
*cont
, struct cftype
*cft
,
1519 struct cgroup_map_cb
*cb
)
1521 struct mem_cgroup
*mem_cont
= mem_cgroup_from_cont(cont
);
1522 struct mem_cgroup_stat
*stat
= &mem_cont
->stat
;
1525 for (i
= 0; i
< ARRAY_SIZE(stat
->cpustat
[0].count
); i
++) {
1528 val
= mem_cgroup_read_stat(stat
, i
);
1529 val
*= mem_cgroup_stat_desc
[i
].unit
;
1530 cb
->fill(cb
, mem_cgroup_stat_desc
[i
].msg
, val
);
1532 /* showing # of active pages */
1534 unsigned long active_anon
, inactive_anon
;
1535 unsigned long active_file
, inactive_file
;
1536 unsigned long unevictable
;
1538 inactive_anon
= mem_cgroup_get_all_zonestat(mem_cont
,
1540 active_anon
= mem_cgroup_get_all_zonestat(mem_cont
,
1542 inactive_file
= mem_cgroup_get_all_zonestat(mem_cont
,
1544 active_file
= mem_cgroup_get_all_zonestat(mem_cont
,
1546 unevictable
= mem_cgroup_get_all_zonestat(mem_cont
,
1549 cb
->fill(cb
, "active_anon", (active_anon
) * PAGE_SIZE
);
1550 cb
->fill(cb
, "inactive_anon", (inactive_anon
) * PAGE_SIZE
);
1551 cb
->fill(cb
, "active_file", (active_file
) * PAGE_SIZE
);
1552 cb
->fill(cb
, "inactive_file", (inactive_file
) * PAGE_SIZE
);
1553 cb
->fill(cb
, "unevictable", unevictable
* PAGE_SIZE
);
1560 static struct cftype mem_cgroup_files
[] = {
1562 .name
= "usage_in_bytes",
1563 .private = MEMFILE_PRIVATE(_MEM
, RES_USAGE
),
1564 .read_u64
= mem_cgroup_read
,
1567 .name
= "max_usage_in_bytes",
1568 .private = MEMFILE_PRIVATE(_MEM
, RES_MAX_USAGE
),
1569 .trigger
= mem_cgroup_reset
,
1570 .read_u64
= mem_cgroup_read
,
1573 .name
= "limit_in_bytes",
1574 .private = MEMFILE_PRIVATE(_MEM
, RES_LIMIT
),
1575 .write_string
= mem_cgroup_write
,
1576 .read_u64
= mem_cgroup_read
,
1580 .private = MEMFILE_PRIVATE(_MEM
, RES_FAILCNT
),
1581 .trigger
= mem_cgroup_reset
,
1582 .read_u64
= mem_cgroup_read
,
1586 .read_map
= mem_control_stat_show
,
1589 .name
= "force_empty",
1590 .trigger
= mem_cgroup_force_empty_write
,
1594 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1595 static struct cftype memsw_cgroup_files
[] = {
1597 .name
= "memsw.usage_in_bytes",
1598 .private = MEMFILE_PRIVATE(_MEMSWAP
, RES_USAGE
),
1599 .read_u64
= mem_cgroup_read
,
1602 .name
= "memsw.max_usage_in_bytes",
1603 .private = MEMFILE_PRIVATE(_MEMSWAP
, RES_MAX_USAGE
),
1604 .trigger
= mem_cgroup_reset
,
1605 .read_u64
= mem_cgroup_read
,
1608 .name
= "memsw.limit_in_bytes",
1609 .private = MEMFILE_PRIVATE(_MEMSWAP
, RES_LIMIT
),
1610 .write_string
= mem_cgroup_write
,
1611 .read_u64
= mem_cgroup_read
,
1614 .name
= "memsw.failcnt",
1615 .private = MEMFILE_PRIVATE(_MEMSWAP
, RES_FAILCNT
),
1616 .trigger
= mem_cgroup_reset
,
1617 .read_u64
= mem_cgroup_read
,
1621 static int register_memsw_files(struct cgroup
*cont
, struct cgroup_subsys
*ss
)
1623 if (!do_swap_account
)
1625 return cgroup_add_files(cont
, ss
, memsw_cgroup_files
,
1626 ARRAY_SIZE(memsw_cgroup_files
));
1629 static int register_memsw_files(struct cgroup
*cont
, struct cgroup_subsys
*ss
)
1635 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup
*mem
, int node
)
1637 struct mem_cgroup_per_node
*pn
;
1638 struct mem_cgroup_per_zone
*mz
;
1640 int zone
, tmp
= node
;
1642 * This routine is called against possible nodes.
1643 * But it's BUG to call kmalloc() against offline node.
1645 * TODO: this routine can waste much memory for nodes which will
1646 * never be onlined. It's better to use memory hotplug callback
1649 if (!node_state(node
, N_NORMAL_MEMORY
))
1651 pn
= kmalloc_node(sizeof(*pn
), GFP_KERNEL
, tmp
);
1655 mem
->info
.nodeinfo
[node
] = pn
;
1656 memset(pn
, 0, sizeof(*pn
));
1658 for (zone
= 0; zone
< MAX_NR_ZONES
; zone
++) {
1659 mz
= &pn
->zoneinfo
[zone
];
1660 spin_lock_init(&mz
->lru_lock
);
1662 INIT_LIST_HEAD(&mz
->lists
[l
]);
1667 static void free_mem_cgroup_per_zone_info(struct mem_cgroup
*mem
, int node
)
1669 kfree(mem
->info
.nodeinfo
[node
]);
1672 static int mem_cgroup_size(void)
1674 int cpustat_size
= nr_cpu_ids
* sizeof(struct mem_cgroup_stat_cpu
);
1675 return sizeof(struct mem_cgroup
) + cpustat_size
;
1678 static struct mem_cgroup
*mem_cgroup_alloc(void)
1680 struct mem_cgroup
*mem
;
1681 int size
= mem_cgroup_size();
1683 if (size
< PAGE_SIZE
)
1684 mem
= kmalloc(size
, GFP_KERNEL
);
1686 mem
= vmalloc(size
);
1689 memset(mem
, 0, size
);
1694 * At destroying mem_cgroup, references from swap_cgroup can remain.
1695 * (scanning all at force_empty is too costly...)
1697 * Instead of clearing all references at force_empty, we remember
1698 * the number of reference from swap_cgroup and free mem_cgroup when
1699 * it goes down to 0.
1701 * When mem_cgroup is destroyed, mem->obsolete will be set to 0 and
1702 * entry which points to this memcg will be ignore at swapin.
1704 * Removal of cgroup itself succeeds regardless of refs from swap.
1707 static void mem_cgroup_free(struct mem_cgroup
*mem
)
1709 if (atomic_read(&mem
->refcnt
) > 0)
1711 if (mem_cgroup_size() < PAGE_SIZE
)
1717 static void mem_cgroup_get(struct mem_cgroup
*mem
)
1719 atomic_inc(&mem
->refcnt
);
1722 static void mem_cgroup_put(struct mem_cgroup
*mem
)
1724 if (atomic_dec_and_test(&mem
->refcnt
)) {
1727 mem_cgroup_free(mem
);
1732 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1733 static void __init
enable_swap_cgroup(void)
1735 if (!mem_cgroup_subsys
.disabled
&& really_do_swap_account
)
1736 do_swap_account
= 1;
1739 static void __init
enable_swap_cgroup(void)
1744 static struct cgroup_subsys_state
*
1745 mem_cgroup_create(struct cgroup_subsys
*ss
, struct cgroup
*cont
)
1747 struct mem_cgroup
*mem
;
1750 mem
= mem_cgroup_alloc();
1752 return ERR_PTR(-ENOMEM
);
1754 res_counter_init(&mem
->res
);
1755 res_counter_init(&mem
->memsw
);
1757 for_each_node_state(node
, N_POSSIBLE
)
1758 if (alloc_mem_cgroup_per_zone_info(mem
, node
))
1761 if (cont
->parent
== NULL
)
1762 enable_swap_cgroup();
1766 for_each_node_state(node
, N_POSSIBLE
)
1767 free_mem_cgroup_per_zone_info(mem
, node
);
1768 mem_cgroup_free(mem
);
1769 return ERR_PTR(-ENOMEM
);
1772 static void mem_cgroup_pre_destroy(struct cgroup_subsys
*ss
,
1773 struct cgroup
*cont
)
1775 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cont
);
1777 mem_cgroup_force_empty(mem
, false);
1780 static void mem_cgroup_destroy(struct cgroup_subsys
*ss
,
1781 struct cgroup
*cont
)
1784 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cont
);
1786 for_each_node_state(node
, N_POSSIBLE
)
1787 free_mem_cgroup_per_zone_info(mem
, node
);
1789 mem_cgroup_free(mem_cgroup_from_cont(cont
));
1792 static int mem_cgroup_populate(struct cgroup_subsys
*ss
,
1793 struct cgroup
*cont
)
1797 ret
= cgroup_add_files(cont
, ss
, mem_cgroup_files
,
1798 ARRAY_SIZE(mem_cgroup_files
));
1801 ret
= register_memsw_files(cont
, ss
);
1805 static void mem_cgroup_move_task(struct cgroup_subsys
*ss
,
1806 struct cgroup
*cont
,
1807 struct cgroup
*old_cont
,
1808 struct task_struct
*p
)
1810 struct mm_struct
*mm
;
1811 struct mem_cgroup
*mem
, *old_mem
;
1813 mm
= get_task_mm(p
);
1817 mem
= mem_cgroup_from_cont(cont
);
1818 old_mem
= mem_cgroup_from_cont(old_cont
);
1821 * Only thread group leaders are allowed to migrate, the mm_struct is
1822 * in effect owned by the leader
1824 if (!thread_group_leader(p
))
1831 struct cgroup_subsys mem_cgroup_subsys
= {
1833 .subsys_id
= mem_cgroup_subsys_id
,
1834 .create
= mem_cgroup_create
,
1835 .pre_destroy
= mem_cgroup_pre_destroy
,
1836 .destroy
= mem_cgroup_destroy
,
1837 .populate
= mem_cgroup_populate
,
1838 .attach
= mem_cgroup_move_task
,
1842 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1844 static int __init
disable_swap_account(char *s
)
1846 really_do_swap_account
= 0;
1849 __setup("noswapaccount", disable_swap_account
);