Commit | Line | Data |
---|---|---|
8cdea7c0 BS |
1 | /* memcontrol.c - Memory Controller |
2 | * | |
3 | * Copyright IBM Corporation, 2007 | |
4 | * Author Balbir Singh <balbir@linux.vnet.ibm.com> | |
5 | * | |
78fb7466 PE |
6 | * Copyright 2007 OpenVZ SWsoft Inc |
7 | * Author: Pavel Emelianov <xemul@openvz.org> | |
8 | * | |
2e72b634 KS |
9 | * Memory thresholds |
10 | * Copyright (C) 2009 Nokia Corporation | |
11 | * Author: Kirill A. Shutemov | |
12 | * | |
8cdea7c0 BS |
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. | |
17 | * | |
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. | |
22 | */ | |
23 | ||
24 | #include <linux/res_counter.h> | |
25 | #include <linux/memcontrol.h> | |
26 | #include <linux/cgroup.h> | |
78fb7466 | 27 | #include <linux/mm.h> |
4ffef5fe | 28 | #include <linux/hugetlb.h> |
d13d1443 | 29 | #include <linux/pagemap.h> |
d52aa412 | 30 | #include <linux/smp.h> |
8a9f3ccd | 31 | #include <linux/page-flags.h> |
66e1707b | 32 | #include <linux/backing-dev.h> |
8a9f3ccd BS |
33 | #include <linux/bit_spinlock.h> |
34 | #include <linux/rcupdate.h> | |
e222432b | 35 | #include <linux/limits.h> |
b9e15baf | 36 | #include <linux/export.h> |
8c7c6e34 | 37 | #include <linux/mutex.h> |
f64c3f54 | 38 | #include <linux/rbtree.h> |
b6ac57d5 | 39 | #include <linux/slab.h> |
66e1707b | 40 | #include <linux/swap.h> |
02491447 | 41 | #include <linux/swapops.h> |
66e1707b | 42 | #include <linux/spinlock.h> |
2e72b634 KS |
43 | #include <linux/eventfd.h> |
44 | #include <linux/sort.h> | |
66e1707b | 45 | #include <linux/fs.h> |
d2ceb9b7 | 46 | #include <linux/seq_file.h> |
33327948 | 47 | #include <linux/vmalloc.h> |
b69408e8 | 48 | #include <linux/mm_inline.h> |
52d4b9ac | 49 | #include <linux/page_cgroup.h> |
cdec2e42 | 50 | #include <linux/cpu.h> |
158e0a2d | 51 | #include <linux/oom.h> |
08e552c6 | 52 | #include "internal.h" |
8cdea7c0 | 53 | |
8697d331 BS |
54 | #include <asm/uaccess.h> |
55 | ||
cc8e970c KM |
56 | #include <trace/events/vmscan.h> |
57 | ||
a181b0e8 | 58 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
a181b0e8 | 59 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
4b3bde4c | 60 | struct mem_cgroup *root_mem_cgroup __read_mostly; |
8cdea7c0 | 61 | |
c077719b | 62 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
338c8431 | 63 | /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */ |
c077719b | 64 | int do_swap_account __read_mostly; |
a42c390c MH |
65 | |
66 | /* for remember boot option*/ | |
67 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP_ENABLED | |
68 | static int really_do_swap_account __initdata = 1; | |
69 | #else | |
70 | static int really_do_swap_account __initdata = 0; | |
71 | #endif | |
72 | ||
c077719b KH |
73 | #else |
74 | #define do_swap_account (0) | |
75 | #endif | |
76 | ||
77 | ||
d52aa412 KH |
78 | /* |
79 | * Statistics for memory cgroup. | |
80 | */ | |
81 | enum mem_cgroup_stat_index { | |
82 | /* | |
83 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. | |
84 | */ | |
85 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ | |
d69b042f | 86 | MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */ |
d8046582 | 87 | MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */ |
0c3e73e8 | 88 | MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */ |
711d3d2c | 89 | MEM_CGROUP_STAT_DATA, /* end of data requires synchronization */ |
32047e2a | 90 | MEM_CGROUP_ON_MOVE, /* someone is moving account between groups */ |
d52aa412 KH |
91 | MEM_CGROUP_STAT_NSTATS, |
92 | }; | |
93 | ||
e9f8974f JW |
94 | enum mem_cgroup_events_index { |
95 | MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */ | |
96 | MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */ | |
97 | MEM_CGROUP_EVENTS_COUNT, /* # of pages paged in/out */ | |
456f998e YH |
98 | MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */ |
99 | MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */ | |
e9f8974f JW |
100 | MEM_CGROUP_EVENTS_NSTATS, |
101 | }; | |
7a159cc9 JW |
102 | /* |
103 | * Per memcg event counter is incremented at every pagein/pageout. With THP, | |
104 | * it will be incremated by the number of pages. This counter is used for | |
105 | * for trigger some periodic events. This is straightforward and better | |
106 | * than using jiffies etc. to handle periodic memcg event. | |
107 | */ | |
108 | enum mem_cgroup_events_target { | |
109 | MEM_CGROUP_TARGET_THRESH, | |
110 | MEM_CGROUP_TARGET_SOFTLIMIT, | |
453a9bf3 | 111 | MEM_CGROUP_TARGET_NUMAINFO, |
7a159cc9 JW |
112 | MEM_CGROUP_NTARGETS, |
113 | }; | |
114 | #define THRESHOLDS_EVENTS_TARGET (128) | |
115 | #define SOFTLIMIT_EVENTS_TARGET (1024) | |
453a9bf3 | 116 | #define NUMAINFO_EVENTS_TARGET (1024) |
e9f8974f | 117 | |
d52aa412 | 118 | struct mem_cgroup_stat_cpu { |
7a159cc9 | 119 | long count[MEM_CGROUP_STAT_NSTATS]; |
e9f8974f | 120 | unsigned long events[MEM_CGROUP_EVENTS_NSTATS]; |
7a159cc9 | 121 | unsigned long targets[MEM_CGROUP_NTARGETS]; |
d52aa412 KH |
122 | }; |
123 | ||
6d12e2d8 KH |
124 | /* |
125 | * per-zone information in memory controller. | |
126 | */ | |
6d12e2d8 | 127 | struct mem_cgroup_per_zone { |
072c56c1 KH |
128 | /* |
129 | * spin_lock to protect the per cgroup LRU | |
130 | */ | |
b69408e8 CL |
131 | struct list_head lists[NR_LRU_LISTS]; |
132 | unsigned long count[NR_LRU_LISTS]; | |
3e2f41f1 KM |
133 | |
134 | struct zone_reclaim_stat reclaim_stat; | |
f64c3f54 BS |
135 | struct rb_node tree_node; /* RB tree node */ |
136 | unsigned long long usage_in_excess;/* Set to the value by which */ | |
137 | /* the soft limit is exceeded*/ | |
138 | bool on_tree; | |
4e416953 BS |
139 | struct mem_cgroup *mem; /* Back pointer, we cannot */ |
140 | /* use container_of */ | |
6d12e2d8 KH |
141 | }; |
142 | /* Macro for accessing counter */ | |
143 | #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)]) | |
144 | ||
145 | struct mem_cgroup_per_node { | |
146 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; | |
147 | }; | |
148 | ||
149 | struct mem_cgroup_lru_info { | |
150 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; | |
151 | }; | |
152 | ||
f64c3f54 BS |
153 | /* |
154 | * Cgroups above their limits are maintained in a RB-Tree, independent of | |
155 | * their hierarchy representation | |
156 | */ | |
157 | ||
158 | struct mem_cgroup_tree_per_zone { | |
159 | struct rb_root rb_root; | |
160 | spinlock_t lock; | |
161 | }; | |
162 | ||
163 | struct mem_cgroup_tree_per_node { | |
164 | struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES]; | |
165 | }; | |
166 | ||
167 | struct mem_cgroup_tree { | |
168 | struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES]; | |
169 | }; | |
170 | ||
171 | static struct mem_cgroup_tree soft_limit_tree __read_mostly; | |
172 | ||
2e72b634 KS |
173 | struct mem_cgroup_threshold { |
174 | struct eventfd_ctx *eventfd; | |
175 | u64 threshold; | |
176 | }; | |
177 | ||
9490ff27 | 178 | /* For threshold */ |
2e72b634 KS |
179 | struct mem_cgroup_threshold_ary { |
180 | /* An array index points to threshold just below usage. */ | |
5407a562 | 181 | int current_threshold; |
2e72b634 KS |
182 | /* Size of entries[] */ |
183 | unsigned int size; | |
184 | /* Array of thresholds */ | |
185 | struct mem_cgroup_threshold entries[0]; | |
186 | }; | |
2c488db2 KS |
187 | |
188 | struct mem_cgroup_thresholds { | |
189 | /* Primary thresholds array */ | |
190 | struct mem_cgroup_threshold_ary *primary; | |
191 | /* | |
192 | * Spare threshold array. | |
193 | * This is needed to make mem_cgroup_unregister_event() "never fail". | |
194 | * It must be able to store at least primary->size - 1 entries. | |
195 | */ | |
196 | struct mem_cgroup_threshold_ary *spare; | |
197 | }; | |
198 | ||
9490ff27 KH |
199 | /* for OOM */ |
200 | struct mem_cgroup_eventfd_list { | |
201 | struct list_head list; | |
202 | struct eventfd_ctx *eventfd; | |
203 | }; | |
2e72b634 | 204 | |
c0ff4b85 R |
205 | static void mem_cgroup_threshold(struct mem_cgroup *memcg); |
206 | static void mem_cgroup_oom_notify(struct mem_cgroup *memcg); | |
2e72b634 | 207 | |
8cdea7c0 BS |
208 | /* |
209 | * The memory controller data structure. The memory controller controls both | |
210 | * page cache and RSS per cgroup. We would eventually like to provide | |
211 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, | |
212 | * to help the administrator determine what knobs to tune. | |
213 | * | |
214 | * TODO: Add a water mark for the memory controller. Reclaim will begin when | |
8a9f3ccd BS |
215 | * we hit the water mark. May be even add a low water mark, such that |
216 | * no reclaim occurs from a cgroup at it's low water mark, this is | |
217 | * a feature that will be implemented much later in the future. | |
8cdea7c0 BS |
218 | */ |
219 | struct mem_cgroup { | |
220 | struct cgroup_subsys_state css; | |
221 | /* | |
222 | * the counter to account for memory usage | |
223 | */ | |
224 | struct res_counter res; | |
8c7c6e34 KH |
225 | /* |
226 | * the counter to account for mem+swap usage. | |
227 | */ | |
228 | struct res_counter memsw; | |
e5671dfa GC |
229 | /* |
230 | * the counter to account for kmem usage. | |
231 | */ | |
232 | struct res_counter kmem; | |
78fb7466 PE |
233 | /* |
234 | * Per cgroup active and inactive list, similar to the | |
235 | * per zone LRU lists. | |
78fb7466 | 236 | */ |
6d12e2d8 | 237 | struct mem_cgroup_lru_info info; |
6d61ef40 | 238 | /* |
af901ca1 | 239 | * While reclaiming in a hierarchy, we cache the last child we |
04046e1a | 240 | * reclaimed from. |
6d61ef40 | 241 | */ |
04046e1a | 242 | int last_scanned_child; |
889976db YH |
243 | int last_scanned_node; |
244 | #if MAX_NUMNODES > 1 | |
245 | nodemask_t scan_nodes; | |
453a9bf3 KH |
246 | atomic_t numainfo_events; |
247 | atomic_t numainfo_updating; | |
889976db | 248 | #endif |
18f59ea7 BS |
249 | /* |
250 | * Should the accounting and control be hierarchical, per subtree? | |
251 | */ | |
252 | bool use_hierarchy; | |
79dfdacc MH |
253 | |
254 | bool oom_lock; | |
255 | atomic_t under_oom; | |
256 | ||
8c7c6e34 | 257 | atomic_t refcnt; |
14797e23 | 258 | |
1f4c025b | 259 | int swappiness; |
3c11ecf4 KH |
260 | /* OOM-Killer disable */ |
261 | int oom_kill_disable; | |
a7885eb8 | 262 | |
22a668d7 KH |
263 | /* set when res.limit == memsw.limit */ |
264 | bool memsw_is_minimum; | |
265 | ||
2e72b634 KS |
266 | /* protect arrays of thresholds */ |
267 | struct mutex thresholds_lock; | |
268 | ||
269 | /* thresholds for memory usage. RCU-protected */ | |
2c488db2 | 270 | struct mem_cgroup_thresholds thresholds; |
907860ed | 271 | |
2e72b634 | 272 | /* thresholds for mem+swap usage. RCU-protected */ |
2c488db2 | 273 | struct mem_cgroup_thresholds memsw_thresholds; |
907860ed | 274 | |
9490ff27 KH |
275 | /* For oom notifier event fd */ |
276 | struct list_head oom_notify; | |
185efc0f | 277 | |
7dc74be0 DN |
278 | /* |
279 | * Should we move charges of a task when a task is moved into this | |
280 | * mem_cgroup ? And what type of charges should we move ? | |
281 | */ | |
282 | unsigned long move_charge_at_immigrate; | |
e5671dfa GC |
283 | /* |
284 | * Should kernel memory limits be stabilished independently | |
285 | * from user memory ? | |
286 | */ | |
287 | int kmem_independent_accounting; | |
d52aa412 | 288 | /* |
c62b1a3b | 289 | * percpu counter. |
d52aa412 | 290 | */ |
c62b1a3b | 291 | struct mem_cgroup_stat_cpu *stat; |
711d3d2c KH |
292 | /* |
293 | * used when a cpu is offlined or other synchronizations | |
294 | * See mem_cgroup_read_stat(). | |
295 | */ | |
296 | struct mem_cgroup_stat_cpu nocpu_base; | |
297 | spinlock_t pcp_counter_lock; | |
8cdea7c0 BS |
298 | }; |
299 | ||
7dc74be0 DN |
300 | /* Stuffs for move charges at task migration. */ |
301 | /* | |
302 | * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a | |
303 | * left-shifted bitmap of these types. | |
304 | */ | |
305 | enum move_type { | |
4ffef5fe | 306 | MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */ |
87946a72 | 307 | MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */ |
7dc74be0 DN |
308 | NR_MOVE_TYPE, |
309 | }; | |
310 | ||
4ffef5fe DN |
311 | /* "mc" and its members are protected by cgroup_mutex */ |
312 | static struct move_charge_struct { | |
b1dd693e | 313 | spinlock_t lock; /* for from, to */ |
4ffef5fe DN |
314 | struct mem_cgroup *from; |
315 | struct mem_cgroup *to; | |
316 | unsigned long precharge; | |
854ffa8d | 317 | unsigned long moved_charge; |
483c30b5 | 318 | unsigned long moved_swap; |
8033b97c DN |
319 | struct task_struct *moving_task; /* a task moving charges */ |
320 | wait_queue_head_t waitq; /* a waitq for other context */ | |
321 | } mc = { | |
2bd9bb20 | 322 | .lock = __SPIN_LOCK_UNLOCKED(mc.lock), |
8033b97c DN |
323 | .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq), |
324 | }; | |
4ffef5fe | 325 | |
90254a65 DN |
326 | static bool move_anon(void) |
327 | { | |
328 | return test_bit(MOVE_CHARGE_TYPE_ANON, | |
329 | &mc.to->move_charge_at_immigrate); | |
330 | } | |
331 | ||
87946a72 DN |
332 | static bool move_file(void) |
333 | { | |
334 | return test_bit(MOVE_CHARGE_TYPE_FILE, | |
335 | &mc.to->move_charge_at_immigrate); | |
336 | } | |
337 | ||
4e416953 BS |
338 | /* |
339 | * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft | |
340 | * limit reclaim to prevent infinite loops, if they ever occur. | |
341 | */ | |
342 | #define MEM_CGROUP_MAX_RECLAIM_LOOPS (100) | |
343 | #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2) | |
344 | ||
217bc319 KH |
345 | enum charge_type { |
346 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, | |
347 | MEM_CGROUP_CHARGE_TYPE_MAPPED, | |
4f98a2fe | 348 | MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */ |
c05555b5 | 349 | MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */ |
d13d1443 | 350 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
8a9478ca | 351 | MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */ |
c05555b5 KH |
352 | NR_CHARGE_TYPE, |
353 | }; | |
354 | ||
8c7c6e34 | 355 | /* for encoding cft->private value on file */ |
e5671dfa GC |
356 | |
357 | enum mem_type { | |
358 | _MEM = 0, | |
359 | _MEMSWAP, | |
360 | _OOM_TYPE, | |
361 | _KMEM, | |
362 | }; | |
363 | ||
8c7c6e34 KH |
364 | #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val)) |
365 | #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff) | |
366 | #define MEMFILE_ATTR(val) ((val) & 0xffff) | |
9490ff27 KH |
367 | /* Used for OOM nofiier */ |
368 | #define OOM_CONTROL (0) | |
8c7c6e34 | 369 | |
75822b44 BS |
370 | /* |
371 | * Reclaim flags for mem_cgroup_hierarchical_reclaim | |
372 | */ | |
373 | #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0 | |
374 | #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT) | |
375 | #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1 | |
376 | #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT) | |
4e416953 BS |
377 | #define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2 |
378 | #define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT) | |
75822b44 | 379 | |
c0ff4b85 R |
380 | static void mem_cgroup_get(struct mem_cgroup *memcg); |
381 | static void mem_cgroup_put(struct mem_cgroup *memcg); | |
382 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg); | |
383 | static void drain_all_stock_async(struct mem_cgroup *memcg); | |
8c7c6e34 | 384 | |
f64c3f54 | 385 | static struct mem_cgroup_per_zone * |
c0ff4b85 | 386 | mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid) |
f64c3f54 | 387 | { |
c0ff4b85 | 388 | return &memcg->info.nodeinfo[nid]->zoneinfo[zid]; |
f64c3f54 BS |
389 | } |
390 | ||
c0ff4b85 | 391 | struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg) |
d324236b | 392 | { |
c0ff4b85 | 393 | return &memcg->css; |
d324236b WF |
394 | } |
395 | ||
f64c3f54 | 396 | static struct mem_cgroup_per_zone * |
c0ff4b85 | 397 | page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page) |
f64c3f54 | 398 | { |
97a6c37b JW |
399 | int nid = page_to_nid(page); |
400 | int zid = page_zonenum(page); | |
f64c3f54 | 401 | |
c0ff4b85 | 402 | return mem_cgroup_zoneinfo(memcg, nid, zid); |
f64c3f54 BS |
403 | } |
404 | ||
405 | static struct mem_cgroup_tree_per_zone * | |
406 | soft_limit_tree_node_zone(int nid, int zid) | |
407 | { | |
408 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; | |
409 | } | |
410 | ||
411 | static struct mem_cgroup_tree_per_zone * | |
412 | soft_limit_tree_from_page(struct page *page) | |
413 | { | |
414 | int nid = page_to_nid(page); | |
415 | int zid = page_zonenum(page); | |
416 | ||
417 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; | |
418 | } | |
419 | ||
420 | static void | |
c0ff4b85 | 421 | __mem_cgroup_insert_exceeded(struct mem_cgroup *memcg, |
f64c3f54 | 422 | struct mem_cgroup_per_zone *mz, |
ef8745c1 KH |
423 | struct mem_cgroup_tree_per_zone *mctz, |
424 | unsigned long long new_usage_in_excess) | |
f64c3f54 BS |
425 | { |
426 | struct rb_node **p = &mctz->rb_root.rb_node; | |
427 | struct rb_node *parent = NULL; | |
428 | struct mem_cgroup_per_zone *mz_node; | |
429 | ||
430 | if (mz->on_tree) | |
431 | return; | |
432 | ||
ef8745c1 KH |
433 | mz->usage_in_excess = new_usage_in_excess; |
434 | if (!mz->usage_in_excess) | |
435 | return; | |
f64c3f54 BS |
436 | while (*p) { |
437 | parent = *p; | |
438 | mz_node = rb_entry(parent, struct mem_cgroup_per_zone, | |
439 | tree_node); | |
440 | if (mz->usage_in_excess < mz_node->usage_in_excess) | |
441 | p = &(*p)->rb_left; | |
442 | /* | |
443 | * We can't avoid mem cgroups that are over their soft | |
444 | * limit by the same amount | |
445 | */ | |
446 | else if (mz->usage_in_excess >= mz_node->usage_in_excess) | |
447 | p = &(*p)->rb_right; | |
448 | } | |
449 | rb_link_node(&mz->tree_node, parent, p); | |
450 | rb_insert_color(&mz->tree_node, &mctz->rb_root); | |
451 | mz->on_tree = true; | |
4e416953 BS |
452 | } |
453 | ||
454 | static void | |
c0ff4b85 | 455 | __mem_cgroup_remove_exceeded(struct mem_cgroup *memcg, |
4e416953 BS |
456 | struct mem_cgroup_per_zone *mz, |
457 | struct mem_cgroup_tree_per_zone *mctz) | |
458 | { | |
459 | if (!mz->on_tree) | |
460 | return; | |
461 | rb_erase(&mz->tree_node, &mctz->rb_root); | |
462 | mz->on_tree = false; | |
463 | } | |
464 | ||
f64c3f54 | 465 | static void |
c0ff4b85 | 466 | mem_cgroup_remove_exceeded(struct mem_cgroup *memcg, |
f64c3f54 BS |
467 | struct mem_cgroup_per_zone *mz, |
468 | struct mem_cgroup_tree_per_zone *mctz) | |
469 | { | |
470 | spin_lock(&mctz->lock); | |
c0ff4b85 | 471 | __mem_cgroup_remove_exceeded(memcg, mz, mctz); |
f64c3f54 BS |
472 | spin_unlock(&mctz->lock); |
473 | } | |
474 | ||
f64c3f54 | 475 | |
c0ff4b85 | 476 | static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page) |
f64c3f54 | 477 | { |
ef8745c1 | 478 | unsigned long long excess; |
f64c3f54 BS |
479 | struct mem_cgroup_per_zone *mz; |
480 | struct mem_cgroup_tree_per_zone *mctz; | |
4e649152 KH |
481 | int nid = page_to_nid(page); |
482 | int zid = page_zonenum(page); | |
f64c3f54 BS |
483 | mctz = soft_limit_tree_from_page(page); |
484 | ||
485 | /* | |
4e649152 KH |
486 | * Necessary to update all ancestors when hierarchy is used. |
487 | * because their event counter is not touched. | |
f64c3f54 | 488 | */ |
c0ff4b85 R |
489 | for (; memcg; memcg = parent_mem_cgroup(memcg)) { |
490 | mz = mem_cgroup_zoneinfo(memcg, nid, zid); | |
491 | excess = res_counter_soft_limit_excess(&memcg->res); | |
4e649152 KH |
492 | /* |
493 | * We have to update the tree if mz is on RB-tree or | |
494 | * mem is over its softlimit. | |
495 | */ | |
ef8745c1 | 496 | if (excess || mz->on_tree) { |
4e649152 KH |
497 | spin_lock(&mctz->lock); |
498 | /* if on-tree, remove it */ | |
499 | if (mz->on_tree) | |
c0ff4b85 | 500 | __mem_cgroup_remove_exceeded(memcg, mz, mctz); |
4e649152 | 501 | /* |
ef8745c1 KH |
502 | * Insert again. mz->usage_in_excess will be updated. |
503 | * If excess is 0, no tree ops. | |
4e649152 | 504 | */ |
c0ff4b85 | 505 | __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess); |
4e649152 KH |
506 | spin_unlock(&mctz->lock); |
507 | } | |
f64c3f54 BS |
508 | } |
509 | } | |
510 | ||
c0ff4b85 | 511 | static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg) |
f64c3f54 BS |
512 | { |
513 | int node, zone; | |
514 | struct mem_cgroup_per_zone *mz; | |
515 | struct mem_cgroup_tree_per_zone *mctz; | |
516 | ||
517 | for_each_node_state(node, N_POSSIBLE) { | |
518 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { | |
c0ff4b85 | 519 | mz = mem_cgroup_zoneinfo(memcg, node, zone); |
f64c3f54 | 520 | mctz = soft_limit_tree_node_zone(node, zone); |
c0ff4b85 | 521 | mem_cgroup_remove_exceeded(memcg, mz, mctz); |
f64c3f54 BS |
522 | } |
523 | } | |
524 | } | |
525 | ||
4e416953 BS |
526 | static struct mem_cgroup_per_zone * |
527 | __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) | |
528 | { | |
529 | struct rb_node *rightmost = NULL; | |
26251eaf | 530 | struct mem_cgroup_per_zone *mz; |
4e416953 BS |
531 | |
532 | retry: | |
26251eaf | 533 | mz = NULL; |
4e416953 BS |
534 | rightmost = rb_last(&mctz->rb_root); |
535 | if (!rightmost) | |
536 | goto done; /* Nothing to reclaim from */ | |
537 | ||
538 | mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node); | |
539 | /* | |
540 | * Remove the node now but someone else can add it back, | |
541 | * we will to add it back at the end of reclaim to its correct | |
542 | * position in the tree. | |
543 | */ | |
544 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); | |
545 | if (!res_counter_soft_limit_excess(&mz->mem->res) || | |
546 | !css_tryget(&mz->mem->css)) | |
547 | goto retry; | |
548 | done: | |
549 | return mz; | |
550 | } | |
551 | ||
552 | static struct mem_cgroup_per_zone * | |
553 | mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) | |
554 | { | |
555 | struct mem_cgroup_per_zone *mz; | |
556 | ||
557 | spin_lock(&mctz->lock); | |
558 | mz = __mem_cgroup_largest_soft_limit_node(mctz); | |
559 | spin_unlock(&mctz->lock); | |
560 | return mz; | |
561 | } | |
562 | ||
711d3d2c KH |
563 | /* |
564 | * Implementation Note: reading percpu statistics for memcg. | |
565 | * | |
566 | * Both of vmstat[] and percpu_counter has threshold and do periodic | |
567 | * synchronization to implement "quick" read. There are trade-off between | |
568 | * reading cost and precision of value. Then, we may have a chance to implement | |
569 | * a periodic synchronizion of counter in memcg's counter. | |
570 | * | |
571 | * But this _read() function is used for user interface now. The user accounts | |
572 | * memory usage by memory cgroup and he _always_ requires exact value because | |
573 | * he accounts memory. Even if we provide quick-and-fuzzy read, we always | |
574 | * have to visit all online cpus and make sum. So, for now, unnecessary | |
575 | * synchronization is not implemented. (just implemented for cpu hotplug) | |
576 | * | |
577 | * If there are kernel internal actions which can make use of some not-exact | |
578 | * value, and reading all cpu value can be performance bottleneck in some | |
579 | * common workload, threashold and synchonization as vmstat[] should be | |
580 | * implemented. | |
581 | */ | |
c0ff4b85 | 582 | static long mem_cgroup_read_stat(struct mem_cgroup *memcg, |
7a159cc9 | 583 | enum mem_cgroup_stat_index idx) |
c62b1a3b | 584 | { |
7a159cc9 | 585 | long val = 0; |
c62b1a3b | 586 | int cpu; |
c62b1a3b | 587 | |
711d3d2c KH |
588 | get_online_cpus(); |
589 | for_each_online_cpu(cpu) | |
c0ff4b85 | 590 | val += per_cpu(memcg->stat->count[idx], cpu); |
711d3d2c | 591 | #ifdef CONFIG_HOTPLUG_CPU |
c0ff4b85 R |
592 | spin_lock(&memcg->pcp_counter_lock); |
593 | val += memcg->nocpu_base.count[idx]; | |
594 | spin_unlock(&memcg->pcp_counter_lock); | |
711d3d2c KH |
595 | #endif |
596 | put_online_cpus(); | |
c62b1a3b KH |
597 | return val; |
598 | } | |
599 | ||
c0ff4b85 | 600 | static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg, |
0c3e73e8 BS |
601 | bool charge) |
602 | { | |
603 | int val = (charge) ? 1 : -1; | |
c0ff4b85 | 604 | this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAPOUT], val); |
0c3e73e8 BS |
605 | } |
606 | ||
c0ff4b85 | 607 | void mem_cgroup_pgfault(struct mem_cgroup *memcg, int val) |
456f998e | 608 | { |
c0ff4b85 | 609 | this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT], val); |
456f998e YH |
610 | } |
611 | ||
c0ff4b85 | 612 | void mem_cgroup_pgmajfault(struct mem_cgroup *memcg, int val) |
456f998e | 613 | { |
c0ff4b85 | 614 | this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT], val); |
456f998e YH |
615 | } |
616 | ||
c0ff4b85 | 617 | static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg, |
e9f8974f JW |
618 | enum mem_cgroup_events_index idx) |
619 | { | |
620 | unsigned long val = 0; | |
621 | int cpu; | |
622 | ||
623 | for_each_online_cpu(cpu) | |
c0ff4b85 | 624 | val += per_cpu(memcg->stat->events[idx], cpu); |
e9f8974f | 625 | #ifdef CONFIG_HOTPLUG_CPU |
c0ff4b85 R |
626 | spin_lock(&memcg->pcp_counter_lock); |
627 | val += memcg->nocpu_base.events[idx]; | |
628 | spin_unlock(&memcg->pcp_counter_lock); | |
e9f8974f JW |
629 | #endif |
630 | return val; | |
631 | } | |
632 | ||
c0ff4b85 | 633 | static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg, |
e401f176 | 634 | bool file, int nr_pages) |
d52aa412 | 635 | { |
c62b1a3b KH |
636 | preempt_disable(); |
637 | ||
e401f176 | 638 | if (file) |
c0ff4b85 R |
639 | __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE], |
640 | nr_pages); | |
d52aa412 | 641 | else |
c0ff4b85 R |
642 | __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS], |
643 | nr_pages); | |
55e462b0 | 644 | |
e401f176 KH |
645 | /* pagein of a big page is an event. So, ignore page size */ |
646 | if (nr_pages > 0) | |
c0ff4b85 | 647 | __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]); |
3751d604 | 648 | else { |
c0ff4b85 | 649 | __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]); |
3751d604 KH |
650 | nr_pages = -nr_pages; /* for event */ |
651 | } | |
e401f176 | 652 | |
c0ff4b85 | 653 | __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT], nr_pages); |
2e72b634 | 654 | |
c62b1a3b | 655 | preempt_enable(); |
6d12e2d8 KH |
656 | } |
657 | ||
bb2a0de9 | 658 | unsigned long |
c0ff4b85 | 659 | mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid, |
bb2a0de9 | 660 | unsigned int lru_mask) |
889976db YH |
661 | { |
662 | struct mem_cgroup_per_zone *mz; | |
bb2a0de9 KH |
663 | enum lru_list l; |
664 | unsigned long ret = 0; | |
665 | ||
c0ff4b85 | 666 | mz = mem_cgroup_zoneinfo(memcg, nid, zid); |
bb2a0de9 KH |
667 | |
668 | for_each_lru(l) { | |
669 | if (BIT(l) & lru_mask) | |
670 | ret += MEM_CGROUP_ZSTAT(mz, l); | |
671 | } | |
672 | return ret; | |
673 | } | |
674 | ||
675 | static unsigned long | |
c0ff4b85 | 676 | mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg, |
bb2a0de9 KH |
677 | int nid, unsigned int lru_mask) |
678 | { | |
889976db YH |
679 | u64 total = 0; |
680 | int zid; | |
681 | ||
bb2a0de9 | 682 | for (zid = 0; zid < MAX_NR_ZONES; zid++) |
c0ff4b85 R |
683 | total += mem_cgroup_zone_nr_lru_pages(memcg, |
684 | nid, zid, lru_mask); | |
bb2a0de9 | 685 | |
889976db YH |
686 | return total; |
687 | } | |
bb2a0de9 | 688 | |
c0ff4b85 | 689 | static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg, |
bb2a0de9 | 690 | unsigned int lru_mask) |
6d12e2d8 | 691 | { |
889976db | 692 | int nid; |
6d12e2d8 KH |
693 | u64 total = 0; |
694 | ||
bb2a0de9 | 695 | for_each_node_state(nid, N_HIGH_MEMORY) |
c0ff4b85 | 696 | total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask); |
6d12e2d8 | 697 | return total; |
d52aa412 KH |
698 | } |
699 | ||
c0ff4b85 | 700 | static bool __memcg_event_check(struct mem_cgroup *memcg, int target) |
7a159cc9 JW |
701 | { |
702 | unsigned long val, next; | |
703 | ||
4799401f SR |
704 | val = __this_cpu_read(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT]); |
705 | next = __this_cpu_read(memcg->stat->targets[target]); | |
7a159cc9 JW |
706 | /* from time_after() in jiffies.h */ |
707 | return ((long)next - (long)val < 0); | |
708 | } | |
709 | ||
c0ff4b85 | 710 | static void __mem_cgroup_target_update(struct mem_cgroup *memcg, int target) |
d2265e6f | 711 | { |
7a159cc9 | 712 | unsigned long val, next; |
d2265e6f | 713 | |
4799401f | 714 | val = __this_cpu_read(memcg->stat->events[MEM_CGROUP_EVENTS_COUNT]); |
d2265e6f | 715 | |
7a159cc9 JW |
716 | switch (target) { |
717 | case MEM_CGROUP_TARGET_THRESH: | |
718 | next = val + THRESHOLDS_EVENTS_TARGET; | |
719 | break; | |
720 | case MEM_CGROUP_TARGET_SOFTLIMIT: | |
721 | next = val + SOFTLIMIT_EVENTS_TARGET; | |
722 | break; | |
453a9bf3 KH |
723 | case MEM_CGROUP_TARGET_NUMAINFO: |
724 | next = val + NUMAINFO_EVENTS_TARGET; | |
725 | break; | |
7a159cc9 JW |
726 | default: |
727 | return; | |
728 | } | |
729 | ||
4799401f | 730 | __this_cpu_write(memcg->stat->targets[target], next); |
d2265e6f KH |
731 | } |
732 | ||
733 | /* | |
734 | * Check events in order. | |
735 | * | |
736 | */ | |
c0ff4b85 | 737 | static void memcg_check_events(struct mem_cgroup *memcg, struct page *page) |
d2265e6f | 738 | { |
4799401f | 739 | preempt_disable(); |
d2265e6f | 740 | /* threshold event is triggered in finer grain than soft limit */ |
c0ff4b85 R |
741 | if (unlikely(__memcg_event_check(memcg, MEM_CGROUP_TARGET_THRESH))) { |
742 | mem_cgroup_threshold(memcg); | |
743 | __mem_cgroup_target_update(memcg, MEM_CGROUP_TARGET_THRESH); | |
744 | if (unlikely(__memcg_event_check(memcg, | |
453a9bf3 | 745 | MEM_CGROUP_TARGET_SOFTLIMIT))) { |
c0ff4b85 R |
746 | mem_cgroup_update_tree(memcg, page); |
747 | __mem_cgroup_target_update(memcg, | |
453a9bf3 KH |
748 | MEM_CGROUP_TARGET_SOFTLIMIT); |
749 | } | |
750 | #if MAX_NUMNODES > 1 | |
c0ff4b85 | 751 | if (unlikely(__memcg_event_check(memcg, |
453a9bf3 | 752 | MEM_CGROUP_TARGET_NUMAINFO))) { |
c0ff4b85 R |
753 | atomic_inc(&memcg->numainfo_events); |
754 | __mem_cgroup_target_update(memcg, | |
453a9bf3 | 755 | MEM_CGROUP_TARGET_NUMAINFO); |
7a159cc9 | 756 | } |
453a9bf3 | 757 | #endif |
d2265e6f | 758 | } |
4799401f | 759 | preempt_enable(); |
d2265e6f KH |
760 | } |
761 | ||
d5b69e38 | 762 | static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
8cdea7c0 BS |
763 | { |
764 | return container_of(cgroup_subsys_state(cont, | |
765 | mem_cgroup_subsys_id), struct mem_cgroup, | |
766 | css); | |
767 | } | |
768 | ||
cf475ad2 | 769 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
78fb7466 | 770 | { |
31a78f23 BS |
771 | /* |
772 | * mm_update_next_owner() may clear mm->owner to NULL | |
773 | * if it races with swapoff, page migration, etc. | |
774 | * So this can be called with p == NULL. | |
775 | */ | |
776 | if (unlikely(!p)) | |
777 | return NULL; | |
778 | ||
78fb7466 PE |
779 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
780 | struct mem_cgroup, css); | |
781 | } | |
782 | ||
a433658c | 783 | struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) |
54595fe2 | 784 | { |
c0ff4b85 | 785 | struct mem_cgroup *memcg = NULL; |
0b7f569e KH |
786 | |
787 | if (!mm) | |
788 | return NULL; | |
54595fe2 KH |
789 | /* |
790 | * Because we have no locks, mm->owner's may be being moved to other | |
791 | * cgroup. We use css_tryget() here even if this looks | |
792 | * pessimistic (rather than adding locks here). | |
793 | */ | |
794 | rcu_read_lock(); | |
795 | do { | |
c0ff4b85 R |
796 | memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
797 | if (unlikely(!memcg)) | |
54595fe2 | 798 | break; |
c0ff4b85 | 799 | } while (!css_tryget(&memcg->css)); |
54595fe2 | 800 | rcu_read_unlock(); |
c0ff4b85 | 801 | return memcg; |
54595fe2 KH |
802 | } |
803 | ||
7d74b06f | 804 | /* The caller has to guarantee "mem" exists before calling this */ |
c0ff4b85 | 805 | static struct mem_cgroup *mem_cgroup_start_loop(struct mem_cgroup *memcg) |
14067bb3 | 806 | { |
711d3d2c KH |
807 | struct cgroup_subsys_state *css; |
808 | int found; | |
809 | ||
c0ff4b85 | 810 | if (!memcg) /* ROOT cgroup has the smallest ID */ |
711d3d2c | 811 | return root_mem_cgroup; /*css_put/get against root is ignored*/ |
c0ff4b85 R |
812 | if (!memcg->use_hierarchy) { |
813 | if (css_tryget(&memcg->css)) | |
814 | return memcg; | |
711d3d2c KH |
815 | return NULL; |
816 | } | |
817 | rcu_read_lock(); | |
818 | /* | |
819 | * searching a memory cgroup which has the smallest ID under given | |
820 | * ROOT cgroup. (ID >= 1) | |
821 | */ | |
c0ff4b85 | 822 | css = css_get_next(&mem_cgroup_subsys, 1, &memcg->css, &found); |
711d3d2c | 823 | if (css && css_tryget(css)) |
c0ff4b85 | 824 | memcg = container_of(css, struct mem_cgroup, css); |
711d3d2c | 825 | else |
c0ff4b85 | 826 | memcg = NULL; |
711d3d2c | 827 | rcu_read_unlock(); |
c0ff4b85 | 828 | return memcg; |
7d74b06f KH |
829 | } |
830 | ||
831 | static struct mem_cgroup *mem_cgroup_get_next(struct mem_cgroup *iter, | |
832 | struct mem_cgroup *root, | |
833 | bool cond) | |
834 | { | |
835 | int nextid = css_id(&iter->css) + 1; | |
836 | int found; | |
837 | int hierarchy_used; | |
14067bb3 | 838 | struct cgroup_subsys_state *css; |
14067bb3 | 839 | |
7d74b06f | 840 | hierarchy_used = iter->use_hierarchy; |
14067bb3 | 841 | |
7d74b06f | 842 | css_put(&iter->css); |
711d3d2c KH |
843 | /* If no ROOT, walk all, ignore hierarchy */ |
844 | if (!cond || (root && !hierarchy_used)) | |
7d74b06f | 845 | return NULL; |
14067bb3 | 846 | |
711d3d2c KH |
847 | if (!root) |
848 | root = root_mem_cgroup; | |
849 | ||
7d74b06f KH |
850 | do { |
851 | iter = NULL; | |
14067bb3 | 852 | rcu_read_lock(); |
7d74b06f KH |
853 | |
854 | css = css_get_next(&mem_cgroup_subsys, nextid, | |
855 | &root->css, &found); | |
14067bb3 | 856 | if (css && css_tryget(css)) |
7d74b06f | 857 | iter = container_of(css, struct mem_cgroup, css); |
14067bb3 | 858 | rcu_read_unlock(); |
7d74b06f | 859 | /* If css is NULL, no more cgroups will be found */ |
14067bb3 | 860 | nextid = found + 1; |
7d74b06f | 861 | } while (css && !iter); |
14067bb3 | 862 | |
7d74b06f | 863 | return iter; |
14067bb3 | 864 | } |
7d74b06f KH |
865 | /* |
866 | * for_eacn_mem_cgroup_tree() for visiting all cgroup under tree. Please | |
867 | * be careful that "break" loop is not allowed. We have reference count. | |
868 | * Instead of that modify "cond" to be false and "continue" to exit the loop. | |
869 | */ | |
870 | #define for_each_mem_cgroup_tree_cond(iter, root, cond) \ | |
871 | for (iter = mem_cgroup_start_loop(root);\ | |
872 | iter != NULL;\ | |
873 | iter = mem_cgroup_get_next(iter, root, cond)) | |
874 | ||
875 | #define for_each_mem_cgroup_tree(iter, root) \ | |
876 | for_each_mem_cgroup_tree_cond(iter, root, true) | |
877 | ||
711d3d2c KH |
878 | #define for_each_mem_cgroup_all(iter) \ |
879 | for_each_mem_cgroup_tree_cond(iter, NULL, true) | |
880 | ||
14067bb3 | 881 | |
c0ff4b85 | 882 | static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg) |
4b3bde4c | 883 | { |
c0ff4b85 | 884 | return (memcg == root_mem_cgroup); |
4b3bde4c BS |
885 | } |
886 | ||
456f998e YH |
887 | void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx) |
888 | { | |
c0ff4b85 | 889 | struct mem_cgroup *memcg; |
456f998e YH |
890 | |
891 | if (!mm) | |
892 | return; | |
893 | ||
894 | rcu_read_lock(); | |
c0ff4b85 R |
895 | memcg = mem_cgroup_from_task(rcu_dereference(mm->owner)); |
896 | if (unlikely(!memcg)) | |
456f998e YH |
897 | goto out; |
898 | ||
899 | switch (idx) { | |
900 | case PGMAJFAULT: | |
c0ff4b85 | 901 | mem_cgroup_pgmajfault(memcg, 1); |
456f998e YH |
902 | break; |
903 | case PGFAULT: | |
c0ff4b85 | 904 | mem_cgroup_pgfault(memcg, 1); |
456f998e YH |
905 | break; |
906 | default: | |
907 | BUG(); | |
908 | } | |
909 | out: | |
910 | rcu_read_unlock(); | |
911 | } | |
912 | EXPORT_SYMBOL(mem_cgroup_count_vm_event); | |
913 | ||
08e552c6 KH |
914 | /* |
915 | * Following LRU functions are allowed to be used without PCG_LOCK. | |
916 | * Operations are called by routine of global LRU independently from memcg. | |
917 | * What we have to take care of here is validness of pc->mem_cgroup. | |
918 | * | |
919 | * Changes to pc->mem_cgroup happens when | |
920 | * 1. charge | |
921 | * 2. moving account | |
922 | * In typical case, "charge" is done before add-to-lru. Exception is SwapCache. | |
923 | * It is added to LRU before charge. | |
924 | * If PCG_USED bit is not set, page_cgroup is not added to this private LRU. | |
925 | * When moving account, the page is not on LRU. It's isolated. | |
926 | */ | |
4f98a2fe | 927 | |
08e552c6 KH |
928 | void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru) |
929 | { | |
930 | struct page_cgroup *pc; | |
08e552c6 | 931 | struct mem_cgroup_per_zone *mz; |
6d12e2d8 | 932 | |
f8d66542 | 933 | if (mem_cgroup_disabled()) |
08e552c6 KH |
934 | return; |
935 | pc = lookup_page_cgroup(page); | |
936 | /* can happen while we handle swapcache. */ | |
4b3bde4c | 937 | if (!TestClearPageCgroupAcctLRU(pc)) |
08e552c6 | 938 | return; |
4b3bde4c | 939 | VM_BUG_ON(!pc->mem_cgroup); |
544122e5 KH |
940 | /* |
941 | * We don't check PCG_USED bit. It's cleared when the "page" is finally | |
942 | * removed from global LRU. | |
943 | */ | |
97a6c37b | 944 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
ece35ca8 KH |
945 | /* huge page split is done under lru_lock. so, we have no races. */ |
946 | MEM_CGROUP_ZSTAT(mz, lru) -= 1 << compound_order(page); | |
4b3bde4c BS |
947 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
948 | return; | |
949 | VM_BUG_ON(list_empty(&pc->lru)); | |
08e552c6 | 950 | list_del_init(&pc->lru); |
6d12e2d8 KH |
951 | } |
952 | ||
08e552c6 | 953 | void mem_cgroup_del_lru(struct page *page) |
6d12e2d8 | 954 | { |
08e552c6 KH |
955 | mem_cgroup_del_lru_list(page, page_lru(page)); |
956 | } | |
b69408e8 | 957 | |
3f58a829 MK |
958 | /* |
959 | * Writeback is about to end against a page which has been marked for immediate | |
960 | * reclaim. If it still appears to be reclaimable, move it to the tail of the | |
961 | * inactive list. | |
962 | */ | |
963 | void mem_cgroup_rotate_reclaimable_page(struct page *page) | |
964 | { | |
965 | struct mem_cgroup_per_zone *mz; | |
966 | struct page_cgroup *pc; | |
967 | enum lru_list lru = page_lru(page); | |
968 | ||
969 | if (mem_cgroup_disabled()) | |
970 | return; | |
971 | ||
972 | pc = lookup_page_cgroup(page); | |
973 | /* unused or root page is not rotated. */ | |
974 | if (!PageCgroupUsed(pc)) | |
975 | return; | |
976 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ | |
977 | smp_rmb(); | |
978 | if (mem_cgroup_is_root(pc->mem_cgroup)) | |
979 | return; | |
97a6c37b | 980 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
3f58a829 MK |
981 | list_move_tail(&pc->lru, &mz->lists[lru]); |
982 | } | |
983 | ||
08e552c6 KH |
984 | void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru) |
985 | { | |
986 | struct mem_cgroup_per_zone *mz; | |
987 | struct page_cgroup *pc; | |
b69408e8 | 988 | |
f8d66542 | 989 | if (mem_cgroup_disabled()) |
08e552c6 | 990 | return; |
6d12e2d8 | 991 | |
08e552c6 | 992 | pc = lookup_page_cgroup(page); |
4b3bde4c | 993 | /* unused or root page is not rotated. */ |
713735b4 JW |
994 | if (!PageCgroupUsed(pc)) |
995 | return; | |
996 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ | |
997 | smp_rmb(); | |
998 | if (mem_cgroup_is_root(pc->mem_cgroup)) | |
08e552c6 | 999 | return; |
97a6c37b | 1000 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
08e552c6 | 1001 | list_move(&pc->lru, &mz->lists[lru]); |
6d12e2d8 KH |
1002 | } |
1003 | ||
08e552c6 | 1004 | void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru) |
66e1707b | 1005 | { |
08e552c6 KH |
1006 | struct page_cgroup *pc; |
1007 | struct mem_cgroup_per_zone *mz; | |
6d12e2d8 | 1008 | |
f8d66542 | 1009 | if (mem_cgroup_disabled()) |
08e552c6 KH |
1010 | return; |
1011 | pc = lookup_page_cgroup(page); | |
4b3bde4c | 1012 | VM_BUG_ON(PageCgroupAcctLRU(pc)); |
a61ed3ce JW |
1013 | /* |
1014 | * putback: charge: | |
1015 | * SetPageLRU SetPageCgroupUsed | |
1016 | * smp_mb smp_mb | |
1017 | * PageCgroupUsed && add to memcg LRU PageLRU && add to memcg LRU | |
1018 | * | |
1019 | * Ensure that one of the two sides adds the page to the memcg | |
1020 | * LRU during a race. | |
1021 | */ | |
1022 | smp_mb(); | |
08e552c6 | 1023 | if (!PageCgroupUsed(pc)) |
894bc310 | 1024 | return; |
713735b4 JW |
1025 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
1026 | smp_rmb(); | |
97a6c37b | 1027 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
ece35ca8 KH |
1028 | /* huge page split is done under lru_lock. so, we have no races. */ |
1029 | MEM_CGROUP_ZSTAT(mz, lru) += 1 << compound_order(page); | |
4b3bde4c BS |
1030 | SetPageCgroupAcctLRU(pc); |
1031 | if (mem_cgroup_is_root(pc->mem_cgroup)) | |
1032 | return; | |
08e552c6 KH |
1033 | list_add(&pc->lru, &mz->lists[lru]); |
1034 | } | |
544122e5 | 1035 | |
08e552c6 | 1036 | /* |
5a6475a4 KH |
1037 | * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed |
1038 | * while it's linked to lru because the page may be reused after it's fully | |
1039 | * uncharged. To handle that, unlink page_cgroup from LRU when charge it again. | |
1040 | * It's done under lock_page and expected that zone->lru_lock isnever held. | |
08e552c6 | 1041 | */ |
5a6475a4 | 1042 | static void mem_cgroup_lru_del_before_commit(struct page *page) |
08e552c6 | 1043 | { |
544122e5 KH |
1044 | unsigned long flags; |
1045 | struct zone *zone = page_zone(page); | |
1046 | struct page_cgroup *pc = lookup_page_cgroup(page); | |
1047 | ||
5a6475a4 KH |
1048 | /* |
1049 | * Doing this check without taking ->lru_lock seems wrong but this | |
1050 | * is safe. Because if page_cgroup's USED bit is unset, the page | |
1051 | * will not be added to any memcg's LRU. If page_cgroup's USED bit is | |
1052 | * set, the commit after this will fail, anyway. | |
1053 | * This all charge/uncharge is done under some mutual execustion. | |
1054 | * So, we don't need to taking care of changes in USED bit. | |
1055 | */ | |
1056 | if (likely(!PageLRU(page))) | |
1057 | return; | |
1058 | ||
544122e5 KH |
1059 | spin_lock_irqsave(&zone->lru_lock, flags); |
1060 | /* | |
1061 | * Forget old LRU when this page_cgroup is *not* used. This Used bit | |
1062 | * is guarded by lock_page() because the page is SwapCache. | |
1063 | */ | |
1064 | if (!PageCgroupUsed(pc)) | |
1065 | mem_cgroup_del_lru_list(page, page_lru(page)); | |
1066 | spin_unlock_irqrestore(&zone->lru_lock, flags); | |
08e552c6 KH |
1067 | } |
1068 | ||
5a6475a4 | 1069 | static void mem_cgroup_lru_add_after_commit(struct page *page) |
544122e5 KH |
1070 | { |
1071 | unsigned long flags; | |
1072 | struct zone *zone = page_zone(page); | |
1073 | struct page_cgroup *pc = lookup_page_cgroup(page); | |
a61ed3ce JW |
1074 | /* |
1075 | * putback: charge: | |
1076 | * SetPageLRU SetPageCgroupUsed | |
1077 | * smp_mb smp_mb | |
1078 | * PageCgroupUsed && add to memcg LRU PageLRU && add to memcg LRU | |
1079 | * | |
1080 | * Ensure that one of the two sides adds the page to the memcg | |
1081 | * LRU during a race. | |
1082 | */ | |
1083 | smp_mb(); | |
5a6475a4 KH |
1084 | /* taking care of that the page is added to LRU while we commit it */ |
1085 | if (likely(!PageLRU(page))) | |
1086 | return; | |
544122e5 KH |
1087 | spin_lock_irqsave(&zone->lru_lock, flags); |
1088 | /* link when the page is linked to LRU but page_cgroup isn't */ | |
4b3bde4c | 1089 | if (PageLRU(page) && !PageCgroupAcctLRU(pc)) |
544122e5 KH |
1090 | mem_cgroup_add_lru_list(page, page_lru(page)); |
1091 | spin_unlock_irqrestore(&zone->lru_lock, flags); | |
1092 | } | |
1093 | ||
1094 | ||
08e552c6 KH |
1095 | void mem_cgroup_move_lists(struct page *page, |
1096 | enum lru_list from, enum lru_list to) | |
1097 | { | |
f8d66542 | 1098 | if (mem_cgroup_disabled()) |
08e552c6 KH |
1099 | return; |
1100 | mem_cgroup_del_lru_list(page, from); | |
1101 | mem_cgroup_add_lru_list(page, to); | |
66e1707b BS |
1102 | } |
1103 | ||
3e92041d | 1104 | /* |
c0ff4b85 | 1105 | * Checks whether given mem is same or in the root_mem_cgroup's |
3e92041d MH |
1106 | * hierarchy subtree |
1107 | */ | |
c0ff4b85 R |
1108 | static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg, |
1109 | struct mem_cgroup *memcg) | |
3e92041d | 1110 | { |
c0ff4b85 R |
1111 | if (root_memcg != memcg) { |
1112 | return (root_memcg->use_hierarchy && | |
1113 | css_is_ancestor(&memcg->css, &root_memcg->css)); | |
3e92041d MH |
1114 | } |
1115 | ||
1116 | return true; | |
1117 | } | |
1118 | ||
c0ff4b85 | 1119 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg) |
4c4a2214 DR |
1120 | { |
1121 | int ret; | |
0b7f569e | 1122 | struct mem_cgroup *curr = NULL; |
158e0a2d | 1123 | struct task_struct *p; |
4c4a2214 | 1124 | |
158e0a2d KH |
1125 | p = find_lock_task_mm(task); |
1126 | if (!p) | |
1127 | return 0; | |
1128 | curr = try_get_mem_cgroup_from_mm(p->mm); | |
1129 | task_unlock(p); | |
0b7f569e KH |
1130 | if (!curr) |
1131 | return 0; | |
d31f56db | 1132 | /* |
c0ff4b85 | 1133 | * We should check use_hierarchy of "memcg" not "curr". Because checking |
d31f56db | 1134 | * use_hierarchy of "curr" here make this function true if hierarchy is |
c0ff4b85 R |
1135 | * enabled in "curr" and "curr" is a child of "memcg" in *cgroup* |
1136 | * hierarchy(even if use_hierarchy is disabled in "memcg"). | |
d31f56db | 1137 | */ |
c0ff4b85 | 1138 | ret = mem_cgroup_same_or_subtree(memcg, curr); |
0b7f569e | 1139 | css_put(&curr->css); |
4c4a2214 DR |
1140 | return ret; |
1141 | } | |
1142 | ||
9b272977 | 1143 | int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg, struct zone *zone) |
14797e23 | 1144 | { |
9b272977 JW |
1145 | unsigned long inactive_ratio; |
1146 | int nid = zone_to_nid(zone); | |
1147 | int zid = zone_idx(zone); | |
14797e23 | 1148 | unsigned long inactive; |
9b272977 | 1149 | unsigned long active; |
c772be93 | 1150 | unsigned long gb; |
14797e23 | 1151 | |
9b272977 JW |
1152 | inactive = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid, |
1153 | BIT(LRU_INACTIVE_ANON)); | |
1154 | active = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid, | |
1155 | BIT(LRU_ACTIVE_ANON)); | |
14797e23 | 1156 | |
c772be93 KM |
1157 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
1158 | if (gb) | |
1159 | inactive_ratio = int_sqrt(10 * gb); | |
1160 | else | |
1161 | inactive_ratio = 1; | |
1162 | ||
9b272977 | 1163 | return inactive * inactive_ratio < active; |
14797e23 KM |
1164 | } |
1165 | ||
9b272977 | 1166 | int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg, struct zone *zone) |
56e49d21 RR |
1167 | { |
1168 | unsigned long active; | |
1169 | unsigned long inactive; | |
9b272977 JW |
1170 | int zid = zone_idx(zone); |
1171 | int nid = zone_to_nid(zone); | |
56e49d21 | 1172 | |
9b272977 JW |
1173 | inactive = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid, |
1174 | BIT(LRU_INACTIVE_FILE)); | |
1175 | active = mem_cgroup_zone_nr_lru_pages(memcg, nid, zid, | |
1176 | BIT(LRU_ACTIVE_FILE)); | |
56e49d21 RR |
1177 | |
1178 | return (active > inactive); | |
1179 | } | |
1180 | ||
3e2f41f1 KM |
1181 | struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg, |
1182 | struct zone *zone) | |
1183 | { | |
13d7e3a2 | 1184 | int nid = zone_to_nid(zone); |
3e2f41f1 KM |
1185 | int zid = zone_idx(zone); |
1186 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); | |
1187 | ||
1188 | return &mz->reclaim_stat; | |
1189 | } | |
1190 | ||
1191 | struct zone_reclaim_stat * | |
1192 | mem_cgroup_get_reclaim_stat_from_page(struct page *page) | |
1193 | { | |
1194 | struct page_cgroup *pc; | |
1195 | struct mem_cgroup_per_zone *mz; | |
1196 | ||
1197 | if (mem_cgroup_disabled()) | |
1198 | return NULL; | |
1199 | ||
1200 | pc = lookup_page_cgroup(page); | |
bd112db8 DN |
1201 | if (!PageCgroupUsed(pc)) |
1202 | return NULL; | |
713735b4 JW |
1203 | /* Ensure pc->mem_cgroup is visible after reading PCG_USED. */ |
1204 | smp_rmb(); | |
97a6c37b | 1205 | mz = page_cgroup_zoneinfo(pc->mem_cgroup, page); |
3e2f41f1 KM |
1206 | return &mz->reclaim_stat; |
1207 | } | |
1208 | ||
66e1707b BS |
1209 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
1210 | struct list_head *dst, | |
1211 | unsigned long *scanned, int order, | |
4356f21d MK |
1212 | isolate_mode_t mode, |
1213 | struct zone *z, | |
66e1707b | 1214 | struct mem_cgroup *mem_cont, |
4f98a2fe | 1215 | int active, int file) |
66e1707b BS |
1216 | { |
1217 | unsigned long nr_taken = 0; | |
1218 | struct page *page; | |
1219 | unsigned long scan; | |
1220 | LIST_HEAD(pc_list); | |
1221 | struct list_head *src; | |
ff7283fa | 1222 | struct page_cgroup *pc, *tmp; |
13d7e3a2 | 1223 | int nid = zone_to_nid(z); |
1ecaab2b KH |
1224 | int zid = zone_idx(z); |
1225 | struct mem_cgroup_per_zone *mz; | |
b7c46d15 | 1226 | int lru = LRU_FILE * file + active; |
2ffebca6 | 1227 | int ret; |
66e1707b | 1228 | |
cf475ad2 | 1229 | BUG_ON(!mem_cont); |
1ecaab2b | 1230 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
b69408e8 | 1231 | src = &mz->lists[lru]; |
66e1707b | 1232 | |
ff7283fa KH |
1233 | scan = 0; |
1234 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { | |
436c6541 | 1235 | if (scan >= nr_to_scan) |
ff7283fa | 1236 | break; |
08e552c6 | 1237 | |
52d4b9ac KH |
1238 | if (unlikely(!PageCgroupUsed(pc))) |
1239 | continue; | |
5564e88b | 1240 | |
6b3ae58e | 1241 | page = lookup_cgroup_page(pc); |
5564e88b | 1242 | |
436c6541 | 1243 | if (unlikely(!PageLRU(page))) |
ff7283fa | 1244 | continue; |
ff7283fa | 1245 | |
436c6541 | 1246 | scan++; |
2ffebca6 KH |
1247 | ret = __isolate_lru_page(page, mode, file); |
1248 | switch (ret) { | |
1249 | case 0: | |
66e1707b | 1250 | list_move(&page->lru, dst); |
2ffebca6 | 1251 | mem_cgroup_del_lru(page); |
2c888cfb | 1252 | nr_taken += hpage_nr_pages(page); |
2ffebca6 KH |
1253 | break; |
1254 | case -EBUSY: | |
1255 | /* we don't affect global LRU but rotate in our LRU */ | |
1256 | mem_cgroup_rotate_lru_list(page, page_lru(page)); | |
1257 | break; | |
1258 | default: | |
1259 | break; | |
66e1707b BS |
1260 | } |
1261 | } | |
1262 | ||
66e1707b | 1263 | *scanned = scan; |
cc8e970c KM |
1264 | |
1265 | trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken, | |
1266 | 0, 0, 0, mode); | |
1267 | ||
66e1707b BS |
1268 | return nr_taken; |
1269 | } | |
1270 | ||
6d61ef40 BS |
1271 | #define mem_cgroup_from_res_counter(counter, member) \ |
1272 | container_of(counter, struct mem_cgroup, member) | |
1273 | ||
19942822 | 1274 | /** |
9d11ea9f JW |
1275 | * mem_cgroup_margin - calculate chargeable space of a memory cgroup |
1276 | * @mem: the memory cgroup | |
19942822 | 1277 | * |
9d11ea9f | 1278 | * Returns the maximum amount of memory @mem can be charged with, in |
7ec99d62 | 1279 | * pages. |
19942822 | 1280 | */ |
c0ff4b85 | 1281 | static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg) |
19942822 | 1282 | { |
9d11ea9f JW |
1283 | unsigned long long margin; |
1284 | ||
c0ff4b85 | 1285 | margin = res_counter_margin(&memcg->res); |
9d11ea9f | 1286 | if (do_swap_account) |
c0ff4b85 | 1287 | margin = min(margin, res_counter_margin(&memcg->memsw)); |
7ec99d62 | 1288 | return margin >> PAGE_SHIFT; |
19942822 JW |
1289 | } |
1290 | ||
1f4c025b | 1291 | int mem_cgroup_swappiness(struct mem_cgroup *memcg) |
a7885eb8 KM |
1292 | { |
1293 | struct cgroup *cgrp = memcg->css.cgroup; | |
a7885eb8 KM |
1294 | |
1295 | /* root ? */ | |
1296 | if (cgrp->parent == NULL) | |
1297 | return vm_swappiness; | |
1298 | ||
bf1ff263 | 1299 | return memcg->swappiness; |
a7885eb8 KM |
1300 | } |
1301 | ||
c0ff4b85 | 1302 | static void mem_cgroup_start_move(struct mem_cgroup *memcg) |
32047e2a KH |
1303 | { |
1304 | int cpu; | |
1489ebad KH |
1305 | |
1306 | get_online_cpus(); | |
c0ff4b85 | 1307 | spin_lock(&memcg->pcp_counter_lock); |
1489ebad | 1308 | for_each_online_cpu(cpu) |
c0ff4b85 R |
1309 | per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1; |
1310 | memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] += 1; | |
1311 | spin_unlock(&memcg->pcp_counter_lock); | |
1489ebad | 1312 | put_online_cpus(); |
32047e2a KH |
1313 | |
1314 | synchronize_rcu(); | |
1315 | } | |
1316 | ||
c0ff4b85 | 1317 | static void mem_cgroup_end_move(struct mem_cgroup *memcg) |
32047e2a KH |
1318 | { |
1319 | int cpu; | |
1320 | ||
c0ff4b85 | 1321 | if (!memcg) |
32047e2a | 1322 | return; |
1489ebad | 1323 | get_online_cpus(); |
c0ff4b85 | 1324 | spin_lock(&memcg->pcp_counter_lock); |
1489ebad | 1325 | for_each_online_cpu(cpu) |
c0ff4b85 R |
1326 | per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1; |
1327 | memcg->nocpu_base.count[MEM_CGROUP_ON_MOVE] -= 1; | |
1328 | spin_unlock(&memcg->pcp_counter_lock); | |
1489ebad | 1329 | put_online_cpus(); |
32047e2a KH |
1330 | } |
1331 | /* | |
1332 | * 2 routines for checking "mem" is under move_account() or not. | |
1333 | * | |
1334 | * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used | |
1335 | * for avoiding race in accounting. If true, | |
1336 | * pc->mem_cgroup may be overwritten. | |
1337 | * | |
1338 | * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or | |
1339 | * under hierarchy of moving cgroups. This is for | |
1340 | * waiting at hith-memory prressure caused by "move". | |
1341 | */ | |
1342 | ||
c0ff4b85 | 1343 | static bool mem_cgroup_stealed(struct mem_cgroup *memcg) |
32047e2a KH |
1344 | { |
1345 | VM_BUG_ON(!rcu_read_lock_held()); | |
c0ff4b85 | 1346 | return this_cpu_read(memcg->stat->count[MEM_CGROUP_ON_MOVE]) > 0; |
32047e2a | 1347 | } |
4b534334 | 1348 | |
c0ff4b85 | 1349 | static bool mem_cgroup_under_move(struct mem_cgroup *memcg) |
4b534334 | 1350 | { |
2bd9bb20 KH |
1351 | struct mem_cgroup *from; |
1352 | struct mem_cgroup *to; | |
4b534334 | 1353 | bool ret = false; |
2bd9bb20 KH |
1354 | /* |
1355 | * Unlike task_move routines, we access mc.to, mc.from not under | |
1356 | * mutual exclusion by cgroup_mutex. Here, we take spinlock instead. | |
1357 | */ | |
1358 | spin_lock(&mc.lock); | |
1359 | from = mc.from; | |
1360 | to = mc.to; | |
1361 | if (!from) | |
1362 | goto unlock; | |
3e92041d | 1363 | |
c0ff4b85 R |
1364 | ret = mem_cgroup_same_or_subtree(memcg, from) |
1365 | || mem_cgroup_same_or_subtree(memcg, to); | |
2bd9bb20 KH |
1366 | unlock: |
1367 | spin_unlock(&mc.lock); | |
4b534334 KH |
1368 | return ret; |
1369 | } | |
1370 | ||
c0ff4b85 | 1371 | static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg) |
4b534334 KH |
1372 | { |
1373 | if (mc.moving_task && current != mc.moving_task) { | |
c0ff4b85 | 1374 | if (mem_cgroup_under_move(memcg)) { |
4b534334 KH |
1375 | DEFINE_WAIT(wait); |
1376 | prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE); | |
1377 | /* moving charge context might have finished. */ | |
1378 | if (mc.moving_task) | |
1379 | schedule(); | |
1380 | finish_wait(&mc.waitq, &wait); | |
1381 | return true; | |
1382 | } | |
1383 | } | |
1384 | return false; | |
1385 | } | |
1386 | ||
e222432b | 1387 | /** |
6a6135b6 | 1388 | * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode. |
e222432b BS |
1389 | * @memcg: The memory cgroup that went over limit |
1390 | * @p: Task that is going to be killed | |
1391 | * | |
1392 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is | |
1393 | * enabled | |
1394 | */ | |
1395 | void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) | |
1396 | { | |
1397 | struct cgroup *task_cgrp; | |
1398 | struct cgroup *mem_cgrp; | |
1399 | /* | |
1400 | * Need a buffer in BSS, can't rely on allocations. The code relies | |
1401 | * on the assumption that OOM is serialized for memory controller. | |
1402 | * If this assumption is broken, revisit this code. | |
1403 | */ | |
1404 | static char memcg_name[PATH_MAX]; | |
1405 | int ret; | |
1406 | ||
d31f56db | 1407 | if (!memcg || !p) |
e222432b BS |
1408 | return; |
1409 | ||
1410 | ||
1411 | rcu_read_lock(); | |
1412 | ||
1413 | mem_cgrp = memcg->css.cgroup; | |
1414 | task_cgrp = task_cgroup(p, mem_cgroup_subsys_id); | |
1415 | ||
1416 | ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX); | |
1417 | if (ret < 0) { | |
1418 | /* | |
1419 | * Unfortunately, we are unable to convert to a useful name | |
1420 | * But we'll still print out the usage information | |
1421 | */ | |
1422 | rcu_read_unlock(); | |
1423 | goto done; | |
1424 | } | |
1425 | rcu_read_unlock(); | |
1426 | ||
1427 | printk(KERN_INFO "Task in %s killed", memcg_name); | |
1428 | ||
1429 | rcu_read_lock(); | |
1430 | ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX); | |
1431 | if (ret < 0) { | |
1432 | rcu_read_unlock(); | |
1433 | goto done; | |
1434 | } | |
1435 | rcu_read_unlock(); | |
1436 | ||
1437 | /* | |
1438 | * Continues from above, so we don't need an KERN_ level | |
1439 | */ | |
1440 | printk(KERN_CONT " as a result of limit of %s\n", memcg_name); | |
1441 | done: | |
1442 | ||
1443 | printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n", | |
1444 | res_counter_read_u64(&memcg->res, RES_USAGE) >> 10, | |
1445 | res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10, | |
1446 | res_counter_read_u64(&memcg->res, RES_FAILCNT)); | |
1447 | printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, " | |
1448 | "failcnt %llu\n", | |
1449 | res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10, | |
1450 | res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10, | |
1451 | res_counter_read_u64(&memcg->memsw, RES_FAILCNT)); | |
1452 | } | |
1453 | ||
81d39c20 KH |
1454 | /* |
1455 | * This function returns the number of memcg under hierarchy tree. Returns | |
1456 | * 1(self count) if no children. | |
1457 | */ | |
c0ff4b85 | 1458 | static int mem_cgroup_count_children(struct mem_cgroup *memcg) |
81d39c20 KH |
1459 | { |
1460 | int num = 0; | |
7d74b06f KH |
1461 | struct mem_cgroup *iter; |
1462 | ||
c0ff4b85 | 1463 | for_each_mem_cgroup_tree(iter, memcg) |
7d74b06f | 1464 | num++; |
81d39c20 KH |
1465 | return num; |
1466 | } | |
1467 | ||
a63d83f4 DR |
1468 | /* |
1469 | * Return the memory (and swap, if configured) limit for a memcg. | |
1470 | */ | |
1471 | u64 mem_cgroup_get_limit(struct mem_cgroup *memcg) | |
1472 | { | |
1473 | u64 limit; | |
1474 | u64 memsw; | |
1475 | ||
f3e8eb70 JW |
1476 | limit = res_counter_read_u64(&memcg->res, RES_LIMIT); |
1477 | limit += total_swap_pages << PAGE_SHIFT; | |
1478 | ||
a63d83f4 DR |
1479 | memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
1480 | /* | |
1481 | * If memsw is finite and limits the amount of swap space available | |
1482 | * to this memcg, return that limit. | |
1483 | */ | |
1484 | return min(limit, memsw); | |
1485 | } | |
1486 | ||
6d61ef40 | 1487 | /* |
04046e1a KH |
1488 | * Visit the first child (need not be the first child as per the ordering |
1489 | * of the cgroup list, since we track last_scanned_child) of @mem and use | |
1490 | * that to reclaim free pages from. | |
1491 | */ | |
1492 | static struct mem_cgroup * | |
c0ff4b85 | 1493 | mem_cgroup_select_victim(struct mem_cgroup *root_memcg) |
04046e1a KH |
1494 | { |
1495 | struct mem_cgroup *ret = NULL; | |
1496 | struct cgroup_subsys_state *css; | |
1497 | int nextid, found; | |
1498 | ||
c0ff4b85 R |
1499 | if (!root_memcg->use_hierarchy) { |
1500 | css_get(&root_memcg->css); | |
1501 | ret = root_memcg; | |
04046e1a KH |
1502 | } |
1503 | ||
1504 | while (!ret) { | |
1505 | rcu_read_lock(); | |
c0ff4b85 R |
1506 | nextid = root_memcg->last_scanned_child + 1; |
1507 | css = css_get_next(&mem_cgroup_subsys, nextid, &root_memcg->css, | |
04046e1a KH |
1508 | &found); |
1509 | if (css && css_tryget(css)) | |
1510 | ret = container_of(css, struct mem_cgroup, css); | |
1511 | ||
1512 | rcu_read_unlock(); | |
1513 | /* Updates scanning parameter */ | |
04046e1a KH |
1514 | if (!css) { |
1515 | /* this means start scan from ID:1 */ | |
c0ff4b85 | 1516 | root_memcg->last_scanned_child = 0; |
04046e1a | 1517 | } else |
c0ff4b85 | 1518 | root_memcg->last_scanned_child = found; |
04046e1a KH |
1519 | } |
1520 | ||
1521 | return ret; | |
1522 | } | |
1523 | ||
4d0c066d KH |
1524 | /** |
1525 | * test_mem_cgroup_node_reclaimable | |
1526 | * @mem: the target memcg | |
1527 | * @nid: the node ID to be checked. | |
1528 | * @noswap : specify true here if the user wants flle only information. | |
1529 | * | |
1530 | * This function returns whether the specified memcg contains any | |
1531 | * reclaimable pages on a node. Returns true if there are any reclaimable | |
1532 | * pages in the node. | |
1533 | */ | |
c0ff4b85 | 1534 | static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg, |
4d0c066d KH |
1535 | int nid, bool noswap) |
1536 | { | |
c0ff4b85 | 1537 | if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE)) |
4d0c066d KH |
1538 | return true; |
1539 | if (noswap || !total_swap_pages) | |
1540 | return false; | |
c0ff4b85 | 1541 | if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON)) |
4d0c066d KH |
1542 | return true; |
1543 | return false; | |
1544 | ||
1545 | } | |
889976db YH |
1546 | #if MAX_NUMNODES > 1 |
1547 | ||
1548 | /* | |
1549 | * Always updating the nodemask is not very good - even if we have an empty | |
1550 | * list or the wrong list here, we can start from some node and traverse all | |
1551 | * nodes based on the zonelist. So update the list loosely once per 10 secs. | |
1552 | * | |
1553 | */ | |
c0ff4b85 | 1554 | static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg) |
889976db YH |
1555 | { |
1556 | int nid; | |
453a9bf3 KH |
1557 | /* |
1558 | * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET | |
1559 | * pagein/pageout changes since the last update. | |
1560 | */ | |
c0ff4b85 | 1561 | if (!atomic_read(&memcg->numainfo_events)) |
453a9bf3 | 1562 | return; |
c0ff4b85 | 1563 | if (atomic_inc_return(&memcg->numainfo_updating) > 1) |
889976db YH |
1564 | return; |
1565 | ||
889976db | 1566 | /* make a nodemask where this memcg uses memory from */ |
c0ff4b85 | 1567 | memcg->scan_nodes = node_states[N_HIGH_MEMORY]; |
889976db YH |
1568 | |
1569 | for_each_node_mask(nid, node_states[N_HIGH_MEMORY]) { | |
1570 | ||
c0ff4b85 R |
1571 | if (!test_mem_cgroup_node_reclaimable(memcg, nid, false)) |
1572 | node_clear(nid, memcg->scan_nodes); | |
889976db | 1573 | } |
453a9bf3 | 1574 | |
c0ff4b85 R |
1575 | atomic_set(&memcg->numainfo_events, 0); |
1576 | atomic_set(&memcg->numainfo_updating, 0); | |
889976db YH |
1577 | } |
1578 | ||
1579 | /* | |
1580 | * Selecting a node where we start reclaim from. Because what we need is just | |
1581 | * reducing usage counter, start from anywhere is O,K. Considering | |
1582 | * memory reclaim from current node, there are pros. and cons. | |
1583 | * | |
1584 | * Freeing memory from current node means freeing memory from a node which | |
1585 | * we'll use or we've used. So, it may make LRU bad. And if several threads | |
1586 | * hit limits, it will see a contention on a node. But freeing from remote | |
1587 | * node means more costs for memory reclaim because of memory latency. | |
1588 | * | |
1589 | * Now, we use round-robin. Better algorithm is welcomed. | |
1590 | */ | |
c0ff4b85 | 1591 | int mem_cgroup_select_victim_node(struct mem_cgroup *memcg) |
889976db YH |
1592 | { |
1593 | int node; | |
1594 | ||
c0ff4b85 R |
1595 | mem_cgroup_may_update_nodemask(memcg); |
1596 | node = memcg->last_scanned_node; | |
889976db | 1597 | |
c0ff4b85 | 1598 | node = next_node(node, memcg->scan_nodes); |
889976db | 1599 | if (node == MAX_NUMNODES) |
c0ff4b85 | 1600 | node = first_node(memcg->scan_nodes); |
889976db YH |
1601 | /* |
1602 | * We call this when we hit limit, not when pages are added to LRU. | |
1603 | * No LRU may hold pages because all pages are UNEVICTABLE or | |
1604 | * memcg is too small and all pages are not on LRU. In that case, | |
1605 | * we use curret node. | |
1606 | */ | |
1607 | if (unlikely(node == MAX_NUMNODES)) | |
1608 | node = numa_node_id(); | |
1609 | ||
c0ff4b85 | 1610 | memcg->last_scanned_node = node; |
889976db YH |
1611 | return node; |
1612 | } | |
1613 | ||
4d0c066d KH |
1614 | /* |
1615 | * Check all nodes whether it contains reclaimable pages or not. | |
1616 | * For quick scan, we make use of scan_nodes. This will allow us to skip | |
1617 | * unused nodes. But scan_nodes is lazily updated and may not cotain | |
1618 | * enough new information. We need to do double check. | |
1619 | */ | |
c0ff4b85 | 1620 | bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap) |
4d0c066d KH |
1621 | { |
1622 | int nid; | |
1623 | ||
1624 | /* | |
1625 | * quick check...making use of scan_node. | |
1626 | * We can skip unused nodes. | |
1627 | */ | |
c0ff4b85 R |
1628 | if (!nodes_empty(memcg->scan_nodes)) { |
1629 | for (nid = first_node(memcg->scan_nodes); | |
4d0c066d | 1630 | nid < MAX_NUMNODES; |
c0ff4b85 | 1631 | nid = next_node(nid, memcg->scan_nodes)) { |
4d0c066d | 1632 | |
c0ff4b85 | 1633 | if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap)) |
4d0c066d KH |
1634 | return true; |
1635 | } | |
1636 | } | |
1637 | /* | |
1638 | * Check rest of nodes. | |
1639 | */ | |
1640 | for_each_node_state(nid, N_HIGH_MEMORY) { | |
c0ff4b85 | 1641 | if (node_isset(nid, memcg->scan_nodes)) |
4d0c066d | 1642 | continue; |
c0ff4b85 | 1643 | if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap)) |
4d0c066d KH |
1644 | return true; |
1645 | } | |
1646 | return false; | |
1647 | } | |
1648 | ||
889976db | 1649 | #else |
c0ff4b85 | 1650 | int mem_cgroup_select_victim_node(struct mem_cgroup *memcg) |
889976db YH |
1651 | { |
1652 | return 0; | |
1653 | } | |
4d0c066d | 1654 | |
c0ff4b85 | 1655 | bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap) |
4d0c066d | 1656 | { |
c0ff4b85 | 1657 | return test_mem_cgroup_node_reclaimable(memcg, 0, noswap); |
4d0c066d | 1658 | } |
889976db YH |
1659 | #endif |
1660 | ||
04046e1a KH |
1661 | /* |
1662 | * Scan the hierarchy if needed to reclaim memory. We remember the last child | |
1663 | * we reclaimed from, so that we don't end up penalizing one child extensively | |
1664 | * based on its position in the children list. | |
6d61ef40 | 1665 | * |
c0ff4b85 | 1666 | * root_memcg is the original ancestor that we've been reclaim from. |
04046e1a | 1667 | * |
c0ff4b85 | 1668 | * We give up and return to the caller when we visit root_memcg twice. |
04046e1a | 1669 | * (other groups can be removed while we're walking....) |
81d39c20 KH |
1670 | * |
1671 | * If shrink==true, for avoiding to free too much, this returns immedieately. | |
6d61ef40 | 1672 | */ |
c0ff4b85 | 1673 | static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_memcg, |
4e416953 | 1674 | struct zone *zone, |
75822b44 | 1675 | gfp_t gfp_mask, |
0ae5e89c YH |
1676 | unsigned long reclaim_options, |
1677 | unsigned long *total_scanned) | |
6d61ef40 | 1678 | { |
04046e1a KH |
1679 | struct mem_cgroup *victim; |
1680 | int ret, total = 0; | |
1681 | int loop = 0; | |
75822b44 BS |
1682 | bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP; |
1683 | bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK; | |
4e416953 | 1684 | bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT; |
9d11ea9f | 1685 | unsigned long excess; |
185efc0f | 1686 | unsigned long nr_scanned; |
9d11ea9f | 1687 | |
c0ff4b85 | 1688 | excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT; |
04046e1a | 1689 | |
22a668d7 | 1690 | /* If memsw_is_minimum==1, swap-out is of-no-use. */ |
c0ff4b85 | 1691 | if (!check_soft && !shrink && root_memcg->memsw_is_minimum) |
22a668d7 KH |
1692 | noswap = true; |
1693 | ||
4e416953 | 1694 | while (1) { |
c0ff4b85 R |
1695 | victim = mem_cgroup_select_victim(root_memcg); |
1696 | if (victim == root_memcg) { | |
04046e1a | 1697 | loop++; |
fbc29a25 KH |
1698 | /* |
1699 | * We are not draining per cpu cached charges during | |
1700 | * soft limit reclaim because global reclaim doesn't | |
1701 | * care about charges. It tries to free some memory and | |
1702 | * charges will not give any. | |
1703 | */ | |
1704 | if (!check_soft && loop >= 1) | |
c0ff4b85 | 1705 | drain_all_stock_async(root_memcg); |
4e416953 BS |
1706 | if (loop >= 2) { |
1707 | /* | |
1708 | * If we have not been able to reclaim | |
1709 | * anything, it might because there are | |
1710 | * no reclaimable pages under this hierarchy | |
1711 | */ | |
1712 | if (!check_soft || !total) { | |
1713 | css_put(&victim->css); | |
1714 | break; | |
1715 | } | |
1716 | /* | |
25985edc | 1717 | * We want to do more targeted reclaim. |
4e416953 BS |
1718 | * excess >> 2 is not to excessive so as to |
1719 | * reclaim too much, nor too less that we keep | |
1720 | * coming back to reclaim from this cgroup | |
1721 | */ | |
1722 | if (total >= (excess >> 2) || | |
1723 | (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) { | |
1724 | css_put(&victim->css); | |
1725 | break; | |
1726 | } | |
1727 | } | |
1728 | } | |
4d0c066d | 1729 | if (!mem_cgroup_reclaimable(victim, noswap)) { |
04046e1a KH |
1730 | /* this cgroup's local usage == 0 */ |
1731 | css_put(&victim->css); | |
6d61ef40 BS |
1732 | continue; |
1733 | } | |
04046e1a | 1734 | /* we use swappiness of local cgroup */ |
0ae5e89c | 1735 | if (check_soft) { |
4e416953 | 1736 | ret = mem_cgroup_shrink_node_zone(victim, gfp_mask, |
185efc0f JW |
1737 | noswap, zone, &nr_scanned); |
1738 | *total_scanned += nr_scanned; | |
0ae5e89c | 1739 | } else |
4e416953 | 1740 | ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, |
185efc0f | 1741 | noswap); |
04046e1a | 1742 | css_put(&victim->css); |
81d39c20 KH |
1743 | /* |
1744 | * At shrinking usage, we can't check we should stop here or | |
1745 | * reclaim more. It's depends on callers. last_scanned_child | |
1746 | * will work enough for keeping fairness under tree. | |
1747 | */ | |
1748 | if (shrink) | |
1749 | return ret; | |
04046e1a | 1750 | total += ret; |
4e416953 | 1751 | if (check_soft) { |
c0ff4b85 | 1752 | if (!res_counter_soft_limit_excess(&root_memcg->res)) |
4e416953 | 1753 | return total; |
c0ff4b85 | 1754 | } else if (mem_cgroup_margin(root_memcg)) |
4fd14ebf | 1755 | return total; |
6d61ef40 | 1756 | } |
04046e1a | 1757 | return total; |
6d61ef40 BS |
1758 | } |
1759 | ||
867578cb KH |
1760 | /* |
1761 | * Check OOM-Killer is already running under our hierarchy. | |
1762 | * If someone is running, return false. | |
1af8efe9 | 1763 | * Has to be called with memcg_oom_lock |
867578cb | 1764 | */ |
c0ff4b85 | 1765 | static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg) |
867578cb | 1766 | { |
79dfdacc MH |
1767 | struct mem_cgroup *iter, *failed = NULL; |
1768 | bool cond = true; | |
a636b327 | 1769 | |
c0ff4b85 | 1770 | for_each_mem_cgroup_tree_cond(iter, memcg, cond) { |
23751be0 | 1771 | if (iter->oom_lock) { |
79dfdacc MH |
1772 | /* |
1773 | * this subtree of our hierarchy is already locked | |
1774 | * so we cannot give a lock. | |
1775 | */ | |
79dfdacc MH |
1776 | failed = iter; |
1777 | cond = false; | |
23751be0 JW |
1778 | } else |
1779 | iter->oom_lock = true; | |
7d74b06f | 1780 | } |
867578cb | 1781 | |
79dfdacc | 1782 | if (!failed) |
23751be0 | 1783 | return true; |
79dfdacc MH |
1784 | |
1785 | /* | |
1786 | * OK, we failed to lock the whole subtree so we have to clean up | |
1787 | * what we set up to the failing subtree | |
1788 | */ | |
1789 | cond = true; | |
c0ff4b85 | 1790 | for_each_mem_cgroup_tree_cond(iter, memcg, cond) { |
79dfdacc MH |
1791 | if (iter == failed) { |
1792 | cond = false; | |
1793 | continue; | |
1794 | } | |
1795 | iter->oom_lock = false; | |
1796 | } | |
23751be0 | 1797 | return false; |
a636b327 | 1798 | } |
0b7f569e | 1799 | |
79dfdacc | 1800 | /* |
1af8efe9 | 1801 | * Has to be called with memcg_oom_lock |
79dfdacc | 1802 | */ |
c0ff4b85 | 1803 | static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg) |
0b7f569e | 1804 | { |
7d74b06f KH |
1805 | struct mem_cgroup *iter; |
1806 | ||
c0ff4b85 | 1807 | for_each_mem_cgroup_tree(iter, memcg) |
79dfdacc MH |
1808 | iter->oom_lock = false; |
1809 | return 0; | |
1810 | } | |
1811 | ||
c0ff4b85 | 1812 | static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg) |
79dfdacc MH |
1813 | { |
1814 | struct mem_cgroup *iter; | |
1815 | ||
c0ff4b85 | 1816 | for_each_mem_cgroup_tree(iter, memcg) |
79dfdacc MH |
1817 | atomic_inc(&iter->under_oom); |
1818 | } | |
1819 | ||
c0ff4b85 | 1820 | static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg) |
79dfdacc MH |
1821 | { |
1822 | struct mem_cgroup *iter; | |
1823 | ||
867578cb KH |
1824 | /* |
1825 | * When a new child is created while the hierarchy is under oom, | |
1826 | * mem_cgroup_oom_lock() may not be called. We have to use | |
1827 | * atomic_add_unless() here. | |
1828 | */ | |
c0ff4b85 | 1829 | for_each_mem_cgroup_tree(iter, memcg) |
79dfdacc | 1830 | atomic_add_unless(&iter->under_oom, -1, 0); |
0b7f569e KH |
1831 | } |
1832 | ||
1af8efe9 | 1833 | static DEFINE_SPINLOCK(memcg_oom_lock); |
867578cb KH |
1834 | static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq); |
1835 | ||
dc98df5a KH |
1836 | struct oom_wait_info { |
1837 | struct mem_cgroup *mem; | |
1838 | wait_queue_t wait; | |
1839 | }; | |
1840 | ||
1841 | static int memcg_oom_wake_function(wait_queue_t *wait, | |
1842 | unsigned mode, int sync, void *arg) | |
1843 | { | |
c0ff4b85 R |
1844 | struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg, |
1845 | *oom_wait_memcg; | |
dc98df5a KH |
1846 | struct oom_wait_info *oom_wait_info; |
1847 | ||
1848 | oom_wait_info = container_of(wait, struct oom_wait_info, wait); | |
c0ff4b85 | 1849 | oom_wait_memcg = oom_wait_info->mem; |
dc98df5a | 1850 | |
dc98df5a KH |
1851 | /* |
1852 | * Both of oom_wait_info->mem and wake_mem are stable under us. | |
1853 | * Then we can use css_is_ancestor without taking care of RCU. | |
1854 | */ | |
c0ff4b85 R |
1855 | if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg) |
1856 | && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg)) | |
dc98df5a | 1857 | return 0; |
dc98df5a KH |
1858 | return autoremove_wake_function(wait, mode, sync, arg); |
1859 | } | |
1860 | ||
c0ff4b85 | 1861 | static void memcg_wakeup_oom(struct mem_cgroup *memcg) |
dc98df5a | 1862 | { |
c0ff4b85 R |
1863 | /* for filtering, pass "memcg" as argument. */ |
1864 | __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg); | |
dc98df5a KH |
1865 | } |
1866 | ||
c0ff4b85 | 1867 | static void memcg_oom_recover(struct mem_cgroup *memcg) |
3c11ecf4 | 1868 | { |
c0ff4b85 R |
1869 | if (memcg && atomic_read(&memcg->under_oom)) |
1870 | memcg_wakeup_oom(memcg); | |
3c11ecf4 KH |
1871 | } |
1872 | ||
867578cb KH |
1873 | /* |
1874 | * try to call OOM killer. returns false if we should exit memory-reclaim loop. | |
1875 | */ | |
c0ff4b85 | 1876 | bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask) |
0b7f569e | 1877 | { |
dc98df5a | 1878 | struct oom_wait_info owait; |
3c11ecf4 | 1879 | bool locked, need_to_kill; |
867578cb | 1880 | |
c0ff4b85 | 1881 | owait.mem = memcg; |
dc98df5a KH |
1882 | owait.wait.flags = 0; |
1883 | owait.wait.func = memcg_oom_wake_function; | |
1884 | owait.wait.private = current; | |
1885 | INIT_LIST_HEAD(&owait.wait.task_list); | |
3c11ecf4 | 1886 | need_to_kill = true; |
c0ff4b85 | 1887 | mem_cgroup_mark_under_oom(memcg); |
79dfdacc | 1888 | |
c0ff4b85 | 1889 | /* At first, try to OOM lock hierarchy under memcg.*/ |
1af8efe9 | 1890 | spin_lock(&memcg_oom_lock); |
c0ff4b85 | 1891 | locked = mem_cgroup_oom_lock(memcg); |
867578cb KH |
1892 | /* |
1893 | * Even if signal_pending(), we can't quit charge() loop without | |
1894 | * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL | |
1895 | * under OOM is always welcomed, use TASK_KILLABLE here. | |
1896 | */ | |
3c11ecf4 | 1897 | prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE); |
c0ff4b85 | 1898 | if (!locked || memcg->oom_kill_disable) |
3c11ecf4 KH |
1899 | need_to_kill = false; |
1900 | if (locked) | |
c0ff4b85 | 1901 | mem_cgroup_oom_notify(memcg); |
1af8efe9 | 1902 | spin_unlock(&memcg_oom_lock); |
867578cb | 1903 | |
3c11ecf4 KH |
1904 | if (need_to_kill) { |
1905 | finish_wait(&memcg_oom_waitq, &owait.wait); | |
c0ff4b85 | 1906 | mem_cgroup_out_of_memory(memcg, mask); |
3c11ecf4 | 1907 | } else { |
867578cb | 1908 | schedule(); |
dc98df5a | 1909 | finish_wait(&memcg_oom_waitq, &owait.wait); |
867578cb | 1910 | } |
1af8efe9 | 1911 | spin_lock(&memcg_oom_lock); |
79dfdacc | 1912 | if (locked) |
c0ff4b85 R |
1913 | mem_cgroup_oom_unlock(memcg); |
1914 | memcg_wakeup_oom(memcg); | |
1af8efe9 | 1915 | spin_unlock(&memcg_oom_lock); |
867578cb | 1916 | |
c0ff4b85 | 1917 | mem_cgroup_unmark_under_oom(memcg); |
79dfdacc | 1918 | |
867578cb KH |
1919 | if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current)) |
1920 | return false; | |
1921 | /* Give chance to dying process */ | |
715a5ee8 | 1922 | schedule_timeout_uninterruptible(1); |
867578cb | 1923 | return true; |
0b7f569e KH |
1924 | } |
1925 | ||
d69b042f BS |
1926 | /* |
1927 | * Currently used to update mapped file statistics, but the routine can be | |
1928 | * generalized to update other statistics as well. | |
32047e2a KH |
1929 | * |
1930 | * Notes: Race condition | |
1931 | * | |
1932 | * We usually use page_cgroup_lock() for accessing page_cgroup member but | |
1933 | * it tends to be costly. But considering some conditions, we doesn't need | |
1934 | * to do so _always_. | |
1935 | * | |
1936 | * Considering "charge", lock_page_cgroup() is not required because all | |
1937 | * file-stat operations happen after a page is attached to radix-tree. There | |
1938 | * are no race with "charge". | |
1939 | * | |
1940 | * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup | |
1941 | * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even | |
1942 | * if there are race with "uncharge". Statistics itself is properly handled | |
1943 | * by flags. | |
1944 | * | |
1945 | * Considering "move", this is an only case we see a race. To make the race | |
1946 | * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are | |
1947 | * possibility of race condition. If there is, we take a lock. | |
d69b042f | 1948 | */ |
26174efd | 1949 | |
2a7106f2 GT |
1950 | void mem_cgroup_update_page_stat(struct page *page, |
1951 | enum mem_cgroup_page_stat_item idx, int val) | |
d69b042f | 1952 | { |
c0ff4b85 | 1953 | struct mem_cgroup *memcg; |
32047e2a KH |
1954 | struct page_cgroup *pc = lookup_page_cgroup(page); |
1955 | bool need_unlock = false; | |
dbd4ea78 | 1956 | unsigned long uninitialized_var(flags); |
d69b042f | 1957 | |
d69b042f BS |
1958 | if (unlikely(!pc)) |
1959 | return; | |
1960 | ||
32047e2a | 1961 | rcu_read_lock(); |
c0ff4b85 R |
1962 | memcg = pc->mem_cgroup; |
1963 | if (unlikely(!memcg || !PageCgroupUsed(pc))) | |
32047e2a KH |
1964 | goto out; |
1965 | /* pc->mem_cgroup is unstable ? */ | |
c0ff4b85 | 1966 | if (unlikely(mem_cgroup_stealed(memcg)) || PageTransHuge(page)) { |
32047e2a | 1967 | /* take a lock against to access pc->mem_cgroup */ |
dbd4ea78 | 1968 | move_lock_page_cgroup(pc, &flags); |
32047e2a | 1969 | need_unlock = true; |
c0ff4b85 R |
1970 | memcg = pc->mem_cgroup; |
1971 | if (!memcg || !PageCgroupUsed(pc)) | |
32047e2a KH |
1972 | goto out; |
1973 | } | |
26174efd | 1974 | |
26174efd | 1975 | switch (idx) { |
2a7106f2 | 1976 | case MEMCG_NR_FILE_MAPPED: |
26174efd KH |
1977 | if (val > 0) |
1978 | SetPageCgroupFileMapped(pc); | |
1979 | else if (!page_mapped(page)) | |
0c270f8f | 1980 | ClearPageCgroupFileMapped(pc); |
2a7106f2 | 1981 | idx = MEM_CGROUP_STAT_FILE_MAPPED; |
26174efd KH |
1982 | break; |
1983 | default: | |
1984 | BUG(); | |
8725d541 | 1985 | } |
d69b042f | 1986 | |
c0ff4b85 | 1987 | this_cpu_add(memcg->stat->count[idx], val); |
2a7106f2 | 1988 | |
32047e2a KH |
1989 | out: |
1990 | if (unlikely(need_unlock)) | |
dbd4ea78 | 1991 | move_unlock_page_cgroup(pc, &flags); |
32047e2a KH |
1992 | rcu_read_unlock(); |
1993 | return; | |
d69b042f | 1994 | } |
2a7106f2 | 1995 | EXPORT_SYMBOL(mem_cgroup_update_page_stat); |
26174efd | 1996 | |
cdec2e42 KH |
1997 | /* |
1998 | * size of first charge trial. "32" comes from vmscan.c's magic value. | |
1999 | * TODO: maybe necessary to use big numbers in big irons. | |
2000 | */ | |
7ec99d62 | 2001 | #define CHARGE_BATCH 32U |
cdec2e42 KH |
2002 | struct memcg_stock_pcp { |
2003 | struct mem_cgroup *cached; /* this never be root cgroup */ | |
11c9ea4e | 2004 | unsigned int nr_pages; |
cdec2e42 | 2005 | struct work_struct work; |
26fe6168 KH |
2006 | unsigned long flags; |
2007 | #define FLUSHING_CACHED_CHARGE (0) | |
cdec2e42 KH |
2008 | }; |
2009 | static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock); | |
9f50fad6 | 2010 | static DEFINE_MUTEX(percpu_charge_mutex); |
cdec2e42 KH |
2011 | |
2012 | /* | |
11c9ea4e | 2013 | * Try to consume stocked charge on this cpu. If success, one page is consumed |
cdec2e42 KH |
2014 | * from local stock and true is returned. If the stock is 0 or charges from a |
2015 | * cgroup which is not current target, returns false. This stock will be | |
2016 | * refilled. | |
2017 | */ | |
c0ff4b85 | 2018 | static bool consume_stock(struct mem_cgroup *memcg) |
cdec2e42 KH |
2019 | { |
2020 | struct memcg_stock_pcp *stock; | |
2021 | bool ret = true; | |
2022 | ||
2023 | stock = &get_cpu_var(memcg_stock); | |
c0ff4b85 | 2024 | if (memcg == stock->cached && stock->nr_pages) |
11c9ea4e | 2025 | stock->nr_pages--; |
cdec2e42 KH |
2026 | else /* need to call res_counter_charge */ |
2027 | ret = false; | |
2028 | put_cpu_var(memcg_stock); | |
2029 | return ret; | |
2030 | } | |
2031 | ||
2032 | /* | |
2033 | * Returns stocks cached in percpu to res_counter and reset cached information. | |
2034 | */ | |
2035 | static void drain_stock(struct memcg_stock_pcp *stock) | |
2036 | { | |
2037 | struct mem_cgroup *old = stock->cached; | |
2038 | ||
11c9ea4e JW |
2039 | if (stock->nr_pages) { |
2040 | unsigned long bytes = stock->nr_pages * PAGE_SIZE; | |
2041 | ||
2042 | res_counter_uncharge(&old->res, bytes); | |
cdec2e42 | 2043 | if (do_swap_account) |
11c9ea4e JW |
2044 | res_counter_uncharge(&old->memsw, bytes); |
2045 | stock->nr_pages = 0; | |
cdec2e42 KH |
2046 | } |
2047 | stock->cached = NULL; | |
cdec2e42 KH |
2048 | } |
2049 | ||
2050 | /* | |
2051 | * This must be called under preempt disabled or must be called by | |
2052 | * a thread which is pinned to local cpu. | |
2053 | */ | |
2054 | static void drain_local_stock(struct work_struct *dummy) | |
2055 | { | |
2056 | struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock); | |
2057 | drain_stock(stock); | |
26fe6168 | 2058 | clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); |
cdec2e42 KH |
2059 | } |
2060 | ||
2061 | /* | |
2062 | * Cache charges(val) which is from res_counter, to local per_cpu area. | |
320cc51d | 2063 | * This will be consumed by consume_stock() function, later. |
cdec2e42 | 2064 | */ |
c0ff4b85 | 2065 | static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages) |
cdec2e42 KH |
2066 | { |
2067 | struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock); | |
2068 | ||
c0ff4b85 | 2069 | if (stock->cached != memcg) { /* reset if necessary */ |
cdec2e42 | 2070 | drain_stock(stock); |
c0ff4b85 | 2071 | stock->cached = memcg; |
cdec2e42 | 2072 | } |
11c9ea4e | 2073 | stock->nr_pages += nr_pages; |
cdec2e42 KH |
2074 | put_cpu_var(memcg_stock); |
2075 | } | |
2076 | ||
2077 | /* | |
c0ff4b85 | 2078 | * Drains all per-CPU charge caches for given root_memcg resp. subtree |
d38144b7 MH |
2079 | * of the hierarchy under it. sync flag says whether we should block |
2080 | * until the work is done. | |
cdec2e42 | 2081 | */ |
c0ff4b85 | 2082 | static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync) |
cdec2e42 | 2083 | { |
26fe6168 | 2084 | int cpu, curcpu; |
d38144b7 | 2085 | |
cdec2e42 | 2086 | /* Notify other cpus that system-wide "drain" is running */ |
cdec2e42 | 2087 | get_online_cpus(); |
5af12d0e | 2088 | curcpu = get_cpu(); |
cdec2e42 KH |
2089 | for_each_online_cpu(cpu) { |
2090 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); | |
c0ff4b85 | 2091 | struct mem_cgroup *memcg; |
26fe6168 | 2092 | |
c0ff4b85 R |
2093 | memcg = stock->cached; |
2094 | if (!memcg || !stock->nr_pages) | |
26fe6168 | 2095 | continue; |
c0ff4b85 | 2096 | if (!mem_cgroup_same_or_subtree(root_memcg, memcg)) |
3e92041d | 2097 | continue; |
d1a05b69 MH |
2098 | if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) { |
2099 | if (cpu == curcpu) | |
2100 | drain_local_stock(&stock->work); | |
2101 | else | |
2102 | schedule_work_on(cpu, &stock->work); | |
2103 | } | |
cdec2e42 | 2104 | } |
5af12d0e | 2105 | put_cpu(); |
d38144b7 MH |
2106 | |
2107 | if (!sync) | |
2108 | goto out; | |
2109 | ||
2110 | for_each_online_cpu(cpu) { | |
2111 | struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu); | |
9f50fad6 | 2112 | if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) |
d38144b7 MH |
2113 | flush_work(&stock->work); |
2114 | } | |
2115 | out: | |
cdec2e42 | 2116 | put_online_cpus(); |
d38144b7 MH |
2117 | } |
2118 | ||
2119 | /* | |
2120 | * Tries to drain stocked charges in other cpus. This function is asynchronous | |
2121 | * and just put a work per cpu for draining localy on each cpu. Caller can | |
2122 | * expects some charges will be back to res_counter later but cannot wait for | |
2123 | * it. | |
2124 | */ | |
c0ff4b85 | 2125 | static void drain_all_stock_async(struct mem_cgroup *root_memcg) |
d38144b7 | 2126 | { |
9f50fad6 MH |
2127 | /* |
2128 | * If someone calls draining, avoid adding more kworker runs. | |
2129 | */ | |
2130 | if (!mutex_trylock(&percpu_charge_mutex)) | |
2131 | return; | |
c0ff4b85 | 2132 | drain_all_stock(root_memcg, false); |
9f50fad6 | 2133 | mutex_unlock(&percpu_charge_mutex); |
cdec2e42 KH |
2134 | } |
2135 | ||
2136 | /* This is a synchronous drain interface. */ | |
c0ff4b85 | 2137 | static void drain_all_stock_sync(struct mem_cgroup *root_memcg) |
cdec2e42 KH |
2138 | { |
2139 | /* called when force_empty is called */ | |
9f50fad6 | 2140 | mutex_lock(&percpu_charge_mutex); |
c0ff4b85 | 2141 | drain_all_stock(root_memcg, true); |
9f50fad6 | 2142 | mutex_unlock(&percpu_charge_mutex); |
cdec2e42 KH |
2143 | } |
2144 | ||
711d3d2c KH |
2145 | /* |
2146 | * This function drains percpu counter value from DEAD cpu and | |
2147 | * move it to local cpu. Note that this function can be preempted. | |
2148 | */ | |
c0ff4b85 | 2149 | static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu) |
711d3d2c KH |
2150 | { |
2151 | int i; | |
2152 | ||
c0ff4b85 | 2153 | spin_lock(&memcg->pcp_counter_lock); |
711d3d2c | 2154 | for (i = 0; i < MEM_CGROUP_STAT_DATA; i++) { |
c0ff4b85 | 2155 | long x = per_cpu(memcg->stat->count[i], cpu); |
711d3d2c | 2156 | |
c0ff4b85 R |
2157 | per_cpu(memcg->stat->count[i], cpu) = 0; |
2158 | memcg->nocpu_base.count[i] += x; | |
711d3d2c | 2159 | } |
e9f8974f | 2160 | for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) { |
c0ff4b85 | 2161 | unsigned long x = per_cpu(memcg->stat->events[i], cpu); |
e9f8974f | 2162 | |
c0ff4b85 R |
2163 | per_cpu(memcg->stat->events[i], cpu) = 0; |
2164 | memcg->nocpu_base.events[i] += x; | |
e9f8974f | 2165 | } |
1489ebad | 2166 | /* need to clear ON_MOVE value, works as a kind of lock. */ |
c0ff4b85 R |
2167 | per_cpu(memcg->stat->count[MEM_CGROUP_ON_MOVE], cpu) = 0; |
2168 | spin_unlock(&memcg->pcp_counter_lock); | |
1489ebad KH |
2169 | } |
2170 | ||
c0ff4b85 | 2171 | static void synchronize_mem_cgroup_on_move(struct mem_cgroup *memcg, int cpu) |
1489ebad KH |
2172 | { |
2173 | int idx = MEM_CGROUP_ON_MOVE; | |
2174 | ||
c0ff4b85 R |
2175 | spin_lock(&memcg->pcp_counter_lock); |
2176 | per_cpu(memcg->stat->count[idx], cpu) = memcg->nocpu_base.count[idx]; | |
2177 | spin_unlock(&memcg->pcp_counter_lock); | |
711d3d2c KH |
2178 | } |
2179 | ||
2180 | static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb, | |
cdec2e42 KH |
2181 | unsigned long action, |
2182 | void *hcpu) | |
2183 | { | |
2184 | int cpu = (unsigned long)hcpu; | |
2185 | struct memcg_stock_pcp *stock; | |
711d3d2c | 2186 | struct mem_cgroup *iter; |
cdec2e42 | 2187 | |
1489ebad KH |
2188 | if ((action == CPU_ONLINE)) { |
2189 | for_each_mem_cgroup_all(iter) | |
2190 | synchronize_mem_cgroup_on_move(iter, cpu); | |
2191 | return NOTIFY_OK; | |
2192 | } | |
2193 | ||
711d3d2c | 2194 | if ((action != CPU_DEAD) || action != CPU_DEAD_FROZEN) |
cdec2e42 | 2195 | return NOTIFY_OK; |
711d3d2c KH |
2196 | |
2197 | for_each_mem_cgroup_all(iter) | |
2198 | mem_cgroup_drain_pcp_counter(iter, cpu); | |
2199 | ||
cdec2e42 KH |
2200 | stock = &per_cpu(memcg_stock, cpu); |
2201 | drain_stock(stock); | |
2202 | return NOTIFY_OK; | |
2203 | } | |
2204 | ||
4b534334 KH |
2205 | |
2206 | /* See __mem_cgroup_try_charge() for details */ | |
2207 | enum { | |
2208 | CHARGE_OK, /* success */ | |
2209 | CHARGE_RETRY, /* need to retry but retry is not bad */ | |
2210 | CHARGE_NOMEM, /* we can't do more. return -ENOMEM */ | |
2211 | CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */ | |
2212 | CHARGE_OOM_DIE, /* the current is killed because of OOM */ | |
2213 | }; | |
2214 | ||
c0ff4b85 | 2215 | static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, |
7ec99d62 | 2216 | unsigned int nr_pages, bool oom_check) |
4b534334 | 2217 | { |
7ec99d62 | 2218 | unsigned long csize = nr_pages * PAGE_SIZE; |
4b534334 KH |
2219 | struct mem_cgroup *mem_over_limit; |
2220 | struct res_counter *fail_res; | |
2221 | unsigned long flags = 0; | |
2222 | int ret; | |
2223 | ||
c0ff4b85 | 2224 | ret = res_counter_charge(&memcg->res, csize, &fail_res); |
4b534334 KH |
2225 | |
2226 | if (likely(!ret)) { | |
2227 | if (!do_swap_account) | |
2228 | return CHARGE_OK; | |
c0ff4b85 | 2229 | ret = res_counter_charge(&memcg->memsw, csize, &fail_res); |
4b534334 KH |
2230 | if (likely(!ret)) |
2231 | return CHARGE_OK; | |
2232 | ||
c0ff4b85 | 2233 | res_counter_uncharge(&memcg->res, csize); |
4b534334 KH |
2234 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw); |
2235 | flags |= MEM_CGROUP_RECLAIM_NOSWAP; | |
2236 | } else | |
2237 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, res); | |
9221edb7 | 2238 | /* |
7ec99d62 JW |
2239 | * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch |
2240 | * of regular pages (CHARGE_BATCH), or a single regular page (1). | |
9221edb7 JW |
2241 | * |
2242 | * Never reclaim on behalf of optional batching, retry with a | |
2243 | * single page instead. | |
2244 | */ | |
7ec99d62 | 2245 | if (nr_pages == CHARGE_BATCH) |
4b534334 KH |
2246 | return CHARGE_RETRY; |
2247 | ||
2248 | if (!(gfp_mask & __GFP_WAIT)) | |
2249 | return CHARGE_WOULDBLOCK; | |
2250 | ||
2251 | ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL, | |
0ae5e89c | 2252 | gfp_mask, flags, NULL); |
7ec99d62 | 2253 | if (mem_cgroup_margin(mem_over_limit) >= nr_pages) |
19942822 | 2254 | return CHARGE_RETRY; |
4b534334 | 2255 | /* |
19942822 JW |
2256 | * Even though the limit is exceeded at this point, reclaim |
2257 | * may have been able to free some pages. Retry the charge | |
2258 | * before killing the task. | |
2259 | * | |
2260 | * Only for regular pages, though: huge pages are rather | |
2261 | * unlikely to succeed so close to the limit, and we fall back | |
2262 | * to regular pages anyway in case of failure. | |
4b534334 | 2263 | */ |
7ec99d62 | 2264 | if (nr_pages == 1 && ret) |
4b534334 KH |
2265 | return CHARGE_RETRY; |
2266 | ||
2267 | /* | |
2268 | * At task move, charge accounts can be doubly counted. So, it's | |
2269 | * better to wait until the end of task_move if something is going on. | |
2270 | */ | |
2271 | if (mem_cgroup_wait_acct_move(mem_over_limit)) | |
2272 | return CHARGE_RETRY; | |
2273 | ||
2274 | /* If we don't need to call oom-killer at el, return immediately */ | |
2275 | if (!oom_check) | |
2276 | return CHARGE_NOMEM; | |
2277 | /* check OOM */ | |
2278 | if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask)) | |
2279 | return CHARGE_OOM_DIE; | |
2280 | ||
2281 | return CHARGE_RETRY; | |
2282 | } | |
2283 | ||
f817ed48 KH |
2284 | /* |
2285 | * Unlike exported interface, "oom" parameter is added. if oom==true, | |
2286 | * oom-killer can be invoked. | |
8a9f3ccd | 2287 | */ |
f817ed48 | 2288 | static int __mem_cgroup_try_charge(struct mm_struct *mm, |
ec168510 | 2289 | gfp_t gfp_mask, |
7ec99d62 | 2290 | unsigned int nr_pages, |
c0ff4b85 | 2291 | struct mem_cgroup **ptr, |
7ec99d62 | 2292 | bool oom) |
8a9f3ccd | 2293 | { |
7ec99d62 | 2294 | unsigned int batch = max(CHARGE_BATCH, nr_pages); |
4b534334 | 2295 | int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; |
c0ff4b85 | 2296 | struct mem_cgroup *memcg = NULL; |
4b534334 | 2297 | int ret; |
a636b327 | 2298 | |
867578cb KH |
2299 | /* |
2300 | * Unlike gloval-vm's OOM-kill, we're not in memory shortage | |
2301 | * in system level. So, allow to go ahead dying process in addition to | |
2302 | * MEMDIE process. | |
2303 | */ | |
2304 | if (unlikely(test_thread_flag(TIF_MEMDIE) | |
2305 | || fatal_signal_pending(current))) | |
2306 | goto bypass; | |
a636b327 | 2307 | |
8a9f3ccd | 2308 | /* |
3be91277 HD |
2309 | * We always charge the cgroup the mm_struct belongs to. |
2310 | * The mm_struct's mem_cgroup changes on task migration if the | |
8a9f3ccd BS |
2311 | * thread group leader migrates. It's possible that mm is not |
2312 | * set, if so charge the init_mm (happens for pagecache usage). | |
2313 | */ | |
c0ff4b85 | 2314 | if (!*ptr && !mm) |
f75ca962 KH |
2315 | goto bypass; |
2316 | again: | |
c0ff4b85 R |
2317 | if (*ptr) { /* css should be a valid one */ |
2318 | memcg = *ptr; | |
2319 | VM_BUG_ON(css_is_removed(&memcg->css)); | |
2320 | if (mem_cgroup_is_root(memcg)) | |
f75ca962 | 2321 | goto done; |
c0ff4b85 | 2322 | if (nr_pages == 1 && consume_stock(memcg)) |
f75ca962 | 2323 | goto done; |
c0ff4b85 | 2324 | css_get(&memcg->css); |
4b534334 | 2325 | } else { |
f75ca962 | 2326 | struct task_struct *p; |
54595fe2 | 2327 | |
f75ca962 KH |
2328 | rcu_read_lock(); |
2329 | p = rcu_dereference(mm->owner); | |
f75ca962 | 2330 | /* |
ebb76ce1 | 2331 | * Because we don't have task_lock(), "p" can exit. |
c0ff4b85 | 2332 | * In that case, "memcg" can point to root or p can be NULL with |
ebb76ce1 KH |
2333 | * race with swapoff. Then, we have small risk of mis-accouning. |
2334 | * But such kind of mis-account by race always happens because | |
2335 | * we don't have cgroup_mutex(). It's overkill and we allo that | |
2336 | * small race, here. | |
2337 | * (*) swapoff at el will charge against mm-struct not against | |
2338 | * task-struct. So, mm->owner can be NULL. | |
f75ca962 | 2339 | */ |
c0ff4b85 R |
2340 | memcg = mem_cgroup_from_task(p); |
2341 | if (!memcg || mem_cgroup_is_root(memcg)) { | |
f75ca962 KH |
2342 | rcu_read_unlock(); |
2343 | goto done; | |
2344 | } | |
c0ff4b85 | 2345 | if (nr_pages == 1 && consume_stock(memcg)) { |
f75ca962 KH |
2346 | /* |
2347 | * It seems dagerous to access memcg without css_get(). | |
2348 | * But considering how consume_stok works, it's not | |
2349 | * necessary. If consume_stock success, some charges | |
2350 | * from this memcg are cached on this cpu. So, we | |
2351 | * don't need to call css_get()/css_tryget() before | |
2352 | * calling consume_stock(). | |
2353 | */ | |
2354 | rcu_read_unlock(); | |
2355 | goto done; | |
2356 | } | |
2357 | /* after here, we may be blocked. we need to get refcnt */ | |
c0ff4b85 | 2358 | if (!css_tryget(&memcg->css)) { |
f75ca962 KH |
2359 | rcu_read_unlock(); |
2360 | goto again; | |
2361 | } | |
2362 | rcu_read_unlock(); | |
2363 | } | |
8a9f3ccd | 2364 | |
4b534334 KH |
2365 | do { |
2366 | bool oom_check; | |
7a81b88c | 2367 | |
4b534334 | 2368 | /* If killed, bypass charge */ |
f75ca962 | 2369 | if (fatal_signal_pending(current)) { |
c0ff4b85 | 2370 | css_put(&memcg->css); |
4b534334 | 2371 | goto bypass; |
f75ca962 | 2372 | } |
6d61ef40 | 2373 | |
4b534334 KH |
2374 | oom_check = false; |
2375 | if (oom && !nr_oom_retries) { | |
2376 | oom_check = true; | |
2377 | nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES; | |
cdec2e42 | 2378 | } |
66e1707b | 2379 | |
c0ff4b85 | 2380 | ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, oom_check); |
4b534334 KH |
2381 | switch (ret) { |
2382 | case CHARGE_OK: | |
2383 | break; | |
2384 | case CHARGE_RETRY: /* not in OOM situation but retry */ | |
7ec99d62 | 2385 | batch = nr_pages; |
c0ff4b85 R |
2386 | css_put(&memcg->css); |
2387 | memcg = NULL; | |
f75ca962 | 2388 | goto again; |
4b534334 | 2389 | case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */ |
c0ff4b85 | 2390 | css_put(&memcg->css); |
4b534334 KH |
2391 | goto nomem; |
2392 | case CHARGE_NOMEM: /* OOM routine works */ | |
f75ca962 | 2393 | if (!oom) { |
c0ff4b85 | 2394 | css_put(&memcg->css); |
867578cb | 2395 | goto nomem; |
f75ca962 | 2396 | } |
4b534334 KH |
2397 | /* If oom, we never return -ENOMEM */ |
2398 | nr_oom_retries--; | |
2399 | break; | |
2400 | case CHARGE_OOM_DIE: /* Killed by OOM Killer */ | |
c0ff4b85 | 2401 | css_put(&memcg->css); |
867578cb | 2402 | goto bypass; |
66e1707b | 2403 | } |
4b534334 KH |
2404 | } while (ret != CHARGE_OK); |
2405 | ||
7ec99d62 | 2406 | if (batch > nr_pages) |
c0ff4b85 R |
2407 | refill_stock(memcg, batch - nr_pages); |
2408 | css_put(&memcg->css); | |
0c3e73e8 | 2409 | done: |
c0ff4b85 | 2410 | *ptr = memcg; |
7a81b88c KH |
2411 | return 0; |
2412 | nomem: | |
c0ff4b85 | 2413 | *ptr = NULL; |
7a81b88c | 2414 | return -ENOMEM; |
867578cb | 2415 | bypass: |
c0ff4b85 | 2416 | *ptr = NULL; |
867578cb | 2417 | return 0; |
7a81b88c | 2418 | } |
8a9f3ccd | 2419 | |
a3032a2c DN |
2420 | /* |
2421 | * Somemtimes we have to undo a charge we got by try_charge(). | |
2422 | * This function is for that and do uncharge, put css's refcnt. | |
2423 | * gotten by try_charge(). | |
2424 | */ | |
c0ff4b85 | 2425 | static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg, |
e7018b8d | 2426 | unsigned int nr_pages) |
a3032a2c | 2427 | { |
c0ff4b85 | 2428 | if (!mem_cgroup_is_root(memcg)) { |
e7018b8d JW |
2429 | unsigned long bytes = nr_pages * PAGE_SIZE; |
2430 | ||
c0ff4b85 | 2431 | res_counter_uncharge(&memcg->res, bytes); |
a3032a2c | 2432 | if (do_swap_account) |
c0ff4b85 | 2433 | res_counter_uncharge(&memcg->memsw, bytes); |
a3032a2c | 2434 | } |
854ffa8d DN |
2435 | } |
2436 | ||
a3b2d692 KH |
2437 | /* |
2438 | * A helper function to get mem_cgroup from ID. must be called under | |
2439 | * rcu_read_lock(). The caller must check css_is_removed() or some if | |
2440 | * it's concern. (dropping refcnt from swap can be called against removed | |
2441 | * memcg.) | |
2442 | */ | |
2443 | static struct mem_cgroup *mem_cgroup_lookup(unsigned short id) | |
2444 | { | |
2445 | struct cgroup_subsys_state *css; | |
2446 | ||
2447 | /* ID 0 is unused ID */ | |
2448 | if (!id) | |
2449 | return NULL; | |
2450 | css = css_lookup(&mem_cgroup_subsys, id); | |
2451 | if (!css) | |
2452 | return NULL; | |
2453 | return container_of(css, struct mem_cgroup, css); | |
2454 | } | |
2455 | ||
e42d9d5d | 2456 | struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page) |
b5a84319 | 2457 | { |
c0ff4b85 | 2458 | struct mem_cgroup *memcg = NULL; |
3c776e64 | 2459 | struct page_cgroup *pc; |
a3b2d692 | 2460 | unsigned short id; |
b5a84319 KH |
2461 | swp_entry_t ent; |
2462 | ||
3c776e64 DN |
2463 | VM_BUG_ON(!PageLocked(page)); |
2464 | ||
3c776e64 | 2465 | pc = lookup_page_cgroup(page); |
c0bd3f63 | 2466 | lock_page_cgroup(pc); |
a3b2d692 | 2467 | if (PageCgroupUsed(pc)) { |
c0ff4b85 R |
2468 | memcg = pc->mem_cgroup; |
2469 | if (memcg && !css_tryget(&memcg->css)) | |
2470 | memcg = NULL; | |
e42d9d5d | 2471 | } else if (PageSwapCache(page)) { |
3c776e64 | 2472 | ent.val = page_private(page); |
a3b2d692 KH |
2473 | id = lookup_swap_cgroup(ent); |
2474 | rcu_read_lock(); | |
c0ff4b85 R |
2475 | memcg = mem_cgroup_lookup(id); |
2476 | if (memcg && !css_tryget(&memcg->css)) | |
2477 | memcg = NULL; | |
a3b2d692 | 2478 | rcu_read_unlock(); |
3c776e64 | 2479 | } |
c0bd3f63 | 2480 | unlock_page_cgroup(pc); |
c0ff4b85 | 2481 | return memcg; |
b5a84319 KH |
2482 | } |
2483 | ||
c0ff4b85 | 2484 | static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg, |
5564e88b | 2485 | struct page *page, |
7ec99d62 | 2486 | unsigned int nr_pages, |
ca3e0214 | 2487 | struct page_cgroup *pc, |
7ec99d62 | 2488 | enum charge_type ctype) |
7a81b88c | 2489 | { |
ca3e0214 KH |
2490 | lock_page_cgroup(pc); |
2491 | if (unlikely(PageCgroupUsed(pc))) { | |
2492 | unlock_page_cgroup(pc); | |
c0ff4b85 | 2493 | __mem_cgroup_cancel_charge(memcg, nr_pages); |
ca3e0214 KH |
2494 | return; |
2495 | } | |
2496 | /* | |
2497 | * we don't need page_cgroup_lock about tail pages, becase they are not | |
2498 | * accessed by any other context at this point. | |
2499 | */ | |
c0ff4b85 | 2500 | pc->mem_cgroup = memcg; |
261fb61a KH |
2501 | /* |
2502 | * We access a page_cgroup asynchronously without lock_page_cgroup(). | |
2503 | * Especially when a page_cgroup is taken from a page, pc->mem_cgroup | |
2504 | * is accessed after testing USED bit. To make pc->mem_cgroup visible | |
2505 | * before USED bit, we need memory barrier here. | |
2506 | * See mem_cgroup_add_lru_list(), etc. | |
2507 | */ | |
08e552c6 | 2508 | smp_wmb(); |
4b3bde4c BS |
2509 | switch (ctype) { |
2510 | case MEM_CGROUP_CHARGE_TYPE_CACHE: | |
2511 | case MEM_CGROUP_CHARGE_TYPE_SHMEM: | |
2512 | SetPageCgroupCache(pc); | |
2513 | SetPageCgroupUsed(pc); | |
2514 | break; | |
2515 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: | |
2516 | ClearPageCgroupCache(pc); | |
2517 | SetPageCgroupUsed(pc); | |
2518 | break; | |
2519 | default: | |
2520 | break; | |
2521 | } | |
3be91277 | 2522 | |
c0ff4b85 | 2523 | mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), nr_pages); |
52d4b9ac | 2524 | unlock_page_cgroup(pc); |
430e4863 KH |
2525 | /* |
2526 | * "charge_statistics" updated event counter. Then, check it. | |
2527 | * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree. | |
2528 | * if they exceeds softlimit. | |
2529 | */ | |
c0ff4b85 | 2530 | memcg_check_events(memcg, page); |
7a81b88c | 2531 | } |
66e1707b | 2532 | |
ca3e0214 KH |
2533 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
2534 | ||
2535 | #define PCGF_NOCOPY_AT_SPLIT ((1 << PCG_LOCK) | (1 << PCG_MOVE_LOCK) |\ | |
2536 | (1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION)) | |
2537 | /* | |
2538 | * Because tail pages are not marked as "used", set it. We're under | |
2539 | * zone->lru_lock, 'splitting on pmd' and compund_lock. | |
2540 | */ | |
2541 | void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail) | |
2542 | { | |
2543 | struct page_cgroup *head_pc = lookup_page_cgroup(head); | |
2544 | struct page_cgroup *tail_pc = lookup_page_cgroup(tail); | |
2545 | unsigned long flags; | |
2546 | ||
3d37c4a9 KH |
2547 | if (mem_cgroup_disabled()) |
2548 | return; | |
ca3e0214 | 2549 | /* |
ece35ca8 | 2550 | * We have no races with charge/uncharge but will have races with |
ca3e0214 KH |
2551 | * page state accounting. |
2552 | */ | |
2553 | move_lock_page_cgroup(head_pc, &flags); | |
2554 | ||
2555 | tail_pc->mem_cgroup = head_pc->mem_cgroup; | |
2556 | smp_wmb(); /* see __commit_charge() */ | |
ece35ca8 KH |
2557 | if (PageCgroupAcctLRU(head_pc)) { |
2558 | enum lru_list lru; | |
2559 | struct mem_cgroup_per_zone *mz; | |
2560 | ||
2561 | /* | |
2562 | * LRU flags cannot be copied because we need to add tail | |
2563 | *.page to LRU by generic call and our hook will be called. | |
2564 | * We hold lru_lock, then, reduce counter directly. | |
2565 | */ | |
2566 | lru = page_lru(head); | |
97a6c37b | 2567 | mz = page_cgroup_zoneinfo(head_pc->mem_cgroup, head); |
ece35ca8 KH |
2568 | MEM_CGROUP_ZSTAT(mz, lru) -= 1; |
2569 | } | |
ca3e0214 KH |
2570 | tail_pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT; |
2571 | move_unlock_page_cgroup(head_pc, &flags); | |
2572 | } | |
2573 | #endif | |
2574 | ||
f817ed48 | 2575 | /** |
de3638d9 | 2576 | * mem_cgroup_move_account - move account of the page |
5564e88b | 2577 | * @page: the page |
7ec99d62 | 2578 | * @nr_pages: number of regular pages (>1 for huge pages) |
f817ed48 KH |
2579 | * @pc: page_cgroup of the page. |
2580 | * @from: mem_cgroup which the page is moved from. | |
2581 | * @to: mem_cgroup which the page is moved to. @from != @to. | |
854ffa8d | 2582 | * @uncharge: whether we should call uncharge and css_put against @from. |
f817ed48 KH |
2583 | * |
2584 | * The caller must confirm following. | |
08e552c6 | 2585 | * - page is not on LRU (isolate_page() is useful.) |
7ec99d62 | 2586 | * - compound_lock is held when nr_pages > 1 |
f817ed48 | 2587 | * |
854ffa8d | 2588 | * This function doesn't do "charge" nor css_get to new cgroup. It should be |
25985edc | 2589 | * done by a caller(__mem_cgroup_try_charge would be useful). If @uncharge is |
854ffa8d DN |
2590 | * true, this function does "uncharge" from old cgroup, but it doesn't if |
2591 | * @uncharge is false, so a caller should do "uncharge". | |
f817ed48 | 2592 | */ |
7ec99d62 JW |
2593 | static int mem_cgroup_move_account(struct page *page, |
2594 | unsigned int nr_pages, | |
2595 | struct page_cgroup *pc, | |
2596 | struct mem_cgroup *from, | |
2597 | struct mem_cgroup *to, | |
2598 | bool uncharge) | |
f817ed48 | 2599 | { |
de3638d9 JW |
2600 | unsigned long flags; |
2601 | int ret; | |
987eba66 | 2602 | |
f817ed48 | 2603 | VM_BUG_ON(from == to); |
5564e88b | 2604 | VM_BUG_ON(PageLRU(page)); |
de3638d9 JW |
2605 | /* |
2606 | * The page is isolated from LRU. So, collapse function | |
2607 | * will not handle this page. But page splitting can happen. | |
2608 | * Do this check under compound_page_lock(). The caller should | |
2609 | * hold it. | |
2610 | */ | |
2611 | ret = -EBUSY; | |
7ec99d62 | 2612 | if (nr_pages > 1 && !PageTransHuge(page)) |
de3638d9 JW |
2613 | goto out; |
2614 | ||
2615 | lock_page_cgroup(pc); | |
2616 | ||
2617 | ret = -EINVAL; | |
2618 | if (!PageCgroupUsed(pc) || pc->mem_cgroup != from) | |
2619 | goto unlock; | |
2620 | ||
2621 | move_lock_page_cgroup(pc, &flags); | |
f817ed48 | 2622 | |
8725d541 | 2623 | if (PageCgroupFileMapped(pc)) { |
c62b1a3b KH |
2624 | /* Update mapped_file data for mem_cgroup */ |
2625 | preempt_disable(); | |
2626 | __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); | |
2627 | __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]); | |
2628 | preempt_enable(); | |
d69b042f | 2629 | } |
987eba66 | 2630 | mem_cgroup_charge_statistics(from, PageCgroupCache(pc), -nr_pages); |
854ffa8d DN |
2631 | if (uncharge) |
2632 | /* This is not "cancel", but cancel_charge does all we need. */ | |
e7018b8d | 2633 | __mem_cgroup_cancel_charge(from, nr_pages); |
d69b042f | 2634 | |
854ffa8d | 2635 | /* caller should have done css_get */ |
08e552c6 | 2636 | pc->mem_cgroup = to; |
987eba66 | 2637 | mem_cgroup_charge_statistics(to, PageCgroupCache(pc), nr_pages); |
88703267 KH |
2638 | /* |
2639 | * We charges against "to" which may not have any tasks. Then, "to" | |
2640 | * can be under rmdir(). But in current implementation, caller of | |
4ffef5fe | 2641 | * this function is just force_empty() and move charge, so it's |
25985edc | 2642 | * guaranteed that "to" is never removed. So, we don't check rmdir |
4ffef5fe | 2643 | * status here. |
88703267 | 2644 | */ |
de3638d9 JW |
2645 | move_unlock_page_cgroup(pc, &flags); |
2646 | ret = 0; | |
2647 | unlock: | |
57f9fd7d | 2648 | unlock_page_cgroup(pc); |
d2265e6f KH |
2649 | /* |
2650 | * check events | |
2651 | */ | |
5564e88b JW |
2652 | memcg_check_events(to, page); |
2653 | memcg_check_events(from, page); | |
de3638d9 | 2654 | out: |
f817ed48 KH |
2655 | return ret; |
2656 | } | |
2657 | ||
2658 | /* | |
2659 | * move charges to its parent. | |
2660 | */ | |
2661 | ||
5564e88b JW |
2662 | static int mem_cgroup_move_parent(struct page *page, |
2663 | struct page_cgroup *pc, | |
f817ed48 KH |
2664 | struct mem_cgroup *child, |
2665 | gfp_t gfp_mask) | |
2666 | { | |
2667 | struct cgroup *cg = child->css.cgroup; | |
2668 | struct cgroup *pcg = cg->parent; | |
2669 | struct mem_cgroup *parent; | |
7ec99d62 | 2670 | unsigned int nr_pages; |
4be4489f | 2671 | unsigned long uninitialized_var(flags); |
f817ed48 KH |
2672 | int ret; |
2673 | ||
2674 | /* Is ROOT ? */ | |
2675 | if (!pcg) | |
2676 | return -EINVAL; | |
2677 | ||
57f9fd7d DN |
2678 | ret = -EBUSY; |
2679 | if (!get_page_unless_zero(page)) | |
2680 | goto out; | |
2681 | if (isolate_lru_page(page)) | |
2682 | goto put; | |
52dbb905 | 2683 | |
7ec99d62 | 2684 | nr_pages = hpage_nr_pages(page); |
08e552c6 | 2685 | |
f817ed48 | 2686 | parent = mem_cgroup_from_cont(pcg); |
7ec99d62 | 2687 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, nr_pages, &parent, false); |
a636b327 | 2688 | if (ret || !parent) |
57f9fd7d | 2689 | goto put_back; |
f817ed48 | 2690 | |
7ec99d62 | 2691 | if (nr_pages > 1) |
987eba66 KH |
2692 | flags = compound_lock_irqsave(page); |
2693 | ||
7ec99d62 | 2694 | ret = mem_cgroup_move_account(page, nr_pages, pc, child, parent, true); |
854ffa8d | 2695 | if (ret) |
7ec99d62 | 2696 | __mem_cgroup_cancel_charge(parent, nr_pages); |
8dba474f | 2697 | |
7ec99d62 | 2698 | if (nr_pages > 1) |
987eba66 | 2699 | compound_unlock_irqrestore(page, flags); |
8dba474f | 2700 | put_back: |
08e552c6 | 2701 | putback_lru_page(page); |
57f9fd7d | 2702 | put: |
40d58138 | 2703 | put_page(page); |
57f9fd7d | 2704 | out: |
f817ed48 KH |
2705 | return ret; |
2706 | } | |
2707 | ||
7a81b88c KH |
2708 | /* |
2709 | * Charge the memory controller for page usage. | |
2710 | * Return | |
2711 | * 0 if the charge was successful | |
2712 | * < 0 if the cgroup is over its limit | |
2713 | */ | |
2714 | static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm, | |
73045c47 | 2715 | gfp_t gfp_mask, enum charge_type ctype) |
7a81b88c | 2716 | { |
c0ff4b85 | 2717 | struct mem_cgroup *memcg = NULL; |
7ec99d62 | 2718 | unsigned int nr_pages = 1; |
7a81b88c | 2719 | struct page_cgroup *pc; |
8493ae43 | 2720 | bool oom = true; |
7a81b88c | 2721 | int ret; |
ec168510 | 2722 | |
37c2ac78 | 2723 | if (PageTransHuge(page)) { |
7ec99d62 | 2724 | nr_pages <<= compound_order(page); |
37c2ac78 | 2725 | VM_BUG_ON(!PageTransHuge(page)); |
8493ae43 JW |
2726 | /* |
2727 | * Never OOM-kill a process for a huge page. The | |
2728 | * fault handler will fall back to regular pages. | |
2729 | */ | |
2730 | oom = false; | |
37c2ac78 | 2731 | } |
7a81b88c KH |
2732 | |
2733 | pc = lookup_page_cgroup(page); | |
af4a6621 | 2734 | BUG_ON(!pc); /* XXX: remove this and move pc lookup into commit */ |
7a81b88c | 2735 | |
c0ff4b85 R |
2736 | ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom); |
2737 | if (ret || !memcg) | |
7a81b88c KH |
2738 | return ret; |
2739 | ||
c0ff4b85 | 2740 | __mem_cgroup_commit_charge(memcg, page, nr_pages, pc, ctype); |
8a9f3ccd | 2741 | return 0; |
8a9f3ccd BS |
2742 | } |
2743 | ||
7a81b88c KH |
2744 | int mem_cgroup_newpage_charge(struct page *page, |
2745 | struct mm_struct *mm, gfp_t gfp_mask) | |
217bc319 | 2746 | { |
f8d66542 | 2747 | if (mem_cgroup_disabled()) |
cede86ac | 2748 | return 0; |
69029cd5 KH |
2749 | /* |
2750 | * If already mapped, we don't have to account. | |
2751 | * If page cache, page->mapping has address_space. | |
2752 | * But page->mapping may have out-of-use anon_vma pointer, | |
2753 | * detecit it by PageAnon() check. newly-mapped-anon's page->mapping | |
2754 | * is NULL. | |
2755 | */ | |
2756 | if (page_mapped(page) || (page->mapping && !PageAnon(page))) | |
2757 | return 0; | |
2758 | if (unlikely(!mm)) | |
2759 | mm = &init_mm; | |
217bc319 | 2760 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
73045c47 | 2761 | MEM_CGROUP_CHARGE_TYPE_MAPPED); |
217bc319 KH |
2762 | } |
2763 | ||
83aae4c7 DN |
2764 | static void |
2765 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, | |
2766 | enum charge_type ctype); | |
2767 | ||
5a6475a4 | 2768 | static void |
c0ff4b85 | 2769 | __mem_cgroup_commit_charge_lrucare(struct page *page, struct mem_cgroup *memcg, |
5a6475a4 KH |
2770 | enum charge_type ctype) |
2771 | { | |
2772 | struct page_cgroup *pc = lookup_page_cgroup(page); | |
2773 | /* | |
2774 | * In some case, SwapCache, FUSE(splice_buf->radixtree), the page | |
2775 | * is already on LRU. It means the page may on some other page_cgroup's | |
2776 | * LRU. Take care of it. | |
2777 | */ | |
2778 | mem_cgroup_lru_del_before_commit(page); | |
c0ff4b85 | 2779 | __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype); |
5a6475a4 KH |
2780 | mem_cgroup_lru_add_after_commit(page); |
2781 | return; | |
2782 | } | |
2783 | ||
e1a1cd59 BS |
2784 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
2785 | gfp_t gfp_mask) | |
8697d331 | 2786 | { |
c0ff4b85 | 2787 | struct mem_cgroup *memcg = NULL; |
b5a84319 KH |
2788 | int ret; |
2789 | ||
f8d66542 | 2790 | if (mem_cgroup_disabled()) |
cede86ac | 2791 | return 0; |
52d4b9ac KH |
2792 | if (PageCompound(page)) |
2793 | return 0; | |
accf163e | 2794 | |
73045c47 | 2795 | if (unlikely(!mm)) |
8697d331 | 2796 | mm = &init_mm; |
accf163e | 2797 | |
5a6475a4 | 2798 | if (page_is_file_cache(page)) { |
c0ff4b85 R |
2799 | ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, &memcg, true); |
2800 | if (ret || !memcg) | |
5a6475a4 | 2801 | return ret; |
b5a84319 | 2802 | |
5a6475a4 KH |
2803 | /* |
2804 | * FUSE reuses pages without going through the final | |
2805 | * put that would remove them from the LRU list, make | |
2806 | * sure that they get relinked properly. | |
2807 | */ | |
c0ff4b85 | 2808 | __mem_cgroup_commit_charge_lrucare(page, memcg, |
5a6475a4 KH |
2809 | MEM_CGROUP_CHARGE_TYPE_CACHE); |
2810 | return ret; | |
2811 | } | |
83aae4c7 DN |
2812 | /* shmem */ |
2813 | if (PageSwapCache(page)) { | |
c0ff4b85 | 2814 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &memcg); |
83aae4c7 | 2815 | if (!ret) |
c0ff4b85 | 2816 | __mem_cgroup_commit_charge_swapin(page, memcg, |
83aae4c7 DN |
2817 | MEM_CGROUP_CHARGE_TYPE_SHMEM); |
2818 | } else | |
2819 | ret = mem_cgroup_charge_common(page, mm, gfp_mask, | |
73045c47 | 2820 | MEM_CGROUP_CHARGE_TYPE_SHMEM); |
b5a84319 | 2821 | |
b5a84319 | 2822 | return ret; |
e8589cc1 KH |
2823 | } |
2824 | ||
54595fe2 KH |
2825 | /* |
2826 | * While swap-in, try_charge -> commit or cancel, the page is locked. | |
2827 | * And when try_charge() successfully returns, one refcnt to memcg without | |
21ae2956 | 2828 | * struct page_cgroup is acquired. This refcnt will be consumed by |
54595fe2 KH |
2829 | * "commit()" or removed by "cancel()" |
2830 | */ | |
8c7c6e34 KH |
2831 | int mem_cgroup_try_charge_swapin(struct mm_struct *mm, |
2832 | struct page *page, | |
2833 | gfp_t mask, struct mem_cgroup **ptr) | |
2834 | { | |
c0ff4b85 | 2835 | struct mem_cgroup *memcg; |
54595fe2 | 2836 | int ret; |
8c7c6e34 | 2837 | |
56039efa KH |
2838 | *ptr = NULL; |
2839 | ||
f8d66542 | 2840 | if (mem_cgroup_disabled()) |
8c7c6e34 KH |
2841 | return 0; |
2842 | ||
2843 | if (!do_swap_account) | |
2844 | goto charge_cur_mm; | |
8c7c6e34 KH |
2845 | /* |
2846 | * A racing thread's fault, or swapoff, may have already updated | |
407f9c8b HD |
2847 | * the pte, and even removed page from swap cache: in those cases |
2848 | * do_swap_page()'s pte_same() test will fail; but there's also a | |
2849 | * KSM case which does need to charge the page. | |
8c7c6e34 KH |
2850 | */ |
2851 | if (!PageSwapCache(page)) | |
407f9c8b | 2852 | goto charge_cur_mm; |
c0ff4b85 R |
2853 | memcg = try_get_mem_cgroup_from_page(page); |
2854 | if (!memcg) | |
54595fe2 | 2855 | goto charge_cur_mm; |
c0ff4b85 | 2856 | *ptr = memcg; |
7ec99d62 | 2857 | ret = __mem_cgroup_try_charge(NULL, mask, 1, ptr, true); |
c0ff4b85 | 2858 | css_put(&memcg->css); |
54595fe2 | 2859 | return ret; |
8c7c6e34 KH |
2860 | charge_cur_mm: |
2861 | if (unlikely(!mm)) | |
2862 | mm = &init_mm; | |
7ec99d62 | 2863 | return __mem_cgroup_try_charge(mm, mask, 1, ptr, true); |
8c7c6e34 KH |
2864 | } |
2865 | ||
83aae4c7 DN |
2866 | static void |
2867 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, | |
2868 | enum charge_type ctype) | |
7a81b88c | 2869 | { |
f8d66542 | 2870 | if (mem_cgroup_disabled()) |
7a81b88c KH |
2871 | return; |
2872 | if (!ptr) | |
2873 | return; | |
88703267 | 2874 | cgroup_exclude_rmdir(&ptr->css); |
5a6475a4 KH |
2875 | |
2876 | __mem_cgroup_commit_charge_lrucare(page, ptr, ctype); | |
8c7c6e34 KH |
2877 | /* |
2878 | * Now swap is on-memory. This means this page may be | |
2879 | * counted both as mem and swap....double count. | |
03f3c433 KH |
2880 | * Fix it by uncharging from memsw. Basically, this SwapCache is stable |
2881 | * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page() | |
2882 | * may call delete_from_swap_cache() before reach here. | |
8c7c6e34 | 2883 | */ |
03f3c433 | 2884 | if (do_swap_account && PageSwapCache(page)) { |
8c7c6e34 | 2885 | swp_entry_t ent = {.val = page_private(page)}; |
a3b2d692 | 2886 | unsigned short id; |
8c7c6e34 | 2887 | struct mem_cgroup *memcg; |
a3b2d692 KH |
2888 | |
2889 | id = swap_cgroup_record(ent, 0); | |
2890 | rcu_read_lock(); | |
2891 | memcg = mem_cgroup_lookup(id); | |
8c7c6e34 | 2892 | if (memcg) { |
a3b2d692 KH |
2893 | /* |
2894 | * This recorded memcg can be obsolete one. So, avoid | |
2895 | * calling css_tryget | |
2896 | */ | |
0c3e73e8 | 2897 | if (!mem_cgroup_is_root(memcg)) |
4e649152 | 2898 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
0c3e73e8 | 2899 | mem_cgroup_swap_statistics(memcg, false); |
8c7c6e34 KH |
2900 | mem_cgroup_put(memcg); |
2901 | } | |
a3b2d692 | 2902 | rcu_read_unlock(); |
8c7c6e34 | 2903 | } |
88703267 KH |
2904 | /* |
2905 | * At swapin, we may charge account against cgroup which has no tasks. | |
2906 | * So, rmdir()->pre_destroy() can be called while we do this charge. | |
2907 | * In that case, we need to call pre_destroy() again. check it here. | |
2908 | */ | |
2909 | cgroup_release_and_wakeup_rmdir(&ptr->css); | |
7a81b88c KH |
2910 | } |
2911 | ||
83aae4c7 DN |
2912 | void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr) |
2913 | { | |
2914 | __mem_cgroup_commit_charge_swapin(page, ptr, | |
2915 | MEM_CGROUP_CHARGE_TYPE_MAPPED); | |
2916 | } | |
2917 | ||
c0ff4b85 | 2918 | void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg) |
7a81b88c | 2919 | { |
f8d66542 | 2920 | if (mem_cgroup_disabled()) |
7a81b88c | 2921 | return; |
c0ff4b85 | 2922 | if (!memcg) |
7a81b88c | 2923 | return; |
c0ff4b85 | 2924 | __mem_cgroup_cancel_charge(memcg, 1); |
7a81b88c KH |
2925 | } |
2926 | ||
c0ff4b85 | 2927 | static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg, |
7ec99d62 JW |
2928 | unsigned int nr_pages, |
2929 | const enum charge_type ctype) | |
569b846d KH |
2930 | { |
2931 | struct memcg_batch_info *batch = NULL; | |
2932 | bool uncharge_memsw = true; | |
7ec99d62 | 2933 | |
569b846d KH |
2934 | /* If swapout, usage of swap doesn't decrease */ |
2935 | if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) | |
2936 | uncharge_memsw = false; | |
569b846d KH |
2937 | |
2938 | batch = ¤t->memcg_batch; | |
2939 | /* | |
2940 | * In usual, we do css_get() when we remember memcg pointer. | |
2941 | * But in this case, we keep res->usage until end of a series of | |
2942 | * uncharges. Then, it's ok to ignore memcg's refcnt. | |
2943 | */ | |
2944 | if (!batch->memcg) | |
c0ff4b85 | 2945 | batch->memcg = memcg; |
3c11ecf4 KH |
2946 | /* |
2947 | * do_batch > 0 when unmapping pages or inode invalidate/truncate. | |
25985edc | 2948 | * In those cases, all pages freed continuously can be expected to be in |
3c11ecf4 KH |
2949 | * the same cgroup and we have chance to coalesce uncharges. |
2950 | * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE) | |
2951 | * because we want to do uncharge as soon as possible. | |
2952 | */ | |
2953 | ||
2954 | if (!batch->do_batch || test_thread_flag(TIF_MEMDIE)) | |
2955 | goto direct_uncharge; | |
2956 | ||
7ec99d62 | 2957 | if (nr_pages > 1) |
ec168510 AA |
2958 | goto direct_uncharge; |
2959 | ||
569b846d KH |
2960 | /* |
2961 | * In typical case, batch->memcg == mem. This means we can | |
2962 | * merge a series of uncharges to an uncharge of res_counter. | |
2963 | * If not, we uncharge res_counter ony by one. | |
2964 | */ | |
c0ff4b85 | 2965 | if (batch->memcg != memcg) |
569b846d KH |
2966 | goto direct_uncharge; |
2967 | /* remember freed charge and uncharge it later */ | |
7ffd4ca7 | 2968 | batch->nr_pages++; |
569b846d | 2969 | if (uncharge_memsw) |
7ffd4ca7 | 2970 | batch->memsw_nr_pages++; |
569b846d KH |
2971 | return; |
2972 | direct_uncharge: | |
c0ff4b85 | 2973 | res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE); |
569b846d | 2974 | if (uncharge_memsw) |
c0ff4b85 R |
2975 | res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE); |
2976 | if (unlikely(batch->memcg != memcg)) | |
2977 | memcg_oom_recover(memcg); | |
569b846d KH |
2978 | return; |
2979 | } | |
7a81b88c | 2980 | |
8a9f3ccd | 2981 | /* |
69029cd5 | 2982 | * uncharge if !page_mapped(page) |
8a9f3ccd | 2983 | */ |
8c7c6e34 | 2984 | static struct mem_cgroup * |
69029cd5 | 2985 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype) |
8a9f3ccd | 2986 | { |
c0ff4b85 | 2987 | struct mem_cgroup *memcg = NULL; |
7ec99d62 JW |
2988 | unsigned int nr_pages = 1; |
2989 | struct page_cgroup *pc; | |
8a9f3ccd | 2990 | |
f8d66542 | 2991 | if (mem_cgroup_disabled()) |
8c7c6e34 | 2992 | return NULL; |
4077960e | 2993 | |
d13d1443 | 2994 | if (PageSwapCache(page)) |
8c7c6e34 | 2995 | return NULL; |
d13d1443 | 2996 | |
37c2ac78 | 2997 | if (PageTransHuge(page)) { |
7ec99d62 | 2998 | nr_pages <<= compound_order(page); |
37c2ac78 AA |
2999 | VM_BUG_ON(!PageTransHuge(page)); |
3000 | } | |
8697d331 | 3001 | /* |
3c541e14 | 3002 | * Check if our page_cgroup is valid |
8697d331 | 3003 | */ |
52d4b9ac KH |
3004 | pc = lookup_page_cgroup(page); |
3005 | if (unlikely(!pc || !PageCgroupUsed(pc))) | |
8c7c6e34 | 3006 | return NULL; |
b9c565d5 | 3007 | |
52d4b9ac | 3008 | lock_page_cgroup(pc); |
d13d1443 | 3009 | |
c0ff4b85 | 3010 | memcg = pc->mem_cgroup; |
8c7c6e34 | 3011 | |
d13d1443 KH |
3012 | if (!PageCgroupUsed(pc)) |
3013 | goto unlock_out; | |
3014 | ||
3015 | switch (ctype) { | |
3016 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: | |
8a9478ca | 3017 | case MEM_CGROUP_CHARGE_TYPE_DROP: |
ac39cf8c | 3018 | /* See mem_cgroup_prepare_migration() */ |
3019 | if (page_mapped(page) || PageCgroupMigration(pc)) | |
d13d1443 KH |
3020 | goto unlock_out; |
3021 | break; | |
3022 | case MEM_CGROUP_CHARGE_TYPE_SWAPOUT: | |
3023 | if (!PageAnon(page)) { /* Shared memory */ | |
3024 | if (page->mapping && !page_is_file_cache(page)) | |
3025 | goto unlock_out; | |
3026 | } else if (page_mapped(page)) /* Anon */ | |
3027 | goto unlock_out; | |
3028 | break; | |
3029 | default: | |
3030 | break; | |
52d4b9ac | 3031 | } |
d13d1443 | 3032 | |
c0ff4b85 | 3033 | mem_cgroup_charge_statistics(memcg, PageCgroupCache(pc), -nr_pages); |
04046e1a | 3034 | |
52d4b9ac | 3035 | ClearPageCgroupUsed(pc); |
544122e5 KH |
3036 | /* |
3037 | * pc->mem_cgroup is not cleared here. It will be accessed when it's | |
3038 | * freed from LRU. This is safe because uncharged page is expected not | |
3039 | * to be reused (freed soon). Exception is SwapCache, it's handled by | |
3040 | * special functions. | |
3041 | */ | |
b9c565d5 | 3042 | |
52d4b9ac | 3043 | unlock_page_cgroup(pc); |
f75ca962 | 3044 | /* |
c0ff4b85 | 3045 | * even after unlock, we have memcg->res.usage here and this memcg |
f75ca962 KH |
3046 | * will never be freed. |
3047 | */ | |
c0ff4b85 | 3048 | memcg_check_events(memcg, page); |
f75ca962 | 3049 | if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) { |
c0ff4b85 R |
3050 | mem_cgroup_swap_statistics(memcg, true); |
3051 | mem_cgroup_get(memcg); | |
f75ca962 | 3052 | } |
c0ff4b85 R |
3053 | if (!mem_cgroup_is_root(memcg)) |
3054 | mem_cgroup_do_uncharge(memcg, nr_pages, ctype); | |
6d12e2d8 | 3055 | |
c0ff4b85 | 3056 | return memcg; |
d13d1443 KH |
3057 | |
3058 | unlock_out: | |
3059 | unlock_page_cgroup(pc); | |
8c7c6e34 | 3060 | return NULL; |
3c541e14 BS |
3061 | } |
3062 | ||
69029cd5 KH |
3063 | void mem_cgroup_uncharge_page(struct page *page) |
3064 | { | |
52d4b9ac KH |
3065 | /* early check. */ |
3066 | if (page_mapped(page)) | |
3067 | return; | |
3068 | if (page->mapping && !PageAnon(page)) | |
3069 | return; | |
69029cd5 KH |
3070 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
3071 | } | |
3072 | ||
3073 | void mem_cgroup_uncharge_cache_page(struct page *page) | |
3074 | { | |
3075 | VM_BUG_ON(page_mapped(page)); | |
b7abea96 | 3076 | VM_BUG_ON(page->mapping); |
69029cd5 KH |
3077 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE); |
3078 | } | |
3079 | ||
569b846d KH |
3080 | /* |
3081 | * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate. | |
3082 | * In that cases, pages are freed continuously and we can expect pages | |
3083 | * are in the same memcg. All these calls itself limits the number of | |
3084 | * pages freed at once, then uncharge_start/end() is called properly. | |
3085 | * This may be called prural(2) times in a context, | |
3086 | */ | |
3087 | ||
3088 | void mem_cgroup_uncharge_start(void) | |
3089 | { | |
3090 | current->memcg_batch.do_batch++; | |
3091 | /* We can do nest. */ | |
3092 | if (current->memcg_batch.do_batch == 1) { | |
3093 | current->memcg_batch.memcg = NULL; | |
7ffd4ca7 JW |
3094 | current->memcg_batch.nr_pages = 0; |
3095 | current->memcg_batch.memsw_nr_pages = 0; | |
569b846d KH |
3096 | } |
3097 | } | |
3098 | ||
3099 | void mem_cgroup_uncharge_end(void) | |
3100 | { | |
3101 | struct memcg_batch_info *batch = ¤t->memcg_batch; | |
3102 | ||
3103 | if (!batch->do_batch) | |
3104 | return; | |
3105 | ||
3106 | batch->do_batch--; | |
3107 | if (batch->do_batch) /* If stacked, do nothing. */ | |
3108 | return; | |
3109 | ||
3110 | if (!batch->memcg) | |
3111 | return; | |
3112 | /* | |
3113 | * This "batch->memcg" is valid without any css_get/put etc... | |
3114 | * bacause we hide charges behind us. | |
3115 | */ | |
7ffd4ca7 JW |
3116 | if (batch->nr_pages) |
3117 | res_counter_uncharge(&batch->memcg->res, | |
3118 | batch->nr_pages * PAGE_SIZE); | |
3119 | if (batch->memsw_nr_pages) | |
3120 | res_counter_uncharge(&batch->memcg->memsw, | |
3121 | batch->memsw_nr_pages * PAGE_SIZE); | |
3c11ecf4 | 3122 | memcg_oom_recover(batch->memcg); |
569b846d KH |
3123 | /* forget this pointer (for sanity check) */ |
3124 | batch->memcg = NULL; | |
3125 | } | |
3126 | ||
e767e056 | 3127 | #ifdef CONFIG_SWAP |
8c7c6e34 | 3128 | /* |
e767e056 | 3129 | * called after __delete_from_swap_cache() and drop "page" account. |
8c7c6e34 KH |
3130 | * memcg information is recorded to swap_cgroup of "ent" |
3131 | */ | |
8a9478ca KH |
3132 | void |
3133 | mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout) | |
8c7c6e34 KH |
3134 | { |
3135 | struct mem_cgroup *memcg; | |
8a9478ca KH |
3136 | int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT; |
3137 | ||
3138 | if (!swapout) /* this was a swap cache but the swap is unused ! */ | |
3139 | ctype = MEM_CGROUP_CHARGE_TYPE_DROP; | |
3140 | ||
3141 | memcg = __mem_cgroup_uncharge_common(page, ctype); | |
8c7c6e34 | 3142 | |
f75ca962 KH |
3143 | /* |
3144 | * record memcg information, if swapout && memcg != NULL, | |
3145 | * mem_cgroup_get() was called in uncharge(). | |
3146 | */ | |
3147 | if (do_swap_account && swapout && memcg) | |
a3b2d692 | 3148 | swap_cgroup_record(ent, css_id(&memcg->css)); |
8c7c6e34 | 3149 | } |
e767e056 | 3150 | #endif |
8c7c6e34 KH |
3151 | |
3152 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP | |
3153 | /* | |
3154 | * called from swap_entry_free(). remove record in swap_cgroup and | |
3155 | * uncharge "memsw" account. | |
3156 | */ | |
3157 | void mem_cgroup_uncharge_swap(swp_entry_t ent) | |
d13d1443 | 3158 | { |
8c7c6e34 | 3159 | struct mem_cgroup *memcg; |
a3b2d692 | 3160 | unsigned short id; |
8c7c6e34 KH |
3161 | |
3162 | if (!do_swap_account) | |
3163 | return; | |
3164 | ||
a3b2d692 KH |
3165 | id = swap_cgroup_record(ent, 0); |
3166 | rcu_read_lock(); | |
3167 | memcg = mem_cgroup_lookup(id); | |
8c7c6e34 | 3168 | if (memcg) { |
a3b2d692 KH |
3169 | /* |
3170 | * We uncharge this because swap is freed. | |
3171 | * This memcg can be obsolete one. We avoid calling css_tryget | |
3172 | */ | |
0c3e73e8 | 3173 | if (!mem_cgroup_is_root(memcg)) |
4e649152 | 3174 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
0c3e73e8 | 3175 | mem_cgroup_swap_statistics(memcg, false); |
8c7c6e34 KH |
3176 | mem_cgroup_put(memcg); |
3177 | } | |
a3b2d692 | 3178 | rcu_read_unlock(); |
d13d1443 | 3179 | } |
02491447 DN |
3180 | |
3181 | /** | |
3182 | * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record. | |
3183 | * @entry: swap entry to be moved | |
3184 | * @from: mem_cgroup which the entry is moved from | |
3185 | * @to: mem_cgroup which the entry is moved to | |
483c30b5 | 3186 | * @need_fixup: whether we should fixup res_counters and refcounts. |
02491447 DN |
3187 | * |
3188 | * It succeeds only when the swap_cgroup's record for this entry is the same | |
3189 | * as the mem_cgroup's id of @from. | |
3190 | * | |
3191 | * Returns 0 on success, -EINVAL on failure. | |
3192 | * | |
3193 | * The caller must have charged to @to, IOW, called res_counter_charge() about | |
3194 | * both res and memsw, and called css_get(). | |
3195 | */ | |
3196 | static int mem_cgroup_move_swap_account(swp_entry_t entry, | |
483c30b5 | 3197 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
02491447 DN |
3198 | { |
3199 | unsigned short old_id, new_id; | |
3200 | ||
3201 | old_id = css_id(&from->css); | |
3202 | new_id = css_id(&to->css); | |
3203 | ||
3204 | if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) { | |
02491447 | 3205 | mem_cgroup_swap_statistics(from, false); |
483c30b5 | 3206 | mem_cgroup_swap_statistics(to, true); |
02491447 | 3207 | /* |
483c30b5 DN |
3208 | * This function is only called from task migration context now. |
3209 | * It postpones res_counter and refcount handling till the end | |
3210 | * of task migration(mem_cgroup_clear_mc()) for performance | |
3211 | * improvement. But we cannot postpone mem_cgroup_get(to) | |
3212 | * because if the process that has been moved to @to does | |
3213 | * swap-in, the refcount of @to might be decreased to 0. | |
02491447 | 3214 | */ |
02491447 | 3215 | mem_cgroup_get(to); |
483c30b5 DN |
3216 | if (need_fixup) { |
3217 | if (!mem_cgroup_is_root(from)) | |
3218 | res_counter_uncharge(&from->memsw, PAGE_SIZE); | |
3219 | mem_cgroup_put(from); | |
3220 | /* | |
3221 | * we charged both to->res and to->memsw, so we should | |
3222 | * uncharge to->res. | |
3223 | */ | |
3224 | if (!mem_cgroup_is_root(to)) | |
3225 | res_counter_uncharge(&to->res, PAGE_SIZE); | |
483c30b5 | 3226 | } |
02491447 DN |
3227 | return 0; |
3228 | } | |
3229 | return -EINVAL; | |
3230 | } | |
3231 | #else | |
3232 | static inline int mem_cgroup_move_swap_account(swp_entry_t entry, | |
483c30b5 | 3233 | struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup) |
02491447 DN |
3234 | { |
3235 | return -EINVAL; | |
3236 | } | |
8c7c6e34 | 3237 | #endif |
d13d1443 | 3238 | |
ae41be37 | 3239 | /* |
01b1ae63 KH |
3240 | * Before starting migration, account PAGE_SIZE to mem_cgroup that the old |
3241 | * page belongs to. | |
ae41be37 | 3242 | */ |
ac39cf8c | 3243 | int mem_cgroup_prepare_migration(struct page *page, |
ef6a3c63 | 3244 | struct page *newpage, struct mem_cgroup **ptr, gfp_t gfp_mask) |
ae41be37 | 3245 | { |
c0ff4b85 | 3246 | struct mem_cgroup *memcg = NULL; |
7ec99d62 | 3247 | struct page_cgroup *pc; |
ac39cf8c | 3248 | enum charge_type ctype; |
e8589cc1 | 3249 | int ret = 0; |
8869b8f6 | 3250 | |
56039efa KH |
3251 | *ptr = NULL; |
3252 | ||
ec168510 | 3253 | VM_BUG_ON(PageTransHuge(page)); |
f8d66542 | 3254 | if (mem_cgroup_disabled()) |
4077960e BS |
3255 | return 0; |
3256 | ||
52d4b9ac KH |
3257 | pc = lookup_page_cgroup(page); |
3258 | lock_page_cgroup(pc); | |
3259 | if (PageCgroupUsed(pc)) { | |
c0ff4b85 R |
3260 | memcg = pc->mem_cgroup; |
3261 | css_get(&memcg->css); | |
ac39cf8c | 3262 | /* |
3263 | * At migrating an anonymous page, its mapcount goes down | |
3264 | * to 0 and uncharge() will be called. But, even if it's fully | |
3265 | * unmapped, migration may fail and this page has to be | |
3266 | * charged again. We set MIGRATION flag here and delay uncharge | |
3267 | * until end_migration() is called | |
3268 | * | |
3269 | * Corner Case Thinking | |
3270 | * A) | |
3271 | * When the old page was mapped as Anon and it's unmap-and-freed | |
3272 | * while migration was ongoing. | |
3273 | * If unmap finds the old page, uncharge() of it will be delayed | |
3274 | * until end_migration(). If unmap finds a new page, it's | |
3275 | * uncharged when it make mapcount to be 1->0. If unmap code | |
3276 | * finds swap_migration_entry, the new page will not be mapped | |
3277 | * and end_migration() will find it(mapcount==0). | |
3278 | * | |
3279 | * B) | |
3280 | * When the old page was mapped but migraion fails, the kernel | |
3281 | * remaps it. A charge for it is kept by MIGRATION flag even | |
3282 | * if mapcount goes down to 0. We can do remap successfully | |
3283 | * without charging it again. | |
3284 | * | |
3285 | * C) | |
3286 | * The "old" page is under lock_page() until the end of | |
3287 | * migration, so, the old page itself will not be swapped-out. | |
3288 | * If the new page is swapped out before end_migraton, our | |
3289 | * hook to usual swap-out path will catch the event. | |
3290 | */ | |
3291 | if (PageAnon(page)) | |
3292 | SetPageCgroupMigration(pc); | |
e8589cc1 | 3293 | } |
52d4b9ac | 3294 | unlock_page_cgroup(pc); |
ac39cf8c | 3295 | /* |
3296 | * If the page is not charged at this point, | |
3297 | * we return here. | |
3298 | */ | |
c0ff4b85 | 3299 | if (!memcg) |
ac39cf8c | 3300 | return 0; |
01b1ae63 | 3301 | |
c0ff4b85 | 3302 | *ptr = memcg; |
7ec99d62 | 3303 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, 1, ptr, false); |
c0ff4b85 | 3304 | css_put(&memcg->css);/* drop extra refcnt */ |
ac39cf8c | 3305 | if (ret || *ptr == NULL) { |
3306 | if (PageAnon(page)) { | |
3307 | lock_page_cgroup(pc); | |
3308 | ClearPageCgroupMigration(pc); | |
3309 | unlock_page_cgroup(pc); | |
3310 | /* | |
3311 | * The old page may be fully unmapped while we kept it. | |
3312 | */ | |
3313 | mem_cgroup_uncharge_page(page); | |
3314 | } | |
3315 | return -ENOMEM; | |
e8589cc1 | 3316 | } |
ac39cf8c | 3317 | /* |
3318 | * We charge new page before it's used/mapped. So, even if unlock_page() | |
3319 | * is called before end_migration, we can catch all events on this new | |
3320 | * page. In the case new page is migrated but not remapped, new page's | |
3321 | * mapcount will be finally 0 and we call uncharge in end_migration(). | |
3322 | */ | |
3323 | pc = lookup_page_cgroup(newpage); | |
3324 | if (PageAnon(page)) | |
3325 | ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED; | |
3326 | else if (page_is_file_cache(page)) | |
3327 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; | |
3328 | else | |
3329 | ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM; | |
c0ff4b85 | 3330 | __mem_cgroup_commit_charge(memcg, page, 1, pc, ctype); |
e8589cc1 | 3331 | return ret; |
ae41be37 | 3332 | } |
8869b8f6 | 3333 | |
69029cd5 | 3334 | /* remove redundant charge if migration failed*/ |
c0ff4b85 | 3335 | void mem_cgroup_end_migration(struct mem_cgroup *memcg, |
50de1dd9 | 3336 | struct page *oldpage, struct page *newpage, bool migration_ok) |
ae41be37 | 3337 | { |
ac39cf8c | 3338 | struct page *used, *unused; |
01b1ae63 | 3339 | struct page_cgroup *pc; |
01b1ae63 | 3340 | |
c0ff4b85 | 3341 | if (!memcg) |
01b1ae63 | 3342 | return; |
ac39cf8c | 3343 | /* blocks rmdir() */ |
c0ff4b85 | 3344 | cgroup_exclude_rmdir(&memcg->css); |
50de1dd9 | 3345 | if (!migration_ok) { |
ac39cf8c | 3346 | used = oldpage; |
3347 | unused = newpage; | |
01b1ae63 | 3348 | } else { |
ac39cf8c | 3349 | used = newpage; |
01b1ae63 KH |
3350 | unused = oldpage; |
3351 | } | |
69029cd5 | 3352 | /* |
ac39cf8c | 3353 | * We disallowed uncharge of pages under migration because mapcount |
3354 | * of the page goes down to zero, temporarly. | |
3355 | * Clear the flag and check the page should be charged. | |
01b1ae63 | 3356 | */ |
ac39cf8c | 3357 | pc = lookup_page_cgroup(oldpage); |
3358 | lock_page_cgroup(pc); | |
3359 | ClearPageCgroupMigration(pc); | |
3360 | unlock_page_cgroup(pc); | |
01b1ae63 | 3361 | |
ac39cf8c | 3362 | __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE); |
3363 | ||
01b1ae63 | 3364 | /* |
ac39cf8c | 3365 | * If a page is a file cache, radix-tree replacement is very atomic |
3366 | * and we can skip this check. When it was an Anon page, its mapcount | |
3367 | * goes down to 0. But because we added MIGRATION flage, it's not | |
3368 | * uncharged yet. There are several case but page->mapcount check | |
3369 | * and USED bit check in mem_cgroup_uncharge_page() will do enough | |
3370 | * check. (see prepare_charge() also) | |
69029cd5 | 3371 | */ |
ac39cf8c | 3372 | if (PageAnon(used)) |
3373 | mem_cgroup_uncharge_page(used); | |
88703267 | 3374 | /* |
ac39cf8c | 3375 | * At migration, we may charge account against cgroup which has no |
3376 | * tasks. | |
88703267 KH |
3377 | * So, rmdir()->pre_destroy() can be called while we do this charge. |
3378 | * In that case, we need to call pre_destroy() again. check it here. | |
3379 | */ | |
c0ff4b85 | 3380 | cgroup_release_and_wakeup_rmdir(&memcg->css); |
ae41be37 | 3381 | } |
78fb7466 | 3382 | |
f212ad7c DN |
3383 | #ifdef CONFIG_DEBUG_VM |
3384 | static struct page_cgroup *lookup_page_cgroup_used(struct page *page) | |
3385 | { | |
3386 | struct page_cgroup *pc; | |
3387 | ||
3388 | pc = lookup_page_cgroup(page); | |
3389 | if (likely(pc) && PageCgroupUsed(pc)) | |
3390 | return pc; | |
3391 | return NULL; | |
3392 | } | |
3393 | ||
3394 | bool mem_cgroup_bad_page_check(struct page *page) | |
3395 | { | |
3396 | if (mem_cgroup_disabled()) | |
3397 | return false; | |
3398 | ||
3399 | return lookup_page_cgroup_used(page) != NULL; | |
3400 | } | |
3401 | ||
3402 | void mem_cgroup_print_bad_page(struct page *page) | |
3403 | { | |
3404 | struct page_cgroup *pc; | |
3405 | ||
3406 | pc = lookup_page_cgroup_used(page); | |
3407 | if (pc) { | |
3408 | int ret = -1; | |
3409 | char *path; | |
3410 | ||
3411 | printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p", | |
3412 | pc, pc->flags, pc->mem_cgroup); | |
3413 | ||
3414 | path = kmalloc(PATH_MAX, GFP_KERNEL); | |
3415 | if (path) { | |
3416 | rcu_read_lock(); | |
3417 | ret = cgroup_path(pc->mem_cgroup->css.cgroup, | |
3418 | path, PATH_MAX); | |
3419 | rcu_read_unlock(); | |
3420 | } | |
3421 | ||
3422 | printk(KERN_CONT "(%s)\n", | |
3423 | (ret < 0) ? "cannot get the path" : path); | |
3424 | kfree(path); | |
3425 | } | |
3426 | } | |
3427 | #endif | |
3428 | ||
8c7c6e34 KH |
3429 | static DEFINE_MUTEX(set_limit_mutex); |
3430 | ||
d38d2a75 | 3431 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
8c7c6e34 | 3432 | unsigned long long val) |
628f4235 | 3433 | { |
81d39c20 | 3434 | int retry_count; |
3c11ecf4 | 3435 | u64 memswlimit, memlimit; |
628f4235 | 3436 | int ret = 0; |
81d39c20 KH |
3437 | int children = mem_cgroup_count_children(memcg); |
3438 | u64 curusage, oldusage; | |
3c11ecf4 | 3439 | int enlarge; |
81d39c20 KH |
3440 | |
3441 | /* | |
3442 | * For keeping hierarchical_reclaim simple, how long we should retry | |
3443 | * is depends on callers. We set our retry-count to be function | |
3444 | * of # of children which we should visit in this loop. | |
3445 | */ | |
3446 | retry_count = MEM_CGROUP_RECLAIM_RETRIES * children; | |
3447 | ||
3448 | oldusage = res_counter_read_u64(&memcg->res, RES_USAGE); | |
628f4235 | 3449 | |
3c11ecf4 | 3450 | enlarge = 0; |
8c7c6e34 | 3451 | while (retry_count) { |
628f4235 KH |
3452 | if (signal_pending(current)) { |
3453 | ret = -EINTR; | |
3454 | break; | |
3455 | } | |
8c7c6e34 KH |
3456 | /* |
3457 | * Rather than hide all in some function, I do this in | |
3458 | * open coded manner. You see what this really does. | |
c0ff4b85 | 3459 | * We have to guarantee memcg->res.limit < memcg->memsw.limit. |
8c7c6e34 KH |
3460 | */ |
3461 | mutex_lock(&set_limit_mutex); | |
3462 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); | |
3463 | if (memswlimit < val) { | |
3464 | ret = -EINVAL; | |
3465 | mutex_unlock(&set_limit_mutex); | |
628f4235 KH |
3466 | break; |
3467 | } | |
3c11ecf4 KH |
3468 | |
3469 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); | |
3470 | if (memlimit < val) | |
3471 | enlarge = 1; | |
3472 | ||
8c7c6e34 | 3473 | ret = res_counter_set_limit(&memcg->res, val); |
22a668d7 KH |
3474 | if (!ret) { |
3475 | if (memswlimit == val) | |
3476 | memcg->memsw_is_minimum = true; | |
3477 | else | |
3478 | memcg->memsw_is_minimum = false; | |
3479 | } | |
8c7c6e34 KH |
3480 | mutex_unlock(&set_limit_mutex); |
3481 | ||
3482 | if (!ret) | |
3483 | break; | |
3484 | ||
aa20d489 | 3485 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
0ae5e89c YH |
3486 | MEM_CGROUP_RECLAIM_SHRINK, |
3487 | NULL); | |
81d39c20 KH |
3488 | curusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
3489 | /* Usage is reduced ? */ | |
3490 | if (curusage >= oldusage) | |
3491 | retry_count--; | |
3492 | else | |
3493 | oldusage = curusage; | |
8c7c6e34 | 3494 | } |
3c11ecf4 KH |
3495 | if (!ret && enlarge) |
3496 | memcg_oom_recover(memcg); | |
14797e23 | 3497 | |
8c7c6e34 KH |
3498 | return ret; |
3499 | } | |
3500 | ||
338c8431 LZ |
3501 | static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg, |
3502 | unsigned long long val) | |
8c7c6e34 | 3503 | { |
81d39c20 | 3504 | int retry_count; |
3c11ecf4 | 3505 | u64 memlimit, memswlimit, oldusage, curusage; |
81d39c20 KH |
3506 | int children = mem_cgroup_count_children(memcg); |
3507 | int ret = -EBUSY; | |
3c11ecf4 | 3508 | int enlarge = 0; |
8c7c6e34 | 3509 | |
81d39c20 KH |
3510 | /* see mem_cgroup_resize_res_limit */ |
3511 | retry_count = children * MEM_CGROUP_RECLAIM_RETRIES; | |
3512 | oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); | |
8c7c6e34 KH |
3513 | while (retry_count) { |
3514 | if (signal_pending(current)) { | |
3515 | ret = -EINTR; | |
3516 | break; | |
3517 | } | |
3518 | /* | |
3519 | * Rather than hide all in some function, I do this in | |
3520 | * open coded manner. You see what this really does. | |
c0ff4b85 | 3521 | * We have to guarantee memcg->res.limit < memcg->memsw.limit. |
8c7c6e34 KH |
3522 | */ |
3523 | mutex_lock(&set_limit_mutex); | |
3524 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); | |
3525 | if (memlimit > val) { | |
3526 | ret = -EINVAL; | |
3527 | mutex_unlock(&set_limit_mutex); | |
3528 | break; | |
3529 | } | |
3c11ecf4 KH |
3530 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); |
3531 | if (memswlimit < val) | |
3532 | enlarge = 1; | |
8c7c6e34 | 3533 | ret = res_counter_set_limit(&memcg->memsw, val); |
22a668d7 KH |
3534 | if (!ret) { |
3535 | if (memlimit == val) | |
3536 | memcg->memsw_is_minimum = true; | |
3537 | else | |
3538 | memcg->memsw_is_minimum = false; | |
3539 | } | |
8c7c6e34 KH |
3540 | mutex_unlock(&set_limit_mutex); |
3541 | ||
3542 | if (!ret) | |
3543 | break; | |
3544 | ||
4e416953 | 3545 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
75822b44 | 3546 | MEM_CGROUP_RECLAIM_NOSWAP | |
0ae5e89c YH |
3547 | MEM_CGROUP_RECLAIM_SHRINK, |
3548 | NULL); | |
8c7c6e34 | 3549 | curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
81d39c20 | 3550 | /* Usage is reduced ? */ |
8c7c6e34 | 3551 | if (curusage >= oldusage) |
628f4235 | 3552 | retry_count--; |
81d39c20 KH |
3553 | else |
3554 | oldusage = curusage; | |
628f4235 | 3555 | } |
3c11ecf4 KH |
3556 | if (!ret && enlarge) |
3557 | memcg_oom_recover(memcg); | |
628f4235 KH |
3558 | return ret; |
3559 | } | |
3560 | ||
4e416953 | 3561 | unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order, |
0ae5e89c YH |
3562 | gfp_t gfp_mask, |
3563 | unsigned long *total_scanned) | |
4e416953 BS |
3564 | { |
3565 | unsigned long nr_reclaimed = 0; | |
3566 | struct mem_cgroup_per_zone *mz, *next_mz = NULL; | |
3567 | unsigned long reclaimed; | |
3568 | int loop = 0; | |
3569 | struct mem_cgroup_tree_per_zone *mctz; | |
ef8745c1 | 3570 | unsigned long long excess; |
0ae5e89c | 3571 | unsigned long nr_scanned; |
4e416953 BS |
3572 | |
3573 | if (order > 0) | |
3574 | return 0; | |
3575 | ||
00918b6a | 3576 | mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone)); |
4e416953 BS |
3577 | /* |
3578 | * This loop can run a while, specially if mem_cgroup's continuously | |
3579 | * keep exceeding their soft limit and putting the system under | |
3580 | * pressure | |
3581 | */ | |
3582 | do { | |
3583 | if (next_mz) | |
3584 | mz = next_mz; | |
3585 | else | |
3586 | mz = mem_cgroup_largest_soft_limit_node(mctz); | |
3587 | if (!mz) | |
3588 | break; | |
3589 | ||
0ae5e89c | 3590 | nr_scanned = 0; |
4e416953 BS |
3591 | reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone, |
3592 | gfp_mask, | |
0ae5e89c YH |
3593 | MEM_CGROUP_RECLAIM_SOFT, |
3594 | &nr_scanned); | |
4e416953 | 3595 | nr_reclaimed += reclaimed; |
0ae5e89c | 3596 | *total_scanned += nr_scanned; |
4e416953 BS |
3597 | spin_lock(&mctz->lock); |
3598 | ||
3599 | /* | |
3600 | * If we failed to reclaim anything from this memory cgroup | |
3601 | * it is time to move on to the next cgroup | |
3602 | */ | |
3603 | next_mz = NULL; | |
3604 | if (!reclaimed) { | |
3605 | do { | |
3606 | /* | |
3607 | * Loop until we find yet another one. | |
3608 | * | |
3609 | * By the time we get the soft_limit lock | |
3610 | * again, someone might have aded the | |
3611 | * group back on the RB tree. Iterate to | |
3612 | * make sure we get a different mem. | |
3613 | * mem_cgroup_largest_soft_limit_node returns | |
3614 | * NULL if no other cgroup is present on | |
3615 | * the tree | |
3616 | */ | |
3617 | next_mz = | |
3618 | __mem_cgroup_largest_soft_limit_node(mctz); | |
39cc98f1 | 3619 | if (next_mz == mz) |
4e416953 | 3620 | css_put(&next_mz->mem->css); |
39cc98f1 | 3621 | else /* next_mz == NULL or other memcg */ |
4e416953 BS |
3622 | break; |
3623 | } while (1); | |
3624 | } | |
4e416953 | 3625 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); |
ef8745c1 | 3626 | excess = res_counter_soft_limit_excess(&mz->mem->res); |
4e416953 BS |
3627 | /* |
3628 | * One school of thought says that we should not add | |
3629 | * back the node to the tree if reclaim returns 0. | |
3630 | * But our reclaim could return 0, simply because due | |
3631 | * to priority we are exposing a smaller subset of | |
3632 | * memory to reclaim from. Consider this as a longer | |
3633 | * term TODO. | |
3634 | */ | |
ef8745c1 KH |
3635 | /* If excess == 0, no tree ops */ |
3636 | __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess); | |
4e416953 BS |
3637 | spin_unlock(&mctz->lock); |
3638 | css_put(&mz->mem->css); | |
3639 | loop++; | |
3640 | /* | |
3641 | * Could not reclaim anything and there are no more | |
3642 | * mem cgroups to try or we seem to be looping without | |
3643 | * reclaiming anything. | |
3644 | */ | |
3645 | if (!nr_reclaimed && | |
3646 | (next_mz == NULL || | |
3647 | loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS)) | |
3648 | break; | |
3649 | } while (!nr_reclaimed); | |
3650 | if (next_mz) | |
3651 | css_put(&next_mz->mem->css); | |
3652 | return nr_reclaimed; | |
3653 | } | |
3654 | ||
cc847582 KH |
3655 | /* |
3656 | * This routine traverse page_cgroup in given list and drop them all. | |
cc847582 KH |
3657 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
3658 | */ | |
c0ff4b85 | 3659 | static int mem_cgroup_force_empty_list(struct mem_cgroup *memcg, |
08e552c6 | 3660 | int node, int zid, enum lru_list lru) |
cc847582 | 3661 | { |
08e552c6 KH |
3662 | struct zone *zone; |
3663 | struct mem_cgroup_per_zone *mz; | |
f817ed48 | 3664 | struct page_cgroup *pc, *busy; |
08e552c6 | 3665 | unsigned long flags, loop; |
072c56c1 | 3666 | struct list_head *list; |
f817ed48 | 3667 | int ret = 0; |
072c56c1 | 3668 | |
08e552c6 | 3669 | zone = &NODE_DATA(node)->node_zones[zid]; |
c0ff4b85 | 3670 | mz = mem_cgroup_zoneinfo(memcg, node, zid); |
b69408e8 | 3671 | list = &mz->lists[lru]; |
cc847582 | 3672 | |
f817ed48 KH |
3673 | loop = MEM_CGROUP_ZSTAT(mz, lru); |
3674 | /* give some margin against EBUSY etc...*/ | |
3675 | loop += 256; | |
3676 | busy = NULL; | |
3677 | while (loop--) { | |
5564e88b JW |
3678 | struct page *page; |
3679 | ||
f817ed48 | 3680 | ret = 0; |
08e552c6 | 3681 | spin_lock_irqsave(&zone->lru_lock, flags); |
f817ed48 | 3682 | if (list_empty(list)) { |
08e552c6 | 3683 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
52d4b9ac | 3684 | break; |
f817ed48 KH |
3685 | } |
3686 | pc = list_entry(list->prev, struct page_cgroup, lru); | |
3687 | if (busy == pc) { | |
3688 | list_move(&pc->lru, list); | |
648bcc77 | 3689 | busy = NULL; |
08e552c6 | 3690 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
f817ed48 KH |
3691 | continue; |
3692 | } | |
08e552c6 | 3693 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
f817ed48 | 3694 | |
6b3ae58e | 3695 | page = lookup_cgroup_page(pc); |
5564e88b | 3696 | |
c0ff4b85 | 3697 | ret = mem_cgroup_move_parent(page, pc, memcg, GFP_KERNEL); |
f817ed48 | 3698 | if (ret == -ENOMEM) |
52d4b9ac | 3699 | break; |
f817ed48 KH |
3700 | |
3701 | if (ret == -EBUSY || ret == -EINVAL) { | |
3702 | /* found lock contention or "pc" is obsolete. */ | |
3703 | busy = pc; | |
3704 | cond_resched(); | |
3705 | } else | |
3706 | busy = NULL; | |
cc847582 | 3707 | } |
08e552c6 | 3708 | |
f817ed48 KH |
3709 | if (!ret && !list_empty(list)) |
3710 | return -EBUSY; | |
3711 | return ret; | |
cc847582 KH |
3712 | } |
3713 | ||
3714 | /* | |
3715 | * make mem_cgroup's charge to be 0 if there is no task. | |
3716 | * This enables deleting this mem_cgroup. | |
3717 | */ | |
c0ff4b85 | 3718 | static int mem_cgroup_force_empty(struct mem_cgroup *memcg, bool free_all) |
cc847582 | 3719 | { |
f817ed48 KH |
3720 | int ret; |
3721 | int node, zid, shrink; | |
3722 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; | |
c0ff4b85 | 3723 | struct cgroup *cgrp = memcg->css.cgroup; |
8869b8f6 | 3724 | |
c0ff4b85 | 3725 | css_get(&memcg->css); |
f817ed48 KH |
3726 | |
3727 | shrink = 0; | |
c1e862c1 KH |
3728 | /* should free all ? */ |
3729 | if (free_all) | |
3730 | goto try_to_free; | |
f817ed48 | 3731 | move_account: |
fce66477 | 3732 | do { |
f817ed48 | 3733 | ret = -EBUSY; |
c1e862c1 KH |
3734 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children)) |
3735 | goto out; | |
3736 | ret = -EINTR; | |
3737 | if (signal_pending(current)) | |
cc847582 | 3738 | goto out; |
52d4b9ac KH |
3739 | /* This is for making all *used* pages to be on LRU. */ |
3740 | lru_add_drain_all(); | |
c0ff4b85 | 3741 | drain_all_stock_sync(memcg); |
f817ed48 | 3742 | ret = 0; |
c0ff4b85 | 3743 | mem_cgroup_start_move(memcg); |
299b4eaa | 3744 | for_each_node_state(node, N_HIGH_MEMORY) { |
f817ed48 | 3745 | for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) { |
b69408e8 | 3746 | enum lru_list l; |
f817ed48 | 3747 | for_each_lru(l) { |
c0ff4b85 | 3748 | ret = mem_cgroup_force_empty_list(memcg, |
08e552c6 | 3749 | node, zid, l); |
f817ed48 KH |
3750 | if (ret) |
3751 | break; | |
3752 | } | |
1ecaab2b | 3753 | } |
f817ed48 KH |
3754 | if (ret) |
3755 | break; | |
3756 | } | |
c0ff4b85 R |
3757 | mem_cgroup_end_move(memcg); |
3758 | memcg_oom_recover(memcg); | |
f817ed48 KH |
3759 | /* it seems parent cgroup doesn't have enough mem */ |
3760 | if (ret == -ENOMEM) | |
3761 | goto try_to_free; | |
52d4b9ac | 3762 | cond_resched(); |
fce66477 | 3763 | /* "ret" should also be checked to ensure all lists are empty. */ |
c0ff4b85 | 3764 | } while (memcg->res.usage > 0 || ret); |
cc847582 | 3765 | out: |
c0ff4b85 | 3766 | css_put(&memcg->css); |
cc847582 | 3767 | return ret; |
f817ed48 KH |
3768 | |
3769 | try_to_free: | |
c1e862c1 KH |
3770 | /* returns EBUSY if there is a task or if we come here twice. */ |
3771 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) { | |
f817ed48 KH |
3772 | ret = -EBUSY; |
3773 | goto out; | |
3774 | } | |
c1e862c1 KH |
3775 | /* we call try-to-free pages for make this cgroup empty */ |
3776 | lru_add_drain_all(); | |
f817ed48 KH |
3777 | /* try to free all pages in this cgroup */ |
3778 | shrink = 1; | |
c0ff4b85 | 3779 | while (nr_retries && memcg->res.usage > 0) { |
f817ed48 | 3780 | int progress; |
c1e862c1 KH |
3781 | |
3782 | if (signal_pending(current)) { | |
3783 | ret = -EINTR; | |
3784 | goto out; | |
3785 | } | |
c0ff4b85 | 3786 | progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL, |
185efc0f | 3787 | false); |
c1e862c1 | 3788 | if (!progress) { |
f817ed48 | 3789 | nr_retries--; |
c1e862c1 | 3790 | /* maybe some writeback is necessary */ |
8aa7e847 | 3791 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
c1e862c1 | 3792 | } |
f817ed48 KH |
3793 | |
3794 | } | |
08e552c6 | 3795 | lru_add_drain(); |
f817ed48 | 3796 | /* try move_account...there may be some *locked* pages. */ |
fce66477 | 3797 | goto move_account; |
cc847582 KH |
3798 | } |
3799 | ||
c1e862c1 KH |
3800 | int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event) |
3801 | { | |
3802 | return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true); | |
3803 | } | |
3804 | ||
3805 | ||
18f59ea7 BS |
3806 | static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft) |
3807 | { | |
3808 | return mem_cgroup_from_cont(cont)->use_hierarchy; | |
3809 | } | |
3810 | ||
3811 | static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft, | |
3812 | u64 val) | |
3813 | { | |
3814 | int retval = 0; | |
c0ff4b85 | 3815 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
18f59ea7 | 3816 | struct cgroup *parent = cont->parent; |
c0ff4b85 | 3817 | struct mem_cgroup *parent_memcg = NULL; |
18f59ea7 BS |
3818 | |
3819 | if (parent) | |
c0ff4b85 | 3820 | parent_memcg = mem_cgroup_from_cont(parent); |
18f59ea7 BS |
3821 | |
3822 | cgroup_lock(); | |
3823 | /* | |
af901ca1 | 3824 | * If parent's use_hierarchy is set, we can't make any modifications |
18f59ea7 BS |
3825 | * in the child subtrees. If it is unset, then the change can |
3826 | * occur, provided the current cgroup has no children. | |
3827 | * | |
3828 | * For the root cgroup, parent_mem is NULL, we allow value to be | |
3829 | * set if there are no children. | |
3830 | */ | |
c0ff4b85 | 3831 | if ((!parent_memcg || !parent_memcg->use_hierarchy) && |
18f59ea7 BS |
3832 | (val == 1 || val == 0)) { |
3833 | if (list_empty(&cont->children)) | |
c0ff4b85 | 3834 | memcg->use_hierarchy = val; |
18f59ea7 BS |
3835 | else |
3836 | retval = -EBUSY; | |
3837 | } else | |
3838 | retval = -EINVAL; | |
3839 | cgroup_unlock(); | |
3840 | ||
3841 | return retval; | |
3842 | } | |
3843 | ||
0c3e73e8 | 3844 | |
c0ff4b85 | 3845 | static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg, |
7a159cc9 | 3846 | enum mem_cgroup_stat_index idx) |
0c3e73e8 | 3847 | { |
7d74b06f | 3848 | struct mem_cgroup *iter; |
7a159cc9 | 3849 | long val = 0; |
0c3e73e8 | 3850 | |
7a159cc9 | 3851 | /* Per-cpu values can be negative, use a signed accumulator */ |
c0ff4b85 | 3852 | for_each_mem_cgroup_tree(iter, memcg) |
7d74b06f KH |
3853 | val += mem_cgroup_read_stat(iter, idx); |
3854 | ||
3855 | if (val < 0) /* race ? */ | |
3856 | val = 0; | |
3857 | return val; | |
0c3e73e8 BS |
3858 | } |
3859 | ||
c0ff4b85 | 3860 | static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap) |
104f3928 | 3861 | { |
7d74b06f | 3862 | u64 val; |
104f3928 | 3863 | |
c0ff4b85 | 3864 | if (!mem_cgroup_is_root(memcg)) { |
e5671dfa GC |
3865 | val = 0; |
3866 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM | |
3867 | if (!memcg->kmem_independent_accounting) | |
3868 | val = res_counter_read_u64(&memcg->kmem, RES_USAGE); | |
3869 | #endif | |
104f3928 | 3870 | if (!swap) |
e5671dfa | 3871 | val += res_counter_read_u64(&memcg->res, RES_USAGE); |
104f3928 | 3872 | else |
e5671dfa GC |
3873 | val += res_counter_read_u64(&memcg->memsw, RES_USAGE); |
3874 | ||
3875 | return val; | |
104f3928 KS |
3876 | } |
3877 | ||
c0ff4b85 R |
3878 | val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE); |
3879 | val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS); | |
104f3928 | 3880 | |
7d74b06f | 3881 | if (swap) |
c0ff4b85 | 3882 | val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAPOUT); |
104f3928 KS |
3883 | |
3884 | return val << PAGE_SHIFT; | |
3885 | } | |
3886 | ||
2c3daa72 | 3887 | static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) |
8cdea7c0 | 3888 | { |
c0ff4b85 | 3889 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
104f3928 | 3890 | u64 val; |
8c7c6e34 KH |
3891 | int type, name; |
3892 | ||
3893 | type = MEMFILE_TYPE(cft->private); | |
3894 | name = MEMFILE_ATTR(cft->private); | |
3895 | switch (type) { | |
3896 | case _MEM: | |
104f3928 | 3897 | if (name == RES_USAGE) |
c0ff4b85 | 3898 | val = mem_cgroup_usage(memcg, false); |
104f3928 | 3899 | else |
c0ff4b85 | 3900 | val = res_counter_read_u64(&memcg->res, name); |
8c7c6e34 KH |
3901 | break; |
3902 | case _MEMSWAP: | |
104f3928 | 3903 | if (name == RES_USAGE) |
c0ff4b85 | 3904 | val = mem_cgroup_usage(memcg, true); |
104f3928 | 3905 | else |
c0ff4b85 | 3906 | val = res_counter_read_u64(&memcg->memsw, name); |
8c7c6e34 | 3907 | break; |
e5671dfa GC |
3908 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM |
3909 | case _KMEM: | |
3910 | val = res_counter_read_u64(&memcg->kmem, name); | |
3911 | break; | |
3912 | #endif | |
8c7c6e34 KH |
3913 | default: |
3914 | BUG(); | |
3915 | break; | |
3916 | } | |
3917 | return val; | |
8cdea7c0 | 3918 | } |
628f4235 KH |
3919 | /* |
3920 | * The user of this function is... | |
3921 | * RES_LIMIT. | |
3922 | */ | |
856c13aa PM |
3923 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
3924 | const char *buffer) | |
8cdea7c0 | 3925 | { |
628f4235 | 3926 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
8c7c6e34 | 3927 | int type, name; |
628f4235 KH |
3928 | unsigned long long val; |
3929 | int ret; | |
3930 | ||
8c7c6e34 KH |
3931 | type = MEMFILE_TYPE(cft->private); |
3932 | name = MEMFILE_ATTR(cft->private); | |
3933 | switch (name) { | |
628f4235 | 3934 | case RES_LIMIT: |
4b3bde4c BS |
3935 | if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */ |
3936 | ret = -EINVAL; | |
3937 | break; | |
3938 | } | |
628f4235 KH |
3939 | /* This function does all necessary parse...reuse it */ |
3940 | ret = res_counter_memparse_write_strategy(buffer, &val); | |
8c7c6e34 KH |
3941 | if (ret) |
3942 | break; | |
3943 | if (type == _MEM) | |
628f4235 | 3944 | ret = mem_cgroup_resize_limit(memcg, val); |
8c7c6e34 KH |
3945 | else |
3946 | ret = mem_cgroup_resize_memsw_limit(memcg, val); | |
628f4235 | 3947 | break; |
296c81d8 BS |
3948 | case RES_SOFT_LIMIT: |
3949 | ret = res_counter_memparse_write_strategy(buffer, &val); | |
3950 | if (ret) | |
3951 | break; | |
3952 | /* | |
3953 | * For memsw, soft limits are hard to implement in terms | |
3954 | * of semantics, for now, we support soft limits for | |
3955 | * control without swap | |
3956 | */ | |
3957 | if (type == _MEM) | |
3958 | ret = res_counter_set_soft_limit(&memcg->res, val); | |
3959 | else | |
3960 | ret = -EINVAL; | |
3961 | break; | |
628f4235 KH |
3962 | default: |
3963 | ret = -EINVAL; /* should be BUG() ? */ | |
3964 | break; | |
3965 | } | |
3966 | return ret; | |
8cdea7c0 BS |
3967 | } |
3968 | ||
fee7b548 KH |
3969 | static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg, |
3970 | unsigned long long *mem_limit, unsigned long long *memsw_limit) | |
3971 | { | |
3972 | struct cgroup *cgroup; | |
3973 | unsigned long long min_limit, min_memsw_limit, tmp; | |
3974 | ||
3975 | min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT); | |
3976 | min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); | |
3977 | cgroup = memcg->css.cgroup; | |
3978 | if (!memcg->use_hierarchy) | |
3979 | goto out; | |
3980 | ||
3981 | while (cgroup->parent) { | |
3982 | cgroup = cgroup->parent; | |
3983 | memcg = mem_cgroup_from_cont(cgroup); | |
3984 | if (!memcg->use_hierarchy) | |
3985 | break; | |
3986 | tmp = res_counter_read_u64(&memcg->res, RES_LIMIT); | |
3987 | min_limit = min(min_limit, tmp); | |
3988 | tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT); | |
3989 | min_memsw_limit = min(min_memsw_limit, tmp); | |
3990 | } | |
3991 | out: | |
3992 | *mem_limit = min_limit; | |
3993 | *memsw_limit = min_memsw_limit; | |
3994 | return; | |
3995 | } | |
3996 | ||
29f2a4da | 3997 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
c84872e1 | 3998 | { |
c0ff4b85 | 3999 | struct mem_cgroup *memcg; |
8c7c6e34 | 4000 | int type, name; |
c84872e1 | 4001 | |
c0ff4b85 | 4002 | memcg = mem_cgroup_from_cont(cont); |
8c7c6e34 KH |
4003 | type = MEMFILE_TYPE(event); |
4004 | name = MEMFILE_ATTR(event); | |
4005 | switch (name) { | |
29f2a4da | 4006 | case RES_MAX_USAGE: |
8c7c6e34 | 4007 | if (type == _MEM) |
c0ff4b85 | 4008 | res_counter_reset_max(&memcg->res); |
8c7c6e34 | 4009 | else |
c0ff4b85 | 4010 | res_counter_reset_max(&memcg->memsw); |
29f2a4da PE |
4011 | break; |
4012 | case RES_FAILCNT: | |
8c7c6e34 | 4013 | if (type == _MEM) |
c0ff4b85 | 4014 | res_counter_reset_failcnt(&memcg->res); |
8c7c6e34 | 4015 | else |
c0ff4b85 | 4016 | res_counter_reset_failcnt(&memcg->memsw); |
29f2a4da PE |
4017 | break; |
4018 | } | |
f64c3f54 | 4019 | |
85cc59db | 4020 | return 0; |
c84872e1 PE |
4021 | } |
4022 | ||
7dc74be0 DN |
4023 | static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp, |
4024 | struct cftype *cft) | |
4025 | { | |
4026 | return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate; | |
4027 | } | |
4028 | ||
02491447 | 4029 | #ifdef CONFIG_MMU |
7dc74be0 DN |
4030 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, |
4031 | struct cftype *cft, u64 val) | |
4032 | { | |
c0ff4b85 | 4033 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
7dc74be0 DN |
4034 | |
4035 | if (val >= (1 << NR_MOVE_TYPE)) | |
4036 | return -EINVAL; | |
4037 | /* | |
4038 | * We check this value several times in both in can_attach() and | |
4039 | * attach(), so we need cgroup lock to prevent this value from being | |
4040 | * inconsistent. | |
4041 | */ | |
4042 | cgroup_lock(); | |
c0ff4b85 | 4043 | memcg->move_charge_at_immigrate = val; |
7dc74be0 DN |
4044 | cgroup_unlock(); |
4045 | ||
4046 | return 0; | |
4047 | } | |
02491447 DN |
4048 | #else |
4049 | static int mem_cgroup_move_charge_write(struct cgroup *cgrp, | |
4050 | struct cftype *cft, u64 val) | |
4051 | { | |
4052 | return -ENOSYS; | |
4053 | } | |
4054 | #endif | |
7dc74be0 | 4055 | |
14067bb3 KH |
4056 | |
4057 | /* For read statistics */ | |
4058 | enum { | |
4059 | MCS_CACHE, | |
4060 | MCS_RSS, | |
d8046582 | 4061 | MCS_FILE_MAPPED, |
14067bb3 KH |
4062 | MCS_PGPGIN, |
4063 | MCS_PGPGOUT, | |
1dd3a273 | 4064 | MCS_SWAP, |
456f998e YH |
4065 | MCS_PGFAULT, |
4066 | MCS_PGMAJFAULT, | |
14067bb3 KH |
4067 | MCS_INACTIVE_ANON, |
4068 | MCS_ACTIVE_ANON, | |
4069 | MCS_INACTIVE_FILE, | |
4070 | MCS_ACTIVE_FILE, | |
4071 | MCS_UNEVICTABLE, | |
4072 | NR_MCS_STAT, | |
4073 | }; | |
4074 | ||
4075 | struct mcs_total_stat { | |
4076 | s64 stat[NR_MCS_STAT]; | |
d2ceb9b7 KH |
4077 | }; |
4078 | ||
14067bb3 KH |
4079 | struct { |
4080 | char *local_name; | |
4081 | char *total_name; | |
4082 | } memcg_stat_strings[NR_MCS_STAT] = { | |
4083 | {"cache", "total_cache"}, | |
4084 | {"rss", "total_rss"}, | |
d69b042f | 4085 | {"mapped_file", "total_mapped_file"}, |
14067bb3 KH |
4086 | {"pgpgin", "total_pgpgin"}, |
4087 | {"pgpgout", "total_pgpgout"}, | |
1dd3a273 | 4088 | {"swap", "total_swap"}, |
456f998e YH |
4089 | {"pgfault", "total_pgfault"}, |
4090 | {"pgmajfault", "total_pgmajfault"}, | |
14067bb3 KH |
4091 | {"inactive_anon", "total_inactive_anon"}, |
4092 | {"active_anon", "total_active_anon"}, | |
4093 | {"inactive_file", "total_inactive_file"}, | |
4094 | {"active_file", "total_active_file"}, | |
4095 | {"unevictable", "total_unevictable"} | |
4096 | }; | |
4097 | ||
4098 | ||
7d74b06f | 4099 | static void |
c0ff4b85 | 4100 | mem_cgroup_get_local_stat(struct mem_cgroup *memcg, struct mcs_total_stat *s) |
14067bb3 | 4101 | { |
14067bb3 KH |
4102 | s64 val; |
4103 | ||
4104 | /* per cpu stat */ | |
c0ff4b85 | 4105 | val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_CACHE); |
14067bb3 | 4106 | s->stat[MCS_CACHE] += val * PAGE_SIZE; |
c0ff4b85 | 4107 | val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_RSS); |
14067bb3 | 4108 | s->stat[MCS_RSS] += val * PAGE_SIZE; |
c0ff4b85 | 4109 | val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_FILE_MAPPED); |
d8046582 | 4110 | s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE; |
c0ff4b85 | 4111 | val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGIN); |
14067bb3 | 4112 | s->stat[MCS_PGPGIN] += val; |
c0ff4b85 | 4113 | val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGPGOUT); |
14067bb3 | 4114 | s->stat[MCS_PGPGOUT] += val; |
1dd3a273 | 4115 | if (do_swap_account) { |
c0ff4b85 | 4116 | val = mem_cgroup_read_stat(memcg, MEM_CGROUP_STAT_SWAPOUT); |
1dd3a273 DN |
4117 | s->stat[MCS_SWAP] += val * PAGE_SIZE; |
4118 | } | |
c0ff4b85 | 4119 | val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGFAULT); |
456f998e | 4120 | s->stat[MCS_PGFAULT] += val; |
c0ff4b85 | 4121 | val = mem_cgroup_read_events(memcg, MEM_CGROUP_EVENTS_PGMAJFAULT); |
456f998e | 4122 | s->stat[MCS_PGMAJFAULT] += val; |
14067bb3 KH |
4123 | |
4124 | /* per zone stat */ | |
c0ff4b85 | 4125 | val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_ANON)); |
14067bb3 | 4126 | s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE; |
c0ff4b85 | 4127 | val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_ANON)); |
14067bb3 | 4128 | s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE; |
c0ff4b85 | 4129 | val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_INACTIVE_FILE)); |
14067bb3 | 4130 | s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE; |
c0ff4b85 | 4131 | val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_ACTIVE_FILE)); |
14067bb3 | 4132 | s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE; |
c0ff4b85 | 4133 | val = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE)); |
14067bb3 | 4134 | s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE; |
14067bb3 KH |
4135 | } |
4136 | ||
4137 | static void | |
c0ff4b85 | 4138 | mem_cgroup_get_total_stat(struct mem_cgroup *memcg, struct mcs_total_stat *s) |
14067bb3 | 4139 | { |
7d74b06f KH |
4140 | struct mem_cgroup *iter; |
4141 | ||
c0ff4b85 | 4142 | for_each_mem_cgroup_tree(iter, memcg) |
7d74b06f | 4143 | mem_cgroup_get_local_stat(iter, s); |
14067bb3 KH |
4144 | } |
4145 | ||
406eb0c9 YH |
4146 | #ifdef CONFIG_NUMA |
4147 | static int mem_control_numa_stat_show(struct seq_file *m, void *arg) | |
4148 | { | |
4149 | int nid; | |
4150 | unsigned long total_nr, file_nr, anon_nr, unevictable_nr; | |
4151 | unsigned long node_nr; | |
4152 | struct cgroup *cont = m->private; | |
4153 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); | |
4154 | ||
bb2a0de9 | 4155 | total_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL); |
406eb0c9 YH |
4156 | seq_printf(m, "total=%lu", total_nr); |
4157 | for_each_node_state(nid, N_HIGH_MEMORY) { | |
bb2a0de9 | 4158 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, LRU_ALL); |
406eb0c9 YH |
4159 | seq_printf(m, " N%d=%lu", nid, node_nr); |
4160 | } | |
4161 | seq_putc(m, '\n'); | |
4162 | ||
bb2a0de9 | 4163 | file_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_FILE); |
406eb0c9 YH |
4164 | seq_printf(m, "file=%lu", file_nr); |
4165 | for_each_node_state(nid, N_HIGH_MEMORY) { | |
bb2a0de9 KH |
4166 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
4167 | LRU_ALL_FILE); | |
406eb0c9 YH |
4168 | seq_printf(m, " N%d=%lu", nid, node_nr); |
4169 | } | |
4170 | seq_putc(m, '\n'); | |
4171 | ||
bb2a0de9 | 4172 | anon_nr = mem_cgroup_nr_lru_pages(mem_cont, LRU_ALL_ANON); |
406eb0c9 YH |
4173 | seq_printf(m, "anon=%lu", anon_nr); |
4174 | for_each_node_state(nid, N_HIGH_MEMORY) { | |
bb2a0de9 KH |
4175 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
4176 | LRU_ALL_ANON); | |
406eb0c9 YH |
4177 | seq_printf(m, " N%d=%lu", nid, node_nr); |
4178 | } | |
4179 | seq_putc(m, '\n'); | |
4180 | ||
bb2a0de9 | 4181 | unevictable_nr = mem_cgroup_nr_lru_pages(mem_cont, BIT(LRU_UNEVICTABLE)); |
406eb0c9 YH |
4182 | seq_printf(m, "unevictable=%lu", unevictable_nr); |
4183 | for_each_node_state(nid, N_HIGH_MEMORY) { | |
bb2a0de9 KH |
4184 | node_nr = mem_cgroup_node_nr_lru_pages(mem_cont, nid, |
4185 | BIT(LRU_UNEVICTABLE)); | |
406eb0c9 YH |
4186 | seq_printf(m, " N%d=%lu", nid, node_nr); |
4187 | } | |
4188 | seq_putc(m, '\n'); | |
4189 | return 0; | |
4190 | } | |
4191 | #endif /* CONFIG_NUMA */ | |
4192 | ||
c64745cf PM |
4193 | static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft, |
4194 | struct cgroup_map_cb *cb) | |
d2ceb9b7 | 4195 | { |
d2ceb9b7 | 4196 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
14067bb3 | 4197 | struct mcs_total_stat mystat; |
d2ceb9b7 KH |
4198 | int i; |
4199 | ||
14067bb3 KH |
4200 | memset(&mystat, 0, sizeof(mystat)); |
4201 | mem_cgroup_get_local_stat(mem_cont, &mystat); | |
d2ceb9b7 | 4202 | |
406eb0c9 | 4203 | |
1dd3a273 DN |
4204 | for (i = 0; i < NR_MCS_STAT; i++) { |
4205 | if (i == MCS_SWAP && !do_swap_account) | |
4206 | continue; | |
14067bb3 | 4207 | cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]); |
1dd3a273 | 4208 | } |
7b854121 | 4209 | |
14067bb3 | 4210 | /* Hierarchical information */ |
fee7b548 KH |
4211 | { |
4212 | unsigned long long limit, memsw_limit; | |
4213 | memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit); | |
4214 | cb->fill(cb, "hierarchical_memory_limit", limit); | |
4215 | if (do_swap_account) | |
4216 | cb->fill(cb, "hierarchical_memsw_limit", memsw_limit); | |
4217 | } | |
7f016ee8 | 4218 | |
14067bb3 KH |
4219 | memset(&mystat, 0, sizeof(mystat)); |
4220 | mem_cgroup_get_total_stat(mem_cont, &mystat); | |
1dd3a273 DN |
4221 | for (i = 0; i < NR_MCS_STAT; i++) { |
4222 | if (i == MCS_SWAP && !do_swap_account) | |
4223 | continue; | |
14067bb3 | 4224 | cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]); |
1dd3a273 | 4225 | } |
14067bb3 | 4226 | |
7f016ee8 | 4227 | #ifdef CONFIG_DEBUG_VM |
7f016ee8 KM |
4228 | { |
4229 | int nid, zid; | |
4230 | struct mem_cgroup_per_zone *mz; | |
4231 | unsigned long recent_rotated[2] = {0, 0}; | |
4232 | unsigned long recent_scanned[2] = {0, 0}; | |
4233 | ||
4234 | for_each_online_node(nid) | |
4235 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { | |
4236 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); | |
4237 | ||
4238 | recent_rotated[0] += | |
4239 | mz->reclaim_stat.recent_rotated[0]; | |
4240 | recent_rotated[1] += | |
4241 | mz->reclaim_stat.recent_rotated[1]; | |
4242 | recent_scanned[0] += | |
4243 | mz->reclaim_stat.recent_scanned[0]; | |
4244 | recent_scanned[1] += | |
4245 | mz->reclaim_stat.recent_scanned[1]; | |
4246 | } | |
4247 | cb->fill(cb, "recent_rotated_anon", recent_rotated[0]); | |
4248 | cb->fill(cb, "recent_rotated_file", recent_rotated[1]); | |
4249 | cb->fill(cb, "recent_scanned_anon", recent_scanned[0]); | |
4250 | cb->fill(cb, "recent_scanned_file", recent_scanned[1]); | |
4251 | } | |
4252 | #endif | |
4253 | ||
d2ceb9b7 KH |
4254 | return 0; |
4255 | } | |
4256 | ||
a7885eb8 KM |
4257 | static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft) |
4258 | { | |
4259 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); | |
4260 | ||
1f4c025b | 4261 | return mem_cgroup_swappiness(memcg); |
a7885eb8 KM |
4262 | } |
4263 | ||
4264 | static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, | |
4265 | u64 val) | |
4266 | { | |
4267 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); | |
4268 | struct mem_cgroup *parent; | |
068b38c1 | 4269 | |
a7885eb8 KM |
4270 | if (val > 100) |
4271 | return -EINVAL; | |
4272 | ||
4273 | if (cgrp->parent == NULL) | |
4274 | return -EINVAL; | |
4275 | ||
4276 | parent = mem_cgroup_from_cont(cgrp->parent); | |
068b38c1 LZ |
4277 | |
4278 | cgroup_lock(); | |
4279 | ||
a7885eb8 KM |
4280 | /* If under hierarchy, only empty-root can set this value */ |
4281 | if ((parent->use_hierarchy) || | |
068b38c1 LZ |
4282 | (memcg->use_hierarchy && !list_empty(&cgrp->children))) { |
4283 | cgroup_unlock(); | |
a7885eb8 | 4284 | return -EINVAL; |
068b38c1 | 4285 | } |
a7885eb8 | 4286 | |
a7885eb8 | 4287 | memcg->swappiness = val; |
a7885eb8 | 4288 | |
068b38c1 LZ |
4289 | cgroup_unlock(); |
4290 | ||
a7885eb8 KM |
4291 | return 0; |
4292 | } | |
4293 | ||
2e72b634 KS |
4294 | static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap) |
4295 | { | |
4296 | struct mem_cgroup_threshold_ary *t; | |
4297 | u64 usage; | |
4298 | int i; | |
4299 | ||
4300 | rcu_read_lock(); | |
4301 | if (!swap) | |
2c488db2 | 4302 | t = rcu_dereference(memcg->thresholds.primary); |
2e72b634 | 4303 | else |
2c488db2 | 4304 | t = rcu_dereference(memcg->memsw_thresholds.primary); |
2e72b634 KS |
4305 | |
4306 | if (!t) | |
4307 | goto unlock; | |
4308 | ||
4309 | usage = mem_cgroup_usage(memcg, swap); | |
4310 | ||
4311 | /* | |
4312 | * current_threshold points to threshold just below usage. | |
4313 | * If it's not true, a threshold was crossed after last | |
4314 | * call of __mem_cgroup_threshold(). | |
4315 | */ | |
5407a562 | 4316 | i = t->current_threshold; |
2e72b634 KS |
4317 | |
4318 | /* | |
4319 | * Iterate backward over array of thresholds starting from | |
4320 | * current_threshold and check if a threshold is crossed. | |
4321 | * If none of thresholds below usage is crossed, we read | |
4322 | * only one element of the array here. | |
4323 | */ | |
4324 | for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--) | |
4325 | eventfd_signal(t->entries[i].eventfd, 1); | |
4326 | ||
4327 | /* i = current_threshold + 1 */ | |
4328 | i++; | |
4329 | ||
4330 | /* | |
4331 | * Iterate forward over array of thresholds starting from | |
4332 | * current_threshold+1 and check if a threshold is crossed. | |
4333 | * If none of thresholds above usage is crossed, we read | |
4334 | * only one element of the array here. | |
4335 | */ | |
4336 | for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++) | |
4337 | eventfd_signal(t->entries[i].eventfd, 1); | |
4338 | ||
4339 | /* Update current_threshold */ | |
5407a562 | 4340 | t->current_threshold = i - 1; |
2e72b634 KS |
4341 | unlock: |
4342 | rcu_read_unlock(); | |
4343 | } | |
4344 | ||
4345 | static void mem_cgroup_threshold(struct mem_cgroup *memcg) | |
4346 | { | |
ad4ca5f4 KS |
4347 | while (memcg) { |
4348 | __mem_cgroup_threshold(memcg, false); | |
4349 | if (do_swap_account) | |
4350 | __mem_cgroup_threshold(memcg, true); | |
4351 | ||
4352 | memcg = parent_mem_cgroup(memcg); | |
4353 | } | |
2e72b634 KS |
4354 | } |
4355 | ||
4356 | static int compare_thresholds(const void *a, const void *b) | |
4357 | { | |
4358 | const struct mem_cgroup_threshold *_a = a; | |
4359 | const struct mem_cgroup_threshold *_b = b; | |
4360 | ||
4361 | return _a->threshold - _b->threshold; | |
4362 | } | |
4363 | ||
c0ff4b85 | 4364 | static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg) |
9490ff27 KH |
4365 | { |
4366 | struct mem_cgroup_eventfd_list *ev; | |
4367 | ||
c0ff4b85 | 4368 | list_for_each_entry(ev, &memcg->oom_notify, list) |
9490ff27 KH |
4369 | eventfd_signal(ev->eventfd, 1); |
4370 | return 0; | |
4371 | } | |
4372 | ||
c0ff4b85 | 4373 | static void mem_cgroup_oom_notify(struct mem_cgroup *memcg) |
9490ff27 | 4374 | { |
7d74b06f KH |
4375 | struct mem_cgroup *iter; |
4376 | ||
c0ff4b85 | 4377 | for_each_mem_cgroup_tree(iter, memcg) |
7d74b06f | 4378 | mem_cgroup_oom_notify_cb(iter); |
9490ff27 KH |
4379 | } |
4380 | ||
4381 | static int mem_cgroup_usage_register_event(struct cgroup *cgrp, | |
4382 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) | |
2e72b634 KS |
4383 | { |
4384 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); | |
2c488db2 KS |
4385 | struct mem_cgroup_thresholds *thresholds; |
4386 | struct mem_cgroup_threshold_ary *new; | |
2e72b634 KS |
4387 | int type = MEMFILE_TYPE(cft->private); |
4388 | u64 threshold, usage; | |
2c488db2 | 4389 | int i, size, ret; |
2e72b634 KS |
4390 | |
4391 | ret = res_counter_memparse_write_strategy(args, &threshold); | |
4392 | if (ret) | |
4393 | return ret; | |
4394 | ||
4395 | mutex_lock(&memcg->thresholds_lock); | |
2c488db2 | 4396 | |
2e72b634 | 4397 | if (type == _MEM) |
2c488db2 | 4398 | thresholds = &memcg->thresholds; |
2e72b634 | 4399 | else if (type == _MEMSWAP) |
2c488db2 | 4400 | thresholds = &memcg->memsw_thresholds; |
2e72b634 KS |
4401 | else |
4402 | BUG(); | |
4403 | ||
4404 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); | |
4405 | ||
4406 | /* Check if a threshold crossed before adding a new one */ | |
2c488db2 | 4407 | if (thresholds->primary) |
2e72b634 KS |
4408 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); |
4409 | ||
2c488db2 | 4410 | size = thresholds->primary ? thresholds->primary->size + 1 : 1; |
2e72b634 KS |
4411 | |
4412 | /* Allocate memory for new array of thresholds */ | |
2c488db2 | 4413 | new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold), |
2e72b634 | 4414 | GFP_KERNEL); |
2c488db2 | 4415 | if (!new) { |
2e72b634 KS |
4416 | ret = -ENOMEM; |
4417 | goto unlock; | |
4418 | } | |
2c488db2 | 4419 | new->size = size; |
2e72b634 KS |
4420 | |
4421 | /* Copy thresholds (if any) to new array */ | |
2c488db2 KS |
4422 | if (thresholds->primary) { |
4423 | memcpy(new->entries, thresholds->primary->entries, (size - 1) * | |
2e72b634 | 4424 | sizeof(struct mem_cgroup_threshold)); |
2c488db2 KS |
4425 | } |
4426 | ||
2e72b634 | 4427 | /* Add new threshold */ |
2c488db2 KS |
4428 | new->entries[size - 1].eventfd = eventfd; |
4429 | new->entries[size - 1].threshold = threshold; | |
2e72b634 KS |
4430 | |
4431 | /* Sort thresholds. Registering of new threshold isn't time-critical */ | |
2c488db2 | 4432 | sort(new->entries, size, sizeof(struct mem_cgroup_threshold), |
2e72b634 KS |
4433 | compare_thresholds, NULL); |
4434 | ||
4435 | /* Find current threshold */ | |
2c488db2 | 4436 | new->current_threshold = -1; |
2e72b634 | 4437 | for (i = 0; i < size; i++) { |
2c488db2 | 4438 | if (new->entries[i].threshold < usage) { |
2e72b634 | 4439 | /* |
2c488db2 KS |
4440 | * new->current_threshold will not be used until |
4441 | * rcu_assign_pointer(), so it's safe to increment | |
2e72b634 KS |
4442 | * it here. |
4443 | */ | |
2c488db2 | 4444 | ++new->current_threshold; |
2e72b634 KS |
4445 | } |
4446 | } | |
4447 | ||
2c488db2 KS |
4448 | /* Free old spare buffer and save old primary buffer as spare */ |
4449 | kfree(thresholds->spare); | |
4450 | thresholds->spare = thresholds->primary; | |
4451 | ||
4452 | rcu_assign_pointer(thresholds->primary, new); | |
2e72b634 | 4453 | |
907860ed | 4454 | /* To be sure that nobody uses thresholds */ |
2e72b634 KS |
4455 | synchronize_rcu(); |
4456 | ||
2e72b634 KS |
4457 | unlock: |
4458 | mutex_unlock(&memcg->thresholds_lock); | |
4459 | ||
4460 | return ret; | |
4461 | } | |
4462 | ||
907860ed | 4463 | static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp, |
9490ff27 | 4464 | struct cftype *cft, struct eventfd_ctx *eventfd) |
2e72b634 KS |
4465 | { |
4466 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); | |
2c488db2 KS |
4467 | struct mem_cgroup_thresholds *thresholds; |
4468 | struct mem_cgroup_threshold_ary *new; | |
2e72b634 KS |
4469 | int type = MEMFILE_TYPE(cft->private); |
4470 | u64 usage; | |
2c488db2 | 4471 | int i, j, size; |
2e72b634 KS |
4472 | |
4473 | mutex_lock(&memcg->thresholds_lock); | |
4474 | if (type == _MEM) | |
2c488db2 | 4475 | thresholds = &memcg->thresholds; |
2e72b634 | 4476 | else if (type == _MEMSWAP) |
2c488db2 | 4477 | thresholds = &memcg->memsw_thresholds; |
2e72b634 KS |
4478 | else |
4479 | BUG(); | |
4480 | ||
4481 | /* | |
4482 | * Something went wrong if we trying to unregister a threshold | |
4483 | * if we don't have thresholds | |
4484 | */ | |
4485 | BUG_ON(!thresholds); | |
4486 | ||
4487 | usage = mem_cgroup_usage(memcg, type == _MEMSWAP); | |
4488 | ||
4489 | /* Check if a threshold crossed before removing */ | |
4490 | __mem_cgroup_threshold(memcg, type == _MEMSWAP); | |
4491 | ||
4492 | /* Calculate new number of threshold */ | |
2c488db2 KS |
4493 | size = 0; |
4494 | for (i = 0; i < thresholds->primary->size; i++) { | |
4495 | if (thresholds->primary->entries[i].eventfd != eventfd) | |
2e72b634 KS |
4496 | size++; |
4497 | } | |
4498 | ||
2c488db2 | 4499 | new = thresholds->spare; |
907860ed | 4500 | |
2e72b634 KS |
4501 | /* Set thresholds array to NULL if we don't have thresholds */ |
4502 | if (!size) { | |
2c488db2 KS |
4503 | kfree(new); |
4504 | new = NULL; | |
907860ed | 4505 | goto swap_buffers; |
2e72b634 KS |
4506 | } |
4507 | ||
2c488db2 | 4508 | new->size = size; |
2e72b634 KS |
4509 | |
4510 | /* Copy thresholds and find current threshold */ | |
2c488db2 KS |
4511 | new->current_threshold = -1; |
4512 | for (i = 0, j = 0; i < thresholds->primary->size; i++) { | |
4513 | if (thresholds->primary->entries[i].eventfd == eventfd) | |
2e72b634 KS |
4514 | continue; |
4515 | ||
2c488db2 KS |
4516 | new->entries[j] = thresholds->primary->entries[i]; |
4517 | if (new->entries[j].threshold < usage) { | |
2e72b634 | 4518 | /* |
2c488db2 | 4519 | * new->current_threshold will not be used |
2e72b634 KS |
4520 | * until rcu_assign_pointer(), so it's safe to increment |
4521 | * it here. | |
4522 | */ | |
2c488db2 | 4523 | ++new->current_threshold; |
2e72b634 KS |
4524 | } |
4525 | j++; | |
4526 | } | |
4527 | ||
907860ed | 4528 | swap_buffers: |
2c488db2 KS |
4529 | /* Swap primary and spare array */ |
4530 | thresholds->spare = thresholds->primary; | |
4531 | rcu_assign_pointer(thresholds->primary, new); | |
2e72b634 | 4532 | |
907860ed | 4533 | /* To be sure that nobody uses thresholds */ |
2e72b634 KS |
4534 | synchronize_rcu(); |
4535 | ||
2e72b634 | 4536 | mutex_unlock(&memcg->thresholds_lock); |
2e72b634 | 4537 | } |
c1e862c1 | 4538 | |
9490ff27 KH |
4539 | static int mem_cgroup_oom_register_event(struct cgroup *cgrp, |
4540 | struct cftype *cft, struct eventfd_ctx *eventfd, const char *args) | |
4541 | { | |
4542 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); | |
4543 | struct mem_cgroup_eventfd_list *event; | |
4544 | int type = MEMFILE_TYPE(cft->private); | |
4545 | ||
4546 | BUG_ON(type != _OOM_TYPE); | |
4547 | event = kmalloc(sizeof(*event), GFP_KERNEL); | |
4548 | if (!event) | |
4549 | return -ENOMEM; | |
4550 | ||
1af8efe9 | 4551 | spin_lock(&memcg_oom_lock); |
9490ff27 KH |
4552 | |
4553 | event->eventfd = eventfd; | |
4554 | list_add(&event->list, &memcg->oom_notify); | |
4555 | ||
4556 | /* already in OOM ? */ | |
79dfdacc | 4557 | if (atomic_read(&memcg->under_oom)) |
9490ff27 | 4558 | eventfd_signal(eventfd, 1); |
1af8efe9 | 4559 | spin_unlock(&memcg_oom_lock); |
9490ff27 KH |
4560 | |
4561 | return 0; | |
4562 | } | |
4563 | ||
907860ed | 4564 | static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp, |
9490ff27 KH |
4565 | struct cftype *cft, struct eventfd_ctx *eventfd) |
4566 | { | |
c0ff4b85 | 4567 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
9490ff27 KH |
4568 | struct mem_cgroup_eventfd_list *ev, *tmp; |
4569 | int type = MEMFILE_TYPE(cft->private); | |
4570 | ||
4571 | BUG_ON(type != _OOM_TYPE); | |
4572 | ||
1af8efe9 | 4573 | spin_lock(&memcg_oom_lock); |
9490ff27 | 4574 | |
c0ff4b85 | 4575 | list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) { |
9490ff27 KH |
4576 | if (ev->eventfd == eventfd) { |
4577 | list_del(&ev->list); | |
4578 | kfree(ev); | |
4579 | } | |
4580 | } | |
4581 | ||
1af8efe9 | 4582 | spin_unlock(&memcg_oom_lock); |
9490ff27 KH |
4583 | } |
4584 | ||
3c11ecf4 KH |
4585 | static int mem_cgroup_oom_control_read(struct cgroup *cgrp, |
4586 | struct cftype *cft, struct cgroup_map_cb *cb) | |
4587 | { | |
c0ff4b85 | 4588 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
3c11ecf4 | 4589 | |
c0ff4b85 | 4590 | cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable); |
3c11ecf4 | 4591 | |
c0ff4b85 | 4592 | if (atomic_read(&memcg->under_oom)) |
3c11ecf4 KH |
4593 | cb->fill(cb, "under_oom", 1); |
4594 | else | |
4595 | cb->fill(cb, "under_oom", 0); | |
4596 | return 0; | |
4597 | } | |
4598 | ||
3c11ecf4 KH |
4599 | static int mem_cgroup_oom_control_write(struct cgroup *cgrp, |
4600 | struct cftype *cft, u64 val) | |
4601 | { | |
c0ff4b85 | 4602 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); |
3c11ecf4 KH |
4603 | struct mem_cgroup *parent; |
4604 | ||
4605 | /* cannot set to root cgroup and only 0 and 1 are allowed */ | |
4606 | if (!cgrp->parent || !((val == 0) || (val == 1))) | |
4607 | return -EINVAL; | |
4608 | ||
4609 | parent = mem_cgroup_from_cont(cgrp->parent); | |
4610 | ||
4611 | cgroup_lock(); | |
4612 | /* oom-kill-disable is a flag for subhierarchy. */ | |
4613 | if ((parent->use_hierarchy) || | |
c0ff4b85 | 4614 | (memcg->use_hierarchy && !list_empty(&cgrp->children))) { |
3c11ecf4 KH |
4615 | cgroup_unlock(); |
4616 | return -EINVAL; | |
4617 | } | |
c0ff4b85 | 4618 | memcg->oom_kill_disable = val; |
4d845ebf | 4619 | if (!val) |
c0ff4b85 | 4620 | memcg_oom_recover(memcg); |
3c11ecf4 KH |
4621 | cgroup_unlock(); |
4622 | return 0; | |
4623 | } | |
4624 | ||
406eb0c9 YH |
4625 | #ifdef CONFIG_NUMA |
4626 | static const struct file_operations mem_control_numa_stat_file_operations = { | |
4627 | .read = seq_read, | |
4628 | .llseek = seq_lseek, | |
4629 | .release = single_release, | |
4630 | }; | |
4631 | ||
4632 | static int mem_control_numa_stat_open(struct inode *unused, struct file *file) | |
4633 | { | |
4634 | struct cgroup *cont = file->f_dentry->d_parent->d_fsdata; | |
4635 | ||
4636 | file->f_op = &mem_control_numa_stat_file_operations; | |
4637 | return single_open(file, mem_control_numa_stat_show, cont); | |
4638 | } | |
4639 | #endif /* CONFIG_NUMA */ | |
4640 | ||
e5671dfa GC |
4641 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM |
4642 | static u64 kmem_limit_independent_read(struct cgroup *cgroup, struct cftype *cft) | |
4643 | { | |
4644 | return mem_cgroup_from_cont(cgroup)->kmem_independent_accounting; | |
4645 | } | |
4646 | ||
4647 | static int kmem_limit_independent_write(struct cgroup *cgroup, struct cftype *cft, | |
4648 | u64 val) | |
4649 | { | |
4650 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup); | |
4651 | struct mem_cgroup *parent = parent_mem_cgroup(memcg); | |
4652 | ||
4653 | val = !!val; | |
4654 | ||
4655 | /* | |
4656 | * This follows the same hierarchy restrictions than | |
4657 | * mem_cgroup_hierarchy_write() | |
4658 | */ | |
4659 | if (!parent || !parent->use_hierarchy) { | |
4660 | if (list_empty(&cgroup->children)) | |
4661 | memcg->kmem_independent_accounting = val; | |
4662 | else | |
4663 | return -EBUSY; | |
4664 | } | |
4665 | else | |
4666 | return -EINVAL; | |
4667 | ||
4668 | return 0; | |
4669 | } | |
4670 | static struct cftype kmem_cgroup_files[] = { | |
4671 | { | |
4672 | .name = "independent_kmem_limit", | |
4673 | .read_u64 = kmem_limit_independent_read, | |
4674 | .write_u64 = kmem_limit_independent_write, | |
4675 | }, | |
4676 | { | |
4677 | .name = "kmem.usage_in_bytes", | |
4678 | .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE), | |
4679 | .read_u64 = mem_cgroup_read, | |
4680 | }, | |
4681 | { | |
4682 | .name = "kmem.limit_in_bytes", | |
4683 | .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT), | |
4684 | .read_u64 = mem_cgroup_read, | |
4685 | }, | |
4686 | }; | |
4687 | ||
4688 | static int register_kmem_files(struct cgroup *cont, struct cgroup_subsys *ss) | |
4689 | { | |
4690 | int ret = 0; | |
4691 | ||
4692 | ret = cgroup_add_files(cont, ss, kmem_cgroup_files, | |
4693 | ARRAY_SIZE(kmem_cgroup_files)); | |
4694 | return ret; | |
4695 | }; | |
4696 | ||
4697 | #else | |
4698 | static int register_kmem_files(struct cgroup *cont, struct cgroup_subsys *ss) | |
4699 | { | |
4700 | return 0; | |
4701 | } | |
4702 | #endif | |
4703 | ||
8cdea7c0 BS |
4704 | static struct cftype mem_cgroup_files[] = { |
4705 | { | |
0eea1030 | 4706 | .name = "usage_in_bytes", |
8c7c6e34 | 4707 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
2c3daa72 | 4708 | .read_u64 = mem_cgroup_read, |
9490ff27 KH |
4709 | .register_event = mem_cgroup_usage_register_event, |
4710 | .unregister_event = mem_cgroup_usage_unregister_event, | |
8cdea7c0 | 4711 | }, |
c84872e1 PE |
4712 | { |
4713 | .name = "max_usage_in_bytes", | |
8c7c6e34 | 4714 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
29f2a4da | 4715 | .trigger = mem_cgroup_reset, |
c84872e1 PE |
4716 | .read_u64 = mem_cgroup_read, |
4717 | }, | |
8cdea7c0 | 4718 | { |
0eea1030 | 4719 | .name = "limit_in_bytes", |
8c7c6e34 | 4720 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
856c13aa | 4721 | .write_string = mem_cgroup_write, |
2c3daa72 | 4722 | .read_u64 = mem_cgroup_read, |
8cdea7c0 | 4723 | }, |
296c81d8 BS |
4724 | { |
4725 | .name = "soft_limit_in_bytes", | |
4726 | .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT), | |
4727 | .write_string = mem_cgroup_write, | |
4728 | .read_u64 = mem_cgroup_read, | |
4729 | }, | |
8cdea7c0 BS |
4730 | { |
4731 | .name = "failcnt", | |
8c7c6e34 | 4732 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
29f2a4da | 4733 | .trigger = mem_cgroup_reset, |
2c3daa72 | 4734 | .read_u64 = mem_cgroup_read, |
8cdea7c0 | 4735 | }, |
d2ceb9b7 KH |
4736 | { |
4737 | .name = "stat", | |
c64745cf | 4738 | .read_map = mem_control_stat_show, |
d2ceb9b7 | 4739 | }, |
c1e862c1 KH |
4740 | { |
4741 | .name = "force_empty", | |
4742 | .trigger = mem_cgroup_force_empty_write, | |
4743 | }, | |
18f59ea7 BS |
4744 | { |
4745 | .name = "use_hierarchy", | |
4746 | .write_u64 = mem_cgroup_hierarchy_write, | |
4747 | .read_u64 = mem_cgroup_hierarchy_read, | |
4748 | }, | |
a7885eb8 KM |
4749 | { |
4750 | .name = "swappiness", | |
4751 | .read_u64 = mem_cgroup_swappiness_read, | |
4752 | .write_u64 = mem_cgroup_swappiness_write, | |
4753 | }, | |
7dc74be0 DN |
4754 | { |
4755 | .name = "move_charge_at_immigrate", | |
4756 | .read_u64 = mem_cgroup_move_charge_read, | |
4757 | .write_u64 = mem_cgroup_move_charge_write, | |
4758 | }, | |
9490ff27 KH |
4759 | { |
4760 | .name = "oom_control", | |
3c11ecf4 KH |
4761 | .read_map = mem_cgroup_oom_control_read, |
4762 | .write_u64 = mem_cgroup_oom_control_write, | |
9490ff27 KH |
4763 | .register_event = mem_cgroup_oom_register_event, |
4764 | .unregister_event = mem_cgroup_oom_unregister_event, | |
4765 | .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL), | |
4766 | }, | |
406eb0c9 YH |
4767 | #ifdef CONFIG_NUMA |
4768 | { | |
4769 | .name = "numa_stat", | |
4770 | .open = mem_control_numa_stat_open, | |
89577127 | 4771 | .mode = S_IRUGO, |
406eb0c9 YH |
4772 | }, |
4773 | #endif | |
8cdea7c0 BS |
4774 | }; |
4775 | ||
8c7c6e34 KH |
4776 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
4777 | static struct cftype memsw_cgroup_files[] = { | |
4778 | { | |
4779 | .name = "memsw.usage_in_bytes", | |
4780 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), | |
4781 | .read_u64 = mem_cgroup_read, | |
9490ff27 KH |
4782 | .register_event = mem_cgroup_usage_register_event, |
4783 | .unregister_event = mem_cgroup_usage_unregister_event, | |
8c7c6e34 KH |
4784 | }, |
4785 | { | |
4786 | .name = "memsw.max_usage_in_bytes", | |
4787 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), | |
4788 | .trigger = mem_cgroup_reset, | |
4789 | .read_u64 = mem_cgroup_read, | |
4790 | }, | |
4791 | { | |
4792 | .name = "memsw.limit_in_bytes", | |
4793 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), | |
4794 | .write_string = mem_cgroup_write, | |
4795 | .read_u64 = mem_cgroup_read, | |
4796 | }, | |
4797 | { | |
4798 | .name = "memsw.failcnt", | |
4799 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), | |
4800 | .trigger = mem_cgroup_reset, | |
4801 | .read_u64 = mem_cgroup_read, | |
4802 | }, | |
4803 | }; | |
4804 | ||
4805 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) | |
4806 | { | |
4807 | if (!do_swap_account) | |
4808 | return 0; | |
4809 | return cgroup_add_files(cont, ss, memsw_cgroup_files, | |
4810 | ARRAY_SIZE(memsw_cgroup_files)); | |
4811 | }; | |
4812 | #else | |
4813 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) | |
4814 | { | |
4815 | return 0; | |
4816 | } | |
4817 | #endif | |
4818 | ||
c0ff4b85 | 4819 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node) |
6d12e2d8 KH |
4820 | { |
4821 | struct mem_cgroup_per_node *pn; | |
1ecaab2b | 4822 | struct mem_cgroup_per_zone *mz; |
b69408e8 | 4823 | enum lru_list l; |
41e3355d | 4824 | int zone, tmp = node; |
1ecaab2b KH |
4825 | /* |
4826 | * This routine is called against possible nodes. | |
4827 | * But it's BUG to call kmalloc() against offline node. | |
4828 | * | |
4829 | * TODO: this routine can waste much memory for nodes which will | |
4830 | * never be onlined. It's better to use memory hotplug callback | |
4831 | * function. | |
4832 | */ | |
41e3355d KH |
4833 | if (!node_state(node, N_NORMAL_MEMORY)) |
4834 | tmp = -1; | |
17295c88 | 4835 | pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp); |
6d12e2d8 KH |
4836 | if (!pn) |
4837 | return 1; | |
1ecaab2b | 4838 | |
1ecaab2b KH |
4839 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { |
4840 | mz = &pn->zoneinfo[zone]; | |
b69408e8 CL |
4841 | for_each_lru(l) |
4842 | INIT_LIST_HEAD(&mz->lists[l]); | |
f64c3f54 | 4843 | mz->usage_in_excess = 0; |
4e416953 | 4844 | mz->on_tree = false; |
c0ff4b85 | 4845 | mz->mem = memcg; |
1ecaab2b | 4846 | } |
0a619e58 | 4847 | memcg->info.nodeinfo[node] = pn; |
6d12e2d8 KH |
4848 | return 0; |
4849 | } | |
4850 | ||
c0ff4b85 | 4851 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node) |
1ecaab2b | 4852 | { |
c0ff4b85 | 4853 | kfree(memcg->info.nodeinfo[node]); |
1ecaab2b KH |
4854 | } |
4855 | ||
33327948 KH |
4856 | static struct mem_cgroup *mem_cgroup_alloc(void) |
4857 | { | |
4858 | struct mem_cgroup *mem; | |
c62b1a3b | 4859 | int size = sizeof(struct mem_cgroup); |
33327948 | 4860 | |
c62b1a3b | 4861 | /* Can be very big if MAX_NUMNODES is very big */ |
c8dad2bb | 4862 | if (size < PAGE_SIZE) |
17295c88 | 4863 | mem = kzalloc(size, GFP_KERNEL); |
33327948 | 4864 | else |
17295c88 | 4865 | mem = vzalloc(size); |
33327948 | 4866 | |
e7bbcdf3 DC |
4867 | if (!mem) |
4868 | return NULL; | |
4869 | ||
c62b1a3b | 4870 | mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu); |
d2e61b8d DC |
4871 | if (!mem->stat) |
4872 | goto out_free; | |
711d3d2c | 4873 | spin_lock_init(&mem->pcp_counter_lock); |
33327948 | 4874 | return mem; |
d2e61b8d DC |
4875 | |
4876 | out_free: | |
4877 | if (size < PAGE_SIZE) | |
4878 | kfree(mem); | |
4879 | else | |
4880 | vfree(mem); | |
4881 | return NULL; | |
33327948 KH |
4882 | } |
4883 | ||
8c7c6e34 KH |
4884 | /* |
4885 | * At destroying mem_cgroup, references from swap_cgroup can remain. | |
4886 | * (scanning all at force_empty is too costly...) | |
4887 | * | |
4888 | * Instead of clearing all references at force_empty, we remember | |
4889 | * the number of reference from swap_cgroup and free mem_cgroup when | |
4890 | * it goes down to 0. | |
4891 | * | |
8c7c6e34 KH |
4892 | * Removal of cgroup itself succeeds regardless of refs from swap. |
4893 | */ | |
4894 | ||
c0ff4b85 | 4895 | static void __mem_cgroup_free(struct mem_cgroup *memcg) |
33327948 | 4896 | { |
08e552c6 KH |
4897 | int node; |
4898 | ||
c0ff4b85 R |
4899 | mem_cgroup_remove_from_trees(memcg); |
4900 | free_css_id(&mem_cgroup_subsys, &memcg->css); | |
04046e1a | 4901 | |
08e552c6 | 4902 | for_each_node_state(node, N_POSSIBLE) |
c0ff4b85 | 4903 | free_mem_cgroup_per_zone_info(memcg, node); |
08e552c6 | 4904 | |
c0ff4b85 | 4905 | free_percpu(memcg->stat); |
c62b1a3b | 4906 | if (sizeof(struct mem_cgroup) < PAGE_SIZE) |
c0ff4b85 | 4907 | kfree(memcg); |
33327948 | 4908 | else |
c0ff4b85 | 4909 | vfree(memcg); |
33327948 KH |
4910 | } |
4911 | ||
c0ff4b85 | 4912 | static void mem_cgroup_get(struct mem_cgroup *memcg) |
8c7c6e34 | 4913 | { |
c0ff4b85 | 4914 | atomic_inc(&memcg->refcnt); |
8c7c6e34 KH |
4915 | } |
4916 | ||
c0ff4b85 | 4917 | static void __mem_cgroup_put(struct mem_cgroup *memcg, int count) |
8c7c6e34 | 4918 | { |
c0ff4b85 R |
4919 | if (atomic_sub_and_test(count, &memcg->refcnt)) { |
4920 | struct mem_cgroup *parent = parent_mem_cgroup(memcg); | |
4921 | __mem_cgroup_free(memcg); | |
7bcc1bb1 DN |
4922 | if (parent) |
4923 | mem_cgroup_put(parent); | |
4924 | } | |
8c7c6e34 KH |
4925 | } |
4926 | ||
c0ff4b85 | 4927 | static void mem_cgroup_put(struct mem_cgroup *memcg) |
483c30b5 | 4928 | { |
c0ff4b85 | 4929 | __mem_cgroup_put(memcg, 1); |
483c30b5 DN |
4930 | } |
4931 | ||
7bcc1bb1 DN |
4932 | /* |
4933 | * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled. | |
4934 | */ | |
c0ff4b85 | 4935 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg) |
7bcc1bb1 | 4936 | { |
c0ff4b85 | 4937 | if (!memcg->res.parent) |
7bcc1bb1 | 4938 | return NULL; |
c0ff4b85 | 4939 | return mem_cgroup_from_res_counter(memcg->res.parent, res); |
7bcc1bb1 | 4940 | } |
33327948 | 4941 | |
c077719b KH |
4942 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
4943 | static void __init enable_swap_cgroup(void) | |
4944 | { | |
f8d66542 | 4945 | if (!mem_cgroup_disabled() && really_do_swap_account) |
c077719b KH |
4946 | do_swap_account = 1; |
4947 | } | |
4948 | #else | |
4949 | static void __init enable_swap_cgroup(void) | |
4950 | { | |
4951 | } | |
4952 | #endif | |
4953 | ||
f64c3f54 BS |
4954 | static int mem_cgroup_soft_limit_tree_init(void) |
4955 | { | |
4956 | struct mem_cgroup_tree_per_node *rtpn; | |
4957 | struct mem_cgroup_tree_per_zone *rtpz; | |
4958 | int tmp, node, zone; | |
4959 | ||
4960 | for_each_node_state(node, N_POSSIBLE) { | |
4961 | tmp = node; | |
4962 | if (!node_state(node, N_NORMAL_MEMORY)) | |
4963 | tmp = -1; | |
4964 | rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp); | |
4965 | if (!rtpn) | |
4966 | return 1; | |
4967 | ||
4968 | soft_limit_tree.rb_tree_per_node[node] = rtpn; | |
4969 | ||
4970 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { | |
4971 | rtpz = &rtpn->rb_tree_per_zone[zone]; | |
4972 | rtpz->rb_root = RB_ROOT; | |
4973 | spin_lock_init(&rtpz->lock); | |
4974 | } | |
4975 | } | |
4976 | return 0; | |
4977 | } | |
4978 | ||
0eb253e2 | 4979 | static struct cgroup_subsys_state * __ref |
8cdea7c0 BS |
4980 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
4981 | { | |
c0ff4b85 | 4982 | struct mem_cgroup *memcg, *parent; |
04046e1a | 4983 | long error = -ENOMEM; |
6d12e2d8 | 4984 | int node; |
8cdea7c0 | 4985 | |
c0ff4b85 R |
4986 | memcg = mem_cgroup_alloc(); |
4987 | if (!memcg) | |
04046e1a | 4988 | return ERR_PTR(error); |
78fb7466 | 4989 | |
6d12e2d8 | 4990 | for_each_node_state(node, N_POSSIBLE) |
c0ff4b85 | 4991 | if (alloc_mem_cgroup_per_zone_info(memcg, node)) |
6d12e2d8 | 4992 | goto free_out; |
f64c3f54 | 4993 | |
c077719b | 4994 | /* root ? */ |
28dbc4b6 | 4995 | if (cont->parent == NULL) { |
cdec2e42 | 4996 | int cpu; |
c077719b | 4997 | enable_swap_cgroup(); |
28dbc4b6 | 4998 | parent = NULL; |
c0ff4b85 | 4999 | root_mem_cgroup = memcg; |
f64c3f54 BS |
5000 | if (mem_cgroup_soft_limit_tree_init()) |
5001 | goto free_out; | |
cdec2e42 KH |
5002 | for_each_possible_cpu(cpu) { |
5003 | struct memcg_stock_pcp *stock = | |
5004 | &per_cpu(memcg_stock, cpu); | |
5005 | INIT_WORK(&stock->work, drain_local_stock); | |
5006 | } | |
711d3d2c | 5007 | hotcpu_notifier(memcg_cpu_hotplug_callback, 0); |
18f59ea7 | 5008 | } else { |
28dbc4b6 | 5009 | parent = mem_cgroup_from_cont(cont->parent); |
c0ff4b85 R |
5010 | memcg->use_hierarchy = parent->use_hierarchy; |
5011 | memcg->oom_kill_disable = parent->oom_kill_disable; | |
18f59ea7 | 5012 | } |
28dbc4b6 | 5013 | |
18f59ea7 | 5014 | if (parent && parent->use_hierarchy) { |
c0ff4b85 R |
5015 | res_counter_init(&memcg->res, &parent->res); |
5016 | res_counter_init(&memcg->memsw, &parent->memsw); | |
e5671dfa | 5017 | res_counter_init(&memcg->kmem, &parent->kmem); |
7bcc1bb1 DN |
5018 | /* |
5019 | * We increment refcnt of the parent to ensure that we can | |
5020 | * safely access it on res_counter_charge/uncharge. | |
5021 | * This refcnt will be decremented when freeing this | |
5022 | * mem_cgroup(see mem_cgroup_put). | |
5023 | */ | |
5024 | mem_cgroup_get(parent); | |
18f59ea7 | 5025 | } else { |
c0ff4b85 R |
5026 | res_counter_init(&memcg->res, NULL); |
5027 | res_counter_init(&memcg->memsw, NULL); | |
e5671dfa | 5028 | res_counter_init(&memcg->kmem, NULL); |
18f59ea7 | 5029 | } |
c0ff4b85 R |
5030 | memcg->last_scanned_child = 0; |
5031 | memcg->last_scanned_node = MAX_NUMNODES; | |
5032 | INIT_LIST_HEAD(&memcg->oom_notify); | |
6d61ef40 | 5033 | |
a7885eb8 | 5034 | if (parent) |
c0ff4b85 R |
5035 | memcg->swappiness = mem_cgroup_swappiness(parent); |
5036 | atomic_set(&memcg->refcnt, 1); | |
5037 | memcg->move_charge_at_immigrate = 0; | |
5038 | mutex_init(&memcg->thresholds_lock); | |
5039 | return &memcg->css; | |
6d12e2d8 | 5040 | free_out: |
c0ff4b85 | 5041 | __mem_cgroup_free(memcg); |
4b3bde4c | 5042 | root_mem_cgroup = NULL; |
04046e1a | 5043 | return ERR_PTR(error); |
8cdea7c0 BS |
5044 | } |
5045 | ||
ec64f515 | 5046 | static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
df878fb0 KH |
5047 | struct cgroup *cont) |
5048 | { | |
c0ff4b85 | 5049 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
ec64f515 | 5050 | |
c0ff4b85 | 5051 | return mem_cgroup_force_empty(memcg, false); |
df878fb0 KH |
5052 | } |
5053 | ||
8cdea7c0 BS |
5054 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
5055 | struct cgroup *cont) | |
5056 | { | |
c0ff4b85 | 5057 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
c268e994 | 5058 | |
c0ff4b85 | 5059 | mem_cgroup_put(memcg); |
8cdea7c0 BS |
5060 | } |
5061 | ||
5062 | static int mem_cgroup_populate(struct cgroup_subsys *ss, | |
5063 | struct cgroup *cont) | |
5064 | { | |
8c7c6e34 KH |
5065 | int ret; |
5066 | ||
5067 | ret = cgroup_add_files(cont, ss, mem_cgroup_files, | |
5068 | ARRAY_SIZE(mem_cgroup_files)); | |
5069 | ||
5070 | if (!ret) | |
5071 | ret = register_memsw_files(cont, ss); | |
e5671dfa GC |
5072 | |
5073 | if (!ret) | |
5074 | ret = register_kmem_files(cont, ss); | |
5075 | ||
8c7c6e34 | 5076 | return ret; |
8cdea7c0 BS |
5077 | } |
5078 | ||
02491447 | 5079 | #ifdef CONFIG_MMU |
7dc74be0 | 5080 | /* Handlers for move charge at task migration. */ |
854ffa8d DN |
5081 | #define PRECHARGE_COUNT_AT_ONCE 256 |
5082 | static int mem_cgroup_do_precharge(unsigned long count) | |
7dc74be0 | 5083 | { |
854ffa8d DN |
5084 | int ret = 0; |
5085 | int batch_count = PRECHARGE_COUNT_AT_ONCE; | |
c0ff4b85 | 5086 | struct mem_cgroup *memcg = mc.to; |
4ffef5fe | 5087 | |
c0ff4b85 | 5088 | if (mem_cgroup_is_root(memcg)) { |
854ffa8d DN |
5089 | mc.precharge += count; |
5090 | /* we don't need css_get for root */ | |
5091 | return ret; | |
5092 | } | |
5093 | /* try to charge at once */ | |
5094 | if (count > 1) { | |
5095 | struct res_counter *dummy; | |
5096 | /* | |
c0ff4b85 | 5097 | * "memcg" cannot be under rmdir() because we've already checked |
854ffa8d DN |
5098 | * by cgroup_lock_live_cgroup() that it is not removed and we |
5099 | * are still under the same cgroup_mutex. So we can postpone | |
5100 | * css_get(). | |
5101 | */ | |
c0ff4b85 | 5102 | if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy)) |
854ffa8d | 5103 | goto one_by_one; |
c0ff4b85 | 5104 | if (do_swap_account && res_counter_charge(&memcg->memsw, |
854ffa8d | 5105 | PAGE_SIZE * count, &dummy)) { |
c0ff4b85 | 5106 | res_counter_uncharge(&memcg->res, PAGE_SIZE * count); |
854ffa8d DN |
5107 | goto one_by_one; |
5108 | } | |
5109 | mc.precharge += count; | |
854ffa8d DN |
5110 | return ret; |
5111 | } | |
5112 | one_by_one: | |
5113 | /* fall back to one by one charge */ | |
5114 | while (count--) { | |
5115 | if (signal_pending(current)) { | |
5116 | ret = -EINTR; | |
5117 | break; | |
5118 | } | |
5119 | if (!batch_count--) { | |
5120 | batch_count = PRECHARGE_COUNT_AT_ONCE; | |
5121 | cond_resched(); | |
5122 | } | |
c0ff4b85 R |
5123 | ret = __mem_cgroup_try_charge(NULL, |
5124 | GFP_KERNEL, 1, &memcg, false); | |
5125 | if (ret || !memcg) | |
854ffa8d DN |
5126 | /* mem_cgroup_clear_mc() will do uncharge later */ |
5127 | return -ENOMEM; | |
5128 | mc.precharge++; | |
5129 | } | |
4ffef5fe DN |
5130 | return ret; |
5131 | } | |
5132 | ||
5133 | /** | |
5134 | * is_target_pte_for_mc - check a pte whether it is valid for move charge | |
5135 | * @vma: the vma the pte to be checked belongs | |
5136 | * @addr: the address corresponding to the pte to be checked | |
5137 | * @ptent: the pte to be checked | |
02491447 | 5138 | * @target: the pointer the target page or swap ent will be stored(can be NULL) |
4ffef5fe DN |
5139 | * |
5140 | * Returns | |
5141 | * 0(MC_TARGET_NONE): if the pte is not a target for move charge. | |
5142 | * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for | |
5143 | * move charge. if @target is not NULL, the page is stored in target->page | |
5144 | * with extra refcnt got(Callers should handle it). | |
02491447 DN |
5145 | * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a |
5146 | * target for charge migration. if @target is not NULL, the entry is stored | |
5147 | * in target->ent. | |
4ffef5fe DN |
5148 | * |
5149 | * Called with pte lock held. | |
5150 | */ | |
4ffef5fe DN |
5151 | union mc_target { |
5152 | struct page *page; | |
02491447 | 5153 | swp_entry_t ent; |
4ffef5fe DN |
5154 | }; |
5155 | ||
4ffef5fe DN |
5156 | enum mc_target_type { |
5157 | MC_TARGET_NONE, /* not used */ | |
5158 | MC_TARGET_PAGE, | |
02491447 | 5159 | MC_TARGET_SWAP, |
4ffef5fe DN |
5160 | }; |
5161 | ||
90254a65 DN |
5162 | static struct page *mc_handle_present_pte(struct vm_area_struct *vma, |
5163 | unsigned long addr, pte_t ptent) | |
4ffef5fe | 5164 | { |
90254a65 | 5165 | struct page *page = vm_normal_page(vma, addr, ptent); |
4ffef5fe | 5166 | |
90254a65 DN |
5167 | if (!page || !page_mapped(page)) |
5168 | return NULL; | |
5169 | if (PageAnon(page)) { | |
5170 | /* we don't move shared anon */ | |
5171 | if (!move_anon() || page_mapcount(page) > 2) | |
5172 | return NULL; | |
87946a72 DN |
5173 | } else if (!move_file()) |
5174 | /* we ignore mapcount for file pages */ | |
90254a65 DN |
5175 | return NULL; |
5176 | if (!get_page_unless_zero(page)) | |
5177 | return NULL; | |
5178 | ||
5179 | return page; | |
5180 | } | |
5181 | ||
5182 | static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, | |
5183 | unsigned long addr, pte_t ptent, swp_entry_t *entry) | |
5184 | { | |
5185 | int usage_count; | |
5186 | struct page *page = NULL; | |
5187 | swp_entry_t ent = pte_to_swp_entry(ptent); | |
5188 | ||
5189 | if (!move_anon() || non_swap_entry(ent)) | |
5190 | return NULL; | |
5191 | usage_count = mem_cgroup_count_swap_user(ent, &page); | |
5192 | if (usage_count > 1) { /* we don't move shared anon */ | |
02491447 DN |
5193 | if (page) |
5194 | put_page(page); | |
90254a65 | 5195 | return NULL; |
02491447 | 5196 | } |
90254a65 DN |
5197 | if (do_swap_account) |
5198 | entry->val = ent.val; | |
5199 | ||
5200 | return page; | |
5201 | } | |
5202 | ||
87946a72 DN |
5203 | static struct page *mc_handle_file_pte(struct vm_area_struct *vma, |
5204 | unsigned long addr, pte_t ptent, swp_entry_t *entry) | |
5205 | { | |
5206 | struct page *page = NULL; | |
5207 | struct inode *inode; | |
5208 | struct address_space *mapping; | |
5209 | pgoff_t pgoff; | |
5210 | ||
5211 | if (!vma->vm_file) /* anonymous vma */ | |
5212 | return NULL; | |
5213 | if (!move_file()) | |
5214 | return NULL; | |
5215 | ||
5216 | inode = vma->vm_file->f_path.dentry->d_inode; | |
5217 | mapping = vma->vm_file->f_mapping; | |
5218 | if (pte_none(ptent)) | |
5219 | pgoff = linear_page_index(vma, addr); | |
5220 | else /* pte_file(ptent) is true */ | |
5221 | pgoff = pte_to_pgoff(ptent); | |
5222 | ||
5223 | /* page is moved even if it's not RSS of this task(page-faulted). */ | |
aa3b1895 HD |
5224 | page = find_get_page(mapping, pgoff); |
5225 | ||
5226 | #ifdef CONFIG_SWAP | |
5227 | /* shmem/tmpfs may report page out on swap: account for that too. */ | |
5228 | if (radix_tree_exceptional_entry(page)) { | |
5229 | swp_entry_t swap = radix_to_swp_entry(page); | |
87946a72 | 5230 | if (do_swap_account) |
aa3b1895 HD |
5231 | *entry = swap; |
5232 | page = find_get_page(&swapper_space, swap.val); | |
87946a72 | 5233 | } |
aa3b1895 | 5234 | #endif |
87946a72 DN |
5235 | return page; |
5236 | } | |
5237 | ||
90254a65 DN |
5238 | static int is_target_pte_for_mc(struct vm_area_struct *vma, |
5239 | unsigned long addr, pte_t ptent, union mc_target *target) | |
5240 | { | |
5241 | struct page *page = NULL; | |
5242 | struct page_cgroup *pc; | |
5243 | int ret = 0; | |
5244 | swp_entry_t ent = { .val = 0 }; | |
5245 | ||
5246 | if (pte_present(ptent)) | |
5247 | page = mc_handle_present_pte(vma, addr, ptent); | |
5248 | else if (is_swap_pte(ptent)) | |
5249 | page = mc_handle_swap_pte(vma, addr, ptent, &ent); | |
87946a72 DN |
5250 | else if (pte_none(ptent) || pte_file(ptent)) |
5251 | page = mc_handle_file_pte(vma, addr, ptent, &ent); | |
90254a65 DN |
5252 | |
5253 | if (!page && !ent.val) | |
5254 | return 0; | |
02491447 DN |
5255 | if (page) { |
5256 | pc = lookup_page_cgroup(page); | |
5257 | /* | |
5258 | * Do only loose check w/o page_cgroup lock. | |
5259 | * mem_cgroup_move_account() checks the pc is valid or not under | |
5260 | * the lock. | |
5261 | */ | |
5262 | if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) { | |
5263 | ret = MC_TARGET_PAGE; | |
5264 | if (target) | |
5265 | target->page = page; | |
5266 | } | |
5267 | if (!ret || !target) | |
5268 | put_page(page); | |
5269 | } | |
90254a65 DN |
5270 | /* There is a swap entry and a page doesn't exist or isn't charged */ |
5271 | if (ent.val && !ret && | |
7f0f1546 KH |
5272 | css_id(&mc.from->css) == lookup_swap_cgroup(ent)) { |
5273 | ret = MC_TARGET_SWAP; | |
5274 | if (target) | |
5275 | target->ent = ent; | |
4ffef5fe | 5276 | } |
4ffef5fe DN |
5277 | return ret; |
5278 | } | |
5279 | ||
5280 | static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd, | |
5281 | unsigned long addr, unsigned long end, | |
5282 | struct mm_walk *walk) | |
5283 | { | |
5284 | struct vm_area_struct *vma = walk->private; | |
5285 | pte_t *pte; | |
5286 | spinlock_t *ptl; | |
5287 | ||
03319327 DH |
5288 | split_huge_page_pmd(walk->mm, pmd); |
5289 | ||
4ffef5fe DN |
5290 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
5291 | for (; addr != end; pte++, addr += PAGE_SIZE) | |
5292 | if (is_target_pte_for_mc(vma, addr, *pte, NULL)) | |
5293 | mc.precharge++; /* increment precharge temporarily */ | |
5294 | pte_unmap_unlock(pte - 1, ptl); | |
5295 | cond_resched(); | |
5296 | ||
7dc74be0 DN |
5297 | return 0; |
5298 | } | |
5299 | ||
4ffef5fe DN |
5300 | static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm) |
5301 | { | |
5302 | unsigned long precharge; | |
5303 | struct vm_area_struct *vma; | |
5304 | ||
dfe076b0 | 5305 | down_read(&mm->mmap_sem); |
4ffef5fe DN |
5306 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
5307 | struct mm_walk mem_cgroup_count_precharge_walk = { | |
5308 | .pmd_entry = mem_cgroup_count_precharge_pte_range, | |
5309 | .mm = mm, | |
5310 | .private = vma, | |
5311 | }; | |
5312 | if (is_vm_hugetlb_page(vma)) | |
5313 | continue; | |
4ffef5fe DN |
5314 | walk_page_range(vma->vm_start, vma->vm_end, |
5315 | &mem_cgroup_count_precharge_walk); | |
5316 | } | |
dfe076b0 | 5317 | up_read(&mm->mmap_sem); |
4ffef5fe DN |
5318 | |
5319 | precharge = mc.precharge; | |
5320 | mc.precharge = 0; | |
5321 | ||
5322 | return precharge; | |
5323 | } | |
5324 | ||
4ffef5fe DN |
5325 | static int mem_cgroup_precharge_mc(struct mm_struct *mm) |
5326 | { | |
dfe076b0 DN |
5327 | unsigned long precharge = mem_cgroup_count_precharge(mm); |
5328 | ||
5329 | VM_BUG_ON(mc.moving_task); | |
5330 | mc.moving_task = current; | |
5331 | return mem_cgroup_do_precharge(precharge); | |
4ffef5fe DN |
5332 | } |
5333 | ||
dfe076b0 DN |
5334 | /* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */ |
5335 | static void __mem_cgroup_clear_mc(void) | |
4ffef5fe | 5336 | { |
2bd9bb20 KH |
5337 | struct mem_cgroup *from = mc.from; |
5338 | struct mem_cgroup *to = mc.to; | |
5339 | ||
4ffef5fe | 5340 | /* we must uncharge all the leftover precharges from mc.to */ |
854ffa8d DN |
5341 | if (mc.precharge) { |
5342 | __mem_cgroup_cancel_charge(mc.to, mc.precharge); | |
5343 | mc.precharge = 0; | |
5344 | } | |
5345 | /* | |
5346 | * we didn't uncharge from mc.from at mem_cgroup_move_account(), so | |
5347 | * we must uncharge here. | |
5348 | */ | |
5349 | if (mc.moved_charge) { | |
5350 | __mem_cgroup_cancel_charge(mc.from, mc.moved_charge); | |
5351 | mc.moved_charge = 0; | |
4ffef5fe | 5352 | } |
483c30b5 DN |
5353 | /* we must fixup refcnts and charges */ |
5354 | if (mc.moved_swap) { | |
483c30b5 DN |
5355 | /* uncharge swap account from the old cgroup */ |
5356 | if (!mem_cgroup_is_root(mc.from)) | |
5357 | res_counter_uncharge(&mc.from->memsw, | |
5358 | PAGE_SIZE * mc.moved_swap); | |
5359 | __mem_cgroup_put(mc.from, mc.moved_swap); | |
5360 | ||
5361 | if (!mem_cgroup_is_root(mc.to)) { | |
5362 | /* | |
5363 | * we charged both to->res and to->memsw, so we should | |
5364 | * uncharge to->res. | |
5365 | */ | |
5366 | res_counter_uncharge(&mc.to->res, | |
5367 | PAGE_SIZE * mc.moved_swap); | |
483c30b5 DN |
5368 | } |
5369 | /* we've already done mem_cgroup_get(mc.to) */ | |
483c30b5 DN |
5370 | mc.moved_swap = 0; |
5371 | } | |
dfe076b0 DN |
5372 | memcg_oom_recover(from); |
5373 | memcg_oom_recover(to); | |
5374 | wake_up_all(&mc.waitq); | |
5375 | } | |
5376 | ||
5377 | static void mem_cgroup_clear_mc(void) | |
5378 | { | |
5379 | struct mem_cgroup *from = mc.from; | |
5380 | ||
5381 | /* | |
5382 | * we must clear moving_task before waking up waiters at the end of | |
5383 | * task migration. | |
5384 | */ | |
5385 | mc.moving_task = NULL; | |
5386 | __mem_cgroup_clear_mc(); | |
2bd9bb20 | 5387 | spin_lock(&mc.lock); |
4ffef5fe DN |
5388 | mc.from = NULL; |
5389 | mc.to = NULL; | |
2bd9bb20 | 5390 | spin_unlock(&mc.lock); |
32047e2a | 5391 | mem_cgroup_end_move(from); |
4ffef5fe DN |
5392 | } |
5393 | ||
7dc74be0 DN |
5394 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, |
5395 | struct cgroup *cgroup, | |
f780bdb7 | 5396 | struct task_struct *p) |
7dc74be0 DN |
5397 | { |
5398 | int ret = 0; | |
c0ff4b85 | 5399 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup); |
7dc74be0 | 5400 | |
c0ff4b85 | 5401 | if (memcg->move_charge_at_immigrate) { |
7dc74be0 DN |
5402 | struct mm_struct *mm; |
5403 | struct mem_cgroup *from = mem_cgroup_from_task(p); | |
5404 | ||
c0ff4b85 | 5405 | VM_BUG_ON(from == memcg); |
7dc74be0 DN |
5406 | |
5407 | mm = get_task_mm(p); | |
5408 | if (!mm) | |
5409 | return 0; | |
7dc74be0 | 5410 | /* We move charges only when we move a owner of the mm */ |
4ffef5fe DN |
5411 | if (mm->owner == p) { |
5412 | VM_BUG_ON(mc.from); | |
5413 | VM_BUG_ON(mc.to); | |
5414 | VM_BUG_ON(mc.precharge); | |
854ffa8d | 5415 | VM_BUG_ON(mc.moved_charge); |
483c30b5 | 5416 | VM_BUG_ON(mc.moved_swap); |
32047e2a | 5417 | mem_cgroup_start_move(from); |
2bd9bb20 | 5418 | spin_lock(&mc.lock); |
4ffef5fe | 5419 | mc.from = from; |
c0ff4b85 | 5420 | mc.to = memcg; |
2bd9bb20 | 5421 | spin_unlock(&mc.lock); |
dfe076b0 | 5422 | /* We set mc.moving_task later */ |
4ffef5fe DN |
5423 | |
5424 | ret = mem_cgroup_precharge_mc(mm); | |
5425 | if (ret) | |
5426 | mem_cgroup_clear_mc(); | |
dfe076b0 DN |
5427 | } |
5428 | mmput(mm); | |
7dc74be0 DN |
5429 | } |
5430 | return ret; | |
5431 | } | |
5432 | ||
5433 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, | |
5434 | struct cgroup *cgroup, | |
f780bdb7 | 5435 | struct task_struct *p) |
7dc74be0 | 5436 | { |
4ffef5fe | 5437 | mem_cgroup_clear_mc(); |
7dc74be0 DN |
5438 | } |
5439 | ||
4ffef5fe DN |
5440 | static int mem_cgroup_move_charge_pte_range(pmd_t *pmd, |
5441 | unsigned long addr, unsigned long end, | |
5442 | struct mm_walk *walk) | |
7dc74be0 | 5443 | { |
4ffef5fe DN |
5444 | int ret = 0; |
5445 | struct vm_area_struct *vma = walk->private; | |
5446 | pte_t *pte; | |
5447 | spinlock_t *ptl; | |
5448 | ||
03319327 | 5449 | split_huge_page_pmd(walk->mm, pmd); |
4ffef5fe DN |
5450 | retry: |
5451 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); | |
5452 | for (; addr != end; addr += PAGE_SIZE) { | |
5453 | pte_t ptent = *(pte++); | |
5454 | union mc_target target; | |
5455 | int type; | |
5456 | struct page *page; | |
5457 | struct page_cgroup *pc; | |
02491447 | 5458 | swp_entry_t ent; |
4ffef5fe DN |
5459 | |
5460 | if (!mc.precharge) | |
5461 | break; | |
5462 | ||
5463 | type = is_target_pte_for_mc(vma, addr, ptent, &target); | |
5464 | switch (type) { | |
5465 | case MC_TARGET_PAGE: | |
5466 | page = target.page; | |
5467 | if (isolate_lru_page(page)) | |
5468 | goto put; | |
5469 | pc = lookup_page_cgroup(page); | |
7ec99d62 JW |
5470 | if (!mem_cgroup_move_account(page, 1, pc, |
5471 | mc.from, mc.to, false)) { | |
4ffef5fe | 5472 | mc.precharge--; |
854ffa8d DN |
5473 | /* we uncharge from mc.from later. */ |
5474 | mc.moved_charge++; | |
4ffef5fe DN |
5475 | } |
5476 | putback_lru_page(page); | |
5477 | put: /* is_target_pte_for_mc() gets the page */ | |
5478 | put_page(page); | |
5479 | break; | |
02491447 DN |
5480 | case MC_TARGET_SWAP: |
5481 | ent = target.ent; | |
483c30b5 DN |
5482 | if (!mem_cgroup_move_swap_account(ent, |
5483 | mc.from, mc.to, false)) { | |
02491447 | 5484 | mc.precharge--; |
483c30b5 DN |
5485 | /* we fixup refcnts and charges later. */ |
5486 | mc.moved_swap++; | |
5487 | } | |
02491447 | 5488 | break; |
4ffef5fe DN |
5489 | default: |
5490 | break; | |
5491 | } | |
5492 | } | |
5493 | pte_unmap_unlock(pte - 1, ptl); | |
5494 | cond_resched(); | |
5495 | ||
5496 | if (addr != end) { | |
5497 | /* | |
5498 | * We have consumed all precharges we got in can_attach(). | |
5499 | * We try charge one by one, but don't do any additional | |
5500 | * charges to mc.to if we have failed in charge once in attach() | |
5501 | * phase. | |
5502 | */ | |
854ffa8d | 5503 | ret = mem_cgroup_do_precharge(1); |
4ffef5fe DN |
5504 | if (!ret) |
5505 | goto retry; | |
5506 | } | |
5507 | ||
5508 | return ret; | |
5509 | } | |
5510 | ||
5511 | static void mem_cgroup_move_charge(struct mm_struct *mm) | |
5512 | { | |
5513 | struct vm_area_struct *vma; | |
5514 | ||
5515 | lru_add_drain_all(); | |
dfe076b0 DN |
5516 | retry: |
5517 | if (unlikely(!down_read_trylock(&mm->mmap_sem))) { | |
5518 | /* | |
5519 | * Someone who are holding the mmap_sem might be waiting in | |
5520 | * waitq. So we cancel all extra charges, wake up all waiters, | |
5521 | * and retry. Because we cancel precharges, we might not be able | |
5522 | * to move enough charges, but moving charge is a best-effort | |
5523 | * feature anyway, so it wouldn't be a big problem. | |
5524 | */ | |
5525 | __mem_cgroup_clear_mc(); | |
5526 | cond_resched(); | |
5527 | goto retry; | |
5528 | } | |
4ffef5fe DN |
5529 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
5530 | int ret; | |
5531 | struct mm_walk mem_cgroup_move_charge_walk = { | |
5532 | .pmd_entry = mem_cgroup_move_charge_pte_range, | |
5533 | .mm = mm, | |
5534 | .private = vma, | |
5535 | }; | |
5536 | if (is_vm_hugetlb_page(vma)) | |
5537 | continue; | |
4ffef5fe DN |
5538 | ret = walk_page_range(vma->vm_start, vma->vm_end, |
5539 | &mem_cgroup_move_charge_walk); | |
5540 | if (ret) | |
5541 | /* | |
5542 | * means we have consumed all precharges and failed in | |
5543 | * doing additional charge. Just abandon here. | |
5544 | */ | |
5545 | break; | |
5546 | } | |
dfe076b0 | 5547 | up_read(&mm->mmap_sem); |
7dc74be0 DN |
5548 | } |
5549 | ||
67e465a7 BS |
5550 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
5551 | struct cgroup *cont, | |
5552 | struct cgroup *old_cont, | |
f780bdb7 | 5553 | struct task_struct *p) |
67e465a7 | 5554 | { |
a433658c | 5555 | struct mm_struct *mm = get_task_mm(p); |
dfe076b0 | 5556 | |
dfe076b0 | 5557 | if (mm) { |
a433658c KM |
5558 | if (mc.to) |
5559 | mem_cgroup_move_charge(mm); | |
5560 | put_swap_token(mm); | |
dfe076b0 DN |
5561 | mmput(mm); |
5562 | } | |
a433658c KM |
5563 | if (mc.to) |
5564 | mem_cgroup_clear_mc(); | |
67e465a7 | 5565 | } |
5cfb80a7 DN |
5566 | #else /* !CONFIG_MMU */ |
5567 | static int mem_cgroup_can_attach(struct cgroup_subsys *ss, | |
5568 | struct cgroup *cgroup, | |
f780bdb7 | 5569 | struct task_struct *p) |
5cfb80a7 DN |
5570 | { |
5571 | return 0; | |
5572 | } | |
5573 | static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss, | |
5574 | struct cgroup *cgroup, | |
f780bdb7 | 5575 | struct task_struct *p) |
5cfb80a7 DN |
5576 | { |
5577 | } | |
5578 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, | |
5579 | struct cgroup *cont, | |
5580 | struct cgroup *old_cont, | |
f780bdb7 | 5581 | struct task_struct *p) |
5cfb80a7 DN |
5582 | { |
5583 | } | |
5584 | #endif | |
67e465a7 | 5585 | |
8cdea7c0 BS |
5586 | struct cgroup_subsys mem_cgroup_subsys = { |
5587 | .name = "memory", | |
5588 | .subsys_id = mem_cgroup_subsys_id, | |
5589 | .create = mem_cgroup_create, | |
df878fb0 | 5590 | .pre_destroy = mem_cgroup_pre_destroy, |
8cdea7c0 BS |
5591 | .destroy = mem_cgroup_destroy, |
5592 | .populate = mem_cgroup_populate, | |
7dc74be0 DN |
5593 | .can_attach = mem_cgroup_can_attach, |
5594 | .cancel_attach = mem_cgroup_cancel_attach, | |
67e465a7 | 5595 | .attach = mem_cgroup_move_task, |
6d12e2d8 | 5596 | .early_init = 0, |
04046e1a | 5597 | .use_id = 1, |
8cdea7c0 | 5598 | }; |
c077719b KH |
5599 | |
5600 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP | |
a42c390c MH |
5601 | static int __init enable_swap_account(char *s) |
5602 | { | |
5603 | /* consider enabled if no parameter or 1 is given */ | |
a2c8990a | 5604 | if (!strcmp(s, "1")) |
a42c390c | 5605 | really_do_swap_account = 1; |
a2c8990a | 5606 | else if (!strcmp(s, "0")) |
a42c390c MH |
5607 | really_do_swap_account = 0; |
5608 | return 1; | |
5609 | } | |
a2c8990a | 5610 | __setup("swapaccount=", enable_swap_account); |
c077719b | 5611 | |
c077719b | 5612 | #endif |