page_cgroup.c 12 KB

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