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