memcontrol.c 29 KB

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