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/slab.h>
31 #include <linux/swap.h>
32 #include <linux/spinlock.h>
34 #include <linux/seq_file.h>
35 #include <linux/vmalloc.h>
36 #include <linux/mm_inline.h>
37 #include <linux/page_cgroup.h>
39 #include <asm/uaccess.h>
41 struct cgroup_subsys mem_cgroup_subsys __read_mostly
;
42 #define MEM_CGROUP_RECLAIM_RETRIES 5
44 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
45 /* Turned on only when memory cgroup is enabled && really_do_swap_account = 0 */
46 int do_swap_account __read_mostly
;
47 static int really_do_swap_account __initdata
= 1; /* for remember boot option*/
49 #define do_swap_account (0)
54 * Statistics for memory cgroup.
56 enum mem_cgroup_stat_index
{
58 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
60 MEM_CGROUP_STAT_CACHE
, /* # of pages charged as cache */
61 MEM_CGROUP_STAT_RSS
, /* # of pages charged as rss */
62 MEM_CGROUP_STAT_PGPGIN_COUNT
, /* # of pages paged in */
63 MEM_CGROUP_STAT_PGPGOUT_COUNT
, /* # of pages paged out */
65 MEM_CGROUP_STAT_NSTATS
,
68 struct mem_cgroup_stat_cpu
{
69 s64 count
[MEM_CGROUP_STAT_NSTATS
];
70 } ____cacheline_aligned_in_smp
;
72 struct mem_cgroup_stat
{
73 struct mem_cgroup_stat_cpu cpustat
[0];
77 * For accounting under irq disable, no need for increment preempt count.
79 static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu
*stat
,
80 enum mem_cgroup_stat_index idx
, int val
)
82 stat
->count
[idx
] += val
;
85 static s64
mem_cgroup_read_stat(struct mem_cgroup_stat
*stat
,
86 enum mem_cgroup_stat_index idx
)
90 for_each_possible_cpu(cpu
)
91 ret
+= stat
->cpustat
[cpu
].count
[idx
];
96 * per-zone information in memory controller.
98 struct mem_cgroup_per_zone
{
100 * spin_lock to protect the per cgroup LRU
103 struct list_head lists
[NR_LRU_LISTS
];
104 unsigned long count
[NR_LRU_LISTS
];
106 /* Macro for accessing counter */
107 #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
109 struct mem_cgroup_per_node
{
110 struct mem_cgroup_per_zone zoneinfo
[MAX_NR_ZONES
];
113 struct mem_cgroup_lru_info
{
114 struct mem_cgroup_per_node
*nodeinfo
[MAX_NUMNODES
];
118 * The memory controller data structure. The memory controller controls both
119 * page cache and RSS per cgroup. We would eventually like to provide
120 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
121 * to help the administrator determine what knobs to tune.
123 * TODO: Add a water mark for the memory controller. Reclaim will begin when
124 * we hit the water mark. May be even add a low water mark, such that
125 * no reclaim occurs from a cgroup at it's low water mark, this is
126 * a feature that will be implemented much later in the future.
129 struct cgroup_subsys_state css
;
131 * the counter to account for memory usage
133 struct res_counter res
;
135 * Per cgroup active and inactive list, similar to the
136 * per zone LRU lists.
138 struct mem_cgroup_lru_info info
;
140 int prev_priority
; /* for recording reclaim priority */
142 * statistics. This must be placed at the end of memcg.
144 struct mem_cgroup_stat stat
;
148 MEM_CGROUP_CHARGE_TYPE_CACHE
= 0,
149 MEM_CGROUP_CHARGE_TYPE_MAPPED
,
150 MEM_CGROUP_CHARGE_TYPE_SHMEM
, /* used by page migration of shmem */
151 MEM_CGROUP_CHARGE_TYPE_FORCE
, /* used by force_empty */
152 MEM_CGROUP_CHARGE_TYPE_SWAPOUT
, /* for accounting swapcache */
156 /* only for here (for easy reading.) */
157 #define PCGF_CACHE (1UL << PCG_CACHE)
158 #define PCGF_USED (1UL << PCG_USED)
159 #define PCGF_ACTIVE (1UL << PCG_ACTIVE)
160 #define PCGF_LOCK (1UL << PCG_LOCK)
161 #define PCGF_FILE (1UL << PCG_FILE)
162 static const unsigned long
163 pcg_default_flags
[NR_CHARGE_TYPE
] = {
164 PCGF_CACHE
| PCGF_FILE
| PCGF_USED
| PCGF_LOCK
, /* File Cache */
165 PCGF_ACTIVE
| PCGF_USED
| PCGF_LOCK
, /* Anon */
166 PCGF_ACTIVE
| PCGF_CACHE
| PCGF_USED
| PCGF_LOCK
, /* Shmem */
171 * Always modified under lru lock. Then, not necessary to preempt_disable()
173 static void mem_cgroup_charge_statistics(struct mem_cgroup
*mem
,
174 struct page_cgroup
*pc
,
177 int val
= (charge
)? 1 : -1;
178 struct mem_cgroup_stat
*stat
= &mem
->stat
;
179 struct mem_cgroup_stat_cpu
*cpustat
;
181 VM_BUG_ON(!irqs_disabled());
183 cpustat
= &stat
->cpustat
[smp_processor_id()];
184 if (PageCgroupCache(pc
))
185 __mem_cgroup_stat_add_safe(cpustat
, MEM_CGROUP_STAT_CACHE
, val
);
187 __mem_cgroup_stat_add_safe(cpustat
, MEM_CGROUP_STAT_RSS
, val
);
190 __mem_cgroup_stat_add_safe(cpustat
,
191 MEM_CGROUP_STAT_PGPGIN_COUNT
, 1);
193 __mem_cgroup_stat_add_safe(cpustat
,
194 MEM_CGROUP_STAT_PGPGOUT_COUNT
, 1);
197 static struct mem_cgroup_per_zone
*
198 mem_cgroup_zoneinfo(struct mem_cgroup
*mem
, int nid
, int zid
)
200 return &mem
->info
.nodeinfo
[nid
]->zoneinfo
[zid
];
203 static struct mem_cgroup_per_zone
*
204 page_cgroup_zoneinfo(struct page_cgroup
*pc
)
206 struct mem_cgroup
*mem
= pc
->mem_cgroup
;
207 int nid
= page_cgroup_nid(pc
);
208 int zid
= page_cgroup_zid(pc
);
210 return mem_cgroup_zoneinfo(mem
, nid
, zid
);
213 static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup
*mem
,
217 struct mem_cgroup_per_zone
*mz
;
220 for_each_online_node(nid
)
221 for (zid
= 0; zid
< MAX_NR_ZONES
; zid
++) {
222 mz
= mem_cgroup_zoneinfo(mem
, nid
, zid
);
223 total
+= MEM_CGROUP_ZSTAT(mz
, idx
);
228 static struct mem_cgroup
*mem_cgroup_from_cont(struct cgroup
*cont
)
230 return container_of(cgroup_subsys_state(cont
,
231 mem_cgroup_subsys_id
), struct mem_cgroup
,
235 struct mem_cgroup
*mem_cgroup_from_task(struct task_struct
*p
)
238 * mm_update_next_owner() may clear mm->owner to NULL
239 * if it races with swapoff, page migration, etc.
240 * So this can be called with p == NULL.
245 return container_of(task_subsys_state(p
, mem_cgroup_subsys_id
),
246 struct mem_cgroup
, css
);
249 static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone
*mz
,
250 struct page_cgroup
*pc
)
254 if (PageCgroupUnevictable(pc
))
255 lru
= LRU_UNEVICTABLE
;
257 if (PageCgroupActive(pc
))
259 if (PageCgroupFile(pc
))
263 MEM_CGROUP_ZSTAT(mz
, lru
) -= 1;
265 mem_cgroup_charge_statistics(pc
->mem_cgroup
, pc
, false);
269 static void __mem_cgroup_add_list(struct mem_cgroup_per_zone
*mz
,
270 struct page_cgroup
*pc
, bool hot
)
274 if (PageCgroupUnevictable(pc
))
275 lru
= LRU_UNEVICTABLE
;
277 if (PageCgroupActive(pc
))
279 if (PageCgroupFile(pc
))
283 MEM_CGROUP_ZSTAT(mz
, lru
) += 1;
285 list_add(&pc
->lru
, &mz
->lists
[lru
]);
287 list_add_tail(&pc
->lru
, &mz
->lists
[lru
]);
289 mem_cgroup_charge_statistics(pc
->mem_cgroup
, pc
, true);
292 static void __mem_cgroup_move_lists(struct page_cgroup
*pc
, enum lru_list lru
)
294 struct mem_cgroup_per_zone
*mz
= page_cgroup_zoneinfo(pc
);
295 int active
= PageCgroupActive(pc
);
296 int file
= PageCgroupFile(pc
);
297 int unevictable
= PageCgroupUnevictable(pc
);
298 enum lru_list from
= unevictable
? LRU_UNEVICTABLE
:
299 (LRU_FILE
* !!file
+ !!active
);
304 MEM_CGROUP_ZSTAT(mz
, from
) -= 1;
306 * However this is done under mz->lru_lock, another flags, which
307 * are not related to LRU, will be modified from out-of-lock.
308 * We have to use atomic set/clear flags.
310 if (is_unevictable_lru(lru
)) {
311 ClearPageCgroupActive(pc
);
312 SetPageCgroupUnevictable(pc
);
314 if (is_active_lru(lru
))
315 SetPageCgroupActive(pc
);
317 ClearPageCgroupActive(pc
);
318 ClearPageCgroupUnevictable(pc
);
321 MEM_CGROUP_ZSTAT(mz
, lru
) += 1;
322 list_move(&pc
->lru
, &mz
->lists
[lru
]);
325 int task_in_mem_cgroup(struct task_struct
*task
, const struct mem_cgroup
*mem
)
330 ret
= task
->mm
&& mm_match_cgroup(task
->mm
, mem
);
336 * This routine assumes that the appropriate zone's lru lock is already held
338 void mem_cgroup_move_lists(struct page
*page
, enum lru_list lru
)
340 struct page_cgroup
*pc
;
341 struct mem_cgroup_per_zone
*mz
;
344 if (mem_cgroup_subsys
.disabled
)
348 * We cannot lock_page_cgroup while holding zone's lru_lock,
349 * because other holders of lock_page_cgroup can be interrupted
350 * with an attempt to rotate_reclaimable_page. But we cannot
351 * safely get to page_cgroup without it, so just try_lock it:
352 * mem_cgroup_isolate_pages allows for page left on wrong list.
354 pc
= lookup_page_cgroup(page
);
355 if (!trylock_page_cgroup(pc
))
357 if (pc
&& PageCgroupUsed(pc
)) {
358 mz
= page_cgroup_zoneinfo(pc
);
359 spin_lock_irqsave(&mz
->lru_lock
, flags
);
360 __mem_cgroup_move_lists(pc
, lru
);
361 spin_unlock_irqrestore(&mz
->lru_lock
, flags
);
363 unlock_page_cgroup(pc
);
367 * Calculate mapped_ratio under memory controller. This will be used in
368 * vmscan.c for deteremining we have to reclaim mapped pages.
370 int mem_cgroup_calc_mapped_ratio(struct mem_cgroup
*mem
)
375 * usage is recorded in bytes. But, here, we assume the number of
376 * physical pages can be represented by "long" on any arch.
378 total
= (long) (mem
->res
.usage
>> PAGE_SHIFT
) + 1L;
379 rss
= (long)mem_cgroup_read_stat(&mem
->stat
, MEM_CGROUP_STAT_RSS
);
380 return (int)((rss
* 100L) / total
);
384 * prev_priority control...this will be used in memory reclaim path.
386 int mem_cgroup_get_reclaim_priority(struct mem_cgroup
*mem
)
388 return mem
->prev_priority
;
391 void mem_cgroup_note_reclaim_priority(struct mem_cgroup
*mem
, int priority
)
393 if (priority
< mem
->prev_priority
)
394 mem
->prev_priority
= priority
;
397 void mem_cgroup_record_reclaim_priority(struct mem_cgroup
*mem
, int priority
)
399 mem
->prev_priority
= priority
;
403 * Calculate # of pages to be scanned in this priority/zone.
406 * priority starts from "DEF_PRIORITY" and decremented in each loop.
407 * (see include/linux/mmzone.h)
410 long mem_cgroup_calc_reclaim(struct mem_cgroup
*mem
, struct zone
*zone
,
411 int priority
, enum lru_list lru
)
414 int nid
= zone
->zone_pgdat
->node_id
;
415 int zid
= zone_idx(zone
);
416 struct mem_cgroup_per_zone
*mz
= mem_cgroup_zoneinfo(mem
, nid
, zid
);
418 nr_pages
= MEM_CGROUP_ZSTAT(mz
, lru
);
420 return (nr_pages
>> priority
);
423 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan
,
424 struct list_head
*dst
,
425 unsigned long *scanned
, int order
,
426 int mode
, struct zone
*z
,
427 struct mem_cgroup
*mem_cont
,
428 int active
, int file
)
430 unsigned long nr_taken
= 0;
434 struct list_head
*src
;
435 struct page_cgroup
*pc
, *tmp
;
436 int nid
= z
->zone_pgdat
->node_id
;
437 int zid
= zone_idx(z
);
438 struct mem_cgroup_per_zone
*mz
;
439 int lru
= LRU_FILE
* !!file
+ !!active
;
442 mz
= mem_cgroup_zoneinfo(mem_cont
, nid
, zid
);
443 src
= &mz
->lists
[lru
];
445 spin_lock(&mz
->lru_lock
);
447 list_for_each_entry_safe_reverse(pc
, tmp
, src
, lru
) {
448 if (scan
>= nr_to_scan
)
450 if (unlikely(!PageCgroupUsed(pc
)))
454 if (unlikely(!PageLRU(page
)))
458 * TODO: play better with lumpy reclaim, grabbing anything.
460 if (PageUnevictable(page
) ||
461 (PageActive(page
) && !active
) ||
462 (!PageActive(page
) && active
)) {
463 __mem_cgroup_move_lists(pc
, page_lru(page
));
468 list_move(&pc
->lru
, &pc_list
);
470 if (__isolate_lru_page(page
, mode
, file
) == 0) {
471 list_move(&page
->lru
, dst
);
476 list_splice(&pc_list
, src
);
477 spin_unlock(&mz
->lru_lock
);
484 * Unlike exported interface, "oom" parameter is added. if oom==true,
485 * oom-killer can be invoked.
487 static int __mem_cgroup_try_charge(struct mm_struct
*mm
,
488 gfp_t gfp_mask
, struct mem_cgroup
**memcg
, bool oom
)
490 struct mem_cgroup
*mem
;
491 int nr_retries
= MEM_CGROUP_RECLAIM_RETRIES
;
493 * We always charge the cgroup the mm_struct belongs to.
494 * The mm_struct's mem_cgroup changes on task migration if the
495 * thread group leader migrates. It's possible that mm is not
496 * set, if so charge the init_mm (happens for pagecache usage).
498 if (likely(!*memcg
)) {
500 mem
= mem_cgroup_from_task(rcu_dereference(mm
->owner
));
501 if (unlikely(!mem
)) {
506 * For every charge from the cgroup, increment reference count
517 while (unlikely(res_counter_charge(&mem
->res
, PAGE_SIZE
))) {
518 if (!(gfp_mask
& __GFP_WAIT
))
521 if (try_to_free_mem_cgroup_pages(mem
, gfp_mask
))
525 * try_to_free_mem_cgroup_pages() might not give us a full
526 * picture of reclaim. Some pages are reclaimed and might be
527 * moved to swap cache or just unmapped from the cgroup.
528 * Check the limit again to see if the reclaim reduced the
529 * current usage of the cgroup before giving up
531 if (res_counter_check_under_limit(&mem
->res
))
536 mem_cgroup_out_of_memory(mem
, gfp_mask
);
547 * mem_cgroup_try_charge - get charge of PAGE_SIZE.
548 * @mm: an mm_struct which is charged against. (when *memcg is NULL)
549 * @gfp_mask: gfp_mask for reclaim.
550 * @memcg: a pointer to memory cgroup which is charged against.
552 * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated
553 * memory cgroup from @mm is got and stored in *memcg.
555 * Returns 0 if success. -ENOMEM at failure.
556 * This call can invoke OOM-Killer.
559 int mem_cgroup_try_charge(struct mm_struct
*mm
,
560 gfp_t mask
, struct mem_cgroup
**memcg
)
562 return __mem_cgroup_try_charge(mm
, mask
, memcg
, true);
566 * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be
567 * USED state. If already USED, uncharge and return.
570 static void __mem_cgroup_commit_charge(struct mem_cgroup
*mem
,
571 struct page_cgroup
*pc
,
572 enum charge_type ctype
)
574 struct mem_cgroup_per_zone
*mz
;
577 /* try_charge() can return NULL to *memcg, taking care of it. */
581 lock_page_cgroup(pc
);
582 if (unlikely(PageCgroupUsed(pc
))) {
583 unlock_page_cgroup(pc
);
584 res_counter_uncharge(&mem
->res
, PAGE_SIZE
);
588 pc
->mem_cgroup
= mem
;
590 * If a page is accounted as a page cache, insert to inactive list.
591 * If anon, insert to active list.
593 pc
->flags
= pcg_default_flags
[ctype
];
595 mz
= page_cgroup_zoneinfo(pc
);
597 spin_lock_irqsave(&mz
->lru_lock
, flags
);
598 __mem_cgroup_add_list(mz
, pc
, true);
599 spin_unlock_irqrestore(&mz
->lru_lock
, flags
);
600 unlock_page_cgroup(pc
);
604 * mem_cgroup_move_account - move account of the page
605 * @pc: page_cgroup of the page.
606 * @from: mem_cgroup which the page is moved from.
607 * @to: mem_cgroup which the page is moved to. @from != @to.
609 * The caller must confirm following.
611 * 2. lru_lock of old mem_cgroup(@from) should be held.
613 * returns 0 at success,
614 * returns -EBUSY when lock is busy or "pc" is unstable.
616 * This function does "uncharge" from old cgroup but doesn't do "charge" to
617 * new cgroup. It should be done by a caller.
620 static int mem_cgroup_move_account(struct page_cgroup
*pc
,
621 struct mem_cgroup
*from
, struct mem_cgroup
*to
)
623 struct mem_cgroup_per_zone
*from_mz
, *to_mz
;
627 VM_BUG_ON(!irqs_disabled());
628 VM_BUG_ON(from
== to
);
630 nid
= page_cgroup_nid(pc
);
631 zid
= page_cgroup_zid(pc
);
632 from_mz
= mem_cgroup_zoneinfo(from
, nid
, zid
);
633 to_mz
= mem_cgroup_zoneinfo(to
, nid
, zid
);
636 if (!trylock_page_cgroup(pc
))
639 if (!PageCgroupUsed(pc
))
642 if (pc
->mem_cgroup
!= from
)
645 if (spin_trylock(&to_mz
->lru_lock
)) {
646 __mem_cgroup_remove_list(from_mz
, pc
);
648 res_counter_uncharge(&from
->res
, PAGE_SIZE
);
651 __mem_cgroup_add_list(to_mz
, pc
, false);
653 spin_unlock(&to_mz
->lru_lock
);
656 unlock_page_cgroup(pc
);
661 * move charges to its parent.
664 static int mem_cgroup_move_parent(struct page_cgroup
*pc
,
665 struct mem_cgroup
*child
,
668 struct cgroup
*cg
= child
->css
.cgroup
;
669 struct cgroup
*pcg
= cg
->parent
;
670 struct mem_cgroup
*parent
;
671 struct mem_cgroup_per_zone
*mz
;
679 parent
= mem_cgroup_from_cont(pcg
);
681 ret
= __mem_cgroup_try_charge(NULL
, gfp_mask
, &parent
, false);
685 mz
= mem_cgroup_zoneinfo(child
,
686 page_cgroup_nid(pc
), page_cgroup_zid(pc
));
688 spin_lock_irqsave(&mz
->lru_lock
, flags
);
689 ret
= mem_cgroup_move_account(pc
, child
, parent
);
690 spin_unlock_irqrestore(&mz
->lru_lock
, flags
);
692 /* drop extra refcnt */
693 css_put(&parent
->css
);
694 /* uncharge if move fails */
696 res_counter_uncharge(&parent
->res
, PAGE_SIZE
);
702 * Charge the memory controller for page usage.
704 * 0 if the charge was successful
705 * < 0 if the cgroup is over its limit
707 static int mem_cgroup_charge_common(struct page
*page
, struct mm_struct
*mm
,
708 gfp_t gfp_mask
, enum charge_type ctype
,
709 struct mem_cgroup
*memcg
)
711 struct mem_cgroup
*mem
;
712 struct page_cgroup
*pc
;
715 pc
= lookup_page_cgroup(page
);
716 /* can happen at boot */
722 ret
= __mem_cgroup_try_charge(mm
, gfp_mask
, &mem
, true);
726 __mem_cgroup_commit_charge(mem
, pc
, ctype
);
730 int mem_cgroup_newpage_charge(struct page
*page
,
731 struct mm_struct
*mm
, gfp_t gfp_mask
)
733 if (mem_cgroup_subsys
.disabled
)
735 if (PageCompound(page
))
738 * If already mapped, we don't have to account.
739 * If page cache, page->mapping has address_space.
740 * But page->mapping may have out-of-use anon_vma pointer,
741 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
744 if (page_mapped(page
) || (page
->mapping
&& !PageAnon(page
)))
748 return mem_cgroup_charge_common(page
, mm
, gfp_mask
,
749 MEM_CGROUP_CHARGE_TYPE_MAPPED
, NULL
);
752 int mem_cgroup_cache_charge(struct page
*page
, struct mm_struct
*mm
,
755 if (mem_cgroup_subsys
.disabled
)
757 if (PageCompound(page
))
760 * Corner case handling. This is called from add_to_page_cache()
761 * in usual. But some FS (shmem) precharges this page before calling it
762 * and call add_to_page_cache() with GFP_NOWAIT.
764 * For GFP_NOWAIT case, the page may be pre-charged before calling
765 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
766 * charge twice. (It works but has to pay a bit larger cost.)
768 if (!(gfp_mask
& __GFP_WAIT
)) {
769 struct page_cgroup
*pc
;
772 pc
= lookup_page_cgroup(page
);
775 lock_page_cgroup(pc
);
776 if (PageCgroupUsed(pc
)) {
777 unlock_page_cgroup(pc
);
780 unlock_page_cgroup(pc
);
786 if (page_is_file_cache(page
))
787 return mem_cgroup_charge_common(page
, mm
, gfp_mask
,
788 MEM_CGROUP_CHARGE_TYPE_CACHE
, NULL
);
790 return mem_cgroup_charge_common(page
, mm
, gfp_mask
,
791 MEM_CGROUP_CHARGE_TYPE_SHMEM
, NULL
);
795 int mem_cgroup_cache_charge_swapin(struct page
*page
,
796 struct mm_struct
*mm
, gfp_t mask
, bool locked
)
800 if (mem_cgroup_subsys
.disabled
)
807 * If not locked, the page can be dropped from SwapCache until
810 if (PageSwapCache(page
)) {
811 ret
= mem_cgroup_charge_common(page
, mm
, mask
,
812 MEM_CGROUP_CHARGE_TYPE_SHMEM
, NULL
);
821 void mem_cgroup_commit_charge_swapin(struct page
*page
, struct mem_cgroup
*ptr
)
823 struct page_cgroup
*pc
;
825 if (mem_cgroup_subsys
.disabled
)
829 pc
= lookup_page_cgroup(page
);
830 __mem_cgroup_commit_charge(ptr
, pc
, MEM_CGROUP_CHARGE_TYPE_MAPPED
);
833 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup
*mem
)
835 if (mem_cgroup_subsys
.disabled
)
839 res_counter_uncharge(&mem
->res
, PAGE_SIZE
);
845 * uncharge if !page_mapped(page)
848 __mem_cgroup_uncharge_common(struct page
*page
, enum charge_type ctype
)
850 struct page_cgroup
*pc
;
851 struct mem_cgroup
*mem
;
852 struct mem_cgroup_per_zone
*mz
;
855 if (mem_cgroup_subsys
.disabled
)
858 if (PageSwapCache(page
))
862 * Check if our page_cgroup is valid
864 pc
= lookup_page_cgroup(page
);
865 if (unlikely(!pc
|| !PageCgroupUsed(pc
)))
868 lock_page_cgroup(pc
);
870 if (!PageCgroupUsed(pc
))
874 case MEM_CGROUP_CHARGE_TYPE_MAPPED
:
875 if (page_mapped(page
))
878 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT
:
879 if (!PageAnon(page
)) { /* Shared memory */
880 if (page
->mapping
&& !page_is_file_cache(page
))
882 } else if (page_mapped(page
)) /* Anon */
889 ClearPageCgroupUsed(pc
);
890 mem
= pc
->mem_cgroup
;
892 mz
= page_cgroup_zoneinfo(pc
);
893 spin_lock_irqsave(&mz
->lru_lock
, flags
);
894 __mem_cgroup_remove_list(mz
, pc
);
895 spin_unlock_irqrestore(&mz
->lru_lock
, flags
);
896 unlock_page_cgroup(pc
);
898 res_counter_uncharge(&mem
->res
, PAGE_SIZE
);
904 unlock_page_cgroup(pc
);
908 void mem_cgroup_uncharge_page(struct page
*page
)
911 if (page_mapped(page
))
913 if (page
->mapping
&& !PageAnon(page
))
915 __mem_cgroup_uncharge_common(page
, MEM_CGROUP_CHARGE_TYPE_MAPPED
);
918 void mem_cgroup_uncharge_cache_page(struct page
*page
)
920 VM_BUG_ON(page_mapped(page
));
921 VM_BUG_ON(page
->mapping
);
922 __mem_cgroup_uncharge_common(page
, MEM_CGROUP_CHARGE_TYPE_CACHE
);
925 void mem_cgroup_uncharge_swapcache(struct page
*page
)
927 __mem_cgroup_uncharge_common(page
, MEM_CGROUP_CHARGE_TYPE_SWAPOUT
);
931 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
934 int mem_cgroup_prepare_migration(struct page
*page
, struct mem_cgroup
**ptr
)
936 struct page_cgroup
*pc
;
937 struct mem_cgroup
*mem
= NULL
;
940 if (mem_cgroup_subsys
.disabled
)
943 pc
= lookup_page_cgroup(page
);
944 lock_page_cgroup(pc
);
945 if (PageCgroupUsed(pc
)) {
946 mem
= pc
->mem_cgroup
;
949 unlock_page_cgroup(pc
);
952 ret
= mem_cgroup_try_charge(NULL
, GFP_HIGHUSER_MOVABLE
, &mem
);
959 /* remove redundant charge if migration failed*/
960 void mem_cgroup_end_migration(struct mem_cgroup
*mem
,
961 struct page
*oldpage
, struct page
*newpage
)
963 struct page
*target
, *unused
;
964 struct page_cgroup
*pc
;
965 enum charge_type ctype
;
970 /* at migration success, oldpage->mapping is NULL. */
971 if (oldpage
->mapping
) {
979 if (PageAnon(target
))
980 ctype
= MEM_CGROUP_CHARGE_TYPE_MAPPED
;
981 else if (page_is_file_cache(target
))
982 ctype
= MEM_CGROUP_CHARGE_TYPE_CACHE
;
984 ctype
= MEM_CGROUP_CHARGE_TYPE_SHMEM
;
986 /* unused page is not on radix-tree now. */
988 __mem_cgroup_uncharge_common(unused
, ctype
);
990 pc
= lookup_page_cgroup(target
);
992 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
993 * So, double-counting is effectively avoided.
995 __mem_cgroup_commit_charge(mem
, pc
, ctype
);
998 * Both of oldpage and newpage are still under lock_page().
999 * Then, we don't have to care about race in radix-tree.
1000 * But we have to be careful that this page is unmapped or not.
1002 * There is a case for !page_mapped(). At the start of
1003 * migration, oldpage was mapped. But now, it's zapped.
1004 * But we know *target* page is not freed/reused under us.
1005 * mem_cgroup_uncharge_page() does all necessary checks.
1007 if (ctype
== MEM_CGROUP_CHARGE_TYPE_MAPPED
)
1008 mem_cgroup_uncharge_page(target
);
1012 * A call to try to shrink memory usage under specified resource controller.
1013 * This is typically used for page reclaiming for shmem for reducing side
1014 * effect of page allocation from shmem, which is used by some mem_cgroup.
1016 int mem_cgroup_shrink_usage(struct mm_struct
*mm
, gfp_t gfp_mask
)
1018 struct mem_cgroup
*mem
;
1020 int retry
= MEM_CGROUP_RECLAIM_RETRIES
;
1022 if (mem_cgroup_subsys
.disabled
)
1028 mem
= mem_cgroup_from_task(rcu_dereference(mm
->owner
));
1029 if (unlikely(!mem
)) {
1037 progress
= try_to_free_mem_cgroup_pages(mem
, gfp_mask
);
1038 progress
+= res_counter_check_under_limit(&mem
->res
);
1039 } while (!progress
&& --retry
);
1047 static int mem_cgroup_resize_limit(struct mem_cgroup
*memcg
,
1048 unsigned long long val
)
1051 int retry_count
= MEM_CGROUP_RECLAIM_RETRIES
;
1055 while (res_counter_set_limit(&memcg
->res
, val
)) {
1056 if (signal_pending(current
)) {
1064 progress
= try_to_free_mem_cgroup_pages(memcg
,
1065 GFP_HIGHUSER_MOVABLE
);
1074 * This routine traverse page_cgroup in given list and drop them all.
1075 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1077 static int mem_cgroup_force_empty_list(struct mem_cgroup
*mem
,
1078 struct mem_cgroup_per_zone
*mz
,
1081 struct page_cgroup
*pc
, *busy
;
1082 unsigned long flags
;
1084 struct list_head
*list
;
1087 list
= &mz
->lists
[lru
];
1089 loop
= MEM_CGROUP_ZSTAT(mz
, lru
);
1090 /* give some margin against EBUSY etc...*/
1095 spin_lock_irqsave(&mz
->lru_lock
, flags
);
1096 if (list_empty(list
)) {
1097 spin_unlock_irqrestore(&mz
->lru_lock
, flags
);
1100 pc
= list_entry(list
->prev
, struct page_cgroup
, lru
);
1102 list_move(&pc
->lru
, list
);
1104 spin_unlock_irqrestore(&mz
->lru_lock
, flags
);
1107 spin_unlock_irqrestore(&mz
->lru_lock
, flags
);
1109 ret
= mem_cgroup_move_parent(pc
, mem
, GFP_HIGHUSER_MOVABLE
);
1113 if (ret
== -EBUSY
|| ret
== -EINVAL
) {
1114 /* found lock contention or "pc" is obsolete. */
1120 if (!ret
&& !list_empty(list
))
1126 * make mem_cgroup's charge to be 0 if there is no task.
1127 * This enables deleting this mem_cgroup.
1129 static int mem_cgroup_force_empty(struct mem_cgroup
*mem
, bool free_all
)
1132 int node
, zid
, shrink
;
1133 int nr_retries
= MEM_CGROUP_RECLAIM_RETRIES
;
1134 struct cgroup
*cgrp
= mem
->css
.cgroup
;
1139 /* should free all ? */
1143 while (mem
->res
.usage
> 0) {
1145 if (cgroup_task_count(cgrp
) || !list_empty(&cgrp
->children
))
1148 if (signal_pending(current
))
1150 /* This is for making all *used* pages to be on LRU. */
1151 lru_add_drain_all();
1153 for_each_node_state(node
, N_POSSIBLE
) {
1154 for (zid
= 0; !ret
&& zid
< MAX_NR_ZONES
; zid
++) {
1155 struct mem_cgroup_per_zone
*mz
;
1157 mz
= mem_cgroup_zoneinfo(mem
, node
, zid
);
1159 ret
= mem_cgroup_force_empty_list(mem
,
1168 /* it seems parent cgroup doesn't have enough mem */
1179 /* returns EBUSY if there is a task or if we come here twice. */
1180 if (cgroup_task_count(cgrp
) || !list_empty(&cgrp
->children
) || shrink
) {
1184 /* we call try-to-free pages for make this cgroup empty */
1185 lru_add_drain_all();
1186 /* try to free all pages in this cgroup */
1188 while (nr_retries
&& mem
->res
.usage
> 0) {
1191 if (signal_pending(current
)) {
1195 progress
= try_to_free_mem_cgroup_pages(mem
,
1196 GFP_HIGHUSER_MOVABLE
);
1199 /* maybe some writeback is necessary */
1200 congestion_wait(WRITE
, HZ
/10);
1204 /* try move_account...there may be some *locked* pages. */
1211 int mem_cgroup_force_empty_write(struct cgroup
*cont
, unsigned int event
)
1213 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont
), true);
1217 static u64
mem_cgroup_read(struct cgroup
*cont
, struct cftype
*cft
)
1219 return res_counter_read_u64(&mem_cgroup_from_cont(cont
)->res
,
1223 * The user of this function is...
1226 static int mem_cgroup_write(struct cgroup
*cont
, struct cftype
*cft
,
1229 struct mem_cgroup
*memcg
= mem_cgroup_from_cont(cont
);
1230 unsigned long long val
;
1233 switch (cft
->private) {
1235 /* This function does all necessary parse...reuse it */
1236 ret
= res_counter_memparse_write_strategy(buffer
, &val
);
1238 ret
= mem_cgroup_resize_limit(memcg
, val
);
1241 ret
= -EINVAL
; /* should be BUG() ? */
1247 static int mem_cgroup_reset(struct cgroup
*cont
, unsigned int event
)
1249 struct mem_cgroup
*mem
;
1251 mem
= mem_cgroup_from_cont(cont
);
1254 res_counter_reset_max(&mem
->res
);
1257 res_counter_reset_failcnt(&mem
->res
);
1263 static const struct mem_cgroup_stat_desc
{
1266 } mem_cgroup_stat_desc
[] = {
1267 [MEM_CGROUP_STAT_CACHE
] = { "cache", PAGE_SIZE
, },
1268 [MEM_CGROUP_STAT_RSS
] = { "rss", PAGE_SIZE
, },
1269 [MEM_CGROUP_STAT_PGPGIN_COUNT
] = {"pgpgin", 1, },
1270 [MEM_CGROUP_STAT_PGPGOUT_COUNT
] = {"pgpgout", 1, },
1273 static int mem_control_stat_show(struct cgroup
*cont
, struct cftype
*cft
,
1274 struct cgroup_map_cb
*cb
)
1276 struct mem_cgroup
*mem_cont
= mem_cgroup_from_cont(cont
);
1277 struct mem_cgroup_stat
*stat
= &mem_cont
->stat
;
1280 for (i
= 0; i
< ARRAY_SIZE(stat
->cpustat
[0].count
); i
++) {
1283 val
= mem_cgroup_read_stat(stat
, i
);
1284 val
*= mem_cgroup_stat_desc
[i
].unit
;
1285 cb
->fill(cb
, mem_cgroup_stat_desc
[i
].msg
, val
);
1287 /* showing # of active pages */
1289 unsigned long active_anon
, inactive_anon
;
1290 unsigned long active_file
, inactive_file
;
1291 unsigned long unevictable
;
1293 inactive_anon
= mem_cgroup_get_all_zonestat(mem_cont
,
1295 active_anon
= mem_cgroup_get_all_zonestat(mem_cont
,
1297 inactive_file
= mem_cgroup_get_all_zonestat(mem_cont
,
1299 active_file
= mem_cgroup_get_all_zonestat(mem_cont
,
1301 unevictable
= mem_cgroup_get_all_zonestat(mem_cont
,
1304 cb
->fill(cb
, "active_anon", (active_anon
) * PAGE_SIZE
);
1305 cb
->fill(cb
, "inactive_anon", (inactive_anon
) * PAGE_SIZE
);
1306 cb
->fill(cb
, "active_file", (active_file
) * PAGE_SIZE
);
1307 cb
->fill(cb
, "inactive_file", (inactive_file
) * PAGE_SIZE
);
1308 cb
->fill(cb
, "unevictable", unevictable
* PAGE_SIZE
);
1315 static struct cftype mem_cgroup_files
[] = {
1317 .name
= "usage_in_bytes",
1318 .private = RES_USAGE
,
1319 .read_u64
= mem_cgroup_read
,
1322 .name
= "max_usage_in_bytes",
1323 .private = RES_MAX_USAGE
,
1324 .trigger
= mem_cgroup_reset
,
1325 .read_u64
= mem_cgroup_read
,
1328 .name
= "limit_in_bytes",
1329 .private = RES_LIMIT
,
1330 .write_string
= mem_cgroup_write
,
1331 .read_u64
= mem_cgroup_read
,
1335 .private = RES_FAILCNT
,
1336 .trigger
= mem_cgroup_reset
,
1337 .read_u64
= mem_cgroup_read
,
1341 .read_map
= mem_control_stat_show
,
1344 .name
= "force_empty",
1345 .trigger
= mem_cgroup_force_empty_write
,
1349 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup
*mem
, int node
)
1351 struct mem_cgroup_per_node
*pn
;
1352 struct mem_cgroup_per_zone
*mz
;
1354 int zone
, tmp
= node
;
1356 * This routine is called against possible nodes.
1357 * But it's BUG to call kmalloc() against offline node.
1359 * TODO: this routine can waste much memory for nodes which will
1360 * never be onlined. It's better to use memory hotplug callback
1363 if (!node_state(node
, N_NORMAL_MEMORY
))
1365 pn
= kmalloc_node(sizeof(*pn
), GFP_KERNEL
, tmp
);
1369 mem
->info
.nodeinfo
[node
] = pn
;
1370 memset(pn
, 0, sizeof(*pn
));
1372 for (zone
= 0; zone
< MAX_NR_ZONES
; zone
++) {
1373 mz
= &pn
->zoneinfo
[zone
];
1374 spin_lock_init(&mz
->lru_lock
);
1376 INIT_LIST_HEAD(&mz
->lists
[l
]);
1381 static void free_mem_cgroup_per_zone_info(struct mem_cgroup
*mem
, int node
)
1383 kfree(mem
->info
.nodeinfo
[node
]);
1386 static int mem_cgroup_size(void)
1388 int cpustat_size
= nr_cpu_ids
* sizeof(struct mem_cgroup_stat_cpu
);
1389 return sizeof(struct mem_cgroup
) + cpustat_size
;
1392 static struct mem_cgroup
*mem_cgroup_alloc(void)
1394 struct mem_cgroup
*mem
;
1395 int size
= mem_cgroup_size();
1397 if (size
< PAGE_SIZE
)
1398 mem
= kmalloc(size
, GFP_KERNEL
);
1400 mem
= vmalloc(size
);
1403 memset(mem
, 0, size
);
1407 static void mem_cgroup_free(struct mem_cgroup
*mem
)
1409 if (mem_cgroup_size() < PAGE_SIZE
)
1416 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1417 static void __init
enable_swap_cgroup(void)
1419 if (!mem_cgroup_subsys
.disabled
&& really_do_swap_account
)
1420 do_swap_account
= 1;
1423 static void __init
enable_swap_cgroup(void)
1428 static struct cgroup_subsys_state
*
1429 mem_cgroup_create(struct cgroup_subsys
*ss
, struct cgroup
*cont
)
1431 struct mem_cgroup
*mem
;
1434 mem
= mem_cgroup_alloc();
1436 return ERR_PTR(-ENOMEM
);
1438 res_counter_init(&mem
->res
);
1440 for_each_node_state(node
, N_POSSIBLE
)
1441 if (alloc_mem_cgroup_per_zone_info(mem
, node
))
1444 if (cont
->parent
== NULL
)
1445 enable_swap_cgroup();
1449 for_each_node_state(node
, N_POSSIBLE
)
1450 free_mem_cgroup_per_zone_info(mem
, node
);
1451 mem_cgroup_free(mem
);
1452 return ERR_PTR(-ENOMEM
);
1455 static void mem_cgroup_pre_destroy(struct cgroup_subsys
*ss
,
1456 struct cgroup
*cont
)
1458 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cont
);
1459 mem_cgroup_force_empty(mem
, false);
1462 static void mem_cgroup_destroy(struct cgroup_subsys
*ss
,
1463 struct cgroup
*cont
)
1466 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cont
);
1468 for_each_node_state(node
, N_POSSIBLE
)
1469 free_mem_cgroup_per_zone_info(mem
, node
);
1471 mem_cgroup_free(mem_cgroup_from_cont(cont
));
1474 static int mem_cgroup_populate(struct cgroup_subsys
*ss
,
1475 struct cgroup
*cont
)
1477 return cgroup_add_files(cont
, ss
, mem_cgroup_files
,
1478 ARRAY_SIZE(mem_cgroup_files
));
1481 static void mem_cgroup_move_task(struct cgroup_subsys
*ss
,
1482 struct cgroup
*cont
,
1483 struct cgroup
*old_cont
,
1484 struct task_struct
*p
)
1486 struct mm_struct
*mm
;
1487 struct mem_cgroup
*mem
, *old_mem
;
1489 mm
= get_task_mm(p
);
1493 mem
= mem_cgroup_from_cont(cont
);
1494 old_mem
= mem_cgroup_from_cont(old_cont
);
1497 * Only thread group leaders are allowed to migrate, the mm_struct is
1498 * in effect owned by the leader
1500 if (!thread_group_leader(p
))
1507 struct cgroup_subsys mem_cgroup_subsys
= {
1509 .subsys_id
= mem_cgroup_subsys_id
,
1510 .create
= mem_cgroup_create
,
1511 .pre_destroy
= mem_cgroup_pre_destroy
,
1512 .destroy
= mem_cgroup_destroy
,
1513 .populate
= mem_cgroup_populate
,
1514 .attach
= mem_cgroup_move_task
,
1518 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1520 static int __init
disable_swap_account(char *s
)
1522 really_do_swap_account
= 0;
1525 __setup("noswapaccount", disable_swap_account
);