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