memcontrol.c 30 KB

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