page_cgroup.c 11 KB

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