page_cgroup.c 12 KB

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