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/slab.h>
39 #include <linux/swap.h>
40 #include <linux/swapops.h>
41 #include <linux/spinlock.h>
42 #include <linux/eventfd.h>
43 #include <linux/sort.h>
45 #include <linux/seq_file.h>
46 #include <linux/vmalloc.h>
47 #include <linux/mm_inline.h>
48 #include <linux/page_cgroup.h>
49 #include <linux/cpu.h>
50 #include <linux/oom.h>
53 #include <asm/uaccess.h>
55 #include <trace/events/vmscan.h>
57 struct cgroup_subsys mem_cgroup_subsys __read_mostly
;
58 #define MEM_CGROUP_RECLAIM_RETRIES 5
59 struct mem_cgroup
*root_mem_cgroup __read_mostly
;
61 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
62 /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
63 int do_swap_account __read_mostly
;
65 /* for remember boot option*/
66 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP_ENABLED
67 static int really_do_swap_account __initdata
= 1;
69 static int really_do_swap_account __initdata
= 0;
73 #define do_swap_account (0)
78 * Statistics for memory cgroup.
80 enum mem_cgroup_stat_index
{
82 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
84 MEM_CGROUP_STAT_CACHE
, /* # of pages charged as cache */
85 MEM_CGROUP_STAT_RSS
, /* # of pages charged as anon rss */
86 MEM_CGROUP_STAT_FILE_MAPPED
, /* # of pages charged as file rss */
87 MEM_CGROUP_STAT_SWAPOUT
, /* # of pages, swapped out */
88 MEM_CGROUP_STAT_DATA
, /* end of data requires synchronization */
89 MEM_CGROUP_ON_MOVE
, /* someone is moving account between groups */
90 MEM_CGROUP_STAT_NSTATS
,
93 enum mem_cgroup_events_index
{
94 MEM_CGROUP_EVENTS_PGPGIN
, /* # of pages paged in */
95 MEM_CGROUP_EVENTS_PGPGOUT
, /* # of pages paged out */
96 MEM_CGROUP_EVENTS_COUNT
, /* # of pages paged in/out */
97 MEM_CGROUP_EVENTS_PGFAULT
, /* # of page-faults */
98 MEM_CGROUP_EVENTS_PGMAJFAULT
, /* # of major page-faults */
99 MEM_CGROUP_EVENTS_NSTATS
,
102 * Per memcg event counter is incremented at every pagein/pageout. With THP,
103 * it will be incremated by the number of pages. This counter is used for
104 * for trigger some periodic events. This is straightforward and better
105 * than using jiffies etc. to handle periodic memcg event.
107 enum mem_cgroup_events_target
{
108 MEM_CGROUP_TARGET_THRESH
,
109 MEM_CGROUP_TARGET_SOFTLIMIT
,
110 MEM_CGROUP_TARGET_NUMAINFO
,
113 #define THRESHOLDS_EVENTS_TARGET (128)
114 #define SOFTLIMIT_EVENTS_TARGET (1024)
115 #define NUMAINFO_EVENTS_TARGET (1024)
117 struct mem_cgroup_stat_cpu
{
118 long count
[MEM_CGROUP_STAT_NSTATS
];
119 unsigned long events
[MEM_CGROUP_EVENTS_NSTATS
];
120 unsigned long targets
[MEM_CGROUP_NTARGETS
];
124 * per-zone information in memory controller.
126 struct mem_cgroup_per_zone
{
128 * spin_lock to protect the per cgroup LRU
130 struct list_head lists
[NR_LRU_LISTS
];
131 unsigned long count
[NR_LRU_LISTS
];
133 struct zone_reclaim_stat reclaim_stat
;
134 struct rb_node tree_node
; /* RB tree node */
135 unsigned long long usage_in_excess
;/* Set to the value by which */
136 /* the soft limit is exceeded*/
138 struct mem_cgroup
*mem
; /* Back pointer, we cannot */
139 /* use container_of */
141 /* Macro for accessing counter */
142 #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
144 struct mem_cgroup_per_node
{
145 struct mem_cgroup_per_zone zoneinfo
[MAX_NR_ZONES
];
148 struct mem_cgroup_lru_info
{
149 struct mem_cgroup_per_node
*nodeinfo
[MAX_NUMNODES
];
153 * Cgroups above their limits are maintained in a RB-Tree, independent of
154 * their hierarchy representation
157 struct mem_cgroup_tree_per_zone
{
158 struct rb_root rb_root
;
162 struct mem_cgroup_tree_per_node
{
163 struct mem_cgroup_tree_per_zone rb_tree_per_zone
[MAX_NR_ZONES
];
166 struct mem_cgroup_tree
{
167 struct mem_cgroup_tree_per_node
*rb_tree_per_node
[MAX_NUMNODES
];
170 static struct mem_cgroup_tree soft_limit_tree __read_mostly
;
172 struct mem_cgroup_threshold
{
173 struct eventfd_ctx
*eventfd
;
178 struct mem_cgroup_threshold_ary
{
179 /* An array index points to threshold just below usage. */
180 int current_threshold
;
181 /* Size of entries[] */
183 /* Array of thresholds */
184 struct mem_cgroup_threshold entries
[0];
187 struct mem_cgroup_thresholds
{
188 /* Primary thresholds array */
189 struct mem_cgroup_threshold_ary
*primary
;
191 * Spare threshold array.
192 * This is needed to make mem_cgroup_unregister_event() "never fail".
193 * It must be able to store at least primary->size - 1 entries.
195 struct mem_cgroup_threshold_ary
*spare
;
199 struct mem_cgroup_eventfd_list
{
200 struct list_head list
;
201 struct eventfd_ctx
*eventfd
;
204 static void mem_cgroup_threshold(struct mem_cgroup
*mem
);
205 static void mem_cgroup_oom_notify(struct mem_cgroup
*mem
);
211 SCAN_BY_SHRINK
, /* not recorded now */
230 unsigned long stats
[NR_SCAN_CONTEXT
][NR_SCANSTATS
];
231 unsigned long rootstats
[NR_SCAN_CONTEXT
][NR_SCANSTATS
];
234 const char *scanstat_string
[NR_SCANSTATS
] = {
236 "scanned_anon_pages",
237 "scanned_file_pages",
239 "rotated_anon_pages",
240 "rotated_file_pages",
246 #define SCANSTAT_WORD_LIMIT "_by_limit"
247 #define SCANSTAT_WORD_SYSTEM "_by_system"
248 #define SCANSTAT_WORD_HIERARCHY "_under_hierarchy"
252 * The memory controller data structure. The memory controller controls both
253 * page cache and RSS per cgroup. We would eventually like to provide
254 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
255 * to help the administrator determine what knobs to tune.
257 * TODO: Add a water mark for the memory controller. Reclaim will begin when
258 * we hit the water mark. May be even add a low water mark, such that
259 * no reclaim occurs from a cgroup at it's low water mark, this is
260 * a feature that will be implemented much later in the future.
263 struct cgroup_subsys_state css
;
265 * the counter to account for memory usage
267 struct res_counter res
;
269 * the counter to account for mem+swap usage.
271 struct res_counter memsw
;
273 * Per cgroup active and inactive list, similar to the
274 * per zone LRU lists.
276 struct mem_cgroup_lru_info info
;
278 * While reclaiming in a hierarchy, we cache the last child we
281 int last_scanned_child
;
282 int last_scanned_node
;
284 nodemask_t scan_nodes
;
285 atomic_t numainfo_events
;
286 atomic_t numainfo_updating
;
289 * Should the accounting and control be hierarchical, per subtree?
299 /* OOM-Killer disable */
300 int oom_kill_disable
;
302 /* set when res.limit == memsw.limit */
303 bool memsw_is_minimum
;
305 /* protect arrays of thresholds */
306 struct mutex thresholds_lock
;
308 /* thresholds for memory usage. RCU-protected */
309 struct mem_cgroup_thresholds thresholds
;
311 /* thresholds for mem+swap usage. RCU-protected */
312 struct mem_cgroup_thresholds memsw_thresholds
;
314 /* For oom notifier event fd */
315 struct list_head oom_notify
;
316 /* For recording LRU-scan statistics */
317 struct scanstat scanstat
;
319 * Should we move charges of a task when a task is moved into this
320 * mem_cgroup ? And what type of charges should we move ?
322 unsigned long move_charge_at_immigrate
;
326 struct mem_cgroup_stat_cpu
*stat
;
328 * used when a cpu is offlined or other synchronizations
329 * See mem_cgroup_read_stat().
331 struct mem_cgroup_stat_cpu nocpu_base
;
332 spinlock_t pcp_counter_lock
;
335 /* Stuffs for move charges at task migration. */
337 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
338 * left-shifted bitmap of these types.
341 MOVE_CHARGE_TYPE_ANON
, /* private anonymous page and swap of it */
342 MOVE_CHARGE_TYPE_FILE
, /* file page(including tmpfs) and swap of it */
346 /* "mc" and its members are protected by cgroup_mutex */
347 static struct move_charge_struct
{
348 spinlock_t lock
; /* for from, to */
349 struct mem_cgroup
*from
;
350 struct mem_cgroup
*to
;
351 unsigned long precharge
;
352 unsigned long moved_charge
;
353 unsigned long moved_swap
;
354 struct task_struct
*moving_task
; /* a task moving charges */
355 wait_queue_head_t waitq
; /* a waitq for other context */
357 .lock
= __SPIN_LOCK_UNLOCKED(mc
.lock
),
358 .waitq
= __WAIT_QUEUE_HEAD_INITIALIZER(mc
.waitq
),
361 static bool move_anon(void)
363 return test_bit(MOVE_CHARGE_TYPE_ANON
,
364 &mc
.to
->move_charge_at_immigrate
);
367 static bool move_file(void)
369 return test_bit(MOVE_CHARGE_TYPE_FILE
,
370 &mc
.to
->move_charge_at_immigrate
);
374 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
375 * limit reclaim to prevent infinite loops, if they ever occur.
377 #define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
378 #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
381 MEM_CGROUP_CHARGE_TYPE_CACHE
= 0,
382 MEM_CGROUP_CHARGE_TYPE_MAPPED
,
383 MEM_CGROUP_CHARGE_TYPE_SHMEM
, /* used by page migration of shmem */
384 MEM_CGROUP_CHARGE_TYPE_FORCE
, /* used by force_empty */
385 MEM_CGROUP_CHARGE_TYPE_SWAPOUT
, /* for accounting swapcache */
386 MEM_CGROUP_CHARGE_TYPE_DROP
, /* a page was unused swap cache */
390 /* for encoding cft->private value on file */
393 #define _OOM_TYPE (2)
394 #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
395 #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
396 #define MEMFILE_ATTR(val) ((val) & 0xffff)
397 /* Used for OOM nofiier */
398 #define OOM_CONTROL (0)
401 * Reclaim flags for mem_cgroup_hierarchical_reclaim
403 #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
404 #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
405 #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
406 #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
407 #define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
408 #define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
410 static void mem_cgroup_get(struct mem_cgroup
*mem
);
411 static void mem_cgroup_put(struct mem_cgroup
*mem
);
412 static struct mem_cgroup
*parent_mem_cgroup(struct mem_cgroup
*mem
);
413 static void drain_all_stock_async(struct mem_cgroup
*mem
);
415 static struct mem_cgroup_per_zone
*
416 mem_cgroup_zoneinfo(struct mem_cgroup
*mem
, int nid
, int zid
)
418 return &mem
->info
.nodeinfo
[nid
]->zoneinfo
[zid
];
421 struct cgroup_subsys_state
*mem_cgroup_css(struct mem_cgroup
*mem
)
426 static struct mem_cgroup_per_zone
*
427 page_cgroup_zoneinfo(struct mem_cgroup
*mem
, struct page
*page
)
429 int nid
= page_to_nid(page
);
430 int zid
= page_zonenum(page
);
432 return mem_cgroup_zoneinfo(mem
, nid
, zid
);
435 static struct mem_cgroup_tree_per_zone
*
436 soft_limit_tree_node_zone(int nid
, int zid
)
438 return &soft_limit_tree
.rb_tree_per_node
[nid
]->rb_tree_per_zone
[zid
];
441 static struct mem_cgroup_tree_per_zone
*
442 soft_limit_tree_from_page(struct page
*page
)
444 int nid
= page_to_nid(page
);
445 int zid
= page_zonenum(page
);
447 return &soft_limit_tree
.rb_tree_per_node
[nid
]->rb_tree_per_zone
[zid
];
451 __mem_cgroup_insert_exceeded(struct mem_cgroup
*mem
,
452 struct mem_cgroup_per_zone
*mz
,
453 struct mem_cgroup_tree_per_zone
*mctz
,
454 unsigned long long new_usage_in_excess
)
456 struct rb_node
**p
= &mctz
->rb_root
.rb_node
;
457 struct rb_node
*parent
= NULL
;
458 struct mem_cgroup_per_zone
*mz_node
;
463 mz
->usage_in_excess
= new_usage_in_excess
;
464 if (!mz
->usage_in_excess
)
468 mz_node
= rb_entry(parent
, struct mem_cgroup_per_zone
,
470 if (mz
->usage_in_excess
< mz_node
->usage_in_excess
)
473 * We can't avoid mem cgroups that are over their soft
474 * limit by the same amount
476 else if (mz
->usage_in_excess
>= mz_node
->usage_in_excess
)
479 rb_link_node(&mz
->tree_node
, parent
, p
);
480 rb_insert_color(&mz
->tree_node
, &mctz
->rb_root
);
485 __mem_cgroup_remove_exceeded(struct mem_cgroup
*mem
,
486 struct mem_cgroup_per_zone
*mz
,
487 struct mem_cgroup_tree_per_zone
*mctz
)
491 rb_erase(&mz
->tree_node
, &mctz
->rb_root
);
496 mem_cgroup_remove_exceeded(struct mem_cgroup
*mem
,
497 struct mem_cgroup_per_zone
*mz
,
498 struct mem_cgroup_tree_per_zone
*mctz
)
500 spin_lock(&mctz
->lock
);
501 __mem_cgroup_remove_exceeded(mem
, mz
, mctz
);
502 spin_unlock(&mctz
->lock
);
506 static void mem_cgroup_update_tree(struct mem_cgroup
*mem
, struct page
*page
)
508 unsigned long long excess
;
509 struct mem_cgroup_per_zone
*mz
;
510 struct mem_cgroup_tree_per_zone
*mctz
;
511 int nid
= page_to_nid(page
);
512 int zid
= page_zonenum(page
);
513 mctz
= soft_limit_tree_from_page(page
);
516 * Necessary to update all ancestors when hierarchy is used.
517 * because their event counter is not touched.
519 for (; mem
; mem
= parent_mem_cgroup(mem
)) {
520 mz
= mem_cgroup_zoneinfo(mem
, nid
, zid
);
521 excess
= res_counter_soft_limit_excess(&mem
->res
);
523 * We have to update the tree if mz is on RB-tree or
524 * mem is over its softlimit.
526 if (excess
|| mz
->on_tree
) {
527 spin_lock(&mctz
->lock
);
528 /* if on-tree, remove it */
530 __mem_cgroup_remove_exceeded(mem
, mz
, mctz
);
532 * Insert again. mz->usage_in_excess will be updated.
533 * If excess is 0, no tree ops.
535 __mem_cgroup_insert_exceeded(mem
, mz
, mctz
, excess
);
536 spin_unlock(&mctz
->lock
);
541 static void mem_cgroup_remove_from_trees(struct mem_cgroup
*mem
)
544 struct mem_cgroup_per_zone
*mz
;
545 struct mem_cgroup_tree_per_zone
*mctz
;
547 for_each_node_state(node
, N_POSSIBLE
) {
548 for (zone
= 0; zone
< MAX_NR_ZONES
; zone
++) {
549 mz
= mem_cgroup_zoneinfo(mem
, node
, zone
);
550 mctz
= soft_limit_tree_node_zone(node
, zone
);
551 mem_cgroup_remove_exceeded(mem
, mz
, mctz
);
556 static struct mem_cgroup_per_zone
*
557 __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone
*mctz
)
559 struct rb_node
*rightmost
= NULL
;
560 struct mem_cgroup_per_zone
*mz
;
564 rightmost
= rb_last(&mctz
->rb_root
);
566 goto done
; /* Nothing to reclaim from */
568 mz
= rb_entry(rightmost
, struct mem_cgroup_per_zone
, tree_node
);
570 * Remove the node now but someone else can add it back,
571 * we will to add it back at the end of reclaim to its correct
572 * position in the tree.
574 __mem_cgroup_remove_exceeded(mz
->mem
, mz
, mctz
);
575 if (!res_counter_soft_limit_excess(&mz
->mem
->res
) ||
576 !css_tryget(&mz
->mem
->css
))
582 static struct mem_cgroup_per_zone
*
583 mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone
*mctz
)
585 struct mem_cgroup_per_zone
*mz
;
587 spin_lock(&mctz
->lock
);
588 mz
= __mem_cgroup_largest_soft_limit_node(mctz
);
589 spin_unlock(&mctz
->lock
);
594 * Implementation Note: reading percpu statistics for memcg.
596 * Both of vmstat[] and percpu_counter has threshold and do periodic
597 * synchronization to implement "quick" read. There are trade-off between
598 * reading cost and precision of value. Then, we may have a chance to implement
599 * a periodic synchronizion of counter in memcg's counter.
601 * But this _read() function is used for user interface now. The user accounts
602 * memory usage by memory cgroup and he _always_ requires exact value because
603 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
604 * have to visit all online cpus and make sum. So, for now, unnecessary
605 * synchronization is not implemented. (just implemented for cpu hotplug)
607 * If there are kernel internal actions which can make use of some not-exact
608 * value, and reading all cpu value can be performance bottleneck in some
609 * common workload, threashold and synchonization as vmstat[] should be
612 static long mem_cgroup_read_stat(struct mem_cgroup
*mem
,
613 enum mem_cgroup_stat_index idx
)
619 for_each_online_cpu(cpu
)
620 val
+= per_cpu(mem
->stat
->count
[idx
], cpu
);
621 #ifdef CONFIG_HOTPLUG_CPU
622 spin_lock(&mem
->pcp_counter_lock
);
623 val
+= mem
->nocpu_base
.count
[idx
];
624 spin_unlock(&mem
->pcp_counter_lock
);
630 static void mem_cgroup_swap_statistics(struct mem_cgroup
*mem
,
633 int val
= (charge
) ? 1 : -1;
634 this_cpu_add(mem
->stat
->count
[MEM_CGROUP_STAT_SWAPOUT
], val
);
637 void mem_cgroup_pgfault(struct mem_cgroup
*mem
, int val
)
639 this_cpu_add(mem
->stat
->events
[MEM_CGROUP_EVENTS_PGFAULT
], val
);
642 void mem_cgroup_pgmajfault(struct mem_cgroup
*mem
, int val
)
644 this_cpu_add(mem
->stat
->events
[MEM_CGROUP_EVENTS_PGMAJFAULT
], val
);
647 static unsigned long mem_cgroup_read_events(struct mem_cgroup
*mem
,
648 enum mem_cgroup_events_index idx
)
650 unsigned long val
= 0;
653 for_each_online_cpu(cpu
)
654 val
+= per_cpu(mem
->stat
->events
[idx
], cpu
);
655 #ifdef CONFIG_HOTPLUG_CPU
656 spin_lock(&mem
->pcp_counter_lock
);
657 val
+= mem
->nocpu_base
.events
[idx
];
658 spin_unlock(&mem
->pcp_counter_lock
);
663 static void mem_cgroup_charge_statistics(struct mem_cgroup
*mem
,
664 bool file
, int nr_pages
)
669 __this_cpu_add(mem
->stat
->count
[MEM_CGROUP_STAT_CACHE
], nr_pages
);
671 __this_cpu_add(mem
->stat
->count
[MEM_CGROUP_STAT_RSS
], nr_pages
);
673 /* pagein of a big page is an event. So, ignore page size */
675 __this_cpu_inc(mem
->stat
->events
[MEM_CGROUP_EVENTS_PGPGIN
]);
677 __this_cpu_inc(mem
->stat
->events
[MEM_CGROUP_EVENTS_PGPGOUT
]);
678 nr_pages
= -nr_pages
; /* for event */
681 __this_cpu_add(mem
->stat
->events
[MEM_CGROUP_EVENTS_COUNT
], nr_pages
);
687 mem_cgroup_zone_nr_lru_pages(struct mem_cgroup
*mem
, int nid
, int zid
,
688 unsigned int lru_mask
)
690 struct mem_cgroup_per_zone
*mz
;
692 unsigned long ret
= 0;
694 mz
= mem_cgroup_zoneinfo(mem
, nid
, zid
);
697 if (BIT(l
) & lru_mask
)
698 ret
+= MEM_CGROUP_ZSTAT(mz
, l
);
704 mem_cgroup_node_nr_lru_pages(struct mem_cgroup
*mem
,
705 int nid
, unsigned int lru_mask
)
710 for (zid
= 0; zid
< MAX_NR_ZONES
; zid
++)
711 total
+= mem_cgroup_zone_nr_lru_pages(mem
, nid
, zid
, lru_mask
);
716 static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup
*mem
,
717 unsigned int lru_mask
)
722 for_each_node_state(nid
, N_HIGH_MEMORY
)
723 total
+= mem_cgroup_node_nr_lru_pages(mem
, nid
, lru_mask
);
727 static bool __memcg_event_check(struct mem_cgroup
*mem
, int target
)
729 unsigned long val
, next
;
731 val
= this_cpu_read(mem
->stat
->events
[MEM_CGROUP_EVENTS_COUNT
]);
732 next
= this_cpu_read(mem
->stat
->targets
[target
]);
733 /* from time_after() in jiffies.h */
734 return ((long)next
- (long)val
< 0);
737 static void __mem_cgroup_target_update(struct mem_cgroup
*mem
, int target
)
739 unsigned long val
, next
;
741 val
= this_cpu_read(mem
->stat
->events
[MEM_CGROUP_EVENTS_COUNT
]);
744 case MEM_CGROUP_TARGET_THRESH
:
745 next
= val
+ THRESHOLDS_EVENTS_TARGET
;
747 case MEM_CGROUP_TARGET_SOFTLIMIT
:
748 next
= val
+ SOFTLIMIT_EVENTS_TARGET
;
750 case MEM_CGROUP_TARGET_NUMAINFO
:
751 next
= val
+ NUMAINFO_EVENTS_TARGET
;
757 this_cpu_write(mem
->stat
->targets
[target
], next
);
761 * Check events in order.
764 static void memcg_check_events(struct mem_cgroup
*mem
, struct page
*page
)
766 /* threshold event is triggered in finer grain than soft limit */
767 if (unlikely(__memcg_event_check(mem
, MEM_CGROUP_TARGET_THRESH
))) {
768 mem_cgroup_threshold(mem
);
769 __mem_cgroup_target_update(mem
, MEM_CGROUP_TARGET_THRESH
);
770 if (unlikely(__memcg_event_check(mem
,
771 MEM_CGROUP_TARGET_SOFTLIMIT
))) {
772 mem_cgroup_update_tree(mem
, page
);
773 __mem_cgroup_target_update(mem
,
774 MEM_CGROUP_TARGET_SOFTLIMIT
);
777 if (unlikely(__memcg_event_check(mem
,
778 MEM_CGROUP_TARGET_NUMAINFO
))) {
779 atomic_inc(&mem
->numainfo_events
);
780 __mem_cgroup_target_update(mem
,
781 MEM_CGROUP_TARGET_NUMAINFO
);
787 static struct mem_cgroup
*mem_cgroup_from_cont(struct cgroup
*cont
)
789 return container_of(cgroup_subsys_state(cont
,
790 mem_cgroup_subsys_id
), struct mem_cgroup
,
794 struct mem_cgroup
*mem_cgroup_from_task(struct task_struct
*p
)
797 * mm_update_next_owner() may clear mm->owner to NULL
798 * if it races with swapoff, page migration, etc.
799 * So this can be called with p == NULL.
804 return container_of(task_subsys_state(p
, mem_cgroup_subsys_id
),
805 struct mem_cgroup
, css
);
808 struct mem_cgroup
*try_get_mem_cgroup_from_mm(struct mm_struct
*mm
)
810 struct mem_cgroup
*mem
= NULL
;
815 * Because we have no locks, mm->owner's may be being moved to other
816 * cgroup. We use css_tryget() here even if this looks
817 * pessimistic (rather than adding locks here).
821 mem
= mem_cgroup_from_task(rcu_dereference(mm
->owner
));
824 } while (!css_tryget(&mem
->css
));
829 /* The caller has to guarantee "mem" exists before calling this */
830 static struct mem_cgroup
*mem_cgroup_start_loop(struct mem_cgroup
*mem
)
832 struct cgroup_subsys_state
*css
;
835 if (!mem
) /* ROOT cgroup has the smallest ID */
836 return root_mem_cgroup
; /*css_put/get against root is ignored*/
837 if (!mem
->use_hierarchy
) {
838 if (css_tryget(&mem
->css
))
844 * searching a memory cgroup which has the smallest ID under given
845 * ROOT cgroup. (ID >= 1)
847 css
= css_get_next(&mem_cgroup_subsys
, 1, &mem
->css
, &found
);
848 if (css
&& css_tryget(css
))
849 mem
= container_of(css
, struct mem_cgroup
, css
);
856 static struct mem_cgroup
*mem_cgroup_get_next(struct mem_cgroup
*iter
,
857 struct mem_cgroup
*root
,
860 int nextid
= css_id(&iter
->css
) + 1;
863 struct cgroup_subsys_state
*css
;
865 hierarchy_used
= iter
->use_hierarchy
;
868 /* If no ROOT, walk all, ignore hierarchy */
869 if (!cond
|| (root
&& !hierarchy_used
))
873 root
= root_mem_cgroup
;
879 css
= css_get_next(&mem_cgroup_subsys
, nextid
,
881 if (css
&& css_tryget(css
))
882 iter
= container_of(css
, struct mem_cgroup
, css
);
884 /* If css is NULL, no more cgroups will be found */
886 } while (css
&& !iter
);
891 * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please
892 * be careful that "break" loop is not allowed. We have reference count.
893 * Instead of that modify "cond" to be false and "continue" to exit the loop.
895 #define for_each_mem_cgroup_tree_cond(iter, root, cond) \
896 for (iter = mem_cgroup_start_loop(root);\
898 iter = mem_cgroup_get_next(iter, root, cond))
900 #define for_each_mem_cgroup_tree(iter, root) \
901 for_each_mem_cgroup_tree_cond(iter, root, true)
903 #define for_each_mem_cgroup_all(iter) \
904 for_each_mem_cgroup_tree_cond(iter, NULL, true)
907 static inline bool mem_cgroup_is_root(struct mem_cgroup
*mem
)
909 return (mem
== root_mem_cgroup
);
912 void mem_cgroup_count_vm_event(struct mm_struct
*mm
, enum vm_event_item idx
)
914 struct mem_cgroup
*mem
;
920 mem
= mem_cgroup_from_task(rcu_dereference(mm
->owner
));
926 mem_cgroup_pgmajfault(mem
, 1);
929 mem_cgroup_pgfault(mem
, 1);
937 EXPORT_SYMBOL(mem_cgroup_count_vm_event
);
940 * Following LRU functions are allowed to be used without PCG_LOCK.
941 * Operations are called by routine of global LRU independently from memcg.
942 * What we have to take care of here is validness of pc->mem_cgroup.
944 * Changes to pc->mem_cgroup happens when
947 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
948 * It is added to LRU before charge.
949 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
950 * When moving account, the page is not on LRU. It's isolated.
953 void mem_cgroup_del_lru_list(struct page
*page
, enum lru_list lru
)
955 struct page_cgroup
*pc
;
956 struct mem_cgroup_per_zone
*mz
;
958 if (mem_cgroup_disabled())
960 pc
= lookup_page_cgroup(page
);
961 /* can happen while we handle swapcache. */
962 if (!TestClearPageCgroupAcctLRU(pc
))
964 VM_BUG_ON(!pc
->mem_cgroup
);
966 * We don't check PCG_USED bit. It's cleared when the "page" is finally
967 * removed from global LRU.
969 mz
= page_cgroup_zoneinfo(pc
->mem_cgroup
, page
);
970 /* huge page split is done under lru_lock. so, we have no races. */
971 MEM_CGROUP_ZSTAT(mz
, lru
) -= 1 << compound_order(page
);
972 if (mem_cgroup_is_root(pc
->mem_cgroup
))
974 VM_BUG_ON(list_empty(&pc
->lru
));
975 list_del_init(&pc
->lru
);
978 void mem_cgroup_del_lru(struct page
*page
)
980 mem_cgroup_del_lru_list(page
, page_lru(page
));
984 * Writeback is about to end against a page which has been marked for immediate
985 * reclaim. If it still appears to be reclaimable, move it to the tail of the
988 void mem_cgroup_rotate_reclaimable_page(struct page
*page
)
990 struct mem_cgroup_per_zone
*mz
;
991 struct page_cgroup
*pc
;
992 enum lru_list lru
= page_lru(page
);
994 if (mem_cgroup_disabled())
997 pc
= lookup_page_cgroup(page
);
998 /* unused or root page is not rotated. */
999 if (!PageCgroupUsed(pc
))
1001 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1003 if (mem_cgroup_is_root(pc
->mem_cgroup
))
1005 mz
= page_cgroup_zoneinfo(pc
->mem_cgroup
, page
);
1006 list_move_tail(&pc
->lru
, &mz
->lists
[lru
]);
1009 void mem_cgroup_rotate_lru_list(struct page
*page
, enum lru_list lru
)
1011 struct mem_cgroup_per_zone
*mz
;
1012 struct page_cgroup
*pc
;
1014 if (mem_cgroup_disabled())
1017 pc
= lookup_page_cgroup(page
);
1018 /* unused or root page is not rotated. */
1019 if (!PageCgroupUsed(pc
))
1021 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1023 if (mem_cgroup_is_root(pc
->mem_cgroup
))
1025 mz
= page_cgroup_zoneinfo(pc
->mem_cgroup
, page
);
1026 list_move(&pc
->lru
, &mz
->lists
[lru
]);
1029 void mem_cgroup_add_lru_list(struct page
*page
, enum lru_list lru
)
1031 struct page_cgroup
*pc
;
1032 struct mem_cgroup_per_zone
*mz
;
1034 if (mem_cgroup_disabled())
1036 pc
= lookup_page_cgroup(page
);
1037 VM_BUG_ON(PageCgroupAcctLRU(pc
));
1038 if (!PageCgroupUsed(pc
))
1040 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1042 mz
= page_cgroup_zoneinfo(pc
->mem_cgroup
, page
);
1043 /* huge page split is done under lru_lock. so, we have no races. */
1044 MEM_CGROUP_ZSTAT(mz
, lru
) += 1 << compound_order(page
);
1045 SetPageCgroupAcctLRU(pc
);
1046 if (mem_cgroup_is_root(pc
->mem_cgroup
))
1048 list_add(&pc
->lru
, &mz
->lists
[lru
]);
1052 * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed
1053 * while it's linked to lru because the page may be reused after it's fully
1054 * uncharged. To handle that, unlink page_cgroup from LRU when charge it again.
1055 * It's done under lock_page and expected that zone->lru_lock isnever held.
1057 static void mem_cgroup_lru_del_before_commit(struct page
*page
)
1059 unsigned long flags
;
1060 struct zone
*zone
= page_zone(page
);
1061 struct page_cgroup
*pc
= lookup_page_cgroup(page
);
1064 * Doing this check without taking ->lru_lock seems wrong but this
1065 * is safe. Because if page_cgroup's USED bit is unset, the page
1066 * will not be added to any memcg's LRU. If page_cgroup's USED bit is
1067 * set, the commit after this will fail, anyway.
1068 * This all charge/uncharge is done under some mutual execustion.
1069 * So, we don't need to taking care of changes in USED bit.
1071 if (likely(!PageLRU(page
)))
1074 spin_lock_irqsave(&zone
->lru_lock
, flags
);
1076 * Forget old LRU when this page_cgroup is *not* used. This Used bit
1077 * is guarded by lock_page() because the page is SwapCache.
1079 if (!PageCgroupUsed(pc
))
1080 mem_cgroup_del_lru_list(page
, page_lru(page
));
1081 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
1084 static void mem_cgroup_lru_add_after_commit(struct page
*page
)
1086 unsigned long flags
;
1087 struct zone
*zone
= page_zone(page
);
1088 struct page_cgroup
*pc
= lookup_page_cgroup(page
);
1090 /* taking care of that the page is added to LRU while we commit it */
1091 if (likely(!PageLRU(page
)))
1093 spin_lock_irqsave(&zone
->lru_lock
, flags
);
1094 /* link when the page is linked to LRU but page_cgroup isn't */
1095 if (PageLRU(page
) && !PageCgroupAcctLRU(pc
))
1096 mem_cgroup_add_lru_list(page
, page_lru(page
));
1097 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
1101 void mem_cgroup_move_lists(struct page
*page
,
1102 enum lru_list from
, enum lru_list to
)
1104 if (mem_cgroup_disabled())
1106 mem_cgroup_del_lru_list(page
, from
);
1107 mem_cgroup_add_lru_list(page
, to
);
1111 * Checks whether given mem is same or in the root_mem's
1114 static bool mem_cgroup_same_or_subtree(const struct mem_cgroup
*root_mem
,
1115 struct mem_cgroup
*mem
)
1117 if (root_mem
!= mem
) {
1118 return (root_mem
->use_hierarchy
&&
1119 css_is_ancestor(&mem
->css
, &root_mem
->css
));
1125 int task_in_mem_cgroup(struct task_struct
*task
, const struct mem_cgroup
*mem
)
1128 struct mem_cgroup
*curr
= NULL
;
1129 struct task_struct
*p
;
1131 p
= find_lock_task_mm(task
);
1134 curr
= try_get_mem_cgroup_from_mm(p
->mm
);
1139 * We should check use_hierarchy of "mem" not "curr". Because checking
1140 * use_hierarchy of "curr" here make this function true if hierarchy is
1141 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
1142 * hierarchy(even if use_hierarchy is disabled in "mem").
1144 ret
= mem_cgroup_same_or_subtree(mem
, curr
);
1145 css_put(&curr
->css
);
1149 static int calc_inactive_ratio(struct mem_cgroup
*memcg
, unsigned long *present_pages
)
1151 unsigned long active
;
1152 unsigned long inactive
;
1154 unsigned long inactive_ratio
;
1156 inactive
= mem_cgroup_nr_lru_pages(memcg
, BIT(LRU_INACTIVE_ANON
));
1157 active
= mem_cgroup_nr_lru_pages(memcg
, BIT(LRU_ACTIVE_ANON
));
1159 gb
= (inactive
+ active
) >> (30 - PAGE_SHIFT
);
1161 inactive_ratio
= int_sqrt(10 * gb
);
1165 if (present_pages
) {
1166 present_pages
[0] = inactive
;
1167 present_pages
[1] = active
;
1170 return inactive_ratio
;
1173 int mem_cgroup_inactive_anon_is_low(struct mem_cgroup
*memcg
)
1175 unsigned long active
;
1176 unsigned long inactive
;
1177 unsigned long present_pages
[2];
1178 unsigned long inactive_ratio
;
1180 inactive_ratio
= calc_inactive_ratio(memcg
, present_pages
);
1182 inactive
= present_pages
[0];
1183 active
= present_pages
[1];
1185 if (inactive
* inactive_ratio
< active
)
1191 int mem_cgroup_inactive_file_is_low(struct mem_cgroup
*memcg
)
1193 unsigned long active
;
1194 unsigned long inactive
;
1196 inactive
= mem_cgroup_nr_lru_pages(memcg
, BIT(LRU_INACTIVE_FILE
));
1197 active
= mem_cgroup_nr_lru_pages(memcg
, BIT(LRU_ACTIVE_FILE
));
1199 return (active
> inactive
);
1202 struct zone_reclaim_stat
*mem_cgroup_get_reclaim_stat(struct mem_cgroup
*memcg
,
1205 int nid
= zone_to_nid(zone
);
1206 int zid
= zone_idx(zone
);
1207 struct mem_cgroup_per_zone
*mz
= mem_cgroup_zoneinfo(memcg
, nid
, zid
);
1209 return &mz
->reclaim_stat
;
1212 struct zone_reclaim_stat
*
1213 mem_cgroup_get_reclaim_stat_from_page(struct page
*page
)
1215 struct page_cgroup
*pc
;
1216 struct mem_cgroup_per_zone
*mz
;
1218 if (mem_cgroup_disabled())
1221 pc
= lookup_page_cgroup(page
);
1222 if (!PageCgroupUsed(pc
))
1224 /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */
1226 mz
= page_cgroup_zoneinfo(pc
->mem_cgroup
, page
);
1227 return &mz
->reclaim_stat
;
1230 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan
,
1231 struct list_head
*dst
,
1232 unsigned long *scanned
, int order
,
1233 int mode
, struct zone
*z
,
1234 struct mem_cgroup
*mem_cont
,
1235 int active
, int file
)
1237 unsigned long nr_taken
= 0;
1241 struct list_head
*src
;
1242 struct page_cgroup
*pc
, *tmp
;
1243 int nid
= zone_to_nid(z
);
1244 int zid
= zone_idx(z
);
1245 struct mem_cgroup_per_zone
*mz
;
1246 int lru
= LRU_FILE
* file
+ active
;
1250 mz
= mem_cgroup_zoneinfo(mem_cont
, nid
, zid
);
1251 src
= &mz
->lists
[lru
];
1254 list_for_each_entry_safe_reverse(pc
, tmp
, src
, lru
) {
1255 if (scan
>= nr_to_scan
)
1258 if (unlikely(!PageCgroupUsed(pc
)))
1261 page
= lookup_cgroup_page(pc
);
1263 if (unlikely(!PageLRU(page
)))
1267 ret
= __isolate_lru_page(page
, mode
, file
);
1270 list_move(&page
->lru
, dst
);
1271 mem_cgroup_del_lru(page
);
1272 nr_taken
+= hpage_nr_pages(page
);
1275 /* we don't affect global LRU but rotate in our LRU */
1276 mem_cgroup_rotate_lru_list(page
, page_lru(page
));
1285 trace_mm_vmscan_memcg_isolate(0, nr_to_scan
, scan
, nr_taken
,
1291 #define mem_cgroup_from_res_counter(counter, member) \
1292 container_of(counter, struct mem_cgroup, member)
1295 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
1296 * @mem: the memory cgroup
1298 * Returns the maximum amount of memory @mem can be charged with, in
1301 static unsigned long mem_cgroup_margin(struct mem_cgroup
*mem
)
1303 unsigned long long margin
;
1305 margin
= res_counter_margin(&mem
->res
);
1306 if (do_swap_account
)
1307 margin
= min(margin
, res_counter_margin(&mem
->memsw
));
1308 return margin
>> PAGE_SHIFT
;
1311 int mem_cgroup_swappiness(struct mem_cgroup
*memcg
)
1313 struct cgroup
*cgrp
= memcg
->css
.cgroup
;
1316 if (cgrp
->parent
== NULL
)
1317 return vm_swappiness
;
1319 return memcg
->swappiness
;
1322 static void mem_cgroup_start_move(struct mem_cgroup
*mem
)
1327 spin_lock(&mem
->pcp_counter_lock
);
1328 for_each_online_cpu(cpu
)
1329 per_cpu(mem
->stat
->count
[MEM_CGROUP_ON_MOVE
], cpu
) += 1;
1330 mem
->nocpu_base
.count
[MEM_CGROUP_ON_MOVE
] += 1;
1331 spin_unlock(&mem
->pcp_counter_lock
);
1337 static void mem_cgroup_end_move(struct mem_cgroup
*mem
)
1344 spin_lock(&mem
->pcp_counter_lock
);
1345 for_each_online_cpu(cpu
)
1346 per_cpu(mem
->stat
->count
[MEM_CGROUP_ON_MOVE
], cpu
) -= 1;
1347 mem
->nocpu_base
.count
[MEM_CGROUP_ON_MOVE
] -= 1;
1348 spin_unlock(&mem
->pcp_counter_lock
);
1352 * 2 routines for checking "mem" is under move_account() or not.
1354 * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used
1355 * for avoiding race in accounting. If true,
1356 * pc->mem_cgroup may be overwritten.
1358 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1359 * under hierarchy of moving cgroups. This is for
1360 * waiting at hith-memory prressure caused by "move".
1363 static bool mem_cgroup_stealed(struct mem_cgroup
*mem
)
1365 VM_BUG_ON(!rcu_read_lock_held());
1366 return this_cpu_read(mem
->stat
->count
[MEM_CGROUP_ON_MOVE
]) > 0;
1369 static bool mem_cgroup_under_move(struct mem_cgroup
*mem
)
1371 struct mem_cgroup
*from
;
1372 struct mem_cgroup
*to
;
1375 * Unlike task_move routines, we access mc.to, mc.from not under
1376 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1378 spin_lock(&mc
.lock
);
1384 ret
= mem_cgroup_same_or_subtree(mem
, from
)
1385 || mem_cgroup_same_or_subtree(mem
, to
);
1387 spin_unlock(&mc
.lock
);
1391 static bool mem_cgroup_wait_acct_move(struct mem_cgroup
*mem
)
1393 if (mc
.moving_task
&& current
!= mc
.moving_task
) {
1394 if (mem_cgroup_under_move(mem
)) {
1396 prepare_to_wait(&mc
.waitq
, &wait
, TASK_INTERRUPTIBLE
);
1397 /* moving charge context might have finished. */
1400 finish_wait(&mc
.waitq
, &wait
);
1408 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
1409 * @memcg: The memory cgroup that went over limit
1410 * @p: Task that is going to be killed
1412 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1415 void mem_cgroup_print_oom_info(struct mem_cgroup
*memcg
, struct task_struct
*p
)
1417 struct cgroup
*task_cgrp
;
1418 struct cgroup
*mem_cgrp
;
1420 * Need a buffer in BSS, can't rely on allocations. The code relies
1421 * on the assumption that OOM is serialized for memory controller.
1422 * If this assumption is broken, revisit this code.
1424 static char memcg_name
[PATH_MAX
];
1433 mem_cgrp
= memcg
->css
.cgroup
;
1434 task_cgrp
= task_cgroup(p
, mem_cgroup_subsys_id
);
1436 ret
= cgroup_path(task_cgrp
, memcg_name
, PATH_MAX
);
1439 * Unfortunately, we are unable to convert to a useful name
1440 * But we'll still print out the usage information
1447 printk(KERN_INFO
"Task in %s killed", memcg_name
);
1450 ret
= cgroup_path(mem_cgrp
, memcg_name
, PATH_MAX
);
1458 * Continues from above, so we don't need an KERN_ level
1460 printk(KERN_CONT
" as a result of limit of %s\n", memcg_name
);
1463 printk(KERN_INFO
"memory: usage %llukB, limit %llukB, failcnt %llu\n",
1464 res_counter_read_u64(&memcg
->res
, RES_USAGE
) >> 10,
1465 res_counter_read_u64(&memcg
->res
, RES_LIMIT
) >> 10,
1466 res_counter_read_u64(&memcg
->res
, RES_FAILCNT
));
1467 printk(KERN_INFO
"memory+swap: usage %llukB, limit %llukB, "
1469 res_counter_read_u64(&memcg
->memsw
, RES_USAGE
) >> 10,
1470 res_counter_read_u64(&memcg
->memsw
, RES_LIMIT
) >> 10,
1471 res_counter_read_u64(&memcg
->memsw
, RES_FAILCNT
));
1475 * This function returns the number of memcg under hierarchy tree. Returns
1476 * 1(self count) if no children.
1478 static int mem_cgroup_count_children(struct mem_cgroup
*mem
)
1481 struct mem_cgroup
*iter
;
1483 for_each_mem_cgroup_tree(iter
, mem
)
1489 * Return the memory (and swap, if configured) limit for a memcg.
1491 u64
mem_cgroup_get_limit(struct mem_cgroup
*memcg
)
1496 limit
= res_counter_read_u64(&memcg
->res
, RES_LIMIT
);
1497 limit
+= total_swap_pages
<< PAGE_SHIFT
;
1499 memsw
= res_counter_read_u64(&memcg
->memsw
, RES_LIMIT
);
1501 * If memsw is finite and limits the amount of swap space available
1502 * to this memcg, return that limit.
1504 return min(limit
, memsw
);
1508 * Visit the first child (need not be the first child as per the ordering
1509 * of the cgroup list, since we track last_scanned_child) of @mem and use
1510 * that to reclaim free pages from.
1512 static struct mem_cgroup
*
1513 mem_cgroup_select_victim(struct mem_cgroup
*root_mem
)
1515 struct mem_cgroup
*ret
= NULL
;
1516 struct cgroup_subsys_state
*css
;
1519 if (!root_mem
->use_hierarchy
) {
1520 css_get(&root_mem
->css
);
1526 nextid
= root_mem
->last_scanned_child
+ 1;
1527 css
= css_get_next(&mem_cgroup_subsys
, nextid
, &root_mem
->css
,
1529 if (css
&& css_tryget(css
))
1530 ret
= container_of(css
, struct mem_cgroup
, css
);
1533 /* Updates scanning parameter */
1535 /* this means start scan from ID:1 */
1536 root_mem
->last_scanned_child
= 0;
1538 root_mem
->last_scanned_child
= found
;
1545 * test_mem_cgroup_node_reclaimable
1546 * @mem: the target memcg
1547 * @nid: the node ID to be checked.
1548 * @noswap : specify true here if the user wants flle only information.
1550 * This function returns whether the specified memcg contains any
1551 * reclaimable pages on a node. Returns true if there are any reclaimable
1552 * pages in the node.
1554 static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup
*mem
,
1555 int nid
, bool noswap
)
1557 if (mem_cgroup_node_nr_lru_pages(mem
, nid
, LRU_ALL_FILE
))
1559 if (noswap
|| !total_swap_pages
)
1561 if (mem_cgroup_node_nr_lru_pages(mem
, nid
, LRU_ALL_ANON
))
1566 #if MAX_NUMNODES > 1
1569 * Always updating the nodemask is not very good - even if we have an empty
1570 * list or the wrong list here, we can start from some node and traverse all
1571 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1574 static void mem_cgroup_may_update_nodemask(struct mem_cgroup
*mem
)
1578 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1579 * pagein/pageout changes since the last update.
1581 if (!atomic_read(&mem
->numainfo_events
))
1583 if (atomic_inc_return(&mem
->numainfo_updating
) > 1)
1586 /* make a nodemask where this memcg uses memory from */
1587 mem
->scan_nodes
= node_states
[N_HIGH_MEMORY
];
1589 for_each_node_mask(nid
, node_states
[N_HIGH_MEMORY
]) {
1591 if (!test_mem_cgroup_node_reclaimable(mem
, nid
, false))
1592 node_clear(nid
, mem
->scan_nodes
);
1595 atomic_set(&mem
->numainfo_events
, 0);
1596 atomic_set(&mem
->numainfo_updating
, 0);
1600 * Selecting a node where we start reclaim from. Because what we need is just
1601 * reducing usage counter, start from anywhere is O,K. Considering
1602 * memory reclaim from current node, there are pros. and cons.
1604 * Freeing memory from current node means freeing memory from a node which
1605 * we'll use or we've used. So, it may make LRU bad. And if several threads
1606 * hit limits, it will see a contention on a node. But freeing from remote
1607 * node means more costs for memory reclaim because of memory latency.
1609 * Now, we use round-robin. Better algorithm is welcomed.
1611 int mem_cgroup_select_victim_node(struct mem_cgroup
*mem
)
1615 mem_cgroup_may_update_nodemask(mem
);
1616 node
= mem
->last_scanned_node
;
1618 node
= next_node(node
, mem
->scan_nodes
);
1619 if (node
== MAX_NUMNODES
)
1620 node
= first_node(mem
->scan_nodes
);
1622 * We call this when we hit limit, not when pages are added to LRU.
1623 * No LRU may hold pages because all pages are UNEVICTABLE or
1624 * memcg is too small and all pages are not on LRU. In that case,
1625 * we use curret node.
1627 if (unlikely(node
== MAX_NUMNODES
))
1628 node
= numa_node_id();
1630 mem
->last_scanned_node
= node
;
1635 * Check all nodes whether it contains reclaimable pages or not.
1636 * For quick scan, we make use of scan_nodes. This will allow us to skip
1637 * unused nodes. But scan_nodes is lazily updated and may not cotain
1638 * enough new information. We need to do double check.
1640 bool mem_cgroup_reclaimable(struct mem_cgroup
*mem
, bool noswap
)
1645 * quick check...making use of scan_node.
1646 * We can skip unused nodes.
1648 if (!nodes_empty(mem
->scan_nodes
)) {
1649 for (nid
= first_node(mem
->scan_nodes
);
1651 nid
= next_node(nid
, mem
->scan_nodes
)) {
1653 if (test_mem_cgroup_node_reclaimable(mem
, nid
, noswap
))
1658 * Check rest of nodes.
1660 for_each_node_state(nid
, N_HIGH_MEMORY
) {
1661 if (node_isset(nid
, mem
->scan_nodes
))
1663 if (test_mem_cgroup_node_reclaimable(mem
, nid
, noswap
))
1670 int mem_cgroup_select_victim_node(struct mem_cgroup
*mem
)
1675 bool mem_cgroup_reclaimable(struct mem_cgroup
*mem
, bool noswap
)
1677 return test_mem_cgroup_node_reclaimable(mem
, 0, noswap
);
1681 static void __mem_cgroup_record_scanstat(unsigned long *stats
,
1682 struct memcg_scanrecord
*rec
)
1685 stats
[SCAN
] += rec
->nr_scanned
[0] + rec
->nr_scanned
[1];
1686 stats
[SCAN_ANON
] += rec
->nr_scanned
[0];
1687 stats
[SCAN_FILE
] += rec
->nr_scanned
[1];
1689 stats
[ROTATE
] += rec
->nr_rotated
[0] + rec
->nr_rotated
[1];
1690 stats
[ROTATE_ANON
] += rec
->nr_rotated
[0];
1691 stats
[ROTATE_FILE
] += rec
->nr_rotated
[1];
1693 stats
[FREED
] += rec
->nr_freed
[0] + rec
->nr_freed
[1];
1694 stats
[FREED_ANON
] += rec
->nr_freed
[0];
1695 stats
[FREED_FILE
] += rec
->nr_freed
[1];
1697 stats
[ELAPSED
] += rec
->elapsed
;
1700 static void mem_cgroup_record_scanstat(struct memcg_scanrecord
*rec
)
1702 struct mem_cgroup
*mem
;
1703 int context
= rec
->context
;
1705 if (context
>= NR_SCAN_CONTEXT
)
1709 spin_lock(&mem
->scanstat
.lock
);
1710 __mem_cgroup_record_scanstat(mem
->scanstat
.stats
[context
], rec
);
1711 spin_unlock(&mem
->scanstat
.lock
);
1714 spin_lock(&mem
->scanstat
.lock
);
1715 __mem_cgroup_record_scanstat(mem
->scanstat
.rootstats
[context
], rec
);
1716 spin_unlock(&mem
->scanstat
.lock
);
1720 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1721 * we reclaimed from, so that we don't end up penalizing one child extensively
1722 * based on its position in the children list.
1724 * root_mem is the original ancestor that we've been reclaim from.
1726 * We give up and return to the caller when we visit root_mem twice.
1727 * (other groups can be removed while we're walking....)
1729 * If shrink==true, for avoiding to free too much, this returns immedieately.
1731 static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup
*root_mem
,
1734 unsigned long reclaim_options
,
1735 unsigned long *total_scanned
)
1737 struct mem_cgroup
*victim
;
1740 bool noswap
= reclaim_options
& MEM_CGROUP_RECLAIM_NOSWAP
;
1741 bool shrink
= reclaim_options
& MEM_CGROUP_RECLAIM_SHRINK
;
1742 bool check_soft
= reclaim_options
& MEM_CGROUP_RECLAIM_SOFT
;
1743 struct memcg_scanrecord rec
;
1744 unsigned long excess
;
1745 unsigned long scanned
;
1747 excess
= res_counter_soft_limit_excess(&root_mem
->res
) >> PAGE_SHIFT
;
1749 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1750 if (!check_soft
&& !shrink
&& root_mem
->memsw_is_minimum
)
1754 rec
.context
= SCAN_BY_SHRINK
;
1755 else if (check_soft
)
1756 rec
.context
= SCAN_BY_SYSTEM
;
1758 rec
.context
= SCAN_BY_LIMIT
;
1760 rec
.root
= root_mem
;
1763 victim
= mem_cgroup_select_victim(root_mem
);
1764 if (victim
== root_mem
) {
1767 * We are not draining per cpu cached charges during
1768 * soft limit reclaim because global reclaim doesn't
1769 * care about charges. It tries to free some memory and
1770 * charges will not give any.
1772 if (!check_soft
&& loop
>= 1)
1773 drain_all_stock_async(root_mem
);
1776 * If we have not been able to reclaim
1777 * anything, it might because there are
1778 * no reclaimable pages under this hierarchy
1780 if (!check_soft
|| !total
) {
1781 css_put(&victim
->css
);
1785 * We want to do more targeted reclaim.
1786 * excess >> 2 is not to excessive so as to
1787 * reclaim too much, nor too less that we keep
1788 * coming back to reclaim from this cgroup
1790 if (total
>= (excess
>> 2) ||
1791 (loop
> MEM_CGROUP_MAX_RECLAIM_LOOPS
)) {
1792 css_put(&victim
->css
);
1797 if (!mem_cgroup_reclaimable(victim
, noswap
)) {
1798 /* this cgroup's local usage == 0 */
1799 css_put(&victim
->css
);
1803 rec
.nr_scanned
[0] = 0;
1804 rec
.nr_scanned
[1] = 0;
1805 rec
.nr_rotated
[0] = 0;
1806 rec
.nr_rotated
[1] = 0;
1807 rec
.nr_freed
[0] = 0;
1808 rec
.nr_freed
[1] = 0;
1810 /* we use swappiness of local cgroup */
1812 ret
= mem_cgroup_shrink_node_zone(victim
, gfp_mask
,
1813 noswap
, zone
, &rec
, &scanned
);
1814 *total_scanned
+= scanned
;
1816 ret
= try_to_free_mem_cgroup_pages(victim
, gfp_mask
,
1818 mem_cgroup_record_scanstat(&rec
);
1819 css_put(&victim
->css
);
1821 * At shrinking usage, we can't check we should stop here or
1822 * reclaim more. It's depends on callers. last_scanned_child
1823 * will work enough for keeping fairness under tree.
1829 if (!res_counter_soft_limit_excess(&root_mem
->res
))
1831 } else if (mem_cgroup_margin(root_mem
))
1838 * Check OOM-Killer is already running under our hierarchy.
1839 * If someone is running, return false.
1840 * Has to be called with memcg_oom_lock
1842 static bool mem_cgroup_oom_lock(struct mem_cgroup
*mem
)
1844 int lock_count
= -1;
1845 struct mem_cgroup
*iter
, *failed
= NULL
;
1848 for_each_mem_cgroup_tree_cond(iter
, mem
, cond
) {
1849 bool locked
= iter
->oom_lock
;
1851 iter
->oom_lock
= true;
1852 if (lock_count
== -1)
1853 lock_count
= iter
->oom_lock
;
1854 else if (lock_count
!= locked
) {
1856 * this subtree of our hierarchy is already locked
1857 * so we cannot give a lock.
1869 * OK, we failed to lock the whole subtree so we have to clean up
1870 * what we set up to the failing subtree
1873 for_each_mem_cgroup_tree_cond(iter
, mem
, cond
) {
1874 if (iter
== failed
) {
1878 iter
->oom_lock
= false;
1885 * Has to be called with memcg_oom_lock
1887 static int mem_cgroup_oom_unlock(struct mem_cgroup
*mem
)
1889 struct mem_cgroup
*iter
;
1891 for_each_mem_cgroup_tree(iter
, mem
)
1892 iter
->oom_lock
= false;
1896 static void mem_cgroup_mark_under_oom(struct mem_cgroup
*mem
)
1898 struct mem_cgroup
*iter
;
1900 for_each_mem_cgroup_tree(iter
, mem
)
1901 atomic_inc(&iter
->under_oom
);
1904 static void mem_cgroup_unmark_under_oom(struct mem_cgroup
*mem
)
1906 struct mem_cgroup
*iter
;
1909 * When a new child is created while the hierarchy is under oom,
1910 * mem_cgroup_oom_lock() may not be called. We have to use
1911 * atomic_add_unless() here.
1913 for_each_mem_cgroup_tree(iter
, mem
)
1914 atomic_add_unless(&iter
->under_oom
, -1, 0);
1917 static DEFINE_SPINLOCK(memcg_oom_lock
);
1918 static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq
);
1920 struct oom_wait_info
{
1921 struct mem_cgroup
*mem
;
1925 static int memcg_oom_wake_function(wait_queue_t
*wait
,
1926 unsigned mode
, int sync
, void *arg
)
1928 struct mem_cgroup
*wake_mem
= (struct mem_cgroup
*)arg
,
1930 struct oom_wait_info
*oom_wait_info
;
1932 oom_wait_info
= container_of(wait
, struct oom_wait_info
, wait
);
1933 oom_wait_mem
= oom_wait_info
->mem
;
1936 * Both of oom_wait_info->mem and wake_mem are stable under us.
1937 * Then we can use css_is_ancestor without taking care of RCU.
1939 if (!mem_cgroup_same_or_subtree(oom_wait_mem
, wake_mem
)
1940 && !mem_cgroup_same_or_subtree(wake_mem
, oom_wait_mem
))
1942 return autoremove_wake_function(wait
, mode
, sync
, arg
);
1945 static void memcg_wakeup_oom(struct mem_cgroup
*mem
)
1947 /* for filtering, pass "mem" as argument. */
1948 __wake_up(&memcg_oom_waitq
, TASK_NORMAL
, 0, mem
);
1951 static void memcg_oom_recover(struct mem_cgroup
*mem
)
1953 if (mem
&& atomic_read(&mem
->under_oom
))
1954 memcg_wakeup_oom(mem
);
1958 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1960 bool mem_cgroup_handle_oom(struct mem_cgroup
*mem
, gfp_t mask
)
1962 struct oom_wait_info owait
;
1963 bool locked
, need_to_kill
;
1966 owait
.wait
.flags
= 0;
1967 owait
.wait
.func
= memcg_oom_wake_function
;
1968 owait
.wait
.private = current
;
1969 INIT_LIST_HEAD(&owait
.wait
.task_list
);
1970 need_to_kill
= true;
1971 mem_cgroup_mark_under_oom(mem
);
1973 /* At first, try to OOM lock hierarchy under mem.*/
1974 spin_lock(&memcg_oom_lock
);
1975 locked
= mem_cgroup_oom_lock(mem
);
1977 * Even if signal_pending(), we can't quit charge() loop without
1978 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1979 * under OOM is always welcomed, use TASK_KILLABLE here.
1981 prepare_to_wait(&memcg_oom_waitq
, &owait
.wait
, TASK_KILLABLE
);
1982 if (!locked
|| mem
->oom_kill_disable
)
1983 need_to_kill
= false;
1985 mem_cgroup_oom_notify(mem
);
1986 spin_unlock(&memcg_oom_lock
);
1989 finish_wait(&memcg_oom_waitq
, &owait
.wait
);
1990 mem_cgroup_out_of_memory(mem
, mask
);
1993 finish_wait(&memcg_oom_waitq
, &owait
.wait
);
1995 spin_lock(&memcg_oom_lock
);
1997 mem_cgroup_oom_unlock(mem
);
1998 memcg_wakeup_oom(mem
);
1999 spin_unlock(&memcg_oom_lock
);
2001 mem_cgroup_unmark_under_oom(mem
);
2003 if (test_thread_flag(TIF_MEMDIE
) || fatal_signal_pending(current
))
2005 /* Give chance to dying process */
2006 schedule_timeout(1);
2011 * Currently used to update mapped file statistics, but the routine can be
2012 * generalized to update other statistics as well.
2014 * Notes: Race condition
2016 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2017 * it tends to be costly. But considering some conditions, we doesn't need
2018 * to do so _always_.
2020 * Considering "charge", lock_page_cgroup() is not required because all
2021 * file-stat operations happen after a page is attached to radix-tree. There
2022 * are no race with "charge".
2024 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2025 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2026 * if there are race with "uncharge". Statistics itself is properly handled
2029 * Considering "move", this is an only case we see a race. To make the race
2030 * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are
2031 * possibility of race condition. If there is, we take a lock.
2034 void mem_cgroup_update_page_stat(struct page
*page
,
2035 enum mem_cgroup_page_stat_item idx
, int val
)
2037 struct mem_cgroup
*mem
;
2038 struct page_cgroup
*pc
= lookup_page_cgroup(page
);
2039 bool need_unlock
= false;
2040 unsigned long uninitialized_var(flags
);
2046 mem
= pc
->mem_cgroup
;
2047 if (unlikely(!mem
|| !PageCgroupUsed(pc
)))
2049 /* pc->mem_cgroup is unstable ? */
2050 if (unlikely(mem_cgroup_stealed(mem
)) || PageTransHuge(page
)) {
2051 /* take a lock against to access pc->mem_cgroup */
2052 move_lock_page_cgroup(pc
, &flags
);
2054 mem
= pc
->mem_cgroup
;
2055 if (!mem
|| !PageCgroupUsed(pc
))
2060 case MEMCG_NR_FILE_MAPPED
:
2062 SetPageCgroupFileMapped(pc
);
2063 else if (!page_mapped(page
))
2064 ClearPageCgroupFileMapped(pc
);
2065 idx
= MEM_CGROUP_STAT_FILE_MAPPED
;
2071 this_cpu_add(mem
->stat
->count
[idx
], val
);
2074 if (unlikely(need_unlock
))
2075 move_unlock_page_cgroup(pc
, &flags
);
2079 EXPORT_SYMBOL(mem_cgroup_update_page_stat
);
2082 * size of first charge trial. "32" comes from vmscan.c's magic value.
2083 * TODO: maybe necessary to use big numbers in big irons.
2085 #define CHARGE_BATCH 32U
2086 struct memcg_stock_pcp
{
2087 struct mem_cgroup
*cached
; /* this never be root cgroup */
2088 unsigned int nr_pages
;
2089 struct work_struct work
;
2090 unsigned long flags
;
2091 #define FLUSHING_CACHED_CHARGE (0)
2093 static DEFINE_PER_CPU(struct memcg_stock_pcp
, memcg_stock
);
2096 * Try to consume stocked charge on this cpu. If success, one page is consumed
2097 * from local stock and true is returned. If the stock is 0 or charges from a
2098 * cgroup which is not current target, returns false. This stock will be
2101 static bool consume_stock(struct mem_cgroup
*mem
)
2103 struct memcg_stock_pcp
*stock
;
2106 stock
= &get_cpu_var(memcg_stock
);
2107 if (mem
== stock
->cached
&& stock
->nr_pages
)
2109 else /* need to call res_counter_charge */
2111 put_cpu_var(memcg_stock
);
2116 * Returns stocks cached in percpu to res_counter and reset cached information.
2118 static void drain_stock(struct memcg_stock_pcp
*stock
)
2120 struct mem_cgroup
*old
= stock
->cached
;
2122 if (stock
->nr_pages
) {
2123 unsigned long bytes
= stock
->nr_pages
* PAGE_SIZE
;
2125 res_counter_uncharge(&old
->res
, bytes
);
2126 if (do_swap_account
)
2127 res_counter_uncharge(&old
->memsw
, bytes
);
2128 stock
->nr_pages
= 0;
2130 stock
->cached
= NULL
;
2134 * This must be called under preempt disabled or must be called by
2135 * a thread which is pinned to local cpu.
2137 static void drain_local_stock(struct work_struct
*dummy
)
2139 struct memcg_stock_pcp
*stock
= &__get_cpu_var(memcg_stock
);
2141 clear_bit(FLUSHING_CACHED_CHARGE
, &stock
->flags
);
2145 * Cache charges(val) which is from res_counter, to local per_cpu area.
2146 * This will be consumed by consume_stock() function, later.
2148 static void refill_stock(struct mem_cgroup
*mem
, unsigned int nr_pages
)
2150 struct memcg_stock_pcp
*stock
= &get_cpu_var(memcg_stock
);
2152 if (stock
->cached
!= mem
) { /* reset if necessary */
2154 stock
->cached
= mem
;
2156 stock
->nr_pages
+= nr_pages
;
2157 put_cpu_var(memcg_stock
);
2161 * Drains all per-CPU charge caches for given root_mem resp. subtree
2162 * of the hierarchy under it. sync flag says whether we should block
2163 * until the work is done.
2165 static void drain_all_stock(struct mem_cgroup
*root_mem
, bool sync
)
2169 /* Notify other cpus that system-wide "drain" is running */
2172 * Get a hint for avoiding draining charges on the current cpu,
2173 * which must be exhausted by our charging. It is not required that
2174 * this be a precise check, so we use raw_smp_processor_id() instead of
2175 * getcpu()/putcpu().
2177 curcpu
= raw_smp_processor_id();
2178 for_each_online_cpu(cpu
) {
2179 struct memcg_stock_pcp
*stock
= &per_cpu(memcg_stock
, cpu
);
2180 struct mem_cgroup
*mem
;
2182 mem
= stock
->cached
;
2183 if (!mem
|| !stock
->nr_pages
)
2185 if (!mem_cgroup_same_or_subtree(root_mem
, mem
))
2187 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE
, &stock
->flags
)) {
2189 drain_local_stock(&stock
->work
);
2191 schedule_work_on(cpu
, &stock
->work
);
2198 for_each_online_cpu(cpu
) {
2199 struct memcg_stock_pcp
*stock
= &per_cpu(memcg_stock
, cpu
);
2200 if (mem_cgroup_same_or_subtree(root_mem
, stock
->cached
) &&
2201 test_bit(FLUSHING_CACHED_CHARGE
, &stock
->flags
))
2202 flush_work(&stock
->work
);
2209 * Tries to drain stocked charges in other cpus. This function is asynchronous
2210 * and just put a work per cpu for draining localy on each cpu. Caller can
2211 * expects some charges will be back to res_counter later but cannot wait for
2214 static void drain_all_stock_async(struct mem_cgroup
*root_mem
)
2216 drain_all_stock(root_mem
, false);
2219 /* This is a synchronous drain interface. */
2220 static void drain_all_stock_sync(struct mem_cgroup
*root_mem
)
2222 /* called when force_empty is called */
2223 drain_all_stock(root_mem
, true);
2227 * This function drains percpu counter value from DEAD cpu and
2228 * move it to local cpu. Note that this function can be preempted.
2230 static void mem_cgroup_drain_pcp_counter(struct mem_cgroup
*mem
, int cpu
)
2234 spin_lock(&mem
->pcp_counter_lock
);
2235 for (i
= 0; i
< MEM_CGROUP_STAT_DATA
; i
++) {
2236 long x
= per_cpu(mem
->stat
->count
[i
], cpu
);
2238 per_cpu(mem
->stat
->count
[i
], cpu
) = 0;
2239 mem
->nocpu_base
.count
[i
] += x
;
2241 for (i
= 0; i
< MEM_CGROUP_EVENTS_NSTATS
; i
++) {
2242 unsigned long x
= per_cpu(mem
->stat
->events
[i
], cpu
);
2244 per_cpu(mem
->stat
->events
[i
], cpu
) = 0;
2245 mem
->nocpu_base
.events
[i
] += x
;
2247 /* need to clear ON_MOVE value, works as a kind of lock. */
2248 per_cpu(mem
->stat
->count
[MEM_CGROUP_ON_MOVE
], cpu
) = 0;
2249 spin_unlock(&mem
->pcp_counter_lock
);
2252 static void synchronize_mem_cgroup_on_move(struct mem_cgroup
*mem
, int cpu
)
2254 int idx
= MEM_CGROUP_ON_MOVE
;
2256 spin_lock(&mem
->pcp_counter_lock
);
2257 per_cpu(mem
->stat
->count
[idx
], cpu
) = mem
->nocpu_base
.count
[idx
];
2258 spin_unlock(&mem
->pcp_counter_lock
);
2261 static int __cpuinit
memcg_cpu_hotplug_callback(struct notifier_block
*nb
,
2262 unsigned long action
,
2265 int cpu
= (unsigned long)hcpu
;
2266 struct memcg_stock_pcp
*stock
;
2267 struct mem_cgroup
*iter
;
2269 if ((action
== CPU_ONLINE
)) {
2270 for_each_mem_cgroup_all(iter
)
2271 synchronize_mem_cgroup_on_move(iter
, cpu
);
2275 if ((action
!= CPU_DEAD
) || action
!= CPU_DEAD_FROZEN
)
2278 for_each_mem_cgroup_all(iter
)
2279 mem_cgroup_drain_pcp_counter(iter
, cpu
);
2281 stock
= &per_cpu(memcg_stock
, cpu
);
2287 /* See __mem_cgroup_try_charge() for details */
2289 CHARGE_OK
, /* success */
2290 CHARGE_RETRY
, /* need to retry but retry is not bad */
2291 CHARGE_NOMEM
, /* we can't do more. return -ENOMEM */
2292 CHARGE_WOULDBLOCK
, /* GFP_WAIT wasn't set and no enough res. */
2293 CHARGE_OOM_DIE
, /* the current is killed because of OOM */
2296 static int mem_cgroup_do_charge(struct mem_cgroup
*mem
, gfp_t gfp_mask
,
2297 unsigned int nr_pages
, bool oom_check
)
2299 unsigned long csize
= nr_pages
* PAGE_SIZE
;
2300 struct mem_cgroup
*mem_over_limit
;
2301 struct res_counter
*fail_res
;
2302 unsigned long flags
= 0;
2305 ret
= res_counter_charge(&mem
->res
, csize
, &fail_res
);
2308 if (!do_swap_account
)
2310 ret
= res_counter_charge(&mem
->memsw
, csize
, &fail_res
);
2314 res_counter_uncharge(&mem
->res
, csize
);
2315 mem_over_limit
= mem_cgroup_from_res_counter(fail_res
, memsw
);
2316 flags
|= MEM_CGROUP_RECLAIM_NOSWAP
;
2318 mem_over_limit
= mem_cgroup_from_res_counter(fail_res
, res
);
2320 * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch
2321 * of regular pages (CHARGE_BATCH), or a single regular page (1).
2323 * Never reclaim on behalf of optional batching, retry with a
2324 * single page instead.
2326 if (nr_pages
== CHARGE_BATCH
)
2327 return CHARGE_RETRY
;
2329 if (!(gfp_mask
& __GFP_WAIT
))
2330 return CHARGE_WOULDBLOCK
;
2332 ret
= mem_cgroup_hierarchical_reclaim(mem_over_limit
, NULL
,
2333 gfp_mask
, flags
, NULL
);
2334 if (mem_cgroup_margin(mem_over_limit
) >= nr_pages
)
2335 return CHARGE_RETRY
;
2337 * Even though the limit is exceeded at this point, reclaim
2338 * may have been able to free some pages. Retry the charge
2339 * before killing the task.
2341 * Only for regular pages, though: huge pages are rather
2342 * unlikely to succeed so close to the limit, and we fall back
2343 * to regular pages anyway in case of failure.
2345 if (nr_pages
== 1 && ret
)
2346 return CHARGE_RETRY
;
2349 * At task move, charge accounts can be doubly counted. So, it's
2350 * better to wait until the end of task_move if something is going on.
2352 if (mem_cgroup_wait_acct_move(mem_over_limit
))
2353 return CHARGE_RETRY
;
2355 /* If we don't need to call oom-killer at el, return immediately */
2357 return CHARGE_NOMEM
;
2359 if (!mem_cgroup_handle_oom(mem_over_limit
, gfp_mask
))
2360 return CHARGE_OOM_DIE
;
2362 return CHARGE_RETRY
;
2366 * Unlike exported interface, "oom" parameter is added. if oom==true,
2367 * oom-killer can be invoked.
2369 static int __mem_cgroup_try_charge(struct mm_struct
*mm
,
2371 unsigned int nr_pages
,
2372 struct mem_cgroup
**memcg
,
2375 unsigned int batch
= max(CHARGE_BATCH
, nr_pages
);
2376 int nr_oom_retries
= MEM_CGROUP_RECLAIM_RETRIES
;
2377 struct mem_cgroup
*mem
= NULL
;
2381 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2382 * in system level. So, allow to go ahead dying process in addition to
2385 if (unlikely(test_thread_flag(TIF_MEMDIE
)
2386 || fatal_signal_pending(current
)))
2390 * We always charge the cgroup the mm_struct belongs to.
2391 * The mm_struct's mem_cgroup changes on task migration if the
2392 * thread group leader migrates. It's possible that mm is not
2393 * set, if so charge the init_mm (happens for pagecache usage).
2398 if (*memcg
) { /* css should be a valid one */
2400 VM_BUG_ON(css_is_removed(&mem
->css
));
2401 if (mem_cgroup_is_root(mem
))
2403 if (nr_pages
== 1 && consume_stock(mem
))
2407 struct task_struct
*p
;
2410 p
= rcu_dereference(mm
->owner
);
2412 * Because we don't have task_lock(), "p" can exit.
2413 * In that case, "mem" can point to root or p can be NULL with
2414 * race with swapoff. Then, we have small risk of mis-accouning.
2415 * But such kind of mis-account by race always happens because
2416 * we don't have cgroup_mutex(). It's overkill and we allo that
2418 * (*) swapoff at el will charge against mm-struct not against
2419 * task-struct. So, mm->owner can be NULL.
2421 mem
= mem_cgroup_from_task(p
);
2422 if (!mem
|| mem_cgroup_is_root(mem
)) {
2426 if (nr_pages
== 1 && consume_stock(mem
)) {
2428 * It seems dagerous to access memcg without css_get().
2429 * But considering how consume_stok works, it's not
2430 * necessary. If consume_stock success, some charges
2431 * from this memcg are cached on this cpu. So, we
2432 * don't need to call css_get()/css_tryget() before
2433 * calling consume_stock().
2438 /* after here, we may be blocked. we need to get refcnt */
2439 if (!css_tryget(&mem
->css
)) {
2449 /* If killed, bypass charge */
2450 if (fatal_signal_pending(current
)) {
2456 if (oom
&& !nr_oom_retries
) {
2458 nr_oom_retries
= MEM_CGROUP_RECLAIM_RETRIES
;
2461 ret
= mem_cgroup_do_charge(mem
, gfp_mask
, batch
, oom_check
);
2465 case CHARGE_RETRY
: /* not in OOM situation but retry */
2470 case CHARGE_WOULDBLOCK
: /* !__GFP_WAIT */
2473 case CHARGE_NOMEM
: /* OOM routine works */
2478 /* If oom, we never return -ENOMEM */
2481 case CHARGE_OOM_DIE
: /* Killed by OOM Killer */
2485 } while (ret
!= CHARGE_OK
);
2487 if (batch
> nr_pages
)
2488 refill_stock(mem
, batch
- nr_pages
);
2502 * Somemtimes we have to undo a charge we got by try_charge().
2503 * This function is for that and do uncharge, put css's refcnt.
2504 * gotten by try_charge().
2506 static void __mem_cgroup_cancel_charge(struct mem_cgroup
*mem
,
2507 unsigned int nr_pages
)
2509 if (!mem_cgroup_is_root(mem
)) {
2510 unsigned long bytes
= nr_pages
* PAGE_SIZE
;
2512 res_counter_uncharge(&mem
->res
, bytes
);
2513 if (do_swap_account
)
2514 res_counter_uncharge(&mem
->memsw
, bytes
);
2519 * A helper function to get mem_cgroup from ID. must be called under
2520 * rcu_read_lock(). The caller must check css_is_removed() or some if
2521 * it's concern. (dropping refcnt from swap can be called against removed
2524 static struct mem_cgroup
*mem_cgroup_lookup(unsigned short id
)
2526 struct cgroup_subsys_state
*css
;
2528 /* ID 0 is unused ID */
2531 css
= css_lookup(&mem_cgroup_subsys
, id
);
2534 return container_of(css
, struct mem_cgroup
, css
);
2537 struct mem_cgroup
*try_get_mem_cgroup_from_page(struct page
*page
)
2539 struct mem_cgroup
*mem
= NULL
;
2540 struct page_cgroup
*pc
;
2544 VM_BUG_ON(!PageLocked(page
));
2546 pc
= lookup_page_cgroup(page
);
2547 lock_page_cgroup(pc
);
2548 if (PageCgroupUsed(pc
)) {
2549 mem
= pc
->mem_cgroup
;
2550 if (mem
&& !css_tryget(&mem
->css
))
2552 } else if (PageSwapCache(page
)) {
2553 ent
.val
= page_private(page
);
2554 id
= lookup_swap_cgroup(ent
);
2556 mem
= mem_cgroup_lookup(id
);
2557 if (mem
&& !css_tryget(&mem
->css
))
2561 unlock_page_cgroup(pc
);
2565 static void __mem_cgroup_commit_charge(struct mem_cgroup
*mem
,
2567 unsigned int nr_pages
,
2568 struct page_cgroup
*pc
,
2569 enum charge_type ctype
)
2571 lock_page_cgroup(pc
);
2572 if (unlikely(PageCgroupUsed(pc
))) {
2573 unlock_page_cgroup(pc
);
2574 __mem_cgroup_cancel_charge(mem
, nr_pages
);
2578 * we don't need page_cgroup_lock about tail pages, becase they are not
2579 * accessed by any other context at this point.
2581 pc
->mem_cgroup
= mem
;
2583 * We access a page_cgroup asynchronously without lock_page_cgroup().
2584 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2585 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2586 * before USED bit, we need memory barrier here.
2587 * See mem_cgroup_add_lru_list(), etc.
2591 case MEM_CGROUP_CHARGE_TYPE_CACHE
:
2592 case MEM_CGROUP_CHARGE_TYPE_SHMEM
:
2593 SetPageCgroupCache(pc
);
2594 SetPageCgroupUsed(pc
);
2596 case MEM_CGROUP_CHARGE_TYPE_MAPPED
:
2597 ClearPageCgroupCache(pc
);
2598 SetPageCgroupUsed(pc
);
2604 mem_cgroup_charge_statistics(mem
, PageCgroupCache(pc
), nr_pages
);
2605 unlock_page_cgroup(pc
);
2607 * "charge_statistics" updated event counter. Then, check it.
2608 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2609 * if they exceeds softlimit.
2611 memcg_check_events(mem
, page
);
2614 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
2616 #define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\
2617 (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION))
2619 * Because tail pages are not marked as "used", set it. We're under
2620 * zone->lru_lock, 'splitting on pmd' and compund_lock.
2622 void mem_cgroup_split_huge_fixup(struct page
*head
, struct page
*tail
)
2624 struct page_cgroup
*head_pc
= lookup_page_cgroup(head
);
2625 struct page_cgroup
*tail_pc
= lookup_page_cgroup(tail
);
2626 unsigned long flags
;
2628 if (mem_cgroup_disabled())
2631 * We have no races with charge/uncharge but will have races with
2632 * page state accounting.
2634 move_lock_page_cgroup(head_pc
, &flags
);
2636 tail_pc
->mem_cgroup
= head_pc
->mem_cgroup
;
2637 smp_wmb(); /* see __commit_charge() */
2638 if (PageCgroupAcctLRU(head_pc
)) {
2640 struct mem_cgroup_per_zone
*mz
;
2643 * LRU flags cannot be copied because we need to add tail
2644 *.page to LRU by generic call and our hook will be called.
2645 * We hold lru_lock, then, reduce counter directly.
2647 lru
= page_lru(head
);
2648 mz
= page_cgroup_zoneinfo(head_pc
->mem_cgroup
, head
);
2649 MEM_CGROUP_ZSTAT(mz
, lru
) -= 1;
2651 tail_pc
->flags
= head_pc
->flags
& ~PCGF_NOCOPY_AT_SPLIT
;
2652 move_unlock_page_cgroup(head_pc
, &flags
);
2657 * mem_cgroup_move_account - move account of the page
2659 * @nr_pages: number of regular pages (>1 for huge pages)
2660 * @pc: page_cgroup of the page.
2661 * @from: mem_cgroup which the page is moved from.
2662 * @to: mem_cgroup which the page is moved to. @from != @to.
2663 * @uncharge: whether we should call uncharge and css_put against @from.
2665 * The caller must confirm following.
2666 * - page is not on LRU (isolate_page() is useful.)
2667 * - compound_lock is held when nr_pages > 1
2669 * This function doesn't do "charge" nor css_get to new cgroup. It should be
2670 * done by a caller(__mem_cgroup_try_charge would be useful). If @uncharge is
2671 * true, this function does "uncharge" from old cgroup, but it doesn't if
2672 * @uncharge is false, so a caller should do "uncharge".
2674 static int mem_cgroup_move_account(struct page
*page
,
2675 unsigned int nr_pages
,
2676 struct page_cgroup
*pc
,
2677 struct mem_cgroup
*from
,
2678 struct mem_cgroup
*to
,
2681 unsigned long flags
;
2684 VM_BUG_ON(from
== to
);
2685 VM_BUG_ON(PageLRU(page
));
2687 * The page is isolated from LRU. So, collapse function
2688 * will not handle this page. But page splitting can happen.
2689 * Do this check under compound_page_lock(). The caller should
2693 if (nr_pages
> 1 && !PageTransHuge(page
))
2696 lock_page_cgroup(pc
);
2699 if (!PageCgroupUsed(pc
) || pc
->mem_cgroup
!= from
)
2702 move_lock_page_cgroup(pc
, &flags
);
2704 if (PageCgroupFileMapped(pc
)) {
2705 /* Update mapped_file data for mem_cgroup */
2707 __this_cpu_dec(from
->stat
->count
[MEM_CGROUP_STAT_FILE_MAPPED
]);
2708 __this_cpu_inc(to
->stat
->count
[MEM_CGROUP_STAT_FILE_MAPPED
]);
2711 mem_cgroup_charge_statistics(from
, PageCgroupCache(pc
), -nr_pages
);
2713 /* This is not "cancel", but cancel_charge does all we need. */
2714 __mem_cgroup_cancel_charge(from
, nr_pages
);
2716 /* caller should have done css_get */
2717 pc
->mem_cgroup
= to
;
2718 mem_cgroup_charge_statistics(to
, PageCgroupCache(pc
), nr_pages
);
2720 * We charges against "to" which may not have any tasks. Then, "to"
2721 * can be under rmdir(). But in current implementation, caller of
2722 * this function is just force_empty() and move charge, so it's
2723 * guaranteed that "to" is never removed. So, we don't check rmdir
2726 move_unlock_page_cgroup(pc
, &flags
);
2729 unlock_page_cgroup(pc
);
2733 memcg_check_events(to
, page
);
2734 memcg_check_events(from
, page
);
2740 * move charges to its parent.
2743 static int mem_cgroup_move_parent(struct page
*page
,
2744 struct page_cgroup
*pc
,
2745 struct mem_cgroup
*child
,
2748 struct cgroup
*cg
= child
->css
.cgroup
;
2749 struct cgroup
*pcg
= cg
->parent
;
2750 struct mem_cgroup
*parent
;
2751 unsigned int nr_pages
;
2752 unsigned long uninitialized_var(flags
);
2760 if (!get_page_unless_zero(page
))
2762 if (isolate_lru_page(page
))
2765 nr_pages
= hpage_nr_pages(page
);
2767 parent
= mem_cgroup_from_cont(pcg
);
2768 ret
= __mem_cgroup_try_charge(NULL
, gfp_mask
, nr_pages
, &parent
, false);
2773 flags
= compound_lock_irqsave(page
);
2775 ret
= mem_cgroup_move_account(page
, nr_pages
, pc
, child
, parent
, true);
2777 __mem_cgroup_cancel_charge(parent
, nr_pages
);
2780 compound_unlock_irqrestore(page
, flags
);
2782 putback_lru_page(page
);
2790 * Charge the memory controller for page usage.
2792 * 0 if the charge was successful
2793 * < 0 if the cgroup is over its limit
2795 static int mem_cgroup_charge_common(struct page
*page
, struct mm_struct
*mm
,
2796 gfp_t gfp_mask
, enum charge_type ctype
)
2798 struct mem_cgroup
*mem
= NULL
;
2799 unsigned int nr_pages
= 1;
2800 struct page_cgroup
*pc
;
2804 if (PageTransHuge(page
)) {
2805 nr_pages
<<= compound_order(page
);
2806 VM_BUG_ON(!PageTransHuge(page
));
2808 * Never OOM-kill a process for a huge page. The
2809 * fault handler will fall back to regular pages.
2814 pc
= lookup_page_cgroup(page
);
2815 BUG_ON(!pc
); /* XXX: remove this and move pc lookup into commit */
2817 ret
= __mem_cgroup_try_charge(mm
, gfp_mask
, nr_pages
, &mem
, oom
);
2821 __mem_cgroup_commit_charge(mem
, page
, nr_pages
, pc
, ctype
);
2825 int mem_cgroup_newpage_charge(struct page
*page
,
2826 struct mm_struct
*mm
, gfp_t gfp_mask
)
2828 if (mem_cgroup_disabled())
2831 * If already mapped, we don't have to account.
2832 * If page cache, page->mapping has address_space.
2833 * But page->mapping may have out-of-use anon_vma pointer,
2834 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2837 if (page_mapped(page
) || (page
->mapping
&& !PageAnon(page
)))
2841 return mem_cgroup_charge_common(page
, mm
, gfp_mask
,
2842 MEM_CGROUP_CHARGE_TYPE_MAPPED
);
2846 __mem_cgroup_commit_charge_swapin(struct page
*page
, struct mem_cgroup
*ptr
,
2847 enum charge_type ctype
);
2850 __mem_cgroup_commit_charge_lrucare(struct page
*page
, struct mem_cgroup
*mem
,
2851 enum charge_type ctype
)
2853 struct page_cgroup
*pc
= lookup_page_cgroup(page
);
2855 * In some case, SwapCache, FUSE(splice_buf->radixtree), the page
2856 * is already on LRU. It means the page may on some other page_cgroup's
2857 * LRU. Take care of it.
2859 mem_cgroup_lru_del_before_commit(page
);
2860 __mem_cgroup_commit_charge(mem
, page
, 1, pc
, ctype
);
2861 mem_cgroup_lru_add_after_commit(page
);
2865 int mem_cgroup_cache_charge(struct page
*page
, struct mm_struct
*mm
,
2868 struct mem_cgroup
*mem
= NULL
;
2871 if (mem_cgroup_disabled())
2873 if (PageCompound(page
))
2879 if (page_is_file_cache(page
)) {
2880 ret
= __mem_cgroup_try_charge(mm
, gfp_mask
, 1, &mem
, true);
2885 * FUSE reuses pages without going through the final
2886 * put that would remove them from the LRU list, make
2887 * sure that they get relinked properly.
2889 __mem_cgroup_commit_charge_lrucare(page
, mem
,
2890 MEM_CGROUP_CHARGE_TYPE_CACHE
);
2894 if (PageSwapCache(page
)) {
2895 ret
= mem_cgroup_try_charge_swapin(mm
, page
, gfp_mask
, &mem
);
2897 __mem_cgroup_commit_charge_swapin(page
, mem
,
2898 MEM_CGROUP_CHARGE_TYPE_SHMEM
);
2900 ret
= mem_cgroup_charge_common(page
, mm
, gfp_mask
,
2901 MEM_CGROUP_CHARGE_TYPE_SHMEM
);
2907 * While swap-in, try_charge -> commit or cancel, the page is locked.
2908 * And when try_charge() successfully returns, one refcnt to memcg without
2909 * struct page_cgroup is acquired. This refcnt will be consumed by
2910 * "commit()" or removed by "cancel()"
2912 int mem_cgroup_try_charge_swapin(struct mm_struct
*mm
,
2914 gfp_t mask
, struct mem_cgroup
**ptr
)
2916 struct mem_cgroup
*mem
;
2921 if (mem_cgroup_disabled())
2924 if (!do_swap_account
)
2927 * A racing thread's fault, or swapoff, may have already updated
2928 * the pte, and even removed page from swap cache: in those cases
2929 * do_swap_page()'s pte_same() test will fail; but there's also a
2930 * KSM case which does need to charge the page.
2932 if (!PageSwapCache(page
))
2934 mem
= try_get_mem_cgroup_from_page(page
);
2938 ret
= __mem_cgroup_try_charge(NULL
, mask
, 1, ptr
, true);
2944 return __mem_cgroup_try_charge(mm
, mask
, 1, ptr
, true);
2948 __mem_cgroup_commit_charge_swapin(struct page
*page
, struct mem_cgroup
*ptr
,
2949 enum charge_type ctype
)
2951 if (mem_cgroup_disabled())
2955 cgroup_exclude_rmdir(&ptr
->css
);
2957 __mem_cgroup_commit_charge_lrucare(page
, ptr
, ctype
);
2959 * Now swap is on-memory. This means this page may be
2960 * counted both as mem and swap....double count.
2961 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2962 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2963 * may call delete_from_swap_cache() before reach here.
2965 if (do_swap_account
&& PageSwapCache(page
)) {
2966 swp_entry_t ent
= {.val
= page_private(page
)};
2968 struct mem_cgroup
*memcg
;
2970 id
= swap_cgroup_record(ent
, 0);
2972 memcg
= mem_cgroup_lookup(id
);
2975 * This recorded memcg can be obsolete one. So, avoid
2976 * calling css_tryget
2978 if (!mem_cgroup_is_root(memcg
))
2979 res_counter_uncharge(&memcg
->memsw
, PAGE_SIZE
);
2980 mem_cgroup_swap_statistics(memcg
, false);
2981 mem_cgroup_put(memcg
);
2986 * At swapin, we may charge account against cgroup which has no tasks.
2987 * So, rmdir()->pre_destroy() can be called while we do this charge.
2988 * In that case, we need to call pre_destroy() again. check it here.
2990 cgroup_release_and_wakeup_rmdir(&ptr
->css
);
2993 void mem_cgroup_commit_charge_swapin(struct page
*page
, struct mem_cgroup
*ptr
)
2995 __mem_cgroup_commit_charge_swapin(page
, ptr
,
2996 MEM_CGROUP_CHARGE_TYPE_MAPPED
);
2999 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup
*mem
)
3001 if (mem_cgroup_disabled())
3005 __mem_cgroup_cancel_charge(mem
, 1);
3008 static void mem_cgroup_do_uncharge(struct mem_cgroup
*mem
,
3009 unsigned int nr_pages
,
3010 const enum charge_type ctype
)
3012 struct memcg_batch_info
*batch
= NULL
;
3013 bool uncharge_memsw
= true;
3015 /* If swapout, usage of swap doesn't decrease */
3016 if (!do_swap_account
|| ctype
== MEM_CGROUP_CHARGE_TYPE_SWAPOUT
)
3017 uncharge_memsw
= false;
3019 batch
= ¤t
->memcg_batch
;
3021 * In usual, we do css_get() when we remember memcg pointer.
3022 * But in this case, we keep res->usage until end of a series of
3023 * uncharges. Then, it's ok to ignore memcg's refcnt.
3028 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
3029 * In those cases, all pages freed continuously can be expected to be in
3030 * the same cgroup and we have chance to coalesce uncharges.
3031 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
3032 * because we want to do uncharge as soon as possible.
3035 if (!batch
->do_batch
|| test_thread_flag(TIF_MEMDIE
))
3036 goto direct_uncharge
;
3039 goto direct_uncharge
;
3042 * In typical case, batch->memcg == mem. This means we can
3043 * merge a series of uncharges to an uncharge of res_counter.
3044 * If not, we uncharge res_counter ony by one.
3046 if (batch
->memcg
!= mem
)
3047 goto direct_uncharge
;
3048 /* remember freed charge and uncharge it later */
3051 batch
->memsw_nr_pages
++;
3054 res_counter_uncharge(&mem
->res
, nr_pages
* PAGE_SIZE
);
3056 res_counter_uncharge(&mem
->memsw
, nr_pages
* PAGE_SIZE
);
3057 if (unlikely(batch
->memcg
!= mem
))
3058 memcg_oom_recover(mem
);
3063 * uncharge if !page_mapped(page)
3065 static struct mem_cgroup
*
3066 __mem_cgroup_uncharge_common(struct page
*page
, enum charge_type ctype
)
3068 struct mem_cgroup
*mem
= NULL
;
3069 unsigned int nr_pages
= 1;
3070 struct page_cgroup
*pc
;
3072 if (mem_cgroup_disabled())
3075 if (PageSwapCache(page
))
3078 if (PageTransHuge(page
)) {
3079 nr_pages
<<= compound_order(page
);
3080 VM_BUG_ON(!PageTransHuge(page
));
3083 * Check if our page_cgroup is valid
3085 pc
= lookup_page_cgroup(page
);
3086 if (unlikely(!pc
|| !PageCgroupUsed(pc
)))
3089 lock_page_cgroup(pc
);
3091 mem
= pc
->mem_cgroup
;
3093 if (!PageCgroupUsed(pc
))
3097 case MEM_CGROUP_CHARGE_TYPE_MAPPED
:
3098 case MEM_CGROUP_CHARGE_TYPE_DROP
:
3099 /* See mem_cgroup_prepare_migration() */
3100 if (page_mapped(page
) || PageCgroupMigration(pc
))
3103 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT
:
3104 if (!PageAnon(page
)) { /* Shared memory */
3105 if (page
->mapping
&& !page_is_file_cache(page
))
3107 } else if (page_mapped(page
)) /* Anon */
3114 mem_cgroup_charge_statistics(mem
, PageCgroupCache(pc
), -nr_pages
);
3116 ClearPageCgroupUsed(pc
);
3118 * pc->mem_cgroup is not cleared here. It will be accessed when it's
3119 * freed from LRU. This is safe because uncharged page is expected not
3120 * to be reused (freed soon). Exception is SwapCache, it's handled by
3121 * special functions.
3124 unlock_page_cgroup(pc
);
3126 * even after unlock, we have mem->res.usage here and this memcg
3127 * will never be freed.
3129 memcg_check_events(mem
, page
);
3130 if (do_swap_account
&& ctype
== MEM_CGROUP_CHARGE_TYPE_SWAPOUT
) {
3131 mem_cgroup_swap_statistics(mem
, true);
3132 mem_cgroup_get(mem
);
3134 if (!mem_cgroup_is_root(mem
))
3135 mem_cgroup_do_uncharge(mem
, nr_pages
, ctype
);
3140 unlock_page_cgroup(pc
);
3144 void mem_cgroup_uncharge_page(struct page
*page
)
3147 if (page_mapped(page
))
3149 if (page
->mapping
&& !PageAnon(page
))
3151 __mem_cgroup_uncharge_common(page
, MEM_CGROUP_CHARGE_TYPE_MAPPED
);
3154 void mem_cgroup_uncharge_cache_page(struct page
*page
)
3156 VM_BUG_ON(page_mapped(page
));
3157 VM_BUG_ON(page
->mapping
);
3158 __mem_cgroup_uncharge_common(page
, MEM_CGROUP_CHARGE_TYPE_CACHE
);
3162 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3163 * In that cases, pages are freed continuously and we can expect pages
3164 * are in the same memcg. All these calls itself limits the number of
3165 * pages freed at once, then uncharge_start/end() is called properly.
3166 * This may be called prural(2) times in a context,
3169 void mem_cgroup_uncharge_start(void)
3171 current
->memcg_batch
.do_batch
++;
3172 /* We can do nest. */
3173 if (current
->memcg_batch
.do_batch
== 1) {
3174 current
->memcg_batch
.memcg
= NULL
;
3175 current
->memcg_batch
.nr_pages
= 0;
3176 current
->memcg_batch
.memsw_nr_pages
= 0;
3180 void mem_cgroup_uncharge_end(void)
3182 struct memcg_batch_info
*batch
= ¤t
->memcg_batch
;
3184 if (!batch
->do_batch
)
3188 if (batch
->do_batch
) /* If stacked, do nothing. */
3194 * This "batch->memcg" is valid without any css_get/put etc...
3195 * bacause we hide charges behind us.
3197 if (batch
->nr_pages
)
3198 res_counter_uncharge(&batch
->memcg
->res
,
3199 batch
->nr_pages
* PAGE_SIZE
);
3200 if (batch
->memsw_nr_pages
)
3201 res_counter_uncharge(&batch
->memcg
->memsw
,
3202 batch
->memsw_nr_pages
* PAGE_SIZE
);
3203 memcg_oom_recover(batch
->memcg
);
3204 /* forget this pointer (for sanity check) */
3205 batch
->memcg
= NULL
;
3210 * called after __delete_from_swap_cache() and drop "page" account.
3211 * memcg information is recorded to swap_cgroup of "ent"
3214 mem_cgroup_uncharge_swapcache(struct page
*page
, swp_entry_t ent
, bool swapout
)
3216 struct mem_cgroup
*memcg
;
3217 int ctype
= MEM_CGROUP_CHARGE_TYPE_SWAPOUT
;
3219 if (!swapout
) /* this was a swap cache but the swap is unused ! */
3220 ctype
= MEM_CGROUP_CHARGE_TYPE_DROP
;
3222 memcg
= __mem_cgroup_uncharge_common(page
, ctype
);
3225 * record memcg information, if swapout && memcg != NULL,
3226 * mem_cgroup_get() was called in uncharge().
3228 if (do_swap_account
&& swapout
&& memcg
)
3229 swap_cgroup_record(ent
, css_id(&memcg
->css
));
3233 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3235 * called from swap_entry_free(). remove record in swap_cgroup and
3236 * uncharge "memsw" account.
3238 void mem_cgroup_uncharge_swap(swp_entry_t ent
)
3240 struct mem_cgroup
*memcg
;
3243 if (!do_swap_account
)
3246 id
= swap_cgroup_record(ent
, 0);
3248 memcg
= mem_cgroup_lookup(id
);
3251 * We uncharge this because swap is freed.
3252 * This memcg can be obsolete one. We avoid calling css_tryget
3254 if (!mem_cgroup_is_root(memcg
))
3255 res_counter_uncharge(&memcg
->memsw
, PAGE_SIZE
);
3256 mem_cgroup_swap_statistics(memcg
, false);
3257 mem_cgroup_put(memcg
);
3263 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3264 * @entry: swap entry to be moved
3265 * @from: mem_cgroup which the entry is moved from
3266 * @to: mem_cgroup which the entry is moved to
3267 * @need_fixup: whether we should fixup res_counters and refcounts.
3269 * It succeeds only when the swap_cgroup's record for this entry is the same
3270 * as the mem_cgroup's id of @from.
3272 * Returns 0 on success, -EINVAL on failure.
3274 * The caller must have charged to @to, IOW, called res_counter_charge() about
3275 * both res and memsw, and called css_get().
3277 static int mem_cgroup_move_swap_account(swp_entry_t entry
,
3278 struct mem_cgroup
*from
, struct mem_cgroup
*to
, bool need_fixup
)
3280 unsigned short old_id
, new_id
;
3282 old_id
= css_id(&from
->css
);
3283 new_id
= css_id(&to
->css
);
3285 if (swap_cgroup_cmpxchg(entry
, old_id
, new_id
) == old_id
) {
3286 mem_cgroup_swap_statistics(from
, false);
3287 mem_cgroup_swap_statistics(to
, true);
3289 * This function is only called from task migration context now.
3290 * It postpones res_counter and refcount handling till the end
3291 * of task migration(mem_cgroup_clear_mc()) for performance
3292 * improvement. But we cannot postpone mem_cgroup_get(to)
3293 * because if the process that has been moved to @to does
3294 * swap-in, the refcount of @to might be decreased to 0.
3298 if (!mem_cgroup_is_root(from
))
3299 res_counter_uncharge(&from
->memsw
, PAGE_SIZE
);
3300 mem_cgroup_put(from
);
3302 * we charged both to->res and to->memsw, so we should
3305 if (!mem_cgroup_is_root(to
))
3306 res_counter_uncharge(&to
->res
, PAGE_SIZE
);
3313 static inline int mem_cgroup_move_swap_account(swp_entry_t entry
,
3314 struct mem_cgroup
*from
, struct mem_cgroup
*to
, bool need_fixup
)
3321 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3324 int mem_cgroup_prepare_migration(struct page
*page
,
3325 struct page
*newpage
, struct mem_cgroup
**ptr
, gfp_t gfp_mask
)
3327 struct mem_cgroup
*mem
= NULL
;
3328 struct page_cgroup
*pc
;
3329 enum charge_type ctype
;
3334 VM_BUG_ON(PageTransHuge(page
));
3335 if (mem_cgroup_disabled())
3338 pc
= lookup_page_cgroup(page
);
3339 lock_page_cgroup(pc
);
3340 if (PageCgroupUsed(pc
)) {
3341 mem
= pc
->mem_cgroup
;
3344 * At migrating an anonymous page, its mapcount goes down
3345 * to 0 and uncharge() will be called. But, even if it's fully
3346 * unmapped, migration may fail and this page has to be
3347 * charged again. We set MIGRATION flag here and delay uncharge
3348 * until end_migration() is called
3350 * Corner Case Thinking
3352 * When the old page was mapped as Anon and it's unmap-and-freed
3353 * while migration was ongoing.
3354 * If unmap finds the old page, uncharge() of it will be delayed
3355 * until end_migration(). If unmap finds a new page, it's
3356 * uncharged when it make mapcount to be 1->0. If unmap code
3357 * finds swap_migration_entry, the new page will not be mapped
3358 * and end_migration() will find it(mapcount==0).
3361 * When the old page was mapped but migraion fails, the kernel
3362 * remaps it. A charge for it is kept by MIGRATION flag even
3363 * if mapcount goes down to 0. We can do remap successfully
3364 * without charging it again.
3367 * The "old" page is under lock_page() until the end of
3368 * migration, so, the old page itself will not be swapped-out.
3369 * If the new page is swapped out before end_migraton, our
3370 * hook to usual swap-out path will catch the event.
3373 SetPageCgroupMigration(pc
);
3375 unlock_page_cgroup(pc
);
3377 * If the page is not charged at this point,
3384 ret
= __mem_cgroup_try_charge(NULL
, gfp_mask
, 1, ptr
, false);
3385 css_put(&mem
->css
);/* drop extra refcnt */
3386 if (ret
|| *ptr
== NULL
) {
3387 if (PageAnon(page
)) {
3388 lock_page_cgroup(pc
);
3389 ClearPageCgroupMigration(pc
);
3390 unlock_page_cgroup(pc
);
3392 * The old page may be fully unmapped while we kept it.
3394 mem_cgroup_uncharge_page(page
);
3399 * We charge new page before it's used/mapped. So, even if unlock_page()
3400 * is called before end_migration, we can catch all events on this new
3401 * page. In the case new page is migrated but not remapped, new page's
3402 * mapcount will be finally 0 and we call uncharge in end_migration().
3404 pc
= lookup_page_cgroup(newpage
);
3406 ctype
= MEM_CGROUP_CHARGE_TYPE_MAPPED
;
3407 else if (page_is_file_cache(page
))
3408 ctype
= MEM_CGROUP_CHARGE_TYPE_CACHE
;
3410 ctype
= MEM_CGROUP_CHARGE_TYPE_SHMEM
;
3411 __mem_cgroup_commit_charge(mem
, page
, 1, pc
, ctype
);
3415 /* remove redundant charge if migration failed*/
3416 void mem_cgroup_end_migration(struct mem_cgroup
*mem
,
3417 struct page
*oldpage
, struct page
*newpage
, bool migration_ok
)
3419 struct page
*used
, *unused
;
3420 struct page_cgroup
*pc
;
3424 /* blocks rmdir() */
3425 cgroup_exclude_rmdir(&mem
->css
);
3426 if (!migration_ok
) {
3434 * We disallowed uncharge of pages under migration because mapcount
3435 * of the page goes down to zero, temporarly.
3436 * Clear the flag and check the page should be charged.
3438 pc
= lookup_page_cgroup(oldpage
);
3439 lock_page_cgroup(pc
);
3440 ClearPageCgroupMigration(pc
);
3441 unlock_page_cgroup(pc
);
3443 __mem_cgroup_uncharge_common(unused
, MEM_CGROUP_CHARGE_TYPE_FORCE
);
3446 * If a page is a file cache, radix-tree replacement is very atomic
3447 * and we can skip this check. When it was an Anon page, its mapcount
3448 * goes down to 0. But because we added MIGRATION flage, it's not
3449 * uncharged yet. There are several case but page->mapcount check
3450 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3451 * check. (see prepare_charge() also)
3454 mem_cgroup_uncharge_page(used
);
3456 * At migration, we may charge account against cgroup which has no
3458 * So, rmdir()->pre_destroy() can be called while we do this charge.
3459 * In that case, we need to call pre_destroy() again. check it here.
3461 cgroup_release_and_wakeup_rmdir(&mem
->css
);
3464 #ifdef CONFIG_DEBUG_VM
3465 static struct page_cgroup
*lookup_page_cgroup_used(struct page
*page
)
3467 struct page_cgroup
*pc
;
3469 pc
= lookup_page_cgroup(page
);
3470 if (likely(pc
) && PageCgroupUsed(pc
))
3475 bool mem_cgroup_bad_page_check(struct page
*page
)
3477 if (mem_cgroup_disabled())
3480 return lookup_page_cgroup_used(page
) != NULL
;
3483 void mem_cgroup_print_bad_page(struct page
*page
)
3485 struct page_cgroup
*pc
;
3487 pc
= lookup_page_cgroup_used(page
);
3492 printk(KERN_ALERT
"pc:%p pc->flags:%lx pc->mem_cgroup:%p",
3493 pc
, pc
->flags
, pc
->mem_cgroup
);
3495 path
= kmalloc(PATH_MAX
, GFP_KERNEL
);
3498 ret
= cgroup_path(pc
->mem_cgroup
->css
.cgroup
,
3503 printk(KERN_CONT
"(%s)\n",
3504 (ret
< 0) ? "cannot get the path" : path
);
3510 static DEFINE_MUTEX(set_limit_mutex
);
3512 static int mem_cgroup_resize_limit(struct mem_cgroup
*memcg
,
3513 unsigned long long val
)
3516 u64 memswlimit
, memlimit
;
3518 int children
= mem_cgroup_count_children(memcg
);
3519 u64 curusage
, oldusage
;
3523 * For keeping hierarchical_reclaim simple, how long we should retry
3524 * is depends on callers. We set our retry-count to be function
3525 * of # of children which we should visit in this loop.
3527 retry_count
= MEM_CGROUP_RECLAIM_RETRIES
* children
;
3529 oldusage
= res_counter_read_u64(&memcg
->res
, RES_USAGE
);
3532 while (retry_count
) {
3533 if (signal_pending(current
)) {
3538 * Rather than hide all in some function, I do this in
3539 * open coded manner. You see what this really does.
3540 * We have to guarantee mem->res.limit < mem->memsw.limit.
3542 mutex_lock(&set_limit_mutex
);
3543 memswlimit
= res_counter_read_u64(&memcg
->memsw
, RES_LIMIT
);
3544 if (memswlimit
< val
) {
3546 mutex_unlock(&set_limit_mutex
);
3550 memlimit
= res_counter_read_u64(&memcg
->res
, RES_LIMIT
);
3554 ret
= res_counter_set_limit(&memcg
->res
, val
);
3556 if (memswlimit
== 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_SHRINK
,
3569 curusage
= res_counter_read_u64(&memcg
->res
, RES_USAGE
);
3570 /* Usage is reduced ? */
3571 if (curusage
>= oldusage
)
3574 oldusage
= curusage
;
3576 if (!ret
&& enlarge
)
3577 memcg_oom_recover(memcg
);
3582 static int mem_cgroup_resize_memsw_limit(struct mem_cgroup
*memcg
,
3583 unsigned long long val
)
3586 u64 memlimit
, memswlimit
, oldusage
, curusage
;
3587 int children
= mem_cgroup_count_children(memcg
);
3591 /* see mem_cgroup_resize_res_limit */
3592 retry_count
= children
* MEM_CGROUP_RECLAIM_RETRIES
;
3593 oldusage
= res_counter_read_u64(&memcg
->memsw
, RES_USAGE
);
3594 while (retry_count
) {
3595 if (signal_pending(current
)) {
3600 * Rather than hide all in some function, I do this in
3601 * open coded manner. You see what this really does.
3602 * We have to guarantee mem->res.limit < mem->memsw.limit.
3604 mutex_lock(&set_limit_mutex
);
3605 memlimit
= res_counter_read_u64(&memcg
->res
, RES_LIMIT
);
3606 if (memlimit
> val
) {
3608 mutex_unlock(&set_limit_mutex
);
3611 memswlimit
= res_counter_read_u64(&memcg
->memsw
, RES_LIMIT
);
3612 if (memswlimit
< val
)
3614 ret
= res_counter_set_limit(&memcg
->memsw
, val
);
3616 if (memlimit
== val
)
3617 memcg
->memsw_is_minimum
= true;
3619 memcg
->memsw_is_minimum
= false;
3621 mutex_unlock(&set_limit_mutex
);
3626 mem_cgroup_hierarchical_reclaim(memcg
, NULL
, GFP_KERNEL
,
3627 MEM_CGROUP_RECLAIM_NOSWAP
|
3628 MEM_CGROUP_RECLAIM_SHRINK
,
3630 curusage
= res_counter_read_u64(&memcg
->memsw
, RES_USAGE
);
3631 /* Usage is reduced ? */
3632 if (curusage
>= oldusage
)
3635 oldusage
= curusage
;
3637 if (!ret
&& enlarge
)
3638 memcg_oom_recover(memcg
);
3642 unsigned long mem_cgroup_soft_limit_reclaim(struct zone
*zone
, int order
,
3644 unsigned long *total_scanned
)
3646 unsigned long nr_reclaimed
= 0;
3647 struct mem_cgroup_per_zone
*mz
, *next_mz
= NULL
;
3648 unsigned long reclaimed
;
3650 struct mem_cgroup_tree_per_zone
*mctz
;
3651 unsigned long long excess
;
3652 unsigned long nr_scanned
;
3657 mctz
= soft_limit_tree_node_zone(zone_to_nid(zone
), zone_idx(zone
));
3659 * This loop can run a while, specially if mem_cgroup's continuously
3660 * keep exceeding their soft limit and putting the system under
3667 mz
= mem_cgroup_largest_soft_limit_node(mctz
);
3672 reclaimed
= mem_cgroup_hierarchical_reclaim(mz
->mem
, zone
,
3674 MEM_CGROUP_RECLAIM_SOFT
,
3676 nr_reclaimed
+= reclaimed
;
3677 *total_scanned
+= nr_scanned
;
3678 spin_lock(&mctz
->lock
);
3681 * If we failed to reclaim anything from this memory cgroup
3682 * it is time to move on to the next cgroup
3688 * Loop until we find yet another one.
3690 * By the time we get the soft_limit lock
3691 * again, someone might have aded the
3692 * group back on the RB tree. Iterate to
3693 * make sure we get a different mem.
3694 * mem_cgroup_largest_soft_limit_node returns
3695 * NULL if no other cgroup is present on
3699 __mem_cgroup_largest_soft_limit_node(mctz
);
3701 css_put(&next_mz
->mem
->css
);
3702 else /* next_mz == NULL or other memcg */
3706 __mem_cgroup_remove_exceeded(mz
->mem
, mz
, mctz
);
3707 excess
= res_counter_soft_limit_excess(&mz
->mem
->res
);
3709 * One school of thought says that we should not add
3710 * back the node to the tree if reclaim returns 0.
3711 * But our reclaim could return 0, simply because due
3712 * to priority we are exposing a smaller subset of
3713 * memory to reclaim from. Consider this as a longer
3716 /* If excess == 0, no tree ops */
3717 __mem_cgroup_insert_exceeded(mz
->mem
, mz
, mctz
, excess
);
3718 spin_unlock(&mctz
->lock
);
3719 css_put(&mz
->mem
->css
);
3722 * Could not reclaim anything and there are no more
3723 * mem cgroups to try or we seem to be looping without
3724 * reclaiming anything.
3726 if (!nr_reclaimed
&&
3728 loop
> MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS
))
3730 } while (!nr_reclaimed
);
3732 css_put(&next_mz
->mem
->css
);
3733 return nr_reclaimed
;
3737 * This routine traverse page_cgroup in given list and drop them all.
3738 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
3740 static int mem_cgroup_force_empty_list(struct mem_cgroup
*mem
,
3741 int node
, int zid
, enum lru_list lru
)
3744 struct mem_cgroup_per_zone
*mz
;
3745 struct page_cgroup
*pc
, *busy
;
3746 unsigned long flags
, loop
;
3747 struct list_head
*list
;
3750 zone
= &NODE_DATA(node
)->node_zones
[zid
];
3751 mz
= mem_cgroup_zoneinfo(mem
, node
, zid
);
3752 list
= &mz
->lists
[lru
];
3754 loop
= MEM_CGROUP_ZSTAT(mz
, lru
);
3755 /* give some margin against EBUSY etc...*/
3762 spin_lock_irqsave(&zone
->lru_lock
, flags
);
3763 if (list_empty(list
)) {
3764 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
3767 pc
= list_entry(list
->prev
, struct page_cgroup
, lru
);
3769 list_move(&pc
->lru
, list
);
3771 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
3774 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
3776 page
= lookup_cgroup_page(pc
);
3778 ret
= mem_cgroup_move_parent(page
, pc
, mem
, GFP_KERNEL
);
3782 if (ret
== -EBUSY
|| ret
== -EINVAL
) {
3783 /* found lock contention or "pc" is obsolete. */
3790 if (!ret
&& !list_empty(list
))
3796 * make mem_cgroup's charge to be 0 if there is no task.
3797 * This enables deleting this mem_cgroup.
3799 static int mem_cgroup_force_empty(struct mem_cgroup
*mem
, bool free_all
)
3802 int node
, zid
, shrink
;
3803 int nr_retries
= MEM_CGROUP_RECLAIM_RETRIES
;
3804 struct cgroup
*cgrp
= mem
->css
.cgroup
;
3809 /* should free all ? */
3815 if (cgroup_task_count(cgrp
) || !list_empty(&cgrp
->children
))
3818 if (signal_pending(current
))
3820 /* This is for making all *used* pages to be on LRU. */
3821 lru_add_drain_all();
3822 drain_all_stock_sync(mem
);
3824 mem_cgroup_start_move(mem
);
3825 for_each_node_state(node
, N_HIGH_MEMORY
) {
3826 for (zid
= 0; !ret
&& zid
< MAX_NR_ZONES
; zid
++) {
3829 ret
= mem_cgroup_force_empty_list(mem
,
3838 mem_cgroup_end_move(mem
);
3839 memcg_oom_recover(mem
);
3840 /* it seems parent cgroup doesn't have enough mem */
3844 /* "ret" should also be checked to ensure all lists are empty. */
3845 } while (mem
->res
.usage
> 0 || ret
);
3851 /* returns EBUSY if there is a task or if we come here twice. */
3852 if (cgroup_task_count(cgrp
) || !list_empty(&cgrp
->children
) || shrink
) {
3856 /* we call try-to-free pages for make this cgroup empty */
3857 lru_add_drain_all();
3858 /* try to free all pages in this cgroup */
3860 while (nr_retries
&& mem
->res
.usage
> 0) {
3861 struct memcg_scanrecord rec
;
3864 if (signal_pending(current
)) {
3868 rec
.context
= SCAN_BY_SHRINK
;
3871 progress
= try_to_free_mem_cgroup_pages(mem
, GFP_KERNEL
,
3875 /* maybe some writeback is necessary */
3876 congestion_wait(BLK_RW_ASYNC
, HZ
/10);
3881 /* try move_account...there may be some *locked* pages. */
3885 int mem_cgroup_force_empty_write(struct cgroup
*cont
, unsigned int event
)
3887 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont
), true);
3891 static u64
mem_cgroup_hierarchy_read(struct cgroup
*cont
, struct cftype
*cft
)
3893 return mem_cgroup_from_cont(cont
)->use_hierarchy
;
3896 static int mem_cgroup_hierarchy_write(struct cgroup
*cont
, struct cftype
*cft
,
3900 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cont
);
3901 struct cgroup
*parent
= cont
->parent
;
3902 struct mem_cgroup
*parent_mem
= NULL
;
3905 parent_mem
= mem_cgroup_from_cont(parent
);
3909 * If parent's use_hierarchy is set, we can't make any modifications
3910 * in the child subtrees. If it is unset, then the change can
3911 * occur, provided the current cgroup has no children.
3913 * For the root cgroup, parent_mem is NULL, we allow value to be
3914 * set if there are no children.
3916 if ((!parent_mem
|| !parent_mem
->use_hierarchy
) &&
3917 (val
== 1 || val
== 0)) {
3918 if (list_empty(&cont
->children
))
3919 mem
->use_hierarchy
= val
;
3930 static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup
*mem
,
3931 enum mem_cgroup_stat_index idx
)
3933 struct mem_cgroup
*iter
;
3936 /* Per-cpu values can be negative, use a signed accumulator */
3937 for_each_mem_cgroup_tree(iter
, mem
)
3938 val
+= mem_cgroup_read_stat(iter
, idx
);
3940 if (val
< 0) /* race ? */
3945 static inline u64
mem_cgroup_usage(struct mem_cgroup
*mem
, bool swap
)
3949 if (!mem_cgroup_is_root(mem
)) {
3951 return res_counter_read_u64(&mem
->res
, RES_USAGE
);
3953 return res_counter_read_u64(&mem
->memsw
, RES_USAGE
);
3956 val
= mem_cgroup_recursive_stat(mem
, MEM_CGROUP_STAT_CACHE
);
3957 val
+= mem_cgroup_recursive_stat(mem
, MEM_CGROUP_STAT_RSS
);
3960 val
+= mem_cgroup_recursive_stat(mem
, MEM_CGROUP_STAT_SWAPOUT
);
3962 return val
<< PAGE_SHIFT
;
3965 static u64
mem_cgroup_read(struct cgroup
*cont
, struct cftype
*cft
)
3967 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cont
);
3971 type
= MEMFILE_TYPE(cft
->private);
3972 name
= MEMFILE_ATTR(cft
->private);
3975 if (name
== RES_USAGE
)
3976 val
= mem_cgroup_usage(mem
, false);
3978 val
= res_counter_read_u64(&mem
->res
, name
);
3981 if (name
== RES_USAGE
)
3982 val
= mem_cgroup_usage(mem
, true);
3984 val
= res_counter_read_u64(&mem
->memsw
, name
);
3993 * The user of this function is...
3996 static int mem_cgroup_write(struct cgroup
*cont
, struct cftype
*cft
,
3999 struct mem_cgroup
*memcg
= mem_cgroup_from_cont(cont
);
4001 unsigned long long val
;
4004 type
= MEMFILE_TYPE(cft
->private);
4005 name
= MEMFILE_ATTR(cft
->private);
4008 if (mem_cgroup_is_root(memcg
)) { /* Can't set limit on root */
4012 /* This function does all necessary parse...reuse it */
4013 ret
= res_counter_memparse_write_strategy(buffer
, &val
);
4017 ret
= mem_cgroup_resize_limit(memcg
, val
);
4019 ret
= mem_cgroup_resize_memsw_limit(memcg
, val
);
4021 case RES_SOFT_LIMIT
:
4022 ret
= res_counter_memparse_write_strategy(buffer
, &val
);
4026 * For memsw, soft limits are hard to implement in terms
4027 * of semantics, for now, we support soft limits for
4028 * control without swap
4031 ret
= res_counter_set_soft_limit(&memcg
->res
, val
);
4036 ret
= -EINVAL
; /* should be BUG() ? */
4042 static void memcg_get_hierarchical_limit(struct mem_cgroup
*memcg
,
4043 unsigned long long *mem_limit
, unsigned long long *memsw_limit
)
4045 struct cgroup
*cgroup
;
4046 unsigned long long min_limit
, min_memsw_limit
, tmp
;
4048 min_limit
= res_counter_read_u64(&memcg
->res
, RES_LIMIT
);
4049 min_memsw_limit
= res_counter_read_u64(&memcg
->memsw
, RES_LIMIT
);
4050 cgroup
= memcg
->css
.cgroup
;
4051 if (!memcg
->use_hierarchy
)
4054 while (cgroup
->parent
) {
4055 cgroup
= cgroup
->parent
;
4056 memcg
= mem_cgroup_from_cont(cgroup
);
4057 if (!memcg
->use_hierarchy
)
4059 tmp
= res_counter_read_u64(&memcg
->res
, RES_LIMIT
);
4060 min_limit
= min(min_limit
, tmp
);
4061 tmp
= res_counter_read_u64(&memcg
->memsw
, RES_LIMIT
);
4062 min_memsw_limit
= min(min_memsw_limit
, tmp
);
4065 *mem_limit
= min_limit
;
4066 *memsw_limit
= min_memsw_limit
;
4070 static int mem_cgroup_reset(struct cgroup
*cont
, unsigned int event
)
4072 struct mem_cgroup
*mem
;
4075 mem
= mem_cgroup_from_cont(cont
);
4076 type
= MEMFILE_TYPE(event
);
4077 name
= MEMFILE_ATTR(event
);
4081 res_counter_reset_max(&mem
->res
);
4083 res_counter_reset_max(&mem
->memsw
);
4087 res_counter_reset_failcnt(&mem
->res
);
4089 res_counter_reset_failcnt(&mem
->memsw
);
4096 static u64
mem_cgroup_move_charge_read(struct cgroup
*cgrp
,
4099 return mem_cgroup_from_cont(cgrp
)->move_charge_at_immigrate
;
4103 static int mem_cgroup_move_charge_write(struct cgroup
*cgrp
,
4104 struct cftype
*cft
, u64 val
)
4106 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cgrp
);
4108 if (val
>= (1 << NR_MOVE_TYPE
))
4111 * We check this value several times in both in can_attach() and
4112 * attach(), so we need cgroup lock to prevent this value from being
4116 mem
->move_charge_at_immigrate
= val
;
4122 static int mem_cgroup_move_charge_write(struct cgroup
*cgrp
,
4123 struct cftype
*cft
, u64 val
)
4130 /* For read statistics */
4148 struct mcs_total_stat
{
4149 s64 stat
[NR_MCS_STAT
];
4155 } memcg_stat_strings
[NR_MCS_STAT
] = {
4156 {"cache", "total_cache"},
4157 {"rss", "total_rss"},
4158 {"mapped_file", "total_mapped_file"},
4159 {"pgpgin", "total_pgpgin"},
4160 {"pgpgout", "total_pgpgout"},
4161 {"swap", "total_swap"},
4162 {"pgfault", "total_pgfault"},
4163 {"pgmajfault", "total_pgmajfault"},
4164 {"inactive_anon", "total_inactive_anon"},
4165 {"active_anon", "total_active_anon"},
4166 {"inactive_file", "total_inactive_file"},
4167 {"active_file", "total_active_file"},
4168 {"unevictable", "total_unevictable"}
4173 mem_cgroup_get_local_stat(struct mem_cgroup
*mem
, struct mcs_total_stat
*s
)
4178 val
= mem_cgroup_read_stat(mem
, MEM_CGROUP_STAT_CACHE
);
4179 s
->stat
[MCS_CACHE
] += val
* PAGE_SIZE
;
4180 val
= mem_cgroup_read_stat(mem
, MEM_CGROUP_STAT_RSS
);
4181 s
->stat
[MCS_RSS
] += val
* PAGE_SIZE
;
4182 val
= mem_cgroup_read_stat(mem
, MEM_CGROUP_STAT_FILE_MAPPED
);
4183 s
->stat
[MCS_FILE_MAPPED
] += val
* PAGE_SIZE
;
4184 val
= mem_cgroup_read_events(mem
, MEM_CGROUP_EVENTS_PGPGIN
);
4185 s
->stat
[MCS_PGPGIN
] += val
;
4186 val
= mem_cgroup_read_events(mem
, MEM_CGROUP_EVENTS_PGPGOUT
);
4187 s
->stat
[MCS_PGPGOUT
] += val
;
4188 if (do_swap_account
) {
4189 val
= mem_cgroup_read_stat(mem
, MEM_CGROUP_STAT_SWAPOUT
);
4190 s
->stat
[MCS_SWAP
] += val
* PAGE_SIZE
;
4192 val
= mem_cgroup_read_events(mem
, MEM_CGROUP_EVENTS_PGFAULT
);
4193 s
->stat
[MCS_PGFAULT
] += val
;
4194 val
= mem_cgroup_read_events(mem
, MEM_CGROUP_EVENTS_PGMAJFAULT
);
4195 s
->stat
[MCS_PGMAJFAULT
] += val
;
4198 val
= mem_cgroup_nr_lru_pages(mem
, BIT(LRU_INACTIVE_ANON
));
4199 s
->stat
[MCS_INACTIVE_ANON
] += val
* PAGE_SIZE
;
4200 val
= mem_cgroup_nr_lru_pages(mem
, BIT(LRU_ACTIVE_ANON
));
4201 s
->stat
[MCS_ACTIVE_ANON
] += val
* PAGE_SIZE
;
4202 val
= mem_cgroup_nr_lru_pages(mem
, BIT(LRU_INACTIVE_FILE
));
4203 s
->stat
[MCS_INACTIVE_FILE
] += val
* PAGE_SIZE
;
4204 val
= mem_cgroup_nr_lru_pages(mem
, BIT(LRU_ACTIVE_FILE
));
4205 s
->stat
[MCS_ACTIVE_FILE
] += val
* PAGE_SIZE
;
4206 val
= mem_cgroup_nr_lru_pages(mem
, BIT(LRU_UNEVICTABLE
));
4207 s
->stat
[MCS_UNEVICTABLE
] += val
* PAGE_SIZE
;
4211 mem_cgroup_get_total_stat(struct mem_cgroup
*mem
, struct mcs_total_stat
*s
)
4213 struct mem_cgroup
*iter
;
4215 for_each_mem_cgroup_tree(iter
, mem
)
4216 mem_cgroup_get_local_stat(iter
, s
);
4220 static int mem_control_numa_stat_show(struct seq_file
*m
, void *arg
)
4223 unsigned long total_nr
, file_nr
, anon_nr
, unevictable_nr
;
4224 unsigned long node_nr
;
4225 struct cgroup
*cont
= m
->private;
4226 struct mem_cgroup
*mem_cont
= mem_cgroup_from_cont(cont
);
4228 total_nr
= mem_cgroup_nr_lru_pages(mem_cont
, LRU_ALL
);
4229 seq_printf(m
, "total=%lu", total_nr
);
4230 for_each_node_state(nid
, N_HIGH_MEMORY
) {
4231 node_nr
= mem_cgroup_node_nr_lru_pages(mem_cont
, nid
, LRU_ALL
);
4232 seq_printf(m
, " N%d=%lu", nid
, node_nr
);
4236 file_nr
= mem_cgroup_nr_lru_pages(mem_cont
, LRU_ALL_FILE
);
4237 seq_printf(m
, "file=%lu", file_nr
);
4238 for_each_node_state(nid
, N_HIGH_MEMORY
) {
4239 node_nr
= mem_cgroup_node_nr_lru_pages(mem_cont
, nid
,
4241 seq_printf(m
, " N%d=%lu", nid
, node_nr
);
4245 anon_nr
= mem_cgroup_nr_lru_pages(mem_cont
, LRU_ALL_ANON
);
4246 seq_printf(m
, "anon=%lu", anon_nr
);
4247 for_each_node_state(nid
, N_HIGH_MEMORY
) {
4248 node_nr
= mem_cgroup_node_nr_lru_pages(mem_cont
, nid
,
4250 seq_printf(m
, " N%d=%lu", nid
, node_nr
);
4254 unevictable_nr
= mem_cgroup_nr_lru_pages(mem_cont
, BIT(LRU_UNEVICTABLE
));
4255 seq_printf(m
, "unevictable=%lu", unevictable_nr
);
4256 for_each_node_state(nid
, N_HIGH_MEMORY
) {
4257 node_nr
= mem_cgroup_node_nr_lru_pages(mem_cont
, nid
,
4258 BIT(LRU_UNEVICTABLE
));
4259 seq_printf(m
, " N%d=%lu", nid
, node_nr
);
4264 #endif /* CONFIG_NUMA */
4266 static int mem_control_stat_show(struct cgroup
*cont
, struct cftype
*cft
,
4267 struct cgroup_map_cb
*cb
)
4269 struct mem_cgroup
*mem_cont
= mem_cgroup_from_cont(cont
);
4270 struct mcs_total_stat mystat
;
4273 memset(&mystat
, 0, sizeof(mystat
));
4274 mem_cgroup_get_local_stat(mem_cont
, &mystat
);
4277 for (i
= 0; i
< NR_MCS_STAT
; i
++) {
4278 if (i
== MCS_SWAP
&& !do_swap_account
)
4280 cb
->fill(cb
, memcg_stat_strings
[i
].local_name
, mystat
.stat
[i
]);
4283 /* Hierarchical information */
4285 unsigned long long limit
, memsw_limit
;
4286 memcg_get_hierarchical_limit(mem_cont
, &limit
, &memsw_limit
);
4287 cb
->fill(cb
, "hierarchical_memory_limit", limit
);
4288 if (do_swap_account
)
4289 cb
->fill(cb
, "hierarchical_memsw_limit", memsw_limit
);
4292 memset(&mystat
, 0, sizeof(mystat
));
4293 mem_cgroup_get_total_stat(mem_cont
, &mystat
);
4294 for (i
= 0; i
< NR_MCS_STAT
; i
++) {
4295 if (i
== MCS_SWAP
&& !do_swap_account
)
4297 cb
->fill(cb
, memcg_stat_strings
[i
].total_name
, mystat
.stat
[i
]);
4300 #ifdef CONFIG_DEBUG_VM
4301 cb
->fill(cb
, "inactive_ratio", calc_inactive_ratio(mem_cont
, NULL
));
4305 struct mem_cgroup_per_zone
*mz
;
4306 unsigned long recent_rotated
[2] = {0, 0};
4307 unsigned long recent_scanned
[2] = {0, 0};
4309 for_each_online_node(nid
)
4310 for (zid
= 0; zid
< MAX_NR_ZONES
; zid
++) {
4311 mz
= mem_cgroup_zoneinfo(mem_cont
, nid
, zid
);
4313 recent_rotated
[0] +=
4314 mz
->reclaim_stat
.recent_rotated
[0];
4315 recent_rotated
[1] +=
4316 mz
->reclaim_stat
.recent_rotated
[1];
4317 recent_scanned
[0] +=
4318 mz
->reclaim_stat
.recent_scanned
[0];
4319 recent_scanned
[1] +=
4320 mz
->reclaim_stat
.recent_scanned
[1];
4322 cb
->fill(cb
, "recent_rotated_anon", recent_rotated
[0]);
4323 cb
->fill(cb
, "recent_rotated_file", recent_rotated
[1]);
4324 cb
->fill(cb
, "recent_scanned_anon", recent_scanned
[0]);
4325 cb
->fill(cb
, "recent_scanned_file", recent_scanned
[1]);
4332 static u64
mem_cgroup_swappiness_read(struct cgroup
*cgrp
, struct cftype
*cft
)
4334 struct mem_cgroup
*memcg
= mem_cgroup_from_cont(cgrp
);
4336 return mem_cgroup_swappiness(memcg
);
4339 static int mem_cgroup_swappiness_write(struct cgroup
*cgrp
, struct cftype
*cft
,
4342 struct mem_cgroup
*memcg
= mem_cgroup_from_cont(cgrp
);
4343 struct mem_cgroup
*parent
;
4348 if (cgrp
->parent
== NULL
)
4351 parent
= mem_cgroup_from_cont(cgrp
->parent
);
4355 /* If under hierarchy, only empty-root can set this value */
4356 if ((parent
->use_hierarchy
) ||
4357 (memcg
->use_hierarchy
&& !list_empty(&cgrp
->children
))) {
4362 memcg
->swappiness
= val
;
4369 static void __mem_cgroup_threshold(struct mem_cgroup
*memcg
, bool swap
)
4371 struct mem_cgroup_threshold_ary
*t
;
4377 t
= rcu_dereference(memcg
->thresholds
.primary
);
4379 t
= rcu_dereference(memcg
->memsw_thresholds
.primary
);
4384 usage
= mem_cgroup_usage(memcg
, swap
);
4387 * current_threshold points to threshold just below usage.
4388 * If it's not true, a threshold was crossed after last
4389 * call of __mem_cgroup_threshold().
4391 i
= t
->current_threshold
;
4394 * Iterate backward over array of thresholds starting from
4395 * current_threshold and check if a threshold is crossed.
4396 * If none of thresholds below usage is crossed, we read
4397 * only one element of the array here.
4399 for (; i
>= 0 && unlikely(t
->entries
[i
].threshold
> usage
); i
--)
4400 eventfd_signal(t
->entries
[i
].eventfd
, 1);
4402 /* i = current_threshold + 1 */
4406 * Iterate forward over array of thresholds starting from
4407 * current_threshold+1 and check if a threshold is crossed.
4408 * If none of thresholds above usage is crossed, we read
4409 * only one element of the array here.
4411 for (; i
< t
->size
&& unlikely(t
->entries
[i
].threshold
<= usage
); i
++)
4412 eventfd_signal(t
->entries
[i
].eventfd
, 1);
4414 /* Update current_threshold */
4415 t
->current_threshold
= i
- 1;
4420 static void mem_cgroup_threshold(struct mem_cgroup
*memcg
)
4423 __mem_cgroup_threshold(memcg
, false);
4424 if (do_swap_account
)
4425 __mem_cgroup_threshold(memcg
, true);
4427 memcg
= parent_mem_cgroup(memcg
);
4431 static int compare_thresholds(const void *a
, const void *b
)
4433 const struct mem_cgroup_threshold
*_a
= a
;
4434 const struct mem_cgroup_threshold
*_b
= b
;
4436 return _a
->threshold
- _b
->threshold
;
4439 static int mem_cgroup_oom_notify_cb(struct mem_cgroup
*mem
)
4441 struct mem_cgroup_eventfd_list
*ev
;
4443 list_for_each_entry(ev
, &mem
->oom_notify
, list
)
4444 eventfd_signal(ev
->eventfd
, 1);
4448 static void mem_cgroup_oom_notify(struct mem_cgroup
*mem
)
4450 struct mem_cgroup
*iter
;
4452 for_each_mem_cgroup_tree(iter
, mem
)
4453 mem_cgroup_oom_notify_cb(iter
);
4456 static int mem_cgroup_usage_register_event(struct cgroup
*cgrp
,
4457 struct cftype
*cft
, struct eventfd_ctx
*eventfd
, const char *args
)
4459 struct mem_cgroup
*memcg
= mem_cgroup_from_cont(cgrp
);
4460 struct mem_cgroup_thresholds
*thresholds
;
4461 struct mem_cgroup_threshold_ary
*new;
4462 int type
= MEMFILE_TYPE(cft
->private);
4463 u64 threshold
, usage
;
4466 ret
= res_counter_memparse_write_strategy(args
, &threshold
);
4470 mutex_lock(&memcg
->thresholds_lock
);
4473 thresholds
= &memcg
->thresholds
;
4474 else if (type
== _MEMSWAP
)
4475 thresholds
= &memcg
->memsw_thresholds
;
4479 usage
= mem_cgroup_usage(memcg
, type
== _MEMSWAP
);
4481 /* Check if a threshold crossed before adding a new one */
4482 if (thresholds
->primary
)
4483 __mem_cgroup_threshold(memcg
, type
== _MEMSWAP
);
4485 size
= thresholds
->primary
? thresholds
->primary
->size
+ 1 : 1;
4487 /* Allocate memory for new array of thresholds */
4488 new = kmalloc(sizeof(*new) + size
* sizeof(struct mem_cgroup_threshold
),
4496 /* Copy thresholds (if any) to new array */
4497 if (thresholds
->primary
) {
4498 memcpy(new->entries
, thresholds
->primary
->entries
, (size
- 1) *
4499 sizeof(struct mem_cgroup_threshold
));
4502 /* Add new threshold */
4503 new->entries
[size
- 1].eventfd
= eventfd
;
4504 new->entries
[size
- 1].threshold
= threshold
;
4506 /* Sort thresholds. Registering of new threshold isn't time-critical */
4507 sort(new->entries
, size
, sizeof(struct mem_cgroup_threshold
),
4508 compare_thresholds
, NULL
);
4510 /* Find current threshold */
4511 new->current_threshold
= -1;
4512 for (i
= 0; i
< size
; i
++) {
4513 if (new->entries
[i
].threshold
< usage
) {
4515 * new->current_threshold will not be used until
4516 * rcu_assign_pointer(), so it's safe to increment
4519 ++new->current_threshold
;
4523 /* Free old spare buffer and save old primary buffer as spare */
4524 kfree(thresholds
->spare
);
4525 thresholds
->spare
= thresholds
->primary
;
4527 rcu_assign_pointer(thresholds
->primary
, new);
4529 /* To be sure that nobody uses thresholds */
4533 mutex_unlock(&memcg
->thresholds_lock
);
4538 static void mem_cgroup_usage_unregister_event(struct cgroup
*cgrp
,
4539 struct cftype
*cft
, struct eventfd_ctx
*eventfd
)
4541 struct mem_cgroup
*memcg
= mem_cgroup_from_cont(cgrp
);
4542 struct mem_cgroup_thresholds
*thresholds
;
4543 struct mem_cgroup_threshold_ary
*new;
4544 int type
= MEMFILE_TYPE(cft
->private);
4548 mutex_lock(&memcg
->thresholds_lock
);
4550 thresholds
= &memcg
->thresholds
;
4551 else if (type
== _MEMSWAP
)
4552 thresholds
= &memcg
->memsw_thresholds
;
4557 * Something went wrong if we trying to unregister a threshold
4558 * if we don't have thresholds
4560 BUG_ON(!thresholds
);
4562 usage
= mem_cgroup_usage(memcg
, type
== _MEMSWAP
);
4564 /* Check if a threshold crossed before removing */
4565 __mem_cgroup_threshold(memcg
, type
== _MEMSWAP
);
4567 /* Calculate new number of threshold */
4569 for (i
= 0; i
< thresholds
->primary
->size
; i
++) {
4570 if (thresholds
->primary
->entries
[i
].eventfd
!= eventfd
)
4574 new = thresholds
->spare
;
4576 /* Set thresholds array to NULL if we don't have thresholds */
4585 /* Copy thresholds and find current threshold */
4586 new->current_threshold
= -1;
4587 for (i
= 0, j
= 0; i
< thresholds
->primary
->size
; i
++) {
4588 if (thresholds
->primary
->entries
[i
].eventfd
== eventfd
)
4591 new->entries
[j
] = thresholds
->primary
->entries
[i
];
4592 if (new->entries
[j
].threshold
< usage
) {
4594 * new->current_threshold will not be used
4595 * until rcu_assign_pointer(), so it's safe to increment
4598 ++new->current_threshold
;
4604 /* Swap primary and spare array */
4605 thresholds
->spare
= thresholds
->primary
;
4606 rcu_assign_pointer(thresholds
->primary
, new);
4608 /* To be sure that nobody uses thresholds */
4611 mutex_unlock(&memcg
->thresholds_lock
);
4614 static int mem_cgroup_oom_register_event(struct cgroup
*cgrp
,
4615 struct cftype
*cft
, struct eventfd_ctx
*eventfd
, const char *args
)
4617 struct mem_cgroup
*memcg
= mem_cgroup_from_cont(cgrp
);
4618 struct mem_cgroup_eventfd_list
*event
;
4619 int type
= MEMFILE_TYPE(cft
->private);
4621 BUG_ON(type
!= _OOM_TYPE
);
4622 event
= kmalloc(sizeof(*event
), GFP_KERNEL
);
4626 spin_lock(&memcg_oom_lock
);
4628 event
->eventfd
= eventfd
;
4629 list_add(&event
->list
, &memcg
->oom_notify
);
4631 /* already in OOM ? */
4632 if (atomic_read(&memcg
->under_oom
))
4633 eventfd_signal(eventfd
, 1);
4634 spin_unlock(&memcg_oom_lock
);
4639 static void mem_cgroup_oom_unregister_event(struct cgroup
*cgrp
,
4640 struct cftype
*cft
, struct eventfd_ctx
*eventfd
)
4642 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cgrp
);
4643 struct mem_cgroup_eventfd_list
*ev
, *tmp
;
4644 int type
= MEMFILE_TYPE(cft
->private);
4646 BUG_ON(type
!= _OOM_TYPE
);
4648 spin_lock(&memcg_oom_lock
);
4650 list_for_each_entry_safe(ev
, tmp
, &mem
->oom_notify
, list
) {
4651 if (ev
->eventfd
== eventfd
) {
4652 list_del(&ev
->list
);
4657 spin_unlock(&memcg_oom_lock
);
4660 static int mem_cgroup_oom_control_read(struct cgroup
*cgrp
,
4661 struct cftype
*cft
, struct cgroup_map_cb
*cb
)
4663 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cgrp
);
4665 cb
->fill(cb
, "oom_kill_disable", mem
->oom_kill_disable
);
4667 if (atomic_read(&mem
->under_oom
))
4668 cb
->fill(cb
, "under_oom", 1);
4670 cb
->fill(cb
, "under_oom", 0);
4674 static int mem_cgroup_oom_control_write(struct cgroup
*cgrp
,
4675 struct cftype
*cft
, u64 val
)
4677 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cgrp
);
4678 struct mem_cgroup
*parent
;
4680 /* cannot set to root cgroup and only 0 and 1 are allowed */
4681 if (!cgrp
->parent
|| !((val
== 0) || (val
== 1)))
4684 parent
= mem_cgroup_from_cont(cgrp
->parent
);
4687 /* oom-kill-disable is a flag for subhierarchy. */
4688 if ((parent
->use_hierarchy
) ||
4689 (mem
->use_hierarchy
&& !list_empty(&cgrp
->children
))) {
4693 mem
->oom_kill_disable
= val
;
4695 memcg_oom_recover(mem
);
4701 static const struct file_operations mem_control_numa_stat_file_operations
= {
4703 .llseek
= seq_lseek
,
4704 .release
= single_release
,
4707 static int mem_control_numa_stat_open(struct inode
*unused
, struct file
*file
)
4709 struct cgroup
*cont
= file
->f_dentry
->d_parent
->d_fsdata
;
4711 file
->f_op
= &mem_control_numa_stat_file_operations
;
4712 return single_open(file
, mem_control_numa_stat_show
, cont
);
4714 #endif /* CONFIG_NUMA */
4716 static int mem_cgroup_vmscan_stat_read(struct cgroup
*cgrp
,
4718 struct cgroup_map_cb
*cb
)
4720 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cgrp
);
4724 for (i
= 0; i
< NR_SCANSTATS
; i
++) {
4725 strcpy(string
, scanstat_string
[i
]);
4726 strcat(string
, SCANSTAT_WORD_LIMIT
);
4727 cb
->fill(cb
, string
, mem
->scanstat
.stats
[SCAN_BY_LIMIT
][i
]);
4730 for (i
= 0; i
< NR_SCANSTATS
; i
++) {
4731 strcpy(string
, scanstat_string
[i
]);
4732 strcat(string
, SCANSTAT_WORD_SYSTEM
);
4733 cb
->fill(cb
, string
, mem
->scanstat
.stats
[SCAN_BY_SYSTEM
][i
]);
4736 for (i
= 0; i
< NR_SCANSTATS
; i
++) {
4737 strcpy(string
, scanstat_string
[i
]);
4738 strcat(string
, SCANSTAT_WORD_LIMIT
);
4739 strcat(string
, SCANSTAT_WORD_HIERARCHY
);
4740 cb
->fill(cb
, string
, mem
->scanstat
.rootstats
[SCAN_BY_LIMIT
][i
]);
4742 for (i
= 0; i
< NR_SCANSTATS
; i
++) {
4743 strcpy(string
, scanstat_string
[i
]);
4744 strcat(string
, SCANSTAT_WORD_SYSTEM
);
4745 strcat(string
, SCANSTAT_WORD_HIERARCHY
);
4746 cb
->fill(cb
, string
, mem
->scanstat
.rootstats
[SCAN_BY_SYSTEM
][i
]);
4751 static int mem_cgroup_reset_vmscan_stat(struct cgroup
*cgrp
,
4754 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cgrp
);
4756 spin_lock(&mem
->scanstat
.lock
);
4757 memset(&mem
->scanstat
.stats
, 0, sizeof(mem
->scanstat
.stats
));
4758 memset(&mem
->scanstat
.rootstats
, 0, sizeof(mem
->scanstat
.rootstats
));
4759 spin_unlock(&mem
->scanstat
.lock
);
4764 static struct cftype mem_cgroup_files
[] = {
4766 .name
= "usage_in_bytes",
4767 .private = MEMFILE_PRIVATE(_MEM
, RES_USAGE
),
4768 .read_u64
= mem_cgroup_read
,
4769 .register_event
= mem_cgroup_usage_register_event
,
4770 .unregister_event
= mem_cgroup_usage_unregister_event
,
4773 .name
= "max_usage_in_bytes",
4774 .private = MEMFILE_PRIVATE(_MEM
, RES_MAX_USAGE
),
4775 .trigger
= mem_cgroup_reset
,
4776 .read_u64
= mem_cgroup_read
,
4779 .name
= "limit_in_bytes",
4780 .private = MEMFILE_PRIVATE(_MEM
, RES_LIMIT
),
4781 .write_string
= mem_cgroup_write
,
4782 .read_u64
= mem_cgroup_read
,
4785 .name
= "soft_limit_in_bytes",
4786 .private = MEMFILE_PRIVATE(_MEM
, RES_SOFT_LIMIT
),
4787 .write_string
= mem_cgroup_write
,
4788 .read_u64
= mem_cgroup_read
,
4792 .private = MEMFILE_PRIVATE(_MEM
, RES_FAILCNT
),
4793 .trigger
= mem_cgroup_reset
,
4794 .read_u64
= mem_cgroup_read
,
4798 .read_map
= mem_control_stat_show
,
4801 .name
= "force_empty",
4802 .trigger
= mem_cgroup_force_empty_write
,
4805 .name
= "use_hierarchy",
4806 .write_u64
= mem_cgroup_hierarchy_write
,
4807 .read_u64
= mem_cgroup_hierarchy_read
,
4810 .name
= "swappiness",
4811 .read_u64
= mem_cgroup_swappiness_read
,
4812 .write_u64
= mem_cgroup_swappiness_write
,
4815 .name
= "move_charge_at_immigrate",
4816 .read_u64
= mem_cgroup_move_charge_read
,
4817 .write_u64
= mem_cgroup_move_charge_write
,
4820 .name
= "oom_control",
4821 .read_map
= mem_cgroup_oom_control_read
,
4822 .write_u64
= mem_cgroup_oom_control_write
,
4823 .register_event
= mem_cgroup_oom_register_event
,
4824 .unregister_event
= mem_cgroup_oom_unregister_event
,
4825 .private = MEMFILE_PRIVATE(_OOM_TYPE
, OOM_CONTROL
),
4829 .name
= "numa_stat",
4830 .open
= mem_control_numa_stat_open
,
4835 .name
= "vmscan_stat",
4836 .read_map
= mem_cgroup_vmscan_stat_read
,
4837 .trigger
= mem_cgroup_reset_vmscan_stat
,
4841 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4842 static struct cftype memsw_cgroup_files
[] = {
4844 .name
= "memsw.usage_in_bytes",
4845 .private = MEMFILE_PRIVATE(_MEMSWAP
, RES_USAGE
),
4846 .read_u64
= mem_cgroup_read
,
4847 .register_event
= mem_cgroup_usage_register_event
,
4848 .unregister_event
= mem_cgroup_usage_unregister_event
,
4851 .name
= "memsw.max_usage_in_bytes",
4852 .private = MEMFILE_PRIVATE(_MEMSWAP
, RES_MAX_USAGE
),
4853 .trigger
= mem_cgroup_reset
,
4854 .read_u64
= mem_cgroup_read
,
4857 .name
= "memsw.limit_in_bytes",
4858 .private = MEMFILE_PRIVATE(_MEMSWAP
, RES_LIMIT
),
4859 .write_string
= mem_cgroup_write
,
4860 .read_u64
= mem_cgroup_read
,
4863 .name
= "memsw.failcnt",
4864 .private = MEMFILE_PRIVATE(_MEMSWAP
, RES_FAILCNT
),
4865 .trigger
= mem_cgroup_reset
,
4866 .read_u64
= mem_cgroup_read
,
4870 static int register_memsw_files(struct cgroup
*cont
, struct cgroup_subsys
*ss
)
4872 if (!do_swap_account
)
4874 return cgroup_add_files(cont
, ss
, memsw_cgroup_files
,
4875 ARRAY_SIZE(memsw_cgroup_files
));
4878 static int register_memsw_files(struct cgroup
*cont
, struct cgroup_subsys
*ss
)
4884 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup
*mem
, int node
)
4886 struct mem_cgroup_per_node
*pn
;
4887 struct mem_cgroup_per_zone
*mz
;
4889 int zone
, tmp
= node
;
4891 * This routine is called against possible nodes.
4892 * But it's BUG to call kmalloc() against offline node.
4894 * TODO: this routine can waste much memory for nodes which will
4895 * never be onlined. It's better to use memory hotplug callback
4898 if (!node_state(node
, N_NORMAL_MEMORY
))
4900 pn
= kzalloc_node(sizeof(*pn
), GFP_KERNEL
, tmp
);
4904 mem
->info
.nodeinfo
[node
] = pn
;
4905 for (zone
= 0; zone
< MAX_NR_ZONES
; zone
++) {
4906 mz
= &pn
->zoneinfo
[zone
];
4908 INIT_LIST_HEAD(&mz
->lists
[l
]);
4909 mz
->usage_in_excess
= 0;
4910 mz
->on_tree
= false;
4916 static void free_mem_cgroup_per_zone_info(struct mem_cgroup
*mem
, int node
)
4918 kfree(mem
->info
.nodeinfo
[node
]);
4921 static struct mem_cgroup
*mem_cgroup_alloc(void)
4923 struct mem_cgroup
*mem
;
4924 int size
= sizeof(struct mem_cgroup
);
4926 /* Can be very big if MAX_NUMNODES is very big */
4927 if (size
< PAGE_SIZE
)
4928 mem
= kzalloc(size
, GFP_KERNEL
);
4930 mem
= vzalloc(size
);
4935 mem
->stat
= alloc_percpu(struct mem_cgroup_stat_cpu
);
4938 spin_lock_init(&mem
->pcp_counter_lock
);
4942 if (size
< PAGE_SIZE
)
4950 * At destroying mem_cgroup, references from swap_cgroup can remain.
4951 * (scanning all at force_empty is too costly...)
4953 * Instead of clearing all references at force_empty, we remember
4954 * the number of reference from swap_cgroup and free mem_cgroup when
4955 * it goes down to 0.
4957 * Removal of cgroup itself succeeds regardless of refs from swap.
4960 static void __mem_cgroup_free(struct mem_cgroup
*mem
)
4964 mem_cgroup_remove_from_trees(mem
);
4965 free_css_id(&mem_cgroup_subsys
, &mem
->css
);
4967 for_each_node_state(node
, N_POSSIBLE
)
4968 free_mem_cgroup_per_zone_info(mem
, node
);
4970 free_percpu(mem
->stat
);
4971 if (sizeof(struct mem_cgroup
) < PAGE_SIZE
)
4977 static void mem_cgroup_get(struct mem_cgroup
*mem
)
4979 atomic_inc(&mem
->refcnt
);
4982 static void __mem_cgroup_put(struct mem_cgroup
*mem
, int count
)
4984 if (atomic_sub_and_test(count
, &mem
->refcnt
)) {
4985 struct mem_cgroup
*parent
= parent_mem_cgroup(mem
);
4986 __mem_cgroup_free(mem
);
4988 mem_cgroup_put(parent
);
4992 static void mem_cgroup_put(struct mem_cgroup
*mem
)
4994 __mem_cgroup_put(mem
, 1);
4998 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
5000 static struct mem_cgroup
*parent_mem_cgroup(struct mem_cgroup
*mem
)
5002 if (!mem
->res
.parent
)
5004 return mem_cgroup_from_res_counter(mem
->res
.parent
, res
);
5007 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
5008 static void __init
enable_swap_cgroup(void)
5010 if (!mem_cgroup_disabled() && really_do_swap_account
)
5011 do_swap_account
= 1;
5014 static void __init
enable_swap_cgroup(void)
5019 static int mem_cgroup_soft_limit_tree_init(void)
5021 struct mem_cgroup_tree_per_node
*rtpn
;
5022 struct mem_cgroup_tree_per_zone
*rtpz
;
5023 int tmp
, node
, zone
;
5025 for_each_node_state(node
, N_POSSIBLE
) {
5027 if (!node_state(node
, N_NORMAL_MEMORY
))
5029 rtpn
= kzalloc_node(sizeof(*rtpn
), GFP_KERNEL
, tmp
);
5033 soft_limit_tree
.rb_tree_per_node
[node
] = rtpn
;
5035 for (zone
= 0; zone
< MAX_NR_ZONES
; zone
++) {
5036 rtpz
= &rtpn
->rb_tree_per_zone
[zone
];
5037 rtpz
->rb_root
= RB_ROOT
;
5038 spin_lock_init(&rtpz
->lock
);
5044 static struct cgroup_subsys_state
* __ref
5045 mem_cgroup_create(struct cgroup_subsys
*ss
, struct cgroup
*cont
)
5047 struct mem_cgroup
*mem
, *parent
;
5048 long error
= -ENOMEM
;
5051 mem
= mem_cgroup_alloc();
5053 return ERR_PTR(error
);
5055 for_each_node_state(node
, N_POSSIBLE
)
5056 if (alloc_mem_cgroup_per_zone_info(mem
, node
))
5060 if (cont
->parent
== NULL
) {
5062 enable_swap_cgroup();
5064 root_mem_cgroup
= mem
;
5065 if (mem_cgroup_soft_limit_tree_init())
5067 for_each_possible_cpu(cpu
) {
5068 struct memcg_stock_pcp
*stock
=
5069 &per_cpu(memcg_stock
, cpu
);
5070 INIT_WORK(&stock
->work
, drain_local_stock
);
5072 hotcpu_notifier(memcg_cpu_hotplug_callback
, 0);
5074 parent
= mem_cgroup_from_cont(cont
->parent
);
5075 mem
->use_hierarchy
= parent
->use_hierarchy
;
5076 mem
->oom_kill_disable
= parent
->oom_kill_disable
;
5079 if (parent
&& parent
->use_hierarchy
) {
5080 res_counter_init(&mem
->res
, &parent
->res
);
5081 res_counter_init(&mem
->memsw
, &parent
->memsw
);
5083 * We increment refcnt of the parent to ensure that we can
5084 * safely access it on res_counter_charge/uncharge.
5085 * This refcnt will be decremented when freeing this
5086 * mem_cgroup(see mem_cgroup_put).
5088 mem_cgroup_get(parent
);
5090 res_counter_init(&mem
->res
, NULL
);
5091 res_counter_init(&mem
->memsw
, NULL
);
5093 mem
->last_scanned_child
= 0;
5094 mem
->last_scanned_node
= MAX_NUMNODES
;
5095 INIT_LIST_HEAD(&mem
->oom_notify
);
5098 mem
->swappiness
= mem_cgroup_swappiness(parent
);
5099 atomic_set(&mem
->refcnt
, 1);
5100 mem
->move_charge_at_immigrate
= 0;
5101 mutex_init(&mem
->thresholds_lock
);
5102 spin_lock_init(&mem
->scanstat
.lock
);
5105 __mem_cgroup_free(mem
);
5106 root_mem_cgroup
= NULL
;
5107 return ERR_PTR(error
);
5110 static int mem_cgroup_pre_destroy(struct cgroup_subsys
*ss
,
5111 struct cgroup
*cont
)
5113 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cont
);
5115 return mem_cgroup_force_empty(mem
, false);
5118 static void mem_cgroup_destroy(struct cgroup_subsys
*ss
,
5119 struct cgroup
*cont
)
5121 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cont
);
5123 mem_cgroup_put(mem
);
5126 static int mem_cgroup_populate(struct cgroup_subsys
*ss
,
5127 struct cgroup
*cont
)
5131 ret
= cgroup_add_files(cont
, ss
, mem_cgroup_files
,
5132 ARRAY_SIZE(mem_cgroup_files
));
5135 ret
= register_memsw_files(cont
, ss
);
5140 /* Handlers for move charge at task migration. */
5141 #define PRECHARGE_COUNT_AT_ONCE 256
5142 static int mem_cgroup_do_precharge(unsigned long count
)
5145 int batch_count
= PRECHARGE_COUNT_AT_ONCE
;
5146 struct mem_cgroup
*mem
= mc
.to
;
5148 if (mem_cgroup_is_root(mem
)) {
5149 mc
.precharge
+= count
;
5150 /* we don't need css_get for root */
5153 /* try to charge at once */
5155 struct res_counter
*dummy
;
5157 * "mem" cannot be under rmdir() because we've already checked
5158 * by cgroup_lock_live_cgroup() that it is not removed and we
5159 * are still under the same cgroup_mutex. So we can postpone
5162 if (res_counter_charge(&mem
->res
, PAGE_SIZE
* count
, &dummy
))
5164 if (do_swap_account
&& res_counter_charge(&mem
->memsw
,
5165 PAGE_SIZE
* count
, &dummy
)) {
5166 res_counter_uncharge(&mem
->res
, PAGE_SIZE
* count
);
5169 mc
.precharge
+= count
;
5173 /* fall back to one by one charge */
5175 if (signal_pending(current
)) {
5179 if (!batch_count
--) {
5180 batch_count
= PRECHARGE_COUNT_AT_ONCE
;
5183 ret
= __mem_cgroup_try_charge(NULL
, GFP_KERNEL
, 1, &mem
, false);
5185 /* mem_cgroup_clear_mc() will do uncharge later */
5193 * is_target_pte_for_mc - check a pte whether it is valid for move charge
5194 * @vma: the vma the pte to be checked belongs
5195 * @addr: the address corresponding to the pte to be checked
5196 * @ptent: the pte to be checked
5197 * @target: the pointer the target page or swap ent will be stored(can be NULL)
5200 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5201 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5202 * move charge. if @target is not NULL, the page is stored in target->page
5203 * with extra refcnt got(Callers should handle it).
5204 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5205 * target for charge migration. if @target is not NULL, the entry is stored
5208 * Called with pte lock held.
5215 enum mc_target_type
{
5216 MC_TARGET_NONE
, /* not used */
5221 static struct page
*mc_handle_present_pte(struct vm_area_struct
*vma
,
5222 unsigned long addr
, pte_t ptent
)
5224 struct page
*page
= vm_normal_page(vma
, addr
, ptent
);
5226 if (!page
|| !page_mapped(page
))
5228 if (PageAnon(page
)) {
5229 /* we don't move shared anon */
5230 if (!move_anon() || page_mapcount(page
) > 2)
5232 } else if (!move_file())
5233 /* we ignore mapcount for file pages */
5235 if (!get_page_unless_zero(page
))
5241 static struct page
*mc_handle_swap_pte(struct vm_area_struct
*vma
,
5242 unsigned long addr
, pte_t ptent
, swp_entry_t
*entry
)
5245 struct page
*page
= NULL
;
5246 swp_entry_t ent
= pte_to_swp_entry(ptent
);
5248 if (!move_anon() || non_swap_entry(ent
))
5250 usage_count
= mem_cgroup_count_swap_user(ent
, &page
);
5251 if (usage_count
> 1) { /* we don't move shared anon */
5256 if (do_swap_account
)
5257 entry
->val
= ent
.val
;
5262 static struct page
*mc_handle_file_pte(struct vm_area_struct
*vma
,
5263 unsigned long addr
, pte_t ptent
, swp_entry_t
*entry
)
5265 struct page
*page
= NULL
;
5266 struct inode
*inode
;
5267 struct address_space
*mapping
;
5270 if (!vma
->vm_file
) /* anonymous vma */
5275 inode
= vma
->vm_file
->f_path
.dentry
->d_inode
;
5276 mapping
= vma
->vm_file
->f_mapping
;
5277 if (pte_none(ptent
))
5278 pgoff
= linear_page_index(vma
, addr
);
5279 else /* pte_file(ptent) is true */
5280 pgoff
= pte_to_pgoff(ptent
);
5282 /* page is moved even if it's not RSS of this task(page-faulted). */
5283 page
= find_get_page(mapping
, pgoff
);
5286 /* shmem/tmpfs may report page out on swap: account for that too. */
5287 if (radix_tree_exceptional_entry(page
)) {
5288 swp_entry_t swap
= radix_to_swp_entry(page
);
5289 if (do_swap_account
)
5291 page
= find_get_page(&swapper_space
, swap
.val
);
5297 static int is_target_pte_for_mc(struct vm_area_struct
*vma
,
5298 unsigned long addr
, pte_t ptent
, union mc_target
*target
)
5300 struct page
*page
= NULL
;
5301 struct page_cgroup
*pc
;
5303 swp_entry_t ent
= { .val
= 0 };
5305 if (pte_present(ptent
))
5306 page
= mc_handle_present_pte(vma
, addr
, ptent
);
5307 else if (is_swap_pte(ptent
))
5308 page
= mc_handle_swap_pte(vma
, addr
, ptent
, &ent
);
5309 else if (pte_none(ptent
) || pte_file(ptent
))
5310 page
= mc_handle_file_pte(vma
, addr
, ptent
, &ent
);
5312 if (!page
&& !ent
.val
)
5315 pc
= lookup_page_cgroup(page
);
5317 * Do only loose check w/o page_cgroup lock.
5318 * mem_cgroup_move_account() checks the pc is valid or not under
5321 if (PageCgroupUsed(pc
) && pc
->mem_cgroup
== mc
.from
) {
5322 ret
= MC_TARGET_PAGE
;
5324 target
->page
= page
;
5326 if (!ret
|| !target
)
5329 /* There is a swap entry and a page doesn't exist or isn't charged */
5330 if (ent
.val
&& !ret
&&
5331 css_id(&mc
.from
->css
) == lookup_swap_cgroup(ent
)) {
5332 ret
= MC_TARGET_SWAP
;
5339 static int mem_cgroup_count_precharge_pte_range(pmd_t
*pmd
,
5340 unsigned long addr
, unsigned long end
,
5341 struct mm_walk
*walk
)
5343 struct vm_area_struct
*vma
= walk
->private;
5347 split_huge_page_pmd(walk
->mm
, pmd
);
5349 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, addr
, &ptl
);
5350 for (; addr
!= end
; pte
++, addr
+= PAGE_SIZE
)
5351 if (is_target_pte_for_mc(vma
, addr
, *pte
, NULL
))
5352 mc
.precharge
++; /* increment precharge temporarily */
5353 pte_unmap_unlock(pte
- 1, ptl
);
5359 static unsigned long mem_cgroup_count_precharge(struct mm_struct
*mm
)
5361 unsigned long precharge
;
5362 struct vm_area_struct
*vma
;
5364 down_read(&mm
->mmap_sem
);
5365 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
5366 struct mm_walk mem_cgroup_count_precharge_walk
= {
5367 .pmd_entry
= mem_cgroup_count_precharge_pte_range
,
5371 if (is_vm_hugetlb_page(vma
))
5373 walk_page_range(vma
->vm_start
, vma
->vm_end
,
5374 &mem_cgroup_count_precharge_walk
);
5376 up_read(&mm
->mmap_sem
);
5378 precharge
= mc
.precharge
;
5384 static int mem_cgroup_precharge_mc(struct mm_struct
*mm
)
5386 unsigned long precharge
= mem_cgroup_count_precharge(mm
);
5388 VM_BUG_ON(mc
.moving_task
);
5389 mc
.moving_task
= current
;
5390 return mem_cgroup_do_precharge(precharge
);
5393 /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5394 static void __mem_cgroup_clear_mc(void)
5396 struct mem_cgroup
*from
= mc
.from
;
5397 struct mem_cgroup
*to
= mc
.to
;
5399 /* we must uncharge all the leftover precharges from mc.to */
5401 __mem_cgroup_cancel_charge(mc
.to
, mc
.precharge
);
5405 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5406 * we must uncharge here.
5408 if (mc
.moved_charge
) {
5409 __mem_cgroup_cancel_charge(mc
.from
, mc
.moved_charge
);
5410 mc
.moved_charge
= 0;
5412 /* we must fixup refcnts and charges */
5413 if (mc
.moved_swap
) {
5414 /* uncharge swap account from the old cgroup */
5415 if (!mem_cgroup_is_root(mc
.from
))
5416 res_counter_uncharge(&mc
.from
->memsw
,
5417 PAGE_SIZE
* mc
.moved_swap
);
5418 __mem_cgroup_put(mc
.from
, mc
.moved_swap
);
5420 if (!mem_cgroup_is_root(mc
.to
)) {
5422 * we charged both to->res and to->memsw, so we should
5425 res_counter_uncharge(&mc
.to
->res
,
5426 PAGE_SIZE
* mc
.moved_swap
);
5428 /* we've already done mem_cgroup_get(mc.to) */
5431 memcg_oom_recover(from
);
5432 memcg_oom_recover(to
);
5433 wake_up_all(&mc
.waitq
);
5436 static void mem_cgroup_clear_mc(void)
5438 struct mem_cgroup
*from
= mc
.from
;
5441 * we must clear moving_task before waking up waiters at the end of
5444 mc
.moving_task
= NULL
;
5445 __mem_cgroup_clear_mc();
5446 spin_lock(&mc
.lock
);
5449 spin_unlock(&mc
.lock
);
5450 mem_cgroup_end_move(from
);
5453 static int mem_cgroup_can_attach(struct cgroup_subsys
*ss
,
5454 struct cgroup
*cgroup
,
5455 struct task_struct
*p
)
5458 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cgroup
);
5460 if (mem
->move_charge_at_immigrate
) {
5461 struct mm_struct
*mm
;
5462 struct mem_cgroup
*from
= mem_cgroup_from_task(p
);
5464 VM_BUG_ON(from
== mem
);
5466 mm
= get_task_mm(p
);
5469 /* We move charges only when we move a owner of the mm */
5470 if (mm
->owner
== p
) {
5473 VM_BUG_ON(mc
.precharge
);
5474 VM_BUG_ON(mc
.moved_charge
);
5475 VM_BUG_ON(mc
.moved_swap
);
5476 mem_cgroup_start_move(from
);
5477 spin_lock(&mc
.lock
);
5480 spin_unlock(&mc
.lock
);
5481 /* We set mc.moving_task later */
5483 ret
= mem_cgroup_precharge_mc(mm
);
5485 mem_cgroup_clear_mc();
5492 static void mem_cgroup_cancel_attach(struct cgroup_subsys
*ss
,
5493 struct cgroup
*cgroup
,
5494 struct task_struct
*p
)
5496 mem_cgroup_clear_mc();
5499 static int mem_cgroup_move_charge_pte_range(pmd_t
*pmd
,
5500 unsigned long addr
, unsigned long end
,
5501 struct mm_walk
*walk
)
5504 struct vm_area_struct
*vma
= walk
->private;
5508 split_huge_page_pmd(walk
->mm
, pmd
);
5510 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, addr
, &ptl
);
5511 for (; addr
!= end
; addr
+= PAGE_SIZE
) {
5512 pte_t ptent
= *(pte
++);
5513 union mc_target target
;
5516 struct page_cgroup
*pc
;
5522 type
= is_target_pte_for_mc(vma
, addr
, ptent
, &target
);
5524 case MC_TARGET_PAGE
:
5526 if (isolate_lru_page(page
))
5528 pc
= lookup_page_cgroup(page
);
5529 if (!mem_cgroup_move_account(page
, 1, pc
,
5530 mc
.from
, mc
.to
, false)) {
5532 /* we uncharge from mc.from later. */
5535 putback_lru_page(page
);
5536 put
: /* is_target_pte_for_mc() gets the page */
5539 case MC_TARGET_SWAP
:
5541 if (!mem_cgroup_move_swap_account(ent
,
5542 mc
.from
, mc
.to
, false)) {
5544 /* we fixup refcnts and charges later. */
5552 pte_unmap_unlock(pte
- 1, ptl
);
5557 * We have consumed all precharges we got in can_attach().
5558 * We try charge one by one, but don't do any additional
5559 * charges to mc.to if we have failed in charge once in attach()
5562 ret
= mem_cgroup_do_precharge(1);
5570 static void mem_cgroup_move_charge(struct mm_struct
*mm
)
5572 struct vm_area_struct
*vma
;
5574 lru_add_drain_all();
5576 if (unlikely(!down_read_trylock(&mm
->mmap_sem
))) {
5578 * Someone who are holding the mmap_sem might be waiting in
5579 * waitq. So we cancel all extra charges, wake up all waiters,
5580 * and retry. Because we cancel precharges, we might not be able
5581 * to move enough charges, but moving charge is a best-effort
5582 * feature anyway, so it wouldn't be a big problem.
5584 __mem_cgroup_clear_mc();
5588 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
5590 struct mm_walk mem_cgroup_move_charge_walk
= {
5591 .pmd_entry
= mem_cgroup_move_charge_pte_range
,
5595 if (is_vm_hugetlb_page(vma
))
5597 ret
= walk_page_range(vma
->vm_start
, vma
->vm_end
,
5598 &mem_cgroup_move_charge_walk
);
5601 * means we have consumed all precharges and failed in
5602 * doing additional charge. Just abandon here.
5606 up_read(&mm
->mmap_sem
);
5609 static void mem_cgroup_move_task(struct cgroup_subsys
*ss
,
5610 struct cgroup
*cont
,
5611 struct cgroup
*old_cont
,
5612 struct task_struct
*p
)
5614 struct mm_struct
*mm
= get_task_mm(p
);
5618 mem_cgroup_move_charge(mm
);
5623 mem_cgroup_clear_mc();
5625 #else /* !CONFIG_MMU */
5626 static int mem_cgroup_can_attach(struct cgroup_subsys
*ss
,
5627 struct cgroup
*cgroup
,
5628 struct task_struct
*p
)
5632 static void mem_cgroup_cancel_attach(struct cgroup_subsys
*ss
,
5633 struct cgroup
*cgroup
,
5634 struct task_struct
*p
)
5637 static void mem_cgroup_move_task(struct cgroup_subsys
*ss
,
5638 struct cgroup
*cont
,
5639 struct cgroup
*old_cont
,
5640 struct task_struct
*p
)
5645 struct cgroup_subsys mem_cgroup_subsys
= {
5647 .subsys_id
= mem_cgroup_subsys_id
,
5648 .create
= mem_cgroup_create
,
5649 .pre_destroy
= mem_cgroup_pre_destroy
,
5650 .destroy
= mem_cgroup_destroy
,
5651 .populate
= mem_cgroup_populate
,
5652 .can_attach
= mem_cgroup_can_attach
,
5653 .cancel_attach
= mem_cgroup_cancel_attach
,
5654 .attach
= mem_cgroup_move_task
,
5659 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
5660 static int __init
enable_swap_account(char *s
)
5662 /* consider enabled if no parameter or 1 is given */
5663 if (!strcmp(s
, "1"))
5664 really_do_swap_account
= 1;
5665 else if (!strcmp(s
, "0"))
5666 really_do_swap_account
= 0;
5669 __setup("swapaccount=", enable_swap_account
);