memcontrol.c 28 KB

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