page_cgroup.c 10 KB

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