page_cgroup.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. #include <linux/mm.h>
  2. #include <linux/mmzone.h>
  3. #include <linux/bootmem.h>
  4. #include <linux/bit_spinlock.h>
  5. #include <linux/page_cgroup.h>
  6. #include <linux/hash.h>
  7. #include <linux/slab.h>
  8. #include <linux/memory.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/cgroup.h>
  11. #include <linux/swapops.h>
  12. static void __meminit
  13. __init_page_cgroup(struct page_cgroup *pc, unsigned long pfn)
  14. {
  15. pc->flags = 0;
  16. pc->mem_cgroup = NULL;
  17. pc->page = pfn_to_page(pfn);
  18. INIT_LIST_HEAD(&pc->lru);
  19. }
  20. static unsigned long total_usage;
  21. #if !defined(CONFIG_SPARSEMEM)
  22. void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
  23. {
  24. pgdat->node_page_cgroup = NULL;
  25. }
  26. struct page_cgroup *lookup_page_cgroup(struct page *page)
  27. {
  28. unsigned long pfn = page_to_pfn(page);
  29. unsigned long offset;
  30. struct page_cgroup *base;
  31. base = NODE_DATA(page_to_nid(page))->node_page_cgroup;
  32. if (unlikely(!base))
  33. return NULL;
  34. offset = pfn - NODE_DATA(page_to_nid(page))->node_start_pfn;
  35. return base + offset;
  36. }
  37. static int __init alloc_node_page_cgroup(int nid)
  38. {
  39. struct page_cgroup *base, *pc;
  40. unsigned long table_size;
  41. unsigned long start_pfn, nr_pages, index;
  42. start_pfn = NODE_DATA(nid)->node_start_pfn;
  43. nr_pages = NODE_DATA(nid)->node_spanned_pages;
  44. if (!nr_pages)
  45. return 0;
  46. table_size = sizeof(struct page_cgroup) * nr_pages;
  47. base = __alloc_bootmem_node_nopanic(NODE_DATA(nid),
  48. table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
  49. if (!base)
  50. return -ENOMEM;
  51. for (index = 0; index < nr_pages; index++) {
  52. pc = base + index;
  53. __init_page_cgroup(pc, start_pfn + index);
  54. }
  55. NODE_DATA(nid)->node_page_cgroup = base;
  56. total_usage += table_size;
  57. return 0;
  58. }
  59. void __init page_cgroup_init_flatmem(void)
  60. {
  61. int nid, fail;
  62. if (mem_cgroup_disabled())
  63. return;
  64. for_each_online_node(nid) {
  65. fail = alloc_node_page_cgroup(nid);
  66. if (fail)
  67. goto fail;
  68. }
  69. printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
  70. printk(KERN_INFO "please try 'cgroup_disable=memory' option if you"
  71. " don't want memory cgroups\n");
  72. return;
  73. fail:
  74. printk(KERN_CRIT "allocation of page_cgroup failed.\n");
  75. printk(KERN_CRIT "please try 'cgroup_disable=memory' boot option\n");
  76. panic("Out of memory");
  77. }
  78. #else /* CONFIG_FLAT_NODE_MEM_MAP */
  79. struct page_cgroup *lookup_page_cgroup(struct page *page)
  80. {
  81. unsigned long pfn = page_to_pfn(page);
  82. struct mem_section *section = __pfn_to_section(pfn);
  83. if (!section->page_cgroup)
  84. return NULL;
  85. return section->page_cgroup + pfn;
  86. }
  87. /* __alloc_bootmem...() is protected by !slab_available() */
  88. static int __init_refok init_section_page_cgroup(unsigned long pfn)
  89. {
  90. struct mem_section *section = __pfn_to_section(pfn);
  91. struct page_cgroup *base, *pc;
  92. unsigned long table_size;
  93. int nid, index;
  94. if (!section->page_cgroup) {
  95. nid = page_to_nid(pfn_to_page(pfn));
  96. table_size = sizeof(struct page_cgroup) * PAGES_PER_SECTION;
  97. VM_BUG_ON(!slab_is_available());
  98. base = kmalloc_node(table_size,
  99. GFP_KERNEL | __GFP_NOWARN, nid);
  100. if (!base)
  101. base = vmalloc_node(table_size, nid);
  102. } else {
  103. /*
  104. * We don't have to allocate page_cgroup again, but
  105. * address of memmap may be changed. So, we have to initialize
  106. * again.
  107. */
  108. base = section->page_cgroup + pfn;
  109. table_size = 0;
  110. /* check address of memmap is changed or not. */
  111. if (base->page == pfn_to_page(pfn))
  112. return 0;
  113. }
  114. if (!base) {
  115. printk(KERN_ERR "page cgroup allocation failure\n");
  116. return -ENOMEM;
  117. }
  118. for (index = 0; index < PAGES_PER_SECTION; index++) {
  119. pc = base + index;
  120. __init_page_cgroup(pc, pfn + index);
  121. }
  122. section->page_cgroup = base - pfn;
  123. total_usage += table_size;
  124. return 0;
  125. }
  126. #ifdef CONFIG_MEMORY_HOTPLUG
  127. void __free_page_cgroup(unsigned long pfn)
  128. {
  129. struct mem_section *ms;
  130. struct page_cgroup *base;
  131. ms = __pfn_to_section(pfn);
  132. if (!ms || !ms->page_cgroup)
  133. return;
  134. base = ms->page_cgroup + pfn;
  135. if (is_vmalloc_addr(base)) {
  136. vfree(base);
  137. ms->page_cgroup = NULL;
  138. } else {
  139. struct page *page = virt_to_page(base);
  140. if (!PageReserved(page)) { /* Is bootmem ? */
  141. kfree(base);
  142. ms->page_cgroup = NULL;
  143. }
  144. }
  145. }
  146. int __meminit online_page_cgroup(unsigned long start_pfn,
  147. unsigned long nr_pages,
  148. int nid)
  149. {
  150. unsigned long start, end, pfn;
  151. int fail = 0;
  152. start = start_pfn & ~(PAGES_PER_SECTION - 1);
  153. end = ALIGN(start_pfn + nr_pages, PAGES_PER_SECTION);
  154. for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) {
  155. if (!pfn_present(pfn))
  156. continue;
  157. fail = init_section_page_cgroup(pfn);
  158. }
  159. if (!fail)
  160. return 0;
  161. /* rollback */
  162. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
  163. __free_page_cgroup(pfn);
  164. return -ENOMEM;
  165. }
  166. int __meminit offline_page_cgroup(unsigned long start_pfn,
  167. unsigned long nr_pages, int nid)
  168. {
  169. unsigned long start, end, pfn;
  170. start = start_pfn & ~(PAGES_PER_SECTION - 1);
  171. end = ALIGN(start_pfn + nr_pages, PAGES_PER_SECTION);
  172. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
  173. __free_page_cgroup(pfn);
  174. return 0;
  175. }
  176. static int __meminit page_cgroup_callback(struct notifier_block *self,
  177. unsigned long action, void *arg)
  178. {
  179. struct memory_notify *mn = arg;
  180. int ret = 0;
  181. switch (action) {
  182. case MEM_GOING_ONLINE:
  183. ret = online_page_cgroup(mn->start_pfn,
  184. mn->nr_pages, mn->status_change_nid);
  185. break;
  186. case MEM_OFFLINE:
  187. offline_page_cgroup(mn->start_pfn,
  188. mn->nr_pages, mn->status_change_nid);
  189. break;
  190. case MEM_CANCEL_ONLINE:
  191. case MEM_GOING_OFFLINE:
  192. break;
  193. case MEM_ONLINE:
  194. case MEM_CANCEL_OFFLINE:
  195. break;
  196. }
  197. if (ret)
  198. ret = notifier_from_errno(ret);
  199. else
  200. ret = NOTIFY_OK;
  201. return ret;
  202. }
  203. #endif
  204. void __init page_cgroup_init(void)
  205. {
  206. unsigned long pfn;
  207. int fail = 0;
  208. if (mem_cgroup_disabled())
  209. return;
  210. for (pfn = 0; !fail && pfn < max_pfn; pfn += PAGES_PER_SECTION) {
  211. if (!pfn_present(pfn))
  212. continue;
  213. fail = init_section_page_cgroup(pfn);
  214. }
  215. if (fail) {
  216. printk(KERN_CRIT "try 'cgroup_disable=memory' boot option\n");
  217. panic("Out of memory");
  218. } else {
  219. hotplug_memory_notifier(page_cgroup_callback, 0);
  220. }
  221. printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
  222. printk(KERN_INFO "please try 'cgroup_disable=memory' option if you don't"
  223. " want memory cgroups\n");
  224. }
  225. void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
  226. {
  227. return;
  228. }
  229. #endif
  230. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  231. static DEFINE_MUTEX(swap_cgroup_mutex);
  232. struct swap_cgroup_ctrl {
  233. struct page **map;
  234. unsigned long length;
  235. };
  236. struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
  237. struct swap_cgroup {
  238. unsigned short id;
  239. };
  240. #define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup))
  241. #define SC_POS_MASK (SC_PER_PAGE - 1)
  242. /*
  243. * SwapCgroup implements "lookup" and "exchange" operations.
  244. * In typical usage, this swap_cgroup is accessed via memcg's charge/uncharge
  245. * against SwapCache. At swap_free(), this is accessed directly from swap.
  246. *
  247. * This means,
  248. * - we have no race in "exchange" when we're accessed via SwapCache because
  249. * SwapCache(and its swp_entry) is under lock.
  250. * - When called via swap_free(), there is no user of this entry and no race.
  251. * Then, we don't need lock around "exchange".
  252. *
  253. * TODO: we can push these buffers out to HIGHMEM.
  254. */
  255. /*
  256. * allocate buffer for swap_cgroup.
  257. */
  258. static int swap_cgroup_prepare(int type)
  259. {
  260. struct page *page;
  261. struct swap_cgroup_ctrl *ctrl;
  262. unsigned long idx, max;
  263. ctrl = &swap_cgroup_ctrl[type];
  264. for (idx = 0; idx < ctrl->length; idx++) {
  265. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  266. if (!page)
  267. goto not_enough_page;
  268. ctrl->map[idx] = page;
  269. }
  270. return 0;
  271. not_enough_page:
  272. max = idx;
  273. for (idx = 0; idx < max; idx++)
  274. __free_page(ctrl->map[idx]);
  275. return -ENOMEM;
  276. }
  277. /**
  278. * swap_cgroup_record - record mem_cgroup for this swp_entry.
  279. * @ent: swap entry to be recorded into
  280. * @mem: mem_cgroup to be recorded
  281. *
  282. * Returns old value at success, 0 at failure.
  283. * (Of course, old value can be 0.)
  284. */
  285. unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
  286. {
  287. int type = swp_type(ent);
  288. unsigned long offset = swp_offset(ent);
  289. unsigned long idx = offset / SC_PER_PAGE;
  290. unsigned long pos = offset & SC_POS_MASK;
  291. struct swap_cgroup_ctrl *ctrl;
  292. struct page *mappage;
  293. struct swap_cgroup *sc;
  294. unsigned short old;
  295. ctrl = &swap_cgroup_ctrl[type];
  296. mappage = ctrl->map[idx];
  297. sc = page_address(mappage);
  298. sc += pos;
  299. old = sc->id;
  300. sc->id = id;
  301. return old;
  302. }
  303. /**
  304. * lookup_swap_cgroup - lookup mem_cgroup tied to swap entry
  305. * @ent: swap entry to be looked up.
  306. *
  307. * Returns CSS ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
  308. */
  309. unsigned short lookup_swap_cgroup(swp_entry_t ent)
  310. {
  311. int type = swp_type(ent);
  312. unsigned long offset = swp_offset(ent);
  313. unsigned long idx = offset / SC_PER_PAGE;
  314. unsigned long pos = offset & SC_POS_MASK;
  315. struct swap_cgroup_ctrl *ctrl;
  316. struct page *mappage;
  317. struct swap_cgroup *sc;
  318. unsigned short ret;
  319. ctrl = &swap_cgroup_ctrl[type];
  320. mappage = ctrl->map[idx];
  321. sc = page_address(mappage);
  322. sc += pos;
  323. ret = sc->id;
  324. return ret;
  325. }
  326. int swap_cgroup_swapon(int type, unsigned long max_pages)
  327. {
  328. void *array;
  329. unsigned long array_size;
  330. unsigned long length;
  331. struct swap_cgroup_ctrl *ctrl;
  332. if (!do_swap_account)
  333. return 0;
  334. length = ((max_pages/SC_PER_PAGE) + 1);
  335. array_size = length * sizeof(void *);
  336. array = vmalloc(array_size);
  337. if (!array)
  338. goto nomem;
  339. memset(array, 0, array_size);
  340. ctrl = &swap_cgroup_ctrl[type];
  341. mutex_lock(&swap_cgroup_mutex);
  342. ctrl->length = length;
  343. ctrl->map = array;
  344. if (swap_cgroup_prepare(type)) {
  345. /* memory shortage */
  346. ctrl->map = NULL;
  347. ctrl->length = 0;
  348. vfree(array);
  349. mutex_unlock(&swap_cgroup_mutex);
  350. goto nomem;
  351. }
  352. mutex_unlock(&swap_cgroup_mutex);
  353. return 0;
  354. nomem:
  355. printk(KERN_INFO "couldn't allocate enough memory for swap_cgroup.\n");
  356. printk(KERN_INFO
  357. "swap_cgroup can be disabled by noswapaccount boot option\n");
  358. return -ENOMEM;
  359. }
  360. void swap_cgroup_swapoff(int type)
  361. {
  362. int i;
  363. struct swap_cgroup_ctrl *ctrl;
  364. if (!do_swap_account)
  365. return;
  366. mutex_lock(&swap_cgroup_mutex);
  367. ctrl = &swap_cgroup_ctrl[type];
  368. if (ctrl->map) {
  369. for (i = 0; i < ctrl->length; i++) {
  370. struct page *page = ctrl->map[i];
  371. if (page)
  372. __free_page(page);
  373. }
  374. vfree(ctrl->map);
  375. ctrl->map = NULL;
  376. ctrl->length = 0;
  377. }
  378. mutex_unlock(&swap_cgroup_mutex);
  379. }
  380. #endif