page_cgroup.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. spinlock_t lock;
  242. };
  243. struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
  244. struct swap_cgroup {
  245. unsigned short id;
  246. };
  247. #define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup))
  248. #define SC_POS_MASK (SC_PER_PAGE - 1)
  249. /*
  250. * SwapCgroup implements "lookup" and "exchange" operations.
  251. * In typical usage, this swap_cgroup is accessed via memcg's charge/uncharge
  252. * against SwapCache. At swap_free(), this is accessed directly from swap.
  253. *
  254. * This means,
  255. * - we have no race in "exchange" when we're accessed via SwapCache because
  256. * SwapCache(and its swp_entry) is under lock.
  257. * - When called via swap_free(), there is no user of this entry and no race.
  258. * Then, we don't need lock around "exchange".
  259. *
  260. * TODO: we can push these buffers out to HIGHMEM.
  261. */
  262. /*
  263. * allocate buffer for swap_cgroup.
  264. */
  265. static int swap_cgroup_prepare(int type)
  266. {
  267. struct page *page;
  268. struct swap_cgroup_ctrl *ctrl;
  269. unsigned long idx, max;
  270. ctrl = &swap_cgroup_ctrl[type];
  271. for (idx = 0; idx < ctrl->length; idx++) {
  272. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  273. if (!page)
  274. goto not_enough_page;
  275. ctrl->map[idx] = page;
  276. }
  277. return 0;
  278. not_enough_page:
  279. max = idx;
  280. for (idx = 0; idx < max; idx++)
  281. __free_page(ctrl->map[idx]);
  282. return -ENOMEM;
  283. }
  284. /**
  285. * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
  286. * @end: swap entry to be cmpxchged
  287. * @old: old id
  288. * @new: new id
  289. *
  290. * Returns old id at success, 0 at failure.
  291. * (There is no mem_cgroup useing 0 as its id)
  292. */
  293. unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
  294. unsigned short old, unsigned short new)
  295. {
  296. int type = swp_type(ent);
  297. unsigned long offset = swp_offset(ent);
  298. unsigned long idx = offset / SC_PER_PAGE;
  299. unsigned long pos = offset & SC_POS_MASK;
  300. struct swap_cgroup_ctrl *ctrl;
  301. struct page *mappage;
  302. struct swap_cgroup *sc;
  303. unsigned long flags;
  304. unsigned short retval;
  305. ctrl = &swap_cgroup_ctrl[type];
  306. mappage = ctrl->map[idx];
  307. sc = page_address(mappage);
  308. sc += pos;
  309. spin_lock_irqsave(&ctrl->lock, flags);
  310. retval = sc->id;
  311. if (retval == old)
  312. sc->id = new;
  313. else
  314. retval = 0;
  315. spin_unlock_irqrestore(&ctrl->lock, flags);
  316. return retval;
  317. }
  318. /**
  319. * swap_cgroup_record - record mem_cgroup for this swp_entry.
  320. * @ent: swap entry to be recorded into
  321. * @mem: mem_cgroup to be recorded
  322. *
  323. * Returns old value at success, 0 at failure.
  324. * (Of course, old value can be 0.)
  325. */
  326. unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
  327. {
  328. int type = swp_type(ent);
  329. unsigned long offset = swp_offset(ent);
  330. unsigned long idx = offset / SC_PER_PAGE;
  331. unsigned long pos = offset & SC_POS_MASK;
  332. struct swap_cgroup_ctrl *ctrl;
  333. struct page *mappage;
  334. struct swap_cgroup *sc;
  335. unsigned short old;
  336. unsigned long flags;
  337. ctrl = &swap_cgroup_ctrl[type];
  338. mappage = ctrl->map[idx];
  339. sc = page_address(mappage);
  340. sc += pos;
  341. spin_lock_irqsave(&ctrl->lock, flags);
  342. old = sc->id;
  343. sc->id = id;
  344. spin_unlock_irqrestore(&ctrl->lock, flags);
  345. return old;
  346. }
  347. /**
  348. * lookup_swap_cgroup - lookup mem_cgroup tied to swap entry
  349. * @ent: swap entry to be looked up.
  350. *
  351. * Returns CSS ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
  352. */
  353. unsigned short lookup_swap_cgroup(swp_entry_t ent)
  354. {
  355. int type = swp_type(ent);
  356. unsigned long offset = swp_offset(ent);
  357. unsigned long idx = offset / SC_PER_PAGE;
  358. unsigned long pos = offset & SC_POS_MASK;
  359. struct swap_cgroup_ctrl *ctrl;
  360. struct page *mappage;
  361. struct swap_cgroup *sc;
  362. unsigned short ret;
  363. ctrl = &swap_cgroup_ctrl[type];
  364. mappage = ctrl->map[idx];
  365. sc = page_address(mappage);
  366. sc += pos;
  367. ret = sc->id;
  368. return ret;
  369. }
  370. int swap_cgroup_swapon(int type, unsigned long max_pages)
  371. {
  372. void *array;
  373. unsigned long array_size;
  374. unsigned long length;
  375. struct swap_cgroup_ctrl *ctrl;
  376. if (!do_swap_account)
  377. return 0;
  378. length = ((max_pages/SC_PER_PAGE) + 1);
  379. array_size = length * sizeof(void *);
  380. array = vmalloc(array_size);
  381. if (!array)
  382. goto nomem;
  383. memset(array, 0, array_size);
  384. ctrl = &swap_cgroup_ctrl[type];
  385. mutex_lock(&swap_cgroup_mutex);
  386. ctrl->length = length;
  387. ctrl->map = array;
  388. spin_lock_init(&ctrl->lock);
  389. if (swap_cgroup_prepare(type)) {
  390. /* memory shortage */
  391. ctrl->map = NULL;
  392. ctrl->length = 0;
  393. vfree(array);
  394. mutex_unlock(&swap_cgroup_mutex);
  395. goto nomem;
  396. }
  397. mutex_unlock(&swap_cgroup_mutex);
  398. return 0;
  399. nomem:
  400. printk(KERN_INFO "couldn't allocate enough memory for swap_cgroup.\n");
  401. printk(KERN_INFO
  402. "swap_cgroup can be disabled by noswapaccount boot option\n");
  403. return -ENOMEM;
  404. }
  405. void swap_cgroup_swapoff(int type)
  406. {
  407. int i;
  408. struct swap_cgroup_ctrl *ctrl;
  409. if (!do_swap_account)
  410. return;
  411. mutex_lock(&swap_cgroup_mutex);
  412. ctrl = &swap_cgroup_ctrl[type];
  413. if (ctrl->map) {
  414. for (i = 0; i < ctrl->length; i++) {
  415. struct page *page = ctrl->map[i];
  416. if (page)
  417. __free_page(page);
  418. }
  419. vfree(ctrl->map);
  420. ctrl->map = NULL;
  421. ctrl->length = 0;
  422. }
  423. mutex_unlock(&swap_cgroup_mutex);
  424. }
  425. #endif