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> |
8a9f3ccd | 24 | #include <linux/page-flags.h> |
66e1707b | 25 | #include <linux/backing-dev.h> |
8a9f3ccd BS |
26 | #include <linux/bit_spinlock.h> |
27 | #include <linux/rcupdate.h> | |
66e1707b BS |
28 | #include <linux/swap.h> |
29 | #include <linux/spinlock.h> | |
30 | #include <linux/fs.h> | |
8cdea7c0 | 31 | |
8697d331 BS |
32 | #include <asm/uaccess.h> |
33 | ||
8cdea7c0 | 34 | struct cgroup_subsys mem_cgroup_subsys; |
66e1707b | 35 | static const int MEM_CGROUP_RECLAIM_RETRIES = 5; |
8cdea7c0 BS |
36 | |
37 | /* | |
38 | * The memory controller data structure. The memory controller controls both | |
39 | * page cache and RSS per cgroup. We would eventually like to provide | |
40 | * statistics based on the statistics developed by Rik Van Riel for clock-pro, | |
41 | * to help the administrator determine what knobs to tune. | |
42 | * | |
43 | * TODO: Add a water mark for the memory controller. Reclaim will begin when | |
8a9f3ccd BS |
44 | * we hit the water mark. May be even add a low water mark, such that |
45 | * no reclaim occurs from a cgroup at it's low water mark, this is | |
46 | * a feature that will be implemented much later in the future. | |
8cdea7c0 BS |
47 | */ |
48 | struct mem_cgroup { | |
49 | struct cgroup_subsys_state css; | |
50 | /* | |
51 | * the counter to account for memory usage | |
52 | */ | |
53 | struct res_counter res; | |
78fb7466 PE |
54 | /* |
55 | * Per cgroup active and inactive list, similar to the | |
56 | * per zone LRU lists. | |
57 | * TODO: Consider making these lists per zone | |
58 | */ | |
59 | struct list_head active_list; | |
60 | struct list_head inactive_list; | |
66e1707b BS |
61 | /* |
62 | * spin_lock to protect the per cgroup LRU | |
63 | */ | |
64 | spinlock_t lru_lock; | |
8697d331 | 65 | unsigned long control_type; /* control RSS or RSS+Pagecache */ |
8cdea7c0 BS |
66 | }; |
67 | ||
8a9f3ccd BS |
68 | /* |
69 | * We use the lower bit of the page->page_cgroup pointer as a bit spin | |
70 | * lock. We need to ensure that page->page_cgroup is atleast two | |
71 | * byte aligned (based on comments from Nick Piggin) | |
72 | */ | |
73 | #define PAGE_CGROUP_LOCK_BIT 0x0 | |
74 | #define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT) | |
75 | ||
8cdea7c0 BS |
76 | /* |
77 | * A page_cgroup page is associated with every page descriptor. The | |
78 | * page_cgroup helps us identify information about the cgroup | |
79 | */ | |
80 | struct page_cgroup { | |
81 | struct list_head lru; /* per cgroup LRU list */ | |
82 | struct page *page; | |
83 | struct mem_cgroup *mem_cgroup; | |
8a9f3ccd BS |
84 | atomic_t ref_cnt; /* Helpful when pages move b/w */ |
85 | /* mapped and cached states */ | |
8cdea7c0 BS |
86 | }; |
87 | ||
8697d331 BS |
88 | enum { |
89 | MEM_CGROUP_TYPE_UNSPEC = 0, | |
90 | MEM_CGROUP_TYPE_MAPPED, | |
91 | MEM_CGROUP_TYPE_CACHED, | |
92 | MEM_CGROUP_TYPE_ALL, | |
93 | MEM_CGROUP_TYPE_MAX, | |
94 | }; | |
95 | ||
96 | static struct mem_cgroup init_mem_cgroup; | |
8cdea7c0 BS |
97 | |
98 | static inline | |
99 | struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont) | |
100 | { | |
101 | return container_of(cgroup_subsys_state(cont, | |
102 | mem_cgroup_subsys_id), struct mem_cgroup, | |
103 | css); | |
104 | } | |
105 | ||
78fb7466 PE |
106 | static inline |
107 | struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p) | |
108 | { | |
109 | return container_of(task_subsys_state(p, mem_cgroup_subsys_id), | |
110 | struct mem_cgroup, css); | |
111 | } | |
112 | ||
113 | void mm_init_cgroup(struct mm_struct *mm, struct task_struct *p) | |
114 | { | |
115 | struct mem_cgroup *mem; | |
116 | ||
117 | mem = mem_cgroup_from_task(p); | |
118 | css_get(&mem->css); | |
119 | mm->mem_cgroup = mem; | |
120 | } | |
121 | ||
122 | void mm_free_cgroup(struct mm_struct *mm) | |
123 | { | |
124 | css_put(&mm->mem_cgroup->css); | |
125 | } | |
126 | ||
8a9f3ccd BS |
127 | static inline int page_cgroup_locked(struct page *page) |
128 | { | |
129 | return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT, | |
130 | &page->page_cgroup); | |
131 | } | |
132 | ||
78fb7466 PE |
133 | void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc) |
134 | { | |
8a9f3ccd BS |
135 | int locked; |
136 | ||
137 | /* | |
138 | * While resetting the page_cgroup we might not hold the | |
139 | * page_cgroup lock. free_hot_cold_page() is an example | |
140 | * of such a scenario | |
141 | */ | |
142 | if (pc) | |
143 | VM_BUG_ON(!page_cgroup_locked(page)); | |
144 | locked = (page->page_cgroup & PAGE_CGROUP_LOCK); | |
145 | page->page_cgroup = ((unsigned long)pc | locked); | |
78fb7466 PE |
146 | } |
147 | ||
148 | struct page_cgroup *page_get_page_cgroup(struct page *page) | |
149 | { | |
8a9f3ccd BS |
150 | return (struct page_cgroup *) |
151 | (page->page_cgroup & ~PAGE_CGROUP_LOCK); | |
152 | } | |
153 | ||
8697d331 | 154 | static void __always_inline lock_page_cgroup(struct page *page) |
8a9f3ccd BS |
155 | { |
156 | bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup); | |
157 | VM_BUG_ON(!page_cgroup_locked(page)); | |
158 | } | |
159 | ||
8697d331 | 160 | static void __always_inline unlock_page_cgroup(struct page *page) |
8a9f3ccd BS |
161 | { |
162 | bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup); | |
163 | } | |
164 | ||
8697d331 | 165 | static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active) |
66e1707b BS |
166 | { |
167 | if (active) | |
168 | list_move(&pc->lru, &pc->mem_cgroup->active_list); | |
169 | else | |
170 | list_move(&pc->lru, &pc->mem_cgroup->inactive_list); | |
171 | } | |
172 | ||
173 | /* | |
174 | * This routine assumes that the appropriate zone's lru lock is already held | |
175 | */ | |
176 | void mem_cgroup_move_lists(struct page_cgroup *pc, bool active) | |
177 | { | |
178 | struct mem_cgroup *mem; | |
179 | if (!pc) | |
180 | return; | |
181 | ||
182 | mem = pc->mem_cgroup; | |
183 | ||
184 | spin_lock(&mem->lru_lock); | |
185 | __mem_cgroup_move_lists(pc, active); | |
186 | spin_unlock(&mem->lru_lock); | |
187 | } | |
188 | ||
189 | unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan, | |
190 | struct list_head *dst, | |
191 | unsigned long *scanned, int order, | |
192 | int mode, struct zone *z, | |
193 | struct mem_cgroup *mem_cont, | |
194 | int active) | |
195 | { | |
196 | unsigned long nr_taken = 0; | |
197 | struct page *page; | |
198 | unsigned long scan; | |
199 | LIST_HEAD(pc_list); | |
200 | struct list_head *src; | |
201 | struct page_cgroup *pc; | |
202 | ||
203 | if (active) | |
204 | src = &mem_cont->active_list; | |
205 | else | |
206 | src = &mem_cont->inactive_list; | |
207 | ||
208 | spin_lock(&mem_cont->lru_lock); | |
209 | for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) { | |
210 | pc = list_entry(src->prev, struct page_cgroup, lru); | |
211 | page = pc->page; | |
212 | VM_BUG_ON(!pc); | |
213 | ||
214 | if (PageActive(page) && !active) { | |
215 | __mem_cgroup_move_lists(pc, true); | |
216 | scan--; | |
217 | continue; | |
218 | } | |
219 | if (!PageActive(page) && active) { | |
220 | __mem_cgroup_move_lists(pc, false); | |
221 | scan--; | |
222 | continue; | |
223 | } | |
224 | ||
225 | /* | |
226 | * Reclaim, per zone | |
227 | * TODO: make the active/inactive lists per zone | |
228 | */ | |
229 | if (page_zone(page) != z) | |
230 | continue; | |
231 | ||
232 | /* | |
233 | * Check if the meta page went away from under us | |
234 | */ | |
235 | if (!list_empty(&pc->lru)) | |
236 | list_move(&pc->lru, &pc_list); | |
237 | else | |
238 | continue; | |
239 | ||
240 | if (__isolate_lru_page(page, mode) == 0) { | |
241 | list_move(&page->lru, dst); | |
242 | nr_taken++; | |
243 | } | |
244 | } | |
245 | ||
246 | list_splice(&pc_list, src); | |
247 | spin_unlock(&mem_cont->lru_lock); | |
248 | ||
249 | *scanned = scan; | |
250 | return nr_taken; | |
251 | } | |
252 | ||
8a9f3ccd BS |
253 | /* |
254 | * Charge the memory controller for page usage. | |
255 | * Return | |
256 | * 0 if the charge was successful | |
257 | * < 0 if the cgroup is over its limit | |
258 | */ | |
e1a1cd59 BS |
259 | int mem_cgroup_charge(struct page *page, struct mm_struct *mm, |
260 | gfp_t gfp_mask) | |
8a9f3ccd BS |
261 | { |
262 | struct mem_cgroup *mem; | |
263 | struct page_cgroup *pc, *race_pc; | |
66e1707b BS |
264 | unsigned long flags; |
265 | unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES; | |
8a9f3ccd BS |
266 | |
267 | /* | |
268 | * Should page_cgroup's go to their own slab? | |
269 | * One could optimize the performance of the charging routine | |
270 | * by saving a bit in the page_flags and using it as a lock | |
271 | * to see if the cgroup page already has a page_cgroup associated | |
272 | * with it | |
273 | */ | |
66e1707b | 274 | retry: |
8a9f3ccd BS |
275 | lock_page_cgroup(page); |
276 | pc = page_get_page_cgroup(page); | |
277 | /* | |
278 | * The page_cgroup exists and the page has already been accounted | |
279 | */ | |
280 | if (pc) { | |
66e1707b BS |
281 | if (unlikely(!atomic_inc_not_zero(&pc->ref_cnt))) { |
282 | /* this page is under being uncharged ? */ | |
283 | unlock_page_cgroup(page); | |
284 | cpu_relax(); | |
285 | goto retry; | |
286 | } else | |
287 | goto done; | |
8a9f3ccd BS |
288 | } |
289 | ||
290 | unlock_page_cgroup(page); | |
291 | ||
e1a1cd59 | 292 | pc = kzalloc(sizeof(struct page_cgroup), gfp_mask); |
8a9f3ccd BS |
293 | if (pc == NULL) |
294 | goto err; | |
295 | ||
296 | rcu_read_lock(); | |
297 | /* | |
298 | * We always charge the cgroup the mm_struct belongs to | |
299 | * the mm_struct's mem_cgroup changes on task migration if the | |
300 | * thread group leader migrates. It's possible that mm is not | |
301 | * set, if so charge the init_mm (happens for pagecache usage). | |
302 | */ | |
303 | if (!mm) | |
304 | mm = &init_mm; | |
305 | ||
306 | mem = rcu_dereference(mm->mem_cgroup); | |
307 | /* | |
308 | * For every charge from the cgroup, increment reference | |
309 | * count | |
310 | */ | |
311 | css_get(&mem->css); | |
312 | rcu_read_unlock(); | |
313 | ||
314 | /* | |
315 | * If we created the page_cgroup, we should free it on exceeding | |
316 | * the cgroup limit. | |
317 | */ | |
0eea1030 | 318 | while (res_counter_charge(&mem->res, PAGE_SIZE)) { |
e1a1cd59 BS |
319 | bool is_atomic = gfp_mask & GFP_ATOMIC; |
320 | /* | |
321 | * We cannot reclaim under GFP_ATOMIC, fail the charge | |
322 | */ | |
323 | if (is_atomic) | |
324 | goto noreclaim; | |
325 | ||
326 | if (try_to_free_mem_cgroup_pages(mem, gfp_mask)) | |
66e1707b BS |
327 | continue; |
328 | ||
329 | /* | |
330 | * try_to_free_mem_cgroup_pages() might not give us a full | |
331 | * picture of reclaim. Some pages are reclaimed and might be | |
332 | * moved to swap cache or just unmapped from the cgroup. | |
333 | * Check the limit again to see if the reclaim reduced the | |
334 | * current usage of the cgroup before giving up | |
335 | */ | |
336 | if (res_counter_check_under_limit(&mem->res)) | |
337 | continue; | |
338 | /* | |
339 | * Since we control both RSS and cache, we end up with a | |
340 | * very interesting scenario where we end up reclaiming | |
341 | * memory (essentially RSS), since the memory is pushed | |
342 | * to swap cache, we eventually end up adding those | |
343 | * pages back to our list. Hence we give ourselves a | |
344 | * few chances before we fail | |
345 | */ | |
346 | else if (nr_retries--) { | |
347 | congestion_wait(WRITE, HZ/10); | |
348 | continue; | |
349 | } | |
e1a1cd59 | 350 | noreclaim: |
8a9f3ccd | 351 | css_put(&mem->css); |
e1a1cd59 BS |
352 | if (!is_atomic) |
353 | mem_cgroup_out_of_memory(mem, GFP_KERNEL); | |
8a9f3ccd BS |
354 | goto free_pc; |
355 | } | |
356 | ||
357 | lock_page_cgroup(page); | |
358 | /* | |
359 | * Check if somebody else beat us to allocating the page_cgroup | |
360 | */ | |
361 | race_pc = page_get_page_cgroup(page); | |
362 | if (race_pc) { | |
363 | kfree(pc); | |
364 | pc = race_pc; | |
365 | atomic_inc(&pc->ref_cnt); | |
0eea1030 | 366 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
8a9f3ccd BS |
367 | css_put(&mem->css); |
368 | goto done; | |
369 | } | |
370 | ||
371 | atomic_set(&pc->ref_cnt, 1); | |
372 | pc->mem_cgroup = mem; | |
373 | pc->page = page; | |
374 | page_assign_page_cgroup(page, pc); | |
375 | ||
66e1707b BS |
376 | spin_lock_irqsave(&mem->lru_lock, flags); |
377 | list_add(&pc->lru, &mem->active_list); | |
378 | spin_unlock_irqrestore(&mem->lru_lock, flags); | |
379 | ||
8a9f3ccd BS |
380 | done: |
381 | unlock_page_cgroup(page); | |
382 | return 0; | |
383 | free_pc: | |
384 | kfree(pc); | |
8a9f3ccd | 385 | err: |
8a9f3ccd BS |
386 | return -ENOMEM; |
387 | } | |
388 | ||
8697d331 BS |
389 | /* |
390 | * See if the cached pages should be charged at all? | |
391 | */ | |
e1a1cd59 BS |
392 | int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm, |
393 | gfp_t gfp_mask) | |
8697d331 BS |
394 | { |
395 | struct mem_cgroup *mem; | |
396 | if (!mm) | |
397 | mm = &init_mm; | |
398 | ||
399 | mem = rcu_dereference(mm->mem_cgroup); | |
400 | if (mem->control_type == MEM_CGROUP_TYPE_ALL) | |
e1a1cd59 | 401 | return mem_cgroup_charge(page, mm, gfp_mask); |
8697d331 BS |
402 | else |
403 | return 0; | |
404 | } | |
405 | ||
8a9f3ccd BS |
406 | /* |
407 | * Uncharging is always a welcome operation, we never complain, simply | |
408 | * uncharge. | |
409 | */ | |
410 | void mem_cgroup_uncharge(struct page_cgroup *pc) | |
411 | { | |
412 | struct mem_cgroup *mem; | |
413 | struct page *page; | |
66e1707b | 414 | unsigned long flags; |
8a9f3ccd | 415 | |
8697d331 BS |
416 | /* |
417 | * This can handle cases when a page is not charged at all and we | |
418 | * are switching between handling the control_type. | |
419 | */ | |
8a9f3ccd BS |
420 | if (!pc) |
421 | return; | |
422 | ||
423 | if (atomic_dec_and_test(&pc->ref_cnt)) { | |
424 | page = pc->page; | |
425 | lock_page_cgroup(page); | |
426 | mem = pc->mem_cgroup; | |
427 | css_put(&mem->css); | |
428 | page_assign_page_cgroup(page, NULL); | |
429 | unlock_page_cgroup(page); | |
0eea1030 | 430 | res_counter_uncharge(&mem->res, PAGE_SIZE); |
66e1707b BS |
431 | |
432 | spin_lock_irqsave(&mem->lru_lock, flags); | |
433 | list_del_init(&pc->lru); | |
434 | spin_unlock_irqrestore(&mem->lru_lock, flags); | |
8a9f3ccd BS |
435 | kfree(pc); |
436 | } | |
78fb7466 PE |
437 | } |
438 | ||
0eea1030 BS |
439 | int mem_cgroup_write_strategy(char *buf, unsigned long long *tmp) |
440 | { | |
441 | *tmp = memparse(buf, &buf); | |
442 | if (*buf != '\0') | |
443 | return -EINVAL; | |
444 | ||
445 | /* | |
446 | * Round up the value to the closest page size | |
447 | */ | |
448 | *tmp = ((*tmp + PAGE_SIZE - 1) >> PAGE_SHIFT) << PAGE_SHIFT; | |
449 | return 0; | |
450 | } | |
451 | ||
452 | static ssize_t mem_cgroup_read(struct cgroup *cont, | |
453 | struct cftype *cft, struct file *file, | |
454 | char __user *userbuf, size_t nbytes, loff_t *ppos) | |
8cdea7c0 BS |
455 | { |
456 | return res_counter_read(&mem_cgroup_from_cont(cont)->res, | |
0eea1030 BS |
457 | cft->private, userbuf, nbytes, ppos, |
458 | NULL); | |
8cdea7c0 BS |
459 | } |
460 | ||
461 | static ssize_t mem_cgroup_write(struct cgroup *cont, struct cftype *cft, | |
462 | struct file *file, const char __user *userbuf, | |
463 | size_t nbytes, loff_t *ppos) | |
464 | { | |
465 | return res_counter_write(&mem_cgroup_from_cont(cont)->res, | |
0eea1030 BS |
466 | cft->private, userbuf, nbytes, ppos, |
467 | mem_cgroup_write_strategy); | |
8cdea7c0 BS |
468 | } |
469 | ||
8697d331 BS |
470 | static ssize_t mem_control_type_write(struct cgroup *cont, |
471 | struct cftype *cft, struct file *file, | |
472 | const char __user *userbuf, | |
473 | size_t nbytes, loff_t *pos) | |
474 | { | |
475 | int ret; | |
476 | char *buf, *end; | |
477 | unsigned long tmp; | |
478 | struct mem_cgroup *mem; | |
479 | ||
480 | mem = mem_cgroup_from_cont(cont); | |
481 | buf = kmalloc(nbytes + 1, GFP_KERNEL); | |
482 | ret = -ENOMEM; | |
483 | if (buf == NULL) | |
484 | goto out; | |
485 | ||
486 | buf[nbytes] = 0; | |
487 | ret = -EFAULT; | |
488 | if (copy_from_user(buf, userbuf, nbytes)) | |
489 | goto out_free; | |
490 | ||
491 | ret = -EINVAL; | |
492 | tmp = simple_strtoul(buf, &end, 10); | |
493 | if (*end != '\0') | |
494 | goto out_free; | |
495 | ||
496 | if (tmp <= MEM_CGROUP_TYPE_UNSPEC || tmp >= MEM_CGROUP_TYPE_MAX) | |
497 | goto out_free; | |
498 | ||
499 | mem->control_type = tmp; | |
500 | ret = nbytes; | |
501 | out_free: | |
502 | kfree(buf); | |
503 | out: | |
504 | return ret; | |
505 | } | |
506 | ||
507 | static ssize_t mem_control_type_read(struct cgroup *cont, | |
508 | struct cftype *cft, | |
509 | struct file *file, char __user *userbuf, | |
510 | size_t nbytes, loff_t *ppos) | |
511 | { | |
512 | unsigned long val; | |
513 | char buf[64], *s; | |
514 | struct mem_cgroup *mem; | |
515 | ||
516 | mem = mem_cgroup_from_cont(cont); | |
517 | s = buf; | |
518 | val = mem->control_type; | |
519 | s += sprintf(s, "%lu\n", val); | |
520 | return simple_read_from_buffer((void __user *)userbuf, nbytes, | |
521 | ppos, buf, s - buf); | |
522 | } | |
523 | ||
8cdea7c0 BS |
524 | static struct cftype mem_cgroup_files[] = { |
525 | { | |
0eea1030 | 526 | .name = "usage_in_bytes", |
8cdea7c0 BS |
527 | .private = RES_USAGE, |
528 | .read = mem_cgroup_read, | |
529 | }, | |
530 | { | |
0eea1030 | 531 | .name = "limit_in_bytes", |
8cdea7c0 BS |
532 | .private = RES_LIMIT, |
533 | .write = mem_cgroup_write, | |
534 | .read = mem_cgroup_read, | |
535 | }, | |
536 | { | |
537 | .name = "failcnt", | |
538 | .private = RES_FAILCNT, | |
539 | .read = mem_cgroup_read, | |
540 | }, | |
8697d331 BS |
541 | { |
542 | .name = "control_type", | |
543 | .write = mem_control_type_write, | |
544 | .read = mem_control_type_read, | |
545 | }, | |
8cdea7c0 BS |
546 | }; |
547 | ||
78fb7466 PE |
548 | static struct mem_cgroup init_mem_cgroup; |
549 | ||
8cdea7c0 BS |
550 | static struct cgroup_subsys_state * |
551 | mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont) | |
552 | { | |
553 | struct mem_cgroup *mem; | |
554 | ||
78fb7466 PE |
555 | if (unlikely((cont->parent) == NULL)) { |
556 | mem = &init_mem_cgroup; | |
557 | init_mm.mem_cgroup = mem; | |
558 | } else | |
559 | mem = kzalloc(sizeof(struct mem_cgroup), GFP_KERNEL); | |
560 | ||
561 | if (mem == NULL) | |
562 | return NULL; | |
8cdea7c0 BS |
563 | |
564 | res_counter_init(&mem->res); | |
8a9f3ccd BS |
565 | INIT_LIST_HEAD(&mem->active_list); |
566 | INIT_LIST_HEAD(&mem->inactive_list); | |
66e1707b | 567 | spin_lock_init(&mem->lru_lock); |
8697d331 | 568 | mem->control_type = MEM_CGROUP_TYPE_ALL; |
8cdea7c0 BS |
569 | return &mem->css; |
570 | } | |
571 | ||
572 | static void mem_cgroup_destroy(struct cgroup_subsys *ss, | |
573 | struct cgroup *cont) | |
574 | { | |
575 | kfree(mem_cgroup_from_cont(cont)); | |
576 | } | |
577 | ||
578 | static int mem_cgroup_populate(struct cgroup_subsys *ss, | |
579 | struct cgroup *cont) | |
580 | { | |
581 | return cgroup_add_files(cont, ss, mem_cgroup_files, | |
582 | ARRAY_SIZE(mem_cgroup_files)); | |
583 | } | |
584 | ||
67e465a7 BS |
585 | static void mem_cgroup_move_task(struct cgroup_subsys *ss, |
586 | struct cgroup *cont, | |
587 | struct cgroup *old_cont, | |
588 | struct task_struct *p) | |
589 | { | |
590 | struct mm_struct *mm; | |
591 | struct mem_cgroup *mem, *old_mem; | |
592 | ||
593 | mm = get_task_mm(p); | |
594 | if (mm == NULL) | |
595 | return; | |
596 | ||
597 | mem = mem_cgroup_from_cont(cont); | |
598 | old_mem = mem_cgroup_from_cont(old_cont); | |
599 | ||
600 | if (mem == old_mem) | |
601 | goto out; | |
602 | ||
603 | /* | |
604 | * Only thread group leaders are allowed to migrate, the mm_struct is | |
605 | * in effect owned by the leader | |
606 | */ | |
607 | if (p->tgid != p->pid) | |
608 | goto out; | |
609 | ||
610 | css_get(&mem->css); | |
611 | rcu_assign_pointer(mm->mem_cgroup, mem); | |
612 | css_put(&old_mem->css); | |
613 | ||
614 | out: | |
615 | mmput(mm); | |
616 | return; | |
617 | } | |
618 | ||
8cdea7c0 BS |
619 | struct cgroup_subsys mem_cgroup_subsys = { |
620 | .name = "memory", | |
621 | .subsys_id = mem_cgroup_subsys_id, | |
622 | .create = mem_cgroup_create, | |
623 | .destroy = mem_cgroup_destroy, | |
624 | .populate = mem_cgroup_populate, | |
67e465a7 | 625 | .attach = mem_cgroup_move_task, |
78fb7466 | 626 | .early_init = 1, |
8cdea7c0 | 627 | }; |