memcontrol.c 28 KB

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