memcontrol.c 27 KB

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