page_cgroup.c 12 KB

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