memcontrol.c 27 KB

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