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