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