memcontrol.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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/slab.h>
  29. #include <linux/swap.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/fs.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/vmalloc.h>
  34. #include <asm/uaccess.h>
  35. struct cgroup_subsys mem_cgroup_subsys __read_mostly;
  36. static struct kmem_cache *page_cgroup_cache __read_mostly;
  37. #define MEM_CGROUP_RECLAIM_RETRIES 5
  38. /*
  39. * Statistics for memory cgroup.
  40. */
  41. enum mem_cgroup_stat_index {
  42. /*
  43. * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
  44. */
  45. MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
  46. MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
  47. MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
  48. MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
  49. MEM_CGROUP_STAT_NSTATS,
  50. };
  51. struct mem_cgroup_stat_cpu {
  52. s64 count[MEM_CGROUP_STAT_NSTATS];
  53. } ____cacheline_aligned_in_smp;
  54. struct mem_cgroup_stat {
  55. struct mem_cgroup_stat_cpu cpustat[NR_CPUS];
  56. };
  57. /*
  58. * For accounting under irq disable, no need for increment preempt count.
  59. */
  60. static void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat *stat,
  61. enum mem_cgroup_stat_index idx, int val)
  62. {
  63. int cpu = smp_processor_id();
  64. stat->cpustat[cpu].count[idx] += val;
  65. }
  66. static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
  67. enum mem_cgroup_stat_index idx)
  68. {
  69. int cpu;
  70. s64 ret = 0;
  71. for_each_possible_cpu(cpu)
  72. ret += stat->cpustat[cpu].count[idx];
  73. return ret;
  74. }
  75. /*
  76. * per-zone information in memory controller.
  77. */
  78. enum mem_cgroup_zstat_index {
  79. MEM_CGROUP_ZSTAT_ACTIVE,
  80. MEM_CGROUP_ZSTAT_INACTIVE,
  81. NR_MEM_CGROUP_ZSTAT,
  82. };
  83. struct mem_cgroup_per_zone {
  84. /*
  85. * spin_lock to protect the per cgroup LRU
  86. */
  87. spinlock_t lru_lock;
  88. struct list_head active_list;
  89. struct list_head inactive_list;
  90. unsigned long count[NR_MEM_CGROUP_ZSTAT];
  91. };
  92. /* Macro for accessing counter */
  93. #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
  94. struct mem_cgroup_per_node {
  95. struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
  96. };
  97. struct mem_cgroup_lru_info {
  98. struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
  99. };
  100. /*
  101. * The memory controller data structure. The memory controller controls both
  102. * page cache and RSS per cgroup. We would eventually like to provide
  103. * statistics based on the statistics developed by Rik Van Riel for clock-pro,
  104. * to help the administrator determine what knobs to tune.
  105. *
  106. * TODO: Add a water mark for the memory controller. Reclaim will begin when
  107. * we hit the water mark. May be even add a low water mark, such that
  108. * no reclaim occurs from a cgroup at it's low water mark, this is
  109. * a feature that will be implemented much later in the future.
  110. */
  111. struct mem_cgroup {
  112. struct cgroup_subsys_state css;
  113. /*
  114. * the counter to account for memory usage
  115. */
  116. struct res_counter res;
  117. /*
  118. * Per cgroup active and inactive list, similar to the
  119. * per zone LRU lists.
  120. */
  121. struct mem_cgroup_lru_info info;
  122. int prev_priority; /* for recording reclaim priority */
  123. /*
  124. * statistics.
  125. */
  126. struct mem_cgroup_stat stat;
  127. };
  128. static struct mem_cgroup init_mem_cgroup;
  129. /*
  130. * We use the lower bit of the page->page_cgroup pointer as a bit spin
  131. * lock. We need to ensure that page->page_cgroup is at least two
  132. * byte aligned (based on comments from Nick Piggin). But since
  133. * bit_spin_lock doesn't actually set that lock bit in a non-debug
  134. * uniprocessor kernel, we should avoid setting it here too.
  135. */
  136. #define PAGE_CGROUP_LOCK_BIT 0x0
  137. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  138. #define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT)
  139. #else
  140. #define PAGE_CGROUP_LOCK 0x0
  141. #endif
  142. /*
  143. * A page_cgroup page is associated with every page descriptor. The
  144. * page_cgroup helps us identify information about the cgroup
  145. */
  146. struct page_cgroup {
  147. struct list_head lru; /* per cgroup LRU list */
  148. struct page *page;
  149. struct mem_cgroup *mem_cgroup;
  150. int flags;
  151. };
  152. #define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */
  153. #define PAGE_CGROUP_FLAG_ACTIVE (0x2) /* page is active in this cgroup */
  154. static int page_cgroup_nid(struct page_cgroup *pc)
  155. {
  156. return page_to_nid(pc->page);
  157. }
  158. static enum zone_type page_cgroup_zid(struct page_cgroup *pc)
  159. {
  160. return page_zonenum(pc->page);
  161. }
  162. enum charge_type {
  163. MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
  164. MEM_CGROUP_CHARGE_TYPE_MAPPED,
  165. MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
  166. };
  167. /*
  168. * Always modified under lru lock. Then, not necessary to preempt_disable()
  169. */
  170. static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, int flags,
  171. bool charge)
  172. {
  173. int val = (charge)? 1 : -1;
  174. struct mem_cgroup_stat *stat = &mem->stat;
  175. VM_BUG_ON(!irqs_disabled());
  176. if (flags & PAGE_CGROUP_FLAG_CACHE)
  177. __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_CACHE, val);
  178. else
  179. __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_RSS, val);
  180. if (charge)
  181. __mem_cgroup_stat_add_safe(stat,
  182. MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
  183. else
  184. __mem_cgroup_stat_add_safe(stat,
  185. MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
  186. }
  187. static struct mem_cgroup_per_zone *
  188. mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
  189. {
  190. return &mem->info.nodeinfo[nid]->zoneinfo[zid];
  191. }
  192. static struct mem_cgroup_per_zone *
  193. page_cgroup_zoneinfo(struct page_cgroup *pc)
  194. {
  195. struct mem_cgroup *mem = pc->mem_cgroup;
  196. int nid = page_cgroup_nid(pc);
  197. int zid = page_cgroup_zid(pc);
  198. return mem_cgroup_zoneinfo(mem, nid, zid);
  199. }
  200. static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
  201. enum mem_cgroup_zstat_index idx)
  202. {
  203. int nid, zid;
  204. struct mem_cgroup_per_zone *mz;
  205. u64 total = 0;
  206. for_each_online_node(nid)
  207. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  208. mz = mem_cgroup_zoneinfo(mem, nid, zid);
  209. total += MEM_CGROUP_ZSTAT(mz, idx);
  210. }
  211. return total;
  212. }
  213. static 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. struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
  220. {
  221. /*
  222. * mm_update_next_owner() may clear mm->owner to NULL
  223. * if it races with swapoff, page migration, etc.
  224. * So this can be called with p == NULL.
  225. */
  226. if (unlikely(!p))
  227. return NULL;
  228. return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
  229. struct mem_cgroup, css);
  230. }
  231. static inline int page_cgroup_locked(struct page *page)
  232. {
  233. return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  234. }
  235. static void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
  236. {
  237. VM_BUG_ON(!page_cgroup_locked(page));
  238. page->page_cgroup = ((unsigned long)pc | PAGE_CGROUP_LOCK);
  239. }
  240. struct page_cgroup *page_get_page_cgroup(struct page *page)
  241. {
  242. return (struct page_cgroup *) (page->page_cgroup & ~PAGE_CGROUP_LOCK);
  243. }
  244. static void lock_page_cgroup(struct page *page)
  245. {
  246. bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  247. }
  248. static int try_lock_page_cgroup(struct page *page)
  249. {
  250. return bit_spin_trylock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  251. }
  252. static void unlock_page_cgroup(struct page *page)
  253. {
  254. bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  255. }
  256. static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
  257. struct page_cgroup *pc)
  258. {
  259. int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
  260. if (from)
  261. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1;
  262. else
  263. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1;
  264. mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, false);
  265. list_del(&pc->lru);
  266. }
  267. static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
  268. struct page_cgroup *pc)
  269. {
  270. int to = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
  271. if (!to) {
  272. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1;
  273. list_add(&pc->lru, &mz->inactive_list);
  274. } else {
  275. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1;
  276. list_add(&pc->lru, &mz->active_list);
  277. }
  278. mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, true);
  279. }
  280. static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
  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. if (active) {
  289. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1;
  290. pc->flags |= PAGE_CGROUP_FLAG_ACTIVE;
  291. list_move(&pc->lru, &mz->active_list);
  292. } else {
  293. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1;
  294. pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE;
  295. list_move(&pc->lru, &mz->inactive_list);
  296. }
  297. }
  298. int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
  299. {
  300. int ret;
  301. task_lock(task);
  302. ret = task->mm && mm_match_cgroup(task->mm, mem);
  303. task_unlock(task);
  304. return ret;
  305. }
  306. /*
  307. * This routine assumes that the appropriate zone's lru lock is already held
  308. */
  309. void mem_cgroup_move_lists(struct page *page, bool active)
  310. {
  311. struct page_cgroup *pc;
  312. struct mem_cgroup_per_zone *mz;
  313. unsigned long flags;
  314. if (mem_cgroup_subsys.disabled)
  315. return;
  316. /*
  317. * We cannot lock_page_cgroup while holding zone's lru_lock,
  318. * because other holders of lock_page_cgroup can be interrupted
  319. * with an attempt to rotate_reclaimable_page. But we cannot
  320. * safely get to page_cgroup without it, so just try_lock it:
  321. * mem_cgroup_isolate_pages allows for page left on wrong list.
  322. */
  323. if (!try_lock_page_cgroup(page))
  324. return;
  325. pc = page_get_page_cgroup(page);
  326. if (pc) {
  327. mz = page_cgroup_zoneinfo(pc);
  328. spin_lock_irqsave(&mz->lru_lock, flags);
  329. __mem_cgroup_move_lists(pc, active);
  330. spin_unlock_irqrestore(&mz->lru_lock, flags);
  331. }
  332. unlock_page_cgroup(page);
  333. }
  334. /*
  335. * Calculate mapped_ratio under memory controller. This will be used in
  336. * vmscan.c for deteremining we have to reclaim mapped pages.
  337. */
  338. int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
  339. {
  340. long total, rss;
  341. /*
  342. * usage is recorded in bytes. But, here, we assume the number of
  343. * physical pages can be represented by "long" on any arch.
  344. */
  345. total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
  346. rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
  347. return (int)((rss * 100L) / total);
  348. }
  349. /*
  350. * This function is called from vmscan.c. In page reclaiming loop. balance
  351. * between active and inactive list is calculated. For memory controller
  352. * page reclaiming, we should use using mem_cgroup's imbalance rather than
  353. * zone's global lru imbalance.
  354. */
  355. long mem_cgroup_reclaim_imbalance(struct mem_cgroup *mem)
  356. {
  357. unsigned long active, inactive;
  358. /* active and inactive are the number of pages. 'long' is ok.*/
  359. active = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_ACTIVE);
  360. inactive = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_INACTIVE);
  361. return (long) (active / (inactive + 1));
  362. }
  363. /*
  364. * prev_priority control...this will be used in memory reclaim path.
  365. */
  366. int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
  367. {
  368. return mem->prev_priority;
  369. }
  370. void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
  371. {
  372. if (priority < mem->prev_priority)
  373. mem->prev_priority = priority;
  374. }
  375. void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
  376. {
  377. mem->prev_priority = priority;
  378. }
  379. /*
  380. * Calculate # of pages to be scanned in this priority/zone.
  381. * See also vmscan.c
  382. *
  383. * priority starts from "DEF_PRIORITY" and decremented in each loop.
  384. * (see include/linux/mmzone.h)
  385. */
  386. long mem_cgroup_calc_reclaim_active(struct mem_cgroup *mem,
  387. struct zone *zone, int priority)
  388. {
  389. long nr_active;
  390. int nid = zone->zone_pgdat->node_id;
  391. int zid = zone_idx(zone);
  392. struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
  393. nr_active = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE);
  394. return (nr_active >> priority);
  395. }
  396. long mem_cgroup_calc_reclaim_inactive(struct mem_cgroup *mem,
  397. struct zone *zone, int priority)
  398. {
  399. long nr_inactive;
  400. int nid = zone->zone_pgdat->node_id;
  401. int zid = zone_idx(zone);
  402. struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
  403. nr_inactive = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE);
  404. return (nr_inactive >> priority);
  405. }
  406. unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
  407. struct list_head *dst,
  408. unsigned long *scanned, int order,
  409. int mode, struct zone *z,
  410. struct mem_cgroup *mem_cont,
  411. int active)
  412. {
  413. unsigned long nr_taken = 0;
  414. struct page *page;
  415. unsigned long scan;
  416. LIST_HEAD(pc_list);
  417. struct list_head *src;
  418. struct page_cgroup *pc, *tmp;
  419. int nid = z->zone_pgdat->node_id;
  420. int zid = zone_idx(z);
  421. struct mem_cgroup_per_zone *mz;
  422. BUG_ON(!mem_cont);
  423. mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
  424. if (active)
  425. src = &mz->active_list;
  426. else
  427. src = &mz->inactive_list;
  428. spin_lock(&mz->lru_lock);
  429. scan = 0;
  430. list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
  431. if (scan >= nr_to_scan)
  432. break;
  433. page = pc->page;
  434. if (unlikely(!PageLRU(page)))
  435. continue;
  436. if (PageActive(page) && !active) {
  437. __mem_cgroup_move_lists(pc, true);
  438. continue;
  439. }
  440. if (!PageActive(page) && active) {
  441. __mem_cgroup_move_lists(pc, false);
  442. continue;
  443. }
  444. scan++;
  445. list_move(&pc->lru, &pc_list);
  446. if (__isolate_lru_page(page, mode) == 0) {
  447. list_move(&page->lru, dst);
  448. nr_taken++;
  449. }
  450. }
  451. list_splice(&pc_list, src);
  452. spin_unlock(&mz->lru_lock);
  453. *scanned = scan;
  454. return nr_taken;
  455. }
  456. /*
  457. * Charge the memory controller for page usage.
  458. * Return
  459. * 0 if the charge was successful
  460. * < 0 if the cgroup is over its limit
  461. */
  462. static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
  463. gfp_t gfp_mask, enum charge_type ctype,
  464. struct mem_cgroup *memcg)
  465. {
  466. struct mem_cgroup *mem;
  467. struct page_cgroup *pc;
  468. unsigned long flags;
  469. unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  470. struct mem_cgroup_per_zone *mz;
  471. pc = kmem_cache_alloc(page_cgroup_cache, gfp_mask);
  472. if (unlikely(pc == NULL))
  473. goto err;
  474. /*
  475. * We always charge the cgroup the mm_struct belongs to.
  476. * The mm_struct's mem_cgroup changes on task migration if the
  477. * thread group leader migrates. It's possible that mm is not
  478. * set, if so charge the init_mm (happens for pagecache usage).
  479. */
  480. if (likely(!memcg)) {
  481. rcu_read_lock();
  482. mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
  483. if (unlikely(!mem)) {
  484. rcu_read_unlock();
  485. kmem_cache_free(page_cgroup_cache, pc);
  486. return 0;
  487. }
  488. /*
  489. * For every charge from the cgroup, increment reference count
  490. */
  491. css_get(&mem->css);
  492. rcu_read_unlock();
  493. } else {
  494. mem = memcg;
  495. css_get(&memcg->css);
  496. }
  497. while (res_counter_charge(&mem->res, PAGE_SIZE)) {
  498. if (!(gfp_mask & __GFP_WAIT))
  499. goto out;
  500. if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
  501. continue;
  502. /*
  503. * try_to_free_mem_cgroup_pages() might not give us a full
  504. * picture of reclaim. Some pages are reclaimed and might be
  505. * moved to swap cache or just unmapped from the cgroup.
  506. * Check the limit again to see if the reclaim reduced the
  507. * current usage of the cgroup before giving up
  508. */
  509. if (res_counter_check_under_limit(&mem->res))
  510. continue;
  511. if (!nr_retries--) {
  512. mem_cgroup_out_of_memory(mem, gfp_mask);
  513. goto out;
  514. }
  515. }
  516. pc->mem_cgroup = mem;
  517. pc->page = page;
  518. /*
  519. * If a page is accounted as a page cache, insert to inactive list.
  520. * If anon, insert to active list.
  521. */
  522. if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE)
  523. pc->flags = PAGE_CGROUP_FLAG_CACHE;
  524. else
  525. pc->flags = PAGE_CGROUP_FLAG_ACTIVE;
  526. lock_page_cgroup(page);
  527. if (unlikely(page_get_page_cgroup(page))) {
  528. unlock_page_cgroup(page);
  529. res_counter_uncharge(&mem->res, PAGE_SIZE);
  530. css_put(&mem->css);
  531. kmem_cache_free(page_cgroup_cache, pc);
  532. goto done;
  533. }
  534. page_assign_page_cgroup(page, pc);
  535. mz = page_cgroup_zoneinfo(pc);
  536. spin_lock_irqsave(&mz->lru_lock, flags);
  537. __mem_cgroup_add_list(mz, pc);
  538. spin_unlock_irqrestore(&mz->lru_lock, flags);
  539. unlock_page_cgroup(page);
  540. done:
  541. return 0;
  542. out:
  543. css_put(&mem->css);
  544. kmem_cache_free(page_cgroup_cache, pc);
  545. err:
  546. return -ENOMEM;
  547. }
  548. int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask)
  549. {
  550. if (mem_cgroup_subsys.disabled)
  551. return 0;
  552. /*
  553. * If already mapped, we don't have to account.
  554. * If page cache, page->mapping has address_space.
  555. * But page->mapping may have out-of-use anon_vma pointer,
  556. * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
  557. * is NULL.
  558. */
  559. if (page_mapped(page) || (page->mapping && !PageAnon(page)))
  560. return 0;
  561. if (unlikely(!mm))
  562. mm = &init_mm;
  563. return mem_cgroup_charge_common(page, mm, gfp_mask,
  564. MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
  565. }
  566. int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
  567. gfp_t gfp_mask)
  568. {
  569. if (mem_cgroup_subsys.disabled)
  570. return 0;
  571. /*
  572. * Corner case handling. This is called from add_to_page_cache()
  573. * in usual. But some FS (shmem) precharges this page before calling it
  574. * and call add_to_page_cache() with GFP_NOWAIT.
  575. *
  576. * For GFP_NOWAIT case, the page may be pre-charged before calling
  577. * add_to_page_cache(). (See shmem.c) check it here and avoid to call
  578. * charge twice. (It works but has to pay a bit larger cost.)
  579. */
  580. if (!(gfp_mask & __GFP_WAIT)) {
  581. struct page_cgroup *pc;
  582. lock_page_cgroup(page);
  583. pc = page_get_page_cgroup(page);
  584. if (pc) {
  585. VM_BUG_ON(pc->page != page);
  586. VM_BUG_ON(!pc->mem_cgroup);
  587. unlock_page_cgroup(page);
  588. return 0;
  589. }
  590. unlock_page_cgroup(page);
  591. }
  592. if (unlikely(!mm))
  593. mm = &init_mm;
  594. return mem_cgroup_charge_common(page, mm, gfp_mask,
  595. MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
  596. }
  597. /*
  598. * uncharge if !page_mapped(page)
  599. */
  600. static void
  601. __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
  602. {
  603. struct page_cgroup *pc;
  604. struct mem_cgroup *mem;
  605. struct mem_cgroup_per_zone *mz;
  606. unsigned long flags;
  607. if (mem_cgroup_subsys.disabled)
  608. return;
  609. /*
  610. * Check if our page_cgroup is valid
  611. */
  612. lock_page_cgroup(page);
  613. pc = page_get_page_cgroup(page);
  614. if (unlikely(!pc))
  615. goto unlock;
  616. VM_BUG_ON(pc->page != page);
  617. if ((ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
  618. && ((pc->flags & PAGE_CGROUP_FLAG_CACHE)
  619. || page_mapped(page)))
  620. goto unlock;
  621. mz = page_cgroup_zoneinfo(pc);
  622. spin_lock_irqsave(&mz->lru_lock, flags);
  623. __mem_cgroup_remove_list(mz, pc);
  624. spin_unlock_irqrestore(&mz->lru_lock, flags);
  625. page_assign_page_cgroup(page, NULL);
  626. unlock_page_cgroup(page);
  627. mem = pc->mem_cgroup;
  628. res_counter_uncharge(&mem->res, PAGE_SIZE);
  629. css_put(&mem->css);
  630. kmem_cache_free(page_cgroup_cache, pc);
  631. return;
  632. unlock:
  633. unlock_page_cgroup(page);
  634. }
  635. void mem_cgroup_uncharge_page(struct page *page)
  636. {
  637. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
  638. }
  639. void mem_cgroup_uncharge_cache_page(struct page *page)
  640. {
  641. VM_BUG_ON(page_mapped(page));
  642. __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
  643. }
  644. /*
  645. * Before starting migration, account against new page.
  646. */
  647. int mem_cgroup_prepare_migration(struct page *page, struct page *newpage)
  648. {
  649. struct page_cgroup *pc;
  650. struct mem_cgroup *mem = NULL;
  651. enum charge_type ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
  652. int ret = 0;
  653. if (mem_cgroup_subsys.disabled)
  654. return 0;
  655. lock_page_cgroup(page);
  656. pc = page_get_page_cgroup(page);
  657. if (pc) {
  658. mem = pc->mem_cgroup;
  659. css_get(&mem->css);
  660. if (pc->flags & PAGE_CGROUP_FLAG_CACHE)
  661. ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
  662. }
  663. unlock_page_cgroup(page);
  664. if (mem) {
  665. ret = mem_cgroup_charge_common(newpage, NULL, GFP_KERNEL,
  666. ctype, mem);
  667. css_put(&mem->css);
  668. }
  669. return ret;
  670. }
  671. /* remove redundant charge if migration failed*/
  672. void mem_cgroup_end_migration(struct page *newpage)
  673. {
  674. /*
  675. * At success, page->mapping is not NULL.
  676. * special rollback care is necessary when
  677. * 1. at migration failure. (newpage->mapping is cleared in this case)
  678. * 2. the newpage was moved but not remapped again because the task
  679. * exits and the newpage is obsolete. In this case, the new page
  680. * may be a swapcache. So, we just call mem_cgroup_uncharge_page()
  681. * always for avoiding mess. The page_cgroup will be removed if
  682. * unnecessary. File cache pages is still on radix-tree. Don't
  683. * care it.
  684. */
  685. if (!newpage->mapping)
  686. __mem_cgroup_uncharge_common(newpage,
  687. MEM_CGROUP_CHARGE_TYPE_FORCE);
  688. else if (PageAnon(newpage))
  689. mem_cgroup_uncharge_page(newpage);
  690. }
  691. /*
  692. * A call to try to shrink memory usage under specified resource controller.
  693. * This is typically used for page reclaiming for shmem for reducing side
  694. * effect of page allocation from shmem, which is used by some mem_cgroup.
  695. */
  696. int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
  697. {
  698. struct mem_cgroup *mem;
  699. int progress = 0;
  700. int retry = MEM_CGROUP_RECLAIM_RETRIES;
  701. if (mem_cgroup_subsys.disabled)
  702. return 0;
  703. if (!mm)
  704. return 0;
  705. rcu_read_lock();
  706. mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
  707. if (unlikely(!mem)) {
  708. rcu_read_unlock();
  709. return 0;
  710. }
  711. css_get(&mem->css);
  712. rcu_read_unlock();
  713. do {
  714. progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
  715. progress += res_counter_check_under_limit(&mem->res);
  716. } while (!progress && --retry);
  717. css_put(&mem->css);
  718. if (!retry)
  719. return -ENOMEM;
  720. return 0;
  721. }
  722. int mem_cgroup_resize_limit(struct mem_cgroup *memcg, unsigned long long val)
  723. {
  724. int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
  725. int progress;
  726. int ret = 0;
  727. while (res_counter_set_limit(&memcg->res, val)) {
  728. if (signal_pending(current)) {
  729. ret = -EINTR;
  730. break;
  731. }
  732. if (!retry_count) {
  733. ret = -EBUSY;
  734. break;
  735. }
  736. progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL);
  737. if (!progress)
  738. retry_count--;
  739. }
  740. return ret;
  741. }
  742. /*
  743. * This routine traverse page_cgroup in given list and drop them all.
  744. * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
  745. */
  746. #define FORCE_UNCHARGE_BATCH (128)
  747. static void mem_cgroup_force_empty_list(struct mem_cgroup *mem,
  748. struct mem_cgroup_per_zone *mz,
  749. int active)
  750. {
  751. struct page_cgroup *pc;
  752. struct page *page;
  753. int count = FORCE_UNCHARGE_BATCH;
  754. unsigned long flags;
  755. struct list_head *list;
  756. if (active)
  757. list = &mz->active_list;
  758. else
  759. list = &mz->inactive_list;
  760. spin_lock_irqsave(&mz->lru_lock, flags);
  761. while (!list_empty(list)) {
  762. pc = list_entry(list->prev, struct page_cgroup, lru);
  763. page = pc->page;
  764. get_page(page);
  765. spin_unlock_irqrestore(&mz->lru_lock, flags);
  766. /*
  767. * Check if this page is on LRU. !LRU page can be found
  768. * if it's under page migration.
  769. */
  770. if (PageLRU(page)) {
  771. __mem_cgroup_uncharge_common(page,
  772. MEM_CGROUP_CHARGE_TYPE_FORCE);
  773. put_page(page);
  774. if (--count <= 0) {
  775. count = FORCE_UNCHARGE_BATCH;
  776. cond_resched();
  777. }
  778. } else
  779. cond_resched();
  780. spin_lock_irqsave(&mz->lru_lock, flags);
  781. }
  782. spin_unlock_irqrestore(&mz->lru_lock, flags);
  783. }
  784. /*
  785. * make mem_cgroup's charge to be 0 if there is no task.
  786. * This enables deleting this mem_cgroup.
  787. */
  788. static int mem_cgroup_force_empty(struct mem_cgroup *mem)
  789. {
  790. int ret = -EBUSY;
  791. int node, zid;
  792. css_get(&mem->css);
  793. /*
  794. * page reclaim code (kswapd etc..) will move pages between
  795. * active_list <-> inactive_list while we don't take a lock.
  796. * So, we have to do loop here until all lists are empty.
  797. */
  798. while (mem->res.usage > 0) {
  799. if (atomic_read(&mem->css.cgroup->count) > 0)
  800. goto out;
  801. for_each_node_state(node, N_POSSIBLE)
  802. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  803. struct mem_cgroup_per_zone *mz;
  804. mz = mem_cgroup_zoneinfo(mem, node, zid);
  805. /* drop all page_cgroup in active_list */
  806. mem_cgroup_force_empty_list(mem, mz, 1);
  807. /* drop all page_cgroup in inactive_list */
  808. mem_cgroup_force_empty_list(mem, mz, 0);
  809. }
  810. }
  811. ret = 0;
  812. out:
  813. css_put(&mem->css);
  814. return ret;
  815. }
  816. static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
  817. {
  818. return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
  819. cft->private);
  820. }
  821. /*
  822. * The user of this function is...
  823. * RES_LIMIT.
  824. */
  825. static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
  826. const char *buffer)
  827. {
  828. struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
  829. unsigned long long val;
  830. int ret;
  831. switch (cft->private) {
  832. case RES_LIMIT:
  833. /* This function does all necessary parse...reuse it */
  834. ret = res_counter_memparse_write_strategy(buffer, &val);
  835. if (!ret)
  836. ret = mem_cgroup_resize_limit(memcg, val);
  837. break;
  838. default:
  839. ret = -EINVAL; /* should be BUG() ? */
  840. break;
  841. }
  842. return ret;
  843. }
  844. static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
  845. {
  846. struct mem_cgroup *mem;
  847. mem = mem_cgroup_from_cont(cont);
  848. switch (event) {
  849. case RES_MAX_USAGE:
  850. res_counter_reset_max(&mem->res);
  851. break;
  852. case RES_FAILCNT:
  853. res_counter_reset_failcnt(&mem->res);
  854. break;
  855. }
  856. return 0;
  857. }
  858. static int mem_force_empty_write(struct cgroup *cont, unsigned int event)
  859. {
  860. return mem_cgroup_force_empty(mem_cgroup_from_cont(cont));
  861. }
  862. static const struct mem_cgroup_stat_desc {
  863. const char *msg;
  864. u64 unit;
  865. } mem_cgroup_stat_desc[] = {
  866. [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
  867. [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
  868. [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
  869. [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
  870. };
  871. static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
  872. struct cgroup_map_cb *cb)
  873. {
  874. struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
  875. struct mem_cgroup_stat *stat = &mem_cont->stat;
  876. int i;
  877. for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
  878. s64 val;
  879. val = mem_cgroup_read_stat(stat, i);
  880. val *= mem_cgroup_stat_desc[i].unit;
  881. cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
  882. }
  883. /* showing # of active pages */
  884. {
  885. unsigned long active, inactive;
  886. inactive = mem_cgroup_get_all_zonestat(mem_cont,
  887. MEM_CGROUP_ZSTAT_INACTIVE);
  888. active = mem_cgroup_get_all_zonestat(mem_cont,
  889. MEM_CGROUP_ZSTAT_ACTIVE);
  890. cb->fill(cb, "active", (active) * PAGE_SIZE);
  891. cb->fill(cb, "inactive", (inactive) * PAGE_SIZE);
  892. }
  893. return 0;
  894. }
  895. static struct cftype mem_cgroup_files[] = {
  896. {
  897. .name = "usage_in_bytes",
  898. .private = RES_USAGE,
  899. .read_u64 = mem_cgroup_read,
  900. },
  901. {
  902. .name = "max_usage_in_bytes",
  903. .private = RES_MAX_USAGE,
  904. .trigger = mem_cgroup_reset,
  905. .read_u64 = mem_cgroup_read,
  906. },
  907. {
  908. .name = "limit_in_bytes",
  909. .private = RES_LIMIT,
  910. .write_string = mem_cgroup_write,
  911. .read_u64 = mem_cgroup_read,
  912. },
  913. {
  914. .name = "failcnt",
  915. .private = RES_FAILCNT,
  916. .trigger = mem_cgroup_reset,
  917. .read_u64 = mem_cgroup_read,
  918. },
  919. {
  920. .name = "force_empty",
  921. .trigger = mem_force_empty_write,
  922. },
  923. {
  924. .name = "stat",
  925. .read_map = mem_control_stat_show,
  926. },
  927. };
  928. static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  929. {
  930. struct mem_cgroup_per_node *pn;
  931. struct mem_cgroup_per_zone *mz;
  932. int zone, tmp = node;
  933. /*
  934. * This routine is called against possible nodes.
  935. * But it's BUG to call kmalloc() against offline node.
  936. *
  937. * TODO: this routine can waste much memory for nodes which will
  938. * never be onlined. It's better to use memory hotplug callback
  939. * function.
  940. */
  941. if (!node_state(node, N_NORMAL_MEMORY))
  942. tmp = -1;
  943. pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
  944. if (!pn)
  945. return 1;
  946. mem->info.nodeinfo[node] = pn;
  947. memset(pn, 0, sizeof(*pn));
  948. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  949. mz = &pn->zoneinfo[zone];
  950. INIT_LIST_HEAD(&mz->active_list);
  951. INIT_LIST_HEAD(&mz->inactive_list);
  952. spin_lock_init(&mz->lru_lock);
  953. }
  954. return 0;
  955. }
  956. static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  957. {
  958. kfree(mem->info.nodeinfo[node]);
  959. }
  960. static struct mem_cgroup *mem_cgroup_alloc(void)
  961. {
  962. struct mem_cgroup *mem;
  963. if (sizeof(*mem) < PAGE_SIZE)
  964. mem = kmalloc(sizeof(*mem), GFP_KERNEL);
  965. else
  966. mem = vmalloc(sizeof(*mem));
  967. if (mem)
  968. memset(mem, 0, sizeof(*mem));
  969. return mem;
  970. }
  971. static void mem_cgroup_free(struct mem_cgroup *mem)
  972. {
  973. if (sizeof(*mem) < PAGE_SIZE)
  974. kfree(mem);
  975. else
  976. vfree(mem);
  977. }
  978. static struct cgroup_subsys_state *
  979. mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
  980. {
  981. struct mem_cgroup *mem;
  982. int node;
  983. if (unlikely((cont->parent) == NULL)) {
  984. mem = &init_mem_cgroup;
  985. page_cgroup_cache = KMEM_CACHE(page_cgroup, SLAB_PANIC);
  986. } else {
  987. mem = mem_cgroup_alloc();
  988. if (!mem)
  989. return ERR_PTR(-ENOMEM);
  990. }
  991. res_counter_init(&mem->res);
  992. for_each_node_state(node, N_POSSIBLE)
  993. if (alloc_mem_cgroup_per_zone_info(mem, node))
  994. goto free_out;
  995. return &mem->css;
  996. free_out:
  997. for_each_node_state(node, N_POSSIBLE)
  998. free_mem_cgroup_per_zone_info(mem, node);
  999. if (cont->parent != NULL)
  1000. mem_cgroup_free(mem);
  1001. return ERR_PTR(-ENOMEM);
  1002. }
  1003. static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
  1004. struct cgroup *cont)
  1005. {
  1006. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  1007. mem_cgroup_force_empty(mem);
  1008. }
  1009. static void mem_cgroup_destroy(struct cgroup_subsys *ss,
  1010. struct cgroup *cont)
  1011. {
  1012. int node;
  1013. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  1014. for_each_node_state(node, N_POSSIBLE)
  1015. free_mem_cgroup_per_zone_info(mem, node);
  1016. mem_cgroup_free(mem_cgroup_from_cont(cont));
  1017. }
  1018. static int mem_cgroup_populate(struct cgroup_subsys *ss,
  1019. struct cgroup *cont)
  1020. {
  1021. return cgroup_add_files(cont, ss, mem_cgroup_files,
  1022. ARRAY_SIZE(mem_cgroup_files));
  1023. }
  1024. static void mem_cgroup_move_task(struct cgroup_subsys *ss,
  1025. struct cgroup *cont,
  1026. struct cgroup *old_cont,
  1027. struct task_struct *p)
  1028. {
  1029. struct mm_struct *mm;
  1030. struct mem_cgroup *mem, *old_mem;
  1031. mm = get_task_mm(p);
  1032. if (mm == NULL)
  1033. return;
  1034. mem = mem_cgroup_from_cont(cont);
  1035. old_mem = mem_cgroup_from_cont(old_cont);
  1036. /*
  1037. * Only thread group leaders are allowed to migrate, the mm_struct is
  1038. * in effect owned by the leader
  1039. */
  1040. if (!thread_group_leader(p))
  1041. goto out;
  1042. out:
  1043. mmput(mm);
  1044. }
  1045. struct cgroup_subsys mem_cgroup_subsys = {
  1046. .name = "memory",
  1047. .subsys_id = mem_cgroup_subsys_id,
  1048. .create = mem_cgroup_create,
  1049. .pre_destroy = mem_cgroup_pre_destroy,
  1050. .destroy = mem_cgroup_destroy,
  1051. .populate = mem_cgroup_populate,
  1052. .attach = mem_cgroup_move_task,
  1053. .early_init = 0,
  1054. };