memcontrol.c 17 KB

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