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> | |
b6ac57d5 | 30 | #include <linux/slab.h> |
66e1707b BS |
31 | #include <linux/swap.h> |
32 | #include <linux/spinlock.h> | |
33 | #include <linux/fs.h> | |
d2ceb9b7 | 34 | #include <linux/seq_file.h> |
33327948 | 35 | #include <linux/vmalloc.h> |
b69408e8 | 36 | #include <linux/mm_inline.h> |
52d4b9ac | 37 | #include <linux/page_cgroup.h> |
8cdea7c0 | 38 | |
8697d331 BS |
39 | #include <asm/uaccess.h> |
40 | ||
a181b0e8 | 41 | struct cgroup_subsys mem_cgroup_subsys __read_mostly; |
a181b0e8 | 42 | #define MEM_CGROUP_RECLAIM_RETRIES 5 |
8cdea7c0 | 43 | |
c077719b KH |
44 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
45 | /* Turned on only when memory cgroup is enabled && really_do_swap_account = 0 */ | |
46 | int do_swap_account __read_mostly; | |
47 | static int really_do_swap_account __initdata = 1; /* for remember boot option*/ | |
48 | #else | |
49 | #define do_swap_account (0) | |
50 | #endif | |
51 | ||
52 | ||
d52aa412 KH |
53 | /* |
54 | * Statistics for memory cgroup. | |
55 | */ | |
56 | enum mem_cgroup_stat_index { | |
57 | /* | |
58 | * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss. | |
59 | */ | |
60 | MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */ | |
61 | MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */ | |
55e462b0 BR |
62 | MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */ |
63 | MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */ | |
d52aa412 KH |
64 | |
65 | MEM_CGROUP_STAT_NSTATS, | |
66 | }; | |
67 | ||
68 | struct mem_cgroup_stat_cpu { | |
69 | s64 count[MEM_CGROUP_STAT_NSTATS]; | |
70 | } ____cacheline_aligned_in_smp; | |
71 | ||
72 | struct mem_cgroup_stat { | |
c8dad2bb | 73 | struct mem_cgroup_stat_cpu cpustat[0]; |
d52aa412 KH |
74 | }; |
75 | ||
76 | /* | |
77 | * For accounting under irq disable, no need for increment preempt count. | |
78 | */ | |
addb9efe | 79 | static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat, |
d52aa412 KH |
80 | enum mem_cgroup_stat_index idx, int val) |
81 | { | |
addb9efe | 82 | stat->count[idx] += val; |
d52aa412 KH |
83 | } |
84 | ||
85 | static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat, | |
86 | enum mem_cgroup_stat_index idx) | |
87 | { | |
88 | int cpu; | |
89 | s64 ret = 0; | |
90 | for_each_possible_cpu(cpu) | |
91 | ret += stat->cpustat[cpu].count[idx]; | |
92 | return ret; | |
93 | } | |
94 | ||
6d12e2d8 KH |
95 | /* |
96 | * per-zone information in memory controller. | |
97 | */ | |
6d12e2d8 | 98 | struct mem_cgroup_per_zone { |
072c56c1 KH |
99 | /* |
100 | * spin_lock to protect the per cgroup LRU | |
101 | */ | |
102 | spinlock_t lru_lock; | |
b69408e8 CL |
103 | struct list_head lists[NR_LRU_LISTS]; |
104 | unsigned long count[NR_LRU_LISTS]; | |
6d12e2d8 KH |
105 | }; |
106 | /* Macro for accessing counter */ | |
107 | #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)]) | |
108 | ||
109 | struct mem_cgroup_per_node { | |
110 | struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES]; | |
111 | }; | |
112 | ||
113 | struct mem_cgroup_lru_info { | |
114 | struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES]; | |
115 | }; | |
116 | ||
8cdea7c0 BS |
117 | /* |
118 | * The memory controller data structure. The memory controller controls both | |
119 | * page cache and RSS per cgroup. We would eventually like to provide | |
120 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, | |
121 | * to help the administrator determine what knobs to tune. | |
122 | * | |
123 | * TODO: Add a water mark for the memory controller. Reclaim will begin when | |
8a9f3ccd BS |
124 | * we hit the water mark. May be even add a low water mark, such that |
125 | * no reclaim occurs from a cgroup at it's low water mark, this is | |
126 | * a feature that will be implemented much later in the future. | |
8cdea7c0 BS |
127 | */ |
128 | struct mem_cgroup { | |
129 | struct cgroup_subsys_state css; | |
130 | /* | |
131 | * the counter to account for memory usage | |
132 | */ | |
133 | struct res_counter res; | |
78fb7466 PE |
134 | /* |
135 | * Per cgroup active and inactive list, similar to the | |
136 | * per zone LRU lists. | |
78fb7466 | 137 | */ |
6d12e2d8 | 138 | struct mem_cgroup_lru_info info; |
072c56c1 | 139 | |
6c48a1d0 | 140 | int prev_priority; /* for recording reclaim priority */ |
d52aa412 | 141 | /* |
c8dad2bb | 142 | * statistics. This must be placed at the end of memcg. |
d52aa412 KH |
143 | */ |
144 | struct mem_cgroup_stat stat; | |
8cdea7c0 BS |
145 | }; |
146 | ||
217bc319 KH |
147 | enum charge_type { |
148 | MEM_CGROUP_CHARGE_TYPE_CACHE = 0, | |
149 | MEM_CGROUP_CHARGE_TYPE_MAPPED, | |
4f98a2fe | 150 | MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */ |
c05555b5 | 151 | MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */ |
d13d1443 | 152 | MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */ |
c05555b5 KH |
153 | NR_CHARGE_TYPE, |
154 | }; | |
155 | ||
52d4b9ac KH |
156 | /* only for here (for easy reading.) */ |
157 | #define PCGF_CACHE (1UL << PCG_CACHE) | |
158 | #define PCGF_USED (1UL << PCG_USED) | |
159 | #define PCGF_ACTIVE (1UL << PCG_ACTIVE) | |
160 | #define PCGF_LOCK (1UL << PCG_LOCK) | |
161 | #define PCGF_FILE (1UL << PCG_FILE) | |
c05555b5 KH |
162 | static const unsigned long |
163 | pcg_default_flags[NR_CHARGE_TYPE] = { | |
52d4b9ac KH |
164 | PCGF_CACHE | PCGF_FILE | PCGF_USED | PCGF_LOCK, /* File Cache */ |
165 | PCGF_ACTIVE | PCGF_USED | PCGF_LOCK, /* Anon */ | |
166 | PCGF_ACTIVE | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */ | |
167 | 0, /* FORCE */ | |
217bc319 KH |
168 | }; |
169 | ||
d52aa412 KH |
170 | /* |
171 | * Always modified under lru lock. Then, not necessary to preempt_disable() | |
172 | */ | |
c05555b5 KH |
173 | static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, |
174 | struct page_cgroup *pc, | |
175 | bool charge) | |
d52aa412 KH |
176 | { |
177 | int val = (charge)? 1 : -1; | |
178 | struct mem_cgroup_stat *stat = &mem->stat; | |
addb9efe | 179 | struct mem_cgroup_stat_cpu *cpustat; |
d52aa412 | 180 | |
8869b8f6 | 181 | VM_BUG_ON(!irqs_disabled()); |
addb9efe KH |
182 | |
183 | cpustat = &stat->cpustat[smp_processor_id()]; | |
c05555b5 | 184 | if (PageCgroupCache(pc)) |
addb9efe | 185 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val); |
d52aa412 | 186 | else |
addb9efe | 187 | __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val); |
55e462b0 BR |
188 | |
189 | if (charge) | |
addb9efe | 190 | __mem_cgroup_stat_add_safe(cpustat, |
55e462b0 BR |
191 | MEM_CGROUP_STAT_PGPGIN_COUNT, 1); |
192 | else | |
addb9efe | 193 | __mem_cgroup_stat_add_safe(cpustat, |
55e462b0 | 194 | MEM_CGROUP_STAT_PGPGOUT_COUNT, 1); |
6d12e2d8 KH |
195 | } |
196 | ||
d5b69e38 | 197 | static struct mem_cgroup_per_zone * |
6d12e2d8 KH |
198 | mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid) |
199 | { | |
6d12e2d8 KH |
200 | return &mem->info.nodeinfo[nid]->zoneinfo[zid]; |
201 | } | |
202 | ||
d5b69e38 | 203 | static struct mem_cgroup_per_zone * |
6d12e2d8 KH |
204 | page_cgroup_zoneinfo(struct page_cgroup *pc) |
205 | { | |
206 | struct mem_cgroup *mem = pc->mem_cgroup; | |
207 | int nid = page_cgroup_nid(pc); | |
208 | int zid = page_cgroup_zid(pc); | |
d52aa412 | 209 | |
6d12e2d8 KH |
210 | return mem_cgroup_zoneinfo(mem, nid, zid); |
211 | } | |
212 | ||
213 | static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem, | |
b69408e8 | 214 | enum lru_list idx) |
6d12e2d8 KH |
215 | { |
216 | int nid, zid; | |
217 | struct mem_cgroup_per_zone *mz; | |
218 | u64 total = 0; | |
219 | ||
220 | for_each_online_node(nid) | |
221 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { | |
222 | mz = mem_cgroup_zoneinfo(mem, nid, zid); | |
223 | total += MEM_CGROUP_ZSTAT(mz, idx); | |
224 | } | |
225 | return total; | |
d52aa412 KH |
226 | } |
227 | ||
d5b69e38 | 228 | static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) |
8cdea7c0 BS |
229 | { |
230 | return container_of(cgroup_subsys_state(cont, | |
231 | mem_cgroup_subsys_id), struct mem_cgroup, | |
232 | css); | |
233 | } | |
234 | ||
cf475ad2 | 235 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) |
78fb7466 | 236 | { |
31a78f23 BS |
237 | /* |
238 | * mm_update_next_owner() may clear mm->owner to NULL | |
239 | * if it races with swapoff, page migration, etc. | |
240 | * So this can be called with p == NULL. | |
241 | */ | |
242 | if (unlikely(!p)) | |
243 | return NULL; | |
244 | ||
78fb7466 PE |
245 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), |
246 | struct mem_cgroup, css); | |
247 | } | |
248 | ||
3eae90c3 KH |
249 | static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz, |
250 | struct page_cgroup *pc) | |
6d12e2d8 | 251 | { |
4f98a2fe RR |
252 | int lru = LRU_BASE; |
253 | ||
c05555b5 | 254 | if (PageCgroupUnevictable(pc)) |
894bc310 LS |
255 | lru = LRU_UNEVICTABLE; |
256 | else { | |
c05555b5 | 257 | if (PageCgroupActive(pc)) |
894bc310 | 258 | lru += LRU_ACTIVE; |
c05555b5 | 259 | if (PageCgroupFile(pc)) |
894bc310 LS |
260 | lru += LRU_FILE; |
261 | } | |
6d12e2d8 | 262 | |
b69408e8 | 263 | MEM_CGROUP_ZSTAT(mz, lru) -= 1; |
6d12e2d8 | 264 | |
c05555b5 | 265 | mem_cgroup_charge_statistics(pc->mem_cgroup, pc, false); |
508b7be0 | 266 | list_del(&pc->lru); |
6d12e2d8 KH |
267 | } |
268 | ||
3eae90c3 | 269 | static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz, |
f817ed48 | 270 | struct page_cgroup *pc, bool hot) |
6d12e2d8 | 271 | { |
4f98a2fe | 272 | int lru = LRU_BASE; |
b69408e8 | 273 | |
c05555b5 | 274 | if (PageCgroupUnevictable(pc)) |
894bc310 LS |
275 | lru = LRU_UNEVICTABLE; |
276 | else { | |
c05555b5 | 277 | if (PageCgroupActive(pc)) |
894bc310 | 278 | lru += LRU_ACTIVE; |
c05555b5 | 279 | if (PageCgroupFile(pc)) |
894bc310 LS |
280 | lru += LRU_FILE; |
281 | } | |
b69408e8 CL |
282 | |
283 | MEM_CGROUP_ZSTAT(mz, lru) += 1; | |
f817ed48 KH |
284 | if (hot) |
285 | list_add(&pc->lru, &mz->lists[lru]); | |
286 | else | |
287 | list_add_tail(&pc->lru, &mz->lists[lru]); | |
6d12e2d8 | 288 | |
c05555b5 | 289 | mem_cgroup_charge_statistics(pc->mem_cgroup, pc, true); |
6d12e2d8 KH |
290 | } |
291 | ||
894bc310 | 292 | static void __mem_cgroup_move_lists(struct page_cgroup *pc, enum lru_list lru) |
66e1707b | 293 | { |
6d12e2d8 | 294 | struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc); |
c05555b5 KH |
295 | int active = PageCgroupActive(pc); |
296 | int file = PageCgroupFile(pc); | |
297 | int unevictable = PageCgroupUnevictable(pc); | |
894bc310 LS |
298 | enum lru_list from = unevictable ? LRU_UNEVICTABLE : |
299 | (LRU_FILE * !!file + !!active); | |
6d12e2d8 | 300 | |
894bc310 LS |
301 | if (lru == from) |
302 | return; | |
b69408e8 | 303 | |
894bc310 | 304 | MEM_CGROUP_ZSTAT(mz, from) -= 1; |
c05555b5 KH |
305 | /* |
306 | * However this is done under mz->lru_lock, another flags, which | |
307 | * are not related to LRU, will be modified from out-of-lock. | |
308 | * We have to use atomic set/clear flags. | |
309 | */ | |
894bc310 | 310 | if (is_unevictable_lru(lru)) { |
c05555b5 KH |
311 | ClearPageCgroupActive(pc); |
312 | SetPageCgroupUnevictable(pc); | |
894bc310 LS |
313 | } else { |
314 | if (is_active_lru(lru)) | |
c05555b5 | 315 | SetPageCgroupActive(pc); |
894bc310 | 316 | else |
c05555b5 KH |
317 | ClearPageCgroupActive(pc); |
318 | ClearPageCgroupUnevictable(pc); | |
894bc310 | 319 | } |
b69408e8 | 320 | |
b69408e8 CL |
321 | MEM_CGROUP_ZSTAT(mz, lru) += 1; |
322 | list_move(&pc->lru, &mz->lists[lru]); | |
66e1707b BS |
323 | } |
324 | ||
4c4a2214 DR |
325 | int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem) |
326 | { | |
327 | int ret; | |
328 | ||
329 | task_lock(task); | |
bd845e38 | 330 | ret = task->mm && mm_match_cgroup(task->mm, mem); |
4c4a2214 DR |
331 | task_unlock(task); |
332 | return ret; | |
333 | } | |
334 | ||
66e1707b BS |
335 | /* |
336 | * This routine assumes that the appropriate zone's lru lock is already held | |
337 | */ | |
894bc310 | 338 | void mem_cgroup_move_lists(struct page *page, enum lru_list lru) |
66e1707b | 339 | { |
427d5416 | 340 | struct page_cgroup *pc; |
072c56c1 KH |
341 | struct mem_cgroup_per_zone *mz; |
342 | unsigned long flags; | |
343 | ||
cede86ac LZ |
344 | if (mem_cgroup_subsys.disabled) |
345 | return; | |
346 | ||
2680eed7 HD |
347 | /* |
348 | * We cannot lock_page_cgroup while holding zone's lru_lock, | |
349 | * because other holders of lock_page_cgroup can be interrupted | |
350 | * with an attempt to rotate_reclaimable_page. But we cannot | |
351 | * safely get to page_cgroup without it, so just try_lock it: | |
352 | * mem_cgroup_isolate_pages allows for page left on wrong list. | |
353 | */ | |
52d4b9ac KH |
354 | pc = lookup_page_cgroup(page); |
355 | if (!trylock_page_cgroup(pc)) | |
66e1707b | 356 | return; |
52d4b9ac | 357 | if (pc && PageCgroupUsed(pc)) { |
2680eed7 | 358 | mz = page_cgroup_zoneinfo(pc); |
2680eed7 | 359 | spin_lock_irqsave(&mz->lru_lock, flags); |
894bc310 | 360 | __mem_cgroup_move_lists(pc, lru); |
2680eed7 | 361 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
9b3c0a07 | 362 | } |
52d4b9ac | 363 | unlock_page_cgroup(pc); |
66e1707b BS |
364 | } |
365 | ||
58ae83db KH |
366 | /* |
367 | * Calculate mapped_ratio under memory controller. This will be used in | |
368 | * vmscan.c for deteremining we have to reclaim mapped pages. | |
369 | */ | |
370 | int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem) | |
371 | { | |
372 | long total, rss; | |
373 | ||
374 | /* | |
375 | * usage is recorded in bytes. But, here, we assume the number of | |
376 | * physical pages can be represented by "long" on any arch. | |
377 | */ | |
378 | total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L; | |
379 | rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS); | |
380 | return (int)((rss * 100L) / total); | |
381 | } | |
8869b8f6 | 382 | |
6c48a1d0 KH |
383 | /* |
384 | * prev_priority control...this will be used in memory reclaim path. | |
385 | */ | |
386 | int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem) | |
387 | { | |
388 | return mem->prev_priority; | |
389 | } | |
390 | ||
391 | void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority) | |
392 | { | |
393 | if (priority < mem->prev_priority) | |
394 | mem->prev_priority = priority; | |
395 | } | |
396 | ||
397 | void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority) | |
398 | { | |
399 | mem->prev_priority = priority; | |
400 | } | |
401 | ||
cc38108e KH |
402 | /* |
403 | * Calculate # of pages to be scanned in this priority/zone. | |
404 | * See also vmscan.c | |
405 | * | |
406 | * priority starts from "DEF_PRIORITY" and decremented in each loop. | |
407 | * (see include/linux/mmzone.h) | |
408 | */ | |
409 | ||
b69408e8 CL |
410 | long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone, |
411 | int priority, enum lru_list lru) | |
cc38108e | 412 | { |
b69408e8 | 413 | long nr_pages; |
cc38108e KH |
414 | int nid = zone->zone_pgdat->node_id; |
415 | int zid = zone_idx(zone); | |
416 | struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid); | |
417 | ||
b69408e8 | 418 | nr_pages = MEM_CGROUP_ZSTAT(mz, lru); |
cc38108e | 419 | |
b69408e8 | 420 | return (nr_pages >> priority); |
cc38108e KH |
421 | } |
422 | ||
66e1707b BS |
423 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, |
424 | struct list_head *dst, | |
425 | unsigned long *scanned, int order, | |
426 | int mode, struct zone *z, | |
427 | struct mem_cgroup *mem_cont, | |
4f98a2fe | 428 | int active, int file) |
66e1707b BS |
429 | { |
430 | unsigned long nr_taken = 0; | |
431 | struct page *page; | |
432 | unsigned long scan; | |
433 | LIST_HEAD(pc_list); | |
434 | struct list_head *src; | |
ff7283fa | 435 | struct page_cgroup *pc, *tmp; |
1ecaab2b KH |
436 | int nid = z->zone_pgdat->node_id; |
437 | int zid = zone_idx(z); | |
438 | struct mem_cgroup_per_zone *mz; | |
4f98a2fe | 439 | int lru = LRU_FILE * !!file + !!active; |
66e1707b | 440 | |
cf475ad2 | 441 | BUG_ON(!mem_cont); |
1ecaab2b | 442 | mz = mem_cgroup_zoneinfo(mem_cont, nid, zid); |
b69408e8 | 443 | src = &mz->lists[lru]; |
66e1707b | 444 | |
072c56c1 | 445 | spin_lock(&mz->lru_lock); |
ff7283fa KH |
446 | scan = 0; |
447 | list_for_each_entry_safe_reverse(pc, tmp, src, lru) { | |
436c6541 | 448 | if (scan >= nr_to_scan) |
ff7283fa | 449 | break; |
52d4b9ac KH |
450 | if (unlikely(!PageCgroupUsed(pc))) |
451 | continue; | |
66e1707b | 452 | page = pc->page; |
66e1707b | 453 | |
436c6541 | 454 | if (unlikely(!PageLRU(page))) |
ff7283fa | 455 | continue; |
ff7283fa | 456 | |
4f98a2fe RR |
457 | /* |
458 | * TODO: play better with lumpy reclaim, grabbing anything. | |
459 | */ | |
894bc310 LS |
460 | if (PageUnevictable(page) || |
461 | (PageActive(page) && !active) || | |
462 | (!PageActive(page) && active)) { | |
463 | __mem_cgroup_move_lists(pc, page_lru(page)); | |
66e1707b BS |
464 | continue; |
465 | } | |
466 | ||
436c6541 HD |
467 | scan++; |
468 | list_move(&pc->lru, &pc_list); | |
66e1707b | 469 | |
4f98a2fe | 470 | if (__isolate_lru_page(page, mode, file) == 0) { |
66e1707b BS |
471 | list_move(&page->lru, dst); |
472 | nr_taken++; | |
473 | } | |
474 | } | |
475 | ||
476 | list_splice(&pc_list, src); | |
072c56c1 | 477 | spin_unlock(&mz->lru_lock); |
66e1707b BS |
478 | |
479 | *scanned = scan; | |
480 | return nr_taken; | |
481 | } | |
482 | ||
f817ed48 KH |
483 | /* |
484 | * Unlike exported interface, "oom" parameter is added. if oom==true, | |
485 | * oom-killer can be invoked. | |
8a9f3ccd | 486 | */ |
f817ed48 KH |
487 | static int __mem_cgroup_try_charge(struct mm_struct *mm, |
488 | gfp_t gfp_mask, struct mem_cgroup **memcg, bool oom) | |
8a9f3ccd BS |
489 | { |
490 | struct mem_cgroup *mem; | |
7a81b88c | 491 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; |
8a9f3ccd | 492 | /* |
3be91277 HD |
493 | * We always charge the cgroup the mm_struct belongs to. |
494 | * The mm_struct's mem_cgroup changes on task migration if the | |
8a9f3ccd BS |
495 | * thread group leader migrates. It's possible that mm is not |
496 | * set, if so charge the init_mm (happens for pagecache usage). | |
497 | */ | |
7a81b88c | 498 | if (likely(!*memcg)) { |
e8589cc1 KH |
499 | rcu_read_lock(); |
500 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); | |
31a78f23 BS |
501 | if (unlikely(!mem)) { |
502 | rcu_read_unlock(); | |
31a78f23 BS |
503 | return 0; |
504 | } | |
e8589cc1 KH |
505 | /* |
506 | * For every charge from the cgroup, increment reference count | |
507 | */ | |
508 | css_get(&mem->css); | |
7a81b88c | 509 | *memcg = mem; |
e8589cc1 KH |
510 | rcu_read_unlock(); |
511 | } else { | |
7a81b88c KH |
512 | mem = *memcg; |
513 | css_get(&mem->css); | |
e8589cc1 | 514 | } |
8a9f3ccd | 515 | |
7a81b88c | 516 | |
addb9efe | 517 | while (unlikely(res_counter_charge(&mem->res, PAGE_SIZE))) { |
3be91277 | 518 | if (!(gfp_mask & __GFP_WAIT)) |
7a81b88c | 519 | goto nomem; |
e1a1cd59 BS |
520 | |
521 | if (try_to_free_mem_cgroup_pages(mem, gfp_mask)) | |
66e1707b BS |
522 | continue; |
523 | ||
524 | /* | |
8869b8f6 HD |
525 | * try_to_free_mem_cgroup_pages() might not give us a full |
526 | * picture of reclaim. Some pages are reclaimed and might be | |
527 | * moved to swap cache or just unmapped from the cgroup. | |
528 | * Check the limit again to see if the reclaim reduced the | |
529 | * current usage of the cgroup before giving up | |
530 | */ | |
66e1707b BS |
531 | if (res_counter_check_under_limit(&mem->res)) |
532 | continue; | |
3be91277 HD |
533 | |
534 | if (!nr_retries--) { | |
f817ed48 KH |
535 | if (oom) |
536 | mem_cgroup_out_of_memory(mem, gfp_mask); | |
7a81b88c | 537 | goto nomem; |
66e1707b | 538 | } |
8a9f3ccd | 539 | } |
7a81b88c KH |
540 | return 0; |
541 | nomem: | |
542 | css_put(&mem->css); | |
543 | return -ENOMEM; | |
544 | } | |
8a9f3ccd | 545 | |
f817ed48 KH |
546 | /** |
547 | * mem_cgroup_try_charge - get charge of PAGE_SIZE. | |
548 | * @mm: an mm_struct which is charged against. (when *memcg is NULL) | |
549 | * @gfp_mask: gfp_mask for reclaim. | |
550 | * @memcg: a pointer to memory cgroup which is charged against. | |
551 | * | |
552 | * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated | |
553 | * memory cgroup from @mm is got and stored in *memcg. | |
554 | * | |
555 | * Returns 0 if success. -ENOMEM at failure. | |
556 | * This call can invoke OOM-Killer. | |
557 | */ | |
558 | ||
559 | int mem_cgroup_try_charge(struct mm_struct *mm, | |
560 | gfp_t mask, struct mem_cgroup **memcg) | |
561 | { | |
562 | return __mem_cgroup_try_charge(mm, mask, memcg, true); | |
563 | } | |
564 | ||
7a81b88c KH |
565 | /* |
566 | * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be | |
567 | * USED state. If already USED, uncharge and return. | |
568 | */ | |
569 | ||
570 | static void __mem_cgroup_commit_charge(struct mem_cgroup *mem, | |
571 | struct page_cgroup *pc, | |
572 | enum charge_type ctype) | |
573 | { | |
574 | struct mem_cgroup_per_zone *mz; | |
575 | unsigned long flags; | |
576 | ||
577 | /* try_charge() can return NULL to *memcg, taking care of it. */ | |
578 | if (!mem) | |
579 | return; | |
52d4b9ac KH |
580 | |
581 | lock_page_cgroup(pc); | |
582 | if (unlikely(PageCgroupUsed(pc))) { | |
583 | unlock_page_cgroup(pc); | |
584 | res_counter_uncharge(&mem->res, PAGE_SIZE); | |
585 | css_put(&mem->css); | |
7a81b88c | 586 | return; |
52d4b9ac | 587 | } |
8a9f3ccd | 588 | pc->mem_cgroup = mem; |
508b7be0 KH |
589 | /* |
590 | * If a page is accounted as a page cache, insert to inactive list. | |
591 | * If anon, insert to active list. | |
592 | */ | |
c05555b5 | 593 | pc->flags = pcg_default_flags[ctype]; |
3be91277 | 594 | |
072c56c1 | 595 | mz = page_cgroup_zoneinfo(pc); |
52d4b9ac | 596 | |
072c56c1 | 597 | spin_lock_irqsave(&mz->lru_lock, flags); |
f817ed48 | 598 | __mem_cgroup_add_list(mz, pc, true); |
072c56c1 | 599 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
52d4b9ac | 600 | unlock_page_cgroup(pc); |
7a81b88c | 601 | } |
66e1707b | 602 | |
f817ed48 KH |
603 | /** |
604 | * mem_cgroup_move_account - move account of the page | |
605 | * @pc: page_cgroup of the page. | |
606 | * @from: mem_cgroup which the page is moved from. | |
607 | * @to: mem_cgroup which the page is moved to. @from != @to. | |
608 | * | |
609 | * The caller must confirm following. | |
610 | * 1. disable irq. | |
611 | * 2. lru_lock of old mem_cgroup(@from) should be held. | |
612 | * | |
613 | * returns 0 at success, | |
614 | * returns -EBUSY when lock is busy or "pc" is unstable. | |
615 | * | |
616 | * This function does "uncharge" from old cgroup but doesn't do "charge" to | |
617 | * new cgroup. It should be done by a caller. | |
618 | */ | |
619 | ||
620 | static int mem_cgroup_move_account(struct page_cgroup *pc, | |
621 | struct mem_cgroup *from, struct mem_cgroup *to) | |
622 | { | |
623 | struct mem_cgroup_per_zone *from_mz, *to_mz; | |
624 | int nid, zid; | |
625 | int ret = -EBUSY; | |
626 | ||
627 | VM_BUG_ON(!irqs_disabled()); | |
628 | VM_BUG_ON(from == to); | |
629 | ||
630 | nid = page_cgroup_nid(pc); | |
631 | zid = page_cgroup_zid(pc); | |
632 | from_mz = mem_cgroup_zoneinfo(from, nid, zid); | |
633 | to_mz = mem_cgroup_zoneinfo(to, nid, zid); | |
634 | ||
635 | ||
636 | if (!trylock_page_cgroup(pc)) | |
637 | return ret; | |
638 | ||
639 | if (!PageCgroupUsed(pc)) | |
640 | goto out; | |
641 | ||
642 | if (pc->mem_cgroup != from) | |
643 | goto out; | |
644 | ||
645 | if (spin_trylock(&to_mz->lru_lock)) { | |
646 | __mem_cgroup_remove_list(from_mz, pc); | |
647 | css_put(&from->css); | |
648 | res_counter_uncharge(&from->res, PAGE_SIZE); | |
649 | pc->mem_cgroup = to; | |
650 | css_get(&to->css); | |
651 | __mem_cgroup_add_list(to_mz, pc, false); | |
652 | ret = 0; | |
653 | spin_unlock(&to_mz->lru_lock); | |
654 | } | |
655 | out: | |
656 | unlock_page_cgroup(pc); | |
657 | return ret; | |
658 | } | |
659 | ||
660 | /* | |
661 | * move charges to its parent. | |
662 | */ | |
663 | ||
664 | static int mem_cgroup_move_parent(struct page_cgroup *pc, | |
665 | struct mem_cgroup *child, | |
666 | gfp_t gfp_mask) | |
667 | { | |
668 | struct cgroup *cg = child->css.cgroup; | |
669 | struct cgroup *pcg = cg->parent; | |
670 | struct mem_cgroup *parent; | |
671 | struct mem_cgroup_per_zone *mz; | |
672 | unsigned long flags; | |
673 | int ret; | |
674 | ||
675 | /* Is ROOT ? */ | |
676 | if (!pcg) | |
677 | return -EINVAL; | |
678 | ||
679 | parent = mem_cgroup_from_cont(pcg); | |
680 | ||
681 | ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false); | |
682 | if (ret) | |
683 | return ret; | |
684 | ||
685 | mz = mem_cgroup_zoneinfo(child, | |
686 | page_cgroup_nid(pc), page_cgroup_zid(pc)); | |
687 | ||
688 | spin_lock_irqsave(&mz->lru_lock, flags); | |
689 | ret = mem_cgroup_move_account(pc, child, parent); | |
690 | spin_unlock_irqrestore(&mz->lru_lock, flags); | |
691 | ||
692 | /* drop extra refcnt */ | |
693 | css_put(&parent->css); | |
694 | /* uncharge if move fails */ | |
695 | if (ret) | |
696 | res_counter_uncharge(&parent->res, PAGE_SIZE); | |
697 | ||
698 | return ret; | |
699 | } | |
700 | ||
7a81b88c KH |
701 | /* |
702 | * Charge the memory controller for page usage. | |
703 | * Return | |
704 | * 0 if the charge was successful | |
705 | * < 0 if the cgroup is over its limit | |
706 | */ | |
707 | static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm, | |
708 | gfp_t gfp_mask, enum charge_type ctype, | |
709 | struct mem_cgroup *memcg) | |
710 | { | |
711 | struct mem_cgroup *mem; | |
712 | struct page_cgroup *pc; | |
713 | int ret; | |
714 | ||
715 | pc = lookup_page_cgroup(page); | |
716 | /* can happen at boot */ | |
717 | if (unlikely(!pc)) | |
718 | return 0; | |
719 | prefetchw(pc); | |
720 | ||
721 | mem = memcg; | |
f817ed48 | 722 | ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true); |
7a81b88c KH |
723 | if (ret) |
724 | return ret; | |
725 | ||
726 | __mem_cgroup_commit_charge(mem, pc, ctype); | |
8a9f3ccd | 727 | return 0; |
8a9f3ccd BS |
728 | } |
729 | ||
7a81b88c KH |
730 | int mem_cgroup_newpage_charge(struct page *page, |
731 | struct mm_struct *mm, gfp_t gfp_mask) | |
217bc319 | 732 | { |
cede86ac LZ |
733 | if (mem_cgroup_subsys.disabled) |
734 | return 0; | |
52d4b9ac KH |
735 | if (PageCompound(page)) |
736 | return 0; | |
69029cd5 KH |
737 | /* |
738 | * If already mapped, we don't have to account. | |
739 | * If page cache, page->mapping has address_space. | |
740 | * But page->mapping may have out-of-use anon_vma pointer, | |
741 | * detecit it by PageAnon() check. newly-mapped-anon's page->mapping | |
742 | * is NULL. | |
743 | */ | |
744 | if (page_mapped(page) || (page->mapping && !PageAnon(page))) | |
745 | return 0; | |
746 | if (unlikely(!mm)) | |
747 | mm = &init_mm; | |
217bc319 | 748 | return mem_cgroup_charge_common(page, mm, gfp_mask, |
e8589cc1 | 749 | MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL); |
217bc319 KH |
750 | } |
751 | ||
e1a1cd59 BS |
752 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
753 | gfp_t gfp_mask) | |
8697d331 | 754 | { |
cede86ac LZ |
755 | if (mem_cgroup_subsys.disabled) |
756 | return 0; | |
52d4b9ac KH |
757 | if (PageCompound(page)) |
758 | return 0; | |
accf163e KH |
759 | /* |
760 | * Corner case handling. This is called from add_to_page_cache() | |
761 | * in usual. But some FS (shmem) precharges this page before calling it | |
762 | * and call add_to_page_cache() with GFP_NOWAIT. | |
763 | * | |
764 | * For GFP_NOWAIT case, the page may be pre-charged before calling | |
765 | * add_to_page_cache(). (See shmem.c) check it here and avoid to call | |
766 | * charge twice. (It works but has to pay a bit larger cost.) | |
767 | */ | |
768 | if (!(gfp_mask & __GFP_WAIT)) { | |
769 | struct page_cgroup *pc; | |
770 | ||
52d4b9ac KH |
771 | |
772 | pc = lookup_page_cgroup(page); | |
773 | if (!pc) | |
774 | return 0; | |
775 | lock_page_cgroup(pc); | |
776 | if (PageCgroupUsed(pc)) { | |
777 | unlock_page_cgroup(pc); | |
accf163e KH |
778 | return 0; |
779 | } | |
52d4b9ac | 780 | unlock_page_cgroup(pc); |
accf163e KH |
781 | } |
782 | ||
69029cd5 | 783 | if (unlikely(!mm)) |
8697d331 | 784 | mm = &init_mm; |
accf163e | 785 | |
c05555b5 KH |
786 | if (page_is_file_cache(page)) |
787 | return mem_cgroup_charge_common(page, mm, gfp_mask, | |
e8589cc1 | 788 | MEM_CGROUP_CHARGE_TYPE_CACHE, NULL); |
c05555b5 KH |
789 | else |
790 | return mem_cgroup_charge_common(page, mm, gfp_mask, | |
791 | MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL); | |
e8589cc1 KH |
792 | } |
793 | ||
d13d1443 KH |
794 | #ifdef CONFIG_SWAP |
795 | int mem_cgroup_cache_charge_swapin(struct page *page, | |
796 | struct mm_struct *mm, gfp_t mask, bool locked) | |
797 | { | |
798 | int ret = 0; | |
799 | ||
800 | if (mem_cgroup_subsys.disabled) | |
801 | return 0; | |
802 | if (unlikely(!mm)) | |
803 | mm = &init_mm; | |
804 | if (!locked) | |
805 | lock_page(page); | |
806 | /* | |
807 | * If not locked, the page can be dropped from SwapCache until | |
808 | * we reach here. | |
809 | */ | |
810 | if (PageSwapCache(page)) { | |
811 | ret = mem_cgroup_charge_common(page, mm, mask, | |
812 | MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL); | |
813 | } | |
814 | if (!locked) | |
815 | unlock_page(page); | |
816 | ||
817 | return ret; | |
818 | } | |
819 | #endif | |
820 | ||
7a81b88c KH |
821 | void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr) |
822 | { | |
823 | struct page_cgroup *pc; | |
824 | ||
825 | if (mem_cgroup_subsys.disabled) | |
826 | return; | |
827 | if (!ptr) | |
828 | return; | |
829 | pc = lookup_page_cgroup(page); | |
830 | __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED); | |
831 | } | |
832 | ||
833 | void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem) | |
834 | { | |
835 | if (mem_cgroup_subsys.disabled) | |
836 | return; | |
837 | if (!mem) | |
838 | return; | |
839 | res_counter_uncharge(&mem->res, PAGE_SIZE); | |
840 | css_put(&mem->css); | |
841 | } | |
842 | ||
843 | ||
8a9f3ccd | 844 | /* |
69029cd5 | 845 | * uncharge if !page_mapped(page) |
8a9f3ccd | 846 | */ |
69029cd5 KH |
847 | static void |
848 | __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype) | |
8a9f3ccd | 849 | { |
8289546e | 850 | struct page_cgroup *pc; |
8a9f3ccd | 851 | struct mem_cgroup *mem; |
072c56c1 | 852 | struct mem_cgroup_per_zone *mz; |
66e1707b | 853 | unsigned long flags; |
8a9f3ccd | 854 | |
4077960e BS |
855 | if (mem_cgroup_subsys.disabled) |
856 | return; | |
857 | ||
d13d1443 KH |
858 | if (PageSwapCache(page)) |
859 | return; | |
860 | ||
8697d331 | 861 | /* |
3c541e14 | 862 | * Check if our page_cgroup is valid |
8697d331 | 863 | */ |
52d4b9ac KH |
864 | pc = lookup_page_cgroup(page); |
865 | if (unlikely(!pc || !PageCgroupUsed(pc))) | |
866 | return; | |
b9c565d5 | 867 | |
52d4b9ac | 868 | lock_page_cgroup(pc); |
d13d1443 KH |
869 | |
870 | if (!PageCgroupUsed(pc)) | |
871 | goto unlock_out; | |
872 | ||
873 | switch (ctype) { | |
874 | case MEM_CGROUP_CHARGE_TYPE_MAPPED: | |
875 | if (page_mapped(page)) | |
876 | goto unlock_out; | |
877 | break; | |
878 | case MEM_CGROUP_CHARGE_TYPE_SWAPOUT: | |
879 | if (!PageAnon(page)) { /* Shared memory */ | |
880 | if (page->mapping && !page_is_file_cache(page)) | |
881 | goto unlock_out; | |
882 | } else if (page_mapped(page)) /* Anon */ | |
883 | goto unlock_out; | |
884 | break; | |
885 | default: | |
886 | break; | |
52d4b9ac | 887 | } |
d13d1443 | 888 | |
52d4b9ac KH |
889 | ClearPageCgroupUsed(pc); |
890 | mem = pc->mem_cgroup; | |
b9c565d5 | 891 | |
69029cd5 KH |
892 | mz = page_cgroup_zoneinfo(pc); |
893 | spin_lock_irqsave(&mz->lru_lock, flags); | |
894 | __mem_cgroup_remove_list(mz, pc); | |
895 | spin_unlock_irqrestore(&mz->lru_lock, flags); | |
52d4b9ac | 896 | unlock_page_cgroup(pc); |
fb59e9f1 | 897 | |
69029cd5 KH |
898 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
899 | css_put(&mem->css); | |
6d12e2d8 | 900 | |
69029cd5 | 901 | return; |
d13d1443 KH |
902 | |
903 | unlock_out: | |
904 | unlock_page_cgroup(pc); | |
905 | return; | |
3c541e14 BS |
906 | } |
907 | ||
69029cd5 KH |
908 | void mem_cgroup_uncharge_page(struct page *page) |
909 | { | |
52d4b9ac KH |
910 | /* early check. */ |
911 | if (page_mapped(page)) | |
912 | return; | |
913 | if (page->mapping && !PageAnon(page)) | |
914 | return; | |
69029cd5 KH |
915 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED); |
916 | } | |
917 | ||
918 | void mem_cgroup_uncharge_cache_page(struct page *page) | |
919 | { | |
920 | VM_BUG_ON(page_mapped(page)); | |
b7abea96 | 921 | VM_BUG_ON(page->mapping); |
69029cd5 KH |
922 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE); |
923 | } | |
924 | ||
d13d1443 KH |
925 | void mem_cgroup_uncharge_swapcache(struct page *page) |
926 | { | |
927 | __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_SWAPOUT); | |
928 | } | |
929 | ||
ae41be37 | 930 | /* |
01b1ae63 KH |
931 | * Before starting migration, account PAGE_SIZE to mem_cgroup that the old |
932 | * page belongs to. | |
ae41be37 | 933 | */ |
01b1ae63 | 934 | int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr) |
ae41be37 KH |
935 | { |
936 | struct page_cgroup *pc; | |
e8589cc1 | 937 | struct mem_cgroup *mem = NULL; |
e8589cc1 | 938 | int ret = 0; |
8869b8f6 | 939 | |
4077960e BS |
940 | if (mem_cgroup_subsys.disabled) |
941 | return 0; | |
942 | ||
52d4b9ac KH |
943 | pc = lookup_page_cgroup(page); |
944 | lock_page_cgroup(pc); | |
945 | if (PageCgroupUsed(pc)) { | |
e8589cc1 KH |
946 | mem = pc->mem_cgroup; |
947 | css_get(&mem->css); | |
e8589cc1 | 948 | } |
52d4b9ac | 949 | unlock_page_cgroup(pc); |
01b1ae63 | 950 | |
e8589cc1 | 951 | if (mem) { |
01b1ae63 | 952 | ret = mem_cgroup_try_charge(NULL, GFP_HIGHUSER_MOVABLE, &mem); |
e8589cc1 KH |
953 | css_put(&mem->css); |
954 | } | |
01b1ae63 | 955 | *ptr = mem; |
e8589cc1 | 956 | return ret; |
ae41be37 | 957 | } |
8869b8f6 | 958 | |
69029cd5 | 959 | /* remove redundant charge if migration failed*/ |
01b1ae63 KH |
960 | void mem_cgroup_end_migration(struct mem_cgroup *mem, |
961 | struct page *oldpage, struct page *newpage) | |
ae41be37 | 962 | { |
01b1ae63 KH |
963 | struct page *target, *unused; |
964 | struct page_cgroup *pc; | |
965 | enum charge_type ctype; | |
966 | ||
967 | if (!mem) | |
968 | return; | |
969 | ||
970 | /* at migration success, oldpage->mapping is NULL. */ | |
971 | if (oldpage->mapping) { | |
972 | target = oldpage; | |
973 | unused = NULL; | |
974 | } else { | |
975 | target = newpage; | |
976 | unused = oldpage; | |
977 | } | |
978 | ||
979 | if (PageAnon(target)) | |
980 | ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED; | |
981 | else if (page_is_file_cache(target)) | |
982 | ctype = MEM_CGROUP_CHARGE_TYPE_CACHE; | |
983 | else | |
984 | ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM; | |
985 | ||
986 | /* unused page is not on radix-tree now. */ | |
d13d1443 | 987 | if (unused) |
01b1ae63 KH |
988 | __mem_cgroup_uncharge_common(unused, ctype); |
989 | ||
990 | pc = lookup_page_cgroup(target); | |
69029cd5 | 991 | /* |
01b1ae63 KH |
992 | * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup. |
993 | * So, double-counting is effectively avoided. | |
994 | */ | |
995 | __mem_cgroup_commit_charge(mem, pc, ctype); | |
996 | ||
997 | /* | |
998 | * Both of oldpage and newpage are still under lock_page(). | |
999 | * Then, we don't have to care about race in radix-tree. | |
1000 | * But we have to be careful that this page is unmapped or not. | |
1001 | * | |
1002 | * There is a case for !page_mapped(). At the start of | |
1003 | * migration, oldpage was mapped. But now, it's zapped. | |
1004 | * But we know *target* page is not freed/reused under us. | |
1005 | * mem_cgroup_uncharge_page() does all necessary checks. | |
69029cd5 | 1006 | */ |
01b1ae63 KH |
1007 | if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED) |
1008 | mem_cgroup_uncharge_page(target); | |
ae41be37 | 1009 | } |
78fb7466 | 1010 | |
c9b0ed51 KH |
1011 | /* |
1012 | * A call to try to shrink memory usage under specified resource controller. | |
1013 | * This is typically used for page reclaiming for shmem for reducing side | |
1014 | * effect of page allocation from shmem, which is used by some mem_cgroup. | |
1015 | */ | |
1016 | int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask) | |
1017 | { | |
1018 | struct mem_cgroup *mem; | |
1019 | int progress = 0; | |
1020 | int retry = MEM_CGROUP_RECLAIM_RETRIES; | |
1021 | ||
cede86ac LZ |
1022 | if (mem_cgroup_subsys.disabled) |
1023 | return 0; | |
9623e078 HD |
1024 | if (!mm) |
1025 | return 0; | |
cede86ac | 1026 | |
c9b0ed51 KH |
1027 | rcu_read_lock(); |
1028 | mem = mem_cgroup_from_task(rcu_dereference(mm->owner)); | |
31a78f23 BS |
1029 | if (unlikely(!mem)) { |
1030 | rcu_read_unlock(); | |
1031 | return 0; | |
1032 | } | |
c9b0ed51 KH |
1033 | css_get(&mem->css); |
1034 | rcu_read_unlock(); | |
1035 | ||
1036 | do { | |
1037 | progress = try_to_free_mem_cgroup_pages(mem, gfp_mask); | |
a10cebf5 | 1038 | progress += res_counter_check_under_limit(&mem->res); |
c9b0ed51 KH |
1039 | } while (!progress && --retry); |
1040 | ||
1041 | css_put(&mem->css); | |
1042 | if (!retry) | |
1043 | return -ENOMEM; | |
1044 | return 0; | |
1045 | } | |
1046 | ||
d38d2a75 KM |
1047 | static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, |
1048 | unsigned long long val) | |
628f4235 KH |
1049 | { |
1050 | ||
1051 | int retry_count = MEM_CGROUP_RECLAIM_RETRIES; | |
1052 | int progress; | |
1053 | int ret = 0; | |
1054 | ||
1055 | while (res_counter_set_limit(&memcg->res, val)) { | |
1056 | if (signal_pending(current)) { | |
1057 | ret = -EINTR; | |
1058 | break; | |
1059 | } | |
1060 | if (!retry_count) { | |
1061 | ret = -EBUSY; | |
1062 | break; | |
1063 | } | |
bced0520 KH |
1064 | progress = try_to_free_mem_cgroup_pages(memcg, |
1065 | GFP_HIGHUSER_MOVABLE); | |
628f4235 KH |
1066 | if (!progress) |
1067 | retry_count--; | |
1068 | } | |
1069 | return ret; | |
1070 | } | |
1071 | ||
1072 | ||
cc847582 KH |
1073 | /* |
1074 | * This routine traverse page_cgroup in given list and drop them all. | |
cc847582 KH |
1075 | * *And* this routine doesn't reclaim page itself, just removes page_cgroup. |
1076 | */ | |
f817ed48 | 1077 | static int mem_cgroup_force_empty_list(struct mem_cgroup *mem, |
072c56c1 | 1078 | struct mem_cgroup_per_zone *mz, |
b69408e8 | 1079 | enum lru_list lru) |
cc847582 | 1080 | { |
f817ed48 | 1081 | struct page_cgroup *pc, *busy; |
cc847582 | 1082 | unsigned long flags; |
f817ed48 | 1083 | unsigned long loop; |
072c56c1 | 1084 | struct list_head *list; |
f817ed48 | 1085 | int ret = 0; |
072c56c1 | 1086 | |
b69408e8 | 1087 | list = &mz->lists[lru]; |
cc847582 | 1088 | |
f817ed48 KH |
1089 | loop = MEM_CGROUP_ZSTAT(mz, lru); |
1090 | /* give some margin against EBUSY etc...*/ | |
1091 | loop += 256; | |
1092 | busy = NULL; | |
1093 | while (loop--) { | |
1094 | ret = 0; | |
1095 | spin_lock_irqsave(&mz->lru_lock, flags); | |
1096 | if (list_empty(list)) { | |
1097 | spin_unlock_irqrestore(&mz->lru_lock, flags); | |
52d4b9ac | 1098 | break; |
f817ed48 KH |
1099 | } |
1100 | pc = list_entry(list->prev, struct page_cgroup, lru); | |
1101 | if (busy == pc) { | |
1102 | list_move(&pc->lru, list); | |
1103 | busy = 0; | |
1104 | spin_unlock_irqrestore(&mz->lru_lock, flags); | |
1105 | continue; | |
1106 | } | |
9b3c0a07 | 1107 | spin_unlock_irqrestore(&mz->lru_lock, flags); |
f817ed48 KH |
1108 | |
1109 | ret = mem_cgroup_move_parent(pc, mem, GFP_HIGHUSER_MOVABLE); | |
1110 | if (ret == -ENOMEM) | |
52d4b9ac | 1111 | break; |
f817ed48 KH |
1112 | |
1113 | if (ret == -EBUSY || ret == -EINVAL) { | |
1114 | /* found lock contention or "pc" is obsolete. */ | |
1115 | busy = pc; | |
1116 | cond_resched(); | |
1117 | } else | |
1118 | busy = NULL; | |
cc847582 | 1119 | } |
f817ed48 KH |
1120 | if (!ret && !list_empty(list)) |
1121 | return -EBUSY; | |
1122 | return ret; | |
cc847582 KH |
1123 | } |
1124 | ||
1125 | /* | |
1126 | * make mem_cgroup's charge to be 0 if there is no task. | |
1127 | * This enables deleting this mem_cgroup. | |
1128 | */ | |
c1e862c1 | 1129 | static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all) |
cc847582 | 1130 | { |
f817ed48 KH |
1131 | int ret; |
1132 | int node, zid, shrink; | |
1133 | int nr_retries = MEM_CGROUP_RECLAIM_RETRIES; | |
c1e862c1 | 1134 | struct cgroup *cgrp = mem->css.cgroup; |
8869b8f6 | 1135 | |
cc847582 | 1136 | css_get(&mem->css); |
f817ed48 KH |
1137 | |
1138 | shrink = 0; | |
c1e862c1 KH |
1139 | /* should free all ? */ |
1140 | if (free_all) | |
1141 | goto try_to_free; | |
f817ed48 | 1142 | move_account: |
1ecaab2b | 1143 | while (mem->res.usage > 0) { |
f817ed48 | 1144 | ret = -EBUSY; |
c1e862c1 KH |
1145 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children)) |
1146 | goto out; | |
1147 | ret = -EINTR; | |
1148 | if (signal_pending(current)) | |
cc847582 | 1149 | goto out; |
52d4b9ac KH |
1150 | /* This is for making all *used* pages to be on LRU. */ |
1151 | lru_add_drain_all(); | |
f817ed48 KH |
1152 | ret = 0; |
1153 | for_each_node_state(node, N_POSSIBLE) { | |
1154 | for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) { | |
1ecaab2b | 1155 | struct mem_cgroup_per_zone *mz; |
b69408e8 | 1156 | enum lru_list l; |
1ecaab2b | 1157 | mz = mem_cgroup_zoneinfo(mem, node, zid); |
f817ed48 KH |
1158 | for_each_lru(l) { |
1159 | ret = mem_cgroup_force_empty_list(mem, | |
1160 | mz, l); | |
1161 | if (ret) | |
1162 | break; | |
1163 | } | |
1ecaab2b | 1164 | } |
f817ed48 KH |
1165 | if (ret) |
1166 | break; | |
1167 | } | |
1168 | /* it seems parent cgroup doesn't have enough mem */ | |
1169 | if (ret == -ENOMEM) | |
1170 | goto try_to_free; | |
52d4b9ac | 1171 | cond_resched(); |
cc847582 KH |
1172 | } |
1173 | ret = 0; | |
1174 | out: | |
1175 | css_put(&mem->css); | |
1176 | return ret; | |
f817ed48 KH |
1177 | |
1178 | try_to_free: | |
c1e862c1 KH |
1179 | /* returns EBUSY if there is a task or if we come here twice. */ |
1180 | if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) { | |
f817ed48 KH |
1181 | ret = -EBUSY; |
1182 | goto out; | |
1183 | } | |
c1e862c1 KH |
1184 | /* we call try-to-free pages for make this cgroup empty */ |
1185 | lru_add_drain_all(); | |
f817ed48 KH |
1186 | /* try to free all pages in this cgroup */ |
1187 | shrink = 1; | |
1188 | while (nr_retries && mem->res.usage > 0) { | |
1189 | int progress; | |
c1e862c1 KH |
1190 | |
1191 | if (signal_pending(current)) { | |
1192 | ret = -EINTR; | |
1193 | goto out; | |
1194 | } | |
f817ed48 KH |
1195 | progress = try_to_free_mem_cgroup_pages(mem, |
1196 | GFP_HIGHUSER_MOVABLE); | |
c1e862c1 | 1197 | if (!progress) { |
f817ed48 | 1198 | nr_retries--; |
c1e862c1 KH |
1199 | /* maybe some writeback is necessary */ |
1200 | congestion_wait(WRITE, HZ/10); | |
1201 | } | |
f817ed48 KH |
1202 | |
1203 | } | |
1204 | /* try move_account...there may be some *locked* pages. */ | |
1205 | if (mem->res.usage) | |
1206 | goto move_account; | |
1207 | ret = 0; | |
1208 | goto out; | |
cc847582 KH |
1209 | } |
1210 | ||
c1e862c1 KH |
1211 | int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event) |
1212 | { | |
1213 | return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true); | |
1214 | } | |
1215 | ||
1216 | ||
2c3daa72 | 1217 | static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft) |
8cdea7c0 | 1218 | { |
2c3daa72 PM |
1219 | return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res, |
1220 | cft->private); | |
8cdea7c0 | 1221 | } |
628f4235 KH |
1222 | /* |
1223 | * The user of this function is... | |
1224 | * RES_LIMIT. | |
1225 | */ | |
856c13aa PM |
1226 | static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft, |
1227 | const char *buffer) | |
8cdea7c0 | 1228 | { |
628f4235 KH |
1229 | struct mem_cgroup *memcg = mem_cgroup_from_cont(cont); |
1230 | unsigned long long val; | |
1231 | int ret; | |
1232 | ||
1233 | switch (cft->private) { | |
1234 | case RES_LIMIT: | |
1235 | /* This function does all necessary parse...reuse it */ | |
1236 | ret = res_counter_memparse_write_strategy(buffer, &val); | |
1237 | if (!ret) | |
1238 | ret = mem_cgroup_resize_limit(memcg, val); | |
1239 | break; | |
1240 | default: | |
1241 | ret = -EINVAL; /* should be BUG() ? */ | |
1242 | break; | |
1243 | } | |
1244 | return ret; | |
8cdea7c0 BS |
1245 | } |
1246 | ||
29f2a4da | 1247 | static int mem_cgroup_reset(struct cgroup *cont, unsigned int event) |
c84872e1 PE |
1248 | { |
1249 | struct mem_cgroup *mem; | |
1250 | ||
1251 | mem = mem_cgroup_from_cont(cont); | |
29f2a4da PE |
1252 | switch (event) { |
1253 | case RES_MAX_USAGE: | |
1254 | res_counter_reset_max(&mem->res); | |
1255 | break; | |
1256 | case RES_FAILCNT: | |
1257 | res_counter_reset_failcnt(&mem->res); | |
1258 | break; | |
1259 | } | |
85cc59db | 1260 | return 0; |
c84872e1 PE |
1261 | } |
1262 | ||
d2ceb9b7 KH |
1263 | static const struct mem_cgroup_stat_desc { |
1264 | const char *msg; | |
1265 | u64 unit; | |
1266 | } mem_cgroup_stat_desc[] = { | |
1267 | [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, }, | |
1268 | [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, }, | |
55e462b0 BR |
1269 | [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, }, |
1270 | [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, }, | |
d2ceb9b7 KH |
1271 | }; |
1272 | ||
c64745cf PM |
1273 | static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft, |
1274 | struct cgroup_map_cb *cb) | |
d2ceb9b7 | 1275 | { |
d2ceb9b7 KH |
1276 | struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont); |
1277 | struct mem_cgroup_stat *stat = &mem_cont->stat; | |
1278 | int i; | |
1279 | ||
1280 | for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) { | |
1281 | s64 val; | |
1282 | ||
1283 | val = mem_cgroup_read_stat(stat, i); | |
1284 | val *= mem_cgroup_stat_desc[i].unit; | |
c64745cf | 1285 | cb->fill(cb, mem_cgroup_stat_desc[i].msg, val); |
d2ceb9b7 | 1286 | } |
6d12e2d8 KH |
1287 | /* showing # of active pages */ |
1288 | { | |
4f98a2fe RR |
1289 | unsigned long active_anon, inactive_anon; |
1290 | unsigned long active_file, inactive_file; | |
7b854121 | 1291 | unsigned long unevictable; |
4f98a2fe RR |
1292 | |
1293 | inactive_anon = mem_cgroup_get_all_zonestat(mem_cont, | |
1294 | LRU_INACTIVE_ANON); | |
1295 | active_anon = mem_cgroup_get_all_zonestat(mem_cont, | |
1296 | LRU_ACTIVE_ANON); | |
1297 | inactive_file = mem_cgroup_get_all_zonestat(mem_cont, | |
1298 | LRU_INACTIVE_FILE); | |
1299 | active_file = mem_cgroup_get_all_zonestat(mem_cont, | |
1300 | LRU_ACTIVE_FILE); | |
7b854121 LS |
1301 | unevictable = mem_cgroup_get_all_zonestat(mem_cont, |
1302 | LRU_UNEVICTABLE); | |
1303 | ||
4f98a2fe RR |
1304 | cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE); |
1305 | cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE); | |
1306 | cb->fill(cb, "active_file", (active_file) * PAGE_SIZE); | |
1307 | cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE); | |
7b854121 LS |
1308 | cb->fill(cb, "unevictable", unevictable * PAGE_SIZE); |
1309 | ||
6d12e2d8 | 1310 | } |
d2ceb9b7 KH |
1311 | return 0; |
1312 | } | |
1313 | ||
c1e862c1 | 1314 | |
8cdea7c0 BS |
1315 | static struct cftype mem_cgroup_files[] = { |
1316 | { | |
0eea1030 | 1317 | .name = "usage_in_bytes", |
8cdea7c0 | 1318 | .private = RES_USAGE, |
2c3daa72 | 1319 | .read_u64 = mem_cgroup_read, |
8cdea7c0 | 1320 | }, |
c84872e1 PE |
1321 | { |
1322 | .name = "max_usage_in_bytes", | |
1323 | .private = RES_MAX_USAGE, | |
29f2a4da | 1324 | .trigger = mem_cgroup_reset, |
c84872e1 PE |
1325 | .read_u64 = mem_cgroup_read, |
1326 | }, | |
8cdea7c0 | 1327 | { |
0eea1030 | 1328 | .name = "limit_in_bytes", |
8cdea7c0 | 1329 | .private = RES_LIMIT, |
856c13aa | 1330 | .write_string = mem_cgroup_write, |
2c3daa72 | 1331 | .read_u64 = mem_cgroup_read, |
8cdea7c0 BS |
1332 | }, |
1333 | { | |
1334 | .name = "failcnt", | |
1335 | .private = RES_FAILCNT, | |
29f2a4da | 1336 | .trigger = mem_cgroup_reset, |
2c3daa72 | 1337 | .read_u64 = mem_cgroup_read, |
8cdea7c0 | 1338 | }, |
d2ceb9b7 KH |
1339 | { |
1340 | .name = "stat", | |
c64745cf | 1341 | .read_map = mem_control_stat_show, |
d2ceb9b7 | 1342 | }, |
c1e862c1 KH |
1343 | { |
1344 | .name = "force_empty", | |
1345 | .trigger = mem_cgroup_force_empty_write, | |
1346 | }, | |
8cdea7c0 BS |
1347 | }; |
1348 | ||
6d12e2d8 KH |
1349 | static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
1350 | { | |
1351 | struct mem_cgroup_per_node *pn; | |
1ecaab2b | 1352 | struct mem_cgroup_per_zone *mz; |
b69408e8 | 1353 | enum lru_list l; |
41e3355d | 1354 | int zone, tmp = node; |
1ecaab2b KH |
1355 | /* |
1356 | * This routine is called against possible nodes. | |
1357 | * But it's BUG to call kmalloc() against offline node. | |
1358 | * | |
1359 | * TODO: this routine can waste much memory for nodes which will | |
1360 | * never be onlined. It's better to use memory hotplug callback | |
1361 | * function. | |
1362 | */ | |
41e3355d KH |
1363 | if (!node_state(node, N_NORMAL_MEMORY)) |
1364 | tmp = -1; | |
1365 | pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp); | |
6d12e2d8 KH |
1366 | if (!pn) |
1367 | return 1; | |
1ecaab2b | 1368 | |
6d12e2d8 KH |
1369 | mem->info.nodeinfo[node] = pn; |
1370 | memset(pn, 0, sizeof(*pn)); | |
1ecaab2b KH |
1371 | |
1372 | for (zone = 0; zone < MAX_NR_ZONES; zone++) { | |
1373 | mz = &pn->zoneinfo[zone]; | |
072c56c1 | 1374 | spin_lock_init(&mz->lru_lock); |
b69408e8 CL |
1375 | for_each_lru(l) |
1376 | INIT_LIST_HEAD(&mz->lists[l]); | |
1ecaab2b | 1377 | } |
6d12e2d8 KH |
1378 | return 0; |
1379 | } | |
1380 | ||
1ecaab2b KH |
1381 | static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node) |
1382 | { | |
1383 | kfree(mem->info.nodeinfo[node]); | |
1384 | } | |
1385 | ||
c8dad2bb JB |
1386 | static int mem_cgroup_size(void) |
1387 | { | |
1388 | int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu); | |
1389 | return sizeof(struct mem_cgroup) + cpustat_size; | |
1390 | } | |
1391 | ||
33327948 KH |
1392 | static struct mem_cgroup *mem_cgroup_alloc(void) |
1393 | { | |
1394 | struct mem_cgroup *mem; | |
c8dad2bb | 1395 | int size = mem_cgroup_size(); |
33327948 | 1396 | |
c8dad2bb JB |
1397 | if (size < PAGE_SIZE) |
1398 | mem = kmalloc(size, GFP_KERNEL); | |
33327948 | 1399 | else |
c8dad2bb | 1400 | mem = vmalloc(size); |
33327948 KH |
1401 | |
1402 | if (mem) | |
c8dad2bb | 1403 | memset(mem, 0, size); |
33327948 KH |
1404 | return mem; |
1405 | } | |
1406 | ||
1407 | static void mem_cgroup_free(struct mem_cgroup *mem) | |
1408 | { | |
c8dad2bb | 1409 | if (mem_cgroup_size() < PAGE_SIZE) |
33327948 KH |
1410 | kfree(mem); |
1411 | else | |
1412 | vfree(mem); | |
1413 | } | |
1414 | ||
1415 | ||
c077719b KH |
1416 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP |
1417 | static void __init enable_swap_cgroup(void) | |
1418 | { | |
1419 | if (!mem_cgroup_subsys.disabled && really_do_swap_account) | |
1420 | do_swap_account = 1; | |
1421 | } | |
1422 | #else | |
1423 | static void __init enable_swap_cgroup(void) | |
1424 | { | |
1425 | } | |
1426 | #endif | |
1427 | ||
8cdea7c0 BS |
1428 | static struct cgroup_subsys_state * |
1429 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) | |
1430 | { | |
1431 | struct mem_cgroup *mem; | |
6d12e2d8 | 1432 | int node; |
8cdea7c0 | 1433 | |
c8dad2bb JB |
1434 | mem = mem_cgroup_alloc(); |
1435 | if (!mem) | |
1436 | return ERR_PTR(-ENOMEM); | |
78fb7466 | 1437 | |
8cdea7c0 | 1438 | res_counter_init(&mem->res); |
1ecaab2b | 1439 | |
6d12e2d8 KH |
1440 | for_each_node_state(node, N_POSSIBLE) |
1441 | if (alloc_mem_cgroup_per_zone_info(mem, node)) | |
1442 | goto free_out; | |
c077719b KH |
1443 | /* root ? */ |
1444 | if (cont->parent == NULL) | |
1445 | enable_swap_cgroup(); | |
6d12e2d8 | 1446 | |
8cdea7c0 | 1447 | return &mem->css; |
6d12e2d8 KH |
1448 | free_out: |
1449 | for_each_node_state(node, N_POSSIBLE) | |
1ecaab2b | 1450 | free_mem_cgroup_per_zone_info(mem, node); |
c8dad2bb | 1451 | mem_cgroup_free(mem); |
2dda81ca | 1452 | return ERR_PTR(-ENOMEM); |
8cdea7c0 BS |
1453 | } |
1454 | ||
df878fb0 KH |
1455 | static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss, |
1456 | struct cgroup *cont) | |
1457 | { | |
1458 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); | |
c1e862c1 | 1459 | mem_cgroup_force_empty(mem, false); |
df878fb0 KH |
1460 | } |
1461 | ||
8cdea7c0 BS |
1462 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, |
1463 | struct cgroup *cont) | |
1464 | { | |
6d12e2d8 KH |
1465 | int node; |
1466 | struct mem_cgroup *mem = mem_cgroup_from_cont(cont); | |
1467 | ||
1468 | for_each_node_state(node, N_POSSIBLE) | |
1ecaab2b | 1469 | free_mem_cgroup_per_zone_info(mem, node); |
6d12e2d8 | 1470 | |
33327948 | 1471 | mem_cgroup_free(mem_cgroup_from_cont(cont)); |
8cdea7c0 BS |
1472 | } |
1473 | ||
1474 | static int mem_cgroup_populate(struct cgroup_subsys *ss, | |
1475 | struct cgroup *cont) | |
1476 | { | |
1477 | return cgroup_add_files(cont, ss, mem_cgroup_files, | |
1478 | ARRAY_SIZE(mem_cgroup_files)); | |
1479 | } | |
1480 | ||
67e465a7 BS |
1481 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
1482 | struct cgroup *cont, | |
1483 | struct cgroup *old_cont, | |
1484 | struct task_struct *p) | |
1485 | { | |
1486 | struct mm_struct *mm; | |
1487 | struct mem_cgroup *mem, *old_mem; | |
1488 | ||
1489 | mm = get_task_mm(p); | |
1490 | if (mm == NULL) | |
1491 | return; | |
1492 | ||
1493 | mem = mem_cgroup_from_cont(cont); | |
1494 | old_mem = mem_cgroup_from_cont(old_cont); | |
1495 | ||
67e465a7 BS |
1496 | /* |
1497 | * Only thread group leaders are allowed to migrate, the mm_struct is | |
1498 | * in effect owned by the leader | |
1499 | */ | |
52ea27eb | 1500 | if (!thread_group_leader(p)) |
67e465a7 BS |
1501 | goto out; |
1502 | ||
67e465a7 BS |
1503 | out: |
1504 | mmput(mm); | |
67e465a7 BS |
1505 | } |
1506 | ||
8cdea7c0 BS |
1507 | struct cgroup_subsys mem_cgroup_subsys = { |
1508 | .name = "memory", | |
1509 | .subsys_id = mem_cgroup_subsys_id, | |
1510 | .create = mem_cgroup_create, | |
df878fb0 | 1511 | .pre_destroy = mem_cgroup_pre_destroy, |
8cdea7c0 BS |
1512 | .destroy = mem_cgroup_destroy, |
1513 | .populate = mem_cgroup_populate, | |
67e465a7 | 1514 | .attach = mem_cgroup_move_task, |
6d12e2d8 | 1515 | .early_init = 0, |
8cdea7c0 | 1516 | }; |
c077719b KH |
1517 | |
1518 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP | |
1519 | ||
1520 | static int __init disable_swap_account(char *s) | |
1521 | { | |
1522 | really_do_swap_account = 0; | |
1523 | return 1; | |
1524 | } | |
1525 | __setup("noswapaccount", disable_swap_account); | |
1526 | #endif |