page_cgroup.c 10 KB

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