page_cgroup.c 13 KB

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