]>
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 | * | |
8cdea7c0 BS |
9 | * This program is free software; you can redistribute it and/or modify |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | */ | |
19 | ||
20 | #include <linux/res_counter.h> | |
21 | #include <linux/memcontrol.h> | |
22 | #include <linux/cgroup.h> | |
78fb7466 | 23 | #include <linux/mm.h> |
d13d1443 | 24 | #include <linux/pagemap.h> |
d52aa412 | 25 | #include <linux/smp.h> |
8a9f3ccd | 26 | #include <linux/page-flags.h> |
66e1707b | 27 | #include <linux/backing-dev.h> |
8a9f3ccd BS |
28 | #include <linux/bit_spinlock.h> |
29 | #include <linux/rcupdate.h> | |
e222432b | 30 | #include <linux/limits.h> |
8c7c6e34 | 31 | #include <linux/mutex.h> |
f64c3f54 | 32 | #include <linux/rbtree.h> |
b6ac57d5 | 33 | #include <linux/slab.h> |
66e1707b BS |
34 | #include <linux/swap.h> |
35 | #include <linux/spinlock.h> | |
36 | #include <linux/fs.h> | |
d2ceb9b7 | 37 | #include <linux/seq_file.h> |
33327948 | 38 | #include <linux/vmalloc.h> |
b69408e8 | 39 | #include <linux/mm_inline.h> |
52d4b9ac | 40 | #include <linux/page_cgroup.h> |
08e552c6 | 41 | #include "internal.h" |
8cdea7c0 | 42 | |
8697d331 BS |
43 | #include <asm/uaccess.h> |
44 | ||
a181b0e8 | 45 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
a181b0e8 | 46 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
4b3bde4c | 47 | struct mem_cgroup *root_mem_cgroup __read_mostly; |
8cdea7c0 | 48 | |
c077719b | 49 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
338c8431 | 50 | /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */ |
c077719b KH |
51 | int do_swap_account __read_mostly; |
52 | static int really_do_swap_account __initdata = 1; /* for remember boot option*/ | |
53 | #else | |
54 | #define do_swap_account (0) | |
55 | #endif | |
56 | ||
7f4d454d | 57 | static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */ |
f64c3f54 | 58 | #define SOFTLIMIT_EVENTS_THRESH (1000) |
c077719b | 59 | |
d52aa412 KH |
60 | /* |
61 | * Statistics for memory cgroup. | |
62 | */ | |
63 | enum mem_cgroup_stat_index { | |
64 | /* | |
65 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. | |
66 | */ | |
67 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ | |
d69b042f BS |
68 | MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */ |
69 | MEM_CGROUP_STAT_MAPPED_FILE, /* # of pages charged as file rss */ | |
55e462b0 BR |
70 | MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */ |
71 | MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */ | |
f64c3f54 | 72 | MEM_CGROUP_STAT_EVENTS, /* sum of pagein + pageout for internal use */ |
0c3e73e8 | 73 | MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */ |
d52aa412 KH |
74 | |
75 | MEM_CGROUP_STAT_NSTATS, | |
76 | }; | |
77 | ||
78 | struct mem_cgroup_stat_cpu { | |
79 | s64 count[MEM_CGROUP_STAT_NSTATS]; | |
80 | } ____cacheline_aligned_in_smp; | |
81 | ||
82 | struct mem_cgroup_stat { | |
c8dad2bb | 83 | struct mem_cgroup_stat_cpu cpustat[0]; |
d52aa412 KH |
84 | }; |
85 | ||
f64c3f54 BS |
86 | static inline void |
87 | __mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat, | |
88 | enum mem_cgroup_stat_index idx) | |
89 | { | |
90 | stat->count[idx] = 0; | |
91 | } | |
92 | ||
93 | static inline s64 | |
94 | __mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat, | |
95 | enum mem_cgroup_stat_index idx) | |
96 | { | |
97 | return stat->count[idx]; | |
98 | } | |
99 | ||
d52aa412 KH |
100 | /* |
101 | * For accounting under irq disable, no need for increment preempt count. | |
102 | */ | |
addb9efe | 103 | static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat, |
d52aa412 KH |
104 | enum mem_cgroup_stat_index idx, int val) |
105 | { | |
addb9efe | 106 | stat->count[idx] += val; |
d52aa412 KH |
107 | } |
108 | ||
109 | static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat, | |
110 | enum mem_cgroup_stat_index idx) | |
111 | { | |
112 | int cpu; | |
113 | s64 ret = 0; | |
114 | for_each_possible_cpu(cpu) | |
115 | ret += stat->cpustat[cpu].count[idx]; | |
116 | return ret; | |
117 | } | |
118 | ||
04046e1a KH |
119 | static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat) |
120 | { | |
121 | s64 ret; | |
122 | ||
123 | ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE); | |
124 | ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS); | |
125 | return ret; | |
126 | } | |
127 | ||
6d12e2d8 KH |
128 | /* |
129 | * per-zone information in memory controller. | |
130 | */ | |
6d12e2d8 | 131 | struct mem_cgroup_per_zone { |
072c56c1 KH |
132 | /* |
133 | * spin_lock to protect the per cgroup LRU | |
134 | */ | |
b69408e8 CL |
135 | struct list_head lists[NR_LRU_LISTS]; |
136 | unsigned long count[NR_LRU_LISTS]; | |
3e2f41f1 KM |
137 | |
138 | struct zone_reclaim_stat reclaim_stat; | |
f64c3f54 BS |
139 | struct rb_node tree_node; /* RB tree node */ |
140 | unsigned long long usage_in_excess;/* Set to the value by which */ | |
141 | /* the soft limit is exceeded*/ | |
142 | bool on_tree; | |
4e416953 BS |
143 | struct mem_cgroup *mem; /* Back pointer, we cannot */ |
144 | /* use container_of */ | |
6d12e2d8 KH |
145 | }; |
146 | /* Macro for accessing counter */ | |
147 | #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)]) | |
148 | ||
149 | struct mem_cgroup_per_node { | |
150 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; | |
151 | }; | |
152 | ||
153 | struct mem_cgroup_lru_info { | |
154 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; | |
155 | }; | |
156 | ||
f64c3f54 BS |
157 | /* |
158 | * Cgroups above their limits are maintained in a RB-Tree, independent of | |
159 | * their hierarchy representation | |
160 | */ | |
161 | ||
162 | struct mem_cgroup_tree_per_zone { | |
163 | struct rb_root rb_root; | |
164 | spinlock_t lock; | |
165 | }; | |
166 | ||
167 | struct mem_cgroup_tree_per_node { | |
168 | struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES]; | |
169 | }; | |
170 | ||
171 | struct mem_cgroup_tree { | |
172 | struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES]; | |
173 | }; | |
174 | ||
175 | static struct mem_cgroup_tree soft_limit_tree __read_mostly; | |
176 | ||
8cdea7c0 BS |
177 | /* |
178 | * The memory controller data structure. The memory controller controls both | |
179 | * page cache and RSS per cgroup. We would eventually like to provide | |
180 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, | |
181 | * to help the administrator determine what knobs to tune. | |
182 | * | |
183 | * TODO: Add a water mark for the memory controller. Reclaim will begin when | |
8a9f3ccd BS |
184 | * we hit the water mark. May be even add a low water mark, such that |
185 | * no reclaim occurs from a cgroup at it's low water mark, this is | |
186 | * a feature that will be implemented much later in the future. | |
8cdea7c0 BS |
187 | */ |
188 | struct mem_cgroup { | |
189 | struct cgroup_subsys_state css; | |
190 | /* | |
191 | * the counter to account for memory usage | |
192 | */ | |
193 | struct res_counter res; | |
8c7c6e34 KH |
194 | /* |
195 | * the counter to account for mem+swap usage. | |
196 | */ | |
197 | struct res_counter memsw; | |
78fb7466 PE |
198 | /* |
199 | * Per cgroup active and inactive list, similar to the | |
200 | * per zone LRU lists. | |
78fb7466 | 201 | */ |
6d12e2d8 | 202 | struct mem_cgroup_lru_info info; |
072c56c1 | 203 | |
2733c06a KM |
204 | /* |
205 | protect against reclaim related member. | |
206 | */ | |
207 | spinlock_t reclaim_param_lock; | |
208 | ||
6c48a1d0 | 209 | int prev_priority; /* for recording reclaim priority */ |
6d61ef40 BS |
210 | |
211 | /* | |
212 | * While reclaiming in a hiearchy, we cache the last child we | |
04046e1a | 213 | * reclaimed from. |
6d61ef40 | 214 | */ |
04046e1a | 215 | int last_scanned_child; |
18f59ea7 BS |
216 | /* |
217 | * Should the accounting and control be hierarchical, per subtree? | |
218 | */ | |
219 | bool use_hierarchy; | |
a636b327 | 220 | unsigned long last_oom_jiffies; |
8c7c6e34 | 221 | atomic_t refcnt; |
14797e23 | 222 | |
a7885eb8 KM |
223 | unsigned int swappiness; |
224 | ||
22a668d7 KH |
225 | /* set when res.limit == memsw.limit */ |
226 | bool memsw_is_minimum; | |
227 | ||
d52aa412 | 228 | /* |
c8dad2bb | 229 | * statistics. This must be placed at the end of memcg. |
d52aa412 KH |
230 | */ |
231 | struct mem_cgroup_stat stat; | |
8cdea7c0 BS |
232 | }; |
233 | ||
4e416953 BS |
234 | /* |
235 | * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft | |
236 | * limit reclaim to prevent infinite loops, if they ever occur. | |
237 | */ | |
238 | #define MEM_CGROUP_MAX_RECLAIM_LOOPS (100) | |
239 | #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2) | |
240 | ||
217bc319 KH |
241 | enum charge_type { |
242 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, | |
243 | MEM_CGROUP_CHARGE_TYPE_MAPPED, | |
4f98a2fe | 244 | MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */ |
c05555b5 | 245 | MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */ |
d13d1443 | 246 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
8a9478ca | 247 | MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */ |
c05555b5 KH |
248 | NR_CHARGE_TYPE, |
249 | }; | |
250 | ||
52d4b9ac KH |
251 | /* only for here (for easy reading.) */ |
252 | #define PCGF_CACHE (1UL << PCG_CACHE) | |
253 | #define PCGF_USED (1UL << PCG_USED) | |
52d4b9ac | 254 | #define PCGF_LOCK (1UL << PCG_LOCK) |
4b3bde4c BS |
255 | /* Not used, but added here for completeness */ |
256 | #define PCGF_ACCT (1UL << PCG_ACCT) | |
217bc319 | 257 | |
8c7c6e34 KH |
258 | /* for encoding cft->private value on file */ |
259 | #define _MEM (0) | |
260 | #define _MEMSWAP (1) | |
261 | #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val)) | |
262 | #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff) | |
263 | #define MEMFILE_ATTR(val) ((val) & 0xffff) | |
264 | ||
75822b44 BS |
265 | /* |
266 | * Reclaim flags for mem_cgroup_hierarchical_reclaim | |
267 | */ | |
268 | #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0 | |
269 | #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT) | |
270 | #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1 | |
271 | #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT) | |
4e416953 BS |
272 | #define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2 |
273 | #define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT) | |
75822b44 | 274 | |
8c7c6e34 KH |
275 | static void mem_cgroup_get(struct mem_cgroup *mem); |
276 | static void mem_cgroup_put(struct mem_cgroup *mem); | |
7bcc1bb1 | 277 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem); |
8c7c6e34 | 278 | |
f64c3f54 BS |
279 | static struct mem_cgroup_per_zone * |
280 | mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid) | |
281 | { | |
282 | return &mem->info.nodeinfo[nid]->zoneinfo[zid]; | |
283 | } | |
284 | ||
285 | static struct mem_cgroup_per_zone * | |
286 | page_cgroup_zoneinfo(struct page_cgroup *pc) | |
287 | { | |
288 | struct mem_cgroup *mem = pc->mem_cgroup; | |
289 | int nid = page_cgroup_nid(pc); | |
290 | int zid = page_cgroup_zid(pc); | |
291 | ||
292 | if (!mem) | |
293 | return NULL; | |
294 | ||
295 | return mem_cgroup_zoneinfo(mem, nid, zid); | |
296 | } | |
297 | ||
298 | static struct mem_cgroup_tree_per_zone * | |
299 | soft_limit_tree_node_zone(int nid, int zid) | |
300 | { | |
301 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; | |
302 | } | |
303 | ||
304 | static struct mem_cgroup_tree_per_zone * | |
305 | soft_limit_tree_from_page(struct page *page) | |
306 | { | |
307 | int nid = page_to_nid(page); | |
308 | int zid = page_zonenum(page); | |
309 | ||
310 | return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid]; | |
311 | } | |
312 | ||
313 | static void | |
4e416953 | 314 | __mem_cgroup_insert_exceeded(struct mem_cgroup *mem, |
f64c3f54 BS |
315 | struct mem_cgroup_per_zone *mz, |
316 | struct mem_cgroup_tree_per_zone *mctz) | |
317 | { | |
318 | struct rb_node **p = &mctz->rb_root.rb_node; | |
319 | struct rb_node *parent = NULL; | |
320 | struct mem_cgroup_per_zone *mz_node; | |
321 | ||
322 | if (mz->on_tree) | |
323 | return; | |
324 | ||
325 | mz->usage_in_excess = res_counter_soft_limit_excess(&mem->res); | |
f64c3f54 BS |
326 | while (*p) { |
327 | parent = *p; | |
328 | mz_node = rb_entry(parent, struct mem_cgroup_per_zone, | |
329 | tree_node); | |
330 | if (mz->usage_in_excess < mz_node->usage_in_excess) | |
331 | p = &(*p)->rb_left; | |
332 | /* | |
333 | * We can't avoid mem cgroups that are over their soft | |
334 | * limit by the same amount | |
335 | */ | |
336 | else if (mz->usage_in_excess >= mz_node->usage_in_excess) | |
337 | p = &(*p)->rb_right; | |
338 | } | |
339 | rb_link_node(&mz->tree_node, parent, p); | |
340 | rb_insert_color(&mz->tree_node, &mctz->rb_root); | |
341 | mz->on_tree = true; | |
4e416953 BS |
342 | } |
343 | ||
344 | static void | |
345 | __mem_cgroup_remove_exceeded(struct mem_cgroup *mem, | |
346 | struct mem_cgroup_per_zone *mz, | |
347 | struct mem_cgroup_tree_per_zone *mctz) | |
348 | { | |
349 | if (!mz->on_tree) | |
350 | return; | |
351 | rb_erase(&mz->tree_node, &mctz->rb_root); | |
352 | mz->on_tree = false; | |
353 | } | |
354 | ||
f64c3f54 BS |
355 | static void |
356 | mem_cgroup_remove_exceeded(struct mem_cgroup *mem, | |
357 | struct mem_cgroup_per_zone *mz, | |
358 | struct mem_cgroup_tree_per_zone *mctz) | |
359 | { | |
360 | spin_lock(&mctz->lock); | |
4e416953 | 361 | __mem_cgroup_remove_exceeded(mem, mz, mctz); |
f64c3f54 BS |
362 | spin_unlock(&mctz->lock); |
363 | } | |
364 | ||
365 | static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem) | |
366 | { | |
367 | bool ret = false; | |
368 | int cpu; | |
369 | s64 val; | |
370 | struct mem_cgroup_stat_cpu *cpustat; | |
371 | ||
372 | cpu = get_cpu(); | |
373 | cpustat = &mem->stat.cpustat[cpu]; | |
374 | val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS); | |
375 | if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) { | |
376 | __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS); | |
377 | ret = true; | |
378 | } | |
379 | put_cpu(); | |
380 | return ret; | |
381 | } | |
382 | ||
383 | static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page) | |
384 | { | |
4e649152 | 385 | unsigned long long new_usage_in_excess; |
f64c3f54 BS |
386 | struct mem_cgroup_per_zone *mz; |
387 | struct mem_cgroup_tree_per_zone *mctz; | |
4e649152 KH |
388 | int nid = page_to_nid(page); |
389 | int zid = page_zonenum(page); | |
f64c3f54 BS |
390 | mctz = soft_limit_tree_from_page(page); |
391 | ||
392 | /* | |
4e649152 KH |
393 | * Necessary to update all ancestors when hierarchy is used. |
394 | * because their event counter is not touched. | |
f64c3f54 | 395 | */ |
4e649152 KH |
396 | for (; mem; mem = parent_mem_cgroup(mem)) { |
397 | mz = mem_cgroup_zoneinfo(mem, nid, zid); | |
398 | new_usage_in_excess = | |
399 | res_counter_soft_limit_excess(&mem->res); | |
400 | /* | |
401 | * We have to update the tree if mz is on RB-tree or | |
402 | * mem is over its softlimit. | |
403 | */ | |
404 | if (new_usage_in_excess || mz->on_tree) { | |
405 | spin_lock(&mctz->lock); | |
406 | /* if on-tree, remove it */ | |
407 | if (mz->on_tree) | |
408 | __mem_cgroup_remove_exceeded(mem, mz, mctz); | |
409 | /* | |
410 | * if over soft limit, insert again. mz->usage_in_excess | |
411 | * will be updated properly. | |
412 | */ | |
413 | if (new_usage_in_excess) | |
414 | __mem_cgroup_insert_exceeded(mem, mz, mctz); | |
415 | else | |
416 | mz->usage_in_excess = 0; | |
417 | spin_unlock(&mctz->lock); | |
418 | } | |
f64c3f54 BS |
419 | } |
420 | } | |
421 | ||
422 | static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem) | |
423 | { | |
424 | int node, zone; | |
425 | struct mem_cgroup_per_zone *mz; | |
426 | struct mem_cgroup_tree_per_zone *mctz; | |
427 | ||
428 | for_each_node_state(node, N_POSSIBLE) { | |
429 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { | |
430 | mz = mem_cgroup_zoneinfo(mem, node, zone); | |
431 | mctz = soft_limit_tree_node_zone(node, zone); | |
432 | mem_cgroup_remove_exceeded(mem, mz, mctz); | |
433 | } | |
434 | } | |
435 | } | |
436 | ||
4e416953 BS |
437 | static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem) |
438 | { | |
439 | return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT; | |
440 | } | |
441 | ||
442 | static struct mem_cgroup_per_zone * | |
443 | __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) | |
444 | { | |
445 | struct rb_node *rightmost = NULL; | |
26251eaf | 446 | struct mem_cgroup_per_zone *mz; |
4e416953 BS |
447 | |
448 | retry: | |
26251eaf | 449 | mz = NULL; |
4e416953 BS |
450 | rightmost = rb_last(&mctz->rb_root); |
451 | if (!rightmost) | |
452 | goto done; /* Nothing to reclaim from */ | |
453 | ||
454 | mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node); | |
455 | /* | |
456 | * Remove the node now but someone else can add it back, | |
457 | * we will to add it back at the end of reclaim to its correct | |
458 | * position in the tree. | |
459 | */ | |
460 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); | |
461 | if (!res_counter_soft_limit_excess(&mz->mem->res) || | |
462 | !css_tryget(&mz->mem->css)) | |
463 | goto retry; | |
464 | done: | |
465 | return mz; | |
466 | } | |
467 | ||
468 | static struct mem_cgroup_per_zone * | |
469 | mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz) | |
470 | { | |
471 | struct mem_cgroup_per_zone *mz; | |
472 | ||
473 | spin_lock(&mctz->lock); | |
474 | mz = __mem_cgroup_largest_soft_limit_node(mctz); | |
475 | spin_unlock(&mctz->lock); | |
476 | return mz; | |
477 | } | |
478 | ||
0c3e73e8 BS |
479 | static void mem_cgroup_swap_statistics(struct mem_cgroup *mem, |
480 | bool charge) | |
481 | { | |
482 | int val = (charge) ? 1 : -1; | |
483 | struct mem_cgroup_stat *stat = &mem->stat; | |
484 | struct mem_cgroup_stat_cpu *cpustat; | |
485 | int cpu = get_cpu(); | |
486 | ||
487 | cpustat = &stat->cpustat[cpu]; | |
488 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val); | |
489 | put_cpu(); | |
490 | } | |
491 | ||
c05555b5 KH |
492 | static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, |
493 | struct page_cgroup *pc, | |
494 | bool charge) | |
d52aa412 | 495 | { |
0c3e73e8 | 496 | int val = (charge) ? 1 : -1; |
d52aa412 | 497 | struct mem_cgroup_stat *stat = &mem->stat; |
addb9efe | 498 | struct mem_cgroup_stat_cpu *cpustat; |
08e552c6 | 499 | int cpu = get_cpu(); |
d52aa412 | 500 | |
08e552c6 | 501 | cpustat = &stat->cpustat[cpu]; |
c05555b5 | 502 | if (PageCgroupCache(pc)) |
addb9efe | 503 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val); |
d52aa412 | 504 | else |
addb9efe | 505 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val); |
55e462b0 BR |
506 | |
507 | if (charge) | |
addb9efe | 508 | __mem_cgroup_stat_add_safe(cpustat, |
55e462b0 BR |
509 | MEM_CGROUP_STAT_PGPGIN_COUNT, 1); |
510 | else | |
addb9efe | 511 | __mem_cgroup_stat_add_safe(cpustat, |
55e462b0 | 512 | MEM_CGROUP_STAT_PGPGOUT_COUNT, 1); |
f64c3f54 | 513 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1); |
08e552c6 | 514 | put_cpu(); |
6d12e2d8 KH |
515 | } |
516 | ||
14067bb3 | 517 | static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem, |
b69408e8 | 518 | enum lru_list idx) |
6d12e2d8 KH |
519 | { |
520 | int nid, zid; | |
521 | struct mem_cgroup_per_zone *mz; | |
522 | u64 total = 0; | |
523 | ||
524 | for_each_online_node(nid) | |
525 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { | |
526 | mz = mem_cgroup_zoneinfo(mem, nid, zid); | |
527 | total += MEM_CGROUP_ZSTAT(mz, idx); | |
528 | } | |
529 | return total; | |
d52aa412 KH |
530 | } |
531 | ||
d5b69e38 | 532 | static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
8cdea7c0 BS |
533 | { |
534 | return container_of(cgroup_subsys_state(cont, | |
535 | mem_cgroup_subsys_id), struct mem_cgroup, | |
536 | css); | |
537 | } | |
538 | ||
cf475ad2 | 539 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
78fb7466 | 540 | { |
31a78f23 BS |
541 | /* |
542 | * mm_update_next_owner() may clear mm->owner to NULL | |
543 | * if it races with swapoff, page migration, etc. | |
544 | * So this can be called with p == NULL. | |
545 | */ | |
546 | if (unlikely(!p)) | |
547 | return NULL; | |
548 | ||
78fb7466 PE |
549 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
550 | struct mem_cgroup, css); | |
551 | } | |
552 | ||
54595fe2 KH |
553 | static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) |
554 | { | |
555 | struct mem_cgroup *mem = NULL; | |
0b7f569e KH |
556 | |
557 | if (!mm) | |
558 | return NULL; | |
54595fe2 KH |
559 | /* |
560 | * Because we have no locks, mm->owner's may be being moved to other | |
561 | * cgroup. We use css_tryget() here even if this looks | |
562 | * pessimistic (rather than adding locks here). | |
563 | */ | |
564 | rcu_read_lock(); | |
565 | do { | |
566 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); | |
567 | if (unlikely(!mem)) | |
568 | break; | |
569 | } while (!css_tryget(&mem->css)); | |
570 | rcu_read_unlock(); | |
571 | return mem; | |
572 | } | |
573 | ||
14067bb3 KH |
574 | /* |
575 | * Call callback function against all cgroup under hierarchy tree. | |
576 | */ | |
577 | static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data, | |
578 | int (*func)(struct mem_cgroup *, void *)) | |
579 | { | |
580 | int found, ret, nextid; | |
581 | struct cgroup_subsys_state *css; | |
582 | struct mem_cgroup *mem; | |
583 | ||
584 | if (!root->use_hierarchy) | |
585 | return (*func)(root, data); | |
586 | ||
587 | nextid = 1; | |
588 | do { | |
589 | ret = 0; | |
590 | mem = NULL; | |
591 | ||
592 | rcu_read_lock(); | |
593 | css = css_get_next(&mem_cgroup_subsys, nextid, &root->css, | |
594 | &found); | |
595 | if (css && css_tryget(css)) | |
596 | mem = container_of(css, struct mem_cgroup, css); | |
597 | rcu_read_unlock(); | |
598 | ||
599 | if (mem) { | |
600 | ret = (*func)(mem, data); | |
601 | css_put(&mem->css); | |
602 | } | |
603 | nextid = found + 1; | |
604 | } while (!ret && css); | |
605 | ||
606 | return ret; | |
607 | } | |
608 | ||
4b3bde4c BS |
609 | static inline bool mem_cgroup_is_root(struct mem_cgroup *mem) |
610 | { | |
611 | return (mem == root_mem_cgroup); | |
612 | } | |
613 | ||
08e552c6 KH |
614 | /* |
615 | * Following LRU functions are allowed to be used without PCG_LOCK. | |
616 | * Operations are called by routine of global LRU independently from memcg. | |
617 | * What we have to take care of here is validness of pc->mem_cgroup. | |
618 | * | |
619 | * Changes to pc->mem_cgroup happens when | |
620 | * 1. charge | |
621 | * 2. moving account | |
622 | * In typical case, "charge" is done before add-to-lru. Exception is SwapCache. | |
623 | * It is added to LRU before charge. | |
624 | * If PCG_USED bit is not set, page_cgroup is not added to this private LRU. | |
625 | * When moving account, the page is not on LRU. It's isolated. | |
626 | */ | |
4f98a2fe | 627 | |
08e552c6 KH |
628 | void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru) |
629 | { | |
630 | struct page_cgroup *pc; | |
08e552c6 | 631 | struct mem_cgroup_per_zone *mz; |
6d12e2d8 | 632 | |
f8d66542 | 633 | if (mem_cgroup_disabled()) |
08e552c6 KH |
634 | return; |
635 | pc = lookup_page_cgroup(page); | |
636 | /* can happen while we handle swapcache. */ | |
4b3bde4c | 637 | if (!TestClearPageCgroupAcctLRU(pc)) |
08e552c6 | 638 | return; |
4b3bde4c | 639 | VM_BUG_ON(!pc->mem_cgroup); |
544122e5 KH |
640 | /* |
641 | * We don't check PCG_USED bit. It's cleared when the "page" is finally | |
642 | * removed from global LRU. | |
643 | */ | |
08e552c6 | 644 | mz = page_cgroup_zoneinfo(pc); |
b69408e8 | 645 | MEM_CGROUP_ZSTAT(mz, lru) -= 1; |
4b3bde4c BS |
646 | if (mem_cgroup_is_root(pc->mem_cgroup)) |
647 | return; | |
648 | VM_BUG_ON(list_empty(&pc->lru)); | |
08e552c6 KH |
649 | list_del_init(&pc->lru); |
650 | return; | |
6d12e2d8 KH |
651 | } |
652 | ||
08e552c6 | 653 | void mem_cgroup_del_lru(struct page *page) |
6d12e2d8 | 654 | { |
08e552c6 KH |
655 | mem_cgroup_del_lru_list(page, page_lru(page)); |
656 | } | |
b69408e8 | 657 | |
08e552c6 KH |
658 | void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru) |
659 | { | |
660 | struct mem_cgroup_per_zone *mz; | |
661 | struct page_cgroup *pc; | |
b69408e8 | 662 | |
f8d66542 | 663 | if (mem_cgroup_disabled()) |
08e552c6 | 664 | return; |
6d12e2d8 | 665 | |
08e552c6 | 666 | pc = lookup_page_cgroup(page); |
bd112db8 DN |
667 | /* |
668 | * Used bit is set without atomic ops but after smp_wmb(). | |
669 | * For making pc->mem_cgroup visible, insert smp_rmb() here. | |
670 | */ | |
08e552c6 | 671 | smp_rmb(); |
4b3bde4c BS |
672 | /* unused or root page is not rotated. */ |
673 | if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup)) | |
08e552c6 KH |
674 | return; |
675 | mz = page_cgroup_zoneinfo(pc); | |
676 | list_move(&pc->lru, &mz->lists[lru]); | |
6d12e2d8 KH |
677 | } |
678 | ||
08e552c6 | 679 | void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru) |
66e1707b | 680 | { |
08e552c6 KH |
681 | struct page_cgroup *pc; |
682 | struct mem_cgroup_per_zone *mz; | |
6d12e2d8 | 683 | |
f8d66542 | 684 | if (mem_cgroup_disabled()) |
08e552c6 KH |
685 | return; |
686 | pc = lookup_page_cgroup(page); | |
4b3bde4c | 687 | VM_BUG_ON(PageCgroupAcctLRU(pc)); |
bd112db8 DN |
688 | /* |
689 | * Used bit is set without atomic ops but after smp_wmb(). | |
690 | * For making pc->mem_cgroup visible, insert smp_rmb() here. | |
691 | */ | |
08e552c6 KH |
692 | smp_rmb(); |
693 | if (!PageCgroupUsed(pc)) | |
894bc310 | 694 | return; |
b69408e8 | 695 | |
08e552c6 | 696 | mz = page_cgroup_zoneinfo(pc); |
b69408e8 | 697 | MEM_CGROUP_ZSTAT(mz, lru) += 1; |
4b3bde4c BS |
698 | SetPageCgroupAcctLRU(pc); |
699 | if (mem_cgroup_is_root(pc->mem_cgroup)) | |
700 | return; | |
08e552c6 KH |
701 | list_add(&pc->lru, &mz->lists[lru]); |
702 | } | |
544122e5 | 703 | |
08e552c6 | 704 | /* |
544122e5 KH |
705 | * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to |
706 | * lru because the page may.be reused after it's fully uncharged (because of | |
707 | * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge | |
708 | * it again. This function is only used to charge SwapCache. It's done under | |
709 | * lock_page and expected that zone->lru_lock is never held. | |
08e552c6 | 710 | */ |
544122e5 | 711 | static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page) |
08e552c6 | 712 | { |
544122e5 KH |
713 | unsigned long flags; |
714 | struct zone *zone = page_zone(page); | |
715 | struct page_cgroup *pc = lookup_page_cgroup(page); | |
716 | ||
717 | spin_lock_irqsave(&zone->lru_lock, flags); | |
718 | /* | |
719 | * Forget old LRU when this page_cgroup is *not* used. This Used bit | |
720 | * is guarded by lock_page() because the page is SwapCache. | |
721 | */ | |
722 | if (!PageCgroupUsed(pc)) | |
723 | mem_cgroup_del_lru_list(page, page_lru(page)); | |
724 | spin_unlock_irqrestore(&zone->lru_lock, flags); | |
08e552c6 KH |
725 | } |
726 | ||
544122e5 KH |
727 | static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page) |
728 | { | |
729 | unsigned long flags; | |
730 | struct zone *zone = page_zone(page); | |
731 | struct page_cgroup *pc = lookup_page_cgroup(page); | |
732 | ||
733 | spin_lock_irqsave(&zone->lru_lock, flags); | |
734 | /* link when the page is linked to LRU but page_cgroup isn't */ | |
4b3bde4c | 735 | if (PageLRU(page) && !PageCgroupAcctLRU(pc)) |
544122e5 KH |
736 | mem_cgroup_add_lru_list(page, page_lru(page)); |
737 | spin_unlock_irqrestore(&zone->lru_lock, flags); | |
738 | } | |
739 | ||
740 | ||
08e552c6 KH |
741 | void mem_cgroup_move_lists(struct page *page, |
742 | enum lru_list from, enum lru_list to) | |
743 | { | |
f8d66542 | 744 | if (mem_cgroup_disabled()) |
08e552c6 KH |
745 | return; |
746 | mem_cgroup_del_lru_list(page, from); | |
747 | mem_cgroup_add_lru_list(page, to); | |
66e1707b BS |
748 | } |
749 | ||
4c4a2214 DR |
750 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
751 | { | |
752 | int ret; | |
0b7f569e | 753 | struct mem_cgroup *curr = NULL; |
4c4a2214 DR |
754 | |
755 | task_lock(task); | |
0b7f569e KH |
756 | rcu_read_lock(); |
757 | curr = try_get_mem_cgroup_from_mm(task->mm); | |
758 | rcu_read_unlock(); | |
4c4a2214 | 759 | task_unlock(task); |
0b7f569e KH |
760 | if (!curr) |
761 | return 0; | |
762 | if (curr->use_hierarchy) | |
763 | ret = css_is_ancestor(&curr->css, &mem->css); | |
764 | else | |
765 | ret = (curr == mem); | |
766 | css_put(&curr->css); | |
4c4a2214 DR |
767 | return ret; |
768 | } | |
769 | ||
6c48a1d0 KH |
770 | /* |
771 | * prev_priority control...this will be used in memory reclaim path. | |
772 | */ | |
773 | int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem) | |
774 | { | |
2733c06a KM |
775 | int prev_priority; |
776 | ||
777 | spin_lock(&mem->reclaim_param_lock); | |
778 | prev_priority = mem->prev_priority; | |
779 | spin_unlock(&mem->reclaim_param_lock); | |
780 | ||
781 | return prev_priority; | |
6c48a1d0 KH |
782 | } |
783 | ||
784 | void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority) | |
785 | { | |
2733c06a | 786 | spin_lock(&mem->reclaim_param_lock); |
6c48a1d0 KH |
787 | if (priority < mem->prev_priority) |
788 | mem->prev_priority = priority; | |
2733c06a | 789 | spin_unlock(&mem->reclaim_param_lock); |
6c48a1d0 KH |
790 | } |
791 | ||
792 | void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority) | |
793 | { | |
2733c06a | 794 | spin_lock(&mem->reclaim_param_lock); |
6c48a1d0 | 795 | mem->prev_priority = priority; |
2733c06a | 796 | spin_unlock(&mem->reclaim_param_lock); |
6c48a1d0 KH |
797 | } |
798 | ||
c772be93 | 799 | static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages) |
14797e23 KM |
800 | { |
801 | unsigned long active; | |
802 | unsigned long inactive; | |
c772be93 KM |
803 | unsigned long gb; |
804 | unsigned long inactive_ratio; | |
14797e23 | 805 | |
14067bb3 KH |
806 | inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON); |
807 | active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON); | |
14797e23 | 808 | |
c772be93 KM |
809 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
810 | if (gb) | |
811 | inactive_ratio = int_sqrt(10 * gb); | |
812 | else | |
813 | inactive_ratio = 1; | |
814 | ||
815 | if (present_pages) { | |
816 | present_pages[0] = inactive; | |
817 | present_pages[1] = active; | |
818 | } | |
819 | ||
820 | return inactive_ratio; | |
821 | } | |
822 | ||
823 | int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg) | |
824 | { | |
825 | unsigned long active; | |
826 | unsigned long inactive; | |
827 | unsigned long present_pages[2]; | |
828 | unsigned long inactive_ratio; | |
829 | ||
830 | inactive_ratio = calc_inactive_ratio(memcg, present_pages); | |
831 | ||
832 | inactive = present_pages[0]; | |
833 | active = present_pages[1]; | |
834 | ||
835 | if (inactive * inactive_ratio < active) | |
14797e23 KM |
836 | return 1; |
837 | ||
838 | return 0; | |
839 | } | |
840 | ||
56e49d21 RR |
841 | int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg) |
842 | { | |
843 | unsigned long active; | |
844 | unsigned long inactive; | |
845 | ||
846 | inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE); | |
847 | active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE); | |
848 | ||
849 | return (active > inactive); | |
850 | } | |
851 | ||
a3d8e054 KM |
852 | unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg, |
853 | struct zone *zone, | |
854 | enum lru_list lru) | |
855 | { | |
856 | int nid = zone->zone_pgdat->node_id; | |
857 | int zid = zone_idx(zone); | |
858 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); | |
859 | ||
860 | return MEM_CGROUP_ZSTAT(mz, lru); | |
861 | } | |
862 | ||
3e2f41f1 KM |
863 | struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg, |
864 | struct zone *zone) | |
865 | { | |
866 | int nid = zone->zone_pgdat->node_id; | |
867 | int zid = zone_idx(zone); | |
868 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); | |
869 | ||
870 | return &mz->reclaim_stat; | |
871 | } | |
872 | ||
873 | struct zone_reclaim_stat * | |
874 | mem_cgroup_get_reclaim_stat_from_page(struct page *page) | |
875 | { | |
876 | struct page_cgroup *pc; | |
877 | struct mem_cgroup_per_zone *mz; | |
878 | ||
879 | if (mem_cgroup_disabled()) | |
880 | return NULL; | |
881 | ||
882 | pc = lookup_page_cgroup(page); | |
bd112db8 DN |
883 | /* |
884 | * Used bit is set without atomic ops but after smp_wmb(). | |
885 | * For making pc->mem_cgroup visible, insert smp_rmb() here. | |
886 | */ | |
887 | smp_rmb(); | |
888 | if (!PageCgroupUsed(pc)) | |
889 | return NULL; | |
890 | ||
3e2f41f1 KM |
891 | mz = page_cgroup_zoneinfo(pc); |
892 | if (!mz) | |
893 | return NULL; | |
894 | ||
895 | return &mz->reclaim_stat; | |
896 | } | |
897 | ||
66e1707b BS |
898 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
899 | struct list_head *dst, | |
900 | unsigned long *scanned, int order, | |
901 | int mode, struct zone *z, | |
902 | struct mem_cgroup *mem_cont, | |
4f98a2fe | 903 | int active, int file) |
66e1707b BS |
904 | { |
905 | unsigned long nr_taken = 0; | |
906 | struct page *page; | |
907 | unsigned long scan; | |
908 | LIST_HEAD(pc_list); | |
909 | struct list_head *src; | |
ff7283fa | 910 | struct page_cgroup *pc, *tmp; |
1ecaab2b KH |
911 | int nid = z->zone_pgdat->node_id; |
912 | int zid = zone_idx(z); | |
913 | struct mem_cgroup_per_zone *mz; | |
b7c46d15 | 914 | int lru = LRU_FILE * file + active; |
2ffebca6 | 915 | int ret; |
66e1707b | 916 | |
cf475ad2 | 917 | BUG_ON(!mem_cont); |
1ecaab2b | 918 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
b69408e8 | 919 | src = &mz->lists[lru]; |
66e1707b | 920 | |
ff7283fa KH |
921 | scan = 0; |
922 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { | |
436c6541 | 923 | if (scan >= nr_to_scan) |
ff7283fa | 924 | break; |
08e552c6 KH |
925 | |
926 | page = pc->page; | |
52d4b9ac KH |
927 | if (unlikely(!PageCgroupUsed(pc))) |
928 | continue; | |
436c6541 | 929 | if (unlikely(!PageLRU(page))) |
ff7283fa | 930 | continue; |
ff7283fa | 931 | |
436c6541 | 932 | scan++; |
2ffebca6 KH |
933 | ret = __isolate_lru_page(page, mode, file); |
934 | switch (ret) { | |
935 | case 0: | |
66e1707b | 936 | list_move(&page->lru, dst); |
2ffebca6 | 937 | mem_cgroup_del_lru(page); |
66e1707b | 938 | nr_taken++; |
2ffebca6 KH |
939 | break; |
940 | case -EBUSY: | |
941 | /* we don't affect global LRU but rotate in our LRU */ | |
942 | mem_cgroup_rotate_lru_list(page, page_lru(page)); | |
943 | break; | |
944 | default: | |
945 | break; | |
66e1707b BS |
946 | } |
947 | } | |
948 | ||
66e1707b BS |
949 | *scanned = scan; |
950 | return nr_taken; | |
951 | } | |
952 | ||
6d61ef40 BS |
953 | #define mem_cgroup_from_res_counter(counter, member) \ |
954 | container_of(counter, struct mem_cgroup, member) | |
955 | ||
b85a96c0 DN |
956 | static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem) |
957 | { | |
958 | if (do_swap_account) { | |
959 | if (res_counter_check_under_limit(&mem->res) && | |
960 | res_counter_check_under_limit(&mem->memsw)) | |
961 | return true; | |
962 | } else | |
963 | if (res_counter_check_under_limit(&mem->res)) | |
964 | return true; | |
965 | return false; | |
966 | } | |
967 | ||
a7885eb8 KM |
968 | static unsigned int get_swappiness(struct mem_cgroup *memcg) |
969 | { | |
970 | struct cgroup *cgrp = memcg->css.cgroup; | |
971 | unsigned int swappiness; | |
972 | ||
973 | /* root ? */ | |
974 | if (cgrp->parent == NULL) | |
975 | return vm_swappiness; | |
976 | ||
977 | spin_lock(&memcg->reclaim_param_lock); | |
978 | swappiness = memcg->swappiness; | |
979 | spin_unlock(&memcg->reclaim_param_lock); | |
980 | ||
981 | return swappiness; | |
982 | } | |
983 | ||
81d39c20 KH |
984 | static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data) |
985 | { | |
986 | int *val = data; | |
987 | (*val)++; | |
988 | return 0; | |
989 | } | |
e222432b BS |
990 | |
991 | /** | |
992 | * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode. | |
993 | * @memcg: The memory cgroup that went over limit | |
994 | * @p: Task that is going to be killed | |
995 | * | |
996 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is | |
997 | * enabled | |
998 | */ | |
999 | void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) | |
1000 | { | |
1001 | struct cgroup *task_cgrp; | |
1002 | struct cgroup *mem_cgrp; | |
1003 | /* | |
1004 | * Need a buffer in BSS, can't rely on allocations. The code relies | |
1005 | * on the assumption that OOM is serialized for memory controller. | |
1006 | * If this assumption is broken, revisit this code. | |
1007 | */ | |
1008 | static char memcg_name[PATH_MAX]; | |
1009 | int ret; | |
1010 | ||
1011 | if (!memcg) | |
1012 | return; | |
1013 | ||
1014 | ||
1015 | rcu_read_lock(); | |
1016 | ||
1017 | mem_cgrp = memcg->css.cgroup; | |
1018 | task_cgrp = task_cgroup(p, mem_cgroup_subsys_id); | |
1019 | ||
1020 | ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX); | |
1021 | if (ret < 0) { | |
1022 | /* | |
1023 | * Unfortunately, we are unable to convert to a useful name | |
1024 | * But we'll still print out the usage information | |
1025 | */ | |
1026 | rcu_read_unlock(); | |
1027 | goto done; | |
1028 | } | |
1029 | rcu_read_unlock(); | |
1030 | ||
1031 | printk(KERN_INFO "Task in %s killed", memcg_name); | |
1032 | ||
1033 | rcu_read_lock(); | |
1034 | ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX); | |
1035 | if (ret < 0) { | |
1036 | rcu_read_unlock(); | |
1037 | goto done; | |
1038 | } | |
1039 | rcu_read_unlock(); | |
1040 | ||
1041 | /* | |
1042 | * Continues from above, so we don't need an KERN_ level | |
1043 | */ | |
1044 | printk(KERN_CONT " as a result of limit of %s\n", memcg_name); | |
1045 | done: | |
1046 | ||
1047 | printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n", | |
1048 | res_counter_read_u64(&memcg->res, RES_USAGE) >> 10, | |
1049 | res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10, | |
1050 | res_counter_read_u64(&memcg->res, RES_FAILCNT)); | |
1051 | printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, " | |
1052 | "failcnt %llu\n", | |
1053 | res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10, | |
1054 | res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10, | |
1055 | res_counter_read_u64(&memcg->memsw, RES_FAILCNT)); | |
1056 | } | |
1057 | ||
81d39c20 KH |
1058 | /* |
1059 | * This function returns the number of memcg under hierarchy tree. Returns | |
1060 | * 1(self count) if no children. | |
1061 | */ | |
1062 | static int mem_cgroup_count_children(struct mem_cgroup *mem) | |
1063 | { | |
1064 | int num = 0; | |
1065 | mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb); | |
1066 | return num; | |
1067 | } | |
1068 | ||
6d61ef40 | 1069 | /* |
04046e1a KH |
1070 | * Visit the first child (need not be the first child as per the ordering |
1071 | * of the cgroup list, since we track last_scanned_child) of @mem and use | |
1072 | * that to reclaim free pages from. | |
1073 | */ | |
1074 | static struct mem_cgroup * | |
1075 | mem_cgroup_select_victim(struct mem_cgroup *root_mem) | |
1076 | { | |
1077 | struct mem_cgroup *ret = NULL; | |
1078 | struct cgroup_subsys_state *css; | |
1079 | int nextid, found; | |
1080 | ||
1081 | if (!root_mem->use_hierarchy) { | |
1082 | css_get(&root_mem->css); | |
1083 | ret = root_mem; | |
1084 | } | |
1085 | ||
1086 | while (!ret) { | |
1087 | rcu_read_lock(); | |
1088 | nextid = root_mem->last_scanned_child + 1; | |
1089 | css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css, | |
1090 | &found); | |
1091 | if (css && css_tryget(css)) | |
1092 | ret = container_of(css, struct mem_cgroup, css); | |
1093 | ||
1094 | rcu_read_unlock(); | |
1095 | /* Updates scanning parameter */ | |
1096 | spin_lock(&root_mem->reclaim_param_lock); | |
1097 | if (!css) { | |
1098 | /* this means start scan from ID:1 */ | |
1099 | root_mem->last_scanned_child = 0; | |
1100 | } else | |
1101 | root_mem->last_scanned_child = found; | |
1102 | spin_unlock(&root_mem->reclaim_param_lock); | |
1103 | } | |
1104 | ||
1105 | return ret; | |
1106 | } | |
1107 | ||
1108 | /* | |
1109 | * Scan the hierarchy if needed to reclaim memory. We remember the last child | |
1110 | * we reclaimed from, so that we don't end up penalizing one child extensively | |
1111 | * based on its position in the children list. | |
6d61ef40 BS |
1112 | * |
1113 | * root_mem is the original ancestor that we've been reclaim from. | |
04046e1a KH |
1114 | * |
1115 | * We give up and return to the caller when we visit root_mem twice. | |
1116 | * (other groups can be removed while we're walking....) | |
81d39c20 KH |
1117 | * |
1118 | * If shrink==true, for avoiding to free too much, this returns immedieately. | |
6d61ef40 BS |
1119 | */ |
1120 | static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem, | |
4e416953 | 1121 | struct zone *zone, |
75822b44 BS |
1122 | gfp_t gfp_mask, |
1123 | unsigned long reclaim_options) | |
6d61ef40 | 1124 | { |
04046e1a KH |
1125 | struct mem_cgroup *victim; |
1126 | int ret, total = 0; | |
1127 | int loop = 0; | |
75822b44 BS |
1128 | bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP; |
1129 | bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK; | |
4e416953 BS |
1130 | bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT; |
1131 | unsigned long excess = mem_cgroup_get_excess(root_mem); | |
04046e1a | 1132 | |
22a668d7 KH |
1133 | /* If memsw_is_minimum==1, swap-out is of-no-use. */ |
1134 | if (root_mem->memsw_is_minimum) | |
1135 | noswap = true; | |
1136 | ||
4e416953 | 1137 | while (1) { |
04046e1a | 1138 | victim = mem_cgroup_select_victim(root_mem); |
4e416953 | 1139 | if (victim == root_mem) { |
04046e1a | 1140 | loop++; |
4e416953 BS |
1141 | if (loop >= 2) { |
1142 | /* | |
1143 | * If we have not been able to reclaim | |
1144 | * anything, it might because there are | |
1145 | * no reclaimable pages under this hierarchy | |
1146 | */ | |
1147 | if (!check_soft || !total) { | |
1148 | css_put(&victim->css); | |
1149 | break; | |
1150 | } | |
1151 | /* | |
1152 | * We want to do more targetted reclaim. | |
1153 | * excess >> 2 is not to excessive so as to | |
1154 | * reclaim too much, nor too less that we keep | |
1155 | * coming back to reclaim from this cgroup | |
1156 | */ | |
1157 | if (total >= (excess >> 2) || | |
1158 | (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) { | |
1159 | css_put(&victim->css); | |
1160 | break; | |
1161 | } | |
1162 | } | |
1163 | } | |
04046e1a KH |
1164 | if (!mem_cgroup_local_usage(&victim->stat)) { |
1165 | /* this cgroup's local usage == 0 */ | |
1166 | css_put(&victim->css); | |
6d61ef40 BS |
1167 | continue; |
1168 | } | |
04046e1a | 1169 | /* we use swappiness of local cgroup */ |
4e416953 BS |
1170 | if (check_soft) |
1171 | ret = mem_cgroup_shrink_node_zone(victim, gfp_mask, | |
1172 | noswap, get_swappiness(victim), zone, | |
1173 | zone->zone_pgdat->node_id); | |
1174 | else | |
1175 | ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, | |
1176 | noswap, get_swappiness(victim)); | |
04046e1a | 1177 | css_put(&victim->css); |
81d39c20 KH |
1178 | /* |
1179 | * At shrinking usage, we can't check we should stop here or | |
1180 | * reclaim more. It's depends on callers. last_scanned_child | |
1181 | * will work enough for keeping fairness under tree. | |
1182 | */ | |
1183 | if (shrink) | |
1184 | return ret; | |
04046e1a | 1185 | total += ret; |
4e416953 BS |
1186 | if (check_soft) { |
1187 | if (res_counter_check_under_soft_limit(&root_mem->res)) | |
1188 | return total; | |
1189 | } else if (mem_cgroup_check_under_limit(root_mem)) | |
04046e1a | 1190 | return 1 + total; |
6d61ef40 | 1191 | } |
04046e1a | 1192 | return total; |
6d61ef40 BS |
1193 | } |
1194 | ||
a636b327 KH |
1195 | bool mem_cgroup_oom_called(struct task_struct *task) |
1196 | { | |
1197 | bool ret = false; | |
1198 | struct mem_cgroup *mem; | |
1199 | struct mm_struct *mm; | |
1200 | ||
1201 | rcu_read_lock(); | |
1202 | mm = task->mm; | |
1203 | if (!mm) | |
1204 | mm = &init_mm; | |
1205 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); | |
1206 | if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10)) | |
1207 | ret = true; | |
1208 | rcu_read_unlock(); | |
1209 | return ret; | |
1210 | } | |
0b7f569e KH |
1211 | |
1212 | static int record_last_oom_cb(struct mem_cgroup *mem, void *data) | |
1213 | { | |
1214 | mem->last_oom_jiffies = jiffies; | |
1215 | return 0; | |
1216 | } | |
1217 | ||
1218 | static void record_last_oom(struct mem_cgroup *mem) | |
1219 | { | |
1220 | mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb); | |
1221 | } | |
1222 | ||
d69b042f BS |
1223 | /* |
1224 | * Currently used to update mapped file statistics, but the routine can be | |
1225 | * generalized to update other statistics as well. | |
1226 | */ | |
1227 | void mem_cgroup_update_mapped_file_stat(struct page *page, int val) | |
1228 | { | |
1229 | struct mem_cgroup *mem; | |
1230 | struct mem_cgroup_stat *stat; | |
1231 | struct mem_cgroup_stat_cpu *cpustat; | |
1232 | int cpu; | |
1233 | struct page_cgroup *pc; | |
1234 | ||
1235 | if (!page_is_file_cache(page)) | |
1236 | return; | |
1237 | ||
1238 | pc = lookup_page_cgroup(page); | |
1239 | if (unlikely(!pc)) | |
1240 | return; | |
1241 | ||
1242 | lock_page_cgroup(pc); | |
1243 | mem = pc->mem_cgroup; | |
1244 | if (!mem) | |
1245 | goto done; | |
1246 | ||
1247 | if (!PageCgroupUsed(pc)) | |
1248 | goto done; | |
1249 | ||
1250 | /* | |
1251 | * Preemption is already disabled, we don't need get_cpu() | |
1252 | */ | |
1253 | cpu = smp_processor_id(); | |
1254 | stat = &mem->stat; | |
1255 | cpustat = &stat->cpustat[cpu]; | |
1256 | ||
1257 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, val); | |
1258 | done: | |
1259 | unlock_page_cgroup(pc); | |
1260 | } | |
0b7f569e | 1261 | |
f817ed48 KH |
1262 | /* |
1263 | * Unlike exported interface, "oom" parameter is added. if oom==true, | |
1264 | * oom-killer can be invoked. | |
8a9f3ccd | 1265 | */ |
f817ed48 | 1266 | static int __mem_cgroup_try_charge(struct mm_struct *mm, |
8c7c6e34 | 1267 | gfp_t gfp_mask, struct mem_cgroup **memcg, |
f64c3f54 | 1268 | bool oom, struct page *page) |
8a9f3ccd | 1269 | { |
4e649152 | 1270 | struct mem_cgroup *mem, *mem_over_limit; |
7a81b88c | 1271 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
4e649152 | 1272 | struct res_counter *fail_res; |
a636b327 KH |
1273 | |
1274 | if (unlikely(test_thread_flag(TIF_MEMDIE))) { | |
1275 | /* Don't account this! */ | |
1276 | *memcg = NULL; | |
1277 | return 0; | |
1278 | } | |
1279 | ||
8a9f3ccd | 1280 | /* |
3be91277 HD |
1281 | * We always charge the cgroup the mm_struct belongs to. |
1282 | * The mm_struct's mem_cgroup changes on task migration if the | |
8a9f3ccd BS |
1283 | * thread group leader migrates. It's possible that mm is not |
1284 | * set, if so charge the init_mm (happens for pagecache usage). | |
1285 | */ | |
54595fe2 KH |
1286 | mem = *memcg; |
1287 | if (likely(!mem)) { | |
1288 | mem = try_get_mem_cgroup_from_mm(mm); | |
7a81b88c | 1289 | *memcg = mem; |
e8589cc1 | 1290 | } else { |
7a81b88c | 1291 | css_get(&mem->css); |
e8589cc1 | 1292 | } |
54595fe2 KH |
1293 | if (unlikely(!mem)) |
1294 | return 0; | |
1295 | ||
46f7e602 | 1296 | VM_BUG_ON(css_is_removed(&mem->css)); |
8a9f3ccd | 1297 | |
8c7c6e34 | 1298 | while (1) { |
0c3e73e8 | 1299 | int ret = 0; |
75822b44 | 1300 | unsigned long flags = 0; |
7a81b88c | 1301 | |
0c3e73e8 BS |
1302 | if (mem_cgroup_is_root(mem)) |
1303 | goto done; | |
4e649152 | 1304 | ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res); |
8c7c6e34 KH |
1305 | if (likely(!ret)) { |
1306 | if (!do_swap_account) | |
1307 | break; | |
28dbc4b6 | 1308 | ret = res_counter_charge(&mem->memsw, PAGE_SIZE, |
4e649152 | 1309 | &fail_res); |
8c7c6e34 KH |
1310 | if (likely(!ret)) |
1311 | break; | |
1312 | /* mem+swap counter fails */ | |
4e649152 | 1313 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
75822b44 | 1314 | flags |= MEM_CGROUP_RECLAIM_NOSWAP; |
6d61ef40 BS |
1315 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, |
1316 | memsw); | |
1317 | } else | |
1318 | /* mem counter fails */ | |
1319 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, | |
1320 | res); | |
1321 | ||
3be91277 | 1322 | if (!(gfp_mask & __GFP_WAIT)) |
7a81b88c | 1323 | goto nomem; |
e1a1cd59 | 1324 | |
4e416953 BS |
1325 | ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL, |
1326 | gfp_mask, flags); | |
4d1c6273 DN |
1327 | if (ret) |
1328 | continue; | |
66e1707b BS |
1329 | |
1330 | /* | |
8869b8f6 HD |
1331 | * try_to_free_mem_cgroup_pages() might not give us a full |
1332 | * picture of reclaim. Some pages are reclaimed and might be | |
1333 | * moved to swap cache or just unmapped from the cgroup. | |
1334 | * Check the limit again to see if the reclaim reduced the | |
1335 | * current usage of the cgroup before giving up | |
8c7c6e34 | 1336 | * |
8869b8f6 | 1337 | */ |
b85a96c0 DN |
1338 | if (mem_cgroup_check_under_limit(mem_over_limit)) |
1339 | continue; | |
3be91277 HD |
1340 | |
1341 | if (!nr_retries--) { | |
a636b327 | 1342 | if (oom) { |
7f4d454d | 1343 | mutex_lock(&memcg_tasklist); |
88700756 | 1344 | mem_cgroup_out_of_memory(mem_over_limit, gfp_mask); |
7f4d454d | 1345 | mutex_unlock(&memcg_tasklist); |
0b7f569e | 1346 | record_last_oom(mem_over_limit); |
a636b327 | 1347 | } |
7a81b88c | 1348 | goto nomem; |
66e1707b | 1349 | } |
8a9f3ccd | 1350 | } |
f64c3f54 | 1351 | /* |
4e649152 KH |
1352 | * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree. |
1353 | * if they exceeds softlimit. | |
f64c3f54 | 1354 | */ |
4e649152 KH |
1355 | if (mem_cgroup_soft_limit_check(mem)) |
1356 | mem_cgroup_update_tree(mem, page); | |
0c3e73e8 | 1357 | done: |
7a81b88c KH |
1358 | return 0; |
1359 | nomem: | |
1360 | css_put(&mem->css); | |
1361 | return -ENOMEM; | |
1362 | } | |
8a9f3ccd | 1363 | |
a3b2d692 KH |
1364 | /* |
1365 | * A helper function to get mem_cgroup from ID. must be called under | |
1366 | * rcu_read_lock(). The caller must check css_is_removed() or some if | |
1367 | * it's concern. (dropping refcnt from swap can be called against removed | |
1368 | * memcg.) | |
1369 | */ | |
1370 | static struct mem_cgroup *mem_cgroup_lookup(unsigned short id) | |
1371 | { | |
1372 | struct cgroup_subsys_state *css; | |
1373 | ||
1374 | /* ID 0 is unused ID */ | |
1375 | if (!id) | |
1376 | return NULL; | |
1377 | css = css_lookup(&mem_cgroup_subsys, id); | |
1378 | if (!css) | |
1379 | return NULL; | |
1380 | return container_of(css, struct mem_cgroup, css); | |
1381 | } | |
1382 | ||
b5a84319 KH |
1383 | static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page) |
1384 | { | |
1385 | struct mem_cgroup *mem; | |
3c776e64 | 1386 | struct page_cgroup *pc; |
a3b2d692 | 1387 | unsigned short id; |
b5a84319 KH |
1388 | swp_entry_t ent; |
1389 | ||
3c776e64 DN |
1390 | VM_BUG_ON(!PageLocked(page)); |
1391 | ||
b5a84319 KH |
1392 | if (!PageSwapCache(page)) |
1393 | return NULL; | |
1394 | ||
3c776e64 | 1395 | pc = lookup_page_cgroup(page); |
c0bd3f63 | 1396 | lock_page_cgroup(pc); |
a3b2d692 | 1397 | if (PageCgroupUsed(pc)) { |
3c776e64 | 1398 | mem = pc->mem_cgroup; |
a3b2d692 KH |
1399 | if (mem && !css_tryget(&mem->css)) |
1400 | mem = NULL; | |
1401 | } else { | |
3c776e64 | 1402 | ent.val = page_private(page); |
a3b2d692 KH |
1403 | id = lookup_swap_cgroup(ent); |
1404 | rcu_read_lock(); | |
1405 | mem = mem_cgroup_lookup(id); | |
1406 | if (mem && !css_tryget(&mem->css)) | |
1407 | mem = NULL; | |
1408 | rcu_read_unlock(); | |
3c776e64 | 1409 | } |
c0bd3f63 | 1410 | unlock_page_cgroup(pc); |
b5a84319 KH |
1411 | return mem; |
1412 | } | |
1413 | ||
7a81b88c | 1414 | /* |
a5e924f5 | 1415 | * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be |
7a81b88c KH |
1416 | * USED state. If already USED, uncharge and return. |
1417 | */ | |
1418 | ||
1419 | static void __mem_cgroup_commit_charge(struct mem_cgroup *mem, | |
1420 | struct page_cgroup *pc, | |
1421 | enum charge_type ctype) | |
1422 | { | |
7a81b88c KH |
1423 | /* try_charge() can return NULL to *memcg, taking care of it. */ |
1424 | if (!mem) | |
1425 | return; | |
52d4b9ac KH |
1426 | |
1427 | lock_page_cgroup(pc); | |
1428 | if (unlikely(PageCgroupUsed(pc))) { | |
1429 | unlock_page_cgroup(pc); | |
0c3e73e8 | 1430 | if (!mem_cgroup_is_root(mem)) { |
4e649152 | 1431 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
0c3e73e8 | 1432 | if (do_swap_account) |
4e649152 | 1433 | res_counter_uncharge(&mem->memsw, PAGE_SIZE); |
0c3e73e8 | 1434 | } |
52d4b9ac | 1435 | css_put(&mem->css); |
7a81b88c | 1436 | return; |
52d4b9ac | 1437 | } |
4b3bde4c | 1438 | |
8a9f3ccd | 1439 | pc->mem_cgroup = mem; |
261fb61a KH |
1440 | /* |
1441 | * We access a page_cgroup asynchronously without lock_page_cgroup(). | |
1442 | * Especially when a page_cgroup is taken from a page, pc->mem_cgroup | |
1443 | * is accessed after testing USED bit. To make pc->mem_cgroup visible | |
1444 | * before USED bit, we need memory barrier here. | |
1445 | * See mem_cgroup_add_lru_list(), etc. | |
1446 | */ | |
08e552c6 | 1447 | smp_wmb(); |
4b3bde4c BS |
1448 | switch (ctype) { |
1449 | case MEM_CGROUP_CHARGE_TYPE_CACHE: | |
1450 | case MEM_CGROUP_CHARGE_TYPE_SHMEM: | |
1451 | SetPageCgroupCache(pc); | |
1452 | SetPageCgroupUsed(pc); | |
1453 | break; | |
1454 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: | |
1455 | ClearPageCgroupCache(pc); | |
1456 | SetPageCgroupUsed(pc); | |
1457 | break; | |
1458 | default: | |
1459 | break; | |
1460 | } | |
3be91277 | 1461 | |
08e552c6 | 1462 | mem_cgroup_charge_statistics(mem, pc, true); |
52d4b9ac | 1463 | |
52d4b9ac | 1464 | unlock_page_cgroup(pc); |
7a81b88c | 1465 | } |
66e1707b | 1466 | |
f817ed48 KH |
1467 | /** |
1468 | * mem_cgroup_move_account - move account of the page | |
1469 | * @pc: page_cgroup of the page. | |
1470 | * @from: mem_cgroup which the page is moved from. | |
1471 | * @to: mem_cgroup which the page is moved to. @from != @to. | |
1472 | * | |
1473 | * The caller must confirm following. | |
08e552c6 | 1474 | * - page is not on LRU (isolate_page() is useful.) |
f817ed48 KH |
1475 | * |
1476 | * returns 0 at success, | |
1477 | * returns -EBUSY when lock is busy or "pc" is unstable. | |
1478 | * | |
1479 | * This function does "uncharge" from old cgroup but doesn't do "charge" to | |
1480 | * new cgroup. It should be done by a caller. | |
1481 | */ | |
1482 | ||
1483 | static int mem_cgroup_move_account(struct page_cgroup *pc, | |
1484 | struct mem_cgroup *from, struct mem_cgroup *to) | |
1485 | { | |
1486 | struct mem_cgroup_per_zone *from_mz, *to_mz; | |
1487 | int nid, zid; | |
1488 | int ret = -EBUSY; | |
d69b042f BS |
1489 | struct page *page; |
1490 | int cpu; | |
1491 | struct mem_cgroup_stat *stat; | |
1492 | struct mem_cgroup_stat_cpu *cpustat; | |
f817ed48 | 1493 | |
f817ed48 | 1494 | VM_BUG_ON(from == to); |
08e552c6 | 1495 | VM_BUG_ON(PageLRU(pc->page)); |
f817ed48 KH |
1496 | |
1497 | nid = page_cgroup_nid(pc); | |
1498 | zid = page_cgroup_zid(pc); | |
1499 | from_mz = mem_cgroup_zoneinfo(from, nid, zid); | |
1500 | to_mz = mem_cgroup_zoneinfo(to, nid, zid); | |
1501 | ||
f817ed48 KH |
1502 | if (!trylock_page_cgroup(pc)) |
1503 | return ret; | |
1504 | ||
1505 | if (!PageCgroupUsed(pc)) | |
1506 | goto out; | |
1507 | ||
1508 | if (pc->mem_cgroup != from) | |
1509 | goto out; | |
1510 | ||
0c3e73e8 | 1511 | if (!mem_cgroup_is_root(from)) |
4e649152 | 1512 | res_counter_uncharge(&from->res, PAGE_SIZE); |
08e552c6 | 1513 | mem_cgroup_charge_statistics(from, pc, false); |
d69b042f BS |
1514 | |
1515 | page = pc->page; | |
1516 | if (page_is_file_cache(page) && page_mapped(page)) { | |
1517 | cpu = smp_processor_id(); | |
1518 | /* Update mapped_file data for mem_cgroup "from" */ | |
1519 | stat = &from->stat; | |
1520 | cpustat = &stat->cpustat[cpu]; | |
1521 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, | |
1522 | -1); | |
1523 | ||
1524 | /* Update mapped_file data for mem_cgroup "to" */ | |
1525 | stat = &to->stat; | |
1526 | cpustat = &stat->cpustat[cpu]; | |
1527 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, | |
1528 | 1); | |
1529 | } | |
1530 | ||
0c3e73e8 | 1531 | if (do_swap_account && !mem_cgroup_is_root(from)) |
4e649152 | 1532 | res_counter_uncharge(&from->memsw, PAGE_SIZE); |
40d58138 DN |
1533 | css_put(&from->css); |
1534 | ||
1535 | css_get(&to->css); | |
08e552c6 KH |
1536 | pc->mem_cgroup = to; |
1537 | mem_cgroup_charge_statistics(to, pc, true); | |
08e552c6 | 1538 | ret = 0; |
f817ed48 KH |
1539 | out: |
1540 | unlock_page_cgroup(pc); | |
88703267 KH |
1541 | /* |
1542 | * We charges against "to" which may not have any tasks. Then, "to" | |
1543 | * can be under rmdir(). But in current implementation, caller of | |
1544 | * this function is just force_empty() and it's garanteed that | |
1545 | * "to" is never removed. So, we don't check rmdir status here. | |
1546 | */ | |
f817ed48 KH |
1547 | return ret; |
1548 | } | |
1549 | ||
1550 | /* | |
1551 | * move charges to its parent. | |
1552 | */ | |
1553 | ||
1554 | static int mem_cgroup_move_parent(struct page_cgroup *pc, | |
1555 | struct mem_cgroup *child, | |
1556 | gfp_t gfp_mask) | |
1557 | { | |
08e552c6 | 1558 | struct page *page = pc->page; |
f817ed48 KH |
1559 | struct cgroup *cg = child->css.cgroup; |
1560 | struct cgroup *pcg = cg->parent; | |
1561 | struct mem_cgroup *parent; | |
f817ed48 KH |
1562 | int ret; |
1563 | ||
1564 | /* Is ROOT ? */ | |
1565 | if (!pcg) | |
1566 | return -EINVAL; | |
1567 | ||
08e552c6 | 1568 | |
f817ed48 KH |
1569 | parent = mem_cgroup_from_cont(pcg); |
1570 | ||
08e552c6 | 1571 | |
f64c3f54 | 1572 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page); |
a636b327 | 1573 | if (ret || !parent) |
f817ed48 KH |
1574 | return ret; |
1575 | ||
40d58138 DN |
1576 | if (!get_page_unless_zero(page)) { |
1577 | ret = -EBUSY; | |
1578 | goto uncharge; | |
1579 | } | |
08e552c6 KH |
1580 | |
1581 | ret = isolate_lru_page(page); | |
1582 | ||
1583 | if (ret) | |
1584 | goto cancel; | |
f817ed48 | 1585 | |
f817ed48 | 1586 | ret = mem_cgroup_move_account(pc, child, parent); |
f817ed48 | 1587 | |
08e552c6 KH |
1588 | putback_lru_page(page); |
1589 | if (!ret) { | |
1590 | put_page(page); | |
40d58138 DN |
1591 | /* drop extra refcnt by try_charge() */ |
1592 | css_put(&parent->css); | |
08e552c6 | 1593 | return 0; |
8c7c6e34 | 1594 | } |
40d58138 | 1595 | |
08e552c6 | 1596 | cancel: |
40d58138 DN |
1597 | put_page(page); |
1598 | uncharge: | |
1599 | /* drop extra refcnt by try_charge() */ | |
1600 | css_put(&parent->css); | |
1601 | /* uncharge if move fails */ | |
0c3e73e8 | 1602 | if (!mem_cgroup_is_root(parent)) { |
4e649152 | 1603 | res_counter_uncharge(&parent->res, PAGE_SIZE); |
0c3e73e8 | 1604 | if (do_swap_account) |
4e649152 | 1605 | res_counter_uncharge(&parent->memsw, PAGE_SIZE); |
0c3e73e8 | 1606 | } |
f817ed48 KH |
1607 | return ret; |
1608 | } | |
1609 | ||
7a81b88c KH |
1610 | /* |
1611 | * Charge the memory controller for page usage. | |
1612 | * Return | |
1613 | * 0 if the charge was successful | |
1614 | * < 0 if the cgroup is over its limit | |
1615 | */ | |
1616 | static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm, | |
1617 | gfp_t gfp_mask, enum charge_type ctype, | |
1618 | struct mem_cgroup *memcg) | |
1619 | { | |
1620 | struct mem_cgroup *mem; | |
1621 | struct page_cgroup *pc; | |
1622 | int ret; | |
1623 | ||
1624 | pc = lookup_page_cgroup(page); | |
1625 | /* can happen at boot */ | |
1626 | if (unlikely(!pc)) | |
1627 | return 0; | |
1628 | prefetchw(pc); | |
1629 | ||
1630 | mem = memcg; | |
f64c3f54 | 1631 | ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page); |
a636b327 | 1632 | if (ret || !mem) |
7a81b88c KH |
1633 | return ret; |
1634 | ||
1635 | __mem_cgroup_commit_charge(mem, pc, ctype); | |
8a9f3ccd | 1636 | return 0; |
8a9f3ccd BS |
1637 | } |
1638 | ||
7a81b88c KH |
1639 | int mem_cgroup_newpage_charge(struct page *page, |
1640 | struct mm_struct *mm, gfp_t gfp_mask) | |
217bc319 | 1641 | { |
f8d66542 | 1642 | if (mem_cgroup_disabled()) |
cede86ac | 1643 | return 0; |
52d4b9ac KH |
1644 | if (PageCompound(page)) |
1645 | return 0; | |
69029cd5 KH |
1646 | /* |
1647 | * If already mapped, we don't have to account. | |
1648 | * If page cache, page->mapping has address_space. | |
1649 | * But page->mapping may have out-of-use anon_vma pointer, | |
1650 | * detecit it by PageAnon() check. newly-mapped-anon's page->mapping | |
1651 | * is NULL. | |
1652 | */ | |
1653 | if (page_mapped(page) || (page->mapping && !PageAnon(page))) | |
1654 | return 0; | |
1655 | if (unlikely(!mm)) | |
1656 | mm = &init_mm; | |
217bc319 | 1657 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
e8589cc1 | 1658 | MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL); |
217bc319 KH |
1659 | } |
1660 | ||
83aae4c7 DN |
1661 | static void |
1662 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, | |
1663 | enum charge_type ctype); | |
1664 | ||
e1a1cd59 BS |
1665 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
1666 | gfp_t gfp_mask) | |
8697d331 | 1667 | { |
b5a84319 KH |
1668 | struct mem_cgroup *mem = NULL; |
1669 | int ret; | |
1670 | ||
f8d66542 | 1671 | if (mem_cgroup_disabled()) |
cede86ac | 1672 | return 0; |
52d4b9ac KH |
1673 | if (PageCompound(page)) |
1674 | return 0; | |
accf163e KH |
1675 | /* |
1676 | * Corner case handling. This is called from add_to_page_cache() | |
1677 | * in usual. But some FS (shmem) precharges this page before calling it | |
1678 | * and call add_to_page_cache() with GFP_NOWAIT. | |
1679 | * | |
1680 | * For GFP_NOWAIT case, the page may be pre-charged before calling | |
1681 | * add_to_page_cache(). (See shmem.c) check it here and avoid to call | |
1682 | * charge twice. (It works but has to pay a bit larger cost.) | |
b5a84319 KH |
1683 | * And when the page is SwapCache, it should take swap information |
1684 | * into account. This is under lock_page() now. | |
accf163e KH |
1685 | */ |
1686 | if (!(gfp_mask & __GFP_WAIT)) { | |
1687 | struct page_cgroup *pc; | |
1688 | ||
52d4b9ac KH |
1689 | |
1690 | pc = lookup_page_cgroup(page); | |
1691 | if (!pc) | |
1692 | return 0; | |
1693 | lock_page_cgroup(pc); | |
1694 | if (PageCgroupUsed(pc)) { | |
1695 | unlock_page_cgroup(pc); | |
accf163e KH |
1696 | return 0; |
1697 | } | |
52d4b9ac | 1698 | unlock_page_cgroup(pc); |
accf163e KH |
1699 | } |
1700 | ||
b5a84319 | 1701 | if (unlikely(!mm && !mem)) |
8697d331 | 1702 | mm = &init_mm; |
accf163e | 1703 | |
c05555b5 KH |
1704 | if (page_is_file_cache(page)) |
1705 | return mem_cgroup_charge_common(page, mm, gfp_mask, | |
e8589cc1 | 1706 | MEM_CGROUP_CHARGE_TYPE_CACHE, NULL); |
b5a84319 | 1707 | |
83aae4c7 DN |
1708 | /* shmem */ |
1709 | if (PageSwapCache(page)) { | |
1710 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); | |
1711 | if (!ret) | |
1712 | __mem_cgroup_commit_charge_swapin(page, mem, | |
1713 | MEM_CGROUP_CHARGE_TYPE_SHMEM); | |
1714 | } else | |
1715 | ret = mem_cgroup_charge_common(page, mm, gfp_mask, | |
1716 | MEM_CGROUP_CHARGE_TYPE_SHMEM, mem); | |
b5a84319 | 1717 | |
b5a84319 | 1718 | return ret; |
e8589cc1 KH |
1719 | } |
1720 | ||
54595fe2 KH |
1721 | /* |
1722 | * While swap-in, try_charge -> commit or cancel, the page is locked. | |
1723 | * And when try_charge() successfully returns, one refcnt to memcg without | |
1724 | * struct page_cgroup is aquired. This refcnt will be cumsumed by | |
1725 | * "commit()" or removed by "cancel()" | |
1726 | */ | |
8c7c6e34 KH |
1727 | int mem_cgroup_try_charge_swapin(struct mm_struct *mm, |
1728 | struct page *page, | |
1729 | gfp_t mask, struct mem_cgroup **ptr) | |
1730 | { | |
1731 | struct mem_cgroup *mem; | |
54595fe2 | 1732 | int ret; |
8c7c6e34 | 1733 | |
f8d66542 | 1734 | if (mem_cgroup_disabled()) |
8c7c6e34 KH |
1735 | return 0; |
1736 | ||
1737 | if (!do_swap_account) | |
1738 | goto charge_cur_mm; | |
8c7c6e34 KH |
1739 | /* |
1740 | * A racing thread's fault, or swapoff, may have already updated | |
1741 | * the pte, and even removed page from swap cache: return success | |
1742 | * to go on to do_swap_page()'s pte_same() test, which should fail. | |
1743 | */ | |
1744 | if (!PageSwapCache(page)) | |
1745 | return 0; | |
b5a84319 | 1746 | mem = try_get_mem_cgroup_from_swapcache(page); |
54595fe2 KH |
1747 | if (!mem) |
1748 | goto charge_cur_mm; | |
8c7c6e34 | 1749 | *ptr = mem; |
f64c3f54 | 1750 | ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page); |
54595fe2 KH |
1751 | /* drop extra refcnt from tryget */ |
1752 | css_put(&mem->css); | |
1753 | return ret; | |
8c7c6e34 KH |
1754 | charge_cur_mm: |
1755 | if (unlikely(!mm)) | |
1756 | mm = &init_mm; | |
f64c3f54 | 1757 | return __mem_cgroup_try_charge(mm, mask, ptr, true, page); |
8c7c6e34 KH |
1758 | } |
1759 | ||
83aae4c7 DN |
1760 | static void |
1761 | __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr, | |
1762 | enum charge_type ctype) | |
7a81b88c KH |
1763 | { |
1764 | struct page_cgroup *pc; | |
1765 | ||
f8d66542 | 1766 | if (mem_cgroup_disabled()) |
7a81b88c KH |
1767 | return; |
1768 | if (!ptr) | |
1769 | return; | |
88703267 | 1770 | cgroup_exclude_rmdir(&ptr->css); |
7a81b88c | 1771 | pc = lookup_page_cgroup(page); |
544122e5 | 1772 | mem_cgroup_lru_del_before_commit_swapcache(page); |
83aae4c7 | 1773 | __mem_cgroup_commit_charge(ptr, pc, ctype); |
544122e5 | 1774 | mem_cgroup_lru_add_after_commit_swapcache(page); |
8c7c6e34 KH |
1775 | /* |
1776 | * Now swap is on-memory. This means this page may be | |
1777 | * counted both as mem and swap....double count. | |
03f3c433 KH |
1778 | * Fix it by uncharging from memsw. Basically, this SwapCache is stable |
1779 | * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page() | |
1780 | * may call delete_from_swap_cache() before reach here. | |
8c7c6e34 | 1781 | */ |
03f3c433 | 1782 | if (do_swap_account && PageSwapCache(page)) { |
8c7c6e34 | 1783 | swp_entry_t ent = {.val = page_private(page)}; |
a3b2d692 | 1784 | unsigned short id; |
8c7c6e34 | 1785 | struct mem_cgroup *memcg; |
a3b2d692 KH |
1786 | |
1787 | id = swap_cgroup_record(ent, 0); | |
1788 | rcu_read_lock(); | |
1789 | memcg = mem_cgroup_lookup(id); | |
8c7c6e34 | 1790 | if (memcg) { |
a3b2d692 KH |
1791 | /* |
1792 | * This recorded memcg can be obsolete one. So, avoid | |
1793 | * calling css_tryget | |
1794 | */ | |
0c3e73e8 | 1795 | if (!mem_cgroup_is_root(memcg)) |
4e649152 | 1796 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
0c3e73e8 | 1797 | mem_cgroup_swap_statistics(memcg, false); |
8c7c6e34 KH |
1798 | mem_cgroup_put(memcg); |
1799 | } | |
a3b2d692 | 1800 | rcu_read_unlock(); |
8c7c6e34 | 1801 | } |
88703267 KH |
1802 | /* |
1803 | * At swapin, we may charge account against cgroup which has no tasks. | |
1804 | * So, rmdir()->pre_destroy() can be called while we do this charge. | |
1805 | * In that case, we need to call pre_destroy() again. check it here. | |
1806 | */ | |
1807 | cgroup_release_and_wakeup_rmdir(&ptr->css); | |
7a81b88c KH |
1808 | } |
1809 | ||
83aae4c7 DN |
1810 | void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr) |
1811 | { | |
1812 | __mem_cgroup_commit_charge_swapin(page, ptr, | |
1813 | MEM_CGROUP_CHARGE_TYPE_MAPPED); | |
1814 | } | |
1815 | ||
7a81b88c KH |
1816 | void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem) |
1817 | { | |
f8d66542 | 1818 | if (mem_cgroup_disabled()) |
7a81b88c KH |
1819 | return; |
1820 | if (!mem) | |
1821 | return; | |
0c3e73e8 | 1822 | if (!mem_cgroup_is_root(mem)) { |
4e649152 | 1823 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
0c3e73e8 | 1824 | if (do_swap_account) |
4e649152 | 1825 | res_counter_uncharge(&mem->memsw, PAGE_SIZE); |
0c3e73e8 | 1826 | } |
7a81b88c KH |
1827 | css_put(&mem->css); |
1828 | } | |
1829 | ||
1830 | ||
8a9f3ccd | 1831 | /* |
69029cd5 | 1832 | * uncharge if !page_mapped(page) |
8a9f3ccd | 1833 | */ |
8c7c6e34 | 1834 | static struct mem_cgroup * |
69029cd5 | 1835 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype) |
8a9f3ccd | 1836 | { |
8289546e | 1837 | struct page_cgroup *pc; |
8c7c6e34 | 1838 | struct mem_cgroup *mem = NULL; |
072c56c1 | 1839 | struct mem_cgroup_per_zone *mz; |
8a9f3ccd | 1840 | |
f8d66542 | 1841 | if (mem_cgroup_disabled()) |
8c7c6e34 | 1842 | return NULL; |
4077960e | 1843 | |
d13d1443 | 1844 | if (PageSwapCache(page)) |
8c7c6e34 | 1845 | return NULL; |
d13d1443 | 1846 | |
8697d331 | 1847 | /* |
3c541e14 | 1848 | * Check if our page_cgroup is valid |
8697d331 | 1849 | */ |
52d4b9ac KH |
1850 | pc = lookup_page_cgroup(page); |
1851 | if (unlikely(!pc || !PageCgroupUsed(pc))) | |
8c7c6e34 | 1852 | return NULL; |
b9c565d5 | 1853 | |
52d4b9ac | 1854 | lock_page_cgroup(pc); |
d13d1443 | 1855 | |
8c7c6e34 KH |
1856 | mem = pc->mem_cgroup; |
1857 | ||
d13d1443 KH |
1858 | if (!PageCgroupUsed(pc)) |
1859 | goto unlock_out; | |
1860 | ||
1861 | switch (ctype) { | |
1862 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: | |
8a9478ca | 1863 | case MEM_CGROUP_CHARGE_TYPE_DROP: |
d13d1443 KH |
1864 | if (page_mapped(page)) |
1865 | goto unlock_out; | |
1866 | break; | |
1867 | case MEM_CGROUP_CHARGE_TYPE_SWAPOUT: | |
1868 | if (!PageAnon(page)) { /* Shared memory */ | |
1869 | if (page->mapping && !page_is_file_cache(page)) | |
1870 | goto unlock_out; | |
1871 | } else if (page_mapped(page)) /* Anon */ | |
1872 | goto unlock_out; | |
1873 | break; | |
1874 | default: | |
1875 | break; | |
52d4b9ac | 1876 | } |
d13d1443 | 1877 | |
0c3e73e8 | 1878 | if (!mem_cgroup_is_root(mem)) { |
4e649152 | 1879 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
0c3e73e8 BS |
1880 | if (do_swap_account && |
1881 | (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)) | |
4e649152 | 1882 | res_counter_uncharge(&mem->memsw, PAGE_SIZE); |
0c3e73e8 BS |
1883 | } |
1884 | if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) | |
1885 | mem_cgroup_swap_statistics(mem, true); | |
08e552c6 | 1886 | mem_cgroup_charge_statistics(mem, pc, false); |
04046e1a | 1887 | |
52d4b9ac | 1888 | ClearPageCgroupUsed(pc); |
544122e5 KH |
1889 | /* |
1890 | * pc->mem_cgroup is not cleared here. It will be accessed when it's | |
1891 | * freed from LRU. This is safe because uncharged page is expected not | |
1892 | * to be reused (freed soon). Exception is SwapCache, it's handled by | |
1893 | * special functions. | |
1894 | */ | |
b9c565d5 | 1895 | |
69029cd5 | 1896 | mz = page_cgroup_zoneinfo(pc); |
52d4b9ac | 1897 | unlock_page_cgroup(pc); |
fb59e9f1 | 1898 | |
4e649152 | 1899 | if (mem_cgroup_soft_limit_check(mem)) |
f64c3f54 | 1900 | mem_cgroup_update_tree(mem, page); |
a7fe942e KH |
1901 | /* at swapout, this memcg will be accessed to record to swap */ |
1902 | if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT) | |
1903 | css_put(&mem->css); | |
6d12e2d8 | 1904 | |
8c7c6e34 | 1905 | return mem; |
d13d1443 KH |
1906 | |
1907 | unlock_out: | |
1908 | unlock_page_cgroup(pc); | |
8c7c6e34 | 1909 | return NULL; |
3c541e14 BS |
1910 | } |
1911 | ||
69029cd5 KH |
1912 | void mem_cgroup_uncharge_page(struct page *page) |
1913 | { | |
52d4b9ac KH |
1914 | /* early check. */ |
1915 | if (page_mapped(page)) | |
1916 | return; | |
1917 | if (page->mapping && !PageAnon(page)) | |
1918 | return; | |
69029cd5 KH |
1919 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
1920 | } | |
1921 | ||
1922 | void mem_cgroup_uncharge_cache_page(struct page *page) | |
1923 | { | |
1924 | VM_BUG_ON(page_mapped(page)); | |
b7abea96 | 1925 | VM_BUG_ON(page->mapping); |
69029cd5 KH |
1926 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE); |
1927 | } | |
1928 | ||
e767e056 | 1929 | #ifdef CONFIG_SWAP |
8c7c6e34 | 1930 | /* |
e767e056 | 1931 | * called after __delete_from_swap_cache() and drop "page" account. |
8c7c6e34 KH |
1932 | * memcg information is recorded to swap_cgroup of "ent" |
1933 | */ | |
8a9478ca KH |
1934 | void |
1935 | mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout) | |
8c7c6e34 KH |
1936 | { |
1937 | struct mem_cgroup *memcg; | |
8a9478ca KH |
1938 | int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT; |
1939 | ||
1940 | if (!swapout) /* this was a swap cache but the swap is unused ! */ | |
1941 | ctype = MEM_CGROUP_CHARGE_TYPE_DROP; | |
1942 | ||
1943 | memcg = __mem_cgroup_uncharge_common(page, ctype); | |
8c7c6e34 | 1944 | |
8c7c6e34 | 1945 | /* record memcg information */ |
8a9478ca | 1946 | if (do_swap_account && swapout && memcg) { |
a3b2d692 | 1947 | swap_cgroup_record(ent, css_id(&memcg->css)); |
8c7c6e34 KH |
1948 | mem_cgroup_get(memcg); |
1949 | } | |
8a9478ca | 1950 | if (swapout && memcg) |
a7fe942e | 1951 | css_put(&memcg->css); |
8c7c6e34 | 1952 | } |
e767e056 | 1953 | #endif |
8c7c6e34 KH |
1954 | |
1955 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP | |
1956 | /* | |
1957 | * called from swap_entry_free(). remove record in swap_cgroup and | |
1958 | * uncharge "memsw" account. | |
1959 | */ | |
1960 | void mem_cgroup_uncharge_swap(swp_entry_t ent) | |
d13d1443 | 1961 | { |
8c7c6e34 | 1962 | struct mem_cgroup *memcg; |
a3b2d692 | 1963 | unsigned short id; |
8c7c6e34 KH |
1964 | |
1965 | if (!do_swap_account) | |
1966 | return; | |
1967 | ||
a3b2d692 KH |
1968 | id = swap_cgroup_record(ent, 0); |
1969 | rcu_read_lock(); | |
1970 | memcg = mem_cgroup_lookup(id); | |
8c7c6e34 | 1971 | if (memcg) { |
a3b2d692 KH |
1972 | /* |
1973 | * We uncharge this because swap is freed. | |
1974 | * This memcg can be obsolete one. We avoid calling css_tryget | |
1975 | */ | |
0c3e73e8 | 1976 | if (!mem_cgroup_is_root(memcg)) |
4e649152 | 1977 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
0c3e73e8 | 1978 | mem_cgroup_swap_statistics(memcg, false); |
8c7c6e34 KH |
1979 | mem_cgroup_put(memcg); |
1980 | } | |
a3b2d692 | 1981 | rcu_read_unlock(); |
d13d1443 | 1982 | } |
8c7c6e34 | 1983 | #endif |
d13d1443 | 1984 | |
ae41be37 | 1985 | /* |
01b1ae63 KH |
1986 | * Before starting migration, account PAGE_SIZE to mem_cgroup that the old |
1987 | * page belongs to. | |
ae41be37 | 1988 | */ |
01b1ae63 | 1989 | int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr) |
ae41be37 KH |
1990 | { |
1991 | struct page_cgroup *pc; | |
e8589cc1 | 1992 | struct mem_cgroup *mem = NULL; |
e8589cc1 | 1993 | int ret = 0; |
8869b8f6 | 1994 | |
f8d66542 | 1995 | if (mem_cgroup_disabled()) |
4077960e BS |
1996 | return 0; |
1997 | ||
52d4b9ac KH |
1998 | pc = lookup_page_cgroup(page); |
1999 | lock_page_cgroup(pc); | |
2000 | if (PageCgroupUsed(pc)) { | |
e8589cc1 KH |
2001 | mem = pc->mem_cgroup; |
2002 | css_get(&mem->css); | |
e8589cc1 | 2003 | } |
52d4b9ac | 2004 | unlock_page_cgroup(pc); |
01b1ae63 | 2005 | |
e8589cc1 | 2006 | if (mem) { |
f64c3f54 BS |
2007 | ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false, |
2008 | page); | |
e8589cc1 KH |
2009 | css_put(&mem->css); |
2010 | } | |
01b1ae63 | 2011 | *ptr = mem; |
e8589cc1 | 2012 | return ret; |
ae41be37 | 2013 | } |
8869b8f6 | 2014 | |
69029cd5 | 2015 | /* remove redundant charge if migration failed*/ |
01b1ae63 KH |
2016 | void mem_cgroup_end_migration(struct mem_cgroup *mem, |
2017 | struct page *oldpage, struct page *newpage) | |
ae41be37 | 2018 | { |
01b1ae63 KH |
2019 | struct page *target, *unused; |
2020 | struct page_cgroup *pc; | |
2021 | enum charge_type ctype; | |
2022 | ||
2023 | if (!mem) | |
2024 | return; | |
88703267 | 2025 | cgroup_exclude_rmdir(&mem->css); |
01b1ae63 KH |
2026 | /* at migration success, oldpage->mapping is NULL. */ |
2027 | if (oldpage->mapping) { | |
2028 | target = oldpage; | |
2029 | unused = NULL; | |
2030 | } else { | |
2031 | target = newpage; | |
2032 | unused = oldpage; | |
2033 | } | |
2034 | ||
2035 | if (PageAnon(target)) | |
2036 | ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED; | |
2037 | else if (page_is_file_cache(target)) | |
2038 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; | |
2039 | else | |
2040 | ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM; | |
2041 | ||
2042 | /* unused page is not on radix-tree now. */ | |
d13d1443 | 2043 | if (unused) |
01b1ae63 KH |
2044 | __mem_cgroup_uncharge_common(unused, ctype); |
2045 | ||
2046 | pc = lookup_page_cgroup(target); | |
69029cd5 | 2047 | /* |
01b1ae63 KH |
2048 | * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup. |
2049 | * So, double-counting is effectively avoided. | |
2050 | */ | |
2051 | __mem_cgroup_commit_charge(mem, pc, ctype); | |
2052 | ||
2053 | /* | |
2054 | * Both of oldpage and newpage are still under lock_page(). | |
2055 | * Then, we don't have to care about race in radix-tree. | |
2056 | * But we have to be careful that this page is unmapped or not. | |
2057 | * | |
2058 | * There is a case for !page_mapped(). At the start of | |
2059 | * migration, oldpage was mapped. But now, it's zapped. | |
2060 | * But we know *target* page is not freed/reused under us. | |
2061 | * mem_cgroup_uncharge_page() does all necessary checks. | |
69029cd5 | 2062 | */ |
01b1ae63 KH |
2063 | if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED) |
2064 | mem_cgroup_uncharge_page(target); | |
88703267 KH |
2065 | /* |
2066 | * At migration, we may charge account against cgroup which has no tasks | |
2067 | * So, rmdir()->pre_destroy() can be called while we do this charge. | |
2068 | * In that case, we need to call pre_destroy() again. check it here. | |
2069 | */ | |
2070 | cgroup_release_and_wakeup_rmdir(&mem->css); | |
ae41be37 | 2071 | } |
78fb7466 | 2072 | |
c9b0ed51 | 2073 | /* |
ae3abae6 DN |
2074 | * A call to try to shrink memory usage on charge failure at shmem's swapin. |
2075 | * Calling hierarchical_reclaim is not enough because we should update | |
2076 | * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM. | |
2077 | * Moreover considering hierarchy, we should reclaim from the mem_over_limit, | |
2078 | * not from the memcg which this page would be charged to. | |
2079 | * try_charge_swapin does all of these works properly. | |
c9b0ed51 | 2080 | */ |
ae3abae6 | 2081 | int mem_cgroup_shmem_charge_fallback(struct page *page, |
b5a84319 KH |
2082 | struct mm_struct *mm, |
2083 | gfp_t gfp_mask) | |
c9b0ed51 | 2084 | { |
b5a84319 | 2085 | struct mem_cgroup *mem = NULL; |
ae3abae6 | 2086 | int ret; |
c9b0ed51 | 2087 | |
f8d66542 | 2088 | if (mem_cgroup_disabled()) |
cede86ac | 2089 | return 0; |
c9b0ed51 | 2090 | |
ae3abae6 DN |
2091 | ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem); |
2092 | if (!ret) | |
2093 | mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */ | |
c9b0ed51 | 2094 | |
ae3abae6 | 2095 | return ret; |
c9b0ed51 KH |
2096 | } |
2097 | ||
8c7c6e34 KH |
2098 | static DEFINE_MUTEX(set_limit_mutex); |
2099 | ||
d38d2a75 | 2100 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
8c7c6e34 | 2101 | unsigned long long val) |
628f4235 | 2102 | { |
81d39c20 | 2103 | int retry_count; |
628f4235 | 2104 | int progress; |
8c7c6e34 | 2105 | u64 memswlimit; |
628f4235 | 2106 | int ret = 0; |
81d39c20 KH |
2107 | int children = mem_cgroup_count_children(memcg); |
2108 | u64 curusage, oldusage; | |
2109 | ||
2110 | /* | |
2111 | * For keeping hierarchical_reclaim simple, how long we should retry | |
2112 | * is depends on callers. We set our retry-count to be function | |
2113 | * of # of children which we should visit in this loop. | |
2114 | */ | |
2115 | retry_count = MEM_CGROUP_RECLAIM_RETRIES * children; | |
2116 | ||
2117 | oldusage = res_counter_read_u64(&memcg->res, RES_USAGE); | |
628f4235 | 2118 | |
8c7c6e34 | 2119 | while (retry_count) { |
628f4235 KH |
2120 | if (signal_pending(current)) { |
2121 | ret = -EINTR; | |
2122 | break; | |
2123 | } | |
8c7c6e34 KH |
2124 | /* |
2125 | * Rather than hide all in some function, I do this in | |
2126 | * open coded manner. You see what this really does. | |
2127 | * We have to guarantee mem->res.limit < mem->memsw.limit. | |
2128 | */ | |
2129 | mutex_lock(&set_limit_mutex); | |
2130 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); | |
2131 | if (memswlimit < val) { | |
2132 | ret = -EINVAL; | |
2133 | mutex_unlock(&set_limit_mutex); | |
628f4235 KH |
2134 | break; |
2135 | } | |
8c7c6e34 | 2136 | ret = res_counter_set_limit(&memcg->res, val); |
22a668d7 KH |
2137 | if (!ret) { |
2138 | if (memswlimit == val) | |
2139 | memcg->memsw_is_minimum = true; | |
2140 | else | |
2141 | memcg->memsw_is_minimum = false; | |
2142 | } | |
8c7c6e34 KH |
2143 | mutex_unlock(&set_limit_mutex); |
2144 | ||
2145 | if (!ret) | |
2146 | break; | |
2147 | ||
4e416953 BS |
2148 | progress = mem_cgroup_hierarchical_reclaim(memcg, NULL, |
2149 | GFP_KERNEL, | |
2150 | MEM_CGROUP_RECLAIM_SHRINK); | |
81d39c20 KH |
2151 | curusage = res_counter_read_u64(&memcg->res, RES_USAGE); |
2152 | /* Usage is reduced ? */ | |
2153 | if (curusage >= oldusage) | |
2154 | retry_count--; | |
2155 | else | |
2156 | oldusage = curusage; | |
8c7c6e34 | 2157 | } |
14797e23 | 2158 | |
8c7c6e34 KH |
2159 | return ret; |
2160 | } | |
2161 | ||
338c8431 LZ |
2162 | static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg, |
2163 | unsigned long long val) | |
8c7c6e34 | 2164 | { |
81d39c20 | 2165 | int retry_count; |
8c7c6e34 | 2166 | u64 memlimit, oldusage, curusage; |
81d39c20 KH |
2167 | int children = mem_cgroup_count_children(memcg); |
2168 | int ret = -EBUSY; | |
8c7c6e34 | 2169 | |
81d39c20 KH |
2170 | /* see mem_cgroup_resize_res_limit */ |
2171 | retry_count = children * MEM_CGROUP_RECLAIM_RETRIES; | |
2172 | oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); | |
8c7c6e34 KH |
2173 | while (retry_count) { |
2174 | if (signal_pending(current)) { | |
2175 | ret = -EINTR; | |
2176 | break; | |
2177 | } | |
2178 | /* | |
2179 | * Rather than hide all in some function, I do this in | |
2180 | * open coded manner. You see what this really does. | |
2181 | * We have to guarantee mem->res.limit < mem->memsw.limit. | |
2182 | */ | |
2183 | mutex_lock(&set_limit_mutex); | |
2184 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); | |
2185 | if (memlimit > val) { | |
2186 | ret = -EINVAL; | |
2187 | mutex_unlock(&set_limit_mutex); | |
2188 | break; | |
2189 | } | |
2190 | ret = res_counter_set_limit(&memcg->memsw, val); | |
22a668d7 KH |
2191 | if (!ret) { |
2192 | if (memlimit == val) | |
2193 | memcg->memsw_is_minimum = true; | |
2194 | else | |
2195 | memcg->memsw_is_minimum = false; | |
2196 | } | |
8c7c6e34 KH |
2197 | mutex_unlock(&set_limit_mutex); |
2198 | ||
2199 | if (!ret) | |
2200 | break; | |
2201 | ||
4e416953 | 2202 | mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL, |
75822b44 BS |
2203 | MEM_CGROUP_RECLAIM_NOSWAP | |
2204 | MEM_CGROUP_RECLAIM_SHRINK); | |
8c7c6e34 | 2205 | curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
81d39c20 | 2206 | /* Usage is reduced ? */ |
8c7c6e34 | 2207 | if (curusage >= oldusage) |
628f4235 | 2208 | retry_count--; |
81d39c20 KH |
2209 | else |
2210 | oldusage = curusage; | |
628f4235 KH |
2211 | } |
2212 | return ret; | |
2213 | } | |
2214 | ||
4e416953 BS |
2215 | unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order, |
2216 | gfp_t gfp_mask, int nid, | |
2217 | int zid) | |
2218 | { | |
2219 | unsigned long nr_reclaimed = 0; | |
2220 | struct mem_cgroup_per_zone *mz, *next_mz = NULL; | |
2221 | unsigned long reclaimed; | |
2222 | int loop = 0; | |
2223 | struct mem_cgroup_tree_per_zone *mctz; | |
2224 | ||
2225 | if (order > 0) | |
2226 | return 0; | |
2227 | ||
2228 | mctz = soft_limit_tree_node_zone(nid, zid); | |
2229 | /* | |
2230 | * This loop can run a while, specially if mem_cgroup's continuously | |
2231 | * keep exceeding their soft limit and putting the system under | |
2232 | * pressure | |
2233 | */ | |
2234 | do { | |
2235 | if (next_mz) | |
2236 | mz = next_mz; | |
2237 | else | |
2238 | mz = mem_cgroup_largest_soft_limit_node(mctz); | |
2239 | if (!mz) | |
2240 | break; | |
2241 | ||
2242 | reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone, | |
2243 | gfp_mask, | |
2244 | MEM_CGROUP_RECLAIM_SOFT); | |
2245 | nr_reclaimed += reclaimed; | |
2246 | spin_lock(&mctz->lock); | |
2247 | ||
2248 | /* | |
2249 | * If we failed to reclaim anything from this memory cgroup | |
2250 | * it is time to move on to the next cgroup | |
2251 | */ | |
2252 | next_mz = NULL; | |
2253 | if (!reclaimed) { | |
2254 | do { | |
2255 | /* | |
2256 | * Loop until we find yet another one. | |
2257 | * | |
2258 | * By the time we get the soft_limit lock | |
2259 | * again, someone might have aded the | |
2260 | * group back on the RB tree. Iterate to | |
2261 | * make sure we get a different mem. | |
2262 | * mem_cgroup_largest_soft_limit_node returns | |
2263 | * NULL if no other cgroup is present on | |
2264 | * the tree | |
2265 | */ | |
2266 | next_mz = | |
2267 | __mem_cgroup_largest_soft_limit_node(mctz); | |
2268 | if (next_mz == mz) { | |
2269 | css_put(&next_mz->mem->css); | |
2270 | next_mz = NULL; | |
2271 | } else /* next_mz == NULL or other memcg */ | |
2272 | break; | |
2273 | } while (1); | |
2274 | } | |
2275 | mz->usage_in_excess = | |
2276 | res_counter_soft_limit_excess(&mz->mem->res); | |
2277 | __mem_cgroup_remove_exceeded(mz->mem, mz, mctz); | |
2278 | /* | |
2279 | * One school of thought says that we should not add | |
2280 | * back the node to the tree if reclaim returns 0. | |
2281 | * But our reclaim could return 0, simply because due | |
2282 | * to priority we are exposing a smaller subset of | |
2283 | * memory to reclaim from. Consider this as a longer | |
2284 | * term TODO. | |
2285 | */ | |
2286 | if (mz->usage_in_excess) | |
2287 | __mem_cgroup_insert_exceeded(mz->mem, mz, mctz); | |
2288 | spin_unlock(&mctz->lock); | |
2289 | css_put(&mz->mem->css); | |
2290 | loop++; | |
2291 | /* | |
2292 | * Could not reclaim anything and there are no more | |
2293 | * mem cgroups to try or we seem to be looping without | |
2294 | * reclaiming anything. | |
2295 | */ | |
2296 | if (!nr_reclaimed && | |
2297 | (next_mz == NULL || | |
2298 | loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS)) | |
2299 | break; | |
2300 | } while (!nr_reclaimed); | |
2301 | if (next_mz) | |
2302 | css_put(&next_mz->mem->css); | |
2303 | return nr_reclaimed; | |
2304 | } | |
2305 | ||
cc847582 KH |
2306 | /* |
2307 | * This routine traverse page_cgroup in given list and drop them all. | |
cc847582 KH |
2308 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
2309 | */ | |
f817ed48 | 2310 | static int mem_cgroup_force_empty_list(struct mem_cgroup *mem, |
08e552c6 | 2311 | int node, int zid, enum lru_list lru) |
cc847582 | 2312 | { |
08e552c6 KH |
2313 | struct zone *zone; |
2314 | struct mem_cgroup_per_zone *mz; | |
f817ed48 | 2315 | struct page_cgroup *pc, *busy; |
08e552c6 | 2316 | unsigned long flags, loop; |
072c56c1 | 2317 | struct list_head *list; |
f817ed48 | 2318 | int ret = 0; |
072c56c1 | 2319 | |
08e552c6 KH |
2320 | zone = &NODE_DATA(node)->node_zones[zid]; |
2321 | mz = mem_cgroup_zoneinfo(mem, node, zid); | |
b69408e8 | 2322 | list = &mz->lists[lru]; |
cc847582 | 2323 | |
f817ed48 KH |
2324 | loop = MEM_CGROUP_ZSTAT(mz, lru); |
2325 | /* give some margin against EBUSY etc...*/ | |
2326 | loop += 256; | |
2327 | busy = NULL; | |
2328 | while (loop--) { | |
2329 | ret = 0; | |
08e552c6 | 2330 | spin_lock_irqsave(&zone->lru_lock, flags); |
f817ed48 | 2331 | if (list_empty(list)) { |
08e552c6 | 2332 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
52d4b9ac | 2333 | break; |
f817ed48 KH |
2334 | } |
2335 | pc = list_entry(list->prev, struct page_cgroup, lru); | |
2336 | if (busy == pc) { | |
2337 | list_move(&pc->lru, list); | |
2338 | busy = 0; | |
08e552c6 | 2339 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
f817ed48 KH |
2340 | continue; |
2341 | } | |
08e552c6 | 2342 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
f817ed48 | 2343 | |
2c26fdd7 | 2344 | ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL); |
f817ed48 | 2345 | if (ret == -ENOMEM) |
52d4b9ac | 2346 | break; |
f817ed48 KH |
2347 | |
2348 | if (ret == -EBUSY || ret == -EINVAL) { | |
2349 | /* found lock contention or "pc" is obsolete. */ | |
2350 | busy = pc; | |
2351 | cond_resched(); | |
2352 | } else | |
2353 | busy = NULL; | |
cc847582 | 2354 | } |
08e552c6 | 2355 | |
f817ed48 KH |
2356 | if (!ret && !list_empty(list)) |
2357 | return -EBUSY; | |
2358 | return ret; | |
cc847582 KH |
2359 | } |
2360 | ||
2361 | /* | |
2362 | * make mem_cgroup's charge to be 0 if there is no task. | |
2363 | * This enables deleting this mem_cgroup. | |
2364 | */ | |
c1e862c1 | 2365 | static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all) |
cc847582 | 2366 | { |
f817ed48 KH |
2367 | int ret; |
2368 | int node, zid, shrink; | |
2369 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; | |
c1e862c1 | 2370 | struct cgroup *cgrp = mem->css.cgroup; |
8869b8f6 | 2371 | |
cc847582 | 2372 | css_get(&mem->css); |
f817ed48 KH |
2373 | |
2374 | shrink = 0; | |
c1e862c1 KH |
2375 | /* should free all ? */ |
2376 | if (free_all) | |
2377 | goto try_to_free; | |
f817ed48 | 2378 | move_account: |
1ecaab2b | 2379 | while (mem->res.usage > 0) { |
f817ed48 | 2380 | ret = -EBUSY; |
c1e862c1 KH |
2381 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children)) |
2382 | goto out; | |
2383 | ret = -EINTR; | |
2384 | if (signal_pending(current)) | |
cc847582 | 2385 | goto out; |
52d4b9ac KH |
2386 | /* This is for making all *used* pages to be on LRU. */ |
2387 | lru_add_drain_all(); | |
f817ed48 | 2388 | ret = 0; |
299b4eaa | 2389 | for_each_node_state(node, N_HIGH_MEMORY) { |
f817ed48 | 2390 | for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) { |
b69408e8 | 2391 | enum lru_list l; |
f817ed48 KH |
2392 | for_each_lru(l) { |
2393 | ret = mem_cgroup_force_empty_list(mem, | |
08e552c6 | 2394 | node, zid, l); |
f817ed48 KH |
2395 | if (ret) |
2396 | break; | |
2397 | } | |
1ecaab2b | 2398 | } |
f817ed48 KH |
2399 | if (ret) |
2400 | break; | |
2401 | } | |
2402 | /* it seems parent cgroup doesn't have enough mem */ | |
2403 | if (ret == -ENOMEM) | |
2404 | goto try_to_free; | |
52d4b9ac | 2405 | cond_resched(); |
cc847582 KH |
2406 | } |
2407 | ret = 0; | |
2408 | out: | |
2409 | css_put(&mem->css); | |
2410 | return ret; | |
f817ed48 KH |
2411 | |
2412 | try_to_free: | |
c1e862c1 KH |
2413 | /* returns EBUSY if there is a task or if we come here twice. */ |
2414 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) { | |
f817ed48 KH |
2415 | ret = -EBUSY; |
2416 | goto out; | |
2417 | } | |
c1e862c1 KH |
2418 | /* we call try-to-free pages for make this cgroup empty */ |
2419 | lru_add_drain_all(); | |
f817ed48 KH |
2420 | /* try to free all pages in this cgroup */ |
2421 | shrink = 1; | |
2422 | while (nr_retries && mem->res.usage > 0) { | |
2423 | int progress; | |
c1e862c1 KH |
2424 | |
2425 | if (signal_pending(current)) { | |
2426 | ret = -EINTR; | |
2427 | goto out; | |
2428 | } | |
a7885eb8 KM |
2429 | progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL, |
2430 | false, get_swappiness(mem)); | |
c1e862c1 | 2431 | if (!progress) { |
f817ed48 | 2432 | nr_retries--; |
c1e862c1 | 2433 | /* maybe some writeback is necessary */ |
8aa7e847 | 2434 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
c1e862c1 | 2435 | } |
f817ed48 KH |
2436 | |
2437 | } | |
08e552c6 | 2438 | lru_add_drain(); |
f817ed48 KH |
2439 | /* try move_account...there may be some *locked* pages. */ |
2440 | if (mem->res.usage) | |
2441 | goto move_account; | |
2442 | ret = 0; | |
2443 | goto out; | |
cc847582 KH |
2444 | } |
2445 | ||
c1e862c1 KH |
2446 | int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event) |
2447 | { | |
2448 | return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true); | |
2449 | } | |
2450 | ||
2451 | ||
18f59ea7 BS |
2452 | static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft) |
2453 | { | |
2454 | return mem_cgroup_from_cont(cont)->use_hierarchy; | |
2455 | } | |
2456 | ||
2457 | static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft, | |
2458 | u64 val) | |
2459 | { | |
2460 | int retval = 0; | |
2461 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); | |
2462 | struct cgroup *parent = cont->parent; | |
2463 | struct mem_cgroup *parent_mem = NULL; | |
2464 | ||
2465 | if (parent) | |
2466 | parent_mem = mem_cgroup_from_cont(parent); | |
2467 | ||
2468 | cgroup_lock(); | |
2469 | /* | |
2470 | * If parent's use_hiearchy is set, we can't make any modifications | |
2471 | * in the child subtrees. If it is unset, then the change can | |
2472 | * occur, provided the current cgroup has no children. | |
2473 | * | |
2474 | * For the root cgroup, parent_mem is NULL, we allow value to be | |
2475 | * set if there are no children. | |
2476 | */ | |
2477 | if ((!parent_mem || !parent_mem->use_hierarchy) && | |
2478 | (val == 1 || val == 0)) { | |
2479 | if (list_empty(&cont->children)) | |
2480 | mem->use_hierarchy = val; | |
2481 | else | |
2482 | retval = -EBUSY; | |
2483 | } else | |
2484 | retval = -EINVAL; | |
2485 | cgroup_unlock(); | |
2486 | ||
2487 | return retval; | |
2488 | } | |
2489 | ||
0c3e73e8 BS |
2490 | struct mem_cgroup_idx_data { |
2491 | s64 val; | |
2492 | enum mem_cgroup_stat_index idx; | |
2493 | }; | |
2494 | ||
2495 | static int | |
2496 | mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data) | |
2497 | { | |
2498 | struct mem_cgroup_idx_data *d = data; | |
2499 | d->val += mem_cgroup_read_stat(&mem->stat, d->idx); | |
2500 | return 0; | |
2501 | } | |
2502 | ||
2503 | static void | |
2504 | mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem, | |
2505 | enum mem_cgroup_stat_index idx, s64 *val) | |
2506 | { | |
2507 | struct mem_cgroup_idx_data d; | |
2508 | d.idx = idx; | |
2509 | d.val = 0; | |
2510 | mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat); | |
2511 | *val = d.val; | |
2512 | } | |
2513 | ||
2c3daa72 | 2514 | static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) |
8cdea7c0 | 2515 | { |
8c7c6e34 | 2516 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
0c3e73e8 | 2517 | u64 idx_val, val; |
8c7c6e34 KH |
2518 | int type, name; |
2519 | ||
2520 | type = MEMFILE_TYPE(cft->private); | |
2521 | name = MEMFILE_ATTR(cft->private); | |
2522 | switch (type) { | |
2523 | case _MEM: | |
0c3e73e8 BS |
2524 | if (name == RES_USAGE && mem_cgroup_is_root(mem)) { |
2525 | mem_cgroup_get_recursive_idx_stat(mem, | |
2526 | MEM_CGROUP_STAT_CACHE, &idx_val); | |
2527 | val = idx_val; | |
2528 | mem_cgroup_get_recursive_idx_stat(mem, | |
2529 | MEM_CGROUP_STAT_RSS, &idx_val); | |
2530 | val += idx_val; | |
2531 | val <<= PAGE_SHIFT; | |
2532 | } else | |
2533 | val = res_counter_read_u64(&mem->res, name); | |
8c7c6e34 KH |
2534 | break; |
2535 | case _MEMSWAP: | |
0c3e73e8 BS |
2536 | if (name == RES_USAGE && mem_cgroup_is_root(mem)) { |
2537 | mem_cgroup_get_recursive_idx_stat(mem, | |
2538 | MEM_CGROUP_STAT_CACHE, &idx_val); | |
2539 | val = idx_val; | |
2540 | mem_cgroup_get_recursive_idx_stat(mem, | |
2541 | MEM_CGROUP_STAT_RSS, &idx_val); | |
2542 | val += idx_val; | |
2543 | mem_cgroup_get_recursive_idx_stat(mem, | |
2544 | MEM_CGROUP_STAT_SWAPOUT, &idx_val); | |
2545 | val <<= PAGE_SHIFT; | |
2546 | } else | |
2547 | val = res_counter_read_u64(&mem->memsw, name); | |
8c7c6e34 KH |
2548 | break; |
2549 | default: | |
2550 | BUG(); | |
2551 | break; | |
2552 | } | |
2553 | return val; | |
8cdea7c0 | 2554 | } |
628f4235 KH |
2555 | /* |
2556 | * The user of this function is... | |
2557 | * RES_LIMIT. | |
2558 | */ | |
856c13aa PM |
2559 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
2560 | const char *buffer) | |
8cdea7c0 | 2561 | { |
628f4235 | 2562 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
8c7c6e34 | 2563 | int type, name; |
628f4235 KH |
2564 | unsigned long long val; |
2565 | int ret; | |
2566 | ||
8c7c6e34 KH |
2567 | type = MEMFILE_TYPE(cft->private); |
2568 | name = MEMFILE_ATTR(cft->private); | |
2569 | switch (name) { | |
628f4235 | 2570 | case RES_LIMIT: |
4b3bde4c BS |
2571 | if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */ |
2572 | ret = -EINVAL; | |
2573 | break; | |
2574 | } | |
628f4235 KH |
2575 | /* This function does all necessary parse...reuse it */ |
2576 | ret = res_counter_memparse_write_strategy(buffer, &val); | |
8c7c6e34 KH |
2577 | if (ret) |
2578 | break; | |
2579 | if (type == _MEM) | |
628f4235 | 2580 | ret = mem_cgroup_resize_limit(memcg, val); |
8c7c6e34 KH |
2581 | else |
2582 | ret = mem_cgroup_resize_memsw_limit(memcg, val); | |
628f4235 | 2583 | break; |
296c81d8 BS |
2584 | case RES_SOFT_LIMIT: |
2585 | ret = res_counter_memparse_write_strategy(buffer, &val); | |
2586 | if (ret) | |
2587 | break; | |
2588 | /* | |
2589 | * For memsw, soft limits are hard to implement in terms | |
2590 | * of semantics, for now, we support soft limits for | |
2591 | * control without swap | |
2592 | */ | |
2593 | if (type == _MEM) | |
2594 | ret = res_counter_set_soft_limit(&memcg->res, val); | |
2595 | else | |
2596 | ret = -EINVAL; | |
2597 | break; | |
628f4235 KH |
2598 | default: |
2599 | ret = -EINVAL; /* should be BUG() ? */ | |
2600 | break; | |
2601 | } | |
2602 | return ret; | |
8cdea7c0 BS |
2603 | } |
2604 | ||
fee7b548 KH |
2605 | static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg, |
2606 | unsigned long long *mem_limit, unsigned long long *memsw_limit) | |
2607 | { | |
2608 | struct cgroup *cgroup; | |
2609 | unsigned long long min_limit, min_memsw_limit, tmp; | |
2610 | ||
2611 | min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT); | |
2612 | min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); | |
2613 | cgroup = memcg->css.cgroup; | |
2614 | if (!memcg->use_hierarchy) | |
2615 | goto out; | |
2616 | ||
2617 | while (cgroup->parent) { | |
2618 | cgroup = cgroup->parent; | |
2619 | memcg = mem_cgroup_from_cont(cgroup); | |
2620 | if (!memcg->use_hierarchy) | |
2621 | break; | |
2622 | tmp = res_counter_read_u64(&memcg->res, RES_LIMIT); | |
2623 | min_limit = min(min_limit, tmp); | |
2624 | tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT); | |
2625 | min_memsw_limit = min(min_memsw_limit, tmp); | |
2626 | } | |
2627 | out: | |
2628 | *mem_limit = min_limit; | |
2629 | *memsw_limit = min_memsw_limit; | |
2630 | return; | |
2631 | } | |
2632 | ||
29f2a4da | 2633 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
c84872e1 PE |
2634 | { |
2635 | struct mem_cgroup *mem; | |
8c7c6e34 | 2636 | int type, name; |
c84872e1 PE |
2637 | |
2638 | mem = mem_cgroup_from_cont(cont); | |
8c7c6e34 KH |
2639 | type = MEMFILE_TYPE(event); |
2640 | name = MEMFILE_ATTR(event); | |
2641 | switch (name) { | |
29f2a4da | 2642 | case RES_MAX_USAGE: |
8c7c6e34 KH |
2643 | if (type == _MEM) |
2644 | res_counter_reset_max(&mem->res); | |
2645 | else | |
2646 | res_counter_reset_max(&mem->memsw); | |
29f2a4da PE |
2647 | break; |
2648 | case RES_FAILCNT: | |
8c7c6e34 KH |
2649 | if (type == _MEM) |
2650 | res_counter_reset_failcnt(&mem->res); | |
2651 | else | |
2652 | res_counter_reset_failcnt(&mem->memsw); | |
29f2a4da PE |
2653 | break; |
2654 | } | |
f64c3f54 | 2655 | |
85cc59db | 2656 | return 0; |
c84872e1 PE |
2657 | } |
2658 | ||
14067bb3 KH |
2659 | |
2660 | /* For read statistics */ | |
2661 | enum { | |
2662 | MCS_CACHE, | |
2663 | MCS_RSS, | |
d69b042f | 2664 | MCS_MAPPED_FILE, |
14067bb3 KH |
2665 | MCS_PGPGIN, |
2666 | MCS_PGPGOUT, | |
1dd3a273 | 2667 | MCS_SWAP, |
14067bb3 KH |
2668 | MCS_INACTIVE_ANON, |
2669 | MCS_ACTIVE_ANON, | |
2670 | MCS_INACTIVE_FILE, | |
2671 | MCS_ACTIVE_FILE, | |
2672 | MCS_UNEVICTABLE, | |
2673 | NR_MCS_STAT, | |
2674 | }; | |
2675 | ||
2676 | struct mcs_total_stat { | |
2677 | s64 stat[NR_MCS_STAT]; | |
d2ceb9b7 KH |
2678 | }; |
2679 | ||
14067bb3 KH |
2680 | struct { |
2681 | char *local_name; | |
2682 | char *total_name; | |
2683 | } memcg_stat_strings[NR_MCS_STAT] = { | |
2684 | {"cache", "total_cache"}, | |
2685 | {"rss", "total_rss"}, | |
d69b042f | 2686 | {"mapped_file", "total_mapped_file"}, |
14067bb3 KH |
2687 | {"pgpgin", "total_pgpgin"}, |
2688 | {"pgpgout", "total_pgpgout"}, | |
1dd3a273 | 2689 | {"swap", "total_swap"}, |
14067bb3 KH |
2690 | {"inactive_anon", "total_inactive_anon"}, |
2691 | {"active_anon", "total_active_anon"}, | |
2692 | {"inactive_file", "total_inactive_file"}, | |
2693 | {"active_file", "total_active_file"}, | |
2694 | {"unevictable", "total_unevictable"} | |
2695 | }; | |
2696 | ||
2697 | ||
2698 | static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data) | |
2699 | { | |
2700 | struct mcs_total_stat *s = data; | |
2701 | s64 val; | |
2702 | ||
2703 | /* per cpu stat */ | |
2704 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE); | |
2705 | s->stat[MCS_CACHE] += val * PAGE_SIZE; | |
2706 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS); | |
2707 | s->stat[MCS_RSS] += val * PAGE_SIZE; | |
d69b042f BS |
2708 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE); |
2709 | s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE; | |
14067bb3 KH |
2710 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT); |
2711 | s->stat[MCS_PGPGIN] += val; | |
2712 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT); | |
2713 | s->stat[MCS_PGPGOUT] += val; | |
1dd3a273 DN |
2714 | if (do_swap_account) { |
2715 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT); | |
2716 | s->stat[MCS_SWAP] += val * PAGE_SIZE; | |
2717 | } | |
14067bb3 KH |
2718 | |
2719 | /* per zone stat */ | |
2720 | val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON); | |
2721 | s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE; | |
2722 | val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON); | |
2723 | s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE; | |
2724 | val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE); | |
2725 | s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE; | |
2726 | val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE); | |
2727 | s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE; | |
2728 | val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE); | |
2729 | s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE; | |
2730 | return 0; | |
2731 | } | |
2732 | ||
2733 | static void | |
2734 | mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s) | |
2735 | { | |
2736 | mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat); | |
2737 | } | |
2738 | ||
c64745cf PM |
2739 | static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft, |
2740 | struct cgroup_map_cb *cb) | |
d2ceb9b7 | 2741 | { |
d2ceb9b7 | 2742 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
14067bb3 | 2743 | struct mcs_total_stat mystat; |
d2ceb9b7 KH |
2744 | int i; |
2745 | ||
14067bb3 KH |
2746 | memset(&mystat, 0, sizeof(mystat)); |
2747 | mem_cgroup_get_local_stat(mem_cont, &mystat); | |
d2ceb9b7 | 2748 | |
1dd3a273 DN |
2749 | for (i = 0; i < NR_MCS_STAT; i++) { |
2750 | if (i == MCS_SWAP && !do_swap_account) | |
2751 | continue; | |
14067bb3 | 2752 | cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]); |
1dd3a273 | 2753 | } |
7b854121 | 2754 | |
14067bb3 | 2755 | /* Hierarchical information */ |
fee7b548 KH |
2756 | { |
2757 | unsigned long long limit, memsw_limit; | |
2758 | memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit); | |
2759 | cb->fill(cb, "hierarchical_memory_limit", limit); | |
2760 | if (do_swap_account) | |
2761 | cb->fill(cb, "hierarchical_memsw_limit", memsw_limit); | |
2762 | } | |
7f016ee8 | 2763 | |
14067bb3 KH |
2764 | memset(&mystat, 0, sizeof(mystat)); |
2765 | mem_cgroup_get_total_stat(mem_cont, &mystat); | |
1dd3a273 DN |
2766 | for (i = 0; i < NR_MCS_STAT; i++) { |
2767 | if (i == MCS_SWAP && !do_swap_account) | |
2768 | continue; | |
14067bb3 | 2769 | cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]); |
1dd3a273 | 2770 | } |
14067bb3 | 2771 | |
7f016ee8 | 2772 | #ifdef CONFIG_DEBUG_VM |
c772be93 | 2773 | cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL)); |
7f016ee8 KM |
2774 | |
2775 | { | |
2776 | int nid, zid; | |
2777 | struct mem_cgroup_per_zone *mz; | |
2778 | unsigned long recent_rotated[2] = {0, 0}; | |
2779 | unsigned long recent_scanned[2] = {0, 0}; | |
2780 | ||
2781 | for_each_online_node(nid) | |
2782 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { | |
2783 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); | |
2784 | ||
2785 | recent_rotated[0] += | |
2786 | mz->reclaim_stat.recent_rotated[0]; | |
2787 | recent_rotated[1] += | |
2788 | mz->reclaim_stat.recent_rotated[1]; | |
2789 | recent_scanned[0] += | |
2790 | mz->reclaim_stat.recent_scanned[0]; | |
2791 | recent_scanned[1] += | |
2792 | mz->reclaim_stat.recent_scanned[1]; | |
2793 | } | |
2794 | cb->fill(cb, "recent_rotated_anon", recent_rotated[0]); | |
2795 | cb->fill(cb, "recent_rotated_file", recent_rotated[1]); | |
2796 | cb->fill(cb, "recent_scanned_anon", recent_scanned[0]); | |
2797 | cb->fill(cb, "recent_scanned_file", recent_scanned[1]); | |
2798 | } | |
2799 | #endif | |
2800 | ||
d2ceb9b7 KH |
2801 | return 0; |
2802 | } | |
2803 | ||
a7885eb8 KM |
2804 | static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft) |
2805 | { | |
2806 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); | |
2807 | ||
2808 | return get_swappiness(memcg); | |
2809 | } | |
2810 | ||
2811 | static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, | |
2812 | u64 val) | |
2813 | { | |
2814 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); | |
2815 | struct mem_cgroup *parent; | |
068b38c1 | 2816 | |
a7885eb8 KM |
2817 | if (val > 100) |
2818 | return -EINVAL; | |
2819 | ||
2820 | if (cgrp->parent == NULL) | |
2821 | return -EINVAL; | |
2822 | ||
2823 | parent = mem_cgroup_from_cont(cgrp->parent); | |
068b38c1 LZ |
2824 | |
2825 | cgroup_lock(); | |
2826 | ||
a7885eb8 KM |
2827 | /* If under hierarchy, only empty-root can set this value */ |
2828 | if ((parent->use_hierarchy) || | |
068b38c1 LZ |
2829 | (memcg->use_hierarchy && !list_empty(&cgrp->children))) { |
2830 | cgroup_unlock(); | |
a7885eb8 | 2831 | return -EINVAL; |
068b38c1 | 2832 | } |
a7885eb8 KM |
2833 | |
2834 | spin_lock(&memcg->reclaim_param_lock); | |
2835 | memcg->swappiness = val; | |
2836 | spin_unlock(&memcg->reclaim_param_lock); | |
2837 | ||
068b38c1 LZ |
2838 | cgroup_unlock(); |
2839 | ||
a7885eb8 KM |
2840 | return 0; |
2841 | } | |
2842 | ||
c1e862c1 | 2843 | |
8cdea7c0 BS |
2844 | static struct cftype mem_cgroup_files[] = { |
2845 | { | |
0eea1030 | 2846 | .name = "usage_in_bytes", |
8c7c6e34 | 2847 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
2c3daa72 | 2848 | .read_u64 = mem_cgroup_read, |
8cdea7c0 | 2849 | }, |
c84872e1 PE |
2850 | { |
2851 | .name = "max_usage_in_bytes", | |
8c7c6e34 | 2852 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
29f2a4da | 2853 | .trigger = mem_cgroup_reset, |
c84872e1 PE |
2854 | .read_u64 = mem_cgroup_read, |
2855 | }, | |
8cdea7c0 | 2856 | { |
0eea1030 | 2857 | .name = "limit_in_bytes", |
8c7c6e34 | 2858 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
856c13aa | 2859 | .write_string = mem_cgroup_write, |
2c3daa72 | 2860 | .read_u64 = mem_cgroup_read, |
8cdea7c0 | 2861 | }, |
296c81d8 BS |
2862 | { |
2863 | .name = "soft_limit_in_bytes", | |
2864 | .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT), | |
2865 | .write_string = mem_cgroup_write, | |
2866 | .read_u64 = mem_cgroup_read, | |
2867 | }, | |
8cdea7c0 BS |
2868 | { |
2869 | .name = "failcnt", | |
8c7c6e34 | 2870 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
29f2a4da | 2871 | .trigger = mem_cgroup_reset, |
2c3daa72 | 2872 | .read_u64 = mem_cgroup_read, |
8cdea7c0 | 2873 | }, |
d2ceb9b7 KH |
2874 | { |
2875 | .name = "stat", | |
c64745cf | 2876 | .read_map = mem_control_stat_show, |
d2ceb9b7 | 2877 | }, |
c1e862c1 KH |
2878 | { |
2879 | .name = "force_empty", | |
2880 | .trigger = mem_cgroup_force_empty_write, | |
2881 | }, | |
18f59ea7 BS |
2882 | { |
2883 | .name = "use_hierarchy", | |
2884 | .write_u64 = mem_cgroup_hierarchy_write, | |
2885 | .read_u64 = mem_cgroup_hierarchy_read, | |
2886 | }, | |
a7885eb8 KM |
2887 | { |
2888 | .name = "swappiness", | |
2889 | .read_u64 = mem_cgroup_swappiness_read, | |
2890 | .write_u64 = mem_cgroup_swappiness_write, | |
2891 | }, | |
8cdea7c0 BS |
2892 | }; |
2893 | ||
8c7c6e34 KH |
2894 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
2895 | static struct cftype memsw_cgroup_files[] = { | |
2896 | { | |
2897 | .name = "memsw.usage_in_bytes", | |
2898 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), | |
2899 | .read_u64 = mem_cgroup_read, | |
2900 | }, | |
2901 | { | |
2902 | .name = "memsw.max_usage_in_bytes", | |
2903 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), | |
2904 | .trigger = mem_cgroup_reset, | |
2905 | .read_u64 = mem_cgroup_read, | |
2906 | }, | |
2907 | { | |
2908 | .name = "memsw.limit_in_bytes", | |
2909 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), | |
2910 | .write_string = mem_cgroup_write, | |
2911 | .read_u64 = mem_cgroup_read, | |
2912 | }, | |
2913 | { | |
2914 | .name = "memsw.failcnt", | |
2915 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), | |
2916 | .trigger = mem_cgroup_reset, | |
2917 | .read_u64 = mem_cgroup_read, | |
2918 | }, | |
2919 | }; | |
2920 | ||
2921 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) | |
2922 | { | |
2923 | if (!do_swap_account) | |
2924 | return 0; | |
2925 | return cgroup_add_files(cont, ss, memsw_cgroup_files, | |
2926 | ARRAY_SIZE(memsw_cgroup_files)); | |
2927 | }; | |
2928 | #else | |
2929 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) | |
2930 | { | |
2931 | return 0; | |
2932 | } | |
2933 | #endif | |
2934 | ||
6d12e2d8 KH |
2935 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
2936 | { | |
2937 | struct mem_cgroup_per_node *pn; | |
1ecaab2b | 2938 | struct mem_cgroup_per_zone *mz; |
b69408e8 | 2939 | enum lru_list l; |
41e3355d | 2940 | int zone, tmp = node; |
1ecaab2b KH |
2941 | /* |
2942 | * This routine is called against possible nodes. | |
2943 | * But it's BUG to call kmalloc() against offline node. | |
2944 | * | |
2945 | * TODO: this routine can waste much memory for nodes which will | |
2946 | * never be onlined. It's better to use memory hotplug callback | |
2947 | * function. | |
2948 | */ | |
41e3355d KH |
2949 | if (!node_state(node, N_NORMAL_MEMORY)) |
2950 | tmp = -1; | |
2951 | pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp); | |
6d12e2d8 KH |
2952 | if (!pn) |
2953 | return 1; | |
1ecaab2b | 2954 | |
6d12e2d8 KH |
2955 | mem->info.nodeinfo[node] = pn; |
2956 | memset(pn, 0, sizeof(*pn)); | |
1ecaab2b KH |
2957 | |
2958 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { | |
2959 | mz = &pn->zoneinfo[zone]; | |
b69408e8 CL |
2960 | for_each_lru(l) |
2961 | INIT_LIST_HEAD(&mz->lists[l]); | |
f64c3f54 | 2962 | mz->usage_in_excess = 0; |
4e416953 BS |
2963 | mz->on_tree = false; |
2964 | mz->mem = mem; | |
1ecaab2b | 2965 | } |
6d12e2d8 KH |
2966 | return 0; |
2967 | } | |
2968 | ||
1ecaab2b KH |
2969 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
2970 | { | |
2971 | kfree(mem->info.nodeinfo[node]); | |
2972 | } | |
2973 | ||
c8dad2bb JB |
2974 | static int mem_cgroup_size(void) |
2975 | { | |
2976 | int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu); | |
2977 | return sizeof(struct mem_cgroup) + cpustat_size; | |
2978 | } | |
2979 | ||
33327948 KH |
2980 | static struct mem_cgroup *mem_cgroup_alloc(void) |
2981 | { | |
2982 | struct mem_cgroup *mem; | |
c8dad2bb | 2983 | int size = mem_cgroup_size(); |
33327948 | 2984 | |
c8dad2bb JB |
2985 | if (size < PAGE_SIZE) |
2986 | mem = kmalloc(size, GFP_KERNEL); | |
33327948 | 2987 | else |
c8dad2bb | 2988 | mem = vmalloc(size); |
33327948 KH |
2989 | |
2990 | if (mem) | |
c8dad2bb | 2991 | memset(mem, 0, size); |
33327948 KH |
2992 | return mem; |
2993 | } | |
2994 | ||
8c7c6e34 KH |
2995 | /* |
2996 | * At destroying mem_cgroup, references from swap_cgroup can remain. | |
2997 | * (scanning all at force_empty is too costly...) | |
2998 | * | |
2999 | * Instead of clearing all references at force_empty, we remember | |
3000 | * the number of reference from swap_cgroup and free mem_cgroup when | |
3001 | * it goes down to 0. | |
3002 | * | |
8c7c6e34 KH |
3003 | * Removal of cgroup itself succeeds regardless of refs from swap. |
3004 | */ | |
3005 | ||
a7ba0eef | 3006 | static void __mem_cgroup_free(struct mem_cgroup *mem) |
33327948 | 3007 | { |
08e552c6 KH |
3008 | int node; |
3009 | ||
f64c3f54 | 3010 | mem_cgroup_remove_from_trees(mem); |
04046e1a KH |
3011 | free_css_id(&mem_cgroup_subsys, &mem->css); |
3012 | ||
08e552c6 KH |
3013 | for_each_node_state(node, N_POSSIBLE) |
3014 | free_mem_cgroup_per_zone_info(mem, node); | |
3015 | ||
c8dad2bb | 3016 | if (mem_cgroup_size() < PAGE_SIZE) |
33327948 KH |
3017 | kfree(mem); |
3018 | else | |
3019 | vfree(mem); | |
3020 | } | |
3021 | ||
8c7c6e34 KH |
3022 | static void mem_cgroup_get(struct mem_cgroup *mem) |
3023 | { | |
3024 | atomic_inc(&mem->refcnt); | |
3025 | } | |
3026 | ||
3027 | static void mem_cgroup_put(struct mem_cgroup *mem) | |
3028 | { | |
7bcc1bb1 DN |
3029 | if (atomic_dec_and_test(&mem->refcnt)) { |
3030 | struct mem_cgroup *parent = parent_mem_cgroup(mem); | |
a7ba0eef | 3031 | __mem_cgroup_free(mem); |
7bcc1bb1 DN |
3032 | if (parent) |
3033 | mem_cgroup_put(parent); | |
3034 | } | |
8c7c6e34 KH |
3035 | } |
3036 | ||
7bcc1bb1 DN |
3037 | /* |
3038 | * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled. | |
3039 | */ | |
3040 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem) | |
3041 | { | |
3042 | if (!mem->res.parent) | |
3043 | return NULL; | |
3044 | return mem_cgroup_from_res_counter(mem->res.parent, res); | |
3045 | } | |
33327948 | 3046 | |
c077719b KH |
3047 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
3048 | static void __init enable_swap_cgroup(void) | |
3049 | { | |
f8d66542 | 3050 | if (!mem_cgroup_disabled() && really_do_swap_account) |
c077719b KH |
3051 | do_swap_account = 1; |
3052 | } | |
3053 | #else | |
3054 | static void __init enable_swap_cgroup(void) | |
3055 | { | |
3056 | } | |
3057 | #endif | |
3058 | ||
f64c3f54 BS |
3059 | static int mem_cgroup_soft_limit_tree_init(void) |
3060 | { | |
3061 | struct mem_cgroup_tree_per_node *rtpn; | |
3062 | struct mem_cgroup_tree_per_zone *rtpz; | |
3063 | int tmp, node, zone; | |
3064 | ||
3065 | for_each_node_state(node, N_POSSIBLE) { | |
3066 | tmp = node; | |
3067 | if (!node_state(node, N_NORMAL_MEMORY)) | |
3068 | tmp = -1; | |
3069 | rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp); | |
3070 | if (!rtpn) | |
3071 | return 1; | |
3072 | ||
3073 | soft_limit_tree.rb_tree_per_node[node] = rtpn; | |
3074 | ||
3075 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { | |
3076 | rtpz = &rtpn->rb_tree_per_zone[zone]; | |
3077 | rtpz->rb_root = RB_ROOT; | |
3078 | spin_lock_init(&rtpz->lock); | |
3079 | } | |
3080 | } | |
3081 | return 0; | |
3082 | } | |
3083 | ||
0eb253e2 | 3084 | static struct cgroup_subsys_state * __ref |
8cdea7c0 BS |
3085 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
3086 | { | |
28dbc4b6 | 3087 | struct mem_cgroup *mem, *parent; |
04046e1a | 3088 | long error = -ENOMEM; |
6d12e2d8 | 3089 | int node; |
8cdea7c0 | 3090 | |
c8dad2bb JB |
3091 | mem = mem_cgroup_alloc(); |
3092 | if (!mem) | |
04046e1a | 3093 | return ERR_PTR(error); |
78fb7466 | 3094 | |
6d12e2d8 KH |
3095 | for_each_node_state(node, N_POSSIBLE) |
3096 | if (alloc_mem_cgroup_per_zone_info(mem, node)) | |
3097 | goto free_out; | |
f64c3f54 | 3098 | |
c077719b | 3099 | /* root ? */ |
28dbc4b6 | 3100 | if (cont->parent == NULL) { |
c077719b | 3101 | enable_swap_cgroup(); |
28dbc4b6 | 3102 | parent = NULL; |
4b3bde4c | 3103 | root_mem_cgroup = mem; |
f64c3f54 BS |
3104 | if (mem_cgroup_soft_limit_tree_init()) |
3105 | goto free_out; | |
3106 | ||
18f59ea7 | 3107 | } else { |
28dbc4b6 | 3108 | parent = mem_cgroup_from_cont(cont->parent); |
18f59ea7 BS |
3109 | mem->use_hierarchy = parent->use_hierarchy; |
3110 | } | |
28dbc4b6 | 3111 | |
18f59ea7 BS |
3112 | if (parent && parent->use_hierarchy) { |
3113 | res_counter_init(&mem->res, &parent->res); | |
3114 | res_counter_init(&mem->memsw, &parent->memsw); | |
7bcc1bb1 DN |
3115 | /* |
3116 | * We increment refcnt of the parent to ensure that we can | |
3117 | * safely access it on res_counter_charge/uncharge. | |
3118 | * This refcnt will be decremented when freeing this | |
3119 | * mem_cgroup(see mem_cgroup_put). | |
3120 | */ | |
3121 | mem_cgroup_get(parent); | |
18f59ea7 BS |
3122 | } else { |
3123 | res_counter_init(&mem->res, NULL); | |
3124 | res_counter_init(&mem->memsw, NULL); | |
3125 | } | |
04046e1a | 3126 | mem->last_scanned_child = 0; |
2733c06a | 3127 | spin_lock_init(&mem->reclaim_param_lock); |
6d61ef40 | 3128 | |
a7885eb8 KM |
3129 | if (parent) |
3130 | mem->swappiness = get_swappiness(parent); | |
a7ba0eef | 3131 | atomic_set(&mem->refcnt, 1); |
8cdea7c0 | 3132 | return &mem->css; |
6d12e2d8 | 3133 | free_out: |
a7ba0eef | 3134 | __mem_cgroup_free(mem); |
4b3bde4c | 3135 | root_mem_cgroup = NULL; |
04046e1a | 3136 | return ERR_PTR(error); |
8cdea7c0 BS |
3137 | } |
3138 | ||
ec64f515 | 3139 | static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
df878fb0 KH |
3140 | struct cgroup *cont) |
3141 | { | |
3142 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); | |
ec64f515 KH |
3143 | |
3144 | return mem_cgroup_force_empty(mem, false); | |
df878fb0 KH |
3145 | } |
3146 | ||
8cdea7c0 BS |
3147 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
3148 | struct cgroup *cont) | |
3149 | { | |
c268e994 | 3150 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
c268e994 | 3151 | |
c268e994 | 3152 | mem_cgroup_put(mem); |
8cdea7c0 BS |
3153 | } |
3154 | ||
3155 | static int mem_cgroup_populate(struct cgroup_subsys *ss, | |
3156 | struct cgroup *cont) | |
3157 | { | |
8c7c6e34 KH |
3158 | int ret; |
3159 | ||
3160 | ret = cgroup_add_files(cont, ss, mem_cgroup_files, | |
3161 | ARRAY_SIZE(mem_cgroup_files)); | |
3162 | ||
3163 | if (!ret) | |
3164 | ret = register_memsw_files(cont, ss); | |
3165 | return ret; | |
8cdea7c0 BS |
3166 | } |
3167 | ||
67e465a7 BS |
3168 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
3169 | struct cgroup *cont, | |
3170 | struct cgroup *old_cont, | |
be367d09 BB |
3171 | struct task_struct *p, |
3172 | bool threadgroup) | |
67e465a7 | 3173 | { |
7f4d454d | 3174 | mutex_lock(&memcg_tasklist); |
67e465a7 | 3175 | /* |
f9717d28 NK |
3176 | * FIXME: It's better to move charges of this process from old |
3177 | * memcg to new memcg. But it's just on TODO-List now. | |
67e465a7 | 3178 | */ |
7f4d454d | 3179 | mutex_unlock(&memcg_tasklist); |
67e465a7 BS |
3180 | } |
3181 | ||
8cdea7c0 BS |
3182 | struct cgroup_subsys mem_cgroup_subsys = { |
3183 | .name = "memory", | |
3184 | .subsys_id = mem_cgroup_subsys_id, | |
3185 | .create = mem_cgroup_create, | |
df878fb0 | 3186 | .pre_destroy = mem_cgroup_pre_destroy, |
8cdea7c0 BS |
3187 | .destroy = mem_cgroup_destroy, |
3188 | .populate = mem_cgroup_populate, | |
67e465a7 | 3189 | .attach = mem_cgroup_move_task, |
6d12e2d8 | 3190 | .early_init = 0, |
04046e1a | 3191 | .use_id = 1, |
8cdea7c0 | 3192 | }; |
c077719b KH |
3193 | |
3194 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP | |
3195 | ||
3196 | static int __init disable_swap_account(char *s) | |
3197 | { | |
3198 | really_do_swap_account = 0; | |
3199 | return 1; | |
3200 | } | |
3201 | __setup("noswapaccount", disable_swap_account); | |
3202 | #endif |