]>
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> |
b6ac57d5 | 32 | #include <linux/slab.h> |
66e1707b BS |
33 | #include <linux/swap.h> |
34 | #include <linux/spinlock.h> | |
35 | #include <linux/fs.h> | |
d2ceb9b7 | 36 | #include <linux/seq_file.h> |
33327948 | 37 | #include <linux/vmalloc.h> |
b69408e8 | 38 | #include <linux/mm_inline.h> |
52d4b9ac | 39 | #include <linux/page_cgroup.h> |
08e552c6 | 40 | #include "internal.h" |
8cdea7c0 | 41 | |
8697d331 BS |
42 | #include <asm/uaccess.h> |
43 | ||
a181b0e8 | 44 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
a181b0e8 | 45 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
8cdea7c0 | 46 | |
c077719b KH |
47 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
48 | /* Turned on only when memory cgroup is enabled && really_do_swap_account = 0 */ | |
49 | int do_swap_account __read_mostly; | |
50 | static int really_do_swap_account __initdata = 1; /* for remember boot option*/ | |
51 | #else | |
52 | #define do_swap_account (0) | |
53 | #endif | |
54 | ||
7f4d454d | 55 | static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */ |
c077719b | 56 | |
d52aa412 KH |
57 | /* |
58 | * Statistics for memory cgroup. | |
59 | */ | |
60 | enum mem_cgroup_stat_index { | |
61 | /* | |
62 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. | |
63 | */ | |
64 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ | |
65 | MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */ | |
55e462b0 BR |
66 | MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */ |
67 | MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */ | |
d52aa412 KH |
68 | |
69 | MEM_CGROUP_STAT_NSTATS, | |
70 | }; | |
71 | ||
72 | struct mem_cgroup_stat_cpu { | |
73 | s64 count[MEM_CGROUP_STAT_NSTATS]; | |
74 | } ____cacheline_aligned_in_smp; | |
75 | ||
76 | struct mem_cgroup_stat { | |
c8dad2bb | 77 | struct mem_cgroup_stat_cpu cpustat[0]; |
d52aa412 KH |
78 | }; |
79 | ||
80 | /* | |
81 | * For accounting under irq disable, no need for increment preempt count. | |
82 | */ | |
addb9efe | 83 | static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat, |
d52aa412 KH |
84 | enum mem_cgroup_stat_index idx, int val) |
85 | { | |
addb9efe | 86 | stat->count[idx] += val; |
d52aa412 KH |
87 | } |
88 | ||
89 | static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat, | |
90 | enum mem_cgroup_stat_index idx) | |
91 | { | |
92 | int cpu; | |
93 | s64 ret = 0; | |
94 | for_each_possible_cpu(cpu) | |
95 | ret += stat->cpustat[cpu].count[idx]; | |
96 | return ret; | |
97 | } | |
98 | ||
04046e1a KH |
99 | static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat) |
100 | { | |
101 | s64 ret; | |
102 | ||
103 | ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE); | |
104 | ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS); | |
105 | return ret; | |
106 | } | |
107 | ||
6d12e2d8 KH |
108 | /* |
109 | * per-zone information in memory controller. | |
110 | */ | |
6d12e2d8 | 111 | struct mem_cgroup_per_zone { |
072c56c1 KH |
112 | /* |
113 | * spin_lock to protect the per cgroup LRU | |
114 | */ | |
b69408e8 CL |
115 | struct list_head lists[NR_LRU_LISTS]; |
116 | unsigned long count[NR_LRU_LISTS]; | |
3e2f41f1 KM |
117 | |
118 | struct zone_reclaim_stat reclaim_stat; | |
6d12e2d8 KH |
119 | }; |
120 | /* Macro for accessing counter */ | |
121 | #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)]) | |
122 | ||
123 | struct mem_cgroup_per_node { | |
124 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; | |
125 | }; | |
126 | ||
127 | struct mem_cgroup_lru_info { | |
128 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; | |
129 | }; | |
130 | ||
8cdea7c0 BS |
131 | /* |
132 | * The memory controller data structure. The memory controller controls both | |
133 | * page cache and RSS per cgroup. We would eventually like to provide | |
134 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, | |
135 | * to help the administrator determine what knobs to tune. | |
136 | * | |
137 | * TODO: Add a water mark for the memory controller. Reclaim will begin when | |
8a9f3ccd BS |
138 | * we hit the water mark. May be even add a low water mark, such that |
139 | * no reclaim occurs from a cgroup at it's low water mark, this is | |
140 | * a feature that will be implemented much later in the future. | |
8cdea7c0 BS |
141 | */ |
142 | struct mem_cgroup { | |
143 | struct cgroup_subsys_state css; | |
144 | /* | |
145 | * the counter to account for memory usage | |
146 | */ | |
147 | struct res_counter res; | |
8c7c6e34 KH |
148 | /* |
149 | * the counter to account for mem+swap usage. | |
150 | */ | |
151 | struct res_counter memsw; | |
78fb7466 PE |
152 | /* |
153 | * Per cgroup active and inactive list, similar to the | |
154 | * per zone LRU lists. | |
78fb7466 | 155 | */ |
6d12e2d8 | 156 | struct mem_cgroup_lru_info info; |
072c56c1 | 157 | |
2733c06a KM |
158 | /* |
159 | protect against reclaim related member. | |
160 | */ | |
161 | spinlock_t reclaim_param_lock; | |
162 | ||
6c48a1d0 | 163 | int prev_priority; /* for recording reclaim priority */ |
6d61ef40 BS |
164 | |
165 | /* | |
166 | * While reclaiming in a hiearchy, we cache the last child we | |
04046e1a | 167 | * reclaimed from. |
6d61ef40 | 168 | */ |
04046e1a | 169 | int last_scanned_child; |
18f59ea7 BS |
170 | /* |
171 | * Should the accounting and control be hierarchical, per subtree? | |
172 | */ | |
173 | bool use_hierarchy; | |
a636b327 | 174 | unsigned long last_oom_jiffies; |
8c7c6e34 | 175 | atomic_t refcnt; |
14797e23 | 176 | |
a7885eb8 KM |
177 | unsigned int swappiness; |
178 | ||
d52aa412 | 179 | /* |
c8dad2bb | 180 | * statistics. This must be placed at the end of memcg. |
d52aa412 KH |
181 | */ |
182 | struct mem_cgroup_stat stat; | |
8cdea7c0 BS |
183 | }; |
184 | ||
217bc319 KH |
185 | enum charge_type { |
186 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, | |
187 | MEM_CGROUP_CHARGE_TYPE_MAPPED, | |
4f98a2fe | 188 | MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */ |
c05555b5 | 189 | MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */ |
d13d1443 | 190 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
c05555b5 KH |
191 | NR_CHARGE_TYPE, |
192 | }; | |
193 | ||
52d4b9ac KH |
194 | /* only for here (for easy reading.) */ |
195 | #define PCGF_CACHE (1UL << PCG_CACHE) | |
196 | #define PCGF_USED (1UL << PCG_USED) | |
52d4b9ac | 197 | #define PCGF_LOCK (1UL << PCG_LOCK) |
c05555b5 KH |
198 | static const unsigned long |
199 | pcg_default_flags[NR_CHARGE_TYPE] = { | |
08e552c6 KH |
200 | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* File Cache */ |
201 | PCGF_USED | PCGF_LOCK, /* Anon */ | |
202 | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */ | |
52d4b9ac | 203 | 0, /* FORCE */ |
217bc319 KH |
204 | }; |
205 | ||
8c7c6e34 KH |
206 | /* for encoding cft->private value on file */ |
207 | #define _MEM (0) | |
208 | #define _MEMSWAP (1) | |
209 | #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val)) | |
210 | #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff) | |
211 | #define MEMFILE_ATTR(val) ((val) & 0xffff) | |
212 | ||
213 | static void mem_cgroup_get(struct mem_cgroup *mem); | |
214 | static void mem_cgroup_put(struct mem_cgroup *mem); | |
7bcc1bb1 | 215 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem); |
8c7c6e34 | 216 | |
c05555b5 KH |
217 | static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, |
218 | struct page_cgroup *pc, | |
219 | bool charge) | |
d52aa412 KH |
220 | { |
221 | int val = (charge)? 1 : -1; | |
222 | struct mem_cgroup_stat *stat = &mem->stat; | |
addb9efe | 223 | struct mem_cgroup_stat_cpu *cpustat; |
08e552c6 | 224 | int cpu = get_cpu(); |
d52aa412 | 225 | |
08e552c6 | 226 | cpustat = &stat->cpustat[cpu]; |
c05555b5 | 227 | if (PageCgroupCache(pc)) |
addb9efe | 228 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val); |
d52aa412 | 229 | else |
addb9efe | 230 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val); |
55e462b0 BR |
231 | |
232 | if (charge) | |
addb9efe | 233 | __mem_cgroup_stat_add_safe(cpustat, |
55e462b0 BR |
234 | MEM_CGROUP_STAT_PGPGIN_COUNT, 1); |
235 | else | |
addb9efe | 236 | __mem_cgroup_stat_add_safe(cpustat, |
55e462b0 | 237 | MEM_CGROUP_STAT_PGPGOUT_COUNT, 1); |
08e552c6 | 238 | put_cpu(); |
6d12e2d8 KH |
239 | } |
240 | ||
d5b69e38 | 241 | static struct mem_cgroup_per_zone * |
6d12e2d8 KH |
242 | mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid) |
243 | { | |
6d12e2d8 KH |
244 | return &mem->info.nodeinfo[nid]->zoneinfo[zid]; |
245 | } | |
246 | ||
d5b69e38 | 247 | static struct mem_cgroup_per_zone * |
6d12e2d8 KH |
248 | page_cgroup_zoneinfo(struct page_cgroup *pc) |
249 | { | |
250 | struct mem_cgroup *mem = pc->mem_cgroup; | |
251 | int nid = page_cgroup_nid(pc); | |
252 | int zid = page_cgroup_zid(pc); | |
d52aa412 | 253 | |
54992762 KM |
254 | if (!mem) |
255 | return NULL; | |
256 | ||
6d12e2d8 KH |
257 | return mem_cgroup_zoneinfo(mem, nid, zid); |
258 | } | |
259 | ||
14067bb3 | 260 | static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem, |
b69408e8 | 261 | enum lru_list idx) |
6d12e2d8 KH |
262 | { |
263 | int nid, zid; | |
264 | struct mem_cgroup_per_zone *mz; | |
265 | u64 total = 0; | |
266 | ||
267 | for_each_online_node(nid) | |
268 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { | |
269 | mz = mem_cgroup_zoneinfo(mem, nid, zid); | |
270 | total += MEM_CGROUP_ZSTAT(mz, idx); | |
271 | } | |
272 | return total; | |
d52aa412 KH |
273 | } |
274 | ||
d5b69e38 | 275 | static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
8cdea7c0 BS |
276 | { |
277 | return container_of(cgroup_subsys_state(cont, | |
278 | mem_cgroup_subsys_id), struct mem_cgroup, | |
279 | css); | |
280 | } | |
281 | ||
cf475ad2 | 282 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
78fb7466 | 283 | { |
31a78f23 BS |
284 | /* |
285 | * mm_update_next_owner() may clear mm->owner to NULL | |
286 | * if it races with swapoff, page migration, etc. | |
287 | * So this can be called with p == NULL. | |
288 | */ | |
289 | if (unlikely(!p)) | |
290 | return NULL; | |
291 | ||
78fb7466 PE |
292 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
293 | struct mem_cgroup, css); | |
294 | } | |
295 | ||
54595fe2 KH |
296 | static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm) |
297 | { | |
298 | struct mem_cgroup *mem = NULL; | |
0b7f569e KH |
299 | |
300 | if (!mm) | |
301 | return NULL; | |
54595fe2 KH |
302 | /* |
303 | * Because we have no locks, mm->owner's may be being moved to other | |
304 | * cgroup. We use css_tryget() here even if this looks | |
305 | * pessimistic (rather than adding locks here). | |
306 | */ | |
307 | rcu_read_lock(); | |
308 | do { | |
309 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); | |
310 | if (unlikely(!mem)) | |
311 | break; | |
312 | } while (!css_tryget(&mem->css)); | |
313 | rcu_read_unlock(); | |
314 | return mem; | |
315 | } | |
316 | ||
317 | static bool mem_cgroup_is_obsolete(struct mem_cgroup *mem) | |
318 | { | |
319 | if (!mem) | |
320 | return true; | |
321 | return css_is_removed(&mem->css); | |
322 | } | |
323 | ||
14067bb3 KH |
324 | |
325 | /* | |
326 | * Call callback function against all cgroup under hierarchy tree. | |
327 | */ | |
328 | static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data, | |
329 | int (*func)(struct mem_cgroup *, void *)) | |
330 | { | |
331 | int found, ret, nextid; | |
332 | struct cgroup_subsys_state *css; | |
333 | struct mem_cgroup *mem; | |
334 | ||
335 | if (!root->use_hierarchy) | |
336 | return (*func)(root, data); | |
337 | ||
338 | nextid = 1; | |
339 | do { | |
340 | ret = 0; | |
341 | mem = NULL; | |
342 | ||
343 | rcu_read_lock(); | |
344 | css = css_get_next(&mem_cgroup_subsys, nextid, &root->css, | |
345 | &found); | |
346 | if (css && css_tryget(css)) | |
347 | mem = container_of(css, struct mem_cgroup, css); | |
348 | rcu_read_unlock(); | |
349 | ||
350 | if (mem) { | |
351 | ret = (*func)(mem, data); | |
352 | css_put(&mem->css); | |
353 | } | |
354 | nextid = found + 1; | |
355 | } while (!ret && css); | |
356 | ||
357 | return ret; | |
358 | } | |
359 | ||
08e552c6 KH |
360 | /* |
361 | * Following LRU functions are allowed to be used without PCG_LOCK. | |
362 | * Operations are called by routine of global LRU independently from memcg. | |
363 | * What we have to take care of here is validness of pc->mem_cgroup. | |
364 | * | |
365 | * Changes to pc->mem_cgroup happens when | |
366 | * 1. charge | |
367 | * 2. moving account | |
368 | * In typical case, "charge" is done before add-to-lru. Exception is SwapCache. | |
369 | * It is added to LRU before charge. | |
370 | * If PCG_USED bit is not set, page_cgroup is not added to this private LRU. | |
371 | * When moving account, the page is not on LRU. It's isolated. | |
372 | */ | |
4f98a2fe | 373 | |
08e552c6 KH |
374 | void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru) |
375 | { | |
376 | struct page_cgroup *pc; | |
377 | struct mem_cgroup *mem; | |
378 | struct mem_cgroup_per_zone *mz; | |
6d12e2d8 | 379 | |
f8d66542 | 380 | if (mem_cgroup_disabled()) |
08e552c6 KH |
381 | return; |
382 | pc = lookup_page_cgroup(page); | |
383 | /* can happen while we handle swapcache. */ | |
544122e5 | 384 | if (list_empty(&pc->lru) || !pc->mem_cgroup) |
08e552c6 | 385 | return; |
544122e5 KH |
386 | /* |
387 | * We don't check PCG_USED bit. It's cleared when the "page" is finally | |
388 | * removed from global LRU. | |
389 | */ | |
08e552c6 KH |
390 | mz = page_cgroup_zoneinfo(pc); |
391 | mem = pc->mem_cgroup; | |
b69408e8 | 392 | MEM_CGROUP_ZSTAT(mz, lru) -= 1; |
08e552c6 KH |
393 | list_del_init(&pc->lru); |
394 | return; | |
6d12e2d8 KH |
395 | } |
396 | ||
08e552c6 | 397 | void mem_cgroup_del_lru(struct page *page) |
6d12e2d8 | 398 | { |
08e552c6 KH |
399 | mem_cgroup_del_lru_list(page, page_lru(page)); |
400 | } | |
b69408e8 | 401 | |
08e552c6 KH |
402 | void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru) |
403 | { | |
404 | struct mem_cgroup_per_zone *mz; | |
405 | struct page_cgroup *pc; | |
b69408e8 | 406 | |
f8d66542 | 407 | if (mem_cgroup_disabled()) |
08e552c6 | 408 | return; |
6d12e2d8 | 409 | |
08e552c6 | 410 | pc = lookup_page_cgroup(page); |
bd112db8 DN |
411 | /* |
412 | * Used bit is set without atomic ops but after smp_wmb(). | |
413 | * For making pc->mem_cgroup visible, insert smp_rmb() here. | |
414 | */ | |
08e552c6 KH |
415 | smp_rmb(); |
416 | /* unused page is not rotated. */ | |
417 | if (!PageCgroupUsed(pc)) | |
418 | return; | |
419 | mz = page_cgroup_zoneinfo(pc); | |
420 | list_move(&pc->lru, &mz->lists[lru]); | |
6d12e2d8 KH |
421 | } |
422 | ||
08e552c6 | 423 | void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru) |
66e1707b | 424 | { |
08e552c6 KH |
425 | struct page_cgroup *pc; |
426 | struct mem_cgroup_per_zone *mz; | |
6d12e2d8 | 427 | |
f8d66542 | 428 | if (mem_cgroup_disabled()) |
08e552c6 KH |
429 | return; |
430 | pc = lookup_page_cgroup(page); | |
bd112db8 DN |
431 | /* |
432 | * Used bit is set without atomic ops but after smp_wmb(). | |
433 | * For making pc->mem_cgroup visible, insert smp_rmb() here. | |
434 | */ | |
08e552c6 KH |
435 | smp_rmb(); |
436 | if (!PageCgroupUsed(pc)) | |
894bc310 | 437 | return; |
b69408e8 | 438 | |
08e552c6 | 439 | mz = page_cgroup_zoneinfo(pc); |
b69408e8 | 440 | MEM_CGROUP_ZSTAT(mz, lru) += 1; |
08e552c6 KH |
441 | list_add(&pc->lru, &mz->lists[lru]); |
442 | } | |
544122e5 | 443 | |
08e552c6 | 444 | /* |
544122e5 KH |
445 | * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to |
446 | * lru because the page may.be reused after it's fully uncharged (because of | |
447 | * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge | |
448 | * it again. This function is only used to charge SwapCache. It's done under | |
449 | * lock_page and expected that zone->lru_lock is never held. | |
08e552c6 | 450 | */ |
544122e5 | 451 | static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page) |
08e552c6 | 452 | { |
544122e5 KH |
453 | unsigned long flags; |
454 | struct zone *zone = page_zone(page); | |
455 | struct page_cgroup *pc = lookup_page_cgroup(page); | |
456 | ||
457 | spin_lock_irqsave(&zone->lru_lock, flags); | |
458 | /* | |
459 | * Forget old LRU when this page_cgroup is *not* used. This Used bit | |
460 | * is guarded by lock_page() because the page is SwapCache. | |
461 | */ | |
462 | if (!PageCgroupUsed(pc)) | |
463 | mem_cgroup_del_lru_list(page, page_lru(page)); | |
464 | spin_unlock_irqrestore(&zone->lru_lock, flags); | |
08e552c6 KH |
465 | } |
466 | ||
544122e5 KH |
467 | static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page) |
468 | { | |
469 | unsigned long flags; | |
470 | struct zone *zone = page_zone(page); | |
471 | struct page_cgroup *pc = lookup_page_cgroup(page); | |
472 | ||
473 | spin_lock_irqsave(&zone->lru_lock, flags); | |
474 | /* link when the page is linked to LRU but page_cgroup isn't */ | |
475 | if (PageLRU(page) && list_empty(&pc->lru)) | |
476 | mem_cgroup_add_lru_list(page, page_lru(page)); | |
477 | spin_unlock_irqrestore(&zone->lru_lock, flags); | |
478 | } | |
479 | ||
480 | ||
08e552c6 KH |
481 | void mem_cgroup_move_lists(struct page *page, |
482 | enum lru_list from, enum lru_list to) | |
483 | { | |
f8d66542 | 484 | if (mem_cgroup_disabled()) |
08e552c6 KH |
485 | return; |
486 | mem_cgroup_del_lru_list(page, from); | |
487 | mem_cgroup_add_lru_list(page, to); | |
66e1707b BS |
488 | } |
489 | ||
4c4a2214 DR |
490 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
491 | { | |
492 | int ret; | |
0b7f569e | 493 | struct mem_cgroup *curr = NULL; |
4c4a2214 DR |
494 | |
495 | task_lock(task); | |
0b7f569e KH |
496 | rcu_read_lock(); |
497 | curr = try_get_mem_cgroup_from_mm(task->mm); | |
498 | rcu_read_unlock(); | |
4c4a2214 | 499 | task_unlock(task); |
0b7f569e KH |
500 | if (!curr) |
501 | return 0; | |
502 | if (curr->use_hierarchy) | |
503 | ret = css_is_ancestor(&curr->css, &mem->css); | |
504 | else | |
505 | ret = (curr == mem); | |
506 | css_put(&curr->css); | |
4c4a2214 DR |
507 | return ret; |
508 | } | |
509 | ||
6c48a1d0 KH |
510 | /* |
511 | * prev_priority control...this will be used in memory reclaim path. | |
512 | */ | |
513 | int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem) | |
514 | { | |
2733c06a KM |
515 | int prev_priority; |
516 | ||
517 | spin_lock(&mem->reclaim_param_lock); | |
518 | prev_priority = mem->prev_priority; | |
519 | spin_unlock(&mem->reclaim_param_lock); | |
520 | ||
521 | return prev_priority; | |
6c48a1d0 KH |
522 | } |
523 | ||
524 | void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority) | |
525 | { | |
2733c06a | 526 | spin_lock(&mem->reclaim_param_lock); |
6c48a1d0 KH |
527 | if (priority < mem->prev_priority) |
528 | mem->prev_priority = priority; | |
2733c06a | 529 | spin_unlock(&mem->reclaim_param_lock); |
6c48a1d0 KH |
530 | } |
531 | ||
532 | void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority) | |
533 | { | |
2733c06a | 534 | spin_lock(&mem->reclaim_param_lock); |
6c48a1d0 | 535 | mem->prev_priority = priority; |
2733c06a | 536 | spin_unlock(&mem->reclaim_param_lock); |
6c48a1d0 KH |
537 | } |
538 | ||
c772be93 | 539 | static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages) |
14797e23 KM |
540 | { |
541 | unsigned long active; | |
542 | unsigned long inactive; | |
c772be93 KM |
543 | unsigned long gb; |
544 | unsigned long inactive_ratio; | |
14797e23 | 545 | |
14067bb3 KH |
546 | inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON); |
547 | active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON); | |
14797e23 | 548 | |
c772be93 KM |
549 | gb = (inactive + active) >> (30 - PAGE_SHIFT); |
550 | if (gb) | |
551 | inactive_ratio = int_sqrt(10 * gb); | |
552 | else | |
553 | inactive_ratio = 1; | |
554 | ||
555 | if (present_pages) { | |
556 | present_pages[0] = inactive; | |
557 | present_pages[1] = active; | |
558 | } | |
559 | ||
560 | return inactive_ratio; | |
561 | } | |
562 | ||
563 | int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg) | |
564 | { | |
565 | unsigned long active; | |
566 | unsigned long inactive; | |
567 | unsigned long present_pages[2]; | |
568 | unsigned long inactive_ratio; | |
569 | ||
570 | inactive_ratio = calc_inactive_ratio(memcg, present_pages); | |
571 | ||
572 | inactive = present_pages[0]; | |
573 | active = present_pages[1]; | |
574 | ||
575 | if (inactive * inactive_ratio < active) | |
14797e23 KM |
576 | return 1; |
577 | ||
578 | return 0; | |
579 | } | |
580 | ||
a3d8e054 KM |
581 | unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg, |
582 | struct zone *zone, | |
583 | enum lru_list lru) | |
584 | { | |
585 | int nid = zone->zone_pgdat->node_id; | |
586 | int zid = zone_idx(zone); | |
587 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); | |
588 | ||
589 | return MEM_CGROUP_ZSTAT(mz, lru); | |
590 | } | |
591 | ||
3e2f41f1 KM |
592 | struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg, |
593 | struct zone *zone) | |
594 | { | |
595 | int nid = zone->zone_pgdat->node_id; | |
596 | int zid = zone_idx(zone); | |
597 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid); | |
598 | ||
599 | return &mz->reclaim_stat; | |
600 | } | |
601 | ||
602 | struct zone_reclaim_stat * | |
603 | mem_cgroup_get_reclaim_stat_from_page(struct page *page) | |
604 | { | |
605 | struct page_cgroup *pc; | |
606 | struct mem_cgroup_per_zone *mz; | |
607 | ||
608 | if (mem_cgroup_disabled()) | |
609 | return NULL; | |
610 | ||
611 | pc = lookup_page_cgroup(page); | |
bd112db8 DN |
612 | /* |
613 | * Used bit is set without atomic ops but after smp_wmb(). | |
614 | * For making pc->mem_cgroup visible, insert smp_rmb() here. | |
615 | */ | |
616 | smp_rmb(); | |
617 | if (!PageCgroupUsed(pc)) | |
618 | return NULL; | |
619 | ||
3e2f41f1 KM |
620 | mz = page_cgroup_zoneinfo(pc); |
621 | if (!mz) | |
622 | return NULL; | |
623 | ||
624 | return &mz->reclaim_stat; | |
625 | } | |
626 | ||
66e1707b BS |
627 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
628 | struct list_head *dst, | |
629 | unsigned long *scanned, int order, | |
630 | int mode, struct zone *z, | |
631 | struct mem_cgroup *mem_cont, | |
4f98a2fe | 632 | int active, int file) |
66e1707b BS |
633 | { |
634 | unsigned long nr_taken = 0; | |
635 | struct page *page; | |
636 | unsigned long scan; | |
637 | LIST_HEAD(pc_list); | |
638 | struct list_head *src; | |
ff7283fa | 639 | struct page_cgroup *pc, *tmp; |
1ecaab2b KH |
640 | int nid = z->zone_pgdat->node_id; |
641 | int zid = zone_idx(z); | |
642 | struct mem_cgroup_per_zone *mz; | |
4f98a2fe | 643 | int lru = LRU_FILE * !!file + !!active; |
66e1707b | 644 | |
cf475ad2 | 645 | BUG_ON(!mem_cont); |
1ecaab2b | 646 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
b69408e8 | 647 | src = &mz->lists[lru]; |
66e1707b | 648 | |
ff7283fa KH |
649 | scan = 0; |
650 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { | |
436c6541 | 651 | if (scan >= nr_to_scan) |
ff7283fa | 652 | break; |
08e552c6 KH |
653 | |
654 | page = pc->page; | |
52d4b9ac KH |
655 | if (unlikely(!PageCgroupUsed(pc))) |
656 | continue; | |
436c6541 | 657 | if (unlikely(!PageLRU(page))) |
ff7283fa | 658 | continue; |
ff7283fa | 659 | |
436c6541 | 660 | scan++; |
4f98a2fe | 661 | if (__isolate_lru_page(page, mode, file) == 0) { |
66e1707b BS |
662 | list_move(&page->lru, dst); |
663 | nr_taken++; | |
664 | } | |
665 | } | |
666 | ||
66e1707b BS |
667 | *scanned = scan; |
668 | return nr_taken; | |
669 | } | |
670 | ||
6d61ef40 BS |
671 | #define mem_cgroup_from_res_counter(counter, member) \ |
672 | container_of(counter, struct mem_cgroup, member) | |
673 | ||
b85a96c0 DN |
674 | static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem) |
675 | { | |
676 | if (do_swap_account) { | |
677 | if (res_counter_check_under_limit(&mem->res) && | |
678 | res_counter_check_under_limit(&mem->memsw)) | |
679 | return true; | |
680 | } else | |
681 | if (res_counter_check_under_limit(&mem->res)) | |
682 | return true; | |
683 | return false; | |
684 | } | |
685 | ||
a7885eb8 KM |
686 | static unsigned int get_swappiness(struct mem_cgroup *memcg) |
687 | { | |
688 | struct cgroup *cgrp = memcg->css.cgroup; | |
689 | unsigned int swappiness; | |
690 | ||
691 | /* root ? */ | |
692 | if (cgrp->parent == NULL) | |
693 | return vm_swappiness; | |
694 | ||
695 | spin_lock(&memcg->reclaim_param_lock); | |
696 | swappiness = memcg->swappiness; | |
697 | spin_unlock(&memcg->reclaim_param_lock); | |
698 | ||
699 | return swappiness; | |
700 | } | |
701 | ||
81d39c20 KH |
702 | static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data) |
703 | { | |
704 | int *val = data; | |
705 | (*val)++; | |
706 | return 0; | |
707 | } | |
e222432b BS |
708 | |
709 | /** | |
710 | * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode. | |
711 | * @memcg: The memory cgroup that went over limit | |
712 | * @p: Task that is going to be killed | |
713 | * | |
714 | * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is | |
715 | * enabled | |
716 | */ | |
717 | void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p) | |
718 | { | |
719 | struct cgroup *task_cgrp; | |
720 | struct cgroup *mem_cgrp; | |
721 | /* | |
722 | * Need a buffer in BSS, can't rely on allocations. The code relies | |
723 | * on the assumption that OOM is serialized for memory controller. | |
724 | * If this assumption is broken, revisit this code. | |
725 | */ | |
726 | static char memcg_name[PATH_MAX]; | |
727 | int ret; | |
728 | ||
729 | if (!memcg) | |
730 | return; | |
731 | ||
732 | ||
733 | rcu_read_lock(); | |
734 | ||
735 | mem_cgrp = memcg->css.cgroup; | |
736 | task_cgrp = task_cgroup(p, mem_cgroup_subsys_id); | |
737 | ||
738 | ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX); | |
739 | if (ret < 0) { | |
740 | /* | |
741 | * Unfortunately, we are unable to convert to a useful name | |
742 | * But we'll still print out the usage information | |
743 | */ | |
744 | rcu_read_unlock(); | |
745 | goto done; | |
746 | } | |
747 | rcu_read_unlock(); | |
748 | ||
749 | printk(KERN_INFO "Task in %s killed", memcg_name); | |
750 | ||
751 | rcu_read_lock(); | |
752 | ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX); | |
753 | if (ret < 0) { | |
754 | rcu_read_unlock(); | |
755 | goto done; | |
756 | } | |
757 | rcu_read_unlock(); | |
758 | ||
759 | /* | |
760 | * Continues from above, so we don't need an KERN_ level | |
761 | */ | |
762 | printk(KERN_CONT " as a result of limit of %s\n", memcg_name); | |
763 | done: | |
764 | ||
765 | printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n", | |
766 | res_counter_read_u64(&memcg->res, RES_USAGE) >> 10, | |
767 | res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10, | |
768 | res_counter_read_u64(&memcg->res, RES_FAILCNT)); | |
769 | printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, " | |
770 | "failcnt %llu\n", | |
771 | res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10, | |
772 | res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10, | |
773 | res_counter_read_u64(&memcg->memsw, RES_FAILCNT)); | |
774 | } | |
775 | ||
81d39c20 KH |
776 | /* |
777 | * This function returns the number of memcg under hierarchy tree. Returns | |
778 | * 1(self count) if no children. | |
779 | */ | |
780 | static int mem_cgroup_count_children(struct mem_cgroup *mem) | |
781 | { | |
782 | int num = 0; | |
783 | mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb); | |
784 | return num; | |
785 | } | |
786 | ||
6d61ef40 | 787 | /* |
04046e1a KH |
788 | * Visit the first child (need not be the first child as per the ordering |
789 | * of the cgroup list, since we track last_scanned_child) of @mem and use | |
790 | * that to reclaim free pages from. | |
791 | */ | |
792 | static struct mem_cgroup * | |
793 | mem_cgroup_select_victim(struct mem_cgroup *root_mem) | |
794 | { | |
795 | struct mem_cgroup *ret = NULL; | |
796 | struct cgroup_subsys_state *css; | |
797 | int nextid, found; | |
798 | ||
799 | if (!root_mem->use_hierarchy) { | |
800 | css_get(&root_mem->css); | |
801 | ret = root_mem; | |
802 | } | |
803 | ||
804 | while (!ret) { | |
805 | rcu_read_lock(); | |
806 | nextid = root_mem->last_scanned_child + 1; | |
807 | css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css, | |
808 | &found); | |
809 | if (css && css_tryget(css)) | |
810 | ret = container_of(css, struct mem_cgroup, css); | |
811 | ||
812 | rcu_read_unlock(); | |
813 | /* Updates scanning parameter */ | |
814 | spin_lock(&root_mem->reclaim_param_lock); | |
815 | if (!css) { | |
816 | /* this means start scan from ID:1 */ | |
817 | root_mem->last_scanned_child = 0; | |
818 | } else | |
819 | root_mem->last_scanned_child = found; | |
820 | spin_unlock(&root_mem->reclaim_param_lock); | |
821 | } | |
822 | ||
823 | return ret; | |
824 | } | |
825 | ||
826 | /* | |
827 | * Scan the hierarchy if needed to reclaim memory. We remember the last child | |
828 | * we reclaimed from, so that we don't end up penalizing one child extensively | |
829 | * based on its position in the children list. | |
6d61ef40 BS |
830 | * |
831 | * root_mem is the original ancestor that we've been reclaim from. | |
04046e1a KH |
832 | * |
833 | * We give up and return to the caller when we visit root_mem twice. | |
834 | * (other groups can be removed while we're walking....) | |
81d39c20 KH |
835 | * |
836 | * If shrink==true, for avoiding to free too much, this returns immedieately. | |
6d61ef40 BS |
837 | */ |
838 | static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem, | |
81d39c20 | 839 | gfp_t gfp_mask, bool noswap, bool shrink) |
6d61ef40 | 840 | { |
04046e1a KH |
841 | struct mem_cgroup *victim; |
842 | int ret, total = 0; | |
843 | int loop = 0; | |
844 | ||
845 | while (loop < 2) { | |
846 | victim = mem_cgroup_select_victim(root_mem); | |
847 | if (victim == root_mem) | |
848 | loop++; | |
849 | if (!mem_cgroup_local_usage(&victim->stat)) { | |
850 | /* this cgroup's local usage == 0 */ | |
851 | css_put(&victim->css); | |
6d61ef40 BS |
852 | continue; |
853 | } | |
04046e1a KH |
854 | /* we use swappiness of local cgroup */ |
855 | ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, noswap, | |
856 | get_swappiness(victim)); | |
857 | css_put(&victim->css); | |
81d39c20 KH |
858 | /* |
859 | * At shrinking usage, we can't check we should stop here or | |
860 | * reclaim more. It's depends on callers. last_scanned_child | |
861 | * will work enough for keeping fairness under tree. | |
862 | */ | |
863 | if (shrink) | |
864 | return ret; | |
04046e1a | 865 | total += ret; |
b85a96c0 | 866 | if (mem_cgroup_check_under_limit(root_mem)) |
04046e1a | 867 | return 1 + total; |
6d61ef40 | 868 | } |
04046e1a | 869 | return total; |
6d61ef40 BS |
870 | } |
871 | ||
a636b327 KH |
872 | bool mem_cgroup_oom_called(struct task_struct *task) |
873 | { | |
874 | bool ret = false; | |
875 | struct mem_cgroup *mem; | |
876 | struct mm_struct *mm; | |
877 | ||
878 | rcu_read_lock(); | |
879 | mm = task->mm; | |
880 | if (!mm) | |
881 | mm = &init_mm; | |
882 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); | |
883 | if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10)) | |
884 | ret = true; | |
885 | rcu_read_unlock(); | |
886 | return ret; | |
887 | } | |
0b7f569e KH |
888 | |
889 | static int record_last_oom_cb(struct mem_cgroup *mem, void *data) | |
890 | { | |
891 | mem->last_oom_jiffies = jiffies; | |
892 | return 0; | |
893 | } | |
894 | ||
895 | static void record_last_oom(struct mem_cgroup *mem) | |
896 | { | |
897 | mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb); | |
898 | } | |
899 | ||
900 | ||
f817ed48 KH |
901 | /* |
902 | * Unlike exported interface, "oom" parameter is added. if oom==true, | |
903 | * oom-killer can be invoked. | |
8a9f3ccd | 904 | */ |
f817ed48 | 905 | static int __mem_cgroup_try_charge(struct mm_struct *mm, |
8c7c6e34 KH |
906 | gfp_t gfp_mask, struct mem_cgroup **memcg, |
907 | bool oom) | |
8a9f3ccd | 908 | { |
6d61ef40 | 909 | struct mem_cgroup *mem, *mem_over_limit; |
7a81b88c | 910 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
28dbc4b6 | 911 | struct res_counter *fail_res; |
a636b327 KH |
912 | |
913 | if (unlikely(test_thread_flag(TIF_MEMDIE))) { | |
914 | /* Don't account this! */ | |
915 | *memcg = NULL; | |
916 | return 0; | |
917 | } | |
918 | ||
8a9f3ccd | 919 | /* |
3be91277 HD |
920 | * We always charge the cgroup the mm_struct belongs to. |
921 | * The mm_struct's mem_cgroup changes on task migration if the | |
8a9f3ccd BS |
922 | * thread group leader migrates. It's possible that mm is not |
923 | * set, if so charge the init_mm (happens for pagecache usage). | |
924 | */ | |
54595fe2 KH |
925 | mem = *memcg; |
926 | if (likely(!mem)) { | |
927 | mem = try_get_mem_cgroup_from_mm(mm); | |
7a81b88c | 928 | *memcg = mem; |
e8589cc1 | 929 | } else { |
7a81b88c | 930 | css_get(&mem->css); |
e8589cc1 | 931 | } |
54595fe2 KH |
932 | if (unlikely(!mem)) |
933 | return 0; | |
934 | ||
935 | VM_BUG_ON(mem_cgroup_is_obsolete(mem)); | |
8a9f3ccd | 936 | |
8c7c6e34 KH |
937 | while (1) { |
938 | int ret; | |
939 | bool noswap = false; | |
7a81b88c | 940 | |
28dbc4b6 | 941 | ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res); |
8c7c6e34 KH |
942 | if (likely(!ret)) { |
943 | if (!do_swap_account) | |
944 | break; | |
28dbc4b6 BS |
945 | ret = res_counter_charge(&mem->memsw, PAGE_SIZE, |
946 | &fail_res); | |
8c7c6e34 KH |
947 | if (likely(!ret)) |
948 | break; | |
949 | /* mem+swap counter fails */ | |
950 | res_counter_uncharge(&mem->res, PAGE_SIZE); | |
951 | noswap = true; | |
6d61ef40 BS |
952 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, |
953 | memsw); | |
954 | } else | |
955 | /* mem counter fails */ | |
956 | mem_over_limit = mem_cgroup_from_res_counter(fail_res, | |
957 | res); | |
958 | ||
3be91277 | 959 | if (!(gfp_mask & __GFP_WAIT)) |
7a81b88c | 960 | goto nomem; |
e1a1cd59 | 961 | |
6d61ef40 | 962 | ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask, |
81d39c20 | 963 | noswap, false); |
4d1c6273 DN |
964 | if (ret) |
965 | continue; | |
66e1707b BS |
966 | |
967 | /* | |
8869b8f6 HD |
968 | * try_to_free_mem_cgroup_pages() might not give us a full |
969 | * picture of reclaim. Some pages are reclaimed and might be | |
970 | * moved to swap cache or just unmapped from the cgroup. | |
971 | * Check the limit again to see if the reclaim reduced the | |
972 | * current usage of the cgroup before giving up | |
8c7c6e34 | 973 | * |
8869b8f6 | 974 | */ |
b85a96c0 DN |
975 | if (mem_cgroup_check_under_limit(mem_over_limit)) |
976 | continue; | |
3be91277 HD |
977 | |
978 | if (!nr_retries--) { | |
a636b327 | 979 | if (oom) { |
7f4d454d | 980 | mutex_lock(&memcg_tasklist); |
88700756 | 981 | mem_cgroup_out_of_memory(mem_over_limit, gfp_mask); |
7f4d454d | 982 | mutex_unlock(&memcg_tasklist); |
0b7f569e | 983 | record_last_oom(mem_over_limit); |
a636b327 | 984 | } |
7a81b88c | 985 | goto nomem; |
66e1707b | 986 | } |
8a9f3ccd | 987 | } |
7a81b88c KH |
988 | return 0; |
989 | nomem: | |
990 | css_put(&mem->css); | |
991 | return -ENOMEM; | |
992 | } | |
8a9f3ccd | 993 | |
b5a84319 KH |
994 | static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page) |
995 | { | |
996 | struct mem_cgroup *mem; | |
3c776e64 | 997 | struct page_cgroup *pc; |
b5a84319 KH |
998 | swp_entry_t ent; |
999 | ||
3c776e64 DN |
1000 | VM_BUG_ON(!PageLocked(page)); |
1001 | ||
b5a84319 KH |
1002 | if (!PageSwapCache(page)) |
1003 | return NULL; | |
1004 | ||
3c776e64 DN |
1005 | pc = lookup_page_cgroup(page); |
1006 | /* | |
1007 | * Used bit of swapcache is solid under page lock. | |
1008 | */ | |
1009 | if (PageCgroupUsed(pc)) | |
1010 | mem = pc->mem_cgroup; | |
1011 | else { | |
1012 | ent.val = page_private(page); | |
1013 | mem = lookup_swap_cgroup(ent); | |
1014 | } | |
b5a84319 KH |
1015 | if (!mem) |
1016 | return NULL; | |
1017 | if (!css_tryget(&mem->css)) | |
1018 | return NULL; | |
1019 | return mem; | |
1020 | } | |
1021 | ||
7a81b88c | 1022 | /* |
a5e924f5 | 1023 | * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be |
7a81b88c KH |
1024 | * USED state. If already USED, uncharge and return. |
1025 | */ | |
1026 | ||
1027 | static void __mem_cgroup_commit_charge(struct mem_cgroup *mem, | |
1028 | struct page_cgroup *pc, | |
1029 | enum charge_type ctype) | |
1030 | { | |
7a81b88c KH |
1031 | /* try_charge() can return NULL to *memcg, taking care of it. */ |
1032 | if (!mem) | |
1033 | return; | |
52d4b9ac KH |
1034 | |
1035 | lock_page_cgroup(pc); | |
1036 | if (unlikely(PageCgroupUsed(pc))) { | |
1037 | unlock_page_cgroup(pc); | |
1038 | res_counter_uncharge(&mem->res, PAGE_SIZE); | |
8c7c6e34 KH |
1039 | if (do_swap_account) |
1040 | res_counter_uncharge(&mem->memsw, PAGE_SIZE); | |
52d4b9ac | 1041 | css_put(&mem->css); |
7a81b88c | 1042 | return; |
52d4b9ac | 1043 | } |
8a9f3ccd | 1044 | pc->mem_cgroup = mem; |
08e552c6 | 1045 | smp_wmb(); |
c05555b5 | 1046 | pc->flags = pcg_default_flags[ctype]; |
3be91277 | 1047 | |
08e552c6 | 1048 | mem_cgroup_charge_statistics(mem, pc, true); |
52d4b9ac | 1049 | |
52d4b9ac | 1050 | unlock_page_cgroup(pc); |
7a81b88c | 1051 | } |
66e1707b | 1052 | |
f817ed48 KH |
1053 | /** |
1054 | * mem_cgroup_move_account - move account of the page | |
1055 | * @pc: page_cgroup of the page. | |
1056 | * @from: mem_cgroup which the page is moved from. | |
1057 | * @to: mem_cgroup which the page is moved to. @from != @to. | |
1058 | * | |
1059 | * The caller must confirm following. | |
08e552c6 | 1060 | * - page is not on LRU (isolate_page() is useful.) |
f817ed48 KH |
1061 | * |
1062 | * returns 0 at success, | |
1063 | * returns -EBUSY when lock is busy or "pc" is unstable. | |
1064 | * | |
1065 | * This function does "uncharge" from old cgroup but doesn't do "charge" to | |
1066 | * new cgroup. It should be done by a caller. | |
1067 | */ | |
1068 | ||
1069 | static int mem_cgroup_move_account(struct page_cgroup *pc, | |
1070 | struct mem_cgroup *from, struct mem_cgroup *to) | |
1071 | { | |
1072 | struct mem_cgroup_per_zone *from_mz, *to_mz; | |
1073 | int nid, zid; | |
1074 | int ret = -EBUSY; | |
1075 | ||
f817ed48 | 1076 | VM_BUG_ON(from == to); |
08e552c6 | 1077 | VM_BUG_ON(PageLRU(pc->page)); |
f817ed48 KH |
1078 | |
1079 | nid = page_cgroup_nid(pc); | |
1080 | zid = page_cgroup_zid(pc); | |
1081 | from_mz = mem_cgroup_zoneinfo(from, nid, zid); | |
1082 | to_mz = mem_cgroup_zoneinfo(to, nid, zid); | |
1083 | ||
f817ed48 KH |
1084 | if (!trylock_page_cgroup(pc)) |
1085 | return ret; | |
1086 | ||
1087 | if (!PageCgroupUsed(pc)) | |
1088 | goto out; | |
1089 | ||
1090 | if (pc->mem_cgroup != from) | |
1091 | goto out; | |
1092 | ||
08e552c6 KH |
1093 | res_counter_uncharge(&from->res, PAGE_SIZE); |
1094 | mem_cgroup_charge_statistics(from, pc, false); | |
1095 | if (do_swap_account) | |
1096 | res_counter_uncharge(&from->memsw, PAGE_SIZE); | |
40d58138 DN |
1097 | css_put(&from->css); |
1098 | ||
1099 | css_get(&to->css); | |
08e552c6 KH |
1100 | pc->mem_cgroup = to; |
1101 | mem_cgroup_charge_statistics(to, pc, true); | |
08e552c6 | 1102 | ret = 0; |
f817ed48 KH |
1103 | out: |
1104 | unlock_page_cgroup(pc); | |
1105 | return ret; | |
1106 | } | |
1107 | ||
1108 | /* | |
1109 | * move charges to its parent. | |
1110 | */ | |
1111 | ||
1112 | static int mem_cgroup_move_parent(struct page_cgroup *pc, | |
1113 | struct mem_cgroup *child, | |
1114 | gfp_t gfp_mask) | |
1115 | { | |
08e552c6 | 1116 | struct page *page = pc->page; |
f817ed48 KH |
1117 | struct cgroup *cg = child->css.cgroup; |
1118 | struct cgroup *pcg = cg->parent; | |
1119 | struct mem_cgroup *parent; | |
f817ed48 KH |
1120 | int ret; |
1121 | ||
1122 | /* Is ROOT ? */ | |
1123 | if (!pcg) | |
1124 | return -EINVAL; | |
1125 | ||
08e552c6 | 1126 | |
f817ed48 KH |
1127 | parent = mem_cgroup_from_cont(pcg); |
1128 | ||
08e552c6 | 1129 | |
f817ed48 | 1130 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false); |
a636b327 | 1131 | if (ret || !parent) |
f817ed48 KH |
1132 | return ret; |
1133 | ||
40d58138 DN |
1134 | if (!get_page_unless_zero(page)) { |
1135 | ret = -EBUSY; | |
1136 | goto uncharge; | |
1137 | } | |
08e552c6 KH |
1138 | |
1139 | ret = isolate_lru_page(page); | |
1140 | ||
1141 | if (ret) | |
1142 | goto cancel; | |
f817ed48 | 1143 | |
f817ed48 | 1144 | ret = mem_cgroup_move_account(pc, child, parent); |
f817ed48 | 1145 | |
08e552c6 KH |
1146 | putback_lru_page(page); |
1147 | if (!ret) { | |
1148 | put_page(page); | |
40d58138 DN |
1149 | /* drop extra refcnt by try_charge() */ |
1150 | css_put(&parent->css); | |
08e552c6 | 1151 | return 0; |
8c7c6e34 | 1152 | } |
40d58138 | 1153 | |
08e552c6 | 1154 | cancel: |
40d58138 DN |
1155 | put_page(page); |
1156 | uncharge: | |
1157 | /* drop extra refcnt by try_charge() */ | |
1158 | css_put(&parent->css); | |
1159 | /* uncharge if move fails */ | |
08e552c6 KH |
1160 | res_counter_uncharge(&parent->res, PAGE_SIZE); |
1161 | if (do_swap_account) | |
1162 | res_counter_uncharge(&parent->memsw, PAGE_SIZE); | |
f817ed48 KH |
1163 | return ret; |
1164 | } | |
1165 | ||
7a81b88c KH |
1166 | /* |
1167 | * Charge the memory controller for page usage. | |
1168 | * Return | |
1169 | * 0 if the charge was successful | |
1170 | * < 0 if the cgroup is over its limit | |
1171 | */ | |
1172 | static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm, | |
1173 | gfp_t gfp_mask, enum charge_type ctype, | |
1174 | struct mem_cgroup *memcg) | |
1175 | { | |
1176 | struct mem_cgroup *mem; | |
1177 | struct page_cgroup *pc; | |
1178 | int ret; | |
1179 | ||
1180 | pc = lookup_page_cgroup(page); | |
1181 | /* can happen at boot */ | |
1182 | if (unlikely(!pc)) | |
1183 | return 0; | |
1184 | prefetchw(pc); | |
1185 | ||
1186 | mem = memcg; | |
f817ed48 | 1187 | ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true); |
a636b327 | 1188 | if (ret || !mem) |
7a81b88c KH |
1189 | return ret; |
1190 | ||
1191 | __mem_cgroup_commit_charge(mem, pc, ctype); | |
8a9f3ccd | 1192 | return 0; |
8a9f3ccd BS |
1193 | } |
1194 | ||
7a81b88c KH |
1195 | int mem_cgroup_newpage_charge(struct page *page, |
1196 | struct mm_struct *mm, gfp_t gfp_mask) | |
217bc319 | 1197 | { |
f8d66542 | 1198 | if (mem_cgroup_disabled()) |
cede86ac | 1199 | return 0; |
52d4b9ac KH |
1200 | if (PageCompound(page)) |
1201 | return 0; | |
69029cd5 KH |
1202 | /* |
1203 | * If already mapped, we don't have to account. | |
1204 | * If page cache, page->mapping has address_space. | |
1205 | * But page->mapping may have out-of-use anon_vma pointer, | |
1206 | * detecit it by PageAnon() check. newly-mapped-anon's page->mapping | |
1207 | * is NULL. | |
1208 | */ | |
1209 | if (page_mapped(page) || (page->mapping && !PageAnon(page))) | |
1210 | return 0; | |
1211 | if (unlikely(!mm)) | |
1212 | mm = &init_mm; | |
217bc319 | 1213 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
e8589cc1 | 1214 | MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL); |
217bc319 KH |
1215 | } |
1216 | ||
e1a1cd59 BS |
1217 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
1218 | gfp_t gfp_mask) | |
8697d331 | 1219 | { |
b5a84319 KH |
1220 | struct mem_cgroup *mem = NULL; |
1221 | int ret; | |
1222 | ||
f8d66542 | 1223 | if (mem_cgroup_disabled()) |
cede86ac | 1224 | return 0; |
52d4b9ac KH |
1225 | if (PageCompound(page)) |
1226 | return 0; | |
accf163e KH |
1227 | /* |
1228 | * Corner case handling. This is called from add_to_page_cache() | |
1229 | * in usual. But some FS (shmem) precharges this page before calling it | |
1230 | * and call add_to_page_cache() with GFP_NOWAIT. | |
1231 | * | |
1232 | * For GFP_NOWAIT case, the page may be pre-charged before calling | |
1233 | * add_to_page_cache(). (See shmem.c) check it here and avoid to call | |
1234 | * charge twice. (It works but has to pay a bit larger cost.) | |
b5a84319 KH |
1235 | * And when the page is SwapCache, it should take swap information |
1236 | * into account. This is under lock_page() now. | |
accf163e KH |
1237 | */ |
1238 | if (!(gfp_mask & __GFP_WAIT)) { | |
1239 | struct page_cgroup *pc; | |
1240 | ||
52d4b9ac KH |
1241 | |
1242 | pc = lookup_page_cgroup(page); | |
1243 | if (!pc) | |
1244 | return 0; | |
1245 | lock_page_cgroup(pc); | |
1246 | if (PageCgroupUsed(pc)) { | |
1247 | unlock_page_cgroup(pc); | |
accf163e KH |
1248 | return 0; |
1249 | } | |
52d4b9ac | 1250 | unlock_page_cgroup(pc); |
accf163e KH |
1251 | } |
1252 | ||
b5a84319 KH |
1253 | if (do_swap_account && PageSwapCache(page)) { |
1254 | mem = try_get_mem_cgroup_from_swapcache(page); | |
1255 | if (mem) | |
1256 | mm = NULL; | |
1257 | else | |
1258 | mem = NULL; | |
1259 | /* SwapCache may be still linked to LRU now. */ | |
1260 | mem_cgroup_lru_del_before_commit_swapcache(page); | |
1261 | } | |
1262 | ||
1263 | if (unlikely(!mm && !mem)) | |
8697d331 | 1264 | mm = &init_mm; |
accf163e | 1265 | |
c05555b5 KH |
1266 | if (page_is_file_cache(page)) |
1267 | return mem_cgroup_charge_common(page, mm, gfp_mask, | |
e8589cc1 | 1268 | MEM_CGROUP_CHARGE_TYPE_CACHE, NULL); |
b5a84319 KH |
1269 | |
1270 | ret = mem_cgroup_charge_common(page, mm, gfp_mask, | |
1271 | MEM_CGROUP_CHARGE_TYPE_SHMEM, mem); | |
1272 | if (mem) | |
1273 | css_put(&mem->css); | |
1274 | if (PageSwapCache(page)) | |
1275 | mem_cgroup_lru_add_after_commit_swapcache(page); | |
1276 | ||
1277 | if (do_swap_account && !ret && PageSwapCache(page)) { | |
1278 | swp_entry_t ent = {.val = page_private(page)}; | |
1279 | /* avoid double counting */ | |
1280 | mem = swap_cgroup_record(ent, NULL); | |
1281 | if (mem) { | |
1282 | res_counter_uncharge(&mem->memsw, PAGE_SIZE); | |
1283 | mem_cgroup_put(mem); | |
1284 | } | |
1285 | } | |
1286 | return ret; | |
e8589cc1 KH |
1287 | } |
1288 | ||
54595fe2 KH |
1289 | /* |
1290 | * While swap-in, try_charge -> commit or cancel, the page is locked. | |
1291 | * And when try_charge() successfully returns, one refcnt to memcg without | |
1292 | * struct page_cgroup is aquired. This refcnt will be cumsumed by | |
1293 | * "commit()" or removed by "cancel()" | |
1294 | */ | |
8c7c6e34 KH |
1295 | int mem_cgroup_try_charge_swapin(struct mm_struct *mm, |
1296 | struct page *page, | |
1297 | gfp_t mask, struct mem_cgroup **ptr) | |
1298 | { | |
1299 | struct mem_cgroup *mem; | |
54595fe2 | 1300 | int ret; |
8c7c6e34 | 1301 | |
f8d66542 | 1302 | if (mem_cgroup_disabled()) |
8c7c6e34 KH |
1303 | return 0; |
1304 | ||
1305 | if (!do_swap_account) | |
1306 | goto charge_cur_mm; | |
8c7c6e34 KH |
1307 | /* |
1308 | * A racing thread's fault, or swapoff, may have already updated | |
1309 | * the pte, and even removed page from swap cache: return success | |
1310 | * to go on to do_swap_page()'s pte_same() test, which should fail. | |
1311 | */ | |
1312 | if (!PageSwapCache(page)) | |
1313 | return 0; | |
b5a84319 | 1314 | mem = try_get_mem_cgroup_from_swapcache(page); |
54595fe2 KH |
1315 | if (!mem) |
1316 | goto charge_cur_mm; | |
8c7c6e34 | 1317 | *ptr = mem; |
54595fe2 KH |
1318 | ret = __mem_cgroup_try_charge(NULL, mask, ptr, true); |
1319 | /* drop extra refcnt from tryget */ | |
1320 | css_put(&mem->css); | |
1321 | return ret; | |
8c7c6e34 KH |
1322 | charge_cur_mm: |
1323 | if (unlikely(!mm)) | |
1324 | mm = &init_mm; | |
1325 | return __mem_cgroup_try_charge(mm, mask, ptr, true); | |
1326 | } | |
1327 | ||
7a81b88c KH |
1328 | void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr) |
1329 | { | |
1330 | struct page_cgroup *pc; | |
1331 | ||
f8d66542 | 1332 | if (mem_cgroup_disabled()) |
7a81b88c KH |
1333 | return; |
1334 | if (!ptr) | |
1335 | return; | |
1336 | pc = lookup_page_cgroup(page); | |
544122e5 | 1337 | mem_cgroup_lru_del_before_commit_swapcache(page); |
7a81b88c | 1338 | __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
544122e5 | 1339 | mem_cgroup_lru_add_after_commit_swapcache(page); |
8c7c6e34 KH |
1340 | /* |
1341 | * Now swap is on-memory. This means this page may be | |
1342 | * counted both as mem and swap....double count. | |
03f3c433 KH |
1343 | * Fix it by uncharging from memsw. Basically, this SwapCache is stable |
1344 | * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page() | |
1345 | * may call delete_from_swap_cache() before reach here. | |
8c7c6e34 | 1346 | */ |
03f3c433 | 1347 | if (do_swap_account && PageSwapCache(page)) { |
8c7c6e34 KH |
1348 | swp_entry_t ent = {.val = page_private(page)}; |
1349 | struct mem_cgroup *memcg; | |
1350 | memcg = swap_cgroup_record(ent, NULL); | |
1351 | if (memcg) { | |
8c7c6e34 KH |
1352 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); |
1353 | mem_cgroup_put(memcg); | |
1354 | } | |
1355 | ||
1356 | } | |
08e552c6 | 1357 | /* add this page(page_cgroup) to the LRU we want. */ |
544122e5 | 1358 | |
7a81b88c KH |
1359 | } |
1360 | ||
1361 | void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem) | |
1362 | { | |
f8d66542 | 1363 | if (mem_cgroup_disabled()) |
7a81b88c KH |
1364 | return; |
1365 | if (!mem) | |
1366 | return; | |
1367 | res_counter_uncharge(&mem->res, PAGE_SIZE); | |
8c7c6e34 KH |
1368 | if (do_swap_account) |
1369 | res_counter_uncharge(&mem->memsw, PAGE_SIZE); | |
7a81b88c KH |
1370 | css_put(&mem->css); |
1371 | } | |
1372 | ||
1373 | ||
8a9f3ccd | 1374 | /* |
69029cd5 | 1375 | * uncharge if !page_mapped(page) |
8a9f3ccd | 1376 | */ |
8c7c6e34 | 1377 | static struct mem_cgroup * |
69029cd5 | 1378 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype) |
8a9f3ccd | 1379 | { |
8289546e | 1380 | struct page_cgroup *pc; |
8c7c6e34 | 1381 | struct mem_cgroup *mem = NULL; |
072c56c1 | 1382 | struct mem_cgroup_per_zone *mz; |
8a9f3ccd | 1383 | |
f8d66542 | 1384 | if (mem_cgroup_disabled()) |
8c7c6e34 | 1385 | return NULL; |
4077960e | 1386 | |
d13d1443 | 1387 | if (PageSwapCache(page)) |
8c7c6e34 | 1388 | return NULL; |
d13d1443 | 1389 | |
8697d331 | 1390 | /* |
3c541e14 | 1391 | * Check if our page_cgroup is valid |
8697d331 | 1392 | */ |
52d4b9ac KH |
1393 | pc = lookup_page_cgroup(page); |
1394 | if (unlikely(!pc || !PageCgroupUsed(pc))) | |
8c7c6e34 | 1395 | return NULL; |
b9c565d5 | 1396 | |
52d4b9ac | 1397 | lock_page_cgroup(pc); |
d13d1443 | 1398 | |
8c7c6e34 KH |
1399 | mem = pc->mem_cgroup; |
1400 | ||
d13d1443 KH |
1401 | if (!PageCgroupUsed(pc)) |
1402 | goto unlock_out; | |
1403 | ||
1404 | switch (ctype) { | |
1405 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: | |
1406 | if (page_mapped(page)) | |
1407 | goto unlock_out; | |
1408 | break; | |
1409 | case MEM_CGROUP_CHARGE_TYPE_SWAPOUT: | |
1410 | if (!PageAnon(page)) { /* Shared memory */ | |
1411 | if (page->mapping && !page_is_file_cache(page)) | |
1412 | goto unlock_out; | |
1413 | } else if (page_mapped(page)) /* Anon */ | |
1414 | goto unlock_out; | |
1415 | break; | |
1416 | default: | |
1417 | break; | |
52d4b9ac | 1418 | } |
d13d1443 | 1419 | |
8c7c6e34 KH |
1420 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
1421 | if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)) | |
1422 | res_counter_uncharge(&mem->memsw, PAGE_SIZE); | |
08e552c6 | 1423 | mem_cgroup_charge_statistics(mem, pc, false); |
04046e1a | 1424 | |
52d4b9ac | 1425 | ClearPageCgroupUsed(pc); |
544122e5 KH |
1426 | /* |
1427 | * pc->mem_cgroup is not cleared here. It will be accessed when it's | |
1428 | * freed from LRU. This is safe because uncharged page is expected not | |
1429 | * to be reused (freed soon). Exception is SwapCache, it's handled by | |
1430 | * special functions. | |
1431 | */ | |
b9c565d5 | 1432 | |
69029cd5 | 1433 | mz = page_cgroup_zoneinfo(pc); |
52d4b9ac | 1434 | unlock_page_cgroup(pc); |
fb59e9f1 | 1435 | |
a7fe942e KH |
1436 | /* at swapout, this memcg will be accessed to record to swap */ |
1437 | if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT) | |
1438 | css_put(&mem->css); | |
6d12e2d8 | 1439 | |
8c7c6e34 | 1440 | return mem; |
d13d1443 KH |
1441 | |
1442 | unlock_out: | |
1443 | unlock_page_cgroup(pc); | |
8c7c6e34 | 1444 | return NULL; |
3c541e14 BS |
1445 | } |
1446 | ||
69029cd5 KH |
1447 | void mem_cgroup_uncharge_page(struct page *page) |
1448 | { | |
52d4b9ac KH |
1449 | /* early check. */ |
1450 | if (page_mapped(page)) | |
1451 | return; | |
1452 | if (page->mapping && !PageAnon(page)) | |
1453 | return; | |
69029cd5 KH |
1454 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
1455 | } | |
1456 | ||
1457 | void mem_cgroup_uncharge_cache_page(struct page *page) | |
1458 | { | |
1459 | VM_BUG_ON(page_mapped(page)); | |
b7abea96 | 1460 | VM_BUG_ON(page->mapping); |
69029cd5 KH |
1461 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE); |
1462 | } | |
1463 | ||
8c7c6e34 KH |
1464 | /* |
1465 | * called from __delete_from_swap_cache() and drop "page" account. | |
1466 | * memcg information is recorded to swap_cgroup of "ent" | |
1467 | */ | |
1468 | void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent) | |
1469 | { | |
1470 | struct mem_cgroup *memcg; | |
1471 | ||
1472 | memcg = __mem_cgroup_uncharge_common(page, | |
1473 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT); | |
1474 | /* record memcg information */ | |
1475 | if (do_swap_account && memcg) { | |
1476 | swap_cgroup_record(ent, memcg); | |
1477 | mem_cgroup_get(memcg); | |
1478 | } | |
a7fe942e KH |
1479 | if (memcg) |
1480 | css_put(&memcg->css); | |
8c7c6e34 KH |
1481 | } |
1482 | ||
1483 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP | |
1484 | /* | |
1485 | * called from swap_entry_free(). remove record in swap_cgroup and | |
1486 | * uncharge "memsw" account. | |
1487 | */ | |
1488 | void mem_cgroup_uncharge_swap(swp_entry_t ent) | |
d13d1443 | 1489 | { |
8c7c6e34 KH |
1490 | struct mem_cgroup *memcg; |
1491 | ||
1492 | if (!do_swap_account) | |
1493 | return; | |
1494 | ||
1495 | memcg = swap_cgroup_record(ent, NULL); | |
1496 | if (memcg) { | |
1497 | res_counter_uncharge(&memcg->memsw, PAGE_SIZE); | |
1498 | mem_cgroup_put(memcg); | |
1499 | } | |
d13d1443 | 1500 | } |
8c7c6e34 | 1501 | #endif |
d13d1443 | 1502 | |
ae41be37 | 1503 | /* |
01b1ae63 KH |
1504 | * Before starting migration, account PAGE_SIZE to mem_cgroup that the old |
1505 | * page belongs to. | |
ae41be37 | 1506 | */ |
01b1ae63 | 1507 | int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr) |
ae41be37 KH |
1508 | { |
1509 | struct page_cgroup *pc; | |
e8589cc1 | 1510 | struct mem_cgroup *mem = NULL; |
e8589cc1 | 1511 | int ret = 0; |
8869b8f6 | 1512 | |
f8d66542 | 1513 | if (mem_cgroup_disabled()) |
4077960e BS |
1514 | return 0; |
1515 | ||
52d4b9ac KH |
1516 | pc = lookup_page_cgroup(page); |
1517 | lock_page_cgroup(pc); | |
1518 | if (PageCgroupUsed(pc)) { | |
e8589cc1 KH |
1519 | mem = pc->mem_cgroup; |
1520 | css_get(&mem->css); | |
e8589cc1 | 1521 | } |
52d4b9ac | 1522 | unlock_page_cgroup(pc); |
01b1ae63 | 1523 | |
e8589cc1 | 1524 | if (mem) { |
3bb4edf2 | 1525 | ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false); |
e8589cc1 KH |
1526 | css_put(&mem->css); |
1527 | } | |
01b1ae63 | 1528 | *ptr = mem; |
e8589cc1 | 1529 | return ret; |
ae41be37 | 1530 | } |
8869b8f6 | 1531 | |
69029cd5 | 1532 | /* remove redundant charge if migration failed*/ |
01b1ae63 KH |
1533 | void mem_cgroup_end_migration(struct mem_cgroup *mem, |
1534 | struct page *oldpage, struct page *newpage) | |
ae41be37 | 1535 | { |
01b1ae63 KH |
1536 | struct page *target, *unused; |
1537 | struct page_cgroup *pc; | |
1538 | enum charge_type ctype; | |
1539 | ||
1540 | if (!mem) | |
1541 | return; | |
1542 | ||
1543 | /* at migration success, oldpage->mapping is NULL. */ | |
1544 | if (oldpage->mapping) { | |
1545 | target = oldpage; | |
1546 | unused = NULL; | |
1547 | } else { | |
1548 | target = newpage; | |
1549 | unused = oldpage; | |
1550 | } | |
1551 | ||
1552 | if (PageAnon(target)) | |
1553 | ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED; | |
1554 | else if (page_is_file_cache(target)) | |
1555 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; | |
1556 | else | |
1557 | ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM; | |
1558 | ||
1559 | /* unused page is not on radix-tree now. */ | |
d13d1443 | 1560 | if (unused) |
01b1ae63 KH |
1561 | __mem_cgroup_uncharge_common(unused, ctype); |
1562 | ||
1563 | pc = lookup_page_cgroup(target); | |
69029cd5 | 1564 | /* |
01b1ae63 KH |
1565 | * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup. |
1566 | * So, double-counting is effectively avoided. | |
1567 | */ | |
1568 | __mem_cgroup_commit_charge(mem, pc, ctype); | |
1569 | ||
1570 | /* | |
1571 | * Both of oldpage and newpage are still under lock_page(). | |
1572 | * Then, we don't have to care about race in radix-tree. | |
1573 | * But we have to be careful that this page is unmapped or not. | |
1574 | * | |
1575 | * There is a case for !page_mapped(). At the start of | |
1576 | * migration, oldpage was mapped. But now, it's zapped. | |
1577 | * But we know *target* page is not freed/reused under us. | |
1578 | * mem_cgroup_uncharge_page() does all necessary checks. | |
69029cd5 | 1579 | */ |
01b1ae63 KH |
1580 | if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED) |
1581 | mem_cgroup_uncharge_page(target); | |
ae41be37 | 1582 | } |
78fb7466 | 1583 | |
c9b0ed51 KH |
1584 | /* |
1585 | * A call to try to shrink memory usage under specified resource controller. | |
1586 | * This is typically used for page reclaiming for shmem for reducing side | |
1587 | * effect of page allocation from shmem, which is used by some mem_cgroup. | |
1588 | */ | |
b5a84319 KH |
1589 | int mem_cgroup_shrink_usage(struct page *page, |
1590 | struct mm_struct *mm, | |
1591 | gfp_t gfp_mask) | |
c9b0ed51 | 1592 | { |
b5a84319 | 1593 | struct mem_cgroup *mem = NULL; |
c9b0ed51 KH |
1594 | int progress = 0; |
1595 | int retry = MEM_CGROUP_RECLAIM_RETRIES; | |
1596 | ||
f8d66542 | 1597 | if (mem_cgroup_disabled()) |
cede86ac | 1598 | return 0; |
b5a84319 KH |
1599 | if (page) |
1600 | mem = try_get_mem_cgroup_from_swapcache(page); | |
1601 | if (!mem && mm) | |
1602 | mem = try_get_mem_cgroup_from_mm(mm); | |
54595fe2 | 1603 | if (unlikely(!mem)) |
31a78f23 | 1604 | return 0; |
c9b0ed51 KH |
1605 | |
1606 | do { | |
81d39c20 KH |
1607 | progress = mem_cgroup_hierarchical_reclaim(mem, |
1608 | gfp_mask, true, false); | |
b85a96c0 | 1609 | progress += mem_cgroup_check_under_limit(mem); |
c9b0ed51 KH |
1610 | } while (!progress && --retry); |
1611 | ||
1612 | css_put(&mem->css); | |
1613 | if (!retry) | |
1614 | return -ENOMEM; | |
1615 | return 0; | |
1616 | } | |
1617 | ||
8c7c6e34 KH |
1618 | static DEFINE_MUTEX(set_limit_mutex); |
1619 | ||
d38d2a75 | 1620 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
8c7c6e34 | 1621 | unsigned long long val) |
628f4235 | 1622 | { |
81d39c20 | 1623 | int retry_count; |
628f4235 | 1624 | int progress; |
8c7c6e34 | 1625 | u64 memswlimit; |
628f4235 | 1626 | int ret = 0; |
81d39c20 KH |
1627 | int children = mem_cgroup_count_children(memcg); |
1628 | u64 curusage, oldusage; | |
1629 | ||
1630 | /* | |
1631 | * For keeping hierarchical_reclaim simple, how long we should retry | |
1632 | * is depends on callers. We set our retry-count to be function | |
1633 | * of # of children which we should visit in this loop. | |
1634 | */ | |
1635 | retry_count = MEM_CGROUP_RECLAIM_RETRIES * children; | |
1636 | ||
1637 | oldusage = res_counter_read_u64(&memcg->res, RES_USAGE); | |
628f4235 | 1638 | |
8c7c6e34 | 1639 | while (retry_count) { |
628f4235 KH |
1640 | if (signal_pending(current)) { |
1641 | ret = -EINTR; | |
1642 | break; | |
1643 | } | |
8c7c6e34 KH |
1644 | /* |
1645 | * Rather than hide all in some function, I do this in | |
1646 | * open coded manner. You see what this really does. | |
1647 | * We have to guarantee mem->res.limit < mem->memsw.limit. | |
1648 | */ | |
1649 | mutex_lock(&set_limit_mutex); | |
1650 | memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); | |
1651 | if (memswlimit < val) { | |
1652 | ret = -EINVAL; | |
1653 | mutex_unlock(&set_limit_mutex); | |
628f4235 KH |
1654 | break; |
1655 | } | |
8c7c6e34 KH |
1656 | ret = res_counter_set_limit(&memcg->res, val); |
1657 | mutex_unlock(&set_limit_mutex); | |
1658 | ||
1659 | if (!ret) | |
1660 | break; | |
1661 | ||
42e9abb6 | 1662 | progress = mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, |
81d39c20 KH |
1663 | false, true); |
1664 | curusage = res_counter_read_u64(&memcg->res, RES_USAGE); | |
1665 | /* Usage is reduced ? */ | |
1666 | if (curusage >= oldusage) | |
1667 | retry_count--; | |
1668 | else | |
1669 | oldusage = curusage; | |
8c7c6e34 | 1670 | } |
14797e23 | 1671 | |
8c7c6e34 KH |
1672 | return ret; |
1673 | } | |
1674 | ||
1675 | int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg, | |
1676 | unsigned long long val) | |
1677 | { | |
81d39c20 | 1678 | int retry_count; |
8c7c6e34 | 1679 | u64 memlimit, oldusage, curusage; |
81d39c20 KH |
1680 | int children = mem_cgroup_count_children(memcg); |
1681 | int ret = -EBUSY; | |
8c7c6e34 KH |
1682 | |
1683 | if (!do_swap_account) | |
1684 | return -EINVAL; | |
81d39c20 KH |
1685 | /* see mem_cgroup_resize_res_limit */ |
1686 | retry_count = children * MEM_CGROUP_RECLAIM_RETRIES; | |
1687 | oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); | |
8c7c6e34 KH |
1688 | while (retry_count) { |
1689 | if (signal_pending(current)) { | |
1690 | ret = -EINTR; | |
1691 | break; | |
1692 | } | |
1693 | /* | |
1694 | * Rather than hide all in some function, I do this in | |
1695 | * open coded manner. You see what this really does. | |
1696 | * We have to guarantee mem->res.limit < mem->memsw.limit. | |
1697 | */ | |
1698 | mutex_lock(&set_limit_mutex); | |
1699 | memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT); | |
1700 | if (memlimit > val) { | |
1701 | ret = -EINVAL; | |
1702 | mutex_unlock(&set_limit_mutex); | |
1703 | break; | |
1704 | } | |
1705 | ret = res_counter_set_limit(&memcg->memsw, val); | |
1706 | mutex_unlock(&set_limit_mutex); | |
1707 | ||
1708 | if (!ret) | |
1709 | break; | |
1710 | ||
81d39c20 | 1711 | mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, true, true); |
8c7c6e34 | 1712 | curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE); |
81d39c20 | 1713 | /* Usage is reduced ? */ |
8c7c6e34 | 1714 | if (curusage >= oldusage) |
628f4235 | 1715 | retry_count--; |
81d39c20 KH |
1716 | else |
1717 | oldusage = curusage; | |
628f4235 KH |
1718 | } |
1719 | return ret; | |
1720 | } | |
1721 | ||
cc847582 KH |
1722 | /* |
1723 | * This routine traverse page_cgroup in given list and drop them all. | |
cc847582 KH |
1724 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
1725 | */ | |
f817ed48 | 1726 | static int mem_cgroup_force_empty_list(struct mem_cgroup *mem, |
08e552c6 | 1727 | int node, int zid, enum lru_list lru) |
cc847582 | 1728 | { |
08e552c6 KH |
1729 | struct zone *zone; |
1730 | struct mem_cgroup_per_zone *mz; | |
f817ed48 | 1731 | struct page_cgroup *pc, *busy; |
08e552c6 | 1732 | unsigned long flags, loop; |
072c56c1 | 1733 | struct list_head *list; |
f817ed48 | 1734 | int ret = 0; |
072c56c1 | 1735 | |
08e552c6 KH |
1736 | zone = &NODE_DATA(node)->node_zones[zid]; |
1737 | mz = mem_cgroup_zoneinfo(mem, node, zid); | |
b69408e8 | 1738 | list = &mz->lists[lru]; |
cc847582 | 1739 | |
f817ed48 KH |
1740 | loop = MEM_CGROUP_ZSTAT(mz, lru); |
1741 | /* give some margin against EBUSY etc...*/ | |
1742 | loop += 256; | |
1743 | busy = NULL; | |
1744 | while (loop--) { | |
1745 | ret = 0; | |
08e552c6 | 1746 | spin_lock_irqsave(&zone->lru_lock, flags); |
f817ed48 | 1747 | if (list_empty(list)) { |
08e552c6 | 1748 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
52d4b9ac | 1749 | break; |
f817ed48 KH |
1750 | } |
1751 | pc = list_entry(list->prev, struct page_cgroup, lru); | |
1752 | if (busy == pc) { | |
1753 | list_move(&pc->lru, list); | |
1754 | busy = 0; | |
08e552c6 | 1755 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
f817ed48 KH |
1756 | continue; |
1757 | } | |
08e552c6 | 1758 | spin_unlock_irqrestore(&zone->lru_lock, flags); |
f817ed48 | 1759 | |
2c26fdd7 | 1760 | ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL); |
f817ed48 | 1761 | if (ret == -ENOMEM) |
52d4b9ac | 1762 | break; |
f817ed48 KH |
1763 | |
1764 | if (ret == -EBUSY || ret == -EINVAL) { | |
1765 | /* found lock contention or "pc" is obsolete. */ | |
1766 | busy = pc; | |
1767 | cond_resched(); | |
1768 | } else | |
1769 | busy = NULL; | |
cc847582 | 1770 | } |
08e552c6 | 1771 | |
f817ed48 KH |
1772 | if (!ret && !list_empty(list)) |
1773 | return -EBUSY; | |
1774 | return ret; | |
cc847582 KH |
1775 | } |
1776 | ||
1777 | /* | |
1778 | * make mem_cgroup's charge to be 0 if there is no task. | |
1779 | * This enables deleting this mem_cgroup. | |
1780 | */ | |
c1e862c1 | 1781 | static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all) |
cc847582 | 1782 | { |
f817ed48 KH |
1783 | int ret; |
1784 | int node, zid, shrink; | |
1785 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; | |
c1e862c1 | 1786 | struct cgroup *cgrp = mem->css.cgroup; |
8869b8f6 | 1787 | |
cc847582 | 1788 | css_get(&mem->css); |
f817ed48 KH |
1789 | |
1790 | shrink = 0; | |
c1e862c1 KH |
1791 | /* should free all ? */ |
1792 | if (free_all) | |
1793 | goto try_to_free; | |
f817ed48 | 1794 | move_account: |
1ecaab2b | 1795 | while (mem->res.usage > 0) { |
f817ed48 | 1796 | ret = -EBUSY; |
c1e862c1 KH |
1797 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children)) |
1798 | goto out; | |
1799 | ret = -EINTR; | |
1800 | if (signal_pending(current)) | |
cc847582 | 1801 | goto out; |
52d4b9ac KH |
1802 | /* This is for making all *used* pages to be on LRU. */ |
1803 | lru_add_drain_all(); | |
f817ed48 | 1804 | ret = 0; |
299b4eaa | 1805 | for_each_node_state(node, N_HIGH_MEMORY) { |
f817ed48 | 1806 | for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) { |
b69408e8 | 1807 | enum lru_list l; |
f817ed48 KH |
1808 | for_each_lru(l) { |
1809 | ret = mem_cgroup_force_empty_list(mem, | |
08e552c6 | 1810 | node, zid, l); |
f817ed48 KH |
1811 | if (ret) |
1812 | break; | |
1813 | } | |
1ecaab2b | 1814 | } |
f817ed48 KH |
1815 | if (ret) |
1816 | break; | |
1817 | } | |
1818 | /* it seems parent cgroup doesn't have enough mem */ | |
1819 | if (ret == -ENOMEM) | |
1820 | goto try_to_free; | |
52d4b9ac | 1821 | cond_resched(); |
cc847582 KH |
1822 | } |
1823 | ret = 0; | |
1824 | out: | |
1825 | css_put(&mem->css); | |
1826 | return ret; | |
f817ed48 KH |
1827 | |
1828 | try_to_free: | |
c1e862c1 KH |
1829 | /* returns EBUSY if there is a task or if we come here twice. */ |
1830 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) { | |
f817ed48 KH |
1831 | ret = -EBUSY; |
1832 | goto out; | |
1833 | } | |
c1e862c1 KH |
1834 | /* we call try-to-free pages for make this cgroup empty */ |
1835 | lru_add_drain_all(); | |
f817ed48 KH |
1836 | /* try to free all pages in this cgroup */ |
1837 | shrink = 1; | |
1838 | while (nr_retries && mem->res.usage > 0) { | |
1839 | int progress; | |
c1e862c1 KH |
1840 | |
1841 | if (signal_pending(current)) { | |
1842 | ret = -EINTR; | |
1843 | goto out; | |
1844 | } | |
a7885eb8 KM |
1845 | progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL, |
1846 | false, get_swappiness(mem)); | |
c1e862c1 | 1847 | if (!progress) { |
f817ed48 | 1848 | nr_retries--; |
c1e862c1 KH |
1849 | /* maybe some writeback is necessary */ |
1850 | congestion_wait(WRITE, HZ/10); | |
1851 | } | |
f817ed48 KH |
1852 | |
1853 | } | |
08e552c6 | 1854 | lru_add_drain(); |
f817ed48 KH |
1855 | /* try move_account...there may be some *locked* pages. */ |
1856 | if (mem->res.usage) | |
1857 | goto move_account; | |
1858 | ret = 0; | |
1859 | goto out; | |
cc847582 KH |
1860 | } |
1861 | ||
c1e862c1 KH |
1862 | int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event) |
1863 | { | |
1864 | return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true); | |
1865 | } | |
1866 | ||
1867 | ||
18f59ea7 BS |
1868 | static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft) |
1869 | { | |
1870 | return mem_cgroup_from_cont(cont)->use_hierarchy; | |
1871 | } | |
1872 | ||
1873 | static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft, | |
1874 | u64 val) | |
1875 | { | |
1876 | int retval = 0; | |
1877 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); | |
1878 | struct cgroup *parent = cont->parent; | |
1879 | struct mem_cgroup *parent_mem = NULL; | |
1880 | ||
1881 | if (parent) | |
1882 | parent_mem = mem_cgroup_from_cont(parent); | |
1883 | ||
1884 | cgroup_lock(); | |
1885 | /* | |
1886 | * If parent's use_hiearchy is set, we can't make any modifications | |
1887 | * in the child subtrees. If it is unset, then the change can | |
1888 | * occur, provided the current cgroup has no children. | |
1889 | * | |
1890 | * For the root cgroup, parent_mem is NULL, we allow value to be | |
1891 | * set if there are no children. | |
1892 | */ | |
1893 | if ((!parent_mem || !parent_mem->use_hierarchy) && | |
1894 | (val == 1 || val == 0)) { | |
1895 | if (list_empty(&cont->children)) | |
1896 | mem->use_hierarchy = val; | |
1897 | else | |
1898 | retval = -EBUSY; | |
1899 | } else | |
1900 | retval = -EINVAL; | |
1901 | cgroup_unlock(); | |
1902 | ||
1903 | return retval; | |
1904 | } | |
1905 | ||
2c3daa72 | 1906 | static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) |
8cdea7c0 | 1907 | { |
8c7c6e34 KH |
1908 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
1909 | u64 val = 0; | |
1910 | int type, name; | |
1911 | ||
1912 | type = MEMFILE_TYPE(cft->private); | |
1913 | name = MEMFILE_ATTR(cft->private); | |
1914 | switch (type) { | |
1915 | case _MEM: | |
1916 | val = res_counter_read_u64(&mem->res, name); | |
1917 | break; | |
1918 | case _MEMSWAP: | |
1919 | if (do_swap_account) | |
1920 | val = res_counter_read_u64(&mem->memsw, name); | |
1921 | break; | |
1922 | default: | |
1923 | BUG(); | |
1924 | break; | |
1925 | } | |
1926 | return val; | |
8cdea7c0 | 1927 | } |
628f4235 KH |
1928 | /* |
1929 | * The user of this function is... | |
1930 | * RES_LIMIT. | |
1931 | */ | |
856c13aa PM |
1932 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
1933 | const char *buffer) | |
8cdea7c0 | 1934 | { |
628f4235 | 1935 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
8c7c6e34 | 1936 | int type, name; |
628f4235 KH |
1937 | unsigned long long val; |
1938 | int ret; | |
1939 | ||
8c7c6e34 KH |
1940 | type = MEMFILE_TYPE(cft->private); |
1941 | name = MEMFILE_ATTR(cft->private); | |
1942 | switch (name) { | |
628f4235 KH |
1943 | case RES_LIMIT: |
1944 | /* This function does all necessary parse...reuse it */ | |
1945 | ret = res_counter_memparse_write_strategy(buffer, &val); | |
8c7c6e34 KH |
1946 | if (ret) |
1947 | break; | |
1948 | if (type == _MEM) | |
628f4235 | 1949 | ret = mem_cgroup_resize_limit(memcg, val); |
8c7c6e34 KH |
1950 | else |
1951 | ret = mem_cgroup_resize_memsw_limit(memcg, val); | |
628f4235 KH |
1952 | break; |
1953 | default: | |
1954 | ret = -EINVAL; /* should be BUG() ? */ | |
1955 | break; | |
1956 | } | |
1957 | return ret; | |
8cdea7c0 BS |
1958 | } |
1959 | ||
fee7b548 KH |
1960 | static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg, |
1961 | unsigned long long *mem_limit, unsigned long long *memsw_limit) | |
1962 | { | |
1963 | struct cgroup *cgroup; | |
1964 | unsigned long long min_limit, min_memsw_limit, tmp; | |
1965 | ||
1966 | min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT); | |
1967 | min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT); | |
1968 | cgroup = memcg->css.cgroup; | |
1969 | if (!memcg->use_hierarchy) | |
1970 | goto out; | |
1971 | ||
1972 | while (cgroup->parent) { | |
1973 | cgroup = cgroup->parent; | |
1974 | memcg = mem_cgroup_from_cont(cgroup); | |
1975 | if (!memcg->use_hierarchy) | |
1976 | break; | |
1977 | tmp = res_counter_read_u64(&memcg->res, RES_LIMIT); | |
1978 | min_limit = min(min_limit, tmp); | |
1979 | tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT); | |
1980 | min_memsw_limit = min(min_memsw_limit, tmp); | |
1981 | } | |
1982 | out: | |
1983 | *mem_limit = min_limit; | |
1984 | *memsw_limit = min_memsw_limit; | |
1985 | return; | |
1986 | } | |
1987 | ||
29f2a4da | 1988 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
c84872e1 PE |
1989 | { |
1990 | struct mem_cgroup *mem; | |
8c7c6e34 | 1991 | int type, name; |
c84872e1 PE |
1992 | |
1993 | mem = mem_cgroup_from_cont(cont); | |
8c7c6e34 KH |
1994 | type = MEMFILE_TYPE(event); |
1995 | name = MEMFILE_ATTR(event); | |
1996 | switch (name) { | |
29f2a4da | 1997 | case RES_MAX_USAGE: |
8c7c6e34 KH |
1998 | if (type == _MEM) |
1999 | res_counter_reset_max(&mem->res); | |
2000 | else | |
2001 | res_counter_reset_max(&mem->memsw); | |
29f2a4da PE |
2002 | break; |
2003 | case RES_FAILCNT: | |
8c7c6e34 KH |
2004 | if (type == _MEM) |
2005 | res_counter_reset_failcnt(&mem->res); | |
2006 | else | |
2007 | res_counter_reset_failcnt(&mem->memsw); | |
29f2a4da PE |
2008 | break; |
2009 | } | |
85cc59db | 2010 | return 0; |
c84872e1 PE |
2011 | } |
2012 | ||
14067bb3 KH |
2013 | |
2014 | /* For read statistics */ | |
2015 | enum { | |
2016 | MCS_CACHE, | |
2017 | MCS_RSS, | |
2018 | MCS_PGPGIN, | |
2019 | MCS_PGPGOUT, | |
2020 | MCS_INACTIVE_ANON, | |
2021 | MCS_ACTIVE_ANON, | |
2022 | MCS_INACTIVE_FILE, | |
2023 | MCS_ACTIVE_FILE, | |
2024 | MCS_UNEVICTABLE, | |
2025 | NR_MCS_STAT, | |
2026 | }; | |
2027 | ||
2028 | struct mcs_total_stat { | |
2029 | s64 stat[NR_MCS_STAT]; | |
d2ceb9b7 KH |
2030 | }; |
2031 | ||
14067bb3 KH |
2032 | struct { |
2033 | char *local_name; | |
2034 | char *total_name; | |
2035 | } memcg_stat_strings[NR_MCS_STAT] = { | |
2036 | {"cache", "total_cache"}, | |
2037 | {"rss", "total_rss"}, | |
2038 | {"pgpgin", "total_pgpgin"}, | |
2039 | {"pgpgout", "total_pgpgout"}, | |
2040 | {"inactive_anon", "total_inactive_anon"}, | |
2041 | {"active_anon", "total_active_anon"}, | |
2042 | {"inactive_file", "total_inactive_file"}, | |
2043 | {"active_file", "total_active_file"}, | |
2044 | {"unevictable", "total_unevictable"} | |
2045 | }; | |
2046 | ||
2047 | ||
2048 | static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data) | |
2049 | { | |
2050 | struct mcs_total_stat *s = data; | |
2051 | s64 val; | |
2052 | ||
2053 | /* per cpu stat */ | |
2054 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE); | |
2055 | s->stat[MCS_CACHE] += val * PAGE_SIZE; | |
2056 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS); | |
2057 | s->stat[MCS_RSS] += val * PAGE_SIZE; | |
2058 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT); | |
2059 | s->stat[MCS_PGPGIN] += val; | |
2060 | val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT); | |
2061 | s->stat[MCS_PGPGOUT] += val; | |
2062 | ||
2063 | /* per zone stat */ | |
2064 | val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON); | |
2065 | s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE; | |
2066 | val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON); | |
2067 | s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE; | |
2068 | val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE); | |
2069 | s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE; | |
2070 | val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE); | |
2071 | s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE; | |
2072 | val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE); | |
2073 | s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE; | |
2074 | return 0; | |
2075 | } | |
2076 | ||
2077 | static void | |
2078 | mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s) | |
2079 | { | |
2080 | mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat); | |
2081 | } | |
2082 | ||
c64745cf PM |
2083 | static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft, |
2084 | struct cgroup_map_cb *cb) | |
d2ceb9b7 | 2085 | { |
d2ceb9b7 | 2086 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
14067bb3 | 2087 | struct mcs_total_stat mystat; |
d2ceb9b7 KH |
2088 | int i; |
2089 | ||
14067bb3 KH |
2090 | memset(&mystat, 0, sizeof(mystat)); |
2091 | mem_cgroup_get_local_stat(mem_cont, &mystat); | |
d2ceb9b7 | 2092 | |
14067bb3 KH |
2093 | for (i = 0; i < NR_MCS_STAT; i++) |
2094 | cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]); | |
7b854121 | 2095 | |
14067bb3 | 2096 | /* Hierarchical information */ |
fee7b548 KH |
2097 | { |
2098 | unsigned long long limit, memsw_limit; | |
2099 | memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit); | |
2100 | cb->fill(cb, "hierarchical_memory_limit", limit); | |
2101 | if (do_swap_account) | |
2102 | cb->fill(cb, "hierarchical_memsw_limit", memsw_limit); | |
2103 | } | |
7f016ee8 | 2104 | |
14067bb3 KH |
2105 | memset(&mystat, 0, sizeof(mystat)); |
2106 | mem_cgroup_get_total_stat(mem_cont, &mystat); | |
2107 | for (i = 0; i < NR_MCS_STAT; i++) | |
2108 | cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]); | |
2109 | ||
2110 | ||
7f016ee8 | 2111 | #ifdef CONFIG_DEBUG_VM |
c772be93 | 2112 | cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL)); |
7f016ee8 KM |
2113 | |
2114 | { | |
2115 | int nid, zid; | |
2116 | struct mem_cgroup_per_zone *mz; | |
2117 | unsigned long recent_rotated[2] = {0, 0}; | |
2118 | unsigned long recent_scanned[2] = {0, 0}; | |
2119 | ||
2120 | for_each_online_node(nid) | |
2121 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { | |
2122 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); | |
2123 | ||
2124 | recent_rotated[0] += | |
2125 | mz->reclaim_stat.recent_rotated[0]; | |
2126 | recent_rotated[1] += | |
2127 | mz->reclaim_stat.recent_rotated[1]; | |
2128 | recent_scanned[0] += | |
2129 | mz->reclaim_stat.recent_scanned[0]; | |
2130 | recent_scanned[1] += | |
2131 | mz->reclaim_stat.recent_scanned[1]; | |
2132 | } | |
2133 | cb->fill(cb, "recent_rotated_anon", recent_rotated[0]); | |
2134 | cb->fill(cb, "recent_rotated_file", recent_rotated[1]); | |
2135 | cb->fill(cb, "recent_scanned_anon", recent_scanned[0]); | |
2136 | cb->fill(cb, "recent_scanned_file", recent_scanned[1]); | |
2137 | } | |
2138 | #endif | |
2139 | ||
d2ceb9b7 KH |
2140 | return 0; |
2141 | } | |
2142 | ||
a7885eb8 KM |
2143 | static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft) |
2144 | { | |
2145 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); | |
2146 | ||
2147 | return get_swappiness(memcg); | |
2148 | } | |
2149 | ||
2150 | static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft, | |
2151 | u64 val) | |
2152 | { | |
2153 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp); | |
2154 | struct mem_cgroup *parent; | |
068b38c1 | 2155 | |
a7885eb8 KM |
2156 | if (val > 100) |
2157 | return -EINVAL; | |
2158 | ||
2159 | if (cgrp->parent == NULL) | |
2160 | return -EINVAL; | |
2161 | ||
2162 | parent = mem_cgroup_from_cont(cgrp->parent); | |
068b38c1 LZ |
2163 | |
2164 | cgroup_lock(); | |
2165 | ||
a7885eb8 KM |
2166 | /* If under hierarchy, only empty-root can set this value */ |
2167 | if ((parent->use_hierarchy) || | |
068b38c1 LZ |
2168 | (memcg->use_hierarchy && !list_empty(&cgrp->children))) { |
2169 | cgroup_unlock(); | |
a7885eb8 | 2170 | return -EINVAL; |
068b38c1 | 2171 | } |
a7885eb8 KM |
2172 | |
2173 | spin_lock(&memcg->reclaim_param_lock); | |
2174 | memcg->swappiness = val; | |
2175 | spin_unlock(&memcg->reclaim_param_lock); | |
2176 | ||
068b38c1 LZ |
2177 | cgroup_unlock(); |
2178 | ||
a7885eb8 KM |
2179 | return 0; |
2180 | } | |
2181 | ||
c1e862c1 | 2182 | |
8cdea7c0 BS |
2183 | static struct cftype mem_cgroup_files[] = { |
2184 | { | |
0eea1030 | 2185 | .name = "usage_in_bytes", |
8c7c6e34 | 2186 | .private = MEMFILE_PRIVATE(_MEM, RES_USAGE), |
2c3daa72 | 2187 | .read_u64 = mem_cgroup_read, |
8cdea7c0 | 2188 | }, |
c84872e1 PE |
2189 | { |
2190 | .name = "max_usage_in_bytes", | |
8c7c6e34 | 2191 | .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE), |
29f2a4da | 2192 | .trigger = mem_cgroup_reset, |
c84872e1 PE |
2193 | .read_u64 = mem_cgroup_read, |
2194 | }, | |
8cdea7c0 | 2195 | { |
0eea1030 | 2196 | .name = "limit_in_bytes", |
8c7c6e34 | 2197 | .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT), |
856c13aa | 2198 | .write_string = mem_cgroup_write, |
2c3daa72 | 2199 | .read_u64 = mem_cgroup_read, |
8cdea7c0 BS |
2200 | }, |
2201 | { | |
2202 | .name = "failcnt", | |
8c7c6e34 | 2203 | .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT), |
29f2a4da | 2204 | .trigger = mem_cgroup_reset, |
2c3daa72 | 2205 | .read_u64 = mem_cgroup_read, |
8cdea7c0 | 2206 | }, |
d2ceb9b7 KH |
2207 | { |
2208 | .name = "stat", | |
c64745cf | 2209 | .read_map = mem_control_stat_show, |
d2ceb9b7 | 2210 | }, |
c1e862c1 KH |
2211 | { |
2212 | .name = "force_empty", | |
2213 | .trigger = mem_cgroup_force_empty_write, | |
2214 | }, | |
18f59ea7 BS |
2215 | { |
2216 | .name = "use_hierarchy", | |
2217 | .write_u64 = mem_cgroup_hierarchy_write, | |
2218 | .read_u64 = mem_cgroup_hierarchy_read, | |
2219 | }, | |
a7885eb8 KM |
2220 | { |
2221 | .name = "swappiness", | |
2222 | .read_u64 = mem_cgroup_swappiness_read, | |
2223 | .write_u64 = mem_cgroup_swappiness_write, | |
2224 | }, | |
8cdea7c0 BS |
2225 | }; |
2226 | ||
8c7c6e34 KH |
2227 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
2228 | static struct cftype memsw_cgroup_files[] = { | |
2229 | { | |
2230 | .name = "memsw.usage_in_bytes", | |
2231 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE), | |
2232 | .read_u64 = mem_cgroup_read, | |
2233 | }, | |
2234 | { | |
2235 | .name = "memsw.max_usage_in_bytes", | |
2236 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE), | |
2237 | .trigger = mem_cgroup_reset, | |
2238 | .read_u64 = mem_cgroup_read, | |
2239 | }, | |
2240 | { | |
2241 | .name = "memsw.limit_in_bytes", | |
2242 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT), | |
2243 | .write_string = mem_cgroup_write, | |
2244 | .read_u64 = mem_cgroup_read, | |
2245 | }, | |
2246 | { | |
2247 | .name = "memsw.failcnt", | |
2248 | .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT), | |
2249 | .trigger = mem_cgroup_reset, | |
2250 | .read_u64 = mem_cgroup_read, | |
2251 | }, | |
2252 | }; | |
2253 | ||
2254 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) | |
2255 | { | |
2256 | if (!do_swap_account) | |
2257 | return 0; | |
2258 | return cgroup_add_files(cont, ss, memsw_cgroup_files, | |
2259 | ARRAY_SIZE(memsw_cgroup_files)); | |
2260 | }; | |
2261 | #else | |
2262 | static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss) | |
2263 | { | |
2264 | return 0; | |
2265 | } | |
2266 | #endif | |
2267 | ||
6d12e2d8 KH |
2268 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
2269 | { | |
2270 | struct mem_cgroup_per_node *pn; | |
1ecaab2b | 2271 | struct mem_cgroup_per_zone *mz; |
b69408e8 | 2272 | enum lru_list l; |
41e3355d | 2273 | int zone, tmp = node; |
1ecaab2b KH |
2274 | /* |
2275 | * This routine is called against possible nodes. | |
2276 | * But it's BUG to call kmalloc() against offline node. | |
2277 | * | |
2278 | * TODO: this routine can waste much memory for nodes which will | |
2279 | * never be onlined. It's better to use memory hotplug callback | |
2280 | * function. | |
2281 | */ | |
41e3355d KH |
2282 | if (!node_state(node, N_NORMAL_MEMORY)) |
2283 | tmp = -1; | |
2284 | pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp); | |
6d12e2d8 KH |
2285 | if (!pn) |
2286 | return 1; | |
1ecaab2b | 2287 | |
6d12e2d8 KH |
2288 | mem->info.nodeinfo[node] = pn; |
2289 | memset(pn, 0, sizeof(*pn)); | |
1ecaab2b KH |
2290 | |
2291 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { | |
2292 | mz = &pn->zoneinfo[zone]; | |
b69408e8 CL |
2293 | for_each_lru(l) |
2294 | INIT_LIST_HEAD(&mz->lists[l]); | |
1ecaab2b | 2295 | } |
6d12e2d8 KH |
2296 | return 0; |
2297 | } | |
2298 | ||
1ecaab2b KH |
2299 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
2300 | { | |
2301 | kfree(mem->info.nodeinfo[node]); | |
2302 | } | |
2303 | ||
c8dad2bb JB |
2304 | static int mem_cgroup_size(void) |
2305 | { | |
2306 | int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu); | |
2307 | return sizeof(struct mem_cgroup) + cpustat_size; | |
2308 | } | |
2309 | ||
33327948 KH |
2310 | static struct mem_cgroup *mem_cgroup_alloc(void) |
2311 | { | |
2312 | struct mem_cgroup *mem; | |
c8dad2bb | 2313 | int size = mem_cgroup_size(); |
33327948 | 2314 | |
c8dad2bb JB |
2315 | if (size < PAGE_SIZE) |
2316 | mem = kmalloc(size, GFP_KERNEL); | |
33327948 | 2317 | else |
c8dad2bb | 2318 | mem = vmalloc(size); |
33327948 KH |
2319 | |
2320 | if (mem) | |
c8dad2bb | 2321 | memset(mem, 0, size); |
33327948 KH |
2322 | return mem; |
2323 | } | |
2324 | ||
8c7c6e34 KH |
2325 | /* |
2326 | * At destroying mem_cgroup, references from swap_cgroup can remain. | |
2327 | * (scanning all at force_empty is too costly...) | |
2328 | * | |
2329 | * Instead of clearing all references at force_empty, we remember | |
2330 | * the number of reference from swap_cgroup and free mem_cgroup when | |
2331 | * it goes down to 0. | |
2332 | * | |
8c7c6e34 KH |
2333 | * Removal of cgroup itself succeeds regardless of refs from swap. |
2334 | */ | |
2335 | ||
a7ba0eef | 2336 | static void __mem_cgroup_free(struct mem_cgroup *mem) |
33327948 | 2337 | { |
08e552c6 KH |
2338 | int node; |
2339 | ||
04046e1a KH |
2340 | free_css_id(&mem_cgroup_subsys, &mem->css); |
2341 | ||
08e552c6 KH |
2342 | for_each_node_state(node, N_POSSIBLE) |
2343 | free_mem_cgroup_per_zone_info(mem, node); | |
2344 | ||
c8dad2bb | 2345 | if (mem_cgroup_size() < PAGE_SIZE) |
33327948 KH |
2346 | kfree(mem); |
2347 | else | |
2348 | vfree(mem); | |
2349 | } | |
2350 | ||
8c7c6e34 KH |
2351 | static void mem_cgroup_get(struct mem_cgroup *mem) |
2352 | { | |
2353 | atomic_inc(&mem->refcnt); | |
2354 | } | |
2355 | ||
2356 | static void mem_cgroup_put(struct mem_cgroup *mem) | |
2357 | { | |
7bcc1bb1 DN |
2358 | if (atomic_dec_and_test(&mem->refcnt)) { |
2359 | struct mem_cgroup *parent = parent_mem_cgroup(mem); | |
a7ba0eef | 2360 | __mem_cgroup_free(mem); |
7bcc1bb1 DN |
2361 | if (parent) |
2362 | mem_cgroup_put(parent); | |
2363 | } | |
8c7c6e34 KH |
2364 | } |
2365 | ||
7bcc1bb1 DN |
2366 | /* |
2367 | * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled. | |
2368 | */ | |
2369 | static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem) | |
2370 | { | |
2371 | if (!mem->res.parent) | |
2372 | return NULL; | |
2373 | return mem_cgroup_from_res_counter(mem->res.parent, res); | |
2374 | } | |
33327948 | 2375 | |
c077719b KH |
2376 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
2377 | static void __init enable_swap_cgroup(void) | |
2378 | { | |
f8d66542 | 2379 | if (!mem_cgroup_disabled() && really_do_swap_account) |
c077719b KH |
2380 | do_swap_account = 1; |
2381 | } | |
2382 | #else | |
2383 | static void __init enable_swap_cgroup(void) | |
2384 | { | |
2385 | } | |
2386 | #endif | |
2387 | ||
0eb253e2 | 2388 | static struct cgroup_subsys_state * __ref |
8cdea7c0 BS |
2389 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) |
2390 | { | |
28dbc4b6 | 2391 | struct mem_cgroup *mem, *parent; |
04046e1a | 2392 | long error = -ENOMEM; |
6d12e2d8 | 2393 | int node; |
8cdea7c0 | 2394 | |
c8dad2bb JB |
2395 | mem = mem_cgroup_alloc(); |
2396 | if (!mem) | |
04046e1a | 2397 | return ERR_PTR(error); |
78fb7466 | 2398 | |
6d12e2d8 KH |
2399 | for_each_node_state(node, N_POSSIBLE) |
2400 | if (alloc_mem_cgroup_per_zone_info(mem, node)) | |
2401 | goto free_out; | |
c077719b | 2402 | /* root ? */ |
28dbc4b6 | 2403 | if (cont->parent == NULL) { |
c077719b | 2404 | enable_swap_cgroup(); |
28dbc4b6 | 2405 | parent = NULL; |
18f59ea7 | 2406 | } else { |
28dbc4b6 | 2407 | parent = mem_cgroup_from_cont(cont->parent); |
18f59ea7 BS |
2408 | mem->use_hierarchy = parent->use_hierarchy; |
2409 | } | |
28dbc4b6 | 2410 | |
18f59ea7 BS |
2411 | if (parent && parent->use_hierarchy) { |
2412 | res_counter_init(&mem->res, &parent->res); | |
2413 | res_counter_init(&mem->memsw, &parent->memsw); | |
7bcc1bb1 DN |
2414 | /* |
2415 | * We increment refcnt of the parent to ensure that we can | |
2416 | * safely access it on res_counter_charge/uncharge. | |
2417 | * This refcnt will be decremented when freeing this | |
2418 | * mem_cgroup(see mem_cgroup_put). | |
2419 | */ | |
2420 | mem_cgroup_get(parent); | |
18f59ea7 BS |
2421 | } else { |
2422 | res_counter_init(&mem->res, NULL); | |
2423 | res_counter_init(&mem->memsw, NULL); | |
2424 | } | |
04046e1a | 2425 | mem->last_scanned_child = 0; |
2733c06a | 2426 | spin_lock_init(&mem->reclaim_param_lock); |
6d61ef40 | 2427 | |
a7885eb8 KM |
2428 | if (parent) |
2429 | mem->swappiness = get_swappiness(parent); | |
a7ba0eef | 2430 | atomic_set(&mem->refcnt, 1); |
8cdea7c0 | 2431 | return &mem->css; |
6d12e2d8 | 2432 | free_out: |
a7ba0eef | 2433 | __mem_cgroup_free(mem); |
04046e1a | 2434 | return ERR_PTR(error); |
8cdea7c0 BS |
2435 | } |
2436 | ||
ec64f515 | 2437 | static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
df878fb0 KH |
2438 | struct cgroup *cont) |
2439 | { | |
2440 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); | |
ec64f515 KH |
2441 | |
2442 | return mem_cgroup_force_empty(mem, false); | |
df878fb0 KH |
2443 | } |
2444 | ||
8cdea7c0 BS |
2445 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
2446 | struct cgroup *cont) | |
2447 | { | |
c268e994 | 2448 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); |
c268e994 | 2449 | |
c268e994 | 2450 | mem_cgroup_put(mem); |
8cdea7c0 BS |
2451 | } |
2452 | ||
2453 | static int mem_cgroup_populate(struct cgroup_subsys *ss, | |
2454 | struct cgroup *cont) | |
2455 | { | |
8c7c6e34 KH |
2456 | int ret; |
2457 | ||
2458 | ret = cgroup_add_files(cont, ss, mem_cgroup_files, | |
2459 | ARRAY_SIZE(mem_cgroup_files)); | |
2460 | ||
2461 | if (!ret) | |
2462 | ret = register_memsw_files(cont, ss); | |
2463 | return ret; | |
8cdea7c0 BS |
2464 | } |
2465 | ||
67e465a7 BS |
2466 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
2467 | struct cgroup *cont, | |
2468 | struct cgroup *old_cont, | |
2469 | struct task_struct *p) | |
2470 | { | |
7f4d454d | 2471 | mutex_lock(&memcg_tasklist); |
67e465a7 | 2472 | /* |
f9717d28 NK |
2473 | * FIXME: It's better to move charges of this process from old |
2474 | * memcg to new memcg. But it's just on TODO-List now. | |
67e465a7 | 2475 | */ |
7f4d454d | 2476 | mutex_unlock(&memcg_tasklist); |
67e465a7 BS |
2477 | } |
2478 | ||
8cdea7c0 BS |
2479 | struct cgroup_subsys mem_cgroup_subsys = { |
2480 | .name = "memory", | |
2481 | .subsys_id = mem_cgroup_subsys_id, | |
2482 | .create = mem_cgroup_create, | |
df878fb0 | 2483 | .pre_destroy = mem_cgroup_pre_destroy, |
8cdea7c0 BS |
2484 | .destroy = mem_cgroup_destroy, |
2485 | .populate = mem_cgroup_populate, | |
67e465a7 | 2486 | .attach = mem_cgroup_move_task, |
6d12e2d8 | 2487 | .early_init = 0, |
04046e1a | 2488 | .use_id = 1, |
8cdea7c0 | 2489 | }; |
c077719b KH |
2490 | |
2491 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP | |
2492 | ||
2493 | static int __init disable_swap_account(char *s) | |
2494 | { | |
2495 | really_do_swap_account = 0; | |
2496 | return 1; | |
2497 | } | |
2498 | __setup("noswapaccount", disable_swap_account); | |
2499 | #endif |