memcontrol.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  1. /* memcontrol.c - Memory Controller
  2. *
  3. * Copyright IBM Corporation, 2007
  4. * Author Balbir Singh <balbir@linux.vnet.ibm.com>
  5. *
  6. * Copyright 2007 OpenVZ SWsoft Inc
  7. * Author: Pavel Emelianov <xemul@openvz.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/res_counter.h>
  20. #include <linux/memcontrol.h>
  21. #include <linux/cgroup.h>
  22. #include <linux/mm.h>
  23. #include <linux/smp.h>
  24. #include <linux/page-flags.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/bit_spinlock.h>
  27. #include <linux/rcupdate.h>
  28. #include <linux/swap.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/fs.h>
  31. #include <linux/seq_file.h>
  32. #include <asm/uaccess.h>
  33. struct cgroup_subsys mem_cgroup_subsys;
  34. static const int MEM_CGROUP_RECLAIM_RETRIES = 5;
  35. /*
  36. * Statistics for memory cgroup.
  37. */
  38. enum mem_cgroup_stat_index {
  39. /*
  40. * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
  41. */
  42. MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
  43. MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
  44. MEM_CGROUP_STAT_NSTATS,
  45. };
  46. struct mem_cgroup_stat_cpu {
  47. s64 count[MEM_CGROUP_STAT_NSTATS];
  48. } ____cacheline_aligned_in_smp;
  49. struct mem_cgroup_stat {
  50. struct mem_cgroup_stat_cpu cpustat[NR_CPUS];
  51. };
  52. /*
  53. * For accounting under irq disable, no need for increment preempt count.
  54. */
  55. static void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat *stat,
  56. enum mem_cgroup_stat_index idx, int val)
  57. {
  58. int cpu = smp_processor_id();
  59. stat->cpustat[cpu].count[idx] += val;
  60. }
  61. static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
  62. enum mem_cgroup_stat_index idx)
  63. {
  64. int cpu;
  65. s64 ret = 0;
  66. for_each_possible_cpu(cpu)
  67. ret += stat->cpustat[cpu].count[idx];
  68. return ret;
  69. }
  70. /*
  71. * per-zone information in memory controller.
  72. */
  73. enum mem_cgroup_zstat_index {
  74. MEM_CGROUP_ZSTAT_ACTIVE,
  75. MEM_CGROUP_ZSTAT_INACTIVE,
  76. NR_MEM_CGROUP_ZSTAT,
  77. };
  78. struct mem_cgroup_per_zone {
  79. struct list_head active_list;
  80. struct list_head inactive_list;
  81. unsigned long count[NR_MEM_CGROUP_ZSTAT];
  82. };
  83. /* Macro for accessing counter */
  84. #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
  85. struct mem_cgroup_per_node {
  86. struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
  87. };
  88. struct mem_cgroup_lru_info {
  89. struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
  90. };
  91. /*
  92. * The memory controller data structure. The memory controller controls both
  93. * page cache and RSS per cgroup. We would eventually like to provide
  94. * statistics based on the statistics developed by Rik Van Riel for clock-pro,
  95. * to help the administrator determine what knobs to tune.
  96. *
  97. * TODO: Add a water mark for the memory controller. Reclaim will begin when
  98. * we hit the water mark. May be even add a low water mark, such that
  99. * no reclaim occurs from a cgroup at it's low water mark, this is
  100. * a feature that will be implemented much later in the future.
  101. */
  102. struct mem_cgroup {
  103. struct cgroup_subsys_state css;
  104. /*
  105. * the counter to account for memory usage
  106. */
  107. struct res_counter res;
  108. /*
  109. * Per cgroup active and inactive list, similar to the
  110. * per zone LRU lists.
  111. */
  112. struct mem_cgroup_lru_info info;
  113. /*
  114. * spin_lock to protect the per cgroup LRU
  115. */
  116. spinlock_t lru_lock;
  117. unsigned long control_type; /* control RSS or RSS+Pagecache */
  118. int prev_priority; /* for recording reclaim priority */
  119. /*
  120. * statistics.
  121. */
  122. struct mem_cgroup_stat stat;
  123. };
  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 atleast two
  127. * byte aligned (based on comments from Nick Piggin)
  128. */
  129. #define PAGE_CGROUP_LOCK_BIT 0x0
  130. #define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT)
  131. /*
  132. * A page_cgroup page is associated with every page descriptor. The
  133. * page_cgroup helps us identify information about the cgroup
  134. */
  135. struct page_cgroup {
  136. struct list_head lru; /* per cgroup LRU list */
  137. struct page *page;
  138. struct mem_cgroup *mem_cgroup;
  139. atomic_t ref_cnt; /* Helpful when pages move b/w */
  140. /* mapped and cached states */
  141. int flags;
  142. };
  143. #define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */
  144. #define PAGE_CGROUP_FLAG_ACTIVE (0x2) /* page is active in this cgroup */
  145. static inline int page_cgroup_nid(struct page_cgroup *pc)
  146. {
  147. return page_to_nid(pc->page);
  148. }
  149. static inline enum zone_type page_cgroup_zid(struct page_cgroup *pc)
  150. {
  151. return page_zonenum(pc->page);
  152. }
  153. enum {
  154. MEM_CGROUP_TYPE_UNSPEC = 0,
  155. MEM_CGROUP_TYPE_MAPPED,
  156. MEM_CGROUP_TYPE_CACHED,
  157. MEM_CGROUP_TYPE_ALL,
  158. MEM_CGROUP_TYPE_MAX,
  159. };
  160. enum charge_type {
  161. MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
  162. MEM_CGROUP_CHARGE_TYPE_MAPPED,
  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. VM_BUG_ON(!irqs_disabled());
  173. if (flags & PAGE_CGROUP_FLAG_CACHE)
  174. __mem_cgroup_stat_add_safe(stat,
  175. MEM_CGROUP_STAT_CACHE, val);
  176. else
  177. __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_RSS, val);
  178. }
  179. static inline struct mem_cgroup_per_zone *
  180. mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
  181. {
  182. BUG_ON(!mem->info.nodeinfo[nid]);
  183. return &mem->info.nodeinfo[nid]->zoneinfo[zid];
  184. }
  185. static inline struct mem_cgroup_per_zone *
  186. page_cgroup_zoneinfo(struct page_cgroup *pc)
  187. {
  188. struct mem_cgroup *mem = pc->mem_cgroup;
  189. int nid = page_cgroup_nid(pc);
  190. int zid = page_cgroup_zid(pc);
  191. return mem_cgroup_zoneinfo(mem, nid, zid);
  192. }
  193. static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
  194. enum mem_cgroup_zstat_index idx)
  195. {
  196. int nid, zid;
  197. struct mem_cgroup_per_zone *mz;
  198. u64 total = 0;
  199. for_each_online_node(nid)
  200. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  201. mz = mem_cgroup_zoneinfo(mem, nid, zid);
  202. total += MEM_CGROUP_ZSTAT(mz, idx);
  203. }
  204. return total;
  205. }
  206. static struct mem_cgroup init_mem_cgroup;
  207. static inline
  208. struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
  209. {
  210. return container_of(cgroup_subsys_state(cont,
  211. mem_cgroup_subsys_id), struct mem_cgroup,
  212. css);
  213. }
  214. static inline
  215. struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
  216. {
  217. return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
  218. struct mem_cgroup, css);
  219. }
  220. void mm_init_cgroup(struct mm_struct *mm, struct task_struct *p)
  221. {
  222. struct mem_cgroup *mem;
  223. mem = mem_cgroup_from_task(p);
  224. css_get(&mem->css);
  225. mm->mem_cgroup = mem;
  226. }
  227. void mm_free_cgroup(struct mm_struct *mm)
  228. {
  229. css_put(&mm->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,
  234. &page->page_cgroup);
  235. }
  236. void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
  237. {
  238. int locked;
  239. /*
  240. * While resetting the page_cgroup we might not hold the
  241. * page_cgroup lock. free_hot_cold_page() is an example
  242. * of such a scenario
  243. */
  244. if (pc)
  245. VM_BUG_ON(!page_cgroup_locked(page));
  246. locked = (page->page_cgroup & PAGE_CGROUP_LOCK);
  247. page->page_cgroup = ((unsigned long)pc | locked);
  248. }
  249. struct page_cgroup *page_get_page_cgroup(struct page *page)
  250. {
  251. return (struct page_cgroup *)
  252. (page->page_cgroup & ~PAGE_CGROUP_LOCK);
  253. }
  254. static void __always_inline lock_page_cgroup(struct page *page)
  255. {
  256. bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  257. VM_BUG_ON(!page_cgroup_locked(page));
  258. }
  259. static void __always_inline unlock_page_cgroup(struct page *page)
  260. {
  261. bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
  262. }
  263. /*
  264. * Tie new page_cgroup to struct page under lock_page_cgroup()
  265. * This can fail if the page has been tied to a page_cgroup.
  266. * If success, returns 0.
  267. */
  268. static int page_cgroup_assign_new_page_cgroup(struct page *page,
  269. struct page_cgroup *pc)
  270. {
  271. int ret = 0;
  272. lock_page_cgroup(page);
  273. if (!page_get_page_cgroup(page))
  274. page_assign_page_cgroup(page, pc);
  275. else /* A page is tied to other pc. */
  276. ret = 1;
  277. unlock_page_cgroup(page);
  278. return ret;
  279. }
  280. /*
  281. * Clear page->page_cgroup member under lock_page_cgroup().
  282. * If given "pc" value is different from one page->page_cgroup,
  283. * page->cgroup is not cleared.
  284. * Returns a value of page->page_cgroup at lock taken.
  285. * A can can detect failure of clearing by following
  286. * clear_page_cgroup(page, pc) == pc
  287. */
  288. static struct page_cgroup *clear_page_cgroup(struct page *page,
  289. struct page_cgroup *pc)
  290. {
  291. struct page_cgroup *ret;
  292. /* lock and clear */
  293. lock_page_cgroup(page);
  294. ret = page_get_page_cgroup(page);
  295. if (likely(ret == pc))
  296. page_assign_page_cgroup(page, NULL);
  297. unlock_page_cgroup(page);
  298. return ret;
  299. }
  300. static void __mem_cgroup_remove_list(struct page_cgroup *pc)
  301. {
  302. int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
  303. struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
  304. if (from)
  305. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1;
  306. else
  307. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1;
  308. mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, false);
  309. list_del_init(&pc->lru);
  310. }
  311. static void __mem_cgroup_add_list(struct page_cgroup *pc)
  312. {
  313. int to = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
  314. struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
  315. if (!to) {
  316. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1;
  317. list_add(&pc->lru, &mz->inactive_list);
  318. } else {
  319. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1;
  320. list_add(&pc->lru, &mz->active_list);
  321. }
  322. mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, true);
  323. }
  324. static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
  325. {
  326. int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
  327. struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
  328. if (from)
  329. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1;
  330. else
  331. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1;
  332. if (active) {
  333. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1;
  334. pc->flags |= PAGE_CGROUP_FLAG_ACTIVE;
  335. list_move(&pc->lru, &mz->active_list);
  336. } else {
  337. MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1;
  338. pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE;
  339. list_move(&pc->lru, &mz->inactive_list);
  340. }
  341. }
  342. int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
  343. {
  344. int ret;
  345. task_lock(task);
  346. ret = task->mm && mm_cgroup(task->mm) == mem;
  347. task_unlock(task);
  348. return ret;
  349. }
  350. /*
  351. * This routine assumes that the appropriate zone's lru lock is already held
  352. */
  353. void mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
  354. {
  355. struct mem_cgroup *mem;
  356. if (!pc)
  357. return;
  358. mem = pc->mem_cgroup;
  359. spin_lock(&mem->lru_lock);
  360. __mem_cgroup_move_lists(pc, active);
  361. spin_unlock(&mem->lru_lock);
  362. }
  363. /*
  364. * Calculate mapped_ratio under memory controller. This will be used in
  365. * vmscan.c for deteremining we have to reclaim mapped pages.
  366. */
  367. int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
  368. {
  369. long total, rss;
  370. /*
  371. * usage is recorded in bytes. But, here, we assume the number of
  372. * physical pages can be represented by "long" on any arch.
  373. */
  374. total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
  375. rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
  376. return (int)((rss * 100L) / total);
  377. }
  378. /*
  379. * This function is called from vmscan.c. In page reclaiming loop. balance
  380. * between active and inactive list is calculated. For memory controller
  381. * page reclaiming, we should use using mem_cgroup's imbalance rather than
  382. * zone's global lru imbalance.
  383. */
  384. long mem_cgroup_reclaim_imbalance(struct mem_cgroup *mem)
  385. {
  386. unsigned long active, inactive;
  387. /* active and inactive are the number of pages. 'long' is ok.*/
  388. active = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_ACTIVE);
  389. inactive = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_INACTIVE);
  390. return (long) (active / (inactive + 1));
  391. }
  392. /*
  393. * prev_priority control...this will be used in memory reclaim path.
  394. */
  395. int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
  396. {
  397. return mem->prev_priority;
  398. }
  399. void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
  400. {
  401. if (priority < mem->prev_priority)
  402. mem->prev_priority = priority;
  403. }
  404. void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
  405. {
  406. mem->prev_priority = priority;
  407. }
  408. /*
  409. * Calculate # of pages to be scanned in this priority/zone.
  410. * See also vmscan.c
  411. *
  412. * priority starts from "DEF_PRIORITY" and decremented in each loop.
  413. * (see include/linux/mmzone.h)
  414. */
  415. long mem_cgroup_calc_reclaim_active(struct mem_cgroup *mem,
  416. struct zone *zone, int priority)
  417. {
  418. long nr_active;
  419. int nid = zone->zone_pgdat->node_id;
  420. int zid = zone_idx(zone);
  421. struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
  422. nr_active = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE);
  423. return (nr_active >> priority);
  424. }
  425. long mem_cgroup_calc_reclaim_inactive(struct mem_cgroup *mem,
  426. struct zone *zone, int priority)
  427. {
  428. long nr_inactive;
  429. int nid = zone->zone_pgdat->node_id;
  430. int zid = zone_idx(zone);
  431. struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
  432. nr_inactive = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE);
  433. return (nr_inactive >> priority);
  434. }
  435. unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
  436. struct list_head *dst,
  437. unsigned long *scanned, int order,
  438. int mode, struct zone *z,
  439. struct mem_cgroup *mem_cont,
  440. int active)
  441. {
  442. unsigned long nr_taken = 0;
  443. struct page *page;
  444. unsigned long scan;
  445. LIST_HEAD(pc_list);
  446. struct list_head *src;
  447. struct page_cgroup *pc, *tmp;
  448. int nid = z->zone_pgdat->node_id;
  449. int zid = zone_idx(z);
  450. struct mem_cgroup_per_zone *mz;
  451. mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
  452. if (active)
  453. src = &mz->active_list;
  454. else
  455. src = &mz->inactive_list;
  456. spin_lock(&mem_cont->lru_lock);
  457. scan = 0;
  458. list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
  459. if (scan >= nr_to_scan)
  460. break;
  461. page = pc->page;
  462. VM_BUG_ON(!pc);
  463. if (unlikely(!PageLRU(page)))
  464. continue;
  465. if (PageActive(page) && !active) {
  466. __mem_cgroup_move_lists(pc, true);
  467. continue;
  468. }
  469. if (!PageActive(page) && active) {
  470. __mem_cgroup_move_lists(pc, false);
  471. continue;
  472. }
  473. scan++;
  474. list_move(&pc->lru, &pc_list);
  475. if (__isolate_lru_page(page, mode) == 0) {
  476. list_move(&page->lru, dst);
  477. nr_taken++;
  478. }
  479. }
  480. list_splice(&pc_list, src);
  481. spin_unlock(&mem_cont->lru_lock);
  482. *scanned = scan;
  483. return nr_taken;
  484. }
  485. /*
  486. * Charge the memory controller for page usage.
  487. * Return
  488. * 0 if the charge was successful
  489. * < 0 if the cgroup is over its limit
  490. */
  491. static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
  492. gfp_t gfp_mask, enum charge_type ctype)
  493. {
  494. struct mem_cgroup *mem;
  495. struct page_cgroup *pc;
  496. unsigned long flags;
  497. unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
  498. /*
  499. * Should page_cgroup's go to their own slab?
  500. * One could optimize the performance of the charging routine
  501. * by saving a bit in the page_flags and using it as a lock
  502. * to see if the cgroup page already has a page_cgroup associated
  503. * with it
  504. */
  505. retry:
  506. if (page) {
  507. lock_page_cgroup(page);
  508. pc = page_get_page_cgroup(page);
  509. /*
  510. * The page_cgroup exists and
  511. * the page has already been accounted.
  512. */
  513. if (pc) {
  514. if (unlikely(!atomic_inc_not_zero(&pc->ref_cnt))) {
  515. /* this page is under being uncharged ? */
  516. unlock_page_cgroup(page);
  517. cpu_relax();
  518. goto retry;
  519. } else {
  520. unlock_page_cgroup(page);
  521. goto done;
  522. }
  523. }
  524. unlock_page_cgroup(page);
  525. }
  526. pc = kzalloc(sizeof(struct page_cgroup), gfp_mask);
  527. if (pc == NULL)
  528. goto err;
  529. /*
  530. * We always charge the cgroup the mm_struct belongs to.
  531. * The mm_struct's mem_cgroup changes on task migration if the
  532. * thread group leader migrates. It's possible that mm is not
  533. * set, if so charge the init_mm (happens for pagecache usage).
  534. */
  535. if (!mm)
  536. mm = &init_mm;
  537. rcu_read_lock();
  538. mem = rcu_dereference(mm->mem_cgroup);
  539. /*
  540. * For every charge from the cgroup, increment reference
  541. * count
  542. */
  543. css_get(&mem->css);
  544. rcu_read_unlock();
  545. /*
  546. * If we created the page_cgroup, we should free it on exceeding
  547. * the cgroup limit.
  548. */
  549. while (res_counter_charge(&mem->res, PAGE_SIZE)) {
  550. if (!(gfp_mask & __GFP_WAIT))
  551. goto out;
  552. if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
  553. continue;
  554. /*
  555. * try_to_free_mem_cgroup_pages() might not give us a full
  556. * picture of reclaim. Some pages are reclaimed and might be
  557. * moved to swap cache or just unmapped from the cgroup.
  558. * Check the limit again to see if the reclaim reduced the
  559. * current usage of the cgroup before giving up
  560. */
  561. if (res_counter_check_under_limit(&mem->res))
  562. continue;
  563. if (!nr_retries--) {
  564. mem_cgroup_out_of_memory(mem, gfp_mask);
  565. goto out;
  566. }
  567. congestion_wait(WRITE, HZ/10);
  568. }
  569. atomic_set(&pc->ref_cnt, 1);
  570. pc->mem_cgroup = mem;
  571. pc->page = page;
  572. pc->flags = PAGE_CGROUP_FLAG_ACTIVE;
  573. if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE)
  574. pc->flags |= PAGE_CGROUP_FLAG_CACHE;
  575. if (!page || page_cgroup_assign_new_page_cgroup(page, pc)) {
  576. /*
  577. * Another charge has been added to this page already.
  578. * We take lock_page_cgroup(page) again and read
  579. * page->cgroup, increment refcnt.... just retry is OK.
  580. */
  581. res_counter_uncharge(&mem->res, PAGE_SIZE);
  582. css_put(&mem->css);
  583. kfree(pc);
  584. if (!page)
  585. goto done;
  586. goto retry;
  587. }
  588. spin_lock_irqsave(&mem->lru_lock, flags);
  589. /* Update statistics vector */
  590. __mem_cgroup_add_list(pc);
  591. spin_unlock_irqrestore(&mem->lru_lock, flags);
  592. done:
  593. return 0;
  594. out:
  595. css_put(&mem->css);
  596. kfree(pc);
  597. err:
  598. return -ENOMEM;
  599. }
  600. int mem_cgroup_charge(struct page *page, struct mm_struct *mm,
  601. gfp_t gfp_mask)
  602. {
  603. return mem_cgroup_charge_common(page, mm, gfp_mask,
  604. MEM_CGROUP_CHARGE_TYPE_MAPPED);
  605. }
  606. /*
  607. * See if the cached pages should be charged at all?
  608. */
  609. int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
  610. gfp_t gfp_mask)
  611. {
  612. int ret = 0;
  613. struct mem_cgroup *mem;
  614. if (!mm)
  615. mm = &init_mm;
  616. rcu_read_lock();
  617. mem = rcu_dereference(mm->mem_cgroup);
  618. css_get(&mem->css);
  619. rcu_read_unlock();
  620. if (mem->control_type == MEM_CGROUP_TYPE_ALL)
  621. ret = mem_cgroup_charge_common(page, mm, gfp_mask,
  622. MEM_CGROUP_CHARGE_TYPE_CACHE);
  623. css_put(&mem->css);
  624. return ret;
  625. }
  626. /*
  627. * Uncharging is always a welcome operation, we never complain, simply
  628. * uncharge.
  629. */
  630. void mem_cgroup_uncharge(struct page_cgroup *pc)
  631. {
  632. struct mem_cgroup *mem;
  633. struct page *page;
  634. unsigned long flags;
  635. /*
  636. * This can handle cases when a page is not charged at all and we
  637. * are switching between handling the control_type.
  638. */
  639. if (!pc)
  640. return;
  641. if (atomic_dec_and_test(&pc->ref_cnt)) {
  642. page = pc->page;
  643. /*
  644. * get page->cgroup and clear it under lock.
  645. * force_empty can drop page->cgroup without checking refcnt.
  646. */
  647. if (clear_page_cgroup(page, pc) == pc) {
  648. mem = pc->mem_cgroup;
  649. css_put(&mem->css);
  650. res_counter_uncharge(&mem->res, PAGE_SIZE);
  651. spin_lock_irqsave(&mem->lru_lock, flags);
  652. __mem_cgroup_remove_list(pc);
  653. spin_unlock_irqrestore(&mem->lru_lock, flags);
  654. kfree(pc);
  655. }
  656. }
  657. }
  658. /*
  659. * Returns non-zero if a page (under migration) has valid page_cgroup member.
  660. * Refcnt of page_cgroup is incremented.
  661. */
  662. int mem_cgroup_prepare_migration(struct page *page)
  663. {
  664. struct page_cgroup *pc;
  665. int ret = 0;
  666. lock_page_cgroup(page);
  667. pc = page_get_page_cgroup(page);
  668. if (pc && atomic_inc_not_zero(&pc->ref_cnt))
  669. ret = 1;
  670. unlock_page_cgroup(page);
  671. return ret;
  672. }
  673. void mem_cgroup_end_migration(struct page *page)
  674. {
  675. struct page_cgroup *pc = page_get_page_cgroup(page);
  676. mem_cgroup_uncharge(pc);
  677. }
  678. /*
  679. * We know both *page* and *newpage* are now not-on-LRU and Pg_locked.
  680. * And no race with uncharge() routines because page_cgroup for *page*
  681. * has extra one reference by mem_cgroup_prepare_migration.
  682. */
  683. void mem_cgroup_page_migration(struct page *page, struct page *newpage)
  684. {
  685. struct page_cgroup *pc;
  686. struct mem_cgroup *mem;
  687. unsigned long flags;
  688. retry:
  689. pc = page_get_page_cgroup(page);
  690. if (!pc)
  691. return;
  692. mem = pc->mem_cgroup;
  693. if (clear_page_cgroup(page, pc) != pc)
  694. goto retry;
  695. spin_lock_irqsave(&mem->lru_lock, flags);
  696. __mem_cgroup_remove_list(pc);
  697. pc->page = newpage;
  698. lock_page_cgroup(newpage);
  699. page_assign_page_cgroup(newpage, pc);
  700. unlock_page_cgroup(newpage);
  701. __mem_cgroup_add_list(pc);
  702. spin_unlock_irqrestore(&mem->lru_lock, flags);
  703. return;
  704. }
  705. /*
  706. * This routine traverse page_cgroup in given list and drop them all.
  707. * This routine ignores page_cgroup->ref_cnt.
  708. * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
  709. */
  710. #define FORCE_UNCHARGE_BATCH (128)
  711. static void
  712. mem_cgroup_force_empty_list(struct mem_cgroup *mem, struct list_head *list)
  713. {
  714. struct page_cgroup *pc;
  715. struct page *page;
  716. int count;
  717. unsigned long flags;
  718. if (list_empty(list))
  719. return;
  720. retry:
  721. count = FORCE_UNCHARGE_BATCH;
  722. spin_lock_irqsave(&mem->lru_lock, flags);
  723. while (--count && !list_empty(list)) {
  724. pc = list_entry(list->prev, struct page_cgroup, lru);
  725. page = pc->page;
  726. /* Avoid race with charge */
  727. atomic_set(&pc->ref_cnt, 0);
  728. if (clear_page_cgroup(page, pc) == pc) {
  729. css_put(&mem->css);
  730. res_counter_uncharge(&mem->res, PAGE_SIZE);
  731. __mem_cgroup_remove_list(pc);
  732. kfree(pc);
  733. } else /* being uncharged ? ...do relax */
  734. break;
  735. }
  736. spin_unlock_irqrestore(&mem->lru_lock, flags);
  737. if (!list_empty(list)) {
  738. cond_resched();
  739. goto retry;
  740. }
  741. return;
  742. }
  743. /*
  744. * make mem_cgroup's charge to be 0 if there is no task.
  745. * This enables deleting this mem_cgroup.
  746. */
  747. int mem_cgroup_force_empty(struct mem_cgroup *mem)
  748. {
  749. int ret = -EBUSY;
  750. int node, zid;
  751. css_get(&mem->css);
  752. /*
  753. * page reclaim code (kswapd etc..) will move pages between
  754. ` * active_list <-> inactive_list while we don't take a lock.
  755. * So, we have to do loop here until all lists are empty.
  756. */
  757. while (mem->res.usage > 0) {
  758. if (atomic_read(&mem->css.cgroup->count) > 0)
  759. goto out;
  760. for_each_node_state(node, N_POSSIBLE)
  761. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  762. struct mem_cgroup_per_zone *mz;
  763. mz = mem_cgroup_zoneinfo(mem, node, zid);
  764. /* drop all page_cgroup in active_list */
  765. mem_cgroup_force_empty_list(mem,
  766. &mz->active_list);
  767. /* drop all page_cgroup in inactive_list */
  768. mem_cgroup_force_empty_list(mem,
  769. &mz->inactive_list);
  770. }
  771. }
  772. ret = 0;
  773. out:
  774. css_put(&mem->css);
  775. return ret;
  776. }
  777. int mem_cgroup_write_strategy(char *buf, unsigned long long *tmp)
  778. {
  779. *tmp = memparse(buf, &buf);
  780. if (*buf != '\0')
  781. return -EINVAL;
  782. /*
  783. * Round up the value to the closest page size
  784. */
  785. *tmp = ((*tmp + PAGE_SIZE - 1) >> PAGE_SHIFT) << PAGE_SHIFT;
  786. return 0;
  787. }
  788. static ssize_t mem_cgroup_read(struct cgroup *cont,
  789. struct cftype *cft, struct file *file,
  790. char __user *userbuf, size_t nbytes, loff_t *ppos)
  791. {
  792. return res_counter_read(&mem_cgroup_from_cont(cont)->res,
  793. cft->private, userbuf, nbytes, ppos,
  794. NULL);
  795. }
  796. static ssize_t mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
  797. struct file *file, const char __user *userbuf,
  798. size_t nbytes, loff_t *ppos)
  799. {
  800. return res_counter_write(&mem_cgroup_from_cont(cont)->res,
  801. cft->private, userbuf, nbytes, ppos,
  802. mem_cgroup_write_strategy);
  803. }
  804. static ssize_t mem_control_type_write(struct cgroup *cont,
  805. struct cftype *cft, struct file *file,
  806. const char __user *userbuf,
  807. size_t nbytes, loff_t *pos)
  808. {
  809. int ret;
  810. char *buf, *end;
  811. unsigned long tmp;
  812. struct mem_cgroup *mem;
  813. mem = mem_cgroup_from_cont(cont);
  814. buf = kmalloc(nbytes + 1, GFP_KERNEL);
  815. ret = -ENOMEM;
  816. if (buf == NULL)
  817. goto out;
  818. buf[nbytes] = 0;
  819. ret = -EFAULT;
  820. if (copy_from_user(buf, userbuf, nbytes))
  821. goto out_free;
  822. ret = -EINVAL;
  823. tmp = simple_strtoul(buf, &end, 10);
  824. if (*end != '\0')
  825. goto out_free;
  826. if (tmp <= MEM_CGROUP_TYPE_UNSPEC || tmp >= MEM_CGROUP_TYPE_MAX)
  827. goto out_free;
  828. mem->control_type = tmp;
  829. ret = nbytes;
  830. out_free:
  831. kfree(buf);
  832. out:
  833. return ret;
  834. }
  835. static ssize_t mem_control_type_read(struct cgroup *cont,
  836. struct cftype *cft,
  837. struct file *file, char __user *userbuf,
  838. size_t nbytes, loff_t *ppos)
  839. {
  840. unsigned long val;
  841. char buf[64], *s;
  842. struct mem_cgroup *mem;
  843. mem = mem_cgroup_from_cont(cont);
  844. s = buf;
  845. val = mem->control_type;
  846. s += sprintf(s, "%lu\n", val);
  847. return simple_read_from_buffer((void __user *)userbuf, nbytes,
  848. ppos, buf, s - buf);
  849. }
  850. static ssize_t mem_force_empty_write(struct cgroup *cont,
  851. struct cftype *cft, struct file *file,
  852. const char __user *userbuf,
  853. size_t nbytes, loff_t *ppos)
  854. {
  855. struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
  856. int ret;
  857. ret = mem_cgroup_force_empty(mem);
  858. if (!ret)
  859. ret = nbytes;
  860. return ret;
  861. }
  862. /*
  863. * Note: This should be removed if cgroup supports write-only file.
  864. */
  865. static ssize_t mem_force_empty_read(struct cgroup *cont,
  866. struct cftype *cft,
  867. struct file *file, char __user *userbuf,
  868. size_t nbytes, loff_t *ppos)
  869. {
  870. return -EINVAL;
  871. }
  872. static const struct mem_cgroup_stat_desc {
  873. const char *msg;
  874. u64 unit;
  875. } mem_cgroup_stat_desc[] = {
  876. [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
  877. [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
  878. };
  879. static int mem_control_stat_show(struct seq_file *m, void *arg)
  880. {
  881. struct cgroup *cont = m->private;
  882. struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
  883. struct mem_cgroup_stat *stat = &mem_cont->stat;
  884. int i;
  885. for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
  886. s64 val;
  887. val = mem_cgroup_read_stat(stat, i);
  888. val *= mem_cgroup_stat_desc[i].unit;
  889. seq_printf(m, "%s %lld\n", mem_cgroup_stat_desc[i].msg,
  890. (long long)val);
  891. }
  892. /* showing # of active pages */
  893. {
  894. unsigned long active, inactive;
  895. inactive = mem_cgroup_get_all_zonestat(mem_cont,
  896. MEM_CGROUP_ZSTAT_INACTIVE);
  897. active = mem_cgroup_get_all_zonestat(mem_cont,
  898. MEM_CGROUP_ZSTAT_ACTIVE);
  899. seq_printf(m, "active %ld\n", (active) * PAGE_SIZE);
  900. seq_printf(m, "inactive %ld\n", (inactive) * PAGE_SIZE);
  901. }
  902. return 0;
  903. }
  904. static const struct file_operations mem_control_stat_file_operations = {
  905. .read = seq_read,
  906. .llseek = seq_lseek,
  907. .release = single_release,
  908. };
  909. static int mem_control_stat_open(struct inode *unused, struct file *file)
  910. {
  911. /* XXX __d_cont */
  912. struct cgroup *cont = file->f_dentry->d_parent->d_fsdata;
  913. file->f_op = &mem_control_stat_file_operations;
  914. return single_open(file, mem_control_stat_show, cont);
  915. }
  916. static struct cftype mem_cgroup_files[] = {
  917. {
  918. .name = "usage_in_bytes",
  919. .private = RES_USAGE,
  920. .read = mem_cgroup_read,
  921. },
  922. {
  923. .name = "limit_in_bytes",
  924. .private = RES_LIMIT,
  925. .write = mem_cgroup_write,
  926. .read = mem_cgroup_read,
  927. },
  928. {
  929. .name = "failcnt",
  930. .private = RES_FAILCNT,
  931. .read = mem_cgroup_read,
  932. },
  933. {
  934. .name = "control_type",
  935. .write = mem_control_type_write,
  936. .read = mem_control_type_read,
  937. },
  938. {
  939. .name = "force_empty",
  940. .write = mem_force_empty_write,
  941. .read = mem_force_empty_read,
  942. },
  943. {
  944. .name = "stat",
  945. .open = mem_control_stat_open,
  946. },
  947. };
  948. static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  949. {
  950. struct mem_cgroup_per_node *pn;
  951. struct mem_cgroup_per_zone *mz;
  952. int zone;
  953. /*
  954. * This routine is called against possible nodes.
  955. * But it's BUG to call kmalloc() against offline node.
  956. *
  957. * TODO: this routine can waste much memory for nodes which will
  958. * never be onlined. It's better to use memory hotplug callback
  959. * function.
  960. */
  961. if (node_state(node, N_HIGH_MEMORY))
  962. pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, node);
  963. else
  964. pn = kmalloc(sizeof(*pn), GFP_KERNEL);
  965. if (!pn)
  966. return 1;
  967. mem->info.nodeinfo[node] = pn;
  968. memset(pn, 0, sizeof(*pn));
  969. for (zone = 0; zone < MAX_NR_ZONES; zone++) {
  970. mz = &pn->zoneinfo[zone];
  971. INIT_LIST_HEAD(&mz->active_list);
  972. INIT_LIST_HEAD(&mz->inactive_list);
  973. }
  974. return 0;
  975. }
  976. static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
  977. {
  978. kfree(mem->info.nodeinfo[node]);
  979. }
  980. static struct mem_cgroup init_mem_cgroup;
  981. static struct cgroup_subsys_state *
  982. mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
  983. {
  984. struct mem_cgroup *mem;
  985. int node;
  986. if (unlikely((cont->parent) == NULL)) {
  987. mem = &init_mem_cgroup;
  988. init_mm.mem_cgroup = mem;
  989. } else
  990. mem = kzalloc(sizeof(struct mem_cgroup), GFP_KERNEL);
  991. if (mem == NULL)
  992. return NULL;
  993. res_counter_init(&mem->res);
  994. spin_lock_init(&mem->lru_lock);
  995. mem->control_type = MEM_CGROUP_TYPE_ALL;
  996. memset(&mem->info, 0, sizeof(mem->info));
  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. kfree(mem);
  1006. return NULL;
  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. kfree(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. if (mem == old_mem)
  1042. goto out;
  1043. /*
  1044. * Only thread group leaders are allowed to migrate, the mm_struct is
  1045. * in effect owned by the leader
  1046. */
  1047. if (p->tgid != p->pid)
  1048. goto out;
  1049. css_get(&mem->css);
  1050. rcu_assign_pointer(mm->mem_cgroup, mem);
  1051. css_put(&old_mem->css);
  1052. out:
  1053. mmput(mm);
  1054. return;
  1055. }
  1056. struct cgroup_subsys mem_cgroup_subsys = {
  1057. .name = "memory",
  1058. .subsys_id = mem_cgroup_subsys_id,
  1059. .create = mem_cgroup_create,
  1060. .pre_destroy = mem_cgroup_pre_destroy,
  1061. .destroy = mem_cgroup_destroy,
  1062. .populate = mem_cgroup_populate,
  1063. .attach = mem_cgroup_move_task,
  1064. .early_init = 0,
  1065. };