memcontrol.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. continue;
  236. if (PageActive(page) && !active) {
  237. __mem_cgroup_move_lists(pc, true);
  238. continue;
  239. }
  240. if (!PageActive(page) && active) {
  241. __mem_cgroup_move_lists(pc, false);
  242. continue;
  243. }
  244. /*
  245. * Reclaim, per zone
  246. * TODO: make the active/inactive lists per zone
  247. */
  248. if (page_zone(page) != z)
  249. continue;
  250. scan++;
  251. list_move(&pc->lru, &pc_list);
  252. if (__isolate_lru_page(page, mode) == 0) {
  253. list_move(&page->lru, dst);
  254. nr_taken++;
  255. }
  256. }
  257. list_splice(&pc_list, src);
  258. spin_unlock(&mem_cont->lru_lock);
  259. *scanned = scan;
  260. return nr_taken;
  261. }
  262. /*
  263. * Charge the memory controller for page usage.
  264. * Return
  265. * 0 if the charge was successful
  266. * < 0 if the cgroup is over its limit
  267. */
  268. int mem_cgroup_charge(struct page *page, struct mm_struct *mm,
  269. gfp_t gfp_mask)
  270. {
  271. struct mem_cgroup *mem;
  272. struct page_cgroup *pc;
  273. unsigned long flags;
  274. unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  275. /*
  276. * Should page_cgroup's go to their own slab?
  277. * One could optimize the performance of the charging routine
  278. * by saving a bit in the page_flags and using it as a lock
  279. * to see if the cgroup page already has a page_cgroup associated
  280. * with it
  281. */
  282. retry:
  283. lock_page_cgroup(page);
  284. pc = page_get_page_cgroup(page);
  285. /*
  286. * The page_cgroup exists and the page has already been accounted
  287. */
  288. if (pc) {
  289. if (unlikely(!atomic_inc_not_zero(&pc->ref_cnt))) {
  290. /* this page is under being uncharged ? */
  291. unlock_page_cgroup(page);
  292. cpu_relax();
  293. goto retry;
  294. } else {
  295. unlock_page_cgroup(page);
  296. goto done;
  297. }
  298. }
  299. unlock_page_cgroup(page);
  300. pc = kzalloc(sizeof(struct page_cgroup), gfp_mask);
  301. if (pc == NULL)
  302. goto err;
  303. rcu_read_lock();
  304. /*
  305. * We always charge the cgroup the mm_struct belongs to
  306. * the mm_struct's mem_cgroup changes on task migration if the
  307. * thread group leader migrates. It's possible that mm is not
  308. * set, if so charge the init_mm (happens for pagecache usage).
  309. */
  310. if (!mm)
  311. mm = &init_mm;
  312. mem = rcu_dereference(mm->mem_cgroup);
  313. /*
  314. * For every charge from the cgroup, increment reference
  315. * count
  316. */
  317. css_get(&mem->css);
  318. rcu_read_unlock();
  319. /*
  320. * If we created the page_cgroup, we should free it on exceeding
  321. * the cgroup limit.
  322. */
  323. while (res_counter_charge(&mem->res, PAGE_SIZE)) {
  324. bool is_atomic = gfp_mask & GFP_ATOMIC;
  325. /*
  326. * We cannot reclaim under GFP_ATOMIC, fail the charge
  327. */
  328. if (is_atomic)
  329. goto noreclaim;
  330. if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
  331. continue;
  332. /*
  333. * try_to_free_mem_cgroup_pages() might not give us a full
  334. * picture of reclaim. Some pages are reclaimed and might be
  335. * moved to swap cache or just unmapped from the cgroup.
  336. * Check the limit again to see if the reclaim reduced the
  337. * current usage of the cgroup before giving up
  338. */
  339. if (res_counter_check_under_limit(&mem->res))
  340. continue;
  341. /*
  342. * Since we control both RSS and cache, we end up with a
  343. * very interesting scenario where we end up reclaiming
  344. * memory (essentially RSS), since the memory is pushed
  345. * to swap cache, we eventually end up adding those
  346. * pages back to our list. Hence we give ourselves a
  347. * few chances before we fail
  348. */
  349. else if (nr_retries--) {
  350. congestion_wait(WRITE, HZ/10);
  351. continue;
  352. }
  353. noreclaim:
  354. css_put(&mem->css);
  355. if (!is_atomic)
  356. mem_cgroup_out_of_memory(mem, GFP_KERNEL);
  357. goto free_pc;
  358. }
  359. atomic_set(&pc->ref_cnt, 1);
  360. pc->mem_cgroup = mem;
  361. pc->page = page;
  362. if (page_cgroup_assign_new_page_cgroup(page, pc)) {
  363. /*
  364. * an another charge is added to this page already.
  365. * we do take lock_page_cgroup(page) again and read
  366. * page->cgroup, increment refcnt.... just retry is OK.
  367. */
  368. res_counter_uncharge(&mem->res, PAGE_SIZE);
  369. css_put(&mem->css);
  370. kfree(pc);
  371. goto retry;
  372. }
  373. spin_lock_irqsave(&mem->lru_lock, flags);
  374. list_add(&pc->lru, &mem->active_list);
  375. spin_unlock_irqrestore(&mem->lru_lock, flags);
  376. done:
  377. return 0;
  378. free_pc:
  379. kfree(pc);
  380. err:
  381. return -ENOMEM;
  382. }
  383. /*
  384. * See if the cached pages should be charged at all?
  385. */
  386. int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
  387. gfp_t gfp_mask)
  388. {
  389. struct mem_cgroup *mem;
  390. if (!mm)
  391. mm = &init_mm;
  392. mem = rcu_dereference(mm->mem_cgroup);
  393. if (mem->control_type == MEM_CGROUP_TYPE_ALL)
  394. return mem_cgroup_charge(page, mm, gfp_mask);
  395. else
  396. return 0;
  397. }
  398. /*
  399. * Uncharging is always a welcome operation, we never complain, simply
  400. * uncharge.
  401. */
  402. void mem_cgroup_uncharge(struct page_cgroup *pc)
  403. {
  404. struct mem_cgroup *mem;
  405. struct page *page;
  406. unsigned long flags;
  407. /*
  408. * This can handle cases when a page is not charged at all and we
  409. * are switching between handling the control_type.
  410. */
  411. if (!pc)
  412. return;
  413. if (atomic_dec_and_test(&pc->ref_cnt)) {
  414. page = pc->page;
  415. /*
  416. * get page->cgroup and clear it under lock.
  417. */
  418. if (clear_page_cgroup(page, pc) == pc) {
  419. mem = pc->mem_cgroup;
  420. css_put(&mem->css);
  421. res_counter_uncharge(&mem->res, PAGE_SIZE);
  422. spin_lock_irqsave(&mem->lru_lock, flags);
  423. list_del_init(&pc->lru);
  424. spin_unlock_irqrestore(&mem->lru_lock, flags);
  425. kfree(pc);
  426. } else {
  427. /*
  428. * Note:This will be removed when force-empty patch is
  429. * applied. just show warning here.
  430. */
  431. printk(KERN_ERR "Race in mem_cgroup_uncharge() ?");
  432. dump_stack();
  433. }
  434. }
  435. }
  436. /*
  437. * Returns non-zero if a page (under migration) has valid page_cgroup member.
  438. * Refcnt of page_cgroup is incremented.
  439. */
  440. int mem_cgroup_prepare_migration(struct page *page)
  441. {
  442. struct page_cgroup *pc;
  443. int ret = 0;
  444. lock_page_cgroup(page);
  445. pc = page_get_page_cgroup(page);
  446. if (pc && atomic_inc_not_zero(&pc->ref_cnt))
  447. ret = 1;
  448. unlock_page_cgroup(page);
  449. return ret;
  450. }
  451. void mem_cgroup_end_migration(struct page *page)
  452. {
  453. struct page_cgroup *pc = page_get_page_cgroup(page);
  454. mem_cgroup_uncharge(pc);
  455. }
  456. /*
  457. * We know both *page* and *newpage* are now not-on-LRU and Pg_locked.
  458. * And no race with uncharge() routines because page_cgroup for *page*
  459. * has extra one reference by mem_cgroup_prepare_migration.
  460. */
  461. void mem_cgroup_page_migration(struct page *page, struct page *newpage)
  462. {
  463. struct page_cgroup *pc;
  464. retry:
  465. pc = page_get_page_cgroup(page);
  466. if (!pc)
  467. return;
  468. if (clear_page_cgroup(page, pc) != pc)
  469. goto retry;
  470. pc->page = newpage;
  471. lock_page_cgroup(newpage);
  472. page_assign_page_cgroup(newpage, pc);
  473. unlock_page_cgroup(newpage);
  474. return;
  475. }
  476. int mem_cgroup_write_strategy(char *buf, unsigned long long *tmp)
  477. {
  478. *tmp = memparse(buf, &buf);
  479. if (*buf != '\0')
  480. return -EINVAL;
  481. /*
  482. * Round up the value to the closest page size
  483. */
  484. *tmp = ((*tmp + PAGE_SIZE - 1) >> PAGE_SHIFT) << PAGE_SHIFT;
  485. return 0;
  486. }
  487. static ssize_t mem_cgroup_read(struct cgroup *cont,
  488. struct cftype *cft, struct file *file,
  489. char __user *userbuf, size_t nbytes, loff_t *ppos)
  490. {
  491. return res_counter_read(&mem_cgroup_from_cont(cont)->res,
  492. cft->private, userbuf, nbytes, ppos,
  493. NULL);
  494. }
  495. static ssize_t mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
  496. struct file *file, const char __user *userbuf,
  497. size_t nbytes, loff_t *ppos)
  498. {
  499. return res_counter_write(&mem_cgroup_from_cont(cont)->res,
  500. cft->private, userbuf, nbytes, ppos,
  501. mem_cgroup_write_strategy);
  502. }
  503. static ssize_t mem_control_type_write(struct cgroup *cont,
  504. struct cftype *cft, struct file *file,
  505. const char __user *userbuf,
  506. size_t nbytes, loff_t *pos)
  507. {
  508. int ret;
  509. char *buf, *end;
  510. unsigned long tmp;
  511. struct mem_cgroup *mem;
  512. mem = mem_cgroup_from_cont(cont);
  513. buf = kmalloc(nbytes + 1, GFP_KERNEL);
  514. ret = -ENOMEM;
  515. if (buf == NULL)
  516. goto out;
  517. buf[nbytes] = 0;
  518. ret = -EFAULT;
  519. if (copy_from_user(buf, userbuf, nbytes))
  520. goto out_free;
  521. ret = -EINVAL;
  522. tmp = simple_strtoul(buf, &end, 10);
  523. if (*end != '\0')
  524. goto out_free;
  525. if (tmp <= MEM_CGROUP_TYPE_UNSPEC || tmp >= MEM_CGROUP_TYPE_MAX)
  526. goto out_free;
  527. mem->control_type = tmp;
  528. ret = nbytes;
  529. out_free:
  530. kfree(buf);
  531. out:
  532. return ret;
  533. }
  534. static ssize_t mem_control_type_read(struct cgroup *cont,
  535. struct cftype *cft,
  536. struct file *file, char __user *userbuf,
  537. size_t nbytes, loff_t *ppos)
  538. {
  539. unsigned long val;
  540. char buf[64], *s;
  541. struct mem_cgroup *mem;
  542. mem = mem_cgroup_from_cont(cont);
  543. s = buf;
  544. val = mem->control_type;
  545. s += sprintf(s, "%lu\n", val);
  546. return simple_read_from_buffer((void __user *)userbuf, nbytes,
  547. ppos, buf, s - buf);
  548. }
  549. static struct cftype mem_cgroup_files[] = {
  550. {
  551. .name = "usage_in_bytes",
  552. .private = RES_USAGE,
  553. .read = mem_cgroup_read,
  554. },
  555. {
  556. .name = "limit_in_bytes",
  557. .private = RES_LIMIT,
  558. .write = mem_cgroup_write,
  559. .read = mem_cgroup_read,
  560. },
  561. {
  562. .name = "failcnt",
  563. .private = RES_FAILCNT,
  564. .read = mem_cgroup_read,
  565. },
  566. {
  567. .name = "control_type",
  568. .write = mem_control_type_write,
  569. .read = mem_control_type_read,
  570. },
  571. };
  572. static struct mem_cgroup init_mem_cgroup;
  573. static struct cgroup_subsys_state *
  574. mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
  575. {
  576. struct mem_cgroup *mem;
  577. if (unlikely((cont->parent) == NULL)) {
  578. mem = &init_mem_cgroup;
  579. init_mm.mem_cgroup = mem;
  580. } else
  581. mem = kzalloc(sizeof(struct mem_cgroup), GFP_KERNEL);
  582. if (mem == NULL)
  583. return NULL;
  584. res_counter_init(&mem->res);
  585. INIT_LIST_HEAD(&mem->active_list);
  586. INIT_LIST_HEAD(&mem->inactive_list);
  587. spin_lock_init(&mem->lru_lock);
  588. mem->control_type = MEM_CGROUP_TYPE_ALL;
  589. return &mem->css;
  590. }
  591. static void mem_cgroup_destroy(struct cgroup_subsys *ss,
  592. struct cgroup *cont)
  593. {
  594. kfree(mem_cgroup_from_cont(cont));
  595. }
  596. static int mem_cgroup_populate(struct cgroup_subsys *ss,
  597. struct cgroup *cont)
  598. {
  599. return cgroup_add_files(cont, ss, mem_cgroup_files,
  600. ARRAY_SIZE(mem_cgroup_files));
  601. }
  602. static void mem_cgroup_move_task(struct cgroup_subsys *ss,
  603. struct cgroup *cont,
  604. struct cgroup *old_cont,
  605. struct task_struct *p)
  606. {
  607. struct mm_struct *mm;
  608. struct mem_cgroup *mem, *old_mem;
  609. mm = get_task_mm(p);
  610. if (mm == NULL)
  611. return;
  612. mem = mem_cgroup_from_cont(cont);
  613. old_mem = mem_cgroup_from_cont(old_cont);
  614. if (mem == old_mem)
  615. goto out;
  616. /*
  617. * Only thread group leaders are allowed to migrate, the mm_struct is
  618. * in effect owned by the leader
  619. */
  620. if (p->tgid != p->pid)
  621. goto out;
  622. css_get(&mem->css);
  623. rcu_assign_pointer(mm->mem_cgroup, mem);
  624. css_put(&old_mem->css);
  625. out:
  626. mmput(mm);
  627. return;
  628. }
  629. struct cgroup_subsys mem_cgroup_subsys = {
  630. .name = "memory",
  631. .subsys_id = mem_cgroup_subsys_id,
  632. .create = mem_cgroup_create,
  633. .destroy = mem_cgroup_destroy,
  634. .populate = mem_cgroup_populate,
  635. .attach = mem_cgroup_move_task,
  636. .early_init = 1,
  637. };