page_cgroup.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. free_pages_exact(addr, table_size);
  147. }
  148. }
  149. static void __free_page_cgroup(unsigned long pfn)
  150. {
  151. struct mem_section *ms;
  152. struct page_cgroup *base;
  153. ms = __pfn_to_section(pfn);
  154. if (!ms || !ms->page_cgroup)
  155. return;
  156. base = ms->page_cgroup + pfn;
  157. free_page_cgroup(base);
  158. ms->page_cgroup = NULL;
  159. }
  160. static int __meminit online_page_cgroup(unsigned long start_pfn,
  161. unsigned long nr_pages,
  162. int nid)
  163. {
  164. unsigned long start, end, pfn;
  165. int fail = 0;
  166. start = SECTION_ALIGN_DOWN(start_pfn);
  167. end = SECTION_ALIGN_UP(start_pfn + nr_pages);
  168. if (nid == -1) {
  169. /*
  170. * In this case, "nid" already exists and contains valid memory.
  171. * "start_pfn" passed to us is a pfn which is an arg for
  172. * online__pages(), and start_pfn should exist.
  173. */
  174. nid = pfn_to_nid(start_pfn);
  175. VM_BUG_ON(!node_state(nid, N_ONLINE));
  176. }
  177. for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) {
  178. if (!pfn_present(pfn))
  179. continue;
  180. fail = init_section_page_cgroup(pfn, nid);
  181. }
  182. if (!fail)
  183. return 0;
  184. /* rollback */
  185. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
  186. __free_page_cgroup(pfn);
  187. return -ENOMEM;
  188. }
  189. static int __meminit offline_page_cgroup(unsigned long start_pfn,
  190. unsigned long nr_pages, int nid)
  191. {
  192. unsigned long start, end, pfn;
  193. start = SECTION_ALIGN_DOWN(start_pfn);
  194. end = SECTION_ALIGN_UP(start_pfn + nr_pages);
  195. for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
  196. __free_page_cgroup(pfn);
  197. return 0;
  198. }
  199. static int __meminit page_cgroup_callback(struct notifier_block *self,
  200. unsigned long action, void *arg)
  201. {
  202. struct memory_notify *mn = arg;
  203. int ret = 0;
  204. switch (action) {
  205. case MEM_GOING_ONLINE:
  206. ret = online_page_cgroup(mn->start_pfn,
  207. mn->nr_pages, mn->status_change_nid);
  208. break;
  209. case MEM_OFFLINE:
  210. offline_page_cgroup(mn->start_pfn,
  211. mn->nr_pages, mn->status_change_nid);
  212. break;
  213. case MEM_CANCEL_ONLINE:
  214. offline_page_cgroup(mn->start_pfn,
  215. mn->nr_pages, mn->status_change_nid);
  216. break;
  217. case MEM_GOING_OFFLINE:
  218. break;
  219. case MEM_ONLINE:
  220. case MEM_CANCEL_OFFLINE:
  221. break;
  222. }
  223. return notifier_from_errno(ret);
  224. }
  225. #endif
  226. void __init page_cgroup_init(void)
  227. {
  228. unsigned long pfn;
  229. int nid;
  230. if (mem_cgroup_disabled())
  231. return;
  232. for_each_node_state(nid, N_MEMORY) {
  233. unsigned long start_pfn, end_pfn;
  234. start_pfn = node_start_pfn(nid);
  235. end_pfn = node_end_pfn(nid);
  236. /*
  237. * start_pfn and end_pfn may not be aligned to SECTION and the
  238. * page->flags of out of node pages are not initialized. So we
  239. * scan [start_pfn, the biggest section's pfn < end_pfn) here.
  240. */
  241. for (pfn = start_pfn;
  242. pfn < end_pfn;
  243. pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) {
  244. if (!pfn_valid(pfn))
  245. continue;
  246. /*
  247. * Nodes's pfns can be overlapping.
  248. * We know some arch can have a nodes layout such as
  249. * -------------pfn-------------->
  250. * N0 | N1 | N2 | N0 | N1 | N2|....
  251. */
  252. if (pfn_to_nid(pfn) != nid)
  253. continue;
  254. if (init_section_page_cgroup(pfn, nid))
  255. goto oom;
  256. }
  257. }
  258. hotplug_memory_notifier(page_cgroup_callback, 0);
  259. printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
  260. printk(KERN_INFO "please try 'cgroup_disable=memory' option if you "
  261. "don't want memory cgroups\n");
  262. return;
  263. oom:
  264. printk(KERN_CRIT "try 'cgroup_disable=memory' boot option\n");
  265. panic("Out of memory");
  266. }
  267. void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
  268. {
  269. return;
  270. }
  271. #endif
  272. #ifdef CONFIG_MEMCG_SWAP
  273. static DEFINE_MUTEX(swap_cgroup_mutex);
  274. struct swap_cgroup_ctrl {
  275. struct page **map;
  276. unsigned long length;
  277. spinlock_t lock;
  278. };
  279. static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
  280. struct swap_cgroup {
  281. unsigned short id;
  282. };
  283. #define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup))
  284. /*
  285. * SwapCgroup implements "lookup" and "exchange" operations.
  286. * In typical usage, this swap_cgroup is accessed via memcg's charge/uncharge
  287. * against SwapCache. At swap_free(), this is accessed directly from swap.
  288. *
  289. * This means,
  290. * - we have no race in "exchange" when we're accessed via SwapCache because
  291. * SwapCache(and its swp_entry) is under lock.
  292. * - When called via swap_free(), there is no user of this entry and no race.
  293. * Then, we don't need lock around "exchange".
  294. *
  295. * TODO: we can push these buffers out to HIGHMEM.
  296. */
  297. /*
  298. * allocate buffer for swap_cgroup.
  299. */
  300. static int swap_cgroup_prepare(int type)
  301. {
  302. struct page *page;
  303. struct swap_cgroup_ctrl *ctrl;
  304. unsigned long idx, max;
  305. ctrl = &swap_cgroup_ctrl[type];
  306. for (idx = 0; idx < ctrl->length; idx++) {
  307. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  308. if (!page)
  309. goto not_enough_page;
  310. ctrl->map[idx] = page;
  311. }
  312. return 0;
  313. not_enough_page:
  314. max = idx;
  315. for (idx = 0; idx < max; idx++)
  316. __free_page(ctrl->map[idx]);
  317. return -ENOMEM;
  318. }
  319. static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent,
  320. struct swap_cgroup_ctrl **ctrlp)
  321. {
  322. pgoff_t offset = swp_offset(ent);
  323. struct swap_cgroup_ctrl *ctrl;
  324. struct page *mappage;
  325. struct swap_cgroup *sc;
  326. ctrl = &swap_cgroup_ctrl[swp_type(ent)];
  327. if (ctrlp)
  328. *ctrlp = ctrl;
  329. mappage = ctrl->map[offset / SC_PER_PAGE];
  330. sc = page_address(mappage);
  331. return sc + offset % SC_PER_PAGE;
  332. }
  333. /**
  334. * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
  335. * @ent: swap entry to be cmpxchged
  336. * @old: old id
  337. * @new: new id
  338. *
  339. * Returns old id at success, 0 at failure.
  340. * (There is no mem_cgroup using 0 as its id)
  341. */
  342. unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
  343. unsigned short old, unsigned short new)
  344. {
  345. struct swap_cgroup_ctrl *ctrl;
  346. struct swap_cgroup *sc;
  347. unsigned long flags;
  348. unsigned short retval;
  349. sc = lookup_swap_cgroup(ent, &ctrl);
  350. spin_lock_irqsave(&ctrl->lock, flags);
  351. retval = sc->id;
  352. if (retval == old)
  353. sc->id = new;
  354. else
  355. retval = 0;
  356. spin_unlock_irqrestore(&ctrl->lock, flags);
  357. return retval;
  358. }
  359. /**
  360. * swap_cgroup_record - record mem_cgroup for this swp_entry.
  361. * @ent: swap entry to be recorded into
  362. * @id: mem_cgroup to be recorded
  363. *
  364. * Returns old value at success, 0 at failure.
  365. * (Of course, old value can be 0.)
  366. */
  367. unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
  368. {
  369. struct swap_cgroup_ctrl *ctrl;
  370. struct swap_cgroup *sc;
  371. unsigned short old;
  372. unsigned long flags;
  373. sc = lookup_swap_cgroup(ent, &ctrl);
  374. spin_lock_irqsave(&ctrl->lock, flags);
  375. old = sc->id;
  376. sc->id = id;
  377. spin_unlock_irqrestore(&ctrl->lock, flags);
  378. return old;
  379. }
  380. /**
  381. * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry
  382. * @ent: swap entry to be looked up.
  383. *
  384. * Returns ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
  385. */
  386. unsigned short lookup_swap_cgroup_id(swp_entry_t ent)
  387. {
  388. return lookup_swap_cgroup(ent, NULL)->id;
  389. }
  390. int swap_cgroup_swapon(int type, unsigned long max_pages)
  391. {
  392. void *array;
  393. unsigned long array_size;
  394. unsigned long length;
  395. struct swap_cgroup_ctrl *ctrl;
  396. if (!do_swap_account)
  397. return 0;
  398. length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
  399. array_size = length * sizeof(void *);
  400. array = vzalloc(array_size);
  401. if (!array)
  402. goto nomem;
  403. ctrl = &swap_cgroup_ctrl[type];
  404. mutex_lock(&swap_cgroup_mutex);
  405. ctrl->length = length;
  406. ctrl->map = array;
  407. spin_lock_init(&ctrl->lock);
  408. if (swap_cgroup_prepare(type)) {
  409. /* memory shortage */
  410. ctrl->map = NULL;
  411. ctrl->length = 0;
  412. mutex_unlock(&swap_cgroup_mutex);
  413. vfree(array);
  414. goto nomem;
  415. }
  416. mutex_unlock(&swap_cgroup_mutex);
  417. return 0;
  418. nomem:
  419. printk(KERN_INFO "couldn't allocate enough memory for swap_cgroup.\n");
  420. printk(KERN_INFO
  421. "swap_cgroup can be disabled by swapaccount=0 boot option\n");
  422. return -ENOMEM;
  423. }
  424. void swap_cgroup_swapoff(int type)
  425. {
  426. struct page **map;
  427. unsigned long i, length;
  428. struct swap_cgroup_ctrl *ctrl;
  429. if (!do_swap_account)
  430. return;
  431. mutex_lock(&swap_cgroup_mutex);
  432. ctrl = &swap_cgroup_ctrl[type];
  433. map = ctrl->map;
  434. length = ctrl->length;
  435. ctrl->map = NULL;
  436. ctrl->length = 0;
  437. mutex_unlock(&swap_cgroup_mutex);
  438. if (map) {
  439. for (i = 0; i < length; i++) {
  440. struct page *page = map[i];
  441. if (page)
  442. __free_page(page);
  443. }
  444. vfree(map);
  445. }
  446. }
  447. #endif