memcontrol.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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/smp.h>
  24. #include <linux/page-flags.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/bit_spinlock.h>
  27. #include <linux/rcupdate.h>
  28. #include <linux/swap.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/fs.h>
  31. #include <linux/seq_file.h>
  32. #include <asm/uaccess.h>
  33. struct cgroup_subsys mem_cgroup_subsys;
  34. static const int MEM_CGROUP_RECLAIM_RETRIES = 5;
  35. /*
  36. * Statistics for memory cgroup.
  37. */
  38. enum mem_cgroup_stat_index {
  39. /*
  40. * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
  41. */
  42. MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
  43. MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
  44. MEM_CGROUP_STAT_NSTATS,
  45. };
  46. struct mem_cgroup_stat_cpu {
  47. s64 count[MEM_CGROUP_STAT_NSTATS];
  48. } ____cacheline_aligned_in_smp;
  49. struct mem_cgroup_stat {
  50. struct mem_cgroup_stat_cpu cpustat[NR_CPUS];
  51. };
  52. /*
  53. * For accounting under irq disable, no need for increment preempt count.
  54. */
  55. static void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat *stat,
  56. enum mem_cgroup_stat_index idx, int val)
  57. {
  58. int cpu = smp_processor_id();
  59. stat->cpustat[cpu].count[idx] += val;
  60. }
  61. static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
  62. enum mem_cgroup_stat_index idx)
  63. {
  64. int cpu;
  65. s64 ret = 0;
  66. for_each_possible_cpu(cpu)
  67. ret += stat->cpustat[cpu].count[idx];
  68. return ret;
  69. }
  70. /*
  71. * The memory controller data structure. The memory controller controls both
  72. * page cache and RSS per cgroup. We would eventually like to provide
  73. * statistics based on the statistics developed by Rik Van Riel for clock-pro,
  74. * to help the administrator determine what knobs to tune.
  75. *
  76. * TODO: Add a water mark for the memory controller. Reclaim will begin when
  77. * we hit the water mark. May be even add a low water mark, such that
  78. * no reclaim occurs from a cgroup at it's low water mark, this is
  79. * a feature that will be implemented much later in the future.
  80. */
  81. struct mem_cgroup {
  82. struct cgroup_subsys_state css;
  83. /*
  84. * the counter to account for memory usage
  85. */
  86. struct res_counter res;
  87. /*
  88. * Per cgroup active and inactive list, similar to the
  89. * per zone LRU lists.
  90. * TODO: Consider making these lists per zone
  91. */
  92. struct list_head active_list;
  93. struct list_head inactive_list;
  94. /*
  95. * spin_lock to protect the per cgroup LRU
  96. */
  97. spinlock_t lru_lock;
  98. unsigned long control_type; /* control RSS or RSS+Pagecache */
  99. /*
  100. * statistics.
  101. */
  102. struct mem_cgroup_stat stat;
  103. };
  104. /*
  105. * We use the lower bit of the page->page_cgroup pointer as a bit spin
  106. * lock. We need to ensure that page->page_cgroup is atleast two
  107. * byte aligned (based on comments from Nick Piggin)
  108. */
  109. #define PAGE_CGROUP_LOCK_BIT 0x0
  110. #define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT)
  111. /*
  112. * A page_cgroup page is associated with every page descriptor. The
  113. * page_cgroup helps us identify information about the cgroup
  114. */
  115. struct page_cgroup {
  116. struct list_head lru; /* per cgroup LRU list */
  117. struct page *page;
  118. struct mem_cgroup *mem_cgroup;
  119. atomic_t ref_cnt; /* Helpful when pages move b/w */
  120. /* mapped and cached states */
  121. int flags;
  122. };
  123. #define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */
  124. #define PAGE_CGROUP_FLAG_ACTIVE (0x2) /* page is active in this cgroup */
  125. enum {
  126. MEM_CGROUP_TYPE_UNSPEC = 0,
  127. MEM_CGROUP_TYPE_MAPPED,
  128. MEM_CGROUP_TYPE_CACHED,
  129. MEM_CGROUP_TYPE_ALL,
  130. MEM_CGROUP_TYPE_MAX,
  131. };
  132. enum charge_type {
  133. MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
  134. MEM_CGROUP_CHARGE_TYPE_MAPPED,
  135. };
  136. /*
  137. * Always modified under lru lock. Then, not necessary to preempt_disable()
  138. */
  139. static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, int flags,
  140. bool charge)
  141. {
  142. int val = (charge)? 1 : -1;
  143. struct mem_cgroup_stat *stat = &mem->stat;
  144. VM_BUG_ON(!irqs_disabled());
  145. if (flags & PAGE_CGROUP_FLAG_CACHE)
  146. __mem_cgroup_stat_add_safe(stat,
  147. MEM_CGROUP_STAT_CACHE, val);
  148. else
  149. __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_RSS, val);
  150. }
  151. static struct mem_cgroup init_mem_cgroup;
  152. static inline
  153. struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
  154. {
  155. return container_of(cgroup_subsys_state(cont,
  156. mem_cgroup_subsys_id), struct mem_cgroup,
  157. css);
  158. }
  159. static inline
  160. struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
  161. {
  162. return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
  163. struct mem_cgroup, css);
  164. }
  165. void mm_init_cgroup(struct mm_struct *mm, struct task_struct *p)
  166. {
  167. struct mem_cgroup *mem;
  168. mem = mem_cgroup_from_task(p);
  169. css_get(&mem->css);
  170. mm->mem_cgroup = mem;
  171. }
  172. void mm_free_cgroup(struct mm_struct *mm)
  173. {
  174. css_put(&mm->mem_cgroup->css);
  175. }
  176. static inline int page_cgroup_locked(struct page *page)
  177. {
  178. return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT,
  179. &page->page_cgroup);
  180. }
  181. void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
  182. {
  183. int locked;
  184. /*
  185. * While resetting the page_cgroup we might not hold the
  186. * page_cgroup lock. free_hot_cold_page() is an example
  187. * of such a scenario
  188. */
  189. if (pc)
  190. VM_BUG_ON(!page_cgroup_locked(page));
  191. locked = (page->page_cgroup & PAGE_CGROUP_LOCK);
  192. page->page_cgroup = ((unsigned long)pc | locked);
  193. }
  194. struct page_cgroup *page_get_page_cgroup(struct page *page)
  195. {
  196. return (struct page_cgroup *)
  197. (page->page_cgroup & ~PAGE_CGROUP_LOCK);
  198. }
  199. static void __always_inline lock_page_cgroup(struct page *page)
  200. {
  201. bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  202. VM_BUG_ON(!page_cgroup_locked(page));
  203. }
  204. static void __always_inline unlock_page_cgroup(struct page *page)
  205. {
  206. bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  207. }
  208. /*
  209. * Tie new page_cgroup to struct page under lock_page_cgroup()
  210. * This can fail if the page has been tied to a page_cgroup.
  211. * If success, returns 0.
  212. */
  213. static int page_cgroup_assign_new_page_cgroup(struct page *page,
  214. struct page_cgroup *pc)
  215. {
  216. int ret = 0;
  217. lock_page_cgroup(page);
  218. if (!page_get_page_cgroup(page))
  219. page_assign_page_cgroup(page, pc);
  220. else /* A page is tied to other pc. */
  221. ret = 1;
  222. unlock_page_cgroup(page);
  223. return ret;
  224. }
  225. /*
  226. * Clear page->page_cgroup member under lock_page_cgroup().
  227. * If given "pc" value is different from one page->page_cgroup,
  228. * page->cgroup is not cleared.
  229. * Returns a value of page->page_cgroup at lock taken.
  230. * A can can detect failure of clearing by following
  231. * clear_page_cgroup(page, pc) == pc
  232. */
  233. static struct page_cgroup *clear_page_cgroup(struct page *page,
  234. struct page_cgroup *pc)
  235. {
  236. struct page_cgroup *ret;
  237. /* lock and clear */
  238. lock_page_cgroup(page);
  239. ret = page_get_page_cgroup(page);
  240. if (likely(ret == pc))
  241. page_assign_page_cgroup(page, NULL);
  242. unlock_page_cgroup(page);
  243. return ret;
  244. }
  245. static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
  246. {
  247. if (active) {
  248. pc->flags |= PAGE_CGROUP_FLAG_ACTIVE;
  249. list_move(&pc->lru, &pc->mem_cgroup->active_list);
  250. } else {
  251. pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE;
  252. list_move(&pc->lru, &pc->mem_cgroup->inactive_list);
  253. }
  254. }
  255. int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
  256. {
  257. int ret;
  258. task_lock(task);
  259. ret = task->mm && mm_cgroup(task->mm) == mem;
  260. task_unlock(task);
  261. return ret;
  262. }
  263. /*
  264. * This routine assumes that the appropriate zone's lru lock is already held
  265. */
  266. void mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
  267. {
  268. struct mem_cgroup *mem;
  269. if (!pc)
  270. return;
  271. mem = pc->mem_cgroup;
  272. spin_lock(&mem->lru_lock);
  273. __mem_cgroup_move_lists(pc, active);
  274. spin_unlock(&mem->lru_lock);
  275. }
  276. unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
  277. struct list_head *dst,
  278. unsigned long *scanned, int order,
  279. int mode, struct zone *z,
  280. struct mem_cgroup *mem_cont,
  281. int active)
  282. {
  283. unsigned long nr_taken = 0;
  284. struct page *page;
  285. unsigned long scan;
  286. LIST_HEAD(pc_list);
  287. struct list_head *src;
  288. struct page_cgroup *pc, *tmp;
  289. if (active)
  290. src = &mem_cont->active_list;
  291. else
  292. src = &mem_cont->inactive_list;
  293. spin_lock(&mem_cont->lru_lock);
  294. scan = 0;
  295. list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
  296. if (scan >= nr_to_scan)
  297. break;
  298. page = pc->page;
  299. VM_BUG_ON(!pc);
  300. if (unlikely(!PageLRU(page)))
  301. continue;
  302. if (PageActive(page) && !active) {
  303. __mem_cgroup_move_lists(pc, true);
  304. continue;
  305. }
  306. if (!PageActive(page) && active) {
  307. __mem_cgroup_move_lists(pc, false);
  308. continue;
  309. }
  310. /*
  311. * Reclaim, per zone
  312. * TODO: make the active/inactive lists per zone
  313. */
  314. if (page_zone(page) != z)
  315. continue;
  316. scan++;
  317. list_move(&pc->lru, &pc_list);
  318. if (__isolate_lru_page(page, mode) == 0) {
  319. list_move(&page->lru, dst);
  320. nr_taken++;
  321. }
  322. }
  323. list_splice(&pc_list, src);
  324. spin_unlock(&mem_cont->lru_lock);
  325. *scanned = scan;
  326. return nr_taken;
  327. }
  328. /*
  329. * Charge the memory controller for page usage.
  330. * Return
  331. * 0 if the charge was successful
  332. * < 0 if the cgroup is over its limit
  333. */
  334. static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
  335. gfp_t gfp_mask, enum charge_type ctype)
  336. {
  337. struct mem_cgroup *mem;
  338. struct page_cgroup *pc;
  339. unsigned long flags;
  340. unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  341. /*
  342. * Should page_cgroup's go to their own slab?
  343. * One could optimize the performance of the charging routine
  344. * by saving a bit in the page_flags and using it as a lock
  345. * to see if the cgroup page already has a page_cgroup associated
  346. * with it
  347. */
  348. retry:
  349. if (page) {
  350. lock_page_cgroup(page);
  351. pc = page_get_page_cgroup(page);
  352. /*
  353. * The page_cgroup exists and
  354. * the page has already been accounted.
  355. */
  356. if (pc) {
  357. if (unlikely(!atomic_inc_not_zero(&pc->ref_cnt))) {
  358. /* this page is under being uncharged ? */
  359. unlock_page_cgroup(page);
  360. cpu_relax();
  361. goto retry;
  362. } else {
  363. unlock_page_cgroup(page);
  364. goto done;
  365. }
  366. }
  367. unlock_page_cgroup(page);
  368. }
  369. pc = kzalloc(sizeof(struct page_cgroup), gfp_mask);
  370. if (pc == NULL)
  371. goto err;
  372. /*
  373. * We always charge the cgroup the mm_struct belongs to.
  374. * The mm_struct's mem_cgroup changes on task migration if the
  375. * thread group leader migrates. It's possible that mm is not
  376. * set, if so charge the init_mm (happens for pagecache usage).
  377. */
  378. if (!mm)
  379. mm = &init_mm;
  380. rcu_read_lock();
  381. mem = rcu_dereference(mm->mem_cgroup);
  382. /*
  383. * For every charge from the cgroup, increment reference
  384. * count
  385. */
  386. css_get(&mem->css);
  387. rcu_read_unlock();
  388. /*
  389. * If we created the page_cgroup, we should free it on exceeding
  390. * the cgroup limit.
  391. */
  392. while (res_counter_charge(&mem->res, PAGE_SIZE)) {
  393. if (!(gfp_mask & __GFP_WAIT))
  394. goto out;
  395. if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
  396. continue;
  397. /*
  398. * try_to_free_mem_cgroup_pages() might not give us a full
  399. * picture of reclaim. Some pages are reclaimed and might be
  400. * moved to swap cache or just unmapped from the cgroup.
  401. * Check the limit again to see if the reclaim reduced the
  402. * current usage of the cgroup before giving up
  403. */
  404. if (res_counter_check_under_limit(&mem->res))
  405. continue;
  406. if (!nr_retries--) {
  407. mem_cgroup_out_of_memory(mem, gfp_mask);
  408. goto out;
  409. }
  410. congestion_wait(WRITE, HZ/10);
  411. }
  412. atomic_set(&pc->ref_cnt, 1);
  413. pc->mem_cgroup = mem;
  414. pc->page = page;
  415. pc->flags = PAGE_CGROUP_FLAG_ACTIVE;
  416. if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE)
  417. pc->flags |= PAGE_CGROUP_FLAG_CACHE;
  418. if (!page || page_cgroup_assign_new_page_cgroup(page, pc)) {
  419. /*
  420. * Another charge has been added to this page already.
  421. * We take lock_page_cgroup(page) again and read
  422. * page->cgroup, increment refcnt.... just retry is OK.
  423. */
  424. res_counter_uncharge(&mem->res, PAGE_SIZE);
  425. css_put(&mem->css);
  426. kfree(pc);
  427. if (!page)
  428. goto done;
  429. goto retry;
  430. }
  431. spin_lock_irqsave(&mem->lru_lock, flags);
  432. /* Update statistics vector */
  433. mem_cgroup_charge_statistics(mem, pc->flags, true);
  434. list_add(&pc->lru, &mem->active_list);
  435. spin_unlock_irqrestore(&mem->lru_lock, flags);
  436. done:
  437. return 0;
  438. out:
  439. css_put(&mem->css);
  440. kfree(pc);
  441. err:
  442. return -ENOMEM;
  443. }
  444. int mem_cgroup_charge(struct page *page, struct mm_struct *mm,
  445. gfp_t gfp_mask)
  446. {
  447. return mem_cgroup_charge_common(page, mm, gfp_mask,
  448. MEM_CGROUP_CHARGE_TYPE_MAPPED);
  449. }
  450. /*
  451. * See if the cached pages should be charged at all?
  452. */
  453. int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
  454. gfp_t gfp_mask)
  455. {
  456. int ret = 0;
  457. struct mem_cgroup *mem;
  458. if (!mm)
  459. mm = &init_mm;
  460. rcu_read_lock();
  461. mem = rcu_dereference(mm->mem_cgroup);
  462. css_get(&mem->css);
  463. rcu_read_unlock();
  464. if (mem->control_type == MEM_CGROUP_TYPE_ALL)
  465. ret = mem_cgroup_charge_common(page, mm, gfp_mask,
  466. MEM_CGROUP_CHARGE_TYPE_CACHE);
  467. css_put(&mem->css);
  468. return ret;
  469. }
  470. /*
  471. * Uncharging is always a welcome operation, we never complain, simply
  472. * uncharge.
  473. */
  474. void mem_cgroup_uncharge(struct page_cgroup *pc)
  475. {
  476. struct mem_cgroup *mem;
  477. struct page *page;
  478. unsigned long flags;
  479. /*
  480. * This can handle cases when a page is not charged at all and we
  481. * are switching between handling the control_type.
  482. */
  483. if (!pc)
  484. return;
  485. if (atomic_dec_and_test(&pc->ref_cnt)) {
  486. page = pc->page;
  487. /*
  488. * get page->cgroup and clear it under lock.
  489. * force_empty can drop page->cgroup without checking refcnt.
  490. */
  491. if (clear_page_cgroup(page, pc) == pc) {
  492. mem = pc->mem_cgroup;
  493. css_put(&mem->css);
  494. res_counter_uncharge(&mem->res, PAGE_SIZE);
  495. spin_lock_irqsave(&mem->lru_lock, flags);
  496. list_del_init(&pc->lru);
  497. mem_cgroup_charge_statistics(mem, pc->flags, false);
  498. spin_unlock_irqrestore(&mem->lru_lock, flags);
  499. kfree(pc);
  500. }
  501. }
  502. }
  503. /*
  504. * Returns non-zero if a page (under migration) has valid page_cgroup member.
  505. * Refcnt of page_cgroup is incremented.
  506. */
  507. int mem_cgroup_prepare_migration(struct page *page)
  508. {
  509. struct page_cgroup *pc;
  510. int ret = 0;
  511. lock_page_cgroup(page);
  512. pc = page_get_page_cgroup(page);
  513. if (pc && atomic_inc_not_zero(&pc->ref_cnt))
  514. ret = 1;
  515. unlock_page_cgroup(page);
  516. return ret;
  517. }
  518. void mem_cgroup_end_migration(struct page *page)
  519. {
  520. struct page_cgroup *pc = page_get_page_cgroup(page);
  521. mem_cgroup_uncharge(pc);
  522. }
  523. /*
  524. * We know both *page* and *newpage* are now not-on-LRU and Pg_locked.
  525. * And no race with uncharge() routines because page_cgroup for *page*
  526. * has extra one reference by mem_cgroup_prepare_migration.
  527. */
  528. void mem_cgroup_page_migration(struct page *page, struct page *newpage)
  529. {
  530. struct page_cgroup *pc;
  531. retry:
  532. pc = page_get_page_cgroup(page);
  533. if (!pc)
  534. return;
  535. if (clear_page_cgroup(page, pc) != pc)
  536. goto retry;
  537. pc->page = newpage;
  538. lock_page_cgroup(newpage);
  539. page_assign_page_cgroup(newpage, pc);
  540. unlock_page_cgroup(newpage);
  541. return;
  542. }
  543. /*
  544. * This routine traverse page_cgroup in given list and drop them all.
  545. * This routine ignores page_cgroup->ref_cnt.
  546. * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
  547. */
  548. #define FORCE_UNCHARGE_BATCH (128)
  549. static void
  550. mem_cgroup_force_empty_list(struct mem_cgroup *mem, struct list_head *list)
  551. {
  552. struct page_cgroup *pc;
  553. struct page *page;
  554. int count;
  555. unsigned long flags;
  556. retry:
  557. count = FORCE_UNCHARGE_BATCH;
  558. spin_lock_irqsave(&mem->lru_lock, flags);
  559. while (--count && !list_empty(list)) {
  560. pc = list_entry(list->prev, struct page_cgroup, lru);
  561. page = pc->page;
  562. /* Avoid race with charge */
  563. atomic_set(&pc->ref_cnt, 0);
  564. if (clear_page_cgroup(page, pc) == pc) {
  565. css_put(&mem->css);
  566. res_counter_uncharge(&mem->res, PAGE_SIZE);
  567. list_del_init(&pc->lru);
  568. mem_cgroup_charge_statistics(mem, pc->flags, false);
  569. kfree(pc);
  570. } else /* being uncharged ? ...do relax */
  571. break;
  572. }
  573. spin_unlock_irqrestore(&mem->lru_lock, flags);
  574. if (!list_empty(list)) {
  575. cond_resched();
  576. goto retry;
  577. }
  578. return;
  579. }
  580. /*
  581. * make mem_cgroup's charge to be 0 if there is no task.
  582. * This enables deleting this mem_cgroup.
  583. */
  584. int mem_cgroup_force_empty(struct mem_cgroup *mem)
  585. {
  586. int ret = -EBUSY;
  587. css_get(&mem->css);
  588. /*
  589. * page reclaim code (kswapd etc..) will move pages between
  590. ` * active_list <-> inactive_list while we don't take a lock.
  591. * So, we have to do loop here until all lists are empty.
  592. */
  593. while (!(list_empty(&mem->active_list) &&
  594. list_empty(&mem->inactive_list))) {
  595. if (atomic_read(&mem->css.cgroup->count) > 0)
  596. goto out;
  597. /* drop all page_cgroup in active_list */
  598. mem_cgroup_force_empty_list(mem, &mem->active_list);
  599. /* drop all page_cgroup in inactive_list */
  600. mem_cgroup_force_empty_list(mem, &mem->inactive_list);
  601. }
  602. ret = 0;
  603. out:
  604. css_put(&mem->css);
  605. return ret;
  606. }
  607. int mem_cgroup_write_strategy(char *buf, unsigned long long *tmp)
  608. {
  609. *tmp = memparse(buf, &buf);
  610. if (*buf != '\0')
  611. return -EINVAL;
  612. /*
  613. * Round up the value to the closest page size
  614. */
  615. *tmp = ((*tmp + PAGE_SIZE - 1) >> PAGE_SHIFT) << PAGE_SHIFT;
  616. return 0;
  617. }
  618. static ssize_t mem_cgroup_read(struct cgroup *cont,
  619. struct cftype *cft, struct file *file,
  620. char __user *userbuf, size_t nbytes, loff_t *ppos)
  621. {
  622. return res_counter_read(&mem_cgroup_from_cont(cont)->res,
  623. cft->private, userbuf, nbytes, ppos,
  624. NULL);
  625. }
  626. static ssize_t mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
  627. struct file *file, const char __user *userbuf,
  628. size_t nbytes, loff_t *ppos)
  629. {
  630. return res_counter_write(&mem_cgroup_from_cont(cont)->res,
  631. cft->private, userbuf, nbytes, ppos,
  632. mem_cgroup_write_strategy);
  633. }
  634. static ssize_t mem_control_type_write(struct cgroup *cont,
  635. struct cftype *cft, struct file *file,
  636. const char __user *userbuf,
  637. size_t nbytes, loff_t *pos)
  638. {
  639. int ret;
  640. char *buf, *end;
  641. unsigned long tmp;
  642. struct mem_cgroup *mem;
  643. mem = mem_cgroup_from_cont(cont);
  644. buf = kmalloc(nbytes + 1, GFP_KERNEL);
  645. ret = -ENOMEM;
  646. if (buf == NULL)
  647. goto out;
  648. buf[nbytes] = 0;
  649. ret = -EFAULT;
  650. if (copy_from_user(buf, userbuf, nbytes))
  651. goto out_free;
  652. ret = -EINVAL;
  653. tmp = simple_strtoul(buf, &end, 10);
  654. if (*end != '\0')
  655. goto out_free;
  656. if (tmp <= MEM_CGROUP_TYPE_UNSPEC || tmp >= MEM_CGROUP_TYPE_MAX)
  657. goto out_free;
  658. mem->control_type = tmp;
  659. ret = nbytes;
  660. out_free:
  661. kfree(buf);
  662. out:
  663. return ret;
  664. }
  665. static ssize_t mem_control_type_read(struct cgroup *cont,
  666. struct cftype *cft,
  667. struct file *file, char __user *userbuf,
  668. size_t nbytes, loff_t *ppos)
  669. {
  670. unsigned long val;
  671. char buf[64], *s;
  672. struct mem_cgroup *mem;
  673. mem = mem_cgroup_from_cont(cont);
  674. s = buf;
  675. val = mem->control_type;
  676. s += sprintf(s, "%lu\n", val);
  677. return simple_read_from_buffer((void __user *)userbuf, nbytes,
  678. ppos, buf, s - buf);
  679. }
  680. static ssize_t mem_force_empty_write(struct cgroup *cont,
  681. struct cftype *cft, struct file *file,
  682. const char __user *userbuf,
  683. size_t nbytes, loff_t *ppos)
  684. {
  685. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  686. int ret;
  687. ret = mem_cgroup_force_empty(mem);
  688. if (!ret)
  689. ret = nbytes;
  690. return ret;
  691. }
  692. /*
  693. * Note: This should be removed if cgroup supports write-only file.
  694. */
  695. static ssize_t mem_force_empty_read(struct cgroup *cont,
  696. struct cftype *cft,
  697. struct file *file, char __user *userbuf,
  698. size_t nbytes, loff_t *ppos)
  699. {
  700. return -EINVAL;
  701. }
  702. static const struct mem_cgroup_stat_desc {
  703. const char *msg;
  704. u64 unit;
  705. } mem_cgroup_stat_desc[] = {
  706. [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
  707. [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
  708. };
  709. static int mem_control_stat_show(struct seq_file *m, void *arg)
  710. {
  711. struct cgroup *cont = m->private;
  712. struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
  713. struct mem_cgroup_stat *stat = &mem_cont->stat;
  714. int i;
  715. for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
  716. s64 val;
  717. val = mem_cgroup_read_stat(stat, i);
  718. val *= mem_cgroup_stat_desc[i].unit;
  719. seq_printf(m, "%s %lld\n", mem_cgroup_stat_desc[i].msg,
  720. (long long)val);
  721. }
  722. return 0;
  723. }
  724. static const struct file_operations mem_control_stat_file_operations = {
  725. .read = seq_read,
  726. .llseek = seq_lseek,
  727. .release = single_release,
  728. };
  729. static int mem_control_stat_open(struct inode *unused, struct file *file)
  730. {
  731. /* XXX __d_cont */
  732. struct cgroup *cont = file->f_dentry->d_parent->d_fsdata;
  733. file->f_op = &mem_control_stat_file_operations;
  734. return single_open(file, mem_control_stat_show, cont);
  735. }
  736. static struct cftype mem_cgroup_files[] = {
  737. {
  738. .name = "usage_in_bytes",
  739. .private = RES_USAGE,
  740. .read = mem_cgroup_read,
  741. },
  742. {
  743. .name = "limit_in_bytes",
  744. .private = RES_LIMIT,
  745. .write = mem_cgroup_write,
  746. .read = mem_cgroup_read,
  747. },
  748. {
  749. .name = "failcnt",
  750. .private = RES_FAILCNT,
  751. .read = mem_cgroup_read,
  752. },
  753. {
  754. .name = "control_type",
  755. .write = mem_control_type_write,
  756. .read = mem_control_type_read,
  757. },
  758. {
  759. .name = "force_empty",
  760. .write = mem_force_empty_write,
  761. .read = mem_force_empty_read,
  762. },
  763. {
  764. .name = "stat",
  765. .open = mem_control_stat_open,
  766. },
  767. };
  768. static struct mem_cgroup init_mem_cgroup;
  769. static struct cgroup_subsys_state *
  770. mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
  771. {
  772. struct mem_cgroup *mem;
  773. if (unlikely((cont->parent) == NULL)) {
  774. mem = &init_mem_cgroup;
  775. init_mm.mem_cgroup = mem;
  776. } else
  777. mem = kzalloc(sizeof(struct mem_cgroup), GFP_KERNEL);
  778. if (mem == NULL)
  779. return NULL;
  780. res_counter_init(&mem->res);
  781. INIT_LIST_HEAD(&mem->active_list);
  782. INIT_LIST_HEAD(&mem->inactive_list);
  783. spin_lock_init(&mem->lru_lock);
  784. mem->control_type = MEM_CGROUP_TYPE_ALL;
  785. return &mem->css;
  786. }
  787. static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
  788. struct cgroup *cont)
  789. {
  790. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  791. mem_cgroup_force_empty(mem);
  792. }
  793. static void mem_cgroup_destroy(struct cgroup_subsys *ss,
  794. struct cgroup *cont)
  795. {
  796. kfree(mem_cgroup_from_cont(cont));
  797. }
  798. static int mem_cgroup_populate(struct cgroup_subsys *ss,
  799. struct cgroup *cont)
  800. {
  801. return cgroup_add_files(cont, ss, mem_cgroup_files,
  802. ARRAY_SIZE(mem_cgroup_files));
  803. }
  804. static void mem_cgroup_move_task(struct cgroup_subsys *ss,
  805. struct cgroup *cont,
  806. struct cgroup *old_cont,
  807. struct task_struct *p)
  808. {
  809. struct mm_struct *mm;
  810. struct mem_cgroup *mem, *old_mem;
  811. mm = get_task_mm(p);
  812. if (mm == NULL)
  813. return;
  814. mem = mem_cgroup_from_cont(cont);
  815. old_mem = mem_cgroup_from_cont(old_cont);
  816. if (mem == old_mem)
  817. goto out;
  818. /*
  819. * Only thread group leaders are allowed to migrate, the mm_struct is
  820. * in effect owned by the leader
  821. */
  822. if (p->tgid != p->pid)
  823. goto out;
  824. css_get(&mem->css);
  825. rcu_assign_pointer(mm->mem_cgroup, mem);
  826. css_put(&old_mem->css);
  827. out:
  828. mmput(mm);
  829. return;
  830. }
  831. struct cgroup_subsys mem_cgroup_subsys = {
  832. .name = "memory",
  833. .subsys_id = mem_cgroup_subsys_id,
  834. .create = mem_cgroup_create,
  835. .pre_destroy = mem_cgroup_pre_destroy,
  836. .destroy = mem_cgroup_destroy,
  837. .populate = mem_cgroup_populate,
  838. .attach = mem_cgroup_move_task,
  839. .early_init = 1,
  840. };