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>
10 * Copyright (C) 2009 Nokia Corporation
11 * Author: Kirill A. Shutemov
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
24 #include <linux/res_counter.h>
25 #include <linux/memcontrol.h>
26 #include <linux/cgroup.h>
28 #include <linux/hugetlb.h>
29 #include <linux/pagemap.h>
30 #include <linux/smp.h>
31 #include <linux/page-flags.h>
32 #include <linux/backing-dev.h>
33 #include <linux/bit_spinlock.h>
34 #include <linux/rcupdate.h>
35 #include <linux/limits.h>
36 #include <linux/mutex.h>
37 #include <linux/rbtree.h>
38 #include <linux/shmem_fs.h>
39 #include <linux/slab.h>
40 #include <linux/swap.h>
41 #include <linux/swapops.h>
42 #include <linux/spinlock.h>
43 #include <linux/eventfd.h>
44 #include <linux/sort.h>
46 #include <linux/seq_file.h>
47 #include <linux/vmalloc.h>
48 #include <linux/mm_inline.h>
49 #include <linux/page_cgroup.h>
50 #include <linux/cpu.h>
51 #include <linux/oom.h>
54 #include <asm/uaccess.h>
56 #include <trace/events/vmscan.h>
58 struct cgroup_subsys mem_cgroup_subsys __read_mostly
;
59 #define MEM_CGROUP_RECLAIM_RETRIES 5
60 struct mem_cgroup
*root_mem_cgroup __read_mostly
;
62 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
63 /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
64 int do_swap_account __read_mostly
;
66 /* for remember boot option*/
67 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP_ENABLED
68 static int really_do_swap_account __initdata
= 1;
70 static int really_do_swap_account __initdata
= 0;
74 #define do_swap_account (0)
79 * Statistics for memory cgroup.
81 enum mem_cgroup_stat_index
{
83 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
85 MEM_CGROUP_STAT_CACHE
, /* # of pages charged as cache */
86 MEM_CGROUP_STAT_RSS
, /* # of pages charged as anon rss */
87 MEM_CGROUP_STAT_FILE_MAPPED
, /* # of pages charged as file rss */
88 MEM_CGROUP_STAT_SWAPOUT
, /* # of pages, swapped out */
89 MEM_CGROUP_STAT_DATA
, /* end of data requires synchronization */
90 MEM_CGROUP_ON_MOVE
, /* someone is moving account between groups */
91 MEM_CGROUP_STAT_NSTATS
,
94 enum mem_cgroup_events_index
{
95 MEM_CGROUP_EVENTS_PGPGIN
, /* # of pages paged in */
96 MEM_CGROUP_EVENTS_PGPGOUT
, /* # of pages paged out */
97 MEM_CGROUP_EVENTS_COUNT
, /* # of pages paged in/out */
98 MEM_CGROUP_EVENTS_PGFAULT
, /* # of page-faults */
99 MEM_CGROUP_EVENTS_PGMAJFAULT
, /* # of major page-faults */
100 MEM_CGROUP_EVENTS_NSTATS
,
103 * Per memcg event counter is incremented at every pagein/pageout. With THP,
104 * it will be incremated by the number of pages. This counter is used for
105 * for trigger some periodic events. This is straightforward and better
106 * than using jiffies etc. to handle periodic memcg event.
108 enum mem_cgroup_events_target
{
109 MEM_CGROUP_TARGET_THRESH
,
110 MEM_CGROUP_TARGET_SOFTLIMIT
,
113 #define THRESHOLDS_EVENTS_TARGET (128)
114 #define SOFTLIMIT_EVENTS_TARGET (1024)
116 struct mem_cgroup_stat_cpu
{
117 long count
[MEM_CGROUP_STAT_NSTATS
];
118 unsigned long events
[MEM_CGROUP_EVENTS_NSTATS
];
119 unsigned long targets
[MEM_CGROUP_NTARGETS
];
123 * per-zone information in memory controller.
125 struct mem_cgroup_per_zone
{
127 * spin_lock to protect the per cgroup LRU
129 struct list_head lists
[NR_LRU_LISTS
];
130 unsigned long count
[NR_LRU_LISTS
];
132 struct zone_reclaim_stat reclaim_stat
;
133 struct rb_node tree_node
; /* RB tree node */
134 unsigned long long usage_in_excess
;/* Set to the value by which */
135 /* the soft limit is exceeded*/
137 struct mem_cgroup
*mem
; /* Back pointer, we cannot */
138 /* use container_of */
140 /* Macro for accessing counter */
141 #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
143 struct mem_cgroup_per_node
{
144 struct mem_cgroup_per_zone zoneinfo
[MAX_NR_ZONES
];
147 struct mem_cgroup_lru_info
{
148 struct mem_cgroup_per_node
*nodeinfo
[MAX_NUMNODES
];
152 * Cgroups above their limits are maintained in a RB-Tree, independent of
153 * their hierarchy representation
156 struct mem_cgroup_tree_per_zone
{
157 struct rb_root rb_root
;
161 struct mem_cgroup_tree_per_node
{
162 struct mem_cgroup_tree_per_zone rb_tree_per_zone
[MAX_NR_ZONES
];
165 struct mem_cgroup_tree
{
166 struct mem_cgroup_tree_per_node
*rb_tree_per_node
[MAX_NUMNODES
];
169 static struct mem_cgroup_tree soft_limit_tree __read_mostly
;
171 struct mem_cgroup_threshold
{
172 struct eventfd_ctx
*eventfd
;
177 struct mem_cgroup_threshold_ary
{
178 /* An array index points to threshold just below usage. */
179 int current_threshold
;
180 /* Size of entries[] */
182 /* Array of thresholds */
183 struct mem_cgroup_threshold entries
[0];
186 struct mem_cgroup_thresholds
{
187 /* Primary thresholds array */
188 struct mem_cgroup_threshold_ary
*primary
;
190 * Spare threshold array.
191 * This is needed to make mem_cgroup_unregister_event() "never fail".
192 * It must be able to store at least primary->size - 1 entries.
194 struct mem_cgroup_threshold_ary
*spare
;
198 struct mem_cgroup_eventfd_list
{
199 struct list_head list
;
200 struct eventfd_ctx
*eventfd
;
203 static void mem_cgroup_threshold(struct mem_cgroup
*mem
);
204 static void mem_cgroup_oom_notify(struct mem_cgroup
*mem
);
207 * The memory controller data structure. The memory controller controls both
208 * page cache and RSS per cgroup. We would eventually like to provide
209 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
210 * to help the administrator determine what knobs to tune.
212 * TODO: Add a water mark for the memory controller. Reclaim will begin when
213 * we hit the water mark. May be even add a low water mark, such that
214 * no reclaim occurs from a cgroup at it's low water mark, this is
215 * a feature that will be implemented much later in the future.
218 struct cgroup_subsys_state css
;
220 * the counter to account for memory usage
222 struct res_counter res
;
224 * the counter to account for mem+swap usage.
226 struct res_counter memsw
;
228 * Per cgroup active and inactive list, similar to the
229 * per zone LRU lists.
231 struct mem_cgroup_lru_info info
;
233 * While reclaiming in a hierarchy, we cache the last child we
236 int last_scanned_child
;
237 int last_scanned_node
;
239 nodemask_t scan_nodes
;
240 unsigned long next_scan_node_update
;
243 * Should the accounting and control be hierarchical, per subtree?
249 unsigned int swappiness
;
250 /* OOM-Killer disable */
251 int oom_kill_disable
;
253 /* set when res.limit == memsw.limit */
254 bool memsw_is_minimum
;
256 /* protect arrays of thresholds */
257 struct mutex thresholds_lock
;
259 /* thresholds for memory usage. RCU-protected */
260 struct mem_cgroup_thresholds thresholds
;
262 /* thresholds for mem+swap usage. RCU-protected */
263 struct mem_cgroup_thresholds memsw_thresholds
;
265 /* For oom notifier event fd */
266 struct list_head oom_notify
;
269 * Should we move charges of a task when a task is moved into this
270 * mem_cgroup ? And what type of charges should we move ?
272 unsigned long move_charge_at_immigrate
;
276 struct mem_cgroup_stat_cpu
*stat
;
278 * used when a cpu is offlined or other synchronizations
279 * See mem_cgroup_read_stat().
281 struct mem_cgroup_stat_cpu nocpu_base
;
282 spinlock_t pcp_counter_lock
;
285 /* Stuffs for move charges at task migration. */
287 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
288 * left-shifted bitmap of these types.
291 MOVE_CHARGE_TYPE_ANON
, /* private anonymous page and swap of it */
292 MOVE_CHARGE_TYPE_FILE
, /* file page(including tmpfs) and swap of it */
296 /* "mc" and its members are protected by cgroup_mutex */
297 static struct move_charge_struct
{
298 spinlock_t lock
; /* for from, to */
299 struct mem_cgroup
*from
;
300 struct mem_cgroup
*to
;
301 unsigned long precharge
;
302 unsigned long moved_charge
;
303 unsigned long moved_swap
;
304 struct task_struct
*moving_task
; /* a task moving charges */
305 wait_queue_head_t waitq
; /* a waitq for other context */
307 .lock
= __SPIN_LOCK_UNLOCKED(mc
.lock
),
308 .waitq
= __WAIT_QUEUE_HEAD_INITIALIZER(mc
.waitq
),
311 static bool move_anon(void)
313 return test_bit(MOVE_CHARGE_TYPE_ANON
,
314 &mc
.to
->move_charge_at_immigrate
);
317 static bool move_file(void)
319 return test_bit(MOVE_CHARGE_TYPE_FILE
,
320 &mc
.to
->move_charge_at_immigrate
);
324 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
325 * limit reclaim to prevent infinite loops, if they ever occur.
327 #define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
328 #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
331 MEM_CGROUP_CHARGE_TYPE_CACHE
= 0,
332 MEM_CGROUP_CHARGE_TYPE_MAPPED
,
333 MEM_CGROUP_CHARGE_TYPE_SHMEM
, /* used by page migration of shmem */
334 MEM_CGROUP_CHARGE_TYPE_FORCE
, /* used by force_empty */
335 MEM_CGROUP_CHARGE_TYPE_SWAPOUT
, /* for accounting swapcache */
336 MEM_CGROUP_CHARGE_TYPE_DROP
, /* a page was unused swap cache */
340 /* for encoding cft->private value on file */
343 #define _OOM_TYPE (2)
344 #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
345 #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
346 #define MEMFILE_ATTR(val) ((val) & 0xffff)
347 /* Used for OOM nofiier */
348 #define OOM_CONTROL (0)
351 * Reclaim flags for mem_cgroup_hierarchical_reclaim
353 #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
354 #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
355 #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
356 #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
357 #define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
358 #define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
360 static void mem_cgroup_get(struct mem_cgroup
*mem
);
361 static void mem_cgroup_put(struct mem_cgroup
*mem
);
362 static struct mem_cgroup
*parent_mem_cgroup(struct mem_cgroup
*mem
);
363 static void drain_all_stock_async(struct mem_cgroup
*mem
);
365 static struct mem_cgroup_per_zone
*
366 mem_cgroup_zoneinfo(struct mem_cgroup
*mem
, int nid
, int zid
)
368 return &mem
->info
.nodeinfo
[nid
]->zoneinfo
[zid
];
371 struct cgroup_subsys_state
*mem_cgroup_css(struct mem_cgroup
*mem
)
376 static struct mem_cgroup_per_zone
*
377 page_cgroup_zoneinfo(struct mem_cgroup
*mem
, struct page
*page
)
379 int nid
= page_to_nid(page
);
380 int zid
= page_zonenum(page
);
382 return mem_cgroup_zoneinfo(mem
, nid
, zid
);
385 static struct mem_cgroup_tree_per_zone
*
386 soft_limit_tree_node_zone(int nid
, int zid
)
388 return &soft_limit_tree
.rb_tree_per_node
[nid
]->rb_tree_per_zone
[zid
];
391 static struct mem_cgroup_tree_per_zone
*
392 soft_limit_tree_from_page(struct page
*page
)
394 int nid
= page_to_nid(page
);
395 int zid
= page_zonenum(page
);
397 return &soft_limit_tree
.rb_tree_per_node
[nid
]->rb_tree_per_zone
[zid
];
401 __mem_cgroup_insert_exceeded(struct mem_cgroup
*mem
,
402 struct mem_cgroup_per_zone
*mz
,
403 struct mem_cgroup_tree_per_zone
*mctz
,
404 unsigned long long new_usage_in_excess
)
406 struct rb_node
**p
= &mctz
->rb_root
.rb_node
;
407 struct rb_node
*parent
= NULL
;
408 struct mem_cgroup_per_zone
*mz_node
;
413 mz
->usage_in_excess
= new_usage_in_excess
;
414 if (!mz
->usage_in_excess
)
418 mz_node
= rb_entry(parent
, struct mem_cgroup_per_zone
,
420 if (mz
->usage_in_excess
< mz_node
->usage_in_excess
)
423 * We can't avoid mem cgroups that are over their soft
424 * limit by the same amount
426 else if (mz
->usage_in_excess
>= mz_node
->usage_in_excess
)
429 rb_link_node(&mz
->tree_node
, parent
, p
);
430 rb_insert_color(&mz
->tree_node
, &mctz
->rb_root
);
435 __mem_cgroup_remove_exceeded(struct mem_cgroup
*mem
,
436 struct mem_cgroup_per_zone
*mz
,
437 struct mem_cgroup_tree_per_zone
*mctz
)
441 rb_erase(&mz
->tree_node
, &mctz
->rb_root
);
446 mem_cgroup_remove_exceeded(struct mem_cgroup
*mem
,
447 struct mem_cgroup_per_zone
*mz
,
448 struct mem_cgroup_tree_per_zone
*mctz
)
450 spin_lock(&mctz
->lock
);
451 __mem_cgroup_remove_exceeded(mem
, mz
, mctz
);
452 spin_unlock(&mctz
->lock
);
456 static void mem_cgroup_update_tree(struct mem_cgroup
*mem
, struct page
*page
)
458 unsigned long long excess
;
459 struct mem_cgroup_per_zone
*mz
;
460 struct mem_cgroup_tree_per_zone
*mctz
;
461 int nid
= page_to_nid(page
);
462 int zid
= page_zonenum(page
);
463 mctz
= soft_limit_tree_from_page(page
);
466 * Necessary to update all ancestors when hierarchy is used.
467 * because their event counter is not touched.
469 for (; mem
; mem
= parent_mem_cgroup(mem
)) {
470 mz
= mem_cgroup_zoneinfo(mem
, nid
, zid
);
471 excess
= res_counter_soft_limit_excess(&mem
->res
);
473 * We have to update the tree if mz is on RB-tree or
474 * mem is over its softlimit.
476 if (excess
|| mz
->on_tree
) {
477 spin_lock(&mctz
->lock
);
478 /* if on-tree, remove it */
480 __mem_cgroup_remove_exceeded(mem
, mz
, mctz
);
482 * Insert again. mz->usage_in_excess will be updated.
483 * If excess is 0, no tree ops.
485 __mem_cgroup_insert_exceeded(mem
, mz
, mctz
, excess
);
486 spin_unlock(&mctz
->lock
);
491 static void mem_cgroup_remove_from_trees(struct mem_cgroup
*mem
)
494 struct mem_cgroup_per_zone
*mz
;
495 struct mem_cgroup_tree_per_zone
*mctz
;
497 for_each_node_state(node
, N_POSSIBLE
) {
498 for (zone
= 0; zone
< MAX_NR_ZONES
; zone
++) {
499 mz
= mem_cgroup_zoneinfo(mem
, node
, zone
);
500 mctz
= soft_limit_tree_node_zone(node
, zone
);
501 mem_cgroup_remove_exceeded(mem
, mz
, mctz
);
506 static struct mem_cgroup_per_zone
*
507 __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone
*mctz
)
509 struct rb_node
*rightmost
= NULL
;
510 struct mem_cgroup_per_zone
*mz
;
514 rightmost
= rb_last(&mctz
->rb_root
);
516 goto done
; /* Nothing to reclaim from */
518 mz
= rb_entry(rightmost
, struct mem_cgroup_per_zone
, tree_node
);
520 * Remove the node now but someone else can add it back,
521 * we will to add it back at the end of reclaim to its correct
522 * position in the tree.
524 __mem_cgroup_remove_exceeded(mz
->mem
, mz
, mctz
);
525 if (!res_counter_soft_limit_excess(&mz
->mem
->res
) ||
526 !css_tryget(&mz
->mem
->css
))
532 static struct mem_cgroup_per_zone
*
533 mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone
*mctz
)
535 struct mem_cgroup_per_zone
*mz
;
537 spin_lock(&mctz
->lock
);
538 mz
= __mem_cgroup_largest_soft_limit_node(mctz
);
539 spin_unlock(&mctz
->lock
);
544 * Implementation Note: reading percpu statistics for memcg.
546 * Both of vmstat[] and percpu_counter has threshold and do periodic
547 * synchronization to implement "quick" read. There are trade-off between
548 * reading cost and precision of value. Then, we may have a chance to implement
549 * a periodic synchronizion of counter in memcg's counter.
551 * But this _read() function is used for user interface now. The user accounts
552 * memory usage by memory cgroup and he _always_ requires exact value because
553 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
554 * have to visit all online cpus and make sum. So, for now, unnecessary
555 * synchronization is not implemented. (just implemented for cpu hotplug)
557 * If there are kernel internal actions which can make use of some not-exact
558 * value, and reading all cpu value can be performance bottleneck in some
559 * common workload, threashold and synchonization as vmstat[] should be
562 static long mem_cgroup_read_stat(struct mem_cgroup
*mem
,
563 enum mem_cgroup_stat_index idx
)
569 for_each_online_cpu(cpu
)
570 val
+= per_cpu(mem
->stat
->count
[idx
], cpu
);
571 #ifdef CONFIG_HOTPLUG_CPU
572 spin_lock(&mem
->pcp_counter_lock
);
573 val
+= mem
->nocpu_base
.count
[idx
];
574 spin_unlock(&mem
->pcp_counter_lock
);
580 static void mem_cgroup_swap_statistics(struct mem_cgroup
*mem
,
583 int val
= (charge
) ? 1 : -1;
584 this_cpu_add(mem
->stat
->count
[MEM_CGROUP_STAT_SWAPOUT
], val
);
587 void mem_cgroup_pgfault(struct mem_cgroup
*mem
, int val
)
589 this_cpu_add(mem
->stat
->events
[MEM_CGROUP_EVENTS_PGFAULT
], val
);
592 void mem_cgroup_pgmajfault(struct mem_cgroup
*mem
, int val
)
594 this_cpu_add(mem
->stat
->events
[MEM_CGROUP_EVENTS_PGMAJFAULT
], val
);
597 static unsigned long mem_cgroup_read_events(struct mem_cgroup
*mem
,
598 enum mem_cgroup_events_index idx
)
600 unsigned long val
= 0;
603 for_each_online_cpu(cpu
)
604 val
+= per_cpu(mem
->stat
->events
[idx
], cpu
);
605 #ifdef CONFIG_HOTPLUG_CPU
606 spin_lock(&mem
->pcp_counter_lock
);
607 val
+= mem
->nocpu_base
.events
[idx
];
608 spin_unlock(&mem
->pcp_counter_lock
);
613 static void mem_cgroup_charge_statistics(struct mem_cgroup
*mem
,
614 bool file
, int nr_pages
)
619 __this_cpu_add(mem
->stat
->count
[MEM_CGROUP_STAT_CACHE
], nr_pages
);
621 __this_cpu_add(mem
->stat
->count
[MEM_CGROUP_STAT_RSS
], nr_pages
);
623 /* pagein of a big page is an event. So, ignore page size */
625 __this_cpu_inc(mem
->stat
->events
[MEM_CGROUP_EVENTS_PGPGIN
]);
627 __this_cpu_inc(mem
->stat
->events
[MEM_CGROUP_EVENTS_PGPGOUT
]);
628 nr_pages
= -nr_pages
; /* for event */
631 __this_cpu_add(mem
->stat
->events
[MEM_CGROUP_EVENTS_COUNT
], nr_pages
);
637 mem_cgroup_get_zonestat_node(struct mem_cgroup
*mem
, int nid
, enum lru_list idx
)
639 struct mem_cgroup_per_zone
*mz
;
643 for (zid
= 0; zid
< MAX_NR_ZONES
; zid
++) {
644 mz
= mem_cgroup_zoneinfo(mem
, nid
, zid
);
645 total
+= MEM_CGROUP_ZSTAT(mz
, idx
);
649 static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup
*mem
,
655 for_each_online_node(nid
)
656 total
+= mem_cgroup_get_zonestat_node(mem
, nid
, idx
);
660 static bool __memcg_event_check(struct mem_cgroup
*mem
, int target
)
662 unsigned long val
, next
;
664 val
= this_cpu_read(mem
->stat
->events
[MEM_CGROUP_EVENTS_COUNT
]);
665 next
= this_cpu_read(mem
->stat
->targets
[target
]);
666 /* from time_after() in jiffies.h */
667 return ((long)next
- (long)val
< 0);
670 static void __mem_cgroup_target_update(struct mem_cgroup
*mem
, int target
)
672 unsigned long val
, next
;
674 val
= this_cpu_read(mem
->stat
->events
[MEM_CGROUP_EVENTS_COUNT
]);
677 case MEM_CGROUP_TARGET_THRESH
:
678 next
= val
+ THRESHOLDS_EVENTS_TARGET
;
680 case MEM_CGROUP_TARGET_SOFTLIMIT
:
681 next
= val
+ SOFTLIMIT_EVENTS_TARGET
;
687 this_cpu_write(mem
->stat
->targets
[target
], next
);
691 * Check events in order.
694 static void memcg_check_events(struct mem_cgroup
*mem
, struct page
*page
)
696 /* threshold event is triggered in finer grain than soft limit */
697 if (unlikely(__memcg_event_check(mem
, MEM_CGROUP_TARGET_THRESH
))) {
698 mem_cgroup_threshold(mem
);
699 __mem_cgroup_target_update(mem
, MEM_CGROUP_TARGET_THRESH
);
700 if (unlikely(__memcg_event_check(mem
,
701 MEM_CGROUP_TARGET_SOFTLIMIT
))){
702 mem_cgroup_update_tree(mem
, page
);
703 __mem_cgroup_target_update(mem
,
704 MEM_CGROUP_TARGET_SOFTLIMIT
);
709 static struct mem_cgroup
*mem_cgroup_from_cont(struct cgroup
*cont
)
711 return container_of(cgroup_subsys_state(cont
,
712 mem_cgroup_subsys_id
), struct mem_cgroup
,
716 struct mem_cgroup
*mem_cgroup_from_task(struct task_struct
*p
)
719 * mm_update_next_owner() may clear mm->owner to NULL
720 * if it races with swapoff, page migration, etc.
721 * So this can be called with p == NULL.
726 return container_of(task_subsys_state(p
, mem_cgroup_subsys_id
),
727 struct mem_cgroup
, css
);
730 struct mem_cgroup
*try_get_mem_cgroup_from_mm(struct mm_struct
*mm
)
732 struct mem_cgroup
*mem
= NULL
;
737 * Because we have no locks, mm->owner's may be being moved to other
738 * cgroup. We use css_tryget() here even if this looks
739 * pessimistic (rather than adding locks here).
743 mem
= mem_cgroup_from_task(rcu_dereference(mm
->owner
));
746 } while (!css_tryget(&mem
->css
));
751 /* The caller has to guarantee "mem" exists before calling this */
752 static struct mem_cgroup
*mem_cgroup_start_loop(struct mem_cgroup
*mem
)
754 struct cgroup_subsys_state
*css
;
757 if (!mem
) /* ROOT cgroup has the smallest ID */
758 return root_mem_cgroup
; /*css_put/get against root is ignored*/
759 if (!mem
->use_hierarchy
) {
760 if (css_tryget(&mem
->css
))
766 * searching a memory cgroup which has the smallest ID under given
767 * ROOT cgroup. (ID >= 1)
769 css
= css_get_next(&mem_cgroup_subsys
, 1, &mem
->css
, &found
);
770 if (css
&& css_tryget(css
))
771 mem
= container_of(css
, struct mem_cgroup
, css
);
778 static struct mem_cgroup
*mem_cgroup_get_next(struct mem_cgroup
*iter
,
779 struct mem_cgroup
*root
,
782 int nextid
= css_id(&iter
->css
) + 1;
785 struct cgroup_subsys_state
*css
;
787 hierarchy_used
= iter
->use_hierarchy
;
790 /* If no ROOT, walk all, ignore hierarchy */
791 if (!cond
|| (root
&& !hierarchy_used
))
795 root
= root_mem_cgroup
;
801 css
= css_get_next(&mem_cgroup_subsys
, nextid
,
803 if (css
&& css_tryget(css
))
804 iter
= container_of(css
, struct mem_cgroup
, css
);
806 /* If css is NULL, no more cgroups will be found */
808 } while (css
&& !iter
);
813 * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please
814 * be careful that "break" loop is not allowed. We have reference count.
815 * Instead of that modify "cond" to be false and "continue" to exit the loop.
817 #define for_each_mem_cgroup_tree_cond(iter, root, cond) \
818 for (iter = mem_cgroup_start_loop(root);\
820 iter = mem_cgroup_get_next(iter, root, cond))
822 #define for_each_mem_cgroup_tree(iter, root) \
823 for_each_mem_cgroup_tree_cond(iter, root, true)
825 #define for_each_mem_cgroup_all(iter) \
826 for_each_mem_cgroup_tree_cond(iter, NULL, true)
829 static inline bool mem_cgroup_is_root(struct mem_cgroup
*mem
)
831 return (mem
== root_mem_cgroup
);
834 void mem_cgroup_count_vm_event(struct mm_struct
*mm
, enum vm_event_item idx
)
836 struct mem_cgroup
*mem
;
842 mem
= mem_cgroup_from_task(rcu_dereference(mm
->owner
));
848 mem_cgroup_pgmajfault(mem
, 1);
851 mem_cgroup_pgfault(mem
, 1);
859 EXPORT_SYMBOL(mem_cgroup_count_vm_event
);
862 * Following LRU functions are allowed to be used without PCG_LOCK.
863 * Operations are called by routine of global LRU independently from memcg.
864 * What we have to take care of here is validness of pc->mem_cgroup.
866 * Changes to pc->mem_cgroup happens when
869 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
870 * It is added to LRU before charge.
871 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
872 * When moving account, the page is not on LRU. It's isolated.
875 void mem_cgroup_del_lru_list(struct page
*page
, enum lru_list lru
)
877 struct page_cgroup
*pc
;
878 struct mem_cgroup_per_zone
*mz
;
880 if (mem_cgroup_disabled())
882 pc
= lookup_page_cgroup(page
);
883 /* can happen while we handle swapcache. */
884 if (!TestClearPageCgroupAcctLRU(pc
))
886 VM_BUG_ON(!pc
->mem_cgroup
);
888 * We don't check PCG_USED bit. It's cleared when the "page" is finally
889 * removed from global LRU.
891 mz
= page_cgroup_zoneinfo(pc
->mem_cgroup
, page
);
892 /* huge page split is done under lru_lock. so, we have no races. */
893 MEM_CGROUP_ZSTAT(mz
, lru
) -= 1 << compound_order(page
);
894 if (mem_cgroup_is_root(pc
->mem_cgroup
))
896 VM_BUG_ON(list_empty(&pc
->lru
));
897 list_del_init(&pc
->lru
);
900 void mem_cgroup_del_lru(struct page
*page
)
902 mem_cgroup_del_lru_list(page
, page_lru(page
));
906 * Writeback is about to end against a page which has been marked for immediate
907 * reclaim. If it still appears to be reclaimable, move it to the tail of the
910 void mem_cgroup_rotate_reclaimable_page(struct page
*page
)
912 struct mem_cgroup_per_zone
*mz
;
913 struct page_cgroup
*pc
;
914 enum lru_list lru
= page_lru(page
);
916 if (mem_cgroup_disabled())
919 pc
= lookup_page_cgroup(page
);
920 /* unused or root page is not rotated. */
921 if (!PageCgroupUsed(pc
))
923 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
925 if (mem_cgroup_is_root(pc
->mem_cgroup
))
927 mz
= page_cgroup_zoneinfo(pc
->mem_cgroup
, page
);
928 list_move_tail(&pc
->lru
, &mz
->lists
[lru
]);
931 void mem_cgroup_rotate_lru_list(struct page
*page
, enum lru_list lru
)
933 struct mem_cgroup_per_zone
*mz
;
934 struct page_cgroup
*pc
;
936 if (mem_cgroup_disabled())
939 pc
= lookup_page_cgroup(page
);
940 /* unused or root page is not rotated. */
941 if (!PageCgroupUsed(pc
))
943 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
945 if (mem_cgroup_is_root(pc
->mem_cgroup
))
947 mz
= page_cgroup_zoneinfo(pc
->mem_cgroup
, page
);
948 list_move(&pc
->lru
, &mz
->lists
[lru
]);
951 void mem_cgroup_add_lru_list(struct page
*page
, enum lru_list lru
)
953 struct page_cgroup
*pc
;
954 struct mem_cgroup_per_zone
*mz
;
956 if (mem_cgroup_disabled())
958 pc
= lookup_page_cgroup(page
);
959 VM_BUG_ON(PageCgroupAcctLRU(pc
));
960 if (!PageCgroupUsed(pc
))
962 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
964 mz
= page_cgroup_zoneinfo(pc
->mem_cgroup
, page
);
965 /* huge page split is done under lru_lock. so, we have no races. */
966 MEM_CGROUP_ZSTAT(mz
, lru
) += 1 << compound_order(page
);
967 SetPageCgroupAcctLRU(pc
);
968 if (mem_cgroup_is_root(pc
->mem_cgroup
))
970 list_add(&pc
->lru
, &mz
->lists
[lru
]);
974 * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed
975 * while it's linked to lru because the page may be reused after it's fully
976 * uncharged. To handle that, unlink page_cgroup from LRU when charge it again.
977 * It's done under lock_page and expected that zone->lru_lock isnever held.
979 static void mem_cgroup_lru_del_before_commit(struct page
*page
)
982 struct zone
*zone
= page_zone(page
);
983 struct page_cgroup
*pc
= lookup_page_cgroup(page
);
986 * Doing this check without taking ->lru_lock seems wrong but this
987 * is safe. Because if page_cgroup's USED bit is unset, the page
988 * will not be added to any memcg's LRU. If page_cgroup's USED bit is
989 * set, the commit after this will fail, anyway.
990 * This all charge/uncharge is done under some mutual execustion.
991 * So, we don't need to taking care of changes in USED bit.
993 if (likely(!PageLRU(page
)))
996 spin_lock_irqsave(&zone
->lru_lock
, flags
);
998 * Forget old LRU when this page_cgroup is *not* used. This Used bit
999 * is guarded by lock_page() because the page is SwapCache.
1001 if (!PageCgroupUsed(pc
))
1002 mem_cgroup_del_lru_list(page
, page_lru(page
));
1003 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
1006 static void mem_cgroup_lru_add_after_commit(struct page
*page
)
1008 unsigned long flags
;
1009 struct zone
*zone
= page_zone(page
);
1010 struct page_cgroup
*pc
= lookup_page_cgroup(page
);
1012 /* taking care of that the page is added to LRU while we commit it */
1013 if (likely(!PageLRU(page
)))
1015 spin_lock_irqsave(&zone
->lru_lock
, flags
);
1016 /* link when the page is linked to LRU but page_cgroup isn't */
1017 if (PageLRU(page
) && !PageCgroupAcctLRU(pc
))
1018 mem_cgroup_add_lru_list(page
, page_lru(page
));
1019 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
1023 void mem_cgroup_move_lists(struct page
*page
,
1024 enum lru_list from
, enum lru_list to
)
1026 if (mem_cgroup_disabled())
1028 mem_cgroup_del_lru_list(page
, from
);
1029 mem_cgroup_add_lru_list(page
, to
);
1032 int task_in_mem_cgroup(struct task_struct
*task
, const struct mem_cgroup
*mem
)
1035 struct mem_cgroup
*curr
= NULL
;
1036 struct task_struct
*p
;
1038 p
= find_lock_task_mm(task
);
1041 curr
= try_get_mem_cgroup_from_mm(p
->mm
);
1046 * We should check use_hierarchy of "mem" not "curr". Because checking
1047 * use_hierarchy of "curr" here make this function true if hierarchy is
1048 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
1049 * hierarchy(even if use_hierarchy is disabled in "mem").
1051 if (mem
->use_hierarchy
)
1052 ret
= css_is_ancestor(&curr
->css
, &mem
->css
);
1054 ret
= (curr
== mem
);
1055 css_put(&curr
->css
);
1059 static int calc_inactive_ratio(struct mem_cgroup
*memcg
, unsigned long *present_pages
)
1061 unsigned long active
;
1062 unsigned long inactive
;
1064 unsigned long inactive_ratio
;
1066 inactive
= mem_cgroup_get_local_zonestat(memcg
, LRU_INACTIVE_ANON
);
1067 active
= mem_cgroup_get_local_zonestat(memcg
, LRU_ACTIVE_ANON
);
1069 gb
= (inactive
+ active
) >> (30 - PAGE_SHIFT
);
1071 inactive_ratio
= int_sqrt(10 * gb
);
1075 if (present_pages
) {
1076 present_pages
[0] = inactive
;
1077 present_pages
[1] = active
;
1080 return inactive_ratio
;
1083 int mem_cgroup_inactive_anon_is_low(struct mem_cgroup
*memcg
)
1085 unsigned long active
;
1086 unsigned long inactive
;
1087 unsigned long present_pages
[2];
1088 unsigned long inactive_ratio
;
1090 inactive_ratio
= calc_inactive_ratio(memcg
, present_pages
);
1092 inactive
= present_pages
[0];
1093 active
= present_pages
[1];
1095 if (inactive
* inactive_ratio
< active
)
1101 int mem_cgroup_inactive_file_is_low(struct mem_cgroup
*memcg
)
1103 unsigned long active
;
1104 unsigned long inactive
;
1106 inactive
= mem_cgroup_get_local_zonestat(memcg
, LRU_INACTIVE_FILE
);
1107 active
= mem_cgroup_get_local_zonestat(memcg
, LRU_ACTIVE_FILE
);
1109 return (active
> inactive
);
1112 unsigned long mem_cgroup_zone_nr_lru_pages(struct mem_cgroup
*memcg
,
1116 int nid
= zone_to_nid(zone
);
1117 int zid
= zone_idx(zone
);
1118 struct mem_cgroup_per_zone
*mz
= mem_cgroup_zoneinfo(memcg
, nid
, zid
);
1120 return MEM_CGROUP_ZSTAT(mz
, lru
);
1123 static unsigned long mem_cgroup_node_nr_file_lru_pages(struct mem_cgroup
*memcg
,
1128 ret
= mem_cgroup_get_zonestat_node(memcg
, nid
, LRU_INACTIVE_FILE
) +
1129 mem_cgroup_get_zonestat_node(memcg
, nid
, LRU_ACTIVE_FILE
);
1134 static unsigned long mem_cgroup_node_nr_anon_lru_pages(struct mem_cgroup
*memcg
,
1139 ret
= mem_cgroup_get_zonestat_node(memcg
, nid
, LRU_INACTIVE_ANON
) +
1140 mem_cgroup_get_zonestat_node(memcg
, nid
, LRU_ACTIVE_ANON
);
1144 #if MAX_NUMNODES > 1
1145 static unsigned long mem_cgroup_nr_file_lru_pages(struct mem_cgroup
*memcg
)
1150 for_each_node_state(nid
, N_HIGH_MEMORY
)
1151 total
+= mem_cgroup_node_nr_file_lru_pages(memcg
, nid
);
1156 static unsigned long mem_cgroup_nr_anon_lru_pages(struct mem_cgroup
*memcg
)
1161 for_each_node_state(nid
, N_HIGH_MEMORY
)
1162 total
+= mem_cgroup_node_nr_anon_lru_pages(memcg
, nid
);
1167 static unsigned long
1168 mem_cgroup_node_nr_unevictable_lru_pages(struct mem_cgroup
*memcg
, int nid
)
1170 return mem_cgroup_get_zonestat_node(memcg
, nid
, LRU_UNEVICTABLE
);
1173 static unsigned long
1174 mem_cgroup_nr_unevictable_lru_pages(struct mem_cgroup
*memcg
)
1179 for_each_node_state(nid
, N_HIGH_MEMORY
)
1180 total
+= mem_cgroup_node_nr_unevictable_lru_pages(memcg
, nid
);
1185 static unsigned long mem_cgroup_node_nr_lru_pages(struct mem_cgroup
*memcg
,
1192 total
+= mem_cgroup_get_zonestat_node(memcg
, nid
, l
);
1197 static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup
*memcg
)
1202 for_each_node_state(nid
, N_HIGH_MEMORY
)
1203 total
+= mem_cgroup_node_nr_lru_pages(memcg
, nid
);
1207 #endif /* CONFIG_NUMA */
1209 struct zone_reclaim_stat
*mem_cgroup_get_reclaim_stat(struct mem_cgroup
*memcg
,
1212 int nid
= zone_to_nid(zone
);
1213 int zid
= zone_idx(zone
);
1214 struct mem_cgroup_per_zone
*mz
= mem_cgroup_zoneinfo(memcg
, nid
, zid
);
1216 return &mz
->reclaim_stat
;
1219 struct zone_reclaim_stat
*
1220 mem_cgroup_get_reclaim_stat_from_page(struct page
*page
)
1222 struct page_cgroup
*pc
;
1223 struct mem_cgroup_per_zone
*mz
;
1225 if (mem_cgroup_disabled())
1228 pc
= lookup_page_cgroup(page
);
1229 if (!PageCgroupUsed(pc
))
1231 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1233 mz
= page_cgroup_zoneinfo(pc
->mem_cgroup
, page
);
1234 return &mz
->reclaim_stat
;
1237 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan
,
1238 struct list_head
*dst
,
1239 unsigned long *scanned
, int order
,
1240 int mode
, struct zone
*z
,
1241 struct mem_cgroup
*mem_cont
,
1242 int active
, int file
)
1244 unsigned long nr_taken
= 0;
1248 struct list_head
*src
;
1249 struct page_cgroup
*pc
, *tmp
;
1250 int nid
= zone_to_nid(z
);
1251 int zid
= zone_idx(z
);
1252 struct mem_cgroup_per_zone
*mz
;
1253 int lru
= LRU_FILE
* file
+ active
;
1257 mz
= mem_cgroup_zoneinfo(mem_cont
, nid
, zid
);
1258 src
= &mz
->lists
[lru
];
1261 list_for_each_entry_safe_reverse(pc
, tmp
, src
, lru
) {
1262 if (scan
>= nr_to_scan
)
1265 if (unlikely(!PageCgroupUsed(pc
)))
1268 page
= lookup_cgroup_page(pc
);
1270 if (unlikely(!PageLRU(page
)))
1274 ret
= __isolate_lru_page(page
, mode
, file
);
1277 list_move(&page
->lru
, dst
);
1278 mem_cgroup_del_lru(page
);
1279 nr_taken
+= hpage_nr_pages(page
);
1282 /* we don't affect global LRU but rotate in our LRU */
1283 mem_cgroup_rotate_lru_list(page
, page_lru(page
));
1292 trace_mm_vmscan_memcg_isolate(0, nr_to_scan
, scan
, nr_taken
,
1298 #define mem_cgroup_from_res_counter(counter, member) \
1299 container_of(counter, struct mem_cgroup, member)
1302 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
1303 * @mem: the memory cgroup
1305 * Returns the maximum amount of memory @mem can be charged with, in
1308 static unsigned long mem_cgroup_margin(struct mem_cgroup
*mem
)
1310 unsigned long long margin
;
1312 margin
= res_counter_margin(&mem
->res
);
1313 if (do_swap_account
)
1314 margin
= min(margin
, res_counter_margin(&mem
->memsw
));
1315 return margin
>> PAGE_SHIFT
;
1318 static unsigned int get_swappiness(struct mem_cgroup
*memcg
)
1320 struct cgroup
*cgrp
= memcg
->css
.cgroup
;
1323 if (cgrp
->parent
== NULL
)
1324 return vm_swappiness
;
1326 return memcg
->swappiness
;
1329 static void mem_cgroup_start_move(struct mem_cgroup
*mem
)
1334 spin_lock(&mem
->pcp_counter_lock
);
1335 for_each_online_cpu(cpu
)
1336 per_cpu(mem
->stat
->count
[MEM_CGROUP_ON_MOVE
], cpu
) += 1;
1337 mem
->nocpu_base
.count
[MEM_CGROUP_ON_MOVE
] += 1;
1338 spin_unlock(&mem
->pcp_counter_lock
);
1344 static void mem_cgroup_end_move(struct mem_cgroup
*mem
)
1351 spin_lock(&mem
->pcp_counter_lock
);
1352 for_each_online_cpu(cpu
)
1353 per_cpu(mem
->stat
->count
[MEM_CGROUP_ON_MOVE
], cpu
) -= 1;
1354 mem
->nocpu_base
.count
[MEM_CGROUP_ON_MOVE
] -= 1;
1355 spin_unlock(&mem
->pcp_counter_lock
);
1359 * 2 routines for checking "mem" is under move_account() or not.
1361 * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used
1362 * for avoiding race in accounting. If true,
1363 * pc->mem_cgroup may be overwritten.
1365 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1366 * under hierarchy of moving cgroups. This is for
1367 * waiting at hith-memory prressure caused by "move".
1370 static bool mem_cgroup_stealed(struct mem_cgroup
*mem
)
1372 VM_BUG_ON(!rcu_read_lock_held());
1373 return this_cpu_read(mem
->stat
->count
[MEM_CGROUP_ON_MOVE
]) > 0;
1376 static bool mem_cgroup_under_move(struct mem_cgroup
*mem
)
1378 struct mem_cgroup
*from
;
1379 struct mem_cgroup
*to
;
1382 * Unlike task_move routines, we access mc.to, mc.from not under
1383 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1385 spin_lock(&mc
.lock
);
1390 if (from
== mem
|| to
== mem
1391 || (mem
->use_hierarchy
&& css_is_ancestor(&from
->css
, &mem
->css
))
1392 || (mem
->use_hierarchy
&& css_is_ancestor(&to
->css
, &mem
->css
)))
1395 spin_unlock(&mc
.lock
);
1399 static bool mem_cgroup_wait_acct_move(struct mem_cgroup
*mem
)
1401 if (mc
.moving_task
&& current
!= mc
.moving_task
) {
1402 if (mem_cgroup_under_move(mem
)) {
1404 prepare_to_wait(&mc
.waitq
, &wait
, TASK_INTERRUPTIBLE
);
1405 /* moving charge context might have finished. */
1408 finish_wait(&mc
.waitq
, &wait
);
1416 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
1417 * @memcg: The memory cgroup that went over limit
1418 * @p: Task that is going to be killed
1420 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1423 void mem_cgroup_print_oom_info(struct mem_cgroup
*memcg
, struct task_struct
*p
)
1425 struct cgroup
*task_cgrp
;
1426 struct cgroup
*mem_cgrp
;
1428 * Need a buffer in BSS, can't rely on allocations. The code relies
1429 * on the assumption that OOM is serialized for memory controller.
1430 * If this assumption is broken, revisit this code.
1432 static char memcg_name
[PATH_MAX
];
1441 mem_cgrp
= memcg
->css
.cgroup
;
1442 task_cgrp
= task_cgroup(p
, mem_cgroup_subsys_id
);
1444 ret
= cgroup_path(task_cgrp
, memcg_name
, PATH_MAX
);
1447 * Unfortunately, we are unable to convert to a useful name
1448 * But we'll still print out the usage information
1455 printk(KERN_INFO
"Task in %s killed", memcg_name
);
1458 ret
= cgroup_path(mem_cgrp
, memcg_name
, PATH_MAX
);
1466 * Continues from above, so we don't need an KERN_ level
1468 printk(KERN_CONT
" as a result of limit of %s\n", memcg_name
);
1471 printk(KERN_INFO
"memory: usage %llukB, limit %llukB, failcnt %llu\n",
1472 res_counter_read_u64(&memcg
->res
, RES_USAGE
) >> 10,
1473 res_counter_read_u64(&memcg
->res
, RES_LIMIT
) >> 10,
1474 res_counter_read_u64(&memcg
->res
, RES_FAILCNT
));
1475 printk(KERN_INFO
"memory+swap: usage %llukB, limit %llukB, "
1477 res_counter_read_u64(&memcg
->memsw
, RES_USAGE
) >> 10,
1478 res_counter_read_u64(&memcg
->memsw
, RES_LIMIT
) >> 10,
1479 res_counter_read_u64(&memcg
->memsw
, RES_FAILCNT
));
1483 * This function returns the number of memcg under hierarchy tree. Returns
1484 * 1(self count) if no children.
1486 static int mem_cgroup_count_children(struct mem_cgroup
*mem
)
1489 struct mem_cgroup
*iter
;
1491 for_each_mem_cgroup_tree(iter
, mem
)
1497 * Return the memory (and swap, if configured) limit for a memcg.
1499 u64
mem_cgroup_get_limit(struct mem_cgroup
*memcg
)
1504 limit
= res_counter_read_u64(&memcg
->res
, RES_LIMIT
);
1505 limit
+= total_swap_pages
<< PAGE_SHIFT
;
1507 memsw
= res_counter_read_u64(&memcg
->memsw
, RES_LIMIT
);
1509 * If memsw is finite and limits the amount of swap space available
1510 * to this memcg, return that limit.
1512 return min(limit
, memsw
);
1516 * Visit the first child (need not be the first child as per the ordering
1517 * of the cgroup list, since we track last_scanned_child) of @mem and use
1518 * that to reclaim free pages from.
1520 static struct mem_cgroup
*
1521 mem_cgroup_select_victim(struct mem_cgroup
*root_mem
)
1523 struct mem_cgroup
*ret
= NULL
;
1524 struct cgroup_subsys_state
*css
;
1527 if (!root_mem
->use_hierarchy
) {
1528 css_get(&root_mem
->css
);
1534 nextid
= root_mem
->last_scanned_child
+ 1;
1535 css
= css_get_next(&mem_cgroup_subsys
, nextid
, &root_mem
->css
,
1537 if (css
&& css_tryget(css
))
1538 ret
= container_of(css
, struct mem_cgroup
, css
);
1541 /* Updates scanning parameter */
1543 /* this means start scan from ID:1 */
1544 root_mem
->last_scanned_child
= 0;
1546 root_mem
->last_scanned_child
= found
;
1553 * test_mem_cgroup_node_reclaimable
1554 * @mem: the target memcg
1555 * @nid: the node ID to be checked.
1556 * @noswap : specify true here if the user wants flle only information.
1558 * This function returns whether the specified memcg contains any
1559 * reclaimable pages on a node. Returns true if there are any reclaimable
1560 * pages in the node.
1562 static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup
*mem
,
1563 int nid
, bool noswap
)
1565 if (mem_cgroup_node_nr_file_lru_pages(mem
, nid
))
1567 if (noswap
|| !total_swap_pages
)
1569 if (mem_cgroup_node_nr_anon_lru_pages(mem
, nid
))
1574 #if MAX_NUMNODES > 1
1577 * Always updating the nodemask is not very good - even if we have an empty
1578 * list or the wrong list here, we can start from some node and traverse all
1579 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1582 static void mem_cgroup_may_update_nodemask(struct mem_cgroup
*mem
)
1586 if (time_after(mem
->next_scan_node_update
, jiffies
))
1589 mem
->next_scan_node_update
= jiffies
+ 10*HZ
;
1590 /* make a nodemask where this memcg uses memory from */
1591 mem
->scan_nodes
= node_states
[N_HIGH_MEMORY
];
1593 for_each_node_mask(nid
, node_states
[N_HIGH_MEMORY
]) {
1595 if (!test_mem_cgroup_node_reclaimable(mem
, nid
, false))
1596 node_clear(nid
, mem
->scan_nodes
);
1601 * Selecting a node where we start reclaim from. Because what we need is just
1602 * reducing usage counter, start from anywhere is O,K. Considering
1603 * memory reclaim from current node, there are pros. and cons.
1605 * Freeing memory from current node means freeing memory from a node which
1606 * we'll use or we've used. So, it may make LRU bad. And if several threads
1607 * hit limits, it will see a contention on a node. But freeing from remote
1608 * node means more costs for memory reclaim because of memory latency.
1610 * Now, we use round-robin. Better algorithm is welcomed.
1612 int mem_cgroup_select_victim_node(struct mem_cgroup
*mem
)
1616 mem_cgroup_may_update_nodemask(mem
);
1617 node
= mem
->last_scanned_node
;
1619 node
= next_node(node
, mem
->scan_nodes
);
1620 if (node
== MAX_NUMNODES
)
1621 node
= first_node(mem
->scan_nodes
);
1623 * We call this when we hit limit, not when pages are added to LRU.
1624 * No LRU may hold pages because all pages are UNEVICTABLE or
1625 * memcg is too small and all pages are not on LRU. In that case,
1626 * we use curret node.
1628 if (unlikely(node
== MAX_NUMNODES
))
1629 node
= numa_node_id();
1631 mem
->last_scanned_node
= node
;
1636 * Check all nodes whether it contains reclaimable pages or not.
1637 * For quick scan, we make use of scan_nodes. This will allow us to skip
1638 * unused nodes. But scan_nodes is lazily updated and may not cotain
1639 * enough new information. We need to do double check.
1641 bool mem_cgroup_reclaimable(struct mem_cgroup
*mem
, bool noswap
)
1646 * quick check...making use of scan_node.
1647 * We can skip unused nodes.
1649 if (!nodes_empty(mem
->scan_nodes
)) {
1650 for (nid
= first_node(mem
->scan_nodes
);
1652 nid
= next_node(nid
, mem
->scan_nodes
)) {
1654 if (test_mem_cgroup_node_reclaimable(mem
, nid
, noswap
))
1659 * Check rest of nodes.
1661 for_each_node_state(nid
, N_HIGH_MEMORY
) {
1662 if (node_isset(nid
, mem
->scan_nodes
))
1664 if (test_mem_cgroup_node_reclaimable(mem
, nid
, noswap
))
1671 int mem_cgroup_select_victim_node(struct mem_cgroup
*mem
)
1676 bool mem_cgroup_reclaimable(struct mem_cgroup
*mem
, bool noswap
)
1678 return test_mem_cgroup_node_reclaimable(mem
, 0, noswap
);
1683 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1684 * we reclaimed from, so that we don't end up penalizing one child extensively
1685 * based on its position in the children list.
1687 * root_mem is the original ancestor that we've been reclaim from.
1689 * We give up and return to the caller when we visit root_mem twice.
1690 * (other groups can be removed while we're walking....)
1692 * If shrink==true, for avoiding to free too much, this returns immedieately.
1694 static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup
*root_mem
,
1697 unsigned long reclaim_options
,
1698 unsigned long *total_scanned
)
1700 struct mem_cgroup
*victim
;
1703 bool noswap
= reclaim_options
& MEM_CGROUP_RECLAIM_NOSWAP
;
1704 bool shrink
= reclaim_options
& MEM_CGROUP_RECLAIM_SHRINK
;
1705 bool check_soft
= reclaim_options
& MEM_CGROUP_RECLAIM_SOFT
;
1706 unsigned long excess
;
1707 unsigned long nr_scanned
;
1709 excess
= res_counter_soft_limit_excess(&root_mem
->res
) >> PAGE_SHIFT
;
1711 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1712 if (!check_soft
&& root_mem
->memsw_is_minimum
)
1716 victim
= mem_cgroup_select_victim(root_mem
);
1717 if (victim
== root_mem
) {
1720 * We are not draining per cpu cached charges during
1721 * soft limit reclaim because global reclaim doesn't
1722 * care about charges. It tries to free some memory and
1723 * charges will not give any.
1725 if (!check_soft
&& loop
>= 1)
1726 drain_all_stock_async(root_mem
);
1729 * If we have not been able to reclaim
1730 * anything, it might because there are
1731 * no reclaimable pages under this hierarchy
1733 if (!check_soft
|| !total
) {
1734 css_put(&victim
->css
);
1738 * We want to do more targeted reclaim.
1739 * excess >> 2 is not to excessive so as to
1740 * reclaim too much, nor too less that we keep
1741 * coming back to reclaim from this cgroup
1743 if (total
>= (excess
>> 2) ||
1744 (loop
> MEM_CGROUP_MAX_RECLAIM_LOOPS
)) {
1745 css_put(&victim
->css
);
1750 if (!mem_cgroup_reclaimable(victim
, noswap
)) {
1751 /* this cgroup's local usage == 0 */
1752 css_put(&victim
->css
);
1755 /* we use swappiness of local cgroup */
1757 ret
= mem_cgroup_shrink_node_zone(victim
, gfp_mask
,
1758 noswap
, get_swappiness(victim
), zone
,
1760 *total_scanned
+= nr_scanned
;
1762 ret
= try_to_free_mem_cgroup_pages(victim
, gfp_mask
,
1763 noswap
, get_swappiness(victim
));
1764 css_put(&victim
->css
);
1766 * At shrinking usage, we can't check we should stop here or
1767 * reclaim more. It's depends on callers. last_scanned_child
1768 * will work enough for keeping fairness under tree.
1774 if (!res_counter_soft_limit_excess(&root_mem
->res
))
1776 } else if (mem_cgroup_margin(root_mem
))
1783 * Check OOM-Killer is already running under our hierarchy.
1784 * If someone is running, return false.
1786 static bool mem_cgroup_oom_lock(struct mem_cgroup
*mem
)
1788 int x
, lock_count
= 0;
1789 struct mem_cgroup
*iter
;
1791 for_each_mem_cgroup_tree(iter
, mem
) {
1792 x
= atomic_inc_return(&iter
->oom_lock
);
1793 lock_count
= max(x
, lock_count
);
1796 if (lock_count
== 1)
1801 static int mem_cgroup_oom_unlock(struct mem_cgroup
*mem
)
1803 struct mem_cgroup
*iter
;
1806 * When a new child is created while the hierarchy is under oom,
1807 * mem_cgroup_oom_lock() may not be called. We have to use
1808 * atomic_add_unless() here.
1810 for_each_mem_cgroup_tree(iter
, mem
)
1811 atomic_add_unless(&iter
->oom_lock
, -1, 0);
1816 static DEFINE_MUTEX(memcg_oom_mutex
);
1817 static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq
);
1819 struct oom_wait_info
{
1820 struct mem_cgroup
*mem
;
1824 static int memcg_oom_wake_function(wait_queue_t
*wait
,
1825 unsigned mode
, int sync
, void *arg
)
1827 struct mem_cgroup
*wake_mem
= (struct mem_cgroup
*)arg
;
1828 struct oom_wait_info
*oom_wait_info
;
1830 oom_wait_info
= container_of(wait
, struct oom_wait_info
, wait
);
1832 if (oom_wait_info
->mem
== wake_mem
)
1834 /* if no hierarchy, no match */
1835 if (!oom_wait_info
->mem
->use_hierarchy
|| !wake_mem
->use_hierarchy
)
1838 * Both of oom_wait_info->mem and wake_mem are stable under us.
1839 * Then we can use css_is_ancestor without taking care of RCU.
1841 if (!css_is_ancestor(&oom_wait_info
->mem
->css
, &wake_mem
->css
) &&
1842 !css_is_ancestor(&wake_mem
->css
, &oom_wait_info
->mem
->css
))
1846 return autoremove_wake_function(wait
, mode
, sync
, arg
);
1849 static void memcg_wakeup_oom(struct mem_cgroup
*mem
)
1851 /* for filtering, pass "mem" as argument. */
1852 __wake_up(&memcg_oom_waitq
, TASK_NORMAL
, 0, mem
);
1855 static void memcg_oom_recover(struct mem_cgroup
*mem
)
1857 if (mem
&& atomic_read(&mem
->oom_lock
))
1858 memcg_wakeup_oom(mem
);
1862 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1864 bool mem_cgroup_handle_oom(struct mem_cgroup
*mem
, gfp_t mask
)
1866 struct oom_wait_info owait
;
1867 bool locked
, need_to_kill
;
1870 owait
.wait
.flags
= 0;
1871 owait
.wait
.func
= memcg_oom_wake_function
;
1872 owait
.wait
.private = current
;
1873 INIT_LIST_HEAD(&owait
.wait
.task_list
);
1874 need_to_kill
= true;
1875 /* At first, try to OOM lock hierarchy under mem.*/
1876 mutex_lock(&memcg_oom_mutex
);
1877 locked
= mem_cgroup_oom_lock(mem
);
1879 * Even if signal_pending(), we can't quit charge() loop without
1880 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1881 * under OOM is always welcomed, use TASK_KILLABLE here.
1883 prepare_to_wait(&memcg_oom_waitq
, &owait
.wait
, TASK_KILLABLE
);
1884 if (!locked
|| mem
->oom_kill_disable
)
1885 need_to_kill
= false;
1887 mem_cgroup_oom_notify(mem
);
1888 mutex_unlock(&memcg_oom_mutex
);
1891 finish_wait(&memcg_oom_waitq
, &owait
.wait
);
1892 mem_cgroup_out_of_memory(mem
, mask
);
1895 finish_wait(&memcg_oom_waitq
, &owait
.wait
);
1897 mutex_lock(&memcg_oom_mutex
);
1898 mem_cgroup_oom_unlock(mem
);
1899 memcg_wakeup_oom(mem
);
1900 mutex_unlock(&memcg_oom_mutex
);
1902 if (test_thread_flag(TIF_MEMDIE
) || fatal_signal_pending(current
))
1904 /* Give chance to dying process */
1905 schedule_timeout(1);
1910 * Currently used to update mapped file statistics, but the routine can be
1911 * generalized to update other statistics as well.
1913 * Notes: Race condition
1915 * We usually use page_cgroup_lock() for accessing page_cgroup member but
1916 * it tends to be costly. But considering some conditions, we doesn't need
1917 * to do so _always_.
1919 * Considering "charge", lock_page_cgroup() is not required because all
1920 * file-stat operations happen after a page is attached to radix-tree. There
1921 * are no race with "charge".
1923 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
1924 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
1925 * if there are race with "uncharge". Statistics itself is properly handled
1928 * Considering "move", this is an only case we see a race. To make the race
1929 * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are
1930 * possibility of race condition. If there is, we take a lock.
1933 void mem_cgroup_update_page_stat(struct page
*page
,
1934 enum mem_cgroup_page_stat_item idx
, int val
)
1936 struct mem_cgroup
*mem
;
1937 struct page_cgroup
*pc
= lookup_page_cgroup(page
);
1938 bool need_unlock
= false;
1939 unsigned long uninitialized_var(flags
);
1945 mem
= pc
->mem_cgroup
;
1946 if (unlikely(!mem
|| !PageCgroupUsed(pc
)))
1948 /* pc->mem_cgroup is unstable ? */
1949 if (unlikely(mem_cgroup_stealed(mem
)) || PageTransHuge(page
)) {
1950 /* take a lock against to access pc->mem_cgroup */
1951 move_lock_page_cgroup(pc
, &flags
);
1953 mem
= pc
->mem_cgroup
;
1954 if (!mem
|| !PageCgroupUsed(pc
))
1959 case MEMCG_NR_FILE_MAPPED
:
1961 SetPageCgroupFileMapped(pc
);
1962 else if (!page_mapped(page
))
1963 ClearPageCgroupFileMapped(pc
);
1964 idx
= MEM_CGROUP_STAT_FILE_MAPPED
;
1970 this_cpu_add(mem
->stat
->count
[idx
], val
);
1973 if (unlikely(need_unlock
))
1974 move_unlock_page_cgroup(pc
, &flags
);
1978 EXPORT_SYMBOL(mem_cgroup_update_page_stat
);
1981 * size of first charge trial. "32" comes from vmscan.c's magic value.
1982 * TODO: maybe necessary to use big numbers in big irons.
1984 #define CHARGE_BATCH 32U
1985 struct memcg_stock_pcp
{
1986 struct mem_cgroup
*cached
; /* this never be root cgroup */
1987 unsigned int nr_pages
;
1988 struct work_struct work
;
1989 unsigned long flags
;
1990 #define FLUSHING_CACHED_CHARGE (0)
1992 static DEFINE_PER_CPU(struct memcg_stock_pcp
, memcg_stock
);
1993 static DEFINE_MUTEX(percpu_charge_mutex
);
1996 * Try to consume stocked charge on this cpu. If success, one page is consumed
1997 * from local stock and true is returned. If the stock is 0 or charges from a
1998 * cgroup which is not current target, returns false. This stock will be
2001 static bool consume_stock(struct mem_cgroup
*mem
)
2003 struct memcg_stock_pcp
*stock
;
2006 stock
= &get_cpu_var(memcg_stock
);
2007 if (mem
== stock
->cached
&& stock
->nr_pages
)
2009 else /* need to call res_counter_charge */
2011 put_cpu_var(memcg_stock
);
2016 * Returns stocks cached in percpu to res_counter and reset cached information.
2018 static void drain_stock(struct memcg_stock_pcp
*stock
)
2020 struct mem_cgroup
*old
= stock
->cached
;
2022 if (stock
->nr_pages
) {
2023 unsigned long bytes
= stock
->nr_pages
* PAGE_SIZE
;
2025 res_counter_uncharge(&old
->res
, bytes
);
2026 if (do_swap_account
)
2027 res_counter_uncharge(&old
->memsw
, bytes
);
2028 stock
->nr_pages
= 0;
2030 stock
->cached
= NULL
;
2034 * This must be called under preempt disabled or must be called by
2035 * a thread which is pinned to local cpu.
2037 static void drain_local_stock(struct work_struct
*dummy
)
2039 struct memcg_stock_pcp
*stock
= &__get_cpu_var(memcg_stock
);
2041 clear_bit(FLUSHING_CACHED_CHARGE
, &stock
->flags
);
2045 * Cache charges(val) which is from res_counter, to local per_cpu area.
2046 * This will be consumed by consume_stock() function, later.
2048 static void refill_stock(struct mem_cgroup
*mem
, unsigned int nr_pages
)
2050 struct memcg_stock_pcp
*stock
= &get_cpu_var(memcg_stock
);
2052 if (stock
->cached
!= mem
) { /* reset if necessary */
2054 stock
->cached
= mem
;
2056 stock
->nr_pages
+= nr_pages
;
2057 put_cpu_var(memcg_stock
);
2061 * Tries to drain stocked charges in other cpus. This function is asynchronous
2062 * and just put a work per cpu for draining localy on each cpu. Caller can
2063 * expects some charges will be back to res_counter later but cannot wait for
2066 static void drain_all_stock_async(struct mem_cgroup
*root_mem
)
2070 * If someone calls draining, avoid adding more kworker runs.
2072 if (!mutex_trylock(&percpu_charge_mutex
))
2074 /* Notify other cpus that system-wide "drain" is running */
2077 * Get a hint for avoiding draining charges on the current cpu,
2078 * which must be exhausted by our charging. It is not required that
2079 * this be a precise check, so we use raw_smp_processor_id() instead of
2080 * getcpu()/putcpu().
2082 curcpu
= raw_smp_processor_id();
2083 for_each_online_cpu(cpu
) {
2084 struct memcg_stock_pcp
*stock
= &per_cpu(memcg_stock
, cpu
);
2085 struct mem_cgroup
*mem
;
2090 mem
= stock
->cached
;
2093 if (mem
!= root_mem
) {
2094 if (!root_mem
->use_hierarchy
)
2096 /* check whether "mem" is under tree of "root_mem" */
2097 if (!css_is_ancestor(&mem
->css
, &root_mem
->css
))
2100 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE
, &stock
->flags
))
2101 schedule_work_on(cpu
, &stock
->work
);
2104 mutex_unlock(&percpu_charge_mutex
);
2105 /* We don't wait for flush_work */
2108 /* This is a synchronous drain interface. */
2109 static void drain_all_stock_sync(void)
2111 /* called when force_empty is called */
2112 mutex_lock(&percpu_charge_mutex
);
2113 schedule_on_each_cpu(drain_local_stock
);
2114 mutex_unlock(&percpu_charge_mutex
);
2118 * This function drains percpu counter value from DEAD cpu and
2119 * move it to local cpu. Note that this function can be preempted.
2121 static void mem_cgroup_drain_pcp_counter(struct mem_cgroup
*mem
, int cpu
)
2125 spin_lock(&mem
->pcp_counter_lock
);
2126 for (i
= 0; i
< MEM_CGROUP_STAT_DATA
; i
++) {
2127 long x
= per_cpu(mem
->stat
->count
[i
], cpu
);
2129 per_cpu(mem
->stat
->count
[i
], cpu
) = 0;
2130 mem
->nocpu_base
.count
[i
] += x
;
2132 for (i
= 0; i
< MEM_CGROUP_EVENTS_NSTATS
; i
++) {
2133 unsigned long x
= per_cpu(mem
->stat
->events
[i
], cpu
);
2135 per_cpu(mem
->stat
->events
[i
], cpu
) = 0;
2136 mem
->nocpu_base
.events
[i
] += x
;
2138 /* need to clear ON_MOVE value, works as a kind of lock. */
2139 per_cpu(mem
->stat
->count
[MEM_CGROUP_ON_MOVE
], cpu
) = 0;
2140 spin_unlock(&mem
->pcp_counter_lock
);
2143 static void synchronize_mem_cgroup_on_move(struct mem_cgroup
*mem
, int cpu
)
2145 int idx
= MEM_CGROUP_ON_MOVE
;
2147 spin_lock(&mem
->pcp_counter_lock
);
2148 per_cpu(mem
->stat
->count
[idx
], cpu
) = mem
->nocpu_base
.count
[idx
];
2149 spin_unlock(&mem
->pcp_counter_lock
);
2152 static int __cpuinit
memcg_cpu_hotplug_callback(struct notifier_block
*nb
,
2153 unsigned long action
,
2156 int cpu
= (unsigned long)hcpu
;
2157 struct memcg_stock_pcp
*stock
;
2158 struct mem_cgroup
*iter
;
2160 if ((action
== CPU_ONLINE
)) {
2161 for_each_mem_cgroup_all(iter
)
2162 synchronize_mem_cgroup_on_move(iter
, cpu
);
2166 if ((action
!= CPU_DEAD
) || action
!= CPU_DEAD_FROZEN
)
2169 for_each_mem_cgroup_all(iter
)
2170 mem_cgroup_drain_pcp_counter(iter
, cpu
);
2172 stock
= &per_cpu(memcg_stock
, cpu
);
2178 /* See __mem_cgroup_try_charge() for details */
2180 CHARGE_OK
, /* success */
2181 CHARGE_RETRY
, /* need to retry but retry is not bad */
2182 CHARGE_NOMEM
, /* we can't do more. return -ENOMEM */
2183 CHARGE_WOULDBLOCK
, /* GFP_WAIT wasn't set and no enough res. */
2184 CHARGE_OOM_DIE
, /* the current is killed because of OOM */
2187 static int mem_cgroup_do_charge(struct mem_cgroup
*mem
, gfp_t gfp_mask
,
2188 unsigned int nr_pages
, bool oom_check
)
2190 unsigned long csize
= nr_pages
* PAGE_SIZE
;
2191 struct mem_cgroup
*mem_over_limit
;
2192 struct res_counter
*fail_res
;
2193 unsigned long flags
= 0;
2196 ret
= res_counter_charge(&mem
->res
, csize
, &fail_res
);
2199 if (!do_swap_account
)
2201 ret
= res_counter_charge(&mem
->memsw
, csize
, &fail_res
);
2205 res_counter_uncharge(&mem
->res
, csize
);
2206 mem_over_limit
= mem_cgroup_from_res_counter(fail_res
, memsw
);
2207 flags
|= MEM_CGROUP_RECLAIM_NOSWAP
;
2209 mem_over_limit
= mem_cgroup_from_res_counter(fail_res
, res
);
2211 * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch
2212 * of regular pages (CHARGE_BATCH), or a single regular page (1).
2214 * Never reclaim on behalf of optional batching, retry with a
2215 * single page instead.
2217 if (nr_pages
== CHARGE_BATCH
)
2218 return CHARGE_RETRY
;
2220 if (!(gfp_mask
& __GFP_WAIT
))
2221 return CHARGE_WOULDBLOCK
;
2223 ret
= mem_cgroup_hierarchical_reclaim(mem_over_limit
, NULL
,
2224 gfp_mask
, flags
, NULL
);
2225 if (mem_cgroup_margin(mem_over_limit
) >= nr_pages
)
2226 return CHARGE_RETRY
;
2228 * Even though the limit is exceeded at this point, reclaim
2229 * may have been able to free some pages. Retry the charge
2230 * before killing the task.
2232 * Only for regular pages, though: huge pages are rather
2233 * unlikely to succeed so close to the limit, and we fall back
2234 * to regular pages anyway in case of failure.
2236 if (nr_pages
== 1 && ret
)
2237 return CHARGE_RETRY
;
2240 * At task move, charge accounts can be doubly counted. So, it's
2241 * better to wait until the end of task_move if something is going on.
2243 if (mem_cgroup_wait_acct_move(mem_over_limit
))
2244 return CHARGE_RETRY
;
2246 /* If we don't need to call oom-killer at el, return immediately */
2248 return CHARGE_NOMEM
;
2250 if (!mem_cgroup_handle_oom(mem_over_limit
, gfp_mask
))
2251 return CHARGE_OOM_DIE
;
2253 return CHARGE_RETRY
;
2257 * Unlike exported interface, "oom" parameter is added. if oom==true,
2258 * oom-killer can be invoked.
2260 static int __mem_cgroup_try_charge(struct mm_struct
*mm
,
2262 unsigned int nr_pages
,
2263 struct mem_cgroup
**memcg
,
2266 unsigned int batch
= max(CHARGE_BATCH
, nr_pages
);
2267 int nr_oom_retries
= MEM_CGROUP_RECLAIM_RETRIES
;
2268 struct mem_cgroup
*mem
= NULL
;
2272 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2273 * in system level. So, allow to go ahead dying process in addition to
2276 if (unlikely(test_thread_flag(TIF_MEMDIE
)
2277 || fatal_signal_pending(current
)))
2281 * We always charge the cgroup the mm_struct belongs to.
2282 * The mm_struct's mem_cgroup changes on task migration if the
2283 * thread group leader migrates. It's possible that mm is not
2284 * set, if so charge the init_mm (happens for pagecache usage).
2289 if (*memcg
) { /* css should be a valid one */
2291 VM_BUG_ON(css_is_removed(&mem
->css
));
2292 if (mem_cgroup_is_root(mem
))
2294 if (nr_pages
== 1 && consume_stock(mem
))
2298 struct task_struct
*p
;
2301 p
= rcu_dereference(mm
->owner
);
2303 * Because we don't have task_lock(), "p" can exit.
2304 * In that case, "mem" can point to root or p can be NULL with
2305 * race with swapoff. Then, we have small risk of mis-accouning.
2306 * But such kind of mis-account by race always happens because
2307 * we don't have cgroup_mutex(). It's overkill and we allo that
2309 * (*) swapoff at el will charge against mm-struct not against
2310 * task-struct. So, mm->owner can be NULL.
2312 mem
= mem_cgroup_from_task(p
);
2313 if (!mem
|| mem_cgroup_is_root(mem
)) {
2317 if (nr_pages
== 1 && consume_stock(mem
)) {
2319 * It seems dagerous to access memcg without css_get().
2320 * But considering how consume_stok works, it's not
2321 * necessary. If consume_stock success, some charges
2322 * from this memcg are cached on this cpu. So, we
2323 * don't need to call css_get()/css_tryget() before
2324 * calling consume_stock().
2329 /* after here, we may be blocked. we need to get refcnt */
2330 if (!css_tryget(&mem
->css
)) {
2340 /* If killed, bypass charge */
2341 if (fatal_signal_pending(current
)) {
2347 if (oom
&& !nr_oom_retries
) {
2349 nr_oom_retries
= MEM_CGROUP_RECLAIM_RETRIES
;
2352 ret
= mem_cgroup_do_charge(mem
, gfp_mask
, batch
, oom_check
);
2356 case CHARGE_RETRY
: /* not in OOM situation but retry */
2361 case CHARGE_WOULDBLOCK
: /* !__GFP_WAIT */
2364 case CHARGE_NOMEM
: /* OOM routine works */
2369 /* If oom, we never return -ENOMEM */
2372 case CHARGE_OOM_DIE
: /* Killed by OOM Killer */
2376 } while (ret
!= CHARGE_OK
);
2378 if (batch
> nr_pages
)
2379 refill_stock(mem
, batch
- nr_pages
);
2393 * Somemtimes we have to undo a charge we got by try_charge().
2394 * This function is for that and do uncharge, put css's refcnt.
2395 * gotten by try_charge().
2397 static void __mem_cgroup_cancel_charge(struct mem_cgroup
*mem
,
2398 unsigned int nr_pages
)
2400 if (!mem_cgroup_is_root(mem
)) {
2401 unsigned long bytes
= nr_pages
* PAGE_SIZE
;
2403 res_counter_uncharge(&mem
->res
, bytes
);
2404 if (do_swap_account
)
2405 res_counter_uncharge(&mem
->memsw
, bytes
);
2410 * A helper function to get mem_cgroup from ID. must be called under
2411 * rcu_read_lock(). The caller must check css_is_removed() or some if
2412 * it's concern. (dropping refcnt from swap can be called against removed
2415 static struct mem_cgroup
*mem_cgroup_lookup(unsigned short id
)
2417 struct cgroup_subsys_state
*css
;
2419 /* ID 0 is unused ID */
2422 css
= css_lookup(&mem_cgroup_subsys
, id
);
2425 return container_of(css
, struct mem_cgroup
, css
);
2428 struct mem_cgroup
*try_get_mem_cgroup_from_page(struct page
*page
)
2430 struct mem_cgroup
*mem
= NULL
;
2431 struct page_cgroup
*pc
;
2435 VM_BUG_ON(!PageLocked(page
));
2437 pc
= lookup_page_cgroup(page
);
2438 lock_page_cgroup(pc
);
2439 if (PageCgroupUsed(pc
)) {
2440 mem
= pc
->mem_cgroup
;
2441 if (mem
&& !css_tryget(&mem
->css
))
2443 } else if (PageSwapCache(page
)) {
2444 ent
.val
= page_private(page
);
2445 id
= lookup_swap_cgroup(ent
);
2447 mem
= mem_cgroup_lookup(id
);
2448 if (mem
&& !css_tryget(&mem
->css
))
2452 unlock_page_cgroup(pc
);
2456 static void __mem_cgroup_commit_charge(struct mem_cgroup
*mem
,
2458 unsigned int nr_pages
,
2459 struct page_cgroup
*pc
,
2460 enum charge_type ctype
)
2462 lock_page_cgroup(pc
);
2463 if (unlikely(PageCgroupUsed(pc
))) {
2464 unlock_page_cgroup(pc
);
2465 __mem_cgroup_cancel_charge(mem
, nr_pages
);
2469 * we don't need page_cgroup_lock about tail pages, becase they are not
2470 * accessed by any other context at this point.
2472 pc
->mem_cgroup
= mem
;
2474 * We access a page_cgroup asynchronously without lock_page_cgroup().
2475 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2476 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2477 * before USED bit, we need memory barrier here.
2478 * See mem_cgroup_add_lru_list(), etc.
2482 case MEM_CGROUP_CHARGE_TYPE_CACHE
:
2483 case MEM_CGROUP_CHARGE_TYPE_SHMEM
:
2484 SetPageCgroupCache(pc
);
2485 SetPageCgroupUsed(pc
);
2487 case MEM_CGROUP_CHARGE_TYPE_MAPPED
:
2488 ClearPageCgroupCache(pc
);
2489 SetPageCgroupUsed(pc
);
2495 mem_cgroup_charge_statistics(mem
, PageCgroupCache(pc
), nr_pages
);
2496 unlock_page_cgroup(pc
);
2498 * "charge_statistics" updated event counter. Then, check it.
2499 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2500 * if they exceeds softlimit.
2502 memcg_check_events(mem
, page
);
2505 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
2507 #define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\
2508 (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION))
2510 * Because tail pages are not marked as "used", set it. We're under
2511 * zone->lru_lock, 'splitting on pmd' and compund_lock.
2513 void mem_cgroup_split_huge_fixup(struct page
*head
, struct page
*tail
)
2515 struct page_cgroup
*head_pc
= lookup_page_cgroup(head
);
2516 struct page_cgroup
*tail_pc
= lookup_page_cgroup(tail
);
2517 unsigned long flags
;
2519 if (mem_cgroup_disabled())
2522 * We have no races with charge/uncharge but will have races with
2523 * page state accounting.
2525 move_lock_page_cgroup(head_pc
, &flags
);
2527 tail_pc
->mem_cgroup
= head_pc
->mem_cgroup
;
2528 smp_wmb(); /* see __commit_charge() */
2529 if (PageCgroupAcctLRU(head_pc
)) {
2531 struct mem_cgroup_per_zone
*mz
;
2534 * LRU flags cannot be copied because we need to add tail
2535 *.page to LRU by generic call and our hook will be called.
2536 * We hold lru_lock, then, reduce counter directly.
2538 lru
= page_lru(head
);
2539 mz
= page_cgroup_zoneinfo(head_pc
->mem_cgroup
, head
);
2540 MEM_CGROUP_ZSTAT(mz
, lru
) -= 1;
2542 tail_pc
->flags
= head_pc
->flags
& ~PCGF_NOCOPY_AT_SPLIT
;
2543 move_unlock_page_cgroup(head_pc
, &flags
);
2548 * mem_cgroup_move_account - move account of the page
2550 * @nr_pages: number of regular pages (>1 for huge pages)
2551 * @pc: page_cgroup of the page.
2552 * @from: mem_cgroup which the page is moved from.
2553 * @to: mem_cgroup which the page is moved to. @from != @to.
2554 * @uncharge: whether we should call uncharge and css_put against @from.
2556 * The caller must confirm following.
2557 * - page is not on LRU (isolate_page() is useful.)
2558 * - compound_lock is held when nr_pages > 1
2560 * This function doesn't do "charge" nor css_get to new cgroup. It should be
2561 * done by a caller(__mem_cgroup_try_charge would be useful). If @uncharge is
2562 * true, this function does "uncharge" from old cgroup, but it doesn't if
2563 * @uncharge is false, so a caller should do "uncharge".
2565 static int mem_cgroup_move_account(struct page
*page
,
2566 unsigned int nr_pages
,
2567 struct page_cgroup
*pc
,
2568 struct mem_cgroup
*from
,
2569 struct mem_cgroup
*to
,
2572 unsigned long flags
;
2575 VM_BUG_ON(from
== to
);
2576 VM_BUG_ON(PageLRU(page
));
2578 * The page is isolated from LRU. So, collapse function
2579 * will not handle this page. But page splitting can happen.
2580 * Do this check under compound_page_lock(). The caller should
2584 if (nr_pages
> 1 && !PageTransHuge(page
))
2587 lock_page_cgroup(pc
);
2590 if (!PageCgroupUsed(pc
) || pc
->mem_cgroup
!= from
)
2593 move_lock_page_cgroup(pc
, &flags
);
2595 if (PageCgroupFileMapped(pc
)) {
2596 /* Update mapped_file data for mem_cgroup */
2598 __this_cpu_dec(from
->stat
->count
[MEM_CGROUP_STAT_FILE_MAPPED
]);
2599 __this_cpu_inc(to
->stat
->count
[MEM_CGROUP_STAT_FILE_MAPPED
]);
2602 mem_cgroup_charge_statistics(from
, PageCgroupCache(pc
), -nr_pages
);
2604 /* This is not "cancel", but cancel_charge does all we need. */
2605 __mem_cgroup_cancel_charge(from
, nr_pages
);
2607 /* caller should have done css_get */
2608 pc
->mem_cgroup
= to
;
2609 mem_cgroup_charge_statistics(to
, PageCgroupCache(pc
), nr_pages
);
2611 * We charges against "to" which may not have any tasks. Then, "to"
2612 * can be under rmdir(). But in current implementation, caller of
2613 * this function is just force_empty() and move charge, so it's
2614 * guaranteed that "to" is never removed. So, we don't check rmdir
2617 move_unlock_page_cgroup(pc
, &flags
);
2620 unlock_page_cgroup(pc
);
2624 memcg_check_events(to
, page
);
2625 memcg_check_events(from
, page
);
2631 * move charges to its parent.
2634 static int mem_cgroup_move_parent(struct page
*page
,
2635 struct page_cgroup
*pc
,
2636 struct mem_cgroup
*child
,
2639 struct cgroup
*cg
= child
->css
.cgroup
;
2640 struct cgroup
*pcg
= cg
->parent
;
2641 struct mem_cgroup
*parent
;
2642 unsigned int nr_pages
;
2643 unsigned long uninitialized_var(flags
);
2651 if (!get_page_unless_zero(page
))
2653 if (isolate_lru_page(page
))
2656 nr_pages
= hpage_nr_pages(page
);
2658 parent
= mem_cgroup_from_cont(pcg
);
2659 ret
= __mem_cgroup_try_charge(NULL
, gfp_mask
, nr_pages
, &parent
, false);
2664 flags
= compound_lock_irqsave(page
);
2666 ret
= mem_cgroup_move_account(page
, nr_pages
, pc
, child
, parent
, true);
2668 __mem_cgroup_cancel_charge(parent
, nr_pages
);
2671 compound_unlock_irqrestore(page
, flags
);
2673 putback_lru_page(page
);
2681 * Charge the memory controller for page usage.
2683 * 0 if the charge was successful
2684 * < 0 if the cgroup is over its limit
2686 static int mem_cgroup_charge_common(struct page
*page
, struct mm_struct
*mm
,
2687 gfp_t gfp_mask
, enum charge_type ctype
)
2689 struct mem_cgroup
*mem
= NULL
;
2690 unsigned int nr_pages
= 1;
2691 struct page_cgroup
*pc
;
2695 if (PageTransHuge(page
)) {
2696 nr_pages
<<= compound_order(page
);
2697 VM_BUG_ON(!PageTransHuge(page
));
2699 * Never OOM-kill a process for a huge page. The
2700 * fault handler will fall back to regular pages.
2705 pc
= lookup_page_cgroup(page
);
2706 BUG_ON(!pc
); /* XXX: remove this and move pc lookup into commit */
2708 ret
= __mem_cgroup_try_charge(mm
, gfp_mask
, nr_pages
, &mem
, oom
);
2712 __mem_cgroup_commit_charge(mem
, page
, nr_pages
, pc
, ctype
);
2716 int mem_cgroup_newpage_charge(struct page
*page
,
2717 struct mm_struct
*mm
, gfp_t gfp_mask
)
2719 if (mem_cgroup_disabled())
2722 * If already mapped, we don't have to account.
2723 * If page cache, page->mapping has address_space.
2724 * But page->mapping may have out-of-use anon_vma pointer,
2725 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2728 if (page_mapped(page
) || (page
->mapping
&& !PageAnon(page
)))
2732 return mem_cgroup_charge_common(page
, mm
, gfp_mask
,
2733 MEM_CGROUP_CHARGE_TYPE_MAPPED
);
2737 __mem_cgroup_commit_charge_swapin(struct page
*page
, struct mem_cgroup
*ptr
,
2738 enum charge_type ctype
);
2741 __mem_cgroup_commit_charge_lrucare(struct page
*page
, struct mem_cgroup
*mem
,
2742 enum charge_type ctype
)
2744 struct page_cgroup
*pc
= lookup_page_cgroup(page
);
2746 * In some case, SwapCache, FUSE(splice_buf->radixtree), the page
2747 * is already on LRU. It means the page may on some other page_cgroup's
2748 * LRU. Take care of it.
2750 mem_cgroup_lru_del_before_commit(page
);
2751 __mem_cgroup_commit_charge(mem
, page
, 1, pc
, ctype
);
2752 mem_cgroup_lru_add_after_commit(page
);
2756 int mem_cgroup_cache_charge(struct page
*page
, struct mm_struct
*mm
,
2759 struct mem_cgroup
*mem
= NULL
;
2762 if (mem_cgroup_disabled())
2764 if (PageCompound(page
))
2767 * Corner case handling. This is called from add_to_page_cache()
2768 * in usual. But some FS (shmem) precharges this page before calling it
2769 * and call add_to_page_cache() with GFP_NOWAIT.
2771 * For GFP_NOWAIT case, the page may be pre-charged before calling
2772 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
2773 * charge twice. (It works but has to pay a bit larger cost.)
2774 * And when the page is SwapCache, it should take swap information
2775 * into account. This is under lock_page() now.
2777 if (!(gfp_mask
& __GFP_WAIT
)) {
2778 struct page_cgroup
*pc
;
2780 pc
= lookup_page_cgroup(page
);
2783 lock_page_cgroup(pc
);
2784 if (PageCgroupUsed(pc
)) {
2785 unlock_page_cgroup(pc
);
2788 unlock_page_cgroup(pc
);
2794 if (page_is_file_cache(page
)) {
2795 ret
= __mem_cgroup_try_charge(mm
, gfp_mask
, 1, &mem
, true);
2800 * FUSE reuses pages without going through the final
2801 * put that would remove them from the LRU list, make
2802 * sure that they get relinked properly.
2804 __mem_cgroup_commit_charge_lrucare(page
, mem
,
2805 MEM_CGROUP_CHARGE_TYPE_CACHE
);
2809 if (PageSwapCache(page
)) {
2810 ret
= mem_cgroup_try_charge_swapin(mm
, page
, gfp_mask
, &mem
);
2812 __mem_cgroup_commit_charge_swapin(page
, mem
,
2813 MEM_CGROUP_CHARGE_TYPE_SHMEM
);
2815 ret
= mem_cgroup_charge_common(page
, mm
, gfp_mask
,
2816 MEM_CGROUP_CHARGE_TYPE_SHMEM
);
2822 * While swap-in, try_charge -> commit or cancel, the page is locked.
2823 * And when try_charge() successfully returns, one refcnt to memcg without
2824 * struct page_cgroup is acquired. This refcnt will be consumed by
2825 * "commit()" or removed by "cancel()"
2827 int mem_cgroup_try_charge_swapin(struct mm_struct
*mm
,
2829 gfp_t mask
, struct mem_cgroup
**ptr
)
2831 struct mem_cgroup
*mem
;
2836 if (mem_cgroup_disabled())
2839 if (!do_swap_account
)
2842 * A racing thread's fault, or swapoff, may have already updated
2843 * the pte, and even removed page from swap cache: in those cases
2844 * do_swap_page()'s pte_same() test will fail; but there's also a
2845 * KSM case which does need to charge the page.
2847 if (!PageSwapCache(page
))
2849 mem
= try_get_mem_cgroup_from_page(page
);
2853 ret
= __mem_cgroup_try_charge(NULL
, mask
, 1, ptr
, true);
2859 return __mem_cgroup_try_charge(mm
, mask
, 1, ptr
, true);
2863 __mem_cgroup_commit_charge_swapin(struct page
*page
, struct mem_cgroup
*ptr
,
2864 enum charge_type ctype
)
2866 if (mem_cgroup_disabled())
2870 cgroup_exclude_rmdir(&ptr
->css
);
2872 __mem_cgroup_commit_charge_lrucare(page
, ptr
, ctype
);
2874 * Now swap is on-memory. This means this page may be
2875 * counted both as mem and swap....double count.
2876 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2877 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2878 * may call delete_from_swap_cache() before reach here.
2880 if (do_swap_account
&& PageSwapCache(page
)) {
2881 swp_entry_t ent
= {.val
= page_private(page
)};
2883 struct mem_cgroup
*memcg
;
2885 id
= swap_cgroup_record(ent
, 0);
2887 memcg
= mem_cgroup_lookup(id
);
2890 * This recorded memcg can be obsolete one. So, avoid
2891 * calling css_tryget
2893 if (!mem_cgroup_is_root(memcg
))
2894 res_counter_uncharge(&memcg
->memsw
, PAGE_SIZE
);
2895 mem_cgroup_swap_statistics(memcg
, false);
2896 mem_cgroup_put(memcg
);
2901 * At swapin, we may charge account against cgroup which has no tasks.
2902 * So, rmdir()->pre_destroy() can be called while we do this charge.
2903 * In that case, we need to call pre_destroy() again. check it here.
2905 cgroup_release_and_wakeup_rmdir(&ptr
->css
);
2908 void mem_cgroup_commit_charge_swapin(struct page
*page
, struct mem_cgroup
*ptr
)
2910 __mem_cgroup_commit_charge_swapin(page
, ptr
,
2911 MEM_CGROUP_CHARGE_TYPE_MAPPED
);
2914 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup
*mem
)
2916 if (mem_cgroup_disabled())
2920 __mem_cgroup_cancel_charge(mem
, 1);
2923 static void mem_cgroup_do_uncharge(struct mem_cgroup
*mem
,
2924 unsigned int nr_pages
,
2925 const enum charge_type ctype
)
2927 struct memcg_batch_info
*batch
= NULL
;
2928 bool uncharge_memsw
= true;
2930 /* If swapout, usage of swap doesn't decrease */
2931 if (!do_swap_account
|| ctype
== MEM_CGROUP_CHARGE_TYPE_SWAPOUT
)
2932 uncharge_memsw
= false;
2934 batch
= ¤t
->memcg_batch
;
2936 * In usual, we do css_get() when we remember memcg pointer.
2937 * But in this case, we keep res->usage until end of a series of
2938 * uncharges. Then, it's ok to ignore memcg's refcnt.
2943 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
2944 * In those cases, all pages freed continuously can be expected to be in
2945 * the same cgroup and we have chance to coalesce uncharges.
2946 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2947 * because we want to do uncharge as soon as possible.
2950 if (!batch
->do_batch
|| test_thread_flag(TIF_MEMDIE
))
2951 goto direct_uncharge
;
2954 goto direct_uncharge
;
2957 * In typical case, batch->memcg == mem. This means we can
2958 * merge a series of uncharges to an uncharge of res_counter.
2959 * If not, we uncharge res_counter ony by one.
2961 if (batch
->memcg
!= mem
)
2962 goto direct_uncharge
;
2963 /* remember freed charge and uncharge it later */
2966 batch
->memsw_nr_pages
++;
2969 res_counter_uncharge(&mem
->res
, nr_pages
* PAGE_SIZE
);
2971 res_counter_uncharge(&mem
->memsw
, nr_pages
* PAGE_SIZE
);
2972 if (unlikely(batch
->memcg
!= mem
))
2973 memcg_oom_recover(mem
);
2978 * uncharge if !page_mapped(page)
2980 static struct mem_cgroup
*
2981 __mem_cgroup_uncharge_common(struct page
*page
, enum charge_type ctype
)
2983 struct mem_cgroup
*mem
= NULL
;
2984 unsigned int nr_pages
= 1;
2985 struct page_cgroup
*pc
;
2987 if (mem_cgroup_disabled())
2990 if (PageSwapCache(page
))
2993 if (PageTransHuge(page
)) {
2994 nr_pages
<<= compound_order(page
);
2995 VM_BUG_ON(!PageTransHuge(page
));
2998 * Check if our page_cgroup is valid
3000 pc
= lookup_page_cgroup(page
);
3001 if (unlikely(!pc
|| !PageCgroupUsed(pc
)))
3004 lock_page_cgroup(pc
);
3006 mem
= pc
->mem_cgroup
;
3008 if (!PageCgroupUsed(pc
))
3012 case MEM_CGROUP_CHARGE_TYPE_MAPPED
:
3013 case MEM_CGROUP_CHARGE_TYPE_DROP
:
3014 /* See mem_cgroup_prepare_migration() */
3015 if (page_mapped(page
) || PageCgroupMigration(pc
))
3018 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT
:
3019 if (!PageAnon(page
)) { /* Shared memory */
3020 if (page
->mapping
&& !page_is_file_cache(page
))
3022 } else if (page_mapped(page
)) /* Anon */
3029 mem_cgroup_charge_statistics(mem
, PageCgroupCache(pc
), -nr_pages
);
3031 ClearPageCgroupUsed(pc
);
3033 * pc->mem_cgroup is not cleared here. It will be accessed when it's
3034 * freed from LRU. This is safe because uncharged page is expected not
3035 * to be reused (freed soon). Exception is SwapCache, it's handled by
3036 * special functions.
3039 unlock_page_cgroup(pc
);
3041 * even after unlock, we have mem->res.usage here and this memcg
3042 * will never be freed.
3044 memcg_check_events(mem
, page
);
3045 if (do_swap_account
&& ctype
== MEM_CGROUP_CHARGE_TYPE_SWAPOUT
) {
3046 mem_cgroup_swap_statistics(mem
, true);
3047 mem_cgroup_get(mem
);
3049 if (!mem_cgroup_is_root(mem
))
3050 mem_cgroup_do_uncharge(mem
, nr_pages
, ctype
);
3055 unlock_page_cgroup(pc
);
3059 void mem_cgroup_uncharge_page(struct page
*page
)
3062 if (page_mapped(page
))
3064 if (page
->mapping
&& !PageAnon(page
))
3066 __mem_cgroup_uncharge_common(page
, MEM_CGROUP_CHARGE_TYPE_MAPPED
);
3069 void mem_cgroup_uncharge_cache_page(struct page
*page
)
3071 VM_BUG_ON(page_mapped(page
));
3072 VM_BUG_ON(page
->mapping
);
3073 __mem_cgroup_uncharge_common(page
, MEM_CGROUP_CHARGE_TYPE_CACHE
);
3077 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3078 * In that cases, pages are freed continuously and we can expect pages
3079 * are in the same memcg. All these calls itself limits the number of
3080 * pages freed at once, then uncharge_start/end() is called properly.
3081 * This may be called prural(2) times in a context,
3084 void mem_cgroup_uncharge_start(void)
3086 current
->memcg_batch
.do_batch
++;
3087 /* We can do nest. */
3088 if (current
->memcg_batch
.do_batch
== 1) {
3089 current
->memcg_batch
.memcg
= NULL
;
3090 current
->memcg_batch
.nr_pages
= 0;
3091 current
->memcg_batch
.memsw_nr_pages
= 0;
3095 void mem_cgroup_uncharge_end(void)
3097 struct memcg_batch_info
*batch
= ¤t
->memcg_batch
;
3099 if (!batch
->do_batch
)
3103 if (batch
->do_batch
) /* If stacked, do nothing. */
3109 * This "batch->memcg" is valid without any css_get/put etc...
3110 * bacause we hide charges behind us.
3112 if (batch
->nr_pages
)
3113 res_counter_uncharge(&batch
->memcg
->res
,
3114 batch
->nr_pages
* PAGE_SIZE
);
3115 if (batch
->memsw_nr_pages
)
3116 res_counter_uncharge(&batch
->memcg
->memsw
,
3117 batch
->memsw_nr_pages
* PAGE_SIZE
);
3118 memcg_oom_recover(batch
->memcg
);
3119 /* forget this pointer (for sanity check) */
3120 batch
->memcg
= NULL
;
3125 * called after __delete_from_swap_cache() and drop "page" account.
3126 * memcg information is recorded to swap_cgroup of "ent"
3129 mem_cgroup_uncharge_swapcache(struct page
*page
, swp_entry_t ent
, bool swapout
)
3131 struct mem_cgroup
*memcg
;
3132 int ctype
= MEM_CGROUP_CHARGE_TYPE_SWAPOUT
;
3134 if (!swapout
) /* this was a swap cache but the swap is unused ! */
3135 ctype
= MEM_CGROUP_CHARGE_TYPE_DROP
;
3137 memcg
= __mem_cgroup_uncharge_common(page
, ctype
);
3140 * record memcg information, if swapout && memcg != NULL,
3141 * mem_cgroup_get() was called in uncharge().
3143 if (do_swap_account
&& swapout
&& memcg
)
3144 swap_cgroup_record(ent
, css_id(&memcg
->css
));
3148 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3150 * called from swap_entry_free(). remove record in swap_cgroup and
3151 * uncharge "memsw" account.
3153 void mem_cgroup_uncharge_swap(swp_entry_t ent
)
3155 struct mem_cgroup
*memcg
;
3158 if (!do_swap_account
)
3161 id
= swap_cgroup_record(ent
, 0);
3163 memcg
= mem_cgroup_lookup(id
);
3166 * We uncharge this because swap is freed.
3167 * This memcg can be obsolete one. We avoid calling css_tryget
3169 if (!mem_cgroup_is_root(memcg
))
3170 res_counter_uncharge(&memcg
->memsw
, PAGE_SIZE
);
3171 mem_cgroup_swap_statistics(memcg
, false);
3172 mem_cgroup_put(memcg
);
3178 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3179 * @entry: swap entry to be moved
3180 * @from: mem_cgroup which the entry is moved from
3181 * @to: mem_cgroup which the entry is moved to
3182 * @need_fixup: whether we should fixup res_counters and refcounts.
3184 * It succeeds only when the swap_cgroup's record for this entry is the same
3185 * as the mem_cgroup's id of @from.
3187 * Returns 0 on success, -EINVAL on failure.
3189 * The caller must have charged to @to, IOW, called res_counter_charge() about
3190 * both res and memsw, and called css_get().
3192 static int mem_cgroup_move_swap_account(swp_entry_t entry
,
3193 struct mem_cgroup
*from
, struct mem_cgroup
*to
, bool need_fixup
)
3195 unsigned short old_id
, new_id
;
3197 old_id
= css_id(&from
->css
);
3198 new_id
= css_id(&to
->css
);
3200 if (swap_cgroup_cmpxchg(entry
, old_id
, new_id
) == old_id
) {
3201 mem_cgroup_swap_statistics(from
, false);
3202 mem_cgroup_swap_statistics(to
, true);
3204 * This function is only called from task migration context now.
3205 * It postpones res_counter and refcount handling till the end
3206 * of task migration(mem_cgroup_clear_mc()) for performance
3207 * improvement. But we cannot postpone mem_cgroup_get(to)
3208 * because if the process that has been moved to @to does
3209 * swap-in, the refcount of @to might be decreased to 0.
3213 if (!mem_cgroup_is_root(from
))
3214 res_counter_uncharge(&from
->memsw
, PAGE_SIZE
);
3215 mem_cgroup_put(from
);
3217 * we charged both to->res and to->memsw, so we should
3220 if (!mem_cgroup_is_root(to
))
3221 res_counter_uncharge(&to
->res
, PAGE_SIZE
);
3228 static inline int mem_cgroup_move_swap_account(swp_entry_t entry
,
3229 struct mem_cgroup
*from
, struct mem_cgroup
*to
, bool need_fixup
)
3236 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3239 int mem_cgroup_prepare_migration(struct page
*page
,
3240 struct page
*newpage
, struct mem_cgroup
**ptr
, gfp_t gfp_mask
)
3242 struct mem_cgroup
*mem
= NULL
;
3243 struct page_cgroup
*pc
;
3244 enum charge_type ctype
;
3249 VM_BUG_ON(PageTransHuge(page
));
3250 if (mem_cgroup_disabled())
3253 pc
= lookup_page_cgroup(page
);
3254 lock_page_cgroup(pc
);
3255 if (PageCgroupUsed(pc
)) {
3256 mem
= pc
->mem_cgroup
;
3259 * At migrating an anonymous page, its mapcount goes down
3260 * to 0 and uncharge() will be called. But, even if it's fully
3261 * unmapped, migration may fail and this page has to be
3262 * charged again. We set MIGRATION flag here and delay uncharge
3263 * until end_migration() is called
3265 * Corner Case Thinking
3267 * When the old page was mapped as Anon and it's unmap-and-freed
3268 * while migration was ongoing.
3269 * If unmap finds the old page, uncharge() of it will be delayed
3270 * until end_migration(). If unmap finds a new page, it's
3271 * uncharged when it make mapcount to be 1->0. If unmap code
3272 * finds swap_migration_entry, the new page will not be mapped
3273 * and end_migration() will find it(mapcount==0).
3276 * When the old page was mapped but migraion fails, the kernel
3277 * remaps it. A charge for it is kept by MIGRATION flag even
3278 * if mapcount goes down to 0. We can do remap successfully
3279 * without charging it again.
3282 * The "old" page is under lock_page() until the end of
3283 * migration, so, the old page itself will not be swapped-out.
3284 * If the new page is swapped out before end_migraton, our
3285 * hook to usual swap-out path will catch the event.
3288 SetPageCgroupMigration(pc
);
3290 unlock_page_cgroup(pc
);
3292 * If the page is not charged at this point,
3299 ret
= __mem_cgroup_try_charge(NULL
, gfp_mask
, 1, ptr
, false);
3300 css_put(&mem
->css
);/* drop extra refcnt */
3301 if (ret
|| *ptr
== NULL
) {
3302 if (PageAnon(page
)) {
3303 lock_page_cgroup(pc
);
3304 ClearPageCgroupMigration(pc
);
3305 unlock_page_cgroup(pc
);
3307 * The old page may be fully unmapped while we kept it.
3309 mem_cgroup_uncharge_page(page
);
3314 * We charge new page before it's used/mapped. So, even if unlock_page()
3315 * is called before end_migration, we can catch all events on this new
3316 * page. In the case new page is migrated but not remapped, new page's
3317 * mapcount will be finally 0 and we call uncharge in end_migration().
3319 pc
= lookup_page_cgroup(newpage
);
3321 ctype
= MEM_CGROUP_CHARGE_TYPE_MAPPED
;
3322 else if (page_is_file_cache(page
))
3323 ctype
= MEM_CGROUP_CHARGE_TYPE_CACHE
;
3325 ctype
= MEM_CGROUP_CHARGE_TYPE_SHMEM
;
3326 __mem_cgroup_commit_charge(mem
, page
, 1, pc
, ctype
);
3330 /* remove redundant charge if migration failed*/
3331 void mem_cgroup_end_migration(struct mem_cgroup
*mem
,
3332 struct page
*oldpage
, struct page
*newpage
, bool migration_ok
)
3334 struct page
*used
, *unused
;
3335 struct page_cgroup
*pc
;
3339 /* blocks rmdir() */
3340 cgroup_exclude_rmdir(&mem
->css
);
3341 if (!migration_ok
) {
3349 * We disallowed uncharge of pages under migration because mapcount
3350 * of the page goes down to zero, temporarly.
3351 * Clear the flag and check the page should be charged.
3353 pc
= lookup_page_cgroup(oldpage
);
3354 lock_page_cgroup(pc
);
3355 ClearPageCgroupMigration(pc
);
3356 unlock_page_cgroup(pc
);
3358 __mem_cgroup_uncharge_common(unused
, MEM_CGROUP_CHARGE_TYPE_FORCE
);
3361 * If a page is a file cache, radix-tree replacement is very atomic
3362 * and we can skip this check. When it was an Anon page, its mapcount
3363 * goes down to 0. But because we added MIGRATION flage, it's not
3364 * uncharged yet. There are several case but page->mapcount check
3365 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3366 * check. (see prepare_charge() also)
3369 mem_cgroup_uncharge_page(used
);
3371 * At migration, we may charge account against cgroup which has no
3373 * So, rmdir()->pre_destroy() can be called while we do this charge.
3374 * In that case, we need to call pre_destroy() again. check it here.
3376 cgroup_release_and_wakeup_rmdir(&mem
->css
);
3380 * A call to try to shrink memory usage on charge failure at shmem's swapin.
3381 * Calling hierarchical_reclaim is not enough because we should update
3382 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
3383 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
3384 * not from the memcg which this page would be charged to.
3385 * try_charge_swapin does all of these works properly.
3387 int mem_cgroup_shmem_charge_fallback(struct page
*page
,
3388 struct mm_struct
*mm
,
3391 struct mem_cgroup
*mem
;
3394 if (mem_cgroup_disabled())
3397 ret
= mem_cgroup_try_charge_swapin(mm
, page
, gfp_mask
, &mem
);
3399 mem_cgroup_cancel_charge_swapin(mem
); /* it does !mem check */
3404 #ifdef CONFIG_DEBUG_VM
3405 static struct page_cgroup
*lookup_page_cgroup_used(struct page
*page
)
3407 struct page_cgroup
*pc
;
3409 pc
= lookup_page_cgroup(page
);
3410 if (likely(pc
) && PageCgroupUsed(pc
))
3415 bool mem_cgroup_bad_page_check(struct page
*page
)
3417 if (mem_cgroup_disabled())
3420 return lookup_page_cgroup_used(page
) != NULL
;
3423 void mem_cgroup_print_bad_page(struct page
*page
)
3425 struct page_cgroup
*pc
;
3427 pc
= lookup_page_cgroup_used(page
);
3432 printk(KERN_ALERT
"pc:%p pc->flags:%lx pc->mem_cgroup:%p",
3433 pc
, pc
->flags
, pc
->mem_cgroup
);
3435 path
= kmalloc(PATH_MAX
, GFP_KERNEL
);
3438 ret
= cgroup_path(pc
->mem_cgroup
->css
.cgroup
,
3443 printk(KERN_CONT
"(%s)\n",
3444 (ret
< 0) ? "cannot get the path" : path
);
3450 static DEFINE_MUTEX(set_limit_mutex
);
3452 static int mem_cgroup_resize_limit(struct mem_cgroup
*memcg
,
3453 unsigned long long val
)
3456 u64 memswlimit
, memlimit
;
3458 int children
= mem_cgroup_count_children(memcg
);
3459 u64 curusage
, oldusage
;
3463 * For keeping hierarchical_reclaim simple, how long we should retry
3464 * is depends on callers. We set our retry-count to be function
3465 * of # of children which we should visit in this loop.
3467 retry_count
= MEM_CGROUP_RECLAIM_RETRIES
* children
;
3469 oldusage
= res_counter_read_u64(&memcg
->res
, RES_USAGE
);
3472 while (retry_count
) {
3473 if (signal_pending(current
)) {
3478 * Rather than hide all in some function, I do this in
3479 * open coded manner. You see what this really does.
3480 * We have to guarantee mem->res.limit < mem->memsw.limit.
3482 mutex_lock(&set_limit_mutex
);
3483 memswlimit
= res_counter_read_u64(&memcg
->memsw
, RES_LIMIT
);
3484 if (memswlimit
< val
) {
3486 mutex_unlock(&set_limit_mutex
);
3490 memlimit
= res_counter_read_u64(&memcg
->res
, RES_LIMIT
);
3494 ret
= res_counter_set_limit(&memcg
->res
, val
);
3496 if (memswlimit
== val
)
3497 memcg
->memsw_is_minimum
= true;
3499 memcg
->memsw_is_minimum
= false;
3501 mutex_unlock(&set_limit_mutex
);
3506 mem_cgroup_hierarchical_reclaim(memcg
, NULL
, GFP_KERNEL
,
3507 MEM_CGROUP_RECLAIM_SHRINK
,
3509 curusage
= res_counter_read_u64(&memcg
->res
, RES_USAGE
);
3510 /* Usage is reduced ? */
3511 if (curusage
>= oldusage
)
3514 oldusage
= curusage
;
3516 if (!ret
&& enlarge
)
3517 memcg_oom_recover(memcg
);
3522 static int mem_cgroup_resize_memsw_limit(struct mem_cgroup
*memcg
,
3523 unsigned long long val
)
3526 u64 memlimit
, memswlimit
, oldusage
, curusage
;
3527 int children
= mem_cgroup_count_children(memcg
);
3531 /* see mem_cgroup_resize_res_limit */
3532 retry_count
= children
* MEM_CGROUP_RECLAIM_RETRIES
;
3533 oldusage
= res_counter_read_u64(&memcg
->memsw
, RES_USAGE
);
3534 while (retry_count
) {
3535 if (signal_pending(current
)) {
3540 * Rather than hide all in some function, I do this in
3541 * open coded manner. You see what this really does.
3542 * We have to guarantee mem->res.limit < mem->memsw.limit.
3544 mutex_lock(&set_limit_mutex
);
3545 memlimit
= res_counter_read_u64(&memcg
->res
, RES_LIMIT
);
3546 if (memlimit
> val
) {
3548 mutex_unlock(&set_limit_mutex
);
3551 memswlimit
= res_counter_read_u64(&memcg
->memsw
, RES_LIMIT
);
3552 if (memswlimit
< val
)
3554 ret
= res_counter_set_limit(&memcg
->memsw
, val
);
3556 if (memlimit
== val
)
3557 memcg
->memsw_is_minimum
= true;
3559 memcg
->memsw_is_minimum
= false;
3561 mutex_unlock(&set_limit_mutex
);
3566 mem_cgroup_hierarchical_reclaim(memcg
, NULL
, GFP_KERNEL
,
3567 MEM_CGROUP_RECLAIM_NOSWAP
|
3568 MEM_CGROUP_RECLAIM_SHRINK
,
3570 curusage
= res_counter_read_u64(&memcg
->memsw
, RES_USAGE
);
3571 /* Usage is reduced ? */
3572 if (curusage
>= oldusage
)
3575 oldusage
= curusage
;
3577 if (!ret
&& enlarge
)
3578 memcg_oom_recover(memcg
);
3582 unsigned long mem_cgroup_soft_limit_reclaim(struct zone
*zone
, int order
,
3584 unsigned long *total_scanned
)
3586 unsigned long nr_reclaimed
= 0;
3587 struct mem_cgroup_per_zone
*mz
, *next_mz
= NULL
;
3588 unsigned long reclaimed
;
3590 struct mem_cgroup_tree_per_zone
*mctz
;
3591 unsigned long long excess
;
3592 unsigned long nr_scanned
;
3597 mctz
= soft_limit_tree_node_zone(zone_to_nid(zone
), zone_idx(zone
));
3599 * This loop can run a while, specially if mem_cgroup's continuously
3600 * keep exceeding their soft limit and putting the system under
3607 mz
= mem_cgroup_largest_soft_limit_node(mctz
);
3612 reclaimed
= mem_cgroup_hierarchical_reclaim(mz
->mem
, zone
,
3614 MEM_CGROUP_RECLAIM_SOFT
,
3616 nr_reclaimed
+= reclaimed
;
3617 *total_scanned
+= nr_scanned
;
3618 spin_lock(&mctz
->lock
);
3621 * If we failed to reclaim anything from this memory cgroup
3622 * it is time to move on to the next cgroup
3628 * Loop until we find yet another one.
3630 * By the time we get the soft_limit lock
3631 * again, someone might have aded the
3632 * group back on the RB tree. Iterate to
3633 * make sure we get a different mem.
3634 * mem_cgroup_largest_soft_limit_node returns
3635 * NULL if no other cgroup is present on
3639 __mem_cgroup_largest_soft_limit_node(mctz
);
3641 css_put(&next_mz
->mem
->css
);
3642 else /* next_mz == NULL or other memcg */
3646 __mem_cgroup_remove_exceeded(mz
->mem
, mz
, mctz
);
3647 excess
= res_counter_soft_limit_excess(&mz
->mem
->res
);
3649 * One school of thought says that we should not add
3650 * back the node to the tree if reclaim returns 0.
3651 * But our reclaim could return 0, simply because due
3652 * to priority we are exposing a smaller subset of
3653 * memory to reclaim from. Consider this as a longer
3656 /* If excess == 0, no tree ops */
3657 __mem_cgroup_insert_exceeded(mz
->mem
, mz
, mctz
, excess
);
3658 spin_unlock(&mctz
->lock
);
3659 css_put(&mz
->mem
->css
);
3662 * Could not reclaim anything and there are no more
3663 * mem cgroups to try or we seem to be looping without
3664 * reclaiming anything.
3666 if (!nr_reclaimed
&&
3668 loop
> MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS
))
3670 } while (!nr_reclaimed
);
3672 css_put(&next_mz
->mem
->css
);
3673 return nr_reclaimed
;
3677 * This routine traverse page_cgroup in given list and drop them all.
3678 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
3680 static int mem_cgroup_force_empty_list(struct mem_cgroup
*mem
,
3681 int node
, int zid
, enum lru_list lru
)
3684 struct mem_cgroup_per_zone
*mz
;
3685 struct page_cgroup
*pc
, *busy
;
3686 unsigned long flags
, loop
;
3687 struct list_head
*list
;
3690 zone
= &NODE_DATA(node
)->node_zones
[zid
];
3691 mz
= mem_cgroup_zoneinfo(mem
, node
, zid
);
3692 list
= &mz
->lists
[lru
];
3694 loop
= MEM_CGROUP_ZSTAT(mz
, lru
);
3695 /* give some margin against EBUSY etc...*/
3702 spin_lock_irqsave(&zone
->lru_lock
, flags
);
3703 if (list_empty(list
)) {
3704 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
3707 pc
= list_entry(list
->prev
, struct page_cgroup
, lru
);
3709 list_move(&pc
->lru
, list
);
3711 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
3714 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
3716 page
= lookup_cgroup_page(pc
);
3718 ret
= mem_cgroup_move_parent(page
, pc
, mem
, GFP_KERNEL
);
3722 if (ret
== -EBUSY
|| ret
== -EINVAL
) {
3723 /* found lock contention or "pc" is obsolete. */
3730 if (!ret
&& !list_empty(list
))
3736 * make mem_cgroup's charge to be 0 if there is no task.
3737 * This enables deleting this mem_cgroup.
3739 static int mem_cgroup_force_empty(struct mem_cgroup
*mem
, bool free_all
)
3742 int node
, zid
, shrink
;
3743 int nr_retries
= MEM_CGROUP_RECLAIM_RETRIES
;
3744 struct cgroup
*cgrp
= mem
->css
.cgroup
;
3749 /* should free all ? */
3755 if (cgroup_task_count(cgrp
) || !list_empty(&cgrp
->children
))
3758 if (signal_pending(current
))
3760 /* This is for making all *used* pages to be on LRU. */
3761 lru_add_drain_all();
3762 drain_all_stock_sync();
3764 mem_cgroup_start_move(mem
);
3765 for_each_node_state(node
, N_HIGH_MEMORY
) {
3766 for (zid
= 0; !ret
&& zid
< MAX_NR_ZONES
; zid
++) {
3769 ret
= mem_cgroup_force_empty_list(mem
,
3778 mem_cgroup_end_move(mem
);
3779 memcg_oom_recover(mem
);
3780 /* it seems parent cgroup doesn't have enough mem */
3784 /* "ret" should also be checked to ensure all lists are empty. */
3785 } while (mem
->res
.usage
> 0 || ret
);
3791 /* returns EBUSY if there is a task or if we come here twice. */
3792 if (cgroup_task_count(cgrp
) || !list_empty(&cgrp
->children
) || shrink
) {
3796 /* we call try-to-free pages for make this cgroup empty */
3797 lru_add_drain_all();
3798 /* try to free all pages in this cgroup */
3800 while (nr_retries
&& mem
->res
.usage
> 0) {
3803 if (signal_pending(current
)) {
3807 progress
= try_to_free_mem_cgroup_pages(mem
, GFP_KERNEL
,
3808 false, get_swappiness(mem
));
3811 /* maybe some writeback is necessary */
3812 congestion_wait(BLK_RW_ASYNC
, HZ
/10);
3817 /* try move_account...there may be some *locked* pages. */
3821 int mem_cgroup_force_empty_write(struct cgroup
*cont
, unsigned int event
)
3823 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont
), true);
3827 static u64
mem_cgroup_hierarchy_read(struct cgroup
*cont
, struct cftype
*cft
)
3829 return mem_cgroup_from_cont(cont
)->use_hierarchy
;
3832 static int mem_cgroup_hierarchy_write(struct cgroup
*cont
, struct cftype
*cft
,
3836 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cont
);
3837 struct cgroup
*parent
= cont
->parent
;
3838 struct mem_cgroup
*parent_mem
= NULL
;
3841 parent_mem
= mem_cgroup_from_cont(parent
);
3845 * If parent's use_hierarchy is set, we can't make any modifications
3846 * in the child subtrees. If it is unset, then the change can
3847 * occur, provided the current cgroup has no children.
3849 * For the root cgroup, parent_mem is NULL, we allow value to be
3850 * set if there are no children.
3852 if ((!parent_mem
|| !parent_mem
->use_hierarchy
) &&
3853 (val
== 1 || val
== 0)) {
3854 if (list_empty(&cont
->children
))
3855 mem
->use_hierarchy
= val
;
3866 static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup
*mem
,
3867 enum mem_cgroup_stat_index idx
)
3869 struct mem_cgroup
*iter
;
3872 /* Per-cpu values can be negative, use a signed accumulator */
3873 for_each_mem_cgroup_tree(iter
, mem
)
3874 val
+= mem_cgroup_read_stat(iter
, idx
);
3876 if (val
< 0) /* race ? */
3881 static inline u64
mem_cgroup_usage(struct mem_cgroup
*mem
, bool swap
)
3885 if (!mem_cgroup_is_root(mem
)) {
3887 return res_counter_read_u64(&mem
->res
, RES_USAGE
);
3889 return res_counter_read_u64(&mem
->memsw
, RES_USAGE
);
3892 val
= mem_cgroup_recursive_stat(mem
, MEM_CGROUP_STAT_CACHE
);
3893 val
+= mem_cgroup_recursive_stat(mem
, MEM_CGROUP_STAT_RSS
);
3896 val
+= mem_cgroup_recursive_stat(mem
, MEM_CGROUP_STAT_SWAPOUT
);
3898 return val
<< PAGE_SHIFT
;
3901 static u64
mem_cgroup_read(struct cgroup
*cont
, struct cftype
*cft
)
3903 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cont
);
3907 type
= MEMFILE_TYPE(cft
->private);
3908 name
= MEMFILE_ATTR(cft
->private);
3911 if (name
== RES_USAGE
)
3912 val
= mem_cgroup_usage(mem
, false);
3914 val
= res_counter_read_u64(&mem
->res
, name
);
3917 if (name
== RES_USAGE
)
3918 val
= mem_cgroup_usage(mem
, true);
3920 val
= res_counter_read_u64(&mem
->memsw
, name
);
3929 * The user of this function is...
3932 static int mem_cgroup_write(struct cgroup
*cont
, struct cftype
*cft
,
3935 struct mem_cgroup
*memcg
= mem_cgroup_from_cont(cont
);
3937 unsigned long long val
;
3940 type
= MEMFILE_TYPE(cft
->private);
3941 name
= MEMFILE_ATTR(cft
->private);
3944 if (mem_cgroup_is_root(memcg
)) { /* Can't set limit on root */
3948 /* This function does all necessary parse...reuse it */
3949 ret
= res_counter_memparse_write_strategy(buffer
, &val
);
3953 ret
= mem_cgroup_resize_limit(memcg
, val
);
3955 ret
= mem_cgroup_resize_memsw_limit(memcg
, val
);
3957 case RES_SOFT_LIMIT
:
3958 ret
= res_counter_memparse_write_strategy(buffer
, &val
);
3962 * For memsw, soft limits are hard to implement in terms
3963 * of semantics, for now, we support soft limits for
3964 * control without swap
3967 ret
= res_counter_set_soft_limit(&memcg
->res
, val
);
3972 ret
= -EINVAL
; /* should be BUG() ? */
3978 static void memcg_get_hierarchical_limit(struct mem_cgroup
*memcg
,
3979 unsigned long long *mem_limit
, unsigned long long *memsw_limit
)
3981 struct cgroup
*cgroup
;
3982 unsigned long long min_limit
, min_memsw_limit
, tmp
;
3984 min_limit
= res_counter_read_u64(&memcg
->res
, RES_LIMIT
);
3985 min_memsw_limit
= res_counter_read_u64(&memcg
->memsw
, RES_LIMIT
);
3986 cgroup
= memcg
->css
.cgroup
;
3987 if (!memcg
->use_hierarchy
)
3990 while (cgroup
->parent
) {
3991 cgroup
= cgroup
->parent
;
3992 memcg
= mem_cgroup_from_cont(cgroup
);
3993 if (!memcg
->use_hierarchy
)
3995 tmp
= res_counter_read_u64(&memcg
->res
, RES_LIMIT
);
3996 min_limit
= min(min_limit
, tmp
);
3997 tmp
= res_counter_read_u64(&memcg
->memsw
, RES_LIMIT
);
3998 min_memsw_limit
= min(min_memsw_limit
, tmp
);
4001 *mem_limit
= min_limit
;
4002 *memsw_limit
= min_memsw_limit
;
4006 static int mem_cgroup_reset(struct cgroup
*cont
, unsigned int event
)
4008 struct mem_cgroup
*mem
;
4011 mem
= mem_cgroup_from_cont(cont
);
4012 type
= MEMFILE_TYPE(event
);
4013 name
= MEMFILE_ATTR(event
);
4017 res_counter_reset_max(&mem
->res
);
4019 res_counter_reset_max(&mem
->memsw
);
4023 res_counter_reset_failcnt(&mem
->res
);
4025 res_counter_reset_failcnt(&mem
->memsw
);
4032 static u64
mem_cgroup_move_charge_read(struct cgroup
*cgrp
,
4035 return mem_cgroup_from_cont(cgrp
)->move_charge_at_immigrate
;
4039 static int mem_cgroup_move_charge_write(struct cgroup
*cgrp
,
4040 struct cftype
*cft
, u64 val
)
4042 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cgrp
);
4044 if (val
>= (1 << NR_MOVE_TYPE
))
4047 * We check this value several times in both in can_attach() and
4048 * attach(), so we need cgroup lock to prevent this value from being
4052 mem
->move_charge_at_immigrate
= val
;
4058 static int mem_cgroup_move_charge_write(struct cgroup
*cgrp
,
4059 struct cftype
*cft
, u64 val
)
4066 /* For read statistics */
4084 struct mcs_total_stat
{
4085 s64 stat
[NR_MCS_STAT
];
4091 } memcg_stat_strings
[NR_MCS_STAT
] = {
4092 {"cache", "total_cache"},
4093 {"rss", "total_rss"},
4094 {"mapped_file", "total_mapped_file"},
4095 {"pgpgin", "total_pgpgin"},
4096 {"pgpgout", "total_pgpgout"},
4097 {"swap", "total_swap"},
4098 {"pgfault", "total_pgfault"},
4099 {"pgmajfault", "total_pgmajfault"},
4100 {"inactive_anon", "total_inactive_anon"},
4101 {"active_anon", "total_active_anon"},
4102 {"inactive_file", "total_inactive_file"},
4103 {"active_file", "total_active_file"},
4104 {"unevictable", "total_unevictable"}
4109 mem_cgroup_get_local_stat(struct mem_cgroup
*mem
, struct mcs_total_stat
*s
)
4114 val
= mem_cgroup_read_stat(mem
, MEM_CGROUP_STAT_CACHE
);
4115 s
->stat
[MCS_CACHE
] += val
* PAGE_SIZE
;
4116 val
= mem_cgroup_read_stat(mem
, MEM_CGROUP_STAT_RSS
);
4117 s
->stat
[MCS_RSS
] += val
* PAGE_SIZE
;
4118 val
= mem_cgroup_read_stat(mem
, MEM_CGROUP_STAT_FILE_MAPPED
);
4119 s
->stat
[MCS_FILE_MAPPED
] += val
* PAGE_SIZE
;
4120 val
= mem_cgroup_read_events(mem
, MEM_CGROUP_EVENTS_PGPGIN
);
4121 s
->stat
[MCS_PGPGIN
] += val
;
4122 val
= mem_cgroup_read_events(mem
, MEM_CGROUP_EVENTS_PGPGOUT
);
4123 s
->stat
[MCS_PGPGOUT
] += val
;
4124 if (do_swap_account
) {
4125 val
= mem_cgroup_read_stat(mem
, MEM_CGROUP_STAT_SWAPOUT
);
4126 s
->stat
[MCS_SWAP
] += val
* PAGE_SIZE
;
4128 val
= mem_cgroup_read_events(mem
, MEM_CGROUP_EVENTS_PGFAULT
);
4129 s
->stat
[MCS_PGFAULT
] += val
;
4130 val
= mem_cgroup_read_events(mem
, MEM_CGROUP_EVENTS_PGMAJFAULT
);
4131 s
->stat
[MCS_PGMAJFAULT
] += val
;
4134 val
= mem_cgroup_get_local_zonestat(mem
, LRU_INACTIVE_ANON
);
4135 s
->stat
[MCS_INACTIVE_ANON
] += val
* PAGE_SIZE
;
4136 val
= mem_cgroup_get_local_zonestat(mem
, LRU_ACTIVE_ANON
);
4137 s
->stat
[MCS_ACTIVE_ANON
] += val
* PAGE_SIZE
;
4138 val
= mem_cgroup_get_local_zonestat(mem
, LRU_INACTIVE_FILE
);
4139 s
->stat
[MCS_INACTIVE_FILE
] += val
* PAGE_SIZE
;
4140 val
= mem_cgroup_get_local_zonestat(mem
, LRU_ACTIVE_FILE
);
4141 s
->stat
[MCS_ACTIVE_FILE
] += val
* PAGE_SIZE
;
4142 val
= mem_cgroup_get_local_zonestat(mem
, LRU_UNEVICTABLE
);
4143 s
->stat
[MCS_UNEVICTABLE
] += val
* PAGE_SIZE
;
4147 mem_cgroup_get_total_stat(struct mem_cgroup
*mem
, struct mcs_total_stat
*s
)
4149 struct mem_cgroup
*iter
;
4151 for_each_mem_cgroup_tree(iter
, mem
)
4152 mem_cgroup_get_local_stat(iter
, s
);
4156 static int mem_control_numa_stat_show(struct seq_file
*m
, void *arg
)
4159 unsigned long total_nr
, file_nr
, anon_nr
, unevictable_nr
;
4160 unsigned long node_nr
;
4161 struct cgroup
*cont
= m
->private;
4162 struct mem_cgroup
*mem_cont
= mem_cgroup_from_cont(cont
);
4164 total_nr
= mem_cgroup_nr_lru_pages(mem_cont
);
4165 seq_printf(m
, "total=%lu", total_nr
);
4166 for_each_node_state(nid
, N_HIGH_MEMORY
) {
4167 node_nr
= mem_cgroup_node_nr_lru_pages(mem_cont
, nid
);
4168 seq_printf(m
, " N%d=%lu", nid
, node_nr
);
4172 file_nr
= mem_cgroup_nr_file_lru_pages(mem_cont
);
4173 seq_printf(m
, "file=%lu", file_nr
);
4174 for_each_node_state(nid
, N_HIGH_MEMORY
) {
4175 node_nr
= mem_cgroup_node_nr_file_lru_pages(mem_cont
, nid
);
4176 seq_printf(m
, " N%d=%lu", nid
, node_nr
);
4180 anon_nr
= mem_cgroup_nr_anon_lru_pages(mem_cont
);
4181 seq_printf(m
, "anon=%lu", anon_nr
);
4182 for_each_node_state(nid
, N_HIGH_MEMORY
) {
4183 node_nr
= mem_cgroup_node_nr_anon_lru_pages(mem_cont
, nid
);
4184 seq_printf(m
, " N%d=%lu", nid
, node_nr
);
4188 unevictable_nr
= mem_cgroup_nr_unevictable_lru_pages(mem_cont
);
4189 seq_printf(m
, "unevictable=%lu", unevictable_nr
);
4190 for_each_node_state(nid
, N_HIGH_MEMORY
) {
4191 node_nr
= mem_cgroup_node_nr_unevictable_lru_pages(mem_cont
,
4193 seq_printf(m
, " N%d=%lu", nid
, node_nr
);
4198 #endif /* CONFIG_NUMA */
4200 static int mem_control_stat_show(struct cgroup
*cont
, struct cftype
*cft
,
4201 struct cgroup_map_cb
*cb
)
4203 struct mem_cgroup
*mem_cont
= mem_cgroup_from_cont(cont
);
4204 struct mcs_total_stat mystat
;
4207 memset(&mystat
, 0, sizeof(mystat
));
4208 mem_cgroup_get_local_stat(mem_cont
, &mystat
);
4211 for (i
= 0; i
< NR_MCS_STAT
; i
++) {
4212 if (i
== MCS_SWAP
&& !do_swap_account
)
4214 cb
->fill(cb
, memcg_stat_strings
[i
].local_name
, mystat
.stat
[i
]);
4217 /* Hierarchical information */
4219 unsigned long long limit
, memsw_limit
;
4220 memcg_get_hierarchical_limit(mem_cont
, &limit
, &memsw_limit
);
4221 cb
->fill(cb
, "hierarchical_memory_limit", limit
);
4222 if (do_swap_account
)
4223 cb
->fill(cb
, "hierarchical_memsw_limit", memsw_limit
);
4226 memset(&mystat
, 0, sizeof(mystat
));
4227 mem_cgroup_get_total_stat(mem_cont
, &mystat
);
4228 for (i
= 0; i
< NR_MCS_STAT
; i
++) {
4229 if (i
== MCS_SWAP
&& !do_swap_account
)
4231 cb
->fill(cb
, memcg_stat_strings
[i
].total_name
, mystat
.stat
[i
]);
4234 #ifdef CONFIG_DEBUG_VM
4235 cb
->fill(cb
, "inactive_ratio", calc_inactive_ratio(mem_cont
, NULL
));
4239 struct mem_cgroup_per_zone
*mz
;
4240 unsigned long recent_rotated
[2] = {0, 0};
4241 unsigned long recent_scanned
[2] = {0, 0};
4243 for_each_online_node(nid
)
4244 for (zid
= 0; zid
< MAX_NR_ZONES
; zid
++) {
4245 mz
= mem_cgroup_zoneinfo(mem_cont
, nid
, zid
);
4247 recent_rotated
[0] +=
4248 mz
->reclaim_stat
.recent_rotated
[0];
4249 recent_rotated
[1] +=
4250 mz
->reclaim_stat
.recent_rotated
[1];
4251 recent_scanned
[0] +=
4252 mz
->reclaim_stat
.recent_scanned
[0];
4253 recent_scanned
[1] +=
4254 mz
->reclaim_stat
.recent_scanned
[1];
4256 cb
->fill(cb
, "recent_rotated_anon", recent_rotated
[0]);
4257 cb
->fill(cb
, "recent_rotated_file", recent_rotated
[1]);
4258 cb
->fill(cb
, "recent_scanned_anon", recent_scanned
[0]);
4259 cb
->fill(cb
, "recent_scanned_file", recent_scanned
[1]);
4266 static u64
mem_cgroup_swappiness_read(struct cgroup
*cgrp
, struct cftype
*cft
)
4268 struct mem_cgroup
*memcg
= mem_cgroup_from_cont(cgrp
);
4270 return get_swappiness(memcg
);
4273 static int mem_cgroup_swappiness_write(struct cgroup
*cgrp
, struct cftype
*cft
,
4276 struct mem_cgroup
*memcg
= mem_cgroup_from_cont(cgrp
);
4277 struct mem_cgroup
*parent
;
4282 if (cgrp
->parent
== NULL
)
4285 parent
= mem_cgroup_from_cont(cgrp
->parent
);
4289 /* If under hierarchy, only empty-root can set this value */
4290 if ((parent
->use_hierarchy
) ||
4291 (memcg
->use_hierarchy
&& !list_empty(&cgrp
->children
))) {
4296 memcg
->swappiness
= val
;
4303 static void __mem_cgroup_threshold(struct mem_cgroup
*memcg
, bool swap
)
4305 struct mem_cgroup_threshold_ary
*t
;
4311 t
= rcu_dereference(memcg
->thresholds
.primary
);
4313 t
= rcu_dereference(memcg
->memsw_thresholds
.primary
);
4318 usage
= mem_cgroup_usage(memcg
, swap
);
4321 * current_threshold points to threshold just below usage.
4322 * If it's not true, a threshold was crossed after last
4323 * call of __mem_cgroup_threshold().
4325 i
= t
->current_threshold
;
4328 * Iterate backward over array of thresholds starting from
4329 * current_threshold and check if a threshold is crossed.
4330 * If none of thresholds below usage is crossed, we read
4331 * only one element of the array here.
4333 for (; i
>= 0 && unlikely(t
->entries
[i
].threshold
> usage
); i
--)
4334 eventfd_signal(t
->entries
[i
].eventfd
, 1);
4336 /* i = current_threshold + 1 */
4340 * Iterate forward over array of thresholds starting from
4341 * current_threshold+1 and check if a threshold is crossed.
4342 * If none of thresholds above usage is crossed, we read
4343 * only one element of the array here.
4345 for (; i
< t
->size
&& unlikely(t
->entries
[i
].threshold
<= usage
); i
++)
4346 eventfd_signal(t
->entries
[i
].eventfd
, 1);
4348 /* Update current_threshold */
4349 t
->current_threshold
= i
- 1;
4354 static void mem_cgroup_threshold(struct mem_cgroup
*memcg
)
4357 __mem_cgroup_threshold(memcg
, false);
4358 if (do_swap_account
)
4359 __mem_cgroup_threshold(memcg
, true);
4361 memcg
= parent_mem_cgroup(memcg
);
4365 static int compare_thresholds(const void *a
, const void *b
)
4367 const struct mem_cgroup_threshold
*_a
= a
;
4368 const struct mem_cgroup_threshold
*_b
= b
;
4370 return _a
->threshold
- _b
->threshold
;
4373 static int mem_cgroup_oom_notify_cb(struct mem_cgroup
*mem
)
4375 struct mem_cgroup_eventfd_list
*ev
;
4377 list_for_each_entry(ev
, &mem
->oom_notify
, list
)
4378 eventfd_signal(ev
->eventfd
, 1);
4382 static void mem_cgroup_oom_notify(struct mem_cgroup
*mem
)
4384 struct mem_cgroup
*iter
;
4386 for_each_mem_cgroup_tree(iter
, mem
)
4387 mem_cgroup_oom_notify_cb(iter
);
4390 static int mem_cgroup_usage_register_event(struct cgroup
*cgrp
,
4391 struct cftype
*cft
, struct eventfd_ctx
*eventfd
, const char *args
)
4393 struct mem_cgroup
*memcg
= mem_cgroup_from_cont(cgrp
);
4394 struct mem_cgroup_thresholds
*thresholds
;
4395 struct mem_cgroup_threshold_ary
*new;
4396 int type
= MEMFILE_TYPE(cft
->private);
4397 u64 threshold
, usage
;
4400 ret
= res_counter_memparse_write_strategy(args
, &threshold
);
4404 mutex_lock(&memcg
->thresholds_lock
);
4407 thresholds
= &memcg
->thresholds
;
4408 else if (type
== _MEMSWAP
)
4409 thresholds
= &memcg
->memsw_thresholds
;
4413 usage
= mem_cgroup_usage(memcg
, type
== _MEMSWAP
);
4415 /* Check if a threshold crossed before adding a new one */
4416 if (thresholds
->primary
)
4417 __mem_cgroup_threshold(memcg
, type
== _MEMSWAP
);
4419 size
= thresholds
->primary
? thresholds
->primary
->size
+ 1 : 1;
4421 /* Allocate memory for new array of thresholds */
4422 new = kmalloc(sizeof(*new) + size
* sizeof(struct mem_cgroup_threshold
),
4430 /* Copy thresholds (if any) to new array */
4431 if (thresholds
->primary
) {
4432 memcpy(new->entries
, thresholds
->primary
->entries
, (size
- 1) *
4433 sizeof(struct mem_cgroup_threshold
));
4436 /* Add new threshold */
4437 new->entries
[size
- 1].eventfd
= eventfd
;
4438 new->entries
[size
- 1].threshold
= threshold
;
4440 /* Sort thresholds. Registering of new threshold isn't time-critical */
4441 sort(new->entries
, size
, sizeof(struct mem_cgroup_threshold
),
4442 compare_thresholds
, NULL
);
4444 /* Find current threshold */
4445 new->current_threshold
= -1;
4446 for (i
= 0; i
< size
; i
++) {
4447 if (new->entries
[i
].threshold
< usage
) {
4449 * new->current_threshold will not be used until
4450 * rcu_assign_pointer(), so it's safe to increment
4453 ++new->current_threshold
;
4457 /* Free old spare buffer and save old primary buffer as spare */
4458 kfree(thresholds
->spare
);
4459 thresholds
->spare
= thresholds
->primary
;
4461 rcu_assign_pointer(thresholds
->primary
, new);
4463 /* To be sure that nobody uses thresholds */
4467 mutex_unlock(&memcg
->thresholds_lock
);
4472 static void mem_cgroup_usage_unregister_event(struct cgroup
*cgrp
,
4473 struct cftype
*cft
, struct eventfd_ctx
*eventfd
)
4475 struct mem_cgroup
*memcg
= mem_cgroup_from_cont(cgrp
);
4476 struct mem_cgroup_thresholds
*thresholds
;
4477 struct mem_cgroup_threshold_ary
*new;
4478 int type
= MEMFILE_TYPE(cft
->private);
4482 mutex_lock(&memcg
->thresholds_lock
);
4484 thresholds
= &memcg
->thresholds
;
4485 else if (type
== _MEMSWAP
)
4486 thresholds
= &memcg
->memsw_thresholds
;
4491 * Something went wrong if we trying to unregister a threshold
4492 * if we don't have thresholds
4494 BUG_ON(!thresholds
);
4496 usage
= mem_cgroup_usage(memcg
, type
== _MEMSWAP
);
4498 /* Check if a threshold crossed before removing */
4499 __mem_cgroup_threshold(memcg
, type
== _MEMSWAP
);
4501 /* Calculate new number of threshold */
4503 for (i
= 0; i
< thresholds
->primary
->size
; i
++) {
4504 if (thresholds
->primary
->entries
[i
].eventfd
!= eventfd
)
4508 new = thresholds
->spare
;
4510 /* Set thresholds array to NULL if we don't have thresholds */
4519 /* Copy thresholds and find current threshold */
4520 new->current_threshold
= -1;
4521 for (i
= 0, j
= 0; i
< thresholds
->primary
->size
; i
++) {
4522 if (thresholds
->primary
->entries
[i
].eventfd
== eventfd
)
4525 new->entries
[j
] = thresholds
->primary
->entries
[i
];
4526 if (new->entries
[j
].threshold
< usage
) {
4528 * new->current_threshold will not be used
4529 * until rcu_assign_pointer(), so it's safe to increment
4532 ++new->current_threshold
;
4538 /* Swap primary and spare array */
4539 thresholds
->spare
= thresholds
->primary
;
4540 rcu_assign_pointer(thresholds
->primary
, new);
4542 /* To be sure that nobody uses thresholds */
4545 mutex_unlock(&memcg
->thresholds_lock
);
4548 static int mem_cgroup_oom_register_event(struct cgroup
*cgrp
,
4549 struct cftype
*cft
, struct eventfd_ctx
*eventfd
, const char *args
)
4551 struct mem_cgroup
*memcg
= mem_cgroup_from_cont(cgrp
);
4552 struct mem_cgroup_eventfd_list
*event
;
4553 int type
= MEMFILE_TYPE(cft
->private);
4555 BUG_ON(type
!= _OOM_TYPE
);
4556 event
= kmalloc(sizeof(*event
), GFP_KERNEL
);
4560 mutex_lock(&memcg_oom_mutex
);
4562 event
->eventfd
= eventfd
;
4563 list_add(&event
->list
, &memcg
->oom_notify
);
4565 /* already in OOM ? */
4566 if (atomic_read(&memcg
->oom_lock
))
4567 eventfd_signal(eventfd
, 1);
4568 mutex_unlock(&memcg_oom_mutex
);
4573 static void mem_cgroup_oom_unregister_event(struct cgroup
*cgrp
,
4574 struct cftype
*cft
, struct eventfd_ctx
*eventfd
)
4576 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cgrp
);
4577 struct mem_cgroup_eventfd_list
*ev
, *tmp
;
4578 int type
= MEMFILE_TYPE(cft
->private);
4580 BUG_ON(type
!= _OOM_TYPE
);
4582 mutex_lock(&memcg_oom_mutex
);
4584 list_for_each_entry_safe(ev
, tmp
, &mem
->oom_notify
, list
) {
4585 if (ev
->eventfd
== eventfd
) {
4586 list_del(&ev
->list
);
4591 mutex_unlock(&memcg_oom_mutex
);
4594 static int mem_cgroup_oom_control_read(struct cgroup
*cgrp
,
4595 struct cftype
*cft
, struct cgroup_map_cb
*cb
)
4597 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cgrp
);
4599 cb
->fill(cb
, "oom_kill_disable", mem
->oom_kill_disable
);
4601 if (atomic_read(&mem
->oom_lock
))
4602 cb
->fill(cb
, "under_oom", 1);
4604 cb
->fill(cb
, "under_oom", 0);
4608 static int mem_cgroup_oom_control_write(struct cgroup
*cgrp
,
4609 struct cftype
*cft
, u64 val
)
4611 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cgrp
);
4612 struct mem_cgroup
*parent
;
4614 /* cannot set to root cgroup and only 0 and 1 are allowed */
4615 if (!cgrp
->parent
|| !((val
== 0) || (val
== 1)))
4618 parent
= mem_cgroup_from_cont(cgrp
->parent
);
4621 /* oom-kill-disable is a flag for subhierarchy. */
4622 if ((parent
->use_hierarchy
) ||
4623 (mem
->use_hierarchy
&& !list_empty(&cgrp
->children
))) {
4627 mem
->oom_kill_disable
= val
;
4629 memcg_oom_recover(mem
);
4635 static const struct file_operations mem_control_numa_stat_file_operations
= {
4637 .llseek
= seq_lseek
,
4638 .release
= single_release
,
4641 static int mem_control_numa_stat_open(struct inode
*unused
, struct file
*file
)
4643 struct cgroup
*cont
= file
->f_dentry
->d_parent
->d_fsdata
;
4645 file
->f_op
= &mem_control_numa_stat_file_operations
;
4646 return single_open(file
, mem_control_numa_stat_show
, cont
);
4648 #endif /* CONFIG_NUMA */
4650 static struct cftype mem_cgroup_files
[] = {
4652 .name
= "usage_in_bytes",
4653 .private = MEMFILE_PRIVATE(_MEM
, RES_USAGE
),
4654 .read_u64
= mem_cgroup_read
,
4655 .register_event
= mem_cgroup_usage_register_event
,
4656 .unregister_event
= mem_cgroup_usage_unregister_event
,
4659 .name
= "max_usage_in_bytes",
4660 .private = MEMFILE_PRIVATE(_MEM
, RES_MAX_USAGE
),
4661 .trigger
= mem_cgroup_reset
,
4662 .read_u64
= mem_cgroup_read
,
4665 .name
= "limit_in_bytes",
4666 .private = MEMFILE_PRIVATE(_MEM
, RES_LIMIT
),
4667 .write_string
= mem_cgroup_write
,
4668 .read_u64
= mem_cgroup_read
,
4671 .name
= "soft_limit_in_bytes",
4672 .private = MEMFILE_PRIVATE(_MEM
, RES_SOFT_LIMIT
),
4673 .write_string
= mem_cgroup_write
,
4674 .read_u64
= mem_cgroup_read
,
4678 .private = MEMFILE_PRIVATE(_MEM
, RES_FAILCNT
),
4679 .trigger
= mem_cgroup_reset
,
4680 .read_u64
= mem_cgroup_read
,
4684 .read_map
= mem_control_stat_show
,
4687 .name
= "force_empty",
4688 .trigger
= mem_cgroup_force_empty_write
,
4691 .name
= "use_hierarchy",
4692 .write_u64
= mem_cgroup_hierarchy_write
,
4693 .read_u64
= mem_cgroup_hierarchy_read
,
4696 .name
= "swappiness",
4697 .read_u64
= mem_cgroup_swappiness_read
,
4698 .write_u64
= mem_cgroup_swappiness_write
,
4701 .name
= "move_charge_at_immigrate",
4702 .read_u64
= mem_cgroup_move_charge_read
,
4703 .write_u64
= mem_cgroup_move_charge_write
,
4706 .name
= "oom_control",
4707 .read_map
= mem_cgroup_oom_control_read
,
4708 .write_u64
= mem_cgroup_oom_control_write
,
4709 .register_event
= mem_cgroup_oom_register_event
,
4710 .unregister_event
= mem_cgroup_oom_unregister_event
,
4711 .private = MEMFILE_PRIVATE(_OOM_TYPE
, OOM_CONTROL
),
4715 .name
= "numa_stat",
4716 .open
= mem_control_numa_stat_open
,
4722 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4723 static struct cftype memsw_cgroup_files
[] = {
4725 .name
= "memsw.usage_in_bytes",
4726 .private = MEMFILE_PRIVATE(_MEMSWAP
, RES_USAGE
),
4727 .read_u64
= mem_cgroup_read
,
4728 .register_event
= mem_cgroup_usage_register_event
,
4729 .unregister_event
= mem_cgroup_usage_unregister_event
,
4732 .name
= "memsw.max_usage_in_bytes",
4733 .private = MEMFILE_PRIVATE(_MEMSWAP
, RES_MAX_USAGE
),
4734 .trigger
= mem_cgroup_reset
,
4735 .read_u64
= mem_cgroup_read
,
4738 .name
= "memsw.limit_in_bytes",
4739 .private = MEMFILE_PRIVATE(_MEMSWAP
, RES_LIMIT
),
4740 .write_string
= mem_cgroup_write
,
4741 .read_u64
= mem_cgroup_read
,
4744 .name
= "memsw.failcnt",
4745 .private = MEMFILE_PRIVATE(_MEMSWAP
, RES_FAILCNT
),
4746 .trigger
= mem_cgroup_reset
,
4747 .read_u64
= mem_cgroup_read
,
4751 static int register_memsw_files(struct cgroup
*cont
, struct cgroup_subsys
*ss
)
4753 if (!do_swap_account
)
4755 return cgroup_add_files(cont
, ss
, memsw_cgroup_files
,
4756 ARRAY_SIZE(memsw_cgroup_files
));
4759 static int register_memsw_files(struct cgroup
*cont
, struct cgroup_subsys
*ss
)
4765 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup
*mem
, int node
)
4767 struct mem_cgroup_per_node
*pn
;
4768 struct mem_cgroup_per_zone
*mz
;
4770 int zone
, tmp
= node
;
4772 * This routine is called against possible nodes.
4773 * But it's BUG to call kmalloc() against offline node.
4775 * TODO: this routine can waste much memory for nodes which will
4776 * never be onlined. It's better to use memory hotplug callback
4779 if (!node_state(node
, N_NORMAL_MEMORY
))
4781 pn
= kzalloc_node(sizeof(*pn
), GFP_KERNEL
, tmp
);
4785 mem
->info
.nodeinfo
[node
] = pn
;
4786 for (zone
= 0; zone
< MAX_NR_ZONES
; zone
++) {
4787 mz
= &pn
->zoneinfo
[zone
];
4789 INIT_LIST_HEAD(&mz
->lists
[l
]);
4790 mz
->usage_in_excess
= 0;
4791 mz
->on_tree
= false;
4797 static void free_mem_cgroup_per_zone_info(struct mem_cgroup
*mem
, int node
)
4799 kfree(mem
->info
.nodeinfo
[node
]);
4802 static struct mem_cgroup
*mem_cgroup_alloc(void)
4804 struct mem_cgroup
*mem
;
4805 int size
= sizeof(struct mem_cgroup
);
4807 /* Can be very big if MAX_NUMNODES is very big */
4808 if (size
< PAGE_SIZE
)
4809 mem
= kzalloc(size
, GFP_KERNEL
);
4811 mem
= vzalloc(size
);
4816 mem
->stat
= alloc_percpu(struct mem_cgroup_stat_cpu
);
4819 spin_lock_init(&mem
->pcp_counter_lock
);
4823 if (size
< PAGE_SIZE
)
4831 * At destroying mem_cgroup, references from swap_cgroup can remain.
4832 * (scanning all at force_empty is too costly...)
4834 * Instead of clearing all references at force_empty, we remember
4835 * the number of reference from swap_cgroup and free mem_cgroup when
4836 * it goes down to 0.
4838 * Removal of cgroup itself succeeds regardless of refs from swap.
4841 static void __mem_cgroup_free(struct mem_cgroup
*mem
)
4845 mem_cgroup_remove_from_trees(mem
);
4846 free_css_id(&mem_cgroup_subsys
, &mem
->css
);
4848 for_each_node_state(node
, N_POSSIBLE
)
4849 free_mem_cgroup_per_zone_info(mem
, node
);
4851 free_percpu(mem
->stat
);
4852 if (sizeof(struct mem_cgroup
) < PAGE_SIZE
)
4858 static void mem_cgroup_get(struct mem_cgroup
*mem
)
4860 atomic_inc(&mem
->refcnt
);
4863 static void __mem_cgroup_put(struct mem_cgroup
*mem
, int count
)
4865 if (atomic_sub_and_test(count
, &mem
->refcnt
)) {
4866 struct mem_cgroup
*parent
= parent_mem_cgroup(mem
);
4867 __mem_cgroup_free(mem
);
4869 mem_cgroup_put(parent
);
4873 static void mem_cgroup_put(struct mem_cgroup
*mem
)
4875 __mem_cgroup_put(mem
, 1);
4879 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4881 static struct mem_cgroup
*parent_mem_cgroup(struct mem_cgroup
*mem
)
4883 if (!mem
->res
.parent
)
4885 return mem_cgroup_from_res_counter(mem
->res
.parent
, res
);
4888 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4889 static void __init
enable_swap_cgroup(void)
4891 if (!mem_cgroup_disabled() && really_do_swap_account
)
4892 do_swap_account
= 1;
4895 static void __init
enable_swap_cgroup(void)
4900 static int mem_cgroup_soft_limit_tree_init(void)
4902 struct mem_cgroup_tree_per_node
*rtpn
;
4903 struct mem_cgroup_tree_per_zone
*rtpz
;
4904 int tmp
, node
, zone
;
4906 for_each_node_state(node
, N_POSSIBLE
) {
4908 if (!node_state(node
, N_NORMAL_MEMORY
))
4910 rtpn
= kzalloc_node(sizeof(*rtpn
), GFP_KERNEL
, tmp
);
4914 soft_limit_tree
.rb_tree_per_node
[node
] = rtpn
;
4916 for (zone
= 0; zone
< MAX_NR_ZONES
; zone
++) {
4917 rtpz
= &rtpn
->rb_tree_per_zone
[zone
];
4918 rtpz
->rb_root
= RB_ROOT
;
4919 spin_lock_init(&rtpz
->lock
);
4925 static struct cgroup_subsys_state
* __ref
4926 mem_cgroup_create(struct cgroup_subsys
*ss
, struct cgroup
*cont
)
4928 struct mem_cgroup
*mem
, *parent
;
4929 long error
= -ENOMEM
;
4932 mem
= mem_cgroup_alloc();
4934 return ERR_PTR(error
);
4936 for_each_node_state(node
, N_POSSIBLE
)
4937 if (alloc_mem_cgroup_per_zone_info(mem
, node
))
4941 if (cont
->parent
== NULL
) {
4943 enable_swap_cgroup();
4945 root_mem_cgroup
= mem
;
4946 if (mem_cgroup_soft_limit_tree_init())
4948 for_each_possible_cpu(cpu
) {
4949 struct memcg_stock_pcp
*stock
=
4950 &per_cpu(memcg_stock
, cpu
);
4951 INIT_WORK(&stock
->work
, drain_local_stock
);
4953 hotcpu_notifier(memcg_cpu_hotplug_callback
, 0);
4955 parent
= mem_cgroup_from_cont(cont
->parent
);
4956 mem
->use_hierarchy
= parent
->use_hierarchy
;
4957 mem
->oom_kill_disable
= parent
->oom_kill_disable
;
4960 if (parent
&& parent
->use_hierarchy
) {
4961 res_counter_init(&mem
->res
, &parent
->res
);
4962 res_counter_init(&mem
->memsw
, &parent
->memsw
);
4964 * We increment refcnt of the parent to ensure that we can
4965 * safely access it on res_counter_charge/uncharge.
4966 * This refcnt will be decremented when freeing this
4967 * mem_cgroup(see mem_cgroup_put).
4969 mem_cgroup_get(parent
);
4971 res_counter_init(&mem
->res
, NULL
);
4972 res_counter_init(&mem
->memsw
, NULL
);
4974 mem
->last_scanned_child
= 0;
4975 mem
->last_scanned_node
= MAX_NUMNODES
;
4976 INIT_LIST_HEAD(&mem
->oom_notify
);
4979 mem
->swappiness
= get_swappiness(parent
);
4980 atomic_set(&mem
->refcnt
, 1);
4981 mem
->move_charge_at_immigrate
= 0;
4982 mutex_init(&mem
->thresholds_lock
);
4985 __mem_cgroup_free(mem
);
4986 root_mem_cgroup
= NULL
;
4987 return ERR_PTR(error
);
4990 static int mem_cgroup_pre_destroy(struct cgroup_subsys
*ss
,
4991 struct cgroup
*cont
)
4993 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cont
);
4995 return mem_cgroup_force_empty(mem
, false);
4998 static void mem_cgroup_destroy(struct cgroup_subsys
*ss
,
4999 struct cgroup
*cont
)
5001 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cont
);
5003 mem_cgroup_put(mem
);
5006 static int mem_cgroup_populate(struct cgroup_subsys
*ss
,
5007 struct cgroup
*cont
)
5011 ret
= cgroup_add_files(cont
, ss
, mem_cgroup_files
,
5012 ARRAY_SIZE(mem_cgroup_files
));
5015 ret
= register_memsw_files(cont
, ss
);
5020 /* Handlers for move charge at task migration. */
5021 #define PRECHARGE_COUNT_AT_ONCE 256
5022 static int mem_cgroup_do_precharge(unsigned long count
)
5025 int batch_count
= PRECHARGE_COUNT_AT_ONCE
;
5026 struct mem_cgroup
*mem
= mc
.to
;
5028 if (mem_cgroup_is_root(mem
)) {
5029 mc
.precharge
+= count
;
5030 /* we don't need css_get for root */
5033 /* try to charge at once */
5035 struct res_counter
*dummy
;
5037 * "mem" cannot be under rmdir() because we've already checked
5038 * by cgroup_lock_live_cgroup() that it is not removed and we
5039 * are still under the same cgroup_mutex. So we can postpone
5042 if (res_counter_charge(&mem
->res
, PAGE_SIZE
* count
, &dummy
))
5044 if (do_swap_account
&& res_counter_charge(&mem
->memsw
,
5045 PAGE_SIZE
* count
, &dummy
)) {
5046 res_counter_uncharge(&mem
->res
, PAGE_SIZE
* count
);
5049 mc
.precharge
+= count
;
5053 /* fall back to one by one charge */
5055 if (signal_pending(current
)) {
5059 if (!batch_count
--) {
5060 batch_count
= PRECHARGE_COUNT_AT_ONCE
;
5063 ret
= __mem_cgroup_try_charge(NULL
, GFP_KERNEL
, 1, &mem
, false);
5065 /* mem_cgroup_clear_mc() will do uncharge later */
5073 * is_target_pte_for_mc - check a pte whether it is valid for move charge
5074 * @vma: the vma the pte to be checked belongs
5075 * @addr: the address corresponding to the pte to be checked
5076 * @ptent: the pte to be checked
5077 * @target: the pointer the target page or swap ent will be stored(can be NULL)
5080 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5081 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5082 * move charge. if @target is not NULL, the page is stored in target->page
5083 * with extra refcnt got(Callers should handle it).
5084 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5085 * target for charge migration. if @target is not NULL, the entry is stored
5088 * Called with pte lock held.
5095 enum mc_target_type
{
5096 MC_TARGET_NONE
, /* not used */
5101 static struct page
*mc_handle_present_pte(struct vm_area_struct
*vma
,
5102 unsigned long addr
, pte_t ptent
)
5104 struct page
*page
= vm_normal_page(vma
, addr
, ptent
);
5106 if (!page
|| !page_mapped(page
))
5108 if (PageAnon(page
)) {
5109 /* we don't move shared anon */
5110 if (!move_anon() || page_mapcount(page
) > 2)
5112 } else if (!move_file())
5113 /* we ignore mapcount for file pages */
5115 if (!get_page_unless_zero(page
))
5121 static struct page
*mc_handle_swap_pte(struct vm_area_struct
*vma
,
5122 unsigned long addr
, pte_t ptent
, swp_entry_t
*entry
)
5125 struct page
*page
= NULL
;
5126 swp_entry_t ent
= pte_to_swp_entry(ptent
);
5128 if (!move_anon() || non_swap_entry(ent
))
5130 usage_count
= mem_cgroup_count_swap_user(ent
, &page
);
5131 if (usage_count
> 1) { /* we don't move shared anon */
5136 if (do_swap_account
)
5137 entry
->val
= ent
.val
;
5142 static struct page
*mc_handle_file_pte(struct vm_area_struct
*vma
,
5143 unsigned long addr
, pte_t ptent
, swp_entry_t
*entry
)
5145 struct page
*page
= NULL
;
5146 struct inode
*inode
;
5147 struct address_space
*mapping
;
5150 if (!vma
->vm_file
) /* anonymous vma */
5155 inode
= vma
->vm_file
->f_path
.dentry
->d_inode
;
5156 mapping
= vma
->vm_file
->f_mapping
;
5157 if (pte_none(ptent
))
5158 pgoff
= linear_page_index(vma
, addr
);
5159 else /* pte_file(ptent) is true */
5160 pgoff
= pte_to_pgoff(ptent
);
5162 /* page is moved even if it's not RSS of this task(page-faulted). */
5163 if (!mapping_cap_swap_backed(mapping
)) { /* normal file */
5164 page
= find_get_page(mapping
, pgoff
);
5165 } else { /* shmem/tmpfs file. we should take account of swap too. */
5167 mem_cgroup_get_shmem_target(inode
, pgoff
, &page
, &ent
);
5168 if (do_swap_account
)
5169 entry
->val
= ent
.val
;
5175 static int is_target_pte_for_mc(struct vm_area_struct
*vma
,
5176 unsigned long addr
, pte_t ptent
, union mc_target
*target
)
5178 struct page
*page
= NULL
;
5179 struct page_cgroup
*pc
;
5181 swp_entry_t ent
= { .val
= 0 };
5183 if (pte_present(ptent
))
5184 page
= mc_handle_present_pte(vma
, addr
, ptent
);
5185 else if (is_swap_pte(ptent
))
5186 page
= mc_handle_swap_pte(vma
, addr
, ptent
, &ent
);
5187 else if (pte_none(ptent
) || pte_file(ptent
))
5188 page
= mc_handle_file_pte(vma
, addr
, ptent
, &ent
);
5190 if (!page
&& !ent
.val
)
5193 pc
= lookup_page_cgroup(page
);
5195 * Do only loose check w/o page_cgroup lock.
5196 * mem_cgroup_move_account() checks the pc is valid or not under
5199 if (PageCgroupUsed(pc
) && pc
->mem_cgroup
== mc
.from
) {
5200 ret
= MC_TARGET_PAGE
;
5202 target
->page
= page
;
5204 if (!ret
|| !target
)
5207 /* There is a swap entry and a page doesn't exist or isn't charged */
5208 if (ent
.val
&& !ret
&&
5209 css_id(&mc
.from
->css
) == lookup_swap_cgroup(ent
)) {
5210 ret
= MC_TARGET_SWAP
;
5217 static int mem_cgroup_count_precharge_pte_range(pmd_t
*pmd
,
5218 unsigned long addr
, unsigned long end
,
5219 struct mm_walk
*walk
)
5221 struct vm_area_struct
*vma
= walk
->private;
5225 split_huge_page_pmd(walk
->mm
, pmd
);
5227 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, addr
, &ptl
);
5228 for (; addr
!= end
; pte
++, addr
+= PAGE_SIZE
)
5229 if (is_target_pte_for_mc(vma
, addr
, *pte
, NULL
))
5230 mc
.precharge
++; /* increment precharge temporarily */
5231 pte_unmap_unlock(pte
- 1, ptl
);
5237 static unsigned long mem_cgroup_count_precharge(struct mm_struct
*mm
)
5239 unsigned long precharge
;
5240 struct vm_area_struct
*vma
;
5242 down_read(&mm
->mmap_sem
);
5243 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
5244 struct mm_walk mem_cgroup_count_precharge_walk
= {
5245 .pmd_entry
= mem_cgroup_count_precharge_pte_range
,
5249 if (is_vm_hugetlb_page(vma
))
5251 walk_page_range(vma
->vm_start
, vma
->vm_end
,
5252 &mem_cgroup_count_precharge_walk
);
5254 up_read(&mm
->mmap_sem
);
5256 precharge
= mc
.precharge
;
5262 static int mem_cgroup_precharge_mc(struct mm_struct
*mm
)
5264 unsigned long precharge
= mem_cgroup_count_precharge(mm
);
5266 VM_BUG_ON(mc
.moving_task
);
5267 mc
.moving_task
= current
;
5268 return mem_cgroup_do_precharge(precharge
);
5271 /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5272 static void __mem_cgroup_clear_mc(void)
5274 struct mem_cgroup
*from
= mc
.from
;
5275 struct mem_cgroup
*to
= mc
.to
;
5277 /* we must uncharge all the leftover precharges from mc.to */
5279 __mem_cgroup_cancel_charge(mc
.to
, mc
.precharge
);
5283 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5284 * we must uncharge here.
5286 if (mc
.moved_charge
) {
5287 __mem_cgroup_cancel_charge(mc
.from
, mc
.moved_charge
);
5288 mc
.moved_charge
= 0;
5290 /* we must fixup refcnts and charges */
5291 if (mc
.moved_swap
) {
5292 /* uncharge swap account from the old cgroup */
5293 if (!mem_cgroup_is_root(mc
.from
))
5294 res_counter_uncharge(&mc
.from
->memsw
,
5295 PAGE_SIZE
* mc
.moved_swap
);
5296 __mem_cgroup_put(mc
.from
, mc
.moved_swap
);
5298 if (!mem_cgroup_is_root(mc
.to
)) {
5300 * we charged both to->res and to->memsw, so we should
5303 res_counter_uncharge(&mc
.to
->res
,
5304 PAGE_SIZE
* mc
.moved_swap
);
5306 /* we've already done mem_cgroup_get(mc.to) */
5309 memcg_oom_recover(from
);
5310 memcg_oom_recover(to
);
5311 wake_up_all(&mc
.waitq
);
5314 static void mem_cgroup_clear_mc(void)
5316 struct mem_cgroup
*from
= mc
.from
;
5319 * we must clear moving_task before waking up waiters at the end of
5322 mc
.moving_task
= NULL
;
5323 __mem_cgroup_clear_mc();
5324 spin_lock(&mc
.lock
);
5327 spin_unlock(&mc
.lock
);
5328 mem_cgroup_end_move(from
);
5331 static int mem_cgroup_can_attach(struct cgroup_subsys
*ss
,
5332 struct cgroup
*cgroup
,
5333 struct task_struct
*p
)
5336 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cgroup
);
5338 if (mem
->move_charge_at_immigrate
) {
5339 struct mm_struct
*mm
;
5340 struct mem_cgroup
*from
= mem_cgroup_from_task(p
);
5342 VM_BUG_ON(from
== mem
);
5344 mm
= get_task_mm(p
);
5347 /* We move charges only when we move a owner of the mm */
5348 if (mm
->owner
== p
) {
5351 VM_BUG_ON(mc
.precharge
);
5352 VM_BUG_ON(mc
.moved_charge
);
5353 VM_BUG_ON(mc
.moved_swap
);
5354 mem_cgroup_start_move(from
);
5355 spin_lock(&mc
.lock
);
5358 spin_unlock(&mc
.lock
);
5359 /* We set mc.moving_task later */
5361 ret
= mem_cgroup_precharge_mc(mm
);
5363 mem_cgroup_clear_mc();
5370 static void mem_cgroup_cancel_attach(struct cgroup_subsys
*ss
,
5371 struct cgroup
*cgroup
,
5372 struct task_struct
*p
)
5374 mem_cgroup_clear_mc();
5377 static int mem_cgroup_move_charge_pte_range(pmd_t
*pmd
,
5378 unsigned long addr
, unsigned long end
,
5379 struct mm_walk
*walk
)
5382 struct vm_area_struct
*vma
= walk
->private;
5386 split_huge_page_pmd(walk
->mm
, pmd
);
5388 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, addr
, &ptl
);
5389 for (; addr
!= end
; addr
+= PAGE_SIZE
) {
5390 pte_t ptent
= *(pte
++);
5391 union mc_target target
;
5394 struct page_cgroup
*pc
;
5400 type
= is_target_pte_for_mc(vma
, addr
, ptent
, &target
);
5402 case MC_TARGET_PAGE
:
5404 if (isolate_lru_page(page
))
5406 pc
= lookup_page_cgroup(page
);
5407 if (!mem_cgroup_move_account(page
, 1, pc
,
5408 mc
.from
, mc
.to
, false)) {
5410 /* we uncharge from mc.from later. */
5413 putback_lru_page(page
);
5414 put
: /* is_target_pte_for_mc() gets the page */
5417 case MC_TARGET_SWAP
:
5419 if (!mem_cgroup_move_swap_account(ent
,
5420 mc
.from
, mc
.to
, false)) {
5422 /* we fixup refcnts and charges later. */
5430 pte_unmap_unlock(pte
- 1, ptl
);
5435 * We have consumed all precharges we got in can_attach().
5436 * We try charge one by one, but don't do any additional
5437 * charges to mc.to if we have failed in charge once in attach()
5440 ret
= mem_cgroup_do_precharge(1);
5448 static void mem_cgroup_move_charge(struct mm_struct
*mm
)
5450 struct vm_area_struct
*vma
;
5452 lru_add_drain_all();
5454 if (unlikely(!down_read_trylock(&mm
->mmap_sem
))) {
5456 * Someone who are holding the mmap_sem might be waiting in
5457 * waitq. So we cancel all extra charges, wake up all waiters,
5458 * and retry. Because we cancel precharges, we might not be able
5459 * to move enough charges, but moving charge is a best-effort
5460 * feature anyway, so it wouldn't be a big problem.
5462 __mem_cgroup_clear_mc();
5466 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
5468 struct mm_walk mem_cgroup_move_charge_walk
= {
5469 .pmd_entry
= mem_cgroup_move_charge_pte_range
,
5473 if (is_vm_hugetlb_page(vma
))
5475 ret
= walk_page_range(vma
->vm_start
, vma
->vm_end
,
5476 &mem_cgroup_move_charge_walk
);
5479 * means we have consumed all precharges and failed in
5480 * doing additional charge. Just abandon here.
5484 up_read(&mm
->mmap_sem
);
5487 static void mem_cgroup_move_task(struct cgroup_subsys
*ss
,
5488 struct cgroup
*cont
,
5489 struct cgroup
*old_cont
,
5490 struct task_struct
*p
)
5492 struct mm_struct
*mm
= get_task_mm(p
);
5496 mem_cgroup_move_charge(mm
);
5501 mem_cgroup_clear_mc();
5503 #else /* !CONFIG_MMU */
5504 static int mem_cgroup_can_attach(struct cgroup_subsys
*ss
,
5505 struct cgroup
*cgroup
,
5506 struct task_struct
*p
)
5510 static void mem_cgroup_cancel_attach(struct cgroup_subsys
*ss
,
5511 struct cgroup
*cgroup
,
5512 struct task_struct
*p
)
5515 static void mem_cgroup_move_task(struct cgroup_subsys
*ss
,
5516 struct cgroup
*cont
,
5517 struct cgroup
*old_cont
,
5518 struct task_struct
*p
)
5523 struct cgroup_subsys mem_cgroup_subsys
= {
5525 .subsys_id
= mem_cgroup_subsys_id
,
5526 .create
= mem_cgroup_create
,
5527 .pre_destroy
= mem_cgroup_pre_destroy
,
5528 .destroy
= mem_cgroup_destroy
,
5529 .populate
= mem_cgroup_populate
,
5530 .can_attach
= mem_cgroup_can_attach
,
5531 .cancel_attach
= mem_cgroup_cancel_attach
,
5532 .attach
= mem_cgroup_move_task
,
5537 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
5538 static int __init
enable_swap_account(char *s
)
5540 /* consider enabled if no parameter or 1 is given */
5541 if (!strcmp(s
, "1"))
5542 really_do_swap_account
= 1;
5543 else if (!strcmp(s
, "0"))
5544 really_do_swap_account
= 0;
5547 __setup("swapaccount=", enable_swap_account
);