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