KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 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> |
KAMEZAWA Hiroyuki | 94b6da5 | 2008-10-22 14:15:05 -0700 | [diff] [blame] | 7 | #include <linux/slab.h> |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 8 | #include <linux/memory.h> |
Paul Mundt | 4c821042 | 2008-10-22 14:14:58 -0700 | [diff] [blame] | 9 | #include <linux/vmalloc.h> |
KAMEZAWA Hiroyuki | 94b6da5 | 2008-10-22 14:15:05 -0700 | [diff] [blame] | 10 | #include <linux/cgroup.h> |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 11 | #include <linux/swapops.h> |
Catalin Marinas | 7952f98 | 2010-07-19 11:54:14 +0100 | [diff] [blame] | 12 | #include <linux/kmemleak.h> |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 13 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 14 | static unsigned long total_usage; |
| 15 | |
| 16 | #if !defined(CONFIG_SPARSEMEM) |
| 17 | |
| 18 | |
Al Viro | 3116848 | 2008-11-22 17:33:24 +0000 | [diff] [blame] | 19 | void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat) |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 20 | { |
| 21 | pgdat->node_page_cgroup = NULL; |
| 22 | } |
| 23 | |
| 24 | struct page_cgroup *lookup_page_cgroup(struct page *page) |
| 25 | { |
| 26 | unsigned long pfn = page_to_pfn(page); |
| 27 | unsigned long offset; |
| 28 | struct page_cgroup *base; |
| 29 | |
| 30 | base = NODE_DATA(page_to_nid(page))->node_page_cgroup; |
Johannes Weiner | 00c54c0 | 2012-01-12 17:18:40 -0800 | [diff] [blame] | 31 | #ifdef CONFIG_DEBUG_VM |
| 32 | /* |
| 33 | * The sanity checks the page allocator does upon freeing a |
| 34 | * page can reach here before the page_cgroup arrays are |
| 35 | * allocated when feeding a range of pages to the allocator |
| 36 | * for the first time during bootup or memory hotplug. |
| 37 | */ |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 38 | if (unlikely(!base)) |
| 39 | return NULL; |
Johannes Weiner | 00c54c0 | 2012-01-12 17:18:40 -0800 | [diff] [blame] | 40 | #endif |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 41 | offset = pfn - NODE_DATA(page_to_nid(page))->node_start_pfn; |
| 42 | return base + offset; |
| 43 | } |
| 44 | |
| 45 | static int __init alloc_node_page_cgroup(int nid) |
| 46 | { |
Johannes Weiner | 6b208e3 | 2012-01-12 17:18:18 -0800 | [diff] [blame] | 47 | struct page_cgroup *base; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 48 | unsigned long table_size; |
Johannes Weiner | 6b208e3 | 2012-01-12 17:18:18 -0800 | [diff] [blame] | 49 | unsigned long nr_pages; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 50 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 51 | nr_pages = NODE_DATA(nid)->node_spanned_pages; |
KAMEZAWA Hiroyuki | 653d22c | 2008-12-09 13:14:20 -0800 | [diff] [blame] | 52 | if (!nr_pages) |
| 53 | return 0; |
| 54 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 55 | table_size = sizeof(struct page_cgroup) * nr_pages; |
KAMEZAWA Hiroyuki | ca371c0 | 2009-06-12 10:33:53 +0300 | [diff] [blame] | 56 | |
Grygorii Strashko | 0d036e9 | 2014-01-21 15:50:38 -0800 | [diff] [blame] | 57 | base = memblock_virt_alloc_try_nid_nopanic( |
| 58 | table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS), |
| 59 | BOOTMEM_ALLOC_ACCESSIBLE, nid); |
KAMEZAWA Hiroyuki | ca371c0 | 2009-06-12 10:33:53 +0300 | [diff] [blame] | 60 | if (!base) |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 61 | return -ENOMEM; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 62 | NODE_DATA(nid)->node_page_cgroup = base; |
| 63 | total_usage += table_size; |
| 64 | return 0; |
| 65 | } |
| 66 | |
KAMEZAWA Hiroyuki | ca371c0 | 2009-06-12 10:33:53 +0300 | [diff] [blame] | 67 | void __init page_cgroup_init_flatmem(void) |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 68 | { |
| 69 | |
| 70 | int nid, fail; |
| 71 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 72 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 94b6da5 | 2008-10-22 14:15:05 -0700 | [diff] [blame] | 73 | return; |
| 74 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 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); |
Randy Dunlap | 8ca739e | 2009-06-17 16:26:32 -0700 | [diff] [blame] | 81 | printk(KERN_INFO "please try 'cgroup_disable=memory' option if you" |
| 82 | " don't want memory cgroups\n"); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 83 | return; |
| 84 | fail: |
Randy Dunlap | 8ca739e | 2009-06-17 16:26:32 -0700 | [diff] [blame] | 85 | printk(KERN_CRIT "allocation of page_cgroup failed.\n"); |
| 86 | printk(KERN_CRIT "please try 'cgroup_disable=memory' boot option\n"); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 87 | panic("Out of memory"); |
| 88 | } |
| 89 | |
| 90 | #else /* CONFIG_FLAT_NODE_MEM_MAP */ |
| 91 | |
| 92 | struct page_cgroup *lookup_page_cgroup(struct page *page) |
| 93 | { |
| 94 | unsigned long pfn = page_to_pfn(page); |
| 95 | struct mem_section *section = __pfn_to_section(pfn); |
Johannes Weiner | 00c54c0 | 2012-01-12 17:18:40 -0800 | [diff] [blame] | 96 | #ifdef CONFIG_DEBUG_VM |
| 97 | /* |
| 98 | * The sanity checks the page allocator does upon freeing a |
| 99 | * page can reach here before the page_cgroup arrays are |
| 100 | * allocated when feeding a range of pages to the allocator |
| 101 | * for the first time during bootup or memory hotplug. |
| 102 | */ |
Balbir Singh | d69b042 | 2009-06-17 16:26:34 -0700 | [diff] [blame] | 103 | if (!section->page_cgroup) |
| 104 | return NULL; |
Johannes Weiner | 00c54c0 | 2012-01-12 17:18:40 -0800 | [diff] [blame] | 105 | #endif |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 106 | return section->page_cgroup + pfn; |
| 107 | } |
| 108 | |
Namhyung Kim | 268433b | 2011-05-26 16:25:29 -0700 | [diff] [blame] | 109 | static void *__meminit alloc_page_cgroup(size_t size, int nid) |
Michal Hocko | dde79e0 | 2011-03-23 16:42:40 -0700 | [diff] [blame] | 110 | { |
Johannes Weiner | 6b208e3 | 2012-01-12 17:18:18 -0800 | [diff] [blame] | 111 | gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN; |
Michal Hocko | dde79e0 | 2011-03-23 16:42:40 -0700 | [diff] [blame] | 112 | void *addr = NULL; |
| 113 | |
Steven Rostedt | ff7ee93 | 2011-11-02 13:38:11 -0700 | [diff] [blame] | 114 | addr = alloc_pages_exact_nid(nid, size, flags); |
| 115 | if (addr) { |
| 116 | kmemleak_alloc(addr, size, 1, flags); |
Michal Hocko | dde79e0 | 2011-03-23 16:42:40 -0700 | [diff] [blame] | 117 | return addr; |
Steven Rostedt | ff7ee93 | 2011-11-02 13:38:11 -0700 | [diff] [blame] | 118 | } |
Michal Hocko | dde79e0 | 2011-03-23 16:42:40 -0700 | [diff] [blame] | 119 | |
| 120 | if (node_state(nid, N_HIGH_MEMORY)) |
Johannes Weiner | 6b208e3 | 2012-01-12 17:18:18 -0800 | [diff] [blame] | 121 | addr = vzalloc_node(size, nid); |
Michal Hocko | dde79e0 | 2011-03-23 16:42:40 -0700 | [diff] [blame] | 122 | else |
Johannes Weiner | 6b208e3 | 2012-01-12 17:18:18 -0800 | [diff] [blame] | 123 | addr = vzalloc(size); |
Michal Hocko | dde79e0 | 2011-03-23 16:42:40 -0700 | [diff] [blame] | 124 | |
| 125 | return addr; |
| 126 | } |
| 127 | |
KAMEZAWA Hiroyuki | 37573e8 | 2011-06-15 15:08:42 -0700 | [diff] [blame] | 128 | static int __meminit init_section_page_cgroup(unsigned long pfn, int nid) |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 129 | { |
Johannes Weiner | 6b3ae58 | 2011-03-23 16:42:30 -0700 | [diff] [blame] | 130 | struct mem_section *section; |
Johannes Weiner | 6b208e3 | 2012-01-12 17:18:18 -0800 | [diff] [blame] | 131 | struct page_cgroup *base; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 132 | unsigned long table_size; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 133 | |
Johannes Weiner | 6b208e3 | 2012-01-12 17:18:18 -0800 | [diff] [blame] | 134 | section = __pfn_to_section(pfn); |
Johannes Weiner | 6b3ae58 | 2011-03-23 16:42:30 -0700 | [diff] [blame] | 135 | |
| 136 | if (section->page_cgroup) |
| 137 | return 0; |
| 138 | |
Johannes Weiner | 6b3ae58 | 2011-03-23 16:42:30 -0700 | [diff] [blame] | 139 | table_size = sizeof(struct page_cgroup) * PAGES_PER_SECTION; |
Michal Hocko | dde79e0 | 2011-03-23 16:42:40 -0700 | [diff] [blame] | 140 | base = alloc_page_cgroup(table_size, nid); |
| 141 | |
Johannes Weiner | 6b3ae58 | 2011-03-23 16:42:30 -0700 | [diff] [blame] | 142 | /* |
| 143 | * The value stored in section->page_cgroup is (base - pfn) |
| 144 | * and it does not point to the memory block allocated above, |
| 145 | * causing kmemleak false positives. |
| 146 | */ |
| 147 | kmemleak_not_leak(base); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 148 | |
| 149 | if (!base) { |
| 150 | printk(KERN_ERR "page cgroup allocation failure\n"); |
| 151 | return -ENOMEM; |
| 152 | } |
| 153 | |
KAMEZAWA Hiroyuki | 37573e8 | 2011-06-15 15:08:42 -0700 | [diff] [blame] | 154 | /* |
| 155 | * The passed "pfn" may not be aligned to SECTION. For the calculation |
| 156 | * we need to apply a mask. |
| 157 | */ |
| 158 | pfn &= PAGE_SECTION_MASK; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 159 | section->page_cgroup = base - pfn; |
| 160 | total_usage += table_size; |
| 161 | return 0; |
| 162 | } |
| 163 | #ifdef CONFIG_MEMORY_HOTPLUG |
Bob Liu | 0efc8eb | 2012-01-12 17:19:08 -0800 | [diff] [blame] | 164 | static void free_page_cgroup(void *addr) |
| 165 | { |
| 166 | if (is_vmalloc_addr(addr)) { |
| 167 | vfree(addr); |
| 168 | } else { |
| 169 | struct page *page = virt_to_page(addr); |
| 170 | size_t table_size = |
| 171 | sizeof(struct page_cgroup) * PAGES_PER_SECTION; |
| 172 | |
| 173 | BUG_ON(PageReserved(page)); |
| 174 | free_pages_exact(addr, table_size); |
| 175 | } |
| 176 | } |
| 177 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 178 | void __free_page_cgroup(unsigned long pfn) |
| 179 | { |
| 180 | struct mem_section *ms; |
| 181 | struct page_cgroup *base; |
| 182 | |
| 183 | ms = __pfn_to_section(pfn); |
| 184 | if (!ms || !ms->page_cgroup) |
| 185 | return; |
| 186 | base = ms->page_cgroup + pfn; |
Michal Hocko | dde79e0 | 2011-03-23 16:42:40 -0700 | [diff] [blame] | 187 | free_page_cgroup(base); |
| 188 | ms->page_cgroup = NULL; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Al Viro | 3116848 | 2008-11-22 17:33:24 +0000 | [diff] [blame] | 191 | int __meminit online_page_cgroup(unsigned long start_pfn, |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 192 | unsigned long nr_pages, |
| 193 | int nid) |
| 194 | { |
| 195 | unsigned long start, end, pfn; |
| 196 | int fail = 0; |
| 197 | |
Daniel Kiper | 1bb36fb | 2011-07-25 17:12:13 -0700 | [diff] [blame] | 198 | start = SECTION_ALIGN_DOWN(start_pfn); |
| 199 | end = SECTION_ALIGN_UP(start_pfn + nr_pages); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 200 | |
KAMEZAWA Hiroyuki | 37573e8 | 2011-06-15 15:08:42 -0700 | [diff] [blame] | 201 | if (nid == -1) { |
| 202 | /* |
| 203 | * In this case, "nid" already exists and contains valid memory. |
| 204 | * "start_pfn" passed to us is a pfn which is an arg for |
| 205 | * online__pages(), and start_pfn should exist. |
| 206 | */ |
| 207 | nid = pfn_to_nid(start_pfn); |
| 208 | VM_BUG_ON(!node_state(nid, N_ONLINE)); |
| 209 | } |
| 210 | |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 211 | for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) { |
| 212 | if (!pfn_present(pfn)) |
| 213 | continue; |
KAMEZAWA Hiroyuki | 37573e8 | 2011-06-15 15:08:42 -0700 | [diff] [blame] | 214 | fail = init_section_page_cgroup(pfn, nid); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 215 | } |
| 216 | if (!fail) |
| 217 | return 0; |
| 218 | |
| 219 | /* rollback */ |
| 220 | for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) |
| 221 | __free_page_cgroup(pfn); |
| 222 | |
| 223 | return -ENOMEM; |
| 224 | } |
| 225 | |
Al Viro | 3116848 | 2008-11-22 17:33:24 +0000 | [diff] [blame] | 226 | int __meminit offline_page_cgroup(unsigned long start_pfn, |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 227 | unsigned long nr_pages, int nid) |
| 228 | { |
| 229 | unsigned long start, end, pfn; |
| 230 | |
Daniel Kiper | 1bb36fb | 2011-07-25 17:12:13 -0700 | [diff] [blame] | 231 | start = SECTION_ALIGN_DOWN(start_pfn); |
| 232 | end = SECTION_ALIGN_UP(start_pfn + nr_pages); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 233 | |
| 234 | for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) |
| 235 | __free_page_cgroup(pfn); |
| 236 | return 0; |
| 237 | |
| 238 | } |
| 239 | |
Al Viro | 3116848 | 2008-11-22 17:33:24 +0000 | [diff] [blame] | 240 | static int __meminit page_cgroup_callback(struct notifier_block *self, |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 241 | unsigned long action, void *arg) |
| 242 | { |
| 243 | struct memory_notify *mn = arg; |
| 244 | int ret = 0; |
| 245 | switch (action) { |
| 246 | case MEM_GOING_ONLINE: |
| 247 | ret = online_page_cgroup(mn->start_pfn, |
| 248 | mn->nr_pages, mn->status_change_nid); |
| 249 | break; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 250 | case MEM_OFFLINE: |
| 251 | offline_page_cgroup(mn->start_pfn, |
| 252 | mn->nr_pages, mn->status_change_nid); |
| 253 | break; |
KAMEZAWA Hiroyuki | dc19f9d | 2008-12-01 13:13:48 -0800 | [diff] [blame] | 254 | case MEM_CANCEL_ONLINE: |
Wen Congyang | 7c72eb3 | 2012-12-11 16:00:49 -0800 | [diff] [blame] | 255 | offline_page_cgroup(mn->start_pfn, |
| 256 | mn->nr_pages, mn->status_change_nid); |
| 257 | break; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 258 | case MEM_GOING_OFFLINE: |
| 259 | break; |
| 260 | case MEM_ONLINE: |
| 261 | case MEM_CANCEL_OFFLINE: |
| 262 | break; |
| 263 | } |
KAMEZAWA Hiroyuki | dc19f9d | 2008-12-01 13:13:48 -0800 | [diff] [blame] | 264 | |
Prarit Bhargava | 5fda1bd | 2011-03-22 16:30:49 -0700 | [diff] [blame] | 265 | return notifier_from_errno(ret); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | #endif |
| 269 | |
| 270 | void __init page_cgroup_init(void) |
| 271 | { |
| 272 | unsigned long pfn; |
KAMEZAWA Hiroyuki | 37573e8 | 2011-06-15 15:08:42 -0700 | [diff] [blame] | 273 | int nid; |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 274 | |
Hirokazu Takahashi | f8d66542 | 2009-01-07 18:08:02 -0800 | [diff] [blame] | 275 | if (mem_cgroup_disabled()) |
KAMEZAWA Hiroyuki | 94b6da5 | 2008-10-22 14:15:05 -0700 | [diff] [blame] | 276 | return; |
| 277 | |
Lai Jiangshan | 31aaea4 | 2012-12-12 13:51:27 -0800 | [diff] [blame] | 278 | for_each_node_state(nid, N_MEMORY) { |
KAMEZAWA Hiroyuki | 37573e8 | 2011-06-15 15:08:42 -0700 | [diff] [blame] | 279 | unsigned long start_pfn, end_pfn; |
| 280 | |
| 281 | start_pfn = node_start_pfn(nid); |
| 282 | end_pfn = node_end_pfn(nid); |
| 283 | /* |
| 284 | * start_pfn and end_pfn may not be aligned to SECTION and the |
| 285 | * page->flags of out of node pages are not initialized. So we |
| 286 | * scan [start_pfn, the biggest section's pfn < end_pfn) here. |
| 287 | */ |
| 288 | for (pfn = start_pfn; |
| 289 | pfn < end_pfn; |
| 290 | pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) { |
| 291 | |
| 292 | if (!pfn_valid(pfn)) |
| 293 | continue; |
| 294 | /* |
| 295 | * Nodes's pfns can be overlapping. |
| 296 | * We know some arch can have a nodes layout such as |
| 297 | * -------------pfn--------------> |
| 298 | * N0 | N1 | N2 | N0 | N1 | N2|.... |
| 299 | */ |
| 300 | if (pfn_to_nid(pfn) != nid) |
| 301 | continue; |
| 302 | if (init_section_page_cgroup(pfn, nid)) |
| 303 | goto oom; |
| 304 | } |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 305 | } |
KAMEZAWA Hiroyuki | 37573e8 | 2011-06-15 15:08:42 -0700 | [diff] [blame] | 306 | hotplug_memory_notifier(page_cgroup_callback, 0); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 307 | printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage); |
KAMEZAWA Hiroyuki | 37573e8 | 2011-06-15 15:08:42 -0700 | [diff] [blame] | 308 | printk(KERN_INFO "please try 'cgroup_disable=memory' option if you " |
| 309 | "don't want memory cgroups\n"); |
| 310 | return; |
| 311 | oom: |
| 312 | printk(KERN_CRIT "try 'cgroup_disable=memory' boot option\n"); |
| 313 | panic("Out of memory"); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Al Viro | 3116848 | 2008-11-22 17:33:24 +0000 | [diff] [blame] | 316 | void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat) |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 317 | { |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | #endif |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 322 | |
| 323 | |
Andrew Morton | c255a45 | 2012-07-31 16:43:02 -0700 | [diff] [blame] | 324 | #ifdef CONFIG_MEMCG_SWAP |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 325 | |
| 326 | static DEFINE_MUTEX(swap_cgroup_mutex); |
| 327 | struct swap_cgroup_ctrl { |
| 328 | struct page **map; |
| 329 | unsigned long length; |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 330 | spinlock_t lock; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 331 | }; |
| 332 | |
H Hartley Sweeten | 61600f5 | 2011-11-02 13:38:36 -0700 | [diff] [blame] | 333 | static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES]; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 334 | |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 335 | struct swap_cgroup { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 336 | unsigned short id; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 337 | }; |
| 338 | #define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup)) |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 339 | |
| 340 | /* |
| 341 | * SwapCgroup implements "lookup" and "exchange" operations. |
| 342 | * In typical usage, this swap_cgroup is accessed via memcg's charge/uncharge |
| 343 | * against SwapCache. At swap_free(), this is accessed directly from swap. |
| 344 | * |
| 345 | * This means, |
| 346 | * - we have no race in "exchange" when we're accessed via SwapCache because |
| 347 | * SwapCache(and its swp_entry) is under lock. |
| 348 | * - When called via swap_free(), there is no user of this entry and no race. |
| 349 | * Then, we don't need lock around "exchange". |
| 350 | * |
| 351 | * TODO: we can push these buffers out to HIGHMEM. |
| 352 | */ |
| 353 | |
| 354 | /* |
| 355 | * allocate buffer for swap_cgroup. |
| 356 | */ |
| 357 | static int swap_cgroup_prepare(int type) |
| 358 | { |
| 359 | struct page *page; |
| 360 | struct swap_cgroup_ctrl *ctrl; |
| 361 | unsigned long idx, max; |
| 362 | |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 363 | ctrl = &swap_cgroup_ctrl[type]; |
| 364 | |
| 365 | for (idx = 0; idx < ctrl->length; idx++) { |
| 366 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 367 | if (!page) |
| 368 | goto not_enough_page; |
| 369 | ctrl->map[idx] = page; |
| 370 | } |
| 371 | return 0; |
| 372 | not_enough_page: |
| 373 | max = idx; |
| 374 | for (idx = 0; idx < max; idx++) |
| 375 | __free_page(ctrl->map[idx]); |
| 376 | |
| 377 | return -ENOMEM; |
| 378 | } |
| 379 | |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 380 | static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent, |
| 381 | struct swap_cgroup_ctrl **ctrlp) |
| 382 | { |
| 383 | pgoff_t offset = swp_offset(ent); |
| 384 | struct swap_cgroup_ctrl *ctrl; |
| 385 | struct page *mappage; |
Hugh Dickins | c09ff08 | 2012-03-05 20:52:55 -0800 | [diff] [blame] | 386 | struct swap_cgroup *sc; |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 387 | |
| 388 | ctrl = &swap_cgroup_ctrl[swp_type(ent)]; |
| 389 | if (ctrlp) |
| 390 | *ctrlp = ctrl; |
| 391 | |
| 392 | mappage = ctrl->map[offset / SC_PER_PAGE]; |
Hugh Dickins | c09ff08 | 2012-03-05 20:52:55 -0800 | [diff] [blame] | 393 | sc = page_address(mappage); |
| 394 | return sc + offset % SC_PER_PAGE; |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 395 | } |
| 396 | |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 397 | /** |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 398 | * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry. |
Wanpeng Li | dad7557 | 2012-06-20 12:53:01 -0700 | [diff] [blame] | 399 | * @ent: swap entry to be cmpxchged |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 400 | * @old: old id |
| 401 | * @new: new id |
| 402 | * |
| 403 | * Returns old id at success, 0 at failure. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 404 | * (There is no mem_cgroup using 0 as its id) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 405 | */ |
| 406 | unsigned short swap_cgroup_cmpxchg(swp_entry_t ent, |
| 407 | unsigned short old, unsigned short new) |
| 408 | { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 409 | struct swap_cgroup_ctrl *ctrl; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 410 | struct swap_cgroup *sc; |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 411 | unsigned long flags; |
| 412 | unsigned short retval; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 413 | |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 414 | sc = lookup_swap_cgroup(ent, &ctrl); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 415 | |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 416 | spin_lock_irqsave(&ctrl->lock, flags); |
| 417 | retval = sc->id; |
| 418 | if (retval == old) |
| 419 | sc->id = new; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 420 | else |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 421 | retval = 0; |
| 422 | spin_unlock_irqrestore(&ctrl->lock, flags); |
| 423 | return retval; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /** |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 427 | * swap_cgroup_record - record mem_cgroup for this swp_entry. |
| 428 | * @ent: swap entry to be recorded into |
Wanpeng Li | dad7557 | 2012-06-20 12:53:01 -0700 | [diff] [blame] | 429 | * @id: mem_cgroup to be recorded |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 430 | * |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 431 | * Returns old value at success, 0 at failure. |
| 432 | * (Of course, old value can be 0.) |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 433 | */ |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 434 | unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id) |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 435 | { |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 436 | struct swap_cgroup_ctrl *ctrl; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 437 | struct swap_cgroup *sc; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 438 | unsigned short old; |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 439 | unsigned long flags; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 440 | |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 441 | sc = lookup_swap_cgroup(ent, &ctrl); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 442 | |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 443 | spin_lock_irqsave(&ctrl->lock, flags); |
| 444 | old = sc->id; |
| 445 | sc->id = id; |
| 446 | spin_unlock_irqrestore(&ctrl->lock, flags); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 447 | |
| 448 | return old; |
| 449 | } |
| 450 | |
| 451 | /** |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 452 | * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 453 | * @ent: swap entry to be looked up. |
| 454 | * |
Hugh Dickins | b3ff8a2 | 2014-01-12 20:23:27 -0800 | [diff] [blame] | 455 | * Returns ID of mem_cgroup at success. 0 at failure. (0 is invalid ID) |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 456 | */ |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 457 | unsigned short lookup_swap_cgroup_id(swp_entry_t ent) |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 458 | { |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 459 | return lookup_swap_cgroup(ent, NULL)->id; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | int swap_cgroup_swapon(int type, unsigned long max_pages) |
| 463 | { |
| 464 | void *array; |
| 465 | unsigned long array_size; |
| 466 | unsigned long length; |
| 467 | struct swap_cgroup_ctrl *ctrl; |
| 468 | |
| 469 | if (!do_swap_account) |
| 470 | return 0; |
| 471 | |
Namhyung Kim | 33278f7 | 2011-05-26 16:25:30 -0700 | [diff] [blame] | 472 | length = DIV_ROUND_UP(max_pages, SC_PER_PAGE); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 473 | array_size = length * sizeof(void *); |
| 474 | |
Joe Perches | 8c1fec1 | 2011-05-28 10:36:34 -0700 | [diff] [blame] | 475 | array = vzalloc(array_size); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 476 | if (!array) |
| 477 | goto nomem; |
| 478 | |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 479 | ctrl = &swap_cgroup_ctrl[type]; |
| 480 | mutex_lock(&swap_cgroup_mutex); |
| 481 | ctrl->length = length; |
| 482 | ctrl->map = array; |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 483 | spin_lock_init(&ctrl->lock); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 484 | if (swap_cgroup_prepare(type)) { |
| 485 | /* memory shortage */ |
| 486 | ctrl->map = NULL; |
| 487 | ctrl->length = 0; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 488 | mutex_unlock(&swap_cgroup_mutex); |
Namhyung Kim | 6a5b18d | 2011-05-26 16:25:31 -0700 | [diff] [blame] | 489 | vfree(array); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 490 | goto nomem; |
| 491 | } |
| 492 | mutex_unlock(&swap_cgroup_mutex); |
| 493 | |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 494 | return 0; |
| 495 | nomem: |
| 496 | printk(KERN_INFO "couldn't allocate enough memory for swap_cgroup.\n"); |
| 497 | printk(KERN_INFO |
WANG Cong | 00a66d2 | 2011-07-25 17:12:12 -0700 | [diff] [blame] | 498 | "swap_cgroup can be disabled by swapaccount=0 boot option\n"); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 499 | return -ENOMEM; |
| 500 | } |
| 501 | |
| 502 | void swap_cgroup_swapoff(int type) |
| 503 | { |
Namhyung Kim | 6a5b18d | 2011-05-26 16:25:31 -0700 | [diff] [blame] | 504 | struct page **map; |
| 505 | unsigned long i, length; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 506 | struct swap_cgroup_ctrl *ctrl; |
| 507 | |
| 508 | if (!do_swap_account) |
| 509 | return; |
| 510 | |
| 511 | mutex_lock(&swap_cgroup_mutex); |
| 512 | ctrl = &swap_cgroup_ctrl[type]; |
Namhyung Kim | 6a5b18d | 2011-05-26 16:25:31 -0700 | [diff] [blame] | 513 | map = ctrl->map; |
| 514 | length = ctrl->length; |
| 515 | ctrl->map = NULL; |
| 516 | ctrl->length = 0; |
| 517 | mutex_unlock(&swap_cgroup_mutex); |
| 518 | |
| 519 | if (map) { |
| 520 | for (i = 0; i < length; i++) { |
| 521 | struct page *page = map[i]; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 522 | if (page) |
| 523 | __free_page(page); |
| 524 | } |
Namhyung Kim | 6a5b18d | 2011-05-26 16:25:31 -0700 | [diff] [blame] | 525 | vfree(map); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 526 | } |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | #endif |