Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/mm/memory_hotplug.c |
| 3 | * |
| 4 | * Copyright (C) |
| 5 | */ |
| 6 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 7 | #include <linux/stddef.h> |
| 8 | #include <linux/mm.h> |
| 9 | #include <linux/swap.h> |
| 10 | #include <linux/interrupt.h> |
| 11 | #include <linux/pagemap.h> |
| 12 | #include <linux/bootmem.h> |
| 13 | #include <linux/compiler.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 15 | #include <linux/pagevec.h> |
Chandra Seetharaman | 2d1d43f | 2006-09-29 02:01:25 -0700 | [diff] [blame] | 16 | #include <linux/writeback.h> |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 17 | #include <linux/slab.h> |
| 18 | #include <linux/sysctl.h> |
| 19 | #include <linux/cpu.h> |
| 20 | #include <linux/memory.h> |
| 21 | #include <linux/memory_hotplug.h> |
| 22 | #include <linux/highmem.h> |
| 23 | #include <linux/vmalloc.h> |
KAMEZAWA Hiroyuki | 0a54703 | 2006-06-27 02:53:35 -0700 | [diff] [blame] | 24 | #include <linux/ioport.h> |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 25 | #include <linux/delay.h> |
| 26 | #include <linux/migrate.h> |
| 27 | #include <linux/page-isolation.h> |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 28 | #include <linux/pfn.h> |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 29 | #include <linux/suspend.h> |
KOSAKI Motohiro | 6d9c285 | 2009-12-14 17:58:11 -0800 | [diff] [blame] | 30 | #include <linux/mm_inline.h> |
akpm@linux-foundation.org | d96ae53 | 2010-03-05 13:41:58 -0800 | [diff] [blame] | 31 | #include <linux/firmware-map.h> |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 32 | |
| 33 | #include <asm/tlbflush.h> |
| 34 | |
Adrian Bunk | 1e5ad9a | 2008-04-28 20:40:08 +0300 | [diff] [blame] | 35 | #include "internal.h" |
| 36 | |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 37 | /* |
| 38 | * online_page_callback contains pointer to current page onlining function. |
| 39 | * Initially it is generic_online_page(). If it is required it could be |
| 40 | * changed by calling set_online_page_callback() for callback registration |
| 41 | * and restore_online_page_callback() for generic callback restore. |
| 42 | */ |
| 43 | |
| 44 | static void generic_online_page(struct page *page); |
| 45 | |
| 46 | static online_page_callback_t online_page_callback = generic_online_page; |
| 47 | |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 48 | DEFINE_MUTEX(mem_hotplug_mutex); |
| 49 | |
| 50 | void lock_memory_hotplug(void) |
| 51 | { |
| 52 | mutex_lock(&mem_hotplug_mutex); |
| 53 | |
| 54 | /* for exclusive hibernation if CONFIG_HIBERNATION=y */ |
| 55 | lock_system_sleep(); |
| 56 | } |
| 57 | |
| 58 | void unlock_memory_hotplug(void) |
| 59 | { |
| 60 | unlock_system_sleep(); |
| 61 | mutex_unlock(&mem_hotplug_mutex); |
| 62 | } |
| 63 | |
| 64 | |
Keith Mannthey | 45e0b78 | 2006-09-30 23:27:09 -0700 | [diff] [blame] | 65 | /* add this memory to iomem resource */ |
| 66 | static struct resource *register_memory_resource(u64 start, u64 size) |
| 67 | { |
| 68 | struct resource *res; |
| 69 | res = kzalloc(sizeof(struct resource), GFP_KERNEL); |
| 70 | BUG_ON(!res); |
| 71 | |
| 72 | res->name = "System RAM"; |
| 73 | res->start = start; |
| 74 | res->end = start + size - 1; |
Yasunori Goto | 887c3cb | 2007-11-14 16:59:20 -0800 | [diff] [blame] | 75 | res->flags = IORESOURCE_MEM | IORESOURCE_BUSY; |
Keith Mannthey | 45e0b78 | 2006-09-30 23:27:09 -0700 | [diff] [blame] | 76 | if (request_resource(&iomem_resource, res) < 0) { |
Bjorn Helgaas | a62e2f4 | 2012-05-29 15:06:30 -0700 | [diff] [blame] | 77 | printk("System RAM resource %pR cannot be added\n", res); |
Keith Mannthey | 45e0b78 | 2006-09-30 23:27:09 -0700 | [diff] [blame] | 78 | kfree(res); |
| 79 | res = NULL; |
| 80 | } |
| 81 | return res; |
| 82 | } |
| 83 | |
| 84 | static void release_memory_resource(struct resource *res) |
| 85 | { |
| 86 | if (!res) |
| 87 | return; |
| 88 | release_resource(res); |
| 89 | kfree(res); |
| 90 | return; |
| 91 | } |
| 92 | |
Keith Mannthey | 5394702 | 2006-09-30 23:27:08 -0700 | [diff] [blame] | 93 | #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 94 | #ifndef CONFIG_SPARSEMEM_VMEMMAP |
Andrea Arcangeli | 5f24ce5 | 2011-01-13 15:47:00 -0800 | [diff] [blame] | 95 | static void get_page_bootmem(unsigned long info, struct page *page, |
| 96 | unsigned long type) |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 97 | { |
Andrea Arcangeli | 5f24ce5 | 2011-01-13 15:47:00 -0800 | [diff] [blame] | 98 | page->lru.next = (struct list_head *) type; |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 99 | SetPagePrivate(page); |
| 100 | set_page_private(page, info); |
| 101 | atomic_inc(&page->_count); |
| 102 | } |
| 103 | |
Rakib Mullick | 23ce932 | 2009-12-14 17:59:44 -0800 | [diff] [blame] | 104 | /* reference to __meminit __free_pages_bootmem is valid |
| 105 | * so use __ref to tell modpost not to generate a warning */ |
| 106 | void __ref put_page_bootmem(struct page *page) |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 107 | { |
Andrea Arcangeli | 5f24ce5 | 2011-01-13 15:47:00 -0800 | [diff] [blame] | 108 | unsigned long type; |
Jiang Liu | 9feedc9 | 2012-12-12 13:52:12 -0800 | [diff] [blame] | 109 | static DEFINE_MUTEX(ppb_lock); |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 110 | |
Andrea Arcangeli | 5f24ce5 | 2011-01-13 15:47:00 -0800 | [diff] [blame] | 111 | type = (unsigned long) page->lru.next; |
| 112 | BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE || |
| 113 | type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE); |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 114 | |
| 115 | if (atomic_dec_return(&page->_count) == 1) { |
| 116 | ClearPagePrivate(page); |
| 117 | set_page_private(page, 0); |
Andrea Arcangeli | 5f24ce5 | 2011-01-13 15:47:00 -0800 | [diff] [blame] | 118 | INIT_LIST_HEAD(&page->lru); |
Jiang Liu | 9feedc9 | 2012-12-12 13:52:12 -0800 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | * Please refer to comment for __free_pages_bootmem() |
| 122 | * for why we serialize here. |
| 123 | */ |
| 124 | mutex_lock(&ppb_lock); |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 125 | __free_pages_bootmem(page, 0); |
Jiang Liu | 9feedc9 | 2012-12-12 13:52:12 -0800 | [diff] [blame] | 126 | mutex_unlock(&ppb_lock); |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | } |
| 130 | |
Adrian Bunk | d92bc31 | 2008-07-23 21:28:12 -0700 | [diff] [blame] | 131 | static void register_page_bootmem_info_section(unsigned long start_pfn) |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 132 | { |
| 133 | unsigned long *usemap, mapsize, section_nr, i; |
| 134 | struct mem_section *ms; |
| 135 | struct page *page, *memmap; |
| 136 | |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 137 | section_nr = pfn_to_section_nr(start_pfn); |
| 138 | ms = __nr_to_section(section_nr); |
| 139 | |
| 140 | /* Get section's memmap address */ |
| 141 | memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); |
| 142 | |
| 143 | /* |
| 144 | * Get page for the memmap's phys address |
| 145 | * XXX: need more consideration for sparse_vmemmap... |
| 146 | */ |
| 147 | page = virt_to_page(memmap); |
| 148 | mapsize = sizeof(struct page) * PAGES_PER_SECTION; |
| 149 | mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT; |
| 150 | |
| 151 | /* remember memmap's page */ |
| 152 | for (i = 0; i < mapsize; i++, page++) |
| 153 | get_page_bootmem(section_nr, page, SECTION_INFO); |
| 154 | |
| 155 | usemap = __nr_to_section(section_nr)->pageblock_flags; |
| 156 | page = virt_to_page(usemap); |
| 157 | |
| 158 | mapsize = PAGE_ALIGN(usemap_size()) >> PAGE_SHIFT; |
| 159 | |
| 160 | for (i = 0; i < mapsize; i++, page++) |
Yasunori Goto | af370fb | 2008-07-23 21:28:17 -0700 | [diff] [blame] | 161 | get_page_bootmem(section_nr, page, MIX_SECTION_INFO); |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 162 | |
| 163 | } |
| 164 | |
| 165 | void register_page_bootmem_info_node(struct pglist_data *pgdat) |
| 166 | { |
| 167 | unsigned long i, pfn, end_pfn, nr_pages; |
| 168 | int node = pgdat->node_id; |
| 169 | struct page *page; |
| 170 | struct zone *zone; |
| 171 | |
| 172 | nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT; |
| 173 | page = virt_to_page(pgdat); |
| 174 | |
| 175 | for (i = 0; i < nr_pages; i++, page++) |
| 176 | get_page_bootmem(node, page, NODE_INFO); |
| 177 | |
| 178 | zone = &pgdat->node_zones[0]; |
| 179 | for (; zone < pgdat->node_zones + MAX_NR_ZONES - 1; zone++) { |
| 180 | if (zone->wait_table) { |
| 181 | nr_pages = zone->wait_table_hash_nr_entries |
| 182 | * sizeof(wait_queue_head_t); |
| 183 | nr_pages = PAGE_ALIGN(nr_pages) >> PAGE_SHIFT; |
| 184 | page = virt_to_page(zone->wait_table); |
| 185 | |
| 186 | for (i = 0; i < nr_pages; i++, page++) |
| 187 | get_page_bootmem(node, page, NODE_INFO); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | pfn = pgdat->node_start_pfn; |
| 192 | end_pfn = pfn + pgdat->node_spanned_pages; |
| 193 | |
| 194 | /* register_section info */ |
qiuxishi | f14851a | 2012-09-17 14:09:24 -0700 | [diff] [blame] | 195 | for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) { |
| 196 | /* |
| 197 | * Some platforms can assign the same pfn to multiple nodes - on |
| 198 | * node0 as well as nodeN. To avoid registering a pfn against |
| 199 | * multiple nodes we check that this pfn does not already |
| 200 | * reside in some other node. |
| 201 | */ |
| 202 | if (pfn_valid(pfn) && (pfn_to_nid(pfn) == node)) |
| 203 | register_page_bootmem_info_section(pfn); |
| 204 | } |
Yasunori Goto | 0475327 | 2008-04-28 02:13:31 -0700 | [diff] [blame] | 205 | } |
| 206 | #endif /* !CONFIG_SPARSEMEM_VMEMMAP */ |
| 207 | |
Heiko Carstens | 76cdd58 | 2008-05-14 16:05:52 -0700 | [diff] [blame] | 208 | static void grow_zone_span(struct zone *zone, unsigned long start_pfn, |
| 209 | unsigned long end_pfn) |
| 210 | { |
| 211 | unsigned long old_zone_end_pfn; |
| 212 | |
| 213 | zone_span_writelock(zone); |
| 214 | |
| 215 | old_zone_end_pfn = zone->zone_start_pfn + zone->spanned_pages; |
Tang Chen | 712cd38 | 2012-12-11 16:01:07 -0800 | [diff] [blame] | 216 | if (!zone->spanned_pages || start_pfn < zone->zone_start_pfn) |
Heiko Carstens | 76cdd58 | 2008-05-14 16:05:52 -0700 | [diff] [blame] | 217 | zone->zone_start_pfn = start_pfn; |
| 218 | |
| 219 | zone->spanned_pages = max(old_zone_end_pfn, end_pfn) - |
| 220 | zone->zone_start_pfn; |
| 221 | |
| 222 | zone_span_writeunlock(zone); |
| 223 | } |
| 224 | |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 225 | static void resize_zone(struct zone *zone, unsigned long start_pfn, |
| 226 | unsigned long end_pfn) |
| 227 | { |
| 228 | zone_span_writelock(zone); |
| 229 | |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 230 | if (end_pfn - start_pfn) { |
| 231 | zone->zone_start_pfn = start_pfn; |
| 232 | zone->spanned_pages = end_pfn - start_pfn; |
| 233 | } else { |
| 234 | /* |
| 235 | * make it consist as free_area_init_core(), |
| 236 | * if spanned_pages = 0, then keep start_pfn = 0 |
| 237 | */ |
| 238 | zone->zone_start_pfn = 0; |
| 239 | zone->spanned_pages = 0; |
| 240 | } |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 241 | |
| 242 | zone_span_writeunlock(zone); |
| 243 | } |
| 244 | |
| 245 | static void fix_zone_id(struct zone *zone, unsigned long start_pfn, |
| 246 | unsigned long end_pfn) |
| 247 | { |
| 248 | enum zone_type zid = zone_idx(zone); |
| 249 | int nid = zone->zone_pgdat->node_id; |
| 250 | unsigned long pfn; |
| 251 | |
| 252 | for (pfn = start_pfn; pfn < end_pfn; pfn++) |
| 253 | set_page_links(pfn_to_page(pfn), zid, nid, pfn); |
| 254 | } |
| 255 | |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 256 | static int __meminit move_pfn_range_left(struct zone *z1, struct zone *z2, |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 257 | unsigned long start_pfn, unsigned long end_pfn) |
| 258 | { |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 259 | int ret; |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 260 | unsigned long flags; |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 261 | unsigned long z1_start_pfn; |
| 262 | |
| 263 | if (!z1->wait_table) { |
| 264 | ret = init_currently_empty_zone(z1, start_pfn, |
| 265 | end_pfn - start_pfn, MEMMAP_HOTPLUG); |
| 266 | if (ret) |
| 267 | return ret; |
| 268 | } |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 269 | |
| 270 | pgdat_resize_lock(z1->zone_pgdat, &flags); |
| 271 | |
| 272 | /* can't move pfns which are higher than @z2 */ |
| 273 | if (end_pfn > z2->zone_start_pfn + z2->spanned_pages) |
| 274 | goto out_fail; |
| 275 | /* the move out part mast at the left most of @z2 */ |
| 276 | if (start_pfn > z2->zone_start_pfn) |
| 277 | goto out_fail; |
| 278 | /* must included/overlap */ |
| 279 | if (end_pfn <= z2->zone_start_pfn) |
| 280 | goto out_fail; |
| 281 | |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 282 | /* use start_pfn for z1's start_pfn if z1 is empty */ |
| 283 | if (z1->spanned_pages) |
| 284 | z1_start_pfn = z1->zone_start_pfn; |
| 285 | else |
| 286 | z1_start_pfn = start_pfn; |
| 287 | |
| 288 | resize_zone(z1, z1_start_pfn, end_pfn); |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 289 | resize_zone(z2, end_pfn, z2->zone_start_pfn + z2->spanned_pages); |
| 290 | |
| 291 | pgdat_resize_unlock(z1->zone_pgdat, &flags); |
| 292 | |
| 293 | fix_zone_id(z1, start_pfn, end_pfn); |
| 294 | |
| 295 | return 0; |
| 296 | out_fail: |
| 297 | pgdat_resize_unlock(z1->zone_pgdat, &flags); |
| 298 | return -1; |
| 299 | } |
| 300 | |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 301 | static int __meminit move_pfn_range_right(struct zone *z1, struct zone *z2, |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 302 | unsigned long start_pfn, unsigned long end_pfn) |
| 303 | { |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 304 | int ret; |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 305 | unsigned long flags; |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 306 | unsigned long z2_end_pfn; |
| 307 | |
| 308 | if (!z2->wait_table) { |
| 309 | ret = init_currently_empty_zone(z2, start_pfn, |
| 310 | end_pfn - start_pfn, MEMMAP_HOTPLUG); |
| 311 | if (ret) |
| 312 | return ret; |
| 313 | } |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 314 | |
| 315 | pgdat_resize_lock(z1->zone_pgdat, &flags); |
| 316 | |
| 317 | /* can't move pfns which are lower than @z1 */ |
| 318 | if (z1->zone_start_pfn > start_pfn) |
| 319 | goto out_fail; |
| 320 | /* the move out part mast at the right most of @z1 */ |
| 321 | if (z1->zone_start_pfn + z1->spanned_pages > end_pfn) |
| 322 | goto out_fail; |
| 323 | /* must included/overlap */ |
| 324 | if (start_pfn >= z1->zone_start_pfn + z1->spanned_pages) |
| 325 | goto out_fail; |
| 326 | |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 327 | /* use end_pfn for z2's end_pfn if z2 is empty */ |
| 328 | if (z2->spanned_pages) |
| 329 | z2_end_pfn = z2->zone_start_pfn + z2->spanned_pages; |
| 330 | else |
| 331 | z2_end_pfn = end_pfn; |
| 332 | |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 333 | resize_zone(z1, z1->zone_start_pfn, start_pfn); |
Lai Jiangshan | e455a9b | 2012-12-11 16:03:20 -0800 | [diff] [blame] | 334 | resize_zone(z2, start_pfn, z2_end_pfn); |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 335 | |
| 336 | pgdat_resize_unlock(z1->zone_pgdat, &flags); |
| 337 | |
| 338 | fix_zone_id(z2, start_pfn, end_pfn); |
| 339 | |
| 340 | return 0; |
| 341 | out_fail: |
| 342 | pgdat_resize_unlock(z1->zone_pgdat, &flags); |
| 343 | return -1; |
| 344 | } |
| 345 | |
Heiko Carstens | 76cdd58 | 2008-05-14 16:05:52 -0700 | [diff] [blame] | 346 | static void grow_pgdat_span(struct pglist_data *pgdat, unsigned long start_pfn, |
| 347 | unsigned long end_pfn) |
| 348 | { |
| 349 | unsigned long old_pgdat_end_pfn = |
| 350 | pgdat->node_start_pfn + pgdat->node_spanned_pages; |
| 351 | |
Tang Chen | 712cd38 | 2012-12-11 16:01:07 -0800 | [diff] [blame] | 352 | if (!pgdat->node_spanned_pages || start_pfn < pgdat->node_start_pfn) |
Heiko Carstens | 76cdd58 | 2008-05-14 16:05:52 -0700 | [diff] [blame] | 353 | pgdat->node_start_pfn = start_pfn; |
| 354 | |
| 355 | pgdat->node_spanned_pages = max(old_pgdat_end_pfn, end_pfn) - |
| 356 | pgdat->node_start_pfn; |
| 357 | } |
| 358 | |
Al Viro | 3116848 | 2008-11-22 17:33:24 +0000 | [diff] [blame] | 359 | static int __meminit __add_zone(struct zone *zone, unsigned long phys_start_pfn) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 360 | { |
| 361 | struct pglist_data *pgdat = zone->zone_pgdat; |
| 362 | int nr_pages = PAGES_PER_SECTION; |
| 363 | int nid = pgdat->node_id; |
| 364 | int zone_type; |
Heiko Carstens | 76cdd58 | 2008-05-14 16:05:52 -0700 | [diff] [blame] | 365 | unsigned long flags; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 366 | |
| 367 | zone_type = zone - pgdat->node_zones; |
Heiko Carstens | 76cdd58 | 2008-05-14 16:05:52 -0700 | [diff] [blame] | 368 | if (!zone->wait_table) { |
| 369 | int ret; |
| 370 | |
| 371 | ret = init_currently_empty_zone(zone, phys_start_pfn, |
| 372 | nr_pages, MEMMAP_HOTPLUG); |
| 373 | if (ret) |
| 374 | return ret; |
| 375 | } |
| 376 | pgdat_resize_lock(zone->zone_pgdat, &flags); |
| 377 | grow_zone_span(zone, phys_start_pfn, phys_start_pfn + nr_pages); |
| 378 | grow_pgdat_span(zone->zone_pgdat, phys_start_pfn, |
| 379 | phys_start_pfn + nr_pages); |
| 380 | pgdat_resize_unlock(zone->zone_pgdat, &flags); |
Dave Hansen | a2f3aa02 | 2007-01-10 23:15:30 -0800 | [diff] [blame] | 381 | memmap_init_zone(nr_pages, nid, zone_type, |
| 382 | phys_start_pfn, MEMMAP_HOTPLUG); |
Yasunori Goto | 718127c | 2006-06-23 02:03:10 -0700 | [diff] [blame] | 383 | return 0; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 386 | static int __meminit __add_section(int nid, struct zone *zone, |
| 387 | unsigned long phys_start_pfn) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 388 | { |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 389 | int nr_pages = PAGES_PER_SECTION; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 390 | int ret; |
| 391 | |
KAMEZAWA Hiroyuki | ebd1530 | 2006-08-05 12:15:06 -0700 | [diff] [blame] | 392 | if (pfn_valid(phys_start_pfn)) |
| 393 | return -EEXIST; |
| 394 | |
Dave Hansen | 0b0acbe | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 395 | ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 396 | |
| 397 | if (ret < 0) |
| 398 | return ret; |
| 399 | |
Yasunori Goto | 718127c | 2006-06-23 02:03:10 -0700 | [diff] [blame] | 400 | ret = __add_zone(zone, phys_start_pfn); |
| 401 | |
| 402 | if (ret < 0) |
| 403 | return ret; |
| 404 | |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 405 | return register_new_memory(nid, __pfn_to_section(phys_start_pfn)); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Yasunori Goto | 0c0a4a5 | 2008-04-28 02:13:34 -0700 | [diff] [blame] | 408 | #ifdef CONFIG_SPARSEMEM_VMEMMAP |
| 409 | static int __remove_section(struct zone *zone, struct mem_section *ms) |
| 410 | { |
| 411 | /* |
| 412 | * XXX: Freeing memmap with vmemmap is not implement yet. |
| 413 | * This should be removed later. |
| 414 | */ |
| 415 | return -EBUSY; |
| 416 | } |
| 417 | #else |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 418 | static int __remove_section(struct zone *zone, struct mem_section *ms) |
| 419 | { |
| 420 | unsigned long flags; |
| 421 | struct pglist_data *pgdat = zone->zone_pgdat; |
| 422 | int ret = -EINVAL; |
| 423 | |
| 424 | if (!valid_section(ms)) |
| 425 | return ret; |
| 426 | |
| 427 | ret = unregister_memory_section(ms); |
| 428 | if (ret) |
| 429 | return ret; |
| 430 | |
| 431 | pgdat_resize_lock(pgdat, &flags); |
| 432 | sparse_remove_one_section(zone, ms); |
| 433 | pgdat_resize_unlock(pgdat, &flags); |
| 434 | return 0; |
| 435 | } |
Yasunori Goto | 0c0a4a5 | 2008-04-28 02:13:34 -0700 | [diff] [blame] | 436 | #endif |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 437 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 438 | /* |
| 439 | * Reasonably generic function for adding memory. It is |
| 440 | * expected that archs that support memory hotplug will |
| 441 | * call this function after deciding the zone to which to |
| 442 | * add the new pages. |
| 443 | */ |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 444 | int __ref __add_pages(int nid, struct zone *zone, unsigned long phys_start_pfn, |
| 445 | unsigned long nr_pages) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 446 | { |
| 447 | unsigned long i; |
| 448 | int err = 0; |
KAMEZAWA Hiroyuki | 6f71271 | 2006-08-05 12:14:58 -0700 | [diff] [blame] | 449 | int start_sec, end_sec; |
| 450 | /* during initialize mem_map, align hot-added range to section */ |
| 451 | start_sec = pfn_to_section_nr(phys_start_pfn); |
| 452 | end_sec = pfn_to_section_nr(phys_start_pfn + nr_pages - 1); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 453 | |
KAMEZAWA Hiroyuki | 6f71271 | 2006-08-05 12:14:58 -0700 | [diff] [blame] | 454 | for (i = start_sec; i <= end_sec; i++) { |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 455 | err = __add_section(nid, zone, i << PFN_SECTION_SHIFT); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 456 | |
KAMEZAWA Hiroyuki | 6f71271 | 2006-08-05 12:14:58 -0700 | [diff] [blame] | 457 | /* |
Simon Arlott | 183ff22 | 2007-10-20 01:27:18 +0200 | [diff] [blame] | 458 | * EEXIST is finally dealt with by ioresource collision |
KAMEZAWA Hiroyuki | 6f71271 | 2006-08-05 12:14:58 -0700 | [diff] [blame] | 459 | * check. see add_memory() => register_memory_resource() |
| 460 | * Warning will be printed if there is collision. |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 461 | */ |
| 462 | if (err && (err != -EEXIST)) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 463 | break; |
KAMEZAWA Hiroyuki | 6f71271 | 2006-08-05 12:14:58 -0700 | [diff] [blame] | 464 | err = 0; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | return err; |
| 468 | } |
Joel H Schopp | bed120c | 2006-05-01 12:16:11 -0700 | [diff] [blame] | 469 | EXPORT_SYMBOL_GPL(__add_pages); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 470 | |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 471 | /** |
| 472 | * __remove_pages() - remove sections of pages from a zone |
| 473 | * @zone: zone from which pages need to be removed |
| 474 | * @phys_start_pfn: starting pageframe (must be aligned to start of a section) |
| 475 | * @nr_pages: number of pages to remove (must be multiple of section size) |
| 476 | * |
| 477 | * Generic helper function to remove section mappings and sysfs entries |
| 478 | * for the section of the memory we are removing. Caller needs to make |
| 479 | * sure that pages are marked reserved and zones are adjust properly by |
| 480 | * calling offline_pages(). |
| 481 | */ |
| 482 | int __remove_pages(struct zone *zone, unsigned long phys_start_pfn, |
| 483 | unsigned long nr_pages) |
| 484 | { |
| 485 | unsigned long i, ret = 0; |
| 486 | int sections_to_remove; |
| 487 | |
| 488 | /* |
| 489 | * We can only remove entire sections |
| 490 | */ |
| 491 | BUG_ON(phys_start_pfn & ~PAGE_SECTION_MASK); |
| 492 | BUG_ON(nr_pages % PAGES_PER_SECTION); |
| 493 | |
Yasuaki Ishimatsu | d760afd | 2012-10-08 16:34:14 -0700 | [diff] [blame] | 494 | release_mem_region(phys_start_pfn << PAGE_SHIFT, nr_pages * PAGE_SIZE); |
| 495 | |
Badari Pulavarty | ea01ea9 | 2008-04-28 02:12:01 -0700 | [diff] [blame] | 496 | sections_to_remove = nr_pages / PAGES_PER_SECTION; |
| 497 | for (i = 0; i < sections_to_remove; i++) { |
| 498 | unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION; |
| 499 | ret = __remove_section(zone, __pfn_to_section(pfn)); |
| 500 | if (ret) |
| 501 | break; |
| 502 | } |
| 503 | return ret; |
| 504 | } |
| 505 | EXPORT_SYMBOL_GPL(__remove_pages); |
| 506 | |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 507 | int set_online_page_callback(online_page_callback_t callback) |
| 508 | { |
| 509 | int rc = -EINVAL; |
| 510 | |
| 511 | lock_memory_hotplug(); |
| 512 | |
| 513 | if (online_page_callback == generic_online_page) { |
| 514 | online_page_callback = callback; |
| 515 | rc = 0; |
| 516 | } |
| 517 | |
| 518 | unlock_memory_hotplug(); |
| 519 | |
| 520 | return rc; |
| 521 | } |
| 522 | EXPORT_SYMBOL_GPL(set_online_page_callback); |
| 523 | |
| 524 | int restore_online_page_callback(online_page_callback_t callback) |
| 525 | { |
| 526 | int rc = -EINVAL; |
| 527 | |
| 528 | lock_memory_hotplug(); |
| 529 | |
| 530 | if (online_page_callback == callback) { |
| 531 | online_page_callback = generic_online_page; |
| 532 | rc = 0; |
| 533 | } |
| 534 | |
| 535 | unlock_memory_hotplug(); |
| 536 | |
| 537 | return rc; |
| 538 | } |
| 539 | EXPORT_SYMBOL_GPL(restore_online_page_callback); |
| 540 | |
| 541 | void __online_page_set_limits(struct page *page) |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame] | 542 | { |
Jan Beulich | 4738e1b | 2009-09-21 17:03:03 -0700 | [diff] [blame] | 543 | unsigned long pfn = page_to_pfn(page); |
| 544 | |
Jan Beulich | 4738e1b | 2009-09-21 17:03:03 -0700 | [diff] [blame] | 545 | if (pfn >= num_physpages) |
| 546 | num_physpages = pfn + 1; |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 547 | } |
| 548 | EXPORT_SYMBOL_GPL(__online_page_set_limits); |
| 549 | |
| 550 | void __online_page_increment_counters(struct page *page) |
| 551 | { |
| 552 | totalram_pages++; |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame] | 553 | |
| 554 | #ifdef CONFIG_HIGHMEM |
| 555 | if (PageHighMem(page)) |
| 556 | totalhigh_pages++; |
| 557 | #endif |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 558 | } |
| 559 | EXPORT_SYMBOL_GPL(__online_page_increment_counters); |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame] | 560 | |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 561 | void __online_page_free(struct page *page) |
| 562 | { |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame] | 563 | ClearPageReserved(page); |
| 564 | init_page_count(page); |
| 565 | __free_page(page); |
| 566 | } |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 567 | EXPORT_SYMBOL_GPL(__online_page_free); |
| 568 | |
| 569 | static void generic_online_page(struct page *page) |
| 570 | { |
| 571 | __online_page_set_limits(page); |
| 572 | __online_page_increment_counters(page); |
| 573 | __online_page_free(page); |
| 574 | } |
Jeremy Fitzhardinge | 180c06e | 2008-04-28 02:12:03 -0700 | [diff] [blame] | 575 | |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 576 | static int online_pages_range(unsigned long start_pfn, unsigned long nr_pages, |
| 577 | void *arg) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 578 | { |
| 579 | unsigned long i; |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 580 | unsigned long onlined_pages = *(unsigned long *)arg; |
| 581 | struct page *page; |
| 582 | if (PageReserved(pfn_to_page(start_pfn))) |
| 583 | for (i = 0; i < nr_pages; i++) { |
| 584 | page = pfn_to_page(start_pfn + i); |
Daniel Kiper | 9d0ad8c | 2011-07-25 17:12:05 -0700 | [diff] [blame] | 585 | (*online_page_callback)(page); |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 586 | onlined_pages++; |
| 587 | } |
| 588 | *(unsigned long *)arg = onlined_pages; |
| 589 | return 0; |
| 590 | } |
| 591 | |
Lai Jiangshan | 09285af | 2012-12-12 13:52:04 -0800 | [diff] [blame] | 592 | #ifdef CONFIG_MOVABLE_NODE |
Tang Chen | 79a4dce | 2012-12-18 14:23:24 -0800 | [diff] [blame] | 593 | /* |
| 594 | * When CONFIG_MOVABLE_NODE, we permit onlining of a node which doesn't have |
| 595 | * normal memory. |
| 596 | */ |
Lai Jiangshan | 09285af | 2012-12-12 13:52:04 -0800 | [diff] [blame] | 597 | static bool can_online_high_movable(struct zone *zone) |
| 598 | { |
| 599 | return true; |
| 600 | } |
Tang Chen | 79a4dce | 2012-12-18 14:23:24 -0800 | [diff] [blame] | 601 | #else /* CONFIG_MOVABLE_NODE */ |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 602 | /* ensure every online node has NORMAL memory */ |
| 603 | static bool can_online_high_movable(struct zone *zone) |
| 604 | { |
| 605 | return node_state(zone_to_nid(zone), N_NORMAL_MEMORY); |
| 606 | } |
Tang Chen | 79a4dce | 2012-12-18 14:23:24 -0800 | [diff] [blame] | 607 | #endif /* CONFIG_MOVABLE_NODE */ |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 608 | |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 609 | /* check which state of node_states will be changed when online memory */ |
| 610 | static void node_states_check_changes_online(unsigned long nr_pages, |
| 611 | struct zone *zone, struct memory_notify *arg) |
| 612 | { |
| 613 | int nid = zone_to_nid(zone); |
| 614 | enum zone_type zone_last = ZONE_NORMAL; |
| 615 | |
| 616 | /* |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 617 | * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY] |
| 618 | * contains nodes which have zones of 0...ZONE_NORMAL, |
| 619 | * set zone_last to ZONE_NORMAL. |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 620 | * |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 621 | * If we don't have HIGHMEM nor movable node, |
| 622 | * node_states[N_NORMAL_MEMORY] contains nodes which have zones of |
| 623 | * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE. |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 624 | */ |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 625 | if (N_MEMORY == N_NORMAL_MEMORY) |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 626 | zone_last = ZONE_MOVABLE; |
| 627 | |
| 628 | /* |
| 629 | * if the memory to be online is in a zone of 0...zone_last, and |
| 630 | * the zones of 0...zone_last don't have memory before online, we will |
| 631 | * need to set the node to node_states[N_NORMAL_MEMORY] after |
| 632 | * the memory is online. |
| 633 | */ |
| 634 | if (zone_idx(zone) <= zone_last && !node_state(nid, N_NORMAL_MEMORY)) |
| 635 | arg->status_change_nid_normal = nid; |
| 636 | else |
| 637 | arg->status_change_nid_normal = -1; |
| 638 | |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 639 | #ifdef CONFIG_HIGHMEM |
| 640 | /* |
| 641 | * If we have movable node, node_states[N_HIGH_MEMORY] |
| 642 | * contains nodes which have zones of 0...ZONE_HIGHMEM, |
| 643 | * set zone_last to ZONE_HIGHMEM. |
| 644 | * |
| 645 | * If we don't have movable node, node_states[N_NORMAL_MEMORY] |
| 646 | * contains nodes which have zones of 0...ZONE_MOVABLE, |
| 647 | * set zone_last to ZONE_MOVABLE. |
| 648 | */ |
| 649 | zone_last = ZONE_HIGHMEM; |
| 650 | if (N_MEMORY == N_HIGH_MEMORY) |
| 651 | zone_last = ZONE_MOVABLE; |
| 652 | |
| 653 | if (zone_idx(zone) <= zone_last && !node_state(nid, N_HIGH_MEMORY)) |
| 654 | arg->status_change_nid_high = nid; |
| 655 | else |
| 656 | arg->status_change_nid_high = -1; |
| 657 | #else |
| 658 | arg->status_change_nid_high = arg->status_change_nid_normal; |
| 659 | #endif |
| 660 | |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 661 | /* |
| 662 | * if the node don't have memory befor online, we will need to |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 663 | * set the node to node_states[N_MEMORY] after the memory |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 664 | * is online. |
| 665 | */ |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 666 | if (!node_state(nid, N_MEMORY)) |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 667 | arg->status_change_nid = nid; |
| 668 | else |
| 669 | arg->status_change_nid = -1; |
| 670 | } |
| 671 | |
| 672 | static void node_states_set_node(int node, struct memory_notify *arg) |
| 673 | { |
| 674 | if (arg->status_change_nid_normal >= 0) |
| 675 | node_set_state(node, N_NORMAL_MEMORY); |
| 676 | |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 677 | if (arg->status_change_nid_high >= 0) |
| 678 | node_set_state(node, N_HIGH_MEMORY); |
| 679 | |
| 680 | node_set_state(node, N_MEMORY); |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 681 | } |
| 682 | |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 683 | |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 684 | int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type) |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 685 | { |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 686 | unsigned long onlined_pages = 0; |
| 687 | struct zone *zone; |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 688 | int need_zonelists_rebuild = 0; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 689 | int nid; |
| 690 | int ret; |
| 691 | struct memory_notify arg; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 692 | |
KAMEZAWA Hiroyuki | 925268a | 2011-01-11 16:44:01 +0900 | [diff] [blame] | 693 | lock_memory_hotplug(); |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 694 | /* |
| 695 | * This doesn't need a lock to do pfn_to_page(). |
| 696 | * The section can't be removed here because of the |
| 697 | * memory_block->state_mutex. |
| 698 | */ |
| 699 | zone = page_zone(pfn_to_page(pfn)); |
| 700 | |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 701 | if ((zone_idx(zone) > ZONE_NORMAL || online_type == ONLINE_MOVABLE) && |
| 702 | !can_online_high_movable(zone)) { |
| 703 | unlock_memory_hotplug(); |
| 704 | return -1; |
| 705 | } |
| 706 | |
Lai Jiangshan | 511c2ab | 2012-12-11 16:03:16 -0800 | [diff] [blame] | 707 | if (online_type == ONLINE_KERNEL && zone_idx(zone) == ZONE_MOVABLE) { |
| 708 | if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages)) { |
| 709 | unlock_memory_hotplug(); |
| 710 | return -1; |
| 711 | } |
| 712 | } |
| 713 | if (online_type == ONLINE_MOVABLE && zone_idx(zone) == ZONE_MOVABLE - 1) { |
| 714 | if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages)) { |
| 715 | unlock_memory_hotplug(); |
| 716 | return -1; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | /* Previous code may changed the zone of the pfn range */ |
| 721 | zone = page_zone(pfn_to_page(pfn)); |
| 722 | |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 723 | arg.start_pfn = pfn; |
| 724 | arg.nr_pages = nr_pages; |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 725 | node_states_check_changes_online(nr_pages, zone, &arg); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 726 | |
| 727 | nid = page_to_nid(pfn_to_page(pfn)); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 728 | |
| 729 | ret = memory_notify(MEM_GOING_ONLINE, &arg); |
| 730 | ret = notifier_to_errno(ret); |
| 731 | if (ret) { |
| 732 | memory_notify(MEM_CANCEL_ONLINE, &arg); |
KAMEZAWA Hiroyuki | 925268a | 2011-01-11 16:44:01 +0900 | [diff] [blame] | 733 | unlock_memory_hotplug(); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 734 | return ret; |
| 735 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 736 | /* |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 737 | * If this zone is not populated, then it is not in zonelist. |
| 738 | * This means the page allocator ignores this zone. |
| 739 | * So, zonelist must be updated after online. |
| 740 | */ |
Haicheng Li | 4eaf3f6 | 2010-05-24 14:32:52 -0700 | [diff] [blame] | 741 | mutex_lock(&zonelists_mutex); |
Wen Congyang | 6dcd73d | 2012-12-11 16:01:01 -0800 | [diff] [blame] | 742 | if (!populated_zone(zone)) { |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 743 | need_zonelists_rebuild = 1; |
Wen Congyang | 6dcd73d | 2012-12-11 16:01:01 -0800 | [diff] [blame] | 744 | build_all_zonelists(NULL, zone); |
| 745 | } |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 746 | |
KAMEZAWA Hiroyuki | 908eedc | 2009-09-22 16:45:46 -0700 | [diff] [blame] | 747 | ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages, |
KAMEZAWA Hiroyuki | 75884fb | 2007-10-16 01:26:10 -0700 | [diff] [blame] | 748 | online_pages_range); |
Geoff Levand | fd8a422 | 2008-05-14 16:05:50 -0700 | [diff] [blame] | 749 | if (ret) { |
Wen Congyang | 6dcd73d | 2012-12-11 16:01:01 -0800 | [diff] [blame] | 750 | if (need_zonelists_rebuild) |
| 751 | zone_pcp_reset(zone); |
Haicheng Li | 4eaf3f6 | 2010-05-24 14:32:52 -0700 | [diff] [blame] | 752 | mutex_unlock(&zonelists_mutex); |
Bjorn Helgaas | a62e2f4 | 2012-05-29 15:06:30 -0700 | [diff] [blame] | 753 | printk(KERN_DEBUG "online_pages [mem %#010llx-%#010llx] failed\n", |
| 754 | (unsigned long long) pfn << PAGE_SHIFT, |
| 755 | (((unsigned long long) pfn + nr_pages) |
| 756 | << PAGE_SHIFT) - 1); |
Geoff Levand | fd8a422 | 2008-05-14 16:05:50 -0700 | [diff] [blame] | 757 | memory_notify(MEM_CANCEL_ONLINE, &arg); |
KAMEZAWA Hiroyuki | 925268a | 2011-01-11 16:44:01 +0900 | [diff] [blame] | 758 | unlock_memory_hotplug(); |
Geoff Levand | fd8a422 | 2008-05-14 16:05:50 -0700 | [diff] [blame] | 759 | return ret; |
| 760 | } |
| 761 | |
Jiang Liu | 9feedc9 | 2012-12-12 13:52:12 -0800 | [diff] [blame] | 762 | zone->managed_pages += onlined_pages; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 763 | zone->present_pages += onlined_pages; |
Yasunori Goto | f2937be | 2006-03-09 17:33:51 -0800 | [diff] [blame] | 764 | zone->zone_pgdat->node_present_pages += onlined_pages; |
Jiang Liu | 08dff7b | 2012-07-31 16:43:30 -0700 | [diff] [blame] | 765 | if (onlined_pages) { |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 766 | node_states_set_node(zone_to_nid(zone), &arg); |
Jiang Liu | 08dff7b | 2012-07-31 16:43:30 -0700 | [diff] [blame] | 767 | if (need_zonelists_rebuild) |
Wen Congyang | 6dcd73d | 2012-12-11 16:01:01 -0800 | [diff] [blame] | 768 | build_all_zonelists(NULL, NULL); |
Jiang Liu | 08dff7b | 2012-07-31 16:43:30 -0700 | [diff] [blame] | 769 | else |
| 770 | zone_pcp_update(zone); |
| 771 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 772 | |
Haicheng Li | 4eaf3f6 | 2010-05-24 14:32:52 -0700 | [diff] [blame] | 773 | mutex_unlock(&zonelists_mutex); |
KOSAKI Motohiro | 1b79acc | 2011-05-24 17:11:32 -0700 | [diff] [blame] | 774 | |
| 775 | init_per_zone_wmark_min(); |
| 776 | |
Jiang Liu | 08dff7b | 2012-07-31 16:43:30 -0700 | [diff] [blame] | 777 | if (onlined_pages) |
Christoph Lameter | 7ea1530 | 2007-10-16 01:25:29 -0700 | [diff] [blame] | 778 | kswapd_run(zone_to_nid(zone)); |
Dave Hansen | 61b1399 | 2005-10-29 18:16:56 -0700 | [diff] [blame] | 779 | |
Haicheng Li | 1f52250 | 2010-05-24 14:32:51 -0700 | [diff] [blame] | 780 | vm_total_pages = nr_free_pagecache_pages(); |
Kent Liu | 2f7f24e | 2008-07-23 21:28:18 -0700 | [diff] [blame] | 781 | |
Chandra Seetharaman | 2d1d43f | 2006-09-29 02:01:25 -0700 | [diff] [blame] | 782 | writeback_set_ratelimit(); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 783 | |
| 784 | if (onlined_pages) |
| 785 | memory_notify(MEM_ONLINE, &arg); |
KAMEZAWA Hiroyuki | 925268a | 2011-01-11 16:44:01 +0900 | [diff] [blame] | 786 | unlock_memory_hotplug(); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 787 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 788 | return 0; |
| 789 | } |
Keith Mannthey | 5394702 | 2006-09-30 23:27:08 -0700 | [diff] [blame] | 790 | #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */ |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 791 | |
Hidetoshi Seto | e131933 | 2009-11-17 14:06:18 -0800 | [diff] [blame] | 792 | /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ |
| 793 | static pg_data_t __ref *hotadd_new_pgdat(int nid, u64 start) |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 794 | { |
| 795 | struct pglist_data *pgdat; |
| 796 | unsigned long zones_size[MAX_NR_ZONES] = {0}; |
| 797 | unsigned long zholes_size[MAX_NR_ZONES] = {0}; |
| 798 | unsigned long start_pfn = start >> PAGE_SHIFT; |
| 799 | |
| 800 | pgdat = arch_alloc_nodedata(nid); |
| 801 | if (!pgdat) |
| 802 | return NULL; |
| 803 | |
| 804 | arch_refresh_nodedata(nid, pgdat); |
| 805 | |
| 806 | /* we can use NODE_DATA(nid) from here */ |
| 807 | |
| 808 | /* init node's zones as empty zones, we don't have any present pages.*/ |
Johannes Weiner | 9109fb7 | 2008-07-23 21:27:20 -0700 | [diff] [blame] | 809 | free_area_init_node(nid, zones_size, start_pfn, zholes_size); |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 810 | |
KAMEZAWA Hiroyuki | 959ecc4 | 2011-06-15 15:08:38 -0700 | [diff] [blame] | 811 | /* |
| 812 | * The node we allocated has no zone fallback lists. For avoiding |
| 813 | * to access not-initialized zonelist, build here. |
| 814 | */ |
David Rientjes | f957db4 | 2011-06-22 18:13:04 -0700 | [diff] [blame] | 815 | mutex_lock(&zonelists_mutex); |
Jiang Liu | 9adb62a | 2012-07-31 16:43:28 -0700 | [diff] [blame] | 816 | build_all_zonelists(pgdat, NULL); |
David Rientjes | f957db4 | 2011-06-22 18:13:04 -0700 | [diff] [blame] | 817 | mutex_unlock(&zonelists_mutex); |
KAMEZAWA Hiroyuki | 959ecc4 | 2011-06-15 15:08:38 -0700 | [diff] [blame] | 818 | |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 819 | return pgdat; |
| 820 | } |
| 821 | |
| 822 | static void rollback_node_hotadd(int nid, pg_data_t *pgdat) |
| 823 | { |
| 824 | arch_refresh_nodedata(nid, NULL); |
| 825 | arch_free_nodedata(pgdat); |
| 826 | return; |
| 827 | } |
| 828 | |
KAMEZAWA Hiroyuki | 0a54703 | 2006-06-27 02:53:35 -0700 | [diff] [blame] | 829 | |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 830 | /* |
| 831 | * called by cpu_up() to online a node without onlined memory. |
| 832 | */ |
| 833 | int mem_online_node(int nid) |
| 834 | { |
| 835 | pg_data_t *pgdat; |
| 836 | int ret; |
| 837 | |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 838 | lock_memory_hotplug(); |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 839 | pgdat = hotadd_new_pgdat(nid, 0); |
David Rientjes | 7553e8f | 2011-06-22 18:13:01 -0700 | [diff] [blame] | 840 | if (!pgdat) { |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 841 | ret = -ENOMEM; |
| 842 | goto out; |
| 843 | } |
| 844 | node_set_online(nid); |
| 845 | ret = register_one_node(nid); |
| 846 | BUG_ON(ret); |
| 847 | |
| 848 | out: |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 849 | unlock_memory_hotplug(); |
minskey guo | cf23422 | 2010-05-24 14:32:41 -0700 | [diff] [blame] | 850 | return ret; |
| 851 | } |
| 852 | |
Al Viro | 3116848 | 2008-11-22 17:33:24 +0000 | [diff] [blame] | 853 | /* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ |
| 854 | int __ref add_memory(int nid, u64 start, u64 size) |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 855 | { |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 856 | pg_data_t *pgdat = NULL; |
| 857 | int new_pgdat = 0; |
KAMEZAWA Hiroyuki | ebd1530 | 2006-08-05 12:15:06 -0700 | [diff] [blame] | 858 | struct resource *res; |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 859 | int ret; |
| 860 | |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 861 | lock_memory_hotplug(); |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 862 | |
KAMEZAWA Hiroyuki | ebd1530 | 2006-08-05 12:15:06 -0700 | [diff] [blame] | 863 | res = register_memory_resource(start, size); |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 864 | ret = -EEXIST; |
KAMEZAWA Hiroyuki | ebd1530 | 2006-08-05 12:15:06 -0700 | [diff] [blame] | 865 | if (!res) |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 866 | goto out; |
KAMEZAWA Hiroyuki | ebd1530 | 2006-08-05 12:15:06 -0700 | [diff] [blame] | 867 | |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 868 | if (!node_online(nid)) { |
| 869 | pgdat = hotadd_new_pgdat(nid, start); |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 870 | ret = -ENOMEM; |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 871 | if (!pgdat) |
Wen Congyang | 41b9e2d | 2012-07-11 14:02:31 -0700 | [diff] [blame] | 872 | goto error; |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 873 | new_pgdat = 1; |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 876 | /* call arch's memory hotadd */ |
| 877 | ret = arch_add_memory(nid, start, size); |
| 878 | |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 879 | if (ret < 0) |
| 880 | goto error; |
| 881 | |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 882 | /* we online node here. we can't roll back from here. */ |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 883 | node_set_online(nid); |
| 884 | |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 885 | if (new_pgdat) { |
| 886 | ret = register_one_node(nid); |
| 887 | /* |
| 888 | * If sysfs file of new node can't create, cpu on the node |
| 889 | * can't be hot-added. There is no rollback way now. |
| 890 | * So, check by BUG_ON() to catch it reluctantly.. |
| 891 | */ |
| 892 | BUG_ON(ret); |
| 893 | } |
| 894 | |
akpm@linux-foundation.org | d96ae53 | 2010-03-05 13:41:58 -0800 | [diff] [blame] | 895 | /* create new memmap entry */ |
| 896 | firmware_map_add_hotplug(start, start + size, "System RAM"); |
| 897 | |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 898 | goto out; |
| 899 | |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 900 | error: |
| 901 | /* rollback pgdat allocation and others */ |
| 902 | if (new_pgdat) |
| 903 | rollback_node_hotadd(nid, pgdat); |
KAMEZAWA Hiroyuki | ebd1530 | 2006-08-05 12:15:06 -0700 | [diff] [blame] | 904 | if (res) |
| 905 | release_memory_resource(res); |
Yasunori Goto | 9af3c2d | 2006-06-27 02:53:34 -0700 | [diff] [blame] | 906 | |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 907 | out: |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 908 | unlock_memory_hotplug(); |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 909 | return ret; |
| 910 | } |
| 911 | EXPORT_SYMBOL_GPL(add_memory); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 912 | |
| 913 | #ifdef CONFIG_MEMORY_HOTREMOVE |
| 914 | /* |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 915 | * A free page on the buddy free lists (not the per-cpu lists) has PageBuddy |
| 916 | * set and the size of the free page is given by page_order(). Using this, |
| 917 | * the function determines if the pageblock contains only free pages. |
| 918 | * Due to buddy contraints, a free page at least the size of a pageblock will |
| 919 | * be located at the start of the pageblock |
| 920 | */ |
| 921 | static inline int pageblock_free(struct page *page) |
| 922 | { |
| 923 | return PageBuddy(page) && page_order(page) >= pageblock_order; |
| 924 | } |
| 925 | |
| 926 | /* Return the start of the next active pageblock after a given page */ |
| 927 | static struct page *next_active_pageblock(struct page *page) |
| 928 | { |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 929 | /* Ensure the starting page is pageblock-aligned */ |
| 930 | BUG_ON(page_to_pfn(page) & (pageblock_nr_pages - 1)); |
| 931 | |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 932 | /* If the entire pageblock is free, move to the end of free page */ |
KAMEZAWA Hiroyuki | 0dcc48c | 2010-09-09 16:38:01 -0700 | [diff] [blame] | 933 | if (pageblock_free(page)) { |
| 934 | int order; |
| 935 | /* be careful. we don't have locks, page_order can be changed.*/ |
| 936 | order = page_order(page); |
| 937 | if ((order < MAX_ORDER) && (order >= pageblock_order)) |
| 938 | return page + (1 << order); |
| 939 | } |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 940 | |
KAMEZAWA Hiroyuki | 0dcc48c | 2010-09-09 16:38:01 -0700 | [diff] [blame] | 941 | return page + pageblock_nr_pages; |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | /* Checks if this range of memory is likely to be hot-removable. */ |
| 945 | int is_mem_section_removable(unsigned long start_pfn, unsigned long nr_pages) |
| 946 | { |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 947 | struct page *page = pfn_to_page(start_pfn); |
| 948 | struct page *end_page = page + nr_pages; |
| 949 | |
| 950 | /* Check the starting page of each pageblock within the range */ |
| 951 | for (; page < end_page; page = next_active_pageblock(page)) { |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 952 | if (!is_pageblock_removable_nolock(page)) |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 953 | return 0; |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 954 | cond_resched(); |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | /* All pageblocks in the memory block are likely to be hot-removable */ |
| 958 | return 1; |
| 959 | } |
| 960 | |
| 961 | /* |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 962 | * Confirm all pages in a range [start, end) is belongs to the same zone. |
| 963 | */ |
| 964 | static int test_pages_in_a_zone(unsigned long start_pfn, unsigned long end_pfn) |
| 965 | { |
| 966 | unsigned long pfn; |
| 967 | struct zone *zone = NULL; |
| 968 | struct page *page; |
| 969 | int i; |
| 970 | for (pfn = start_pfn; |
| 971 | pfn < end_pfn; |
| 972 | pfn += MAX_ORDER_NR_PAGES) { |
| 973 | i = 0; |
| 974 | /* This is just a CONFIG_HOLES_IN_ZONE check.*/ |
| 975 | while ((i < MAX_ORDER_NR_PAGES) && !pfn_valid_within(pfn + i)) |
| 976 | i++; |
| 977 | if (i == MAX_ORDER_NR_PAGES) |
| 978 | continue; |
| 979 | page = pfn_to_page(pfn + i); |
| 980 | if (zone && page_zone(page) != zone) |
| 981 | return 0; |
| 982 | zone = page_zone(page); |
| 983 | } |
| 984 | return 1; |
| 985 | } |
| 986 | |
| 987 | /* |
| 988 | * Scanning pfn is much easier than scanning lru list. |
| 989 | * Scan pfn from start to end and Find LRU page. |
| 990 | */ |
Andrew Morton | 7bbc090 | 2010-10-26 14:22:05 -0700 | [diff] [blame] | 991 | static unsigned long scan_lru_pages(unsigned long start, unsigned long end) |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 992 | { |
| 993 | unsigned long pfn; |
| 994 | struct page *page; |
| 995 | for (pfn = start; pfn < end; pfn++) { |
| 996 | if (pfn_valid(pfn)) { |
| 997 | page = pfn_to_page(pfn); |
| 998 | if (PageLRU(page)) |
| 999 | return pfn; |
| 1000 | } |
| 1001 | } |
| 1002 | return 0; |
| 1003 | } |
| 1004 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1005 | #define NR_OFFLINE_AT_ONCE_PAGES (256) |
| 1006 | static int |
| 1007 | do_migrate_range(unsigned long start_pfn, unsigned long end_pfn) |
| 1008 | { |
| 1009 | unsigned long pfn; |
| 1010 | struct page *page; |
| 1011 | int move_pages = NR_OFFLINE_AT_ONCE_PAGES; |
| 1012 | int not_managed = 0; |
| 1013 | int ret = 0; |
| 1014 | LIST_HEAD(source); |
| 1015 | |
| 1016 | for (pfn = start_pfn; pfn < end_pfn && move_pages > 0; pfn++) { |
| 1017 | if (!pfn_valid(pfn)) |
| 1018 | continue; |
| 1019 | page = pfn_to_page(pfn); |
Konstantin Khlebnikov | 700c2a4 | 2011-05-24 17:12:19 -0700 | [diff] [blame] | 1020 | if (!get_page_unless_zero(page)) |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1021 | continue; |
| 1022 | /* |
| 1023 | * We can skip free pages. And we can only deal with pages on |
| 1024 | * LRU. |
| 1025 | */ |
Nick Piggin | 62695a8 | 2008-10-18 20:26:09 -0700 | [diff] [blame] | 1026 | ret = isolate_lru_page(page); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1027 | if (!ret) { /* Success */ |
Konstantin Khlebnikov | 700c2a4 | 2011-05-24 17:12:19 -0700 | [diff] [blame] | 1028 | put_page(page); |
Nick Piggin | 62695a8 | 2008-10-18 20:26:09 -0700 | [diff] [blame] | 1029 | list_add_tail(&page->lru, &source); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1030 | move_pages--; |
KOSAKI Motohiro | 6d9c285 | 2009-12-14 17:58:11 -0800 | [diff] [blame] | 1031 | inc_zone_page_state(page, NR_ISOLATED_ANON + |
| 1032 | page_is_file_cache(page)); |
| 1033 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1034 | } else { |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1035 | #ifdef CONFIG_DEBUG_VM |
Wu Fengguang | 718a382 | 2010-03-10 15:20:43 -0800 | [diff] [blame] | 1036 | printk(KERN_ALERT "removing pfn %lx from LRU failed\n", |
| 1037 | pfn); |
| 1038 | dump_page(page); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1039 | #endif |
Konstantin Khlebnikov | 700c2a4 | 2011-05-24 17:12:19 -0700 | [diff] [blame] | 1040 | put_page(page); |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1041 | /* Because we don't have big zone->lock. we should |
Bob Liu | 809c444 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 1042 | check this again here. */ |
| 1043 | if (page_count(page)) { |
| 1044 | not_managed++; |
Bob Liu | f3ab263 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 1045 | ret = -EBUSY; |
Bob Liu | 809c444 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 1046 | break; |
| 1047 | } |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1048 | } |
| 1049 | } |
Bob Liu | f3ab263 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 1050 | if (!list_empty(&source)) { |
| 1051 | if (not_managed) { |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1052 | putback_lru_pages(&source); |
Bob Liu | f3ab263 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 1053 | goto out; |
| 1054 | } |
Minchan Kim | 74c08f9 | 2012-10-08 16:32:54 -0700 | [diff] [blame] | 1055 | |
| 1056 | /* |
| 1057 | * alloc_migrate_target should be improooooved!! |
| 1058 | * migrate_pages returns # of failed pages. |
| 1059 | */ |
| 1060 | ret = migrate_pages(&source, alloc_migrate_target, 0, |
Mel Gorman | 7b2a2d4 | 2012-10-19 14:07:31 +0100 | [diff] [blame] | 1061 | true, MIGRATE_SYNC, |
| 1062 | MR_MEMORY_HOTPLUG); |
Bob Liu | f3ab263 | 2010-10-26 14:22:10 -0700 | [diff] [blame] | 1063 | if (ret) |
| 1064 | putback_lru_pages(&source); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1065 | } |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1066 | out: |
| 1067 | return ret; |
| 1068 | } |
| 1069 | |
| 1070 | /* |
| 1071 | * remove from free_area[] and mark all as Reserved. |
| 1072 | */ |
| 1073 | static int |
| 1074 | offline_isolated_pages_cb(unsigned long start, unsigned long nr_pages, |
| 1075 | void *data) |
| 1076 | { |
| 1077 | __offline_isolated_pages(start, start + nr_pages); |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
| 1081 | static void |
| 1082 | offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn) |
| 1083 | { |
KAMEZAWA Hiroyuki | 908eedc | 2009-09-22 16:45:46 -0700 | [diff] [blame] | 1084 | walk_system_ram_range(start_pfn, end_pfn - start_pfn, NULL, |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1085 | offline_isolated_pages_cb); |
| 1086 | } |
| 1087 | |
| 1088 | /* |
| 1089 | * Check all pages in range, recoreded as memory resource, are isolated. |
| 1090 | */ |
| 1091 | static int |
| 1092 | check_pages_isolated_cb(unsigned long start_pfn, unsigned long nr_pages, |
| 1093 | void *data) |
| 1094 | { |
| 1095 | int ret; |
| 1096 | long offlined = *(long *)data; |
Wen Congyang | b023f46 | 2012-12-11 16:00:45 -0800 | [diff] [blame] | 1097 | ret = test_pages_isolated(start_pfn, start_pfn + nr_pages, true); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1098 | offlined = nr_pages; |
| 1099 | if (!ret) |
| 1100 | *(long *)data += offlined; |
| 1101 | return ret; |
| 1102 | } |
| 1103 | |
| 1104 | static long |
| 1105 | check_pages_isolated(unsigned long start_pfn, unsigned long end_pfn) |
| 1106 | { |
| 1107 | long offlined = 0; |
| 1108 | int ret; |
| 1109 | |
KAMEZAWA Hiroyuki | 908eedc | 2009-09-22 16:45:46 -0700 | [diff] [blame] | 1110 | ret = walk_system_ram_range(start_pfn, end_pfn - start_pfn, &offlined, |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1111 | check_pages_isolated_cb); |
| 1112 | if (ret < 0) |
| 1113 | offlined = (long)ret; |
| 1114 | return offlined; |
| 1115 | } |
| 1116 | |
Lai Jiangshan | 09285af | 2012-12-12 13:52:04 -0800 | [diff] [blame] | 1117 | #ifdef CONFIG_MOVABLE_NODE |
Tang Chen | 79a4dce | 2012-12-18 14:23:24 -0800 | [diff] [blame] | 1118 | /* |
| 1119 | * When CONFIG_MOVABLE_NODE, we permit offlining of a node which doesn't have |
| 1120 | * normal memory. |
| 1121 | */ |
Lai Jiangshan | 09285af | 2012-12-12 13:52:04 -0800 | [diff] [blame] | 1122 | static bool can_offline_normal(struct zone *zone, unsigned long nr_pages) |
| 1123 | { |
| 1124 | return true; |
| 1125 | } |
Tang Chen | 79a4dce | 2012-12-18 14:23:24 -0800 | [diff] [blame] | 1126 | #else /* CONFIG_MOVABLE_NODE */ |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 1127 | /* ensure the node has NORMAL memory if it is still online */ |
| 1128 | static bool can_offline_normal(struct zone *zone, unsigned long nr_pages) |
| 1129 | { |
| 1130 | struct pglist_data *pgdat = zone->zone_pgdat; |
| 1131 | unsigned long present_pages = 0; |
| 1132 | enum zone_type zt; |
| 1133 | |
| 1134 | for (zt = 0; zt <= ZONE_NORMAL; zt++) |
| 1135 | present_pages += pgdat->node_zones[zt].present_pages; |
| 1136 | |
| 1137 | if (present_pages > nr_pages) |
| 1138 | return true; |
| 1139 | |
| 1140 | present_pages = 0; |
| 1141 | for (; zt <= ZONE_MOVABLE; zt++) |
| 1142 | present_pages += pgdat->node_zones[zt].present_pages; |
| 1143 | |
| 1144 | /* |
| 1145 | * we can't offline the last normal memory until all |
| 1146 | * higher memory is offlined. |
| 1147 | */ |
| 1148 | return present_pages == 0; |
| 1149 | } |
Tang Chen | 79a4dce | 2012-12-18 14:23:24 -0800 | [diff] [blame] | 1150 | #endif /* CONFIG_MOVABLE_NODE */ |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 1151 | |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1152 | /* check which state of node_states will be changed when offline memory */ |
| 1153 | static void node_states_check_changes_offline(unsigned long nr_pages, |
| 1154 | struct zone *zone, struct memory_notify *arg) |
| 1155 | { |
| 1156 | struct pglist_data *pgdat = zone->zone_pgdat; |
| 1157 | unsigned long present_pages = 0; |
| 1158 | enum zone_type zt, zone_last = ZONE_NORMAL; |
| 1159 | |
| 1160 | /* |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1161 | * If we have HIGHMEM or movable node, node_states[N_NORMAL_MEMORY] |
| 1162 | * contains nodes which have zones of 0...ZONE_NORMAL, |
| 1163 | * set zone_last to ZONE_NORMAL. |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1164 | * |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1165 | * If we don't have HIGHMEM nor movable node, |
| 1166 | * node_states[N_NORMAL_MEMORY] contains nodes which have zones of |
| 1167 | * 0...ZONE_MOVABLE, set zone_last to ZONE_MOVABLE. |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1168 | */ |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1169 | if (N_MEMORY == N_NORMAL_MEMORY) |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1170 | zone_last = ZONE_MOVABLE; |
| 1171 | |
| 1172 | /* |
| 1173 | * check whether node_states[N_NORMAL_MEMORY] will be changed. |
| 1174 | * If the memory to be offline is in a zone of 0...zone_last, |
| 1175 | * and it is the last present memory, 0...zone_last will |
| 1176 | * become empty after offline , thus we can determind we will |
| 1177 | * need to clear the node from node_states[N_NORMAL_MEMORY]. |
| 1178 | */ |
| 1179 | for (zt = 0; zt <= zone_last; zt++) |
| 1180 | present_pages += pgdat->node_zones[zt].present_pages; |
| 1181 | if (zone_idx(zone) <= zone_last && nr_pages >= present_pages) |
| 1182 | arg->status_change_nid_normal = zone_to_nid(zone); |
| 1183 | else |
| 1184 | arg->status_change_nid_normal = -1; |
| 1185 | |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1186 | #ifdef CONFIG_HIGHMEM |
| 1187 | /* |
| 1188 | * If we have movable node, node_states[N_HIGH_MEMORY] |
| 1189 | * contains nodes which have zones of 0...ZONE_HIGHMEM, |
| 1190 | * set zone_last to ZONE_HIGHMEM. |
| 1191 | * |
| 1192 | * If we don't have movable node, node_states[N_NORMAL_MEMORY] |
| 1193 | * contains nodes which have zones of 0...ZONE_MOVABLE, |
| 1194 | * set zone_last to ZONE_MOVABLE. |
| 1195 | */ |
| 1196 | zone_last = ZONE_HIGHMEM; |
| 1197 | if (N_MEMORY == N_HIGH_MEMORY) |
| 1198 | zone_last = ZONE_MOVABLE; |
| 1199 | |
| 1200 | for (; zt <= zone_last; zt++) |
| 1201 | present_pages += pgdat->node_zones[zt].present_pages; |
| 1202 | if (zone_idx(zone) <= zone_last && nr_pages >= present_pages) |
| 1203 | arg->status_change_nid_high = zone_to_nid(zone); |
| 1204 | else |
| 1205 | arg->status_change_nid_high = -1; |
| 1206 | #else |
| 1207 | arg->status_change_nid_high = arg->status_change_nid_normal; |
| 1208 | #endif |
| 1209 | |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1210 | /* |
| 1211 | * node_states[N_HIGH_MEMORY] contains nodes which have 0...ZONE_MOVABLE |
| 1212 | */ |
| 1213 | zone_last = ZONE_MOVABLE; |
| 1214 | |
| 1215 | /* |
| 1216 | * check whether node_states[N_HIGH_MEMORY] will be changed |
| 1217 | * If we try to offline the last present @nr_pages from the node, |
| 1218 | * we can determind we will need to clear the node from |
| 1219 | * node_states[N_HIGH_MEMORY]. |
| 1220 | */ |
| 1221 | for (; zt <= zone_last; zt++) |
| 1222 | present_pages += pgdat->node_zones[zt].present_pages; |
| 1223 | if (nr_pages >= present_pages) |
| 1224 | arg->status_change_nid = zone_to_nid(zone); |
| 1225 | else |
| 1226 | arg->status_change_nid = -1; |
| 1227 | } |
| 1228 | |
| 1229 | static void node_states_clear_node(int node, struct memory_notify *arg) |
| 1230 | { |
| 1231 | if (arg->status_change_nid_normal >= 0) |
| 1232 | node_clear_state(node, N_NORMAL_MEMORY); |
| 1233 | |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1234 | if ((N_MEMORY != N_NORMAL_MEMORY) && |
| 1235 | (arg->status_change_nid_high >= 0)) |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1236 | node_clear_state(node, N_HIGH_MEMORY); |
Lai Jiangshan | 6715ddf | 2012-12-12 13:51:49 -0800 | [diff] [blame] | 1237 | |
| 1238 | if ((N_MEMORY != N_HIGH_MEMORY) && |
| 1239 | (arg->status_change_nid >= 0)) |
| 1240 | node_clear_state(node, N_MEMORY); |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1241 | } |
| 1242 | |
Wen Congyang | a16cee1 | 2012-10-08 16:33:58 -0700 | [diff] [blame] | 1243 | static int __ref __offline_pages(unsigned long start_pfn, |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1244 | unsigned long end_pfn, unsigned long timeout) |
| 1245 | { |
| 1246 | unsigned long pfn, nr_pages, expire; |
| 1247 | long offlined_pages; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1248 | int ret, drain, retry_max, node; |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1249 | struct zone *zone; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1250 | struct memory_notify arg; |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1251 | |
| 1252 | BUG_ON(start_pfn >= end_pfn); |
| 1253 | /* at least, alignment against pageblock is necessary */ |
| 1254 | if (!IS_ALIGNED(start_pfn, pageblock_nr_pages)) |
| 1255 | return -EINVAL; |
| 1256 | if (!IS_ALIGNED(end_pfn, pageblock_nr_pages)) |
| 1257 | return -EINVAL; |
| 1258 | /* This makes hotplug much easier...and readable. |
| 1259 | we assume this for now. .*/ |
| 1260 | if (!test_pages_in_a_zone(start_pfn, end_pfn)) |
| 1261 | return -EINVAL; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1262 | |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 1263 | lock_memory_hotplug(); |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 1264 | |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1265 | zone = page_zone(pfn_to_page(start_pfn)); |
| 1266 | node = zone_to_nid(zone); |
| 1267 | nr_pages = end_pfn - start_pfn; |
| 1268 | |
Lai Jiangshan | 74d42d8 | 2012-12-11 16:03:23 -0800 | [diff] [blame] | 1269 | ret = -EINVAL; |
| 1270 | if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages)) |
| 1271 | goto out; |
| 1272 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1273 | /* set above range as isolated */ |
Wen Congyang | b023f46 | 2012-12-11 16:00:45 -0800 | [diff] [blame] | 1274 | ret = start_isolate_page_range(start_pfn, end_pfn, |
| 1275 | MIGRATE_MOVABLE, true); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1276 | if (ret) |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 1277 | goto out; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1278 | |
| 1279 | arg.start_pfn = start_pfn; |
| 1280 | arg.nr_pages = nr_pages; |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1281 | node_states_check_changes_offline(nr_pages, zone, &arg); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1282 | |
| 1283 | ret = memory_notify(MEM_GOING_OFFLINE, &arg); |
| 1284 | ret = notifier_to_errno(ret); |
| 1285 | if (ret) |
| 1286 | goto failed_removal; |
| 1287 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1288 | pfn = start_pfn; |
| 1289 | expire = jiffies + timeout; |
| 1290 | drain = 0; |
| 1291 | retry_max = 5; |
| 1292 | repeat: |
| 1293 | /* start memory hot removal */ |
| 1294 | ret = -EAGAIN; |
| 1295 | if (time_after(jiffies, expire)) |
| 1296 | goto failed_removal; |
| 1297 | ret = -EINTR; |
| 1298 | if (signal_pending(current)) |
| 1299 | goto failed_removal; |
| 1300 | ret = 0; |
| 1301 | if (drain) { |
| 1302 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1303 | cond_resched(); |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 1304 | drain_all_pages(); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | pfn = scan_lru_pages(start_pfn, end_pfn); |
| 1308 | if (pfn) { /* We have page on LRU */ |
| 1309 | ret = do_migrate_range(pfn, end_pfn); |
| 1310 | if (!ret) { |
| 1311 | drain = 1; |
| 1312 | goto repeat; |
| 1313 | } else { |
| 1314 | if (ret < 0) |
| 1315 | if (--retry_max == 0) |
| 1316 | goto failed_removal; |
| 1317 | yield(); |
| 1318 | drain = 1; |
| 1319 | goto repeat; |
| 1320 | } |
| 1321 | } |
Adam Buchbinder | b3834be | 2012-09-19 21:48:02 -0400 | [diff] [blame] | 1322 | /* drain all zone's lru pagevec, this is asynchronous... */ |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1323 | lru_add_drain_all(); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1324 | yield(); |
Adam Buchbinder | b3834be | 2012-09-19 21:48:02 -0400 | [diff] [blame] | 1325 | /* drain pcp pages, this is synchronous. */ |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 1326 | drain_all_pages(); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1327 | /* check again */ |
| 1328 | offlined_pages = check_pages_isolated(start_pfn, end_pfn); |
| 1329 | if (offlined_pages < 0) { |
| 1330 | ret = -EBUSY; |
| 1331 | goto failed_removal; |
| 1332 | } |
| 1333 | printk(KERN_INFO "Offlined Pages %ld\n", offlined_pages); |
Adam Buchbinder | b3834be | 2012-09-19 21:48:02 -0400 | [diff] [blame] | 1334 | /* Ok, all of our target is isolated. |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1335 | We cannot do rollback at this point. */ |
| 1336 | offline_isolated_pages(start_pfn, end_pfn); |
KAMEZAWA Hiroyuki | dbc0e4c | 2007-11-14 16:59:12 -0800 | [diff] [blame] | 1337 | /* reset pagetype flags and makes migrate type to be MOVABLE */ |
Michal Nazarewicz | 0815f3d | 2012-04-03 15:06:15 +0200 | [diff] [blame] | 1338 | undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1339 | /* removal success */ |
Jiang Liu | 9feedc9 | 2012-12-12 13:52:12 -0800 | [diff] [blame] | 1340 | zone->managed_pages -= offlined_pages; |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1341 | zone->present_pages -= offlined_pages; |
| 1342 | zone->zone_pgdat->node_present_pages -= offlined_pages; |
| 1343 | totalram_pages -= offlined_pages; |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1344 | |
KOSAKI Motohiro | 1b79acc | 2011-05-24 17:11:32 -0700 | [diff] [blame] | 1345 | init_per_zone_wmark_min(); |
| 1346 | |
Xishi Qiu | 1e8537b | 2012-10-08 16:31:51 -0700 | [diff] [blame] | 1347 | if (!populated_zone(zone)) { |
Jiang Liu | 340175b | 2012-07-31 16:43:32 -0700 | [diff] [blame] | 1348 | zone_pcp_reset(zone); |
Xishi Qiu | 1e8537b | 2012-10-08 16:31:51 -0700 | [diff] [blame] | 1349 | mutex_lock(&zonelists_mutex); |
| 1350 | build_all_zonelists(NULL, NULL); |
| 1351 | mutex_unlock(&zonelists_mutex); |
| 1352 | } else |
| 1353 | zone_pcp_update(zone); |
Jiang Liu | 340175b | 2012-07-31 16:43:32 -0700 | [diff] [blame] | 1354 | |
Lai Jiangshan | d971367 | 2012-12-11 16:01:03 -0800 | [diff] [blame] | 1355 | node_states_clear_node(node, &arg); |
| 1356 | if (arg.status_change_nid >= 0) |
David Rientjes | 8fe23e0 | 2009-12-14 17:58:33 -0800 | [diff] [blame] | 1357 | kswapd_stop(node); |
Minchan Kim | bce7394 | 2009-06-16 15:32:50 -0700 | [diff] [blame] | 1358 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1359 | vm_total_pages = nr_free_pagecache_pages(); |
| 1360 | writeback_set_ratelimit(); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1361 | |
| 1362 | memory_notify(MEM_OFFLINE, &arg); |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 1363 | unlock_memory_hotplug(); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1364 | return 0; |
| 1365 | |
| 1366 | failed_removal: |
Bjorn Helgaas | a62e2f4 | 2012-05-29 15:06:30 -0700 | [diff] [blame] | 1367 | printk(KERN_INFO "memory offlining [mem %#010llx-%#010llx] failed\n", |
| 1368 | (unsigned long long) start_pfn << PAGE_SHIFT, |
| 1369 | ((unsigned long long) end_pfn << PAGE_SHIFT) - 1); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1370 | memory_notify(MEM_CANCEL_OFFLINE, &arg); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1371 | /* pushback to free area */ |
Michal Nazarewicz | 0815f3d | 2012-04-03 15:06:15 +0200 | [diff] [blame] | 1372 | undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE); |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 1373 | |
Andi Kleen | 6ad696d | 2009-11-17 14:06:22 -0800 | [diff] [blame] | 1374 | out: |
KOSAKI Motohiro | 20d6c96 | 2010-12-02 14:31:19 -0800 | [diff] [blame] | 1375 | unlock_memory_hotplug(); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1376 | return ret; |
| 1377 | } |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 1378 | |
Wen Congyang | a16cee1 | 2012-10-08 16:33:58 -0700 | [diff] [blame] | 1379 | int offline_pages(unsigned long start_pfn, unsigned long nr_pages) |
| 1380 | { |
| 1381 | return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ); |
| 1382 | } |
| 1383 | |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 1384 | int remove_memory(u64 start, u64 size) |
| 1385 | { |
Wen Congyang | e90bdb7 | 2012-10-08 16:34:01 -0700 | [diff] [blame] | 1386 | struct memory_block *mem = NULL; |
| 1387 | struct mem_section *section; |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 1388 | unsigned long start_pfn, end_pfn; |
Wen Congyang | e90bdb7 | 2012-10-08 16:34:01 -0700 | [diff] [blame] | 1389 | unsigned long pfn, section_nr; |
| 1390 | int ret; |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 1391 | |
| 1392 | start_pfn = PFN_DOWN(start); |
| 1393 | end_pfn = start_pfn + PFN_DOWN(size); |
Wen Congyang | e90bdb7 | 2012-10-08 16:34:01 -0700 | [diff] [blame] | 1394 | |
| 1395 | for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { |
| 1396 | section_nr = pfn_to_section_nr(pfn); |
| 1397 | if (!present_section_nr(section_nr)) |
| 1398 | continue; |
| 1399 | |
| 1400 | section = __nr_to_section(section_nr); |
| 1401 | /* same memblock? */ |
| 1402 | if (mem) |
| 1403 | if ((section_nr >= mem->start_section_nr) && |
| 1404 | (section_nr <= mem->end_section_nr)) |
| 1405 | continue; |
| 1406 | |
| 1407 | mem = find_memory_block_hinted(section, mem); |
| 1408 | if (!mem) |
| 1409 | continue; |
| 1410 | |
| 1411 | ret = offline_memory_block(mem); |
| 1412 | if (ret) { |
| 1413 | kobject_put(&mem->dev.kobj); |
| 1414 | return ret; |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | if (mem) |
| 1419 | kobject_put(&mem->dev.kobj); |
| 1420 | |
| 1421 | return 0; |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 1422 | } |
KAMEZAWA Hiroyuki | 48e9419 | 2007-10-16 01:26:14 -0700 | [diff] [blame] | 1423 | #else |
Wen Congyang | a16cee1 | 2012-10-08 16:33:58 -0700 | [diff] [blame] | 1424 | int offline_pages(unsigned long start_pfn, unsigned long nr_pages) |
| 1425 | { |
| 1426 | return -EINVAL; |
| 1427 | } |
KAMEZAWA Hiroyuki | 48e9419 | 2007-10-16 01:26:14 -0700 | [diff] [blame] | 1428 | int remove_memory(u64 start, u64 size) |
| 1429 | { |
| 1430 | return -EINVAL; |
| 1431 | } |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 1432 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
Badari Pulavarty | 7108878 | 2008-10-18 20:25:58 -0700 | [diff] [blame] | 1433 | EXPORT_SYMBOL_GPL(remove_memory); |