page_cgroup.c 13 KB

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