memcontrol.c 29 KB

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