page_cgroup.c 10 KB

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