page_cgroup.c 11 KB

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