page_cgroup.c 12 KB

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