memcontrol.c 30 KB

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